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.h"
18 #include "xfs_trans_space.h"
19 #include "xfs_icache.h"
20 #include "xfs_rtalloc.h"
22 #include "xfs_rtbitmap.h"
25 * Read and return the summary information for a given extent size,
26 * bitmap block combination.
27 * Keeps track of a current summary block, so we don't keep reading
28 * it from the buffer cache.
32 struct xfs_rtalloc_args
*args
,
33 int log
, /* log2 of extent size */
34 xfs_fileoff_t bbno
, /* bitmap block number */
35 xfs_suminfo_t
*sum
) /* out: summary info for this block */
37 return xfs_rtmodify_summary_int(args
, log
, bbno
, 0, sum
);
41 * Return whether there are any free extents in the size range given
42 * by low and high, for the bitmap block bbno.
46 struct xfs_rtalloc_args
*args
,
47 int low
, /* low log2 extent size */
48 int high
, /* high log2 extent size */
49 xfs_fileoff_t bbno
, /* bitmap block number */
50 int *maxlog
) /* out: max log2 extent size free */
52 struct xfs_mount
*mp
= args
->mp
;
54 int log
; /* loop counter, log2 of ext. size */
55 xfs_suminfo_t sum
; /* summary data */
57 /* There are no extents at levels >= m_rsum_cache[bbno]. */
58 if (mp
->m_rsum_cache
) {
59 high
= min(high
, mp
->m_rsum_cache
[bbno
] - 1);
67 * Loop over logs of extent sizes.
69 for (log
= high
; log
>= low
; log
--) {
71 * Get one summary datum.
73 error
= xfs_rtget_summary(args
, log
, bbno
, &sum
);
78 * If there are any, return success.
86 * Found nothing, return failure.
90 /* There were no extents at levels > log. */
91 if (mp
->m_rsum_cache
&& log
+ 1 < mp
->m_rsum_cache
[bbno
])
92 mp
->m_rsum_cache
[bbno
] = log
+ 1;
98 * Copy and transform the summary file, given the old and new
99 * parameters in the mount structures.
103 struct xfs_rtalloc_args
*oargs
,
104 struct xfs_rtalloc_args
*nargs
)
106 xfs_fileoff_t bbno
; /* bitmap block number */
108 int log
; /* summary level number (log length) */
109 xfs_suminfo_t sum
; /* summary data */
111 for (log
= oargs
->mp
->m_rsumlevels
- 1; log
>= 0; log
--) {
112 for (bbno
= oargs
->mp
->m_sb
.sb_rbmblocks
- 1;
113 (xfs_srtblock_t
)bbno
>= 0;
115 error
= xfs_rtget_summary(oargs
, log
, bbno
, &sum
);
120 error
= xfs_rtmodify_summary(oargs
, log
, bbno
, -sum
);
123 error
= xfs_rtmodify_summary(nargs
, log
, bbno
, sum
);
131 xfs_rtbuf_cache_relse(oargs
);
135 * Mark an extent specified by start and len allocated.
136 * Updates all the summary information as well as the bitmap.
139 xfs_rtallocate_range(
140 struct xfs_rtalloc_args
*args
,
141 xfs_rtxnum_t start
, /* start rtext to allocate */
142 xfs_rtxlen_t len
) /* in/out: summary block number */
144 struct xfs_mount
*mp
= args
->mp
;
145 xfs_rtxnum_t end
; /* end of the allocated rtext */
147 xfs_rtxnum_t postblock
= 0; /* first rtext allocated > end */
148 xfs_rtxnum_t preblock
= 0; /* first rtext allocated < start */
150 end
= start
+ len
- 1;
152 * Assume we're allocating out of the middle of a free extent.
153 * We need to find the beginning and end of the extent so we can
154 * properly update the summary.
156 error
= xfs_rtfind_back(args
, start
, 0, &preblock
);
161 * Find the next allocated block (end of free extent).
163 error
= xfs_rtfind_forw(args
, end
, mp
->m_sb
.sb_rextents
- 1,
169 * Decrement the summary information corresponding to the entire
172 error
= xfs_rtmodify_summary(args
,
173 XFS_RTBLOCKLOG(postblock
+ 1 - preblock
),
174 xfs_rtx_to_rbmblock(mp
, preblock
), -1);
179 * If there are blocks not being allocated at the front of the
180 * old extent, add summary data for them to be free.
182 if (preblock
< start
) {
183 error
= xfs_rtmodify_summary(args
,
184 XFS_RTBLOCKLOG(start
- preblock
),
185 xfs_rtx_to_rbmblock(mp
, preblock
), 1);
191 * If there are blocks not being allocated at the end of the
192 * old extent, add summary data for them to be free.
194 if (postblock
> end
) {
195 error
= xfs_rtmodify_summary(args
,
196 XFS_RTBLOCKLOG(postblock
- end
),
197 xfs_rtx_to_rbmblock(mp
, end
+ 1), 1);
203 * Modify the bitmap to mark this extent allocated.
205 error
= xfs_rtmodify_range(args
, start
, len
, 0);
210 * Make sure we don't run off the end of the rt volume. Be careful that
211 * adjusting maxlen downwards doesn't cause us to fail the alignment checks.
213 static inline xfs_rtxlen_t
214 xfs_rtallocate_clamp_len(
215 struct xfs_mount
*mp
,
216 xfs_rtxnum_t startrtx
,
222 ret
= min(mp
->m_sb
.sb_rextents
, startrtx
+ rtxlen
) - startrtx
;
223 return rounddown(ret
, prod
);
227 * Attempt to allocate an extent minlen<=len<=maxlen starting from
228 * bitmap block bbno. If we don't get maxlen then use prod to trim
229 * the length, if given. Returns error; returns starting block in *rtx.
230 * The lengths are all in rtextents.
233 xfs_rtallocate_extent_block(
234 struct xfs_rtalloc_args
*args
,
235 xfs_fileoff_t bbno
, /* bitmap block number */
236 xfs_rtxlen_t minlen
, /* minimum length to allocate */
237 xfs_rtxlen_t maxlen
, /* maximum length to allocate */
238 xfs_rtxlen_t
*len
, /* out: actual length allocated */
239 xfs_rtxnum_t
*nextp
, /* out: next rtext to try */
240 xfs_rtxlen_t prod
, /* extent product factor */
241 xfs_rtxnum_t
*rtx
) /* out: start rtext allocated */
243 struct xfs_mount
*mp
= args
->mp
;
244 xfs_rtxnum_t besti
; /* best rtext found so far */
245 xfs_rtxnum_t bestlen
;/* best length found so far */
246 xfs_rtxnum_t end
; /* last rtext in chunk */
248 xfs_rtxnum_t i
; /* current rtext trying */
249 xfs_rtxnum_t next
; /* next rtext to try */
250 int stat
; /* status from internal calls */
253 * Loop over all the extents starting in this bitmap block,
254 * looking for one that's long enough.
256 for (i
= xfs_rbmblock_to_rtx(mp
, bbno
), besti
= -1, bestlen
= 0,
257 end
= xfs_rbmblock_to_rtx(mp
, bbno
+ 1) - 1;
260 /* Make sure we don't scan off the end of the rt volume. */
261 maxlen
= xfs_rtallocate_clamp_len(mp
, i
, maxlen
, prod
);
264 * See if there's a free extent of maxlen starting at i.
265 * If it's not so then next will contain the first non-free.
267 error
= xfs_rtcheck_range(args
, i
, maxlen
, 1, &next
, &stat
);
273 * i for maxlen is all free, allocate and return that.
275 error
= xfs_rtallocate_range(args
, i
, maxlen
);
284 * In the case where we have a variable-sized allocation
285 * request, figure out how big this free piece is,
286 * and if it's big enough for the minimum, and the best
287 * so far, remember it.
289 if (minlen
< maxlen
) {
290 xfs_rtxnum_t thislen
; /* this extent size */
293 if (thislen
>= minlen
&& thislen
> bestlen
) {
299 * If not done yet, find the start of the next free space.
302 error
= xfs_rtfind_forw(args
, next
, end
, &i
);
310 * Searched the whole thing & didn't find a maxlen free extent.
312 if (minlen
< maxlen
&& besti
!= -1) {
313 xfs_rtxlen_t p
; /* amount to trim length by */
316 * If size should be a multiple of prod, make that so.
319 div_u64_rem(bestlen
, prod
, &p
);
325 * Allocate besti for bestlen & return that.
327 error
= xfs_rtallocate_range(args
, besti
, bestlen
);
336 * Allocation failed. Set *nextp to the next block to try.
344 * Allocate an extent of length minlen<=len<=maxlen, starting at block
345 * bno. If we don't get maxlen then use prod to trim the length, if given.
346 * Returns error; returns starting block in *rtx.
347 * The lengths are all in rtextents.
350 xfs_rtallocate_extent_exact(
351 struct xfs_rtalloc_args
*args
,
352 xfs_rtxnum_t start
, /* starting rtext number to allocate */
353 xfs_rtxlen_t minlen
, /* minimum length to allocate */
354 xfs_rtxlen_t maxlen
, /* maximum length to allocate */
355 xfs_rtxlen_t
*len
, /* out: actual length allocated */
356 xfs_rtxlen_t prod
, /* extent product factor */
357 xfs_rtxnum_t
*rtx
) /* out: start rtext allocated */
360 xfs_rtxlen_t i
; /* extent length trimmed due to prod */
361 int isfree
; /* extent is free */
362 xfs_rtxnum_t next
; /* next rtext to try (dummy) */
364 ASSERT(minlen
% prod
== 0);
365 ASSERT(maxlen
% prod
== 0);
367 * Check if the range in question (for maxlen) is free.
369 error
= xfs_rtcheck_range(args
, start
, maxlen
, 1, &next
, &isfree
);
375 * If it is, allocate it and return success.
377 error
= xfs_rtallocate_range(args
, start
, maxlen
);
386 * If not, allocate what there is, if it's at least minlen.
388 maxlen
= next
- start
;
389 if (maxlen
< minlen
) {
391 * Failed, return failure status.
397 * Trim off tail of extent, if prod is specified.
399 if (prod
> 1 && (i
= maxlen
% prod
)) {
401 if (maxlen
< minlen
) {
403 * Now we can't do it, return failure status.
410 * Allocate what we can and return it.
412 error
= xfs_rtallocate_range(args
, start
, maxlen
);
422 * Allocate an extent of length minlen<=len<=maxlen, starting as near
423 * to start as possible. If we don't get maxlen then use prod to trim
424 * the length, if given. The lengths are all in rtextents.
427 xfs_rtallocate_extent_near(
428 struct xfs_rtalloc_args
*args
,
429 xfs_rtxnum_t start
, /* starting rtext number to allocate */
430 xfs_rtxlen_t minlen
, /* minimum length to allocate */
431 xfs_rtxlen_t maxlen
, /* maximum length to allocate */
432 xfs_rtxlen_t
*len
, /* out: actual length allocated */
433 xfs_rtxlen_t prod
, /* extent product factor */
434 xfs_rtxnum_t
*rtx
) /* out: start rtext allocated */
436 struct xfs_mount
*mp
= args
->mp
;
437 int maxlog
; /* max useful extent from summary */
438 xfs_fileoff_t bbno
; /* bitmap block number */
440 int i
; /* bitmap block offset (loop control) */
441 int j
; /* secondary loop control */
442 int log2len
; /* log2 of minlen */
443 xfs_rtxnum_t n
; /* next rtext to try */
444 xfs_rtxnum_t r
; /* result rtext */
446 ASSERT(minlen
% prod
== 0);
447 ASSERT(maxlen
% prod
== 0);
450 * If the block number given is off the end, silently set it to
453 if (start
>= mp
->m_sb
.sb_rextents
)
454 start
= mp
->m_sb
.sb_rextents
- 1;
456 /* Make sure we don't run off the end of the rt volume. */
457 maxlen
= xfs_rtallocate_clamp_len(mp
, start
, maxlen
, prod
);
458 if (maxlen
< minlen
) {
464 * Try the exact allocation first.
466 error
= xfs_rtallocate_extent_exact(args
, start
, minlen
, maxlen
, len
,
472 * If the exact allocation worked, return that.
474 if (r
!= NULLRTEXTNO
) {
478 bbno
= xfs_rtx_to_rbmblock(mp
, start
);
482 log2len
= xfs_highbit32(minlen
);
484 * Loop over all bitmap blocks (bbno + i is current block).
488 * Get summary information of extents of all useful levels
489 * starting in this bitmap block.
491 error
= xfs_rtany_summary(args
, log2len
, mp
->m_rsumlevels
- 1,
497 * If there are any useful extents starting here, try
501 xfs_extlen_t maxavail
=
502 min_t(xfs_rtblock_t
, maxlen
,
503 (1ULL << (maxlog
+ 1)) - 1);
505 * On the positive side of the starting location.
509 * Try to allocate an extent starting in
512 error
= xfs_rtallocate_extent_block(args
,
513 bbno
+ i
, minlen
, maxavail
, len
,
519 * If it worked, return it.
521 if (r
!= NULLRTEXTNO
) {
527 * On the negative side of the starting location.
533 * Loop backwards to find the end of the extent
534 * we found in the realtime summary.
536 * maxblocks is the maximum possible number of
537 * bitmap blocks from the start of the extent
538 * to the end of the extent.
542 else if (maxlog
< mp
->m_blkbit_log
)
545 maxblocks
= 2 << (maxlog
- mp
->m_blkbit_log
);
548 * We need to check bbno + i + maxblocks down to
549 * bbno + i. We already checked bbno down to
550 * bbno + j + 1, so we don't need to check those
553 j
= min(i
+ maxblocks
, j
);
554 for (; j
>= i
; j
--) {
555 error
= xfs_rtallocate_extent_block(args
,
557 maxavail
, len
, &n
, prod
,
563 * If it works, return the extent.
565 if (r
!= NULLRTEXTNO
) {
573 * Loop control. If we were on the positive side, and there's
574 * still more blocks on the negative side, go there.
576 if (i
> 0 && (int)bbno
- i
>= 0)
579 * If positive, and no more negative, but there are more
580 * positive, go there.
582 else if (i
> 0 && (int)bbno
+ i
< mp
->m_sb
.sb_rbmblocks
- 1)
585 * If negative or 0 (just started), and there are positive
586 * blocks to go, go there. The 0 case moves to block 1.
588 else if (i
<= 0 && (int)bbno
- i
< mp
->m_sb
.sb_rbmblocks
- 1)
591 * If negative or 0 and there are more negative blocks,
594 else if (i
<= 0 && (int)bbno
+ i
> 0)
597 * Must be done. Return failure.
607 * Allocate an extent of length minlen<=len<=maxlen, with no position
608 * specified. If we don't get maxlen then use prod to trim
609 * the length, if given. The lengths are all in rtextents.
612 xfs_rtallocate_extent_size(
613 struct xfs_rtalloc_args
*args
,
614 xfs_rtxlen_t minlen
, /* minimum length to allocate */
615 xfs_rtxlen_t maxlen
, /* maximum length to allocate */
616 xfs_rtxlen_t
*len
, /* out: actual length allocated */
617 xfs_rtxlen_t prod
, /* extent product factor */
618 xfs_rtxnum_t
*rtx
) /* out: start rtext allocated */
620 struct xfs_mount
*mp
= args
->mp
;
622 xfs_fileoff_t i
; /* bitmap block number */
623 int l
; /* level number (loop control) */
624 xfs_rtxnum_t n
; /* next rtext to be tried */
625 xfs_rtxnum_t r
; /* result rtext number */
626 xfs_suminfo_t sum
; /* summary information for extents */
628 ASSERT(minlen
% prod
== 0);
629 ASSERT(maxlen
% prod
== 0);
633 * Loop over all the levels starting with maxlen.
634 * At each level, look at all the bitmap blocks, to see if there
635 * are extents starting there that are long enough (>= maxlen).
636 * Note, only on the initial level can the allocation fail if
637 * the summary says there's an extent.
639 for (l
= xfs_highbit32(maxlen
); l
< mp
->m_rsumlevels
; l
++) {
641 * Loop over all the bitmap blocks.
643 for (i
= 0; i
< mp
->m_sb
.sb_rbmblocks
; i
++) {
645 * Get the summary for this level/block.
647 error
= xfs_rtget_summary(args
, l
, i
, &sum
);
652 * Nothing there, on to the next block.
657 * Try allocating the extent.
659 error
= xfs_rtallocate_extent_block(args
, i
, maxlen
,
660 maxlen
, len
, &n
, prod
, &r
);
665 * If it worked, return that.
667 if (r
!= NULLRTEXTNO
) {
672 * If the "next block to try" returned from the
673 * allocator is beyond the next bitmap block,
674 * skip to that bitmap block.
676 if (xfs_rtx_to_rbmblock(mp
, n
) > i
+ 1)
677 i
= xfs_rtx_to_rbmblock(mp
, n
) - 1;
681 * Didn't find any maxlen blocks. Try smaller ones, unless
682 * we're asking for a fixed size extent.
684 if (minlen
> --maxlen
) {
692 * Loop over sizes, from maxlen down to minlen.
693 * This time, when we do the allocations, allow smaller ones
696 for (l
= xfs_highbit32(maxlen
); l
>= xfs_highbit32(minlen
); l
--) {
698 * Loop over all the bitmap blocks, try an allocation
699 * starting in that block.
701 for (i
= 0; i
< mp
->m_sb
.sb_rbmblocks
; i
++) {
703 * Get the summary information for this level/block.
705 error
= xfs_rtget_summary(args
, l
, i
, &sum
);
710 * If nothing there, go on to next.
715 * Try the allocation. Make sure the specified
716 * minlen/maxlen are in the possible range for
717 * this summary level.
719 error
= xfs_rtallocate_extent_block(args
, i
,
720 XFS_RTMAX(minlen
, 1 << l
),
721 XFS_RTMIN(maxlen
, (1 << (l
+ 1)) - 1),
727 * If it worked, return that extent.
729 if (r
!= NULLRTEXTNO
) {
734 * If the "next block to try" returned from the
735 * allocator is beyond the next bitmap block,
736 * skip to that bitmap block.
738 if (xfs_rtx_to_rbmblock(mp
, n
) > i
+ 1)
739 i
= xfs_rtx_to_rbmblock(mp
, n
) - 1;
743 * Got nothing, return failure.
750 * Allocate space to the bitmap or summary file, and zero it, for growfs.
754 struct xfs_mount
*mp
, /* file system mount point */
755 xfs_extlen_t oblocks
, /* old count of blocks */
756 xfs_extlen_t nblocks
, /* new count of blocks */
757 struct xfs_inode
*ip
) /* inode (bitmap/summary) */
759 xfs_fileoff_t bno
; /* block number in file */
760 struct xfs_buf
*bp
; /* temporary buffer for zeroing */
761 xfs_daddr_t d
; /* disk block address */
762 int error
; /* error return value */
763 xfs_fsblock_t fsbno
; /* filesystem block for bno */
764 struct xfs_bmbt_irec map
; /* block map output */
765 int nmap
; /* number of block maps */
766 int resblks
; /* space reservation */
767 enum xfs_blft buf_type
;
768 struct xfs_trans
*tp
;
770 if (ip
== mp
->m_rsumip
)
771 buf_type
= XFS_BLFT_RTSUMMARY_BUF
;
773 buf_type
= XFS_BLFT_RTBITMAP_BUF
;
776 * Allocate space to the file, as necessary.
778 while (oblocks
< nblocks
) {
779 resblks
= XFS_GROWFSRT_SPACE_RES(mp
, nblocks
- oblocks
);
781 * Reserve space & log for one extent added to the file.
783 error
= xfs_trans_alloc(mp
, &M_RES(mp
)->tr_growrtalloc
, resblks
,
790 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
791 xfs_trans_ijoin(tp
, ip
, XFS_ILOCK_EXCL
);
793 error
= xfs_iext_count_may_overflow(ip
, XFS_DATA_FORK
,
794 XFS_IEXT_ADD_NOSPLIT_CNT
);
796 error
= xfs_iext_count_upgrade(tp
, ip
,
797 XFS_IEXT_ADD_NOSPLIT_CNT
);
799 goto out_trans_cancel
;
802 * Allocate blocks to the bitmap file.
805 error
= xfs_bmapi_write(tp
, ip
, oblocks
, nblocks
- oblocks
,
806 XFS_BMAPI_METADATA
, 0, &map
, &nmap
);
807 if (!error
&& nmap
< 1)
810 goto out_trans_cancel
;
812 * Free any blocks freed up in the transaction, then commit.
814 error
= xfs_trans_commit(tp
);
818 * Now we need to clear the allocated blocks.
819 * Do this one block per transaction, to keep it simple.
821 for (bno
= map
.br_startoff
, fsbno
= map
.br_startblock
;
822 bno
< map
.br_startoff
+ map
.br_blockcount
;
825 * Reserve log for one block zeroing.
827 error
= xfs_trans_alloc(mp
, &M_RES(mp
)->tr_growrtzero
,
832 * Lock the bitmap inode.
834 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
835 xfs_trans_ijoin(tp
, ip
, XFS_ILOCK_EXCL
);
837 * Get a buffer for the block.
839 d
= XFS_FSB_TO_DADDR(mp
, fsbno
);
840 error
= xfs_trans_get_buf(tp
, mp
->m_ddev_targp
, d
,
841 mp
->m_bsize
, 0, &bp
);
843 goto out_trans_cancel
;
845 xfs_trans_buf_set_type(tp
, bp
, buf_type
);
846 bp
->b_ops
= &xfs_rtbuf_ops
;
847 memset(bp
->b_addr
, 0, mp
->m_sb
.sb_blocksize
);
848 xfs_trans_log_buf(tp
, bp
, 0, mp
->m_sb
.sb_blocksize
- 1);
850 * Commit the transaction.
852 error
= xfs_trans_commit(tp
);
857 * Go on to the next extent, if any.
859 oblocks
= map
.br_startoff
+ map
.br_blockcount
;
865 xfs_trans_cancel(tp
);
870 xfs_alloc_rsum_cache(
871 xfs_mount_t
*mp
, /* file system mount structure */
872 xfs_extlen_t rbmblocks
) /* number of rt bitmap blocks */
875 * The rsum cache is initialized to the maximum value, which is
876 * trivially an upper bound on the maximum level with any free extents.
877 * We can continue without the cache if it couldn't be allocated.
879 mp
->m_rsum_cache
= kvmalloc(rbmblocks
, GFP_KERNEL
);
880 if (mp
->m_rsum_cache
)
881 memset(mp
->m_rsum_cache
, -1, rbmblocks
);
883 xfs_warn(mp
, "could not allocate realtime summary cache");
887 * Visible (exported) functions.
891 * Grow the realtime area of the filesystem.
895 xfs_mount_t
*mp
, /* mount point for filesystem */
896 xfs_growfs_rt_t
*in
) /* growfs rt input struct */
898 xfs_fileoff_t bmbno
; /* bitmap block number */
899 struct xfs_buf
*bp
; /* temporary buffer */
900 int error
; /* error return value */
901 xfs_mount_t
*nmp
; /* new (fake) mount structure */
902 xfs_rfsblock_t nrblocks
; /* new number of realtime blocks */
903 xfs_extlen_t nrbmblocks
; /* new number of rt bitmap blocks */
904 xfs_rtxnum_t nrextents
; /* new number of realtime extents */
905 uint8_t nrextslog
; /* new log2 of sb_rextents */
906 xfs_extlen_t nrsumblocks
; /* new number of summary blocks */
907 uint nrsumlevels
; /* new rt summary levels */
908 uint nrsumsize
; /* new size of rt summary, bytes */
909 xfs_sb_t
*nsbp
; /* new superblock */
910 xfs_extlen_t rbmblocks
; /* current number of rt bitmap blocks */
911 xfs_extlen_t rsumblocks
; /* current number of rt summary blks */
912 xfs_sb_t
*sbp
; /* old superblock */
913 uint8_t *rsum_cache
; /* old summary cache */
917 if (!capable(CAP_SYS_ADMIN
))
920 /* Needs to have been mounted with an rt device. */
921 if (!XFS_IS_REALTIME_MOUNT(mp
))
924 * Mount should fail if the rt bitmap/summary files don't load, but
925 * we'll check anyway.
927 if (!mp
->m_rbmip
|| !mp
->m_rsumip
)
930 /* Shrink not supported. */
931 if (in
->newblocks
<= sbp
->sb_rblocks
)
934 /* Can only change rt extent size when adding rt volume. */
935 if (sbp
->sb_rblocks
> 0 && in
->extsize
!= sbp
->sb_rextsize
)
938 /* Range check the extent size. */
939 if (XFS_FSB_TO_B(mp
, in
->extsize
) > XFS_MAX_RTEXTSIZE
||
940 XFS_FSB_TO_B(mp
, in
->extsize
) < XFS_MIN_RTEXTSIZE
)
943 /* Unsupported realtime features. */
944 if (xfs_has_rmapbt(mp
) || xfs_has_reflink(mp
) || xfs_has_quota(mp
))
947 nrblocks
= in
->newblocks
;
948 error
= xfs_sb_validate_fsb_count(sbp
, nrblocks
);
952 * Read in the last block of the device, make sure it exists.
954 error
= xfs_buf_read_uncached(mp
->m_rtdev_targp
,
955 XFS_FSB_TO_BB(mp
, nrblocks
- 1),
956 XFS_FSB_TO_BB(mp
, 1), 0, &bp
, NULL
);
962 * Calculate new parameters. These are the final values to be reached.
964 nrextents
= nrblocks
;
965 do_div(nrextents
, in
->extsize
);
966 nrbmblocks
= xfs_rtbitmap_blockcount(mp
, nrextents
);
967 nrextslog
= xfs_highbit32(nrextents
);
968 nrsumlevels
= nrextslog
+ 1;
969 nrsumblocks
= xfs_rtsummary_blockcount(mp
, nrsumlevels
, nrbmblocks
);
970 nrsumsize
= XFS_FSB_TO_B(mp
, nrsumblocks
);
972 * New summary size can't be more than half the size of
973 * the log. This prevents us from getting a log overflow,
974 * since we'll log basically the whole summary file at once.
976 if (nrsumblocks
> (mp
->m_sb
.sb_logblocks
>> 1))
979 * Get the old block counts for bitmap and summary inodes.
980 * These can't change since other growfs callers are locked out.
982 rbmblocks
= XFS_B_TO_FSB(mp
, mp
->m_rbmip
->i_disk_size
);
983 rsumblocks
= XFS_B_TO_FSB(mp
, mp
->m_rsumip
->i_disk_size
);
985 * Allocate space to the bitmap and summary files, as necessary.
987 error
= xfs_growfs_rt_alloc(mp
, rbmblocks
, nrbmblocks
, mp
->m_rbmip
);
990 error
= xfs_growfs_rt_alloc(mp
, rsumblocks
, nrsumblocks
, mp
->m_rsumip
);
994 rsum_cache
= mp
->m_rsum_cache
;
995 if (nrbmblocks
!= sbp
->sb_rbmblocks
)
996 xfs_alloc_rsum_cache(mp
, nrbmblocks
);
999 * Allocate a new (fake) mount/sb.
1001 nmp
= kmem_alloc(sizeof(*nmp
), 0);
1003 * Loop over the bitmap blocks.
1004 * We will do everything one bitmap block at a time.
1005 * Skip the current block if it is exactly full.
1006 * This also deals with the case where there were no rtextents before.
1008 for (bmbno
= sbp
->sb_rbmblocks
-
1009 ((sbp
->sb_rextents
& ((1 << mp
->m_blkbit_log
) - 1)) != 0);
1012 struct xfs_rtalloc_args args
= {
1015 struct xfs_rtalloc_args nargs
= {
1018 struct xfs_trans
*tp
;
1019 xfs_rfsblock_t nrblocks_step
;
1024 * Calculate new sb and mount fields for this round.
1026 nsbp
->sb_rextsize
= in
->extsize
;
1027 nmp
->m_rtxblklog
= -1; /* don't use shift or masking */
1028 nsbp
->sb_rbmblocks
= bmbno
+ 1;
1029 nrblocks_step
= (bmbno
+ 1) * NBBY
* nsbp
->sb_blocksize
*
1031 nsbp
->sb_rblocks
= min(nrblocks
, nrblocks_step
);
1032 nsbp
->sb_rextents
= xfs_rtb_to_rtx(nmp
, nsbp
->sb_rblocks
);
1033 ASSERT(nsbp
->sb_rextents
!= 0);
1034 nsbp
->sb_rextslog
= xfs_highbit32(nsbp
->sb_rextents
);
1035 nrsumlevels
= nmp
->m_rsumlevels
= nsbp
->sb_rextslog
+ 1;
1036 nrsumblocks
= xfs_rtsummary_blockcount(mp
, nrsumlevels
,
1037 nsbp
->sb_rbmblocks
);
1038 nmp
->m_rsumsize
= nrsumsize
= XFS_FSB_TO_B(mp
, nrsumblocks
);
1040 * Start a transaction, get the log reservation.
1042 error
= xfs_trans_alloc(mp
, &M_RES(mp
)->tr_growrtfree
, 0, 0, 0,
1050 * Lock out other callers by grabbing the bitmap inode lock.
1052 xfs_ilock(mp
->m_rbmip
, XFS_ILOCK_EXCL
| XFS_ILOCK_RTBITMAP
);
1053 xfs_trans_ijoin(tp
, mp
->m_rbmip
, XFS_ILOCK_EXCL
);
1055 * Update the bitmap inode's size ondisk and incore. We need
1056 * to update the incore size so that inode inactivation won't
1057 * punch what it thinks are "posteof" blocks.
1059 mp
->m_rbmip
->i_disk_size
=
1060 nsbp
->sb_rbmblocks
* nsbp
->sb_blocksize
;
1061 i_size_write(VFS_I(mp
->m_rbmip
), mp
->m_rbmip
->i_disk_size
);
1062 xfs_trans_log_inode(tp
, mp
->m_rbmip
, XFS_ILOG_CORE
);
1064 * Get the summary inode into the transaction.
1066 xfs_ilock(mp
->m_rsumip
, XFS_ILOCK_EXCL
| XFS_ILOCK_RTSUM
);
1067 xfs_trans_ijoin(tp
, mp
->m_rsumip
, XFS_ILOCK_EXCL
);
1069 * Update the summary inode's size. We need to update the
1070 * incore size so that inode inactivation won't punch what it
1071 * thinks are "posteof" blocks.
1073 mp
->m_rsumip
->i_disk_size
= nmp
->m_rsumsize
;
1074 i_size_write(VFS_I(mp
->m_rsumip
), mp
->m_rsumip
->i_disk_size
);
1075 xfs_trans_log_inode(tp
, mp
->m_rsumip
, XFS_ILOG_CORE
);
1077 * Copy summary data from old to new sizes.
1078 * Do this when the real size (not block-aligned) changes.
1080 if (sbp
->sb_rbmblocks
!= nsbp
->sb_rbmblocks
||
1081 mp
->m_rsumlevels
!= nmp
->m_rsumlevels
) {
1082 error
= xfs_rtcopy_summary(&args
, &nargs
);
1087 * Update superblock fields.
1089 if (nsbp
->sb_rextsize
!= sbp
->sb_rextsize
)
1090 xfs_trans_mod_sb(tp
, XFS_TRANS_SB_REXTSIZE
,
1091 nsbp
->sb_rextsize
- sbp
->sb_rextsize
);
1092 if (nsbp
->sb_rbmblocks
!= sbp
->sb_rbmblocks
)
1093 xfs_trans_mod_sb(tp
, XFS_TRANS_SB_RBMBLOCKS
,
1094 nsbp
->sb_rbmblocks
- sbp
->sb_rbmblocks
);
1095 if (nsbp
->sb_rblocks
!= sbp
->sb_rblocks
)
1096 xfs_trans_mod_sb(tp
, XFS_TRANS_SB_RBLOCKS
,
1097 nsbp
->sb_rblocks
- sbp
->sb_rblocks
);
1098 if (nsbp
->sb_rextents
!= sbp
->sb_rextents
)
1099 xfs_trans_mod_sb(tp
, XFS_TRANS_SB_REXTENTS
,
1100 nsbp
->sb_rextents
- sbp
->sb_rextents
);
1101 if (nsbp
->sb_rextslog
!= sbp
->sb_rextslog
)
1102 xfs_trans_mod_sb(tp
, XFS_TRANS_SB_REXTSLOG
,
1103 nsbp
->sb_rextslog
- sbp
->sb_rextslog
);
1107 error
= xfs_rtfree_range(&nargs
, sbp
->sb_rextents
,
1108 nsbp
->sb_rextents
- sbp
->sb_rextents
);
1109 xfs_rtbuf_cache_relse(&nargs
);
1112 xfs_trans_cancel(tp
);
1116 * Mark more blocks free in the superblock.
1118 xfs_trans_mod_sb(tp
, XFS_TRANS_SB_FREXTENTS
,
1119 nsbp
->sb_rextents
- sbp
->sb_rextents
);
1121 * Update mp values into the real mp structure.
1123 mp
->m_rsumlevels
= nrsumlevels
;
1124 mp
->m_rsumsize
= nrsumsize
;
1126 error
= xfs_trans_commit(tp
);
1130 /* Ensure the mount RT feature flag is now set. */
1131 mp
->m_features
|= XFS_FEAT_REALTIME
;
1136 /* Update secondary superblocks now the physical grow has completed */
1137 error
= xfs_update_secondary_sbs(mp
);
1141 * Free the fake mp structure.
1146 * If we had to allocate a new rsum_cache, we either need to free the
1147 * old one (if we succeeded) or free the new one and restore the old one
1148 * (if there was an error).
1150 if (rsum_cache
!= mp
->m_rsum_cache
) {
1152 kmem_free(mp
->m_rsum_cache
);
1153 mp
->m_rsum_cache
= rsum_cache
;
1155 kmem_free(rsum_cache
);
1163 * Allocate an extent in the realtime subvolume, with the usual allocation
1164 * parameters. The length units are all in realtime extents, as is the
1165 * result block number.
1168 xfs_rtallocate_extent(
1169 struct xfs_trans
*tp
,
1170 xfs_rtxnum_t start
, /* starting rtext number to allocate */
1171 xfs_rtxlen_t minlen
, /* minimum length to allocate */
1172 xfs_rtxlen_t maxlen
, /* maximum length to allocate */
1173 xfs_rtxlen_t
*len
, /* out: actual length allocated */
1174 int wasdel
, /* was a delayed allocation extent */
1175 xfs_rtxlen_t prod
, /* extent product factor */
1176 xfs_rtxnum_t
*rtblock
) /* out: start rtext allocated */
1178 struct xfs_rtalloc_args args
= {
1182 int error
; /* error value */
1183 xfs_rtxnum_t r
; /* result allocated rtext */
1185 ASSERT(xfs_isilocked(args
.mp
->m_rbmip
, XFS_ILOCK_EXCL
));
1186 ASSERT(minlen
> 0 && minlen
<= maxlen
);
1189 * If prod is set then figure out what to do to minlen and maxlen.
1194 if ((i
= maxlen
% prod
))
1196 if ((i
= minlen
% prod
))
1198 if (maxlen
< minlen
) {
1199 *rtblock
= NULLRTEXTNO
;
1206 error
= xfs_rtallocate_extent_size(&args
, minlen
,
1207 maxlen
, len
, prod
, &r
);
1209 error
= xfs_rtallocate_extent_near(&args
, start
, minlen
,
1210 maxlen
, len
, prod
, &r
);
1213 xfs_rtbuf_cache_relse(&args
);
1218 * If it worked, update the superblock.
1220 if (r
!= NULLRTEXTNO
) {
1221 long slen
= (long)*len
;
1223 ASSERT(*len
>= minlen
&& *len
<= maxlen
);
1225 xfs_trans_mod_sb(tp
, XFS_TRANS_SB_RES_FREXTENTS
, -slen
);
1227 xfs_trans_mod_sb(tp
, XFS_TRANS_SB_FREXTENTS
, -slen
);
1228 } else if (prod
> 1) {
1238 * Initialize realtime fields in the mount structure.
1242 struct xfs_mount
*mp
) /* file system mount structure */
1244 struct xfs_buf
*bp
; /* buffer for last block of subvolume */
1245 struct xfs_sb
*sbp
; /* filesystem superblock copy in mount */
1246 xfs_daddr_t d
; /* address of last block of subvolume */
1247 unsigned int rsumblocks
;
1251 if (sbp
->sb_rblocks
== 0)
1253 if (mp
->m_rtdev_targp
== NULL
) {
1255 "Filesystem has a realtime volume, use rtdev=device option");
1258 mp
->m_rsumlevels
= sbp
->sb_rextslog
+ 1;
1259 rsumblocks
= xfs_rtsummary_blockcount(mp
, mp
->m_rsumlevels
,
1260 mp
->m_sb
.sb_rbmblocks
);
1261 mp
->m_rsumsize
= XFS_FSB_TO_B(mp
, rsumblocks
);
1262 mp
->m_rbmip
= mp
->m_rsumip
= NULL
;
1264 * Check that the realtime section is an ok size.
1266 d
= (xfs_daddr_t
)XFS_FSB_TO_BB(mp
, mp
->m_sb
.sb_rblocks
);
1267 if (XFS_BB_TO_FSB(mp
, d
) != mp
->m_sb
.sb_rblocks
) {
1268 xfs_warn(mp
, "realtime mount -- %llu != %llu",
1269 (unsigned long long) XFS_BB_TO_FSB(mp
, d
),
1270 (unsigned long long) mp
->m_sb
.sb_rblocks
);
1273 error
= xfs_buf_read_uncached(mp
->m_rtdev_targp
,
1274 d
- XFS_FSB_TO_BB(mp
, 1),
1275 XFS_FSB_TO_BB(mp
, 1), 0, &bp
, NULL
);
1277 xfs_warn(mp
, "realtime device size check failed");
1285 xfs_rtalloc_count_frextent(
1286 struct xfs_mount
*mp
,
1287 struct xfs_trans
*tp
,
1288 const struct xfs_rtalloc_rec
*rec
,
1291 uint64_t *valp
= priv
;
1293 *valp
+= rec
->ar_extcount
;
1298 * Reinitialize the number of free realtime extents from the realtime bitmap.
1299 * Callers must ensure that there is no other activity in the filesystem.
1302 xfs_rtalloc_reinit_frextents(
1303 struct xfs_mount
*mp
)
1308 xfs_ilock(mp
->m_rbmip
, XFS_ILOCK_SHARED
| XFS_ILOCK_RTBITMAP
);
1309 error
= xfs_rtalloc_query_all(mp
, NULL
, xfs_rtalloc_count_frextent
,
1311 xfs_iunlock(mp
->m_rbmip
, XFS_ILOCK_SHARED
| XFS_ILOCK_RTBITMAP
);
1315 spin_lock(&mp
->m_sb_lock
);
1316 mp
->m_sb
.sb_frextents
= val
;
1317 spin_unlock(&mp
->m_sb_lock
);
1318 percpu_counter_set(&mp
->m_frextents
, mp
->m_sb
.sb_frextents
);
1323 * Read in the bmbt of an rt metadata inode so that we never have to load them
1324 * at runtime. This enables the use of shared ILOCKs for rtbitmap scans. Use
1325 * an empty transaction to avoid deadlocking on loops in the bmbt.
1328 xfs_rtmount_iread_extents(
1329 struct xfs_inode
*ip
,
1330 unsigned int lock_class
)
1332 struct xfs_trans
*tp
;
1335 error
= xfs_trans_alloc_empty(ip
->i_mount
, &tp
);
1339 xfs_ilock(ip
, XFS_ILOCK_EXCL
| lock_class
);
1341 error
= xfs_iread_extents(tp
, ip
, XFS_DATA_FORK
);
1345 if (xfs_inode_has_attr_fork(ip
)) {
1346 error
= xfs_iread_extents(tp
, ip
, XFS_ATTR_FORK
);
1352 xfs_iunlock(ip
, XFS_ILOCK_EXCL
| lock_class
);
1353 xfs_trans_cancel(tp
);
1358 * Get the bitmap and summary inodes and the summary cache into the mount
1359 * structure at mount time.
1363 xfs_mount_t
*mp
) /* file system mount structure */
1365 int error
; /* error return value */
1369 error
= xfs_iget(mp
, NULL
, sbp
->sb_rbmino
, 0, 0, &mp
->m_rbmip
);
1372 ASSERT(mp
->m_rbmip
!= NULL
);
1374 error
= xfs_rtmount_iread_extents(mp
->m_rbmip
, XFS_ILOCK_RTBITMAP
);
1376 goto out_rele_bitmap
;
1378 error
= xfs_iget(mp
, NULL
, sbp
->sb_rsumino
, 0, 0, &mp
->m_rsumip
);
1380 goto out_rele_bitmap
;
1381 ASSERT(mp
->m_rsumip
!= NULL
);
1383 error
= xfs_rtmount_iread_extents(mp
->m_rsumip
, XFS_ILOCK_RTSUM
);
1385 goto out_rele_summary
;
1387 xfs_alloc_rsum_cache(mp
, sbp
->sb_rbmblocks
);
1391 xfs_irele(mp
->m_rsumip
);
1393 xfs_irele(mp
->m_rbmip
);
1398 xfs_rtunmount_inodes(
1399 struct xfs_mount
*mp
)
1401 kmem_free(mp
->m_rsum_cache
);
1403 xfs_irele(mp
->m_rbmip
);
1405 xfs_irele(mp
->m_rsumip
);
1409 * Pick an extent for allocation at the start of a new realtime file.
1410 * Use the sequence number stored in the atime field of the bitmap inode.
1411 * Translate this to a fraction of the rtextents, and return the product
1412 * of rtextents and the fraction.
1413 * The fraction sequence is 0, 1/2, 1/4, 3/4, 1/8, ..., 7/8, 1/16, ...
1417 xfs_mount_t
*mp
, /* file system mount point */
1418 xfs_trans_t
*tp
, /* transaction pointer */
1419 xfs_rtxlen_t len
, /* allocation length (rtextents) */
1420 xfs_rtxnum_t
*pick
) /* result rt extent */
1422 xfs_rtxnum_t b
; /* result rtext */
1423 int log2
; /* log of sequence number */
1424 uint64_t resid
; /* residual after log removed */
1425 uint64_t seq
; /* sequence number of file creation */
1426 struct timespec64 ts
; /* timespec in inode */
1428 ASSERT(xfs_isilocked(mp
->m_rbmip
, XFS_ILOCK_EXCL
));
1430 ts
= inode_get_atime(VFS_I(mp
->m_rbmip
));
1431 if (!(mp
->m_rbmip
->i_diflags
& XFS_DIFLAG_NEWRTBM
)) {
1432 mp
->m_rbmip
->i_diflags
|= XFS_DIFLAG_NEWRTBM
;
1437 if ((log2
= xfs_highbit64(seq
)) == -1)
1440 resid
= seq
- (1ULL << log2
);
1441 b
= (mp
->m_sb
.sb_rextents
* ((resid
<< 1) + 1ULL)) >>
1443 if (b
>= mp
->m_sb
.sb_rextents
)
1444 div64_u64_rem(b
, mp
->m_sb
.sb_rextents
, &b
);
1445 if (b
+ len
> mp
->m_sb
.sb_rextents
)
1446 b
= mp
->m_sb
.sb_rextents
- len
;
1448 ts
.tv_sec
= seq
+ 1;
1449 inode_set_atime_to_ts(VFS_I(mp
->m_rbmip
), ts
);
1450 xfs_trans_log_inode(tp
, mp
->m_rbmip
, XFS_ILOG_CORE
);