2 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 #include "xfs_shared.h"
21 #include "xfs_format.h"
22 #include "xfs_log_format.h"
23 #include "xfs_trans_resv.h"
25 #include "xfs_mount.h"
26 #include "xfs_defer.h"
27 #include "xfs_inode.h"
29 #include "xfs_bmap_util.h"
30 #include "xfs_bmap_btree.h"
31 #include "xfs_alloc.h"
32 #include "xfs_error.h"
33 #include "xfs_trans.h"
34 #include "xfs_trans_space.h"
35 #include "xfs_trace.h"
37 #include "xfs_icache.h"
38 #include "xfs_rtalloc.h"
42 * Read and return the summary information for a given extent size,
43 * bitmap block combination.
44 * Keeps track of a current summary block, so we don't keep reading
45 * it from the buffer cache.
49 xfs_mount_t *mp, /* file system mount structure */
50 xfs_trans_t *tp, /* transaction pointer */
51 int log, /* log2 of extent size */
52 xfs_rtblock_t bbno, /* bitmap block number */
53 xfs_buf_t **rbpp, /* in/out: summary block buffer */
54 xfs_fsblock_t *rsb, /* in/out: summary block number */
55 xfs_suminfo_t *sum) /* out: summary info for this block */
57 return xfs_rtmodify_summary_int(mp, tp, log, bbno, 0, rbpp, rsb, sum);
61 * Return whether there are any free extents in the size range given
62 * by low and high, for the bitmap block bbno.
64 STATIC int /* error */
66 xfs_mount_t *mp, /* file system mount structure */
67 xfs_trans_t *tp, /* transaction pointer */
68 int low, /* low log2 extent size */
69 int high, /* high log2 extent size */
70 xfs_rtblock_t bbno, /* bitmap block number */
71 xfs_buf_t **rbpp, /* in/out: summary block buffer */
72 xfs_fsblock_t *rsb, /* in/out: summary block number */
73 int *stat) /* out: any good extents here? */
75 int error; /* error value */
76 int log; /* loop counter, log2 of ext. size */
77 xfs_suminfo_t sum; /* summary data */
80 * Loop over logs of extent sizes. Order is irrelevant.
82 for (log = low; log <= high; log++) {
84 * Get one summary datum.
86 error = xfs_rtget_summary(mp, tp, log, bbno, rbpp, rsb, &sum);
91 * If there are any, return success.
99 * Found nothing, return failure.
107 * Copy and transform the summary file, given the old and new
108 * parameters in the mount structures.
110 STATIC int /* error */
112 xfs_mount_t *omp, /* old file system mount point */
113 xfs_mount_t *nmp, /* new file system mount point */
114 xfs_trans_t *tp) /* transaction pointer */
116 xfs_rtblock_t bbno; /* bitmap block number */
117 xfs_buf_t *bp; /* summary buffer */
118 int error; /* error return value */
119 int log; /* summary level number (log length) */
120 xfs_suminfo_t sum; /* summary data */
121 xfs_fsblock_t sumbno; /* summary block number */
124 for (log = omp->m_rsumlevels - 1; log >= 0; log--) {
125 for (bbno = omp->m_sb.sb_rbmblocks - 1;
126 (xfs_srtblock_t)bbno >= 0;
128 error = xfs_rtget_summary(omp, tp, log, bbno, &bp,
134 error = xfs_rtmodify_summary(omp, tp, log, bbno, -sum,
138 error = xfs_rtmodify_summary(nmp, tp, log, bbno, sum,
148 * Mark an extent specified by start and len allocated.
149 * Updates all the summary information as well as the bitmap.
151 STATIC int /* error */
152 xfs_rtallocate_range(
153 xfs_mount_t *mp, /* file system mount point */
154 xfs_trans_t *tp, /* transaction pointer */
155 xfs_rtblock_t start, /* start block to allocate */
156 xfs_extlen_t len, /* length to allocate */
157 xfs_buf_t **rbpp, /* in/out: summary block buffer */
158 xfs_fsblock_t *rsb) /* in/out: summary block number */
160 xfs_rtblock_t end; /* end of the allocated extent */
161 int error; /* error value */
162 xfs_rtblock_t postblock = 0; /* first block allocated > end */
163 xfs_rtblock_t preblock = 0; /* first block allocated < start */
165 end = start + len - 1;
167 * Assume we're allocating out of the middle of a free extent.
168 * We need to find the beginning and end of the extent so we can
169 * properly update the summary.
171 error = xfs_rtfind_back(mp, tp, start, 0, &preblock);
176 * Find the next allocated block (end of free extent).
178 error = xfs_rtfind_forw(mp, tp, end, mp->m_sb.sb_rextents - 1,
184 * Decrement the summary information corresponding to the entire
187 error = xfs_rtmodify_summary(mp, tp,
188 XFS_RTBLOCKLOG(postblock + 1 - preblock),
189 XFS_BITTOBLOCK(mp, preblock), -1, rbpp, rsb);
194 * If there are blocks not being allocated at the front of the
195 * old extent, add summary data for them to be free.
197 if (preblock < start) {
198 error = xfs_rtmodify_summary(mp, tp,
199 XFS_RTBLOCKLOG(start - preblock),
200 XFS_BITTOBLOCK(mp, preblock), 1, rbpp, rsb);
206 * If there are blocks not being allocated at the end of the
207 * old extent, add summary data for them to be free.
209 if (postblock > end) {
210 error = xfs_rtmodify_summary(mp, tp,
211 XFS_RTBLOCKLOG(postblock - end),
212 XFS_BITTOBLOCK(mp, end + 1), 1, rbpp, rsb);
218 * Modify the bitmap to mark this extent allocated.
220 error = xfs_rtmodify_range(mp, tp, start, len, 0);
225 * Attempt to allocate an extent minlen<=len<=maxlen starting from
226 * bitmap block bbno. If we don't get maxlen then use prod to trim
227 * the length, if given. Returns error; returns starting block in *rtblock.
228 * The lengths are all in rtextents.
230 STATIC int /* error */
231 xfs_rtallocate_extent_block(
232 xfs_mount_t *mp, /* file system mount point */
233 xfs_trans_t *tp, /* transaction pointer */
234 xfs_rtblock_t bbno, /* bitmap block number */
235 xfs_extlen_t minlen, /* minimum length to allocate */
236 xfs_extlen_t maxlen, /* maximum length to allocate */
237 xfs_extlen_t *len, /* out: actual length allocated */
238 xfs_rtblock_t *nextp, /* out: next block to try */
239 xfs_buf_t **rbpp, /* in/out: summary block buffer */
240 xfs_fsblock_t *rsb, /* in/out: summary block number */
241 xfs_extlen_t prod, /* extent product factor */
242 xfs_rtblock_t *rtblock) /* out: start block allocated */
244 xfs_rtblock_t besti; /* best rtblock found so far */
245 xfs_rtblock_t bestlen; /* best length found so far */
246 xfs_rtblock_t end; /* last rtblock in chunk */
247 int error; /* error value */
248 xfs_rtblock_t i; /* current rtblock trying */
249 xfs_rtblock_t next; /* next rtblock 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_BLOCKTOBIT(mp, bbno), besti = -1, bestlen = 0,
257 end = XFS_BLOCKTOBIT(mp, bbno + 1) - 1;
260 /* Make sure we don't scan off the end of the rt volume. */
261 maxlen = min(mp->m_sb.sb_rextents, i + maxlen) - i;
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(mp, tp, i, maxlen, 1, &next, &stat);
273 * i for maxlen is all free, allocate and return that.
275 error = xfs_rtallocate_range(mp, tp, i, maxlen, rbpp,
285 * In the case where we have a variable-sized allocation
286 * request, figure out how big this free piece is,
287 * and if it's big enough for the minimum, and the best
288 * so far, remember it.
290 if (minlen < maxlen) {
291 xfs_rtblock_t thislen; /* this extent size */
294 if (thislen >= minlen && thislen > bestlen) {
300 * If not done yet, find the start of the next free space.
303 error = xfs_rtfind_forw(mp, tp, next, end, &i);
311 * Searched the whole thing & didn't find a maxlen free extent.
313 if (minlen < maxlen && besti != -1) {
314 xfs_extlen_t p; /* amount to trim length by */
317 * If size should be a multiple of prod, make that so.
319 if (prod > 1 && (p = do_mod(bestlen, prod)))
322 * Allocate besti for bestlen & return that.
324 error = xfs_rtallocate_range(mp, tp, besti, bestlen, rbpp, rsb);
333 * Allocation failed. Set *nextp to the next block to try.
336 *rtblock = NULLRTBLOCK;
341 * Allocate an extent of length minlen<=len<=maxlen, starting at block
342 * bno. If we don't get maxlen then use prod to trim the length, if given.
343 * Returns error; returns starting block in *rtblock.
344 * The lengths are all in rtextents.
346 STATIC int /* error */
347 xfs_rtallocate_extent_exact(
348 xfs_mount_t *mp, /* file system mount point */
349 xfs_trans_t *tp, /* transaction pointer */
350 xfs_rtblock_t bno, /* starting block number to allocate */
351 xfs_extlen_t minlen, /* minimum length to allocate */
352 xfs_extlen_t maxlen, /* maximum length to allocate */
353 xfs_extlen_t *len, /* out: actual length allocated */
354 xfs_buf_t **rbpp, /* in/out: summary block buffer */
355 xfs_fsblock_t *rsb, /* in/out: summary block number */
356 xfs_extlen_t prod, /* extent product factor */
357 xfs_rtblock_t *rtblock) /* out: start block allocated */
359 int error; /* error value */
360 xfs_extlen_t i; /* extent length trimmed due to prod */
361 int isfree; /* extent is free */
362 xfs_rtblock_t next; /* next block to try (dummy) */
364 ASSERT(minlen % prod == 0 && maxlen % prod == 0);
366 * Check if the range in question (for maxlen) is free.
368 error = xfs_rtcheck_range(mp, tp, bno, maxlen, 1, &next, &isfree);
374 * If it is, allocate it and return success.
376 error = xfs_rtallocate_range(mp, tp, bno, maxlen, rbpp, rsb);
385 * If not, allocate what there is, if it's at least minlen.
388 if (maxlen < minlen) {
390 * Failed, return failure status.
392 *rtblock = NULLRTBLOCK;
396 * Trim off tail of extent, if prod is specified.
398 if (prod > 1 && (i = maxlen % prod)) {
400 if (maxlen < minlen) {
402 * Now we can't do it, return failure status.
404 *rtblock = NULLRTBLOCK;
409 * Allocate what we can and return it.
411 error = xfs_rtallocate_range(mp, tp, bno, maxlen, rbpp, rsb);
421 * Allocate an extent of length minlen<=len<=maxlen, starting as near
422 * to bno as possible. If we don't get maxlen then use prod to trim
423 * the length, if given. The lengths are all in rtextents.
425 STATIC int /* error */
426 xfs_rtallocate_extent_near(
427 xfs_mount_t *mp, /* file system mount point */
428 xfs_trans_t *tp, /* transaction pointer */
429 xfs_rtblock_t bno, /* starting block number to allocate */
430 xfs_extlen_t minlen, /* minimum length to allocate */
431 xfs_extlen_t maxlen, /* maximum length to allocate */
432 xfs_extlen_t *len, /* out: actual length allocated */
433 xfs_buf_t **rbpp, /* in/out: summary block buffer */
434 xfs_fsblock_t *rsb, /* in/out: summary block number */
435 xfs_extlen_t prod, /* extent product factor */
436 xfs_rtblock_t *rtblock) /* out: start block allocated */
438 int any; /* any useful extents from summary */
439 xfs_rtblock_t bbno; /* bitmap block number */
440 int error; /* error value */
441 int i; /* bitmap block offset (loop control) */
442 int j; /* secondary loop control */
443 int log2len; /* log2 of minlen */
444 xfs_rtblock_t n; /* next block to try */
445 xfs_rtblock_t r; /* result block */
447 ASSERT(minlen % prod == 0 && maxlen % prod == 0);
449 * If the block number given is off the end, silently set it to
452 if (bno >= mp->m_sb.sb_rextents)
453 bno = mp->m_sb.sb_rextents - 1;
455 /* Make sure we don't run off the end of the rt volume. */
456 maxlen = min(mp->m_sb.sb_rextents, bno + maxlen) - bno;
457 if (maxlen < minlen) {
458 *rtblock = NULLRTBLOCK;
463 * Try the exact allocation first.
465 error = xfs_rtallocate_extent_exact(mp, tp, bno, minlen, maxlen, len,
466 rbpp, rsb, prod, &r);
471 * If the exact allocation worked, return that.
473 if (r != NULLRTBLOCK) {
477 bbno = XFS_BITTOBLOCK(mp, bno);
480 log2len = xfs_highbit32(minlen);
482 * Loop over all bitmap blocks (bbno + i is current block).
486 * Get summary information of extents of all useful levels
487 * starting in this bitmap block.
489 error = xfs_rtany_summary(mp, tp, log2len, mp->m_rsumlevels - 1,
490 bbno + i, rbpp, rsb, &any);
495 * If there are any useful extents starting here, try
500 * On the positive side of the starting location.
504 * Try to allocate an extent starting in
507 error = xfs_rtallocate_extent_block(mp, tp,
508 bbno + i, minlen, maxlen, len, &n, rbpp,
514 * If it worked, return it.
516 if (r != NULLRTBLOCK) {
522 * On the negative side of the starting location.
526 * Loop backwards through the bitmap blocks from
527 * the starting point-1 up to where we are now.
528 * There should be an extent which ends in this
529 * bitmap block and is long enough.
531 for (j = -1; j > i; j--) {
533 * Grab the summary information for
536 error = xfs_rtany_summary(mp, tp,
537 log2len, mp->m_rsumlevels - 1,
538 bbno + j, rbpp, rsb, &any);
543 * If there's no extent given in the
544 * summary that means the extent we
545 * found must carry over from an
546 * earlier block. If there is an
547 * extent given, we've already tried
548 * that allocation, don't do it again.
552 error = xfs_rtallocate_extent_block(mp,
553 tp, bbno + j, minlen, maxlen,
554 len, &n, rbpp, rsb, prod, &r);
559 * If it works, return the extent.
561 if (r != NULLRTBLOCK) {
567 * There weren't intervening bitmap blocks
568 * with a long enough extent, or the
569 * allocation didn't work for some reason
570 * (i.e. it's a little * too short).
571 * Try to allocate from the summary block
574 error = xfs_rtallocate_extent_block(mp, tp,
575 bbno + i, minlen, maxlen, len, &n, rbpp,
581 * If it works, return the extent.
583 if (r != NULLRTBLOCK) {
590 * Loop control. If we were on the positive side, and there's
591 * still more blocks on the negative side, go there.
593 if (i > 0 && (int)bbno - i >= 0)
596 * If positive, and no more negative, but there are more
597 * positive, go there.
599 else if (i > 0 && (int)bbno + i < mp->m_sb.sb_rbmblocks - 1)
602 * If negative or 0 (just started), and there are positive
603 * blocks to go, go there. The 0 case moves to block 1.
605 else if (i <= 0 && (int)bbno - i < mp->m_sb.sb_rbmblocks - 1)
608 * If negative or 0 and there are more negative blocks,
611 else if (i <= 0 && (int)bbno + i > 0)
614 * Must be done. Return failure.
619 *rtblock = NULLRTBLOCK;
624 * Allocate an extent of length minlen<=len<=maxlen, with no position
625 * specified. If we don't get maxlen then use prod to trim
626 * the length, if given. The lengths are all in rtextents.
628 STATIC int /* error */
629 xfs_rtallocate_extent_size(
630 xfs_mount_t *mp, /* file system mount point */
631 xfs_trans_t *tp, /* transaction pointer */
632 xfs_extlen_t minlen, /* minimum length to allocate */
633 xfs_extlen_t maxlen, /* maximum length to allocate */
634 xfs_extlen_t *len, /* out: actual length allocated */
635 xfs_buf_t **rbpp, /* in/out: summary block buffer */
636 xfs_fsblock_t *rsb, /* in/out: summary block number */
637 xfs_extlen_t prod, /* extent product factor */
638 xfs_rtblock_t *rtblock) /* out: start block allocated */
640 int error; /* error value */
641 int i; /* bitmap block number */
642 int l; /* level number (loop control) */
643 xfs_rtblock_t n; /* next block to be tried */
644 xfs_rtblock_t r; /* result block number */
645 xfs_suminfo_t sum; /* summary information for extents */
647 ASSERT(minlen % prod == 0 && maxlen % prod == 0);
651 * Loop over all the levels starting with maxlen.
652 * At each level, look at all the bitmap blocks, to see if there
653 * are extents starting there that are long enough (>= maxlen).
654 * Note, only on the initial level can the allocation fail if
655 * the summary says there's an extent.
657 for (l = xfs_highbit32(maxlen); l < mp->m_rsumlevels; l++) {
659 * Loop over all the bitmap blocks.
661 for (i = 0; i < mp->m_sb.sb_rbmblocks; i++) {
663 * Get the summary for this level/block.
665 error = xfs_rtget_summary(mp, tp, l, i, rbpp, rsb,
671 * Nothing there, on to the next block.
676 * Try allocating the extent.
678 error = xfs_rtallocate_extent_block(mp, tp, i, maxlen,
679 maxlen, len, &n, rbpp, rsb, prod, &r);
684 * If it worked, return that.
686 if (r != NULLRTBLOCK) {
691 * If the "next block to try" returned from the
692 * allocator is beyond the next bitmap block,
693 * skip to that bitmap block.
695 if (XFS_BITTOBLOCK(mp, n) > i + 1)
696 i = XFS_BITTOBLOCK(mp, n) - 1;
700 * Didn't find any maxlen blocks. Try smaller ones, unless
701 * we're asking for a fixed size extent.
703 if (minlen > --maxlen) {
704 *rtblock = NULLRTBLOCK;
711 * Loop over sizes, from maxlen down to minlen.
712 * This time, when we do the allocations, allow smaller ones
715 for (l = xfs_highbit32(maxlen); l >= xfs_highbit32(minlen); l--) {
717 * Loop over all the bitmap blocks, try an allocation
718 * starting in that block.
720 for (i = 0; i < mp->m_sb.sb_rbmblocks; i++) {
722 * Get the summary information for this level/block.
724 error = xfs_rtget_summary(mp, tp, l, i, rbpp, rsb,
730 * If nothing there, go on to next.
735 * Try the allocation. Make sure the specified
736 * minlen/maxlen are in the possible range for
737 * this summary level.
739 error = xfs_rtallocate_extent_block(mp, tp, i,
740 XFS_RTMAX(minlen, 1 << l),
741 XFS_RTMIN(maxlen, (1 << (l + 1)) - 1),
742 len, &n, rbpp, rsb, prod, &r);
747 * If it worked, return that extent.
749 if (r != NULLRTBLOCK) {
754 * If the "next block to try" returned from the
755 * allocator is beyond the next bitmap block,
756 * skip to that bitmap block.
758 if (XFS_BITTOBLOCK(mp, n) > i + 1)
759 i = XFS_BITTOBLOCK(mp, n) - 1;
763 * Got nothing, return failure.
765 *rtblock = NULLRTBLOCK;
770 * Allocate space to the bitmap or summary file, and zero it, for growfs.
774 struct xfs_mount *mp, /* file system mount point */
775 xfs_extlen_t oblocks, /* old count of blocks */
776 xfs_extlen_t nblocks, /* new count of blocks */
777 struct xfs_inode *ip) /* inode (bitmap/summary) */
779 xfs_fileoff_t bno; /* block number in file */
780 struct xfs_buf *bp; /* temporary buffer for zeroing */
781 xfs_daddr_t d; /* disk block address */
782 int error; /* error return value */
783 xfs_fsblock_t firstblock;/* first block allocated in xaction */
784 struct xfs_defer_ops dfops; /* list of freed blocks */
785 xfs_fsblock_t fsbno; /* filesystem block for bno */
786 struct xfs_bmbt_irec map; /* block map output */
787 int nmap; /* number of block maps */
788 int resblks; /* space reservation */
789 struct xfs_trans *tp;
792 * Allocate space to the file, as necessary.
794 while (oblocks < nblocks) {
795 resblks = XFS_GROWFSRT_SPACE_RES(mp, nblocks - oblocks);
797 * Reserve space & log for one extent added to the file.
799 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_growrtalloc, resblks,
806 xfs_ilock(ip, XFS_ILOCK_EXCL);
807 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
809 xfs_defer_init(&dfops, &firstblock);
811 * Allocate blocks to the bitmap file.
814 error = xfs_bmapi_write(tp, ip, oblocks, nblocks - oblocks,
815 XFS_BMAPI_METADATA, &firstblock,
816 resblks, &map, &nmap, &dfops);
817 if (!error && nmap < 1)
820 goto out_bmap_cancel;
822 * Free any blocks freed up in the transaction, then commit.
824 error = xfs_defer_finish(&tp, &dfops, NULL);
826 goto out_bmap_cancel;
827 error = xfs_trans_commit(tp);
831 * Now we need to clear the allocated blocks.
832 * Do this one block per transaction, to keep it simple.
834 for (bno = map.br_startoff, fsbno = map.br_startblock;
835 bno < map.br_startoff + map.br_blockcount;
838 * Reserve log for one block zeroing.
840 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_growrtzero,
845 * Lock the bitmap inode.
847 xfs_ilock(ip, XFS_ILOCK_EXCL);
848 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
850 * Get a buffer for the block.
852 d = XFS_FSB_TO_DADDR(mp, fsbno);
853 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
857 goto out_trans_cancel;
859 memset(bp->b_addr, 0, mp->m_sb.sb_blocksize);
860 xfs_trans_log_buf(tp, bp, 0, mp->m_sb.sb_blocksize - 1);
862 * Commit the transaction.
864 error = xfs_trans_commit(tp);
869 * Go on to the next extent, if any.
871 oblocks = map.br_startoff + map.br_blockcount;
877 xfs_defer_cancel(&dfops);
879 xfs_trans_cancel(tp);
884 * Visible (exported) functions.
888 * Grow the realtime area of the filesystem.
892 xfs_mount_t *mp, /* mount point for filesystem */
893 xfs_growfs_rt_t *in) /* growfs rt input struct */
895 xfs_rtblock_t bmbno; /* bitmap block number */
896 xfs_buf_t *bp; /* temporary buffer */
897 int error; /* error return value */
898 xfs_mount_t *nmp; /* new (fake) mount structure */
899 xfs_rfsblock_t nrblocks; /* new number of realtime blocks */
900 xfs_extlen_t nrbmblocks; /* new number of rt bitmap blocks */
901 xfs_rtblock_t nrextents; /* new number of realtime extents */
902 uint8_t nrextslog; /* new log2 of sb_rextents */
903 xfs_extlen_t nrsumblocks; /* new number of summary blocks */
904 uint nrsumlevels; /* new rt summary levels */
905 uint nrsumsize; /* new size of rt summary, bytes */
906 xfs_sb_t *nsbp; /* new superblock */
907 xfs_extlen_t rbmblocks; /* current number of rt bitmap blocks */
908 xfs_extlen_t rsumblocks; /* current number of rt summary blks */
909 xfs_sb_t *sbp; /* old superblock */
910 xfs_fsblock_t sumbno; /* summary block number */
914 * Initial error checking.
916 if (!capable(CAP_SYS_ADMIN))
918 if (mp->m_rtdev_targp == NULL || mp->m_rbmip == NULL ||
919 (nrblocks = in->newblocks) <= sbp->sb_rblocks ||
920 (sbp->sb_rblocks && (in->extsize != sbp->sb_rextsize)))
922 if ((error = xfs_sb_validate_fsb_count(sbp, nrblocks)))
925 * Read in the last block of the device, make sure it exists.
927 error = xfs_buf_read_uncached(mp->m_rtdev_targp,
928 XFS_FSB_TO_BB(mp, nrblocks - 1),
929 XFS_FSB_TO_BB(mp, 1), 0, &bp, NULL);
935 * Calculate new parameters. These are the final values to be reached.
937 nrextents = nrblocks;
938 do_div(nrextents, in->extsize);
939 nrbmblocks = howmany_64(nrextents, NBBY * sbp->sb_blocksize);
940 nrextslog = xfs_highbit32(nrextents);
941 nrsumlevels = nrextslog + 1;
942 nrsumsize = (uint)sizeof(xfs_suminfo_t) * nrsumlevels * nrbmblocks;
943 nrsumblocks = XFS_B_TO_FSB(mp, nrsumsize);
944 nrsumsize = XFS_FSB_TO_B(mp, nrsumblocks);
946 * New summary size can't be more than half the size of
947 * the log. This prevents us from getting a log overflow,
948 * since we'll log basically the whole summary file at once.
950 if (nrsumblocks > (mp->m_sb.sb_logblocks >> 1))
953 * Get the old block counts for bitmap and summary inodes.
954 * These can't change since other growfs callers are locked out.
956 rbmblocks = XFS_B_TO_FSB(mp, mp->m_rbmip->i_d.di_size);
957 rsumblocks = XFS_B_TO_FSB(mp, mp->m_rsumip->i_d.di_size);
959 * Allocate space to the bitmap and summary files, as necessary.
961 error = xfs_growfs_rt_alloc(mp, rbmblocks, nrbmblocks, mp->m_rbmip);
964 error = xfs_growfs_rt_alloc(mp, rsumblocks, nrsumblocks, mp->m_rsumip);
968 * Allocate a new (fake) mount/sb.
970 nmp = kmem_alloc(sizeof(*nmp), KM_SLEEP);
972 * Loop over the bitmap blocks.
973 * We will do everything one bitmap block at a time.
974 * Skip the current block if it is exactly full.
975 * This also deals with the case where there were no rtextents before.
977 for (bmbno = sbp->sb_rbmblocks -
978 ((sbp->sb_rextents & ((1 << mp->m_blkbit_log) - 1)) != 0);
986 * Calculate new sb and mount fields for this round.
988 nsbp->sb_rextsize = in->extsize;
989 nsbp->sb_rbmblocks = bmbno + 1;
992 nsbp->sb_rbmblocks * NBBY *
993 nsbp->sb_blocksize * nsbp->sb_rextsize);
994 nsbp->sb_rextents = nsbp->sb_rblocks;
995 do_div(nsbp->sb_rextents, nsbp->sb_rextsize);
996 ASSERT(nsbp->sb_rextents != 0);
997 nsbp->sb_rextslog = xfs_highbit32(nsbp->sb_rextents);
998 nrsumlevels = nmp->m_rsumlevels = nsbp->sb_rextslog + 1;
1000 (uint)sizeof(xfs_suminfo_t) * nrsumlevels *
1002 nrsumblocks = XFS_B_TO_FSB(mp, nrsumsize);
1003 nmp->m_rsumsize = nrsumsize = XFS_FSB_TO_B(mp, nrsumblocks);
1005 * Start a transaction, get the log reservation.
1007 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_growrtfree, 0, 0, 0,
1012 * Lock out other callers by grabbing the bitmap inode lock.
1014 xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL);
1015 xfs_trans_ijoin(tp, mp->m_rbmip, XFS_ILOCK_EXCL);
1017 * Update the bitmap inode's size ondisk and incore. We need
1018 * to update the incore size so that inode inactivation won't
1019 * punch what it thinks are "posteof" blocks.
1021 mp->m_rbmip->i_d.di_size =
1022 nsbp->sb_rbmblocks * nsbp->sb_blocksize;
1023 i_size_write(VFS_I(mp->m_rbmip), mp->m_rbmip->i_d.di_size);
1024 xfs_trans_log_inode(tp, mp->m_rbmip, XFS_ILOG_CORE);
1026 * Get the summary inode into the transaction.
1028 xfs_ilock(mp->m_rsumip, XFS_ILOCK_EXCL);
1029 xfs_trans_ijoin(tp, mp->m_rsumip, XFS_ILOCK_EXCL);
1031 * Update the summary inode's size. We need to update the
1032 * incore size so that inode inactivation won't punch what it
1033 * thinks are "posteof" blocks.
1035 mp->m_rsumip->i_d.di_size = nmp->m_rsumsize;
1036 i_size_write(VFS_I(mp->m_rsumip), mp->m_rsumip->i_d.di_size);
1037 xfs_trans_log_inode(tp, mp->m_rsumip, XFS_ILOG_CORE);
1039 * Copy summary data from old to new sizes.
1040 * Do this when the real size (not block-aligned) changes.
1042 if (sbp->sb_rbmblocks != nsbp->sb_rbmblocks ||
1043 mp->m_rsumlevels != nmp->m_rsumlevels) {
1044 error = xfs_rtcopy_summary(mp, nmp, tp);
1049 * Update superblock fields.
1051 if (nsbp->sb_rextsize != sbp->sb_rextsize)
1052 xfs_trans_mod_sb(tp, XFS_TRANS_SB_REXTSIZE,
1053 nsbp->sb_rextsize - sbp->sb_rextsize);
1054 if (nsbp->sb_rbmblocks != sbp->sb_rbmblocks)
1055 xfs_trans_mod_sb(tp, XFS_TRANS_SB_RBMBLOCKS,
1056 nsbp->sb_rbmblocks - sbp->sb_rbmblocks);
1057 if (nsbp->sb_rblocks != sbp->sb_rblocks)
1058 xfs_trans_mod_sb(tp, XFS_TRANS_SB_RBLOCKS,
1059 nsbp->sb_rblocks - sbp->sb_rblocks);
1060 if (nsbp->sb_rextents != sbp->sb_rextents)
1061 xfs_trans_mod_sb(tp, XFS_TRANS_SB_REXTENTS,
1062 nsbp->sb_rextents - sbp->sb_rextents);
1063 if (nsbp->sb_rextslog != sbp->sb_rextslog)
1064 xfs_trans_mod_sb(tp, XFS_TRANS_SB_REXTSLOG,
1065 nsbp->sb_rextslog - sbp->sb_rextslog);
1070 error = xfs_rtfree_range(nmp, tp, sbp->sb_rextents,
1071 nsbp->sb_rextents - sbp->sb_rextents, &bp, &sumbno);
1074 xfs_trans_cancel(tp);
1078 * Mark more blocks free in the superblock.
1080 xfs_trans_mod_sb(tp, XFS_TRANS_SB_FREXTENTS,
1081 nsbp->sb_rextents - sbp->sb_rextents);
1083 * Update mp values into the real mp structure.
1085 mp->m_rsumlevels = nrsumlevels;
1086 mp->m_rsumsize = nrsumsize;
1088 error = xfs_trans_commit(tp);
1094 * Free the fake mp structure.
1102 * Allocate an extent in the realtime subvolume, with the usual allocation
1103 * parameters. The length units are all in realtime extents, as is the
1104 * result block number.
1107 xfs_rtallocate_extent(
1108 xfs_trans_t *tp, /* transaction pointer */
1109 xfs_rtblock_t bno, /* starting block number to allocate */
1110 xfs_extlen_t minlen, /* minimum length to allocate */
1111 xfs_extlen_t maxlen, /* maximum length to allocate */
1112 xfs_extlen_t *len, /* out: actual length allocated */
1113 xfs_alloctype_t type, /* allocation type XFS_ALLOCTYPE... */
1114 int wasdel, /* was a delayed allocation extent */
1115 xfs_extlen_t prod, /* extent product factor */
1116 xfs_rtblock_t *rtblock) /* out: start block allocated */
1118 xfs_mount_t *mp = tp->t_mountp;
1119 int error; /* error value */
1120 xfs_rtblock_t r; /* result allocated block */
1121 xfs_fsblock_t sb; /* summary file block number */
1122 xfs_buf_t *sumbp; /* summary file block buffer */
1124 ASSERT(xfs_isilocked(mp->m_rbmip, XFS_ILOCK_EXCL));
1125 ASSERT(minlen > 0 && minlen <= maxlen);
1128 * If prod is set then figure out what to do to minlen and maxlen.
1133 if ((i = maxlen % prod))
1135 if ((i = minlen % prod))
1137 if (maxlen < minlen) {
1138 *rtblock = NULLRTBLOCK;
1145 * Allocate by size, or near another block, or exactly at some block.
1148 case XFS_ALLOCTYPE_ANY_AG:
1149 error = xfs_rtallocate_extent_size(mp, tp, minlen, maxlen, len,
1150 &sumbp, &sb, prod, &r);
1152 case XFS_ALLOCTYPE_NEAR_BNO:
1153 error = xfs_rtallocate_extent_near(mp, tp, bno, minlen, maxlen,
1154 len, &sumbp, &sb, prod, &r);
1156 case XFS_ALLOCTYPE_THIS_BNO:
1157 error = xfs_rtallocate_extent_exact(mp, tp, bno, minlen, maxlen,
1158 len, &sumbp, &sb, prod, &r);
1168 * If it worked, update the superblock.
1170 if (r != NULLRTBLOCK) {
1171 long slen = (long)*len;
1173 ASSERT(*len >= minlen && *len <= maxlen);
1175 xfs_trans_mod_sb(tp, XFS_TRANS_SB_RES_FREXTENTS, -slen);
1177 xfs_trans_mod_sb(tp, XFS_TRANS_SB_FREXTENTS, -slen);
1184 * Initialize realtime fields in the mount structure.
1188 struct xfs_mount *mp) /* file system mount structure */
1190 struct xfs_buf *bp; /* buffer for last block of subvolume */
1191 struct xfs_sb *sbp; /* filesystem superblock copy in mount */
1192 xfs_daddr_t d; /* address of last block of subvolume */
1196 if (sbp->sb_rblocks == 0)
1198 if (mp->m_rtdev_targp == NULL) {
1200 "Filesystem has a realtime volume, use rtdev=device option");
1203 mp->m_rsumlevels = sbp->sb_rextslog + 1;
1205 (uint)sizeof(xfs_suminfo_t) * mp->m_rsumlevels *
1207 mp->m_rsumsize = roundup(mp->m_rsumsize, sbp->sb_blocksize);
1208 mp->m_rbmip = mp->m_rsumip = NULL;
1210 * Check that the realtime section is an ok size.
1212 d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_rblocks);
1213 if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_rblocks) {
1214 xfs_warn(mp, "realtime mount -- %llu != %llu",
1215 (unsigned long long) XFS_BB_TO_FSB(mp, d),
1216 (unsigned long long) mp->m_sb.sb_rblocks);
1219 error = xfs_buf_read_uncached(mp->m_rtdev_targp,
1220 d - XFS_FSB_TO_BB(mp, 1),
1221 XFS_FSB_TO_BB(mp, 1), 0, &bp, NULL);
1223 xfs_warn(mp, "realtime device size check failed");
1231 * Get the bitmap and summary inodes into the mount structure
1236 xfs_mount_t *mp) /* file system mount structure */
1238 int error; /* error return value */
1242 error = xfs_iget(mp, NULL, sbp->sb_rbmino, 0, 0, &mp->m_rbmip);
1245 ASSERT(mp->m_rbmip != NULL);
1247 error = xfs_iget(mp, NULL, sbp->sb_rsumino, 0, 0, &mp->m_rsumip);
1252 ASSERT(mp->m_rsumip != NULL);
1257 xfs_rtunmount_inodes(
1258 struct xfs_mount *mp)
1263 IRELE(mp->m_rsumip);
1267 * Pick an extent for allocation at the start of a new realtime file.
1268 * Use the sequence number stored in the atime field of the bitmap inode.
1269 * Translate this to a fraction of the rtextents, and return the product
1270 * of rtextents and the fraction.
1271 * The fraction sequence is 0, 1/2, 1/4, 3/4, 1/8, ..., 7/8, 1/16, ...
1275 xfs_mount_t *mp, /* file system mount point */
1276 xfs_trans_t *tp, /* transaction pointer */
1277 xfs_extlen_t len, /* allocation length (rtextents) */
1278 xfs_rtblock_t *pick) /* result rt extent */
1280 xfs_rtblock_t b; /* result block */
1281 int log2; /* log of sequence number */
1282 __uint64_t resid; /* residual after log removed */
1283 __uint64_t seq; /* sequence number of file creation */
1284 __uint64_t *seqp; /* pointer to seqno in inode */
1286 ASSERT(xfs_isilocked(mp->m_rbmip, XFS_ILOCK_EXCL));
1288 seqp = (__uint64_t *)&VFS_I(mp->m_rbmip)->i_atime;
1289 if (!(mp->m_rbmip->i_d.di_flags & XFS_DIFLAG_NEWRTBM)) {
1290 mp->m_rbmip->i_d.di_flags |= XFS_DIFLAG_NEWRTBM;
1294 if ((log2 = xfs_highbit64(seq)) == -1)
1297 resid = seq - (1ULL << log2);
1298 b = (mp->m_sb.sb_rextents * ((resid << 1) + 1ULL)) >>
1300 if (b >= mp->m_sb.sb_rextents)
1301 b = do_mod(b, mp->m_sb.sb_rextents);
1302 if (b + len > mp->m_sb.sb_rextents)
1303 b = mp->m_sb.sb_rextents - len;
1306 xfs_trans_log_inode(tp, mp->m_rbmip, XFS_ILOG_CORE);