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(INT_GET(block
->bb_h
.bb_level
, ARCH_CONVERT
), cur
);
95 return (int)XFS_BMAP_BLOCK_IMAXRECS(INT_GET(block
->bb_h
.bb_level
, ARCH_CONVERT
), cur
);
97 return (int)XFS_INOBT_BLOCK_MAXRECS(INT_GET(block
->bb_h
.bb_level
, ARCH_CONVERT
), cur
);
110 * Debug routine: check that block header is ok.
113 xfs_btree_check_block(
114 xfs_btree_cur_t
*cur
, /* btree cursor */
115 xfs_btree_block_t
*block
, /* generic btree block pointer */
116 int level
, /* level of the btree block */
117 xfs_buf_t
*bp
) /* buffer containing block, if any */
119 if (XFS_BTREE_LONG_PTRS(cur
->bc_btnum
))
120 xfs_btree_check_lblock(cur
, (xfs_btree_lblock_t
*)block
, level
,
123 xfs_btree_check_sblock(cur
, (xfs_btree_sblock_t
*)block
, level
,
128 * Debug routine: check that keys are in the right order.
132 xfs_btnum_t btnum
, /* btree identifier */
133 void *ak1
, /* pointer to left (lower) key */
134 void *ak2
) /* pointer to right (higher) key */
137 case XFS_BTNUM_BNO
: {
143 ASSERT(INT_GET(k1
->ar_startblock
, ARCH_CONVERT
) < INT_GET(k2
->ar_startblock
, ARCH_CONVERT
));
146 case XFS_BTNUM_CNT
: {
152 ASSERT(INT_GET(k1
->ar_blockcount
, ARCH_CONVERT
) < INT_GET(k2
->ar_blockcount
, ARCH_CONVERT
) ||
153 (INT_GET(k1
->ar_blockcount
, ARCH_CONVERT
) == INT_GET(k2
->ar_blockcount
, ARCH_CONVERT
) &&
154 INT_GET(k1
->ar_startblock
, ARCH_CONVERT
) < INT_GET(k2
->ar_startblock
, ARCH_CONVERT
)));
157 case XFS_BTNUM_BMAP
: {
163 ASSERT(INT_GET(k1
->br_startoff
, ARCH_CONVERT
) < INT_GET(k2
->br_startoff
, ARCH_CONVERT
));
166 case XFS_BTNUM_INO
: {
172 ASSERT(INT_GET(k1
->ir_startino
, ARCH_CONVERT
) < INT_GET(k2
->ir_startino
, ARCH_CONVERT
));
182 * Checking routine: check that long form block header is ok.
185 int /* error (0 or EFSCORRUPTED) */
186 xfs_btree_check_lblock(
187 xfs_btree_cur_t
*cur
, /* btree cursor */
188 xfs_btree_lblock_t
*block
, /* btree long form block pointer */
189 int level
, /* level of the btree block */
190 xfs_buf_t
*bp
) /* buffer for block, if any */
192 int lblock_ok
; /* block passes checks */
193 xfs_mount_t
*mp
; /* file system mount point */
197 INT_GET(block
->bb_magic
, ARCH_CONVERT
) == xfs_magics
[cur
->bc_btnum
] &&
198 INT_GET(block
->bb_level
, ARCH_CONVERT
) == level
&&
199 INT_GET(block
->bb_numrecs
, ARCH_CONVERT
) <=
200 xfs_btree_maxrecs(cur
, (xfs_btree_block_t
*)block
) &&
202 (INT_GET(block
->bb_leftsib
, ARCH_CONVERT
) == NULLDFSBNO
||
203 XFS_FSB_SANITY_CHECK(mp
, INT_GET(block
->bb_leftsib
, ARCH_CONVERT
))) &&
204 block
->bb_rightsib
&&
205 (INT_GET(block
->bb_rightsib
, ARCH_CONVERT
) == NULLDFSBNO
||
206 XFS_FSB_SANITY_CHECK(mp
, INT_GET(block
->bb_rightsib
, ARCH_CONVERT
)));
207 if (unlikely(XFS_TEST_ERROR(!lblock_ok
, mp
, XFS_ERRTAG_BTREE_CHECK_LBLOCK
,
208 XFS_RANDOM_BTREE_CHECK_LBLOCK
))) {
210 xfs_buftrace("LBTREE ERROR", bp
);
211 XFS_ERROR_REPORT("xfs_btree_check_lblock", XFS_ERRLEVEL_LOW
,
213 return XFS_ERROR(EFSCORRUPTED
);
219 * Checking routine: check that (long) pointer is ok.
221 int /* error (0 or EFSCORRUPTED) */
222 xfs_btree_check_lptr(
223 xfs_btree_cur_t
*cur
, /* btree cursor */
224 xfs_dfsbno_t ptr
, /* btree block disk address */
225 int level
) /* btree block level */
227 xfs_mount_t
*mp
; /* file system mount point */
230 XFS_WANT_CORRUPTED_RETURN(
233 XFS_FSB_SANITY_CHECK(mp
, ptr
));
239 * Debug routine: check that records are in the right order.
243 xfs_btnum_t btnum
, /* btree identifier */
244 void *ar1
, /* pointer to left (lower) record */
245 void *ar2
) /* pointer to right (higher) record */
248 case XFS_BTNUM_BNO
: {
254 ASSERT(INT_GET(r1
->ar_startblock
, ARCH_CONVERT
) + INT_GET(r1
->ar_blockcount
, ARCH_CONVERT
) <=
255 INT_GET(r2
->ar_startblock
, ARCH_CONVERT
));
258 case XFS_BTNUM_CNT
: {
264 ASSERT(INT_GET(r1
->ar_blockcount
, ARCH_CONVERT
) < INT_GET(r2
->ar_blockcount
, ARCH_CONVERT
) ||
265 (INT_GET(r1
->ar_blockcount
, ARCH_CONVERT
) == INT_GET(r2
->ar_blockcount
, ARCH_CONVERT
) &&
266 INT_GET(r1
->ar_startblock
, ARCH_CONVERT
) < INT_GET(r2
->ar_startblock
, ARCH_CONVERT
)));
269 case XFS_BTNUM_BMAP
: {
275 ASSERT(xfs_bmbt_disk_get_startoff(r1
) +
276 xfs_bmbt_disk_get_blockcount(r1
) <=
277 xfs_bmbt_disk_get_startoff(r2
));
280 case XFS_BTNUM_INO
: {
286 ASSERT(INT_GET(r1
->ir_startino
, ARCH_CONVERT
) + XFS_INODES_PER_CHUNK
<=
287 INT_GET(r2
->ir_startino
, ARCH_CONVERT
));
297 * Checking routine: check that block header is ok.
300 int /* error (0 or EFSCORRUPTED) */
301 xfs_btree_check_sblock(
302 xfs_btree_cur_t
*cur
, /* btree cursor */
303 xfs_btree_sblock_t
*block
, /* btree short form block pointer */
304 int level
, /* level of the btree block */
305 xfs_buf_t
*bp
) /* buffer containing block */
307 xfs_buf_t
*agbp
; /* buffer for ag. freespace struct */
308 xfs_agf_t
*agf
; /* ag. freespace structure */
309 xfs_agblock_t agflen
; /* native ag. freespace length */
310 int sblock_ok
; /* block passes checks */
312 agbp
= cur
->bc_private
.a
.agbp
;
313 agf
= XFS_BUF_TO_AGF(agbp
);
314 agflen
= INT_GET(agf
->agf_length
, ARCH_CONVERT
);
316 INT_GET(block
->bb_magic
, ARCH_CONVERT
) == xfs_magics
[cur
->bc_btnum
] &&
317 INT_GET(block
->bb_level
, ARCH_CONVERT
) == level
&&
318 INT_GET(block
->bb_numrecs
, ARCH_CONVERT
) <=
319 xfs_btree_maxrecs(cur
, (xfs_btree_block_t
*)block
) &&
320 (INT_GET(block
->bb_leftsib
, ARCH_CONVERT
) == NULLAGBLOCK
||
321 INT_GET(block
->bb_leftsib
, ARCH_CONVERT
) < agflen
) &&
323 (INT_GET(block
->bb_rightsib
, ARCH_CONVERT
) == NULLAGBLOCK
||
324 INT_GET(block
->bb_rightsib
, ARCH_CONVERT
) < agflen
) &&
326 if (unlikely(XFS_TEST_ERROR(!sblock_ok
, cur
->bc_mp
,
327 XFS_ERRTAG_BTREE_CHECK_SBLOCK
,
328 XFS_RANDOM_BTREE_CHECK_SBLOCK
))) {
330 xfs_buftrace("SBTREE ERROR", bp
);
331 XFS_ERROR_REPORT("xfs_btree_check_sblock", XFS_ERRLEVEL_LOW
,
333 return XFS_ERROR(EFSCORRUPTED
);
339 * Checking routine: check that (short) pointer is ok.
341 int /* error (0 or EFSCORRUPTED) */
342 xfs_btree_check_sptr(
343 xfs_btree_cur_t
*cur
, /* btree cursor */
344 xfs_agblock_t ptr
, /* btree block disk address */
345 int level
) /* btree block level */
347 xfs_buf_t
*agbp
; /* buffer for ag. freespace struct */
348 xfs_agf_t
*agf
; /* ag. freespace structure */
350 agbp
= cur
->bc_private
.a
.agbp
;
351 agf
= XFS_BUF_TO_AGF(agbp
);
352 XFS_WANT_CORRUPTED_RETURN(
354 ptr
!= NULLAGBLOCK
&& ptr
!= 0 &&
355 ptr
< INT_GET(agf
->agf_length
, ARCH_CONVERT
));
360 * Delete the btree cursor.
363 xfs_btree_del_cursor(
364 xfs_btree_cur_t
*cur
, /* btree cursor */
365 int error
) /* del because of error */
367 int i
; /* btree level */
370 * Clear the buffer pointers, and release the buffers.
371 * If we're doing this in the face of an error, we
372 * need to make sure to inspect all of the entries
373 * in the bc_bufs array for buffers to be unlocked.
374 * This is because some of the btree code works from
375 * level n down to 0, and if we get an error along
376 * the way we won't have initialized all the entries
379 for (i
= 0; i
< cur
->bc_nlevels
; i
++) {
381 xfs_btree_setbuf(cur
, i
, NULL
);
386 * Can't free a bmap cursor without having dealt with the
387 * allocated indirect blocks' accounting.
389 ASSERT(cur
->bc_btnum
!= XFS_BTNUM_BMAP
||
390 cur
->bc_private
.b
.allocated
== 0);
394 kmem_zone_free(xfs_btree_cur_zone
, cur
);
398 * Duplicate the btree cursor.
399 * Allocate a new one, copy the record, re-get the buffers.
402 xfs_btree_dup_cursor(
403 xfs_btree_cur_t
*cur
, /* input cursor */
404 xfs_btree_cur_t
**ncur
) /* output cursor */
406 xfs_buf_t
*bp
; /* btree block's buffer pointer */
407 int error
; /* error return value */
408 int i
; /* level number of btree block */
409 xfs_mount_t
*mp
; /* mount structure for filesystem */
410 xfs_btree_cur_t
*new; /* new cursor value */
411 xfs_trans_t
*tp
; /* transaction pointer, can be NULL */
416 * Allocate a new cursor like the old one.
418 new = xfs_btree_init_cursor(mp
, tp
, cur
->bc_private
.a
.agbp
,
419 cur
->bc_private
.a
.agno
, cur
->bc_btnum
, cur
->bc_private
.b
.ip
,
420 cur
->bc_private
.b
.whichfork
);
422 * Copy the record currently in the cursor.
424 new->bc_rec
= cur
->bc_rec
;
426 * For each level current, re-get the buffer and copy the ptr value.
428 for (i
= 0; i
< new->bc_nlevels
; i
++) {
429 new->bc_ptrs
[i
] = cur
->bc_ptrs
[i
];
430 new->bc_ra
[i
] = cur
->bc_ra
[i
];
431 if ((bp
= cur
->bc_bufs
[i
])) {
432 if ((error
= xfs_trans_read_buf(mp
, tp
, mp
->m_ddev_targp
,
433 XFS_BUF_ADDR(bp
), mp
->m_bsize
, 0, &bp
))) {
434 xfs_btree_del_cursor(new, error
);
438 new->bc_bufs
[i
] = bp
;
440 ASSERT(!XFS_BUF_GETERROR(bp
));
442 new->bc_bufs
[i
] = NULL
;
445 * For bmap btrees, copy the firstblock, flist, and flags values,
446 * since init cursor doesn't get them.
448 if (new->bc_btnum
== XFS_BTNUM_BMAP
) {
449 new->bc_private
.b
.firstblock
= cur
->bc_private
.b
.firstblock
;
450 new->bc_private
.b
.flist
= cur
->bc_private
.b
.flist
;
451 new->bc_private
.b
.flags
= cur
->bc_private
.b
.flags
;
458 * Change the cursor to point to the first record at the given level.
459 * Other levels are unaffected.
461 int /* success=1, failure=0 */
463 xfs_btree_cur_t
*cur
, /* btree cursor */
464 int level
) /* level to change */
466 xfs_btree_block_t
*block
; /* generic btree block pointer */
467 xfs_buf_t
*bp
; /* buffer containing block */
470 * Get the block pointer for this level.
472 block
= xfs_btree_get_block(cur
, level
, &bp
);
473 xfs_btree_check_block(cur
, block
, level
, bp
);
475 * It's empty, there is no such record.
477 if (!block
->bb_h
.bb_numrecs
)
480 * Set the ptr value to 1, that's the first record/key.
482 cur
->bc_ptrs
[level
] = 1;
487 * Retrieve the block pointer from the cursor at the given level.
488 * This may be a bmap btree root or from a buffer.
490 STATIC xfs_btree_block_t
* /* generic btree block pointer */
492 xfs_btree_cur_t
*cur
, /* btree cursor */
493 int level
, /* level in btree */
494 xfs_buf_t
**bpp
) /* buffer containing the block */
496 xfs_btree_block_t
*block
; /* return value */
497 xfs_buf_t
*bp
; /* return buffer */
498 xfs_ifork_t
*ifp
; /* inode fork pointer */
499 int whichfork
; /* data or attr fork */
501 if (cur
->bc_btnum
== XFS_BTNUM_BMAP
&& level
== cur
->bc_nlevels
- 1) {
502 whichfork
= cur
->bc_private
.b
.whichfork
;
503 ifp
= XFS_IFORK_PTR(cur
->bc_private
.b
.ip
, whichfork
);
504 block
= (xfs_btree_block_t
*)ifp
->if_broot
;
507 bp
= cur
->bc_bufs
[level
];
508 block
= XFS_BUF_TO_BLOCK(bp
);
510 ASSERT(block
!= NULL
);
516 * Get a buffer for the block, return it with no data read.
517 * Long-form addressing.
519 xfs_buf_t
* /* buffer for fsbno */
521 xfs_mount_t
*mp
, /* file system mount point */
522 xfs_trans_t
*tp
, /* transaction pointer */
523 xfs_fsblock_t fsbno
, /* file system block number */
524 uint lock
) /* lock flags for get_buf */
526 xfs_buf_t
*bp
; /* buffer pointer (return value) */
527 xfs_daddr_t d
; /* real disk block address */
529 ASSERT(fsbno
!= NULLFSBLOCK
);
530 d
= XFS_FSB_TO_DADDR(mp
, fsbno
);
531 bp
= xfs_trans_get_buf(tp
, mp
->m_ddev_targp
, d
, mp
->m_bsize
, lock
);
533 ASSERT(!XFS_BUF_GETERROR(bp
));
538 * Get a buffer for the block, return it with no data read.
539 * Short-form addressing.
541 xfs_buf_t
* /* buffer for agno/agbno */
543 xfs_mount_t
*mp
, /* file system mount point */
544 xfs_trans_t
*tp
, /* transaction pointer */
545 xfs_agnumber_t agno
, /* allocation group number */
546 xfs_agblock_t agbno
, /* allocation group block number */
547 uint lock
) /* lock flags for get_buf */
549 xfs_buf_t
*bp
; /* buffer pointer (return value) */
550 xfs_daddr_t d
; /* real disk block address */
552 ASSERT(agno
!= NULLAGNUMBER
);
553 ASSERT(agbno
!= NULLAGBLOCK
);
554 d
= XFS_AGB_TO_DADDR(mp
, agno
, agbno
);
555 bp
= xfs_trans_get_buf(tp
, mp
->m_ddev_targp
, d
, mp
->m_bsize
, lock
);
557 ASSERT(!XFS_BUF_GETERROR(bp
));
562 * Allocate a new btree cursor.
563 * The cursor is either for allocation (A) or bmap (B) or inodes (I).
565 xfs_btree_cur_t
* /* new btree cursor */
566 xfs_btree_init_cursor(
567 xfs_mount_t
*mp
, /* file system mount point */
568 xfs_trans_t
*tp
, /* transaction pointer */
569 xfs_buf_t
*agbp
, /* (A only) buffer for agf structure */
570 /* (I only) buffer for agi structure */
571 xfs_agnumber_t agno
, /* (AI only) allocation group number */
572 xfs_btnum_t btnum
, /* btree identifier */
573 xfs_inode_t
*ip
, /* (B only) inode owning the btree */
574 int whichfork
) /* (B only) data or attr fork */
576 xfs_agf_t
*agf
; /* (A) allocation group freespace */
577 xfs_agi_t
*agi
; /* (I) allocation group inodespace */
578 xfs_btree_cur_t
*cur
; /* return value */
579 xfs_ifork_t
*ifp
; /* (I) inode fork pointer */
580 int nlevels
=0; /* number of levels in the btree */
582 ASSERT(xfs_btree_cur_zone
!= NULL
);
584 * Allocate a new cursor.
586 cur
= kmem_zone_zalloc(xfs_btree_cur_zone
, KM_SLEEP
);
588 * Deduce the number of btree levels from the arguments.
593 agf
= XFS_BUF_TO_AGF(agbp
);
594 nlevels
= INT_GET(agf
->agf_levels
[btnum
], ARCH_CONVERT
);
597 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
598 nlevels
= INT_GET(ifp
->if_broot
->bb_level
, ARCH_CONVERT
) + 1;
601 agi
= XFS_BUF_TO_AGI(agbp
);
602 nlevels
= INT_GET(agi
->agi_level
, ARCH_CONVERT
);
608 * Fill in the common fields.
612 cur
->bc_nlevels
= nlevels
;
613 cur
->bc_btnum
= btnum
;
614 cur
->bc_blocklog
= mp
->m_sb
.sb_blocklog
;
616 * Fill in private fields.
622 * Allocation btree fields.
624 cur
->bc_private
.a
.agbp
= agbp
;
625 cur
->bc_private
.a
.agno
= agno
;
631 cur
->bc_private
.b
.forksize
= XFS_IFORK_SIZE(ip
, whichfork
);
632 cur
->bc_private
.b
.ip
= ip
;
633 cur
->bc_private
.b
.firstblock
= NULLFSBLOCK
;
634 cur
->bc_private
.b
.flist
= NULL
;
635 cur
->bc_private
.b
.allocated
= 0;
636 cur
->bc_private
.b
.flags
= 0;
637 cur
->bc_private
.b
.whichfork
= whichfork
;
641 * Inode allocation btree fields.
643 cur
->bc_private
.i
.agbp
= agbp
;
644 cur
->bc_private
.i
.agno
= agno
;
653 * Check for the cursor referring to the last block at the given level.
655 int /* 1=is last block, 0=not last block */
656 xfs_btree_islastblock(
657 xfs_btree_cur_t
*cur
, /* btree cursor */
658 int level
) /* level to check */
660 xfs_btree_block_t
*block
; /* generic btree block pointer */
661 xfs_buf_t
*bp
; /* buffer containing block */
663 block
= xfs_btree_get_block(cur
, level
, &bp
);
664 xfs_btree_check_block(cur
, block
, level
, bp
);
665 if (XFS_BTREE_LONG_PTRS(cur
->bc_btnum
))
666 return INT_GET(block
->bb_u
.l
.bb_rightsib
, ARCH_CONVERT
) == NULLDFSBNO
;
668 return INT_GET(block
->bb_u
.s
.bb_rightsib
, ARCH_CONVERT
) == NULLAGBLOCK
;
672 * Change the cursor to point to the last record in the current block
673 * at the given level. Other levels are unaffected.
675 int /* success=1, failure=0 */
677 xfs_btree_cur_t
*cur
, /* btree cursor */
678 int level
) /* level to change */
680 xfs_btree_block_t
*block
; /* generic btree block pointer */
681 xfs_buf_t
*bp
; /* buffer containing block */
684 * Get the block pointer for this level.
686 block
= xfs_btree_get_block(cur
, level
, &bp
);
687 xfs_btree_check_block(cur
, block
, level
, bp
);
689 * It's empty, there is no such record.
691 if (!block
->bb_h
.bb_numrecs
)
694 * Set the ptr value to numrecs, that's the last record/key.
696 cur
->bc_ptrs
[level
] = INT_GET(block
->bb_h
.bb_numrecs
, ARCH_CONVERT
);
701 * Compute first and last byte offsets for the fields given.
702 * Interprets the offsets table, which contains struct field offsets.
706 __int64_t fields
, /* bitmask of fields */
707 const short *offsets
, /* table of field offsets */
708 int nbits
, /* number of bits to inspect */
709 int *first
, /* output: first byte offset */
710 int *last
) /* output: last byte offset */
712 int i
; /* current bit number */
713 __int64_t imask
; /* mask for current bit number */
717 * Find the lowest bit, so the first byte offset.
719 for (i
= 0, imask
= 1LL; ; i
++, imask
<<= 1) {
720 if (imask
& fields
) {
726 * Find the highest bit, so the last byte offset.
728 for (i
= nbits
- 1, imask
= 1LL << i
; ; i
--, imask
>>= 1) {
729 if (imask
& fields
) {
730 *last
= offsets
[i
+ 1] - 1;
737 * Get a buffer for the block, return it read in.
738 * Long-form addressing.
742 xfs_mount_t
*mp
, /* file system mount point */
743 xfs_trans_t
*tp
, /* transaction pointer */
744 xfs_fsblock_t fsbno
, /* file system block number */
745 uint lock
, /* lock flags for read_buf */
746 xfs_buf_t
**bpp
, /* buffer for fsbno */
747 int refval
) /* ref count value for buffer */
749 xfs_buf_t
*bp
; /* return value */
750 xfs_daddr_t d
; /* real disk block address */
753 ASSERT(fsbno
!= NULLFSBLOCK
);
754 d
= XFS_FSB_TO_DADDR(mp
, fsbno
);
755 if ((error
= xfs_trans_read_buf(mp
, tp
, mp
->m_ddev_targp
, d
,
756 mp
->m_bsize
, lock
, &bp
))) {
759 ASSERT(!bp
|| !XFS_BUF_GETERROR(bp
));
761 XFS_BUF_SET_VTYPE_REF(bp
, B_FS_MAP
, refval
);
768 * Get a buffer for the block, return it read in.
769 * Short-form addressing.
773 xfs_mount_t
*mp
, /* file system mount point */
774 xfs_trans_t
*tp
, /* transaction pointer */
775 xfs_agnumber_t agno
, /* allocation group number */
776 xfs_agblock_t agbno
, /* allocation group block number */
777 uint lock
, /* lock flags for read_buf */
778 xfs_buf_t
**bpp
, /* buffer for agno/agbno */
779 int refval
) /* ref count value for buffer */
781 xfs_buf_t
*bp
; /* return value */
782 xfs_daddr_t d
; /* real disk block address */
785 ASSERT(agno
!= NULLAGNUMBER
);
786 ASSERT(agbno
!= NULLAGBLOCK
);
787 d
= XFS_AGB_TO_DADDR(mp
, agno
, agbno
);
788 if ((error
= xfs_trans_read_buf(mp
, tp
, mp
->m_ddev_targp
, d
,
789 mp
->m_bsize
, lock
, &bp
))) {
792 ASSERT(!bp
|| !XFS_BUF_GETERROR(bp
));
795 case XFS_ALLOC_BTREE_REF
:
796 XFS_BUF_SET_VTYPE_REF(bp
, B_FS_MAP
, refval
);
798 case XFS_INO_BTREE_REF
:
799 XFS_BUF_SET_VTYPE_REF(bp
, B_FS_INOMAP
, refval
);
808 * Read-ahead the block, don't wait for it, don't return a buffer.
809 * Long-form addressing.
813 xfs_btree_reada_bufl(
814 xfs_mount_t
*mp
, /* file system mount point */
815 xfs_fsblock_t fsbno
, /* file system block number */
816 xfs_extlen_t count
) /* count of filesystem blocks */
820 ASSERT(fsbno
!= NULLFSBLOCK
);
821 d
= XFS_FSB_TO_DADDR(mp
, fsbno
);
822 xfs_baread(mp
->m_ddev_targp
, d
, mp
->m_bsize
* count
);
826 * Read-ahead the block, don't wait for it, don't return a buffer.
827 * Short-form addressing.
831 xfs_btree_reada_bufs(
832 xfs_mount_t
*mp
, /* file system mount point */
833 xfs_agnumber_t agno
, /* allocation group number */
834 xfs_agblock_t agbno
, /* allocation group block number */
835 xfs_extlen_t count
) /* count of filesystem blocks */
839 ASSERT(agno
!= NULLAGNUMBER
);
840 ASSERT(agbno
!= NULLAGBLOCK
);
841 d
= XFS_AGB_TO_DADDR(mp
, agno
, agbno
);
842 xfs_baread(mp
->m_ddev_targp
, d
, mp
->m_bsize
* count
);
846 * Read-ahead btree blocks, at the given level.
847 * Bits in lr are set from XFS_BTCUR_{LEFT,RIGHT}RA.
850 xfs_btree_readahead_core(
851 xfs_btree_cur_t
*cur
, /* btree cursor */
852 int lev
, /* level in btree */
853 int lr
) /* left/right bits */
855 xfs_alloc_block_t
*a
;
857 xfs_inobt_block_t
*i
;
860 ASSERT(cur
->bc_bufs
[lev
] != NULL
);
861 cur
->bc_ra
[lev
] |= lr
;
862 switch (cur
->bc_btnum
) {
865 a
= XFS_BUF_TO_ALLOC_BLOCK(cur
->bc_bufs
[lev
]);
866 if ((lr
& XFS_BTCUR_LEFTRA
) && INT_GET(a
->bb_leftsib
, ARCH_CONVERT
) != NULLAGBLOCK
) {
867 xfs_btree_reada_bufs(cur
->bc_mp
, cur
->bc_private
.a
.agno
,
868 INT_GET(a
->bb_leftsib
, ARCH_CONVERT
), 1);
871 if ((lr
& XFS_BTCUR_RIGHTRA
) && INT_GET(a
->bb_rightsib
, ARCH_CONVERT
) != NULLAGBLOCK
) {
872 xfs_btree_reada_bufs(cur
->bc_mp
, cur
->bc_private
.a
.agno
,
873 INT_GET(a
->bb_rightsib
, ARCH_CONVERT
), 1);
878 b
= XFS_BUF_TO_BMBT_BLOCK(cur
->bc_bufs
[lev
]);
879 if ((lr
& XFS_BTCUR_LEFTRA
) && INT_GET(b
->bb_leftsib
, ARCH_CONVERT
) != NULLDFSBNO
) {
880 xfs_btree_reada_bufl(cur
->bc_mp
, INT_GET(b
->bb_leftsib
, ARCH_CONVERT
), 1);
883 if ((lr
& XFS_BTCUR_RIGHTRA
) && INT_GET(b
->bb_rightsib
, ARCH_CONVERT
) != NULLDFSBNO
) {
884 xfs_btree_reada_bufl(cur
->bc_mp
, INT_GET(b
->bb_rightsib
, ARCH_CONVERT
), 1);
889 i
= XFS_BUF_TO_INOBT_BLOCK(cur
->bc_bufs
[lev
]);
890 if ((lr
& XFS_BTCUR_LEFTRA
) && INT_GET(i
->bb_leftsib
, ARCH_CONVERT
) != NULLAGBLOCK
) {
891 xfs_btree_reada_bufs(cur
->bc_mp
, cur
->bc_private
.i
.agno
,
892 INT_GET(i
->bb_leftsib
, ARCH_CONVERT
), 1);
895 if ((lr
& XFS_BTCUR_RIGHTRA
) && INT_GET(i
->bb_rightsib
, ARCH_CONVERT
) != NULLAGBLOCK
) {
896 xfs_btree_reada_bufs(cur
->bc_mp
, cur
->bc_private
.i
.agno
,
897 INT_GET(i
->bb_rightsib
, ARCH_CONVERT
), 1);
908 * Set the buffer for level "lev" in the cursor to bp, releasing
909 * any previous buffer.
913 xfs_btree_cur_t
*cur
, /* btree cursor */
914 int lev
, /* level in btree */
915 xfs_buf_t
*bp
) /* new buffer to set */
917 xfs_btree_block_t
*b
; /* btree block */
918 xfs_buf_t
*obp
; /* old buffer pointer */
920 obp
= cur
->bc_bufs
[lev
];
922 xfs_trans_brelse(cur
->bc_tp
, obp
);
923 cur
->bc_bufs
[lev
] = bp
;
927 b
= XFS_BUF_TO_BLOCK(bp
);
928 if (XFS_BTREE_LONG_PTRS(cur
->bc_btnum
)) {
929 if (INT_GET(b
->bb_u
.l
.bb_leftsib
, ARCH_CONVERT
) == NULLDFSBNO
)
930 cur
->bc_ra
[lev
] |= XFS_BTCUR_LEFTRA
;
931 if (INT_GET(b
->bb_u
.l
.bb_rightsib
, ARCH_CONVERT
) == NULLDFSBNO
)
932 cur
->bc_ra
[lev
] |= XFS_BTCUR_RIGHTRA
;
934 if (INT_GET(b
->bb_u
.s
.bb_leftsib
, ARCH_CONVERT
) == NULLAGBLOCK
)
935 cur
->bc_ra
[lev
] |= XFS_BTCUR_LEFTRA
;
936 if (INT_GET(b
->bb_u
.s
.bb_rightsib
, ARCH_CONVERT
) == NULLAGBLOCK
)
937 cur
->bc_ra
[lev
] |= XFS_BTCUR_RIGHTRA
;