2 * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 #include "xfs_types.h"
24 #include "xfs_trans.h"
28 #include "xfs_dmapi.h"
29 #include "xfs_mount.h"
30 #include "xfs_bmap_btree.h"
31 #include "xfs_alloc_btree.h"
32 #include "xfs_ialloc_btree.h"
33 #include "xfs_dir2_sf.h"
34 #include "xfs_attr_sf.h"
35 #include "xfs_dinode.h"
36 #include "xfs_inode.h"
37 #include "xfs_inode_item.h"
38 #include "xfs_btree.h"
39 #include "xfs_btree_trace.h"
40 #include "xfs_ialloc.h"
41 #include "xfs_error.h"
42 #include "xfs_trace.h"
45 * Cursor allocation zone.
47 kmem_zone_t
*xfs_btree_cur_zone
;
50 * Btree magic numbers.
52 const __uint32_t xfs_magics
[XFS_BTNUM_MAX
] = {
53 XFS_ABTB_MAGIC
, XFS_ABTC_MAGIC
, XFS_BMAP_MAGIC
, XFS_IBT_MAGIC
57 STATIC
int /* error (0 or EFSCORRUPTED) */
58 xfs_btree_check_lblock(
59 struct xfs_btree_cur
*cur
, /* btree cursor */
60 struct xfs_btree_block
*block
, /* btree long form block pointer */
61 int level
, /* level of the btree block */
62 struct xfs_buf
*bp
) /* buffer for block, if any */
64 int lblock_ok
; /* block passes checks */
65 struct xfs_mount
*mp
; /* file system mount point */
69 be32_to_cpu(block
->bb_magic
) == xfs_magics
[cur
->bc_btnum
] &&
70 be16_to_cpu(block
->bb_level
) == level
&&
71 be16_to_cpu(block
->bb_numrecs
) <=
72 cur
->bc_ops
->get_maxrecs(cur
, level
) &&
73 block
->bb_u
.l
.bb_leftsib
&&
74 (be64_to_cpu(block
->bb_u
.l
.bb_leftsib
) == NULLDFSBNO
||
75 XFS_FSB_SANITY_CHECK(mp
,
76 be64_to_cpu(block
->bb_u
.l
.bb_leftsib
))) &&
77 block
->bb_u
.l
.bb_rightsib
&&
78 (be64_to_cpu(block
->bb_u
.l
.bb_rightsib
) == NULLDFSBNO
||
79 XFS_FSB_SANITY_CHECK(mp
,
80 be64_to_cpu(block
->bb_u
.l
.bb_rightsib
)));
81 if (unlikely(XFS_TEST_ERROR(!lblock_ok
, mp
,
82 XFS_ERRTAG_BTREE_CHECK_LBLOCK
,
83 XFS_RANDOM_BTREE_CHECK_LBLOCK
))) {
85 trace_xfs_btree_corrupt(bp
, _RET_IP_
);
86 XFS_ERROR_REPORT("xfs_btree_check_lblock", XFS_ERRLEVEL_LOW
,
88 return XFS_ERROR(EFSCORRUPTED
);
93 STATIC
int /* error (0 or EFSCORRUPTED) */
94 xfs_btree_check_sblock(
95 struct xfs_btree_cur
*cur
, /* btree cursor */
96 struct xfs_btree_block
*block
, /* btree short form block pointer */
97 int level
, /* level of the btree block */
98 struct xfs_buf
*bp
) /* buffer containing block */
100 struct xfs_buf
*agbp
; /* buffer for ag. freespace struct */
101 struct xfs_agf
*agf
; /* ag. freespace structure */
102 xfs_agblock_t agflen
; /* native ag. freespace length */
103 int sblock_ok
; /* block passes checks */
105 agbp
= cur
->bc_private
.a
.agbp
;
106 agf
= XFS_BUF_TO_AGF(agbp
);
107 agflen
= be32_to_cpu(agf
->agf_length
);
109 be32_to_cpu(block
->bb_magic
) == xfs_magics
[cur
->bc_btnum
] &&
110 be16_to_cpu(block
->bb_level
) == level
&&
111 be16_to_cpu(block
->bb_numrecs
) <=
112 cur
->bc_ops
->get_maxrecs(cur
, level
) &&
113 (be32_to_cpu(block
->bb_u
.s
.bb_leftsib
) == NULLAGBLOCK
||
114 be32_to_cpu(block
->bb_u
.s
.bb_leftsib
) < agflen
) &&
115 block
->bb_u
.s
.bb_leftsib
&&
116 (be32_to_cpu(block
->bb_u
.s
.bb_rightsib
) == NULLAGBLOCK
||
117 be32_to_cpu(block
->bb_u
.s
.bb_rightsib
) < agflen
) &&
118 block
->bb_u
.s
.bb_rightsib
;
119 if (unlikely(XFS_TEST_ERROR(!sblock_ok
, cur
->bc_mp
,
120 XFS_ERRTAG_BTREE_CHECK_SBLOCK
,
121 XFS_RANDOM_BTREE_CHECK_SBLOCK
))) {
123 trace_xfs_btree_corrupt(bp
, _RET_IP_
);
124 XFS_CORRUPTION_ERROR("xfs_btree_check_sblock",
125 XFS_ERRLEVEL_LOW
, cur
->bc_mp
, block
);
126 return XFS_ERROR(EFSCORRUPTED
);
132 * Debug routine: check that block header is ok.
135 xfs_btree_check_block(
136 struct xfs_btree_cur
*cur
, /* btree cursor */
137 struct xfs_btree_block
*block
, /* generic btree block pointer */
138 int level
, /* level of the btree block */
139 struct xfs_buf
*bp
) /* buffer containing block, if any */
141 if (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
)
142 return xfs_btree_check_lblock(cur
, block
, level
, bp
);
144 return xfs_btree_check_sblock(cur
, block
, level
, bp
);
148 * Check that (long) pointer is ok.
150 int /* error (0 or EFSCORRUPTED) */
151 xfs_btree_check_lptr(
152 struct xfs_btree_cur
*cur
, /* btree cursor */
153 xfs_dfsbno_t bno
, /* btree block disk address */
154 int level
) /* btree block level */
156 XFS_WANT_CORRUPTED_RETURN(
159 XFS_FSB_SANITY_CHECK(cur
->bc_mp
, bno
));
165 * Check that (short) pointer is ok.
167 STATIC
int /* error (0 or EFSCORRUPTED) */
168 xfs_btree_check_sptr(
169 struct xfs_btree_cur
*cur
, /* btree cursor */
170 xfs_agblock_t bno
, /* btree block disk address */
171 int level
) /* btree block level */
173 xfs_agblock_t agblocks
= cur
->bc_mp
->m_sb
.sb_agblocks
;
175 XFS_WANT_CORRUPTED_RETURN(
177 bno
!= NULLAGBLOCK
&&
184 * Check that block ptr is ok.
186 STATIC
int /* error (0 or EFSCORRUPTED) */
188 struct xfs_btree_cur
*cur
, /* btree cursor */
189 union xfs_btree_ptr
*ptr
, /* btree block disk address */
190 int index
, /* offset from ptr to check */
191 int level
) /* btree block level */
193 if (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
) {
194 return xfs_btree_check_lptr(cur
,
195 be64_to_cpu((&ptr
->l
)[index
]), level
);
197 return xfs_btree_check_sptr(cur
,
198 be32_to_cpu((&ptr
->s
)[index
]), level
);
204 * Delete the btree cursor.
207 xfs_btree_del_cursor(
208 xfs_btree_cur_t
*cur
, /* btree cursor */
209 int error
) /* del because of error */
211 int i
; /* btree level */
214 * Clear the buffer pointers, and release the buffers.
215 * If we're doing this in the face of an error, we
216 * need to make sure to inspect all of the entries
217 * in the bc_bufs array for buffers to be unlocked.
218 * This is because some of the btree code works from
219 * level n down to 0, and if we get an error along
220 * the way we won't have initialized all the entries
223 for (i
= 0; i
< cur
->bc_nlevels
; i
++) {
225 xfs_btree_setbuf(cur
, i
, NULL
);
230 * Can't free a bmap cursor without having dealt with the
231 * allocated indirect blocks' accounting.
233 ASSERT(cur
->bc_btnum
!= XFS_BTNUM_BMAP
||
234 cur
->bc_private
.b
.allocated
== 0);
238 kmem_zone_free(xfs_btree_cur_zone
, cur
);
242 * Duplicate the btree cursor.
243 * Allocate a new one, copy the record, re-get the buffers.
246 xfs_btree_dup_cursor(
247 xfs_btree_cur_t
*cur
, /* input cursor */
248 xfs_btree_cur_t
**ncur
) /* output cursor */
250 xfs_buf_t
*bp
; /* btree block's buffer pointer */
251 int error
; /* error return value */
252 int i
; /* level number of btree block */
253 xfs_mount_t
*mp
; /* mount structure for filesystem */
254 xfs_btree_cur_t
*new; /* new cursor value */
255 xfs_trans_t
*tp
; /* transaction pointer, can be NULL */
261 * Allocate a new cursor like the old one.
263 new = cur
->bc_ops
->dup_cursor(cur
);
266 * Copy the record currently in the cursor.
268 new->bc_rec
= cur
->bc_rec
;
271 * For each level current, re-get the buffer and copy the ptr value.
273 for (i
= 0; i
< new->bc_nlevels
; i
++) {
274 new->bc_ptrs
[i
] = cur
->bc_ptrs
[i
];
275 new->bc_ra
[i
] = cur
->bc_ra
[i
];
276 if ((bp
= cur
->bc_bufs
[i
])) {
277 if ((error
= xfs_trans_read_buf(mp
, tp
, mp
->m_ddev_targp
,
278 XFS_BUF_ADDR(bp
), mp
->m_bsize
, 0, &bp
))) {
279 xfs_btree_del_cursor(new, error
);
283 new->bc_bufs
[i
] = bp
;
285 ASSERT(!XFS_BUF_GETERROR(bp
));
287 new->bc_bufs
[i
] = NULL
;
294 * XFS btree block layout and addressing:
296 * There are two types of blocks in the btree: leaf and non-leaf blocks.
298 * The leaf record start with a header then followed by records containing
299 * the values. A non-leaf block also starts with the same header, and
300 * then first contains lookup keys followed by an equal number of pointers
301 * to the btree blocks at the previous level.
303 * +--------+-------+-------+-------+-------+-------+-------+
304 * Leaf: | header | rec 1 | rec 2 | rec 3 | rec 4 | rec 5 | rec N |
305 * +--------+-------+-------+-------+-------+-------+-------+
307 * +--------+-------+-------+-------+-------+-------+-------+
308 * Non-Leaf: | header | key 1 | key 2 | key N | ptr 1 | ptr 2 | ptr N |
309 * +--------+-------+-------+-------+-------+-------+-------+
311 * The header is called struct xfs_btree_block for reasons better left unknown
312 * and comes in different versions for short (32bit) and long (64bit) block
313 * pointers. The record and key structures are defined by the btree instances
314 * and opaque to the btree core. The block pointers are simple disk endian
315 * integers, available in a short (32bit) and long (64bit) variant.
317 * The helpers below calculate the offset of a given record, key or pointer
318 * into a btree block (xfs_btree_*_offset) or return a pointer to the given
319 * record, key or pointer (xfs_btree_*_addr). Note that all addressing
320 * inside the btree block is done using indices starting at one, not zero!
324 * Return size of the btree block header for this btree instance.
326 static inline size_t xfs_btree_block_len(struct xfs_btree_cur
*cur
)
328 return (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
) ?
329 XFS_BTREE_LBLOCK_LEN
:
330 XFS_BTREE_SBLOCK_LEN
;
334 * Return size of btree block pointers for this btree instance.
336 static inline size_t xfs_btree_ptr_len(struct xfs_btree_cur
*cur
)
338 return (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
) ?
339 sizeof(__be64
) : sizeof(__be32
);
343 * Calculate offset of the n-th record in a btree block.
346 xfs_btree_rec_offset(
347 struct xfs_btree_cur
*cur
,
350 return xfs_btree_block_len(cur
) +
351 (n
- 1) * cur
->bc_ops
->rec_len
;
355 * Calculate offset of the n-th key in a btree block.
358 xfs_btree_key_offset(
359 struct xfs_btree_cur
*cur
,
362 return xfs_btree_block_len(cur
) +
363 (n
- 1) * cur
->bc_ops
->key_len
;
367 * Calculate offset of the n-th block pointer in a btree block.
370 xfs_btree_ptr_offset(
371 struct xfs_btree_cur
*cur
,
375 return xfs_btree_block_len(cur
) +
376 cur
->bc_ops
->get_maxrecs(cur
, level
) * cur
->bc_ops
->key_len
+
377 (n
- 1) * xfs_btree_ptr_len(cur
);
381 * Return a pointer to the n-th record in the btree block.
383 STATIC
union xfs_btree_rec
*
385 struct xfs_btree_cur
*cur
,
387 struct xfs_btree_block
*block
)
389 return (union xfs_btree_rec
*)
390 ((char *)block
+ xfs_btree_rec_offset(cur
, n
));
394 * Return a pointer to the n-th key in the btree block.
396 STATIC
union xfs_btree_key
*
398 struct xfs_btree_cur
*cur
,
400 struct xfs_btree_block
*block
)
402 return (union xfs_btree_key
*)
403 ((char *)block
+ xfs_btree_key_offset(cur
, n
));
407 * Return a pointer to the n-th block pointer in the btree block.
409 STATIC
union xfs_btree_ptr
*
411 struct xfs_btree_cur
*cur
,
413 struct xfs_btree_block
*block
)
415 int level
= xfs_btree_get_level(block
);
417 ASSERT(block
->bb_level
!= 0);
419 return (union xfs_btree_ptr
*)
420 ((char *)block
+ xfs_btree_ptr_offset(cur
, n
, level
));
424 * Get a the root block which is stored in the inode.
426 * For now this btree implementation assumes the btree root is always
427 * stored in the if_broot field of an inode fork.
429 STATIC
struct xfs_btree_block
*
431 struct xfs_btree_cur
*cur
)
433 struct xfs_ifork
*ifp
;
435 ifp
= XFS_IFORK_PTR(cur
->bc_private
.b
.ip
, cur
->bc_private
.b
.whichfork
);
436 return (struct xfs_btree_block
*)ifp
->if_broot
;
440 * Retrieve the block pointer from the cursor at the given level.
441 * This may be an inode btree root or from a buffer.
443 STATIC
struct xfs_btree_block
* /* generic btree block pointer */
445 struct xfs_btree_cur
*cur
, /* btree cursor */
446 int level
, /* level in btree */
447 struct xfs_buf
**bpp
) /* buffer containing the block */
449 if ((cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
) &&
450 (level
== cur
->bc_nlevels
- 1)) {
452 return xfs_btree_get_iroot(cur
);
455 *bpp
= cur
->bc_bufs
[level
];
456 return XFS_BUF_TO_BLOCK(*bpp
);
460 * Get a buffer for the block, return it with no data read.
461 * Long-form addressing.
463 xfs_buf_t
* /* buffer for fsbno */
465 xfs_mount_t
*mp
, /* file system mount point */
466 xfs_trans_t
*tp
, /* transaction pointer */
467 xfs_fsblock_t fsbno
, /* file system 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(fsbno
!= NULLFSBLOCK
);
474 d
= XFS_FSB_TO_DADDR(mp
, fsbno
);
475 bp
= xfs_trans_get_buf(tp
, mp
->m_ddev_targp
, d
, mp
->m_bsize
, lock
);
477 ASSERT(!XFS_BUF_GETERROR(bp
));
482 * Get a buffer for the block, return it with no data read.
483 * Short-form addressing.
485 xfs_buf_t
* /* buffer for agno/agbno */
487 xfs_mount_t
*mp
, /* file system mount point */
488 xfs_trans_t
*tp
, /* transaction pointer */
489 xfs_agnumber_t agno
, /* allocation group number */
490 xfs_agblock_t agbno
, /* allocation group block number */
491 uint lock
) /* lock flags for get_buf */
493 xfs_buf_t
*bp
; /* buffer pointer (return value) */
494 xfs_daddr_t d
; /* real disk block address */
496 ASSERT(agno
!= NULLAGNUMBER
);
497 ASSERT(agbno
!= NULLAGBLOCK
);
498 d
= XFS_AGB_TO_DADDR(mp
, agno
, agbno
);
499 bp
= xfs_trans_get_buf(tp
, mp
->m_ddev_targp
, d
, mp
->m_bsize
, lock
);
501 ASSERT(!XFS_BUF_GETERROR(bp
));
506 * Check for the cursor referring to the last block at the given level.
508 int /* 1=is last block, 0=not last block */
509 xfs_btree_islastblock(
510 xfs_btree_cur_t
*cur
, /* btree cursor */
511 int level
) /* level to check */
513 struct xfs_btree_block
*block
; /* generic btree block pointer */
514 xfs_buf_t
*bp
; /* buffer containing block */
516 block
= xfs_btree_get_block(cur
, level
, &bp
);
517 xfs_btree_check_block(cur
, block
, level
, bp
);
518 if (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
)
519 return be64_to_cpu(block
->bb_u
.l
.bb_rightsib
) == NULLDFSBNO
;
521 return be32_to_cpu(block
->bb_u
.s
.bb_rightsib
) == NULLAGBLOCK
;
525 * Change the cursor to point to the first record at the given level.
526 * Other levels are unaffected.
528 STATIC
int /* success=1, failure=0 */
530 xfs_btree_cur_t
*cur
, /* btree cursor */
531 int level
) /* level to change */
533 struct xfs_btree_block
*block
; /* generic btree block pointer */
534 xfs_buf_t
*bp
; /* buffer containing block */
537 * Get the block pointer for this level.
539 block
= xfs_btree_get_block(cur
, level
, &bp
);
540 xfs_btree_check_block(cur
, block
, level
, bp
);
542 * It's empty, there is no such record.
544 if (!block
->bb_numrecs
)
547 * Set the ptr value to 1, that's the first record/key.
549 cur
->bc_ptrs
[level
] = 1;
554 * Change the cursor to point to the last record in the current block
555 * at the given level. Other levels are unaffected.
557 STATIC
int /* success=1, failure=0 */
559 xfs_btree_cur_t
*cur
, /* btree cursor */
560 int level
) /* level to change */
562 struct xfs_btree_block
*block
; /* generic btree block pointer */
563 xfs_buf_t
*bp
; /* buffer containing block */
566 * Get the block pointer for this level.
568 block
= xfs_btree_get_block(cur
, level
, &bp
);
569 xfs_btree_check_block(cur
, block
, level
, bp
);
571 * It's empty, there is no such record.
573 if (!block
->bb_numrecs
)
576 * Set the ptr value to numrecs, that's the last record/key.
578 cur
->bc_ptrs
[level
] = be16_to_cpu(block
->bb_numrecs
);
583 * Compute first and last byte offsets for the fields given.
584 * Interprets the offsets table, which contains struct field offsets.
588 __int64_t fields
, /* bitmask of fields */
589 const short *offsets
, /* table of field offsets */
590 int nbits
, /* number of bits to inspect */
591 int *first
, /* output: first byte offset */
592 int *last
) /* output: last byte offset */
594 int i
; /* current bit number */
595 __int64_t imask
; /* mask for current bit number */
599 * Find the lowest bit, so the first byte offset.
601 for (i
= 0, imask
= 1LL; ; i
++, imask
<<= 1) {
602 if (imask
& fields
) {
608 * Find the highest bit, so the last byte offset.
610 for (i
= nbits
- 1, imask
= 1LL << i
; ; i
--, imask
>>= 1) {
611 if (imask
& fields
) {
612 *last
= offsets
[i
+ 1] - 1;
619 * Get a buffer for the block, return it read in.
620 * Long-form addressing.
624 xfs_mount_t
*mp
, /* file system mount point */
625 xfs_trans_t
*tp
, /* transaction pointer */
626 xfs_fsblock_t fsbno
, /* file system block number */
627 uint lock
, /* lock flags for read_buf */
628 xfs_buf_t
**bpp
, /* buffer for fsbno */
629 int refval
) /* ref count value for buffer */
631 xfs_buf_t
*bp
; /* return value */
632 xfs_daddr_t d
; /* real disk block address */
635 ASSERT(fsbno
!= NULLFSBLOCK
);
636 d
= XFS_FSB_TO_DADDR(mp
, fsbno
);
637 if ((error
= xfs_trans_read_buf(mp
, tp
, mp
->m_ddev_targp
, d
,
638 mp
->m_bsize
, lock
, &bp
))) {
641 ASSERT(!bp
|| !XFS_BUF_GETERROR(bp
));
643 XFS_BUF_SET_VTYPE_REF(bp
, B_FS_MAP
, refval
);
650 * Read-ahead the block, don't wait for it, don't return a buffer.
651 * Long-form addressing.
655 xfs_btree_reada_bufl(
656 xfs_mount_t
*mp
, /* file system mount point */
657 xfs_fsblock_t fsbno
, /* file system block number */
658 xfs_extlen_t count
) /* count of filesystem blocks */
662 ASSERT(fsbno
!= NULLFSBLOCK
);
663 d
= XFS_FSB_TO_DADDR(mp
, fsbno
);
664 xfs_baread(mp
->m_ddev_targp
, d
, mp
->m_bsize
* count
);
668 * Read-ahead the block, don't wait for it, don't return a buffer.
669 * Short-form addressing.
673 xfs_btree_reada_bufs(
674 xfs_mount_t
*mp
, /* file system mount point */
675 xfs_agnumber_t agno
, /* allocation group number */
676 xfs_agblock_t agbno
, /* allocation group block number */
677 xfs_extlen_t count
) /* count of filesystem blocks */
681 ASSERT(agno
!= NULLAGNUMBER
);
682 ASSERT(agbno
!= NULLAGBLOCK
);
683 d
= XFS_AGB_TO_DADDR(mp
, agno
, agbno
);
684 xfs_baread(mp
->m_ddev_targp
, d
, mp
->m_bsize
* count
);
688 xfs_btree_readahead_lblock(
689 struct xfs_btree_cur
*cur
,
691 struct xfs_btree_block
*block
)
694 xfs_dfsbno_t left
= be64_to_cpu(block
->bb_u
.l
.bb_leftsib
);
695 xfs_dfsbno_t right
= be64_to_cpu(block
->bb_u
.l
.bb_rightsib
);
697 if ((lr
& XFS_BTCUR_LEFTRA
) && left
!= NULLDFSBNO
) {
698 xfs_btree_reada_bufl(cur
->bc_mp
, left
, 1);
702 if ((lr
& XFS_BTCUR_RIGHTRA
) && right
!= NULLDFSBNO
) {
703 xfs_btree_reada_bufl(cur
->bc_mp
, right
, 1);
711 xfs_btree_readahead_sblock(
712 struct xfs_btree_cur
*cur
,
714 struct xfs_btree_block
*block
)
717 xfs_agblock_t left
= be32_to_cpu(block
->bb_u
.s
.bb_leftsib
);
718 xfs_agblock_t right
= be32_to_cpu(block
->bb_u
.s
.bb_rightsib
);
721 if ((lr
& XFS_BTCUR_LEFTRA
) && left
!= NULLAGBLOCK
) {
722 xfs_btree_reada_bufs(cur
->bc_mp
, cur
->bc_private
.a
.agno
,
727 if ((lr
& XFS_BTCUR_RIGHTRA
) && right
!= NULLAGBLOCK
) {
728 xfs_btree_reada_bufs(cur
->bc_mp
, cur
->bc_private
.a
.agno
,
737 * Read-ahead btree blocks, at the given level.
738 * Bits in lr are set from XFS_BTCUR_{LEFT,RIGHT}RA.
742 struct xfs_btree_cur
*cur
, /* btree cursor */
743 int lev
, /* level in btree */
744 int lr
) /* left/right bits */
746 struct xfs_btree_block
*block
;
749 * No readahead needed if we are at the root level and the
750 * btree root is stored in the inode.
752 if ((cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
) &&
753 (lev
== cur
->bc_nlevels
- 1))
756 if ((cur
->bc_ra
[lev
] | lr
) == cur
->bc_ra
[lev
])
759 cur
->bc_ra
[lev
] |= lr
;
760 block
= XFS_BUF_TO_BLOCK(cur
->bc_bufs
[lev
]);
762 if (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
)
763 return xfs_btree_readahead_lblock(cur
, lr
, block
);
764 return xfs_btree_readahead_sblock(cur
, lr
, block
);
768 * Set the buffer for level "lev" in the cursor to bp, releasing
769 * any previous buffer.
773 xfs_btree_cur_t
*cur
, /* btree cursor */
774 int lev
, /* level in btree */
775 xfs_buf_t
*bp
) /* new buffer to set */
777 struct xfs_btree_block
*b
; /* btree block */
778 xfs_buf_t
*obp
; /* old buffer pointer */
780 obp
= cur
->bc_bufs
[lev
];
782 xfs_trans_brelse(cur
->bc_tp
, obp
);
783 cur
->bc_bufs
[lev
] = bp
;
787 b
= XFS_BUF_TO_BLOCK(bp
);
788 if (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
) {
789 if (be64_to_cpu(b
->bb_u
.l
.bb_leftsib
) == NULLDFSBNO
)
790 cur
->bc_ra
[lev
] |= XFS_BTCUR_LEFTRA
;
791 if (be64_to_cpu(b
->bb_u
.l
.bb_rightsib
) == NULLDFSBNO
)
792 cur
->bc_ra
[lev
] |= XFS_BTCUR_RIGHTRA
;
794 if (be32_to_cpu(b
->bb_u
.s
.bb_leftsib
) == NULLAGBLOCK
)
795 cur
->bc_ra
[lev
] |= XFS_BTCUR_LEFTRA
;
796 if (be32_to_cpu(b
->bb_u
.s
.bb_rightsib
) == NULLAGBLOCK
)
797 cur
->bc_ra
[lev
] |= XFS_BTCUR_RIGHTRA
;
802 xfs_btree_ptr_is_null(
803 struct xfs_btree_cur
*cur
,
804 union xfs_btree_ptr
*ptr
)
806 if (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
)
807 return be64_to_cpu(ptr
->l
) == NULLDFSBNO
;
809 return be32_to_cpu(ptr
->s
) == NULLAGBLOCK
;
813 xfs_btree_set_ptr_null(
814 struct xfs_btree_cur
*cur
,
815 union xfs_btree_ptr
*ptr
)
817 if (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
)
818 ptr
->l
= cpu_to_be64(NULLDFSBNO
);
820 ptr
->s
= cpu_to_be32(NULLAGBLOCK
);
824 * Get/set/init sibling pointers
827 xfs_btree_get_sibling(
828 struct xfs_btree_cur
*cur
,
829 struct xfs_btree_block
*block
,
830 union xfs_btree_ptr
*ptr
,
833 ASSERT(lr
== XFS_BB_LEFTSIB
|| lr
== XFS_BB_RIGHTSIB
);
835 if (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
) {
836 if (lr
== XFS_BB_RIGHTSIB
)
837 ptr
->l
= block
->bb_u
.l
.bb_rightsib
;
839 ptr
->l
= block
->bb_u
.l
.bb_leftsib
;
841 if (lr
== XFS_BB_RIGHTSIB
)
842 ptr
->s
= block
->bb_u
.s
.bb_rightsib
;
844 ptr
->s
= block
->bb_u
.s
.bb_leftsib
;
849 xfs_btree_set_sibling(
850 struct xfs_btree_cur
*cur
,
851 struct xfs_btree_block
*block
,
852 union xfs_btree_ptr
*ptr
,
855 ASSERT(lr
== XFS_BB_LEFTSIB
|| lr
== XFS_BB_RIGHTSIB
);
857 if (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
) {
858 if (lr
== XFS_BB_RIGHTSIB
)
859 block
->bb_u
.l
.bb_rightsib
= ptr
->l
;
861 block
->bb_u
.l
.bb_leftsib
= ptr
->l
;
863 if (lr
== XFS_BB_RIGHTSIB
)
864 block
->bb_u
.s
.bb_rightsib
= ptr
->s
;
866 block
->bb_u
.s
.bb_leftsib
= ptr
->s
;
871 xfs_btree_init_block(
872 struct xfs_btree_cur
*cur
,
875 struct xfs_btree_block
*new) /* new block */
877 new->bb_magic
= cpu_to_be32(xfs_magics
[cur
->bc_btnum
]);
878 new->bb_level
= cpu_to_be16(level
);
879 new->bb_numrecs
= cpu_to_be16(numrecs
);
881 if (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
) {
882 new->bb_u
.l
.bb_leftsib
= cpu_to_be64(NULLDFSBNO
);
883 new->bb_u
.l
.bb_rightsib
= cpu_to_be64(NULLDFSBNO
);
885 new->bb_u
.s
.bb_leftsib
= cpu_to_be32(NULLAGBLOCK
);
886 new->bb_u
.s
.bb_rightsib
= cpu_to_be32(NULLAGBLOCK
);
891 * Return true if ptr is the last record in the btree and
892 * we need to track updateѕ to this record. The decision
893 * will be further refined in the update_lastrec method.
896 xfs_btree_is_lastrec(
897 struct xfs_btree_cur
*cur
,
898 struct xfs_btree_block
*block
,
901 union xfs_btree_ptr ptr
;
905 if (!(cur
->bc_flags
& XFS_BTREE_LASTREC_UPDATE
))
908 xfs_btree_get_sibling(cur
, block
, &ptr
, XFS_BB_RIGHTSIB
);
909 if (!xfs_btree_ptr_is_null(cur
, &ptr
))
915 xfs_btree_buf_to_ptr(
916 struct xfs_btree_cur
*cur
,
918 union xfs_btree_ptr
*ptr
)
920 if (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
)
921 ptr
->l
= cpu_to_be64(XFS_DADDR_TO_FSB(cur
->bc_mp
,
924 ptr
->s
= cpu_to_be32(xfs_daddr_to_agbno(cur
->bc_mp
,
930 xfs_btree_ptr_to_daddr(
931 struct xfs_btree_cur
*cur
,
932 union xfs_btree_ptr
*ptr
)
934 if (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
) {
935 ASSERT(be64_to_cpu(ptr
->l
) != NULLDFSBNO
);
937 return XFS_FSB_TO_DADDR(cur
->bc_mp
, be64_to_cpu(ptr
->l
));
939 ASSERT(cur
->bc_private
.a
.agno
!= NULLAGNUMBER
);
940 ASSERT(be32_to_cpu(ptr
->s
) != NULLAGBLOCK
);
942 return XFS_AGB_TO_DADDR(cur
->bc_mp
, cur
->bc_private
.a
.agno
,
943 be32_to_cpu(ptr
->s
));
949 struct xfs_btree_cur
*cur
,
952 switch (cur
->bc_btnum
) {
955 XFS_BUF_SET_VTYPE_REF(*bpp
, B_FS_MAP
, XFS_ALLOC_BTREE_REF
);
958 XFS_BUF_SET_VTYPE_REF(*bpp
, B_FS_INOMAP
, XFS_INO_BTREE_REF
);
961 XFS_BUF_SET_VTYPE_REF(*bpp
, B_FS_MAP
, XFS_BMAP_BTREE_REF
);
969 xfs_btree_get_buf_block(
970 struct xfs_btree_cur
*cur
,
971 union xfs_btree_ptr
*ptr
,
973 struct xfs_btree_block
**block
,
974 struct xfs_buf
**bpp
)
976 struct xfs_mount
*mp
= cur
->bc_mp
;
979 /* need to sort out how callers deal with failures first */
980 ASSERT(!(flags
& XBF_TRYLOCK
));
982 d
= xfs_btree_ptr_to_daddr(cur
, ptr
);
983 *bpp
= xfs_trans_get_buf(cur
->bc_tp
, mp
->m_ddev_targp
, d
,
987 ASSERT(!XFS_BUF_GETERROR(*bpp
));
989 *block
= XFS_BUF_TO_BLOCK(*bpp
);
994 * Read in the buffer at the given ptr and return the buffer and
995 * the block pointer within the buffer.
998 xfs_btree_read_buf_block(
999 struct xfs_btree_cur
*cur
,
1000 union xfs_btree_ptr
*ptr
,
1003 struct xfs_btree_block
**block
,
1004 struct xfs_buf
**bpp
)
1006 struct xfs_mount
*mp
= cur
->bc_mp
;
1010 /* need to sort out how callers deal with failures first */
1011 ASSERT(!(flags
& XBF_TRYLOCK
));
1013 d
= xfs_btree_ptr_to_daddr(cur
, ptr
);
1014 error
= xfs_trans_read_buf(mp
, cur
->bc_tp
, mp
->m_ddev_targp
, d
,
1015 mp
->m_bsize
, flags
, bpp
);
1019 ASSERT(*bpp
!= NULL
);
1020 ASSERT(!XFS_BUF_GETERROR(*bpp
));
1022 xfs_btree_set_refs(cur
, *bpp
);
1023 *block
= XFS_BUF_TO_BLOCK(*bpp
);
1025 error
= xfs_btree_check_block(cur
, *block
, level
, *bpp
);
1027 xfs_trans_brelse(cur
->bc_tp
, *bpp
);
1032 * Copy keys from one btree block to another.
1035 xfs_btree_copy_keys(
1036 struct xfs_btree_cur
*cur
,
1037 union xfs_btree_key
*dst_key
,
1038 union xfs_btree_key
*src_key
,
1041 ASSERT(numkeys
>= 0);
1042 memcpy(dst_key
, src_key
, numkeys
* cur
->bc_ops
->key_len
);
1046 * Copy records from one btree block to another.
1049 xfs_btree_copy_recs(
1050 struct xfs_btree_cur
*cur
,
1051 union xfs_btree_rec
*dst_rec
,
1052 union xfs_btree_rec
*src_rec
,
1055 ASSERT(numrecs
>= 0);
1056 memcpy(dst_rec
, src_rec
, numrecs
* cur
->bc_ops
->rec_len
);
1060 * Copy block pointers from one btree block to another.
1063 xfs_btree_copy_ptrs(
1064 struct xfs_btree_cur
*cur
,
1065 union xfs_btree_ptr
*dst_ptr
,
1066 union xfs_btree_ptr
*src_ptr
,
1069 ASSERT(numptrs
>= 0);
1070 memcpy(dst_ptr
, src_ptr
, numptrs
* xfs_btree_ptr_len(cur
));
1074 * Shift keys one index left/right inside a single btree block.
1077 xfs_btree_shift_keys(
1078 struct xfs_btree_cur
*cur
,
1079 union xfs_btree_key
*key
,
1085 ASSERT(numkeys
>= 0);
1086 ASSERT(dir
== 1 || dir
== -1);
1088 dst_key
= (char *)key
+ (dir
* cur
->bc_ops
->key_len
);
1089 memmove(dst_key
, key
, numkeys
* cur
->bc_ops
->key_len
);
1093 * Shift records one index left/right inside a single btree block.
1096 xfs_btree_shift_recs(
1097 struct xfs_btree_cur
*cur
,
1098 union xfs_btree_rec
*rec
,
1104 ASSERT(numrecs
>= 0);
1105 ASSERT(dir
== 1 || dir
== -1);
1107 dst_rec
= (char *)rec
+ (dir
* cur
->bc_ops
->rec_len
);
1108 memmove(dst_rec
, rec
, numrecs
* cur
->bc_ops
->rec_len
);
1112 * Shift block pointers one index left/right inside a single btree block.
1115 xfs_btree_shift_ptrs(
1116 struct xfs_btree_cur
*cur
,
1117 union xfs_btree_ptr
*ptr
,
1123 ASSERT(numptrs
>= 0);
1124 ASSERT(dir
== 1 || dir
== -1);
1126 dst_ptr
= (char *)ptr
+ (dir
* xfs_btree_ptr_len(cur
));
1127 memmove(dst_ptr
, ptr
, numptrs
* xfs_btree_ptr_len(cur
));
1131 * Log key values from the btree block.
1135 struct xfs_btree_cur
*cur
,
1140 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
1141 XFS_BTREE_TRACE_ARGBII(cur
, bp
, first
, last
);
1144 xfs_trans_log_buf(cur
->bc_tp
, bp
,
1145 xfs_btree_key_offset(cur
, first
),
1146 xfs_btree_key_offset(cur
, last
+ 1) - 1);
1148 xfs_trans_log_inode(cur
->bc_tp
, cur
->bc_private
.b
.ip
,
1149 xfs_ilog_fbroot(cur
->bc_private
.b
.whichfork
));
1152 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
1156 * Log record values from the btree block.
1160 struct xfs_btree_cur
*cur
,
1165 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
1166 XFS_BTREE_TRACE_ARGBII(cur
, bp
, first
, last
);
1168 xfs_trans_log_buf(cur
->bc_tp
, bp
,
1169 xfs_btree_rec_offset(cur
, first
),
1170 xfs_btree_rec_offset(cur
, last
+ 1) - 1);
1172 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
1176 * Log block pointer fields from a btree block (nonleaf).
1180 struct xfs_btree_cur
*cur
, /* btree cursor */
1181 struct xfs_buf
*bp
, /* buffer containing btree block */
1182 int first
, /* index of first pointer to log */
1183 int last
) /* index of last pointer to log */
1185 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
1186 XFS_BTREE_TRACE_ARGBII(cur
, bp
, first
, last
);
1189 struct xfs_btree_block
*block
= XFS_BUF_TO_BLOCK(bp
);
1190 int level
= xfs_btree_get_level(block
);
1192 xfs_trans_log_buf(cur
->bc_tp
, bp
,
1193 xfs_btree_ptr_offset(cur
, first
, level
),
1194 xfs_btree_ptr_offset(cur
, last
+ 1, level
) - 1);
1196 xfs_trans_log_inode(cur
->bc_tp
, cur
->bc_private
.b
.ip
,
1197 xfs_ilog_fbroot(cur
->bc_private
.b
.whichfork
));
1200 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
1204 * Log fields from a btree block header.
1207 xfs_btree_log_block(
1208 struct xfs_btree_cur
*cur
, /* btree cursor */
1209 struct xfs_buf
*bp
, /* buffer containing btree block */
1210 int fields
) /* mask of fields: XFS_BB_... */
1212 int first
; /* first byte offset logged */
1213 int last
; /* last byte offset logged */
1214 static const short soffsets
[] = { /* table of offsets (short) */
1215 offsetof(struct xfs_btree_block
, bb_magic
),
1216 offsetof(struct xfs_btree_block
, bb_level
),
1217 offsetof(struct xfs_btree_block
, bb_numrecs
),
1218 offsetof(struct xfs_btree_block
, bb_u
.s
.bb_leftsib
),
1219 offsetof(struct xfs_btree_block
, bb_u
.s
.bb_rightsib
),
1220 XFS_BTREE_SBLOCK_LEN
1222 static const short loffsets
[] = { /* table of offsets (long) */
1223 offsetof(struct xfs_btree_block
, bb_magic
),
1224 offsetof(struct xfs_btree_block
, bb_level
),
1225 offsetof(struct xfs_btree_block
, bb_numrecs
),
1226 offsetof(struct xfs_btree_block
, bb_u
.l
.bb_leftsib
),
1227 offsetof(struct xfs_btree_block
, bb_u
.l
.bb_rightsib
),
1228 XFS_BTREE_LBLOCK_LEN
1231 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
1232 XFS_BTREE_TRACE_ARGBI(cur
, bp
, fields
);
1235 xfs_btree_offsets(fields
,
1236 (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
) ?
1237 loffsets
: soffsets
,
1238 XFS_BB_NUM_BITS
, &first
, &last
);
1239 xfs_trans_log_buf(cur
->bc_tp
, bp
, first
, last
);
1241 xfs_trans_log_inode(cur
->bc_tp
, cur
->bc_private
.b
.ip
,
1242 xfs_ilog_fbroot(cur
->bc_private
.b
.whichfork
));
1245 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
1249 * Increment cursor by one record at the level.
1250 * For nonzero levels the leaf-ward information is untouched.
1253 xfs_btree_increment(
1254 struct xfs_btree_cur
*cur
,
1256 int *stat
) /* success/failure */
1258 struct xfs_btree_block
*block
;
1259 union xfs_btree_ptr ptr
;
1261 int error
; /* error return value */
1264 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
1265 XFS_BTREE_TRACE_ARGI(cur
, level
);
1267 ASSERT(level
< cur
->bc_nlevels
);
1269 /* Read-ahead to the right at this level. */
1270 xfs_btree_readahead(cur
, level
, XFS_BTCUR_RIGHTRA
);
1272 /* Get a pointer to the btree block. */
1273 block
= xfs_btree_get_block(cur
, level
, &bp
);
1276 error
= xfs_btree_check_block(cur
, block
, level
, bp
);
1281 /* We're done if we remain in the block after the increment. */
1282 if (++cur
->bc_ptrs
[level
] <= xfs_btree_get_numrecs(block
))
1285 /* Fail if we just went off the right edge of the tree. */
1286 xfs_btree_get_sibling(cur
, block
, &ptr
, XFS_BB_RIGHTSIB
);
1287 if (xfs_btree_ptr_is_null(cur
, &ptr
))
1290 XFS_BTREE_STATS_INC(cur
, increment
);
1293 * March up the tree incrementing pointers.
1294 * Stop when we don't go off the right edge of a block.
1296 for (lev
= level
+ 1; lev
< cur
->bc_nlevels
; lev
++) {
1297 block
= xfs_btree_get_block(cur
, lev
, &bp
);
1300 error
= xfs_btree_check_block(cur
, block
, lev
, bp
);
1305 if (++cur
->bc_ptrs
[lev
] <= xfs_btree_get_numrecs(block
))
1308 /* Read-ahead the right block for the next loop. */
1309 xfs_btree_readahead(cur
, lev
, XFS_BTCUR_RIGHTRA
);
1313 * If we went off the root then we are either seriously
1314 * confused or have the tree root in an inode.
1316 if (lev
== cur
->bc_nlevels
) {
1317 if (cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
)
1320 error
= EFSCORRUPTED
;
1323 ASSERT(lev
< cur
->bc_nlevels
);
1326 * Now walk back down the tree, fixing up the cursor's buffer
1327 * pointers and key numbers.
1329 for (block
= xfs_btree_get_block(cur
, lev
, &bp
); lev
> level
; ) {
1330 union xfs_btree_ptr
*ptrp
;
1332 ptrp
= xfs_btree_ptr_addr(cur
, cur
->bc_ptrs
[lev
], block
);
1333 error
= xfs_btree_read_buf_block(cur
, ptrp
, --lev
,
1338 xfs_btree_setbuf(cur
, lev
, bp
);
1339 cur
->bc_ptrs
[lev
] = 1;
1342 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
1347 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
1352 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
1357 * Decrement cursor by one record at the level.
1358 * For nonzero levels the leaf-ward information is untouched.
1361 xfs_btree_decrement(
1362 struct xfs_btree_cur
*cur
,
1364 int *stat
) /* success/failure */
1366 struct xfs_btree_block
*block
;
1368 int error
; /* error return value */
1370 union xfs_btree_ptr ptr
;
1372 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
1373 XFS_BTREE_TRACE_ARGI(cur
, level
);
1375 ASSERT(level
< cur
->bc_nlevels
);
1377 /* Read-ahead to the left at this level. */
1378 xfs_btree_readahead(cur
, level
, XFS_BTCUR_LEFTRA
);
1380 /* We're done if we remain in the block after the decrement. */
1381 if (--cur
->bc_ptrs
[level
] > 0)
1384 /* Get a pointer to the btree block. */
1385 block
= xfs_btree_get_block(cur
, level
, &bp
);
1388 error
= xfs_btree_check_block(cur
, block
, level
, bp
);
1393 /* Fail if we just went off the left edge of the tree. */
1394 xfs_btree_get_sibling(cur
, block
, &ptr
, XFS_BB_LEFTSIB
);
1395 if (xfs_btree_ptr_is_null(cur
, &ptr
))
1398 XFS_BTREE_STATS_INC(cur
, decrement
);
1401 * March up the tree decrementing pointers.
1402 * Stop when we don't go off the left edge of a block.
1404 for (lev
= level
+ 1; lev
< cur
->bc_nlevels
; lev
++) {
1405 if (--cur
->bc_ptrs
[lev
] > 0)
1407 /* Read-ahead the left block for the next loop. */
1408 xfs_btree_readahead(cur
, lev
, XFS_BTCUR_LEFTRA
);
1412 * If we went off the root then we are seriously confused.
1413 * or the root of the tree is in an inode.
1415 if (lev
== cur
->bc_nlevels
) {
1416 if (cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
)
1419 error
= EFSCORRUPTED
;
1422 ASSERT(lev
< cur
->bc_nlevels
);
1425 * Now walk back down the tree, fixing up the cursor's buffer
1426 * pointers and key numbers.
1428 for (block
= xfs_btree_get_block(cur
, lev
, &bp
); lev
> level
; ) {
1429 union xfs_btree_ptr
*ptrp
;
1431 ptrp
= xfs_btree_ptr_addr(cur
, cur
->bc_ptrs
[lev
], block
);
1432 error
= xfs_btree_read_buf_block(cur
, ptrp
, --lev
,
1436 xfs_btree_setbuf(cur
, lev
, bp
);
1437 cur
->bc_ptrs
[lev
] = xfs_btree_get_numrecs(block
);
1440 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
1445 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
1450 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
1455 xfs_btree_lookup_get_block(
1456 struct xfs_btree_cur
*cur
, /* btree cursor */
1457 int level
, /* level in the btree */
1458 union xfs_btree_ptr
*pp
, /* ptr to btree block */
1459 struct xfs_btree_block
**blkp
) /* return btree block */
1461 struct xfs_buf
*bp
; /* buffer pointer for btree block */
1464 /* special case the root block if in an inode */
1465 if ((cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
) &&
1466 (level
== cur
->bc_nlevels
- 1)) {
1467 *blkp
= xfs_btree_get_iroot(cur
);
1472 * If the old buffer at this level for the disk address we are
1473 * looking for re-use it.
1475 * Otherwise throw it away and get a new one.
1477 bp
= cur
->bc_bufs
[level
];
1478 if (bp
&& XFS_BUF_ADDR(bp
) == xfs_btree_ptr_to_daddr(cur
, pp
)) {
1479 *blkp
= XFS_BUF_TO_BLOCK(bp
);
1483 error
= xfs_btree_read_buf_block(cur
, pp
, level
, 0, blkp
, &bp
);
1487 xfs_btree_setbuf(cur
, level
, bp
);
1492 * Get current search key. For level 0 we don't actually have a key
1493 * structure so we make one up from the record. For all other levels
1494 * we just return the right key.
1496 STATIC
union xfs_btree_key
*
1497 xfs_lookup_get_search_key(
1498 struct xfs_btree_cur
*cur
,
1501 struct xfs_btree_block
*block
,
1502 union xfs_btree_key
*kp
)
1505 cur
->bc_ops
->init_key_from_rec(kp
,
1506 xfs_btree_rec_addr(cur
, keyno
, block
));
1510 return xfs_btree_key_addr(cur
, keyno
, block
);
1514 * Lookup the record. The cursor is made to point to it, based on dir.
1515 * Return 0 if can't find any such record, 1 for success.
1519 struct xfs_btree_cur
*cur
, /* btree cursor */
1520 xfs_lookup_t dir
, /* <=, ==, or >= */
1521 int *stat
) /* success/failure */
1523 struct xfs_btree_block
*block
; /* current btree block */
1524 __int64_t diff
; /* difference for the current key */
1525 int error
; /* error return value */
1526 int keyno
; /* current key number */
1527 int level
; /* level in the btree */
1528 union xfs_btree_ptr
*pp
; /* ptr to btree block */
1529 union xfs_btree_ptr ptr
; /* ptr to btree block */
1531 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
1532 XFS_BTREE_TRACE_ARGI(cur
, dir
);
1534 XFS_BTREE_STATS_INC(cur
, lookup
);
1539 /* initialise start pointer from cursor */
1540 cur
->bc_ops
->init_ptr_from_cur(cur
, &ptr
);
1544 * Iterate over each level in the btree, starting at the root.
1545 * For each level above the leaves, find the key we need, based
1546 * on the lookup record, then follow the corresponding block
1547 * pointer down to the next level.
1549 for (level
= cur
->bc_nlevels
- 1, diff
= 1; level
>= 0; level
--) {
1550 /* Get the block we need to do the lookup on. */
1551 error
= xfs_btree_lookup_get_block(cur
, level
, pp
, &block
);
1557 * If we already had a key match at a higher level, we
1558 * know we need to use the first entry in this block.
1562 /* Otherwise search this block. Do a binary search. */
1564 int high
; /* high entry number */
1565 int low
; /* low entry number */
1567 /* Set low and high entry numbers, 1-based. */
1569 high
= xfs_btree_get_numrecs(block
);
1571 /* Block is empty, must be an empty leaf. */
1572 ASSERT(level
== 0 && cur
->bc_nlevels
== 1);
1574 cur
->bc_ptrs
[0] = dir
!= XFS_LOOKUP_LE
;
1575 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
1580 /* Binary search the block. */
1581 while (low
<= high
) {
1582 union xfs_btree_key key
;
1583 union xfs_btree_key
*kp
;
1585 XFS_BTREE_STATS_INC(cur
, compare
);
1587 /* keyno is average of low and high. */
1588 keyno
= (low
+ high
) >> 1;
1590 /* Get current search key */
1591 kp
= xfs_lookup_get_search_key(cur
, level
,
1592 keyno
, block
, &key
);
1595 * Compute difference to get next direction:
1596 * - less than, move right
1597 * - greater than, move left
1598 * - equal, we're done
1600 diff
= cur
->bc_ops
->key_diff(cur
, kp
);
1611 * If there are more levels, set up for the next level
1612 * by getting the block number and filling in the cursor.
1616 * If we moved left, need the previous key number,
1617 * unless there isn't one.
1619 if (diff
> 0 && --keyno
< 1)
1621 pp
= xfs_btree_ptr_addr(cur
, keyno
, block
);
1624 error
= xfs_btree_check_ptr(cur
, pp
, 0, level
);
1628 cur
->bc_ptrs
[level
] = keyno
;
1632 /* Done with the search. See if we need to adjust the results. */
1633 if (dir
!= XFS_LOOKUP_LE
&& diff
< 0) {
1636 * If ge search and we went off the end of the block, but it's
1637 * not the last block, we're in the wrong block.
1639 xfs_btree_get_sibling(cur
, block
, &ptr
, XFS_BB_RIGHTSIB
);
1640 if (dir
== XFS_LOOKUP_GE
&&
1641 keyno
> xfs_btree_get_numrecs(block
) &&
1642 !xfs_btree_ptr_is_null(cur
, &ptr
)) {
1645 cur
->bc_ptrs
[0] = keyno
;
1646 error
= xfs_btree_increment(cur
, 0, &i
);
1649 XFS_WANT_CORRUPTED_RETURN(i
== 1);
1650 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
1654 } else if (dir
== XFS_LOOKUP_LE
&& diff
> 0)
1656 cur
->bc_ptrs
[0] = keyno
;
1658 /* Return if we succeeded or not. */
1659 if (keyno
== 0 || keyno
> xfs_btree_get_numrecs(block
))
1661 else if (dir
!= XFS_LOOKUP_EQ
|| diff
== 0)
1665 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
1669 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
1674 * Update keys at all levels from here to the root along the cursor's path.
1678 struct xfs_btree_cur
*cur
,
1679 union xfs_btree_key
*keyp
,
1682 struct xfs_btree_block
*block
;
1684 union xfs_btree_key
*kp
;
1687 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
1688 XFS_BTREE_TRACE_ARGIK(cur
, level
, keyp
);
1690 ASSERT(!(cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
) || level
>= 1);
1693 * Go up the tree from this level toward the root.
1694 * At each level, update the key value to the value input.
1695 * Stop when we reach a level where the cursor isn't pointing
1696 * at the first entry in the block.
1698 for (ptr
= 1; ptr
== 1 && level
< cur
->bc_nlevels
; level
++) {
1702 block
= xfs_btree_get_block(cur
, level
, &bp
);
1704 error
= xfs_btree_check_block(cur
, block
, level
, bp
);
1706 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
1710 ptr
= cur
->bc_ptrs
[level
];
1711 kp
= xfs_btree_key_addr(cur
, ptr
, block
);
1712 xfs_btree_copy_keys(cur
, kp
, keyp
, 1);
1713 xfs_btree_log_keys(cur
, bp
, ptr
, ptr
);
1716 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
1721 * Update the record referred to by cur to the value in the
1722 * given record. This either works (return 0) or gets an
1723 * EFSCORRUPTED error.
1727 struct xfs_btree_cur
*cur
,
1728 union xfs_btree_rec
*rec
)
1730 struct xfs_btree_block
*block
;
1734 union xfs_btree_rec
*rp
;
1736 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
1737 XFS_BTREE_TRACE_ARGR(cur
, rec
);
1739 /* Pick up the current block. */
1740 block
= xfs_btree_get_block(cur
, 0, &bp
);
1743 error
= xfs_btree_check_block(cur
, block
, 0, bp
);
1747 /* Get the address of the rec to be updated. */
1748 ptr
= cur
->bc_ptrs
[0];
1749 rp
= xfs_btree_rec_addr(cur
, ptr
, block
);
1751 /* Fill in the new contents and log them. */
1752 xfs_btree_copy_recs(cur
, rp
, rec
, 1);
1753 xfs_btree_log_recs(cur
, bp
, ptr
, ptr
);
1756 * If we are tracking the last record in the tree and
1757 * we are at the far right edge of the tree, update it.
1759 if (xfs_btree_is_lastrec(cur
, block
, 0)) {
1760 cur
->bc_ops
->update_lastrec(cur
, block
, rec
,
1761 ptr
, LASTREC_UPDATE
);
1764 /* Updating first rec in leaf. Pass new key value up to our parent. */
1766 union xfs_btree_key key
;
1768 cur
->bc_ops
->init_key_from_rec(&key
, rec
);
1769 error
= xfs_btree_updkey(cur
, &key
, 1);
1774 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
1778 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
1783 * Move 1 record left from cur/level if possible.
1784 * Update cur to reflect the new path.
1786 STATIC
int /* error */
1788 struct xfs_btree_cur
*cur
,
1790 int *stat
) /* success/failure */
1792 union xfs_btree_key key
; /* btree key */
1793 struct xfs_buf
*lbp
; /* left buffer pointer */
1794 struct xfs_btree_block
*left
; /* left btree block */
1795 int lrecs
; /* left record count */
1796 struct xfs_buf
*rbp
; /* right buffer pointer */
1797 struct xfs_btree_block
*right
; /* right btree block */
1798 int rrecs
; /* right record count */
1799 union xfs_btree_ptr lptr
; /* left btree pointer */
1800 union xfs_btree_key
*rkp
= NULL
; /* right btree key */
1801 union xfs_btree_ptr
*rpp
= NULL
; /* right address pointer */
1802 union xfs_btree_rec
*rrp
= NULL
; /* right record pointer */
1803 int error
; /* error return value */
1805 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
1806 XFS_BTREE_TRACE_ARGI(cur
, level
);
1808 if ((cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
) &&
1809 level
== cur
->bc_nlevels
- 1)
1812 /* Set up variables for this block as "right". */
1813 right
= xfs_btree_get_block(cur
, level
, &rbp
);
1816 error
= xfs_btree_check_block(cur
, right
, level
, rbp
);
1821 /* If we've got no left sibling then we can't shift an entry left. */
1822 xfs_btree_get_sibling(cur
, right
, &lptr
, XFS_BB_LEFTSIB
);
1823 if (xfs_btree_ptr_is_null(cur
, &lptr
))
1827 * If the cursor entry is the one that would be moved, don't
1828 * do it... it's too complicated.
1830 if (cur
->bc_ptrs
[level
] <= 1)
1833 /* Set up the left neighbor as "left". */
1834 error
= xfs_btree_read_buf_block(cur
, &lptr
, level
, 0, &left
, &lbp
);
1838 /* If it's full, it can't take another entry. */
1839 lrecs
= xfs_btree_get_numrecs(left
);
1840 if (lrecs
== cur
->bc_ops
->get_maxrecs(cur
, level
))
1843 rrecs
= xfs_btree_get_numrecs(right
);
1846 * We add one entry to the left side and remove one for the right side.
1847 * Account for it here, the changes will be updated on disk and logged
1853 XFS_BTREE_STATS_INC(cur
, lshift
);
1854 XFS_BTREE_STATS_ADD(cur
, moves
, 1);
1857 * If non-leaf, copy a key and a ptr to the left block.
1858 * Log the changes to the left block.
1861 /* It's a non-leaf. Move keys and pointers. */
1862 union xfs_btree_key
*lkp
; /* left btree key */
1863 union xfs_btree_ptr
*lpp
; /* left address pointer */
1865 lkp
= xfs_btree_key_addr(cur
, lrecs
, left
);
1866 rkp
= xfs_btree_key_addr(cur
, 1, right
);
1868 lpp
= xfs_btree_ptr_addr(cur
, lrecs
, left
);
1869 rpp
= xfs_btree_ptr_addr(cur
, 1, right
);
1871 error
= xfs_btree_check_ptr(cur
, rpp
, 0, level
);
1875 xfs_btree_copy_keys(cur
, lkp
, rkp
, 1);
1876 xfs_btree_copy_ptrs(cur
, lpp
, rpp
, 1);
1878 xfs_btree_log_keys(cur
, lbp
, lrecs
, lrecs
);
1879 xfs_btree_log_ptrs(cur
, lbp
, lrecs
, lrecs
);
1881 ASSERT(cur
->bc_ops
->keys_inorder(cur
,
1882 xfs_btree_key_addr(cur
, lrecs
- 1, left
), lkp
));
1884 /* It's a leaf. Move records. */
1885 union xfs_btree_rec
*lrp
; /* left record pointer */
1887 lrp
= xfs_btree_rec_addr(cur
, lrecs
, left
);
1888 rrp
= xfs_btree_rec_addr(cur
, 1, right
);
1890 xfs_btree_copy_recs(cur
, lrp
, rrp
, 1);
1891 xfs_btree_log_recs(cur
, lbp
, lrecs
, lrecs
);
1893 ASSERT(cur
->bc_ops
->recs_inorder(cur
,
1894 xfs_btree_rec_addr(cur
, lrecs
- 1, left
), lrp
));
1897 xfs_btree_set_numrecs(left
, lrecs
);
1898 xfs_btree_log_block(cur
, lbp
, XFS_BB_NUMRECS
);
1900 xfs_btree_set_numrecs(right
, rrecs
);
1901 xfs_btree_log_block(cur
, rbp
, XFS_BB_NUMRECS
);
1904 * Slide the contents of right down one entry.
1906 XFS_BTREE_STATS_ADD(cur
, moves
, rrecs
- 1);
1908 /* It's a nonleaf. operate on keys and ptrs */
1910 int i
; /* loop index */
1912 for (i
= 0; i
< rrecs
; i
++) {
1913 error
= xfs_btree_check_ptr(cur
, rpp
, i
+ 1, level
);
1918 xfs_btree_shift_keys(cur
,
1919 xfs_btree_key_addr(cur
, 2, right
),
1921 xfs_btree_shift_ptrs(cur
,
1922 xfs_btree_ptr_addr(cur
, 2, right
),
1925 xfs_btree_log_keys(cur
, rbp
, 1, rrecs
);
1926 xfs_btree_log_ptrs(cur
, rbp
, 1, rrecs
);
1928 /* It's a leaf. operate on records */
1929 xfs_btree_shift_recs(cur
,
1930 xfs_btree_rec_addr(cur
, 2, right
),
1932 xfs_btree_log_recs(cur
, rbp
, 1, rrecs
);
1935 * If it's the first record in the block, we'll need a key
1936 * structure to pass up to the next level (updkey).
1938 cur
->bc_ops
->init_key_from_rec(&key
,
1939 xfs_btree_rec_addr(cur
, 1, right
));
1943 /* Update the parent key values of right. */
1944 error
= xfs_btree_updkey(cur
, rkp
, level
+ 1);
1948 /* Slide the cursor value left one. */
1949 cur
->bc_ptrs
[level
]--;
1951 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
1956 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
1961 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
1966 * Move 1 record right from cur/level if possible.
1967 * Update cur to reflect the new path.
1969 STATIC
int /* error */
1971 struct xfs_btree_cur
*cur
,
1973 int *stat
) /* success/failure */
1975 union xfs_btree_key key
; /* btree key */
1976 struct xfs_buf
*lbp
; /* left buffer pointer */
1977 struct xfs_btree_block
*left
; /* left btree block */
1978 struct xfs_buf
*rbp
; /* right buffer pointer */
1979 struct xfs_btree_block
*right
; /* right btree block */
1980 struct xfs_btree_cur
*tcur
; /* temporary btree cursor */
1981 union xfs_btree_ptr rptr
; /* right block pointer */
1982 union xfs_btree_key
*rkp
; /* right btree key */
1983 int rrecs
; /* right record count */
1984 int lrecs
; /* left record count */
1985 int error
; /* error return value */
1986 int i
; /* loop counter */
1988 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
1989 XFS_BTREE_TRACE_ARGI(cur
, level
);
1991 if ((cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
) &&
1992 (level
== cur
->bc_nlevels
- 1))
1995 /* Set up variables for this block as "left". */
1996 left
= xfs_btree_get_block(cur
, level
, &lbp
);
1999 error
= xfs_btree_check_block(cur
, left
, level
, lbp
);
2004 /* If we've got no right sibling then we can't shift an entry right. */
2005 xfs_btree_get_sibling(cur
, left
, &rptr
, XFS_BB_RIGHTSIB
);
2006 if (xfs_btree_ptr_is_null(cur
, &rptr
))
2010 * If the cursor entry is the one that would be moved, don't
2011 * do it... it's too complicated.
2013 lrecs
= xfs_btree_get_numrecs(left
);
2014 if (cur
->bc_ptrs
[level
] >= lrecs
)
2017 /* Set up the right neighbor as "right". */
2018 error
= xfs_btree_read_buf_block(cur
, &rptr
, level
, 0, &right
, &rbp
);
2022 /* If it's full, it can't take another entry. */
2023 rrecs
= xfs_btree_get_numrecs(right
);
2024 if (rrecs
== cur
->bc_ops
->get_maxrecs(cur
, level
))
2027 XFS_BTREE_STATS_INC(cur
, rshift
);
2028 XFS_BTREE_STATS_ADD(cur
, moves
, rrecs
);
2031 * Make a hole at the start of the right neighbor block, then
2032 * copy the last left block entry to the hole.
2035 /* It's a nonleaf. make a hole in the keys and ptrs */
2036 union xfs_btree_key
*lkp
;
2037 union xfs_btree_ptr
*lpp
;
2038 union xfs_btree_ptr
*rpp
;
2040 lkp
= xfs_btree_key_addr(cur
, lrecs
, left
);
2041 lpp
= xfs_btree_ptr_addr(cur
, lrecs
, left
);
2042 rkp
= xfs_btree_key_addr(cur
, 1, right
);
2043 rpp
= xfs_btree_ptr_addr(cur
, 1, right
);
2046 for (i
= rrecs
- 1; i
>= 0; i
--) {
2047 error
= xfs_btree_check_ptr(cur
, rpp
, i
, level
);
2053 xfs_btree_shift_keys(cur
, rkp
, 1, rrecs
);
2054 xfs_btree_shift_ptrs(cur
, rpp
, 1, rrecs
);
2057 error
= xfs_btree_check_ptr(cur
, lpp
, 0, level
);
2062 /* Now put the new data in, and log it. */
2063 xfs_btree_copy_keys(cur
, rkp
, lkp
, 1);
2064 xfs_btree_copy_ptrs(cur
, rpp
, lpp
, 1);
2066 xfs_btree_log_keys(cur
, rbp
, 1, rrecs
+ 1);
2067 xfs_btree_log_ptrs(cur
, rbp
, 1, rrecs
+ 1);
2069 ASSERT(cur
->bc_ops
->keys_inorder(cur
, rkp
,
2070 xfs_btree_key_addr(cur
, 2, right
)));
2072 /* It's a leaf. make a hole in the records */
2073 union xfs_btree_rec
*lrp
;
2074 union xfs_btree_rec
*rrp
;
2076 lrp
= xfs_btree_rec_addr(cur
, lrecs
, left
);
2077 rrp
= xfs_btree_rec_addr(cur
, 1, right
);
2079 xfs_btree_shift_recs(cur
, rrp
, 1, rrecs
);
2081 /* Now put the new data in, and log it. */
2082 xfs_btree_copy_recs(cur
, rrp
, lrp
, 1);
2083 xfs_btree_log_recs(cur
, rbp
, 1, rrecs
+ 1);
2085 cur
->bc_ops
->init_key_from_rec(&key
, rrp
);
2088 ASSERT(cur
->bc_ops
->recs_inorder(cur
, rrp
,
2089 xfs_btree_rec_addr(cur
, 2, right
)));
2093 * Decrement and log left's numrecs, bump and log right's numrecs.
2095 xfs_btree_set_numrecs(left
, --lrecs
);
2096 xfs_btree_log_block(cur
, lbp
, XFS_BB_NUMRECS
);
2098 xfs_btree_set_numrecs(right
, ++rrecs
);
2099 xfs_btree_log_block(cur
, rbp
, XFS_BB_NUMRECS
);
2102 * Using a temporary cursor, update the parent key values of the
2103 * block on the right.
2105 error
= xfs_btree_dup_cursor(cur
, &tcur
);
2108 i
= xfs_btree_lastrec(tcur
, level
);
2109 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
2111 error
= xfs_btree_increment(tcur
, level
, &i
);
2115 error
= xfs_btree_updkey(tcur
, rkp
, level
+ 1);
2119 xfs_btree_del_cursor(tcur
, XFS_BTREE_NOERROR
);
2121 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
2126 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
2131 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
2135 XFS_BTREE_TRACE_CURSOR(tcur
, XBT_ERROR
);
2136 xfs_btree_del_cursor(tcur
, XFS_BTREE_ERROR
);
2141 * Split cur/level block in half.
2142 * Return new block number and the key to its first
2143 * record (to be inserted into parent).
2145 STATIC
int /* error */
2147 struct xfs_btree_cur
*cur
,
2149 union xfs_btree_ptr
*ptrp
,
2150 union xfs_btree_key
*key
,
2151 struct xfs_btree_cur
**curp
,
2152 int *stat
) /* success/failure */
2154 union xfs_btree_ptr lptr
; /* left sibling block ptr */
2155 struct xfs_buf
*lbp
; /* left buffer pointer */
2156 struct xfs_btree_block
*left
; /* left btree block */
2157 union xfs_btree_ptr rptr
; /* right sibling block ptr */
2158 struct xfs_buf
*rbp
; /* right buffer pointer */
2159 struct xfs_btree_block
*right
; /* right btree block */
2160 union xfs_btree_ptr rrptr
; /* right-right sibling ptr */
2161 struct xfs_buf
*rrbp
; /* right-right buffer pointer */
2162 struct xfs_btree_block
*rrblock
; /* right-right btree block */
2166 int error
; /* error return value */
2171 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
2172 XFS_BTREE_TRACE_ARGIPK(cur
, level
, *ptrp
, key
);
2174 XFS_BTREE_STATS_INC(cur
, split
);
2176 /* Set up left block (current one). */
2177 left
= xfs_btree_get_block(cur
, level
, &lbp
);
2180 error
= xfs_btree_check_block(cur
, left
, level
, lbp
);
2185 xfs_btree_buf_to_ptr(cur
, lbp
, &lptr
);
2187 /* Allocate the new block. If we can't do it, we're toast. Give up. */
2188 error
= cur
->bc_ops
->alloc_block(cur
, &lptr
, &rptr
, 1, stat
);
2193 XFS_BTREE_STATS_INC(cur
, alloc
);
2195 /* Set up the new block as "right". */
2196 error
= xfs_btree_get_buf_block(cur
, &rptr
, 0, &right
, &rbp
);
2200 /* Fill in the btree header for the new right block. */
2201 xfs_btree_init_block(cur
, xfs_btree_get_level(left
), 0, right
);
2204 * Split the entries between the old and the new block evenly.
2205 * Make sure that if there's an odd number of entries now, that
2206 * each new block will have the same number of entries.
2208 lrecs
= xfs_btree_get_numrecs(left
);
2210 if ((lrecs
& 1) && cur
->bc_ptrs
[level
] <= rrecs
+ 1)
2212 src_index
= (lrecs
- rrecs
+ 1);
2214 XFS_BTREE_STATS_ADD(cur
, moves
, rrecs
);
2217 * Copy btree block entries from the left block over to the
2218 * new block, the right. Update the right block and log the
2222 /* It's a non-leaf. Move keys and pointers. */
2223 union xfs_btree_key
*lkp
; /* left btree key */
2224 union xfs_btree_ptr
*lpp
; /* left address pointer */
2225 union xfs_btree_key
*rkp
; /* right btree key */
2226 union xfs_btree_ptr
*rpp
; /* right address pointer */
2228 lkp
= xfs_btree_key_addr(cur
, src_index
, left
);
2229 lpp
= xfs_btree_ptr_addr(cur
, src_index
, left
);
2230 rkp
= xfs_btree_key_addr(cur
, 1, right
);
2231 rpp
= xfs_btree_ptr_addr(cur
, 1, right
);
2234 for (i
= src_index
; i
< rrecs
; i
++) {
2235 error
= xfs_btree_check_ptr(cur
, lpp
, i
, level
);
2241 xfs_btree_copy_keys(cur
, rkp
, lkp
, rrecs
);
2242 xfs_btree_copy_ptrs(cur
, rpp
, lpp
, rrecs
);
2244 xfs_btree_log_keys(cur
, rbp
, 1, rrecs
);
2245 xfs_btree_log_ptrs(cur
, rbp
, 1, rrecs
);
2247 /* Grab the keys to the entries moved to the right block */
2248 xfs_btree_copy_keys(cur
, key
, rkp
, 1);
2250 /* It's a leaf. Move records. */
2251 union xfs_btree_rec
*lrp
; /* left record pointer */
2252 union xfs_btree_rec
*rrp
; /* right record pointer */
2254 lrp
= xfs_btree_rec_addr(cur
, src_index
, left
);
2255 rrp
= xfs_btree_rec_addr(cur
, 1, right
);
2257 xfs_btree_copy_recs(cur
, rrp
, lrp
, rrecs
);
2258 xfs_btree_log_recs(cur
, rbp
, 1, rrecs
);
2260 cur
->bc_ops
->init_key_from_rec(key
,
2261 xfs_btree_rec_addr(cur
, 1, right
));
2266 * Find the left block number by looking in the buffer.
2267 * Adjust numrecs, sibling pointers.
2269 xfs_btree_get_sibling(cur
, left
, &rrptr
, XFS_BB_RIGHTSIB
);
2270 xfs_btree_set_sibling(cur
, right
, &rrptr
, XFS_BB_RIGHTSIB
);
2271 xfs_btree_set_sibling(cur
, right
, &lptr
, XFS_BB_LEFTSIB
);
2272 xfs_btree_set_sibling(cur
, left
, &rptr
, XFS_BB_RIGHTSIB
);
2275 xfs_btree_set_numrecs(left
, lrecs
);
2276 xfs_btree_set_numrecs(right
, xfs_btree_get_numrecs(right
) + rrecs
);
2278 xfs_btree_log_block(cur
, rbp
, XFS_BB_ALL_BITS
);
2279 xfs_btree_log_block(cur
, lbp
, XFS_BB_NUMRECS
| XFS_BB_RIGHTSIB
);
2282 * If there's a block to the new block's right, make that block
2283 * point back to right instead of to left.
2285 if (!xfs_btree_ptr_is_null(cur
, &rrptr
)) {
2286 error
= xfs_btree_read_buf_block(cur
, &rrptr
, level
,
2287 0, &rrblock
, &rrbp
);
2290 xfs_btree_set_sibling(cur
, rrblock
, &rptr
, XFS_BB_LEFTSIB
);
2291 xfs_btree_log_block(cur
, rrbp
, XFS_BB_LEFTSIB
);
2294 * If the cursor is really in the right block, move it there.
2295 * If it's just pointing past the last entry in left, then we'll
2296 * insert there, so don't change anything in that case.
2298 if (cur
->bc_ptrs
[level
] > lrecs
+ 1) {
2299 xfs_btree_setbuf(cur
, level
, rbp
);
2300 cur
->bc_ptrs
[level
] -= lrecs
;
2303 * If there are more levels, we'll need another cursor which refers
2304 * the right block, no matter where this cursor was.
2306 if (level
+ 1 < cur
->bc_nlevels
) {
2307 error
= xfs_btree_dup_cursor(cur
, curp
);
2310 (*curp
)->bc_ptrs
[level
+ 1]++;
2313 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
2317 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
2322 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
2327 * Copy the old inode root contents into a real block and make the
2328 * broot point to it.
2331 xfs_btree_new_iroot(
2332 struct xfs_btree_cur
*cur
, /* btree cursor */
2333 int *logflags
, /* logging flags for inode */
2334 int *stat
) /* return status - 0 fail */
2336 struct xfs_buf
*cbp
; /* buffer for cblock */
2337 struct xfs_btree_block
*block
; /* btree block */
2338 struct xfs_btree_block
*cblock
; /* child btree block */
2339 union xfs_btree_key
*ckp
; /* child key pointer */
2340 union xfs_btree_ptr
*cpp
; /* child ptr pointer */
2341 union xfs_btree_key
*kp
; /* pointer to btree key */
2342 union xfs_btree_ptr
*pp
; /* pointer to block addr */
2343 union xfs_btree_ptr nptr
; /* new block addr */
2344 int level
; /* btree level */
2345 int error
; /* error return code */
2347 int i
; /* loop counter */
2350 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
2351 XFS_BTREE_STATS_INC(cur
, newroot
);
2353 ASSERT(cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
);
2355 level
= cur
->bc_nlevels
- 1;
2357 block
= xfs_btree_get_iroot(cur
);
2358 pp
= xfs_btree_ptr_addr(cur
, 1, block
);
2360 /* Allocate the new block. If we can't do it, we're toast. Give up. */
2361 error
= cur
->bc_ops
->alloc_block(cur
, pp
, &nptr
, 1, stat
);
2365 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
2368 XFS_BTREE_STATS_INC(cur
, alloc
);
2370 /* Copy the root into a real block. */
2371 error
= xfs_btree_get_buf_block(cur
, &nptr
, 0, &cblock
, &cbp
);
2375 memcpy(cblock
, block
, xfs_btree_block_len(cur
));
2377 be16_add_cpu(&block
->bb_level
, 1);
2378 xfs_btree_set_numrecs(block
, 1);
2380 cur
->bc_ptrs
[level
+ 1] = 1;
2382 kp
= xfs_btree_key_addr(cur
, 1, block
);
2383 ckp
= xfs_btree_key_addr(cur
, 1, cblock
);
2384 xfs_btree_copy_keys(cur
, ckp
, kp
, xfs_btree_get_numrecs(cblock
));
2386 cpp
= xfs_btree_ptr_addr(cur
, 1, cblock
);
2388 for (i
= 0; i
< be16_to_cpu(cblock
->bb_numrecs
); i
++) {
2389 error
= xfs_btree_check_ptr(cur
, pp
, i
, level
);
2394 xfs_btree_copy_ptrs(cur
, cpp
, pp
, xfs_btree_get_numrecs(cblock
));
2397 error
= xfs_btree_check_ptr(cur
, &nptr
, 0, level
);
2401 xfs_btree_copy_ptrs(cur
, pp
, &nptr
, 1);
2403 xfs_iroot_realloc(cur
->bc_private
.b
.ip
,
2404 1 - xfs_btree_get_numrecs(cblock
),
2405 cur
->bc_private
.b
.whichfork
);
2407 xfs_btree_setbuf(cur
, level
, cbp
);
2410 * Do all this logging at the end so that
2411 * the root is at the right level.
2413 xfs_btree_log_block(cur
, cbp
, XFS_BB_ALL_BITS
);
2414 xfs_btree_log_keys(cur
, cbp
, 1, be16_to_cpu(cblock
->bb_numrecs
));
2415 xfs_btree_log_ptrs(cur
, cbp
, 1, be16_to_cpu(cblock
->bb_numrecs
));
2418 XFS_ILOG_CORE
| xfs_ilog_fbroot(cur
->bc_private
.b
.whichfork
);
2420 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
2423 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
2428 * Allocate a new root block, fill it in.
2430 STATIC
int /* error */
2432 struct xfs_btree_cur
*cur
, /* btree cursor */
2433 int *stat
) /* success/failure */
2435 struct xfs_btree_block
*block
; /* one half of the old root block */
2436 struct xfs_buf
*bp
; /* buffer containing block */
2437 int error
; /* error return value */
2438 struct xfs_buf
*lbp
; /* left buffer pointer */
2439 struct xfs_btree_block
*left
; /* left btree block */
2440 struct xfs_buf
*nbp
; /* new (root) buffer */
2441 struct xfs_btree_block
*new; /* new (root) btree block */
2442 int nptr
; /* new value for key index, 1 or 2 */
2443 struct xfs_buf
*rbp
; /* right buffer pointer */
2444 struct xfs_btree_block
*right
; /* right btree block */
2445 union xfs_btree_ptr rptr
;
2446 union xfs_btree_ptr lptr
;
2448 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
2449 XFS_BTREE_STATS_INC(cur
, newroot
);
2451 /* initialise our start point from the cursor */
2452 cur
->bc_ops
->init_ptr_from_cur(cur
, &rptr
);
2454 /* Allocate the new block. If we can't do it, we're toast. Give up. */
2455 error
= cur
->bc_ops
->alloc_block(cur
, &rptr
, &lptr
, 1, stat
);
2460 XFS_BTREE_STATS_INC(cur
, alloc
);
2462 /* Set up the new block. */
2463 error
= xfs_btree_get_buf_block(cur
, &lptr
, 0, &new, &nbp
);
2467 /* Set the root in the holding structure increasing the level by 1. */
2468 cur
->bc_ops
->set_root(cur
, &lptr
, 1);
2471 * At the previous root level there are now two blocks: the old root,
2472 * and the new block generated when it was split. We don't know which
2473 * one the cursor is pointing at, so we set up variables "left" and
2474 * "right" for each case.
2476 block
= xfs_btree_get_block(cur
, cur
->bc_nlevels
- 1, &bp
);
2479 error
= xfs_btree_check_block(cur
, block
, cur
->bc_nlevels
- 1, bp
);
2484 xfs_btree_get_sibling(cur
, block
, &rptr
, XFS_BB_RIGHTSIB
);
2485 if (!xfs_btree_ptr_is_null(cur
, &rptr
)) {
2486 /* Our block is left, pick up the right block. */
2488 xfs_btree_buf_to_ptr(cur
, lbp
, &lptr
);
2490 error
= xfs_btree_read_buf_block(cur
, &rptr
,
2491 cur
->bc_nlevels
- 1, 0, &right
, &rbp
);
2497 /* Our block is right, pick up the left block. */
2499 xfs_btree_buf_to_ptr(cur
, rbp
, &rptr
);
2501 xfs_btree_get_sibling(cur
, right
, &lptr
, XFS_BB_LEFTSIB
);
2502 error
= xfs_btree_read_buf_block(cur
, &lptr
,
2503 cur
->bc_nlevels
- 1, 0, &left
, &lbp
);
2509 /* Fill in the new block's btree header and log it. */
2510 xfs_btree_init_block(cur
, cur
->bc_nlevels
, 2, new);
2511 xfs_btree_log_block(cur
, nbp
, XFS_BB_ALL_BITS
);
2512 ASSERT(!xfs_btree_ptr_is_null(cur
, &lptr
) &&
2513 !xfs_btree_ptr_is_null(cur
, &rptr
));
2515 /* Fill in the key data in the new root. */
2516 if (xfs_btree_get_level(left
) > 0) {
2517 xfs_btree_copy_keys(cur
,
2518 xfs_btree_key_addr(cur
, 1, new),
2519 xfs_btree_key_addr(cur
, 1, left
), 1);
2520 xfs_btree_copy_keys(cur
,
2521 xfs_btree_key_addr(cur
, 2, new),
2522 xfs_btree_key_addr(cur
, 1, right
), 1);
2524 cur
->bc_ops
->init_key_from_rec(
2525 xfs_btree_key_addr(cur
, 1, new),
2526 xfs_btree_rec_addr(cur
, 1, left
));
2527 cur
->bc_ops
->init_key_from_rec(
2528 xfs_btree_key_addr(cur
, 2, new),
2529 xfs_btree_rec_addr(cur
, 1, right
));
2531 xfs_btree_log_keys(cur
, nbp
, 1, 2);
2533 /* Fill in the pointer data in the new root. */
2534 xfs_btree_copy_ptrs(cur
,
2535 xfs_btree_ptr_addr(cur
, 1, new), &lptr
, 1);
2536 xfs_btree_copy_ptrs(cur
,
2537 xfs_btree_ptr_addr(cur
, 2, new), &rptr
, 1);
2538 xfs_btree_log_ptrs(cur
, nbp
, 1, 2);
2540 /* Fix up the cursor. */
2541 xfs_btree_setbuf(cur
, cur
->bc_nlevels
, nbp
);
2542 cur
->bc_ptrs
[cur
->bc_nlevels
] = nptr
;
2544 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
2548 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
2551 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
2557 xfs_btree_make_block_unfull(
2558 struct xfs_btree_cur
*cur
, /* btree cursor */
2559 int level
, /* btree level */
2560 int numrecs
,/* # of recs in block */
2561 int *oindex
,/* old tree index */
2562 int *index
, /* new tree index */
2563 union xfs_btree_ptr
*nptr
, /* new btree ptr */
2564 struct xfs_btree_cur
**ncur
, /* new btree cursor */
2565 union xfs_btree_rec
*nrec
, /* new record */
2568 union xfs_btree_key key
; /* new btree key value */
2571 if ((cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
) &&
2572 level
== cur
->bc_nlevels
- 1) {
2573 struct xfs_inode
*ip
= cur
->bc_private
.b
.ip
;
2575 if (numrecs
< cur
->bc_ops
->get_dmaxrecs(cur
, level
)) {
2576 /* A root block that can be made bigger. */
2578 xfs_iroot_realloc(ip
, 1, cur
->bc_private
.b
.whichfork
);
2580 /* A root block that needs replacing */
2583 error
= xfs_btree_new_iroot(cur
, &logflags
, stat
);
2584 if (error
|| *stat
== 0)
2587 xfs_trans_log_inode(cur
->bc_tp
, ip
, logflags
);
2593 /* First, try shifting an entry to the right neighbor. */
2594 error
= xfs_btree_rshift(cur
, level
, stat
);
2598 /* Next, try shifting an entry to the left neighbor. */
2599 error
= xfs_btree_lshift(cur
, level
, stat
);
2604 *oindex
= *index
= cur
->bc_ptrs
[level
];
2609 * Next, try splitting the current block in half.
2611 * If this works we have to re-set our variables because we
2612 * could be in a different block now.
2614 error
= xfs_btree_split(cur
, level
, nptr
, &key
, ncur
, stat
);
2615 if (error
|| *stat
== 0)
2619 *index
= cur
->bc_ptrs
[level
];
2620 cur
->bc_ops
->init_rec_from_key(&key
, nrec
);
2625 * Insert one record/level. Return information to the caller
2626 * allowing the next level up to proceed if necessary.
2630 struct xfs_btree_cur
*cur
, /* btree cursor */
2631 int level
, /* level to insert record at */
2632 union xfs_btree_ptr
*ptrp
, /* i/o: block number inserted */
2633 union xfs_btree_rec
*recp
, /* i/o: record data inserted */
2634 struct xfs_btree_cur
**curp
, /* output: new cursor replacing cur */
2635 int *stat
) /* success/failure */
2637 struct xfs_btree_block
*block
; /* btree block */
2638 struct xfs_buf
*bp
; /* buffer for block */
2639 union xfs_btree_key key
; /* btree key */
2640 union xfs_btree_ptr nptr
; /* new block ptr */
2641 struct xfs_btree_cur
*ncur
; /* new btree cursor */
2642 union xfs_btree_rec nrec
; /* new record count */
2643 int optr
; /* old key/record index */
2644 int ptr
; /* key/record index */
2645 int numrecs
;/* number of records */
2646 int error
; /* error return value */
2651 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
2652 XFS_BTREE_TRACE_ARGIPR(cur
, level
, *ptrp
, recp
);
2657 * If we have an external root pointer, and we've made it to the
2658 * root level, allocate a new root block and we're done.
2660 if (!(cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
) &&
2661 (level
>= cur
->bc_nlevels
)) {
2662 error
= xfs_btree_new_root(cur
, stat
);
2663 xfs_btree_set_ptr_null(cur
, ptrp
);
2665 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
2669 /* If we're off the left edge, return failure. */
2670 ptr
= cur
->bc_ptrs
[level
];
2672 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
2677 /* Make a key out of the record data to be inserted, and save it. */
2678 cur
->bc_ops
->init_key_from_rec(&key
, recp
);
2682 XFS_BTREE_STATS_INC(cur
, insrec
);
2684 /* Get pointers to the btree buffer and block. */
2685 block
= xfs_btree_get_block(cur
, level
, &bp
);
2686 numrecs
= xfs_btree_get_numrecs(block
);
2689 error
= xfs_btree_check_block(cur
, block
, level
, bp
);
2693 /* Check that the new entry is being inserted in the right place. */
2694 if (ptr
<= numrecs
) {
2696 ASSERT(cur
->bc_ops
->recs_inorder(cur
, recp
,
2697 xfs_btree_rec_addr(cur
, ptr
, block
)));
2699 ASSERT(cur
->bc_ops
->keys_inorder(cur
, &key
,
2700 xfs_btree_key_addr(cur
, ptr
, block
)));
2706 * If the block is full, we can't insert the new entry until we
2707 * make the block un-full.
2709 xfs_btree_set_ptr_null(cur
, &nptr
);
2710 if (numrecs
== cur
->bc_ops
->get_maxrecs(cur
, level
)) {
2711 error
= xfs_btree_make_block_unfull(cur
, level
, numrecs
,
2712 &optr
, &ptr
, &nptr
, &ncur
, &nrec
, stat
);
2713 if (error
|| *stat
== 0)
2718 * The current block may have changed if the block was
2719 * previously full and we have just made space in it.
2721 block
= xfs_btree_get_block(cur
, level
, &bp
);
2722 numrecs
= xfs_btree_get_numrecs(block
);
2725 error
= xfs_btree_check_block(cur
, block
, level
, bp
);
2731 * At this point we know there's room for our new entry in the block
2732 * we're pointing at.
2734 XFS_BTREE_STATS_ADD(cur
, moves
, numrecs
- ptr
+ 1);
2737 /* It's a nonleaf. make a hole in the keys and ptrs */
2738 union xfs_btree_key
*kp
;
2739 union xfs_btree_ptr
*pp
;
2741 kp
= xfs_btree_key_addr(cur
, ptr
, block
);
2742 pp
= xfs_btree_ptr_addr(cur
, ptr
, block
);
2745 for (i
= numrecs
- ptr
; i
>= 0; i
--) {
2746 error
= xfs_btree_check_ptr(cur
, pp
, i
, level
);
2752 xfs_btree_shift_keys(cur
, kp
, 1, numrecs
- ptr
+ 1);
2753 xfs_btree_shift_ptrs(cur
, pp
, 1, numrecs
- ptr
+ 1);
2756 error
= xfs_btree_check_ptr(cur
, ptrp
, 0, level
);
2761 /* Now put the new data in, bump numrecs and log it. */
2762 xfs_btree_copy_keys(cur
, kp
, &key
, 1);
2763 xfs_btree_copy_ptrs(cur
, pp
, ptrp
, 1);
2765 xfs_btree_set_numrecs(block
, numrecs
);
2766 xfs_btree_log_ptrs(cur
, bp
, ptr
, numrecs
);
2767 xfs_btree_log_keys(cur
, bp
, ptr
, numrecs
);
2769 if (ptr
< numrecs
) {
2770 ASSERT(cur
->bc_ops
->keys_inorder(cur
, kp
,
2771 xfs_btree_key_addr(cur
, ptr
+ 1, block
)));
2775 /* It's a leaf. make a hole in the records */
2776 union xfs_btree_rec
*rp
;
2778 rp
= xfs_btree_rec_addr(cur
, ptr
, block
);
2780 xfs_btree_shift_recs(cur
, rp
, 1, numrecs
- ptr
+ 1);
2782 /* Now put the new data in, bump numrecs and log it. */
2783 xfs_btree_copy_recs(cur
, rp
, recp
, 1);
2784 xfs_btree_set_numrecs(block
, ++numrecs
);
2785 xfs_btree_log_recs(cur
, bp
, ptr
, numrecs
);
2787 if (ptr
< numrecs
) {
2788 ASSERT(cur
->bc_ops
->recs_inorder(cur
, rp
,
2789 xfs_btree_rec_addr(cur
, ptr
+ 1, block
)));
2794 /* Log the new number of records in the btree header. */
2795 xfs_btree_log_block(cur
, bp
, XFS_BB_NUMRECS
);
2797 /* If we inserted at the start of a block, update the parents' keys. */
2799 error
= xfs_btree_updkey(cur
, &key
, level
+ 1);
2805 * If we are tracking the last record in the tree and
2806 * we are at the far right edge of the tree, update it.
2808 if (xfs_btree_is_lastrec(cur
, block
, level
)) {
2809 cur
->bc_ops
->update_lastrec(cur
, block
, recp
,
2810 ptr
, LASTREC_INSREC
);
2814 * Return the new block number, if any.
2815 * If there is one, give back a record value and a cursor too.
2818 if (!xfs_btree_ptr_is_null(cur
, &nptr
)) {
2823 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
2828 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
2833 * Insert the record at the point referenced by cur.
2835 * A multi-level split of the tree on insert will invalidate the original
2836 * cursor. All callers of this function should assume that the cursor is
2837 * no longer valid and revalidate it.
2841 struct xfs_btree_cur
*cur
,
2844 int error
; /* error return value */
2845 int i
; /* result value, 0 for failure */
2846 int level
; /* current level number in btree */
2847 union xfs_btree_ptr nptr
; /* new block number (split result) */
2848 struct xfs_btree_cur
*ncur
; /* new cursor (split result) */
2849 struct xfs_btree_cur
*pcur
; /* previous level's cursor */
2850 union xfs_btree_rec rec
; /* record to insert */
2856 xfs_btree_set_ptr_null(cur
, &nptr
);
2857 cur
->bc_ops
->init_rec_from_cur(cur
, &rec
);
2860 * Loop going up the tree, starting at the leaf level.
2861 * Stop when we don't get a split block, that must mean that
2862 * the insert is finished with this level.
2866 * Insert nrec/nptr into this level of the tree.
2867 * Note if we fail, nptr will be null.
2869 error
= xfs_btree_insrec(pcur
, level
, &nptr
, &rec
, &ncur
, &i
);
2872 xfs_btree_del_cursor(pcur
, XFS_BTREE_ERROR
);
2876 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
2880 * See if the cursor we just used is trash.
2881 * Can't trash the caller's cursor, but otherwise we should
2882 * if ncur is a new cursor or we're about to be done.
2885 (ncur
|| xfs_btree_ptr_is_null(cur
, &nptr
))) {
2886 /* Save the state from the cursor before we trash it */
2887 if (cur
->bc_ops
->update_cursor
)
2888 cur
->bc_ops
->update_cursor(pcur
, cur
);
2889 cur
->bc_nlevels
= pcur
->bc_nlevels
;
2890 xfs_btree_del_cursor(pcur
, XFS_BTREE_NOERROR
);
2892 /* If we got a new cursor, switch to it. */
2897 } while (!xfs_btree_ptr_is_null(cur
, &nptr
));
2899 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
2903 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
2908 * Try to merge a non-leaf block back into the inode root.
2910 * Note: the killroot names comes from the fact that we're effectively
2911 * killing the old root block. But because we can't just delete the
2912 * inode we have to copy the single block it was pointing to into the
2916 xfs_btree_kill_iroot(
2917 struct xfs_btree_cur
*cur
)
2919 int whichfork
= cur
->bc_private
.b
.whichfork
;
2920 struct xfs_inode
*ip
= cur
->bc_private
.b
.ip
;
2921 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
2922 struct xfs_btree_block
*block
;
2923 struct xfs_btree_block
*cblock
;
2924 union xfs_btree_key
*kp
;
2925 union xfs_btree_key
*ckp
;
2926 union xfs_btree_ptr
*pp
;
2927 union xfs_btree_ptr
*cpp
;
2928 struct xfs_buf
*cbp
;
2933 union xfs_btree_ptr ptr
;
2937 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
2939 ASSERT(cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
);
2940 ASSERT(cur
->bc_nlevels
> 1);
2943 * Don't deal with the root block needs to be a leaf case.
2944 * We're just going to turn the thing back into extents anyway.
2946 level
= cur
->bc_nlevels
- 1;
2951 * Give up if the root has multiple children.
2953 block
= xfs_btree_get_iroot(cur
);
2954 if (xfs_btree_get_numrecs(block
) != 1)
2957 cblock
= xfs_btree_get_block(cur
, level
- 1, &cbp
);
2958 numrecs
= xfs_btree_get_numrecs(cblock
);
2961 * Only do this if the next level will fit.
2962 * Then the data must be copied up to the inode,
2963 * instead of freeing the root you free the next level.
2965 if (numrecs
> cur
->bc_ops
->get_dmaxrecs(cur
, level
))
2968 XFS_BTREE_STATS_INC(cur
, killroot
);
2971 xfs_btree_get_sibling(cur
, block
, &ptr
, XFS_BB_LEFTSIB
);
2972 ASSERT(xfs_btree_ptr_is_null(cur
, &ptr
));
2973 xfs_btree_get_sibling(cur
, block
, &ptr
, XFS_BB_RIGHTSIB
);
2974 ASSERT(xfs_btree_ptr_is_null(cur
, &ptr
));
2977 index
= numrecs
- cur
->bc_ops
->get_maxrecs(cur
, level
);
2979 xfs_iroot_realloc(cur
->bc_private
.b
.ip
, index
,
2980 cur
->bc_private
.b
.whichfork
);
2981 block
= ifp
->if_broot
;
2984 be16_add_cpu(&block
->bb_numrecs
, index
);
2985 ASSERT(block
->bb_numrecs
== cblock
->bb_numrecs
);
2987 kp
= xfs_btree_key_addr(cur
, 1, block
);
2988 ckp
= xfs_btree_key_addr(cur
, 1, cblock
);
2989 xfs_btree_copy_keys(cur
, kp
, ckp
, numrecs
);
2991 pp
= xfs_btree_ptr_addr(cur
, 1, block
);
2992 cpp
= xfs_btree_ptr_addr(cur
, 1, cblock
);
2994 for (i
= 0; i
< numrecs
; i
++) {
2997 error
= xfs_btree_check_ptr(cur
, cpp
, i
, level
- 1);
2999 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
3004 xfs_btree_copy_ptrs(cur
, pp
, cpp
, numrecs
);
3006 cur
->bc_ops
->free_block(cur
, cbp
);
3007 XFS_BTREE_STATS_INC(cur
, free
);
3009 cur
->bc_bufs
[level
- 1] = NULL
;
3010 be16_add_cpu(&block
->bb_level
, -1);
3011 xfs_trans_log_inode(cur
->bc_tp
, ip
,
3012 XFS_ILOG_CORE
| xfs_ilog_fbroot(cur
->bc_private
.b
.whichfork
));
3015 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
3020 xfs_btree_dec_cursor(
3021 struct xfs_btree_cur
*cur
,
3029 error
= xfs_btree_decrement(cur
, level
, &i
);
3034 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
3040 * Single level of the btree record deletion routine.
3041 * Delete record pointed to by cur/level.
3042 * Remove the record from its block then rebalance the tree.
3043 * Return 0 for error, 1 for done, 2 to go on to the next level.
3045 STATIC
int /* error */
3047 struct xfs_btree_cur
*cur
, /* btree cursor */
3048 int level
, /* level removing record from */
3049 int *stat
) /* fail/done/go-on */
3051 struct xfs_btree_block
*block
; /* btree block */
3052 union xfs_btree_ptr cptr
; /* current block ptr */
3053 struct xfs_buf
*bp
; /* buffer for block */
3054 int error
; /* error return value */
3055 int i
; /* loop counter */
3056 union xfs_btree_key key
; /* storage for keyp */
3057 union xfs_btree_key
*keyp
= &key
; /* passed to the next level */
3058 union xfs_btree_ptr lptr
; /* left sibling block ptr */
3059 struct xfs_buf
*lbp
; /* left buffer pointer */
3060 struct xfs_btree_block
*left
; /* left btree block */
3061 int lrecs
= 0; /* left record count */
3062 int ptr
; /* key/record index */
3063 union xfs_btree_ptr rptr
; /* right sibling block ptr */
3064 struct xfs_buf
*rbp
; /* right buffer pointer */
3065 struct xfs_btree_block
*right
; /* right btree block */
3066 struct xfs_btree_block
*rrblock
; /* right-right btree block */
3067 struct xfs_buf
*rrbp
; /* right-right buffer pointer */
3068 int rrecs
= 0; /* right record count */
3069 struct xfs_btree_cur
*tcur
; /* temporary btree cursor */
3070 int numrecs
; /* temporary numrec count */
3072 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
3073 XFS_BTREE_TRACE_ARGI(cur
, level
);
3077 /* Get the index of the entry being deleted, check for nothing there. */
3078 ptr
= cur
->bc_ptrs
[level
];
3080 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
3085 /* Get the buffer & block containing the record or key/ptr. */
3086 block
= xfs_btree_get_block(cur
, level
, &bp
);
3087 numrecs
= xfs_btree_get_numrecs(block
);
3090 error
= xfs_btree_check_block(cur
, block
, level
, bp
);
3095 /* Fail if we're off the end of the block. */
3096 if (ptr
> numrecs
) {
3097 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
3102 XFS_BTREE_STATS_INC(cur
, delrec
);
3103 XFS_BTREE_STATS_ADD(cur
, moves
, numrecs
- ptr
);
3105 /* Excise the entries being deleted. */
3107 /* It's a nonleaf. operate on keys and ptrs */
3108 union xfs_btree_key
*lkp
;
3109 union xfs_btree_ptr
*lpp
;
3111 lkp
= xfs_btree_key_addr(cur
, ptr
+ 1, block
);
3112 lpp
= xfs_btree_ptr_addr(cur
, ptr
+ 1, block
);
3115 for (i
= 0; i
< numrecs
- ptr
; i
++) {
3116 error
= xfs_btree_check_ptr(cur
, lpp
, i
, level
);
3122 if (ptr
< numrecs
) {
3123 xfs_btree_shift_keys(cur
, lkp
, -1, numrecs
- ptr
);
3124 xfs_btree_shift_ptrs(cur
, lpp
, -1, numrecs
- ptr
);
3125 xfs_btree_log_keys(cur
, bp
, ptr
, numrecs
- 1);
3126 xfs_btree_log_ptrs(cur
, bp
, ptr
, numrecs
- 1);
3130 * If it's the first record in the block, we'll need to pass a
3131 * key up to the next level (updkey).
3134 keyp
= xfs_btree_key_addr(cur
, 1, block
);
3136 /* It's a leaf. operate on records */
3137 if (ptr
< numrecs
) {
3138 xfs_btree_shift_recs(cur
,
3139 xfs_btree_rec_addr(cur
, ptr
+ 1, block
),
3141 xfs_btree_log_recs(cur
, bp
, ptr
, numrecs
- 1);
3145 * If it's the first record in the block, we'll need a key
3146 * structure to pass up to the next level (updkey).
3149 cur
->bc_ops
->init_key_from_rec(&key
,
3150 xfs_btree_rec_addr(cur
, 1, block
));
3156 * Decrement and log the number of entries in the block.
3158 xfs_btree_set_numrecs(block
, --numrecs
);
3159 xfs_btree_log_block(cur
, bp
, XFS_BB_NUMRECS
);
3162 * If we are tracking the last record in the tree and
3163 * we are at the far right edge of the tree, update it.
3165 if (xfs_btree_is_lastrec(cur
, block
, level
)) {
3166 cur
->bc_ops
->update_lastrec(cur
, block
, NULL
,
3167 ptr
, LASTREC_DELREC
);
3171 * We're at the root level. First, shrink the root block in-memory.
3172 * Try to get rid of the next level down. If we can't then there's
3173 * nothing left to do.
3175 if (level
== cur
->bc_nlevels
- 1) {
3176 if (cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
) {
3177 xfs_iroot_realloc(cur
->bc_private
.b
.ip
, -1,
3178 cur
->bc_private
.b
.whichfork
);
3180 error
= xfs_btree_kill_iroot(cur
);
3184 error
= xfs_btree_dec_cursor(cur
, level
, stat
);
3192 * If this is the root level, and there's only one entry left,
3193 * and it's NOT the leaf level, then we can get rid of this
3196 if (numrecs
== 1 && level
> 0) {
3197 union xfs_btree_ptr
*pp
;
3199 * pp is still set to the first pointer in the block.
3200 * Make it the new root of the btree.
3202 pp
= xfs_btree_ptr_addr(cur
, 1, block
);
3203 error
= cur
->bc_ops
->kill_root(cur
, bp
, level
, pp
);
3206 } else if (level
> 0) {
3207 error
= xfs_btree_dec_cursor(cur
, level
, stat
);
3216 * If we deleted the leftmost entry in the block, update the
3217 * key values above us in the tree.
3220 error
= xfs_btree_updkey(cur
, keyp
, level
+ 1);
3226 * If the number of records remaining in the block is at least
3227 * the minimum, we're done.
3229 if (numrecs
>= cur
->bc_ops
->get_minrecs(cur
, level
)) {
3230 error
= xfs_btree_dec_cursor(cur
, level
, stat
);
3237 * Otherwise, we have to move some records around to keep the
3238 * tree balanced. Look at the left and right sibling blocks to
3239 * see if we can re-balance by moving only one record.
3241 xfs_btree_get_sibling(cur
, block
, &rptr
, XFS_BB_RIGHTSIB
);
3242 xfs_btree_get_sibling(cur
, block
, &lptr
, XFS_BB_LEFTSIB
);
3244 if (cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
) {
3246 * One child of root, need to get a chance to copy its contents
3247 * into the root and delete it. Can't go up to next level,
3248 * there's nothing to delete there.
3250 if (xfs_btree_ptr_is_null(cur
, &rptr
) &&
3251 xfs_btree_ptr_is_null(cur
, &lptr
) &&
3252 level
== cur
->bc_nlevels
- 2) {
3253 error
= xfs_btree_kill_iroot(cur
);
3255 error
= xfs_btree_dec_cursor(cur
, level
, stat
);
3262 ASSERT(!xfs_btree_ptr_is_null(cur
, &rptr
) ||
3263 !xfs_btree_ptr_is_null(cur
, &lptr
));
3266 * Duplicate the cursor so our btree manipulations here won't
3267 * disrupt the next level up.
3269 error
= xfs_btree_dup_cursor(cur
, &tcur
);
3274 * If there's a right sibling, see if it's ok to shift an entry
3277 if (!xfs_btree_ptr_is_null(cur
, &rptr
)) {
3279 * Move the temp cursor to the last entry in the next block.
3280 * Actually any entry but the first would suffice.
3282 i
= xfs_btree_lastrec(tcur
, level
);
3283 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
3285 error
= xfs_btree_increment(tcur
, level
, &i
);
3288 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
3290 i
= xfs_btree_lastrec(tcur
, level
);
3291 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
3293 /* Grab a pointer to the block. */
3294 right
= xfs_btree_get_block(tcur
, level
, &rbp
);
3296 error
= xfs_btree_check_block(tcur
, right
, level
, rbp
);
3300 /* Grab the current block number, for future use. */
3301 xfs_btree_get_sibling(tcur
, right
, &cptr
, XFS_BB_LEFTSIB
);
3304 * If right block is full enough so that removing one entry
3305 * won't make it too empty, and left-shifting an entry out
3306 * of right to us works, we're done.
3308 if (xfs_btree_get_numrecs(right
) - 1 >=
3309 cur
->bc_ops
->get_minrecs(tcur
, level
)) {
3310 error
= xfs_btree_lshift(tcur
, level
, &i
);
3314 ASSERT(xfs_btree_get_numrecs(block
) >=
3315 cur
->bc_ops
->get_minrecs(tcur
, level
));
3317 xfs_btree_del_cursor(tcur
, XFS_BTREE_NOERROR
);
3320 error
= xfs_btree_dec_cursor(cur
, level
, stat
);
3328 * Otherwise, grab the number of records in right for
3329 * future reference, and fix up the temp cursor to point
3330 * to our block again (last record).
3332 rrecs
= xfs_btree_get_numrecs(right
);
3333 if (!xfs_btree_ptr_is_null(cur
, &lptr
)) {
3334 i
= xfs_btree_firstrec(tcur
, level
);
3335 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
3337 error
= xfs_btree_decrement(tcur
, level
, &i
);
3340 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
3345 * If there's a left sibling, see if it's ok to shift an entry
3348 if (!xfs_btree_ptr_is_null(cur
, &lptr
)) {
3350 * Move the temp cursor to the first entry in the
3353 i
= xfs_btree_firstrec(tcur
, level
);
3354 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
3356 error
= xfs_btree_decrement(tcur
, level
, &i
);
3359 i
= xfs_btree_firstrec(tcur
, level
);
3360 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
3362 /* Grab a pointer to the block. */
3363 left
= xfs_btree_get_block(tcur
, level
, &lbp
);
3365 error
= xfs_btree_check_block(cur
, left
, level
, lbp
);
3369 /* Grab the current block number, for future use. */
3370 xfs_btree_get_sibling(tcur
, left
, &cptr
, XFS_BB_RIGHTSIB
);
3373 * If left block is full enough so that removing one entry
3374 * won't make it too empty, and right-shifting an entry out
3375 * of left to us works, we're done.
3377 if (xfs_btree_get_numrecs(left
) - 1 >=
3378 cur
->bc_ops
->get_minrecs(tcur
, level
)) {
3379 error
= xfs_btree_rshift(tcur
, level
, &i
);
3383 ASSERT(xfs_btree_get_numrecs(block
) >=
3384 cur
->bc_ops
->get_minrecs(tcur
, level
));
3385 xfs_btree_del_cursor(tcur
, XFS_BTREE_NOERROR
);
3389 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
3396 * Otherwise, grab the number of records in right for
3399 lrecs
= xfs_btree_get_numrecs(left
);
3402 /* Delete the temp cursor, we're done with it. */
3403 xfs_btree_del_cursor(tcur
, XFS_BTREE_NOERROR
);
3406 /* If here, we need to do a join to keep the tree balanced. */
3407 ASSERT(!xfs_btree_ptr_is_null(cur
, &cptr
));
3409 if (!xfs_btree_ptr_is_null(cur
, &lptr
) &&
3410 lrecs
+ xfs_btree_get_numrecs(block
) <=
3411 cur
->bc_ops
->get_maxrecs(cur
, level
)) {
3413 * Set "right" to be the starting block,
3414 * "left" to be the left neighbor.
3419 error
= xfs_btree_read_buf_block(cur
, &lptr
, level
,
3425 * If that won't work, see if we can join with the right neighbor block.
3427 } else if (!xfs_btree_ptr_is_null(cur
, &rptr
) &&
3428 rrecs
+ xfs_btree_get_numrecs(block
) <=
3429 cur
->bc_ops
->get_maxrecs(cur
, level
)) {
3431 * Set "left" to be the starting block,
3432 * "right" to be the right neighbor.
3437 error
= xfs_btree_read_buf_block(cur
, &rptr
, level
,
3443 * Otherwise, we can't fix the imbalance.
3444 * Just return. This is probably a logic error, but it's not fatal.
3447 error
= xfs_btree_dec_cursor(cur
, level
, stat
);
3453 rrecs
= xfs_btree_get_numrecs(right
);
3454 lrecs
= xfs_btree_get_numrecs(left
);
3457 * We're now going to join "left" and "right" by moving all the stuff
3458 * in "right" to "left" and deleting "right".
3460 XFS_BTREE_STATS_ADD(cur
, moves
, rrecs
);
3462 /* It's a non-leaf. Move keys and pointers. */
3463 union xfs_btree_key
*lkp
; /* left btree key */
3464 union xfs_btree_ptr
*lpp
; /* left address pointer */
3465 union xfs_btree_key
*rkp
; /* right btree key */
3466 union xfs_btree_ptr
*rpp
; /* right address pointer */
3468 lkp
= xfs_btree_key_addr(cur
, lrecs
+ 1, left
);
3469 lpp
= xfs_btree_ptr_addr(cur
, lrecs
+ 1, left
);
3470 rkp
= xfs_btree_key_addr(cur
, 1, right
);
3471 rpp
= xfs_btree_ptr_addr(cur
, 1, right
);
3473 for (i
= 1; i
< rrecs
; i
++) {
3474 error
= xfs_btree_check_ptr(cur
, rpp
, i
, level
);
3479 xfs_btree_copy_keys(cur
, lkp
, rkp
, rrecs
);
3480 xfs_btree_copy_ptrs(cur
, lpp
, rpp
, rrecs
);
3482 xfs_btree_log_keys(cur
, lbp
, lrecs
+ 1, lrecs
+ rrecs
);
3483 xfs_btree_log_ptrs(cur
, lbp
, lrecs
+ 1, lrecs
+ rrecs
);
3485 /* It's a leaf. Move records. */
3486 union xfs_btree_rec
*lrp
; /* left record pointer */
3487 union xfs_btree_rec
*rrp
; /* right record pointer */
3489 lrp
= xfs_btree_rec_addr(cur
, lrecs
+ 1, left
);
3490 rrp
= xfs_btree_rec_addr(cur
, 1, right
);
3492 xfs_btree_copy_recs(cur
, lrp
, rrp
, rrecs
);
3493 xfs_btree_log_recs(cur
, lbp
, lrecs
+ 1, lrecs
+ rrecs
);
3496 XFS_BTREE_STATS_INC(cur
, join
);
3499 * Fix up the number of records and right block pointer in the
3500 * surviving block, and log it.
3502 xfs_btree_set_numrecs(left
, lrecs
+ rrecs
);
3503 xfs_btree_get_sibling(cur
, right
, &cptr
, XFS_BB_RIGHTSIB
),
3504 xfs_btree_set_sibling(cur
, left
, &cptr
, XFS_BB_RIGHTSIB
);
3505 xfs_btree_log_block(cur
, lbp
, XFS_BB_NUMRECS
| XFS_BB_RIGHTSIB
);
3507 /* If there is a right sibling, point it to the remaining block. */
3508 xfs_btree_get_sibling(cur
, left
, &cptr
, XFS_BB_RIGHTSIB
);
3509 if (!xfs_btree_ptr_is_null(cur
, &cptr
)) {
3510 error
= xfs_btree_read_buf_block(cur
, &cptr
, level
,
3511 0, &rrblock
, &rrbp
);
3514 xfs_btree_set_sibling(cur
, rrblock
, &lptr
, XFS_BB_LEFTSIB
);
3515 xfs_btree_log_block(cur
, rrbp
, XFS_BB_LEFTSIB
);
3518 /* Free the deleted block. */
3519 error
= cur
->bc_ops
->free_block(cur
, rbp
);
3522 XFS_BTREE_STATS_INC(cur
, free
);
3525 * If we joined with the left neighbor, set the buffer in the
3526 * cursor to the left block, and fix up the index.
3529 cur
->bc_bufs
[level
] = lbp
;
3530 cur
->bc_ptrs
[level
] += lrecs
;
3531 cur
->bc_ra
[level
] = 0;
3534 * If we joined with the right neighbor and there's a level above
3535 * us, increment the cursor at that level.
3537 else if ((cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
) ||
3538 (level
+ 1 < cur
->bc_nlevels
)) {
3539 error
= xfs_btree_increment(cur
, level
+ 1, &i
);
3545 * Readjust the ptr at this level if it's not a leaf, since it's
3546 * still pointing at the deletion point, which makes the cursor
3547 * inconsistent. If this makes the ptr 0, the caller fixes it up.
3548 * We can't use decrement because it would change the next level up.
3551 cur
->bc_ptrs
[level
]--;
3553 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
3554 /* Return value means the next level up has something to do. */
3559 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
3561 xfs_btree_del_cursor(tcur
, XFS_BTREE_ERROR
);
3566 * Delete the record pointed to by cur.
3567 * The cursor refers to the place where the record was (could be inserted)
3568 * when the operation returns.
3572 struct xfs_btree_cur
*cur
,
3573 int *stat
) /* success/failure */
3575 int error
; /* error return value */
3579 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
3582 * Go up the tree, starting at leaf level.
3584 * If 2 is returned then a join was done; go to the next level.
3585 * Otherwise we are done.
3587 for (level
= 0, i
= 2; i
== 2; level
++) {
3588 error
= xfs_btree_delrec(cur
, level
, &i
);
3594 for (level
= 1; level
< cur
->bc_nlevels
; level
++) {
3595 if (cur
->bc_ptrs
[level
] == 0) {
3596 error
= xfs_btree_decrement(cur
, level
, &i
);
3604 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
3608 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
3613 * Get the data from the pointed-to record.
3617 struct xfs_btree_cur
*cur
, /* btree cursor */
3618 union xfs_btree_rec
**recp
, /* output: btree record */
3619 int *stat
) /* output: success/failure */
3621 struct xfs_btree_block
*block
; /* btree block */
3622 struct xfs_buf
*bp
; /* buffer pointer */
3623 int ptr
; /* record number */
3625 int error
; /* error return value */
3628 ptr
= cur
->bc_ptrs
[0];
3629 block
= xfs_btree_get_block(cur
, 0, &bp
);
3632 error
= xfs_btree_check_block(cur
, block
, 0, bp
);
3638 * Off the right end or left end, return failure.
3640 if (ptr
> xfs_btree_get_numrecs(block
) || ptr
<= 0) {
3646 * Point to the record and extract its data.
3648 *recp
= xfs_btree_rec_addr(cur
, ptr
, block
);