2 * Copyright (c) 2000-2002 Silicon Graphics, Inc. All Rights Reserved.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it would be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 * Further, this software is distributed without any warranty that it is
13 * free of the rightful claim of any third person regarding infringement
14 * or the like. Any license provided herein, whether implied or
15 * otherwise, applies only to this software file. Patent licenses, if
16 * any, provided herein do not apply to combinations of this program with
17 * other software, or any other product whatsoever.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write the Free Software Foundation, Inc., 59
21 * Temple Place - Suite 330, Boston MA 02111-1307, USA.
23 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA 94043, or:
28 * For further information regarding this notice, see:
30 * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
34 * This file contains common code for the space manager's btree implementations.
39 #include "xfs_macros.h"
40 #include "xfs_types.h"
43 #include "xfs_trans.h"
48 #include "xfs_dmapi.h"
49 #include "xfs_mount.h"
50 #include "xfs_alloc_btree.h"
51 #include "xfs_bmap_btree.h"
52 #include "xfs_ialloc_btree.h"
53 #include "xfs_btree.h"
54 #include "xfs_ialloc.h"
55 #include "xfs_attr_sf.h"
56 #include "xfs_dir_sf.h"
57 #include "xfs_dir2_sf.h"
58 #include "xfs_dinode.h"
59 #include "xfs_inode.h"
61 #include "xfs_error.h"
64 * Cursor allocation zone.
66 kmem_zone_t *xfs_btree_cur_zone;
69 * Btree magic numbers.
71 const __uint32_t xfs_magics[XFS_BTNUM_MAX] =
73 XFS_ABTB_MAGIC, XFS_ABTC_MAGIC, XFS_BMAP_MAGIC, XFS_IBT_MAGIC
77 * Prototypes for internal routines.
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 */
93 * Checking routine: return maxrecs for the block.
95 STATIC int /* number of records fitting in block */
97 xfs_btree_cur_t *cur, /* btree cursor */
98 xfs_btree_block_t *block) /* generic btree block pointer */
100 switch (cur->bc_btnum) {
103 return (int)XFS_ALLOC_BLOCK_MAXRECS(INT_GET(block->bb_h.bb_level, ARCH_CONVERT), cur);
105 return (int)XFS_BMAP_BLOCK_IMAXRECS(INT_GET(block->bb_h.bb_level, ARCH_CONVERT), cur);
107 return (int)XFS_INOBT_BLOCK_MAXRECS(INT_GET(block->bb_h.bb_level, ARCH_CONVERT), cur);
120 * Debug routine: check that block header is ok.
123 xfs_btree_check_block(
124 xfs_btree_cur_t *cur, /* btree cursor */
125 xfs_btree_block_t *block, /* generic btree block pointer */
126 int level, /* level of the btree block */
127 xfs_buf_t *bp) /* buffer containing block, if any */
129 if (XFS_BTREE_LONG_PTRS(cur->bc_btnum))
130 xfs_btree_check_lblock(cur, (xfs_btree_lblock_t *)block, level,
133 xfs_btree_check_sblock(cur, (xfs_btree_sblock_t *)block, level,
138 * Debug routine: check that keys are in the right order.
142 xfs_btnum_t btnum, /* btree identifier */
143 void *ak1, /* pointer to left (lower) key */
144 void *ak2) /* pointer to right (higher) key */
147 case XFS_BTNUM_BNO: {
153 ASSERT(INT_GET(k1->ar_startblock, ARCH_CONVERT) < INT_GET(k2->ar_startblock, ARCH_CONVERT));
156 case XFS_BTNUM_CNT: {
162 ASSERT(INT_GET(k1->ar_blockcount, ARCH_CONVERT) < INT_GET(k2->ar_blockcount, ARCH_CONVERT) ||
163 (INT_GET(k1->ar_blockcount, ARCH_CONVERT) == INT_GET(k2->ar_blockcount, ARCH_CONVERT) &&
164 INT_GET(k1->ar_startblock, ARCH_CONVERT) < INT_GET(k2->ar_startblock, ARCH_CONVERT)));
167 case XFS_BTNUM_BMAP: {
173 ASSERT(INT_GET(k1->br_startoff, ARCH_CONVERT) < INT_GET(k2->br_startoff, ARCH_CONVERT));
176 case XFS_BTNUM_INO: {
182 ASSERT(INT_GET(k1->ir_startino, ARCH_CONVERT) < INT_GET(k2->ir_startino, ARCH_CONVERT));
192 * Checking routine: check that long form block header is ok.
195 int /* error (0 or EFSCORRUPTED) */
196 xfs_btree_check_lblock(
197 xfs_btree_cur_t *cur, /* btree cursor */
198 xfs_btree_lblock_t *block, /* btree long form block pointer */
199 int level, /* level of the btree block */
200 xfs_buf_t *bp) /* buffer for block, if any */
202 int lblock_ok; /* block passes checks */
203 xfs_mount_t *mp; /* file system mount point */
207 INT_GET(block->bb_magic, ARCH_CONVERT) == xfs_magics[cur->bc_btnum] &&
208 INT_GET(block->bb_level, ARCH_CONVERT) == level &&
209 INT_GET(block->bb_numrecs, ARCH_CONVERT) <=
210 xfs_btree_maxrecs(cur, (xfs_btree_block_t *)block) &&
212 (INT_GET(block->bb_leftsib, ARCH_CONVERT) == NULLDFSBNO ||
213 XFS_FSB_SANITY_CHECK(mp, INT_GET(block->bb_leftsib, ARCH_CONVERT))) &&
214 block->bb_rightsib &&
215 (INT_GET(block->bb_rightsib, ARCH_CONVERT) == NULLDFSBNO ||
216 XFS_FSB_SANITY_CHECK(mp, INT_GET(block->bb_rightsib, ARCH_CONVERT)));
217 if (unlikely(XFS_TEST_ERROR(!lblock_ok, mp, XFS_ERRTAG_BTREE_CHECK_LBLOCK,
218 XFS_RANDOM_BTREE_CHECK_LBLOCK))) {
220 xfs_buftrace("LBTREE ERROR", bp);
221 XFS_ERROR_REPORT("xfs_btree_check_lblock", XFS_ERRLEVEL_LOW,
223 return XFS_ERROR(EFSCORRUPTED);
229 * Checking routine: check that (long) pointer is ok.
231 int /* error (0 or EFSCORRUPTED) */
232 xfs_btree_check_lptr(
233 xfs_btree_cur_t *cur, /* btree cursor */
234 xfs_dfsbno_t ptr, /* btree block disk address */
235 int level) /* btree block level */
237 xfs_mount_t *mp; /* file system mount point */
240 XFS_WANT_CORRUPTED_RETURN(
243 XFS_FSB_SANITY_CHECK(mp, ptr));
249 * Debug routine: check that records are in the right order.
253 xfs_btnum_t btnum, /* btree identifier */
254 void *ar1, /* pointer to left (lower) record */
255 void *ar2) /* pointer to right (higher) record */
258 case XFS_BTNUM_BNO: {
264 ASSERT(INT_GET(r1->ar_startblock, ARCH_CONVERT) + INT_GET(r1->ar_blockcount, ARCH_CONVERT) <=
265 INT_GET(r2->ar_startblock, ARCH_CONVERT));
268 case XFS_BTNUM_CNT: {
274 ASSERT(INT_GET(r1->ar_blockcount, ARCH_CONVERT) < INT_GET(r2->ar_blockcount, ARCH_CONVERT) ||
275 (INT_GET(r1->ar_blockcount, ARCH_CONVERT) == INT_GET(r2->ar_blockcount, ARCH_CONVERT) &&
276 INT_GET(r1->ar_startblock, ARCH_CONVERT) < INT_GET(r2->ar_startblock, ARCH_CONVERT)));
279 case XFS_BTNUM_BMAP: {
285 ASSERT(xfs_bmbt_disk_get_startoff(r1) +
286 xfs_bmbt_disk_get_blockcount(r1) <=
287 xfs_bmbt_disk_get_startoff(r2));
290 case XFS_BTNUM_INO: {
296 ASSERT(INT_GET(r1->ir_startino, ARCH_CONVERT) + XFS_INODES_PER_CHUNK <=
297 INT_GET(r2->ir_startino, ARCH_CONVERT));
307 * Checking routine: check that block header is ok.
310 int /* error (0 or EFSCORRUPTED) */
311 xfs_btree_check_sblock(
312 xfs_btree_cur_t *cur, /* btree cursor */
313 xfs_btree_sblock_t *block, /* btree short form block pointer */
314 int level, /* level of the btree block */
315 xfs_buf_t *bp) /* buffer containing block */
317 xfs_buf_t *agbp; /* buffer for ag. freespace struct */
318 xfs_agf_t *agf; /* ag. freespace structure */
319 xfs_agblock_t agflen; /* native ag. freespace length */
320 int sblock_ok; /* block passes checks */
322 agbp = cur->bc_private.a.agbp;
323 agf = XFS_BUF_TO_AGF(agbp);
324 agflen = INT_GET(agf->agf_length, ARCH_CONVERT);
326 INT_GET(block->bb_magic, ARCH_CONVERT) == xfs_magics[cur->bc_btnum] &&
327 INT_GET(block->bb_level, ARCH_CONVERT) == level &&
328 INT_GET(block->bb_numrecs, ARCH_CONVERT) <=
329 xfs_btree_maxrecs(cur, (xfs_btree_block_t *)block) &&
330 (INT_GET(block->bb_leftsib, ARCH_CONVERT) == NULLAGBLOCK ||
331 INT_GET(block->bb_leftsib, ARCH_CONVERT) < agflen) &&
333 (INT_GET(block->bb_rightsib, ARCH_CONVERT) == NULLAGBLOCK ||
334 INT_GET(block->bb_rightsib, ARCH_CONVERT) < agflen) &&
336 if (unlikely(XFS_TEST_ERROR(!sblock_ok, cur->bc_mp,
337 XFS_ERRTAG_BTREE_CHECK_SBLOCK,
338 XFS_RANDOM_BTREE_CHECK_SBLOCK))) {
340 xfs_buftrace("SBTREE ERROR", bp);
341 XFS_ERROR_REPORT("xfs_btree_check_sblock", XFS_ERRLEVEL_LOW,
343 return XFS_ERROR(EFSCORRUPTED);
349 * Checking routine: check that (short) pointer is ok.
351 int /* error (0 or EFSCORRUPTED) */
352 xfs_btree_check_sptr(
353 xfs_btree_cur_t *cur, /* btree cursor */
354 xfs_agblock_t ptr, /* btree block disk address */
355 int level) /* btree block level */
357 xfs_buf_t *agbp; /* buffer for ag. freespace struct */
358 xfs_agf_t *agf; /* ag. freespace structure */
360 agbp = cur->bc_private.a.agbp;
361 agf = XFS_BUF_TO_AGF(agbp);
362 XFS_WANT_CORRUPTED_RETURN(
364 ptr != NULLAGBLOCK && ptr != 0 &&
365 ptr < INT_GET(agf->agf_length, ARCH_CONVERT));
370 * Delete the btree cursor.
373 xfs_btree_del_cursor(
374 xfs_btree_cur_t *cur, /* btree cursor */
375 int error) /* del because of error */
377 int i; /* btree level */
380 * Clear the buffer pointers, and release the buffers.
381 * If we're doing this in the face of an error, we
382 * need to make sure to inspect all of the entries
383 * in the bc_bufs array for buffers to be unlocked.
384 * This is because some of the btree code works from
385 * level n down to 0, and if we get an error along
386 * the way we won't have initialized all the entries
389 for (i = 0; i < cur->bc_nlevels; i++) {
391 xfs_btree_setbuf(cur, i, NULL);
396 * Can't free a bmap cursor without having dealt with the
397 * allocated indirect blocks' accounting.
399 ASSERT(cur->bc_btnum != XFS_BTNUM_BMAP ||
400 cur->bc_private.b.allocated == 0);
404 kmem_zone_free(xfs_btree_cur_zone, cur);
408 * Duplicate the btree cursor.
409 * Allocate a new one, copy the record, re-get the buffers.
412 xfs_btree_dup_cursor(
413 xfs_btree_cur_t *cur, /* input cursor */
414 xfs_btree_cur_t **ncur) /* output cursor */
416 xfs_buf_t *bp; /* btree block's buffer pointer */
417 int error; /* error return value */
418 int i; /* level number of btree block */
419 xfs_mount_t *mp; /* mount structure for filesystem */
420 xfs_btree_cur_t *new; /* new cursor value */
421 xfs_trans_t *tp; /* transaction pointer, can be NULL */
426 * Allocate a new cursor like the old one.
428 new = xfs_btree_init_cursor(mp, tp, cur->bc_private.a.agbp,
429 cur->bc_private.a.agno, cur->bc_btnum, cur->bc_private.b.ip,
430 cur->bc_private.b.whichfork);
432 * Copy the record currently in the cursor.
434 new->bc_rec = cur->bc_rec;
436 * For each level current, re-get the buffer and copy the ptr value.
438 for (i = 0; i < new->bc_nlevels; i++) {
439 new->bc_ptrs[i] = cur->bc_ptrs[i];
440 new->bc_ra[i] = cur->bc_ra[i];
441 if ((bp = cur->bc_bufs[i])) {
442 if ((error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp,
443 XFS_BUF_ADDR(bp), mp->m_bsize, 0, &bp))) {
444 xfs_btree_del_cursor(new, error);
448 new->bc_bufs[i] = bp;
450 ASSERT(!XFS_BUF_GETERROR(bp));
452 new->bc_bufs[i] = NULL;
455 * For bmap btrees, copy the firstblock, flist, and flags values,
456 * since init cursor doesn't get them.
458 if (new->bc_btnum == XFS_BTNUM_BMAP) {
459 new->bc_private.b.firstblock = cur->bc_private.b.firstblock;
460 new->bc_private.b.flist = cur->bc_private.b.flist;
461 new->bc_private.b.flags = cur->bc_private.b.flags;
468 * Change the cursor to point to the first record at the given level.
469 * Other levels are unaffected.
471 int /* success=1, failure=0 */
473 xfs_btree_cur_t *cur, /* btree cursor */
474 int level) /* level to change */
476 xfs_btree_block_t *block; /* generic btree block pointer */
477 xfs_buf_t *bp; /* buffer containing block */
480 * Get the block pointer for this level.
482 block = xfs_btree_get_block(cur, level, &bp);
483 xfs_btree_check_block(cur, block, level, bp);
485 * It's empty, there is no such record.
487 if (!block->bb_h.bb_numrecs)
490 * Set the ptr value to 1, that's the first record/key.
492 cur->bc_ptrs[level] = 1;
497 * Retrieve the block pointer from the cursor at the given level.
498 * This may be a bmap btree root or from a buffer.
500 xfs_btree_block_t * /* generic btree block pointer */
502 xfs_btree_cur_t *cur, /* btree cursor */
503 int level, /* level in btree */
504 xfs_buf_t **bpp) /* buffer containing the block */
506 xfs_btree_block_t *block; /* return value */
507 xfs_buf_t *bp; /* return buffer */
508 xfs_ifork_t *ifp; /* inode fork pointer */
509 int whichfork; /* data or attr fork */
511 if (cur->bc_btnum == XFS_BTNUM_BMAP && level == cur->bc_nlevels - 1) {
512 whichfork = cur->bc_private.b.whichfork;
513 ifp = XFS_IFORK_PTR(cur->bc_private.b.ip, whichfork);
514 block = (xfs_btree_block_t *)ifp->if_broot;
517 bp = cur->bc_bufs[level];
518 block = XFS_BUF_TO_BLOCK(bp);
520 ASSERT(block != NULL);
526 * Get a buffer for the block, return it with no data read.
527 * Long-form addressing.
529 xfs_buf_t * /* buffer for fsbno */
531 xfs_mount_t *mp, /* file system mount point */
532 xfs_trans_t *tp, /* transaction pointer */
533 xfs_fsblock_t fsbno, /* file system block number */
534 uint lock) /* lock flags for get_buf */
536 xfs_buf_t *bp; /* buffer pointer (return value) */
537 xfs_daddr_t d; /* real disk block address */
539 ASSERT(fsbno != NULLFSBLOCK);
540 d = XFS_FSB_TO_DADDR(mp, fsbno);
541 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d, mp->m_bsize, lock);
543 ASSERT(!XFS_BUF_GETERROR(bp));
548 * Get a buffer for the block, return it with no data read.
549 * Short-form addressing.
551 xfs_buf_t * /* buffer for agno/agbno */
553 xfs_mount_t *mp, /* file system mount point */
554 xfs_trans_t *tp, /* transaction pointer */
555 xfs_agnumber_t agno, /* allocation group number */
556 xfs_agblock_t agbno, /* allocation group block number */
557 uint lock) /* lock flags for get_buf */
559 xfs_buf_t *bp; /* buffer pointer (return value) */
560 xfs_daddr_t d; /* real disk block address */
562 ASSERT(agno != NULLAGNUMBER);
563 ASSERT(agbno != NULLAGBLOCK);
564 d = XFS_AGB_TO_DADDR(mp, agno, agbno);
565 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d, mp->m_bsize, lock);
567 ASSERT(!XFS_BUF_GETERROR(bp));
572 * Allocate a new btree cursor.
573 * The cursor is either for allocation (A) or bmap (B) or inodes (I).
575 xfs_btree_cur_t * /* new btree cursor */
576 xfs_btree_init_cursor(
577 xfs_mount_t *mp, /* file system mount point */
578 xfs_trans_t *tp, /* transaction pointer */
579 xfs_buf_t *agbp, /* (A only) buffer for agf structure */
580 /* (I only) buffer for agi structure */
581 xfs_agnumber_t agno, /* (AI only) allocation group number */
582 xfs_btnum_t btnum, /* btree identifier */
583 xfs_inode_t *ip, /* (B only) inode owning the btree */
584 int whichfork) /* (B only) data or attr fork */
586 xfs_agf_t *agf; /* (A) allocation group freespace */
587 xfs_agi_t *agi; /* (I) allocation group inodespace */
588 xfs_btree_cur_t *cur; /* return value */
589 xfs_ifork_t *ifp; /* (I) inode fork pointer */
590 int nlevels=0; /* number of levels in the btree */
592 ASSERT(xfs_btree_cur_zone != NULL);
594 * Allocate a new cursor.
596 cur = kmem_zone_zalloc(xfs_btree_cur_zone, KM_SLEEP);
598 * Deduce the number of btree levels from the arguments.
603 agf = XFS_BUF_TO_AGF(agbp);
604 nlevels = INT_GET(agf->agf_levels[btnum], ARCH_CONVERT);
607 ifp = XFS_IFORK_PTR(ip, whichfork);
608 nlevels = INT_GET(ifp->if_broot->bb_level, ARCH_CONVERT) + 1;
611 agi = XFS_BUF_TO_AGI(agbp);
612 nlevels = INT_GET(agi->agi_level, ARCH_CONVERT);
618 * Fill in the common fields.
622 cur->bc_nlevels = nlevels;
623 cur->bc_btnum = btnum;
624 cur->bc_blocklog = mp->m_sb.sb_blocklog;
626 * Fill in private fields.
632 * Allocation btree fields.
634 cur->bc_private.a.agbp = agbp;
635 cur->bc_private.a.agno = agno;
641 cur->bc_private.b.forksize = XFS_IFORK_SIZE(ip, whichfork);
642 cur->bc_private.b.ip = ip;
643 cur->bc_private.b.firstblock = NULLFSBLOCK;
644 cur->bc_private.b.flist = NULL;
645 cur->bc_private.b.allocated = 0;
646 cur->bc_private.b.flags = 0;
647 cur->bc_private.b.whichfork = whichfork;
651 * Inode allocation btree fields.
653 cur->bc_private.i.agbp = agbp;
654 cur->bc_private.i.agno = agno;
663 * Check for the cursor referring to the last block at the given level.
665 int /* 1=is last block, 0=not last block */
666 xfs_btree_islastblock(
667 xfs_btree_cur_t *cur, /* btree cursor */
668 int level) /* level to check */
670 xfs_btree_block_t *block; /* generic btree block pointer */
671 xfs_buf_t *bp; /* buffer containing block */
673 block = xfs_btree_get_block(cur, level, &bp);
674 xfs_btree_check_block(cur, block, level, bp);
675 if (XFS_BTREE_LONG_PTRS(cur->bc_btnum))
676 return INT_GET(block->bb_u.l.bb_rightsib, ARCH_CONVERT) == NULLDFSBNO;
678 return INT_GET(block->bb_u.s.bb_rightsib, ARCH_CONVERT) == NULLAGBLOCK;
682 * Change the cursor to point to the last record in the current block
683 * at the given level. Other levels are unaffected.
685 int /* success=1, failure=0 */
687 xfs_btree_cur_t *cur, /* btree cursor */
688 int level) /* level to change */
690 xfs_btree_block_t *block; /* generic btree block pointer */
691 xfs_buf_t *bp; /* buffer containing block */
694 * Get the block pointer for this level.
696 block = xfs_btree_get_block(cur, level, &bp);
697 xfs_btree_check_block(cur, block, level, bp);
699 * It's empty, there is no such record.
701 if (!block->bb_h.bb_numrecs)
704 * Set the ptr value to numrecs, that's the last record/key.
706 cur->bc_ptrs[level] = INT_GET(block->bb_h.bb_numrecs, ARCH_CONVERT);
711 * Compute first and last byte offsets for the fields given.
712 * Interprets the offsets table, which contains struct field offsets.
716 __int64_t fields, /* bitmask of fields */
717 const short *offsets, /* table of field offsets */
718 int nbits, /* number of bits to inspect */
719 int *first, /* output: first byte offset */
720 int *last) /* output: last byte offset */
722 int i; /* current bit number */
723 __int64_t imask; /* mask for current bit number */
727 * Find the lowest bit, so the first byte offset.
729 for (i = 0, imask = 1LL; ; i++, imask <<= 1) {
730 if (imask & fields) {
736 * Find the highest bit, so the last byte offset.
738 for (i = nbits - 1, imask = 1LL << i; ; i--, imask >>= 1) {
739 if (imask & fields) {
740 *last = offsets[i + 1] - 1;
747 * Get a buffer for the block, return it read in.
748 * Long-form addressing.
752 xfs_mount_t *mp, /* file system mount point */
753 xfs_trans_t *tp, /* transaction pointer */
754 xfs_fsblock_t fsbno, /* file system block number */
755 uint lock, /* lock flags for read_buf */
756 xfs_buf_t **bpp, /* buffer for fsbno */
757 int refval) /* ref count value for buffer */
759 xfs_buf_t *bp; /* return value */
760 xfs_daddr_t d; /* real disk block address */
763 ASSERT(fsbno != NULLFSBLOCK);
764 d = XFS_FSB_TO_DADDR(mp, fsbno);
765 if ((error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, d,
766 mp->m_bsize, lock, &bp))) {
769 ASSERT(!bp || !XFS_BUF_GETERROR(bp));
771 XFS_BUF_SET_VTYPE_REF(bp, B_FS_MAP, refval);
778 * Get a buffer for the block, return it read in.
779 * Short-form addressing.
783 xfs_mount_t *mp, /* file system mount point */
784 xfs_trans_t *tp, /* transaction pointer */
785 xfs_agnumber_t agno, /* allocation group number */
786 xfs_agblock_t agbno, /* allocation group block number */
787 uint lock, /* lock flags for read_buf */
788 xfs_buf_t **bpp, /* buffer for agno/agbno */
789 int refval) /* ref count value for buffer */
791 xfs_buf_t *bp; /* return value */
792 xfs_daddr_t d; /* real disk block address */
795 ASSERT(agno != NULLAGNUMBER);
796 ASSERT(agbno != NULLAGBLOCK);
797 d = XFS_AGB_TO_DADDR(mp, agno, agbno);
798 if ((error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, d,
799 mp->m_bsize, lock, &bp))) {
802 ASSERT(!bp || !XFS_BUF_GETERROR(bp));
805 case XFS_ALLOC_BTREE_REF:
806 XFS_BUF_SET_VTYPE_REF(bp, B_FS_MAP, refval);
808 case XFS_INO_BTREE_REF:
809 XFS_BUF_SET_VTYPE_REF(bp, B_FS_INOMAP, refval);
818 * Read-ahead the block, don't wait for it, don't return a buffer.
819 * Long-form addressing.
823 xfs_btree_reada_bufl(
824 xfs_mount_t *mp, /* file system mount point */
825 xfs_fsblock_t fsbno, /* file system block number */
826 xfs_extlen_t count) /* count of filesystem blocks */
830 ASSERT(fsbno != NULLFSBLOCK);
831 d = XFS_FSB_TO_DADDR(mp, fsbno);
832 xfs_baread(mp->m_ddev_targp, d, mp->m_bsize * count);
836 * Read-ahead the block, don't wait for it, don't return a buffer.
837 * Short-form addressing.
841 xfs_btree_reada_bufs(
842 xfs_mount_t *mp, /* file system mount point */
843 xfs_agnumber_t agno, /* allocation group number */
844 xfs_agblock_t agbno, /* allocation group block number */
845 xfs_extlen_t count) /* count of filesystem blocks */
849 ASSERT(agno != NULLAGNUMBER);
850 ASSERT(agbno != NULLAGBLOCK);
851 d = XFS_AGB_TO_DADDR(mp, agno, agbno);
852 xfs_baread(mp->m_ddev_targp, d, mp->m_bsize * count);
856 * Read-ahead btree blocks, at the given level.
857 * Bits in lr are set from XFS_BTCUR_{LEFT,RIGHT}RA.
860 xfs_btree_readahead_core(
861 xfs_btree_cur_t *cur, /* btree cursor */
862 int lev, /* level in btree */
863 int lr) /* left/right bits */
865 xfs_alloc_block_t *a;
867 xfs_inobt_block_t *i;
870 ASSERT(cur->bc_bufs[lev] != NULL);
871 cur->bc_ra[lev] |= lr;
872 switch (cur->bc_btnum) {
875 a = XFS_BUF_TO_ALLOC_BLOCK(cur->bc_bufs[lev]);
876 if ((lr & XFS_BTCUR_LEFTRA) && INT_GET(a->bb_leftsib, ARCH_CONVERT) != NULLAGBLOCK) {
877 xfs_btree_reada_bufs(cur->bc_mp, cur->bc_private.a.agno,
878 INT_GET(a->bb_leftsib, ARCH_CONVERT), 1);
881 if ((lr & XFS_BTCUR_RIGHTRA) && INT_GET(a->bb_rightsib, ARCH_CONVERT) != NULLAGBLOCK) {
882 xfs_btree_reada_bufs(cur->bc_mp, cur->bc_private.a.agno,
883 INT_GET(a->bb_rightsib, ARCH_CONVERT), 1);
888 b = XFS_BUF_TO_BMBT_BLOCK(cur->bc_bufs[lev]);
889 if ((lr & XFS_BTCUR_LEFTRA) && INT_GET(b->bb_leftsib, ARCH_CONVERT) != NULLDFSBNO) {
890 xfs_btree_reada_bufl(cur->bc_mp, INT_GET(b->bb_leftsib, ARCH_CONVERT), 1);
893 if ((lr & XFS_BTCUR_RIGHTRA) && INT_GET(b->bb_rightsib, ARCH_CONVERT) != NULLDFSBNO) {
894 xfs_btree_reada_bufl(cur->bc_mp, INT_GET(b->bb_rightsib, ARCH_CONVERT), 1);
899 i = XFS_BUF_TO_INOBT_BLOCK(cur->bc_bufs[lev]);
900 if ((lr & XFS_BTCUR_LEFTRA) && INT_GET(i->bb_leftsib, ARCH_CONVERT) != NULLAGBLOCK) {
901 xfs_btree_reada_bufs(cur->bc_mp, cur->bc_private.i.agno,
902 INT_GET(i->bb_leftsib, ARCH_CONVERT), 1);
905 if ((lr & XFS_BTCUR_RIGHTRA) && INT_GET(i->bb_rightsib, ARCH_CONVERT) != NULLAGBLOCK) {
906 xfs_btree_reada_bufs(cur->bc_mp, cur->bc_private.i.agno,
907 INT_GET(i->bb_rightsib, ARCH_CONVERT), 1);
918 * Set the buffer for level "lev" in the cursor to bp, releasing
919 * any previous buffer.
923 xfs_btree_cur_t *cur, /* btree cursor */
924 int lev, /* level in btree */
925 xfs_buf_t *bp) /* new buffer to set */
927 xfs_btree_block_t *b; /* btree block */
928 xfs_buf_t *obp; /* old buffer pointer */
930 obp = cur->bc_bufs[lev];
932 xfs_trans_brelse(cur->bc_tp, obp);
933 cur->bc_bufs[lev] = bp;
937 b = XFS_BUF_TO_BLOCK(bp);
938 if (XFS_BTREE_LONG_PTRS(cur->bc_btnum)) {
939 if (INT_GET(b->bb_u.l.bb_leftsib, ARCH_CONVERT) == NULLDFSBNO)
940 cur->bc_ra[lev] |= XFS_BTCUR_LEFTRA;
941 if (INT_GET(b->bb_u.l.bb_rightsib, ARCH_CONVERT) == NULLDFSBNO)
942 cur->bc_ra[lev] |= XFS_BTCUR_RIGHTRA;
944 if (INT_GET(b->bb_u.s.bb_leftsib, ARCH_CONVERT) == NULLAGBLOCK)
945 cur->bc_ra[lev] |= XFS_BTCUR_LEFTRA;
946 if (INT_GET(b->bb_u.s.bb_rightsib, ARCH_CONVERT) == NULLAGBLOCK)
947 cur->bc_ra[lev] |= XFS_BTCUR_RIGHTRA;