x86/amd-iommu: Add per IOMMU reference counting
[linux/fpc-iii.git] / fs / xfs / xfs_rtalloc.c
blob385f6dceba5db357a8bf7361fc24adde3e1185a2
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 committed; /* transaction committed flag */
89 xfs_daddr_t d; /* disk block address */
90 int error; /* error return value */
91 xfs_fsblock_t firstblock; /* first block allocated in xaction */
92 xfs_bmap_free_t flist; /* list of freed blocks */
93 xfs_fsblock_t fsbno; /* filesystem block for bno */
94 xfs_inode_t *ip; /* pointer to incore inode */
95 xfs_bmbt_irec_t map; /* block map output */
96 int nmap; /* number of block maps */
97 int resblks; /* space reservation */
100 * Allocate space to the file, as necessary.
102 while (oblocks < nblocks) {
103 int cancelflags = 0;
104 xfs_trans_t *tp;
106 tp = xfs_trans_alloc(mp, XFS_TRANS_GROWFSRT_ALLOC);
107 resblks = XFS_GROWFSRT_SPACE_RES(mp, nblocks - oblocks);
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 error_cancel:
175 xfs_trans_cancel(tp, cancelflags);
176 goto error;
178 memset(XFS_BUF_PTR(bp), 0, mp->m_sb.sb_blocksize);
179 xfs_trans_log_buf(tp, bp, 0, mp->m_sb.sb_blocksize - 1);
181 * Commit the transaction.
183 error = xfs_trans_commit(tp, 0);
184 if (error)
185 goto error;
188 * Go on to the next extent, if any.
190 oblocks = map.br_startoff + map.br_blockcount;
192 return 0;
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 error; /* error return value */
1860 xfs_inode_t *ip; /* bitmap inode, used as lock */
1861 xfs_mount_t *nmp; /* new (fake) mount structure */
1862 xfs_drfsbno_t nrblocks; /* new number of realtime blocks */
1863 xfs_extlen_t nrbmblocks; /* new number of rt bitmap blocks */
1864 xfs_drtbno_t nrextents; /* new number of realtime extents */
1865 uint8_t nrextslog; /* new log2 of sb_rextents */
1866 xfs_extlen_t nrsumblocks; /* new number of summary blocks */
1867 uint nrsumlevels; /* new rt summary levels */
1868 uint nrsumsize; /* new size of rt summary, bytes */
1869 xfs_sb_t *nsbp; /* new superblock */
1870 xfs_extlen_t rbmblocks; /* current number of rt bitmap blocks */
1871 xfs_extlen_t rsumblocks; /* current number of rt summary blks */
1872 xfs_sb_t *sbp; /* old superblock */
1873 xfs_fsblock_t sumbno; /* summary block number */
1875 sbp = &mp->m_sb;
1877 * Initial error checking.
1879 if (!capable(CAP_SYS_ADMIN))
1880 return XFS_ERROR(EPERM);
1881 if (mp->m_rtdev_targp == NULL || mp->m_rbmip == NULL ||
1882 (nrblocks = in->newblocks) <= sbp->sb_rblocks ||
1883 (sbp->sb_rblocks && (in->extsize != sbp->sb_rextsize)))
1884 return XFS_ERROR(EINVAL);
1885 if ((error = xfs_sb_validate_fsb_count(sbp, nrblocks)))
1886 return error;
1888 * Read in the last block of the device, make sure it exists.
1890 error = xfs_read_buf(mp, mp->m_rtdev_targp,
1891 XFS_FSB_TO_BB(mp, nrblocks - 1),
1892 XFS_FSB_TO_BB(mp, 1), 0, &bp);
1893 if (error)
1894 return error;
1895 ASSERT(bp);
1896 xfs_buf_relse(bp);
1898 * Calculate new parameters. These are the final values to be reached.
1900 nrextents = nrblocks;
1901 do_div(nrextents, in->extsize);
1902 nrbmblocks = howmany_64(nrextents, NBBY * sbp->sb_blocksize);
1903 nrextslog = xfs_highbit32(nrextents);
1904 nrsumlevels = nrextslog + 1;
1905 nrsumsize = (uint)sizeof(xfs_suminfo_t) * nrsumlevels * nrbmblocks;
1906 nrsumblocks = XFS_B_TO_FSB(mp, nrsumsize);
1907 nrsumsize = XFS_FSB_TO_B(mp, nrsumblocks);
1909 * New summary size can't be more than half the size of
1910 * the log. This prevents us from getting a log overflow,
1911 * since we'll log basically the whole summary file at once.
1913 if (nrsumblocks > (mp->m_sb.sb_logblocks >> 1))
1914 return XFS_ERROR(EINVAL);
1916 * Get the old block counts for bitmap and summary inodes.
1917 * These can't change since other growfs callers are locked out.
1919 rbmblocks = XFS_B_TO_FSB(mp, mp->m_rbmip->i_d.di_size);
1920 rsumblocks = XFS_B_TO_FSB(mp, mp->m_rsumip->i_d.di_size);
1922 * Allocate space to the bitmap and summary files, as necessary.
1924 if ((error = xfs_growfs_rt_alloc(mp, rbmblocks, nrbmblocks,
1925 mp->m_sb.sb_rbmino)))
1926 return error;
1927 if ((error = xfs_growfs_rt_alloc(mp, rsumblocks, nrsumblocks,
1928 mp->m_sb.sb_rsumino)))
1929 return error;
1931 * Allocate a new (fake) mount/sb.
1933 nmp = kmem_alloc(sizeof(*nmp), KM_SLEEP);
1935 * Loop over the bitmap blocks.
1936 * We will do everything one bitmap block at a time.
1937 * Skip the current block if it is exactly full.
1938 * This also deals with the case where there were no rtextents before.
1940 for (bmbno = sbp->sb_rbmblocks -
1941 ((sbp->sb_rextents & ((1 << mp->m_blkbit_log) - 1)) != 0);
1942 bmbno < nrbmblocks;
1943 bmbno++) {
1944 xfs_trans_t *tp;
1945 int cancelflags = 0;
1947 *nmp = *mp;
1948 nsbp = &nmp->m_sb;
1950 * Calculate new sb and mount fields for this round.
1952 nsbp->sb_rextsize = in->extsize;
1953 nsbp->sb_rbmblocks = bmbno + 1;
1954 nsbp->sb_rblocks =
1955 XFS_RTMIN(nrblocks,
1956 nsbp->sb_rbmblocks * NBBY *
1957 nsbp->sb_blocksize * nsbp->sb_rextsize);
1958 nsbp->sb_rextents = nsbp->sb_rblocks;
1959 do_div(nsbp->sb_rextents, nsbp->sb_rextsize);
1960 ASSERT(nsbp->sb_rextents != 0);
1961 nsbp->sb_rextslog = xfs_highbit32(nsbp->sb_rextents);
1962 nrsumlevels = nmp->m_rsumlevels = nsbp->sb_rextslog + 1;
1963 nrsumsize =
1964 (uint)sizeof(xfs_suminfo_t) * nrsumlevels *
1965 nsbp->sb_rbmblocks;
1966 nrsumblocks = XFS_B_TO_FSB(mp, nrsumsize);
1967 nmp->m_rsumsize = nrsumsize = XFS_FSB_TO_B(mp, nrsumblocks);
1969 * Start a transaction, get the log reservation.
1971 tp = xfs_trans_alloc(mp, XFS_TRANS_GROWFSRT_FREE);
1972 if ((error = xfs_trans_reserve(tp, 0,
1973 XFS_GROWRTFREE_LOG_RES(nmp), 0, 0, 0)))
1974 goto error_cancel;
1976 * Lock out other callers by grabbing the bitmap inode lock.
1978 if ((error = xfs_trans_iget(mp, tp, mp->m_sb.sb_rbmino, 0,
1979 XFS_ILOCK_EXCL, &ip)))
1980 goto error_cancel;
1981 ASSERT(ip == mp->m_rbmip);
1983 * Update the bitmap inode's size.
1985 mp->m_rbmip->i_d.di_size =
1986 nsbp->sb_rbmblocks * nsbp->sb_blocksize;
1987 xfs_trans_log_inode(tp, mp->m_rbmip, XFS_ILOG_CORE);
1988 cancelflags |= XFS_TRANS_ABORT;
1990 * Get the summary inode into the transaction.
1992 if ((error = xfs_trans_iget(mp, tp, mp->m_sb.sb_rsumino, 0,
1993 XFS_ILOCK_EXCL, &ip)))
1994 goto error_cancel;
1995 ASSERT(ip == mp->m_rsumip);
1997 * Update the summary inode's size.
1999 mp->m_rsumip->i_d.di_size = nmp->m_rsumsize;
2000 xfs_trans_log_inode(tp, mp->m_rsumip, XFS_ILOG_CORE);
2002 * Copy summary data from old to new sizes.
2003 * Do this when the real size (not block-aligned) changes.
2005 if (sbp->sb_rbmblocks != nsbp->sb_rbmblocks ||
2006 mp->m_rsumlevels != nmp->m_rsumlevels) {
2007 error = xfs_rtcopy_summary(mp, nmp, tp);
2008 if (error)
2009 goto error_cancel;
2012 * Update superblock fields.
2014 if (nsbp->sb_rextsize != sbp->sb_rextsize)
2015 xfs_trans_mod_sb(tp, XFS_TRANS_SB_REXTSIZE,
2016 nsbp->sb_rextsize - sbp->sb_rextsize);
2017 if (nsbp->sb_rbmblocks != sbp->sb_rbmblocks)
2018 xfs_trans_mod_sb(tp, XFS_TRANS_SB_RBMBLOCKS,
2019 nsbp->sb_rbmblocks - sbp->sb_rbmblocks);
2020 if (nsbp->sb_rblocks != sbp->sb_rblocks)
2021 xfs_trans_mod_sb(tp, XFS_TRANS_SB_RBLOCKS,
2022 nsbp->sb_rblocks - sbp->sb_rblocks);
2023 if (nsbp->sb_rextents != sbp->sb_rextents)
2024 xfs_trans_mod_sb(tp, XFS_TRANS_SB_REXTENTS,
2025 nsbp->sb_rextents - sbp->sb_rextents);
2026 if (nsbp->sb_rextslog != sbp->sb_rextslog)
2027 xfs_trans_mod_sb(tp, XFS_TRANS_SB_REXTSLOG,
2028 nsbp->sb_rextslog - sbp->sb_rextslog);
2030 * Free new extent.
2032 bp = NULL;
2033 error = xfs_rtfree_range(nmp, tp, sbp->sb_rextents,
2034 nsbp->sb_rextents - sbp->sb_rextents, &bp, &sumbno);
2035 if (error) {
2036 error_cancel:
2037 xfs_trans_cancel(tp, cancelflags);
2038 break;
2041 * Mark more blocks free in the superblock.
2043 xfs_trans_mod_sb(tp, XFS_TRANS_SB_FREXTENTS,
2044 nsbp->sb_rextents - sbp->sb_rextents);
2046 * Update mp values into the real mp structure.
2048 mp->m_rsumlevels = nrsumlevels;
2049 mp->m_rsumsize = nrsumsize;
2051 error = xfs_trans_commit(tp, 0);
2052 if (error)
2053 break;
2057 * Free the fake mp structure.
2059 kmem_free(nmp);
2061 return error;
2065 * Allocate an extent in the realtime subvolume, with the usual allocation
2066 * parameters. The length units are all in realtime extents, as is the
2067 * result block number.
2069 int /* error */
2070 xfs_rtallocate_extent(
2071 xfs_trans_t *tp, /* transaction pointer */
2072 xfs_rtblock_t bno, /* starting block number to allocate */
2073 xfs_extlen_t minlen, /* minimum length to allocate */
2074 xfs_extlen_t maxlen, /* maximum length to allocate */
2075 xfs_extlen_t *len, /* out: actual length allocated */
2076 xfs_alloctype_t type, /* allocation type XFS_ALLOCTYPE... */
2077 int wasdel, /* was a delayed allocation extent */
2078 xfs_extlen_t prod, /* extent product factor */
2079 xfs_rtblock_t *rtblock) /* out: start block allocated */
2081 int error; /* error value */
2082 xfs_inode_t *ip; /* inode for bitmap file */
2083 xfs_mount_t *mp; /* file system mount structure */
2084 xfs_rtblock_t r; /* result allocated block */
2085 xfs_fsblock_t sb; /* summary file block number */
2086 xfs_buf_t *sumbp; /* summary file block buffer */
2088 ASSERT(minlen > 0 && minlen <= maxlen);
2089 mp = tp->t_mountp;
2091 * If prod is set then figure out what to do to minlen and maxlen.
2093 if (prod > 1) {
2094 xfs_extlen_t i;
2096 if ((i = maxlen % prod))
2097 maxlen -= i;
2098 if ((i = minlen % prod))
2099 minlen += prod - i;
2100 if (maxlen < minlen) {
2101 *rtblock = NULLRTBLOCK;
2102 return 0;
2106 * Lock out other callers by grabbing the bitmap inode lock.
2108 if ((error = xfs_trans_iget(mp, tp, mp->m_sb.sb_rbmino, 0,
2109 XFS_ILOCK_EXCL, &ip)))
2110 return error;
2111 sumbp = NULL;
2113 * Allocate by size, or near another block, or exactly at some block.
2115 switch (type) {
2116 case XFS_ALLOCTYPE_ANY_AG:
2117 error = xfs_rtallocate_extent_size(mp, tp, minlen, maxlen, len,
2118 &sumbp, &sb, prod, &r);
2119 break;
2120 case XFS_ALLOCTYPE_NEAR_BNO:
2121 error = xfs_rtallocate_extent_near(mp, tp, bno, minlen, maxlen,
2122 len, &sumbp, &sb, prod, &r);
2123 break;
2124 case XFS_ALLOCTYPE_THIS_BNO:
2125 error = xfs_rtallocate_extent_exact(mp, tp, bno, minlen, maxlen,
2126 len, &sumbp, &sb, prod, &r);
2127 break;
2128 default:
2129 ASSERT(0);
2131 if (error) {
2132 return error;
2135 * If it worked, update the superblock.
2137 if (r != NULLRTBLOCK) {
2138 long slen = (long)*len;
2140 ASSERT(*len >= minlen && *len <= maxlen);
2141 if (wasdel)
2142 xfs_trans_mod_sb(tp, XFS_TRANS_SB_RES_FREXTENTS, -slen);
2143 else
2144 xfs_trans_mod_sb(tp, XFS_TRANS_SB_FREXTENTS, -slen);
2146 *rtblock = r;
2147 return 0;
2151 * Free an extent in the realtime subvolume. Length is expressed in
2152 * realtime extents, as is the block number.
2154 int /* error */
2155 xfs_rtfree_extent(
2156 xfs_trans_t *tp, /* transaction pointer */
2157 xfs_rtblock_t bno, /* starting block number to free */
2158 xfs_extlen_t len) /* length of extent freed */
2160 int error; /* error value */
2161 xfs_inode_t *ip; /* bitmap file inode */
2162 xfs_mount_t *mp; /* file system mount structure */
2163 xfs_fsblock_t sb; /* summary file block number */
2164 xfs_buf_t *sumbp; /* summary file block buffer */
2166 mp = tp->t_mountp;
2168 * Synchronize by locking the bitmap inode.
2170 if ((error = xfs_trans_iget(mp, tp, mp->m_sb.sb_rbmino, 0,
2171 XFS_ILOCK_EXCL, &ip)))
2172 return error;
2173 #if defined(__KERNEL__) && defined(DEBUG)
2175 * Check to see that this whole range is currently allocated.
2178 int stat; /* result from checking range */
2180 error = xfs_rtcheck_alloc_range(mp, tp, bno, len, &stat);
2181 if (error) {
2182 return error;
2184 ASSERT(stat);
2186 #endif
2187 sumbp = NULL;
2189 * Free the range of realtime blocks.
2191 error = xfs_rtfree_range(mp, tp, bno, len, &sumbp, &sb);
2192 if (error) {
2193 return error;
2196 * Mark more blocks free in the superblock.
2198 xfs_trans_mod_sb(tp, XFS_TRANS_SB_FREXTENTS, (long)len);
2200 * If we've now freed all the blocks, reset the file sequence
2201 * number to 0.
2203 if (tp->t_frextents_delta + mp->m_sb.sb_frextents ==
2204 mp->m_sb.sb_rextents) {
2205 if (!(ip->i_d.di_flags & XFS_DIFLAG_NEWRTBM))
2206 ip->i_d.di_flags |= XFS_DIFLAG_NEWRTBM;
2207 *(__uint64_t *)&ip->i_d.di_atime = 0;
2208 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
2210 return 0;
2214 * Initialize realtime fields in the mount structure.
2216 int /* error */
2217 xfs_rtmount_init(
2218 xfs_mount_t *mp) /* file system mount structure */
2220 xfs_buf_t *bp; /* buffer for last block of subvolume */
2221 xfs_daddr_t d; /* address of last block of subvolume */
2222 int error; /* error return value */
2223 xfs_sb_t *sbp; /* filesystem superblock copy in mount */
2225 sbp = &mp->m_sb;
2226 if (sbp->sb_rblocks == 0)
2227 return 0;
2228 if (mp->m_rtdev_targp == NULL) {
2229 cmn_err(CE_WARN,
2230 "XFS: This filesystem has a realtime volume, use rtdev=device option");
2231 return XFS_ERROR(ENODEV);
2233 mp->m_rsumlevels = sbp->sb_rextslog + 1;
2234 mp->m_rsumsize =
2235 (uint)sizeof(xfs_suminfo_t) * mp->m_rsumlevels *
2236 sbp->sb_rbmblocks;
2237 mp->m_rsumsize = roundup(mp->m_rsumsize, sbp->sb_blocksize);
2238 mp->m_rbmip = mp->m_rsumip = NULL;
2240 * Check that the realtime section is an ok size.
2242 d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_rblocks);
2243 if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_rblocks) {
2244 cmn_err(CE_WARN, "XFS: realtime mount -- %llu != %llu",
2245 (unsigned long long) XFS_BB_TO_FSB(mp, d),
2246 (unsigned long long) mp->m_sb.sb_rblocks);
2247 return XFS_ERROR(E2BIG);
2249 error = xfs_read_buf(mp, mp->m_rtdev_targp,
2250 d - XFS_FSB_TO_BB(mp, 1),
2251 XFS_FSB_TO_BB(mp, 1), 0, &bp);
2252 if (error) {
2253 cmn_err(CE_WARN,
2254 "XFS: realtime mount -- xfs_read_buf failed, returned %d", error);
2255 if (error == ENOSPC)
2256 return XFS_ERROR(E2BIG);
2257 return error;
2259 xfs_buf_relse(bp);
2260 return 0;
2264 * Get the bitmap and summary inodes into the mount structure
2265 * at mount time.
2267 int /* error */
2268 xfs_rtmount_inodes(
2269 xfs_mount_t *mp) /* file system mount structure */
2271 int error; /* error return value */
2272 xfs_sb_t *sbp;
2274 sbp = &mp->m_sb;
2275 if (sbp->sb_rbmino == NULLFSINO)
2276 return 0;
2277 error = xfs_iget(mp, NULL, sbp->sb_rbmino, 0, 0, &mp->m_rbmip, 0);
2278 if (error)
2279 return error;
2280 ASSERT(mp->m_rbmip != NULL);
2281 ASSERT(sbp->sb_rsumino != NULLFSINO);
2282 error = xfs_iget(mp, NULL, sbp->sb_rsumino, 0, 0, &mp->m_rsumip, 0);
2283 if (error) {
2284 IRELE(mp->m_rbmip);
2285 return error;
2287 ASSERT(mp->m_rsumip != NULL);
2288 return 0;
2291 void
2292 xfs_rtunmount_inodes(
2293 struct xfs_mount *mp)
2295 if (mp->m_rbmip)
2296 IRELE(mp->m_rbmip);
2297 if (mp->m_rsumip)
2298 IRELE(mp->m_rsumip);
2302 * Pick an extent for allocation at the start of a new realtime file.
2303 * Use the sequence number stored in the atime field of the bitmap inode.
2304 * Translate this to a fraction of the rtextents, and return the product
2305 * of rtextents and the fraction.
2306 * The fraction sequence is 0, 1/2, 1/4, 3/4, 1/8, ..., 7/8, 1/16, ...
2308 int /* error */
2309 xfs_rtpick_extent(
2310 xfs_mount_t *mp, /* file system mount point */
2311 xfs_trans_t *tp, /* transaction pointer */
2312 xfs_extlen_t len, /* allocation length (rtextents) */
2313 xfs_rtblock_t *pick) /* result rt extent */
2315 xfs_rtblock_t b; /* result block */
2316 int error; /* error return value */
2317 xfs_inode_t *ip; /* bitmap incore inode */
2318 int log2; /* log of sequence number */
2319 __uint64_t resid; /* residual after log removed */
2320 __uint64_t seq; /* sequence number of file creation */
2321 __uint64_t *seqp; /* pointer to seqno in inode */
2323 if ((error = xfs_trans_iget(mp, tp, mp->m_sb.sb_rbmino, 0,
2324 XFS_ILOCK_EXCL, &ip)))
2325 return error;
2326 ASSERT(ip == mp->m_rbmip);
2327 seqp = (__uint64_t *)&ip->i_d.di_atime;
2328 if (!(ip->i_d.di_flags & XFS_DIFLAG_NEWRTBM)) {
2329 ip->i_d.di_flags |= XFS_DIFLAG_NEWRTBM;
2330 *seqp = 0;
2332 seq = *seqp;
2333 if ((log2 = xfs_highbit64(seq)) == -1)
2334 b = 0;
2335 else {
2336 resid = seq - (1ULL << log2);
2337 b = (mp->m_sb.sb_rextents * ((resid << 1) + 1ULL)) >>
2338 (log2 + 1);
2339 if (b >= mp->m_sb.sb_rextents)
2340 b = do_mod(b, mp->m_sb.sb_rextents);
2341 if (b + len > mp->m_sb.sb_rextents)
2342 b = mp->m_sb.sb_rextents - len;
2344 *seqp = seq + 1;
2345 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
2346 *pick = b;
2347 return 0;