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"
23 #include "xfs_trans.h"
26 #include "xfs_mount.h"
27 #include "xfs_bmap_btree.h"
28 #include "xfs_alloc_btree.h"
29 #include "xfs_ialloc_btree.h"
30 #include "xfs_dinode.h"
31 #include "xfs_inode.h"
32 #include "xfs_inode_item.h"
33 #include "xfs_btree.h"
34 #include "xfs_error.h"
35 #include "xfs_trace.h"
38 * Cursor allocation zone.
40 kmem_zone_t
*xfs_btree_cur_zone
;
43 * Btree magic numbers.
45 const __uint32_t xfs_magics
[XFS_BTNUM_MAX
] = {
46 XFS_ABTB_MAGIC
, XFS_ABTC_MAGIC
, XFS_BMAP_MAGIC
, XFS_IBT_MAGIC
50 STATIC
int /* error (0 or EFSCORRUPTED) */
51 xfs_btree_check_lblock(
52 struct xfs_btree_cur
*cur
, /* btree cursor */
53 struct xfs_btree_block
*block
, /* btree long form block pointer */
54 int level
, /* level of the btree block */
55 struct xfs_buf
*bp
) /* buffer for block, if any */
57 int lblock_ok
; /* block passes checks */
58 struct xfs_mount
*mp
; /* file system mount point */
62 be32_to_cpu(block
->bb_magic
) == xfs_magics
[cur
->bc_btnum
] &&
63 be16_to_cpu(block
->bb_level
) == level
&&
64 be16_to_cpu(block
->bb_numrecs
) <=
65 cur
->bc_ops
->get_maxrecs(cur
, level
) &&
66 block
->bb_u
.l
.bb_leftsib
&&
67 (block
->bb_u
.l
.bb_leftsib
== cpu_to_be64(NULLDFSBNO
) ||
68 XFS_FSB_SANITY_CHECK(mp
,
69 be64_to_cpu(block
->bb_u
.l
.bb_leftsib
))) &&
70 block
->bb_u
.l
.bb_rightsib
&&
71 (block
->bb_u
.l
.bb_rightsib
== cpu_to_be64(NULLDFSBNO
) ||
72 XFS_FSB_SANITY_CHECK(mp
,
73 be64_to_cpu(block
->bb_u
.l
.bb_rightsib
)));
74 if (unlikely(XFS_TEST_ERROR(!lblock_ok
, mp
,
75 XFS_ERRTAG_BTREE_CHECK_LBLOCK
,
76 XFS_RANDOM_BTREE_CHECK_LBLOCK
))) {
78 trace_xfs_btree_corrupt(bp
, _RET_IP_
);
79 XFS_ERROR_REPORT("xfs_btree_check_lblock", XFS_ERRLEVEL_LOW
,
81 return XFS_ERROR(EFSCORRUPTED
);
86 STATIC
int /* error (0 or EFSCORRUPTED) */
87 xfs_btree_check_sblock(
88 struct xfs_btree_cur
*cur
, /* btree cursor */
89 struct xfs_btree_block
*block
, /* btree short form block pointer */
90 int level
, /* level of the btree block */
91 struct xfs_buf
*bp
) /* buffer containing block */
93 struct xfs_buf
*agbp
; /* buffer for ag. freespace struct */
94 struct xfs_agf
*agf
; /* ag. freespace structure */
95 xfs_agblock_t agflen
; /* native ag. freespace length */
96 int sblock_ok
; /* block passes checks */
98 agbp
= cur
->bc_private
.a
.agbp
;
99 agf
= XFS_BUF_TO_AGF(agbp
);
100 agflen
= be32_to_cpu(agf
->agf_length
);
102 be32_to_cpu(block
->bb_magic
) == xfs_magics
[cur
->bc_btnum
] &&
103 be16_to_cpu(block
->bb_level
) == level
&&
104 be16_to_cpu(block
->bb_numrecs
) <=
105 cur
->bc_ops
->get_maxrecs(cur
, level
) &&
106 (block
->bb_u
.s
.bb_leftsib
== cpu_to_be32(NULLAGBLOCK
) ||
107 be32_to_cpu(block
->bb_u
.s
.bb_leftsib
) < agflen
) &&
108 block
->bb_u
.s
.bb_leftsib
&&
109 (block
->bb_u
.s
.bb_rightsib
== cpu_to_be32(NULLAGBLOCK
) ||
110 be32_to_cpu(block
->bb_u
.s
.bb_rightsib
) < agflen
) &&
111 block
->bb_u
.s
.bb_rightsib
;
112 if (unlikely(XFS_TEST_ERROR(!sblock_ok
, cur
->bc_mp
,
113 XFS_ERRTAG_BTREE_CHECK_SBLOCK
,
114 XFS_RANDOM_BTREE_CHECK_SBLOCK
))) {
116 trace_xfs_btree_corrupt(bp
, _RET_IP_
);
117 XFS_CORRUPTION_ERROR("xfs_btree_check_sblock",
118 XFS_ERRLEVEL_LOW
, cur
->bc_mp
, block
);
119 return XFS_ERROR(EFSCORRUPTED
);
125 * Debug routine: check that block header is ok.
128 xfs_btree_check_block(
129 struct xfs_btree_cur
*cur
, /* btree cursor */
130 struct xfs_btree_block
*block
, /* generic btree block pointer */
131 int level
, /* level of the btree block */
132 struct xfs_buf
*bp
) /* buffer containing block, if any */
134 if (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
)
135 return xfs_btree_check_lblock(cur
, block
, level
, bp
);
137 return xfs_btree_check_sblock(cur
, block
, level
, bp
);
141 * Check that (long) pointer is ok.
143 int /* error (0 or EFSCORRUPTED) */
144 xfs_btree_check_lptr(
145 struct xfs_btree_cur
*cur
, /* btree cursor */
146 xfs_dfsbno_t bno
, /* btree block disk address */
147 int level
) /* btree block level */
149 XFS_WANT_CORRUPTED_RETURN(
152 XFS_FSB_SANITY_CHECK(cur
->bc_mp
, bno
));
158 * Check that (short) pointer is ok.
160 STATIC
int /* error (0 or EFSCORRUPTED) */
161 xfs_btree_check_sptr(
162 struct xfs_btree_cur
*cur
, /* btree cursor */
163 xfs_agblock_t bno
, /* btree block disk address */
164 int level
) /* btree block level */
166 xfs_agblock_t agblocks
= cur
->bc_mp
->m_sb
.sb_agblocks
;
168 XFS_WANT_CORRUPTED_RETURN(
170 bno
!= NULLAGBLOCK
&&
177 * Check that block ptr is ok.
179 STATIC
int /* error (0 or EFSCORRUPTED) */
181 struct xfs_btree_cur
*cur
, /* btree cursor */
182 union xfs_btree_ptr
*ptr
, /* btree block disk address */
183 int index
, /* offset from ptr to check */
184 int level
) /* btree block level */
186 if (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
) {
187 return xfs_btree_check_lptr(cur
,
188 be64_to_cpu((&ptr
->l
)[index
]), level
);
190 return xfs_btree_check_sptr(cur
,
191 be32_to_cpu((&ptr
->s
)[index
]), level
);
197 * Delete the btree cursor.
200 xfs_btree_del_cursor(
201 xfs_btree_cur_t
*cur
, /* btree cursor */
202 int error
) /* del because of error */
204 int i
; /* btree level */
207 * Clear the buffer pointers, and release the buffers.
208 * If we're doing this in the face of an error, we
209 * need to make sure to inspect all of the entries
210 * in the bc_bufs array for buffers to be unlocked.
211 * This is because some of the btree code works from
212 * level n down to 0, and if we get an error along
213 * the way we won't have initialized all the entries
216 for (i
= 0; i
< cur
->bc_nlevels
; i
++) {
218 xfs_trans_brelse(cur
->bc_tp
, cur
->bc_bufs
[i
]);
223 * Can't free a bmap cursor without having dealt with the
224 * allocated indirect blocks' accounting.
226 ASSERT(cur
->bc_btnum
!= XFS_BTNUM_BMAP
||
227 cur
->bc_private
.b
.allocated
== 0);
231 kmem_zone_free(xfs_btree_cur_zone
, cur
);
235 * Duplicate the btree cursor.
236 * Allocate a new one, copy the record, re-get the buffers.
239 xfs_btree_dup_cursor(
240 xfs_btree_cur_t
*cur
, /* input cursor */
241 xfs_btree_cur_t
**ncur
) /* output cursor */
243 xfs_buf_t
*bp
; /* btree block's buffer pointer */
244 int error
; /* error return value */
245 int i
; /* level number of btree block */
246 xfs_mount_t
*mp
; /* mount structure for filesystem */
247 xfs_btree_cur_t
*new; /* new cursor value */
248 xfs_trans_t
*tp
; /* transaction pointer, can be NULL */
254 * Allocate a new cursor like the old one.
256 new = cur
->bc_ops
->dup_cursor(cur
);
259 * Copy the record currently in the cursor.
261 new->bc_rec
= cur
->bc_rec
;
264 * For each level current, re-get the buffer and copy the ptr value.
266 for (i
= 0; i
< new->bc_nlevels
; i
++) {
267 new->bc_ptrs
[i
] = cur
->bc_ptrs
[i
];
268 new->bc_ra
[i
] = cur
->bc_ra
[i
];
269 bp
= cur
->bc_bufs
[i
];
271 error
= xfs_trans_read_buf(mp
, tp
, mp
->m_ddev_targp
,
272 XFS_BUF_ADDR(bp
), mp
->m_bsize
,
274 cur
->bc_ops
->buf_ops
);
276 xfs_btree_del_cursor(new, error
);
280 new->bc_bufs
[i
] = bp
;
281 ASSERT(!xfs_buf_geterror(bp
));
283 new->bc_bufs
[i
] = NULL
;
290 * XFS btree block layout and addressing:
292 * There are two types of blocks in the btree: leaf and non-leaf blocks.
294 * The leaf record start with a header then followed by records containing
295 * the values. A non-leaf block also starts with the same header, and
296 * then first contains lookup keys followed by an equal number of pointers
297 * to the btree blocks at the previous level.
299 * +--------+-------+-------+-------+-------+-------+-------+
300 * Leaf: | header | rec 1 | rec 2 | rec 3 | rec 4 | rec 5 | rec N |
301 * +--------+-------+-------+-------+-------+-------+-------+
303 * +--------+-------+-------+-------+-------+-------+-------+
304 * Non-Leaf: | header | key 1 | key 2 | key N | ptr 1 | ptr 2 | ptr N |
305 * +--------+-------+-------+-------+-------+-------+-------+
307 * The header is called struct xfs_btree_block for reasons better left unknown
308 * and comes in different versions for short (32bit) and long (64bit) block
309 * pointers. The record and key structures are defined by the btree instances
310 * and opaque to the btree core. The block pointers are simple disk endian
311 * integers, available in a short (32bit) and long (64bit) variant.
313 * The helpers below calculate the offset of a given record, key or pointer
314 * into a btree block (xfs_btree_*_offset) or return a pointer to the given
315 * record, key or pointer (xfs_btree_*_addr). Note that all addressing
316 * inside the btree block is done using indices starting at one, not zero!
320 * Return size of the btree block header for this btree instance.
322 static inline size_t xfs_btree_block_len(struct xfs_btree_cur
*cur
)
324 return (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
) ?
325 XFS_BTREE_LBLOCK_LEN
:
326 XFS_BTREE_SBLOCK_LEN
;
330 * Return size of btree block pointers for this btree instance.
332 static inline size_t xfs_btree_ptr_len(struct xfs_btree_cur
*cur
)
334 return (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
) ?
335 sizeof(__be64
) : sizeof(__be32
);
339 * Calculate offset of the n-th record in a btree block.
342 xfs_btree_rec_offset(
343 struct xfs_btree_cur
*cur
,
346 return xfs_btree_block_len(cur
) +
347 (n
- 1) * cur
->bc_ops
->rec_len
;
351 * Calculate offset of the n-th key in a btree block.
354 xfs_btree_key_offset(
355 struct xfs_btree_cur
*cur
,
358 return xfs_btree_block_len(cur
) +
359 (n
- 1) * cur
->bc_ops
->key_len
;
363 * Calculate offset of the n-th block pointer in a btree block.
366 xfs_btree_ptr_offset(
367 struct xfs_btree_cur
*cur
,
371 return xfs_btree_block_len(cur
) +
372 cur
->bc_ops
->get_maxrecs(cur
, level
) * cur
->bc_ops
->key_len
+
373 (n
- 1) * xfs_btree_ptr_len(cur
);
377 * Return a pointer to the n-th record in the btree block.
379 STATIC
union xfs_btree_rec
*
381 struct xfs_btree_cur
*cur
,
383 struct xfs_btree_block
*block
)
385 return (union xfs_btree_rec
*)
386 ((char *)block
+ xfs_btree_rec_offset(cur
, n
));
390 * Return a pointer to the n-th key in the btree block.
392 STATIC
union xfs_btree_key
*
394 struct xfs_btree_cur
*cur
,
396 struct xfs_btree_block
*block
)
398 return (union xfs_btree_key
*)
399 ((char *)block
+ xfs_btree_key_offset(cur
, n
));
403 * Return a pointer to the n-th block pointer in the btree block.
405 STATIC
union xfs_btree_ptr
*
407 struct xfs_btree_cur
*cur
,
409 struct xfs_btree_block
*block
)
411 int level
= xfs_btree_get_level(block
);
413 ASSERT(block
->bb_level
!= 0);
415 return (union xfs_btree_ptr
*)
416 ((char *)block
+ xfs_btree_ptr_offset(cur
, n
, level
));
420 * Get a the root block which is stored in the inode.
422 * For now this btree implementation assumes the btree root is always
423 * stored in the if_broot field of an inode fork.
425 STATIC
struct xfs_btree_block
*
427 struct xfs_btree_cur
*cur
)
429 struct xfs_ifork
*ifp
;
431 ifp
= XFS_IFORK_PTR(cur
->bc_private
.b
.ip
, cur
->bc_private
.b
.whichfork
);
432 return (struct xfs_btree_block
*)ifp
->if_broot
;
436 * Retrieve the block pointer from the cursor at the given level.
437 * This may be an inode btree root or from a buffer.
439 STATIC
struct xfs_btree_block
* /* generic btree block pointer */
441 struct xfs_btree_cur
*cur
, /* btree cursor */
442 int level
, /* level in btree */
443 struct xfs_buf
**bpp
) /* buffer containing the block */
445 if ((cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
) &&
446 (level
== cur
->bc_nlevels
- 1)) {
448 return xfs_btree_get_iroot(cur
);
451 *bpp
= cur
->bc_bufs
[level
];
452 return XFS_BUF_TO_BLOCK(*bpp
);
456 * Get a buffer for the block, return it with no data read.
457 * Long-form addressing.
459 xfs_buf_t
* /* buffer for fsbno */
461 xfs_mount_t
*mp
, /* file system mount point */
462 xfs_trans_t
*tp
, /* transaction pointer */
463 xfs_fsblock_t fsbno
, /* file system block number */
464 uint lock
) /* lock flags for get_buf */
466 xfs_buf_t
*bp
; /* buffer pointer (return value) */
467 xfs_daddr_t d
; /* real disk block address */
469 ASSERT(fsbno
!= NULLFSBLOCK
);
470 d
= XFS_FSB_TO_DADDR(mp
, fsbno
);
471 bp
= xfs_trans_get_buf(tp
, mp
->m_ddev_targp
, d
, mp
->m_bsize
, lock
);
472 ASSERT(!xfs_buf_geterror(bp
));
477 * Get a buffer for the block, return it with no data read.
478 * Short-form addressing.
480 xfs_buf_t
* /* buffer for agno/agbno */
482 xfs_mount_t
*mp
, /* file system mount point */
483 xfs_trans_t
*tp
, /* transaction pointer */
484 xfs_agnumber_t agno
, /* allocation group number */
485 xfs_agblock_t agbno
, /* allocation group block number */
486 uint lock
) /* lock flags for get_buf */
488 xfs_buf_t
*bp
; /* buffer pointer (return value) */
489 xfs_daddr_t d
; /* real disk block address */
491 ASSERT(agno
!= NULLAGNUMBER
);
492 ASSERT(agbno
!= NULLAGBLOCK
);
493 d
= XFS_AGB_TO_DADDR(mp
, agno
, agbno
);
494 bp
= xfs_trans_get_buf(tp
, mp
->m_ddev_targp
, d
, mp
->m_bsize
, lock
);
495 ASSERT(!xfs_buf_geterror(bp
));
500 * Check for the cursor referring to the last block at the given level.
502 int /* 1=is last block, 0=not last block */
503 xfs_btree_islastblock(
504 xfs_btree_cur_t
*cur
, /* btree cursor */
505 int level
) /* level to check */
507 struct xfs_btree_block
*block
; /* generic btree block pointer */
508 xfs_buf_t
*bp
; /* buffer containing block */
510 block
= xfs_btree_get_block(cur
, level
, &bp
);
511 xfs_btree_check_block(cur
, block
, level
, bp
);
512 if (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
)
513 return block
->bb_u
.l
.bb_rightsib
== cpu_to_be64(NULLDFSBNO
);
515 return block
->bb_u
.s
.bb_rightsib
== cpu_to_be32(NULLAGBLOCK
);
519 * Change the cursor to point to the first record at the given level.
520 * Other levels are unaffected.
522 STATIC
int /* success=1, failure=0 */
524 xfs_btree_cur_t
*cur
, /* btree cursor */
525 int level
) /* level to change */
527 struct xfs_btree_block
*block
; /* generic btree block pointer */
528 xfs_buf_t
*bp
; /* buffer containing block */
531 * Get the block pointer for this level.
533 block
= xfs_btree_get_block(cur
, level
, &bp
);
534 xfs_btree_check_block(cur
, block
, level
, bp
);
536 * It's empty, there is no such record.
538 if (!block
->bb_numrecs
)
541 * Set the ptr value to 1, that's the first record/key.
543 cur
->bc_ptrs
[level
] = 1;
548 * Change the cursor to point to the last record in the current block
549 * at the given level. Other levels are unaffected.
551 STATIC
int /* success=1, failure=0 */
553 xfs_btree_cur_t
*cur
, /* btree cursor */
554 int level
) /* level to change */
556 struct xfs_btree_block
*block
; /* generic btree block pointer */
557 xfs_buf_t
*bp
; /* buffer containing block */
560 * Get the block pointer for this level.
562 block
= xfs_btree_get_block(cur
, level
, &bp
);
563 xfs_btree_check_block(cur
, block
, level
, bp
);
565 * It's empty, there is no such record.
567 if (!block
->bb_numrecs
)
570 * Set the ptr value to numrecs, that's the last record/key.
572 cur
->bc_ptrs
[level
] = be16_to_cpu(block
->bb_numrecs
);
577 * Compute first and last byte offsets for the fields given.
578 * Interprets the offsets table, which contains struct field offsets.
582 __int64_t fields
, /* bitmask of fields */
583 const short *offsets
, /* table of field offsets */
584 int nbits
, /* number of bits to inspect */
585 int *first
, /* output: first byte offset */
586 int *last
) /* output: last byte offset */
588 int i
; /* current bit number */
589 __int64_t imask
; /* mask for current bit number */
593 * Find the lowest bit, so the first byte offset.
595 for (i
= 0, imask
= 1LL; ; i
++, imask
<<= 1) {
596 if (imask
& fields
) {
602 * Find the highest bit, so the last byte offset.
604 for (i
= nbits
- 1, imask
= 1LL << i
; ; i
--, imask
>>= 1) {
605 if (imask
& fields
) {
606 *last
= offsets
[i
+ 1] - 1;
613 * Get a buffer for the block, return it read in.
614 * Long-form addressing.
618 struct xfs_mount
*mp
, /* file system mount point */
619 struct xfs_trans
*tp
, /* transaction pointer */
620 xfs_fsblock_t fsbno
, /* file system block number */
621 uint lock
, /* lock flags for read_buf */
622 struct xfs_buf
**bpp
, /* buffer for fsbno */
623 int refval
, /* ref count value for buffer */
624 const struct xfs_buf_ops
*ops
)
626 struct xfs_buf
*bp
; /* return value */
627 xfs_daddr_t d
; /* real disk block address */
630 ASSERT(fsbno
!= NULLFSBLOCK
);
631 d
= XFS_FSB_TO_DADDR(mp
, fsbno
);
632 error
= xfs_trans_read_buf(mp
, tp
, mp
->m_ddev_targp
, d
,
633 mp
->m_bsize
, lock
, &bp
, ops
);
636 ASSERT(!xfs_buf_geterror(bp
));
638 xfs_buf_set_ref(bp
, refval
);
644 * Read-ahead the block, don't wait for it, don't return a buffer.
645 * Long-form addressing.
649 xfs_btree_reada_bufl(
650 struct xfs_mount
*mp
, /* file system mount point */
651 xfs_fsblock_t fsbno
, /* file system block number */
652 xfs_extlen_t count
, /* count of filesystem blocks */
653 const struct xfs_buf_ops
*ops
)
657 ASSERT(fsbno
!= NULLFSBLOCK
);
658 d
= XFS_FSB_TO_DADDR(mp
, fsbno
);
659 xfs_buf_readahead(mp
->m_ddev_targp
, d
, mp
->m_bsize
* count
, ops
);
663 * Read-ahead the block, don't wait for it, don't return a buffer.
664 * Short-form addressing.
668 xfs_btree_reada_bufs(
669 struct xfs_mount
*mp
, /* file system mount point */
670 xfs_agnumber_t agno
, /* allocation group number */
671 xfs_agblock_t agbno
, /* allocation group block number */
672 xfs_extlen_t count
, /* count of filesystem blocks */
673 const struct xfs_buf_ops
*ops
)
677 ASSERT(agno
!= NULLAGNUMBER
);
678 ASSERT(agbno
!= NULLAGBLOCK
);
679 d
= XFS_AGB_TO_DADDR(mp
, agno
, agbno
);
680 xfs_buf_readahead(mp
->m_ddev_targp
, d
, mp
->m_bsize
* count
, ops
);
684 xfs_btree_readahead_lblock(
685 struct xfs_btree_cur
*cur
,
687 struct xfs_btree_block
*block
)
690 xfs_dfsbno_t left
= be64_to_cpu(block
->bb_u
.l
.bb_leftsib
);
691 xfs_dfsbno_t right
= be64_to_cpu(block
->bb_u
.l
.bb_rightsib
);
693 if ((lr
& XFS_BTCUR_LEFTRA
) && left
!= NULLDFSBNO
) {
694 xfs_btree_reada_bufl(cur
->bc_mp
, left
, 1,
695 cur
->bc_ops
->buf_ops
);
699 if ((lr
& XFS_BTCUR_RIGHTRA
) && right
!= NULLDFSBNO
) {
700 xfs_btree_reada_bufl(cur
->bc_mp
, right
, 1,
701 cur
->bc_ops
->buf_ops
);
709 xfs_btree_readahead_sblock(
710 struct xfs_btree_cur
*cur
,
712 struct xfs_btree_block
*block
)
715 xfs_agblock_t left
= be32_to_cpu(block
->bb_u
.s
.bb_leftsib
);
716 xfs_agblock_t right
= be32_to_cpu(block
->bb_u
.s
.bb_rightsib
);
719 if ((lr
& XFS_BTCUR_LEFTRA
) && left
!= NULLAGBLOCK
) {
720 xfs_btree_reada_bufs(cur
->bc_mp
, cur
->bc_private
.a
.agno
,
721 left
, 1, cur
->bc_ops
->buf_ops
);
725 if ((lr
& XFS_BTCUR_RIGHTRA
) && right
!= NULLAGBLOCK
) {
726 xfs_btree_reada_bufs(cur
->bc_mp
, cur
->bc_private
.a
.agno
,
727 right
, 1, cur
->bc_ops
->buf_ops
);
735 * Read-ahead btree blocks, at the given level.
736 * Bits in lr are set from XFS_BTCUR_{LEFT,RIGHT}RA.
740 struct xfs_btree_cur
*cur
, /* btree cursor */
741 int lev
, /* level in btree */
742 int lr
) /* left/right bits */
744 struct xfs_btree_block
*block
;
747 * No readahead needed if we are at the root level and the
748 * btree root is stored in the inode.
750 if ((cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
) &&
751 (lev
== cur
->bc_nlevels
- 1))
754 if ((cur
->bc_ra
[lev
] | lr
) == cur
->bc_ra
[lev
])
757 cur
->bc_ra
[lev
] |= lr
;
758 block
= XFS_BUF_TO_BLOCK(cur
->bc_bufs
[lev
]);
760 if (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
)
761 return xfs_btree_readahead_lblock(cur
, lr
, block
);
762 return xfs_btree_readahead_sblock(cur
, lr
, block
);
766 * Set the buffer for level "lev" in the cursor to bp, releasing
767 * any previous buffer.
771 xfs_btree_cur_t
*cur
, /* btree cursor */
772 int lev
, /* level in btree */
773 xfs_buf_t
*bp
) /* new buffer to set */
775 struct xfs_btree_block
*b
; /* btree block */
777 if (cur
->bc_bufs
[lev
])
778 xfs_trans_brelse(cur
->bc_tp
, cur
->bc_bufs
[lev
]);
779 cur
->bc_bufs
[lev
] = bp
;
782 b
= XFS_BUF_TO_BLOCK(bp
);
783 if (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
) {
784 if (b
->bb_u
.l
.bb_leftsib
== cpu_to_be64(NULLDFSBNO
))
785 cur
->bc_ra
[lev
] |= XFS_BTCUR_LEFTRA
;
786 if (b
->bb_u
.l
.bb_rightsib
== cpu_to_be64(NULLDFSBNO
))
787 cur
->bc_ra
[lev
] |= XFS_BTCUR_RIGHTRA
;
789 if (b
->bb_u
.s
.bb_leftsib
== cpu_to_be32(NULLAGBLOCK
))
790 cur
->bc_ra
[lev
] |= XFS_BTCUR_LEFTRA
;
791 if (b
->bb_u
.s
.bb_rightsib
== cpu_to_be32(NULLAGBLOCK
))
792 cur
->bc_ra
[lev
] |= XFS_BTCUR_RIGHTRA
;
797 xfs_btree_ptr_is_null(
798 struct xfs_btree_cur
*cur
,
799 union xfs_btree_ptr
*ptr
)
801 if (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
)
802 return ptr
->l
== cpu_to_be64(NULLDFSBNO
);
804 return ptr
->s
== cpu_to_be32(NULLAGBLOCK
);
808 xfs_btree_set_ptr_null(
809 struct xfs_btree_cur
*cur
,
810 union xfs_btree_ptr
*ptr
)
812 if (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
)
813 ptr
->l
= cpu_to_be64(NULLDFSBNO
);
815 ptr
->s
= cpu_to_be32(NULLAGBLOCK
);
819 * Get/set/init sibling pointers
822 xfs_btree_get_sibling(
823 struct xfs_btree_cur
*cur
,
824 struct xfs_btree_block
*block
,
825 union xfs_btree_ptr
*ptr
,
828 ASSERT(lr
== XFS_BB_LEFTSIB
|| lr
== XFS_BB_RIGHTSIB
);
830 if (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
) {
831 if (lr
== XFS_BB_RIGHTSIB
)
832 ptr
->l
= block
->bb_u
.l
.bb_rightsib
;
834 ptr
->l
= block
->bb_u
.l
.bb_leftsib
;
836 if (lr
== XFS_BB_RIGHTSIB
)
837 ptr
->s
= block
->bb_u
.s
.bb_rightsib
;
839 ptr
->s
= block
->bb_u
.s
.bb_leftsib
;
844 xfs_btree_set_sibling(
845 struct xfs_btree_cur
*cur
,
846 struct xfs_btree_block
*block
,
847 union xfs_btree_ptr
*ptr
,
850 ASSERT(lr
== XFS_BB_LEFTSIB
|| lr
== XFS_BB_RIGHTSIB
);
852 if (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
) {
853 if (lr
== XFS_BB_RIGHTSIB
)
854 block
->bb_u
.l
.bb_rightsib
= ptr
->l
;
856 block
->bb_u
.l
.bb_leftsib
= ptr
->l
;
858 if (lr
== XFS_BB_RIGHTSIB
)
859 block
->bb_u
.s
.bb_rightsib
= ptr
->s
;
861 block
->bb_u
.s
.bb_leftsib
= ptr
->s
;
866 xfs_btree_init_block(
867 struct xfs_mount
*mp
,
874 struct xfs_btree_block
*new = XFS_BUF_TO_BLOCK(bp
);
876 new->bb_magic
= cpu_to_be32(magic
);
877 new->bb_level
= cpu_to_be16(level
);
878 new->bb_numrecs
= cpu_to_be16(numrecs
);
880 if (flags
& XFS_BTREE_LONG_PTRS
) {
881 new->bb_u
.l
.bb_leftsib
= cpu_to_be64(NULLDFSBNO
);
882 new->bb_u
.l
.bb_rightsib
= cpu_to_be64(NULLDFSBNO
);
884 new->bb_u
.s
.bb_leftsib
= cpu_to_be32(NULLAGBLOCK
);
885 new->bb_u
.s
.bb_rightsib
= cpu_to_be32(NULLAGBLOCK
);
890 xfs_btree_init_block_cur(
891 struct xfs_btree_cur
*cur
,
896 xfs_btree_init_block(cur
->bc_mp
, bp
, xfs_magics
[cur
->bc_btnum
],
897 level
, numrecs
, cur
->bc_flags
);
901 * Return true if ptr is the last record in the btree and
902 * we need to track updateѕ to this record. The decision
903 * will be further refined in the update_lastrec method.
906 xfs_btree_is_lastrec(
907 struct xfs_btree_cur
*cur
,
908 struct xfs_btree_block
*block
,
911 union xfs_btree_ptr ptr
;
915 if (!(cur
->bc_flags
& XFS_BTREE_LASTREC_UPDATE
))
918 xfs_btree_get_sibling(cur
, block
, &ptr
, XFS_BB_RIGHTSIB
);
919 if (!xfs_btree_ptr_is_null(cur
, &ptr
))
925 xfs_btree_buf_to_ptr(
926 struct xfs_btree_cur
*cur
,
928 union xfs_btree_ptr
*ptr
)
930 if (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
)
931 ptr
->l
= cpu_to_be64(XFS_DADDR_TO_FSB(cur
->bc_mp
,
934 ptr
->s
= cpu_to_be32(xfs_daddr_to_agbno(cur
->bc_mp
,
940 xfs_btree_ptr_to_daddr(
941 struct xfs_btree_cur
*cur
,
942 union xfs_btree_ptr
*ptr
)
944 if (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
) {
945 ASSERT(ptr
->l
!= cpu_to_be64(NULLDFSBNO
));
947 return XFS_FSB_TO_DADDR(cur
->bc_mp
, be64_to_cpu(ptr
->l
));
949 ASSERT(cur
->bc_private
.a
.agno
!= NULLAGNUMBER
);
950 ASSERT(ptr
->s
!= cpu_to_be32(NULLAGBLOCK
));
952 return XFS_AGB_TO_DADDR(cur
->bc_mp
, cur
->bc_private
.a
.agno
,
953 be32_to_cpu(ptr
->s
));
959 struct xfs_btree_cur
*cur
,
962 switch (cur
->bc_btnum
) {
965 xfs_buf_set_ref(bp
, XFS_ALLOC_BTREE_REF
);
968 xfs_buf_set_ref(bp
, XFS_INO_BTREE_REF
);
971 xfs_buf_set_ref(bp
, XFS_BMAP_BTREE_REF
);
979 xfs_btree_get_buf_block(
980 struct xfs_btree_cur
*cur
,
981 union xfs_btree_ptr
*ptr
,
983 struct xfs_btree_block
**block
,
984 struct xfs_buf
**bpp
)
986 struct xfs_mount
*mp
= cur
->bc_mp
;
989 /* need to sort out how callers deal with failures first */
990 ASSERT(!(flags
& XBF_TRYLOCK
));
992 d
= xfs_btree_ptr_to_daddr(cur
, ptr
);
993 *bpp
= xfs_trans_get_buf(cur
->bc_tp
, mp
->m_ddev_targp
, d
,
999 (*bpp
)->b_ops
= cur
->bc_ops
->buf_ops
;
1000 *block
= XFS_BUF_TO_BLOCK(*bpp
);
1005 * Read in the buffer at the given ptr and return the buffer and
1006 * the block pointer within the buffer.
1009 xfs_btree_read_buf_block(
1010 struct xfs_btree_cur
*cur
,
1011 union xfs_btree_ptr
*ptr
,
1014 struct xfs_btree_block
**block
,
1015 struct xfs_buf
**bpp
)
1017 struct xfs_mount
*mp
= cur
->bc_mp
;
1021 /* need to sort out how callers deal with failures first */
1022 ASSERT(!(flags
& XBF_TRYLOCK
));
1024 d
= xfs_btree_ptr_to_daddr(cur
, ptr
);
1025 error
= xfs_trans_read_buf(mp
, cur
->bc_tp
, mp
->m_ddev_targp
, d
,
1026 mp
->m_bsize
, flags
, bpp
,
1027 cur
->bc_ops
->buf_ops
);
1031 ASSERT(!xfs_buf_geterror(*bpp
));
1032 xfs_btree_set_refs(cur
, *bpp
);
1033 *block
= XFS_BUF_TO_BLOCK(*bpp
);
1038 * Copy keys from one btree block to another.
1041 xfs_btree_copy_keys(
1042 struct xfs_btree_cur
*cur
,
1043 union xfs_btree_key
*dst_key
,
1044 union xfs_btree_key
*src_key
,
1047 ASSERT(numkeys
>= 0);
1048 memcpy(dst_key
, src_key
, numkeys
* cur
->bc_ops
->key_len
);
1052 * Copy records from one btree block to another.
1055 xfs_btree_copy_recs(
1056 struct xfs_btree_cur
*cur
,
1057 union xfs_btree_rec
*dst_rec
,
1058 union xfs_btree_rec
*src_rec
,
1061 ASSERT(numrecs
>= 0);
1062 memcpy(dst_rec
, src_rec
, numrecs
* cur
->bc_ops
->rec_len
);
1066 * Copy block pointers from one btree block to another.
1069 xfs_btree_copy_ptrs(
1070 struct xfs_btree_cur
*cur
,
1071 union xfs_btree_ptr
*dst_ptr
,
1072 union xfs_btree_ptr
*src_ptr
,
1075 ASSERT(numptrs
>= 0);
1076 memcpy(dst_ptr
, src_ptr
, numptrs
* xfs_btree_ptr_len(cur
));
1080 * Shift keys one index left/right inside a single btree block.
1083 xfs_btree_shift_keys(
1084 struct xfs_btree_cur
*cur
,
1085 union xfs_btree_key
*key
,
1091 ASSERT(numkeys
>= 0);
1092 ASSERT(dir
== 1 || dir
== -1);
1094 dst_key
= (char *)key
+ (dir
* cur
->bc_ops
->key_len
);
1095 memmove(dst_key
, key
, numkeys
* cur
->bc_ops
->key_len
);
1099 * Shift records one index left/right inside a single btree block.
1102 xfs_btree_shift_recs(
1103 struct xfs_btree_cur
*cur
,
1104 union xfs_btree_rec
*rec
,
1110 ASSERT(numrecs
>= 0);
1111 ASSERT(dir
== 1 || dir
== -1);
1113 dst_rec
= (char *)rec
+ (dir
* cur
->bc_ops
->rec_len
);
1114 memmove(dst_rec
, rec
, numrecs
* cur
->bc_ops
->rec_len
);
1118 * Shift block pointers one index left/right inside a single btree block.
1121 xfs_btree_shift_ptrs(
1122 struct xfs_btree_cur
*cur
,
1123 union xfs_btree_ptr
*ptr
,
1129 ASSERT(numptrs
>= 0);
1130 ASSERT(dir
== 1 || dir
== -1);
1132 dst_ptr
= (char *)ptr
+ (dir
* xfs_btree_ptr_len(cur
));
1133 memmove(dst_ptr
, ptr
, numptrs
* xfs_btree_ptr_len(cur
));
1137 * Log key values from the btree block.
1141 struct xfs_btree_cur
*cur
,
1146 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
1147 XFS_BTREE_TRACE_ARGBII(cur
, bp
, first
, last
);
1150 xfs_trans_log_buf(cur
->bc_tp
, bp
,
1151 xfs_btree_key_offset(cur
, first
),
1152 xfs_btree_key_offset(cur
, last
+ 1) - 1);
1154 xfs_trans_log_inode(cur
->bc_tp
, cur
->bc_private
.b
.ip
,
1155 xfs_ilog_fbroot(cur
->bc_private
.b
.whichfork
));
1158 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
1162 * Log record values from the btree block.
1166 struct xfs_btree_cur
*cur
,
1171 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
1172 XFS_BTREE_TRACE_ARGBII(cur
, bp
, first
, last
);
1174 xfs_trans_log_buf(cur
->bc_tp
, bp
,
1175 xfs_btree_rec_offset(cur
, first
),
1176 xfs_btree_rec_offset(cur
, last
+ 1) - 1);
1178 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
1182 * Log block pointer fields from a btree block (nonleaf).
1186 struct xfs_btree_cur
*cur
, /* btree cursor */
1187 struct xfs_buf
*bp
, /* buffer containing btree block */
1188 int first
, /* index of first pointer to log */
1189 int last
) /* index of last pointer to log */
1191 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
1192 XFS_BTREE_TRACE_ARGBII(cur
, bp
, first
, last
);
1195 struct xfs_btree_block
*block
= XFS_BUF_TO_BLOCK(bp
);
1196 int level
= xfs_btree_get_level(block
);
1198 xfs_trans_log_buf(cur
->bc_tp
, bp
,
1199 xfs_btree_ptr_offset(cur
, first
, level
),
1200 xfs_btree_ptr_offset(cur
, last
+ 1, level
) - 1);
1202 xfs_trans_log_inode(cur
->bc_tp
, cur
->bc_private
.b
.ip
,
1203 xfs_ilog_fbroot(cur
->bc_private
.b
.whichfork
));
1206 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
1210 * Log fields from a btree block header.
1213 xfs_btree_log_block(
1214 struct xfs_btree_cur
*cur
, /* btree cursor */
1215 struct xfs_buf
*bp
, /* buffer containing btree block */
1216 int fields
) /* mask of fields: XFS_BB_... */
1218 int first
; /* first byte offset logged */
1219 int last
; /* last byte offset logged */
1220 static const short soffsets
[] = { /* table of offsets (short) */
1221 offsetof(struct xfs_btree_block
, bb_magic
),
1222 offsetof(struct xfs_btree_block
, bb_level
),
1223 offsetof(struct xfs_btree_block
, bb_numrecs
),
1224 offsetof(struct xfs_btree_block
, bb_u
.s
.bb_leftsib
),
1225 offsetof(struct xfs_btree_block
, bb_u
.s
.bb_rightsib
),
1226 XFS_BTREE_SBLOCK_LEN
1228 static const short loffsets
[] = { /* table of offsets (long) */
1229 offsetof(struct xfs_btree_block
, bb_magic
),
1230 offsetof(struct xfs_btree_block
, bb_level
),
1231 offsetof(struct xfs_btree_block
, bb_numrecs
),
1232 offsetof(struct xfs_btree_block
, bb_u
.l
.bb_leftsib
),
1233 offsetof(struct xfs_btree_block
, bb_u
.l
.bb_rightsib
),
1234 XFS_BTREE_LBLOCK_LEN
1237 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
1238 XFS_BTREE_TRACE_ARGBI(cur
, bp
, fields
);
1241 xfs_btree_offsets(fields
,
1242 (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
) ?
1243 loffsets
: soffsets
,
1244 XFS_BB_NUM_BITS
, &first
, &last
);
1245 xfs_trans_log_buf(cur
->bc_tp
, bp
, first
, last
);
1247 xfs_trans_log_inode(cur
->bc_tp
, cur
->bc_private
.b
.ip
,
1248 xfs_ilog_fbroot(cur
->bc_private
.b
.whichfork
));
1251 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
1255 * Increment cursor by one record at the level.
1256 * For nonzero levels the leaf-ward information is untouched.
1259 xfs_btree_increment(
1260 struct xfs_btree_cur
*cur
,
1262 int *stat
) /* success/failure */
1264 struct xfs_btree_block
*block
;
1265 union xfs_btree_ptr ptr
;
1267 int error
; /* error return value */
1270 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
1271 XFS_BTREE_TRACE_ARGI(cur
, level
);
1273 ASSERT(level
< cur
->bc_nlevels
);
1275 /* Read-ahead to the right at this level. */
1276 xfs_btree_readahead(cur
, level
, XFS_BTCUR_RIGHTRA
);
1278 /* Get a pointer to the btree block. */
1279 block
= xfs_btree_get_block(cur
, level
, &bp
);
1282 error
= xfs_btree_check_block(cur
, block
, level
, bp
);
1287 /* We're done if we remain in the block after the increment. */
1288 if (++cur
->bc_ptrs
[level
] <= xfs_btree_get_numrecs(block
))
1291 /* Fail if we just went off the right edge of the tree. */
1292 xfs_btree_get_sibling(cur
, block
, &ptr
, XFS_BB_RIGHTSIB
);
1293 if (xfs_btree_ptr_is_null(cur
, &ptr
))
1296 XFS_BTREE_STATS_INC(cur
, increment
);
1299 * March up the tree incrementing pointers.
1300 * Stop when we don't go off the right edge of a block.
1302 for (lev
= level
+ 1; lev
< cur
->bc_nlevels
; lev
++) {
1303 block
= xfs_btree_get_block(cur
, lev
, &bp
);
1306 error
= xfs_btree_check_block(cur
, block
, lev
, bp
);
1311 if (++cur
->bc_ptrs
[lev
] <= xfs_btree_get_numrecs(block
))
1314 /* Read-ahead the right block for the next loop. */
1315 xfs_btree_readahead(cur
, lev
, XFS_BTCUR_RIGHTRA
);
1319 * If we went off the root then we are either seriously
1320 * confused or have the tree root in an inode.
1322 if (lev
== cur
->bc_nlevels
) {
1323 if (cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
)
1326 error
= EFSCORRUPTED
;
1329 ASSERT(lev
< cur
->bc_nlevels
);
1332 * Now walk back down the tree, fixing up the cursor's buffer
1333 * pointers and key numbers.
1335 for (block
= xfs_btree_get_block(cur
, lev
, &bp
); lev
> level
; ) {
1336 union xfs_btree_ptr
*ptrp
;
1338 ptrp
= xfs_btree_ptr_addr(cur
, cur
->bc_ptrs
[lev
], block
);
1339 error
= xfs_btree_read_buf_block(cur
, ptrp
, --lev
,
1344 xfs_btree_setbuf(cur
, lev
, bp
);
1345 cur
->bc_ptrs
[lev
] = 1;
1348 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
1353 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
1358 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
1363 * Decrement cursor by one record at the level.
1364 * For nonzero levels the leaf-ward information is untouched.
1367 xfs_btree_decrement(
1368 struct xfs_btree_cur
*cur
,
1370 int *stat
) /* success/failure */
1372 struct xfs_btree_block
*block
;
1374 int error
; /* error return value */
1376 union xfs_btree_ptr ptr
;
1378 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
1379 XFS_BTREE_TRACE_ARGI(cur
, level
);
1381 ASSERT(level
< cur
->bc_nlevels
);
1383 /* Read-ahead to the left at this level. */
1384 xfs_btree_readahead(cur
, level
, XFS_BTCUR_LEFTRA
);
1386 /* We're done if we remain in the block after the decrement. */
1387 if (--cur
->bc_ptrs
[level
] > 0)
1390 /* Get a pointer to the btree block. */
1391 block
= xfs_btree_get_block(cur
, level
, &bp
);
1394 error
= xfs_btree_check_block(cur
, block
, level
, bp
);
1399 /* Fail if we just went off the left edge of the tree. */
1400 xfs_btree_get_sibling(cur
, block
, &ptr
, XFS_BB_LEFTSIB
);
1401 if (xfs_btree_ptr_is_null(cur
, &ptr
))
1404 XFS_BTREE_STATS_INC(cur
, decrement
);
1407 * March up the tree decrementing pointers.
1408 * Stop when we don't go off the left edge of a block.
1410 for (lev
= level
+ 1; lev
< cur
->bc_nlevels
; lev
++) {
1411 if (--cur
->bc_ptrs
[lev
] > 0)
1413 /* Read-ahead the left block for the next loop. */
1414 xfs_btree_readahead(cur
, lev
, XFS_BTCUR_LEFTRA
);
1418 * If we went off the root then we are seriously confused.
1419 * or the root of the tree is in an inode.
1421 if (lev
== cur
->bc_nlevels
) {
1422 if (cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
)
1425 error
= EFSCORRUPTED
;
1428 ASSERT(lev
< cur
->bc_nlevels
);
1431 * Now walk back down the tree, fixing up the cursor's buffer
1432 * pointers and key numbers.
1434 for (block
= xfs_btree_get_block(cur
, lev
, &bp
); lev
> level
; ) {
1435 union xfs_btree_ptr
*ptrp
;
1437 ptrp
= xfs_btree_ptr_addr(cur
, cur
->bc_ptrs
[lev
], block
);
1438 error
= xfs_btree_read_buf_block(cur
, ptrp
, --lev
,
1442 xfs_btree_setbuf(cur
, lev
, bp
);
1443 cur
->bc_ptrs
[lev
] = xfs_btree_get_numrecs(block
);
1446 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
1451 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
1456 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
1461 xfs_btree_lookup_get_block(
1462 struct xfs_btree_cur
*cur
, /* btree cursor */
1463 int level
, /* level in the btree */
1464 union xfs_btree_ptr
*pp
, /* ptr to btree block */
1465 struct xfs_btree_block
**blkp
) /* return btree block */
1467 struct xfs_buf
*bp
; /* buffer pointer for btree block */
1470 /* special case the root block if in an inode */
1471 if ((cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
) &&
1472 (level
== cur
->bc_nlevels
- 1)) {
1473 *blkp
= xfs_btree_get_iroot(cur
);
1478 * If the old buffer at this level for the disk address we are
1479 * looking for re-use it.
1481 * Otherwise throw it away and get a new one.
1483 bp
= cur
->bc_bufs
[level
];
1484 if (bp
&& XFS_BUF_ADDR(bp
) == xfs_btree_ptr_to_daddr(cur
, pp
)) {
1485 *blkp
= XFS_BUF_TO_BLOCK(bp
);
1489 error
= xfs_btree_read_buf_block(cur
, pp
, level
, 0, blkp
, &bp
);
1493 xfs_btree_setbuf(cur
, level
, bp
);
1498 * Get current search key. For level 0 we don't actually have a key
1499 * structure so we make one up from the record. For all other levels
1500 * we just return the right key.
1502 STATIC
union xfs_btree_key
*
1503 xfs_lookup_get_search_key(
1504 struct xfs_btree_cur
*cur
,
1507 struct xfs_btree_block
*block
,
1508 union xfs_btree_key
*kp
)
1511 cur
->bc_ops
->init_key_from_rec(kp
,
1512 xfs_btree_rec_addr(cur
, keyno
, block
));
1516 return xfs_btree_key_addr(cur
, keyno
, block
);
1520 * Lookup the record. The cursor is made to point to it, based on dir.
1521 * Return 0 if can't find any such record, 1 for success.
1525 struct xfs_btree_cur
*cur
, /* btree cursor */
1526 xfs_lookup_t dir
, /* <=, ==, or >= */
1527 int *stat
) /* success/failure */
1529 struct xfs_btree_block
*block
; /* current btree block */
1530 __int64_t diff
; /* difference for the current key */
1531 int error
; /* error return value */
1532 int keyno
; /* current key number */
1533 int level
; /* level in the btree */
1534 union xfs_btree_ptr
*pp
; /* ptr to btree block */
1535 union xfs_btree_ptr ptr
; /* ptr to btree block */
1537 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
1538 XFS_BTREE_TRACE_ARGI(cur
, dir
);
1540 XFS_BTREE_STATS_INC(cur
, lookup
);
1545 /* initialise start pointer from cursor */
1546 cur
->bc_ops
->init_ptr_from_cur(cur
, &ptr
);
1550 * Iterate over each level in the btree, starting at the root.
1551 * For each level above the leaves, find the key we need, based
1552 * on the lookup record, then follow the corresponding block
1553 * pointer down to the next level.
1555 for (level
= cur
->bc_nlevels
- 1, diff
= 1; level
>= 0; level
--) {
1556 /* Get the block we need to do the lookup on. */
1557 error
= xfs_btree_lookup_get_block(cur
, level
, pp
, &block
);
1563 * If we already had a key match at a higher level, we
1564 * know we need to use the first entry in this block.
1568 /* Otherwise search this block. Do a binary search. */
1570 int high
; /* high entry number */
1571 int low
; /* low entry number */
1573 /* Set low and high entry numbers, 1-based. */
1575 high
= xfs_btree_get_numrecs(block
);
1577 /* Block is empty, must be an empty leaf. */
1578 ASSERT(level
== 0 && cur
->bc_nlevels
== 1);
1580 cur
->bc_ptrs
[0] = dir
!= XFS_LOOKUP_LE
;
1581 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
1586 /* Binary search the block. */
1587 while (low
<= high
) {
1588 union xfs_btree_key key
;
1589 union xfs_btree_key
*kp
;
1591 XFS_BTREE_STATS_INC(cur
, compare
);
1593 /* keyno is average of low and high. */
1594 keyno
= (low
+ high
) >> 1;
1596 /* Get current search key */
1597 kp
= xfs_lookup_get_search_key(cur
, level
,
1598 keyno
, block
, &key
);
1601 * Compute difference to get next direction:
1602 * - less than, move right
1603 * - greater than, move left
1604 * - equal, we're done
1606 diff
= cur
->bc_ops
->key_diff(cur
, kp
);
1617 * If there are more levels, set up for the next level
1618 * by getting the block number and filling in the cursor.
1622 * If we moved left, need the previous key number,
1623 * unless there isn't one.
1625 if (diff
> 0 && --keyno
< 1)
1627 pp
= xfs_btree_ptr_addr(cur
, keyno
, block
);
1630 error
= xfs_btree_check_ptr(cur
, pp
, 0, level
);
1634 cur
->bc_ptrs
[level
] = keyno
;
1638 /* Done with the search. See if we need to adjust the results. */
1639 if (dir
!= XFS_LOOKUP_LE
&& diff
< 0) {
1642 * If ge search and we went off the end of the block, but it's
1643 * not the last block, we're in the wrong block.
1645 xfs_btree_get_sibling(cur
, block
, &ptr
, XFS_BB_RIGHTSIB
);
1646 if (dir
== XFS_LOOKUP_GE
&&
1647 keyno
> xfs_btree_get_numrecs(block
) &&
1648 !xfs_btree_ptr_is_null(cur
, &ptr
)) {
1651 cur
->bc_ptrs
[0] = keyno
;
1652 error
= xfs_btree_increment(cur
, 0, &i
);
1655 XFS_WANT_CORRUPTED_RETURN(i
== 1);
1656 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
1660 } else if (dir
== XFS_LOOKUP_LE
&& diff
> 0)
1662 cur
->bc_ptrs
[0] = keyno
;
1664 /* Return if we succeeded or not. */
1665 if (keyno
== 0 || keyno
> xfs_btree_get_numrecs(block
))
1667 else if (dir
!= XFS_LOOKUP_EQ
|| diff
== 0)
1671 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
1675 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
1680 * Update keys at all levels from here to the root along the cursor's path.
1684 struct xfs_btree_cur
*cur
,
1685 union xfs_btree_key
*keyp
,
1688 struct xfs_btree_block
*block
;
1690 union xfs_btree_key
*kp
;
1693 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
1694 XFS_BTREE_TRACE_ARGIK(cur
, level
, keyp
);
1696 ASSERT(!(cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
) || level
>= 1);
1699 * Go up the tree from this level toward the root.
1700 * At each level, update the key value to the value input.
1701 * Stop when we reach a level where the cursor isn't pointing
1702 * at the first entry in the block.
1704 for (ptr
= 1; ptr
== 1 && level
< cur
->bc_nlevels
; level
++) {
1708 block
= xfs_btree_get_block(cur
, level
, &bp
);
1710 error
= xfs_btree_check_block(cur
, block
, level
, bp
);
1712 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
1716 ptr
= cur
->bc_ptrs
[level
];
1717 kp
= xfs_btree_key_addr(cur
, ptr
, block
);
1718 xfs_btree_copy_keys(cur
, kp
, keyp
, 1);
1719 xfs_btree_log_keys(cur
, bp
, ptr
, ptr
);
1722 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
1727 * Update the record referred to by cur to the value in the
1728 * given record. This either works (return 0) or gets an
1729 * EFSCORRUPTED error.
1733 struct xfs_btree_cur
*cur
,
1734 union xfs_btree_rec
*rec
)
1736 struct xfs_btree_block
*block
;
1740 union xfs_btree_rec
*rp
;
1742 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
1743 XFS_BTREE_TRACE_ARGR(cur
, rec
);
1745 /* Pick up the current block. */
1746 block
= xfs_btree_get_block(cur
, 0, &bp
);
1749 error
= xfs_btree_check_block(cur
, block
, 0, bp
);
1753 /* Get the address of the rec to be updated. */
1754 ptr
= cur
->bc_ptrs
[0];
1755 rp
= xfs_btree_rec_addr(cur
, ptr
, block
);
1757 /* Fill in the new contents and log them. */
1758 xfs_btree_copy_recs(cur
, rp
, rec
, 1);
1759 xfs_btree_log_recs(cur
, bp
, ptr
, ptr
);
1762 * If we are tracking the last record in the tree and
1763 * we are at the far right edge of the tree, update it.
1765 if (xfs_btree_is_lastrec(cur
, block
, 0)) {
1766 cur
->bc_ops
->update_lastrec(cur
, block
, rec
,
1767 ptr
, LASTREC_UPDATE
);
1770 /* Updating first rec in leaf. Pass new key value up to our parent. */
1772 union xfs_btree_key key
;
1774 cur
->bc_ops
->init_key_from_rec(&key
, rec
);
1775 error
= xfs_btree_updkey(cur
, &key
, 1);
1780 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
1784 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
1789 * Move 1 record left from cur/level if possible.
1790 * Update cur to reflect the new path.
1792 STATIC
int /* error */
1794 struct xfs_btree_cur
*cur
,
1796 int *stat
) /* success/failure */
1798 union xfs_btree_key key
; /* btree key */
1799 struct xfs_buf
*lbp
; /* left buffer pointer */
1800 struct xfs_btree_block
*left
; /* left btree block */
1801 int lrecs
; /* left record count */
1802 struct xfs_buf
*rbp
; /* right buffer pointer */
1803 struct xfs_btree_block
*right
; /* right btree block */
1804 int rrecs
; /* right record count */
1805 union xfs_btree_ptr lptr
; /* left btree pointer */
1806 union xfs_btree_key
*rkp
= NULL
; /* right btree key */
1807 union xfs_btree_ptr
*rpp
= NULL
; /* right address pointer */
1808 union xfs_btree_rec
*rrp
= NULL
; /* right record pointer */
1809 int error
; /* error return value */
1811 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
1812 XFS_BTREE_TRACE_ARGI(cur
, level
);
1814 if ((cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
) &&
1815 level
== cur
->bc_nlevels
- 1)
1818 /* Set up variables for this block as "right". */
1819 right
= xfs_btree_get_block(cur
, level
, &rbp
);
1822 error
= xfs_btree_check_block(cur
, right
, level
, rbp
);
1827 /* If we've got no left sibling then we can't shift an entry left. */
1828 xfs_btree_get_sibling(cur
, right
, &lptr
, XFS_BB_LEFTSIB
);
1829 if (xfs_btree_ptr_is_null(cur
, &lptr
))
1833 * If the cursor entry is the one that would be moved, don't
1834 * do it... it's too complicated.
1836 if (cur
->bc_ptrs
[level
] <= 1)
1839 /* Set up the left neighbor as "left". */
1840 error
= xfs_btree_read_buf_block(cur
, &lptr
, level
, 0, &left
, &lbp
);
1844 /* If it's full, it can't take another entry. */
1845 lrecs
= xfs_btree_get_numrecs(left
);
1846 if (lrecs
== cur
->bc_ops
->get_maxrecs(cur
, level
))
1849 rrecs
= xfs_btree_get_numrecs(right
);
1852 * We add one entry to the left side and remove one for the right side.
1853 * Account for it here, the changes will be updated on disk and logged
1859 XFS_BTREE_STATS_INC(cur
, lshift
);
1860 XFS_BTREE_STATS_ADD(cur
, moves
, 1);
1863 * If non-leaf, copy a key and a ptr to the left block.
1864 * Log the changes to the left block.
1867 /* It's a non-leaf. Move keys and pointers. */
1868 union xfs_btree_key
*lkp
; /* left btree key */
1869 union xfs_btree_ptr
*lpp
; /* left address pointer */
1871 lkp
= xfs_btree_key_addr(cur
, lrecs
, left
);
1872 rkp
= xfs_btree_key_addr(cur
, 1, right
);
1874 lpp
= xfs_btree_ptr_addr(cur
, lrecs
, left
);
1875 rpp
= xfs_btree_ptr_addr(cur
, 1, right
);
1877 error
= xfs_btree_check_ptr(cur
, rpp
, 0, level
);
1881 xfs_btree_copy_keys(cur
, lkp
, rkp
, 1);
1882 xfs_btree_copy_ptrs(cur
, lpp
, rpp
, 1);
1884 xfs_btree_log_keys(cur
, lbp
, lrecs
, lrecs
);
1885 xfs_btree_log_ptrs(cur
, lbp
, lrecs
, lrecs
);
1887 ASSERT(cur
->bc_ops
->keys_inorder(cur
,
1888 xfs_btree_key_addr(cur
, lrecs
- 1, left
), lkp
));
1890 /* It's a leaf. Move records. */
1891 union xfs_btree_rec
*lrp
; /* left record pointer */
1893 lrp
= xfs_btree_rec_addr(cur
, lrecs
, left
);
1894 rrp
= xfs_btree_rec_addr(cur
, 1, right
);
1896 xfs_btree_copy_recs(cur
, lrp
, rrp
, 1);
1897 xfs_btree_log_recs(cur
, lbp
, lrecs
, lrecs
);
1899 ASSERT(cur
->bc_ops
->recs_inorder(cur
,
1900 xfs_btree_rec_addr(cur
, lrecs
- 1, left
), lrp
));
1903 xfs_btree_set_numrecs(left
, lrecs
);
1904 xfs_btree_log_block(cur
, lbp
, XFS_BB_NUMRECS
);
1906 xfs_btree_set_numrecs(right
, rrecs
);
1907 xfs_btree_log_block(cur
, rbp
, XFS_BB_NUMRECS
);
1910 * Slide the contents of right down one entry.
1912 XFS_BTREE_STATS_ADD(cur
, moves
, rrecs
- 1);
1914 /* It's a nonleaf. operate on keys and ptrs */
1916 int i
; /* loop index */
1918 for (i
= 0; i
< rrecs
; i
++) {
1919 error
= xfs_btree_check_ptr(cur
, rpp
, i
+ 1, level
);
1924 xfs_btree_shift_keys(cur
,
1925 xfs_btree_key_addr(cur
, 2, right
),
1927 xfs_btree_shift_ptrs(cur
,
1928 xfs_btree_ptr_addr(cur
, 2, right
),
1931 xfs_btree_log_keys(cur
, rbp
, 1, rrecs
);
1932 xfs_btree_log_ptrs(cur
, rbp
, 1, rrecs
);
1934 /* It's a leaf. operate on records */
1935 xfs_btree_shift_recs(cur
,
1936 xfs_btree_rec_addr(cur
, 2, right
),
1938 xfs_btree_log_recs(cur
, rbp
, 1, rrecs
);
1941 * If it's the first record in the block, we'll need a key
1942 * structure to pass up to the next level (updkey).
1944 cur
->bc_ops
->init_key_from_rec(&key
,
1945 xfs_btree_rec_addr(cur
, 1, right
));
1949 /* Update the parent key values of right. */
1950 error
= xfs_btree_updkey(cur
, rkp
, level
+ 1);
1954 /* Slide the cursor value left one. */
1955 cur
->bc_ptrs
[level
]--;
1957 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
1962 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
1967 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
1972 * Move 1 record right from cur/level if possible.
1973 * Update cur to reflect the new path.
1975 STATIC
int /* error */
1977 struct xfs_btree_cur
*cur
,
1979 int *stat
) /* success/failure */
1981 union xfs_btree_key key
; /* btree key */
1982 struct xfs_buf
*lbp
; /* left buffer pointer */
1983 struct xfs_btree_block
*left
; /* left btree block */
1984 struct xfs_buf
*rbp
; /* right buffer pointer */
1985 struct xfs_btree_block
*right
; /* right btree block */
1986 struct xfs_btree_cur
*tcur
; /* temporary btree cursor */
1987 union xfs_btree_ptr rptr
; /* right block pointer */
1988 union xfs_btree_key
*rkp
; /* right btree key */
1989 int rrecs
; /* right record count */
1990 int lrecs
; /* left record count */
1991 int error
; /* error return value */
1992 int i
; /* loop counter */
1994 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
1995 XFS_BTREE_TRACE_ARGI(cur
, level
);
1997 if ((cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
) &&
1998 (level
== cur
->bc_nlevels
- 1))
2001 /* Set up variables for this block as "left". */
2002 left
= xfs_btree_get_block(cur
, level
, &lbp
);
2005 error
= xfs_btree_check_block(cur
, left
, level
, lbp
);
2010 /* If we've got no right sibling then we can't shift an entry right. */
2011 xfs_btree_get_sibling(cur
, left
, &rptr
, XFS_BB_RIGHTSIB
);
2012 if (xfs_btree_ptr_is_null(cur
, &rptr
))
2016 * If the cursor entry is the one that would be moved, don't
2017 * do it... it's too complicated.
2019 lrecs
= xfs_btree_get_numrecs(left
);
2020 if (cur
->bc_ptrs
[level
] >= lrecs
)
2023 /* Set up the right neighbor as "right". */
2024 error
= xfs_btree_read_buf_block(cur
, &rptr
, level
, 0, &right
, &rbp
);
2028 /* If it's full, it can't take another entry. */
2029 rrecs
= xfs_btree_get_numrecs(right
);
2030 if (rrecs
== cur
->bc_ops
->get_maxrecs(cur
, level
))
2033 XFS_BTREE_STATS_INC(cur
, rshift
);
2034 XFS_BTREE_STATS_ADD(cur
, moves
, rrecs
);
2037 * Make a hole at the start of the right neighbor block, then
2038 * copy the last left block entry to the hole.
2041 /* It's a nonleaf. make a hole in the keys and ptrs */
2042 union xfs_btree_key
*lkp
;
2043 union xfs_btree_ptr
*lpp
;
2044 union xfs_btree_ptr
*rpp
;
2046 lkp
= xfs_btree_key_addr(cur
, lrecs
, left
);
2047 lpp
= xfs_btree_ptr_addr(cur
, lrecs
, left
);
2048 rkp
= xfs_btree_key_addr(cur
, 1, right
);
2049 rpp
= xfs_btree_ptr_addr(cur
, 1, right
);
2052 for (i
= rrecs
- 1; i
>= 0; i
--) {
2053 error
= xfs_btree_check_ptr(cur
, rpp
, i
, level
);
2059 xfs_btree_shift_keys(cur
, rkp
, 1, rrecs
);
2060 xfs_btree_shift_ptrs(cur
, rpp
, 1, rrecs
);
2063 error
= xfs_btree_check_ptr(cur
, lpp
, 0, level
);
2068 /* Now put the new data in, and log it. */
2069 xfs_btree_copy_keys(cur
, rkp
, lkp
, 1);
2070 xfs_btree_copy_ptrs(cur
, rpp
, lpp
, 1);
2072 xfs_btree_log_keys(cur
, rbp
, 1, rrecs
+ 1);
2073 xfs_btree_log_ptrs(cur
, rbp
, 1, rrecs
+ 1);
2075 ASSERT(cur
->bc_ops
->keys_inorder(cur
, rkp
,
2076 xfs_btree_key_addr(cur
, 2, right
)));
2078 /* It's a leaf. make a hole in the records */
2079 union xfs_btree_rec
*lrp
;
2080 union xfs_btree_rec
*rrp
;
2082 lrp
= xfs_btree_rec_addr(cur
, lrecs
, left
);
2083 rrp
= xfs_btree_rec_addr(cur
, 1, right
);
2085 xfs_btree_shift_recs(cur
, rrp
, 1, rrecs
);
2087 /* Now put the new data in, and log it. */
2088 xfs_btree_copy_recs(cur
, rrp
, lrp
, 1);
2089 xfs_btree_log_recs(cur
, rbp
, 1, rrecs
+ 1);
2091 cur
->bc_ops
->init_key_from_rec(&key
, rrp
);
2094 ASSERT(cur
->bc_ops
->recs_inorder(cur
, rrp
,
2095 xfs_btree_rec_addr(cur
, 2, right
)));
2099 * Decrement and log left's numrecs, bump and log right's numrecs.
2101 xfs_btree_set_numrecs(left
, --lrecs
);
2102 xfs_btree_log_block(cur
, lbp
, XFS_BB_NUMRECS
);
2104 xfs_btree_set_numrecs(right
, ++rrecs
);
2105 xfs_btree_log_block(cur
, rbp
, XFS_BB_NUMRECS
);
2108 * Using a temporary cursor, update the parent key values of the
2109 * block on the right.
2111 error
= xfs_btree_dup_cursor(cur
, &tcur
);
2114 i
= xfs_btree_lastrec(tcur
, level
);
2115 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
2117 error
= xfs_btree_increment(tcur
, level
, &i
);
2121 error
= xfs_btree_updkey(tcur
, rkp
, level
+ 1);
2125 xfs_btree_del_cursor(tcur
, XFS_BTREE_NOERROR
);
2127 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
2132 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
2137 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
2141 XFS_BTREE_TRACE_CURSOR(tcur
, XBT_ERROR
);
2142 xfs_btree_del_cursor(tcur
, XFS_BTREE_ERROR
);
2147 * Split cur/level block in half.
2148 * Return new block number and the key to its first
2149 * record (to be inserted into parent).
2151 STATIC
int /* error */
2153 struct xfs_btree_cur
*cur
,
2155 union xfs_btree_ptr
*ptrp
,
2156 union xfs_btree_key
*key
,
2157 struct xfs_btree_cur
**curp
,
2158 int *stat
) /* success/failure */
2160 union xfs_btree_ptr lptr
; /* left sibling block ptr */
2161 struct xfs_buf
*lbp
; /* left buffer pointer */
2162 struct xfs_btree_block
*left
; /* left btree block */
2163 union xfs_btree_ptr rptr
; /* right sibling block ptr */
2164 struct xfs_buf
*rbp
; /* right buffer pointer */
2165 struct xfs_btree_block
*right
; /* right btree block */
2166 union xfs_btree_ptr rrptr
; /* right-right sibling ptr */
2167 struct xfs_buf
*rrbp
; /* right-right buffer pointer */
2168 struct xfs_btree_block
*rrblock
; /* right-right btree block */
2172 int error
; /* error return value */
2177 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
2178 XFS_BTREE_TRACE_ARGIPK(cur
, level
, *ptrp
, key
);
2180 XFS_BTREE_STATS_INC(cur
, split
);
2182 /* Set up left block (current one). */
2183 left
= xfs_btree_get_block(cur
, level
, &lbp
);
2186 error
= xfs_btree_check_block(cur
, left
, level
, lbp
);
2191 xfs_btree_buf_to_ptr(cur
, lbp
, &lptr
);
2193 /* Allocate the new block. If we can't do it, we're toast. Give up. */
2194 error
= cur
->bc_ops
->alloc_block(cur
, &lptr
, &rptr
, 1, stat
);
2199 XFS_BTREE_STATS_INC(cur
, alloc
);
2201 /* Set up the new block as "right". */
2202 error
= xfs_btree_get_buf_block(cur
, &rptr
, 0, &right
, &rbp
);
2206 /* Fill in the btree header for the new right block. */
2207 xfs_btree_init_block_cur(cur
, xfs_btree_get_level(left
), 0, rbp
);
2210 * Split the entries between the old and the new block evenly.
2211 * Make sure that if there's an odd number of entries now, that
2212 * each new block will have the same number of entries.
2214 lrecs
= xfs_btree_get_numrecs(left
);
2216 if ((lrecs
& 1) && cur
->bc_ptrs
[level
] <= rrecs
+ 1)
2218 src_index
= (lrecs
- rrecs
+ 1);
2220 XFS_BTREE_STATS_ADD(cur
, moves
, rrecs
);
2223 * Copy btree block entries from the left block over to the
2224 * new block, the right. Update the right block and log the
2228 /* It's a non-leaf. Move keys and pointers. */
2229 union xfs_btree_key
*lkp
; /* left btree key */
2230 union xfs_btree_ptr
*lpp
; /* left address pointer */
2231 union xfs_btree_key
*rkp
; /* right btree key */
2232 union xfs_btree_ptr
*rpp
; /* right address pointer */
2234 lkp
= xfs_btree_key_addr(cur
, src_index
, left
);
2235 lpp
= xfs_btree_ptr_addr(cur
, src_index
, left
);
2236 rkp
= xfs_btree_key_addr(cur
, 1, right
);
2237 rpp
= xfs_btree_ptr_addr(cur
, 1, right
);
2240 for (i
= src_index
; i
< rrecs
; i
++) {
2241 error
= xfs_btree_check_ptr(cur
, lpp
, i
, level
);
2247 xfs_btree_copy_keys(cur
, rkp
, lkp
, rrecs
);
2248 xfs_btree_copy_ptrs(cur
, rpp
, lpp
, rrecs
);
2250 xfs_btree_log_keys(cur
, rbp
, 1, rrecs
);
2251 xfs_btree_log_ptrs(cur
, rbp
, 1, rrecs
);
2253 /* Grab the keys to the entries moved to the right block */
2254 xfs_btree_copy_keys(cur
, key
, rkp
, 1);
2256 /* It's a leaf. Move records. */
2257 union xfs_btree_rec
*lrp
; /* left record pointer */
2258 union xfs_btree_rec
*rrp
; /* right record pointer */
2260 lrp
= xfs_btree_rec_addr(cur
, src_index
, left
);
2261 rrp
= xfs_btree_rec_addr(cur
, 1, right
);
2263 xfs_btree_copy_recs(cur
, rrp
, lrp
, rrecs
);
2264 xfs_btree_log_recs(cur
, rbp
, 1, rrecs
);
2266 cur
->bc_ops
->init_key_from_rec(key
,
2267 xfs_btree_rec_addr(cur
, 1, right
));
2272 * Find the left block number by looking in the buffer.
2273 * Adjust numrecs, sibling pointers.
2275 xfs_btree_get_sibling(cur
, left
, &rrptr
, XFS_BB_RIGHTSIB
);
2276 xfs_btree_set_sibling(cur
, right
, &rrptr
, XFS_BB_RIGHTSIB
);
2277 xfs_btree_set_sibling(cur
, right
, &lptr
, XFS_BB_LEFTSIB
);
2278 xfs_btree_set_sibling(cur
, left
, &rptr
, XFS_BB_RIGHTSIB
);
2281 xfs_btree_set_numrecs(left
, lrecs
);
2282 xfs_btree_set_numrecs(right
, xfs_btree_get_numrecs(right
) + rrecs
);
2284 xfs_btree_log_block(cur
, rbp
, XFS_BB_ALL_BITS
);
2285 xfs_btree_log_block(cur
, lbp
, XFS_BB_NUMRECS
| XFS_BB_RIGHTSIB
);
2288 * If there's a block to the new block's right, make that block
2289 * point back to right instead of to left.
2291 if (!xfs_btree_ptr_is_null(cur
, &rrptr
)) {
2292 error
= xfs_btree_read_buf_block(cur
, &rrptr
, level
,
2293 0, &rrblock
, &rrbp
);
2296 xfs_btree_set_sibling(cur
, rrblock
, &rptr
, XFS_BB_LEFTSIB
);
2297 xfs_btree_log_block(cur
, rrbp
, XFS_BB_LEFTSIB
);
2300 * If the cursor is really in the right block, move it there.
2301 * If it's just pointing past the last entry in left, then we'll
2302 * insert there, so don't change anything in that case.
2304 if (cur
->bc_ptrs
[level
] > lrecs
+ 1) {
2305 xfs_btree_setbuf(cur
, level
, rbp
);
2306 cur
->bc_ptrs
[level
] -= lrecs
;
2309 * If there are more levels, we'll need another cursor which refers
2310 * the right block, no matter where this cursor was.
2312 if (level
+ 1 < cur
->bc_nlevels
) {
2313 error
= xfs_btree_dup_cursor(cur
, curp
);
2316 (*curp
)->bc_ptrs
[level
+ 1]++;
2319 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
2323 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
2328 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
2333 * Copy the old inode root contents into a real block and make the
2334 * broot point to it.
2337 xfs_btree_new_iroot(
2338 struct xfs_btree_cur
*cur
, /* btree cursor */
2339 int *logflags
, /* logging flags for inode */
2340 int *stat
) /* return status - 0 fail */
2342 struct xfs_buf
*cbp
; /* buffer for cblock */
2343 struct xfs_btree_block
*block
; /* btree block */
2344 struct xfs_btree_block
*cblock
; /* child btree block */
2345 union xfs_btree_key
*ckp
; /* child key pointer */
2346 union xfs_btree_ptr
*cpp
; /* child ptr pointer */
2347 union xfs_btree_key
*kp
; /* pointer to btree key */
2348 union xfs_btree_ptr
*pp
; /* pointer to block addr */
2349 union xfs_btree_ptr nptr
; /* new block addr */
2350 int level
; /* btree level */
2351 int error
; /* error return code */
2353 int i
; /* loop counter */
2356 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
2357 XFS_BTREE_STATS_INC(cur
, newroot
);
2359 ASSERT(cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
);
2361 level
= cur
->bc_nlevels
- 1;
2363 block
= xfs_btree_get_iroot(cur
);
2364 pp
= xfs_btree_ptr_addr(cur
, 1, block
);
2366 /* Allocate the new block. If we can't do it, we're toast. Give up. */
2367 error
= cur
->bc_ops
->alloc_block(cur
, pp
, &nptr
, 1, stat
);
2371 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
2374 XFS_BTREE_STATS_INC(cur
, alloc
);
2376 /* Copy the root into a real block. */
2377 error
= xfs_btree_get_buf_block(cur
, &nptr
, 0, &cblock
, &cbp
);
2381 memcpy(cblock
, block
, xfs_btree_block_len(cur
));
2383 be16_add_cpu(&block
->bb_level
, 1);
2384 xfs_btree_set_numrecs(block
, 1);
2386 cur
->bc_ptrs
[level
+ 1] = 1;
2388 kp
= xfs_btree_key_addr(cur
, 1, block
);
2389 ckp
= xfs_btree_key_addr(cur
, 1, cblock
);
2390 xfs_btree_copy_keys(cur
, ckp
, kp
, xfs_btree_get_numrecs(cblock
));
2392 cpp
= xfs_btree_ptr_addr(cur
, 1, cblock
);
2394 for (i
= 0; i
< be16_to_cpu(cblock
->bb_numrecs
); i
++) {
2395 error
= xfs_btree_check_ptr(cur
, pp
, i
, level
);
2400 xfs_btree_copy_ptrs(cur
, cpp
, pp
, xfs_btree_get_numrecs(cblock
));
2403 error
= xfs_btree_check_ptr(cur
, &nptr
, 0, level
);
2407 xfs_btree_copy_ptrs(cur
, pp
, &nptr
, 1);
2409 xfs_iroot_realloc(cur
->bc_private
.b
.ip
,
2410 1 - xfs_btree_get_numrecs(cblock
),
2411 cur
->bc_private
.b
.whichfork
);
2413 xfs_btree_setbuf(cur
, level
, cbp
);
2416 * Do all this logging at the end so that
2417 * the root is at the right level.
2419 xfs_btree_log_block(cur
, cbp
, XFS_BB_ALL_BITS
);
2420 xfs_btree_log_keys(cur
, cbp
, 1, be16_to_cpu(cblock
->bb_numrecs
));
2421 xfs_btree_log_ptrs(cur
, cbp
, 1, be16_to_cpu(cblock
->bb_numrecs
));
2424 XFS_ILOG_CORE
| xfs_ilog_fbroot(cur
->bc_private
.b
.whichfork
);
2426 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
2429 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
2434 * Allocate a new root block, fill it in.
2436 STATIC
int /* error */
2438 struct xfs_btree_cur
*cur
, /* btree cursor */
2439 int *stat
) /* success/failure */
2441 struct xfs_btree_block
*block
; /* one half of the old root block */
2442 struct xfs_buf
*bp
; /* buffer containing block */
2443 int error
; /* error return value */
2444 struct xfs_buf
*lbp
; /* left buffer pointer */
2445 struct xfs_btree_block
*left
; /* left btree block */
2446 struct xfs_buf
*nbp
; /* new (root) buffer */
2447 struct xfs_btree_block
*new; /* new (root) btree block */
2448 int nptr
; /* new value for key index, 1 or 2 */
2449 struct xfs_buf
*rbp
; /* right buffer pointer */
2450 struct xfs_btree_block
*right
; /* right btree block */
2451 union xfs_btree_ptr rptr
;
2452 union xfs_btree_ptr lptr
;
2454 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
2455 XFS_BTREE_STATS_INC(cur
, newroot
);
2457 /* initialise our start point from the cursor */
2458 cur
->bc_ops
->init_ptr_from_cur(cur
, &rptr
);
2460 /* Allocate the new block. If we can't do it, we're toast. Give up. */
2461 error
= cur
->bc_ops
->alloc_block(cur
, &rptr
, &lptr
, 1, stat
);
2466 XFS_BTREE_STATS_INC(cur
, alloc
);
2468 /* Set up the new block. */
2469 error
= xfs_btree_get_buf_block(cur
, &lptr
, 0, &new, &nbp
);
2473 /* Set the root in the holding structure increasing the level by 1. */
2474 cur
->bc_ops
->set_root(cur
, &lptr
, 1);
2477 * At the previous root level there are now two blocks: the old root,
2478 * and the new block generated when it was split. We don't know which
2479 * one the cursor is pointing at, so we set up variables "left" and
2480 * "right" for each case.
2482 block
= xfs_btree_get_block(cur
, cur
->bc_nlevels
- 1, &bp
);
2485 error
= xfs_btree_check_block(cur
, block
, cur
->bc_nlevels
- 1, bp
);
2490 xfs_btree_get_sibling(cur
, block
, &rptr
, XFS_BB_RIGHTSIB
);
2491 if (!xfs_btree_ptr_is_null(cur
, &rptr
)) {
2492 /* Our block is left, pick up the right block. */
2494 xfs_btree_buf_to_ptr(cur
, lbp
, &lptr
);
2496 error
= xfs_btree_read_buf_block(cur
, &rptr
,
2497 cur
->bc_nlevels
- 1, 0, &right
, &rbp
);
2503 /* Our block is right, pick up the left block. */
2505 xfs_btree_buf_to_ptr(cur
, rbp
, &rptr
);
2507 xfs_btree_get_sibling(cur
, right
, &lptr
, XFS_BB_LEFTSIB
);
2508 error
= xfs_btree_read_buf_block(cur
, &lptr
,
2509 cur
->bc_nlevels
- 1, 0, &left
, &lbp
);
2515 /* Fill in the new block's btree header and log it. */
2516 xfs_btree_init_block_cur(cur
, cur
->bc_nlevels
, 2, nbp
);
2517 xfs_btree_log_block(cur
, nbp
, XFS_BB_ALL_BITS
);
2518 ASSERT(!xfs_btree_ptr_is_null(cur
, &lptr
) &&
2519 !xfs_btree_ptr_is_null(cur
, &rptr
));
2521 /* Fill in the key data in the new root. */
2522 if (xfs_btree_get_level(left
) > 0) {
2523 xfs_btree_copy_keys(cur
,
2524 xfs_btree_key_addr(cur
, 1, new),
2525 xfs_btree_key_addr(cur
, 1, left
), 1);
2526 xfs_btree_copy_keys(cur
,
2527 xfs_btree_key_addr(cur
, 2, new),
2528 xfs_btree_key_addr(cur
, 1, right
), 1);
2530 cur
->bc_ops
->init_key_from_rec(
2531 xfs_btree_key_addr(cur
, 1, new),
2532 xfs_btree_rec_addr(cur
, 1, left
));
2533 cur
->bc_ops
->init_key_from_rec(
2534 xfs_btree_key_addr(cur
, 2, new),
2535 xfs_btree_rec_addr(cur
, 1, right
));
2537 xfs_btree_log_keys(cur
, nbp
, 1, 2);
2539 /* Fill in the pointer data in the new root. */
2540 xfs_btree_copy_ptrs(cur
,
2541 xfs_btree_ptr_addr(cur
, 1, new), &lptr
, 1);
2542 xfs_btree_copy_ptrs(cur
,
2543 xfs_btree_ptr_addr(cur
, 2, new), &rptr
, 1);
2544 xfs_btree_log_ptrs(cur
, nbp
, 1, 2);
2546 /* Fix up the cursor. */
2547 xfs_btree_setbuf(cur
, cur
->bc_nlevels
, nbp
);
2548 cur
->bc_ptrs
[cur
->bc_nlevels
] = nptr
;
2550 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
2554 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
2557 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
2563 xfs_btree_make_block_unfull(
2564 struct xfs_btree_cur
*cur
, /* btree cursor */
2565 int level
, /* btree level */
2566 int numrecs
,/* # of recs in block */
2567 int *oindex
,/* old tree index */
2568 int *index
, /* new tree index */
2569 union xfs_btree_ptr
*nptr
, /* new btree ptr */
2570 struct xfs_btree_cur
**ncur
, /* new btree cursor */
2571 union xfs_btree_rec
*nrec
, /* new record */
2574 union xfs_btree_key key
; /* new btree key value */
2577 if ((cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
) &&
2578 level
== cur
->bc_nlevels
- 1) {
2579 struct xfs_inode
*ip
= cur
->bc_private
.b
.ip
;
2581 if (numrecs
< cur
->bc_ops
->get_dmaxrecs(cur
, level
)) {
2582 /* A root block that can be made bigger. */
2584 xfs_iroot_realloc(ip
, 1, cur
->bc_private
.b
.whichfork
);
2586 /* A root block that needs replacing */
2589 error
= xfs_btree_new_iroot(cur
, &logflags
, stat
);
2590 if (error
|| *stat
== 0)
2593 xfs_trans_log_inode(cur
->bc_tp
, ip
, logflags
);
2599 /* First, try shifting an entry to the right neighbor. */
2600 error
= xfs_btree_rshift(cur
, level
, stat
);
2604 /* Next, try shifting an entry to the left neighbor. */
2605 error
= xfs_btree_lshift(cur
, level
, stat
);
2610 *oindex
= *index
= cur
->bc_ptrs
[level
];
2615 * Next, try splitting the current block in half.
2617 * If this works we have to re-set our variables because we
2618 * could be in a different block now.
2620 error
= xfs_btree_split(cur
, level
, nptr
, &key
, ncur
, stat
);
2621 if (error
|| *stat
== 0)
2625 *index
= cur
->bc_ptrs
[level
];
2626 cur
->bc_ops
->init_rec_from_key(&key
, nrec
);
2631 * Insert one record/level. Return information to the caller
2632 * allowing the next level up to proceed if necessary.
2636 struct xfs_btree_cur
*cur
, /* btree cursor */
2637 int level
, /* level to insert record at */
2638 union xfs_btree_ptr
*ptrp
, /* i/o: block number inserted */
2639 union xfs_btree_rec
*recp
, /* i/o: record data inserted */
2640 struct xfs_btree_cur
**curp
, /* output: new cursor replacing cur */
2641 int *stat
) /* success/failure */
2643 struct xfs_btree_block
*block
; /* btree block */
2644 struct xfs_buf
*bp
; /* buffer for block */
2645 union xfs_btree_key key
; /* btree key */
2646 union xfs_btree_ptr nptr
; /* new block ptr */
2647 struct xfs_btree_cur
*ncur
; /* new btree cursor */
2648 union xfs_btree_rec nrec
; /* new record count */
2649 int optr
; /* old key/record index */
2650 int ptr
; /* key/record index */
2651 int numrecs
;/* number of records */
2652 int error
; /* error return value */
2657 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
2658 XFS_BTREE_TRACE_ARGIPR(cur
, level
, *ptrp
, recp
);
2663 * If we have an external root pointer, and we've made it to the
2664 * root level, allocate a new root block and we're done.
2666 if (!(cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
) &&
2667 (level
>= cur
->bc_nlevels
)) {
2668 error
= xfs_btree_new_root(cur
, stat
);
2669 xfs_btree_set_ptr_null(cur
, ptrp
);
2671 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
2675 /* If we're off the left edge, return failure. */
2676 ptr
= cur
->bc_ptrs
[level
];
2678 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
2683 /* Make a key out of the record data to be inserted, and save it. */
2684 cur
->bc_ops
->init_key_from_rec(&key
, recp
);
2688 XFS_BTREE_STATS_INC(cur
, insrec
);
2690 /* Get pointers to the btree buffer and block. */
2691 block
= xfs_btree_get_block(cur
, level
, &bp
);
2692 numrecs
= xfs_btree_get_numrecs(block
);
2695 error
= xfs_btree_check_block(cur
, block
, level
, bp
);
2699 /* Check that the new entry is being inserted in the right place. */
2700 if (ptr
<= numrecs
) {
2702 ASSERT(cur
->bc_ops
->recs_inorder(cur
, recp
,
2703 xfs_btree_rec_addr(cur
, ptr
, block
)));
2705 ASSERT(cur
->bc_ops
->keys_inorder(cur
, &key
,
2706 xfs_btree_key_addr(cur
, ptr
, block
)));
2712 * If the block is full, we can't insert the new entry until we
2713 * make the block un-full.
2715 xfs_btree_set_ptr_null(cur
, &nptr
);
2716 if (numrecs
== cur
->bc_ops
->get_maxrecs(cur
, level
)) {
2717 error
= xfs_btree_make_block_unfull(cur
, level
, numrecs
,
2718 &optr
, &ptr
, &nptr
, &ncur
, &nrec
, stat
);
2719 if (error
|| *stat
== 0)
2724 * The current block may have changed if the block was
2725 * previously full and we have just made space in it.
2727 block
= xfs_btree_get_block(cur
, level
, &bp
);
2728 numrecs
= xfs_btree_get_numrecs(block
);
2731 error
= xfs_btree_check_block(cur
, block
, level
, bp
);
2737 * At this point we know there's room for our new entry in the block
2738 * we're pointing at.
2740 XFS_BTREE_STATS_ADD(cur
, moves
, numrecs
- ptr
+ 1);
2743 /* It's a nonleaf. make a hole in the keys and ptrs */
2744 union xfs_btree_key
*kp
;
2745 union xfs_btree_ptr
*pp
;
2747 kp
= xfs_btree_key_addr(cur
, ptr
, block
);
2748 pp
= xfs_btree_ptr_addr(cur
, ptr
, block
);
2751 for (i
= numrecs
- ptr
; i
>= 0; i
--) {
2752 error
= xfs_btree_check_ptr(cur
, pp
, i
, level
);
2758 xfs_btree_shift_keys(cur
, kp
, 1, numrecs
- ptr
+ 1);
2759 xfs_btree_shift_ptrs(cur
, pp
, 1, numrecs
- ptr
+ 1);
2762 error
= xfs_btree_check_ptr(cur
, ptrp
, 0, level
);
2767 /* Now put the new data in, bump numrecs and log it. */
2768 xfs_btree_copy_keys(cur
, kp
, &key
, 1);
2769 xfs_btree_copy_ptrs(cur
, pp
, ptrp
, 1);
2771 xfs_btree_set_numrecs(block
, numrecs
);
2772 xfs_btree_log_ptrs(cur
, bp
, ptr
, numrecs
);
2773 xfs_btree_log_keys(cur
, bp
, ptr
, numrecs
);
2775 if (ptr
< numrecs
) {
2776 ASSERT(cur
->bc_ops
->keys_inorder(cur
, kp
,
2777 xfs_btree_key_addr(cur
, ptr
+ 1, block
)));
2781 /* It's a leaf. make a hole in the records */
2782 union xfs_btree_rec
*rp
;
2784 rp
= xfs_btree_rec_addr(cur
, ptr
, block
);
2786 xfs_btree_shift_recs(cur
, rp
, 1, numrecs
- ptr
+ 1);
2788 /* Now put the new data in, bump numrecs and log it. */
2789 xfs_btree_copy_recs(cur
, rp
, recp
, 1);
2790 xfs_btree_set_numrecs(block
, ++numrecs
);
2791 xfs_btree_log_recs(cur
, bp
, ptr
, numrecs
);
2793 if (ptr
< numrecs
) {
2794 ASSERT(cur
->bc_ops
->recs_inorder(cur
, rp
,
2795 xfs_btree_rec_addr(cur
, ptr
+ 1, block
)));
2800 /* Log the new number of records in the btree header. */
2801 xfs_btree_log_block(cur
, bp
, XFS_BB_NUMRECS
);
2803 /* If we inserted at the start of a block, update the parents' keys. */
2805 error
= xfs_btree_updkey(cur
, &key
, level
+ 1);
2811 * If we are tracking the last record in the tree and
2812 * we are at the far right edge of the tree, update it.
2814 if (xfs_btree_is_lastrec(cur
, block
, level
)) {
2815 cur
->bc_ops
->update_lastrec(cur
, block
, recp
,
2816 ptr
, LASTREC_INSREC
);
2820 * Return the new block number, if any.
2821 * If there is one, give back a record value and a cursor too.
2824 if (!xfs_btree_ptr_is_null(cur
, &nptr
)) {
2829 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
2834 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
2839 * Insert the record at the point referenced by cur.
2841 * A multi-level split of the tree on insert will invalidate the original
2842 * cursor. All callers of this function should assume that the cursor is
2843 * no longer valid and revalidate it.
2847 struct xfs_btree_cur
*cur
,
2850 int error
; /* error return value */
2851 int i
; /* result value, 0 for failure */
2852 int level
; /* current level number in btree */
2853 union xfs_btree_ptr nptr
; /* new block number (split result) */
2854 struct xfs_btree_cur
*ncur
; /* new cursor (split result) */
2855 struct xfs_btree_cur
*pcur
; /* previous level's cursor */
2856 union xfs_btree_rec rec
; /* record to insert */
2862 xfs_btree_set_ptr_null(cur
, &nptr
);
2863 cur
->bc_ops
->init_rec_from_cur(cur
, &rec
);
2866 * Loop going up the tree, starting at the leaf level.
2867 * Stop when we don't get a split block, that must mean that
2868 * the insert is finished with this level.
2872 * Insert nrec/nptr into this level of the tree.
2873 * Note if we fail, nptr will be null.
2875 error
= xfs_btree_insrec(pcur
, level
, &nptr
, &rec
, &ncur
, &i
);
2878 xfs_btree_del_cursor(pcur
, XFS_BTREE_ERROR
);
2882 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
2886 * See if the cursor we just used is trash.
2887 * Can't trash the caller's cursor, but otherwise we should
2888 * if ncur is a new cursor or we're about to be done.
2891 (ncur
|| xfs_btree_ptr_is_null(cur
, &nptr
))) {
2892 /* Save the state from the cursor before we trash it */
2893 if (cur
->bc_ops
->update_cursor
)
2894 cur
->bc_ops
->update_cursor(pcur
, cur
);
2895 cur
->bc_nlevels
= pcur
->bc_nlevels
;
2896 xfs_btree_del_cursor(pcur
, XFS_BTREE_NOERROR
);
2898 /* If we got a new cursor, switch to it. */
2903 } while (!xfs_btree_ptr_is_null(cur
, &nptr
));
2905 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
2909 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
2914 * Try to merge a non-leaf block back into the inode root.
2916 * Note: the killroot names comes from the fact that we're effectively
2917 * killing the old root block. But because we can't just delete the
2918 * inode we have to copy the single block it was pointing to into the
2922 xfs_btree_kill_iroot(
2923 struct xfs_btree_cur
*cur
)
2925 int whichfork
= cur
->bc_private
.b
.whichfork
;
2926 struct xfs_inode
*ip
= cur
->bc_private
.b
.ip
;
2927 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
2928 struct xfs_btree_block
*block
;
2929 struct xfs_btree_block
*cblock
;
2930 union xfs_btree_key
*kp
;
2931 union xfs_btree_key
*ckp
;
2932 union xfs_btree_ptr
*pp
;
2933 union xfs_btree_ptr
*cpp
;
2934 struct xfs_buf
*cbp
;
2939 union xfs_btree_ptr ptr
;
2943 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
2945 ASSERT(cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
);
2946 ASSERT(cur
->bc_nlevels
> 1);
2949 * Don't deal with the root block needs to be a leaf case.
2950 * We're just going to turn the thing back into extents anyway.
2952 level
= cur
->bc_nlevels
- 1;
2957 * Give up if the root has multiple children.
2959 block
= xfs_btree_get_iroot(cur
);
2960 if (xfs_btree_get_numrecs(block
) != 1)
2963 cblock
= xfs_btree_get_block(cur
, level
- 1, &cbp
);
2964 numrecs
= xfs_btree_get_numrecs(cblock
);
2967 * Only do this if the next level will fit.
2968 * Then the data must be copied up to the inode,
2969 * instead of freeing the root you free the next level.
2971 if (numrecs
> cur
->bc_ops
->get_dmaxrecs(cur
, level
))
2974 XFS_BTREE_STATS_INC(cur
, killroot
);
2977 xfs_btree_get_sibling(cur
, block
, &ptr
, XFS_BB_LEFTSIB
);
2978 ASSERT(xfs_btree_ptr_is_null(cur
, &ptr
));
2979 xfs_btree_get_sibling(cur
, block
, &ptr
, XFS_BB_RIGHTSIB
);
2980 ASSERT(xfs_btree_ptr_is_null(cur
, &ptr
));
2983 index
= numrecs
- cur
->bc_ops
->get_maxrecs(cur
, level
);
2985 xfs_iroot_realloc(cur
->bc_private
.b
.ip
, index
,
2986 cur
->bc_private
.b
.whichfork
);
2987 block
= ifp
->if_broot
;
2990 be16_add_cpu(&block
->bb_numrecs
, index
);
2991 ASSERT(block
->bb_numrecs
== cblock
->bb_numrecs
);
2993 kp
= xfs_btree_key_addr(cur
, 1, block
);
2994 ckp
= xfs_btree_key_addr(cur
, 1, cblock
);
2995 xfs_btree_copy_keys(cur
, kp
, ckp
, numrecs
);
2997 pp
= xfs_btree_ptr_addr(cur
, 1, block
);
2998 cpp
= xfs_btree_ptr_addr(cur
, 1, cblock
);
3000 for (i
= 0; i
< numrecs
; i
++) {
3003 error
= xfs_btree_check_ptr(cur
, cpp
, i
, level
- 1);
3005 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
3010 xfs_btree_copy_ptrs(cur
, pp
, cpp
, numrecs
);
3012 cur
->bc_ops
->free_block(cur
, cbp
);
3013 XFS_BTREE_STATS_INC(cur
, free
);
3015 cur
->bc_bufs
[level
- 1] = NULL
;
3016 be16_add_cpu(&block
->bb_level
, -1);
3017 xfs_trans_log_inode(cur
->bc_tp
, ip
,
3018 XFS_ILOG_CORE
| xfs_ilog_fbroot(cur
->bc_private
.b
.whichfork
));
3021 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
3026 * Kill the current root node, and replace it with it's only child node.
3029 xfs_btree_kill_root(
3030 struct xfs_btree_cur
*cur
,
3033 union xfs_btree_ptr
*newroot
)
3037 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
3038 XFS_BTREE_STATS_INC(cur
, killroot
);
3041 * Update the root pointer, decreasing the level by 1 and then
3042 * free the old root.
3044 cur
->bc_ops
->set_root(cur
, newroot
, -1);
3046 error
= cur
->bc_ops
->free_block(cur
, bp
);
3048 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
3052 XFS_BTREE_STATS_INC(cur
, free
);
3054 cur
->bc_bufs
[level
] = NULL
;
3055 cur
->bc_ra
[level
] = 0;
3058 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
3063 xfs_btree_dec_cursor(
3064 struct xfs_btree_cur
*cur
,
3072 error
= xfs_btree_decrement(cur
, level
, &i
);
3077 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
3083 * Single level of the btree record deletion routine.
3084 * Delete record pointed to by cur/level.
3085 * Remove the record from its block then rebalance the tree.
3086 * Return 0 for error, 1 for done, 2 to go on to the next level.
3088 STATIC
int /* error */
3090 struct xfs_btree_cur
*cur
, /* btree cursor */
3091 int level
, /* level removing record from */
3092 int *stat
) /* fail/done/go-on */
3094 struct xfs_btree_block
*block
; /* btree block */
3095 union xfs_btree_ptr cptr
; /* current block ptr */
3096 struct xfs_buf
*bp
; /* buffer for block */
3097 int error
; /* error return value */
3098 int i
; /* loop counter */
3099 union xfs_btree_key key
; /* storage for keyp */
3100 union xfs_btree_key
*keyp
= &key
; /* passed to the next level */
3101 union xfs_btree_ptr lptr
; /* left sibling block ptr */
3102 struct xfs_buf
*lbp
; /* left buffer pointer */
3103 struct xfs_btree_block
*left
; /* left btree block */
3104 int lrecs
= 0; /* left record count */
3105 int ptr
; /* key/record index */
3106 union xfs_btree_ptr rptr
; /* right sibling block ptr */
3107 struct xfs_buf
*rbp
; /* right buffer pointer */
3108 struct xfs_btree_block
*right
; /* right btree block */
3109 struct xfs_btree_block
*rrblock
; /* right-right btree block */
3110 struct xfs_buf
*rrbp
; /* right-right buffer pointer */
3111 int rrecs
= 0; /* right record count */
3112 struct xfs_btree_cur
*tcur
; /* temporary btree cursor */
3113 int numrecs
; /* temporary numrec count */
3115 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
3116 XFS_BTREE_TRACE_ARGI(cur
, level
);
3120 /* Get the index of the entry being deleted, check for nothing there. */
3121 ptr
= cur
->bc_ptrs
[level
];
3123 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
3128 /* Get the buffer & block containing the record or key/ptr. */
3129 block
= xfs_btree_get_block(cur
, level
, &bp
);
3130 numrecs
= xfs_btree_get_numrecs(block
);
3133 error
= xfs_btree_check_block(cur
, block
, level
, bp
);
3138 /* Fail if we're off the end of the block. */
3139 if (ptr
> numrecs
) {
3140 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
3145 XFS_BTREE_STATS_INC(cur
, delrec
);
3146 XFS_BTREE_STATS_ADD(cur
, moves
, numrecs
- ptr
);
3148 /* Excise the entries being deleted. */
3150 /* It's a nonleaf. operate on keys and ptrs */
3151 union xfs_btree_key
*lkp
;
3152 union xfs_btree_ptr
*lpp
;
3154 lkp
= xfs_btree_key_addr(cur
, ptr
+ 1, block
);
3155 lpp
= xfs_btree_ptr_addr(cur
, ptr
+ 1, block
);
3158 for (i
= 0; i
< numrecs
- ptr
; i
++) {
3159 error
= xfs_btree_check_ptr(cur
, lpp
, i
, level
);
3165 if (ptr
< numrecs
) {
3166 xfs_btree_shift_keys(cur
, lkp
, -1, numrecs
- ptr
);
3167 xfs_btree_shift_ptrs(cur
, lpp
, -1, numrecs
- ptr
);
3168 xfs_btree_log_keys(cur
, bp
, ptr
, numrecs
- 1);
3169 xfs_btree_log_ptrs(cur
, bp
, ptr
, numrecs
- 1);
3173 * If it's the first record in the block, we'll need to pass a
3174 * key up to the next level (updkey).
3177 keyp
= xfs_btree_key_addr(cur
, 1, block
);
3179 /* It's a leaf. operate on records */
3180 if (ptr
< numrecs
) {
3181 xfs_btree_shift_recs(cur
,
3182 xfs_btree_rec_addr(cur
, ptr
+ 1, block
),
3184 xfs_btree_log_recs(cur
, bp
, ptr
, numrecs
- 1);
3188 * If it's the first record in the block, we'll need a key
3189 * structure to pass up to the next level (updkey).
3192 cur
->bc_ops
->init_key_from_rec(&key
,
3193 xfs_btree_rec_addr(cur
, 1, block
));
3199 * Decrement and log the number of entries in the block.
3201 xfs_btree_set_numrecs(block
, --numrecs
);
3202 xfs_btree_log_block(cur
, bp
, XFS_BB_NUMRECS
);
3205 * If we are tracking the last record in the tree and
3206 * we are at the far right edge of the tree, update it.
3208 if (xfs_btree_is_lastrec(cur
, block
, level
)) {
3209 cur
->bc_ops
->update_lastrec(cur
, block
, NULL
,
3210 ptr
, LASTREC_DELREC
);
3214 * We're at the root level. First, shrink the root block in-memory.
3215 * Try to get rid of the next level down. If we can't then there's
3216 * nothing left to do.
3218 if (level
== cur
->bc_nlevels
- 1) {
3219 if (cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
) {
3220 xfs_iroot_realloc(cur
->bc_private
.b
.ip
, -1,
3221 cur
->bc_private
.b
.whichfork
);
3223 error
= xfs_btree_kill_iroot(cur
);
3227 error
= xfs_btree_dec_cursor(cur
, level
, stat
);
3235 * If this is the root level, and there's only one entry left,
3236 * and it's NOT the leaf level, then we can get rid of this
3239 if (numrecs
== 1 && level
> 0) {
3240 union xfs_btree_ptr
*pp
;
3242 * pp is still set to the first pointer in the block.
3243 * Make it the new root of the btree.
3245 pp
= xfs_btree_ptr_addr(cur
, 1, block
);
3246 error
= xfs_btree_kill_root(cur
, bp
, level
, pp
);
3249 } else if (level
> 0) {
3250 error
= xfs_btree_dec_cursor(cur
, level
, stat
);
3259 * If we deleted the leftmost entry in the block, update the
3260 * key values above us in the tree.
3263 error
= xfs_btree_updkey(cur
, keyp
, level
+ 1);
3269 * If the number of records remaining in the block is at least
3270 * the minimum, we're done.
3272 if (numrecs
>= cur
->bc_ops
->get_minrecs(cur
, level
)) {
3273 error
= xfs_btree_dec_cursor(cur
, level
, stat
);
3280 * Otherwise, we have to move some records around to keep the
3281 * tree balanced. Look at the left and right sibling blocks to
3282 * see if we can re-balance by moving only one record.
3284 xfs_btree_get_sibling(cur
, block
, &rptr
, XFS_BB_RIGHTSIB
);
3285 xfs_btree_get_sibling(cur
, block
, &lptr
, XFS_BB_LEFTSIB
);
3287 if (cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
) {
3289 * One child of root, need to get a chance to copy its contents
3290 * into the root and delete it. Can't go up to next level,
3291 * there's nothing to delete there.
3293 if (xfs_btree_ptr_is_null(cur
, &rptr
) &&
3294 xfs_btree_ptr_is_null(cur
, &lptr
) &&
3295 level
== cur
->bc_nlevels
- 2) {
3296 error
= xfs_btree_kill_iroot(cur
);
3298 error
= xfs_btree_dec_cursor(cur
, level
, stat
);
3305 ASSERT(!xfs_btree_ptr_is_null(cur
, &rptr
) ||
3306 !xfs_btree_ptr_is_null(cur
, &lptr
));
3309 * Duplicate the cursor so our btree manipulations here won't
3310 * disrupt the next level up.
3312 error
= xfs_btree_dup_cursor(cur
, &tcur
);
3317 * If there's a right sibling, see if it's ok to shift an entry
3320 if (!xfs_btree_ptr_is_null(cur
, &rptr
)) {
3322 * Move the temp cursor to the last entry in the next block.
3323 * Actually any entry but the first would suffice.
3325 i
= xfs_btree_lastrec(tcur
, level
);
3326 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
3328 error
= xfs_btree_increment(tcur
, level
, &i
);
3331 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
3333 i
= xfs_btree_lastrec(tcur
, level
);
3334 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
3336 /* Grab a pointer to the block. */
3337 right
= xfs_btree_get_block(tcur
, level
, &rbp
);
3339 error
= xfs_btree_check_block(tcur
, right
, level
, rbp
);
3343 /* Grab the current block number, for future use. */
3344 xfs_btree_get_sibling(tcur
, right
, &cptr
, XFS_BB_LEFTSIB
);
3347 * If right block is full enough so that removing one entry
3348 * won't make it too empty, and left-shifting an entry out
3349 * of right to us works, we're done.
3351 if (xfs_btree_get_numrecs(right
) - 1 >=
3352 cur
->bc_ops
->get_minrecs(tcur
, level
)) {
3353 error
= xfs_btree_lshift(tcur
, level
, &i
);
3357 ASSERT(xfs_btree_get_numrecs(block
) >=
3358 cur
->bc_ops
->get_minrecs(tcur
, level
));
3360 xfs_btree_del_cursor(tcur
, XFS_BTREE_NOERROR
);
3363 error
= xfs_btree_dec_cursor(cur
, level
, stat
);
3371 * Otherwise, grab the number of records in right for
3372 * future reference, and fix up the temp cursor to point
3373 * to our block again (last record).
3375 rrecs
= xfs_btree_get_numrecs(right
);
3376 if (!xfs_btree_ptr_is_null(cur
, &lptr
)) {
3377 i
= xfs_btree_firstrec(tcur
, level
);
3378 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
3380 error
= xfs_btree_decrement(tcur
, level
, &i
);
3383 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
3388 * If there's a left sibling, see if it's ok to shift an entry
3391 if (!xfs_btree_ptr_is_null(cur
, &lptr
)) {
3393 * Move the temp cursor to the first entry in the
3396 i
= xfs_btree_firstrec(tcur
, level
);
3397 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
3399 error
= xfs_btree_decrement(tcur
, level
, &i
);
3402 i
= xfs_btree_firstrec(tcur
, level
);
3403 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
3405 /* Grab a pointer to the block. */
3406 left
= xfs_btree_get_block(tcur
, level
, &lbp
);
3408 error
= xfs_btree_check_block(cur
, left
, level
, lbp
);
3412 /* Grab the current block number, for future use. */
3413 xfs_btree_get_sibling(tcur
, left
, &cptr
, XFS_BB_RIGHTSIB
);
3416 * If left block is full enough so that removing one entry
3417 * won't make it too empty, and right-shifting an entry out
3418 * of left to us works, we're done.
3420 if (xfs_btree_get_numrecs(left
) - 1 >=
3421 cur
->bc_ops
->get_minrecs(tcur
, level
)) {
3422 error
= xfs_btree_rshift(tcur
, level
, &i
);
3426 ASSERT(xfs_btree_get_numrecs(block
) >=
3427 cur
->bc_ops
->get_minrecs(tcur
, level
));
3428 xfs_btree_del_cursor(tcur
, XFS_BTREE_NOERROR
);
3432 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
3439 * Otherwise, grab the number of records in right for
3442 lrecs
= xfs_btree_get_numrecs(left
);
3445 /* Delete the temp cursor, we're done with it. */
3446 xfs_btree_del_cursor(tcur
, XFS_BTREE_NOERROR
);
3449 /* If here, we need to do a join to keep the tree balanced. */
3450 ASSERT(!xfs_btree_ptr_is_null(cur
, &cptr
));
3452 if (!xfs_btree_ptr_is_null(cur
, &lptr
) &&
3453 lrecs
+ xfs_btree_get_numrecs(block
) <=
3454 cur
->bc_ops
->get_maxrecs(cur
, level
)) {
3456 * Set "right" to be the starting block,
3457 * "left" to be the left neighbor.
3462 error
= xfs_btree_read_buf_block(cur
, &lptr
, level
,
3468 * If that won't work, see if we can join with the right neighbor block.
3470 } else if (!xfs_btree_ptr_is_null(cur
, &rptr
) &&
3471 rrecs
+ xfs_btree_get_numrecs(block
) <=
3472 cur
->bc_ops
->get_maxrecs(cur
, level
)) {
3474 * Set "left" to be the starting block,
3475 * "right" to be the right neighbor.
3480 error
= xfs_btree_read_buf_block(cur
, &rptr
, level
,
3486 * Otherwise, we can't fix the imbalance.
3487 * Just return. This is probably a logic error, but it's not fatal.
3490 error
= xfs_btree_dec_cursor(cur
, level
, stat
);
3496 rrecs
= xfs_btree_get_numrecs(right
);
3497 lrecs
= xfs_btree_get_numrecs(left
);
3500 * We're now going to join "left" and "right" by moving all the stuff
3501 * in "right" to "left" and deleting "right".
3503 XFS_BTREE_STATS_ADD(cur
, moves
, rrecs
);
3505 /* It's a non-leaf. Move keys and pointers. */
3506 union xfs_btree_key
*lkp
; /* left btree key */
3507 union xfs_btree_ptr
*lpp
; /* left address pointer */
3508 union xfs_btree_key
*rkp
; /* right btree key */
3509 union xfs_btree_ptr
*rpp
; /* right address pointer */
3511 lkp
= xfs_btree_key_addr(cur
, lrecs
+ 1, left
);
3512 lpp
= xfs_btree_ptr_addr(cur
, lrecs
+ 1, left
);
3513 rkp
= xfs_btree_key_addr(cur
, 1, right
);
3514 rpp
= xfs_btree_ptr_addr(cur
, 1, right
);
3516 for (i
= 1; i
< rrecs
; i
++) {
3517 error
= xfs_btree_check_ptr(cur
, rpp
, i
, level
);
3522 xfs_btree_copy_keys(cur
, lkp
, rkp
, rrecs
);
3523 xfs_btree_copy_ptrs(cur
, lpp
, rpp
, rrecs
);
3525 xfs_btree_log_keys(cur
, lbp
, lrecs
+ 1, lrecs
+ rrecs
);
3526 xfs_btree_log_ptrs(cur
, lbp
, lrecs
+ 1, lrecs
+ rrecs
);
3528 /* It's a leaf. Move records. */
3529 union xfs_btree_rec
*lrp
; /* left record pointer */
3530 union xfs_btree_rec
*rrp
; /* right record pointer */
3532 lrp
= xfs_btree_rec_addr(cur
, lrecs
+ 1, left
);
3533 rrp
= xfs_btree_rec_addr(cur
, 1, right
);
3535 xfs_btree_copy_recs(cur
, lrp
, rrp
, rrecs
);
3536 xfs_btree_log_recs(cur
, lbp
, lrecs
+ 1, lrecs
+ rrecs
);
3539 XFS_BTREE_STATS_INC(cur
, join
);
3542 * Fix up the number of records and right block pointer in the
3543 * surviving block, and log it.
3545 xfs_btree_set_numrecs(left
, lrecs
+ rrecs
);
3546 xfs_btree_get_sibling(cur
, right
, &cptr
, XFS_BB_RIGHTSIB
),
3547 xfs_btree_set_sibling(cur
, left
, &cptr
, XFS_BB_RIGHTSIB
);
3548 xfs_btree_log_block(cur
, lbp
, XFS_BB_NUMRECS
| XFS_BB_RIGHTSIB
);
3550 /* If there is a right sibling, point it to the remaining block. */
3551 xfs_btree_get_sibling(cur
, left
, &cptr
, XFS_BB_RIGHTSIB
);
3552 if (!xfs_btree_ptr_is_null(cur
, &cptr
)) {
3553 error
= xfs_btree_read_buf_block(cur
, &cptr
, level
,
3554 0, &rrblock
, &rrbp
);
3557 xfs_btree_set_sibling(cur
, rrblock
, &lptr
, XFS_BB_LEFTSIB
);
3558 xfs_btree_log_block(cur
, rrbp
, XFS_BB_LEFTSIB
);
3561 /* Free the deleted block. */
3562 error
= cur
->bc_ops
->free_block(cur
, rbp
);
3565 XFS_BTREE_STATS_INC(cur
, free
);
3568 * If we joined with the left neighbor, set the buffer in the
3569 * cursor to the left block, and fix up the index.
3572 cur
->bc_bufs
[level
] = lbp
;
3573 cur
->bc_ptrs
[level
] += lrecs
;
3574 cur
->bc_ra
[level
] = 0;
3577 * If we joined with the right neighbor and there's a level above
3578 * us, increment the cursor at that level.
3580 else if ((cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
) ||
3581 (level
+ 1 < cur
->bc_nlevels
)) {
3582 error
= xfs_btree_increment(cur
, level
+ 1, &i
);
3588 * Readjust the ptr at this level if it's not a leaf, since it's
3589 * still pointing at the deletion point, which makes the cursor
3590 * inconsistent. If this makes the ptr 0, the caller fixes it up.
3591 * We can't use decrement because it would change the next level up.
3594 cur
->bc_ptrs
[level
]--;
3596 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
3597 /* Return value means the next level up has something to do. */
3602 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
3604 xfs_btree_del_cursor(tcur
, XFS_BTREE_ERROR
);
3609 * Delete the record pointed to by cur.
3610 * The cursor refers to the place where the record was (could be inserted)
3611 * when the operation returns.
3615 struct xfs_btree_cur
*cur
,
3616 int *stat
) /* success/failure */
3618 int error
; /* error return value */
3622 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
3625 * Go up the tree, starting at leaf level.
3627 * If 2 is returned then a join was done; go to the next level.
3628 * Otherwise we are done.
3630 for (level
= 0, i
= 2; i
== 2; level
++) {
3631 error
= xfs_btree_delrec(cur
, level
, &i
);
3637 for (level
= 1; level
< cur
->bc_nlevels
; level
++) {
3638 if (cur
->bc_ptrs
[level
] == 0) {
3639 error
= xfs_btree_decrement(cur
, level
, &i
);
3647 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
3651 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
3656 * Get the data from the pointed-to record.
3660 struct xfs_btree_cur
*cur
, /* btree cursor */
3661 union xfs_btree_rec
**recp
, /* output: btree record */
3662 int *stat
) /* output: success/failure */
3664 struct xfs_btree_block
*block
; /* btree block */
3665 struct xfs_buf
*bp
; /* buffer pointer */
3666 int ptr
; /* record number */
3668 int error
; /* error return value */
3671 ptr
= cur
->bc_ptrs
[0];
3672 block
= xfs_btree_get_block(cur
, 0, &bp
);
3675 error
= xfs_btree_check_block(cur
, block
, 0, bp
);
3681 * Off the right end or left end, return failure.
3683 if (ptr
> xfs_btree_get_numrecs(block
) || ptr
<= 0) {
3689 * Point to the record and extract its data.
3691 *recp
= xfs_btree_rec_addr(cur
, ptr
, block
);