RT-AC56 3.0.0.4.374.37 core
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / fs / xfs / xfs_btree.c
blob829af92f0fbadd0f4e311b8151055cc9e135e969
1 /*
2 * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
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
18 #include "xfs.h"
19 #include "xfs_fs.h"
20 #include "xfs_types.h"
21 #include "xfs_bit.h"
22 #include "xfs_log.h"
23 #include "xfs_inum.h"
24 #include "xfs_trans.h"
25 #include "xfs_sb.h"
26 #include "xfs_ag.h"
27 #include "xfs_mount.h"
28 #include "xfs_bmap_btree.h"
29 #include "xfs_alloc_btree.h"
30 #include "xfs_ialloc_btree.h"
31 #include "xfs_dinode.h"
32 #include "xfs_inode.h"
33 #include "xfs_inode_item.h"
34 #include "xfs_btree.h"
35 #include "xfs_btree_trace.h"
36 #include "xfs_error.h"
37 #include "xfs_trace.h"
40 * Cursor allocation zone.
42 kmem_zone_t *xfs_btree_cur_zone;
45 * Btree magic numbers.
47 const __uint32_t xfs_magics[XFS_BTNUM_MAX] = {
48 XFS_ABTB_MAGIC, XFS_ABTC_MAGIC, XFS_BMAP_MAGIC, XFS_IBT_MAGIC
52 STATIC int /* error (0 or EFSCORRUPTED) */
53 xfs_btree_check_lblock(
54 struct xfs_btree_cur *cur, /* btree cursor */
55 struct xfs_btree_block *block, /* btree long form block pointer */
56 int level, /* level of the btree block */
57 struct xfs_buf *bp) /* buffer for block, if any */
59 int lblock_ok; /* block passes checks */
60 struct xfs_mount *mp; /* file system mount point */
62 mp = cur->bc_mp;
63 lblock_ok =
64 be32_to_cpu(block->bb_magic) == xfs_magics[cur->bc_btnum] &&
65 be16_to_cpu(block->bb_level) == level &&
66 be16_to_cpu(block->bb_numrecs) <=
67 cur->bc_ops->get_maxrecs(cur, level) &&
68 block->bb_u.l.bb_leftsib &&
69 (be64_to_cpu(block->bb_u.l.bb_leftsib) == NULLDFSBNO ||
70 XFS_FSB_SANITY_CHECK(mp,
71 be64_to_cpu(block->bb_u.l.bb_leftsib))) &&
72 block->bb_u.l.bb_rightsib &&
73 (be64_to_cpu(block->bb_u.l.bb_rightsib) == NULLDFSBNO ||
74 XFS_FSB_SANITY_CHECK(mp,
75 be64_to_cpu(block->bb_u.l.bb_rightsib)));
76 if (unlikely(XFS_TEST_ERROR(!lblock_ok, mp,
77 XFS_ERRTAG_BTREE_CHECK_LBLOCK,
78 XFS_RANDOM_BTREE_CHECK_LBLOCK))) {
79 if (bp)
80 trace_xfs_btree_corrupt(bp, _RET_IP_);
81 XFS_ERROR_REPORT("xfs_btree_check_lblock", XFS_ERRLEVEL_LOW,
82 mp);
83 return XFS_ERROR(EFSCORRUPTED);
85 return 0;
88 STATIC int /* error (0 or EFSCORRUPTED) */
89 xfs_btree_check_sblock(
90 struct xfs_btree_cur *cur, /* btree cursor */
91 struct xfs_btree_block *block, /* btree short form block pointer */
92 int level, /* level of the btree block */
93 struct xfs_buf *bp) /* buffer containing block */
95 struct xfs_buf *agbp; /* buffer for ag. freespace struct */
96 struct xfs_agf *agf; /* ag. freespace structure */
97 xfs_agblock_t agflen; /* native ag. freespace length */
98 int sblock_ok; /* block passes checks */
100 agbp = cur->bc_private.a.agbp;
101 agf = XFS_BUF_TO_AGF(agbp);
102 agflen = be32_to_cpu(agf->agf_length);
103 sblock_ok =
104 be32_to_cpu(block->bb_magic) == xfs_magics[cur->bc_btnum] &&
105 be16_to_cpu(block->bb_level) == level &&
106 be16_to_cpu(block->bb_numrecs) <=
107 cur->bc_ops->get_maxrecs(cur, level) &&
108 (be32_to_cpu(block->bb_u.s.bb_leftsib) == NULLAGBLOCK ||
109 be32_to_cpu(block->bb_u.s.bb_leftsib) < agflen) &&
110 block->bb_u.s.bb_leftsib &&
111 (be32_to_cpu(block->bb_u.s.bb_rightsib) == NULLAGBLOCK ||
112 be32_to_cpu(block->bb_u.s.bb_rightsib) < agflen) &&
113 block->bb_u.s.bb_rightsib;
114 if (unlikely(XFS_TEST_ERROR(!sblock_ok, cur->bc_mp,
115 XFS_ERRTAG_BTREE_CHECK_SBLOCK,
116 XFS_RANDOM_BTREE_CHECK_SBLOCK))) {
117 if (bp)
118 trace_xfs_btree_corrupt(bp, _RET_IP_);
119 XFS_CORRUPTION_ERROR("xfs_btree_check_sblock",
120 XFS_ERRLEVEL_LOW, cur->bc_mp, block);
121 return XFS_ERROR(EFSCORRUPTED);
123 return 0;
127 * Debug routine: check that block header is ok.
130 xfs_btree_check_block(
131 struct xfs_btree_cur *cur, /* btree cursor */
132 struct xfs_btree_block *block, /* generic btree block pointer */
133 int level, /* level of the btree block */
134 struct xfs_buf *bp) /* buffer containing block, if any */
136 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
137 return xfs_btree_check_lblock(cur, block, level, bp);
138 else
139 return xfs_btree_check_sblock(cur, block, level, bp);
143 * Check that (long) pointer is ok.
145 int /* error (0 or EFSCORRUPTED) */
146 xfs_btree_check_lptr(
147 struct xfs_btree_cur *cur, /* btree cursor */
148 xfs_dfsbno_t bno, /* btree block disk address */
149 int level) /* btree block level */
151 XFS_WANT_CORRUPTED_RETURN(
152 level > 0 &&
153 bno != NULLDFSBNO &&
154 XFS_FSB_SANITY_CHECK(cur->bc_mp, bno));
155 return 0;
158 #ifdef DEBUG
160 * Check that (short) pointer is ok.
162 STATIC int /* error (0 or EFSCORRUPTED) */
163 xfs_btree_check_sptr(
164 struct xfs_btree_cur *cur, /* btree cursor */
165 xfs_agblock_t bno, /* btree block disk address */
166 int level) /* btree block level */
168 xfs_agblock_t agblocks = cur->bc_mp->m_sb.sb_agblocks;
170 XFS_WANT_CORRUPTED_RETURN(
171 level > 0 &&
172 bno != NULLAGBLOCK &&
173 bno != 0 &&
174 bno < agblocks);
175 return 0;
179 * Check that block ptr is ok.
181 STATIC int /* error (0 or EFSCORRUPTED) */
182 xfs_btree_check_ptr(
183 struct xfs_btree_cur *cur, /* btree cursor */
184 union xfs_btree_ptr *ptr, /* btree block disk address */
185 int index, /* offset from ptr to check */
186 int level) /* btree block level */
188 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
189 return xfs_btree_check_lptr(cur,
190 be64_to_cpu((&ptr->l)[index]), level);
191 } else {
192 return xfs_btree_check_sptr(cur,
193 be32_to_cpu((&ptr->s)[index]), level);
196 #endif
199 * Delete the btree cursor.
201 void
202 xfs_btree_del_cursor(
203 xfs_btree_cur_t *cur, /* btree cursor */
204 int error) /* del because of error */
206 int i; /* btree level */
209 * Clear the buffer pointers, and release the buffers.
210 * If we're doing this in the face of an error, we
211 * need to make sure to inspect all of the entries
212 * in the bc_bufs array for buffers to be unlocked.
213 * This is because some of the btree code works from
214 * level n down to 0, and if we get an error along
215 * the way we won't have initialized all the entries
216 * down to 0.
218 for (i = 0; i < cur->bc_nlevels; i++) {
219 if (cur->bc_bufs[i])
220 xfs_btree_setbuf(cur, i, NULL);
221 else if (!error)
222 break;
225 * Can't free a bmap cursor without having dealt with the
226 * allocated indirect blocks' accounting.
228 ASSERT(cur->bc_btnum != XFS_BTNUM_BMAP ||
229 cur->bc_private.b.allocated == 0);
231 * Free the cursor.
233 kmem_zone_free(xfs_btree_cur_zone, cur);
237 * Duplicate the btree cursor.
238 * Allocate a new one, copy the record, re-get the buffers.
240 int /* error */
241 xfs_btree_dup_cursor(
242 xfs_btree_cur_t *cur, /* input cursor */
243 xfs_btree_cur_t **ncur) /* output cursor */
245 xfs_buf_t *bp; /* btree block's buffer pointer */
246 int error; /* error return value */
247 int i; /* level number of btree block */
248 xfs_mount_t *mp; /* mount structure for filesystem */
249 xfs_btree_cur_t *new; /* new cursor value */
250 xfs_trans_t *tp; /* transaction pointer, can be NULL */
252 tp = cur->bc_tp;
253 mp = cur->bc_mp;
256 * Allocate a new cursor like the old one.
258 new = cur->bc_ops->dup_cursor(cur);
261 * Copy the record currently in the cursor.
263 new->bc_rec = cur->bc_rec;
266 * For each level current, re-get the buffer and copy the ptr value.
268 for (i = 0; i < new->bc_nlevels; i++) {
269 new->bc_ptrs[i] = cur->bc_ptrs[i];
270 new->bc_ra[i] = cur->bc_ra[i];
271 if ((bp = cur->bc_bufs[i])) {
272 if ((error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp,
273 XFS_BUF_ADDR(bp), mp->m_bsize, 0, &bp))) {
274 xfs_btree_del_cursor(new, error);
275 *ncur = NULL;
276 return error;
278 new->bc_bufs[i] = bp;
279 ASSERT(bp);
280 ASSERT(!XFS_BUF_GETERROR(bp));
281 } else
282 new->bc_bufs[i] = NULL;
284 *ncur = new;
285 return 0;
289 * XFS btree block layout and addressing:
291 * There are two types of blocks in the btree: leaf and non-leaf blocks.
293 * The leaf record start with a header then followed by records containing
294 * the values. A non-leaf block also starts with the same header, and
295 * then first contains lookup keys followed by an equal number of pointers
296 * to the btree blocks at the previous level.
298 * +--------+-------+-------+-------+-------+-------+-------+
299 * Leaf: | header | rec 1 | rec 2 | rec 3 | rec 4 | rec 5 | rec N |
300 * +--------+-------+-------+-------+-------+-------+-------+
302 * +--------+-------+-------+-------+-------+-------+-------+
303 * Non-Leaf: | header | key 1 | key 2 | key N | ptr 1 | ptr 2 | ptr N |
304 * +--------+-------+-------+-------+-------+-------+-------+
306 * The header is called struct xfs_btree_block for reasons better left unknown
307 * and comes in different versions for short (32bit) and long (64bit) block
308 * pointers. The record and key structures are defined by the btree instances
309 * and opaque to the btree core. The block pointers are simple disk endian
310 * integers, available in a short (32bit) and long (64bit) variant.
312 * The helpers below calculate the offset of a given record, key or pointer
313 * into a btree block (xfs_btree_*_offset) or return a pointer to the given
314 * record, key or pointer (xfs_btree_*_addr). Note that all addressing
315 * inside the btree block is done using indices starting at one, not zero!
319 * Return size of the btree block header for this btree instance.
321 static inline size_t xfs_btree_block_len(struct xfs_btree_cur *cur)
323 return (cur->bc_flags & XFS_BTREE_LONG_PTRS) ?
324 XFS_BTREE_LBLOCK_LEN :
325 XFS_BTREE_SBLOCK_LEN;
329 * Return size of btree block pointers for this btree instance.
331 static inline size_t xfs_btree_ptr_len(struct xfs_btree_cur *cur)
333 return (cur->bc_flags & XFS_BTREE_LONG_PTRS) ?
334 sizeof(__be64) : sizeof(__be32);
338 * Calculate offset of the n-th record in a btree block.
340 STATIC size_t
341 xfs_btree_rec_offset(
342 struct xfs_btree_cur *cur,
343 int n)
345 return xfs_btree_block_len(cur) +
346 (n - 1) * cur->bc_ops->rec_len;
350 * Calculate offset of the n-th key in a btree block.
352 STATIC size_t
353 xfs_btree_key_offset(
354 struct xfs_btree_cur *cur,
355 int n)
357 return xfs_btree_block_len(cur) +
358 (n - 1) * cur->bc_ops->key_len;
362 * Calculate offset of the n-th block pointer in a btree block.
364 STATIC size_t
365 xfs_btree_ptr_offset(
366 struct xfs_btree_cur *cur,
367 int n,
368 int level)
370 return xfs_btree_block_len(cur) +
371 cur->bc_ops->get_maxrecs(cur, level) * cur->bc_ops->key_len +
372 (n - 1) * xfs_btree_ptr_len(cur);
376 * Return a pointer to the n-th record in the btree block.
378 STATIC union xfs_btree_rec *
379 xfs_btree_rec_addr(
380 struct xfs_btree_cur *cur,
381 int n,
382 struct xfs_btree_block *block)
384 return (union xfs_btree_rec *)
385 ((char *)block + xfs_btree_rec_offset(cur, n));
389 * Return a pointer to the n-th key in the btree block.
391 STATIC union xfs_btree_key *
392 xfs_btree_key_addr(
393 struct xfs_btree_cur *cur,
394 int n,
395 struct xfs_btree_block *block)
397 return (union xfs_btree_key *)
398 ((char *)block + xfs_btree_key_offset(cur, n));
402 * Return a pointer to the n-th block pointer in the btree block.
404 STATIC union xfs_btree_ptr *
405 xfs_btree_ptr_addr(
406 struct xfs_btree_cur *cur,
407 int n,
408 struct xfs_btree_block *block)
410 int level = xfs_btree_get_level(block);
412 ASSERT(block->bb_level != 0);
414 return (union xfs_btree_ptr *)
415 ((char *)block + xfs_btree_ptr_offset(cur, n, level));
419 * Get a the root block which is stored in the inode.
421 * For now this btree implementation assumes the btree root is always
422 * stored in the if_broot field of an inode fork.
424 STATIC struct xfs_btree_block *
425 xfs_btree_get_iroot(
426 struct xfs_btree_cur *cur)
428 struct xfs_ifork *ifp;
430 ifp = XFS_IFORK_PTR(cur->bc_private.b.ip, cur->bc_private.b.whichfork);
431 return (struct xfs_btree_block *)ifp->if_broot;
435 * Retrieve the block pointer from the cursor at the given level.
436 * This may be an inode btree root or from a buffer.
438 STATIC struct xfs_btree_block * /* generic btree block pointer */
439 xfs_btree_get_block(
440 struct xfs_btree_cur *cur, /* btree cursor */
441 int level, /* level in btree */
442 struct xfs_buf **bpp) /* buffer containing the block */
444 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
445 (level == cur->bc_nlevels - 1)) {
446 *bpp = NULL;
447 return xfs_btree_get_iroot(cur);
450 *bpp = cur->bc_bufs[level];
451 return XFS_BUF_TO_BLOCK(*bpp);
455 * Get a buffer for the block, return it with no data read.
456 * Long-form addressing.
458 xfs_buf_t * /* buffer for fsbno */
459 xfs_btree_get_bufl(
460 xfs_mount_t *mp, /* file system mount point */
461 xfs_trans_t *tp, /* transaction pointer */
462 xfs_fsblock_t fsbno, /* file system block number */
463 uint lock) /* lock flags for get_buf */
465 xfs_buf_t *bp; /* buffer pointer (return value) */
466 xfs_daddr_t d; /* real disk block address */
468 ASSERT(fsbno != NULLFSBLOCK);
469 d = XFS_FSB_TO_DADDR(mp, fsbno);
470 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d, mp->m_bsize, lock);
471 ASSERT(bp);
472 ASSERT(!XFS_BUF_GETERROR(bp));
473 return bp;
477 * Get a buffer for the block, return it with no data read.
478 * Short-form addressing.
480 xfs_buf_t * /* buffer for agno/agbno */
481 xfs_btree_get_bufs(
482 xfs_mount_t *mp, /* file system mount point */
483 xfs_trans_t *tp, /* transaction pointer */
484 xfs_agnumber_t agno, /* allocation group number */
485 xfs_agblock_t agbno, /* allocation group block number */
486 uint lock) /* lock flags for get_buf */
488 xfs_buf_t *bp; /* buffer pointer (return value) */
489 xfs_daddr_t d; /* real disk block address */
491 ASSERT(agno != NULLAGNUMBER);
492 ASSERT(agbno != NULLAGBLOCK);
493 d = XFS_AGB_TO_DADDR(mp, agno, agbno);
494 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d, mp->m_bsize, lock);
495 ASSERT(bp);
496 ASSERT(!XFS_BUF_GETERROR(bp));
497 return bp;
501 * Check for the cursor referring to the last block at the given level.
503 int /* 1=is last block, 0=not last block */
504 xfs_btree_islastblock(
505 xfs_btree_cur_t *cur, /* btree cursor */
506 int level) /* level to check */
508 struct xfs_btree_block *block; /* generic btree block pointer */
509 xfs_buf_t *bp; /* buffer containing block */
511 block = xfs_btree_get_block(cur, level, &bp);
512 xfs_btree_check_block(cur, block, level, bp);
513 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
514 return be64_to_cpu(block->bb_u.l.bb_rightsib) == NULLDFSBNO;
515 else
516 return be32_to_cpu(block->bb_u.s.bb_rightsib) == NULLAGBLOCK;
520 * Change the cursor to point to the first record at the given level.
521 * Other levels are unaffected.
523 STATIC int /* success=1, failure=0 */
524 xfs_btree_firstrec(
525 xfs_btree_cur_t *cur, /* btree cursor */
526 int level) /* level to change */
528 struct xfs_btree_block *block; /* generic btree block pointer */
529 xfs_buf_t *bp; /* buffer containing block */
532 * Get the block pointer for this level.
534 block = xfs_btree_get_block(cur, level, &bp);
535 xfs_btree_check_block(cur, block, level, bp);
537 * It's empty, there is no such record.
539 if (!block->bb_numrecs)
540 return 0;
542 * Set the ptr value to 1, that's the first record/key.
544 cur->bc_ptrs[level] = 1;
545 return 1;
549 * Change the cursor to point to the last record in the current block
550 * at the given level. Other levels are unaffected.
552 STATIC int /* success=1, failure=0 */
553 xfs_btree_lastrec(
554 xfs_btree_cur_t *cur, /* btree cursor */
555 int level) /* level to change */
557 struct xfs_btree_block *block; /* generic btree block pointer */
558 xfs_buf_t *bp; /* buffer containing block */
561 * Get the block pointer for this level.
563 block = xfs_btree_get_block(cur, level, &bp);
564 xfs_btree_check_block(cur, block, level, bp);
566 * It's empty, there is no such record.
568 if (!block->bb_numrecs)
569 return 0;
571 * Set the ptr value to numrecs, that's the last record/key.
573 cur->bc_ptrs[level] = be16_to_cpu(block->bb_numrecs);
574 return 1;
578 * Compute first and last byte offsets for the fields given.
579 * Interprets the offsets table, which contains struct field offsets.
581 void
582 xfs_btree_offsets(
583 __int64_t fields, /* bitmask of fields */
584 const short *offsets, /* table of field offsets */
585 int nbits, /* number of bits to inspect */
586 int *first, /* output: first byte offset */
587 int *last) /* output: last byte offset */
589 int i; /* current bit number */
590 __int64_t imask; /* mask for current bit number */
592 ASSERT(fields != 0);
594 * Find the lowest bit, so the first byte offset.
596 for (i = 0, imask = 1LL; ; i++, imask <<= 1) {
597 if (imask & fields) {
598 *first = offsets[i];
599 break;
603 * Find the highest bit, so the last byte offset.
605 for (i = nbits - 1, imask = 1LL << i; ; i--, imask >>= 1) {
606 if (imask & fields) {
607 *last = offsets[i + 1] - 1;
608 break;
614 * Get a buffer for the block, return it read in.
615 * Long-form addressing.
617 int /* error */
618 xfs_btree_read_bufl(
619 xfs_mount_t *mp, /* file system mount point */
620 xfs_trans_t *tp, /* transaction pointer */
621 xfs_fsblock_t fsbno, /* file system block number */
622 uint lock, /* lock flags for read_buf */
623 xfs_buf_t **bpp, /* buffer for fsbno */
624 int refval) /* ref count value for buffer */
626 xfs_buf_t *bp; /* return value */
627 xfs_daddr_t d; /* real disk block address */
628 int error;
630 ASSERT(fsbno != NULLFSBLOCK);
631 d = XFS_FSB_TO_DADDR(mp, fsbno);
632 if ((error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, d,
633 mp->m_bsize, lock, &bp))) {
634 return error;
636 ASSERT(!bp || !XFS_BUF_GETERROR(bp));
637 if (bp != NULL) {
638 XFS_BUF_SET_VTYPE_REF(bp, B_FS_MAP, refval);
640 *bpp = bp;
641 return 0;
645 * Read-ahead the block, don't wait for it, don't return a buffer.
646 * Long-form addressing.
648 /* ARGSUSED */
649 void
650 xfs_btree_reada_bufl(
651 xfs_mount_t *mp, /* file system mount point */
652 xfs_fsblock_t fsbno, /* file system block number */
653 xfs_extlen_t count) /* count of filesystem blocks */
655 xfs_daddr_t d;
657 ASSERT(fsbno != NULLFSBLOCK);
658 d = XFS_FSB_TO_DADDR(mp, fsbno);
659 xfs_baread(mp->m_ddev_targp, d, mp->m_bsize * count);
663 * Read-ahead the block, don't wait for it, don't return a buffer.
664 * Short-form addressing.
666 /* ARGSUSED */
667 void
668 xfs_btree_reada_bufs(
669 xfs_mount_t *mp, /* file system mount point */
670 xfs_agnumber_t agno, /* allocation group number */
671 xfs_agblock_t agbno, /* allocation group block number */
672 xfs_extlen_t count) /* count of filesystem blocks */
674 xfs_daddr_t d;
676 ASSERT(agno != NULLAGNUMBER);
677 ASSERT(agbno != NULLAGBLOCK);
678 d = XFS_AGB_TO_DADDR(mp, agno, agbno);
679 xfs_baread(mp->m_ddev_targp, d, mp->m_bsize * count);
682 STATIC int
683 xfs_btree_readahead_lblock(
684 struct xfs_btree_cur *cur,
685 int lr,
686 struct xfs_btree_block *block)
688 int rval = 0;
689 xfs_dfsbno_t left = be64_to_cpu(block->bb_u.l.bb_leftsib);
690 xfs_dfsbno_t right = be64_to_cpu(block->bb_u.l.bb_rightsib);
692 if ((lr & XFS_BTCUR_LEFTRA) && left != NULLDFSBNO) {
693 xfs_btree_reada_bufl(cur->bc_mp, left, 1);
694 rval++;
697 if ((lr & XFS_BTCUR_RIGHTRA) && right != NULLDFSBNO) {
698 xfs_btree_reada_bufl(cur->bc_mp, right, 1);
699 rval++;
702 return rval;
705 STATIC int
706 xfs_btree_readahead_sblock(
707 struct xfs_btree_cur *cur,
708 int lr,
709 struct xfs_btree_block *block)
711 int rval = 0;
712 xfs_agblock_t left = be32_to_cpu(block->bb_u.s.bb_leftsib);
713 xfs_agblock_t right = be32_to_cpu(block->bb_u.s.bb_rightsib);
716 if ((lr & XFS_BTCUR_LEFTRA) && left != NULLAGBLOCK) {
717 xfs_btree_reada_bufs(cur->bc_mp, cur->bc_private.a.agno,
718 left, 1);
719 rval++;
722 if ((lr & XFS_BTCUR_RIGHTRA) && right != NULLAGBLOCK) {
723 xfs_btree_reada_bufs(cur->bc_mp, cur->bc_private.a.agno,
724 right, 1);
725 rval++;
728 return rval;
732 * Read-ahead btree blocks, at the given level.
733 * Bits in lr are set from XFS_BTCUR_{LEFT,RIGHT}RA.
735 STATIC int
736 xfs_btree_readahead(
737 struct xfs_btree_cur *cur, /* btree cursor */
738 int lev, /* level in btree */
739 int lr) /* left/right bits */
741 struct xfs_btree_block *block;
744 * No readahead needed if we are at the root level and the
745 * btree root is stored in the inode.
747 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
748 (lev == cur->bc_nlevels - 1))
749 return 0;
751 if ((cur->bc_ra[lev] | lr) == cur->bc_ra[lev])
752 return 0;
754 cur->bc_ra[lev] |= lr;
755 block = XFS_BUF_TO_BLOCK(cur->bc_bufs[lev]);
757 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
758 return xfs_btree_readahead_lblock(cur, lr, block);
759 return xfs_btree_readahead_sblock(cur, lr, block);
763 * Set the buffer for level "lev" in the cursor to bp, releasing
764 * any previous buffer.
766 void
767 xfs_btree_setbuf(
768 xfs_btree_cur_t *cur, /* btree cursor */
769 int lev, /* level in btree */
770 xfs_buf_t *bp) /* new buffer to set */
772 struct xfs_btree_block *b; /* btree block */
773 xfs_buf_t *obp; /* old buffer pointer */
775 obp = cur->bc_bufs[lev];
776 if (obp)
777 xfs_trans_brelse(cur->bc_tp, obp);
778 cur->bc_bufs[lev] = bp;
779 cur->bc_ra[lev] = 0;
780 if (!bp)
781 return;
782 b = XFS_BUF_TO_BLOCK(bp);
783 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
784 if (be64_to_cpu(b->bb_u.l.bb_leftsib) == NULLDFSBNO)
785 cur->bc_ra[lev] |= XFS_BTCUR_LEFTRA;
786 if (be64_to_cpu(b->bb_u.l.bb_rightsib) == NULLDFSBNO)
787 cur->bc_ra[lev] |= XFS_BTCUR_RIGHTRA;
788 } else {
789 if (be32_to_cpu(b->bb_u.s.bb_leftsib) == NULLAGBLOCK)
790 cur->bc_ra[lev] |= XFS_BTCUR_LEFTRA;
791 if (be32_to_cpu(b->bb_u.s.bb_rightsib) == NULLAGBLOCK)
792 cur->bc_ra[lev] |= XFS_BTCUR_RIGHTRA;
796 STATIC int
797 xfs_btree_ptr_is_null(
798 struct xfs_btree_cur *cur,
799 union xfs_btree_ptr *ptr)
801 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
802 return be64_to_cpu(ptr->l) == NULLDFSBNO;
803 else
804 return be32_to_cpu(ptr->s) == NULLAGBLOCK;
807 STATIC void
808 xfs_btree_set_ptr_null(
809 struct xfs_btree_cur *cur,
810 union xfs_btree_ptr *ptr)
812 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
813 ptr->l = cpu_to_be64(NULLDFSBNO);
814 else
815 ptr->s = cpu_to_be32(NULLAGBLOCK);
819 * Get/set/init sibling pointers
821 STATIC void
822 xfs_btree_get_sibling(
823 struct xfs_btree_cur *cur,
824 struct xfs_btree_block *block,
825 union xfs_btree_ptr *ptr,
826 int lr)
828 ASSERT(lr == XFS_BB_LEFTSIB || lr == XFS_BB_RIGHTSIB);
830 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
831 if (lr == XFS_BB_RIGHTSIB)
832 ptr->l = block->bb_u.l.bb_rightsib;
833 else
834 ptr->l = block->bb_u.l.bb_leftsib;
835 } else {
836 if (lr == XFS_BB_RIGHTSIB)
837 ptr->s = block->bb_u.s.bb_rightsib;
838 else
839 ptr->s = block->bb_u.s.bb_leftsib;
843 STATIC void
844 xfs_btree_set_sibling(
845 struct xfs_btree_cur *cur,
846 struct xfs_btree_block *block,
847 union xfs_btree_ptr *ptr,
848 int lr)
850 ASSERT(lr == XFS_BB_LEFTSIB || lr == XFS_BB_RIGHTSIB);
852 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
853 if (lr == XFS_BB_RIGHTSIB)
854 block->bb_u.l.bb_rightsib = ptr->l;
855 else
856 block->bb_u.l.bb_leftsib = ptr->l;
857 } else {
858 if (lr == XFS_BB_RIGHTSIB)
859 block->bb_u.s.bb_rightsib = ptr->s;
860 else
861 block->bb_u.s.bb_leftsib = ptr->s;
865 STATIC void
866 xfs_btree_init_block(
867 struct xfs_btree_cur *cur,
868 int level,
869 int numrecs,
870 struct xfs_btree_block *new) /* new block */
872 new->bb_magic = cpu_to_be32(xfs_magics[cur->bc_btnum]);
873 new->bb_level = cpu_to_be16(level);
874 new->bb_numrecs = cpu_to_be16(numrecs);
876 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
877 new->bb_u.l.bb_leftsib = cpu_to_be64(NULLDFSBNO);
878 new->bb_u.l.bb_rightsib = cpu_to_be64(NULLDFSBNO);
879 } else {
880 new->bb_u.s.bb_leftsib = cpu_to_be32(NULLAGBLOCK);
881 new->bb_u.s.bb_rightsib = cpu_to_be32(NULLAGBLOCK);
886 * Return true if ptr is the last record in the btree and
887 * we need to track updateѕ to this record. The decision
888 * will be further refined in the update_lastrec method.
890 STATIC int
891 xfs_btree_is_lastrec(
892 struct xfs_btree_cur *cur,
893 struct xfs_btree_block *block,
894 int level)
896 union xfs_btree_ptr ptr;
898 if (level > 0)
899 return 0;
900 if (!(cur->bc_flags & XFS_BTREE_LASTREC_UPDATE))
901 return 0;
903 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_RIGHTSIB);
904 if (!xfs_btree_ptr_is_null(cur, &ptr))
905 return 0;
906 return 1;
909 STATIC void
910 xfs_btree_buf_to_ptr(
911 struct xfs_btree_cur *cur,
912 struct xfs_buf *bp,
913 union xfs_btree_ptr *ptr)
915 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
916 ptr->l = cpu_to_be64(XFS_DADDR_TO_FSB(cur->bc_mp,
917 XFS_BUF_ADDR(bp)));
918 else {
919 ptr->s = cpu_to_be32(xfs_daddr_to_agbno(cur->bc_mp,
920 XFS_BUF_ADDR(bp)));
924 STATIC xfs_daddr_t
925 xfs_btree_ptr_to_daddr(
926 struct xfs_btree_cur *cur,
927 union xfs_btree_ptr *ptr)
929 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
930 ASSERT(be64_to_cpu(ptr->l) != NULLDFSBNO);
932 return XFS_FSB_TO_DADDR(cur->bc_mp, be64_to_cpu(ptr->l));
933 } else {
934 ASSERT(cur->bc_private.a.agno != NULLAGNUMBER);
935 ASSERT(be32_to_cpu(ptr->s) != NULLAGBLOCK);
937 return XFS_AGB_TO_DADDR(cur->bc_mp, cur->bc_private.a.agno,
938 be32_to_cpu(ptr->s));
942 STATIC void
943 xfs_btree_set_refs(
944 struct xfs_btree_cur *cur,
945 struct xfs_buf *bp)
947 switch (cur->bc_btnum) {
948 case XFS_BTNUM_BNO:
949 case XFS_BTNUM_CNT:
950 XFS_BUF_SET_VTYPE_REF(*bpp, B_FS_MAP, XFS_ALLOC_BTREE_REF);
951 break;
952 case XFS_BTNUM_INO:
953 XFS_BUF_SET_VTYPE_REF(*bpp, B_FS_INOMAP, XFS_INO_BTREE_REF);
954 break;
955 case XFS_BTNUM_BMAP:
956 XFS_BUF_SET_VTYPE_REF(*bpp, B_FS_MAP, XFS_BMAP_BTREE_REF);
957 break;
958 default:
959 ASSERT(0);
963 STATIC int
964 xfs_btree_get_buf_block(
965 struct xfs_btree_cur *cur,
966 union xfs_btree_ptr *ptr,
967 int flags,
968 struct xfs_btree_block **block,
969 struct xfs_buf **bpp)
971 struct xfs_mount *mp = cur->bc_mp;
972 xfs_daddr_t d;
974 /* need to sort out how callers deal with failures first */
975 ASSERT(!(flags & XBF_TRYLOCK));
977 d = xfs_btree_ptr_to_daddr(cur, ptr);
978 *bpp = xfs_trans_get_buf(cur->bc_tp, mp->m_ddev_targp, d,
979 mp->m_bsize, flags);
981 ASSERT(*bpp);
982 ASSERT(!XFS_BUF_GETERROR(*bpp));
984 *block = XFS_BUF_TO_BLOCK(*bpp);
985 return 0;
989 * Read in the buffer at the given ptr and return the buffer and
990 * the block pointer within the buffer.
992 STATIC int
993 xfs_btree_read_buf_block(
994 struct xfs_btree_cur *cur,
995 union xfs_btree_ptr *ptr,
996 int level,
997 int flags,
998 struct xfs_btree_block **block,
999 struct xfs_buf **bpp)
1001 struct xfs_mount *mp = cur->bc_mp;
1002 xfs_daddr_t d;
1003 int error;
1005 /* need to sort out how callers deal with failures first */
1006 ASSERT(!(flags & XBF_TRYLOCK));
1008 d = xfs_btree_ptr_to_daddr(cur, ptr);
1009 error = xfs_trans_read_buf(mp, cur->bc_tp, mp->m_ddev_targp, d,
1010 mp->m_bsize, flags, bpp);
1011 if (error)
1012 return error;
1014 ASSERT(*bpp != NULL);
1015 ASSERT(!XFS_BUF_GETERROR(*bpp));
1017 xfs_btree_set_refs(cur, *bpp);
1018 *block = XFS_BUF_TO_BLOCK(*bpp);
1020 error = xfs_btree_check_block(cur, *block, level, *bpp);
1021 if (error)
1022 xfs_trans_brelse(cur->bc_tp, *bpp);
1023 return error;
1027 * Copy keys from one btree block to another.
1029 STATIC void
1030 xfs_btree_copy_keys(
1031 struct xfs_btree_cur *cur,
1032 union xfs_btree_key *dst_key,
1033 union xfs_btree_key *src_key,
1034 int numkeys)
1036 ASSERT(numkeys >= 0);
1037 memcpy(dst_key, src_key, numkeys * cur->bc_ops->key_len);
1041 * Copy records from one btree block to another.
1043 STATIC void
1044 xfs_btree_copy_recs(
1045 struct xfs_btree_cur *cur,
1046 union xfs_btree_rec *dst_rec,
1047 union xfs_btree_rec *src_rec,
1048 int numrecs)
1050 ASSERT(numrecs >= 0);
1051 memcpy(dst_rec, src_rec, numrecs * cur->bc_ops->rec_len);
1055 * Copy block pointers from one btree block to another.
1057 STATIC void
1058 xfs_btree_copy_ptrs(
1059 struct xfs_btree_cur *cur,
1060 union xfs_btree_ptr *dst_ptr,
1061 union xfs_btree_ptr *src_ptr,
1062 int numptrs)
1064 ASSERT(numptrs >= 0);
1065 memcpy(dst_ptr, src_ptr, numptrs * xfs_btree_ptr_len(cur));
1069 * Shift keys one index left/right inside a single btree block.
1071 STATIC void
1072 xfs_btree_shift_keys(
1073 struct xfs_btree_cur *cur,
1074 union xfs_btree_key *key,
1075 int dir,
1076 int numkeys)
1078 char *dst_key;
1080 ASSERT(numkeys >= 0);
1081 ASSERT(dir == 1 || dir == -1);
1083 dst_key = (char *)key + (dir * cur->bc_ops->key_len);
1084 memmove(dst_key, key, numkeys * cur->bc_ops->key_len);
1088 * Shift records one index left/right inside a single btree block.
1090 STATIC void
1091 xfs_btree_shift_recs(
1092 struct xfs_btree_cur *cur,
1093 union xfs_btree_rec *rec,
1094 int dir,
1095 int numrecs)
1097 char *dst_rec;
1099 ASSERT(numrecs >= 0);
1100 ASSERT(dir == 1 || dir == -1);
1102 dst_rec = (char *)rec + (dir * cur->bc_ops->rec_len);
1103 memmove(dst_rec, rec, numrecs * cur->bc_ops->rec_len);
1107 * Shift block pointers one index left/right inside a single btree block.
1109 STATIC void
1110 xfs_btree_shift_ptrs(
1111 struct xfs_btree_cur *cur,
1112 union xfs_btree_ptr *ptr,
1113 int dir,
1114 int numptrs)
1116 char *dst_ptr;
1118 ASSERT(numptrs >= 0);
1119 ASSERT(dir == 1 || dir == -1);
1121 dst_ptr = (char *)ptr + (dir * xfs_btree_ptr_len(cur));
1122 memmove(dst_ptr, ptr, numptrs * xfs_btree_ptr_len(cur));
1126 * Log key values from the btree block.
1128 STATIC void
1129 xfs_btree_log_keys(
1130 struct xfs_btree_cur *cur,
1131 struct xfs_buf *bp,
1132 int first,
1133 int last)
1135 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1136 XFS_BTREE_TRACE_ARGBII(cur, bp, first, last);
1138 if (bp) {
1139 xfs_trans_log_buf(cur->bc_tp, bp,
1140 xfs_btree_key_offset(cur, first),
1141 xfs_btree_key_offset(cur, last + 1) - 1);
1142 } else {
1143 xfs_trans_log_inode(cur->bc_tp, cur->bc_private.b.ip,
1144 xfs_ilog_fbroot(cur->bc_private.b.whichfork));
1147 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1151 * Log record values from the btree block.
1153 void
1154 xfs_btree_log_recs(
1155 struct xfs_btree_cur *cur,
1156 struct xfs_buf *bp,
1157 int first,
1158 int last)
1160 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1161 XFS_BTREE_TRACE_ARGBII(cur, bp, first, last);
1163 xfs_trans_log_buf(cur->bc_tp, bp,
1164 xfs_btree_rec_offset(cur, first),
1165 xfs_btree_rec_offset(cur, last + 1) - 1);
1167 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1171 * Log block pointer fields from a btree block (nonleaf).
1173 STATIC void
1174 xfs_btree_log_ptrs(
1175 struct xfs_btree_cur *cur, /* btree cursor */
1176 struct xfs_buf *bp, /* buffer containing btree block */
1177 int first, /* index of first pointer to log */
1178 int last) /* index of last pointer to log */
1180 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1181 XFS_BTREE_TRACE_ARGBII(cur, bp, first, last);
1183 if (bp) {
1184 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
1185 int level = xfs_btree_get_level(block);
1187 xfs_trans_log_buf(cur->bc_tp, bp,
1188 xfs_btree_ptr_offset(cur, first, level),
1189 xfs_btree_ptr_offset(cur, last + 1, level) - 1);
1190 } else {
1191 xfs_trans_log_inode(cur->bc_tp, cur->bc_private.b.ip,
1192 xfs_ilog_fbroot(cur->bc_private.b.whichfork));
1195 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1199 * Log fields from a btree block header.
1201 void
1202 xfs_btree_log_block(
1203 struct xfs_btree_cur *cur, /* btree cursor */
1204 struct xfs_buf *bp, /* buffer containing btree block */
1205 int fields) /* mask of fields: XFS_BB_... */
1207 int first; /* first byte offset logged */
1208 int last; /* last byte offset logged */
1209 static const short soffsets[] = { /* table of offsets (short) */
1210 offsetof(struct xfs_btree_block, bb_magic),
1211 offsetof(struct xfs_btree_block, bb_level),
1212 offsetof(struct xfs_btree_block, bb_numrecs),
1213 offsetof(struct xfs_btree_block, bb_u.s.bb_leftsib),
1214 offsetof(struct xfs_btree_block, bb_u.s.bb_rightsib),
1215 XFS_BTREE_SBLOCK_LEN
1217 static const short loffsets[] = { /* table of offsets (long) */
1218 offsetof(struct xfs_btree_block, bb_magic),
1219 offsetof(struct xfs_btree_block, bb_level),
1220 offsetof(struct xfs_btree_block, bb_numrecs),
1221 offsetof(struct xfs_btree_block, bb_u.l.bb_leftsib),
1222 offsetof(struct xfs_btree_block, bb_u.l.bb_rightsib),
1223 XFS_BTREE_LBLOCK_LEN
1226 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1227 XFS_BTREE_TRACE_ARGBI(cur, bp, fields);
1229 if (bp) {
1230 xfs_btree_offsets(fields,
1231 (cur->bc_flags & XFS_BTREE_LONG_PTRS) ?
1232 loffsets : soffsets,
1233 XFS_BB_NUM_BITS, &first, &last);
1234 xfs_trans_log_buf(cur->bc_tp, bp, first, last);
1235 } else {
1236 xfs_trans_log_inode(cur->bc_tp, cur->bc_private.b.ip,
1237 xfs_ilog_fbroot(cur->bc_private.b.whichfork));
1240 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1244 * Increment cursor by one record at the level.
1245 * For nonzero levels the leaf-ward information is untouched.
1247 int /* error */
1248 xfs_btree_increment(
1249 struct xfs_btree_cur *cur,
1250 int level,
1251 int *stat) /* success/failure */
1253 struct xfs_btree_block *block;
1254 union xfs_btree_ptr ptr;
1255 struct xfs_buf *bp;
1256 int error; /* error return value */
1257 int lev;
1259 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1260 XFS_BTREE_TRACE_ARGI(cur, level);
1262 ASSERT(level < cur->bc_nlevels);
1264 /* Read-ahead to the right at this level. */
1265 xfs_btree_readahead(cur, level, XFS_BTCUR_RIGHTRA);
1267 /* Get a pointer to the btree block. */
1268 block = xfs_btree_get_block(cur, level, &bp);
1270 #ifdef DEBUG
1271 error = xfs_btree_check_block(cur, block, level, bp);
1272 if (error)
1273 goto error0;
1274 #endif
1276 /* We're done if we remain in the block after the increment. */
1277 if (++cur->bc_ptrs[level] <= xfs_btree_get_numrecs(block))
1278 goto out1;
1280 /* Fail if we just went off the right edge of the tree. */
1281 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_RIGHTSIB);
1282 if (xfs_btree_ptr_is_null(cur, &ptr))
1283 goto out0;
1285 XFS_BTREE_STATS_INC(cur, increment);
1288 * March up the tree incrementing pointers.
1289 * Stop when we don't go off the right edge of a block.
1291 for (lev = level + 1; lev < cur->bc_nlevels; lev++) {
1292 block = xfs_btree_get_block(cur, lev, &bp);
1294 #ifdef DEBUG
1295 error = xfs_btree_check_block(cur, block, lev, bp);
1296 if (error)
1297 goto error0;
1298 #endif
1300 if (++cur->bc_ptrs[lev] <= xfs_btree_get_numrecs(block))
1301 break;
1303 /* Read-ahead the right block for the next loop. */
1304 xfs_btree_readahead(cur, lev, XFS_BTCUR_RIGHTRA);
1308 * If we went off the root then we are either seriously
1309 * confused or have the tree root in an inode.
1311 if (lev == cur->bc_nlevels) {
1312 if (cur->bc_flags & XFS_BTREE_ROOT_IN_INODE)
1313 goto out0;
1314 ASSERT(0);
1315 error = EFSCORRUPTED;
1316 goto error0;
1318 ASSERT(lev < cur->bc_nlevels);
1321 * Now walk back down the tree, fixing up the cursor's buffer
1322 * pointers and key numbers.
1324 for (block = xfs_btree_get_block(cur, lev, &bp); lev > level; ) {
1325 union xfs_btree_ptr *ptrp;
1327 ptrp = xfs_btree_ptr_addr(cur, cur->bc_ptrs[lev], block);
1328 error = xfs_btree_read_buf_block(cur, ptrp, --lev,
1329 0, &block, &bp);
1330 if (error)
1331 goto error0;
1333 xfs_btree_setbuf(cur, lev, bp);
1334 cur->bc_ptrs[lev] = 1;
1336 out1:
1337 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1338 *stat = 1;
1339 return 0;
1341 out0:
1342 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1343 *stat = 0;
1344 return 0;
1346 error0:
1347 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
1348 return error;
1352 * Decrement cursor by one record at the level.
1353 * For nonzero levels the leaf-ward information is untouched.
1355 int /* error */
1356 xfs_btree_decrement(
1357 struct xfs_btree_cur *cur,
1358 int level,
1359 int *stat) /* success/failure */
1361 struct xfs_btree_block *block;
1362 xfs_buf_t *bp;
1363 int error; /* error return value */
1364 int lev;
1365 union xfs_btree_ptr ptr;
1367 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1368 XFS_BTREE_TRACE_ARGI(cur, level);
1370 ASSERT(level < cur->bc_nlevels);
1372 /* Read-ahead to the left at this level. */
1373 xfs_btree_readahead(cur, level, XFS_BTCUR_LEFTRA);
1375 /* We're done if we remain in the block after the decrement. */
1376 if (--cur->bc_ptrs[level] > 0)
1377 goto out1;
1379 /* Get a pointer to the btree block. */
1380 block = xfs_btree_get_block(cur, level, &bp);
1382 #ifdef DEBUG
1383 error = xfs_btree_check_block(cur, block, level, bp);
1384 if (error)
1385 goto error0;
1386 #endif
1388 /* Fail if we just went off the left edge of the tree. */
1389 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_LEFTSIB);
1390 if (xfs_btree_ptr_is_null(cur, &ptr))
1391 goto out0;
1393 XFS_BTREE_STATS_INC(cur, decrement);
1396 * March up the tree decrementing pointers.
1397 * Stop when we don't go off the left edge of a block.
1399 for (lev = level + 1; lev < cur->bc_nlevels; lev++) {
1400 if (--cur->bc_ptrs[lev] > 0)
1401 break;
1402 /* Read-ahead the left block for the next loop. */
1403 xfs_btree_readahead(cur, lev, XFS_BTCUR_LEFTRA);
1407 * If we went off the root then we are seriously confused.
1408 * or the root of the tree is in an inode.
1410 if (lev == cur->bc_nlevels) {
1411 if (cur->bc_flags & XFS_BTREE_ROOT_IN_INODE)
1412 goto out0;
1413 ASSERT(0);
1414 error = EFSCORRUPTED;
1415 goto error0;
1417 ASSERT(lev < cur->bc_nlevels);
1420 * Now walk back down the tree, fixing up the cursor's buffer
1421 * pointers and key numbers.
1423 for (block = xfs_btree_get_block(cur, lev, &bp); lev > level; ) {
1424 union xfs_btree_ptr *ptrp;
1426 ptrp = xfs_btree_ptr_addr(cur, cur->bc_ptrs[lev], block);
1427 error = xfs_btree_read_buf_block(cur, ptrp, --lev,
1428 0, &block, &bp);
1429 if (error)
1430 goto error0;
1431 xfs_btree_setbuf(cur, lev, bp);
1432 cur->bc_ptrs[lev] = xfs_btree_get_numrecs(block);
1434 out1:
1435 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1436 *stat = 1;
1437 return 0;
1439 out0:
1440 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1441 *stat = 0;
1442 return 0;
1444 error0:
1445 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
1446 return error;
1449 STATIC int
1450 xfs_btree_lookup_get_block(
1451 struct xfs_btree_cur *cur, /* btree cursor */
1452 int level, /* level in the btree */
1453 union xfs_btree_ptr *pp, /* ptr to btree block */
1454 struct xfs_btree_block **blkp) /* return btree block */
1456 struct xfs_buf *bp; /* buffer pointer for btree block */
1457 int error = 0;
1459 /* special case the root block if in an inode */
1460 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
1461 (level == cur->bc_nlevels - 1)) {
1462 *blkp = xfs_btree_get_iroot(cur);
1463 return 0;
1467 * If the old buffer at this level for the disk address we are
1468 * looking for re-use it.
1470 * Otherwise throw it away and get a new one.
1472 bp = cur->bc_bufs[level];
1473 if (bp && XFS_BUF_ADDR(bp) == xfs_btree_ptr_to_daddr(cur, pp)) {
1474 *blkp = XFS_BUF_TO_BLOCK(bp);
1475 return 0;
1478 error = xfs_btree_read_buf_block(cur, pp, level, 0, blkp, &bp);
1479 if (error)
1480 return error;
1482 xfs_btree_setbuf(cur, level, bp);
1483 return 0;
1487 * Get current search key. For level 0 we don't actually have a key
1488 * structure so we make one up from the record. For all other levels
1489 * we just return the right key.
1491 STATIC union xfs_btree_key *
1492 xfs_lookup_get_search_key(
1493 struct xfs_btree_cur *cur,
1494 int level,
1495 int keyno,
1496 struct xfs_btree_block *block,
1497 union xfs_btree_key *kp)
1499 if (level == 0) {
1500 cur->bc_ops->init_key_from_rec(kp,
1501 xfs_btree_rec_addr(cur, keyno, block));
1502 return kp;
1505 return xfs_btree_key_addr(cur, keyno, block);
1509 * Lookup the record. The cursor is made to point to it, based on dir.
1510 * Return 0 if can't find any such record, 1 for success.
1512 int /* error */
1513 xfs_btree_lookup(
1514 struct xfs_btree_cur *cur, /* btree cursor */
1515 xfs_lookup_t dir, /* <=, ==, or >= */
1516 int *stat) /* success/failure */
1518 struct xfs_btree_block *block; /* current btree block */
1519 __int64_t diff; /* difference for the current key */
1520 int error; /* error return value */
1521 int keyno; /* current key number */
1522 int level; /* level in the btree */
1523 union xfs_btree_ptr *pp; /* ptr to btree block */
1524 union xfs_btree_ptr ptr; /* ptr to btree block */
1526 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1527 XFS_BTREE_TRACE_ARGI(cur, dir);
1529 XFS_BTREE_STATS_INC(cur, lookup);
1531 block = NULL;
1532 keyno = 0;
1534 /* initialise start pointer from cursor */
1535 cur->bc_ops->init_ptr_from_cur(cur, &ptr);
1536 pp = &ptr;
1539 * Iterate over each level in the btree, starting at the root.
1540 * For each level above the leaves, find the key we need, based
1541 * on the lookup record, then follow the corresponding block
1542 * pointer down to the next level.
1544 for (level = cur->bc_nlevels - 1, diff = 1; level >= 0; level--) {
1545 /* Get the block we need to do the lookup on. */
1546 error = xfs_btree_lookup_get_block(cur, level, pp, &block);
1547 if (error)
1548 goto error0;
1550 if (diff == 0) {
1552 * If we already had a key match at a higher level, we
1553 * know we need to use the first entry in this block.
1555 keyno = 1;
1556 } else {
1557 /* Otherwise search this block. Do a binary search. */
1559 int high; /* high entry number */
1560 int low; /* low entry number */
1562 /* Set low and high entry numbers, 1-based. */
1563 low = 1;
1564 high = xfs_btree_get_numrecs(block);
1565 if (!high) {
1566 /* Block is empty, must be an empty leaf. */
1567 ASSERT(level == 0 && cur->bc_nlevels == 1);
1569 cur->bc_ptrs[0] = dir != XFS_LOOKUP_LE;
1570 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1571 *stat = 0;
1572 return 0;
1575 /* Binary search the block. */
1576 while (low <= high) {
1577 union xfs_btree_key key;
1578 union xfs_btree_key *kp;
1580 XFS_BTREE_STATS_INC(cur, compare);
1582 /* keyno is average of low and high. */
1583 keyno = (low + high) >> 1;
1585 /* Get current search key */
1586 kp = xfs_lookup_get_search_key(cur, level,
1587 keyno, block, &key);
1590 * Compute difference to get next direction:
1591 * - less than, move right
1592 * - greater than, move left
1593 * - equal, we're done
1595 diff = cur->bc_ops->key_diff(cur, kp);
1596 if (diff < 0)
1597 low = keyno + 1;
1598 else if (diff > 0)
1599 high = keyno - 1;
1600 else
1601 break;
1606 * If there are more levels, set up for the next level
1607 * by getting the block number and filling in the cursor.
1609 if (level > 0) {
1611 * If we moved left, need the previous key number,
1612 * unless there isn't one.
1614 if (diff > 0 && --keyno < 1)
1615 keyno = 1;
1616 pp = xfs_btree_ptr_addr(cur, keyno, block);
1618 #ifdef DEBUG
1619 error = xfs_btree_check_ptr(cur, pp, 0, level);
1620 if (error)
1621 goto error0;
1622 #endif
1623 cur->bc_ptrs[level] = keyno;
1627 /* Done with the search. See if we need to adjust the results. */
1628 if (dir != XFS_LOOKUP_LE && diff < 0) {
1629 keyno++;
1631 * If ge search and we went off the end of the block, but it's
1632 * not the last block, we're in the wrong block.
1634 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_RIGHTSIB);
1635 if (dir == XFS_LOOKUP_GE &&
1636 keyno > xfs_btree_get_numrecs(block) &&
1637 !xfs_btree_ptr_is_null(cur, &ptr)) {
1638 int i;
1640 cur->bc_ptrs[0] = keyno;
1641 error = xfs_btree_increment(cur, 0, &i);
1642 if (error)
1643 goto error0;
1644 XFS_WANT_CORRUPTED_RETURN(i == 1);
1645 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1646 *stat = 1;
1647 return 0;
1649 } else if (dir == XFS_LOOKUP_LE && diff > 0)
1650 keyno--;
1651 cur->bc_ptrs[0] = keyno;
1653 /* Return if we succeeded or not. */
1654 if (keyno == 0 || keyno > xfs_btree_get_numrecs(block))
1655 *stat = 0;
1656 else if (dir != XFS_LOOKUP_EQ || diff == 0)
1657 *stat = 1;
1658 else
1659 *stat = 0;
1660 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1661 return 0;
1663 error0:
1664 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
1665 return error;
1669 * Update keys at all levels from here to the root along the cursor's path.
1671 STATIC int
1672 xfs_btree_updkey(
1673 struct xfs_btree_cur *cur,
1674 union xfs_btree_key *keyp,
1675 int level)
1677 struct xfs_btree_block *block;
1678 struct xfs_buf *bp;
1679 union xfs_btree_key *kp;
1680 int ptr;
1682 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1683 XFS_BTREE_TRACE_ARGIK(cur, level, keyp);
1685 ASSERT(!(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) || level >= 1);
1688 * Go up the tree from this level toward the root.
1689 * At each level, update the key value to the value input.
1690 * Stop when we reach a level where the cursor isn't pointing
1691 * at the first entry in the block.
1693 for (ptr = 1; ptr == 1 && level < cur->bc_nlevels; level++) {
1694 #ifdef DEBUG
1695 int error;
1696 #endif
1697 block = xfs_btree_get_block(cur, level, &bp);
1698 #ifdef DEBUG
1699 error = xfs_btree_check_block(cur, block, level, bp);
1700 if (error) {
1701 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
1702 return error;
1704 #endif
1705 ptr = cur->bc_ptrs[level];
1706 kp = xfs_btree_key_addr(cur, ptr, block);
1707 xfs_btree_copy_keys(cur, kp, keyp, 1);
1708 xfs_btree_log_keys(cur, bp, ptr, ptr);
1711 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1712 return 0;
1716 * Update the record referred to by cur to the value in the
1717 * given record. This either works (return 0) or gets an
1718 * EFSCORRUPTED error.
1721 xfs_btree_update(
1722 struct xfs_btree_cur *cur,
1723 union xfs_btree_rec *rec)
1725 struct xfs_btree_block *block;
1726 struct xfs_buf *bp;
1727 int error;
1728 int ptr;
1729 union xfs_btree_rec *rp;
1731 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1732 XFS_BTREE_TRACE_ARGR(cur, rec);
1734 /* Pick up the current block. */
1735 block = xfs_btree_get_block(cur, 0, &bp);
1737 #ifdef DEBUG
1738 error = xfs_btree_check_block(cur, block, 0, bp);
1739 if (error)
1740 goto error0;
1741 #endif
1742 /* Get the address of the rec to be updated. */
1743 ptr = cur->bc_ptrs[0];
1744 rp = xfs_btree_rec_addr(cur, ptr, block);
1746 /* Fill in the new contents and log them. */
1747 xfs_btree_copy_recs(cur, rp, rec, 1);
1748 xfs_btree_log_recs(cur, bp, ptr, ptr);
1751 * If we are tracking the last record in the tree and
1752 * we are at the far right edge of the tree, update it.
1754 if (xfs_btree_is_lastrec(cur, block, 0)) {
1755 cur->bc_ops->update_lastrec(cur, block, rec,
1756 ptr, LASTREC_UPDATE);
1759 /* Updating first rec in leaf. Pass new key value up to our parent. */
1760 if (ptr == 1) {
1761 union xfs_btree_key key;
1763 cur->bc_ops->init_key_from_rec(&key, rec);
1764 error = xfs_btree_updkey(cur, &key, 1);
1765 if (error)
1766 goto error0;
1769 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1770 return 0;
1772 error0:
1773 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
1774 return error;
1778 * Move 1 record left from cur/level if possible.
1779 * Update cur to reflect the new path.
1781 STATIC int /* error */
1782 xfs_btree_lshift(
1783 struct xfs_btree_cur *cur,
1784 int level,
1785 int *stat) /* success/failure */
1787 union xfs_btree_key key; /* btree key */
1788 struct xfs_buf *lbp; /* left buffer pointer */
1789 struct xfs_btree_block *left; /* left btree block */
1790 int lrecs; /* left record count */
1791 struct xfs_buf *rbp; /* right buffer pointer */
1792 struct xfs_btree_block *right; /* right btree block */
1793 int rrecs; /* right record count */
1794 union xfs_btree_ptr lptr; /* left btree pointer */
1795 union xfs_btree_key *rkp = NULL; /* right btree key */
1796 union xfs_btree_ptr *rpp = NULL; /* right address pointer */
1797 union xfs_btree_rec *rrp = NULL; /* right record pointer */
1798 int error; /* error return value */
1800 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1801 XFS_BTREE_TRACE_ARGI(cur, level);
1803 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
1804 level == cur->bc_nlevels - 1)
1805 goto out0;
1807 /* Set up variables for this block as "right". */
1808 right = xfs_btree_get_block(cur, level, &rbp);
1810 #ifdef DEBUG
1811 error = xfs_btree_check_block(cur, right, level, rbp);
1812 if (error)
1813 goto error0;
1814 #endif
1816 /* If we've got no left sibling then we can't shift an entry left. */
1817 xfs_btree_get_sibling(cur, right, &lptr, XFS_BB_LEFTSIB);
1818 if (xfs_btree_ptr_is_null(cur, &lptr))
1819 goto out0;
1822 * If the cursor entry is the one that would be moved, don't
1823 * do it... it's too complicated.
1825 if (cur->bc_ptrs[level] <= 1)
1826 goto out0;
1828 /* Set up the left neighbor as "left". */
1829 error = xfs_btree_read_buf_block(cur, &lptr, level, 0, &left, &lbp);
1830 if (error)
1831 goto error0;
1833 /* If it's full, it can't take another entry. */
1834 lrecs = xfs_btree_get_numrecs(left);
1835 if (lrecs == cur->bc_ops->get_maxrecs(cur, level))
1836 goto out0;
1838 rrecs = xfs_btree_get_numrecs(right);
1841 * We add one entry to the left side and remove one for the right side.
1842 * Account for it here, the changes will be updated on disk and logged
1843 * later.
1845 lrecs++;
1846 rrecs--;
1848 XFS_BTREE_STATS_INC(cur, lshift);
1849 XFS_BTREE_STATS_ADD(cur, moves, 1);
1852 * If non-leaf, copy a key and a ptr to the left block.
1853 * Log the changes to the left block.
1855 if (level > 0) {
1856 /* It's a non-leaf. Move keys and pointers. */
1857 union xfs_btree_key *lkp; /* left btree key */
1858 union xfs_btree_ptr *lpp; /* left address pointer */
1860 lkp = xfs_btree_key_addr(cur, lrecs, left);
1861 rkp = xfs_btree_key_addr(cur, 1, right);
1863 lpp = xfs_btree_ptr_addr(cur, lrecs, left);
1864 rpp = xfs_btree_ptr_addr(cur, 1, right);
1865 #ifdef DEBUG
1866 error = xfs_btree_check_ptr(cur, rpp, 0, level);
1867 if (error)
1868 goto error0;
1869 #endif
1870 xfs_btree_copy_keys(cur, lkp, rkp, 1);
1871 xfs_btree_copy_ptrs(cur, lpp, rpp, 1);
1873 xfs_btree_log_keys(cur, lbp, lrecs, lrecs);
1874 xfs_btree_log_ptrs(cur, lbp, lrecs, lrecs);
1876 ASSERT(cur->bc_ops->keys_inorder(cur,
1877 xfs_btree_key_addr(cur, lrecs - 1, left), lkp));
1878 } else {
1879 /* It's a leaf. Move records. */
1880 union xfs_btree_rec *lrp; /* left record pointer */
1882 lrp = xfs_btree_rec_addr(cur, lrecs, left);
1883 rrp = xfs_btree_rec_addr(cur, 1, right);
1885 xfs_btree_copy_recs(cur, lrp, rrp, 1);
1886 xfs_btree_log_recs(cur, lbp, lrecs, lrecs);
1888 ASSERT(cur->bc_ops->recs_inorder(cur,
1889 xfs_btree_rec_addr(cur, lrecs - 1, left), lrp));
1892 xfs_btree_set_numrecs(left, lrecs);
1893 xfs_btree_log_block(cur, lbp, XFS_BB_NUMRECS);
1895 xfs_btree_set_numrecs(right, rrecs);
1896 xfs_btree_log_block(cur, rbp, XFS_BB_NUMRECS);
1899 * Slide the contents of right down one entry.
1901 XFS_BTREE_STATS_ADD(cur, moves, rrecs - 1);
1902 if (level > 0) {
1903 /* It's a nonleaf. operate on keys and ptrs */
1904 #ifdef DEBUG
1905 int i; /* loop index */
1907 for (i = 0; i < rrecs; i++) {
1908 error = xfs_btree_check_ptr(cur, rpp, i + 1, level);
1909 if (error)
1910 goto error0;
1912 #endif
1913 xfs_btree_shift_keys(cur,
1914 xfs_btree_key_addr(cur, 2, right),
1915 -1, rrecs);
1916 xfs_btree_shift_ptrs(cur,
1917 xfs_btree_ptr_addr(cur, 2, right),
1918 -1, rrecs);
1920 xfs_btree_log_keys(cur, rbp, 1, rrecs);
1921 xfs_btree_log_ptrs(cur, rbp, 1, rrecs);
1922 } else {
1923 /* It's a leaf. operate on records */
1924 xfs_btree_shift_recs(cur,
1925 xfs_btree_rec_addr(cur, 2, right),
1926 -1, rrecs);
1927 xfs_btree_log_recs(cur, rbp, 1, rrecs);
1930 * If it's the first record in the block, we'll need a key
1931 * structure to pass up to the next level (updkey).
1933 cur->bc_ops->init_key_from_rec(&key,
1934 xfs_btree_rec_addr(cur, 1, right));
1935 rkp = &key;
1938 /* Update the parent key values of right. */
1939 error = xfs_btree_updkey(cur, rkp, level + 1);
1940 if (error)
1941 goto error0;
1943 /* Slide the cursor value left one. */
1944 cur->bc_ptrs[level]--;
1946 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1947 *stat = 1;
1948 return 0;
1950 out0:
1951 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1952 *stat = 0;
1953 return 0;
1955 error0:
1956 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
1957 return error;
1961 * Move 1 record right from cur/level if possible.
1962 * Update cur to reflect the new path.
1964 STATIC int /* error */
1965 xfs_btree_rshift(
1966 struct xfs_btree_cur *cur,
1967 int level,
1968 int *stat) /* success/failure */
1970 union xfs_btree_key key; /* btree key */
1971 struct xfs_buf *lbp; /* left buffer pointer */
1972 struct xfs_btree_block *left; /* left btree block */
1973 struct xfs_buf *rbp; /* right buffer pointer */
1974 struct xfs_btree_block *right; /* right btree block */
1975 struct xfs_btree_cur *tcur; /* temporary btree cursor */
1976 union xfs_btree_ptr rptr; /* right block pointer */
1977 union xfs_btree_key *rkp; /* right btree key */
1978 int rrecs; /* right record count */
1979 int lrecs; /* left record count */
1980 int error; /* error return value */
1981 int i; /* loop counter */
1983 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1984 XFS_BTREE_TRACE_ARGI(cur, level);
1986 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
1987 (level == cur->bc_nlevels - 1))
1988 goto out0;
1990 /* Set up variables for this block as "left". */
1991 left = xfs_btree_get_block(cur, level, &lbp);
1993 #ifdef DEBUG
1994 error = xfs_btree_check_block(cur, left, level, lbp);
1995 if (error)
1996 goto error0;
1997 #endif
1999 /* If we've got no right sibling then we can't shift an entry right. */
2000 xfs_btree_get_sibling(cur, left, &rptr, XFS_BB_RIGHTSIB);
2001 if (xfs_btree_ptr_is_null(cur, &rptr))
2002 goto out0;
2005 * If the cursor entry is the one that would be moved, don't
2006 * do it... it's too complicated.
2008 lrecs = xfs_btree_get_numrecs(left);
2009 if (cur->bc_ptrs[level] >= lrecs)
2010 goto out0;
2012 /* Set up the right neighbor as "right". */
2013 error = xfs_btree_read_buf_block(cur, &rptr, level, 0, &right, &rbp);
2014 if (error)
2015 goto error0;
2017 /* If it's full, it can't take another entry. */
2018 rrecs = xfs_btree_get_numrecs(right);
2019 if (rrecs == cur->bc_ops->get_maxrecs(cur, level))
2020 goto out0;
2022 XFS_BTREE_STATS_INC(cur, rshift);
2023 XFS_BTREE_STATS_ADD(cur, moves, rrecs);
2026 * Make a hole at the start of the right neighbor block, then
2027 * copy the last left block entry to the hole.
2029 if (level > 0) {
2030 /* It's a nonleaf. make a hole in the keys and ptrs */
2031 union xfs_btree_key *lkp;
2032 union xfs_btree_ptr *lpp;
2033 union xfs_btree_ptr *rpp;
2035 lkp = xfs_btree_key_addr(cur, lrecs, left);
2036 lpp = xfs_btree_ptr_addr(cur, lrecs, left);
2037 rkp = xfs_btree_key_addr(cur, 1, right);
2038 rpp = xfs_btree_ptr_addr(cur, 1, right);
2040 #ifdef DEBUG
2041 for (i = rrecs - 1; i >= 0; i--) {
2042 error = xfs_btree_check_ptr(cur, rpp, i, level);
2043 if (error)
2044 goto error0;
2046 #endif
2048 xfs_btree_shift_keys(cur, rkp, 1, rrecs);
2049 xfs_btree_shift_ptrs(cur, rpp, 1, rrecs);
2051 #ifdef DEBUG
2052 error = xfs_btree_check_ptr(cur, lpp, 0, level);
2053 if (error)
2054 goto error0;
2055 #endif
2057 /* Now put the new data in, and log it. */
2058 xfs_btree_copy_keys(cur, rkp, lkp, 1);
2059 xfs_btree_copy_ptrs(cur, rpp, lpp, 1);
2061 xfs_btree_log_keys(cur, rbp, 1, rrecs + 1);
2062 xfs_btree_log_ptrs(cur, rbp, 1, rrecs + 1);
2064 ASSERT(cur->bc_ops->keys_inorder(cur, rkp,
2065 xfs_btree_key_addr(cur, 2, right)));
2066 } else {
2067 /* It's a leaf. make a hole in the records */
2068 union xfs_btree_rec *lrp;
2069 union xfs_btree_rec *rrp;
2071 lrp = xfs_btree_rec_addr(cur, lrecs, left);
2072 rrp = xfs_btree_rec_addr(cur, 1, right);
2074 xfs_btree_shift_recs(cur, rrp, 1, rrecs);
2076 /* Now put the new data in, and log it. */
2077 xfs_btree_copy_recs(cur, rrp, lrp, 1);
2078 xfs_btree_log_recs(cur, rbp, 1, rrecs + 1);
2080 cur->bc_ops->init_key_from_rec(&key, rrp);
2081 rkp = &key;
2083 ASSERT(cur->bc_ops->recs_inorder(cur, rrp,
2084 xfs_btree_rec_addr(cur, 2, right)));
2088 * Decrement and log left's numrecs, bump and log right's numrecs.
2090 xfs_btree_set_numrecs(left, --lrecs);
2091 xfs_btree_log_block(cur, lbp, XFS_BB_NUMRECS);
2093 xfs_btree_set_numrecs(right, ++rrecs);
2094 xfs_btree_log_block(cur, rbp, XFS_BB_NUMRECS);
2097 * Using a temporary cursor, update the parent key values of the
2098 * block on the right.
2100 error = xfs_btree_dup_cursor(cur, &tcur);
2101 if (error)
2102 goto error0;
2103 i = xfs_btree_lastrec(tcur, level);
2104 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
2106 error = xfs_btree_increment(tcur, level, &i);
2107 if (error)
2108 goto error1;
2110 error = xfs_btree_updkey(tcur, rkp, level + 1);
2111 if (error)
2112 goto error1;
2114 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
2116 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2117 *stat = 1;
2118 return 0;
2120 out0:
2121 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2122 *stat = 0;
2123 return 0;
2125 error0:
2126 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
2127 return error;
2129 error1:
2130 XFS_BTREE_TRACE_CURSOR(tcur, XBT_ERROR);
2131 xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
2132 return error;
2136 * Split cur/level block in half.
2137 * Return new block number and the key to its first
2138 * record (to be inserted into parent).
2140 STATIC int /* error */
2141 xfs_btree_split(
2142 struct xfs_btree_cur *cur,
2143 int level,
2144 union xfs_btree_ptr *ptrp,
2145 union xfs_btree_key *key,
2146 struct xfs_btree_cur **curp,
2147 int *stat) /* success/failure */
2149 union xfs_btree_ptr lptr; /* left sibling block ptr */
2150 struct xfs_buf *lbp; /* left buffer pointer */
2151 struct xfs_btree_block *left; /* left btree block */
2152 union xfs_btree_ptr rptr; /* right sibling block ptr */
2153 struct xfs_buf *rbp; /* right buffer pointer */
2154 struct xfs_btree_block *right; /* right btree block */
2155 union xfs_btree_ptr rrptr; /* right-right sibling ptr */
2156 struct xfs_buf *rrbp; /* right-right buffer pointer */
2157 struct xfs_btree_block *rrblock; /* right-right btree block */
2158 int lrecs;
2159 int rrecs;
2160 int src_index;
2161 int error; /* error return value */
2162 #ifdef DEBUG
2163 int i;
2164 #endif
2166 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
2167 XFS_BTREE_TRACE_ARGIPK(cur, level, *ptrp, key);
2169 XFS_BTREE_STATS_INC(cur, split);
2171 /* Set up left block (current one). */
2172 left = xfs_btree_get_block(cur, level, &lbp);
2174 #ifdef DEBUG
2175 error = xfs_btree_check_block(cur, left, level, lbp);
2176 if (error)
2177 goto error0;
2178 #endif
2180 xfs_btree_buf_to_ptr(cur, lbp, &lptr);
2182 /* Allocate the new block. If we can't do it, we're toast. Give up. */
2183 error = cur->bc_ops->alloc_block(cur, &lptr, &rptr, 1, stat);
2184 if (error)
2185 goto error0;
2186 if (*stat == 0)
2187 goto out0;
2188 XFS_BTREE_STATS_INC(cur, alloc);
2190 /* Set up the new block as "right". */
2191 error = xfs_btree_get_buf_block(cur, &rptr, 0, &right, &rbp);
2192 if (error)
2193 goto error0;
2195 /* Fill in the btree header for the new right block. */
2196 xfs_btree_init_block(cur, xfs_btree_get_level(left), 0, right);
2199 * Split the entries between the old and the new block evenly.
2200 * Make sure that if there's an odd number of entries now, that
2201 * each new block will have the same number of entries.
2203 lrecs = xfs_btree_get_numrecs(left);
2204 rrecs = lrecs / 2;
2205 if ((lrecs & 1) && cur->bc_ptrs[level] <= rrecs + 1)
2206 rrecs++;
2207 src_index = (lrecs - rrecs + 1);
2209 XFS_BTREE_STATS_ADD(cur, moves, rrecs);
2212 * Copy btree block entries from the left block over to the
2213 * new block, the right. Update the right block and log the
2214 * changes.
2216 if (level > 0) {
2217 /* It's a non-leaf. Move keys and pointers. */
2218 union xfs_btree_key *lkp; /* left btree key */
2219 union xfs_btree_ptr *lpp; /* left address pointer */
2220 union xfs_btree_key *rkp; /* right btree key */
2221 union xfs_btree_ptr *rpp; /* right address pointer */
2223 lkp = xfs_btree_key_addr(cur, src_index, left);
2224 lpp = xfs_btree_ptr_addr(cur, src_index, left);
2225 rkp = xfs_btree_key_addr(cur, 1, right);
2226 rpp = xfs_btree_ptr_addr(cur, 1, right);
2228 #ifdef DEBUG
2229 for (i = src_index; i < rrecs; i++) {
2230 error = xfs_btree_check_ptr(cur, lpp, i, level);
2231 if (error)
2232 goto error0;
2234 #endif
2236 xfs_btree_copy_keys(cur, rkp, lkp, rrecs);
2237 xfs_btree_copy_ptrs(cur, rpp, lpp, rrecs);
2239 xfs_btree_log_keys(cur, rbp, 1, rrecs);
2240 xfs_btree_log_ptrs(cur, rbp, 1, rrecs);
2242 /* Grab the keys to the entries moved to the right block */
2243 xfs_btree_copy_keys(cur, key, rkp, 1);
2244 } else {
2245 /* It's a leaf. Move records. */
2246 union xfs_btree_rec *lrp; /* left record pointer */
2247 union xfs_btree_rec *rrp; /* right record pointer */
2249 lrp = xfs_btree_rec_addr(cur, src_index, left);
2250 rrp = xfs_btree_rec_addr(cur, 1, right);
2252 xfs_btree_copy_recs(cur, rrp, lrp, rrecs);
2253 xfs_btree_log_recs(cur, rbp, 1, rrecs);
2255 cur->bc_ops->init_key_from_rec(key,
2256 xfs_btree_rec_addr(cur, 1, right));
2261 * Find the left block number by looking in the buffer.
2262 * Adjust numrecs, sibling pointers.
2264 xfs_btree_get_sibling(cur, left, &rrptr, XFS_BB_RIGHTSIB);
2265 xfs_btree_set_sibling(cur, right, &rrptr, XFS_BB_RIGHTSIB);
2266 xfs_btree_set_sibling(cur, right, &lptr, XFS_BB_LEFTSIB);
2267 xfs_btree_set_sibling(cur, left, &rptr, XFS_BB_RIGHTSIB);
2269 lrecs -= rrecs;
2270 xfs_btree_set_numrecs(left, lrecs);
2271 xfs_btree_set_numrecs(right, xfs_btree_get_numrecs(right) + rrecs);
2273 xfs_btree_log_block(cur, rbp, XFS_BB_ALL_BITS);
2274 xfs_btree_log_block(cur, lbp, XFS_BB_NUMRECS | XFS_BB_RIGHTSIB);
2277 * If there's a block to the new block's right, make that block
2278 * point back to right instead of to left.
2280 if (!xfs_btree_ptr_is_null(cur, &rrptr)) {
2281 error = xfs_btree_read_buf_block(cur, &rrptr, level,
2282 0, &rrblock, &rrbp);
2283 if (error)
2284 goto error0;
2285 xfs_btree_set_sibling(cur, rrblock, &rptr, XFS_BB_LEFTSIB);
2286 xfs_btree_log_block(cur, rrbp, XFS_BB_LEFTSIB);
2289 * If the cursor is really in the right block, move it there.
2290 * If it's just pointing past the last entry in left, then we'll
2291 * insert there, so don't change anything in that case.
2293 if (cur->bc_ptrs[level] > lrecs + 1) {
2294 xfs_btree_setbuf(cur, level, rbp);
2295 cur->bc_ptrs[level] -= lrecs;
2298 * If there are more levels, we'll need another cursor which refers
2299 * the right block, no matter where this cursor was.
2301 if (level + 1 < cur->bc_nlevels) {
2302 error = xfs_btree_dup_cursor(cur, curp);
2303 if (error)
2304 goto error0;
2305 (*curp)->bc_ptrs[level + 1]++;
2307 *ptrp = rptr;
2308 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2309 *stat = 1;
2310 return 0;
2311 out0:
2312 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2313 *stat = 0;
2314 return 0;
2316 error0:
2317 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
2318 return error;
2322 * Copy the old inode root contents into a real block and make the
2323 * broot point to it.
2325 int /* error */
2326 xfs_btree_new_iroot(
2327 struct xfs_btree_cur *cur, /* btree cursor */
2328 int *logflags, /* logging flags for inode */
2329 int *stat) /* return status - 0 fail */
2331 struct xfs_buf *cbp; /* buffer for cblock */
2332 struct xfs_btree_block *block; /* btree block */
2333 struct xfs_btree_block *cblock; /* child btree block */
2334 union xfs_btree_key *ckp; /* child key pointer */
2335 union xfs_btree_ptr *cpp; /* child ptr pointer */
2336 union xfs_btree_key *kp; /* pointer to btree key */
2337 union xfs_btree_ptr *pp; /* pointer to block addr */
2338 union xfs_btree_ptr nptr; /* new block addr */
2339 int level; /* btree level */
2340 int error; /* error return code */
2341 #ifdef DEBUG
2342 int i; /* loop counter */
2343 #endif
2345 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
2346 XFS_BTREE_STATS_INC(cur, newroot);
2348 ASSERT(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE);
2350 level = cur->bc_nlevels - 1;
2352 block = xfs_btree_get_iroot(cur);
2353 pp = xfs_btree_ptr_addr(cur, 1, block);
2355 /* Allocate the new block. If we can't do it, we're toast. Give up. */
2356 error = cur->bc_ops->alloc_block(cur, pp, &nptr, 1, stat);
2357 if (error)
2358 goto error0;
2359 if (*stat == 0) {
2360 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2361 return 0;
2363 XFS_BTREE_STATS_INC(cur, alloc);
2365 /* Copy the root into a real block. */
2366 error = xfs_btree_get_buf_block(cur, &nptr, 0, &cblock, &cbp);
2367 if (error)
2368 goto error0;
2370 memcpy(cblock, block, xfs_btree_block_len(cur));
2372 be16_add_cpu(&block->bb_level, 1);
2373 xfs_btree_set_numrecs(block, 1);
2374 cur->bc_nlevels++;
2375 cur->bc_ptrs[level + 1] = 1;
2377 kp = xfs_btree_key_addr(cur, 1, block);
2378 ckp = xfs_btree_key_addr(cur, 1, cblock);
2379 xfs_btree_copy_keys(cur, ckp, kp, xfs_btree_get_numrecs(cblock));
2381 cpp = xfs_btree_ptr_addr(cur, 1, cblock);
2382 #ifdef DEBUG
2383 for (i = 0; i < be16_to_cpu(cblock->bb_numrecs); i++) {
2384 error = xfs_btree_check_ptr(cur, pp, i, level);
2385 if (error)
2386 goto error0;
2388 #endif
2389 xfs_btree_copy_ptrs(cur, cpp, pp, xfs_btree_get_numrecs(cblock));
2391 #ifdef DEBUG
2392 error = xfs_btree_check_ptr(cur, &nptr, 0, level);
2393 if (error)
2394 goto error0;
2395 #endif
2396 xfs_btree_copy_ptrs(cur, pp, &nptr, 1);
2398 xfs_iroot_realloc(cur->bc_private.b.ip,
2399 1 - xfs_btree_get_numrecs(cblock),
2400 cur->bc_private.b.whichfork);
2402 xfs_btree_setbuf(cur, level, cbp);
2405 * Do all this logging at the end so that
2406 * the root is at the right level.
2408 xfs_btree_log_block(cur, cbp, XFS_BB_ALL_BITS);
2409 xfs_btree_log_keys(cur, cbp, 1, be16_to_cpu(cblock->bb_numrecs));
2410 xfs_btree_log_ptrs(cur, cbp, 1, be16_to_cpu(cblock->bb_numrecs));
2412 *logflags |=
2413 XFS_ILOG_CORE | xfs_ilog_fbroot(cur->bc_private.b.whichfork);
2414 *stat = 1;
2415 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2416 return 0;
2417 error0:
2418 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
2419 return error;
2423 * Allocate a new root block, fill it in.
2425 STATIC int /* error */
2426 xfs_btree_new_root(
2427 struct xfs_btree_cur *cur, /* btree cursor */
2428 int *stat) /* success/failure */
2430 struct xfs_btree_block *block; /* one half of the old root block */
2431 struct xfs_buf *bp; /* buffer containing block */
2432 int error; /* error return value */
2433 struct xfs_buf *lbp; /* left buffer pointer */
2434 struct xfs_btree_block *left; /* left btree block */
2435 struct xfs_buf *nbp; /* new (root) buffer */
2436 struct xfs_btree_block *new; /* new (root) btree block */
2437 int nptr; /* new value for key index, 1 or 2 */
2438 struct xfs_buf *rbp; /* right buffer pointer */
2439 struct xfs_btree_block *right; /* right btree block */
2440 union xfs_btree_ptr rptr;
2441 union xfs_btree_ptr lptr;
2443 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
2444 XFS_BTREE_STATS_INC(cur, newroot);
2446 /* initialise our start point from the cursor */
2447 cur->bc_ops->init_ptr_from_cur(cur, &rptr);
2449 /* Allocate the new block. If we can't do it, we're toast. Give up. */
2450 error = cur->bc_ops->alloc_block(cur, &rptr, &lptr, 1, stat);
2451 if (error)
2452 goto error0;
2453 if (*stat == 0)
2454 goto out0;
2455 XFS_BTREE_STATS_INC(cur, alloc);
2457 /* Set up the new block. */
2458 error = xfs_btree_get_buf_block(cur, &lptr, 0, &new, &nbp);
2459 if (error)
2460 goto error0;
2462 /* Set the root in the holding structure increasing the level by 1. */
2463 cur->bc_ops->set_root(cur, &lptr, 1);
2466 * At the previous root level there are now two blocks: the old root,
2467 * and the new block generated when it was split. We don't know which
2468 * one the cursor is pointing at, so we set up variables "left" and
2469 * "right" for each case.
2471 block = xfs_btree_get_block(cur, cur->bc_nlevels - 1, &bp);
2473 #ifdef DEBUG
2474 error = xfs_btree_check_block(cur, block, cur->bc_nlevels - 1, bp);
2475 if (error)
2476 goto error0;
2477 #endif
2479 xfs_btree_get_sibling(cur, block, &rptr, XFS_BB_RIGHTSIB);
2480 if (!xfs_btree_ptr_is_null(cur, &rptr)) {
2481 /* Our block is left, pick up the right block. */
2482 lbp = bp;
2483 xfs_btree_buf_to_ptr(cur, lbp, &lptr);
2484 left = block;
2485 error = xfs_btree_read_buf_block(cur, &rptr,
2486 cur->bc_nlevels - 1, 0, &right, &rbp);
2487 if (error)
2488 goto error0;
2489 bp = rbp;
2490 nptr = 1;
2491 } else {
2492 /* Our block is right, pick up the left block. */
2493 rbp = bp;
2494 xfs_btree_buf_to_ptr(cur, rbp, &rptr);
2495 right = block;
2496 xfs_btree_get_sibling(cur, right, &lptr, XFS_BB_LEFTSIB);
2497 error = xfs_btree_read_buf_block(cur, &lptr,
2498 cur->bc_nlevels - 1, 0, &left, &lbp);
2499 if (error)
2500 goto error0;
2501 bp = lbp;
2502 nptr = 2;
2504 /* Fill in the new block's btree header and log it. */
2505 xfs_btree_init_block(cur, cur->bc_nlevels, 2, new);
2506 xfs_btree_log_block(cur, nbp, XFS_BB_ALL_BITS);
2507 ASSERT(!xfs_btree_ptr_is_null(cur, &lptr) &&
2508 !xfs_btree_ptr_is_null(cur, &rptr));
2510 /* Fill in the key data in the new root. */
2511 if (xfs_btree_get_level(left) > 0) {
2512 xfs_btree_copy_keys(cur,
2513 xfs_btree_key_addr(cur, 1, new),
2514 xfs_btree_key_addr(cur, 1, left), 1);
2515 xfs_btree_copy_keys(cur,
2516 xfs_btree_key_addr(cur, 2, new),
2517 xfs_btree_key_addr(cur, 1, right), 1);
2518 } else {
2519 cur->bc_ops->init_key_from_rec(
2520 xfs_btree_key_addr(cur, 1, new),
2521 xfs_btree_rec_addr(cur, 1, left));
2522 cur->bc_ops->init_key_from_rec(
2523 xfs_btree_key_addr(cur, 2, new),
2524 xfs_btree_rec_addr(cur, 1, right));
2526 xfs_btree_log_keys(cur, nbp, 1, 2);
2528 /* Fill in the pointer data in the new root. */
2529 xfs_btree_copy_ptrs(cur,
2530 xfs_btree_ptr_addr(cur, 1, new), &lptr, 1);
2531 xfs_btree_copy_ptrs(cur,
2532 xfs_btree_ptr_addr(cur, 2, new), &rptr, 1);
2533 xfs_btree_log_ptrs(cur, nbp, 1, 2);
2535 /* Fix up the cursor. */
2536 xfs_btree_setbuf(cur, cur->bc_nlevels, nbp);
2537 cur->bc_ptrs[cur->bc_nlevels] = nptr;
2538 cur->bc_nlevels++;
2539 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2540 *stat = 1;
2541 return 0;
2542 error0:
2543 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
2544 return error;
2545 out0:
2546 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2547 *stat = 0;
2548 return 0;
2551 STATIC int
2552 xfs_btree_make_block_unfull(
2553 struct xfs_btree_cur *cur, /* btree cursor */
2554 int level, /* btree level */
2555 int numrecs,/* # of recs in block */
2556 int *oindex,/* old tree index */
2557 int *index, /* new tree index */
2558 union xfs_btree_ptr *nptr, /* new btree ptr */
2559 struct xfs_btree_cur **ncur, /* new btree cursor */
2560 union xfs_btree_rec *nrec, /* new record */
2561 int *stat)
2563 union xfs_btree_key key; /* new btree key value */
2564 int error = 0;
2566 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
2567 level == cur->bc_nlevels - 1) {
2568 struct xfs_inode *ip = cur->bc_private.b.ip;
2570 if (numrecs < cur->bc_ops->get_dmaxrecs(cur, level)) {
2571 /* A root block that can be made bigger. */
2573 xfs_iroot_realloc(ip, 1, cur->bc_private.b.whichfork);
2574 } else {
2575 /* A root block that needs replacing */
2576 int logflags = 0;
2578 error = xfs_btree_new_iroot(cur, &logflags, stat);
2579 if (error || *stat == 0)
2580 return error;
2582 xfs_trans_log_inode(cur->bc_tp, ip, logflags);
2585 return 0;
2588 /* First, try shifting an entry to the right neighbor. */
2589 error = xfs_btree_rshift(cur, level, stat);
2590 if (error || *stat)
2591 return error;
2593 /* Next, try shifting an entry to the left neighbor. */
2594 error = xfs_btree_lshift(cur, level, stat);
2595 if (error)
2596 return error;
2598 if (*stat) {
2599 *oindex = *index = cur->bc_ptrs[level];
2600 return 0;
2604 * Next, try splitting the current block in half.
2606 * If this works we have to re-set our variables because we
2607 * could be in a different block now.
2609 error = xfs_btree_split(cur, level, nptr, &key, ncur, stat);
2610 if (error || *stat == 0)
2611 return error;
2614 *index = cur->bc_ptrs[level];
2615 cur->bc_ops->init_rec_from_key(&key, nrec);
2616 return 0;
2620 * Insert one record/level. Return information to the caller
2621 * allowing the next level up to proceed if necessary.
2623 STATIC int
2624 xfs_btree_insrec(
2625 struct xfs_btree_cur *cur, /* btree cursor */
2626 int level, /* level to insert record at */
2627 union xfs_btree_ptr *ptrp, /* i/o: block number inserted */
2628 union xfs_btree_rec *recp, /* i/o: record data inserted */
2629 struct xfs_btree_cur **curp, /* output: new cursor replacing cur */
2630 int *stat) /* success/failure */
2632 struct xfs_btree_block *block; /* btree block */
2633 struct xfs_buf *bp; /* buffer for block */
2634 union xfs_btree_key key; /* btree key */
2635 union xfs_btree_ptr nptr; /* new block ptr */
2636 struct xfs_btree_cur *ncur; /* new btree cursor */
2637 union xfs_btree_rec nrec; /* new record count */
2638 int optr; /* old key/record index */
2639 int ptr; /* key/record index */
2640 int numrecs;/* number of records */
2641 int error; /* error return value */
2642 #ifdef DEBUG
2643 int i;
2644 #endif
2646 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
2647 XFS_BTREE_TRACE_ARGIPR(cur, level, *ptrp, recp);
2649 ncur = NULL;
2652 * If we have an external root pointer, and we've made it to the
2653 * root level, allocate a new root block and we're done.
2655 if (!(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
2656 (level >= cur->bc_nlevels)) {
2657 error = xfs_btree_new_root(cur, stat);
2658 xfs_btree_set_ptr_null(cur, ptrp);
2660 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2661 return error;
2664 /* If we're off the left edge, return failure. */
2665 ptr = cur->bc_ptrs[level];
2666 if (ptr == 0) {
2667 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2668 *stat = 0;
2669 return 0;
2672 /* Make a key out of the record data to be inserted, and save it. */
2673 cur->bc_ops->init_key_from_rec(&key, recp);
2675 optr = ptr;
2677 XFS_BTREE_STATS_INC(cur, insrec);
2679 /* Get pointers to the btree buffer and block. */
2680 block = xfs_btree_get_block(cur, level, &bp);
2681 numrecs = xfs_btree_get_numrecs(block);
2683 #ifdef DEBUG
2684 error = xfs_btree_check_block(cur, block, level, bp);
2685 if (error)
2686 goto error0;
2688 /* Check that the new entry is being inserted in the right place. */
2689 if (ptr <= numrecs) {
2690 if (level == 0) {
2691 ASSERT(cur->bc_ops->recs_inorder(cur, recp,
2692 xfs_btree_rec_addr(cur, ptr, block)));
2693 } else {
2694 ASSERT(cur->bc_ops->keys_inorder(cur, &key,
2695 xfs_btree_key_addr(cur, ptr, block)));
2698 #endif
2701 * If the block is full, we can't insert the new entry until we
2702 * make the block un-full.
2704 xfs_btree_set_ptr_null(cur, &nptr);
2705 if (numrecs == cur->bc_ops->get_maxrecs(cur, level)) {
2706 error = xfs_btree_make_block_unfull(cur, level, numrecs,
2707 &optr, &ptr, &nptr, &ncur, &nrec, stat);
2708 if (error || *stat == 0)
2709 goto error0;
2713 * The current block may have changed if the block was
2714 * previously full and we have just made space in it.
2716 block = xfs_btree_get_block(cur, level, &bp);
2717 numrecs = xfs_btree_get_numrecs(block);
2719 #ifdef DEBUG
2720 error = xfs_btree_check_block(cur, block, level, bp);
2721 if (error)
2722 return error;
2723 #endif
2726 * At this point we know there's room for our new entry in the block
2727 * we're pointing at.
2729 XFS_BTREE_STATS_ADD(cur, moves, numrecs - ptr + 1);
2731 if (level > 0) {
2732 /* It's a nonleaf. make a hole in the keys and ptrs */
2733 union xfs_btree_key *kp;
2734 union xfs_btree_ptr *pp;
2736 kp = xfs_btree_key_addr(cur, ptr, block);
2737 pp = xfs_btree_ptr_addr(cur, ptr, block);
2739 #ifdef DEBUG
2740 for (i = numrecs - ptr; i >= 0; i--) {
2741 error = xfs_btree_check_ptr(cur, pp, i, level);
2742 if (error)
2743 return error;
2745 #endif
2747 xfs_btree_shift_keys(cur, kp, 1, numrecs - ptr + 1);
2748 xfs_btree_shift_ptrs(cur, pp, 1, numrecs - ptr + 1);
2750 #ifdef DEBUG
2751 error = xfs_btree_check_ptr(cur, ptrp, 0, level);
2752 if (error)
2753 goto error0;
2754 #endif
2756 /* Now put the new data in, bump numrecs and log it. */
2757 xfs_btree_copy_keys(cur, kp, &key, 1);
2758 xfs_btree_copy_ptrs(cur, pp, ptrp, 1);
2759 numrecs++;
2760 xfs_btree_set_numrecs(block, numrecs);
2761 xfs_btree_log_ptrs(cur, bp, ptr, numrecs);
2762 xfs_btree_log_keys(cur, bp, ptr, numrecs);
2763 #ifdef DEBUG
2764 if (ptr < numrecs) {
2765 ASSERT(cur->bc_ops->keys_inorder(cur, kp,
2766 xfs_btree_key_addr(cur, ptr + 1, block)));
2768 #endif
2769 } else {
2770 /* It's a leaf. make a hole in the records */
2771 union xfs_btree_rec *rp;
2773 rp = xfs_btree_rec_addr(cur, ptr, block);
2775 xfs_btree_shift_recs(cur, rp, 1, numrecs - ptr + 1);
2777 /* Now put the new data in, bump numrecs and log it. */
2778 xfs_btree_copy_recs(cur, rp, recp, 1);
2779 xfs_btree_set_numrecs(block, ++numrecs);
2780 xfs_btree_log_recs(cur, bp, ptr, numrecs);
2781 #ifdef DEBUG
2782 if (ptr < numrecs) {
2783 ASSERT(cur->bc_ops->recs_inorder(cur, rp,
2784 xfs_btree_rec_addr(cur, ptr + 1, block)));
2786 #endif
2789 /* Log the new number of records in the btree header. */
2790 xfs_btree_log_block(cur, bp, XFS_BB_NUMRECS);
2792 /* If we inserted at the start of a block, update the parents' keys. */
2793 if (optr == 1) {
2794 error = xfs_btree_updkey(cur, &key, level + 1);
2795 if (error)
2796 goto error0;
2800 * If we are tracking the last record in the tree and
2801 * we are at the far right edge of the tree, update it.
2803 if (xfs_btree_is_lastrec(cur, block, level)) {
2804 cur->bc_ops->update_lastrec(cur, block, recp,
2805 ptr, LASTREC_INSREC);
2809 * Return the new block number, if any.
2810 * If there is one, give back a record value and a cursor too.
2812 *ptrp = nptr;
2813 if (!xfs_btree_ptr_is_null(cur, &nptr)) {
2814 *recp = nrec;
2815 *curp = ncur;
2818 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2819 *stat = 1;
2820 return 0;
2822 error0:
2823 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
2824 return error;
2828 * Insert the record at the point referenced by cur.
2830 * A multi-level split of the tree on insert will invalidate the original
2831 * cursor. All callers of this function should assume that the cursor is
2832 * no longer valid and revalidate it.
2835 xfs_btree_insert(
2836 struct xfs_btree_cur *cur,
2837 int *stat)
2839 int error; /* error return value */
2840 int i; /* result value, 0 for failure */
2841 int level; /* current level number in btree */
2842 union xfs_btree_ptr nptr; /* new block number (split result) */
2843 struct xfs_btree_cur *ncur; /* new cursor (split result) */
2844 struct xfs_btree_cur *pcur; /* previous level's cursor */
2845 union xfs_btree_rec rec; /* record to insert */
2847 level = 0;
2848 ncur = NULL;
2849 pcur = cur;
2851 xfs_btree_set_ptr_null(cur, &nptr);
2852 cur->bc_ops->init_rec_from_cur(cur, &rec);
2855 * Loop going up the tree, starting at the leaf level.
2856 * Stop when we don't get a split block, that must mean that
2857 * the insert is finished with this level.
2859 do {
2861 * Insert nrec/nptr into this level of the tree.
2862 * Note if we fail, nptr will be null.
2864 error = xfs_btree_insrec(pcur, level, &nptr, &rec, &ncur, &i);
2865 if (error) {
2866 if (pcur != cur)
2867 xfs_btree_del_cursor(pcur, XFS_BTREE_ERROR);
2868 goto error0;
2871 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
2872 level++;
2875 * See if the cursor we just used is trash.
2876 * Can't trash the caller's cursor, but otherwise we should
2877 * if ncur is a new cursor or we're about to be done.
2879 if (pcur != cur &&
2880 (ncur || xfs_btree_ptr_is_null(cur, &nptr))) {
2881 /* Save the state from the cursor before we trash it */
2882 if (cur->bc_ops->update_cursor)
2883 cur->bc_ops->update_cursor(pcur, cur);
2884 cur->bc_nlevels = pcur->bc_nlevels;
2885 xfs_btree_del_cursor(pcur, XFS_BTREE_NOERROR);
2887 /* If we got a new cursor, switch to it. */
2888 if (ncur) {
2889 pcur = ncur;
2890 ncur = NULL;
2892 } while (!xfs_btree_ptr_is_null(cur, &nptr));
2894 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2895 *stat = i;
2896 return 0;
2897 error0:
2898 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
2899 return error;
2903 * Try to merge a non-leaf block back into the inode root.
2905 * Note: the killroot names comes from the fact that we're effectively
2906 * killing the old root block. But because we can't just delete the
2907 * inode we have to copy the single block it was pointing to into the
2908 * inode.
2910 STATIC int
2911 xfs_btree_kill_iroot(
2912 struct xfs_btree_cur *cur)
2914 int whichfork = cur->bc_private.b.whichfork;
2915 struct xfs_inode *ip = cur->bc_private.b.ip;
2916 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
2917 struct xfs_btree_block *block;
2918 struct xfs_btree_block *cblock;
2919 union xfs_btree_key *kp;
2920 union xfs_btree_key *ckp;
2921 union xfs_btree_ptr *pp;
2922 union xfs_btree_ptr *cpp;
2923 struct xfs_buf *cbp;
2924 int level;
2925 int index;
2926 int numrecs;
2927 #ifdef DEBUG
2928 union xfs_btree_ptr ptr;
2929 int i;
2930 #endif
2932 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
2934 ASSERT(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE);
2935 ASSERT(cur->bc_nlevels > 1);
2938 * Don't deal with the root block needs to be a leaf case.
2939 * We're just going to turn the thing back into extents anyway.
2941 level = cur->bc_nlevels - 1;
2942 if (level == 1)
2943 goto out0;
2946 * Give up if the root has multiple children.
2948 block = xfs_btree_get_iroot(cur);
2949 if (xfs_btree_get_numrecs(block) != 1)
2950 goto out0;
2952 cblock = xfs_btree_get_block(cur, level - 1, &cbp);
2953 numrecs = xfs_btree_get_numrecs(cblock);
2956 * Only do this if the next level will fit.
2957 * Then the data must be copied up to the inode,
2958 * instead of freeing the root you free the next level.
2960 if (numrecs > cur->bc_ops->get_dmaxrecs(cur, level))
2961 goto out0;
2963 XFS_BTREE_STATS_INC(cur, killroot);
2965 #ifdef DEBUG
2966 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_LEFTSIB);
2967 ASSERT(xfs_btree_ptr_is_null(cur, &ptr));
2968 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_RIGHTSIB);
2969 ASSERT(xfs_btree_ptr_is_null(cur, &ptr));
2970 #endif
2972 index = numrecs - cur->bc_ops->get_maxrecs(cur, level);
2973 if (index) {
2974 xfs_iroot_realloc(cur->bc_private.b.ip, index,
2975 cur->bc_private.b.whichfork);
2976 block = ifp->if_broot;
2979 be16_add_cpu(&block->bb_numrecs, index);
2980 ASSERT(block->bb_numrecs == cblock->bb_numrecs);
2982 kp = xfs_btree_key_addr(cur, 1, block);
2983 ckp = xfs_btree_key_addr(cur, 1, cblock);
2984 xfs_btree_copy_keys(cur, kp, ckp, numrecs);
2986 pp = xfs_btree_ptr_addr(cur, 1, block);
2987 cpp = xfs_btree_ptr_addr(cur, 1, cblock);
2988 #ifdef DEBUG
2989 for (i = 0; i < numrecs; i++) {
2990 int error;
2992 error = xfs_btree_check_ptr(cur, cpp, i, level - 1);
2993 if (error) {
2994 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
2995 return error;
2998 #endif
2999 xfs_btree_copy_ptrs(cur, pp, cpp, numrecs);
3001 cur->bc_ops->free_block(cur, cbp);
3002 XFS_BTREE_STATS_INC(cur, free);
3004 cur->bc_bufs[level - 1] = NULL;
3005 be16_add_cpu(&block->bb_level, -1);
3006 xfs_trans_log_inode(cur->bc_tp, ip,
3007 XFS_ILOG_CORE | xfs_ilog_fbroot(cur->bc_private.b.whichfork));
3008 cur->bc_nlevels--;
3009 out0:
3010 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3011 return 0;
3014 STATIC int
3015 xfs_btree_dec_cursor(
3016 struct xfs_btree_cur *cur,
3017 int level,
3018 int *stat)
3020 int error;
3021 int i;
3023 if (level > 0) {
3024 error = xfs_btree_decrement(cur, level, &i);
3025 if (error)
3026 return error;
3029 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3030 *stat = 1;
3031 return 0;
3035 * Single level of the btree record deletion routine.
3036 * Delete record pointed to by cur/level.
3037 * Remove the record from its block then rebalance the tree.
3038 * Return 0 for error, 1 for done, 2 to go on to the next level.
3040 STATIC int /* error */
3041 xfs_btree_delrec(
3042 struct xfs_btree_cur *cur, /* btree cursor */
3043 int level, /* level removing record from */
3044 int *stat) /* fail/done/go-on */
3046 struct xfs_btree_block *block; /* btree block */
3047 union xfs_btree_ptr cptr; /* current block ptr */
3048 struct xfs_buf *bp; /* buffer for block */
3049 int error; /* error return value */
3050 int i; /* loop counter */
3051 union xfs_btree_key key; /* storage for keyp */
3052 union xfs_btree_key *keyp = &key; /* passed to the next level */
3053 union xfs_btree_ptr lptr; /* left sibling block ptr */
3054 struct xfs_buf *lbp; /* left buffer pointer */
3055 struct xfs_btree_block *left; /* left btree block */
3056 int lrecs = 0; /* left record count */
3057 int ptr; /* key/record index */
3058 union xfs_btree_ptr rptr; /* right sibling block ptr */
3059 struct xfs_buf *rbp; /* right buffer pointer */
3060 struct xfs_btree_block *right; /* right btree block */
3061 struct xfs_btree_block *rrblock; /* right-right btree block */
3062 struct xfs_buf *rrbp; /* right-right buffer pointer */
3063 int rrecs = 0; /* right record count */
3064 struct xfs_btree_cur *tcur; /* temporary btree cursor */
3065 int numrecs; /* temporary numrec count */
3067 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
3068 XFS_BTREE_TRACE_ARGI(cur, level);
3070 tcur = NULL;
3072 /* Get the index of the entry being deleted, check for nothing there. */
3073 ptr = cur->bc_ptrs[level];
3074 if (ptr == 0) {
3075 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3076 *stat = 0;
3077 return 0;
3080 /* Get the buffer & block containing the record or key/ptr. */
3081 block = xfs_btree_get_block(cur, level, &bp);
3082 numrecs = xfs_btree_get_numrecs(block);
3084 #ifdef DEBUG
3085 error = xfs_btree_check_block(cur, block, level, bp);
3086 if (error)
3087 goto error0;
3088 #endif
3090 /* Fail if we're off the end of the block. */
3091 if (ptr > numrecs) {
3092 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3093 *stat = 0;
3094 return 0;
3097 XFS_BTREE_STATS_INC(cur, delrec);
3098 XFS_BTREE_STATS_ADD(cur, moves, numrecs - ptr);
3100 /* Excise the entries being deleted. */
3101 if (level > 0) {
3102 /* It's a nonleaf. operate on keys and ptrs */
3103 union xfs_btree_key *lkp;
3104 union xfs_btree_ptr *lpp;
3106 lkp = xfs_btree_key_addr(cur, ptr + 1, block);
3107 lpp = xfs_btree_ptr_addr(cur, ptr + 1, block);
3109 #ifdef DEBUG
3110 for (i = 0; i < numrecs - ptr; i++) {
3111 error = xfs_btree_check_ptr(cur, lpp, i, level);
3112 if (error)
3113 goto error0;
3115 #endif
3117 if (ptr < numrecs) {
3118 xfs_btree_shift_keys(cur, lkp, -1, numrecs - ptr);
3119 xfs_btree_shift_ptrs(cur, lpp, -1, numrecs - ptr);
3120 xfs_btree_log_keys(cur, bp, ptr, numrecs - 1);
3121 xfs_btree_log_ptrs(cur, bp, ptr, numrecs - 1);
3125 * If it's the first record in the block, we'll need to pass a
3126 * key up to the next level (updkey).
3128 if (ptr == 1)
3129 keyp = xfs_btree_key_addr(cur, 1, block);
3130 } else {
3131 /* It's a leaf. operate on records */
3132 if (ptr < numrecs) {
3133 xfs_btree_shift_recs(cur,
3134 xfs_btree_rec_addr(cur, ptr + 1, block),
3135 -1, numrecs - ptr);
3136 xfs_btree_log_recs(cur, bp, ptr, numrecs - 1);
3140 * If it's the first record in the block, we'll need a key
3141 * structure to pass up to the next level (updkey).
3143 if (ptr == 1) {
3144 cur->bc_ops->init_key_from_rec(&key,
3145 xfs_btree_rec_addr(cur, 1, block));
3146 keyp = &key;
3151 * Decrement and log the number of entries in the block.
3153 xfs_btree_set_numrecs(block, --numrecs);
3154 xfs_btree_log_block(cur, bp, XFS_BB_NUMRECS);
3157 * If we are tracking the last record in the tree and
3158 * we are at the far right edge of the tree, update it.
3160 if (xfs_btree_is_lastrec(cur, block, level)) {
3161 cur->bc_ops->update_lastrec(cur, block, NULL,
3162 ptr, LASTREC_DELREC);
3166 * We're at the root level. First, shrink the root block in-memory.
3167 * Try to get rid of the next level down. If we can't then there's
3168 * nothing left to do.
3170 if (level == cur->bc_nlevels - 1) {
3171 if (cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) {
3172 xfs_iroot_realloc(cur->bc_private.b.ip, -1,
3173 cur->bc_private.b.whichfork);
3175 error = xfs_btree_kill_iroot(cur);
3176 if (error)
3177 goto error0;
3179 error = xfs_btree_dec_cursor(cur, level, stat);
3180 if (error)
3181 goto error0;
3182 *stat = 1;
3183 return 0;
3187 * If this is the root level, and there's only one entry left,
3188 * and it's NOT the leaf level, then we can get rid of this
3189 * level.
3191 if (numrecs == 1 && level > 0) {
3192 union xfs_btree_ptr *pp;
3194 * pp is still set to the first pointer in the block.
3195 * Make it the new root of the btree.
3197 pp = xfs_btree_ptr_addr(cur, 1, block);
3198 error = cur->bc_ops->kill_root(cur, bp, level, pp);
3199 if (error)
3200 goto error0;
3201 } else if (level > 0) {
3202 error = xfs_btree_dec_cursor(cur, level, stat);
3203 if (error)
3204 goto error0;
3206 *stat = 1;
3207 return 0;
3211 * If we deleted the leftmost entry in the block, update the
3212 * key values above us in the tree.
3214 if (ptr == 1) {
3215 error = xfs_btree_updkey(cur, keyp, level + 1);
3216 if (error)
3217 goto error0;
3221 * If the number of records remaining in the block is at least
3222 * the minimum, we're done.
3224 if (numrecs >= cur->bc_ops->get_minrecs(cur, level)) {
3225 error = xfs_btree_dec_cursor(cur, level, stat);
3226 if (error)
3227 goto error0;
3228 return 0;
3232 * Otherwise, we have to move some records around to keep the
3233 * tree balanced. Look at the left and right sibling blocks to
3234 * see if we can re-balance by moving only one record.
3236 xfs_btree_get_sibling(cur, block, &rptr, XFS_BB_RIGHTSIB);
3237 xfs_btree_get_sibling(cur, block, &lptr, XFS_BB_LEFTSIB);
3239 if (cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) {
3241 * One child of root, need to get a chance to copy its contents
3242 * into the root and delete it. Can't go up to next level,
3243 * there's nothing to delete there.
3245 if (xfs_btree_ptr_is_null(cur, &rptr) &&
3246 xfs_btree_ptr_is_null(cur, &lptr) &&
3247 level == cur->bc_nlevels - 2) {
3248 error = xfs_btree_kill_iroot(cur);
3249 if (!error)
3250 error = xfs_btree_dec_cursor(cur, level, stat);
3251 if (error)
3252 goto error0;
3253 return 0;
3257 ASSERT(!xfs_btree_ptr_is_null(cur, &rptr) ||
3258 !xfs_btree_ptr_is_null(cur, &lptr));
3261 * Duplicate the cursor so our btree manipulations here won't
3262 * disrupt the next level up.
3264 error = xfs_btree_dup_cursor(cur, &tcur);
3265 if (error)
3266 goto error0;
3269 * If there's a right sibling, see if it's ok to shift an entry
3270 * out of it.
3272 if (!xfs_btree_ptr_is_null(cur, &rptr)) {
3274 * Move the temp cursor to the last entry in the next block.
3275 * Actually any entry but the first would suffice.
3277 i = xfs_btree_lastrec(tcur, level);
3278 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
3280 error = xfs_btree_increment(tcur, level, &i);
3281 if (error)
3282 goto error0;
3283 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
3285 i = xfs_btree_lastrec(tcur, level);
3286 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
3288 /* Grab a pointer to the block. */
3289 right = xfs_btree_get_block(tcur, level, &rbp);
3290 #ifdef DEBUG
3291 error = xfs_btree_check_block(tcur, right, level, rbp);
3292 if (error)
3293 goto error0;
3294 #endif
3295 /* Grab the current block number, for future use. */
3296 xfs_btree_get_sibling(tcur, right, &cptr, XFS_BB_LEFTSIB);
3299 * If right block is full enough so that removing one entry
3300 * won't make it too empty, and left-shifting an entry out
3301 * of right to us works, we're done.
3303 if (xfs_btree_get_numrecs(right) - 1 >=
3304 cur->bc_ops->get_minrecs(tcur, level)) {
3305 error = xfs_btree_lshift(tcur, level, &i);
3306 if (error)
3307 goto error0;
3308 if (i) {
3309 ASSERT(xfs_btree_get_numrecs(block) >=
3310 cur->bc_ops->get_minrecs(tcur, level));
3312 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
3313 tcur = NULL;
3315 error = xfs_btree_dec_cursor(cur, level, stat);
3316 if (error)
3317 goto error0;
3318 return 0;
3323 * Otherwise, grab the number of records in right for
3324 * future reference, and fix up the temp cursor to point
3325 * to our block again (last record).
3327 rrecs = xfs_btree_get_numrecs(right);
3328 if (!xfs_btree_ptr_is_null(cur, &lptr)) {
3329 i = xfs_btree_firstrec(tcur, level);
3330 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
3332 error = xfs_btree_decrement(tcur, level, &i);
3333 if (error)
3334 goto error0;
3335 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
3340 * If there's a left sibling, see if it's ok to shift an entry
3341 * out of it.
3343 if (!xfs_btree_ptr_is_null(cur, &lptr)) {
3345 * Move the temp cursor to the first entry in the
3346 * previous block.
3348 i = xfs_btree_firstrec(tcur, level);
3349 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
3351 error = xfs_btree_decrement(tcur, level, &i);
3352 if (error)
3353 goto error0;
3354 i = xfs_btree_firstrec(tcur, level);
3355 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
3357 /* Grab a pointer to the block. */
3358 left = xfs_btree_get_block(tcur, level, &lbp);
3359 #ifdef DEBUG
3360 error = xfs_btree_check_block(cur, left, level, lbp);
3361 if (error)
3362 goto error0;
3363 #endif
3364 /* Grab the current block number, for future use. */
3365 xfs_btree_get_sibling(tcur, left, &cptr, XFS_BB_RIGHTSIB);
3368 * If left block is full enough so that removing one entry
3369 * won't make it too empty, and right-shifting an entry out
3370 * of left to us works, we're done.
3372 if (xfs_btree_get_numrecs(left) - 1 >=
3373 cur->bc_ops->get_minrecs(tcur, level)) {
3374 error = xfs_btree_rshift(tcur, level, &i);
3375 if (error)
3376 goto error0;
3377 if (i) {
3378 ASSERT(xfs_btree_get_numrecs(block) >=
3379 cur->bc_ops->get_minrecs(tcur, level));
3380 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
3381 tcur = NULL;
3382 if (level == 0)
3383 cur->bc_ptrs[0]++;
3384 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3385 *stat = 1;
3386 return 0;
3391 * Otherwise, grab the number of records in right for
3392 * future reference.
3394 lrecs = xfs_btree_get_numrecs(left);
3397 /* Delete the temp cursor, we're done with it. */
3398 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
3399 tcur = NULL;
3401 /* If here, we need to do a join to keep the tree balanced. */
3402 ASSERT(!xfs_btree_ptr_is_null(cur, &cptr));
3404 if (!xfs_btree_ptr_is_null(cur, &lptr) &&
3405 lrecs + xfs_btree_get_numrecs(block) <=
3406 cur->bc_ops->get_maxrecs(cur, level)) {
3408 * Set "right" to be the starting block,
3409 * "left" to be the left neighbor.
3411 rptr = cptr;
3412 right = block;
3413 rbp = bp;
3414 error = xfs_btree_read_buf_block(cur, &lptr, level,
3415 0, &left, &lbp);
3416 if (error)
3417 goto error0;
3420 * If that won't work, see if we can join with the right neighbor block.
3422 } else if (!xfs_btree_ptr_is_null(cur, &rptr) &&
3423 rrecs + xfs_btree_get_numrecs(block) <=
3424 cur->bc_ops->get_maxrecs(cur, level)) {
3426 * Set "left" to be the starting block,
3427 * "right" to be the right neighbor.
3429 lptr = cptr;
3430 left = block;
3431 lbp = bp;
3432 error = xfs_btree_read_buf_block(cur, &rptr, level,
3433 0, &right, &rbp);
3434 if (error)
3435 goto error0;
3438 * Otherwise, we can't fix the imbalance.
3439 * Just return. This is probably a logic error, but it's not fatal.
3441 } else {
3442 error = xfs_btree_dec_cursor(cur, level, stat);
3443 if (error)
3444 goto error0;
3445 return 0;
3448 rrecs = xfs_btree_get_numrecs(right);
3449 lrecs = xfs_btree_get_numrecs(left);
3452 * We're now going to join "left" and "right" by moving all the stuff
3453 * in "right" to "left" and deleting "right".
3455 XFS_BTREE_STATS_ADD(cur, moves, rrecs);
3456 if (level > 0) {
3457 /* It's a non-leaf. Move keys and pointers. */
3458 union xfs_btree_key *lkp; /* left btree key */
3459 union xfs_btree_ptr *lpp; /* left address pointer */
3460 union xfs_btree_key *rkp; /* right btree key */
3461 union xfs_btree_ptr *rpp; /* right address pointer */
3463 lkp = xfs_btree_key_addr(cur, lrecs + 1, left);
3464 lpp = xfs_btree_ptr_addr(cur, lrecs + 1, left);
3465 rkp = xfs_btree_key_addr(cur, 1, right);
3466 rpp = xfs_btree_ptr_addr(cur, 1, right);
3467 #ifdef DEBUG
3468 for (i = 1; i < rrecs; i++) {
3469 error = xfs_btree_check_ptr(cur, rpp, i, level);
3470 if (error)
3471 goto error0;
3473 #endif
3474 xfs_btree_copy_keys(cur, lkp, rkp, rrecs);
3475 xfs_btree_copy_ptrs(cur, lpp, rpp, rrecs);
3477 xfs_btree_log_keys(cur, lbp, lrecs + 1, lrecs + rrecs);
3478 xfs_btree_log_ptrs(cur, lbp, lrecs + 1, lrecs + rrecs);
3479 } else {
3480 /* It's a leaf. Move records. */
3481 union xfs_btree_rec *lrp; /* left record pointer */
3482 union xfs_btree_rec *rrp; /* right record pointer */
3484 lrp = xfs_btree_rec_addr(cur, lrecs + 1, left);
3485 rrp = xfs_btree_rec_addr(cur, 1, right);
3487 xfs_btree_copy_recs(cur, lrp, rrp, rrecs);
3488 xfs_btree_log_recs(cur, lbp, lrecs + 1, lrecs + rrecs);
3491 XFS_BTREE_STATS_INC(cur, join);
3494 * Fix up the number of records and right block pointer in the
3495 * surviving block, and log it.
3497 xfs_btree_set_numrecs(left, lrecs + rrecs);
3498 xfs_btree_get_sibling(cur, right, &cptr, XFS_BB_RIGHTSIB),
3499 xfs_btree_set_sibling(cur, left, &cptr, XFS_BB_RIGHTSIB);
3500 xfs_btree_log_block(cur, lbp, XFS_BB_NUMRECS | XFS_BB_RIGHTSIB);
3502 /* If there is a right sibling, point it to the remaining block. */
3503 xfs_btree_get_sibling(cur, left, &cptr, XFS_BB_RIGHTSIB);
3504 if (!xfs_btree_ptr_is_null(cur, &cptr)) {
3505 error = xfs_btree_read_buf_block(cur, &cptr, level,
3506 0, &rrblock, &rrbp);
3507 if (error)
3508 goto error0;
3509 xfs_btree_set_sibling(cur, rrblock, &lptr, XFS_BB_LEFTSIB);
3510 xfs_btree_log_block(cur, rrbp, XFS_BB_LEFTSIB);
3513 /* Free the deleted block. */
3514 error = cur->bc_ops->free_block(cur, rbp);
3515 if (error)
3516 goto error0;
3517 XFS_BTREE_STATS_INC(cur, free);
3520 * If we joined with the left neighbor, set the buffer in the
3521 * cursor to the left block, and fix up the index.
3523 if (bp != lbp) {
3524 cur->bc_bufs[level] = lbp;
3525 cur->bc_ptrs[level] += lrecs;
3526 cur->bc_ra[level] = 0;
3529 * If we joined with the right neighbor and there's a level above
3530 * us, increment the cursor at that level.
3532 else if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) ||
3533 (level + 1 < cur->bc_nlevels)) {
3534 error = xfs_btree_increment(cur, level + 1, &i);
3535 if (error)
3536 goto error0;
3540 * Readjust the ptr at this level if it's not a leaf, since it's
3541 * still pointing at the deletion point, which makes the cursor
3542 * inconsistent. If this makes the ptr 0, the caller fixes it up.
3543 * We can't use decrement because it would change the next level up.
3545 if (level > 0)
3546 cur->bc_ptrs[level]--;
3548 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3549 /* Return value means the next level up has something to do. */
3550 *stat = 2;
3551 return 0;
3553 error0:
3554 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
3555 if (tcur)
3556 xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
3557 return error;
3561 * Delete the record pointed to by cur.
3562 * The cursor refers to the place where the record was (could be inserted)
3563 * when the operation returns.
3565 int /* error */
3566 xfs_btree_delete(
3567 struct xfs_btree_cur *cur,
3568 int *stat) /* success/failure */
3570 int error; /* error return value */
3571 int level;
3572 int i;
3574 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
3577 * Go up the tree, starting at leaf level.
3579 * If 2 is returned then a join was done; go to the next level.
3580 * Otherwise we are done.
3582 for (level = 0, i = 2; i == 2; level++) {
3583 error = xfs_btree_delrec(cur, level, &i);
3584 if (error)
3585 goto error0;
3588 if (i == 0) {
3589 for (level = 1; level < cur->bc_nlevels; level++) {
3590 if (cur->bc_ptrs[level] == 0) {
3591 error = xfs_btree_decrement(cur, level, &i);
3592 if (error)
3593 goto error0;
3594 break;
3599 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3600 *stat = i;
3601 return 0;
3602 error0:
3603 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
3604 return error;
3608 * Get the data from the pointed-to record.
3610 int /* error */
3611 xfs_btree_get_rec(
3612 struct xfs_btree_cur *cur, /* btree cursor */
3613 union xfs_btree_rec **recp, /* output: btree record */
3614 int *stat) /* output: success/failure */
3616 struct xfs_btree_block *block; /* btree block */
3617 struct xfs_buf *bp; /* buffer pointer */
3618 int ptr; /* record number */
3619 #ifdef DEBUG
3620 int error; /* error return value */
3621 #endif
3623 ptr = cur->bc_ptrs[0];
3624 block = xfs_btree_get_block(cur, 0, &bp);
3626 #ifdef DEBUG
3627 error = xfs_btree_check_block(cur, block, 0, bp);
3628 if (error)
3629 return error;
3630 #endif
3633 * Off the right end or left end, return failure.
3635 if (ptr > xfs_btree_get_numrecs(block) || ptr <= 0) {
3636 *stat = 0;
3637 return 0;
3641 * Point to the record and extract its data.
3643 *recp = xfs_btree_rec_addr(cur, ptr, block);
3644 *stat = 1;
3645 return 0;