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_format.h"
22 #include "xfs_log_format.h"
23 #include "xfs_trans_resv.h"
27 #include "xfs_mount.h"
28 #include "xfs_inode.h"
29 #include "xfs_trans.h"
30 #include "xfs_inode_item.h"
31 #include "xfs_buf_item.h"
32 #include "xfs_btree.h"
33 #include "xfs_error.h"
34 #include "xfs_trace.h"
35 #include "xfs_cksum.h"
38 * Cursor allocation zone.
40 kmem_zone_t
*xfs_btree_cur_zone
;
43 * Btree magic numbers.
45 static const __uint32_t xfs_magics
[2][XFS_BTNUM_MAX
] = {
46 { XFS_ABTB_MAGIC
, XFS_ABTC_MAGIC
, XFS_BMAP_MAGIC
, XFS_IBT_MAGIC
},
47 { XFS_ABTB_CRC_MAGIC
, XFS_ABTC_CRC_MAGIC
,
48 XFS_BMAP_CRC_MAGIC
, XFS_IBT_CRC_MAGIC
}
50 #define xfs_btree_magic(cur) \
51 xfs_magics[!!((cur)->bc_flags & XFS_BTREE_CRC_BLOCKS)][cur->bc_btnum]
54 STATIC
int /* error (0 or EFSCORRUPTED) */
55 xfs_btree_check_lblock(
56 struct xfs_btree_cur
*cur
, /* btree cursor */
57 struct xfs_btree_block
*block
, /* btree long form block pointer */
58 int level
, /* level of the btree block */
59 struct xfs_buf
*bp
) /* buffer for block, if any */
61 int lblock_ok
= 1; /* block passes checks */
62 struct xfs_mount
*mp
; /* file system mount point */
66 if (xfs_sb_version_hascrc(&mp
->m_sb
)) {
67 lblock_ok
= lblock_ok
&&
68 uuid_equal(&block
->bb_u
.l
.bb_uuid
, &mp
->m_sb
.sb_uuid
) &&
69 block
->bb_u
.l
.bb_blkno
== cpu_to_be64(
70 bp
? bp
->b_bn
: XFS_BUF_DADDR_NULL
);
73 lblock_ok
= lblock_ok
&&
74 be32_to_cpu(block
->bb_magic
) == xfs_btree_magic(cur
) &&
75 be16_to_cpu(block
->bb_level
) == level
&&
76 be16_to_cpu(block
->bb_numrecs
) <=
77 cur
->bc_ops
->get_maxrecs(cur
, level
) &&
78 block
->bb_u
.l
.bb_leftsib
&&
79 (block
->bb_u
.l
.bb_leftsib
== cpu_to_be64(NULLDFSBNO
) ||
80 XFS_FSB_SANITY_CHECK(mp
,
81 be64_to_cpu(block
->bb_u
.l
.bb_leftsib
))) &&
82 block
->bb_u
.l
.bb_rightsib
&&
83 (block
->bb_u
.l
.bb_rightsib
== cpu_to_be64(NULLDFSBNO
) ||
84 XFS_FSB_SANITY_CHECK(mp
,
85 be64_to_cpu(block
->bb_u
.l
.bb_rightsib
)));
87 if (unlikely(XFS_TEST_ERROR(!lblock_ok
, mp
,
88 XFS_ERRTAG_BTREE_CHECK_LBLOCK
,
89 XFS_RANDOM_BTREE_CHECK_LBLOCK
))) {
91 trace_xfs_btree_corrupt(bp
, _RET_IP_
);
92 XFS_ERROR_REPORT(__func__
, XFS_ERRLEVEL_LOW
, mp
);
93 return XFS_ERROR(EFSCORRUPTED
);
98 STATIC
int /* error (0 or EFSCORRUPTED) */
99 xfs_btree_check_sblock(
100 struct xfs_btree_cur
*cur
, /* btree cursor */
101 struct xfs_btree_block
*block
, /* btree short form block pointer */
102 int level
, /* level of the btree block */
103 struct xfs_buf
*bp
) /* buffer containing block */
105 struct xfs_mount
*mp
; /* file system mount point */
106 struct xfs_buf
*agbp
; /* buffer for ag. freespace struct */
107 struct xfs_agf
*agf
; /* ag. freespace structure */
108 xfs_agblock_t agflen
; /* native ag. freespace length */
109 int sblock_ok
= 1; /* block passes checks */
112 agbp
= cur
->bc_private
.a
.agbp
;
113 agf
= XFS_BUF_TO_AGF(agbp
);
114 agflen
= be32_to_cpu(agf
->agf_length
);
116 if (xfs_sb_version_hascrc(&mp
->m_sb
)) {
117 sblock_ok
= sblock_ok
&&
118 uuid_equal(&block
->bb_u
.s
.bb_uuid
, &mp
->m_sb
.sb_uuid
) &&
119 block
->bb_u
.s
.bb_blkno
== cpu_to_be64(
120 bp
? bp
->b_bn
: XFS_BUF_DADDR_NULL
);
123 sblock_ok
= sblock_ok
&&
124 be32_to_cpu(block
->bb_magic
) == xfs_btree_magic(cur
) &&
125 be16_to_cpu(block
->bb_level
) == level
&&
126 be16_to_cpu(block
->bb_numrecs
) <=
127 cur
->bc_ops
->get_maxrecs(cur
, level
) &&
128 (block
->bb_u
.s
.bb_leftsib
== cpu_to_be32(NULLAGBLOCK
) ||
129 be32_to_cpu(block
->bb_u
.s
.bb_leftsib
) < agflen
) &&
130 block
->bb_u
.s
.bb_leftsib
&&
131 (block
->bb_u
.s
.bb_rightsib
== cpu_to_be32(NULLAGBLOCK
) ||
132 be32_to_cpu(block
->bb_u
.s
.bb_rightsib
) < agflen
) &&
133 block
->bb_u
.s
.bb_rightsib
;
135 if (unlikely(XFS_TEST_ERROR(!sblock_ok
, mp
,
136 XFS_ERRTAG_BTREE_CHECK_SBLOCK
,
137 XFS_RANDOM_BTREE_CHECK_SBLOCK
))) {
139 trace_xfs_btree_corrupt(bp
, _RET_IP_
);
140 XFS_ERROR_REPORT(__func__
, XFS_ERRLEVEL_LOW
, mp
);
141 return XFS_ERROR(EFSCORRUPTED
);
147 * Debug routine: check that block header is ok.
150 xfs_btree_check_block(
151 struct xfs_btree_cur
*cur
, /* btree cursor */
152 struct xfs_btree_block
*block
, /* generic btree block pointer */
153 int level
, /* level of the btree block */
154 struct xfs_buf
*bp
) /* buffer containing block, if any */
156 if (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
)
157 return xfs_btree_check_lblock(cur
, block
, level
, bp
);
159 return xfs_btree_check_sblock(cur
, block
, level
, bp
);
163 * Check that (long) pointer is ok.
165 int /* error (0 or EFSCORRUPTED) */
166 xfs_btree_check_lptr(
167 struct xfs_btree_cur
*cur
, /* btree cursor */
168 xfs_dfsbno_t bno
, /* btree block disk address */
169 int level
) /* btree block level */
171 XFS_WANT_CORRUPTED_RETURN(
174 XFS_FSB_SANITY_CHECK(cur
->bc_mp
, bno
));
180 * Check that (short) pointer is ok.
182 STATIC
int /* error (0 or EFSCORRUPTED) */
183 xfs_btree_check_sptr(
184 struct xfs_btree_cur
*cur
, /* btree cursor */
185 xfs_agblock_t bno
, /* btree block disk address */
186 int level
) /* btree block level */
188 xfs_agblock_t agblocks
= cur
->bc_mp
->m_sb
.sb_agblocks
;
190 XFS_WANT_CORRUPTED_RETURN(
192 bno
!= NULLAGBLOCK
&&
199 * Check that block ptr is ok.
201 STATIC
int /* error (0 or EFSCORRUPTED) */
203 struct xfs_btree_cur
*cur
, /* btree cursor */
204 union xfs_btree_ptr
*ptr
, /* btree block disk address */
205 int index
, /* offset from ptr to check */
206 int level
) /* btree block level */
208 if (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
) {
209 return xfs_btree_check_lptr(cur
,
210 be64_to_cpu((&ptr
->l
)[index
]), level
);
212 return xfs_btree_check_sptr(cur
,
213 be32_to_cpu((&ptr
->s
)[index
]), level
);
219 * Calculate CRC on the whole btree block and stuff it into the
220 * long-form btree header.
222 * Prior to calculting the CRC, pull the LSN out of the buffer log item and put
223 * it into the buffer so recovery knows what the last modifcation was that made
227 xfs_btree_lblock_calc_crc(
230 struct xfs_btree_block
*block
= XFS_BUF_TO_BLOCK(bp
);
231 struct xfs_buf_log_item
*bip
= bp
->b_fspriv
;
233 if (!xfs_sb_version_hascrc(&bp
->b_target
->bt_mount
->m_sb
))
236 block
->bb_u
.l
.bb_lsn
= cpu_to_be64(bip
->bli_item
.li_lsn
);
237 xfs_update_cksum(bp
->b_addr
, BBTOB(bp
->b_length
),
238 XFS_BTREE_LBLOCK_CRC_OFF
);
242 xfs_btree_lblock_verify_crc(
245 if (xfs_sb_version_hascrc(&bp
->b_target
->bt_mount
->m_sb
))
246 return xfs_verify_cksum(bp
->b_addr
, BBTOB(bp
->b_length
),
247 XFS_BTREE_LBLOCK_CRC_OFF
);
252 * Calculate CRC on the whole btree block and stuff it into the
253 * short-form btree header.
255 * Prior to calculting the CRC, pull the LSN out of the buffer log item and put
256 * it into the buffer so recovery knows what the last modifcation was that made
260 xfs_btree_sblock_calc_crc(
263 struct xfs_btree_block
*block
= XFS_BUF_TO_BLOCK(bp
);
264 struct xfs_buf_log_item
*bip
= bp
->b_fspriv
;
266 if (!xfs_sb_version_hascrc(&bp
->b_target
->bt_mount
->m_sb
))
269 block
->bb_u
.s
.bb_lsn
= cpu_to_be64(bip
->bli_item
.li_lsn
);
270 xfs_update_cksum(bp
->b_addr
, BBTOB(bp
->b_length
),
271 XFS_BTREE_SBLOCK_CRC_OFF
);
275 xfs_btree_sblock_verify_crc(
278 if (xfs_sb_version_hascrc(&bp
->b_target
->bt_mount
->m_sb
))
279 return xfs_verify_cksum(bp
->b_addr
, BBTOB(bp
->b_length
),
280 XFS_BTREE_SBLOCK_CRC_OFF
);
285 * Delete the btree cursor.
288 xfs_btree_del_cursor(
289 xfs_btree_cur_t
*cur
, /* btree cursor */
290 int error
) /* del because of error */
292 int i
; /* btree level */
295 * Clear the buffer pointers, and release the buffers.
296 * If we're doing this in the face of an error, we
297 * need to make sure to inspect all of the entries
298 * in the bc_bufs array for buffers to be unlocked.
299 * This is because some of the btree code works from
300 * level n down to 0, and if we get an error along
301 * the way we won't have initialized all the entries
304 for (i
= 0; i
< cur
->bc_nlevels
; i
++) {
306 xfs_trans_brelse(cur
->bc_tp
, cur
->bc_bufs
[i
]);
311 * Can't free a bmap cursor without having dealt with the
312 * allocated indirect blocks' accounting.
314 ASSERT(cur
->bc_btnum
!= XFS_BTNUM_BMAP
||
315 cur
->bc_private
.b
.allocated
== 0);
319 kmem_zone_free(xfs_btree_cur_zone
, cur
);
323 * Duplicate the btree cursor.
324 * Allocate a new one, copy the record, re-get the buffers.
327 xfs_btree_dup_cursor(
328 xfs_btree_cur_t
*cur
, /* input cursor */
329 xfs_btree_cur_t
**ncur
) /* output cursor */
331 xfs_buf_t
*bp
; /* btree block's buffer pointer */
332 int error
; /* error return value */
333 int i
; /* level number of btree block */
334 xfs_mount_t
*mp
; /* mount structure for filesystem */
335 xfs_btree_cur_t
*new; /* new cursor value */
336 xfs_trans_t
*tp
; /* transaction pointer, can be NULL */
342 * Allocate a new cursor like the old one.
344 new = cur
->bc_ops
->dup_cursor(cur
);
347 * Copy the record currently in the cursor.
349 new->bc_rec
= cur
->bc_rec
;
352 * For each level current, re-get the buffer and copy the ptr value.
354 for (i
= 0; i
< new->bc_nlevels
; i
++) {
355 new->bc_ptrs
[i
] = cur
->bc_ptrs
[i
];
356 new->bc_ra
[i
] = cur
->bc_ra
[i
];
357 bp
= cur
->bc_bufs
[i
];
359 error
= xfs_trans_read_buf(mp
, tp
, mp
->m_ddev_targp
,
360 XFS_BUF_ADDR(bp
), mp
->m_bsize
,
362 cur
->bc_ops
->buf_ops
);
364 xfs_btree_del_cursor(new, error
);
369 new->bc_bufs
[i
] = bp
;
376 * XFS btree block layout and addressing:
378 * There are two types of blocks in the btree: leaf and non-leaf blocks.
380 * The leaf record start with a header then followed by records containing
381 * the values. A non-leaf block also starts with the same header, and
382 * then first contains lookup keys followed by an equal number of pointers
383 * to the btree blocks at the previous level.
385 * +--------+-------+-------+-------+-------+-------+-------+
386 * Leaf: | header | rec 1 | rec 2 | rec 3 | rec 4 | rec 5 | rec N |
387 * +--------+-------+-------+-------+-------+-------+-------+
389 * +--------+-------+-------+-------+-------+-------+-------+
390 * Non-Leaf: | header | key 1 | key 2 | key N | ptr 1 | ptr 2 | ptr N |
391 * +--------+-------+-------+-------+-------+-------+-------+
393 * The header is called struct xfs_btree_block for reasons better left unknown
394 * and comes in different versions for short (32bit) and long (64bit) block
395 * pointers. The record and key structures are defined by the btree instances
396 * and opaque to the btree core. The block pointers are simple disk endian
397 * integers, available in a short (32bit) and long (64bit) variant.
399 * The helpers below calculate the offset of a given record, key or pointer
400 * into a btree block (xfs_btree_*_offset) or return a pointer to the given
401 * record, key or pointer (xfs_btree_*_addr). Note that all addressing
402 * inside the btree block is done using indices starting at one, not zero!
406 * Return size of the btree block header for this btree instance.
408 static inline size_t xfs_btree_block_len(struct xfs_btree_cur
*cur
)
410 if (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
) {
411 if (cur
->bc_flags
& XFS_BTREE_CRC_BLOCKS
)
412 return XFS_BTREE_LBLOCK_CRC_LEN
;
413 return XFS_BTREE_LBLOCK_LEN
;
415 if (cur
->bc_flags
& XFS_BTREE_CRC_BLOCKS
)
416 return XFS_BTREE_SBLOCK_CRC_LEN
;
417 return XFS_BTREE_SBLOCK_LEN
;
421 * Return size of btree block pointers for this btree instance.
423 static inline size_t xfs_btree_ptr_len(struct xfs_btree_cur
*cur
)
425 return (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
) ?
426 sizeof(__be64
) : sizeof(__be32
);
430 * Calculate offset of the n-th record in a btree block.
433 xfs_btree_rec_offset(
434 struct xfs_btree_cur
*cur
,
437 return xfs_btree_block_len(cur
) +
438 (n
- 1) * cur
->bc_ops
->rec_len
;
442 * Calculate offset of the n-th key in a btree block.
445 xfs_btree_key_offset(
446 struct xfs_btree_cur
*cur
,
449 return xfs_btree_block_len(cur
) +
450 (n
- 1) * cur
->bc_ops
->key_len
;
454 * Calculate offset of the n-th block pointer in a btree block.
457 xfs_btree_ptr_offset(
458 struct xfs_btree_cur
*cur
,
462 return xfs_btree_block_len(cur
) +
463 cur
->bc_ops
->get_maxrecs(cur
, level
) * cur
->bc_ops
->key_len
+
464 (n
- 1) * xfs_btree_ptr_len(cur
);
468 * Return a pointer to the n-th record in the btree block.
470 STATIC
union xfs_btree_rec
*
472 struct xfs_btree_cur
*cur
,
474 struct xfs_btree_block
*block
)
476 return (union xfs_btree_rec
*)
477 ((char *)block
+ xfs_btree_rec_offset(cur
, n
));
481 * Return a pointer to the n-th key in the btree block.
483 STATIC
union xfs_btree_key
*
485 struct xfs_btree_cur
*cur
,
487 struct xfs_btree_block
*block
)
489 return (union xfs_btree_key
*)
490 ((char *)block
+ xfs_btree_key_offset(cur
, n
));
494 * Return a pointer to the n-th block pointer in the btree block.
496 STATIC
union xfs_btree_ptr
*
498 struct xfs_btree_cur
*cur
,
500 struct xfs_btree_block
*block
)
502 int level
= xfs_btree_get_level(block
);
504 ASSERT(block
->bb_level
!= 0);
506 return (union xfs_btree_ptr
*)
507 ((char *)block
+ xfs_btree_ptr_offset(cur
, n
, level
));
511 * Get the root block which is stored in the inode.
513 * For now this btree implementation assumes the btree root is always
514 * stored in the if_broot field of an inode fork.
516 STATIC
struct xfs_btree_block
*
518 struct xfs_btree_cur
*cur
)
520 struct xfs_ifork
*ifp
;
522 ifp
= XFS_IFORK_PTR(cur
->bc_private
.b
.ip
, cur
->bc_private
.b
.whichfork
);
523 return (struct xfs_btree_block
*)ifp
->if_broot
;
527 * Retrieve the block pointer from the cursor at the given level.
528 * This may be an inode btree root or from a buffer.
530 STATIC
struct xfs_btree_block
* /* generic btree block pointer */
532 struct xfs_btree_cur
*cur
, /* btree cursor */
533 int level
, /* level in btree */
534 struct xfs_buf
**bpp
) /* buffer containing the block */
536 if ((cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
) &&
537 (level
== cur
->bc_nlevels
- 1)) {
539 return xfs_btree_get_iroot(cur
);
542 *bpp
= cur
->bc_bufs
[level
];
543 return XFS_BUF_TO_BLOCK(*bpp
);
547 * Get a buffer for the block, return it with no data read.
548 * Long-form addressing.
550 xfs_buf_t
* /* buffer for fsbno */
552 xfs_mount_t
*mp
, /* file system mount point */
553 xfs_trans_t
*tp
, /* transaction pointer */
554 xfs_fsblock_t fsbno
, /* file system block number */
555 uint lock
) /* lock flags for get_buf */
557 xfs_buf_t
*bp
; /* buffer pointer (return value) */
558 xfs_daddr_t d
; /* real disk block address */
560 ASSERT(fsbno
!= NULLFSBLOCK
);
561 d
= XFS_FSB_TO_DADDR(mp
, fsbno
);
562 bp
= xfs_trans_get_buf(tp
, mp
->m_ddev_targp
, d
, mp
->m_bsize
, lock
);
563 ASSERT(!xfs_buf_geterror(bp
));
568 * Get a buffer for the block, return it with no data read.
569 * Short-form addressing.
571 xfs_buf_t
* /* buffer for agno/agbno */
573 xfs_mount_t
*mp
, /* file system mount point */
574 xfs_trans_t
*tp
, /* transaction pointer */
575 xfs_agnumber_t agno
, /* allocation group number */
576 xfs_agblock_t agbno
, /* allocation group block number */
577 uint lock
) /* lock flags for get_buf */
579 xfs_buf_t
*bp
; /* buffer pointer (return value) */
580 xfs_daddr_t d
; /* real disk block address */
582 ASSERT(agno
!= NULLAGNUMBER
);
583 ASSERT(agbno
!= NULLAGBLOCK
);
584 d
= XFS_AGB_TO_DADDR(mp
, agno
, agbno
);
585 bp
= xfs_trans_get_buf(tp
, mp
->m_ddev_targp
, d
, mp
->m_bsize
, lock
);
586 ASSERT(!xfs_buf_geterror(bp
));
591 * Check for the cursor referring to the last block at the given level.
593 int /* 1=is last block, 0=not last block */
594 xfs_btree_islastblock(
595 xfs_btree_cur_t
*cur
, /* btree cursor */
596 int level
) /* level to check */
598 struct xfs_btree_block
*block
; /* generic btree block pointer */
599 xfs_buf_t
*bp
; /* buffer containing block */
601 block
= xfs_btree_get_block(cur
, level
, &bp
);
602 xfs_btree_check_block(cur
, block
, level
, bp
);
603 if (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
)
604 return block
->bb_u
.l
.bb_rightsib
== cpu_to_be64(NULLDFSBNO
);
606 return block
->bb_u
.s
.bb_rightsib
== cpu_to_be32(NULLAGBLOCK
);
610 * Change the cursor to point to the first record at the given level.
611 * Other levels are unaffected.
613 STATIC
int /* success=1, failure=0 */
615 xfs_btree_cur_t
*cur
, /* btree cursor */
616 int level
) /* level to change */
618 struct xfs_btree_block
*block
; /* generic btree block pointer */
619 xfs_buf_t
*bp
; /* buffer containing block */
622 * Get the block pointer for this level.
624 block
= xfs_btree_get_block(cur
, level
, &bp
);
625 xfs_btree_check_block(cur
, block
, level
, bp
);
627 * It's empty, there is no such record.
629 if (!block
->bb_numrecs
)
632 * Set the ptr value to 1, that's the first record/key.
634 cur
->bc_ptrs
[level
] = 1;
639 * Change the cursor to point to the last record in the current block
640 * at the given level. Other levels are unaffected.
642 STATIC
int /* success=1, failure=0 */
644 xfs_btree_cur_t
*cur
, /* btree cursor */
645 int level
) /* level to change */
647 struct xfs_btree_block
*block
; /* generic btree block pointer */
648 xfs_buf_t
*bp
; /* buffer containing block */
651 * Get the block pointer for this level.
653 block
= xfs_btree_get_block(cur
, level
, &bp
);
654 xfs_btree_check_block(cur
, block
, level
, bp
);
656 * It's empty, there is no such record.
658 if (!block
->bb_numrecs
)
661 * Set the ptr value to numrecs, that's the last record/key.
663 cur
->bc_ptrs
[level
] = be16_to_cpu(block
->bb_numrecs
);
668 * Compute first and last byte offsets for the fields given.
669 * Interprets the offsets table, which contains struct field offsets.
673 __int64_t fields
, /* bitmask of fields */
674 const short *offsets
, /* table of field offsets */
675 int nbits
, /* number of bits to inspect */
676 int *first
, /* output: first byte offset */
677 int *last
) /* output: last byte offset */
679 int i
; /* current bit number */
680 __int64_t imask
; /* mask for current bit number */
684 * Find the lowest bit, so the first byte offset.
686 for (i
= 0, imask
= 1LL; ; i
++, imask
<<= 1) {
687 if (imask
& fields
) {
693 * Find the highest bit, so the last byte offset.
695 for (i
= nbits
- 1, imask
= 1LL << i
; ; i
--, imask
>>= 1) {
696 if (imask
& fields
) {
697 *last
= offsets
[i
+ 1] - 1;
704 * Get a buffer for the block, return it read in.
705 * Long-form addressing.
709 struct xfs_mount
*mp
, /* file system mount point */
710 struct xfs_trans
*tp
, /* transaction pointer */
711 xfs_fsblock_t fsbno
, /* file system block number */
712 uint lock
, /* lock flags for read_buf */
713 struct xfs_buf
**bpp
, /* buffer for fsbno */
714 int refval
, /* ref count value for buffer */
715 const struct xfs_buf_ops
*ops
)
717 struct xfs_buf
*bp
; /* return value */
718 xfs_daddr_t d
; /* real disk block address */
721 ASSERT(fsbno
!= NULLFSBLOCK
);
722 d
= XFS_FSB_TO_DADDR(mp
, fsbno
);
723 error
= xfs_trans_read_buf(mp
, tp
, mp
->m_ddev_targp
, d
,
724 mp
->m_bsize
, lock
, &bp
, ops
);
727 ASSERT(!xfs_buf_geterror(bp
));
729 xfs_buf_set_ref(bp
, refval
);
735 * Read-ahead the block, don't wait for it, don't return a buffer.
736 * Long-form addressing.
740 xfs_btree_reada_bufl(
741 struct xfs_mount
*mp
, /* file system mount point */
742 xfs_fsblock_t fsbno
, /* file system block number */
743 xfs_extlen_t count
, /* count of filesystem blocks */
744 const struct xfs_buf_ops
*ops
)
748 ASSERT(fsbno
!= NULLFSBLOCK
);
749 d
= XFS_FSB_TO_DADDR(mp
, fsbno
);
750 xfs_buf_readahead(mp
->m_ddev_targp
, d
, mp
->m_bsize
* count
, ops
);
754 * Read-ahead the block, don't wait for it, don't return a buffer.
755 * Short-form addressing.
759 xfs_btree_reada_bufs(
760 struct xfs_mount
*mp
, /* file system mount point */
761 xfs_agnumber_t agno
, /* allocation group number */
762 xfs_agblock_t agbno
, /* allocation group block number */
763 xfs_extlen_t count
, /* count of filesystem blocks */
764 const struct xfs_buf_ops
*ops
)
768 ASSERT(agno
!= NULLAGNUMBER
);
769 ASSERT(agbno
!= NULLAGBLOCK
);
770 d
= XFS_AGB_TO_DADDR(mp
, agno
, agbno
);
771 xfs_buf_readahead(mp
->m_ddev_targp
, d
, mp
->m_bsize
* count
, ops
);
775 xfs_btree_readahead_lblock(
776 struct xfs_btree_cur
*cur
,
778 struct xfs_btree_block
*block
)
781 xfs_dfsbno_t left
= be64_to_cpu(block
->bb_u
.l
.bb_leftsib
);
782 xfs_dfsbno_t right
= be64_to_cpu(block
->bb_u
.l
.bb_rightsib
);
784 if ((lr
& XFS_BTCUR_LEFTRA
) && left
!= NULLDFSBNO
) {
785 xfs_btree_reada_bufl(cur
->bc_mp
, left
, 1,
786 cur
->bc_ops
->buf_ops
);
790 if ((lr
& XFS_BTCUR_RIGHTRA
) && right
!= NULLDFSBNO
) {
791 xfs_btree_reada_bufl(cur
->bc_mp
, right
, 1,
792 cur
->bc_ops
->buf_ops
);
800 xfs_btree_readahead_sblock(
801 struct xfs_btree_cur
*cur
,
803 struct xfs_btree_block
*block
)
806 xfs_agblock_t left
= be32_to_cpu(block
->bb_u
.s
.bb_leftsib
);
807 xfs_agblock_t right
= be32_to_cpu(block
->bb_u
.s
.bb_rightsib
);
810 if ((lr
& XFS_BTCUR_LEFTRA
) && left
!= NULLAGBLOCK
) {
811 xfs_btree_reada_bufs(cur
->bc_mp
, cur
->bc_private
.a
.agno
,
812 left
, 1, cur
->bc_ops
->buf_ops
);
816 if ((lr
& XFS_BTCUR_RIGHTRA
) && right
!= NULLAGBLOCK
) {
817 xfs_btree_reada_bufs(cur
->bc_mp
, cur
->bc_private
.a
.agno
,
818 right
, 1, cur
->bc_ops
->buf_ops
);
826 * Read-ahead btree blocks, at the given level.
827 * Bits in lr are set from XFS_BTCUR_{LEFT,RIGHT}RA.
831 struct xfs_btree_cur
*cur
, /* btree cursor */
832 int lev
, /* level in btree */
833 int lr
) /* left/right bits */
835 struct xfs_btree_block
*block
;
838 * No readahead needed if we are at the root level and the
839 * btree root is stored in the inode.
841 if ((cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
) &&
842 (lev
== cur
->bc_nlevels
- 1))
845 if ((cur
->bc_ra
[lev
] | lr
) == cur
->bc_ra
[lev
])
848 cur
->bc_ra
[lev
] |= lr
;
849 block
= XFS_BUF_TO_BLOCK(cur
->bc_bufs
[lev
]);
851 if (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
)
852 return xfs_btree_readahead_lblock(cur
, lr
, block
);
853 return xfs_btree_readahead_sblock(cur
, lr
, block
);
857 xfs_btree_ptr_to_daddr(
858 struct xfs_btree_cur
*cur
,
859 union xfs_btree_ptr
*ptr
)
861 if (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
) {
862 ASSERT(ptr
->l
!= cpu_to_be64(NULLDFSBNO
));
864 return XFS_FSB_TO_DADDR(cur
->bc_mp
, be64_to_cpu(ptr
->l
));
866 ASSERT(cur
->bc_private
.a
.agno
!= NULLAGNUMBER
);
867 ASSERT(ptr
->s
!= cpu_to_be32(NULLAGBLOCK
));
869 return XFS_AGB_TO_DADDR(cur
->bc_mp
, cur
->bc_private
.a
.agno
,
870 be32_to_cpu(ptr
->s
));
875 * Readahead @count btree blocks at the given @ptr location.
877 * We don't need to care about long or short form btrees here as we have a
878 * method of converting the ptr directly to a daddr available to us.
881 xfs_btree_readahead_ptr(
882 struct xfs_btree_cur
*cur
,
883 union xfs_btree_ptr
*ptr
,
886 xfs_buf_readahead(cur
->bc_mp
->m_ddev_targp
,
887 xfs_btree_ptr_to_daddr(cur
, ptr
),
888 cur
->bc_mp
->m_bsize
* count
, cur
->bc_ops
->buf_ops
);
892 * Set the buffer for level "lev" in the cursor to bp, releasing
893 * any previous buffer.
897 xfs_btree_cur_t
*cur
, /* btree cursor */
898 int lev
, /* level in btree */
899 xfs_buf_t
*bp
) /* new buffer to set */
901 struct xfs_btree_block
*b
; /* btree block */
903 if (cur
->bc_bufs
[lev
])
904 xfs_trans_brelse(cur
->bc_tp
, cur
->bc_bufs
[lev
]);
905 cur
->bc_bufs
[lev
] = bp
;
908 b
= XFS_BUF_TO_BLOCK(bp
);
909 if (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
) {
910 if (b
->bb_u
.l
.bb_leftsib
== cpu_to_be64(NULLDFSBNO
))
911 cur
->bc_ra
[lev
] |= XFS_BTCUR_LEFTRA
;
912 if (b
->bb_u
.l
.bb_rightsib
== cpu_to_be64(NULLDFSBNO
))
913 cur
->bc_ra
[lev
] |= XFS_BTCUR_RIGHTRA
;
915 if (b
->bb_u
.s
.bb_leftsib
== cpu_to_be32(NULLAGBLOCK
))
916 cur
->bc_ra
[lev
] |= XFS_BTCUR_LEFTRA
;
917 if (b
->bb_u
.s
.bb_rightsib
== cpu_to_be32(NULLAGBLOCK
))
918 cur
->bc_ra
[lev
] |= XFS_BTCUR_RIGHTRA
;
923 xfs_btree_ptr_is_null(
924 struct xfs_btree_cur
*cur
,
925 union xfs_btree_ptr
*ptr
)
927 if (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
)
928 return ptr
->l
== cpu_to_be64(NULLDFSBNO
);
930 return ptr
->s
== cpu_to_be32(NULLAGBLOCK
);
934 xfs_btree_set_ptr_null(
935 struct xfs_btree_cur
*cur
,
936 union xfs_btree_ptr
*ptr
)
938 if (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
)
939 ptr
->l
= cpu_to_be64(NULLDFSBNO
);
941 ptr
->s
= cpu_to_be32(NULLAGBLOCK
);
945 * Get/set/init sibling pointers
948 xfs_btree_get_sibling(
949 struct xfs_btree_cur
*cur
,
950 struct xfs_btree_block
*block
,
951 union xfs_btree_ptr
*ptr
,
954 ASSERT(lr
== XFS_BB_LEFTSIB
|| lr
== XFS_BB_RIGHTSIB
);
956 if (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
) {
957 if (lr
== XFS_BB_RIGHTSIB
)
958 ptr
->l
= block
->bb_u
.l
.bb_rightsib
;
960 ptr
->l
= block
->bb_u
.l
.bb_leftsib
;
962 if (lr
== XFS_BB_RIGHTSIB
)
963 ptr
->s
= block
->bb_u
.s
.bb_rightsib
;
965 ptr
->s
= block
->bb_u
.s
.bb_leftsib
;
970 xfs_btree_set_sibling(
971 struct xfs_btree_cur
*cur
,
972 struct xfs_btree_block
*block
,
973 union xfs_btree_ptr
*ptr
,
976 ASSERT(lr
== XFS_BB_LEFTSIB
|| lr
== XFS_BB_RIGHTSIB
);
978 if (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
) {
979 if (lr
== XFS_BB_RIGHTSIB
)
980 block
->bb_u
.l
.bb_rightsib
= ptr
->l
;
982 block
->bb_u
.l
.bb_leftsib
= ptr
->l
;
984 if (lr
== XFS_BB_RIGHTSIB
)
985 block
->bb_u
.s
.bb_rightsib
= ptr
->s
;
987 block
->bb_u
.s
.bb_leftsib
= ptr
->s
;
992 xfs_btree_init_block_int(
993 struct xfs_mount
*mp
,
994 struct xfs_btree_block
*buf
,
1002 buf
->bb_magic
= cpu_to_be32(magic
);
1003 buf
->bb_level
= cpu_to_be16(level
);
1004 buf
->bb_numrecs
= cpu_to_be16(numrecs
);
1006 if (flags
& XFS_BTREE_LONG_PTRS
) {
1007 buf
->bb_u
.l
.bb_leftsib
= cpu_to_be64(NULLDFSBNO
);
1008 buf
->bb_u
.l
.bb_rightsib
= cpu_to_be64(NULLDFSBNO
);
1009 if (flags
& XFS_BTREE_CRC_BLOCKS
) {
1010 buf
->bb_u
.l
.bb_blkno
= cpu_to_be64(blkno
);
1011 buf
->bb_u
.l
.bb_owner
= cpu_to_be64(owner
);
1012 uuid_copy(&buf
->bb_u
.l
.bb_uuid
, &mp
->m_sb
.sb_uuid
);
1013 buf
->bb_u
.l
.bb_pad
= 0;
1014 buf
->bb_u
.l
.bb_lsn
= 0;
1017 /* owner is a 32 bit value on short blocks */
1018 __u32 __owner
= (__u32
)owner
;
1020 buf
->bb_u
.s
.bb_leftsib
= cpu_to_be32(NULLAGBLOCK
);
1021 buf
->bb_u
.s
.bb_rightsib
= cpu_to_be32(NULLAGBLOCK
);
1022 if (flags
& XFS_BTREE_CRC_BLOCKS
) {
1023 buf
->bb_u
.s
.bb_blkno
= cpu_to_be64(blkno
);
1024 buf
->bb_u
.s
.bb_owner
= cpu_to_be32(__owner
);
1025 uuid_copy(&buf
->bb_u
.s
.bb_uuid
, &mp
->m_sb
.sb_uuid
);
1026 buf
->bb_u
.s
.bb_lsn
= 0;
1032 xfs_btree_init_block(
1033 struct xfs_mount
*mp
,
1041 xfs_btree_init_block_int(mp
, XFS_BUF_TO_BLOCK(bp
), bp
->b_bn
,
1042 magic
, level
, numrecs
, owner
, flags
);
1046 xfs_btree_init_block_cur(
1047 struct xfs_btree_cur
*cur
,
1055 * we can pull the owner from the cursor right now as the different
1056 * owners align directly with the pointer size of the btree. This may
1057 * change in future, but is safe for current users of the generic btree
1060 if (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
)
1061 owner
= cur
->bc_private
.b
.ip
->i_ino
;
1063 owner
= cur
->bc_private
.a
.agno
;
1065 xfs_btree_init_block_int(cur
->bc_mp
, XFS_BUF_TO_BLOCK(bp
), bp
->b_bn
,
1066 xfs_btree_magic(cur
), level
, numrecs
,
1067 owner
, cur
->bc_flags
);
1071 * Return true if ptr is the last record in the btree and
1072 * we need to track updates to this record. The decision
1073 * will be further refined in the update_lastrec method.
1076 xfs_btree_is_lastrec(
1077 struct xfs_btree_cur
*cur
,
1078 struct xfs_btree_block
*block
,
1081 union xfs_btree_ptr ptr
;
1085 if (!(cur
->bc_flags
& XFS_BTREE_LASTREC_UPDATE
))
1088 xfs_btree_get_sibling(cur
, block
, &ptr
, XFS_BB_RIGHTSIB
);
1089 if (!xfs_btree_ptr_is_null(cur
, &ptr
))
1095 xfs_btree_buf_to_ptr(
1096 struct xfs_btree_cur
*cur
,
1098 union xfs_btree_ptr
*ptr
)
1100 if (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
)
1101 ptr
->l
= cpu_to_be64(XFS_DADDR_TO_FSB(cur
->bc_mp
,
1104 ptr
->s
= cpu_to_be32(xfs_daddr_to_agbno(cur
->bc_mp
,
1111 struct xfs_btree_cur
*cur
,
1114 switch (cur
->bc_btnum
) {
1117 xfs_buf_set_ref(bp
, XFS_ALLOC_BTREE_REF
);
1120 xfs_buf_set_ref(bp
, XFS_INO_BTREE_REF
);
1122 case XFS_BTNUM_BMAP
:
1123 xfs_buf_set_ref(bp
, XFS_BMAP_BTREE_REF
);
1131 xfs_btree_get_buf_block(
1132 struct xfs_btree_cur
*cur
,
1133 union xfs_btree_ptr
*ptr
,
1135 struct xfs_btree_block
**block
,
1136 struct xfs_buf
**bpp
)
1138 struct xfs_mount
*mp
= cur
->bc_mp
;
1141 /* need to sort out how callers deal with failures first */
1142 ASSERT(!(flags
& XBF_TRYLOCK
));
1144 d
= xfs_btree_ptr_to_daddr(cur
, ptr
);
1145 *bpp
= xfs_trans_get_buf(cur
->bc_tp
, mp
->m_ddev_targp
, d
,
1146 mp
->m_bsize
, flags
);
1151 (*bpp
)->b_ops
= cur
->bc_ops
->buf_ops
;
1152 *block
= XFS_BUF_TO_BLOCK(*bpp
);
1157 * Read in the buffer at the given ptr and return the buffer and
1158 * the block pointer within the buffer.
1161 xfs_btree_read_buf_block(
1162 struct xfs_btree_cur
*cur
,
1163 union xfs_btree_ptr
*ptr
,
1166 struct xfs_btree_block
**block
,
1167 struct xfs_buf
**bpp
)
1169 struct xfs_mount
*mp
= cur
->bc_mp
;
1173 /* need to sort out how callers deal with failures first */
1174 ASSERT(!(flags
& XBF_TRYLOCK
));
1176 d
= xfs_btree_ptr_to_daddr(cur
, ptr
);
1177 error
= xfs_trans_read_buf(mp
, cur
->bc_tp
, mp
->m_ddev_targp
, d
,
1178 mp
->m_bsize
, flags
, bpp
,
1179 cur
->bc_ops
->buf_ops
);
1183 ASSERT(!xfs_buf_geterror(*bpp
));
1184 xfs_btree_set_refs(cur
, *bpp
);
1185 *block
= XFS_BUF_TO_BLOCK(*bpp
);
1190 * Copy keys from one btree block to another.
1193 xfs_btree_copy_keys(
1194 struct xfs_btree_cur
*cur
,
1195 union xfs_btree_key
*dst_key
,
1196 union xfs_btree_key
*src_key
,
1199 ASSERT(numkeys
>= 0);
1200 memcpy(dst_key
, src_key
, numkeys
* cur
->bc_ops
->key_len
);
1204 * Copy records from one btree block to another.
1207 xfs_btree_copy_recs(
1208 struct xfs_btree_cur
*cur
,
1209 union xfs_btree_rec
*dst_rec
,
1210 union xfs_btree_rec
*src_rec
,
1213 ASSERT(numrecs
>= 0);
1214 memcpy(dst_rec
, src_rec
, numrecs
* cur
->bc_ops
->rec_len
);
1218 * Copy block pointers from one btree block to another.
1221 xfs_btree_copy_ptrs(
1222 struct xfs_btree_cur
*cur
,
1223 union xfs_btree_ptr
*dst_ptr
,
1224 union xfs_btree_ptr
*src_ptr
,
1227 ASSERT(numptrs
>= 0);
1228 memcpy(dst_ptr
, src_ptr
, numptrs
* xfs_btree_ptr_len(cur
));
1232 * Shift keys one index left/right inside a single btree block.
1235 xfs_btree_shift_keys(
1236 struct xfs_btree_cur
*cur
,
1237 union xfs_btree_key
*key
,
1243 ASSERT(numkeys
>= 0);
1244 ASSERT(dir
== 1 || dir
== -1);
1246 dst_key
= (char *)key
+ (dir
* cur
->bc_ops
->key_len
);
1247 memmove(dst_key
, key
, numkeys
* cur
->bc_ops
->key_len
);
1251 * Shift records one index left/right inside a single btree block.
1254 xfs_btree_shift_recs(
1255 struct xfs_btree_cur
*cur
,
1256 union xfs_btree_rec
*rec
,
1262 ASSERT(numrecs
>= 0);
1263 ASSERT(dir
== 1 || dir
== -1);
1265 dst_rec
= (char *)rec
+ (dir
* cur
->bc_ops
->rec_len
);
1266 memmove(dst_rec
, rec
, numrecs
* cur
->bc_ops
->rec_len
);
1270 * Shift block pointers one index left/right inside a single btree block.
1273 xfs_btree_shift_ptrs(
1274 struct xfs_btree_cur
*cur
,
1275 union xfs_btree_ptr
*ptr
,
1281 ASSERT(numptrs
>= 0);
1282 ASSERT(dir
== 1 || dir
== -1);
1284 dst_ptr
= (char *)ptr
+ (dir
* xfs_btree_ptr_len(cur
));
1285 memmove(dst_ptr
, ptr
, numptrs
* xfs_btree_ptr_len(cur
));
1289 * Log key values from the btree block.
1293 struct xfs_btree_cur
*cur
,
1298 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
1299 XFS_BTREE_TRACE_ARGBII(cur
, bp
, first
, last
);
1302 xfs_trans_buf_set_type(cur
->bc_tp
, bp
, XFS_BLFT_BTREE_BUF
);
1303 xfs_trans_log_buf(cur
->bc_tp
, bp
,
1304 xfs_btree_key_offset(cur
, first
),
1305 xfs_btree_key_offset(cur
, last
+ 1) - 1);
1307 xfs_trans_log_inode(cur
->bc_tp
, cur
->bc_private
.b
.ip
,
1308 xfs_ilog_fbroot(cur
->bc_private
.b
.whichfork
));
1311 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
1315 * Log record values from the btree block.
1319 struct xfs_btree_cur
*cur
,
1324 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
1325 XFS_BTREE_TRACE_ARGBII(cur
, bp
, first
, last
);
1327 xfs_trans_buf_set_type(cur
->bc_tp
, bp
, XFS_BLFT_BTREE_BUF
);
1328 xfs_trans_log_buf(cur
->bc_tp
, bp
,
1329 xfs_btree_rec_offset(cur
, first
),
1330 xfs_btree_rec_offset(cur
, last
+ 1) - 1);
1332 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
1336 * Log block pointer fields from a btree block (nonleaf).
1340 struct xfs_btree_cur
*cur
, /* btree cursor */
1341 struct xfs_buf
*bp
, /* buffer containing btree block */
1342 int first
, /* index of first pointer to log */
1343 int last
) /* index of last pointer to log */
1345 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
1346 XFS_BTREE_TRACE_ARGBII(cur
, bp
, first
, last
);
1349 struct xfs_btree_block
*block
= XFS_BUF_TO_BLOCK(bp
);
1350 int level
= xfs_btree_get_level(block
);
1352 xfs_trans_buf_set_type(cur
->bc_tp
, bp
, XFS_BLFT_BTREE_BUF
);
1353 xfs_trans_log_buf(cur
->bc_tp
, bp
,
1354 xfs_btree_ptr_offset(cur
, first
, level
),
1355 xfs_btree_ptr_offset(cur
, last
+ 1, level
) - 1);
1357 xfs_trans_log_inode(cur
->bc_tp
, cur
->bc_private
.b
.ip
,
1358 xfs_ilog_fbroot(cur
->bc_private
.b
.whichfork
));
1361 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
1365 * Log fields from a btree block header.
1368 xfs_btree_log_block(
1369 struct xfs_btree_cur
*cur
, /* btree cursor */
1370 struct xfs_buf
*bp
, /* buffer containing btree block */
1371 int fields
) /* mask of fields: XFS_BB_... */
1373 int first
; /* first byte offset logged */
1374 int last
; /* last byte offset logged */
1375 static const short soffsets
[] = { /* table of offsets (short) */
1376 offsetof(struct xfs_btree_block
, bb_magic
),
1377 offsetof(struct xfs_btree_block
, bb_level
),
1378 offsetof(struct xfs_btree_block
, bb_numrecs
),
1379 offsetof(struct xfs_btree_block
, bb_u
.s
.bb_leftsib
),
1380 offsetof(struct xfs_btree_block
, bb_u
.s
.bb_rightsib
),
1381 offsetof(struct xfs_btree_block
, bb_u
.s
.bb_blkno
),
1382 offsetof(struct xfs_btree_block
, bb_u
.s
.bb_lsn
),
1383 offsetof(struct xfs_btree_block
, bb_u
.s
.bb_uuid
),
1384 offsetof(struct xfs_btree_block
, bb_u
.s
.bb_owner
),
1385 offsetof(struct xfs_btree_block
, bb_u
.s
.bb_crc
),
1386 XFS_BTREE_SBLOCK_CRC_LEN
1388 static const short loffsets
[] = { /* table of offsets (long) */
1389 offsetof(struct xfs_btree_block
, bb_magic
),
1390 offsetof(struct xfs_btree_block
, bb_level
),
1391 offsetof(struct xfs_btree_block
, bb_numrecs
),
1392 offsetof(struct xfs_btree_block
, bb_u
.l
.bb_leftsib
),
1393 offsetof(struct xfs_btree_block
, bb_u
.l
.bb_rightsib
),
1394 offsetof(struct xfs_btree_block
, bb_u
.l
.bb_blkno
),
1395 offsetof(struct xfs_btree_block
, bb_u
.l
.bb_lsn
),
1396 offsetof(struct xfs_btree_block
, bb_u
.l
.bb_uuid
),
1397 offsetof(struct xfs_btree_block
, bb_u
.l
.bb_owner
),
1398 offsetof(struct xfs_btree_block
, bb_u
.l
.bb_crc
),
1399 offsetof(struct xfs_btree_block
, bb_u
.l
.bb_pad
),
1400 XFS_BTREE_LBLOCK_CRC_LEN
1403 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
1404 XFS_BTREE_TRACE_ARGBI(cur
, bp
, fields
);
1409 if (cur
->bc_flags
& XFS_BTREE_CRC_BLOCKS
) {
1411 * We don't log the CRC when updating a btree
1412 * block but instead recreate it during log
1413 * recovery. As the log buffers have checksums
1414 * of their own this is safe and avoids logging a crc
1415 * update in a lot of places.
1417 if (fields
== XFS_BB_ALL_BITS
)
1418 fields
= XFS_BB_ALL_BITS_CRC
;
1419 nbits
= XFS_BB_NUM_BITS_CRC
;
1421 nbits
= XFS_BB_NUM_BITS
;
1423 xfs_btree_offsets(fields
,
1424 (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
) ?
1425 loffsets
: soffsets
,
1426 nbits
, &first
, &last
);
1427 xfs_trans_buf_set_type(cur
->bc_tp
, bp
, XFS_BLFT_BTREE_BUF
);
1428 xfs_trans_log_buf(cur
->bc_tp
, bp
, first
, last
);
1430 xfs_trans_log_inode(cur
->bc_tp
, cur
->bc_private
.b
.ip
,
1431 xfs_ilog_fbroot(cur
->bc_private
.b
.whichfork
));
1434 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
1438 * Increment cursor by one record at the level.
1439 * For nonzero levels the leaf-ward information is untouched.
1442 xfs_btree_increment(
1443 struct xfs_btree_cur
*cur
,
1445 int *stat
) /* success/failure */
1447 struct xfs_btree_block
*block
;
1448 union xfs_btree_ptr ptr
;
1450 int error
; /* error return value */
1453 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
1454 XFS_BTREE_TRACE_ARGI(cur
, level
);
1456 ASSERT(level
< cur
->bc_nlevels
);
1458 /* Read-ahead to the right at this level. */
1459 xfs_btree_readahead(cur
, level
, XFS_BTCUR_RIGHTRA
);
1461 /* Get a pointer to the btree block. */
1462 block
= xfs_btree_get_block(cur
, level
, &bp
);
1465 error
= xfs_btree_check_block(cur
, block
, level
, bp
);
1470 /* We're done if we remain in the block after the increment. */
1471 if (++cur
->bc_ptrs
[level
] <= xfs_btree_get_numrecs(block
))
1474 /* Fail if we just went off the right edge of the tree. */
1475 xfs_btree_get_sibling(cur
, block
, &ptr
, XFS_BB_RIGHTSIB
);
1476 if (xfs_btree_ptr_is_null(cur
, &ptr
))
1479 XFS_BTREE_STATS_INC(cur
, increment
);
1482 * March up the tree incrementing pointers.
1483 * Stop when we don't go off the right edge of a block.
1485 for (lev
= level
+ 1; lev
< cur
->bc_nlevels
; lev
++) {
1486 block
= xfs_btree_get_block(cur
, lev
, &bp
);
1489 error
= xfs_btree_check_block(cur
, block
, lev
, bp
);
1494 if (++cur
->bc_ptrs
[lev
] <= xfs_btree_get_numrecs(block
))
1497 /* Read-ahead the right block for the next loop. */
1498 xfs_btree_readahead(cur
, lev
, XFS_BTCUR_RIGHTRA
);
1502 * If we went off the root then we are either seriously
1503 * confused or have the tree root in an inode.
1505 if (lev
== cur
->bc_nlevels
) {
1506 if (cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
)
1509 error
= EFSCORRUPTED
;
1512 ASSERT(lev
< cur
->bc_nlevels
);
1515 * Now walk back down the tree, fixing up the cursor's buffer
1516 * pointers and key numbers.
1518 for (block
= xfs_btree_get_block(cur
, lev
, &bp
); lev
> level
; ) {
1519 union xfs_btree_ptr
*ptrp
;
1521 ptrp
= xfs_btree_ptr_addr(cur
, cur
->bc_ptrs
[lev
], block
);
1522 error
= xfs_btree_read_buf_block(cur
, ptrp
, --lev
,
1527 xfs_btree_setbuf(cur
, lev
, bp
);
1528 cur
->bc_ptrs
[lev
] = 1;
1531 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
1536 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
1541 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
1546 * Decrement cursor by one record at the level.
1547 * For nonzero levels the leaf-ward information is untouched.
1550 xfs_btree_decrement(
1551 struct xfs_btree_cur
*cur
,
1553 int *stat
) /* success/failure */
1555 struct xfs_btree_block
*block
;
1557 int error
; /* error return value */
1559 union xfs_btree_ptr ptr
;
1561 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
1562 XFS_BTREE_TRACE_ARGI(cur
, level
);
1564 ASSERT(level
< cur
->bc_nlevels
);
1566 /* Read-ahead to the left at this level. */
1567 xfs_btree_readahead(cur
, level
, XFS_BTCUR_LEFTRA
);
1569 /* We're done if we remain in the block after the decrement. */
1570 if (--cur
->bc_ptrs
[level
] > 0)
1573 /* Get a pointer to the btree block. */
1574 block
= xfs_btree_get_block(cur
, level
, &bp
);
1577 error
= xfs_btree_check_block(cur
, block
, level
, bp
);
1582 /* Fail if we just went off the left edge of the tree. */
1583 xfs_btree_get_sibling(cur
, block
, &ptr
, XFS_BB_LEFTSIB
);
1584 if (xfs_btree_ptr_is_null(cur
, &ptr
))
1587 XFS_BTREE_STATS_INC(cur
, decrement
);
1590 * March up the tree decrementing pointers.
1591 * Stop when we don't go off the left edge of a block.
1593 for (lev
= level
+ 1; lev
< cur
->bc_nlevels
; lev
++) {
1594 if (--cur
->bc_ptrs
[lev
] > 0)
1596 /* Read-ahead the left block for the next loop. */
1597 xfs_btree_readahead(cur
, lev
, XFS_BTCUR_LEFTRA
);
1601 * If we went off the root then we are seriously confused.
1602 * or the root of the tree is in an inode.
1604 if (lev
== cur
->bc_nlevels
) {
1605 if (cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
)
1608 error
= EFSCORRUPTED
;
1611 ASSERT(lev
< cur
->bc_nlevels
);
1614 * Now walk back down the tree, fixing up the cursor's buffer
1615 * pointers and key numbers.
1617 for (block
= xfs_btree_get_block(cur
, lev
, &bp
); lev
> level
; ) {
1618 union xfs_btree_ptr
*ptrp
;
1620 ptrp
= xfs_btree_ptr_addr(cur
, cur
->bc_ptrs
[lev
], block
);
1621 error
= xfs_btree_read_buf_block(cur
, ptrp
, --lev
,
1625 xfs_btree_setbuf(cur
, lev
, bp
);
1626 cur
->bc_ptrs
[lev
] = xfs_btree_get_numrecs(block
);
1629 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
1634 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
1639 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
1644 xfs_btree_lookup_get_block(
1645 struct xfs_btree_cur
*cur
, /* btree cursor */
1646 int level
, /* level in the btree */
1647 union xfs_btree_ptr
*pp
, /* ptr to btree block */
1648 struct xfs_btree_block
**blkp
) /* return btree block */
1650 struct xfs_buf
*bp
; /* buffer pointer for btree block */
1653 /* special case the root block if in an inode */
1654 if ((cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
) &&
1655 (level
== cur
->bc_nlevels
- 1)) {
1656 *blkp
= xfs_btree_get_iroot(cur
);
1661 * If the old buffer at this level for the disk address we are
1662 * looking for re-use it.
1664 * Otherwise throw it away and get a new one.
1666 bp
= cur
->bc_bufs
[level
];
1667 if (bp
&& XFS_BUF_ADDR(bp
) == xfs_btree_ptr_to_daddr(cur
, pp
)) {
1668 *blkp
= XFS_BUF_TO_BLOCK(bp
);
1672 error
= xfs_btree_read_buf_block(cur
, pp
, level
, 0, blkp
, &bp
);
1676 xfs_btree_setbuf(cur
, level
, bp
);
1681 * Get current search key. For level 0 we don't actually have a key
1682 * structure so we make one up from the record. For all other levels
1683 * we just return the right key.
1685 STATIC
union xfs_btree_key
*
1686 xfs_lookup_get_search_key(
1687 struct xfs_btree_cur
*cur
,
1690 struct xfs_btree_block
*block
,
1691 union xfs_btree_key
*kp
)
1694 cur
->bc_ops
->init_key_from_rec(kp
,
1695 xfs_btree_rec_addr(cur
, keyno
, block
));
1699 return xfs_btree_key_addr(cur
, keyno
, block
);
1703 * Lookup the record. The cursor is made to point to it, based on dir.
1704 * stat is set to 0 if can't find any such record, 1 for success.
1708 struct xfs_btree_cur
*cur
, /* btree cursor */
1709 xfs_lookup_t dir
, /* <=, ==, or >= */
1710 int *stat
) /* success/failure */
1712 struct xfs_btree_block
*block
; /* current btree block */
1713 __int64_t diff
; /* difference for the current key */
1714 int error
; /* error return value */
1715 int keyno
; /* current key number */
1716 int level
; /* level in the btree */
1717 union xfs_btree_ptr
*pp
; /* ptr to btree block */
1718 union xfs_btree_ptr ptr
; /* ptr to btree block */
1720 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
1721 XFS_BTREE_TRACE_ARGI(cur
, dir
);
1723 XFS_BTREE_STATS_INC(cur
, lookup
);
1728 /* initialise start pointer from cursor */
1729 cur
->bc_ops
->init_ptr_from_cur(cur
, &ptr
);
1733 * Iterate over each level in the btree, starting at the root.
1734 * For each level above the leaves, find the key we need, based
1735 * on the lookup record, then follow the corresponding block
1736 * pointer down to the next level.
1738 for (level
= cur
->bc_nlevels
- 1, diff
= 1; level
>= 0; level
--) {
1739 /* Get the block we need to do the lookup on. */
1740 error
= xfs_btree_lookup_get_block(cur
, level
, pp
, &block
);
1746 * If we already had a key match at a higher level, we
1747 * know we need to use the first entry in this block.
1751 /* Otherwise search this block. Do a binary search. */
1753 int high
; /* high entry number */
1754 int low
; /* low entry number */
1756 /* Set low and high entry numbers, 1-based. */
1758 high
= xfs_btree_get_numrecs(block
);
1760 /* Block is empty, must be an empty leaf. */
1761 ASSERT(level
== 0 && cur
->bc_nlevels
== 1);
1763 cur
->bc_ptrs
[0] = dir
!= XFS_LOOKUP_LE
;
1764 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
1769 /* Binary search the block. */
1770 while (low
<= high
) {
1771 union xfs_btree_key key
;
1772 union xfs_btree_key
*kp
;
1774 XFS_BTREE_STATS_INC(cur
, compare
);
1776 /* keyno is average of low and high. */
1777 keyno
= (low
+ high
) >> 1;
1779 /* Get current search key */
1780 kp
= xfs_lookup_get_search_key(cur
, level
,
1781 keyno
, block
, &key
);
1784 * Compute difference to get next direction:
1785 * - less than, move right
1786 * - greater than, move left
1787 * - equal, we're done
1789 diff
= cur
->bc_ops
->key_diff(cur
, kp
);
1800 * If there are more levels, set up for the next level
1801 * by getting the block number and filling in the cursor.
1805 * If we moved left, need the previous key number,
1806 * unless there isn't one.
1808 if (diff
> 0 && --keyno
< 1)
1810 pp
= xfs_btree_ptr_addr(cur
, keyno
, block
);
1813 error
= xfs_btree_check_ptr(cur
, pp
, 0, level
);
1817 cur
->bc_ptrs
[level
] = keyno
;
1821 /* Done with the search. See if we need to adjust the results. */
1822 if (dir
!= XFS_LOOKUP_LE
&& diff
< 0) {
1825 * If ge search and we went off the end of the block, but it's
1826 * not the last block, we're in the wrong block.
1828 xfs_btree_get_sibling(cur
, block
, &ptr
, XFS_BB_RIGHTSIB
);
1829 if (dir
== XFS_LOOKUP_GE
&&
1830 keyno
> xfs_btree_get_numrecs(block
) &&
1831 !xfs_btree_ptr_is_null(cur
, &ptr
)) {
1834 cur
->bc_ptrs
[0] = keyno
;
1835 error
= xfs_btree_increment(cur
, 0, &i
);
1838 XFS_WANT_CORRUPTED_RETURN(i
== 1);
1839 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
1843 } else if (dir
== XFS_LOOKUP_LE
&& diff
> 0)
1845 cur
->bc_ptrs
[0] = keyno
;
1847 /* Return if we succeeded or not. */
1848 if (keyno
== 0 || keyno
> xfs_btree_get_numrecs(block
))
1850 else if (dir
!= XFS_LOOKUP_EQ
|| diff
== 0)
1854 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
1858 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
1863 * Update keys at all levels from here to the root along the cursor's path.
1867 struct xfs_btree_cur
*cur
,
1868 union xfs_btree_key
*keyp
,
1871 struct xfs_btree_block
*block
;
1873 union xfs_btree_key
*kp
;
1876 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
1877 XFS_BTREE_TRACE_ARGIK(cur
, level
, keyp
);
1879 ASSERT(!(cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
) || level
>= 1);
1882 * Go up the tree from this level toward the root.
1883 * At each level, update the key value to the value input.
1884 * Stop when we reach a level where the cursor isn't pointing
1885 * at the first entry in the block.
1887 for (ptr
= 1; ptr
== 1 && level
< cur
->bc_nlevels
; level
++) {
1891 block
= xfs_btree_get_block(cur
, level
, &bp
);
1893 error
= xfs_btree_check_block(cur
, block
, level
, bp
);
1895 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
1899 ptr
= cur
->bc_ptrs
[level
];
1900 kp
= xfs_btree_key_addr(cur
, ptr
, block
);
1901 xfs_btree_copy_keys(cur
, kp
, keyp
, 1);
1902 xfs_btree_log_keys(cur
, bp
, ptr
, ptr
);
1905 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
1910 * Update the record referred to by cur to the value in the
1911 * given record. This either works (return 0) or gets an
1912 * EFSCORRUPTED error.
1916 struct xfs_btree_cur
*cur
,
1917 union xfs_btree_rec
*rec
)
1919 struct xfs_btree_block
*block
;
1923 union xfs_btree_rec
*rp
;
1925 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
1926 XFS_BTREE_TRACE_ARGR(cur
, rec
);
1928 /* Pick up the current block. */
1929 block
= xfs_btree_get_block(cur
, 0, &bp
);
1932 error
= xfs_btree_check_block(cur
, block
, 0, bp
);
1936 /* Get the address of the rec to be updated. */
1937 ptr
= cur
->bc_ptrs
[0];
1938 rp
= xfs_btree_rec_addr(cur
, ptr
, block
);
1940 /* Fill in the new contents and log them. */
1941 xfs_btree_copy_recs(cur
, rp
, rec
, 1);
1942 xfs_btree_log_recs(cur
, bp
, ptr
, ptr
);
1945 * If we are tracking the last record in the tree and
1946 * we are at the far right edge of the tree, update it.
1948 if (xfs_btree_is_lastrec(cur
, block
, 0)) {
1949 cur
->bc_ops
->update_lastrec(cur
, block
, rec
,
1950 ptr
, LASTREC_UPDATE
);
1953 /* Updating first rec in leaf. Pass new key value up to our parent. */
1955 union xfs_btree_key key
;
1957 cur
->bc_ops
->init_key_from_rec(&key
, rec
);
1958 error
= xfs_btree_updkey(cur
, &key
, 1);
1963 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
1967 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
1972 * Move 1 record left from cur/level if possible.
1973 * Update cur to reflect the new path.
1975 STATIC
int /* error */
1977 struct xfs_btree_cur
*cur
,
1979 int *stat
) /* success/failure */
1981 union xfs_btree_key key
; /* btree key */
1982 struct xfs_buf
*lbp
; /* left buffer pointer */
1983 struct xfs_btree_block
*left
; /* left btree block */
1984 int lrecs
; /* left record count */
1985 struct xfs_buf
*rbp
; /* right buffer pointer */
1986 struct xfs_btree_block
*right
; /* right btree block */
1987 int rrecs
; /* right record count */
1988 union xfs_btree_ptr lptr
; /* left btree pointer */
1989 union xfs_btree_key
*rkp
= NULL
; /* right btree key */
1990 union xfs_btree_ptr
*rpp
= NULL
; /* right address pointer */
1991 union xfs_btree_rec
*rrp
= NULL
; /* right record pointer */
1992 int error
; /* error return value */
1994 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
1995 XFS_BTREE_TRACE_ARGI(cur
, level
);
1997 if ((cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
) &&
1998 level
== cur
->bc_nlevels
- 1)
2001 /* Set up variables for this block as "right". */
2002 right
= xfs_btree_get_block(cur
, level
, &rbp
);
2005 error
= xfs_btree_check_block(cur
, right
, level
, rbp
);
2010 /* If we've got no left sibling then we can't shift an entry left. */
2011 xfs_btree_get_sibling(cur
, right
, &lptr
, XFS_BB_LEFTSIB
);
2012 if (xfs_btree_ptr_is_null(cur
, &lptr
))
2016 * If the cursor entry is the one that would be moved, don't
2017 * do it... it's too complicated.
2019 if (cur
->bc_ptrs
[level
] <= 1)
2022 /* Set up the left neighbor as "left". */
2023 error
= xfs_btree_read_buf_block(cur
, &lptr
, level
, 0, &left
, &lbp
);
2027 /* If it's full, it can't take another entry. */
2028 lrecs
= xfs_btree_get_numrecs(left
);
2029 if (lrecs
== cur
->bc_ops
->get_maxrecs(cur
, level
))
2032 rrecs
= xfs_btree_get_numrecs(right
);
2035 * We add one entry to the left side and remove one for the right side.
2036 * Account for it here, the changes will be updated on disk and logged
2042 XFS_BTREE_STATS_INC(cur
, lshift
);
2043 XFS_BTREE_STATS_ADD(cur
, moves
, 1);
2046 * If non-leaf, copy a key and a ptr to the left block.
2047 * Log the changes to the left block.
2050 /* It's a non-leaf. Move keys and pointers. */
2051 union xfs_btree_key
*lkp
; /* left btree key */
2052 union xfs_btree_ptr
*lpp
; /* left address pointer */
2054 lkp
= xfs_btree_key_addr(cur
, lrecs
, left
);
2055 rkp
= xfs_btree_key_addr(cur
, 1, right
);
2057 lpp
= xfs_btree_ptr_addr(cur
, lrecs
, left
);
2058 rpp
= xfs_btree_ptr_addr(cur
, 1, right
);
2060 error
= xfs_btree_check_ptr(cur
, rpp
, 0, level
);
2064 xfs_btree_copy_keys(cur
, lkp
, rkp
, 1);
2065 xfs_btree_copy_ptrs(cur
, lpp
, rpp
, 1);
2067 xfs_btree_log_keys(cur
, lbp
, lrecs
, lrecs
);
2068 xfs_btree_log_ptrs(cur
, lbp
, lrecs
, lrecs
);
2070 ASSERT(cur
->bc_ops
->keys_inorder(cur
,
2071 xfs_btree_key_addr(cur
, lrecs
- 1, left
), lkp
));
2073 /* It's a leaf. Move records. */
2074 union xfs_btree_rec
*lrp
; /* left record pointer */
2076 lrp
= xfs_btree_rec_addr(cur
, lrecs
, left
);
2077 rrp
= xfs_btree_rec_addr(cur
, 1, right
);
2079 xfs_btree_copy_recs(cur
, lrp
, rrp
, 1);
2080 xfs_btree_log_recs(cur
, lbp
, lrecs
, lrecs
);
2082 ASSERT(cur
->bc_ops
->recs_inorder(cur
,
2083 xfs_btree_rec_addr(cur
, lrecs
- 1, left
), lrp
));
2086 xfs_btree_set_numrecs(left
, lrecs
);
2087 xfs_btree_log_block(cur
, lbp
, XFS_BB_NUMRECS
);
2089 xfs_btree_set_numrecs(right
, rrecs
);
2090 xfs_btree_log_block(cur
, rbp
, XFS_BB_NUMRECS
);
2093 * Slide the contents of right down one entry.
2095 XFS_BTREE_STATS_ADD(cur
, moves
, rrecs
- 1);
2097 /* It's a nonleaf. operate on keys and ptrs */
2099 int i
; /* loop index */
2101 for (i
= 0; i
< rrecs
; i
++) {
2102 error
= xfs_btree_check_ptr(cur
, rpp
, i
+ 1, level
);
2107 xfs_btree_shift_keys(cur
,
2108 xfs_btree_key_addr(cur
, 2, right
),
2110 xfs_btree_shift_ptrs(cur
,
2111 xfs_btree_ptr_addr(cur
, 2, right
),
2114 xfs_btree_log_keys(cur
, rbp
, 1, rrecs
);
2115 xfs_btree_log_ptrs(cur
, rbp
, 1, rrecs
);
2117 /* It's a leaf. operate on records */
2118 xfs_btree_shift_recs(cur
,
2119 xfs_btree_rec_addr(cur
, 2, right
),
2121 xfs_btree_log_recs(cur
, rbp
, 1, rrecs
);
2124 * If it's the first record in the block, we'll need a key
2125 * structure to pass up to the next level (updkey).
2127 cur
->bc_ops
->init_key_from_rec(&key
,
2128 xfs_btree_rec_addr(cur
, 1, right
));
2132 /* Update the parent key values of right. */
2133 error
= xfs_btree_updkey(cur
, rkp
, level
+ 1);
2137 /* Slide the cursor value left one. */
2138 cur
->bc_ptrs
[level
]--;
2140 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
2145 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
2150 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
2155 * Move 1 record right from cur/level if possible.
2156 * Update cur to reflect the new path.
2158 STATIC
int /* error */
2160 struct xfs_btree_cur
*cur
,
2162 int *stat
) /* success/failure */
2164 union xfs_btree_key key
; /* btree key */
2165 struct xfs_buf
*lbp
; /* left buffer pointer */
2166 struct xfs_btree_block
*left
; /* left btree block */
2167 struct xfs_buf
*rbp
; /* right buffer pointer */
2168 struct xfs_btree_block
*right
; /* right btree block */
2169 struct xfs_btree_cur
*tcur
; /* temporary btree cursor */
2170 union xfs_btree_ptr rptr
; /* right block pointer */
2171 union xfs_btree_key
*rkp
; /* right btree key */
2172 int rrecs
; /* right record count */
2173 int lrecs
; /* left record count */
2174 int error
; /* error return value */
2175 int i
; /* loop counter */
2177 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
2178 XFS_BTREE_TRACE_ARGI(cur
, level
);
2180 if ((cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
) &&
2181 (level
== cur
->bc_nlevels
- 1))
2184 /* Set up variables for this block as "left". */
2185 left
= xfs_btree_get_block(cur
, level
, &lbp
);
2188 error
= xfs_btree_check_block(cur
, left
, level
, lbp
);
2193 /* If we've got no right sibling then we can't shift an entry right. */
2194 xfs_btree_get_sibling(cur
, left
, &rptr
, XFS_BB_RIGHTSIB
);
2195 if (xfs_btree_ptr_is_null(cur
, &rptr
))
2199 * If the cursor entry is the one that would be moved, don't
2200 * do it... it's too complicated.
2202 lrecs
= xfs_btree_get_numrecs(left
);
2203 if (cur
->bc_ptrs
[level
] >= lrecs
)
2206 /* Set up the right neighbor as "right". */
2207 error
= xfs_btree_read_buf_block(cur
, &rptr
, level
, 0, &right
, &rbp
);
2211 /* If it's full, it can't take another entry. */
2212 rrecs
= xfs_btree_get_numrecs(right
);
2213 if (rrecs
== cur
->bc_ops
->get_maxrecs(cur
, level
))
2216 XFS_BTREE_STATS_INC(cur
, rshift
);
2217 XFS_BTREE_STATS_ADD(cur
, moves
, rrecs
);
2220 * Make a hole at the start of the right neighbor block, then
2221 * copy the last left block entry to the hole.
2224 /* It's a nonleaf. make a hole in the keys and ptrs */
2225 union xfs_btree_key
*lkp
;
2226 union xfs_btree_ptr
*lpp
;
2227 union xfs_btree_ptr
*rpp
;
2229 lkp
= xfs_btree_key_addr(cur
, lrecs
, left
);
2230 lpp
= xfs_btree_ptr_addr(cur
, lrecs
, left
);
2231 rkp
= xfs_btree_key_addr(cur
, 1, right
);
2232 rpp
= xfs_btree_ptr_addr(cur
, 1, right
);
2235 for (i
= rrecs
- 1; i
>= 0; i
--) {
2236 error
= xfs_btree_check_ptr(cur
, rpp
, i
, level
);
2242 xfs_btree_shift_keys(cur
, rkp
, 1, rrecs
);
2243 xfs_btree_shift_ptrs(cur
, rpp
, 1, rrecs
);
2246 error
= xfs_btree_check_ptr(cur
, lpp
, 0, level
);
2251 /* Now put the new data in, and log it. */
2252 xfs_btree_copy_keys(cur
, rkp
, lkp
, 1);
2253 xfs_btree_copy_ptrs(cur
, rpp
, lpp
, 1);
2255 xfs_btree_log_keys(cur
, rbp
, 1, rrecs
+ 1);
2256 xfs_btree_log_ptrs(cur
, rbp
, 1, rrecs
+ 1);
2258 ASSERT(cur
->bc_ops
->keys_inorder(cur
, rkp
,
2259 xfs_btree_key_addr(cur
, 2, right
)));
2261 /* It's a leaf. make a hole in the records */
2262 union xfs_btree_rec
*lrp
;
2263 union xfs_btree_rec
*rrp
;
2265 lrp
= xfs_btree_rec_addr(cur
, lrecs
, left
);
2266 rrp
= xfs_btree_rec_addr(cur
, 1, right
);
2268 xfs_btree_shift_recs(cur
, rrp
, 1, rrecs
);
2270 /* Now put the new data in, and log it. */
2271 xfs_btree_copy_recs(cur
, rrp
, lrp
, 1);
2272 xfs_btree_log_recs(cur
, rbp
, 1, rrecs
+ 1);
2274 cur
->bc_ops
->init_key_from_rec(&key
, rrp
);
2277 ASSERT(cur
->bc_ops
->recs_inorder(cur
, rrp
,
2278 xfs_btree_rec_addr(cur
, 2, right
)));
2282 * Decrement and log left's numrecs, bump and log right's numrecs.
2284 xfs_btree_set_numrecs(left
, --lrecs
);
2285 xfs_btree_log_block(cur
, lbp
, XFS_BB_NUMRECS
);
2287 xfs_btree_set_numrecs(right
, ++rrecs
);
2288 xfs_btree_log_block(cur
, rbp
, XFS_BB_NUMRECS
);
2291 * Using a temporary cursor, update the parent key values of the
2292 * block on the right.
2294 error
= xfs_btree_dup_cursor(cur
, &tcur
);
2297 i
= xfs_btree_lastrec(tcur
, level
);
2298 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
2300 error
= xfs_btree_increment(tcur
, level
, &i
);
2304 error
= xfs_btree_updkey(tcur
, rkp
, level
+ 1);
2308 xfs_btree_del_cursor(tcur
, XFS_BTREE_NOERROR
);
2310 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
2315 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
2320 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
2324 XFS_BTREE_TRACE_CURSOR(tcur
, XBT_ERROR
);
2325 xfs_btree_del_cursor(tcur
, XFS_BTREE_ERROR
);
2330 * Split cur/level block in half.
2331 * Return new block number and the key to its first
2332 * record (to be inserted into parent).
2334 STATIC
int /* error */
2336 struct xfs_btree_cur
*cur
,
2338 union xfs_btree_ptr
*ptrp
,
2339 union xfs_btree_key
*key
,
2340 struct xfs_btree_cur
**curp
,
2341 int *stat
) /* success/failure */
2343 union xfs_btree_ptr lptr
; /* left sibling block ptr */
2344 struct xfs_buf
*lbp
; /* left buffer pointer */
2345 struct xfs_btree_block
*left
; /* left btree block */
2346 union xfs_btree_ptr rptr
; /* right sibling block ptr */
2347 struct xfs_buf
*rbp
; /* right buffer pointer */
2348 struct xfs_btree_block
*right
; /* right btree block */
2349 union xfs_btree_ptr rrptr
; /* right-right sibling ptr */
2350 struct xfs_buf
*rrbp
; /* right-right buffer pointer */
2351 struct xfs_btree_block
*rrblock
; /* right-right btree block */
2355 int error
; /* error return value */
2360 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
2361 XFS_BTREE_TRACE_ARGIPK(cur
, level
, *ptrp
, key
);
2363 XFS_BTREE_STATS_INC(cur
, split
);
2365 /* Set up left block (current one). */
2366 left
= xfs_btree_get_block(cur
, level
, &lbp
);
2369 error
= xfs_btree_check_block(cur
, left
, level
, lbp
);
2374 xfs_btree_buf_to_ptr(cur
, lbp
, &lptr
);
2376 /* Allocate the new block. If we can't do it, we're toast. Give up. */
2377 error
= cur
->bc_ops
->alloc_block(cur
, &lptr
, &rptr
, 1, stat
);
2382 XFS_BTREE_STATS_INC(cur
, alloc
);
2384 /* Set up the new block as "right". */
2385 error
= xfs_btree_get_buf_block(cur
, &rptr
, 0, &right
, &rbp
);
2389 /* Fill in the btree header for the new right block. */
2390 xfs_btree_init_block_cur(cur
, rbp
, xfs_btree_get_level(left
), 0);
2393 * Split the entries between the old and the new block evenly.
2394 * Make sure that if there's an odd number of entries now, that
2395 * each new block will have the same number of entries.
2397 lrecs
= xfs_btree_get_numrecs(left
);
2399 if ((lrecs
& 1) && cur
->bc_ptrs
[level
] <= rrecs
+ 1)
2401 src_index
= (lrecs
- rrecs
+ 1);
2403 XFS_BTREE_STATS_ADD(cur
, moves
, rrecs
);
2406 * Copy btree block entries from the left block over to the
2407 * new block, the right. Update the right block and log the
2411 /* It's a non-leaf. Move keys and pointers. */
2412 union xfs_btree_key
*lkp
; /* left btree key */
2413 union xfs_btree_ptr
*lpp
; /* left address pointer */
2414 union xfs_btree_key
*rkp
; /* right btree key */
2415 union xfs_btree_ptr
*rpp
; /* right address pointer */
2417 lkp
= xfs_btree_key_addr(cur
, src_index
, left
);
2418 lpp
= xfs_btree_ptr_addr(cur
, src_index
, left
);
2419 rkp
= xfs_btree_key_addr(cur
, 1, right
);
2420 rpp
= xfs_btree_ptr_addr(cur
, 1, right
);
2423 for (i
= src_index
; i
< rrecs
; i
++) {
2424 error
= xfs_btree_check_ptr(cur
, lpp
, i
, level
);
2430 xfs_btree_copy_keys(cur
, rkp
, lkp
, rrecs
);
2431 xfs_btree_copy_ptrs(cur
, rpp
, lpp
, rrecs
);
2433 xfs_btree_log_keys(cur
, rbp
, 1, rrecs
);
2434 xfs_btree_log_ptrs(cur
, rbp
, 1, rrecs
);
2436 /* Grab the keys to the entries moved to the right block */
2437 xfs_btree_copy_keys(cur
, key
, rkp
, 1);
2439 /* It's a leaf. Move records. */
2440 union xfs_btree_rec
*lrp
; /* left record pointer */
2441 union xfs_btree_rec
*rrp
; /* right record pointer */
2443 lrp
= xfs_btree_rec_addr(cur
, src_index
, left
);
2444 rrp
= xfs_btree_rec_addr(cur
, 1, right
);
2446 xfs_btree_copy_recs(cur
, rrp
, lrp
, rrecs
);
2447 xfs_btree_log_recs(cur
, rbp
, 1, rrecs
);
2449 cur
->bc_ops
->init_key_from_rec(key
,
2450 xfs_btree_rec_addr(cur
, 1, right
));
2455 * Find the left block number by looking in the buffer.
2456 * Adjust numrecs, sibling pointers.
2458 xfs_btree_get_sibling(cur
, left
, &rrptr
, XFS_BB_RIGHTSIB
);
2459 xfs_btree_set_sibling(cur
, right
, &rrptr
, XFS_BB_RIGHTSIB
);
2460 xfs_btree_set_sibling(cur
, right
, &lptr
, XFS_BB_LEFTSIB
);
2461 xfs_btree_set_sibling(cur
, left
, &rptr
, XFS_BB_RIGHTSIB
);
2464 xfs_btree_set_numrecs(left
, lrecs
);
2465 xfs_btree_set_numrecs(right
, xfs_btree_get_numrecs(right
) + rrecs
);
2467 xfs_btree_log_block(cur
, rbp
, XFS_BB_ALL_BITS
);
2468 xfs_btree_log_block(cur
, lbp
, XFS_BB_NUMRECS
| XFS_BB_RIGHTSIB
);
2471 * If there's a block to the new block's right, make that block
2472 * point back to right instead of to left.
2474 if (!xfs_btree_ptr_is_null(cur
, &rrptr
)) {
2475 error
= xfs_btree_read_buf_block(cur
, &rrptr
, level
,
2476 0, &rrblock
, &rrbp
);
2479 xfs_btree_set_sibling(cur
, rrblock
, &rptr
, XFS_BB_LEFTSIB
);
2480 xfs_btree_log_block(cur
, rrbp
, XFS_BB_LEFTSIB
);
2483 * If the cursor is really in the right block, move it there.
2484 * If it's just pointing past the last entry in left, then we'll
2485 * insert there, so don't change anything in that case.
2487 if (cur
->bc_ptrs
[level
] > lrecs
+ 1) {
2488 xfs_btree_setbuf(cur
, level
, rbp
);
2489 cur
->bc_ptrs
[level
] -= lrecs
;
2492 * If there are more levels, we'll need another cursor which refers
2493 * the right block, no matter where this cursor was.
2495 if (level
+ 1 < cur
->bc_nlevels
) {
2496 error
= xfs_btree_dup_cursor(cur
, curp
);
2499 (*curp
)->bc_ptrs
[level
+ 1]++;
2502 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
2506 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
2511 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
2516 * Copy the old inode root contents into a real block and make the
2517 * broot point to it.
2520 xfs_btree_new_iroot(
2521 struct xfs_btree_cur
*cur
, /* btree cursor */
2522 int *logflags
, /* logging flags for inode */
2523 int *stat
) /* return status - 0 fail */
2525 struct xfs_buf
*cbp
; /* buffer for cblock */
2526 struct xfs_btree_block
*block
; /* btree block */
2527 struct xfs_btree_block
*cblock
; /* child btree block */
2528 union xfs_btree_key
*ckp
; /* child key pointer */
2529 union xfs_btree_ptr
*cpp
; /* child ptr pointer */
2530 union xfs_btree_key
*kp
; /* pointer to btree key */
2531 union xfs_btree_ptr
*pp
; /* pointer to block addr */
2532 union xfs_btree_ptr nptr
; /* new block addr */
2533 int level
; /* btree level */
2534 int error
; /* error return code */
2536 int i
; /* loop counter */
2539 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
2540 XFS_BTREE_STATS_INC(cur
, newroot
);
2542 ASSERT(cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
);
2544 level
= cur
->bc_nlevels
- 1;
2546 block
= xfs_btree_get_iroot(cur
);
2547 pp
= xfs_btree_ptr_addr(cur
, 1, block
);
2549 /* Allocate the new block. If we can't do it, we're toast. Give up. */
2550 error
= cur
->bc_ops
->alloc_block(cur
, pp
, &nptr
, 1, stat
);
2554 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
2557 XFS_BTREE_STATS_INC(cur
, alloc
);
2559 /* Copy the root into a real block. */
2560 error
= xfs_btree_get_buf_block(cur
, &nptr
, 0, &cblock
, &cbp
);
2565 * we can't just memcpy() the root in for CRC enabled btree blocks.
2566 * In that case have to also ensure the blkno remains correct
2568 memcpy(cblock
, block
, xfs_btree_block_len(cur
));
2569 if (cur
->bc_flags
& XFS_BTREE_CRC_BLOCKS
) {
2570 if (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
)
2571 cblock
->bb_u
.l
.bb_blkno
= cpu_to_be64(cbp
->b_bn
);
2573 cblock
->bb_u
.s
.bb_blkno
= cpu_to_be64(cbp
->b_bn
);
2576 be16_add_cpu(&block
->bb_level
, 1);
2577 xfs_btree_set_numrecs(block
, 1);
2579 cur
->bc_ptrs
[level
+ 1] = 1;
2581 kp
= xfs_btree_key_addr(cur
, 1, block
);
2582 ckp
= xfs_btree_key_addr(cur
, 1, cblock
);
2583 xfs_btree_copy_keys(cur
, ckp
, kp
, xfs_btree_get_numrecs(cblock
));
2585 cpp
= xfs_btree_ptr_addr(cur
, 1, cblock
);
2587 for (i
= 0; i
< be16_to_cpu(cblock
->bb_numrecs
); i
++) {
2588 error
= xfs_btree_check_ptr(cur
, pp
, i
, level
);
2593 xfs_btree_copy_ptrs(cur
, cpp
, pp
, xfs_btree_get_numrecs(cblock
));
2596 error
= xfs_btree_check_ptr(cur
, &nptr
, 0, level
);
2600 xfs_btree_copy_ptrs(cur
, pp
, &nptr
, 1);
2602 xfs_iroot_realloc(cur
->bc_private
.b
.ip
,
2603 1 - xfs_btree_get_numrecs(cblock
),
2604 cur
->bc_private
.b
.whichfork
);
2606 xfs_btree_setbuf(cur
, level
, cbp
);
2609 * Do all this logging at the end so that
2610 * the root is at the right level.
2612 xfs_btree_log_block(cur
, cbp
, XFS_BB_ALL_BITS
);
2613 xfs_btree_log_keys(cur
, cbp
, 1, be16_to_cpu(cblock
->bb_numrecs
));
2614 xfs_btree_log_ptrs(cur
, cbp
, 1, be16_to_cpu(cblock
->bb_numrecs
));
2617 XFS_ILOG_CORE
| xfs_ilog_fbroot(cur
->bc_private
.b
.whichfork
);
2619 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
2622 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
2627 * Allocate a new root block, fill it in.
2629 STATIC
int /* error */
2631 struct xfs_btree_cur
*cur
, /* btree cursor */
2632 int *stat
) /* success/failure */
2634 struct xfs_btree_block
*block
; /* one half of the old root block */
2635 struct xfs_buf
*bp
; /* buffer containing block */
2636 int error
; /* error return value */
2637 struct xfs_buf
*lbp
; /* left buffer pointer */
2638 struct xfs_btree_block
*left
; /* left btree block */
2639 struct xfs_buf
*nbp
; /* new (root) buffer */
2640 struct xfs_btree_block
*new; /* new (root) btree block */
2641 int nptr
; /* new value for key index, 1 or 2 */
2642 struct xfs_buf
*rbp
; /* right buffer pointer */
2643 struct xfs_btree_block
*right
; /* right btree block */
2644 union xfs_btree_ptr rptr
;
2645 union xfs_btree_ptr lptr
;
2647 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
2648 XFS_BTREE_STATS_INC(cur
, newroot
);
2650 /* initialise our start point from the cursor */
2651 cur
->bc_ops
->init_ptr_from_cur(cur
, &rptr
);
2653 /* Allocate the new block. If we can't do it, we're toast. Give up. */
2654 error
= cur
->bc_ops
->alloc_block(cur
, &rptr
, &lptr
, 1, stat
);
2659 XFS_BTREE_STATS_INC(cur
, alloc
);
2661 /* Set up the new block. */
2662 error
= xfs_btree_get_buf_block(cur
, &lptr
, 0, &new, &nbp
);
2666 /* Set the root in the holding structure increasing the level by 1. */
2667 cur
->bc_ops
->set_root(cur
, &lptr
, 1);
2670 * At the previous root level there are now two blocks: the old root,
2671 * and the new block generated when it was split. We don't know which
2672 * one the cursor is pointing at, so we set up variables "left" and
2673 * "right" for each case.
2675 block
= xfs_btree_get_block(cur
, cur
->bc_nlevels
- 1, &bp
);
2678 error
= xfs_btree_check_block(cur
, block
, cur
->bc_nlevels
- 1, bp
);
2683 xfs_btree_get_sibling(cur
, block
, &rptr
, XFS_BB_RIGHTSIB
);
2684 if (!xfs_btree_ptr_is_null(cur
, &rptr
)) {
2685 /* Our block is left, pick up the right block. */
2687 xfs_btree_buf_to_ptr(cur
, lbp
, &lptr
);
2689 error
= xfs_btree_read_buf_block(cur
, &rptr
,
2690 cur
->bc_nlevels
- 1, 0, &right
, &rbp
);
2696 /* Our block is right, pick up the left block. */
2698 xfs_btree_buf_to_ptr(cur
, rbp
, &rptr
);
2700 xfs_btree_get_sibling(cur
, right
, &lptr
, XFS_BB_LEFTSIB
);
2701 error
= xfs_btree_read_buf_block(cur
, &lptr
,
2702 cur
->bc_nlevels
- 1, 0, &left
, &lbp
);
2708 /* Fill in the new block's btree header and log it. */
2709 xfs_btree_init_block_cur(cur
, nbp
, cur
->bc_nlevels
, 2);
2710 xfs_btree_log_block(cur
, nbp
, XFS_BB_ALL_BITS
);
2711 ASSERT(!xfs_btree_ptr_is_null(cur
, &lptr
) &&
2712 !xfs_btree_ptr_is_null(cur
, &rptr
));
2714 /* Fill in the key data in the new root. */
2715 if (xfs_btree_get_level(left
) > 0) {
2716 xfs_btree_copy_keys(cur
,
2717 xfs_btree_key_addr(cur
, 1, new),
2718 xfs_btree_key_addr(cur
, 1, left
), 1);
2719 xfs_btree_copy_keys(cur
,
2720 xfs_btree_key_addr(cur
, 2, new),
2721 xfs_btree_key_addr(cur
, 1, right
), 1);
2723 cur
->bc_ops
->init_key_from_rec(
2724 xfs_btree_key_addr(cur
, 1, new),
2725 xfs_btree_rec_addr(cur
, 1, left
));
2726 cur
->bc_ops
->init_key_from_rec(
2727 xfs_btree_key_addr(cur
, 2, new),
2728 xfs_btree_rec_addr(cur
, 1, right
));
2730 xfs_btree_log_keys(cur
, nbp
, 1, 2);
2732 /* Fill in the pointer data in the new root. */
2733 xfs_btree_copy_ptrs(cur
,
2734 xfs_btree_ptr_addr(cur
, 1, new), &lptr
, 1);
2735 xfs_btree_copy_ptrs(cur
,
2736 xfs_btree_ptr_addr(cur
, 2, new), &rptr
, 1);
2737 xfs_btree_log_ptrs(cur
, nbp
, 1, 2);
2739 /* Fix up the cursor. */
2740 xfs_btree_setbuf(cur
, cur
->bc_nlevels
, nbp
);
2741 cur
->bc_ptrs
[cur
->bc_nlevels
] = nptr
;
2743 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
2747 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
2750 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
2756 xfs_btree_make_block_unfull(
2757 struct xfs_btree_cur
*cur
, /* btree cursor */
2758 int level
, /* btree level */
2759 int numrecs
,/* # of recs in block */
2760 int *oindex
,/* old tree index */
2761 int *index
, /* new tree index */
2762 union xfs_btree_ptr
*nptr
, /* new btree ptr */
2763 struct xfs_btree_cur
**ncur
, /* new btree cursor */
2764 union xfs_btree_rec
*nrec
, /* new record */
2767 union xfs_btree_key key
; /* new btree key value */
2770 if ((cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
) &&
2771 level
== cur
->bc_nlevels
- 1) {
2772 struct xfs_inode
*ip
= cur
->bc_private
.b
.ip
;
2774 if (numrecs
< cur
->bc_ops
->get_dmaxrecs(cur
, level
)) {
2775 /* A root block that can be made bigger. */
2776 xfs_iroot_realloc(ip
, 1, cur
->bc_private
.b
.whichfork
);
2778 /* A root block that needs replacing */
2781 error
= xfs_btree_new_iroot(cur
, &logflags
, stat
);
2782 if (error
|| *stat
== 0)
2785 xfs_trans_log_inode(cur
->bc_tp
, ip
, logflags
);
2791 /* First, try shifting an entry to the right neighbor. */
2792 error
= xfs_btree_rshift(cur
, level
, stat
);
2796 /* Next, try shifting an entry to the left neighbor. */
2797 error
= xfs_btree_lshift(cur
, level
, stat
);
2802 *oindex
= *index
= cur
->bc_ptrs
[level
];
2807 * Next, try splitting the current block in half.
2809 * If this works we have to re-set our variables because we
2810 * could be in a different block now.
2812 error
= xfs_btree_split(cur
, level
, nptr
, &key
, ncur
, stat
);
2813 if (error
|| *stat
== 0)
2817 *index
= cur
->bc_ptrs
[level
];
2818 cur
->bc_ops
->init_rec_from_key(&key
, nrec
);
2823 * Insert one record/level. Return information to the caller
2824 * allowing the next level up to proceed if necessary.
2828 struct xfs_btree_cur
*cur
, /* btree cursor */
2829 int level
, /* level to insert record at */
2830 union xfs_btree_ptr
*ptrp
, /* i/o: block number inserted */
2831 union xfs_btree_rec
*recp
, /* i/o: record data inserted */
2832 struct xfs_btree_cur
**curp
, /* output: new cursor replacing cur */
2833 int *stat
) /* success/failure */
2835 struct xfs_btree_block
*block
; /* btree block */
2836 struct xfs_buf
*bp
; /* buffer for block */
2837 union xfs_btree_key key
; /* btree key */
2838 union xfs_btree_ptr nptr
; /* new block ptr */
2839 struct xfs_btree_cur
*ncur
; /* new btree cursor */
2840 union xfs_btree_rec nrec
; /* new record count */
2841 int optr
; /* old key/record index */
2842 int ptr
; /* key/record index */
2843 int numrecs
;/* number of records */
2844 int error
; /* error return value */
2849 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
2850 XFS_BTREE_TRACE_ARGIPR(cur
, level
, *ptrp
, recp
);
2855 * If we have an external root pointer, and we've made it to the
2856 * root level, allocate a new root block and we're done.
2858 if (!(cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
) &&
2859 (level
>= cur
->bc_nlevels
)) {
2860 error
= xfs_btree_new_root(cur
, stat
);
2861 xfs_btree_set_ptr_null(cur
, ptrp
);
2863 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
2867 /* If we're off the left edge, return failure. */
2868 ptr
= cur
->bc_ptrs
[level
];
2870 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
2875 /* Make a key out of the record data to be inserted, and save it. */
2876 cur
->bc_ops
->init_key_from_rec(&key
, recp
);
2880 XFS_BTREE_STATS_INC(cur
, insrec
);
2882 /* Get pointers to the btree buffer and block. */
2883 block
= xfs_btree_get_block(cur
, level
, &bp
);
2884 numrecs
= xfs_btree_get_numrecs(block
);
2887 error
= xfs_btree_check_block(cur
, block
, level
, bp
);
2891 /* Check that the new entry is being inserted in the right place. */
2892 if (ptr
<= numrecs
) {
2894 ASSERT(cur
->bc_ops
->recs_inorder(cur
, recp
,
2895 xfs_btree_rec_addr(cur
, ptr
, block
)));
2897 ASSERT(cur
->bc_ops
->keys_inorder(cur
, &key
,
2898 xfs_btree_key_addr(cur
, ptr
, block
)));
2904 * If the block is full, we can't insert the new entry until we
2905 * make the block un-full.
2907 xfs_btree_set_ptr_null(cur
, &nptr
);
2908 if (numrecs
== cur
->bc_ops
->get_maxrecs(cur
, level
)) {
2909 error
= xfs_btree_make_block_unfull(cur
, level
, numrecs
,
2910 &optr
, &ptr
, &nptr
, &ncur
, &nrec
, stat
);
2911 if (error
|| *stat
== 0)
2916 * The current block may have changed if the block was
2917 * previously full and we have just made space in it.
2919 block
= xfs_btree_get_block(cur
, level
, &bp
);
2920 numrecs
= xfs_btree_get_numrecs(block
);
2923 error
= xfs_btree_check_block(cur
, block
, level
, bp
);
2929 * At this point we know there's room for our new entry in the block
2930 * we're pointing at.
2932 XFS_BTREE_STATS_ADD(cur
, moves
, numrecs
- ptr
+ 1);
2935 /* It's a nonleaf. make a hole in the keys and ptrs */
2936 union xfs_btree_key
*kp
;
2937 union xfs_btree_ptr
*pp
;
2939 kp
= xfs_btree_key_addr(cur
, ptr
, block
);
2940 pp
= xfs_btree_ptr_addr(cur
, ptr
, block
);
2943 for (i
= numrecs
- ptr
; i
>= 0; i
--) {
2944 error
= xfs_btree_check_ptr(cur
, pp
, i
, level
);
2950 xfs_btree_shift_keys(cur
, kp
, 1, numrecs
- ptr
+ 1);
2951 xfs_btree_shift_ptrs(cur
, pp
, 1, numrecs
- ptr
+ 1);
2954 error
= xfs_btree_check_ptr(cur
, ptrp
, 0, level
);
2959 /* Now put the new data in, bump numrecs and log it. */
2960 xfs_btree_copy_keys(cur
, kp
, &key
, 1);
2961 xfs_btree_copy_ptrs(cur
, pp
, ptrp
, 1);
2963 xfs_btree_set_numrecs(block
, numrecs
);
2964 xfs_btree_log_ptrs(cur
, bp
, ptr
, numrecs
);
2965 xfs_btree_log_keys(cur
, bp
, ptr
, numrecs
);
2967 if (ptr
< numrecs
) {
2968 ASSERT(cur
->bc_ops
->keys_inorder(cur
, kp
,
2969 xfs_btree_key_addr(cur
, ptr
+ 1, block
)));
2973 /* It's a leaf. make a hole in the records */
2974 union xfs_btree_rec
*rp
;
2976 rp
= xfs_btree_rec_addr(cur
, ptr
, block
);
2978 xfs_btree_shift_recs(cur
, rp
, 1, numrecs
- ptr
+ 1);
2980 /* Now put the new data in, bump numrecs and log it. */
2981 xfs_btree_copy_recs(cur
, rp
, recp
, 1);
2982 xfs_btree_set_numrecs(block
, ++numrecs
);
2983 xfs_btree_log_recs(cur
, bp
, ptr
, numrecs
);
2985 if (ptr
< numrecs
) {
2986 ASSERT(cur
->bc_ops
->recs_inorder(cur
, rp
,
2987 xfs_btree_rec_addr(cur
, ptr
+ 1, block
)));
2992 /* Log the new number of records in the btree header. */
2993 xfs_btree_log_block(cur
, bp
, XFS_BB_NUMRECS
);
2995 /* If we inserted at the start of a block, update the parents' keys. */
2997 error
= xfs_btree_updkey(cur
, &key
, level
+ 1);
3003 * If we are tracking the last record in the tree and
3004 * we are at the far right edge of the tree, update it.
3006 if (xfs_btree_is_lastrec(cur
, block
, level
)) {
3007 cur
->bc_ops
->update_lastrec(cur
, block
, recp
,
3008 ptr
, LASTREC_INSREC
);
3012 * Return the new block number, if any.
3013 * If there is one, give back a record value and a cursor too.
3016 if (!xfs_btree_ptr_is_null(cur
, &nptr
)) {
3021 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
3026 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
3031 * Insert the record at the point referenced by cur.
3033 * A multi-level split of the tree on insert will invalidate the original
3034 * cursor. All callers of this function should assume that the cursor is
3035 * no longer valid and revalidate it.
3039 struct xfs_btree_cur
*cur
,
3042 int error
; /* error return value */
3043 int i
; /* result value, 0 for failure */
3044 int level
; /* current level number in btree */
3045 union xfs_btree_ptr nptr
; /* new block number (split result) */
3046 struct xfs_btree_cur
*ncur
; /* new cursor (split result) */
3047 struct xfs_btree_cur
*pcur
; /* previous level's cursor */
3048 union xfs_btree_rec rec
; /* record to insert */
3054 xfs_btree_set_ptr_null(cur
, &nptr
);
3055 cur
->bc_ops
->init_rec_from_cur(cur
, &rec
);
3058 * Loop going up the tree, starting at the leaf level.
3059 * Stop when we don't get a split block, that must mean that
3060 * the insert is finished with this level.
3064 * Insert nrec/nptr into this level of the tree.
3065 * Note if we fail, nptr will be null.
3067 error
= xfs_btree_insrec(pcur
, level
, &nptr
, &rec
, &ncur
, &i
);
3070 xfs_btree_del_cursor(pcur
, XFS_BTREE_ERROR
);
3074 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
3078 * See if the cursor we just used is trash.
3079 * Can't trash the caller's cursor, but otherwise we should
3080 * if ncur is a new cursor or we're about to be done.
3083 (ncur
|| xfs_btree_ptr_is_null(cur
, &nptr
))) {
3084 /* Save the state from the cursor before we trash it */
3085 if (cur
->bc_ops
->update_cursor
)
3086 cur
->bc_ops
->update_cursor(pcur
, cur
);
3087 cur
->bc_nlevels
= pcur
->bc_nlevels
;
3088 xfs_btree_del_cursor(pcur
, XFS_BTREE_NOERROR
);
3090 /* If we got a new cursor, switch to it. */
3095 } while (!xfs_btree_ptr_is_null(cur
, &nptr
));
3097 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
3101 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
3106 * Try to merge a non-leaf block back into the inode root.
3108 * Note: the killroot names comes from the fact that we're effectively
3109 * killing the old root block. But because we can't just delete the
3110 * inode we have to copy the single block it was pointing to into the
3114 xfs_btree_kill_iroot(
3115 struct xfs_btree_cur
*cur
)
3117 int whichfork
= cur
->bc_private
.b
.whichfork
;
3118 struct xfs_inode
*ip
= cur
->bc_private
.b
.ip
;
3119 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
3120 struct xfs_btree_block
*block
;
3121 struct xfs_btree_block
*cblock
;
3122 union xfs_btree_key
*kp
;
3123 union xfs_btree_key
*ckp
;
3124 union xfs_btree_ptr
*pp
;
3125 union xfs_btree_ptr
*cpp
;
3126 struct xfs_buf
*cbp
;
3131 union xfs_btree_ptr ptr
;
3135 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
3137 ASSERT(cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
);
3138 ASSERT(cur
->bc_nlevels
> 1);
3141 * Don't deal with the root block needs to be a leaf case.
3142 * We're just going to turn the thing back into extents anyway.
3144 level
= cur
->bc_nlevels
- 1;
3149 * Give up if the root has multiple children.
3151 block
= xfs_btree_get_iroot(cur
);
3152 if (xfs_btree_get_numrecs(block
) != 1)
3155 cblock
= xfs_btree_get_block(cur
, level
- 1, &cbp
);
3156 numrecs
= xfs_btree_get_numrecs(cblock
);
3159 * Only do this if the next level will fit.
3160 * Then the data must be copied up to the inode,
3161 * instead of freeing the root you free the next level.
3163 if (numrecs
> cur
->bc_ops
->get_dmaxrecs(cur
, level
))
3166 XFS_BTREE_STATS_INC(cur
, killroot
);
3169 xfs_btree_get_sibling(cur
, block
, &ptr
, XFS_BB_LEFTSIB
);
3170 ASSERT(xfs_btree_ptr_is_null(cur
, &ptr
));
3171 xfs_btree_get_sibling(cur
, block
, &ptr
, XFS_BB_RIGHTSIB
);
3172 ASSERT(xfs_btree_ptr_is_null(cur
, &ptr
));
3175 index
= numrecs
- cur
->bc_ops
->get_maxrecs(cur
, level
);
3177 xfs_iroot_realloc(cur
->bc_private
.b
.ip
, index
,
3178 cur
->bc_private
.b
.whichfork
);
3179 block
= ifp
->if_broot
;
3182 be16_add_cpu(&block
->bb_numrecs
, index
);
3183 ASSERT(block
->bb_numrecs
== cblock
->bb_numrecs
);
3185 kp
= xfs_btree_key_addr(cur
, 1, block
);
3186 ckp
= xfs_btree_key_addr(cur
, 1, cblock
);
3187 xfs_btree_copy_keys(cur
, kp
, ckp
, numrecs
);
3189 pp
= xfs_btree_ptr_addr(cur
, 1, block
);
3190 cpp
= xfs_btree_ptr_addr(cur
, 1, cblock
);
3192 for (i
= 0; i
< numrecs
; i
++) {
3195 error
= xfs_btree_check_ptr(cur
, cpp
, i
, level
- 1);
3197 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
3202 xfs_btree_copy_ptrs(cur
, pp
, cpp
, numrecs
);
3204 cur
->bc_ops
->free_block(cur
, cbp
);
3205 XFS_BTREE_STATS_INC(cur
, free
);
3207 cur
->bc_bufs
[level
- 1] = NULL
;
3208 be16_add_cpu(&block
->bb_level
, -1);
3209 xfs_trans_log_inode(cur
->bc_tp
, ip
,
3210 XFS_ILOG_CORE
| xfs_ilog_fbroot(cur
->bc_private
.b
.whichfork
));
3213 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
3218 * Kill the current root node, and replace it with it's only child node.
3221 xfs_btree_kill_root(
3222 struct xfs_btree_cur
*cur
,
3225 union xfs_btree_ptr
*newroot
)
3229 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
3230 XFS_BTREE_STATS_INC(cur
, killroot
);
3233 * Update the root pointer, decreasing the level by 1 and then
3234 * free the old root.
3236 cur
->bc_ops
->set_root(cur
, newroot
, -1);
3238 error
= cur
->bc_ops
->free_block(cur
, bp
);
3240 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
3244 XFS_BTREE_STATS_INC(cur
, free
);
3246 cur
->bc_bufs
[level
] = NULL
;
3247 cur
->bc_ra
[level
] = 0;
3250 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
3255 xfs_btree_dec_cursor(
3256 struct xfs_btree_cur
*cur
,
3264 error
= xfs_btree_decrement(cur
, level
, &i
);
3269 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
3275 * Single level of the btree record deletion routine.
3276 * Delete record pointed to by cur/level.
3277 * Remove the record from its block then rebalance the tree.
3278 * Return 0 for error, 1 for done, 2 to go on to the next level.
3280 STATIC
int /* error */
3282 struct xfs_btree_cur
*cur
, /* btree cursor */
3283 int level
, /* level removing record from */
3284 int *stat
) /* fail/done/go-on */
3286 struct xfs_btree_block
*block
; /* btree block */
3287 union xfs_btree_ptr cptr
; /* current block ptr */
3288 struct xfs_buf
*bp
; /* buffer for block */
3289 int error
; /* error return value */
3290 int i
; /* loop counter */
3291 union xfs_btree_key key
; /* storage for keyp */
3292 union xfs_btree_key
*keyp
= &key
; /* passed to the next level */
3293 union xfs_btree_ptr lptr
; /* left sibling block ptr */
3294 struct xfs_buf
*lbp
; /* left buffer pointer */
3295 struct xfs_btree_block
*left
; /* left btree block */
3296 int lrecs
= 0; /* left record count */
3297 int ptr
; /* key/record index */
3298 union xfs_btree_ptr rptr
; /* right sibling block ptr */
3299 struct xfs_buf
*rbp
; /* right buffer pointer */
3300 struct xfs_btree_block
*right
; /* right btree block */
3301 struct xfs_btree_block
*rrblock
; /* right-right btree block */
3302 struct xfs_buf
*rrbp
; /* right-right buffer pointer */
3303 int rrecs
= 0; /* right record count */
3304 struct xfs_btree_cur
*tcur
; /* temporary btree cursor */
3305 int numrecs
; /* temporary numrec count */
3307 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
3308 XFS_BTREE_TRACE_ARGI(cur
, level
);
3312 /* Get the index of the entry being deleted, check for nothing there. */
3313 ptr
= cur
->bc_ptrs
[level
];
3315 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
3320 /* Get the buffer & block containing the record or key/ptr. */
3321 block
= xfs_btree_get_block(cur
, level
, &bp
);
3322 numrecs
= xfs_btree_get_numrecs(block
);
3325 error
= xfs_btree_check_block(cur
, block
, level
, bp
);
3330 /* Fail if we're off the end of the block. */
3331 if (ptr
> numrecs
) {
3332 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
3337 XFS_BTREE_STATS_INC(cur
, delrec
);
3338 XFS_BTREE_STATS_ADD(cur
, moves
, numrecs
- ptr
);
3340 /* Excise the entries being deleted. */
3342 /* It's a nonleaf. operate on keys and ptrs */
3343 union xfs_btree_key
*lkp
;
3344 union xfs_btree_ptr
*lpp
;
3346 lkp
= xfs_btree_key_addr(cur
, ptr
+ 1, block
);
3347 lpp
= xfs_btree_ptr_addr(cur
, ptr
+ 1, block
);
3350 for (i
= 0; i
< numrecs
- ptr
; i
++) {
3351 error
= xfs_btree_check_ptr(cur
, lpp
, i
, level
);
3357 if (ptr
< numrecs
) {
3358 xfs_btree_shift_keys(cur
, lkp
, -1, numrecs
- ptr
);
3359 xfs_btree_shift_ptrs(cur
, lpp
, -1, numrecs
- ptr
);
3360 xfs_btree_log_keys(cur
, bp
, ptr
, numrecs
- 1);
3361 xfs_btree_log_ptrs(cur
, bp
, ptr
, numrecs
- 1);
3365 * If it's the first record in the block, we'll need to pass a
3366 * key up to the next level (updkey).
3369 keyp
= xfs_btree_key_addr(cur
, 1, block
);
3371 /* It's a leaf. operate on records */
3372 if (ptr
< numrecs
) {
3373 xfs_btree_shift_recs(cur
,
3374 xfs_btree_rec_addr(cur
, ptr
+ 1, block
),
3376 xfs_btree_log_recs(cur
, bp
, ptr
, numrecs
- 1);
3380 * If it's the first record in the block, we'll need a key
3381 * structure to pass up to the next level (updkey).
3384 cur
->bc_ops
->init_key_from_rec(&key
,
3385 xfs_btree_rec_addr(cur
, 1, block
));
3391 * Decrement and log the number of entries in the block.
3393 xfs_btree_set_numrecs(block
, --numrecs
);
3394 xfs_btree_log_block(cur
, bp
, XFS_BB_NUMRECS
);
3397 * If we are tracking the last record in the tree and
3398 * we are at the far right edge of the tree, update it.
3400 if (xfs_btree_is_lastrec(cur
, block
, level
)) {
3401 cur
->bc_ops
->update_lastrec(cur
, block
, NULL
,
3402 ptr
, LASTREC_DELREC
);
3406 * We're at the root level. First, shrink the root block in-memory.
3407 * Try to get rid of the next level down. If we can't then there's
3408 * nothing left to do.
3410 if (level
== cur
->bc_nlevels
- 1) {
3411 if (cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
) {
3412 xfs_iroot_realloc(cur
->bc_private
.b
.ip
, -1,
3413 cur
->bc_private
.b
.whichfork
);
3415 error
= xfs_btree_kill_iroot(cur
);
3419 error
= xfs_btree_dec_cursor(cur
, level
, stat
);
3427 * If this is the root level, and there's only one entry left,
3428 * and it's NOT the leaf level, then we can get rid of this
3431 if (numrecs
== 1 && level
> 0) {
3432 union xfs_btree_ptr
*pp
;
3434 * pp is still set to the first pointer in the block.
3435 * Make it the new root of the btree.
3437 pp
= xfs_btree_ptr_addr(cur
, 1, block
);
3438 error
= xfs_btree_kill_root(cur
, bp
, level
, pp
);
3441 } else if (level
> 0) {
3442 error
= xfs_btree_dec_cursor(cur
, level
, stat
);
3451 * If we deleted the leftmost entry in the block, update the
3452 * key values above us in the tree.
3455 error
= xfs_btree_updkey(cur
, keyp
, level
+ 1);
3461 * If the number of records remaining in the block is at least
3462 * the minimum, we're done.
3464 if (numrecs
>= cur
->bc_ops
->get_minrecs(cur
, level
)) {
3465 error
= xfs_btree_dec_cursor(cur
, level
, stat
);
3472 * Otherwise, we have to move some records around to keep the
3473 * tree balanced. Look at the left and right sibling blocks to
3474 * see if we can re-balance by moving only one record.
3476 xfs_btree_get_sibling(cur
, block
, &rptr
, XFS_BB_RIGHTSIB
);
3477 xfs_btree_get_sibling(cur
, block
, &lptr
, XFS_BB_LEFTSIB
);
3479 if (cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
) {
3481 * One child of root, need to get a chance to copy its contents
3482 * into the root and delete it. Can't go up to next level,
3483 * there's nothing to delete there.
3485 if (xfs_btree_ptr_is_null(cur
, &rptr
) &&
3486 xfs_btree_ptr_is_null(cur
, &lptr
) &&
3487 level
== cur
->bc_nlevels
- 2) {
3488 error
= xfs_btree_kill_iroot(cur
);
3490 error
= xfs_btree_dec_cursor(cur
, level
, stat
);
3497 ASSERT(!xfs_btree_ptr_is_null(cur
, &rptr
) ||
3498 !xfs_btree_ptr_is_null(cur
, &lptr
));
3501 * Duplicate the cursor so our btree manipulations here won't
3502 * disrupt the next level up.
3504 error
= xfs_btree_dup_cursor(cur
, &tcur
);
3509 * If there's a right sibling, see if it's ok to shift an entry
3512 if (!xfs_btree_ptr_is_null(cur
, &rptr
)) {
3514 * Move the temp cursor to the last entry in the next block.
3515 * Actually any entry but the first would suffice.
3517 i
= xfs_btree_lastrec(tcur
, level
);
3518 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
3520 error
= xfs_btree_increment(tcur
, level
, &i
);
3523 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
3525 i
= xfs_btree_lastrec(tcur
, level
);
3526 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
3528 /* Grab a pointer to the block. */
3529 right
= xfs_btree_get_block(tcur
, level
, &rbp
);
3531 error
= xfs_btree_check_block(tcur
, right
, level
, rbp
);
3535 /* Grab the current block number, for future use. */
3536 xfs_btree_get_sibling(tcur
, right
, &cptr
, XFS_BB_LEFTSIB
);
3539 * If right block is full enough so that removing one entry
3540 * won't make it too empty, and left-shifting an entry out
3541 * of right to us works, we're done.
3543 if (xfs_btree_get_numrecs(right
) - 1 >=
3544 cur
->bc_ops
->get_minrecs(tcur
, level
)) {
3545 error
= xfs_btree_lshift(tcur
, level
, &i
);
3549 ASSERT(xfs_btree_get_numrecs(block
) >=
3550 cur
->bc_ops
->get_minrecs(tcur
, level
));
3552 xfs_btree_del_cursor(tcur
, XFS_BTREE_NOERROR
);
3555 error
= xfs_btree_dec_cursor(cur
, level
, stat
);
3563 * Otherwise, grab the number of records in right for
3564 * future reference, and fix up the temp cursor to point
3565 * to our block again (last record).
3567 rrecs
= xfs_btree_get_numrecs(right
);
3568 if (!xfs_btree_ptr_is_null(cur
, &lptr
)) {
3569 i
= xfs_btree_firstrec(tcur
, level
);
3570 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
3572 error
= xfs_btree_decrement(tcur
, level
, &i
);
3575 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
3580 * If there's a left sibling, see if it's ok to shift an entry
3583 if (!xfs_btree_ptr_is_null(cur
, &lptr
)) {
3585 * Move the temp cursor to the first entry in the
3588 i
= xfs_btree_firstrec(tcur
, level
);
3589 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
3591 error
= xfs_btree_decrement(tcur
, level
, &i
);
3594 i
= xfs_btree_firstrec(tcur
, level
);
3595 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
3597 /* Grab a pointer to the block. */
3598 left
= xfs_btree_get_block(tcur
, level
, &lbp
);
3600 error
= xfs_btree_check_block(cur
, left
, level
, lbp
);
3604 /* Grab the current block number, for future use. */
3605 xfs_btree_get_sibling(tcur
, left
, &cptr
, XFS_BB_RIGHTSIB
);
3608 * If left block is full enough so that removing one entry
3609 * won't make it too empty, and right-shifting an entry out
3610 * of left to us works, we're done.
3612 if (xfs_btree_get_numrecs(left
) - 1 >=
3613 cur
->bc_ops
->get_minrecs(tcur
, level
)) {
3614 error
= xfs_btree_rshift(tcur
, level
, &i
);
3618 ASSERT(xfs_btree_get_numrecs(block
) >=
3619 cur
->bc_ops
->get_minrecs(tcur
, level
));
3620 xfs_btree_del_cursor(tcur
, XFS_BTREE_NOERROR
);
3624 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
3631 * Otherwise, grab the number of records in right for
3634 lrecs
= xfs_btree_get_numrecs(left
);
3637 /* Delete the temp cursor, we're done with it. */
3638 xfs_btree_del_cursor(tcur
, XFS_BTREE_NOERROR
);
3641 /* If here, we need to do a join to keep the tree balanced. */
3642 ASSERT(!xfs_btree_ptr_is_null(cur
, &cptr
));
3644 if (!xfs_btree_ptr_is_null(cur
, &lptr
) &&
3645 lrecs
+ xfs_btree_get_numrecs(block
) <=
3646 cur
->bc_ops
->get_maxrecs(cur
, level
)) {
3648 * Set "right" to be the starting block,
3649 * "left" to be the left neighbor.
3654 error
= xfs_btree_read_buf_block(cur
, &lptr
, level
,
3660 * If that won't work, see if we can join with the right neighbor block.
3662 } else if (!xfs_btree_ptr_is_null(cur
, &rptr
) &&
3663 rrecs
+ xfs_btree_get_numrecs(block
) <=
3664 cur
->bc_ops
->get_maxrecs(cur
, level
)) {
3666 * Set "left" to be the starting block,
3667 * "right" to be the right neighbor.
3672 error
= xfs_btree_read_buf_block(cur
, &rptr
, level
,
3678 * Otherwise, we can't fix the imbalance.
3679 * Just return. This is probably a logic error, but it's not fatal.
3682 error
= xfs_btree_dec_cursor(cur
, level
, stat
);
3688 rrecs
= xfs_btree_get_numrecs(right
);
3689 lrecs
= xfs_btree_get_numrecs(left
);
3692 * We're now going to join "left" and "right" by moving all the stuff
3693 * in "right" to "left" and deleting "right".
3695 XFS_BTREE_STATS_ADD(cur
, moves
, rrecs
);
3697 /* It's a non-leaf. Move keys and pointers. */
3698 union xfs_btree_key
*lkp
; /* left btree key */
3699 union xfs_btree_ptr
*lpp
; /* left address pointer */
3700 union xfs_btree_key
*rkp
; /* right btree key */
3701 union xfs_btree_ptr
*rpp
; /* right address pointer */
3703 lkp
= xfs_btree_key_addr(cur
, lrecs
+ 1, left
);
3704 lpp
= xfs_btree_ptr_addr(cur
, lrecs
+ 1, left
);
3705 rkp
= xfs_btree_key_addr(cur
, 1, right
);
3706 rpp
= xfs_btree_ptr_addr(cur
, 1, right
);
3708 for (i
= 1; i
< rrecs
; i
++) {
3709 error
= xfs_btree_check_ptr(cur
, rpp
, i
, level
);
3714 xfs_btree_copy_keys(cur
, lkp
, rkp
, rrecs
);
3715 xfs_btree_copy_ptrs(cur
, lpp
, rpp
, rrecs
);
3717 xfs_btree_log_keys(cur
, lbp
, lrecs
+ 1, lrecs
+ rrecs
);
3718 xfs_btree_log_ptrs(cur
, lbp
, lrecs
+ 1, lrecs
+ rrecs
);
3720 /* It's a leaf. Move records. */
3721 union xfs_btree_rec
*lrp
; /* left record pointer */
3722 union xfs_btree_rec
*rrp
; /* right record pointer */
3724 lrp
= xfs_btree_rec_addr(cur
, lrecs
+ 1, left
);
3725 rrp
= xfs_btree_rec_addr(cur
, 1, right
);
3727 xfs_btree_copy_recs(cur
, lrp
, rrp
, rrecs
);
3728 xfs_btree_log_recs(cur
, lbp
, lrecs
+ 1, lrecs
+ rrecs
);
3731 XFS_BTREE_STATS_INC(cur
, join
);
3734 * Fix up the number of records and right block pointer in the
3735 * surviving block, and log it.
3737 xfs_btree_set_numrecs(left
, lrecs
+ rrecs
);
3738 xfs_btree_get_sibling(cur
, right
, &cptr
, XFS_BB_RIGHTSIB
),
3739 xfs_btree_set_sibling(cur
, left
, &cptr
, XFS_BB_RIGHTSIB
);
3740 xfs_btree_log_block(cur
, lbp
, XFS_BB_NUMRECS
| XFS_BB_RIGHTSIB
);
3742 /* If there is a right sibling, point it to the remaining block. */
3743 xfs_btree_get_sibling(cur
, left
, &cptr
, XFS_BB_RIGHTSIB
);
3744 if (!xfs_btree_ptr_is_null(cur
, &cptr
)) {
3745 error
= xfs_btree_read_buf_block(cur
, &cptr
, level
,
3746 0, &rrblock
, &rrbp
);
3749 xfs_btree_set_sibling(cur
, rrblock
, &lptr
, XFS_BB_LEFTSIB
);
3750 xfs_btree_log_block(cur
, rrbp
, XFS_BB_LEFTSIB
);
3753 /* Free the deleted block. */
3754 error
= cur
->bc_ops
->free_block(cur
, rbp
);
3757 XFS_BTREE_STATS_INC(cur
, free
);
3760 * If we joined with the left neighbor, set the buffer in the
3761 * cursor to the left block, and fix up the index.
3764 cur
->bc_bufs
[level
] = lbp
;
3765 cur
->bc_ptrs
[level
] += lrecs
;
3766 cur
->bc_ra
[level
] = 0;
3769 * If we joined with the right neighbor and there's a level above
3770 * us, increment the cursor at that level.
3772 else if ((cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
) ||
3773 (level
+ 1 < cur
->bc_nlevels
)) {
3774 error
= xfs_btree_increment(cur
, level
+ 1, &i
);
3780 * Readjust the ptr at this level if it's not a leaf, since it's
3781 * still pointing at the deletion point, which makes the cursor
3782 * inconsistent. If this makes the ptr 0, the caller fixes it up.
3783 * We can't use decrement because it would change the next level up.
3786 cur
->bc_ptrs
[level
]--;
3788 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
3789 /* Return value means the next level up has something to do. */
3794 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
3796 xfs_btree_del_cursor(tcur
, XFS_BTREE_ERROR
);
3801 * Delete the record pointed to by cur.
3802 * The cursor refers to the place where the record was (could be inserted)
3803 * when the operation returns.
3807 struct xfs_btree_cur
*cur
,
3808 int *stat
) /* success/failure */
3810 int error
; /* error return value */
3814 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
3817 * Go up the tree, starting at leaf level.
3819 * If 2 is returned then a join was done; go to the next level.
3820 * Otherwise we are done.
3822 for (level
= 0, i
= 2; i
== 2; level
++) {
3823 error
= xfs_btree_delrec(cur
, level
, &i
);
3829 for (level
= 1; level
< cur
->bc_nlevels
; level
++) {
3830 if (cur
->bc_ptrs
[level
] == 0) {
3831 error
= xfs_btree_decrement(cur
, level
, &i
);
3839 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
3843 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
3848 * Get the data from the pointed-to record.
3852 struct xfs_btree_cur
*cur
, /* btree cursor */
3853 union xfs_btree_rec
**recp
, /* output: btree record */
3854 int *stat
) /* output: success/failure */
3856 struct xfs_btree_block
*block
; /* btree block */
3857 struct xfs_buf
*bp
; /* buffer pointer */
3858 int ptr
; /* record number */
3860 int error
; /* error return value */
3863 ptr
= cur
->bc_ptrs
[0];
3864 block
= xfs_btree_get_block(cur
, 0, &bp
);
3867 error
= xfs_btree_check_block(cur
, block
, 0, bp
);
3873 * Off the right end or left end, return failure.
3875 if (ptr
> xfs_btree_get_numrecs(block
) || ptr
<= 0) {
3881 * Point to the record and extract its data.
3883 *recp
= xfs_btree_rec_addr(cur
, ptr
, block
);
3889 * Change the owner of a btree.
3891 * The mechanism we use here is ordered buffer logging. Because we don't know
3892 * how many buffers were are going to need to modify, we don't really want to
3893 * have to make transaction reservations for the worst case of every buffer in a
3894 * full size btree as that may be more space that we can fit in the log....
3896 * We do the btree walk in the most optimal manner possible - we have sibling
3897 * pointers so we can just walk all the blocks on each level from left to right
3898 * in a single pass, and then move to the next level and do the same. We can
3899 * also do readahead on the sibling pointers to get IO moving more quickly,
3900 * though for slow disks this is unlikely to make much difference to performance
3901 * as the amount of CPU work we have to do before moving to the next block is
3904 * For each btree block that we load, modify the owner appropriately, set the
3905 * buffer as an ordered buffer and log it appropriately. We need to ensure that
3906 * we mark the region we change dirty so that if the buffer is relogged in
3907 * a subsequent transaction the changes we make here as an ordered buffer are
3908 * correctly relogged in that transaction. If we are in recovery context, then
3909 * just queue the modified buffer as delayed write buffer so the transaction
3910 * recovery completion writes the changes to disk.
3913 xfs_btree_block_change_owner(
3914 struct xfs_btree_cur
*cur
,
3916 __uint64_t new_owner
,
3917 struct list_head
*buffer_list
)
3919 struct xfs_btree_block
*block
;
3921 union xfs_btree_ptr rptr
;
3923 /* do right sibling readahead */
3924 xfs_btree_readahead(cur
, level
, XFS_BTCUR_RIGHTRA
);
3926 /* modify the owner */
3927 block
= xfs_btree_get_block(cur
, level
, &bp
);
3928 if (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
)
3929 block
->bb_u
.l
.bb_owner
= cpu_to_be64(new_owner
);
3931 block
->bb_u
.s
.bb_owner
= cpu_to_be32(new_owner
);
3934 * If the block is a root block hosted in an inode, we might not have a
3935 * buffer pointer here and we shouldn't attempt to log the change as the
3936 * information is already held in the inode and discarded when the root
3937 * block is formatted into the on-disk inode fork. We still change it,
3938 * though, so everything is consistent in memory.
3942 xfs_trans_ordered_buf(cur
->bc_tp
, bp
);
3943 xfs_btree_log_block(cur
, bp
, XFS_BB_OWNER
);
3945 xfs_buf_delwri_queue(bp
, buffer_list
);
3948 ASSERT(cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
);
3949 ASSERT(level
== cur
->bc_nlevels
- 1);
3952 /* now read rh sibling block for next iteration */
3953 xfs_btree_get_sibling(cur
, block
, &rptr
, XFS_BB_RIGHTSIB
);
3954 if (xfs_btree_ptr_is_null(cur
, &rptr
))
3957 return xfs_btree_lookup_get_block(cur
, level
, &rptr
, &block
);
3961 xfs_btree_change_owner(
3962 struct xfs_btree_cur
*cur
,
3963 __uint64_t new_owner
,
3964 struct list_head
*buffer_list
)
3966 union xfs_btree_ptr lptr
;
3968 struct xfs_btree_block
*block
= NULL
;
3971 cur
->bc_ops
->init_ptr_from_cur(cur
, &lptr
);
3973 /* for each level */
3974 for (level
= cur
->bc_nlevels
- 1; level
>= 0; level
--) {
3975 /* grab the left hand block */
3976 error
= xfs_btree_lookup_get_block(cur
, level
, &lptr
, &block
);
3980 /* readahead the left most block for the next level down */
3982 union xfs_btree_ptr
*ptr
;
3984 ptr
= xfs_btree_ptr_addr(cur
, 1, block
);
3985 xfs_btree_readahead_ptr(cur
, ptr
, 1);
3987 /* save for the next iteration of the loop */
3991 /* for each buffer in the level */
3993 error
= xfs_btree_block_change_owner(cur
, level
,
3998 if (error
!= ENOENT
)