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
56 int /* error (0 or EFSCORRUPTED) */
57 xfs_btree_check_lblock(
58 struct xfs_btree_cur *cur, /* btree cursor */
59 struct xfs_btree_lblock *block, /* btree long form block pointer */
60 int level, /* level of the btree block */
61 struct xfs_buf *bp) /* buffer for block, if any */
63 int lblock_ok; /* block passes checks */
64 struct xfs_mount *mp; /* file system mount point */
68 be32_to_cpu(block->bb_magic) == xfs_magics[cur->bc_btnum] &&
69 be16_to_cpu(block->bb_level) == level &&
70 be16_to_cpu(block->bb_numrecs) <=
71 cur->bc_ops->get_maxrecs(cur, level) &&
73 (be64_to_cpu(block->bb_leftsib) == NULLDFSBNO ||
74 XFS_FSB_SANITY_CHECK(mp, be64_to_cpu(block->bb_leftsib))) &&
76 (be64_to_cpu(block->bb_rightsib) == NULLDFSBNO ||
77 XFS_FSB_SANITY_CHECK(mp, be64_to_cpu(block->bb_rightsib)));
78 if (unlikely(XFS_TEST_ERROR(!lblock_ok, mp,
79 XFS_ERRTAG_BTREE_CHECK_LBLOCK,
80 XFS_RANDOM_BTREE_CHECK_LBLOCK))) {
82 xfs_buftrace("LBTREE ERROR", bp);
83 XFS_ERROR_REPORT("xfs_btree_check_lblock", XFS_ERRLEVEL_LOW,
85 return XFS_ERROR(EFSCORRUPTED);
90 int /* error (0 or EFSCORRUPTED) */
91 xfs_btree_check_sblock(
92 struct xfs_btree_cur *cur, /* btree cursor */
93 struct xfs_btree_sblock *block, /* btree short form block pointer */
94 int level, /* level of the btree block */
95 struct xfs_buf *bp) /* buffer containing block */
97 struct xfs_buf *agbp; /* buffer for ag. freespace struct */
98 struct xfs_agf *agf; /* ag. freespace structure */
99 xfs_agblock_t agflen; /* native ag. freespace length */
100 int sblock_ok; /* block passes checks */
102 agbp = cur->bc_private.a.agbp;
103 agf = XFS_BUF_TO_AGF(agbp);
104 agflen = be32_to_cpu(agf->agf_length);
106 be32_to_cpu(block->bb_magic) == xfs_magics[cur->bc_btnum] &&
107 be16_to_cpu(block->bb_level) == level &&
108 be16_to_cpu(block->bb_numrecs) <=
109 cur->bc_ops->get_maxrecs(cur, level) &&
110 (be32_to_cpu(block->bb_leftsib) == NULLAGBLOCK ||
111 be32_to_cpu(block->bb_leftsib) < agflen) &&
113 (be32_to_cpu(block->bb_rightsib) == NULLAGBLOCK ||
114 be32_to_cpu(block->bb_rightsib) < agflen) &&
116 if (unlikely(XFS_TEST_ERROR(!sblock_ok, cur->bc_mp,
117 XFS_ERRTAG_BTREE_CHECK_SBLOCK,
118 XFS_RANDOM_BTREE_CHECK_SBLOCK))) {
120 xfs_buftrace("SBTREE ERROR", bp);
121 XFS_ERROR_REPORT("xfs_btree_check_sblock", XFS_ERRLEVEL_LOW,
123 return XFS_ERROR(EFSCORRUPTED);
129 * Debug routine: check that block header is ok.
132 xfs_btree_check_block(
133 struct xfs_btree_cur *cur, /* btree cursor */
134 struct xfs_btree_block *block, /* generic btree block pointer */
135 int level, /* level of the btree block */
136 struct xfs_buf *bp) /* buffer containing block, if any */
138 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
139 return xfs_btree_check_lblock(cur,
140 (struct xfs_btree_lblock *)block, level, bp);
142 return xfs_btree_check_sblock(cur,
143 (struct xfs_btree_sblock *)block, level, bp);
148 * Check that (long) pointer is ok.
150 int /* error (0 or EFSCORRUPTED) */
151 xfs_btree_check_lptr(
152 struct xfs_btree_cur *cur, /* btree cursor */
153 xfs_dfsbno_t bno, /* btree block disk address */
154 int level) /* btree block level */
156 XFS_WANT_CORRUPTED_RETURN(
159 XFS_FSB_SANITY_CHECK(cur->bc_mp, bno));
164 * Check that (short) pointer is ok.
166 int /* error (0 or EFSCORRUPTED) */
167 xfs_btree_check_sptr(
168 struct xfs_btree_cur *cur, /* btree cursor */
169 xfs_agblock_t bno, /* btree block disk address */
170 int level) /* btree block level */
172 xfs_agblock_t agblocks = cur->bc_mp->m_sb.sb_agblocks;
174 XFS_WANT_CORRUPTED_RETURN(
176 bno != NULLAGBLOCK &&
183 * Check that block ptr is ok.
185 int /* error (0 or EFSCORRUPTED) */
187 struct xfs_btree_cur *cur, /* btree cursor */
188 union xfs_btree_ptr *ptr, /* btree block disk address */
189 int index, /* offset from ptr to check */
190 int level) /* btree block level */
192 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
193 return xfs_btree_check_lptr(cur,
194 be64_to_cpu((&ptr->l)[index]), level);
196 return xfs_btree_check_sptr(cur,
197 be32_to_cpu((&ptr->s)[index]), level);
202 * Delete the btree cursor.
205 xfs_btree_del_cursor(
206 xfs_btree_cur_t *cur, /* btree cursor */
207 int error) /* del because of error */
209 int i; /* btree level */
212 * Clear the buffer pointers, and release the buffers.
213 * If we're doing this in the face of an error, we
214 * need to make sure to inspect all of the entries
215 * in the bc_bufs array for buffers to be unlocked.
216 * This is because some of the btree code works from
217 * level n down to 0, and if we get an error along
218 * the way we won't have initialized all the entries
221 for (i = 0; i < cur->bc_nlevels; i++) {
223 xfs_btree_setbuf(cur, i, NULL);
228 * Can't free a bmap cursor without having dealt with the
229 * allocated indirect blocks' accounting.
231 ASSERT(cur->bc_btnum != XFS_BTNUM_BMAP ||
232 cur->bc_private.b.allocated == 0);
236 kmem_zone_free(xfs_btree_cur_zone, cur);
240 * Duplicate the btree cursor.
241 * Allocate a new one, copy the record, re-get the buffers.
244 xfs_btree_dup_cursor(
245 xfs_btree_cur_t *cur, /* input cursor */
246 xfs_btree_cur_t **ncur) /* output cursor */
248 xfs_buf_t *bp; /* btree block's buffer pointer */
249 int error; /* error return value */
250 int i; /* level number of btree block */
251 xfs_mount_t *mp; /* mount structure for filesystem */
252 xfs_btree_cur_t *new; /* new cursor value */
253 xfs_trans_t *tp; /* transaction pointer, can be NULL */
259 * Allocate a new cursor like the old one.
261 new = cur->bc_ops->dup_cursor(cur);
264 * Copy the record currently in the cursor.
266 new->bc_rec = cur->bc_rec;
269 * For each level current, re-get the buffer and copy the ptr value.
271 for (i = 0; i < new->bc_nlevels; i++) {
272 new->bc_ptrs[i] = cur->bc_ptrs[i];
273 new->bc_ra[i] = cur->bc_ra[i];
274 if ((bp = cur->bc_bufs[i])) {
275 if ((error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp,
276 XFS_BUF_ADDR(bp), mp->m_bsize, 0, &bp))) {
277 xfs_btree_del_cursor(new, error);
281 new->bc_bufs[i] = bp;
283 ASSERT(!XFS_BUF_GETERROR(bp));
285 new->bc_bufs[i] = NULL;
292 * XFS btree block layout and addressing:
294 * There are two types of blocks in the btree: leaf and non-leaf blocks.
296 * The leaf record start with a header then followed by records containing
297 * the values. A non-leaf block also starts with the same header, and
298 * then first contains lookup keys followed by an equal number of pointers
299 * to the btree blocks at the previous level.
301 * +--------+-------+-------+-------+-------+-------+-------+
302 * Leaf: | header | rec 1 | rec 2 | rec 3 | rec 4 | rec 5 | rec N |
303 * +--------+-------+-------+-------+-------+-------+-------+
305 * +--------+-------+-------+-------+-------+-------+-------+
306 * Non-Leaf: | header | key 1 | key 2 | key N | ptr 1 | ptr 2 | ptr N |
307 * +--------+-------+-------+-------+-------+-------+-------+
309 * The header is called struct xfs_btree_block for reasons better left unknown
310 * and comes in different versions for short (32bit) and long (64bit) block
311 * pointers. The record and key structures are defined by the btree instances
312 * and opaque to the btree core. The block pointers are simple disk endian
313 * integers, available in a short (32bit) and long (64bit) variant.
315 * The helpers below calculate the offset of a given record, key or pointer
316 * into a btree block (xfs_btree_*_offset) or return a pointer to the given
317 * record, key or pointer (xfs_btree_*_addr). Note that all addressing
318 * inside the btree block is done using indices starting at one, not zero!
322 * Return size of the btree block header for this btree instance.
324 static inline size_t xfs_btree_block_len(struct xfs_btree_cur *cur)
326 return (cur->bc_flags & XFS_BTREE_LONG_PTRS) ?
327 sizeof(struct xfs_btree_lblock) :
328 sizeof(struct xfs_btree_sblock);
332 * Return size of btree block pointers for this btree instance.
334 static inline size_t xfs_btree_ptr_len(struct xfs_btree_cur *cur)
336 return (cur->bc_flags & XFS_BTREE_LONG_PTRS) ?
337 sizeof(__be64) : sizeof(__be32);
341 * Calculate offset of the n-th record in a btree block.
344 xfs_btree_rec_offset(
345 struct xfs_btree_cur *cur,
348 return xfs_btree_block_len(cur) +
349 (n - 1) * cur->bc_ops->rec_len;
353 * Calculate offset of the n-th key in a btree block.
356 xfs_btree_key_offset(
357 struct xfs_btree_cur *cur,
360 return xfs_btree_block_len(cur) +
361 (n - 1) * cur->bc_ops->key_len;
365 * Calculate offset of the n-th block pointer in a btree block.
368 xfs_btree_ptr_offset(
369 struct xfs_btree_cur *cur,
373 return xfs_btree_block_len(cur) +
374 cur->bc_ops->get_maxrecs(cur, level) * cur->bc_ops->key_len +
375 (n - 1) * xfs_btree_ptr_len(cur);
379 * Return a pointer to the n-th record in the btree block.
381 STATIC union xfs_btree_rec *
383 struct xfs_btree_cur *cur,
385 struct xfs_btree_block *block)
387 return (union xfs_btree_rec *)
388 ((char *)block + xfs_btree_rec_offset(cur, n));
392 * Return a pointer to the n-th key in the btree block.
394 STATIC union xfs_btree_key *
396 struct xfs_btree_cur *cur,
398 struct xfs_btree_block *block)
400 return (union xfs_btree_key *)
401 ((char *)block + xfs_btree_key_offset(cur, n));
405 * Return a pointer to the n-th block pointer in the btree block.
407 STATIC union xfs_btree_ptr *
409 struct xfs_btree_cur *cur,
411 struct xfs_btree_block *block)
413 int level = xfs_btree_get_level(block);
415 ASSERT(block->bb_level != 0);
417 return (union xfs_btree_ptr *)
418 ((char *)block + xfs_btree_ptr_offset(cur, n, level));
422 * Get a the root block which is stored in the inode.
424 * For now this btree implementation assumes the btree root is always
425 * stored in the if_broot field of an inode fork.
427 STATIC struct xfs_btree_block *
429 struct xfs_btree_cur *cur)
431 struct xfs_ifork *ifp;
433 ifp = XFS_IFORK_PTR(cur->bc_private.b.ip, cur->bc_private.b.whichfork);
434 return (struct xfs_btree_block *)ifp->if_broot;
438 * Retrieve the block pointer from the cursor at the given level.
439 * This may be an inode btree root or from a buffer.
441 STATIC struct xfs_btree_block * /* generic btree block pointer */
443 struct xfs_btree_cur *cur, /* btree cursor */
444 int level, /* level in btree */
445 struct xfs_buf **bpp) /* buffer containing the block */
447 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
448 (level == cur->bc_nlevels - 1)) {
450 return xfs_btree_get_iroot(cur);
453 *bpp = cur->bc_bufs[level];
454 return XFS_BUF_TO_BLOCK(*bpp);
458 * Get a buffer for the block, return it with no data read.
459 * Long-form addressing.
461 xfs_buf_t * /* buffer for fsbno */
463 xfs_mount_t *mp, /* file system mount point */
464 xfs_trans_t *tp, /* transaction pointer */
465 xfs_fsblock_t fsbno, /* file system block number */
466 uint lock) /* lock flags for get_buf */
468 xfs_buf_t *bp; /* buffer pointer (return value) */
469 xfs_daddr_t d; /* real disk block address */
471 ASSERT(fsbno != NULLFSBLOCK);
472 d = XFS_FSB_TO_DADDR(mp, fsbno);
473 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d, mp->m_bsize, lock);
475 ASSERT(!XFS_BUF_GETERROR(bp));
480 * Get a buffer for the block, return it with no data read.
481 * Short-form addressing.
483 xfs_buf_t * /* buffer for agno/agbno */
485 xfs_mount_t *mp, /* file system mount point */
486 xfs_trans_t *tp, /* transaction pointer */
487 xfs_agnumber_t agno, /* allocation group number */
488 xfs_agblock_t agbno, /* allocation group block number */
489 uint lock) /* lock flags for get_buf */
491 xfs_buf_t *bp; /* buffer pointer (return value) */
492 xfs_daddr_t d; /* real disk block address */
494 ASSERT(agno != NULLAGNUMBER);
495 ASSERT(agbno != NULLAGBLOCK);
496 d = XFS_AGB_TO_DADDR(mp, agno, agbno);
497 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d, mp->m_bsize, lock);
499 ASSERT(!XFS_BUF_GETERROR(bp));
504 * Check for the cursor referring to the last block at the given level.
506 int /* 1=is last block, 0=not last block */
507 xfs_btree_islastblock(
508 xfs_btree_cur_t *cur, /* btree cursor */
509 int level) /* level to check */
511 xfs_btree_block_t *block; /* generic btree block pointer */
512 xfs_buf_t *bp; /* buffer containing block */
514 block = xfs_btree_get_block(cur, level, &bp);
515 xfs_btree_check_block(cur, block, level, bp);
516 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
517 return be64_to_cpu(block->bb_u.l.bb_rightsib) == NULLDFSBNO;
519 return be32_to_cpu(block->bb_u.s.bb_rightsib) == NULLAGBLOCK;
523 * Change the cursor to point to the first record at the given level.
524 * Other levels are unaffected.
526 int /* success=1, failure=0 */
528 xfs_btree_cur_t *cur, /* btree cursor */
529 int level) /* level to change */
531 xfs_btree_block_t *block; /* generic btree block pointer */
532 xfs_buf_t *bp; /* buffer containing block */
535 * Get the block pointer for this level.
537 block = xfs_btree_get_block(cur, level, &bp);
538 xfs_btree_check_block(cur, block, level, bp);
540 * It's empty, there is no such record.
542 if (!block->bb_numrecs)
545 * Set the ptr value to 1, that's the first record/key.
547 cur->bc_ptrs[level] = 1;
552 * Change the cursor to point to the last record in the current block
553 * at the given level. Other levels are unaffected.
555 int /* success=1, failure=0 */
557 xfs_btree_cur_t *cur, /* btree cursor */
558 int level) /* level to change */
560 xfs_btree_block_t *block; /* generic btree block pointer */
561 xfs_buf_t *bp; /* buffer containing block */
564 * Get the block pointer for this level.
566 block = xfs_btree_get_block(cur, level, &bp);
567 xfs_btree_check_block(cur, block, level, bp);
569 * It's empty, there is no such record.
571 if (!block->bb_numrecs)
574 * Set the ptr value to numrecs, that's the last record/key.
576 cur->bc_ptrs[level] = be16_to_cpu(block->bb_numrecs);
581 * Compute first and last byte offsets for the fields given.
582 * Interprets the offsets table, which contains struct field offsets.
586 __int64_t fields, /* bitmask of fields */
587 const short *offsets, /* table of field offsets */
588 int nbits, /* number of bits to inspect */
589 int *first, /* output: first byte offset */
590 int *last) /* output: last byte offset */
592 int i; /* current bit number */
593 __int64_t imask; /* mask for current bit number */
597 * Find the lowest bit, so the first byte offset.
599 for (i = 0, imask = 1LL; ; i++, imask <<= 1) {
600 if (imask & fields) {
606 * Find the highest bit, so the last byte offset.
608 for (i = nbits - 1, imask = 1LL << i; ; i--, imask >>= 1) {
609 if (imask & fields) {
610 *last = offsets[i + 1] - 1;
617 * Get a buffer for the block, return it read in.
618 * Long-form addressing.
622 xfs_mount_t *mp, /* file system mount point */
623 xfs_trans_t *tp, /* transaction pointer */
624 xfs_fsblock_t fsbno, /* file system block number */
625 uint lock, /* lock flags for read_buf */
626 xfs_buf_t **bpp, /* buffer for fsbno */
627 int refval) /* ref count value for buffer */
629 xfs_buf_t *bp; /* return value */
630 xfs_daddr_t d; /* real disk block address */
633 ASSERT(fsbno != NULLFSBLOCK);
634 d = XFS_FSB_TO_DADDR(mp, fsbno);
635 if ((error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, d,
636 mp->m_bsize, lock, &bp))) {
639 ASSERT(!bp || !XFS_BUF_GETERROR(bp));
641 XFS_BUF_SET_VTYPE_REF(bp, B_FS_MAP, refval);
648 * Get a buffer for the block, return it read in.
649 * Short-form addressing.
653 xfs_mount_t *mp, /* file system mount point */
654 xfs_trans_t *tp, /* transaction pointer */
655 xfs_agnumber_t agno, /* allocation group number */
656 xfs_agblock_t agbno, /* allocation group block number */
657 uint lock, /* lock flags for read_buf */
658 xfs_buf_t **bpp, /* buffer for agno/agbno */
659 int refval) /* ref count value for buffer */
661 xfs_buf_t *bp; /* return value */
662 xfs_daddr_t d; /* real disk block address */
665 ASSERT(agno != NULLAGNUMBER);
666 ASSERT(agbno != NULLAGBLOCK);
667 d = XFS_AGB_TO_DADDR(mp, agno, agbno);
668 if ((error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, d,
669 mp->m_bsize, lock, &bp))) {
672 ASSERT(!bp || !XFS_BUF_GETERROR(bp));
675 case XFS_ALLOC_BTREE_REF:
676 XFS_BUF_SET_VTYPE_REF(bp, B_FS_MAP, refval);
678 case XFS_INO_BTREE_REF:
679 XFS_BUF_SET_VTYPE_REF(bp, B_FS_INOMAP, refval);
688 * Read-ahead the block, don't wait for it, don't return a buffer.
689 * Long-form addressing.
693 xfs_btree_reada_bufl(
694 xfs_mount_t *mp, /* file system mount point */
695 xfs_fsblock_t fsbno, /* file system block number */
696 xfs_extlen_t count) /* count of filesystem blocks */
700 ASSERT(fsbno != NULLFSBLOCK);
701 d = XFS_FSB_TO_DADDR(mp, fsbno);
702 xfs_baread(mp->m_ddev_targp, d, mp->m_bsize * count);
706 * Read-ahead the block, don't wait for it, don't return a buffer.
707 * Short-form addressing.
711 xfs_btree_reada_bufs(
712 xfs_mount_t *mp, /* file system mount point */
713 xfs_agnumber_t agno, /* allocation group number */
714 xfs_agblock_t agbno, /* allocation group block number */
715 xfs_extlen_t count) /* count of filesystem blocks */
719 ASSERT(agno != NULLAGNUMBER);
720 ASSERT(agbno != NULLAGBLOCK);
721 d = XFS_AGB_TO_DADDR(mp, agno, agbno);
722 xfs_baread(mp->m_ddev_targp, d, mp->m_bsize * count);
726 xfs_btree_readahead_lblock(
727 struct xfs_btree_cur *cur,
729 struct xfs_btree_block *block)
732 xfs_fsblock_t left = be64_to_cpu(block->bb_u.l.bb_leftsib);
733 xfs_fsblock_t right = be64_to_cpu(block->bb_u.l.bb_rightsib);
735 if ((lr & XFS_BTCUR_LEFTRA) && left != NULLDFSBNO) {
736 xfs_btree_reada_bufl(cur->bc_mp, left, 1);
740 if ((lr & XFS_BTCUR_RIGHTRA) && right != NULLDFSBNO) {
741 xfs_btree_reada_bufl(cur->bc_mp, right, 1);
749 xfs_btree_readahead_sblock(
750 struct xfs_btree_cur *cur,
752 struct xfs_btree_block *block)
755 xfs_agblock_t left = be32_to_cpu(block->bb_u.s.bb_leftsib);
756 xfs_agblock_t right = be32_to_cpu(block->bb_u.s.bb_rightsib);
759 if ((lr & XFS_BTCUR_LEFTRA) && left != NULLAGBLOCK) {
760 xfs_btree_reada_bufs(cur->bc_mp, cur->bc_private.a.agno,
765 if ((lr & XFS_BTCUR_RIGHTRA) && right != NULLAGBLOCK) {
766 xfs_btree_reada_bufs(cur->bc_mp, cur->bc_private.a.agno,
775 * Read-ahead btree blocks, at the given level.
776 * Bits in lr are set from XFS_BTCUR_{LEFT,RIGHT}RA.
780 struct xfs_btree_cur *cur, /* btree cursor */
781 int lev, /* level in btree */
782 int lr) /* left/right bits */
784 struct xfs_btree_block *block;
787 * No readahead needed if we are at the root level and the
788 * btree root is stored in the inode.
790 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
791 (lev == cur->bc_nlevels - 1))
794 if ((cur->bc_ra[lev] | lr) == cur->bc_ra[lev])
797 cur->bc_ra[lev] |= lr;
798 block = XFS_BUF_TO_BLOCK(cur->bc_bufs[lev]);
800 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
801 return xfs_btree_readahead_lblock(cur, lr, block);
802 return xfs_btree_readahead_sblock(cur, lr, block);
806 * Set the buffer for level "lev" in the cursor to bp, releasing
807 * any previous buffer.
811 xfs_btree_cur_t *cur, /* btree cursor */
812 int lev, /* level in btree */
813 xfs_buf_t *bp) /* new buffer to set */
815 xfs_btree_block_t *b; /* btree block */
816 xfs_buf_t *obp; /* old buffer pointer */
818 obp = cur->bc_bufs[lev];
820 xfs_trans_brelse(cur->bc_tp, obp);
821 cur->bc_bufs[lev] = bp;
825 b = XFS_BUF_TO_BLOCK(bp);
826 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
827 if (be64_to_cpu(b->bb_u.l.bb_leftsib) == NULLDFSBNO)
828 cur->bc_ra[lev] |= XFS_BTCUR_LEFTRA;
829 if (be64_to_cpu(b->bb_u.l.bb_rightsib) == NULLDFSBNO)
830 cur->bc_ra[lev] |= XFS_BTCUR_RIGHTRA;
832 if (be32_to_cpu(b->bb_u.s.bb_leftsib) == NULLAGBLOCK)
833 cur->bc_ra[lev] |= XFS_BTCUR_LEFTRA;
834 if (be32_to_cpu(b->bb_u.s.bb_rightsib) == NULLAGBLOCK)
835 cur->bc_ra[lev] |= XFS_BTCUR_RIGHTRA;
840 xfs_btree_ptr_is_null(
841 struct xfs_btree_cur *cur,
842 union xfs_btree_ptr *ptr)
844 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
845 return be64_to_cpu(ptr->l) == NULLFSBLOCK;
847 return be32_to_cpu(ptr->s) == NULLAGBLOCK;
851 xfs_btree_set_ptr_null(
852 struct xfs_btree_cur *cur,
853 union xfs_btree_ptr *ptr)
855 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
856 ptr->l = cpu_to_be64(NULLFSBLOCK);
858 ptr->s = cpu_to_be32(NULLAGBLOCK);
862 * Get/set/init sibling pointers
865 xfs_btree_get_sibling(
866 struct xfs_btree_cur *cur,
867 struct xfs_btree_block *block,
868 union xfs_btree_ptr *ptr,
871 ASSERT(lr == XFS_BB_LEFTSIB || lr == XFS_BB_RIGHTSIB);
873 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
874 if (lr == XFS_BB_RIGHTSIB)
875 ptr->l = block->bb_u.l.bb_rightsib;
877 ptr->l = block->bb_u.l.bb_leftsib;
879 if (lr == XFS_BB_RIGHTSIB)
880 ptr->s = block->bb_u.s.bb_rightsib;
882 ptr->s = block->bb_u.s.bb_leftsib;
887 xfs_btree_set_sibling(
888 struct xfs_btree_cur *cur,
889 struct xfs_btree_block *block,
890 union xfs_btree_ptr *ptr,
893 ASSERT(lr == XFS_BB_LEFTSIB || lr == XFS_BB_RIGHTSIB);
895 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
896 if (lr == XFS_BB_RIGHTSIB)
897 block->bb_u.l.bb_rightsib = ptr->l;
899 block->bb_u.l.bb_leftsib = ptr->l;
901 if (lr == XFS_BB_RIGHTSIB)
902 block->bb_u.s.bb_rightsib = ptr->s;
904 block->bb_u.s.bb_leftsib = ptr->s;
909 xfs_btree_init_block(
910 struct xfs_btree_cur *cur,
913 struct xfs_btree_block *new) /* new block */
915 new->bb_magic = cpu_to_be32(xfs_magics[cur->bc_btnum]);
916 new->bb_level = cpu_to_be16(level);
917 new->bb_numrecs = cpu_to_be16(numrecs);
919 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
920 new->bb_u.l.bb_leftsib = cpu_to_be64(NULLFSBLOCK);
921 new->bb_u.l.bb_rightsib = cpu_to_be64(NULLFSBLOCK);
923 new->bb_u.s.bb_leftsib = cpu_to_be32(NULLAGBLOCK);
924 new->bb_u.s.bb_rightsib = cpu_to_be32(NULLAGBLOCK);
929 * Return true if ptr is the last record in the btree and
930 * we need to track updateѕ to this record. The decision
931 * will be further refined in the update_lastrec method.
934 xfs_btree_is_lastrec(
935 struct xfs_btree_cur *cur,
936 struct xfs_btree_block *block,
939 union xfs_btree_ptr ptr;
943 if (!(cur->bc_flags & XFS_BTREE_LASTREC_UPDATE))
946 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_RIGHTSIB);
947 if (!xfs_btree_ptr_is_null(cur, &ptr))
953 xfs_btree_buf_to_ptr(
954 struct xfs_btree_cur *cur,
956 union xfs_btree_ptr *ptr)
958 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
959 ptr->l = cpu_to_be64(XFS_DADDR_TO_FSB(cur->bc_mp,
962 ptr->s = cpu_to_be32(XFS_DADDR_TO_AGBNO(cur->bc_mp,
968 xfs_btree_ptr_to_daddr(
969 struct xfs_btree_cur *cur,
970 union xfs_btree_ptr *ptr)
972 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
973 ASSERT(be64_to_cpu(ptr->l) != NULLFSBLOCK);
975 return XFS_FSB_TO_DADDR(cur->bc_mp, be64_to_cpu(ptr->l));
977 ASSERT(cur->bc_private.a.agno != NULLAGNUMBER);
978 ASSERT(be32_to_cpu(ptr->s) != NULLAGBLOCK);
980 return XFS_AGB_TO_DADDR(cur->bc_mp, cur->bc_private.a.agno,
981 be32_to_cpu(ptr->s));
987 struct xfs_btree_cur *cur,
990 switch (cur->bc_btnum) {
993 XFS_BUF_SET_VTYPE_REF(*bpp, B_FS_MAP, XFS_ALLOC_BTREE_REF);
996 XFS_BUF_SET_VTYPE_REF(*bpp, B_FS_INOMAP, XFS_INO_BTREE_REF);
999 XFS_BUF_SET_VTYPE_REF(*bpp, B_FS_MAP, XFS_BMAP_BTREE_REF);
1007 xfs_btree_get_buf_block(
1008 struct xfs_btree_cur *cur,
1009 union xfs_btree_ptr *ptr,
1011 struct xfs_btree_block **block,
1012 struct xfs_buf **bpp)
1014 struct xfs_mount *mp = cur->bc_mp;
1017 /* need to sort out how callers deal with failures first */
1018 ASSERT(!(flags & XFS_BUF_TRYLOCK));
1020 d = xfs_btree_ptr_to_daddr(cur, ptr);
1021 *bpp = xfs_trans_get_buf(cur->bc_tp, mp->m_ddev_targp, d,
1022 mp->m_bsize, flags);
1025 ASSERT(!XFS_BUF_GETERROR(*bpp));
1027 *block = XFS_BUF_TO_BLOCK(*bpp);
1032 * Read in the buffer at the given ptr and return the buffer and
1033 * the block pointer within the buffer.
1036 xfs_btree_read_buf_block(
1037 struct xfs_btree_cur *cur,
1038 union xfs_btree_ptr *ptr,
1041 struct xfs_btree_block **block,
1042 struct xfs_buf **bpp)
1044 struct xfs_mount *mp = cur->bc_mp;
1048 /* need to sort out how callers deal with failures first */
1049 ASSERT(!(flags & XFS_BUF_TRYLOCK));
1051 d = xfs_btree_ptr_to_daddr(cur, ptr);
1052 error = xfs_trans_read_buf(mp, cur->bc_tp, mp->m_ddev_targp, d,
1053 mp->m_bsize, flags, bpp);
1057 ASSERT(*bpp != NULL);
1058 ASSERT(!XFS_BUF_GETERROR(*bpp));
1060 xfs_btree_set_refs(cur, *bpp);
1061 *block = XFS_BUF_TO_BLOCK(*bpp);
1063 error = xfs_btree_check_block(cur, *block, level, *bpp);
1065 xfs_trans_brelse(cur->bc_tp, *bpp);
1070 * Copy keys from one btree block to another.
1073 xfs_btree_copy_keys(
1074 struct xfs_btree_cur *cur,
1075 union xfs_btree_key *dst_key,
1076 union xfs_btree_key *src_key,
1079 ASSERT(numkeys >= 0);
1080 memcpy(dst_key, src_key, numkeys * cur->bc_ops->key_len);
1084 * Copy records from one btree block to another.
1087 xfs_btree_copy_recs(
1088 struct xfs_btree_cur *cur,
1089 union xfs_btree_rec *dst_rec,
1090 union xfs_btree_rec *src_rec,
1093 ASSERT(numrecs >= 0);
1094 memcpy(dst_rec, src_rec, numrecs * cur->bc_ops->rec_len);
1098 * Copy block pointers from one btree block to another.
1101 xfs_btree_copy_ptrs(
1102 struct xfs_btree_cur *cur,
1103 union xfs_btree_ptr *dst_ptr,
1104 union xfs_btree_ptr *src_ptr,
1107 ASSERT(numptrs >= 0);
1108 memcpy(dst_ptr, src_ptr, numptrs * xfs_btree_ptr_len(cur));
1112 * Shift keys one index left/right inside a single btree block.
1115 xfs_btree_shift_keys(
1116 struct xfs_btree_cur *cur,
1117 union xfs_btree_key *key,
1123 ASSERT(numkeys >= 0);
1124 ASSERT(dir == 1 || dir == -1);
1126 dst_key = (char *)key + (dir * cur->bc_ops->key_len);
1127 memmove(dst_key, key, numkeys * cur->bc_ops->key_len);
1131 * Shift records one index left/right inside a single btree block.
1134 xfs_btree_shift_recs(
1135 struct xfs_btree_cur *cur,
1136 union xfs_btree_rec *rec,
1142 ASSERT(numrecs >= 0);
1143 ASSERT(dir == 1 || dir == -1);
1145 dst_rec = (char *)rec + (dir * cur->bc_ops->rec_len);
1146 memmove(dst_rec, rec, numrecs * cur->bc_ops->rec_len);
1150 * Shift block pointers one index left/right inside a single btree block.
1153 xfs_btree_shift_ptrs(
1154 struct xfs_btree_cur *cur,
1155 union xfs_btree_ptr *ptr,
1161 ASSERT(numptrs >= 0);
1162 ASSERT(dir == 1 || dir == -1);
1164 dst_ptr = (char *)ptr + (dir * xfs_btree_ptr_len(cur));
1165 memmove(dst_ptr, ptr, numptrs * xfs_btree_ptr_len(cur));
1169 * Log key values from the btree block.
1173 struct xfs_btree_cur *cur,
1178 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1179 XFS_BTREE_TRACE_ARGBII(cur, bp, first, last);
1182 xfs_trans_log_buf(cur->bc_tp, bp,
1183 xfs_btree_key_offset(cur, first),
1184 xfs_btree_key_offset(cur, last + 1) - 1);
1186 xfs_trans_log_inode(cur->bc_tp, cur->bc_private.b.ip,
1187 xfs_ilog_fbroot(cur->bc_private.b.whichfork));
1190 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1194 * Log record values from the btree block.
1198 struct xfs_btree_cur *cur,
1203 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1204 XFS_BTREE_TRACE_ARGBII(cur, bp, first, last);
1206 xfs_trans_log_buf(cur->bc_tp, bp,
1207 xfs_btree_rec_offset(cur, first),
1208 xfs_btree_rec_offset(cur, last + 1) - 1);
1210 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1214 * Log block pointer fields from a btree block (nonleaf).
1218 struct xfs_btree_cur *cur, /* btree cursor */
1219 struct xfs_buf *bp, /* buffer containing btree block */
1220 int first, /* index of first pointer to log */
1221 int last) /* index of last pointer to log */
1223 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1224 XFS_BTREE_TRACE_ARGBII(cur, bp, first, last);
1227 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
1228 int level = xfs_btree_get_level(block);
1230 xfs_trans_log_buf(cur->bc_tp, bp,
1231 xfs_btree_ptr_offset(cur, first, level),
1232 xfs_btree_ptr_offset(cur, last + 1, level) - 1);
1234 xfs_trans_log_inode(cur->bc_tp, cur->bc_private.b.ip,
1235 xfs_ilog_fbroot(cur->bc_private.b.whichfork));
1238 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1242 * Log fields from a btree block header.
1245 xfs_btree_log_block(
1246 struct xfs_btree_cur *cur, /* btree cursor */
1247 struct xfs_buf *bp, /* buffer containing btree block */
1248 int fields) /* mask of fields: XFS_BB_... */
1250 int first; /* first byte offset logged */
1251 int last; /* last byte offset logged */
1252 static const short soffsets[] = { /* table of offsets (short) */
1253 offsetof(struct xfs_btree_sblock, bb_magic),
1254 offsetof(struct xfs_btree_sblock, bb_level),
1255 offsetof(struct xfs_btree_sblock, bb_numrecs),
1256 offsetof(struct xfs_btree_sblock, bb_leftsib),
1257 offsetof(struct xfs_btree_sblock, bb_rightsib),
1258 sizeof(struct xfs_btree_sblock)
1260 static const short loffsets[] = { /* table of offsets (long) */
1261 offsetof(struct xfs_btree_lblock, bb_magic),
1262 offsetof(struct xfs_btree_lblock, bb_level),
1263 offsetof(struct xfs_btree_lblock, bb_numrecs),
1264 offsetof(struct xfs_btree_lblock, bb_leftsib),
1265 offsetof(struct xfs_btree_lblock, bb_rightsib),
1266 sizeof(struct xfs_btree_lblock)
1269 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1270 XFS_BTREE_TRACE_ARGBI(cur, bp, fields);
1273 xfs_btree_offsets(fields,
1274 (cur->bc_flags & XFS_BTREE_LONG_PTRS) ?
1275 loffsets : soffsets,
1276 XFS_BB_NUM_BITS, &first, &last);
1277 xfs_trans_log_buf(cur->bc_tp, bp, first, last);
1279 xfs_trans_log_inode(cur->bc_tp, cur->bc_private.b.ip,
1280 xfs_ilog_fbroot(cur->bc_private.b.whichfork));
1283 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1287 * Increment cursor by one record at the level.
1288 * For nonzero levels the leaf-ward information is untouched.
1291 xfs_btree_increment(
1292 struct xfs_btree_cur *cur,
1294 int *stat) /* success/failure */
1296 struct xfs_btree_block *block;
1297 union xfs_btree_ptr ptr;
1299 int error; /* error return value */
1302 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1303 XFS_BTREE_TRACE_ARGI(cur, level);
1305 ASSERT(level < cur->bc_nlevels);
1307 /* Read-ahead to the right at this level. */
1308 xfs_btree_readahead(cur, level, XFS_BTCUR_RIGHTRA);
1310 /* Get a pointer to the btree block. */
1311 block = xfs_btree_get_block(cur, level, &bp);
1314 error = xfs_btree_check_block(cur, block, level, bp);
1319 /* We're done if we remain in the block after the increment. */
1320 if (++cur->bc_ptrs[level] <= xfs_btree_get_numrecs(block))
1323 /* Fail if we just went off the right edge of the tree. */
1324 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_RIGHTSIB);
1325 if (xfs_btree_ptr_is_null(cur, &ptr))
1328 XFS_BTREE_STATS_INC(cur, increment);
1331 * March up the tree incrementing pointers.
1332 * Stop when we don't go off the right edge of a block.
1334 for (lev = level + 1; lev < cur->bc_nlevels; lev++) {
1335 block = xfs_btree_get_block(cur, lev, &bp);
1338 error = xfs_btree_check_block(cur, block, lev, bp);
1343 if (++cur->bc_ptrs[lev] <= xfs_btree_get_numrecs(block))
1346 /* Read-ahead the right block for the next loop. */
1347 xfs_btree_readahead(cur, lev, XFS_BTCUR_RIGHTRA);
1351 * If we went off the root then we are either seriously
1352 * confused or have the tree root in an inode.
1354 if (lev == cur->bc_nlevels) {
1355 if (cur->bc_flags & XFS_BTREE_ROOT_IN_INODE)
1358 error = EFSCORRUPTED;
1361 ASSERT(lev < cur->bc_nlevels);
1364 * Now walk back down the tree, fixing up the cursor's buffer
1365 * pointers and key numbers.
1367 for (block = xfs_btree_get_block(cur, lev, &bp); lev > level; ) {
1368 union xfs_btree_ptr *ptrp;
1370 ptrp = xfs_btree_ptr_addr(cur, cur->bc_ptrs[lev], block);
1371 error = xfs_btree_read_buf_block(cur, ptrp, --lev,
1376 xfs_btree_setbuf(cur, lev, bp);
1377 cur->bc_ptrs[lev] = 1;
1380 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1385 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1390 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
1395 * Decrement cursor by one record at the level.
1396 * For nonzero levels the leaf-ward information is untouched.
1399 xfs_btree_decrement(
1400 struct xfs_btree_cur *cur,
1402 int *stat) /* success/failure */
1404 struct xfs_btree_block *block;
1406 int error; /* error return value */
1408 union xfs_btree_ptr ptr;
1410 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1411 XFS_BTREE_TRACE_ARGI(cur, level);
1413 ASSERT(level < cur->bc_nlevels);
1415 /* Read-ahead to the left at this level. */
1416 xfs_btree_readahead(cur, level, XFS_BTCUR_LEFTRA);
1418 /* We're done if we remain in the block after the decrement. */
1419 if (--cur->bc_ptrs[level] > 0)
1422 /* Get a pointer to the btree block. */
1423 block = xfs_btree_get_block(cur, level, &bp);
1426 error = xfs_btree_check_block(cur, block, level, bp);
1431 /* Fail if we just went off the left edge of the tree. */
1432 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_LEFTSIB);
1433 if (xfs_btree_ptr_is_null(cur, &ptr))
1436 XFS_BTREE_STATS_INC(cur, decrement);
1439 * March up the tree decrementing pointers.
1440 * Stop when we don't go off the left edge of a block.
1442 for (lev = level + 1; lev < cur->bc_nlevels; lev++) {
1443 if (--cur->bc_ptrs[lev] > 0)
1445 /* Read-ahead the left block for the next loop. */
1446 xfs_btree_readahead(cur, lev, XFS_BTCUR_LEFTRA);
1450 * If we went off the root then we are seriously confused.
1451 * or the root of the tree is in an inode.
1453 if (lev == cur->bc_nlevels) {
1454 if (cur->bc_flags & XFS_BTREE_ROOT_IN_INODE)
1457 error = EFSCORRUPTED;
1460 ASSERT(lev < cur->bc_nlevels);
1463 * Now walk back down the tree, fixing up the cursor's buffer
1464 * pointers and key numbers.
1466 for (block = xfs_btree_get_block(cur, lev, &bp); lev > level; ) {
1467 union xfs_btree_ptr *ptrp;
1469 ptrp = xfs_btree_ptr_addr(cur, cur->bc_ptrs[lev], block);
1470 error = xfs_btree_read_buf_block(cur, ptrp, --lev,
1474 xfs_btree_setbuf(cur, lev, bp);
1475 cur->bc_ptrs[lev] = xfs_btree_get_numrecs(block);
1478 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1483 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1488 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
1493 xfs_btree_lookup_get_block(
1494 struct xfs_btree_cur *cur, /* btree cursor */
1495 int level, /* level in the btree */
1496 union xfs_btree_ptr *pp, /* ptr to btree block */
1497 struct xfs_btree_block **blkp) /* return btree block */
1499 struct xfs_buf *bp; /* buffer pointer for btree block */
1502 /* special case the root block if in an inode */
1503 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
1504 (level == cur->bc_nlevels - 1)) {
1505 *blkp = xfs_btree_get_iroot(cur);
1510 * If the old buffer at this level for the disk address we are
1511 * looking for re-use it.
1513 * Otherwise throw it away and get a new one.
1515 bp = cur->bc_bufs[level];
1516 if (bp && XFS_BUF_ADDR(bp) == xfs_btree_ptr_to_daddr(cur, pp)) {
1517 *blkp = XFS_BUF_TO_BLOCK(bp);
1521 error = xfs_btree_read_buf_block(cur, pp, level, 0, blkp, &bp);
1525 xfs_btree_setbuf(cur, level, bp);
1530 * Get current search key. For level 0 we don't actually have a key
1531 * structure so we make one up from the record. For all other levels
1532 * we just return the right key.
1534 STATIC union xfs_btree_key *
1535 xfs_lookup_get_search_key(
1536 struct xfs_btree_cur *cur,
1539 struct xfs_btree_block *block,
1540 union xfs_btree_key *kp)
1543 cur->bc_ops->init_key_from_rec(kp,
1544 xfs_btree_rec_addr(cur, keyno, block));
1548 return xfs_btree_key_addr(cur, keyno, block);
1552 * Lookup the record. The cursor is made to point to it, based on dir.
1553 * Return 0 if can't find any such record, 1 for success.
1557 struct xfs_btree_cur *cur, /* btree cursor */
1558 xfs_lookup_t dir, /* <=, ==, or >= */
1559 int *stat) /* success/failure */
1561 struct xfs_btree_block *block; /* current btree block */
1562 __int64_t diff; /* difference for the current key */
1563 int error; /* error return value */
1564 int keyno; /* current key number */
1565 int level; /* level in the btree */
1566 union xfs_btree_ptr *pp; /* ptr to btree block */
1567 union xfs_btree_ptr ptr; /* ptr to btree block */
1569 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1570 XFS_BTREE_TRACE_ARGI(cur, dir);
1572 XFS_BTREE_STATS_INC(cur, lookup);
1577 /* initialise start pointer from cursor */
1578 cur->bc_ops->init_ptr_from_cur(cur, &ptr);
1582 * Iterate over each level in the btree, starting at the root.
1583 * For each level above the leaves, find the key we need, based
1584 * on the lookup record, then follow the corresponding block
1585 * pointer down to the next level.
1587 for (level = cur->bc_nlevels - 1, diff = 1; level >= 0; level--) {
1588 /* Get the block we need to do the lookup on. */
1589 error = xfs_btree_lookup_get_block(cur, level, pp, &block);
1595 * If we already had a key match at a higher level, we
1596 * know we need to use the first entry in this block.
1600 /* Otherwise search this block. Do a binary search. */
1602 int high; /* high entry number */
1603 int low; /* low entry number */
1605 /* Set low and high entry numbers, 1-based. */
1607 high = xfs_btree_get_numrecs(block);
1609 /* Block is empty, must be an empty leaf. */
1610 ASSERT(level == 0 && cur->bc_nlevels == 1);
1612 cur->bc_ptrs[0] = dir != XFS_LOOKUP_LE;
1613 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1618 /* Binary search the block. */
1619 while (low <= high) {
1620 union xfs_btree_key key;
1621 union xfs_btree_key *kp;
1623 XFS_BTREE_STATS_INC(cur, compare);
1625 /* keyno is average of low and high. */
1626 keyno = (low + high) >> 1;
1628 /* Get current search key */
1629 kp = xfs_lookup_get_search_key(cur, level,
1630 keyno, block, &key);
1633 * Compute difference to get next direction:
1634 * - less than, move right
1635 * - greater than, move left
1636 * - equal, we're done
1638 diff = cur->bc_ops->key_diff(cur, kp);
1649 * If there are more levels, set up for the next level
1650 * by getting the block number and filling in the cursor.
1654 * If we moved left, need the previous key number,
1655 * unless there isn't one.
1657 if (diff > 0 && --keyno < 1)
1659 pp = xfs_btree_ptr_addr(cur, keyno, block);
1662 error = xfs_btree_check_ptr(cur, pp, 0, level);
1666 cur->bc_ptrs[level] = keyno;
1670 /* Done with the search. See if we need to adjust the results. */
1671 if (dir != XFS_LOOKUP_LE && diff < 0) {
1674 * If ge search and we went off the end of the block, but it's
1675 * not the last block, we're in the wrong block.
1677 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_RIGHTSIB);
1678 if (dir == XFS_LOOKUP_GE &&
1679 keyno > xfs_btree_get_numrecs(block) &&
1680 !xfs_btree_ptr_is_null(cur, &ptr)) {
1683 cur->bc_ptrs[0] = keyno;
1684 error = xfs_btree_increment(cur, 0, &i);
1687 XFS_WANT_CORRUPTED_RETURN(i == 1);
1688 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1692 } else if (dir == XFS_LOOKUP_LE && diff > 0)
1694 cur->bc_ptrs[0] = keyno;
1696 /* Return if we succeeded or not. */
1697 if (keyno == 0 || keyno > xfs_btree_get_numrecs(block))
1699 else if (dir != XFS_LOOKUP_EQ || diff == 0)
1703 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1707 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
1712 * Update keys at all levels from here to the root along the cursor's path.
1716 struct xfs_btree_cur *cur,
1717 union xfs_btree_key *keyp,
1720 struct xfs_btree_block *block;
1722 union xfs_btree_key *kp;
1725 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1726 XFS_BTREE_TRACE_ARGIK(cur, level, keyp);
1728 ASSERT(!(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) || level >= 1);
1731 * Go up the tree from this level toward the root.
1732 * At each level, update the key value to the value input.
1733 * Stop when we reach a level where the cursor isn't pointing
1734 * at the first entry in the block.
1736 for (ptr = 1; ptr == 1 && level < cur->bc_nlevels; level++) {
1740 block = xfs_btree_get_block(cur, level, &bp);
1742 error = xfs_btree_check_block(cur, block, level, bp);
1744 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
1748 ptr = cur->bc_ptrs[level];
1749 kp = xfs_btree_key_addr(cur, ptr, block);
1750 xfs_btree_copy_keys(cur, kp, keyp, 1);
1751 xfs_btree_log_keys(cur, bp, ptr, ptr);
1754 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1759 * Update the record referred to by cur to the value in the
1760 * given record. This either works (return 0) or gets an
1761 * EFSCORRUPTED error.
1765 struct xfs_btree_cur *cur,
1766 union xfs_btree_rec *rec)
1768 struct xfs_btree_block *block;
1772 union xfs_btree_rec *rp;
1774 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1775 XFS_BTREE_TRACE_ARGR(cur, rec);
1777 /* Pick up the current block. */
1778 block = xfs_btree_get_block(cur, 0, &bp);
1781 error = xfs_btree_check_block(cur, block, 0, bp);
1785 /* Get the address of the rec to be updated. */
1786 ptr = cur->bc_ptrs[0];
1787 rp = xfs_btree_rec_addr(cur, ptr, block);
1789 /* Fill in the new contents and log them. */
1790 xfs_btree_copy_recs(cur, rp, rec, 1);
1791 xfs_btree_log_recs(cur, bp, ptr, ptr);
1794 * If we are tracking the last record in the tree and
1795 * we are at the far right edge of the tree, update it.
1797 if (xfs_btree_is_lastrec(cur, block, 0)) {
1798 cur->bc_ops->update_lastrec(cur, block, rec,
1799 ptr, LASTREC_UPDATE);
1802 /* Updating first rec in leaf. Pass new key value up to our parent. */
1804 union xfs_btree_key key;
1806 cur->bc_ops->init_key_from_rec(&key, rec);
1807 error = xfs_btree_updkey(cur, &key, 1);
1812 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1816 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
1821 * Move 1 record left from cur/level if possible.
1822 * Update cur to reflect the new path.
1826 struct xfs_btree_cur *cur,
1828 int *stat) /* success/failure */
1830 union xfs_btree_key key; /* btree key */
1831 struct xfs_buf *lbp; /* left buffer pointer */
1832 struct xfs_btree_block *left; /* left btree block */
1833 int lrecs; /* left record count */
1834 struct xfs_buf *rbp; /* right buffer pointer */
1835 struct xfs_btree_block *right; /* right btree block */
1836 int rrecs; /* right record count */
1837 union xfs_btree_ptr lptr; /* left btree pointer */
1838 union xfs_btree_key *rkp = NULL; /* right btree key */
1839 union xfs_btree_ptr *rpp = NULL; /* right address pointer */
1840 union xfs_btree_rec *rrp = NULL; /* right record pointer */
1841 int error; /* error return value */
1843 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1844 XFS_BTREE_TRACE_ARGI(cur, level);
1846 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
1847 level == cur->bc_nlevels - 1)
1850 /* Set up variables for this block as "right". */
1851 right = xfs_btree_get_block(cur, level, &rbp);
1854 error = xfs_btree_check_block(cur, right, level, rbp);
1859 /* If we've got no left sibling then we can't shift an entry left. */
1860 xfs_btree_get_sibling(cur, right, &lptr, XFS_BB_LEFTSIB);
1861 if (xfs_btree_ptr_is_null(cur, &lptr))
1865 * If the cursor entry is the one that would be moved, don't
1866 * do it... it's too complicated.
1868 if (cur->bc_ptrs[level] <= 1)
1871 /* Set up the left neighbor as "left". */
1872 error = xfs_btree_read_buf_block(cur, &lptr, level, 0, &left, &lbp);
1876 /* If it's full, it can't take another entry. */
1877 lrecs = xfs_btree_get_numrecs(left);
1878 if (lrecs == cur->bc_ops->get_maxrecs(cur, level))
1881 rrecs = xfs_btree_get_numrecs(right);
1884 * We add one entry to the left side and remove one for the right side.
1885 * Accout for it here, the changes will be updated on disk and logged
1891 XFS_BTREE_STATS_INC(cur, lshift);
1892 XFS_BTREE_STATS_ADD(cur, moves, 1);
1895 * If non-leaf, copy a key and a ptr to the left block.
1896 * Log the changes to the left block.
1899 /* It's a non-leaf. Move keys and pointers. */
1900 union xfs_btree_key *lkp; /* left btree key */
1901 union xfs_btree_ptr *lpp; /* left address pointer */
1903 lkp = xfs_btree_key_addr(cur, lrecs, left);
1904 rkp = xfs_btree_key_addr(cur, 1, right);
1906 lpp = xfs_btree_ptr_addr(cur, lrecs, left);
1907 rpp = xfs_btree_ptr_addr(cur, 1, right);
1909 error = xfs_btree_check_ptr(cur, rpp, 0, level);
1913 xfs_btree_copy_keys(cur, lkp, rkp, 1);
1914 xfs_btree_copy_ptrs(cur, lpp, rpp, 1);
1916 xfs_btree_log_keys(cur, lbp, lrecs, lrecs);
1917 xfs_btree_log_ptrs(cur, lbp, lrecs, lrecs);
1919 ASSERT(cur->bc_ops->keys_inorder(cur,
1920 xfs_btree_key_addr(cur, lrecs - 1, left), lkp));
1922 /* It's a leaf. Move records. */
1923 union xfs_btree_rec *lrp; /* left record pointer */
1925 lrp = xfs_btree_rec_addr(cur, lrecs, left);
1926 rrp = xfs_btree_rec_addr(cur, 1, right);
1928 xfs_btree_copy_recs(cur, lrp, rrp, 1);
1929 xfs_btree_log_recs(cur, lbp, lrecs, lrecs);
1931 ASSERT(cur->bc_ops->recs_inorder(cur,
1932 xfs_btree_rec_addr(cur, lrecs - 1, left), lrp));
1935 xfs_btree_set_numrecs(left, lrecs);
1936 xfs_btree_log_block(cur, lbp, XFS_BB_NUMRECS);
1938 xfs_btree_set_numrecs(right, rrecs);
1939 xfs_btree_log_block(cur, rbp, XFS_BB_NUMRECS);
1942 * Slide the contents of right down one entry.
1944 XFS_BTREE_STATS_ADD(cur, moves, rrecs - 1);
1946 /* It's a nonleaf. operate on keys and ptrs */
1948 int i; /* loop index */
1950 for (i = 0; i < rrecs; i++) {
1951 error = xfs_btree_check_ptr(cur, rpp, i + 1, level);
1956 xfs_btree_shift_keys(cur,
1957 xfs_btree_key_addr(cur, 2, right),
1959 xfs_btree_shift_ptrs(cur,
1960 xfs_btree_ptr_addr(cur, 2, right),
1963 xfs_btree_log_keys(cur, rbp, 1, rrecs);
1964 xfs_btree_log_ptrs(cur, rbp, 1, rrecs);
1966 /* It's a leaf. operate on records */
1967 xfs_btree_shift_recs(cur,
1968 xfs_btree_rec_addr(cur, 2, right),
1970 xfs_btree_log_recs(cur, rbp, 1, rrecs);
1973 * If it's the first record in the block, we'll need a key
1974 * structure to pass up to the next level (updkey).
1976 cur->bc_ops->init_key_from_rec(&key,
1977 xfs_btree_rec_addr(cur, 1, right));
1981 /* Update the parent key values of right. */
1982 error = xfs_btree_updkey(cur, rkp, level + 1);
1986 /* Slide the cursor value left one. */
1987 cur->bc_ptrs[level]--;
1989 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1994 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1999 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
2004 * Move 1 record right from cur/level if possible.
2005 * Update cur to reflect the new path.
2009 struct xfs_btree_cur *cur,
2011 int *stat) /* success/failure */
2013 union xfs_btree_key key; /* btree key */
2014 struct xfs_buf *lbp; /* left buffer pointer */
2015 struct xfs_btree_block *left; /* left btree block */
2016 struct xfs_buf *rbp; /* right buffer pointer */
2017 struct xfs_btree_block *right; /* right btree block */
2018 struct xfs_btree_cur *tcur; /* temporary btree cursor */
2019 union xfs_btree_ptr rptr; /* right block pointer */
2020 union xfs_btree_key *rkp; /* right btree key */
2021 int rrecs; /* right record count */
2022 int lrecs; /* left record count */
2023 int error; /* error return value */
2024 int i; /* loop counter */
2026 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
2027 XFS_BTREE_TRACE_ARGI(cur, level);
2029 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
2030 (level == cur->bc_nlevels - 1))
2033 /* Set up variables for this block as "left". */
2034 left = xfs_btree_get_block(cur, level, &lbp);
2037 error = xfs_btree_check_block(cur, left, level, lbp);
2042 /* If we've got no right sibling then we can't shift an entry right. */
2043 xfs_btree_get_sibling(cur, left, &rptr, XFS_BB_RIGHTSIB);
2044 if (xfs_btree_ptr_is_null(cur, &rptr))
2048 * If the cursor entry is the one that would be moved, don't
2049 * do it... it's too complicated.
2051 lrecs = xfs_btree_get_numrecs(left);
2052 if (cur->bc_ptrs[level] >= lrecs)
2055 /* Set up the right neighbor as "right". */
2056 error = xfs_btree_read_buf_block(cur, &rptr, level, 0, &right, &rbp);
2060 /* If it's full, it can't take another entry. */
2061 rrecs = xfs_btree_get_numrecs(right);
2062 if (rrecs == cur->bc_ops->get_maxrecs(cur, level))
2065 XFS_BTREE_STATS_INC(cur, rshift);
2066 XFS_BTREE_STATS_ADD(cur, moves, rrecs);
2069 * Make a hole at the start of the right neighbor block, then
2070 * copy the last left block entry to the hole.
2073 /* It's a nonleaf. make a hole in the keys and ptrs */
2074 union xfs_btree_key *lkp;
2075 union xfs_btree_ptr *lpp;
2076 union xfs_btree_ptr *rpp;
2078 lkp = xfs_btree_key_addr(cur, lrecs, left);
2079 lpp = xfs_btree_ptr_addr(cur, lrecs, left);
2080 rkp = xfs_btree_key_addr(cur, 1, right);
2081 rpp = xfs_btree_ptr_addr(cur, 1, right);
2084 for (i = rrecs - 1; i >= 0; i--) {
2085 error = xfs_btree_check_ptr(cur, rpp, i, level);
2091 xfs_btree_shift_keys(cur, rkp, 1, rrecs);
2092 xfs_btree_shift_ptrs(cur, rpp, 1, rrecs);
2095 error = xfs_btree_check_ptr(cur, lpp, 0, level);
2100 /* Now put the new data in, and log it. */
2101 xfs_btree_copy_keys(cur, rkp, lkp, 1);
2102 xfs_btree_copy_ptrs(cur, rpp, lpp, 1);
2104 xfs_btree_log_keys(cur, rbp, 1, rrecs + 1);
2105 xfs_btree_log_ptrs(cur, rbp, 1, rrecs + 1);
2107 ASSERT(cur->bc_ops->keys_inorder(cur, rkp,
2108 xfs_btree_key_addr(cur, 2, right)));
2110 /* It's a leaf. make a hole in the records */
2111 union xfs_btree_rec *lrp;
2112 union xfs_btree_rec *rrp;
2114 lrp = xfs_btree_rec_addr(cur, lrecs, left);
2115 rrp = xfs_btree_rec_addr(cur, 1, right);
2117 xfs_btree_shift_recs(cur, rrp, 1, rrecs);
2119 /* Now put the new data in, and log it. */
2120 xfs_btree_copy_recs(cur, rrp, lrp, 1);
2121 xfs_btree_log_recs(cur, rbp, 1, rrecs + 1);
2123 cur->bc_ops->init_key_from_rec(&key, rrp);
2126 ASSERT(cur->bc_ops->recs_inorder(cur, rrp,
2127 xfs_btree_rec_addr(cur, 2, right)));
2131 * Decrement and log left's numrecs, bump and log right's numrecs.
2133 xfs_btree_set_numrecs(left, --lrecs);
2134 xfs_btree_log_block(cur, lbp, XFS_BB_NUMRECS);
2136 xfs_btree_set_numrecs(right, ++rrecs);
2137 xfs_btree_log_block(cur, rbp, XFS_BB_NUMRECS);
2140 * Using a temporary cursor, update the parent key values of the
2141 * block on the right.
2143 error = xfs_btree_dup_cursor(cur, &tcur);
2146 i = xfs_btree_lastrec(tcur, level);
2147 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
2149 error = xfs_btree_increment(tcur, level, &i);
2153 error = xfs_btree_updkey(tcur, rkp, level + 1);
2157 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
2159 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2164 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2169 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
2173 XFS_BTREE_TRACE_CURSOR(tcur, XBT_ERROR);
2174 xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
2179 * Split cur/level block in half.
2180 * Return new block number and the key to its first
2181 * record (to be inserted into parent).
2185 struct xfs_btree_cur *cur,
2187 union xfs_btree_ptr *ptrp,
2188 union xfs_btree_key *key,
2189 struct xfs_btree_cur **curp,
2190 int *stat) /* success/failure */
2192 union xfs_btree_ptr lptr; /* left sibling block ptr */
2193 struct xfs_buf *lbp; /* left buffer pointer */
2194 struct xfs_btree_block *left; /* left btree block */
2195 union xfs_btree_ptr rptr; /* right sibling block ptr */
2196 struct xfs_buf *rbp; /* right buffer pointer */
2197 struct xfs_btree_block *right; /* right btree block */
2198 union xfs_btree_ptr rrptr; /* right-right sibling ptr */
2199 struct xfs_buf *rrbp; /* right-right buffer pointer */
2200 struct xfs_btree_block *rrblock; /* right-right btree block */
2204 int error; /* error return value */
2209 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
2210 XFS_BTREE_TRACE_ARGIPK(cur, level, *ptrp, key);
2212 XFS_BTREE_STATS_INC(cur, split);
2214 /* Set up left block (current one). */
2215 left = xfs_btree_get_block(cur, level, &lbp);
2218 error = xfs_btree_check_block(cur, left, level, lbp);
2223 xfs_btree_buf_to_ptr(cur, lbp, &lptr);
2225 /* Allocate the new block. If we can't do it, we're toast. Give up. */
2226 error = cur->bc_ops->alloc_block(cur, &lptr, &rptr, 1, stat);
2231 XFS_BTREE_STATS_INC(cur, alloc);
2233 /* Set up the new block as "right". */
2234 error = xfs_btree_get_buf_block(cur, &rptr, 0, &right, &rbp);
2238 /* Fill in the btree header for the new right block. */
2239 xfs_btree_init_block(cur, xfs_btree_get_level(left), 0, right);
2242 * Split the entries between the old and the new block evenly.
2243 * Make sure that if there's an odd number of entries now, that
2244 * each new block will have the same number of entries.
2246 lrecs = xfs_btree_get_numrecs(left);
2248 if ((lrecs & 1) && cur->bc_ptrs[level] <= rrecs + 1)
2250 src_index = (lrecs - rrecs + 1);
2252 XFS_BTREE_STATS_ADD(cur, moves, rrecs);
2255 * Copy btree block entries from the left block over to the
2256 * new block, the right. Update the right block and log the
2260 /* It's a non-leaf. Move keys and pointers. */
2261 union xfs_btree_key *lkp; /* left btree key */
2262 union xfs_btree_ptr *lpp; /* left address pointer */
2263 union xfs_btree_key *rkp; /* right btree key */
2264 union xfs_btree_ptr *rpp; /* right address pointer */
2266 lkp = xfs_btree_key_addr(cur, src_index, left);
2267 lpp = xfs_btree_ptr_addr(cur, src_index, left);
2268 rkp = xfs_btree_key_addr(cur, 1, right);
2269 rpp = xfs_btree_ptr_addr(cur, 1, right);
2272 for (i = src_index; i < rrecs; i++) {
2273 error = xfs_btree_check_ptr(cur, lpp, i, level);
2279 xfs_btree_copy_keys(cur, rkp, lkp, rrecs);
2280 xfs_btree_copy_ptrs(cur, rpp, lpp, rrecs);
2282 xfs_btree_log_keys(cur, rbp, 1, rrecs);
2283 xfs_btree_log_ptrs(cur, rbp, 1, rrecs);
2285 /* Grab the keys to the entries moved to the right block */
2286 xfs_btree_copy_keys(cur, key, rkp, 1);
2288 /* It's a leaf. Move records. */
2289 union xfs_btree_rec *lrp; /* left record pointer */
2290 union xfs_btree_rec *rrp; /* right record pointer */
2292 lrp = xfs_btree_rec_addr(cur, src_index, left);
2293 rrp = xfs_btree_rec_addr(cur, 1, right);
2295 xfs_btree_copy_recs(cur, rrp, lrp, rrecs);
2296 xfs_btree_log_recs(cur, rbp, 1, rrecs);
2298 cur->bc_ops->init_key_from_rec(key,
2299 xfs_btree_rec_addr(cur, 1, right));
2304 * Find the left block number by looking in the buffer.
2305 * Adjust numrecs, sibling pointers.
2307 xfs_btree_get_sibling(cur, left, &rrptr, XFS_BB_RIGHTSIB);
2308 xfs_btree_set_sibling(cur, right, &rrptr, XFS_BB_RIGHTSIB);
2309 xfs_btree_set_sibling(cur, right, &lptr, XFS_BB_LEFTSIB);
2310 xfs_btree_set_sibling(cur, left, &rptr, XFS_BB_RIGHTSIB);
2313 xfs_btree_set_numrecs(left, lrecs);
2314 xfs_btree_set_numrecs(right, xfs_btree_get_numrecs(right) + rrecs);
2316 xfs_btree_log_block(cur, rbp, XFS_BB_ALL_BITS);
2317 xfs_btree_log_block(cur, lbp, XFS_BB_NUMRECS | XFS_BB_RIGHTSIB);
2320 * If there's a block to the new block's right, make that block
2321 * point back to right instead of to left.
2323 if (!xfs_btree_ptr_is_null(cur, &rrptr)) {
2324 error = xfs_btree_read_buf_block(cur, &rrptr, level,
2325 0, &rrblock, &rrbp);
2328 xfs_btree_set_sibling(cur, rrblock, &rptr, XFS_BB_LEFTSIB);
2329 xfs_btree_log_block(cur, rrbp, XFS_BB_LEFTSIB);
2332 * If the cursor is really in the right block, move it there.
2333 * If it's just pointing past the last entry in left, then we'll
2334 * insert there, so don't change anything in that case.
2336 if (cur->bc_ptrs[level] > lrecs + 1) {
2337 xfs_btree_setbuf(cur, level, rbp);
2338 cur->bc_ptrs[level] -= lrecs;
2341 * If there are more levels, we'll need another cursor which refers
2342 * the right block, no matter where this cursor was.
2344 if (level + 1 < cur->bc_nlevels) {
2345 error = xfs_btree_dup_cursor(cur, curp);
2348 (*curp)->bc_ptrs[level + 1]++;
2351 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2355 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2360 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
2365 * Copy the old inode root contents into a real block and make the
2366 * broot point to it.
2369 xfs_btree_new_iroot(
2370 struct xfs_btree_cur *cur, /* btree cursor */
2371 int *logflags, /* logging flags for inode */
2372 int *stat) /* return status - 0 fail */
2374 struct xfs_buf *cbp; /* buffer for cblock */
2375 struct xfs_btree_block *block; /* btree block */
2376 struct xfs_btree_block *cblock; /* child btree block */
2377 union xfs_btree_key *ckp; /* child key pointer */
2378 union xfs_btree_ptr *cpp; /* child ptr pointer */
2379 union xfs_btree_key *kp; /* pointer to btree key */
2380 union xfs_btree_ptr *pp; /* pointer to block addr */
2381 union xfs_btree_ptr nptr; /* new block addr */
2382 int level; /* btree level */
2383 int error; /* error return code */
2385 int i; /* loop counter */
2388 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
2389 XFS_BTREE_STATS_INC(cur, newroot);
2391 ASSERT(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE);
2393 level = cur->bc_nlevels - 1;
2395 block = xfs_btree_get_iroot(cur);
2396 pp = xfs_btree_ptr_addr(cur, 1, block);
2398 /* Allocate the new block. If we can't do it, we're toast. Give up. */
2399 error = cur->bc_ops->alloc_block(cur, pp, &nptr, 1, stat);
2403 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2406 XFS_BTREE_STATS_INC(cur, alloc);
2408 /* Copy the root into a real block. */
2409 error = xfs_btree_get_buf_block(cur, &nptr, 0, &cblock, &cbp);
2413 memcpy(cblock, block, xfs_btree_block_len(cur));
2415 be16_add_cpu(&block->bb_level, 1);
2416 xfs_btree_set_numrecs(block, 1);
2418 cur->bc_ptrs[level + 1] = 1;
2420 kp = xfs_btree_key_addr(cur, 1, block);
2421 ckp = xfs_btree_key_addr(cur, 1, cblock);
2422 xfs_btree_copy_keys(cur, ckp, kp, xfs_btree_get_numrecs(cblock));
2424 cpp = xfs_btree_ptr_addr(cur, 1, cblock);
2426 for (i = 0; i < be16_to_cpu(cblock->bb_numrecs); i++) {
2427 error = xfs_btree_check_ptr(cur, pp, i, level);
2432 xfs_btree_copy_ptrs(cur, cpp, pp, xfs_btree_get_numrecs(cblock));
2435 error = xfs_btree_check_ptr(cur, &nptr, 0, level);
2439 xfs_btree_copy_ptrs(cur, pp, &nptr, 1);
2441 xfs_iroot_realloc(cur->bc_private.b.ip,
2442 1 - xfs_btree_get_numrecs(cblock),
2443 cur->bc_private.b.whichfork);
2445 xfs_btree_setbuf(cur, level, cbp);
2448 * Do all this logging at the end so that
2449 * the root is at the right level.
2451 xfs_btree_log_block(cur, cbp, XFS_BB_ALL_BITS);
2452 xfs_btree_log_keys(cur, cbp, 1, be16_to_cpu(cblock->bb_numrecs));
2453 xfs_btree_log_ptrs(cur, cbp, 1, be16_to_cpu(cblock->bb_numrecs));
2456 XFS_ILOG_CORE | XFS_ILOG_FBROOT(cur->bc_private.b.whichfork);
2458 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2461 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
2466 * Allocate a new root block, fill it in.
2470 struct xfs_btree_cur *cur, /* btree cursor */
2471 int *stat) /* success/failure */
2473 struct xfs_btree_block *block; /* one half of the old root block */
2474 struct xfs_buf *bp; /* buffer containing block */
2475 int error; /* error return value */
2476 struct xfs_buf *lbp; /* left buffer pointer */
2477 struct xfs_btree_block *left; /* left btree block */
2478 struct xfs_buf *nbp; /* new (root) buffer */
2479 struct xfs_btree_block *new; /* new (root) btree block */
2480 int nptr; /* new value for key index, 1 or 2 */
2481 struct xfs_buf *rbp; /* right buffer pointer */
2482 struct xfs_btree_block *right; /* right btree block */
2483 union xfs_btree_ptr rptr;
2484 union xfs_btree_ptr lptr;
2486 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
2487 XFS_BTREE_STATS_INC(cur, newroot);
2489 /* initialise our start point from the cursor */
2490 cur->bc_ops->init_ptr_from_cur(cur, &rptr);
2492 /* Allocate the new block. If we can't do it, we're toast. Give up. */
2493 error = cur->bc_ops->alloc_block(cur, &rptr, &lptr, 1, stat);
2498 XFS_BTREE_STATS_INC(cur, alloc);
2500 /* Set up the new block. */
2501 error = xfs_btree_get_buf_block(cur, &lptr, 0, &new, &nbp);
2505 /* Set the root in the holding structure increasing the level by 1. */
2506 cur->bc_ops->set_root(cur, &lptr, 1);
2509 * At the previous root level there are now two blocks: the old root,
2510 * and the new block generated when it was split. We don't know which
2511 * one the cursor is pointing at, so we set up variables "left" and
2512 * "right" for each case.
2514 block = xfs_btree_get_block(cur, cur->bc_nlevels - 1, &bp);
2517 error = xfs_btree_check_block(cur, block, cur->bc_nlevels - 1, bp);
2522 xfs_btree_get_sibling(cur, block, &rptr, XFS_BB_RIGHTSIB);
2523 if (!xfs_btree_ptr_is_null(cur, &rptr)) {
2524 /* Our block is left, pick up the right block. */
2526 xfs_btree_buf_to_ptr(cur, lbp, &lptr);
2528 error = xfs_btree_read_buf_block(cur, &rptr,
2529 cur->bc_nlevels - 1, 0, &right, &rbp);
2535 /* Our block is right, pick up the left block. */
2537 xfs_btree_buf_to_ptr(cur, rbp, &rptr);
2539 xfs_btree_get_sibling(cur, right, &lptr, XFS_BB_LEFTSIB);
2540 error = xfs_btree_read_buf_block(cur, &lptr,
2541 cur->bc_nlevels - 1, 0, &left, &lbp);
2547 /* Fill in the new block's btree header and log it. */
2548 xfs_btree_init_block(cur, cur->bc_nlevels, 2, new);
2549 xfs_btree_log_block(cur, nbp, XFS_BB_ALL_BITS);
2550 ASSERT(!xfs_btree_ptr_is_null(cur, &lptr) &&
2551 !xfs_btree_ptr_is_null(cur, &rptr));
2553 /* Fill in the key data in the new root. */
2554 if (xfs_btree_get_level(left) > 0) {
2555 xfs_btree_copy_keys(cur,
2556 xfs_btree_key_addr(cur, 1, new),
2557 xfs_btree_key_addr(cur, 1, left), 1);
2558 xfs_btree_copy_keys(cur,
2559 xfs_btree_key_addr(cur, 2, new),
2560 xfs_btree_key_addr(cur, 1, right), 1);
2562 cur->bc_ops->init_key_from_rec(
2563 xfs_btree_key_addr(cur, 1, new),
2564 xfs_btree_rec_addr(cur, 1, left));
2565 cur->bc_ops->init_key_from_rec(
2566 xfs_btree_key_addr(cur, 2, new),
2567 xfs_btree_rec_addr(cur, 1, right));
2569 xfs_btree_log_keys(cur, nbp, 1, 2);
2571 /* Fill in the pointer data in the new root. */
2572 xfs_btree_copy_ptrs(cur,
2573 xfs_btree_ptr_addr(cur, 1, new), &lptr, 1);
2574 xfs_btree_copy_ptrs(cur,
2575 xfs_btree_ptr_addr(cur, 2, new), &rptr, 1);
2576 xfs_btree_log_ptrs(cur, nbp, 1, 2);
2578 /* Fix up the cursor. */
2579 xfs_btree_setbuf(cur, cur->bc_nlevels, nbp);
2580 cur->bc_ptrs[cur->bc_nlevels] = nptr;
2582 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2586 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
2589 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2595 xfs_btree_make_block_unfull(
2596 struct xfs_btree_cur *cur, /* btree cursor */
2597 int level, /* btree level */
2598 int numrecs,/* # of recs in block */
2599 int *oindex,/* old tree index */
2600 int *index, /* new tree index */
2601 union xfs_btree_ptr *nptr, /* new btree ptr */
2602 struct xfs_btree_cur **ncur, /* new btree cursor */
2603 union xfs_btree_rec *nrec, /* new record */
2606 union xfs_btree_key key; /* new btree key value */
2609 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
2610 level == cur->bc_nlevels - 1) {
2611 struct xfs_inode *ip = cur->bc_private.b.ip;
2613 if (numrecs < cur->bc_ops->get_dmaxrecs(cur, level)) {
2614 /* A root block that can be made bigger. */
2616 xfs_iroot_realloc(ip, 1, cur->bc_private.b.whichfork);
2618 /* A root block that needs replacing */
2621 error = xfs_btree_new_iroot(cur, &logflags, stat);
2622 if (error || *stat == 0)
2625 xfs_trans_log_inode(cur->bc_tp, ip, logflags);
2631 /* First, try shifting an entry to the right neighbor. */
2632 error = xfs_btree_rshift(cur, level, stat);
2636 /* Next, try shifting an entry to the left neighbor. */
2637 error = xfs_btree_lshift(cur, level, stat);
2642 *oindex = *index = cur->bc_ptrs[level];
2647 * Next, try splitting the current block in half.
2649 * If this works we have to re-set our variables because we
2650 * could be in a different block now.
2652 error = xfs_btree_split(cur, level, nptr, &key, ncur, stat);
2653 if (error || *stat == 0)
2657 *index = cur->bc_ptrs[level];
2658 cur->bc_ops->init_rec_from_key(&key, nrec);
2663 * Insert one record/level. Return information to the caller
2664 * allowing the next level up to proceed if necessary.
2668 struct xfs_btree_cur *cur, /* btree cursor */
2669 int level, /* level to insert record at */
2670 union xfs_btree_ptr *ptrp, /* i/o: block number inserted */
2671 union xfs_btree_rec *recp, /* i/o: record data inserted */
2672 struct xfs_btree_cur **curp, /* output: new cursor replacing cur */
2673 int *stat) /* success/failure */
2675 struct xfs_btree_block *block; /* btree block */
2676 struct xfs_buf *bp; /* buffer for block */
2677 union xfs_btree_key key; /* btree key */
2678 union xfs_btree_ptr nptr; /* new block ptr */
2679 struct xfs_btree_cur *ncur; /* new btree cursor */
2680 union xfs_btree_rec nrec; /* new record count */
2681 int optr; /* old key/record index */
2682 int ptr; /* key/record index */
2683 int numrecs;/* number of records */
2684 int error; /* error return value */
2689 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
2690 XFS_BTREE_TRACE_ARGIPR(cur, level, *ptrp, recp);
2695 * If we have an external root pointer, and we've made it to the
2696 * root level, allocate a new root block and we're done.
2698 if (!(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
2699 (level >= cur->bc_nlevels)) {
2700 error = xfs_btree_new_root(cur, stat);
2701 xfs_btree_set_ptr_null(cur, ptrp);
2703 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2707 /* If we're off the left edge, return failure. */
2708 ptr = cur->bc_ptrs[level];
2710 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2715 /* Make a key out of the record data to be inserted, and save it. */
2716 cur->bc_ops->init_key_from_rec(&key, recp);
2720 XFS_BTREE_STATS_INC(cur, insrec);
2722 /* Get pointers to the btree buffer and block. */
2723 block = xfs_btree_get_block(cur, level, &bp);
2724 numrecs = xfs_btree_get_numrecs(block);
2727 error = xfs_btree_check_block(cur, block, level, bp);
2731 /* Check that the new entry is being inserted in the right place. */
2732 if (ptr <= numrecs) {
2734 ASSERT(cur->bc_ops->recs_inorder(cur, recp,
2735 xfs_btree_rec_addr(cur, ptr, block)));
2737 ASSERT(cur->bc_ops->keys_inorder(cur, &key,
2738 xfs_btree_key_addr(cur, ptr, block)));
2744 * If the block is full, we can't insert the new entry until we
2745 * make the block un-full.
2747 xfs_btree_set_ptr_null(cur, &nptr);
2748 if (numrecs == cur->bc_ops->get_maxrecs(cur, level)) {
2749 error = xfs_btree_make_block_unfull(cur, level, numrecs,
2750 &optr, &ptr, &nptr, &ncur, &nrec, stat);
2751 if (error || *stat == 0)
2756 * The current block may have changed if the block was
2757 * previously full and we have just made space in it.
2759 block = xfs_btree_get_block(cur, level, &bp);
2760 numrecs = xfs_btree_get_numrecs(block);
2763 error = xfs_btree_check_block(cur, block, level, bp);
2769 * At this point we know there's room for our new entry in the block
2770 * we're pointing at.
2772 XFS_BTREE_STATS_ADD(cur, moves, numrecs - ptr + 1);
2775 /* It's a nonleaf. make a hole in the keys and ptrs */
2776 union xfs_btree_key *kp;
2777 union xfs_btree_ptr *pp;
2779 kp = xfs_btree_key_addr(cur, ptr, block);
2780 pp = xfs_btree_ptr_addr(cur, ptr, block);
2783 for (i = numrecs - ptr; i >= 0; i--) {
2784 error = xfs_btree_check_ptr(cur, pp, i, level);
2790 xfs_btree_shift_keys(cur, kp, 1, numrecs - ptr + 1);
2791 xfs_btree_shift_ptrs(cur, pp, 1, numrecs - ptr + 1);
2794 error = xfs_btree_check_ptr(cur, ptrp, 0, level);
2799 /* Now put the new data in, bump numrecs and log it. */
2800 xfs_btree_copy_keys(cur, kp, &key, 1);
2801 xfs_btree_copy_ptrs(cur, pp, ptrp, 1);
2803 xfs_btree_set_numrecs(block, numrecs);
2804 xfs_btree_log_ptrs(cur, bp, ptr, numrecs);
2805 xfs_btree_log_keys(cur, bp, ptr, numrecs);
2807 if (ptr < numrecs) {
2808 ASSERT(cur->bc_ops->keys_inorder(cur, kp,
2809 xfs_btree_key_addr(cur, ptr + 1, block)));
2813 /* It's a leaf. make a hole in the records */
2814 union xfs_btree_rec *rp;
2816 rp = xfs_btree_rec_addr(cur, ptr, block);
2818 xfs_btree_shift_recs(cur, rp, 1, numrecs - ptr + 1);
2820 /* Now put the new data in, bump numrecs and log it. */
2821 xfs_btree_copy_recs(cur, rp, recp, 1);
2822 xfs_btree_set_numrecs(block, ++numrecs);
2823 xfs_btree_log_recs(cur, bp, ptr, numrecs);
2825 if (ptr < numrecs) {
2826 ASSERT(cur->bc_ops->recs_inorder(cur, rp,
2827 xfs_btree_rec_addr(cur, ptr + 1, block)));
2832 /* Log the new number of records in the btree header. */
2833 xfs_btree_log_block(cur, bp, XFS_BB_NUMRECS);
2835 /* If we inserted at the start of a block, update the parents' keys. */
2837 error = xfs_btree_updkey(cur, &key, level + 1);
2843 * If we are tracking the last record in the tree and
2844 * we are at the far right edge of the tree, update it.
2846 if (xfs_btree_is_lastrec(cur, block, level)) {
2847 cur->bc_ops->update_lastrec(cur, block, recp,
2848 ptr, LASTREC_INSREC);
2852 * Return the new block number, if any.
2853 * If there is one, give back a record value and a cursor too.
2856 if (!xfs_btree_ptr_is_null(cur, &nptr)) {
2861 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2866 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
2871 * Insert the record at the point referenced by cur.
2873 * A multi-level split of the tree on insert will invalidate the original
2874 * cursor. All callers of this function should assume that the cursor is
2875 * no longer valid and revalidate it.
2879 struct xfs_btree_cur *cur,
2882 int error; /* error return value */
2883 int i; /* result value, 0 for failure */
2884 int level; /* current level number in btree */
2885 union xfs_btree_ptr nptr; /* new block number (split result) */
2886 struct xfs_btree_cur *ncur; /* new cursor (split result) */
2887 struct xfs_btree_cur *pcur; /* previous level's cursor */
2888 union xfs_btree_rec rec; /* record to insert */
2894 xfs_btree_set_ptr_null(cur, &nptr);
2895 cur->bc_ops->init_rec_from_cur(cur, &rec);
2898 * Loop going up the tree, starting at the leaf level.
2899 * Stop when we don't get a split block, that must mean that
2900 * the insert is finished with this level.
2904 * Insert nrec/nptr into this level of the tree.
2905 * Note if we fail, nptr will be null.
2907 error = xfs_btree_insrec(pcur, level, &nptr, &rec, &ncur, &i);
2910 xfs_btree_del_cursor(pcur, XFS_BTREE_ERROR);
2914 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
2918 * See if the cursor we just used is trash.
2919 * Can't trash the caller's cursor, but otherwise we should
2920 * if ncur is a new cursor or we're about to be done.
2923 (ncur || xfs_btree_ptr_is_null(cur, &nptr))) {
2924 /* Save the state from the cursor before we trash it */
2925 if (cur->bc_ops->update_cursor)
2926 cur->bc_ops->update_cursor(pcur, cur);
2927 cur->bc_nlevels = pcur->bc_nlevels;
2928 xfs_btree_del_cursor(pcur, XFS_BTREE_NOERROR);
2930 /* If we got a new cursor, switch to it. */
2935 } while (!xfs_btree_ptr_is_null(cur, &nptr));
2937 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2941 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
2946 * Try to merge a non-leaf block back into the inode root.
2948 * Note: the killroot names comes from the fact that we're effectively
2949 * killing the old root block. But because we can't just delete the
2950 * inode we have to copy the single block it was pointing to into the
2954 xfs_btree_kill_iroot(
2955 struct xfs_btree_cur *cur)
2957 int whichfork = cur->bc_private.b.whichfork;
2958 struct xfs_inode *ip = cur->bc_private.b.ip;
2959 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
2960 struct xfs_btree_block *block;
2961 struct xfs_btree_block *cblock;
2962 union xfs_btree_key *kp;
2963 union xfs_btree_key *ckp;
2964 union xfs_btree_ptr *pp;
2965 union xfs_btree_ptr *cpp;
2966 struct xfs_buf *cbp;
2971 union xfs_btree_ptr ptr;
2975 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
2977 ASSERT(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE);
2978 ASSERT(cur->bc_nlevels > 1);
2981 * Don't deal with the root block needs to be a leaf case.
2982 * We're just going to turn the thing back into extents anyway.
2984 level = cur->bc_nlevels - 1;
2989 * Give up if the root has multiple children.
2991 block = xfs_btree_get_iroot(cur);
2992 if (xfs_btree_get_numrecs(block) != 1)
2995 cblock = xfs_btree_get_block(cur, level - 1, &cbp);
2996 numrecs = xfs_btree_get_numrecs(cblock);
2999 * Only do this if the next level will fit.
3000 * Then the data must be copied up to the inode,
3001 * instead of freeing the root you free the next level.
3003 if (numrecs > cur->bc_ops->get_dmaxrecs(cur, level))
3006 XFS_BTREE_STATS_INC(cur, killroot);
3009 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_LEFTSIB);
3010 ASSERT(xfs_btree_ptr_is_null(cur, &ptr));
3011 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_RIGHTSIB);
3012 ASSERT(xfs_btree_ptr_is_null(cur, &ptr));
3015 index = numrecs - cur->bc_ops->get_maxrecs(cur, level);
3017 xfs_iroot_realloc(cur->bc_private.b.ip, index,
3018 cur->bc_private.b.whichfork);
3019 block = (struct xfs_btree_block *)ifp->if_broot;
3022 be16_add_cpu(&block->bb_numrecs, index);
3023 ASSERT(block->bb_numrecs == cblock->bb_numrecs);
3025 kp = xfs_btree_key_addr(cur, 1, block);
3026 ckp = xfs_btree_key_addr(cur, 1, cblock);
3027 xfs_btree_copy_keys(cur, kp, ckp, numrecs);
3029 pp = xfs_btree_ptr_addr(cur, 1, block);
3030 cpp = xfs_btree_ptr_addr(cur, 1, cblock);
3032 for (i = 0; i < numrecs; i++) {
3035 error = xfs_btree_check_ptr(cur, cpp, i, level - 1);
3037 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
3042 xfs_btree_copy_ptrs(cur, pp, cpp, numrecs);
3044 cur->bc_ops->free_block(cur, cbp);
3045 XFS_BTREE_STATS_INC(cur, free);
3047 cur->bc_bufs[level - 1] = NULL;
3048 be16_add_cpu(&block->bb_level, -1);
3049 xfs_trans_log_inode(cur->bc_tp, ip,
3050 XFS_ILOG_CORE | XFS_ILOG_FBROOT(cur->bc_private.b.whichfork));
3053 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3058 xfs_btree_dec_cursor(
3059 struct xfs_btree_cur *cur,
3067 error = xfs_btree_decrement(cur, level, &i);
3072 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3078 * Single level of the btree record deletion routine.
3079 * Delete record pointed to by cur/level.
3080 * Remove the record from its block then rebalance the tree.
3081 * Return 0 for error, 1 for done, 2 to go on to the next level.
3083 STATIC int /* error */
3085 struct xfs_btree_cur *cur, /* btree cursor */
3086 int level, /* level removing record from */
3087 int *stat) /* fail/done/go-on */
3089 struct xfs_btree_block *block; /* btree block */
3090 union xfs_btree_ptr cptr; /* current block ptr */
3091 struct xfs_buf *bp; /* buffer for block */
3092 int error; /* error return value */
3093 int i; /* loop counter */
3094 union xfs_btree_key key; /* storage for keyp */
3095 union xfs_btree_key *keyp = &key; /* passed to the next level */
3096 union xfs_btree_ptr lptr; /* left sibling block ptr */
3097 struct xfs_buf *lbp; /* left buffer pointer */
3098 struct xfs_btree_block *left; /* left btree block */
3099 int lrecs = 0; /* left record count */
3100 int ptr; /* key/record index */
3101 union xfs_btree_ptr rptr; /* right sibling block ptr */
3102 struct xfs_buf *rbp; /* right buffer pointer */
3103 struct xfs_btree_block *right; /* right btree block */
3104 struct xfs_btree_block *rrblock; /* right-right btree block */
3105 struct xfs_buf *rrbp; /* right-right buffer pointer */
3106 int rrecs = 0; /* right record count */
3107 struct xfs_btree_cur *tcur; /* temporary btree cursor */
3108 int numrecs; /* temporary numrec count */
3110 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
3111 XFS_BTREE_TRACE_ARGI(cur, level);
3115 /* Get the index of the entry being deleted, check for nothing there. */
3116 ptr = cur->bc_ptrs[level];
3118 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3123 /* Get the buffer & block containing the record or key/ptr. */
3124 block = xfs_btree_get_block(cur, level, &bp);
3125 numrecs = xfs_btree_get_numrecs(block);
3128 error = xfs_btree_check_block(cur, block, level, bp);
3133 /* Fail if we're off the end of the block. */
3134 if (ptr > numrecs) {
3135 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3140 XFS_BTREE_STATS_INC(cur, delrec);
3141 XFS_BTREE_STATS_ADD(cur, moves, numrecs - ptr);
3143 /* Excise the entries being deleted. */
3145 /* It's a nonleaf. operate on keys and ptrs */
3146 union xfs_btree_key *lkp;
3147 union xfs_btree_ptr *lpp;
3149 lkp = xfs_btree_key_addr(cur, ptr + 1, block);
3150 lpp = xfs_btree_ptr_addr(cur, ptr + 1, block);
3153 for (i = 0; i < numrecs - ptr; i++) {
3154 error = xfs_btree_check_ptr(cur, lpp, i, level);
3160 if (ptr < numrecs) {
3161 xfs_btree_shift_keys(cur, lkp, -1, numrecs - ptr);
3162 xfs_btree_shift_ptrs(cur, lpp, -1, numrecs - ptr);
3163 xfs_btree_log_keys(cur, bp, ptr, numrecs - 1);
3164 xfs_btree_log_ptrs(cur, bp, ptr, numrecs - 1);
3168 * If it's the first record in the block, we'll need to pass a
3169 * key up to the next level (updkey).
3172 keyp = xfs_btree_key_addr(cur, 1, block);
3174 /* It's a leaf. operate on records */
3175 if (ptr < numrecs) {
3176 xfs_btree_shift_recs(cur,
3177 xfs_btree_rec_addr(cur, ptr + 1, block),
3179 xfs_btree_log_recs(cur, bp, ptr, numrecs - 1);
3183 * If it's the first record in the block, we'll need a key
3184 * structure to pass up to the next level (updkey).
3187 cur->bc_ops->init_key_from_rec(&key,
3188 xfs_btree_rec_addr(cur, 1, block));
3194 * Decrement and log the number of entries in the block.
3196 xfs_btree_set_numrecs(block, --numrecs);
3197 xfs_btree_log_block(cur, bp, XFS_BB_NUMRECS);
3200 * If we are tracking the last record in the tree and
3201 * we are at the far right edge of the tree, update it.
3203 if (xfs_btree_is_lastrec(cur, block, level)) {
3204 cur->bc_ops->update_lastrec(cur, block, NULL,
3205 ptr, LASTREC_DELREC);
3209 * We're at the root level. First, shrink the root block in-memory.
3210 * Try to get rid of the next level down. If we can't then there's
3211 * nothing left to do.
3213 if (level == cur->bc_nlevels - 1) {
3214 if (cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) {
3215 xfs_iroot_realloc(cur->bc_private.b.ip, -1,
3216 cur->bc_private.b.whichfork);
3218 error = xfs_btree_kill_iroot(cur);
3222 error = xfs_btree_dec_cursor(cur, level, stat);
3230 * If this is the root level, and there's only one entry left,
3231 * and it's NOT the leaf level, then we can get rid of this
3234 if (numrecs == 1 && level > 0) {
3235 union xfs_btree_ptr *pp;
3237 * pp is still set to the first pointer in the block.
3238 * Make it the new root of the btree.
3240 pp = xfs_btree_ptr_addr(cur, 1, block);
3241 error = cur->bc_ops->kill_root(cur, bp, level, pp);
3244 } else if (level > 0) {
3245 error = xfs_btree_dec_cursor(cur, level, stat);
3254 * If we deleted the leftmost entry in the block, update the
3255 * key values above us in the tree.
3258 error = xfs_btree_updkey(cur, keyp, level + 1);
3264 * If the number of records remaining in the block is at least
3265 * the minimum, we're done.
3267 if (numrecs >= cur->bc_ops->get_minrecs(cur, level)) {
3268 error = xfs_btree_dec_cursor(cur, level, stat);
3275 * Otherwise, we have to move some records around to keep the
3276 * tree balanced. Look at the left and right sibling blocks to
3277 * see if we can re-balance by moving only one record.
3279 xfs_btree_get_sibling(cur, block, &rptr, XFS_BB_RIGHTSIB);
3280 xfs_btree_get_sibling(cur, block, &lptr, XFS_BB_LEFTSIB);
3282 if (cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) {
3284 * One child of root, need to get a chance to copy its contents
3285 * into the root and delete it. Can't go up to next level,
3286 * there's nothing to delete there.
3288 if (xfs_btree_ptr_is_null(cur, &rptr) &&
3289 xfs_btree_ptr_is_null(cur, &lptr) &&
3290 level == cur->bc_nlevels - 2) {
3291 error = xfs_btree_kill_iroot(cur);
3293 error = xfs_btree_dec_cursor(cur, level, stat);
3300 ASSERT(!xfs_btree_ptr_is_null(cur, &rptr) ||
3301 !xfs_btree_ptr_is_null(cur, &lptr));
3304 * Duplicate the cursor so our btree manipulations here won't
3305 * disrupt the next level up.
3307 error = xfs_btree_dup_cursor(cur, &tcur);
3312 * If there's a right sibling, see if it's ok to shift an entry
3315 if (!xfs_btree_ptr_is_null(cur, &rptr)) {
3317 * Move the temp cursor to the last entry in the next block.
3318 * Actually any entry but the first would suffice.
3320 i = xfs_btree_lastrec(tcur, level);
3321 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
3323 error = xfs_btree_increment(tcur, level, &i);
3326 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
3328 i = xfs_btree_lastrec(tcur, level);
3329 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
3331 /* Grab a pointer to the block. */
3332 right = xfs_btree_get_block(tcur, level, &rbp);
3334 error = xfs_btree_check_block(tcur, right, level, rbp);
3338 /* Grab the current block number, for future use. */
3339 xfs_btree_get_sibling(tcur, right, &cptr, XFS_BB_LEFTSIB);
3342 * If right block is full enough so that removing one entry
3343 * won't make it too empty, and left-shifting an entry out
3344 * of right to us works, we're done.
3346 if (xfs_btree_get_numrecs(right) - 1 >=
3347 cur->bc_ops->get_minrecs(tcur, level)) {
3348 error = xfs_btree_lshift(tcur, level, &i);
3352 ASSERT(xfs_btree_get_numrecs(block) >=
3353 cur->bc_ops->get_minrecs(tcur, level));
3355 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
3358 error = xfs_btree_dec_cursor(cur, level, stat);
3366 * Otherwise, grab the number of records in right for
3367 * future reference, and fix up the temp cursor to point
3368 * to our block again (last record).
3370 rrecs = xfs_btree_get_numrecs(right);
3371 if (!xfs_btree_ptr_is_null(cur, &lptr)) {
3372 i = xfs_btree_firstrec(tcur, level);
3373 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
3375 error = xfs_btree_decrement(tcur, level, &i);
3378 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
3383 * If there's a left sibling, see if it's ok to shift an entry
3386 if (!xfs_btree_ptr_is_null(cur, &lptr)) {
3388 * Move the temp cursor to the first entry in the
3391 i = xfs_btree_firstrec(tcur, level);
3392 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
3394 error = xfs_btree_decrement(tcur, level, &i);
3397 i = xfs_btree_firstrec(tcur, level);
3398 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
3400 /* Grab a pointer to the block. */
3401 left = xfs_btree_get_block(tcur, level, &lbp);
3403 error = xfs_btree_check_block(cur, left, level, lbp);
3407 /* Grab the current block number, for future use. */
3408 xfs_btree_get_sibling(tcur, left, &cptr, XFS_BB_RIGHTSIB);
3411 * If left block is full enough so that removing one entry
3412 * won't make it too empty, and right-shifting an entry out
3413 * of left to us works, we're done.
3415 if (xfs_btree_get_numrecs(left) - 1 >=
3416 cur->bc_ops->get_minrecs(tcur, level)) {
3417 error = xfs_btree_rshift(tcur, level, &i);
3421 ASSERT(xfs_btree_get_numrecs(block) >=
3422 cur->bc_ops->get_minrecs(tcur, level));
3423 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
3427 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3434 * Otherwise, grab the number of records in right for
3437 lrecs = xfs_btree_get_numrecs(left);
3440 /* Delete the temp cursor, we're done with it. */
3441 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
3444 /* If here, we need to do a join to keep the tree balanced. */
3445 ASSERT(!xfs_btree_ptr_is_null(cur, &cptr));
3447 if (!xfs_btree_ptr_is_null(cur, &lptr) &&
3448 lrecs + xfs_btree_get_numrecs(block) <=
3449 cur->bc_ops->get_maxrecs(cur, level)) {
3451 * Set "right" to be the starting block,
3452 * "left" to be the left neighbor.
3457 error = xfs_btree_read_buf_block(cur, &lptr, level,
3463 * If that won't work, see if we can join with the right neighbor block.
3465 } else if (!xfs_btree_ptr_is_null(cur, &rptr) &&
3466 rrecs + xfs_btree_get_numrecs(block) <=
3467 cur->bc_ops->get_maxrecs(cur, level)) {
3469 * Set "left" to be the starting block,
3470 * "right" to be the right neighbor.
3475 error = xfs_btree_read_buf_block(cur, &rptr, level,
3481 * Otherwise, we can't fix the imbalance.
3482 * Just return. This is probably a logic error, but it's not fatal.
3485 error = xfs_btree_dec_cursor(cur, level, stat);
3491 rrecs = xfs_btree_get_numrecs(right);
3492 lrecs = xfs_btree_get_numrecs(left);
3495 * We're now going to join "left" and "right" by moving all the stuff
3496 * in "right" to "left" and deleting "right".
3498 XFS_BTREE_STATS_ADD(cur, moves, rrecs);
3500 /* It's a non-leaf. Move keys and pointers. */
3501 union xfs_btree_key *lkp; /* left btree key */
3502 union xfs_btree_ptr *lpp; /* left address pointer */
3503 union xfs_btree_key *rkp; /* right btree key */
3504 union xfs_btree_ptr *rpp; /* right address pointer */
3506 lkp = xfs_btree_key_addr(cur, lrecs + 1, left);
3507 lpp = xfs_btree_ptr_addr(cur, lrecs + 1, left);
3508 rkp = xfs_btree_key_addr(cur, 1, right);
3509 rpp = xfs_btree_ptr_addr(cur, 1, right);
3511 for (i = 1; i < rrecs; i++) {
3512 error = xfs_btree_check_ptr(cur, rpp, i, level);
3517 xfs_btree_copy_keys(cur, lkp, rkp, rrecs);
3518 xfs_btree_copy_ptrs(cur, lpp, rpp, rrecs);
3520 xfs_btree_log_keys(cur, lbp, lrecs + 1, lrecs + rrecs);
3521 xfs_btree_log_ptrs(cur, lbp, lrecs + 1, lrecs + rrecs);
3523 /* It's a leaf. Move records. */
3524 union xfs_btree_rec *lrp; /* left record pointer */
3525 union xfs_btree_rec *rrp; /* right record pointer */
3527 lrp = xfs_btree_rec_addr(cur, lrecs + 1, left);
3528 rrp = xfs_btree_rec_addr(cur, 1, right);
3530 xfs_btree_copy_recs(cur, lrp, rrp, rrecs);
3531 xfs_btree_log_recs(cur, lbp, lrecs + 1, lrecs + rrecs);
3534 XFS_BTREE_STATS_INC(cur, join);
3537 * Fix up the the number of records and right block pointer in the
3538 * surviving block, and log it.
3540 xfs_btree_set_numrecs(left, lrecs + rrecs);
3541 xfs_btree_get_sibling(cur, right, &cptr, XFS_BB_RIGHTSIB),
3542 xfs_btree_set_sibling(cur, left, &cptr, XFS_BB_RIGHTSIB);
3543 xfs_btree_log_block(cur, lbp, XFS_BB_NUMRECS | XFS_BB_RIGHTSIB);
3545 /* If there is a right sibling, point it to the remaining block. */
3546 xfs_btree_get_sibling(cur, left, &cptr, XFS_BB_RIGHTSIB);
3547 if (!xfs_btree_ptr_is_null(cur, &cptr)) {
3548 error = xfs_btree_read_buf_block(cur, &cptr, level,
3549 0, &rrblock, &rrbp);
3552 xfs_btree_set_sibling(cur, rrblock, &lptr, XFS_BB_LEFTSIB);
3553 xfs_btree_log_block(cur, rrbp, XFS_BB_LEFTSIB);
3556 /* Free the deleted block. */
3557 error = cur->bc_ops->free_block(cur, rbp);
3560 XFS_BTREE_STATS_INC(cur, free);
3563 * If we joined with the left neighbor, set the buffer in the
3564 * cursor to the left block, and fix up the index.
3567 cur->bc_bufs[level] = lbp;
3568 cur->bc_ptrs[level] += lrecs;
3569 cur->bc_ra[level] = 0;
3572 * If we joined with the right neighbor and there's a level above
3573 * us, increment the cursor at that level.
3575 else if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) ||
3576 (level + 1 < cur->bc_nlevels)) {
3577 error = xfs_btree_increment(cur, level + 1, &i);
3583 * Readjust the ptr at this level if it's not a leaf, since it's
3584 * still pointing at the deletion point, which makes the cursor
3585 * inconsistent. If this makes the ptr 0, the caller fixes it up.
3586 * We can't use decrement because it would change the next level up.
3589 cur->bc_ptrs[level]--;
3591 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3592 /* Return value means the next level up has something to do. */
3597 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
3599 xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
3604 * Delete the record pointed to by cur.
3605 * The cursor refers to the place where the record was (could be inserted)
3606 * when the operation returns.
3610 struct xfs_btree_cur *cur,
3611 int *stat) /* success/failure */
3613 int error; /* error return value */
3617 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
3620 * Go up the tree, starting at leaf level.
3622 * If 2 is returned then a join was done; go to the next level.
3623 * Otherwise we are done.
3625 for (level = 0, i = 2; i == 2; level++) {
3626 error = xfs_btree_delrec(cur, level, &i);
3632 for (level = 1; level < cur->bc_nlevels; level++) {
3633 if (cur->bc_ptrs[level] == 0) {
3634 error = xfs_btree_decrement(cur, level, &i);
3642 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3646 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
3651 * Get the data from the pointed-to record.
3655 struct xfs_btree_cur *cur, /* btree cursor */
3656 union xfs_btree_rec **recp, /* output: btree record */
3657 int *stat) /* output: success/failure */
3659 struct xfs_btree_block *block; /* btree block */
3660 struct xfs_buf *bp; /* buffer pointer */
3661 int ptr; /* record number */
3663 int error; /* error return value */
3666 ptr = cur->bc_ptrs[0];
3667 block = xfs_btree_get_block(cur, 0, &bp);
3670 error = xfs_btree_check_block(cur, block, 0, bp);
3676 * Off the right end or left end, return failure.
3678 if (ptr > xfs_btree_get_numrecs(block) || ptr <= 0) {
3684 * Point to the record and extract its data.
3686 *recp = xfs_btree_rec_addr(cur, ptr, block);