Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / fs / xfs / xfs_rtalloc.c
blob06c19e59928478aee12a9e513b9a72c7be4bd617
1 /*
2 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
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
18 #include "xfs.h"
19 #include "xfs_fs.h"
20 #include "xfs_types.h"
21 #include "xfs_bit.h"
22 #include "xfs_log.h"
23 #include "xfs_inum.h"
24 #include "xfs_trans.h"
25 #include "xfs_sb.h"
26 #include "xfs_ag.h"
27 #include "xfs_dir2.h"
28 #include "xfs_dmapi.h"
29 #include "xfs_mount.h"
30 #include "xfs_bmap_btree.h"
31 #include "xfs_alloc_btree.h"
32 #include "xfs_ialloc_btree.h"
33 #include "xfs_dir2_sf.h"
34 #include "xfs_attr_sf.h"
35 #include "xfs_dinode.h"
36 #include "xfs_inode.h"
37 #include "xfs_btree.h"
38 #include "xfs_ialloc.h"
39 #include "xfs_alloc.h"
40 #include "xfs_bmap.h"
41 #include "xfs_rtalloc.h"
42 #include "xfs_fsops.h"
43 #include "xfs_error.h"
44 #include "xfs_rw.h"
45 #include "xfs_inode_item.h"
46 #include "xfs_trans_space.h"
50 * Prototypes for internal functions.
54 STATIC int xfs_rtallocate_range(xfs_mount_t *, xfs_trans_t *, xfs_rtblock_t,
55 xfs_extlen_t, xfs_buf_t **, xfs_fsblock_t *);
56 STATIC int xfs_rtany_summary(xfs_mount_t *, xfs_trans_t *, int, int,
57 xfs_rtblock_t, xfs_buf_t **, xfs_fsblock_t *, int *);
58 STATIC int xfs_rtcheck_range(xfs_mount_t *, xfs_trans_t *, xfs_rtblock_t,
59 xfs_extlen_t, int, xfs_rtblock_t *, int *);
60 STATIC int xfs_rtfind_back(xfs_mount_t *, xfs_trans_t *, xfs_rtblock_t,
61 xfs_rtblock_t, xfs_rtblock_t *);
62 STATIC int xfs_rtfind_forw(xfs_mount_t *, xfs_trans_t *, xfs_rtblock_t,
63 xfs_rtblock_t, xfs_rtblock_t *);
64 STATIC int xfs_rtget_summary( xfs_mount_t *, xfs_trans_t *, int,
65 xfs_rtblock_t, xfs_buf_t **, xfs_fsblock_t *, xfs_suminfo_t *);
66 STATIC int xfs_rtmodify_range(xfs_mount_t *, xfs_trans_t *, xfs_rtblock_t,
67 xfs_extlen_t, int);
68 STATIC int xfs_rtmodify_summary(xfs_mount_t *, xfs_trans_t *, int,
69 xfs_rtblock_t, int, xfs_buf_t **, xfs_fsblock_t *);
72 * Internal functions.
76 <<<<<<< HEAD:fs/xfs/xfs_rtalloc.c
77 =======
78 * xfs_lowbit32: get low bit set out of 32-bit argument, -1 if none set.
80 STATIC int
81 xfs_lowbit32(
82 __uint32_t v)
84 if (v)
85 return ffs(v) - 1;
86 return -1;
90 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:fs/xfs/xfs_rtalloc.c
91 * Allocate space to the bitmap or summary file, and zero it, for growfs.
93 STATIC int /* error */
94 xfs_growfs_rt_alloc(
95 xfs_mount_t *mp, /* file system mount point */
96 xfs_extlen_t oblocks, /* old count of blocks */
97 xfs_extlen_t nblocks, /* new count of blocks */
98 xfs_ino_t ino) /* inode number (bitmap/summary) */
100 xfs_fileoff_t bno; /* block number in file */
101 xfs_buf_t *bp; /* temporary buffer for zeroing */
102 int cancelflags; /* flags for xfs_trans_cancel */
103 int committed; /* transaction committed flag */
104 xfs_daddr_t d; /* disk block address */
105 int error; /* error return value */
106 xfs_fsblock_t firstblock; /* first block allocated in xaction */
107 xfs_bmap_free_t flist; /* list of freed blocks */
108 xfs_fsblock_t fsbno; /* filesystem block for bno */
109 xfs_inode_t *ip; /* pointer to incore inode */
110 xfs_bmbt_irec_t map; /* block map output */
111 int nmap; /* number of block maps */
112 int resblks; /* space reservation */
113 xfs_trans_t *tp; /* transaction pointer */
116 * Allocate space to the file, as necessary.
118 while (oblocks < nblocks) {
119 tp = xfs_trans_alloc(mp, XFS_TRANS_GROWFSRT_ALLOC);
120 resblks = XFS_GROWFSRT_SPACE_RES(mp, nblocks - oblocks);
121 cancelflags = 0;
123 * Reserve space & log for one extent added to the file.
125 if ((error = xfs_trans_reserve(tp, resblks,
126 XFS_GROWRTALLOC_LOG_RES(mp), 0,
127 XFS_TRANS_PERM_LOG_RES,
128 XFS_DEFAULT_PERM_LOG_COUNT)))
129 goto error_exit;
130 cancelflags = XFS_TRANS_RELEASE_LOG_RES;
132 * Lock the inode.
134 if ((error = xfs_trans_iget(mp, tp, ino, 0,
135 XFS_ILOCK_EXCL, &ip)))
136 goto error_exit;
137 XFS_BMAP_INIT(&flist, &firstblock);
139 * Allocate blocks to the bitmap file.
141 nmap = 1;
142 cancelflags |= XFS_TRANS_ABORT;
143 error = xfs_bmapi(tp, ip, oblocks, nblocks - oblocks,
144 XFS_BMAPI_WRITE | XFS_BMAPI_METADATA, &firstblock,
145 resblks, &map, &nmap, &flist, NULL);
146 if (!error && nmap < 1)
147 error = XFS_ERROR(ENOSPC);
148 if (error)
149 goto error_exit;
151 * Free any blocks freed up in the transaction, then commit.
153 error = xfs_bmap_finish(&tp, &flist, &committed);
154 if (error)
155 goto error_exit;
156 xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
158 * Now we need to clear the allocated blocks.
159 * Do this one block per transaction, to keep it simple.
161 cancelflags = 0;
162 for (bno = map.br_startoff, fsbno = map.br_startblock;
163 bno < map.br_startoff + map.br_blockcount;
164 bno++, fsbno++) {
165 tp = xfs_trans_alloc(mp, XFS_TRANS_GROWFSRT_ZERO);
167 * Reserve log for one block zeroing.
169 if ((error = xfs_trans_reserve(tp, 0,
170 XFS_GROWRTZERO_LOG_RES(mp), 0, 0, 0)))
171 goto error_exit;
173 * Lock the bitmap inode.
175 if ((error = xfs_trans_iget(mp, tp, ino, 0,
176 XFS_ILOCK_EXCL, &ip)))
177 goto error_exit;
179 * Get a buffer for the block.
181 d = XFS_FSB_TO_DADDR(mp, fsbno);
182 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
183 mp->m_bsize, 0);
184 if (bp == NULL) {
185 error = XFS_ERROR(EIO);
186 goto error_exit;
188 memset(XFS_BUF_PTR(bp), 0, mp->m_sb.sb_blocksize);
189 xfs_trans_log_buf(tp, bp, 0, mp->m_sb.sb_blocksize - 1);
191 * Commit the transaction.
193 xfs_trans_commit(tp, 0);
196 * Go on to the next extent, if any.
198 oblocks = map.br_startoff + map.br_blockcount;
200 return 0;
201 error_exit:
202 xfs_trans_cancel(tp, cancelflags);
203 return error;
207 * Attempt to allocate an extent minlen<=len<=maxlen starting from
208 * bitmap block bbno. If we don't get maxlen then use prod to trim
209 * the length, if given. Returns error; returns starting block in *rtblock.
210 * The lengths are all in rtextents.
212 STATIC int /* error */
213 xfs_rtallocate_extent_block(
214 xfs_mount_t *mp, /* file system mount point */
215 xfs_trans_t *tp, /* transaction pointer */
216 xfs_rtblock_t bbno, /* bitmap block number */
217 xfs_extlen_t minlen, /* minimum length to allocate */
218 xfs_extlen_t maxlen, /* maximum length to allocate */
219 xfs_extlen_t *len, /* out: actual length allocated */
220 xfs_rtblock_t *nextp, /* out: next block to try */
221 xfs_buf_t **rbpp, /* in/out: summary block buffer */
222 xfs_fsblock_t *rsb, /* in/out: summary block number */
223 xfs_extlen_t prod, /* extent product factor */
224 xfs_rtblock_t *rtblock) /* out: start block allocated */
226 xfs_rtblock_t besti; /* best rtblock found so far */
227 xfs_rtblock_t bestlen; /* best length found so far */
228 xfs_rtblock_t end; /* last rtblock in chunk */
229 int error; /* error value */
230 xfs_rtblock_t i; /* current rtblock trying */
231 xfs_rtblock_t next; /* next rtblock to try */
232 int stat; /* status from internal calls */
235 * Loop over all the extents starting in this bitmap block,
236 * looking for one that's long enough.
238 for (i = XFS_BLOCKTOBIT(mp, bbno), besti = -1, bestlen = 0,
239 end = XFS_BLOCKTOBIT(mp, bbno + 1) - 1;
240 i <= end;
241 i++) {
243 * See if there's a free extent of maxlen starting at i.
244 * If it's not so then next will contain the first non-free.
246 error = xfs_rtcheck_range(mp, tp, i, maxlen, 1, &next, &stat);
247 if (error) {
248 return error;
250 if (stat) {
252 * i for maxlen is all free, allocate and return that.
254 error = xfs_rtallocate_range(mp, tp, i, maxlen, rbpp,
255 rsb);
256 if (error) {
257 return error;
259 *len = maxlen;
260 *rtblock = i;
261 return 0;
264 * In the case where we have a variable-sized allocation
265 * request, figure out how big this free piece is,
266 * and if it's big enough for the minimum, and the best
267 * so far, remember it.
269 if (minlen < maxlen) {
270 xfs_rtblock_t thislen; /* this extent size */
272 thislen = next - i;
273 if (thislen >= minlen && thislen > bestlen) {
274 besti = i;
275 bestlen = thislen;
279 * If not done yet, find the start of the next free space.
281 if (next < end) {
282 error = xfs_rtfind_forw(mp, tp, next, end, &i);
283 if (error) {
284 return error;
286 } else
287 break;
290 * Searched the whole thing & didn't find a maxlen free extent.
292 if (minlen < maxlen && besti != -1) {
293 xfs_extlen_t p; /* amount to trim length by */
296 * If size should be a multiple of prod, make that so.
298 if (prod > 1 && (p = do_mod(bestlen, prod)))
299 bestlen -= p;
301 * Allocate besti for bestlen & return that.
303 error = xfs_rtallocate_range(mp, tp, besti, bestlen, rbpp, rsb);
304 if (error) {
305 return error;
307 *len = bestlen;
308 *rtblock = besti;
309 return 0;
312 * Allocation failed. Set *nextp to the next block to try.
314 *nextp = next;
315 *rtblock = NULLRTBLOCK;
316 return 0;
320 * Allocate an extent of length minlen<=len<=maxlen, starting at block
321 * bno. If we don't get maxlen then use prod to trim the length, if given.
322 * Returns error; returns starting block in *rtblock.
323 * The lengths are all in rtextents.
325 STATIC int /* error */
326 xfs_rtallocate_extent_exact(
327 xfs_mount_t *mp, /* file system mount point */
328 xfs_trans_t *tp, /* transaction pointer */
329 xfs_rtblock_t bno, /* starting block number to allocate */
330 xfs_extlen_t minlen, /* minimum length to allocate */
331 xfs_extlen_t maxlen, /* maximum length to allocate */
332 xfs_extlen_t *len, /* out: actual length allocated */
333 xfs_buf_t **rbpp, /* in/out: summary block buffer */
334 xfs_fsblock_t *rsb, /* in/out: summary block number */
335 xfs_extlen_t prod, /* extent product factor */
336 xfs_rtblock_t *rtblock) /* out: start block allocated */
338 int error; /* error value */
339 xfs_extlen_t i; /* extent length trimmed due to prod */
340 int isfree; /* extent is free */
341 xfs_rtblock_t next; /* next block to try (dummy) */
343 ASSERT(minlen % prod == 0 && maxlen % prod == 0);
345 * Check if the range in question (for maxlen) is free.
347 error = xfs_rtcheck_range(mp, tp, bno, maxlen, 1, &next, &isfree);
348 if (error) {
349 return error;
351 if (isfree) {
353 * If it is, allocate it and return success.
355 error = xfs_rtallocate_range(mp, tp, bno, maxlen, rbpp, rsb);
356 if (error) {
357 return error;
359 *len = maxlen;
360 *rtblock = bno;
361 return 0;
364 * If not, allocate what there is, if it's at least minlen.
366 maxlen = next - bno;
367 if (maxlen < minlen) {
369 * Failed, return failure status.
371 *rtblock = NULLRTBLOCK;
372 return 0;
375 * Trim off tail of extent, if prod is specified.
377 if (prod > 1 && (i = maxlen % prod)) {
378 maxlen -= i;
379 if (maxlen < minlen) {
381 * Now we can't do it, return failure status.
383 *rtblock = NULLRTBLOCK;
384 return 0;
388 * Allocate what we can and return it.
390 error = xfs_rtallocate_range(mp, tp, bno, maxlen, rbpp, rsb);
391 if (error) {
392 return error;
394 *len = maxlen;
395 *rtblock = bno;
396 return 0;
400 * Allocate an extent of length minlen<=len<=maxlen, starting as near
401 * to bno as possible. If we don't get maxlen then use prod to trim
402 * the length, if given. The lengths are all in rtextents.
404 STATIC int /* error */
405 xfs_rtallocate_extent_near(
406 xfs_mount_t *mp, /* file system mount point */
407 xfs_trans_t *tp, /* transaction pointer */
408 xfs_rtblock_t bno, /* starting block number to allocate */
409 xfs_extlen_t minlen, /* minimum length to allocate */
410 xfs_extlen_t maxlen, /* maximum length to allocate */
411 xfs_extlen_t *len, /* out: actual length allocated */
412 xfs_buf_t **rbpp, /* in/out: summary block buffer */
413 xfs_fsblock_t *rsb, /* in/out: summary block number */
414 xfs_extlen_t prod, /* extent product factor */
415 xfs_rtblock_t *rtblock) /* out: start block allocated */
417 int any; /* any useful extents from summary */
418 xfs_rtblock_t bbno; /* bitmap block number */
419 int error; /* error value */
420 int i; /* bitmap block offset (loop control) */
421 int j; /* secondary loop control */
422 int log2len; /* log2 of minlen */
423 xfs_rtblock_t n; /* next block to try */
424 xfs_rtblock_t r; /* result block */
426 ASSERT(minlen % prod == 0 && maxlen % prod == 0);
428 * If the block number given is off the end, silently set it to
429 * the last block.
431 if (bno >= mp->m_sb.sb_rextents)
432 bno = mp->m_sb.sb_rextents - 1;
434 * Try the exact allocation first.
436 error = xfs_rtallocate_extent_exact(mp, tp, bno, minlen, maxlen, len,
437 rbpp, rsb, prod, &r);
438 if (error) {
439 return error;
442 * If the exact allocation worked, return that.
444 if (r != NULLRTBLOCK) {
445 *rtblock = r;
446 return 0;
448 bbno = XFS_BITTOBLOCK(mp, bno);
449 i = 0;
450 <<<<<<< HEAD:fs/xfs/xfs_rtalloc.c
451 ASSERT(minlen != 0);
452 =======
453 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:fs/xfs/xfs_rtalloc.c
454 log2len = xfs_highbit32(minlen);
456 * Loop over all bitmap blocks (bbno + i is current block).
458 for (;;) {
460 * Get summary information of extents of all useful levels
461 * starting in this bitmap block.
463 error = xfs_rtany_summary(mp, tp, log2len, mp->m_rsumlevels - 1,
464 bbno + i, rbpp, rsb, &any);
465 if (error) {
466 return error;
469 * If there are any useful extents starting here, try
470 * allocating one.
472 if (any) {
474 * On the positive side of the starting location.
476 if (i >= 0) {
478 * Try to allocate an extent starting in
479 * this block.
481 error = xfs_rtallocate_extent_block(mp, tp,
482 bbno + i, minlen, maxlen, len, &n, rbpp,
483 rsb, prod, &r);
484 if (error) {
485 return error;
488 * If it worked, return it.
490 if (r != NULLRTBLOCK) {
491 *rtblock = r;
492 return 0;
496 * On the negative side of the starting location.
498 else { /* i < 0 */
500 * Loop backwards through the bitmap blocks from
501 * the starting point-1 up to where we are now.
502 * There should be an extent which ends in this
503 * bitmap block and is long enough.
505 for (j = -1; j > i; j--) {
507 * Grab the summary information for
508 * this bitmap block.
510 error = xfs_rtany_summary(mp, tp,
511 log2len, mp->m_rsumlevels - 1,
512 bbno + j, rbpp, rsb, &any);
513 if (error) {
514 return error;
517 * If there's no extent given in the
518 * summary that means the extent we
519 * found must carry over from an
520 * earlier block. If there is an
521 * extent given, we've already tried
522 * that allocation, don't do it again.
524 if (any)
525 continue;
526 error = xfs_rtallocate_extent_block(mp,
527 tp, bbno + j, minlen, maxlen,
528 len, &n, rbpp, rsb, prod, &r);
529 if (error) {
530 return error;
533 * If it works, return the extent.
535 if (r != NULLRTBLOCK) {
536 *rtblock = r;
537 return 0;
541 * There weren't intervening bitmap blocks
542 * with a long enough extent, or the
543 * allocation didn't work for some reason
544 * (i.e. it's a little * too short).
545 * Try to allocate from the summary block
546 * that we found.
548 error = xfs_rtallocate_extent_block(mp, tp,
549 bbno + i, minlen, maxlen, len, &n, rbpp,
550 rsb, prod, &r);
551 if (error) {
552 return error;
555 * If it works, return the extent.
557 if (r != NULLRTBLOCK) {
558 *rtblock = r;
559 return 0;
564 * Loop control. If we were on the positive side, and there's
565 * still more blocks on the negative side, go there.
567 if (i > 0 && (int)bbno - i >= 0)
568 i = -i;
570 * If positive, and no more negative, but there are more
571 * positive, go there.
573 else if (i > 0 && (int)bbno + i < mp->m_sb.sb_rbmblocks - 1)
574 i++;
576 * If negative or 0 (just started), and there are positive
577 * blocks to go, go there. The 0 case moves to block 1.
579 else if (i <= 0 && (int)bbno - i < mp->m_sb.sb_rbmblocks - 1)
580 i = 1 - i;
582 * If negative or 0 and there are more negative blocks,
583 * go there.
585 else if (i <= 0 && (int)bbno + i > 0)
586 i--;
588 * Must be done. Return failure.
590 else
591 break;
593 *rtblock = NULLRTBLOCK;
594 return 0;
598 * Allocate an extent of length minlen<=len<=maxlen, with no position
599 * specified. If we don't get maxlen then use prod to trim
600 * the length, if given. The lengths are all in rtextents.
602 STATIC int /* error */
603 xfs_rtallocate_extent_size(
604 xfs_mount_t *mp, /* file system mount point */
605 xfs_trans_t *tp, /* transaction pointer */
606 xfs_extlen_t minlen, /* minimum length to allocate */
607 xfs_extlen_t maxlen, /* maximum length to allocate */
608 xfs_extlen_t *len, /* out: actual length allocated */
609 xfs_buf_t **rbpp, /* in/out: summary block buffer */
610 xfs_fsblock_t *rsb, /* in/out: summary block number */
611 xfs_extlen_t prod, /* extent product factor */
612 xfs_rtblock_t *rtblock) /* out: start block allocated */
614 int error; /* error value */
615 int i; /* bitmap block number */
616 int l; /* level number (loop control) */
617 xfs_rtblock_t n; /* next block to be tried */
618 xfs_rtblock_t r; /* result block number */
619 xfs_suminfo_t sum; /* summary information for extents */
621 ASSERT(minlen % prod == 0 && maxlen % prod == 0);
622 <<<<<<< HEAD:fs/xfs/xfs_rtalloc.c
623 ASSERT(maxlen != 0);
625 =======
626 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:fs/xfs/xfs_rtalloc.c
628 * Loop over all the levels starting with maxlen.
629 * At each level, look at all the bitmap blocks, to see if there
630 * are extents starting there that are long enough (>= maxlen).
631 * Note, only on the initial level can the allocation fail if
632 * the summary says there's an extent.
634 for (l = xfs_highbit32(maxlen); l < mp->m_rsumlevels; l++) {
636 * Loop over all the bitmap blocks.
638 for (i = 0; i < mp->m_sb.sb_rbmblocks; i++) {
640 * Get the summary for this level/block.
642 error = xfs_rtget_summary(mp, tp, l, i, rbpp, rsb,
643 &sum);
644 if (error) {
645 return error;
648 * Nothing there, on to the next block.
650 if (!sum)
651 continue;
653 * Try allocating the extent.
655 error = xfs_rtallocate_extent_block(mp, tp, i, maxlen,
656 maxlen, len, &n, rbpp, rsb, prod, &r);
657 if (error) {
658 return error;
661 * If it worked, return that.
663 if (r != NULLRTBLOCK) {
664 *rtblock = r;
665 return 0;
668 * If the "next block to try" returned from the
669 * allocator is beyond the next bitmap block,
670 * skip to that bitmap block.
672 if (XFS_BITTOBLOCK(mp, n) > i + 1)
673 i = XFS_BITTOBLOCK(mp, n) - 1;
677 * Didn't find any maxlen blocks. Try smaller ones, unless
678 * we're asking for a fixed size extent.
680 if (minlen > --maxlen) {
681 *rtblock = NULLRTBLOCK;
682 return 0;
684 <<<<<<< HEAD:fs/xfs/xfs_rtalloc.c
685 ASSERT(minlen != 0);
686 ASSERT(maxlen != 0);
688 =======
689 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:fs/xfs/xfs_rtalloc.c
691 * Loop over sizes, from maxlen down to minlen.
692 * This time, when we do the allocations, allow smaller ones
693 * to succeed.
695 for (l = xfs_highbit32(maxlen); l >= xfs_highbit32(minlen); l--) {
697 * Loop over all the bitmap blocks, try an allocation
698 * starting in that block.
700 for (i = 0; i < mp->m_sb.sb_rbmblocks; i++) {
702 * Get the summary information for this level/block.
704 error = xfs_rtget_summary(mp, tp, l, i, rbpp, rsb,
705 &sum);
706 if (error) {
707 return error;
710 * If nothing there, go on to next.
712 if (!sum)
713 continue;
715 * Try the allocation. Make sure the specified
716 * minlen/maxlen are in the possible range for
717 * this summary level.
719 error = xfs_rtallocate_extent_block(mp, tp, i,
720 XFS_RTMAX(minlen, 1 << l),
721 XFS_RTMIN(maxlen, (1 << (l + 1)) - 1),
722 len, &n, rbpp, rsb, prod, &r);
723 if (error) {
724 return error;
727 * If it worked, return that extent.
729 if (r != NULLRTBLOCK) {
730 *rtblock = r;
731 return 0;
734 * If the "next block to try" returned from the
735 * allocator is beyond the next bitmap block,
736 * skip to that bitmap block.
738 if (XFS_BITTOBLOCK(mp, n) > i + 1)
739 i = XFS_BITTOBLOCK(mp, n) - 1;
743 * Got nothing, return failure.
745 *rtblock = NULLRTBLOCK;
746 return 0;
750 * Mark an extent specified by start and len allocated.
751 * Updates all the summary information as well as the bitmap.
753 STATIC int /* error */
754 xfs_rtallocate_range(
755 xfs_mount_t *mp, /* file system mount point */
756 xfs_trans_t *tp, /* transaction pointer */
757 xfs_rtblock_t start, /* start block to allocate */
758 xfs_extlen_t len, /* length to allocate */
759 xfs_buf_t **rbpp, /* in/out: summary block buffer */
760 xfs_fsblock_t *rsb) /* in/out: summary block number */
762 xfs_rtblock_t end; /* end of the allocated extent */
763 int error; /* error value */
764 xfs_rtblock_t postblock; /* first block allocated > end */
765 xfs_rtblock_t preblock; /* first block allocated < start */
767 end = start + len - 1;
769 * Assume we're allocating out of the middle of a free extent.
770 * We need to find the beginning and end of the extent so we can
771 * properly update the summary.
773 error = xfs_rtfind_back(mp, tp, start, 0, &preblock);
774 if (error) {
775 return error;
778 * Find the next allocated block (end of free extent).
780 error = xfs_rtfind_forw(mp, tp, end, mp->m_sb.sb_rextents - 1,
781 &postblock);
782 if (error) {
783 return error;
786 * Decrement the summary information corresponding to the entire
787 * (old) free extent.
789 error = xfs_rtmodify_summary(mp, tp,
790 XFS_RTBLOCKLOG(postblock + 1 - preblock),
791 XFS_BITTOBLOCK(mp, preblock), -1, rbpp, rsb);
792 if (error) {
793 return error;
796 * If there are blocks not being allocated at the front of the
797 * old extent, add summary data for them to be free.
799 if (preblock < start) {
800 error = xfs_rtmodify_summary(mp, tp,
801 XFS_RTBLOCKLOG(start - preblock),
802 XFS_BITTOBLOCK(mp, preblock), 1, rbpp, rsb);
803 if (error) {
804 return error;
808 * If there are blocks not being allocated at the end of the
809 * old extent, add summary data for them to be free.
811 if (postblock > end) {
812 error = xfs_rtmodify_summary(mp, tp,
813 XFS_RTBLOCKLOG(postblock - end),
814 XFS_BITTOBLOCK(mp, end + 1), 1, rbpp, rsb);
815 if (error) {
816 return error;
820 * Modify the bitmap to mark this extent allocated.
822 error = xfs_rtmodify_range(mp, tp, start, len, 0);
823 return error;
827 * Return whether there are any free extents in the size range given
828 * by low and high, for the bitmap block bbno.
830 STATIC int /* error */
831 xfs_rtany_summary(
832 xfs_mount_t *mp, /* file system mount structure */
833 xfs_trans_t *tp, /* transaction pointer */
834 int low, /* low log2 extent size */
835 int high, /* high log2 extent size */
836 xfs_rtblock_t bbno, /* bitmap block number */
837 xfs_buf_t **rbpp, /* in/out: summary block buffer */
838 xfs_fsblock_t *rsb, /* in/out: summary block number */
839 int *stat) /* out: any good extents here? */
841 int error; /* error value */
842 int log; /* loop counter, log2 of ext. size */
843 xfs_suminfo_t sum; /* summary data */
846 * Loop over logs of extent sizes. Order is irrelevant.
848 for (log = low; log <= high; log++) {
850 * Get one summary datum.
852 error = xfs_rtget_summary(mp, tp, log, bbno, rbpp, rsb, &sum);
853 if (error) {
854 return error;
857 * If there are any, return success.
859 if (sum) {
860 *stat = 1;
861 return 0;
865 * Found nothing, return failure.
867 *stat = 0;
868 return 0;
872 * Get a buffer for the bitmap or summary file block specified.
873 * The buffer is returned read and locked.
875 STATIC int /* error */
876 xfs_rtbuf_get(
877 xfs_mount_t *mp, /* file system mount structure */
878 xfs_trans_t *tp, /* transaction pointer */
879 xfs_rtblock_t block, /* block number in bitmap or summary */
880 int issum, /* is summary not bitmap */
881 xfs_buf_t **bpp) /* output: buffer for the block */
883 xfs_buf_t *bp; /* block buffer, result */
884 xfs_daddr_t d; /* disk addr of block */
885 int error; /* error value */
886 xfs_fsblock_t fsb; /* fs block number for block */
887 xfs_inode_t *ip; /* bitmap or summary inode */
889 ip = issum ? mp->m_rsumip : mp->m_rbmip;
891 * Map from the file offset (block) and inode number to the
892 * file system block.
894 error = xfs_bmapi_single(tp, ip, XFS_DATA_FORK, &fsb, block);
895 if (error) {
896 return error;
898 ASSERT(fsb != NULLFSBLOCK);
900 * Convert to disk address for buffer cache.
902 d = XFS_FSB_TO_DADDR(mp, fsb);
904 * Read the buffer.
906 error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, d,
907 mp->m_bsize, 0, &bp);
908 if (error) {
909 return error;
911 ASSERT(bp && !XFS_BUF_GETERROR(bp));
912 *bpp = bp;
913 return 0;
916 #ifdef DEBUG
918 * Check that the given extent (block range) is allocated already.
920 STATIC int /* error */
921 xfs_rtcheck_alloc_range(
922 xfs_mount_t *mp, /* file system mount point */
923 xfs_trans_t *tp, /* transaction pointer */
924 xfs_rtblock_t bno, /* starting block number of extent */
925 xfs_extlen_t len, /* length of extent */
926 int *stat) /* out: 1 for allocated, 0 for not */
928 xfs_rtblock_t new; /* dummy for xfs_rtcheck_range */
930 return xfs_rtcheck_range(mp, tp, bno, len, 0, &new, stat);
932 #endif
935 * Check that the given range is either all allocated (val = 0) or
936 * all free (val = 1).
938 STATIC int /* error */
939 xfs_rtcheck_range(
940 xfs_mount_t *mp, /* file system mount point */
941 xfs_trans_t *tp, /* transaction pointer */
942 xfs_rtblock_t start, /* starting block number of extent */
943 xfs_extlen_t len, /* length of extent */
944 int val, /* 1 for free, 0 for allocated */
945 xfs_rtblock_t *new, /* out: first block not matching */
946 int *stat) /* out: 1 for matches, 0 for not */
948 xfs_rtword_t *b; /* current word in buffer */
949 int bit; /* bit number in the word */
950 xfs_rtblock_t block; /* bitmap block number */
951 xfs_buf_t *bp; /* buf for the block */
952 xfs_rtword_t *bufp; /* starting word in buffer */
953 int error; /* error value */
954 xfs_rtblock_t i; /* current bit number rel. to start */
955 xfs_rtblock_t lastbit; /* last useful bit in word */
956 xfs_rtword_t mask; /* mask of relevant bits for value */
957 xfs_rtword_t wdiff; /* difference from wanted value */
958 int word; /* word number in the buffer */
961 * Compute starting bitmap block number
963 block = XFS_BITTOBLOCK(mp, start);
965 * Read the bitmap block.
967 error = xfs_rtbuf_get(mp, tp, block, 0, &bp);
968 if (error) {
969 return error;
971 bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
973 * Compute the starting word's address, and starting bit.
975 word = XFS_BITTOWORD(mp, start);
976 b = &bufp[word];
977 bit = (int)(start & (XFS_NBWORD - 1));
979 * 0 (allocated) => all zero's; 1 (free) => all one's.
981 val = -val;
983 * If not starting on a word boundary, deal with the first
984 * (partial) word.
986 if (bit) {
988 * Compute first bit not examined.
990 lastbit = XFS_RTMIN(bit + len, XFS_NBWORD);
992 * Mask of relevant bits.
994 mask = (((xfs_rtword_t)1 << (lastbit - bit)) - 1) << bit;
996 * Compute difference between actual and desired value.
998 if ((wdiff = (*b ^ val) & mask)) {
1000 * Different, compute first wrong bit and return.
1002 xfs_trans_brelse(tp, bp);
1003 i = XFS_RTLOBIT(wdiff) - bit;
1004 *new = start + i;
1005 *stat = 0;
1006 return 0;
1008 i = lastbit - bit;
1010 * Go on to next block if that's where the next word is
1011 * and we need the next word.
1013 if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
1015 * If done with this block, get the next one.
1017 xfs_trans_brelse(tp, bp);
1018 error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
1019 if (error) {
1020 return error;
1022 b = bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1023 word = 0;
1024 } else {
1026 * Go on to the next word in the buffer.
1028 b++;
1030 } else {
1032 * Starting on a word boundary, no partial word.
1034 i = 0;
1037 * Loop over whole words in buffers. When we use up one buffer
1038 * we move on to the next one.
1040 while (len - i >= XFS_NBWORD) {
1042 * Compute difference between actual and desired value.
1044 if ((wdiff = *b ^ val)) {
1046 * Different, compute first wrong bit and return.
1048 xfs_trans_brelse(tp, bp);
1049 i += XFS_RTLOBIT(wdiff);
1050 *new = start + i;
1051 *stat = 0;
1052 return 0;
1054 i += XFS_NBWORD;
1056 * Go on to next block if that's where the next word is
1057 * and we need the next word.
1059 if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
1061 * If done with this block, get the next one.
1063 xfs_trans_brelse(tp, bp);
1064 error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
1065 if (error) {
1066 return error;
1068 b = bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1069 word = 0;
1070 } else {
1072 * Go on to the next word in the buffer.
1074 b++;
1078 * If not ending on a word boundary, deal with the last
1079 * (partial) word.
1081 if ((lastbit = len - i)) {
1083 * Mask of relevant bits.
1085 mask = ((xfs_rtword_t)1 << lastbit) - 1;
1087 * Compute difference between actual and desired value.
1089 if ((wdiff = (*b ^ val) & mask)) {
1091 * Different, compute first wrong bit and return.
1093 xfs_trans_brelse(tp, bp);
1094 i += XFS_RTLOBIT(wdiff);
1095 *new = start + i;
1096 *stat = 0;
1097 return 0;
1098 } else
1099 i = len;
1102 * Successful, return.
1104 xfs_trans_brelse(tp, bp);
1105 *new = start + i;
1106 *stat = 1;
1107 return 0;
1111 * Copy and transform the summary file, given the old and new
1112 * parameters in the mount structures.
1114 STATIC int /* error */
1115 xfs_rtcopy_summary(
1116 xfs_mount_t *omp, /* old file system mount point */
1117 xfs_mount_t *nmp, /* new file system mount point */
1118 xfs_trans_t *tp) /* transaction pointer */
1120 xfs_rtblock_t bbno; /* bitmap block number */
1121 xfs_buf_t *bp; /* summary buffer */
1122 int error; /* error return value */
1123 int log; /* summary level number (log length) */
1124 xfs_suminfo_t sum; /* summary data */
1125 xfs_fsblock_t sumbno; /* summary block number */
1127 bp = NULL;
1128 for (log = omp->m_rsumlevels - 1; log >= 0; log--) {
1129 for (bbno = omp->m_sb.sb_rbmblocks - 1;
1130 (xfs_srtblock_t)bbno >= 0;
1131 bbno--) {
1132 error = xfs_rtget_summary(omp, tp, log, bbno, &bp,
1133 &sumbno, &sum);
1134 if (error)
1135 return error;
1136 if (sum == 0)
1137 continue;
1138 error = xfs_rtmodify_summary(omp, tp, log, bbno, -sum,
1139 &bp, &sumbno);
1140 if (error)
1141 return error;
1142 error = xfs_rtmodify_summary(nmp, tp, log, bbno, sum,
1143 &bp, &sumbno);
1144 if (error)
1145 return error;
1146 ASSERT(sum > 0);
1149 return 0;
1153 * Searching backward from start to limit, find the first block whose
1154 * allocated/free state is different from start's.
1156 STATIC int /* error */
1157 xfs_rtfind_back(
1158 xfs_mount_t *mp, /* file system mount point */
1159 xfs_trans_t *tp, /* transaction pointer */
1160 xfs_rtblock_t start, /* starting block to look at */
1161 xfs_rtblock_t limit, /* last block to look at */
1162 xfs_rtblock_t *rtblock) /* out: start block found */
1164 xfs_rtword_t *b; /* current word in buffer */
1165 int bit; /* bit number in the word */
1166 xfs_rtblock_t block; /* bitmap block number */
1167 xfs_buf_t *bp; /* buf for the block */
1168 xfs_rtword_t *bufp; /* starting word in buffer */
1169 int error; /* error value */
1170 xfs_rtblock_t firstbit; /* first useful bit in the word */
1171 xfs_rtblock_t i; /* current bit number rel. to start */
1172 xfs_rtblock_t len; /* length of inspected area */
1173 xfs_rtword_t mask; /* mask of relevant bits for value */
1174 xfs_rtword_t want; /* mask for "good" values */
1175 xfs_rtword_t wdiff; /* difference from wanted value */
1176 int word; /* word number in the buffer */
1179 * Compute and read in starting bitmap block for starting block.
1181 block = XFS_BITTOBLOCK(mp, start);
1182 error = xfs_rtbuf_get(mp, tp, block, 0, &bp);
1183 if (error) {
1184 return error;
1186 bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1188 * Get the first word's index & point to it.
1190 word = XFS_BITTOWORD(mp, start);
1191 b = &bufp[word];
1192 bit = (int)(start & (XFS_NBWORD - 1));
1193 len = start - limit + 1;
1195 * Compute match value, based on the bit at start: if 1 (free)
1196 * then all-ones, else all-zeroes.
1198 want = (*b & ((xfs_rtword_t)1 << bit)) ? -1 : 0;
1200 * If the starting position is not word-aligned, deal with the
1201 * partial word.
1203 if (bit < XFS_NBWORD - 1) {
1205 * Calculate first (leftmost) bit number to look at,
1206 * and mask for all the relevant bits in this word.
1208 firstbit = XFS_RTMAX((xfs_srtblock_t)(bit - len + 1), 0);
1209 mask = (((xfs_rtword_t)1 << (bit - firstbit + 1)) - 1) <<
1210 firstbit;
1212 * Calculate the difference between the value there
1213 * and what we're looking for.
1215 if ((wdiff = (*b ^ want) & mask)) {
1217 * Different. Mark where we are and return.
1219 xfs_trans_brelse(tp, bp);
1220 i = bit - XFS_RTHIBIT(wdiff);
1221 *rtblock = start - i + 1;
1222 return 0;
1224 i = bit - firstbit + 1;
1226 * Go on to previous block if that's where the previous word is
1227 * and we need the previous word.
1229 if (--word == -1 && i < len) {
1231 * If done with this block, get the previous one.
1233 xfs_trans_brelse(tp, bp);
1234 error = xfs_rtbuf_get(mp, tp, --block, 0, &bp);
1235 if (error) {
1236 return error;
1238 bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1239 word = XFS_BLOCKWMASK(mp);
1240 b = &bufp[word];
1241 } else {
1243 * Go on to the previous word in the buffer.
1245 b--;
1247 } else {
1249 * Starting on a word boundary, no partial word.
1251 i = 0;
1254 * Loop over whole words in buffers. When we use up one buffer
1255 * we move on to the previous one.
1257 while (len - i >= XFS_NBWORD) {
1259 * Compute difference between actual and desired value.
1261 if ((wdiff = *b ^ want)) {
1263 * Different, mark where we are and return.
1265 xfs_trans_brelse(tp, bp);
1266 i += XFS_NBWORD - 1 - XFS_RTHIBIT(wdiff);
1267 *rtblock = start - i + 1;
1268 return 0;
1270 i += XFS_NBWORD;
1272 * Go on to previous block if that's where the previous word is
1273 * and we need the previous word.
1275 if (--word == -1 && i < len) {
1277 * If done with this block, get the previous one.
1279 xfs_trans_brelse(tp, bp);
1280 error = xfs_rtbuf_get(mp, tp, --block, 0, &bp);
1281 if (error) {
1282 return error;
1284 bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1285 word = XFS_BLOCKWMASK(mp);
1286 b = &bufp[word];
1287 } else {
1289 * Go on to the previous word in the buffer.
1291 b--;
1295 * If not ending on a word boundary, deal with the last
1296 * (partial) word.
1298 if (len - i) {
1300 * Calculate first (leftmost) bit number to look at,
1301 * and mask for all the relevant bits in this word.
1303 firstbit = XFS_NBWORD - (len - i);
1304 mask = (((xfs_rtword_t)1 << (len - i)) - 1) << firstbit;
1306 * Compute difference between actual and desired value.
1308 if ((wdiff = (*b ^ want) & mask)) {
1310 * Different, mark where we are and return.
1312 xfs_trans_brelse(tp, bp);
1313 i += XFS_NBWORD - 1 - XFS_RTHIBIT(wdiff);
1314 *rtblock = start - i + 1;
1315 return 0;
1316 } else
1317 i = len;
1320 * No match, return that we scanned the whole area.
1322 xfs_trans_brelse(tp, bp);
1323 *rtblock = start - i + 1;
1324 return 0;
1328 * Searching forward from start to limit, find the first block whose
1329 * allocated/free state is different from start's.
1331 STATIC int /* error */
1332 xfs_rtfind_forw(
1333 xfs_mount_t *mp, /* file system mount point */
1334 xfs_trans_t *tp, /* transaction pointer */
1335 xfs_rtblock_t start, /* starting block to look at */
1336 xfs_rtblock_t limit, /* last block to look at */
1337 xfs_rtblock_t *rtblock) /* out: start block found */
1339 xfs_rtword_t *b; /* current word in buffer */
1340 int bit; /* bit number in the word */
1341 xfs_rtblock_t block; /* bitmap block number */
1342 xfs_buf_t *bp; /* buf for the block */
1343 xfs_rtword_t *bufp; /* starting word in buffer */
1344 int error; /* error value */
1345 xfs_rtblock_t i; /* current bit number rel. to start */
1346 xfs_rtblock_t lastbit; /* last useful bit in the word */
1347 xfs_rtblock_t len; /* length of inspected area */
1348 xfs_rtword_t mask; /* mask of relevant bits for value */
1349 xfs_rtword_t want; /* mask for "good" values */
1350 xfs_rtword_t wdiff; /* difference from wanted value */
1351 int word; /* word number in the buffer */
1354 * Compute and read in starting bitmap block for starting block.
1356 block = XFS_BITTOBLOCK(mp, start);
1357 error = xfs_rtbuf_get(mp, tp, block, 0, &bp);
1358 if (error) {
1359 return error;
1361 bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1363 * Get the first word's index & point to it.
1365 word = XFS_BITTOWORD(mp, start);
1366 b = &bufp[word];
1367 bit = (int)(start & (XFS_NBWORD - 1));
1368 len = limit - start + 1;
1370 * Compute match value, based on the bit at start: if 1 (free)
1371 * then all-ones, else all-zeroes.
1373 want = (*b & ((xfs_rtword_t)1 << bit)) ? -1 : 0;
1375 * If the starting position is not word-aligned, deal with the
1376 * partial word.
1378 if (bit) {
1380 * Calculate last (rightmost) bit number to look at,
1381 * and mask for all the relevant bits in this word.
1383 lastbit = XFS_RTMIN(bit + len, XFS_NBWORD);
1384 mask = (((xfs_rtword_t)1 << (lastbit - bit)) - 1) << bit;
1386 * Calculate the difference between the value there
1387 * and what we're looking for.
1389 if ((wdiff = (*b ^ want) & mask)) {
1391 * Different. Mark where we are and return.
1393 xfs_trans_brelse(tp, bp);
1394 i = XFS_RTLOBIT(wdiff) - bit;
1395 *rtblock = start + i - 1;
1396 return 0;
1398 i = lastbit - bit;
1400 * Go on to next block if that's where the next word is
1401 * and we need the next word.
1403 if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
1405 * If done with this block, get the previous one.
1407 xfs_trans_brelse(tp, bp);
1408 error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
1409 if (error) {
1410 return error;
1412 b = bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1413 word = 0;
1414 } else {
1416 * Go on to the previous word in the buffer.
1418 b++;
1420 } else {
1422 * Starting on a word boundary, no partial word.
1424 i = 0;
1427 * Loop over whole words in buffers. When we use up one buffer
1428 * we move on to the next one.
1430 while (len - i >= XFS_NBWORD) {
1432 * Compute difference between actual and desired value.
1434 if ((wdiff = *b ^ want)) {
1436 * Different, mark where we are and return.
1438 xfs_trans_brelse(tp, bp);
1439 i += XFS_RTLOBIT(wdiff);
1440 *rtblock = start + i - 1;
1441 return 0;
1443 i += XFS_NBWORD;
1445 * Go on to next block if that's where the next word is
1446 * and we need the next word.
1448 if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
1450 * If done with this block, get the next one.
1452 xfs_trans_brelse(tp, bp);
1453 error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
1454 if (error) {
1455 return error;
1457 b = bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1458 word = 0;
1459 } else {
1461 * Go on to the next word in the buffer.
1463 b++;
1467 * If not ending on a word boundary, deal with the last
1468 * (partial) word.
1470 if ((lastbit = len - i)) {
1472 * Calculate mask for all the relevant bits in this word.
1474 mask = ((xfs_rtword_t)1 << lastbit) - 1;
1476 * Compute difference between actual and desired value.
1478 if ((wdiff = (*b ^ want) & mask)) {
1480 * Different, mark where we are and return.
1482 xfs_trans_brelse(tp, bp);
1483 i += XFS_RTLOBIT(wdiff);
1484 *rtblock = start + i - 1;
1485 return 0;
1486 } else
1487 i = len;
1490 * No match, return that we scanned the whole area.
1492 xfs_trans_brelse(tp, bp);
1493 *rtblock = start + i - 1;
1494 return 0;
1498 * Mark an extent specified by start and len freed.
1499 * Updates all the summary information as well as the bitmap.
1501 STATIC int /* error */
1502 xfs_rtfree_range(
1503 xfs_mount_t *mp, /* file system mount point */
1504 xfs_trans_t *tp, /* transaction pointer */
1505 xfs_rtblock_t start, /* starting block to free */
1506 xfs_extlen_t len, /* length to free */
1507 xfs_buf_t **rbpp, /* in/out: summary block buffer */
1508 xfs_fsblock_t *rsb) /* in/out: summary block number */
1510 xfs_rtblock_t end; /* end of the freed extent */
1511 int error; /* error value */
1512 xfs_rtblock_t postblock; /* first block freed > end */
1513 xfs_rtblock_t preblock; /* first block freed < start */
1515 end = start + len - 1;
1517 * Modify the bitmap to mark this extent freed.
1519 error = xfs_rtmodify_range(mp, tp, start, len, 1);
1520 if (error) {
1521 return error;
1524 * Assume we're freeing out of the middle of an allocated extent.
1525 * We need to find the beginning and end of the extent so we can
1526 * properly update the summary.
1528 error = xfs_rtfind_back(mp, tp, start, 0, &preblock);
1529 if (error) {
1530 return error;
1533 * Find the next allocated block (end of allocated extent).
1535 error = xfs_rtfind_forw(mp, tp, end, mp->m_sb.sb_rextents - 1,
1536 &postblock);
1538 * If there are blocks not being freed at the front of the
1539 * old extent, add summary data for them to be allocated.
1541 if (preblock < start) {
1542 error = xfs_rtmodify_summary(mp, tp,
1543 XFS_RTBLOCKLOG(start - preblock),
1544 XFS_BITTOBLOCK(mp, preblock), -1, rbpp, rsb);
1545 if (error) {
1546 return error;
1550 * If there are blocks not being freed at the end of the
1551 * old extent, add summary data for them to be allocated.
1553 if (postblock > end) {
1554 error = xfs_rtmodify_summary(mp, tp,
1555 XFS_RTBLOCKLOG(postblock - end),
1556 XFS_BITTOBLOCK(mp, end + 1), -1, rbpp, rsb);
1557 if (error) {
1558 return error;
1562 * Increment the summary information corresponding to the entire
1563 * (new) free extent.
1565 error = xfs_rtmodify_summary(mp, tp,
1566 XFS_RTBLOCKLOG(postblock + 1 - preblock),
1567 XFS_BITTOBLOCK(mp, preblock), 1, rbpp, rsb);
1568 return error;
1572 * Read and return the summary information for a given extent size,
1573 * bitmap block combination.
1574 * Keeps track of a current summary block, so we don't keep reading
1575 * it from the buffer cache.
1577 STATIC int /* error */
1578 xfs_rtget_summary(
1579 xfs_mount_t *mp, /* file system mount structure */
1580 xfs_trans_t *tp, /* transaction pointer */
1581 int log, /* log2 of extent size */
1582 xfs_rtblock_t bbno, /* bitmap block number */
1583 xfs_buf_t **rbpp, /* in/out: summary block buffer */
1584 xfs_fsblock_t *rsb, /* in/out: summary block number */
1585 xfs_suminfo_t *sum) /* out: summary info for this block */
1587 xfs_buf_t *bp; /* buffer for summary block */
1588 int error; /* error value */
1589 xfs_fsblock_t sb; /* summary fsblock */
1590 int so; /* index into the summary file */
1591 xfs_suminfo_t *sp; /* pointer to returned data */
1594 * Compute entry number in the summary file.
1596 so = XFS_SUMOFFS(mp, log, bbno);
1598 * Compute the block number in the summary file.
1600 sb = XFS_SUMOFFSTOBLOCK(mp, so);
1602 * If we have an old buffer, and the block number matches, use that.
1604 if (rbpp && *rbpp && *rsb == sb)
1605 bp = *rbpp;
1607 * Otherwise we have to get the buffer.
1609 else {
1611 * If there was an old one, get rid of it first.
1613 if (rbpp && *rbpp)
1614 xfs_trans_brelse(tp, *rbpp);
1615 error = xfs_rtbuf_get(mp, tp, sb, 1, &bp);
1616 if (error) {
1617 return error;
1620 * Remember this buffer and block for the next call.
1622 if (rbpp) {
1623 *rbpp = bp;
1624 *rsb = sb;
1628 * Point to the summary information & copy it out.
1630 sp = XFS_SUMPTR(mp, bp, so);
1631 *sum = *sp;
1633 * Drop the buffer if we're not asked to remember it.
1635 if (!rbpp)
1636 xfs_trans_brelse(tp, bp);
1637 return 0;
1641 * Set the given range of bitmap bits to the given value.
1642 * Do whatever I/O and logging is required.
1644 STATIC int /* error */
1645 xfs_rtmodify_range(
1646 xfs_mount_t *mp, /* file system mount point */
1647 xfs_trans_t *tp, /* transaction pointer */
1648 xfs_rtblock_t start, /* starting block to modify */
1649 xfs_extlen_t len, /* length of extent to modify */
1650 int val) /* 1 for free, 0 for allocated */
1652 xfs_rtword_t *b; /* current word in buffer */
1653 int bit; /* bit number in the word */
1654 xfs_rtblock_t block; /* bitmap block number */
1655 xfs_buf_t *bp; /* buf for the block */
1656 xfs_rtword_t *bufp; /* starting word in buffer */
1657 int error; /* error value */
1658 xfs_rtword_t *first; /* first used word in the buffer */
1659 int i; /* current bit number rel. to start */
1660 int lastbit; /* last useful bit in word */
1661 xfs_rtword_t mask; /* mask o frelevant bits for value */
1662 int word; /* word number in the buffer */
1665 * Compute starting bitmap block number.
1667 block = XFS_BITTOBLOCK(mp, start);
1669 * Read the bitmap block, and point to its data.
1671 error = xfs_rtbuf_get(mp, tp, block, 0, &bp);
1672 if (error) {
1673 return error;
1675 bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1677 * Compute the starting word's address, and starting bit.
1679 word = XFS_BITTOWORD(mp, start);
1680 first = b = &bufp[word];
1681 bit = (int)(start & (XFS_NBWORD - 1));
1683 * 0 (allocated) => all zeroes; 1 (free) => all ones.
1685 val = -val;
1687 * If not starting on a word boundary, deal with the first
1688 * (partial) word.
1690 if (bit) {
1692 * Compute first bit not changed and mask of relevant bits.
1694 lastbit = XFS_RTMIN(bit + len, XFS_NBWORD);
1695 mask = (((xfs_rtword_t)1 << (lastbit - bit)) - 1) << bit;
1697 * Set/clear the active bits.
1699 if (val)
1700 *b |= mask;
1701 else
1702 *b &= ~mask;
1703 i = lastbit - bit;
1705 * Go on to the next block if that's where the next word is
1706 * and we need the next word.
1708 if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
1710 * Log the changed part of this block.
1711 * Get the next one.
1713 xfs_trans_log_buf(tp, bp,
1714 (uint)((char *)first - (char *)bufp),
1715 (uint)((char *)b - (char *)bufp));
1716 error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
1717 if (error) {
1718 return error;
1720 first = b = bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1721 word = 0;
1722 } else {
1724 * Go on to the next word in the buffer
1726 b++;
1728 } else {
1730 * Starting on a word boundary, no partial word.
1732 i = 0;
1735 * Loop over whole words in buffers. When we use up one buffer
1736 * we move on to the next one.
1738 while (len - i >= XFS_NBWORD) {
1740 * Set the word value correctly.
1742 *b = val;
1743 i += XFS_NBWORD;
1745 * Go on to the next block if that's where the next word is
1746 * and we need the next word.
1748 if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
1750 * Log the changed part of this block.
1751 * Get the next one.
1753 xfs_trans_log_buf(tp, bp,
1754 (uint)((char *)first - (char *)bufp),
1755 (uint)((char *)b - (char *)bufp));
1756 error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
1757 if (error) {
1758 return error;
1760 first = b = bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1761 word = 0;
1762 } else {
1764 * Go on to the next word in the buffer
1766 b++;
1770 * If not ending on a word boundary, deal with the last
1771 * (partial) word.
1773 if ((lastbit = len - i)) {
1775 * Compute a mask of relevant bits.
1777 bit = 0;
1778 mask = ((xfs_rtword_t)1 << lastbit) - 1;
1780 * Set/clear the active bits.
1782 if (val)
1783 *b |= mask;
1784 else
1785 *b &= ~mask;
1786 b++;
1789 * Log any remaining changed bytes.
1791 if (b > first)
1792 xfs_trans_log_buf(tp, bp, (uint)((char *)first - (char *)bufp),
1793 (uint)((char *)b - (char *)bufp - 1));
1794 return 0;
1798 * Read and modify the summary information for a given extent size,
1799 * bitmap block combination.
1800 * Keeps track of a current summary block, so we don't keep reading
1801 * it from the buffer cache.
1803 STATIC int /* error */
1804 xfs_rtmodify_summary(
1805 xfs_mount_t *mp, /* file system mount point */
1806 xfs_trans_t *tp, /* transaction pointer */
1807 int log, /* log2 of extent size */
1808 xfs_rtblock_t bbno, /* bitmap block number */
1809 int delta, /* change to make to summary info */
1810 xfs_buf_t **rbpp, /* in/out: summary block buffer */
1811 xfs_fsblock_t *rsb) /* in/out: summary block number */
1813 xfs_buf_t *bp; /* buffer for the summary block */
1814 int error; /* error value */
1815 xfs_fsblock_t sb; /* summary fsblock */
1816 int so; /* index into the summary file */
1817 xfs_suminfo_t *sp; /* pointer to returned data */
1820 * Compute entry number in the summary file.
1822 so = XFS_SUMOFFS(mp, log, bbno);
1824 * Compute the block number in the summary file.
1826 sb = XFS_SUMOFFSTOBLOCK(mp, so);
1828 * If we have an old buffer, and the block number matches, use that.
1830 if (rbpp && *rbpp && *rsb == sb)
1831 bp = *rbpp;
1833 * Otherwise we have to get the buffer.
1835 else {
1837 * If there was an old one, get rid of it first.
1839 if (rbpp && *rbpp)
1840 xfs_trans_brelse(tp, *rbpp);
1841 error = xfs_rtbuf_get(mp, tp, sb, 1, &bp);
1842 if (error) {
1843 return error;
1846 * Remember this buffer and block for the next call.
1848 if (rbpp) {
1849 *rbpp = bp;
1850 *rsb = sb;
1854 * Point to the summary information, modify and log it.
1856 sp = XFS_SUMPTR(mp, bp, so);
1857 *sp += delta;
1858 xfs_trans_log_buf(tp, bp, (uint)((char *)sp - (char *)XFS_BUF_PTR(bp)),
1859 (uint)((char *)sp - (char *)XFS_BUF_PTR(bp) + sizeof(*sp) - 1));
1860 return 0;
1864 * Visible (exported) functions.
1868 * Grow the realtime area of the filesystem.
1871 xfs_growfs_rt(
1872 xfs_mount_t *mp, /* mount point for filesystem */
1873 xfs_growfs_rt_t *in) /* growfs rt input struct */
1875 xfs_rtblock_t bmbno; /* bitmap block number */
1876 xfs_buf_t *bp; /* temporary buffer */
1877 int cancelflags; /* flags for xfs_trans_cancel */
1878 int error; /* error return value */
1879 xfs_inode_t *ip; /* bitmap inode, used as lock */
1880 xfs_mount_t *nmp; /* new (fake) mount structure */
1881 xfs_drfsbno_t nrblocks; /* new number of realtime blocks */
1882 xfs_extlen_t nrbmblocks; /* new number of rt bitmap blocks */
1883 xfs_drtbno_t nrextents; /* new number of realtime extents */
1884 uint8_t nrextslog; /* new log2 of sb_rextents */
1885 xfs_extlen_t nrsumblocks; /* new number of summary blocks */
1886 uint nrsumlevels; /* new rt summary levels */
1887 uint nrsumsize; /* new size of rt summary, bytes */
1888 xfs_sb_t *nsbp; /* new superblock */
1889 xfs_extlen_t rbmblocks; /* current number of rt bitmap blocks */
1890 xfs_extlen_t rsumblocks; /* current number of rt summary blks */
1891 xfs_sb_t *sbp; /* old superblock */
1892 xfs_fsblock_t sumbno; /* summary block number */
1893 xfs_trans_t *tp; /* transaction pointer */
1895 sbp = &mp->m_sb;
1897 * Initial error checking.
1899 if (mp->m_rtdev_targp == NULL || mp->m_rbmip == NULL ||
1900 (nrblocks = in->newblocks) <= sbp->sb_rblocks ||
1901 (sbp->sb_rblocks && (in->extsize != sbp->sb_rextsize)))
1902 return XFS_ERROR(EINVAL);
1903 if ((error = xfs_sb_validate_fsb_count(sbp, nrblocks)))
1904 return error;
1906 * Read in the last block of the device, make sure it exists.
1908 error = xfs_read_buf(mp, mp->m_rtdev_targp,
1909 XFS_FSB_TO_BB(mp, nrblocks - 1),
1910 XFS_FSB_TO_BB(mp, 1), 0, &bp);
1911 if (error)
1912 return error;
1913 ASSERT(bp);
1914 xfs_buf_relse(bp);
1916 * Calculate new parameters. These are the final values to be reached.
1918 nrextents = nrblocks;
1919 do_div(nrextents, in->extsize);
1920 nrbmblocks = howmany_64(nrextents, NBBY * sbp->sb_blocksize);
1921 nrextslog = xfs_highbit32(nrextents);
1922 nrsumlevels = nrextslog + 1;
1923 nrsumsize = (uint)sizeof(xfs_suminfo_t) * nrsumlevels * nrbmblocks;
1924 nrsumblocks = XFS_B_TO_FSB(mp, nrsumsize);
1925 nrsumsize = XFS_FSB_TO_B(mp, nrsumblocks);
1927 * New summary size can't be more than half the size of
1928 * the log. This prevents us from getting a log overflow,
1929 * since we'll log basically the whole summary file at once.
1931 if (nrsumblocks > (mp->m_sb.sb_logblocks >> 1))
1932 return XFS_ERROR(EINVAL);
1934 * Get the old block counts for bitmap and summary inodes.
1935 * These can't change since other growfs callers are locked out.
1937 rbmblocks = XFS_B_TO_FSB(mp, mp->m_rbmip->i_d.di_size);
1938 rsumblocks = XFS_B_TO_FSB(mp, mp->m_rsumip->i_d.di_size);
1940 * Allocate space to the bitmap and summary files, as necessary.
1942 if ((error = xfs_growfs_rt_alloc(mp, rbmblocks, nrbmblocks,
1943 mp->m_sb.sb_rbmino)))
1944 return error;
1945 if ((error = xfs_growfs_rt_alloc(mp, rsumblocks, nrsumblocks,
1946 mp->m_sb.sb_rsumino)))
1947 return error;
1949 * Allocate a new (fake) mount/sb.
1951 nmp = kmem_alloc(sizeof(*nmp), KM_SLEEP);
1953 * Loop over the bitmap blocks.
1954 * We will do everything one bitmap block at a time.
1955 * Skip the current block if it is exactly full.
1956 * This also deals with the case where there were no rtextents before.
1958 for (bmbno = sbp->sb_rbmblocks -
1959 ((sbp->sb_rextents & ((1 << mp->m_blkbit_log) - 1)) != 0);
1960 bmbno < nrbmblocks;
1961 bmbno++) {
1962 *nmp = *mp;
1963 nsbp = &nmp->m_sb;
1965 * Calculate new sb and mount fields for this round.
1967 nsbp->sb_rextsize = in->extsize;
1968 nsbp->sb_rbmblocks = bmbno + 1;
1969 nsbp->sb_rblocks =
1970 XFS_RTMIN(nrblocks,
1971 nsbp->sb_rbmblocks * NBBY *
1972 nsbp->sb_blocksize * nsbp->sb_rextsize);
1973 nsbp->sb_rextents = nsbp->sb_rblocks;
1974 do_div(nsbp->sb_rextents, nsbp->sb_rextsize);
1975 <<<<<<< HEAD:fs/xfs/xfs_rtalloc.c
1976 ASSERT(nsbp->sb_rextents != 0);
1977 =======
1978 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:fs/xfs/xfs_rtalloc.c
1979 nsbp->sb_rextslog = xfs_highbit32(nsbp->sb_rextents);
1980 nrsumlevels = nmp->m_rsumlevels = nsbp->sb_rextslog + 1;
1981 nrsumsize =
1982 (uint)sizeof(xfs_suminfo_t) * nrsumlevels *
1983 nsbp->sb_rbmblocks;
1984 nrsumblocks = XFS_B_TO_FSB(mp, nrsumsize);
1985 nmp->m_rsumsize = nrsumsize = XFS_FSB_TO_B(mp, nrsumblocks);
1987 * Start a transaction, get the log reservation.
1989 tp = xfs_trans_alloc(mp, XFS_TRANS_GROWFSRT_FREE);
1990 cancelflags = 0;
1991 if ((error = xfs_trans_reserve(tp, 0,
1992 XFS_GROWRTFREE_LOG_RES(nmp), 0, 0, 0)))
1993 break;
1995 * Lock out other callers by grabbing the bitmap inode lock.
1997 if ((error = xfs_trans_iget(mp, tp, mp->m_sb.sb_rbmino, 0,
1998 XFS_ILOCK_EXCL, &ip)))
1999 break;
2000 ASSERT(ip == mp->m_rbmip);
2002 * Update the bitmap inode's size.
2004 mp->m_rbmip->i_d.di_size =
2005 nsbp->sb_rbmblocks * nsbp->sb_blocksize;
2006 xfs_trans_log_inode(tp, mp->m_rbmip, XFS_ILOG_CORE);
2007 cancelflags |= XFS_TRANS_ABORT;
2009 * Get the summary inode into the transaction.
2011 if ((error = xfs_trans_iget(mp, tp, mp->m_sb.sb_rsumino, 0,
2012 XFS_ILOCK_EXCL, &ip)))
2013 break;
2014 ASSERT(ip == mp->m_rsumip);
2016 * Update the summary inode's size.
2018 mp->m_rsumip->i_d.di_size = nmp->m_rsumsize;
2019 xfs_trans_log_inode(tp, mp->m_rsumip, XFS_ILOG_CORE);
2021 * Copy summary data from old to new sizes.
2022 * Do this when the real size (not block-aligned) changes.
2024 if (sbp->sb_rbmblocks != nsbp->sb_rbmblocks ||
2025 mp->m_rsumlevels != nmp->m_rsumlevels) {
2026 error = xfs_rtcopy_summary(mp, nmp, tp);
2027 if (error)
2028 break;
2031 * Update superblock fields.
2033 if (nsbp->sb_rextsize != sbp->sb_rextsize)
2034 xfs_trans_mod_sb(tp, XFS_TRANS_SB_REXTSIZE,
2035 nsbp->sb_rextsize - sbp->sb_rextsize);
2036 if (nsbp->sb_rbmblocks != sbp->sb_rbmblocks)
2037 xfs_trans_mod_sb(tp, XFS_TRANS_SB_RBMBLOCKS,
2038 nsbp->sb_rbmblocks - sbp->sb_rbmblocks);
2039 if (nsbp->sb_rblocks != sbp->sb_rblocks)
2040 xfs_trans_mod_sb(tp, XFS_TRANS_SB_RBLOCKS,
2041 nsbp->sb_rblocks - sbp->sb_rblocks);
2042 if (nsbp->sb_rextents != sbp->sb_rextents)
2043 xfs_trans_mod_sb(tp, XFS_TRANS_SB_REXTENTS,
2044 nsbp->sb_rextents - sbp->sb_rextents);
2045 if (nsbp->sb_rextslog != sbp->sb_rextslog)
2046 xfs_trans_mod_sb(tp, XFS_TRANS_SB_REXTSLOG,
2047 nsbp->sb_rextslog - sbp->sb_rextslog);
2049 * Free new extent.
2051 bp = NULL;
2052 error = xfs_rtfree_range(nmp, tp, sbp->sb_rextents,
2053 nsbp->sb_rextents - sbp->sb_rextents, &bp, &sumbno);
2054 if (error)
2055 break;
2057 * Mark more blocks free in the superblock.
2059 xfs_trans_mod_sb(tp, XFS_TRANS_SB_FREXTENTS,
2060 nsbp->sb_rextents - sbp->sb_rextents);
2062 * Update mp values into the real mp structure.
2064 mp->m_rsumlevels = nrsumlevels;
2065 mp->m_rsumsize = nrsumsize;
2067 * Commit the transaction.
2069 xfs_trans_commit(tp, 0);
2072 if (error)
2073 xfs_trans_cancel(tp, cancelflags);
2076 * Free the fake mp structure.
2078 kmem_free(nmp, sizeof(*nmp));
2080 return error;
2084 * Allocate an extent in the realtime subvolume, with the usual allocation
2085 * parameters. The length units are all in realtime extents, as is the
2086 * result block number.
2088 int /* error */
2089 xfs_rtallocate_extent(
2090 xfs_trans_t *tp, /* transaction pointer */
2091 xfs_rtblock_t bno, /* starting block number to allocate */
2092 xfs_extlen_t minlen, /* minimum length to allocate */
2093 xfs_extlen_t maxlen, /* maximum length to allocate */
2094 xfs_extlen_t *len, /* out: actual length allocated */
2095 xfs_alloctype_t type, /* allocation type XFS_ALLOCTYPE... */
2096 int wasdel, /* was a delayed allocation extent */
2097 xfs_extlen_t prod, /* extent product factor */
2098 xfs_rtblock_t *rtblock) /* out: start block allocated */
2100 int error; /* error value */
2101 xfs_inode_t *ip; /* inode for bitmap file */
2102 xfs_mount_t *mp; /* file system mount structure */
2103 xfs_rtblock_t r; /* result allocated block */
2104 xfs_fsblock_t sb; /* summary file block number */
2105 xfs_buf_t *sumbp; /* summary file block buffer */
2107 ASSERT(minlen > 0 && minlen <= maxlen);
2108 mp = tp->t_mountp;
2110 * If prod is set then figure out what to do to minlen and maxlen.
2112 if (prod > 1) {
2113 xfs_extlen_t i;
2115 if ((i = maxlen % prod))
2116 maxlen -= i;
2117 if ((i = minlen % prod))
2118 minlen += prod - i;
2119 if (maxlen < minlen) {
2120 *rtblock = NULLRTBLOCK;
2121 return 0;
2125 * Lock out other callers by grabbing the bitmap inode lock.
2127 if ((error = xfs_trans_iget(mp, tp, mp->m_sb.sb_rbmino, 0,
2128 XFS_ILOCK_EXCL, &ip)))
2129 return error;
2130 sumbp = NULL;
2132 * Allocate by size, or near another block, or exactly at some block.
2134 switch (type) {
2135 case XFS_ALLOCTYPE_ANY_AG:
2136 error = xfs_rtallocate_extent_size(mp, tp, minlen, maxlen, len,
2137 &sumbp, &sb, prod, &r);
2138 break;
2139 case XFS_ALLOCTYPE_NEAR_BNO:
2140 error = xfs_rtallocate_extent_near(mp, tp, bno, minlen, maxlen,
2141 len, &sumbp, &sb, prod, &r);
2142 break;
2143 case XFS_ALLOCTYPE_THIS_BNO:
2144 error = xfs_rtallocate_extent_exact(mp, tp, bno, minlen, maxlen,
2145 len, &sumbp, &sb, prod, &r);
2146 break;
2147 default:
2148 ASSERT(0);
2150 if (error) {
2151 return error;
2154 * If it worked, update the superblock.
2156 if (r != NULLRTBLOCK) {
2157 long slen = (long)*len;
2159 ASSERT(*len >= minlen && *len <= maxlen);
2160 if (wasdel)
2161 xfs_trans_mod_sb(tp, XFS_TRANS_SB_RES_FREXTENTS, -slen);
2162 else
2163 xfs_trans_mod_sb(tp, XFS_TRANS_SB_FREXTENTS, -slen);
2165 *rtblock = r;
2166 return 0;
2170 * Free an extent in the realtime subvolume. Length is expressed in
2171 * realtime extents, as is the block number.
2173 int /* error */
2174 xfs_rtfree_extent(
2175 xfs_trans_t *tp, /* transaction pointer */
2176 xfs_rtblock_t bno, /* starting block number to free */
2177 xfs_extlen_t len) /* length of extent freed */
2179 int error; /* error value */
2180 xfs_inode_t *ip; /* bitmap file inode */
2181 xfs_mount_t *mp; /* file system mount structure */
2182 xfs_fsblock_t sb; /* summary file block number */
2183 xfs_buf_t *sumbp; /* summary file block buffer */
2185 mp = tp->t_mountp;
2187 * Synchronize by locking the bitmap inode.
2189 if ((error = xfs_trans_iget(mp, tp, mp->m_sb.sb_rbmino, 0,
2190 XFS_ILOCK_EXCL, &ip)))
2191 return error;
2192 #if defined(__KERNEL__) && defined(DEBUG)
2194 * Check to see that this whole range is currently allocated.
2197 int stat; /* result from checking range */
2199 error = xfs_rtcheck_alloc_range(mp, tp, bno, len, &stat);
2200 if (error) {
2201 return error;
2203 ASSERT(stat);
2205 #endif
2206 sumbp = NULL;
2208 * Free the range of realtime blocks.
2210 error = xfs_rtfree_range(mp, tp, bno, len, &sumbp, &sb);
2211 if (error) {
2212 return error;
2215 * Mark more blocks free in the superblock.
2217 xfs_trans_mod_sb(tp, XFS_TRANS_SB_FREXTENTS, (long)len);
2219 * If we've now freed all the blocks, reset the file sequence
2220 * number to 0.
2222 if (tp->t_frextents_delta + mp->m_sb.sb_frextents ==
2223 mp->m_sb.sb_rextents) {
2224 if (!(ip->i_d.di_flags & XFS_DIFLAG_NEWRTBM))
2225 ip->i_d.di_flags |= XFS_DIFLAG_NEWRTBM;
2226 *(__uint64_t *)&ip->i_d.di_atime = 0;
2227 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
2229 return 0;
2233 * Initialize realtime fields in the mount structure.
2235 int /* error */
2236 xfs_rtmount_init(
2237 xfs_mount_t *mp) /* file system mount structure */
2239 xfs_buf_t *bp; /* buffer for last block of subvolume */
2240 xfs_daddr_t d; /* address of last block of subvolume */
2241 int error; /* error return value */
2242 xfs_sb_t *sbp; /* filesystem superblock copy in mount */
2244 sbp = &mp->m_sb;
2245 if (sbp->sb_rblocks == 0)
2246 return 0;
2247 if (mp->m_rtdev_targp == NULL) {
2248 cmn_err(CE_WARN,
2249 "XFS: This filesystem has a realtime volume, use rtdev=device option");
2250 return XFS_ERROR(ENODEV);
2252 mp->m_rsumlevels = sbp->sb_rextslog + 1;
2253 mp->m_rsumsize =
2254 (uint)sizeof(xfs_suminfo_t) * mp->m_rsumlevels *
2255 sbp->sb_rbmblocks;
2256 mp->m_rsumsize = roundup(mp->m_rsumsize, sbp->sb_blocksize);
2257 mp->m_rbmip = mp->m_rsumip = NULL;
2259 * Check that the realtime section is an ok size.
2261 d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_rblocks);
2262 if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_rblocks) {
2263 cmn_err(CE_WARN, "XFS: realtime mount -- %llu != %llu",
2264 (unsigned long long) XFS_BB_TO_FSB(mp, d),
2265 (unsigned long long) mp->m_sb.sb_rblocks);
2266 return XFS_ERROR(E2BIG);
2268 error = xfs_read_buf(mp, mp->m_rtdev_targp,
2269 d - XFS_FSB_TO_BB(mp, 1),
2270 XFS_FSB_TO_BB(mp, 1), 0, &bp);
2271 if (error) {
2272 cmn_err(CE_WARN,
2273 "XFS: realtime mount -- xfs_read_buf failed, returned %d", error);
2274 if (error == ENOSPC)
2275 return XFS_ERROR(E2BIG);
2276 return error;
2278 xfs_buf_relse(bp);
2279 return 0;
2283 * Get the bitmap and summary inodes into the mount structure
2284 * at mount time.
2286 int /* error */
2287 xfs_rtmount_inodes(
2288 xfs_mount_t *mp) /* file system mount structure */
2290 int error; /* error return value */
2291 xfs_sb_t *sbp;
2293 sbp = &mp->m_sb;
2294 if (sbp->sb_rbmino == NULLFSINO)
2295 return 0;
2296 error = xfs_iget(mp, NULL, sbp->sb_rbmino, 0, 0, &mp->m_rbmip, 0);
2297 if (error)
2298 return error;
2299 ASSERT(mp->m_rbmip != NULL);
2300 ASSERT(sbp->sb_rsumino != NULLFSINO);
2301 error = xfs_iget(mp, NULL, sbp->sb_rsumino, 0, 0, &mp->m_rsumip, 0);
2302 if (error) {
2303 VN_RELE(XFS_ITOV(mp->m_rbmip));
2304 return error;
2306 ASSERT(mp->m_rsumip != NULL);
2307 return 0;
2311 * Pick an extent for allocation at the start of a new realtime file.
2312 * Use the sequence number stored in the atime field of the bitmap inode.
2313 * Translate this to a fraction of the rtextents, and return the product
2314 * of rtextents and the fraction.
2315 * The fraction sequence is 0, 1/2, 1/4, 3/4, 1/8, ..., 7/8, 1/16, ...
2317 int /* error */
2318 xfs_rtpick_extent(
2319 xfs_mount_t *mp, /* file system mount point */
2320 xfs_trans_t *tp, /* transaction pointer */
2321 xfs_extlen_t len, /* allocation length (rtextents) */
2322 xfs_rtblock_t *pick) /* result rt extent */
2324 xfs_rtblock_t b; /* result block */
2325 int error; /* error return value */
2326 xfs_inode_t *ip; /* bitmap incore inode */
2327 int log2; /* log of sequence number */
2328 __uint64_t resid; /* residual after log removed */
2329 __uint64_t seq; /* sequence number of file creation */
2330 __uint64_t *seqp; /* pointer to seqno in inode */
2332 if ((error = xfs_trans_iget(mp, tp, mp->m_sb.sb_rbmino, 0,
2333 XFS_ILOCK_EXCL, &ip)))
2334 return error;
2335 ASSERT(ip == mp->m_rbmip);
2336 seqp = (__uint64_t *)&ip->i_d.di_atime;
2337 if (!(ip->i_d.di_flags & XFS_DIFLAG_NEWRTBM)) {
2338 ip->i_d.di_flags |= XFS_DIFLAG_NEWRTBM;
2339 *seqp = 0;
2341 seq = *seqp;
2342 if ((log2 = xfs_highbit64(seq)) == -1)
2343 b = 0;
2344 else {
2345 resid = seq - (1ULL << log2);
2346 b = (mp->m_sb.sb_rextents * ((resid << 1) + 1ULL)) >>
2347 (log2 + 1);
2348 if (b >= mp->m_sb.sb_rextents)
2349 b = do_mod(b, mp->m_sb.sb_rextents);
2350 if (b + len > mp->m_sb.sb_rextents)
2351 b = mp->m_sb.sb_rextents - len;
2353 *seqp = seq + 1;
2354 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
2355 *pick = b;
2356 return 0;