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"
22 * Realtime allocator bitmap functions shared with userspace.
26 * Real time buffers need verifiers to avoid runtime warnings during IO.
27 * We don't have anything to verify, however, so these are just dummy
31 xfs_rtbuf_verify_read(
38 xfs_rtbuf_verify_write(
44 const struct xfs_buf_ops xfs_rtbuf_ops = {
46 .verify_read = xfs_rtbuf_verify_read,
47 .verify_write = xfs_rtbuf_verify_write,
50 /* Release cached rt bitmap and summary buffers. */
52 xfs_rtbuf_cache_relse(
53 struct xfs_rtalloc_args *args)
56 xfs_trans_brelse(args->tp, args->rbmbp);
58 args->rbmoff = NULLFILEOFF;
61 xfs_trans_brelse(args->tp, args->sumbp);
63 args->sumoff = NULLFILEOFF;
68 * Get a buffer for the bitmap or summary file block specified.
69 * The buffer is returned read and locked.
73 struct xfs_rtalloc_args *args,
74 xfs_fileoff_t block, /* block number in bitmap or summary */
75 int issum) /* is summary not bitmap */
77 struct xfs_mount *mp = args->mp;
78 struct xfs_buf **cbpp; /* cached block buffer */
79 xfs_fileoff_t *coffp; /* cached block number */
80 struct xfs_buf *bp; /* block buffer, result */
81 struct xfs_inode *ip; /* bitmap or summary inode */
82 struct xfs_bmbt_irec map;
89 coffp = &args->sumoff;
91 type = XFS_BLFT_RTSUMMARY_BUF;
94 coffp = &args->rbmoff;
96 type = XFS_BLFT_RTBITMAP_BUF;
100 * If we have a cached buffer, and the block number matches, use that.
102 if (*cbpp && *coffp == block)
106 * Otherwise we have to have to get the buffer. If there was an old
107 * one, get rid of it first.
110 xfs_trans_brelse(args->tp, *cbpp);
114 error = xfs_bmapi_read(ip, block, 1, &map, &nmap, 0);
118 if (XFS_IS_CORRUPT(mp, nmap == 0 || !xfs_bmap_is_written_extent(&map)))
119 return -EFSCORRUPTED;
121 ASSERT(map.br_startblock != NULLFSBLOCK);
122 error = xfs_trans_read_buf(mp, args->tp, mp->m_ddev_targp,
123 XFS_FSB_TO_DADDR(mp, map.br_startblock),
124 mp->m_bsize, 0, &bp, &xfs_rtbuf_ops);
128 xfs_trans_buf_set_type(args->tp, bp, type);
135 * Searching backward from start to limit, find the first block whose
136 * allocated/free state is different from start's.
140 struct xfs_rtalloc_args *args,
141 xfs_rtxnum_t start, /* starting rtext to look at */
142 xfs_rtxnum_t limit, /* last rtext to look at */
143 xfs_rtxnum_t *rtx) /* out: start rtext found */
145 struct xfs_mount *mp = args->mp;
146 int bit; /* bit number in the word */
147 xfs_fileoff_t block; /* bitmap block number */
148 int error; /* error value */
149 xfs_rtxnum_t firstbit; /* first useful bit in the word */
150 xfs_rtxnum_t i; /* current bit number rel. to start */
151 xfs_rtxnum_t len; /* length of inspected area */
152 xfs_rtword_t mask; /* mask of relevant bits for value */
153 xfs_rtword_t want; /* mask for "good" values */
154 xfs_rtword_t wdiff; /* difference from wanted value */
156 unsigned int word; /* word number in the buffer */
159 * Compute and read in starting bitmap block for starting block.
161 block = xfs_rtx_to_rbmblock(mp, start);
162 error = xfs_rtbitmap_read_buf(args, block);
167 * Get the first word's index & point to it.
169 word = xfs_rtx_to_rbmword(mp, start);
170 bit = (int)(start & (XFS_NBWORD - 1));
171 len = start - limit + 1;
173 * Compute match value, based on the bit at start: if 1 (free)
174 * then all-ones, else all-zeroes.
176 incore = xfs_rtbitmap_getword(args, word);
177 want = (incore & ((xfs_rtword_t)1 << bit)) ? -1 : 0;
179 * If the starting position is not word-aligned, deal with the
182 if (bit < XFS_NBWORD - 1) {
184 * Calculate first (leftmost) bit number to look at,
185 * and mask for all the relevant bits in this word.
187 firstbit = XFS_RTMAX((xfs_srtblock_t)(bit - len + 1), 0);
188 mask = (((xfs_rtword_t)1 << (bit - firstbit + 1)) - 1) <<
191 * Calculate the difference between the value there
192 * and what we're looking for.
194 if ((wdiff = (incore ^ want) & mask)) {
196 * Different. Mark where we are and return.
198 i = bit - xfs_highbit32(wdiff);
199 *rtx = start - i + 1;
202 i = bit - firstbit + 1;
204 * Go on to previous block if that's where the previous word is
205 * and we need the previous word.
207 if (--word == -1 && i < len) {
209 * If done with this block, get the previous one.
211 error = xfs_rtbitmap_read_buf(args, --block);
215 word = mp->m_blockwsize - 1;
219 * Starting on a word boundary, no partial word.
224 * Loop over whole words in buffers. When we use up one buffer
225 * we move on to the previous one.
227 while (len - i >= XFS_NBWORD) {
229 * Compute difference between actual and desired value.
231 incore = xfs_rtbitmap_getword(args, word);
232 if ((wdiff = incore ^ want)) {
234 * Different, mark where we are and return.
236 i += XFS_NBWORD - 1 - xfs_highbit32(wdiff);
237 *rtx = start - i + 1;
242 * Go on to previous block if that's where the previous word is
243 * and we need the previous word.
245 if (--word == -1 && i < len) {
247 * If done with this block, get the previous one.
249 error = xfs_rtbitmap_read_buf(args, --block);
253 word = mp->m_blockwsize - 1;
257 * If not ending on a word boundary, deal with the last
262 * Calculate first (leftmost) bit number to look at,
263 * and mask for all the relevant bits in this word.
265 firstbit = XFS_NBWORD - (len - i);
266 mask = (((xfs_rtword_t)1 << (len - i)) - 1) << firstbit;
268 * Compute difference between actual and desired value.
270 incore = xfs_rtbitmap_getword(args, word);
271 if ((wdiff = (incore ^ want) & mask)) {
273 * Different, mark where we are and return.
275 i += XFS_NBWORD - 1 - xfs_highbit32(wdiff);
276 *rtx = start - i + 1;
282 * No match, return that we scanned the whole area.
284 *rtx = start - i + 1;
289 * Searching forward from start to limit, find the first block whose
290 * allocated/free state is different from start's.
294 struct xfs_rtalloc_args *args,
295 xfs_rtxnum_t start, /* starting rtext to look at */
296 xfs_rtxnum_t limit, /* last rtext to look at */
297 xfs_rtxnum_t *rtx) /* out: start rtext found */
299 struct xfs_mount *mp = args->mp;
300 int bit; /* bit number in the word */
301 xfs_fileoff_t block; /* bitmap block number */
303 xfs_rtxnum_t i; /* current bit number rel. to start */
304 xfs_rtxnum_t lastbit;/* last useful bit in the word */
305 xfs_rtxnum_t len; /* length of inspected area */
306 xfs_rtword_t mask; /* mask of relevant bits for value */
307 xfs_rtword_t want; /* mask for "good" values */
308 xfs_rtword_t wdiff; /* difference from wanted value */
310 unsigned int word; /* word number in the buffer */
313 * Compute and read in starting bitmap block for starting block.
315 block = xfs_rtx_to_rbmblock(mp, start);
316 error = xfs_rtbitmap_read_buf(args, block);
321 * Get the first word's index & point to it.
323 word = xfs_rtx_to_rbmword(mp, start);
324 bit = (int)(start & (XFS_NBWORD - 1));
325 len = limit - start + 1;
327 * Compute match value, based on the bit at start: if 1 (free)
328 * then all-ones, else all-zeroes.
330 incore = xfs_rtbitmap_getword(args, word);
331 want = (incore & ((xfs_rtword_t)1 << bit)) ? -1 : 0;
333 * If the starting position is not word-aligned, deal with the
338 * Calculate last (rightmost) bit number to look at,
339 * and mask for all the relevant bits in this word.
341 lastbit = XFS_RTMIN(bit + len, XFS_NBWORD);
342 mask = (((xfs_rtword_t)1 << (lastbit - bit)) - 1) << bit;
344 * Calculate the difference between the value there
345 * and what we're looking for.
347 if ((wdiff = (incore ^ want) & mask)) {
349 * Different. Mark where we are and return.
351 i = xfs_lowbit32(wdiff) - bit;
352 *rtx = start + i - 1;
357 * Go on to next block if that's where the next word is
358 * and we need the next word.
360 if (++word == mp->m_blockwsize && i < len) {
362 * If done with this block, get the previous one.
364 error = xfs_rtbitmap_read_buf(args, ++block);
372 * Starting on a word boundary, no partial word.
377 * Loop over whole words in buffers. When we use up one buffer
378 * we move on to the next one.
380 while (len - i >= XFS_NBWORD) {
382 * Compute difference between actual and desired value.
384 incore = xfs_rtbitmap_getword(args, word);
385 if ((wdiff = incore ^ want)) {
387 * Different, mark where we are and return.
389 i += xfs_lowbit32(wdiff);
390 *rtx = start + i - 1;
395 * Go on to next block if that's where the next word is
396 * and we need the next word.
398 if (++word == mp->m_blockwsize && i < len) {
400 * If done with this block, get the next one.
402 error = xfs_rtbitmap_read_buf(args, ++block);
410 * If not ending on a word boundary, deal with the last
413 if ((lastbit = len - i)) {
415 * Calculate mask for all the relevant bits in this word.
417 mask = ((xfs_rtword_t)1 << lastbit) - 1;
419 * Compute difference between actual and desired value.
421 incore = xfs_rtbitmap_getword(args, word);
422 if ((wdiff = (incore ^ want) & mask)) {
424 * Different, mark where we are and return.
426 i += xfs_lowbit32(wdiff);
427 *rtx = start + i - 1;
433 * No match, return that we scanned the whole area.
435 *rtx = start + i - 1;
439 /* Log rtsummary counter at @infoword. */
441 xfs_trans_log_rtsummary(
442 struct xfs_rtalloc_args *args,
443 unsigned int infoword)
445 struct xfs_buf *bp = args->sumbp;
448 first = (void *)xfs_rsumblock_infoptr(args, infoword) - bp->b_addr;
449 last = first + sizeof(xfs_suminfo_t) - 1;
451 xfs_trans_log_buf(args->tp, bp, first, last);
455 * Modify the summary information for a given extent size, bitmap block
459 xfs_rtmodify_summary(
460 struct xfs_rtalloc_args *args,
461 int log, /* log2 of extent size */
462 xfs_fileoff_t bbno, /* bitmap block number */
463 int delta) /* in/out: summary block number */
465 struct xfs_mount *mp = args->mp;
466 xfs_rtsumoff_t so = xfs_rtsumoffs(mp, log, bbno);
467 unsigned int infoword;
471 error = xfs_rtsummary_read_buf(args, xfs_rtsumoffs_to_block(mp, so));
475 infoword = xfs_rtsumoffs_to_infoword(mp, so);
476 val = xfs_suminfo_add(args, infoword, delta);
478 if (mp->m_rsum_cache) {
479 if (val == 0 && log + 1 == mp->m_rsum_cache[bbno])
480 mp->m_rsum_cache[bbno] = log;
481 if (val != 0 && log >= mp->m_rsum_cache[bbno])
482 mp->m_rsum_cache[bbno] = log + 1;
485 xfs_trans_log_rtsummary(args, infoword);
490 * Read and return the summary information for a given extent size, bitmap block
495 struct xfs_rtalloc_args *args,
496 int log, /* log2 of extent size */
497 xfs_fileoff_t bbno, /* bitmap block number */
498 xfs_suminfo_t *sum) /* out: summary info for this block */
500 struct xfs_mount *mp = args->mp;
501 xfs_rtsumoff_t so = xfs_rtsumoffs(mp, log, bbno);
504 error = xfs_rtsummary_read_buf(args, xfs_rtsumoffs_to_block(mp, so));
506 *sum = xfs_suminfo_get(args, xfs_rtsumoffs_to_infoword(mp, so));
510 /* Log rtbitmap block from the word @from to the byte before @next. */
512 xfs_trans_log_rtbitmap(
513 struct xfs_rtalloc_args *args,
517 struct xfs_buf *bp = args->rbmbp;
520 first = (void *)xfs_rbmblock_wordptr(args, from) - bp->b_addr;
521 last = ((void *)xfs_rbmblock_wordptr(args, next) - 1) - bp->b_addr;
523 xfs_trans_log_buf(args->tp, bp, first, last);
527 * Set the given range of bitmap bits to the given value.
528 * Do whatever I/O and logging is required.
532 struct xfs_rtalloc_args *args,
533 xfs_rtxnum_t start, /* starting rtext to modify */
534 xfs_rtxlen_t len, /* length of extent to modify */
535 int val) /* 1 for free, 0 for allocated */
537 struct xfs_mount *mp = args->mp;
538 int bit; /* bit number in the word */
539 xfs_fileoff_t block; /* bitmap block number */
541 int i; /* current bit number rel. to start */
542 int lastbit; /* last useful bit in word */
543 xfs_rtword_t mask; /* mask of relevant bits for value */
545 unsigned int firstword; /* first word used in the buffer */
546 unsigned int word; /* word number in the buffer */
549 * Compute starting bitmap block number.
551 block = xfs_rtx_to_rbmblock(mp, start);
553 * Read the bitmap block, and point to its data.
555 error = xfs_rtbitmap_read_buf(args, block);
560 * Compute the starting word's address, and starting bit.
562 firstword = word = xfs_rtx_to_rbmword(mp, start);
563 bit = (int)(start & (XFS_NBWORD - 1));
565 * 0 (allocated) => all zeroes; 1 (free) => all ones.
569 * If not starting on a word boundary, deal with the first
574 * Compute first bit not changed and mask of relevant bits.
576 lastbit = XFS_RTMIN(bit + len, XFS_NBWORD);
577 mask = (((xfs_rtword_t)1 << (lastbit - bit)) - 1) << bit;
579 * Set/clear the active bits.
581 incore = xfs_rtbitmap_getword(args, word);
586 xfs_rtbitmap_setword(args, word, incore);
589 * Go on to the next block if that's where the next word is
590 * and we need the next word.
592 if (++word == mp->m_blockwsize && i < len) {
594 * Log the changed part of this block.
597 xfs_trans_log_rtbitmap(args, firstword, word);
598 error = xfs_rtbitmap_read_buf(args, ++block);
602 firstword = word = 0;
606 * Starting on a word boundary, no partial word.
611 * Loop over whole words in buffers. When we use up one buffer
612 * we move on to the next one.
614 while (len - i >= XFS_NBWORD) {
616 * Set the word value correctly.
618 xfs_rtbitmap_setword(args, word, val);
621 * Go on to the next block if that's where the next word is
622 * and we need the next word.
624 if (++word == mp->m_blockwsize && i < len) {
626 * Log the changed part of this block.
629 xfs_trans_log_rtbitmap(args, firstword, word);
630 error = xfs_rtbitmap_read_buf(args, ++block);
634 firstword = word = 0;
638 * If not ending on a word boundary, deal with the last
641 if ((lastbit = len - i)) {
643 * Compute a mask of relevant bits.
645 mask = ((xfs_rtword_t)1 << lastbit) - 1;
647 * Set/clear the active bits.
649 incore = xfs_rtbitmap_getword(args, word);
654 xfs_rtbitmap_setword(args, word, incore);
658 * Log any remaining changed bytes.
660 if (word > firstword)
661 xfs_trans_log_rtbitmap(args, firstword, word);
666 * Mark an extent specified by start and len freed.
667 * Updates all the summary information as well as the bitmap.
671 struct xfs_rtalloc_args *args,
672 xfs_rtxnum_t start, /* starting rtext to free */
673 xfs_rtxlen_t len) /* in/out: summary block number */
675 struct xfs_mount *mp = args->mp;
676 xfs_rtxnum_t end; /* end of the freed extent */
677 int error; /* error value */
678 xfs_rtxnum_t postblock; /* first rtext freed > end */
679 xfs_rtxnum_t preblock; /* first rtext freed < start */
681 end = start + len - 1;
683 * Modify the bitmap to mark this extent freed.
685 error = xfs_rtmodify_range(args, start, len, 1);
690 * Assume we're freeing out of the middle of an allocated extent.
691 * We need to find the beginning and end of the extent so we can
692 * properly update the summary.
694 error = xfs_rtfind_back(args, start, 0, &preblock);
699 * Find the next allocated block (end of allocated extent).
701 error = xfs_rtfind_forw(args, end, mp->m_sb.sb_rextents - 1,
706 * If there are blocks not being freed at the front of the
707 * old extent, add summary data for them to be allocated.
709 if (preblock < start) {
710 error = xfs_rtmodify_summary(args,
711 xfs_highbit64(start - preblock),
712 xfs_rtx_to_rbmblock(mp, preblock), -1);
718 * If there are blocks not being freed at the end of the
719 * old extent, add summary data for them to be allocated.
721 if (postblock > end) {
722 error = xfs_rtmodify_summary(args,
723 xfs_highbit64(postblock - end),
724 xfs_rtx_to_rbmblock(mp, end + 1), -1);
730 * Increment the summary information corresponding to the entire
733 return xfs_rtmodify_summary(args,
734 xfs_highbit64(postblock + 1 - preblock),
735 xfs_rtx_to_rbmblock(mp, preblock), 1);
739 * Check that the given range is either all allocated (val = 0) or
740 * all free (val = 1).
744 struct xfs_rtalloc_args *args,
745 xfs_rtxnum_t start, /* starting rtext number of extent */
746 xfs_rtxlen_t len, /* length of extent */
747 int val, /* 1 for free, 0 for allocated */
748 xfs_rtxnum_t *new, /* out: first rtext not matching */
749 int *stat) /* out: 1 for matches, 0 for not */
751 struct xfs_mount *mp = args->mp;
752 int bit; /* bit number in the word */
753 xfs_fileoff_t block; /* bitmap block number */
755 xfs_rtxnum_t i; /* current bit number rel. to start */
756 xfs_rtxnum_t lastbit; /* last useful bit in word */
757 xfs_rtword_t mask; /* mask of relevant bits for value */
758 xfs_rtword_t wdiff; /* difference from wanted value */
760 unsigned int word; /* word number in the buffer */
763 * Compute starting bitmap block number
765 block = xfs_rtx_to_rbmblock(mp, start);
767 * Read the bitmap block.
769 error = xfs_rtbitmap_read_buf(args, block);
774 * Compute the starting word's address, and starting bit.
776 word = xfs_rtx_to_rbmword(mp, start);
777 bit = (int)(start & (XFS_NBWORD - 1));
779 * 0 (allocated) => all zero's; 1 (free) => all one's.
783 * If not starting on a word boundary, deal with the first
788 * Compute first bit not examined.
790 lastbit = XFS_RTMIN(bit + len, XFS_NBWORD);
792 * Mask of relevant bits.
794 mask = (((xfs_rtword_t)1 << (lastbit - bit)) - 1) << bit;
796 * Compute difference between actual and desired value.
798 incore = xfs_rtbitmap_getword(args, word);
799 if ((wdiff = (incore ^ val) & mask)) {
801 * Different, compute first wrong bit and return.
803 i = xfs_lowbit32(wdiff) - bit;
810 * Go on to next block if that's where the next word is
811 * and we need the next word.
813 if (++word == mp->m_blockwsize && i < len) {
815 * If done with this block, get the next one.
817 error = xfs_rtbitmap_read_buf(args, ++block);
825 * Starting on a word boundary, no partial word.
830 * Loop over whole words in buffers. When we use up one buffer
831 * we move on to the next one.
833 while (len - i >= XFS_NBWORD) {
835 * Compute difference between actual and desired value.
837 incore = xfs_rtbitmap_getword(args, word);
838 if ((wdiff = incore ^ val)) {
840 * Different, compute first wrong bit and return.
842 i += xfs_lowbit32(wdiff);
849 * Go on to next block if that's where the next word is
850 * and we need the next word.
852 if (++word == mp->m_blockwsize && i < len) {
854 * If done with this block, get the next one.
856 error = xfs_rtbitmap_read_buf(args, ++block);
864 * If not ending on a word boundary, deal with the last
867 if ((lastbit = len - i)) {
869 * Mask of relevant bits.
871 mask = ((xfs_rtword_t)1 << lastbit) - 1;
873 * Compute difference between actual and desired value.
875 incore = xfs_rtbitmap_getword(args, word);
876 if ((wdiff = (incore ^ val) & mask)) {
878 * Different, compute first wrong bit and return.
880 i += xfs_lowbit32(wdiff);
888 * Successful, return.
897 * Check that the given extent (block range) is allocated already.
900 xfs_rtcheck_alloc_range(
901 struct xfs_rtalloc_args *args,
902 xfs_rtxnum_t start, /* starting rtext number of extent */
903 xfs_rtxlen_t len) /* length of extent */
905 xfs_rtxnum_t new; /* dummy for xfs_rtcheck_range */
909 error = xfs_rtcheck_range(args, start, len, 0, &new, &stat);
916 #define xfs_rtcheck_alloc_range(a,b,l) (0)
919 * Free an extent in the realtime subvolume. Length is expressed in
920 * realtime extents, as is the block number.
924 struct xfs_trans *tp, /* transaction pointer */
925 xfs_rtxnum_t start, /* starting rtext number to free */
926 xfs_rtxlen_t len) /* length of extent freed */
928 struct xfs_mount *mp = tp->t_mountp;
929 struct xfs_rtalloc_args args = {
934 struct timespec64 atime;
936 ASSERT(mp->m_rbmip->i_itemp != NULL);
937 ASSERT(xfs_isilocked(mp->m_rbmip, XFS_ILOCK_EXCL));
939 error = xfs_rtcheck_alloc_range(&args, start, len);
944 * Free the range of realtime blocks.
946 error = xfs_rtfree_range(&args, start, len);
951 * Mark more blocks free in the superblock.
953 xfs_trans_mod_sb(tp, XFS_TRANS_SB_FREXTENTS, (long)len);
955 * If we've now freed all the blocks, reset the file sequence
958 if (tp->t_frextents_delta + mp->m_sb.sb_frextents ==
959 mp->m_sb.sb_rextents) {
960 if (!(mp->m_rbmip->i_diflags & XFS_DIFLAG_NEWRTBM))
961 mp->m_rbmip->i_diflags |= XFS_DIFLAG_NEWRTBM;
963 atime = inode_get_atime(VFS_I(mp->m_rbmip));
965 inode_set_atime_to_ts(VFS_I(mp->m_rbmip), atime);
966 xfs_trans_log_inode(tp, mp->m_rbmip, XFS_ILOG_CORE);
970 xfs_rtbuf_cache_relse(&args);
975 * Free some blocks in the realtime subvolume. rtbno and rtlen are in units of
976 * rt blocks, not rt extents; must be aligned to the rt extent size; and rtlen
977 * cannot exceed XFS_MAX_BMBT_EXTLEN.
981 struct xfs_trans *tp,
985 struct xfs_mount *mp = tp->t_mountp;
990 ASSERT(rtlen <= XFS_MAX_BMBT_EXTLEN);
992 len = xfs_rtb_to_rtxrem(mp, rtlen, &mod);
998 start = xfs_rtb_to_rtxrem(mp, rtbno, &mod);
1004 return xfs_rtfree_extent(tp, start, len);
1007 /* Find all the free records within a given range. */
1009 xfs_rtalloc_query_range(
1010 struct xfs_mount *mp,
1011 struct xfs_trans *tp,
1012 const struct xfs_rtalloc_rec *low_rec,
1013 const struct xfs_rtalloc_rec *high_rec,
1014 xfs_rtalloc_query_range_fn fn,
1017 struct xfs_rtalloc_args args = {
1021 struct xfs_rtalloc_rec rec;
1022 xfs_rtxnum_t rtstart;
1024 xfs_rtxnum_t high_key;
1028 if (low_rec->ar_startext > high_rec->ar_startext)
1030 if (low_rec->ar_startext >= mp->m_sb.sb_rextents ||
1031 low_rec->ar_startext == high_rec->ar_startext)
1034 high_key = min(high_rec->ar_startext, mp->m_sb.sb_rextents - 1);
1036 /* Iterate the bitmap, looking for discrepancies. */
1037 rtstart = low_rec->ar_startext;
1038 while (rtstart <= high_key) {
1039 /* Is the first block free? */
1040 error = xfs_rtcheck_range(&args, rtstart, 1, 1, &rtend,
1045 /* How long does the extent go for? */
1046 error = xfs_rtfind_forw(&args, rtstart, high_key, &rtend);
1051 rec.ar_startext = rtstart;
1052 rec.ar_extcount = rtend - rtstart + 1;
1054 error = fn(mp, tp, &rec, priv);
1059 rtstart = rtend + 1;
1062 xfs_rtbuf_cache_relse(&args);
1066 /* Find all the free records. */
1068 xfs_rtalloc_query_all(
1069 struct xfs_mount *mp,
1070 struct xfs_trans *tp,
1071 xfs_rtalloc_query_range_fn fn,
1074 struct xfs_rtalloc_rec keys[2];
1076 keys[0].ar_startext = 0;
1077 keys[1].ar_startext = mp->m_sb.sb_rextents - 1;
1078 keys[0].ar_extcount = keys[1].ar_extcount = 0;
1080 return xfs_rtalloc_query_range(mp, tp, &keys[0], &keys[1], fn, priv);
1083 /* Is the given extent all free? */
1085 xfs_rtalloc_extent_is_free(
1086 struct xfs_mount *mp,
1087 struct xfs_trans *tp,
1092 struct xfs_rtalloc_args args = {
1100 error = xfs_rtcheck_range(&args, start, len, 1, &end, &matches);
1101 xfs_rtbuf_cache_relse(&args);
1110 * Compute the number of rtbitmap blocks needed to track the given number of rt
1114 xfs_rtbitmap_blockcount(
1115 struct xfs_mount *mp,
1116 xfs_rtbxlen_t rtextents)
1118 return howmany_64(rtextents, NBBY * mp->m_sb.sb_blocksize);
1122 * Compute the maximum level number of the realtime summary file, as defined by
1123 * mkfs. The historic use of highbit32 on a 64-bit quantity prohibited correct
1124 * use of rt volumes with more than 2^32 extents.
1127 xfs_compute_rextslog(
1128 xfs_rtbxlen_t rtextents)
1132 return xfs_highbit64(rtextents);
1136 * Compute the number of rtbitmap words needed to populate every block of a
1137 * bitmap that is large enough to track the given number of rt extents.
1140 xfs_rtbitmap_wordcount(
1141 struct xfs_mount *mp,
1142 xfs_rtbxlen_t rtextents)
1144 xfs_filblks_t blocks;
1146 blocks = xfs_rtbitmap_blockcount(mp, rtextents);
1147 return XFS_FSB_TO_B(mp, blocks) >> XFS_WORDLOG;
1150 /* Compute the number of rtsummary blocks needed to track the given rt space. */
1152 xfs_rtsummary_blockcount(
1153 struct xfs_mount *mp,
1154 unsigned int rsumlevels,
1155 xfs_extlen_t rbmblocks)
1157 unsigned long long rsumwords;
1159 rsumwords = (unsigned long long)rsumlevels * rbmblocks;
1160 return XFS_B_TO_FSB(mp, rsumwords << XFS_WORDLOG);
1164 * Compute the number of rtsummary info words needed to populate every block of
1165 * a summary file that is large enough to track the given rt space.
1168 xfs_rtsummary_wordcount(
1169 struct xfs_mount *mp,
1170 unsigned int rsumlevels,
1171 xfs_extlen_t rbmblocks)
1173 xfs_filblks_t blocks;
1175 blocks = xfs_rtsummary_blockcount(mp, rsumlevels, rbmblocks);
1176 return XFS_FSB_TO_B(mp, blocks) >> XFS_WORDLOG;