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] = {
50 XFS_ABTB_MAGIC, XFS_ABTC_MAGIC, XFS_BMAP_MAGIC, XFS_IBT_MAGIC
59 * Debug routine: check that keys are in the right order.
63 xfs_btnum_t btnum, /* btree identifier */
64 void *ak1, /* pointer to left (lower) key */
65 void *ak2) /* pointer to right (higher) key */
74 ASSERT(be32_to_cpu(k1->ar_startblock) < be32_to_cpu(k2->ar_startblock));
83 ASSERT(be32_to_cpu(k1->ar_blockcount) < be32_to_cpu(k2->ar_blockcount) ||
84 (k1->ar_blockcount == k2->ar_blockcount &&
85 be32_to_cpu(k1->ar_startblock) < be32_to_cpu(k2->ar_startblock)));
88 case XFS_BTNUM_BMAP: {
94 ASSERT(be64_to_cpu(k1->br_startoff) < be64_to_cpu(k2->br_startoff));
103 ASSERT(be32_to_cpu(k1->ir_startino) < be32_to_cpu(k2->ir_startino));
112 * Debug routine: check that records are in the right order.
116 xfs_btnum_t btnum, /* btree identifier */
117 void *ar1, /* pointer to left (lower) record */
118 void *ar2) /* pointer to right (higher) record */
121 case XFS_BTNUM_BNO: {
127 ASSERT(be32_to_cpu(r1->ar_startblock) +
128 be32_to_cpu(r1->ar_blockcount) <=
129 be32_to_cpu(r2->ar_startblock));
132 case XFS_BTNUM_CNT: {
138 ASSERT(be32_to_cpu(r1->ar_blockcount) < be32_to_cpu(r2->ar_blockcount) ||
139 (r1->ar_blockcount == r2->ar_blockcount &&
140 be32_to_cpu(r1->ar_startblock) < be32_to_cpu(r2->ar_startblock)));
143 case XFS_BTNUM_BMAP: {
149 ASSERT(xfs_bmbt_disk_get_startoff(r1) +
150 xfs_bmbt_disk_get_blockcount(r1) <=
151 xfs_bmbt_disk_get_startoff(r2));
154 case XFS_BTNUM_INO: {
160 ASSERT(be32_to_cpu(r1->ir_startino) + XFS_INODES_PER_CHUNK <=
161 be32_to_cpu(r2->ir_startino));
170 int /* error (0 or EFSCORRUPTED) */
171 xfs_btree_check_lblock(
172 struct xfs_btree_cur *cur, /* btree cursor */
173 struct xfs_btree_lblock *block, /* btree long form block pointer */
174 int level, /* level of the btree block */
175 struct xfs_buf *bp) /* buffer for block, if any */
177 int lblock_ok; /* block passes checks */
178 struct xfs_mount *mp; /* file system mount point */
182 be32_to_cpu(block->bb_magic) == xfs_magics[cur->bc_btnum] &&
183 be16_to_cpu(block->bb_level) == level &&
184 be16_to_cpu(block->bb_numrecs) <=
185 cur->bc_ops->get_maxrecs(cur, level) &&
187 (be64_to_cpu(block->bb_leftsib) == NULLDFSBNO ||
188 XFS_FSB_SANITY_CHECK(mp, be64_to_cpu(block->bb_leftsib))) &&
189 block->bb_rightsib &&
190 (be64_to_cpu(block->bb_rightsib) == NULLDFSBNO ||
191 XFS_FSB_SANITY_CHECK(mp, be64_to_cpu(block->bb_rightsib)));
192 if (unlikely(XFS_TEST_ERROR(!lblock_ok, mp,
193 XFS_ERRTAG_BTREE_CHECK_LBLOCK,
194 XFS_RANDOM_BTREE_CHECK_LBLOCK))) {
196 xfs_buftrace("LBTREE ERROR", bp);
197 XFS_ERROR_REPORT("xfs_btree_check_lblock", XFS_ERRLEVEL_LOW,
199 return XFS_ERROR(EFSCORRUPTED);
204 int /* error (0 or EFSCORRUPTED) */
205 xfs_btree_check_sblock(
206 struct xfs_btree_cur *cur, /* btree cursor */
207 struct xfs_btree_sblock *block, /* btree short form block pointer */
208 int level, /* level of the btree block */
209 struct xfs_buf *bp) /* buffer containing block */
211 struct xfs_buf *agbp; /* buffer for ag. freespace struct */
212 struct xfs_agf *agf; /* ag. freespace structure */
213 xfs_agblock_t agflen; /* native ag. freespace length */
214 int sblock_ok; /* block passes checks */
216 agbp = cur->bc_private.a.agbp;
217 agf = XFS_BUF_TO_AGF(agbp);
218 agflen = be32_to_cpu(agf->agf_length);
220 be32_to_cpu(block->bb_magic) == xfs_magics[cur->bc_btnum] &&
221 be16_to_cpu(block->bb_level) == level &&
222 be16_to_cpu(block->bb_numrecs) <=
223 cur->bc_ops->get_maxrecs(cur, level) &&
224 (be32_to_cpu(block->bb_leftsib) == NULLAGBLOCK ||
225 be32_to_cpu(block->bb_leftsib) < agflen) &&
227 (be32_to_cpu(block->bb_rightsib) == NULLAGBLOCK ||
228 be32_to_cpu(block->bb_rightsib) < agflen) &&
230 if (unlikely(XFS_TEST_ERROR(!sblock_ok, cur->bc_mp,
231 XFS_ERRTAG_BTREE_CHECK_SBLOCK,
232 XFS_RANDOM_BTREE_CHECK_SBLOCK))) {
234 xfs_buftrace("SBTREE ERROR", bp);
235 XFS_ERROR_REPORT("xfs_btree_check_sblock", XFS_ERRLEVEL_LOW,
237 return XFS_ERROR(EFSCORRUPTED);
243 * Debug routine: check that block header is ok.
246 xfs_btree_check_block(
247 struct xfs_btree_cur *cur, /* btree cursor */
248 struct xfs_btree_block *block, /* generic btree block pointer */
249 int level, /* level of the btree block */
250 struct xfs_buf *bp) /* buffer containing block, if any */
252 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
253 return xfs_btree_check_lblock(cur,
254 (struct xfs_btree_lblock *)block, level, bp);
256 return xfs_btree_check_sblock(cur,
257 (struct xfs_btree_sblock *)block, level, bp);
262 * Check that (long) pointer is ok.
264 int /* error (0 or EFSCORRUPTED) */
265 xfs_btree_check_lptr(
266 struct xfs_btree_cur *cur, /* btree cursor */
267 xfs_dfsbno_t bno, /* btree block disk address */
268 int level) /* btree block level */
270 XFS_WANT_CORRUPTED_RETURN(
273 XFS_FSB_SANITY_CHECK(cur->bc_mp, bno));
278 * Check that (short) pointer is ok.
280 int /* error (0 or EFSCORRUPTED) */
281 xfs_btree_check_sptr(
282 struct xfs_btree_cur *cur, /* btree cursor */
283 xfs_agblock_t bno, /* btree block disk address */
284 int level) /* btree block level */
286 xfs_agblock_t agblocks = cur->bc_mp->m_sb.sb_agblocks;
288 XFS_WANT_CORRUPTED_RETURN(
290 bno != NULLAGBLOCK &&
297 * Check that block ptr is ok.
299 int /* error (0 or EFSCORRUPTED) */
301 struct xfs_btree_cur *cur, /* btree cursor */
302 union xfs_btree_ptr *ptr, /* btree block disk address */
303 int index, /* offset from ptr to check */
304 int level) /* btree block level */
306 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
307 return xfs_btree_check_lptr(cur,
308 be64_to_cpu((&ptr->l)[index]), level);
310 return xfs_btree_check_sptr(cur,
311 be32_to_cpu((&ptr->s)[index]), level);
316 * Delete the btree cursor.
319 xfs_btree_del_cursor(
320 xfs_btree_cur_t *cur, /* btree cursor */
321 int error) /* del because of error */
323 int i; /* btree level */
326 * Clear the buffer pointers, and release the buffers.
327 * If we're doing this in the face of an error, we
328 * need to make sure to inspect all of the entries
329 * in the bc_bufs array for buffers to be unlocked.
330 * This is because some of the btree code works from
331 * level n down to 0, and if we get an error along
332 * the way we won't have initialized all the entries
335 for (i = 0; i < cur->bc_nlevels; i++) {
337 xfs_btree_setbuf(cur, i, NULL);
342 * Can't free a bmap cursor without having dealt with the
343 * allocated indirect blocks' accounting.
345 ASSERT(cur->bc_btnum != XFS_BTNUM_BMAP ||
346 cur->bc_private.b.allocated == 0);
350 kmem_zone_free(xfs_btree_cur_zone, cur);
354 * Duplicate the btree cursor.
355 * Allocate a new one, copy the record, re-get the buffers.
358 xfs_btree_dup_cursor(
359 xfs_btree_cur_t *cur, /* input cursor */
360 xfs_btree_cur_t **ncur) /* output cursor */
362 xfs_buf_t *bp; /* btree block's buffer pointer */
363 int error; /* error return value */
364 int i; /* level number of btree block */
365 xfs_mount_t *mp; /* mount structure for filesystem */
366 xfs_btree_cur_t *new; /* new cursor value */
367 xfs_trans_t *tp; /* transaction pointer, can be NULL */
373 * Allocate a new cursor like the old one.
375 new = cur->bc_ops->dup_cursor(cur);
378 * Copy the record currently in the cursor.
380 new->bc_rec = cur->bc_rec;
383 * For each level current, re-get the buffer and copy the ptr value.
385 for (i = 0; i < new->bc_nlevels; i++) {
386 new->bc_ptrs[i] = cur->bc_ptrs[i];
387 new->bc_ra[i] = cur->bc_ra[i];
388 if ((bp = cur->bc_bufs[i])) {
389 if ((error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp,
390 XFS_BUF_ADDR(bp), mp->m_bsize, 0, &bp))) {
391 xfs_btree_del_cursor(new, error);
395 new->bc_bufs[i] = bp;
397 ASSERT(!XFS_BUF_GETERROR(bp));
399 new->bc_bufs[i] = NULL;
406 * XFS btree block layout and addressing:
408 * There are two types of blocks in the btree: leaf and non-leaf blocks.
410 * The leaf record start with a header then followed by records containing
411 * the values. A non-leaf block also starts with the same header, and
412 * then first contains lookup keys followed by an equal number of pointers
413 * to the btree blocks at the previous level.
415 * +--------+-------+-------+-------+-------+-------+-------+
416 * Leaf: | header | rec 1 | rec 2 | rec 3 | rec 4 | rec 5 | rec N |
417 * +--------+-------+-------+-------+-------+-------+-------+
419 * +--------+-------+-------+-------+-------+-------+-------+
420 * Non-Leaf: | header | key 1 | key 2 | key N | ptr 1 | ptr 2 | ptr N |
421 * +--------+-------+-------+-------+-------+-------+-------+
423 * The header is called struct xfs_btree_block for reasons better left unknown
424 * and comes in different versions for short (32bit) and long (64bit) block
425 * pointers. The record and key structures are defined by the btree instances
426 * and opaque to the btree core. The block pointers are simple disk endian
427 * integers, available in a short (32bit) and long (64bit) variant.
429 * The helpers below calculate the offset of a given record, key or pointer
430 * into a btree block (xfs_btree_*_offset) or return a pointer to the given
431 * record, key or pointer (xfs_btree_*_addr). Note that all addressing
432 * inside the btree block is done using indices starting at one, not zero!
436 * Return size of the btree block header for this btree instance.
438 static inline size_t xfs_btree_block_len(struct xfs_btree_cur *cur)
440 return (cur->bc_flags & XFS_BTREE_LONG_PTRS) ?
441 sizeof(struct xfs_btree_lblock) :
442 sizeof(struct xfs_btree_sblock);
446 * Return size of btree block pointers for this btree instance.
448 static inline size_t xfs_btree_ptr_len(struct xfs_btree_cur *cur)
450 return (cur->bc_flags & XFS_BTREE_LONG_PTRS) ?
451 sizeof(__be64) : sizeof(__be32);
455 * Calculate offset of the n-th record in a btree block.
458 xfs_btree_rec_offset(
459 struct xfs_btree_cur *cur,
462 return xfs_btree_block_len(cur) +
463 (n - 1) * cur->bc_ops->rec_len;
467 * Calculate offset of the n-th key in a btree block.
470 xfs_btree_key_offset(
471 struct xfs_btree_cur *cur,
474 return xfs_btree_block_len(cur) +
475 (n - 1) * cur->bc_ops->key_len;
479 * Calculate offset of the n-th block pointer in a btree block.
482 xfs_btree_ptr_offset(
483 struct xfs_btree_cur *cur,
487 return xfs_btree_block_len(cur) +
488 cur->bc_ops->get_maxrecs(cur, level) * cur->bc_ops->key_len +
489 (n - 1) * xfs_btree_ptr_len(cur);
493 * Return a pointer to the n-th record in the btree block.
495 STATIC union xfs_btree_rec *
497 struct xfs_btree_cur *cur,
499 struct xfs_btree_block *block)
501 return (union xfs_btree_rec *)
502 ((char *)block + xfs_btree_rec_offset(cur, n));
506 * Return a pointer to the n-th key in the btree block.
508 STATIC union xfs_btree_key *
510 struct xfs_btree_cur *cur,
512 struct xfs_btree_block *block)
514 return (union xfs_btree_key *)
515 ((char *)block + xfs_btree_key_offset(cur, n));
519 * Return a pointer to the n-th block pointer in the btree block.
521 STATIC union xfs_btree_ptr *
523 struct xfs_btree_cur *cur,
525 struct xfs_btree_block *block)
527 int level = xfs_btree_get_level(block);
529 ASSERT(block->bb_level != 0);
531 return (union xfs_btree_ptr *)
532 ((char *)block + xfs_btree_ptr_offset(cur, n, level));
536 * Get a the root block which is stored in the inode.
538 * For now this btree implementation assumes the btree root is always
539 * stored in the if_broot field of an inode fork.
541 STATIC struct xfs_btree_block *
543 struct xfs_btree_cur *cur)
545 struct xfs_ifork *ifp;
547 ifp = XFS_IFORK_PTR(cur->bc_private.b.ip, cur->bc_private.b.whichfork);
548 return (struct xfs_btree_block *)ifp->if_broot;
552 * Retrieve the block pointer from the cursor at the given level.
553 * This may be an inode btree root or from a buffer.
555 STATIC struct xfs_btree_block * /* generic btree block pointer */
557 struct xfs_btree_cur *cur, /* btree cursor */
558 int level, /* level in btree */
559 struct xfs_buf **bpp) /* buffer containing the block */
561 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
562 (level == cur->bc_nlevels - 1)) {
564 return xfs_btree_get_iroot(cur);
567 *bpp = cur->bc_bufs[level];
568 return XFS_BUF_TO_BLOCK(*bpp);
572 * Get a buffer for the block, return it with no data read.
573 * Long-form addressing.
575 xfs_buf_t * /* buffer for fsbno */
577 xfs_mount_t *mp, /* file system mount point */
578 xfs_trans_t *tp, /* transaction pointer */
579 xfs_fsblock_t fsbno, /* file system block number */
580 uint lock) /* lock flags for get_buf */
582 xfs_buf_t *bp; /* buffer pointer (return value) */
583 xfs_daddr_t d; /* real disk block address */
585 ASSERT(fsbno != NULLFSBLOCK);
586 d = XFS_FSB_TO_DADDR(mp, fsbno);
587 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d, mp->m_bsize, lock);
589 ASSERT(!XFS_BUF_GETERROR(bp));
594 * Get a buffer for the block, return it with no data read.
595 * Short-form addressing.
597 xfs_buf_t * /* buffer for agno/agbno */
599 xfs_mount_t *mp, /* file system mount point */
600 xfs_trans_t *tp, /* transaction pointer */
601 xfs_agnumber_t agno, /* allocation group number */
602 xfs_agblock_t agbno, /* allocation group block number */
603 uint lock) /* lock flags for get_buf */
605 xfs_buf_t *bp; /* buffer pointer (return value) */
606 xfs_daddr_t d; /* real disk block address */
608 ASSERT(agno != NULLAGNUMBER);
609 ASSERT(agbno != NULLAGBLOCK);
610 d = XFS_AGB_TO_DADDR(mp, agno, agbno);
611 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d, mp->m_bsize, lock);
613 ASSERT(!XFS_BUF_GETERROR(bp));
618 * Check for the cursor referring to the last block at the given level.
620 int /* 1=is last block, 0=not last block */
621 xfs_btree_islastblock(
622 xfs_btree_cur_t *cur, /* btree cursor */
623 int level) /* level to check */
625 xfs_btree_block_t *block; /* generic btree block pointer */
626 xfs_buf_t *bp; /* buffer containing block */
628 block = xfs_btree_get_block(cur, level, &bp);
629 xfs_btree_check_block(cur, block, level, bp);
630 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
631 return be64_to_cpu(block->bb_u.l.bb_rightsib) == NULLDFSBNO;
633 return be32_to_cpu(block->bb_u.s.bb_rightsib) == NULLAGBLOCK;
637 * Change the cursor to point to the first record at the given level.
638 * Other levels are unaffected.
640 int /* success=1, failure=0 */
642 xfs_btree_cur_t *cur, /* btree cursor */
643 int level) /* level to change */
645 xfs_btree_block_t *block; /* generic btree block pointer */
646 xfs_buf_t *bp; /* buffer containing block */
649 * Get the block pointer for this level.
651 block = xfs_btree_get_block(cur, level, &bp);
652 xfs_btree_check_block(cur, block, level, bp);
654 * It's empty, there is no such record.
656 if (!block->bb_numrecs)
659 * Set the ptr value to 1, that's the first record/key.
661 cur->bc_ptrs[level] = 1;
666 * Change the cursor to point to the last record in the current block
667 * at the given level. Other levels are unaffected.
669 int /* success=1, failure=0 */
671 xfs_btree_cur_t *cur, /* btree cursor */
672 int level) /* level to change */
674 xfs_btree_block_t *block; /* generic btree block pointer */
675 xfs_buf_t *bp; /* buffer containing block */
678 * Get the block pointer for this level.
680 block = xfs_btree_get_block(cur, level, &bp);
681 xfs_btree_check_block(cur, block, level, bp);
683 * It's empty, there is no such record.
685 if (!block->bb_numrecs)
688 * Set the ptr value to numrecs, that's the last record/key.
690 cur->bc_ptrs[level] = be16_to_cpu(block->bb_numrecs);
695 * Compute first and last byte offsets for the fields given.
696 * Interprets the offsets table, which contains struct field offsets.
700 __int64_t fields, /* bitmask of fields */
701 const short *offsets, /* table of field offsets */
702 int nbits, /* number of bits to inspect */
703 int *first, /* output: first byte offset */
704 int *last) /* output: last byte offset */
706 int i; /* current bit number */
707 __int64_t imask; /* mask for current bit number */
711 * Find the lowest bit, so the first byte offset.
713 for (i = 0, imask = 1LL; ; i++, imask <<= 1) {
714 if (imask & fields) {
720 * Find the highest bit, so the last byte offset.
722 for (i = nbits - 1, imask = 1LL << i; ; i--, imask >>= 1) {
723 if (imask & fields) {
724 *last = offsets[i + 1] - 1;
731 * Get a buffer for the block, return it read in.
732 * Long-form addressing.
736 xfs_mount_t *mp, /* file system mount point */
737 xfs_trans_t *tp, /* transaction pointer */
738 xfs_fsblock_t fsbno, /* file system block number */
739 uint lock, /* lock flags for read_buf */
740 xfs_buf_t **bpp, /* buffer for fsbno */
741 int refval) /* ref count value for buffer */
743 xfs_buf_t *bp; /* return value */
744 xfs_daddr_t d; /* real disk block address */
747 ASSERT(fsbno != NULLFSBLOCK);
748 d = XFS_FSB_TO_DADDR(mp, fsbno);
749 if ((error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, d,
750 mp->m_bsize, lock, &bp))) {
753 ASSERT(!bp || !XFS_BUF_GETERROR(bp));
755 XFS_BUF_SET_VTYPE_REF(bp, B_FS_MAP, refval);
762 * Get a buffer for the block, return it read in.
763 * Short-form addressing.
767 xfs_mount_t *mp, /* file system mount point */
768 xfs_trans_t *tp, /* transaction pointer */
769 xfs_agnumber_t agno, /* allocation group number */
770 xfs_agblock_t agbno, /* allocation group block number */
771 uint lock, /* lock flags for read_buf */
772 xfs_buf_t **bpp, /* buffer for agno/agbno */
773 int refval) /* ref count value for buffer */
775 xfs_buf_t *bp; /* return value */
776 xfs_daddr_t d; /* real disk block address */
779 ASSERT(agno != NULLAGNUMBER);
780 ASSERT(agbno != NULLAGBLOCK);
781 d = XFS_AGB_TO_DADDR(mp, agno, agbno);
782 if ((error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, d,
783 mp->m_bsize, lock, &bp))) {
786 ASSERT(!bp || !XFS_BUF_GETERROR(bp));
789 case XFS_ALLOC_BTREE_REF:
790 XFS_BUF_SET_VTYPE_REF(bp, B_FS_MAP, refval);
792 case XFS_INO_BTREE_REF:
793 XFS_BUF_SET_VTYPE_REF(bp, B_FS_INOMAP, refval);
802 * Read-ahead the block, don't wait for it, don't return a buffer.
803 * Long-form addressing.
807 xfs_btree_reada_bufl(
808 xfs_mount_t *mp, /* file system mount point */
809 xfs_fsblock_t fsbno, /* file system block number */
810 xfs_extlen_t count) /* count of filesystem blocks */
814 ASSERT(fsbno != NULLFSBLOCK);
815 d = XFS_FSB_TO_DADDR(mp, fsbno);
816 xfs_baread(mp->m_ddev_targp, d, mp->m_bsize * count);
820 * Read-ahead the block, don't wait for it, don't return a buffer.
821 * Short-form addressing.
825 xfs_btree_reada_bufs(
826 xfs_mount_t *mp, /* file system mount point */
827 xfs_agnumber_t agno, /* allocation group number */
828 xfs_agblock_t agbno, /* allocation group block number */
829 xfs_extlen_t count) /* count of filesystem blocks */
833 ASSERT(agno != NULLAGNUMBER);
834 ASSERT(agbno != NULLAGBLOCK);
835 d = XFS_AGB_TO_DADDR(mp, agno, agbno);
836 xfs_baread(mp->m_ddev_targp, d, mp->m_bsize * count);
840 xfs_btree_readahead_lblock(
841 struct xfs_btree_cur *cur,
843 struct xfs_btree_block *block)
846 xfs_fsblock_t left = be64_to_cpu(block->bb_u.l.bb_leftsib);
847 xfs_fsblock_t right = be64_to_cpu(block->bb_u.l.bb_rightsib);
849 if ((lr & XFS_BTCUR_LEFTRA) && left != NULLDFSBNO) {
850 xfs_btree_reada_bufl(cur->bc_mp, left, 1);
854 if ((lr & XFS_BTCUR_RIGHTRA) && right != NULLDFSBNO) {
855 xfs_btree_reada_bufl(cur->bc_mp, right, 1);
863 xfs_btree_readahead_sblock(
864 struct xfs_btree_cur *cur,
866 struct xfs_btree_block *block)
869 xfs_agblock_t left = be32_to_cpu(block->bb_u.s.bb_leftsib);
870 xfs_agblock_t right = be32_to_cpu(block->bb_u.s.bb_rightsib);
873 if ((lr & XFS_BTCUR_LEFTRA) && left != NULLAGBLOCK) {
874 xfs_btree_reada_bufs(cur->bc_mp, cur->bc_private.a.agno,
879 if ((lr & XFS_BTCUR_RIGHTRA) && right != NULLAGBLOCK) {
880 xfs_btree_reada_bufs(cur->bc_mp, cur->bc_private.a.agno,
889 * Read-ahead btree blocks, at the given level.
890 * Bits in lr are set from XFS_BTCUR_{LEFT,RIGHT}RA.
894 struct xfs_btree_cur *cur, /* btree cursor */
895 int lev, /* level in btree */
896 int lr) /* left/right bits */
898 struct xfs_btree_block *block;
901 * No readahead needed if we are at the root level and the
902 * btree root is stored in the inode.
904 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
905 (lev == cur->bc_nlevels - 1))
908 if ((cur->bc_ra[lev] | lr) == cur->bc_ra[lev])
911 cur->bc_ra[lev] |= lr;
912 block = XFS_BUF_TO_BLOCK(cur->bc_bufs[lev]);
914 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
915 return xfs_btree_readahead_lblock(cur, lr, block);
916 return xfs_btree_readahead_sblock(cur, lr, block);
920 * Set the buffer for level "lev" in the cursor to bp, releasing
921 * any previous buffer.
925 xfs_btree_cur_t *cur, /* btree cursor */
926 int lev, /* level in btree */
927 xfs_buf_t *bp) /* new buffer to set */
929 xfs_btree_block_t *b; /* btree block */
930 xfs_buf_t *obp; /* old buffer pointer */
932 obp = cur->bc_bufs[lev];
934 xfs_trans_brelse(cur->bc_tp, obp);
935 cur->bc_bufs[lev] = bp;
939 b = XFS_BUF_TO_BLOCK(bp);
940 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
941 if (be64_to_cpu(b->bb_u.l.bb_leftsib) == NULLDFSBNO)
942 cur->bc_ra[lev] |= XFS_BTCUR_LEFTRA;
943 if (be64_to_cpu(b->bb_u.l.bb_rightsib) == NULLDFSBNO)
944 cur->bc_ra[lev] |= XFS_BTCUR_RIGHTRA;
946 if (be32_to_cpu(b->bb_u.s.bb_leftsib) == NULLAGBLOCK)
947 cur->bc_ra[lev] |= XFS_BTCUR_LEFTRA;
948 if (be32_to_cpu(b->bb_u.s.bb_rightsib) == NULLAGBLOCK)
949 cur->bc_ra[lev] |= XFS_BTCUR_RIGHTRA;