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_defer.h"
15 #include "xfs_inode.h"
17 #include "xfs_bmap_util.h"
18 #include "xfs_bmap_btree.h"
19 #include "xfs_alloc.h"
20 #include "xfs_error.h"
21 #include "xfs_trans.h"
22 #include "xfs_trans_space.h"
23 #include "xfs_trace.h"
25 #include "xfs_icache.h"
26 #include "xfs_rtalloc.h"
30 * Read and return the summary information for a given extent size,
31 * bitmap block combination.
32 * Keeps track of a current summary block, so we don't keep reading
33 * it from the buffer cache.
37 xfs_mount_t *mp, /* file system mount structure */
38 xfs_trans_t *tp, /* transaction pointer */
39 int log, /* log2 of extent size */
40 xfs_rtblock_t bbno, /* bitmap block number */
41 xfs_buf_t **rbpp, /* in/out: summary block buffer */
42 xfs_fsblock_t *rsb, /* in/out: summary block number */
43 xfs_suminfo_t *sum) /* out: summary info for this block */
45 return xfs_rtmodify_summary_int(mp, tp, log, bbno, 0, rbpp, rsb, sum);
49 * Return whether there are any free extents in the size range given
50 * by low and high, for the bitmap block bbno.
52 STATIC int /* error */
54 xfs_mount_t *mp, /* file system mount structure */
55 xfs_trans_t *tp, /* transaction pointer */
56 int low, /* low log2 extent size */
57 int high, /* high log2 extent size */
58 xfs_rtblock_t bbno, /* bitmap block number */
59 xfs_buf_t **rbpp, /* in/out: summary block buffer */
60 xfs_fsblock_t *rsb, /* in/out: summary block number */
61 int *stat) /* out: any good extents here? */
63 int error; /* error value */
64 int log; /* loop counter, log2 of ext. size */
65 xfs_suminfo_t sum; /* summary data */
68 * Loop over logs of extent sizes. Order is irrelevant.
70 for (log = low; log <= high; log++) {
72 * Get one summary datum.
74 error = xfs_rtget_summary(mp, tp, log, bbno, rbpp, rsb, &sum);
79 * If there are any, return success.
87 * Found nothing, return failure.
95 * Copy and transform the summary file, given the old and new
96 * parameters in the mount structures.
98 STATIC int /* error */
100 xfs_mount_t *omp, /* old file system mount point */
101 xfs_mount_t *nmp, /* new file system mount point */
102 xfs_trans_t *tp) /* transaction pointer */
104 xfs_rtblock_t bbno; /* bitmap block number */
105 xfs_buf_t *bp; /* summary buffer */
106 int error; /* error return value */
107 int log; /* summary level number (log length) */
108 xfs_suminfo_t sum; /* summary data */
109 xfs_fsblock_t sumbno; /* summary block number */
112 for (log = omp->m_rsumlevels - 1; log >= 0; log--) {
113 for (bbno = omp->m_sb.sb_rbmblocks - 1;
114 (xfs_srtblock_t)bbno >= 0;
116 error = xfs_rtget_summary(omp, tp, log, bbno, &bp,
122 error = xfs_rtmodify_summary(omp, tp, log, bbno, -sum,
126 error = xfs_rtmodify_summary(nmp, tp, log, bbno, sum,
136 * Mark an extent specified by start and len allocated.
137 * Updates all the summary information as well as the bitmap.
139 STATIC int /* error */
140 xfs_rtallocate_range(
141 xfs_mount_t *mp, /* file system mount point */
142 xfs_trans_t *tp, /* transaction pointer */
143 xfs_rtblock_t start, /* start block to allocate */
144 xfs_extlen_t len, /* length to allocate */
145 xfs_buf_t **rbpp, /* in/out: summary block buffer */
146 xfs_fsblock_t *rsb) /* in/out: summary block number */
148 xfs_rtblock_t end; /* end of the allocated extent */
149 int error; /* error value */
150 xfs_rtblock_t postblock = 0; /* first block allocated > end */
151 xfs_rtblock_t preblock = 0; /* first block allocated < start */
153 end = start + len - 1;
155 * Assume we're allocating out of the middle of a free extent.
156 * We need to find the beginning and end of the extent so we can
157 * properly update the summary.
159 error = xfs_rtfind_back(mp, tp, start, 0, &preblock);
164 * Find the next allocated block (end of free extent).
166 error = xfs_rtfind_forw(mp, tp, end, mp->m_sb.sb_rextents - 1,
172 * Decrement the summary information corresponding to the entire
175 error = xfs_rtmodify_summary(mp, tp,
176 XFS_RTBLOCKLOG(postblock + 1 - preblock),
177 XFS_BITTOBLOCK(mp, preblock), -1, rbpp, rsb);
182 * If there are blocks not being allocated at the front of the
183 * old extent, add summary data for them to be free.
185 if (preblock < start) {
186 error = xfs_rtmodify_summary(mp, tp,
187 XFS_RTBLOCKLOG(start - preblock),
188 XFS_BITTOBLOCK(mp, preblock), 1, rbpp, rsb);
194 * If there are blocks not being allocated at the end of the
195 * old extent, add summary data for them to be free.
197 if (postblock > end) {
198 error = xfs_rtmodify_summary(mp, tp,
199 XFS_RTBLOCKLOG(postblock - end),
200 XFS_BITTOBLOCK(mp, end + 1), 1, rbpp, rsb);
206 * Modify the bitmap to mark this extent allocated.
208 error = xfs_rtmodify_range(mp, tp, start, len, 0);
213 * Attempt to allocate an extent minlen<=len<=maxlen starting from
214 * bitmap block bbno. If we don't get maxlen then use prod to trim
215 * the length, if given. Returns error; returns starting block in *rtblock.
216 * The lengths are all in rtextents.
218 STATIC int /* error */
219 xfs_rtallocate_extent_block(
220 xfs_mount_t *mp, /* file system mount point */
221 xfs_trans_t *tp, /* transaction pointer */
222 xfs_rtblock_t bbno, /* bitmap block number */
223 xfs_extlen_t minlen, /* minimum length to allocate */
224 xfs_extlen_t maxlen, /* maximum length to allocate */
225 xfs_extlen_t *len, /* out: actual length allocated */
226 xfs_rtblock_t *nextp, /* out: next block to try */
227 xfs_buf_t **rbpp, /* in/out: summary block buffer */
228 xfs_fsblock_t *rsb, /* in/out: summary block number */
229 xfs_extlen_t prod, /* extent product factor */
230 xfs_rtblock_t *rtblock) /* out: start block allocated */
232 xfs_rtblock_t besti; /* best rtblock found so far */
233 xfs_rtblock_t bestlen; /* best length found so far */
234 xfs_rtblock_t end; /* last rtblock in chunk */
235 int error; /* error value */
236 xfs_rtblock_t i; /* current rtblock trying */
237 xfs_rtblock_t next; /* next rtblock 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_BLOCKTOBIT(mp, bbno), besti = -1, bestlen = 0,
245 end = XFS_BLOCKTOBIT(mp, bbno + 1) - 1;
249 * See if there's a free extent of maxlen starting at i.
250 * If it's not so then next will contain the first non-free.
252 error = xfs_rtcheck_range(mp, tp, i, maxlen, 1, &next, &stat);
258 * i for maxlen is all free, allocate and return that.
260 error = xfs_rtallocate_range(mp, tp, i, maxlen, rbpp,
270 * In the case where we have a variable-sized allocation
271 * request, figure out how big this free piece is,
272 * and if it's big enough for the minimum, and the best
273 * so far, remember it.
275 if (minlen < maxlen) {
276 xfs_rtblock_t thislen; /* this extent size */
279 if (thislen >= minlen && thislen > bestlen) {
285 * If not done yet, find the start of the next free space.
288 error = xfs_rtfind_forw(mp, tp, next, end, &i);
296 * Searched the whole thing & didn't find a maxlen free extent.
298 if (minlen < maxlen && besti != -1) {
299 xfs_extlen_t p; /* amount to trim length by */
302 * If size should be a multiple of prod, make that so.
305 div_u64_rem(bestlen, prod, &p);
311 * Allocate besti for bestlen & return that.
313 error = xfs_rtallocate_range(mp, tp, besti, bestlen, rbpp, rsb);
322 * Allocation failed. Set *nextp to the next block to try.
325 *rtblock = NULLRTBLOCK;
330 * Allocate an extent of length minlen<=len<=maxlen, starting at block
331 * bno. If we don't get maxlen then use prod to trim the length, if given.
332 * Returns error; returns starting block in *rtblock.
333 * The lengths are all in rtextents.
335 STATIC int /* error */
336 xfs_rtallocate_extent_exact(
337 xfs_mount_t *mp, /* file system mount point */
338 xfs_trans_t *tp, /* transaction pointer */
339 xfs_rtblock_t bno, /* starting block number to allocate */
340 xfs_extlen_t minlen, /* minimum length to allocate */
341 xfs_extlen_t maxlen, /* maximum length to allocate */
342 xfs_extlen_t *len, /* out: actual length allocated */
343 xfs_buf_t **rbpp, /* in/out: summary block buffer */
344 xfs_fsblock_t *rsb, /* in/out: summary block number */
345 xfs_extlen_t prod, /* extent product factor */
346 xfs_rtblock_t *rtblock) /* out: start block allocated */
348 int error; /* error value */
349 xfs_extlen_t i; /* extent length trimmed due to prod */
350 int isfree; /* extent is free */
351 xfs_rtblock_t next; /* next block to try (dummy) */
353 ASSERT(minlen % prod == 0 && maxlen % prod == 0);
355 * Check if the range in question (for maxlen) is free.
357 error = xfs_rtcheck_range(mp, tp, bno, maxlen, 1, &next, &isfree);
363 * If it is, allocate it and return success.
365 error = xfs_rtallocate_range(mp, tp, bno, maxlen, rbpp, rsb);
374 * If not, allocate what there is, if it's at least minlen.
377 if (maxlen < minlen) {
379 * Failed, return failure status.
381 *rtblock = NULLRTBLOCK;
385 * Trim off tail of extent, if prod is specified.
387 if (prod > 1 && (i = maxlen % prod)) {
389 if (maxlen < minlen) {
391 * Now we can't do it, return failure status.
393 *rtblock = NULLRTBLOCK;
398 * Allocate what we can and return it.
400 error = xfs_rtallocate_range(mp, tp, bno, maxlen, rbpp, rsb);
410 * Allocate an extent of length minlen<=len<=maxlen, starting as near
411 * to bno as possible. If we don't get maxlen then use prod to trim
412 * the length, if given. The lengths are all in rtextents.
414 STATIC int /* error */
415 xfs_rtallocate_extent_near(
416 xfs_mount_t *mp, /* file system mount point */
417 xfs_trans_t *tp, /* transaction pointer */
418 xfs_rtblock_t bno, /* starting block number to allocate */
419 xfs_extlen_t minlen, /* minimum length to allocate */
420 xfs_extlen_t maxlen, /* maximum length to allocate */
421 xfs_extlen_t *len, /* out: actual length allocated */
422 xfs_buf_t **rbpp, /* in/out: summary block buffer */
423 xfs_fsblock_t *rsb, /* in/out: summary block number */
424 xfs_extlen_t prod, /* extent product factor */
425 xfs_rtblock_t *rtblock) /* out: start block allocated */
427 int any; /* any useful extents from summary */
428 xfs_rtblock_t bbno; /* bitmap block number */
429 int error; /* error value */
430 int i; /* bitmap block offset (loop control) */
431 int j; /* secondary loop control */
432 int log2len; /* log2 of minlen */
433 xfs_rtblock_t n; /* next block to try */
434 xfs_rtblock_t r; /* result block */
436 ASSERT(minlen % prod == 0 && maxlen % prod == 0);
438 * If the block number given is off the end, silently set it to
441 if (bno >= mp->m_sb.sb_rextents)
442 bno = mp->m_sb.sb_rextents - 1;
444 * Try the exact allocation first.
446 error = xfs_rtallocate_extent_exact(mp, tp, bno, minlen, maxlen, len,
447 rbpp, rsb, prod, &r);
452 * If the exact allocation worked, return that.
454 if (r != NULLRTBLOCK) {
458 bbno = XFS_BITTOBLOCK(mp, bno);
461 log2len = xfs_highbit32(minlen);
463 * Loop over all bitmap blocks (bbno + i is current block).
467 * Get summary information of extents of all useful levels
468 * starting in this bitmap block.
470 error = xfs_rtany_summary(mp, tp, log2len, mp->m_rsumlevels - 1,
471 bbno + i, rbpp, rsb, &any);
476 * If there are any useful extents starting here, try
481 * On the positive side of the starting location.
485 * Try to allocate an extent starting in
488 error = xfs_rtallocate_extent_block(mp, tp,
489 bbno + i, minlen, maxlen, len, &n, rbpp,
495 * If it worked, return it.
497 if (r != NULLRTBLOCK) {
503 * On the negative side of the starting location.
507 * Loop backwards through the bitmap blocks from
508 * the starting point-1 up to where we are now.
509 * There should be an extent which ends in this
510 * bitmap block and is long enough.
512 for (j = -1; j > i; j--) {
514 * Grab the summary information for
517 error = xfs_rtany_summary(mp, tp,
518 log2len, mp->m_rsumlevels - 1,
519 bbno + j, rbpp, rsb, &any);
524 * If there's no extent given in the
525 * summary that means the extent we
526 * found must carry over from an
527 * earlier block. If there is an
528 * extent given, we've already tried
529 * that allocation, don't do it again.
533 error = xfs_rtallocate_extent_block(mp,
534 tp, bbno + j, minlen, maxlen,
535 len, &n, rbpp, rsb, prod, &r);
540 * If it works, return the extent.
542 if (r != NULLRTBLOCK) {
548 * There weren't intervening bitmap blocks
549 * with a long enough extent, or the
550 * allocation didn't work for some reason
551 * (i.e. it's a little * too short).
552 * Try to allocate from the summary block
555 error = xfs_rtallocate_extent_block(mp, tp,
556 bbno + i, minlen, maxlen, len, &n, rbpp,
562 * If it works, return the extent.
564 if (r != NULLRTBLOCK) {
571 * Loop control. If we were on the positive side, and there's
572 * still more blocks on the negative side, go there.
574 if (i > 0 && (int)bbno - i >= 0)
577 * If positive, and no more negative, but there are more
578 * positive, go there.
580 else if (i > 0 && (int)bbno + i < mp->m_sb.sb_rbmblocks - 1)
583 * If negative or 0 (just started), and there are positive
584 * blocks to go, go there. The 0 case moves to block 1.
586 else if (i <= 0 && (int)bbno - i < mp->m_sb.sb_rbmblocks - 1)
589 * If negative or 0 and there are more negative blocks,
592 else if (i <= 0 && (int)bbno + i > 0)
595 * Must be done. Return failure.
600 *rtblock = NULLRTBLOCK;
605 * Allocate an extent of length minlen<=len<=maxlen, with no position
606 * specified. If we don't get maxlen then use prod to trim
607 * the length, if given. The lengths are all in rtextents.
609 STATIC int /* error */
610 xfs_rtallocate_extent_size(
611 xfs_mount_t *mp, /* file system mount point */
612 xfs_trans_t *tp, /* transaction pointer */
613 xfs_extlen_t minlen, /* minimum length to allocate */
614 xfs_extlen_t maxlen, /* maximum length to allocate */
615 xfs_extlen_t *len, /* out: actual length allocated */
616 xfs_buf_t **rbpp, /* in/out: summary block buffer */
617 xfs_fsblock_t *rsb, /* in/out: summary block number */
618 xfs_extlen_t prod, /* extent product factor */
619 xfs_rtblock_t *rtblock) /* out: start block allocated */
621 int error; /* error value */
622 int i; /* bitmap block number */
623 int l; /* level number (loop control) */
624 xfs_rtblock_t n; /* next block to be tried */
625 xfs_rtblock_t r; /* result block number */
626 xfs_suminfo_t sum; /* summary information for extents */
628 ASSERT(minlen % prod == 0 && maxlen % prod == 0);
632 * Loop over all the levels starting with maxlen.
633 * At each level, look at all the bitmap blocks, to see if there
634 * are extents starting there that are long enough (>= maxlen).
635 * Note, only on the initial level can the allocation fail if
636 * the summary says there's an extent.
638 for (l = xfs_highbit32(maxlen); l < mp->m_rsumlevels; l++) {
640 * Loop over all the bitmap blocks.
642 for (i = 0; i < mp->m_sb.sb_rbmblocks; i++) {
644 * Get the summary for this level/block.
646 error = xfs_rtget_summary(mp, tp, l, i, rbpp, rsb,
652 * Nothing there, on to the next block.
657 * Try allocating the extent.
659 error = xfs_rtallocate_extent_block(mp, tp, i, maxlen,
660 maxlen, len, &n, rbpp, rsb, prod, &r);
665 * If it worked, return that.
667 if (r != NULLRTBLOCK) {
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_BITTOBLOCK(mp, n) > i + 1)
677 i = XFS_BITTOBLOCK(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) {
685 *rtblock = NULLRTBLOCK;
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(mp, tp, l, i, rbpp, rsb,
711 * If nothing there, go on to next.
716 * Try the allocation. Make sure the specified
717 * minlen/maxlen are in the possible range for
718 * this summary level.
720 error = xfs_rtallocate_extent_block(mp, tp, i,
721 XFS_RTMAX(minlen, 1 << l),
722 XFS_RTMIN(maxlen, (1 << (l + 1)) - 1),
723 len, &n, rbpp, rsb, prod, &r);
728 * If it worked, return that extent.
730 if (r != NULLRTBLOCK) {
735 * If the "next block to try" returned from the
736 * allocator is beyond the next bitmap block,
737 * skip to that bitmap block.
739 if (XFS_BITTOBLOCK(mp, n) > i + 1)
740 i = XFS_BITTOBLOCK(mp, n) - 1;
744 * Got nothing, return failure.
746 *rtblock = NULLRTBLOCK;
751 * Allocate space to the bitmap or summary file, and zero it, for growfs.
755 struct xfs_mount *mp, /* file system mount point */
756 xfs_extlen_t oblocks, /* old count of blocks */
757 xfs_extlen_t nblocks, /* new count of blocks */
758 struct xfs_inode *ip) /* inode (bitmap/summary) */
760 xfs_fileoff_t bno; /* block number in file */
761 struct xfs_buf *bp; /* temporary buffer for zeroing */
762 xfs_daddr_t d; /* disk block address */
763 int error; /* error return value */
764 xfs_fsblock_t firstblock;/* first block allocated in xaction */
765 struct xfs_defer_ops dfops; /* list of freed blocks */
766 xfs_fsblock_t fsbno; /* filesystem block for bno */
767 struct xfs_bmbt_irec map; /* block map output */
768 int nmap; /* number of block maps */
769 int resblks; /* space reservation */
770 struct xfs_trans *tp;
773 * Allocate space to the file, as necessary.
775 while (oblocks < nblocks) {
776 resblks = XFS_GROWFSRT_SPACE_RES(mp, nblocks - oblocks);
778 * Reserve space & log for one extent added to the file.
780 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_growrtalloc, resblks,
787 xfs_ilock(ip, XFS_ILOCK_EXCL);
788 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
790 xfs_defer_init(&dfops, &firstblock);
792 * Allocate blocks to the bitmap file.
795 error = xfs_bmapi_write(tp, ip, oblocks, nblocks - oblocks,
796 XFS_BMAPI_METADATA, &firstblock,
797 resblks, &map, &nmap, &dfops);
798 if (!error && nmap < 1)
801 goto out_bmap_cancel;
803 * Free any blocks freed up in the transaction, then commit.
805 error = xfs_defer_finish(&tp, &dfops);
807 goto out_bmap_cancel;
808 error = xfs_trans_commit(tp);
812 * Now we need to clear the allocated blocks.
813 * Do this one block per transaction, to keep it simple.
815 for (bno = map.br_startoff, fsbno = map.br_startblock;
816 bno < map.br_startoff + map.br_blockcount;
819 * Reserve log for one block zeroing.
821 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_growrtzero,
826 * Lock the bitmap inode.
828 xfs_ilock(ip, XFS_ILOCK_EXCL);
829 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
831 * Get a buffer for the block.
833 d = XFS_FSB_TO_DADDR(mp, fsbno);
834 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
838 goto out_trans_cancel;
840 memset(bp->b_addr, 0, mp->m_sb.sb_blocksize);
841 xfs_trans_log_buf(tp, bp, 0, mp->m_sb.sb_blocksize - 1);
843 * Commit the transaction.
845 error = xfs_trans_commit(tp);
850 * Go on to the next extent, if any.
852 oblocks = map.br_startoff + map.br_blockcount;
858 xfs_defer_cancel(&dfops);
860 xfs_trans_cancel(tp);
865 * Visible (exported) functions.
869 * Grow the realtime area of the filesystem.
873 xfs_mount_t *mp, /* mount point for filesystem */
874 xfs_growfs_rt_t *in) /* growfs rt input struct */
876 xfs_rtblock_t bmbno; /* bitmap block number */
877 xfs_buf_t *bp; /* temporary buffer */
878 int error; /* error return value */
879 xfs_mount_t *nmp; /* new (fake) mount structure */
880 xfs_rfsblock_t nrblocks; /* new number of realtime blocks */
881 xfs_extlen_t nrbmblocks; /* new number of rt bitmap blocks */
882 xfs_rtblock_t nrextents; /* new number of realtime extents */
883 uint8_t nrextslog; /* new log2 of sb_rextents */
884 xfs_extlen_t nrsumblocks; /* new number of summary blocks */
885 uint nrsumlevels; /* new rt summary levels */
886 uint nrsumsize; /* new size of rt summary, bytes */
887 xfs_sb_t *nsbp; /* new superblock */
888 xfs_extlen_t rbmblocks; /* current number of rt bitmap blocks */
889 xfs_extlen_t rsumblocks; /* current number of rt summary blks */
890 xfs_sb_t *sbp; /* old superblock */
891 xfs_fsblock_t sumbno; /* summary block number */
895 * Initial error checking.
897 if (!capable(CAP_SYS_ADMIN))
899 if (mp->m_rtdev_targp == NULL || mp->m_rbmip == NULL ||
900 (nrblocks = in->newblocks) <= sbp->sb_rblocks ||
901 (sbp->sb_rblocks && (in->extsize != sbp->sb_rextsize)))
903 if ((error = xfs_sb_validate_fsb_count(sbp, nrblocks)))
906 * Read in the last block of the device, make sure it exists.
908 error = xfs_buf_read_uncached(mp->m_rtdev_targp,
909 XFS_FSB_TO_BB(mp, nrblocks - 1),
910 XFS_FSB_TO_BB(mp, 1), 0, &bp, NULL);
916 * Calculate new parameters. These are the final values to be reached.
918 nrextents = nrblocks;
919 do_div(nrextents, in->extsize);
920 nrbmblocks = howmany_64(nrextents, NBBY * sbp->sb_blocksize);
921 nrextslog = xfs_highbit32(nrextents);
922 nrsumlevels = nrextslog + 1;
923 nrsumsize = (uint)sizeof(xfs_suminfo_t) * nrsumlevels * nrbmblocks;
924 nrsumblocks = XFS_B_TO_FSB(mp, nrsumsize);
925 nrsumsize = XFS_FSB_TO_B(mp, nrsumblocks);
927 * New summary size can't be more than half the size of
928 * the log. This prevents us from getting a log overflow,
929 * since we'll log basically the whole summary file at once.
931 if (nrsumblocks > (mp->m_sb.sb_logblocks >> 1))
934 * Get the old block counts for bitmap and summary inodes.
935 * These can't change since other growfs callers are locked out.
937 rbmblocks = XFS_B_TO_FSB(mp, mp->m_rbmip->i_d.di_size);
938 rsumblocks = XFS_B_TO_FSB(mp, mp->m_rsumip->i_d.di_size);
940 * Allocate space to the bitmap and summary files, as necessary.
942 error = xfs_growfs_rt_alloc(mp, rbmblocks, nrbmblocks, mp->m_rbmip);
945 error = xfs_growfs_rt_alloc(mp, rsumblocks, nrsumblocks, mp->m_rsumip);
949 * Allocate a new (fake) mount/sb.
951 nmp = kmem_alloc(sizeof(*nmp), KM_SLEEP);
953 * Loop over the bitmap blocks.
954 * We will do everything one bitmap block at a time.
955 * Skip the current block if it is exactly full.
956 * This also deals with the case where there were no rtextents before.
958 for (bmbno = sbp->sb_rbmblocks -
959 ((sbp->sb_rextents & ((1 << mp->m_blkbit_log) - 1)) != 0);
967 * Calculate new sb and mount fields for this round.
969 nsbp->sb_rextsize = in->extsize;
970 nsbp->sb_rbmblocks = bmbno + 1;
973 nsbp->sb_rbmblocks * NBBY *
974 nsbp->sb_blocksize * nsbp->sb_rextsize);
975 nsbp->sb_rextents = nsbp->sb_rblocks;
976 do_div(nsbp->sb_rextents, nsbp->sb_rextsize);
977 ASSERT(nsbp->sb_rextents != 0);
978 nsbp->sb_rextslog = xfs_highbit32(nsbp->sb_rextents);
979 nrsumlevels = nmp->m_rsumlevels = nsbp->sb_rextslog + 1;
981 (uint)sizeof(xfs_suminfo_t) * nrsumlevels *
983 nrsumblocks = XFS_B_TO_FSB(mp, nrsumsize);
984 nmp->m_rsumsize = nrsumsize = XFS_FSB_TO_B(mp, nrsumblocks);
986 * Start a transaction, get the log reservation.
988 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_growrtfree, 0, 0, 0,
993 * Lock out other callers by grabbing the bitmap inode lock.
995 xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL);
996 xfs_trans_ijoin(tp, mp->m_rbmip, XFS_ILOCK_EXCL);
998 * Update the bitmap inode's size.
1000 mp->m_rbmip->i_d.di_size =
1001 nsbp->sb_rbmblocks * nsbp->sb_blocksize;
1002 xfs_trans_log_inode(tp, mp->m_rbmip, XFS_ILOG_CORE);
1004 * Get the summary inode into the transaction.
1006 xfs_ilock(mp->m_rsumip, XFS_ILOCK_EXCL);
1007 xfs_trans_ijoin(tp, mp->m_rsumip, XFS_ILOCK_EXCL);
1009 * Update the summary inode's size.
1011 mp->m_rsumip->i_d.di_size = nmp->m_rsumsize;
1012 xfs_trans_log_inode(tp, mp->m_rsumip, XFS_ILOG_CORE);
1014 * Copy summary data from old to new sizes.
1015 * Do this when the real size (not block-aligned) changes.
1017 if (sbp->sb_rbmblocks != nsbp->sb_rbmblocks ||
1018 mp->m_rsumlevels != nmp->m_rsumlevels) {
1019 error = xfs_rtcopy_summary(mp, nmp, tp);
1024 * Update superblock fields.
1026 if (nsbp->sb_rextsize != sbp->sb_rextsize)
1027 xfs_trans_mod_sb(tp, XFS_TRANS_SB_REXTSIZE,
1028 nsbp->sb_rextsize - sbp->sb_rextsize);
1029 if (nsbp->sb_rbmblocks != sbp->sb_rbmblocks)
1030 xfs_trans_mod_sb(tp, XFS_TRANS_SB_RBMBLOCKS,
1031 nsbp->sb_rbmblocks - sbp->sb_rbmblocks);
1032 if (nsbp->sb_rblocks != sbp->sb_rblocks)
1033 xfs_trans_mod_sb(tp, XFS_TRANS_SB_RBLOCKS,
1034 nsbp->sb_rblocks - sbp->sb_rblocks);
1035 if (nsbp->sb_rextents != sbp->sb_rextents)
1036 xfs_trans_mod_sb(tp, XFS_TRANS_SB_REXTENTS,
1037 nsbp->sb_rextents - sbp->sb_rextents);
1038 if (nsbp->sb_rextslog != sbp->sb_rextslog)
1039 xfs_trans_mod_sb(tp, XFS_TRANS_SB_REXTSLOG,
1040 nsbp->sb_rextslog - sbp->sb_rextslog);
1045 error = xfs_rtfree_range(nmp, tp, sbp->sb_rextents,
1046 nsbp->sb_rextents - sbp->sb_rextents, &bp, &sumbno);
1049 xfs_trans_cancel(tp);
1053 * Mark more blocks free in the superblock.
1055 xfs_trans_mod_sb(tp, XFS_TRANS_SB_FREXTENTS,
1056 nsbp->sb_rextents - sbp->sb_rextents);
1058 * Update mp values into the real mp structure.
1060 mp->m_rsumlevels = nrsumlevels;
1061 mp->m_rsumsize = nrsumsize;
1063 error = xfs_trans_commit(tp);
1069 * Free the fake mp structure.
1077 * Allocate an extent in the realtime subvolume, with the usual allocation
1078 * parameters. The length units are all in realtime extents, as is the
1079 * result block number.
1082 xfs_rtallocate_extent(
1083 xfs_trans_t *tp, /* transaction pointer */
1084 xfs_rtblock_t bno, /* starting block number to allocate */
1085 xfs_extlen_t minlen, /* minimum length to allocate */
1086 xfs_extlen_t maxlen, /* maximum length to allocate */
1087 xfs_extlen_t *len, /* out: actual length allocated */
1088 int wasdel, /* was a delayed allocation extent */
1089 xfs_extlen_t prod, /* extent product factor */
1090 xfs_rtblock_t *rtblock) /* out: start block allocated */
1092 xfs_mount_t *mp = tp->t_mountp;
1093 int error; /* error value */
1094 xfs_rtblock_t r; /* result allocated block */
1095 xfs_fsblock_t sb; /* summary file block number */
1096 xfs_buf_t *sumbp; /* summary file block buffer */
1098 ASSERT(xfs_isilocked(mp->m_rbmip, XFS_ILOCK_EXCL));
1099 ASSERT(minlen > 0 && minlen <= maxlen);
1102 * If prod is set then figure out what to do to minlen and maxlen.
1107 if ((i = maxlen % prod))
1109 if ((i = minlen % prod))
1111 if (maxlen < minlen) {
1112 *rtblock = NULLRTBLOCK;
1120 error = xfs_rtallocate_extent_size(mp, tp, minlen, maxlen, len,
1121 &sumbp, &sb, prod, &r);
1123 error = xfs_rtallocate_extent_near(mp, tp, bno, minlen, maxlen,
1124 len, &sumbp, &sb, prod, &r);
1131 * If it worked, update the superblock.
1133 if (r != NULLRTBLOCK) {
1134 long slen = (long)*len;
1136 ASSERT(*len >= minlen && *len <= maxlen);
1138 xfs_trans_mod_sb(tp, XFS_TRANS_SB_RES_FREXTENTS, -slen);
1140 xfs_trans_mod_sb(tp, XFS_TRANS_SB_FREXTENTS, -slen);
1141 } else if (prod > 1) {
1151 * Initialize realtime fields in the mount structure.
1155 struct xfs_mount *mp) /* file system mount structure */
1157 struct xfs_buf *bp; /* buffer for last block of subvolume */
1158 struct xfs_sb *sbp; /* filesystem superblock copy in mount */
1159 xfs_daddr_t d; /* address of last block of subvolume */
1163 if (sbp->sb_rblocks == 0)
1165 if (mp->m_rtdev_targp == NULL) {
1167 "Filesystem has a realtime volume, use rtdev=device option");
1170 mp->m_rsumlevels = sbp->sb_rextslog + 1;
1172 (uint)sizeof(xfs_suminfo_t) * mp->m_rsumlevels *
1174 mp->m_rsumsize = roundup(mp->m_rsumsize, sbp->sb_blocksize);
1175 mp->m_rbmip = mp->m_rsumip = NULL;
1177 * Check that the realtime section is an ok size.
1179 d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_rblocks);
1180 if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_rblocks) {
1181 xfs_warn(mp, "realtime mount -- %llu != %llu",
1182 (unsigned long long) XFS_BB_TO_FSB(mp, d),
1183 (unsigned long long) mp->m_sb.sb_rblocks);
1186 error = xfs_buf_read_uncached(mp->m_rtdev_targp,
1187 d - XFS_FSB_TO_BB(mp, 1),
1188 XFS_FSB_TO_BB(mp, 1), 0, &bp, NULL);
1190 xfs_warn(mp, "realtime device size check failed");
1198 * Get the bitmap and summary inodes into the mount structure
1203 xfs_mount_t *mp) /* file system mount structure */
1205 int error; /* error return value */
1209 if (sbp->sb_rbmino == NULLFSINO)
1211 error = xfs_iget(mp, NULL, sbp->sb_rbmino, 0, 0, &mp->m_rbmip);
1214 ASSERT(mp->m_rbmip != NULL);
1215 ASSERT(sbp->sb_rsumino != NULLFSINO);
1216 error = xfs_iget(mp, NULL, sbp->sb_rsumino, 0, 0, &mp->m_rsumip);
1221 ASSERT(mp->m_rsumip != NULL);
1226 xfs_rtunmount_inodes(
1227 struct xfs_mount *mp)
1232 IRELE(mp->m_rsumip);
1236 * Pick an extent for allocation at the start of a new realtime file.
1237 * Use the sequence number stored in the atime field of the bitmap inode.
1238 * Translate this to a fraction of the rtextents, and return the product
1239 * of rtextents and the fraction.
1240 * The fraction sequence is 0, 1/2, 1/4, 3/4, 1/8, ..., 7/8, 1/16, ...
1244 xfs_mount_t *mp, /* file system mount point */
1245 xfs_trans_t *tp, /* transaction pointer */
1246 xfs_extlen_t len, /* allocation length (rtextents) */
1247 xfs_rtblock_t *pick) /* result rt extent */
1249 xfs_rtblock_t b; /* result block */
1250 int log2; /* log of sequence number */
1251 uint64_t resid; /* residual after log removed */
1252 uint64_t seq; /* sequence number of file creation */
1253 uint64_t *seqp; /* pointer to seqno in inode */
1255 ASSERT(xfs_isilocked(mp->m_rbmip, XFS_ILOCK_EXCL));
1257 seqp = (uint64_t *)&VFS_I(mp->m_rbmip)->i_atime;
1258 if (!(mp->m_rbmip->i_d.di_flags & XFS_DIFLAG_NEWRTBM)) {
1259 mp->m_rbmip->i_d.di_flags |= XFS_DIFLAG_NEWRTBM;
1263 if ((log2 = xfs_highbit64(seq)) == -1)
1266 resid = seq - (1ULL << log2);
1267 b = (mp->m_sb.sb_rextents * ((resid << 1) + 1ULL)) >>
1269 if (b >= mp->m_sb.sb_rextents)
1270 div64_u64_rem(b, mp->m_sb.sb_rextents, &b);
1271 if (b + len > mp->m_sb.sb_rextents)
1272 b = mp->m_sb.sb_rextents - len;
1275 xfs_trans_log_inode(tp, mp->m_rbmip, XFS_ILOG_CORE);