2 * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 #include "xfs_types.h"
24 #include "xfs_trans.h"
29 #include "xfs_dmapi.h"
30 #include "xfs_mount.h"
31 #include "xfs_bmap_btree.h"
32 #include "xfs_alloc_btree.h"
33 #include "xfs_ialloc_btree.h"
34 #include "xfs_dir_sf.h"
35 #include "xfs_dir2_sf.h"
36 #include "xfs_attr_sf.h"
37 #include "xfs_dinode.h"
38 #include "xfs_inode.h"
39 #include "xfs_btree.h"
40 #include "xfs_ialloc.h"
41 #include "xfs_error.h"
44 * Cursor allocation zone.
46 kmem_zone_t *xfs_btree_cur_zone;
49 * Btree magic numbers.
51 const __uint32_t xfs_magics[XFS_BTNUM_MAX] =
53 XFS_ABTB_MAGIC, XFS_ABTC_MAGIC, XFS_BMAP_MAGIC, XFS_IBT_MAGIC
57 * Prototypes for internal routines.
61 * Checking routine: return maxrecs for the block.
63 STATIC int /* number of records fitting in block */
65 xfs_btree_cur_t *cur, /* btree cursor */
66 xfs_btree_block_t *block);/* generic btree block pointer */
73 * Retrieve the block pointer from the cursor at the given level.
74 * This may be a bmap btree root or from a buffer.
76 STATIC xfs_btree_block_t * /* generic btree block pointer */
78 xfs_btree_cur_t *cur, /* btree cursor */
79 int level, /* level in btree */
80 struct xfs_buf **bpp); /* buffer containing the block */
83 * Checking routine: return maxrecs for the block.
85 STATIC int /* number of records fitting in block */
87 xfs_btree_cur_t *cur, /* btree cursor */
88 xfs_btree_block_t *block) /* generic btree block pointer */
90 switch (cur->bc_btnum) {
93 return (int)XFS_ALLOC_BLOCK_MAXRECS(
94 be16_to_cpu(block->bb_h.bb_level), cur);
96 return (int)XFS_BMAP_BLOCK_IMAXRECS(
97 be16_to_cpu(block->bb_h.bb_level), cur);
99 return (int)XFS_INOBT_BLOCK_MAXRECS(
100 be16_to_cpu(block->bb_h.bb_level), cur);
113 * Debug routine: check that block header is ok.
116 xfs_btree_check_block(
117 xfs_btree_cur_t *cur, /* btree cursor */
118 xfs_btree_block_t *block, /* generic btree block pointer */
119 int level, /* level of the btree block */
120 xfs_buf_t *bp) /* buffer containing block, if any */
122 if (XFS_BTREE_LONG_PTRS(cur->bc_btnum))
123 xfs_btree_check_lblock(cur, (xfs_btree_lblock_t *)block, level,
126 xfs_btree_check_sblock(cur, (xfs_btree_sblock_t *)block, level,
131 * Debug routine: check that keys are in the right order.
135 xfs_btnum_t btnum, /* btree identifier */
136 void *ak1, /* pointer to left (lower) key */
137 void *ak2) /* pointer to right (higher) key */
140 case XFS_BTNUM_BNO: {
146 ASSERT(be32_to_cpu(k1->ar_startblock) < be32_to_cpu(k2->ar_startblock));
149 case XFS_BTNUM_CNT: {
155 ASSERT(be32_to_cpu(k1->ar_blockcount) < be32_to_cpu(k2->ar_blockcount) ||
156 (k1->ar_blockcount == k2->ar_blockcount &&
157 be32_to_cpu(k1->ar_startblock) < be32_to_cpu(k2->ar_startblock)));
160 case XFS_BTNUM_BMAP: {
166 ASSERT(INT_GET(k1->br_startoff, ARCH_CONVERT) < INT_GET(k2->br_startoff, ARCH_CONVERT));
169 case XFS_BTNUM_INO: {
175 ASSERT(INT_GET(k1->ir_startino, ARCH_CONVERT) < INT_GET(k2->ir_startino, ARCH_CONVERT));
185 * Checking routine: check that long form block header is ok.
188 int /* error (0 or EFSCORRUPTED) */
189 xfs_btree_check_lblock(
190 xfs_btree_cur_t *cur, /* btree cursor */
191 xfs_btree_lblock_t *block, /* btree long form block pointer */
192 int level, /* level of the btree block */
193 xfs_buf_t *bp) /* buffer for block, if any */
195 int lblock_ok; /* block passes checks */
196 xfs_mount_t *mp; /* file system mount point */
200 be32_to_cpu(block->bb_magic) == xfs_magics[cur->bc_btnum] &&
201 be16_to_cpu(block->bb_level) == level &&
202 be16_to_cpu(block->bb_numrecs) <=
203 xfs_btree_maxrecs(cur, (xfs_btree_block_t *)block) &&
205 (be64_to_cpu(block->bb_leftsib) == NULLDFSBNO ||
206 XFS_FSB_SANITY_CHECK(mp, be64_to_cpu(block->bb_leftsib))) &&
207 block->bb_rightsib &&
208 (be64_to_cpu(block->bb_rightsib) == NULLDFSBNO ||
209 XFS_FSB_SANITY_CHECK(mp, be64_to_cpu(block->bb_rightsib)));
210 if (unlikely(XFS_TEST_ERROR(!lblock_ok, mp, XFS_ERRTAG_BTREE_CHECK_LBLOCK,
211 XFS_RANDOM_BTREE_CHECK_LBLOCK))) {
213 xfs_buftrace("LBTREE ERROR", bp);
214 XFS_ERROR_REPORT("xfs_btree_check_lblock", XFS_ERRLEVEL_LOW,
216 return XFS_ERROR(EFSCORRUPTED);
222 * Checking routine: check that (long) pointer is ok.
224 int /* error (0 or EFSCORRUPTED) */
225 xfs_btree_check_lptr(
226 xfs_btree_cur_t *cur, /* btree cursor */
227 xfs_dfsbno_t ptr, /* btree block disk address */
228 int level) /* btree block level */
230 xfs_mount_t *mp; /* file system mount point */
233 XFS_WANT_CORRUPTED_RETURN(
236 XFS_FSB_SANITY_CHECK(mp, ptr));
242 * Debug routine: check that records are in the right order.
246 xfs_btnum_t btnum, /* btree identifier */
247 void *ar1, /* pointer to left (lower) record */
248 void *ar2) /* pointer to right (higher) record */
251 case XFS_BTNUM_BNO: {
257 ASSERT(be32_to_cpu(r1->ar_startblock) +
258 be32_to_cpu(r1->ar_blockcount) <=
259 be32_to_cpu(r2->ar_startblock));
262 case XFS_BTNUM_CNT: {
268 ASSERT(be32_to_cpu(r1->ar_blockcount) < be32_to_cpu(r2->ar_blockcount) ||
269 (r1->ar_blockcount == r2->ar_blockcount &&
270 be32_to_cpu(r1->ar_startblock) < be32_to_cpu(r2->ar_startblock)));
273 case XFS_BTNUM_BMAP: {
279 ASSERT(xfs_bmbt_disk_get_startoff(r1) +
280 xfs_bmbt_disk_get_blockcount(r1) <=
281 xfs_bmbt_disk_get_startoff(r2));
284 case XFS_BTNUM_INO: {
290 ASSERT(INT_GET(r1->ir_startino, ARCH_CONVERT) + XFS_INODES_PER_CHUNK <=
291 INT_GET(r2->ir_startino, ARCH_CONVERT));
301 * Checking routine: check that block header is ok.
304 int /* error (0 or EFSCORRUPTED) */
305 xfs_btree_check_sblock(
306 xfs_btree_cur_t *cur, /* btree cursor */
307 xfs_btree_sblock_t *block, /* btree short form block pointer */
308 int level, /* level of the btree block */
309 xfs_buf_t *bp) /* buffer containing block */
311 xfs_buf_t *agbp; /* buffer for ag. freespace struct */
312 xfs_agf_t *agf; /* ag. freespace structure */
313 xfs_agblock_t agflen; /* native ag. freespace length */
314 int sblock_ok; /* block passes checks */
316 agbp = cur->bc_private.a.agbp;
317 agf = XFS_BUF_TO_AGF(agbp);
318 agflen = be32_to_cpu(agf->agf_length);
320 be32_to_cpu(block->bb_magic) == xfs_magics[cur->bc_btnum] &&
321 be16_to_cpu(block->bb_level) == level &&
322 be16_to_cpu(block->bb_numrecs) <=
323 xfs_btree_maxrecs(cur, (xfs_btree_block_t *)block) &&
324 (be32_to_cpu(block->bb_leftsib) == NULLAGBLOCK ||
325 be32_to_cpu(block->bb_leftsib) < agflen) &&
327 (be32_to_cpu(block->bb_rightsib) == NULLAGBLOCK ||
328 be32_to_cpu(block->bb_rightsib) < agflen) &&
330 if (unlikely(XFS_TEST_ERROR(!sblock_ok, cur->bc_mp,
331 XFS_ERRTAG_BTREE_CHECK_SBLOCK,
332 XFS_RANDOM_BTREE_CHECK_SBLOCK))) {
334 xfs_buftrace("SBTREE ERROR", bp);
335 XFS_ERROR_REPORT("xfs_btree_check_sblock", XFS_ERRLEVEL_LOW,
337 return XFS_ERROR(EFSCORRUPTED);
343 * Checking routine: check that (short) pointer is ok.
345 int /* error (0 or EFSCORRUPTED) */
346 xfs_btree_check_sptr(
347 xfs_btree_cur_t *cur, /* btree cursor */
348 xfs_agblock_t ptr, /* btree block disk address */
349 int level) /* btree block level */
351 xfs_buf_t *agbp; /* buffer for ag. freespace struct */
352 xfs_agf_t *agf; /* ag. freespace structure */
354 agbp = cur->bc_private.a.agbp;
355 agf = XFS_BUF_TO_AGF(agbp);
356 XFS_WANT_CORRUPTED_RETURN(
358 ptr != NULLAGBLOCK && ptr != 0 &&
359 ptr < be32_to_cpu(agf->agf_length));
364 * Delete the btree cursor.
367 xfs_btree_del_cursor(
368 xfs_btree_cur_t *cur, /* btree cursor */
369 int error) /* del because of error */
371 int i; /* btree level */
374 * Clear the buffer pointers, and release the buffers.
375 * If we're doing this in the face of an error, we
376 * need to make sure to inspect all of the entries
377 * in the bc_bufs array for buffers to be unlocked.
378 * This is because some of the btree code works from
379 * level n down to 0, and if we get an error along
380 * the way we won't have initialized all the entries
383 for (i = 0; i < cur->bc_nlevels; i++) {
385 xfs_btree_setbuf(cur, i, NULL);
390 * Can't free a bmap cursor without having dealt with the
391 * allocated indirect blocks' accounting.
393 ASSERT(cur->bc_btnum != XFS_BTNUM_BMAP ||
394 cur->bc_private.b.allocated == 0);
398 kmem_zone_free(xfs_btree_cur_zone, cur);
402 * Duplicate the btree cursor.
403 * Allocate a new one, copy the record, re-get the buffers.
406 xfs_btree_dup_cursor(
407 xfs_btree_cur_t *cur, /* input cursor */
408 xfs_btree_cur_t **ncur) /* output cursor */
410 xfs_buf_t *bp; /* btree block's buffer pointer */
411 int error; /* error return value */
412 int i; /* level number of btree block */
413 xfs_mount_t *mp; /* mount structure for filesystem */
414 xfs_btree_cur_t *new; /* new cursor value */
415 xfs_trans_t *tp; /* transaction pointer, can be NULL */
420 * Allocate a new cursor like the old one.
422 new = xfs_btree_init_cursor(mp, tp, cur->bc_private.a.agbp,
423 cur->bc_private.a.agno, cur->bc_btnum, cur->bc_private.b.ip,
424 cur->bc_private.b.whichfork);
426 * Copy the record currently in the cursor.
428 new->bc_rec = cur->bc_rec;
430 * For each level current, re-get the buffer and copy the ptr value.
432 for (i = 0; i < new->bc_nlevels; i++) {
433 new->bc_ptrs[i] = cur->bc_ptrs[i];
434 new->bc_ra[i] = cur->bc_ra[i];
435 if ((bp = cur->bc_bufs[i])) {
436 if ((error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp,
437 XFS_BUF_ADDR(bp), mp->m_bsize, 0, &bp))) {
438 xfs_btree_del_cursor(new, error);
442 new->bc_bufs[i] = bp;
444 ASSERT(!XFS_BUF_GETERROR(bp));
446 new->bc_bufs[i] = NULL;
449 * For bmap btrees, copy the firstblock, flist, and flags values,
450 * since init cursor doesn't get them.
452 if (new->bc_btnum == XFS_BTNUM_BMAP) {
453 new->bc_private.b.firstblock = cur->bc_private.b.firstblock;
454 new->bc_private.b.flist = cur->bc_private.b.flist;
455 new->bc_private.b.flags = cur->bc_private.b.flags;
462 * Change the cursor to point to the first record at the given level.
463 * Other levels are unaffected.
465 int /* success=1, failure=0 */
467 xfs_btree_cur_t *cur, /* btree cursor */
468 int level) /* level to change */
470 xfs_btree_block_t *block; /* generic btree block pointer */
471 xfs_buf_t *bp; /* buffer containing block */
474 * Get the block pointer for this level.
476 block = xfs_btree_get_block(cur, level, &bp);
477 xfs_btree_check_block(cur, block, level, bp);
479 * It's empty, there is no such record.
481 if (!block->bb_h.bb_numrecs)
484 * Set the ptr value to 1, that's the first record/key.
486 cur->bc_ptrs[level] = 1;
491 * Retrieve the block pointer from the cursor at the given level.
492 * This may be a bmap btree root or from a buffer.
494 STATIC xfs_btree_block_t * /* generic btree block pointer */
496 xfs_btree_cur_t *cur, /* btree cursor */
497 int level, /* level in btree */
498 xfs_buf_t **bpp) /* buffer containing the block */
500 xfs_btree_block_t *block; /* return value */
501 xfs_buf_t *bp; /* return buffer */
502 xfs_ifork_t *ifp; /* inode fork pointer */
503 int whichfork; /* data or attr fork */
505 if (cur->bc_btnum == XFS_BTNUM_BMAP && level == cur->bc_nlevels - 1) {
506 whichfork = cur->bc_private.b.whichfork;
507 ifp = XFS_IFORK_PTR(cur->bc_private.b.ip, whichfork);
508 block = (xfs_btree_block_t *)ifp->if_broot;
511 bp = cur->bc_bufs[level];
512 block = XFS_BUF_TO_BLOCK(bp);
514 ASSERT(block != NULL);
520 * Get a buffer for the block, return it with no data read.
521 * Long-form addressing.
523 xfs_buf_t * /* buffer for fsbno */
525 xfs_mount_t *mp, /* file system mount point */
526 xfs_trans_t *tp, /* transaction pointer */
527 xfs_fsblock_t fsbno, /* file system block number */
528 uint lock) /* lock flags for get_buf */
530 xfs_buf_t *bp; /* buffer pointer (return value) */
531 xfs_daddr_t d; /* real disk block address */
533 ASSERT(fsbno != NULLFSBLOCK);
534 d = XFS_FSB_TO_DADDR(mp, fsbno);
535 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d, mp->m_bsize, lock);
537 ASSERT(!XFS_BUF_GETERROR(bp));
542 * Get a buffer for the block, return it with no data read.
543 * Short-form addressing.
545 xfs_buf_t * /* buffer for agno/agbno */
547 xfs_mount_t *mp, /* file system mount point */
548 xfs_trans_t *tp, /* transaction pointer */
549 xfs_agnumber_t agno, /* allocation group number */
550 xfs_agblock_t agbno, /* allocation group block number */
551 uint lock) /* lock flags for get_buf */
553 xfs_buf_t *bp; /* buffer pointer (return value) */
554 xfs_daddr_t d; /* real disk block address */
556 ASSERT(agno != NULLAGNUMBER);
557 ASSERT(agbno != NULLAGBLOCK);
558 d = XFS_AGB_TO_DADDR(mp, agno, agbno);
559 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d, mp->m_bsize, lock);
561 ASSERT(!XFS_BUF_GETERROR(bp));
566 * Allocate a new btree cursor.
567 * The cursor is either for allocation (A) or bmap (B) or inodes (I).
569 xfs_btree_cur_t * /* new btree cursor */
570 xfs_btree_init_cursor(
571 xfs_mount_t *mp, /* file system mount point */
572 xfs_trans_t *tp, /* transaction pointer */
573 xfs_buf_t *agbp, /* (A only) buffer for agf structure */
574 /* (I only) buffer for agi structure */
575 xfs_agnumber_t agno, /* (AI only) allocation group number */
576 xfs_btnum_t btnum, /* btree identifier */
577 xfs_inode_t *ip, /* (B only) inode owning the btree */
578 int whichfork) /* (B only) data or attr fork */
580 xfs_agf_t *agf; /* (A) allocation group freespace */
581 xfs_agi_t *agi; /* (I) allocation group inodespace */
582 xfs_btree_cur_t *cur; /* return value */
583 xfs_ifork_t *ifp; /* (I) inode fork pointer */
584 int nlevels=0; /* number of levels in the btree */
586 ASSERT(xfs_btree_cur_zone != NULL);
588 * Allocate a new cursor.
590 cur = kmem_zone_zalloc(xfs_btree_cur_zone, KM_SLEEP);
592 * Deduce the number of btree levels from the arguments.
597 agf = XFS_BUF_TO_AGF(agbp);
598 nlevels = be32_to_cpu(agf->agf_levels[btnum]);
601 ifp = XFS_IFORK_PTR(ip, whichfork);
602 nlevels = be16_to_cpu(ifp->if_broot->bb_level) + 1;
605 agi = XFS_BUF_TO_AGI(agbp);
606 nlevels = be32_to_cpu(agi->agi_level);
612 * Fill in the common fields.
616 cur->bc_nlevels = nlevels;
617 cur->bc_btnum = btnum;
618 cur->bc_blocklog = mp->m_sb.sb_blocklog;
620 * Fill in private fields.
626 * Allocation btree fields.
628 cur->bc_private.a.agbp = agbp;
629 cur->bc_private.a.agno = agno;
635 cur->bc_private.b.forksize = XFS_IFORK_SIZE(ip, whichfork);
636 cur->bc_private.b.ip = ip;
637 cur->bc_private.b.firstblock = NULLFSBLOCK;
638 cur->bc_private.b.flist = NULL;
639 cur->bc_private.b.allocated = 0;
640 cur->bc_private.b.flags = 0;
641 cur->bc_private.b.whichfork = whichfork;
645 * Inode allocation btree fields.
647 cur->bc_private.i.agbp = agbp;
648 cur->bc_private.i.agno = agno;
657 * Check for the cursor referring to the last block at the given level.
659 int /* 1=is last block, 0=not last block */
660 xfs_btree_islastblock(
661 xfs_btree_cur_t *cur, /* btree cursor */
662 int level) /* level to check */
664 xfs_btree_block_t *block; /* generic btree block pointer */
665 xfs_buf_t *bp; /* buffer containing block */
667 block = xfs_btree_get_block(cur, level, &bp);
668 xfs_btree_check_block(cur, block, level, bp);
669 if (XFS_BTREE_LONG_PTRS(cur->bc_btnum))
670 return be64_to_cpu(block->bb_u.l.bb_rightsib) == NULLDFSBNO;
672 return be32_to_cpu(block->bb_u.s.bb_rightsib) == NULLAGBLOCK;
676 * Change the cursor to point to the last record in the current block
677 * at the given level. Other levels are unaffected.
679 int /* success=1, failure=0 */
681 xfs_btree_cur_t *cur, /* btree cursor */
682 int level) /* level to change */
684 xfs_btree_block_t *block; /* generic btree block pointer */
685 xfs_buf_t *bp; /* buffer containing block */
688 * Get the block pointer for this level.
690 block = xfs_btree_get_block(cur, level, &bp);
691 xfs_btree_check_block(cur, block, level, bp);
693 * It's empty, there is no such record.
695 if (!block->bb_h.bb_numrecs)
698 * Set the ptr value to numrecs, that's the last record/key.
700 cur->bc_ptrs[level] = be16_to_cpu(block->bb_h.bb_numrecs);
705 * Compute first and last byte offsets for the fields given.
706 * Interprets the offsets table, which contains struct field offsets.
710 __int64_t fields, /* bitmask of fields */
711 const short *offsets, /* table of field offsets */
712 int nbits, /* number of bits to inspect */
713 int *first, /* output: first byte offset */
714 int *last) /* output: last byte offset */
716 int i; /* current bit number */
717 __int64_t imask; /* mask for current bit number */
721 * Find the lowest bit, so the first byte offset.
723 for (i = 0, imask = 1LL; ; i++, imask <<= 1) {
724 if (imask & fields) {
730 * Find the highest bit, so the last byte offset.
732 for (i = nbits - 1, imask = 1LL << i; ; i--, imask >>= 1) {
733 if (imask & fields) {
734 *last = offsets[i + 1] - 1;
741 * Get a buffer for the block, return it read in.
742 * Long-form addressing.
746 xfs_mount_t *mp, /* file system mount point */
747 xfs_trans_t *tp, /* transaction pointer */
748 xfs_fsblock_t fsbno, /* file system block number */
749 uint lock, /* lock flags for read_buf */
750 xfs_buf_t **bpp, /* buffer for fsbno */
751 int refval) /* ref count value for buffer */
753 xfs_buf_t *bp; /* return value */
754 xfs_daddr_t d; /* real disk block address */
757 ASSERT(fsbno != NULLFSBLOCK);
758 d = XFS_FSB_TO_DADDR(mp, fsbno);
759 if ((error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, d,
760 mp->m_bsize, lock, &bp))) {
763 ASSERT(!bp || !XFS_BUF_GETERROR(bp));
765 XFS_BUF_SET_VTYPE_REF(bp, B_FS_MAP, refval);
772 * Get a buffer for the block, return it read in.
773 * Short-form addressing.
777 xfs_mount_t *mp, /* file system mount point */
778 xfs_trans_t *tp, /* transaction pointer */
779 xfs_agnumber_t agno, /* allocation group number */
780 xfs_agblock_t agbno, /* allocation group block number */
781 uint lock, /* lock flags for read_buf */
782 xfs_buf_t **bpp, /* buffer for agno/agbno */
783 int refval) /* ref count value for buffer */
785 xfs_buf_t *bp; /* return value */
786 xfs_daddr_t d; /* real disk block address */
789 ASSERT(agno != NULLAGNUMBER);
790 ASSERT(agbno != NULLAGBLOCK);
791 d = XFS_AGB_TO_DADDR(mp, agno, agbno);
792 if ((error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, d,
793 mp->m_bsize, lock, &bp))) {
796 ASSERT(!bp || !XFS_BUF_GETERROR(bp));
799 case XFS_ALLOC_BTREE_REF:
800 XFS_BUF_SET_VTYPE_REF(bp, B_FS_MAP, refval);
802 case XFS_INO_BTREE_REF:
803 XFS_BUF_SET_VTYPE_REF(bp, B_FS_INOMAP, refval);
812 * Read-ahead the block, don't wait for it, don't return a buffer.
813 * Long-form addressing.
817 xfs_btree_reada_bufl(
818 xfs_mount_t *mp, /* file system mount point */
819 xfs_fsblock_t fsbno, /* file system block number */
820 xfs_extlen_t count) /* count of filesystem blocks */
824 ASSERT(fsbno != NULLFSBLOCK);
825 d = XFS_FSB_TO_DADDR(mp, fsbno);
826 xfs_baread(mp->m_ddev_targp, d, mp->m_bsize * count);
830 * Read-ahead the block, don't wait for it, don't return a buffer.
831 * Short-form addressing.
835 xfs_btree_reada_bufs(
836 xfs_mount_t *mp, /* file system mount point */
837 xfs_agnumber_t agno, /* allocation group number */
838 xfs_agblock_t agbno, /* allocation group block number */
839 xfs_extlen_t count) /* count of filesystem blocks */
843 ASSERT(agno != NULLAGNUMBER);
844 ASSERT(agbno != NULLAGBLOCK);
845 d = XFS_AGB_TO_DADDR(mp, agno, agbno);
846 xfs_baread(mp->m_ddev_targp, d, mp->m_bsize * count);
850 * Read-ahead btree blocks, at the given level.
851 * Bits in lr are set from XFS_BTCUR_{LEFT,RIGHT}RA.
854 xfs_btree_readahead_core(
855 xfs_btree_cur_t *cur, /* btree cursor */
856 int lev, /* level in btree */
857 int lr) /* left/right bits */
859 xfs_alloc_block_t *a;
861 xfs_inobt_block_t *i;
864 ASSERT(cur->bc_bufs[lev] != NULL);
865 cur->bc_ra[lev] |= lr;
866 switch (cur->bc_btnum) {
869 a = XFS_BUF_TO_ALLOC_BLOCK(cur->bc_bufs[lev]);
870 if ((lr & XFS_BTCUR_LEFTRA) && be32_to_cpu(a->bb_leftsib) != NULLAGBLOCK) {
871 xfs_btree_reada_bufs(cur->bc_mp, cur->bc_private.a.agno,
872 be32_to_cpu(a->bb_leftsib), 1);
875 if ((lr & XFS_BTCUR_RIGHTRA) && be32_to_cpu(a->bb_rightsib) != NULLAGBLOCK) {
876 xfs_btree_reada_bufs(cur->bc_mp, cur->bc_private.a.agno,
877 be32_to_cpu(a->bb_rightsib), 1);
882 b = XFS_BUF_TO_BMBT_BLOCK(cur->bc_bufs[lev]);
883 if ((lr & XFS_BTCUR_LEFTRA) && be64_to_cpu(b->bb_leftsib) != NULLDFSBNO) {
884 xfs_btree_reada_bufl(cur->bc_mp, be64_to_cpu(b->bb_leftsib), 1);
887 if ((lr & XFS_BTCUR_RIGHTRA) && be64_to_cpu(b->bb_rightsib) != NULLDFSBNO) {
888 xfs_btree_reada_bufl(cur->bc_mp, be64_to_cpu(b->bb_rightsib), 1);
893 i = XFS_BUF_TO_INOBT_BLOCK(cur->bc_bufs[lev]);
894 if ((lr & XFS_BTCUR_LEFTRA) && be32_to_cpu(i->bb_leftsib) != NULLAGBLOCK) {
895 xfs_btree_reada_bufs(cur->bc_mp, cur->bc_private.i.agno,
896 be32_to_cpu(i->bb_leftsib), 1);
899 if ((lr & XFS_BTCUR_RIGHTRA) && be32_to_cpu(i->bb_rightsib) != NULLAGBLOCK) {
900 xfs_btree_reada_bufs(cur->bc_mp, cur->bc_private.i.agno,
901 be32_to_cpu(i->bb_rightsib), 1);
912 * Set the buffer for level "lev" in the cursor to bp, releasing
913 * any previous buffer.
917 xfs_btree_cur_t *cur, /* btree cursor */
918 int lev, /* level in btree */
919 xfs_buf_t *bp) /* new buffer to set */
921 xfs_btree_block_t *b; /* btree block */
922 xfs_buf_t *obp; /* old buffer pointer */
924 obp = cur->bc_bufs[lev];
926 xfs_trans_brelse(cur->bc_tp, obp);
927 cur->bc_bufs[lev] = bp;
931 b = XFS_BUF_TO_BLOCK(bp);
932 if (XFS_BTREE_LONG_PTRS(cur->bc_btnum)) {
933 if (be64_to_cpu(b->bb_u.l.bb_leftsib) == NULLDFSBNO)
934 cur->bc_ra[lev] |= XFS_BTCUR_LEFTRA;
935 if (be64_to_cpu(b->bb_u.l.bb_rightsib) == NULLDFSBNO)
936 cur->bc_ra[lev] |= XFS_BTCUR_RIGHTRA;
938 if (be32_to_cpu(b->bb_u.s.bb_leftsib) == NULLAGBLOCK)
939 cur->bc_ra[lev] |= XFS_BTCUR_LEFTRA;
940 if (be32_to_cpu(b->bb_u.s.bb_rightsib) == NULLAGBLOCK)
941 cur->bc_ra[lev] |= XFS_BTCUR_RIGHTRA;