2 * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 #include "xfs_types.h"
23 #include "xfs_trans.h"
26 #include "xfs_mount.h"
27 #include "xfs_bmap_btree.h"
28 #include "xfs_alloc_btree.h"
29 #include "xfs_ialloc_btree.h"
30 #include "xfs_dinode.h"
31 #include "xfs_inode.h"
32 #include "xfs_inode_item.h"
33 #include "xfs_buf_item.h"
34 #include "xfs_btree.h"
35 #include "xfs_error.h"
36 #include "xfs_trace.h"
37 #include "xfs_cksum.h"
40 * Cursor allocation zone.
42 kmem_zone_t
*xfs_btree_cur_zone
;
45 * Btree magic numbers.
47 static const __uint32_t xfs_magics
[2][XFS_BTNUM_MAX
] = {
48 { XFS_ABTB_MAGIC
, XFS_ABTC_MAGIC
, XFS_BMAP_MAGIC
, XFS_IBT_MAGIC
},
49 { XFS_ABTB_CRC_MAGIC
, XFS_ABTC_CRC_MAGIC
,
50 XFS_BMAP_CRC_MAGIC
, XFS_IBT_CRC_MAGIC
}
52 #define xfs_btree_magic(cur) \
53 xfs_magics[!!((cur)->bc_flags & XFS_BTREE_CRC_BLOCKS)][cur->bc_btnum]
56 STATIC
int /* error (0 or EFSCORRUPTED) */
57 xfs_btree_check_lblock(
58 struct xfs_btree_cur
*cur
, /* btree cursor */
59 struct xfs_btree_block
*block
, /* btree long form block pointer */
60 int level
, /* level of the btree block */
61 struct xfs_buf
*bp
) /* buffer for block, if any */
63 int lblock_ok
= 1; /* block passes checks */
64 struct xfs_mount
*mp
; /* file system mount point */
68 if (xfs_sb_version_hascrc(&mp
->m_sb
)) {
69 lblock_ok
= lblock_ok
&&
70 uuid_equal(&block
->bb_u
.l
.bb_uuid
, &mp
->m_sb
.sb_uuid
) &&
71 block
->bb_u
.l
.bb_blkno
== cpu_to_be64(
72 bp
? bp
->b_bn
: XFS_BUF_DADDR_NULL
);
75 lblock_ok
= lblock_ok
&&
76 be32_to_cpu(block
->bb_magic
) == xfs_btree_magic(cur
) &&
77 be16_to_cpu(block
->bb_level
) == level
&&
78 be16_to_cpu(block
->bb_numrecs
) <=
79 cur
->bc_ops
->get_maxrecs(cur
, level
) &&
80 block
->bb_u
.l
.bb_leftsib
&&
81 (block
->bb_u
.l
.bb_leftsib
== cpu_to_be64(NULLDFSBNO
) ||
82 XFS_FSB_SANITY_CHECK(mp
,
83 be64_to_cpu(block
->bb_u
.l
.bb_leftsib
))) &&
84 block
->bb_u
.l
.bb_rightsib
&&
85 (block
->bb_u
.l
.bb_rightsib
== cpu_to_be64(NULLDFSBNO
) ||
86 XFS_FSB_SANITY_CHECK(mp
,
87 be64_to_cpu(block
->bb_u
.l
.bb_rightsib
)));
89 if (unlikely(XFS_TEST_ERROR(!lblock_ok
, mp
,
90 XFS_ERRTAG_BTREE_CHECK_LBLOCK
,
91 XFS_RANDOM_BTREE_CHECK_LBLOCK
))) {
93 trace_xfs_btree_corrupt(bp
, _RET_IP_
);
94 XFS_ERROR_REPORT(__func__
, XFS_ERRLEVEL_LOW
, mp
);
95 return XFS_ERROR(EFSCORRUPTED
);
100 STATIC
int /* error (0 or EFSCORRUPTED) */
101 xfs_btree_check_sblock(
102 struct xfs_btree_cur
*cur
, /* btree cursor */
103 struct xfs_btree_block
*block
, /* btree short form block pointer */
104 int level
, /* level of the btree block */
105 struct xfs_buf
*bp
) /* buffer containing block */
107 struct xfs_mount
*mp
; /* file system mount point */
108 struct xfs_buf
*agbp
; /* buffer for ag. freespace struct */
109 struct xfs_agf
*agf
; /* ag. freespace structure */
110 xfs_agblock_t agflen
; /* native ag. freespace length */
111 int sblock_ok
= 1; /* block passes checks */
114 agbp
= cur
->bc_private
.a
.agbp
;
115 agf
= XFS_BUF_TO_AGF(agbp
);
116 agflen
= be32_to_cpu(agf
->agf_length
);
118 if (xfs_sb_version_hascrc(&mp
->m_sb
)) {
119 sblock_ok
= sblock_ok
&&
120 uuid_equal(&block
->bb_u
.s
.bb_uuid
, &mp
->m_sb
.sb_uuid
) &&
121 block
->bb_u
.s
.bb_blkno
== cpu_to_be64(
122 bp
? bp
->b_bn
: XFS_BUF_DADDR_NULL
);
125 sblock_ok
= sblock_ok
&&
126 be32_to_cpu(block
->bb_magic
) == xfs_btree_magic(cur
) &&
127 be16_to_cpu(block
->bb_level
) == level
&&
128 be16_to_cpu(block
->bb_numrecs
) <=
129 cur
->bc_ops
->get_maxrecs(cur
, level
) &&
130 (block
->bb_u
.s
.bb_leftsib
== cpu_to_be32(NULLAGBLOCK
) ||
131 be32_to_cpu(block
->bb_u
.s
.bb_leftsib
) < agflen
) &&
132 block
->bb_u
.s
.bb_leftsib
&&
133 (block
->bb_u
.s
.bb_rightsib
== cpu_to_be32(NULLAGBLOCK
) ||
134 be32_to_cpu(block
->bb_u
.s
.bb_rightsib
) < agflen
) &&
135 block
->bb_u
.s
.bb_rightsib
;
137 if (unlikely(XFS_TEST_ERROR(!sblock_ok
, mp
,
138 XFS_ERRTAG_BTREE_CHECK_SBLOCK
,
139 XFS_RANDOM_BTREE_CHECK_SBLOCK
))) {
141 trace_xfs_btree_corrupt(bp
, _RET_IP_
);
142 XFS_ERROR_REPORT(__func__
, XFS_ERRLEVEL_LOW
, mp
);
143 return XFS_ERROR(EFSCORRUPTED
);
149 * Debug routine: check that block header is ok.
152 xfs_btree_check_block(
153 struct xfs_btree_cur
*cur
, /* btree cursor */
154 struct xfs_btree_block
*block
, /* generic btree block pointer */
155 int level
, /* level of the btree block */
156 struct xfs_buf
*bp
) /* buffer containing block, if any */
158 if (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
)
159 return xfs_btree_check_lblock(cur
, block
, level
, bp
);
161 return xfs_btree_check_sblock(cur
, block
, level
, bp
);
165 * Check that (long) pointer is ok.
167 int /* error (0 or EFSCORRUPTED) */
168 xfs_btree_check_lptr(
169 struct xfs_btree_cur
*cur
, /* btree cursor */
170 xfs_dfsbno_t bno
, /* btree block disk address */
171 int level
) /* btree block level */
173 XFS_WANT_CORRUPTED_RETURN(
176 XFS_FSB_SANITY_CHECK(cur
->bc_mp
, bno
));
182 * Check that (short) pointer is ok.
184 STATIC
int /* error (0 or EFSCORRUPTED) */
185 xfs_btree_check_sptr(
186 struct xfs_btree_cur
*cur
, /* btree cursor */
187 xfs_agblock_t bno
, /* btree block disk address */
188 int level
) /* btree block level */
190 xfs_agblock_t agblocks
= cur
->bc_mp
->m_sb
.sb_agblocks
;
192 XFS_WANT_CORRUPTED_RETURN(
194 bno
!= NULLAGBLOCK
&&
201 * Check that block ptr is ok.
203 STATIC
int /* error (0 or EFSCORRUPTED) */
205 struct xfs_btree_cur
*cur
, /* btree cursor */
206 union xfs_btree_ptr
*ptr
, /* btree block disk address */
207 int index
, /* offset from ptr to check */
208 int level
) /* btree block level */
210 if (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
) {
211 return xfs_btree_check_lptr(cur
,
212 be64_to_cpu((&ptr
->l
)[index
]), level
);
214 return xfs_btree_check_sptr(cur
,
215 be32_to_cpu((&ptr
->s
)[index
]), level
);
221 * Calculate CRC on the whole btree block and stuff it into the
222 * long-form btree header.
224 * Prior to calculting the CRC, pull the LSN out of the buffer log item and put
225 * it into the buffer so recovery knows what the last modifcation was that made
229 xfs_btree_lblock_calc_crc(
232 struct xfs_btree_block
*block
= XFS_BUF_TO_BLOCK(bp
);
233 struct xfs_buf_log_item
*bip
= bp
->b_fspriv
;
235 if (!xfs_sb_version_hascrc(&bp
->b_target
->bt_mount
->m_sb
))
238 block
->bb_u
.l
.bb_lsn
= cpu_to_be64(bip
->bli_item
.li_lsn
);
239 xfs_update_cksum(bp
->b_addr
, BBTOB(bp
->b_length
),
240 XFS_BTREE_LBLOCK_CRC_OFF
);
244 xfs_btree_lblock_verify_crc(
247 if (xfs_sb_version_hascrc(&bp
->b_target
->bt_mount
->m_sb
))
248 return xfs_verify_cksum(bp
->b_addr
, BBTOB(bp
->b_length
),
249 XFS_BTREE_LBLOCK_CRC_OFF
);
254 * Calculate CRC on the whole btree block and stuff it into the
255 * short-form btree header.
257 * Prior to calculting the CRC, pull the LSN out of the buffer log item and put
258 * it into the buffer so recovery knows what the last modifcation was that made
262 xfs_btree_sblock_calc_crc(
265 struct xfs_btree_block
*block
= XFS_BUF_TO_BLOCK(bp
);
266 struct xfs_buf_log_item
*bip
= bp
->b_fspriv
;
268 if (!xfs_sb_version_hascrc(&bp
->b_target
->bt_mount
->m_sb
))
271 block
->bb_u
.s
.bb_lsn
= cpu_to_be64(bip
->bli_item
.li_lsn
);
272 xfs_update_cksum(bp
->b_addr
, BBTOB(bp
->b_length
),
273 XFS_BTREE_SBLOCK_CRC_OFF
);
277 xfs_btree_sblock_verify_crc(
280 if (xfs_sb_version_hascrc(&bp
->b_target
->bt_mount
->m_sb
))
281 return xfs_verify_cksum(bp
->b_addr
, BBTOB(bp
->b_length
),
282 XFS_BTREE_SBLOCK_CRC_OFF
);
287 * Delete the btree cursor.
290 xfs_btree_del_cursor(
291 xfs_btree_cur_t
*cur
, /* btree cursor */
292 int error
) /* del because of error */
294 int i
; /* btree level */
297 * Clear the buffer pointers, and release the buffers.
298 * If we're doing this in the face of an error, we
299 * need to make sure to inspect all of the entries
300 * in the bc_bufs array for buffers to be unlocked.
301 * This is because some of the btree code works from
302 * level n down to 0, and if we get an error along
303 * the way we won't have initialized all the entries
306 for (i
= 0; i
< cur
->bc_nlevels
; i
++) {
308 xfs_trans_brelse(cur
->bc_tp
, cur
->bc_bufs
[i
]);
313 * Can't free a bmap cursor without having dealt with the
314 * allocated indirect blocks' accounting.
316 ASSERT(cur
->bc_btnum
!= XFS_BTNUM_BMAP
||
317 cur
->bc_private
.b
.allocated
== 0);
321 kmem_zone_free(xfs_btree_cur_zone
, cur
);
325 * Duplicate the btree cursor.
326 * Allocate a new one, copy the record, re-get the buffers.
329 xfs_btree_dup_cursor(
330 xfs_btree_cur_t
*cur
, /* input cursor */
331 xfs_btree_cur_t
**ncur
) /* output cursor */
333 xfs_buf_t
*bp
; /* btree block's buffer pointer */
334 int error
; /* error return value */
335 int i
; /* level number of btree block */
336 xfs_mount_t
*mp
; /* mount structure for filesystem */
337 xfs_btree_cur_t
*new; /* new cursor value */
338 xfs_trans_t
*tp
; /* transaction pointer, can be NULL */
344 * Allocate a new cursor like the old one.
346 new = cur
->bc_ops
->dup_cursor(cur
);
349 * Copy the record currently in the cursor.
351 new->bc_rec
= cur
->bc_rec
;
354 * For each level current, re-get the buffer and copy the ptr value.
356 for (i
= 0; i
< new->bc_nlevels
; i
++) {
357 new->bc_ptrs
[i
] = cur
->bc_ptrs
[i
];
358 new->bc_ra
[i
] = cur
->bc_ra
[i
];
359 bp
= cur
->bc_bufs
[i
];
361 error
= xfs_trans_read_buf(mp
, tp
, mp
->m_ddev_targp
,
362 XFS_BUF_ADDR(bp
), mp
->m_bsize
,
364 cur
->bc_ops
->buf_ops
);
366 xfs_btree_del_cursor(new, error
);
371 new->bc_bufs
[i
] = bp
;
378 * XFS btree block layout and addressing:
380 * There are two types of blocks in the btree: leaf and non-leaf blocks.
382 * The leaf record start with a header then followed by records containing
383 * the values. A non-leaf block also starts with the same header, and
384 * then first contains lookup keys followed by an equal number of pointers
385 * to the btree blocks at the previous level.
387 * +--------+-------+-------+-------+-------+-------+-------+
388 * Leaf: | header | rec 1 | rec 2 | rec 3 | rec 4 | rec 5 | rec N |
389 * +--------+-------+-------+-------+-------+-------+-------+
391 * +--------+-------+-------+-------+-------+-------+-------+
392 * Non-Leaf: | header | key 1 | key 2 | key N | ptr 1 | ptr 2 | ptr N |
393 * +--------+-------+-------+-------+-------+-------+-------+
395 * The header is called struct xfs_btree_block for reasons better left unknown
396 * and comes in different versions for short (32bit) and long (64bit) block
397 * pointers. The record and key structures are defined by the btree instances
398 * and opaque to the btree core. The block pointers are simple disk endian
399 * integers, available in a short (32bit) and long (64bit) variant.
401 * The helpers below calculate the offset of a given record, key or pointer
402 * into a btree block (xfs_btree_*_offset) or return a pointer to the given
403 * record, key or pointer (xfs_btree_*_addr). Note that all addressing
404 * inside the btree block is done using indices starting at one, not zero!
408 * Return size of the btree block header for this btree instance.
410 static inline size_t xfs_btree_block_len(struct xfs_btree_cur
*cur
)
412 if (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
) {
413 if (cur
->bc_flags
& XFS_BTREE_CRC_BLOCKS
)
414 return XFS_BTREE_LBLOCK_CRC_LEN
;
415 return XFS_BTREE_LBLOCK_LEN
;
417 if (cur
->bc_flags
& XFS_BTREE_CRC_BLOCKS
)
418 return XFS_BTREE_SBLOCK_CRC_LEN
;
419 return XFS_BTREE_SBLOCK_LEN
;
423 * Return size of btree block pointers for this btree instance.
425 static inline size_t xfs_btree_ptr_len(struct xfs_btree_cur
*cur
)
427 return (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
) ?
428 sizeof(__be64
) : sizeof(__be32
);
432 * Calculate offset of the n-th record in a btree block.
435 xfs_btree_rec_offset(
436 struct xfs_btree_cur
*cur
,
439 return xfs_btree_block_len(cur
) +
440 (n
- 1) * cur
->bc_ops
->rec_len
;
444 * Calculate offset of the n-th key in a btree block.
447 xfs_btree_key_offset(
448 struct xfs_btree_cur
*cur
,
451 return xfs_btree_block_len(cur
) +
452 (n
- 1) * cur
->bc_ops
->key_len
;
456 * Calculate offset of the n-th block pointer in a btree block.
459 xfs_btree_ptr_offset(
460 struct xfs_btree_cur
*cur
,
464 return xfs_btree_block_len(cur
) +
465 cur
->bc_ops
->get_maxrecs(cur
, level
) * cur
->bc_ops
->key_len
+
466 (n
- 1) * xfs_btree_ptr_len(cur
);
470 * Return a pointer to the n-th record in the btree block.
472 STATIC
union xfs_btree_rec
*
474 struct xfs_btree_cur
*cur
,
476 struct xfs_btree_block
*block
)
478 return (union xfs_btree_rec
*)
479 ((char *)block
+ xfs_btree_rec_offset(cur
, n
));
483 * Return a pointer to the n-th key in the btree block.
485 STATIC
union xfs_btree_key
*
487 struct xfs_btree_cur
*cur
,
489 struct xfs_btree_block
*block
)
491 return (union xfs_btree_key
*)
492 ((char *)block
+ xfs_btree_key_offset(cur
, n
));
496 * Return a pointer to the n-th block pointer in the btree block.
498 STATIC
union xfs_btree_ptr
*
500 struct xfs_btree_cur
*cur
,
502 struct xfs_btree_block
*block
)
504 int level
= xfs_btree_get_level(block
);
506 ASSERT(block
->bb_level
!= 0);
508 return (union xfs_btree_ptr
*)
509 ((char *)block
+ xfs_btree_ptr_offset(cur
, n
, level
));
513 * Get a the root block which is stored in the inode.
515 * For now this btree implementation assumes the btree root is always
516 * stored in the if_broot field of an inode fork.
518 STATIC
struct xfs_btree_block
*
520 struct xfs_btree_cur
*cur
)
522 struct xfs_ifork
*ifp
;
524 ifp
= XFS_IFORK_PTR(cur
->bc_private
.b
.ip
, cur
->bc_private
.b
.whichfork
);
525 return (struct xfs_btree_block
*)ifp
->if_broot
;
529 * Retrieve the block pointer from the cursor at the given level.
530 * This may be an inode btree root or from a buffer.
532 STATIC
struct xfs_btree_block
* /* generic btree block pointer */
534 struct xfs_btree_cur
*cur
, /* btree cursor */
535 int level
, /* level in btree */
536 struct xfs_buf
**bpp
) /* buffer containing the block */
538 if ((cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
) &&
539 (level
== cur
->bc_nlevels
- 1)) {
541 return xfs_btree_get_iroot(cur
);
544 *bpp
= cur
->bc_bufs
[level
];
545 return XFS_BUF_TO_BLOCK(*bpp
);
549 * Get a buffer for the block, return it with no data read.
550 * Long-form addressing.
552 xfs_buf_t
* /* buffer for fsbno */
554 xfs_mount_t
*mp
, /* file system mount point */
555 xfs_trans_t
*tp
, /* transaction pointer */
556 xfs_fsblock_t fsbno
, /* file system block number */
557 uint lock
) /* lock flags for get_buf */
559 xfs_buf_t
*bp
; /* buffer pointer (return value) */
560 xfs_daddr_t d
; /* real disk block address */
562 ASSERT(fsbno
!= NULLFSBLOCK
);
563 d
= XFS_FSB_TO_DADDR(mp
, fsbno
);
564 bp
= xfs_trans_get_buf(tp
, mp
->m_ddev_targp
, d
, mp
->m_bsize
, lock
);
565 ASSERT(!xfs_buf_geterror(bp
));
570 * Get a buffer for the block, return it with no data read.
571 * Short-form addressing.
573 xfs_buf_t
* /* buffer for agno/agbno */
575 xfs_mount_t
*mp
, /* file system mount point */
576 xfs_trans_t
*tp
, /* transaction pointer */
577 xfs_agnumber_t agno
, /* allocation group number */
578 xfs_agblock_t agbno
, /* allocation group block number */
579 uint lock
) /* lock flags for get_buf */
581 xfs_buf_t
*bp
; /* buffer pointer (return value) */
582 xfs_daddr_t d
; /* real disk block address */
584 ASSERT(agno
!= NULLAGNUMBER
);
585 ASSERT(agbno
!= NULLAGBLOCK
);
586 d
= XFS_AGB_TO_DADDR(mp
, agno
, agbno
);
587 bp
= xfs_trans_get_buf(tp
, mp
->m_ddev_targp
, d
, mp
->m_bsize
, lock
);
588 ASSERT(!xfs_buf_geterror(bp
));
593 * Check for the cursor referring to the last block at the given level.
595 int /* 1=is last block, 0=not last block */
596 xfs_btree_islastblock(
597 xfs_btree_cur_t
*cur
, /* btree cursor */
598 int level
) /* level to check */
600 struct xfs_btree_block
*block
; /* generic btree block pointer */
601 xfs_buf_t
*bp
; /* buffer containing block */
603 block
= xfs_btree_get_block(cur
, level
, &bp
);
604 xfs_btree_check_block(cur
, block
, level
, bp
);
605 if (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
)
606 return block
->bb_u
.l
.bb_rightsib
== cpu_to_be64(NULLDFSBNO
);
608 return block
->bb_u
.s
.bb_rightsib
== cpu_to_be32(NULLAGBLOCK
);
612 * Change the cursor to point to the first record at the given level.
613 * Other levels are unaffected.
615 STATIC
int /* success=1, failure=0 */
617 xfs_btree_cur_t
*cur
, /* btree cursor */
618 int level
) /* level to change */
620 struct xfs_btree_block
*block
; /* generic btree block pointer */
621 xfs_buf_t
*bp
; /* buffer containing block */
624 * Get the block pointer for this level.
626 block
= xfs_btree_get_block(cur
, level
, &bp
);
627 xfs_btree_check_block(cur
, block
, level
, bp
);
629 * It's empty, there is no such record.
631 if (!block
->bb_numrecs
)
634 * Set the ptr value to 1, that's the first record/key.
636 cur
->bc_ptrs
[level
] = 1;
641 * Change the cursor to point to the last record in the current block
642 * at the given level. Other levels are unaffected.
644 STATIC
int /* success=1, failure=0 */
646 xfs_btree_cur_t
*cur
, /* btree cursor */
647 int level
) /* level to change */
649 struct xfs_btree_block
*block
; /* generic btree block pointer */
650 xfs_buf_t
*bp
; /* buffer containing block */
653 * Get the block pointer for this level.
655 block
= xfs_btree_get_block(cur
, level
, &bp
);
656 xfs_btree_check_block(cur
, block
, level
, bp
);
658 * It's empty, there is no such record.
660 if (!block
->bb_numrecs
)
663 * Set the ptr value to numrecs, that's the last record/key.
665 cur
->bc_ptrs
[level
] = be16_to_cpu(block
->bb_numrecs
);
670 * Compute first and last byte offsets for the fields given.
671 * Interprets the offsets table, which contains struct field offsets.
675 __int64_t fields
, /* bitmask of fields */
676 const short *offsets
, /* table of field offsets */
677 int nbits
, /* number of bits to inspect */
678 int *first
, /* output: first byte offset */
679 int *last
) /* output: last byte offset */
681 int i
; /* current bit number */
682 __int64_t imask
; /* mask for current bit number */
686 * Find the lowest bit, so the first byte offset.
688 for (i
= 0, imask
= 1LL; ; i
++, imask
<<= 1) {
689 if (imask
& fields
) {
695 * Find the highest bit, so the last byte offset.
697 for (i
= nbits
- 1, imask
= 1LL << i
; ; i
--, imask
>>= 1) {
698 if (imask
& fields
) {
699 *last
= offsets
[i
+ 1] - 1;
706 * Get a buffer for the block, return it read in.
707 * Long-form addressing.
711 struct xfs_mount
*mp
, /* file system mount point */
712 struct xfs_trans
*tp
, /* transaction pointer */
713 xfs_fsblock_t fsbno
, /* file system block number */
714 uint lock
, /* lock flags for read_buf */
715 struct xfs_buf
**bpp
, /* buffer for fsbno */
716 int refval
, /* ref count value for buffer */
717 const struct xfs_buf_ops
*ops
)
719 struct xfs_buf
*bp
; /* return value */
720 xfs_daddr_t d
; /* real disk block address */
723 ASSERT(fsbno
!= NULLFSBLOCK
);
724 d
= XFS_FSB_TO_DADDR(mp
, fsbno
);
725 error
= xfs_trans_read_buf(mp
, tp
, mp
->m_ddev_targp
, d
,
726 mp
->m_bsize
, lock
, &bp
, ops
);
729 ASSERT(!xfs_buf_geterror(bp
));
731 xfs_buf_set_ref(bp
, refval
);
737 * Read-ahead the block, don't wait for it, don't return a buffer.
738 * Long-form addressing.
742 xfs_btree_reada_bufl(
743 struct xfs_mount
*mp
, /* file system mount point */
744 xfs_fsblock_t fsbno
, /* file system block number */
745 xfs_extlen_t count
, /* count of filesystem blocks */
746 const struct xfs_buf_ops
*ops
)
750 ASSERT(fsbno
!= NULLFSBLOCK
);
751 d
= XFS_FSB_TO_DADDR(mp
, fsbno
);
752 xfs_buf_readahead(mp
->m_ddev_targp
, d
, mp
->m_bsize
* count
, ops
);
756 * Read-ahead the block, don't wait for it, don't return a buffer.
757 * Short-form addressing.
761 xfs_btree_reada_bufs(
762 struct xfs_mount
*mp
, /* file system mount point */
763 xfs_agnumber_t agno
, /* allocation group number */
764 xfs_agblock_t agbno
, /* allocation group block number */
765 xfs_extlen_t count
, /* count of filesystem blocks */
766 const struct xfs_buf_ops
*ops
)
770 ASSERT(agno
!= NULLAGNUMBER
);
771 ASSERT(agbno
!= NULLAGBLOCK
);
772 d
= XFS_AGB_TO_DADDR(mp
, agno
, agbno
);
773 xfs_buf_readahead(mp
->m_ddev_targp
, d
, mp
->m_bsize
* count
, ops
);
777 xfs_btree_readahead_lblock(
778 struct xfs_btree_cur
*cur
,
780 struct xfs_btree_block
*block
)
783 xfs_dfsbno_t left
= be64_to_cpu(block
->bb_u
.l
.bb_leftsib
);
784 xfs_dfsbno_t right
= be64_to_cpu(block
->bb_u
.l
.bb_rightsib
);
786 if ((lr
& XFS_BTCUR_LEFTRA
) && left
!= NULLDFSBNO
) {
787 xfs_btree_reada_bufl(cur
->bc_mp
, left
, 1,
788 cur
->bc_ops
->buf_ops
);
792 if ((lr
& XFS_BTCUR_RIGHTRA
) && right
!= NULLDFSBNO
) {
793 xfs_btree_reada_bufl(cur
->bc_mp
, right
, 1,
794 cur
->bc_ops
->buf_ops
);
802 xfs_btree_readahead_sblock(
803 struct xfs_btree_cur
*cur
,
805 struct xfs_btree_block
*block
)
808 xfs_agblock_t left
= be32_to_cpu(block
->bb_u
.s
.bb_leftsib
);
809 xfs_agblock_t right
= be32_to_cpu(block
->bb_u
.s
.bb_rightsib
);
812 if ((lr
& XFS_BTCUR_LEFTRA
) && left
!= NULLAGBLOCK
) {
813 xfs_btree_reada_bufs(cur
->bc_mp
, cur
->bc_private
.a
.agno
,
814 left
, 1, cur
->bc_ops
->buf_ops
);
818 if ((lr
& XFS_BTCUR_RIGHTRA
) && right
!= NULLAGBLOCK
) {
819 xfs_btree_reada_bufs(cur
->bc_mp
, cur
->bc_private
.a
.agno
,
820 right
, 1, cur
->bc_ops
->buf_ops
);
828 * Read-ahead btree blocks, at the given level.
829 * Bits in lr are set from XFS_BTCUR_{LEFT,RIGHT}RA.
833 struct xfs_btree_cur
*cur
, /* btree cursor */
834 int lev
, /* level in btree */
835 int lr
) /* left/right bits */
837 struct xfs_btree_block
*block
;
840 * No readahead needed if we are at the root level and the
841 * btree root is stored in the inode.
843 if ((cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
) &&
844 (lev
== cur
->bc_nlevels
- 1))
847 if ((cur
->bc_ra
[lev
] | lr
) == cur
->bc_ra
[lev
])
850 cur
->bc_ra
[lev
] |= lr
;
851 block
= XFS_BUF_TO_BLOCK(cur
->bc_bufs
[lev
]);
853 if (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
)
854 return xfs_btree_readahead_lblock(cur
, lr
, block
);
855 return xfs_btree_readahead_sblock(cur
, lr
, block
);
859 * Set the buffer for level "lev" in the cursor to bp, releasing
860 * any previous buffer.
864 xfs_btree_cur_t
*cur
, /* btree cursor */
865 int lev
, /* level in btree */
866 xfs_buf_t
*bp
) /* new buffer to set */
868 struct xfs_btree_block
*b
; /* btree block */
870 if (cur
->bc_bufs
[lev
])
871 xfs_trans_brelse(cur
->bc_tp
, cur
->bc_bufs
[lev
]);
872 cur
->bc_bufs
[lev
] = bp
;
875 b
= XFS_BUF_TO_BLOCK(bp
);
876 if (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
) {
877 if (b
->bb_u
.l
.bb_leftsib
== cpu_to_be64(NULLDFSBNO
))
878 cur
->bc_ra
[lev
] |= XFS_BTCUR_LEFTRA
;
879 if (b
->bb_u
.l
.bb_rightsib
== cpu_to_be64(NULLDFSBNO
))
880 cur
->bc_ra
[lev
] |= XFS_BTCUR_RIGHTRA
;
882 if (b
->bb_u
.s
.bb_leftsib
== cpu_to_be32(NULLAGBLOCK
))
883 cur
->bc_ra
[lev
] |= XFS_BTCUR_LEFTRA
;
884 if (b
->bb_u
.s
.bb_rightsib
== cpu_to_be32(NULLAGBLOCK
))
885 cur
->bc_ra
[lev
] |= XFS_BTCUR_RIGHTRA
;
890 xfs_btree_ptr_is_null(
891 struct xfs_btree_cur
*cur
,
892 union xfs_btree_ptr
*ptr
)
894 if (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
)
895 return ptr
->l
== cpu_to_be64(NULLDFSBNO
);
897 return ptr
->s
== cpu_to_be32(NULLAGBLOCK
);
901 xfs_btree_set_ptr_null(
902 struct xfs_btree_cur
*cur
,
903 union xfs_btree_ptr
*ptr
)
905 if (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
)
906 ptr
->l
= cpu_to_be64(NULLDFSBNO
);
908 ptr
->s
= cpu_to_be32(NULLAGBLOCK
);
912 * Get/set/init sibling pointers
915 xfs_btree_get_sibling(
916 struct xfs_btree_cur
*cur
,
917 struct xfs_btree_block
*block
,
918 union xfs_btree_ptr
*ptr
,
921 ASSERT(lr
== XFS_BB_LEFTSIB
|| lr
== XFS_BB_RIGHTSIB
);
923 if (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
) {
924 if (lr
== XFS_BB_RIGHTSIB
)
925 ptr
->l
= block
->bb_u
.l
.bb_rightsib
;
927 ptr
->l
= block
->bb_u
.l
.bb_leftsib
;
929 if (lr
== XFS_BB_RIGHTSIB
)
930 ptr
->s
= block
->bb_u
.s
.bb_rightsib
;
932 ptr
->s
= block
->bb_u
.s
.bb_leftsib
;
937 xfs_btree_set_sibling(
938 struct xfs_btree_cur
*cur
,
939 struct xfs_btree_block
*block
,
940 union xfs_btree_ptr
*ptr
,
943 ASSERT(lr
== XFS_BB_LEFTSIB
|| lr
== XFS_BB_RIGHTSIB
);
945 if (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
) {
946 if (lr
== XFS_BB_RIGHTSIB
)
947 block
->bb_u
.l
.bb_rightsib
= ptr
->l
;
949 block
->bb_u
.l
.bb_leftsib
= ptr
->l
;
951 if (lr
== XFS_BB_RIGHTSIB
)
952 block
->bb_u
.s
.bb_rightsib
= ptr
->s
;
954 block
->bb_u
.s
.bb_leftsib
= ptr
->s
;
959 xfs_btree_init_block_int(
960 struct xfs_mount
*mp
,
961 struct xfs_btree_block
*buf
,
969 buf
->bb_magic
= cpu_to_be32(magic
);
970 buf
->bb_level
= cpu_to_be16(level
);
971 buf
->bb_numrecs
= cpu_to_be16(numrecs
);
973 if (flags
& XFS_BTREE_LONG_PTRS
) {
974 buf
->bb_u
.l
.bb_leftsib
= cpu_to_be64(NULLDFSBNO
);
975 buf
->bb_u
.l
.bb_rightsib
= cpu_to_be64(NULLDFSBNO
);
976 if (flags
& XFS_BTREE_CRC_BLOCKS
) {
977 buf
->bb_u
.l
.bb_blkno
= cpu_to_be64(blkno
);
978 buf
->bb_u
.l
.bb_owner
= cpu_to_be64(owner
);
979 uuid_copy(&buf
->bb_u
.l
.bb_uuid
, &mp
->m_sb
.sb_uuid
);
980 buf
->bb_u
.l
.bb_pad
= 0;
983 /* owner is a 32 bit value on short blocks */
984 __u32 __owner
= (__u32
)owner
;
986 buf
->bb_u
.s
.bb_leftsib
= cpu_to_be32(NULLAGBLOCK
);
987 buf
->bb_u
.s
.bb_rightsib
= cpu_to_be32(NULLAGBLOCK
);
988 if (flags
& XFS_BTREE_CRC_BLOCKS
) {
989 buf
->bb_u
.s
.bb_blkno
= cpu_to_be64(blkno
);
990 buf
->bb_u
.s
.bb_owner
= cpu_to_be32(__owner
);
991 uuid_copy(&buf
->bb_u
.s
.bb_uuid
, &mp
->m_sb
.sb_uuid
);
997 xfs_btree_init_block(
998 struct xfs_mount
*mp
,
1006 xfs_btree_init_block_int(mp
, XFS_BUF_TO_BLOCK(bp
), bp
->b_bn
,
1007 magic
, level
, numrecs
, owner
, flags
);
1011 xfs_btree_init_block_cur(
1012 struct xfs_btree_cur
*cur
,
1020 * we can pull the owner from the cursor right now as the different
1021 * owners align directly with the pointer size of the btree. This may
1022 * change in future, but is safe for current users of the generic btree
1025 if (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
)
1026 owner
= cur
->bc_private
.b
.ip
->i_ino
;
1028 owner
= cur
->bc_private
.a
.agno
;
1030 xfs_btree_init_block_int(cur
->bc_mp
, XFS_BUF_TO_BLOCK(bp
), bp
->b_bn
,
1031 xfs_btree_magic(cur
), level
, numrecs
,
1032 owner
, cur
->bc_flags
);
1036 * Return true if ptr is the last record in the btree and
1037 * we need to track updates to this record. The decision
1038 * will be further refined in the update_lastrec method.
1041 xfs_btree_is_lastrec(
1042 struct xfs_btree_cur
*cur
,
1043 struct xfs_btree_block
*block
,
1046 union xfs_btree_ptr ptr
;
1050 if (!(cur
->bc_flags
& XFS_BTREE_LASTREC_UPDATE
))
1053 xfs_btree_get_sibling(cur
, block
, &ptr
, XFS_BB_RIGHTSIB
);
1054 if (!xfs_btree_ptr_is_null(cur
, &ptr
))
1060 xfs_btree_buf_to_ptr(
1061 struct xfs_btree_cur
*cur
,
1063 union xfs_btree_ptr
*ptr
)
1065 if (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
)
1066 ptr
->l
= cpu_to_be64(XFS_DADDR_TO_FSB(cur
->bc_mp
,
1069 ptr
->s
= cpu_to_be32(xfs_daddr_to_agbno(cur
->bc_mp
,
1075 xfs_btree_ptr_to_daddr(
1076 struct xfs_btree_cur
*cur
,
1077 union xfs_btree_ptr
*ptr
)
1079 if (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
) {
1080 ASSERT(ptr
->l
!= cpu_to_be64(NULLDFSBNO
));
1082 return XFS_FSB_TO_DADDR(cur
->bc_mp
, be64_to_cpu(ptr
->l
));
1084 ASSERT(cur
->bc_private
.a
.agno
!= NULLAGNUMBER
);
1085 ASSERT(ptr
->s
!= cpu_to_be32(NULLAGBLOCK
));
1087 return XFS_AGB_TO_DADDR(cur
->bc_mp
, cur
->bc_private
.a
.agno
,
1088 be32_to_cpu(ptr
->s
));
1094 struct xfs_btree_cur
*cur
,
1097 switch (cur
->bc_btnum
) {
1100 xfs_buf_set_ref(bp
, XFS_ALLOC_BTREE_REF
);
1103 xfs_buf_set_ref(bp
, XFS_INO_BTREE_REF
);
1105 case XFS_BTNUM_BMAP
:
1106 xfs_buf_set_ref(bp
, XFS_BMAP_BTREE_REF
);
1114 xfs_btree_get_buf_block(
1115 struct xfs_btree_cur
*cur
,
1116 union xfs_btree_ptr
*ptr
,
1118 struct xfs_btree_block
**block
,
1119 struct xfs_buf
**bpp
)
1121 struct xfs_mount
*mp
= cur
->bc_mp
;
1124 /* need to sort out how callers deal with failures first */
1125 ASSERT(!(flags
& XBF_TRYLOCK
));
1127 d
= xfs_btree_ptr_to_daddr(cur
, ptr
);
1128 *bpp
= xfs_trans_get_buf(cur
->bc_tp
, mp
->m_ddev_targp
, d
,
1129 mp
->m_bsize
, flags
);
1134 (*bpp
)->b_ops
= cur
->bc_ops
->buf_ops
;
1135 *block
= XFS_BUF_TO_BLOCK(*bpp
);
1140 * Read in the buffer at the given ptr and return the buffer and
1141 * the block pointer within the buffer.
1144 xfs_btree_read_buf_block(
1145 struct xfs_btree_cur
*cur
,
1146 union xfs_btree_ptr
*ptr
,
1149 struct xfs_btree_block
**block
,
1150 struct xfs_buf
**bpp
)
1152 struct xfs_mount
*mp
= cur
->bc_mp
;
1156 /* need to sort out how callers deal with failures first */
1157 ASSERT(!(flags
& XBF_TRYLOCK
));
1159 d
= xfs_btree_ptr_to_daddr(cur
, ptr
);
1160 error
= xfs_trans_read_buf(mp
, cur
->bc_tp
, mp
->m_ddev_targp
, d
,
1161 mp
->m_bsize
, flags
, bpp
,
1162 cur
->bc_ops
->buf_ops
);
1166 ASSERT(!xfs_buf_geterror(*bpp
));
1167 xfs_btree_set_refs(cur
, *bpp
);
1168 *block
= XFS_BUF_TO_BLOCK(*bpp
);
1173 * Copy keys from one btree block to another.
1176 xfs_btree_copy_keys(
1177 struct xfs_btree_cur
*cur
,
1178 union xfs_btree_key
*dst_key
,
1179 union xfs_btree_key
*src_key
,
1182 ASSERT(numkeys
>= 0);
1183 memcpy(dst_key
, src_key
, numkeys
* cur
->bc_ops
->key_len
);
1187 * Copy records from one btree block to another.
1190 xfs_btree_copy_recs(
1191 struct xfs_btree_cur
*cur
,
1192 union xfs_btree_rec
*dst_rec
,
1193 union xfs_btree_rec
*src_rec
,
1196 ASSERT(numrecs
>= 0);
1197 memcpy(dst_rec
, src_rec
, numrecs
* cur
->bc_ops
->rec_len
);
1201 * Copy block pointers from one btree block to another.
1204 xfs_btree_copy_ptrs(
1205 struct xfs_btree_cur
*cur
,
1206 union xfs_btree_ptr
*dst_ptr
,
1207 union xfs_btree_ptr
*src_ptr
,
1210 ASSERT(numptrs
>= 0);
1211 memcpy(dst_ptr
, src_ptr
, numptrs
* xfs_btree_ptr_len(cur
));
1215 * Shift keys one index left/right inside a single btree block.
1218 xfs_btree_shift_keys(
1219 struct xfs_btree_cur
*cur
,
1220 union xfs_btree_key
*key
,
1226 ASSERT(numkeys
>= 0);
1227 ASSERT(dir
== 1 || dir
== -1);
1229 dst_key
= (char *)key
+ (dir
* cur
->bc_ops
->key_len
);
1230 memmove(dst_key
, key
, numkeys
* cur
->bc_ops
->key_len
);
1234 * Shift records one index left/right inside a single btree block.
1237 xfs_btree_shift_recs(
1238 struct xfs_btree_cur
*cur
,
1239 union xfs_btree_rec
*rec
,
1245 ASSERT(numrecs
>= 0);
1246 ASSERT(dir
== 1 || dir
== -1);
1248 dst_rec
= (char *)rec
+ (dir
* cur
->bc_ops
->rec_len
);
1249 memmove(dst_rec
, rec
, numrecs
* cur
->bc_ops
->rec_len
);
1253 * Shift block pointers one index left/right inside a single btree block.
1256 xfs_btree_shift_ptrs(
1257 struct xfs_btree_cur
*cur
,
1258 union xfs_btree_ptr
*ptr
,
1264 ASSERT(numptrs
>= 0);
1265 ASSERT(dir
== 1 || dir
== -1);
1267 dst_ptr
= (char *)ptr
+ (dir
* xfs_btree_ptr_len(cur
));
1268 memmove(dst_ptr
, ptr
, numptrs
* xfs_btree_ptr_len(cur
));
1272 * Log key values from the btree block.
1276 struct xfs_btree_cur
*cur
,
1281 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
1282 XFS_BTREE_TRACE_ARGBII(cur
, bp
, first
, last
);
1285 xfs_trans_buf_set_type(cur
->bc_tp
, bp
, XFS_BLFT_BTREE_BUF
);
1286 xfs_trans_log_buf(cur
->bc_tp
, bp
,
1287 xfs_btree_key_offset(cur
, first
),
1288 xfs_btree_key_offset(cur
, last
+ 1) - 1);
1290 xfs_trans_log_inode(cur
->bc_tp
, cur
->bc_private
.b
.ip
,
1291 xfs_ilog_fbroot(cur
->bc_private
.b
.whichfork
));
1294 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
1298 * Log record values from the btree block.
1302 struct xfs_btree_cur
*cur
,
1307 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
1308 XFS_BTREE_TRACE_ARGBII(cur
, bp
, first
, last
);
1310 xfs_trans_buf_set_type(cur
->bc_tp
, bp
, XFS_BLFT_BTREE_BUF
);
1311 xfs_trans_log_buf(cur
->bc_tp
, bp
,
1312 xfs_btree_rec_offset(cur
, first
),
1313 xfs_btree_rec_offset(cur
, last
+ 1) - 1);
1315 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
1319 * Log block pointer fields from a btree block (nonleaf).
1323 struct xfs_btree_cur
*cur
, /* btree cursor */
1324 struct xfs_buf
*bp
, /* buffer containing btree block */
1325 int first
, /* index of first pointer to log */
1326 int last
) /* index of last pointer to log */
1328 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
1329 XFS_BTREE_TRACE_ARGBII(cur
, bp
, first
, last
);
1332 struct xfs_btree_block
*block
= XFS_BUF_TO_BLOCK(bp
);
1333 int level
= xfs_btree_get_level(block
);
1335 xfs_trans_buf_set_type(cur
->bc_tp
, bp
, XFS_BLFT_BTREE_BUF
);
1336 xfs_trans_log_buf(cur
->bc_tp
, bp
,
1337 xfs_btree_ptr_offset(cur
, first
, level
),
1338 xfs_btree_ptr_offset(cur
, last
+ 1, level
) - 1);
1340 xfs_trans_log_inode(cur
->bc_tp
, cur
->bc_private
.b
.ip
,
1341 xfs_ilog_fbroot(cur
->bc_private
.b
.whichfork
));
1344 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
1348 * Log fields from a btree block header.
1351 xfs_btree_log_block(
1352 struct xfs_btree_cur
*cur
, /* btree cursor */
1353 struct xfs_buf
*bp
, /* buffer containing btree block */
1354 int fields
) /* mask of fields: XFS_BB_... */
1356 int first
; /* first byte offset logged */
1357 int last
; /* last byte offset logged */
1358 static const short soffsets
[] = { /* table of offsets (short) */
1359 offsetof(struct xfs_btree_block
, bb_magic
),
1360 offsetof(struct xfs_btree_block
, bb_level
),
1361 offsetof(struct xfs_btree_block
, bb_numrecs
),
1362 offsetof(struct xfs_btree_block
, bb_u
.s
.bb_leftsib
),
1363 offsetof(struct xfs_btree_block
, bb_u
.s
.bb_rightsib
),
1364 offsetof(struct xfs_btree_block
, bb_u
.s
.bb_blkno
),
1365 offsetof(struct xfs_btree_block
, bb_u
.s
.bb_lsn
),
1366 offsetof(struct xfs_btree_block
, bb_u
.s
.bb_uuid
),
1367 offsetof(struct xfs_btree_block
, bb_u
.s
.bb_owner
),
1368 offsetof(struct xfs_btree_block
, bb_u
.s
.bb_crc
),
1369 XFS_BTREE_SBLOCK_CRC_LEN
1371 static const short loffsets
[] = { /* table of offsets (long) */
1372 offsetof(struct xfs_btree_block
, bb_magic
),
1373 offsetof(struct xfs_btree_block
, bb_level
),
1374 offsetof(struct xfs_btree_block
, bb_numrecs
),
1375 offsetof(struct xfs_btree_block
, bb_u
.l
.bb_leftsib
),
1376 offsetof(struct xfs_btree_block
, bb_u
.l
.bb_rightsib
),
1377 offsetof(struct xfs_btree_block
, bb_u
.l
.bb_blkno
),
1378 offsetof(struct xfs_btree_block
, bb_u
.l
.bb_lsn
),
1379 offsetof(struct xfs_btree_block
, bb_u
.l
.bb_uuid
),
1380 offsetof(struct xfs_btree_block
, bb_u
.l
.bb_owner
),
1381 offsetof(struct xfs_btree_block
, bb_u
.l
.bb_crc
),
1382 offsetof(struct xfs_btree_block
, bb_u
.l
.bb_pad
),
1383 XFS_BTREE_LBLOCK_CRC_LEN
1386 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
1387 XFS_BTREE_TRACE_ARGBI(cur
, bp
, fields
);
1392 if (cur
->bc_flags
& XFS_BTREE_CRC_BLOCKS
) {
1394 * We don't log the CRC when updating a btree
1395 * block but instead recreate it during log
1396 * recovery. As the log buffers have checksums
1397 * of their own this is safe and avoids logging a crc
1398 * update in a lot of places.
1400 if (fields
== XFS_BB_ALL_BITS
)
1401 fields
= XFS_BB_ALL_BITS_CRC
;
1402 nbits
= XFS_BB_NUM_BITS_CRC
;
1404 nbits
= XFS_BB_NUM_BITS
;
1406 xfs_btree_offsets(fields
,
1407 (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
) ?
1408 loffsets
: soffsets
,
1409 nbits
, &first
, &last
);
1410 xfs_trans_buf_set_type(cur
->bc_tp
, bp
, XFS_BLFT_BTREE_BUF
);
1411 xfs_trans_log_buf(cur
->bc_tp
, bp
, first
, last
);
1413 xfs_trans_log_inode(cur
->bc_tp
, cur
->bc_private
.b
.ip
,
1414 xfs_ilog_fbroot(cur
->bc_private
.b
.whichfork
));
1417 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
1421 * Increment cursor by one record at the level.
1422 * For nonzero levels the leaf-ward information is untouched.
1425 xfs_btree_increment(
1426 struct xfs_btree_cur
*cur
,
1428 int *stat
) /* success/failure */
1430 struct xfs_btree_block
*block
;
1431 union xfs_btree_ptr ptr
;
1433 int error
; /* error return value */
1436 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
1437 XFS_BTREE_TRACE_ARGI(cur
, level
);
1439 ASSERT(level
< cur
->bc_nlevels
);
1441 /* Read-ahead to the right at this level. */
1442 xfs_btree_readahead(cur
, level
, XFS_BTCUR_RIGHTRA
);
1444 /* Get a pointer to the btree block. */
1445 block
= xfs_btree_get_block(cur
, level
, &bp
);
1448 error
= xfs_btree_check_block(cur
, block
, level
, bp
);
1453 /* We're done if we remain in the block after the increment. */
1454 if (++cur
->bc_ptrs
[level
] <= xfs_btree_get_numrecs(block
))
1457 /* Fail if we just went off the right edge of the tree. */
1458 xfs_btree_get_sibling(cur
, block
, &ptr
, XFS_BB_RIGHTSIB
);
1459 if (xfs_btree_ptr_is_null(cur
, &ptr
))
1462 XFS_BTREE_STATS_INC(cur
, increment
);
1465 * March up the tree incrementing pointers.
1466 * Stop when we don't go off the right edge of a block.
1468 for (lev
= level
+ 1; lev
< cur
->bc_nlevels
; lev
++) {
1469 block
= xfs_btree_get_block(cur
, lev
, &bp
);
1472 error
= xfs_btree_check_block(cur
, block
, lev
, bp
);
1477 if (++cur
->bc_ptrs
[lev
] <= xfs_btree_get_numrecs(block
))
1480 /* Read-ahead the right block for the next loop. */
1481 xfs_btree_readahead(cur
, lev
, XFS_BTCUR_RIGHTRA
);
1485 * If we went off the root then we are either seriously
1486 * confused or have the tree root in an inode.
1488 if (lev
== cur
->bc_nlevels
) {
1489 if (cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
)
1492 error
= EFSCORRUPTED
;
1495 ASSERT(lev
< cur
->bc_nlevels
);
1498 * Now walk back down the tree, fixing up the cursor's buffer
1499 * pointers and key numbers.
1501 for (block
= xfs_btree_get_block(cur
, lev
, &bp
); lev
> level
; ) {
1502 union xfs_btree_ptr
*ptrp
;
1504 ptrp
= xfs_btree_ptr_addr(cur
, cur
->bc_ptrs
[lev
], block
);
1505 error
= xfs_btree_read_buf_block(cur
, ptrp
, --lev
,
1510 xfs_btree_setbuf(cur
, lev
, bp
);
1511 cur
->bc_ptrs
[lev
] = 1;
1514 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
1519 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
1524 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
1529 * Decrement cursor by one record at the level.
1530 * For nonzero levels the leaf-ward information is untouched.
1533 xfs_btree_decrement(
1534 struct xfs_btree_cur
*cur
,
1536 int *stat
) /* success/failure */
1538 struct xfs_btree_block
*block
;
1540 int error
; /* error return value */
1542 union xfs_btree_ptr ptr
;
1544 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
1545 XFS_BTREE_TRACE_ARGI(cur
, level
);
1547 ASSERT(level
< cur
->bc_nlevels
);
1549 /* Read-ahead to the left at this level. */
1550 xfs_btree_readahead(cur
, level
, XFS_BTCUR_LEFTRA
);
1552 /* We're done if we remain in the block after the decrement. */
1553 if (--cur
->bc_ptrs
[level
] > 0)
1556 /* Get a pointer to the btree block. */
1557 block
= xfs_btree_get_block(cur
, level
, &bp
);
1560 error
= xfs_btree_check_block(cur
, block
, level
, bp
);
1565 /* Fail if we just went off the left edge of the tree. */
1566 xfs_btree_get_sibling(cur
, block
, &ptr
, XFS_BB_LEFTSIB
);
1567 if (xfs_btree_ptr_is_null(cur
, &ptr
))
1570 XFS_BTREE_STATS_INC(cur
, decrement
);
1573 * March up the tree decrementing pointers.
1574 * Stop when we don't go off the left edge of a block.
1576 for (lev
= level
+ 1; lev
< cur
->bc_nlevels
; lev
++) {
1577 if (--cur
->bc_ptrs
[lev
] > 0)
1579 /* Read-ahead the left block for the next loop. */
1580 xfs_btree_readahead(cur
, lev
, XFS_BTCUR_LEFTRA
);
1584 * If we went off the root then we are seriously confused.
1585 * or the root of the tree is in an inode.
1587 if (lev
== cur
->bc_nlevels
) {
1588 if (cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
)
1591 error
= EFSCORRUPTED
;
1594 ASSERT(lev
< cur
->bc_nlevels
);
1597 * Now walk back down the tree, fixing up the cursor's buffer
1598 * pointers and key numbers.
1600 for (block
= xfs_btree_get_block(cur
, lev
, &bp
); lev
> level
; ) {
1601 union xfs_btree_ptr
*ptrp
;
1603 ptrp
= xfs_btree_ptr_addr(cur
, cur
->bc_ptrs
[lev
], block
);
1604 error
= xfs_btree_read_buf_block(cur
, ptrp
, --lev
,
1608 xfs_btree_setbuf(cur
, lev
, bp
);
1609 cur
->bc_ptrs
[lev
] = xfs_btree_get_numrecs(block
);
1612 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
1617 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
1622 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
1627 xfs_btree_lookup_get_block(
1628 struct xfs_btree_cur
*cur
, /* btree cursor */
1629 int level
, /* level in the btree */
1630 union xfs_btree_ptr
*pp
, /* ptr to btree block */
1631 struct xfs_btree_block
**blkp
) /* return btree block */
1633 struct xfs_buf
*bp
; /* buffer pointer for btree block */
1636 /* special case the root block if in an inode */
1637 if ((cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
) &&
1638 (level
== cur
->bc_nlevels
- 1)) {
1639 *blkp
= xfs_btree_get_iroot(cur
);
1644 * If the old buffer at this level for the disk address we are
1645 * looking for re-use it.
1647 * Otherwise throw it away and get a new one.
1649 bp
= cur
->bc_bufs
[level
];
1650 if (bp
&& XFS_BUF_ADDR(bp
) == xfs_btree_ptr_to_daddr(cur
, pp
)) {
1651 *blkp
= XFS_BUF_TO_BLOCK(bp
);
1655 error
= xfs_btree_read_buf_block(cur
, pp
, level
, 0, blkp
, &bp
);
1659 xfs_btree_setbuf(cur
, level
, bp
);
1664 * Get current search key. For level 0 we don't actually have a key
1665 * structure so we make one up from the record. For all other levels
1666 * we just return the right key.
1668 STATIC
union xfs_btree_key
*
1669 xfs_lookup_get_search_key(
1670 struct xfs_btree_cur
*cur
,
1673 struct xfs_btree_block
*block
,
1674 union xfs_btree_key
*kp
)
1677 cur
->bc_ops
->init_key_from_rec(kp
,
1678 xfs_btree_rec_addr(cur
, keyno
, block
));
1682 return xfs_btree_key_addr(cur
, keyno
, block
);
1686 * Lookup the record. The cursor is made to point to it, based on dir.
1687 * Return 0 if can't find any such record, 1 for success.
1691 struct xfs_btree_cur
*cur
, /* btree cursor */
1692 xfs_lookup_t dir
, /* <=, ==, or >= */
1693 int *stat
) /* success/failure */
1695 struct xfs_btree_block
*block
; /* current btree block */
1696 __int64_t diff
; /* difference for the current key */
1697 int error
; /* error return value */
1698 int keyno
; /* current key number */
1699 int level
; /* level in the btree */
1700 union xfs_btree_ptr
*pp
; /* ptr to btree block */
1701 union xfs_btree_ptr ptr
; /* ptr to btree block */
1703 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
1704 XFS_BTREE_TRACE_ARGI(cur
, dir
);
1706 XFS_BTREE_STATS_INC(cur
, lookup
);
1711 /* initialise start pointer from cursor */
1712 cur
->bc_ops
->init_ptr_from_cur(cur
, &ptr
);
1716 * Iterate over each level in the btree, starting at the root.
1717 * For each level above the leaves, find the key we need, based
1718 * on the lookup record, then follow the corresponding block
1719 * pointer down to the next level.
1721 for (level
= cur
->bc_nlevels
- 1, diff
= 1; level
>= 0; level
--) {
1722 /* Get the block we need to do the lookup on. */
1723 error
= xfs_btree_lookup_get_block(cur
, level
, pp
, &block
);
1729 * If we already had a key match at a higher level, we
1730 * know we need to use the first entry in this block.
1734 /* Otherwise search this block. Do a binary search. */
1736 int high
; /* high entry number */
1737 int low
; /* low entry number */
1739 /* Set low and high entry numbers, 1-based. */
1741 high
= xfs_btree_get_numrecs(block
);
1743 /* Block is empty, must be an empty leaf. */
1744 ASSERT(level
== 0 && cur
->bc_nlevels
== 1);
1746 cur
->bc_ptrs
[0] = dir
!= XFS_LOOKUP_LE
;
1747 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
1752 /* Binary search the block. */
1753 while (low
<= high
) {
1754 union xfs_btree_key key
;
1755 union xfs_btree_key
*kp
;
1757 XFS_BTREE_STATS_INC(cur
, compare
);
1759 /* keyno is average of low and high. */
1760 keyno
= (low
+ high
) >> 1;
1762 /* Get current search key */
1763 kp
= xfs_lookup_get_search_key(cur
, level
,
1764 keyno
, block
, &key
);
1767 * Compute difference to get next direction:
1768 * - less than, move right
1769 * - greater than, move left
1770 * - equal, we're done
1772 diff
= cur
->bc_ops
->key_diff(cur
, kp
);
1783 * If there are more levels, set up for the next level
1784 * by getting the block number and filling in the cursor.
1788 * If we moved left, need the previous key number,
1789 * unless there isn't one.
1791 if (diff
> 0 && --keyno
< 1)
1793 pp
= xfs_btree_ptr_addr(cur
, keyno
, block
);
1796 error
= xfs_btree_check_ptr(cur
, pp
, 0, level
);
1800 cur
->bc_ptrs
[level
] = keyno
;
1804 /* Done with the search. See if we need to adjust the results. */
1805 if (dir
!= XFS_LOOKUP_LE
&& diff
< 0) {
1808 * If ge search and we went off the end of the block, but it's
1809 * not the last block, we're in the wrong block.
1811 xfs_btree_get_sibling(cur
, block
, &ptr
, XFS_BB_RIGHTSIB
);
1812 if (dir
== XFS_LOOKUP_GE
&&
1813 keyno
> xfs_btree_get_numrecs(block
) &&
1814 !xfs_btree_ptr_is_null(cur
, &ptr
)) {
1817 cur
->bc_ptrs
[0] = keyno
;
1818 error
= xfs_btree_increment(cur
, 0, &i
);
1821 XFS_WANT_CORRUPTED_RETURN(i
== 1);
1822 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
1826 } else if (dir
== XFS_LOOKUP_LE
&& diff
> 0)
1828 cur
->bc_ptrs
[0] = keyno
;
1830 /* Return if we succeeded or not. */
1831 if (keyno
== 0 || keyno
> xfs_btree_get_numrecs(block
))
1833 else if (dir
!= XFS_LOOKUP_EQ
|| diff
== 0)
1837 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
1841 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
1846 * Update keys at all levels from here to the root along the cursor's path.
1850 struct xfs_btree_cur
*cur
,
1851 union xfs_btree_key
*keyp
,
1854 struct xfs_btree_block
*block
;
1856 union xfs_btree_key
*kp
;
1859 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
1860 XFS_BTREE_TRACE_ARGIK(cur
, level
, keyp
);
1862 ASSERT(!(cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
) || level
>= 1);
1865 * Go up the tree from this level toward the root.
1866 * At each level, update the key value to the value input.
1867 * Stop when we reach a level where the cursor isn't pointing
1868 * at the first entry in the block.
1870 for (ptr
= 1; ptr
== 1 && level
< cur
->bc_nlevels
; level
++) {
1874 block
= xfs_btree_get_block(cur
, level
, &bp
);
1876 error
= xfs_btree_check_block(cur
, block
, level
, bp
);
1878 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
1882 ptr
= cur
->bc_ptrs
[level
];
1883 kp
= xfs_btree_key_addr(cur
, ptr
, block
);
1884 xfs_btree_copy_keys(cur
, kp
, keyp
, 1);
1885 xfs_btree_log_keys(cur
, bp
, ptr
, ptr
);
1888 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
1893 * Update the record referred to by cur to the value in the
1894 * given record. This either works (return 0) or gets an
1895 * EFSCORRUPTED error.
1899 struct xfs_btree_cur
*cur
,
1900 union xfs_btree_rec
*rec
)
1902 struct xfs_btree_block
*block
;
1906 union xfs_btree_rec
*rp
;
1908 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
1909 XFS_BTREE_TRACE_ARGR(cur
, rec
);
1911 /* Pick up the current block. */
1912 block
= xfs_btree_get_block(cur
, 0, &bp
);
1915 error
= xfs_btree_check_block(cur
, block
, 0, bp
);
1919 /* Get the address of the rec to be updated. */
1920 ptr
= cur
->bc_ptrs
[0];
1921 rp
= xfs_btree_rec_addr(cur
, ptr
, block
);
1923 /* Fill in the new contents and log them. */
1924 xfs_btree_copy_recs(cur
, rp
, rec
, 1);
1925 xfs_btree_log_recs(cur
, bp
, ptr
, ptr
);
1928 * If we are tracking the last record in the tree and
1929 * we are at the far right edge of the tree, update it.
1931 if (xfs_btree_is_lastrec(cur
, block
, 0)) {
1932 cur
->bc_ops
->update_lastrec(cur
, block
, rec
,
1933 ptr
, LASTREC_UPDATE
);
1936 /* Updating first rec in leaf. Pass new key value up to our parent. */
1938 union xfs_btree_key key
;
1940 cur
->bc_ops
->init_key_from_rec(&key
, rec
);
1941 error
= xfs_btree_updkey(cur
, &key
, 1);
1946 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
1950 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
1955 * Move 1 record left from cur/level if possible.
1956 * Update cur to reflect the new path.
1958 STATIC
int /* error */
1960 struct xfs_btree_cur
*cur
,
1962 int *stat
) /* success/failure */
1964 union xfs_btree_key key
; /* btree key */
1965 struct xfs_buf
*lbp
; /* left buffer pointer */
1966 struct xfs_btree_block
*left
; /* left btree block */
1967 int lrecs
; /* left record count */
1968 struct xfs_buf
*rbp
; /* right buffer pointer */
1969 struct xfs_btree_block
*right
; /* right btree block */
1970 int rrecs
; /* right record count */
1971 union xfs_btree_ptr lptr
; /* left btree pointer */
1972 union xfs_btree_key
*rkp
= NULL
; /* right btree key */
1973 union xfs_btree_ptr
*rpp
= NULL
; /* right address pointer */
1974 union xfs_btree_rec
*rrp
= NULL
; /* right record pointer */
1975 int error
; /* error return value */
1977 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
1978 XFS_BTREE_TRACE_ARGI(cur
, level
);
1980 if ((cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
) &&
1981 level
== cur
->bc_nlevels
- 1)
1984 /* Set up variables for this block as "right". */
1985 right
= xfs_btree_get_block(cur
, level
, &rbp
);
1988 error
= xfs_btree_check_block(cur
, right
, level
, rbp
);
1993 /* If we've got no left sibling then we can't shift an entry left. */
1994 xfs_btree_get_sibling(cur
, right
, &lptr
, XFS_BB_LEFTSIB
);
1995 if (xfs_btree_ptr_is_null(cur
, &lptr
))
1999 * If the cursor entry is the one that would be moved, don't
2000 * do it... it's too complicated.
2002 if (cur
->bc_ptrs
[level
] <= 1)
2005 /* Set up the left neighbor as "left". */
2006 error
= xfs_btree_read_buf_block(cur
, &lptr
, level
, 0, &left
, &lbp
);
2010 /* If it's full, it can't take another entry. */
2011 lrecs
= xfs_btree_get_numrecs(left
);
2012 if (lrecs
== cur
->bc_ops
->get_maxrecs(cur
, level
))
2015 rrecs
= xfs_btree_get_numrecs(right
);
2018 * We add one entry to the left side and remove one for the right side.
2019 * Account for it here, the changes will be updated on disk and logged
2025 XFS_BTREE_STATS_INC(cur
, lshift
);
2026 XFS_BTREE_STATS_ADD(cur
, moves
, 1);
2029 * If non-leaf, copy a key and a ptr to the left block.
2030 * Log the changes to the left block.
2033 /* It's a non-leaf. Move keys and pointers. */
2034 union xfs_btree_key
*lkp
; /* left btree key */
2035 union xfs_btree_ptr
*lpp
; /* left address pointer */
2037 lkp
= xfs_btree_key_addr(cur
, lrecs
, left
);
2038 rkp
= xfs_btree_key_addr(cur
, 1, right
);
2040 lpp
= xfs_btree_ptr_addr(cur
, lrecs
, left
);
2041 rpp
= xfs_btree_ptr_addr(cur
, 1, right
);
2043 error
= xfs_btree_check_ptr(cur
, rpp
, 0, level
);
2047 xfs_btree_copy_keys(cur
, lkp
, rkp
, 1);
2048 xfs_btree_copy_ptrs(cur
, lpp
, rpp
, 1);
2050 xfs_btree_log_keys(cur
, lbp
, lrecs
, lrecs
);
2051 xfs_btree_log_ptrs(cur
, lbp
, lrecs
, lrecs
);
2053 ASSERT(cur
->bc_ops
->keys_inorder(cur
,
2054 xfs_btree_key_addr(cur
, lrecs
- 1, left
), lkp
));
2056 /* It's a leaf. Move records. */
2057 union xfs_btree_rec
*lrp
; /* left record pointer */
2059 lrp
= xfs_btree_rec_addr(cur
, lrecs
, left
);
2060 rrp
= xfs_btree_rec_addr(cur
, 1, right
);
2062 xfs_btree_copy_recs(cur
, lrp
, rrp
, 1);
2063 xfs_btree_log_recs(cur
, lbp
, lrecs
, lrecs
);
2065 ASSERT(cur
->bc_ops
->recs_inorder(cur
,
2066 xfs_btree_rec_addr(cur
, lrecs
- 1, left
), lrp
));
2069 xfs_btree_set_numrecs(left
, lrecs
);
2070 xfs_btree_log_block(cur
, lbp
, XFS_BB_NUMRECS
);
2072 xfs_btree_set_numrecs(right
, rrecs
);
2073 xfs_btree_log_block(cur
, rbp
, XFS_BB_NUMRECS
);
2076 * Slide the contents of right down one entry.
2078 XFS_BTREE_STATS_ADD(cur
, moves
, rrecs
- 1);
2080 /* It's a nonleaf. operate on keys and ptrs */
2082 int i
; /* loop index */
2084 for (i
= 0; i
< rrecs
; i
++) {
2085 error
= xfs_btree_check_ptr(cur
, rpp
, i
+ 1, level
);
2090 xfs_btree_shift_keys(cur
,
2091 xfs_btree_key_addr(cur
, 2, right
),
2093 xfs_btree_shift_ptrs(cur
,
2094 xfs_btree_ptr_addr(cur
, 2, right
),
2097 xfs_btree_log_keys(cur
, rbp
, 1, rrecs
);
2098 xfs_btree_log_ptrs(cur
, rbp
, 1, rrecs
);
2100 /* It's a leaf. operate on records */
2101 xfs_btree_shift_recs(cur
,
2102 xfs_btree_rec_addr(cur
, 2, right
),
2104 xfs_btree_log_recs(cur
, rbp
, 1, rrecs
);
2107 * If it's the first record in the block, we'll need a key
2108 * structure to pass up to the next level (updkey).
2110 cur
->bc_ops
->init_key_from_rec(&key
,
2111 xfs_btree_rec_addr(cur
, 1, right
));
2115 /* Update the parent key values of right. */
2116 error
= xfs_btree_updkey(cur
, rkp
, level
+ 1);
2120 /* Slide the cursor value left one. */
2121 cur
->bc_ptrs
[level
]--;
2123 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
2128 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
2133 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
2138 * Move 1 record right from cur/level if possible.
2139 * Update cur to reflect the new path.
2141 STATIC
int /* error */
2143 struct xfs_btree_cur
*cur
,
2145 int *stat
) /* success/failure */
2147 union xfs_btree_key key
; /* btree key */
2148 struct xfs_buf
*lbp
; /* left buffer pointer */
2149 struct xfs_btree_block
*left
; /* left btree block */
2150 struct xfs_buf
*rbp
; /* right buffer pointer */
2151 struct xfs_btree_block
*right
; /* right btree block */
2152 struct xfs_btree_cur
*tcur
; /* temporary btree cursor */
2153 union xfs_btree_ptr rptr
; /* right block pointer */
2154 union xfs_btree_key
*rkp
; /* right btree key */
2155 int rrecs
; /* right record count */
2156 int lrecs
; /* left record count */
2157 int error
; /* error return value */
2158 int i
; /* loop counter */
2160 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
2161 XFS_BTREE_TRACE_ARGI(cur
, level
);
2163 if ((cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
) &&
2164 (level
== cur
->bc_nlevels
- 1))
2167 /* Set up variables for this block as "left". */
2168 left
= xfs_btree_get_block(cur
, level
, &lbp
);
2171 error
= xfs_btree_check_block(cur
, left
, level
, lbp
);
2176 /* If we've got no right sibling then we can't shift an entry right. */
2177 xfs_btree_get_sibling(cur
, left
, &rptr
, XFS_BB_RIGHTSIB
);
2178 if (xfs_btree_ptr_is_null(cur
, &rptr
))
2182 * If the cursor entry is the one that would be moved, don't
2183 * do it... it's too complicated.
2185 lrecs
= xfs_btree_get_numrecs(left
);
2186 if (cur
->bc_ptrs
[level
] >= lrecs
)
2189 /* Set up the right neighbor as "right". */
2190 error
= xfs_btree_read_buf_block(cur
, &rptr
, level
, 0, &right
, &rbp
);
2194 /* If it's full, it can't take another entry. */
2195 rrecs
= xfs_btree_get_numrecs(right
);
2196 if (rrecs
== cur
->bc_ops
->get_maxrecs(cur
, level
))
2199 XFS_BTREE_STATS_INC(cur
, rshift
);
2200 XFS_BTREE_STATS_ADD(cur
, moves
, rrecs
);
2203 * Make a hole at the start of the right neighbor block, then
2204 * copy the last left block entry to the hole.
2207 /* It's a nonleaf. make a hole in the keys and ptrs */
2208 union xfs_btree_key
*lkp
;
2209 union xfs_btree_ptr
*lpp
;
2210 union xfs_btree_ptr
*rpp
;
2212 lkp
= xfs_btree_key_addr(cur
, lrecs
, left
);
2213 lpp
= xfs_btree_ptr_addr(cur
, lrecs
, left
);
2214 rkp
= xfs_btree_key_addr(cur
, 1, right
);
2215 rpp
= xfs_btree_ptr_addr(cur
, 1, right
);
2218 for (i
= rrecs
- 1; i
>= 0; i
--) {
2219 error
= xfs_btree_check_ptr(cur
, rpp
, i
, level
);
2225 xfs_btree_shift_keys(cur
, rkp
, 1, rrecs
);
2226 xfs_btree_shift_ptrs(cur
, rpp
, 1, rrecs
);
2229 error
= xfs_btree_check_ptr(cur
, lpp
, 0, level
);
2234 /* Now put the new data in, and log it. */
2235 xfs_btree_copy_keys(cur
, rkp
, lkp
, 1);
2236 xfs_btree_copy_ptrs(cur
, rpp
, lpp
, 1);
2238 xfs_btree_log_keys(cur
, rbp
, 1, rrecs
+ 1);
2239 xfs_btree_log_ptrs(cur
, rbp
, 1, rrecs
+ 1);
2241 ASSERT(cur
->bc_ops
->keys_inorder(cur
, rkp
,
2242 xfs_btree_key_addr(cur
, 2, right
)));
2244 /* It's a leaf. make a hole in the records */
2245 union xfs_btree_rec
*lrp
;
2246 union xfs_btree_rec
*rrp
;
2248 lrp
= xfs_btree_rec_addr(cur
, lrecs
, left
);
2249 rrp
= xfs_btree_rec_addr(cur
, 1, right
);
2251 xfs_btree_shift_recs(cur
, rrp
, 1, rrecs
);
2253 /* Now put the new data in, and log it. */
2254 xfs_btree_copy_recs(cur
, rrp
, lrp
, 1);
2255 xfs_btree_log_recs(cur
, rbp
, 1, rrecs
+ 1);
2257 cur
->bc_ops
->init_key_from_rec(&key
, rrp
);
2260 ASSERT(cur
->bc_ops
->recs_inorder(cur
, rrp
,
2261 xfs_btree_rec_addr(cur
, 2, right
)));
2265 * Decrement and log left's numrecs, bump and log right's numrecs.
2267 xfs_btree_set_numrecs(left
, --lrecs
);
2268 xfs_btree_log_block(cur
, lbp
, XFS_BB_NUMRECS
);
2270 xfs_btree_set_numrecs(right
, ++rrecs
);
2271 xfs_btree_log_block(cur
, rbp
, XFS_BB_NUMRECS
);
2274 * Using a temporary cursor, update the parent key values of the
2275 * block on the right.
2277 error
= xfs_btree_dup_cursor(cur
, &tcur
);
2280 i
= xfs_btree_lastrec(tcur
, level
);
2281 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
2283 error
= xfs_btree_increment(tcur
, level
, &i
);
2287 error
= xfs_btree_updkey(tcur
, rkp
, level
+ 1);
2291 xfs_btree_del_cursor(tcur
, XFS_BTREE_NOERROR
);
2293 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
2298 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
2303 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
2307 XFS_BTREE_TRACE_CURSOR(tcur
, XBT_ERROR
);
2308 xfs_btree_del_cursor(tcur
, XFS_BTREE_ERROR
);
2313 * Split cur/level block in half.
2314 * Return new block number and the key to its first
2315 * record (to be inserted into parent).
2317 STATIC
int /* error */
2319 struct xfs_btree_cur
*cur
,
2321 union xfs_btree_ptr
*ptrp
,
2322 union xfs_btree_key
*key
,
2323 struct xfs_btree_cur
**curp
,
2324 int *stat
) /* success/failure */
2326 union xfs_btree_ptr lptr
; /* left sibling block ptr */
2327 struct xfs_buf
*lbp
; /* left buffer pointer */
2328 struct xfs_btree_block
*left
; /* left btree block */
2329 union xfs_btree_ptr rptr
; /* right sibling block ptr */
2330 struct xfs_buf
*rbp
; /* right buffer pointer */
2331 struct xfs_btree_block
*right
; /* right btree block */
2332 union xfs_btree_ptr rrptr
; /* right-right sibling ptr */
2333 struct xfs_buf
*rrbp
; /* right-right buffer pointer */
2334 struct xfs_btree_block
*rrblock
; /* right-right btree block */
2338 int error
; /* error return value */
2343 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
2344 XFS_BTREE_TRACE_ARGIPK(cur
, level
, *ptrp
, key
);
2346 XFS_BTREE_STATS_INC(cur
, split
);
2348 /* Set up left block (current one). */
2349 left
= xfs_btree_get_block(cur
, level
, &lbp
);
2352 error
= xfs_btree_check_block(cur
, left
, level
, lbp
);
2357 xfs_btree_buf_to_ptr(cur
, lbp
, &lptr
);
2359 /* Allocate the new block. If we can't do it, we're toast. Give up. */
2360 error
= cur
->bc_ops
->alloc_block(cur
, &lptr
, &rptr
, 1, stat
);
2365 XFS_BTREE_STATS_INC(cur
, alloc
);
2367 /* Set up the new block as "right". */
2368 error
= xfs_btree_get_buf_block(cur
, &rptr
, 0, &right
, &rbp
);
2372 /* Fill in the btree header for the new right block. */
2373 xfs_btree_init_block_cur(cur
, rbp
, xfs_btree_get_level(left
), 0);
2376 * Split the entries between the old and the new block evenly.
2377 * Make sure that if there's an odd number of entries now, that
2378 * each new block will have the same number of entries.
2380 lrecs
= xfs_btree_get_numrecs(left
);
2382 if ((lrecs
& 1) && cur
->bc_ptrs
[level
] <= rrecs
+ 1)
2384 src_index
= (lrecs
- rrecs
+ 1);
2386 XFS_BTREE_STATS_ADD(cur
, moves
, rrecs
);
2389 * Copy btree block entries from the left block over to the
2390 * new block, the right. Update the right block and log the
2394 /* It's a non-leaf. Move keys and pointers. */
2395 union xfs_btree_key
*lkp
; /* left btree key */
2396 union xfs_btree_ptr
*lpp
; /* left address pointer */
2397 union xfs_btree_key
*rkp
; /* right btree key */
2398 union xfs_btree_ptr
*rpp
; /* right address pointer */
2400 lkp
= xfs_btree_key_addr(cur
, src_index
, left
);
2401 lpp
= xfs_btree_ptr_addr(cur
, src_index
, left
);
2402 rkp
= xfs_btree_key_addr(cur
, 1, right
);
2403 rpp
= xfs_btree_ptr_addr(cur
, 1, right
);
2406 for (i
= src_index
; i
< rrecs
; i
++) {
2407 error
= xfs_btree_check_ptr(cur
, lpp
, i
, level
);
2413 xfs_btree_copy_keys(cur
, rkp
, lkp
, rrecs
);
2414 xfs_btree_copy_ptrs(cur
, rpp
, lpp
, rrecs
);
2416 xfs_btree_log_keys(cur
, rbp
, 1, rrecs
);
2417 xfs_btree_log_ptrs(cur
, rbp
, 1, rrecs
);
2419 /* Grab the keys to the entries moved to the right block */
2420 xfs_btree_copy_keys(cur
, key
, rkp
, 1);
2422 /* It's a leaf. Move records. */
2423 union xfs_btree_rec
*lrp
; /* left record pointer */
2424 union xfs_btree_rec
*rrp
; /* right record pointer */
2426 lrp
= xfs_btree_rec_addr(cur
, src_index
, left
);
2427 rrp
= xfs_btree_rec_addr(cur
, 1, right
);
2429 xfs_btree_copy_recs(cur
, rrp
, lrp
, rrecs
);
2430 xfs_btree_log_recs(cur
, rbp
, 1, rrecs
);
2432 cur
->bc_ops
->init_key_from_rec(key
,
2433 xfs_btree_rec_addr(cur
, 1, right
));
2438 * Find the left block number by looking in the buffer.
2439 * Adjust numrecs, sibling pointers.
2441 xfs_btree_get_sibling(cur
, left
, &rrptr
, XFS_BB_RIGHTSIB
);
2442 xfs_btree_set_sibling(cur
, right
, &rrptr
, XFS_BB_RIGHTSIB
);
2443 xfs_btree_set_sibling(cur
, right
, &lptr
, XFS_BB_LEFTSIB
);
2444 xfs_btree_set_sibling(cur
, left
, &rptr
, XFS_BB_RIGHTSIB
);
2447 xfs_btree_set_numrecs(left
, lrecs
);
2448 xfs_btree_set_numrecs(right
, xfs_btree_get_numrecs(right
) + rrecs
);
2450 xfs_btree_log_block(cur
, rbp
, XFS_BB_ALL_BITS
);
2451 xfs_btree_log_block(cur
, lbp
, XFS_BB_NUMRECS
| XFS_BB_RIGHTSIB
);
2454 * If there's a block to the new block's right, make that block
2455 * point back to right instead of to left.
2457 if (!xfs_btree_ptr_is_null(cur
, &rrptr
)) {
2458 error
= xfs_btree_read_buf_block(cur
, &rrptr
, level
,
2459 0, &rrblock
, &rrbp
);
2462 xfs_btree_set_sibling(cur
, rrblock
, &rptr
, XFS_BB_LEFTSIB
);
2463 xfs_btree_log_block(cur
, rrbp
, XFS_BB_LEFTSIB
);
2466 * If the cursor is really in the right block, move it there.
2467 * If it's just pointing past the last entry in left, then we'll
2468 * insert there, so don't change anything in that case.
2470 if (cur
->bc_ptrs
[level
] > lrecs
+ 1) {
2471 xfs_btree_setbuf(cur
, level
, rbp
);
2472 cur
->bc_ptrs
[level
] -= lrecs
;
2475 * If there are more levels, we'll need another cursor which refers
2476 * the right block, no matter where this cursor was.
2478 if (level
+ 1 < cur
->bc_nlevels
) {
2479 error
= xfs_btree_dup_cursor(cur
, curp
);
2482 (*curp
)->bc_ptrs
[level
+ 1]++;
2485 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
2489 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
2494 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
2499 * Copy the old inode root contents into a real block and make the
2500 * broot point to it.
2503 xfs_btree_new_iroot(
2504 struct xfs_btree_cur
*cur
, /* btree cursor */
2505 int *logflags
, /* logging flags for inode */
2506 int *stat
) /* return status - 0 fail */
2508 struct xfs_buf
*cbp
; /* buffer for cblock */
2509 struct xfs_btree_block
*block
; /* btree block */
2510 struct xfs_btree_block
*cblock
; /* child btree block */
2511 union xfs_btree_key
*ckp
; /* child key pointer */
2512 union xfs_btree_ptr
*cpp
; /* child ptr pointer */
2513 union xfs_btree_key
*kp
; /* pointer to btree key */
2514 union xfs_btree_ptr
*pp
; /* pointer to block addr */
2515 union xfs_btree_ptr nptr
; /* new block addr */
2516 int level
; /* btree level */
2517 int error
; /* error return code */
2519 int i
; /* loop counter */
2522 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
2523 XFS_BTREE_STATS_INC(cur
, newroot
);
2525 ASSERT(cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
);
2527 level
= cur
->bc_nlevels
- 1;
2529 block
= xfs_btree_get_iroot(cur
);
2530 pp
= xfs_btree_ptr_addr(cur
, 1, block
);
2532 /* Allocate the new block. If we can't do it, we're toast. Give up. */
2533 error
= cur
->bc_ops
->alloc_block(cur
, pp
, &nptr
, 1, stat
);
2537 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
2540 XFS_BTREE_STATS_INC(cur
, alloc
);
2542 /* Copy the root into a real block. */
2543 error
= xfs_btree_get_buf_block(cur
, &nptr
, 0, &cblock
, &cbp
);
2548 * we can't just memcpy() the root in for CRC enabled btree blocks.
2549 * In that case have to also ensure the blkno remains correct
2551 memcpy(cblock
, block
, xfs_btree_block_len(cur
));
2552 if (cur
->bc_flags
& XFS_BTREE_CRC_BLOCKS
) {
2553 if (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
)
2554 cblock
->bb_u
.l
.bb_blkno
= cpu_to_be64(cbp
->b_bn
);
2556 cblock
->bb_u
.s
.bb_blkno
= cpu_to_be64(cbp
->b_bn
);
2559 be16_add_cpu(&block
->bb_level
, 1);
2560 xfs_btree_set_numrecs(block
, 1);
2562 cur
->bc_ptrs
[level
+ 1] = 1;
2564 kp
= xfs_btree_key_addr(cur
, 1, block
);
2565 ckp
= xfs_btree_key_addr(cur
, 1, cblock
);
2566 xfs_btree_copy_keys(cur
, ckp
, kp
, xfs_btree_get_numrecs(cblock
));
2568 cpp
= xfs_btree_ptr_addr(cur
, 1, cblock
);
2570 for (i
= 0; i
< be16_to_cpu(cblock
->bb_numrecs
); i
++) {
2571 error
= xfs_btree_check_ptr(cur
, pp
, i
, level
);
2576 xfs_btree_copy_ptrs(cur
, cpp
, pp
, xfs_btree_get_numrecs(cblock
));
2579 error
= xfs_btree_check_ptr(cur
, &nptr
, 0, level
);
2583 xfs_btree_copy_ptrs(cur
, pp
, &nptr
, 1);
2585 xfs_iroot_realloc(cur
->bc_private
.b
.ip
,
2586 1 - xfs_btree_get_numrecs(cblock
),
2587 cur
->bc_private
.b
.whichfork
);
2589 xfs_btree_setbuf(cur
, level
, cbp
);
2592 * Do all this logging at the end so that
2593 * the root is at the right level.
2595 xfs_btree_log_block(cur
, cbp
, XFS_BB_ALL_BITS
);
2596 xfs_btree_log_keys(cur
, cbp
, 1, be16_to_cpu(cblock
->bb_numrecs
));
2597 xfs_btree_log_ptrs(cur
, cbp
, 1, be16_to_cpu(cblock
->bb_numrecs
));
2600 XFS_ILOG_CORE
| xfs_ilog_fbroot(cur
->bc_private
.b
.whichfork
);
2602 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
2605 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
2610 * Allocate a new root block, fill it in.
2612 STATIC
int /* error */
2614 struct xfs_btree_cur
*cur
, /* btree cursor */
2615 int *stat
) /* success/failure */
2617 struct xfs_btree_block
*block
; /* one half of the old root block */
2618 struct xfs_buf
*bp
; /* buffer containing block */
2619 int error
; /* error return value */
2620 struct xfs_buf
*lbp
; /* left buffer pointer */
2621 struct xfs_btree_block
*left
; /* left btree block */
2622 struct xfs_buf
*nbp
; /* new (root) buffer */
2623 struct xfs_btree_block
*new; /* new (root) btree block */
2624 int nptr
; /* new value for key index, 1 or 2 */
2625 struct xfs_buf
*rbp
; /* right buffer pointer */
2626 struct xfs_btree_block
*right
; /* right btree block */
2627 union xfs_btree_ptr rptr
;
2628 union xfs_btree_ptr lptr
;
2630 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
2631 XFS_BTREE_STATS_INC(cur
, newroot
);
2633 /* initialise our start point from the cursor */
2634 cur
->bc_ops
->init_ptr_from_cur(cur
, &rptr
);
2636 /* Allocate the new block. If we can't do it, we're toast. Give up. */
2637 error
= cur
->bc_ops
->alloc_block(cur
, &rptr
, &lptr
, 1, stat
);
2642 XFS_BTREE_STATS_INC(cur
, alloc
);
2644 /* Set up the new block. */
2645 error
= xfs_btree_get_buf_block(cur
, &lptr
, 0, &new, &nbp
);
2649 /* Set the root in the holding structure increasing the level by 1. */
2650 cur
->bc_ops
->set_root(cur
, &lptr
, 1);
2653 * At the previous root level there are now two blocks: the old root,
2654 * and the new block generated when it was split. We don't know which
2655 * one the cursor is pointing at, so we set up variables "left" and
2656 * "right" for each case.
2658 block
= xfs_btree_get_block(cur
, cur
->bc_nlevels
- 1, &bp
);
2661 error
= xfs_btree_check_block(cur
, block
, cur
->bc_nlevels
- 1, bp
);
2666 xfs_btree_get_sibling(cur
, block
, &rptr
, XFS_BB_RIGHTSIB
);
2667 if (!xfs_btree_ptr_is_null(cur
, &rptr
)) {
2668 /* Our block is left, pick up the right block. */
2670 xfs_btree_buf_to_ptr(cur
, lbp
, &lptr
);
2672 error
= xfs_btree_read_buf_block(cur
, &rptr
,
2673 cur
->bc_nlevels
- 1, 0, &right
, &rbp
);
2679 /* Our block is right, pick up the left block. */
2681 xfs_btree_buf_to_ptr(cur
, rbp
, &rptr
);
2683 xfs_btree_get_sibling(cur
, right
, &lptr
, XFS_BB_LEFTSIB
);
2684 error
= xfs_btree_read_buf_block(cur
, &lptr
,
2685 cur
->bc_nlevels
- 1, 0, &left
, &lbp
);
2691 /* Fill in the new block's btree header and log it. */
2692 xfs_btree_init_block_cur(cur
, nbp
, cur
->bc_nlevels
, 2);
2693 xfs_btree_log_block(cur
, nbp
, XFS_BB_ALL_BITS
);
2694 ASSERT(!xfs_btree_ptr_is_null(cur
, &lptr
) &&
2695 !xfs_btree_ptr_is_null(cur
, &rptr
));
2697 /* Fill in the key data in the new root. */
2698 if (xfs_btree_get_level(left
) > 0) {
2699 xfs_btree_copy_keys(cur
,
2700 xfs_btree_key_addr(cur
, 1, new),
2701 xfs_btree_key_addr(cur
, 1, left
), 1);
2702 xfs_btree_copy_keys(cur
,
2703 xfs_btree_key_addr(cur
, 2, new),
2704 xfs_btree_key_addr(cur
, 1, right
), 1);
2706 cur
->bc_ops
->init_key_from_rec(
2707 xfs_btree_key_addr(cur
, 1, new),
2708 xfs_btree_rec_addr(cur
, 1, left
));
2709 cur
->bc_ops
->init_key_from_rec(
2710 xfs_btree_key_addr(cur
, 2, new),
2711 xfs_btree_rec_addr(cur
, 1, right
));
2713 xfs_btree_log_keys(cur
, nbp
, 1, 2);
2715 /* Fill in the pointer data in the new root. */
2716 xfs_btree_copy_ptrs(cur
,
2717 xfs_btree_ptr_addr(cur
, 1, new), &lptr
, 1);
2718 xfs_btree_copy_ptrs(cur
,
2719 xfs_btree_ptr_addr(cur
, 2, new), &rptr
, 1);
2720 xfs_btree_log_ptrs(cur
, nbp
, 1, 2);
2722 /* Fix up the cursor. */
2723 xfs_btree_setbuf(cur
, cur
->bc_nlevels
, nbp
);
2724 cur
->bc_ptrs
[cur
->bc_nlevels
] = nptr
;
2726 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
2730 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
2733 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
2739 xfs_btree_make_block_unfull(
2740 struct xfs_btree_cur
*cur
, /* btree cursor */
2741 int level
, /* btree level */
2742 int numrecs
,/* # of recs in block */
2743 int *oindex
,/* old tree index */
2744 int *index
, /* new tree index */
2745 union xfs_btree_ptr
*nptr
, /* new btree ptr */
2746 struct xfs_btree_cur
**ncur
, /* new btree cursor */
2747 union xfs_btree_rec
*nrec
, /* new record */
2750 union xfs_btree_key key
; /* new btree key value */
2753 if ((cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
) &&
2754 level
== cur
->bc_nlevels
- 1) {
2755 struct xfs_inode
*ip
= cur
->bc_private
.b
.ip
;
2757 if (numrecs
< cur
->bc_ops
->get_dmaxrecs(cur
, level
)) {
2758 /* A root block that can be made bigger. */
2760 xfs_iroot_realloc(ip
, 1, cur
->bc_private
.b
.whichfork
);
2762 /* A root block that needs replacing */
2765 error
= xfs_btree_new_iroot(cur
, &logflags
, stat
);
2766 if (error
|| *stat
== 0)
2769 xfs_trans_log_inode(cur
->bc_tp
, ip
, logflags
);
2775 /* First, try shifting an entry to the right neighbor. */
2776 error
= xfs_btree_rshift(cur
, level
, stat
);
2780 /* Next, try shifting an entry to the left neighbor. */
2781 error
= xfs_btree_lshift(cur
, level
, stat
);
2786 *oindex
= *index
= cur
->bc_ptrs
[level
];
2791 * Next, try splitting the current block in half.
2793 * If this works we have to re-set our variables because we
2794 * could be in a different block now.
2796 error
= xfs_btree_split(cur
, level
, nptr
, &key
, ncur
, stat
);
2797 if (error
|| *stat
== 0)
2801 *index
= cur
->bc_ptrs
[level
];
2802 cur
->bc_ops
->init_rec_from_key(&key
, nrec
);
2807 * Insert one record/level. Return information to the caller
2808 * allowing the next level up to proceed if necessary.
2812 struct xfs_btree_cur
*cur
, /* btree cursor */
2813 int level
, /* level to insert record at */
2814 union xfs_btree_ptr
*ptrp
, /* i/o: block number inserted */
2815 union xfs_btree_rec
*recp
, /* i/o: record data inserted */
2816 struct xfs_btree_cur
**curp
, /* output: new cursor replacing cur */
2817 int *stat
) /* success/failure */
2819 struct xfs_btree_block
*block
; /* btree block */
2820 struct xfs_buf
*bp
; /* buffer for block */
2821 union xfs_btree_key key
; /* btree key */
2822 union xfs_btree_ptr nptr
; /* new block ptr */
2823 struct xfs_btree_cur
*ncur
; /* new btree cursor */
2824 union xfs_btree_rec nrec
; /* new record count */
2825 int optr
; /* old key/record index */
2826 int ptr
; /* key/record index */
2827 int numrecs
;/* number of records */
2828 int error
; /* error return value */
2833 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
2834 XFS_BTREE_TRACE_ARGIPR(cur
, level
, *ptrp
, recp
);
2839 * If we have an external root pointer, and we've made it to the
2840 * root level, allocate a new root block and we're done.
2842 if (!(cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
) &&
2843 (level
>= cur
->bc_nlevels
)) {
2844 error
= xfs_btree_new_root(cur
, stat
);
2845 xfs_btree_set_ptr_null(cur
, ptrp
);
2847 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
2851 /* If we're off the left edge, return failure. */
2852 ptr
= cur
->bc_ptrs
[level
];
2854 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
2859 /* Make a key out of the record data to be inserted, and save it. */
2860 cur
->bc_ops
->init_key_from_rec(&key
, recp
);
2864 XFS_BTREE_STATS_INC(cur
, insrec
);
2866 /* Get pointers to the btree buffer and block. */
2867 block
= xfs_btree_get_block(cur
, level
, &bp
);
2868 numrecs
= xfs_btree_get_numrecs(block
);
2871 error
= xfs_btree_check_block(cur
, block
, level
, bp
);
2875 /* Check that the new entry is being inserted in the right place. */
2876 if (ptr
<= numrecs
) {
2878 ASSERT(cur
->bc_ops
->recs_inorder(cur
, recp
,
2879 xfs_btree_rec_addr(cur
, ptr
, block
)));
2881 ASSERT(cur
->bc_ops
->keys_inorder(cur
, &key
,
2882 xfs_btree_key_addr(cur
, ptr
, block
)));
2888 * If the block is full, we can't insert the new entry until we
2889 * make the block un-full.
2891 xfs_btree_set_ptr_null(cur
, &nptr
);
2892 if (numrecs
== cur
->bc_ops
->get_maxrecs(cur
, level
)) {
2893 error
= xfs_btree_make_block_unfull(cur
, level
, numrecs
,
2894 &optr
, &ptr
, &nptr
, &ncur
, &nrec
, stat
);
2895 if (error
|| *stat
== 0)
2900 * The current block may have changed if the block was
2901 * previously full and we have just made space in it.
2903 block
= xfs_btree_get_block(cur
, level
, &bp
);
2904 numrecs
= xfs_btree_get_numrecs(block
);
2907 error
= xfs_btree_check_block(cur
, block
, level
, bp
);
2913 * At this point we know there's room for our new entry in the block
2914 * we're pointing at.
2916 XFS_BTREE_STATS_ADD(cur
, moves
, numrecs
- ptr
+ 1);
2919 /* It's a nonleaf. make a hole in the keys and ptrs */
2920 union xfs_btree_key
*kp
;
2921 union xfs_btree_ptr
*pp
;
2923 kp
= xfs_btree_key_addr(cur
, ptr
, block
);
2924 pp
= xfs_btree_ptr_addr(cur
, ptr
, block
);
2927 for (i
= numrecs
- ptr
; i
>= 0; i
--) {
2928 error
= xfs_btree_check_ptr(cur
, pp
, i
, level
);
2934 xfs_btree_shift_keys(cur
, kp
, 1, numrecs
- ptr
+ 1);
2935 xfs_btree_shift_ptrs(cur
, pp
, 1, numrecs
- ptr
+ 1);
2938 error
= xfs_btree_check_ptr(cur
, ptrp
, 0, level
);
2943 /* Now put the new data in, bump numrecs and log it. */
2944 xfs_btree_copy_keys(cur
, kp
, &key
, 1);
2945 xfs_btree_copy_ptrs(cur
, pp
, ptrp
, 1);
2947 xfs_btree_set_numrecs(block
, numrecs
);
2948 xfs_btree_log_ptrs(cur
, bp
, ptr
, numrecs
);
2949 xfs_btree_log_keys(cur
, bp
, ptr
, numrecs
);
2951 if (ptr
< numrecs
) {
2952 ASSERT(cur
->bc_ops
->keys_inorder(cur
, kp
,
2953 xfs_btree_key_addr(cur
, ptr
+ 1, block
)));
2957 /* It's a leaf. make a hole in the records */
2958 union xfs_btree_rec
*rp
;
2960 rp
= xfs_btree_rec_addr(cur
, ptr
, block
);
2962 xfs_btree_shift_recs(cur
, rp
, 1, numrecs
- ptr
+ 1);
2964 /* Now put the new data in, bump numrecs and log it. */
2965 xfs_btree_copy_recs(cur
, rp
, recp
, 1);
2966 xfs_btree_set_numrecs(block
, ++numrecs
);
2967 xfs_btree_log_recs(cur
, bp
, ptr
, numrecs
);
2969 if (ptr
< numrecs
) {
2970 ASSERT(cur
->bc_ops
->recs_inorder(cur
, rp
,
2971 xfs_btree_rec_addr(cur
, ptr
+ 1, block
)));
2976 /* Log the new number of records in the btree header. */
2977 xfs_btree_log_block(cur
, bp
, XFS_BB_NUMRECS
);
2979 /* If we inserted at the start of a block, update the parents' keys. */
2981 error
= xfs_btree_updkey(cur
, &key
, level
+ 1);
2987 * If we are tracking the last record in the tree and
2988 * we are at the far right edge of the tree, update it.
2990 if (xfs_btree_is_lastrec(cur
, block
, level
)) {
2991 cur
->bc_ops
->update_lastrec(cur
, block
, recp
,
2992 ptr
, LASTREC_INSREC
);
2996 * Return the new block number, if any.
2997 * If there is one, give back a record value and a cursor too.
3000 if (!xfs_btree_ptr_is_null(cur
, &nptr
)) {
3005 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
3010 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
3015 * Insert the record at the point referenced by cur.
3017 * A multi-level split of the tree on insert will invalidate the original
3018 * cursor. All callers of this function should assume that the cursor is
3019 * no longer valid and revalidate it.
3023 struct xfs_btree_cur
*cur
,
3026 int error
; /* error return value */
3027 int i
; /* result value, 0 for failure */
3028 int level
; /* current level number in btree */
3029 union xfs_btree_ptr nptr
; /* new block number (split result) */
3030 struct xfs_btree_cur
*ncur
; /* new cursor (split result) */
3031 struct xfs_btree_cur
*pcur
; /* previous level's cursor */
3032 union xfs_btree_rec rec
; /* record to insert */
3038 xfs_btree_set_ptr_null(cur
, &nptr
);
3039 cur
->bc_ops
->init_rec_from_cur(cur
, &rec
);
3042 * Loop going up the tree, starting at the leaf level.
3043 * Stop when we don't get a split block, that must mean that
3044 * the insert is finished with this level.
3048 * Insert nrec/nptr into this level of the tree.
3049 * Note if we fail, nptr will be null.
3051 error
= xfs_btree_insrec(pcur
, level
, &nptr
, &rec
, &ncur
, &i
);
3054 xfs_btree_del_cursor(pcur
, XFS_BTREE_ERROR
);
3058 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
3062 * See if the cursor we just used is trash.
3063 * Can't trash the caller's cursor, but otherwise we should
3064 * if ncur is a new cursor or we're about to be done.
3067 (ncur
|| xfs_btree_ptr_is_null(cur
, &nptr
))) {
3068 /* Save the state from the cursor before we trash it */
3069 if (cur
->bc_ops
->update_cursor
)
3070 cur
->bc_ops
->update_cursor(pcur
, cur
);
3071 cur
->bc_nlevels
= pcur
->bc_nlevels
;
3072 xfs_btree_del_cursor(pcur
, XFS_BTREE_NOERROR
);
3074 /* If we got a new cursor, switch to it. */
3079 } while (!xfs_btree_ptr_is_null(cur
, &nptr
));
3081 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
3085 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
3090 * Try to merge a non-leaf block back into the inode root.
3092 * Note: the killroot names comes from the fact that we're effectively
3093 * killing the old root block. But because we can't just delete the
3094 * inode we have to copy the single block it was pointing to into the
3098 xfs_btree_kill_iroot(
3099 struct xfs_btree_cur
*cur
)
3101 int whichfork
= cur
->bc_private
.b
.whichfork
;
3102 struct xfs_inode
*ip
= cur
->bc_private
.b
.ip
;
3103 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
3104 struct xfs_btree_block
*block
;
3105 struct xfs_btree_block
*cblock
;
3106 union xfs_btree_key
*kp
;
3107 union xfs_btree_key
*ckp
;
3108 union xfs_btree_ptr
*pp
;
3109 union xfs_btree_ptr
*cpp
;
3110 struct xfs_buf
*cbp
;
3115 union xfs_btree_ptr ptr
;
3119 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
3121 ASSERT(cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
);
3122 ASSERT(cur
->bc_nlevels
> 1);
3125 * Don't deal with the root block needs to be a leaf case.
3126 * We're just going to turn the thing back into extents anyway.
3128 level
= cur
->bc_nlevels
- 1;
3133 * Give up if the root has multiple children.
3135 block
= xfs_btree_get_iroot(cur
);
3136 if (xfs_btree_get_numrecs(block
) != 1)
3139 cblock
= xfs_btree_get_block(cur
, level
- 1, &cbp
);
3140 numrecs
= xfs_btree_get_numrecs(cblock
);
3143 * Only do this if the next level will fit.
3144 * Then the data must be copied up to the inode,
3145 * instead of freeing the root you free the next level.
3147 if (numrecs
> cur
->bc_ops
->get_dmaxrecs(cur
, level
))
3150 XFS_BTREE_STATS_INC(cur
, killroot
);
3153 xfs_btree_get_sibling(cur
, block
, &ptr
, XFS_BB_LEFTSIB
);
3154 ASSERT(xfs_btree_ptr_is_null(cur
, &ptr
));
3155 xfs_btree_get_sibling(cur
, block
, &ptr
, XFS_BB_RIGHTSIB
);
3156 ASSERT(xfs_btree_ptr_is_null(cur
, &ptr
));
3159 index
= numrecs
- cur
->bc_ops
->get_maxrecs(cur
, level
);
3161 xfs_iroot_realloc(cur
->bc_private
.b
.ip
, index
,
3162 cur
->bc_private
.b
.whichfork
);
3163 block
= ifp
->if_broot
;
3166 be16_add_cpu(&block
->bb_numrecs
, index
);
3167 ASSERT(block
->bb_numrecs
== cblock
->bb_numrecs
);
3169 kp
= xfs_btree_key_addr(cur
, 1, block
);
3170 ckp
= xfs_btree_key_addr(cur
, 1, cblock
);
3171 xfs_btree_copy_keys(cur
, kp
, ckp
, numrecs
);
3173 pp
= xfs_btree_ptr_addr(cur
, 1, block
);
3174 cpp
= xfs_btree_ptr_addr(cur
, 1, cblock
);
3176 for (i
= 0; i
< numrecs
; i
++) {
3179 error
= xfs_btree_check_ptr(cur
, cpp
, i
, level
- 1);
3181 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
3186 xfs_btree_copy_ptrs(cur
, pp
, cpp
, numrecs
);
3188 cur
->bc_ops
->free_block(cur
, cbp
);
3189 XFS_BTREE_STATS_INC(cur
, free
);
3191 cur
->bc_bufs
[level
- 1] = NULL
;
3192 be16_add_cpu(&block
->bb_level
, -1);
3193 xfs_trans_log_inode(cur
->bc_tp
, ip
,
3194 XFS_ILOG_CORE
| xfs_ilog_fbroot(cur
->bc_private
.b
.whichfork
));
3197 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
3202 * Kill the current root node, and replace it with it's only child node.
3205 xfs_btree_kill_root(
3206 struct xfs_btree_cur
*cur
,
3209 union xfs_btree_ptr
*newroot
)
3213 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
3214 XFS_BTREE_STATS_INC(cur
, killroot
);
3217 * Update the root pointer, decreasing the level by 1 and then
3218 * free the old root.
3220 cur
->bc_ops
->set_root(cur
, newroot
, -1);
3222 error
= cur
->bc_ops
->free_block(cur
, bp
);
3224 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
3228 XFS_BTREE_STATS_INC(cur
, free
);
3230 cur
->bc_bufs
[level
] = NULL
;
3231 cur
->bc_ra
[level
] = 0;
3234 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
3239 xfs_btree_dec_cursor(
3240 struct xfs_btree_cur
*cur
,
3248 error
= xfs_btree_decrement(cur
, level
, &i
);
3253 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
3259 * Single level of the btree record deletion routine.
3260 * Delete record pointed to by cur/level.
3261 * Remove the record from its block then rebalance the tree.
3262 * Return 0 for error, 1 for done, 2 to go on to the next level.
3264 STATIC
int /* error */
3266 struct xfs_btree_cur
*cur
, /* btree cursor */
3267 int level
, /* level removing record from */
3268 int *stat
) /* fail/done/go-on */
3270 struct xfs_btree_block
*block
; /* btree block */
3271 union xfs_btree_ptr cptr
; /* current block ptr */
3272 struct xfs_buf
*bp
; /* buffer for block */
3273 int error
; /* error return value */
3274 int i
; /* loop counter */
3275 union xfs_btree_key key
; /* storage for keyp */
3276 union xfs_btree_key
*keyp
= &key
; /* passed to the next level */
3277 union xfs_btree_ptr lptr
; /* left sibling block ptr */
3278 struct xfs_buf
*lbp
; /* left buffer pointer */
3279 struct xfs_btree_block
*left
; /* left btree block */
3280 int lrecs
= 0; /* left record count */
3281 int ptr
; /* key/record index */
3282 union xfs_btree_ptr rptr
; /* right sibling block ptr */
3283 struct xfs_buf
*rbp
; /* right buffer pointer */
3284 struct xfs_btree_block
*right
; /* right btree block */
3285 struct xfs_btree_block
*rrblock
; /* right-right btree block */
3286 struct xfs_buf
*rrbp
; /* right-right buffer pointer */
3287 int rrecs
= 0; /* right record count */
3288 struct xfs_btree_cur
*tcur
; /* temporary btree cursor */
3289 int numrecs
; /* temporary numrec count */
3291 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
3292 XFS_BTREE_TRACE_ARGI(cur
, level
);
3296 /* Get the index of the entry being deleted, check for nothing there. */
3297 ptr
= cur
->bc_ptrs
[level
];
3299 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
3304 /* Get the buffer & block containing the record or key/ptr. */
3305 block
= xfs_btree_get_block(cur
, level
, &bp
);
3306 numrecs
= xfs_btree_get_numrecs(block
);
3309 error
= xfs_btree_check_block(cur
, block
, level
, bp
);
3314 /* Fail if we're off the end of the block. */
3315 if (ptr
> numrecs
) {
3316 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
3321 XFS_BTREE_STATS_INC(cur
, delrec
);
3322 XFS_BTREE_STATS_ADD(cur
, moves
, numrecs
- ptr
);
3324 /* Excise the entries being deleted. */
3326 /* It's a nonleaf. operate on keys and ptrs */
3327 union xfs_btree_key
*lkp
;
3328 union xfs_btree_ptr
*lpp
;
3330 lkp
= xfs_btree_key_addr(cur
, ptr
+ 1, block
);
3331 lpp
= xfs_btree_ptr_addr(cur
, ptr
+ 1, block
);
3334 for (i
= 0; i
< numrecs
- ptr
; i
++) {
3335 error
= xfs_btree_check_ptr(cur
, lpp
, i
, level
);
3341 if (ptr
< numrecs
) {
3342 xfs_btree_shift_keys(cur
, lkp
, -1, numrecs
- ptr
);
3343 xfs_btree_shift_ptrs(cur
, lpp
, -1, numrecs
- ptr
);
3344 xfs_btree_log_keys(cur
, bp
, ptr
, numrecs
- 1);
3345 xfs_btree_log_ptrs(cur
, bp
, ptr
, numrecs
- 1);
3349 * If it's the first record in the block, we'll need to pass a
3350 * key up to the next level (updkey).
3353 keyp
= xfs_btree_key_addr(cur
, 1, block
);
3355 /* It's a leaf. operate on records */
3356 if (ptr
< numrecs
) {
3357 xfs_btree_shift_recs(cur
,
3358 xfs_btree_rec_addr(cur
, ptr
+ 1, block
),
3360 xfs_btree_log_recs(cur
, bp
, ptr
, numrecs
- 1);
3364 * If it's the first record in the block, we'll need a key
3365 * structure to pass up to the next level (updkey).
3368 cur
->bc_ops
->init_key_from_rec(&key
,
3369 xfs_btree_rec_addr(cur
, 1, block
));
3375 * Decrement and log the number of entries in the block.
3377 xfs_btree_set_numrecs(block
, --numrecs
);
3378 xfs_btree_log_block(cur
, bp
, XFS_BB_NUMRECS
);
3381 * If we are tracking the last record in the tree and
3382 * we are at the far right edge of the tree, update it.
3384 if (xfs_btree_is_lastrec(cur
, block
, level
)) {
3385 cur
->bc_ops
->update_lastrec(cur
, block
, NULL
,
3386 ptr
, LASTREC_DELREC
);
3390 * We're at the root level. First, shrink the root block in-memory.
3391 * Try to get rid of the next level down. If we can't then there's
3392 * nothing left to do.
3394 if (level
== cur
->bc_nlevels
- 1) {
3395 if (cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
) {
3396 xfs_iroot_realloc(cur
->bc_private
.b
.ip
, -1,
3397 cur
->bc_private
.b
.whichfork
);
3399 error
= xfs_btree_kill_iroot(cur
);
3403 error
= xfs_btree_dec_cursor(cur
, level
, stat
);
3411 * If this is the root level, and there's only one entry left,
3412 * and it's NOT the leaf level, then we can get rid of this
3415 if (numrecs
== 1 && level
> 0) {
3416 union xfs_btree_ptr
*pp
;
3418 * pp is still set to the first pointer in the block.
3419 * Make it the new root of the btree.
3421 pp
= xfs_btree_ptr_addr(cur
, 1, block
);
3422 error
= xfs_btree_kill_root(cur
, bp
, level
, pp
);
3425 } else if (level
> 0) {
3426 error
= xfs_btree_dec_cursor(cur
, level
, stat
);
3435 * If we deleted the leftmost entry in the block, update the
3436 * key values above us in the tree.
3439 error
= xfs_btree_updkey(cur
, keyp
, level
+ 1);
3445 * If the number of records remaining in the block is at least
3446 * the minimum, we're done.
3448 if (numrecs
>= cur
->bc_ops
->get_minrecs(cur
, level
)) {
3449 error
= xfs_btree_dec_cursor(cur
, level
, stat
);
3456 * Otherwise, we have to move some records around to keep the
3457 * tree balanced. Look at the left and right sibling blocks to
3458 * see if we can re-balance by moving only one record.
3460 xfs_btree_get_sibling(cur
, block
, &rptr
, XFS_BB_RIGHTSIB
);
3461 xfs_btree_get_sibling(cur
, block
, &lptr
, XFS_BB_LEFTSIB
);
3463 if (cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
) {
3465 * One child of root, need to get a chance to copy its contents
3466 * into the root and delete it. Can't go up to next level,
3467 * there's nothing to delete there.
3469 if (xfs_btree_ptr_is_null(cur
, &rptr
) &&
3470 xfs_btree_ptr_is_null(cur
, &lptr
) &&
3471 level
== cur
->bc_nlevels
- 2) {
3472 error
= xfs_btree_kill_iroot(cur
);
3474 error
= xfs_btree_dec_cursor(cur
, level
, stat
);
3481 ASSERT(!xfs_btree_ptr_is_null(cur
, &rptr
) ||
3482 !xfs_btree_ptr_is_null(cur
, &lptr
));
3485 * Duplicate the cursor so our btree manipulations here won't
3486 * disrupt the next level up.
3488 error
= xfs_btree_dup_cursor(cur
, &tcur
);
3493 * If there's a right sibling, see if it's ok to shift an entry
3496 if (!xfs_btree_ptr_is_null(cur
, &rptr
)) {
3498 * Move the temp cursor to the last entry in the next block.
3499 * Actually any entry but the first would suffice.
3501 i
= xfs_btree_lastrec(tcur
, level
);
3502 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
3504 error
= xfs_btree_increment(tcur
, level
, &i
);
3507 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
3509 i
= xfs_btree_lastrec(tcur
, level
);
3510 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
3512 /* Grab a pointer to the block. */
3513 right
= xfs_btree_get_block(tcur
, level
, &rbp
);
3515 error
= xfs_btree_check_block(tcur
, right
, level
, rbp
);
3519 /* Grab the current block number, for future use. */
3520 xfs_btree_get_sibling(tcur
, right
, &cptr
, XFS_BB_LEFTSIB
);
3523 * If right block is full enough so that removing one entry
3524 * won't make it too empty, and left-shifting an entry out
3525 * of right to us works, we're done.
3527 if (xfs_btree_get_numrecs(right
) - 1 >=
3528 cur
->bc_ops
->get_minrecs(tcur
, level
)) {
3529 error
= xfs_btree_lshift(tcur
, level
, &i
);
3533 ASSERT(xfs_btree_get_numrecs(block
) >=
3534 cur
->bc_ops
->get_minrecs(tcur
, level
));
3536 xfs_btree_del_cursor(tcur
, XFS_BTREE_NOERROR
);
3539 error
= xfs_btree_dec_cursor(cur
, level
, stat
);
3547 * Otherwise, grab the number of records in right for
3548 * future reference, and fix up the temp cursor to point
3549 * to our block again (last record).
3551 rrecs
= xfs_btree_get_numrecs(right
);
3552 if (!xfs_btree_ptr_is_null(cur
, &lptr
)) {
3553 i
= xfs_btree_firstrec(tcur
, level
);
3554 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
3556 error
= xfs_btree_decrement(tcur
, level
, &i
);
3559 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
3564 * If there's a left sibling, see if it's ok to shift an entry
3567 if (!xfs_btree_ptr_is_null(cur
, &lptr
)) {
3569 * Move the temp cursor to the first entry in the
3572 i
= xfs_btree_firstrec(tcur
, level
);
3573 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
3575 error
= xfs_btree_decrement(tcur
, level
, &i
);
3578 i
= xfs_btree_firstrec(tcur
, level
);
3579 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
3581 /* Grab a pointer to the block. */
3582 left
= xfs_btree_get_block(tcur
, level
, &lbp
);
3584 error
= xfs_btree_check_block(cur
, left
, level
, lbp
);
3588 /* Grab the current block number, for future use. */
3589 xfs_btree_get_sibling(tcur
, left
, &cptr
, XFS_BB_RIGHTSIB
);
3592 * If left block is full enough so that removing one entry
3593 * won't make it too empty, and right-shifting an entry out
3594 * of left to us works, we're done.
3596 if (xfs_btree_get_numrecs(left
) - 1 >=
3597 cur
->bc_ops
->get_minrecs(tcur
, level
)) {
3598 error
= xfs_btree_rshift(tcur
, level
, &i
);
3602 ASSERT(xfs_btree_get_numrecs(block
) >=
3603 cur
->bc_ops
->get_minrecs(tcur
, level
));
3604 xfs_btree_del_cursor(tcur
, XFS_BTREE_NOERROR
);
3608 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
3615 * Otherwise, grab the number of records in right for
3618 lrecs
= xfs_btree_get_numrecs(left
);
3621 /* Delete the temp cursor, we're done with it. */
3622 xfs_btree_del_cursor(tcur
, XFS_BTREE_NOERROR
);
3625 /* If here, we need to do a join to keep the tree balanced. */
3626 ASSERT(!xfs_btree_ptr_is_null(cur
, &cptr
));
3628 if (!xfs_btree_ptr_is_null(cur
, &lptr
) &&
3629 lrecs
+ xfs_btree_get_numrecs(block
) <=
3630 cur
->bc_ops
->get_maxrecs(cur
, level
)) {
3632 * Set "right" to be the starting block,
3633 * "left" to be the left neighbor.
3638 error
= xfs_btree_read_buf_block(cur
, &lptr
, level
,
3644 * If that won't work, see if we can join with the right neighbor block.
3646 } else if (!xfs_btree_ptr_is_null(cur
, &rptr
) &&
3647 rrecs
+ xfs_btree_get_numrecs(block
) <=
3648 cur
->bc_ops
->get_maxrecs(cur
, level
)) {
3650 * Set "left" to be the starting block,
3651 * "right" to be the right neighbor.
3656 error
= xfs_btree_read_buf_block(cur
, &rptr
, level
,
3662 * Otherwise, we can't fix the imbalance.
3663 * Just return. This is probably a logic error, but it's not fatal.
3666 error
= xfs_btree_dec_cursor(cur
, level
, stat
);
3672 rrecs
= xfs_btree_get_numrecs(right
);
3673 lrecs
= xfs_btree_get_numrecs(left
);
3676 * We're now going to join "left" and "right" by moving all the stuff
3677 * in "right" to "left" and deleting "right".
3679 XFS_BTREE_STATS_ADD(cur
, moves
, rrecs
);
3681 /* It's a non-leaf. Move keys and pointers. */
3682 union xfs_btree_key
*lkp
; /* left btree key */
3683 union xfs_btree_ptr
*lpp
; /* left address pointer */
3684 union xfs_btree_key
*rkp
; /* right btree key */
3685 union xfs_btree_ptr
*rpp
; /* right address pointer */
3687 lkp
= xfs_btree_key_addr(cur
, lrecs
+ 1, left
);
3688 lpp
= xfs_btree_ptr_addr(cur
, lrecs
+ 1, left
);
3689 rkp
= xfs_btree_key_addr(cur
, 1, right
);
3690 rpp
= xfs_btree_ptr_addr(cur
, 1, right
);
3692 for (i
= 1; i
< rrecs
; i
++) {
3693 error
= xfs_btree_check_ptr(cur
, rpp
, i
, level
);
3698 xfs_btree_copy_keys(cur
, lkp
, rkp
, rrecs
);
3699 xfs_btree_copy_ptrs(cur
, lpp
, rpp
, rrecs
);
3701 xfs_btree_log_keys(cur
, lbp
, lrecs
+ 1, lrecs
+ rrecs
);
3702 xfs_btree_log_ptrs(cur
, lbp
, lrecs
+ 1, lrecs
+ rrecs
);
3704 /* It's a leaf. Move records. */
3705 union xfs_btree_rec
*lrp
; /* left record pointer */
3706 union xfs_btree_rec
*rrp
; /* right record pointer */
3708 lrp
= xfs_btree_rec_addr(cur
, lrecs
+ 1, left
);
3709 rrp
= xfs_btree_rec_addr(cur
, 1, right
);
3711 xfs_btree_copy_recs(cur
, lrp
, rrp
, rrecs
);
3712 xfs_btree_log_recs(cur
, lbp
, lrecs
+ 1, lrecs
+ rrecs
);
3715 XFS_BTREE_STATS_INC(cur
, join
);
3718 * Fix up the number of records and right block pointer in the
3719 * surviving block, and log it.
3721 xfs_btree_set_numrecs(left
, lrecs
+ rrecs
);
3722 xfs_btree_get_sibling(cur
, right
, &cptr
, XFS_BB_RIGHTSIB
),
3723 xfs_btree_set_sibling(cur
, left
, &cptr
, XFS_BB_RIGHTSIB
);
3724 xfs_btree_log_block(cur
, lbp
, XFS_BB_NUMRECS
| XFS_BB_RIGHTSIB
);
3726 /* If there is a right sibling, point it to the remaining block. */
3727 xfs_btree_get_sibling(cur
, left
, &cptr
, XFS_BB_RIGHTSIB
);
3728 if (!xfs_btree_ptr_is_null(cur
, &cptr
)) {
3729 error
= xfs_btree_read_buf_block(cur
, &cptr
, level
,
3730 0, &rrblock
, &rrbp
);
3733 xfs_btree_set_sibling(cur
, rrblock
, &lptr
, XFS_BB_LEFTSIB
);
3734 xfs_btree_log_block(cur
, rrbp
, XFS_BB_LEFTSIB
);
3737 /* Free the deleted block. */
3738 error
= cur
->bc_ops
->free_block(cur
, rbp
);
3741 XFS_BTREE_STATS_INC(cur
, free
);
3744 * If we joined with the left neighbor, set the buffer in the
3745 * cursor to the left block, and fix up the index.
3748 cur
->bc_bufs
[level
] = lbp
;
3749 cur
->bc_ptrs
[level
] += lrecs
;
3750 cur
->bc_ra
[level
] = 0;
3753 * If we joined with the right neighbor and there's a level above
3754 * us, increment the cursor at that level.
3756 else if ((cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
) ||
3757 (level
+ 1 < cur
->bc_nlevels
)) {
3758 error
= xfs_btree_increment(cur
, level
+ 1, &i
);
3764 * Readjust the ptr at this level if it's not a leaf, since it's
3765 * still pointing at the deletion point, which makes the cursor
3766 * inconsistent. If this makes the ptr 0, the caller fixes it up.
3767 * We can't use decrement because it would change the next level up.
3770 cur
->bc_ptrs
[level
]--;
3772 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
3773 /* Return value means the next level up has something to do. */
3778 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
3780 xfs_btree_del_cursor(tcur
, XFS_BTREE_ERROR
);
3785 * Delete the record pointed to by cur.
3786 * The cursor refers to the place where the record was (could be inserted)
3787 * when the operation returns.
3791 struct xfs_btree_cur
*cur
,
3792 int *stat
) /* success/failure */
3794 int error
; /* error return value */
3798 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
3801 * Go up the tree, starting at leaf level.
3803 * If 2 is returned then a join was done; go to the next level.
3804 * Otherwise we are done.
3806 for (level
= 0, i
= 2; i
== 2; level
++) {
3807 error
= xfs_btree_delrec(cur
, level
, &i
);
3813 for (level
= 1; level
< cur
->bc_nlevels
; level
++) {
3814 if (cur
->bc_ptrs
[level
] == 0) {
3815 error
= xfs_btree_decrement(cur
, level
, &i
);
3823 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
3827 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
3832 * Get the data from the pointed-to record.
3836 struct xfs_btree_cur
*cur
, /* btree cursor */
3837 union xfs_btree_rec
**recp
, /* output: btree record */
3838 int *stat
) /* output: success/failure */
3840 struct xfs_btree_block
*block
; /* btree block */
3841 struct xfs_buf
*bp
; /* buffer pointer */
3842 int ptr
; /* record number */
3844 int error
; /* error return value */
3847 ptr
= cur
->bc_ptrs
[0];
3848 block
= xfs_btree_get_block(cur
, 0, &bp
);
3851 error
= xfs_btree_check_block(cur
, block
, 0, bp
);
3857 * Off the right end or left end, return failure.
3859 if (ptr
> xfs_btree_get_numrecs(block
) || ptr
<= 0) {
3865 * Point to the record and extract its data.
3867 *recp
= xfs_btree_rec_addr(cur
, ptr
, block
);