1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
6 #include "libxfs_priv.h"
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_rtbitmap.h"
20 #include "xfs_health.h"
22 #include "xfs_errortag.h"
25 * Realtime allocator bitmap functions shared with userspace.
32 struct xfs_mount
*mp
= bp
->b_mount
;
33 struct xfs_rtbuf_blkinfo
*hdr
= bp
->b_addr
;
35 if (!xfs_verify_magic(bp
, hdr
->rt_magic
))
36 return __this_address
;
37 if (!xfs_has_rtgroups(mp
))
38 return __this_address
;
40 return __this_address
;
41 if (!uuid_equal(&hdr
->rt_uuid
, &mp
->m_sb
.sb_meta_uuid
))
42 return __this_address
;
43 if (hdr
->rt_blkno
!= cpu_to_be64(xfs_buf_daddr(bp
)))
44 return __this_address
;
49 xfs_rtbuf_verify_read(
52 struct xfs_mount
*mp
= bp
->b_mount
;
53 struct xfs_rtbuf_blkinfo
*hdr
= bp
->b_addr
;
56 if (!xfs_has_rtgroups(mp
))
59 if (!xfs_log_check_lsn(mp
, be64_to_cpu(hdr
->rt_lsn
))) {
64 if (!xfs_buf_verify_cksum(bp
, XFS_RTBUF_CRC_OFF
)) {
69 fa
= xfs_rtbuf_verify(bp
);
75 xfs_verifier_error(bp
, -EFSCORRUPTED
, fa
);
79 xfs_rtbuf_verify_write(
82 struct xfs_mount
*mp
= bp
->b_mount
;
83 struct xfs_rtbuf_blkinfo
*hdr
= bp
->b_addr
;
84 struct xfs_buf_log_item
*bip
= bp
->b_log_item
;
87 if (!xfs_has_rtgroups(mp
))
90 fa
= xfs_rtbuf_verify(bp
);
92 xfs_verifier_error(bp
, -EFSCORRUPTED
, fa
);
97 hdr
->rt_lsn
= cpu_to_be64(bip
->bli_item
.li_lsn
);
98 xfs_buf_update_cksum(bp
, XFS_RTBUF_CRC_OFF
);
101 const struct xfs_buf_ops xfs_rtbuf_ops
= {
103 .verify_read
= xfs_rtbuf_verify_read
,
104 .verify_write
= xfs_rtbuf_verify_write
,
107 const struct xfs_buf_ops xfs_rtbitmap_buf_ops
= {
108 .name
= "xfs_rtbitmap",
109 .magic
= { 0, cpu_to_be32(XFS_RTBITMAP_MAGIC
) },
110 .verify_read
= xfs_rtbuf_verify_read
,
111 .verify_write
= xfs_rtbuf_verify_write
,
112 .verify_struct
= xfs_rtbuf_verify
,
115 const struct xfs_buf_ops xfs_rtsummary_buf_ops
= {
116 .name
= "xfs_rtsummary",
117 .magic
= { 0, cpu_to_be32(XFS_RTSUMMARY_MAGIC
) },
118 .verify_read
= xfs_rtbuf_verify_read
,
119 .verify_write
= xfs_rtbuf_verify_write
,
120 .verify_struct
= xfs_rtbuf_verify
,
123 /* Release cached rt bitmap and summary buffers. */
125 xfs_rtbuf_cache_relse(
126 struct xfs_rtalloc_args
*args
)
129 xfs_trans_brelse(args
->tp
, args
->rbmbp
);
131 args
->rbmoff
= NULLFILEOFF
;
134 xfs_trans_brelse(args
->tp
, args
->sumbp
);
136 args
->sumoff
= NULLFILEOFF
;
141 * Get a buffer for the bitmap or summary file block specified.
142 * The buffer is returned read and locked.
146 struct xfs_rtalloc_args
*args
,
147 xfs_fileoff_t block
, /* block number in bitmap or summary */
148 enum xfs_rtg_inodes type
)
150 struct xfs_inode
*ip
= args
->rtg
->rtg_inodes
[type
];
151 struct xfs_mount
*mp
= args
->mp
;
152 struct xfs_buf
**cbpp
; /* cached block buffer */
153 xfs_fileoff_t
*coffp
; /* cached block number */
154 struct xfs_buf
*bp
; /* block buffer, result */
155 struct xfs_bmbt_irec map
;
156 enum xfs_blft buf_type
;
161 case XFS_RTGI_SUMMARY
:
163 coffp
= &args
->sumoff
;
164 buf_type
= XFS_BLFT_RTSUMMARY_BUF
;
166 case XFS_RTGI_BITMAP
:
168 coffp
= &args
->rbmoff
;
169 buf_type
= XFS_BLFT_RTBITMAP_BUF
;
176 * If we have a cached buffer, and the block number matches, use that.
178 if (*cbpp
&& *coffp
== block
)
182 * Otherwise we have to have to get the buffer. If there was an old
183 * one, get rid of it first.
186 xfs_trans_brelse(args
->tp
, *cbpp
);
190 error
= xfs_bmapi_read(ip
, block
, 1, &map
, &nmap
, 0);
194 if (XFS_IS_CORRUPT(mp
, nmap
== 0 || !xfs_bmap_is_written_extent(&map
))) {
195 xfs_rtginode_mark_sick(args
->rtg
, type
);
196 return -EFSCORRUPTED
;
199 ASSERT(map
.br_startblock
!= NULLFSBLOCK
);
200 error
= xfs_trans_read_buf(mp
, args
->tp
, mp
->m_ddev_targp
,
201 XFS_FSB_TO_DADDR(mp
, map
.br_startblock
),
203 xfs_rtblock_ops(mp
, type
));
204 if (xfs_metadata_is_sick(error
))
205 xfs_rtginode_mark_sick(args
->rtg
, type
);
209 if (xfs_has_rtgroups(mp
)) {
210 struct xfs_rtbuf_blkinfo
*hdr
= bp
->b_addr
;
212 if (hdr
->rt_owner
!= cpu_to_be64(ip
->i_ino
)) {
213 xfs_buf_mark_corrupt(bp
);
214 xfs_trans_brelse(args
->tp
, bp
);
215 xfs_rtginode_mark_sick(args
->rtg
, type
);
216 return -EFSCORRUPTED
;
220 xfs_trans_buf_set_type(args
->tp
, bp
, buf_type
);
227 xfs_rtbitmap_read_buf(
228 struct xfs_rtalloc_args
*args
,
231 struct xfs_mount
*mp
= args
->mp
;
233 if (XFS_IS_CORRUPT(mp
, block
>= mp
->m_sb
.sb_rbmblocks
)) {
234 xfs_rtginode_mark_sick(args
->rtg
, XFS_RTGI_BITMAP
);
235 return -EFSCORRUPTED
;
238 return xfs_rtbuf_get(args
, block
, XFS_RTGI_BITMAP
);
242 xfs_rtsummary_read_buf(
243 struct xfs_rtalloc_args
*args
,
246 struct xfs_mount
*mp
= args
->mp
;
248 if (XFS_IS_CORRUPT(mp
, block
>= mp
->m_rsumblocks
)) {
249 xfs_rtginode_mark_sick(args
->rtg
, XFS_RTGI_SUMMARY
);
250 return -EFSCORRUPTED
;
252 return xfs_rtbuf_get(args
, block
, XFS_RTGI_SUMMARY
);
256 * Searching backward from start find the first block whose allocated/free state
257 * is different from start's.
261 struct xfs_rtalloc_args
*args
,
262 xfs_rtxnum_t start
, /* starting rtext to look at */
263 xfs_rtxnum_t
*rtx
) /* out: start rtext found */
265 struct xfs_mount
*mp
= args
->mp
;
266 int bit
; /* bit number in the word */
267 xfs_fileoff_t block
; /* bitmap block number */
268 int error
; /* error value */
269 xfs_rtxnum_t firstbit
; /* first useful bit in the word */
270 xfs_rtxnum_t i
; /* current bit number rel. to start */
271 xfs_rtxnum_t len
; /* length of inspected area */
272 xfs_rtword_t mask
; /* mask of relevant bits for value */
273 xfs_rtword_t want
; /* mask for "good" values */
274 xfs_rtword_t wdiff
; /* difference from wanted value */
276 unsigned int word
; /* word number in the buffer */
279 * Compute and read in starting bitmap block for starting block.
281 block
= xfs_rtx_to_rbmblock(mp
, start
);
282 error
= xfs_rtbitmap_read_buf(args
, block
);
287 * Get the first word's index & point to it.
289 word
= xfs_rtx_to_rbmword(mp
, start
);
290 bit
= (int)(start
& (XFS_NBWORD
- 1));
293 * Compute match value, based on the bit at start: if 1 (free)
294 * then all-ones, else all-zeroes.
296 incore
= xfs_rtbitmap_getword(args
, word
);
297 want
= (incore
& ((xfs_rtword_t
)1 << bit
)) ? -1 : 0;
299 * If the starting position is not word-aligned, deal with the
302 if (bit
< XFS_NBWORD
- 1) {
304 * Calculate first (leftmost) bit number to look at,
305 * and mask for all the relevant bits in this word.
307 firstbit
= max_t(xfs_srtblock_t
, bit
- len
+ 1, 0);
308 mask
= (((xfs_rtword_t
)1 << (bit
- firstbit
+ 1)) - 1) <<
311 * Calculate the difference between the value there
312 * and what we're looking for.
314 if ((wdiff
= (incore
^ want
) & mask
)) {
316 * Different. Mark where we are and return.
318 i
= bit
- xfs_highbit32(wdiff
);
319 *rtx
= start
- i
+ 1;
322 i
= bit
- firstbit
+ 1;
324 * Go on to previous block if that's where the previous word is
325 * and we need the previous word.
327 if (--word
== -1 && i
< len
) {
329 * If done with this block, get the previous one.
331 error
= xfs_rtbitmap_read_buf(args
, --block
);
335 word
= mp
->m_blockwsize
- 1;
339 * Starting on a word boundary, no partial word.
344 * Loop over whole words in buffers. When we use up one buffer
345 * we move on to the previous one.
347 while (len
- i
>= XFS_NBWORD
) {
349 * Compute difference between actual and desired value.
351 incore
= xfs_rtbitmap_getword(args
, word
);
352 if ((wdiff
= incore
^ want
)) {
354 * Different, mark where we are and return.
356 i
+= XFS_NBWORD
- 1 - xfs_highbit32(wdiff
);
357 *rtx
= start
- i
+ 1;
362 * Go on to previous block if that's where the previous word is
363 * and we need the previous word.
365 if (--word
== -1 && i
< len
) {
367 * If done with this block, get the previous one.
369 error
= xfs_rtbitmap_read_buf(args
, --block
);
373 word
= mp
->m_blockwsize
- 1;
377 * If not ending on a word boundary, deal with the last
382 * Calculate first (leftmost) bit number to look at,
383 * and mask for all the relevant bits in this word.
385 firstbit
= XFS_NBWORD
- (len
- i
);
386 mask
= (((xfs_rtword_t
)1 << (len
- i
)) - 1) << firstbit
;
388 * Compute difference between actual and desired value.
390 incore
= xfs_rtbitmap_getword(args
, word
);
391 if ((wdiff
= (incore
^ want
) & mask
)) {
393 * Different, mark where we are and return.
395 i
+= XFS_NBWORD
- 1 - xfs_highbit32(wdiff
);
396 *rtx
= start
- i
+ 1;
402 * No match, return that we scanned the whole area.
404 *rtx
= start
- i
+ 1;
409 * Searching forward from start to limit, find the first block whose
410 * allocated/free state is different from start's.
414 struct xfs_rtalloc_args
*args
,
415 xfs_rtxnum_t start
, /* starting rtext to look at */
416 xfs_rtxnum_t limit
, /* last rtext to look at */
417 xfs_rtxnum_t
*rtx
) /* out: start rtext found */
419 struct xfs_mount
*mp
= args
->mp
;
420 int bit
; /* bit number in the word */
421 xfs_fileoff_t block
; /* bitmap block number */
423 xfs_rtxnum_t i
; /* current bit number rel. to start */
424 xfs_rtxnum_t lastbit
;/* last useful bit in the word */
425 xfs_rtxnum_t len
; /* length of inspected area */
426 xfs_rtword_t mask
; /* mask of relevant bits for value */
427 xfs_rtword_t want
; /* mask for "good" values */
428 xfs_rtword_t wdiff
; /* difference from wanted value */
430 unsigned int word
; /* word number in the buffer */
432 ASSERT(start
<= limit
);
435 * Compute and read in starting bitmap block for starting block.
437 block
= xfs_rtx_to_rbmblock(mp
, start
);
438 error
= xfs_rtbitmap_read_buf(args
, block
);
443 * Get the first word's index & point to it.
445 word
= xfs_rtx_to_rbmword(mp
, start
);
446 bit
= (int)(start
& (XFS_NBWORD
- 1));
447 len
= limit
- start
+ 1;
449 * Compute match value, based on the bit at start: if 1 (free)
450 * then all-ones, else all-zeroes.
452 incore
= xfs_rtbitmap_getword(args
, word
);
453 want
= (incore
& ((xfs_rtword_t
)1 << bit
)) ? -1 : 0;
455 * If the starting position is not word-aligned, deal with the
460 * Calculate last (rightmost) bit number to look at,
461 * and mask for all the relevant bits in this word.
463 lastbit
= min(bit
+ len
, XFS_NBWORD
);
464 mask
= (((xfs_rtword_t
)1 << (lastbit
- bit
)) - 1) << bit
;
466 * Calculate the difference between the value there
467 * and what we're looking for.
469 if ((wdiff
= (incore
^ want
) & mask
)) {
471 * Different. Mark where we are and return.
473 i
= xfs_lowbit32(wdiff
) - bit
;
474 *rtx
= start
+ i
- 1;
479 * Go on to next block if that's where the next word is
480 * and we need the next word.
482 if (++word
== mp
->m_blockwsize
&& i
< len
) {
484 * If done with this block, get the previous one.
486 error
= xfs_rtbitmap_read_buf(args
, ++block
);
494 * Starting on a word boundary, no partial word.
499 * Loop over whole words in buffers. When we use up one buffer
500 * we move on to the next one.
502 while (len
- i
>= XFS_NBWORD
) {
504 * Compute difference between actual and desired value.
506 incore
= xfs_rtbitmap_getword(args
, word
);
507 if ((wdiff
= incore
^ want
)) {
509 * Different, mark where we are and return.
511 i
+= xfs_lowbit32(wdiff
);
512 *rtx
= start
+ i
- 1;
517 * Go on to next block if that's where the next word is
518 * and we need the next word.
520 if (++word
== mp
->m_blockwsize
&& i
< len
) {
522 * If done with this block, get the next one.
524 error
= xfs_rtbitmap_read_buf(args
, ++block
);
532 * If not ending on a word boundary, deal with the last
535 if ((lastbit
= len
- i
)) {
537 * Calculate mask for all the relevant bits in this word.
539 mask
= ((xfs_rtword_t
)1 << lastbit
) - 1;
541 * Compute difference between actual and desired value.
543 incore
= xfs_rtbitmap_getword(args
, word
);
544 if ((wdiff
= (incore
^ want
) & mask
)) {
546 * Different, mark where we are and return.
548 i
+= xfs_lowbit32(wdiff
);
549 *rtx
= start
+ i
- 1;
555 * No match, return that we scanned the whole area.
557 *rtx
= start
+ i
- 1;
561 /* Log rtsummary counter at @infoword. */
563 xfs_trans_log_rtsummary(
564 struct xfs_rtalloc_args
*args
,
565 unsigned int infoword
)
567 struct xfs_buf
*bp
= args
->sumbp
;
570 first
= (void *)xfs_rsumblock_infoptr(args
, infoword
) - bp
->b_addr
;
571 last
= first
+ sizeof(xfs_suminfo_t
) - 1;
573 xfs_trans_log_buf(args
->tp
, bp
, first
, last
);
577 * Modify the summary information for a given extent size, bitmap block
581 xfs_rtmodify_summary(
582 struct xfs_rtalloc_args
*args
,
583 int log
, /* log2 of extent size */
584 xfs_fileoff_t bbno
, /* bitmap block number */
585 int delta
) /* in/out: summary block number */
587 struct xfs_mount
*mp
= args
->mp
;
588 xfs_rtsumoff_t so
= xfs_rtsumoffs(mp
, log
, bbno
);
589 uint8_t *rsum_cache
= args
->rtg
->rtg_rsum_cache
;
590 unsigned int infoword
;
594 error
= xfs_rtsummary_read_buf(args
, xfs_rtsumoffs_to_block(mp
, so
));
598 infoword
= xfs_rtsumoffs_to_infoword(mp
, so
);
599 val
= xfs_suminfo_add(args
, infoword
, delta
);
602 if (val
== 0 && log
+ 1 == rsum_cache
[bbno
])
603 rsum_cache
[bbno
] = log
;
604 if (val
!= 0 && log
>= rsum_cache
[bbno
])
605 rsum_cache
[bbno
] = log
+ 1;
608 xfs_trans_log_rtsummary(args
, infoword
);
613 * Read and return the summary information for a given extent size, bitmap block
618 struct xfs_rtalloc_args
*args
,
619 int log
, /* log2 of extent size */
620 xfs_fileoff_t bbno
, /* bitmap block number */
621 xfs_suminfo_t
*sum
) /* out: summary info for this block */
623 struct xfs_mount
*mp
= args
->mp
;
624 xfs_rtsumoff_t so
= xfs_rtsumoffs(mp
, log
, bbno
);
627 error
= xfs_rtsummary_read_buf(args
, xfs_rtsumoffs_to_block(mp
, so
));
629 *sum
= xfs_suminfo_get(args
, xfs_rtsumoffs_to_infoword(mp
, so
));
633 /* Log rtbitmap block from the word @from to the byte before @next. */
635 xfs_trans_log_rtbitmap(
636 struct xfs_rtalloc_args
*args
,
640 struct xfs_buf
*bp
= args
->rbmbp
;
643 first
= (void *)xfs_rbmblock_wordptr(args
, from
) - bp
->b_addr
;
644 last
= ((void *)xfs_rbmblock_wordptr(args
, next
) - 1) - bp
->b_addr
;
646 xfs_trans_log_buf(args
->tp
, bp
, first
, last
);
650 * Set the given range of bitmap bits to the given value.
651 * Do whatever I/O and logging is required.
655 struct xfs_rtalloc_args
*args
,
656 xfs_rtxnum_t start
, /* starting rtext to modify */
657 xfs_rtxlen_t len
, /* length of extent to modify */
658 int val
) /* 1 for free, 0 for allocated */
660 struct xfs_mount
*mp
= args
->mp
;
661 int bit
; /* bit number in the word */
662 xfs_fileoff_t block
; /* bitmap block number */
664 int i
; /* current bit number rel. to start */
665 int lastbit
; /* last useful bit in word */
666 xfs_rtword_t mask
; /* mask of relevant bits for value */
668 unsigned int firstword
; /* first word used in the buffer */
669 unsigned int word
; /* word number in the buffer */
672 * Compute starting bitmap block number.
674 block
= xfs_rtx_to_rbmblock(mp
, start
);
676 * Read the bitmap block, and point to its data.
678 error
= xfs_rtbitmap_read_buf(args
, block
);
683 * Compute the starting word's address, and starting bit.
685 firstword
= word
= xfs_rtx_to_rbmword(mp
, start
);
686 bit
= (int)(start
& (XFS_NBWORD
- 1));
688 * 0 (allocated) => all zeroes; 1 (free) => all ones.
692 * If not starting on a word boundary, deal with the first
697 * Compute first bit not changed and mask of relevant bits.
699 lastbit
= min(bit
+ len
, XFS_NBWORD
);
700 mask
= (((xfs_rtword_t
)1 << (lastbit
- bit
)) - 1) << bit
;
702 * Set/clear the active bits.
704 incore
= xfs_rtbitmap_getword(args
, word
);
709 xfs_rtbitmap_setword(args
, word
, incore
);
712 * Go on to the next block if that's where the next word is
713 * and we need the next word.
715 if (++word
== mp
->m_blockwsize
&& i
< len
) {
717 * Log the changed part of this block.
720 xfs_trans_log_rtbitmap(args
, firstword
, word
);
721 error
= xfs_rtbitmap_read_buf(args
, ++block
);
725 firstword
= word
= 0;
729 * Starting on a word boundary, no partial word.
734 * Loop over whole words in buffers. When we use up one buffer
735 * we move on to the next one.
737 while (len
- i
>= XFS_NBWORD
) {
739 * Set the word value correctly.
741 xfs_rtbitmap_setword(args
, word
, val
);
744 * Go on to the next block if that's where the next word is
745 * and we need the next word.
747 if (++word
== mp
->m_blockwsize
&& i
< len
) {
749 * Log the changed part of this block.
752 xfs_trans_log_rtbitmap(args
, firstword
, word
);
753 error
= xfs_rtbitmap_read_buf(args
, ++block
);
757 firstword
= word
= 0;
761 * If not ending on a word boundary, deal with the last
764 if ((lastbit
= len
- i
)) {
766 * Compute a mask of relevant bits.
768 mask
= ((xfs_rtword_t
)1 << lastbit
) - 1;
770 * Set/clear the active bits.
772 incore
= xfs_rtbitmap_getword(args
, word
);
777 xfs_rtbitmap_setword(args
, word
, incore
);
781 * Log any remaining changed bytes.
783 if (word
> firstword
)
784 xfs_trans_log_rtbitmap(args
, firstword
, word
);
789 * Mark an extent specified by start and len freed.
790 * Updates all the summary information as well as the bitmap.
794 struct xfs_rtalloc_args
*args
,
795 xfs_rtxnum_t start
, /* starting rtext to free */
796 xfs_rtxlen_t len
) /* in/out: summary block number */
798 struct xfs_mount
*mp
= args
->mp
;
799 xfs_rtxnum_t end
; /* end of the freed extent */
800 int error
; /* error value */
801 xfs_rtxnum_t postblock
; /* first rtext freed > end */
802 xfs_rtxnum_t preblock
; /* first rtext freed < start */
804 end
= start
+ len
- 1;
806 * Modify the bitmap to mark this extent freed.
808 error
= xfs_rtmodify_range(args
, start
, len
, 1);
813 * Assume we're freeing out of the middle of an allocated extent.
814 * We need to find the beginning and end of the extent so we can
815 * properly update the summary.
817 error
= xfs_rtfind_back(args
, start
, &preblock
);
822 * Find the next allocated block (end of allocated extent).
824 error
= xfs_rtfind_forw(args
, end
, args
->rtg
->rtg_extents
- 1,
829 * If there are blocks not being freed at the front of the
830 * old extent, add summary data for them to be allocated.
832 if (preblock
< start
) {
833 error
= xfs_rtmodify_summary(args
,
834 xfs_highbit64(start
- preblock
),
835 xfs_rtx_to_rbmblock(mp
, preblock
), -1);
841 * If there are blocks not being freed at the end of the
842 * old extent, add summary data for them to be allocated.
844 if (postblock
> end
) {
845 error
= xfs_rtmodify_summary(args
,
846 xfs_highbit64(postblock
- end
),
847 xfs_rtx_to_rbmblock(mp
, end
+ 1), -1);
853 * Increment the summary information corresponding to the entire
856 return xfs_rtmodify_summary(args
,
857 xfs_highbit64(postblock
+ 1 - preblock
),
858 xfs_rtx_to_rbmblock(mp
, preblock
), 1);
862 * Check that the given range is either all allocated (val = 0) or
863 * all free (val = 1).
867 struct xfs_rtalloc_args
*args
,
868 xfs_rtxnum_t start
, /* starting rtext number of extent */
869 xfs_rtxlen_t len
, /* length of extent */
870 int val
, /* 1 for free, 0 for allocated */
871 xfs_rtxnum_t
*new, /* out: first rtext not matching */
872 int *stat
) /* out: 1 for matches, 0 for not */
874 struct xfs_mount
*mp
= args
->mp
;
875 int bit
; /* bit number in the word */
876 xfs_fileoff_t block
; /* bitmap block number */
878 xfs_rtxnum_t i
; /* current bit number rel. to start */
879 xfs_rtxnum_t lastbit
; /* last useful bit in word */
880 xfs_rtword_t mask
; /* mask of relevant bits for value */
881 xfs_rtword_t wdiff
; /* difference from wanted value */
883 unsigned int word
; /* word number in the buffer */
886 * Compute starting bitmap block number
888 block
= xfs_rtx_to_rbmblock(mp
, start
);
890 * Read the bitmap block.
892 error
= xfs_rtbitmap_read_buf(args
, block
);
897 * Compute the starting word's address, and starting bit.
899 word
= xfs_rtx_to_rbmword(mp
, start
);
900 bit
= (int)(start
& (XFS_NBWORD
- 1));
902 * 0 (allocated) => all zero's; 1 (free) => all one's.
906 * If not starting on a word boundary, deal with the first
911 * Compute first bit not examined.
913 lastbit
= min(bit
+ len
, XFS_NBWORD
);
915 * Mask of relevant bits.
917 mask
= (((xfs_rtword_t
)1 << (lastbit
- bit
)) - 1) << bit
;
919 * Compute difference between actual and desired value.
921 incore
= xfs_rtbitmap_getword(args
, word
);
922 if ((wdiff
= (incore
^ val
) & mask
)) {
924 * Different, compute first wrong bit and return.
926 i
= xfs_lowbit32(wdiff
) - bit
;
933 * Go on to next block if that's where the next word is
934 * and we need the next word.
936 if (++word
== mp
->m_blockwsize
&& i
< len
) {
938 * If done with this block, get the next one.
940 error
= xfs_rtbitmap_read_buf(args
, ++block
);
948 * Starting on a word boundary, no partial word.
953 * Loop over whole words in buffers. When we use up one buffer
954 * we move on to the next one.
956 while (len
- i
>= XFS_NBWORD
) {
958 * Compute difference between actual and desired value.
960 incore
= xfs_rtbitmap_getword(args
, word
);
961 if ((wdiff
= incore
^ val
)) {
963 * Different, compute first wrong bit and return.
965 i
+= xfs_lowbit32(wdiff
);
972 * Go on to next block if that's where the next word is
973 * and we need the next word.
975 if (++word
== mp
->m_blockwsize
&& i
< len
) {
977 * If done with this block, get the next one.
979 error
= xfs_rtbitmap_read_buf(args
, ++block
);
987 * If not ending on a word boundary, deal with the last
990 if ((lastbit
= len
- i
)) {
992 * Mask of relevant bits.
994 mask
= ((xfs_rtword_t
)1 << lastbit
) - 1;
996 * Compute difference between actual and desired value.
998 incore
= xfs_rtbitmap_getword(args
, word
);
999 if ((wdiff
= (incore
^ val
) & mask
)) {
1001 * Different, compute first wrong bit and return.
1003 i
+= xfs_lowbit32(wdiff
);
1011 * Successful, return.
1020 * Check that the given extent (block range) is allocated already.
1023 xfs_rtcheck_alloc_range(
1024 struct xfs_rtalloc_args
*args
,
1025 xfs_rtxnum_t start
, /* starting rtext number of extent */
1026 xfs_rtxlen_t len
) /* length of extent */
1028 xfs_rtxnum_t
new; /* dummy for xfs_rtcheck_range */
1032 error
= xfs_rtcheck_range(args
, start
, len
, 0, &new, &stat
);
1039 #define xfs_rtcheck_alloc_range(a,b,l) (0)
1042 * Free an extent in the realtime subvolume. Length is expressed in
1043 * realtime extents, as is the block number.
1047 struct xfs_trans
*tp
, /* transaction pointer */
1048 struct xfs_rtgroup
*rtg
,
1049 xfs_rtxnum_t start
, /* starting rtext number to free */
1050 xfs_rtxlen_t len
) /* length of extent freed */
1052 struct xfs_mount
*mp
= tp
->t_mountp
;
1053 struct xfs_inode
*rbmip
= rtg_bitmap(rtg
);
1054 struct xfs_rtalloc_args args
= {
1060 struct timespec64 atime
;
1062 ASSERT(rbmip
->i_itemp
!= NULL
);
1063 xfs_assert_ilocked(rbmip
, XFS_ILOCK_EXCL
);
1065 if (XFS_TEST_ERROR(false, mp
, XFS_ERRTAG_FREE_EXTENT
))
1068 error
= xfs_rtcheck_alloc_range(&args
, start
, len
);
1073 * Free the range of realtime blocks.
1075 error
= xfs_rtfree_range(&args
, start
, len
);
1080 * Mark more blocks free in the superblock.
1082 xfs_trans_mod_sb(tp
, XFS_TRANS_SB_FREXTENTS
, (long)len
);
1085 * If we've now freed all the blocks, reset the file sequence
1086 * number to 0 for pre-RTG file systems.
1088 if (!xfs_has_rtgroups(mp
) &&
1089 tp
->t_frextents_delta
+ mp
->m_sb
.sb_frextents
==
1090 mp
->m_sb
.sb_rextents
) {
1091 if (!(rbmip
->i_diflags
& XFS_DIFLAG_NEWRTBM
))
1092 rbmip
->i_diflags
|= XFS_DIFLAG_NEWRTBM
;
1094 atime
= inode_get_atime(VFS_I(rbmip
));
1096 inode_set_atime_to_ts(VFS_I(rbmip
), atime
);
1097 xfs_trans_log_inode(tp
, rbmip
, XFS_ILOG_CORE
);
1101 xfs_rtbuf_cache_relse(&args
);
1106 * Free some blocks in the realtime subvolume. rtbno and rtlen are in units of
1107 * rt blocks, not rt extents; must be aligned to the rt extent size; and rtlen
1108 * cannot exceed XFS_MAX_BMBT_EXTLEN.
1112 struct xfs_trans
*tp
,
1113 struct xfs_rtgroup
*rtg
,
1114 xfs_fsblock_t rtbno
,
1115 xfs_filblks_t rtlen
)
1117 struct xfs_mount
*mp
= tp
->t_mountp
;
1121 ASSERT(!xfs_has_zoned(mp
));
1122 ASSERT(rtlen
<= XFS_MAX_BMBT_EXTLEN
);
1124 mod
= xfs_blen_to_rtxoff(mp
, rtlen
);
1130 mod
= xfs_rtb_to_rtxoff(mp
, rtbno
);
1136 error
= xfs_rtfree_extent(tp
, rtg
, xfs_rtb_to_rtx(mp
, rtbno
),
1137 xfs_extlen_to_rtxlen(mp
, rtlen
));
1141 if (xfs_has_rtgroups(mp
))
1142 xfs_extent_busy_insert(tp
, rtg_group(rtg
),
1143 xfs_rtb_to_rgbno(mp
, rtbno
), rtlen
, 0);
1148 /* Find all the free records within a given range. */
1150 xfs_rtalloc_query_range(
1151 struct xfs_rtgroup
*rtg
,
1152 struct xfs_trans
*tp
,
1155 xfs_rtalloc_query_range_fn fn
,
1158 struct xfs_mount
*mp
= rtg_mount(rtg
);
1159 struct xfs_rtalloc_args args
= {
1168 if (start
== end
|| start
>= rtg
->rtg_extents
)
1171 end
= min(end
, rtg
->rtg_extents
- 1);
1173 if (xfs_has_zoned(mp
))
1176 /* Iterate the bitmap, looking for discrepancies. */
1177 while (start
<= end
) {
1178 struct xfs_rtalloc_rec rec
;
1182 /* Is the first block free? */
1183 error
= xfs_rtcheck_range(&args
, start
, 1, 1, &rtend
,
1188 /* How long does the extent go for? */
1189 error
= xfs_rtfind_forw(&args
, start
, end
, &rtend
);
1194 rec
.ar_startext
= start
;
1195 rec
.ar_extcount
= rtend
- start
+ 1;
1197 error
= fn(rtg
, tp
, &rec
, priv
);
1205 xfs_rtbuf_cache_relse(&args
);
1209 /* Find all the free records. */
1211 xfs_rtalloc_query_all(
1212 struct xfs_rtgroup
*rtg
,
1213 struct xfs_trans
*tp
,
1214 xfs_rtalloc_query_range_fn fn
,
1217 return xfs_rtalloc_query_range(rtg
, tp
, 0, rtg
->rtg_extents
- 1, fn
,
1221 /* Is the given extent all free? */
1223 xfs_rtalloc_extent_is_free(
1224 struct xfs_rtgroup
*rtg
,
1225 struct xfs_trans
*tp
,
1230 struct xfs_rtalloc_args args
= {
1231 .mp
= rtg_mount(rtg
),
1239 error
= xfs_rtcheck_range(&args
, start
, len
, 1, &end
, &matches
);
1240 xfs_rtbuf_cache_relse(&args
);
1248 /* Compute the number of rt extents tracked by a single bitmap block. */
1250 xfs_rtbitmap_rtx_per_rbmblock(
1251 struct xfs_mount
*mp
)
1253 unsigned int rbmblock_bytes
= mp
->m_sb
.sb_blocksize
;
1255 if (xfs_has_rtgroups(mp
))
1256 rbmblock_bytes
-= sizeof(struct xfs_rtbuf_blkinfo
);
1258 return rbmblock_bytes
* NBBY
;
1262 * Compute the number of rtbitmap blocks needed to track the given number of rt
1266 xfs_rtbitmap_blockcount_len(
1267 struct xfs_mount
*mp
,
1268 xfs_rtbxlen_t rtextents
)
1270 if (xfs_has_zoned(mp
))
1272 return howmany_64(rtextents
, xfs_rtbitmap_rtx_per_rbmblock(mp
));
1275 /* How many rt extents does each rtbitmap file track? */
1276 static inline xfs_rtbxlen_t
1277 xfs_rtbitmap_bitcount(
1278 struct xfs_mount
*mp
)
1280 if (!mp
->m_sb
.sb_rextents
)
1283 /* rtgroup size can be nonzero even if rextents is zero */
1284 if (xfs_has_rtgroups(mp
))
1285 return mp
->m_sb
.sb_rgextents
;
1287 return mp
->m_sb
.sb_rextents
;
1291 * Compute the number of rtbitmap blocks used for a given file system.
1294 xfs_rtbitmap_blockcount(
1295 struct xfs_mount
*mp
)
1297 return xfs_rtbitmap_blockcount_len(mp
, xfs_rtbitmap_bitcount(mp
));
1301 * Compute the geometry of the rtsummary file needed to track the given rt
1305 xfs_rtsummary_blockcount(
1306 struct xfs_mount
*mp
,
1307 unsigned int *rsumlevels
)
1309 xfs_rtbxlen_t rextents
= xfs_rtbitmap_bitcount(mp
);
1310 unsigned long long rsumwords
;
1312 if (xfs_has_zoned(mp
)) {
1317 *rsumlevels
= xfs_compute_rextslog(rextents
) + 1;
1318 rsumwords
= xfs_rtbitmap_blockcount_len(mp
, rextents
) * (*rsumlevels
);
1319 return howmany_64(rsumwords
, mp
->m_blockwsize
);
1323 xfs_rtfile_alloc_blocks(
1324 struct xfs_inode
*ip
,
1325 xfs_fileoff_t offset_fsb
,
1326 xfs_filblks_t count_fsb
,
1327 struct xfs_bmbt_irec
*map
)
1329 struct xfs_mount
*mp
= ip
->i_mount
;
1330 struct xfs_trans
*tp
;
1334 error
= xfs_trans_alloc(mp
, &M_RES(mp
)->tr_growrtalloc
,
1335 XFS_GROWFSRT_SPACE_RES(mp
, count_fsb
), 0, 0, &tp
);
1339 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
1340 xfs_trans_ijoin(tp
, ip
, XFS_ILOCK_EXCL
);
1342 error
= xfs_iext_count_extend(tp
, ip
, XFS_DATA_FORK
,
1343 XFS_IEXT_ADD_NOSPLIT_CNT
);
1345 goto out_trans_cancel
;
1347 error
= xfs_bmapi_write(tp
, ip
, offset_fsb
, count_fsb
,
1348 XFS_BMAPI_METADATA
, 0, map
, &nmap
);
1350 goto out_trans_cancel
;
1352 return xfs_trans_commit(tp
);
1355 xfs_trans_cancel(tp
);
1359 /* Get a buffer for the block. */
1361 xfs_rtfile_initialize_block(
1362 struct xfs_rtgroup
*rtg
,
1363 enum xfs_rtg_inodes type
,
1364 xfs_fsblock_t fsbno
,
1367 struct xfs_mount
*mp
= rtg_mount(rtg
);
1368 struct xfs_inode
*ip
= rtg
->rtg_inodes
[type
];
1369 struct xfs_trans
*tp
;
1372 const size_t copylen
= mp
->m_blockwsize
<< XFS_WORDLOG
;
1373 enum xfs_blft buf_type
;
1376 if (type
== XFS_RTGI_BITMAP
)
1377 buf_type
= XFS_BLFT_RTBITMAP_BUF
;
1378 else if (type
== XFS_RTGI_SUMMARY
)
1379 buf_type
= XFS_BLFT_RTSUMMARY_BUF
;
1383 error
= xfs_trans_alloc(mp
, &M_RES(mp
)->tr_growrtzero
, 0, 0, 0, &tp
);
1386 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
1387 xfs_trans_ijoin(tp
, ip
, XFS_ILOCK_EXCL
);
1389 error
= xfs_trans_get_buf(tp
, mp
->m_ddev_targp
,
1390 XFS_FSB_TO_DADDR(mp
, fsbno
), mp
->m_bsize
, 0, &bp
);
1392 xfs_trans_cancel(tp
);
1395 bufdata
= bp
->b_addr
;
1397 xfs_trans_buf_set_type(tp
, bp
, buf_type
);
1398 bp
->b_ops
= xfs_rtblock_ops(mp
, type
);
1400 if (xfs_has_rtgroups(mp
)) {
1401 struct xfs_rtbuf_blkinfo
*hdr
= bp
->b_addr
;
1403 if (type
== XFS_RTGI_BITMAP
)
1404 hdr
->rt_magic
= cpu_to_be32(XFS_RTBITMAP_MAGIC
);
1406 hdr
->rt_magic
= cpu_to_be32(XFS_RTSUMMARY_MAGIC
);
1407 hdr
->rt_owner
= cpu_to_be64(ip
->i_ino
);
1408 hdr
->rt_blkno
= cpu_to_be64(XFS_FSB_TO_DADDR(mp
, fsbno
));
1410 uuid_copy(&hdr
->rt_uuid
, &mp
->m_sb
.sb_meta_uuid
);
1412 bufdata
+= sizeof(*hdr
);
1416 memcpy(bufdata
, data
, copylen
);
1418 memset(bufdata
, 0, copylen
);
1419 xfs_trans_log_buf(tp
, bp
, 0, mp
->m_sb
.sb_blocksize
- 1);
1420 return xfs_trans_commit(tp
);
1424 * Allocate space to the bitmap or summary file, and zero it, for growfs.
1425 * @data must be a contiguous buffer large enough to fill all blocks in the
1426 * file; or NULL to initialize the contents to zeroes.
1429 xfs_rtfile_initialize_blocks(
1430 struct xfs_rtgroup
*rtg
,
1431 enum xfs_rtg_inodes type
,
1432 xfs_fileoff_t offset_fsb
, /* offset to start from */
1433 xfs_fileoff_t end_fsb
, /* offset to allocate to */
1434 void *data
) /* data to fill the blocks */
1436 struct xfs_mount
*mp
= rtg_mount(rtg
);
1437 const size_t copylen
= mp
->m_blockwsize
<< XFS_WORDLOG
;
1439 while (offset_fsb
< end_fsb
) {
1440 struct xfs_bmbt_irec map
;
1444 error
= xfs_rtfile_alloc_blocks(rtg
->rtg_inodes
[type
],
1445 offset_fsb
, end_fsb
- offset_fsb
, &map
);
1450 * Now we need to clear the allocated blocks.
1452 * Do this one block per transaction, to keep it simple.
1454 for (i
= 0; i
< map
.br_blockcount
; i
++) {
1455 error
= xfs_rtfile_initialize_block(rtg
, type
,
1456 map
.br_startblock
+ i
, data
);
1463 offset_fsb
= map
.br_startoff
+ map
.br_blockcount
;
1470 xfs_rtbitmap_create(
1471 struct xfs_rtgroup
*rtg
,
1472 struct xfs_inode
*ip
,
1473 struct xfs_trans
*tp
,
1476 struct xfs_mount
*mp
= rtg_mount(rtg
);
1478 ip
->i_disk_size
= mp
->m_sb
.sb_rbmblocks
* mp
->m_sb
.sb_blocksize
;
1479 if (init
&& !xfs_has_rtgroups(mp
)) {
1480 ip
->i_diflags
|= XFS_DIFLAG_NEWRTBM
;
1481 inode_set_atime(VFS_I(ip
), 0, 0);
1483 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
1488 xfs_rtsummary_create(
1489 struct xfs_rtgroup
*rtg
,
1490 struct xfs_inode
*ip
,
1491 struct xfs_trans
*tp
,
1494 struct xfs_mount
*mp
= rtg_mount(rtg
);
1496 ip
->i_disk_size
= mp
->m_rsumblocks
* mp
->m_sb
.sb_blocksize
;
1497 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);