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_bmap_btree.h"
17 #include "xfs_trans_space.h"
18 #include "xfs_trans.h"
19 #include "xfs_rtalloc.h"
20 #include "xfs_error.h"
21 #include "xfs_rtbitmap.h"
22 #include "xfs_health.h"
24 #include "xfs_errortag.h"
26 #include "xfs_buf_item.h"
27 #include "xfs_extent_busy.h"
30 * Realtime allocator bitmap functions shared with userspace.
37 struct xfs_mount *mp = bp->b_mount;
38 struct xfs_rtbuf_blkinfo *hdr = bp->b_addr;
40 if (!xfs_verify_magic(bp, hdr->rt_magic))
41 return __this_address;
42 if (!xfs_has_rtgroups(mp))
43 return __this_address;
45 return __this_address;
46 if (!uuid_equal(&hdr->rt_uuid, &mp->m_sb.sb_meta_uuid))
47 return __this_address;
48 if (hdr->rt_blkno != cpu_to_be64(xfs_buf_daddr(bp)))
49 return __this_address;
54 xfs_rtbuf_verify_read(
57 struct xfs_mount *mp = bp->b_mount;
58 struct xfs_rtbuf_blkinfo *hdr = bp->b_addr;
61 if (!xfs_has_rtgroups(mp))
64 if (!xfs_log_check_lsn(mp, be64_to_cpu(hdr->rt_lsn))) {
69 if (!xfs_buf_verify_cksum(bp, XFS_RTBUF_CRC_OFF)) {
74 fa = xfs_rtbuf_verify(bp);
80 xfs_verifier_error(bp, -EFSCORRUPTED, fa);
84 xfs_rtbuf_verify_write(
87 struct xfs_mount *mp = bp->b_mount;
88 struct xfs_rtbuf_blkinfo *hdr = bp->b_addr;
89 struct xfs_buf_log_item *bip = bp->b_log_item;
92 if (!xfs_has_rtgroups(mp))
95 fa = xfs_rtbuf_verify(bp);
97 xfs_verifier_error(bp, -EFSCORRUPTED, fa);
102 hdr->rt_lsn = cpu_to_be64(bip->bli_item.li_lsn);
103 xfs_buf_update_cksum(bp, XFS_RTBUF_CRC_OFF);
106 const struct xfs_buf_ops xfs_rtbuf_ops = {
108 .verify_read = xfs_rtbuf_verify_read,
109 .verify_write = xfs_rtbuf_verify_write,
112 const struct xfs_buf_ops xfs_rtbitmap_buf_ops = {
113 .name = "xfs_rtbitmap",
114 .magic = { 0, cpu_to_be32(XFS_RTBITMAP_MAGIC) },
115 .verify_read = xfs_rtbuf_verify_read,
116 .verify_write = xfs_rtbuf_verify_write,
117 .verify_struct = xfs_rtbuf_verify,
120 const struct xfs_buf_ops xfs_rtsummary_buf_ops = {
121 .name = "xfs_rtsummary",
122 .magic = { 0, cpu_to_be32(XFS_RTSUMMARY_MAGIC) },
123 .verify_read = xfs_rtbuf_verify_read,
124 .verify_write = xfs_rtbuf_verify_write,
125 .verify_struct = xfs_rtbuf_verify,
128 /* Release cached rt bitmap and summary buffers. */
130 xfs_rtbuf_cache_relse(
131 struct xfs_rtalloc_args *args)
134 xfs_trans_brelse(args->tp, args->rbmbp);
136 args->rbmoff = NULLFILEOFF;
139 xfs_trans_brelse(args->tp, args->sumbp);
141 args->sumoff = NULLFILEOFF;
146 * Get a buffer for the bitmap or summary file block specified.
147 * The buffer is returned read and locked.
151 struct xfs_rtalloc_args *args,
152 xfs_fileoff_t block, /* block number in bitmap or summary */
153 enum xfs_rtg_inodes type)
155 struct xfs_inode *ip = args->rtg->rtg_inodes[type];
156 struct xfs_mount *mp = args->mp;
157 struct xfs_buf **cbpp; /* cached block buffer */
158 xfs_fileoff_t *coffp; /* cached block number */
159 struct xfs_buf *bp; /* block buffer, result */
160 struct xfs_bmbt_irec map;
161 enum xfs_blft buf_type;
166 case XFS_RTGI_SUMMARY:
168 coffp = &args->sumoff;
169 buf_type = XFS_BLFT_RTSUMMARY_BUF;
171 case XFS_RTGI_BITMAP:
173 coffp = &args->rbmoff;
174 buf_type = XFS_BLFT_RTBITMAP_BUF;
181 * If we have a cached buffer, and the block number matches, use that.
183 if (*cbpp && *coffp == block)
187 * Otherwise we have to have to get the buffer. If there was an old
188 * one, get rid of it first.
191 xfs_trans_brelse(args->tp, *cbpp);
195 error = xfs_bmapi_read(ip, block, 1, &map, &nmap, 0);
199 if (XFS_IS_CORRUPT(mp, nmap == 0 || !xfs_bmap_is_written_extent(&map))) {
200 xfs_rtginode_mark_sick(args->rtg, type);
201 return -EFSCORRUPTED;
204 ASSERT(map.br_startblock != NULLFSBLOCK);
205 error = xfs_trans_read_buf(mp, args->tp, mp->m_ddev_targp,
206 XFS_FSB_TO_DADDR(mp, map.br_startblock),
208 xfs_rtblock_ops(mp, type));
209 if (xfs_metadata_is_sick(error))
210 xfs_rtginode_mark_sick(args->rtg, type);
214 if (xfs_has_rtgroups(mp)) {
215 struct xfs_rtbuf_blkinfo *hdr = bp->b_addr;
217 if (hdr->rt_owner != cpu_to_be64(ip->i_ino)) {
218 xfs_buf_mark_corrupt(bp);
219 xfs_trans_brelse(args->tp, bp);
220 xfs_rtginode_mark_sick(args->rtg, type);
221 return -EFSCORRUPTED;
225 xfs_trans_buf_set_type(args->tp, bp, buf_type);
232 xfs_rtbitmap_read_buf(
233 struct xfs_rtalloc_args *args,
236 struct xfs_mount *mp = args->mp;
238 if (XFS_IS_CORRUPT(mp, block >= mp->m_sb.sb_rbmblocks)) {
239 xfs_rtginode_mark_sick(args->rtg, XFS_RTGI_BITMAP);
240 return -EFSCORRUPTED;
243 return xfs_rtbuf_get(args, block, XFS_RTGI_BITMAP);
247 xfs_rtsummary_read_buf(
248 struct xfs_rtalloc_args *args,
251 struct xfs_mount *mp = args->mp;
253 if (XFS_IS_CORRUPT(mp, block >= mp->m_rsumblocks)) {
254 xfs_rtginode_mark_sick(args->rtg, XFS_RTGI_SUMMARY);
255 return -EFSCORRUPTED;
257 return xfs_rtbuf_get(args, block, XFS_RTGI_SUMMARY);
261 * Searching backward from start find the first block whose allocated/free state
262 * is different from start's.
266 struct xfs_rtalloc_args *args,
267 xfs_rtxnum_t start, /* starting rtext to look at */
268 xfs_rtxnum_t *rtx) /* out: start rtext found */
270 struct xfs_mount *mp = args->mp;
271 int bit; /* bit number in the word */
272 xfs_fileoff_t block; /* bitmap block number */
273 int error; /* error value */
274 xfs_rtxnum_t firstbit; /* first useful bit in the word */
275 xfs_rtxnum_t i; /* current bit number rel. to start */
276 xfs_rtxnum_t len; /* length of inspected area */
277 xfs_rtword_t mask; /* mask of relevant bits for value */
278 xfs_rtword_t want; /* mask for "good" values */
279 xfs_rtword_t wdiff; /* difference from wanted value */
281 unsigned int word; /* word number in the buffer */
284 * Compute and read in starting bitmap block for starting block.
286 block = xfs_rtx_to_rbmblock(mp, start);
287 error = xfs_rtbitmap_read_buf(args, block);
292 * Get the first word's index & point to it.
294 word = xfs_rtx_to_rbmword(mp, start);
295 bit = (int)(start & (XFS_NBWORD - 1));
298 * Compute match value, based on the bit at start: if 1 (free)
299 * then all-ones, else all-zeroes.
301 incore = xfs_rtbitmap_getword(args, word);
302 want = (incore & ((xfs_rtword_t)1 << bit)) ? -1 : 0;
304 * If the starting position is not word-aligned, deal with the
307 if (bit < XFS_NBWORD - 1) {
309 * Calculate first (leftmost) bit number to look at,
310 * and mask for all the relevant bits in this word.
312 firstbit = max_t(xfs_srtblock_t, bit - len + 1, 0);
313 mask = (((xfs_rtword_t)1 << (bit - firstbit + 1)) - 1) <<
316 * Calculate the difference between the value there
317 * and what we're looking for.
319 if ((wdiff = (incore ^ want) & mask)) {
321 * Different. Mark where we are and return.
323 i = bit - xfs_highbit32(wdiff);
324 *rtx = start - i + 1;
327 i = bit - firstbit + 1;
329 * Go on to previous block if that's where the previous word is
330 * and we need the previous word.
332 if (--word == -1 && i < len) {
334 * If done with this block, get the previous one.
336 error = xfs_rtbitmap_read_buf(args, --block);
340 word = mp->m_blockwsize - 1;
344 * Starting on a word boundary, no partial word.
349 * Loop over whole words in buffers. When we use up one buffer
350 * we move on to the previous one.
352 while (len - i >= XFS_NBWORD) {
354 * Compute difference between actual and desired value.
356 incore = xfs_rtbitmap_getword(args, word);
357 if ((wdiff = incore ^ want)) {
359 * Different, mark where we are and return.
361 i += XFS_NBWORD - 1 - xfs_highbit32(wdiff);
362 *rtx = start - i + 1;
367 * Go on to previous block if that's where the previous word is
368 * and we need the previous word.
370 if (--word == -1 && i < len) {
372 * If done with this block, get the previous one.
374 error = xfs_rtbitmap_read_buf(args, --block);
378 word = mp->m_blockwsize - 1;
382 * If not ending on a word boundary, deal with the last
387 * Calculate first (leftmost) bit number to look at,
388 * and mask for all the relevant bits in this word.
390 firstbit = XFS_NBWORD - (len - i);
391 mask = (((xfs_rtword_t)1 << (len - i)) - 1) << firstbit;
393 * Compute difference between actual and desired value.
395 incore = xfs_rtbitmap_getword(args, word);
396 if ((wdiff = (incore ^ want) & mask)) {
398 * Different, mark where we are and return.
400 i += XFS_NBWORD - 1 - xfs_highbit32(wdiff);
401 *rtx = start - i + 1;
407 * No match, return that we scanned the whole area.
409 *rtx = start - i + 1;
414 * Searching forward from start to limit, find the first block whose
415 * allocated/free state is different from start's.
419 struct xfs_rtalloc_args *args,
420 xfs_rtxnum_t start, /* starting rtext to look at */
421 xfs_rtxnum_t limit, /* last rtext to look at */
422 xfs_rtxnum_t *rtx) /* out: start rtext found */
424 struct xfs_mount *mp = args->mp;
425 int bit; /* bit number in the word */
426 xfs_fileoff_t block; /* bitmap block number */
428 xfs_rtxnum_t i; /* current bit number rel. to start */
429 xfs_rtxnum_t lastbit;/* last useful bit in the word */
430 xfs_rtxnum_t len; /* length of inspected area */
431 xfs_rtword_t mask; /* mask of relevant bits for value */
432 xfs_rtword_t want; /* mask for "good" values */
433 xfs_rtword_t wdiff; /* difference from wanted value */
435 unsigned int word; /* word number in the buffer */
437 ASSERT(start <= limit);
440 * Compute and read in starting bitmap block for starting block.
442 block = xfs_rtx_to_rbmblock(mp, start);
443 error = xfs_rtbitmap_read_buf(args, block);
448 * Get the first word's index & point to it.
450 word = xfs_rtx_to_rbmword(mp, start);
451 bit = (int)(start & (XFS_NBWORD - 1));
452 len = limit - start + 1;
454 * Compute match value, based on the bit at start: if 1 (free)
455 * then all-ones, else all-zeroes.
457 incore = xfs_rtbitmap_getword(args, word);
458 want = (incore & ((xfs_rtword_t)1 << bit)) ? -1 : 0;
460 * If the starting position is not word-aligned, deal with the
465 * Calculate last (rightmost) bit number to look at,
466 * and mask for all the relevant bits in this word.
468 lastbit = min(bit + len, XFS_NBWORD);
469 mask = (((xfs_rtword_t)1 << (lastbit - bit)) - 1) << bit;
471 * Calculate the difference between the value there
472 * and what we're looking for.
474 if ((wdiff = (incore ^ want) & mask)) {
476 * Different. Mark where we are and return.
478 i = xfs_lowbit32(wdiff) - bit;
479 *rtx = start + i - 1;
484 * Go on to next block if that's where the next word is
485 * and we need the next word.
487 if (++word == mp->m_blockwsize && i < len) {
489 * If done with this block, get the previous one.
491 error = xfs_rtbitmap_read_buf(args, ++block);
499 * Starting on a word boundary, no partial word.
504 * Loop over whole words in buffers. When we use up one buffer
505 * we move on to the next one.
507 while (len - i >= XFS_NBWORD) {
509 * Compute difference between actual and desired value.
511 incore = xfs_rtbitmap_getword(args, word);
512 if ((wdiff = incore ^ want)) {
514 * Different, mark where we are and return.
516 i += xfs_lowbit32(wdiff);
517 *rtx = start + i - 1;
522 * Go on to next block if that's where the next word is
523 * and we need the next word.
525 if (++word == mp->m_blockwsize && i < len) {
527 * If done with this block, get the next one.
529 error = xfs_rtbitmap_read_buf(args, ++block);
537 * If not ending on a word boundary, deal with the last
540 if ((lastbit = len - i)) {
542 * Calculate mask for all the relevant bits in this word.
544 mask = ((xfs_rtword_t)1 << lastbit) - 1;
546 * Compute difference between actual and desired value.
548 incore = xfs_rtbitmap_getword(args, word);
549 if ((wdiff = (incore ^ want) & mask)) {
551 * Different, mark where we are and return.
553 i += xfs_lowbit32(wdiff);
554 *rtx = start + i - 1;
560 * No match, return that we scanned the whole area.
562 *rtx = start + i - 1;
566 /* Log rtsummary counter at @infoword. */
568 xfs_trans_log_rtsummary(
569 struct xfs_rtalloc_args *args,
570 unsigned int infoword)
572 struct xfs_buf *bp = args->sumbp;
575 first = (void *)xfs_rsumblock_infoptr(args, infoword) - bp->b_addr;
576 last = first + sizeof(xfs_suminfo_t) - 1;
578 xfs_trans_log_buf(args->tp, bp, first, last);
582 * Modify the summary information for a given extent size, bitmap block
586 xfs_rtmodify_summary(
587 struct xfs_rtalloc_args *args,
588 int log, /* log2 of extent size */
589 xfs_fileoff_t bbno, /* bitmap block number */
590 int delta) /* in/out: summary block number */
592 struct xfs_mount *mp = args->mp;
593 xfs_rtsumoff_t so = xfs_rtsumoffs(mp, log, bbno);
594 uint8_t *rsum_cache = args->rtg->rtg_rsum_cache;
595 unsigned int infoword;
599 error = xfs_rtsummary_read_buf(args, xfs_rtsumoffs_to_block(mp, so));
603 infoword = xfs_rtsumoffs_to_infoword(mp, so);
604 val = xfs_suminfo_add(args, infoword, delta);
607 if (val == 0 && log + 1 == rsum_cache[bbno])
608 rsum_cache[bbno] = log;
609 if (val != 0 && log >= rsum_cache[bbno])
610 rsum_cache[bbno] = log + 1;
613 xfs_trans_log_rtsummary(args, infoword);
618 * Read and return the summary information for a given extent size, bitmap block
623 struct xfs_rtalloc_args *args,
624 int log, /* log2 of extent size */
625 xfs_fileoff_t bbno, /* bitmap block number */
626 xfs_suminfo_t *sum) /* out: summary info for this block */
628 struct xfs_mount *mp = args->mp;
629 xfs_rtsumoff_t so = xfs_rtsumoffs(mp, log, bbno);
632 error = xfs_rtsummary_read_buf(args, xfs_rtsumoffs_to_block(mp, so));
634 *sum = xfs_suminfo_get(args, xfs_rtsumoffs_to_infoword(mp, so));
638 /* Log rtbitmap block from the word @from to the byte before @next. */
640 xfs_trans_log_rtbitmap(
641 struct xfs_rtalloc_args *args,
645 struct xfs_buf *bp = args->rbmbp;
648 first = (void *)xfs_rbmblock_wordptr(args, from) - bp->b_addr;
649 last = ((void *)xfs_rbmblock_wordptr(args, next) - 1) - bp->b_addr;
651 xfs_trans_log_buf(args->tp, bp, first, last);
655 * Set the given range of bitmap bits to the given value.
656 * Do whatever I/O and logging is required.
660 struct xfs_rtalloc_args *args,
661 xfs_rtxnum_t start, /* starting rtext to modify */
662 xfs_rtxlen_t len, /* length of extent to modify */
663 int val) /* 1 for free, 0 for allocated */
665 struct xfs_mount *mp = args->mp;
666 int bit; /* bit number in the word */
667 xfs_fileoff_t block; /* bitmap block number */
669 int i; /* current bit number rel. to start */
670 int lastbit; /* last useful bit in word */
671 xfs_rtword_t mask; /* mask of relevant bits for value */
673 unsigned int firstword; /* first word used in the buffer */
674 unsigned int word; /* word number in the buffer */
677 * Compute starting bitmap block number.
679 block = xfs_rtx_to_rbmblock(mp, start);
681 * Read the bitmap block, and point to its data.
683 error = xfs_rtbitmap_read_buf(args, block);
688 * Compute the starting word's address, and starting bit.
690 firstword = word = xfs_rtx_to_rbmword(mp, start);
691 bit = (int)(start & (XFS_NBWORD - 1));
693 * 0 (allocated) => all zeroes; 1 (free) => all ones.
697 * If not starting on a word boundary, deal with the first
702 * Compute first bit not changed and mask of relevant bits.
704 lastbit = min(bit + len, XFS_NBWORD);
705 mask = (((xfs_rtword_t)1 << (lastbit - bit)) - 1) << bit;
707 * Set/clear the active bits.
709 incore = xfs_rtbitmap_getword(args, word);
714 xfs_rtbitmap_setword(args, word, incore);
717 * Go on to the next block if that's where the next word is
718 * and we need the next word.
720 if (++word == mp->m_blockwsize && i < len) {
722 * Log the changed part of this block.
725 xfs_trans_log_rtbitmap(args, firstword, word);
726 error = xfs_rtbitmap_read_buf(args, ++block);
730 firstword = word = 0;
734 * Starting on a word boundary, no partial word.
739 * Loop over whole words in buffers. When we use up one buffer
740 * we move on to the next one.
742 while (len - i >= XFS_NBWORD) {
744 * Set the word value correctly.
746 xfs_rtbitmap_setword(args, word, val);
749 * Go on to the next block if that's where the next word is
750 * and we need the next word.
752 if (++word == mp->m_blockwsize && i < len) {
754 * Log the changed part of this block.
757 xfs_trans_log_rtbitmap(args, firstword, word);
758 error = xfs_rtbitmap_read_buf(args, ++block);
762 firstword = word = 0;
766 * If not ending on a word boundary, deal with the last
769 if ((lastbit = len - i)) {
771 * Compute a mask of relevant bits.
773 mask = ((xfs_rtword_t)1 << lastbit) - 1;
775 * Set/clear the active bits.
777 incore = xfs_rtbitmap_getword(args, word);
782 xfs_rtbitmap_setword(args, word, incore);
786 * Log any remaining changed bytes.
788 if (word > firstword)
789 xfs_trans_log_rtbitmap(args, firstword, word);
794 * Mark an extent specified by start and len freed.
795 * Updates all the summary information as well as the bitmap.
799 struct xfs_rtalloc_args *args,
800 xfs_rtxnum_t start, /* starting rtext to free */
801 xfs_rtxlen_t len) /* in/out: summary block number */
803 struct xfs_mount *mp = args->mp;
804 xfs_rtxnum_t end; /* end of the freed extent */
805 int error; /* error value */
806 xfs_rtxnum_t postblock; /* first rtext freed > end */
807 xfs_rtxnum_t preblock; /* first rtext freed < start */
809 end = start + len - 1;
811 * Modify the bitmap to mark this extent freed.
813 error = xfs_rtmodify_range(args, start, len, 1);
818 * Assume we're freeing out of the middle of an allocated extent.
819 * We need to find the beginning and end of the extent so we can
820 * properly update the summary.
822 error = xfs_rtfind_back(args, start, &preblock);
827 * Find the next allocated block (end of allocated extent).
829 error = xfs_rtfind_forw(args, end, args->rtg->rtg_extents - 1,
834 * If there are blocks not being freed at the front of the
835 * old extent, add summary data for them to be allocated.
837 if (preblock < start) {
838 error = xfs_rtmodify_summary(args,
839 xfs_highbit64(start - preblock),
840 xfs_rtx_to_rbmblock(mp, preblock), -1);
846 * If there are blocks not being freed at the end of the
847 * old extent, add summary data for them to be allocated.
849 if (postblock > end) {
850 error = xfs_rtmodify_summary(args,
851 xfs_highbit64(postblock - end),
852 xfs_rtx_to_rbmblock(mp, end + 1), -1);
858 * Increment the summary information corresponding to the entire
861 return xfs_rtmodify_summary(args,
862 xfs_highbit64(postblock + 1 - preblock),
863 xfs_rtx_to_rbmblock(mp, preblock), 1);
867 * Check that the given range is either all allocated (val = 0) or
868 * all free (val = 1).
872 struct xfs_rtalloc_args *args,
873 xfs_rtxnum_t start, /* starting rtext number of extent */
874 xfs_rtxlen_t len, /* length of extent */
875 int val, /* 1 for free, 0 for allocated */
876 xfs_rtxnum_t *new, /* out: first rtext not matching */
877 int *stat) /* out: 1 for matches, 0 for not */
879 struct xfs_mount *mp = args->mp;
880 int bit; /* bit number in the word */
881 xfs_fileoff_t block; /* bitmap block number */
883 xfs_rtxnum_t i; /* current bit number rel. to start */
884 xfs_rtxnum_t lastbit; /* last useful bit in word */
885 xfs_rtword_t mask; /* mask of relevant bits for value */
886 xfs_rtword_t wdiff; /* difference from wanted value */
888 unsigned int word; /* word number in the buffer */
891 * Compute starting bitmap block number
893 block = xfs_rtx_to_rbmblock(mp, start);
895 * Read the bitmap block.
897 error = xfs_rtbitmap_read_buf(args, block);
902 * Compute the starting word's address, and starting bit.
904 word = xfs_rtx_to_rbmword(mp, start);
905 bit = (int)(start & (XFS_NBWORD - 1));
907 * 0 (allocated) => all zero's; 1 (free) => all one's.
911 * If not starting on a word boundary, deal with the first
916 * Compute first bit not examined.
918 lastbit = min(bit + len, XFS_NBWORD);
920 * Mask of relevant bits.
922 mask = (((xfs_rtword_t)1 << (lastbit - bit)) - 1) << bit;
924 * Compute difference between actual and desired value.
926 incore = xfs_rtbitmap_getword(args, word);
927 if ((wdiff = (incore ^ val) & mask)) {
929 * Different, compute first wrong bit and return.
931 i = xfs_lowbit32(wdiff) - bit;
938 * Go on to next block if that's where the next word is
939 * and we need the next word.
941 if (++word == mp->m_blockwsize && i < len) {
943 * If done with this block, get the next one.
945 error = xfs_rtbitmap_read_buf(args, ++block);
953 * Starting on a word boundary, no partial word.
958 * Loop over whole words in buffers. When we use up one buffer
959 * we move on to the next one.
961 while (len - i >= XFS_NBWORD) {
963 * Compute difference between actual and desired value.
965 incore = xfs_rtbitmap_getword(args, word);
966 if ((wdiff = incore ^ val)) {
968 * Different, compute first wrong bit and return.
970 i += xfs_lowbit32(wdiff);
977 * Go on to next block if that's where the next word is
978 * and we need the next word.
980 if (++word == mp->m_blockwsize && i < len) {
982 * If done with this block, get the next one.
984 error = xfs_rtbitmap_read_buf(args, ++block);
992 * If not ending on a word boundary, deal with the last
995 if ((lastbit = len - i)) {
997 * Mask of relevant bits.
999 mask = ((xfs_rtword_t)1 << lastbit) - 1;
1001 * Compute difference between actual and desired value.
1003 incore = xfs_rtbitmap_getword(args, word);
1004 if ((wdiff = (incore ^ val) & mask)) {
1006 * Different, compute first wrong bit and return.
1008 i += xfs_lowbit32(wdiff);
1016 * Successful, return.
1025 * Check that the given extent (block range) is allocated already.
1028 xfs_rtcheck_alloc_range(
1029 struct xfs_rtalloc_args *args,
1030 xfs_rtxnum_t start, /* starting rtext number of extent */
1031 xfs_rtxlen_t len) /* length of extent */
1033 xfs_rtxnum_t new; /* dummy for xfs_rtcheck_range */
1037 error = xfs_rtcheck_range(args, start, len, 0, &new, &stat);
1044 #define xfs_rtcheck_alloc_range(a,b,l) (0)
1047 * Free an extent in the realtime subvolume. Length is expressed in
1048 * realtime extents, as is the block number.
1052 struct xfs_trans *tp, /* transaction pointer */
1053 struct xfs_rtgroup *rtg,
1054 xfs_rtxnum_t start, /* starting rtext number to free */
1055 xfs_rtxlen_t len) /* length of extent freed */
1057 struct xfs_mount *mp = tp->t_mountp;
1058 struct xfs_inode *rbmip = rtg->rtg_inodes[XFS_RTGI_BITMAP];
1059 struct xfs_rtalloc_args args = {
1065 struct timespec64 atime;
1067 ASSERT(rbmip->i_itemp != NULL);
1068 xfs_assert_ilocked(rbmip, XFS_ILOCK_EXCL);
1070 if (XFS_TEST_ERROR(false, mp, XFS_ERRTAG_FREE_EXTENT))
1073 error = xfs_rtcheck_alloc_range(&args, start, len);
1078 * Free the range of realtime blocks.
1080 error = xfs_rtfree_range(&args, start, len);
1085 * Mark more blocks free in the superblock.
1087 xfs_trans_mod_sb(tp, XFS_TRANS_SB_FREXTENTS, (long)len);
1090 * If we've now freed all the blocks, reset the file sequence
1091 * number to 0 for pre-RTG file systems.
1093 if (!xfs_has_rtgroups(mp) &&
1094 tp->t_frextents_delta + mp->m_sb.sb_frextents ==
1095 mp->m_sb.sb_rextents) {
1096 if (!(rbmip->i_diflags & XFS_DIFLAG_NEWRTBM))
1097 rbmip->i_diflags |= XFS_DIFLAG_NEWRTBM;
1099 atime = inode_get_atime(VFS_I(rbmip));
1101 inode_set_atime_to_ts(VFS_I(rbmip), atime);
1102 xfs_trans_log_inode(tp, rbmip, XFS_ILOG_CORE);
1106 xfs_rtbuf_cache_relse(&args);
1111 * Free some blocks in the realtime subvolume. rtbno and rtlen are in units of
1112 * rt blocks, not rt extents; must be aligned to the rt extent size; and rtlen
1113 * cannot exceed XFS_MAX_BMBT_EXTLEN.
1117 struct xfs_trans *tp,
1118 struct xfs_rtgroup *rtg,
1119 xfs_fsblock_t rtbno,
1120 xfs_filblks_t rtlen)
1122 struct xfs_mount *mp = tp->t_mountp;
1126 ASSERT(rtlen <= XFS_MAX_BMBT_EXTLEN);
1128 mod = xfs_blen_to_rtxoff(mp, rtlen);
1134 mod = xfs_rtb_to_rtxoff(mp, rtbno);
1140 error = xfs_rtfree_extent(tp, rtg, xfs_rtb_to_rtx(mp, rtbno),
1141 xfs_extlen_to_rtxlen(mp, rtlen));
1145 if (xfs_has_rtgroups(mp))
1146 xfs_extent_busy_insert(tp, rtg_group(rtg),
1147 xfs_rtb_to_rgbno(mp, rtbno), rtlen, 0);
1152 /* Find all the free records within a given range. */
1154 xfs_rtalloc_query_range(
1155 struct xfs_rtgroup *rtg,
1156 struct xfs_trans *tp,
1159 xfs_rtalloc_query_range_fn fn,
1162 struct xfs_mount *mp = rtg_mount(rtg);
1163 struct xfs_rtalloc_args args = {
1172 if (start == end || start >= rtg->rtg_extents)
1175 end = min(end, rtg->rtg_extents - 1);
1177 /* Iterate the bitmap, looking for discrepancies. */
1178 while (start <= end) {
1179 struct xfs_rtalloc_rec rec;
1183 /* Is the first block free? */
1184 error = xfs_rtcheck_range(&args, start, 1, 1, &rtend,
1189 /* How long does the extent go for? */
1190 error = xfs_rtfind_forw(&args, start, end, &rtend);
1195 rec.ar_startext = start;
1196 rec.ar_extcount = rtend - start + 1;
1198 error = fn(rtg, tp, &rec, priv);
1206 xfs_rtbuf_cache_relse(&args);
1210 /* Find all the free records. */
1212 xfs_rtalloc_query_all(
1213 struct xfs_rtgroup *rtg,
1214 struct xfs_trans *tp,
1215 xfs_rtalloc_query_range_fn fn,
1218 return xfs_rtalloc_query_range(rtg, tp, 0, rtg->rtg_extents - 1, fn,
1222 /* Is the given extent all free? */
1224 xfs_rtalloc_extent_is_free(
1225 struct xfs_rtgroup *rtg,
1226 struct xfs_trans *tp,
1231 struct xfs_rtalloc_args args = {
1232 .mp = rtg_mount(rtg),
1240 error = xfs_rtcheck_range(&args, start, len, 1, &end, &matches);
1241 xfs_rtbuf_cache_relse(&args);
1249 /* Compute the number of rt extents tracked by a single bitmap block. */
1251 xfs_rtbitmap_rtx_per_rbmblock(
1252 struct xfs_mount *mp)
1254 unsigned int rbmblock_bytes = mp->m_sb.sb_blocksize;
1256 if (xfs_has_rtgroups(mp))
1257 rbmblock_bytes -= sizeof(struct xfs_rtbuf_blkinfo);
1259 return rbmblock_bytes * NBBY;
1263 * Compute the number of rtbitmap blocks needed to track the given number of rt
1267 xfs_rtbitmap_blockcount_len(
1268 struct xfs_mount *mp,
1269 xfs_rtbxlen_t rtextents)
1271 return howmany_64(rtextents, xfs_rtbitmap_rtx_per_rbmblock(mp));
1274 /* How many rt extents does each rtbitmap file track? */
1275 static inline xfs_rtbxlen_t
1276 xfs_rtbitmap_bitcount(
1277 struct xfs_mount *mp)
1279 if (!mp->m_sb.sb_rextents)
1282 /* rtgroup size can be nonzero even if rextents is zero */
1283 if (xfs_has_rtgroups(mp))
1284 return mp->m_sb.sb_rgextents;
1286 return mp->m_sb.sb_rextents;
1290 * Compute the number of rtbitmap blocks used for a given file system.
1293 xfs_rtbitmap_blockcount(
1294 struct xfs_mount *mp)
1296 return xfs_rtbitmap_blockcount_len(mp, xfs_rtbitmap_bitcount(mp));
1300 * Compute the geometry of the rtsummary file needed to track the given rt
1304 xfs_rtsummary_blockcount(
1305 struct xfs_mount *mp,
1306 unsigned int *rsumlevels)
1308 xfs_rtbxlen_t rextents = xfs_rtbitmap_bitcount(mp);
1309 unsigned long long rsumwords;
1311 *rsumlevels = xfs_compute_rextslog(rextents) + 1;
1312 rsumwords = xfs_rtbitmap_blockcount_len(mp, rextents) * (*rsumlevels);
1313 return howmany_64(rsumwords, mp->m_blockwsize);
1317 xfs_rtfile_alloc_blocks(
1318 struct xfs_inode *ip,
1319 xfs_fileoff_t offset_fsb,
1320 xfs_filblks_t count_fsb,
1321 struct xfs_bmbt_irec *map)
1323 struct xfs_mount *mp = ip->i_mount;
1324 struct xfs_trans *tp;
1328 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_growrtalloc,
1329 XFS_GROWFSRT_SPACE_RES(mp, count_fsb), 0, 0, &tp);
1333 xfs_ilock(ip, XFS_ILOCK_EXCL);
1334 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
1336 error = xfs_iext_count_extend(tp, ip, XFS_DATA_FORK,
1337 XFS_IEXT_ADD_NOSPLIT_CNT);
1339 goto out_trans_cancel;
1341 error = xfs_bmapi_write(tp, ip, offset_fsb, count_fsb,
1342 XFS_BMAPI_METADATA, 0, map, &nmap);
1344 goto out_trans_cancel;
1346 return xfs_trans_commit(tp);
1349 xfs_trans_cancel(tp);
1353 /* Get a buffer for the block. */
1355 xfs_rtfile_initialize_block(
1356 struct xfs_rtgroup *rtg,
1357 enum xfs_rtg_inodes type,
1358 xfs_fsblock_t fsbno,
1361 struct xfs_mount *mp = rtg_mount(rtg);
1362 struct xfs_inode *ip = rtg->rtg_inodes[type];
1363 struct xfs_trans *tp;
1366 const size_t copylen = mp->m_blockwsize << XFS_WORDLOG;
1367 enum xfs_blft buf_type;
1370 if (type == XFS_RTGI_BITMAP)
1371 buf_type = XFS_BLFT_RTBITMAP_BUF;
1372 else if (type == XFS_RTGI_SUMMARY)
1373 buf_type = XFS_BLFT_RTSUMMARY_BUF;
1377 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_growrtzero, 0, 0, 0, &tp);
1380 xfs_ilock(ip, XFS_ILOCK_EXCL);
1381 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
1383 error = xfs_trans_get_buf(tp, mp->m_ddev_targp,
1384 XFS_FSB_TO_DADDR(mp, fsbno), mp->m_bsize, 0, &bp);
1386 xfs_trans_cancel(tp);
1389 bufdata = bp->b_addr;
1391 xfs_trans_buf_set_type(tp, bp, buf_type);
1392 bp->b_ops = xfs_rtblock_ops(mp, type);
1394 if (xfs_has_rtgroups(mp)) {
1395 struct xfs_rtbuf_blkinfo *hdr = bp->b_addr;
1397 if (type == XFS_RTGI_BITMAP)
1398 hdr->rt_magic = cpu_to_be32(XFS_RTBITMAP_MAGIC);
1400 hdr->rt_magic = cpu_to_be32(XFS_RTSUMMARY_MAGIC);
1401 hdr->rt_owner = cpu_to_be64(ip->i_ino);
1402 hdr->rt_blkno = cpu_to_be64(XFS_FSB_TO_DADDR(mp, fsbno));
1404 uuid_copy(&hdr->rt_uuid, &mp->m_sb.sb_meta_uuid);
1406 bufdata += sizeof(*hdr);
1410 memcpy(bufdata, data, copylen);
1412 memset(bufdata, 0, copylen);
1413 xfs_trans_log_buf(tp, bp, 0, mp->m_sb.sb_blocksize - 1);
1414 return xfs_trans_commit(tp);
1418 * Allocate space to the bitmap or summary file, and zero it, for growfs.
1419 * @data must be a contiguous buffer large enough to fill all blocks in the
1420 * file; or NULL to initialize the contents to zeroes.
1423 xfs_rtfile_initialize_blocks(
1424 struct xfs_rtgroup *rtg,
1425 enum xfs_rtg_inodes type,
1426 xfs_fileoff_t offset_fsb, /* offset to start from */
1427 xfs_fileoff_t end_fsb, /* offset to allocate to */
1428 void *data) /* data to fill the blocks */
1430 struct xfs_mount *mp = rtg_mount(rtg);
1431 const size_t copylen = mp->m_blockwsize << XFS_WORDLOG;
1433 while (offset_fsb < end_fsb) {
1434 struct xfs_bmbt_irec map;
1438 error = xfs_rtfile_alloc_blocks(rtg->rtg_inodes[type],
1439 offset_fsb, end_fsb - offset_fsb, &map);
1444 * Now we need to clear the allocated blocks.
1446 * Do this one block per transaction, to keep it simple.
1448 for (i = 0; i < map.br_blockcount; i++) {
1449 error = xfs_rtfile_initialize_block(rtg, type,
1450 map.br_startblock + i, data);
1457 offset_fsb = map.br_startoff + map.br_blockcount;
1464 xfs_rtbitmap_create(
1465 struct xfs_rtgroup *rtg,
1466 struct xfs_inode *ip,
1467 struct xfs_trans *tp,
1470 struct xfs_mount *mp = rtg_mount(rtg);
1472 ip->i_disk_size = mp->m_sb.sb_rbmblocks * mp->m_sb.sb_blocksize;
1473 if (init && !xfs_has_rtgroups(mp)) {
1474 ip->i_diflags |= XFS_DIFLAG_NEWRTBM;
1475 inode_set_atime(VFS_I(ip), 0, 0);
1477 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1482 xfs_rtsummary_create(
1483 struct xfs_rtgroup *rtg,
1484 struct xfs_inode *ip,
1485 struct xfs_trans *tp,
1488 struct xfs_mount *mp = rtg_mount(rtg);
1490 ip->i_disk_size = mp->m_rsumblocks * mp->m_sb.sb_blocksize;
1491 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);