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"
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_error.h"
42 * Cursor allocation zone.
44 kmem_zone_t *xfs_btree_cur_zone;
47 * Btree magic numbers.
49 const __uint32_t xfs_magics[XFS_BTNUM_MAX] =
51 XFS_ABTB_MAGIC, XFS_ABTC_MAGIC, XFS_BMAP_MAGIC, XFS_IBT_MAGIC
55 * Prototypes for internal routines.
59 * Checking routine: return maxrecs for the block.
61 STATIC int /* number of records fitting in block */
63 xfs_btree_cur_t *cur, /* btree cursor */
64 xfs_btree_block_t *block);/* generic btree block pointer */
71 * Retrieve the block pointer from the cursor at the given level.
72 * This may be a bmap btree root or from a buffer.
74 STATIC xfs_btree_block_t * /* generic btree block pointer */
76 xfs_btree_cur_t *cur, /* btree cursor */
77 int level, /* level in btree */
78 struct xfs_buf **bpp); /* buffer containing the block */
81 * Checking routine: return maxrecs for the block.
83 STATIC int /* number of records fitting in block */
85 xfs_btree_cur_t *cur, /* btree cursor */
86 xfs_btree_block_t *block) /* generic btree block pointer */
88 switch (cur->bc_btnum) {
91 return (int)XFS_ALLOC_BLOCK_MAXRECS(
92 be16_to_cpu(block->bb_h.bb_level), cur);
94 return (int)XFS_BMAP_BLOCK_IMAXRECS(
95 be16_to_cpu(block->bb_h.bb_level), cur);
97 return (int)XFS_INOBT_BLOCK_MAXRECS(
98 be16_to_cpu(block->bb_h.bb_level), cur);
111 * Debug routine: check that block header is ok.
114 xfs_btree_check_block(
115 xfs_btree_cur_t *cur, /* btree cursor */
116 xfs_btree_block_t *block, /* generic btree block pointer */
117 int level, /* level of the btree block */
118 xfs_buf_t *bp) /* buffer containing block, if any */
120 if (XFS_BTREE_LONG_PTRS(cur->bc_btnum))
121 xfs_btree_check_lblock(cur, (xfs_btree_lblock_t *)block, level,
124 xfs_btree_check_sblock(cur, (xfs_btree_sblock_t *)block, level,
129 * Debug routine: check that keys are in the right order.
133 xfs_btnum_t btnum, /* btree identifier */
134 void *ak1, /* pointer to left (lower) key */
135 void *ak2) /* pointer to right (higher) key */
138 case XFS_BTNUM_BNO: {
144 ASSERT(be32_to_cpu(k1->ar_startblock) < be32_to_cpu(k2->ar_startblock));
147 case XFS_BTNUM_CNT: {
153 ASSERT(be32_to_cpu(k1->ar_blockcount) < be32_to_cpu(k2->ar_blockcount) ||
154 (k1->ar_blockcount == k2->ar_blockcount &&
155 be32_to_cpu(k1->ar_startblock) < be32_to_cpu(k2->ar_startblock)));
158 case XFS_BTNUM_BMAP: {
164 ASSERT(INT_GET(k1->br_startoff, ARCH_CONVERT) < INT_GET(k2->br_startoff, ARCH_CONVERT));
167 case XFS_BTNUM_INO: {
173 ASSERT(INT_GET(k1->ir_startino, ARCH_CONVERT) < INT_GET(k2->ir_startino, ARCH_CONVERT));
183 * Checking routine: check that long form block header is ok.
186 int /* error (0 or EFSCORRUPTED) */
187 xfs_btree_check_lblock(
188 xfs_btree_cur_t *cur, /* btree cursor */
189 xfs_btree_lblock_t *block, /* btree long form block pointer */
190 int level, /* level of the btree block */
191 xfs_buf_t *bp) /* buffer for block, if any */
193 int lblock_ok; /* block passes checks */
194 xfs_mount_t *mp; /* file system mount point */
198 be32_to_cpu(block->bb_magic) == xfs_magics[cur->bc_btnum] &&
199 be16_to_cpu(block->bb_level) == level &&
200 be16_to_cpu(block->bb_numrecs) <=
201 xfs_btree_maxrecs(cur, (xfs_btree_block_t *)block) &&
203 (be64_to_cpu(block->bb_leftsib) == NULLDFSBNO ||
204 XFS_FSB_SANITY_CHECK(mp, be64_to_cpu(block->bb_leftsib))) &&
205 block->bb_rightsib &&
206 (be64_to_cpu(block->bb_rightsib) == NULLDFSBNO ||
207 XFS_FSB_SANITY_CHECK(mp, be64_to_cpu(block->bb_rightsib)));
208 if (unlikely(XFS_TEST_ERROR(!lblock_ok, mp, XFS_ERRTAG_BTREE_CHECK_LBLOCK,
209 XFS_RANDOM_BTREE_CHECK_LBLOCK))) {
211 xfs_buftrace("LBTREE ERROR", bp);
212 XFS_ERROR_REPORT("xfs_btree_check_lblock", XFS_ERRLEVEL_LOW,
214 return XFS_ERROR(EFSCORRUPTED);
220 * Checking routine: check that (long) pointer is ok.
222 int /* error (0 or EFSCORRUPTED) */
223 xfs_btree_check_lptr(
224 xfs_btree_cur_t *cur, /* btree cursor */
225 xfs_dfsbno_t ptr, /* btree block disk address */
226 int level) /* btree block level */
228 xfs_mount_t *mp; /* file system mount point */
231 XFS_WANT_CORRUPTED_RETURN(
234 XFS_FSB_SANITY_CHECK(mp, ptr));
240 * Debug routine: check that records are in the right order.
244 xfs_btnum_t btnum, /* btree identifier */
245 void *ar1, /* pointer to left (lower) record */
246 void *ar2) /* pointer to right (higher) record */
249 case XFS_BTNUM_BNO: {
255 ASSERT(be32_to_cpu(r1->ar_startblock) +
256 be32_to_cpu(r1->ar_blockcount) <=
257 be32_to_cpu(r2->ar_startblock));
260 case XFS_BTNUM_CNT: {
266 ASSERT(be32_to_cpu(r1->ar_blockcount) < be32_to_cpu(r2->ar_blockcount) ||
267 (r1->ar_blockcount == r2->ar_blockcount &&
268 be32_to_cpu(r1->ar_startblock) < be32_to_cpu(r2->ar_startblock)));
271 case XFS_BTNUM_BMAP: {
277 ASSERT(xfs_bmbt_disk_get_startoff(r1) +
278 xfs_bmbt_disk_get_blockcount(r1) <=
279 xfs_bmbt_disk_get_startoff(r2));
282 case XFS_BTNUM_INO: {
288 ASSERT(INT_GET(r1->ir_startino, ARCH_CONVERT) + XFS_INODES_PER_CHUNK <=
289 INT_GET(r2->ir_startino, ARCH_CONVERT));
299 * Checking routine: check that block header is ok.
302 int /* error (0 or EFSCORRUPTED) */
303 xfs_btree_check_sblock(
304 xfs_btree_cur_t *cur, /* btree cursor */
305 xfs_btree_sblock_t *block, /* btree short form block pointer */
306 int level, /* level of the btree block */
307 xfs_buf_t *bp) /* buffer containing block */
309 xfs_buf_t *agbp; /* buffer for ag. freespace struct */
310 xfs_agf_t *agf; /* ag. freespace structure */
311 xfs_agblock_t agflen; /* native ag. freespace length */
312 int sblock_ok; /* block passes checks */
314 agbp = cur->bc_private.a.agbp;
315 agf = XFS_BUF_TO_AGF(agbp);
316 agflen = be32_to_cpu(agf->agf_length);
318 be32_to_cpu(block->bb_magic) == xfs_magics[cur->bc_btnum] &&
319 be16_to_cpu(block->bb_level) == level &&
320 be16_to_cpu(block->bb_numrecs) <=
321 xfs_btree_maxrecs(cur, (xfs_btree_block_t *)block) &&
322 (be32_to_cpu(block->bb_leftsib) == NULLAGBLOCK ||
323 be32_to_cpu(block->bb_leftsib) < agflen) &&
325 (be32_to_cpu(block->bb_rightsib) == NULLAGBLOCK ||
326 be32_to_cpu(block->bb_rightsib) < agflen) &&
328 if (unlikely(XFS_TEST_ERROR(!sblock_ok, cur->bc_mp,
329 XFS_ERRTAG_BTREE_CHECK_SBLOCK,
330 XFS_RANDOM_BTREE_CHECK_SBLOCK))) {
332 xfs_buftrace("SBTREE ERROR", bp);
333 XFS_ERROR_REPORT("xfs_btree_check_sblock", XFS_ERRLEVEL_LOW,
335 return XFS_ERROR(EFSCORRUPTED);
341 * Checking routine: check that (short) pointer is ok.
343 int /* error (0 or EFSCORRUPTED) */
344 xfs_btree_check_sptr(
345 xfs_btree_cur_t *cur, /* btree cursor */
346 xfs_agblock_t ptr, /* btree block disk address */
347 int level) /* btree block level */
349 xfs_buf_t *agbp; /* buffer for ag. freespace struct */
350 xfs_agf_t *agf; /* ag. freespace structure */
352 agbp = cur->bc_private.a.agbp;
353 agf = XFS_BUF_TO_AGF(agbp);
354 XFS_WANT_CORRUPTED_RETURN(
356 ptr != NULLAGBLOCK && ptr != 0 &&
357 ptr < be32_to_cpu(agf->agf_length));
362 * Delete the btree cursor.
365 xfs_btree_del_cursor(
366 xfs_btree_cur_t *cur, /* btree cursor */
367 int error) /* del because of error */
369 int i; /* btree level */
372 * Clear the buffer pointers, and release the buffers.
373 * If we're doing this in the face of an error, we
374 * need to make sure to inspect all of the entries
375 * in the bc_bufs array for buffers to be unlocked.
376 * This is because some of the btree code works from
377 * level n down to 0, and if we get an error along
378 * the way we won't have initialized all the entries
381 for (i = 0; i < cur->bc_nlevels; i++) {
383 xfs_btree_setbuf(cur, i, NULL);
388 * Can't free a bmap cursor without having dealt with the
389 * allocated indirect blocks' accounting.
391 ASSERT(cur->bc_btnum != XFS_BTNUM_BMAP ||
392 cur->bc_private.b.allocated == 0);
396 kmem_zone_free(xfs_btree_cur_zone, cur);
400 * Duplicate the btree cursor.
401 * Allocate a new one, copy the record, re-get the buffers.
404 xfs_btree_dup_cursor(
405 xfs_btree_cur_t *cur, /* input cursor */
406 xfs_btree_cur_t **ncur) /* output cursor */
408 xfs_buf_t *bp; /* btree block's buffer pointer */
409 int error; /* error return value */
410 int i; /* level number of btree block */
411 xfs_mount_t *mp; /* mount structure for filesystem */
412 xfs_btree_cur_t *new; /* new cursor value */
413 xfs_trans_t *tp; /* transaction pointer, can be NULL */
418 * Allocate a new cursor like the old one.
420 new = xfs_btree_init_cursor(mp, tp, cur->bc_private.a.agbp,
421 cur->bc_private.a.agno, cur->bc_btnum, cur->bc_private.b.ip,
422 cur->bc_private.b.whichfork);
424 * Copy the record currently in the cursor.
426 new->bc_rec = cur->bc_rec;
428 * For each level current, re-get the buffer and copy the ptr value.
430 for (i = 0; i < new->bc_nlevels; i++) {
431 new->bc_ptrs[i] = cur->bc_ptrs[i];
432 new->bc_ra[i] = cur->bc_ra[i];
433 if ((bp = cur->bc_bufs[i])) {
434 if ((error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp,
435 XFS_BUF_ADDR(bp), mp->m_bsize, 0, &bp))) {
436 xfs_btree_del_cursor(new, error);
440 new->bc_bufs[i] = bp;
442 ASSERT(!XFS_BUF_GETERROR(bp));
444 new->bc_bufs[i] = NULL;
447 * For bmap btrees, copy the firstblock, flist, and flags values,
448 * since init cursor doesn't get them.
450 if (new->bc_btnum == XFS_BTNUM_BMAP) {
451 new->bc_private.b.firstblock = cur->bc_private.b.firstblock;
452 new->bc_private.b.flist = cur->bc_private.b.flist;
453 new->bc_private.b.flags = cur->bc_private.b.flags;
460 * Change the cursor to point to the first record at the given level.
461 * Other levels are unaffected.
463 int /* success=1, failure=0 */
465 xfs_btree_cur_t *cur, /* btree cursor */
466 int level) /* level to change */
468 xfs_btree_block_t *block; /* generic btree block pointer */
469 xfs_buf_t *bp; /* buffer containing block */
472 * Get the block pointer for this level.
474 block = xfs_btree_get_block(cur, level, &bp);
475 xfs_btree_check_block(cur, block, level, bp);
477 * It's empty, there is no such record.
479 if (!block->bb_h.bb_numrecs)
482 * Set the ptr value to 1, that's the first record/key.
484 cur->bc_ptrs[level] = 1;
489 * Retrieve the block pointer from the cursor at the given level.
490 * This may be a bmap btree root or from a buffer.
492 STATIC xfs_btree_block_t * /* generic btree block pointer */
494 xfs_btree_cur_t *cur, /* btree cursor */
495 int level, /* level in btree */
496 xfs_buf_t **bpp) /* buffer containing the block */
498 xfs_btree_block_t *block; /* return value */
499 xfs_buf_t *bp; /* return buffer */
500 xfs_ifork_t *ifp; /* inode fork pointer */
501 int whichfork; /* data or attr fork */
503 if (cur->bc_btnum == XFS_BTNUM_BMAP && level == cur->bc_nlevels - 1) {
504 whichfork = cur->bc_private.b.whichfork;
505 ifp = XFS_IFORK_PTR(cur->bc_private.b.ip, whichfork);
506 block = (xfs_btree_block_t *)ifp->if_broot;
509 bp = cur->bc_bufs[level];
510 block = XFS_BUF_TO_BLOCK(bp);
512 ASSERT(block != NULL);
518 * Get a buffer for the block, return it with no data read.
519 * Long-form addressing.
521 xfs_buf_t * /* buffer for fsbno */
523 xfs_mount_t *mp, /* file system mount point */
524 xfs_trans_t *tp, /* transaction pointer */
525 xfs_fsblock_t fsbno, /* file system block number */
526 uint lock) /* lock flags for get_buf */
528 xfs_buf_t *bp; /* buffer pointer (return value) */
529 xfs_daddr_t d; /* real disk block address */
531 ASSERT(fsbno != NULLFSBLOCK);
532 d = XFS_FSB_TO_DADDR(mp, fsbno);
533 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d, mp->m_bsize, lock);
535 ASSERT(!XFS_BUF_GETERROR(bp));
540 * Get a buffer for the block, return it with no data read.
541 * Short-form addressing.
543 xfs_buf_t * /* buffer for agno/agbno */
545 xfs_mount_t *mp, /* file system mount point */
546 xfs_trans_t *tp, /* transaction pointer */
547 xfs_agnumber_t agno, /* allocation group number */
548 xfs_agblock_t agbno, /* allocation group block number */
549 uint lock) /* lock flags for get_buf */
551 xfs_buf_t *bp; /* buffer pointer (return value) */
552 xfs_daddr_t d; /* real disk block address */
554 ASSERT(agno != NULLAGNUMBER);
555 ASSERT(agbno != NULLAGBLOCK);
556 d = XFS_AGB_TO_DADDR(mp, agno, agbno);
557 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d, mp->m_bsize, lock);
559 ASSERT(!XFS_BUF_GETERROR(bp));
564 * Allocate a new btree cursor.
565 * The cursor is either for allocation (A) or bmap (B) or inodes (I).
567 xfs_btree_cur_t * /* new btree cursor */
568 xfs_btree_init_cursor(
569 xfs_mount_t *mp, /* file system mount point */
570 xfs_trans_t *tp, /* transaction pointer */
571 xfs_buf_t *agbp, /* (A only) buffer for agf structure */
572 /* (I only) buffer for agi structure */
573 xfs_agnumber_t agno, /* (AI only) allocation group number */
574 xfs_btnum_t btnum, /* btree identifier */
575 xfs_inode_t *ip, /* (B only) inode owning the btree */
576 int whichfork) /* (B only) data or attr fork */
578 xfs_agf_t *agf; /* (A) allocation group freespace */
579 xfs_agi_t *agi; /* (I) allocation group inodespace */
580 xfs_btree_cur_t *cur; /* return value */
581 xfs_ifork_t *ifp; /* (I) inode fork pointer */
582 int nlevels=0; /* number of levels in the btree */
584 ASSERT(xfs_btree_cur_zone != NULL);
586 * Allocate a new cursor.
588 cur = kmem_zone_zalloc(xfs_btree_cur_zone, KM_SLEEP);
590 * Deduce the number of btree levels from the arguments.
595 agf = XFS_BUF_TO_AGF(agbp);
596 nlevels = be32_to_cpu(agf->agf_levels[btnum]);
599 ifp = XFS_IFORK_PTR(ip, whichfork);
600 nlevels = be16_to_cpu(ifp->if_broot->bb_level) + 1;
603 agi = XFS_BUF_TO_AGI(agbp);
604 nlevels = be32_to_cpu(agi->agi_level);
610 * Fill in the common fields.
614 cur->bc_nlevels = nlevels;
615 cur->bc_btnum = btnum;
616 cur->bc_blocklog = mp->m_sb.sb_blocklog;
618 * Fill in private fields.
624 * Allocation btree fields.
626 cur->bc_private.a.agbp = agbp;
627 cur->bc_private.a.agno = agno;
633 cur->bc_private.b.forksize = XFS_IFORK_SIZE(ip, whichfork);
634 cur->bc_private.b.ip = ip;
635 cur->bc_private.b.firstblock = NULLFSBLOCK;
636 cur->bc_private.b.flist = NULL;
637 cur->bc_private.b.allocated = 0;
638 cur->bc_private.b.flags = 0;
639 cur->bc_private.b.whichfork = whichfork;
643 * Inode allocation btree fields.
645 cur->bc_private.i.agbp = agbp;
646 cur->bc_private.i.agno = agno;
655 * Check for the cursor referring to the last block at the given level.
657 int /* 1=is last block, 0=not last block */
658 xfs_btree_islastblock(
659 xfs_btree_cur_t *cur, /* btree cursor */
660 int level) /* level to check */
662 xfs_btree_block_t *block; /* generic btree block pointer */
663 xfs_buf_t *bp; /* buffer containing block */
665 block = xfs_btree_get_block(cur, level, &bp);
666 xfs_btree_check_block(cur, block, level, bp);
667 if (XFS_BTREE_LONG_PTRS(cur->bc_btnum))
668 return be64_to_cpu(block->bb_u.l.bb_rightsib) == NULLDFSBNO;
670 return be32_to_cpu(block->bb_u.s.bb_rightsib) == NULLAGBLOCK;
674 * Change the cursor to point to the last record in the current block
675 * at the given level. Other levels are unaffected.
677 int /* success=1, failure=0 */
679 xfs_btree_cur_t *cur, /* btree cursor */
680 int level) /* level to change */
682 xfs_btree_block_t *block; /* generic btree block pointer */
683 xfs_buf_t *bp; /* buffer containing block */
686 * Get the block pointer for this level.
688 block = xfs_btree_get_block(cur, level, &bp);
689 xfs_btree_check_block(cur, block, level, bp);
691 * It's empty, there is no such record.
693 if (!block->bb_h.bb_numrecs)
696 * Set the ptr value to numrecs, that's the last record/key.
698 cur->bc_ptrs[level] = be16_to_cpu(block->bb_h.bb_numrecs);
703 * Compute first and last byte offsets for the fields given.
704 * Interprets the offsets table, which contains struct field offsets.
708 __int64_t fields, /* bitmask of fields */
709 const short *offsets, /* table of field offsets */
710 int nbits, /* number of bits to inspect */
711 int *first, /* output: first byte offset */
712 int *last) /* output: last byte offset */
714 int i; /* current bit number */
715 __int64_t imask; /* mask for current bit number */
719 * Find the lowest bit, so the first byte offset.
721 for (i = 0, imask = 1LL; ; i++, imask <<= 1) {
722 if (imask & fields) {
728 * Find the highest bit, so the last byte offset.
730 for (i = nbits - 1, imask = 1LL << i; ; i--, imask >>= 1) {
731 if (imask & fields) {
732 *last = offsets[i + 1] - 1;
739 * Get a buffer for the block, return it read in.
740 * Long-form addressing.
744 xfs_mount_t *mp, /* file system mount point */
745 xfs_trans_t *tp, /* transaction pointer */
746 xfs_fsblock_t fsbno, /* file system block number */
747 uint lock, /* lock flags for read_buf */
748 xfs_buf_t **bpp, /* buffer for fsbno */
749 int refval) /* ref count value for buffer */
751 xfs_buf_t *bp; /* return value */
752 xfs_daddr_t d; /* real disk block address */
755 ASSERT(fsbno != NULLFSBLOCK);
756 d = XFS_FSB_TO_DADDR(mp, fsbno);
757 if ((error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, d,
758 mp->m_bsize, lock, &bp))) {
761 ASSERT(!bp || !XFS_BUF_GETERROR(bp));
763 XFS_BUF_SET_VTYPE_REF(bp, B_FS_MAP, refval);
770 * Get a buffer for the block, return it read in.
771 * Short-form addressing.
775 xfs_mount_t *mp, /* file system mount point */
776 xfs_trans_t *tp, /* transaction pointer */
777 xfs_agnumber_t agno, /* allocation group number */
778 xfs_agblock_t agbno, /* allocation group block number */
779 uint lock, /* lock flags for read_buf */
780 xfs_buf_t **bpp, /* buffer for agno/agbno */
781 int refval) /* ref count value for buffer */
783 xfs_buf_t *bp; /* return value */
784 xfs_daddr_t d; /* real disk block address */
787 ASSERT(agno != NULLAGNUMBER);
788 ASSERT(agbno != NULLAGBLOCK);
789 d = XFS_AGB_TO_DADDR(mp, agno, agbno);
790 if ((error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, d,
791 mp->m_bsize, lock, &bp))) {
794 ASSERT(!bp || !XFS_BUF_GETERROR(bp));
797 case XFS_ALLOC_BTREE_REF:
798 XFS_BUF_SET_VTYPE_REF(bp, B_FS_MAP, refval);
800 case XFS_INO_BTREE_REF:
801 XFS_BUF_SET_VTYPE_REF(bp, B_FS_INOMAP, refval);
810 * Read-ahead the block, don't wait for it, don't return a buffer.
811 * Long-form addressing.
815 xfs_btree_reada_bufl(
816 xfs_mount_t *mp, /* file system mount point */
817 xfs_fsblock_t fsbno, /* file system block number */
818 xfs_extlen_t count) /* count of filesystem blocks */
822 ASSERT(fsbno != NULLFSBLOCK);
823 d = XFS_FSB_TO_DADDR(mp, fsbno);
824 xfs_baread(mp->m_ddev_targp, d, mp->m_bsize * count);
828 * Read-ahead the block, don't wait for it, don't return a buffer.
829 * Short-form addressing.
833 xfs_btree_reada_bufs(
834 xfs_mount_t *mp, /* file system mount point */
835 xfs_agnumber_t agno, /* allocation group number */
836 xfs_agblock_t agbno, /* allocation group block number */
837 xfs_extlen_t count) /* count of filesystem blocks */
841 ASSERT(agno != NULLAGNUMBER);
842 ASSERT(agbno != NULLAGBLOCK);
843 d = XFS_AGB_TO_DADDR(mp, agno, agbno);
844 xfs_baread(mp->m_ddev_targp, d, mp->m_bsize * count);
848 * Read-ahead btree blocks, at the given level.
849 * Bits in lr are set from XFS_BTCUR_{LEFT,RIGHT}RA.
852 xfs_btree_readahead_core(
853 xfs_btree_cur_t *cur, /* btree cursor */
854 int lev, /* level in btree */
855 int lr) /* left/right bits */
857 xfs_alloc_block_t *a;
859 xfs_inobt_block_t *i;
862 ASSERT(cur->bc_bufs[lev] != NULL);
863 cur->bc_ra[lev] |= lr;
864 switch (cur->bc_btnum) {
867 a = XFS_BUF_TO_ALLOC_BLOCK(cur->bc_bufs[lev]);
868 if ((lr & XFS_BTCUR_LEFTRA) && be32_to_cpu(a->bb_leftsib) != NULLAGBLOCK) {
869 xfs_btree_reada_bufs(cur->bc_mp, cur->bc_private.a.agno,
870 be32_to_cpu(a->bb_leftsib), 1);
873 if ((lr & XFS_BTCUR_RIGHTRA) && be32_to_cpu(a->bb_rightsib) != NULLAGBLOCK) {
874 xfs_btree_reada_bufs(cur->bc_mp, cur->bc_private.a.agno,
875 be32_to_cpu(a->bb_rightsib), 1);
880 b = XFS_BUF_TO_BMBT_BLOCK(cur->bc_bufs[lev]);
881 if ((lr & XFS_BTCUR_LEFTRA) && be64_to_cpu(b->bb_leftsib) != NULLDFSBNO) {
882 xfs_btree_reada_bufl(cur->bc_mp, be64_to_cpu(b->bb_leftsib), 1);
885 if ((lr & XFS_BTCUR_RIGHTRA) && be64_to_cpu(b->bb_rightsib) != NULLDFSBNO) {
886 xfs_btree_reada_bufl(cur->bc_mp, be64_to_cpu(b->bb_rightsib), 1);
891 i = XFS_BUF_TO_INOBT_BLOCK(cur->bc_bufs[lev]);
892 if ((lr & XFS_BTCUR_LEFTRA) && be32_to_cpu(i->bb_leftsib) != NULLAGBLOCK) {
893 xfs_btree_reada_bufs(cur->bc_mp, cur->bc_private.i.agno,
894 be32_to_cpu(i->bb_leftsib), 1);
897 if ((lr & XFS_BTCUR_RIGHTRA) && be32_to_cpu(i->bb_rightsib) != NULLAGBLOCK) {
898 xfs_btree_reada_bufs(cur->bc_mp, cur->bc_private.i.agno,
899 be32_to_cpu(i->bb_rightsib), 1);
910 * Set the buffer for level "lev" in the cursor to bp, releasing
911 * any previous buffer.
915 xfs_btree_cur_t *cur, /* btree cursor */
916 int lev, /* level in btree */
917 xfs_buf_t *bp) /* new buffer to set */
919 xfs_btree_block_t *b; /* btree block */
920 xfs_buf_t *obp; /* old buffer pointer */
922 obp = cur->bc_bufs[lev];
924 xfs_trans_brelse(cur->bc_tp, obp);
925 cur->bc_bufs[lev] = bp;
929 b = XFS_BUF_TO_BLOCK(bp);
930 if (XFS_BTREE_LONG_PTRS(cur->bc_btnum)) {
931 if (be64_to_cpu(b->bb_u.l.bb_leftsib) == NULLDFSBNO)
932 cur->bc_ra[lev] |= XFS_BTCUR_LEFTRA;
933 if (be64_to_cpu(b->bb_u.l.bb_rightsib) == NULLDFSBNO)
934 cur->bc_ra[lev] |= XFS_BTCUR_RIGHTRA;
936 if (be32_to_cpu(b->bb_u.s.bb_leftsib) == NULLAGBLOCK)
937 cur->bc_ra[lev] |= XFS_BTCUR_LEFTRA;
938 if (be32_to_cpu(b->bb_u.s.bb_rightsib) == NULLAGBLOCK)
939 cur->bc_ra[lev] |= XFS_BTCUR_RIGHTRA;