1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
8 #include "xfs_shared.h"
9 #include "xfs_format.h"
10 #include "xfs_log_format.h"
11 #include "xfs_trans_resv.h"
13 #include "xfs_mount.h"
14 #include "xfs_inode.h"
16 #include "xfs_trans.h"
17 #include "xfs_rtalloc.h"
18 #include "xfs_error.h"
19 #include "xfs_rtbitmap.h"
20 #include "xfs_health.h"
23 * Realtime allocator bitmap functions shared with userspace.
27 * Real time buffers need verifiers to avoid runtime warnings during IO.
28 * We don't have anything to verify, however, so these are just dummy
32 xfs_rtbuf_verify_read(
39 xfs_rtbuf_verify_write(
45 const struct xfs_buf_ops xfs_rtbuf_ops = {
47 .verify_read = xfs_rtbuf_verify_read,
48 .verify_write = xfs_rtbuf_verify_write,
51 /* Release cached rt bitmap and summary buffers. */
53 xfs_rtbuf_cache_relse(
54 struct xfs_rtalloc_args *args)
57 xfs_trans_brelse(args->tp, args->rbmbp);
59 args->rbmoff = NULLFILEOFF;
62 xfs_trans_brelse(args->tp, args->sumbp);
64 args->sumoff = NULLFILEOFF;
69 * Get a buffer for the bitmap or summary file block specified.
70 * The buffer is returned read and locked.
74 struct xfs_rtalloc_args *args,
75 xfs_fileoff_t block, /* block number in bitmap or summary */
76 int issum) /* is summary not bitmap */
78 struct xfs_mount *mp = args->mp;
79 struct xfs_buf **cbpp; /* cached block buffer */
80 xfs_fileoff_t *coffp; /* cached block number */
81 struct xfs_buf *bp; /* block buffer, result */
82 struct xfs_inode *ip; /* bitmap or summary inode */
83 struct xfs_bmbt_irec map;
90 coffp = &args->sumoff;
92 type = XFS_BLFT_RTSUMMARY_BUF;
95 coffp = &args->rbmoff;
97 type = XFS_BLFT_RTBITMAP_BUF;
101 * If we have a cached buffer, and the block number matches, use that.
103 if (*cbpp && *coffp == block)
107 * Otherwise we have to have to get the buffer. If there was an old
108 * one, get rid of it first.
111 xfs_trans_brelse(args->tp, *cbpp);
115 error = xfs_bmapi_read(ip, block, 1, &map, &nmap, 0);
119 if (XFS_IS_CORRUPT(mp, nmap == 0 || !xfs_bmap_is_written_extent(&map))) {
120 xfs_rt_mark_sick(mp, issum ? XFS_SICK_RT_SUMMARY :
122 return -EFSCORRUPTED;
125 ASSERT(map.br_startblock != NULLFSBLOCK);
126 error = xfs_trans_read_buf(mp, args->tp, mp->m_ddev_targp,
127 XFS_FSB_TO_DADDR(mp, map.br_startblock),
128 mp->m_bsize, 0, &bp, &xfs_rtbuf_ops);
129 if (xfs_metadata_is_sick(error))
130 xfs_rt_mark_sick(mp, issum ? XFS_SICK_RT_SUMMARY :
135 xfs_trans_buf_set_type(args->tp, bp, type);
142 * Searching backward from start to limit, find the first block whose
143 * allocated/free state is different from start's.
147 struct xfs_rtalloc_args *args,
148 xfs_rtxnum_t start, /* starting rtext to look at */
149 xfs_rtxnum_t limit, /* last rtext to look at */
150 xfs_rtxnum_t *rtx) /* out: start rtext found */
152 struct xfs_mount *mp = args->mp;
153 int bit; /* bit number in the word */
154 xfs_fileoff_t block; /* bitmap block number */
155 int error; /* error value */
156 xfs_rtxnum_t firstbit; /* first useful bit in the word */
157 xfs_rtxnum_t i; /* current bit number rel. to start */
158 xfs_rtxnum_t len; /* length of inspected area */
159 xfs_rtword_t mask; /* mask of relevant bits for value */
160 xfs_rtword_t want; /* mask for "good" values */
161 xfs_rtword_t wdiff; /* difference from wanted value */
163 unsigned int word; /* word number in the buffer */
166 * Compute and read in starting bitmap block for starting block.
168 block = xfs_rtx_to_rbmblock(mp, start);
169 error = xfs_rtbitmap_read_buf(args, block);
174 * Get the first word's index & point to it.
176 word = xfs_rtx_to_rbmword(mp, start);
177 bit = (int)(start & (XFS_NBWORD - 1));
178 len = start - limit + 1;
180 * Compute match value, based on the bit at start: if 1 (free)
181 * then all-ones, else all-zeroes.
183 incore = xfs_rtbitmap_getword(args, word);
184 want = (incore & ((xfs_rtword_t)1 << bit)) ? -1 : 0;
186 * If the starting position is not word-aligned, deal with the
189 if (bit < XFS_NBWORD - 1) {
191 * Calculate first (leftmost) bit number to look at,
192 * and mask for all the relevant bits in this word.
194 firstbit = max_t(xfs_srtblock_t, bit - len + 1, 0);
195 mask = (((xfs_rtword_t)1 << (bit - firstbit + 1)) - 1) <<
198 * Calculate the difference between the value there
199 * and what we're looking for.
201 if ((wdiff = (incore ^ want) & mask)) {
203 * Different. Mark where we are and return.
205 i = bit - xfs_highbit32(wdiff);
206 *rtx = start - i + 1;
209 i = bit - firstbit + 1;
211 * Go on to previous block if that's where the previous word is
212 * and we need the previous word.
214 if (--word == -1 && i < len) {
216 * If done with this block, get the previous one.
218 error = xfs_rtbitmap_read_buf(args, --block);
222 word = mp->m_blockwsize - 1;
226 * Starting on a word boundary, no partial word.
231 * Loop over whole words in buffers. When we use up one buffer
232 * we move on to the previous one.
234 while (len - i >= XFS_NBWORD) {
236 * Compute difference between actual and desired value.
238 incore = xfs_rtbitmap_getword(args, word);
239 if ((wdiff = incore ^ want)) {
241 * Different, mark where we are and return.
243 i += XFS_NBWORD - 1 - xfs_highbit32(wdiff);
244 *rtx = start - i + 1;
249 * Go on to previous block if that's where the previous word is
250 * and we need the previous word.
252 if (--word == -1 && i < len) {
254 * If done with this block, get the previous one.
256 error = xfs_rtbitmap_read_buf(args, --block);
260 word = mp->m_blockwsize - 1;
264 * If not ending on a word boundary, deal with the last
269 * Calculate first (leftmost) bit number to look at,
270 * and mask for all the relevant bits in this word.
272 firstbit = XFS_NBWORD - (len - i);
273 mask = (((xfs_rtword_t)1 << (len - i)) - 1) << firstbit;
275 * Compute difference between actual and desired value.
277 incore = xfs_rtbitmap_getword(args, word);
278 if ((wdiff = (incore ^ want) & mask)) {
280 * Different, mark where we are and return.
282 i += XFS_NBWORD - 1 - xfs_highbit32(wdiff);
283 *rtx = start - i + 1;
289 * No match, return that we scanned the whole area.
291 *rtx = start - i + 1;
296 * Searching forward from start to limit, find the first block whose
297 * allocated/free state is different from start's.
301 struct xfs_rtalloc_args *args,
302 xfs_rtxnum_t start, /* starting rtext to look at */
303 xfs_rtxnum_t limit, /* last rtext to look at */
304 xfs_rtxnum_t *rtx) /* out: start rtext found */
306 struct xfs_mount *mp = args->mp;
307 int bit; /* bit number in the word */
308 xfs_fileoff_t block; /* bitmap block number */
310 xfs_rtxnum_t i; /* current bit number rel. to start */
311 xfs_rtxnum_t lastbit;/* last useful bit in the word */
312 xfs_rtxnum_t len; /* length of inspected area */
313 xfs_rtword_t mask; /* mask of relevant bits for value */
314 xfs_rtword_t want; /* mask for "good" values */
315 xfs_rtword_t wdiff; /* difference from wanted value */
317 unsigned int word; /* word number in the buffer */
320 * Compute and read in starting bitmap block for starting block.
322 block = xfs_rtx_to_rbmblock(mp, start);
323 error = xfs_rtbitmap_read_buf(args, block);
328 * Get the first word's index & point to it.
330 word = xfs_rtx_to_rbmword(mp, start);
331 bit = (int)(start & (XFS_NBWORD - 1));
332 len = limit - start + 1;
334 * Compute match value, based on the bit at start: if 1 (free)
335 * then all-ones, else all-zeroes.
337 incore = xfs_rtbitmap_getword(args, word);
338 want = (incore & ((xfs_rtword_t)1 << bit)) ? -1 : 0;
340 * If the starting position is not word-aligned, deal with the
345 * Calculate last (rightmost) bit number to look at,
346 * and mask for all the relevant bits in this word.
348 lastbit = min(bit + len, XFS_NBWORD);
349 mask = (((xfs_rtword_t)1 << (lastbit - bit)) - 1) << bit;
351 * Calculate the difference between the value there
352 * and what we're looking for.
354 if ((wdiff = (incore ^ want) & mask)) {
356 * Different. Mark where we are and return.
358 i = xfs_lowbit32(wdiff) - bit;
359 *rtx = start + i - 1;
364 * Go on to next block if that's where the next word is
365 * and we need the next word.
367 if (++word == mp->m_blockwsize && i < len) {
369 * If done with this block, get the previous one.
371 error = xfs_rtbitmap_read_buf(args, ++block);
379 * Starting on a word boundary, no partial word.
384 * Loop over whole words in buffers. When we use up one buffer
385 * we move on to the next one.
387 while (len - i >= XFS_NBWORD) {
389 * Compute difference between actual and desired value.
391 incore = xfs_rtbitmap_getword(args, word);
392 if ((wdiff = incore ^ want)) {
394 * Different, mark where we are and return.
396 i += xfs_lowbit32(wdiff);
397 *rtx = start + i - 1;
402 * Go on to next block if that's where the next word is
403 * and we need the next word.
405 if (++word == mp->m_blockwsize && i < len) {
407 * If done with this block, get the next one.
409 error = xfs_rtbitmap_read_buf(args, ++block);
417 * If not ending on a word boundary, deal with the last
420 if ((lastbit = len - i)) {
422 * Calculate mask for all the relevant bits in this word.
424 mask = ((xfs_rtword_t)1 << lastbit) - 1;
426 * Compute difference between actual and desired value.
428 incore = xfs_rtbitmap_getword(args, word);
429 if ((wdiff = (incore ^ want) & mask)) {
431 * Different, mark where we are and return.
433 i += xfs_lowbit32(wdiff);
434 *rtx = start + i - 1;
440 * No match, return that we scanned the whole area.
442 *rtx = start + i - 1;
446 /* Log rtsummary counter at @infoword. */
448 xfs_trans_log_rtsummary(
449 struct xfs_rtalloc_args *args,
450 unsigned int infoword)
452 struct xfs_buf *bp = args->sumbp;
455 first = (void *)xfs_rsumblock_infoptr(args, infoword) - bp->b_addr;
456 last = first + sizeof(xfs_suminfo_t) - 1;
458 xfs_trans_log_buf(args->tp, bp, first, last);
462 * Modify the summary information for a given extent size, bitmap block
466 xfs_rtmodify_summary(
467 struct xfs_rtalloc_args *args,
468 int log, /* log2 of extent size */
469 xfs_fileoff_t bbno, /* bitmap block number */
470 int delta) /* in/out: summary block number */
472 struct xfs_mount *mp = args->mp;
473 xfs_rtsumoff_t so = xfs_rtsumoffs(mp, log, bbno);
474 unsigned int infoword;
478 error = xfs_rtsummary_read_buf(args, xfs_rtsumoffs_to_block(mp, so));
482 infoword = xfs_rtsumoffs_to_infoword(mp, so);
483 val = xfs_suminfo_add(args, infoword, delta);
485 if (mp->m_rsum_cache) {
486 if (val == 0 && log + 1 == mp->m_rsum_cache[bbno])
487 mp->m_rsum_cache[bbno] = log;
488 if (val != 0 && log >= mp->m_rsum_cache[bbno])
489 mp->m_rsum_cache[bbno] = log + 1;
492 xfs_trans_log_rtsummary(args, infoword);
497 * Read and return the summary information for a given extent size, bitmap block
502 struct xfs_rtalloc_args *args,
503 int log, /* log2 of extent size */
504 xfs_fileoff_t bbno, /* bitmap block number */
505 xfs_suminfo_t *sum) /* out: summary info for this block */
507 struct xfs_mount *mp = args->mp;
508 xfs_rtsumoff_t so = xfs_rtsumoffs(mp, log, bbno);
511 error = xfs_rtsummary_read_buf(args, xfs_rtsumoffs_to_block(mp, so));
513 *sum = xfs_suminfo_get(args, xfs_rtsumoffs_to_infoword(mp, so));
517 /* Log rtbitmap block from the word @from to the byte before @next. */
519 xfs_trans_log_rtbitmap(
520 struct xfs_rtalloc_args *args,
524 struct xfs_buf *bp = args->rbmbp;
527 first = (void *)xfs_rbmblock_wordptr(args, from) - bp->b_addr;
528 last = ((void *)xfs_rbmblock_wordptr(args, next) - 1) - bp->b_addr;
530 xfs_trans_log_buf(args->tp, bp, first, last);
534 * Set the given range of bitmap bits to the given value.
535 * Do whatever I/O and logging is required.
539 struct xfs_rtalloc_args *args,
540 xfs_rtxnum_t start, /* starting rtext to modify */
541 xfs_rtxlen_t len, /* length of extent to modify */
542 int val) /* 1 for free, 0 for allocated */
544 struct xfs_mount *mp = args->mp;
545 int bit; /* bit number in the word */
546 xfs_fileoff_t block; /* bitmap block number */
548 int i; /* current bit number rel. to start */
549 int lastbit; /* last useful bit in word */
550 xfs_rtword_t mask; /* mask of relevant bits for value */
552 unsigned int firstword; /* first word used in the buffer */
553 unsigned int word; /* word number in the buffer */
556 * Compute starting bitmap block number.
558 block = xfs_rtx_to_rbmblock(mp, start);
560 * Read the bitmap block, and point to its data.
562 error = xfs_rtbitmap_read_buf(args, block);
567 * Compute the starting word's address, and starting bit.
569 firstword = word = xfs_rtx_to_rbmword(mp, start);
570 bit = (int)(start & (XFS_NBWORD - 1));
572 * 0 (allocated) => all zeroes; 1 (free) => all ones.
576 * If not starting on a word boundary, deal with the first
581 * Compute first bit not changed and mask of relevant bits.
583 lastbit = min(bit + len, XFS_NBWORD);
584 mask = (((xfs_rtword_t)1 << (lastbit - bit)) - 1) << bit;
586 * Set/clear the active bits.
588 incore = xfs_rtbitmap_getword(args, word);
593 xfs_rtbitmap_setword(args, word, incore);
596 * Go on to the next block if that's where the next word is
597 * and we need the next word.
599 if (++word == mp->m_blockwsize && i < len) {
601 * Log the changed part of this block.
604 xfs_trans_log_rtbitmap(args, firstword, word);
605 error = xfs_rtbitmap_read_buf(args, ++block);
609 firstword = word = 0;
613 * Starting on a word boundary, no partial word.
618 * Loop over whole words in buffers. When we use up one buffer
619 * we move on to the next one.
621 while (len - i >= XFS_NBWORD) {
623 * Set the word value correctly.
625 xfs_rtbitmap_setword(args, word, val);
628 * Go on to the next block if that's where the next word is
629 * and we need the next word.
631 if (++word == mp->m_blockwsize && i < len) {
633 * Log the changed part of this block.
636 xfs_trans_log_rtbitmap(args, firstword, word);
637 error = xfs_rtbitmap_read_buf(args, ++block);
641 firstword = word = 0;
645 * If not ending on a word boundary, deal with the last
648 if ((lastbit = len - i)) {
650 * Compute a mask of relevant bits.
652 mask = ((xfs_rtword_t)1 << lastbit) - 1;
654 * Set/clear the active bits.
656 incore = xfs_rtbitmap_getword(args, word);
661 xfs_rtbitmap_setword(args, word, incore);
665 * Log any remaining changed bytes.
667 if (word > firstword)
668 xfs_trans_log_rtbitmap(args, firstword, word);
673 * Mark an extent specified by start and len freed.
674 * Updates all the summary information as well as the bitmap.
678 struct xfs_rtalloc_args *args,
679 xfs_rtxnum_t start, /* starting rtext to free */
680 xfs_rtxlen_t len) /* in/out: summary block number */
682 struct xfs_mount *mp = args->mp;
683 xfs_rtxnum_t end; /* end of the freed extent */
684 int error; /* error value */
685 xfs_rtxnum_t postblock; /* first rtext freed > end */
686 xfs_rtxnum_t preblock; /* first rtext freed < start */
688 end = start + len - 1;
690 * Modify the bitmap to mark this extent freed.
692 error = xfs_rtmodify_range(args, start, len, 1);
697 * Assume we're freeing out of the middle of an allocated extent.
698 * We need to find the beginning and end of the extent so we can
699 * properly update the summary.
701 error = xfs_rtfind_back(args, start, 0, &preblock);
706 * Find the next allocated block (end of allocated extent).
708 error = xfs_rtfind_forw(args, end, mp->m_sb.sb_rextents - 1,
713 * If there are blocks not being freed at the front of the
714 * old extent, add summary data for them to be allocated.
716 if (preblock < start) {
717 error = xfs_rtmodify_summary(args,
718 xfs_highbit64(start - preblock),
719 xfs_rtx_to_rbmblock(mp, preblock), -1);
725 * If there are blocks not being freed at the end of the
726 * old extent, add summary data for them to be allocated.
728 if (postblock > end) {
729 error = xfs_rtmodify_summary(args,
730 xfs_highbit64(postblock - end),
731 xfs_rtx_to_rbmblock(mp, end + 1), -1);
737 * Increment the summary information corresponding to the entire
740 return xfs_rtmodify_summary(args,
741 xfs_highbit64(postblock + 1 - preblock),
742 xfs_rtx_to_rbmblock(mp, preblock), 1);
746 * Check that the given range is either all allocated (val = 0) or
747 * all free (val = 1).
751 struct xfs_rtalloc_args *args,
752 xfs_rtxnum_t start, /* starting rtext number of extent */
753 xfs_rtxlen_t len, /* length of extent */
754 int val, /* 1 for free, 0 for allocated */
755 xfs_rtxnum_t *new, /* out: first rtext not matching */
756 int *stat) /* out: 1 for matches, 0 for not */
758 struct xfs_mount *mp = args->mp;
759 int bit; /* bit number in the word */
760 xfs_fileoff_t block; /* bitmap block number */
762 xfs_rtxnum_t i; /* current bit number rel. to start */
763 xfs_rtxnum_t lastbit; /* last useful bit in word */
764 xfs_rtword_t mask; /* mask of relevant bits for value */
765 xfs_rtword_t wdiff; /* difference from wanted value */
767 unsigned int word; /* word number in the buffer */
770 * Compute starting bitmap block number
772 block = xfs_rtx_to_rbmblock(mp, start);
774 * Read the bitmap block.
776 error = xfs_rtbitmap_read_buf(args, block);
781 * Compute the starting word's address, and starting bit.
783 word = xfs_rtx_to_rbmword(mp, start);
784 bit = (int)(start & (XFS_NBWORD - 1));
786 * 0 (allocated) => all zero's; 1 (free) => all one's.
790 * If not starting on a word boundary, deal with the first
795 * Compute first bit not examined.
797 lastbit = min(bit + len, XFS_NBWORD);
799 * Mask of relevant bits.
801 mask = (((xfs_rtword_t)1 << (lastbit - bit)) - 1) << bit;
803 * Compute difference between actual and desired value.
805 incore = xfs_rtbitmap_getword(args, word);
806 if ((wdiff = (incore ^ val) & mask)) {
808 * Different, compute first wrong bit and return.
810 i = xfs_lowbit32(wdiff) - bit;
817 * Go on to next block if that's where the next word is
818 * and we need the next word.
820 if (++word == mp->m_blockwsize && i < len) {
822 * If done with this block, get the next one.
824 error = xfs_rtbitmap_read_buf(args, ++block);
832 * Starting on a word boundary, no partial word.
837 * Loop over whole words in buffers. When we use up one buffer
838 * we move on to the next one.
840 while (len - i >= XFS_NBWORD) {
842 * Compute difference between actual and desired value.
844 incore = xfs_rtbitmap_getword(args, word);
845 if ((wdiff = incore ^ val)) {
847 * Different, compute first wrong bit and return.
849 i += xfs_lowbit32(wdiff);
856 * Go on to next block if that's where the next word is
857 * and we need the next word.
859 if (++word == mp->m_blockwsize && i < len) {
861 * If done with this block, get the next one.
863 error = xfs_rtbitmap_read_buf(args, ++block);
871 * If not ending on a word boundary, deal with the last
874 if ((lastbit = len - i)) {
876 * Mask of relevant bits.
878 mask = ((xfs_rtword_t)1 << lastbit) - 1;
880 * Compute difference between actual and desired value.
882 incore = xfs_rtbitmap_getword(args, word);
883 if ((wdiff = (incore ^ val) & mask)) {
885 * Different, compute first wrong bit and return.
887 i += xfs_lowbit32(wdiff);
895 * Successful, return.
904 * Check that the given extent (block range) is allocated already.
907 xfs_rtcheck_alloc_range(
908 struct xfs_rtalloc_args *args,
909 xfs_rtxnum_t start, /* starting rtext number of extent */
910 xfs_rtxlen_t len) /* length of extent */
912 xfs_rtxnum_t new; /* dummy for xfs_rtcheck_range */
916 error = xfs_rtcheck_range(args, start, len, 0, &new, &stat);
923 #define xfs_rtcheck_alloc_range(a,b,l) (0)
926 * Free an extent in the realtime subvolume. Length is expressed in
927 * realtime extents, as is the block number.
931 struct xfs_trans *tp, /* transaction pointer */
932 xfs_rtxnum_t start, /* starting rtext number to free */
933 xfs_rtxlen_t len) /* length of extent freed */
935 struct xfs_mount *mp = tp->t_mountp;
936 struct xfs_rtalloc_args args = {
941 struct timespec64 atime;
943 ASSERT(mp->m_rbmip->i_itemp != NULL);
944 xfs_assert_ilocked(mp->m_rbmip, XFS_ILOCK_EXCL);
946 error = xfs_rtcheck_alloc_range(&args, start, len);
951 * Free the range of realtime blocks.
953 error = xfs_rtfree_range(&args, start, len);
958 * Mark more blocks free in the superblock.
960 xfs_trans_mod_sb(tp, XFS_TRANS_SB_FREXTENTS, (long)len);
962 * If we've now freed all the blocks, reset the file sequence
965 if (tp->t_frextents_delta + mp->m_sb.sb_frextents ==
966 mp->m_sb.sb_rextents) {
967 if (!(mp->m_rbmip->i_diflags & XFS_DIFLAG_NEWRTBM))
968 mp->m_rbmip->i_diflags |= XFS_DIFLAG_NEWRTBM;
970 atime = inode_get_atime(VFS_I(mp->m_rbmip));
972 inode_set_atime_to_ts(VFS_I(mp->m_rbmip), atime);
973 xfs_trans_log_inode(tp, mp->m_rbmip, XFS_ILOG_CORE);
977 xfs_rtbuf_cache_relse(&args);
982 * Free some blocks in the realtime subvolume. rtbno and rtlen are in units of
983 * rt blocks, not rt extents; must be aligned to the rt extent size; and rtlen
984 * cannot exceed XFS_MAX_BMBT_EXTLEN.
988 struct xfs_trans *tp,
992 struct xfs_mount *mp = tp->t_mountp;
997 ASSERT(rtlen <= XFS_MAX_BMBT_EXTLEN);
999 len = xfs_rtb_to_rtxrem(mp, rtlen, &mod);
1005 start = xfs_rtb_to_rtxrem(mp, rtbno, &mod);
1011 return xfs_rtfree_extent(tp, start, len);
1014 /* Find all the free records within a given range. */
1016 xfs_rtalloc_query_range(
1017 struct xfs_mount *mp,
1018 struct xfs_trans *tp,
1019 const struct xfs_rtalloc_rec *low_rec,
1020 const struct xfs_rtalloc_rec *high_rec,
1021 xfs_rtalloc_query_range_fn fn,
1024 struct xfs_rtalloc_args args = {
1028 struct xfs_rtalloc_rec rec;
1029 xfs_rtxnum_t rtstart;
1031 xfs_rtxnum_t high_key;
1035 if (low_rec->ar_startext > high_rec->ar_startext)
1037 if (low_rec->ar_startext >= mp->m_sb.sb_rextents ||
1038 low_rec->ar_startext == high_rec->ar_startext)
1041 high_key = min(high_rec->ar_startext, mp->m_sb.sb_rextents - 1);
1043 /* Iterate the bitmap, looking for discrepancies. */
1044 rtstart = low_rec->ar_startext;
1045 while (rtstart <= high_key) {
1046 /* Is the first block free? */
1047 error = xfs_rtcheck_range(&args, rtstart, 1, 1, &rtend,
1052 /* How long does the extent go for? */
1053 error = xfs_rtfind_forw(&args, rtstart, high_key, &rtend);
1058 rec.ar_startext = rtstart;
1059 rec.ar_extcount = rtend - rtstart + 1;
1061 error = fn(mp, tp, &rec, priv);
1066 rtstart = rtend + 1;
1069 xfs_rtbuf_cache_relse(&args);
1073 /* Find all the free records. */
1075 xfs_rtalloc_query_all(
1076 struct xfs_mount *mp,
1077 struct xfs_trans *tp,
1078 xfs_rtalloc_query_range_fn fn,
1081 struct xfs_rtalloc_rec keys[2];
1083 keys[0].ar_startext = 0;
1084 keys[1].ar_startext = mp->m_sb.sb_rextents - 1;
1085 keys[0].ar_extcount = keys[1].ar_extcount = 0;
1087 return xfs_rtalloc_query_range(mp, tp, &keys[0], &keys[1], fn, priv);
1090 /* Is the given extent all free? */
1092 xfs_rtalloc_extent_is_free(
1093 struct xfs_mount *mp,
1094 struct xfs_trans *tp,
1099 struct xfs_rtalloc_args args = {
1107 error = xfs_rtcheck_range(&args, start, len, 1, &end, &matches);
1108 xfs_rtbuf_cache_relse(&args);
1117 * Compute the number of rtbitmap blocks needed to track the given number of rt
1121 xfs_rtbitmap_blockcount(
1122 struct xfs_mount *mp,
1123 xfs_rtbxlen_t rtextents)
1125 return howmany_64(rtextents, NBBY * mp->m_sb.sb_blocksize);
1129 * Compute the number of rtbitmap words needed to populate every block of a
1130 * bitmap that is large enough to track the given number of rt extents.
1133 xfs_rtbitmap_wordcount(
1134 struct xfs_mount *mp,
1135 xfs_rtbxlen_t rtextents)
1137 xfs_filblks_t blocks;
1139 blocks = xfs_rtbitmap_blockcount(mp, rtextents);
1140 return XFS_FSB_TO_B(mp, blocks) >> XFS_WORDLOG;
1143 /* Compute the number of rtsummary blocks needed to track the given rt space. */
1145 xfs_rtsummary_blockcount(
1146 struct xfs_mount *mp,
1147 unsigned int rsumlevels,
1148 xfs_extlen_t rbmblocks)
1150 unsigned long long rsumwords;
1152 rsumwords = (unsigned long long)rsumlevels * rbmblocks;
1153 return XFS_B_TO_FSB(mp, rsumwords << XFS_WORDLOG);
1157 * Compute the number of rtsummary info words needed to populate every block of
1158 * a summary file that is large enough to track the given rt space.
1161 xfs_rtsummary_wordcount(
1162 struct xfs_mount *mp,
1163 unsigned int rsumlevels,
1164 xfs_extlen_t rbmblocks)
1166 xfs_filblks_t blocks;
1168 blocks = xfs_rtsummary_blockcount(mp, rsumlevels, rbmblocks);
1169 return XFS_FSB_TO_B(mp, blocks) >> XFS_WORDLOG;