2 * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 * Cursor allocation zone.
24 kmem_zone_t
*xfs_btree_cur_zone
;
27 * Btree magic numbers.
29 static const __uint32_t xfs_magics
[2][XFS_BTNUM_MAX
] = {
30 { XFS_ABTB_MAGIC
, XFS_ABTC_MAGIC
, XFS_BMAP_MAGIC
, XFS_IBT_MAGIC
},
31 { XFS_ABTB_CRC_MAGIC
, XFS_ABTC_CRC_MAGIC
,
32 XFS_BMAP_CRC_MAGIC
, XFS_IBT_CRC_MAGIC
}
34 #define xfs_btree_magic(cur) \
35 xfs_magics[!!((cur)->bc_flags & XFS_BTREE_CRC_BLOCKS)][cur->bc_btnum]
38 STATIC
int /* error (0 or EFSCORRUPTED) */
39 xfs_btree_check_lblock(
40 struct xfs_btree_cur
*cur
, /* btree cursor */
41 struct xfs_btree_block
*block
, /* btree long form block pointer */
42 int level
, /* level of the btree block */
43 struct xfs_buf
*bp
) /* buffer for block, if any */
45 int lblock_ok
= 1; /* block passes checks */
46 struct xfs_mount
*mp
; /* file system mount point */
50 if (xfs_sb_version_hascrc(&mp
->m_sb
)) {
51 lblock_ok
= lblock_ok
&&
52 uuid_equal(&block
->bb_u
.l
.bb_uuid
, &mp
->m_sb
.sb_uuid
) &&
53 block
->bb_u
.l
.bb_blkno
== cpu_to_be64(
54 bp
? bp
->b_bn
: XFS_BUF_DADDR_NULL
);
57 lblock_ok
= lblock_ok
&&
58 be32_to_cpu(block
->bb_magic
) == xfs_btree_magic(cur
) &&
59 be16_to_cpu(block
->bb_level
) == level
&&
60 be16_to_cpu(block
->bb_numrecs
) <=
61 cur
->bc_ops
->get_maxrecs(cur
, level
) &&
62 block
->bb_u
.l
.bb_leftsib
&&
63 (block
->bb_u
.l
.bb_leftsib
== cpu_to_be64(NULLDFSBNO
) ||
64 XFS_FSB_SANITY_CHECK(mp
,
65 be64_to_cpu(block
->bb_u
.l
.bb_leftsib
))) &&
66 block
->bb_u
.l
.bb_rightsib
&&
67 (block
->bb_u
.l
.bb_rightsib
== cpu_to_be64(NULLDFSBNO
) ||
68 XFS_FSB_SANITY_CHECK(mp
,
69 be64_to_cpu(block
->bb_u
.l
.bb_rightsib
)));
71 if (unlikely(XFS_TEST_ERROR(!lblock_ok
, mp
,
72 XFS_ERRTAG_BTREE_CHECK_LBLOCK
,
73 XFS_RANDOM_BTREE_CHECK_LBLOCK
))) {
75 trace_xfs_btree_corrupt(bp
, _RET_IP_
);
76 XFS_ERROR_REPORT(__func__
, XFS_ERRLEVEL_LOW
, mp
);
77 return XFS_ERROR(EFSCORRUPTED
);
82 STATIC
int /* error (0 or EFSCORRUPTED) */
83 xfs_btree_check_sblock(
84 struct xfs_btree_cur
*cur
, /* btree cursor */
85 struct xfs_btree_block
*block
, /* btree short form block pointer */
86 int level
, /* level of the btree block */
87 struct xfs_buf
*bp
) /* buffer containing block */
89 struct xfs_mount
*mp
; /* file system mount point */
90 struct xfs_buf
*agbp
; /* buffer for ag. freespace struct */
91 struct xfs_agf
*agf
; /* ag. freespace structure */
92 xfs_agblock_t agflen
; /* native ag. freespace length */
93 int sblock_ok
= 1; /* block passes checks */
96 agbp
= cur
->bc_private
.a
.agbp
;
97 agf
= XFS_BUF_TO_AGF(agbp
);
98 agflen
= be32_to_cpu(agf
->agf_length
);
100 if (xfs_sb_version_hascrc(&mp
->m_sb
)) {
101 sblock_ok
= sblock_ok
&&
102 uuid_equal(&block
->bb_u
.s
.bb_uuid
, &mp
->m_sb
.sb_uuid
) &&
103 block
->bb_u
.s
.bb_blkno
== cpu_to_be64(
104 bp
? bp
->b_bn
: XFS_BUF_DADDR_NULL
);
107 sblock_ok
= sblock_ok
&&
108 be32_to_cpu(block
->bb_magic
) == xfs_btree_magic(cur
) &&
109 be16_to_cpu(block
->bb_level
) == level
&&
110 be16_to_cpu(block
->bb_numrecs
) <=
111 cur
->bc_ops
->get_maxrecs(cur
, level
) &&
112 (block
->bb_u
.s
.bb_leftsib
== cpu_to_be32(NULLAGBLOCK
) ||
113 be32_to_cpu(block
->bb_u
.s
.bb_leftsib
) < agflen
) &&
114 block
->bb_u
.s
.bb_leftsib
&&
115 (block
->bb_u
.s
.bb_rightsib
== cpu_to_be32(NULLAGBLOCK
) ||
116 be32_to_cpu(block
->bb_u
.s
.bb_rightsib
) < agflen
) &&
117 block
->bb_u
.s
.bb_rightsib
;
119 if (unlikely(XFS_TEST_ERROR(!sblock_ok
, mp
,
120 XFS_ERRTAG_BTREE_CHECK_SBLOCK
,
121 XFS_RANDOM_BTREE_CHECK_SBLOCK
))) {
123 trace_xfs_btree_corrupt(bp
, _RET_IP_
);
124 XFS_ERROR_REPORT(__func__
, XFS_ERRLEVEL_LOW
, mp
);
125 return XFS_ERROR(EFSCORRUPTED
);
131 * Debug routine: check that block header is ok.
134 xfs_btree_check_block(
135 struct xfs_btree_cur
*cur
, /* btree cursor */
136 struct xfs_btree_block
*block
, /* generic btree block pointer */
137 int level
, /* level of the btree block */
138 struct xfs_buf
*bp
) /* buffer containing block, if any */
140 if (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
)
141 return xfs_btree_check_lblock(cur
, block
, level
, bp
);
143 return xfs_btree_check_sblock(cur
, block
, level
, bp
);
147 * Check that (long) pointer is ok.
149 int /* error (0 or EFSCORRUPTED) */
150 xfs_btree_check_lptr(
151 struct xfs_btree_cur
*cur
, /* btree cursor */
152 xfs_dfsbno_t bno
, /* btree block disk address */
153 int level
) /* btree block level */
155 XFS_WANT_CORRUPTED_RETURN(
158 XFS_FSB_SANITY_CHECK(cur
->bc_mp
, bno
));
164 * Check that (short) pointer is ok.
166 STATIC
int /* error (0 or EFSCORRUPTED) */
167 xfs_btree_check_sptr(
168 struct xfs_btree_cur
*cur
, /* btree cursor */
169 xfs_agblock_t bno
, /* btree block disk address */
170 int level
) /* btree block level */
172 xfs_agblock_t agblocks
= cur
->bc_mp
->m_sb
.sb_agblocks
;
174 XFS_WANT_CORRUPTED_RETURN(
176 bno
!= NULLAGBLOCK
&&
183 * Check that block ptr is ok.
185 STATIC
int /* error (0 or EFSCORRUPTED) */
187 struct xfs_btree_cur
*cur
, /* btree cursor */
188 union xfs_btree_ptr
*ptr
, /* btree block disk address */
189 int index
, /* offset from ptr to check */
190 int level
) /* btree block level */
192 if (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
) {
193 return xfs_btree_check_lptr(cur
,
194 be64_to_cpu((&ptr
->l
)[index
]), level
);
196 return xfs_btree_check_sptr(cur
,
197 be32_to_cpu((&ptr
->s
)[index
]), level
);
203 * Calculate CRC on the whole btree block and stuff it into the
204 * long-form btree header.
206 * Prior to calculting the CRC, pull the LSN out of the buffer log item and put
207 * it into the buffer so recovery knows what the last modifcation was that made
211 xfs_btree_lblock_calc_crc(
214 struct xfs_btree_block
*block
= XFS_BUF_TO_BLOCK(bp
);
215 struct xfs_buf_log_item
*bip
= bp
->b_fspriv
;
217 if (!xfs_sb_version_hascrc(&bp
->b_target
->bt_mount
->m_sb
))
220 block
->bb_u
.l
.bb_lsn
= cpu_to_be64(bip
->bli_item
.li_lsn
);
221 xfs_update_cksum(bp
->b_addr
, BBTOB(bp
->b_length
),
222 XFS_BTREE_LBLOCK_CRC_OFF
);
226 xfs_btree_lblock_verify_crc(
229 if (xfs_sb_version_hascrc(&bp
->b_target
->bt_mount
->m_sb
))
230 return xfs_verify_cksum(bp
->b_addr
, BBTOB(bp
->b_length
),
231 XFS_BTREE_LBLOCK_CRC_OFF
);
236 * Calculate CRC on the whole btree block and stuff it into the
237 * short-form btree header.
239 * Prior to calculting the CRC, pull the LSN out of the buffer log item and put
240 * it into the buffer so recovery knows what the last modifcation was that made
244 xfs_btree_sblock_calc_crc(
247 struct xfs_btree_block
*block
= XFS_BUF_TO_BLOCK(bp
);
248 struct xfs_buf_log_item
*bip
= bp
->b_fspriv
;
250 if (!xfs_sb_version_hascrc(&bp
->b_target
->bt_mount
->m_sb
))
253 block
->bb_u
.s
.bb_lsn
= cpu_to_be64(bip
->bli_item
.li_lsn
);
254 xfs_update_cksum(bp
->b_addr
, BBTOB(bp
->b_length
),
255 XFS_BTREE_SBLOCK_CRC_OFF
);
259 xfs_btree_sblock_verify_crc(
262 if (xfs_sb_version_hascrc(&bp
->b_target
->bt_mount
->m_sb
))
263 return xfs_verify_cksum(bp
->b_addr
, BBTOB(bp
->b_length
),
264 XFS_BTREE_SBLOCK_CRC_OFF
);
269 * Delete the btree cursor.
272 xfs_btree_del_cursor(
273 xfs_btree_cur_t
*cur
, /* btree cursor */
274 int error
) /* del because of error */
276 int i
; /* btree level */
279 * Clear the buffer pointers, and release the buffers.
280 * If we're doing this in the face of an error, we
281 * need to make sure to inspect all of the entries
282 * in the bc_bufs array for buffers to be unlocked.
283 * This is because some of the btree code works from
284 * level n down to 0, and if we get an error along
285 * the way we won't have initialized all the entries
288 for (i
= 0; i
< cur
->bc_nlevels
; i
++) {
290 xfs_trans_brelse(cur
->bc_tp
, cur
->bc_bufs
[i
]);
295 * Can't free a bmap cursor without having dealt with the
296 * allocated indirect blocks' accounting.
298 ASSERT(cur
->bc_btnum
!= XFS_BTNUM_BMAP
||
299 cur
->bc_private
.b
.allocated
== 0);
303 kmem_zone_free(xfs_btree_cur_zone
, cur
);
307 * Duplicate the btree cursor.
308 * Allocate a new one, copy the record, re-get the buffers.
311 xfs_btree_dup_cursor(
312 xfs_btree_cur_t
*cur
, /* input cursor */
313 xfs_btree_cur_t
**ncur
) /* output cursor */
315 xfs_buf_t
*bp
; /* btree block's buffer pointer */
316 int error
; /* error return value */
317 int i
; /* level number of btree block */
318 xfs_mount_t
*mp
; /* mount structure for filesystem */
319 xfs_btree_cur_t
*new; /* new cursor value */
320 xfs_trans_t
*tp
; /* transaction pointer, can be NULL */
326 * Allocate a new cursor like the old one.
328 new = cur
->bc_ops
->dup_cursor(cur
);
331 * Copy the record currently in the cursor.
333 new->bc_rec
= cur
->bc_rec
;
336 * For each level current, re-get the buffer and copy the ptr value.
338 for (i
= 0; i
< new->bc_nlevels
; i
++) {
339 new->bc_ptrs
[i
] = cur
->bc_ptrs
[i
];
340 new->bc_ra
[i
] = cur
->bc_ra
[i
];
341 bp
= cur
->bc_bufs
[i
];
343 error
= xfs_trans_read_buf(mp
, tp
, mp
->m_ddev_targp
,
344 XFS_BUF_ADDR(bp
), mp
->m_bsize
,
346 cur
->bc_ops
->buf_ops
);
348 xfs_btree_del_cursor(new, error
);
353 new->bc_bufs
[i
] = bp
;
360 * XFS btree block layout and addressing:
362 * There are two types of blocks in the btree: leaf and non-leaf blocks.
364 * The leaf record start with a header then followed by records containing
365 * the values. A non-leaf block also starts with the same header, and
366 * then first contains lookup keys followed by an equal number of pointers
367 * to the btree blocks at the previous level.
369 * +--------+-------+-------+-------+-------+-------+-------+
370 * Leaf: | header | rec 1 | rec 2 | rec 3 | rec 4 | rec 5 | rec N |
371 * +--------+-------+-------+-------+-------+-------+-------+
373 * +--------+-------+-------+-------+-------+-------+-------+
374 * Non-Leaf: | header | key 1 | key 2 | key N | ptr 1 | ptr 2 | ptr N |
375 * +--------+-------+-------+-------+-------+-------+-------+
377 * The header is called struct xfs_btree_block for reasons better left unknown
378 * and comes in different versions for short (32bit) and long (64bit) block
379 * pointers. The record and key structures are defined by the btree instances
380 * and opaque to the btree core. The block pointers are simple disk endian
381 * integers, available in a short (32bit) and long (64bit) variant.
383 * The helpers below calculate the offset of a given record, key or pointer
384 * into a btree block (xfs_btree_*_offset) or return a pointer to the given
385 * record, key or pointer (xfs_btree_*_addr). Note that all addressing
386 * inside the btree block is done using indices starting at one, not zero!
390 * Return size of the btree block header for this btree instance.
392 static inline size_t xfs_btree_block_len(struct xfs_btree_cur
*cur
)
394 if (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
) {
395 if (cur
->bc_flags
& XFS_BTREE_CRC_BLOCKS
)
396 return XFS_BTREE_LBLOCK_CRC_LEN
;
397 return XFS_BTREE_LBLOCK_LEN
;
400 if (cur
->bc_flags
& XFS_BTREE_CRC_BLOCKS
)
401 return XFS_BTREE_SBLOCK_CRC_LEN
;
402 return XFS_BTREE_SBLOCK_LEN
;
406 * Return size of btree block pointers for this btree instance.
408 static inline size_t xfs_btree_ptr_len(struct xfs_btree_cur
*cur
)
410 return (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
) ?
411 sizeof(__be64
) : sizeof(__be32
);
415 * Calculate offset of the n-th record in a btree block.
418 xfs_btree_rec_offset(
419 struct xfs_btree_cur
*cur
,
422 return xfs_btree_block_len(cur
) +
423 (n
- 1) * cur
->bc_ops
->rec_len
;
427 * Calculate offset of the n-th key in a btree block.
430 xfs_btree_key_offset(
431 struct xfs_btree_cur
*cur
,
434 return xfs_btree_block_len(cur
) +
435 (n
- 1) * cur
->bc_ops
->key_len
;
439 * Calculate offset of the n-th block pointer in a btree block.
442 xfs_btree_ptr_offset(
443 struct xfs_btree_cur
*cur
,
447 return xfs_btree_block_len(cur
) +
448 cur
->bc_ops
->get_maxrecs(cur
, level
) * cur
->bc_ops
->key_len
+
449 (n
- 1) * xfs_btree_ptr_len(cur
);
453 * Return a pointer to the n-th record in the btree block.
455 STATIC
union xfs_btree_rec
*
457 struct xfs_btree_cur
*cur
,
459 struct xfs_btree_block
*block
)
461 return (union xfs_btree_rec
*)
462 ((char *)block
+ xfs_btree_rec_offset(cur
, n
));
466 * Return a pointer to the n-th key in the btree block.
468 STATIC
union xfs_btree_key
*
470 struct xfs_btree_cur
*cur
,
472 struct xfs_btree_block
*block
)
474 return (union xfs_btree_key
*)
475 ((char *)block
+ xfs_btree_key_offset(cur
, n
));
479 * Return a pointer to the n-th block pointer in the btree block.
481 STATIC
union xfs_btree_ptr
*
483 struct xfs_btree_cur
*cur
,
485 struct xfs_btree_block
*block
)
487 int level
= xfs_btree_get_level(block
);
489 ASSERT(block
->bb_level
!= 0);
491 return (union xfs_btree_ptr
*)
492 ((char *)block
+ xfs_btree_ptr_offset(cur
, n
, level
));
496 * Get a the root block which is stored in the inode.
498 * For now this btree implementation assumes the btree root is always
499 * stored in the if_broot field of an inode fork.
501 STATIC
struct xfs_btree_block
*
503 struct xfs_btree_cur
*cur
)
505 struct xfs_ifork
*ifp
;
507 ifp
= XFS_IFORK_PTR(cur
->bc_private
.b
.ip
, cur
->bc_private
.b
.whichfork
);
508 return (struct xfs_btree_block
*)ifp
->if_broot
;
512 * Retrieve the block pointer from the cursor at the given level.
513 * This may be an inode btree root or from a buffer.
515 STATIC
struct xfs_btree_block
* /* generic btree block pointer */
517 struct xfs_btree_cur
*cur
, /* btree cursor */
518 int level
, /* level in btree */
519 struct xfs_buf
**bpp
) /* buffer containing the block */
521 if ((cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
) &&
522 (level
== cur
->bc_nlevels
- 1)) {
524 return xfs_btree_get_iroot(cur
);
527 *bpp
= cur
->bc_bufs
[level
];
528 return XFS_BUF_TO_BLOCK(*bpp
);
532 * Get a buffer for the block, return it with no data read.
533 * Long-form addressing.
535 xfs_buf_t
* /* buffer for fsbno */
537 xfs_mount_t
*mp
, /* file system mount point */
538 xfs_trans_t
*tp
, /* transaction pointer */
539 xfs_fsblock_t fsbno
, /* file system block number */
540 uint lock
) /* lock flags for get_buf */
542 xfs_buf_t
*bp
; /* buffer pointer (return value) */
543 xfs_daddr_t d
; /* real disk block address */
545 ASSERT(fsbno
!= NULLFSBLOCK
);
546 d
= XFS_FSB_TO_DADDR(mp
, fsbno
);
547 bp
= xfs_trans_get_buf(tp
, mp
->m_ddev_targp
, d
, mp
->m_bsize
, lock
);
548 ASSERT(!xfs_buf_geterror(bp
));
553 * Get a buffer for the block, return it with no data read.
554 * Short-form addressing.
556 xfs_buf_t
* /* buffer for agno/agbno */
558 xfs_mount_t
*mp
, /* file system mount point */
559 xfs_trans_t
*tp
, /* transaction pointer */
560 xfs_agnumber_t agno
, /* allocation group number */
561 xfs_agblock_t agbno
, /* allocation group block number */
562 uint lock
) /* lock flags for get_buf */
564 xfs_buf_t
*bp
; /* buffer pointer (return value) */
565 xfs_daddr_t d
; /* real disk block address */
567 ASSERT(agno
!= NULLAGNUMBER
);
568 ASSERT(agbno
!= NULLAGBLOCK
);
569 d
= XFS_AGB_TO_DADDR(mp
, agno
, agbno
);
570 bp
= xfs_trans_get_buf(tp
, mp
->m_ddev_targp
, d
, mp
->m_bsize
, lock
);
571 ASSERT(!xfs_buf_geterror(bp
));
576 * Check for the cursor referring to the last block at the given level.
578 int /* 1=is last block, 0=not last block */
579 xfs_btree_islastblock(
580 xfs_btree_cur_t
*cur
, /* btree cursor */
581 int level
) /* level to check */
583 struct xfs_btree_block
*block
; /* generic btree block pointer */
584 xfs_buf_t
*bp
; /* buffer containing block */
586 block
= xfs_btree_get_block(cur
, level
, &bp
);
587 xfs_btree_check_block(cur
, block
, level
, bp
);
588 if (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
)
589 return block
->bb_u
.l
.bb_rightsib
== cpu_to_be64(NULLDFSBNO
);
591 return block
->bb_u
.s
.bb_rightsib
== cpu_to_be32(NULLAGBLOCK
);
595 * Change the cursor to point to the first record at the given level.
596 * Other levels are unaffected.
598 STATIC
int /* success=1, failure=0 */
600 xfs_btree_cur_t
*cur
, /* btree cursor */
601 int level
) /* level to change */
603 struct xfs_btree_block
*block
; /* generic btree block pointer */
604 xfs_buf_t
*bp
; /* buffer containing block */
607 * Get the block pointer for this level.
609 block
= xfs_btree_get_block(cur
, level
, &bp
);
610 xfs_btree_check_block(cur
, block
, level
, bp
);
612 * It's empty, there is no such record.
614 if (!block
->bb_numrecs
)
617 * Set the ptr value to 1, that's the first record/key.
619 cur
->bc_ptrs
[level
] = 1;
624 * Change the cursor to point to the last record in the current block
625 * at the given level. Other levels are unaffected.
627 STATIC
int /* success=1, failure=0 */
629 xfs_btree_cur_t
*cur
, /* btree cursor */
630 int level
) /* level to change */
632 struct xfs_btree_block
*block
; /* generic btree block pointer */
633 xfs_buf_t
*bp
; /* buffer containing block */
636 * Get the block pointer for this level.
638 block
= xfs_btree_get_block(cur
, level
, &bp
);
639 xfs_btree_check_block(cur
, block
, level
, bp
);
641 * It's empty, there is no such record.
643 if (!block
->bb_numrecs
)
646 * Set the ptr value to numrecs, that's the last record/key.
648 cur
->bc_ptrs
[level
] = be16_to_cpu(block
->bb_numrecs
);
653 * Compute first and last byte offsets for the fields given.
654 * Interprets the offsets table, which contains struct field offsets.
658 __int64_t fields
, /* bitmask of fields */
659 const short *offsets
, /* table of field offsets */
660 int nbits
, /* number of bits to inspect */
661 int *first
, /* output: first byte offset */
662 int *last
) /* output: last byte offset */
664 int i
; /* current bit number */
665 __int64_t imask
; /* mask for current bit number */
669 * Find the lowest bit, so the first byte offset.
671 for (i
= 0, imask
= 1LL; ; i
++, imask
<<= 1) {
672 if (imask
& fields
) {
678 * Find the highest bit, so the last byte offset.
680 for (i
= nbits
- 1, imask
= 1LL << i
; ; i
--, imask
>>= 1) {
681 if (imask
& fields
) {
682 *last
= offsets
[i
+ 1] - 1;
689 * Get a buffer for the block, return it read in.
690 * Long-form addressing.
694 struct xfs_mount
*mp
, /* file system mount point */
695 struct xfs_trans
*tp
, /* transaction pointer */
696 xfs_fsblock_t fsbno
, /* file system block number */
697 uint lock
, /* lock flags for read_buf */
698 struct xfs_buf
**bpp
, /* buffer for fsbno */
699 int refval
, /* ref count value for buffer */
700 const struct xfs_buf_ops
*ops
)
702 struct xfs_buf
*bp
; /* return value */
703 xfs_daddr_t d
; /* real disk block address */
706 ASSERT(fsbno
!= NULLFSBLOCK
);
707 d
= XFS_FSB_TO_DADDR(mp
, fsbno
);
708 error
= xfs_trans_read_buf(mp
, tp
, mp
->m_ddev_targp
, d
,
709 mp
->m_bsize
, lock
, &bp
, ops
);
712 ASSERT(!xfs_buf_geterror(bp
));
714 xfs_buf_set_ref(bp
, refval
);
720 xfs_btree_readahead_lblock(
721 struct xfs_btree_cur
*cur
,
723 struct xfs_btree_block
*block
)
726 xfs_dfsbno_t left
= be64_to_cpu(block
->bb_u
.l
.bb_leftsib
);
727 xfs_dfsbno_t right
= be64_to_cpu(block
->bb_u
.l
.bb_rightsib
);
729 if ((lr
& XFS_BTCUR_LEFTRA
) && left
!= NULLDFSBNO
) {
730 xfs_btree_reada_bufl(cur
->bc_mp
, left
, 1,
731 cur
->bc_ops
->buf_ops
);
735 if ((lr
& XFS_BTCUR_RIGHTRA
) && right
!= NULLDFSBNO
) {
736 xfs_btree_reada_bufl(cur
->bc_mp
, right
, 1,
737 cur
->bc_ops
->buf_ops
);
745 xfs_btree_readahead_sblock(
746 struct xfs_btree_cur
*cur
,
748 struct xfs_btree_block
*block
)
751 xfs_agblock_t left
= be32_to_cpu(block
->bb_u
.s
.bb_leftsib
);
752 xfs_agblock_t right
= be32_to_cpu(block
->bb_u
.s
.bb_rightsib
);
755 if ((lr
& XFS_BTCUR_LEFTRA
) && left
!= NULLAGBLOCK
) {
756 xfs_btree_reada_bufs(cur
->bc_mp
, cur
->bc_private
.a
.agno
,
757 left
, 1, cur
->bc_ops
->buf_ops
);
761 if ((lr
& XFS_BTCUR_RIGHTRA
) && right
!= NULLAGBLOCK
) {
762 xfs_btree_reada_bufs(cur
->bc_mp
, cur
->bc_private
.a
.agno
,
763 right
, 1, cur
->bc_ops
->buf_ops
);
771 * Read-ahead btree blocks, at the given level.
772 * Bits in lr are set from XFS_BTCUR_{LEFT,RIGHT}RA.
776 struct xfs_btree_cur
*cur
, /* btree cursor */
777 int lev
, /* level in btree */
778 int lr
) /* left/right bits */
780 struct xfs_btree_block
*block
;
783 * No readahead needed if we are at the root level and the
784 * btree root is stored in the inode.
786 if ((cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
) &&
787 (lev
== cur
->bc_nlevels
- 1))
790 if ((cur
->bc_ra
[lev
] | lr
) == cur
->bc_ra
[lev
])
793 cur
->bc_ra
[lev
] |= lr
;
794 block
= XFS_BUF_TO_BLOCK(cur
->bc_bufs
[lev
]);
796 if (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
)
797 return xfs_btree_readahead_lblock(cur
, lr
, block
);
798 return xfs_btree_readahead_sblock(cur
, lr
, block
);
802 * Set the buffer for level "lev" in the cursor to bp, releasing
803 * any previous buffer.
807 xfs_btree_cur_t
*cur
, /* btree cursor */
808 int lev
, /* level in btree */
809 xfs_buf_t
*bp
) /* new buffer to set */
811 struct xfs_btree_block
*b
; /* btree block */
813 if (cur
->bc_bufs
[lev
])
814 xfs_trans_brelse(cur
->bc_tp
, cur
->bc_bufs
[lev
]);
815 cur
->bc_bufs
[lev
] = bp
;
818 b
= XFS_BUF_TO_BLOCK(bp
);
819 if (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
) {
820 if (b
->bb_u
.l
.bb_leftsib
== cpu_to_be64(NULLDFSBNO
))
821 cur
->bc_ra
[lev
] |= XFS_BTCUR_LEFTRA
;
822 if (b
->bb_u
.l
.bb_rightsib
== cpu_to_be64(NULLDFSBNO
))
823 cur
->bc_ra
[lev
] |= XFS_BTCUR_RIGHTRA
;
825 if (b
->bb_u
.s
.bb_leftsib
== cpu_to_be32(NULLAGBLOCK
))
826 cur
->bc_ra
[lev
] |= XFS_BTCUR_LEFTRA
;
827 if (b
->bb_u
.s
.bb_rightsib
== cpu_to_be32(NULLAGBLOCK
))
828 cur
->bc_ra
[lev
] |= XFS_BTCUR_RIGHTRA
;
833 xfs_btree_ptr_is_null(
834 struct xfs_btree_cur
*cur
,
835 union xfs_btree_ptr
*ptr
)
837 if (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
)
838 return ptr
->l
== cpu_to_be64(NULLDFSBNO
);
840 return ptr
->s
== cpu_to_be32(NULLAGBLOCK
);
844 xfs_btree_set_ptr_null(
845 struct xfs_btree_cur
*cur
,
846 union xfs_btree_ptr
*ptr
)
848 if (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
)
849 ptr
->l
= cpu_to_be64(NULLDFSBNO
);
851 ptr
->s
= cpu_to_be32(NULLAGBLOCK
);
855 * Get/set/init sibling pointers
858 xfs_btree_get_sibling(
859 struct xfs_btree_cur
*cur
,
860 struct xfs_btree_block
*block
,
861 union xfs_btree_ptr
*ptr
,
864 ASSERT(lr
== XFS_BB_LEFTSIB
|| lr
== XFS_BB_RIGHTSIB
);
866 if (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
) {
867 if (lr
== XFS_BB_RIGHTSIB
)
868 ptr
->l
= block
->bb_u
.l
.bb_rightsib
;
870 ptr
->l
= block
->bb_u
.l
.bb_leftsib
;
872 if (lr
== XFS_BB_RIGHTSIB
)
873 ptr
->s
= block
->bb_u
.s
.bb_rightsib
;
875 ptr
->s
= block
->bb_u
.s
.bb_leftsib
;
880 xfs_btree_set_sibling(
881 struct xfs_btree_cur
*cur
,
882 struct xfs_btree_block
*block
,
883 union xfs_btree_ptr
*ptr
,
886 ASSERT(lr
== XFS_BB_LEFTSIB
|| lr
== XFS_BB_RIGHTSIB
);
888 if (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
) {
889 if (lr
== XFS_BB_RIGHTSIB
)
890 block
->bb_u
.l
.bb_rightsib
= ptr
->l
;
892 block
->bb_u
.l
.bb_leftsib
= ptr
->l
;
894 if (lr
== XFS_BB_RIGHTSIB
)
895 block
->bb_u
.s
.bb_rightsib
= ptr
->s
;
897 block
->bb_u
.s
.bb_leftsib
= ptr
->s
;
902 xfs_btree_init_block_int(
903 struct xfs_mount
*mp
,
904 struct xfs_btree_block
*buf
,
912 buf
->bb_magic
= cpu_to_be32(magic
);
913 buf
->bb_level
= cpu_to_be16(level
);
914 buf
->bb_numrecs
= cpu_to_be16(numrecs
);
916 if (flags
& XFS_BTREE_LONG_PTRS
) {
917 buf
->bb_u
.l
.bb_leftsib
= cpu_to_be64(NULLDFSBNO
);
918 buf
->bb_u
.l
.bb_rightsib
= cpu_to_be64(NULLDFSBNO
);
919 if (flags
& XFS_BTREE_CRC_BLOCKS
) {
920 buf
->bb_u
.l
.bb_blkno
= cpu_to_be64(blkno
);
921 buf
->bb_u
.l
.bb_owner
= cpu_to_be64(owner
);
922 uuid_copy(&buf
->bb_u
.l
.bb_uuid
, &mp
->m_sb
.sb_uuid
);
923 buf
->bb_u
.l
.bb_pad
= 0;
926 /* owner is a 32 bit value on short blocks */
927 __u32 __owner
= (__u32
)owner
;
929 buf
->bb_u
.s
.bb_leftsib
= cpu_to_be32(NULLAGBLOCK
);
930 buf
->bb_u
.s
.bb_rightsib
= cpu_to_be32(NULLAGBLOCK
);
931 if (flags
& XFS_BTREE_CRC_BLOCKS
) {
932 buf
->bb_u
.s
.bb_blkno
= cpu_to_be64(blkno
);
933 buf
->bb_u
.s
.bb_owner
= cpu_to_be32(__owner
);
934 uuid_copy(&buf
->bb_u
.s
.bb_uuid
, &mp
->m_sb
.sb_uuid
);
940 xfs_btree_init_block(
941 struct xfs_mount
*mp
,
949 xfs_btree_init_block_int(mp
, XFS_BUF_TO_BLOCK(bp
), bp
->b_bn
,
950 magic
, level
, numrecs
, owner
, flags
);
954 xfs_btree_init_block_cur(
955 struct xfs_btree_cur
*cur
,
963 * we can pull the owner from the cursor right now as the different
964 * owners align directly with the pointer size of the btree. This may
965 * change in future, but is safe for current users of the generic btree
968 if (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
)
969 owner
= cur
->bc_private
.b
.ip
->i_ino
;
971 owner
= cur
->bc_private
.a
.agno
;
973 xfs_btree_init_block_int(cur
->bc_mp
, XFS_BUF_TO_BLOCK(bp
), bp
->b_bn
,
974 xfs_btree_magic(cur
), level
, numrecs
,
975 owner
, cur
->bc_flags
);
979 * Return true if ptr is the last record in the btree and
980 * we need to track updates to this record. The decision
981 * will be further refined in the update_lastrec method.
984 xfs_btree_is_lastrec(
985 struct xfs_btree_cur
*cur
,
986 struct xfs_btree_block
*block
,
989 union xfs_btree_ptr ptr
;
993 if (!(cur
->bc_flags
& XFS_BTREE_LASTREC_UPDATE
))
996 xfs_btree_get_sibling(cur
, block
, &ptr
, XFS_BB_RIGHTSIB
);
997 if (!xfs_btree_ptr_is_null(cur
, &ptr
))
1003 xfs_btree_buf_to_ptr(
1004 struct xfs_btree_cur
*cur
,
1006 union xfs_btree_ptr
*ptr
)
1008 if (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
)
1009 ptr
->l
= cpu_to_be64(XFS_DADDR_TO_FSB(cur
->bc_mp
,
1012 ptr
->s
= cpu_to_be32(xfs_daddr_to_agbno(cur
->bc_mp
,
1018 xfs_btree_ptr_to_daddr(
1019 struct xfs_btree_cur
*cur
,
1020 union xfs_btree_ptr
*ptr
)
1022 if (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
) {
1023 ASSERT(ptr
->l
!= cpu_to_be64(NULLDFSBNO
));
1025 return XFS_FSB_TO_DADDR(cur
->bc_mp
, be64_to_cpu(ptr
->l
));
1027 ASSERT(cur
->bc_private
.a
.agno
!= NULLAGNUMBER
);
1028 ASSERT(ptr
->s
!= cpu_to_be32(NULLAGBLOCK
));
1030 return XFS_AGB_TO_DADDR(cur
->bc_mp
, cur
->bc_private
.a
.agno
,
1031 be32_to_cpu(ptr
->s
));
1037 struct xfs_btree_cur
*cur
,
1040 switch (cur
->bc_btnum
) {
1043 xfs_buf_set_ref(bp
, XFS_ALLOC_BTREE_REF
);
1046 xfs_buf_set_ref(bp
, XFS_INO_BTREE_REF
);
1048 case XFS_BTNUM_BMAP
:
1049 xfs_buf_set_ref(bp
, XFS_BMAP_BTREE_REF
);
1057 xfs_btree_get_buf_block(
1058 struct xfs_btree_cur
*cur
,
1059 union xfs_btree_ptr
*ptr
,
1061 struct xfs_btree_block
**block
,
1062 struct xfs_buf
**bpp
)
1064 struct xfs_mount
*mp
= cur
->bc_mp
;
1067 /* need to sort out how callers deal with failures first */
1068 ASSERT(!(flags
& XBF_TRYLOCK
));
1070 d
= xfs_btree_ptr_to_daddr(cur
, ptr
);
1071 *bpp
= xfs_trans_get_buf(cur
->bc_tp
, mp
->m_ddev_targp
, d
,
1072 mp
->m_bsize
, flags
);
1077 (*bpp
)->b_ops
= cur
->bc_ops
->buf_ops
;
1078 *block
= XFS_BUF_TO_BLOCK(*bpp
);
1083 * Read in the buffer at the given ptr and return the buffer and
1084 * the block pointer within the buffer.
1087 xfs_btree_read_buf_block(
1088 struct xfs_btree_cur
*cur
,
1089 union xfs_btree_ptr
*ptr
,
1092 struct xfs_btree_block
**block
,
1093 struct xfs_buf
**bpp
)
1095 struct xfs_mount
*mp
= cur
->bc_mp
;
1099 /* need to sort out how callers deal with failures first */
1100 ASSERT(!(flags
& XBF_TRYLOCK
));
1102 d
= xfs_btree_ptr_to_daddr(cur
, ptr
);
1103 error
= xfs_trans_read_buf(mp
, cur
->bc_tp
, mp
->m_ddev_targp
, d
,
1104 mp
->m_bsize
, flags
, bpp
,
1105 cur
->bc_ops
->buf_ops
);
1109 ASSERT(!xfs_buf_geterror(*bpp
));
1110 xfs_btree_set_refs(cur
, *bpp
);
1111 *block
= XFS_BUF_TO_BLOCK(*bpp
);
1116 * Copy keys from one btree block to another.
1119 xfs_btree_copy_keys(
1120 struct xfs_btree_cur
*cur
,
1121 union xfs_btree_key
*dst_key
,
1122 union xfs_btree_key
*src_key
,
1125 ASSERT(numkeys
>= 0);
1126 memcpy(dst_key
, src_key
, numkeys
* cur
->bc_ops
->key_len
);
1130 * Copy records from one btree block to another.
1133 xfs_btree_copy_recs(
1134 struct xfs_btree_cur
*cur
,
1135 union xfs_btree_rec
*dst_rec
,
1136 union xfs_btree_rec
*src_rec
,
1139 ASSERT(numrecs
>= 0);
1140 memcpy(dst_rec
, src_rec
, numrecs
* cur
->bc_ops
->rec_len
);
1144 * Copy block pointers from one btree block to another.
1147 xfs_btree_copy_ptrs(
1148 struct xfs_btree_cur
*cur
,
1149 union xfs_btree_ptr
*dst_ptr
,
1150 union xfs_btree_ptr
*src_ptr
,
1153 ASSERT(numptrs
>= 0);
1154 memcpy(dst_ptr
, src_ptr
, numptrs
* xfs_btree_ptr_len(cur
));
1158 * Shift keys one index left/right inside a single btree block.
1161 xfs_btree_shift_keys(
1162 struct xfs_btree_cur
*cur
,
1163 union xfs_btree_key
*key
,
1169 ASSERT(numkeys
>= 0);
1170 ASSERT(dir
== 1 || dir
== -1);
1172 dst_key
= (char *)key
+ (dir
* cur
->bc_ops
->key_len
);
1173 memmove(dst_key
, key
, numkeys
* cur
->bc_ops
->key_len
);
1177 * Shift records one index left/right inside a single btree block.
1180 xfs_btree_shift_recs(
1181 struct xfs_btree_cur
*cur
,
1182 union xfs_btree_rec
*rec
,
1188 ASSERT(numrecs
>= 0);
1189 ASSERT(dir
== 1 || dir
== -1);
1191 dst_rec
= (char *)rec
+ (dir
* cur
->bc_ops
->rec_len
);
1192 memmove(dst_rec
, rec
, numrecs
* cur
->bc_ops
->rec_len
);
1196 * Shift block pointers one index left/right inside a single btree block.
1199 xfs_btree_shift_ptrs(
1200 struct xfs_btree_cur
*cur
,
1201 union xfs_btree_ptr
*ptr
,
1207 ASSERT(numptrs
>= 0);
1208 ASSERT(dir
== 1 || dir
== -1);
1210 dst_ptr
= (char *)ptr
+ (dir
* xfs_btree_ptr_len(cur
));
1211 memmove(dst_ptr
, ptr
, numptrs
* xfs_btree_ptr_len(cur
));
1215 * Log key values from the btree block.
1219 struct xfs_btree_cur
*cur
,
1224 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
1225 XFS_BTREE_TRACE_ARGBII(cur
, bp
, first
, last
);
1228 xfs_trans_buf_set_type(cur
->bc_tp
, bp
, XFS_BLFT_BTREE_BUF
);
1229 xfs_trans_log_buf(cur
->bc_tp
, bp
,
1230 xfs_btree_key_offset(cur
, first
),
1231 xfs_btree_key_offset(cur
, last
+ 1) - 1);
1233 xfs_trans_log_inode(cur
->bc_tp
, cur
->bc_private
.b
.ip
,
1234 xfs_ilog_fbroot(cur
->bc_private
.b
.whichfork
));
1237 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
1241 * Log record values from the btree block.
1245 struct xfs_btree_cur
*cur
,
1250 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
1251 XFS_BTREE_TRACE_ARGBII(cur
, bp
, first
, last
);
1253 xfs_trans_buf_set_type(cur
->bc_tp
, bp
, XFS_BLFT_BTREE_BUF
);
1254 xfs_trans_log_buf(cur
->bc_tp
, bp
,
1255 xfs_btree_rec_offset(cur
, first
),
1256 xfs_btree_rec_offset(cur
, last
+ 1) - 1);
1258 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
1262 * Log block pointer fields from a btree block (nonleaf).
1266 struct xfs_btree_cur
*cur
, /* btree cursor */
1267 struct xfs_buf
*bp
, /* buffer containing btree block */
1268 int first
, /* index of first pointer to log */
1269 int last
) /* index of last pointer to log */
1271 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
1272 XFS_BTREE_TRACE_ARGBII(cur
, bp
, first
, last
);
1275 struct xfs_btree_block
*block
= XFS_BUF_TO_BLOCK(bp
);
1276 int level
= xfs_btree_get_level(block
);
1278 xfs_trans_buf_set_type(cur
->bc_tp
, bp
, XFS_BLFT_BTREE_BUF
);
1279 xfs_trans_log_buf(cur
->bc_tp
, bp
,
1280 xfs_btree_ptr_offset(cur
, first
, level
),
1281 xfs_btree_ptr_offset(cur
, last
+ 1, level
) - 1);
1283 xfs_trans_log_inode(cur
->bc_tp
, cur
->bc_private
.b
.ip
,
1284 xfs_ilog_fbroot(cur
->bc_private
.b
.whichfork
));
1287 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
1291 * Log fields from a btree block header.
1294 xfs_btree_log_block(
1295 struct xfs_btree_cur
*cur
, /* btree cursor */
1296 struct xfs_buf
*bp
, /* buffer containing btree block */
1297 int fields
) /* mask of fields: XFS_BB_... */
1299 int first
; /* first byte offset logged */
1300 int last
; /* last byte offset logged */
1301 static const short soffsets
[] = { /* table of offsets (short) */
1302 offsetof(struct xfs_btree_block
, bb_magic
),
1303 offsetof(struct xfs_btree_block
, bb_level
),
1304 offsetof(struct xfs_btree_block
, bb_numrecs
),
1305 offsetof(struct xfs_btree_block
, bb_u
.s
.bb_leftsib
),
1306 offsetof(struct xfs_btree_block
, bb_u
.s
.bb_rightsib
),
1307 offsetof(struct xfs_btree_block
, bb_u
.s
.bb_blkno
),
1308 offsetof(struct xfs_btree_block
, bb_u
.s
.bb_lsn
),
1309 offsetof(struct xfs_btree_block
, bb_u
.s
.bb_uuid
),
1310 offsetof(struct xfs_btree_block
, bb_u
.s
.bb_owner
),
1311 offsetof(struct xfs_btree_block
, bb_u
.s
.bb_crc
),
1312 XFS_BTREE_SBLOCK_CRC_LEN
1314 static const short loffsets
[] = { /* table of offsets (long) */
1315 offsetof(struct xfs_btree_block
, bb_magic
),
1316 offsetof(struct xfs_btree_block
, bb_level
),
1317 offsetof(struct xfs_btree_block
, bb_numrecs
),
1318 offsetof(struct xfs_btree_block
, bb_u
.l
.bb_leftsib
),
1319 offsetof(struct xfs_btree_block
, bb_u
.l
.bb_rightsib
),
1320 offsetof(struct xfs_btree_block
, bb_u
.l
.bb_blkno
),
1321 offsetof(struct xfs_btree_block
, bb_u
.l
.bb_lsn
),
1322 offsetof(struct xfs_btree_block
, bb_u
.l
.bb_uuid
),
1323 offsetof(struct xfs_btree_block
, bb_u
.l
.bb_owner
),
1324 offsetof(struct xfs_btree_block
, bb_u
.l
.bb_crc
),
1325 offsetof(struct xfs_btree_block
, bb_u
.l
.bb_pad
),
1326 XFS_BTREE_LBLOCK_CRC_LEN
1329 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
1330 XFS_BTREE_TRACE_ARGBI(cur
, bp
, fields
);
1335 if (cur
->bc_flags
& XFS_BTREE_CRC_BLOCKS
) {
1337 * We don't log the CRC when updating a btree
1338 * block but instead recreate it during log
1339 * recovery. As the log buffers have checksums
1340 * of their this is safe and avoids logging a crc
1341 * update in a lot of places.
1343 if (fields
== XFS_BB_ALL_BITS
)
1344 fields
= XFS_BB_ALL_BITS_CRC
;
1345 nbits
= XFS_BB_NUM_BITS_CRC
;
1347 nbits
= XFS_BB_NUM_BITS
;
1349 xfs_btree_offsets(fields
,
1350 (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
) ?
1351 loffsets
: soffsets
,
1352 nbits
, &first
, &last
);
1353 xfs_trans_buf_set_type(cur
->bc_tp
, bp
, XFS_BLFT_BTREE_BUF
);
1354 xfs_trans_log_buf(cur
->bc_tp
, bp
, first
, last
);
1356 xfs_trans_log_inode(cur
->bc_tp
, cur
->bc_private
.b
.ip
,
1357 xfs_ilog_fbroot(cur
->bc_private
.b
.whichfork
));
1360 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
1364 * Increment cursor by one record at the level.
1365 * For nonzero levels the leaf-ward information is untouched.
1368 xfs_btree_increment(
1369 struct xfs_btree_cur
*cur
,
1371 int *stat
) /* success/failure */
1373 struct xfs_btree_block
*block
;
1374 union xfs_btree_ptr ptr
;
1376 int error
; /* error return value */
1379 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
1380 XFS_BTREE_TRACE_ARGI(cur
, level
);
1382 ASSERT(level
< cur
->bc_nlevels
);
1384 /* Read-ahead to the right at this level. */
1385 xfs_btree_readahead(cur
, level
, XFS_BTCUR_RIGHTRA
);
1387 /* Get a pointer to the btree block. */
1388 block
= xfs_btree_get_block(cur
, level
, &bp
);
1391 error
= xfs_btree_check_block(cur
, block
, level
, bp
);
1396 /* We're done if we remain in the block after the increment. */
1397 if (++cur
->bc_ptrs
[level
] <= xfs_btree_get_numrecs(block
))
1400 /* Fail if we just went off the right edge of the tree. */
1401 xfs_btree_get_sibling(cur
, block
, &ptr
, XFS_BB_RIGHTSIB
);
1402 if (xfs_btree_ptr_is_null(cur
, &ptr
))
1405 XFS_BTREE_STATS_INC(cur
, increment
);
1408 * March up the tree incrementing pointers.
1409 * Stop when we don't go off the right edge of a block.
1411 for (lev
= level
+ 1; lev
< cur
->bc_nlevels
; lev
++) {
1412 block
= xfs_btree_get_block(cur
, lev
, &bp
);
1415 error
= xfs_btree_check_block(cur
, block
, lev
, bp
);
1420 if (++cur
->bc_ptrs
[lev
] <= xfs_btree_get_numrecs(block
))
1423 /* Read-ahead the right block for the next loop. */
1424 xfs_btree_readahead(cur
, lev
, XFS_BTCUR_RIGHTRA
);
1428 * If we went off the root then we are either seriously
1429 * confused or have the tree root in an inode.
1431 if (lev
== cur
->bc_nlevels
) {
1432 if (cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
)
1435 error
= EFSCORRUPTED
;
1438 ASSERT(lev
< cur
->bc_nlevels
);
1441 * Now walk back down the tree, fixing up the cursor's buffer
1442 * pointers and key numbers.
1444 for (block
= xfs_btree_get_block(cur
, lev
, &bp
); lev
> level
; ) {
1445 union xfs_btree_ptr
*ptrp
;
1447 ptrp
= xfs_btree_ptr_addr(cur
, cur
->bc_ptrs
[lev
], block
);
1448 error
= xfs_btree_read_buf_block(cur
, ptrp
, --lev
,
1453 xfs_btree_setbuf(cur
, lev
, bp
);
1454 cur
->bc_ptrs
[lev
] = 1;
1457 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
1462 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
1467 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
1472 * Decrement cursor by one record at the level.
1473 * For nonzero levels the leaf-ward information is untouched.
1476 xfs_btree_decrement(
1477 struct xfs_btree_cur
*cur
,
1479 int *stat
) /* success/failure */
1481 struct xfs_btree_block
*block
;
1483 int error
; /* error return value */
1485 union xfs_btree_ptr ptr
;
1487 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
1488 XFS_BTREE_TRACE_ARGI(cur
, level
);
1490 ASSERT(level
< cur
->bc_nlevels
);
1492 /* Read-ahead to the left at this level. */
1493 xfs_btree_readahead(cur
, level
, XFS_BTCUR_LEFTRA
);
1495 /* We're done if we remain in the block after the decrement. */
1496 if (--cur
->bc_ptrs
[level
] > 0)
1499 /* Get a pointer to the btree block. */
1500 block
= xfs_btree_get_block(cur
, level
, &bp
);
1503 error
= xfs_btree_check_block(cur
, block
, level
, bp
);
1508 /* Fail if we just went off the left edge of the tree. */
1509 xfs_btree_get_sibling(cur
, block
, &ptr
, XFS_BB_LEFTSIB
);
1510 if (xfs_btree_ptr_is_null(cur
, &ptr
))
1513 XFS_BTREE_STATS_INC(cur
, decrement
);
1516 * March up the tree decrementing pointers.
1517 * Stop when we don't go off the left edge of a block.
1519 for (lev
= level
+ 1; lev
< cur
->bc_nlevels
; lev
++) {
1520 if (--cur
->bc_ptrs
[lev
] > 0)
1522 /* Read-ahead the left block for the next loop. */
1523 xfs_btree_readahead(cur
, lev
, XFS_BTCUR_LEFTRA
);
1527 * If we went off the root then we are seriously confused.
1528 * or the root of the tree is in an inode.
1530 if (lev
== cur
->bc_nlevels
) {
1531 if (cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
)
1534 error
= EFSCORRUPTED
;
1537 ASSERT(lev
< cur
->bc_nlevels
);
1540 * Now walk back down the tree, fixing up the cursor's buffer
1541 * pointers and key numbers.
1543 for (block
= xfs_btree_get_block(cur
, lev
, &bp
); lev
> level
; ) {
1544 union xfs_btree_ptr
*ptrp
;
1546 ptrp
= xfs_btree_ptr_addr(cur
, cur
->bc_ptrs
[lev
], block
);
1547 error
= xfs_btree_read_buf_block(cur
, ptrp
, --lev
,
1551 xfs_btree_setbuf(cur
, lev
, bp
);
1552 cur
->bc_ptrs
[lev
] = xfs_btree_get_numrecs(block
);
1555 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
1560 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
1565 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
1570 xfs_btree_lookup_get_block(
1571 struct xfs_btree_cur
*cur
, /* btree cursor */
1572 int level
, /* level in the btree */
1573 union xfs_btree_ptr
*pp
, /* ptr to btree block */
1574 struct xfs_btree_block
**blkp
) /* return btree block */
1576 struct xfs_buf
*bp
; /* buffer pointer for btree block */
1579 /* special case the root block if in an inode */
1580 if ((cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
) &&
1581 (level
== cur
->bc_nlevels
- 1)) {
1582 *blkp
= xfs_btree_get_iroot(cur
);
1587 * If the old buffer at this level for the disk address we are
1588 * looking for re-use it.
1590 * Otherwise throw it away and get a new one.
1592 bp
= cur
->bc_bufs
[level
];
1593 if (bp
&& XFS_BUF_ADDR(bp
) == xfs_btree_ptr_to_daddr(cur
, pp
)) {
1594 *blkp
= XFS_BUF_TO_BLOCK(bp
);
1598 error
= xfs_btree_read_buf_block(cur
, pp
, level
, 0, blkp
, &bp
);
1602 xfs_btree_setbuf(cur
, level
, bp
);
1607 * Get current search key. For level 0 we don't actually have a key
1608 * structure so we make one up from the record. For all other levels
1609 * we just return the right key.
1611 STATIC
union xfs_btree_key
*
1612 xfs_lookup_get_search_key(
1613 struct xfs_btree_cur
*cur
,
1616 struct xfs_btree_block
*block
,
1617 union xfs_btree_key
*kp
)
1620 cur
->bc_ops
->init_key_from_rec(kp
,
1621 xfs_btree_rec_addr(cur
, keyno
, block
));
1625 return xfs_btree_key_addr(cur
, keyno
, block
);
1629 * Lookup the record. The cursor is made to point to it, based on dir.
1630 * Return 0 if can't find any such record, 1 for success.
1634 struct xfs_btree_cur
*cur
, /* btree cursor */
1635 xfs_lookup_t dir
, /* <=, ==, or >= */
1636 int *stat
) /* success/failure */
1638 struct xfs_btree_block
*block
; /* current btree block */
1639 __int64_t diff
; /* difference for the current key */
1640 int error
; /* error return value */
1641 int keyno
; /* current key number */
1642 int level
; /* level in the btree */
1643 union xfs_btree_ptr
*pp
; /* ptr to btree block */
1644 union xfs_btree_ptr ptr
; /* ptr to btree block */
1646 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
1647 XFS_BTREE_TRACE_ARGI(cur
, dir
);
1649 XFS_BTREE_STATS_INC(cur
, lookup
);
1654 /* initialise start pointer from cursor */
1655 cur
->bc_ops
->init_ptr_from_cur(cur
, &ptr
);
1659 * Iterate over each level in the btree, starting at the root.
1660 * For each level above the leaves, find the key we need, based
1661 * on the lookup record, then follow the corresponding block
1662 * pointer down to the next level.
1664 for (level
= cur
->bc_nlevels
- 1, diff
= 1; level
>= 0; level
--) {
1665 /* Get the block we need to do the lookup on. */
1666 error
= xfs_btree_lookup_get_block(cur
, level
, pp
, &block
);
1672 * If we already had a key match at a higher level, we
1673 * know we need to use the first entry in this block.
1677 /* Otherwise search this block. Do a binary search. */
1679 int high
; /* high entry number */
1680 int low
; /* low entry number */
1682 /* Set low and high entry numbers, 1-based. */
1684 high
= xfs_btree_get_numrecs(block
);
1686 /* Block is empty, must be an empty leaf. */
1687 ASSERT(level
== 0 && cur
->bc_nlevels
== 1);
1689 cur
->bc_ptrs
[0] = dir
!= XFS_LOOKUP_LE
;
1690 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
1695 /* Binary search the block. */
1696 while (low
<= high
) {
1697 union xfs_btree_key key
;
1698 union xfs_btree_key
*kp
;
1700 XFS_BTREE_STATS_INC(cur
, compare
);
1702 /* keyno is average of low and high. */
1703 keyno
= (low
+ high
) >> 1;
1705 /* Get current search key */
1706 kp
= xfs_lookup_get_search_key(cur
, level
,
1707 keyno
, block
, &key
);
1710 * Compute difference to get next direction:
1711 * - less than, move right
1712 * - greater than, move left
1713 * - equal, we're done
1715 diff
= cur
->bc_ops
->key_diff(cur
, kp
);
1726 * If there are more levels, set up for the next level
1727 * by getting the block number and filling in the cursor.
1731 * If we moved left, need the previous key number,
1732 * unless there isn't one.
1734 if (diff
> 0 && --keyno
< 1)
1736 pp
= xfs_btree_ptr_addr(cur
, keyno
, block
);
1739 error
= xfs_btree_check_ptr(cur
, pp
, 0, level
);
1743 cur
->bc_ptrs
[level
] = keyno
;
1747 /* Done with the search. See if we need to adjust the results. */
1748 if (dir
!= XFS_LOOKUP_LE
&& diff
< 0) {
1751 * If ge search and we went off the end of the block, but it's
1752 * not the last block, we're in the wrong block.
1754 xfs_btree_get_sibling(cur
, block
, &ptr
, XFS_BB_RIGHTSIB
);
1755 if (dir
== XFS_LOOKUP_GE
&&
1756 keyno
> xfs_btree_get_numrecs(block
) &&
1757 !xfs_btree_ptr_is_null(cur
, &ptr
)) {
1760 cur
->bc_ptrs
[0] = keyno
;
1761 error
= xfs_btree_increment(cur
, 0, &i
);
1764 XFS_WANT_CORRUPTED_RETURN(i
== 1);
1765 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
1769 } else if (dir
== XFS_LOOKUP_LE
&& diff
> 0)
1771 cur
->bc_ptrs
[0] = keyno
;
1773 /* Return if we succeeded or not. */
1774 if (keyno
== 0 || keyno
> xfs_btree_get_numrecs(block
))
1776 else if (dir
!= XFS_LOOKUP_EQ
|| diff
== 0)
1780 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
1784 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
1789 * Update keys at all levels from here to the root along the cursor's path.
1793 struct xfs_btree_cur
*cur
,
1794 union xfs_btree_key
*keyp
,
1797 struct xfs_btree_block
*block
;
1799 union xfs_btree_key
*kp
;
1802 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
1803 XFS_BTREE_TRACE_ARGIK(cur
, level
, keyp
);
1805 ASSERT(!(cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
) || level
>= 1);
1808 * Go up the tree from this level toward the root.
1809 * At each level, update the key value to the value input.
1810 * Stop when we reach a level where the cursor isn't pointing
1811 * at the first entry in the block.
1813 for (ptr
= 1; ptr
== 1 && level
< cur
->bc_nlevels
; level
++) {
1817 block
= xfs_btree_get_block(cur
, level
, &bp
);
1819 error
= xfs_btree_check_block(cur
, block
, level
, bp
);
1821 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
1825 ptr
= cur
->bc_ptrs
[level
];
1826 kp
= xfs_btree_key_addr(cur
, ptr
, block
);
1827 xfs_btree_copy_keys(cur
, kp
, keyp
, 1);
1828 xfs_btree_log_keys(cur
, bp
, ptr
, ptr
);
1831 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
1836 * Update the record referred to by cur to the value in the
1837 * given record. This either works (return 0) or gets an
1838 * EFSCORRUPTED error.
1842 struct xfs_btree_cur
*cur
,
1843 union xfs_btree_rec
*rec
)
1845 struct xfs_btree_block
*block
;
1849 union xfs_btree_rec
*rp
;
1851 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
1852 XFS_BTREE_TRACE_ARGR(cur
, rec
);
1854 /* Pick up the current block. */
1855 block
= xfs_btree_get_block(cur
, 0, &bp
);
1858 error
= xfs_btree_check_block(cur
, block
, 0, bp
);
1862 /* Get the address of the rec to be updated. */
1863 ptr
= cur
->bc_ptrs
[0];
1864 rp
= xfs_btree_rec_addr(cur
, ptr
, block
);
1866 /* Fill in the new contents and log them. */
1867 xfs_btree_copy_recs(cur
, rp
, rec
, 1);
1868 xfs_btree_log_recs(cur
, bp
, ptr
, ptr
);
1871 * If we are tracking the last record in the tree and
1872 * we are at the far right edge of the tree, update it.
1874 if (xfs_btree_is_lastrec(cur
, block
, 0)) {
1875 cur
->bc_ops
->update_lastrec(cur
, block
, rec
,
1876 ptr
, LASTREC_UPDATE
);
1879 /* Updating first rec in leaf. Pass new key value up to our parent. */
1881 union xfs_btree_key key
;
1883 cur
->bc_ops
->init_key_from_rec(&key
, rec
);
1884 error
= xfs_btree_updkey(cur
, &key
, 1);
1889 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
1893 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
1898 * Move 1 record left from cur/level if possible.
1899 * Update cur to reflect the new path.
1901 STATIC
int /* error */
1903 struct xfs_btree_cur
*cur
,
1905 int *stat
) /* success/failure */
1907 union xfs_btree_key key
; /* btree key */
1908 struct xfs_buf
*lbp
; /* left buffer pointer */
1909 struct xfs_btree_block
*left
; /* left btree block */
1910 int lrecs
; /* left record count */
1911 struct xfs_buf
*rbp
; /* right buffer pointer */
1912 struct xfs_btree_block
*right
; /* right btree block */
1913 int rrecs
; /* right record count */
1914 union xfs_btree_ptr lptr
; /* left btree pointer */
1915 union xfs_btree_key
*rkp
= NULL
; /* right btree key */
1916 union xfs_btree_ptr
*rpp
= NULL
; /* right address pointer */
1917 union xfs_btree_rec
*rrp
= NULL
; /* right record pointer */
1918 int error
; /* error return value */
1920 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
1921 XFS_BTREE_TRACE_ARGI(cur
, level
);
1923 if ((cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
) &&
1924 level
== cur
->bc_nlevels
- 1)
1927 /* Set up variables for this block as "right". */
1928 right
= xfs_btree_get_block(cur
, level
, &rbp
);
1931 error
= xfs_btree_check_block(cur
, right
, level
, rbp
);
1936 /* If we've got no left sibling then we can't shift an entry left. */
1937 xfs_btree_get_sibling(cur
, right
, &lptr
, XFS_BB_LEFTSIB
);
1938 if (xfs_btree_ptr_is_null(cur
, &lptr
))
1942 * If the cursor entry is the one that would be moved, don't
1943 * do it... it's too complicated.
1945 if (cur
->bc_ptrs
[level
] <= 1)
1948 /* Set up the left neighbor as "left". */
1949 error
= xfs_btree_read_buf_block(cur
, &lptr
, level
, 0, &left
, &lbp
);
1953 /* If it's full, it can't take another entry. */
1954 lrecs
= xfs_btree_get_numrecs(left
);
1955 if (lrecs
== cur
->bc_ops
->get_maxrecs(cur
, level
))
1958 rrecs
= xfs_btree_get_numrecs(right
);
1961 * We add one entry to the left side and remove one for the right side.
1962 * Account for it here, the changes will be updated on disk and logged
1968 XFS_BTREE_STATS_INC(cur
, lshift
);
1969 XFS_BTREE_STATS_ADD(cur
, moves
, 1);
1972 * If non-leaf, copy a key and a ptr to the left block.
1973 * Log the changes to the left block.
1976 /* It's a non-leaf. Move keys and pointers. */
1977 union xfs_btree_key
*lkp
; /* left btree key */
1978 union xfs_btree_ptr
*lpp
; /* left address pointer */
1980 lkp
= xfs_btree_key_addr(cur
, lrecs
, left
);
1981 rkp
= xfs_btree_key_addr(cur
, 1, right
);
1983 lpp
= xfs_btree_ptr_addr(cur
, lrecs
, left
);
1984 rpp
= xfs_btree_ptr_addr(cur
, 1, right
);
1986 error
= xfs_btree_check_ptr(cur
, rpp
, 0, level
);
1990 xfs_btree_copy_keys(cur
, lkp
, rkp
, 1);
1991 xfs_btree_copy_ptrs(cur
, lpp
, rpp
, 1);
1993 xfs_btree_log_keys(cur
, lbp
, lrecs
, lrecs
);
1994 xfs_btree_log_ptrs(cur
, lbp
, lrecs
, lrecs
);
1996 ASSERT(cur
->bc_ops
->keys_inorder(cur
,
1997 xfs_btree_key_addr(cur
, lrecs
- 1, left
), lkp
));
1999 /* It's a leaf. Move records. */
2000 union xfs_btree_rec
*lrp
; /* left record pointer */
2002 lrp
= xfs_btree_rec_addr(cur
, lrecs
, left
);
2003 rrp
= xfs_btree_rec_addr(cur
, 1, right
);
2005 xfs_btree_copy_recs(cur
, lrp
, rrp
, 1);
2006 xfs_btree_log_recs(cur
, lbp
, lrecs
, lrecs
);
2008 ASSERT(cur
->bc_ops
->recs_inorder(cur
,
2009 xfs_btree_rec_addr(cur
, lrecs
- 1, left
), lrp
));
2012 xfs_btree_set_numrecs(left
, lrecs
);
2013 xfs_btree_log_block(cur
, lbp
, XFS_BB_NUMRECS
);
2015 xfs_btree_set_numrecs(right
, rrecs
);
2016 xfs_btree_log_block(cur
, rbp
, XFS_BB_NUMRECS
);
2019 * Slide the contents of right down one entry.
2021 XFS_BTREE_STATS_ADD(cur
, moves
, rrecs
- 1);
2023 /* It's a nonleaf. operate on keys and ptrs */
2025 int i
; /* loop index */
2027 for (i
= 0; i
< rrecs
; i
++) {
2028 error
= xfs_btree_check_ptr(cur
, rpp
, i
+ 1, level
);
2033 xfs_btree_shift_keys(cur
,
2034 xfs_btree_key_addr(cur
, 2, right
),
2036 xfs_btree_shift_ptrs(cur
,
2037 xfs_btree_ptr_addr(cur
, 2, right
),
2040 xfs_btree_log_keys(cur
, rbp
, 1, rrecs
);
2041 xfs_btree_log_ptrs(cur
, rbp
, 1, rrecs
);
2043 /* It's a leaf. operate on records */
2044 xfs_btree_shift_recs(cur
,
2045 xfs_btree_rec_addr(cur
, 2, right
),
2047 xfs_btree_log_recs(cur
, rbp
, 1, rrecs
);
2050 * If it's the first record in the block, we'll need a key
2051 * structure to pass up to the next level (updkey).
2053 cur
->bc_ops
->init_key_from_rec(&key
,
2054 xfs_btree_rec_addr(cur
, 1, right
));
2058 /* Update the parent key values of right. */
2059 error
= xfs_btree_updkey(cur
, rkp
, level
+ 1);
2063 /* Slide the cursor value left one. */
2064 cur
->bc_ptrs
[level
]--;
2066 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
2071 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
2076 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
2081 * Move 1 record right from cur/level if possible.
2082 * Update cur to reflect the new path.
2084 STATIC
int /* error */
2086 struct xfs_btree_cur
*cur
,
2088 int *stat
) /* success/failure */
2090 union xfs_btree_key key
; /* btree key */
2091 struct xfs_buf
*lbp
; /* left buffer pointer */
2092 struct xfs_btree_block
*left
; /* left btree block */
2093 struct xfs_buf
*rbp
; /* right buffer pointer */
2094 struct xfs_btree_block
*right
; /* right btree block */
2095 struct xfs_btree_cur
*tcur
; /* temporary btree cursor */
2096 union xfs_btree_ptr rptr
; /* right block pointer */
2097 union xfs_btree_key
*rkp
; /* right btree key */
2098 int rrecs
; /* right record count */
2099 int lrecs
; /* left record count */
2100 int error
; /* error return value */
2101 int i
; /* loop counter */
2103 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
2104 XFS_BTREE_TRACE_ARGI(cur
, level
);
2106 if ((cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
) &&
2107 (level
== cur
->bc_nlevels
- 1))
2110 /* Set up variables for this block as "left". */
2111 left
= xfs_btree_get_block(cur
, level
, &lbp
);
2114 error
= xfs_btree_check_block(cur
, left
, level
, lbp
);
2119 /* If we've got no right sibling then we can't shift an entry right. */
2120 xfs_btree_get_sibling(cur
, left
, &rptr
, XFS_BB_RIGHTSIB
);
2121 if (xfs_btree_ptr_is_null(cur
, &rptr
))
2125 * If the cursor entry is the one that would be moved, don't
2126 * do it... it's too complicated.
2128 lrecs
= xfs_btree_get_numrecs(left
);
2129 if (cur
->bc_ptrs
[level
] >= lrecs
)
2132 /* Set up the right neighbor as "right". */
2133 error
= xfs_btree_read_buf_block(cur
, &rptr
, level
, 0, &right
, &rbp
);
2137 /* If it's full, it can't take another entry. */
2138 rrecs
= xfs_btree_get_numrecs(right
);
2139 if (rrecs
== cur
->bc_ops
->get_maxrecs(cur
, level
))
2142 XFS_BTREE_STATS_INC(cur
, rshift
);
2143 XFS_BTREE_STATS_ADD(cur
, moves
, rrecs
);
2146 * Make a hole at the start of the right neighbor block, then
2147 * copy the last left block entry to the hole.
2150 /* It's a nonleaf. make a hole in the keys and ptrs */
2151 union xfs_btree_key
*lkp
;
2152 union xfs_btree_ptr
*lpp
;
2153 union xfs_btree_ptr
*rpp
;
2155 lkp
= xfs_btree_key_addr(cur
, lrecs
, left
);
2156 lpp
= xfs_btree_ptr_addr(cur
, lrecs
, left
);
2157 rkp
= xfs_btree_key_addr(cur
, 1, right
);
2158 rpp
= xfs_btree_ptr_addr(cur
, 1, right
);
2161 for (i
= rrecs
- 1; i
>= 0; i
--) {
2162 error
= xfs_btree_check_ptr(cur
, rpp
, i
, level
);
2168 xfs_btree_shift_keys(cur
, rkp
, 1, rrecs
);
2169 xfs_btree_shift_ptrs(cur
, rpp
, 1, rrecs
);
2172 error
= xfs_btree_check_ptr(cur
, lpp
, 0, level
);
2177 /* Now put the new data in, and log it. */
2178 xfs_btree_copy_keys(cur
, rkp
, lkp
, 1);
2179 xfs_btree_copy_ptrs(cur
, rpp
, lpp
, 1);
2181 xfs_btree_log_keys(cur
, rbp
, 1, rrecs
+ 1);
2182 xfs_btree_log_ptrs(cur
, rbp
, 1, rrecs
+ 1);
2184 ASSERT(cur
->bc_ops
->keys_inorder(cur
, rkp
,
2185 xfs_btree_key_addr(cur
, 2, right
)));
2187 /* It's a leaf. make a hole in the records */
2188 union xfs_btree_rec
*lrp
;
2189 union xfs_btree_rec
*rrp
;
2191 lrp
= xfs_btree_rec_addr(cur
, lrecs
, left
);
2192 rrp
= xfs_btree_rec_addr(cur
, 1, right
);
2194 xfs_btree_shift_recs(cur
, rrp
, 1, rrecs
);
2196 /* Now put the new data in, and log it. */
2197 xfs_btree_copy_recs(cur
, rrp
, lrp
, 1);
2198 xfs_btree_log_recs(cur
, rbp
, 1, rrecs
+ 1);
2200 cur
->bc_ops
->init_key_from_rec(&key
, rrp
);
2203 ASSERT(cur
->bc_ops
->recs_inorder(cur
, rrp
,
2204 xfs_btree_rec_addr(cur
, 2, right
)));
2208 * Decrement and log left's numrecs, bump and log right's numrecs.
2210 xfs_btree_set_numrecs(left
, --lrecs
);
2211 xfs_btree_log_block(cur
, lbp
, XFS_BB_NUMRECS
);
2213 xfs_btree_set_numrecs(right
, ++rrecs
);
2214 xfs_btree_log_block(cur
, rbp
, XFS_BB_NUMRECS
);
2217 * Using a temporary cursor, update the parent key values of the
2218 * block on the right.
2220 error
= xfs_btree_dup_cursor(cur
, &tcur
);
2223 i
= xfs_btree_lastrec(tcur
, level
);
2224 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
2226 error
= xfs_btree_increment(tcur
, level
, &i
);
2230 error
= xfs_btree_updkey(tcur
, rkp
, level
+ 1);
2234 xfs_btree_del_cursor(tcur
, XFS_BTREE_NOERROR
);
2236 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
2241 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
2246 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
2250 XFS_BTREE_TRACE_CURSOR(tcur
, XBT_ERROR
);
2251 xfs_btree_del_cursor(tcur
, XFS_BTREE_ERROR
);
2256 * Split cur/level block in half.
2257 * Return new block number and the key to its first
2258 * record (to be inserted into parent).
2260 STATIC
int /* error */
2262 struct xfs_btree_cur
*cur
,
2264 union xfs_btree_ptr
*ptrp
,
2265 union xfs_btree_key
*key
,
2266 struct xfs_btree_cur
**curp
,
2267 int *stat
) /* success/failure */
2269 union xfs_btree_ptr lptr
; /* left sibling block ptr */
2270 struct xfs_buf
*lbp
; /* left buffer pointer */
2271 struct xfs_btree_block
*left
; /* left btree block */
2272 union xfs_btree_ptr rptr
; /* right sibling block ptr */
2273 struct xfs_buf
*rbp
; /* right buffer pointer */
2274 struct xfs_btree_block
*right
; /* right btree block */
2275 union xfs_btree_ptr rrptr
; /* right-right sibling ptr */
2276 struct xfs_buf
*rrbp
; /* right-right buffer pointer */
2277 struct xfs_btree_block
*rrblock
; /* right-right btree block */
2281 int error
; /* error return value */
2286 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
2287 XFS_BTREE_TRACE_ARGIPK(cur
, level
, *ptrp
, key
);
2289 XFS_BTREE_STATS_INC(cur
, split
);
2291 /* Set up left block (current one). */
2292 left
= xfs_btree_get_block(cur
, level
, &lbp
);
2295 error
= xfs_btree_check_block(cur
, left
, level
, lbp
);
2300 xfs_btree_buf_to_ptr(cur
, lbp
, &lptr
);
2302 /* Allocate the new block. If we can't do it, we're toast. Give up. */
2303 error
= cur
->bc_ops
->alloc_block(cur
, &lptr
, &rptr
, 1, stat
);
2308 XFS_BTREE_STATS_INC(cur
, alloc
);
2310 /* Set up the new block as "right". */
2311 error
= xfs_btree_get_buf_block(cur
, &rptr
, 0, &right
, &rbp
);
2315 /* Fill in the btree header for the new right block. */
2316 xfs_btree_init_block_cur(cur
, rbp
, xfs_btree_get_level(left
), 0);
2319 * Split the entries between the old and the new block evenly.
2320 * Make sure that if there's an odd number of entries now, that
2321 * each new block will have the same number of entries.
2323 lrecs
= xfs_btree_get_numrecs(left
);
2325 if ((lrecs
& 1) && cur
->bc_ptrs
[level
] <= rrecs
+ 1)
2327 src_index
= (lrecs
- rrecs
+ 1);
2329 XFS_BTREE_STATS_ADD(cur
, moves
, rrecs
);
2332 * Copy btree block entries from the left block over to the
2333 * new block, the right. Update the right block and log the
2337 /* It's a non-leaf. Move keys and pointers. */
2338 union xfs_btree_key
*lkp
; /* left btree key */
2339 union xfs_btree_ptr
*lpp
; /* left address pointer */
2340 union xfs_btree_key
*rkp
; /* right btree key */
2341 union xfs_btree_ptr
*rpp
; /* right address pointer */
2343 lkp
= xfs_btree_key_addr(cur
, src_index
, left
);
2344 lpp
= xfs_btree_ptr_addr(cur
, src_index
, left
);
2345 rkp
= xfs_btree_key_addr(cur
, 1, right
);
2346 rpp
= xfs_btree_ptr_addr(cur
, 1, right
);
2349 for (i
= src_index
; i
< rrecs
; i
++) {
2350 error
= xfs_btree_check_ptr(cur
, lpp
, i
, level
);
2356 xfs_btree_copy_keys(cur
, rkp
, lkp
, rrecs
);
2357 xfs_btree_copy_ptrs(cur
, rpp
, lpp
, rrecs
);
2359 xfs_btree_log_keys(cur
, rbp
, 1, rrecs
);
2360 xfs_btree_log_ptrs(cur
, rbp
, 1, rrecs
);
2362 /* Grab the keys to the entries moved to the right block */
2363 xfs_btree_copy_keys(cur
, key
, rkp
, 1);
2365 /* It's a leaf. Move records. */
2366 union xfs_btree_rec
*lrp
; /* left record pointer */
2367 union xfs_btree_rec
*rrp
; /* right record pointer */
2369 lrp
= xfs_btree_rec_addr(cur
, src_index
, left
);
2370 rrp
= xfs_btree_rec_addr(cur
, 1, right
);
2372 xfs_btree_copy_recs(cur
, rrp
, lrp
, rrecs
);
2373 xfs_btree_log_recs(cur
, rbp
, 1, rrecs
);
2375 cur
->bc_ops
->init_key_from_rec(key
,
2376 xfs_btree_rec_addr(cur
, 1, right
));
2381 * Find the left block number by looking in the buffer.
2382 * Adjust numrecs, sibling pointers.
2384 xfs_btree_get_sibling(cur
, left
, &rrptr
, XFS_BB_RIGHTSIB
);
2385 xfs_btree_set_sibling(cur
, right
, &rrptr
, XFS_BB_RIGHTSIB
);
2386 xfs_btree_set_sibling(cur
, right
, &lptr
, XFS_BB_LEFTSIB
);
2387 xfs_btree_set_sibling(cur
, left
, &rptr
, XFS_BB_RIGHTSIB
);
2390 xfs_btree_set_numrecs(left
, lrecs
);
2391 xfs_btree_set_numrecs(right
, xfs_btree_get_numrecs(right
) + rrecs
);
2393 xfs_btree_log_block(cur
, rbp
, XFS_BB_ALL_BITS
);
2394 xfs_btree_log_block(cur
, lbp
, XFS_BB_NUMRECS
| XFS_BB_RIGHTSIB
);
2397 * If there's a block to the new block's right, make that block
2398 * point back to right instead of to left.
2400 if (!xfs_btree_ptr_is_null(cur
, &rrptr
)) {
2401 error
= xfs_btree_read_buf_block(cur
, &rrptr
, level
,
2402 0, &rrblock
, &rrbp
);
2405 xfs_btree_set_sibling(cur
, rrblock
, &rptr
, XFS_BB_LEFTSIB
);
2406 xfs_btree_log_block(cur
, rrbp
, XFS_BB_LEFTSIB
);
2409 * If the cursor is really in the right block, move it there.
2410 * If it's just pointing past the last entry in left, then we'll
2411 * insert there, so don't change anything in that case.
2413 if (cur
->bc_ptrs
[level
] > lrecs
+ 1) {
2414 xfs_btree_setbuf(cur
, level
, rbp
);
2415 cur
->bc_ptrs
[level
] -= lrecs
;
2418 * If there are more levels, we'll need another cursor which refers
2419 * the right block, no matter where this cursor was.
2421 if (level
+ 1 < cur
->bc_nlevels
) {
2422 error
= xfs_btree_dup_cursor(cur
, curp
);
2425 (*curp
)->bc_ptrs
[level
+ 1]++;
2428 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
2432 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
2437 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
2442 * Copy the old inode root contents into a real block and make the
2443 * broot point to it.
2446 xfs_btree_new_iroot(
2447 struct xfs_btree_cur
*cur
, /* btree cursor */
2448 int *logflags
, /* logging flags for inode */
2449 int *stat
) /* return status - 0 fail */
2451 struct xfs_buf
*cbp
; /* buffer for cblock */
2452 struct xfs_btree_block
*block
; /* btree block */
2453 struct xfs_btree_block
*cblock
; /* child btree block */
2454 union xfs_btree_key
*ckp
; /* child key pointer */
2455 union xfs_btree_ptr
*cpp
; /* child ptr pointer */
2456 union xfs_btree_key
*kp
; /* pointer to btree key */
2457 union xfs_btree_ptr
*pp
; /* pointer to block addr */
2458 union xfs_btree_ptr nptr
; /* new block addr */
2459 int level
; /* btree level */
2460 int error
; /* error return code */
2462 int i
; /* loop counter */
2465 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
2466 XFS_BTREE_STATS_INC(cur
, newroot
);
2468 ASSERT(cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
);
2470 level
= cur
->bc_nlevels
- 1;
2472 block
= xfs_btree_get_iroot(cur
);
2473 pp
= xfs_btree_ptr_addr(cur
, 1, block
);
2475 /* Allocate the new block. If we can't do it, we're toast. Give up. */
2476 error
= cur
->bc_ops
->alloc_block(cur
, pp
, &nptr
, 1, stat
);
2480 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
2483 XFS_BTREE_STATS_INC(cur
, alloc
);
2485 /* Copy the root into a real block. */
2486 error
= xfs_btree_get_buf_block(cur
, &nptr
, 0, &cblock
, &cbp
);
2491 * we can't just memcpy() the root in for CRC enabled btree blocks.
2492 * In that case have to also ensure the blkno remains correct
2494 memcpy(cblock
, block
, xfs_btree_block_len(cur
));
2495 if (cur
->bc_flags
& XFS_BTREE_CRC_BLOCKS
) {
2496 if (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
)
2497 cblock
->bb_u
.l
.bb_blkno
= cpu_to_be64(cbp
->b_bn
);
2499 cblock
->bb_u
.s
.bb_blkno
= cpu_to_be64(cbp
->b_bn
);
2502 be16_add_cpu(&block
->bb_level
, 1);
2503 xfs_btree_set_numrecs(block
, 1);
2505 cur
->bc_ptrs
[level
+ 1] = 1;
2507 kp
= xfs_btree_key_addr(cur
, 1, block
);
2508 ckp
= xfs_btree_key_addr(cur
, 1, cblock
);
2509 xfs_btree_copy_keys(cur
, ckp
, kp
, xfs_btree_get_numrecs(cblock
));
2511 cpp
= xfs_btree_ptr_addr(cur
, 1, cblock
);
2513 for (i
= 0; i
< be16_to_cpu(cblock
->bb_numrecs
); i
++) {
2514 error
= xfs_btree_check_ptr(cur
, pp
, i
, level
);
2519 xfs_btree_copy_ptrs(cur
, cpp
, pp
, xfs_btree_get_numrecs(cblock
));
2522 error
= xfs_btree_check_ptr(cur
, &nptr
, 0, level
);
2526 xfs_btree_copy_ptrs(cur
, pp
, &nptr
, 1);
2528 xfs_iroot_realloc(cur
->bc_private
.b
.ip
,
2529 1 - xfs_btree_get_numrecs(cblock
),
2530 cur
->bc_private
.b
.whichfork
);
2532 xfs_btree_setbuf(cur
, level
, cbp
);
2535 * Do all this logging at the end so that
2536 * the root is at the right level.
2538 xfs_btree_log_block(cur
, cbp
, XFS_BB_ALL_BITS
);
2539 xfs_btree_log_keys(cur
, cbp
, 1, be16_to_cpu(cblock
->bb_numrecs
));
2540 xfs_btree_log_ptrs(cur
, cbp
, 1, be16_to_cpu(cblock
->bb_numrecs
));
2543 XFS_ILOG_CORE
| xfs_ilog_fbroot(cur
->bc_private
.b
.whichfork
);
2545 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
2548 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
2553 * Allocate a new root block, fill it in.
2555 STATIC
int /* error */
2557 struct xfs_btree_cur
*cur
, /* btree cursor */
2558 int *stat
) /* success/failure */
2560 struct xfs_btree_block
*block
; /* one half of the old root block */
2561 struct xfs_buf
*bp
; /* buffer containing block */
2562 int error
; /* error return value */
2563 struct xfs_buf
*lbp
; /* left buffer pointer */
2564 struct xfs_btree_block
*left
; /* left btree block */
2565 struct xfs_buf
*nbp
; /* new (root) buffer */
2566 struct xfs_btree_block
*new; /* new (root) btree block */
2567 int nptr
; /* new value for key index, 1 or 2 */
2568 struct xfs_buf
*rbp
; /* right buffer pointer */
2569 struct xfs_btree_block
*right
; /* right btree block */
2570 union xfs_btree_ptr rptr
;
2571 union xfs_btree_ptr lptr
;
2573 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
2574 XFS_BTREE_STATS_INC(cur
, newroot
);
2576 /* initialise our start point from the cursor */
2577 cur
->bc_ops
->init_ptr_from_cur(cur
, &rptr
);
2579 /* Allocate the new block. If we can't do it, we're toast. Give up. */
2580 error
= cur
->bc_ops
->alloc_block(cur
, &rptr
, &lptr
, 1, stat
);
2585 XFS_BTREE_STATS_INC(cur
, alloc
);
2587 /* Set up the new block. */
2588 error
= xfs_btree_get_buf_block(cur
, &lptr
, 0, &new, &nbp
);
2592 /* Set the root in the holding structure increasing the level by 1. */
2593 cur
->bc_ops
->set_root(cur
, &lptr
, 1);
2596 * At the previous root level there are now two blocks: the old root,
2597 * and the new block generated when it was split. We don't know which
2598 * one the cursor is pointing at, so we set up variables "left" and
2599 * "right" for each case.
2601 block
= xfs_btree_get_block(cur
, cur
->bc_nlevels
- 1, &bp
);
2604 error
= xfs_btree_check_block(cur
, block
, cur
->bc_nlevels
- 1, bp
);
2609 xfs_btree_get_sibling(cur
, block
, &rptr
, XFS_BB_RIGHTSIB
);
2610 if (!xfs_btree_ptr_is_null(cur
, &rptr
)) {
2611 /* Our block is left, pick up the right block. */
2613 xfs_btree_buf_to_ptr(cur
, lbp
, &lptr
);
2615 error
= xfs_btree_read_buf_block(cur
, &rptr
,
2616 cur
->bc_nlevels
- 1, 0, &right
, &rbp
);
2622 /* Our block is right, pick up the left block. */
2624 xfs_btree_buf_to_ptr(cur
, rbp
, &rptr
);
2626 xfs_btree_get_sibling(cur
, right
, &lptr
, XFS_BB_LEFTSIB
);
2627 error
= xfs_btree_read_buf_block(cur
, &lptr
,
2628 cur
->bc_nlevels
- 1, 0, &left
, &lbp
);
2634 /* Fill in the new block's btree header and log it. */
2635 xfs_btree_init_block_cur(cur
, nbp
, cur
->bc_nlevels
, 2);
2636 xfs_btree_log_block(cur
, nbp
, XFS_BB_ALL_BITS
);
2637 ASSERT(!xfs_btree_ptr_is_null(cur
, &lptr
) &&
2638 !xfs_btree_ptr_is_null(cur
, &rptr
));
2640 /* Fill in the key data in the new root. */
2641 if (xfs_btree_get_level(left
) > 0) {
2642 xfs_btree_copy_keys(cur
,
2643 xfs_btree_key_addr(cur
, 1, new),
2644 xfs_btree_key_addr(cur
, 1, left
), 1);
2645 xfs_btree_copy_keys(cur
,
2646 xfs_btree_key_addr(cur
, 2, new),
2647 xfs_btree_key_addr(cur
, 1, right
), 1);
2649 cur
->bc_ops
->init_key_from_rec(
2650 xfs_btree_key_addr(cur
, 1, new),
2651 xfs_btree_rec_addr(cur
, 1, left
));
2652 cur
->bc_ops
->init_key_from_rec(
2653 xfs_btree_key_addr(cur
, 2, new),
2654 xfs_btree_rec_addr(cur
, 1, right
));
2656 xfs_btree_log_keys(cur
, nbp
, 1, 2);
2658 /* Fill in the pointer data in the new root. */
2659 xfs_btree_copy_ptrs(cur
,
2660 xfs_btree_ptr_addr(cur
, 1, new), &lptr
, 1);
2661 xfs_btree_copy_ptrs(cur
,
2662 xfs_btree_ptr_addr(cur
, 2, new), &rptr
, 1);
2663 xfs_btree_log_ptrs(cur
, nbp
, 1, 2);
2665 /* Fix up the cursor. */
2666 xfs_btree_setbuf(cur
, cur
->bc_nlevels
, nbp
);
2667 cur
->bc_ptrs
[cur
->bc_nlevels
] = nptr
;
2669 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
2673 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
2676 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
2682 xfs_btree_make_block_unfull(
2683 struct xfs_btree_cur
*cur
, /* btree cursor */
2684 int level
, /* btree level */
2685 int numrecs
,/* # of recs in block */
2686 int *oindex
,/* old tree index */
2687 int *index
, /* new tree index */
2688 union xfs_btree_ptr
*nptr
, /* new btree ptr */
2689 struct xfs_btree_cur
**ncur
, /* new btree cursor */
2690 union xfs_btree_rec
*nrec
, /* new record */
2693 union xfs_btree_key key
; /* new btree key value */
2696 if ((cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
) &&
2697 level
== cur
->bc_nlevels
- 1) {
2698 struct xfs_inode
*ip
= cur
->bc_private
.b
.ip
;
2700 if (numrecs
< cur
->bc_ops
->get_dmaxrecs(cur
, level
)) {
2701 /* A root block that can be made bigger. */
2703 xfs_iroot_realloc(ip
, 1, cur
->bc_private
.b
.whichfork
);
2705 /* A root block that needs replacing */
2708 error
= xfs_btree_new_iroot(cur
, &logflags
, stat
);
2709 if (error
|| *stat
== 0)
2712 xfs_trans_log_inode(cur
->bc_tp
, ip
, logflags
);
2718 /* First, try shifting an entry to the right neighbor. */
2719 error
= xfs_btree_rshift(cur
, level
, stat
);
2723 /* Next, try shifting an entry to the left neighbor. */
2724 error
= xfs_btree_lshift(cur
, level
, stat
);
2729 *oindex
= *index
= cur
->bc_ptrs
[level
];
2734 * Next, try splitting the current block in half.
2736 * If this works we have to re-set our variables because we
2737 * could be in a different block now.
2739 error
= xfs_btree_split(cur
, level
, nptr
, &key
, ncur
, stat
);
2740 if (error
|| *stat
== 0)
2744 *index
= cur
->bc_ptrs
[level
];
2745 cur
->bc_ops
->init_rec_from_key(&key
, nrec
);
2750 * Insert one record/level. Return information to the caller
2751 * allowing the next level up to proceed if necessary.
2755 struct xfs_btree_cur
*cur
, /* btree cursor */
2756 int level
, /* level to insert record at */
2757 union xfs_btree_ptr
*ptrp
, /* i/o: block number inserted */
2758 union xfs_btree_rec
*recp
, /* i/o: record data inserted */
2759 struct xfs_btree_cur
**curp
, /* output: new cursor replacing cur */
2760 int *stat
) /* success/failure */
2762 struct xfs_btree_block
*block
; /* btree block */
2763 struct xfs_buf
*bp
; /* buffer for block */
2764 union xfs_btree_key key
; /* btree key */
2765 union xfs_btree_ptr nptr
; /* new block ptr */
2766 struct xfs_btree_cur
*ncur
; /* new btree cursor */
2767 union xfs_btree_rec nrec
; /* new record count */
2768 int optr
; /* old key/record index */
2769 int ptr
; /* key/record index */
2770 int numrecs
;/* number of records */
2771 int error
; /* error return value */
2776 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
2777 XFS_BTREE_TRACE_ARGIPR(cur
, level
, *ptrp
, recp
);
2782 * If we have an external root pointer, and we've made it to the
2783 * root level, allocate a new root block and we're done.
2785 if (!(cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
) &&
2786 (level
>= cur
->bc_nlevels
)) {
2787 error
= xfs_btree_new_root(cur
, stat
);
2788 xfs_btree_set_ptr_null(cur
, ptrp
);
2790 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
2794 /* If we're off the left edge, return failure. */
2795 ptr
= cur
->bc_ptrs
[level
];
2797 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
2802 /* Make a key out of the record data to be inserted, and save it. */
2803 cur
->bc_ops
->init_key_from_rec(&key
, recp
);
2807 XFS_BTREE_STATS_INC(cur
, insrec
);
2809 /* Get pointers to the btree buffer and block. */
2810 block
= xfs_btree_get_block(cur
, level
, &bp
);
2811 numrecs
= xfs_btree_get_numrecs(block
);
2814 error
= xfs_btree_check_block(cur
, block
, level
, bp
);
2818 /* Check that the new entry is being inserted in the right place. */
2819 if (ptr
<= numrecs
) {
2821 ASSERT(cur
->bc_ops
->recs_inorder(cur
, recp
,
2822 xfs_btree_rec_addr(cur
, ptr
, block
)));
2824 ASSERT(cur
->bc_ops
->keys_inorder(cur
, &key
,
2825 xfs_btree_key_addr(cur
, ptr
, block
)));
2831 * If the block is full, we can't insert the new entry until we
2832 * make the block un-full.
2834 xfs_btree_set_ptr_null(cur
, &nptr
);
2835 if (numrecs
== cur
->bc_ops
->get_maxrecs(cur
, level
)) {
2836 error
= xfs_btree_make_block_unfull(cur
, level
, numrecs
,
2837 &optr
, &ptr
, &nptr
, &ncur
, &nrec
, stat
);
2838 if (error
|| *stat
== 0)
2843 * The current block may have changed if the block was
2844 * previously full and we have just made space in it.
2846 block
= xfs_btree_get_block(cur
, level
, &bp
);
2847 numrecs
= xfs_btree_get_numrecs(block
);
2850 error
= xfs_btree_check_block(cur
, block
, level
, bp
);
2856 * At this point we know there's room for our new entry in the block
2857 * we're pointing at.
2859 XFS_BTREE_STATS_ADD(cur
, moves
, numrecs
- ptr
+ 1);
2862 /* It's a nonleaf. make a hole in the keys and ptrs */
2863 union xfs_btree_key
*kp
;
2864 union xfs_btree_ptr
*pp
;
2866 kp
= xfs_btree_key_addr(cur
, ptr
, block
);
2867 pp
= xfs_btree_ptr_addr(cur
, ptr
, block
);
2870 for (i
= numrecs
- ptr
; i
>= 0; i
--) {
2871 error
= xfs_btree_check_ptr(cur
, pp
, i
, level
);
2877 xfs_btree_shift_keys(cur
, kp
, 1, numrecs
- ptr
+ 1);
2878 xfs_btree_shift_ptrs(cur
, pp
, 1, numrecs
- ptr
+ 1);
2881 error
= xfs_btree_check_ptr(cur
, ptrp
, 0, level
);
2886 /* Now put the new data in, bump numrecs and log it. */
2887 xfs_btree_copy_keys(cur
, kp
, &key
, 1);
2888 xfs_btree_copy_ptrs(cur
, pp
, ptrp
, 1);
2890 xfs_btree_set_numrecs(block
, numrecs
);
2891 xfs_btree_log_ptrs(cur
, bp
, ptr
, numrecs
);
2892 xfs_btree_log_keys(cur
, bp
, ptr
, numrecs
);
2894 if (ptr
< numrecs
) {
2895 ASSERT(cur
->bc_ops
->keys_inorder(cur
, kp
,
2896 xfs_btree_key_addr(cur
, ptr
+ 1, block
)));
2900 /* It's a leaf. make a hole in the records */
2901 union xfs_btree_rec
*rp
;
2903 rp
= xfs_btree_rec_addr(cur
, ptr
, block
);
2905 xfs_btree_shift_recs(cur
, rp
, 1, numrecs
- ptr
+ 1);
2907 /* Now put the new data in, bump numrecs and log it. */
2908 xfs_btree_copy_recs(cur
, rp
, recp
, 1);
2909 xfs_btree_set_numrecs(block
, ++numrecs
);
2910 xfs_btree_log_recs(cur
, bp
, ptr
, numrecs
);
2912 if (ptr
< numrecs
) {
2913 ASSERT(cur
->bc_ops
->recs_inorder(cur
, rp
,
2914 xfs_btree_rec_addr(cur
, ptr
+ 1, block
)));
2919 /* Log the new number of records in the btree header. */
2920 xfs_btree_log_block(cur
, bp
, XFS_BB_NUMRECS
);
2922 /* If we inserted at the start of a block, update the parents' keys. */
2924 error
= xfs_btree_updkey(cur
, &key
, level
+ 1);
2930 * If we are tracking the last record in the tree and
2931 * we are at the far right edge of the tree, update it.
2933 if (xfs_btree_is_lastrec(cur
, block
, level
)) {
2934 cur
->bc_ops
->update_lastrec(cur
, block
, recp
,
2935 ptr
, LASTREC_INSREC
);
2939 * Return the new block number, if any.
2940 * If there is one, give back a record value and a cursor too.
2943 if (!xfs_btree_ptr_is_null(cur
, &nptr
)) {
2948 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
2953 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
2958 * Insert the record at the point referenced by cur.
2960 * A multi-level split of the tree on insert will invalidate the original
2961 * cursor. All callers of this function should assume that the cursor is
2962 * no longer valid and revalidate it.
2966 struct xfs_btree_cur
*cur
,
2969 int error
; /* error return value */
2970 int i
; /* result value, 0 for failure */
2971 int level
; /* current level number in btree */
2972 union xfs_btree_ptr nptr
; /* new block number (split result) */
2973 struct xfs_btree_cur
*ncur
; /* new cursor (split result) */
2974 struct xfs_btree_cur
*pcur
; /* previous level's cursor */
2975 union xfs_btree_rec rec
; /* record to insert */
2981 xfs_btree_set_ptr_null(cur
, &nptr
);
2982 cur
->bc_ops
->init_rec_from_cur(cur
, &rec
);
2985 * Loop going up the tree, starting at the leaf level.
2986 * Stop when we don't get a split block, that must mean that
2987 * the insert is finished with this level.
2991 * Insert nrec/nptr into this level of the tree.
2992 * Note if we fail, nptr will be null.
2994 error
= xfs_btree_insrec(pcur
, level
, &nptr
, &rec
, &ncur
, &i
);
2997 xfs_btree_del_cursor(pcur
, XFS_BTREE_ERROR
);
3001 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
3005 * See if the cursor we just used is trash.
3006 * Can't trash the caller's cursor, but otherwise we should
3007 * if ncur is a new cursor or we're about to be done.
3010 (ncur
|| xfs_btree_ptr_is_null(cur
, &nptr
))) {
3011 /* Save the state from the cursor before we trash it */
3012 if (cur
->bc_ops
->update_cursor
)
3013 cur
->bc_ops
->update_cursor(pcur
, cur
);
3014 cur
->bc_nlevels
= pcur
->bc_nlevels
;
3015 xfs_btree_del_cursor(pcur
, XFS_BTREE_NOERROR
);
3017 /* If we got a new cursor, switch to it. */
3022 } while (!xfs_btree_ptr_is_null(cur
, &nptr
));
3024 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
3028 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
3033 * Try to merge a non-leaf block back into the inode root.
3035 * Note: the killroot names comes from the fact that we're effectively
3036 * killing the old root block. But because we can't just delete the
3037 * inode we have to copy the single block it was pointing to into the
3041 xfs_btree_kill_iroot(
3042 struct xfs_btree_cur
*cur
)
3044 int whichfork
= cur
->bc_private
.b
.whichfork
;
3045 struct xfs_inode
*ip
= cur
->bc_private
.b
.ip
;
3046 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
3047 struct xfs_btree_block
*block
;
3048 struct xfs_btree_block
*cblock
;
3049 union xfs_btree_key
*kp
;
3050 union xfs_btree_key
*ckp
;
3051 union xfs_btree_ptr
*pp
;
3052 union xfs_btree_ptr
*cpp
;
3053 struct xfs_buf
*cbp
;
3058 union xfs_btree_ptr ptr
;
3062 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
3064 ASSERT(cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
);
3065 ASSERT(cur
->bc_nlevels
> 1);
3068 * Don't deal with the root block needs to be a leaf case.
3069 * We're just going to turn the thing back into extents anyway.
3071 level
= cur
->bc_nlevels
- 1;
3076 * Give up if the root has multiple children.
3078 block
= xfs_btree_get_iroot(cur
);
3079 if (xfs_btree_get_numrecs(block
) != 1)
3082 cblock
= xfs_btree_get_block(cur
, level
- 1, &cbp
);
3083 numrecs
= xfs_btree_get_numrecs(cblock
);
3086 * Only do this if the next level will fit.
3087 * Then the data must be copied up to the inode,
3088 * instead of freeing the root you free the next level.
3090 if (numrecs
> cur
->bc_ops
->get_dmaxrecs(cur
, level
))
3093 XFS_BTREE_STATS_INC(cur
, killroot
);
3096 xfs_btree_get_sibling(cur
, block
, &ptr
, XFS_BB_LEFTSIB
);
3097 ASSERT(xfs_btree_ptr_is_null(cur
, &ptr
));
3098 xfs_btree_get_sibling(cur
, block
, &ptr
, XFS_BB_RIGHTSIB
);
3099 ASSERT(xfs_btree_ptr_is_null(cur
, &ptr
));
3102 index
= numrecs
- cur
->bc_ops
->get_maxrecs(cur
, level
);
3104 xfs_iroot_realloc(cur
->bc_private
.b
.ip
, index
,
3105 cur
->bc_private
.b
.whichfork
);
3106 block
= ifp
->if_broot
;
3109 be16_add_cpu(&block
->bb_numrecs
, index
);
3110 ASSERT(block
->bb_numrecs
== cblock
->bb_numrecs
);
3112 kp
= xfs_btree_key_addr(cur
, 1, block
);
3113 ckp
= xfs_btree_key_addr(cur
, 1, cblock
);
3114 xfs_btree_copy_keys(cur
, kp
, ckp
, numrecs
);
3116 pp
= xfs_btree_ptr_addr(cur
, 1, block
);
3117 cpp
= xfs_btree_ptr_addr(cur
, 1, cblock
);
3119 for (i
= 0; i
< numrecs
; i
++) {
3122 error
= xfs_btree_check_ptr(cur
, cpp
, i
, level
- 1);
3124 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
3129 xfs_btree_copy_ptrs(cur
, pp
, cpp
, numrecs
);
3131 cur
->bc_ops
->free_block(cur
, cbp
);
3132 XFS_BTREE_STATS_INC(cur
, free
);
3134 cur
->bc_bufs
[level
- 1] = NULL
;
3135 be16_add_cpu(&block
->bb_level
, -1);
3136 xfs_trans_log_inode(cur
->bc_tp
, ip
,
3137 XFS_ILOG_CORE
| xfs_ilog_fbroot(cur
->bc_private
.b
.whichfork
));
3140 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
3145 * Kill the current root node, and replace it with it's only child node.
3148 xfs_btree_kill_root(
3149 struct xfs_btree_cur
*cur
,
3152 union xfs_btree_ptr
*newroot
)
3156 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
3157 XFS_BTREE_STATS_INC(cur
, killroot
);
3160 * Update the root pointer, decreasing the level by 1 and then
3161 * free the old root.
3163 cur
->bc_ops
->set_root(cur
, newroot
, -1);
3165 error
= cur
->bc_ops
->free_block(cur
, bp
);
3167 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
3171 XFS_BTREE_STATS_INC(cur
, free
);
3173 cur
->bc_bufs
[level
] = NULL
;
3174 cur
->bc_ra
[level
] = 0;
3177 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
3182 xfs_btree_dec_cursor(
3183 struct xfs_btree_cur
*cur
,
3191 error
= xfs_btree_decrement(cur
, level
, &i
);
3196 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
3202 * Single level of the btree record deletion routine.
3203 * Delete record pointed to by cur/level.
3204 * Remove the record from its block then rebalance the tree.
3205 * Return 0 for error, 1 for done, 2 to go on to the next level.
3207 STATIC
int /* error */
3209 struct xfs_btree_cur
*cur
, /* btree cursor */
3210 int level
, /* level removing record from */
3211 int *stat
) /* fail/done/go-on */
3213 struct xfs_btree_block
*block
; /* btree block */
3214 union xfs_btree_ptr cptr
; /* current block ptr */
3215 struct xfs_buf
*bp
; /* buffer for block */
3216 int error
; /* error return value */
3217 int i
; /* loop counter */
3218 union xfs_btree_key key
; /* storage for keyp */
3219 union xfs_btree_key
*keyp
= &key
; /* passed to the next level */
3220 union xfs_btree_ptr lptr
; /* left sibling block ptr */
3221 struct xfs_buf
*lbp
; /* left buffer pointer */
3222 struct xfs_btree_block
*left
; /* left btree block */
3223 int lrecs
= 0; /* left record count */
3224 int ptr
; /* key/record index */
3225 union xfs_btree_ptr rptr
; /* right sibling block ptr */
3226 struct xfs_buf
*rbp
; /* right buffer pointer */
3227 struct xfs_btree_block
*right
; /* right btree block */
3228 struct xfs_btree_block
*rrblock
; /* right-right btree block */
3229 struct xfs_buf
*rrbp
; /* right-right buffer pointer */
3230 int rrecs
= 0; /* right record count */
3231 struct xfs_btree_cur
*tcur
; /* temporary btree cursor */
3232 int numrecs
; /* temporary numrec count */
3234 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
3235 XFS_BTREE_TRACE_ARGI(cur
, level
);
3239 /* Get the index of the entry being deleted, check for nothing there. */
3240 ptr
= cur
->bc_ptrs
[level
];
3242 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
3247 /* Get the buffer & block containing the record or key/ptr. */
3248 block
= xfs_btree_get_block(cur
, level
, &bp
);
3249 numrecs
= xfs_btree_get_numrecs(block
);
3252 error
= xfs_btree_check_block(cur
, block
, level
, bp
);
3257 /* Fail if we're off the end of the block. */
3258 if (ptr
> numrecs
) {
3259 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
3264 XFS_BTREE_STATS_INC(cur
, delrec
);
3265 XFS_BTREE_STATS_ADD(cur
, moves
, numrecs
- ptr
);
3267 /* Excise the entries being deleted. */
3269 /* It's a nonleaf. operate on keys and ptrs */
3270 union xfs_btree_key
*lkp
;
3271 union xfs_btree_ptr
*lpp
;
3273 lkp
= xfs_btree_key_addr(cur
, ptr
+ 1, block
);
3274 lpp
= xfs_btree_ptr_addr(cur
, ptr
+ 1, block
);
3277 for (i
= 0; i
< numrecs
- ptr
; i
++) {
3278 error
= xfs_btree_check_ptr(cur
, lpp
, i
, level
);
3284 if (ptr
< numrecs
) {
3285 xfs_btree_shift_keys(cur
, lkp
, -1, numrecs
- ptr
);
3286 xfs_btree_shift_ptrs(cur
, lpp
, -1, numrecs
- ptr
);
3287 xfs_btree_log_keys(cur
, bp
, ptr
, numrecs
- 1);
3288 xfs_btree_log_ptrs(cur
, bp
, ptr
, numrecs
- 1);
3292 * If it's the first record in the block, we'll need to pass a
3293 * key up to the next level (updkey).
3296 keyp
= xfs_btree_key_addr(cur
, 1, block
);
3298 /* It's a leaf. operate on records */
3299 if (ptr
< numrecs
) {
3300 xfs_btree_shift_recs(cur
,
3301 xfs_btree_rec_addr(cur
, ptr
+ 1, block
),
3303 xfs_btree_log_recs(cur
, bp
, ptr
, numrecs
- 1);
3307 * If it's the first record in the block, we'll need a key
3308 * structure to pass up to the next level (updkey).
3311 cur
->bc_ops
->init_key_from_rec(&key
,
3312 xfs_btree_rec_addr(cur
, 1, block
));
3318 * Decrement and log the number of entries in the block.
3320 xfs_btree_set_numrecs(block
, --numrecs
);
3321 xfs_btree_log_block(cur
, bp
, XFS_BB_NUMRECS
);
3324 * If we are tracking the last record in the tree and
3325 * we are at the far right edge of the tree, update it.
3327 if (xfs_btree_is_lastrec(cur
, block
, level
)) {
3328 cur
->bc_ops
->update_lastrec(cur
, block
, NULL
,
3329 ptr
, LASTREC_DELREC
);
3333 * We're at the root level. First, shrink the root block in-memory.
3334 * Try to get rid of the next level down. If we can't then there's
3335 * nothing left to do.
3337 if (level
== cur
->bc_nlevels
- 1) {
3338 if (cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
) {
3339 xfs_iroot_realloc(cur
->bc_private
.b
.ip
, -1,
3340 cur
->bc_private
.b
.whichfork
);
3342 error
= xfs_btree_kill_iroot(cur
);
3346 error
= xfs_btree_dec_cursor(cur
, level
, stat
);
3354 * If this is the root level, and there's only one entry left,
3355 * and it's NOT the leaf level, then we can get rid of this
3358 if (numrecs
== 1 && level
> 0) {
3359 union xfs_btree_ptr
*pp
;
3361 * pp is still set to the first pointer in the block.
3362 * Make it the new root of the btree.
3364 pp
= xfs_btree_ptr_addr(cur
, 1, block
);
3365 error
= xfs_btree_kill_root(cur
, bp
, level
, pp
);
3368 } else if (level
> 0) {
3369 error
= xfs_btree_dec_cursor(cur
, level
, stat
);
3378 * If we deleted the leftmost entry in the block, update the
3379 * key values above us in the tree.
3382 error
= xfs_btree_updkey(cur
, keyp
, level
+ 1);
3388 * If the number of records remaining in the block is at least
3389 * the minimum, we're done.
3391 if (numrecs
>= cur
->bc_ops
->get_minrecs(cur
, level
)) {
3392 error
= xfs_btree_dec_cursor(cur
, level
, stat
);
3399 * Otherwise, we have to move some records around to keep the
3400 * tree balanced. Look at the left and right sibling blocks to
3401 * see if we can re-balance by moving only one record.
3403 xfs_btree_get_sibling(cur
, block
, &rptr
, XFS_BB_RIGHTSIB
);
3404 xfs_btree_get_sibling(cur
, block
, &lptr
, XFS_BB_LEFTSIB
);
3406 if (cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
) {
3408 * One child of root, need to get a chance to copy its contents
3409 * into the root and delete it. Can't go up to next level,
3410 * there's nothing to delete there.
3412 if (xfs_btree_ptr_is_null(cur
, &rptr
) &&
3413 xfs_btree_ptr_is_null(cur
, &lptr
) &&
3414 level
== cur
->bc_nlevels
- 2) {
3415 error
= xfs_btree_kill_iroot(cur
);
3417 error
= xfs_btree_dec_cursor(cur
, level
, stat
);
3424 ASSERT(!xfs_btree_ptr_is_null(cur
, &rptr
) ||
3425 !xfs_btree_ptr_is_null(cur
, &lptr
));
3428 * Duplicate the cursor so our btree manipulations here won't
3429 * disrupt the next level up.
3431 error
= xfs_btree_dup_cursor(cur
, &tcur
);
3436 * If there's a right sibling, see if it's ok to shift an entry
3439 if (!xfs_btree_ptr_is_null(cur
, &rptr
)) {
3441 * Move the temp cursor to the last entry in the next block.
3442 * Actually any entry but the first would suffice.
3444 i
= xfs_btree_lastrec(tcur
, level
);
3445 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
3447 error
= xfs_btree_increment(tcur
, level
, &i
);
3450 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
3452 i
= xfs_btree_lastrec(tcur
, level
);
3453 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
3455 /* Grab a pointer to the block. */
3456 right
= xfs_btree_get_block(tcur
, level
, &rbp
);
3458 error
= xfs_btree_check_block(tcur
, right
, level
, rbp
);
3462 /* Grab the current block number, for future use. */
3463 xfs_btree_get_sibling(tcur
, right
, &cptr
, XFS_BB_LEFTSIB
);
3466 * If right block is full enough so that removing one entry
3467 * won't make it too empty, and left-shifting an entry out
3468 * of right to us works, we're done.
3470 if (xfs_btree_get_numrecs(right
) - 1 >=
3471 cur
->bc_ops
->get_minrecs(tcur
, level
)) {
3472 error
= xfs_btree_lshift(tcur
, level
, &i
);
3476 ASSERT(xfs_btree_get_numrecs(block
) >=
3477 cur
->bc_ops
->get_minrecs(tcur
, level
));
3479 xfs_btree_del_cursor(tcur
, XFS_BTREE_NOERROR
);
3482 error
= xfs_btree_dec_cursor(cur
, level
, stat
);
3490 * Otherwise, grab the number of records in right for
3491 * future reference, and fix up the temp cursor to point
3492 * to our block again (last record).
3494 rrecs
= xfs_btree_get_numrecs(right
);
3495 if (!xfs_btree_ptr_is_null(cur
, &lptr
)) {
3496 i
= xfs_btree_firstrec(tcur
, level
);
3497 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
3499 error
= xfs_btree_decrement(tcur
, level
, &i
);
3502 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
3507 * If there's a left sibling, see if it's ok to shift an entry
3510 if (!xfs_btree_ptr_is_null(cur
, &lptr
)) {
3512 * Move the temp cursor to the first entry in the
3515 i
= xfs_btree_firstrec(tcur
, level
);
3516 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
3518 error
= xfs_btree_decrement(tcur
, level
, &i
);
3521 i
= xfs_btree_firstrec(tcur
, level
);
3522 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
3524 /* Grab a pointer to the block. */
3525 left
= xfs_btree_get_block(tcur
, level
, &lbp
);
3527 error
= xfs_btree_check_block(cur
, left
, level
, lbp
);
3531 /* Grab the current block number, for future use. */
3532 xfs_btree_get_sibling(tcur
, left
, &cptr
, XFS_BB_RIGHTSIB
);
3535 * If left block is full enough so that removing one entry
3536 * won't make it too empty, and right-shifting an entry out
3537 * of left to us works, we're done.
3539 if (xfs_btree_get_numrecs(left
) - 1 >=
3540 cur
->bc_ops
->get_minrecs(tcur
, level
)) {
3541 error
= xfs_btree_rshift(tcur
, level
, &i
);
3545 ASSERT(xfs_btree_get_numrecs(block
) >=
3546 cur
->bc_ops
->get_minrecs(tcur
, level
));
3547 xfs_btree_del_cursor(tcur
, XFS_BTREE_NOERROR
);
3551 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
3558 * Otherwise, grab the number of records in right for
3561 lrecs
= xfs_btree_get_numrecs(left
);
3564 /* Delete the temp cursor, we're done with it. */
3565 xfs_btree_del_cursor(tcur
, XFS_BTREE_NOERROR
);
3568 /* If here, we need to do a join to keep the tree balanced. */
3569 ASSERT(!xfs_btree_ptr_is_null(cur
, &cptr
));
3571 if (!xfs_btree_ptr_is_null(cur
, &lptr
) &&
3572 lrecs
+ xfs_btree_get_numrecs(block
) <=
3573 cur
->bc_ops
->get_maxrecs(cur
, level
)) {
3575 * Set "right" to be the starting block,
3576 * "left" to be the left neighbor.
3581 error
= xfs_btree_read_buf_block(cur
, &lptr
, level
,
3587 * If that won't work, see if we can join with the right neighbor block.
3589 } else if (!xfs_btree_ptr_is_null(cur
, &rptr
) &&
3590 rrecs
+ xfs_btree_get_numrecs(block
) <=
3591 cur
->bc_ops
->get_maxrecs(cur
, level
)) {
3593 * Set "left" to be the starting block,
3594 * "right" to be the right neighbor.
3599 error
= xfs_btree_read_buf_block(cur
, &rptr
, level
,
3605 * Otherwise, we can't fix the imbalance.
3606 * Just return. This is probably a logic error, but it's not fatal.
3609 error
= xfs_btree_dec_cursor(cur
, level
, stat
);
3615 rrecs
= xfs_btree_get_numrecs(right
);
3616 lrecs
= xfs_btree_get_numrecs(left
);
3619 * We're now going to join "left" and "right" by moving all the stuff
3620 * in "right" to "left" and deleting "right".
3622 XFS_BTREE_STATS_ADD(cur
, moves
, rrecs
);
3624 /* It's a non-leaf. Move keys and pointers. */
3625 union xfs_btree_key
*lkp
; /* left btree key */
3626 union xfs_btree_ptr
*lpp
; /* left address pointer */
3627 union xfs_btree_key
*rkp
; /* right btree key */
3628 union xfs_btree_ptr
*rpp
; /* right address pointer */
3630 lkp
= xfs_btree_key_addr(cur
, lrecs
+ 1, left
);
3631 lpp
= xfs_btree_ptr_addr(cur
, lrecs
+ 1, left
);
3632 rkp
= xfs_btree_key_addr(cur
, 1, right
);
3633 rpp
= xfs_btree_ptr_addr(cur
, 1, right
);
3635 for (i
= 1; i
< rrecs
; i
++) {
3636 error
= xfs_btree_check_ptr(cur
, rpp
, i
, level
);
3641 xfs_btree_copy_keys(cur
, lkp
, rkp
, rrecs
);
3642 xfs_btree_copy_ptrs(cur
, lpp
, rpp
, rrecs
);
3644 xfs_btree_log_keys(cur
, lbp
, lrecs
+ 1, lrecs
+ rrecs
);
3645 xfs_btree_log_ptrs(cur
, lbp
, lrecs
+ 1, lrecs
+ rrecs
);
3647 /* It's a leaf. Move records. */
3648 union xfs_btree_rec
*lrp
; /* left record pointer */
3649 union xfs_btree_rec
*rrp
; /* right record pointer */
3651 lrp
= xfs_btree_rec_addr(cur
, lrecs
+ 1, left
);
3652 rrp
= xfs_btree_rec_addr(cur
, 1, right
);
3654 xfs_btree_copy_recs(cur
, lrp
, rrp
, rrecs
);
3655 xfs_btree_log_recs(cur
, lbp
, lrecs
+ 1, lrecs
+ rrecs
);
3658 XFS_BTREE_STATS_INC(cur
, join
);
3661 * Fix up the number of records and right block pointer in the
3662 * surviving block, and log it.
3664 xfs_btree_set_numrecs(left
, lrecs
+ rrecs
);
3665 xfs_btree_get_sibling(cur
, right
, &cptr
, XFS_BB_RIGHTSIB
),
3666 xfs_btree_set_sibling(cur
, left
, &cptr
, XFS_BB_RIGHTSIB
);
3667 xfs_btree_log_block(cur
, lbp
, XFS_BB_NUMRECS
| XFS_BB_RIGHTSIB
);
3669 /* If there is a right sibling, point it to the remaining block. */
3670 xfs_btree_get_sibling(cur
, left
, &cptr
, XFS_BB_RIGHTSIB
);
3671 if (!xfs_btree_ptr_is_null(cur
, &cptr
)) {
3672 error
= xfs_btree_read_buf_block(cur
, &cptr
, level
,
3673 0, &rrblock
, &rrbp
);
3676 xfs_btree_set_sibling(cur
, rrblock
, &lptr
, XFS_BB_LEFTSIB
);
3677 xfs_btree_log_block(cur
, rrbp
, XFS_BB_LEFTSIB
);
3680 /* Free the deleted block. */
3681 error
= cur
->bc_ops
->free_block(cur
, rbp
);
3684 XFS_BTREE_STATS_INC(cur
, free
);
3687 * If we joined with the left neighbor, set the buffer in the
3688 * cursor to the left block, and fix up the index.
3691 cur
->bc_bufs
[level
] = lbp
;
3692 cur
->bc_ptrs
[level
] += lrecs
;
3693 cur
->bc_ra
[level
] = 0;
3696 * If we joined with the right neighbor and there's a level above
3697 * us, increment the cursor at that level.
3699 else if ((cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
) ||
3700 (level
+ 1 < cur
->bc_nlevels
)) {
3701 error
= xfs_btree_increment(cur
, level
+ 1, &i
);
3707 * Readjust the ptr at this level if it's not a leaf, since it's
3708 * still pointing at the deletion point, which makes the cursor
3709 * inconsistent. If this makes the ptr 0, the caller fixes it up.
3710 * We can't use decrement because it would change the next level up.
3713 cur
->bc_ptrs
[level
]--;
3715 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
3716 /* Return value means the next level up has something to do. */
3721 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
3723 xfs_btree_del_cursor(tcur
, XFS_BTREE_ERROR
);
3728 * Delete the record pointed to by cur.
3729 * The cursor refers to the place where the record was (could be inserted)
3730 * when the operation returns.
3734 struct xfs_btree_cur
*cur
,
3735 int *stat
) /* success/failure */
3737 int error
; /* error return value */
3741 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
3744 * Go up the tree, starting at leaf level.
3746 * If 2 is returned then a join was done; go to the next level.
3747 * Otherwise we are done.
3749 for (level
= 0, i
= 2; i
== 2; level
++) {
3750 error
= xfs_btree_delrec(cur
, level
, &i
);
3756 for (level
= 1; level
< cur
->bc_nlevels
; level
++) {
3757 if (cur
->bc_ptrs
[level
] == 0) {
3758 error
= xfs_btree_decrement(cur
, level
, &i
);
3766 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
3770 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
3775 * Get the data from the pointed-to record.
3779 struct xfs_btree_cur
*cur
, /* btree cursor */
3780 union xfs_btree_rec
**recp
, /* output: btree record */
3781 int *stat
) /* output: success/failure */
3783 struct xfs_btree_block
*block
; /* btree block */
3784 struct xfs_buf
*bp
; /* buffer pointer */
3785 int ptr
; /* record number */
3787 int error
; /* error return value */
3790 ptr
= cur
->bc_ptrs
[0];
3791 block
= xfs_btree_get_block(cur
, 0, &bp
);
3794 error
= xfs_btree_check_block(cur
, block
, 0, bp
);
3800 * Off the right end or left end, return failure.
3802 if (ptr
> xfs_btree_get_numrecs(block
) || ptr
<= 0) {
3808 * Point to the record and extract its data.
3810 *recp
= xfs_btree_rec_addr(cur
, ptr
, block
);