Linux 2.6.28-rc5
[cris-mirror.git] / fs / xfs / xfs_rtalloc.c
blobe2f68de161599d28d7b33f3348fd6dea4680f5e7
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"
47 #include "xfs_utils.h"
51 * Prototypes for internal functions.
55 STATIC int xfs_rtallocate_range(xfs_mount_t *, xfs_trans_t *, xfs_rtblock_t,
56 xfs_extlen_t, xfs_buf_t **, xfs_fsblock_t *);
57 STATIC int xfs_rtany_summary(xfs_mount_t *, xfs_trans_t *, int, int,
58 xfs_rtblock_t, xfs_buf_t **, xfs_fsblock_t *, int *);
59 STATIC int xfs_rtcheck_range(xfs_mount_t *, xfs_trans_t *, xfs_rtblock_t,
60 xfs_extlen_t, int, xfs_rtblock_t *, int *);
61 STATIC int xfs_rtfind_back(xfs_mount_t *, xfs_trans_t *, xfs_rtblock_t,
62 xfs_rtblock_t, xfs_rtblock_t *);
63 STATIC int xfs_rtfind_forw(xfs_mount_t *, xfs_trans_t *, xfs_rtblock_t,
64 xfs_rtblock_t, xfs_rtblock_t *);
65 STATIC int xfs_rtget_summary( xfs_mount_t *, xfs_trans_t *, int,
66 xfs_rtblock_t, xfs_buf_t **, xfs_fsblock_t *, xfs_suminfo_t *);
67 STATIC int xfs_rtmodify_range(xfs_mount_t *, xfs_trans_t *, xfs_rtblock_t,
68 xfs_extlen_t, int);
69 STATIC int xfs_rtmodify_summary(xfs_mount_t *, xfs_trans_t *, int,
70 xfs_rtblock_t, int, xfs_buf_t **, xfs_fsblock_t *);
73 * Internal functions.
77 * Allocate space to the bitmap or summary file, and zero it, for growfs.
79 STATIC int /* error */
80 xfs_growfs_rt_alloc(
81 xfs_mount_t *mp, /* file system mount point */
82 xfs_extlen_t oblocks, /* old count of blocks */
83 xfs_extlen_t nblocks, /* new count of blocks */
84 xfs_ino_t ino) /* inode number (bitmap/summary) */
86 xfs_fileoff_t bno; /* block number in file */
87 xfs_buf_t *bp; /* temporary buffer for zeroing */
88 int cancelflags; /* flags for xfs_trans_cancel */
89 int committed; /* transaction committed flag */
90 xfs_daddr_t d; /* disk block address */
91 int error; /* error return value */
92 xfs_fsblock_t firstblock; /* first block allocated in xaction */
93 xfs_bmap_free_t flist; /* list of freed blocks */
94 xfs_fsblock_t fsbno; /* filesystem block for bno */
95 xfs_inode_t *ip; /* pointer to incore inode */
96 xfs_bmbt_irec_t map; /* block map output */
97 int nmap; /* number of block maps */
98 int resblks; /* space reservation */
99 xfs_trans_t *tp; /* transaction pointer */
102 * Allocate space to the file, as necessary.
104 while (oblocks < nblocks) {
105 tp = xfs_trans_alloc(mp, XFS_TRANS_GROWFSRT_ALLOC);
106 resblks = XFS_GROWFSRT_SPACE_RES(mp, nblocks - oblocks);
107 cancelflags = 0;
109 * Reserve space & log for one extent added to the file.
111 if ((error = xfs_trans_reserve(tp, resblks,
112 XFS_GROWRTALLOC_LOG_RES(mp), 0,
113 XFS_TRANS_PERM_LOG_RES,
114 XFS_DEFAULT_PERM_LOG_COUNT)))
115 goto error_cancel;
116 cancelflags = XFS_TRANS_RELEASE_LOG_RES;
118 * Lock the inode.
120 if ((error = xfs_trans_iget(mp, tp, ino, 0,
121 XFS_ILOCK_EXCL, &ip)))
122 goto error_cancel;
123 XFS_BMAP_INIT(&flist, &firstblock);
125 * Allocate blocks to the bitmap file.
127 nmap = 1;
128 cancelflags |= XFS_TRANS_ABORT;
129 error = xfs_bmapi(tp, ip, oblocks, nblocks - oblocks,
130 XFS_BMAPI_WRITE | XFS_BMAPI_METADATA, &firstblock,
131 resblks, &map, &nmap, &flist, NULL);
132 if (!error && nmap < 1)
133 error = XFS_ERROR(ENOSPC);
134 if (error)
135 goto error_cancel;
137 * Free any blocks freed up in the transaction, then commit.
139 error = xfs_bmap_finish(&tp, &flist, &committed);
140 if (error)
141 goto error_cancel;
142 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
143 if (error)
144 goto error;
146 * Now we need to clear the allocated blocks.
147 * Do this one block per transaction, to keep it simple.
149 cancelflags = 0;
150 for (bno = map.br_startoff, fsbno = map.br_startblock;
151 bno < map.br_startoff + map.br_blockcount;
152 bno++, fsbno++) {
153 tp = xfs_trans_alloc(mp, XFS_TRANS_GROWFSRT_ZERO);
155 * Reserve log for one block zeroing.
157 if ((error = xfs_trans_reserve(tp, 0,
158 XFS_GROWRTZERO_LOG_RES(mp), 0, 0, 0)))
159 goto error_cancel;
161 * Lock the bitmap inode.
163 if ((error = xfs_trans_iget(mp, tp, ino, 0,
164 XFS_ILOCK_EXCL, &ip)))
165 goto error_cancel;
167 * Get a buffer for the block.
169 d = XFS_FSB_TO_DADDR(mp, fsbno);
170 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
171 mp->m_bsize, 0);
172 if (bp == NULL) {
173 error = XFS_ERROR(EIO);
174 goto error_cancel;
176 memset(XFS_BUF_PTR(bp), 0, mp->m_sb.sb_blocksize);
177 xfs_trans_log_buf(tp, bp, 0, mp->m_sb.sb_blocksize - 1);
179 * Commit the transaction.
181 error = xfs_trans_commit(tp, 0);
182 if (error)
183 goto error;
186 * Go on to the next extent, if any.
188 oblocks = map.br_startoff + map.br_blockcount;
190 return 0;
191 error_cancel:
192 xfs_trans_cancel(tp, cancelflags);
193 error:
194 return error;
198 * Attempt to allocate an extent minlen<=len<=maxlen starting from
199 * bitmap block bbno. If we don't get maxlen then use prod to trim
200 * the length, if given. Returns error; returns starting block in *rtblock.
201 * The lengths are all in rtextents.
203 STATIC int /* error */
204 xfs_rtallocate_extent_block(
205 xfs_mount_t *mp, /* file system mount point */
206 xfs_trans_t *tp, /* transaction pointer */
207 xfs_rtblock_t bbno, /* bitmap block number */
208 xfs_extlen_t minlen, /* minimum length to allocate */
209 xfs_extlen_t maxlen, /* maximum length to allocate */
210 xfs_extlen_t *len, /* out: actual length allocated */
211 xfs_rtblock_t *nextp, /* out: next block to try */
212 xfs_buf_t **rbpp, /* in/out: summary block buffer */
213 xfs_fsblock_t *rsb, /* in/out: summary block number */
214 xfs_extlen_t prod, /* extent product factor */
215 xfs_rtblock_t *rtblock) /* out: start block allocated */
217 xfs_rtblock_t besti; /* best rtblock found so far */
218 xfs_rtblock_t bestlen; /* best length found so far */
219 xfs_rtblock_t end; /* last rtblock in chunk */
220 int error; /* error value */
221 xfs_rtblock_t i; /* current rtblock trying */
222 xfs_rtblock_t next; /* next rtblock to try */
223 int stat; /* status from internal calls */
226 * Loop over all the extents starting in this bitmap block,
227 * looking for one that's long enough.
229 for (i = XFS_BLOCKTOBIT(mp, bbno), besti = -1, bestlen = 0,
230 end = XFS_BLOCKTOBIT(mp, bbno + 1) - 1;
231 i <= end;
232 i++) {
234 * See if there's a free extent of maxlen starting at i.
235 * If it's not so then next will contain the first non-free.
237 error = xfs_rtcheck_range(mp, tp, i, maxlen, 1, &next, &stat);
238 if (error) {
239 return error;
241 if (stat) {
243 * i for maxlen is all free, allocate and return that.
245 error = xfs_rtallocate_range(mp, tp, i, maxlen, rbpp,
246 rsb);
247 if (error) {
248 return error;
250 *len = maxlen;
251 *rtblock = i;
252 return 0;
255 * In the case where we have a variable-sized allocation
256 * request, figure out how big this free piece is,
257 * and if it's big enough for the minimum, and the best
258 * so far, remember it.
260 if (minlen < maxlen) {
261 xfs_rtblock_t thislen; /* this extent size */
263 thislen = next - i;
264 if (thislen >= minlen && thislen > bestlen) {
265 besti = i;
266 bestlen = thislen;
270 * If not done yet, find the start of the next free space.
272 if (next < end) {
273 error = xfs_rtfind_forw(mp, tp, next, end, &i);
274 if (error) {
275 return error;
277 } else
278 break;
281 * Searched the whole thing & didn't find a maxlen free extent.
283 if (minlen < maxlen && besti != -1) {
284 xfs_extlen_t p; /* amount to trim length by */
287 * If size should be a multiple of prod, make that so.
289 if (prod > 1 && (p = do_mod(bestlen, prod)))
290 bestlen -= p;
292 * Allocate besti for bestlen & return that.
294 error = xfs_rtallocate_range(mp, tp, besti, bestlen, rbpp, rsb);
295 if (error) {
296 return error;
298 *len = bestlen;
299 *rtblock = besti;
300 return 0;
303 * Allocation failed. Set *nextp to the next block to try.
305 *nextp = next;
306 *rtblock = NULLRTBLOCK;
307 return 0;
311 * Allocate an extent of length minlen<=len<=maxlen, starting at block
312 * bno. If we don't get maxlen then use prod to trim the length, if given.
313 * Returns error; returns starting block in *rtblock.
314 * The lengths are all in rtextents.
316 STATIC int /* error */
317 xfs_rtallocate_extent_exact(
318 xfs_mount_t *mp, /* file system mount point */
319 xfs_trans_t *tp, /* transaction pointer */
320 xfs_rtblock_t bno, /* starting block number to allocate */
321 xfs_extlen_t minlen, /* minimum length to allocate */
322 xfs_extlen_t maxlen, /* maximum length to allocate */
323 xfs_extlen_t *len, /* out: actual length allocated */
324 xfs_buf_t **rbpp, /* in/out: summary block buffer */
325 xfs_fsblock_t *rsb, /* in/out: summary block number */
326 xfs_extlen_t prod, /* extent product factor */
327 xfs_rtblock_t *rtblock) /* out: start block allocated */
329 int error; /* error value */
330 xfs_extlen_t i; /* extent length trimmed due to prod */
331 int isfree; /* extent is free */
332 xfs_rtblock_t next; /* next block to try (dummy) */
334 ASSERT(minlen % prod == 0 && maxlen % prod == 0);
336 * Check if the range in question (for maxlen) is free.
338 error = xfs_rtcheck_range(mp, tp, bno, maxlen, 1, &next, &isfree);
339 if (error) {
340 return error;
342 if (isfree) {
344 * If it is, allocate it and return success.
346 error = xfs_rtallocate_range(mp, tp, bno, maxlen, rbpp, rsb);
347 if (error) {
348 return error;
350 *len = maxlen;
351 *rtblock = bno;
352 return 0;
355 * If not, allocate what there is, if it's at least minlen.
357 maxlen = next - bno;
358 if (maxlen < minlen) {
360 * Failed, return failure status.
362 *rtblock = NULLRTBLOCK;
363 return 0;
366 * Trim off tail of extent, if prod is specified.
368 if (prod > 1 && (i = maxlen % prod)) {
369 maxlen -= i;
370 if (maxlen < minlen) {
372 * Now we can't do it, return failure status.
374 *rtblock = NULLRTBLOCK;
375 return 0;
379 * Allocate what we can and return it.
381 error = xfs_rtallocate_range(mp, tp, bno, maxlen, rbpp, rsb);
382 if (error) {
383 return error;
385 *len = maxlen;
386 *rtblock = bno;
387 return 0;
391 * Allocate an extent of length minlen<=len<=maxlen, starting as near
392 * to bno as possible. If we don't get maxlen then use prod to trim
393 * the length, if given. The lengths are all in rtextents.
395 STATIC int /* error */
396 xfs_rtallocate_extent_near(
397 xfs_mount_t *mp, /* file system mount point */
398 xfs_trans_t *tp, /* transaction pointer */
399 xfs_rtblock_t bno, /* starting block number to allocate */
400 xfs_extlen_t minlen, /* minimum length to allocate */
401 xfs_extlen_t maxlen, /* maximum length to allocate */
402 xfs_extlen_t *len, /* out: actual length allocated */
403 xfs_buf_t **rbpp, /* in/out: summary block buffer */
404 xfs_fsblock_t *rsb, /* in/out: summary block number */
405 xfs_extlen_t prod, /* extent product factor */
406 xfs_rtblock_t *rtblock) /* out: start block allocated */
408 int any; /* any useful extents from summary */
409 xfs_rtblock_t bbno; /* bitmap block number */
410 int error; /* error value */
411 int i; /* bitmap block offset (loop control) */
412 int j; /* secondary loop control */
413 int log2len; /* log2 of minlen */
414 xfs_rtblock_t n; /* next block to try */
415 xfs_rtblock_t r; /* result block */
417 ASSERT(minlen % prod == 0 && maxlen % prod == 0);
419 * If the block number given is off the end, silently set it to
420 * the last block.
422 if (bno >= mp->m_sb.sb_rextents)
423 bno = mp->m_sb.sb_rextents - 1;
425 * Try the exact allocation first.
427 error = xfs_rtallocate_extent_exact(mp, tp, bno, minlen, maxlen, len,
428 rbpp, rsb, prod, &r);
429 if (error) {
430 return error;
433 * If the exact allocation worked, return that.
435 if (r != NULLRTBLOCK) {
436 *rtblock = r;
437 return 0;
439 bbno = XFS_BITTOBLOCK(mp, bno);
440 i = 0;
441 ASSERT(minlen != 0);
442 log2len = xfs_highbit32(minlen);
444 * Loop over all bitmap blocks (bbno + i is current block).
446 for (;;) {
448 * Get summary information of extents of all useful levels
449 * starting in this bitmap block.
451 error = xfs_rtany_summary(mp, tp, log2len, mp->m_rsumlevels - 1,
452 bbno + i, rbpp, rsb, &any);
453 if (error) {
454 return error;
457 * If there are any useful extents starting here, try
458 * allocating one.
460 if (any) {
462 * On the positive side of the starting location.
464 if (i >= 0) {
466 * Try to allocate an extent starting in
467 * this block.
469 error = xfs_rtallocate_extent_block(mp, tp,
470 bbno + i, minlen, maxlen, len, &n, rbpp,
471 rsb, prod, &r);
472 if (error) {
473 return error;
476 * If it worked, return it.
478 if (r != NULLRTBLOCK) {
479 *rtblock = r;
480 return 0;
484 * On the negative side of the starting location.
486 else { /* i < 0 */
488 * Loop backwards through the bitmap blocks from
489 * the starting point-1 up to where we are now.
490 * There should be an extent which ends in this
491 * bitmap block and is long enough.
493 for (j = -1; j > i; j--) {
495 * Grab the summary information for
496 * this bitmap block.
498 error = xfs_rtany_summary(mp, tp,
499 log2len, mp->m_rsumlevels - 1,
500 bbno + j, rbpp, rsb, &any);
501 if (error) {
502 return error;
505 * If there's no extent given in the
506 * summary that means the extent we
507 * found must carry over from an
508 * earlier block. If there is an
509 * extent given, we've already tried
510 * that allocation, don't do it again.
512 if (any)
513 continue;
514 error = xfs_rtallocate_extent_block(mp,
515 tp, bbno + j, minlen, maxlen,
516 len, &n, rbpp, rsb, prod, &r);
517 if (error) {
518 return error;
521 * If it works, return the extent.
523 if (r != NULLRTBLOCK) {
524 *rtblock = r;
525 return 0;
529 * There weren't intervening bitmap blocks
530 * with a long enough extent, or the
531 * allocation didn't work for some reason
532 * (i.e. it's a little * too short).
533 * Try to allocate from the summary block
534 * that we found.
536 error = xfs_rtallocate_extent_block(mp, tp,
537 bbno + i, minlen, maxlen, len, &n, rbpp,
538 rsb, prod, &r);
539 if (error) {
540 return error;
543 * If it works, return the extent.
545 if (r != NULLRTBLOCK) {
546 *rtblock = r;
547 return 0;
552 * Loop control. If we were on the positive side, and there's
553 * still more blocks on the negative side, go there.
555 if (i > 0 && (int)bbno - i >= 0)
556 i = -i;
558 * If positive, and no more negative, but there are more
559 * positive, go there.
561 else if (i > 0 && (int)bbno + i < mp->m_sb.sb_rbmblocks - 1)
562 i++;
564 * If negative or 0 (just started), and there are positive
565 * blocks to go, go there. The 0 case moves to block 1.
567 else if (i <= 0 && (int)bbno - i < mp->m_sb.sb_rbmblocks - 1)
568 i = 1 - i;
570 * If negative or 0 and there are more negative blocks,
571 * go there.
573 else if (i <= 0 && (int)bbno + i > 0)
574 i--;
576 * Must be done. Return failure.
578 else
579 break;
581 *rtblock = NULLRTBLOCK;
582 return 0;
586 * Allocate an extent of length minlen<=len<=maxlen, with no position
587 * specified. If we don't get maxlen then use prod to trim
588 * the length, if given. The lengths are all in rtextents.
590 STATIC int /* error */
591 xfs_rtallocate_extent_size(
592 xfs_mount_t *mp, /* file system mount point */
593 xfs_trans_t *tp, /* transaction pointer */
594 xfs_extlen_t minlen, /* minimum length to allocate */
595 xfs_extlen_t maxlen, /* maximum length to allocate */
596 xfs_extlen_t *len, /* out: actual length allocated */
597 xfs_buf_t **rbpp, /* in/out: summary block buffer */
598 xfs_fsblock_t *rsb, /* in/out: summary block number */
599 xfs_extlen_t prod, /* extent product factor */
600 xfs_rtblock_t *rtblock) /* out: start block allocated */
602 int error; /* error value */
603 int i; /* bitmap block number */
604 int l; /* level number (loop control) */
605 xfs_rtblock_t n; /* next block to be tried */
606 xfs_rtblock_t r; /* result block number */
607 xfs_suminfo_t sum; /* summary information for extents */
609 ASSERT(minlen % prod == 0 && maxlen % prod == 0);
610 ASSERT(maxlen != 0);
613 * Loop over all the levels starting with maxlen.
614 * At each level, look at all the bitmap blocks, to see if there
615 * are extents starting there that are long enough (>= maxlen).
616 * Note, only on the initial level can the allocation fail if
617 * the summary says there's an extent.
619 for (l = xfs_highbit32(maxlen); l < mp->m_rsumlevels; l++) {
621 * Loop over all the bitmap blocks.
623 for (i = 0; i < mp->m_sb.sb_rbmblocks; i++) {
625 * Get the summary for this level/block.
627 error = xfs_rtget_summary(mp, tp, l, i, rbpp, rsb,
628 &sum);
629 if (error) {
630 return error;
633 * Nothing there, on to the next block.
635 if (!sum)
636 continue;
638 * Try allocating the extent.
640 error = xfs_rtallocate_extent_block(mp, tp, i, maxlen,
641 maxlen, len, &n, rbpp, rsb, prod, &r);
642 if (error) {
643 return error;
646 * If it worked, return that.
648 if (r != NULLRTBLOCK) {
649 *rtblock = r;
650 return 0;
653 * If the "next block to try" returned from the
654 * allocator is beyond the next bitmap block,
655 * skip to that bitmap block.
657 if (XFS_BITTOBLOCK(mp, n) > i + 1)
658 i = XFS_BITTOBLOCK(mp, n) - 1;
662 * Didn't find any maxlen blocks. Try smaller ones, unless
663 * we're asking for a fixed size extent.
665 if (minlen > --maxlen) {
666 *rtblock = NULLRTBLOCK;
667 return 0;
669 ASSERT(minlen != 0);
670 ASSERT(maxlen != 0);
673 * Loop over sizes, from maxlen down to minlen.
674 * This time, when we do the allocations, allow smaller ones
675 * to succeed.
677 for (l = xfs_highbit32(maxlen); l >= xfs_highbit32(minlen); l--) {
679 * Loop over all the bitmap blocks, try an allocation
680 * starting in that block.
682 for (i = 0; i < mp->m_sb.sb_rbmblocks; i++) {
684 * Get the summary information for this level/block.
686 error = xfs_rtget_summary(mp, tp, l, i, rbpp, rsb,
687 &sum);
688 if (error) {
689 return error;
692 * If nothing there, go on to next.
694 if (!sum)
695 continue;
697 * Try the allocation. Make sure the specified
698 * minlen/maxlen are in the possible range for
699 * this summary level.
701 error = xfs_rtallocate_extent_block(mp, tp, i,
702 XFS_RTMAX(minlen, 1 << l),
703 XFS_RTMIN(maxlen, (1 << (l + 1)) - 1),
704 len, &n, rbpp, rsb, prod, &r);
705 if (error) {
706 return error;
709 * If it worked, return that extent.
711 if (r != NULLRTBLOCK) {
712 *rtblock = r;
713 return 0;
716 * If the "next block to try" returned from the
717 * allocator is beyond the next bitmap block,
718 * skip to that bitmap block.
720 if (XFS_BITTOBLOCK(mp, n) > i + 1)
721 i = XFS_BITTOBLOCK(mp, n) - 1;
725 * Got nothing, return failure.
727 *rtblock = NULLRTBLOCK;
728 return 0;
732 * Mark an extent specified by start and len allocated.
733 * Updates all the summary information as well as the bitmap.
735 STATIC int /* error */
736 xfs_rtallocate_range(
737 xfs_mount_t *mp, /* file system mount point */
738 xfs_trans_t *tp, /* transaction pointer */
739 xfs_rtblock_t start, /* start block to allocate */
740 xfs_extlen_t len, /* length to allocate */
741 xfs_buf_t **rbpp, /* in/out: summary block buffer */
742 xfs_fsblock_t *rsb) /* in/out: summary block number */
744 xfs_rtblock_t end; /* end of the allocated extent */
745 int error; /* error value */
746 xfs_rtblock_t postblock; /* first block allocated > end */
747 xfs_rtblock_t preblock; /* first block allocated < start */
749 end = start + len - 1;
751 * Assume we're allocating out of the middle of a free extent.
752 * We need to find the beginning and end of the extent so we can
753 * properly update the summary.
755 error = xfs_rtfind_back(mp, tp, start, 0, &preblock);
756 if (error) {
757 return error;
760 * Find the next allocated block (end of free extent).
762 error = xfs_rtfind_forw(mp, tp, end, mp->m_sb.sb_rextents - 1,
763 &postblock);
764 if (error) {
765 return error;
768 * Decrement the summary information corresponding to the entire
769 * (old) free extent.
771 error = xfs_rtmodify_summary(mp, tp,
772 XFS_RTBLOCKLOG(postblock + 1 - preblock),
773 XFS_BITTOBLOCK(mp, preblock), -1, rbpp, rsb);
774 if (error) {
775 return error;
778 * If there are blocks not being allocated at the front of the
779 * old extent, add summary data for them to be free.
781 if (preblock < start) {
782 error = xfs_rtmodify_summary(mp, tp,
783 XFS_RTBLOCKLOG(start - preblock),
784 XFS_BITTOBLOCK(mp, preblock), 1, rbpp, rsb);
785 if (error) {
786 return error;
790 * If there are blocks not being allocated at the end of the
791 * old extent, add summary data for them to be free.
793 if (postblock > end) {
794 error = xfs_rtmodify_summary(mp, tp,
795 XFS_RTBLOCKLOG(postblock - end),
796 XFS_BITTOBLOCK(mp, end + 1), 1, rbpp, rsb);
797 if (error) {
798 return error;
802 * Modify the bitmap to mark this extent allocated.
804 error = xfs_rtmodify_range(mp, tp, start, len, 0);
805 return error;
809 * Return whether there are any free extents in the size range given
810 * by low and high, for the bitmap block bbno.
812 STATIC int /* error */
813 xfs_rtany_summary(
814 xfs_mount_t *mp, /* file system mount structure */
815 xfs_trans_t *tp, /* transaction pointer */
816 int low, /* low log2 extent size */
817 int high, /* high log2 extent size */
818 xfs_rtblock_t bbno, /* bitmap block number */
819 xfs_buf_t **rbpp, /* in/out: summary block buffer */
820 xfs_fsblock_t *rsb, /* in/out: summary block number */
821 int *stat) /* out: any good extents here? */
823 int error; /* error value */
824 int log; /* loop counter, log2 of ext. size */
825 xfs_suminfo_t sum; /* summary data */
828 * Loop over logs of extent sizes. Order is irrelevant.
830 for (log = low; log <= high; log++) {
832 * Get one summary datum.
834 error = xfs_rtget_summary(mp, tp, log, bbno, rbpp, rsb, &sum);
835 if (error) {
836 return error;
839 * If there are any, return success.
841 if (sum) {
842 *stat = 1;
843 return 0;
847 * Found nothing, return failure.
849 *stat = 0;
850 return 0;
854 * Get a buffer for the bitmap or summary file block specified.
855 * The buffer is returned read and locked.
857 STATIC int /* error */
858 xfs_rtbuf_get(
859 xfs_mount_t *mp, /* file system mount structure */
860 xfs_trans_t *tp, /* transaction pointer */
861 xfs_rtblock_t block, /* block number in bitmap or summary */
862 int issum, /* is summary not bitmap */
863 xfs_buf_t **bpp) /* output: buffer for the block */
865 xfs_buf_t *bp; /* block buffer, result */
866 xfs_daddr_t d; /* disk addr of block */
867 int error; /* error value */
868 xfs_fsblock_t fsb; /* fs block number for block */
869 xfs_inode_t *ip; /* bitmap or summary inode */
871 ip = issum ? mp->m_rsumip : mp->m_rbmip;
873 * Map from the file offset (block) and inode number to the
874 * file system block.
876 error = xfs_bmapi_single(tp, ip, XFS_DATA_FORK, &fsb, block);
877 if (error) {
878 return error;
880 ASSERT(fsb != NULLFSBLOCK);
882 * Convert to disk address for buffer cache.
884 d = XFS_FSB_TO_DADDR(mp, fsb);
886 * Read the buffer.
888 error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, d,
889 mp->m_bsize, 0, &bp);
890 if (error) {
891 return error;
893 ASSERT(bp && !XFS_BUF_GETERROR(bp));
894 *bpp = bp;
895 return 0;
898 #ifdef DEBUG
900 * Check that the given extent (block range) is allocated already.
902 STATIC int /* error */
903 xfs_rtcheck_alloc_range(
904 xfs_mount_t *mp, /* file system mount point */
905 xfs_trans_t *tp, /* transaction pointer */
906 xfs_rtblock_t bno, /* starting block number of extent */
907 xfs_extlen_t len, /* length of extent */
908 int *stat) /* out: 1 for allocated, 0 for not */
910 xfs_rtblock_t new; /* dummy for xfs_rtcheck_range */
912 return xfs_rtcheck_range(mp, tp, bno, len, 0, &new, stat);
914 #endif
917 * Check that the given range is either all allocated (val = 0) or
918 * all free (val = 1).
920 STATIC int /* error */
921 xfs_rtcheck_range(
922 xfs_mount_t *mp, /* file system mount point */
923 xfs_trans_t *tp, /* transaction pointer */
924 xfs_rtblock_t start, /* starting block number of extent */
925 xfs_extlen_t len, /* length of extent */
926 int val, /* 1 for free, 0 for allocated */
927 xfs_rtblock_t *new, /* out: first block not matching */
928 int *stat) /* out: 1 for matches, 0 for not */
930 xfs_rtword_t *b; /* current word in buffer */
931 int bit; /* bit number in the word */
932 xfs_rtblock_t block; /* bitmap block number */
933 xfs_buf_t *bp; /* buf for the block */
934 xfs_rtword_t *bufp; /* starting word in buffer */
935 int error; /* error value */
936 xfs_rtblock_t i; /* current bit number rel. to start */
937 xfs_rtblock_t lastbit; /* last useful bit in word */
938 xfs_rtword_t mask; /* mask of relevant bits for value */
939 xfs_rtword_t wdiff; /* difference from wanted value */
940 int word; /* word number in the buffer */
943 * Compute starting bitmap block number
945 block = XFS_BITTOBLOCK(mp, start);
947 * Read the bitmap block.
949 error = xfs_rtbuf_get(mp, tp, block, 0, &bp);
950 if (error) {
951 return error;
953 bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
955 * Compute the starting word's address, and starting bit.
957 word = XFS_BITTOWORD(mp, start);
958 b = &bufp[word];
959 bit = (int)(start & (XFS_NBWORD - 1));
961 * 0 (allocated) => all zero's; 1 (free) => all one's.
963 val = -val;
965 * If not starting on a word boundary, deal with the first
966 * (partial) word.
968 if (bit) {
970 * Compute first bit not examined.
972 lastbit = XFS_RTMIN(bit + len, XFS_NBWORD);
974 * Mask of relevant bits.
976 mask = (((xfs_rtword_t)1 << (lastbit - bit)) - 1) << bit;
978 * Compute difference between actual and desired value.
980 if ((wdiff = (*b ^ val) & mask)) {
982 * Different, compute first wrong bit and return.
984 xfs_trans_brelse(tp, bp);
985 i = XFS_RTLOBIT(wdiff) - bit;
986 *new = start + i;
987 *stat = 0;
988 return 0;
990 i = lastbit - bit;
992 * Go on to next block if that's where the next word is
993 * and we need the next word.
995 if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
997 * If done with this block, get the next one.
999 xfs_trans_brelse(tp, bp);
1000 error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
1001 if (error) {
1002 return error;
1004 b = bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1005 word = 0;
1006 } else {
1008 * Go on to the next word in the buffer.
1010 b++;
1012 } else {
1014 * Starting on a word boundary, no partial word.
1016 i = 0;
1019 * Loop over whole words in buffers. When we use up one buffer
1020 * we move on to the next one.
1022 while (len - i >= XFS_NBWORD) {
1024 * Compute difference between actual and desired value.
1026 if ((wdiff = *b ^ val)) {
1028 * Different, compute first wrong bit and return.
1030 xfs_trans_brelse(tp, bp);
1031 i += XFS_RTLOBIT(wdiff);
1032 *new = start + i;
1033 *stat = 0;
1034 return 0;
1036 i += XFS_NBWORD;
1038 * Go on to next block if that's where the next word is
1039 * and we need the next word.
1041 if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
1043 * If done with this block, get the next one.
1045 xfs_trans_brelse(tp, bp);
1046 error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
1047 if (error) {
1048 return error;
1050 b = bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1051 word = 0;
1052 } else {
1054 * Go on to the next word in the buffer.
1056 b++;
1060 * If not ending on a word boundary, deal with the last
1061 * (partial) word.
1063 if ((lastbit = len - i)) {
1065 * Mask of relevant bits.
1067 mask = ((xfs_rtword_t)1 << lastbit) - 1;
1069 * Compute difference between actual and desired value.
1071 if ((wdiff = (*b ^ val) & mask)) {
1073 * Different, compute first wrong bit and return.
1075 xfs_trans_brelse(tp, bp);
1076 i += XFS_RTLOBIT(wdiff);
1077 *new = start + i;
1078 *stat = 0;
1079 return 0;
1080 } else
1081 i = len;
1084 * Successful, return.
1086 xfs_trans_brelse(tp, bp);
1087 *new = start + i;
1088 *stat = 1;
1089 return 0;
1093 * Copy and transform the summary file, given the old and new
1094 * parameters in the mount structures.
1096 STATIC int /* error */
1097 xfs_rtcopy_summary(
1098 xfs_mount_t *omp, /* old file system mount point */
1099 xfs_mount_t *nmp, /* new file system mount point */
1100 xfs_trans_t *tp) /* transaction pointer */
1102 xfs_rtblock_t bbno; /* bitmap block number */
1103 xfs_buf_t *bp; /* summary buffer */
1104 int error; /* error return value */
1105 int log; /* summary level number (log length) */
1106 xfs_suminfo_t sum; /* summary data */
1107 xfs_fsblock_t sumbno; /* summary block number */
1109 bp = NULL;
1110 for (log = omp->m_rsumlevels - 1; log >= 0; log--) {
1111 for (bbno = omp->m_sb.sb_rbmblocks - 1;
1112 (xfs_srtblock_t)bbno >= 0;
1113 bbno--) {
1114 error = xfs_rtget_summary(omp, tp, log, bbno, &bp,
1115 &sumbno, &sum);
1116 if (error)
1117 return error;
1118 if (sum == 0)
1119 continue;
1120 error = xfs_rtmodify_summary(omp, tp, log, bbno, -sum,
1121 &bp, &sumbno);
1122 if (error)
1123 return error;
1124 error = xfs_rtmodify_summary(nmp, tp, log, bbno, sum,
1125 &bp, &sumbno);
1126 if (error)
1127 return error;
1128 ASSERT(sum > 0);
1131 return 0;
1135 * Searching backward from start to limit, find the first block whose
1136 * allocated/free state is different from start's.
1138 STATIC int /* error */
1139 xfs_rtfind_back(
1140 xfs_mount_t *mp, /* file system mount point */
1141 xfs_trans_t *tp, /* transaction pointer */
1142 xfs_rtblock_t start, /* starting block to look at */
1143 xfs_rtblock_t limit, /* last block to look at */
1144 xfs_rtblock_t *rtblock) /* out: start block found */
1146 xfs_rtword_t *b; /* current word in buffer */
1147 int bit; /* bit number in the word */
1148 xfs_rtblock_t block; /* bitmap block number */
1149 xfs_buf_t *bp; /* buf for the block */
1150 xfs_rtword_t *bufp; /* starting word in buffer */
1151 int error; /* error value */
1152 xfs_rtblock_t firstbit; /* first useful bit in the word */
1153 xfs_rtblock_t i; /* current bit number rel. to start */
1154 xfs_rtblock_t len; /* length of inspected area */
1155 xfs_rtword_t mask; /* mask of relevant bits for value */
1156 xfs_rtword_t want; /* mask for "good" values */
1157 xfs_rtword_t wdiff; /* difference from wanted value */
1158 int word; /* word number in the buffer */
1161 * Compute and read in starting bitmap block for starting block.
1163 block = XFS_BITTOBLOCK(mp, start);
1164 error = xfs_rtbuf_get(mp, tp, block, 0, &bp);
1165 if (error) {
1166 return error;
1168 bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1170 * Get the first word's index & point to it.
1172 word = XFS_BITTOWORD(mp, start);
1173 b = &bufp[word];
1174 bit = (int)(start & (XFS_NBWORD - 1));
1175 len = start - limit + 1;
1177 * Compute match value, based on the bit at start: if 1 (free)
1178 * then all-ones, else all-zeroes.
1180 want = (*b & ((xfs_rtword_t)1 << bit)) ? -1 : 0;
1182 * If the starting position is not word-aligned, deal with the
1183 * partial word.
1185 if (bit < XFS_NBWORD - 1) {
1187 * Calculate first (leftmost) bit number to look at,
1188 * and mask for all the relevant bits in this word.
1190 firstbit = XFS_RTMAX((xfs_srtblock_t)(bit - len + 1), 0);
1191 mask = (((xfs_rtword_t)1 << (bit - firstbit + 1)) - 1) <<
1192 firstbit;
1194 * Calculate the difference between the value there
1195 * and what we're looking for.
1197 if ((wdiff = (*b ^ want) & mask)) {
1199 * Different. Mark where we are and return.
1201 xfs_trans_brelse(tp, bp);
1202 i = bit - XFS_RTHIBIT(wdiff);
1203 *rtblock = start - i + 1;
1204 return 0;
1206 i = bit - firstbit + 1;
1208 * Go on to previous block if that's where the previous word is
1209 * and we need the previous word.
1211 if (--word == -1 && i < len) {
1213 * If done with this block, get the previous one.
1215 xfs_trans_brelse(tp, bp);
1216 error = xfs_rtbuf_get(mp, tp, --block, 0, &bp);
1217 if (error) {
1218 return error;
1220 bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1221 word = XFS_BLOCKWMASK(mp);
1222 b = &bufp[word];
1223 } else {
1225 * Go on to the previous word in the buffer.
1227 b--;
1229 } else {
1231 * Starting on a word boundary, no partial word.
1233 i = 0;
1236 * Loop over whole words in buffers. When we use up one buffer
1237 * we move on to the previous one.
1239 while (len - i >= XFS_NBWORD) {
1241 * Compute difference between actual and desired value.
1243 if ((wdiff = *b ^ want)) {
1245 * Different, mark where we are and return.
1247 xfs_trans_brelse(tp, bp);
1248 i += XFS_NBWORD - 1 - XFS_RTHIBIT(wdiff);
1249 *rtblock = start - i + 1;
1250 return 0;
1252 i += XFS_NBWORD;
1254 * Go on to previous block if that's where the previous word is
1255 * and we need the previous word.
1257 if (--word == -1 && i < len) {
1259 * If done with this block, get the previous one.
1261 xfs_trans_brelse(tp, bp);
1262 error = xfs_rtbuf_get(mp, tp, --block, 0, &bp);
1263 if (error) {
1264 return error;
1266 bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1267 word = XFS_BLOCKWMASK(mp);
1268 b = &bufp[word];
1269 } else {
1271 * Go on to the previous word in the buffer.
1273 b--;
1277 * If not ending on a word boundary, deal with the last
1278 * (partial) word.
1280 if (len - i) {
1282 * Calculate first (leftmost) bit number to look at,
1283 * and mask for all the relevant bits in this word.
1285 firstbit = XFS_NBWORD - (len - i);
1286 mask = (((xfs_rtword_t)1 << (len - i)) - 1) << firstbit;
1288 * Compute difference between actual and desired value.
1290 if ((wdiff = (*b ^ want) & mask)) {
1292 * Different, mark where we are and return.
1294 xfs_trans_brelse(tp, bp);
1295 i += XFS_NBWORD - 1 - XFS_RTHIBIT(wdiff);
1296 *rtblock = start - i + 1;
1297 return 0;
1298 } else
1299 i = len;
1302 * No match, return that we scanned the whole area.
1304 xfs_trans_brelse(tp, bp);
1305 *rtblock = start - i + 1;
1306 return 0;
1310 * Searching forward from start to limit, find the first block whose
1311 * allocated/free state is different from start's.
1313 STATIC int /* error */
1314 xfs_rtfind_forw(
1315 xfs_mount_t *mp, /* file system mount point */
1316 xfs_trans_t *tp, /* transaction pointer */
1317 xfs_rtblock_t start, /* starting block to look at */
1318 xfs_rtblock_t limit, /* last block to look at */
1319 xfs_rtblock_t *rtblock) /* out: start block found */
1321 xfs_rtword_t *b; /* current word in buffer */
1322 int bit; /* bit number in the word */
1323 xfs_rtblock_t block; /* bitmap block number */
1324 xfs_buf_t *bp; /* buf for the block */
1325 xfs_rtword_t *bufp; /* starting word in buffer */
1326 int error; /* error value */
1327 xfs_rtblock_t i; /* current bit number rel. to start */
1328 xfs_rtblock_t lastbit; /* last useful bit in the word */
1329 xfs_rtblock_t len; /* length of inspected area */
1330 xfs_rtword_t mask; /* mask of relevant bits for value */
1331 xfs_rtword_t want; /* mask for "good" values */
1332 xfs_rtword_t wdiff; /* difference from wanted value */
1333 int word; /* word number in the buffer */
1336 * Compute and read in starting bitmap block for starting block.
1338 block = XFS_BITTOBLOCK(mp, start);
1339 error = xfs_rtbuf_get(mp, tp, block, 0, &bp);
1340 if (error) {
1341 return error;
1343 bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1345 * Get the first word's index & point to it.
1347 word = XFS_BITTOWORD(mp, start);
1348 b = &bufp[word];
1349 bit = (int)(start & (XFS_NBWORD - 1));
1350 len = limit - start + 1;
1352 * Compute match value, based on the bit at start: if 1 (free)
1353 * then all-ones, else all-zeroes.
1355 want = (*b & ((xfs_rtword_t)1 << bit)) ? -1 : 0;
1357 * If the starting position is not word-aligned, deal with the
1358 * partial word.
1360 if (bit) {
1362 * Calculate last (rightmost) bit number to look at,
1363 * and mask for all the relevant bits in this word.
1365 lastbit = XFS_RTMIN(bit + len, XFS_NBWORD);
1366 mask = (((xfs_rtword_t)1 << (lastbit - bit)) - 1) << bit;
1368 * Calculate the difference between the value there
1369 * and what we're looking for.
1371 if ((wdiff = (*b ^ want) & mask)) {
1373 * Different. Mark where we are and return.
1375 xfs_trans_brelse(tp, bp);
1376 i = XFS_RTLOBIT(wdiff) - bit;
1377 *rtblock = start + i - 1;
1378 return 0;
1380 i = lastbit - bit;
1382 * Go on to next block if that's where the next word is
1383 * and we need the next word.
1385 if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
1387 * If done with this block, get the previous one.
1389 xfs_trans_brelse(tp, bp);
1390 error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
1391 if (error) {
1392 return error;
1394 b = bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1395 word = 0;
1396 } else {
1398 * Go on to the previous word in the buffer.
1400 b++;
1402 } else {
1404 * Starting on a word boundary, no partial word.
1406 i = 0;
1409 * Loop over whole words in buffers. When we use up one buffer
1410 * we move on to the next one.
1412 while (len - i >= XFS_NBWORD) {
1414 * Compute difference between actual and desired value.
1416 if ((wdiff = *b ^ want)) {
1418 * Different, mark where we are and return.
1420 xfs_trans_brelse(tp, bp);
1421 i += XFS_RTLOBIT(wdiff);
1422 *rtblock = start + i - 1;
1423 return 0;
1425 i += XFS_NBWORD;
1427 * Go on to next block if that's where the next word is
1428 * and we need the next word.
1430 if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
1432 * If done with this block, get the next one.
1434 xfs_trans_brelse(tp, bp);
1435 error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
1436 if (error) {
1437 return error;
1439 b = bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1440 word = 0;
1441 } else {
1443 * Go on to the next word in the buffer.
1445 b++;
1449 * If not ending on a word boundary, deal with the last
1450 * (partial) word.
1452 if ((lastbit = len - i)) {
1454 * Calculate mask for all the relevant bits in this word.
1456 mask = ((xfs_rtword_t)1 << lastbit) - 1;
1458 * Compute difference between actual and desired value.
1460 if ((wdiff = (*b ^ want) & mask)) {
1462 * Different, mark where we are and return.
1464 xfs_trans_brelse(tp, bp);
1465 i += XFS_RTLOBIT(wdiff);
1466 *rtblock = start + i - 1;
1467 return 0;
1468 } else
1469 i = len;
1472 * No match, return that we scanned the whole area.
1474 xfs_trans_brelse(tp, bp);
1475 *rtblock = start + i - 1;
1476 return 0;
1480 * Mark an extent specified by start and len freed.
1481 * Updates all the summary information as well as the bitmap.
1483 STATIC int /* error */
1484 xfs_rtfree_range(
1485 xfs_mount_t *mp, /* file system mount point */
1486 xfs_trans_t *tp, /* transaction pointer */
1487 xfs_rtblock_t start, /* starting block to free */
1488 xfs_extlen_t len, /* length to free */
1489 xfs_buf_t **rbpp, /* in/out: summary block buffer */
1490 xfs_fsblock_t *rsb) /* in/out: summary block number */
1492 xfs_rtblock_t end; /* end of the freed extent */
1493 int error; /* error value */
1494 xfs_rtblock_t postblock; /* first block freed > end */
1495 xfs_rtblock_t preblock; /* first block freed < start */
1497 end = start + len - 1;
1499 * Modify the bitmap to mark this extent freed.
1501 error = xfs_rtmodify_range(mp, tp, start, len, 1);
1502 if (error) {
1503 return error;
1506 * Assume we're freeing out of the middle of an allocated extent.
1507 * We need to find the beginning and end of the extent so we can
1508 * properly update the summary.
1510 error = xfs_rtfind_back(mp, tp, start, 0, &preblock);
1511 if (error) {
1512 return error;
1515 * Find the next allocated block (end of allocated extent).
1517 error = xfs_rtfind_forw(mp, tp, end, mp->m_sb.sb_rextents - 1,
1518 &postblock);
1520 * If there are blocks not being freed at the front of the
1521 * old extent, add summary data for them to be allocated.
1523 if (preblock < start) {
1524 error = xfs_rtmodify_summary(mp, tp,
1525 XFS_RTBLOCKLOG(start - preblock),
1526 XFS_BITTOBLOCK(mp, preblock), -1, rbpp, rsb);
1527 if (error) {
1528 return error;
1532 * If there are blocks not being freed at the end of the
1533 * old extent, add summary data for them to be allocated.
1535 if (postblock > end) {
1536 error = xfs_rtmodify_summary(mp, tp,
1537 XFS_RTBLOCKLOG(postblock - end),
1538 XFS_BITTOBLOCK(mp, end + 1), -1, rbpp, rsb);
1539 if (error) {
1540 return error;
1544 * Increment the summary information corresponding to the entire
1545 * (new) free extent.
1547 error = xfs_rtmodify_summary(mp, tp,
1548 XFS_RTBLOCKLOG(postblock + 1 - preblock),
1549 XFS_BITTOBLOCK(mp, preblock), 1, rbpp, rsb);
1550 return error;
1554 * Read and return the summary information for a given extent size,
1555 * bitmap block combination.
1556 * Keeps track of a current summary block, so we don't keep reading
1557 * it from the buffer cache.
1559 STATIC int /* error */
1560 xfs_rtget_summary(
1561 xfs_mount_t *mp, /* file system mount structure */
1562 xfs_trans_t *tp, /* transaction pointer */
1563 int log, /* log2 of extent size */
1564 xfs_rtblock_t bbno, /* bitmap block number */
1565 xfs_buf_t **rbpp, /* in/out: summary block buffer */
1566 xfs_fsblock_t *rsb, /* in/out: summary block number */
1567 xfs_suminfo_t *sum) /* out: summary info for this block */
1569 xfs_buf_t *bp; /* buffer for summary block */
1570 int error; /* error value */
1571 xfs_fsblock_t sb; /* summary fsblock */
1572 int so; /* index into the summary file */
1573 xfs_suminfo_t *sp; /* pointer to returned data */
1576 * Compute entry number in the summary file.
1578 so = XFS_SUMOFFS(mp, log, bbno);
1580 * Compute the block number in the summary file.
1582 sb = XFS_SUMOFFSTOBLOCK(mp, so);
1584 * If we have an old buffer, and the block number matches, use that.
1586 if (rbpp && *rbpp && *rsb == sb)
1587 bp = *rbpp;
1589 * Otherwise we have to get the buffer.
1591 else {
1593 * If there was an old one, get rid of it first.
1595 if (rbpp && *rbpp)
1596 xfs_trans_brelse(tp, *rbpp);
1597 error = xfs_rtbuf_get(mp, tp, sb, 1, &bp);
1598 if (error) {
1599 return error;
1602 * Remember this buffer and block for the next call.
1604 if (rbpp) {
1605 *rbpp = bp;
1606 *rsb = sb;
1610 * Point to the summary information & copy it out.
1612 sp = XFS_SUMPTR(mp, bp, so);
1613 *sum = *sp;
1615 * Drop the buffer if we're not asked to remember it.
1617 if (!rbpp)
1618 xfs_trans_brelse(tp, bp);
1619 return 0;
1623 * Set the given range of bitmap bits to the given value.
1624 * Do whatever I/O and logging is required.
1626 STATIC int /* error */
1627 xfs_rtmodify_range(
1628 xfs_mount_t *mp, /* file system mount point */
1629 xfs_trans_t *tp, /* transaction pointer */
1630 xfs_rtblock_t start, /* starting block to modify */
1631 xfs_extlen_t len, /* length of extent to modify */
1632 int val) /* 1 for free, 0 for allocated */
1634 xfs_rtword_t *b; /* current word in buffer */
1635 int bit; /* bit number in the word */
1636 xfs_rtblock_t block; /* bitmap block number */
1637 xfs_buf_t *bp; /* buf for the block */
1638 xfs_rtword_t *bufp; /* starting word in buffer */
1639 int error; /* error value */
1640 xfs_rtword_t *first; /* first used word in the buffer */
1641 int i; /* current bit number rel. to start */
1642 int lastbit; /* last useful bit in word */
1643 xfs_rtword_t mask; /* mask o frelevant bits for value */
1644 int word; /* word number in the buffer */
1647 * Compute starting bitmap block number.
1649 block = XFS_BITTOBLOCK(mp, start);
1651 * Read the bitmap block, and point to its data.
1653 error = xfs_rtbuf_get(mp, tp, block, 0, &bp);
1654 if (error) {
1655 return error;
1657 bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1659 * Compute the starting word's address, and starting bit.
1661 word = XFS_BITTOWORD(mp, start);
1662 first = b = &bufp[word];
1663 bit = (int)(start & (XFS_NBWORD - 1));
1665 * 0 (allocated) => all zeroes; 1 (free) => all ones.
1667 val = -val;
1669 * If not starting on a word boundary, deal with the first
1670 * (partial) word.
1672 if (bit) {
1674 * Compute first bit not changed and mask of relevant bits.
1676 lastbit = XFS_RTMIN(bit + len, XFS_NBWORD);
1677 mask = (((xfs_rtword_t)1 << (lastbit - bit)) - 1) << bit;
1679 * Set/clear the active bits.
1681 if (val)
1682 *b |= mask;
1683 else
1684 *b &= ~mask;
1685 i = lastbit - bit;
1687 * Go on to the next block if that's where the next word is
1688 * and we need the next word.
1690 if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
1692 * Log the changed part of this block.
1693 * Get the next one.
1695 xfs_trans_log_buf(tp, bp,
1696 (uint)((char *)first - (char *)bufp),
1697 (uint)((char *)b - (char *)bufp));
1698 error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
1699 if (error) {
1700 return error;
1702 first = b = bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1703 word = 0;
1704 } else {
1706 * Go on to the next word in the buffer
1708 b++;
1710 } else {
1712 * Starting on a word boundary, no partial word.
1714 i = 0;
1717 * Loop over whole words in buffers. When we use up one buffer
1718 * we move on to the next one.
1720 while (len - i >= XFS_NBWORD) {
1722 * Set the word value correctly.
1724 *b = val;
1725 i += XFS_NBWORD;
1727 * Go on to the next block if that's where the next word is
1728 * and we need the next word.
1730 if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
1732 * Log the changed part of this block.
1733 * Get the next one.
1735 xfs_trans_log_buf(tp, bp,
1736 (uint)((char *)first - (char *)bufp),
1737 (uint)((char *)b - (char *)bufp));
1738 error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
1739 if (error) {
1740 return error;
1742 first = b = bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1743 word = 0;
1744 } else {
1746 * Go on to the next word in the buffer
1748 b++;
1752 * If not ending on a word boundary, deal with the last
1753 * (partial) word.
1755 if ((lastbit = len - i)) {
1757 * Compute a mask of relevant bits.
1759 bit = 0;
1760 mask = ((xfs_rtword_t)1 << lastbit) - 1;
1762 * Set/clear the active bits.
1764 if (val)
1765 *b |= mask;
1766 else
1767 *b &= ~mask;
1768 b++;
1771 * Log any remaining changed bytes.
1773 if (b > first)
1774 xfs_trans_log_buf(tp, bp, (uint)((char *)first - (char *)bufp),
1775 (uint)((char *)b - (char *)bufp - 1));
1776 return 0;
1780 * Read and modify the summary information for a given extent size,
1781 * bitmap block combination.
1782 * Keeps track of a current summary block, so we don't keep reading
1783 * it from the buffer cache.
1785 STATIC int /* error */
1786 xfs_rtmodify_summary(
1787 xfs_mount_t *mp, /* file system mount point */
1788 xfs_trans_t *tp, /* transaction pointer */
1789 int log, /* log2 of extent size */
1790 xfs_rtblock_t bbno, /* bitmap block number */
1791 int delta, /* change to make to summary info */
1792 xfs_buf_t **rbpp, /* in/out: summary block buffer */
1793 xfs_fsblock_t *rsb) /* in/out: summary block number */
1795 xfs_buf_t *bp; /* buffer for the summary block */
1796 int error; /* error value */
1797 xfs_fsblock_t sb; /* summary fsblock */
1798 int so; /* index into the summary file */
1799 xfs_suminfo_t *sp; /* pointer to returned data */
1802 * Compute entry number in the summary file.
1804 so = XFS_SUMOFFS(mp, log, bbno);
1806 * Compute the block number in the summary file.
1808 sb = XFS_SUMOFFSTOBLOCK(mp, so);
1810 * If we have an old buffer, and the block number matches, use that.
1812 if (rbpp && *rbpp && *rsb == sb)
1813 bp = *rbpp;
1815 * Otherwise we have to get the buffer.
1817 else {
1819 * If there was an old one, get rid of it first.
1821 if (rbpp && *rbpp)
1822 xfs_trans_brelse(tp, *rbpp);
1823 error = xfs_rtbuf_get(mp, tp, sb, 1, &bp);
1824 if (error) {
1825 return error;
1828 * Remember this buffer and block for the next call.
1830 if (rbpp) {
1831 *rbpp = bp;
1832 *rsb = sb;
1836 * Point to the summary information, modify and log it.
1838 sp = XFS_SUMPTR(mp, bp, so);
1839 *sp += delta;
1840 xfs_trans_log_buf(tp, bp, (uint)((char *)sp - (char *)XFS_BUF_PTR(bp)),
1841 (uint)((char *)sp - (char *)XFS_BUF_PTR(bp) + sizeof(*sp) - 1));
1842 return 0;
1846 * Visible (exported) functions.
1850 * Grow the realtime area of the filesystem.
1853 xfs_growfs_rt(
1854 xfs_mount_t *mp, /* mount point for filesystem */
1855 xfs_growfs_rt_t *in) /* growfs rt input struct */
1857 xfs_rtblock_t bmbno; /* bitmap block number */
1858 xfs_buf_t *bp; /* temporary buffer */
1859 int cancelflags; /* flags for xfs_trans_cancel */
1860 int error; /* error return value */
1861 xfs_inode_t *ip; /* bitmap inode, used as lock */
1862 xfs_mount_t *nmp; /* new (fake) mount structure */
1863 xfs_drfsbno_t nrblocks; /* new number of realtime blocks */
1864 xfs_extlen_t nrbmblocks; /* new number of rt bitmap blocks */
1865 xfs_drtbno_t nrextents; /* new number of realtime extents */
1866 uint8_t nrextslog; /* new log2 of sb_rextents */
1867 xfs_extlen_t nrsumblocks; /* new number of summary blocks */
1868 uint nrsumlevels; /* new rt summary levels */
1869 uint nrsumsize; /* new size of rt summary, bytes */
1870 xfs_sb_t *nsbp; /* new superblock */
1871 xfs_extlen_t rbmblocks; /* current number of rt bitmap blocks */
1872 xfs_extlen_t rsumblocks; /* current number of rt summary blks */
1873 xfs_sb_t *sbp; /* old superblock */
1874 xfs_fsblock_t sumbno; /* summary block number */
1875 xfs_trans_t *tp; /* transaction pointer */
1877 sbp = &mp->m_sb;
1878 cancelflags = 0;
1880 * Initial error checking.
1882 if (mp->m_rtdev_targp == NULL || mp->m_rbmip == NULL ||
1883 (nrblocks = in->newblocks) <= sbp->sb_rblocks ||
1884 (sbp->sb_rblocks && (in->extsize != sbp->sb_rextsize)))
1885 return XFS_ERROR(EINVAL);
1886 if ((error = xfs_sb_validate_fsb_count(sbp, nrblocks)))
1887 return error;
1889 * Read in the last block of the device, make sure it exists.
1891 error = xfs_read_buf(mp, mp->m_rtdev_targp,
1892 XFS_FSB_TO_BB(mp, nrblocks - 1),
1893 XFS_FSB_TO_BB(mp, 1), 0, &bp);
1894 if (error)
1895 return error;
1896 ASSERT(bp);
1897 xfs_buf_relse(bp);
1899 * Calculate new parameters. These are the final values to be reached.
1901 nrextents = nrblocks;
1902 do_div(nrextents, in->extsize);
1903 nrbmblocks = howmany_64(nrextents, NBBY * sbp->sb_blocksize);
1904 nrextslog = xfs_highbit32(nrextents);
1905 nrsumlevels = nrextslog + 1;
1906 nrsumsize = (uint)sizeof(xfs_suminfo_t) * nrsumlevels * nrbmblocks;
1907 nrsumblocks = XFS_B_TO_FSB(mp, nrsumsize);
1908 nrsumsize = XFS_FSB_TO_B(mp, nrsumblocks);
1910 * New summary size can't be more than half the size of
1911 * the log. This prevents us from getting a log overflow,
1912 * since we'll log basically the whole summary file at once.
1914 if (nrsumblocks > (mp->m_sb.sb_logblocks >> 1))
1915 return XFS_ERROR(EINVAL);
1917 * Get the old block counts for bitmap and summary inodes.
1918 * These can't change since other growfs callers are locked out.
1920 rbmblocks = XFS_B_TO_FSB(mp, mp->m_rbmip->i_d.di_size);
1921 rsumblocks = XFS_B_TO_FSB(mp, mp->m_rsumip->i_d.di_size);
1923 * Allocate space to the bitmap and summary files, as necessary.
1925 if ((error = xfs_growfs_rt_alloc(mp, rbmblocks, nrbmblocks,
1926 mp->m_sb.sb_rbmino)))
1927 return error;
1928 if ((error = xfs_growfs_rt_alloc(mp, rsumblocks, nrsumblocks,
1929 mp->m_sb.sb_rsumino)))
1930 return error;
1932 * Allocate a new (fake) mount/sb.
1934 nmp = kmem_alloc(sizeof(*nmp), KM_SLEEP);
1936 * Loop over the bitmap blocks.
1937 * We will do everything one bitmap block at a time.
1938 * Skip the current block if it is exactly full.
1939 * This also deals with the case where there were no rtextents before.
1941 for (bmbno = sbp->sb_rbmblocks -
1942 ((sbp->sb_rextents & ((1 << mp->m_blkbit_log) - 1)) != 0);
1943 bmbno < nrbmblocks;
1944 bmbno++) {
1945 *nmp = *mp;
1946 nsbp = &nmp->m_sb;
1948 * Calculate new sb and mount fields for this round.
1950 nsbp->sb_rextsize = in->extsize;
1951 nsbp->sb_rbmblocks = bmbno + 1;
1952 nsbp->sb_rblocks =
1953 XFS_RTMIN(nrblocks,
1954 nsbp->sb_rbmblocks * NBBY *
1955 nsbp->sb_blocksize * nsbp->sb_rextsize);
1956 nsbp->sb_rextents = nsbp->sb_rblocks;
1957 do_div(nsbp->sb_rextents, nsbp->sb_rextsize);
1958 ASSERT(nsbp->sb_rextents != 0);
1959 nsbp->sb_rextslog = xfs_highbit32(nsbp->sb_rextents);
1960 nrsumlevels = nmp->m_rsumlevels = nsbp->sb_rextslog + 1;
1961 nrsumsize =
1962 (uint)sizeof(xfs_suminfo_t) * nrsumlevels *
1963 nsbp->sb_rbmblocks;
1964 nrsumblocks = XFS_B_TO_FSB(mp, nrsumsize);
1965 nmp->m_rsumsize = nrsumsize = XFS_FSB_TO_B(mp, nrsumblocks);
1967 * Start a transaction, get the log reservation.
1969 tp = xfs_trans_alloc(mp, XFS_TRANS_GROWFSRT_FREE);
1970 cancelflags = 0;
1971 if ((error = xfs_trans_reserve(tp, 0,
1972 XFS_GROWRTFREE_LOG_RES(nmp), 0, 0, 0)))
1973 break;
1975 * Lock out other callers by grabbing the bitmap inode lock.
1977 if ((error = xfs_trans_iget(mp, tp, mp->m_sb.sb_rbmino, 0,
1978 XFS_ILOCK_EXCL, &ip)))
1979 break;
1980 ASSERT(ip == mp->m_rbmip);
1982 * Update the bitmap inode's size.
1984 mp->m_rbmip->i_d.di_size =
1985 nsbp->sb_rbmblocks * nsbp->sb_blocksize;
1986 xfs_trans_log_inode(tp, mp->m_rbmip, XFS_ILOG_CORE);
1987 cancelflags |= XFS_TRANS_ABORT;
1989 * Get the summary inode into the transaction.
1991 if ((error = xfs_trans_iget(mp, tp, mp->m_sb.sb_rsumino, 0,
1992 XFS_ILOCK_EXCL, &ip)))
1993 break;
1994 ASSERT(ip == mp->m_rsumip);
1996 * Update the summary inode's size.
1998 mp->m_rsumip->i_d.di_size = nmp->m_rsumsize;
1999 xfs_trans_log_inode(tp, mp->m_rsumip, XFS_ILOG_CORE);
2001 * Copy summary data from old to new sizes.
2002 * Do this when the real size (not block-aligned) changes.
2004 if (sbp->sb_rbmblocks != nsbp->sb_rbmblocks ||
2005 mp->m_rsumlevels != nmp->m_rsumlevels) {
2006 error = xfs_rtcopy_summary(mp, nmp, tp);
2007 if (error)
2008 break;
2011 * Update superblock fields.
2013 if (nsbp->sb_rextsize != sbp->sb_rextsize)
2014 xfs_trans_mod_sb(tp, XFS_TRANS_SB_REXTSIZE,
2015 nsbp->sb_rextsize - sbp->sb_rextsize);
2016 if (nsbp->sb_rbmblocks != sbp->sb_rbmblocks)
2017 xfs_trans_mod_sb(tp, XFS_TRANS_SB_RBMBLOCKS,
2018 nsbp->sb_rbmblocks - sbp->sb_rbmblocks);
2019 if (nsbp->sb_rblocks != sbp->sb_rblocks)
2020 xfs_trans_mod_sb(tp, XFS_TRANS_SB_RBLOCKS,
2021 nsbp->sb_rblocks - sbp->sb_rblocks);
2022 if (nsbp->sb_rextents != sbp->sb_rextents)
2023 xfs_trans_mod_sb(tp, XFS_TRANS_SB_REXTENTS,
2024 nsbp->sb_rextents - sbp->sb_rextents);
2025 if (nsbp->sb_rextslog != sbp->sb_rextslog)
2026 xfs_trans_mod_sb(tp, XFS_TRANS_SB_REXTSLOG,
2027 nsbp->sb_rextslog - sbp->sb_rextslog);
2029 * Free new extent.
2031 bp = NULL;
2032 error = xfs_rtfree_range(nmp, tp, sbp->sb_rextents,
2033 nsbp->sb_rextents - sbp->sb_rextents, &bp, &sumbno);
2034 if (error)
2035 break;
2037 * Mark more blocks free in the superblock.
2039 xfs_trans_mod_sb(tp, XFS_TRANS_SB_FREXTENTS,
2040 nsbp->sb_rextents - sbp->sb_rextents);
2042 * Update mp values into the real mp structure.
2044 mp->m_rsumlevels = nrsumlevels;
2045 mp->m_rsumsize = nrsumsize;
2047 error = xfs_trans_commit(tp, 0);
2048 if (error) {
2049 tp = NULL;
2050 break;
2054 if (error && tp)
2055 xfs_trans_cancel(tp, cancelflags);
2058 * Free the fake mp structure.
2060 kmem_free(nmp);
2062 return error;
2066 * Allocate an extent in the realtime subvolume, with the usual allocation
2067 * parameters. The length units are all in realtime extents, as is the
2068 * result block number.
2070 int /* error */
2071 xfs_rtallocate_extent(
2072 xfs_trans_t *tp, /* transaction pointer */
2073 xfs_rtblock_t bno, /* starting block number to allocate */
2074 xfs_extlen_t minlen, /* minimum length to allocate */
2075 xfs_extlen_t maxlen, /* maximum length to allocate */
2076 xfs_extlen_t *len, /* out: actual length allocated */
2077 xfs_alloctype_t type, /* allocation type XFS_ALLOCTYPE... */
2078 int wasdel, /* was a delayed allocation extent */
2079 xfs_extlen_t prod, /* extent product factor */
2080 xfs_rtblock_t *rtblock) /* out: start block allocated */
2082 int error; /* error value */
2083 xfs_inode_t *ip; /* inode for bitmap file */
2084 xfs_mount_t *mp; /* file system mount structure */
2085 xfs_rtblock_t r; /* result allocated block */
2086 xfs_fsblock_t sb; /* summary file block number */
2087 xfs_buf_t *sumbp; /* summary file block buffer */
2089 ASSERT(minlen > 0 && minlen <= maxlen);
2090 mp = tp->t_mountp;
2092 * If prod is set then figure out what to do to minlen and maxlen.
2094 if (prod > 1) {
2095 xfs_extlen_t i;
2097 if ((i = maxlen % prod))
2098 maxlen -= i;
2099 if ((i = minlen % prod))
2100 minlen += prod - i;
2101 if (maxlen < minlen) {
2102 *rtblock = NULLRTBLOCK;
2103 return 0;
2107 * Lock out other callers by grabbing the bitmap inode lock.
2109 if ((error = xfs_trans_iget(mp, tp, mp->m_sb.sb_rbmino, 0,
2110 XFS_ILOCK_EXCL, &ip)))
2111 return error;
2112 sumbp = NULL;
2114 * Allocate by size, or near another block, or exactly at some block.
2116 switch (type) {
2117 case XFS_ALLOCTYPE_ANY_AG:
2118 error = xfs_rtallocate_extent_size(mp, tp, minlen, maxlen, len,
2119 &sumbp, &sb, prod, &r);
2120 break;
2121 case XFS_ALLOCTYPE_NEAR_BNO:
2122 error = xfs_rtallocate_extent_near(mp, tp, bno, minlen, maxlen,
2123 len, &sumbp, &sb, prod, &r);
2124 break;
2125 case XFS_ALLOCTYPE_THIS_BNO:
2126 error = xfs_rtallocate_extent_exact(mp, tp, bno, minlen, maxlen,
2127 len, &sumbp, &sb, prod, &r);
2128 break;
2129 default:
2130 ASSERT(0);
2132 if (error) {
2133 return error;
2136 * If it worked, update the superblock.
2138 if (r != NULLRTBLOCK) {
2139 long slen = (long)*len;
2141 ASSERT(*len >= minlen && *len <= maxlen);
2142 if (wasdel)
2143 xfs_trans_mod_sb(tp, XFS_TRANS_SB_RES_FREXTENTS, -slen);
2144 else
2145 xfs_trans_mod_sb(tp, XFS_TRANS_SB_FREXTENTS, -slen);
2147 *rtblock = r;
2148 return 0;
2152 * Free an extent in the realtime subvolume. Length is expressed in
2153 * realtime extents, as is the block number.
2155 int /* error */
2156 xfs_rtfree_extent(
2157 xfs_trans_t *tp, /* transaction pointer */
2158 xfs_rtblock_t bno, /* starting block number to free */
2159 xfs_extlen_t len) /* length of extent freed */
2161 int error; /* error value */
2162 xfs_inode_t *ip; /* bitmap file inode */
2163 xfs_mount_t *mp; /* file system mount structure */
2164 xfs_fsblock_t sb; /* summary file block number */
2165 xfs_buf_t *sumbp; /* summary file block buffer */
2167 mp = tp->t_mountp;
2169 * Synchronize by locking the bitmap inode.
2171 if ((error = xfs_trans_iget(mp, tp, mp->m_sb.sb_rbmino, 0,
2172 XFS_ILOCK_EXCL, &ip)))
2173 return error;
2174 #if defined(__KERNEL__) && defined(DEBUG)
2176 * Check to see that this whole range is currently allocated.
2179 int stat; /* result from checking range */
2181 error = xfs_rtcheck_alloc_range(mp, tp, bno, len, &stat);
2182 if (error) {
2183 return error;
2185 ASSERT(stat);
2187 #endif
2188 sumbp = NULL;
2190 * Free the range of realtime blocks.
2192 error = xfs_rtfree_range(mp, tp, bno, len, &sumbp, &sb);
2193 if (error) {
2194 return error;
2197 * Mark more blocks free in the superblock.
2199 xfs_trans_mod_sb(tp, XFS_TRANS_SB_FREXTENTS, (long)len);
2201 * If we've now freed all the blocks, reset the file sequence
2202 * number to 0.
2204 if (tp->t_frextents_delta + mp->m_sb.sb_frextents ==
2205 mp->m_sb.sb_rextents) {
2206 if (!(ip->i_d.di_flags & XFS_DIFLAG_NEWRTBM))
2207 ip->i_d.di_flags |= XFS_DIFLAG_NEWRTBM;
2208 *(__uint64_t *)&ip->i_d.di_atime = 0;
2209 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
2211 return 0;
2215 * Initialize realtime fields in the mount structure.
2217 int /* error */
2218 xfs_rtmount_init(
2219 xfs_mount_t *mp) /* file system mount structure */
2221 xfs_buf_t *bp; /* buffer for last block of subvolume */
2222 xfs_daddr_t d; /* address of last block of subvolume */
2223 int error; /* error return value */
2224 xfs_sb_t *sbp; /* filesystem superblock copy in mount */
2226 sbp = &mp->m_sb;
2227 if (sbp->sb_rblocks == 0)
2228 return 0;
2229 if (mp->m_rtdev_targp == NULL) {
2230 cmn_err(CE_WARN,
2231 "XFS: This filesystem has a realtime volume, use rtdev=device option");
2232 return XFS_ERROR(ENODEV);
2234 mp->m_rsumlevels = sbp->sb_rextslog + 1;
2235 mp->m_rsumsize =
2236 (uint)sizeof(xfs_suminfo_t) * mp->m_rsumlevels *
2237 sbp->sb_rbmblocks;
2238 mp->m_rsumsize = roundup(mp->m_rsumsize, sbp->sb_blocksize);
2239 mp->m_rbmip = mp->m_rsumip = NULL;
2241 * Check that the realtime section is an ok size.
2243 d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_rblocks);
2244 if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_rblocks) {
2245 cmn_err(CE_WARN, "XFS: realtime mount -- %llu != %llu",
2246 (unsigned long long) XFS_BB_TO_FSB(mp, d),
2247 (unsigned long long) mp->m_sb.sb_rblocks);
2248 return XFS_ERROR(E2BIG);
2250 error = xfs_read_buf(mp, mp->m_rtdev_targp,
2251 d - XFS_FSB_TO_BB(mp, 1),
2252 XFS_FSB_TO_BB(mp, 1), 0, &bp);
2253 if (error) {
2254 cmn_err(CE_WARN,
2255 "XFS: realtime mount -- xfs_read_buf failed, returned %d", error);
2256 if (error == ENOSPC)
2257 return XFS_ERROR(E2BIG);
2258 return error;
2260 xfs_buf_relse(bp);
2261 return 0;
2265 * Get the bitmap and summary inodes into the mount structure
2266 * at mount time.
2268 int /* error */
2269 xfs_rtmount_inodes(
2270 xfs_mount_t *mp) /* file system mount structure */
2272 int error; /* error return value */
2273 xfs_sb_t *sbp;
2275 sbp = &mp->m_sb;
2276 if (sbp->sb_rbmino == NULLFSINO)
2277 return 0;
2278 error = xfs_iget(mp, NULL, sbp->sb_rbmino, 0, 0, &mp->m_rbmip, 0);
2279 if (error)
2280 return error;
2281 ASSERT(mp->m_rbmip != NULL);
2282 ASSERT(sbp->sb_rsumino != NULLFSINO);
2283 error = xfs_iget(mp, NULL, sbp->sb_rsumino, 0, 0, &mp->m_rsumip, 0);
2284 if (error) {
2285 IRELE(mp->m_rbmip);
2286 return error;
2288 ASSERT(mp->m_rsumip != NULL);
2289 return 0;
2293 * Pick an extent for allocation at the start of a new realtime file.
2294 * Use the sequence number stored in the atime field of the bitmap inode.
2295 * Translate this to a fraction of the rtextents, and return the product
2296 * of rtextents and the fraction.
2297 * The fraction sequence is 0, 1/2, 1/4, 3/4, 1/8, ..., 7/8, 1/16, ...
2299 int /* error */
2300 xfs_rtpick_extent(
2301 xfs_mount_t *mp, /* file system mount point */
2302 xfs_trans_t *tp, /* transaction pointer */
2303 xfs_extlen_t len, /* allocation length (rtextents) */
2304 xfs_rtblock_t *pick) /* result rt extent */
2306 xfs_rtblock_t b; /* result block */
2307 int error; /* error return value */
2308 xfs_inode_t *ip; /* bitmap incore inode */
2309 int log2; /* log of sequence number */
2310 __uint64_t resid; /* residual after log removed */
2311 __uint64_t seq; /* sequence number of file creation */
2312 __uint64_t *seqp; /* pointer to seqno in inode */
2314 if ((error = xfs_trans_iget(mp, tp, mp->m_sb.sb_rbmino, 0,
2315 XFS_ILOCK_EXCL, &ip)))
2316 return error;
2317 ASSERT(ip == mp->m_rbmip);
2318 seqp = (__uint64_t *)&ip->i_d.di_atime;
2319 if (!(ip->i_d.di_flags & XFS_DIFLAG_NEWRTBM)) {
2320 ip->i_d.di_flags |= XFS_DIFLAG_NEWRTBM;
2321 *seqp = 0;
2323 seq = *seqp;
2324 if ((log2 = xfs_highbit64(seq)) == -1)
2325 b = 0;
2326 else {
2327 resid = seq - (1ULL << log2);
2328 b = (mp->m_sb.sb_rextents * ((resid << 1) + 1ULL)) >>
2329 (log2 + 1);
2330 if (b >= mp->m_sb.sb_rextents)
2331 b = do_mod(b, mp->m_sb.sb_rextents);
2332 if (b + len > mp->m_sb.sb_rextents)
2333 b = mp->m_sb.sb_rextents - len;
2335 *seqp = seq + 1;
2336 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
2337 *pick = b;
2338 return 0;