Linux-2.6.12-rc2
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / xfs / xfs_rtalloc.c
blob2c37822d1012e60173d396d2fc4cf94097ba8f1b
1 /*
2 * Copyright (c) 2000-2004 Silicon Graphics, Inc. All Rights Reserved.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it would be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 * Further, this software is distributed without any warranty that it is
13 * free of the rightful claim of any third person regarding infringement
14 * or the like. Any license provided herein, whether implied or
15 * otherwise, applies only to this software file. Patent licenses, if
16 * any, provided herein do not apply to combinations of this program with
17 * other software, or any other product whatsoever.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write the Free Software Foundation, Inc., 59
21 * Temple Place - Suite 330, Boston MA 02111-1307, USA.
23 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA 94043, or:
26 * http://www.sgi.com
28 * For further information regarding this notice, see:
30 * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
34 * Free realtime space allocation for XFS.
37 #include "xfs.h"
38 #include "xfs_macros.h"
39 #include "xfs_types.h"
40 #include "xfs_inum.h"
41 #include "xfs_log.h"
42 #include "xfs_trans.h"
43 #include "xfs_sb.h"
44 #include "xfs_ag.h"
45 #include "xfs_dir.h"
46 #include "xfs_dir2.h"
47 #include "xfs_dmapi.h"
48 #include "xfs_mount.h"
49 #include "xfs_alloc_btree.h"
50 #include "xfs_bmap_btree.h"
51 #include "xfs_ialloc_btree.h"
52 #include "xfs_btree.h"
53 #include "xfs_ialloc.h"
54 #include "xfs_attr_sf.h"
55 #include "xfs_dir_sf.h"
56 #include "xfs_dir2_sf.h"
57 #include "xfs_dinode.h"
58 #include "xfs_inode.h"
59 #include "xfs_alloc.h"
60 #include "xfs_bmap.h"
61 #include "xfs_bit.h"
62 #include "xfs_rtalloc.h"
63 #include "xfs_fsops.h"
64 #include "xfs_error.h"
65 #include "xfs_rw.h"
66 #include "xfs_inode_item.h"
67 #include "xfs_trans_space.h"
71 * Prototypes for internal functions.
75 STATIC int xfs_rtallocate_range(xfs_mount_t *, xfs_trans_t *, xfs_rtblock_t,
76 xfs_extlen_t, xfs_buf_t **, xfs_fsblock_t *);
77 STATIC int xfs_rtany_summary(xfs_mount_t *, xfs_trans_t *, int, int,
78 xfs_rtblock_t, xfs_buf_t **, xfs_fsblock_t *, int *);
79 STATIC int xfs_rtcheck_range(xfs_mount_t *, xfs_trans_t *, xfs_rtblock_t,
80 xfs_extlen_t, int, xfs_rtblock_t *, int *);
81 STATIC int xfs_rtfind_back(xfs_mount_t *, xfs_trans_t *, xfs_rtblock_t,
82 xfs_rtblock_t, xfs_rtblock_t *);
83 STATIC int xfs_rtfind_forw(xfs_mount_t *, xfs_trans_t *, xfs_rtblock_t,
84 xfs_rtblock_t, xfs_rtblock_t *);
85 STATIC int xfs_rtget_summary( xfs_mount_t *, xfs_trans_t *, int,
86 xfs_rtblock_t, xfs_buf_t **, xfs_fsblock_t *, xfs_suminfo_t *);
87 STATIC int xfs_rtmodify_range(xfs_mount_t *, xfs_trans_t *, xfs_rtblock_t,
88 xfs_extlen_t, int);
89 STATIC int xfs_rtmodify_summary(xfs_mount_t *, xfs_trans_t *, int,
90 xfs_rtblock_t, int, xfs_buf_t **, xfs_fsblock_t *);
93 * Internal functions.
97 * xfs_lowbit32: get low bit set out of 32-bit argument, -1 if none set.
99 STATIC int
100 xfs_lowbit32(
101 __uint32_t v)
103 if (v)
104 return ffs(v) - 1;
105 return -1;
109 * Allocate space to the bitmap or summary file, and zero it, for growfs.
111 STATIC int /* error */
112 xfs_growfs_rt_alloc(
113 xfs_mount_t *mp, /* file system mount point */
114 xfs_extlen_t oblocks, /* old count of blocks */
115 xfs_extlen_t nblocks, /* new count of blocks */
116 xfs_ino_t ino) /* inode number (bitmap/summary) */
118 xfs_fileoff_t bno; /* block number in file */
119 xfs_buf_t *bp; /* temporary buffer for zeroing */
120 int cancelflags; /* flags for xfs_trans_cancel */
121 int committed; /* transaction committed flag */
122 xfs_daddr_t d; /* disk block address */
123 int error; /* error return value */
124 xfs_fsblock_t firstblock; /* first block allocated in xaction */
125 xfs_bmap_free_t flist; /* list of freed blocks */
126 xfs_fsblock_t fsbno; /* filesystem block for bno */
127 xfs_inode_t *ip; /* pointer to incore inode */
128 xfs_bmbt_irec_t map; /* block map output */
129 int nmap; /* number of block maps */
130 int resblks; /* space reservation */
131 xfs_trans_t *tp; /* transaction pointer */
134 * Allocate space to the file, as necessary.
136 while (oblocks < nblocks) {
137 tp = xfs_trans_alloc(mp, XFS_TRANS_GROWFSRT_ALLOC);
138 resblks = XFS_GROWFSRT_SPACE_RES(mp, nblocks - oblocks);
139 cancelflags = 0;
141 * Reserve space & log for one extent added to the file.
143 if ((error = xfs_trans_reserve(tp, resblks,
144 XFS_GROWRTALLOC_LOG_RES(mp), 0,
145 XFS_TRANS_PERM_LOG_RES,
146 XFS_DEFAULT_PERM_LOG_COUNT)))
147 goto error_exit;
148 cancelflags = XFS_TRANS_RELEASE_LOG_RES;
150 * Lock the inode.
152 if ((error = xfs_trans_iget(mp, tp, ino, 0, XFS_ILOCK_EXCL, &ip)))
153 goto error_exit;
154 XFS_BMAP_INIT(&flist, &firstblock);
156 * Allocate blocks to the bitmap file.
158 nmap = 1;
159 cancelflags |= XFS_TRANS_ABORT;
160 error = xfs_bmapi(tp, ip, oblocks, nblocks - oblocks,
161 XFS_BMAPI_WRITE | XFS_BMAPI_METADATA, &firstblock,
162 resblks, &map, &nmap, &flist);
163 if (!error && nmap < 1)
164 error = XFS_ERROR(ENOSPC);
165 if (error)
166 goto error_exit;
168 * Free any blocks freed up in the transaction, then commit.
170 error = xfs_bmap_finish(&tp, &flist, firstblock, &committed);
171 if (error)
172 goto error_exit;
173 xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES, NULL);
175 * Now we need to clear the allocated blocks.
176 * Do this one block per transaction, to keep it simple.
178 cancelflags = 0;
179 for (bno = map.br_startoff, fsbno = map.br_startblock;
180 bno < map.br_startoff + map.br_blockcount;
181 bno++, fsbno++) {
182 tp = xfs_trans_alloc(mp, XFS_TRANS_GROWFSRT_ZERO);
184 * Reserve log for one block zeroing.
186 if ((error = xfs_trans_reserve(tp, 0,
187 XFS_GROWRTZERO_LOG_RES(mp), 0, 0, 0)))
188 goto error_exit;
190 * Lock the bitmap inode.
192 if ((error = xfs_trans_iget(mp, tp, ino, 0, XFS_ILOCK_EXCL,
193 &ip)))
194 goto error_exit;
196 * Get a buffer for the block.
198 d = XFS_FSB_TO_DADDR(mp, fsbno);
199 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
200 mp->m_bsize, 0);
201 if (bp == NULL) {
202 error = XFS_ERROR(EIO);
203 goto error_exit;
205 memset(XFS_BUF_PTR(bp), 0, mp->m_sb.sb_blocksize);
206 xfs_trans_log_buf(tp, bp, 0, mp->m_sb.sb_blocksize - 1);
208 * Commit the transaction.
210 xfs_trans_commit(tp, 0, NULL);
213 * Go on to the next extent, if any.
215 oblocks = map.br_startoff + map.br_blockcount;
217 return 0;
218 error_exit:
219 xfs_trans_cancel(tp, cancelflags);
220 return error;
224 * Attempt to allocate an extent minlen<=len<=maxlen starting from
225 * bitmap block bbno. If we don't get maxlen then use prod to trim
226 * the length, if given. Returns error; returns starting block in *rtblock.
227 * The lengths are all in rtextents.
229 STATIC int /* error */
230 xfs_rtallocate_extent_block(
231 xfs_mount_t *mp, /* file system mount point */
232 xfs_trans_t *tp, /* transaction pointer */
233 xfs_rtblock_t bbno, /* bitmap block number */
234 xfs_extlen_t minlen, /* minimum length to allocate */
235 xfs_extlen_t maxlen, /* maximum length to allocate */
236 xfs_extlen_t *len, /* out: actual length allocated */
237 xfs_rtblock_t *nextp, /* out: next block to try */
238 xfs_buf_t **rbpp, /* in/out: summary block buffer */
239 xfs_fsblock_t *rsb, /* in/out: summary block number */
240 xfs_extlen_t prod, /* extent product factor */
241 xfs_rtblock_t *rtblock) /* out: start block allocated */
243 xfs_rtblock_t besti; /* best rtblock found so far */
244 xfs_rtblock_t bestlen; /* best length found so far */
245 xfs_rtblock_t end; /* last rtblock in chunk */
246 int error; /* error value */
247 xfs_rtblock_t i; /* current rtblock trying */
248 xfs_rtblock_t next; /* next rtblock to try */
249 int stat; /* status from internal calls */
252 * Loop over all the extents starting in this bitmap block,
253 * looking for one that's long enough.
255 for (i = XFS_BLOCKTOBIT(mp, bbno), besti = -1, bestlen = 0,
256 end = XFS_BLOCKTOBIT(mp, bbno + 1) - 1;
257 i <= end;
258 i++) {
260 * See if there's a free extent of maxlen starting at i.
261 * If it's not so then next will contain the first non-free.
263 error = xfs_rtcheck_range(mp, tp, i, maxlen, 1, &next, &stat);
264 if (error) {
265 return error;
267 if (stat) {
269 * i for maxlen is all free, allocate and return that.
271 error = xfs_rtallocate_range(mp, tp, i, maxlen, rbpp,
272 rsb);
273 if (error) {
274 return error;
276 *len = maxlen;
277 *rtblock = i;
278 return 0;
281 * In the case where we have a variable-sized allocation
282 * request, figure out how big this free piece is,
283 * and if it's big enough for the minimum, and the best
284 * so far, remember it.
286 if (minlen < maxlen) {
287 xfs_rtblock_t thislen; /* this extent size */
289 thislen = next - i;
290 if (thislen >= minlen && thislen > bestlen) {
291 besti = i;
292 bestlen = thislen;
296 * If not done yet, find the start of the next free space.
298 if (next < end) {
299 error = xfs_rtfind_forw(mp, tp, next, end, &i);
300 if (error) {
301 return error;
303 } else
304 break;
307 * Searched the whole thing & didn't find a maxlen free extent.
309 if (minlen < maxlen && besti != -1) {
310 xfs_extlen_t p; /* amount to trim length by */
313 * If size should be a multiple of prod, make that so.
315 if (prod > 1 && (p = do_mod(bestlen, prod)))
316 bestlen -= p;
318 * Allocate besti for bestlen & return that.
320 error = xfs_rtallocate_range(mp, tp, besti, bestlen, rbpp, rsb);
321 if (error) {
322 return error;
324 *len = bestlen;
325 *rtblock = besti;
326 return 0;
329 * Allocation failed. Set *nextp to the next block to try.
331 *nextp = next;
332 *rtblock = NULLRTBLOCK;
333 return 0;
337 * Allocate an extent of length minlen<=len<=maxlen, starting at block
338 * bno. If we don't get maxlen then use prod to trim the length, if given.
339 * Returns error; returns starting block in *rtblock.
340 * The lengths are all in rtextents.
342 STATIC int /* error */
343 xfs_rtallocate_extent_exact(
344 xfs_mount_t *mp, /* file system mount point */
345 xfs_trans_t *tp, /* transaction pointer */
346 xfs_rtblock_t bno, /* starting block number to allocate */
347 xfs_extlen_t minlen, /* minimum length to allocate */
348 xfs_extlen_t maxlen, /* maximum length to allocate */
349 xfs_extlen_t *len, /* out: actual length allocated */
350 xfs_buf_t **rbpp, /* in/out: summary block buffer */
351 xfs_fsblock_t *rsb, /* in/out: summary block number */
352 xfs_extlen_t prod, /* extent product factor */
353 xfs_rtblock_t *rtblock) /* out: start block allocated */
355 int error; /* error value */
356 xfs_extlen_t i; /* extent length trimmed due to prod */
357 int isfree; /* extent is free */
358 xfs_rtblock_t next; /* next block to try (dummy) */
360 ASSERT(minlen % prod == 0 && maxlen % prod == 0);
362 * Check if the range in question (for maxlen) is free.
364 error = xfs_rtcheck_range(mp, tp, bno, maxlen, 1, &next, &isfree);
365 if (error) {
366 return error;
368 if (isfree) {
370 * If it is, allocate it and return success.
372 error = xfs_rtallocate_range(mp, tp, bno, maxlen, rbpp, rsb);
373 if (error) {
374 return error;
376 *len = maxlen;
377 *rtblock = bno;
378 return 0;
381 * If not, allocate what there is, if it's at least minlen.
383 maxlen = next - bno;
384 if (maxlen < minlen) {
386 * Failed, return failure status.
388 *rtblock = NULLRTBLOCK;
389 return 0;
392 * Trim off tail of extent, if prod is specified.
394 if (prod > 1 && (i = maxlen % prod)) {
395 maxlen -= i;
396 if (maxlen < minlen) {
398 * Now we can't do it, return failure status.
400 *rtblock = NULLRTBLOCK;
401 return 0;
405 * Allocate what we can and return it.
407 error = xfs_rtallocate_range(mp, tp, bno, maxlen, rbpp, rsb);
408 if (error) {
409 return error;
411 *len = maxlen;
412 *rtblock = bno;
413 return 0;
417 * Allocate an extent of length minlen<=len<=maxlen, starting as near
418 * to bno as possible. If we don't get maxlen then use prod to trim
419 * the length, if given. The lengths are all in rtextents.
421 STATIC int /* error */
422 xfs_rtallocate_extent_near(
423 xfs_mount_t *mp, /* file system mount point */
424 xfs_trans_t *tp, /* transaction pointer */
425 xfs_rtblock_t bno, /* starting block number to allocate */
426 xfs_extlen_t minlen, /* minimum length to allocate */
427 xfs_extlen_t maxlen, /* maximum length to allocate */
428 xfs_extlen_t *len, /* out: actual length allocated */
429 xfs_buf_t **rbpp, /* in/out: summary block buffer */
430 xfs_fsblock_t *rsb, /* in/out: summary block number */
431 xfs_extlen_t prod, /* extent product factor */
432 xfs_rtblock_t *rtblock) /* out: start block allocated */
434 int any; /* any useful extents from summary */
435 xfs_rtblock_t bbno; /* bitmap block number */
436 int error; /* error value */
437 int i; /* bitmap block offset (loop control) */
438 int j; /* secondary loop control */
439 int log2len; /* log2 of minlen */
440 xfs_rtblock_t n; /* next block to try */
441 xfs_rtblock_t r; /* result block */
443 ASSERT(minlen % prod == 0 && maxlen % prod == 0);
445 * If the block number given is off the end, silently set it to
446 * the last block.
448 if (bno >= mp->m_sb.sb_rextents)
449 bno = mp->m_sb.sb_rextents - 1;
451 * Try the exact allocation first.
453 error = xfs_rtallocate_extent_exact(mp, tp, bno, minlen, maxlen, len,
454 rbpp, rsb, prod, &r);
455 if (error) {
456 return error;
459 * If the exact allocation worked, return that.
461 if (r != NULLRTBLOCK) {
462 *rtblock = r;
463 return 0;
465 bbno = XFS_BITTOBLOCK(mp, bno);
466 i = 0;
467 log2len = xfs_highbit32(minlen);
469 * Loop over all bitmap blocks (bbno + i is current block).
471 for (;;) {
473 * Get summary information of extents of all useful levels
474 * starting in this bitmap block.
476 error = xfs_rtany_summary(mp, tp, log2len, mp->m_rsumlevels - 1,
477 bbno + i, rbpp, rsb, &any);
478 if (error) {
479 return error;
482 * If there are any useful extents starting here, try
483 * allocating one.
485 if (any) {
487 * On the positive side of the starting location.
489 if (i >= 0) {
491 * Try to allocate an extent starting in
492 * this block.
494 error = xfs_rtallocate_extent_block(mp, tp,
495 bbno + i, minlen, maxlen, len, &n, rbpp,
496 rsb, prod, &r);
497 if (error) {
498 return error;
501 * If it worked, return it.
503 if (r != NULLRTBLOCK) {
504 *rtblock = r;
505 return 0;
509 * On the negative side of the starting location.
511 else { /* i < 0 */
513 * Loop backwards through the bitmap blocks from
514 * the starting point-1 up to where we are now.
515 * There should be an extent which ends in this
516 * bitmap block and is long enough.
518 for (j = -1; j > i; j--) {
520 * Grab the summary information for
521 * this bitmap block.
523 error = xfs_rtany_summary(mp, tp,
524 log2len, mp->m_rsumlevels - 1,
525 bbno + j, rbpp, rsb, &any);
526 if (error) {
527 return error;
530 * If there's no extent given in the
531 * summary that means the extent we
532 * found must carry over from an
533 * earlier block. If there is an
534 * extent given, we've already tried
535 * that allocation, don't do it again.
537 if (any)
538 continue;
539 error = xfs_rtallocate_extent_block(mp,
540 tp, bbno + j, minlen, maxlen,
541 len, &n, rbpp, rsb, prod, &r);
542 if (error) {
543 return error;
546 * If it works, return the extent.
548 if (r != NULLRTBLOCK) {
549 *rtblock = r;
550 return 0;
554 * There weren't intervening bitmap blocks
555 * with a long enough extent, or the
556 * allocation didn't work for some reason
557 * (i.e. it's a little * too short).
558 * Try to allocate from the summary block
559 * that we found.
561 error = xfs_rtallocate_extent_block(mp, tp,
562 bbno + i, minlen, maxlen, len, &n, rbpp,
563 rsb, prod, &r);
564 if (error) {
565 return error;
568 * If it works, return the extent.
570 if (r != NULLRTBLOCK) {
571 *rtblock = r;
572 return 0;
577 * Loop control. If we were on the positive side, and there's
578 * still more blocks on the negative side, go there.
580 if (i > 0 && (int)bbno - i >= 0)
581 i = -i;
583 * If positive, and no more negative, but there are more
584 * positive, go there.
586 else if (i > 0 && (int)bbno + i < mp->m_sb.sb_rbmblocks - 1)
587 i++;
589 * If negative or 0 (just started), and there are positive
590 * blocks to go, go there. The 0 case moves to block 1.
592 else if (i <= 0 && (int)bbno - i < mp->m_sb.sb_rbmblocks - 1)
593 i = 1 - i;
595 * If negative or 0 and there are more negative blocks,
596 * go there.
598 else if (i <= 0 && (int)bbno + i > 0)
599 i--;
601 * Must be done. Return failure.
603 else
604 break;
606 *rtblock = NULLRTBLOCK;
607 return 0;
611 * Allocate an extent of length minlen<=len<=maxlen, with no position
612 * specified. If we don't get maxlen then use prod to trim
613 * the length, if given. The lengths are all in rtextents.
615 STATIC int /* error */
616 xfs_rtallocate_extent_size(
617 xfs_mount_t *mp, /* file system mount point */
618 xfs_trans_t *tp, /* transaction pointer */
619 xfs_extlen_t minlen, /* minimum length to allocate */
620 xfs_extlen_t maxlen, /* maximum length to allocate */
621 xfs_extlen_t *len, /* out: actual length allocated */
622 xfs_buf_t **rbpp, /* in/out: summary block buffer */
623 xfs_fsblock_t *rsb, /* in/out: summary block number */
624 xfs_extlen_t prod, /* extent product factor */
625 xfs_rtblock_t *rtblock) /* out: start block allocated */
627 int error; /* error value */
628 int i; /* bitmap block number */
629 int l; /* level number (loop control) */
630 xfs_rtblock_t n; /* next block to be tried */
631 xfs_rtblock_t r; /* result block number */
632 xfs_suminfo_t sum; /* summary information for extents */
634 ASSERT(minlen % prod == 0 && maxlen % prod == 0);
636 * Loop over all the levels starting with maxlen.
637 * At each level, look at all the bitmap blocks, to see if there
638 * are extents starting there that are long enough (>= maxlen).
639 * Note, only on the initial level can the allocation fail if
640 * the summary says there's an extent.
642 for (l = xfs_highbit32(maxlen); l < mp->m_rsumlevels; l++) {
644 * Loop over all the bitmap blocks.
646 for (i = 0; i < mp->m_sb.sb_rbmblocks; i++) {
648 * Get the summary for this level/block.
650 error = xfs_rtget_summary(mp, tp, l, i, rbpp, rsb,
651 &sum);
652 if (error) {
653 return error;
656 * Nothing there, on to the next block.
658 if (!sum)
659 continue;
661 * Try allocating the extent.
663 error = xfs_rtallocate_extent_block(mp, tp, i, maxlen,
664 maxlen, len, &n, rbpp, rsb, prod, &r);
665 if (error) {
666 return error;
669 * If it worked, return that.
671 if (r != NULLRTBLOCK) {
672 *rtblock = r;
673 return 0;
676 * If the "next block to try" returned from the
677 * allocator is beyond the next bitmap block,
678 * skip to that bitmap block.
680 if (XFS_BITTOBLOCK(mp, n) > i + 1)
681 i = XFS_BITTOBLOCK(mp, n) - 1;
685 * Didn't find any maxlen blocks. Try smaller ones, unless
686 * we're asking for a fixed size extent.
688 if (minlen > --maxlen) {
689 *rtblock = NULLRTBLOCK;
690 return 0;
693 * Loop over sizes, from maxlen down to minlen.
694 * This time, when we do the allocations, allow smaller ones
695 * to succeed.
697 for (l = xfs_highbit32(maxlen); l >= xfs_highbit32(minlen); l--) {
699 * Loop over all the bitmap blocks, try an allocation
700 * starting in that block.
702 for (i = 0; i < mp->m_sb.sb_rbmblocks; i++) {
704 * Get the summary information for this level/block.
706 error = xfs_rtget_summary(mp, tp, l, i, rbpp, rsb,
707 &sum);
708 if (error) {
709 return error;
712 * If nothing there, go on to next.
714 if (!sum)
715 continue;
717 * Try the allocation. Make sure the specified
718 * minlen/maxlen are in the possible range for
719 * this summary level.
721 error = xfs_rtallocate_extent_block(mp, tp, i,
722 XFS_RTMAX(minlen, 1 << l),
723 XFS_RTMIN(maxlen, (1 << (l + 1)) - 1),
724 len, &n, rbpp, rsb, prod, &r);
725 if (error) {
726 return error;
729 * If it worked, return that extent.
731 if (r != NULLRTBLOCK) {
732 *rtblock = r;
733 return 0;
736 * If the "next block to try" returned from the
737 * allocator is beyond the next bitmap block,
738 * skip to that bitmap block.
740 if (XFS_BITTOBLOCK(mp, n) > i + 1)
741 i = XFS_BITTOBLOCK(mp, n) - 1;
745 * Got nothing, return failure.
747 *rtblock = NULLRTBLOCK;
748 return 0;
752 * Mark an extent specified by start and len allocated.
753 * Updates all the summary information as well as the bitmap.
755 STATIC int /* error */
756 xfs_rtallocate_range(
757 xfs_mount_t *mp, /* file system mount point */
758 xfs_trans_t *tp, /* transaction pointer */
759 xfs_rtblock_t start, /* start block to allocate */
760 xfs_extlen_t len, /* length to allocate */
761 xfs_buf_t **rbpp, /* in/out: summary block buffer */
762 xfs_fsblock_t *rsb) /* in/out: summary block number */
764 xfs_rtblock_t end; /* end of the allocated extent */
765 int error; /* error value */
766 xfs_rtblock_t postblock; /* first block allocated > end */
767 xfs_rtblock_t preblock; /* first block allocated < start */
769 end = start + len - 1;
771 * Assume we're allocating out of the middle of a free extent.
772 * We need to find the beginning and end of the extent so we can
773 * properly update the summary.
775 error = xfs_rtfind_back(mp, tp, start, 0, &preblock);
776 if (error) {
777 return error;
780 * Find the next allocated block (end of free extent).
782 error = xfs_rtfind_forw(mp, tp, end, mp->m_sb.sb_rextents - 1,
783 &postblock);
784 if (error) {
785 return error;
788 * Decrement the summary information corresponding to the entire
789 * (old) free extent.
791 error = xfs_rtmodify_summary(mp, tp,
792 XFS_RTBLOCKLOG(postblock + 1 - preblock),
793 XFS_BITTOBLOCK(mp, preblock), -1, rbpp, rsb);
794 if (error) {
795 return error;
798 * If there are blocks not being allocated at the front of the
799 * old extent, add summary data for them to be free.
801 if (preblock < start) {
802 error = xfs_rtmodify_summary(mp, tp,
803 XFS_RTBLOCKLOG(start - preblock),
804 XFS_BITTOBLOCK(mp, preblock), 1, rbpp, rsb);
805 if (error) {
806 return error;
810 * If there are blocks not being allocated at the end of the
811 * old extent, add summary data for them to be free.
813 if (postblock > end) {
814 error = xfs_rtmodify_summary(mp, tp,
815 XFS_RTBLOCKLOG(postblock - end),
816 XFS_BITTOBLOCK(mp, end + 1), 1, rbpp, rsb);
817 if (error) {
818 return error;
822 * Modify the bitmap to mark this extent allocated.
824 error = xfs_rtmodify_range(mp, tp, start, len, 0);
825 return error;
829 * Return whether there are any free extents in the size range given
830 * by low and high, for the bitmap block bbno.
832 STATIC int /* error */
833 xfs_rtany_summary(
834 xfs_mount_t *mp, /* file system mount structure */
835 xfs_trans_t *tp, /* transaction pointer */
836 int low, /* low log2 extent size */
837 int high, /* high log2 extent size */
838 xfs_rtblock_t bbno, /* bitmap block number */
839 xfs_buf_t **rbpp, /* in/out: summary block buffer */
840 xfs_fsblock_t *rsb, /* in/out: summary block number */
841 int *stat) /* out: any good extents here? */
843 int error; /* error value */
844 int log; /* loop counter, log2 of ext. size */
845 xfs_suminfo_t sum; /* summary data */
848 * Loop over logs of extent sizes. Order is irrelevant.
850 for (log = low; log <= high; log++) {
852 * Get one summary datum.
854 error = xfs_rtget_summary(mp, tp, log, bbno, rbpp, rsb, &sum);
855 if (error) {
856 return error;
859 * If there are any, return success.
861 if (sum) {
862 *stat = 1;
863 return 0;
867 * Found nothing, return failure.
869 *stat = 0;
870 return 0;
874 * Get a buffer for the bitmap or summary file block specified.
875 * The buffer is returned read and locked.
877 STATIC int /* error */
878 xfs_rtbuf_get(
879 xfs_mount_t *mp, /* file system mount structure */
880 xfs_trans_t *tp, /* transaction pointer */
881 xfs_rtblock_t block, /* block number in bitmap or summary */
882 int issum, /* is summary not bitmap */
883 xfs_buf_t **bpp) /* output: buffer for the block */
885 xfs_buf_t *bp; /* block buffer, result */
886 xfs_daddr_t d; /* disk addr of block */
887 int error; /* error value */
888 xfs_fsblock_t fsb; /* fs block number for block */
889 xfs_inode_t *ip; /* bitmap or summary inode */
891 ip = issum ? mp->m_rsumip : mp->m_rbmip;
893 * Map from the file offset (block) and inode number to the
894 * file system block.
896 error = xfs_bmapi_single(tp, ip, XFS_DATA_FORK, &fsb, block);
897 if (error) {
898 return error;
900 ASSERT(fsb != NULLFSBLOCK);
902 * Convert to disk address for buffer cache.
904 d = XFS_FSB_TO_DADDR(mp, fsb);
906 * Read the buffer.
908 error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, d,
909 mp->m_bsize, 0, &bp);
910 if (error) {
911 return error;
913 ASSERT(bp && !XFS_BUF_GETERROR(bp));
914 *bpp = bp;
915 return 0;
918 #ifdef DEBUG
920 * Check that the given extent (block range) is allocated already.
922 STATIC int /* error */
923 xfs_rtcheck_alloc_range(
924 xfs_mount_t *mp, /* file system mount point */
925 xfs_trans_t *tp, /* transaction pointer */
926 xfs_rtblock_t bno, /* starting block number of extent */
927 xfs_extlen_t len, /* length of extent */
928 int *stat) /* out: 1 for allocated, 0 for not */
930 xfs_rtblock_t new; /* dummy for xfs_rtcheck_range */
932 return xfs_rtcheck_range(mp, tp, bno, len, 0, &new, stat);
934 #endif
936 #ifdef DEBUG
938 * Check whether the given block in the bitmap has the given value.
940 STATIC int /* 1 for matches, 0 for not */
941 xfs_rtcheck_bit(
942 xfs_mount_t *mp, /* file system mount structure */
943 xfs_trans_t *tp, /* transaction pointer */
944 xfs_rtblock_t start, /* bit (block) to check */
945 int val) /* 1 for free, 0 for allocated */
947 int bit; /* bit number in the word */
948 xfs_rtblock_t block; /* bitmap block number */
949 xfs_buf_t *bp; /* buf for the block */
950 xfs_rtword_t *bufp; /* pointer into the buffer */
951 /* REFERENCED */
952 int error; /* error value */
953 xfs_rtword_t wdiff; /* difference between bit & expected */
954 int word; /* word number in the buffer */
955 xfs_rtword_t wval; /* word value from buffer */
957 block = XFS_BITTOBLOCK(mp, start);
958 error = xfs_rtbuf_get(mp, tp, block, 0, &bp);
959 bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
960 word = XFS_BITTOWORD(mp, start);
961 bit = (int)(start & (XFS_NBWORD - 1));
962 wval = bufp[word];
963 xfs_trans_brelse(tp, bp);
964 wdiff = (wval ^ -val) & ((xfs_rtword_t)1 << bit);
965 return !wdiff;
967 #endif /* DEBUG */
969 #if 0
971 * Check that the given extent (block range) is free already.
973 STATIC int /* error */
974 xfs_rtcheck_free_range(
975 xfs_mount_t *mp, /* file system mount point */
976 xfs_trans_t *tp, /* transaction pointer */
977 xfs_rtblock_t bno, /* starting block number of extent */
978 xfs_extlen_t len, /* length of extent */
979 int *stat) /* out: 1 for free, 0 for not */
981 xfs_rtblock_t new; /* dummy for xfs_rtcheck_range */
983 return xfs_rtcheck_range(mp, tp, bno, len, 1, &new, stat);
985 #endif
988 * Check that the given range is either all allocated (val = 0) or
989 * all free (val = 1).
991 STATIC int /* error */
992 xfs_rtcheck_range(
993 xfs_mount_t *mp, /* file system mount point */
994 xfs_trans_t *tp, /* transaction pointer */
995 xfs_rtblock_t start, /* starting block number of extent */
996 xfs_extlen_t len, /* length of extent */
997 int val, /* 1 for free, 0 for allocated */
998 xfs_rtblock_t *new, /* out: first block not matching */
999 int *stat) /* out: 1 for matches, 0 for not */
1001 xfs_rtword_t *b; /* current word in buffer */
1002 int bit; /* bit number in the word */
1003 xfs_rtblock_t block; /* bitmap block number */
1004 xfs_buf_t *bp; /* buf for the block */
1005 xfs_rtword_t *bufp; /* starting word in buffer */
1006 int error; /* error value */
1007 xfs_rtblock_t i; /* current bit number rel. to start */
1008 xfs_rtblock_t lastbit; /* last useful bit in word */
1009 xfs_rtword_t mask; /* mask of relevant bits for value */
1010 xfs_rtword_t wdiff; /* difference from wanted value */
1011 int word; /* word number in the buffer */
1014 * Compute starting bitmap block number
1016 block = XFS_BITTOBLOCK(mp, start);
1018 * Read the bitmap block.
1020 error = xfs_rtbuf_get(mp, tp, block, 0, &bp);
1021 if (error) {
1022 return error;
1024 bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1026 * Compute the starting word's address, and starting bit.
1028 word = XFS_BITTOWORD(mp, start);
1029 b = &bufp[word];
1030 bit = (int)(start & (XFS_NBWORD - 1));
1032 * 0 (allocated) => all zero's; 1 (free) => all one's.
1034 val = -val;
1036 * If not starting on a word boundary, deal with the first
1037 * (partial) word.
1039 if (bit) {
1041 * Compute first bit not examined.
1043 lastbit = XFS_RTMIN(bit + len, XFS_NBWORD);
1045 * Mask of relevant bits.
1047 mask = (((xfs_rtword_t)1 << (lastbit - bit)) - 1) << bit;
1049 * Compute difference between actual and desired value.
1051 if ((wdiff = (*b ^ val) & mask)) {
1053 * Different, compute first wrong bit and return.
1055 xfs_trans_brelse(tp, bp);
1056 i = XFS_RTLOBIT(wdiff) - bit;
1057 *new = start + i;
1058 *stat = 0;
1059 return 0;
1061 i = lastbit - bit;
1063 * Go on to next block if that's where the next word is
1064 * and we need the next word.
1066 if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
1068 * If done with this block, get the next one.
1070 xfs_trans_brelse(tp, bp);
1071 error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
1072 if (error) {
1073 return error;
1075 b = bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1076 word = 0;
1077 } else {
1079 * Go on to the next word in the buffer.
1081 b++;
1083 } else {
1085 * Starting on a word boundary, no partial word.
1087 i = 0;
1090 * Loop over whole words in buffers. When we use up one buffer
1091 * we move on to the next one.
1093 while (len - i >= XFS_NBWORD) {
1095 * Compute difference between actual and desired value.
1097 if ((wdiff = *b ^ val)) {
1099 * Different, compute first wrong bit and return.
1101 xfs_trans_brelse(tp, bp);
1102 i += XFS_RTLOBIT(wdiff);
1103 *new = start + i;
1104 *stat = 0;
1105 return 0;
1107 i += XFS_NBWORD;
1109 * Go on to next block if that's where the next word is
1110 * and we need the next word.
1112 if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
1114 * If done with this block, get the next one.
1116 xfs_trans_brelse(tp, bp);
1117 error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
1118 if (error) {
1119 return error;
1121 b = bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1122 word = 0;
1123 } else {
1125 * Go on to the next word in the buffer.
1127 b++;
1131 * If not ending on a word boundary, deal with the last
1132 * (partial) word.
1134 if ((lastbit = len - i)) {
1136 * Mask of relevant bits.
1138 mask = ((xfs_rtword_t)1 << lastbit) - 1;
1140 * Compute difference between actual and desired value.
1142 if ((wdiff = (*b ^ val) & mask)) {
1144 * Different, compute first wrong bit and return.
1146 xfs_trans_brelse(tp, bp);
1147 i += XFS_RTLOBIT(wdiff);
1148 *new = start + i;
1149 *stat = 0;
1150 return 0;
1151 } else
1152 i = len;
1155 * Successful, return.
1157 xfs_trans_brelse(tp, bp);
1158 *new = start + i;
1159 *stat = 1;
1160 return 0;
1164 * Copy and transform the summary file, given the old and new
1165 * parameters in the mount structures.
1167 STATIC int /* error */
1168 xfs_rtcopy_summary(
1169 xfs_mount_t *omp, /* old file system mount point */
1170 xfs_mount_t *nmp, /* new file system mount point */
1171 xfs_trans_t *tp) /* transaction pointer */
1173 xfs_rtblock_t bbno; /* bitmap block number */
1174 xfs_buf_t *bp; /* summary buffer */
1175 int error; /* error return value */
1176 int log; /* summary level number (log length) */
1177 xfs_suminfo_t sum; /* summary data */
1178 xfs_fsblock_t sumbno; /* summary block number */
1180 bp = NULL;
1181 for (log = omp->m_rsumlevels - 1; log >= 0; log--) {
1182 for (bbno = omp->m_sb.sb_rbmblocks - 1;
1183 (xfs_srtblock_t)bbno >= 0;
1184 bbno--) {
1185 error = xfs_rtget_summary(omp, tp, log, bbno, &bp,
1186 &sumbno, &sum);
1187 if (error)
1188 return error;
1189 if (sum == 0)
1190 continue;
1191 error = xfs_rtmodify_summary(omp, tp, log, bbno, -sum,
1192 &bp, &sumbno);
1193 if (error)
1194 return error;
1195 error = xfs_rtmodify_summary(nmp, tp, log, bbno, sum,
1196 &bp, &sumbno);
1197 if (error)
1198 return error;
1199 ASSERT(sum > 0);
1202 return 0;
1206 * Searching backward from start to limit, find the first block whose
1207 * allocated/free state is different from start's.
1209 STATIC int /* error */
1210 xfs_rtfind_back(
1211 xfs_mount_t *mp, /* file system mount point */
1212 xfs_trans_t *tp, /* transaction pointer */
1213 xfs_rtblock_t start, /* starting block to look at */
1214 xfs_rtblock_t limit, /* last block to look at */
1215 xfs_rtblock_t *rtblock) /* out: start block found */
1217 xfs_rtword_t *b; /* current word in buffer */
1218 int bit; /* bit number in the word */
1219 xfs_rtblock_t block; /* bitmap block number */
1220 xfs_buf_t *bp; /* buf for the block */
1221 xfs_rtword_t *bufp; /* starting word in buffer */
1222 int error; /* error value */
1223 xfs_rtblock_t firstbit; /* first useful bit in the word */
1224 xfs_rtblock_t i; /* current bit number rel. to start */
1225 xfs_rtblock_t len; /* length of inspected area */
1226 xfs_rtword_t mask; /* mask of relevant bits for value */
1227 xfs_rtword_t want; /* mask for "good" values */
1228 xfs_rtword_t wdiff; /* difference from wanted value */
1229 int word; /* word number in the buffer */
1232 * Compute and read in starting bitmap block for starting block.
1234 block = XFS_BITTOBLOCK(mp, start);
1235 error = xfs_rtbuf_get(mp, tp, block, 0, &bp);
1236 if (error) {
1237 return error;
1239 bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1241 * Get the first word's index & point to it.
1243 word = XFS_BITTOWORD(mp, start);
1244 b = &bufp[word];
1245 bit = (int)(start & (XFS_NBWORD - 1));
1246 len = start - limit + 1;
1248 * Compute match value, based on the bit at start: if 1 (free)
1249 * then all-ones, else all-zeroes.
1251 want = (*b & ((xfs_rtword_t)1 << bit)) ? -1 : 0;
1253 * If the starting position is not word-aligned, deal with the
1254 * partial word.
1256 if (bit < XFS_NBWORD - 1) {
1258 * Calculate first (leftmost) bit number to look at,
1259 * and mask for all the relevant bits in this word.
1261 firstbit = XFS_RTMAX((xfs_srtblock_t)(bit - len + 1), 0);
1262 mask = (((xfs_rtword_t)1 << (bit - firstbit + 1)) - 1) <<
1263 firstbit;
1265 * Calculate the difference between the value there
1266 * and what we're looking for.
1268 if ((wdiff = (*b ^ want) & mask)) {
1270 * Different. Mark where we are and return.
1272 xfs_trans_brelse(tp, bp);
1273 i = bit - XFS_RTHIBIT(wdiff);
1274 *rtblock = start - i + 1;
1275 return 0;
1277 i = bit - firstbit + 1;
1279 * Go on to previous block if that's where the previous word is
1280 * and we need the previous word.
1282 if (--word == -1 && i < len) {
1284 * If done with this block, get the previous one.
1286 xfs_trans_brelse(tp, bp);
1287 error = xfs_rtbuf_get(mp, tp, --block, 0, &bp);
1288 if (error) {
1289 return error;
1291 bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1292 word = XFS_BLOCKWMASK(mp);
1293 b = &bufp[word];
1294 } else {
1296 * Go on to the previous word in the buffer.
1298 b--;
1300 } else {
1302 * Starting on a word boundary, no partial word.
1304 i = 0;
1307 * Loop over whole words in buffers. When we use up one buffer
1308 * we move on to the previous one.
1310 while (len - i >= XFS_NBWORD) {
1312 * Compute difference between actual and desired value.
1314 if ((wdiff = *b ^ want)) {
1316 * Different, mark where we are and return.
1318 xfs_trans_brelse(tp, bp);
1319 i += XFS_NBWORD - 1 - XFS_RTHIBIT(wdiff);
1320 *rtblock = start - i + 1;
1321 return 0;
1323 i += XFS_NBWORD;
1325 * Go on to previous block if that's where the previous word is
1326 * and we need the previous word.
1328 if (--word == -1 && i < len) {
1330 * If done with this block, get the previous one.
1332 xfs_trans_brelse(tp, bp);
1333 error = xfs_rtbuf_get(mp, tp, --block, 0, &bp);
1334 if (error) {
1335 return error;
1337 bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1338 word = XFS_BLOCKWMASK(mp);
1339 b = &bufp[word];
1340 } else {
1342 * Go on to the previous word in the buffer.
1344 b--;
1348 * If not ending on a word boundary, deal with the last
1349 * (partial) word.
1351 if (len - i) {
1353 * Calculate first (leftmost) bit number to look at,
1354 * and mask for all the relevant bits in this word.
1356 firstbit = XFS_NBWORD - (len - i);
1357 mask = (((xfs_rtword_t)1 << (len - i)) - 1) << firstbit;
1359 * Compute difference between actual and desired value.
1361 if ((wdiff = (*b ^ want) & mask)) {
1363 * Different, mark where we are and return.
1365 xfs_trans_brelse(tp, bp);
1366 i += XFS_NBWORD - 1 - XFS_RTHIBIT(wdiff);
1367 *rtblock = start - i + 1;
1368 return 0;
1369 } else
1370 i = len;
1373 * No match, return that we scanned the whole area.
1375 xfs_trans_brelse(tp, bp);
1376 *rtblock = start - i + 1;
1377 return 0;
1381 * Searching forward from start to limit, find the first block whose
1382 * allocated/free state is different from start's.
1384 STATIC int /* error */
1385 xfs_rtfind_forw(
1386 xfs_mount_t *mp, /* file system mount point */
1387 xfs_trans_t *tp, /* transaction pointer */
1388 xfs_rtblock_t start, /* starting block to look at */
1389 xfs_rtblock_t limit, /* last block to look at */
1390 xfs_rtblock_t *rtblock) /* out: start block found */
1392 xfs_rtword_t *b; /* current word in buffer */
1393 int bit; /* bit number in the word */
1394 xfs_rtblock_t block; /* bitmap block number */
1395 xfs_buf_t *bp; /* buf for the block */
1396 xfs_rtword_t *bufp; /* starting word in buffer */
1397 int error; /* error value */
1398 xfs_rtblock_t i; /* current bit number rel. to start */
1399 xfs_rtblock_t lastbit; /* last useful bit in the word */
1400 xfs_rtblock_t len; /* length of inspected area */
1401 xfs_rtword_t mask; /* mask of relevant bits for value */
1402 xfs_rtword_t want; /* mask for "good" values */
1403 xfs_rtword_t wdiff; /* difference from wanted value */
1404 int word; /* word number in the buffer */
1407 * Compute and read in starting bitmap block for starting block.
1409 block = XFS_BITTOBLOCK(mp, start);
1410 error = xfs_rtbuf_get(mp, tp, block, 0, &bp);
1411 if (error) {
1412 return error;
1414 bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1416 * Get the first word's index & point to it.
1418 word = XFS_BITTOWORD(mp, start);
1419 b = &bufp[word];
1420 bit = (int)(start & (XFS_NBWORD - 1));
1421 len = limit - start + 1;
1423 * Compute match value, based on the bit at start: if 1 (free)
1424 * then all-ones, else all-zeroes.
1426 want = (*b & ((xfs_rtword_t)1 << bit)) ? -1 : 0;
1428 * If the starting position is not word-aligned, deal with the
1429 * partial word.
1431 if (bit) {
1433 * Calculate last (rightmost) bit number to look at,
1434 * and mask for all the relevant bits in this word.
1436 lastbit = XFS_RTMIN(bit + len, XFS_NBWORD);
1437 mask = (((xfs_rtword_t)1 << (lastbit - bit)) - 1) << bit;
1439 * Calculate the difference between the value there
1440 * and what we're looking for.
1442 if ((wdiff = (*b ^ want) & mask)) {
1444 * Different. Mark where we are and return.
1446 xfs_trans_brelse(tp, bp);
1447 i = XFS_RTLOBIT(wdiff) - bit;
1448 *rtblock = start + i - 1;
1449 return 0;
1451 i = lastbit - bit;
1453 * Go on to next block if that's where the next word is
1454 * and we need the next word.
1456 if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
1458 * If done with this block, get the previous one.
1460 xfs_trans_brelse(tp, bp);
1461 error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
1462 if (error) {
1463 return error;
1465 b = bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1466 word = 0;
1467 } else {
1469 * Go on to the previous word in the buffer.
1471 b++;
1473 } else {
1475 * Starting on a word boundary, no partial word.
1477 i = 0;
1480 * Loop over whole words in buffers. When we use up one buffer
1481 * we move on to the next one.
1483 while (len - i >= XFS_NBWORD) {
1485 * Compute difference between actual and desired value.
1487 if ((wdiff = *b ^ want)) {
1489 * Different, mark where we are and return.
1491 xfs_trans_brelse(tp, bp);
1492 i += XFS_RTLOBIT(wdiff);
1493 *rtblock = start + i - 1;
1494 return 0;
1496 i += XFS_NBWORD;
1498 * Go on to next block if that's where the next word is
1499 * and we need the next word.
1501 if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
1503 * If done with this block, get the next one.
1505 xfs_trans_brelse(tp, bp);
1506 error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
1507 if (error) {
1508 return error;
1510 b = bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1511 word = 0;
1512 } else {
1514 * Go on to the next word in the buffer.
1516 b++;
1520 * If not ending on a word boundary, deal with the last
1521 * (partial) word.
1523 if ((lastbit = len - i)) {
1525 * Calculate mask for all the relevant bits in this word.
1527 mask = ((xfs_rtword_t)1 << lastbit) - 1;
1529 * Compute difference between actual and desired value.
1531 if ((wdiff = (*b ^ want) & mask)) {
1533 * Different, mark where we are and return.
1535 xfs_trans_brelse(tp, bp);
1536 i += XFS_RTLOBIT(wdiff);
1537 *rtblock = start + i - 1;
1538 return 0;
1539 } else
1540 i = len;
1543 * No match, return that we scanned the whole area.
1545 xfs_trans_brelse(tp, bp);
1546 *rtblock = start + i - 1;
1547 return 0;
1551 * Mark an extent specified by start and len freed.
1552 * Updates all the summary information as well as the bitmap.
1554 STATIC int /* error */
1555 xfs_rtfree_range(
1556 xfs_mount_t *mp, /* file system mount point */
1557 xfs_trans_t *tp, /* transaction pointer */
1558 xfs_rtblock_t start, /* starting block to free */
1559 xfs_extlen_t len, /* length to free */
1560 xfs_buf_t **rbpp, /* in/out: summary block buffer */
1561 xfs_fsblock_t *rsb) /* in/out: summary block number */
1563 xfs_rtblock_t end; /* end of the freed extent */
1564 int error; /* error value */
1565 xfs_rtblock_t postblock; /* first block freed > end */
1566 xfs_rtblock_t preblock; /* first block freed < start */
1568 end = start + len - 1;
1570 * Modify the bitmap to mark this extent freed.
1572 error = xfs_rtmodify_range(mp, tp, start, len, 1);
1573 if (error) {
1574 return error;
1577 * Assume we're freeing out of the middle of an allocated extent.
1578 * We need to find the beginning and end of the extent so we can
1579 * properly update the summary.
1581 error = xfs_rtfind_back(mp, tp, start, 0, &preblock);
1582 if (error) {
1583 return error;
1586 * Find the next allocated block (end of allocated extent).
1588 error = xfs_rtfind_forw(mp, tp, end, mp->m_sb.sb_rextents - 1,
1589 &postblock);
1591 * If there are blocks not being freed at the front of the
1592 * old extent, add summary data for them to be allocated.
1594 if (preblock < start) {
1595 error = xfs_rtmodify_summary(mp, tp,
1596 XFS_RTBLOCKLOG(start - preblock),
1597 XFS_BITTOBLOCK(mp, preblock), -1, rbpp, rsb);
1598 if (error) {
1599 return error;
1603 * If there are blocks not being freed at the end of the
1604 * old extent, add summary data for them to be allocated.
1606 if (postblock > end) {
1607 error = xfs_rtmodify_summary(mp, tp,
1608 XFS_RTBLOCKLOG(postblock - end),
1609 XFS_BITTOBLOCK(mp, end + 1), -1, rbpp, rsb);
1610 if (error) {
1611 return error;
1615 * Increment the summary information corresponding to the entire
1616 * (new) free extent.
1618 error = xfs_rtmodify_summary(mp, tp,
1619 XFS_RTBLOCKLOG(postblock + 1 - preblock),
1620 XFS_BITTOBLOCK(mp, preblock), 1, rbpp, rsb);
1621 return error;
1625 * Read and return the summary information for a given extent size,
1626 * bitmap block combination.
1627 * Keeps track of a current summary block, so we don't keep reading
1628 * it from the buffer cache.
1630 STATIC int /* error */
1631 xfs_rtget_summary(
1632 xfs_mount_t *mp, /* file system mount structure */
1633 xfs_trans_t *tp, /* transaction pointer */
1634 int log, /* log2 of extent size */
1635 xfs_rtblock_t bbno, /* bitmap block number */
1636 xfs_buf_t **rbpp, /* in/out: summary block buffer */
1637 xfs_fsblock_t *rsb, /* in/out: summary block number */
1638 xfs_suminfo_t *sum) /* out: summary info for this block */
1640 xfs_buf_t *bp; /* buffer for summary block */
1641 int error; /* error value */
1642 xfs_fsblock_t sb; /* summary fsblock */
1643 int so; /* index into the summary file */
1644 xfs_suminfo_t *sp; /* pointer to returned data */
1647 * Compute entry number in the summary file.
1649 so = XFS_SUMOFFS(mp, log, bbno);
1651 * Compute the block number in the summary file.
1653 sb = XFS_SUMOFFSTOBLOCK(mp, so);
1655 * If we have an old buffer, and the block number matches, use that.
1657 if (rbpp && *rbpp && *rsb == sb)
1658 bp = *rbpp;
1660 * Otherwise we have to get the buffer.
1662 else {
1664 * If there was an old one, get rid of it first.
1666 if (rbpp && *rbpp)
1667 xfs_trans_brelse(tp, *rbpp);
1668 error = xfs_rtbuf_get(mp, tp, sb, 1, &bp);
1669 if (error) {
1670 return error;
1673 * Remember this buffer and block for the next call.
1675 if (rbpp) {
1676 *rbpp = bp;
1677 *rsb = sb;
1681 * Point to the summary information & copy it out.
1683 sp = XFS_SUMPTR(mp, bp, so);
1684 *sum = *sp;
1686 * Drop the buffer if we're not asked to remember it.
1688 if (!rbpp)
1689 xfs_trans_brelse(tp, bp);
1690 return 0;
1694 * Set the given range of bitmap bits to the given value.
1695 * Do whatever I/O and logging is required.
1697 STATIC int /* error */
1698 xfs_rtmodify_range(
1699 xfs_mount_t *mp, /* file system mount point */
1700 xfs_trans_t *tp, /* transaction pointer */
1701 xfs_rtblock_t start, /* starting block to modify */
1702 xfs_extlen_t len, /* length of extent to modify */
1703 int val) /* 1 for free, 0 for allocated */
1705 xfs_rtword_t *b; /* current word in buffer */
1706 int bit; /* bit number in the word */
1707 xfs_rtblock_t block; /* bitmap block number */
1708 xfs_buf_t *bp; /* buf for the block */
1709 xfs_rtword_t *bufp; /* starting word in buffer */
1710 int error; /* error value */
1711 xfs_rtword_t *first; /* first used word in the buffer */
1712 int i; /* current bit number rel. to start */
1713 int lastbit; /* last useful bit in word */
1714 xfs_rtword_t mask; /* mask o frelevant bits for value */
1715 int word; /* word number in the buffer */
1718 * Compute starting bitmap block number.
1720 block = XFS_BITTOBLOCK(mp, start);
1722 * Read the bitmap block, and point to its data.
1724 error = xfs_rtbuf_get(mp, tp, block, 0, &bp);
1725 if (error) {
1726 return error;
1728 bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1730 * Compute the starting word's address, and starting bit.
1732 word = XFS_BITTOWORD(mp, start);
1733 first = b = &bufp[word];
1734 bit = (int)(start & (XFS_NBWORD - 1));
1736 * 0 (allocated) => all zeroes; 1 (free) => all ones.
1738 val = -val;
1740 * If not starting on a word boundary, deal with the first
1741 * (partial) word.
1743 if (bit) {
1745 * Compute first bit not changed and mask of relevant bits.
1747 lastbit = XFS_RTMIN(bit + len, XFS_NBWORD);
1748 mask = (((xfs_rtword_t)1 << (lastbit - bit)) - 1) << bit;
1750 * Set/clear the active bits.
1752 if (val)
1753 *b |= mask;
1754 else
1755 *b &= ~mask;
1756 i = lastbit - bit;
1758 * Go on to the next block if that's where the next word is
1759 * and we need the next word.
1761 if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
1763 * Log the changed part of this block.
1764 * Get the next one.
1766 xfs_trans_log_buf(tp, bp,
1767 (uint)((char *)first - (char *)bufp),
1768 (uint)((char *)b - (char *)bufp));
1769 error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
1770 if (error) {
1771 return error;
1773 first = b = bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1774 word = 0;
1775 } else {
1777 * Go on to the next word in the buffer
1779 b++;
1781 } else {
1783 * Starting on a word boundary, no partial word.
1785 i = 0;
1788 * Loop over whole words in buffers. When we use up one buffer
1789 * we move on to the next one.
1791 while (len - i >= XFS_NBWORD) {
1793 * Set the word value correctly.
1795 *b = val;
1796 i += XFS_NBWORD;
1798 * Go on to the next block if that's where the next word is
1799 * and we need the next word.
1801 if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
1803 * Log the changed part of this block.
1804 * Get the next one.
1806 xfs_trans_log_buf(tp, bp,
1807 (uint)((char *)first - (char *)bufp),
1808 (uint)((char *)b - (char *)bufp));
1809 error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
1810 if (error) {
1811 return error;
1813 first = b = bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1814 word = 0;
1815 } else {
1817 * Go on to the next word in the buffer
1819 b++;
1823 * If not ending on a word boundary, deal with the last
1824 * (partial) word.
1826 if ((lastbit = len - i)) {
1828 * Compute a mask of relevant bits.
1830 bit = 0;
1831 mask = ((xfs_rtword_t)1 << lastbit) - 1;
1833 * Set/clear the active bits.
1835 if (val)
1836 *b |= mask;
1837 else
1838 *b &= ~mask;
1839 b++;
1842 * Log any remaining changed bytes.
1844 if (b > first)
1845 xfs_trans_log_buf(tp, bp, (uint)((char *)first - (char *)bufp),
1846 (uint)((char *)b - (char *)bufp - 1));
1847 return 0;
1851 * Read and modify the summary information for a given extent size,
1852 * bitmap block combination.
1853 * Keeps track of a current summary block, so we don't keep reading
1854 * it from the buffer cache.
1856 STATIC int /* error */
1857 xfs_rtmodify_summary(
1858 xfs_mount_t *mp, /* file system mount point */
1859 xfs_trans_t *tp, /* transaction pointer */
1860 int log, /* log2 of extent size */
1861 xfs_rtblock_t bbno, /* bitmap block number */
1862 int delta, /* change to make to summary info */
1863 xfs_buf_t **rbpp, /* in/out: summary block buffer */
1864 xfs_fsblock_t *rsb) /* in/out: summary block number */
1866 xfs_buf_t *bp; /* buffer for the summary block */
1867 int error; /* error value */
1868 xfs_fsblock_t sb; /* summary fsblock */
1869 int so; /* index into the summary file */
1870 xfs_suminfo_t *sp; /* pointer to returned data */
1873 * Compute entry number in the summary file.
1875 so = XFS_SUMOFFS(mp, log, bbno);
1877 * Compute the block number in the summary file.
1879 sb = XFS_SUMOFFSTOBLOCK(mp, so);
1881 * If we have an old buffer, and the block number matches, use that.
1883 if (rbpp && *rbpp && *rsb == sb)
1884 bp = *rbpp;
1886 * Otherwise we have to get the buffer.
1888 else {
1890 * If there was an old one, get rid of it first.
1892 if (rbpp && *rbpp)
1893 xfs_trans_brelse(tp, *rbpp);
1894 error = xfs_rtbuf_get(mp, tp, sb, 1, &bp);
1895 if (error) {
1896 return error;
1899 * Remember this buffer and block for the next call.
1901 if (rbpp) {
1902 *rbpp = bp;
1903 *rsb = sb;
1907 * Point to the summary information, modify and log it.
1909 sp = XFS_SUMPTR(mp, bp, so);
1910 *sp += delta;
1911 xfs_trans_log_buf(tp, bp, (uint)((char *)sp - (char *)XFS_BUF_PTR(bp)),
1912 (uint)((char *)sp - (char *)XFS_BUF_PTR(bp) + sizeof(*sp) - 1));
1913 return 0;
1917 * Visible (exported) functions.
1921 * Grow the realtime area of the filesystem.
1924 xfs_growfs_rt(
1925 xfs_mount_t *mp, /* mount point for filesystem */
1926 xfs_growfs_rt_t *in) /* growfs rt input struct */
1928 xfs_rtblock_t bmbno; /* bitmap block number */
1929 xfs_buf_t *bp; /* temporary buffer */
1930 int cancelflags; /* flags for xfs_trans_cancel */
1931 int error; /* error return value */
1932 xfs_inode_t *ip; /* bitmap inode, used as lock */
1933 xfs_mount_t *nmp; /* new (fake) mount structure */
1934 xfs_drfsbno_t nrblocks; /* new number of realtime blocks */
1935 xfs_extlen_t nrbmblocks; /* new number of rt bitmap blocks */
1936 xfs_drtbno_t nrextents; /* new number of realtime extents */
1937 uint8_t nrextslog; /* new log2 of sb_rextents */
1938 xfs_extlen_t nrsumblocks; /* new number of summary blocks */
1939 uint nrsumlevels; /* new rt summary levels */
1940 uint nrsumsize; /* new size of rt summary, bytes */
1941 xfs_sb_t *nsbp; /* new superblock */
1942 xfs_extlen_t rbmblocks; /* current number of rt bitmap blocks */
1943 xfs_extlen_t rsumblocks; /* current number of rt summary blks */
1944 xfs_sb_t *sbp; /* old superblock */
1945 xfs_fsblock_t sumbno; /* summary block number */
1946 xfs_trans_t *tp; /* transaction pointer */
1948 sbp = &mp->m_sb;
1950 * Initial error checking.
1952 if (mp->m_rtdev_targp || mp->m_rbmip == NULL ||
1953 (nrblocks = in->newblocks) <= sbp->sb_rblocks ||
1954 (sbp->sb_rblocks && (in->extsize != sbp->sb_rextsize)))
1955 return XFS_ERROR(EINVAL);
1957 * Read in the last block of the device, make sure it exists.
1959 error = xfs_read_buf(mp, mp->m_rtdev_targp,
1960 XFS_FSB_TO_BB(mp, in->newblocks - 1),
1961 XFS_FSB_TO_BB(mp, 1), 0, &bp);
1962 if (error)
1963 return error;
1964 ASSERT(bp);
1965 xfs_buf_relse(bp);
1967 * Calculate new parameters. These are the final values to be reached.
1969 nrextents = nrblocks;
1970 do_div(nrextents, in->extsize);
1971 nrbmblocks = roundup_64(nrextents, NBBY * sbp->sb_blocksize);
1972 nrextslog = xfs_highbit32(nrextents);
1973 nrsumlevels = nrextslog + 1;
1974 nrsumsize = (uint)sizeof(xfs_suminfo_t) * nrsumlevels * nrbmblocks;
1975 nrsumblocks = XFS_B_TO_FSB(mp, nrsumsize);
1976 nrsumsize = XFS_FSB_TO_B(mp, nrsumblocks);
1978 * New summary size can't be more than half the size of
1979 * the log. This prevents us from getting a log overflow,
1980 * since we'll log basically the whole summary file at once.
1982 if (nrsumblocks > (mp->m_sb.sb_logblocks >> 1))
1983 return XFS_ERROR(EINVAL);
1985 * Get the old block counts for bitmap and summary inodes.
1986 * These can't change since other growfs callers are locked out.
1988 rbmblocks = XFS_B_TO_FSB(mp, mp->m_rbmip->i_d.di_size);
1989 rsumblocks = XFS_B_TO_FSB(mp, mp->m_rsumip->i_d.di_size);
1991 * Allocate space to the bitmap and summary files, as necessary.
1993 if ((error = xfs_growfs_rt_alloc(mp, rbmblocks, nrbmblocks,
1994 mp->m_sb.sb_rbmino)))
1995 return error;
1996 if ((error = xfs_growfs_rt_alloc(mp, rsumblocks, nrsumblocks,
1997 mp->m_sb.sb_rsumino)))
1998 return error;
1999 nmp = NULL;
2001 * Loop over the bitmap blocks.
2002 * We will do everything one bitmap block at a time.
2003 * Skip the current block if it is exactly full.
2004 * This also deals with the case where there were no rtextents before.
2006 for (bmbno = sbp->sb_rbmblocks -
2007 ((sbp->sb_rextents & ((1 << mp->m_blkbit_log) - 1)) != 0);
2008 bmbno < nrbmblocks;
2009 bmbno++) {
2011 * Allocate a new (fake) mount/sb.
2013 nmp = kmem_alloc(sizeof(*nmp), KM_SLEEP);
2014 *nmp = *mp;
2015 nsbp = &nmp->m_sb;
2017 * Calculate new sb and mount fields for this round.
2019 nsbp->sb_rextsize = in->extsize;
2020 nsbp->sb_rbmblocks = bmbno + 1;
2021 nsbp->sb_rblocks =
2022 XFS_RTMIN(nrblocks,
2023 nsbp->sb_rbmblocks * NBBY *
2024 nsbp->sb_blocksize * nsbp->sb_rextsize);
2025 nsbp->sb_rextents = nsbp->sb_rblocks;
2026 do_div(nsbp->sb_rextents, nsbp->sb_rextsize);
2027 nsbp->sb_rextslog = xfs_highbit32(nsbp->sb_rextents);
2028 nrsumlevels = nmp->m_rsumlevels = nsbp->sb_rextslog + 1;
2029 nrsumsize =
2030 (uint)sizeof(xfs_suminfo_t) * nrsumlevels *
2031 nsbp->sb_rbmblocks;
2032 nrsumblocks = XFS_B_TO_FSB(mp, nrsumsize);
2033 nmp->m_rsumsize = nrsumsize = XFS_FSB_TO_B(mp, nrsumblocks);
2035 * Start a transaction, get the log reservation.
2037 tp = xfs_trans_alloc(mp, XFS_TRANS_GROWFSRT_FREE);
2038 cancelflags = 0;
2039 if ((error = xfs_trans_reserve(tp, 0,
2040 XFS_GROWRTFREE_LOG_RES(nmp), 0, 0, 0)))
2041 goto error_exit;
2043 * Lock out other callers by grabbing the bitmap inode lock.
2045 if ((error = xfs_trans_iget(mp, tp, 0, mp->m_sb.sb_rbmino,
2046 XFS_ILOCK_EXCL, &ip)))
2047 goto error_exit;
2048 ASSERT(ip == mp->m_rbmip);
2050 * Update the bitmap inode's size.
2052 mp->m_rbmip->i_d.di_size =
2053 nsbp->sb_rbmblocks * nsbp->sb_blocksize;
2054 xfs_trans_log_inode(tp, mp->m_rbmip, XFS_ILOG_CORE);
2055 cancelflags |= XFS_TRANS_ABORT;
2057 * Get the summary inode into the transaction.
2059 if ((error = xfs_trans_iget(mp, tp, mp->m_sb.sb_rsumino,
2060 0, XFS_ILOCK_EXCL, &ip)))
2061 goto error_exit;
2062 ASSERT(ip == mp->m_rsumip);
2064 * Update the summary inode's size.
2066 mp->m_rsumip->i_d.di_size = nmp->m_rsumsize;
2067 xfs_trans_log_inode(tp, mp->m_rsumip, XFS_ILOG_CORE);
2069 * Copy summary data from old to new sizes.
2070 * Do this when the real size (not block-aligned) changes.
2072 if (sbp->sb_rbmblocks != nsbp->sb_rbmblocks ||
2073 mp->m_rsumlevels != nmp->m_rsumlevels) {
2074 error = xfs_rtcopy_summary(mp, nmp, tp);
2075 if (error)
2076 goto error_exit;
2079 * Update superblock fields.
2081 if (nsbp->sb_rextsize != sbp->sb_rextsize)
2082 xfs_trans_mod_sb(tp, XFS_TRANS_SB_REXTSIZE,
2083 nsbp->sb_rextsize - sbp->sb_rextsize);
2084 if (nsbp->sb_rbmblocks != sbp->sb_rbmblocks)
2085 xfs_trans_mod_sb(tp, XFS_TRANS_SB_RBMBLOCKS,
2086 nsbp->sb_rbmblocks - sbp->sb_rbmblocks);
2087 if (nsbp->sb_rblocks != sbp->sb_rblocks)
2088 xfs_trans_mod_sb(tp, XFS_TRANS_SB_RBLOCKS,
2089 nsbp->sb_rblocks - sbp->sb_rblocks);
2090 if (nsbp->sb_rextents != sbp->sb_rextents)
2091 xfs_trans_mod_sb(tp, XFS_TRANS_SB_REXTENTS,
2092 nsbp->sb_rextents - sbp->sb_rextents);
2093 if (nsbp->sb_rextslog != sbp->sb_rextslog)
2094 xfs_trans_mod_sb(tp, XFS_TRANS_SB_REXTSLOG,
2095 nsbp->sb_rextslog - sbp->sb_rextslog);
2097 * Free new extent.
2099 bp = NULL;
2100 error = xfs_rtfree_range(nmp, tp, sbp->sb_rextents,
2101 nsbp->sb_rextents - sbp->sb_rextents, &bp, &sumbno);
2102 if (error)
2103 goto error_exit;
2105 * Mark more blocks free in the superblock.
2107 xfs_trans_mod_sb(tp, XFS_TRANS_SB_FREXTENTS,
2108 nsbp->sb_rextents - sbp->sb_rextents);
2110 * Free the fake mp structure.
2112 kmem_free(nmp, sizeof(*nmp));
2113 nmp = NULL;
2115 * Update mp values into the real mp structure.
2117 mp->m_rsumlevels = nrsumlevels;
2118 mp->m_rsumsize = nrsumsize;
2120 * Commit the transaction.
2122 xfs_trans_commit(tp, 0, NULL);
2124 return 0;
2127 * Error paths come here.
2129 error_exit:
2130 if (nmp)
2131 kmem_free(nmp, sizeof(*nmp));
2132 xfs_trans_cancel(tp, cancelflags);
2133 return error;
2137 * Allocate an extent in the realtime subvolume, with the usual allocation
2138 * parameters. The length units are all in realtime extents, as is the
2139 * result block number.
2141 int /* error */
2142 xfs_rtallocate_extent(
2143 xfs_trans_t *tp, /* transaction pointer */
2144 xfs_rtblock_t bno, /* starting block number to allocate */
2145 xfs_extlen_t minlen, /* minimum length to allocate */
2146 xfs_extlen_t maxlen, /* maximum length to allocate */
2147 xfs_extlen_t *len, /* out: actual length allocated */
2148 xfs_alloctype_t type, /* allocation type XFS_ALLOCTYPE... */
2149 int wasdel, /* was a delayed allocation extent */
2150 xfs_extlen_t prod, /* extent product factor */
2151 xfs_rtblock_t *rtblock) /* out: start block allocated */
2153 int error; /* error value */
2154 xfs_inode_t *ip; /* inode for bitmap file */
2155 xfs_mount_t *mp; /* file system mount structure */
2156 xfs_rtblock_t r; /* result allocated block */
2157 xfs_fsblock_t sb; /* summary file block number */
2158 xfs_buf_t *sumbp; /* summary file block buffer */
2160 ASSERT(minlen > 0 && minlen <= maxlen);
2161 mp = tp->t_mountp;
2163 * If prod is set then figure out what to do to minlen and maxlen.
2165 if (prod > 1) {
2166 xfs_extlen_t i;
2168 if ((i = maxlen % prod))
2169 maxlen -= i;
2170 if ((i = minlen % prod))
2171 minlen += prod - i;
2172 if (maxlen < minlen) {
2173 *rtblock = NULLRTBLOCK;
2174 return 0;
2178 * Lock out other callers by grabbing the bitmap inode lock.
2180 error = xfs_trans_iget(mp, tp, mp->m_sb.sb_rbmino, 0, XFS_ILOCK_EXCL, &ip);
2181 if (error) {
2182 return error;
2184 sumbp = NULL;
2186 * Allocate by size, or near another block, or exactly at some block.
2188 switch (type) {
2189 case XFS_ALLOCTYPE_ANY_AG:
2190 error = xfs_rtallocate_extent_size(mp, tp, minlen, maxlen, len,
2191 &sumbp, &sb, prod, &r);
2192 break;
2193 case XFS_ALLOCTYPE_NEAR_BNO:
2194 error = xfs_rtallocate_extent_near(mp, tp, bno, minlen, maxlen,
2195 len, &sumbp, &sb, prod, &r);
2196 break;
2197 case XFS_ALLOCTYPE_THIS_BNO:
2198 error = xfs_rtallocate_extent_exact(mp, tp, bno, minlen, maxlen,
2199 len, &sumbp, &sb, prod, &r);
2200 break;
2201 default:
2202 ASSERT(0);
2204 if (error) {
2205 return error;
2208 * If it worked, update the superblock.
2210 if (r != NULLRTBLOCK) {
2211 long slen = (long)*len;
2213 ASSERT(*len >= minlen && *len <= maxlen);
2214 if (wasdel)
2215 xfs_trans_mod_sb(tp, XFS_TRANS_SB_RES_FREXTENTS, -slen);
2216 else
2217 xfs_trans_mod_sb(tp, XFS_TRANS_SB_FREXTENTS, -slen);
2219 *rtblock = r;
2220 return 0;
2224 * Free an extent in the realtime subvolume. Length is expressed in
2225 * realtime extents, as is the block number.
2227 int /* error */
2228 xfs_rtfree_extent(
2229 xfs_trans_t *tp, /* transaction pointer */
2230 xfs_rtblock_t bno, /* starting block number to free */
2231 xfs_extlen_t len) /* length of extent freed */
2233 int error; /* error value */
2234 xfs_inode_t *ip; /* bitmap file inode */
2235 xfs_mount_t *mp; /* file system mount structure */
2236 xfs_fsblock_t sb; /* summary file block number */
2237 xfs_buf_t *sumbp; /* summary file block buffer */
2239 mp = tp->t_mountp;
2241 * Synchronize by locking the bitmap inode.
2243 error = xfs_trans_iget(mp, tp, mp->m_sb.sb_rbmino, 0, XFS_ILOCK_EXCL, &ip);
2244 if (error) {
2245 return error;
2247 #if defined(__KERNEL__) && defined(DEBUG)
2249 * Check to see that this whole range is currently allocated.
2252 int stat; /* result from checking range */
2254 error = xfs_rtcheck_alloc_range(mp, tp, bno, len, &stat);
2255 if (error) {
2256 return error;
2258 ASSERT(stat);
2260 #endif
2261 sumbp = NULL;
2263 * Free the range of realtime blocks.
2265 error = xfs_rtfree_range(mp, tp, bno, len, &sumbp, &sb);
2266 if (error) {
2267 return error;
2270 * Mark more blocks free in the superblock.
2272 xfs_trans_mod_sb(tp, XFS_TRANS_SB_FREXTENTS, (long)len);
2274 * If we've now freed all the blocks, reset the file sequence
2275 * number to 0.
2277 if (tp->t_frextents_delta + mp->m_sb.sb_frextents ==
2278 mp->m_sb.sb_rextents) {
2279 if (!(ip->i_d.di_flags & XFS_DIFLAG_NEWRTBM))
2280 ip->i_d.di_flags |= XFS_DIFLAG_NEWRTBM;
2281 *(__uint64_t *)&ip->i_d.di_atime = 0;
2282 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
2284 return 0;
2288 * Initialize realtime fields in the mount structure.
2290 int /* error */
2291 xfs_rtmount_init(
2292 xfs_mount_t *mp) /* file system mount structure */
2294 xfs_buf_t *bp; /* buffer for last block of subvolume */
2295 xfs_daddr_t d; /* address of last block of subvolume */
2296 int error; /* error return value */
2297 xfs_sb_t *sbp; /* filesystem superblock copy in mount */
2299 sbp = &mp->m_sb;
2300 if (sbp->sb_rblocks == 0)
2301 return 0;
2302 if (mp->m_rtdev_targp == NULL) {
2303 cmn_err(CE_WARN,
2304 "XFS: This filesystem has a realtime volume, use rtdev=device option");
2305 return XFS_ERROR(ENODEV);
2307 mp->m_rsumlevels = sbp->sb_rextslog + 1;
2308 mp->m_rsumsize =
2309 (uint)sizeof(xfs_suminfo_t) * mp->m_rsumlevels *
2310 sbp->sb_rbmblocks;
2311 mp->m_rsumsize = roundup(mp->m_rsumsize, sbp->sb_blocksize);
2312 mp->m_rbmip = mp->m_rsumip = NULL;
2314 * Check that the realtime section is an ok size.
2316 d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_rblocks);
2317 if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_rblocks) {
2318 cmn_err(CE_WARN, "XFS: realtime mount -- %llu != %llu",
2319 (unsigned long long) XFS_BB_TO_FSB(mp, d),
2320 (unsigned long long) mp->m_sb.sb_rblocks);
2321 return XFS_ERROR(E2BIG);
2323 error = xfs_read_buf(mp, mp->m_rtdev_targp,
2324 d - XFS_FSB_TO_BB(mp, 1),
2325 XFS_FSB_TO_BB(mp, 1), 0, &bp);
2326 if (error) {
2327 cmn_err(CE_WARN,
2328 "XFS: realtime mount -- xfs_read_buf failed, returned %d", error);
2329 if (error == ENOSPC)
2330 return XFS_ERROR(E2BIG);
2331 return error;
2333 xfs_buf_relse(bp);
2334 return 0;
2338 * Get the bitmap and summary inodes into the mount structure
2339 * at mount time.
2341 int /* error */
2342 xfs_rtmount_inodes(
2343 xfs_mount_t *mp) /* file system mount structure */
2345 int error; /* error return value */
2346 xfs_sb_t *sbp;
2348 sbp = &mp->m_sb;
2349 if (sbp->sb_rbmino == NULLFSINO)
2350 return 0;
2351 error = xfs_iget(mp, NULL, sbp->sb_rbmino, 0, 0, &mp->m_rbmip, 0);
2352 if (error)
2353 return error;
2354 ASSERT(mp->m_rbmip != NULL);
2355 ASSERT(sbp->sb_rsumino != NULLFSINO);
2356 error = xfs_iget(mp, NULL, sbp->sb_rsumino, 0, 0, &mp->m_rsumip, 0);
2357 if (error) {
2358 VN_RELE(XFS_ITOV(mp->m_rbmip));
2359 return error;
2361 ASSERT(mp->m_rsumip != NULL);
2362 return 0;
2366 * Pick an extent for allocation at the start of a new realtime file.
2367 * Use the sequence number stored in the atime field of the bitmap inode.
2368 * Translate this to a fraction of the rtextents, and return the product
2369 * of rtextents and the fraction.
2370 * The fraction sequence is 0, 1/2, 1/4, 3/4, 1/8, ..., 7/8, 1/16, ...
2372 int /* error */
2373 xfs_rtpick_extent(
2374 xfs_mount_t *mp, /* file system mount point */
2375 xfs_trans_t *tp, /* transaction pointer */
2376 xfs_extlen_t len, /* allocation length (rtextents) */
2377 xfs_rtblock_t *pick) /* result rt extent */
2379 xfs_rtblock_t b; /* result block */
2380 int error; /* error return value */
2381 xfs_inode_t *ip; /* bitmap incore inode */
2382 int log2; /* log of sequence number */
2383 __uint64_t resid; /* residual after log removed */
2384 __uint64_t seq; /* sequence number of file creation */
2385 __uint64_t *seqp; /* pointer to seqno in inode */
2387 error = xfs_trans_iget(mp, tp, mp->m_sb.sb_rbmino, 0, XFS_ILOCK_EXCL, &ip);
2388 if (error)
2389 return error;
2390 ASSERT(ip == mp->m_rbmip);
2391 seqp = (__uint64_t *)&ip->i_d.di_atime;
2392 if (!(ip->i_d.di_flags & XFS_DIFLAG_NEWRTBM)) {
2393 ip->i_d.di_flags |= XFS_DIFLAG_NEWRTBM;
2394 *seqp = 0;
2396 seq = *seqp;
2397 if ((log2 = xfs_highbit64(seq)) == -1)
2398 b = 0;
2399 else {
2400 resid = seq - (1ULL << log2);
2401 b = (mp->m_sb.sb_rextents * ((resid << 1) + 1ULL)) >>
2402 (log2 + 1);
2403 if (b >= mp->m_sb.sb_rextents)
2404 b = do_mod(b, mp->m_sb.sb_rextents);
2405 if (b + len > mp->m_sb.sb_rextents)
2406 b = mp->m_sb.sb_rextents - len;
2408 *seqp = seq + 1;
2409 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
2410 *pick = b;
2411 return 0;
2414 #ifdef DEBUG
2416 * Debug code: print out the value of a range in the bitmap.
2418 void
2419 xfs_rtprint_range(
2420 xfs_mount_t *mp, /* file system mount structure */
2421 xfs_trans_t *tp, /* transaction pointer */
2422 xfs_rtblock_t start, /* starting block to print */
2423 xfs_extlen_t len) /* length to print */
2425 xfs_extlen_t i; /* block number in the extent */
2427 printk("%Ld: ", (long long)start);
2428 for (i = 0; i < len; i++)
2429 printk("%d", xfs_rtcheck_bit(mp, tp, start + i, 1));
2430 printk("\n");
2434 * Debug code: print the summary file.
2436 void
2437 xfs_rtprint_summary(
2438 xfs_mount_t *mp, /* file system mount structure */
2439 xfs_trans_t *tp) /* transaction pointer */
2441 xfs_suminfo_t c; /* summary data */
2442 xfs_rtblock_t i; /* bitmap block number */
2443 int l; /* summary information level */
2444 int p; /* flag for printed anything */
2445 xfs_fsblock_t sb; /* summary block number */
2446 xfs_buf_t *sumbp; /* summary block buffer */
2448 sumbp = NULL;
2449 for (l = 0; l < mp->m_rsumlevels; l++) {
2450 for (p = 0, i = 0; i < mp->m_sb.sb_rbmblocks; i++) {
2451 (void)xfs_rtget_summary(mp, tp, l, i, &sumbp, &sb, &c);
2452 if (c) {
2453 if (!p) {
2454 printk("%Ld-%Ld:", 1LL << l,
2455 XFS_RTMIN((1LL << l) +
2456 ((1LL << l) - 1LL),
2457 mp->m_sb.sb_rextents));
2458 p = 1;
2460 printk(" %Ld:%d", (long long)i, c);
2463 if (p)
2464 printk("\n");
2466 if (sumbp)
2467 xfs_trans_brelse(tp, sumbp);
2469 #endif /* DEBUG */