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"
15 #include "xfs_alloc.h"
17 #include "xfs_bmap_btree.h"
18 #include "xfs_bmap_util.h"
19 #include "xfs_trans.h"
20 #include "xfs_trans_space.h"
21 #include "xfs_icache.h"
22 #include "xfs_rtalloc.h"
24 #include "xfs_rtbitmap.h"
25 #include "xfs_quota.h"
26 #include "xfs_log_priv.h"
27 #include "xfs_health.h"
30 * Return whether there are any free extents in the size range given
31 * by low and high, for the bitmap block bbno.
35 struct xfs_rtalloc_args *args,
36 int low, /* low log2 extent size */
37 int high, /* high log2 extent size */
38 xfs_fileoff_t bbno, /* bitmap block number */
39 int *maxlog) /* out: max log2 extent size free */
41 struct xfs_mount *mp = args->mp;
43 int log; /* loop counter, log2 of ext. size */
44 xfs_suminfo_t sum; /* summary data */
46 /* There are no extents at levels >= m_rsum_cache[bbno]. */
47 if (mp->m_rsum_cache) {
48 high = min(high, mp->m_rsum_cache[bbno] - 1);
56 * Loop over logs of extent sizes.
58 for (log = high; log >= low; log--) {
60 * Get one summary datum.
62 error = xfs_rtget_summary(args, log, bbno, &sum);
67 * If there are any, return success.
75 * Found nothing, return failure.
79 /* There were no extents at levels > log. */
80 if (mp->m_rsum_cache && log + 1 < mp->m_rsum_cache[bbno])
81 mp->m_rsum_cache[bbno] = log + 1;
87 * Copy and transform the summary file, given the old and new
88 * parameters in the mount structures.
92 struct xfs_rtalloc_args *oargs,
93 struct xfs_rtalloc_args *nargs)
95 xfs_fileoff_t bbno; /* bitmap block number */
97 int log; /* summary level number (log length) */
98 xfs_suminfo_t sum; /* summary data */
100 for (log = oargs->mp->m_rsumlevels - 1; log >= 0; log--) {
101 for (bbno = oargs->mp->m_sb.sb_rbmblocks - 1;
102 (xfs_srtblock_t)bbno >= 0;
104 error = xfs_rtget_summary(oargs, log, bbno, &sum);
109 error = xfs_rtmodify_summary(oargs, log, bbno, -sum);
112 error = xfs_rtmodify_summary(nargs, log, bbno, sum);
120 xfs_rtbuf_cache_relse(oargs);
124 * Mark an extent specified by start and len allocated.
125 * Updates all the summary information as well as the bitmap.
128 xfs_rtallocate_range(
129 struct xfs_rtalloc_args *args,
130 xfs_rtxnum_t start, /* start rtext to allocate */
131 xfs_rtxlen_t len) /* in/out: summary block number */
133 struct xfs_mount *mp = args->mp;
134 xfs_rtxnum_t end; /* end of the allocated rtext */
136 xfs_rtxnum_t postblock = 0; /* first rtext allocated > end */
137 xfs_rtxnum_t preblock = 0; /* first rtext allocated < start */
139 end = start + len - 1;
141 * Assume we're allocating out of the middle of a free extent.
142 * We need to find the beginning and end of the extent so we can
143 * properly update the summary.
145 error = xfs_rtfind_back(args, start, 0, &preblock);
150 * Find the next allocated block (end of free extent).
152 error = xfs_rtfind_forw(args, end, mp->m_sb.sb_rextents - 1,
158 * Decrement the summary information corresponding to the entire
161 error = xfs_rtmodify_summary(args,
162 xfs_highbit64(postblock + 1 - preblock),
163 xfs_rtx_to_rbmblock(mp, preblock), -1);
168 * If there are blocks not being allocated at the front of the
169 * old extent, add summary data for them to be free.
171 if (preblock < start) {
172 error = xfs_rtmodify_summary(args,
173 xfs_highbit64(start - preblock),
174 xfs_rtx_to_rbmblock(mp, preblock), 1);
180 * If there are blocks not being allocated at the end of the
181 * old extent, add summary data for them to be free.
183 if (postblock > end) {
184 error = xfs_rtmodify_summary(args,
185 xfs_highbit64(postblock - end),
186 xfs_rtx_to_rbmblock(mp, end + 1), 1);
192 * Modify the bitmap to mark this extent allocated.
194 return xfs_rtmodify_range(args, start, len, 0);
198 * Make sure we don't run off the end of the rt volume. Be careful that
199 * adjusting maxlen downwards doesn't cause us to fail the alignment checks.
201 static inline xfs_rtxlen_t
202 xfs_rtallocate_clamp_len(
203 struct xfs_mount *mp,
204 xfs_rtxnum_t startrtx,
210 ret = min(mp->m_sb.sb_rextents, startrtx + rtxlen) - startrtx;
211 return rounddown(ret, prod);
215 * Attempt to allocate an extent minlen<=len<=maxlen starting from
216 * bitmap block bbno. If we don't get maxlen then use prod to trim
217 * the length, if given. Returns error; returns starting block in *rtx.
218 * The lengths are all in rtextents.
221 xfs_rtallocate_extent_block(
222 struct xfs_rtalloc_args *args,
223 xfs_fileoff_t bbno, /* bitmap block number */
224 xfs_rtxlen_t minlen, /* minimum length to allocate */
225 xfs_rtxlen_t maxlen, /* maximum length to allocate */
226 xfs_rtxlen_t *len, /* out: actual length allocated */
227 xfs_rtxnum_t *nextp, /* out: next rtext to try */
228 xfs_rtxlen_t prod, /* extent product factor */
229 xfs_rtxnum_t *rtx) /* out: start rtext allocated */
231 struct xfs_mount *mp = args->mp;
232 xfs_rtxnum_t besti; /* best rtext found so far */
233 xfs_rtxnum_t bestlen;/* best length found so far */
234 xfs_rtxnum_t end; /* last rtext in chunk */
236 xfs_rtxnum_t i; /* current rtext trying */
237 xfs_rtxnum_t next; /* next rtext to try */
238 int stat; /* status from internal calls */
241 * Loop over all the extents starting in this bitmap block,
242 * looking for one that's long enough.
244 for (i = xfs_rbmblock_to_rtx(mp, bbno), besti = -1, bestlen = 0,
245 end = xfs_rbmblock_to_rtx(mp, bbno + 1) - 1;
248 /* Make sure we don't scan off the end of the rt volume. */
249 maxlen = xfs_rtallocate_clamp_len(mp, i, maxlen, prod);
252 * See if there's a free extent of maxlen starting at i.
253 * If it's not so then next will contain the first non-free.
255 error = xfs_rtcheck_range(args, i, maxlen, 1, &next, &stat);
260 * i for maxlen is all free, allocate and return that.
268 * In the case where we have a variable-sized allocation
269 * request, figure out how big this free piece is,
270 * and if it's big enough for the minimum, and the best
271 * so far, remember it.
273 if (minlen < maxlen) {
274 xfs_rtxnum_t thislen; /* this extent size */
277 if (thislen >= minlen && thislen > bestlen) {
283 * If not done yet, find the start of the next free space.
287 error = xfs_rtfind_forw(args, next, end, &i);
293 * Searched the whole thing & didn't find a maxlen free extent.
295 if (minlen > maxlen || besti == -1) {
297 * Allocation failed. Set *nextp to the next block to try.
304 * If size should be a multiple of prod, make that so.
307 xfs_rtxlen_t p; /* amount to trim length by */
309 div_u64_rem(bestlen, prod, &p);
315 * Allocate besti for bestlen & return that.
318 error = xfs_rtallocate_range(args, besti, bestlen);
327 * Allocate an extent of length minlen<=len<=maxlen, starting at block
328 * bno. If we don't get maxlen then use prod to trim the length, if given.
329 * Returns error; returns starting block in *rtx.
330 * The lengths are all in rtextents.
333 xfs_rtallocate_extent_exact(
334 struct xfs_rtalloc_args *args,
335 xfs_rtxnum_t start, /* starting rtext number to allocate */
336 xfs_rtxlen_t minlen, /* minimum length to allocate */
337 xfs_rtxlen_t maxlen, /* maximum length to allocate */
338 xfs_rtxlen_t *len, /* out: actual length allocated */
339 xfs_rtxlen_t prod, /* extent product factor */
340 xfs_rtxnum_t *rtx) /* out: start rtext allocated */
343 xfs_rtxlen_t i; /* extent length trimmed due to prod */
344 int isfree; /* extent is free */
345 xfs_rtxnum_t next; /* next rtext to try (dummy) */
347 ASSERT(minlen % prod == 0);
348 ASSERT(maxlen % prod == 0);
350 * Check if the range in question (for maxlen) is free.
352 error = xfs_rtcheck_range(args, start, maxlen, 1, &next, &isfree);
358 * If not, allocate what there is, if it's at least minlen.
360 maxlen = next - start;
365 * Trim off tail of extent, if prod is specified.
367 if (prod > 1 && (i = maxlen % prod)) {
375 * Allocate what we can and return it.
377 error = xfs_rtallocate_range(args, start, maxlen);
386 * Allocate an extent of length minlen<=len<=maxlen, starting as near
387 * to start as possible. If we don't get maxlen then use prod to trim
388 * the length, if given. The lengths are all in rtextents.
391 xfs_rtallocate_extent_near(
392 struct xfs_rtalloc_args *args,
393 xfs_rtxnum_t start, /* starting rtext number to allocate */
394 xfs_rtxlen_t minlen, /* minimum length to allocate */
395 xfs_rtxlen_t maxlen, /* maximum length to allocate */
396 xfs_rtxlen_t *len, /* out: actual length allocated */
397 xfs_rtxlen_t prod, /* extent product factor */
398 xfs_rtxnum_t *rtx) /* out: start rtext allocated */
400 struct xfs_mount *mp = args->mp;
401 int maxlog; /* max useful extent from summary */
402 xfs_fileoff_t bbno; /* bitmap block number */
404 int i; /* bitmap block offset (loop control) */
405 int j; /* secondary loop control */
406 int log2len; /* log2 of minlen */
407 xfs_rtxnum_t n; /* next rtext to try */
409 ASSERT(minlen % prod == 0);
410 ASSERT(maxlen % prod == 0);
413 * If the block number given is off the end, silently set it to
416 if (start >= mp->m_sb.sb_rextents)
417 start = mp->m_sb.sb_rextents - 1;
419 /* Make sure we don't run off the end of the rt volume. */
420 maxlen = xfs_rtallocate_clamp_len(mp, start, maxlen, prod);
425 * Try the exact allocation first.
427 error = xfs_rtallocate_extent_exact(args, start, minlen, maxlen, len,
429 if (error != -ENOSPC)
433 bbno = xfs_rtx_to_rbmblock(mp, start);
437 log2len = xfs_highbit32(minlen);
439 * Loop over all bitmap blocks (bbno + i is current block).
443 * Get summary information of extents of all useful levels
444 * starting in this bitmap block.
446 error = xfs_rtany_summary(args, log2len, mp->m_rsumlevels - 1,
452 * If there are any useful extents starting here, try
456 xfs_extlen_t maxavail =
457 min_t(xfs_rtblock_t, maxlen,
458 (1ULL << (maxlog + 1)) - 1);
460 * On the positive side of the starting location.
464 * Try to allocate an extent starting in
467 error = xfs_rtallocate_extent_block(args,
468 bbno + i, minlen, maxavail, len,
470 if (error != -ENOSPC)
474 * On the negative side of the starting location.
480 * Loop backwards to find the end of the extent
481 * we found in the realtime summary.
483 * maxblocks is the maximum possible number of
484 * bitmap blocks from the start of the extent
485 * to the end of the extent.
489 else if (maxlog < mp->m_blkbit_log)
492 maxblocks = 2 << (maxlog - mp->m_blkbit_log);
495 * We need to check bbno + i + maxblocks down to
496 * bbno + i. We already checked bbno down to
497 * bbno + j + 1, so we don't need to check those
500 j = min(i + maxblocks, j);
501 for (; j >= i; j--) {
502 error = xfs_rtallocate_extent_block(args,
504 maxavail, len, &n, prod,
506 if (error != -ENOSPC)
512 * Loop control. If we were on the positive side, and there's
513 * still more blocks on the negative side, go there.
515 if (i > 0 && (int)bbno - i >= 0)
518 * If positive, and no more negative, but there are more
519 * positive, go there.
521 else if (i > 0 && (int)bbno + i < mp->m_sb.sb_rbmblocks - 1)
524 * If negative or 0 (just started), and there are positive
525 * blocks to go, go there. The 0 case moves to block 1.
527 else if (i <= 0 && (int)bbno - i < mp->m_sb.sb_rbmblocks - 1)
530 * If negative or 0 and there are more negative blocks,
533 else if (i <= 0 && (int)bbno + i > 0)
536 * Must be done. Return failure.
545 xfs_rtalloc_sumlevel(
546 struct xfs_rtalloc_args *args,
547 int l, /* level number */
548 xfs_rtxlen_t minlen, /* minimum length to allocate */
549 xfs_rtxlen_t maxlen, /* maximum length to allocate */
550 xfs_rtxlen_t prod, /* extent product factor */
551 xfs_rtxlen_t *len, /* out: actual length allocated */
552 xfs_rtxnum_t *rtx) /* out: start rtext allocated */
554 xfs_fileoff_t i; /* bitmap block number */
556 for (i = 0; i < args->mp->m_sb.sb_rbmblocks; i++) {
557 xfs_suminfo_t sum; /* summary information for extents */
558 xfs_rtxnum_t n; /* next rtext to be tried */
561 error = xfs_rtget_summary(args, l, i, &sum);
566 * Nothing there, on to the next block.
572 * Try allocating the extent.
574 error = xfs_rtallocate_extent_block(args, i, minlen, maxlen,
576 if (error != -ENOSPC)
580 * If the "next block to try" returned from the allocator is
581 * beyond the next bitmap block, skip to that bitmap block.
583 if (xfs_rtx_to_rbmblock(args->mp, n) > i + 1)
584 i = xfs_rtx_to_rbmblock(args->mp, n) - 1;
591 * Allocate an extent of length minlen<=len<=maxlen, with no position
592 * specified. If we don't get maxlen then use prod to trim
593 * the length, if given. The lengths are all in rtextents.
596 xfs_rtallocate_extent_size(
597 struct xfs_rtalloc_args *args,
598 xfs_rtxlen_t minlen, /* minimum length to allocate */
599 xfs_rtxlen_t maxlen, /* maximum length to allocate */
600 xfs_rtxlen_t *len, /* out: actual length allocated */
601 xfs_rtxlen_t prod, /* extent product factor */
602 xfs_rtxnum_t *rtx) /* out: start rtext allocated */
605 int l; /* level number (loop control) */
607 ASSERT(minlen % prod == 0);
608 ASSERT(maxlen % prod == 0);
612 * Loop over all the levels starting with maxlen.
614 * At each level, look at all the bitmap blocks, to see if there are
615 * extents starting there that are long enough (>= maxlen).
617 * Note, only on the initial level can the allocation fail if the
618 * summary says there's an extent.
620 for (l = xfs_highbit32(maxlen); l < args->mp->m_rsumlevels; l++) {
621 error = xfs_rtalloc_sumlevel(args, l, minlen, maxlen, prod, len,
623 if (error != -ENOSPC)
628 * Didn't find any maxlen blocks. Try smaller ones, unless we are
629 * looking for a fixed size extent.
631 if (minlen > --maxlen)
637 * Loop over sizes, from maxlen down to minlen.
639 * This time, when we do the allocations, allow smaller ones to succeed,
640 * but make sure the specified minlen/maxlen are in the possible range
641 * for this summary level.
643 for (l = xfs_highbit32(maxlen); l >= xfs_highbit32(minlen); l--) {
644 error = xfs_rtalloc_sumlevel(args, l,
645 max_t(xfs_rtxlen_t, minlen, 1 << l),
646 min_t(xfs_rtxlen_t, maxlen, (1 << (l + 1)) - 1),
648 if (error != -ENOSPC)
656 * Allocate space to the bitmap or summary file, and zero it, for growfs.
660 struct xfs_mount *mp, /* file system mount point */
661 xfs_extlen_t oblocks, /* old count of blocks */
662 xfs_extlen_t nblocks, /* new count of blocks */
663 struct xfs_inode *ip) /* inode (bitmap/summary) */
665 xfs_fileoff_t bno; /* block number in file */
666 struct xfs_buf *bp; /* temporary buffer for zeroing */
667 xfs_daddr_t d; /* disk block address */
668 int error; /* error return value */
669 xfs_fsblock_t fsbno; /* filesystem block for bno */
670 struct xfs_bmbt_irec map; /* block map output */
671 int nmap; /* number of block maps */
672 int resblks; /* space reservation */
673 enum xfs_blft buf_type;
674 struct xfs_trans *tp;
676 if (ip == mp->m_rsumip)
677 buf_type = XFS_BLFT_RTSUMMARY_BUF;
679 buf_type = XFS_BLFT_RTBITMAP_BUF;
682 * Allocate space to the file, as necessary.
684 while (oblocks < nblocks) {
685 resblks = XFS_GROWFSRT_SPACE_RES(mp, nblocks - oblocks);
687 * Reserve space & log for one extent added to the file.
689 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_growrtalloc, resblks,
696 xfs_ilock(ip, XFS_ILOCK_EXCL);
697 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
699 error = xfs_iext_count_extend(tp, ip, XFS_DATA_FORK,
700 XFS_IEXT_ADD_NOSPLIT_CNT);
702 goto out_trans_cancel;
705 * Allocate blocks to the bitmap file.
708 error = xfs_bmapi_write(tp, ip, oblocks, nblocks - oblocks,
709 XFS_BMAPI_METADATA, 0, &map, &nmap);
711 goto out_trans_cancel;
713 * Free any blocks freed up in the transaction, then commit.
715 error = xfs_trans_commit(tp);
719 * Now we need to clear the allocated blocks.
720 * Do this one block per transaction, to keep it simple.
722 for (bno = map.br_startoff, fsbno = map.br_startblock;
723 bno < map.br_startoff + map.br_blockcount;
726 * Reserve log for one block zeroing.
728 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_growrtzero,
733 * Lock the bitmap inode.
735 xfs_ilock(ip, XFS_ILOCK_EXCL);
736 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
738 * Get a buffer for the block.
740 d = XFS_FSB_TO_DADDR(mp, fsbno);
741 error = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
742 mp->m_bsize, 0, &bp);
744 goto out_trans_cancel;
746 xfs_trans_buf_set_type(tp, bp, buf_type);
747 bp->b_ops = &xfs_rtbuf_ops;
748 memset(bp->b_addr, 0, mp->m_sb.sb_blocksize);
749 xfs_trans_log_buf(tp, bp, 0, mp->m_sb.sb_blocksize - 1);
751 * Commit the transaction.
753 error = xfs_trans_commit(tp);
758 * Go on to the next extent, if any.
760 oblocks = map.br_startoff + map.br_blockcount;
766 xfs_trans_cancel(tp);
771 xfs_alloc_rsum_cache(
772 xfs_mount_t *mp, /* file system mount structure */
773 xfs_extlen_t rbmblocks) /* number of rt bitmap blocks */
776 * The rsum cache is initialized to the maximum value, which is
777 * trivially an upper bound on the maximum level with any free extents.
778 * We can continue without the cache if it couldn't be allocated.
780 mp->m_rsum_cache = kvmalloc(rbmblocks, GFP_KERNEL);
781 if (mp->m_rsum_cache)
782 memset(mp->m_rsum_cache, -1, rbmblocks);
784 xfs_warn(mp, "could not allocate realtime summary cache");
788 * Visible (exported) functions.
792 * Grow the realtime area of the filesystem.
796 xfs_mount_t *mp, /* mount point for filesystem */
797 xfs_growfs_rt_t *in) /* growfs rt input struct */
799 xfs_fileoff_t bmbno; /* bitmap block number */
800 struct xfs_buf *bp; /* temporary buffer */
801 int error; /* error return value */
802 xfs_mount_t *nmp; /* new (fake) mount structure */
803 xfs_rfsblock_t nrblocks; /* new number of realtime blocks */
804 xfs_extlen_t nrbmblocks; /* new number of rt bitmap blocks */
805 xfs_rtxnum_t nrextents; /* new number of realtime extents */
806 uint8_t nrextslog; /* new log2 of sb_rextents */
807 xfs_extlen_t nrsumblocks; /* new number of summary blocks */
808 uint nrsumlevels; /* new rt summary levels */
809 uint nrsumsize; /* new size of rt summary, bytes */
810 xfs_sb_t *nsbp; /* new superblock */
811 xfs_extlen_t rbmblocks; /* current number of rt bitmap blocks */
812 xfs_extlen_t rsumblocks; /* current number of rt summary blks */
813 xfs_sb_t *sbp; /* old superblock */
814 uint8_t *rsum_cache; /* old summary cache */
818 if (!capable(CAP_SYS_ADMIN))
821 /* Needs to have been mounted with an rt device. */
822 if (!XFS_IS_REALTIME_MOUNT(mp))
825 * Mount should fail if the rt bitmap/summary files don't load, but
826 * we'll check anyway.
828 if (!mp->m_rbmip || !mp->m_rsumip)
831 /* Shrink not supported. */
832 if (in->newblocks <= sbp->sb_rblocks)
835 /* Can only change rt extent size when adding rt volume. */
836 if (sbp->sb_rblocks > 0 && in->extsize != sbp->sb_rextsize)
839 /* Range check the extent size. */
840 if (XFS_FSB_TO_B(mp, in->extsize) > XFS_MAX_RTEXTSIZE ||
841 XFS_FSB_TO_B(mp, in->extsize) < XFS_MIN_RTEXTSIZE)
844 /* Unsupported realtime features. */
845 if (xfs_has_rmapbt(mp) || xfs_has_reflink(mp) || xfs_has_quota(mp))
848 nrblocks = in->newblocks;
849 error = xfs_sb_validate_fsb_count(sbp, nrblocks);
853 * Read in the last block of the device, make sure it exists.
855 error = xfs_buf_read_uncached(mp->m_rtdev_targp,
856 XFS_FSB_TO_BB(mp, nrblocks - 1),
857 XFS_FSB_TO_BB(mp, 1), 0, &bp, NULL);
863 * Calculate new parameters. These are the final values to be reached.
865 nrextents = nrblocks;
866 do_div(nrextents, in->extsize);
867 if (!xfs_validate_rtextents(nrextents))
869 nrbmblocks = xfs_rtbitmap_blockcount(mp, nrextents);
870 nrextslog = xfs_compute_rextslog(nrextents);
871 nrsumlevels = nrextslog + 1;
872 nrsumblocks = xfs_rtsummary_blockcount(mp, nrsumlevels, nrbmblocks);
873 nrsumsize = XFS_FSB_TO_B(mp, nrsumblocks);
875 * New summary size can't be more than half the size of
876 * the log. This prevents us from getting a log overflow,
877 * since we'll log basically the whole summary file at once.
879 if (nrsumblocks > (mp->m_sb.sb_logblocks >> 1))
882 * Get the old block counts for bitmap and summary inodes.
883 * These can't change since other growfs callers are locked out.
885 rbmblocks = XFS_B_TO_FSB(mp, mp->m_rbmip->i_disk_size);
886 rsumblocks = XFS_B_TO_FSB(mp, mp->m_rsumip->i_disk_size);
888 * Allocate space to the bitmap and summary files, as necessary.
890 error = xfs_growfs_rt_alloc(mp, rbmblocks, nrbmblocks, mp->m_rbmip);
893 error = xfs_growfs_rt_alloc(mp, rsumblocks, nrsumblocks, mp->m_rsumip);
897 rsum_cache = mp->m_rsum_cache;
898 if (nrbmblocks != sbp->sb_rbmblocks)
899 xfs_alloc_rsum_cache(mp, nrbmblocks);
902 * Allocate a new (fake) mount/sb.
904 nmp = kmalloc(sizeof(*nmp), GFP_KERNEL | __GFP_NOFAIL);
906 * Loop over the bitmap blocks.
907 * We will do everything one bitmap block at a time.
908 * Skip the current block if it is exactly full.
909 * This also deals with the case where there were no rtextents before.
911 for (bmbno = sbp->sb_rbmblocks -
912 ((sbp->sb_rextents & ((1 << mp->m_blkbit_log) - 1)) != 0);
915 struct xfs_rtalloc_args args = {
918 struct xfs_rtalloc_args nargs = {
921 struct xfs_trans *tp;
922 xfs_rfsblock_t nrblocks_step;
927 * Calculate new sb and mount fields for this round.
929 nsbp->sb_rextsize = in->extsize;
930 nmp->m_rtxblklog = -1; /* don't use shift or masking */
931 nsbp->sb_rbmblocks = bmbno + 1;
932 nrblocks_step = (bmbno + 1) * NBBY * nsbp->sb_blocksize *
934 nsbp->sb_rblocks = min(nrblocks, nrblocks_step);
935 nsbp->sb_rextents = xfs_rtb_to_rtx(nmp, nsbp->sb_rblocks);
936 ASSERT(nsbp->sb_rextents != 0);
937 nsbp->sb_rextslog = xfs_compute_rextslog(nsbp->sb_rextents);
938 nrsumlevels = nmp->m_rsumlevels = nsbp->sb_rextslog + 1;
939 nrsumblocks = xfs_rtsummary_blockcount(mp, nrsumlevels,
941 nmp->m_rsumsize = nrsumsize = XFS_FSB_TO_B(mp, nrsumblocks);
942 /* recompute growfsrt reservation from new rsumsize */
943 xfs_trans_resv_calc(nmp, &nmp->m_resv);
946 * Start a transaction, get the log reservation.
948 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_growrtfree, 0, 0, 0,
956 * Lock out other callers by grabbing the bitmap and summary
957 * inode locks and joining them to the transaction.
959 xfs_rtbitmap_lock(tp, mp);
961 * Update the bitmap inode's size ondisk and incore. We need
962 * to update the incore size so that inode inactivation won't
963 * punch what it thinks are "posteof" blocks.
965 mp->m_rbmip->i_disk_size =
966 nsbp->sb_rbmblocks * nsbp->sb_blocksize;
967 i_size_write(VFS_I(mp->m_rbmip), mp->m_rbmip->i_disk_size);
968 xfs_trans_log_inode(tp, mp->m_rbmip, XFS_ILOG_CORE);
970 * Update the summary inode's size. We need to update the
971 * incore size so that inode inactivation won't punch what it
972 * thinks are "posteof" blocks.
974 mp->m_rsumip->i_disk_size = nmp->m_rsumsize;
975 i_size_write(VFS_I(mp->m_rsumip), mp->m_rsumip->i_disk_size);
976 xfs_trans_log_inode(tp, mp->m_rsumip, XFS_ILOG_CORE);
978 * Copy summary data from old to new sizes.
979 * Do this when the real size (not block-aligned) changes.
981 if (sbp->sb_rbmblocks != nsbp->sb_rbmblocks ||
982 mp->m_rsumlevels != nmp->m_rsumlevels) {
983 error = xfs_rtcopy_summary(&args, &nargs);
988 * Update superblock fields.
990 if (nsbp->sb_rextsize != sbp->sb_rextsize)
991 xfs_trans_mod_sb(tp, XFS_TRANS_SB_REXTSIZE,
992 nsbp->sb_rextsize - sbp->sb_rextsize);
993 if (nsbp->sb_rbmblocks != sbp->sb_rbmblocks)
994 xfs_trans_mod_sb(tp, XFS_TRANS_SB_RBMBLOCKS,
995 nsbp->sb_rbmblocks - sbp->sb_rbmblocks);
996 if (nsbp->sb_rblocks != sbp->sb_rblocks)
997 xfs_trans_mod_sb(tp, XFS_TRANS_SB_RBLOCKS,
998 nsbp->sb_rblocks - sbp->sb_rblocks);
999 if (nsbp->sb_rextents != sbp->sb_rextents)
1000 xfs_trans_mod_sb(tp, XFS_TRANS_SB_REXTENTS,
1001 nsbp->sb_rextents - sbp->sb_rextents);
1002 if (nsbp->sb_rextslog != sbp->sb_rextslog)
1003 xfs_trans_mod_sb(tp, XFS_TRANS_SB_REXTSLOG,
1004 nsbp->sb_rextslog - sbp->sb_rextslog);
1008 error = xfs_rtfree_range(&nargs, sbp->sb_rextents,
1009 nsbp->sb_rextents - sbp->sb_rextents);
1010 xfs_rtbuf_cache_relse(&nargs);
1013 xfs_trans_cancel(tp);
1017 * Mark more blocks free in the superblock.
1019 xfs_trans_mod_sb(tp, XFS_TRANS_SB_FREXTENTS,
1020 nsbp->sb_rextents - sbp->sb_rextents);
1022 * Update mp values into the real mp structure.
1024 mp->m_rsumlevels = nrsumlevels;
1025 mp->m_rsumsize = nrsumsize;
1026 /* recompute growfsrt reservation from new rsumsize */
1027 xfs_trans_resv_calc(mp, &mp->m_resv);
1029 error = xfs_trans_commit(tp);
1033 /* Ensure the mount RT feature flag is now set. */
1034 mp->m_features |= XFS_FEAT_REALTIME;
1039 /* Update secondary superblocks now the physical grow has completed */
1040 error = xfs_update_secondary_sbs(mp);
1044 * Free the fake mp structure.
1049 * If we had to allocate a new rsum_cache, we either need to free the
1050 * old one (if we succeeded) or free the new one and restore the old one
1051 * (if there was an error).
1053 if (rsum_cache != mp->m_rsum_cache) {
1055 kvfree(mp->m_rsum_cache);
1056 mp->m_rsum_cache = rsum_cache;
1066 * Initialize realtime fields in the mount structure.
1070 struct xfs_mount *mp) /* file system mount structure */
1072 struct xfs_buf *bp; /* buffer for last block of subvolume */
1073 struct xfs_sb *sbp; /* filesystem superblock copy in mount */
1074 xfs_daddr_t d; /* address of last block of subvolume */
1075 unsigned int rsumblocks;
1079 if (sbp->sb_rblocks == 0)
1081 if (mp->m_rtdev_targp == NULL) {
1083 "Filesystem has a realtime volume, use rtdev=device option");
1086 mp->m_rsumlevels = sbp->sb_rextslog + 1;
1087 rsumblocks = xfs_rtsummary_blockcount(mp, mp->m_rsumlevels,
1088 mp->m_sb.sb_rbmblocks);
1089 mp->m_rsumsize = XFS_FSB_TO_B(mp, rsumblocks);
1090 mp->m_rbmip = mp->m_rsumip = NULL;
1092 * Check that the realtime section is an ok size.
1094 d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_rblocks);
1095 if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_rblocks) {
1096 xfs_warn(mp, "realtime mount -- %llu != %llu",
1097 (unsigned long long) XFS_BB_TO_FSB(mp, d),
1098 (unsigned long long) mp->m_sb.sb_rblocks);
1101 error = xfs_buf_read_uncached(mp->m_rtdev_targp,
1102 d - XFS_FSB_TO_BB(mp, 1),
1103 XFS_FSB_TO_BB(mp, 1), 0, &bp, NULL);
1105 xfs_warn(mp, "realtime device size check failed");
1113 xfs_rtalloc_count_frextent(
1114 struct xfs_mount *mp,
1115 struct xfs_trans *tp,
1116 const struct xfs_rtalloc_rec *rec,
1119 uint64_t *valp = priv;
1121 *valp += rec->ar_extcount;
1126 * Reinitialize the number of free realtime extents from the realtime bitmap.
1127 * Callers must ensure that there is no other activity in the filesystem.
1130 xfs_rtalloc_reinit_frextents(
1131 struct xfs_mount *mp)
1136 xfs_rtbitmap_lock_shared(mp, XFS_RBMLOCK_BITMAP);
1137 error = xfs_rtalloc_query_all(mp, NULL, xfs_rtalloc_count_frextent,
1139 xfs_rtbitmap_unlock_shared(mp, XFS_RBMLOCK_BITMAP);
1143 spin_lock(&mp->m_sb_lock);
1144 mp->m_sb.sb_frextents = val;
1145 spin_unlock(&mp->m_sb_lock);
1146 percpu_counter_set(&mp->m_frextents, mp->m_sb.sb_frextents);
1151 * Read in the bmbt of an rt metadata inode so that we never have to load them
1152 * at runtime. This enables the use of shared ILOCKs for rtbitmap scans. Use
1153 * an empty transaction to avoid deadlocking on loops in the bmbt.
1156 xfs_rtmount_iread_extents(
1157 struct xfs_inode *ip,
1158 unsigned int lock_class)
1160 struct xfs_trans *tp;
1163 error = xfs_trans_alloc_empty(ip->i_mount, &tp);
1167 xfs_ilock(ip, XFS_ILOCK_EXCL | lock_class);
1169 error = xfs_iread_extents(tp, ip, XFS_DATA_FORK);
1173 if (xfs_inode_has_attr_fork(ip)) {
1174 error = xfs_iread_extents(tp, ip, XFS_ATTR_FORK);
1180 xfs_iunlock(ip, XFS_ILOCK_EXCL | lock_class);
1181 xfs_trans_cancel(tp);
1186 * Get the bitmap and summary inodes and the summary cache into the mount
1187 * structure at mount time.
1191 xfs_mount_t *mp) /* file system mount structure */
1193 int error; /* error return value */
1197 error = xfs_iget(mp, NULL, sbp->sb_rbmino, 0, 0, &mp->m_rbmip);
1198 if (xfs_metadata_is_sick(error))
1199 xfs_rt_mark_sick(mp, XFS_SICK_RT_BITMAP);
1202 ASSERT(mp->m_rbmip != NULL);
1204 error = xfs_rtmount_iread_extents(mp->m_rbmip, XFS_ILOCK_RTBITMAP);
1206 goto out_rele_bitmap;
1208 error = xfs_iget(mp, NULL, sbp->sb_rsumino, 0, 0, &mp->m_rsumip);
1209 if (xfs_metadata_is_sick(error))
1210 xfs_rt_mark_sick(mp, XFS_SICK_RT_SUMMARY);
1212 goto out_rele_bitmap;
1213 ASSERT(mp->m_rsumip != NULL);
1215 error = xfs_rtmount_iread_extents(mp->m_rsumip, XFS_ILOCK_RTSUM);
1217 goto out_rele_summary;
1219 xfs_alloc_rsum_cache(mp, sbp->sb_rbmblocks);
1223 xfs_irele(mp->m_rsumip);
1225 xfs_irele(mp->m_rbmip);
1230 xfs_rtunmount_inodes(
1231 struct xfs_mount *mp)
1233 kvfree(mp->m_rsum_cache);
1235 xfs_irele(mp->m_rbmip);
1237 xfs_irele(mp->m_rsumip);
1241 * Pick an extent for allocation at the start of a new realtime file.
1242 * Use the sequence number stored in the atime field of the bitmap inode.
1243 * Translate this to a fraction of the rtextents, and return the product
1244 * of rtextents and the fraction.
1245 * The fraction sequence is 0, 1/2, 1/4, 3/4, 1/8, ..., 7/8, 1/16, ...
1249 xfs_mount_t *mp, /* file system mount point */
1250 xfs_trans_t *tp, /* transaction pointer */
1251 xfs_rtxlen_t len, /* allocation length (rtextents) */
1252 xfs_rtxnum_t *pick) /* result rt extent */
1254 xfs_rtxnum_t b; /* result rtext */
1255 int log2; /* log of sequence number */
1256 uint64_t resid; /* residual after log removed */
1257 uint64_t seq; /* sequence number of file creation */
1258 struct timespec64 ts; /* timespec in inode */
1260 xfs_assert_ilocked(mp->m_rbmip, XFS_ILOCK_EXCL);
1262 ts = inode_get_atime(VFS_I(mp->m_rbmip));
1263 if (!(mp->m_rbmip->i_diflags & XFS_DIFLAG_NEWRTBM)) {
1264 mp->m_rbmip->i_diflags |= XFS_DIFLAG_NEWRTBM;
1269 if ((log2 = xfs_highbit64(seq)) == -1)
1272 resid = seq - (1ULL << log2);
1273 b = (mp->m_sb.sb_rextents * ((resid << 1) + 1ULL)) >>
1275 if (b >= mp->m_sb.sb_rextents)
1276 div64_u64_rem(b, mp->m_sb.sb_rextents, &b);
1277 if (b + len > mp->m_sb.sb_rextents)
1278 b = mp->m_sb.sb_rextents - len;
1280 ts.tv_sec = seq + 1;
1281 inode_set_atime_to_ts(VFS_I(mp->m_rbmip), ts);
1282 xfs_trans_log_inode(tp, mp->m_rbmip, XFS_ILOG_CORE);
1288 xfs_rtalloc_align_minmax(
1289 xfs_rtxlen_t *raminlen,
1290 xfs_rtxlen_t *ramaxlen,
1293 xfs_rtxlen_t newmaxlen = *ramaxlen;
1294 xfs_rtxlen_t newminlen = *raminlen;
1297 slack = newmaxlen % *prod;
1300 slack = newminlen % *prod;
1302 newminlen += *prod - slack;
1305 * If adjusting for extent size hint alignment produces an invalid
1306 * min/max len combination, go ahead without it.
1308 if (newmaxlen < newminlen) {
1312 *ramaxlen = newmaxlen;
1313 *raminlen = newminlen;
1318 struct xfs_bmalloca *ap)
1320 struct xfs_mount *mp = ap->ip->i_mount;
1321 xfs_fileoff_t orig_offset = ap->offset;
1322 xfs_rtxnum_t start; /* allocation hint rtextent no */
1323 xfs_rtxnum_t rtx; /* actually allocated rtextent no */
1324 xfs_rtxlen_t prod = 0; /* product factor for allocators */
1325 xfs_extlen_t mod = 0; /* product factor for allocators */
1326 xfs_rtxlen_t ralen = 0; /* realtime allocation length */
1327 xfs_extlen_t align; /* minimum allocation alignment */
1328 xfs_extlen_t orig_length = ap->length;
1329 xfs_extlen_t minlen = mp->m_sb.sb_rextsize;
1330 xfs_rtxlen_t raminlen;
1331 bool rtlocked = false;
1332 bool ignore_locality = false;
1333 struct xfs_rtalloc_args args = {
1339 align = xfs_get_extsz_hint(ap->ip);
1343 error = xfs_bmap_extsize_align(mp, &ap->got, &ap->prev,
1344 align, 1, ap->eof, 0,
1345 ap->conv, &ap->offset, &ap->length);
1349 ASSERT(xfs_extlen_to_rtxmod(mp, ap->length) == 0);
1352 * If we shifted the file offset downward to satisfy an extent size
1353 * hint, increase minlen by that amount so that the allocator won't
1354 * give us an allocation that's too short to cover at least one of the
1355 * blocks that the caller asked for.
1357 if (ap->offset != orig_offset)
1358 minlen += orig_offset - ap->offset;
1361 * Set ralen to be the actual requested length in rtextents.
1363 * If the old value was close enough to XFS_BMBT_MAX_EXTLEN that
1364 * we rounded up to it, cut it back so it's valid again.
1365 * Note that if it's a really large request (bigger than
1366 * XFS_BMBT_MAX_EXTLEN), we don't hear about that number, and can't
1367 * adjust the starting point to match it.
1369 ralen = xfs_extlen_to_rtxlen(mp, min(ap->length, XFS_MAX_BMBT_EXTLEN));
1370 raminlen = max_t(xfs_rtxlen_t, 1, xfs_extlen_to_rtxlen(mp, minlen));
1371 ASSERT(raminlen > 0);
1372 ASSERT(raminlen <= ralen);
1375 * Lock out modifications to both the RT bitmap and summary inodes
1378 xfs_rtbitmap_lock(ap->tp, mp);
1382 if (ignore_locality) {
1384 } else if (xfs_bmap_adjacent(ap)) {
1385 start = xfs_rtb_to_rtx(mp, ap->blkno);
1386 } else if (ap->datatype & XFS_ALLOC_INITIAL_USER_DATA) {
1388 * If it's an allocation to an empty file at offset 0, pick an
1389 * extent that will space things out in the rt area.
1391 error = xfs_rtpick_extent(mp, ap->tp, ralen, &start);
1399 * Only bother calculating a real prod factor if offset & length are
1400 * perfectly aligned, otherwise it will just get us in trouble.
1402 div_u64_rem(ap->offset, align, &mod);
1403 if (mod || ap->length % align) {
1406 prod = xfs_extlen_to_rtxlen(mp, align);
1408 xfs_rtalloc_align_minmax(&raminlen, &ralen, &prod);
1412 error = xfs_rtallocate_extent_near(&args, start, raminlen,
1413 ralen, &ralen, prod, &rtx);
1415 error = xfs_rtallocate_extent_size(&args, raminlen,
1416 ralen, &ralen, prod, &rtx);
1418 xfs_rtbuf_cache_relse(&args);
1420 if (error == -ENOSPC) {
1421 if (align > mp->m_sb.sb_rextsize) {
1423 * We previously enlarged the request length to try to
1424 * satisfy an extent size hint. The allocator didn't
1425 * return anything, so reset the parameters to the
1426 * original values and try again without alignment
1429 ap->offset = orig_offset;
1430 ap->length = orig_length;
1431 minlen = align = mp->m_sb.sb_rextsize;
1435 if (!ignore_locality && start != 0) {
1437 * If we can't allocate near a specific rt extent, try
1438 * again without locality criteria.
1440 ignore_locality = true;
1444 ap->blkno = NULLFSBLOCK;
1451 xfs_trans_mod_sb(ap->tp, ap->wasdel ?
1452 XFS_TRANS_SB_RES_FREXTENTS : XFS_TRANS_SB_FREXTENTS,
1454 ap->blkno = xfs_rtx_to_rtb(mp, rtx);
1455 ap->length = xfs_rtxlen_to_extlen(mp, ralen);
1456 xfs_bmap_alloc_account(ap);