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_shared.h"
21 #include "xfs_log_format.h"
22 #include "xfs_trans_resv.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_trans.h"
33 #include "xfs_inode_item.h"
34 #include "xfs_buf_item.h"
35 #include "xfs_btree.h"
36 #include "xfs_error.h"
37 #include "xfs_trace.h"
38 #include "xfs_cksum.h"
41 * Cursor allocation zone.
43 kmem_zone_t
*xfs_btree_cur_zone
;
46 * Btree magic numbers.
48 static const __uint32_t xfs_magics
[2][XFS_BTNUM_MAX
] = {
49 { XFS_ABTB_MAGIC
, XFS_ABTC_MAGIC
, XFS_BMAP_MAGIC
, XFS_IBT_MAGIC
},
50 { XFS_ABTB_CRC_MAGIC
, XFS_ABTC_CRC_MAGIC
,
51 XFS_BMAP_CRC_MAGIC
, XFS_IBT_CRC_MAGIC
}
53 #define xfs_btree_magic(cur) \
54 xfs_magics[!!((cur)->bc_flags & XFS_BTREE_CRC_BLOCKS)][cur->bc_btnum]
57 STATIC
int /* error (0 or EFSCORRUPTED) */
58 xfs_btree_check_lblock(
59 struct xfs_btree_cur
*cur
, /* btree cursor */
60 struct xfs_btree_block
*block
, /* btree long form block pointer */
61 int level
, /* level of the btree block */
62 struct xfs_buf
*bp
) /* buffer for block, if any */
64 int lblock_ok
= 1; /* block passes checks */
65 struct xfs_mount
*mp
; /* file system mount point */
69 if (xfs_sb_version_hascrc(&mp
->m_sb
)) {
70 lblock_ok
= lblock_ok
&&
71 uuid_equal(&block
->bb_u
.l
.bb_uuid
, &mp
->m_sb
.sb_uuid
) &&
72 block
->bb_u
.l
.bb_blkno
== cpu_to_be64(
73 bp
? bp
->b_bn
: XFS_BUF_DADDR_NULL
);
76 lblock_ok
= lblock_ok
&&
77 be32_to_cpu(block
->bb_magic
) == xfs_btree_magic(cur
) &&
78 be16_to_cpu(block
->bb_level
) == level
&&
79 be16_to_cpu(block
->bb_numrecs
) <=
80 cur
->bc_ops
->get_maxrecs(cur
, level
) &&
81 block
->bb_u
.l
.bb_leftsib
&&
82 (block
->bb_u
.l
.bb_leftsib
== cpu_to_be64(NULLDFSBNO
) ||
83 XFS_FSB_SANITY_CHECK(mp
,
84 be64_to_cpu(block
->bb_u
.l
.bb_leftsib
))) &&
85 block
->bb_u
.l
.bb_rightsib
&&
86 (block
->bb_u
.l
.bb_rightsib
== cpu_to_be64(NULLDFSBNO
) ||
87 XFS_FSB_SANITY_CHECK(mp
,
88 be64_to_cpu(block
->bb_u
.l
.bb_rightsib
)));
90 if (unlikely(XFS_TEST_ERROR(!lblock_ok
, mp
,
91 XFS_ERRTAG_BTREE_CHECK_LBLOCK
,
92 XFS_RANDOM_BTREE_CHECK_LBLOCK
))) {
94 trace_xfs_btree_corrupt(bp
, _RET_IP_
);
95 XFS_ERROR_REPORT(__func__
, XFS_ERRLEVEL_LOW
, mp
);
96 return XFS_ERROR(EFSCORRUPTED
);
101 STATIC
int /* error (0 or EFSCORRUPTED) */
102 xfs_btree_check_sblock(
103 struct xfs_btree_cur
*cur
, /* btree cursor */
104 struct xfs_btree_block
*block
, /* btree short form block pointer */
105 int level
, /* level of the btree block */
106 struct xfs_buf
*bp
) /* buffer containing block */
108 struct xfs_mount
*mp
; /* file system mount point */
109 struct xfs_buf
*agbp
; /* buffer for ag. freespace struct */
110 struct xfs_agf
*agf
; /* ag. freespace structure */
111 xfs_agblock_t agflen
; /* native ag. freespace length */
112 int sblock_ok
= 1; /* block passes checks */
115 agbp
= cur
->bc_private
.a
.agbp
;
116 agf
= XFS_BUF_TO_AGF(agbp
);
117 agflen
= be32_to_cpu(agf
->agf_length
);
119 if (xfs_sb_version_hascrc(&mp
->m_sb
)) {
120 sblock_ok
= sblock_ok
&&
121 uuid_equal(&block
->bb_u
.s
.bb_uuid
, &mp
->m_sb
.sb_uuid
) &&
122 block
->bb_u
.s
.bb_blkno
== cpu_to_be64(
123 bp
? bp
->b_bn
: XFS_BUF_DADDR_NULL
);
126 sblock_ok
= sblock_ok
&&
127 be32_to_cpu(block
->bb_magic
) == xfs_btree_magic(cur
) &&
128 be16_to_cpu(block
->bb_level
) == level
&&
129 be16_to_cpu(block
->bb_numrecs
) <=
130 cur
->bc_ops
->get_maxrecs(cur
, level
) &&
131 (block
->bb_u
.s
.bb_leftsib
== cpu_to_be32(NULLAGBLOCK
) ||
132 be32_to_cpu(block
->bb_u
.s
.bb_leftsib
) < agflen
) &&
133 block
->bb_u
.s
.bb_leftsib
&&
134 (block
->bb_u
.s
.bb_rightsib
== cpu_to_be32(NULLAGBLOCK
) ||
135 be32_to_cpu(block
->bb_u
.s
.bb_rightsib
) < agflen
) &&
136 block
->bb_u
.s
.bb_rightsib
;
138 if (unlikely(XFS_TEST_ERROR(!sblock_ok
, mp
,
139 XFS_ERRTAG_BTREE_CHECK_SBLOCK
,
140 XFS_RANDOM_BTREE_CHECK_SBLOCK
))) {
142 trace_xfs_btree_corrupt(bp
, _RET_IP_
);
143 XFS_ERROR_REPORT(__func__
, XFS_ERRLEVEL_LOW
, mp
);
144 return XFS_ERROR(EFSCORRUPTED
);
150 * Debug routine: check that block header is ok.
153 xfs_btree_check_block(
154 struct xfs_btree_cur
*cur
, /* btree cursor */
155 struct xfs_btree_block
*block
, /* generic btree block pointer */
156 int level
, /* level of the btree block */
157 struct xfs_buf
*bp
) /* buffer containing block, if any */
159 if (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
)
160 return xfs_btree_check_lblock(cur
, block
, level
, bp
);
162 return xfs_btree_check_sblock(cur
, block
, level
, bp
);
166 * Check that (long) pointer is ok.
168 int /* error (0 or EFSCORRUPTED) */
169 xfs_btree_check_lptr(
170 struct xfs_btree_cur
*cur
, /* btree cursor */
171 xfs_dfsbno_t bno
, /* btree block disk address */
172 int level
) /* btree block level */
174 XFS_WANT_CORRUPTED_RETURN(
177 XFS_FSB_SANITY_CHECK(cur
->bc_mp
, bno
));
183 * Check that (short) pointer is ok.
185 STATIC
int /* error (0 or EFSCORRUPTED) */
186 xfs_btree_check_sptr(
187 struct xfs_btree_cur
*cur
, /* btree cursor */
188 xfs_agblock_t bno
, /* btree block disk address */
189 int level
) /* btree block level */
191 xfs_agblock_t agblocks
= cur
->bc_mp
->m_sb
.sb_agblocks
;
193 XFS_WANT_CORRUPTED_RETURN(
195 bno
!= NULLAGBLOCK
&&
202 * Check that block ptr is ok.
204 STATIC
int /* error (0 or EFSCORRUPTED) */
206 struct xfs_btree_cur
*cur
, /* btree cursor */
207 union xfs_btree_ptr
*ptr
, /* btree block disk address */
208 int index
, /* offset from ptr to check */
209 int level
) /* btree block level */
211 if (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
) {
212 return xfs_btree_check_lptr(cur
,
213 be64_to_cpu((&ptr
->l
)[index
]), level
);
215 return xfs_btree_check_sptr(cur
,
216 be32_to_cpu((&ptr
->s
)[index
]), level
);
222 * Calculate CRC on the whole btree block and stuff it into the
223 * long-form btree header.
225 * Prior to calculting the CRC, pull the LSN out of the buffer log item and put
226 * it into the buffer so recovery knows what the last modifcation was that made
230 xfs_btree_lblock_calc_crc(
233 struct xfs_btree_block
*block
= XFS_BUF_TO_BLOCK(bp
);
234 struct xfs_buf_log_item
*bip
= bp
->b_fspriv
;
236 if (!xfs_sb_version_hascrc(&bp
->b_target
->bt_mount
->m_sb
))
239 block
->bb_u
.l
.bb_lsn
= cpu_to_be64(bip
->bli_item
.li_lsn
);
240 xfs_update_cksum(bp
->b_addr
, BBTOB(bp
->b_length
),
241 XFS_BTREE_LBLOCK_CRC_OFF
);
245 xfs_btree_lblock_verify_crc(
248 if (xfs_sb_version_hascrc(&bp
->b_target
->bt_mount
->m_sb
))
249 return xfs_verify_cksum(bp
->b_addr
, BBTOB(bp
->b_length
),
250 XFS_BTREE_LBLOCK_CRC_OFF
);
255 * Calculate CRC on the whole btree block and stuff it into the
256 * short-form btree header.
258 * Prior to calculting the CRC, pull the LSN out of the buffer log item and put
259 * it into the buffer so recovery knows what the last modifcation was that made
263 xfs_btree_sblock_calc_crc(
266 struct xfs_btree_block
*block
= XFS_BUF_TO_BLOCK(bp
);
267 struct xfs_buf_log_item
*bip
= bp
->b_fspriv
;
269 if (!xfs_sb_version_hascrc(&bp
->b_target
->bt_mount
->m_sb
))
272 block
->bb_u
.s
.bb_lsn
= cpu_to_be64(bip
->bli_item
.li_lsn
);
273 xfs_update_cksum(bp
->b_addr
, BBTOB(bp
->b_length
),
274 XFS_BTREE_SBLOCK_CRC_OFF
);
278 xfs_btree_sblock_verify_crc(
281 if (xfs_sb_version_hascrc(&bp
->b_target
->bt_mount
->m_sb
))
282 return xfs_verify_cksum(bp
->b_addr
, BBTOB(bp
->b_length
),
283 XFS_BTREE_SBLOCK_CRC_OFF
);
288 * Delete the btree cursor.
291 xfs_btree_del_cursor(
292 xfs_btree_cur_t
*cur
, /* btree cursor */
293 int error
) /* del because of error */
295 int i
; /* btree level */
298 * Clear the buffer pointers, and release the buffers.
299 * If we're doing this in the face of an error, we
300 * need to make sure to inspect all of the entries
301 * in the bc_bufs array for buffers to be unlocked.
302 * This is because some of the btree code works from
303 * level n down to 0, and if we get an error along
304 * the way we won't have initialized all the entries
307 for (i
= 0; i
< cur
->bc_nlevels
; i
++) {
309 xfs_trans_brelse(cur
->bc_tp
, cur
->bc_bufs
[i
]);
314 * Can't free a bmap cursor without having dealt with the
315 * allocated indirect blocks' accounting.
317 ASSERT(cur
->bc_btnum
!= XFS_BTNUM_BMAP
||
318 cur
->bc_private
.b
.allocated
== 0);
322 kmem_zone_free(xfs_btree_cur_zone
, cur
);
326 * Duplicate the btree cursor.
327 * Allocate a new one, copy the record, re-get the buffers.
330 xfs_btree_dup_cursor(
331 xfs_btree_cur_t
*cur
, /* input cursor */
332 xfs_btree_cur_t
**ncur
) /* output cursor */
334 xfs_buf_t
*bp
; /* btree block's buffer pointer */
335 int error
; /* error return value */
336 int i
; /* level number of btree block */
337 xfs_mount_t
*mp
; /* mount structure for filesystem */
338 xfs_btree_cur_t
*new; /* new cursor value */
339 xfs_trans_t
*tp
; /* transaction pointer, can be NULL */
345 * Allocate a new cursor like the old one.
347 new = cur
->bc_ops
->dup_cursor(cur
);
350 * Copy the record currently in the cursor.
352 new->bc_rec
= cur
->bc_rec
;
355 * For each level current, re-get the buffer and copy the ptr value.
357 for (i
= 0; i
< new->bc_nlevels
; i
++) {
358 new->bc_ptrs
[i
] = cur
->bc_ptrs
[i
];
359 new->bc_ra
[i
] = cur
->bc_ra
[i
];
360 bp
= cur
->bc_bufs
[i
];
362 error
= xfs_trans_read_buf(mp
, tp
, mp
->m_ddev_targp
,
363 XFS_BUF_ADDR(bp
), mp
->m_bsize
,
365 cur
->bc_ops
->buf_ops
);
367 xfs_btree_del_cursor(new, error
);
372 new->bc_bufs
[i
] = bp
;
379 * XFS btree block layout and addressing:
381 * There are two types of blocks in the btree: leaf and non-leaf blocks.
383 * The leaf record start with a header then followed by records containing
384 * the values. A non-leaf block also starts with the same header, and
385 * then first contains lookup keys followed by an equal number of pointers
386 * to the btree blocks at the previous level.
388 * +--------+-------+-------+-------+-------+-------+-------+
389 * Leaf: | header | rec 1 | rec 2 | rec 3 | rec 4 | rec 5 | rec N |
390 * +--------+-------+-------+-------+-------+-------+-------+
392 * +--------+-------+-------+-------+-------+-------+-------+
393 * Non-Leaf: | header | key 1 | key 2 | key N | ptr 1 | ptr 2 | ptr N |
394 * +--------+-------+-------+-------+-------+-------+-------+
396 * The header is called struct xfs_btree_block for reasons better left unknown
397 * and comes in different versions for short (32bit) and long (64bit) block
398 * pointers. The record and key structures are defined by the btree instances
399 * and opaque to the btree core. The block pointers are simple disk endian
400 * integers, available in a short (32bit) and long (64bit) variant.
402 * The helpers below calculate the offset of a given record, key or pointer
403 * into a btree block (xfs_btree_*_offset) or return a pointer to the given
404 * record, key or pointer (xfs_btree_*_addr). Note that all addressing
405 * inside the btree block is done using indices starting at one, not zero!
409 * Return size of the btree block header for this btree instance.
411 static inline size_t xfs_btree_block_len(struct xfs_btree_cur
*cur
)
413 if (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
) {
414 if (cur
->bc_flags
& XFS_BTREE_CRC_BLOCKS
)
415 return XFS_BTREE_LBLOCK_CRC_LEN
;
416 return XFS_BTREE_LBLOCK_LEN
;
418 if (cur
->bc_flags
& XFS_BTREE_CRC_BLOCKS
)
419 return XFS_BTREE_SBLOCK_CRC_LEN
;
420 return XFS_BTREE_SBLOCK_LEN
;
424 * Return size of btree block pointers for this btree instance.
426 static inline size_t xfs_btree_ptr_len(struct xfs_btree_cur
*cur
)
428 return (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
) ?
429 sizeof(__be64
) : sizeof(__be32
);
433 * Calculate offset of the n-th record in a btree block.
436 xfs_btree_rec_offset(
437 struct xfs_btree_cur
*cur
,
440 return xfs_btree_block_len(cur
) +
441 (n
- 1) * cur
->bc_ops
->rec_len
;
445 * Calculate offset of the n-th key in a btree block.
448 xfs_btree_key_offset(
449 struct xfs_btree_cur
*cur
,
452 return xfs_btree_block_len(cur
) +
453 (n
- 1) * cur
->bc_ops
->key_len
;
457 * Calculate offset of the n-th block pointer in a btree block.
460 xfs_btree_ptr_offset(
461 struct xfs_btree_cur
*cur
,
465 return xfs_btree_block_len(cur
) +
466 cur
->bc_ops
->get_maxrecs(cur
, level
) * cur
->bc_ops
->key_len
+
467 (n
- 1) * xfs_btree_ptr_len(cur
);
471 * Return a pointer to the n-th record in the btree block.
473 STATIC
union xfs_btree_rec
*
475 struct xfs_btree_cur
*cur
,
477 struct xfs_btree_block
*block
)
479 return (union xfs_btree_rec
*)
480 ((char *)block
+ xfs_btree_rec_offset(cur
, n
));
484 * Return a pointer to the n-th key in the btree block.
486 STATIC
union xfs_btree_key
*
488 struct xfs_btree_cur
*cur
,
490 struct xfs_btree_block
*block
)
492 return (union xfs_btree_key
*)
493 ((char *)block
+ xfs_btree_key_offset(cur
, n
));
497 * Return a pointer to the n-th block pointer in the btree block.
499 STATIC
union xfs_btree_ptr
*
501 struct xfs_btree_cur
*cur
,
503 struct xfs_btree_block
*block
)
505 int level
= xfs_btree_get_level(block
);
507 ASSERT(block
->bb_level
!= 0);
509 return (union xfs_btree_ptr
*)
510 ((char *)block
+ xfs_btree_ptr_offset(cur
, n
, level
));
514 * Get the root block which is stored in the inode.
516 * For now this btree implementation assumes the btree root is always
517 * stored in the if_broot field of an inode fork.
519 STATIC
struct xfs_btree_block
*
521 struct xfs_btree_cur
*cur
)
523 struct xfs_ifork
*ifp
;
525 ifp
= XFS_IFORK_PTR(cur
->bc_private
.b
.ip
, cur
->bc_private
.b
.whichfork
);
526 return (struct xfs_btree_block
*)ifp
->if_broot
;
530 * Retrieve the block pointer from the cursor at the given level.
531 * This may be an inode btree root or from a buffer.
533 STATIC
struct xfs_btree_block
* /* generic btree block pointer */
535 struct xfs_btree_cur
*cur
, /* btree cursor */
536 int level
, /* level in btree */
537 struct xfs_buf
**bpp
) /* buffer containing the block */
539 if ((cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
) &&
540 (level
== cur
->bc_nlevels
- 1)) {
542 return xfs_btree_get_iroot(cur
);
545 *bpp
= cur
->bc_bufs
[level
];
546 return XFS_BUF_TO_BLOCK(*bpp
);
550 * Get a buffer for the block, return it with no data read.
551 * Long-form addressing.
553 xfs_buf_t
* /* buffer for fsbno */
555 xfs_mount_t
*mp
, /* file system mount point */
556 xfs_trans_t
*tp
, /* transaction pointer */
557 xfs_fsblock_t fsbno
, /* file system block number */
558 uint lock
) /* lock flags for get_buf */
560 xfs_buf_t
*bp
; /* buffer pointer (return value) */
561 xfs_daddr_t d
; /* real disk block address */
563 ASSERT(fsbno
!= NULLFSBLOCK
);
564 d
= XFS_FSB_TO_DADDR(mp
, fsbno
);
565 bp
= xfs_trans_get_buf(tp
, mp
->m_ddev_targp
, d
, mp
->m_bsize
, lock
);
566 ASSERT(!xfs_buf_geterror(bp
));
571 * Get a buffer for the block, return it with no data read.
572 * Short-form addressing.
574 xfs_buf_t
* /* buffer for agno/agbno */
576 xfs_mount_t
*mp
, /* file system mount point */
577 xfs_trans_t
*tp
, /* transaction pointer */
578 xfs_agnumber_t agno
, /* allocation group number */
579 xfs_agblock_t agbno
, /* allocation group block number */
580 uint lock
) /* lock flags for get_buf */
582 xfs_buf_t
*bp
; /* buffer pointer (return value) */
583 xfs_daddr_t d
; /* real disk block address */
585 ASSERT(agno
!= NULLAGNUMBER
);
586 ASSERT(agbno
!= NULLAGBLOCK
);
587 d
= XFS_AGB_TO_DADDR(mp
, agno
, agbno
);
588 bp
= xfs_trans_get_buf(tp
, mp
->m_ddev_targp
, d
, mp
->m_bsize
, lock
);
589 ASSERT(!xfs_buf_geterror(bp
));
594 * Check for the cursor referring to the last block at the given level.
596 int /* 1=is last block, 0=not last block */
597 xfs_btree_islastblock(
598 xfs_btree_cur_t
*cur
, /* btree cursor */
599 int level
) /* level to check */
601 struct xfs_btree_block
*block
; /* generic btree block pointer */
602 xfs_buf_t
*bp
; /* buffer containing block */
604 block
= xfs_btree_get_block(cur
, level
, &bp
);
605 xfs_btree_check_block(cur
, block
, level
, bp
);
606 if (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
)
607 return block
->bb_u
.l
.bb_rightsib
== cpu_to_be64(NULLDFSBNO
);
609 return block
->bb_u
.s
.bb_rightsib
== cpu_to_be32(NULLAGBLOCK
);
613 * Change the cursor to point to the first record at the given level.
614 * Other levels are unaffected.
616 STATIC
int /* success=1, failure=0 */
618 xfs_btree_cur_t
*cur
, /* btree cursor */
619 int level
) /* level to change */
621 struct xfs_btree_block
*block
; /* generic btree block pointer */
622 xfs_buf_t
*bp
; /* buffer containing block */
625 * Get the block pointer for this level.
627 block
= xfs_btree_get_block(cur
, level
, &bp
);
628 xfs_btree_check_block(cur
, block
, level
, bp
);
630 * It's empty, there is no such record.
632 if (!block
->bb_numrecs
)
635 * Set the ptr value to 1, that's the first record/key.
637 cur
->bc_ptrs
[level
] = 1;
642 * Change the cursor to point to the last record in the current block
643 * at the given level. Other levels are unaffected.
645 STATIC
int /* success=1, failure=0 */
647 xfs_btree_cur_t
*cur
, /* btree cursor */
648 int level
) /* level to change */
650 struct xfs_btree_block
*block
; /* generic btree block pointer */
651 xfs_buf_t
*bp
; /* buffer containing block */
654 * Get the block pointer for this level.
656 block
= xfs_btree_get_block(cur
, level
, &bp
);
657 xfs_btree_check_block(cur
, block
, level
, bp
);
659 * It's empty, there is no such record.
661 if (!block
->bb_numrecs
)
664 * Set the ptr value to numrecs, that's the last record/key.
666 cur
->bc_ptrs
[level
] = be16_to_cpu(block
->bb_numrecs
);
671 * Compute first and last byte offsets for the fields given.
672 * Interprets the offsets table, which contains struct field offsets.
676 __int64_t fields
, /* bitmask of fields */
677 const short *offsets
, /* table of field offsets */
678 int nbits
, /* number of bits to inspect */
679 int *first
, /* output: first byte offset */
680 int *last
) /* output: last byte offset */
682 int i
; /* current bit number */
683 __int64_t imask
; /* mask for current bit number */
687 * Find the lowest bit, so the first byte offset.
689 for (i
= 0, imask
= 1LL; ; i
++, imask
<<= 1) {
690 if (imask
& fields
) {
696 * Find the highest bit, so the last byte offset.
698 for (i
= nbits
- 1, imask
= 1LL << i
; ; i
--, imask
>>= 1) {
699 if (imask
& fields
) {
700 *last
= offsets
[i
+ 1] - 1;
707 * Get a buffer for the block, return it read in.
708 * Long-form addressing.
712 struct xfs_mount
*mp
, /* file system mount point */
713 struct xfs_trans
*tp
, /* transaction pointer */
714 xfs_fsblock_t fsbno
, /* file system block number */
715 uint lock
, /* lock flags for read_buf */
716 struct xfs_buf
**bpp
, /* buffer for fsbno */
717 int refval
, /* ref count value for buffer */
718 const struct xfs_buf_ops
*ops
)
720 struct xfs_buf
*bp
; /* return value */
721 xfs_daddr_t d
; /* real disk block address */
724 ASSERT(fsbno
!= NULLFSBLOCK
);
725 d
= XFS_FSB_TO_DADDR(mp
, fsbno
);
726 error
= xfs_trans_read_buf(mp
, tp
, mp
->m_ddev_targp
, d
,
727 mp
->m_bsize
, lock
, &bp
, ops
);
730 ASSERT(!xfs_buf_geterror(bp
));
732 xfs_buf_set_ref(bp
, refval
);
738 * Read-ahead the block, don't wait for it, don't return a buffer.
739 * Long-form addressing.
743 xfs_btree_reada_bufl(
744 struct xfs_mount
*mp
, /* file system mount point */
745 xfs_fsblock_t fsbno
, /* file system block number */
746 xfs_extlen_t count
, /* count of filesystem blocks */
747 const struct xfs_buf_ops
*ops
)
751 ASSERT(fsbno
!= NULLFSBLOCK
);
752 d
= XFS_FSB_TO_DADDR(mp
, fsbno
);
753 xfs_buf_readahead(mp
->m_ddev_targp
, d
, mp
->m_bsize
* count
, ops
);
757 * Read-ahead the block, don't wait for it, don't return a buffer.
758 * Short-form addressing.
762 xfs_btree_reada_bufs(
763 struct xfs_mount
*mp
, /* file system mount point */
764 xfs_agnumber_t agno
, /* allocation group number */
765 xfs_agblock_t agbno
, /* allocation group block number */
766 xfs_extlen_t count
, /* count of filesystem blocks */
767 const struct xfs_buf_ops
*ops
)
771 ASSERT(agno
!= NULLAGNUMBER
);
772 ASSERT(agbno
!= NULLAGBLOCK
);
773 d
= XFS_AGB_TO_DADDR(mp
, agno
, agbno
);
774 xfs_buf_readahead(mp
->m_ddev_targp
, d
, mp
->m_bsize
* count
, ops
);
778 xfs_btree_readahead_lblock(
779 struct xfs_btree_cur
*cur
,
781 struct xfs_btree_block
*block
)
784 xfs_dfsbno_t left
= be64_to_cpu(block
->bb_u
.l
.bb_leftsib
);
785 xfs_dfsbno_t right
= be64_to_cpu(block
->bb_u
.l
.bb_rightsib
);
787 if ((lr
& XFS_BTCUR_LEFTRA
) && left
!= NULLDFSBNO
) {
788 xfs_btree_reada_bufl(cur
->bc_mp
, left
, 1,
789 cur
->bc_ops
->buf_ops
);
793 if ((lr
& XFS_BTCUR_RIGHTRA
) && right
!= NULLDFSBNO
) {
794 xfs_btree_reada_bufl(cur
->bc_mp
, right
, 1,
795 cur
->bc_ops
->buf_ops
);
803 xfs_btree_readahead_sblock(
804 struct xfs_btree_cur
*cur
,
806 struct xfs_btree_block
*block
)
809 xfs_agblock_t left
= be32_to_cpu(block
->bb_u
.s
.bb_leftsib
);
810 xfs_agblock_t right
= be32_to_cpu(block
->bb_u
.s
.bb_rightsib
);
813 if ((lr
& XFS_BTCUR_LEFTRA
) && left
!= NULLAGBLOCK
) {
814 xfs_btree_reada_bufs(cur
->bc_mp
, cur
->bc_private
.a
.agno
,
815 left
, 1, cur
->bc_ops
->buf_ops
);
819 if ((lr
& XFS_BTCUR_RIGHTRA
) && right
!= NULLAGBLOCK
) {
820 xfs_btree_reada_bufs(cur
->bc_mp
, cur
->bc_private
.a
.agno
,
821 right
, 1, cur
->bc_ops
->buf_ops
);
829 * Read-ahead btree blocks, at the given level.
830 * Bits in lr are set from XFS_BTCUR_{LEFT,RIGHT}RA.
834 struct xfs_btree_cur
*cur
, /* btree cursor */
835 int lev
, /* level in btree */
836 int lr
) /* left/right bits */
838 struct xfs_btree_block
*block
;
841 * No readahead needed if we are at the root level and the
842 * btree root is stored in the inode.
844 if ((cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
) &&
845 (lev
== cur
->bc_nlevels
- 1))
848 if ((cur
->bc_ra
[lev
] | lr
) == cur
->bc_ra
[lev
])
851 cur
->bc_ra
[lev
] |= lr
;
852 block
= XFS_BUF_TO_BLOCK(cur
->bc_bufs
[lev
]);
854 if (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
)
855 return xfs_btree_readahead_lblock(cur
, lr
, block
);
856 return xfs_btree_readahead_sblock(cur
, lr
, block
);
860 xfs_btree_ptr_to_daddr(
861 struct xfs_btree_cur
*cur
,
862 union xfs_btree_ptr
*ptr
)
864 if (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
) {
865 ASSERT(ptr
->l
!= cpu_to_be64(NULLDFSBNO
));
867 return XFS_FSB_TO_DADDR(cur
->bc_mp
, be64_to_cpu(ptr
->l
));
869 ASSERT(cur
->bc_private
.a
.agno
!= NULLAGNUMBER
);
870 ASSERT(ptr
->s
!= cpu_to_be32(NULLAGBLOCK
));
872 return XFS_AGB_TO_DADDR(cur
->bc_mp
, cur
->bc_private
.a
.agno
,
873 be32_to_cpu(ptr
->s
));
878 * Readahead @count btree blocks at the given @ptr location.
880 * We don't need to care about long or short form btrees here as we have a
881 * method of converting the ptr directly to a daddr available to us.
884 xfs_btree_readahead_ptr(
885 struct xfs_btree_cur
*cur
,
886 union xfs_btree_ptr
*ptr
,
889 xfs_buf_readahead(cur
->bc_mp
->m_ddev_targp
,
890 xfs_btree_ptr_to_daddr(cur
, ptr
),
891 cur
->bc_mp
->m_bsize
* count
, cur
->bc_ops
->buf_ops
);
895 * Set the buffer for level "lev" in the cursor to bp, releasing
896 * any previous buffer.
900 xfs_btree_cur_t
*cur
, /* btree cursor */
901 int lev
, /* level in btree */
902 xfs_buf_t
*bp
) /* new buffer to set */
904 struct xfs_btree_block
*b
; /* btree block */
906 if (cur
->bc_bufs
[lev
])
907 xfs_trans_brelse(cur
->bc_tp
, cur
->bc_bufs
[lev
]);
908 cur
->bc_bufs
[lev
] = bp
;
911 b
= XFS_BUF_TO_BLOCK(bp
);
912 if (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
) {
913 if (b
->bb_u
.l
.bb_leftsib
== cpu_to_be64(NULLDFSBNO
))
914 cur
->bc_ra
[lev
] |= XFS_BTCUR_LEFTRA
;
915 if (b
->bb_u
.l
.bb_rightsib
== cpu_to_be64(NULLDFSBNO
))
916 cur
->bc_ra
[lev
] |= XFS_BTCUR_RIGHTRA
;
918 if (b
->bb_u
.s
.bb_leftsib
== cpu_to_be32(NULLAGBLOCK
))
919 cur
->bc_ra
[lev
] |= XFS_BTCUR_LEFTRA
;
920 if (b
->bb_u
.s
.bb_rightsib
== cpu_to_be32(NULLAGBLOCK
))
921 cur
->bc_ra
[lev
] |= XFS_BTCUR_RIGHTRA
;
926 xfs_btree_ptr_is_null(
927 struct xfs_btree_cur
*cur
,
928 union xfs_btree_ptr
*ptr
)
930 if (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
)
931 return ptr
->l
== cpu_to_be64(NULLDFSBNO
);
933 return ptr
->s
== cpu_to_be32(NULLAGBLOCK
);
937 xfs_btree_set_ptr_null(
938 struct xfs_btree_cur
*cur
,
939 union xfs_btree_ptr
*ptr
)
941 if (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
)
942 ptr
->l
= cpu_to_be64(NULLDFSBNO
);
944 ptr
->s
= cpu_to_be32(NULLAGBLOCK
);
948 * Get/set/init sibling pointers
951 xfs_btree_get_sibling(
952 struct xfs_btree_cur
*cur
,
953 struct xfs_btree_block
*block
,
954 union xfs_btree_ptr
*ptr
,
957 ASSERT(lr
== XFS_BB_LEFTSIB
|| lr
== XFS_BB_RIGHTSIB
);
959 if (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
) {
960 if (lr
== XFS_BB_RIGHTSIB
)
961 ptr
->l
= block
->bb_u
.l
.bb_rightsib
;
963 ptr
->l
= block
->bb_u
.l
.bb_leftsib
;
965 if (lr
== XFS_BB_RIGHTSIB
)
966 ptr
->s
= block
->bb_u
.s
.bb_rightsib
;
968 ptr
->s
= block
->bb_u
.s
.bb_leftsib
;
973 xfs_btree_set_sibling(
974 struct xfs_btree_cur
*cur
,
975 struct xfs_btree_block
*block
,
976 union xfs_btree_ptr
*ptr
,
979 ASSERT(lr
== XFS_BB_LEFTSIB
|| lr
== XFS_BB_RIGHTSIB
);
981 if (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
) {
982 if (lr
== XFS_BB_RIGHTSIB
)
983 block
->bb_u
.l
.bb_rightsib
= ptr
->l
;
985 block
->bb_u
.l
.bb_leftsib
= ptr
->l
;
987 if (lr
== XFS_BB_RIGHTSIB
)
988 block
->bb_u
.s
.bb_rightsib
= ptr
->s
;
990 block
->bb_u
.s
.bb_leftsib
= ptr
->s
;
995 xfs_btree_init_block_int(
996 struct xfs_mount
*mp
,
997 struct xfs_btree_block
*buf
,
1005 buf
->bb_magic
= cpu_to_be32(magic
);
1006 buf
->bb_level
= cpu_to_be16(level
);
1007 buf
->bb_numrecs
= cpu_to_be16(numrecs
);
1009 if (flags
& XFS_BTREE_LONG_PTRS
) {
1010 buf
->bb_u
.l
.bb_leftsib
= cpu_to_be64(NULLDFSBNO
);
1011 buf
->bb_u
.l
.bb_rightsib
= cpu_to_be64(NULLDFSBNO
);
1012 if (flags
& XFS_BTREE_CRC_BLOCKS
) {
1013 buf
->bb_u
.l
.bb_blkno
= cpu_to_be64(blkno
);
1014 buf
->bb_u
.l
.bb_owner
= cpu_to_be64(owner
);
1015 uuid_copy(&buf
->bb_u
.l
.bb_uuid
, &mp
->m_sb
.sb_uuid
);
1016 buf
->bb_u
.l
.bb_pad
= 0;
1017 buf
->bb_u
.l
.bb_lsn
= 0;
1020 /* owner is a 32 bit value on short blocks */
1021 __u32 __owner
= (__u32
)owner
;
1023 buf
->bb_u
.s
.bb_leftsib
= cpu_to_be32(NULLAGBLOCK
);
1024 buf
->bb_u
.s
.bb_rightsib
= cpu_to_be32(NULLAGBLOCK
);
1025 if (flags
& XFS_BTREE_CRC_BLOCKS
) {
1026 buf
->bb_u
.s
.bb_blkno
= cpu_to_be64(blkno
);
1027 buf
->bb_u
.s
.bb_owner
= cpu_to_be32(__owner
);
1028 uuid_copy(&buf
->bb_u
.s
.bb_uuid
, &mp
->m_sb
.sb_uuid
);
1029 buf
->bb_u
.s
.bb_lsn
= 0;
1035 xfs_btree_init_block(
1036 struct xfs_mount
*mp
,
1044 xfs_btree_init_block_int(mp
, XFS_BUF_TO_BLOCK(bp
), bp
->b_bn
,
1045 magic
, level
, numrecs
, owner
, flags
);
1049 xfs_btree_init_block_cur(
1050 struct xfs_btree_cur
*cur
,
1058 * we can pull the owner from the cursor right now as the different
1059 * owners align directly with the pointer size of the btree. This may
1060 * change in future, but is safe for current users of the generic btree
1063 if (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
)
1064 owner
= cur
->bc_private
.b
.ip
->i_ino
;
1066 owner
= cur
->bc_private
.a
.agno
;
1068 xfs_btree_init_block_int(cur
->bc_mp
, XFS_BUF_TO_BLOCK(bp
), bp
->b_bn
,
1069 xfs_btree_magic(cur
), level
, numrecs
,
1070 owner
, cur
->bc_flags
);
1074 * Return true if ptr is the last record in the btree and
1075 * we need to track updates to this record. The decision
1076 * will be further refined in the update_lastrec method.
1079 xfs_btree_is_lastrec(
1080 struct xfs_btree_cur
*cur
,
1081 struct xfs_btree_block
*block
,
1084 union xfs_btree_ptr ptr
;
1088 if (!(cur
->bc_flags
& XFS_BTREE_LASTREC_UPDATE
))
1091 xfs_btree_get_sibling(cur
, block
, &ptr
, XFS_BB_RIGHTSIB
);
1092 if (!xfs_btree_ptr_is_null(cur
, &ptr
))
1098 xfs_btree_buf_to_ptr(
1099 struct xfs_btree_cur
*cur
,
1101 union xfs_btree_ptr
*ptr
)
1103 if (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
)
1104 ptr
->l
= cpu_to_be64(XFS_DADDR_TO_FSB(cur
->bc_mp
,
1107 ptr
->s
= cpu_to_be32(xfs_daddr_to_agbno(cur
->bc_mp
,
1114 struct xfs_btree_cur
*cur
,
1117 switch (cur
->bc_btnum
) {
1120 xfs_buf_set_ref(bp
, XFS_ALLOC_BTREE_REF
);
1123 xfs_buf_set_ref(bp
, XFS_INO_BTREE_REF
);
1125 case XFS_BTNUM_BMAP
:
1126 xfs_buf_set_ref(bp
, XFS_BMAP_BTREE_REF
);
1134 xfs_btree_get_buf_block(
1135 struct xfs_btree_cur
*cur
,
1136 union xfs_btree_ptr
*ptr
,
1138 struct xfs_btree_block
**block
,
1139 struct xfs_buf
**bpp
)
1141 struct xfs_mount
*mp
= cur
->bc_mp
;
1144 /* need to sort out how callers deal with failures first */
1145 ASSERT(!(flags
& XBF_TRYLOCK
));
1147 d
= xfs_btree_ptr_to_daddr(cur
, ptr
);
1148 *bpp
= xfs_trans_get_buf(cur
->bc_tp
, mp
->m_ddev_targp
, d
,
1149 mp
->m_bsize
, flags
);
1154 (*bpp
)->b_ops
= cur
->bc_ops
->buf_ops
;
1155 *block
= XFS_BUF_TO_BLOCK(*bpp
);
1160 * Read in the buffer at the given ptr and return the buffer and
1161 * the block pointer within the buffer.
1164 xfs_btree_read_buf_block(
1165 struct xfs_btree_cur
*cur
,
1166 union xfs_btree_ptr
*ptr
,
1169 struct xfs_btree_block
**block
,
1170 struct xfs_buf
**bpp
)
1172 struct xfs_mount
*mp
= cur
->bc_mp
;
1176 /* need to sort out how callers deal with failures first */
1177 ASSERT(!(flags
& XBF_TRYLOCK
));
1179 d
= xfs_btree_ptr_to_daddr(cur
, ptr
);
1180 error
= xfs_trans_read_buf(mp
, cur
->bc_tp
, mp
->m_ddev_targp
, d
,
1181 mp
->m_bsize
, flags
, bpp
,
1182 cur
->bc_ops
->buf_ops
);
1186 ASSERT(!xfs_buf_geterror(*bpp
));
1187 xfs_btree_set_refs(cur
, *bpp
);
1188 *block
= XFS_BUF_TO_BLOCK(*bpp
);
1193 * Copy keys from one btree block to another.
1196 xfs_btree_copy_keys(
1197 struct xfs_btree_cur
*cur
,
1198 union xfs_btree_key
*dst_key
,
1199 union xfs_btree_key
*src_key
,
1202 ASSERT(numkeys
>= 0);
1203 memcpy(dst_key
, src_key
, numkeys
* cur
->bc_ops
->key_len
);
1207 * Copy records from one btree block to another.
1210 xfs_btree_copy_recs(
1211 struct xfs_btree_cur
*cur
,
1212 union xfs_btree_rec
*dst_rec
,
1213 union xfs_btree_rec
*src_rec
,
1216 ASSERT(numrecs
>= 0);
1217 memcpy(dst_rec
, src_rec
, numrecs
* cur
->bc_ops
->rec_len
);
1221 * Copy block pointers from one btree block to another.
1224 xfs_btree_copy_ptrs(
1225 struct xfs_btree_cur
*cur
,
1226 union xfs_btree_ptr
*dst_ptr
,
1227 union xfs_btree_ptr
*src_ptr
,
1230 ASSERT(numptrs
>= 0);
1231 memcpy(dst_ptr
, src_ptr
, numptrs
* xfs_btree_ptr_len(cur
));
1235 * Shift keys one index left/right inside a single btree block.
1238 xfs_btree_shift_keys(
1239 struct xfs_btree_cur
*cur
,
1240 union xfs_btree_key
*key
,
1246 ASSERT(numkeys
>= 0);
1247 ASSERT(dir
== 1 || dir
== -1);
1249 dst_key
= (char *)key
+ (dir
* cur
->bc_ops
->key_len
);
1250 memmove(dst_key
, key
, numkeys
* cur
->bc_ops
->key_len
);
1254 * Shift records one index left/right inside a single btree block.
1257 xfs_btree_shift_recs(
1258 struct xfs_btree_cur
*cur
,
1259 union xfs_btree_rec
*rec
,
1265 ASSERT(numrecs
>= 0);
1266 ASSERT(dir
== 1 || dir
== -1);
1268 dst_rec
= (char *)rec
+ (dir
* cur
->bc_ops
->rec_len
);
1269 memmove(dst_rec
, rec
, numrecs
* cur
->bc_ops
->rec_len
);
1273 * Shift block pointers one index left/right inside a single btree block.
1276 xfs_btree_shift_ptrs(
1277 struct xfs_btree_cur
*cur
,
1278 union xfs_btree_ptr
*ptr
,
1284 ASSERT(numptrs
>= 0);
1285 ASSERT(dir
== 1 || dir
== -1);
1287 dst_ptr
= (char *)ptr
+ (dir
* xfs_btree_ptr_len(cur
));
1288 memmove(dst_ptr
, ptr
, numptrs
* xfs_btree_ptr_len(cur
));
1292 * Log key values from the btree block.
1296 struct xfs_btree_cur
*cur
,
1301 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
1302 XFS_BTREE_TRACE_ARGBII(cur
, bp
, first
, last
);
1305 xfs_trans_buf_set_type(cur
->bc_tp
, bp
, XFS_BLFT_BTREE_BUF
);
1306 xfs_trans_log_buf(cur
->bc_tp
, bp
,
1307 xfs_btree_key_offset(cur
, first
),
1308 xfs_btree_key_offset(cur
, last
+ 1) - 1);
1310 xfs_trans_log_inode(cur
->bc_tp
, cur
->bc_private
.b
.ip
,
1311 xfs_ilog_fbroot(cur
->bc_private
.b
.whichfork
));
1314 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
1318 * Log record values from the btree block.
1322 struct xfs_btree_cur
*cur
,
1327 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
1328 XFS_BTREE_TRACE_ARGBII(cur
, bp
, first
, last
);
1330 xfs_trans_buf_set_type(cur
->bc_tp
, bp
, XFS_BLFT_BTREE_BUF
);
1331 xfs_trans_log_buf(cur
->bc_tp
, bp
,
1332 xfs_btree_rec_offset(cur
, first
),
1333 xfs_btree_rec_offset(cur
, last
+ 1) - 1);
1335 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
1339 * Log block pointer fields from a btree block (nonleaf).
1343 struct xfs_btree_cur
*cur
, /* btree cursor */
1344 struct xfs_buf
*bp
, /* buffer containing btree block */
1345 int first
, /* index of first pointer to log */
1346 int last
) /* index of last pointer to log */
1348 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
1349 XFS_BTREE_TRACE_ARGBII(cur
, bp
, first
, last
);
1352 struct xfs_btree_block
*block
= XFS_BUF_TO_BLOCK(bp
);
1353 int level
= xfs_btree_get_level(block
);
1355 xfs_trans_buf_set_type(cur
->bc_tp
, bp
, XFS_BLFT_BTREE_BUF
);
1356 xfs_trans_log_buf(cur
->bc_tp
, bp
,
1357 xfs_btree_ptr_offset(cur
, first
, level
),
1358 xfs_btree_ptr_offset(cur
, last
+ 1, level
) - 1);
1360 xfs_trans_log_inode(cur
->bc_tp
, cur
->bc_private
.b
.ip
,
1361 xfs_ilog_fbroot(cur
->bc_private
.b
.whichfork
));
1364 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
1368 * Log fields from a btree block header.
1371 xfs_btree_log_block(
1372 struct xfs_btree_cur
*cur
, /* btree cursor */
1373 struct xfs_buf
*bp
, /* buffer containing btree block */
1374 int fields
) /* mask of fields: XFS_BB_... */
1376 int first
; /* first byte offset logged */
1377 int last
; /* last byte offset logged */
1378 static const short soffsets
[] = { /* table of offsets (short) */
1379 offsetof(struct xfs_btree_block
, bb_magic
),
1380 offsetof(struct xfs_btree_block
, bb_level
),
1381 offsetof(struct xfs_btree_block
, bb_numrecs
),
1382 offsetof(struct xfs_btree_block
, bb_u
.s
.bb_leftsib
),
1383 offsetof(struct xfs_btree_block
, bb_u
.s
.bb_rightsib
),
1384 offsetof(struct xfs_btree_block
, bb_u
.s
.bb_blkno
),
1385 offsetof(struct xfs_btree_block
, bb_u
.s
.bb_lsn
),
1386 offsetof(struct xfs_btree_block
, bb_u
.s
.bb_uuid
),
1387 offsetof(struct xfs_btree_block
, bb_u
.s
.bb_owner
),
1388 offsetof(struct xfs_btree_block
, bb_u
.s
.bb_crc
),
1389 XFS_BTREE_SBLOCK_CRC_LEN
1391 static const short loffsets
[] = { /* table of offsets (long) */
1392 offsetof(struct xfs_btree_block
, bb_magic
),
1393 offsetof(struct xfs_btree_block
, bb_level
),
1394 offsetof(struct xfs_btree_block
, bb_numrecs
),
1395 offsetof(struct xfs_btree_block
, bb_u
.l
.bb_leftsib
),
1396 offsetof(struct xfs_btree_block
, bb_u
.l
.bb_rightsib
),
1397 offsetof(struct xfs_btree_block
, bb_u
.l
.bb_blkno
),
1398 offsetof(struct xfs_btree_block
, bb_u
.l
.bb_lsn
),
1399 offsetof(struct xfs_btree_block
, bb_u
.l
.bb_uuid
),
1400 offsetof(struct xfs_btree_block
, bb_u
.l
.bb_owner
),
1401 offsetof(struct xfs_btree_block
, bb_u
.l
.bb_crc
),
1402 offsetof(struct xfs_btree_block
, bb_u
.l
.bb_pad
),
1403 XFS_BTREE_LBLOCK_CRC_LEN
1406 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
1407 XFS_BTREE_TRACE_ARGBI(cur
, bp
, fields
);
1412 if (cur
->bc_flags
& XFS_BTREE_CRC_BLOCKS
) {
1414 * We don't log the CRC when updating a btree
1415 * block but instead recreate it during log
1416 * recovery. As the log buffers have checksums
1417 * of their own this is safe and avoids logging a crc
1418 * update in a lot of places.
1420 if (fields
== XFS_BB_ALL_BITS
)
1421 fields
= XFS_BB_ALL_BITS_CRC
;
1422 nbits
= XFS_BB_NUM_BITS_CRC
;
1424 nbits
= XFS_BB_NUM_BITS
;
1426 xfs_btree_offsets(fields
,
1427 (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
) ?
1428 loffsets
: soffsets
,
1429 nbits
, &first
, &last
);
1430 xfs_trans_buf_set_type(cur
->bc_tp
, bp
, XFS_BLFT_BTREE_BUF
);
1431 xfs_trans_log_buf(cur
->bc_tp
, bp
, first
, last
);
1433 xfs_trans_log_inode(cur
->bc_tp
, cur
->bc_private
.b
.ip
,
1434 xfs_ilog_fbroot(cur
->bc_private
.b
.whichfork
));
1437 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
1441 * Increment cursor by one record at the level.
1442 * For nonzero levels the leaf-ward information is untouched.
1445 xfs_btree_increment(
1446 struct xfs_btree_cur
*cur
,
1448 int *stat
) /* success/failure */
1450 struct xfs_btree_block
*block
;
1451 union xfs_btree_ptr ptr
;
1453 int error
; /* error return value */
1456 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
1457 XFS_BTREE_TRACE_ARGI(cur
, level
);
1459 ASSERT(level
< cur
->bc_nlevels
);
1461 /* Read-ahead to the right at this level. */
1462 xfs_btree_readahead(cur
, level
, XFS_BTCUR_RIGHTRA
);
1464 /* Get a pointer to the btree block. */
1465 block
= xfs_btree_get_block(cur
, level
, &bp
);
1468 error
= xfs_btree_check_block(cur
, block
, level
, bp
);
1473 /* We're done if we remain in the block after the increment. */
1474 if (++cur
->bc_ptrs
[level
] <= xfs_btree_get_numrecs(block
))
1477 /* Fail if we just went off the right edge of the tree. */
1478 xfs_btree_get_sibling(cur
, block
, &ptr
, XFS_BB_RIGHTSIB
);
1479 if (xfs_btree_ptr_is_null(cur
, &ptr
))
1482 XFS_BTREE_STATS_INC(cur
, increment
);
1485 * March up the tree incrementing pointers.
1486 * Stop when we don't go off the right edge of a block.
1488 for (lev
= level
+ 1; lev
< cur
->bc_nlevels
; lev
++) {
1489 block
= xfs_btree_get_block(cur
, lev
, &bp
);
1492 error
= xfs_btree_check_block(cur
, block
, lev
, bp
);
1497 if (++cur
->bc_ptrs
[lev
] <= xfs_btree_get_numrecs(block
))
1500 /* Read-ahead the right block for the next loop. */
1501 xfs_btree_readahead(cur
, lev
, XFS_BTCUR_RIGHTRA
);
1505 * If we went off the root then we are either seriously
1506 * confused or have the tree root in an inode.
1508 if (lev
== cur
->bc_nlevels
) {
1509 if (cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
)
1512 error
= EFSCORRUPTED
;
1515 ASSERT(lev
< cur
->bc_nlevels
);
1518 * Now walk back down the tree, fixing up the cursor's buffer
1519 * pointers and key numbers.
1521 for (block
= xfs_btree_get_block(cur
, lev
, &bp
); lev
> level
; ) {
1522 union xfs_btree_ptr
*ptrp
;
1524 ptrp
= xfs_btree_ptr_addr(cur
, cur
->bc_ptrs
[lev
], block
);
1525 error
= xfs_btree_read_buf_block(cur
, ptrp
, --lev
,
1530 xfs_btree_setbuf(cur
, lev
, bp
);
1531 cur
->bc_ptrs
[lev
] = 1;
1534 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
1539 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
1544 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
1549 * Decrement cursor by one record at the level.
1550 * For nonzero levels the leaf-ward information is untouched.
1553 xfs_btree_decrement(
1554 struct xfs_btree_cur
*cur
,
1556 int *stat
) /* success/failure */
1558 struct xfs_btree_block
*block
;
1560 int error
; /* error return value */
1562 union xfs_btree_ptr ptr
;
1564 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
1565 XFS_BTREE_TRACE_ARGI(cur
, level
);
1567 ASSERT(level
< cur
->bc_nlevels
);
1569 /* Read-ahead to the left at this level. */
1570 xfs_btree_readahead(cur
, level
, XFS_BTCUR_LEFTRA
);
1572 /* We're done if we remain in the block after the decrement. */
1573 if (--cur
->bc_ptrs
[level
] > 0)
1576 /* Get a pointer to the btree block. */
1577 block
= xfs_btree_get_block(cur
, level
, &bp
);
1580 error
= xfs_btree_check_block(cur
, block
, level
, bp
);
1585 /* Fail if we just went off the left edge of the tree. */
1586 xfs_btree_get_sibling(cur
, block
, &ptr
, XFS_BB_LEFTSIB
);
1587 if (xfs_btree_ptr_is_null(cur
, &ptr
))
1590 XFS_BTREE_STATS_INC(cur
, decrement
);
1593 * March up the tree decrementing pointers.
1594 * Stop when we don't go off the left edge of a block.
1596 for (lev
= level
+ 1; lev
< cur
->bc_nlevels
; lev
++) {
1597 if (--cur
->bc_ptrs
[lev
] > 0)
1599 /* Read-ahead the left block for the next loop. */
1600 xfs_btree_readahead(cur
, lev
, XFS_BTCUR_LEFTRA
);
1604 * If we went off the root then we are seriously confused.
1605 * or the root of the tree is in an inode.
1607 if (lev
== cur
->bc_nlevels
) {
1608 if (cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
)
1611 error
= EFSCORRUPTED
;
1614 ASSERT(lev
< cur
->bc_nlevels
);
1617 * Now walk back down the tree, fixing up the cursor's buffer
1618 * pointers and key numbers.
1620 for (block
= xfs_btree_get_block(cur
, lev
, &bp
); lev
> level
; ) {
1621 union xfs_btree_ptr
*ptrp
;
1623 ptrp
= xfs_btree_ptr_addr(cur
, cur
->bc_ptrs
[lev
], block
);
1624 error
= xfs_btree_read_buf_block(cur
, ptrp
, --lev
,
1628 xfs_btree_setbuf(cur
, lev
, bp
);
1629 cur
->bc_ptrs
[lev
] = xfs_btree_get_numrecs(block
);
1632 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
1637 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
1642 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
1647 xfs_btree_lookup_get_block(
1648 struct xfs_btree_cur
*cur
, /* btree cursor */
1649 int level
, /* level in the btree */
1650 union xfs_btree_ptr
*pp
, /* ptr to btree block */
1651 struct xfs_btree_block
**blkp
) /* return btree block */
1653 struct xfs_buf
*bp
; /* buffer pointer for btree block */
1656 /* special case the root block if in an inode */
1657 if ((cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
) &&
1658 (level
== cur
->bc_nlevels
- 1)) {
1659 *blkp
= xfs_btree_get_iroot(cur
);
1664 * If the old buffer at this level for the disk address we are
1665 * looking for re-use it.
1667 * Otherwise throw it away and get a new one.
1669 bp
= cur
->bc_bufs
[level
];
1670 if (bp
&& XFS_BUF_ADDR(bp
) == xfs_btree_ptr_to_daddr(cur
, pp
)) {
1671 *blkp
= XFS_BUF_TO_BLOCK(bp
);
1675 error
= xfs_btree_read_buf_block(cur
, pp
, level
, 0, blkp
, &bp
);
1679 xfs_btree_setbuf(cur
, level
, bp
);
1684 * Get current search key. For level 0 we don't actually have a key
1685 * structure so we make one up from the record. For all other levels
1686 * we just return the right key.
1688 STATIC
union xfs_btree_key
*
1689 xfs_lookup_get_search_key(
1690 struct xfs_btree_cur
*cur
,
1693 struct xfs_btree_block
*block
,
1694 union xfs_btree_key
*kp
)
1697 cur
->bc_ops
->init_key_from_rec(kp
,
1698 xfs_btree_rec_addr(cur
, keyno
, block
));
1702 return xfs_btree_key_addr(cur
, keyno
, block
);
1706 * Lookup the record. The cursor is made to point to it, based on dir.
1707 * stat is set to 0 if can't find any such record, 1 for success.
1711 struct xfs_btree_cur
*cur
, /* btree cursor */
1712 xfs_lookup_t dir
, /* <=, ==, or >= */
1713 int *stat
) /* success/failure */
1715 struct xfs_btree_block
*block
; /* current btree block */
1716 __int64_t diff
; /* difference for the current key */
1717 int error
; /* error return value */
1718 int keyno
; /* current key number */
1719 int level
; /* level in the btree */
1720 union xfs_btree_ptr
*pp
; /* ptr to btree block */
1721 union xfs_btree_ptr ptr
; /* ptr to btree block */
1723 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
1724 XFS_BTREE_TRACE_ARGI(cur
, dir
);
1726 XFS_BTREE_STATS_INC(cur
, lookup
);
1731 /* initialise start pointer from cursor */
1732 cur
->bc_ops
->init_ptr_from_cur(cur
, &ptr
);
1736 * Iterate over each level in the btree, starting at the root.
1737 * For each level above the leaves, find the key we need, based
1738 * on the lookup record, then follow the corresponding block
1739 * pointer down to the next level.
1741 for (level
= cur
->bc_nlevels
- 1, diff
= 1; level
>= 0; level
--) {
1742 /* Get the block we need to do the lookup on. */
1743 error
= xfs_btree_lookup_get_block(cur
, level
, pp
, &block
);
1749 * If we already had a key match at a higher level, we
1750 * know we need to use the first entry in this block.
1754 /* Otherwise search this block. Do a binary search. */
1756 int high
; /* high entry number */
1757 int low
; /* low entry number */
1759 /* Set low and high entry numbers, 1-based. */
1761 high
= xfs_btree_get_numrecs(block
);
1763 /* Block is empty, must be an empty leaf. */
1764 ASSERT(level
== 0 && cur
->bc_nlevels
== 1);
1766 cur
->bc_ptrs
[0] = dir
!= XFS_LOOKUP_LE
;
1767 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
1772 /* Binary search the block. */
1773 while (low
<= high
) {
1774 union xfs_btree_key key
;
1775 union xfs_btree_key
*kp
;
1777 XFS_BTREE_STATS_INC(cur
, compare
);
1779 /* keyno is average of low and high. */
1780 keyno
= (low
+ high
) >> 1;
1782 /* Get current search key */
1783 kp
= xfs_lookup_get_search_key(cur
, level
,
1784 keyno
, block
, &key
);
1787 * Compute difference to get next direction:
1788 * - less than, move right
1789 * - greater than, move left
1790 * - equal, we're done
1792 diff
= cur
->bc_ops
->key_diff(cur
, kp
);
1803 * If there are more levels, set up for the next level
1804 * by getting the block number and filling in the cursor.
1808 * If we moved left, need the previous key number,
1809 * unless there isn't one.
1811 if (diff
> 0 && --keyno
< 1)
1813 pp
= xfs_btree_ptr_addr(cur
, keyno
, block
);
1816 error
= xfs_btree_check_ptr(cur
, pp
, 0, level
);
1820 cur
->bc_ptrs
[level
] = keyno
;
1824 /* Done with the search. See if we need to adjust the results. */
1825 if (dir
!= XFS_LOOKUP_LE
&& diff
< 0) {
1828 * If ge search and we went off the end of the block, but it's
1829 * not the last block, we're in the wrong block.
1831 xfs_btree_get_sibling(cur
, block
, &ptr
, XFS_BB_RIGHTSIB
);
1832 if (dir
== XFS_LOOKUP_GE
&&
1833 keyno
> xfs_btree_get_numrecs(block
) &&
1834 !xfs_btree_ptr_is_null(cur
, &ptr
)) {
1837 cur
->bc_ptrs
[0] = keyno
;
1838 error
= xfs_btree_increment(cur
, 0, &i
);
1841 XFS_WANT_CORRUPTED_RETURN(i
== 1);
1842 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
1846 } else if (dir
== XFS_LOOKUP_LE
&& diff
> 0)
1848 cur
->bc_ptrs
[0] = keyno
;
1850 /* Return if we succeeded or not. */
1851 if (keyno
== 0 || keyno
> xfs_btree_get_numrecs(block
))
1853 else if (dir
!= XFS_LOOKUP_EQ
|| diff
== 0)
1857 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
1861 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
1866 * Update keys at all levels from here to the root along the cursor's path.
1870 struct xfs_btree_cur
*cur
,
1871 union xfs_btree_key
*keyp
,
1874 struct xfs_btree_block
*block
;
1876 union xfs_btree_key
*kp
;
1879 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
1880 XFS_BTREE_TRACE_ARGIK(cur
, level
, keyp
);
1882 ASSERT(!(cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
) || level
>= 1);
1885 * Go up the tree from this level toward the root.
1886 * At each level, update the key value to the value input.
1887 * Stop when we reach a level where the cursor isn't pointing
1888 * at the first entry in the block.
1890 for (ptr
= 1; ptr
== 1 && level
< cur
->bc_nlevels
; level
++) {
1894 block
= xfs_btree_get_block(cur
, level
, &bp
);
1896 error
= xfs_btree_check_block(cur
, block
, level
, bp
);
1898 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
1902 ptr
= cur
->bc_ptrs
[level
];
1903 kp
= xfs_btree_key_addr(cur
, ptr
, block
);
1904 xfs_btree_copy_keys(cur
, kp
, keyp
, 1);
1905 xfs_btree_log_keys(cur
, bp
, ptr
, ptr
);
1908 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
1913 * Update the record referred to by cur to the value in the
1914 * given record. This either works (return 0) or gets an
1915 * EFSCORRUPTED error.
1919 struct xfs_btree_cur
*cur
,
1920 union xfs_btree_rec
*rec
)
1922 struct xfs_btree_block
*block
;
1926 union xfs_btree_rec
*rp
;
1928 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
1929 XFS_BTREE_TRACE_ARGR(cur
, rec
);
1931 /* Pick up the current block. */
1932 block
= xfs_btree_get_block(cur
, 0, &bp
);
1935 error
= xfs_btree_check_block(cur
, block
, 0, bp
);
1939 /* Get the address of the rec to be updated. */
1940 ptr
= cur
->bc_ptrs
[0];
1941 rp
= xfs_btree_rec_addr(cur
, ptr
, block
);
1943 /* Fill in the new contents and log them. */
1944 xfs_btree_copy_recs(cur
, rp
, rec
, 1);
1945 xfs_btree_log_recs(cur
, bp
, ptr
, ptr
);
1948 * If we are tracking the last record in the tree and
1949 * we are at the far right edge of the tree, update it.
1951 if (xfs_btree_is_lastrec(cur
, block
, 0)) {
1952 cur
->bc_ops
->update_lastrec(cur
, block
, rec
,
1953 ptr
, LASTREC_UPDATE
);
1956 /* Updating first rec in leaf. Pass new key value up to our parent. */
1958 union xfs_btree_key key
;
1960 cur
->bc_ops
->init_key_from_rec(&key
, rec
);
1961 error
= xfs_btree_updkey(cur
, &key
, 1);
1966 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
1970 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
1975 * Move 1 record left from cur/level if possible.
1976 * Update cur to reflect the new path.
1978 STATIC
int /* error */
1980 struct xfs_btree_cur
*cur
,
1982 int *stat
) /* success/failure */
1984 union xfs_btree_key key
; /* btree key */
1985 struct xfs_buf
*lbp
; /* left buffer pointer */
1986 struct xfs_btree_block
*left
; /* left btree block */
1987 int lrecs
; /* left record count */
1988 struct xfs_buf
*rbp
; /* right buffer pointer */
1989 struct xfs_btree_block
*right
; /* right btree block */
1990 int rrecs
; /* right record count */
1991 union xfs_btree_ptr lptr
; /* left btree pointer */
1992 union xfs_btree_key
*rkp
= NULL
; /* right btree key */
1993 union xfs_btree_ptr
*rpp
= NULL
; /* right address pointer */
1994 union xfs_btree_rec
*rrp
= NULL
; /* right record pointer */
1995 int error
; /* error return value */
1997 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
1998 XFS_BTREE_TRACE_ARGI(cur
, level
);
2000 if ((cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
) &&
2001 level
== cur
->bc_nlevels
- 1)
2004 /* Set up variables for this block as "right". */
2005 right
= xfs_btree_get_block(cur
, level
, &rbp
);
2008 error
= xfs_btree_check_block(cur
, right
, level
, rbp
);
2013 /* If we've got no left sibling then we can't shift an entry left. */
2014 xfs_btree_get_sibling(cur
, right
, &lptr
, XFS_BB_LEFTSIB
);
2015 if (xfs_btree_ptr_is_null(cur
, &lptr
))
2019 * If the cursor entry is the one that would be moved, don't
2020 * do it... it's too complicated.
2022 if (cur
->bc_ptrs
[level
] <= 1)
2025 /* Set up the left neighbor as "left". */
2026 error
= xfs_btree_read_buf_block(cur
, &lptr
, level
, 0, &left
, &lbp
);
2030 /* If it's full, it can't take another entry. */
2031 lrecs
= xfs_btree_get_numrecs(left
);
2032 if (lrecs
== cur
->bc_ops
->get_maxrecs(cur
, level
))
2035 rrecs
= xfs_btree_get_numrecs(right
);
2038 * We add one entry to the left side and remove one for the right side.
2039 * Account for it here, the changes will be updated on disk and logged
2045 XFS_BTREE_STATS_INC(cur
, lshift
);
2046 XFS_BTREE_STATS_ADD(cur
, moves
, 1);
2049 * If non-leaf, copy a key and a ptr to the left block.
2050 * Log the changes to the left block.
2053 /* It's a non-leaf. Move keys and pointers. */
2054 union xfs_btree_key
*lkp
; /* left btree key */
2055 union xfs_btree_ptr
*lpp
; /* left address pointer */
2057 lkp
= xfs_btree_key_addr(cur
, lrecs
, left
);
2058 rkp
= xfs_btree_key_addr(cur
, 1, right
);
2060 lpp
= xfs_btree_ptr_addr(cur
, lrecs
, left
);
2061 rpp
= xfs_btree_ptr_addr(cur
, 1, right
);
2063 error
= xfs_btree_check_ptr(cur
, rpp
, 0, level
);
2067 xfs_btree_copy_keys(cur
, lkp
, rkp
, 1);
2068 xfs_btree_copy_ptrs(cur
, lpp
, rpp
, 1);
2070 xfs_btree_log_keys(cur
, lbp
, lrecs
, lrecs
);
2071 xfs_btree_log_ptrs(cur
, lbp
, lrecs
, lrecs
);
2073 ASSERT(cur
->bc_ops
->keys_inorder(cur
,
2074 xfs_btree_key_addr(cur
, lrecs
- 1, left
), lkp
));
2076 /* It's a leaf. Move records. */
2077 union xfs_btree_rec
*lrp
; /* left record pointer */
2079 lrp
= xfs_btree_rec_addr(cur
, lrecs
, left
);
2080 rrp
= xfs_btree_rec_addr(cur
, 1, right
);
2082 xfs_btree_copy_recs(cur
, lrp
, rrp
, 1);
2083 xfs_btree_log_recs(cur
, lbp
, lrecs
, lrecs
);
2085 ASSERT(cur
->bc_ops
->recs_inorder(cur
,
2086 xfs_btree_rec_addr(cur
, lrecs
- 1, left
), lrp
));
2089 xfs_btree_set_numrecs(left
, lrecs
);
2090 xfs_btree_log_block(cur
, lbp
, XFS_BB_NUMRECS
);
2092 xfs_btree_set_numrecs(right
, rrecs
);
2093 xfs_btree_log_block(cur
, rbp
, XFS_BB_NUMRECS
);
2096 * Slide the contents of right down one entry.
2098 XFS_BTREE_STATS_ADD(cur
, moves
, rrecs
- 1);
2100 /* It's a nonleaf. operate on keys and ptrs */
2102 int i
; /* loop index */
2104 for (i
= 0; i
< rrecs
; i
++) {
2105 error
= xfs_btree_check_ptr(cur
, rpp
, i
+ 1, level
);
2110 xfs_btree_shift_keys(cur
,
2111 xfs_btree_key_addr(cur
, 2, right
),
2113 xfs_btree_shift_ptrs(cur
,
2114 xfs_btree_ptr_addr(cur
, 2, right
),
2117 xfs_btree_log_keys(cur
, rbp
, 1, rrecs
);
2118 xfs_btree_log_ptrs(cur
, rbp
, 1, rrecs
);
2120 /* It's a leaf. operate on records */
2121 xfs_btree_shift_recs(cur
,
2122 xfs_btree_rec_addr(cur
, 2, right
),
2124 xfs_btree_log_recs(cur
, rbp
, 1, rrecs
);
2127 * If it's the first record in the block, we'll need a key
2128 * structure to pass up to the next level (updkey).
2130 cur
->bc_ops
->init_key_from_rec(&key
,
2131 xfs_btree_rec_addr(cur
, 1, right
));
2135 /* Update the parent key values of right. */
2136 error
= xfs_btree_updkey(cur
, rkp
, level
+ 1);
2140 /* Slide the cursor value left one. */
2141 cur
->bc_ptrs
[level
]--;
2143 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
2148 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
2153 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
2158 * Move 1 record right from cur/level if possible.
2159 * Update cur to reflect the new path.
2161 STATIC
int /* error */
2163 struct xfs_btree_cur
*cur
,
2165 int *stat
) /* success/failure */
2167 union xfs_btree_key key
; /* btree key */
2168 struct xfs_buf
*lbp
; /* left buffer pointer */
2169 struct xfs_btree_block
*left
; /* left btree block */
2170 struct xfs_buf
*rbp
; /* right buffer pointer */
2171 struct xfs_btree_block
*right
; /* right btree block */
2172 struct xfs_btree_cur
*tcur
; /* temporary btree cursor */
2173 union xfs_btree_ptr rptr
; /* right block pointer */
2174 union xfs_btree_key
*rkp
; /* right btree key */
2175 int rrecs
; /* right record count */
2176 int lrecs
; /* left record count */
2177 int error
; /* error return value */
2178 int i
; /* loop counter */
2180 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
2181 XFS_BTREE_TRACE_ARGI(cur
, level
);
2183 if ((cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
) &&
2184 (level
== cur
->bc_nlevels
- 1))
2187 /* Set up variables for this block as "left". */
2188 left
= xfs_btree_get_block(cur
, level
, &lbp
);
2191 error
= xfs_btree_check_block(cur
, left
, level
, lbp
);
2196 /* If we've got no right sibling then we can't shift an entry right. */
2197 xfs_btree_get_sibling(cur
, left
, &rptr
, XFS_BB_RIGHTSIB
);
2198 if (xfs_btree_ptr_is_null(cur
, &rptr
))
2202 * If the cursor entry is the one that would be moved, don't
2203 * do it... it's too complicated.
2205 lrecs
= xfs_btree_get_numrecs(left
);
2206 if (cur
->bc_ptrs
[level
] >= lrecs
)
2209 /* Set up the right neighbor as "right". */
2210 error
= xfs_btree_read_buf_block(cur
, &rptr
, level
, 0, &right
, &rbp
);
2214 /* If it's full, it can't take another entry. */
2215 rrecs
= xfs_btree_get_numrecs(right
);
2216 if (rrecs
== cur
->bc_ops
->get_maxrecs(cur
, level
))
2219 XFS_BTREE_STATS_INC(cur
, rshift
);
2220 XFS_BTREE_STATS_ADD(cur
, moves
, rrecs
);
2223 * Make a hole at the start of the right neighbor block, then
2224 * copy the last left block entry to the hole.
2227 /* It's a nonleaf. make a hole in the keys and ptrs */
2228 union xfs_btree_key
*lkp
;
2229 union xfs_btree_ptr
*lpp
;
2230 union xfs_btree_ptr
*rpp
;
2232 lkp
= xfs_btree_key_addr(cur
, lrecs
, left
);
2233 lpp
= xfs_btree_ptr_addr(cur
, lrecs
, left
);
2234 rkp
= xfs_btree_key_addr(cur
, 1, right
);
2235 rpp
= xfs_btree_ptr_addr(cur
, 1, right
);
2238 for (i
= rrecs
- 1; i
>= 0; i
--) {
2239 error
= xfs_btree_check_ptr(cur
, rpp
, i
, level
);
2245 xfs_btree_shift_keys(cur
, rkp
, 1, rrecs
);
2246 xfs_btree_shift_ptrs(cur
, rpp
, 1, rrecs
);
2249 error
= xfs_btree_check_ptr(cur
, lpp
, 0, level
);
2254 /* Now put the new data in, and log it. */
2255 xfs_btree_copy_keys(cur
, rkp
, lkp
, 1);
2256 xfs_btree_copy_ptrs(cur
, rpp
, lpp
, 1);
2258 xfs_btree_log_keys(cur
, rbp
, 1, rrecs
+ 1);
2259 xfs_btree_log_ptrs(cur
, rbp
, 1, rrecs
+ 1);
2261 ASSERT(cur
->bc_ops
->keys_inorder(cur
, rkp
,
2262 xfs_btree_key_addr(cur
, 2, right
)));
2264 /* It's a leaf. make a hole in the records */
2265 union xfs_btree_rec
*lrp
;
2266 union xfs_btree_rec
*rrp
;
2268 lrp
= xfs_btree_rec_addr(cur
, lrecs
, left
);
2269 rrp
= xfs_btree_rec_addr(cur
, 1, right
);
2271 xfs_btree_shift_recs(cur
, rrp
, 1, rrecs
);
2273 /* Now put the new data in, and log it. */
2274 xfs_btree_copy_recs(cur
, rrp
, lrp
, 1);
2275 xfs_btree_log_recs(cur
, rbp
, 1, rrecs
+ 1);
2277 cur
->bc_ops
->init_key_from_rec(&key
, rrp
);
2280 ASSERT(cur
->bc_ops
->recs_inorder(cur
, rrp
,
2281 xfs_btree_rec_addr(cur
, 2, right
)));
2285 * Decrement and log left's numrecs, bump and log right's numrecs.
2287 xfs_btree_set_numrecs(left
, --lrecs
);
2288 xfs_btree_log_block(cur
, lbp
, XFS_BB_NUMRECS
);
2290 xfs_btree_set_numrecs(right
, ++rrecs
);
2291 xfs_btree_log_block(cur
, rbp
, XFS_BB_NUMRECS
);
2294 * Using a temporary cursor, update the parent key values of the
2295 * block on the right.
2297 error
= xfs_btree_dup_cursor(cur
, &tcur
);
2300 i
= xfs_btree_lastrec(tcur
, level
);
2301 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
2303 error
= xfs_btree_increment(tcur
, level
, &i
);
2307 error
= xfs_btree_updkey(tcur
, rkp
, level
+ 1);
2311 xfs_btree_del_cursor(tcur
, XFS_BTREE_NOERROR
);
2313 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
2318 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
2323 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
2327 XFS_BTREE_TRACE_CURSOR(tcur
, XBT_ERROR
);
2328 xfs_btree_del_cursor(tcur
, XFS_BTREE_ERROR
);
2333 * Split cur/level block in half.
2334 * Return new block number and the key to its first
2335 * record (to be inserted into parent).
2337 STATIC
int /* error */
2339 struct xfs_btree_cur
*cur
,
2341 union xfs_btree_ptr
*ptrp
,
2342 union xfs_btree_key
*key
,
2343 struct xfs_btree_cur
**curp
,
2344 int *stat
) /* success/failure */
2346 union xfs_btree_ptr lptr
; /* left sibling block ptr */
2347 struct xfs_buf
*lbp
; /* left buffer pointer */
2348 struct xfs_btree_block
*left
; /* left btree block */
2349 union xfs_btree_ptr rptr
; /* right sibling block ptr */
2350 struct xfs_buf
*rbp
; /* right buffer pointer */
2351 struct xfs_btree_block
*right
; /* right btree block */
2352 union xfs_btree_ptr rrptr
; /* right-right sibling ptr */
2353 struct xfs_buf
*rrbp
; /* right-right buffer pointer */
2354 struct xfs_btree_block
*rrblock
; /* right-right btree block */
2358 int error
; /* error return value */
2363 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
2364 XFS_BTREE_TRACE_ARGIPK(cur
, level
, *ptrp
, key
);
2366 XFS_BTREE_STATS_INC(cur
, split
);
2368 /* Set up left block (current one). */
2369 left
= xfs_btree_get_block(cur
, level
, &lbp
);
2372 error
= xfs_btree_check_block(cur
, left
, level
, lbp
);
2377 xfs_btree_buf_to_ptr(cur
, lbp
, &lptr
);
2379 /* Allocate the new block. If we can't do it, we're toast. Give up. */
2380 error
= cur
->bc_ops
->alloc_block(cur
, &lptr
, &rptr
, 1, stat
);
2385 XFS_BTREE_STATS_INC(cur
, alloc
);
2387 /* Set up the new block as "right". */
2388 error
= xfs_btree_get_buf_block(cur
, &rptr
, 0, &right
, &rbp
);
2392 /* Fill in the btree header for the new right block. */
2393 xfs_btree_init_block_cur(cur
, rbp
, xfs_btree_get_level(left
), 0);
2396 * Split the entries between the old and the new block evenly.
2397 * Make sure that if there's an odd number of entries now, that
2398 * each new block will have the same number of entries.
2400 lrecs
= xfs_btree_get_numrecs(left
);
2402 if ((lrecs
& 1) && cur
->bc_ptrs
[level
] <= rrecs
+ 1)
2404 src_index
= (lrecs
- rrecs
+ 1);
2406 XFS_BTREE_STATS_ADD(cur
, moves
, rrecs
);
2409 * Copy btree block entries from the left block over to the
2410 * new block, the right. Update the right block and log the
2414 /* It's a non-leaf. Move keys and pointers. */
2415 union xfs_btree_key
*lkp
; /* left btree key */
2416 union xfs_btree_ptr
*lpp
; /* left address pointer */
2417 union xfs_btree_key
*rkp
; /* right btree key */
2418 union xfs_btree_ptr
*rpp
; /* right address pointer */
2420 lkp
= xfs_btree_key_addr(cur
, src_index
, left
);
2421 lpp
= xfs_btree_ptr_addr(cur
, src_index
, left
);
2422 rkp
= xfs_btree_key_addr(cur
, 1, right
);
2423 rpp
= xfs_btree_ptr_addr(cur
, 1, right
);
2426 for (i
= src_index
; i
< rrecs
; i
++) {
2427 error
= xfs_btree_check_ptr(cur
, lpp
, i
, level
);
2433 xfs_btree_copy_keys(cur
, rkp
, lkp
, rrecs
);
2434 xfs_btree_copy_ptrs(cur
, rpp
, lpp
, rrecs
);
2436 xfs_btree_log_keys(cur
, rbp
, 1, rrecs
);
2437 xfs_btree_log_ptrs(cur
, rbp
, 1, rrecs
);
2439 /* Grab the keys to the entries moved to the right block */
2440 xfs_btree_copy_keys(cur
, key
, rkp
, 1);
2442 /* It's a leaf. Move records. */
2443 union xfs_btree_rec
*lrp
; /* left record pointer */
2444 union xfs_btree_rec
*rrp
; /* right record pointer */
2446 lrp
= xfs_btree_rec_addr(cur
, src_index
, left
);
2447 rrp
= xfs_btree_rec_addr(cur
, 1, right
);
2449 xfs_btree_copy_recs(cur
, rrp
, lrp
, rrecs
);
2450 xfs_btree_log_recs(cur
, rbp
, 1, rrecs
);
2452 cur
->bc_ops
->init_key_from_rec(key
,
2453 xfs_btree_rec_addr(cur
, 1, right
));
2458 * Find the left block number by looking in the buffer.
2459 * Adjust numrecs, sibling pointers.
2461 xfs_btree_get_sibling(cur
, left
, &rrptr
, XFS_BB_RIGHTSIB
);
2462 xfs_btree_set_sibling(cur
, right
, &rrptr
, XFS_BB_RIGHTSIB
);
2463 xfs_btree_set_sibling(cur
, right
, &lptr
, XFS_BB_LEFTSIB
);
2464 xfs_btree_set_sibling(cur
, left
, &rptr
, XFS_BB_RIGHTSIB
);
2467 xfs_btree_set_numrecs(left
, lrecs
);
2468 xfs_btree_set_numrecs(right
, xfs_btree_get_numrecs(right
) + rrecs
);
2470 xfs_btree_log_block(cur
, rbp
, XFS_BB_ALL_BITS
);
2471 xfs_btree_log_block(cur
, lbp
, XFS_BB_NUMRECS
| XFS_BB_RIGHTSIB
);
2474 * If there's a block to the new block's right, make that block
2475 * point back to right instead of to left.
2477 if (!xfs_btree_ptr_is_null(cur
, &rrptr
)) {
2478 error
= xfs_btree_read_buf_block(cur
, &rrptr
, level
,
2479 0, &rrblock
, &rrbp
);
2482 xfs_btree_set_sibling(cur
, rrblock
, &rptr
, XFS_BB_LEFTSIB
);
2483 xfs_btree_log_block(cur
, rrbp
, XFS_BB_LEFTSIB
);
2486 * If the cursor is really in the right block, move it there.
2487 * If it's just pointing past the last entry in left, then we'll
2488 * insert there, so don't change anything in that case.
2490 if (cur
->bc_ptrs
[level
] > lrecs
+ 1) {
2491 xfs_btree_setbuf(cur
, level
, rbp
);
2492 cur
->bc_ptrs
[level
] -= lrecs
;
2495 * If there are more levels, we'll need another cursor which refers
2496 * the right block, no matter where this cursor was.
2498 if (level
+ 1 < cur
->bc_nlevels
) {
2499 error
= xfs_btree_dup_cursor(cur
, curp
);
2502 (*curp
)->bc_ptrs
[level
+ 1]++;
2505 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
2509 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
2514 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
2519 * Copy the old inode root contents into a real block and make the
2520 * broot point to it.
2523 xfs_btree_new_iroot(
2524 struct xfs_btree_cur
*cur
, /* btree cursor */
2525 int *logflags
, /* logging flags for inode */
2526 int *stat
) /* return status - 0 fail */
2528 struct xfs_buf
*cbp
; /* buffer for cblock */
2529 struct xfs_btree_block
*block
; /* btree block */
2530 struct xfs_btree_block
*cblock
; /* child btree block */
2531 union xfs_btree_key
*ckp
; /* child key pointer */
2532 union xfs_btree_ptr
*cpp
; /* child ptr pointer */
2533 union xfs_btree_key
*kp
; /* pointer to btree key */
2534 union xfs_btree_ptr
*pp
; /* pointer to block addr */
2535 union xfs_btree_ptr nptr
; /* new block addr */
2536 int level
; /* btree level */
2537 int error
; /* error return code */
2539 int i
; /* loop counter */
2542 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
2543 XFS_BTREE_STATS_INC(cur
, newroot
);
2545 ASSERT(cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
);
2547 level
= cur
->bc_nlevels
- 1;
2549 block
= xfs_btree_get_iroot(cur
);
2550 pp
= xfs_btree_ptr_addr(cur
, 1, block
);
2552 /* Allocate the new block. If we can't do it, we're toast. Give up. */
2553 error
= cur
->bc_ops
->alloc_block(cur
, pp
, &nptr
, 1, stat
);
2557 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
2560 XFS_BTREE_STATS_INC(cur
, alloc
);
2562 /* Copy the root into a real block. */
2563 error
= xfs_btree_get_buf_block(cur
, &nptr
, 0, &cblock
, &cbp
);
2568 * we can't just memcpy() the root in for CRC enabled btree blocks.
2569 * In that case have to also ensure the blkno remains correct
2571 memcpy(cblock
, block
, xfs_btree_block_len(cur
));
2572 if (cur
->bc_flags
& XFS_BTREE_CRC_BLOCKS
) {
2573 if (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
)
2574 cblock
->bb_u
.l
.bb_blkno
= cpu_to_be64(cbp
->b_bn
);
2576 cblock
->bb_u
.s
.bb_blkno
= cpu_to_be64(cbp
->b_bn
);
2579 be16_add_cpu(&block
->bb_level
, 1);
2580 xfs_btree_set_numrecs(block
, 1);
2582 cur
->bc_ptrs
[level
+ 1] = 1;
2584 kp
= xfs_btree_key_addr(cur
, 1, block
);
2585 ckp
= xfs_btree_key_addr(cur
, 1, cblock
);
2586 xfs_btree_copy_keys(cur
, ckp
, kp
, xfs_btree_get_numrecs(cblock
));
2588 cpp
= xfs_btree_ptr_addr(cur
, 1, cblock
);
2590 for (i
= 0; i
< be16_to_cpu(cblock
->bb_numrecs
); i
++) {
2591 error
= xfs_btree_check_ptr(cur
, pp
, i
, level
);
2596 xfs_btree_copy_ptrs(cur
, cpp
, pp
, xfs_btree_get_numrecs(cblock
));
2599 error
= xfs_btree_check_ptr(cur
, &nptr
, 0, level
);
2603 xfs_btree_copy_ptrs(cur
, pp
, &nptr
, 1);
2605 xfs_iroot_realloc(cur
->bc_private
.b
.ip
,
2606 1 - xfs_btree_get_numrecs(cblock
),
2607 cur
->bc_private
.b
.whichfork
);
2609 xfs_btree_setbuf(cur
, level
, cbp
);
2612 * Do all this logging at the end so that
2613 * the root is at the right level.
2615 xfs_btree_log_block(cur
, cbp
, XFS_BB_ALL_BITS
);
2616 xfs_btree_log_keys(cur
, cbp
, 1, be16_to_cpu(cblock
->bb_numrecs
));
2617 xfs_btree_log_ptrs(cur
, cbp
, 1, be16_to_cpu(cblock
->bb_numrecs
));
2620 XFS_ILOG_CORE
| xfs_ilog_fbroot(cur
->bc_private
.b
.whichfork
);
2622 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
2625 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
2630 * Allocate a new root block, fill it in.
2632 STATIC
int /* error */
2634 struct xfs_btree_cur
*cur
, /* btree cursor */
2635 int *stat
) /* success/failure */
2637 struct xfs_btree_block
*block
; /* one half of the old root block */
2638 struct xfs_buf
*bp
; /* buffer containing block */
2639 int error
; /* error return value */
2640 struct xfs_buf
*lbp
; /* left buffer pointer */
2641 struct xfs_btree_block
*left
; /* left btree block */
2642 struct xfs_buf
*nbp
; /* new (root) buffer */
2643 struct xfs_btree_block
*new; /* new (root) btree block */
2644 int nptr
; /* new value for key index, 1 or 2 */
2645 struct xfs_buf
*rbp
; /* right buffer pointer */
2646 struct xfs_btree_block
*right
; /* right btree block */
2647 union xfs_btree_ptr rptr
;
2648 union xfs_btree_ptr lptr
;
2650 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
2651 XFS_BTREE_STATS_INC(cur
, newroot
);
2653 /* initialise our start point from the cursor */
2654 cur
->bc_ops
->init_ptr_from_cur(cur
, &rptr
);
2656 /* Allocate the new block. If we can't do it, we're toast. Give up. */
2657 error
= cur
->bc_ops
->alloc_block(cur
, &rptr
, &lptr
, 1, stat
);
2662 XFS_BTREE_STATS_INC(cur
, alloc
);
2664 /* Set up the new block. */
2665 error
= xfs_btree_get_buf_block(cur
, &lptr
, 0, &new, &nbp
);
2669 /* Set the root in the holding structure increasing the level by 1. */
2670 cur
->bc_ops
->set_root(cur
, &lptr
, 1);
2673 * At the previous root level there are now two blocks: the old root,
2674 * and the new block generated when it was split. We don't know which
2675 * one the cursor is pointing at, so we set up variables "left" and
2676 * "right" for each case.
2678 block
= xfs_btree_get_block(cur
, cur
->bc_nlevels
- 1, &bp
);
2681 error
= xfs_btree_check_block(cur
, block
, cur
->bc_nlevels
- 1, bp
);
2686 xfs_btree_get_sibling(cur
, block
, &rptr
, XFS_BB_RIGHTSIB
);
2687 if (!xfs_btree_ptr_is_null(cur
, &rptr
)) {
2688 /* Our block is left, pick up the right block. */
2690 xfs_btree_buf_to_ptr(cur
, lbp
, &lptr
);
2692 error
= xfs_btree_read_buf_block(cur
, &rptr
,
2693 cur
->bc_nlevels
- 1, 0, &right
, &rbp
);
2699 /* Our block is right, pick up the left block. */
2701 xfs_btree_buf_to_ptr(cur
, rbp
, &rptr
);
2703 xfs_btree_get_sibling(cur
, right
, &lptr
, XFS_BB_LEFTSIB
);
2704 error
= xfs_btree_read_buf_block(cur
, &lptr
,
2705 cur
->bc_nlevels
- 1, 0, &left
, &lbp
);
2711 /* Fill in the new block's btree header and log it. */
2712 xfs_btree_init_block_cur(cur
, nbp
, cur
->bc_nlevels
, 2);
2713 xfs_btree_log_block(cur
, nbp
, XFS_BB_ALL_BITS
);
2714 ASSERT(!xfs_btree_ptr_is_null(cur
, &lptr
) &&
2715 !xfs_btree_ptr_is_null(cur
, &rptr
));
2717 /* Fill in the key data in the new root. */
2718 if (xfs_btree_get_level(left
) > 0) {
2719 xfs_btree_copy_keys(cur
,
2720 xfs_btree_key_addr(cur
, 1, new),
2721 xfs_btree_key_addr(cur
, 1, left
), 1);
2722 xfs_btree_copy_keys(cur
,
2723 xfs_btree_key_addr(cur
, 2, new),
2724 xfs_btree_key_addr(cur
, 1, right
), 1);
2726 cur
->bc_ops
->init_key_from_rec(
2727 xfs_btree_key_addr(cur
, 1, new),
2728 xfs_btree_rec_addr(cur
, 1, left
));
2729 cur
->bc_ops
->init_key_from_rec(
2730 xfs_btree_key_addr(cur
, 2, new),
2731 xfs_btree_rec_addr(cur
, 1, right
));
2733 xfs_btree_log_keys(cur
, nbp
, 1, 2);
2735 /* Fill in the pointer data in the new root. */
2736 xfs_btree_copy_ptrs(cur
,
2737 xfs_btree_ptr_addr(cur
, 1, new), &lptr
, 1);
2738 xfs_btree_copy_ptrs(cur
,
2739 xfs_btree_ptr_addr(cur
, 2, new), &rptr
, 1);
2740 xfs_btree_log_ptrs(cur
, nbp
, 1, 2);
2742 /* Fix up the cursor. */
2743 xfs_btree_setbuf(cur
, cur
->bc_nlevels
, nbp
);
2744 cur
->bc_ptrs
[cur
->bc_nlevels
] = nptr
;
2746 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
2750 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
2753 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
2759 xfs_btree_make_block_unfull(
2760 struct xfs_btree_cur
*cur
, /* btree cursor */
2761 int level
, /* btree level */
2762 int numrecs
,/* # of recs in block */
2763 int *oindex
,/* old tree index */
2764 int *index
, /* new tree index */
2765 union xfs_btree_ptr
*nptr
, /* new btree ptr */
2766 struct xfs_btree_cur
**ncur
, /* new btree cursor */
2767 union xfs_btree_rec
*nrec
, /* new record */
2770 union xfs_btree_key key
; /* new btree key value */
2773 if ((cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
) &&
2774 level
== cur
->bc_nlevels
- 1) {
2775 struct xfs_inode
*ip
= cur
->bc_private
.b
.ip
;
2777 if (numrecs
< cur
->bc_ops
->get_dmaxrecs(cur
, level
)) {
2778 /* A root block that can be made bigger. */
2779 xfs_iroot_realloc(ip
, 1, cur
->bc_private
.b
.whichfork
);
2781 /* A root block that needs replacing */
2784 error
= xfs_btree_new_iroot(cur
, &logflags
, stat
);
2785 if (error
|| *stat
== 0)
2788 xfs_trans_log_inode(cur
->bc_tp
, ip
, logflags
);
2794 /* First, try shifting an entry to the right neighbor. */
2795 error
= xfs_btree_rshift(cur
, level
, stat
);
2799 /* Next, try shifting an entry to the left neighbor. */
2800 error
= xfs_btree_lshift(cur
, level
, stat
);
2805 *oindex
= *index
= cur
->bc_ptrs
[level
];
2810 * Next, try splitting the current block in half.
2812 * If this works we have to re-set our variables because we
2813 * could be in a different block now.
2815 error
= xfs_btree_split(cur
, level
, nptr
, &key
, ncur
, stat
);
2816 if (error
|| *stat
== 0)
2820 *index
= cur
->bc_ptrs
[level
];
2821 cur
->bc_ops
->init_rec_from_key(&key
, nrec
);
2826 * Insert one record/level. Return information to the caller
2827 * allowing the next level up to proceed if necessary.
2831 struct xfs_btree_cur
*cur
, /* btree cursor */
2832 int level
, /* level to insert record at */
2833 union xfs_btree_ptr
*ptrp
, /* i/o: block number inserted */
2834 union xfs_btree_rec
*recp
, /* i/o: record data inserted */
2835 struct xfs_btree_cur
**curp
, /* output: new cursor replacing cur */
2836 int *stat
) /* success/failure */
2838 struct xfs_btree_block
*block
; /* btree block */
2839 struct xfs_buf
*bp
; /* buffer for block */
2840 union xfs_btree_key key
; /* btree key */
2841 union xfs_btree_ptr nptr
; /* new block ptr */
2842 struct xfs_btree_cur
*ncur
; /* new btree cursor */
2843 union xfs_btree_rec nrec
; /* new record count */
2844 int optr
; /* old key/record index */
2845 int ptr
; /* key/record index */
2846 int numrecs
;/* number of records */
2847 int error
; /* error return value */
2852 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
2853 XFS_BTREE_TRACE_ARGIPR(cur
, level
, *ptrp
, recp
);
2858 * If we have an external root pointer, and we've made it to the
2859 * root level, allocate a new root block and we're done.
2861 if (!(cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
) &&
2862 (level
>= cur
->bc_nlevels
)) {
2863 error
= xfs_btree_new_root(cur
, stat
);
2864 xfs_btree_set_ptr_null(cur
, ptrp
);
2866 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
2870 /* If we're off the left edge, return failure. */
2871 ptr
= cur
->bc_ptrs
[level
];
2873 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
2878 /* Make a key out of the record data to be inserted, and save it. */
2879 cur
->bc_ops
->init_key_from_rec(&key
, recp
);
2883 XFS_BTREE_STATS_INC(cur
, insrec
);
2885 /* Get pointers to the btree buffer and block. */
2886 block
= xfs_btree_get_block(cur
, level
, &bp
);
2887 numrecs
= xfs_btree_get_numrecs(block
);
2890 error
= xfs_btree_check_block(cur
, block
, level
, bp
);
2894 /* Check that the new entry is being inserted in the right place. */
2895 if (ptr
<= numrecs
) {
2897 ASSERT(cur
->bc_ops
->recs_inorder(cur
, recp
,
2898 xfs_btree_rec_addr(cur
, ptr
, block
)));
2900 ASSERT(cur
->bc_ops
->keys_inorder(cur
, &key
,
2901 xfs_btree_key_addr(cur
, ptr
, block
)));
2907 * If the block is full, we can't insert the new entry until we
2908 * make the block un-full.
2910 xfs_btree_set_ptr_null(cur
, &nptr
);
2911 if (numrecs
== cur
->bc_ops
->get_maxrecs(cur
, level
)) {
2912 error
= xfs_btree_make_block_unfull(cur
, level
, numrecs
,
2913 &optr
, &ptr
, &nptr
, &ncur
, &nrec
, stat
);
2914 if (error
|| *stat
== 0)
2919 * The current block may have changed if the block was
2920 * previously full and we have just made space in it.
2922 block
= xfs_btree_get_block(cur
, level
, &bp
);
2923 numrecs
= xfs_btree_get_numrecs(block
);
2926 error
= xfs_btree_check_block(cur
, block
, level
, bp
);
2932 * At this point we know there's room for our new entry in the block
2933 * we're pointing at.
2935 XFS_BTREE_STATS_ADD(cur
, moves
, numrecs
- ptr
+ 1);
2938 /* It's a nonleaf. make a hole in the keys and ptrs */
2939 union xfs_btree_key
*kp
;
2940 union xfs_btree_ptr
*pp
;
2942 kp
= xfs_btree_key_addr(cur
, ptr
, block
);
2943 pp
= xfs_btree_ptr_addr(cur
, ptr
, block
);
2946 for (i
= numrecs
- ptr
; i
>= 0; i
--) {
2947 error
= xfs_btree_check_ptr(cur
, pp
, i
, level
);
2953 xfs_btree_shift_keys(cur
, kp
, 1, numrecs
- ptr
+ 1);
2954 xfs_btree_shift_ptrs(cur
, pp
, 1, numrecs
- ptr
+ 1);
2957 error
= xfs_btree_check_ptr(cur
, ptrp
, 0, level
);
2962 /* Now put the new data in, bump numrecs and log it. */
2963 xfs_btree_copy_keys(cur
, kp
, &key
, 1);
2964 xfs_btree_copy_ptrs(cur
, pp
, ptrp
, 1);
2966 xfs_btree_set_numrecs(block
, numrecs
);
2967 xfs_btree_log_ptrs(cur
, bp
, ptr
, numrecs
);
2968 xfs_btree_log_keys(cur
, bp
, ptr
, numrecs
);
2970 if (ptr
< numrecs
) {
2971 ASSERT(cur
->bc_ops
->keys_inorder(cur
, kp
,
2972 xfs_btree_key_addr(cur
, ptr
+ 1, block
)));
2976 /* It's a leaf. make a hole in the records */
2977 union xfs_btree_rec
*rp
;
2979 rp
= xfs_btree_rec_addr(cur
, ptr
, block
);
2981 xfs_btree_shift_recs(cur
, rp
, 1, numrecs
- ptr
+ 1);
2983 /* Now put the new data in, bump numrecs and log it. */
2984 xfs_btree_copy_recs(cur
, rp
, recp
, 1);
2985 xfs_btree_set_numrecs(block
, ++numrecs
);
2986 xfs_btree_log_recs(cur
, bp
, ptr
, numrecs
);
2988 if (ptr
< numrecs
) {
2989 ASSERT(cur
->bc_ops
->recs_inorder(cur
, rp
,
2990 xfs_btree_rec_addr(cur
, ptr
+ 1, block
)));
2995 /* Log the new number of records in the btree header. */
2996 xfs_btree_log_block(cur
, bp
, XFS_BB_NUMRECS
);
2998 /* If we inserted at the start of a block, update the parents' keys. */
3000 error
= xfs_btree_updkey(cur
, &key
, level
+ 1);
3006 * If we are tracking the last record in the tree and
3007 * we are at the far right edge of the tree, update it.
3009 if (xfs_btree_is_lastrec(cur
, block
, level
)) {
3010 cur
->bc_ops
->update_lastrec(cur
, block
, recp
,
3011 ptr
, LASTREC_INSREC
);
3015 * Return the new block number, if any.
3016 * If there is one, give back a record value and a cursor too.
3019 if (!xfs_btree_ptr_is_null(cur
, &nptr
)) {
3024 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
3029 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
3034 * Insert the record at the point referenced by cur.
3036 * A multi-level split of the tree on insert will invalidate the original
3037 * cursor. All callers of this function should assume that the cursor is
3038 * no longer valid and revalidate it.
3042 struct xfs_btree_cur
*cur
,
3045 int error
; /* error return value */
3046 int i
; /* result value, 0 for failure */
3047 int level
; /* current level number in btree */
3048 union xfs_btree_ptr nptr
; /* new block number (split result) */
3049 struct xfs_btree_cur
*ncur
; /* new cursor (split result) */
3050 struct xfs_btree_cur
*pcur
; /* previous level's cursor */
3051 union xfs_btree_rec rec
; /* record to insert */
3057 xfs_btree_set_ptr_null(cur
, &nptr
);
3058 cur
->bc_ops
->init_rec_from_cur(cur
, &rec
);
3061 * Loop going up the tree, starting at the leaf level.
3062 * Stop when we don't get a split block, that must mean that
3063 * the insert is finished with this level.
3067 * Insert nrec/nptr into this level of the tree.
3068 * Note if we fail, nptr will be null.
3070 error
= xfs_btree_insrec(pcur
, level
, &nptr
, &rec
, &ncur
, &i
);
3073 xfs_btree_del_cursor(pcur
, XFS_BTREE_ERROR
);
3077 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
3081 * See if the cursor we just used is trash.
3082 * Can't trash the caller's cursor, but otherwise we should
3083 * if ncur is a new cursor or we're about to be done.
3086 (ncur
|| xfs_btree_ptr_is_null(cur
, &nptr
))) {
3087 /* Save the state from the cursor before we trash it */
3088 if (cur
->bc_ops
->update_cursor
)
3089 cur
->bc_ops
->update_cursor(pcur
, cur
);
3090 cur
->bc_nlevels
= pcur
->bc_nlevels
;
3091 xfs_btree_del_cursor(pcur
, XFS_BTREE_NOERROR
);
3093 /* If we got a new cursor, switch to it. */
3098 } while (!xfs_btree_ptr_is_null(cur
, &nptr
));
3100 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
3104 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
3109 * Try to merge a non-leaf block back into the inode root.
3111 * Note: the killroot names comes from the fact that we're effectively
3112 * killing the old root block. But because we can't just delete the
3113 * inode we have to copy the single block it was pointing to into the
3117 xfs_btree_kill_iroot(
3118 struct xfs_btree_cur
*cur
)
3120 int whichfork
= cur
->bc_private
.b
.whichfork
;
3121 struct xfs_inode
*ip
= cur
->bc_private
.b
.ip
;
3122 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
3123 struct xfs_btree_block
*block
;
3124 struct xfs_btree_block
*cblock
;
3125 union xfs_btree_key
*kp
;
3126 union xfs_btree_key
*ckp
;
3127 union xfs_btree_ptr
*pp
;
3128 union xfs_btree_ptr
*cpp
;
3129 struct xfs_buf
*cbp
;
3134 union xfs_btree_ptr ptr
;
3138 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
3140 ASSERT(cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
);
3141 ASSERT(cur
->bc_nlevels
> 1);
3144 * Don't deal with the root block needs to be a leaf case.
3145 * We're just going to turn the thing back into extents anyway.
3147 level
= cur
->bc_nlevels
- 1;
3152 * Give up if the root has multiple children.
3154 block
= xfs_btree_get_iroot(cur
);
3155 if (xfs_btree_get_numrecs(block
) != 1)
3158 cblock
= xfs_btree_get_block(cur
, level
- 1, &cbp
);
3159 numrecs
= xfs_btree_get_numrecs(cblock
);
3162 * Only do this if the next level will fit.
3163 * Then the data must be copied up to the inode,
3164 * instead of freeing the root you free the next level.
3166 if (numrecs
> cur
->bc_ops
->get_dmaxrecs(cur
, level
))
3169 XFS_BTREE_STATS_INC(cur
, killroot
);
3172 xfs_btree_get_sibling(cur
, block
, &ptr
, XFS_BB_LEFTSIB
);
3173 ASSERT(xfs_btree_ptr_is_null(cur
, &ptr
));
3174 xfs_btree_get_sibling(cur
, block
, &ptr
, XFS_BB_RIGHTSIB
);
3175 ASSERT(xfs_btree_ptr_is_null(cur
, &ptr
));
3178 index
= numrecs
- cur
->bc_ops
->get_maxrecs(cur
, level
);
3180 xfs_iroot_realloc(cur
->bc_private
.b
.ip
, index
,
3181 cur
->bc_private
.b
.whichfork
);
3182 block
= ifp
->if_broot
;
3185 be16_add_cpu(&block
->bb_numrecs
, index
);
3186 ASSERT(block
->bb_numrecs
== cblock
->bb_numrecs
);
3188 kp
= xfs_btree_key_addr(cur
, 1, block
);
3189 ckp
= xfs_btree_key_addr(cur
, 1, cblock
);
3190 xfs_btree_copy_keys(cur
, kp
, ckp
, numrecs
);
3192 pp
= xfs_btree_ptr_addr(cur
, 1, block
);
3193 cpp
= xfs_btree_ptr_addr(cur
, 1, cblock
);
3195 for (i
= 0; i
< numrecs
; i
++) {
3198 error
= xfs_btree_check_ptr(cur
, cpp
, i
, level
- 1);
3200 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
3205 xfs_btree_copy_ptrs(cur
, pp
, cpp
, numrecs
);
3207 cur
->bc_ops
->free_block(cur
, cbp
);
3208 XFS_BTREE_STATS_INC(cur
, free
);
3210 cur
->bc_bufs
[level
- 1] = NULL
;
3211 be16_add_cpu(&block
->bb_level
, -1);
3212 xfs_trans_log_inode(cur
->bc_tp
, ip
,
3213 XFS_ILOG_CORE
| xfs_ilog_fbroot(cur
->bc_private
.b
.whichfork
));
3216 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
3221 * Kill the current root node, and replace it with it's only child node.
3224 xfs_btree_kill_root(
3225 struct xfs_btree_cur
*cur
,
3228 union xfs_btree_ptr
*newroot
)
3232 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
3233 XFS_BTREE_STATS_INC(cur
, killroot
);
3236 * Update the root pointer, decreasing the level by 1 and then
3237 * free the old root.
3239 cur
->bc_ops
->set_root(cur
, newroot
, -1);
3241 error
= cur
->bc_ops
->free_block(cur
, bp
);
3243 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
3247 XFS_BTREE_STATS_INC(cur
, free
);
3249 cur
->bc_bufs
[level
] = NULL
;
3250 cur
->bc_ra
[level
] = 0;
3253 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
3258 xfs_btree_dec_cursor(
3259 struct xfs_btree_cur
*cur
,
3267 error
= xfs_btree_decrement(cur
, level
, &i
);
3272 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
3278 * Single level of the btree record deletion routine.
3279 * Delete record pointed to by cur/level.
3280 * Remove the record from its block then rebalance the tree.
3281 * Return 0 for error, 1 for done, 2 to go on to the next level.
3283 STATIC
int /* error */
3285 struct xfs_btree_cur
*cur
, /* btree cursor */
3286 int level
, /* level removing record from */
3287 int *stat
) /* fail/done/go-on */
3289 struct xfs_btree_block
*block
; /* btree block */
3290 union xfs_btree_ptr cptr
; /* current block ptr */
3291 struct xfs_buf
*bp
; /* buffer for block */
3292 int error
; /* error return value */
3293 int i
; /* loop counter */
3294 union xfs_btree_key key
; /* storage for keyp */
3295 union xfs_btree_key
*keyp
= &key
; /* passed to the next level */
3296 union xfs_btree_ptr lptr
; /* left sibling block ptr */
3297 struct xfs_buf
*lbp
; /* left buffer pointer */
3298 struct xfs_btree_block
*left
; /* left btree block */
3299 int lrecs
= 0; /* left record count */
3300 int ptr
; /* key/record index */
3301 union xfs_btree_ptr rptr
; /* right sibling block ptr */
3302 struct xfs_buf
*rbp
; /* right buffer pointer */
3303 struct xfs_btree_block
*right
; /* right btree block */
3304 struct xfs_btree_block
*rrblock
; /* right-right btree block */
3305 struct xfs_buf
*rrbp
; /* right-right buffer pointer */
3306 int rrecs
= 0; /* right record count */
3307 struct xfs_btree_cur
*tcur
; /* temporary btree cursor */
3308 int numrecs
; /* temporary numrec count */
3310 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
3311 XFS_BTREE_TRACE_ARGI(cur
, level
);
3315 /* Get the index of the entry being deleted, check for nothing there. */
3316 ptr
= cur
->bc_ptrs
[level
];
3318 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
3323 /* Get the buffer & block containing the record or key/ptr. */
3324 block
= xfs_btree_get_block(cur
, level
, &bp
);
3325 numrecs
= xfs_btree_get_numrecs(block
);
3328 error
= xfs_btree_check_block(cur
, block
, level
, bp
);
3333 /* Fail if we're off the end of the block. */
3334 if (ptr
> numrecs
) {
3335 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
3340 XFS_BTREE_STATS_INC(cur
, delrec
);
3341 XFS_BTREE_STATS_ADD(cur
, moves
, numrecs
- ptr
);
3343 /* Excise the entries being deleted. */
3345 /* It's a nonleaf. operate on keys and ptrs */
3346 union xfs_btree_key
*lkp
;
3347 union xfs_btree_ptr
*lpp
;
3349 lkp
= xfs_btree_key_addr(cur
, ptr
+ 1, block
);
3350 lpp
= xfs_btree_ptr_addr(cur
, ptr
+ 1, block
);
3353 for (i
= 0; i
< numrecs
- ptr
; i
++) {
3354 error
= xfs_btree_check_ptr(cur
, lpp
, i
, level
);
3360 if (ptr
< numrecs
) {
3361 xfs_btree_shift_keys(cur
, lkp
, -1, numrecs
- ptr
);
3362 xfs_btree_shift_ptrs(cur
, lpp
, -1, numrecs
- ptr
);
3363 xfs_btree_log_keys(cur
, bp
, ptr
, numrecs
- 1);
3364 xfs_btree_log_ptrs(cur
, bp
, ptr
, numrecs
- 1);
3368 * If it's the first record in the block, we'll need to pass a
3369 * key up to the next level (updkey).
3372 keyp
= xfs_btree_key_addr(cur
, 1, block
);
3374 /* It's a leaf. operate on records */
3375 if (ptr
< numrecs
) {
3376 xfs_btree_shift_recs(cur
,
3377 xfs_btree_rec_addr(cur
, ptr
+ 1, block
),
3379 xfs_btree_log_recs(cur
, bp
, ptr
, numrecs
- 1);
3383 * If it's the first record in the block, we'll need a key
3384 * structure to pass up to the next level (updkey).
3387 cur
->bc_ops
->init_key_from_rec(&key
,
3388 xfs_btree_rec_addr(cur
, 1, block
));
3394 * Decrement and log the number of entries in the block.
3396 xfs_btree_set_numrecs(block
, --numrecs
);
3397 xfs_btree_log_block(cur
, bp
, XFS_BB_NUMRECS
);
3400 * If we are tracking the last record in the tree and
3401 * we are at the far right edge of the tree, update it.
3403 if (xfs_btree_is_lastrec(cur
, block
, level
)) {
3404 cur
->bc_ops
->update_lastrec(cur
, block
, NULL
,
3405 ptr
, LASTREC_DELREC
);
3409 * We're at the root level. First, shrink the root block in-memory.
3410 * Try to get rid of the next level down. If we can't then there's
3411 * nothing left to do.
3413 if (level
== cur
->bc_nlevels
- 1) {
3414 if (cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
) {
3415 xfs_iroot_realloc(cur
->bc_private
.b
.ip
, -1,
3416 cur
->bc_private
.b
.whichfork
);
3418 error
= xfs_btree_kill_iroot(cur
);
3422 error
= xfs_btree_dec_cursor(cur
, level
, stat
);
3430 * If this is the root level, and there's only one entry left,
3431 * and it's NOT the leaf level, then we can get rid of this
3434 if (numrecs
== 1 && level
> 0) {
3435 union xfs_btree_ptr
*pp
;
3437 * pp is still set to the first pointer in the block.
3438 * Make it the new root of the btree.
3440 pp
= xfs_btree_ptr_addr(cur
, 1, block
);
3441 error
= xfs_btree_kill_root(cur
, bp
, level
, pp
);
3444 } else if (level
> 0) {
3445 error
= xfs_btree_dec_cursor(cur
, level
, stat
);
3454 * If we deleted the leftmost entry in the block, update the
3455 * key values above us in the tree.
3458 error
= xfs_btree_updkey(cur
, keyp
, level
+ 1);
3464 * If the number of records remaining in the block is at least
3465 * the minimum, we're done.
3467 if (numrecs
>= cur
->bc_ops
->get_minrecs(cur
, level
)) {
3468 error
= xfs_btree_dec_cursor(cur
, level
, stat
);
3475 * Otherwise, we have to move some records around to keep the
3476 * tree balanced. Look at the left and right sibling blocks to
3477 * see if we can re-balance by moving only one record.
3479 xfs_btree_get_sibling(cur
, block
, &rptr
, XFS_BB_RIGHTSIB
);
3480 xfs_btree_get_sibling(cur
, block
, &lptr
, XFS_BB_LEFTSIB
);
3482 if (cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
) {
3484 * One child of root, need to get a chance to copy its contents
3485 * into the root and delete it. Can't go up to next level,
3486 * there's nothing to delete there.
3488 if (xfs_btree_ptr_is_null(cur
, &rptr
) &&
3489 xfs_btree_ptr_is_null(cur
, &lptr
) &&
3490 level
== cur
->bc_nlevels
- 2) {
3491 error
= xfs_btree_kill_iroot(cur
);
3493 error
= xfs_btree_dec_cursor(cur
, level
, stat
);
3500 ASSERT(!xfs_btree_ptr_is_null(cur
, &rptr
) ||
3501 !xfs_btree_ptr_is_null(cur
, &lptr
));
3504 * Duplicate the cursor so our btree manipulations here won't
3505 * disrupt the next level up.
3507 error
= xfs_btree_dup_cursor(cur
, &tcur
);
3512 * If there's a right sibling, see if it's ok to shift an entry
3515 if (!xfs_btree_ptr_is_null(cur
, &rptr
)) {
3517 * Move the temp cursor to the last entry in the next block.
3518 * Actually any entry but the first would suffice.
3520 i
= xfs_btree_lastrec(tcur
, level
);
3521 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
3523 error
= xfs_btree_increment(tcur
, level
, &i
);
3526 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
3528 i
= xfs_btree_lastrec(tcur
, level
);
3529 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
3531 /* Grab a pointer to the block. */
3532 right
= xfs_btree_get_block(tcur
, level
, &rbp
);
3534 error
= xfs_btree_check_block(tcur
, right
, level
, rbp
);
3538 /* Grab the current block number, for future use. */
3539 xfs_btree_get_sibling(tcur
, right
, &cptr
, XFS_BB_LEFTSIB
);
3542 * If right block is full enough so that removing one entry
3543 * won't make it too empty, and left-shifting an entry out
3544 * of right to us works, we're done.
3546 if (xfs_btree_get_numrecs(right
) - 1 >=
3547 cur
->bc_ops
->get_minrecs(tcur
, level
)) {
3548 error
= xfs_btree_lshift(tcur
, level
, &i
);
3552 ASSERT(xfs_btree_get_numrecs(block
) >=
3553 cur
->bc_ops
->get_minrecs(tcur
, level
));
3555 xfs_btree_del_cursor(tcur
, XFS_BTREE_NOERROR
);
3558 error
= xfs_btree_dec_cursor(cur
, level
, stat
);
3566 * Otherwise, grab the number of records in right for
3567 * future reference, and fix up the temp cursor to point
3568 * to our block again (last record).
3570 rrecs
= xfs_btree_get_numrecs(right
);
3571 if (!xfs_btree_ptr_is_null(cur
, &lptr
)) {
3572 i
= xfs_btree_firstrec(tcur
, level
);
3573 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
3575 error
= xfs_btree_decrement(tcur
, level
, &i
);
3578 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
3583 * If there's a left sibling, see if it's ok to shift an entry
3586 if (!xfs_btree_ptr_is_null(cur
, &lptr
)) {
3588 * Move the temp cursor to the first entry in the
3591 i
= xfs_btree_firstrec(tcur
, level
);
3592 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
3594 error
= xfs_btree_decrement(tcur
, level
, &i
);
3597 i
= xfs_btree_firstrec(tcur
, level
);
3598 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
3600 /* Grab a pointer to the block. */
3601 left
= xfs_btree_get_block(tcur
, level
, &lbp
);
3603 error
= xfs_btree_check_block(cur
, left
, level
, lbp
);
3607 /* Grab the current block number, for future use. */
3608 xfs_btree_get_sibling(tcur
, left
, &cptr
, XFS_BB_RIGHTSIB
);
3611 * If left block is full enough so that removing one entry
3612 * won't make it too empty, and right-shifting an entry out
3613 * of left to us works, we're done.
3615 if (xfs_btree_get_numrecs(left
) - 1 >=
3616 cur
->bc_ops
->get_minrecs(tcur
, level
)) {
3617 error
= xfs_btree_rshift(tcur
, level
, &i
);
3621 ASSERT(xfs_btree_get_numrecs(block
) >=
3622 cur
->bc_ops
->get_minrecs(tcur
, level
));
3623 xfs_btree_del_cursor(tcur
, XFS_BTREE_NOERROR
);
3627 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
3634 * Otherwise, grab the number of records in right for
3637 lrecs
= xfs_btree_get_numrecs(left
);
3640 /* Delete the temp cursor, we're done with it. */
3641 xfs_btree_del_cursor(tcur
, XFS_BTREE_NOERROR
);
3644 /* If here, we need to do a join to keep the tree balanced. */
3645 ASSERT(!xfs_btree_ptr_is_null(cur
, &cptr
));
3647 if (!xfs_btree_ptr_is_null(cur
, &lptr
) &&
3648 lrecs
+ xfs_btree_get_numrecs(block
) <=
3649 cur
->bc_ops
->get_maxrecs(cur
, level
)) {
3651 * Set "right" to be the starting block,
3652 * "left" to be the left neighbor.
3657 error
= xfs_btree_read_buf_block(cur
, &lptr
, level
,
3663 * If that won't work, see if we can join with the right neighbor block.
3665 } else if (!xfs_btree_ptr_is_null(cur
, &rptr
) &&
3666 rrecs
+ xfs_btree_get_numrecs(block
) <=
3667 cur
->bc_ops
->get_maxrecs(cur
, level
)) {
3669 * Set "left" to be the starting block,
3670 * "right" to be the right neighbor.
3675 error
= xfs_btree_read_buf_block(cur
, &rptr
, level
,
3681 * Otherwise, we can't fix the imbalance.
3682 * Just return. This is probably a logic error, but it's not fatal.
3685 error
= xfs_btree_dec_cursor(cur
, level
, stat
);
3691 rrecs
= xfs_btree_get_numrecs(right
);
3692 lrecs
= xfs_btree_get_numrecs(left
);
3695 * We're now going to join "left" and "right" by moving all the stuff
3696 * in "right" to "left" and deleting "right".
3698 XFS_BTREE_STATS_ADD(cur
, moves
, rrecs
);
3700 /* It's a non-leaf. Move keys and pointers. */
3701 union xfs_btree_key
*lkp
; /* left btree key */
3702 union xfs_btree_ptr
*lpp
; /* left address pointer */
3703 union xfs_btree_key
*rkp
; /* right btree key */
3704 union xfs_btree_ptr
*rpp
; /* right address pointer */
3706 lkp
= xfs_btree_key_addr(cur
, lrecs
+ 1, left
);
3707 lpp
= xfs_btree_ptr_addr(cur
, lrecs
+ 1, left
);
3708 rkp
= xfs_btree_key_addr(cur
, 1, right
);
3709 rpp
= xfs_btree_ptr_addr(cur
, 1, right
);
3711 for (i
= 1; i
< rrecs
; i
++) {
3712 error
= xfs_btree_check_ptr(cur
, rpp
, i
, level
);
3717 xfs_btree_copy_keys(cur
, lkp
, rkp
, rrecs
);
3718 xfs_btree_copy_ptrs(cur
, lpp
, rpp
, rrecs
);
3720 xfs_btree_log_keys(cur
, lbp
, lrecs
+ 1, lrecs
+ rrecs
);
3721 xfs_btree_log_ptrs(cur
, lbp
, lrecs
+ 1, lrecs
+ rrecs
);
3723 /* It's a leaf. Move records. */
3724 union xfs_btree_rec
*lrp
; /* left record pointer */
3725 union xfs_btree_rec
*rrp
; /* right record pointer */
3727 lrp
= xfs_btree_rec_addr(cur
, lrecs
+ 1, left
);
3728 rrp
= xfs_btree_rec_addr(cur
, 1, right
);
3730 xfs_btree_copy_recs(cur
, lrp
, rrp
, rrecs
);
3731 xfs_btree_log_recs(cur
, lbp
, lrecs
+ 1, lrecs
+ rrecs
);
3734 XFS_BTREE_STATS_INC(cur
, join
);
3737 * Fix up the number of records and right block pointer in the
3738 * surviving block, and log it.
3740 xfs_btree_set_numrecs(left
, lrecs
+ rrecs
);
3741 xfs_btree_get_sibling(cur
, right
, &cptr
, XFS_BB_RIGHTSIB
),
3742 xfs_btree_set_sibling(cur
, left
, &cptr
, XFS_BB_RIGHTSIB
);
3743 xfs_btree_log_block(cur
, lbp
, XFS_BB_NUMRECS
| XFS_BB_RIGHTSIB
);
3745 /* If there is a right sibling, point it to the remaining block. */
3746 xfs_btree_get_sibling(cur
, left
, &cptr
, XFS_BB_RIGHTSIB
);
3747 if (!xfs_btree_ptr_is_null(cur
, &cptr
)) {
3748 error
= xfs_btree_read_buf_block(cur
, &cptr
, level
,
3749 0, &rrblock
, &rrbp
);
3752 xfs_btree_set_sibling(cur
, rrblock
, &lptr
, XFS_BB_LEFTSIB
);
3753 xfs_btree_log_block(cur
, rrbp
, XFS_BB_LEFTSIB
);
3756 /* Free the deleted block. */
3757 error
= cur
->bc_ops
->free_block(cur
, rbp
);
3760 XFS_BTREE_STATS_INC(cur
, free
);
3763 * If we joined with the left neighbor, set the buffer in the
3764 * cursor to the left block, and fix up the index.
3767 cur
->bc_bufs
[level
] = lbp
;
3768 cur
->bc_ptrs
[level
] += lrecs
;
3769 cur
->bc_ra
[level
] = 0;
3772 * If we joined with the right neighbor and there's a level above
3773 * us, increment the cursor at that level.
3775 else if ((cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
) ||
3776 (level
+ 1 < cur
->bc_nlevels
)) {
3777 error
= xfs_btree_increment(cur
, level
+ 1, &i
);
3783 * Readjust the ptr at this level if it's not a leaf, since it's
3784 * still pointing at the deletion point, which makes the cursor
3785 * inconsistent. If this makes the ptr 0, the caller fixes it up.
3786 * We can't use decrement because it would change the next level up.
3789 cur
->bc_ptrs
[level
]--;
3791 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
3792 /* Return value means the next level up has something to do. */
3797 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
3799 xfs_btree_del_cursor(tcur
, XFS_BTREE_ERROR
);
3804 * Delete the record pointed to by cur.
3805 * The cursor refers to the place where the record was (could be inserted)
3806 * when the operation returns.
3810 struct xfs_btree_cur
*cur
,
3811 int *stat
) /* success/failure */
3813 int error
; /* error return value */
3817 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
3820 * Go up the tree, starting at leaf level.
3822 * If 2 is returned then a join was done; go to the next level.
3823 * Otherwise we are done.
3825 for (level
= 0, i
= 2; i
== 2; level
++) {
3826 error
= xfs_btree_delrec(cur
, level
, &i
);
3832 for (level
= 1; level
< cur
->bc_nlevels
; level
++) {
3833 if (cur
->bc_ptrs
[level
] == 0) {
3834 error
= xfs_btree_decrement(cur
, level
, &i
);
3842 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
3846 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
3851 * Get the data from the pointed-to record.
3855 struct xfs_btree_cur
*cur
, /* btree cursor */
3856 union xfs_btree_rec
**recp
, /* output: btree record */
3857 int *stat
) /* output: success/failure */
3859 struct xfs_btree_block
*block
; /* btree block */
3860 struct xfs_buf
*bp
; /* buffer pointer */
3861 int ptr
; /* record number */
3863 int error
; /* error return value */
3866 ptr
= cur
->bc_ptrs
[0];
3867 block
= xfs_btree_get_block(cur
, 0, &bp
);
3870 error
= xfs_btree_check_block(cur
, block
, 0, bp
);
3876 * Off the right end or left end, return failure.
3878 if (ptr
> xfs_btree_get_numrecs(block
) || ptr
<= 0) {
3884 * Point to the record and extract its data.
3886 *recp
= xfs_btree_rec_addr(cur
, ptr
, block
);
3892 * Change the owner of a btree.
3894 * The mechanism we use here is ordered buffer logging. Because we don't know
3895 * how many buffers were are going to need to modify, we don't really want to
3896 * have to make transaction reservations for the worst case of every buffer in a
3897 * full size btree as that may be more space that we can fit in the log....
3899 * We do the btree walk in the most optimal manner possible - we have sibling
3900 * pointers so we can just walk all the blocks on each level from left to right
3901 * in a single pass, and then move to the next level and do the same. We can
3902 * also do readahead on the sibling pointers to get IO moving more quickly,
3903 * though for slow disks this is unlikely to make much difference to performance
3904 * as the amount of CPU work we have to do before moving to the next block is
3907 * For each btree block that we load, modify the owner appropriately, set the
3908 * buffer as an ordered buffer and log it appropriately. We need to ensure that
3909 * we mark the region we change dirty so that if the buffer is relogged in
3910 * a subsequent transaction the changes we make here as an ordered buffer are
3911 * correctly relogged in that transaction. If we are in recovery context, then
3912 * just queue the modified buffer as delayed write buffer so the transaction
3913 * recovery completion writes the changes to disk.
3916 xfs_btree_block_change_owner(
3917 struct xfs_btree_cur
*cur
,
3919 __uint64_t new_owner
,
3920 struct list_head
*buffer_list
)
3922 struct xfs_btree_block
*block
;
3924 union xfs_btree_ptr rptr
;
3926 /* do right sibling readahead */
3927 xfs_btree_readahead(cur
, level
, XFS_BTCUR_RIGHTRA
);
3929 /* modify the owner */
3930 block
= xfs_btree_get_block(cur
, level
, &bp
);
3931 if (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
)
3932 block
->bb_u
.l
.bb_owner
= cpu_to_be64(new_owner
);
3934 block
->bb_u
.s
.bb_owner
= cpu_to_be32(new_owner
);
3937 * If the block is a root block hosted in an inode, we might not have a
3938 * buffer pointer here and we shouldn't attempt to log the change as the
3939 * information is already held in the inode and discarded when the root
3940 * block is formatted into the on-disk inode fork. We still change it,
3941 * though, so everything is consistent in memory.
3945 xfs_trans_ordered_buf(cur
->bc_tp
, bp
);
3946 xfs_btree_log_block(cur
, bp
, XFS_BB_OWNER
);
3948 xfs_buf_delwri_queue(bp
, buffer_list
);
3951 ASSERT(cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
);
3952 ASSERT(level
== cur
->bc_nlevels
- 1);
3955 /* now read rh sibling block for next iteration */
3956 xfs_btree_get_sibling(cur
, block
, &rptr
, XFS_BB_RIGHTSIB
);
3957 if (xfs_btree_ptr_is_null(cur
, &rptr
))
3960 return xfs_btree_lookup_get_block(cur
, level
, &rptr
, &block
);
3964 xfs_btree_change_owner(
3965 struct xfs_btree_cur
*cur
,
3966 __uint64_t new_owner
,
3967 struct list_head
*buffer_list
)
3969 union xfs_btree_ptr lptr
;
3971 struct xfs_btree_block
*block
= NULL
;
3974 cur
->bc_ops
->init_ptr_from_cur(cur
, &lptr
);
3976 /* for each level */
3977 for (level
= cur
->bc_nlevels
- 1; level
>= 0; level
--) {
3978 /* grab the left hand block */
3979 error
= xfs_btree_lookup_get_block(cur
, level
, &lptr
, &block
);
3983 /* readahead the left most block for the next level down */
3985 union xfs_btree_ptr
*ptr
;
3987 ptr
= xfs_btree_ptr_addr(cur
, 1, block
);
3988 xfs_btree_readahead_ptr(cur
, ptr
, 1);
3990 /* save for the next iteration of the loop */
3994 /* for each buffer in the level */
3996 error
= xfs_btree_block_change_owner(cur
, level
,
4001 if (error
!= ENOENT
)