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
22 * Cursor allocation zone.
24 kmem_zone_t
*xfs_btree_cur_zone
;
27 * Btree magic numbers.
29 const __uint32_t xfs_magics
[XFS_BTNUM_MAX
] = {
30 XFS_ABTB_MAGIC
, XFS_ABTC_MAGIC
, XFS_BMAP_MAGIC
, XFS_IBT_MAGIC
34 STATIC
int /* error (0 or EFSCORRUPTED) */
35 xfs_btree_check_lblock(
36 struct xfs_btree_cur
*cur
, /* btree cursor */
37 struct xfs_btree_block
*block
, /* btree long form block pointer */
38 int level
, /* level of the btree block */
39 struct xfs_buf
*bp
) /* buffer for block, if any */
41 int lblock_ok
; /* block passes checks */
42 struct xfs_mount
*mp
; /* file system mount point */
46 be32_to_cpu(block
->bb_magic
) == xfs_magics
[cur
->bc_btnum
] &&
47 be16_to_cpu(block
->bb_level
) == level
&&
48 be16_to_cpu(block
->bb_numrecs
) <=
49 cur
->bc_ops
->get_maxrecs(cur
, level
) &&
50 block
->bb_u
.l
.bb_leftsib
&&
51 (be64_to_cpu(block
->bb_u
.l
.bb_leftsib
) == NULLDFSBNO
||
52 XFS_FSB_SANITY_CHECK(mp
,
53 be64_to_cpu(block
->bb_u
.l
.bb_leftsib
))) &&
54 block
->bb_u
.l
.bb_rightsib
&&
55 (be64_to_cpu(block
->bb_u
.l
.bb_rightsib
) == NULLDFSBNO
||
56 XFS_FSB_SANITY_CHECK(mp
,
57 be64_to_cpu(block
->bb_u
.l
.bb_rightsib
)));
58 if (unlikely(XFS_TEST_ERROR(!lblock_ok
, mp
,
59 XFS_ERRTAG_BTREE_CHECK_LBLOCK
,
60 XFS_RANDOM_BTREE_CHECK_LBLOCK
))) {
62 xfs_buftrace("LBTREE ERROR", bp
);
63 XFS_ERROR_REPORT("xfs_btree_check_lblock", XFS_ERRLEVEL_LOW
,
65 return XFS_ERROR(EFSCORRUPTED
);
70 STATIC
int /* error (0 or EFSCORRUPTED) */
71 xfs_btree_check_sblock(
72 struct xfs_btree_cur
*cur
, /* btree cursor */
73 struct xfs_btree_block
*block
, /* btree short form block pointer */
74 int level
, /* level of the btree block */
75 struct xfs_buf
*bp
) /* buffer containing block */
77 struct xfs_buf
*agbp
; /* buffer for ag. freespace struct */
78 struct xfs_agf
*agf
; /* ag. freespace structure */
79 xfs_agblock_t agflen
; /* native ag. freespace length */
80 int sblock_ok
; /* block passes checks */
82 agbp
= cur
->bc_private
.a
.agbp
;
83 agf
= XFS_BUF_TO_AGF(agbp
);
84 agflen
= be32_to_cpu(agf
->agf_length
);
86 be32_to_cpu(block
->bb_magic
) == xfs_magics
[cur
->bc_btnum
] &&
87 be16_to_cpu(block
->bb_level
) == level
&&
88 be16_to_cpu(block
->bb_numrecs
) <=
89 cur
->bc_ops
->get_maxrecs(cur
, level
) &&
90 (be32_to_cpu(block
->bb_u
.s
.bb_leftsib
) == NULLAGBLOCK
||
91 be32_to_cpu(block
->bb_u
.s
.bb_leftsib
) < agflen
) &&
92 block
->bb_u
.s
.bb_leftsib
&&
93 (be32_to_cpu(block
->bb_u
.s
.bb_rightsib
) == NULLAGBLOCK
||
94 be32_to_cpu(block
->bb_u
.s
.bb_rightsib
) < agflen
) &&
95 block
->bb_u
.s
.bb_rightsib
;
96 if (unlikely(XFS_TEST_ERROR(!sblock_ok
, cur
->bc_mp
,
97 XFS_ERRTAG_BTREE_CHECK_SBLOCK
,
98 XFS_RANDOM_BTREE_CHECK_SBLOCK
))) {
100 xfs_buftrace("SBTREE ERROR", bp
);
101 XFS_ERROR_REPORT("xfs_btree_check_sblock", XFS_ERRLEVEL_LOW
,
103 return XFS_ERROR(EFSCORRUPTED
);
109 * Debug routine: check that block header is ok.
112 xfs_btree_check_block(
113 struct xfs_btree_cur
*cur
, /* btree cursor */
114 struct xfs_btree_block
*block
, /* generic btree block pointer */
115 int level
, /* level of the btree block */
116 struct xfs_buf
*bp
) /* buffer containing block, if any */
118 if (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
)
119 return xfs_btree_check_lblock(cur
, block
, level
, bp
);
121 return xfs_btree_check_sblock(cur
, block
, level
, bp
);
125 * Check that (long) pointer is ok.
127 int /* error (0 or EFSCORRUPTED) */
128 xfs_btree_check_lptr(
129 struct xfs_btree_cur
*cur
, /* btree cursor */
130 xfs_dfsbno_t bno
, /* btree block disk address */
131 int level
) /* btree block level */
133 XFS_WANT_CORRUPTED_RETURN(
136 XFS_FSB_SANITY_CHECK(cur
->bc_mp
, bno
));
142 * Check that (short) pointer is ok.
144 STATIC
int /* error (0 or EFSCORRUPTED) */
145 xfs_btree_check_sptr(
146 struct xfs_btree_cur
*cur
, /* btree cursor */
147 xfs_agblock_t bno
, /* btree block disk address */
148 int level
) /* btree block level */
150 xfs_agblock_t agblocks
= cur
->bc_mp
->m_sb
.sb_agblocks
;
152 XFS_WANT_CORRUPTED_RETURN(
154 bno
!= NULLAGBLOCK
&&
161 * Check that block ptr is ok.
163 STATIC
int /* error (0 or EFSCORRUPTED) */
165 struct xfs_btree_cur
*cur
, /* btree cursor */
166 union xfs_btree_ptr
*ptr
, /* btree block disk address */
167 int index
, /* offset from ptr to check */
168 int level
) /* btree block level */
170 if (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
) {
171 return xfs_btree_check_lptr(cur
,
172 be64_to_cpu((&ptr
->l
)[index
]), level
);
174 return xfs_btree_check_sptr(cur
,
175 be32_to_cpu((&ptr
->s
)[index
]), level
);
181 * Delete the btree cursor.
184 xfs_btree_del_cursor(
185 xfs_btree_cur_t
*cur
, /* btree cursor */
186 int error
) /* del because of error */
188 int i
; /* btree level */
191 * Clear the buffer pointers, and release the buffers.
192 * If we're doing this in the face of an error, we
193 * need to make sure to inspect all of the entries
194 * in the bc_bufs array for buffers to be unlocked.
195 * This is because some of the btree code works from
196 * level n down to 0, and if we get an error along
197 * the way we won't have initialized all the entries
200 for (i
= 0; i
< cur
->bc_nlevels
; i
++) {
202 xfs_btree_setbuf(cur
, i
, NULL
);
207 * Can't free a bmap cursor without having dealt with the
208 * allocated indirect blocks' accounting.
210 ASSERT(cur
->bc_btnum
!= XFS_BTNUM_BMAP
||
211 cur
->bc_private
.b
.allocated
== 0);
215 kmem_zone_free(xfs_btree_cur_zone
, cur
);
219 * Duplicate the btree cursor.
220 * Allocate a new one, copy the record, re-get the buffers.
223 xfs_btree_dup_cursor(
224 xfs_btree_cur_t
*cur
, /* input cursor */
225 xfs_btree_cur_t
**ncur
) /* output cursor */
227 xfs_buf_t
*bp
; /* btree block's buffer pointer */
228 int error
; /* error return value */
229 int i
; /* level number of btree block */
230 xfs_mount_t
*mp
; /* mount structure for filesystem */
231 xfs_btree_cur_t
*new; /* new cursor value */
232 xfs_trans_t
*tp
; /* transaction pointer, can be NULL */
238 * Allocate a new cursor like the old one.
240 new = cur
->bc_ops
->dup_cursor(cur
);
243 * Copy the record currently in the cursor.
245 new->bc_rec
= cur
->bc_rec
;
248 * For each level current, re-get the buffer and copy the ptr value.
250 for (i
= 0; i
< new->bc_nlevels
; i
++) {
251 new->bc_ptrs
[i
] = cur
->bc_ptrs
[i
];
252 new->bc_ra
[i
] = cur
->bc_ra
[i
];
253 if ((bp
= cur
->bc_bufs
[i
])) {
254 if ((error
= xfs_trans_read_buf(mp
, tp
, mp
->m_ddev_targp
,
255 XFS_BUF_ADDR(bp
), mp
->m_bsize
, 0, &bp
))) {
256 xfs_btree_del_cursor(new, error
);
260 new->bc_bufs
[i
] = bp
;
262 ASSERT(!XFS_BUF_GETERROR(bp
));
264 new->bc_bufs
[i
] = NULL
;
271 * XFS btree block layout and addressing:
273 * There are two types of blocks in the btree: leaf and non-leaf blocks.
275 * The leaf record start with a header then followed by records containing
276 * the values. A non-leaf block also starts with the same header, and
277 * then first contains lookup keys followed by an equal number of pointers
278 * to the btree blocks at the previous level.
280 * +--------+-------+-------+-------+-------+-------+-------+
281 * Leaf: | header | rec 1 | rec 2 | rec 3 | rec 4 | rec 5 | rec N |
282 * +--------+-------+-------+-------+-------+-------+-------+
284 * +--------+-------+-------+-------+-------+-------+-------+
285 * Non-Leaf: | header | key 1 | key 2 | key N | ptr 1 | ptr 2 | ptr N |
286 * +--------+-------+-------+-------+-------+-------+-------+
288 * The header is called struct xfs_btree_block for reasons better left unknown
289 * and comes in different versions for short (32bit) and long (64bit) block
290 * pointers. The record and key structures are defined by the btree instances
291 * and opaque to the btree core. The block pointers are simple disk endian
292 * integers, available in a short (32bit) and long (64bit) variant.
294 * The helpers below calculate the offset of a given record, key or pointer
295 * into a btree block (xfs_btree_*_offset) or return a pointer to the given
296 * record, key or pointer (xfs_btree_*_addr). Note that all addressing
297 * inside the btree block is done using indices starting at one, not zero!
301 * Return size of the btree block header for this btree instance.
303 static inline size_t xfs_btree_block_len(struct xfs_btree_cur
*cur
)
305 return (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
) ?
306 XFS_BTREE_LBLOCK_LEN
:
307 XFS_BTREE_SBLOCK_LEN
;
311 * Return size of btree block pointers for this btree instance.
313 static inline size_t xfs_btree_ptr_len(struct xfs_btree_cur
*cur
)
315 return (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
) ?
316 sizeof(__be64
) : sizeof(__be32
);
320 * Calculate offset of the n-th record in a btree block.
323 xfs_btree_rec_offset(
324 struct xfs_btree_cur
*cur
,
327 return xfs_btree_block_len(cur
) +
328 (n
- 1) * cur
->bc_ops
->rec_len
;
332 * Calculate offset of the n-th key in a btree block.
335 xfs_btree_key_offset(
336 struct xfs_btree_cur
*cur
,
339 return xfs_btree_block_len(cur
) +
340 (n
- 1) * cur
->bc_ops
->key_len
;
344 * Calculate offset of the n-th block pointer in a btree block.
347 xfs_btree_ptr_offset(
348 struct xfs_btree_cur
*cur
,
352 return xfs_btree_block_len(cur
) +
353 cur
->bc_ops
->get_maxrecs(cur
, level
) * cur
->bc_ops
->key_len
+
354 (n
- 1) * xfs_btree_ptr_len(cur
);
358 * Return a pointer to the n-th record in the btree block.
360 STATIC
union xfs_btree_rec
*
362 struct xfs_btree_cur
*cur
,
364 struct xfs_btree_block
*block
)
366 return (union xfs_btree_rec
*)
367 ((char *)block
+ xfs_btree_rec_offset(cur
, n
));
371 * Return a pointer to the n-th key in the btree block.
373 STATIC
union xfs_btree_key
*
375 struct xfs_btree_cur
*cur
,
377 struct xfs_btree_block
*block
)
379 return (union xfs_btree_key
*)
380 ((char *)block
+ xfs_btree_key_offset(cur
, n
));
384 * Return a pointer to the n-th block pointer in the btree block.
386 STATIC
union xfs_btree_ptr
*
388 struct xfs_btree_cur
*cur
,
390 struct xfs_btree_block
*block
)
392 int level
= xfs_btree_get_level(block
);
394 ASSERT(block
->bb_level
!= 0);
396 return (union xfs_btree_ptr
*)
397 ((char *)block
+ xfs_btree_ptr_offset(cur
, n
, level
));
401 * Get a the root block which is stored in the inode.
403 * For now this btree implementation assumes the btree root is always
404 * stored in the if_broot field of an inode fork.
406 STATIC
struct xfs_btree_block
*
408 struct xfs_btree_cur
*cur
)
410 struct xfs_ifork
*ifp
;
412 ifp
= XFS_IFORK_PTR(cur
->bc_private
.b
.ip
, cur
->bc_private
.b
.whichfork
);
413 return (struct xfs_btree_block
*)ifp
->if_broot
;
417 * Retrieve the block pointer from the cursor at the given level.
418 * This may be an inode btree root or from a buffer.
420 STATIC
struct xfs_btree_block
* /* generic btree block pointer */
422 struct xfs_btree_cur
*cur
, /* btree cursor */
423 int level
, /* level in btree */
424 struct xfs_buf
**bpp
) /* buffer containing the block */
426 if ((cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
) &&
427 (level
== cur
->bc_nlevels
- 1)) {
429 return xfs_btree_get_iroot(cur
);
432 *bpp
= cur
->bc_bufs
[level
];
433 return XFS_BUF_TO_BLOCK(*bpp
);
437 * Get a buffer for the block, return it with no data read.
438 * Long-form addressing.
440 xfs_buf_t
* /* buffer for fsbno */
442 xfs_mount_t
*mp
, /* file system mount point */
443 xfs_trans_t
*tp
, /* transaction pointer */
444 xfs_fsblock_t fsbno
, /* file system block number */
445 uint lock
) /* lock flags for get_buf */
447 xfs_buf_t
*bp
; /* buffer pointer (return value) */
448 xfs_daddr_t d
; /* real disk block address */
450 ASSERT(fsbno
!= NULLFSBLOCK
);
451 d
= XFS_FSB_TO_DADDR(mp
, fsbno
);
452 bp
= xfs_trans_get_buf(tp
, mp
->m_ddev_targp
, d
, mp
->m_bsize
, lock
);
454 ASSERT(!XFS_BUF_GETERROR(bp
));
459 * Get a buffer for the block, return it with no data read.
460 * Short-form addressing.
462 xfs_buf_t
* /* buffer for agno/agbno */
464 xfs_mount_t
*mp
, /* file system mount point */
465 xfs_trans_t
*tp
, /* transaction pointer */
466 xfs_agnumber_t agno
, /* allocation group number */
467 xfs_agblock_t agbno
, /* allocation group block number */
468 uint lock
) /* lock flags for get_buf */
470 xfs_buf_t
*bp
; /* buffer pointer (return value) */
471 xfs_daddr_t d
; /* real disk block address */
473 ASSERT(agno
!= NULLAGNUMBER
);
474 ASSERT(agbno
!= NULLAGBLOCK
);
475 d
= XFS_AGB_TO_DADDR(mp
, agno
, agbno
);
476 bp
= xfs_trans_get_buf(tp
, mp
->m_ddev_targp
, d
, mp
->m_bsize
, lock
);
478 ASSERT(!XFS_BUF_GETERROR(bp
));
483 * Check for the cursor referring to the last block at the given level.
485 int /* 1=is last block, 0=not last block */
486 xfs_btree_islastblock(
487 xfs_btree_cur_t
*cur
, /* btree cursor */
488 int level
) /* level to check */
490 struct xfs_btree_block
*block
; /* generic btree block pointer */
491 xfs_buf_t
*bp
; /* buffer containing block */
493 block
= xfs_btree_get_block(cur
, level
, &bp
);
494 xfs_btree_check_block(cur
, block
, level
, bp
);
495 if (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
)
496 return be64_to_cpu(block
->bb_u
.l
.bb_rightsib
) == NULLDFSBNO
;
498 return be32_to_cpu(block
->bb_u
.s
.bb_rightsib
) == NULLAGBLOCK
;
502 * Change the cursor to point to the first record at the given level.
503 * Other levels are unaffected.
505 STATIC
int /* success=1, failure=0 */
507 xfs_btree_cur_t
*cur
, /* btree cursor */
508 int level
) /* level to change */
510 struct xfs_btree_block
*block
; /* generic btree block pointer */
511 xfs_buf_t
*bp
; /* buffer containing block */
514 * Get the block pointer for this level.
516 block
= xfs_btree_get_block(cur
, level
, &bp
);
517 xfs_btree_check_block(cur
, block
, level
, bp
);
519 * It's empty, there is no such record.
521 if (!block
->bb_numrecs
)
524 * Set the ptr value to 1, that's the first record/key.
526 cur
->bc_ptrs
[level
] = 1;
531 * Change the cursor to point to the last record in the current block
532 * at the given level. Other levels are unaffected.
534 STATIC
int /* success=1, failure=0 */
536 xfs_btree_cur_t
*cur
, /* btree cursor */
537 int level
) /* level to change */
539 struct xfs_btree_block
*block
; /* generic btree block pointer */
540 xfs_buf_t
*bp
; /* buffer containing block */
543 * Get the block pointer for this level.
545 block
= xfs_btree_get_block(cur
, level
, &bp
);
546 xfs_btree_check_block(cur
, block
, level
, bp
);
548 * It's empty, there is no such record.
550 if (!block
->bb_numrecs
)
553 * Set the ptr value to numrecs, that's the last record/key.
555 cur
->bc_ptrs
[level
] = be16_to_cpu(block
->bb_numrecs
);
560 * Compute first and last byte offsets for the fields given.
561 * Interprets the offsets table, which contains struct field offsets.
565 __int64_t fields
, /* bitmask of fields */
566 const short *offsets
, /* table of field offsets */
567 int nbits
, /* number of bits to inspect */
568 int *first
, /* output: first byte offset */
569 int *last
) /* output: last byte offset */
571 int i
; /* current bit number */
572 __int64_t imask
; /* mask for current bit number */
576 * Find the lowest bit, so the first byte offset.
578 for (i
= 0, imask
= 1LL; ; i
++, imask
<<= 1) {
579 if (imask
& fields
) {
585 * Find the highest bit, so the last byte offset.
587 for (i
= nbits
- 1, imask
= 1LL << i
; ; i
--, imask
>>= 1) {
588 if (imask
& fields
) {
589 *last
= offsets
[i
+ 1] - 1;
596 * Get a buffer for the block, return it read in.
597 * Long-form addressing.
601 xfs_mount_t
*mp
, /* file system mount point */
602 xfs_trans_t
*tp
, /* transaction pointer */
603 xfs_fsblock_t fsbno
, /* file system block number */
604 uint lock
, /* lock flags for read_buf */
605 xfs_buf_t
**bpp
, /* buffer for fsbno */
606 int refval
) /* ref count value for buffer */
608 xfs_buf_t
*bp
; /* return value */
609 xfs_daddr_t d
; /* real disk block address */
612 ASSERT(fsbno
!= NULLFSBLOCK
);
613 d
= XFS_FSB_TO_DADDR(mp
, fsbno
);
614 if ((error
= xfs_trans_read_buf(mp
, tp
, mp
->m_ddev_targp
, d
,
615 mp
->m_bsize
, lock
, &bp
))) {
618 ASSERT(!bp
|| !XFS_BUF_GETERROR(bp
));
620 XFS_BUF_SET_VTYPE_REF(bp
, B_FS_MAP
, refval
);
627 * Get a buffer for the block, return it read in.
628 * Short-form addressing.
632 xfs_mount_t
*mp
, /* file system mount point */
633 xfs_trans_t
*tp
, /* transaction pointer */
634 xfs_agnumber_t agno
, /* allocation group number */
635 xfs_agblock_t agbno
, /* allocation group block number */
636 uint lock
, /* lock flags for read_buf */
637 xfs_buf_t
**bpp
, /* buffer for agno/agbno */
638 int refval
) /* ref count value for buffer */
640 xfs_buf_t
*bp
; /* return value */
641 xfs_daddr_t d
; /* real disk block address */
644 ASSERT(agno
!= NULLAGNUMBER
);
645 ASSERT(agbno
!= NULLAGBLOCK
);
646 d
= XFS_AGB_TO_DADDR(mp
, agno
, agbno
);
647 if ((error
= xfs_trans_read_buf(mp
, tp
, mp
->m_ddev_targp
, d
,
648 mp
->m_bsize
, lock
, &bp
))) {
651 ASSERT(!bp
|| !XFS_BUF_GETERROR(bp
));
654 case XFS_ALLOC_BTREE_REF
:
655 XFS_BUF_SET_VTYPE_REF(bp
, B_FS_MAP
, refval
);
657 case XFS_INO_BTREE_REF
:
658 XFS_BUF_SET_VTYPE_REF(bp
, B_FS_INOMAP
, refval
);
667 xfs_btree_readahead_lblock(
668 struct xfs_btree_cur
*cur
,
670 struct xfs_btree_block
*block
)
673 xfs_fsblock_t left
= be64_to_cpu(block
->bb_u
.l
.bb_leftsib
);
674 xfs_fsblock_t right
= be64_to_cpu(block
->bb_u
.l
.bb_rightsib
);
676 if ((lr
& XFS_BTCUR_LEFTRA
) && left
!= NULLDFSBNO
) {
677 xfs_btree_reada_bufl(cur
->bc_mp
, left
, 1);
681 if ((lr
& XFS_BTCUR_RIGHTRA
) && right
!= NULLDFSBNO
) {
682 xfs_btree_reada_bufl(cur
->bc_mp
, right
, 1);
690 xfs_btree_readahead_sblock(
691 struct xfs_btree_cur
*cur
,
693 struct xfs_btree_block
*block
)
696 xfs_agblock_t left
= be32_to_cpu(block
->bb_u
.s
.bb_leftsib
);
697 xfs_agblock_t right
= be32_to_cpu(block
->bb_u
.s
.bb_rightsib
);
700 if ((lr
& XFS_BTCUR_LEFTRA
) && left
!= NULLAGBLOCK
) {
701 xfs_btree_reada_bufs(cur
->bc_mp
, cur
->bc_private
.a
.agno
,
706 if ((lr
& XFS_BTCUR_RIGHTRA
) && right
!= NULLAGBLOCK
) {
707 xfs_btree_reada_bufs(cur
->bc_mp
, cur
->bc_private
.a
.agno
,
716 * Read-ahead btree blocks, at the given level.
717 * Bits in lr are set from XFS_BTCUR_{LEFT,RIGHT}RA.
721 struct xfs_btree_cur
*cur
, /* btree cursor */
722 int lev
, /* level in btree */
723 int lr
) /* left/right bits */
725 struct xfs_btree_block
*block
;
728 * No readahead needed if we are at the root level and the
729 * btree root is stored in the inode.
731 if ((cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
) &&
732 (lev
== cur
->bc_nlevels
- 1))
735 if ((cur
->bc_ra
[lev
] | lr
) == cur
->bc_ra
[lev
])
738 cur
->bc_ra
[lev
] |= lr
;
739 block
= XFS_BUF_TO_BLOCK(cur
->bc_bufs
[lev
]);
741 if (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
)
742 return xfs_btree_readahead_lblock(cur
, lr
, block
);
743 return xfs_btree_readahead_sblock(cur
, lr
, block
);
747 * Set the buffer for level "lev" in the cursor to bp, releasing
748 * any previous buffer.
752 xfs_btree_cur_t
*cur
, /* btree cursor */
753 int lev
, /* level in btree */
754 xfs_buf_t
*bp
) /* new buffer to set */
756 struct xfs_btree_block
*b
; /* btree block */
757 xfs_buf_t
*obp
; /* old buffer pointer */
759 obp
= cur
->bc_bufs
[lev
];
761 xfs_trans_brelse(cur
->bc_tp
, obp
);
762 cur
->bc_bufs
[lev
] = bp
;
766 b
= XFS_BUF_TO_BLOCK(bp
);
767 if (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
) {
768 if (be64_to_cpu(b
->bb_u
.l
.bb_leftsib
) == NULLDFSBNO
)
769 cur
->bc_ra
[lev
] |= XFS_BTCUR_LEFTRA
;
770 if (be64_to_cpu(b
->bb_u
.l
.bb_rightsib
) == NULLDFSBNO
)
771 cur
->bc_ra
[lev
] |= XFS_BTCUR_RIGHTRA
;
773 if (be32_to_cpu(b
->bb_u
.s
.bb_leftsib
) == NULLAGBLOCK
)
774 cur
->bc_ra
[lev
] |= XFS_BTCUR_LEFTRA
;
775 if (be32_to_cpu(b
->bb_u
.s
.bb_rightsib
) == NULLAGBLOCK
)
776 cur
->bc_ra
[lev
] |= XFS_BTCUR_RIGHTRA
;
781 xfs_btree_ptr_is_null(
782 struct xfs_btree_cur
*cur
,
783 union xfs_btree_ptr
*ptr
)
785 if (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
)
786 return be64_to_cpu(ptr
->l
) == NULLFSBLOCK
;
788 return be32_to_cpu(ptr
->s
) == NULLAGBLOCK
;
792 xfs_btree_set_ptr_null(
793 struct xfs_btree_cur
*cur
,
794 union xfs_btree_ptr
*ptr
)
796 if (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
)
797 ptr
->l
= cpu_to_be64(NULLFSBLOCK
);
799 ptr
->s
= cpu_to_be32(NULLAGBLOCK
);
803 * Get/set/init sibling pointers
806 xfs_btree_get_sibling(
807 struct xfs_btree_cur
*cur
,
808 struct xfs_btree_block
*block
,
809 union xfs_btree_ptr
*ptr
,
812 ASSERT(lr
== XFS_BB_LEFTSIB
|| lr
== XFS_BB_RIGHTSIB
);
814 if (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
) {
815 if (lr
== XFS_BB_RIGHTSIB
)
816 ptr
->l
= block
->bb_u
.l
.bb_rightsib
;
818 ptr
->l
= block
->bb_u
.l
.bb_leftsib
;
820 if (lr
== XFS_BB_RIGHTSIB
)
821 ptr
->s
= block
->bb_u
.s
.bb_rightsib
;
823 ptr
->s
= block
->bb_u
.s
.bb_leftsib
;
828 xfs_btree_set_sibling(
829 struct xfs_btree_cur
*cur
,
830 struct xfs_btree_block
*block
,
831 union xfs_btree_ptr
*ptr
,
834 ASSERT(lr
== XFS_BB_LEFTSIB
|| lr
== XFS_BB_RIGHTSIB
);
836 if (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
) {
837 if (lr
== XFS_BB_RIGHTSIB
)
838 block
->bb_u
.l
.bb_rightsib
= ptr
->l
;
840 block
->bb_u
.l
.bb_leftsib
= ptr
->l
;
842 if (lr
== XFS_BB_RIGHTSIB
)
843 block
->bb_u
.s
.bb_rightsib
= ptr
->s
;
845 block
->bb_u
.s
.bb_leftsib
= ptr
->s
;
850 xfs_btree_init_block(
851 struct xfs_btree_cur
*cur
,
854 struct xfs_btree_block
*new) /* new block */
856 new->bb_magic
= cpu_to_be32(xfs_magics
[cur
->bc_btnum
]);
857 new->bb_level
= cpu_to_be16(level
);
858 new->bb_numrecs
= cpu_to_be16(numrecs
);
860 if (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
) {
861 new->bb_u
.l
.bb_leftsib
= cpu_to_be64(NULLFSBLOCK
);
862 new->bb_u
.l
.bb_rightsib
= cpu_to_be64(NULLFSBLOCK
);
864 new->bb_u
.s
.bb_leftsib
= cpu_to_be32(NULLAGBLOCK
);
865 new->bb_u
.s
.bb_rightsib
= cpu_to_be32(NULLAGBLOCK
);
870 * Return true if ptr is the last record in the btree and
871 * we need to track updateѕ to this record. The decision
872 * will be further refined in the update_lastrec method.
875 xfs_btree_is_lastrec(
876 struct xfs_btree_cur
*cur
,
877 struct xfs_btree_block
*block
,
880 union xfs_btree_ptr ptr
;
884 if (!(cur
->bc_flags
& XFS_BTREE_LASTREC_UPDATE
))
887 xfs_btree_get_sibling(cur
, block
, &ptr
, XFS_BB_RIGHTSIB
);
888 if (!xfs_btree_ptr_is_null(cur
, &ptr
))
894 xfs_btree_buf_to_ptr(
895 struct xfs_btree_cur
*cur
,
897 union xfs_btree_ptr
*ptr
)
899 if (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
)
900 ptr
->l
= cpu_to_be64(XFS_DADDR_TO_FSB(cur
->bc_mp
,
903 ptr
->s
= cpu_to_be32(XFS_DADDR_TO_AGBNO(cur
->bc_mp
,
909 xfs_btree_ptr_to_daddr(
910 struct xfs_btree_cur
*cur
,
911 union xfs_btree_ptr
*ptr
)
913 if (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
) {
914 ASSERT(be64_to_cpu(ptr
->l
) != NULLFSBLOCK
);
916 return XFS_FSB_TO_DADDR(cur
->bc_mp
, be64_to_cpu(ptr
->l
));
918 ASSERT(cur
->bc_private
.a
.agno
!= NULLAGNUMBER
);
919 ASSERT(be32_to_cpu(ptr
->s
) != NULLAGBLOCK
);
921 return XFS_AGB_TO_DADDR(cur
->bc_mp
, cur
->bc_private
.a
.agno
,
922 be32_to_cpu(ptr
->s
));
928 struct xfs_btree_cur
*cur
,
931 switch (cur
->bc_btnum
) {
934 XFS_BUF_SET_VTYPE_REF(*bpp
, B_FS_MAP
, XFS_ALLOC_BTREE_REF
);
937 XFS_BUF_SET_VTYPE_REF(*bpp
, B_FS_INOMAP
, XFS_INO_BTREE_REF
);
940 XFS_BUF_SET_VTYPE_REF(*bpp
, B_FS_MAP
, XFS_BMAP_BTREE_REF
);
948 xfs_btree_get_buf_block(
949 struct xfs_btree_cur
*cur
,
950 union xfs_btree_ptr
*ptr
,
952 struct xfs_btree_block
**block
,
953 struct xfs_buf
**bpp
)
955 struct xfs_mount
*mp
= cur
->bc_mp
;
958 /* need to sort out how callers deal with failures first */
959 ASSERT(!(flags
& XFS_BUF_TRYLOCK
));
961 d
= xfs_btree_ptr_to_daddr(cur
, ptr
);
962 *bpp
= xfs_trans_get_buf(cur
->bc_tp
, mp
->m_ddev_targp
, d
,
966 ASSERT(!XFS_BUF_GETERROR(*bpp
));
968 *block
= XFS_BUF_TO_BLOCK(*bpp
);
973 * Read in the buffer at the given ptr and return the buffer and
974 * the block pointer within the buffer.
977 xfs_btree_read_buf_block(
978 struct xfs_btree_cur
*cur
,
979 union xfs_btree_ptr
*ptr
,
982 struct xfs_btree_block
**block
,
983 struct xfs_buf
**bpp
)
985 struct xfs_mount
*mp
= cur
->bc_mp
;
989 /* need to sort out how callers deal with failures first */
990 ASSERT(!(flags
& XFS_BUF_TRYLOCK
));
992 d
= xfs_btree_ptr_to_daddr(cur
, ptr
);
993 error
= xfs_trans_read_buf(mp
, cur
->bc_tp
, mp
->m_ddev_targp
, d
,
994 mp
->m_bsize
, flags
, bpp
);
998 ASSERT(*bpp
!= NULL
);
999 ASSERT(!XFS_BUF_GETERROR(*bpp
));
1001 xfs_btree_set_refs(cur
, *bpp
);
1002 *block
= XFS_BUF_TO_BLOCK(*bpp
);
1004 error
= xfs_btree_check_block(cur
, *block
, level
, *bpp
);
1006 xfs_trans_brelse(cur
->bc_tp
, *bpp
);
1011 * Copy keys from one btree block to another.
1014 xfs_btree_copy_keys(
1015 struct xfs_btree_cur
*cur
,
1016 union xfs_btree_key
*dst_key
,
1017 union xfs_btree_key
*src_key
,
1020 ASSERT(numkeys
>= 0);
1021 memcpy(dst_key
, src_key
, numkeys
* cur
->bc_ops
->key_len
);
1025 * Copy records from one btree block to another.
1028 xfs_btree_copy_recs(
1029 struct xfs_btree_cur
*cur
,
1030 union xfs_btree_rec
*dst_rec
,
1031 union xfs_btree_rec
*src_rec
,
1034 ASSERT(numrecs
>= 0);
1035 memcpy(dst_rec
, src_rec
, numrecs
* cur
->bc_ops
->rec_len
);
1039 * Copy block pointers from one btree block to another.
1042 xfs_btree_copy_ptrs(
1043 struct xfs_btree_cur
*cur
,
1044 union xfs_btree_ptr
*dst_ptr
,
1045 union xfs_btree_ptr
*src_ptr
,
1048 ASSERT(numptrs
>= 0);
1049 memcpy(dst_ptr
, src_ptr
, numptrs
* xfs_btree_ptr_len(cur
));
1053 * Shift keys one index left/right inside a single btree block.
1056 xfs_btree_shift_keys(
1057 struct xfs_btree_cur
*cur
,
1058 union xfs_btree_key
*key
,
1064 ASSERT(numkeys
>= 0);
1065 ASSERT(dir
== 1 || dir
== -1);
1067 dst_key
= (char *)key
+ (dir
* cur
->bc_ops
->key_len
);
1068 memmove(dst_key
, key
, numkeys
* cur
->bc_ops
->key_len
);
1072 * Shift records one index left/right inside a single btree block.
1075 xfs_btree_shift_recs(
1076 struct xfs_btree_cur
*cur
,
1077 union xfs_btree_rec
*rec
,
1083 ASSERT(numrecs
>= 0);
1084 ASSERT(dir
== 1 || dir
== -1);
1086 dst_rec
= (char *)rec
+ (dir
* cur
->bc_ops
->rec_len
);
1087 memmove(dst_rec
, rec
, numrecs
* cur
->bc_ops
->rec_len
);
1091 * Shift block pointers one index left/right inside a single btree block.
1094 xfs_btree_shift_ptrs(
1095 struct xfs_btree_cur
*cur
,
1096 union xfs_btree_ptr
*ptr
,
1102 ASSERT(numptrs
>= 0);
1103 ASSERT(dir
== 1 || dir
== -1);
1105 dst_ptr
= (char *)ptr
+ (dir
* xfs_btree_ptr_len(cur
));
1106 memmove(dst_ptr
, ptr
, numptrs
* xfs_btree_ptr_len(cur
));
1110 * Log key values from the btree block.
1114 struct xfs_btree_cur
*cur
,
1119 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
1120 XFS_BTREE_TRACE_ARGBII(cur
, bp
, first
, last
);
1123 xfs_trans_log_buf(cur
->bc_tp
, bp
,
1124 xfs_btree_key_offset(cur
, first
),
1125 xfs_btree_key_offset(cur
, last
+ 1) - 1);
1127 xfs_trans_log_inode(cur
->bc_tp
, cur
->bc_private
.b
.ip
,
1128 xfs_ilog_fbroot(cur
->bc_private
.b
.whichfork
));
1131 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
1135 * Log record values from the btree block.
1139 struct xfs_btree_cur
*cur
,
1144 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
1145 XFS_BTREE_TRACE_ARGBII(cur
, bp
, first
, last
);
1147 xfs_trans_log_buf(cur
->bc_tp
, bp
,
1148 xfs_btree_rec_offset(cur
, first
),
1149 xfs_btree_rec_offset(cur
, last
+ 1) - 1);
1151 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
1155 * Log block pointer fields from a btree block (nonleaf).
1159 struct xfs_btree_cur
*cur
, /* btree cursor */
1160 struct xfs_buf
*bp
, /* buffer containing btree block */
1161 int first
, /* index of first pointer to log */
1162 int last
) /* index of last pointer to log */
1164 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
1165 XFS_BTREE_TRACE_ARGBII(cur
, bp
, first
, last
);
1168 struct xfs_btree_block
*block
= XFS_BUF_TO_BLOCK(bp
);
1169 int level
= xfs_btree_get_level(block
);
1171 xfs_trans_log_buf(cur
->bc_tp
, bp
,
1172 xfs_btree_ptr_offset(cur
, first
, level
),
1173 xfs_btree_ptr_offset(cur
, last
+ 1, level
) - 1);
1175 xfs_trans_log_inode(cur
->bc_tp
, cur
->bc_private
.b
.ip
,
1176 xfs_ilog_fbroot(cur
->bc_private
.b
.whichfork
));
1179 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
1183 * Log fields from a btree block header.
1186 xfs_btree_log_block(
1187 struct xfs_btree_cur
*cur
, /* btree cursor */
1188 struct xfs_buf
*bp
, /* buffer containing btree block */
1189 int fields
) /* mask of fields: XFS_BB_... */
1191 int first
; /* first byte offset logged */
1192 int last
; /* last byte offset logged */
1193 static const short soffsets
[] = { /* table of offsets (short) */
1194 offsetof(struct xfs_btree_block
, bb_magic
),
1195 offsetof(struct xfs_btree_block
, bb_level
),
1196 offsetof(struct xfs_btree_block
, bb_numrecs
),
1197 offsetof(struct xfs_btree_block
, bb_u
.s
.bb_leftsib
),
1198 offsetof(struct xfs_btree_block
, bb_u
.s
.bb_rightsib
),
1199 XFS_BTREE_SBLOCK_LEN
1201 static const short loffsets
[] = { /* table of offsets (long) */
1202 offsetof(struct xfs_btree_block
, bb_magic
),
1203 offsetof(struct xfs_btree_block
, bb_level
),
1204 offsetof(struct xfs_btree_block
, bb_numrecs
),
1205 offsetof(struct xfs_btree_block
, bb_u
.l
.bb_leftsib
),
1206 offsetof(struct xfs_btree_block
, bb_u
.l
.bb_rightsib
),
1207 XFS_BTREE_LBLOCK_LEN
1210 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
1211 XFS_BTREE_TRACE_ARGBI(cur
, bp
, fields
);
1214 xfs_btree_offsets(fields
,
1215 (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
) ?
1216 loffsets
: soffsets
,
1217 XFS_BB_NUM_BITS
, &first
, &last
);
1218 xfs_trans_log_buf(cur
->bc_tp
, bp
, first
, last
);
1220 xfs_trans_log_inode(cur
->bc_tp
, cur
->bc_private
.b
.ip
,
1221 xfs_ilog_fbroot(cur
->bc_private
.b
.whichfork
));
1224 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
1228 * Increment cursor by one record at the level.
1229 * For nonzero levels the leaf-ward information is untouched.
1232 xfs_btree_increment(
1233 struct xfs_btree_cur
*cur
,
1235 int *stat
) /* success/failure */
1237 struct xfs_btree_block
*block
;
1238 union xfs_btree_ptr ptr
;
1240 int error
; /* error return value */
1243 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
1244 XFS_BTREE_TRACE_ARGI(cur
, level
);
1246 ASSERT(level
< cur
->bc_nlevels
);
1248 /* Read-ahead to the right at this level. */
1249 xfs_btree_readahead(cur
, level
, XFS_BTCUR_RIGHTRA
);
1251 /* Get a pointer to the btree block. */
1252 block
= xfs_btree_get_block(cur
, level
, &bp
);
1255 error
= xfs_btree_check_block(cur
, block
, level
, bp
);
1260 /* We're done if we remain in the block after the increment. */
1261 if (++cur
->bc_ptrs
[level
] <= xfs_btree_get_numrecs(block
))
1264 /* Fail if we just went off the right edge of the tree. */
1265 xfs_btree_get_sibling(cur
, block
, &ptr
, XFS_BB_RIGHTSIB
);
1266 if (xfs_btree_ptr_is_null(cur
, &ptr
))
1269 XFS_BTREE_STATS_INC(cur
, increment
);
1272 * March up the tree incrementing pointers.
1273 * Stop when we don't go off the right edge of a block.
1275 for (lev
= level
+ 1; lev
< cur
->bc_nlevels
; lev
++) {
1276 block
= xfs_btree_get_block(cur
, lev
, &bp
);
1279 error
= xfs_btree_check_block(cur
, block
, lev
, bp
);
1284 if (++cur
->bc_ptrs
[lev
] <= xfs_btree_get_numrecs(block
))
1287 /* Read-ahead the right block for the next loop. */
1288 xfs_btree_readahead(cur
, lev
, XFS_BTCUR_RIGHTRA
);
1292 * If we went off the root then we are either seriously
1293 * confused or have the tree root in an inode.
1295 if (lev
== cur
->bc_nlevels
) {
1296 if (cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
)
1299 error
= EFSCORRUPTED
;
1302 ASSERT(lev
< cur
->bc_nlevels
);
1305 * Now walk back down the tree, fixing up the cursor's buffer
1306 * pointers and key numbers.
1308 for (block
= xfs_btree_get_block(cur
, lev
, &bp
); lev
> level
; ) {
1309 union xfs_btree_ptr
*ptrp
;
1311 ptrp
= xfs_btree_ptr_addr(cur
, cur
->bc_ptrs
[lev
], block
);
1312 error
= xfs_btree_read_buf_block(cur
, ptrp
, --lev
,
1317 xfs_btree_setbuf(cur
, lev
, bp
);
1318 cur
->bc_ptrs
[lev
] = 1;
1321 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
1326 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
1331 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
1336 * Decrement cursor by one record at the level.
1337 * For nonzero levels the leaf-ward information is untouched.
1340 xfs_btree_decrement(
1341 struct xfs_btree_cur
*cur
,
1343 int *stat
) /* success/failure */
1345 struct xfs_btree_block
*block
;
1347 int error
; /* error return value */
1349 union xfs_btree_ptr ptr
;
1351 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
1352 XFS_BTREE_TRACE_ARGI(cur
, level
);
1354 ASSERT(level
< cur
->bc_nlevels
);
1356 /* Read-ahead to the left at this level. */
1357 xfs_btree_readahead(cur
, level
, XFS_BTCUR_LEFTRA
);
1359 /* We're done if we remain in the block after the decrement. */
1360 if (--cur
->bc_ptrs
[level
] > 0)
1363 /* Get a pointer to the btree block. */
1364 block
= xfs_btree_get_block(cur
, level
, &bp
);
1367 error
= xfs_btree_check_block(cur
, block
, level
, bp
);
1372 /* Fail if we just went off the left edge of the tree. */
1373 xfs_btree_get_sibling(cur
, block
, &ptr
, XFS_BB_LEFTSIB
);
1374 if (xfs_btree_ptr_is_null(cur
, &ptr
))
1377 XFS_BTREE_STATS_INC(cur
, decrement
);
1380 * March up the tree decrementing pointers.
1381 * Stop when we don't go off the left edge of a block.
1383 for (lev
= level
+ 1; lev
< cur
->bc_nlevels
; lev
++) {
1384 if (--cur
->bc_ptrs
[lev
] > 0)
1386 /* Read-ahead the left block for the next loop. */
1387 xfs_btree_readahead(cur
, lev
, XFS_BTCUR_LEFTRA
);
1391 * If we went off the root then we are seriously confused.
1392 * or the root of the tree is in an inode.
1394 if (lev
== cur
->bc_nlevels
) {
1395 if (cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
)
1398 error
= EFSCORRUPTED
;
1401 ASSERT(lev
< cur
->bc_nlevels
);
1404 * Now walk back down the tree, fixing up the cursor's buffer
1405 * pointers and key numbers.
1407 for (block
= xfs_btree_get_block(cur
, lev
, &bp
); lev
> level
; ) {
1408 union xfs_btree_ptr
*ptrp
;
1410 ptrp
= xfs_btree_ptr_addr(cur
, cur
->bc_ptrs
[lev
], block
);
1411 error
= xfs_btree_read_buf_block(cur
, ptrp
, --lev
,
1415 xfs_btree_setbuf(cur
, lev
, bp
);
1416 cur
->bc_ptrs
[lev
] = xfs_btree_get_numrecs(block
);
1419 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
1424 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
1429 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
1434 xfs_btree_lookup_get_block(
1435 struct xfs_btree_cur
*cur
, /* btree cursor */
1436 int level
, /* level in the btree */
1437 union xfs_btree_ptr
*pp
, /* ptr to btree block */
1438 struct xfs_btree_block
**blkp
) /* return btree block */
1440 struct xfs_buf
*bp
; /* buffer pointer for btree block */
1443 /* special case the root block if in an inode */
1444 if ((cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
) &&
1445 (level
== cur
->bc_nlevels
- 1)) {
1446 *blkp
= xfs_btree_get_iroot(cur
);
1451 * If the old buffer at this level for the disk address we are
1452 * looking for re-use it.
1454 * Otherwise throw it away and get a new one.
1456 bp
= cur
->bc_bufs
[level
];
1457 if (bp
&& XFS_BUF_ADDR(bp
) == xfs_btree_ptr_to_daddr(cur
, pp
)) {
1458 *blkp
= XFS_BUF_TO_BLOCK(bp
);
1462 error
= xfs_btree_read_buf_block(cur
, pp
, level
, 0, blkp
, &bp
);
1466 xfs_btree_setbuf(cur
, level
, bp
);
1471 * Get current search key. For level 0 we don't actually have a key
1472 * structure so we make one up from the record. For all other levels
1473 * we just return the right key.
1475 STATIC
union xfs_btree_key
*
1476 xfs_lookup_get_search_key(
1477 struct xfs_btree_cur
*cur
,
1480 struct xfs_btree_block
*block
,
1481 union xfs_btree_key
*kp
)
1484 cur
->bc_ops
->init_key_from_rec(kp
,
1485 xfs_btree_rec_addr(cur
, keyno
, block
));
1489 return xfs_btree_key_addr(cur
, keyno
, block
);
1493 * Lookup the record. The cursor is made to point to it, based on dir.
1494 * Return 0 if can't find any such record, 1 for success.
1498 struct xfs_btree_cur
*cur
, /* btree cursor */
1499 xfs_lookup_t dir
, /* <=, ==, or >= */
1500 int *stat
) /* success/failure */
1502 struct xfs_btree_block
*block
; /* current btree block */
1503 __int64_t diff
; /* difference for the current key */
1504 int error
; /* error return value */
1505 int keyno
; /* current key number */
1506 int level
; /* level in the btree */
1507 union xfs_btree_ptr
*pp
; /* ptr to btree block */
1508 union xfs_btree_ptr ptr
; /* ptr to btree block */
1510 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
1511 XFS_BTREE_TRACE_ARGI(cur
, dir
);
1513 XFS_BTREE_STATS_INC(cur
, lookup
);
1518 /* initialise start pointer from cursor */
1519 cur
->bc_ops
->init_ptr_from_cur(cur
, &ptr
);
1523 * Iterate over each level in the btree, starting at the root.
1524 * For each level above the leaves, find the key we need, based
1525 * on the lookup record, then follow the corresponding block
1526 * pointer down to the next level.
1528 for (level
= cur
->bc_nlevels
- 1, diff
= 1; level
>= 0; level
--) {
1529 /* Get the block we need to do the lookup on. */
1530 error
= xfs_btree_lookup_get_block(cur
, level
, pp
, &block
);
1536 * If we already had a key match at a higher level, we
1537 * know we need to use the first entry in this block.
1541 /* Otherwise search this block. Do a binary search. */
1543 int high
; /* high entry number */
1544 int low
; /* low entry number */
1546 /* Set low and high entry numbers, 1-based. */
1548 high
= xfs_btree_get_numrecs(block
);
1550 /* Block is empty, must be an empty leaf. */
1551 ASSERT(level
== 0 && cur
->bc_nlevels
== 1);
1553 cur
->bc_ptrs
[0] = dir
!= XFS_LOOKUP_LE
;
1554 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
1559 /* Binary search the block. */
1560 while (low
<= high
) {
1561 union xfs_btree_key key
;
1562 union xfs_btree_key
*kp
;
1564 XFS_BTREE_STATS_INC(cur
, compare
);
1566 /* keyno is average of low and high. */
1567 keyno
= (low
+ high
) >> 1;
1569 /* Get current search key */
1570 kp
= xfs_lookup_get_search_key(cur
, level
,
1571 keyno
, block
, &key
);
1574 * Compute difference to get next direction:
1575 * - less than, move right
1576 * - greater than, move left
1577 * - equal, we're done
1579 diff
= cur
->bc_ops
->key_diff(cur
, kp
);
1590 * If there are more levels, set up for the next level
1591 * by getting the block number and filling in the cursor.
1595 * If we moved left, need the previous key number,
1596 * unless there isn't one.
1598 if (diff
> 0 && --keyno
< 1)
1600 pp
= xfs_btree_ptr_addr(cur
, keyno
, block
);
1603 error
= xfs_btree_check_ptr(cur
, pp
, 0, level
);
1607 cur
->bc_ptrs
[level
] = keyno
;
1611 /* Done with the search. See if we need to adjust the results. */
1612 if (dir
!= XFS_LOOKUP_LE
&& diff
< 0) {
1615 * If ge search and we went off the end of the block, but it's
1616 * not the last block, we're in the wrong block.
1618 xfs_btree_get_sibling(cur
, block
, &ptr
, XFS_BB_RIGHTSIB
);
1619 if (dir
== XFS_LOOKUP_GE
&&
1620 keyno
> xfs_btree_get_numrecs(block
) &&
1621 !xfs_btree_ptr_is_null(cur
, &ptr
)) {
1624 cur
->bc_ptrs
[0] = keyno
;
1625 error
= xfs_btree_increment(cur
, 0, &i
);
1628 XFS_WANT_CORRUPTED_RETURN(i
== 1);
1629 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
1633 } else if (dir
== XFS_LOOKUP_LE
&& diff
> 0)
1635 cur
->bc_ptrs
[0] = keyno
;
1637 /* Return if we succeeded or not. */
1638 if (keyno
== 0 || keyno
> xfs_btree_get_numrecs(block
))
1640 else if (dir
!= XFS_LOOKUP_EQ
|| diff
== 0)
1644 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
1648 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
1653 * Update keys at all levels from here to the root along the cursor's path.
1657 struct xfs_btree_cur
*cur
,
1658 union xfs_btree_key
*keyp
,
1661 struct xfs_btree_block
*block
;
1663 union xfs_btree_key
*kp
;
1666 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
1667 XFS_BTREE_TRACE_ARGIK(cur
, level
, keyp
);
1669 ASSERT(!(cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
) || level
>= 1);
1672 * Go up the tree from this level toward the root.
1673 * At each level, update the key value to the value input.
1674 * Stop when we reach a level where the cursor isn't pointing
1675 * at the first entry in the block.
1677 for (ptr
= 1; ptr
== 1 && level
< cur
->bc_nlevels
; level
++) {
1681 block
= xfs_btree_get_block(cur
, level
, &bp
);
1683 error
= xfs_btree_check_block(cur
, block
, level
, bp
);
1685 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
1689 ptr
= cur
->bc_ptrs
[level
];
1690 kp
= xfs_btree_key_addr(cur
, ptr
, block
);
1691 xfs_btree_copy_keys(cur
, kp
, keyp
, 1);
1692 xfs_btree_log_keys(cur
, bp
, ptr
, ptr
);
1695 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
1700 * Update the record referred to by cur to the value in the
1701 * given record. This either works (return 0) or gets an
1702 * EFSCORRUPTED error.
1706 struct xfs_btree_cur
*cur
,
1707 union xfs_btree_rec
*rec
)
1709 struct xfs_btree_block
*block
;
1713 union xfs_btree_rec
*rp
;
1715 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
1716 XFS_BTREE_TRACE_ARGR(cur
, rec
);
1718 /* Pick up the current block. */
1719 block
= xfs_btree_get_block(cur
, 0, &bp
);
1722 error
= xfs_btree_check_block(cur
, block
, 0, bp
);
1726 /* Get the address of the rec to be updated. */
1727 ptr
= cur
->bc_ptrs
[0];
1728 rp
= xfs_btree_rec_addr(cur
, ptr
, block
);
1730 /* Fill in the new contents and log them. */
1731 xfs_btree_copy_recs(cur
, rp
, rec
, 1);
1732 xfs_btree_log_recs(cur
, bp
, ptr
, ptr
);
1735 * If we are tracking the last record in the tree and
1736 * we are at the far right edge of the tree, update it.
1738 if (xfs_btree_is_lastrec(cur
, block
, 0)) {
1739 cur
->bc_ops
->update_lastrec(cur
, block
, rec
,
1740 ptr
, LASTREC_UPDATE
);
1743 /* Updating first rec in leaf. Pass new key value up to our parent. */
1745 union xfs_btree_key key
;
1747 cur
->bc_ops
->init_key_from_rec(&key
, rec
);
1748 error
= xfs_btree_updkey(cur
, &key
, 1);
1753 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
1757 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
1762 * Move 1 record left from cur/level if possible.
1763 * Update cur to reflect the new path.
1765 STATIC
int /* error */
1767 struct xfs_btree_cur
*cur
,
1769 int *stat
) /* success/failure */
1771 union xfs_btree_key key
; /* btree key */
1772 struct xfs_buf
*lbp
; /* left buffer pointer */
1773 struct xfs_btree_block
*left
; /* left btree block */
1774 int lrecs
; /* left record count */
1775 struct xfs_buf
*rbp
; /* right buffer pointer */
1776 struct xfs_btree_block
*right
; /* right btree block */
1777 int rrecs
; /* right record count */
1778 union xfs_btree_ptr lptr
; /* left btree pointer */
1779 union xfs_btree_key
*rkp
= NULL
; /* right btree key */
1780 union xfs_btree_ptr
*rpp
= NULL
; /* right address pointer */
1781 union xfs_btree_rec
*rrp
= NULL
; /* right record pointer */
1782 int error
; /* error return value */
1784 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
1785 XFS_BTREE_TRACE_ARGI(cur
, level
);
1787 if ((cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
) &&
1788 level
== cur
->bc_nlevels
- 1)
1791 /* Set up variables for this block as "right". */
1792 right
= xfs_btree_get_block(cur
, level
, &rbp
);
1795 error
= xfs_btree_check_block(cur
, right
, level
, rbp
);
1800 /* If we've got no left sibling then we can't shift an entry left. */
1801 xfs_btree_get_sibling(cur
, right
, &lptr
, XFS_BB_LEFTSIB
);
1802 if (xfs_btree_ptr_is_null(cur
, &lptr
))
1806 * If the cursor entry is the one that would be moved, don't
1807 * do it... it's too complicated.
1809 if (cur
->bc_ptrs
[level
] <= 1)
1812 /* Set up the left neighbor as "left". */
1813 error
= xfs_btree_read_buf_block(cur
, &lptr
, level
, 0, &left
, &lbp
);
1817 /* If it's full, it can't take another entry. */
1818 lrecs
= xfs_btree_get_numrecs(left
);
1819 if (lrecs
== cur
->bc_ops
->get_maxrecs(cur
, level
))
1822 rrecs
= xfs_btree_get_numrecs(right
);
1825 * We add one entry to the left side and remove one for the right side.
1826 * Accout for it here, the changes will be updated on disk and logged
1832 XFS_BTREE_STATS_INC(cur
, lshift
);
1833 XFS_BTREE_STATS_ADD(cur
, moves
, 1);
1836 * If non-leaf, copy a key and a ptr to the left block.
1837 * Log the changes to the left block.
1840 /* It's a non-leaf. Move keys and pointers. */
1841 union xfs_btree_key
*lkp
; /* left btree key */
1842 union xfs_btree_ptr
*lpp
; /* left address pointer */
1844 lkp
= xfs_btree_key_addr(cur
, lrecs
, left
);
1845 rkp
= xfs_btree_key_addr(cur
, 1, right
);
1847 lpp
= xfs_btree_ptr_addr(cur
, lrecs
, left
);
1848 rpp
= xfs_btree_ptr_addr(cur
, 1, right
);
1850 error
= xfs_btree_check_ptr(cur
, rpp
, 0, level
);
1854 xfs_btree_copy_keys(cur
, lkp
, rkp
, 1);
1855 xfs_btree_copy_ptrs(cur
, lpp
, rpp
, 1);
1857 xfs_btree_log_keys(cur
, lbp
, lrecs
, lrecs
);
1858 xfs_btree_log_ptrs(cur
, lbp
, lrecs
, lrecs
);
1860 ASSERT(cur
->bc_ops
->keys_inorder(cur
,
1861 xfs_btree_key_addr(cur
, lrecs
- 1, left
), lkp
));
1863 /* It's a leaf. Move records. */
1864 union xfs_btree_rec
*lrp
; /* left record pointer */
1866 lrp
= xfs_btree_rec_addr(cur
, lrecs
, left
);
1867 rrp
= xfs_btree_rec_addr(cur
, 1, right
);
1869 xfs_btree_copy_recs(cur
, lrp
, rrp
, 1);
1870 xfs_btree_log_recs(cur
, lbp
, lrecs
, lrecs
);
1872 ASSERT(cur
->bc_ops
->recs_inorder(cur
,
1873 xfs_btree_rec_addr(cur
, lrecs
- 1, left
), lrp
));
1876 xfs_btree_set_numrecs(left
, lrecs
);
1877 xfs_btree_log_block(cur
, lbp
, XFS_BB_NUMRECS
);
1879 xfs_btree_set_numrecs(right
, rrecs
);
1880 xfs_btree_log_block(cur
, rbp
, XFS_BB_NUMRECS
);
1883 * Slide the contents of right down one entry.
1885 XFS_BTREE_STATS_ADD(cur
, moves
, rrecs
- 1);
1887 /* It's a nonleaf. operate on keys and ptrs */
1889 int i
; /* loop index */
1891 for (i
= 0; i
< rrecs
; i
++) {
1892 error
= xfs_btree_check_ptr(cur
, rpp
, i
+ 1, level
);
1897 xfs_btree_shift_keys(cur
,
1898 xfs_btree_key_addr(cur
, 2, right
),
1900 xfs_btree_shift_ptrs(cur
,
1901 xfs_btree_ptr_addr(cur
, 2, right
),
1904 xfs_btree_log_keys(cur
, rbp
, 1, rrecs
);
1905 xfs_btree_log_ptrs(cur
, rbp
, 1, rrecs
);
1907 /* It's a leaf. operate on records */
1908 xfs_btree_shift_recs(cur
,
1909 xfs_btree_rec_addr(cur
, 2, right
),
1911 xfs_btree_log_recs(cur
, rbp
, 1, rrecs
);
1914 * If it's the first record in the block, we'll need a key
1915 * structure to pass up to the next level (updkey).
1917 cur
->bc_ops
->init_key_from_rec(&key
,
1918 xfs_btree_rec_addr(cur
, 1, right
));
1922 /* Update the parent key values of right. */
1923 error
= xfs_btree_updkey(cur
, rkp
, level
+ 1);
1927 /* Slide the cursor value left one. */
1928 cur
->bc_ptrs
[level
]--;
1930 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
1935 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
1940 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
1945 * Move 1 record right from cur/level if possible.
1946 * Update cur to reflect the new path.
1948 STATIC
int /* error */
1950 struct xfs_btree_cur
*cur
,
1952 int *stat
) /* success/failure */
1954 union xfs_btree_key key
; /* btree key */
1955 struct xfs_buf
*lbp
; /* left buffer pointer */
1956 struct xfs_btree_block
*left
; /* left btree block */
1957 struct xfs_buf
*rbp
; /* right buffer pointer */
1958 struct xfs_btree_block
*right
; /* right btree block */
1959 struct xfs_btree_cur
*tcur
; /* temporary btree cursor */
1960 union xfs_btree_ptr rptr
; /* right block pointer */
1961 union xfs_btree_key
*rkp
; /* right btree key */
1962 int rrecs
; /* right record count */
1963 int lrecs
; /* left record count */
1964 int error
; /* error return value */
1965 int i
; /* loop counter */
1967 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
1968 XFS_BTREE_TRACE_ARGI(cur
, level
);
1970 if ((cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
) &&
1971 (level
== cur
->bc_nlevels
- 1))
1974 /* Set up variables for this block as "left". */
1975 left
= xfs_btree_get_block(cur
, level
, &lbp
);
1978 error
= xfs_btree_check_block(cur
, left
, level
, lbp
);
1983 /* If we've got no right sibling then we can't shift an entry right. */
1984 xfs_btree_get_sibling(cur
, left
, &rptr
, XFS_BB_RIGHTSIB
);
1985 if (xfs_btree_ptr_is_null(cur
, &rptr
))
1989 * If the cursor entry is the one that would be moved, don't
1990 * do it... it's too complicated.
1992 lrecs
= xfs_btree_get_numrecs(left
);
1993 if (cur
->bc_ptrs
[level
] >= lrecs
)
1996 /* Set up the right neighbor as "right". */
1997 error
= xfs_btree_read_buf_block(cur
, &rptr
, level
, 0, &right
, &rbp
);
2001 /* If it's full, it can't take another entry. */
2002 rrecs
= xfs_btree_get_numrecs(right
);
2003 if (rrecs
== cur
->bc_ops
->get_maxrecs(cur
, level
))
2006 XFS_BTREE_STATS_INC(cur
, rshift
);
2007 XFS_BTREE_STATS_ADD(cur
, moves
, rrecs
);
2010 * Make a hole at the start of the right neighbor block, then
2011 * copy the last left block entry to the hole.
2014 /* It's a nonleaf. make a hole in the keys and ptrs */
2015 union xfs_btree_key
*lkp
;
2016 union xfs_btree_ptr
*lpp
;
2017 union xfs_btree_ptr
*rpp
;
2019 lkp
= xfs_btree_key_addr(cur
, lrecs
, left
);
2020 lpp
= xfs_btree_ptr_addr(cur
, lrecs
, left
);
2021 rkp
= xfs_btree_key_addr(cur
, 1, right
);
2022 rpp
= xfs_btree_ptr_addr(cur
, 1, right
);
2025 for (i
= rrecs
- 1; i
>= 0; i
--) {
2026 error
= xfs_btree_check_ptr(cur
, rpp
, i
, level
);
2032 xfs_btree_shift_keys(cur
, rkp
, 1, rrecs
);
2033 xfs_btree_shift_ptrs(cur
, rpp
, 1, rrecs
);
2036 error
= xfs_btree_check_ptr(cur
, lpp
, 0, level
);
2041 /* Now put the new data in, and log it. */
2042 xfs_btree_copy_keys(cur
, rkp
, lkp
, 1);
2043 xfs_btree_copy_ptrs(cur
, rpp
, lpp
, 1);
2045 xfs_btree_log_keys(cur
, rbp
, 1, rrecs
+ 1);
2046 xfs_btree_log_ptrs(cur
, rbp
, 1, rrecs
+ 1);
2048 ASSERT(cur
->bc_ops
->keys_inorder(cur
, rkp
,
2049 xfs_btree_key_addr(cur
, 2, right
)));
2051 /* It's a leaf. make a hole in the records */
2052 union xfs_btree_rec
*lrp
;
2053 union xfs_btree_rec
*rrp
;
2055 lrp
= xfs_btree_rec_addr(cur
, lrecs
, left
);
2056 rrp
= xfs_btree_rec_addr(cur
, 1, right
);
2058 xfs_btree_shift_recs(cur
, rrp
, 1, rrecs
);
2060 /* Now put the new data in, and log it. */
2061 xfs_btree_copy_recs(cur
, rrp
, lrp
, 1);
2062 xfs_btree_log_recs(cur
, rbp
, 1, rrecs
+ 1);
2064 cur
->bc_ops
->init_key_from_rec(&key
, rrp
);
2067 ASSERT(cur
->bc_ops
->recs_inorder(cur
, rrp
,
2068 xfs_btree_rec_addr(cur
, 2, right
)));
2072 * Decrement and log left's numrecs, bump and log right's numrecs.
2074 xfs_btree_set_numrecs(left
, --lrecs
);
2075 xfs_btree_log_block(cur
, lbp
, XFS_BB_NUMRECS
);
2077 xfs_btree_set_numrecs(right
, ++rrecs
);
2078 xfs_btree_log_block(cur
, rbp
, XFS_BB_NUMRECS
);
2081 * Using a temporary cursor, update the parent key values of the
2082 * block on the right.
2084 error
= xfs_btree_dup_cursor(cur
, &tcur
);
2087 i
= xfs_btree_lastrec(tcur
, level
);
2088 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
2090 error
= xfs_btree_increment(tcur
, level
, &i
);
2094 error
= xfs_btree_updkey(tcur
, rkp
, level
+ 1);
2098 xfs_btree_del_cursor(tcur
, XFS_BTREE_NOERROR
);
2100 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
2105 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
2110 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
2114 XFS_BTREE_TRACE_CURSOR(tcur
, XBT_ERROR
);
2115 xfs_btree_del_cursor(tcur
, XFS_BTREE_ERROR
);
2120 * Split cur/level block in half.
2121 * Return new block number and the key to its first
2122 * record (to be inserted into parent).
2124 STATIC
int /* error */
2126 struct xfs_btree_cur
*cur
,
2128 union xfs_btree_ptr
*ptrp
,
2129 union xfs_btree_key
*key
,
2130 struct xfs_btree_cur
**curp
,
2131 int *stat
) /* success/failure */
2133 union xfs_btree_ptr lptr
; /* left sibling block ptr */
2134 struct xfs_buf
*lbp
; /* left buffer pointer */
2135 struct xfs_btree_block
*left
; /* left btree block */
2136 union xfs_btree_ptr rptr
; /* right sibling block ptr */
2137 struct xfs_buf
*rbp
; /* right buffer pointer */
2138 struct xfs_btree_block
*right
; /* right btree block */
2139 union xfs_btree_ptr rrptr
; /* right-right sibling ptr */
2140 struct xfs_buf
*rrbp
; /* right-right buffer pointer */
2141 struct xfs_btree_block
*rrblock
; /* right-right btree block */
2145 int error
; /* error return value */
2150 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
2151 XFS_BTREE_TRACE_ARGIPK(cur
, level
, *ptrp
, key
);
2153 XFS_BTREE_STATS_INC(cur
, split
);
2155 /* Set up left block (current one). */
2156 left
= xfs_btree_get_block(cur
, level
, &lbp
);
2159 error
= xfs_btree_check_block(cur
, left
, level
, lbp
);
2164 xfs_btree_buf_to_ptr(cur
, lbp
, &lptr
);
2166 /* Allocate the new block. If we can't do it, we're toast. Give up. */
2167 error
= cur
->bc_ops
->alloc_block(cur
, &lptr
, &rptr
, 1, stat
);
2172 XFS_BTREE_STATS_INC(cur
, alloc
);
2174 /* Set up the new block as "right". */
2175 error
= xfs_btree_get_buf_block(cur
, &rptr
, 0, &right
, &rbp
);
2179 /* Fill in the btree header for the new right block. */
2180 xfs_btree_init_block(cur
, xfs_btree_get_level(left
), 0, right
);
2183 * Split the entries between the old and the new block evenly.
2184 * Make sure that if there's an odd number of entries now, that
2185 * each new block will have the same number of entries.
2187 lrecs
= xfs_btree_get_numrecs(left
);
2189 if ((lrecs
& 1) && cur
->bc_ptrs
[level
] <= rrecs
+ 1)
2191 src_index
= (lrecs
- rrecs
+ 1);
2193 XFS_BTREE_STATS_ADD(cur
, moves
, rrecs
);
2196 * Copy btree block entries from the left block over to the
2197 * new block, the right. Update the right block and log the
2201 /* It's a non-leaf. Move keys and pointers. */
2202 union xfs_btree_key
*lkp
; /* left btree key */
2203 union xfs_btree_ptr
*lpp
; /* left address pointer */
2204 union xfs_btree_key
*rkp
; /* right btree key */
2205 union xfs_btree_ptr
*rpp
; /* right address pointer */
2207 lkp
= xfs_btree_key_addr(cur
, src_index
, left
);
2208 lpp
= xfs_btree_ptr_addr(cur
, src_index
, left
);
2209 rkp
= xfs_btree_key_addr(cur
, 1, right
);
2210 rpp
= xfs_btree_ptr_addr(cur
, 1, right
);
2213 for (i
= src_index
; i
< rrecs
; i
++) {
2214 error
= xfs_btree_check_ptr(cur
, lpp
, i
, level
);
2220 xfs_btree_copy_keys(cur
, rkp
, lkp
, rrecs
);
2221 xfs_btree_copy_ptrs(cur
, rpp
, lpp
, rrecs
);
2223 xfs_btree_log_keys(cur
, rbp
, 1, rrecs
);
2224 xfs_btree_log_ptrs(cur
, rbp
, 1, rrecs
);
2226 /* Grab the keys to the entries moved to the right block */
2227 xfs_btree_copy_keys(cur
, key
, rkp
, 1);
2229 /* It's a leaf. Move records. */
2230 union xfs_btree_rec
*lrp
; /* left record pointer */
2231 union xfs_btree_rec
*rrp
; /* right record pointer */
2233 lrp
= xfs_btree_rec_addr(cur
, src_index
, left
);
2234 rrp
= xfs_btree_rec_addr(cur
, 1, right
);
2236 xfs_btree_copy_recs(cur
, rrp
, lrp
, rrecs
);
2237 xfs_btree_log_recs(cur
, rbp
, 1, rrecs
);
2239 cur
->bc_ops
->init_key_from_rec(key
,
2240 xfs_btree_rec_addr(cur
, 1, right
));
2245 * Find the left block number by looking in the buffer.
2246 * Adjust numrecs, sibling pointers.
2248 xfs_btree_get_sibling(cur
, left
, &rrptr
, XFS_BB_RIGHTSIB
);
2249 xfs_btree_set_sibling(cur
, right
, &rrptr
, XFS_BB_RIGHTSIB
);
2250 xfs_btree_set_sibling(cur
, right
, &lptr
, XFS_BB_LEFTSIB
);
2251 xfs_btree_set_sibling(cur
, left
, &rptr
, XFS_BB_RIGHTSIB
);
2254 xfs_btree_set_numrecs(left
, lrecs
);
2255 xfs_btree_set_numrecs(right
, xfs_btree_get_numrecs(right
) + rrecs
);
2257 xfs_btree_log_block(cur
, rbp
, XFS_BB_ALL_BITS
);
2258 xfs_btree_log_block(cur
, lbp
, XFS_BB_NUMRECS
| XFS_BB_RIGHTSIB
);
2261 * If there's a block to the new block's right, make that block
2262 * point back to right instead of to left.
2264 if (!xfs_btree_ptr_is_null(cur
, &rrptr
)) {
2265 error
= xfs_btree_read_buf_block(cur
, &rrptr
, level
,
2266 0, &rrblock
, &rrbp
);
2269 xfs_btree_set_sibling(cur
, rrblock
, &rptr
, XFS_BB_LEFTSIB
);
2270 xfs_btree_log_block(cur
, rrbp
, XFS_BB_LEFTSIB
);
2273 * If the cursor is really in the right block, move it there.
2274 * If it's just pointing past the last entry in left, then we'll
2275 * insert there, so don't change anything in that case.
2277 if (cur
->bc_ptrs
[level
] > lrecs
+ 1) {
2278 xfs_btree_setbuf(cur
, level
, rbp
);
2279 cur
->bc_ptrs
[level
] -= lrecs
;
2282 * If there are more levels, we'll need another cursor which refers
2283 * the right block, no matter where this cursor was.
2285 if (level
+ 1 < cur
->bc_nlevels
) {
2286 error
= xfs_btree_dup_cursor(cur
, curp
);
2289 (*curp
)->bc_ptrs
[level
+ 1]++;
2292 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
2296 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
2301 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
2306 * Copy the old inode root contents into a real block and make the
2307 * broot point to it.
2310 xfs_btree_new_iroot(
2311 struct xfs_btree_cur
*cur
, /* btree cursor */
2312 int *logflags
, /* logging flags for inode */
2313 int *stat
) /* return status - 0 fail */
2315 struct xfs_buf
*cbp
; /* buffer for cblock */
2316 struct xfs_btree_block
*block
; /* btree block */
2317 struct xfs_btree_block
*cblock
; /* child btree block */
2318 union xfs_btree_key
*ckp
; /* child key pointer */
2319 union xfs_btree_ptr
*cpp
; /* child ptr pointer */
2320 union xfs_btree_key
*kp
; /* pointer to btree key */
2321 union xfs_btree_ptr
*pp
; /* pointer to block addr */
2322 union xfs_btree_ptr nptr
; /* new block addr */
2323 int level
; /* btree level */
2324 int error
; /* error return code */
2326 int i
; /* loop counter */
2329 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
2330 XFS_BTREE_STATS_INC(cur
, newroot
);
2332 ASSERT(cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
);
2334 level
= cur
->bc_nlevels
- 1;
2336 block
= xfs_btree_get_iroot(cur
);
2337 pp
= xfs_btree_ptr_addr(cur
, 1, block
);
2339 /* Allocate the new block. If we can't do it, we're toast. Give up. */
2340 error
= cur
->bc_ops
->alloc_block(cur
, pp
, &nptr
, 1, stat
);
2344 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
2347 XFS_BTREE_STATS_INC(cur
, alloc
);
2349 /* Copy the root into a real block. */
2350 error
= xfs_btree_get_buf_block(cur
, &nptr
, 0, &cblock
, &cbp
);
2354 memcpy(cblock
, block
, xfs_btree_block_len(cur
));
2356 be16_add_cpu(&block
->bb_level
, 1);
2357 xfs_btree_set_numrecs(block
, 1);
2359 cur
->bc_ptrs
[level
+ 1] = 1;
2361 kp
= xfs_btree_key_addr(cur
, 1, block
);
2362 ckp
= xfs_btree_key_addr(cur
, 1, cblock
);
2363 xfs_btree_copy_keys(cur
, ckp
, kp
, xfs_btree_get_numrecs(cblock
));
2365 cpp
= xfs_btree_ptr_addr(cur
, 1, cblock
);
2367 for (i
= 0; i
< be16_to_cpu(cblock
->bb_numrecs
); i
++) {
2368 error
= xfs_btree_check_ptr(cur
, pp
, i
, level
);
2373 xfs_btree_copy_ptrs(cur
, cpp
, pp
, xfs_btree_get_numrecs(cblock
));
2376 error
= xfs_btree_check_ptr(cur
, &nptr
, 0, level
);
2380 xfs_btree_copy_ptrs(cur
, pp
, &nptr
, 1);
2382 xfs_iroot_realloc(cur
->bc_private
.b
.ip
,
2383 1 - xfs_btree_get_numrecs(cblock
),
2384 cur
->bc_private
.b
.whichfork
);
2386 xfs_btree_setbuf(cur
, level
, cbp
);
2389 * Do all this logging at the end so that
2390 * the root is at the right level.
2392 xfs_btree_log_block(cur
, cbp
, XFS_BB_ALL_BITS
);
2393 xfs_btree_log_keys(cur
, cbp
, 1, be16_to_cpu(cblock
->bb_numrecs
));
2394 xfs_btree_log_ptrs(cur
, cbp
, 1, be16_to_cpu(cblock
->bb_numrecs
));
2397 XFS_ILOG_CORE
| XFS_ILOG_FBROOT(cur
->bc_private
.b
.whichfork
);
2399 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
2402 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
2407 * Allocate a new root block, fill it in.
2409 STATIC
int /* error */
2411 struct xfs_btree_cur
*cur
, /* btree cursor */
2412 int *stat
) /* success/failure */
2414 struct xfs_btree_block
*block
; /* one half of the old root block */
2415 struct xfs_buf
*bp
; /* buffer containing block */
2416 int error
; /* error return value */
2417 struct xfs_buf
*lbp
; /* left buffer pointer */
2418 struct xfs_btree_block
*left
; /* left btree block */
2419 struct xfs_buf
*nbp
; /* new (root) buffer */
2420 struct xfs_btree_block
*new; /* new (root) btree block */
2421 int nptr
; /* new value for key index, 1 or 2 */
2422 struct xfs_buf
*rbp
; /* right buffer pointer */
2423 struct xfs_btree_block
*right
; /* right btree block */
2424 union xfs_btree_ptr rptr
;
2425 union xfs_btree_ptr lptr
;
2427 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
2428 XFS_BTREE_STATS_INC(cur
, newroot
);
2430 /* initialise our start point from the cursor */
2431 cur
->bc_ops
->init_ptr_from_cur(cur
, &rptr
);
2433 /* Allocate the new block. If we can't do it, we're toast. Give up. */
2434 error
= cur
->bc_ops
->alloc_block(cur
, &rptr
, &lptr
, 1, stat
);
2439 XFS_BTREE_STATS_INC(cur
, alloc
);
2441 /* Set up the new block. */
2442 error
= xfs_btree_get_buf_block(cur
, &lptr
, 0, &new, &nbp
);
2446 /* Set the root in the holding structure increasing the level by 1. */
2447 cur
->bc_ops
->set_root(cur
, &lptr
, 1);
2450 * At the previous root level there are now two blocks: the old root,
2451 * and the new block generated when it was split. We don't know which
2452 * one the cursor is pointing at, so we set up variables "left" and
2453 * "right" for each case.
2455 block
= xfs_btree_get_block(cur
, cur
->bc_nlevels
- 1, &bp
);
2458 error
= xfs_btree_check_block(cur
, block
, cur
->bc_nlevels
- 1, bp
);
2463 xfs_btree_get_sibling(cur
, block
, &rptr
, XFS_BB_RIGHTSIB
);
2464 if (!xfs_btree_ptr_is_null(cur
, &rptr
)) {
2465 /* Our block is left, pick up the right block. */
2467 xfs_btree_buf_to_ptr(cur
, lbp
, &lptr
);
2469 error
= xfs_btree_read_buf_block(cur
, &rptr
,
2470 cur
->bc_nlevels
- 1, 0, &right
, &rbp
);
2476 /* Our block is right, pick up the left block. */
2478 xfs_btree_buf_to_ptr(cur
, rbp
, &rptr
);
2480 xfs_btree_get_sibling(cur
, right
, &lptr
, XFS_BB_LEFTSIB
);
2481 error
= xfs_btree_read_buf_block(cur
, &lptr
,
2482 cur
->bc_nlevels
- 1, 0, &left
, &lbp
);
2488 /* Fill in the new block's btree header and log it. */
2489 xfs_btree_init_block(cur
, cur
->bc_nlevels
, 2, new);
2490 xfs_btree_log_block(cur
, nbp
, XFS_BB_ALL_BITS
);
2491 ASSERT(!xfs_btree_ptr_is_null(cur
, &lptr
) &&
2492 !xfs_btree_ptr_is_null(cur
, &rptr
));
2494 /* Fill in the key data in the new root. */
2495 if (xfs_btree_get_level(left
) > 0) {
2496 xfs_btree_copy_keys(cur
,
2497 xfs_btree_key_addr(cur
, 1, new),
2498 xfs_btree_key_addr(cur
, 1, left
), 1);
2499 xfs_btree_copy_keys(cur
,
2500 xfs_btree_key_addr(cur
, 2, new),
2501 xfs_btree_key_addr(cur
, 1, right
), 1);
2503 cur
->bc_ops
->init_key_from_rec(
2504 xfs_btree_key_addr(cur
, 1, new),
2505 xfs_btree_rec_addr(cur
, 1, left
));
2506 cur
->bc_ops
->init_key_from_rec(
2507 xfs_btree_key_addr(cur
, 2, new),
2508 xfs_btree_rec_addr(cur
, 1, right
));
2510 xfs_btree_log_keys(cur
, nbp
, 1, 2);
2512 /* Fill in the pointer data in the new root. */
2513 xfs_btree_copy_ptrs(cur
,
2514 xfs_btree_ptr_addr(cur
, 1, new), &lptr
, 1);
2515 xfs_btree_copy_ptrs(cur
,
2516 xfs_btree_ptr_addr(cur
, 2, new), &rptr
, 1);
2517 xfs_btree_log_ptrs(cur
, nbp
, 1, 2);
2519 /* Fix up the cursor. */
2520 xfs_btree_setbuf(cur
, cur
->bc_nlevels
, nbp
);
2521 cur
->bc_ptrs
[cur
->bc_nlevels
] = nptr
;
2523 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
2527 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
2530 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
2536 xfs_btree_make_block_unfull(
2537 struct xfs_btree_cur
*cur
, /* btree cursor */
2538 int level
, /* btree level */
2539 int numrecs
,/* # of recs in block */
2540 int *oindex
,/* old tree index */
2541 int *index
, /* new tree index */
2542 union xfs_btree_ptr
*nptr
, /* new btree ptr */
2543 struct xfs_btree_cur
**ncur
, /* new btree cursor */
2544 union xfs_btree_rec
*nrec
, /* new record */
2547 union xfs_btree_key key
; /* new btree key value */
2550 if ((cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
) &&
2551 level
== cur
->bc_nlevels
- 1) {
2552 struct xfs_inode
*ip
= cur
->bc_private
.b
.ip
;
2554 if (numrecs
< cur
->bc_ops
->get_dmaxrecs(cur
, level
)) {
2555 /* A root block that can be made bigger. */
2557 xfs_iroot_realloc(ip
, 1, cur
->bc_private
.b
.whichfork
);
2559 /* A root block that needs replacing */
2562 error
= xfs_btree_new_iroot(cur
, &logflags
, stat
);
2563 if (error
|| *stat
== 0)
2566 xfs_trans_log_inode(cur
->bc_tp
, ip
, logflags
);
2572 /* First, try shifting an entry to the right neighbor. */
2573 error
= xfs_btree_rshift(cur
, level
, stat
);
2577 /* Next, try shifting an entry to the left neighbor. */
2578 error
= xfs_btree_lshift(cur
, level
, stat
);
2583 *oindex
= *index
= cur
->bc_ptrs
[level
];
2588 * Next, try splitting the current block in half.
2590 * If this works we have to re-set our variables because we
2591 * could be in a different block now.
2593 error
= xfs_btree_split(cur
, level
, nptr
, &key
, ncur
, stat
);
2594 if (error
|| *stat
== 0)
2598 *index
= cur
->bc_ptrs
[level
];
2599 cur
->bc_ops
->init_rec_from_key(&key
, nrec
);
2604 * Insert one record/level. Return information to the caller
2605 * allowing the next level up to proceed if necessary.
2609 struct xfs_btree_cur
*cur
, /* btree cursor */
2610 int level
, /* level to insert record at */
2611 union xfs_btree_ptr
*ptrp
, /* i/o: block number inserted */
2612 union xfs_btree_rec
*recp
, /* i/o: record data inserted */
2613 struct xfs_btree_cur
**curp
, /* output: new cursor replacing cur */
2614 int *stat
) /* success/failure */
2616 struct xfs_btree_block
*block
; /* btree block */
2617 struct xfs_buf
*bp
; /* buffer for block */
2618 union xfs_btree_key key
; /* btree key */
2619 union xfs_btree_ptr nptr
; /* new block ptr */
2620 struct xfs_btree_cur
*ncur
; /* new btree cursor */
2621 union xfs_btree_rec nrec
; /* new record count */
2622 int optr
; /* old key/record index */
2623 int ptr
; /* key/record index */
2624 int numrecs
;/* number of records */
2625 int error
; /* error return value */
2630 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
2631 XFS_BTREE_TRACE_ARGIPR(cur
, level
, *ptrp
, recp
);
2636 * If we have an external root pointer, and we've made it to the
2637 * root level, allocate a new root block and we're done.
2639 if (!(cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
) &&
2640 (level
>= cur
->bc_nlevels
)) {
2641 error
= xfs_btree_new_root(cur
, stat
);
2642 xfs_btree_set_ptr_null(cur
, ptrp
);
2644 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
2648 /* If we're off the left edge, return failure. */
2649 ptr
= cur
->bc_ptrs
[level
];
2651 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
2656 /* Make a key out of the record data to be inserted, and save it. */
2657 cur
->bc_ops
->init_key_from_rec(&key
, recp
);
2661 XFS_BTREE_STATS_INC(cur
, insrec
);
2663 /* Get pointers to the btree buffer and block. */
2664 block
= xfs_btree_get_block(cur
, level
, &bp
);
2665 numrecs
= xfs_btree_get_numrecs(block
);
2668 error
= xfs_btree_check_block(cur
, block
, level
, bp
);
2672 /* Check that the new entry is being inserted in the right place. */
2673 if (ptr
<= numrecs
) {
2675 ASSERT(cur
->bc_ops
->recs_inorder(cur
, recp
,
2676 xfs_btree_rec_addr(cur
, ptr
, block
)));
2678 ASSERT(cur
->bc_ops
->keys_inorder(cur
, &key
,
2679 xfs_btree_key_addr(cur
, ptr
, block
)));
2685 * If the block is full, we can't insert the new entry until we
2686 * make the block un-full.
2688 xfs_btree_set_ptr_null(cur
, &nptr
);
2689 if (numrecs
== cur
->bc_ops
->get_maxrecs(cur
, level
)) {
2690 error
= xfs_btree_make_block_unfull(cur
, level
, numrecs
,
2691 &optr
, &ptr
, &nptr
, &ncur
, &nrec
, stat
);
2692 if (error
|| *stat
== 0)
2697 * The current block may have changed if the block was
2698 * previously full and we have just made space in it.
2700 block
= xfs_btree_get_block(cur
, level
, &bp
);
2701 numrecs
= xfs_btree_get_numrecs(block
);
2704 error
= xfs_btree_check_block(cur
, block
, level
, bp
);
2710 * At this point we know there's room for our new entry in the block
2711 * we're pointing at.
2713 XFS_BTREE_STATS_ADD(cur
, moves
, numrecs
- ptr
+ 1);
2716 /* It's a nonleaf. make a hole in the keys and ptrs */
2717 union xfs_btree_key
*kp
;
2718 union xfs_btree_ptr
*pp
;
2720 kp
= xfs_btree_key_addr(cur
, ptr
, block
);
2721 pp
= xfs_btree_ptr_addr(cur
, ptr
, block
);
2724 for (i
= numrecs
- ptr
; i
>= 0; i
--) {
2725 error
= xfs_btree_check_ptr(cur
, pp
, i
, level
);
2731 xfs_btree_shift_keys(cur
, kp
, 1, numrecs
- ptr
+ 1);
2732 xfs_btree_shift_ptrs(cur
, pp
, 1, numrecs
- ptr
+ 1);
2735 error
= xfs_btree_check_ptr(cur
, ptrp
, 0, level
);
2740 /* Now put the new data in, bump numrecs and log it. */
2741 xfs_btree_copy_keys(cur
, kp
, &key
, 1);
2742 xfs_btree_copy_ptrs(cur
, pp
, ptrp
, 1);
2744 xfs_btree_set_numrecs(block
, numrecs
);
2745 xfs_btree_log_ptrs(cur
, bp
, ptr
, numrecs
);
2746 xfs_btree_log_keys(cur
, bp
, ptr
, numrecs
);
2748 if (ptr
< numrecs
) {
2749 ASSERT(cur
->bc_ops
->keys_inorder(cur
, kp
,
2750 xfs_btree_key_addr(cur
, ptr
+ 1, block
)));
2754 /* It's a leaf. make a hole in the records */
2755 union xfs_btree_rec
*rp
;
2757 rp
= xfs_btree_rec_addr(cur
, ptr
, block
);
2759 xfs_btree_shift_recs(cur
, rp
, 1, numrecs
- ptr
+ 1);
2761 /* Now put the new data in, bump numrecs and log it. */
2762 xfs_btree_copy_recs(cur
, rp
, recp
, 1);
2763 xfs_btree_set_numrecs(block
, ++numrecs
);
2764 xfs_btree_log_recs(cur
, bp
, ptr
, numrecs
);
2766 if (ptr
< numrecs
) {
2767 ASSERT(cur
->bc_ops
->recs_inorder(cur
, rp
,
2768 xfs_btree_rec_addr(cur
, ptr
+ 1, block
)));
2773 /* Log the new number of records in the btree header. */
2774 xfs_btree_log_block(cur
, bp
, XFS_BB_NUMRECS
);
2776 /* If we inserted at the start of a block, update the parents' keys. */
2778 error
= xfs_btree_updkey(cur
, &key
, level
+ 1);
2784 * If we are tracking the last record in the tree and
2785 * we are at the far right edge of the tree, update it.
2787 if (xfs_btree_is_lastrec(cur
, block
, level
)) {
2788 cur
->bc_ops
->update_lastrec(cur
, block
, recp
,
2789 ptr
, LASTREC_INSREC
);
2793 * Return the new block number, if any.
2794 * If there is one, give back a record value and a cursor too.
2797 if (!xfs_btree_ptr_is_null(cur
, &nptr
)) {
2802 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
2807 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
2812 * Insert the record at the point referenced by cur.
2814 * A multi-level split of the tree on insert will invalidate the original
2815 * cursor. All callers of this function should assume that the cursor is
2816 * no longer valid and revalidate it.
2820 struct xfs_btree_cur
*cur
,
2823 int error
; /* error return value */
2824 int i
; /* result value, 0 for failure */
2825 int level
; /* current level number in btree */
2826 union xfs_btree_ptr nptr
; /* new block number (split result) */
2827 struct xfs_btree_cur
*ncur
; /* new cursor (split result) */
2828 struct xfs_btree_cur
*pcur
; /* previous level's cursor */
2829 union xfs_btree_rec rec
; /* record to insert */
2835 xfs_btree_set_ptr_null(cur
, &nptr
);
2836 cur
->bc_ops
->init_rec_from_cur(cur
, &rec
);
2839 * Loop going up the tree, starting at the leaf level.
2840 * Stop when we don't get a split block, that must mean that
2841 * the insert is finished with this level.
2845 * Insert nrec/nptr into this level of the tree.
2846 * Note if we fail, nptr will be null.
2848 error
= xfs_btree_insrec(pcur
, level
, &nptr
, &rec
, &ncur
, &i
);
2851 xfs_btree_del_cursor(pcur
, XFS_BTREE_ERROR
);
2855 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
2859 * See if the cursor we just used is trash.
2860 * Can't trash the caller's cursor, but otherwise we should
2861 * if ncur is a new cursor or we're about to be done.
2864 (ncur
|| xfs_btree_ptr_is_null(cur
, &nptr
))) {
2865 /* Save the state from the cursor before we trash it */
2866 if (cur
->bc_ops
->update_cursor
)
2867 cur
->bc_ops
->update_cursor(pcur
, cur
);
2868 cur
->bc_nlevels
= pcur
->bc_nlevels
;
2869 xfs_btree_del_cursor(pcur
, XFS_BTREE_NOERROR
);
2871 /* If we got a new cursor, switch to it. */
2876 } while (!xfs_btree_ptr_is_null(cur
, &nptr
));
2878 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
2882 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
2887 * Try to merge a non-leaf block back into the inode root.
2889 * Note: the killroot names comes from the fact that we're effectively
2890 * killing the old root block. But because we can't just delete the
2891 * inode we have to copy the single block it was pointing to into the
2895 xfs_btree_kill_iroot(
2896 struct xfs_btree_cur
*cur
)
2898 int whichfork
= cur
->bc_private
.b
.whichfork
;
2899 struct xfs_inode
*ip
= cur
->bc_private
.b
.ip
;
2900 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
2901 struct xfs_btree_block
*block
;
2902 struct xfs_btree_block
*cblock
;
2903 union xfs_btree_key
*kp
;
2904 union xfs_btree_key
*ckp
;
2905 union xfs_btree_ptr
*pp
;
2906 union xfs_btree_ptr
*cpp
;
2907 struct xfs_buf
*cbp
;
2912 union xfs_btree_ptr ptr
;
2916 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
2918 ASSERT(cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
);
2919 ASSERT(cur
->bc_nlevels
> 1);
2922 * Don't deal with the root block needs to be a leaf case.
2923 * We're just going to turn the thing back into extents anyway.
2925 level
= cur
->bc_nlevels
- 1;
2930 * Give up if the root has multiple children.
2932 block
= xfs_btree_get_iroot(cur
);
2933 if (xfs_btree_get_numrecs(block
) != 1)
2936 cblock
= xfs_btree_get_block(cur
, level
- 1, &cbp
);
2937 numrecs
= xfs_btree_get_numrecs(cblock
);
2940 * Only do this if the next level will fit.
2941 * Then the data must be copied up to the inode,
2942 * instead of freeing the root you free the next level.
2944 if (numrecs
> cur
->bc_ops
->get_dmaxrecs(cur
, level
))
2947 XFS_BTREE_STATS_INC(cur
, killroot
);
2950 xfs_btree_get_sibling(cur
, block
, &ptr
, XFS_BB_LEFTSIB
);
2951 ASSERT(xfs_btree_ptr_is_null(cur
, &ptr
));
2952 xfs_btree_get_sibling(cur
, block
, &ptr
, XFS_BB_RIGHTSIB
);
2953 ASSERT(xfs_btree_ptr_is_null(cur
, &ptr
));
2956 index
= numrecs
- cur
->bc_ops
->get_maxrecs(cur
, level
);
2958 xfs_iroot_realloc(cur
->bc_private
.b
.ip
, index
,
2959 cur
->bc_private
.b
.whichfork
);
2960 block
= ifp
->if_broot
;
2963 be16_add_cpu(&block
->bb_numrecs
, index
);
2964 ASSERT(block
->bb_numrecs
== cblock
->bb_numrecs
);
2966 kp
= xfs_btree_key_addr(cur
, 1, block
);
2967 ckp
= xfs_btree_key_addr(cur
, 1, cblock
);
2968 xfs_btree_copy_keys(cur
, kp
, ckp
, numrecs
);
2970 pp
= xfs_btree_ptr_addr(cur
, 1, block
);
2971 cpp
= xfs_btree_ptr_addr(cur
, 1, cblock
);
2973 for (i
= 0; i
< numrecs
; i
++) {
2976 error
= xfs_btree_check_ptr(cur
, cpp
, i
, level
- 1);
2978 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
2983 xfs_btree_copy_ptrs(cur
, pp
, cpp
, numrecs
);
2985 cur
->bc_ops
->free_block(cur
, cbp
);
2986 XFS_BTREE_STATS_INC(cur
, free
);
2988 cur
->bc_bufs
[level
- 1] = NULL
;
2989 be16_add_cpu(&block
->bb_level
, -1);
2990 xfs_trans_log_inode(cur
->bc_tp
, ip
,
2991 XFS_ILOG_CORE
| XFS_ILOG_FBROOT(cur
->bc_private
.b
.whichfork
));
2994 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
2999 xfs_btree_dec_cursor(
3000 struct xfs_btree_cur
*cur
,
3008 error
= xfs_btree_decrement(cur
, level
, &i
);
3013 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
3019 * Single level of the btree record deletion routine.
3020 * Delete record pointed to by cur/level.
3021 * Remove the record from its block then rebalance the tree.
3022 * Return 0 for error, 1 for done, 2 to go on to the next level.
3024 STATIC
int /* error */
3026 struct xfs_btree_cur
*cur
, /* btree cursor */
3027 int level
, /* level removing record from */
3028 int *stat
) /* fail/done/go-on */
3030 struct xfs_btree_block
*block
; /* btree block */
3031 union xfs_btree_ptr cptr
; /* current block ptr */
3032 struct xfs_buf
*bp
; /* buffer for block */
3033 int error
; /* error return value */
3034 int i
; /* loop counter */
3035 union xfs_btree_key key
; /* storage for keyp */
3036 union xfs_btree_key
*keyp
= &key
; /* passed to the next level */
3037 union xfs_btree_ptr lptr
; /* left sibling block ptr */
3038 struct xfs_buf
*lbp
; /* left buffer pointer */
3039 struct xfs_btree_block
*left
; /* left btree block */
3040 int lrecs
= 0; /* left record count */
3041 int ptr
; /* key/record index */
3042 union xfs_btree_ptr rptr
; /* right sibling block ptr */
3043 struct xfs_buf
*rbp
; /* right buffer pointer */
3044 struct xfs_btree_block
*right
; /* right btree block */
3045 struct xfs_btree_block
*rrblock
; /* right-right btree block */
3046 struct xfs_buf
*rrbp
; /* right-right buffer pointer */
3047 int rrecs
= 0; /* right record count */
3048 struct xfs_btree_cur
*tcur
; /* temporary btree cursor */
3049 int numrecs
; /* temporary numrec count */
3051 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
3052 XFS_BTREE_TRACE_ARGI(cur
, level
);
3056 /* Get the index of the entry being deleted, check for nothing there. */
3057 ptr
= cur
->bc_ptrs
[level
];
3059 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
3064 /* Get the buffer & block containing the record or key/ptr. */
3065 block
= xfs_btree_get_block(cur
, level
, &bp
);
3066 numrecs
= xfs_btree_get_numrecs(block
);
3069 error
= xfs_btree_check_block(cur
, block
, level
, bp
);
3074 /* Fail if we're off the end of the block. */
3075 if (ptr
> numrecs
) {
3076 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
3081 XFS_BTREE_STATS_INC(cur
, delrec
);
3082 XFS_BTREE_STATS_ADD(cur
, moves
, numrecs
- ptr
);
3084 /* Excise the entries being deleted. */
3086 /* It's a nonleaf. operate on keys and ptrs */
3087 union xfs_btree_key
*lkp
;
3088 union xfs_btree_ptr
*lpp
;
3090 lkp
= xfs_btree_key_addr(cur
, ptr
+ 1, block
);
3091 lpp
= xfs_btree_ptr_addr(cur
, ptr
+ 1, block
);
3094 for (i
= 0; i
< numrecs
- ptr
; i
++) {
3095 error
= xfs_btree_check_ptr(cur
, lpp
, i
, level
);
3101 if (ptr
< numrecs
) {
3102 xfs_btree_shift_keys(cur
, lkp
, -1, numrecs
- ptr
);
3103 xfs_btree_shift_ptrs(cur
, lpp
, -1, numrecs
- ptr
);
3104 xfs_btree_log_keys(cur
, bp
, ptr
, numrecs
- 1);
3105 xfs_btree_log_ptrs(cur
, bp
, ptr
, numrecs
- 1);
3109 * If it's the first record in the block, we'll need to pass a
3110 * key up to the next level (updkey).
3113 keyp
= xfs_btree_key_addr(cur
, 1, block
);
3115 /* It's a leaf. operate on records */
3116 if (ptr
< numrecs
) {
3117 xfs_btree_shift_recs(cur
,
3118 xfs_btree_rec_addr(cur
, ptr
+ 1, block
),
3120 xfs_btree_log_recs(cur
, bp
, ptr
, numrecs
- 1);
3124 * If it's the first record in the block, we'll need a key
3125 * structure to pass up to the next level (updkey).
3128 cur
->bc_ops
->init_key_from_rec(&key
,
3129 xfs_btree_rec_addr(cur
, 1, block
));
3135 * Decrement and log the number of entries in the block.
3137 xfs_btree_set_numrecs(block
, --numrecs
);
3138 xfs_btree_log_block(cur
, bp
, XFS_BB_NUMRECS
);
3141 * If we are tracking the last record in the tree and
3142 * we are at the far right edge of the tree, update it.
3144 if (xfs_btree_is_lastrec(cur
, block
, level
)) {
3145 cur
->bc_ops
->update_lastrec(cur
, block
, NULL
,
3146 ptr
, LASTREC_DELREC
);
3150 * We're at the root level. First, shrink the root block in-memory.
3151 * Try to get rid of the next level down. If we can't then there's
3152 * nothing left to do.
3154 if (level
== cur
->bc_nlevels
- 1) {
3155 if (cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
) {
3156 xfs_iroot_realloc(cur
->bc_private
.b
.ip
, -1,
3157 cur
->bc_private
.b
.whichfork
);
3159 error
= xfs_btree_kill_iroot(cur
);
3163 error
= xfs_btree_dec_cursor(cur
, level
, stat
);
3171 * If this is the root level, and there's only one entry left,
3172 * and it's NOT the leaf level, then we can get rid of this
3175 if (numrecs
== 1 && level
> 0) {
3176 union xfs_btree_ptr
*pp
;
3178 * pp is still set to the first pointer in the block.
3179 * Make it the new root of the btree.
3181 pp
= xfs_btree_ptr_addr(cur
, 1, block
);
3182 error
= cur
->bc_ops
->kill_root(cur
, bp
, level
, pp
);
3185 } else if (level
> 0) {
3186 error
= xfs_btree_dec_cursor(cur
, level
, stat
);
3195 * If we deleted the leftmost entry in the block, update the
3196 * key values above us in the tree.
3199 error
= xfs_btree_updkey(cur
, keyp
, level
+ 1);
3205 * If the number of records remaining in the block is at least
3206 * the minimum, we're done.
3208 if (numrecs
>= cur
->bc_ops
->get_minrecs(cur
, level
)) {
3209 error
= xfs_btree_dec_cursor(cur
, level
, stat
);
3216 * Otherwise, we have to move some records around to keep the
3217 * tree balanced. Look at the left and right sibling blocks to
3218 * see if we can re-balance by moving only one record.
3220 xfs_btree_get_sibling(cur
, block
, &rptr
, XFS_BB_RIGHTSIB
);
3221 xfs_btree_get_sibling(cur
, block
, &lptr
, XFS_BB_LEFTSIB
);
3223 if (cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
) {
3225 * One child of root, need to get a chance to copy its contents
3226 * into the root and delete it. Can't go up to next level,
3227 * there's nothing to delete there.
3229 if (xfs_btree_ptr_is_null(cur
, &rptr
) &&
3230 xfs_btree_ptr_is_null(cur
, &lptr
) &&
3231 level
== cur
->bc_nlevels
- 2) {
3232 error
= xfs_btree_kill_iroot(cur
);
3234 error
= xfs_btree_dec_cursor(cur
, level
, stat
);
3241 ASSERT(!xfs_btree_ptr_is_null(cur
, &rptr
) ||
3242 !xfs_btree_ptr_is_null(cur
, &lptr
));
3245 * Duplicate the cursor so our btree manipulations here won't
3246 * disrupt the next level up.
3248 error
= xfs_btree_dup_cursor(cur
, &tcur
);
3253 * If there's a right sibling, see if it's ok to shift an entry
3256 if (!xfs_btree_ptr_is_null(cur
, &rptr
)) {
3258 * Move the temp cursor to the last entry in the next block.
3259 * Actually any entry but the first would suffice.
3261 i
= xfs_btree_lastrec(tcur
, level
);
3262 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
3264 error
= xfs_btree_increment(tcur
, level
, &i
);
3267 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
3269 i
= xfs_btree_lastrec(tcur
, level
);
3270 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
3272 /* Grab a pointer to the block. */
3273 right
= xfs_btree_get_block(tcur
, level
, &rbp
);
3275 error
= xfs_btree_check_block(tcur
, right
, level
, rbp
);
3279 /* Grab the current block number, for future use. */
3280 xfs_btree_get_sibling(tcur
, right
, &cptr
, XFS_BB_LEFTSIB
);
3283 * If right block is full enough so that removing one entry
3284 * won't make it too empty, and left-shifting an entry out
3285 * of right to us works, we're done.
3287 if (xfs_btree_get_numrecs(right
) - 1 >=
3288 cur
->bc_ops
->get_minrecs(tcur
, level
)) {
3289 error
= xfs_btree_lshift(tcur
, level
, &i
);
3293 ASSERT(xfs_btree_get_numrecs(block
) >=
3294 cur
->bc_ops
->get_minrecs(tcur
, level
));
3296 xfs_btree_del_cursor(tcur
, XFS_BTREE_NOERROR
);
3299 error
= xfs_btree_dec_cursor(cur
, level
, stat
);
3307 * Otherwise, grab the number of records in right for
3308 * future reference, and fix up the temp cursor to point
3309 * to our block again (last record).
3311 rrecs
= xfs_btree_get_numrecs(right
);
3312 if (!xfs_btree_ptr_is_null(cur
, &lptr
)) {
3313 i
= xfs_btree_firstrec(tcur
, level
);
3314 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
3316 error
= xfs_btree_decrement(tcur
, level
, &i
);
3319 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
3324 * If there's a left sibling, see if it's ok to shift an entry
3327 if (!xfs_btree_ptr_is_null(cur
, &lptr
)) {
3329 * Move the temp cursor to the first entry in the
3332 i
= xfs_btree_firstrec(tcur
, level
);
3333 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
3335 error
= xfs_btree_decrement(tcur
, level
, &i
);
3338 i
= xfs_btree_firstrec(tcur
, level
);
3339 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
3341 /* Grab a pointer to the block. */
3342 left
= xfs_btree_get_block(tcur
, level
, &lbp
);
3344 error
= xfs_btree_check_block(cur
, left
, level
, lbp
);
3348 /* Grab the current block number, for future use. */
3349 xfs_btree_get_sibling(tcur
, left
, &cptr
, XFS_BB_RIGHTSIB
);
3352 * If left block is full enough so that removing one entry
3353 * won't make it too empty, and right-shifting an entry out
3354 * of left to us works, we're done.
3356 if (xfs_btree_get_numrecs(left
) - 1 >=
3357 cur
->bc_ops
->get_minrecs(tcur
, level
)) {
3358 error
= xfs_btree_rshift(tcur
, level
, &i
);
3362 ASSERT(xfs_btree_get_numrecs(block
) >=
3363 cur
->bc_ops
->get_minrecs(tcur
, level
));
3364 xfs_btree_del_cursor(tcur
, XFS_BTREE_NOERROR
);
3368 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
3375 * Otherwise, grab the number of records in right for
3378 lrecs
= xfs_btree_get_numrecs(left
);
3381 /* Delete the temp cursor, we're done with it. */
3382 xfs_btree_del_cursor(tcur
, XFS_BTREE_NOERROR
);
3385 /* If here, we need to do a join to keep the tree balanced. */
3386 ASSERT(!xfs_btree_ptr_is_null(cur
, &cptr
));
3388 if (!xfs_btree_ptr_is_null(cur
, &lptr
) &&
3389 lrecs
+ xfs_btree_get_numrecs(block
) <=
3390 cur
->bc_ops
->get_maxrecs(cur
, level
)) {
3392 * Set "right" to be the starting block,
3393 * "left" to be the left neighbor.
3398 error
= xfs_btree_read_buf_block(cur
, &lptr
, level
,
3404 * If that won't work, see if we can join with the right neighbor block.
3406 } else if (!xfs_btree_ptr_is_null(cur
, &rptr
) &&
3407 rrecs
+ xfs_btree_get_numrecs(block
) <=
3408 cur
->bc_ops
->get_maxrecs(cur
, level
)) {
3410 * Set "left" to be the starting block,
3411 * "right" to be the right neighbor.
3416 error
= xfs_btree_read_buf_block(cur
, &rptr
, level
,
3422 * Otherwise, we can't fix the imbalance.
3423 * Just return. This is probably a logic error, but it's not fatal.
3426 error
= xfs_btree_dec_cursor(cur
, level
, stat
);
3432 rrecs
= xfs_btree_get_numrecs(right
);
3433 lrecs
= xfs_btree_get_numrecs(left
);
3436 * We're now going to join "left" and "right" by moving all the stuff
3437 * in "right" to "left" and deleting "right".
3439 XFS_BTREE_STATS_ADD(cur
, moves
, rrecs
);
3441 /* It's a non-leaf. Move keys and pointers. */
3442 union xfs_btree_key
*lkp
; /* left btree key */
3443 union xfs_btree_ptr
*lpp
; /* left address pointer */
3444 union xfs_btree_key
*rkp
; /* right btree key */
3445 union xfs_btree_ptr
*rpp
; /* right address pointer */
3447 lkp
= xfs_btree_key_addr(cur
, lrecs
+ 1, left
);
3448 lpp
= xfs_btree_ptr_addr(cur
, lrecs
+ 1, left
);
3449 rkp
= xfs_btree_key_addr(cur
, 1, right
);
3450 rpp
= xfs_btree_ptr_addr(cur
, 1, right
);
3452 for (i
= 1; i
< rrecs
; i
++) {
3453 error
= xfs_btree_check_ptr(cur
, rpp
, i
, level
);
3458 xfs_btree_copy_keys(cur
, lkp
, rkp
, rrecs
);
3459 xfs_btree_copy_ptrs(cur
, lpp
, rpp
, rrecs
);
3461 xfs_btree_log_keys(cur
, lbp
, lrecs
+ 1, lrecs
+ rrecs
);
3462 xfs_btree_log_ptrs(cur
, lbp
, lrecs
+ 1, lrecs
+ rrecs
);
3464 /* It's a leaf. Move records. */
3465 union xfs_btree_rec
*lrp
; /* left record pointer */
3466 union xfs_btree_rec
*rrp
; /* right record pointer */
3468 lrp
= xfs_btree_rec_addr(cur
, lrecs
+ 1, left
);
3469 rrp
= xfs_btree_rec_addr(cur
, 1, right
);
3471 xfs_btree_copy_recs(cur
, lrp
, rrp
, rrecs
);
3472 xfs_btree_log_recs(cur
, lbp
, lrecs
+ 1, lrecs
+ rrecs
);
3475 XFS_BTREE_STATS_INC(cur
, join
);
3478 * Fix up the the number of records and right block pointer in the
3479 * surviving block, and log it.
3481 xfs_btree_set_numrecs(left
, lrecs
+ rrecs
);
3482 xfs_btree_get_sibling(cur
, right
, &cptr
, XFS_BB_RIGHTSIB
),
3483 xfs_btree_set_sibling(cur
, left
, &cptr
, XFS_BB_RIGHTSIB
);
3484 xfs_btree_log_block(cur
, lbp
, XFS_BB_NUMRECS
| XFS_BB_RIGHTSIB
);
3486 /* If there is a right sibling, point it to the remaining block. */
3487 xfs_btree_get_sibling(cur
, left
, &cptr
, XFS_BB_RIGHTSIB
);
3488 if (!xfs_btree_ptr_is_null(cur
, &cptr
)) {
3489 error
= xfs_btree_read_buf_block(cur
, &cptr
, level
,
3490 0, &rrblock
, &rrbp
);
3493 xfs_btree_set_sibling(cur
, rrblock
, &lptr
, XFS_BB_LEFTSIB
);
3494 xfs_btree_log_block(cur
, rrbp
, XFS_BB_LEFTSIB
);
3497 /* Free the deleted block. */
3498 error
= cur
->bc_ops
->free_block(cur
, rbp
);
3501 XFS_BTREE_STATS_INC(cur
, free
);
3504 * If we joined with the left neighbor, set the buffer in the
3505 * cursor to the left block, and fix up the index.
3508 cur
->bc_bufs
[level
] = lbp
;
3509 cur
->bc_ptrs
[level
] += lrecs
;
3510 cur
->bc_ra
[level
] = 0;
3513 * If we joined with the right neighbor and there's a level above
3514 * us, increment the cursor at that level.
3516 else if ((cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
) ||
3517 (level
+ 1 < cur
->bc_nlevels
)) {
3518 error
= xfs_btree_increment(cur
, level
+ 1, &i
);
3524 * Readjust the ptr at this level if it's not a leaf, since it's
3525 * still pointing at the deletion point, which makes the cursor
3526 * inconsistent. If this makes the ptr 0, the caller fixes it up.
3527 * We can't use decrement because it would change the next level up.
3530 cur
->bc_ptrs
[level
]--;
3532 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
3533 /* Return value means the next level up has something to do. */
3538 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
3540 xfs_btree_del_cursor(tcur
, XFS_BTREE_ERROR
);
3545 * Delete the record pointed to by cur.
3546 * The cursor refers to the place where the record was (could be inserted)
3547 * when the operation returns.
3551 struct xfs_btree_cur
*cur
,
3552 int *stat
) /* success/failure */
3554 int error
; /* error return value */
3558 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
3561 * Go up the tree, starting at leaf level.
3563 * If 2 is returned then a join was done; go to the next level.
3564 * Otherwise we are done.
3566 for (level
= 0, i
= 2; i
== 2; level
++) {
3567 error
= xfs_btree_delrec(cur
, level
, &i
);
3573 for (level
= 1; level
< cur
->bc_nlevels
; level
++) {
3574 if (cur
->bc_ptrs
[level
] == 0) {
3575 error
= xfs_btree_decrement(cur
, level
, &i
);
3583 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
3587 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
3592 * Get the data from the pointed-to record.
3596 struct xfs_btree_cur
*cur
, /* btree cursor */
3597 union xfs_btree_rec
**recp
, /* output: btree record */
3598 int *stat
) /* output: success/failure */
3600 struct xfs_btree_block
*block
; /* btree block */
3601 struct xfs_buf
*bp
; /* buffer pointer */
3602 int ptr
; /* record number */
3604 int error
; /* error return value */
3607 ptr
= cur
->bc_ptrs
[0];
3608 block
= xfs_btree_get_block(cur
, 0, &bp
);
3611 error
= xfs_btree_check_block(cur
, block
, 0, bp
);
3617 * Off the right end or left end, return failure.
3619 if (ptr
> xfs_btree_get_numrecs(block
) || ptr
<= 0) {
3625 * Point to the record and extract its data.
3627 *recp
= xfs_btree_rec_addr(cur
, ptr
, block
);