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"
27 #include "xfs_mount.h"
28 #include "xfs_bmap_btree.h"
29 #include "xfs_alloc_btree.h"
30 #include "xfs_ialloc_btree.h"
31 #include "xfs_dinode.h"
32 #include "xfs_inode.h"
33 #include "xfs_inode_item.h"
34 #include "xfs_btree.h"
35 #include "xfs_error.h"
36 #include "xfs_trace.h"
39 * Cursor allocation zone.
41 kmem_zone_t
*xfs_btree_cur_zone
;
44 * Btree magic numbers.
46 const __uint32_t xfs_magics
[XFS_BTNUM_MAX
] = {
47 XFS_ABTB_MAGIC
, XFS_ABTC_MAGIC
, XFS_BMAP_MAGIC
, XFS_IBT_MAGIC
51 STATIC
int /* error (0 or EFSCORRUPTED) */
52 xfs_btree_check_lblock(
53 struct xfs_btree_cur
*cur
, /* btree cursor */
54 struct xfs_btree_block
*block
, /* btree long form block pointer */
55 int level
, /* level of the btree block */
56 struct xfs_buf
*bp
) /* buffer for block, if any */
58 int lblock_ok
; /* block passes checks */
59 struct xfs_mount
*mp
; /* file system mount point */
63 be32_to_cpu(block
->bb_magic
) == xfs_magics
[cur
->bc_btnum
] &&
64 be16_to_cpu(block
->bb_level
) == level
&&
65 be16_to_cpu(block
->bb_numrecs
) <=
66 cur
->bc_ops
->get_maxrecs(cur
, level
) &&
67 block
->bb_u
.l
.bb_leftsib
&&
68 (block
->bb_u
.l
.bb_leftsib
== cpu_to_be64(NULLDFSBNO
) ||
69 XFS_FSB_SANITY_CHECK(mp
,
70 be64_to_cpu(block
->bb_u
.l
.bb_leftsib
))) &&
71 block
->bb_u
.l
.bb_rightsib
&&
72 (block
->bb_u
.l
.bb_rightsib
== cpu_to_be64(NULLDFSBNO
) ||
73 XFS_FSB_SANITY_CHECK(mp
,
74 be64_to_cpu(block
->bb_u
.l
.bb_rightsib
)));
75 if (unlikely(XFS_TEST_ERROR(!lblock_ok
, mp
,
76 XFS_ERRTAG_BTREE_CHECK_LBLOCK
,
77 XFS_RANDOM_BTREE_CHECK_LBLOCK
))) {
79 trace_xfs_btree_corrupt(bp
, _RET_IP_
);
80 XFS_ERROR_REPORT("xfs_btree_check_lblock", XFS_ERRLEVEL_LOW
,
82 return XFS_ERROR(EFSCORRUPTED
);
87 STATIC
int /* error (0 or EFSCORRUPTED) */
88 xfs_btree_check_sblock(
89 struct xfs_btree_cur
*cur
, /* btree cursor */
90 struct xfs_btree_block
*block
, /* btree short form block pointer */
91 int level
, /* level of the btree block */
92 struct xfs_buf
*bp
) /* buffer containing block */
94 struct xfs_buf
*agbp
; /* buffer for ag. freespace struct */
95 struct xfs_agf
*agf
; /* ag. freespace structure */
96 xfs_agblock_t agflen
; /* native ag. freespace length */
97 int sblock_ok
; /* block passes checks */
99 agbp
= cur
->bc_private
.a
.agbp
;
100 agf
= XFS_BUF_TO_AGF(agbp
);
101 agflen
= be32_to_cpu(agf
->agf_length
);
103 be32_to_cpu(block
->bb_magic
) == xfs_magics
[cur
->bc_btnum
] &&
104 be16_to_cpu(block
->bb_level
) == level
&&
105 be16_to_cpu(block
->bb_numrecs
) <=
106 cur
->bc_ops
->get_maxrecs(cur
, level
) &&
107 (block
->bb_u
.s
.bb_leftsib
== cpu_to_be32(NULLAGBLOCK
) ||
108 be32_to_cpu(block
->bb_u
.s
.bb_leftsib
) < agflen
) &&
109 block
->bb_u
.s
.bb_leftsib
&&
110 (block
->bb_u
.s
.bb_rightsib
== cpu_to_be32(NULLAGBLOCK
) ||
111 be32_to_cpu(block
->bb_u
.s
.bb_rightsib
) < agflen
) &&
112 block
->bb_u
.s
.bb_rightsib
;
113 if (unlikely(XFS_TEST_ERROR(!sblock_ok
, cur
->bc_mp
,
114 XFS_ERRTAG_BTREE_CHECK_SBLOCK
,
115 XFS_RANDOM_BTREE_CHECK_SBLOCK
))) {
117 trace_xfs_btree_corrupt(bp
, _RET_IP_
);
118 XFS_CORRUPTION_ERROR("xfs_btree_check_sblock",
119 XFS_ERRLEVEL_LOW
, cur
->bc_mp
, block
);
120 return XFS_ERROR(EFSCORRUPTED
);
126 * Debug routine: check that block header is ok.
129 xfs_btree_check_block(
130 struct xfs_btree_cur
*cur
, /* btree cursor */
131 struct xfs_btree_block
*block
, /* generic btree block pointer */
132 int level
, /* level of the btree block */
133 struct xfs_buf
*bp
) /* buffer containing block, if any */
135 if (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
)
136 return xfs_btree_check_lblock(cur
, block
, level
, bp
);
138 return xfs_btree_check_sblock(cur
, block
, level
, bp
);
142 * Check that (long) pointer is ok.
144 int /* error (0 or EFSCORRUPTED) */
145 xfs_btree_check_lptr(
146 struct xfs_btree_cur
*cur
, /* btree cursor */
147 xfs_dfsbno_t bno
, /* btree block disk address */
148 int level
) /* btree block level */
150 XFS_WANT_CORRUPTED_RETURN(
153 XFS_FSB_SANITY_CHECK(cur
->bc_mp
, bno
));
159 * Check that (short) pointer is ok.
161 STATIC
int /* error (0 or EFSCORRUPTED) */
162 xfs_btree_check_sptr(
163 struct xfs_btree_cur
*cur
, /* btree cursor */
164 xfs_agblock_t bno
, /* btree block disk address */
165 int level
) /* btree block level */
167 xfs_agblock_t agblocks
= cur
->bc_mp
->m_sb
.sb_agblocks
;
169 XFS_WANT_CORRUPTED_RETURN(
171 bno
!= NULLAGBLOCK
&&
178 * Check that block ptr is ok.
180 STATIC
int /* error (0 or EFSCORRUPTED) */
182 struct xfs_btree_cur
*cur
, /* btree cursor */
183 union xfs_btree_ptr
*ptr
, /* btree block disk address */
184 int index
, /* offset from ptr to check */
185 int level
) /* btree block level */
187 if (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
) {
188 return xfs_btree_check_lptr(cur
,
189 be64_to_cpu((&ptr
->l
)[index
]), level
);
191 return xfs_btree_check_sptr(cur
,
192 be32_to_cpu((&ptr
->s
)[index
]), level
);
198 * Delete the btree cursor.
201 xfs_btree_del_cursor(
202 xfs_btree_cur_t
*cur
, /* btree cursor */
203 int error
) /* del because of error */
205 int i
; /* btree level */
208 * Clear the buffer pointers, and release the buffers.
209 * If we're doing this in the face of an error, we
210 * need to make sure to inspect all of the entries
211 * in the bc_bufs array for buffers to be unlocked.
212 * This is because some of the btree code works from
213 * level n down to 0, and if we get an error along
214 * the way we won't have initialized all the entries
217 for (i
= 0; i
< cur
->bc_nlevels
; i
++) {
219 xfs_trans_brelse(cur
->bc_tp
, cur
->bc_bufs
[i
]);
224 * Can't free a bmap cursor without having dealt with the
225 * allocated indirect blocks' accounting.
227 ASSERT(cur
->bc_btnum
!= XFS_BTNUM_BMAP
||
228 cur
->bc_private
.b
.allocated
== 0);
232 kmem_zone_free(xfs_btree_cur_zone
, cur
);
236 * Duplicate the btree cursor.
237 * Allocate a new one, copy the record, re-get the buffers.
240 xfs_btree_dup_cursor(
241 xfs_btree_cur_t
*cur
, /* input cursor */
242 xfs_btree_cur_t
**ncur
) /* output cursor */
244 xfs_buf_t
*bp
; /* btree block's buffer pointer */
245 int error
; /* error return value */
246 int i
; /* level number of btree block */
247 xfs_mount_t
*mp
; /* mount structure for filesystem */
248 xfs_btree_cur_t
*new; /* new cursor value */
249 xfs_trans_t
*tp
; /* transaction pointer, can be NULL */
255 * Allocate a new cursor like the old one.
257 new = cur
->bc_ops
->dup_cursor(cur
);
260 * Copy the record currently in the cursor.
262 new->bc_rec
= cur
->bc_rec
;
265 * For each level current, re-get the buffer and copy the ptr value.
267 for (i
= 0; i
< new->bc_nlevels
; i
++) {
268 new->bc_ptrs
[i
] = cur
->bc_ptrs
[i
];
269 new->bc_ra
[i
] = cur
->bc_ra
[i
];
270 if ((bp
= cur
->bc_bufs
[i
])) {
271 if ((error
= xfs_trans_read_buf(mp
, tp
, mp
->m_ddev_targp
,
272 XFS_BUF_ADDR(bp
), mp
->m_bsize
, 0, &bp
))) {
273 xfs_btree_del_cursor(new, error
);
277 new->bc_bufs
[i
] = bp
;
278 ASSERT(!xfs_buf_geterror(bp
));
280 new->bc_bufs
[i
] = NULL
;
287 * XFS btree block layout and addressing:
289 * There are two types of blocks in the btree: leaf and non-leaf blocks.
291 * The leaf record start with a header then followed by records containing
292 * the values. A non-leaf block also starts with the same header, and
293 * then first contains lookup keys followed by an equal number of pointers
294 * to the btree blocks at the previous level.
296 * +--------+-------+-------+-------+-------+-------+-------+
297 * Leaf: | header | rec 1 | rec 2 | rec 3 | rec 4 | rec 5 | rec N |
298 * +--------+-------+-------+-------+-------+-------+-------+
300 * +--------+-------+-------+-------+-------+-------+-------+
301 * Non-Leaf: | header | key 1 | key 2 | key N | ptr 1 | ptr 2 | ptr N |
302 * +--------+-------+-------+-------+-------+-------+-------+
304 * The header is called struct xfs_btree_block for reasons better left unknown
305 * and comes in different versions for short (32bit) and long (64bit) block
306 * pointers. The record and key structures are defined by the btree instances
307 * and opaque to the btree core. The block pointers are simple disk endian
308 * integers, available in a short (32bit) and long (64bit) variant.
310 * The helpers below calculate the offset of a given record, key or pointer
311 * into a btree block (xfs_btree_*_offset) or return a pointer to the given
312 * record, key or pointer (xfs_btree_*_addr). Note that all addressing
313 * inside the btree block is done using indices starting at one, not zero!
317 * Return size of the btree block header for this btree instance.
319 static inline size_t xfs_btree_block_len(struct xfs_btree_cur
*cur
)
321 return (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
) ?
322 XFS_BTREE_LBLOCK_LEN
:
323 XFS_BTREE_SBLOCK_LEN
;
327 * Return size of btree block pointers for this btree instance.
329 static inline size_t xfs_btree_ptr_len(struct xfs_btree_cur
*cur
)
331 return (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
) ?
332 sizeof(__be64
) : sizeof(__be32
);
336 * Calculate offset of the n-th record in a btree block.
339 xfs_btree_rec_offset(
340 struct xfs_btree_cur
*cur
,
343 return xfs_btree_block_len(cur
) +
344 (n
- 1) * cur
->bc_ops
->rec_len
;
348 * Calculate offset of the n-th key in a btree block.
351 xfs_btree_key_offset(
352 struct xfs_btree_cur
*cur
,
355 return xfs_btree_block_len(cur
) +
356 (n
- 1) * cur
->bc_ops
->key_len
;
360 * Calculate offset of the n-th block pointer in a btree block.
363 xfs_btree_ptr_offset(
364 struct xfs_btree_cur
*cur
,
368 return xfs_btree_block_len(cur
) +
369 cur
->bc_ops
->get_maxrecs(cur
, level
) * cur
->bc_ops
->key_len
+
370 (n
- 1) * xfs_btree_ptr_len(cur
);
374 * Return a pointer to the n-th record in the btree block.
376 STATIC
union xfs_btree_rec
*
378 struct xfs_btree_cur
*cur
,
380 struct xfs_btree_block
*block
)
382 return (union xfs_btree_rec
*)
383 ((char *)block
+ xfs_btree_rec_offset(cur
, n
));
387 * Return a pointer to the n-th key in the btree block.
389 STATIC
union xfs_btree_key
*
391 struct xfs_btree_cur
*cur
,
393 struct xfs_btree_block
*block
)
395 return (union xfs_btree_key
*)
396 ((char *)block
+ xfs_btree_key_offset(cur
, n
));
400 * Return a pointer to the n-th block pointer in the btree block.
402 STATIC
union xfs_btree_ptr
*
404 struct xfs_btree_cur
*cur
,
406 struct xfs_btree_block
*block
)
408 int level
= xfs_btree_get_level(block
);
410 ASSERT(block
->bb_level
!= 0);
412 return (union xfs_btree_ptr
*)
413 ((char *)block
+ xfs_btree_ptr_offset(cur
, n
, level
));
417 * Get a the root block which is stored in the inode.
419 * For now this btree implementation assumes the btree root is always
420 * stored in the if_broot field of an inode fork.
422 STATIC
struct xfs_btree_block
*
424 struct xfs_btree_cur
*cur
)
426 struct xfs_ifork
*ifp
;
428 ifp
= XFS_IFORK_PTR(cur
->bc_private
.b
.ip
, cur
->bc_private
.b
.whichfork
);
429 return (struct xfs_btree_block
*)ifp
->if_broot
;
433 * Retrieve the block pointer from the cursor at the given level.
434 * This may be an inode btree root or from a buffer.
436 STATIC
struct xfs_btree_block
* /* generic btree block pointer */
438 struct xfs_btree_cur
*cur
, /* btree cursor */
439 int level
, /* level in btree */
440 struct xfs_buf
**bpp
) /* buffer containing the block */
442 if ((cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
) &&
443 (level
== cur
->bc_nlevels
- 1)) {
445 return xfs_btree_get_iroot(cur
);
448 *bpp
= cur
->bc_bufs
[level
];
449 return XFS_BUF_TO_BLOCK(*bpp
);
453 * Get a buffer for the block, return it with no data read.
454 * Long-form addressing.
456 xfs_buf_t
* /* buffer for fsbno */
458 xfs_mount_t
*mp
, /* file system mount point */
459 xfs_trans_t
*tp
, /* transaction pointer */
460 xfs_fsblock_t fsbno
, /* file system block number */
461 uint lock
) /* lock flags for get_buf */
463 xfs_buf_t
*bp
; /* buffer pointer (return value) */
464 xfs_daddr_t d
; /* real disk block address */
466 ASSERT(fsbno
!= NULLFSBLOCK
);
467 d
= XFS_FSB_TO_DADDR(mp
, fsbno
);
468 bp
= xfs_trans_get_buf(tp
, mp
->m_ddev_targp
, d
, mp
->m_bsize
, lock
);
469 ASSERT(!xfs_buf_geterror(bp
));
474 * Get a buffer for the block, return it with no data read.
475 * Short-form addressing.
477 xfs_buf_t
* /* buffer for agno/agbno */
479 xfs_mount_t
*mp
, /* file system mount point */
480 xfs_trans_t
*tp
, /* transaction pointer */
481 xfs_agnumber_t agno
, /* allocation group number */
482 xfs_agblock_t agbno
, /* allocation group block number */
483 uint lock
) /* lock flags for get_buf */
485 xfs_buf_t
*bp
; /* buffer pointer (return value) */
486 xfs_daddr_t d
; /* real disk block address */
488 ASSERT(agno
!= NULLAGNUMBER
);
489 ASSERT(agbno
!= NULLAGBLOCK
);
490 d
= XFS_AGB_TO_DADDR(mp
, agno
, agbno
);
491 bp
= xfs_trans_get_buf(tp
, mp
->m_ddev_targp
, d
, mp
->m_bsize
, lock
);
492 ASSERT(!xfs_buf_geterror(bp
));
497 * Check for the cursor referring to the last block at the given level.
499 int /* 1=is last block, 0=not last block */
500 xfs_btree_islastblock(
501 xfs_btree_cur_t
*cur
, /* btree cursor */
502 int level
) /* level to check */
504 struct xfs_btree_block
*block
; /* generic btree block pointer */
505 xfs_buf_t
*bp
; /* buffer containing block */
507 block
= xfs_btree_get_block(cur
, level
, &bp
);
508 xfs_btree_check_block(cur
, block
, level
, bp
);
509 if (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
)
510 return block
->bb_u
.l
.bb_rightsib
== cpu_to_be64(NULLDFSBNO
);
512 return block
->bb_u
.s
.bb_rightsib
== cpu_to_be32(NULLAGBLOCK
);
516 * Change the cursor to point to the first record at the given level.
517 * Other levels are unaffected.
519 STATIC
int /* success=1, failure=0 */
521 xfs_btree_cur_t
*cur
, /* btree cursor */
522 int level
) /* level to change */
524 struct xfs_btree_block
*block
; /* generic btree block pointer */
525 xfs_buf_t
*bp
; /* buffer containing block */
528 * Get the block pointer for this level.
530 block
= xfs_btree_get_block(cur
, level
, &bp
);
531 xfs_btree_check_block(cur
, block
, level
, bp
);
533 * It's empty, there is no such record.
535 if (!block
->bb_numrecs
)
538 * Set the ptr value to 1, that's the first record/key.
540 cur
->bc_ptrs
[level
] = 1;
545 * Change the cursor to point to the last record in the current block
546 * at the given level. Other levels are unaffected.
548 STATIC
int /* success=1, failure=0 */
550 xfs_btree_cur_t
*cur
, /* btree cursor */
551 int level
) /* level to change */
553 struct xfs_btree_block
*block
; /* generic btree block pointer */
554 xfs_buf_t
*bp
; /* buffer containing block */
557 * Get the block pointer for this level.
559 block
= xfs_btree_get_block(cur
, level
, &bp
);
560 xfs_btree_check_block(cur
, block
, level
, bp
);
562 * It's empty, there is no such record.
564 if (!block
->bb_numrecs
)
567 * Set the ptr value to numrecs, that's the last record/key.
569 cur
->bc_ptrs
[level
] = be16_to_cpu(block
->bb_numrecs
);
574 * Compute first and last byte offsets for the fields given.
575 * Interprets the offsets table, which contains struct field offsets.
579 __int64_t fields
, /* bitmask of fields */
580 const short *offsets
, /* table of field offsets */
581 int nbits
, /* number of bits to inspect */
582 int *first
, /* output: first byte offset */
583 int *last
) /* output: last byte offset */
585 int i
; /* current bit number */
586 __int64_t imask
; /* mask for current bit number */
590 * Find the lowest bit, so the first byte offset.
592 for (i
= 0, imask
= 1LL; ; i
++, imask
<<= 1) {
593 if (imask
& fields
) {
599 * Find the highest bit, so the last byte offset.
601 for (i
= nbits
- 1, imask
= 1LL << i
; ; i
--, imask
>>= 1) {
602 if (imask
& fields
) {
603 *last
= offsets
[i
+ 1] - 1;
610 * Get a buffer for the block, return it read in.
611 * Long-form addressing.
615 xfs_mount_t
*mp
, /* file system mount point */
616 xfs_trans_t
*tp
, /* transaction pointer */
617 xfs_fsblock_t fsbno
, /* file system block number */
618 uint lock
, /* lock flags for read_buf */
619 xfs_buf_t
**bpp
, /* buffer for fsbno */
620 int refval
) /* ref count value for buffer */
622 xfs_buf_t
*bp
; /* return value */
623 xfs_daddr_t d
; /* real disk block address */
626 ASSERT(fsbno
!= NULLFSBLOCK
);
627 d
= XFS_FSB_TO_DADDR(mp
, fsbno
);
628 if ((error
= xfs_trans_read_buf(mp
, tp
, mp
->m_ddev_targp
, d
,
629 mp
->m_bsize
, lock
, &bp
))) {
632 ASSERT(!xfs_buf_geterror(bp
));
634 xfs_buf_set_ref(bp
, refval
);
640 * Read-ahead the block, don't wait for it, don't return a buffer.
641 * Long-form addressing.
645 xfs_btree_reada_bufl(
646 xfs_mount_t
*mp
, /* file system mount point */
647 xfs_fsblock_t fsbno
, /* file system block number */
648 xfs_extlen_t count
) /* count of filesystem blocks */
652 ASSERT(fsbno
!= NULLFSBLOCK
);
653 d
= XFS_FSB_TO_DADDR(mp
, fsbno
);
654 xfs_buf_readahead(mp
->m_ddev_targp
, d
, mp
->m_bsize
* count
);
658 * Read-ahead the block, don't wait for it, don't return a buffer.
659 * Short-form addressing.
663 xfs_btree_reada_bufs(
664 xfs_mount_t
*mp
, /* file system mount point */
665 xfs_agnumber_t agno
, /* allocation group number */
666 xfs_agblock_t agbno
, /* allocation group block number */
667 xfs_extlen_t count
) /* count of filesystem blocks */
671 ASSERT(agno
!= NULLAGNUMBER
);
672 ASSERT(agbno
!= NULLAGBLOCK
);
673 d
= XFS_AGB_TO_DADDR(mp
, agno
, agbno
);
674 xfs_buf_readahead(mp
->m_ddev_targp
, d
, mp
->m_bsize
* count
);
678 xfs_btree_readahead_lblock(
679 struct xfs_btree_cur
*cur
,
681 struct xfs_btree_block
*block
)
684 xfs_dfsbno_t left
= be64_to_cpu(block
->bb_u
.l
.bb_leftsib
);
685 xfs_dfsbno_t right
= be64_to_cpu(block
->bb_u
.l
.bb_rightsib
);
687 if ((lr
& XFS_BTCUR_LEFTRA
) && left
!= NULLDFSBNO
) {
688 xfs_btree_reada_bufl(cur
->bc_mp
, left
, 1);
692 if ((lr
& XFS_BTCUR_RIGHTRA
) && right
!= NULLDFSBNO
) {
693 xfs_btree_reada_bufl(cur
->bc_mp
, right
, 1);
701 xfs_btree_readahead_sblock(
702 struct xfs_btree_cur
*cur
,
704 struct xfs_btree_block
*block
)
707 xfs_agblock_t left
= be32_to_cpu(block
->bb_u
.s
.bb_leftsib
);
708 xfs_agblock_t right
= be32_to_cpu(block
->bb_u
.s
.bb_rightsib
);
711 if ((lr
& XFS_BTCUR_LEFTRA
) && left
!= NULLAGBLOCK
) {
712 xfs_btree_reada_bufs(cur
->bc_mp
, cur
->bc_private
.a
.agno
,
717 if ((lr
& XFS_BTCUR_RIGHTRA
) && right
!= NULLAGBLOCK
) {
718 xfs_btree_reada_bufs(cur
->bc_mp
, cur
->bc_private
.a
.agno
,
727 * Read-ahead btree blocks, at the given level.
728 * Bits in lr are set from XFS_BTCUR_{LEFT,RIGHT}RA.
732 struct xfs_btree_cur
*cur
, /* btree cursor */
733 int lev
, /* level in btree */
734 int lr
) /* left/right bits */
736 struct xfs_btree_block
*block
;
739 * No readahead needed if we are at the root level and the
740 * btree root is stored in the inode.
742 if ((cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
) &&
743 (lev
== cur
->bc_nlevels
- 1))
746 if ((cur
->bc_ra
[lev
] | lr
) == cur
->bc_ra
[lev
])
749 cur
->bc_ra
[lev
] |= lr
;
750 block
= XFS_BUF_TO_BLOCK(cur
->bc_bufs
[lev
]);
752 if (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
)
753 return xfs_btree_readahead_lblock(cur
, lr
, block
);
754 return xfs_btree_readahead_sblock(cur
, lr
, block
);
758 * Set the buffer for level "lev" in the cursor to bp, releasing
759 * any previous buffer.
763 xfs_btree_cur_t
*cur
, /* btree cursor */
764 int lev
, /* level in btree */
765 xfs_buf_t
*bp
) /* new buffer to set */
767 struct xfs_btree_block
*b
; /* btree block */
769 if (cur
->bc_bufs
[lev
])
770 xfs_trans_brelse(cur
->bc_tp
, cur
->bc_bufs
[lev
]);
771 cur
->bc_bufs
[lev
] = bp
;
774 b
= XFS_BUF_TO_BLOCK(bp
);
775 if (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
) {
776 if (b
->bb_u
.l
.bb_leftsib
== cpu_to_be64(NULLDFSBNO
))
777 cur
->bc_ra
[lev
] |= XFS_BTCUR_LEFTRA
;
778 if (b
->bb_u
.l
.bb_rightsib
== cpu_to_be64(NULLDFSBNO
))
779 cur
->bc_ra
[lev
] |= XFS_BTCUR_RIGHTRA
;
781 if (b
->bb_u
.s
.bb_leftsib
== cpu_to_be32(NULLAGBLOCK
))
782 cur
->bc_ra
[lev
] |= XFS_BTCUR_LEFTRA
;
783 if (b
->bb_u
.s
.bb_rightsib
== cpu_to_be32(NULLAGBLOCK
))
784 cur
->bc_ra
[lev
] |= XFS_BTCUR_RIGHTRA
;
789 xfs_btree_ptr_is_null(
790 struct xfs_btree_cur
*cur
,
791 union xfs_btree_ptr
*ptr
)
793 if (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
)
794 return ptr
->l
== cpu_to_be64(NULLDFSBNO
);
796 return ptr
->s
== cpu_to_be32(NULLAGBLOCK
);
800 xfs_btree_set_ptr_null(
801 struct xfs_btree_cur
*cur
,
802 union xfs_btree_ptr
*ptr
)
804 if (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
)
805 ptr
->l
= cpu_to_be64(NULLDFSBNO
);
807 ptr
->s
= cpu_to_be32(NULLAGBLOCK
);
811 * Get/set/init sibling pointers
814 xfs_btree_get_sibling(
815 struct xfs_btree_cur
*cur
,
816 struct xfs_btree_block
*block
,
817 union xfs_btree_ptr
*ptr
,
820 ASSERT(lr
== XFS_BB_LEFTSIB
|| lr
== XFS_BB_RIGHTSIB
);
822 if (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
) {
823 if (lr
== XFS_BB_RIGHTSIB
)
824 ptr
->l
= block
->bb_u
.l
.bb_rightsib
;
826 ptr
->l
= block
->bb_u
.l
.bb_leftsib
;
828 if (lr
== XFS_BB_RIGHTSIB
)
829 ptr
->s
= block
->bb_u
.s
.bb_rightsib
;
831 ptr
->s
= block
->bb_u
.s
.bb_leftsib
;
836 xfs_btree_set_sibling(
837 struct xfs_btree_cur
*cur
,
838 struct xfs_btree_block
*block
,
839 union xfs_btree_ptr
*ptr
,
842 ASSERT(lr
== XFS_BB_LEFTSIB
|| lr
== XFS_BB_RIGHTSIB
);
844 if (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
) {
845 if (lr
== XFS_BB_RIGHTSIB
)
846 block
->bb_u
.l
.bb_rightsib
= ptr
->l
;
848 block
->bb_u
.l
.bb_leftsib
= ptr
->l
;
850 if (lr
== XFS_BB_RIGHTSIB
)
851 block
->bb_u
.s
.bb_rightsib
= ptr
->s
;
853 block
->bb_u
.s
.bb_leftsib
= ptr
->s
;
858 xfs_btree_init_block(
859 struct xfs_btree_cur
*cur
,
862 struct xfs_btree_block
*new) /* new block */
864 new->bb_magic
= cpu_to_be32(xfs_magics
[cur
->bc_btnum
]);
865 new->bb_level
= cpu_to_be16(level
);
866 new->bb_numrecs
= cpu_to_be16(numrecs
);
868 if (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
) {
869 new->bb_u
.l
.bb_leftsib
= cpu_to_be64(NULLDFSBNO
);
870 new->bb_u
.l
.bb_rightsib
= cpu_to_be64(NULLDFSBNO
);
872 new->bb_u
.s
.bb_leftsib
= cpu_to_be32(NULLAGBLOCK
);
873 new->bb_u
.s
.bb_rightsib
= cpu_to_be32(NULLAGBLOCK
);
878 * Return true if ptr is the last record in the btree and
879 * we need to track updateѕ to this record. The decision
880 * will be further refined in the update_lastrec method.
883 xfs_btree_is_lastrec(
884 struct xfs_btree_cur
*cur
,
885 struct xfs_btree_block
*block
,
888 union xfs_btree_ptr ptr
;
892 if (!(cur
->bc_flags
& XFS_BTREE_LASTREC_UPDATE
))
895 xfs_btree_get_sibling(cur
, block
, &ptr
, XFS_BB_RIGHTSIB
);
896 if (!xfs_btree_ptr_is_null(cur
, &ptr
))
902 xfs_btree_buf_to_ptr(
903 struct xfs_btree_cur
*cur
,
905 union xfs_btree_ptr
*ptr
)
907 if (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
)
908 ptr
->l
= cpu_to_be64(XFS_DADDR_TO_FSB(cur
->bc_mp
,
911 ptr
->s
= cpu_to_be32(xfs_daddr_to_agbno(cur
->bc_mp
,
917 xfs_btree_ptr_to_daddr(
918 struct xfs_btree_cur
*cur
,
919 union xfs_btree_ptr
*ptr
)
921 if (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
) {
922 ASSERT(ptr
->l
!= cpu_to_be64(NULLDFSBNO
));
924 return XFS_FSB_TO_DADDR(cur
->bc_mp
, be64_to_cpu(ptr
->l
));
926 ASSERT(cur
->bc_private
.a
.agno
!= NULLAGNUMBER
);
927 ASSERT(ptr
->s
!= cpu_to_be32(NULLAGBLOCK
));
929 return XFS_AGB_TO_DADDR(cur
->bc_mp
, cur
->bc_private
.a
.agno
,
930 be32_to_cpu(ptr
->s
));
936 struct xfs_btree_cur
*cur
,
939 switch (cur
->bc_btnum
) {
942 xfs_buf_set_ref(bp
, XFS_ALLOC_BTREE_REF
);
945 xfs_buf_set_ref(bp
, XFS_INO_BTREE_REF
);
948 xfs_buf_set_ref(bp
, XFS_BMAP_BTREE_REF
);
956 xfs_btree_get_buf_block(
957 struct xfs_btree_cur
*cur
,
958 union xfs_btree_ptr
*ptr
,
960 struct xfs_btree_block
**block
,
961 struct xfs_buf
**bpp
)
963 struct xfs_mount
*mp
= cur
->bc_mp
;
966 /* need to sort out how callers deal with failures first */
967 ASSERT(!(flags
& XBF_TRYLOCK
));
969 d
= xfs_btree_ptr_to_daddr(cur
, ptr
);
970 *bpp
= xfs_trans_get_buf(cur
->bc_tp
, mp
->m_ddev_targp
, d
,
976 *block
= XFS_BUF_TO_BLOCK(*bpp
);
981 * Read in the buffer at the given ptr and return the buffer and
982 * the block pointer within the buffer.
985 xfs_btree_read_buf_block(
986 struct xfs_btree_cur
*cur
,
987 union xfs_btree_ptr
*ptr
,
990 struct xfs_btree_block
**block
,
991 struct xfs_buf
**bpp
)
993 struct xfs_mount
*mp
= cur
->bc_mp
;
997 /* need to sort out how callers deal with failures first */
998 ASSERT(!(flags
& XBF_TRYLOCK
));
1000 d
= xfs_btree_ptr_to_daddr(cur
, ptr
);
1001 error
= xfs_trans_read_buf(mp
, cur
->bc_tp
, mp
->m_ddev_targp
, d
,
1002 mp
->m_bsize
, flags
, bpp
);
1006 ASSERT(!xfs_buf_geterror(*bpp
));
1008 xfs_btree_set_refs(cur
, *bpp
);
1009 *block
= XFS_BUF_TO_BLOCK(*bpp
);
1011 error
= xfs_btree_check_block(cur
, *block
, level
, *bpp
);
1013 xfs_trans_brelse(cur
->bc_tp
, *bpp
);
1018 * Copy keys from one btree block to another.
1021 xfs_btree_copy_keys(
1022 struct xfs_btree_cur
*cur
,
1023 union xfs_btree_key
*dst_key
,
1024 union xfs_btree_key
*src_key
,
1027 ASSERT(numkeys
>= 0);
1028 memcpy(dst_key
, src_key
, numkeys
* cur
->bc_ops
->key_len
);
1032 * Copy records from one btree block to another.
1035 xfs_btree_copy_recs(
1036 struct xfs_btree_cur
*cur
,
1037 union xfs_btree_rec
*dst_rec
,
1038 union xfs_btree_rec
*src_rec
,
1041 ASSERT(numrecs
>= 0);
1042 memcpy(dst_rec
, src_rec
, numrecs
* cur
->bc_ops
->rec_len
);
1046 * Copy block pointers from one btree block to another.
1049 xfs_btree_copy_ptrs(
1050 struct xfs_btree_cur
*cur
,
1051 union xfs_btree_ptr
*dst_ptr
,
1052 union xfs_btree_ptr
*src_ptr
,
1055 ASSERT(numptrs
>= 0);
1056 memcpy(dst_ptr
, src_ptr
, numptrs
* xfs_btree_ptr_len(cur
));
1060 * Shift keys one index left/right inside a single btree block.
1063 xfs_btree_shift_keys(
1064 struct xfs_btree_cur
*cur
,
1065 union xfs_btree_key
*key
,
1071 ASSERT(numkeys
>= 0);
1072 ASSERT(dir
== 1 || dir
== -1);
1074 dst_key
= (char *)key
+ (dir
* cur
->bc_ops
->key_len
);
1075 memmove(dst_key
, key
, numkeys
* cur
->bc_ops
->key_len
);
1079 * Shift records one index left/right inside a single btree block.
1082 xfs_btree_shift_recs(
1083 struct xfs_btree_cur
*cur
,
1084 union xfs_btree_rec
*rec
,
1090 ASSERT(numrecs
>= 0);
1091 ASSERT(dir
== 1 || dir
== -1);
1093 dst_rec
= (char *)rec
+ (dir
* cur
->bc_ops
->rec_len
);
1094 memmove(dst_rec
, rec
, numrecs
* cur
->bc_ops
->rec_len
);
1098 * Shift block pointers one index left/right inside a single btree block.
1101 xfs_btree_shift_ptrs(
1102 struct xfs_btree_cur
*cur
,
1103 union xfs_btree_ptr
*ptr
,
1109 ASSERT(numptrs
>= 0);
1110 ASSERT(dir
== 1 || dir
== -1);
1112 dst_ptr
= (char *)ptr
+ (dir
* xfs_btree_ptr_len(cur
));
1113 memmove(dst_ptr
, ptr
, numptrs
* xfs_btree_ptr_len(cur
));
1117 * Log key values from the btree block.
1121 struct xfs_btree_cur
*cur
,
1126 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
1127 XFS_BTREE_TRACE_ARGBII(cur
, bp
, first
, last
);
1130 xfs_trans_log_buf(cur
->bc_tp
, bp
,
1131 xfs_btree_key_offset(cur
, first
),
1132 xfs_btree_key_offset(cur
, last
+ 1) - 1);
1134 xfs_trans_log_inode(cur
->bc_tp
, cur
->bc_private
.b
.ip
,
1135 xfs_ilog_fbroot(cur
->bc_private
.b
.whichfork
));
1138 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
1142 * Log record values from the btree block.
1146 struct xfs_btree_cur
*cur
,
1151 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
1152 XFS_BTREE_TRACE_ARGBII(cur
, bp
, first
, last
);
1154 xfs_trans_log_buf(cur
->bc_tp
, bp
,
1155 xfs_btree_rec_offset(cur
, first
),
1156 xfs_btree_rec_offset(cur
, last
+ 1) - 1);
1158 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
1162 * Log block pointer fields from a btree block (nonleaf).
1166 struct xfs_btree_cur
*cur
, /* btree cursor */
1167 struct xfs_buf
*bp
, /* buffer containing btree block */
1168 int first
, /* index of first pointer to log */
1169 int last
) /* index of last pointer to log */
1171 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
1172 XFS_BTREE_TRACE_ARGBII(cur
, bp
, first
, last
);
1175 struct xfs_btree_block
*block
= XFS_BUF_TO_BLOCK(bp
);
1176 int level
= xfs_btree_get_level(block
);
1178 xfs_trans_log_buf(cur
->bc_tp
, bp
,
1179 xfs_btree_ptr_offset(cur
, first
, level
),
1180 xfs_btree_ptr_offset(cur
, last
+ 1, level
) - 1);
1182 xfs_trans_log_inode(cur
->bc_tp
, cur
->bc_private
.b
.ip
,
1183 xfs_ilog_fbroot(cur
->bc_private
.b
.whichfork
));
1186 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
1190 * Log fields from a btree block header.
1193 xfs_btree_log_block(
1194 struct xfs_btree_cur
*cur
, /* btree cursor */
1195 struct xfs_buf
*bp
, /* buffer containing btree block */
1196 int fields
) /* mask of fields: XFS_BB_... */
1198 int first
; /* first byte offset logged */
1199 int last
; /* last byte offset logged */
1200 static const short soffsets
[] = { /* table of offsets (short) */
1201 offsetof(struct xfs_btree_block
, bb_magic
),
1202 offsetof(struct xfs_btree_block
, bb_level
),
1203 offsetof(struct xfs_btree_block
, bb_numrecs
),
1204 offsetof(struct xfs_btree_block
, bb_u
.s
.bb_leftsib
),
1205 offsetof(struct xfs_btree_block
, bb_u
.s
.bb_rightsib
),
1206 XFS_BTREE_SBLOCK_LEN
1208 static const short loffsets
[] = { /* table of offsets (long) */
1209 offsetof(struct xfs_btree_block
, bb_magic
),
1210 offsetof(struct xfs_btree_block
, bb_level
),
1211 offsetof(struct xfs_btree_block
, bb_numrecs
),
1212 offsetof(struct xfs_btree_block
, bb_u
.l
.bb_leftsib
),
1213 offsetof(struct xfs_btree_block
, bb_u
.l
.bb_rightsib
),
1214 XFS_BTREE_LBLOCK_LEN
1217 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
1218 XFS_BTREE_TRACE_ARGBI(cur
, bp
, fields
);
1221 xfs_btree_offsets(fields
,
1222 (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
) ?
1223 loffsets
: soffsets
,
1224 XFS_BB_NUM_BITS
, &first
, &last
);
1225 xfs_trans_log_buf(cur
->bc_tp
, bp
, first
, last
);
1227 xfs_trans_log_inode(cur
->bc_tp
, cur
->bc_private
.b
.ip
,
1228 xfs_ilog_fbroot(cur
->bc_private
.b
.whichfork
));
1231 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
1235 * Increment cursor by one record at the level.
1236 * For nonzero levels the leaf-ward information is untouched.
1239 xfs_btree_increment(
1240 struct xfs_btree_cur
*cur
,
1242 int *stat
) /* success/failure */
1244 struct xfs_btree_block
*block
;
1245 union xfs_btree_ptr ptr
;
1247 int error
; /* error return value */
1250 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
1251 XFS_BTREE_TRACE_ARGI(cur
, level
);
1253 ASSERT(level
< cur
->bc_nlevels
);
1255 /* Read-ahead to the right at this level. */
1256 xfs_btree_readahead(cur
, level
, XFS_BTCUR_RIGHTRA
);
1258 /* Get a pointer to the btree block. */
1259 block
= xfs_btree_get_block(cur
, level
, &bp
);
1262 error
= xfs_btree_check_block(cur
, block
, level
, bp
);
1267 /* We're done if we remain in the block after the increment. */
1268 if (++cur
->bc_ptrs
[level
] <= xfs_btree_get_numrecs(block
))
1271 /* Fail if we just went off the right edge of the tree. */
1272 xfs_btree_get_sibling(cur
, block
, &ptr
, XFS_BB_RIGHTSIB
);
1273 if (xfs_btree_ptr_is_null(cur
, &ptr
))
1276 XFS_BTREE_STATS_INC(cur
, increment
);
1279 * March up the tree incrementing pointers.
1280 * Stop when we don't go off the right edge of a block.
1282 for (lev
= level
+ 1; lev
< cur
->bc_nlevels
; lev
++) {
1283 block
= xfs_btree_get_block(cur
, lev
, &bp
);
1286 error
= xfs_btree_check_block(cur
, block
, lev
, bp
);
1291 if (++cur
->bc_ptrs
[lev
] <= xfs_btree_get_numrecs(block
))
1294 /* Read-ahead the right block for the next loop. */
1295 xfs_btree_readahead(cur
, lev
, XFS_BTCUR_RIGHTRA
);
1299 * If we went off the root then we are either seriously
1300 * confused or have the tree root in an inode.
1302 if (lev
== cur
->bc_nlevels
) {
1303 if (cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
)
1306 error
= EFSCORRUPTED
;
1309 ASSERT(lev
< cur
->bc_nlevels
);
1312 * Now walk back down the tree, fixing up the cursor's buffer
1313 * pointers and key numbers.
1315 for (block
= xfs_btree_get_block(cur
, lev
, &bp
); lev
> level
; ) {
1316 union xfs_btree_ptr
*ptrp
;
1318 ptrp
= xfs_btree_ptr_addr(cur
, cur
->bc_ptrs
[lev
], block
);
1319 error
= xfs_btree_read_buf_block(cur
, ptrp
, --lev
,
1324 xfs_btree_setbuf(cur
, lev
, bp
);
1325 cur
->bc_ptrs
[lev
] = 1;
1328 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
1333 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
1338 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
1343 * Decrement cursor by one record at the level.
1344 * For nonzero levels the leaf-ward information is untouched.
1347 xfs_btree_decrement(
1348 struct xfs_btree_cur
*cur
,
1350 int *stat
) /* success/failure */
1352 struct xfs_btree_block
*block
;
1354 int error
; /* error return value */
1356 union xfs_btree_ptr ptr
;
1358 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
1359 XFS_BTREE_TRACE_ARGI(cur
, level
);
1361 ASSERT(level
< cur
->bc_nlevels
);
1363 /* Read-ahead to the left at this level. */
1364 xfs_btree_readahead(cur
, level
, XFS_BTCUR_LEFTRA
);
1366 /* We're done if we remain in the block after the decrement. */
1367 if (--cur
->bc_ptrs
[level
] > 0)
1370 /* Get a pointer to the btree block. */
1371 block
= xfs_btree_get_block(cur
, level
, &bp
);
1374 error
= xfs_btree_check_block(cur
, block
, level
, bp
);
1379 /* Fail if we just went off the left edge of the tree. */
1380 xfs_btree_get_sibling(cur
, block
, &ptr
, XFS_BB_LEFTSIB
);
1381 if (xfs_btree_ptr_is_null(cur
, &ptr
))
1384 XFS_BTREE_STATS_INC(cur
, decrement
);
1387 * March up the tree decrementing pointers.
1388 * Stop when we don't go off the left edge of a block.
1390 for (lev
= level
+ 1; lev
< cur
->bc_nlevels
; lev
++) {
1391 if (--cur
->bc_ptrs
[lev
] > 0)
1393 /* Read-ahead the left block for the next loop. */
1394 xfs_btree_readahead(cur
, lev
, XFS_BTCUR_LEFTRA
);
1398 * If we went off the root then we are seriously confused.
1399 * or the root of the tree is in an inode.
1401 if (lev
== cur
->bc_nlevels
) {
1402 if (cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
)
1405 error
= EFSCORRUPTED
;
1408 ASSERT(lev
< cur
->bc_nlevels
);
1411 * Now walk back down the tree, fixing up the cursor's buffer
1412 * pointers and key numbers.
1414 for (block
= xfs_btree_get_block(cur
, lev
, &bp
); lev
> level
; ) {
1415 union xfs_btree_ptr
*ptrp
;
1417 ptrp
= xfs_btree_ptr_addr(cur
, cur
->bc_ptrs
[lev
], block
);
1418 error
= xfs_btree_read_buf_block(cur
, ptrp
, --lev
,
1422 xfs_btree_setbuf(cur
, lev
, bp
);
1423 cur
->bc_ptrs
[lev
] = xfs_btree_get_numrecs(block
);
1426 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
1431 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
1436 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
1441 xfs_btree_lookup_get_block(
1442 struct xfs_btree_cur
*cur
, /* btree cursor */
1443 int level
, /* level in the btree */
1444 union xfs_btree_ptr
*pp
, /* ptr to btree block */
1445 struct xfs_btree_block
**blkp
) /* return btree block */
1447 struct xfs_buf
*bp
; /* buffer pointer for btree block */
1450 /* special case the root block if in an inode */
1451 if ((cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
) &&
1452 (level
== cur
->bc_nlevels
- 1)) {
1453 *blkp
= xfs_btree_get_iroot(cur
);
1458 * If the old buffer at this level for the disk address we are
1459 * looking for re-use it.
1461 * Otherwise throw it away and get a new one.
1463 bp
= cur
->bc_bufs
[level
];
1464 if (bp
&& XFS_BUF_ADDR(bp
) == xfs_btree_ptr_to_daddr(cur
, pp
)) {
1465 *blkp
= XFS_BUF_TO_BLOCK(bp
);
1469 error
= xfs_btree_read_buf_block(cur
, pp
, level
, 0, blkp
, &bp
);
1473 xfs_btree_setbuf(cur
, level
, bp
);
1478 * Get current search key. For level 0 we don't actually have a key
1479 * structure so we make one up from the record. For all other levels
1480 * we just return the right key.
1482 STATIC
union xfs_btree_key
*
1483 xfs_lookup_get_search_key(
1484 struct xfs_btree_cur
*cur
,
1487 struct xfs_btree_block
*block
,
1488 union xfs_btree_key
*kp
)
1491 cur
->bc_ops
->init_key_from_rec(kp
,
1492 xfs_btree_rec_addr(cur
, keyno
, block
));
1496 return xfs_btree_key_addr(cur
, keyno
, block
);
1500 * Lookup the record. The cursor is made to point to it, based on dir.
1501 * Return 0 if can't find any such record, 1 for success.
1505 struct xfs_btree_cur
*cur
, /* btree cursor */
1506 xfs_lookup_t dir
, /* <=, ==, or >= */
1507 int *stat
) /* success/failure */
1509 struct xfs_btree_block
*block
; /* current btree block */
1510 __int64_t diff
; /* difference for the current key */
1511 int error
; /* error return value */
1512 int keyno
; /* current key number */
1513 int level
; /* level in the btree */
1514 union xfs_btree_ptr
*pp
; /* ptr to btree block */
1515 union xfs_btree_ptr ptr
; /* ptr to btree block */
1517 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
1518 XFS_BTREE_TRACE_ARGI(cur
, dir
);
1520 XFS_BTREE_STATS_INC(cur
, lookup
);
1525 /* initialise start pointer from cursor */
1526 cur
->bc_ops
->init_ptr_from_cur(cur
, &ptr
);
1530 * Iterate over each level in the btree, starting at the root.
1531 * For each level above the leaves, find the key we need, based
1532 * on the lookup record, then follow the corresponding block
1533 * pointer down to the next level.
1535 for (level
= cur
->bc_nlevels
- 1, diff
= 1; level
>= 0; level
--) {
1536 /* Get the block we need to do the lookup on. */
1537 error
= xfs_btree_lookup_get_block(cur
, level
, pp
, &block
);
1543 * If we already had a key match at a higher level, we
1544 * know we need to use the first entry in this block.
1548 /* Otherwise search this block. Do a binary search. */
1550 int high
; /* high entry number */
1551 int low
; /* low entry number */
1553 /* Set low and high entry numbers, 1-based. */
1555 high
= xfs_btree_get_numrecs(block
);
1557 /* Block is empty, must be an empty leaf. */
1558 ASSERT(level
== 0 && cur
->bc_nlevels
== 1);
1560 cur
->bc_ptrs
[0] = dir
!= XFS_LOOKUP_LE
;
1561 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
1566 /* Binary search the block. */
1567 while (low
<= high
) {
1568 union xfs_btree_key key
;
1569 union xfs_btree_key
*kp
;
1571 XFS_BTREE_STATS_INC(cur
, compare
);
1573 /* keyno is average of low and high. */
1574 keyno
= (low
+ high
) >> 1;
1576 /* Get current search key */
1577 kp
= xfs_lookup_get_search_key(cur
, level
,
1578 keyno
, block
, &key
);
1581 * Compute difference to get next direction:
1582 * - less than, move right
1583 * - greater than, move left
1584 * - equal, we're done
1586 diff
= cur
->bc_ops
->key_diff(cur
, kp
);
1597 * If there are more levels, set up for the next level
1598 * by getting the block number and filling in the cursor.
1602 * If we moved left, need the previous key number,
1603 * unless there isn't one.
1605 if (diff
> 0 && --keyno
< 1)
1607 pp
= xfs_btree_ptr_addr(cur
, keyno
, block
);
1610 error
= xfs_btree_check_ptr(cur
, pp
, 0, level
);
1614 cur
->bc_ptrs
[level
] = keyno
;
1618 /* Done with the search. See if we need to adjust the results. */
1619 if (dir
!= XFS_LOOKUP_LE
&& diff
< 0) {
1622 * If ge search and we went off the end of the block, but it's
1623 * not the last block, we're in the wrong block.
1625 xfs_btree_get_sibling(cur
, block
, &ptr
, XFS_BB_RIGHTSIB
);
1626 if (dir
== XFS_LOOKUP_GE
&&
1627 keyno
> xfs_btree_get_numrecs(block
) &&
1628 !xfs_btree_ptr_is_null(cur
, &ptr
)) {
1631 cur
->bc_ptrs
[0] = keyno
;
1632 error
= xfs_btree_increment(cur
, 0, &i
);
1635 XFS_WANT_CORRUPTED_RETURN(i
== 1);
1636 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
1640 } else if (dir
== XFS_LOOKUP_LE
&& diff
> 0)
1642 cur
->bc_ptrs
[0] = keyno
;
1644 /* Return if we succeeded or not. */
1645 if (keyno
== 0 || keyno
> xfs_btree_get_numrecs(block
))
1647 else if (dir
!= XFS_LOOKUP_EQ
|| diff
== 0)
1651 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
1655 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
1660 * Update keys at all levels from here to the root along the cursor's path.
1664 struct xfs_btree_cur
*cur
,
1665 union xfs_btree_key
*keyp
,
1668 struct xfs_btree_block
*block
;
1670 union xfs_btree_key
*kp
;
1673 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
1674 XFS_BTREE_TRACE_ARGIK(cur
, level
, keyp
);
1676 ASSERT(!(cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
) || level
>= 1);
1679 * Go up the tree from this level toward the root.
1680 * At each level, update the key value to the value input.
1681 * Stop when we reach a level where the cursor isn't pointing
1682 * at the first entry in the block.
1684 for (ptr
= 1; ptr
== 1 && level
< cur
->bc_nlevels
; level
++) {
1688 block
= xfs_btree_get_block(cur
, level
, &bp
);
1690 error
= xfs_btree_check_block(cur
, block
, level
, bp
);
1692 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
1696 ptr
= cur
->bc_ptrs
[level
];
1697 kp
= xfs_btree_key_addr(cur
, ptr
, block
);
1698 xfs_btree_copy_keys(cur
, kp
, keyp
, 1);
1699 xfs_btree_log_keys(cur
, bp
, ptr
, ptr
);
1702 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
1707 * Update the record referred to by cur to the value in the
1708 * given record. This either works (return 0) or gets an
1709 * EFSCORRUPTED error.
1713 struct xfs_btree_cur
*cur
,
1714 union xfs_btree_rec
*rec
)
1716 struct xfs_btree_block
*block
;
1720 union xfs_btree_rec
*rp
;
1722 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
1723 XFS_BTREE_TRACE_ARGR(cur
, rec
);
1725 /* Pick up the current block. */
1726 block
= xfs_btree_get_block(cur
, 0, &bp
);
1729 error
= xfs_btree_check_block(cur
, block
, 0, bp
);
1733 /* Get the address of the rec to be updated. */
1734 ptr
= cur
->bc_ptrs
[0];
1735 rp
= xfs_btree_rec_addr(cur
, ptr
, block
);
1737 /* Fill in the new contents and log them. */
1738 xfs_btree_copy_recs(cur
, rp
, rec
, 1);
1739 xfs_btree_log_recs(cur
, bp
, ptr
, ptr
);
1742 * If we are tracking the last record in the tree and
1743 * we are at the far right edge of the tree, update it.
1745 if (xfs_btree_is_lastrec(cur
, block
, 0)) {
1746 cur
->bc_ops
->update_lastrec(cur
, block
, rec
,
1747 ptr
, LASTREC_UPDATE
);
1750 /* Updating first rec in leaf. Pass new key value up to our parent. */
1752 union xfs_btree_key key
;
1754 cur
->bc_ops
->init_key_from_rec(&key
, rec
);
1755 error
= xfs_btree_updkey(cur
, &key
, 1);
1760 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
1764 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
1769 * Move 1 record left from cur/level if possible.
1770 * Update cur to reflect the new path.
1772 STATIC
int /* error */
1774 struct xfs_btree_cur
*cur
,
1776 int *stat
) /* success/failure */
1778 union xfs_btree_key key
; /* btree key */
1779 struct xfs_buf
*lbp
; /* left buffer pointer */
1780 struct xfs_btree_block
*left
; /* left btree block */
1781 int lrecs
; /* left record count */
1782 struct xfs_buf
*rbp
; /* right buffer pointer */
1783 struct xfs_btree_block
*right
; /* right btree block */
1784 int rrecs
; /* right record count */
1785 union xfs_btree_ptr lptr
; /* left btree pointer */
1786 union xfs_btree_key
*rkp
= NULL
; /* right btree key */
1787 union xfs_btree_ptr
*rpp
= NULL
; /* right address pointer */
1788 union xfs_btree_rec
*rrp
= NULL
; /* right record pointer */
1789 int error
; /* error return value */
1791 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
1792 XFS_BTREE_TRACE_ARGI(cur
, level
);
1794 if ((cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
) &&
1795 level
== cur
->bc_nlevels
- 1)
1798 /* Set up variables for this block as "right". */
1799 right
= xfs_btree_get_block(cur
, level
, &rbp
);
1802 error
= xfs_btree_check_block(cur
, right
, level
, rbp
);
1807 /* If we've got no left sibling then we can't shift an entry left. */
1808 xfs_btree_get_sibling(cur
, right
, &lptr
, XFS_BB_LEFTSIB
);
1809 if (xfs_btree_ptr_is_null(cur
, &lptr
))
1813 * If the cursor entry is the one that would be moved, don't
1814 * do it... it's too complicated.
1816 if (cur
->bc_ptrs
[level
] <= 1)
1819 /* Set up the left neighbor as "left". */
1820 error
= xfs_btree_read_buf_block(cur
, &lptr
, level
, 0, &left
, &lbp
);
1824 /* If it's full, it can't take another entry. */
1825 lrecs
= xfs_btree_get_numrecs(left
);
1826 if (lrecs
== cur
->bc_ops
->get_maxrecs(cur
, level
))
1829 rrecs
= xfs_btree_get_numrecs(right
);
1832 * We add one entry to the left side and remove one for the right side.
1833 * Account for it here, the changes will be updated on disk and logged
1839 XFS_BTREE_STATS_INC(cur
, lshift
);
1840 XFS_BTREE_STATS_ADD(cur
, moves
, 1);
1843 * If non-leaf, copy a key and a ptr to the left block.
1844 * Log the changes to the left block.
1847 /* It's a non-leaf. Move keys and pointers. */
1848 union xfs_btree_key
*lkp
; /* left btree key */
1849 union xfs_btree_ptr
*lpp
; /* left address pointer */
1851 lkp
= xfs_btree_key_addr(cur
, lrecs
, left
);
1852 rkp
= xfs_btree_key_addr(cur
, 1, right
);
1854 lpp
= xfs_btree_ptr_addr(cur
, lrecs
, left
);
1855 rpp
= xfs_btree_ptr_addr(cur
, 1, right
);
1857 error
= xfs_btree_check_ptr(cur
, rpp
, 0, level
);
1861 xfs_btree_copy_keys(cur
, lkp
, rkp
, 1);
1862 xfs_btree_copy_ptrs(cur
, lpp
, rpp
, 1);
1864 xfs_btree_log_keys(cur
, lbp
, lrecs
, lrecs
);
1865 xfs_btree_log_ptrs(cur
, lbp
, lrecs
, lrecs
);
1867 ASSERT(cur
->bc_ops
->keys_inorder(cur
,
1868 xfs_btree_key_addr(cur
, lrecs
- 1, left
), lkp
));
1870 /* It's a leaf. Move records. */
1871 union xfs_btree_rec
*lrp
; /* left record pointer */
1873 lrp
= xfs_btree_rec_addr(cur
, lrecs
, left
);
1874 rrp
= xfs_btree_rec_addr(cur
, 1, right
);
1876 xfs_btree_copy_recs(cur
, lrp
, rrp
, 1);
1877 xfs_btree_log_recs(cur
, lbp
, lrecs
, lrecs
);
1879 ASSERT(cur
->bc_ops
->recs_inorder(cur
,
1880 xfs_btree_rec_addr(cur
, lrecs
- 1, left
), lrp
));
1883 xfs_btree_set_numrecs(left
, lrecs
);
1884 xfs_btree_log_block(cur
, lbp
, XFS_BB_NUMRECS
);
1886 xfs_btree_set_numrecs(right
, rrecs
);
1887 xfs_btree_log_block(cur
, rbp
, XFS_BB_NUMRECS
);
1890 * Slide the contents of right down one entry.
1892 XFS_BTREE_STATS_ADD(cur
, moves
, rrecs
- 1);
1894 /* It's a nonleaf. operate on keys and ptrs */
1896 int i
; /* loop index */
1898 for (i
= 0; i
< rrecs
; i
++) {
1899 error
= xfs_btree_check_ptr(cur
, rpp
, i
+ 1, level
);
1904 xfs_btree_shift_keys(cur
,
1905 xfs_btree_key_addr(cur
, 2, right
),
1907 xfs_btree_shift_ptrs(cur
,
1908 xfs_btree_ptr_addr(cur
, 2, right
),
1911 xfs_btree_log_keys(cur
, rbp
, 1, rrecs
);
1912 xfs_btree_log_ptrs(cur
, rbp
, 1, rrecs
);
1914 /* It's a leaf. operate on records */
1915 xfs_btree_shift_recs(cur
,
1916 xfs_btree_rec_addr(cur
, 2, right
),
1918 xfs_btree_log_recs(cur
, rbp
, 1, rrecs
);
1921 * If it's the first record in the block, we'll need a key
1922 * structure to pass up to the next level (updkey).
1924 cur
->bc_ops
->init_key_from_rec(&key
,
1925 xfs_btree_rec_addr(cur
, 1, right
));
1929 /* Update the parent key values of right. */
1930 error
= xfs_btree_updkey(cur
, rkp
, level
+ 1);
1934 /* Slide the cursor value left one. */
1935 cur
->bc_ptrs
[level
]--;
1937 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
1942 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
1947 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
1952 * Move 1 record right from cur/level if possible.
1953 * Update cur to reflect the new path.
1955 STATIC
int /* error */
1957 struct xfs_btree_cur
*cur
,
1959 int *stat
) /* success/failure */
1961 union xfs_btree_key key
; /* btree key */
1962 struct xfs_buf
*lbp
; /* left buffer pointer */
1963 struct xfs_btree_block
*left
; /* left btree block */
1964 struct xfs_buf
*rbp
; /* right buffer pointer */
1965 struct xfs_btree_block
*right
; /* right btree block */
1966 struct xfs_btree_cur
*tcur
; /* temporary btree cursor */
1967 union xfs_btree_ptr rptr
; /* right block pointer */
1968 union xfs_btree_key
*rkp
; /* right btree key */
1969 int rrecs
; /* right record count */
1970 int lrecs
; /* left record count */
1971 int error
; /* error return value */
1972 int i
; /* loop counter */
1974 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
1975 XFS_BTREE_TRACE_ARGI(cur
, level
);
1977 if ((cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
) &&
1978 (level
== cur
->bc_nlevels
- 1))
1981 /* Set up variables for this block as "left". */
1982 left
= xfs_btree_get_block(cur
, level
, &lbp
);
1985 error
= xfs_btree_check_block(cur
, left
, level
, lbp
);
1990 /* If we've got no right sibling then we can't shift an entry right. */
1991 xfs_btree_get_sibling(cur
, left
, &rptr
, XFS_BB_RIGHTSIB
);
1992 if (xfs_btree_ptr_is_null(cur
, &rptr
))
1996 * If the cursor entry is the one that would be moved, don't
1997 * do it... it's too complicated.
1999 lrecs
= xfs_btree_get_numrecs(left
);
2000 if (cur
->bc_ptrs
[level
] >= lrecs
)
2003 /* Set up the right neighbor as "right". */
2004 error
= xfs_btree_read_buf_block(cur
, &rptr
, level
, 0, &right
, &rbp
);
2008 /* If it's full, it can't take another entry. */
2009 rrecs
= xfs_btree_get_numrecs(right
);
2010 if (rrecs
== cur
->bc_ops
->get_maxrecs(cur
, level
))
2013 XFS_BTREE_STATS_INC(cur
, rshift
);
2014 XFS_BTREE_STATS_ADD(cur
, moves
, rrecs
);
2017 * Make a hole at the start of the right neighbor block, then
2018 * copy the last left block entry to the hole.
2021 /* It's a nonleaf. make a hole in the keys and ptrs */
2022 union xfs_btree_key
*lkp
;
2023 union xfs_btree_ptr
*lpp
;
2024 union xfs_btree_ptr
*rpp
;
2026 lkp
= xfs_btree_key_addr(cur
, lrecs
, left
);
2027 lpp
= xfs_btree_ptr_addr(cur
, lrecs
, left
);
2028 rkp
= xfs_btree_key_addr(cur
, 1, right
);
2029 rpp
= xfs_btree_ptr_addr(cur
, 1, right
);
2032 for (i
= rrecs
- 1; i
>= 0; i
--) {
2033 error
= xfs_btree_check_ptr(cur
, rpp
, i
, level
);
2039 xfs_btree_shift_keys(cur
, rkp
, 1, rrecs
);
2040 xfs_btree_shift_ptrs(cur
, rpp
, 1, rrecs
);
2043 error
= xfs_btree_check_ptr(cur
, lpp
, 0, level
);
2048 /* Now put the new data in, and log it. */
2049 xfs_btree_copy_keys(cur
, rkp
, lkp
, 1);
2050 xfs_btree_copy_ptrs(cur
, rpp
, lpp
, 1);
2052 xfs_btree_log_keys(cur
, rbp
, 1, rrecs
+ 1);
2053 xfs_btree_log_ptrs(cur
, rbp
, 1, rrecs
+ 1);
2055 ASSERT(cur
->bc_ops
->keys_inorder(cur
, rkp
,
2056 xfs_btree_key_addr(cur
, 2, right
)));
2058 /* It's a leaf. make a hole in the records */
2059 union xfs_btree_rec
*lrp
;
2060 union xfs_btree_rec
*rrp
;
2062 lrp
= xfs_btree_rec_addr(cur
, lrecs
, left
);
2063 rrp
= xfs_btree_rec_addr(cur
, 1, right
);
2065 xfs_btree_shift_recs(cur
, rrp
, 1, rrecs
);
2067 /* Now put the new data in, and log it. */
2068 xfs_btree_copy_recs(cur
, rrp
, lrp
, 1);
2069 xfs_btree_log_recs(cur
, rbp
, 1, rrecs
+ 1);
2071 cur
->bc_ops
->init_key_from_rec(&key
, rrp
);
2074 ASSERT(cur
->bc_ops
->recs_inorder(cur
, rrp
,
2075 xfs_btree_rec_addr(cur
, 2, right
)));
2079 * Decrement and log left's numrecs, bump and log right's numrecs.
2081 xfs_btree_set_numrecs(left
, --lrecs
);
2082 xfs_btree_log_block(cur
, lbp
, XFS_BB_NUMRECS
);
2084 xfs_btree_set_numrecs(right
, ++rrecs
);
2085 xfs_btree_log_block(cur
, rbp
, XFS_BB_NUMRECS
);
2088 * Using a temporary cursor, update the parent key values of the
2089 * block on the right.
2091 error
= xfs_btree_dup_cursor(cur
, &tcur
);
2094 i
= xfs_btree_lastrec(tcur
, level
);
2095 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
2097 error
= xfs_btree_increment(tcur
, level
, &i
);
2101 error
= xfs_btree_updkey(tcur
, rkp
, level
+ 1);
2105 xfs_btree_del_cursor(tcur
, XFS_BTREE_NOERROR
);
2107 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
2112 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
2117 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
2121 XFS_BTREE_TRACE_CURSOR(tcur
, XBT_ERROR
);
2122 xfs_btree_del_cursor(tcur
, XFS_BTREE_ERROR
);
2127 * Split cur/level block in half.
2128 * Return new block number and the key to its first
2129 * record (to be inserted into parent).
2131 STATIC
int /* error */
2133 struct xfs_btree_cur
*cur
,
2135 union xfs_btree_ptr
*ptrp
,
2136 union xfs_btree_key
*key
,
2137 struct xfs_btree_cur
**curp
,
2138 int *stat
) /* success/failure */
2140 union xfs_btree_ptr lptr
; /* left sibling block ptr */
2141 struct xfs_buf
*lbp
; /* left buffer pointer */
2142 struct xfs_btree_block
*left
; /* left btree block */
2143 union xfs_btree_ptr rptr
; /* right sibling block ptr */
2144 struct xfs_buf
*rbp
; /* right buffer pointer */
2145 struct xfs_btree_block
*right
; /* right btree block */
2146 union xfs_btree_ptr rrptr
; /* right-right sibling ptr */
2147 struct xfs_buf
*rrbp
; /* right-right buffer pointer */
2148 struct xfs_btree_block
*rrblock
; /* right-right btree block */
2152 int error
; /* error return value */
2157 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
2158 XFS_BTREE_TRACE_ARGIPK(cur
, level
, *ptrp
, key
);
2160 XFS_BTREE_STATS_INC(cur
, split
);
2162 /* Set up left block (current one). */
2163 left
= xfs_btree_get_block(cur
, level
, &lbp
);
2166 error
= xfs_btree_check_block(cur
, left
, level
, lbp
);
2171 xfs_btree_buf_to_ptr(cur
, lbp
, &lptr
);
2173 /* Allocate the new block. If we can't do it, we're toast. Give up. */
2174 error
= cur
->bc_ops
->alloc_block(cur
, &lptr
, &rptr
, 1, stat
);
2179 XFS_BTREE_STATS_INC(cur
, alloc
);
2181 /* Set up the new block as "right". */
2182 error
= xfs_btree_get_buf_block(cur
, &rptr
, 0, &right
, &rbp
);
2186 /* Fill in the btree header for the new right block. */
2187 xfs_btree_init_block(cur
, xfs_btree_get_level(left
), 0, right
);
2190 * Split the entries between the old and the new block evenly.
2191 * Make sure that if there's an odd number of entries now, that
2192 * each new block will have the same number of entries.
2194 lrecs
= xfs_btree_get_numrecs(left
);
2196 if ((lrecs
& 1) && cur
->bc_ptrs
[level
] <= rrecs
+ 1)
2198 src_index
= (lrecs
- rrecs
+ 1);
2200 XFS_BTREE_STATS_ADD(cur
, moves
, rrecs
);
2203 * Copy btree block entries from the left block over to the
2204 * new block, the right. Update the right block and log the
2208 /* It's a non-leaf. Move keys and pointers. */
2209 union xfs_btree_key
*lkp
; /* left btree key */
2210 union xfs_btree_ptr
*lpp
; /* left address pointer */
2211 union xfs_btree_key
*rkp
; /* right btree key */
2212 union xfs_btree_ptr
*rpp
; /* right address pointer */
2214 lkp
= xfs_btree_key_addr(cur
, src_index
, left
);
2215 lpp
= xfs_btree_ptr_addr(cur
, src_index
, left
);
2216 rkp
= xfs_btree_key_addr(cur
, 1, right
);
2217 rpp
= xfs_btree_ptr_addr(cur
, 1, right
);
2220 for (i
= src_index
; i
< rrecs
; i
++) {
2221 error
= xfs_btree_check_ptr(cur
, lpp
, i
, level
);
2227 xfs_btree_copy_keys(cur
, rkp
, lkp
, rrecs
);
2228 xfs_btree_copy_ptrs(cur
, rpp
, lpp
, rrecs
);
2230 xfs_btree_log_keys(cur
, rbp
, 1, rrecs
);
2231 xfs_btree_log_ptrs(cur
, rbp
, 1, rrecs
);
2233 /* Grab the keys to the entries moved to the right block */
2234 xfs_btree_copy_keys(cur
, key
, rkp
, 1);
2236 /* It's a leaf. Move records. */
2237 union xfs_btree_rec
*lrp
; /* left record pointer */
2238 union xfs_btree_rec
*rrp
; /* right record pointer */
2240 lrp
= xfs_btree_rec_addr(cur
, src_index
, left
);
2241 rrp
= xfs_btree_rec_addr(cur
, 1, right
);
2243 xfs_btree_copy_recs(cur
, rrp
, lrp
, rrecs
);
2244 xfs_btree_log_recs(cur
, rbp
, 1, rrecs
);
2246 cur
->bc_ops
->init_key_from_rec(key
,
2247 xfs_btree_rec_addr(cur
, 1, right
));
2252 * Find the left block number by looking in the buffer.
2253 * Adjust numrecs, sibling pointers.
2255 xfs_btree_get_sibling(cur
, left
, &rrptr
, XFS_BB_RIGHTSIB
);
2256 xfs_btree_set_sibling(cur
, right
, &rrptr
, XFS_BB_RIGHTSIB
);
2257 xfs_btree_set_sibling(cur
, right
, &lptr
, XFS_BB_LEFTSIB
);
2258 xfs_btree_set_sibling(cur
, left
, &rptr
, XFS_BB_RIGHTSIB
);
2261 xfs_btree_set_numrecs(left
, lrecs
);
2262 xfs_btree_set_numrecs(right
, xfs_btree_get_numrecs(right
) + rrecs
);
2264 xfs_btree_log_block(cur
, rbp
, XFS_BB_ALL_BITS
);
2265 xfs_btree_log_block(cur
, lbp
, XFS_BB_NUMRECS
| XFS_BB_RIGHTSIB
);
2268 * If there's a block to the new block's right, make that block
2269 * point back to right instead of to left.
2271 if (!xfs_btree_ptr_is_null(cur
, &rrptr
)) {
2272 error
= xfs_btree_read_buf_block(cur
, &rrptr
, level
,
2273 0, &rrblock
, &rrbp
);
2276 xfs_btree_set_sibling(cur
, rrblock
, &rptr
, XFS_BB_LEFTSIB
);
2277 xfs_btree_log_block(cur
, rrbp
, XFS_BB_LEFTSIB
);
2280 * If the cursor is really in the right block, move it there.
2281 * If it's just pointing past the last entry in left, then we'll
2282 * insert there, so don't change anything in that case.
2284 if (cur
->bc_ptrs
[level
] > lrecs
+ 1) {
2285 xfs_btree_setbuf(cur
, level
, rbp
);
2286 cur
->bc_ptrs
[level
] -= lrecs
;
2289 * If there are more levels, we'll need another cursor which refers
2290 * the right block, no matter where this cursor was.
2292 if (level
+ 1 < cur
->bc_nlevels
) {
2293 error
= xfs_btree_dup_cursor(cur
, curp
);
2296 (*curp
)->bc_ptrs
[level
+ 1]++;
2299 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
2303 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
2308 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
2313 * Copy the old inode root contents into a real block and make the
2314 * broot point to it.
2317 xfs_btree_new_iroot(
2318 struct xfs_btree_cur
*cur
, /* btree cursor */
2319 int *logflags
, /* logging flags for inode */
2320 int *stat
) /* return status - 0 fail */
2322 struct xfs_buf
*cbp
; /* buffer for cblock */
2323 struct xfs_btree_block
*block
; /* btree block */
2324 struct xfs_btree_block
*cblock
; /* child btree block */
2325 union xfs_btree_key
*ckp
; /* child key pointer */
2326 union xfs_btree_ptr
*cpp
; /* child ptr pointer */
2327 union xfs_btree_key
*kp
; /* pointer to btree key */
2328 union xfs_btree_ptr
*pp
; /* pointer to block addr */
2329 union xfs_btree_ptr nptr
; /* new block addr */
2330 int level
; /* btree level */
2331 int error
; /* error return code */
2333 int i
; /* loop counter */
2336 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
2337 XFS_BTREE_STATS_INC(cur
, newroot
);
2339 ASSERT(cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
);
2341 level
= cur
->bc_nlevels
- 1;
2343 block
= xfs_btree_get_iroot(cur
);
2344 pp
= xfs_btree_ptr_addr(cur
, 1, block
);
2346 /* Allocate the new block. If we can't do it, we're toast. Give up. */
2347 error
= cur
->bc_ops
->alloc_block(cur
, pp
, &nptr
, 1, stat
);
2351 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
2354 XFS_BTREE_STATS_INC(cur
, alloc
);
2356 /* Copy the root into a real block. */
2357 error
= xfs_btree_get_buf_block(cur
, &nptr
, 0, &cblock
, &cbp
);
2361 memcpy(cblock
, block
, xfs_btree_block_len(cur
));
2363 be16_add_cpu(&block
->bb_level
, 1);
2364 xfs_btree_set_numrecs(block
, 1);
2366 cur
->bc_ptrs
[level
+ 1] = 1;
2368 kp
= xfs_btree_key_addr(cur
, 1, block
);
2369 ckp
= xfs_btree_key_addr(cur
, 1, cblock
);
2370 xfs_btree_copy_keys(cur
, ckp
, kp
, xfs_btree_get_numrecs(cblock
));
2372 cpp
= xfs_btree_ptr_addr(cur
, 1, cblock
);
2374 for (i
= 0; i
< be16_to_cpu(cblock
->bb_numrecs
); i
++) {
2375 error
= xfs_btree_check_ptr(cur
, pp
, i
, level
);
2380 xfs_btree_copy_ptrs(cur
, cpp
, pp
, xfs_btree_get_numrecs(cblock
));
2383 error
= xfs_btree_check_ptr(cur
, &nptr
, 0, level
);
2387 xfs_btree_copy_ptrs(cur
, pp
, &nptr
, 1);
2389 xfs_iroot_realloc(cur
->bc_private
.b
.ip
,
2390 1 - xfs_btree_get_numrecs(cblock
),
2391 cur
->bc_private
.b
.whichfork
);
2393 xfs_btree_setbuf(cur
, level
, cbp
);
2396 * Do all this logging at the end so that
2397 * the root is at the right level.
2399 xfs_btree_log_block(cur
, cbp
, XFS_BB_ALL_BITS
);
2400 xfs_btree_log_keys(cur
, cbp
, 1, be16_to_cpu(cblock
->bb_numrecs
));
2401 xfs_btree_log_ptrs(cur
, cbp
, 1, be16_to_cpu(cblock
->bb_numrecs
));
2404 XFS_ILOG_CORE
| xfs_ilog_fbroot(cur
->bc_private
.b
.whichfork
);
2406 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
2409 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
2414 * Allocate a new root block, fill it in.
2416 STATIC
int /* error */
2418 struct xfs_btree_cur
*cur
, /* btree cursor */
2419 int *stat
) /* success/failure */
2421 struct xfs_btree_block
*block
; /* one half of the old root block */
2422 struct xfs_buf
*bp
; /* buffer containing block */
2423 int error
; /* error return value */
2424 struct xfs_buf
*lbp
; /* left buffer pointer */
2425 struct xfs_btree_block
*left
; /* left btree block */
2426 struct xfs_buf
*nbp
; /* new (root) buffer */
2427 struct xfs_btree_block
*new; /* new (root) btree block */
2428 int nptr
; /* new value for key index, 1 or 2 */
2429 struct xfs_buf
*rbp
; /* right buffer pointer */
2430 struct xfs_btree_block
*right
; /* right btree block */
2431 union xfs_btree_ptr rptr
;
2432 union xfs_btree_ptr lptr
;
2434 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
2435 XFS_BTREE_STATS_INC(cur
, newroot
);
2437 /* initialise our start point from the cursor */
2438 cur
->bc_ops
->init_ptr_from_cur(cur
, &rptr
);
2440 /* Allocate the new block. If we can't do it, we're toast. Give up. */
2441 error
= cur
->bc_ops
->alloc_block(cur
, &rptr
, &lptr
, 1, stat
);
2446 XFS_BTREE_STATS_INC(cur
, alloc
);
2448 /* Set up the new block. */
2449 error
= xfs_btree_get_buf_block(cur
, &lptr
, 0, &new, &nbp
);
2453 /* Set the root in the holding structure increasing the level by 1. */
2454 cur
->bc_ops
->set_root(cur
, &lptr
, 1);
2457 * At the previous root level there are now two blocks: the old root,
2458 * and the new block generated when it was split. We don't know which
2459 * one the cursor is pointing at, so we set up variables "left" and
2460 * "right" for each case.
2462 block
= xfs_btree_get_block(cur
, cur
->bc_nlevels
- 1, &bp
);
2465 error
= xfs_btree_check_block(cur
, block
, cur
->bc_nlevels
- 1, bp
);
2470 xfs_btree_get_sibling(cur
, block
, &rptr
, XFS_BB_RIGHTSIB
);
2471 if (!xfs_btree_ptr_is_null(cur
, &rptr
)) {
2472 /* Our block is left, pick up the right block. */
2474 xfs_btree_buf_to_ptr(cur
, lbp
, &lptr
);
2476 error
= xfs_btree_read_buf_block(cur
, &rptr
,
2477 cur
->bc_nlevels
- 1, 0, &right
, &rbp
);
2483 /* Our block is right, pick up the left block. */
2485 xfs_btree_buf_to_ptr(cur
, rbp
, &rptr
);
2487 xfs_btree_get_sibling(cur
, right
, &lptr
, XFS_BB_LEFTSIB
);
2488 error
= xfs_btree_read_buf_block(cur
, &lptr
,
2489 cur
->bc_nlevels
- 1, 0, &left
, &lbp
);
2495 /* Fill in the new block's btree header and log it. */
2496 xfs_btree_init_block(cur
, cur
->bc_nlevels
, 2, new);
2497 xfs_btree_log_block(cur
, nbp
, XFS_BB_ALL_BITS
);
2498 ASSERT(!xfs_btree_ptr_is_null(cur
, &lptr
) &&
2499 !xfs_btree_ptr_is_null(cur
, &rptr
));
2501 /* Fill in the key data in the new root. */
2502 if (xfs_btree_get_level(left
) > 0) {
2503 xfs_btree_copy_keys(cur
,
2504 xfs_btree_key_addr(cur
, 1, new),
2505 xfs_btree_key_addr(cur
, 1, left
), 1);
2506 xfs_btree_copy_keys(cur
,
2507 xfs_btree_key_addr(cur
, 2, new),
2508 xfs_btree_key_addr(cur
, 1, right
), 1);
2510 cur
->bc_ops
->init_key_from_rec(
2511 xfs_btree_key_addr(cur
, 1, new),
2512 xfs_btree_rec_addr(cur
, 1, left
));
2513 cur
->bc_ops
->init_key_from_rec(
2514 xfs_btree_key_addr(cur
, 2, new),
2515 xfs_btree_rec_addr(cur
, 1, right
));
2517 xfs_btree_log_keys(cur
, nbp
, 1, 2);
2519 /* Fill in the pointer data in the new root. */
2520 xfs_btree_copy_ptrs(cur
,
2521 xfs_btree_ptr_addr(cur
, 1, new), &lptr
, 1);
2522 xfs_btree_copy_ptrs(cur
,
2523 xfs_btree_ptr_addr(cur
, 2, new), &rptr
, 1);
2524 xfs_btree_log_ptrs(cur
, nbp
, 1, 2);
2526 /* Fix up the cursor. */
2527 xfs_btree_setbuf(cur
, cur
->bc_nlevels
, nbp
);
2528 cur
->bc_ptrs
[cur
->bc_nlevels
] = nptr
;
2530 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
2534 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
2537 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
2543 xfs_btree_make_block_unfull(
2544 struct xfs_btree_cur
*cur
, /* btree cursor */
2545 int level
, /* btree level */
2546 int numrecs
,/* # of recs in block */
2547 int *oindex
,/* old tree index */
2548 int *index
, /* new tree index */
2549 union xfs_btree_ptr
*nptr
, /* new btree ptr */
2550 struct xfs_btree_cur
**ncur
, /* new btree cursor */
2551 union xfs_btree_rec
*nrec
, /* new record */
2554 union xfs_btree_key key
; /* new btree key value */
2557 if ((cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
) &&
2558 level
== cur
->bc_nlevels
- 1) {
2559 struct xfs_inode
*ip
= cur
->bc_private
.b
.ip
;
2561 if (numrecs
< cur
->bc_ops
->get_dmaxrecs(cur
, level
)) {
2562 /* A root block that can be made bigger. */
2564 xfs_iroot_realloc(ip
, 1, cur
->bc_private
.b
.whichfork
);
2566 /* A root block that needs replacing */
2569 error
= xfs_btree_new_iroot(cur
, &logflags
, stat
);
2570 if (error
|| *stat
== 0)
2573 xfs_trans_log_inode(cur
->bc_tp
, ip
, logflags
);
2579 /* First, try shifting an entry to the right neighbor. */
2580 error
= xfs_btree_rshift(cur
, level
, stat
);
2584 /* Next, try shifting an entry to the left neighbor. */
2585 error
= xfs_btree_lshift(cur
, level
, stat
);
2590 *oindex
= *index
= cur
->bc_ptrs
[level
];
2595 * Next, try splitting the current block in half.
2597 * If this works we have to re-set our variables because we
2598 * could be in a different block now.
2600 error
= xfs_btree_split(cur
, level
, nptr
, &key
, ncur
, stat
);
2601 if (error
|| *stat
== 0)
2605 *index
= cur
->bc_ptrs
[level
];
2606 cur
->bc_ops
->init_rec_from_key(&key
, nrec
);
2611 * Insert one record/level. Return information to the caller
2612 * allowing the next level up to proceed if necessary.
2616 struct xfs_btree_cur
*cur
, /* btree cursor */
2617 int level
, /* level to insert record at */
2618 union xfs_btree_ptr
*ptrp
, /* i/o: block number inserted */
2619 union xfs_btree_rec
*recp
, /* i/o: record data inserted */
2620 struct xfs_btree_cur
**curp
, /* output: new cursor replacing cur */
2621 int *stat
) /* success/failure */
2623 struct xfs_btree_block
*block
; /* btree block */
2624 struct xfs_buf
*bp
; /* buffer for block */
2625 union xfs_btree_key key
; /* btree key */
2626 union xfs_btree_ptr nptr
; /* new block ptr */
2627 struct xfs_btree_cur
*ncur
; /* new btree cursor */
2628 union xfs_btree_rec nrec
; /* new record count */
2629 int optr
; /* old key/record index */
2630 int ptr
; /* key/record index */
2631 int numrecs
;/* number of records */
2632 int error
; /* error return value */
2637 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
2638 XFS_BTREE_TRACE_ARGIPR(cur
, level
, *ptrp
, recp
);
2643 * If we have an external root pointer, and we've made it to the
2644 * root level, allocate a new root block and we're done.
2646 if (!(cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
) &&
2647 (level
>= cur
->bc_nlevels
)) {
2648 error
= xfs_btree_new_root(cur
, stat
);
2649 xfs_btree_set_ptr_null(cur
, ptrp
);
2651 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
2655 /* If we're off the left edge, return failure. */
2656 ptr
= cur
->bc_ptrs
[level
];
2658 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
2663 /* Make a key out of the record data to be inserted, and save it. */
2664 cur
->bc_ops
->init_key_from_rec(&key
, recp
);
2668 XFS_BTREE_STATS_INC(cur
, insrec
);
2670 /* Get pointers to the btree buffer and block. */
2671 block
= xfs_btree_get_block(cur
, level
, &bp
);
2672 numrecs
= xfs_btree_get_numrecs(block
);
2675 error
= xfs_btree_check_block(cur
, block
, level
, bp
);
2679 /* Check that the new entry is being inserted in the right place. */
2680 if (ptr
<= numrecs
) {
2682 ASSERT(cur
->bc_ops
->recs_inorder(cur
, recp
,
2683 xfs_btree_rec_addr(cur
, ptr
, block
)));
2685 ASSERT(cur
->bc_ops
->keys_inorder(cur
, &key
,
2686 xfs_btree_key_addr(cur
, ptr
, block
)));
2692 * If the block is full, we can't insert the new entry until we
2693 * make the block un-full.
2695 xfs_btree_set_ptr_null(cur
, &nptr
);
2696 if (numrecs
== cur
->bc_ops
->get_maxrecs(cur
, level
)) {
2697 error
= xfs_btree_make_block_unfull(cur
, level
, numrecs
,
2698 &optr
, &ptr
, &nptr
, &ncur
, &nrec
, stat
);
2699 if (error
|| *stat
== 0)
2704 * The current block may have changed if the block was
2705 * previously full and we have just made space in it.
2707 block
= xfs_btree_get_block(cur
, level
, &bp
);
2708 numrecs
= xfs_btree_get_numrecs(block
);
2711 error
= xfs_btree_check_block(cur
, block
, level
, bp
);
2717 * At this point we know there's room for our new entry in the block
2718 * we're pointing at.
2720 XFS_BTREE_STATS_ADD(cur
, moves
, numrecs
- ptr
+ 1);
2723 /* It's a nonleaf. make a hole in the keys and ptrs */
2724 union xfs_btree_key
*kp
;
2725 union xfs_btree_ptr
*pp
;
2727 kp
= xfs_btree_key_addr(cur
, ptr
, block
);
2728 pp
= xfs_btree_ptr_addr(cur
, ptr
, block
);
2731 for (i
= numrecs
- ptr
; i
>= 0; i
--) {
2732 error
= xfs_btree_check_ptr(cur
, pp
, i
, level
);
2738 xfs_btree_shift_keys(cur
, kp
, 1, numrecs
- ptr
+ 1);
2739 xfs_btree_shift_ptrs(cur
, pp
, 1, numrecs
- ptr
+ 1);
2742 error
= xfs_btree_check_ptr(cur
, ptrp
, 0, level
);
2747 /* Now put the new data in, bump numrecs and log it. */
2748 xfs_btree_copy_keys(cur
, kp
, &key
, 1);
2749 xfs_btree_copy_ptrs(cur
, pp
, ptrp
, 1);
2751 xfs_btree_set_numrecs(block
, numrecs
);
2752 xfs_btree_log_ptrs(cur
, bp
, ptr
, numrecs
);
2753 xfs_btree_log_keys(cur
, bp
, ptr
, numrecs
);
2755 if (ptr
< numrecs
) {
2756 ASSERT(cur
->bc_ops
->keys_inorder(cur
, kp
,
2757 xfs_btree_key_addr(cur
, ptr
+ 1, block
)));
2761 /* It's a leaf. make a hole in the records */
2762 union xfs_btree_rec
*rp
;
2764 rp
= xfs_btree_rec_addr(cur
, ptr
, block
);
2766 xfs_btree_shift_recs(cur
, rp
, 1, numrecs
- ptr
+ 1);
2768 /* Now put the new data in, bump numrecs and log it. */
2769 xfs_btree_copy_recs(cur
, rp
, recp
, 1);
2770 xfs_btree_set_numrecs(block
, ++numrecs
);
2771 xfs_btree_log_recs(cur
, bp
, ptr
, numrecs
);
2773 if (ptr
< numrecs
) {
2774 ASSERT(cur
->bc_ops
->recs_inorder(cur
, rp
,
2775 xfs_btree_rec_addr(cur
, ptr
+ 1, block
)));
2780 /* Log the new number of records in the btree header. */
2781 xfs_btree_log_block(cur
, bp
, XFS_BB_NUMRECS
);
2783 /* If we inserted at the start of a block, update the parents' keys. */
2785 error
= xfs_btree_updkey(cur
, &key
, level
+ 1);
2791 * If we are tracking the last record in the tree and
2792 * we are at the far right edge of the tree, update it.
2794 if (xfs_btree_is_lastrec(cur
, block
, level
)) {
2795 cur
->bc_ops
->update_lastrec(cur
, block
, recp
,
2796 ptr
, LASTREC_INSREC
);
2800 * Return the new block number, if any.
2801 * If there is one, give back a record value and a cursor too.
2804 if (!xfs_btree_ptr_is_null(cur
, &nptr
)) {
2809 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
2814 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
2819 * Insert the record at the point referenced by cur.
2821 * A multi-level split of the tree on insert will invalidate the original
2822 * cursor. All callers of this function should assume that the cursor is
2823 * no longer valid and revalidate it.
2827 struct xfs_btree_cur
*cur
,
2830 int error
; /* error return value */
2831 int i
; /* result value, 0 for failure */
2832 int level
; /* current level number in btree */
2833 union xfs_btree_ptr nptr
; /* new block number (split result) */
2834 struct xfs_btree_cur
*ncur
; /* new cursor (split result) */
2835 struct xfs_btree_cur
*pcur
; /* previous level's cursor */
2836 union xfs_btree_rec rec
; /* record to insert */
2842 xfs_btree_set_ptr_null(cur
, &nptr
);
2843 cur
->bc_ops
->init_rec_from_cur(cur
, &rec
);
2846 * Loop going up the tree, starting at the leaf level.
2847 * Stop when we don't get a split block, that must mean that
2848 * the insert is finished with this level.
2852 * Insert nrec/nptr into this level of the tree.
2853 * Note if we fail, nptr will be null.
2855 error
= xfs_btree_insrec(pcur
, level
, &nptr
, &rec
, &ncur
, &i
);
2858 xfs_btree_del_cursor(pcur
, XFS_BTREE_ERROR
);
2862 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
2866 * See if the cursor we just used is trash.
2867 * Can't trash the caller's cursor, but otherwise we should
2868 * if ncur is a new cursor or we're about to be done.
2871 (ncur
|| xfs_btree_ptr_is_null(cur
, &nptr
))) {
2872 /* Save the state from the cursor before we trash it */
2873 if (cur
->bc_ops
->update_cursor
)
2874 cur
->bc_ops
->update_cursor(pcur
, cur
);
2875 cur
->bc_nlevels
= pcur
->bc_nlevels
;
2876 xfs_btree_del_cursor(pcur
, XFS_BTREE_NOERROR
);
2878 /* If we got a new cursor, switch to it. */
2883 } while (!xfs_btree_ptr_is_null(cur
, &nptr
));
2885 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
2889 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
2894 * Try to merge a non-leaf block back into the inode root.
2896 * Note: the killroot names comes from the fact that we're effectively
2897 * killing the old root block. But because we can't just delete the
2898 * inode we have to copy the single block it was pointing to into the
2902 xfs_btree_kill_iroot(
2903 struct xfs_btree_cur
*cur
)
2905 int whichfork
= cur
->bc_private
.b
.whichfork
;
2906 struct xfs_inode
*ip
= cur
->bc_private
.b
.ip
;
2907 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
2908 struct xfs_btree_block
*block
;
2909 struct xfs_btree_block
*cblock
;
2910 union xfs_btree_key
*kp
;
2911 union xfs_btree_key
*ckp
;
2912 union xfs_btree_ptr
*pp
;
2913 union xfs_btree_ptr
*cpp
;
2914 struct xfs_buf
*cbp
;
2919 union xfs_btree_ptr ptr
;
2923 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
2925 ASSERT(cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
);
2926 ASSERT(cur
->bc_nlevels
> 1);
2929 * Don't deal with the root block needs to be a leaf case.
2930 * We're just going to turn the thing back into extents anyway.
2932 level
= cur
->bc_nlevels
- 1;
2937 * Give up if the root has multiple children.
2939 block
= xfs_btree_get_iroot(cur
);
2940 if (xfs_btree_get_numrecs(block
) != 1)
2943 cblock
= xfs_btree_get_block(cur
, level
- 1, &cbp
);
2944 numrecs
= xfs_btree_get_numrecs(cblock
);
2947 * Only do this if the next level will fit.
2948 * Then the data must be copied up to the inode,
2949 * instead of freeing the root you free the next level.
2951 if (numrecs
> cur
->bc_ops
->get_dmaxrecs(cur
, level
))
2954 XFS_BTREE_STATS_INC(cur
, killroot
);
2957 xfs_btree_get_sibling(cur
, block
, &ptr
, XFS_BB_LEFTSIB
);
2958 ASSERT(xfs_btree_ptr_is_null(cur
, &ptr
));
2959 xfs_btree_get_sibling(cur
, block
, &ptr
, XFS_BB_RIGHTSIB
);
2960 ASSERT(xfs_btree_ptr_is_null(cur
, &ptr
));
2963 index
= numrecs
- cur
->bc_ops
->get_maxrecs(cur
, level
);
2965 xfs_iroot_realloc(cur
->bc_private
.b
.ip
, index
,
2966 cur
->bc_private
.b
.whichfork
);
2967 block
= ifp
->if_broot
;
2970 be16_add_cpu(&block
->bb_numrecs
, index
);
2971 ASSERT(block
->bb_numrecs
== cblock
->bb_numrecs
);
2973 kp
= xfs_btree_key_addr(cur
, 1, block
);
2974 ckp
= xfs_btree_key_addr(cur
, 1, cblock
);
2975 xfs_btree_copy_keys(cur
, kp
, ckp
, numrecs
);
2977 pp
= xfs_btree_ptr_addr(cur
, 1, block
);
2978 cpp
= xfs_btree_ptr_addr(cur
, 1, cblock
);
2980 for (i
= 0; i
< numrecs
; i
++) {
2983 error
= xfs_btree_check_ptr(cur
, cpp
, i
, level
- 1);
2985 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
2990 xfs_btree_copy_ptrs(cur
, pp
, cpp
, numrecs
);
2992 cur
->bc_ops
->free_block(cur
, cbp
);
2993 XFS_BTREE_STATS_INC(cur
, free
);
2995 cur
->bc_bufs
[level
- 1] = NULL
;
2996 be16_add_cpu(&block
->bb_level
, -1);
2997 xfs_trans_log_inode(cur
->bc_tp
, ip
,
2998 XFS_ILOG_CORE
| xfs_ilog_fbroot(cur
->bc_private
.b
.whichfork
));
3001 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
3006 * Kill the current root node, and replace it with it's only child node.
3009 xfs_btree_kill_root(
3010 struct xfs_btree_cur
*cur
,
3013 union xfs_btree_ptr
*newroot
)
3017 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
3018 XFS_BTREE_STATS_INC(cur
, killroot
);
3021 * Update the root pointer, decreasing the level by 1 and then
3022 * free the old root.
3024 cur
->bc_ops
->set_root(cur
, newroot
, -1);
3026 error
= cur
->bc_ops
->free_block(cur
, bp
);
3028 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
3032 XFS_BTREE_STATS_INC(cur
, free
);
3034 cur
->bc_bufs
[level
] = NULL
;
3035 cur
->bc_ra
[level
] = 0;
3038 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
3043 xfs_btree_dec_cursor(
3044 struct xfs_btree_cur
*cur
,
3052 error
= xfs_btree_decrement(cur
, level
, &i
);
3057 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
3063 * Single level of the btree record deletion routine.
3064 * Delete record pointed to by cur/level.
3065 * Remove the record from its block then rebalance the tree.
3066 * Return 0 for error, 1 for done, 2 to go on to the next level.
3068 STATIC
int /* error */
3070 struct xfs_btree_cur
*cur
, /* btree cursor */
3071 int level
, /* level removing record from */
3072 int *stat
) /* fail/done/go-on */
3074 struct xfs_btree_block
*block
; /* btree block */
3075 union xfs_btree_ptr cptr
; /* current block ptr */
3076 struct xfs_buf
*bp
; /* buffer for block */
3077 int error
; /* error return value */
3078 int i
; /* loop counter */
3079 union xfs_btree_key key
; /* storage for keyp */
3080 union xfs_btree_key
*keyp
= &key
; /* passed to the next level */
3081 union xfs_btree_ptr lptr
; /* left sibling block ptr */
3082 struct xfs_buf
*lbp
; /* left buffer pointer */
3083 struct xfs_btree_block
*left
; /* left btree block */
3084 int lrecs
= 0; /* left record count */
3085 int ptr
; /* key/record index */
3086 union xfs_btree_ptr rptr
; /* right sibling block ptr */
3087 struct xfs_buf
*rbp
; /* right buffer pointer */
3088 struct xfs_btree_block
*right
; /* right btree block */
3089 struct xfs_btree_block
*rrblock
; /* right-right btree block */
3090 struct xfs_buf
*rrbp
; /* right-right buffer pointer */
3091 int rrecs
= 0; /* right record count */
3092 struct xfs_btree_cur
*tcur
; /* temporary btree cursor */
3093 int numrecs
; /* temporary numrec count */
3095 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
3096 XFS_BTREE_TRACE_ARGI(cur
, level
);
3100 /* Get the index of the entry being deleted, check for nothing there. */
3101 ptr
= cur
->bc_ptrs
[level
];
3103 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
3108 /* Get the buffer & block containing the record or key/ptr. */
3109 block
= xfs_btree_get_block(cur
, level
, &bp
);
3110 numrecs
= xfs_btree_get_numrecs(block
);
3113 error
= xfs_btree_check_block(cur
, block
, level
, bp
);
3118 /* Fail if we're off the end of the block. */
3119 if (ptr
> numrecs
) {
3120 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
3125 XFS_BTREE_STATS_INC(cur
, delrec
);
3126 XFS_BTREE_STATS_ADD(cur
, moves
, numrecs
- ptr
);
3128 /* Excise the entries being deleted. */
3130 /* It's a nonleaf. operate on keys and ptrs */
3131 union xfs_btree_key
*lkp
;
3132 union xfs_btree_ptr
*lpp
;
3134 lkp
= xfs_btree_key_addr(cur
, ptr
+ 1, block
);
3135 lpp
= xfs_btree_ptr_addr(cur
, ptr
+ 1, block
);
3138 for (i
= 0; i
< numrecs
- ptr
; i
++) {
3139 error
= xfs_btree_check_ptr(cur
, lpp
, i
, level
);
3145 if (ptr
< numrecs
) {
3146 xfs_btree_shift_keys(cur
, lkp
, -1, numrecs
- ptr
);
3147 xfs_btree_shift_ptrs(cur
, lpp
, -1, numrecs
- ptr
);
3148 xfs_btree_log_keys(cur
, bp
, ptr
, numrecs
- 1);
3149 xfs_btree_log_ptrs(cur
, bp
, ptr
, numrecs
- 1);
3153 * If it's the first record in the block, we'll need to pass a
3154 * key up to the next level (updkey).
3157 keyp
= xfs_btree_key_addr(cur
, 1, block
);
3159 /* It's a leaf. operate on records */
3160 if (ptr
< numrecs
) {
3161 xfs_btree_shift_recs(cur
,
3162 xfs_btree_rec_addr(cur
, ptr
+ 1, block
),
3164 xfs_btree_log_recs(cur
, bp
, ptr
, numrecs
- 1);
3168 * If it's the first record in the block, we'll need a key
3169 * structure to pass up to the next level (updkey).
3172 cur
->bc_ops
->init_key_from_rec(&key
,
3173 xfs_btree_rec_addr(cur
, 1, block
));
3179 * Decrement and log the number of entries in the block.
3181 xfs_btree_set_numrecs(block
, --numrecs
);
3182 xfs_btree_log_block(cur
, bp
, XFS_BB_NUMRECS
);
3185 * If we are tracking the last record in the tree and
3186 * we are at the far right edge of the tree, update it.
3188 if (xfs_btree_is_lastrec(cur
, block
, level
)) {
3189 cur
->bc_ops
->update_lastrec(cur
, block
, NULL
,
3190 ptr
, LASTREC_DELREC
);
3194 * We're at the root level. First, shrink the root block in-memory.
3195 * Try to get rid of the next level down. If we can't then there's
3196 * nothing left to do.
3198 if (level
== cur
->bc_nlevels
- 1) {
3199 if (cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
) {
3200 xfs_iroot_realloc(cur
->bc_private
.b
.ip
, -1,
3201 cur
->bc_private
.b
.whichfork
);
3203 error
= xfs_btree_kill_iroot(cur
);
3207 error
= xfs_btree_dec_cursor(cur
, level
, stat
);
3215 * If this is the root level, and there's only one entry left,
3216 * and it's NOT the leaf level, then we can get rid of this
3219 if (numrecs
== 1 && level
> 0) {
3220 union xfs_btree_ptr
*pp
;
3222 * pp is still set to the first pointer in the block.
3223 * Make it the new root of the btree.
3225 pp
= xfs_btree_ptr_addr(cur
, 1, block
);
3226 error
= xfs_btree_kill_root(cur
, bp
, level
, pp
);
3229 } else if (level
> 0) {
3230 error
= xfs_btree_dec_cursor(cur
, level
, stat
);
3239 * If we deleted the leftmost entry in the block, update the
3240 * key values above us in the tree.
3243 error
= xfs_btree_updkey(cur
, keyp
, level
+ 1);
3249 * If the number of records remaining in the block is at least
3250 * the minimum, we're done.
3252 if (numrecs
>= cur
->bc_ops
->get_minrecs(cur
, level
)) {
3253 error
= xfs_btree_dec_cursor(cur
, level
, stat
);
3260 * Otherwise, we have to move some records around to keep the
3261 * tree balanced. Look at the left and right sibling blocks to
3262 * see if we can re-balance by moving only one record.
3264 xfs_btree_get_sibling(cur
, block
, &rptr
, XFS_BB_RIGHTSIB
);
3265 xfs_btree_get_sibling(cur
, block
, &lptr
, XFS_BB_LEFTSIB
);
3267 if (cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
) {
3269 * One child of root, need to get a chance to copy its contents
3270 * into the root and delete it. Can't go up to next level,
3271 * there's nothing to delete there.
3273 if (xfs_btree_ptr_is_null(cur
, &rptr
) &&
3274 xfs_btree_ptr_is_null(cur
, &lptr
) &&
3275 level
== cur
->bc_nlevels
- 2) {
3276 error
= xfs_btree_kill_iroot(cur
);
3278 error
= xfs_btree_dec_cursor(cur
, level
, stat
);
3285 ASSERT(!xfs_btree_ptr_is_null(cur
, &rptr
) ||
3286 !xfs_btree_ptr_is_null(cur
, &lptr
));
3289 * Duplicate the cursor so our btree manipulations here won't
3290 * disrupt the next level up.
3292 error
= xfs_btree_dup_cursor(cur
, &tcur
);
3297 * If there's a right sibling, see if it's ok to shift an entry
3300 if (!xfs_btree_ptr_is_null(cur
, &rptr
)) {
3302 * Move the temp cursor to the last entry in the next block.
3303 * Actually any entry but the first would suffice.
3305 i
= xfs_btree_lastrec(tcur
, level
);
3306 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
3308 error
= xfs_btree_increment(tcur
, level
, &i
);
3311 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
3313 i
= xfs_btree_lastrec(tcur
, level
);
3314 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
3316 /* Grab a pointer to the block. */
3317 right
= xfs_btree_get_block(tcur
, level
, &rbp
);
3319 error
= xfs_btree_check_block(tcur
, right
, level
, rbp
);
3323 /* Grab the current block number, for future use. */
3324 xfs_btree_get_sibling(tcur
, right
, &cptr
, XFS_BB_LEFTSIB
);
3327 * If right block is full enough so that removing one entry
3328 * won't make it too empty, and left-shifting an entry out
3329 * of right to us works, we're done.
3331 if (xfs_btree_get_numrecs(right
) - 1 >=
3332 cur
->bc_ops
->get_minrecs(tcur
, level
)) {
3333 error
= xfs_btree_lshift(tcur
, level
, &i
);
3337 ASSERT(xfs_btree_get_numrecs(block
) >=
3338 cur
->bc_ops
->get_minrecs(tcur
, level
));
3340 xfs_btree_del_cursor(tcur
, XFS_BTREE_NOERROR
);
3343 error
= xfs_btree_dec_cursor(cur
, level
, stat
);
3351 * Otherwise, grab the number of records in right for
3352 * future reference, and fix up the temp cursor to point
3353 * to our block again (last record).
3355 rrecs
= xfs_btree_get_numrecs(right
);
3356 if (!xfs_btree_ptr_is_null(cur
, &lptr
)) {
3357 i
= xfs_btree_firstrec(tcur
, level
);
3358 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
3360 error
= xfs_btree_decrement(tcur
, level
, &i
);
3363 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
3368 * If there's a left sibling, see if it's ok to shift an entry
3371 if (!xfs_btree_ptr_is_null(cur
, &lptr
)) {
3373 * Move the temp cursor to the first entry in the
3376 i
= xfs_btree_firstrec(tcur
, level
);
3377 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
3379 error
= xfs_btree_decrement(tcur
, level
, &i
);
3382 i
= xfs_btree_firstrec(tcur
, level
);
3383 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
3385 /* Grab a pointer to the block. */
3386 left
= xfs_btree_get_block(tcur
, level
, &lbp
);
3388 error
= xfs_btree_check_block(cur
, left
, level
, lbp
);
3392 /* Grab the current block number, for future use. */
3393 xfs_btree_get_sibling(tcur
, left
, &cptr
, XFS_BB_RIGHTSIB
);
3396 * If left block is full enough so that removing one entry
3397 * won't make it too empty, and right-shifting an entry out
3398 * of left to us works, we're done.
3400 if (xfs_btree_get_numrecs(left
) - 1 >=
3401 cur
->bc_ops
->get_minrecs(tcur
, level
)) {
3402 error
= xfs_btree_rshift(tcur
, level
, &i
);
3406 ASSERT(xfs_btree_get_numrecs(block
) >=
3407 cur
->bc_ops
->get_minrecs(tcur
, level
));
3408 xfs_btree_del_cursor(tcur
, XFS_BTREE_NOERROR
);
3412 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
3419 * Otherwise, grab the number of records in right for
3422 lrecs
= xfs_btree_get_numrecs(left
);
3425 /* Delete the temp cursor, we're done with it. */
3426 xfs_btree_del_cursor(tcur
, XFS_BTREE_NOERROR
);
3429 /* If here, we need to do a join to keep the tree balanced. */
3430 ASSERT(!xfs_btree_ptr_is_null(cur
, &cptr
));
3432 if (!xfs_btree_ptr_is_null(cur
, &lptr
) &&
3433 lrecs
+ xfs_btree_get_numrecs(block
) <=
3434 cur
->bc_ops
->get_maxrecs(cur
, level
)) {
3436 * Set "right" to be the starting block,
3437 * "left" to be the left neighbor.
3442 error
= xfs_btree_read_buf_block(cur
, &lptr
, level
,
3448 * If that won't work, see if we can join with the right neighbor block.
3450 } else if (!xfs_btree_ptr_is_null(cur
, &rptr
) &&
3451 rrecs
+ xfs_btree_get_numrecs(block
) <=
3452 cur
->bc_ops
->get_maxrecs(cur
, level
)) {
3454 * Set "left" to be the starting block,
3455 * "right" to be the right neighbor.
3460 error
= xfs_btree_read_buf_block(cur
, &rptr
, level
,
3466 * Otherwise, we can't fix the imbalance.
3467 * Just return. This is probably a logic error, but it's not fatal.
3470 error
= xfs_btree_dec_cursor(cur
, level
, stat
);
3476 rrecs
= xfs_btree_get_numrecs(right
);
3477 lrecs
= xfs_btree_get_numrecs(left
);
3480 * We're now going to join "left" and "right" by moving all the stuff
3481 * in "right" to "left" and deleting "right".
3483 XFS_BTREE_STATS_ADD(cur
, moves
, rrecs
);
3485 /* It's a non-leaf. Move keys and pointers. */
3486 union xfs_btree_key
*lkp
; /* left btree key */
3487 union xfs_btree_ptr
*lpp
; /* left address pointer */
3488 union xfs_btree_key
*rkp
; /* right btree key */
3489 union xfs_btree_ptr
*rpp
; /* right address pointer */
3491 lkp
= xfs_btree_key_addr(cur
, lrecs
+ 1, left
);
3492 lpp
= xfs_btree_ptr_addr(cur
, lrecs
+ 1, left
);
3493 rkp
= xfs_btree_key_addr(cur
, 1, right
);
3494 rpp
= xfs_btree_ptr_addr(cur
, 1, right
);
3496 for (i
= 1; i
< rrecs
; i
++) {
3497 error
= xfs_btree_check_ptr(cur
, rpp
, i
, level
);
3502 xfs_btree_copy_keys(cur
, lkp
, rkp
, rrecs
);
3503 xfs_btree_copy_ptrs(cur
, lpp
, rpp
, rrecs
);
3505 xfs_btree_log_keys(cur
, lbp
, lrecs
+ 1, lrecs
+ rrecs
);
3506 xfs_btree_log_ptrs(cur
, lbp
, lrecs
+ 1, lrecs
+ rrecs
);
3508 /* It's a leaf. Move records. */
3509 union xfs_btree_rec
*lrp
; /* left record pointer */
3510 union xfs_btree_rec
*rrp
; /* right record pointer */
3512 lrp
= xfs_btree_rec_addr(cur
, lrecs
+ 1, left
);
3513 rrp
= xfs_btree_rec_addr(cur
, 1, right
);
3515 xfs_btree_copy_recs(cur
, lrp
, rrp
, rrecs
);
3516 xfs_btree_log_recs(cur
, lbp
, lrecs
+ 1, lrecs
+ rrecs
);
3519 XFS_BTREE_STATS_INC(cur
, join
);
3522 * Fix up the number of records and right block pointer in the
3523 * surviving block, and log it.
3525 xfs_btree_set_numrecs(left
, lrecs
+ rrecs
);
3526 xfs_btree_get_sibling(cur
, right
, &cptr
, XFS_BB_RIGHTSIB
),
3527 xfs_btree_set_sibling(cur
, left
, &cptr
, XFS_BB_RIGHTSIB
);
3528 xfs_btree_log_block(cur
, lbp
, XFS_BB_NUMRECS
| XFS_BB_RIGHTSIB
);
3530 /* If there is a right sibling, point it to the remaining block. */
3531 xfs_btree_get_sibling(cur
, left
, &cptr
, XFS_BB_RIGHTSIB
);
3532 if (!xfs_btree_ptr_is_null(cur
, &cptr
)) {
3533 error
= xfs_btree_read_buf_block(cur
, &cptr
, level
,
3534 0, &rrblock
, &rrbp
);
3537 xfs_btree_set_sibling(cur
, rrblock
, &lptr
, XFS_BB_LEFTSIB
);
3538 xfs_btree_log_block(cur
, rrbp
, XFS_BB_LEFTSIB
);
3541 /* Free the deleted block. */
3542 error
= cur
->bc_ops
->free_block(cur
, rbp
);
3545 XFS_BTREE_STATS_INC(cur
, free
);
3548 * If we joined with the left neighbor, set the buffer in the
3549 * cursor to the left block, and fix up the index.
3552 cur
->bc_bufs
[level
] = lbp
;
3553 cur
->bc_ptrs
[level
] += lrecs
;
3554 cur
->bc_ra
[level
] = 0;
3557 * If we joined with the right neighbor and there's a level above
3558 * us, increment the cursor at that level.
3560 else if ((cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
) ||
3561 (level
+ 1 < cur
->bc_nlevels
)) {
3562 error
= xfs_btree_increment(cur
, level
+ 1, &i
);
3568 * Readjust the ptr at this level if it's not a leaf, since it's
3569 * still pointing at the deletion point, which makes the cursor
3570 * inconsistent. If this makes the ptr 0, the caller fixes it up.
3571 * We can't use decrement because it would change the next level up.
3574 cur
->bc_ptrs
[level
]--;
3576 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
3577 /* Return value means the next level up has something to do. */
3582 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
3584 xfs_btree_del_cursor(tcur
, XFS_BTREE_ERROR
);
3589 * Delete the record pointed to by cur.
3590 * The cursor refers to the place where the record was (could be inserted)
3591 * when the operation returns.
3595 struct xfs_btree_cur
*cur
,
3596 int *stat
) /* success/failure */
3598 int error
; /* error return value */
3602 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
3605 * Go up the tree, starting at leaf level.
3607 * If 2 is returned then a join was done; go to the next level.
3608 * Otherwise we are done.
3610 for (level
= 0, i
= 2; i
== 2; level
++) {
3611 error
= xfs_btree_delrec(cur
, level
, &i
);
3617 for (level
= 1; level
< cur
->bc_nlevels
; level
++) {
3618 if (cur
->bc_ptrs
[level
] == 0) {
3619 error
= xfs_btree_decrement(cur
, level
, &i
);
3627 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
3631 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
3636 * Get the data from the pointed-to record.
3640 struct xfs_btree_cur
*cur
, /* btree cursor */
3641 union xfs_btree_rec
**recp
, /* output: btree record */
3642 int *stat
) /* output: success/failure */
3644 struct xfs_btree_block
*block
; /* btree block */
3645 struct xfs_buf
*bp
; /* buffer pointer */
3646 int ptr
; /* record number */
3648 int error
; /* error return value */
3651 ptr
= cur
->bc_ptrs
[0];
3652 block
= xfs_btree_get_block(cur
, 0, &bp
);
3655 error
= xfs_btree_check_block(cur
, block
, 0, bp
);
3661 * Off the right end or left end, return failure.
3663 if (ptr
> xfs_btree_get_numrecs(block
) || ptr
<= 0) {
3669 * Point to the record and extract its data.
3671 *recp
= xfs_btree_rec_addr(cur
, ptr
, block
);