2 * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 #include "xfs_types.h"
24 #include "xfs_trans.h"
28 #include "xfs_dmapi.h"
29 #include "xfs_mount.h"
30 #include "xfs_bmap_btree.h"
31 #include "xfs_alloc_btree.h"
32 #include "xfs_ialloc_btree.h"
33 #include "xfs_dir2_sf.h"
34 #include "xfs_attr_sf.h"
35 #include "xfs_dinode.h"
36 #include "xfs_inode.h"
37 #include "xfs_inode_item.h"
38 #include "xfs_btree.h"
39 #include "xfs_btree_trace.h"
40 #include "xfs_ialloc.h"
41 #include "xfs_error.h"
44 * Cursor allocation zone.
46 kmem_zone_t
*xfs_btree_cur_zone
;
49 * Btree magic numbers.
51 const __uint32_t xfs_magics
[XFS_BTNUM_MAX
] = {
52 XFS_ABTB_MAGIC
, XFS_ABTC_MAGIC
, XFS_BMAP_MAGIC
, XFS_IBT_MAGIC
61 * Debug routine: check that keys are in the right order.
65 xfs_btnum_t btnum
, /* btree identifier */
66 void *ak1
, /* pointer to left (lower) key */
67 void *ak2
) /* pointer to right (higher) key */
76 ASSERT(be32_to_cpu(k1
->ar_startblock
) < be32_to_cpu(k2
->ar_startblock
));
85 ASSERT(be32_to_cpu(k1
->ar_blockcount
) < be32_to_cpu(k2
->ar_blockcount
) ||
86 (k1
->ar_blockcount
== k2
->ar_blockcount
&&
87 be32_to_cpu(k1
->ar_startblock
) < be32_to_cpu(k2
->ar_startblock
)));
90 case XFS_BTNUM_BMAP
: {
96 ASSERT(be64_to_cpu(k1
->br_startoff
) < be64_to_cpu(k2
->br_startoff
));
105 ASSERT(be32_to_cpu(k1
->ir_startino
) < be32_to_cpu(k2
->ir_startino
));
114 * Debug routine: check that records are in the right order.
118 xfs_btnum_t btnum
, /* btree identifier */
119 void *ar1
, /* pointer to left (lower) record */
120 void *ar2
) /* pointer to right (higher) record */
123 case XFS_BTNUM_BNO
: {
129 ASSERT(be32_to_cpu(r1
->ar_startblock
) +
130 be32_to_cpu(r1
->ar_blockcount
) <=
131 be32_to_cpu(r2
->ar_startblock
));
134 case XFS_BTNUM_CNT
: {
140 ASSERT(be32_to_cpu(r1
->ar_blockcount
) < be32_to_cpu(r2
->ar_blockcount
) ||
141 (r1
->ar_blockcount
== r2
->ar_blockcount
&&
142 be32_to_cpu(r1
->ar_startblock
) < be32_to_cpu(r2
->ar_startblock
)));
145 case XFS_BTNUM_BMAP
: {
151 ASSERT(xfs_bmbt_disk_get_startoff(r1
) +
152 xfs_bmbt_disk_get_blockcount(r1
) <=
153 xfs_bmbt_disk_get_startoff(r2
));
156 case XFS_BTNUM_INO
: {
162 ASSERT(be32_to_cpu(r1
->ir_startino
) + XFS_INODES_PER_CHUNK
<=
163 be32_to_cpu(r2
->ir_startino
));
172 int /* error (0 or EFSCORRUPTED) */
173 xfs_btree_check_lblock(
174 struct xfs_btree_cur
*cur
, /* btree cursor */
175 struct xfs_btree_lblock
*block
, /* btree long form block pointer */
176 int level
, /* level of the btree block */
177 struct xfs_buf
*bp
) /* buffer for block, if any */
179 int lblock_ok
; /* block passes checks */
180 struct xfs_mount
*mp
; /* file system mount point */
184 be32_to_cpu(block
->bb_magic
) == xfs_magics
[cur
->bc_btnum
] &&
185 be16_to_cpu(block
->bb_level
) == level
&&
186 be16_to_cpu(block
->bb_numrecs
) <=
187 cur
->bc_ops
->get_maxrecs(cur
, level
) &&
189 (be64_to_cpu(block
->bb_leftsib
) == NULLDFSBNO
||
190 XFS_FSB_SANITY_CHECK(mp
, be64_to_cpu(block
->bb_leftsib
))) &&
191 block
->bb_rightsib
&&
192 (be64_to_cpu(block
->bb_rightsib
) == NULLDFSBNO
||
193 XFS_FSB_SANITY_CHECK(mp
, be64_to_cpu(block
->bb_rightsib
)));
194 if (unlikely(XFS_TEST_ERROR(!lblock_ok
, mp
,
195 XFS_ERRTAG_BTREE_CHECK_LBLOCK
,
196 XFS_RANDOM_BTREE_CHECK_LBLOCK
))) {
198 xfs_buftrace("LBTREE ERROR", bp
);
199 XFS_ERROR_REPORT("xfs_btree_check_lblock", XFS_ERRLEVEL_LOW
,
201 return XFS_ERROR(EFSCORRUPTED
);
206 int /* error (0 or EFSCORRUPTED) */
207 xfs_btree_check_sblock(
208 struct xfs_btree_cur
*cur
, /* btree cursor */
209 struct xfs_btree_sblock
*block
, /* btree short form block pointer */
210 int level
, /* level of the btree block */
211 struct xfs_buf
*bp
) /* buffer containing block */
213 struct xfs_buf
*agbp
; /* buffer for ag. freespace struct */
214 struct xfs_agf
*agf
; /* ag. freespace structure */
215 xfs_agblock_t agflen
; /* native ag. freespace length */
216 int sblock_ok
; /* block passes checks */
218 agbp
= cur
->bc_private
.a
.agbp
;
219 agf
= XFS_BUF_TO_AGF(agbp
);
220 agflen
= be32_to_cpu(agf
->agf_length
);
222 be32_to_cpu(block
->bb_magic
) == xfs_magics
[cur
->bc_btnum
] &&
223 be16_to_cpu(block
->bb_level
) == level
&&
224 be16_to_cpu(block
->bb_numrecs
) <=
225 cur
->bc_ops
->get_maxrecs(cur
, level
) &&
226 (be32_to_cpu(block
->bb_leftsib
) == NULLAGBLOCK
||
227 be32_to_cpu(block
->bb_leftsib
) < agflen
) &&
229 (be32_to_cpu(block
->bb_rightsib
) == NULLAGBLOCK
||
230 be32_to_cpu(block
->bb_rightsib
) < agflen
) &&
232 if (unlikely(XFS_TEST_ERROR(!sblock_ok
, cur
->bc_mp
,
233 XFS_ERRTAG_BTREE_CHECK_SBLOCK
,
234 XFS_RANDOM_BTREE_CHECK_SBLOCK
))) {
236 xfs_buftrace("SBTREE ERROR", bp
);
237 XFS_ERROR_REPORT("xfs_btree_check_sblock", XFS_ERRLEVEL_LOW
,
239 return XFS_ERROR(EFSCORRUPTED
);
245 * Debug routine: check that block header is ok.
248 xfs_btree_check_block(
249 struct xfs_btree_cur
*cur
, /* btree cursor */
250 struct xfs_btree_block
*block
, /* generic btree block pointer */
251 int level
, /* level of the btree block */
252 struct xfs_buf
*bp
) /* buffer containing block, if any */
254 if (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
) {
255 return xfs_btree_check_lblock(cur
,
256 (struct xfs_btree_lblock
*)block
, level
, bp
);
258 return xfs_btree_check_sblock(cur
,
259 (struct xfs_btree_sblock
*)block
, level
, bp
);
264 * Check that (long) pointer is ok.
266 int /* error (0 or EFSCORRUPTED) */
267 xfs_btree_check_lptr(
268 struct xfs_btree_cur
*cur
, /* btree cursor */
269 xfs_dfsbno_t bno
, /* btree block disk address */
270 int level
) /* btree block level */
272 XFS_WANT_CORRUPTED_RETURN(
275 XFS_FSB_SANITY_CHECK(cur
->bc_mp
, bno
));
280 * Check that (short) pointer is ok.
282 int /* error (0 or EFSCORRUPTED) */
283 xfs_btree_check_sptr(
284 struct xfs_btree_cur
*cur
, /* btree cursor */
285 xfs_agblock_t bno
, /* btree block disk address */
286 int level
) /* btree block level */
288 xfs_agblock_t agblocks
= cur
->bc_mp
->m_sb
.sb_agblocks
;
290 XFS_WANT_CORRUPTED_RETURN(
292 bno
!= NULLAGBLOCK
&&
299 * Check that block ptr is ok.
301 int /* error (0 or EFSCORRUPTED) */
303 struct xfs_btree_cur
*cur
, /* btree cursor */
304 union xfs_btree_ptr
*ptr
, /* btree block disk address */
305 int index
, /* offset from ptr to check */
306 int level
) /* btree block level */
308 if (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
) {
309 return xfs_btree_check_lptr(cur
,
310 be64_to_cpu((&ptr
->l
)[index
]), level
);
312 return xfs_btree_check_sptr(cur
,
313 be32_to_cpu((&ptr
->s
)[index
]), level
);
318 * Delete the btree cursor.
321 xfs_btree_del_cursor(
322 xfs_btree_cur_t
*cur
, /* btree cursor */
323 int error
) /* del because of error */
325 int i
; /* btree level */
328 * Clear the buffer pointers, and release the buffers.
329 * If we're doing this in the face of an error, we
330 * need to make sure to inspect all of the entries
331 * in the bc_bufs array for buffers to be unlocked.
332 * This is because some of the btree code works from
333 * level n down to 0, and if we get an error along
334 * the way we won't have initialized all the entries
337 for (i
= 0; i
< cur
->bc_nlevels
; i
++) {
339 xfs_btree_setbuf(cur
, i
, NULL
);
344 * Can't free a bmap cursor without having dealt with the
345 * allocated indirect blocks' accounting.
347 ASSERT(cur
->bc_btnum
!= XFS_BTNUM_BMAP
||
348 cur
->bc_private
.b
.allocated
== 0);
352 kmem_zone_free(xfs_btree_cur_zone
, cur
);
356 * Duplicate the btree cursor.
357 * Allocate a new one, copy the record, re-get the buffers.
360 xfs_btree_dup_cursor(
361 xfs_btree_cur_t
*cur
, /* input cursor */
362 xfs_btree_cur_t
**ncur
) /* output cursor */
364 xfs_buf_t
*bp
; /* btree block's buffer pointer */
365 int error
; /* error return value */
366 int i
; /* level number of btree block */
367 xfs_mount_t
*mp
; /* mount structure for filesystem */
368 xfs_btree_cur_t
*new; /* new cursor value */
369 xfs_trans_t
*tp
; /* transaction pointer, can be NULL */
375 * Allocate a new cursor like the old one.
377 new = cur
->bc_ops
->dup_cursor(cur
);
380 * Copy the record currently in the cursor.
382 new->bc_rec
= cur
->bc_rec
;
385 * For each level current, re-get the buffer and copy the ptr value.
387 for (i
= 0; i
< new->bc_nlevels
; i
++) {
388 new->bc_ptrs
[i
] = cur
->bc_ptrs
[i
];
389 new->bc_ra
[i
] = cur
->bc_ra
[i
];
390 if ((bp
= cur
->bc_bufs
[i
])) {
391 if ((error
= xfs_trans_read_buf(mp
, tp
, mp
->m_ddev_targp
,
392 XFS_BUF_ADDR(bp
), mp
->m_bsize
, 0, &bp
))) {
393 xfs_btree_del_cursor(new, error
);
397 new->bc_bufs
[i
] = bp
;
399 ASSERT(!XFS_BUF_GETERROR(bp
));
401 new->bc_bufs
[i
] = NULL
;
408 * XFS btree block layout and addressing:
410 * There are two types of blocks in the btree: leaf and non-leaf blocks.
412 * The leaf record start with a header then followed by records containing
413 * the values. A non-leaf block also starts with the same header, and
414 * then first contains lookup keys followed by an equal number of pointers
415 * to the btree blocks at the previous level.
417 * +--------+-------+-------+-------+-------+-------+-------+
418 * Leaf: | header | rec 1 | rec 2 | rec 3 | rec 4 | rec 5 | rec N |
419 * +--------+-------+-------+-------+-------+-------+-------+
421 * +--------+-------+-------+-------+-------+-------+-------+
422 * Non-Leaf: | header | key 1 | key 2 | key N | ptr 1 | ptr 2 | ptr N |
423 * +--------+-------+-------+-------+-------+-------+-------+
425 * The header is called struct xfs_btree_block for reasons better left unknown
426 * and comes in different versions for short (32bit) and long (64bit) block
427 * pointers. The record and key structures are defined by the btree instances
428 * and opaque to the btree core. The block pointers are simple disk endian
429 * integers, available in a short (32bit) and long (64bit) variant.
431 * The helpers below calculate the offset of a given record, key or pointer
432 * into a btree block (xfs_btree_*_offset) or return a pointer to the given
433 * record, key or pointer (xfs_btree_*_addr). Note that all addressing
434 * inside the btree block is done using indices starting at one, not zero!
438 * Return size of the btree block header for this btree instance.
440 static inline size_t xfs_btree_block_len(struct xfs_btree_cur
*cur
)
442 return (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
) ?
443 sizeof(struct xfs_btree_lblock
) :
444 sizeof(struct xfs_btree_sblock
);
448 * Return size of btree block pointers for this btree instance.
450 static inline size_t xfs_btree_ptr_len(struct xfs_btree_cur
*cur
)
452 return (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
) ?
453 sizeof(__be64
) : sizeof(__be32
);
457 * Calculate offset of the n-th record in a btree block.
460 xfs_btree_rec_offset(
461 struct xfs_btree_cur
*cur
,
464 return xfs_btree_block_len(cur
) +
465 (n
- 1) * cur
->bc_ops
->rec_len
;
469 * Calculate offset of the n-th key in a btree block.
472 xfs_btree_key_offset(
473 struct xfs_btree_cur
*cur
,
476 return xfs_btree_block_len(cur
) +
477 (n
- 1) * cur
->bc_ops
->key_len
;
481 * Calculate offset of the n-th block pointer in a btree block.
484 xfs_btree_ptr_offset(
485 struct xfs_btree_cur
*cur
,
489 return xfs_btree_block_len(cur
) +
490 cur
->bc_ops
->get_maxrecs(cur
, level
) * cur
->bc_ops
->key_len
+
491 (n
- 1) * xfs_btree_ptr_len(cur
);
495 * Return a pointer to the n-th record in the btree block.
497 STATIC
union xfs_btree_rec
*
499 struct xfs_btree_cur
*cur
,
501 struct xfs_btree_block
*block
)
503 return (union xfs_btree_rec
*)
504 ((char *)block
+ xfs_btree_rec_offset(cur
, n
));
508 * Return a pointer to the n-th key in the btree block.
510 STATIC
union xfs_btree_key
*
512 struct xfs_btree_cur
*cur
,
514 struct xfs_btree_block
*block
)
516 return (union xfs_btree_key
*)
517 ((char *)block
+ xfs_btree_key_offset(cur
, n
));
521 * Return a pointer to the n-th block pointer in the btree block.
523 STATIC
union xfs_btree_ptr
*
525 struct xfs_btree_cur
*cur
,
527 struct xfs_btree_block
*block
)
529 int level
= xfs_btree_get_level(block
);
531 ASSERT(block
->bb_level
!= 0);
533 return (union xfs_btree_ptr
*)
534 ((char *)block
+ xfs_btree_ptr_offset(cur
, n
, level
));
538 * Get a the root block which is stored in the inode.
540 * For now this btree implementation assumes the btree root is always
541 * stored in the if_broot field of an inode fork.
543 STATIC
struct xfs_btree_block
*
545 struct xfs_btree_cur
*cur
)
547 struct xfs_ifork
*ifp
;
549 ifp
= XFS_IFORK_PTR(cur
->bc_private
.b
.ip
, cur
->bc_private
.b
.whichfork
);
550 return (struct xfs_btree_block
*)ifp
->if_broot
;
554 * Retrieve the block pointer from the cursor at the given level.
555 * This may be an inode btree root or from a buffer.
557 STATIC
struct xfs_btree_block
* /* generic btree block pointer */
559 struct xfs_btree_cur
*cur
, /* btree cursor */
560 int level
, /* level in btree */
561 struct xfs_buf
**bpp
) /* buffer containing the block */
563 if ((cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
) &&
564 (level
== cur
->bc_nlevels
- 1)) {
566 return xfs_btree_get_iroot(cur
);
569 *bpp
= cur
->bc_bufs
[level
];
570 return XFS_BUF_TO_BLOCK(*bpp
);
574 * Get a buffer for the block, return it with no data read.
575 * Long-form addressing.
577 xfs_buf_t
* /* buffer for fsbno */
579 xfs_mount_t
*mp
, /* file system mount point */
580 xfs_trans_t
*tp
, /* transaction pointer */
581 xfs_fsblock_t fsbno
, /* file system block number */
582 uint lock
) /* lock flags for get_buf */
584 xfs_buf_t
*bp
; /* buffer pointer (return value) */
585 xfs_daddr_t d
; /* real disk block address */
587 ASSERT(fsbno
!= NULLFSBLOCK
);
588 d
= XFS_FSB_TO_DADDR(mp
, fsbno
);
589 bp
= xfs_trans_get_buf(tp
, mp
->m_ddev_targp
, d
, mp
->m_bsize
, lock
);
591 ASSERT(!XFS_BUF_GETERROR(bp
));
596 * Get a buffer for the block, return it with no data read.
597 * Short-form addressing.
599 xfs_buf_t
* /* buffer for agno/agbno */
601 xfs_mount_t
*mp
, /* file system mount point */
602 xfs_trans_t
*tp
, /* transaction pointer */
603 xfs_agnumber_t agno
, /* allocation group number */
604 xfs_agblock_t agbno
, /* allocation group block number */
605 uint lock
) /* lock flags for get_buf */
607 xfs_buf_t
*bp
; /* buffer pointer (return value) */
608 xfs_daddr_t d
; /* real disk block address */
610 ASSERT(agno
!= NULLAGNUMBER
);
611 ASSERT(agbno
!= NULLAGBLOCK
);
612 d
= XFS_AGB_TO_DADDR(mp
, agno
, agbno
);
613 bp
= xfs_trans_get_buf(tp
, mp
->m_ddev_targp
, d
, mp
->m_bsize
, lock
);
615 ASSERT(!XFS_BUF_GETERROR(bp
));
620 * Check for the cursor referring to the last block at the given level.
622 int /* 1=is last block, 0=not last block */
623 xfs_btree_islastblock(
624 xfs_btree_cur_t
*cur
, /* btree cursor */
625 int level
) /* level to check */
627 xfs_btree_block_t
*block
; /* generic btree block pointer */
628 xfs_buf_t
*bp
; /* buffer containing block */
630 block
= xfs_btree_get_block(cur
, level
, &bp
);
631 xfs_btree_check_block(cur
, block
, level
, bp
);
632 if (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
)
633 return be64_to_cpu(block
->bb_u
.l
.bb_rightsib
) == NULLDFSBNO
;
635 return be32_to_cpu(block
->bb_u
.s
.bb_rightsib
) == NULLAGBLOCK
;
639 * Change the cursor to point to the first record at the given level.
640 * Other levels are unaffected.
642 int /* success=1, failure=0 */
644 xfs_btree_cur_t
*cur
, /* btree cursor */
645 int level
) /* level to change */
647 xfs_btree_block_t
*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 1, that's the first record/key.
663 cur
->bc_ptrs
[level
] = 1;
668 * Change the cursor to point to the last record in the current block
669 * at the given level. Other levels are unaffected.
671 int /* success=1, failure=0 */
673 xfs_btree_cur_t
*cur
, /* btree cursor */
674 int level
) /* level to change */
676 xfs_btree_block_t
*block
; /* generic btree block pointer */
677 xfs_buf_t
*bp
; /* buffer containing block */
680 * Get the block pointer for this level.
682 block
= xfs_btree_get_block(cur
, level
, &bp
);
683 xfs_btree_check_block(cur
, block
, level
, bp
);
685 * It's empty, there is no such record.
687 if (!block
->bb_numrecs
)
690 * Set the ptr value to numrecs, that's the last record/key.
692 cur
->bc_ptrs
[level
] = be16_to_cpu(block
->bb_numrecs
);
697 * Compute first and last byte offsets for the fields given.
698 * Interprets the offsets table, which contains struct field offsets.
702 __int64_t fields
, /* bitmask of fields */
703 const short *offsets
, /* table of field offsets */
704 int nbits
, /* number of bits to inspect */
705 int *first
, /* output: first byte offset */
706 int *last
) /* output: last byte offset */
708 int i
; /* current bit number */
709 __int64_t imask
; /* mask for current bit number */
713 * Find the lowest bit, so the first byte offset.
715 for (i
= 0, imask
= 1LL; ; i
++, imask
<<= 1) {
716 if (imask
& fields
) {
722 * Find the highest bit, so the last byte offset.
724 for (i
= nbits
- 1, imask
= 1LL << i
; ; i
--, imask
>>= 1) {
725 if (imask
& fields
) {
726 *last
= offsets
[i
+ 1] - 1;
733 * Get a buffer for the block, return it read in.
734 * Long-form addressing.
738 xfs_mount_t
*mp
, /* file system mount point */
739 xfs_trans_t
*tp
, /* transaction pointer */
740 xfs_fsblock_t fsbno
, /* file system block number */
741 uint lock
, /* lock flags for read_buf */
742 xfs_buf_t
**bpp
, /* buffer for fsbno */
743 int refval
) /* ref count value for buffer */
745 xfs_buf_t
*bp
; /* return value */
746 xfs_daddr_t d
; /* real disk block address */
749 ASSERT(fsbno
!= NULLFSBLOCK
);
750 d
= XFS_FSB_TO_DADDR(mp
, fsbno
);
751 if ((error
= xfs_trans_read_buf(mp
, tp
, mp
->m_ddev_targp
, d
,
752 mp
->m_bsize
, lock
, &bp
))) {
755 ASSERT(!bp
|| !XFS_BUF_GETERROR(bp
));
757 XFS_BUF_SET_VTYPE_REF(bp
, B_FS_MAP
, refval
);
764 * Get a buffer for the block, return it read in.
765 * Short-form addressing.
769 xfs_mount_t
*mp
, /* file system mount point */
770 xfs_trans_t
*tp
, /* transaction pointer */
771 xfs_agnumber_t agno
, /* allocation group number */
772 xfs_agblock_t agbno
, /* allocation group block number */
773 uint lock
, /* lock flags for read_buf */
774 xfs_buf_t
**bpp
, /* buffer for agno/agbno */
775 int refval
) /* ref count value for buffer */
777 xfs_buf_t
*bp
; /* return value */
778 xfs_daddr_t d
; /* real disk block address */
781 ASSERT(agno
!= NULLAGNUMBER
);
782 ASSERT(agbno
!= NULLAGBLOCK
);
783 d
= XFS_AGB_TO_DADDR(mp
, agno
, agbno
);
784 if ((error
= xfs_trans_read_buf(mp
, tp
, mp
->m_ddev_targp
, d
,
785 mp
->m_bsize
, lock
, &bp
))) {
788 ASSERT(!bp
|| !XFS_BUF_GETERROR(bp
));
791 case XFS_ALLOC_BTREE_REF
:
792 XFS_BUF_SET_VTYPE_REF(bp
, B_FS_MAP
, refval
);
794 case XFS_INO_BTREE_REF
:
795 XFS_BUF_SET_VTYPE_REF(bp
, B_FS_INOMAP
, refval
);
804 * Read-ahead the block, don't wait for it, don't return a buffer.
805 * Long-form addressing.
809 xfs_btree_reada_bufl(
810 xfs_mount_t
*mp
, /* file system mount point */
811 xfs_fsblock_t fsbno
, /* file system block number */
812 xfs_extlen_t count
) /* count of filesystem blocks */
816 ASSERT(fsbno
!= NULLFSBLOCK
);
817 d
= XFS_FSB_TO_DADDR(mp
, fsbno
);
818 xfs_baread(mp
->m_ddev_targp
, d
, mp
->m_bsize
* count
);
822 * Read-ahead the block, don't wait for it, don't return a buffer.
823 * Short-form addressing.
827 xfs_btree_reada_bufs(
828 xfs_mount_t
*mp
, /* file system mount point */
829 xfs_agnumber_t agno
, /* allocation group number */
830 xfs_agblock_t agbno
, /* allocation group block number */
831 xfs_extlen_t count
) /* count of filesystem blocks */
835 ASSERT(agno
!= NULLAGNUMBER
);
836 ASSERT(agbno
!= NULLAGBLOCK
);
837 d
= XFS_AGB_TO_DADDR(mp
, agno
, agbno
);
838 xfs_baread(mp
->m_ddev_targp
, d
, mp
->m_bsize
* count
);
842 xfs_btree_readahead_lblock(
843 struct xfs_btree_cur
*cur
,
845 struct xfs_btree_block
*block
)
848 xfs_fsblock_t left
= be64_to_cpu(block
->bb_u
.l
.bb_leftsib
);
849 xfs_fsblock_t right
= be64_to_cpu(block
->bb_u
.l
.bb_rightsib
);
851 if ((lr
& XFS_BTCUR_LEFTRA
) && left
!= NULLDFSBNO
) {
852 xfs_btree_reada_bufl(cur
->bc_mp
, left
, 1);
856 if ((lr
& XFS_BTCUR_RIGHTRA
) && right
!= NULLDFSBNO
) {
857 xfs_btree_reada_bufl(cur
->bc_mp
, right
, 1);
865 xfs_btree_readahead_sblock(
866 struct xfs_btree_cur
*cur
,
868 struct xfs_btree_block
*block
)
871 xfs_agblock_t left
= be32_to_cpu(block
->bb_u
.s
.bb_leftsib
);
872 xfs_agblock_t right
= be32_to_cpu(block
->bb_u
.s
.bb_rightsib
);
875 if ((lr
& XFS_BTCUR_LEFTRA
) && left
!= NULLAGBLOCK
) {
876 xfs_btree_reada_bufs(cur
->bc_mp
, cur
->bc_private
.a
.agno
,
881 if ((lr
& XFS_BTCUR_RIGHTRA
) && right
!= NULLAGBLOCK
) {
882 xfs_btree_reada_bufs(cur
->bc_mp
, cur
->bc_private
.a
.agno
,
891 * Read-ahead btree blocks, at the given level.
892 * Bits in lr are set from XFS_BTCUR_{LEFT,RIGHT}RA.
896 struct xfs_btree_cur
*cur
, /* btree cursor */
897 int lev
, /* level in btree */
898 int lr
) /* left/right bits */
900 struct xfs_btree_block
*block
;
903 * No readahead needed if we are at the root level and the
904 * btree root is stored in the inode.
906 if ((cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
) &&
907 (lev
== cur
->bc_nlevels
- 1))
910 if ((cur
->bc_ra
[lev
] | lr
) == cur
->bc_ra
[lev
])
913 cur
->bc_ra
[lev
] |= lr
;
914 block
= XFS_BUF_TO_BLOCK(cur
->bc_bufs
[lev
]);
916 if (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
)
917 return xfs_btree_readahead_lblock(cur
, lr
, block
);
918 return xfs_btree_readahead_sblock(cur
, lr
, block
);
922 * Set the buffer for level "lev" in the cursor to bp, releasing
923 * any previous buffer.
927 xfs_btree_cur_t
*cur
, /* btree cursor */
928 int lev
, /* level in btree */
929 xfs_buf_t
*bp
) /* new buffer to set */
931 xfs_btree_block_t
*b
; /* btree block */
932 xfs_buf_t
*obp
; /* old buffer pointer */
934 obp
= cur
->bc_bufs
[lev
];
936 xfs_trans_brelse(cur
->bc_tp
, obp
);
937 cur
->bc_bufs
[lev
] = bp
;
941 b
= XFS_BUF_TO_BLOCK(bp
);
942 if (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
) {
943 if (be64_to_cpu(b
->bb_u
.l
.bb_leftsib
) == NULLDFSBNO
)
944 cur
->bc_ra
[lev
] |= XFS_BTCUR_LEFTRA
;
945 if (be64_to_cpu(b
->bb_u
.l
.bb_rightsib
) == NULLDFSBNO
)
946 cur
->bc_ra
[lev
] |= XFS_BTCUR_RIGHTRA
;
948 if (be32_to_cpu(b
->bb_u
.s
.bb_leftsib
) == NULLAGBLOCK
)
949 cur
->bc_ra
[lev
] |= XFS_BTCUR_LEFTRA
;
950 if (be32_to_cpu(b
->bb_u
.s
.bb_rightsib
) == NULLAGBLOCK
)
951 cur
->bc_ra
[lev
] |= XFS_BTCUR_RIGHTRA
;
956 xfs_btree_ptr_is_null(
957 struct xfs_btree_cur
*cur
,
958 union xfs_btree_ptr
*ptr
)
960 if (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
)
961 return be64_to_cpu(ptr
->l
) == NULLFSBLOCK
;
963 return be32_to_cpu(ptr
->s
) == NULLAGBLOCK
;
967 xfs_btree_set_ptr_null(
968 struct xfs_btree_cur
*cur
,
969 union xfs_btree_ptr
*ptr
)
971 if (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
)
972 ptr
->l
= cpu_to_be64(NULLFSBLOCK
);
974 ptr
->s
= cpu_to_be32(NULLAGBLOCK
);
978 * Get/set/init sibling pointers
981 xfs_btree_get_sibling(
982 struct xfs_btree_cur
*cur
,
983 struct xfs_btree_block
*block
,
984 union xfs_btree_ptr
*ptr
,
987 ASSERT(lr
== XFS_BB_LEFTSIB
|| lr
== XFS_BB_RIGHTSIB
);
989 if (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
) {
990 if (lr
== XFS_BB_RIGHTSIB
)
991 ptr
->l
= block
->bb_u
.l
.bb_rightsib
;
993 ptr
->l
= block
->bb_u
.l
.bb_leftsib
;
995 if (lr
== XFS_BB_RIGHTSIB
)
996 ptr
->s
= block
->bb_u
.s
.bb_rightsib
;
998 ptr
->s
= block
->bb_u
.s
.bb_leftsib
;
1003 xfs_btree_set_sibling(
1004 struct xfs_btree_cur
*cur
,
1005 struct xfs_btree_block
*block
,
1006 union xfs_btree_ptr
*ptr
,
1009 ASSERT(lr
== XFS_BB_LEFTSIB
|| lr
== XFS_BB_RIGHTSIB
);
1011 if (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
) {
1012 if (lr
== XFS_BB_RIGHTSIB
)
1013 block
->bb_u
.l
.bb_rightsib
= ptr
->l
;
1015 block
->bb_u
.l
.bb_leftsib
= ptr
->l
;
1017 if (lr
== XFS_BB_RIGHTSIB
)
1018 block
->bb_u
.s
.bb_rightsib
= ptr
->s
;
1020 block
->bb_u
.s
.bb_leftsib
= ptr
->s
;
1025 xfs_btree_init_block(
1026 struct xfs_btree_cur
*cur
,
1029 struct xfs_btree_block
*new) /* new block */
1031 new->bb_magic
= cpu_to_be32(xfs_magics
[cur
->bc_btnum
]);
1032 new->bb_level
= cpu_to_be16(level
);
1033 new->bb_numrecs
= cpu_to_be16(numrecs
);
1035 if (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
) {
1036 new->bb_u
.l
.bb_leftsib
= cpu_to_be64(NULLFSBLOCK
);
1037 new->bb_u
.l
.bb_rightsib
= cpu_to_be64(NULLFSBLOCK
);
1039 new->bb_u
.s
.bb_leftsib
= cpu_to_be32(NULLAGBLOCK
);
1040 new->bb_u
.s
.bb_rightsib
= cpu_to_be32(NULLAGBLOCK
);
1045 * Return true if ptr is the last record in the btree and
1046 * we need to track updateѕ to this record. The decision
1047 * will be further refined in the update_lastrec method.
1050 xfs_btree_is_lastrec(
1051 struct xfs_btree_cur
*cur
,
1052 struct xfs_btree_block
*block
,
1055 union xfs_btree_ptr ptr
;
1059 if (!(cur
->bc_flags
& XFS_BTREE_LASTREC_UPDATE
))
1062 xfs_btree_get_sibling(cur
, block
, &ptr
, XFS_BB_RIGHTSIB
);
1063 if (!xfs_btree_ptr_is_null(cur
, &ptr
))
1069 xfs_btree_buf_to_ptr(
1070 struct xfs_btree_cur
*cur
,
1072 union xfs_btree_ptr
*ptr
)
1074 if (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
)
1075 ptr
->l
= cpu_to_be64(XFS_DADDR_TO_FSB(cur
->bc_mp
,
1078 ptr
->s
= cpu_to_be32(XFS_DADDR_TO_AGBNO(cur
->bc_mp
,
1084 xfs_btree_ptr_to_daddr(
1085 struct xfs_btree_cur
*cur
,
1086 union xfs_btree_ptr
*ptr
)
1088 if (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
) {
1089 ASSERT(be64_to_cpu(ptr
->l
) != NULLFSBLOCK
);
1091 return XFS_FSB_TO_DADDR(cur
->bc_mp
, be64_to_cpu(ptr
->l
));
1093 ASSERT(cur
->bc_private
.a
.agno
!= NULLAGNUMBER
);
1094 ASSERT(be32_to_cpu(ptr
->s
) != NULLAGBLOCK
);
1096 return XFS_AGB_TO_DADDR(cur
->bc_mp
, cur
->bc_private
.a
.agno
,
1097 be32_to_cpu(ptr
->s
));
1103 struct xfs_btree_cur
*cur
,
1106 switch (cur
->bc_btnum
) {
1109 XFS_BUF_SET_VTYPE_REF(*bpp
, B_FS_MAP
, XFS_ALLOC_BTREE_REF
);
1112 XFS_BUF_SET_VTYPE_REF(*bpp
, B_FS_INOMAP
, XFS_INO_BTREE_REF
);
1114 case XFS_BTNUM_BMAP
:
1115 XFS_BUF_SET_VTYPE_REF(*bpp
, B_FS_MAP
, XFS_BMAP_BTREE_REF
);
1123 xfs_btree_get_buf_block(
1124 struct xfs_btree_cur
*cur
,
1125 union xfs_btree_ptr
*ptr
,
1127 struct xfs_btree_block
**block
,
1128 struct xfs_buf
**bpp
)
1130 struct xfs_mount
*mp
= cur
->bc_mp
;
1133 /* need to sort out how callers deal with failures first */
1134 ASSERT(!(flags
& XFS_BUF_TRYLOCK
));
1136 d
= xfs_btree_ptr_to_daddr(cur
, ptr
);
1137 *bpp
= xfs_trans_get_buf(cur
->bc_tp
, mp
->m_ddev_targp
, d
,
1138 mp
->m_bsize
, flags
);
1141 ASSERT(!XFS_BUF_GETERROR(*bpp
));
1143 *block
= XFS_BUF_TO_BLOCK(*bpp
);
1148 * Read in the buffer at the given ptr and return the buffer and
1149 * the block pointer within the buffer.
1152 xfs_btree_read_buf_block(
1153 struct xfs_btree_cur
*cur
,
1154 union xfs_btree_ptr
*ptr
,
1157 struct xfs_btree_block
**block
,
1158 struct xfs_buf
**bpp
)
1160 struct xfs_mount
*mp
= cur
->bc_mp
;
1164 /* need to sort out how callers deal with failures first */
1165 ASSERT(!(flags
& XFS_BUF_TRYLOCK
));
1167 d
= xfs_btree_ptr_to_daddr(cur
, ptr
);
1168 error
= xfs_trans_read_buf(mp
, cur
->bc_tp
, mp
->m_ddev_targp
, d
,
1169 mp
->m_bsize
, flags
, bpp
);
1173 ASSERT(*bpp
!= NULL
);
1174 ASSERT(!XFS_BUF_GETERROR(*bpp
));
1176 xfs_btree_set_refs(cur
, *bpp
);
1177 *block
= XFS_BUF_TO_BLOCK(*bpp
);
1179 error
= xfs_btree_check_block(cur
, *block
, level
, *bpp
);
1181 xfs_trans_brelse(cur
->bc_tp
, *bpp
);
1186 * Copy keys from one btree block to another.
1189 xfs_btree_copy_keys(
1190 struct xfs_btree_cur
*cur
,
1191 union xfs_btree_key
*dst_key
,
1192 union xfs_btree_key
*src_key
,
1195 ASSERT(numkeys
>= 0);
1196 memcpy(dst_key
, src_key
, numkeys
* cur
->bc_ops
->key_len
);
1200 * Copy records from one btree block to another.
1203 xfs_btree_copy_recs(
1204 struct xfs_btree_cur
*cur
,
1205 union xfs_btree_rec
*dst_rec
,
1206 union xfs_btree_rec
*src_rec
,
1209 ASSERT(numrecs
>= 0);
1210 memcpy(dst_rec
, src_rec
, numrecs
* cur
->bc_ops
->rec_len
);
1214 * Copy block pointers from one btree block to another.
1217 xfs_btree_copy_ptrs(
1218 struct xfs_btree_cur
*cur
,
1219 union xfs_btree_ptr
*dst_ptr
,
1220 union xfs_btree_ptr
*src_ptr
,
1223 ASSERT(numptrs
>= 0);
1224 memcpy(dst_ptr
, src_ptr
, numptrs
* xfs_btree_ptr_len(cur
));
1228 * Shift keys one index left/right inside a single btree block.
1231 xfs_btree_shift_keys(
1232 struct xfs_btree_cur
*cur
,
1233 union xfs_btree_key
*key
,
1239 ASSERT(numkeys
>= 0);
1240 ASSERT(dir
== 1 || dir
== -1);
1242 dst_key
= (char *)key
+ (dir
* cur
->bc_ops
->key_len
);
1243 memmove(dst_key
, key
, numkeys
* cur
->bc_ops
->key_len
);
1247 * Shift records one index left/right inside a single btree block.
1250 xfs_btree_shift_recs(
1251 struct xfs_btree_cur
*cur
,
1252 union xfs_btree_rec
*rec
,
1258 ASSERT(numrecs
>= 0);
1259 ASSERT(dir
== 1 || dir
== -1);
1261 dst_rec
= (char *)rec
+ (dir
* cur
->bc_ops
->rec_len
);
1262 memmove(dst_rec
, rec
, numrecs
* cur
->bc_ops
->rec_len
);
1266 * Shift block pointers one index left/right inside a single btree block.
1269 xfs_btree_shift_ptrs(
1270 struct xfs_btree_cur
*cur
,
1271 union xfs_btree_ptr
*ptr
,
1277 ASSERT(numptrs
>= 0);
1278 ASSERT(dir
== 1 || dir
== -1);
1280 dst_ptr
= (char *)ptr
+ (dir
* xfs_btree_ptr_len(cur
));
1281 memmove(dst_ptr
, ptr
, numptrs
* xfs_btree_ptr_len(cur
));
1285 * Log key values from the btree block.
1289 struct xfs_btree_cur
*cur
,
1294 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
1295 XFS_BTREE_TRACE_ARGBII(cur
, bp
, first
, last
);
1298 xfs_trans_log_buf(cur
->bc_tp
, bp
,
1299 xfs_btree_key_offset(cur
, first
),
1300 xfs_btree_key_offset(cur
, last
+ 1) - 1);
1302 xfs_trans_log_inode(cur
->bc_tp
, cur
->bc_private
.b
.ip
,
1303 xfs_ilog_fbroot(cur
->bc_private
.b
.whichfork
));
1306 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
1310 * Log record values from the btree block.
1314 struct xfs_btree_cur
*cur
,
1319 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
1320 XFS_BTREE_TRACE_ARGBII(cur
, bp
, first
, last
);
1322 xfs_trans_log_buf(cur
->bc_tp
, bp
,
1323 xfs_btree_rec_offset(cur
, first
),
1324 xfs_btree_rec_offset(cur
, last
+ 1) - 1);
1326 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
1330 * Log block pointer fields from a btree block (nonleaf).
1334 struct xfs_btree_cur
*cur
, /* btree cursor */
1335 struct xfs_buf
*bp
, /* buffer containing btree block */
1336 int first
, /* index of first pointer to log */
1337 int last
) /* index of last pointer to log */
1339 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
1340 XFS_BTREE_TRACE_ARGBII(cur
, bp
, first
, last
);
1343 struct xfs_btree_block
*block
= XFS_BUF_TO_BLOCK(bp
);
1344 int level
= xfs_btree_get_level(block
);
1346 xfs_trans_log_buf(cur
->bc_tp
, bp
,
1347 xfs_btree_ptr_offset(cur
, first
, level
),
1348 xfs_btree_ptr_offset(cur
, last
+ 1, level
) - 1);
1350 xfs_trans_log_inode(cur
->bc_tp
, cur
->bc_private
.b
.ip
,
1351 xfs_ilog_fbroot(cur
->bc_private
.b
.whichfork
));
1354 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
1358 * Log fields from a btree block header.
1361 xfs_btree_log_block(
1362 struct xfs_btree_cur
*cur
, /* btree cursor */
1363 struct xfs_buf
*bp
, /* buffer containing btree block */
1364 int fields
) /* mask of fields: XFS_BB_... */
1366 int first
; /* first byte offset logged */
1367 int last
; /* last byte offset logged */
1368 static const short soffsets
[] = { /* table of offsets (short) */
1369 offsetof(struct xfs_btree_sblock
, bb_magic
),
1370 offsetof(struct xfs_btree_sblock
, bb_level
),
1371 offsetof(struct xfs_btree_sblock
, bb_numrecs
),
1372 offsetof(struct xfs_btree_sblock
, bb_leftsib
),
1373 offsetof(struct xfs_btree_sblock
, bb_rightsib
),
1374 sizeof(struct xfs_btree_sblock
)
1376 static const short loffsets
[] = { /* table of offsets (long) */
1377 offsetof(struct xfs_btree_lblock
, bb_magic
),
1378 offsetof(struct xfs_btree_lblock
, bb_level
),
1379 offsetof(struct xfs_btree_lblock
, bb_numrecs
),
1380 offsetof(struct xfs_btree_lblock
, bb_leftsib
),
1381 offsetof(struct xfs_btree_lblock
, bb_rightsib
),
1382 sizeof(struct xfs_btree_lblock
)
1385 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
1386 XFS_BTREE_TRACE_ARGBI(cur
, bp
, fields
);
1389 xfs_btree_offsets(fields
,
1390 (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
) ?
1391 loffsets
: soffsets
,
1392 XFS_BB_NUM_BITS
, &first
, &last
);
1393 xfs_trans_log_buf(cur
->bc_tp
, bp
, first
, last
);
1395 xfs_trans_log_inode(cur
->bc_tp
, cur
->bc_private
.b
.ip
,
1396 xfs_ilog_fbroot(cur
->bc_private
.b
.whichfork
));
1399 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
1403 * Increment cursor by one record at the level.
1404 * For nonzero levels the leaf-ward information is untouched.
1407 xfs_btree_increment(
1408 struct xfs_btree_cur
*cur
,
1410 int *stat
) /* success/failure */
1412 struct xfs_btree_block
*block
;
1413 union xfs_btree_ptr ptr
;
1415 int error
; /* error return value */
1418 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
1419 XFS_BTREE_TRACE_ARGI(cur
, level
);
1421 ASSERT(level
< cur
->bc_nlevels
);
1423 /* Read-ahead to the right at this level. */
1424 xfs_btree_readahead(cur
, level
, XFS_BTCUR_RIGHTRA
);
1426 /* Get a pointer to the btree block. */
1427 block
= xfs_btree_get_block(cur
, level
, &bp
);
1430 error
= xfs_btree_check_block(cur
, block
, level
, bp
);
1435 /* We're done if we remain in the block after the increment. */
1436 if (++cur
->bc_ptrs
[level
] <= xfs_btree_get_numrecs(block
))
1439 /* Fail if we just went off the right edge of the tree. */
1440 xfs_btree_get_sibling(cur
, block
, &ptr
, XFS_BB_RIGHTSIB
);
1441 if (xfs_btree_ptr_is_null(cur
, &ptr
))
1444 XFS_BTREE_STATS_INC(cur
, increment
);
1447 * March up the tree incrementing pointers.
1448 * Stop when we don't go off the right edge of a block.
1450 for (lev
= level
+ 1; lev
< cur
->bc_nlevels
; lev
++) {
1451 block
= xfs_btree_get_block(cur
, lev
, &bp
);
1454 error
= xfs_btree_check_block(cur
, block
, lev
, bp
);
1459 if (++cur
->bc_ptrs
[lev
] <= xfs_btree_get_numrecs(block
))
1462 /* Read-ahead the right block for the next loop. */
1463 xfs_btree_readahead(cur
, lev
, XFS_BTCUR_RIGHTRA
);
1467 * If we went off the root then we are either seriously
1468 * confused or have the tree root in an inode.
1470 if (lev
== cur
->bc_nlevels
) {
1471 if (cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
)
1474 error
= EFSCORRUPTED
;
1477 ASSERT(lev
< cur
->bc_nlevels
);
1480 * Now walk back down the tree, fixing up the cursor's buffer
1481 * pointers and key numbers.
1483 for (block
= xfs_btree_get_block(cur
, lev
, &bp
); lev
> level
; ) {
1484 union xfs_btree_ptr
*ptrp
;
1486 ptrp
= xfs_btree_ptr_addr(cur
, cur
->bc_ptrs
[lev
], block
);
1487 error
= xfs_btree_read_buf_block(cur
, ptrp
, --lev
,
1492 xfs_btree_setbuf(cur
, lev
, bp
);
1493 cur
->bc_ptrs
[lev
] = 1;
1496 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
1501 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
1506 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
1511 * Decrement cursor by one record at the level.
1512 * For nonzero levels the leaf-ward information is untouched.
1515 xfs_btree_decrement(
1516 struct xfs_btree_cur
*cur
,
1518 int *stat
) /* success/failure */
1520 struct xfs_btree_block
*block
;
1522 int error
; /* error return value */
1524 union xfs_btree_ptr ptr
;
1526 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
1527 XFS_BTREE_TRACE_ARGI(cur
, level
);
1529 ASSERT(level
< cur
->bc_nlevels
);
1531 /* Read-ahead to the left at this level. */
1532 xfs_btree_readahead(cur
, level
, XFS_BTCUR_LEFTRA
);
1534 /* We're done if we remain in the block after the decrement. */
1535 if (--cur
->bc_ptrs
[level
] > 0)
1538 /* Get a pointer to the btree block. */
1539 block
= xfs_btree_get_block(cur
, level
, &bp
);
1542 error
= xfs_btree_check_block(cur
, block
, level
, bp
);
1547 /* Fail if we just went off the left edge of the tree. */
1548 xfs_btree_get_sibling(cur
, block
, &ptr
, XFS_BB_LEFTSIB
);
1549 if (xfs_btree_ptr_is_null(cur
, &ptr
))
1552 XFS_BTREE_STATS_INC(cur
, decrement
);
1555 * March up the tree decrementing pointers.
1556 * Stop when we don't go off the left edge of a block.
1558 for (lev
= level
+ 1; lev
< cur
->bc_nlevels
; lev
++) {
1559 if (--cur
->bc_ptrs
[lev
] > 0)
1561 /* Read-ahead the left block for the next loop. */
1562 xfs_btree_readahead(cur
, lev
, XFS_BTCUR_LEFTRA
);
1566 * If we went off the root then we are seriously confused.
1567 * or the root of the tree is in an inode.
1569 if (lev
== cur
->bc_nlevels
) {
1570 if (cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
)
1573 error
= EFSCORRUPTED
;
1576 ASSERT(lev
< cur
->bc_nlevels
);
1579 * Now walk back down the tree, fixing up the cursor's buffer
1580 * pointers and key numbers.
1582 for (block
= xfs_btree_get_block(cur
, lev
, &bp
); lev
> level
; ) {
1583 union xfs_btree_ptr
*ptrp
;
1585 ptrp
= xfs_btree_ptr_addr(cur
, cur
->bc_ptrs
[lev
], block
);
1586 error
= xfs_btree_read_buf_block(cur
, ptrp
, --lev
,
1590 xfs_btree_setbuf(cur
, lev
, bp
);
1591 cur
->bc_ptrs
[lev
] = xfs_btree_get_numrecs(block
);
1594 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
1599 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
1604 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
1609 xfs_btree_lookup_get_block(
1610 struct xfs_btree_cur
*cur
, /* btree cursor */
1611 int level
, /* level in the btree */
1612 union xfs_btree_ptr
*pp
, /* ptr to btree block */
1613 struct xfs_btree_block
**blkp
) /* return btree block */
1615 struct xfs_buf
*bp
; /* buffer pointer for btree block */
1618 /* special case the root block if in an inode */
1619 if ((cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
) &&
1620 (level
== cur
->bc_nlevels
- 1)) {
1621 *blkp
= xfs_btree_get_iroot(cur
);
1626 * If the old buffer at this level for the disk address we are
1627 * looking for re-use it.
1629 * Otherwise throw it away and get a new one.
1631 bp
= cur
->bc_bufs
[level
];
1632 if (bp
&& XFS_BUF_ADDR(bp
) == xfs_btree_ptr_to_daddr(cur
, pp
)) {
1633 *blkp
= XFS_BUF_TO_BLOCK(bp
);
1637 error
= xfs_btree_read_buf_block(cur
, pp
, level
, 0, blkp
, &bp
);
1641 xfs_btree_setbuf(cur
, level
, bp
);
1646 * Get current search key. For level 0 we don't actually have a key
1647 * structure so we make one up from the record. For all other levels
1648 * we just return the right key.
1650 STATIC
union xfs_btree_key
*
1651 xfs_lookup_get_search_key(
1652 struct xfs_btree_cur
*cur
,
1655 struct xfs_btree_block
*block
,
1656 union xfs_btree_key
*kp
)
1659 cur
->bc_ops
->init_key_from_rec(kp
,
1660 xfs_btree_rec_addr(cur
, keyno
, block
));
1664 return xfs_btree_key_addr(cur
, keyno
, block
);
1668 * Lookup the record. The cursor is made to point to it, based on dir.
1669 * Return 0 if can't find any such record, 1 for success.
1673 struct xfs_btree_cur
*cur
, /* btree cursor */
1674 xfs_lookup_t dir
, /* <=, ==, or >= */
1675 int *stat
) /* success/failure */
1677 struct xfs_btree_block
*block
; /* current btree block */
1678 __int64_t diff
; /* difference for the current key */
1679 int error
; /* error return value */
1680 int keyno
; /* current key number */
1681 int level
; /* level in the btree */
1682 union xfs_btree_ptr
*pp
; /* ptr to btree block */
1683 union xfs_btree_ptr ptr
; /* ptr to btree block */
1685 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
1686 XFS_BTREE_TRACE_ARGI(cur
, dir
);
1688 XFS_BTREE_STATS_INC(cur
, lookup
);
1693 /* initialise start pointer from cursor */
1694 cur
->bc_ops
->init_ptr_from_cur(cur
, &ptr
);
1698 * Iterate over each level in the btree, starting at the root.
1699 * For each level above the leaves, find the key we need, based
1700 * on the lookup record, then follow the corresponding block
1701 * pointer down to the next level.
1703 for (level
= cur
->bc_nlevels
- 1, diff
= 1; level
>= 0; level
--) {
1704 /* Get the block we need to do the lookup on. */
1705 error
= xfs_btree_lookup_get_block(cur
, level
, pp
, &block
);
1711 * If we already had a key match at a higher level, we
1712 * know we need to use the first entry in this block.
1716 /* Otherwise search this block. Do a binary search. */
1718 int high
; /* high entry number */
1719 int low
; /* low entry number */
1721 /* Set low and high entry numbers, 1-based. */
1723 high
= xfs_btree_get_numrecs(block
);
1725 /* Block is empty, must be an empty leaf. */
1726 ASSERT(level
== 0 && cur
->bc_nlevels
== 1);
1728 cur
->bc_ptrs
[0] = dir
!= XFS_LOOKUP_LE
;
1729 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
1734 /* Binary search the block. */
1735 while (low
<= high
) {
1736 union xfs_btree_key key
;
1737 union xfs_btree_key
*kp
;
1739 XFS_BTREE_STATS_INC(cur
, compare
);
1741 /* keyno is average of low and high. */
1742 keyno
= (low
+ high
) >> 1;
1744 /* Get current search key */
1745 kp
= xfs_lookup_get_search_key(cur
, level
,
1746 keyno
, block
, &key
);
1749 * Compute difference to get next direction:
1750 * - less than, move right
1751 * - greater than, move left
1752 * - equal, we're done
1754 diff
= cur
->bc_ops
->key_diff(cur
, kp
);
1765 * If there are more levels, set up for the next level
1766 * by getting the block number and filling in the cursor.
1770 * If we moved left, need the previous key number,
1771 * unless there isn't one.
1773 if (diff
> 0 && --keyno
< 1)
1775 pp
= xfs_btree_ptr_addr(cur
, keyno
, block
);
1778 error
= xfs_btree_check_ptr(cur
, pp
, 0, level
);
1782 cur
->bc_ptrs
[level
] = keyno
;
1786 /* Done with the search. See if we need to adjust the results. */
1787 if (dir
!= XFS_LOOKUP_LE
&& diff
< 0) {
1790 * If ge search and we went off the end of the block, but it's
1791 * not the last block, we're in the wrong block.
1793 xfs_btree_get_sibling(cur
, block
, &ptr
, XFS_BB_RIGHTSIB
);
1794 if (dir
== XFS_LOOKUP_GE
&&
1795 keyno
> xfs_btree_get_numrecs(block
) &&
1796 !xfs_btree_ptr_is_null(cur
, &ptr
)) {
1799 cur
->bc_ptrs
[0] = keyno
;
1800 error
= xfs_btree_increment(cur
, 0, &i
);
1803 XFS_WANT_CORRUPTED_RETURN(i
== 1);
1804 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
1808 } else if (dir
== XFS_LOOKUP_LE
&& diff
> 0)
1810 cur
->bc_ptrs
[0] = keyno
;
1812 /* Return if we succeeded or not. */
1813 if (keyno
== 0 || keyno
> xfs_btree_get_numrecs(block
))
1815 else if (dir
!= XFS_LOOKUP_EQ
|| diff
== 0)
1819 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
1823 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
1828 * Update keys at all levels from here to the root along the cursor's path.
1832 struct xfs_btree_cur
*cur
,
1833 union xfs_btree_key
*keyp
,
1836 struct xfs_btree_block
*block
;
1838 union xfs_btree_key
*kp
;
1841 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
1842 XFS_BTREE_TRACE_ARGIK(cur
, level
, keyp
);
1844 ASSERT(!(cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
) || level
>= 1);
1847 * Go up the tree from this level toward the root.
1848 * At each level, update the key value to the value input.
1849 * Stop when we reach a level where the cursor isn't pointing
1850 * at the first entry in the block.
1852 for (ptr
= 1; ptr
== 1 && level
< cur
->bc_nlevels
; level
++) {
1856 block
= xfs_btree_get_block(cur
, level
, &bp
);
1858 error
= xfs_btree_check_block(cur
, block
, level
, bp
);
1860 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
1864 ptr
= cur
->bc_ptrs
[level
];
1865 kp
= xfs_btree_key_addr(cur
, ptr
, block
);
1866 xfs_btree_copy_keys(cur
, kp
, keyp
, 1);
1867 xfs_btree_log_keys(cur
, bp
, ptr
, ptr
);
1870 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
1875 * Update the record referred to by cur to the value in the
1876 * given record. This either works (return 0) or gets an
1877 * EFSCORRUPTED error.
1881 struct xfs_btree_cur
*cur
,
1882 union xfs_btree_rec
*rec
)
1884 struct xfs_btree_block
*block
;
1888 union xfs_btree_rec
*rp
;
1890 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
1891 XFS_BTREE_TRACE_ARGR(cur
, rec
);
1893 /* Pick up the current block. */
1894 block
= xfs_btree_get_block(cur
, 0, &bp
);
1897 error
= xfs_btree_check_block(cur
, block
, 0, bp
);
1901 /* Get the address of the rec to be updated. */
1902 ptr
= cur
->bc_ptrs
[0];
1903 rp
= xfs_btree_rec_addr(cur
, ptr
, block
);
1905 /* Fill in the new contents and log them. */
1906 xfs_btree_copy_recs(cur
, rp
, rec
, 1);
1907 xfs_btree_log_recs(cur
, bp
, ptr
, ptr
);
1910 * If we are tracking the last record in the tree and
1911 * we are at the far right edge of the tree, update it.
1913 if (xfs_btree_is_lastrec(cur
, block
, 0)) {
1914 cur
->bc_ops
->update_lastrec(cur
, block
, rec
,
1915 ptr
, LASTREC_UPDATE
);
1918 /* Updating first rec in leaf. Pass new key value up to our parent. */
1920 union xfs_btree_key key
;
1922 cur
->bc_ops
->init_key_from_rec(&key
, rec
);
1923 error
= xfs_btree_updkey(cur
, &key
, 1);
1928 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
1932 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
1937 * Move 1 record left from cur/level if possible.
1938 * Update cur to reflect the new path.
1942 struct xfs_btree_cur
*cur
,
1944 int *stat
) /* success/failure */
1946 union xfs_btree_key key
; /* btree key */
1947 struct xfs_buf
*lbp
; /* left buffer pointer */
1948 struct xfs_btree_block
*left
; /* left btree block */
1949 int lrecs
; /* left record count */
1950 struct xfs_buf
*rbp
; /* right buffer pointer */
1951 struct xfs_btree_block
*right
; /* right btree block */
1952 int rrecs
; /* right record count */
1953 union xfs_btree_ptr lptr
; /* left btree pointer */
1954 union xfs_btree_key
*rkp
= NULL
; /* right btree key */
1955 union xfs_btree_ptr
*rpp
= NULL
; /* right address pointer */
1956 union xfs_btree_rec
*rrp
= NULL
; /* right record pointer */
1957 int error
; /* error return value */
1959 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
1960 XFS_BTREE_TRACE_ARGI(cur
, level
);
1962 if ((cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
) &&
1963 level
== cur
->bc_nlevels
- 1)
1966 /* Set up variables for this block as "right". */
1967 right
= xfs_btree_get_block(cur
, level
, &rbp
);
1970 error
= xfs_btree_check_block(cur
, right
, level
, rbp
);
1975 /* If we've got no left sibling then we can't shift an entry left. */
1976 xfs_btree_get_sibling(cur
, right
, &lptr
, XFS_BB_LEFTSIB
);
1977 if (xfs_btree_ptr_is_null(cur
, &lptr
))
1981 * If the cursor entry is the one that would be moved, don't
1982 * do it... it's too complicated.
1984 if (cur
->bc_ptrs
[level
] <= 1)
1987 /* Set up the left neighbor as "left". */
1988 error
= xfs_btree_read_buf_block(cur
, &lptr
, level
, 0, &left
, &lbp
);
1992 /* If it's full, it can't take another entry. */
1993 lrecs
= xfs_btree_get_numrecs(left
);
1994 if (lrecs
== cur
->bc_ops
->get_maxrecs(cur
, level
))
1997 rrecs
= xfs_btree_get_numrecs(right
);
2000 * We add one entry to the left side and remove one for the right side.
2001 * Accout for it here, the changes will be updated on disk and logged
2007 XFS_BTREE_STATS_INC(cur
, lshift
);
2008 XFS_BTREE_STATS_ADD(cur
, moves
, 1);
2011 * If non-leaf, copy a key and a ptr to the left block.
2012 * Log the changes to the left block.
2015 /* It's a non-leaf. Move keys and pointers. */
2016 union xfs_btree_key
*lkp
; /* left btree key */
2017 union xfs_btree_ptr
*lpp
; /* left address pointer */
2019 lkp
= xfs_btree_key_addr(cur
, lrecs
, left
);
2020 rkp
= xfs_btree_key_addr(cur
, 1, right
);
2022 lpp
= xfs_btree_ptr_addr(cur
, lrecs
, left
);
2023 rpp
= xfs_btree_ptr_addr(cur
, 1, right
);
2025 error
= xfs_btree_check_ptr(cur
, rpp
, 0, level
);
2029 xfs_btree_copy_keys(cur
, lkp
, rkp
, 1);
2030 xfs_btree_copy_ptrs(cur
, lpp
, rpp
, 1);
2032 xfs_btree_log_keys(cur
, lbp
, lrecs
, lrecs
);
2033 xfs_btree_log_ptrs(cur
, lbp
, lrecs
, lrecs
);
2035 xfs_btree_check_key(cur
->bc_btnum
,
2036 xfs_btree_key_addr(cur
, lrecs
- 1, left
),
2039 /* It's a leaf. Move records. */
2040 union xfs_btree_rec
*lrp
; /* left record pointer */
2042 lrp
= xfs_btree_rec_addr(cur
, lrecs
, left
);
2043 rrp
= xfs_btree_rec_addr(cur
, 1, right
);
2045 xfs_btree_copy_recs(cur
, lrp
, rrp
, 1);
2046 xfs_btree_log_recs(cur
, lbp
, lrecs
, lrecs
);
2048 xfs_btree_check_rec(cur
->bc_btnum
,
2049 xfs_btree_rec_addr(cur
, lrecs
- 1, left
),
2053 xfs_btree_set_numrecs(left
, lrecs
);
2054 xfs_btree_log_block(cur
, lbp
, XFS_BB_NUMRECS
);
2056 xfs_btree_set_numrecs(right
, rrecs
);
2057 xfs_btree_log_block(cur
, rbp
, XFS_BB_NUMRECS
);
2060 * Slide the contents of right down one entry.
2062 XFS_BTREE_STATS_ADD(cur
, moves
, rrecs
- 1);
2064 /* It's a nonleaf. operate on keys and ptrs */
2066 int i
; /* loop index */
2068 for (i
= 0; i
< rrecs
; i
++) {
2069 error
= xfs_btree_check_ptr(cur
, rpp
, i
+ 1, level
);
2074 xfs_btree_shift_keys(cur
,
2075 xfs_btree_key_addr(cur
, 2, right
),
2077 xfs_btree_shift_ptrs(cur
,
2078 xfs_btree_ptr_addr(cur
, 2, right
),
2081 xfs_btree_log_keys(cur
, rbp
, 1, rrecs
);
2082 xfs_btree_log_ptrs(cur
, rbp
, 1, rrecs
);
2084 /* It's a leaf. operate on records */
2085 xfs_btree_shift_recs(cur
,
2086 xfs_btree_rec_addr(cur
, 2, right
),
2088 xfs_btree_log_recs(cur
, rbp
, 1, rrecs
);
2091 * If it's the first record in the block, we'll need a key
2092 * structure to pass up to the next level (updkey).
2094 cur
->bc_ops
->init_key_from_rec(&key
,
2095 xfs_btree_rec_addr(cur
, 1, right
));
2099 /* Update the parent key values of right. */
2100 error
= xfs_btree_updkey(cur
, rkp
, level
+ 1);
2104 /* Slide the cursor value left one. */
2105 cur
->bc_ptrs
[level
]--;
2107 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
2112 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
2117 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
2122 * Move 1 record right from cur/level if possible.
2123 * Update cur to reflect the new path.
2127 struct xfs_btree_cur
*cur
,
2129 int *stat
) /* success/failure */
2131 union xfs_btree_key key
; /* btree key */
2132 struct xfs_buf
*lbp
; /* left buffer pointer */
2133 struct xfs_btree_block
*left
; /* left btree block */
2134 struct xfs_buf
*rbp
; /* right buffer pointer */
2135 struct xfs_btree_block
*right
; /* right btree block */
2136 struct xfs_btree_cur
*tcur
; /* temporary btree cursor */
2137 union xfs_btree_ptr rptr
; /* right block pointer */
2138 union xfs_btree_key
*rkp
; /* right btree key */
2139 int rrecs
; /* right record count */
2140 int lrecs
; /* left record count */
2141 int error
; /* error return value */
2142 int i
; /* loop counter */
2144 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
2145 XFS_BTREE_TRACE_ARGI(cur
, level
);
2147 if ((cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
) &&
2148 (level
== cur
->bc_nlevels
- 1))
2151 /* Set up variables for this block as "left". */
2152 left
= xfs_btree_get_block(cur
, level
, &lbp
);
2155 error
= xfs_btree_check_block(cur
, left
, level
, lbp
);
2160 /* If we've got no right sibling then we can't shift an entry right. */
2161 xfs_btree_get_sibling(cur
, left
, &rptr
, XFS_BB_RIGHTSIB
);
2162 if (xfs_btree_ptr_is_null(cur
, &rptr
))
2166 * If the cursor entry is the one that would be moved, don't
2167 * do it... it's too complicated.
2169 lrecs
= xfs_btree_get_numrecs(left
);
2170 if (cur
->bc_ptrs
[level
] >= lrecs
)
2173 /* Set up the right neighbor as "right". */
2174 error
= xfs_btree_read_buf_block(cur
, &rptr
, level
, 0, &right
, &rbp
);
2178 /* If it's full, it can't take another entry. */
2179 rrecs
= xfs_btree_get_numrecs(right
);
2180 if (rrecs
== cur
->bc_ops
->get_maxrecs(cur
, level
))
2183 XFS_BTREE_STATS_INC(cur
, rshift
);
2184 XFS_BTREE_STATS_ADD(cur
, moves
, rrecs
);
2187 * Make a hole at the start of the right neighbor block, then
2188 * copy the last left block entry to the hole.
2191 /* It's a nonleaf. make a hole in the keys and ptrs */
2192 union xfs_btree_key
*lkp
;
2193 union xfs_btree_ptr
*lpp
;
2194 union xfs_btree_ptr
*rpp
;
2196 lkp
= xfs_btree_key_addr(cur
, lrecs
, left
);
2197 lpp
= xfs_btree_ptr_addr(cur
, lrecs
, left
);
2198 rkp
= xfs_btree_key_addr(cur
, 1, right
);
2199 rpp
= xfs_btree_ptr_addr(cur
, 1, right
);
2202 for (i
= rrecs
- 1; i
>= 0; i
--) {
2203 error
= xfs_btree_check_ptr(cur
, rpp
, i
, level
);
2209 xfs_btree_shift_keys(cur
, rkp
, 1, rrecs
);
2210 xfs_btree_shift_ptrs(cur
, rpp
, 1, rrecs
);
2213 error
= xfs_btree_check_ptr(cur
, lpp
, 0, level
);
2218 /* Now put the new data in, and log it. */
2219 xfs_btree_copy_keys(cur
, rkp
, lkp
, 1);
2220 xfs_btree_copy_ptrs(cur
, rpp
, lpp
, 1);
2222 xfs_btree_log_keys(cur
, rbp
, 1, rrecs
+ 1);
2223 xfs_btree_log_ptrs(cur
, rbp
, 1, rrecs
+ 1);
2225 xfs_btree_check_key(cur
->bc_btnum
, rkp
,
2226 xfs_btree_key_addr(cur
, 2, right
));
2228 /* It's a leaf. make a hole in the records */
2229 union xfs_btree_rec
*lrp
;
2230 union xfs_btree_rec
*rrp
;
2232 lrp
= xfs_btree_rec_addr(cur
, lrecs
, left
);
2233 rrp
= xfs_btree_rec_addr(cur
, 1, right
);
2235 xfs_btree_shift_recs(cur
, rrp
, 1, rrecs
);
2237 /* Now put the new data in, and log it. */
2238 xfs_btree_copy_recs(cur
, rrp
, lrp
, 1);
2239 xfs_btree_log_recs(cur
, rbp
, 1, rrecs
+ 1);
2241 cur
->bc_ops
->init_key_from_rec(&key
, rrp
);
2244 xfs_btree_check_rec(cur
->bc_btnum
, rrp
,
2245 xfs_btree_rec_addr(cur
, 2, right
));
2249 * Decrement and log left's numrecs, bump and log right's numrecs.
2251 xfs_btree_set_numrecs(left
, --lrecs
);
2252 xfs_btree_log_block(cur
, lbp
, XFS_BB_NUMRECS
);
2254 xfs_btree_set_numrecs(right
, ++rrecs
);
2255 xfs_btree_log_block(cur
, rbp
, XFS_BB_NUMRECS
);
2258 * Using a temporary cursor, update the parent key values of the
2259 * block on the right.
2261 error
= xfs_btree_dup_cursor(cur
, &tcur
);
2264 i
= xfs_btree_lastrec(tcur
, level
);
2265 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
2267 error
= xfs_btree_increment(tcur
, level
, &i
);
2271 error
= xfs_btree_updkey(tcur
, rkp
, level
+ 1);
2275 xfs_btree_del_cursor(tcur
, XFS_BTREE_NOERROR
);
2277 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
2282 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
2287 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
2291 XFS_BTREE_TRACE_CURSOR(tcur
, XBT_ERROR
);
2292 xfs_btree_del_cursor(tcur
, XFS_BTREE_ERROR
);
2297 * Split cur/level block in half.
2298 * Return new block number and the key to its first
2299 * record (to be inserted into parent).
2303 struct xfs_btree_cur
*cur
,
2305 union xfs_btree_ptr
*ptrp
,
2306 union xfs_btree_key
*key
,
2307 struct xfs_btree_cur
**curp
,
2308 int *stat
) /* success/failure */
2310 union xfs_btree_ptr lptr
; /* left sibling block ptr */
2311 struct xfs_buf
*lbp
; /* left buffer pointer */
2312 struct xfs_btree_block
*left
; /* left btree block */
2313 union xfs_btree_ptr rptr
; /* right sibling block ptr */
2314 struct xfs_buf
*rbp
; /* right buffer pointer */
2315 struct xfs_btree_block
*right
; /* right btree block */
2316 union xfs_btree_ptr rrptr
; /* right-right sibling ptr */
2317 struct xfs_buf
*rrbp
; /* right-right buffer pointer */
2318 struct xfs_btree_block
*rrblock
; /* right-right btree block */
2322 int error
; /* error return value */
2327 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
2328 XFS_BTREE_TRACE_ARGIPK(cur
, level
, *ptrp
, key
);
2330 XFS_BTREE_STATS_INC(cur
, split
);
2332 /* Set up left block (current one). */
2333 left
= xfs_btree_get_block(cur
, level
, &lbp
);
2336 error
= xfs_btree_check_block(cur
, left
, level
, lbp
);
2341 xfs_btree_buf_to_ptr(cur
, lbp
, &lptr
);
2343 /* Allocate the new block. If we can't do it, we're toast. Give up. */
2344 error
= cur
->bc_ops
->alloc_block(cur
, &lptr
, &rptr
, 1, stat
);
2349 XFS_BTREE_STATS_INC(cur
, alloc
);
2351 /* Set up the new block as "right". */
2352 error
= xfs_btree_get_buf_block(cur
, &rptr
, 0, &right
, &rbp
);
2356 /* Fill in the btree header for the new right block. */
2357 xfs_btree_init_block(cur
, xfs_btree_get_level(left
), 0, right
);
2360 * Split the entries between the old and the new block evenly.
2361 * Make sure that if there's an odd number of entries now, that
2362 * each new block will have the same number of entries.
2364 lrecs
= xfs_btree_get_numrecs(left
);
2366 if ((lrecs
& 1) && cur
->bc_ptrs
[level
] <= rrecs
+ 1)
2368 src_index
= (lrecs
- rrecs
+ 1);
2370 XFS_BTREE_STATS_ADD(cur
, moves
, rrecs
);
2373 * Copy btree block entries from the left block over to the
2374 * new block, the right. Update the right block and log the
2378 /* It's a non-leaf. Move keys and pointers. */
2379 union xfs_btree_key
*lkp
; /* left btree key */
2380 union xfs_btree_ptr
*lpp
; /* left address pointer */
2381 union xfs_btree_key
*rkp
; /* right btree key */
2382 union xfs_btree_ptr
*rpp
; /* right address pointer */
2384 lkp
= xfs_btree_key_addr(cur
, src_index
, left
);
2385 lpp
= xfs_btree_ptr_addr(cur
, src_index
, left
);
2386 rkp
= xfs_btree_key_addr(cur
, 1, right
);
2387 rpp
= xfs_btree_ptr_addr(cur
, 1, right
);
2390 for (i
= src_index
; i
< rrecs
; i
++) {
2391 error
= xfs_btree_check_ptr(cur
, lpp
, i
, level
);
2397 xfs_btree_copy_keys(cur
, rkp
, lkp
, rrecs
);
2398 xfs_btree_copy_ptrs(cur
, rpp
, lpp
, rrecs
);
2400 xfs_btree_log_keys(cur
, rbp
, 1, rrecs
);
2401 xfs_btree_log_ptrs(cur
, rbp
, 1, rrecs
);
2403 /* Grab the keys to the entries moved to the right block */
2404 xfs_btree_copy_keys(cur
, key
, rkp
, 1);
2406 /* It's a leaf. Move records. */
2407 union xfs_btree_rec
*lrp
; /* left record pointer */
2408 union xfs_btree_rec
*rrp
; /* right record pointer */
2410 lrp
= xfs_btree_rec_addr(cur
, src_index
, left
);
2411 rrp
= xfs_btree_rec_addr(cur
, 1, right
);
2413 xfs_btree_copy_recs(cur
, rrp
, lrp
, rrecs
);
2414 xfs_btree_log_recs(cur
, rbp
, 1, rrecs
);
2416 cur
->bc_ops
->init_key_from_rec(key
,
2417 xfs_btree_rec_addr(cur
, 1, right
));
2422 * Find the left block number by looking in the buffer.
2423 * Adjust numrecs, sibling pointers.
2425 xfs_btree_get_sibling(cur
, left
, &rrptr
, XFS_BB_RIGHTSIB
);
2426 xfs_btree_set_sibling(cur
, right
, &rrptr
, XFS_BB_RIGHTSIB
);
2427 xfs_btree_set_sibling(cur
, right
, &lptr
, XFS_BB_LEFTSIB
);
2428 xfs_btree_set_sibling(cur
, left
, &rptr
, XFS_BB_RIGHTSIB
);
2431 xfs_btree_set_numrecs(left
, lrecs
);
2432 xfs_btree_set_numrecs(right
, xfs_btree_get_numrecs(right
) + rrecs
);
2434 xfs_btree_log_block(cur
, rbp
, XFS_BB_ALL_BITS
);
2435 xfs_btree_log_block(cur
, lbp
, XFS_BB_NUMRECS
| XFS_BB_RIGHTSIB
);
2438 * If there's a block to the new block's right, make that block
2439 * point back to right instead of to left.
2441 if (!xfs_btree_ptr_is_null(cur
, &rrptr
)) {
2442 error
= xfs_btree_read_buf_block(cur
, &rrptr
, level
,
2443 0, &rrblock
, &rrbp
);
2446 xfs_btree_set_sibling(cur
, rrblock
, &rptr
, XFS_BB_LEFTSIB
);
2447 xfs_btree_log_block(cur
, rrbp
, XFS_BB_LEFTSIB
);
2450 * If the cursor is really in the right block, move it there.
2451 * If it's just pointing past the last entry in left, then we'll
2452 * insert there, so don't change anything in that case.
2454 if (cur
->bc_ptrs
[level
] > lrecs
+ 1) {
2455 xfs_btree_setbuf(cur
, level
, rbp
);
2456 cur
->bc_ptrs
[level
] -= lrecs
;
2459 * If there are more levels, we'll need another cursor which refers
2460 * the right block, no matter where this cursor was.
2462 if (level
+ 1 < cur
->bc_nlevels
) {
2463 error
= xfs_btree_dup_cursor(cur
, curp
);
2466 (*curp
)->bc_ptrs
[level
+ 1]++;
2469 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
2473 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
2478 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
2483 * Copy the old inode root contents into a real block and make the
2484 * broot point to it.
2487 xfs_btree_new_iroot(
2488 struct xfs_btree_cur
*cur
, /* btree cursor */
2489 int *logflags
, /* logging flags for inode */
2490 int *stat
) /* return status - 0 fail */
2492 struct xfs_buf
*cbp
; /* buffer for cblock */
2493 struct xfs_btree_block
*block
; /* btree block */
2494 struct xfs_btree_block
*cblock
; /* child btree block */
2495 union xfs_btree_key
*ckp
; /* child key pointer */
2496 union xfs_btree_ptr
*cpp
; /* child ptr pointer */
2497 union xfs_btree_key
*kp
; /* pointer to btree key */
2498 union xfs_btree_ptr
*pp
; /* pointer to block addr */
2499 union xfs_btree_ptr nptr
; /* new block addr */
2500 int level
; /* btree level */
2501 int error
; /* error return code */
2503 int i
; /* loop counter */
2506 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
2507 XFS_BTREE_STATS_INC(cur
, newroot
);
2509 ASSERT(cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
);
2511 level
= cur
->bc_nlevels
- 1;
2513 block
= xfs_btree_get_iroot(cur
);
2514 pp
= xfs_btree_ptr_addr(cur
, 1, block
);
2516 /* Allocate the new block. If we can't do it, we're toast. Give up. */
2517 error
= cur
->bc_ops
->alloc_block(cur
, pp
, &nptr
, 1, stat
);
2521 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
2524 XFS_BTREE_STATS_INC(cur
, alloc
);
2526 /* Copy the root into a real block. */
2527 error
= xfs_btree_get_buf_block(cur
, &nptr
, 0, &cblock
, &cbp
);
2531 memcpy(cblock
, block
, xfs_btree_block_len(cur
));
2533 be16_add_cpu(&block
->bb_level
, 1);
2534 xfs_btree_set_numrecs(block
, 1);
2536 cur
->bc_ptrs
[level
+ 1] = 1;
2538 kp
= xfs_btree_key_addr(cur
, 1, block
);
2539 ckp
= xfs_btree_key_addr(cur
, 1, cblock
);
2540 xfs_btree_copy_keys(cur
, ckp
, kp
, xfs_btree_get_numrecs(cblock
));
2542 cpp
= xfs_btree_ptr_addr(cur
, 1, cblock
);
2544 for (i
= 0; i
< be16_to_cpu(cblock
->bb_numrecs
); i
++) {
2545 error
= xfs_btree_check_ptr(cur
, pp
, i
, level
);
2550 xfs_btree_copy_ptrs(cur
, cpp
, pp
, xfs_btree_get_numrecs(cblock
));
2553 error
= xfs_btree_check_ptr(cur
, &nptr
, 0, level
);
2557 xfs_btree_copy_ptrs(cur
, pp
, &nptr
, 1);
2559 xfs_iroot_realloc(cur
->bc_private
.b
.ip
,
2560 1 - xfs_btree_get_numrecs(cblock
),
2561 cur
->bc_private
.b
.whichfork
);
2563 xfs_btree_setbuf(cur
, level
, cbp
);
2566 * Do all this logging at the end so that
2567 * the root is at the right level.
2569 xfs_btree_log_block(cur
, cbp
, XFS_BB_ALL_BITS
);
2570 xfs_btree_log_keys(cur
, cbp
, 1, be16_to_cpu(cblock
->bb_numrecs
));
2571 xfs_btree_log_ptrs(cur
, cbp
, 1, be16_to_cpu(cblock
->bb_numrecs
));
2574 XFS_ILOG_CORE
| XFS_ILOG_FBROOT(cur
->bc_private
.b
.whichfork
);
2576 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
2579 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
2584 * Allocate a new root block, fill it in.
2588 struct xfs_btree_cur
*cur
, /* btree cursor */
2589 int *stat
) /* success/failure */
2591 struct xfs_btree_block
*block
; /* one half of the old root block */
2592 struct xfs_buf
*bp
; /* buffer containing block */
2593 int error
; /* error return value */
2594 struct xfs_buf
*lbp
; /* left buffer pointer */
2595 struct xfs_btree_block
*left
; /* left btree block */
2596 struct xfs_buf
*nbp
; /* new (root) buffer */
2597 struct xfs_btree_block
*new; /* new (root) btree block */
2598 int nptr
; /* new value for key index, 1 or 2 */
2599 struct xfs_buf
*rbp
; /* right buffer pointer */
2600 struct xfs_btree_block
*right
; /* right btree block */
2601 union xfs_btree_ptr rptr
;
2602 union xfs_btree_ptr lptr
;
2604 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
2605 XFS_BTREE_STATS_INC(cur
, newroot
);
2607 /* initialise our start point from the cursor */
2608 cur
->bc_ops
->init_ptr_from_cur(cur
, &rptr
);
2610 /* Allocate the new block. If we can't do it, we're toast. Give up. */
2611 error
= cur
->bc_ops
->alloc_block(cur
, &rptr
, &lptr
, 1, stat
);
2616 XFS_BTREE_STATS_INC(cur
, alloc
);
2618 /* Set up the new block. */
2619 error
= xfs_btree_get_buf_block(cur
, &lptr
, 0, &new, &nbp
);
2623 /* Set the root in the holding structure increasing the level by 1. */
2624 cur
->bc_ops
->set_root(cur
, &lptr
, 1);
2627 * At the previous root level there are now two blocks: the old root,
2628 * and the new block generated when it was split. We don't know which
2629 * one the cursor is pointing at, so we set up variables "left" and
2630 * "right" for each case.
2632 block
= xfs_btree_get_block(cur
, cur
->bc_nlevels
- 1, &bp
);
2635 error
= xfs_btree_check_block(cur
, block
, cur
->bc_nlevels
- 1, bp
);
2640 xfs_btree_get_sibling(cur
, block
, &rptr
, XFS_BB_RIGHTSIB
);
2641 if (!xfs_btree_ptr_is_null(cur
, &rptr
)) {
2642 /* Our block is left, pick up the right block. */
2644 xfs_btree_buf_to_ptr(cur
, lbp
, &lptr
);
2646 error
= xfs_btree_read_buf_block(cur
, &rptr
,
2647 cur
->bc_nlevels
- 1, 0, &right
, &rbp
);
2653 /* Our block is right, pick up the left block. */
2655 xfs_btree_buf_to_ptr(cur
, rbp
, &rptr
);
2657 xfs_btree_get_sibling(cur
, right
, &lptr
, XFS_BB_LEFTSIB
);
2658 error
= xfs_btree_read_buf_block(cur
, &lptr
,
2659 cur
->bc_nlevels
- 1, 0, &left
, &lbp
);
2665 /* Fill in the new block's btree header and log it. */
2666 xfs_btree_init_block(cur
, cur
->bc_nlevels
, 2, new);
2667 xfs_btree_log_block(cur
, nbp
, XFS_BB_ALL_BITS
);
2668 ASSERT(!xfs_btree_ptr_is_null(cur
, &lptr
) &&
2669 !xfs_btree_ptr_is_null(cur
, &rptr
));
2671 /* Fill in the key data in the new root. */
2672 if (xfs_btree_get_level(left
) > 0) {
2673 xfs_btree_copy_keys(cur
,
2674 xfs_btree_key_addr(cur
, 1, new),
2675 xfs_btree_key_addr(cur
, 1, left
), 1);
2676 xfs_btree_copy_keys(cur
,
2677 xfs_btree_key_addr(cur
, 2, new),
2678 xfs_btree_key_addr(cur
, 1, right
), 1);
2680 cur
->bc_ops
->init_key_from_rec(
2681 xfs_btree_key_addr(cur
, 1, new),
2682 xfs_btree_rec_addr(cur
, 1, left
));
2683 cur
->bc_ops
->init_key_from_rec(
2684 xfs_btree_key_addr(cur
, 2, new),
2685 xfs_btree_rec_addr(cur
, 1, right
));
2687 xfs_btree_log_keys(cur
, nbp
, 1, 2);
2689 /* Fill in the pointer data in the new root. */
2690 xfs_btree_copy_ptrs(cur
,
2691 xfs_btree_ptr_addr(cur
, 1, new), &lptr
, 1);
2692 xfs_btree_copy_ptrs(cur
,
2693 xfs_btree_ptr_addr(cur
, 2, new), &rptr
, 1);
2694 xfs_btree_log_ptrs(cur
, nbp
, 1, 2);
2696 /* Fix up the cursor. */
2697 xfs_btree_setbuf(cur
, cur
->bc_nlevels
, nbp
);
2698 cur
->bc_ptrs
[cur
->bc_nlevels
] = nptr
;
2700 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
2704 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
2707 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
2713 xfs_btree_make_block_unfull(
2714 struct xfs_btree_cur
*cur
, /* btree cursor */
2715 int level
, /* btree level */
2716 int numrecs
,/* # of recs in block */
2717 int *oindex
,/* old tree index */
2718 int *index
, /* new tree index */
2719 union xfs_btree_ptr
*nptr
, /* new btree ptr */
2720 struct xfs_btree_cur
**ncur
, /* new btree cursor */
2721 union xfs_btree_rec
*nrec
, /* new record */
2724 union xfs_btree_key key
; /* new btree key value */
2727 if ((cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
) &&
2728 level
== cur
->bc_nlevels
- 1) {
2729 struct xfs_inode
*ip
= cur
->bc_private
.b
.ip
;
2731 if (numrecs
< cur
->bc_ops
->get_dmaxrecs(cur
, level
)) {
2732 /* A root block that can be made bigger. */
2734 xfs_iroot_realloc(ip
, 1, cur
->bc_private
.b
.whichfork
);
2736 /* A root block that needs replacing */
2739 error
= xfs_btree_new_iroot(cur
, &logflags
, stat
);
2740 if (error
|| *stat
== 0)
2743 xfs_trans_log_inode(cur
->bc_tp
, ip
, logflags
);
2749 /* First, try shifting an entry to the right neighbor. */
2750 error
= xfs_btree_rshift(cur
, level
, stat
);
2754 /* Next, try shifting an entry to the left neighbor. */
2755 error
= xfs_btree_lshift(cur
, level
, stat
);
2760 *oindex
= *index
= cur
->bc_ptrs
[level
];
2765 * Next, try splitting the current block in half.
2767 * If this works we have to re-set our variables because we
2768 * could be in a different block now.
2770 error
= xfs_btree_split(cur
, level
, nptr
, &key
, ncur
, stat
);
2771 if (error
|| *stat
== 0)
2775 *index
= cur
->bc_ptrs
[level
];
2776 cur
->bc_ops
->init_rec_from_key(&key
, nrec
);
2781 * Insert one record/level. Return information to the caller
2782 * allowing the next level up to proceed if necessary.
2786 struct xfs_btree_cur
*cur
, /* btree cursor */
2787 int level
, /* level to insert record at */
2788 union xfs_btree_ptr
*ptrp
, /* i/o: block number inserted */
2789 union xfs_btree_rec
*recp
, /* i/o: record data inserted */
2790 struct xfs_btree_cur
**curp
, /* output: new cursor replacing cur */
2791 int *stat
) /* success/failure */
2793 struct xfs_btree_block
*block
; /* btree block */
2794 struct xfs_buf
*bp
; /* buffer for block */
2795 union xfs_btree_key key
; /* btree key */
2796 union xfs_btree_ptr nptr
; /* new block ptr */
2797 struct xfs_btree_cur
*ncur
; /* new btree cursor */
2798 union xfs_btree_rec nrec
; /* new record count */
2799 int optr
; /* old key/record index */
2800 int ptr
; /* key/record index */
2801 int numrecs
;/* number of records */
2802 int error
; /* error return value */
2807 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
2808 XFS_BTREE_TRACE_ARGIPR(cur
, level
, *ptrp
, recp
);
2813 * If we have an external root pointer, and we've made it to the
2814 * root level, allocate a new root block and we're done.
2816 if (!(cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
) &&
2817 (level
>= cur
->bc_nlevels
)) {
2818 error
= xfs_btree_new_root(cur
, stat
);
2819 xfs_btree_set_ptr_null(cur
, ptrp
);
2821 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
2825 /* If we're off the left edge, return failure. */
2826 ptr
= cur
->bc_ptrs
[level
];
2828 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
2833 /* Make a key out of the record data to be inserted, and save it. */
2834 cur
->bc_ops
->init_key_from_rec(&key
, recp
);
2838 XFS_BTREE_STATS_INC(cur
, insrec
);
2840 /* Get pointers to the btree buffer and block. */
2841 block
= xfs_btree_get_block(cur
, level
, &bp
);
2842 numrecs
= xfs_btree_get_numrecs(block
);
2845 error
= xfs_btree_check_block(cur
, block
, level
, bp
);
2849 /* Check that the new entry is being inserted in the right place. */
2850 if (ptr
<= numrecs
) {
2852 xfs_btree_check_rec(cur
->bc_btnum
, recp
,
2853 xfs_btree_rec_addr(cur
, ptr
, block
));
2855 xfs_btree_check_key(cur
->bc_btnum
, &key
,
2856 xfs_btree_key_addr(cur
, ptr
, block
));
2862 * If the block is full, we can't insert the new entry until we
2863 * make the block un-full.
2865 xfs_btree_set_ptr_null(cur
, &nptr
);
2866 if (numrecs
== cur
->bc_ops
->get_maxrecs(cur
, level
)) {
2867 error
= xfs_btree_make_block_unfull(cur
, level
, numrecs
,
2868 &optr
, &ptr
, &nptr
, &ncur
, &nrec
, stat
);
2869 if (error
|| *stat
== 0)
2874 * The current block may have changed if the block was
2875 * previously full and we have just made space in it.
2877 block
= xfs_btree_get_block(cur
, level
, &bp
);
2878 numrecs
= xfs_btree_get_numrecs(block
);
2881 error
= xfs_btree_check_block(cur
, block
, level
, bp
);
2887 * At this point we know there's room for our new entry in the block
2888 * we're pointing at.
2890 XFS_BTREE_STATS_ADD(cur
, moves
, numrecs
- ptr
+ 1);
2893 /* It's a nonleaf. make a hole in the keys and ptrs */
2894 union xfs_btree_key
*kp
;
2895 union xfs_btree_ptr
*pp
;
2897 kp
= xfs_btree_key_addr(cur
, ptr
, block
);
2898 pp
= xfs_btree_ptr_addr(cur
, ptr
, block
);
2901 for (i
= numrecs
- ptr
; i
>= 0; i
--) {
2902 error
= xfs_btree_check_ptr(cur
, pp
, i
, level
);
2908 xfs_btree_shift_keys(cur
, kp
, 1, numrecs
- ptr
+ 1);
2909 xfs_btree_shift_ptrs(cur
, pp
, 1, numrecs
- ptr
+ 1);
2912 error
= xfs_btree_check_ptr(cur
, ptrp
, 0, level
);
2917 /* Now put the new data in, bump numrecs and log it. */
2918 xfs_btree_copy_keys(cur
, kp
, &key
, 1);
2919 xfs_btree_copy_ptrs(cur
, pp
, ptrp
, 1);
2921 xfs_btree_set_numrecs(block
, numrecs
);
2922 xfs_btree_log_ptrs(cur
, bp
, ptr
, numrecs
);
2923 xfs_btree_log_keys(cur
, bp
, ptr
, numrecs
);
2925 if (ptr
< numrecs
) {
2926 xfs_btree_check_key(cur
->bc_btnum
, kp
,
2927 xfs_btree_key_addr(cur
, ptr
+ 1, block
));
2931 /* It's a leaf. make a hole in the records */
2932 union xfs_btree_rec
*rp
;
2934 rp
= xfs_btree_rec_addr(cur
, ptr
, block
);
2936 xfs_btree_shift_recs(cur
, rp
, 1, numrecs
- ptr
+ 1);
2938 /* Now put the new data in, bump numrecs and log it. */
2939 xfs_btree_copy_recs(cur
, rp
, recp
, 1);
2940 xfs_btree_set_numrecs(block
, ++numrecs
);
2941 xfs_btree_log_recs(cur
, bp
, ptr
, numrecs
);
2943 if (ptr
< numrecs
) {
2944 xfs_btree_check_rec(cur
->bc_btnum
, rp
,
2945 xfs_btree_rec_addr(cur
, ptr
+ 1, block
));
2950 /* Log the new number of records in the btree header. */
2951 xfs_btree_log_block(cur
, bp
, XFS_BB_NUMRECS
);
2953 /* If we inserted at the start of a block, update the parents' keys. */
2955 error
= xfs_btree_updkey(cur
, &key
, level
+ 1);
2961 * If we are tracking the last record in the tree and
2962 * we are at the far right edge of the tree, update it.
2964 if (xfs_btree_is_lastrec(cur
, block
, level
)) {
2965 cur
->bc_ops
->update_lastrec(cur
, block
, recp
,
2966 ptr
, LASTREC_INSREC
);
2970 * Return the new block number, if any.
2971 * If there is one, give back a record value and a cursor too.
2974 if (!xfs_btree_ptr_is_null(cur
, &nptr
)) {
2979 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
2984 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
2989 * Insert the record at the point referenced by cur.
2991 * A multi-level split of the tree on insert will invalidate the original
2992 * cursor. All callers of this function should assume that the cursor is
2993 * no longer valid and revalidate it.
2997 struct xfs_btree_cur
*cur
,
3000 int error
; /* error return value */
3001 int i
; /* result value, 0 for failure */
3002 int level
; /* current level number in btree */
3003 union xfs_btree_ptr nptr
; /* new block number (split result) */
3004 struct xfs_btree_cur
*ncur
; /* new cursor (split result) */
3005 struct xfs_btree_cur
*pcur
; /* previous level's cursor */
3006 union xfs_btree_rec rec
; /* record to insert */
3012 xfs_btree_set_ptr_null(cur
, &nptr
);
3013 cur
->bc_ops
->init_rec_from_cur(cur
, &rec
);
3016 * Loop going up the tree, starting at the leaf level.
3017 * Stop when we don't get a split block, that must mean that
3018 * the insert is finished with this level.
3022 * Insert nrec/nptr into this level of the tree.
3023 * Note if we fail, nptr will be null.
3025 error
= xfs_btree_insrec(pcur
, level
, &nptr
, &rec
, &ncur
, &i
);
3028 xfs_btree_del_cursor(pcur
, XFS_BTREE_ERROR
);
3032 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
3036 * See if the cursor we just used is trash.
3037 * Can't trash the caller's cursor, but otherwise we should
3038 * if ncur is a new cursor or we're about to be done.
3041 (ncur
|| xfs_btree_ptr_is_null(cur
, &nptr
))) {
3042 /* Save the state from the cursor before we trash it */
3043 if (cur
->bc_ops
->update_cursor
)
3044 cur
->bc_ops
->update_cursor(pcur
, cur
);
3045 cur
->bc_nlevels
= pcur
->bc_nlevels
;
3046 xfs_btree_del_cursor(pcur
, XFS_BTREE_NOERROR
);
3048 /* If we got a new cursor, switch to it. */
3053 } while (!xfs_btree_ptr_is_null(cur
, &nptr
));
3055 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
3059 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
3064 * Try to merge a non-leaf block back into the inode root.
3066 * Note: the killroot names comes from the fact that we're effectively
3067 * killing the old root block. But because we can't just delete the
3068 * inode we have to copy the single block it was pointing to into the
3072 xfs_btree_kill_iroot(
3073 struct xfs_btree_cur
*cur
)
3075 int whichfork
= cur
->bc_private
.b
.whichfork
;
3076 struct xfs_inode
*ip
= cur
->bc_private
.b
.ip
;
3077 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
3078 struct xfs_btree_block
*block
;
3079 struct xfs_btree_block
*cblock
;
3080 union xfs_btree_key
*kp
;
3081 union xfs_btree_key
*ckp
;
3082 union xfs_btree_ptr
*pp
;
3083 union xfs_btree_ptr
*cpp
;
3084 struct xfs_buf
*cbp
;
3089 union xfs_btree_ptr ptr
;
3093 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
3095 ASSERT(cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
);
3096 ASSERT(cur
->bc_nlevels
> 1);
3099 * Don't deal with the root block needs to be a leaf case.
3100 * We're just going to turn the thing back into extents anyway.
3102 level
= cur
->bc_nlevels
- 1;
3107 * Give up if the root has multiple children.
3109 block
= xfs_btree_get_iroot(cur
);
3110 if (xfs_btree_get_numrecs(block
) != 1)
3113 cblock
= xfs_btree_get_block(cur
, level
- 1, &cbp
);
3114 numrecs
= xfs_btree_get_numrecs(cblock
);
3117 * Only do this if the next level will fit.
3118 * Then the data must be copied up to the inode,
3119 * instead of freeing the root you free the next level.
3121 if (numrecs
> cur
->bc_ops
->get_dmaxrecs(cur
, level
))
3124 XFS_BTREE_STATS_INC(cur
, killroot
);
3127 xfs_btree_get_sibling(cur
, block
, &ptr
, XFS_BB_LEFTSIB
);
3128 ASSERT(xfs_btree_ptr_is_null(cur
, &ptr
));
3129 xfs_btree_get_sibling(cur
, block
, &ptr
, XFS_BB_RIGHTSIB
);
3130 ASSERT(xfs_btree_ptr_is_null(cur
, &ptr
));
3133 index
= numrecs
- cur
->bc_ops
->get_maxrecs(cur
, level
);
3135 xfs_iroot_realloc(cur
->bc_private
.b
.ip
, index
,
3136 cur
->bc_private
.b
.whichfork
);
3137 block
= (struct xfs_btree_block
*)ifp
->if_broot
;
3140 be16_add_cpu(&block
->bb_numrecs
, index
);
3141 ASSERT(block
->bb_numrecs
== cblock
->bb_numrecs
);
3143 kp
= xfs_btree_key_addr(cur
, 1, block
);
3144 ckp
= xfs_btree_key_addr(cur
, 1, cblock
);
3145 xfs_btree_copy_keys(cur
, kp
, ckp
, numrecs
);
3147 pp
= xfs_btree_ptr_addr(cur
, 1, block
);
3148 cpp
= xfs_btree_ptr_addr(cur
, 1, cblock
);
3150 for (i
= 0; i
< numrecs
; i
++) {
3153 error
= xfs_btree_check_ptr(cur
, cpp
, i
, level
- 1);
3155 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
3160 xfs_btree_copy_ptrs(cur
, pp
, cpp
, numrecs
);
3162 cur
->bc_ops
->free_block(cur
, cbp
);
3163 XFS_BTREE_STATS_INC(cur
, free
);
3165 cur
->bc_bufs
[level
- 1] = NULL
;
3166 be16_add_cpu(&block
->bb_level
, -1);
3167 xfs_trans_log_inode(cur
->bc_tp
, ip
,
3168 XFS_ILOG_CORE
| XFS_ILOG_FBROOT(cur
->bc_private
.b
.whichfork
));
3171 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
3176 xfs_btree_dec_cursor(
3177 struct xfs_btree_cur
*cur
,
3185 error
= xfs_btree_decrement(cur
, level
, &i
);
3190 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
3196 * Single level of the btree record deletion routine.
3197 * Delete record pointed to by cur/level.
3198 * Remove the record from its block then rebalance the tree.
3199 * Return 0 for error, 1 for done, 2 to go on to the next level.
3201 STATIC
int /* error */
3203 struct xfs_btree_cur
*cur
, /* btree cursor */
3204 int level
, /* level removing record from */
3205 int *stat
) /* fail/done/go-on */
3207 struct xfs_btree_block
*block
; /* btree block */
3208 union xfs_btree_ptr cptr
; /* current block ptr */
3209 struct xfs_buf
*bp
; /* buffer for block */
3210 int error
; /* error return value */
3211 int i
; /* loop counter */
3212 union xfs_btree_key key
; /* storage for keyp */
3213 union xfs_btree_key
*keyp
= &key
; /* passed to the next level */
3214 union xfs_btree_ptr lptr
; /* left sibling block ptr */
3215 struct xfs_buf
*lbp
; /* left buffer pointer */
3216 struct xfs_btree_block
*left
; /* left btree block */
3217 int lrecs
= 0; /* left record count */
3218 int ptr
; /* key/record index */
3219 union xfs_btree_ptr rptr
; /* right sibling block ptr */
3220 struct xfs_buf
*rbp
; /* right buffer pointer */
3221 struct xfs_btree_block
*right
; /* right btree block */
3222 struct xfs_btree_block
*rrblock
; /* right-right btree block */
3223 struct xfs_buf
*rrbp
; /* right-right buffer pointer */
3224 int rrecs
= 0; /* right record count */
3225 struct xfs_btree_cur
*tcur
; /* temporary btree cursor */
3226 int numrecs
; /* temporary numrec count */
3228 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
3229 XFS_BTREE_TRACE_ARGI(cur
, level
);
3233 /* Get the index of the entry being deleted, check for nothing there. */
3234 ptr
= cur
->bc_ptrs
[level
];
3236 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
3241 /* Get the buffer & block containing the record or key/ptr. */
3242 block
= xfs_btree_get_block(cur
, level
, &bp
);
3243 numrecs
= xfs_btree_get_numrecs(block
);
3246 error
= xfs_btree_check_block(cur
, block
, level
, bp
);
3251 /* Fail if we're off the end of the block. */
3252 if (ptr
> numrecs
) {
3253 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
3258 XFS_BTREE_STATS_INC(cur
, delrec
);
3259 XFS_BTREE_STATS_ADD(cur
, moves
, numrecs
- ptr
);
3261 /* Excise the entries being deleted. */
3263 /* It's a nonleaf. operate on keys and ptrs */
3264 union xfs_btree_key
*lkp
;
3265 union xfs_btree_ptr
*lpp
;
3267 lkp
= xfs_btree_key_addr(cur
, ptr
+ 1, block
);
3268 lpp
= xfs_btree_ptr_addr(cur
, ptr
+ 1, block
);
3271 for (i
= 0; i
< numrecs
- ptr
; i
++) {
3272 error
= xfs_btree_check_ptr(cur
, lpp
, i
, level
);
3278 if (ptr
< numrecs
) {
3279 xfs_btree_shift_keys(cur
, lkp
, -1, numrecs
- ptr
);
3280 xfs_btree_shift_ptrs(cur
, lpp
, -1, numrecs
- ptr
);
3281 xfs_btree_log_keys(cur
, bp
, ptr
, numrecs
- 1);
3282 xfs_btree_log_ptrs(cur
, bp
, ptr
, numrecs
- 1);
3286 * If it's the first record in the block, we'll need to pass a
3287 * key up to the next level (updkey).
3290 keyp
= xfs_btree_key_addr(cur
, 1, block
);
3292 /* It's a leaf. operate on records */
3293 if (ptr
< numrecs
) {
3294 xfs_btree_shift_recs(cur
,
3295 xfs_btree_rec_addr(cur
, ptr
+ 1, block
),
3297 xfs_btree_log_recs(cur
, bp
, ptr
, numrecs
- 1);
3301 * If it's the first record in the block, we'll need a key
3302 * structure to pass up to the next level (updkey).
3305 cur
->bc_ops
->init_key_from_rec(&key
,
3306 xfs_btree_rec_addr(cur
, 1, block
));
3312 * Decrement and log the number of entries in the block.
3314 xfs_btree_set_numrecs(block
, --numrecs
);
3315 xfs_btree_log_block(cur
, bp
, XFS_BB_NUMRECS
);
3318 * If we are tracking the last record in the tree and
3319 * we are at the far right edge of the tree, update it.
3321 if (xfs_btree_is_lastrec(cur
, block
, level
)) {
3322 cur
->bc_ops
->update_lastrec(cur
, block
, NULL
,
3323 ptr
, LASTREC_DELREC
);
3327 * We're at the root level. First, shrink the root block in-memory.
3328 * Try to get rid of the next level down. If we can't then there's
3329 * nothing left to do.
3331 if (level
== cur
->bc_nlevels
- 1) {
3332 if (cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
) {
3333 xfs_iroot_realloc(cur
->bc_private
.b
.ip
, -1,
3334 cur
->bc_private
.b
.whichfork
);
3336 error
= xfs_btree_kill_iroot(cur
);
3340 error
= xfs_btree_dec_cursor(cur
, level
, stat
);
3348 * If this is the root level, and there's only one entry left,
3349 * and it's NOT the leaf level, then we can get rid of this
3352 if (numrecs
== 1 && level
> 0) {
3353 union xfs_btree_ptr
*pp
;
3355 * pp is still set to the first pointer in the block.
3356 * Make it the new root of the btree.
3358 pp
= xfs_btree_ptr_addr(cur
, 1, block
);
3359 error
= cur
->bc_ops
->kill_root(cur
, bp
, level
, pp
);
3362 } else if (level
> 0) {
3363 error
= xfs_btree_dec_cursor(cur
, level
, stat
);
3372 * If we deleted the leftmost entry in the block, update the
3373 * key values above us in the tree.
3376 error
= xfs_btree_updkey(cur
, keyp
, level
+ 1);
3382 * If the number of records remaining in the block is at least
3383 * the minimum, we're done.
3385 if (numrecs
>= cur
->bc_ops
->get_minrecs(cur
, level
)) {
3386 error
= xfs_btree_dec_cursor(cur
, level
, stat
);
3393 * Otherwise, we have to move some records around to keep the
3394 * tree balanced. Look at the left and right sibling blocks to
3395 * see if we can re-balance by moving only one record.
3397 xfs_btree_get_sibling(cur
, block
, &rptr
, XFS_BB_RIGHTSIB
);
3398 xfs_btree_get_sibling(cur
, block
, &lptr
, XFS_BB_LEFTSIB
);
3400 if (cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
) {
3402 * One child of root, need to get a chance to copy its contents
3403 * into the root and delete it. Can't go up to next level,
3404 * there's nothing to delete there.
3406 if (xfs_btree_ptr_is_null(cur
, &rptr
) &&
3407 xfs_btree_ptr_is_null(cur
, &lptr
) &&
3408 level
== cur
->bc_nlevels
- 2) {
3409 error
= xfs_btree_kill_iroot(cur
);
3411 error
= xfs_btree_dec_cursor(cur
, level
, stat
);
3418 ASSERT(!xfs_btree_ptr_is_null(cur
, &rptr
) ||
3419 !xfs_btree_ptr_is_null(cur
, &lptr
));
3422 * Duplicate the cursor so our btree manipulations here won't
3423 * disrupt the next level up.
3425 error
= xfs_btree_dup_cursor(cur
, &tcur
);
3430 * If there's a right sibling, see if it's ok to shift an entry
3433 if (!xfs_btree_ptr_is_null(cur
, &rptr
)) {
3435 * Move the temp cursor to the last entry in the next block.
3436 * Actually any entry but the first would suffice.
3438 i
= xfs_btree_lastrec(tcur
, level
);
3439 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
3441 error
= xfs_btree_increment(tcur
, level
, &i
);
3444 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
3446 i
= xfs_btree_lastrec(tcur
, level
);
3447 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
3449 /* Grab a pointer to the block. */
3450 right
= xfs_btree_get_block(tcur
, level
, &rbp
);
3452 error
= xfs_btree_check_block(tcur
, right
, level
, rbp
);
3456 /* Grab the current block number, for future use. */
3457 xfs_btree_get_sibling(tcur
, right
, &cptr
, XFS_BB_LEFTSIB
);
3460 * If right block is full enough so that removing one entry
3461 * won't make it too empty, and left-shifting an entry out
3462 * of right to us works, we're done.
3464 if (xfs_btree_get_numrecs(right
) - 1 >=
3465 cur
->bc_ops
->get_minrecs(tcur
, level
)) {
3466 error
= xfs_btree_lshift(tcur
, level
, &i
);
3470 ASSERT(xfs_btree_get_numrecs(block
) >=
3471 cur
->bc_ops
->get_minrecs(tcur
, level
));
3473 xfs_btree_del_cursor(tcur
, XFS_BTREE_NOERROR
);
3476 error
= xfs_btree_dec_cursor(cur
, level
, stat
);
3484 * Otherwise, grab the number of records in right for
3485 * future reference, and fix up the temp cursor to point
3486 * to our block again (last record).
3488 rrecs
= xfs_btree_get_numrecs(right
);
3489 if (!xfs_btree_ptr_is_null(cur
, &lptr
)) {
3490 i
= xfs_btree_firstrec(tcur
, level
);
3491 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
3493 error
= xfs_btree_decrement(tcur
, level
, &i
);
3496 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
3501 * If there's a left sibling, see if it's ok to shift an entry
3504 if (!xfs_btree_ptr_is_null(cur
, &lptr
)) {
3506 * Move the temp cursor to the first entry in the
3509 i
= xfs_btree_firstrec(tcur
, level
);
3510 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
3512 error
= xfs_btree_decrement(tcur
, level
, &i
);
3515 i
= xfs_btree_firstrec(tcur
, level
);
3516 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
3518 /* Grab a pointer to the block. */
3519 left
= xfs_btree_get_block(tcur
, level
, &lbp
);
3521 error
= xfs_btree_check_block(cur
, left
, level
, lbp
);
3525 /* Grab the current block number, for future use. */
3526 xfs_btree_get_sibling(tcur
, left
, &cptr
, XFS_BB_RIGHTSIB
);
3529 * If left block is full enough so that removing one entry
3530 * won't make it too empty, and right-shifting an entry out
3531 * of left to us works, we're done.
3533 if (xfs_btree_get_numrecs(left
) - 1 >=
3534 cur
->bc_ops
->get_minrecs(tcur
, level
)) {
3535 error
= xfs_btree_rshift(tcur
, level
, &i
);
3539 ASSERT(xfs_btree_get_numrecs(block
) >=
3540 cur
->bc_ops
->get_minrecs(tcur
, level
));
3541 xfs_btree_del_cursor(tcur
, XFS_BTREE_NOERROR
);
3545 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
3552 * Otherwise, grab the number of records in right for
3555 lrecs
= xfs_btree_get_numrecs(left
);
3558 /* Delete the temp cursor, we're done with it. */
3559 xfs_btree_del_cursor(tcur
, XFS_BTREE_NOERROR
);
3562 /* If here, we need to do a join to keep the tree balanced. */
3563 ASSERT(!xfs_btree_ptr_is_null(cur
, &cptr
));
3565 if (!xfs_btree_ptr_is_null(cur
, &lptr
) &&
3566 lrecs
+ xfs_btree_get_numrecs(block
) <=
3567 cur
->bc_ops
->get_maxrecs(cur
, level
)) {
3569 * Set "right" to be the starting block,
3570 * "left" to be the left neighbor.
3575 error
= xfs_btree_read_buf_block(cur
, &lptr
, level
,
3581 * If that won't work, see if we can join with the right neighbor block.
3583 } else if (!xfs_btree_ptr_is_null(cur
, &rptr
) &&
3584 rrecs
+ xfs_btree_get_numrecs(block
) <=
3585 cur
->bc_ops
->get_maxrecs(cur
, level
)) {
3587 * Set "left" to be the starting block,
3588 * "right" to be the right neighbor.
3593 error
= xfs_btree_read_buf_block(cur
, &rptr
, level
,
3599 * Otherwise, we can't fix the imbalance.
3600 * Just return. This is probably a logic error, but it's not fatal.
3603 error
= xfs_btree_dec_cursor(cur
, level
, stat
);
3609 rrecs
= xfs_btree_get_numrecs(right
);
3610 lrecs
= xfs_btree_get_numrecs(left
);
3613 * We're now going to join "left" and "right" by moving all the stuff
3614 * in "right" to "left" and deleting "right".
3616 XFS_BTREE_STATS_ADD(cur
, moves
, rrecs
);
3618 /* It's a non-leaf. Move keys and pointers. */
3619 union xfs_btree_key
*lkp
; /* left btree key */
3620 union xfs_btree_ptr
*lpp
; /* left address pointer */
3621 union xfs_btree_key
*rkp
; /* right btree key */
3622 union xfs_btree_ptr
*rpp
; /* right address pointer */
3624 lkp
= xfs_btree_key_addr(cur
, lrecs
+ 1, left
);
3625 lpp
= xfs_btree_ptr_addr(cur
, lrecs
+ 1, left
);
3626 rkp
= xfs_btree_key_addr(cur
, 1, right
);
3627 rpp
= xfs_btree_ptr_addr(cur
, 1, right
);
3629 for (i
= 1; i
< rrecs
; i
++) {
3630 error
= xfs_btree_check_ptr(cur
, rpp
, i
, level
);
3635 xfs_btree_copy_keys(cur
, lkp
, rkp
, rrecs
);
3636 xfs_btree_copy_ptrs(cur
, lpp
, rpp
, rrecs
);
3638 xfs_btree_log_keys(cur
, lbp
, lrecs
+ 1, lrecs
+ rrecs
);
3639 xfs_btree_log_ptrs(cur
, lbp
, lrecs
+ 1, lrecs
+ rrecs
);
3641 /* It's a leaf. Move records. */
3642 union xfs_btree_rec
*lrp
; /* left record pointer */
3643 union xfs_btree_rec
*rrp
; /* right record pointer */
3645 lrp
= xfs_btree_rec_addr(cur
, lrecs
+ 1, left
);
3646 rrp
= xfs_btree_rec_addr(cur
, 1, right
);
3648 xfs_btree_copy_recs(cur
, lrp
, rrp
, rrecs
);
3649 xfs_btree_log_recs(cur
, lbp
, lrecs
+ 1, lrecs
+ rrecs
);
3652 XFS_BTREE_STATS_INC(cur
, join
);
3655 * Fix up the the number of records and right block pointer in the
3656 * surviving block, and log it.
3658 xfs_btree_set_numrecs(left
, lrecs
+ rrecs
);
3659 xfs_btree_get_sibling(cur
, right
, &cptr
, XFS_BB_RIGHTSIB
),
3660 xfs_btree_set_sibling(cur
, left
, &cptr
, XFS_BB_RIGHTSIB
);
3661 xfs_btree_log_block(cur
, lbp
, XFS_BB_NUMRECS
| XFS_BB_RIGHTSIB
);
3663 /* If there is a right sibling, point it to the remaining block. */
3664 xfs_btree_get_sibling(cur
, left
, &cptr
, XFS_BB_RIGHTSIB
);
3665 if (!xfs_btree_ptr_is_null(cur
, &cptr
)) {
3666 error
= xfs_btree_read_buf_block(cur
, &cptr
, level
,
3667 0, &rrblock
, &rrbp
);
3670 xfs_btree_set_sibling(cur
, rrblock
, &lptr
, XFS_BB_LEFTSIB
);
3671 xfs_btree_log_block(cur
, rrbp
, XFS_BB_LEFTSIB
);
3674 /* Free the deleted block. */
3675 error
= cur
->bc_ops
->free_block(cur
, rbp
);
3678 XFS_BTREE_STATS_INC(cur
, free
);
3681 * If we joined with the left neighbor, set the buffer in the
3682 * cursor to the left block, and fix up the index.
3685 cur
->bc_bufs
[level
] = lbp
;
3686 cur
->bc_ptrs
[level
] += lrecs
;
3687 cur
->bc_ra
[level
] = 0;
3690 * If we joined with the right neighbor and there's a level above
3691 * us, increment the cursor at that level.
3693 else if ((cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
) ||
3694 (level
+ 1 < cur
->bc_nlevels
)) {
3695 error
= xfs_btree_increment(cur
, level
+ 1, &i
);
3701 * Readjust the ptr at this level if it's not a leaf, since it's
3702 * still pointing at the deletion point, which makes the cursor
3703 * inconsistent. If this makes the ptr 0, the caller fixes it up.
3704 * We can't use decrement because it would change the next level up.
3707 cur
->bc_ptrs
[level
]--;
3709 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
3710 /* Return value means the next level up has something to do. */
3715 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
3717 xfs_btree_del_cursor(tcur
, XFS_BTREE_ERROR
);
3722 * Delete the record pointed to by cur.
3723 * The cursor refers to the place where the record was (could be inserted)
3724 * when the operation returns.
3728 struct xfs_btree_cur
*cur
,
3729 int *stat
) /* success/failure */
3731 int error
; /* error return value */
3735 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
3738 * Go up the tree, starting at leaf level.
3740 * If 2 is returned then a join was done; go to the next level.
3741 * Otherwise we are done.
3743 for (level
= 0, i
= 2; i
== 2; level
++) {
3744 error
= xfs_btree_delrec(cur
, level
, &i
);
3750 for (level
= 1; level
< cur
->bc_nlevels
; level
++) {
3751 if (cur
->bc_ptrs
[level
] == 0) {
3752 error
= xfs_btree_decrement(cur
, level
, &i
);
3760 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
3764 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
3769 * Get the data from the pointed-to record.
3773 struct xfs_btree_cur
*cur
, /* btree cursor */
3774 union xfs_btree_rec
**recp
, /* output: btree record */
3775 int *stat
) /* output: success/failure */
3777 struct xfs_btree_block
*block
; /* btree block */
3778 struct xfs_buf
*bp
; /* buffer pointer */
3779 int ptr
; /* record number */
3781 int error
; /* error return value */
3784 ptr
= cur
->bc_ptrs
[0];
3785 block
= xfs_btree_get_block(cur
, 0, &bp
);
3788 error
= xfs_btree_check_block(cur
, block
, 0, bp
);
3794 * Off the right end or left end, return failure.
3796 if (ptr
> xfs_btree_get_numrecs(block
) || ptr
<= 0) {
3802 * Point to the record and extract its data.
3804 *recp
= xfs_btree_rec_addr(cur
, ptr
, block
);