fed up with those stupid warnings
[mmotm.git] / fs / xfs / xfs_btree.c
blob52b5f14d0c32ef4573074b4e5962d5055aaec643
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_dir2.h"
28 #include "xfs_dmapi.h"
29 #include "xfs_mount.h"
30 #include "xfs_bmap_btree.h"
31 #include "xfs_alloc_btree.h"
32 #include "xfs_ialloc_btree.h"
33 #include "xfs_dir2_sf.h"
34 #include "xfs_attr_sf.h"
35 #include "xfs_dinode.h"
36 #include "xfs_inode.h"
37 #include "xfs_inode_item.h"
38 #include "xfs_btree.h"
39 #include "xfs_btree_trace.h"
40 #include "xfs_ialloc.h"
41 #include "xfs_error.h"
44 * Cursor allocation zone.
46 kmem_zone_t *xfs_btree_cur_zone;
49 * Btree magic numbers.
51 const __uint32_t xfs_magics[XFS_BTNUM_MAX] = {
52 XFS_ABTB_MAGIC, XFS_ABTC_MAGIC, XFS_BMAP_MAGIC, XFS_IBT_MAGIC
56 STATIC int /* error (0 or EFSCORRUPTED) */
57 xfs_btree_check_lblock(
58 struct xfs_btree_cur *cur, /* btree cursor */
59 struct xfs_btree_block *block, /* btree long form block pointer */
60 int level, /* level of the btree block */
61 struct xfs_buf *bp) /* buffer for block, if any */
63 int lblock_ok; /* block passes checks */
64 struct xfs_mount *mp; /* file system mount point */
66 mp = cur->bc_mp;
67 lblock_ok =
68 be32_to_cpu(block->bb_magic) == xfs_magics[cur->bc_btnum] &&
69 be16_to_cpu(block->bb_level) == level &&
70 be16_to_cpu(block->bb_numrecs) <=
71 cur->bc_ops->get_maxrecs(cur, level) &&
72 block->bb_u.l.bb_leftsib &&
73 (be64_to_cpu(block->bb_u.l.bb_leftsib) == NULLDFSBNO ||
74 XFS_FSB_SANITY_CHECK(mp,
75 be64_to_cpu(block->bb_u.l.bb_leftsib))) &&
76 block->bb_u.l.bb_rightsib &&
77 (be64_to_cpu(block->bb_u.l.bb_rightsib) == NULLDFSBNO ||
78 XFS_FSB_SANITY_CHECK(mp,
79 be64_to_cpu(block->bb_u.l.bb_rightsib)));
80 if (unlikely(XFS_TEST_ERROR(!lblock_ok, mp,
81 XFS_ERRTAG_BTREE_CHECK_LBLOCK,
82 XFS_RANDOM_BTREE_CHECK_LBLOCK))) {
83 if (bp)
84 xfs_buftrace("LBTREE ERROR", bp);
85 XFS_ERROR_REPORT("xfs_btree_check_lblock", XFS_ERRLEVEL_LOW,
86 mp);
87 return XFS_ERROR(EFSCORRUPTED);
89 return 0;
92 STATIC int /* error (0 or EFSCORRUPTED) */
93 xfs_btree_check_sblock(
94 struct xfs_btree_cur *cur, /* btree cursor */
95 struct xfs_btree_block *block, /* btree short form block pointer */
96 int level, /* level of the btree block */
97 struct xfs_buf *bp) /* buffer containing block */
99 struct xfs_buf *agbp; /* buffer for ag. freespace struct */
100 struct xfs_agf *agf; /* ag. freespace structure */
101 xfs_agblock_t agflen; /* native ag. freespace length */
102 int sblock_ok; /* block passes checks */
104 agbp = cur->bc_private.a.agbp;
105 agf = XFS_BUF_TO_AGF(agbp);
106 agflen = be32_to_cpu(agf->agf_length);
107 sblock_ok =
108 be32_to_cpu(block->bb_magic) == xfs_magics[cur->bc_btnum] &&
109 be16_to_cpu(block->bb_level) == level &&
110 be16_to_cpu(block->bb_numrecs) <=
111 cur->bc_ops->get_maxrecs(cur, level) &&
112 (be32_to_cpu(block->bb_u.s.bb_leftsib) == NULLAGBLOCK ||
113 be32_to_cpu(block->bb_u.s.bb_leftsib) < agflen) &&
114 block->bb_u.s.bb_leftsib &&
115 (be32_to_cpu(block->bb_u.s.bb_rightsib) == NULLAGBLOCK ||
116 be32_to_cpu(block->bb_u.s.bb_rightsib) < agflen) &&
117 block->bb_u.s.bb_rightsib;
118 if (unlikely(XFS_TEST_ERROR(!sblock_ok, cur->bc_mp,
119 XFS_ERRTAG_BTREE_CHECK_SBLOCK,
120 XFS_RANDOM_BTREE_CHECK_SBLOCK))) {
121 if (bp)
122 xfs_buftrace("SBTREE ERROR", bp);
123 XFS_CORRUPTION_ERROR("xfs_btree_check_sblock",
124 XFS_ERRLEVEL_LOW, cur->bc_mp, block);
125 return XFS_ERROR(EFSCORRUPTED);
127 return 0;
131 * Debug routine: check that block header is ok.
134 xfs_btree_check_block(
135 struct xfs_btree_cur *cur, /* btree cursor */
136 struct xfs_btree_block *block, /* generic btree block pointer */
137 int level, /* level of the btree block */
138 struct xfs_buf *bp) /* buffer containing block, if any */
140 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
141 return xfs_btree_check_lblock(cur, block, level, bp);
142 else
143 return xfs_btree_check_sblock(cur, block, level, bp);
147 * Check that (long) pointer is ok.
149 int /* error (0 or EFSCORRUPTED) */
150 xfs_btree_check_lptr(
151 struct xfs_btree_cur *cur, /* btree cursor */
152 xfs_dfsbno_t bno, /* btree block disk address */
153 int level) /* btree block level */
155 XFS_WANT_CORRUPTED_RETURN(
156 level > 0 &&
157 bno != NULLDFSBNO &&
158 XFS_FSB_SANITY_CHECK(cur->bc_mp, bno));
159 return 0;
162 #ifdef DEBUG
164 * Check that (short) pointer is ok.
166 STATIC int /* error (0 or EFSCORRUPTED) */
167 xfs_btree_check_sptr(
168 struct xfs_btree_cur *cur, /* btree cursor */
169 xfs_agblock_t bno, /* btree block disk address */
170 int level) /* btree block level */
172 xfs_agblock_t agblocks = cur->bc_mp->m_sb.sb_agblocks;
174 XFS_WANT_CORRUPTED_RETURN(
175 level > 0 &&
176 bno != NULLAGBLOCK &&
177 bno != 0 &&
178 bno < agblocks);
179 return 0;
183 * Check that block ptr is ok.
185 STATIC int /* error (0 or EFSCORRUPTED) */
186 xfs_btree_check_ptr(
187 struct xfs_btree_cur *cur, /* btree cursor */
188 union xfs_btree_ptr *ptr, /* btree block disk address */
189 int index, /* offset from ptr to check */
190 int level) /* btree block level */
192 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
193 return xfs_btree_check_lptr(cur,
194 be64_to_cpu((&ptr->l)[index]), level);
195 } else {
196 return xfs_btree_check_sptr(cur,
197 be32_to_cpu((&ptr->s)[index]), level);
200 #endif
203 * Delete the btree cursor.
205 void
206 xfs_btree_del_cursor(
207 xfs_btree_cur_t *cur, /* btree cursor */
208 int error) /* del because of error */
210 int i; /* btree level */
213 * Clear the buffer pointers, and release the buffers.
214 * If we're doing this in the face of an error, we
215 * need to make sure to inspect all of the entries
216 * in the bc_bufs array for buffers to be unlocked.
217 * This is because some of the btree code works from
218 * level n down to 0, and if we get an error along
219 * the way we won't have initialized all the entries
220 * down to 0.
222 for (i = 0; i < cur->bc_nlevels; i++) {
223 if (cur->bc_bufs[i])
224 xfs_btree_setbuf(cur, i, NULL);
225 else if (!error)
226 break;
229 * Can't free a bmap cursor without having dealt with the
230 * allocated indirect blocks' accounting.
232 ASSERT(cur->bc_btnum != XFS_BTNUM_BMAP ||
233 cur->bc_private.b.allocated == 0);
235 * Free the cursor.
237 kmem_zone_free(xfs_btree_cur_zone, cur);
241 * Duplicate the btree cursor.
242 * Allocate a new one, copy the record, re-get the buffers.
244 int /* error */
245 xfs_btree_dup_cursor(
246 xfs_btree_cur_t *cur, /* input cursor */
247 xfs_btree_cur_t **ncur) /* output cursor */
249 xfs_buf_t *bp; /* btree block's buffer pointer */
250 int error; /* error return value */
251 int i; /* level number of btree block */
252 xfs_mount_t *mp; /* mount structure for filesystem */
253 xfs_btree_cur_t *new; /* new cursor value */
254 xfs_trans_t *tp; /* transaction pointer, can be NULL */
256 tp = cur->bc_tp;
257 mp = cur->bc_mp;
260 * Allocate a new cursor like the old one.
262 new = cur->bc_ops->dup_cursor(cur);
265 * Copy the record currently in the cursor.
267 new->bc_rec = cur->bc_rec;
270 * For each level current, re-get the buffer and copy the ptr value.
272 for (i = 0; i < new->bc_nlevels; i++) {
273 new->bc_ptrs[i] = cur->bc_ptrs[i];
274 new->bc_ra[i] = cur->bc_ra[i];
275 if ((bp = cur->bc_bufs[i])) {
276 if ((error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp,
277 XFS_BUF_ADDR(bp), mp->m_bsize, 0, &bp))) {
278 xfs_btree_del_cursor(new, error);
279 *ncur = NULL;
280 return error;
282 new->bc_bufs[i] = bp;
283 ASSERT(bp);
284 ASSERT(!XFS_BUF_GETERROR(bp));
285 } else
286 new->bc_bufs[i] = NULL;
288 *ncur = new;
289 return 0;
293 * XFS btree block layout and addressing:
295 * There are two types of blocks in the btree: leaf and non-leaf blocks.
297 * The leaf record start with a header then followed by records containing
298 * the values. A non-leaf block also starts with the same header, and
299 * then first contains lookup keys followed by an equal number of pointers
300 * to the btree blocks at the previous level.
302 * +--------+-------+-------+-------+-------+-------+-------+
303 * Leaf: | header | rec 1 | rec 2 | rec 3 | rec 4 | rec 5 | rec N |
304 * +--------+-------+-------+-------+-------+-------+-------+
306 * +--------+-------+-------+-------+-------+-------+-------+
307 * Non-Leaf: | header | key 1 | key 2 | key N | ptr 1 | ptr 2 | ptr N |
308 * +--------+-------+-------+-------+-------+-------+-------+
310 * The header is called struct xfs_btree_block for reasons better left unknown
311 * and comes in different versions for short (32bit) and long (64bit) block
312 * pointers. The record and key structures are defined by the btree instances
313 * and opaque to the btree core. The block pointers are simple disk endian
314 * integers, available in a short (32bit) and long (64bit) variant.
316 * The helpers below calculate the offset of a given record, key or pointer
317 * into a btree block (xfs_btree_*_offset) or return a pointer to the given
318 * record, key or pointer (xfs_btree_*_addr). Note that all addressing
319 * inside the btree block is done using indices starting at one, not zero!
323 * Return size of the btree block header for this btree instance.
325 static inline size_t xfs_btree_block_len(struct xfs_btree_cur *cur)
327 return (cur->bc_flags & XFS_BTREE_LONG_PTRS) ?
328 XFS_BTREE_LBLOCK_LEN :
329 XFS_BTREE_SBLOCK_LEN;
333 * Return size of btree block pointers for this btree instance.
335 static inline size_t xfs_btree_ptr_len(struct xfs_btree_cur *cur)
337 return (cur->bc_flags & XFS_BTREE_LONG_PTRS) ?
338 sizeof(__be64) : sizeof(__be32);
342 * Calculate offset of the n-th record in a btree block.
344 STATIC size_t
345 xfs_btree_rec_offset(
346 struct xfs_btree_cur *cur,
347 int n)
349 return xfs_btree_block_len(cur) +
350 (n - 1) * cur->bc_ops->rec_len;
354 * Calculate offset of the n-th key in a btree block.
356 STATIC size_t
357 xfs_btree_key_offset(
358 struct xfs_btree_cur *cur,
359 int n)
361 return xfs_btree_block_len(cur) +
362 (n - 1) * cur->bc_ops->key_len;
366 * Calculate offset of the n-th block pointer in a btree block.
368 STATIC size_t
369 xfs_btree_ptr_offset(
370 struct xfs_btree_cur *cur,
371 int n,
372 int level)
374 return xfs_btree_block_len(cur) +
375 cur->bc_ops->get_maxrecs(cur, level) * cur->bc_ops->key_len +
376 (n - 1) * xfs_btree_ptr_len(cur);
380 * Return a pointer to the n-th record in the btree block.
382 STATIC union xfs_btree_rec *
383 xfs_btree_rec_addr(
384 struct xfs_btree_cur *cur,
385 int n,
386 struct xfs_btree_block *block)
388 return (union xfs_btree_rec *)
389 ((char *)block + xfs_btree_rec_offset(cur, n));
393 * Return a pointer to the n-th key in the btree block.
395 STATIC union xfs_btree_key *
396 xfs_btree_key_addr(
397 struct xfs_btree_cur *cur,
398 int n,
399 struct xfs_btree_block *block)
401 return (union xfs_btree_key *)
402 ((char *)block + xfs_btree_key_offset(cur, n));
406 * Return a pointer to the n-th block pointer in the btree block.
408 STATIC union xfs_btree_ptr *
409 xfs_btree_ptr_addr(
410 struct xfs_btree_cur *cur,
411 int n,
412 struct xfs_btree_block *block)
414 int level = xfs_btree_get_level(block);
416 ASSERT(block->bb_level != 0);
418 return (union xfs_btree_ptr *)
419 ((char *)block + xfs_btree_ptr_offset(cur, n, level));
423 * Get a the root block which is stored in the inode.
425 * For now this btree implementation assumes the btree root is always
426 * stored in the if_broot field of an inode fork.
428 STATIC struct xfs_btree_block *
429 xfs_btree_get_iroot(
430 struct xfs_btree_cur *cur)
432 struct xfs_ifork *ifp;
434 ifp = XFS_IFORK_PTR(cur->bc_private.b.ip, cur->bc_private.b.whichfork);
435 return (struct xfs_btree_block *)ifp->if_broot;
439 * Retrieve the block pointer from the cursor at the given level.
440 * This may be an inode btree root or from a buffer.
442 STATIC struct xfs_btree_block * /* generic btree block pointer */
443 xfs_btree_get_block(
444 struct xfs_btree_cur *cur, /* btree cursor */
445 int level, /* level in btree */
446 struct xfs_buf **bpp) /* buffer containing the block */
448 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
449 (level == cur->bc_nlevels - 1)) {
450 *bpp = NULL;
451 return xfs_btree_get_iroot(cur);
454 *bpp = cur->bc_bufs[level];
455 return XFS_BUF_TO_BLOCK(*bpp);
459 * Get a buffer for the block, return it with no data read.
460 * Long-form addressing.
462 xfs_buf_t * /* buffer for fsbno */
463 xfs_btree_get_bufl(
464 xfs_mount_t *mp, /* file system mount point */
465 xfs_trans_t *tp, /* transaction pointer */
466 xfs_fsblock_t fsbno, /* file system block number */
467 uint lock) /* lock flags for get_buf */
469 xfs_buf_t *bp; /* buffer pointer (return value) */
470 xfs_daddr_t d; /* real disk block address */
472 ASSERT(fsbno != NULLFSBLOCK);
473 d = XFS_FSB_TO_DADDR(mp, fsbno);
474 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d, mp->m_bsize, lock);
475 ASSERT(bp);
476 ASSERT(!XFS_BUF_GETERROR(bp));
477 return bp;
481 * Get a buffer for the block, return it with no data read.
482 * Short-form addressing.
484 xfs_buf_t * /* buffer for agno/agbno */
485 xfs_btree_get_bufs(
486 xfs_mount_t *mp, /* file system mount point */
487 xfs_trans_t *tp, /* transaction pointer */
488 xfs_agnumber_t agno, /* allocation group number */
489 xfs_agblock_t agbno, /* allocation group block number */
490 uint lock) /* lock flags for get_buf */
492 xfs_buf_t *bp; /* buffer pointer (return value) */
493 xfs_daddr_t d; /* real disk block address */
495 ASSERT(agno != NULLAGNUMBER);
496 ASSERT(agbno != NULLAGBLOCK);
497 d = XFS_AGB_TO_DADDR(mp, agno, agbno);
498 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d, mp->m_bsize, lock);
499 ASSERT(bp);
500 ASSERT(!XFS_BUF_GETERROR(bp));
501 return bp;
505 * Check for the cursor referring to the last block at the given level.
507 int /* 1=is last block, 0=not last block */
508 xfs_btree_islastblock(
509 xfs_btree_cur_t *cur, /* btree cursor */
510 int level) /* level to check */
512 struct xfs_btree_block *block; /* generic btree block pointer */
513 xfs_buf_t *bp; /* buffer containing block */
515 block = xfs_btree_get_block(cur, level, &bp);
516 xfs_btree_check_block(cur, block, level, bp);
517 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
518 return be64_to_cpu(block->bb_u.l.bb_rightsib) == NULLDFSBNO;
519 else
520 return be32_to_cpu(block->bb_u.s.bb_rightsib) == NULLAGBLOCK;
524 * Change the cursor to point to the first record at the given level.
525 * Other levels are unaffected.
527 STATIC int /* success=1, failure=0 */
528 xfs_btree_firstrec(
529 xfs_btree_cur_t *cur, /* btree cursor */
530 int level) /* level to change */
532 struct xfs_btree_block *block; /* generic btree block pointer */
533 xfs_buf_t *bp; /* buffer containing block */
536 * Get the block pointer for this level.
538 block = xfs_btree_get_block(cur, level, &bp);
539 xfs_btree_check_block(cur, block, level, bp);
541 * It's empty, there is no such record.
543 if (!block->bb_numrecs)
544 return 0;
546 * Set the ptr value to 1, that's the first record/key.
548 cur->bc_ptrs[level] = 1;
549 return 1;
553 * Change the cursor to point to the last record in the current block
554 * at the given level. Other levels are unaffected.
556 STATIC int /* success=1, failure=0 */
557 xfs_btree_lastrec(
558 xfs_btree_cur_t *cur, /* btree cursor */
559 int level) /* level to change */
561 struct xfs_btree_block *block; /* generic btree block pointer */
562 xfs_buf_t *bp; /* buffer containing block */
565 * Get the block pointer for this level.
567 block = xfs_btree_get_block(cur, level, &bp);
568 xfs_btree_check_block(cur, block, level, bp);
570 * It's empty, there is no such record.
572 if (!block->bb_numrecs)
573 return 0;
575 * Set the ptr value to numrecs, that's the last record/key.
577 cur->bc_ptrs[level] = be16_to_cpu(block->bb_numrecs);
578 return 1;
582 * Compute first and last byte offsets for the fields given.
583 * Interprets the offsets table, which contains struct field offsets.
585 void
586 xfs_btree_offsets(
587 __int64_t fields, /* bitmask of fields */
588 const short *offsets, /* table of field offsets */
589 int nbits, /* number of bits to inspect */
590 int *first, /* output: first byte offset */
591 int *last) /* output: last byte offset */
593 int i; /* current bit number */
594 __int64_t imask; /* mask for current bit number */
596 ASSERT(fields != 0);
598 * Find the lowest bit, so the first byte offset.
600 for (i = 0, imask = 1LL; ; i++, imask <<= 1) {
601 if (imask & fields) {
602 *first = offsets[i];
603 break;
607 * Find the highest bit, so the last byte offset.
609 for (i = nbits - 1, imask = 1LL << i; ; i--, imask >>= 1) {
610 if (imask & fields) {
611 *last = offsets[i + 1] - 1;
612 break;
618 * Get a buffer for the block, return it read in.
619 * Long-form addressing.
621 int /* error */
622 xfs_btree_read_bufl(
623 xfs_mount_t *mp, /* file system mount point */
624 xfs_trans_t *tp, /* transaction pointer */
625 xfs_fsblock_t fsbno, /* file system block number */
626 uint lock, /* lock flags for read_buf */
627 xfs_buf_t **bpp, /* buffer for fsbno */
628 int refval) /* ref count value for buffer */
630 xfs_buf_t *bp; /* return value */
631 xfs_daddr_t d; /* real disk block address */
632 int error;
634 ASSERT(fsbno != NULLFSBLOCK);
635 d = XFS_FSB_TO_DADDR(mp, fsbno);
636 if ((error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, d,
637 mp->m_bsize, lock, &bp))) {
638 return error;
640 ASSERT(!bp || !XFS_BUF_GETERROR(bp));
641 if (bp != NULL) {
642 XFS_BUF_SET_VTYPE_REF(bp, B_FS_MAP, refval);
644 *bpp = bp;
645 return 0;
649 * Read-ahead the block, don't wait for it, don't return a buffer.
650 * Long-form addressing.
652 /* ARGSUSED */
653 void
654 xfs_btree_reada_bufl(
655 xfs_mount_t *mp, /* file system mount point */
656 xfs_fsblock_t fsbno, /* file system block number */
657 xfs_extlen_t count) /* count of filesystem blocks */
659 xfs_daddr_t d;
661 ASSERT(fsbno != NULLFSBLOCK);
662 d = XFS_FSB_TO_DADDR(mp, fsbno);
663 xfs_baread(mp->m_ddev_targp, d, mp->m_bsize * count);
667 * Read-ahead the block, don't wait for it, don't return a buffer.
668 * Short-form addressing.
670 /* ARGSUSED */
671 void
672 xfs_btree_reada_bufs(
673 xfs_mount_t *mp, /* file system mount point */
674 xfs_agnumber_t agno, /* allocation group number */
675 xfs_agblock_t agbno, /* allocation group block number */
676 xfs_extlen_t count) /* count of filesystem blocks */
678 xfs_daddr_t d;
680 ASSERT(agno != NULLAGNUMBER);
681 ASSERT(agbno != NULLAGBLOCK);
682 d = XFS_AGB_TO_DADDR(mp, agno, agbno);
683 xfs_baread(mp->m_ddev_targp, d, mp->m_bsize * count);
686 STATIC int
687 xfs_btree_readahead_lblock(
688 struct xfs_btree_cur *cur,
689 int lr,
690 struct xfs_btree_block *block)
692 int rval = 0;
693 xfs_dfsbno_t left = be64_to_cpu(block->bb_u.l.bb_leftsib);
694 xfs_dfsbno_t right = be64_to_cpu(block->bb_u.l.bb_rightsib);
696 if ((lr & XFS_BTCUR_LEFTRA) && left != NULLDFSBNO) {
697 xfs_btree_reada_bufl(cur->bc_mp, left, 1);
698 rval++;
701 if ((lr & XFS_BTCUR_RIGHTRA) && right != NULLDFSBNO) {
702 xfs_btree_reada_bufl(cur->bc_mp, right, 1);
703 rval++;
706 return rval;
709 STATIC int
710 xfs_btree_readahead_sblock(
711 struct xfs_btree_cur *cur,
712 int lr,
713 struct xfs_btree_block *block)
715 int rval = 0;
716 xfs_agblock_t left = be32_to_cpu(block->bb_u.s.bb_leftsib);
717 xfs_agblock_t right = be32_to_cpu(block->bb_u.s.bb_rightsib);
720 if ((lr & XFS_BTCUR_LEFTRA) && left != NULLAGBLOCK) {
721 xfs_btree_reada_bufs(cur->bc_mp, cur->bc_private.a.agno,
722 left, 1);
723 rval++;
726 if ((lr & XFS_BTCUR_RIGHTRA) && right != NULLAGBLOCK) {
727 xfs_btree_reada_bufs(cur->bc_mp, cur->bc_private.a.agno,
728 right, 1);
729 rval++;
732 return rval;
736 * Read-ahead btree blocks, at the given level.
737 * Bits in lr are set from XFS_BTCUR_{LEFT,RIGHT}RA.
739 STATIC int
740 xfs_btree_readahead(
741 struct xfs_btree_cur *cur, /* btree cursor */
742 int lev, /* level in btree */
743 int lr) /* left/right bits */
745 struct xfs_btree_block *block;
748 * No readahead needed if we are at the root level and the
749 * btree root is stored in the inode.
751 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
752 (lev == cur->bc_nlevels - 1))
753 return 0;
755 if ((cur->bc_ra[lev] | lr) == cur->bc_ra[lev])
756 return 0;
758 cur->bc_ra[lev] |= lr;
759 block = XFS_BUF_TO_BLOCK(cur->bc_bufs[lev]);
761 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
762 return xfs_btree_readahead_lblock(cur, lr, block);
763 return xfs_btree_readahead_sblock(cur, lr, block);
767 * Set the buffer for level "lev" in the cursor to bp, releasing
768 * any previous buffer.
770 void
771 xfs_btree_setbuf(
772 xfs_btree_cur_t *cur, /* btree cursor */
773 int lev, /* level in btree */
774 xfs_buf_t *bp) /* new buffer to set */
776 struct xfs_btree_block *b; /* btree block */
777 xfs_buf_t *obp; /* old buffer pointer */
779 obp = cur->bc_bufs[lev];
780 if (obp)
781 xfs_trans_brelse(cur->bc_tp, obp);
782 cur->bc_bufs[lev] = bp;
783 cur->bc_ra[lev] = 0;
784 if (!bp)
785 return;
786 b = XFS_BUF_TO_BLOCK(bp);
787 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
788 if (be64_to_cpu(b->bb_u.l.bb_leftsib) == NULLDFSBNO)
789 cur->bc_ra[lev] |= XFS_BTCUR_LEFTRA;
790 if (be64_to_cpu(b->bb_u.l.bb_rightsib) == NULLDFSBNO)
791 cur->bc_ra[lev] |= XFS_BTCUR_RIGHTRA;
792 } else {
793 if (be32_to_cpu(b->bb_u.s.bb_leftsib) == NULLAGBLOCK)
794 cur->bc_ra[lev] |= XFS_BTCUR_LEFTRA;
795 if (be32_to_cpu(b->bb_u.s.bb_rightsib) == NULLAGBLOCK)
796 cur->bc_ra[lev] |= XFS_BTCUR_RIGHTRA;
800 STATIC int
801 xfs_btree_ptr_is_null(
802 struct xfs_btree_cur *cur,
803 union xfs_btree_ptr *ptr)
805 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
806 return be64_to_cpu(ptr->l) == NULLDFSBNO;
807 else
808 return be32_to_cpu(ptr->s) == NULLAGBLOCK;
811 STATIC void
812 xfs_btree_set_ptr_null(
813 struct xfs_btree_cur *cur,
814 union xfs_btree_ptr *ptr)
816 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
817 ptr->l = cpu_to_be64(NULLDFSBNO);
818 else
819 ptr->s = cpu_to_be32(NULLAGBLOCK);
823 * Get/set/init sibling pointers
825 STATIC void
826 xfs_btree_get_sibling(
827 struct xfs_btree_cur *cur,
828 struct xfs_btree_block *block,
829 union xfs_btree_ptr *ptr,
830 int lr)
832 ASSERT(lr == XFS_BB_LEFTSIB || lr == XFS_BB_RIGHTSIB);
834 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
835 if (lr == XFS_BB_RIGHTSIB)
836 ptr->l = block->bb_u.l.bb_rightsib;
837 else
838 ptr->l = block->bb_u.l.bb_leftsib;
839 } else {
840 if (lr == XFS_BB_RIGHTSIB)
841 ptr->s = block->bb_u.s.bb_rightsib;
842 else
843 ptr->s = block->bb_u.s.bb_leftsib;
847 STATIC void
848 xfs_btree_set_sibling(
849 struct xfs_btree_cur *cur,
850 struct xfs_btree_block *block,
851 union xfs_btree_ptr *ptr,
852 int lr)
854 ASSERT(lr == XFS_BB_LEFTSIB || lr == XFS_BB_RIGHTSIB);
856 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
857 if (lr == XFS_BB_RIGHTSIB)
858 block->bb_u.l.bb_rightsib = ptr->l;
859 else
860 block->bb_u.l.bb_leftsib = ptr->l;
861 } else {
862 if (lr == XFS_BB_RIGHTSIB)
863 block->bb_u.s.bb_rightsib = ptr->s;
864 else
865 block->bb_u.s.bb_leftsib = ptr->s;
869 STATIC void
870 xfs_btree_init_block(
871 struct xfs_btree_cur *cur,
872 int level,
873 int numrecs,
874 struct xfs_btree_block *new) /* new block */
876 new->bb_magic = cpu_to_be32(xfs_magics[cur->bc_btnum]);
877 new->bb_level = cpu_to_be16(level);
878 new->bb_numrecs = cpu_to_be16(numrecs);
880 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
881 new->bb_u.l.bb_leftsib = cpu_to_be64(NULLDFSBNO);
882 new->bb_u.l.bb_rightsib = cpu_to_be64(NULLDFSBNO);
883 } else {
884 new->bb_u.s.bb_leftsib = cpu_to_be32(NULLAGBLOCK);
885 new->bb_u.s.bb_rightsib = cpu_to_be32(NULLAGBLOCK);
890 * Return true if ptr is the last record in the btree and
891 * we need to track updateѕ to this record. The decision
892 * will be further refined in the update_lastrec method.
894 STATIC int
895 xfs_btree_is_lastrec(
896 struct xfs_btree_cur *cur,
897 struct xfs_btree_block *block,
898 int level)
900 union xfs_btree_ptr ptr;
902 if (level > 0)
903 return 0;
904 if (!(cur->bc_flags & XFS_BTREE_LASTREC_UPDATE))
905 return 0;
907 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_RIGHTSIB);
908 if (!xfs_btree_ptr_is_null(cur, &ptr))
909 return 0;
910 return 1;
913 STATIC void
914 xfs_btree_buf_to_ptr(
915 struct xfs_btree_cur *cur,
916 struct xfs_buf *bp,
917 union xfs_btree_ptr *ptr)
919 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
920 ptr->l = cpu_to_be64(XFS_DADDR_TO_FSB(cur->bc_mp,
921 XFS_BUF_ADDR(bp)));
922 else {
923 ptr->s = cpu_to_be32(xfs_daddr_to_agbno(cur->bc_mp,
924 XFS_BUF_ADDR(bp)));
928 STATIC xfs_daddr_t
929 xfs_btree_ptr_to_daddr(
930 struct xfs_btree_cur *cur,
931 union xfs_btree_ptr *ptr)
933 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
934 ASSERT(be64_to_cpu(ptr->l) != NULLDFSBNO);
936 return XFS_FSB_TO_DADDR(cur->bc_mp, be64_to_cpu(ptr->l));
937 } else {
938 ASSERT(cur->bc_private.a.agno != NULLAGNUMBER);
939 ASSERT(be32_to_cpu(ptr->s) != NULLAGBLOCK);
941 return XFS_AGB_TO_DADDR(cur->bc_mp, cur->bc_private.a.agno,
942 be32_to_cpu(ptr->s));
946 STATIC void
947 xfs_btree_set_refs(
948 struct xfs_btree_cur *cur,
949 struct xfs_buf *bp)
951 switch (cur->bc_btnum) {
952 case XFS_BTNUM_BNO:
953 case XFS_BTNUM_CNT:
954 XFS_BUF_SET_VTYPE_REF(*bpp, B_FS_MAP, XFS_ALLOC_BTREE_REF);
955 break;
956 case XFS_BTNUM_INO:
957 XFS_BUF_SET_VTYPE_REF(*bpp, B_FS_INOMAP, XFS_INO_BTREE_REF);
958 break;
959 case XFS_BTNUM_BMAP:
960 XFS_BUF_SET_VTYPE_REF(*bpp, B_FS_MAP, XFS_BMAP_BTREE_REF);
961 break;
962 default:
963 ASSERT(0);
967 STATIC int
968 xfs_btree_get_buf_block(
969 struct xfs_btree_cur *cur,
970 union xfs_btree_ptr *ptr,
971 int flags,
972 struct xfs_btree_block **block,
973 struct xfs_buf **bpp)
975 struct xfs_mount *mp = cur->bc_mp;
976 xfs_daddr_t d;
978 /* need to sort out how callers deal with failures first */
979 ASSERT(!(flags & XFS_BUF_TRYLOCK));
981 d = xfs_btree_ptr_to_daddr(cur, ptr);
982 *bpp = xfs_trans_get_buf(cur->bc_tp, mp->m_ddev_targp, d,
983 mp->m_bsize, flags);
985 ASSERT(*bpp);
986 ASSERT(!XFS_BUF_GETERROR(*bpp));
988 *block = XFS_BUF_TO_BLOCK(*bpp);
989 return 0;
993 * Read in the buffer at the given ptr and return the buffer and
994 * the block pointer within the buffer.
996 STATIC int
997 xfs_btree_read_buf_block(
998 struct xfs_btree_cur *cur,
999 union xfs_btree_ptr *ptr,
1000 int level,
1001 int flags,
1002 struct xfs_btree_block **block,
1003 struct xfs_buf **bpp)
1005 struct xfs_mount *mp = cur->bc_mp;
1006 xfs_daddr_t d;
1007 int error;
1009 /* need to sort out how callers deal with failures first */
1010 ASSERT(!(flags & XFS_BUF_TRYLOCK));
1012 d = xfs_btree_ptr_to_daddr(cur, ptr);
1013 error = xfs_trans_read_buf(mp, cur->bc_tp, mp->m_ddev_targp, d,
1014 mp->m_bsize, flags, bpp);
1015 if (error)
1016 return error;
1018 ASSERT(*bpp != NULL);
1019 ASSERT(!XFS_BUF_GETERROR(*bpp));
1021 xfs_btree_set_refs(cur, *bpp);
1022 *block = XFS_BUF_TO_BLOCK(*bpp);
1024 error = xfs_btree_check_block(cur, *block, level, *bpp);
1025 if (error)
1026 xfs_trans_brelse(cur->bc_tp, *bpp);
1027 return error;
1031 * Copy keys from one btree block to another.
1033 STATIC void
1034 xfs_btree_copy_keys(
1035 struct xfs_btree_cur *cur,
1036 union xfs_btree_key *dst_key,
1037 union xfs_btree_key *src_key,
1038 int numkeys)
1040 ASSERT(numkeys >= 0);
1041 memcpy(dst_key, src_key, numkeys * cur->bc_ops->key_len);
1045 * Copy records from one btree block to another.
1047 STATIC void
1048 xfs_btree_copy_recs(
1049 struct xfs_btree_cur *cur,
1050 union xfs_btree_rec *dst_rec,
1051 union xfs_btree_rec *src_rec,
1052 int numrecs)
1054 ASSERT(numrecs >= 0);
1055 memcpy(dst_rec, src_rec, numrecs * cur->bc_ops->rec_len);
1059 * Copy block pointers from one btree block to another.
1061 STATIC void
1062 xfs_btree_copy_ptrs(
1063 struct xfs_btree_cur *cur,
1064 union xfs_btree_ptr *dst_ptr,
1065 union xfs_btree_ptr *src_ptr,
1066 int numptrs)
1068 ASSERT(numptrs >= 0);
1069 memcpy(dst_ptr, src_ptr, numptrs * xfs_btree_ptr_len(cur));
1073 * Shift keys one index left/right inside a single btree block.
1075 STATIC void
1076 xfs_btree_shift_keys(
1077 struct xfs_btree_cur *cur,
1078 union xfs_btree_key *key,
1079 int dir,
1080 int numkeys)
1082 char *dst_key;
1084 ASSERT(numkeys >= 0);
1085 ASSERT(dir == 1 || dir == -1);
1087 dst_key = (char *)key + (dir * cur->bc_ops->key_len);
1088 memmove(dst_key, key, numkeys * cur->bc_ops->key_len);
1092 * Shift records one index left/right inside a single btree block.
1094 STATIC void
1095 xfs_btree_shift_recs(
1096 struct xfs_btree_cur *cur,
1097 union xfs_btree_rec *rec,
1098 int dir,
1099 int numrecs)
1101 char *dst_rec;
1103 ASSERT(numrecs >= 0);
1104 ASSERT(dir == 1 || dir == -1);
1106 dst_rec = (char *)rec + (dir * cur->bc_ops->rec_len);
1107 memmove(dst_rec, rec, numrecs * cur->bc_ops->rec_len);
1111 * Shift block pointers one index left/right inside a single btree block.
1113 STATIC void
1114 xfs_btree_shift_ptrs(
1115 struct xfs_btree_cur *cur,
1116 union xfs_btree_ptr *ptr,
1117 int dir,
1118 int numptrs)
1120 char *dst_ptr;
1122 ASSERT(numptrs >= 0);
1123 ASSERT(dir == 1 || dir == -1);
1125 dst_ptr = (char *)ptr + (dir * xfs_btree_ptr_len(cur));
1126 memmove(dst_ptr, ptr, numptrs * xfs_btree_ptr_len(cur));
1130 * Log key values from the btree block.
1132 STATIC void
1133 xfs_btree_log_keys(
1134 struct xfs_btree_cur *cur,
1135 struct xfs_buf *bp,
1136 int first,
1137 int last)
1139 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1140 XFS_BTREE_TRACE_ARGBII(cur, bp, first, last);
1142 if (bp) {
1143 xfs_trans_log_buf(cur->bc_tp, bp,
1144 xfs_btree_key_offset(cur, first),
1145 xfs_btree_key_offset(cur, last + 1) - 1);
1146 } else {
1147 xfs_trans_log_inode(cur->bc_tp, cur->bc_private.b.ip,
1148 xfs_ilog_fbroot(cur->bc_private.b.whichfork));
1151 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1155 * Log record values from the btree block.
1157 void
1158 xfs_btree_log_recs(
1159 struct xfs_btree_cur *cur,
1160 struct xfs_buf *bp,
1161 int first,
1162 int last)
1164 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1165 XFS_BTREE_TRACE_ARGBII(cur, bp, first, last);
1167 xfs_trans_log_buf(cur->bc_tp, bp,
1168 xfs_btree_rec_offset(cur, first),
1169 xfs_btree_rec_offset(cur, last + 1) - 1);
1171 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1175 * Log block pointer fields from a btree block (nonleaf).
1177 STATIC void
1178 xfs_btree_log_ptrs(
1179 struct xfs_btree_cur *cur, /* btree cursor */
1180 struct xfs_buf *bp, /* buffer containing btree block */
1181 int first, /* index of first pointer to log */
1182 int last) /* index of last pointer to log */
1184 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1185 XFS_BTREE_TRACE_ARGBII(cur, bp, first, last);
1187 if (bp) {
1188 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
1189 int level = xfs_btree_get_level(block);
1191 xfs_trans_log_buf(cur->bc_tp, bp,
1192 xfs_btree_ptr_offset(cur, first, level),
1193 xfs_btree_ptr_offset(cur, last + 1, level) - 1);
1194 } else {
1195 xfs_trans_log_inode(cur->bc_tp, cur->bc_private.b.ip,
1196 xfs_ilog_fbroot(cur->bc_private.b.whichfork));
1199 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1203 * Log fields from a btree block header.
1205 void
1206 xfs_btree_log_block(
1207 struct xfs_btree_cur *cur, /* btree cursor */
1208 struct xfs_buf *bp, /* buffer containing btree block */
1209 int fields) /* mask of fields: XFS_BB_... */
1211 int first; /* first byte offset logged */
1212 int last; /* last byte offset logged */
1213 static const short soffsets[] = { /* table of offsets (short) */
1214 offsetof(struct xfs_btree_block, bb_magic),
1215 offsetof(struct xfs_btree_block, bb_level),
1216 offsetof(struct xfs_btree_block, bb_numrecs),
1217 offsetof(struct xfs_btree_block, bb_u.s.bb_leftsib),
1218 offsetof(struct xfs_btree_block, bb_u.s.bb_rightsib),
1219 XFS_BTREE_SBLOCK_LEN
1221 static const short loffsets[] = { /* table of offsets (long) */
1222 offsetof(struct xfs_btree_block, bb_magic),
1223 offsetof(struct xfs_btree_block, bb_level),
1224 offsetof(struct xfs_btree_block, bb_numrecs),
1225 offsetof(struct xfs_btree_block, bb_u.l.bb_leftsib),
1226 offsetof(struct xfs_btree_block, bb_u.l.bb_rightsib),
1227 XFS_BTREE_LBLOCK_LEN
1230 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1231 XFS_BTREE_TRACE_ARGBI(cur, bp, fields);
1233 if (bp) {
1234 xfs_btree_offsets(fields,
1235 (cur->bc_flags & XFS_BTREE_LONG_PTRS) ?
1236 loffsets : soffsets,
1237 XFS_BB_NUM_BITS, &first, &last);
1238 xfs_trans_log_buf(cur->bc_tp, bp, first, last);
1239 } else {
1240 xfs_trans_log_inode(cur->bc_tp, cur->bc_private.b.ip,
1241 xfs_ilog_fbroot(cur->bc_private.b.whichfork));
1244 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1248 * Increment cursor by one record at the level.
1249 * For nonzero levels the leaf-ward information is untouched.
1251 int /* error */
1252 xfs_btree_increment(
1253 struct xfs_btree_cur *cur,
1254 int level,
1255 int *stat) /* success/failure */
1257 struct xfs_btree_block *block;
1258 union xfs_btree_ptr ptr;
1259 struct xfs_buf *bp;
1260 int error; /* error return value */
1261 int lev;
1263 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1264 XFS_BTREE_TRACE_ARGI(cur, level);
1266 ASSERT(level < cur->bc_nlevels);
1268 /* Read-ahead to the right at this level. */
1269 xfs_btree_readahead(cur, level, XFS_BTCUR_RIGHTRA);
1271 /* Get a pointer to the btree block. */
1272 block = xfs_btree_get_block(cur, level, &bp);
1274 #ifdef DEBUG
1275 error = xfs_btree_check_block(cur, block, level, bp);
1276 if (error)
1277 goto error0;
1278 #endif
1280 /* We're done if we remain in the block after the increment. */
1281 if (++cur->bc_ptrs[level] <= xfs_btree_get_numrecs(block))
1282 goto out1;
1284 /* Fail if we just went off the right edge of the tree. */
1285 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_RIGHTSIB);
1286 if (xfs_btree_ptr_is_null(cur, &ptr))
1287 goto out0;
1289 XFS_BTREE_STATS_INC(cur, increment);
1292 * March up the tree incrementing pointers.
1293 * Stop when we don't go off the right edge of a block.
1295 for (lev = level + 1; lev < cur->bc_nlevels; lev++) {
1296 block = xfs_btree_get_block(cur, lev, &bp);
1298 #ifdef DEBUG
1299 error = xfs_btree_check_block(cur, block, lev, bp);
1300 if (error)
1301 goto error0;
1302 #endif
1304 if (++cur->bc_ptrs[lev] <= xfs_btree_get_numrecs(block))
1305 break;
1307 /* Read-ahead the right block for the next loop. */
1308 xfs_btree_readahead(cur, lev, XFS_BTCUR_RIGHTRA);
1312 * If we went off the root then we are either seriously
1313 * confused or have the tree root in an inode.
1315 if (lev == cur->bc_nlevels) {
1316 if (cur->bc_flags & XFS_BTREE_ROOT_IN_INODE)
1317 goto out0;
1318 ASSERT(0);
1319 error = EFSCORRUPTED;
1320 goto error0;
1322 ASSERT(lev < cur->bc_nlevels);
1325 * Now walk back down the tree, fixing up the cursor's buffer
1326 * pointers and key numbers.
1328 for (block = xfs_btree_get_block(cur, lev, &bp); lev > level; ) {
1329 union xfs_btree_ptr *ptrp;
1331 ptrp = xfs_btree_ptr_addr(cur, cur->bc_ptrs[lev], block);
1332 error = xfs_btree_read_buf_block(cur, ptrp, --lev,
1333 0, &block, &bp);
1334 if (error)
1335 goto error0;
1337 xfs_btree_setbuf(cur, lev, bp);
1338 cur->bc_ptrs[lev] = 1;
1340 out1:
1341 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1342 *stat = 1;
1343 return 0;
1345 out0:
1346 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1347 *stat = 0;
1348 return 0;
1350 error0:
1351 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
1352 return error;
1356 * Decrement cursor by one record at the level.
1357 * For nonzero levels the leaf-ward information is untouched.
1359 int /* error */
1360 xfs_btree_decrement(
1361 struct xfs_btree_cur *cur,
1362 int level,
1363 int *stat) /* success/failure */
1365 struct xfs_btree_block *block;
1366 xfs_buf_t *bp;
1367 int error; /* error return value */
1368 int lev;
1369 union xfs_btree_ptr ptr;
1371 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1372 XFS_BTREE_TRACE_ARGI(cur, level);
1374 ASSERT(level < cur->bc_nlevels);
1376 /* Read-ahead to the left at this level. */
1377 xfs_btree_readahead(cur, level, XFS_BTCUR_LEFTRA);
1379 /* We're done if we remain in the block after the decrement. */
1380 if (--cur->bc_ptrs[level] > 0)
1381 goto out1;
1383 /* Get a pointer to the btree block. */
1384 block = xfs_btree_get_block(cur, level, &bp);
1386 #ifdef DEBUG
1387 error = xfs_btree_check_block(cur, block, level, bp);
1388 if (error)
1389 goto error0;
1390 #endif
1392 /* Fail if we just went off the left edge of the tree. */
1393 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_LEFTSIB);
1394 if (xfs_btree_ptr_is_null(cur, &ptr))
1395 goto out0;
1397 XFS_BTREE_STATS_INC(cur, decrement);
1400 * March up the tree decrementing pointers.
1401 * Stop when we don't go off the left edge of a block.
1403 for (lev = level + 1; lev < cur->bc_nlevels; lev++) {
1404 if (--cur->bc_ptrs[lev] > 0)
1405 break;
1406 /* Read-ahead the left block for the next loop. */
1407 xfs_btree_readahead(cur, lev, XFS_BTCUR_LEFTRA);
1411 * If we went off the root then we are seriously confused.
1412 * or the root of the tree is in an inode.
1414 if (lev == cur->bc_nlevels) {
1415 if (cur->bc_flags & XFS_BTREE_ROOT_IN_INODE)
1416 goto out0;
1417 ASSERT(0);
1418 error = EFSCORRUPTED;
1419 goto error0;
1421 ASSERT(lev < cur->bc_nlevels);
1424 * Now walk back down the tree, fixing up the cursor's buffer
1425 * pointers and key numbers.
1427 for (block = xfs_btree_get_block(cur, lev, &bp); lev > level; ) {
1428 union xfs_btree_ptr *ptrp;
1430 ptrp = xfs_btree_ptr_addr(cur, cur->bc_ptrs[lev], block);
1431 error = xfs_btree_read_buf_block(cur, ptrp, --lev,
1432 0, &block, &bp);
1433 if (error)
1434 goto error0;
1435 xfs_btree_setbuf(cur, lev, bp);
1436 cur->bc_ptrs[lev] = xfs_btree_get_numrecs(block);
1438 out1:
1439 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1440 *stat = 1;
1441 return 0;
1443 out0:
1444 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1445 *stat = 0;
1446 return 0;
1448 error0:
1449 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
1450 return error;
1453 STATIC int
1454 xfs_btree_lookup_get_block(
1455 struct xfs_btree_cur *cur, /* btree cursor */
1456 int level, /* level in the btree */
1457 union xfs_btree_ptr *pp, /* ptr to btree block */
1458 struct xfs_btree_block **blkp) /* return btree block */
1460 struct xfs_buf *bp; /* buffer pointer for btree block */
1461 int error = 0;
1463 /* special case the root block if in an inode */
1464 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
1465 (level == cur->bc_nlevels - 1)) {
1466 *blkp = xfs_btree_get_iroot(cur);
1467 return 0;
1471 * If the old buffer at this level for the disk address we are
1472 * looking for re-use it.
1474 * Otherwise throw it away and get a new one.
1476 bp = cur->bc_bufs[level];
1477 if (bp && XFS_BUF_ADDR(bp) == xfs_btree_ptr_to_daddr(cur, pp)) {
1478 *blkp = XFS_BUF_TO_BLOCK(bp);
1479 return 0;
1482 error = xfs_btree_read_buf_block(cur, pp, level, 0, blkp, &bp);
1483 if (error)
1484 return error;
1486 xfs_btree_setbuf(cur, level, bp);
1487 return 0;
1491 * Get current search key. For level 0 we don't actually have a key
1492 * structure so we make one up from the record. For all other levels
1493 * we just return the right key.
1495 STATIC union xfs_btree_key *
1496 xfs_lookup_get_search_key(
1497 struct xfs_btree_cur *cur,
1498 int level,
1499 int keyno,
1500 struct xfs_btree_block *block,
1501 union xfs_btree_key *kp)
1503 if (level == 0) {
1504 cur->bc_ops->init_key_from_rec(kp,
1505 xfs_btree_rec_addr(cur, keyno, block));
1506 return kp;
1509 return xfs_btree_key_addr(cur, keyno, block);
1513 * Lookup the record. The cursor is made to point to it, based on dir.
1514 * Return 0 if can't find any such record, 1 for success.
1516 int /* error */
1517 xfs_btree_lookup(
1518 struct xfs_btree_cur *cur, /* btree cursor */
1519 xfs_lookup_t dir, /* <=, ==, or >= */
1520 int *stat) /* success/failure */
1522 struct xfs_btree_block *block; /* current btree block */
1523 __int64_t diff; /* difference for the current key */
1524 int error; /* error return value */
1525 int keyno; /* current key number */
1526 int level; /* level in the btree */
1527 union xfs_btree_ptr *pp; /* ptr to btree block */
1528 union xfs_btree_ptr ptr; /* ptr to btree block */
1530 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1531 XFS_BTREE_TRACE_ARGI(cur, dir);
1533 XFS_BTREE_STATS_INC(cur, lookup);
1535 block = NULL;
1536 keyno = 0;
1538 /* initialise start pointer from cursor */
1539 cur->bc_ops->init_ptr_from_cur(cur, &ptr);
1540 pp = &ptr;
1543 * Iterate over each level in the btree, starting at the root.
1544 * For each level above the leaves, find the key we need, based
1545 * on the lookup record, then follow the corresponding block
1546 * pointer down to the next level.
1548 for (level = cur->bc_nlevels - 1, diff = 1; level >= 0; level--) {
1549 /* Get the block we need to do the lookup on. */
1550 error = xfs_btree_lookup_get_block(cur, level, pp, &block);
1551 if (error)
1552 goto error0;
1554 if (diff == 0) {
1556 * If we already had a key match at a higher level, we
1557 * know we need to use the first entry in this block.
1559 keyno = 1;
1560 } else {
1561 /* Otherwise search this block. Do a binary search. */
1563 int high; /* high entry number */
1564 int low; /* low entry number */
1566 /* Set low and high entry numbers, 1-based. */
1567 low = 1;
1568 high = xfs_btree_get_numrecs(block);
1569 if (!high) {
1570 /* Block is empty, must be an empty leaf. */
1571 ASSERT(level == 0 && cur->bc_nlevels == 1);
1573 cur->bc_ptrs[0] = dir != XFS_LOOKUP_LE;
1574 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1575 *stat = 0;
1576 return 0;
1579 /* Binary search the block. */
1580 while (low <= high) {
1581 union xfs_btree_key key;
1582 union xfs_btree_key *kp;
1584 XFS_BTREE_STATS_INC(cur, compare);
1586 /* keyno is average of low and high. */
1587 keyno = (low + high) >> 1;
1589 /* Get current search key */
1590 kp = xfs_lookup_get_search_key(cur, level,
1591 keyno, block, &key);
1594 * Compute difference to get next direction:
1595 * - less than, move right
1596 * - greater than, move left
1597 * - equal, we're done
1599 diff = cur->bc_ops->key_diff(cur, kp);
1600 if (diff < 0)
1601 low = keyno + 1;
1602 else if (diff > 0)
1603 high = keyno - 1;
1604 else
1605 break;
1610 * If there are more levels, set up for the next level
1611 * by getting the block number and filling in the cursor.
1613 if (level > 0) {
1615 * If we moved left, need the previous key number,
1616 * unless there isn't one.
1618 if (diff > 0 && --keyno < 1)
1619 keyno = 1;
1620 pp = xfs_btree_ptr_addr(cur, keyno, block);
1622 #ifdef DEBUG
1623 error = xfs_btree_check_ptr(cur, pp, 0, level);
1624 if (error)
1625 goto error0;
1626 #endif
1627 cur->bc_ptrs[level] = keyno;
1631 /* Done with the search. See if we need to adjust the results. */
1632 if (dir != XFS_LOOKUP_LE && diff < 0) {
1633 keyno++;
1635 * If ge search and we went off the end of the block, but it's
1636 * not the last block, we're in the wrong block.
1638 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_RIGHTSIB);
1639 if (dir == XFS_LOOKUP_GE &&
1640 keyno > xfs_btree_get_numrecs(block) &&
1641 !xfs_btree_ptr_is_null(cur, &ptr)) {
1642 int i;
1644 cur->bc_ptrs[0] = keyno;
1645 error = xfs_btree_increment(cur, 0, &i);
1646 if (error)
1647 goto error0;
1648 XFS_WANT_CORRUPTED_RETURN(i == 1);
1649 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1650 *stat = 1;
1651 return 0;
1653 } else if (dir == XFS_LOOKUP_LE && diff > 0)
1654 keyno--;
1655 cur->bc_ptrs[0] = keyno;
1657 /* Return if we succeeded or not. */
1658 if (keyno == 0 || keyno > xfs_btree_get_numrecs(block))
1659 *stat = 0;
1660 else if (dir != XFS_LOOKUP_EQ || diff == 0)
1661 *stat = 1;
1662 else
1663 *stat = 0;
1664 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1665 return 0;
1667 error0:
1668 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
1669 return error;
1673 * Update keys at all levels from here to the root along the cursor's path.
1675 STATIC int
1676 xfs_btree_updkey(
1677 struct xfs_btree_cur *cur,
1678 union xfs_btree_key *keyp,
1679 int level)
1681 struct xfs_btree_block *block;
1682 struct xfs_buf *bp;
1683 union xfs_btree_key *kp;
1684 int ptr;
1686 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1687 XFS_BTREE_TRACE_ARGIK(cur, level, keyp);
1689 ASSERT(!(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) || level >= 1);
1692 * Go up the tree from this level toward the root.
1693 * At each level, update the key value to the value input.
1694 * Stop when we reach a level where the cursor isn't pointing
1695 * at the first entry in the block.
1697 for (ptr = 1; ptr == 1 && level < cur->bc_nlevels; level++) {
1698 #ifdef DEBUG
1699 int error;
1700 #endif
1701 block = xfs_btree_get_block(cur, level, &bp);
1702 #ifdef DEBUG
1703 error = xfs_btree_check_block(cur, block, level, bp);
1704 if (error) {
1705 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
1706 return error;
1708 #endif
1709 ptr = cur->bc_ptrs[level];
1710 kp = xfs_btree_key_addr(cur, ptr, block);
1711 xfs_btree_copy_keys(cur, kp, keyp, 1);
1712 xfs_btree_log_keys(cur, bp, ptr, ptr);
1715 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1716 return 0;
1720 * Update the record referred to by cur to the value in the
1721 * given record. This either works (return 0) or gets an
1722 * EFSCORRUPTED error.
1725 xfs_btree_update(
1726 struct xfs_btree_cur *cur,
1727 union xfs_btree_rec *rec)
1729 struct xfs_btree_block *block;
1730 struct xfs_buf *bp;
1731 int error;
1732 int ptr;
1733 union xfs_btree_rec *rp;
1735 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1736 XFS_BTREE_TRACE_ARGR(cur, rec);
1738 /* Pick up the current block. */
1739 block = xfs_btree_get_block(cur, 0, &bp);
1741 #ifdef DEBUG
1742 error = xfs_btree_check_block(cur, block, 0, bp);
1743 if (error)
1744 goto error0;
1745 #endif
1746 /* Get the address of the rec to be updated. */
1747 ptr = cur->bc_ptrs[0];
1748 rp = xfs_btree_rec_addr(cur, ptr, block);
1750 /* Fill in the new contents and log them. */
1751 xfs_btree_copy_recs(cur, rp, rec, 1);
1752 xfs_btree_log_recs(cur, bp, ptr, ptr);
1755 * If we are tracking the last record in the tree and
1756 * we are at the far right edge of the tree, update it.
1758 if (xfs_btree_is_lastrec(cur, block, 0)) {
1759 cur->bc_ops->update_lastrec(cur, block, rec,
1760 ptr, LASTREC_UPDATE);
1763 /* Updating first rec in leaf. Pass new key value up to our parent. */
1764 if (ptr == 1) {
1765 union xfs_btree_key key;
1767 cur->bc_ops->init_key_from_rec(&key, rec);
1768 error = xfs_btree_updkey(cur, &key, 1);
1769 if (error)
1770 goto error0;
1773 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1774 return 0;
1776 error0:
1777 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
1778 return error;
1782 * Move 1 record left from cur/level if possible.
1783 * Update cur to reflect the new path.
1785 STATIC int /* error */
1786 xfs_btree_lshift(
1787 struct xfs_btree_cur *cur,
1788 int level,
1789 int *stat) /* success/failure */
1791 union xfs_btree_key key; /* btree key */
1792 struct xfs_buf *lbp; /* left buffer pointer */
1793 struct xfs_btree_block *left; /* left btree block */
1794 int lrecs; /* left record count */
1795 struct xfs_buf *rbp; /* right buffer pointer */
1796 struct xfs_btree_block *right; /* right btree block */
1797 int rrecs; /* right record count */
1798 union xfs_btree_ptr lptr; /* left btree pointer */
1799 union xfs_btree_key *rkp = NULL; /* right btree key */
1800 union xfs_btree_ptr *rpp = NULL; /* right address pointer */
1801 union xfs_btree_rec *rrp = NULL; /* right record pointer */
1802 int error; /* error return value */
1804 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1805 XFS_BTREE_TRACE_ARGI(cur, level);
1807 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
1808 level == cur->bc_nlevels - 1)
1809 goto out0;
1811 /* Set up variables for this block as "right". */
1812 right = xfs_btree_get_block(cur, level, &rbp);
1814 #ifdef DEBUG
1815 error = xfs_btree_check_block(cur, right, level, rbp);
1816 if (error)
1817 goto error0;
1818 #endif
1820 /* If we've got no left sibling then we can't shift an entry left. */
1821 xfs_btree_get_sibling(cur, right, &lptr, XFS_BB_LEFTSIB);
1822 if (xfs_btree_ptr_is_null(cur, &lptr))
1823 goto out0;
1826 * If the cursor entry is the one that would be moved, don't
1827 * do it... it's too complicated.
1829 if (cur->bc_ptrs[level] <= 1)
1830 goto out0;
1832 /* Set up the left neighbor as "left". */
1833 error = xfs_btree_read_buf_block(cur, &lptr, level, 0, &left, &lbp);
1834 if (error)
1835 goto error0;
1837 /* If it's full, it can't take another entry. */
1838 lrecs = xfs_btree_get_numrecs(left);
1839 if (lrecs == cur->bc_ops->get_maxrecs(cur, level))
1840 goto out0;
1842 rrecs = xfs_btree_get_numrecs(right);
1845 * We add one entry to the left side and remove one for the right side.
1846 * Account for it here, the changes will be updated on disk and logged
1847 * later.
1849 lrecs++;
1850 rrecs--;
1852 XFS_BTREE_STATS_INC(cur, lshift);
1853 XFS_BTREE_STATS_ADD(cur, moves, 1);
1856 * If non-leaf, copy a key and a ptr to the left block.
1857 * Log the changes to the left block.
1859 if (level > 0) {
1860 /* It's a non-leaf. Move keys and pointers. */
1861 union xfs_btree_key *lkp; /* left btree key */
1862 union xfs_btree_ptr *lpp; /* left address pointer */
1864 lkp = xfs_btree_key_addr(cur, lrecs, left);
1865 rkp = xfs_btree_key_addr(cur, 1, right);
1867 lpp = xfs_btree_ptr_addr(cur, lrecs, left);
1868 rpp = xfs_btree_ptr_addr(cur, 1, right);
1869 #ifdef DEBUG
1870 error = xfs_btree_check_ptr(cur, rpp, 0, level);
1871 if (error)
1872 goto error0;
1873 #endif
1874 xfs_btree_copy_keys(cur, lkp, rkp, 1);
1875 xfs_btree_copy_ptrs(cur, lpp, rpp, 1);
1877 xfs_btree_log_keys(cur, lbp, lrecs, lrecs);
1878 xfs_btree_log_ptrs(cur, lbp, lrecs, lrecs);
1880 ASSERT(cur->bc_ops->keys_inorder(cur,
1881 xfs_btree_key_addr(cur, lrecs - 1, left), lkp));
1882 } else {
1883 /* It's a leaf. Move records. */
1884 union xfs_btree_rec *lrp; /* left record pointer */
1886 lrp = xfs_btree_rec_addr(cur, lrecs, left);
1887 rrp = xfs_btree_rec_addr(cur, 1, right);
1889 xfs_btree_copy_recs(cur, lrp, rrp, 1);
1890 xfs_btree_log_recs(cur, lbp, lrecs, lrecs);
1892 ASSERT(cur->bc_ops->recs_inorder(cur,
1893 xfs_btree_rec_addr(cur, lrecs - 1, left), lrp));
1896 xfs_btree_set_numrecs(left, lrecs);
1897 xfs_btree_log_block(cur, lbp, XFS_BB_NUMRECS);
1899 xfs_btree_set_numrecs(right, rrecs);
1900 xfs_btree_log_block(cur, rbp, XFS_BB_NUMRECS);
1903 * Slide the contents of right down one entry.
1905 XFS_BTREE_STATS_ADD(cur, moves, rrecs - 1);
1906 if (level > 0) {
1907 /* It's a nonleaf. operate on keys and ptrs */
1908 #ifdef DEBUG
1909 int i; /* loop index */
1911 for (i = 0; i < rrecs; i++) {
1912 error = xfs_btree_check_ptr(cur, rpp, i + 1, level);
1913 if (error)
1914 goto error0;
1916 #endif
1917 xfs_btree_shift_keys(cur,
1918 xfs_btree_key_addr(cur, 2, right),
1919 -1, rrecs);
1920 xfs_btree_shift_ptrs(cur,
1921 xfs_btree_ptr_addr(cur, 2, right),
1922 -1, rrecs);
1924 xfs_btree_log_keys(cur, rbp, 1, rrecs);
1925 xfs_btree_log_ptrs(cur, rbp, 1, rrecs);
1926 } else {
1927 /* It's a leaf. operate on records */
1928 xfs_btree_shift_recs(cur,
1929 xfs_btree_rec_addr(cur, 2, right),
1930 -1, rrecs);
1931 xfs_btree_log_recs(cur, rbp, 1, rrecs);
1934 * If it's the first record in the block, we'll need a key
1935 * structure to pass up to the next level (updkey).
1937 cur->bc_ops->init_key_from_rec(&key,
1938 xfs_btree_rec_addr(cur, 1, right));
1939 rkp = &key;
1942 /* Update the parent key values of right. */
1943 error = xfs_btree_updkey(cur, rkp, level + 1);
1944 if (error)
1945 goto error0;
1947 /* Slide the cursor value left one. */
1948 cur->bc_ptrs[level]--;
1950 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1951 *stat = 1;
1952 return 0;
1954 out0:
1955 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1956 *stat = 0;
1957 return 0;
1959 error0:
1960 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
1961 return error;
1965 * Move 1 record right from cur/level if possible.
1966 * Update cur to reflect the new path.
1968 STATIC int /* error */
1969 xfs_btree_rshift(
1970 struct xfs_btree_cur *cur,
1971 int level,
1972 int *stat) /* success/failure */
1974 union xfs_btree_key key; /* btree key */
1975 struct xfs_buf *lbp; /* left buffer pointer */
1976 struct xfs_btree_block *left; /* left btree block */
1977 struct xfs_buf *rbp; /* right buffer pointer */
1978 struct xfs_btree_block *right; /* right btree block */
1979 struct xfs_btree_cur *tcur; /* temporary btree cursor */
1980 union xfs_btree_ptr rptr; /* right block pointer */
1981 union xfs_btree_key *rkp; /* right btree key */
1982 int rrecs; /* right record count */
1983 int lrecs; /* left record count */
1984 int error; /* error return value */
1985 int i; /* loop counter */
1987 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1988 XFS_BTREE_TRACE_ARGI(cur, level);
1990 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
1991 (level == cur->bc_nlevels - 1))
1992 goto out0;
1994 /* Set up variables for this block as "left". */
1995 left = xfs_btree_get_block(cur, level, &lbp);
1997 #ifdef DEBUG
1998 error = xfs_btree_check_block(cur, left, level, lbp);
1999 if (error)
2000 goto error0;
2001 #endif
2003 /* If we've got no right sibling then we can't shift an entry right. */
2004 xfs_btree_get_sibling(cur, left, &rptr, XFS_BB_RIGHTSIB);
2005 if (xfs_btree_ptr_is_null(cur, &rptr))
2006 goto out0;
2009 * If the cursor entry is the one that would be moved, don't
2010 * do it... it's too complicated.
2012 lrecs = xfs_btree_get_numrecs(left);
2013 if (cur->bc_ptrs[level] >= lrecs)
2014 goto out0;
2016 /* Set up the right neighbor as "right". */
2017 error = xfs_btree_read_buf_block(cur, &rptr, level, 0, &right, &rbp);
2018 if (error)
2019 goto error0;
2021 /* If it's full, it can't take another entry. */
2022 rrecs = xfs_btree_get_numrecs(right);
2023 if (rrecs == cur->bc_ops->get_maxrecs(cur, level))
2024 goto out0;
2026 XFS_BTREE_STATS_INC(cur, rshift);
2027 XFS_BTREE_STATS_ADD(cur, moves, rrecs);
2030 * Make a hole at the start of the right neighbor block, then
2031 * copy the last left block entry to the hole.
2033 if (level > 0) {
2034 /* It's a nonleaf. make a hole in the keys and ptrs */
2035 union xfs_btree_key *lkp;
2036 union xfs_btree_ptr *lpp;
2037 union xfs_btree_ptr *rpp;
2039 lkp = xfs_btree_key_addr(cur, lrecs, left);
2040 lpp = xfs_btree_ptr_addr(cur, lrecs, left);
2041 rkp = xfs_btree_key_addr(cur, 1, right);
2042 rpp = xfs_btree_ptr_addr(cur, 1, right);
2044 #ifdef DEBUG
2045 for (i = rrecs - 1; i >= 0; i--) {
2046 error = xfs_btree_check_ptr(cur, rpp, i, level);
2047 if (error)
2048 goto error0;
2050 #endif
2052 xfs_btree_shift_keys(cur, rkp, 1, rrecs);
2053 xfs_btree_shift_ptrs(cur, rpp, 1, rrecs);
2055 #ifdef DEBUG
2056 error = xfs_btree_check_ptr(cur, lpp, 0, level);
2057 if (error)
2058 goto error0;
2059 #endif
2061 /* Now put the new data in, and log it. */
2062 xfs_btree_copy_keys(cur, rkp, lkp, 1);
2063 xfs_btree_copy_ptrs(cur, rpp, lpp, 1);
2065 xfs_btree_log_keys(cur, rbp, 1, rrecs + 1);
2066 xfs_btree_log_ptrs(cur, rbp, 1, rrecs + 1);
2068 ASSERT(cur->bc_ops->keys_inorder(cur, rkp,
2069 xfs_btree_key_addr(cur, 2, right)));
2070 } else {
2071 /* It's a leaf. make a hole in the records */
2072 union xfs_btree_rec *lrp;
2073 union xfs_btree_rec *rrp;
2075 lrp = xfs_btree_rec_addr(cur, lrecs, left);
2076 rrp = xfs_btree_rec_addr(cur, 1, right);
2078 xfs_btree_shift_recs(cur, rrp, 1, rrecs);
2080 /* Now put the new data in, and log it. */
2081 xfs_btree_copy_recs(cur, rrp, lrp, 1);
2082 xfs_btree_log_recs(cur, rbp, 1, rrecs + 1);
2084 cur->bc_ops->init_key_from_rec(&key, rrp);
2085 rkp = &key;
2087 ASSERT(cur->bc_ops->recs_inorder(cur, rrp,
2088 xfs_btree_rec_addr(cur, 2, right)));
2092 * Decrement and log left's numrecs, bump and log right's numrecs.
2094 xfs_btree_set_numrecs(left, --lrecs);
2095 xfs_btree_log_block(cur, lbp, XFS_BB_NUMRECS);
2097 xfs_btree_set_numrecs(right, ++rrecs);
2098 xfs_btree_log_block(cur, rbp, XFS_BB_NUMRECS);
2101 * Using a temporary cursor, update the parent key values of the
2102 * block on the right.
2104 error = xfs_btree_dup_cursor(cur, &tcur);
2105 if (error)
2106 goto error0;
2107 i = xfs_btree_lastrec(tcur, level);
2108 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
2110 error = xfs_btree_increment(tcur, level, &i);
2111 if (error)
2112 goto error1;
2114 error = xfs_btree_updkey(tcur, rkp, level + 1);
2115 if (error)
2116 goto error1;
2118 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
2120 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2121 *stat = 1;
2122 return 0;
2124 out0:
2125 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2126 *stat = 0;
2127 return 0;
2129 error0:
2130 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
2131 return error;
2133 error1:
2134 XFS_BTREE_TRACE_CURSOR(tcur, XBT_ERROR);
2135 xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
2136 return error;
2140 * Split cur/level block in half.
2141 * Return new block number and the key to its first
2142 * record (to be inserted into parent).
2144 STATIC int /* error */
2145 xfs_btree_split(
2146 struct xfs_btree_cur *cur,
2147 int level,
2148 union xfs_btree_ptr *ptrp,
2149 union xfs_btree_key *key,
2150 struct xfs_btree_cur **curp,
2151 int *stat) /* success/failure */
2153 union xfs_btree_ptr lptr; /* left sibling block ptr */
2154 struct xfs_buf *lbp; /* left buffer pointer */
2155 struct xfs_btree_block *left; /* left btree block */
2156 union xfs_btree_ptr rptr; /* right sibling block ptr */
2157 struct xfs_buf *rbp; /* right buffer pointer */
2158 struct xfs_btree_block *right; /* right btree block */
2159 union xfs_btree_ptr rrptr; /* right-right sibling ptr */
2160 struct xfs_buf *rrbp; /* right-right buffer pointer */
2161 struct xfs_btree_block *rrblock; /* right-right btree block */
2162 int lrecs;
2163 int rrecs;
2164 int src_index;
2165 int error; /* error return value */
2166 #ifdef DEBUG
2167 int i;
2168 #endif
2170 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
2171 XFS_BTREE_TRACE_ARGIPK(cur, level, *ptrp, key);
2173 XFS_BTREE_STATS_INC(cur, split);
2175 /* Set up left block (current one). */
2176 left = xfs_btree_get_block(cur, level, &lbp);
2178 #ifdef DEBUG
2179 error = xfs_btree_check_block(cur, left, level, lbp);
2180 if (error)
2181 goto error0;
2182 #endif
2184 xfs_btree_buf_to_ptr(cur, lbp, &lptr);
2186 /* Allocate the new block. If we can't do it, we're toast. Give up. */
2187 error = cur->bc_ops->alloc_block(cur, &lptr, &rptr, 1, stat);
2188 if (error)
2189 goto error0;
2190 if (*stat == 0)
2191 goto out0;
2192 XFS_BTREE_STATS_INC(cur, alloc);
2194 /* Set up the new block as "right". */
2195 error = xfs_btree_get_buf_block(cur, &rptr, 0, &right, &rbp);
2196 if (error)
2197 goto error0;
2199 /* Fill in the btree header for the new right block. */
2200 xfs_btree_init_block(cur, xfs_btree_get_level(left), 0, right);
2203 * Split the entries between the old and the new block evenly.
2204 * Make sure that if there's an odd number of entries now, that
2205 * each new block will have the same number of entries.
2207 lrecs = xfs_btree_get_numrecs(left);
2208 rrecs = lrecs / 2;
2209 if ((lrecs & 1) && cur->bc_ptrs[level] <= rrecs + 1)
2210 rrecs++;
2211 src_index = (lrecs - rrecs + 1);
2213 XFS_BTREE_STATS_ADD(cur, moves, rrecs);
2216 * Copy btree block entries from the left block over to the
2217 * new block, the right. Update the right block and log the
2218 * changes.
2220 if (level > 0) {
2221 /* It's a non-leaf. Move keys and pointers. */
2222 union xfs_btree_key *lkp; /* left btree key */
2223 union xfs_btree_ptr *lpp; /* left address pointer */
2224 union xfs_btree_key *rkp; /* right btree key */
2225 union xfs_btree_ptr *rpp; /* right address pointer */
2227 lkp = xfs_btree_key_addr(cur, src_index, left);
2228 lpp = xfs_btree_ptr_addr(cur, src_index, left);
2229 rkp = xfs_btree_key_addr(cur, 1, right);
2230 rpp = xfs_btree_ptr_addr(cur, 1, right);
2232 #ifdef DEBUG
2233 for (i = src_index; i < rrecs; i++) {
2234 error = xfs_btree_check_ptr(cur, lpp, i, level);
2235 if (error)
2236 goto error0;
2238 #endif
2240 xfs_btree_copy_keys(cur, rkp, lkp, rrecs);
2241 xfs_btree_copy_ptrs(cur, rpp, lpp, rrecs);
2243 xfs_btree_log_keys(cur, rbp, 1, rrecs);
2244 xfs_btree_log_ptrs(cur, rbp, 1, rrecs);
2246 /* Grab the keys to the entries moved to the right block */
2247 xfs_btree_copy_keys(cur, key, rkp, 1);
2248 } else {
2249 /* It's a leaf. Move records. */
2250 union xfs_btree_rec *lrp; /* left record pointer */
2251 union xfs_btree_rec *rrp; /* right record pointer */
2253 lrp = xfs_btree_rec_addr(cur, src_index, left);
2254 rrp = xfs_btree_rec_addr(cur, 1, right);
2256 xfs_btree_copy_recs(cur, rrp, lrp, rrecs);
2257 xfs_btree_log_recs(cur, rbp, 1, rrecs);
2259 cur->bc_ops->init_key_from_rec(key,
2260 xfs_btree_rec_addr(cur, 1, right));
2265 * Find the left block number by looking in the buffer.
2266 * Adjust numrecs, sibling pointers.
2268 xfs_btree_get_sibling(cur, left, &rrptr, XFS_BB_RIGHTSIB);
2269 xfs_btree_set_sibling(cur, right, &rrptr, XFS_BB_RIGHTSIB);
2270 xfs_btree_set_sibling(cur, right, &lptr, XFS_BB_LEFTSIB);
2271 xfs_btree_set_sibling(cur, left, &rptr, XFS_BB_RIGHTSIB);
2273 lrecs -= rrecs;
2274 xfs_btree_set_numrecs(left, lrecs);
2275 xfs_btree_set_numrecs(right, xfs_btree_get_numrecs(right) + rrecs);
2277 xfs_btree_log_block(cur, rbp, XFS_BB_ALL_BITS);
2278 xfs_btree_log_block(cur, lbp, XFS_BB_NUMRECS | XFS_BB_RIGHTSIB);
2281 * If there's a block to the new block's right, make that block
2282 * point back to right instead of to left.
2284 if (!xfs_btree_ptr_is_null(cur, &rrptr)) {
2285 error = xfs_btree_read_buf_block(cur, &rrptr, level,
2286 0, &rrblock, &rrbp);
2287 if (error)
2288 goto error0;
2289 xfs_btree_set_sibling(cur, rrblock, &rptr, XFS_BB_LEFTSIB);
2290 xfs_btree_log_block(cur, rrbp, XFS_BB_LEFTSIB);
2293 * If the cursor is really in the right block, move it there.
2294 * If it's just pointing past the last entry in left, then we'll
2295 * insert there, so don't change anything in that case.
2297 if (cur->bc_ptrs[level] > lrecs + 1) {
2298 xfs_btree_setbuf(cur, level, rbp);
2299 cur->bc_ptrs[level] -= lrecs;
2302 * If there are more levels, we'll need another cursor which refers
2303 * the right block, no matter where this cursor was.
2305 if (level + 1 < cur->bc_nlevels) {
2306 error = xfs_btree_dup_cursor(cur, curp);
2307 if (error)
2308 goto error0;
2309 (*curp)->bc_ptrs[level + 1]++;
2311 *ptrp = rptr;
2312 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2313 *stat = 1;
2314 return 0;
2315 out0:
2316 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2317 *stat = 0;
2318 return 0;
2320 error0:
2321 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
2322 return error;
2326 * Copy the old inode root contents into a real block and make the
2327 * broot point to it.
2329 int /* error */
2330 xfs_btree_new_iroot(
2331 struct xfs_btree_cur *cur, /* btree cursor */
2332 int *logflags, /* logging flags for inode */
2333 int *stat) /* return status - 0 fail */
2335 struct xfs_buf *cbp; /* buffer for cblock */
2336 struct xfs_btree_block *block; /* btree block */
2337 struct xfs_btree_block *cblock; /* child btree block */
2338 union xfs_btree_key *ckp; /* child key pointer */
2339 union xfs_btree_ptr *cpp; /* child ptr pointer */
2340 union xfs_btree_key *kp; /* pointer to btree key */
2341 union xfs_btree_ptr *pp; /* pointer to block addr */
2342 union xfs_btree_ptr nptr; /* new block addr */
2343 int level; /* btree level */
2344 int error; /* error return code */
2345 #ifdef DEBUG
2346 int i; /* loop counter */
2347 #endif
2349 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
2350 XFS_BTREE_STATS_INC(cur, newroot);
2352 ASSERT(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE);
2354 level = cur->bc_nlevels - 1;
2356 block = xfs_btree_get_iroot(cur);
2357 pp = xfs_btree_ptr_addr(cur, 1, block);
2359 /* Allocate the new block. If we can't do it, we're toast. Give up. */
2360 error = cur->bc_ops->alloc_block(cur, pp, &nptr, 1, stat);
2361 if (error)
2362 goto error0;
2363 if (*stat == 0) {
2364 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2365 return 0;
2367 XFS_BTREE_STATS_INC(cur, alloc);
2369 /* Copy the root into a real block. */
2370 error = xfs_btree_get_buf_block(cur, &nptr, 0, &cblock, &cbp);
2371 if (error)
2372 goto error0;
2374 memcpy(cblock, block, xfs_btree_block_len(cur));
2376 be16_add_cpu(&block->bb_level, 1);
2377 xfs_btree_set_numrecs(block, 1);
2378 cur->bc_nlevels++;
2379 cur->bc_ptrs[level + 1] = 1;
2381 kp = xfs_btree_key_addr(cur, 1, block);
2382 ckp = xfs_btree_key_addr(cur, 1, cblock);
2383 xfs_btree_copy_keys(cur, ckp, kp, xfs_btree_get_numrecs(cblock));
2385 cpp = xfs_btree_ptr_addr(cur, 1, cblock);
2386 #ifdef DEBUG
2387 for (i = 0; i < be16_to_cpu(cblock->bb_numrecs); i++) {
2388 error = xfs_btree_check_ptr(cur, pp, i, level);
2389 if (error)
2390 goto error0;
2392 #endif
2393 xfs_btree_copy_ptrs(cur, cpp, pp, xfs_btree_get_numrecs(cblock));
2395 #ifdef DEBUG
2396 error = xfs_btree_check_ptr(cur, &nptr, 0, level);
2397 if (error)
2398 goto error0;
2399 #endif
2400 xfs_btree_copy_ptrs(cur, pp, &nptr, 1);
2402 xfs_iroot_realloc(cur->bc_private.b.ip,
2403 1 - xfs_btree_get_numrecs(cblock),
2404 cur->bc_private.b.whichfork);
2406 xfs_btree_setbuf(cur, level, cbp);
2409 * Do all this logging at the end so that
2410 * the root is at the right level.
2412 xfs_btree_log_block(cur, cbp, XFS_BB_ALL_BITS);
2413 xfs_btree_log_keys(cur, cbp, 1, be16_to_cpu(cblock->bb_numrecs));
2414 xfs_btree_log_ptrs(cur, cbp, 1, be16_to_cpu(cblock->bb_numrecs));
2416 *logflags |=
2417 XFS_ILOG_CORE | xfs_ilog_fbroot(cur->bc_private.b.whichfork);
2418 *stat = 1;
2419 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2420 return 0;
2421 error0:
2422 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
2423 return error;
2427 * Allocate a new root block, fill it in.
2429 STATIC int /* error */
2430 xfs_btree_new_root(
2431 struct xfs_btree_cur *cur, /* btree cursor */
2432 int *stat) /* success/failure */
2434 struct xfs_btree_block *block; /* one half of the old root block */
2435 struct xfs_buf *bp; /* buffer containing block */
2436 int error; /* error return value */
2437 struct xfs_buf *lbp; /* left buffer pointer */
2438 struct xfs_btree_block *left; /* left btree block */
2439 struct xfs_buf *nbp; /* new (root) buffer */
2440 struct xfs_btree_block *new; /* new (root) btree block */
2441 int nptr; /* new value for key index, 1 or 2 */
2442 struct xfs_buf *rbp; /* right buffer pointer */
2443 struct xfs_btree_block *right; /* right btree block */
2444 union xfs_btree_ptr rptr;
2445 union xfs_btree_ptr lptr;
2447 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
2448 XFS_BTREE_STATS_INC(cur, newroot);
2450 /* initialise our start point from the cursor */
2451 cur->bc_ops->init_ptr_from_cur(cur, &rptr);
2453 /* Allocate the new block. If we can't do it, we're toast. Give up. */
2454 error = cur->bc_ops->alloc_block(cur, &rptr, &lptr, 1, stat);
2455 if (error)
2456 goto error0;
2457 if (*stat == 0)
2458 goto out0;
2459 XFS_BTREE_STATS_INC(cur, alloc);
2461 /* Set up the new block. */
2462 error = xfs_btree_get_buf_block(cur, &lptr, 0, &new, &nbp);
2463 if (error)
2464 goto error0;
2466 /* Set the root in the holding structure increasing the level by 1. */
2467 cur->bc_ops->set_root(cur, &lptr, 1);
2470 * At the previous root level there are now two blocks: the old root,
2471 * and the new block generated when it was split. We don't know which
2472 * one the cursor is pointing at, so we set up variables "left" and
2473 * "right" for each case.
2475 block = xfs_btree_get_block(cur, cur->bc_nlevels - 1, &bp);
2477 #ifdef DEBUG
2478 error = xfs_btree_check_block(cur, block, cur->bc_nlevels - 1, bp);
2479 if (error)
2480 goto error0;
2481 #endif
2483 xfs_btree_get_sibling(cur, block, &rptr, XFS_BB_RIGHTSIB);
2484 if (!xfs_btree_ptr_is_null(cur, &rptr)) {
2485 /* Our block is left, pick up the right block. */
2486 lbp = bp;
2487 xfs_btree_buf_to_ptr(cur, lbp, &lptr);
2488 left = block;
2489 error = xfs_btree_read_buf_block(cur, &rptr,
2490 cur->bc_nlevels - 1, 0, &right, &rbp);
2491 if (error)
2492 goto error0;
2493 bp = rbp;
2494 nptr = 1;
2495 } else {
2496 /* Our block is right, pick up the left block. */
2497 rbp = bp;
2498 xfs_btree_buf_to_ptr(cur, rbp, &rptr);
2499 right = block;
2500 xfs_btree_get_sibling(cur, right, &lptr, XFS_BB_LEFTSIB);
2501 error = xfs_btree_read_buf_block(cur, &lptr,
2502 cur->bc_nlevels - 1, 0, &left, &lbp);
2503 if (error)
2504 goto error0;
2505 bp = lbp;
2506 nptr = 2;
2508 /* Fill in the new block's btree header and log it. */
2509 xfs_btree_init_block(cur, cur->bc_nlevels, 2, new);
2510 xfs_btree_log_block(cur, nbp, XFS_BB_ALL_BITS);
2511 ASSERT(!xfs_btree_ptr_is_null(cur, &lptr) &&
2512 !xfs_btree_ptr_is_null(cur, &rptr));
2514 /* Fill in the key data in the new root. */
2515 if (xfs_btree_get_level(left) > 0) {
2516 xfs_btree_copy_keys(cur,
2517 xfs_btree_key_addr(cur, 1, new),
2518 xfs_btree_key_addr(cur, 1, left), 1);
2519 xfs_btree_copy_keys(cur,
2520 xfs_btree_key_addr(cur, 2, new),
2521 xfs_btree_key_addr(cur, 1, right), 1);
2522 } else {
2523 cur->bc_ops->init_key_from_rec(
2524 xfs_btree_key_addr(cur, 1, new),
2525 xfs_btree_rec_addr(cur, 1, left));
2526 cur->bc_ops->init_key_from_rec(
2527 xfs_btree_key_addr(cur, 2, new),
2528 xfs_btree_rec_addr(cur, 1, right));
2530 xfs_btree_log_keys(cur, nbp, 1, 2);
2532 /* Fill in the pointer data in the new root. */
2533 xfs_btree_copy_ptrs(cur,
2534 xfs_btree_ptr_addr(cur, 1, new), &lptr, 1);
2535 xfs_btree_copy_ptrs(cur,
2536 xfs_btree_ptr_addr(cur, 2, new), &rptr, 1);
2537 xfs_btree_log_ptrs(cur, nbp, 1, 2);
2539 /* Fix up the cursor. */
2540 xfs_btree_setbuf(cur, cur->bc_nlevels, nbp);
2541 cur->bc_ptrs[cur->bc_nlevels] = nptr;
2542 cur->bc_nlevels++;
2543 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2544 *stat = 1;
2545 return 0;
2546 error0:
2547 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
2548 return error;
2549 out0:
2550 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2551 *stat = 0;
2552 return 0;
2555 STATIC int
2556 xfs_btree_make_block_unfull(
2557 struct xfs_btree_cur *cur, /* btree cursor */
2558 int level, /* btree level */
2559 int numrecs,/* # of recs in block */
2560 int *oindex,/* old tree index */
2561 int *index, /* new tree index */
2562 union xfs_btree_ptr *nptr, /* new btree ptr */
2563 struct xfs_btree_cur **ncur, /* new btree cursor */
2564 union xfs_btree_rec *nrec, /* new record */
2565 int *stat)
2567 union xfs_btree_key key; /* new btree key value */
2568 int error = 0;
2570 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
2571 level == cur->bc_nlevels - 1) {
2572 struct xfs_inode *ip = cur->bc_private.b.ip;
2574 if (numrecs < cur->bc_ops->get_dmaxrecs(cur, level)) {
2575 /* A root block that can be made bigger. */
2577 xfs_iroot_realloc(ip, 1, cur->bc_private.b.whichfork);
2578 } else {
2579 /* A root block that needs replacing */
2580 int logflags = 0;
2582 error = xfs_btree_new_iroot(cur, &logflags, stat);
2583 if (error || *stat == 0)
2584 return error;
2586 xfs_trans_log_inode(cur->bc_tp, ip, logflags);
2589 return 0;
2592 /* First, try shifting an entry to the right neighbor. */
2593 error = xfs_btree_rshift(cur, level, stat);
2594 if (error || *stat)
2595 return error;
2597 /* Next, try shifting an entry to the left neighbor. */
2598 error = xfs_btree_lshift(cur, level, stat);
2599 if (error)
2600 return error;
2602 if (*stat) {
2603 *oindex = *index = cur->bc_ptrs[level];
2604 return 0;
2608 * Next, try splitting the current block in half.
2610 * If this works we have to re-set our variables because we
2611 * could be in a different block now.
2613 error = xfs_btree_split(cur, level, nptr, &key, ncur, stat);
2614 if (error || *stat == 0)
2615 return error;
2618 *index = cur->bc_ptrs[level];
2619 cur->bc_ops->init_rec_from_key(&key, nrec);
2620 return 0;
2624 * Insert one record/level. Return information to the caller
2625 * allowing the next level up to proceed if necessary.
2627 STATIC int
2628 xfs_btree_insrec(
2629 struct xfs_btree_cur *cur, /* btree cursor */
2630 int level, /* level to insert record at */
2631 union xfs_btree_ptr *ptrp, /* i/o: block number inserted */
2632 union xfs_btree_rec *recp, /* i/o: record data inserted */
2633 struct xfs_btree_cur **curp, /* output: new cursor replacing cur */
2634 int *stat) /* success/failure */
2636 struct xfs_btree_block *block; /* btree block */
2637 struct xfs_buf *bp; /* buffer for block */
2638 union xfs_btree_key key; /* btree key */
2639 union xfs_btree_ptr nptr; /* new block ptr */
2640 struct xfs_btree_cur *ncur; /* new btree cursor */
2641 union xfs_btree_rec nrec; /* new record count */
2642 int optr; /* old key/record index */
2643 int ptr; /* key/record index */
2644 int numrecs;/* number of records */
2645 int error; /* error return value */
2646 #ifdef DEBUG
2647 int i;
2648 #endif
2650 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
2651 XFS_BTREE_TRACE_ARGIPR(cur, level, *ptrp, recp);
2653 ncur = NULL;
2656 * If we have an external root pointer, and we've made it to the
2657 * root level, allocate a new root block and we're done.
2659 if (!(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
2660 (level >= cur->bc_nlevels)) {
2661 error = xfs_btree_new_root(cur, stat);
2662 xfs_btree_set_ptr_null(cur, ptrp);
2664 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2665 return error;
2668 /* If we're off the left edge, return failure. */
2669 ptr = cur->bc_ptrs[level];
2670 if (ptr == 0) {
2671 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2672 *stat = 0;
2673 return 0;
2676 /* Make a key out of the record data to be inserted, and save it. */
2677 cur->bc_ops->init_key_from_rec(&key, recp);
2679 optr = ptr;
2681 XFS_BTREE_STATS_INC(cur, insrec);
2683 /* Get pointers to the btree buffer and block. */
2684 block = xfs_btree_get_block(cur, level, &bp);
2685 numrecs = xfs_btree_get_numrecs(block);
2687 #ifdef DEBUG
2688 error = xfs_btree_check_block(cur, block, level, bp);
2689 if (error)
2690 goto error0;
2692 /* Check that the new entry is being inserted in the right place. */
2693 if (ptr <= numrecs) {
2694 if (level == 0) {
2695 ASSERT(cur->bc_ops->recs_inorder(cur, recp,
2696 xfs_btree_rec_addr(cur, ptr, block)));
2697 } else {
2698 ASSERT(cur->bc_ops->keys_inorder(cur, &key,
2699 xfs_btree_key_addr(cur, ptr, block)));
2702 #endif
2705 * If the block is full, we can't insert the new entry until we
2706 * make the block un-full.
2708 xfs_btree_set_ptr_null(cur, &nptr);
2709 if (numrecs == cur->bc_ops->get_maxrecs(cur, level)) {
2710 error = xfs_btree_make_block_unfull(cur, level, numrecs,
2711 &optr, &ptr, &nptr, &ncur, &nrec, stat);
2712 if (error || *stat == 0)
2713 goto error0;
2717 * The current block may have changed if the block was
2718 * previously full and we have just made space in it.
2720 block = xfs_btree_get_block(cur, level, &bp);
2721 numrecs = xfs_btree_get_numrecs(block);
2723 #ifdef DEBUG
2724 error = xfs_btree_check_block(cur, block, level, bp);
2725 if (error)
2726 return error;
2727 #endif
2730 * At this point we know there's room for our new entry in the block
2731 * we're pointing at.
2733 XFS_BTREE_STATS_ADD(cur, moves, numrecs - ptr + 1);
2735 if (level > 0) {
2736 /* It's a nonleaf. make a hole in the keys and ptrs */
2737 union xfs_btree_key *kp;
2738 union xfs_btree_ptr *pp;
2740 kp = xfs_btree_key_addr(cur, ptr, block);
2741 pp = xfs_btree_ptr_addr(cur, ptr, block);
2743 #ifdef DEBUG
2744 for (i = numrecs - ptr; i >= 0; i--) {
2745 error = xfs_btree_check_ptr(cur, pp, i, level);
2746 if (error)
2747 return error;
2749 #endif
2751 xfs_btree_shift_keys(cur, kp, 1, numrecs - ptr + 1);
2752 xfs_btree_shift_ptrs(cur, pp, 1, numrecs - ptr + 1);
2754 #ifdef DEBUG
2755 error = xfs_btree_check_ptr(cur, ptrp, 0, level);
2756 if (error)
2757 goto error0;
2758 #endif
2760 /* Now put the new data in, bump numrecs and log it. */
2761 xfs_btree_copy_keys(cur, kp, &key, 1);
2762 xfs_btree_copy_ptrs(cur, pp, ptrp, 1);
2763 numrecs++;
2764 xfs_btree_set_numrecs(block, numrecs);
2765 xfs_btree_log_ptrs(cur, bp, ptr, numrecs);
2766 xfs_btree_log_keys(cur, bp, ptr, numrecs);
2767 #ifdef DEBUG
2768 if (ptr < numrecs) {
2769 ASSERT(cur->bc_ops->keys_inorder(cur, kp,
2770 xfs_btree_key_addr(cur, ptr + 1, block)));
2772 #endif
2773 } else {
2774 /* It's a leaf. make a hole in the records */
2775 union xfs_btree_rec *rp;
2777 rp = xfs_btree_rec_addr(cur, ptr, block);
2779 xfs_btree_shift_recs(cur, rp, 1, numrecs - ptr + 1);
2781 /* Now put the new data in, bump numrecs and log it. */
2782 xfs_btree_copy_recs(cur, rp, recp, 1);
2783 xfs_btree_set_numrecs(block, ++numrecs);
2784 xfs_btree_log_recs(cur, bp, ptr, numrecs);
2785 #ifdef DEBUG
2786 if (ptr < numrecs) {
2787 ASSERT(cur->bc_ops->recs_inorder(cur, rp,
2788 xfs_btree_rec_addr(cur, ptr + 1, block)));
2790 #endif
2793 /* Log the new number of records in the btree header. */
2794 xfs_btree_log_block(cur, bp, XFS_BB_NUMRECS);
2796 /* If we inserted at the start of a block, update the parents' keys. */
2797 if (optr == 1) {
2798 error = xfs_btree_updkey(cur, &key, level + 1);
2799 if (error)
2800 goto error0;
2804 * If we are tracking the last record in the tree and
2805 * we are at the far right edge of the tree, update it.
2807 if (xfs_btree_is_lastrec(cur, block, level)) {
2808 cur->bc_ops->update_lastrec(cur, block, recp,
2809 ptr, LASTREC_INSREC);
2813 * Return the new block number, if any.
2814 * If there is one, give back a record value and a cursor too.
2816 *ptrp = nptr;
2817 if (!xfs_btree_ptr_is_null(cur, &nptr)) {
2818 *recp = nrec;
2819 *curp = ncur;
2822 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2823 *stat = 1;
2824 return 0;
2826 error0:
2827 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
2828 return error;
2832 * Insert the record at the point referenced by cur.
2834 * A multi-level split of the tree on insert will invalidate the original
2835 * cursor. All callers of this function should assume that the cursor is
2836 * no longer valid and revalidate it.
2839 xfs_btree_insert(
2840 struct xfs_btree_cur *cur,
2841 int *stat)
2843 int error; /* error return value */
2844 int i; /* result value, 0 for failure */
2845 int level; /* current level number in btree */
2846 union xfs_btree_ptr nptr; /* new block number (split result) */
2847 struct xfs_btree_cur *ncur; /* new cursor (split result) */
2848 struct xfs_btree_cur *pcur; /* previous level's cursor */
2849 union xfs_btree_rec rec; /* record to insert */
2851 level = 0;
2852 ncur = NULL;
2853 pcur = cur;
2855 xfs_btree_set_ptr_null(cur, &nptr);
2856 cur->bc_ops->init_rec_from_cur(cur, &rec);
2859 * Loop going up the tree, starting at the leaf level.
2860 * Stop when we don't get a split block, that must mean that
2861 * the insert is finished with this level.
2863 do {
2865 * Insert nrec/nptr into this level of the tree.
2866 * Note if we fail, nptr will be null.
2868 error = xfs_btree_insrec(pcur, level, &nptr, &rec, &ncur, &i);
2869 if (error) {
2870 if (pcur != cur)
2871 xfs_btree_del_cursor(pcur, XFS_BTREE_ERROR);
2872 goto error0;
2875 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
2876 level++;
2879 * See if the cursor we just used is trash.
2880 * Can't trash the caller's cursor, but otherwise we should
2881 * if ncur is a new cursor or we're about to be done.
2883 if (pcur != cur &&
2884 (ncur || xfs_btree_ptr_is_null(cur, &nptr))) {
2885 /* Save the state from the cursor before we trash it */
2886 if (cur->bc_ops->update_cursor)
2887 cur->bc_ops->update_cursor(pcur, cur);
2888 cur->bc_nlevels = pcur->bc_nlevels;
2889 xfs_btree_del_cursor(pcur, XFS_BTREE_NOERROR);
2891 /* If we got a new cursor, switch to it. */
2892 if (ncur) {
2893 pcur = ncur;
2894 ncur = NULL;
2896 } while (!xfs_btree_ptr_is_null(cur, &nptr));
2898 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2899 *stat = i;
2900 return 0;
2901 error0:
2902 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
2903 return error;
2907 * Try to merge a non-leaf block back into the inode root.
2909 * Note: the killroot names comes from the fact that we're effectively
2910 * killing the old root block. But because we can't just delete the
2911 * inode we have to copy the single block it was pointing to into the
2912 * inode.
2914 STATIC int
2915 xfs_btree_kill_iroot(
2916 struct xfs_btree_cur *cur)
2918 int whichfork = cur->bc_private.b.whichfork;
2919 struct xfs_inode *ip = cur->bc_private.b.ip;
2920 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
2921 struct xfs_btree_block *block;
2922 struct xfs_btree_block *cblock;
2923 union xfs_btree_key *kp;
2924 union xfs_btree_key *ckp;
2925 union xfs_btree_ptr *pp;
2926 union xfs_btree_ptr *cpp;
2927 struct xfs_buf *cbp;
2928 int level;
2929 int index;
2930 int numrecs;
2931 #ifdef DEBUG
2932 union xfs_btree_ptr ptr;
2933 int i;
2934 #endif
2936 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
2938 ASSERT(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE);
2939 ASSERT(cur->bc_nlevels > 1);
2942 * Don't deal with the root block needs to be a leaf case.
2943 * We're just going to turn the thing back into extents anyway.
2945 level = cur->bc_nlevels - 1;
2946 if (level == 1)
2947 goto out0;
2950 * Give up if the root has multiple children.
2952 block = xfs_btree_get_iroot(cur);
2953 if (xfs_btree_get_numrecs(block) != 1)
2954 goto out0;
2956 cblock = xfs_btree_get_block(cur, level - 1, &cbp);
2957 numrecs = xfs_btree_get_numrecs(cblock);
2960 * Only do this if the next level will fit.
2961 * Then the data must be copied up to the inode,
2962 * instead of freeing the root you free the next level.
2964 if (numrecs > cur->bc_ops->get_dmaxrecs(cur, level))
2965 goto out0;
2967 XFS_BTREE_STATS_INC(cur, killroot);
2969 #ifdef DEBUG
2970 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_LEFTSIB);
2971 ASSERT(xfs_btree_ptr_is_null(cur, &ptr));
2972 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_RIGHTSIB);
2973 ASSERT(xfs_btree_ptr_is_null(cur, &ptr));
2974 #endif
2976 index = numrecs - cur->bc_ops->get_maxrecs(cur, level);
2977 if (index) {
2978 xfs_iroot_realloc(cur->bc_private.b.ip, index,
2979 cur->bc_private.b.whichfork);
2980 block = ifp->if_broot;
2983 be16_add_cpu(&block->bb_numrecs, index);
2984 ASSERT(block->bb_numrecs == cblock->bb_numrecs);
2986 kp = xfs_btree_key_addr(cur, 1, block);
2987 ckp = xfs_btree_key_addr(cur, 1, cblock);
2988 xfs_btree_copy_keys(cur, kp, ckp, numrecs);
2990 pp = xfs_btree_ptr_addr(cur, 1, block);
2991 cpp = xfs_btree_ptr_addr(cur, 1, cblock);
2992 #ifdef DEBUG
2993 for (i = 0; i < numrecs; i++) {
2994 int error;
2996 error = xfs_btree_check_ptr(cur, cpp, i, level - 1);
2997 if (error) {
2998 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
2999 return error;
3002 #endif
3003 xfs_btree_copy_ptrs(cur, pp, cpp, numrecs);
3005 cur->bc_ops->free_block(cur, cbp);
3006 XFS_BTREE_STATS_INC(cur, free);
3008 cur->bc_bufs[level - 1] = NULL;
3009 be16_add_cpu(&block->bb_level, -1);
3010 xfs_trans_log_inode(cur->bc_tp, ip,
3011 XFS_ILOG_CORE | xfs_ilog_fbroot(cur->bc_private.b.whichfork));
3012 cur->bc_nlevels--;
3013 out0:
3014 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3015 return 0;
3018 STATIC int
3019 xfs_btree_dec_cursor(
3020 struct xfs_btree_cur *cur,
3021 int level,
3022 int *stat)
3024 int error;
3025 int i;
3027 if (level > 0) {
3028 error = xfs_btree_decrement(cur, level, &i);
3029 if (error)
3030 return error;
3033 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3034 *stat = 1;
3035 return 0;
3039 * Single level of the btree record deletion routine.
3040 * Delete record pointed to by cur/level.
3041 * Remove the record from its block then rebalance the tree.
3042 * Return 0 for error, 1 for done, 2 to go on to the next level.
3044 STATIC int /* error */
3045 xfs_btree_delrec(
3046 struct xfs_btree_cur *cur, /* btree cursor */
3047 int level, /* level removing record from */
3048 int *stat) /* fail/done/go-on */
3050 struct xfs_btree_block *block; /* btree block */
3051 union xfs_btree_ptr cptr; /* current block ptr */
3052 struct xfs_buf *bp; /* buffer for block */
3053 int error; /* error return value */
3054 int i; /* loop counter */
3055 union xfs_btree_key key; /* storage for keyp */
3056 union xfs_btree_key *keyp = &key; /* passed to the next level */
3057 union xfs_btree_ptr lptr; /* left sibling block ptr */
3058 struct xfs_buf *lbp; /* left buffer pointer */
3059 struct xfs_btree_block *left; /* left btree block */
3060 int lrecs = 0; /* left record count */
3061 int ptr; /* key/record index */
3062 union xfs_btree_ptr rptr; /* right sibling block ptr */
3063 struct xfs_buf *rbp; /* right buffer pointer */
3064 struct xfs_btree_block *right; /* right btree block */
3065 struct xfs_btree_block *rrblock; /* right-right btree block */
3066 struct xfs_buf *rrbp; /* right-right buffer pointer */
3067 int rrecs = 0; /* right record count */
3068 struct xfs_btree_cur *tcur; /* temporary btree cursor */
3069 int numrecs; /* temporary numrec count */
3071 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
3072 XFS_BTREE_TRACE_ARGI(cur, level);
3074 tcur = NULL;
3076 /* Get the index of the entry being deleted, check for nothing there. */
3077 ptr = cur->bc_ptrs[level];
3078 if (ptr == 0) {
3079 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3080 *stat = 0;
3081 return 0;
3084 /* Get the buffer & block containing the record or key/ptr. */
3085 block = xfs_btree_get_block(cur, level, &bp);
3086 numrecs = xfs_btree_get_numrecs(block);
3088 #ifdef DEBUG
3089 error = xfs_btree_check_block(cur, block, level, bp);
3090 if (error)
3091 goto error0;
3092 #endif
3094 /* Fail if we're off the end of the block. */
3095 if (ptr > numrecs) {
3096 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3097 *stat = 0;
3098 return 0;
3101 XFS_BTREE_STATS_INC(cur, delrec);
3102 XFS_BTREE_STATS_ADD(cur, moves, numrecs - ptr);
3104 /* Excise the entries being deleted. */
3105 if (level > 0) {
3106 /* It's a nonleaf. operate on keys and ptrs */
3107 union xfs_btree_key *lkp;
3108 union xfs_btree_ptr *lpp;
3110 lkp = xfs_btree_key_addr(cur, ptr + 1, block);
3111 lpp = xfs_btree_ptr_addr(cur, ptr + 1, block);
3113 #ifdef DEBUG
3114 for (i = 0; i < numrecs - ptr; i++) {
3115 error = xfs_btree_check_ptr(cur, lpp, i, level);
3116 if (error)
3117 goto error0;
3119 #endif
3121 if (ptr < numrecs) {
3122 xfs_btree_shift_keys(cur, lkp, -1, numrecs - ptr);
3123 xfs_btree_shift_ptrs(cur, lpp, -1, numrecs - ptr);
3124 xfs_btree_log_keys(cur, bp, ptr, numrecs - 1);
3125 xfs_btree_log_ptrs(cur, bp, ptr, numrecs - 1);
3129 * If it's the first record in the block, we'll need to pass a
3130 * key up to the next level (updkey).
3132 if (ptr == 1)
3133 keyp = xfs_btree_key_addr(cur, 1, block);
3134 } else {
3135 /* It's a leaf. operate on records */
3136 if (ptr < numrecs) {
3137 xfs_btree_shift_recs(cur,
3138 xfs_btree_rec_addr(cur, ptr + 1, block),
3139 -1, numrecs - ptr);
3140 xfs_btree_log_recs(cur, bp, ptr, numrecs - 1);
3144 * If it's the first record in the block, we'll need a key
3145 * structure to pass up to the next level (updkey).
3147 if (ptr == 1) {
3148 cur->bc_ops->init_key_from_rec(&key,
3149 xfs_btree_rec_addr(cur, 1, block));
3150 keyp = &key;
3155 * Decrement and log the number of entries in the block.
3157 xfs_btree_set_numrecs(block, --numrecs);
3158 xfs_btree_log_block(cur, bp, XFS_BB_NUMRECS);
3161 * If we are tracking the last record in the tree and
3162 * we are at the far right edge of the tree, update it.
3164 if (xfs_btree_is_lastrec(cur, block, level)) {
3165 cur->bc_ops->update_lastrec(cur, block, NULL,
3166 ptr, LASTREC_DELREC);
3170 * We're at the root level. First, shrink the root block in-memory.
3171 * Try to get rid of the next level down. If we can't then there's
3172 * nothing left to do.
3174 if (level == cur->bc_nlevels - 1) {
3175 if (cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) {
3176 xfs_iroot_realloc(cur->bc_private.b.ip, -1,
3177 cur->bc_private.b.whichfork);
3179 error = xfs_btree_kill_iroot(cur);
3180 if (error)
3181 goto error0;
3183 error = xfs_btree_dec_cursor(cur, level, stat);
3184 if (error)
3185 goto error0;
3186 *stat = 1;
3187 return 0;
3191 * If this is the root level, and there's only one entry left,
3192 * and it's NOT the leaf level, then we can get rid of this
3193 * level.
3195 if (numrecs == 1 && level > 0) {
3196 union xfs_btree_ptr *pp;
3198 * pp is still set to the first pointer in the block.
3199 * Make it the new root of the btree.
3201 pp = xfs_btree_ptr_addr(cur, 1, block);
3202 error = cur->bc_ops->kill_root(cur, bp, level, pp);
3203 if (error)
3204 goto error0;
3205 } else if (level > 0) {
3206 error = xfs_btree_dec_cursor(cur, level, stat);
3207 if (error)
3208 goto error0;
3210 *stat = 1;
3211 return 0;
3215 * If we deleted the leftmost entry in the block, update the
3216 * key values above us in the tree.
3218 if (ptr == 1) {
3219 error = xfs_btree_updkey(cur, keyp, level + 1);
3220 if (error)
3221 goto error0;
3225 * If the number of records remaining in the block is at least
3226 * the minimum, we're done.
3228 if (numrecs >= cur->bc_ops->get_minrecs(cur, level)) {
3229 error = xfs_btree_dec_cursor(cur, level, stat);
3230 if (error)
3231 goto error0;
3232 return 0;
3236 * Otherwise, we have to move some records around to keep the
3237 * tree balanced. Look at the left and right sibling blocks to
3238 * see if we can re-balance by moving only one record.
3240 xfs_btree_get_sibling(cur, block, &rptr, XFS_BB_RIGHTSIB);
3241 xfs_btree_get_sibling(cur, block, &lptr, XFS_BB_LEFTSIB);
3243 if (cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) {
3245 * One child of root, need to get a chance to copy its contents
3246 * into the root and delete it. Can't go up to next level,
3247 * there's nothing to delete there.
3249 if (xfs_btree_ptr_is_null(cur, &rptr) &&
3250 xfs_btree_ptr_is_null(cur, &lptr) &&
3251 level == cur->bc_nlevels - 2) {
3252 error = xfs_btree_kill_iroot(cur);
3253 if (!error)
3254 error = xfs_btree_dec_cursor(cur, level, stat);
3255 if (error)
3256 goto error0;
3257 return 0;
3261 ASSERT(!xfs_btree_ptr_is_null(cur, &rptr) ||
3262 !xfs_btree_ptr_is_null(cur, &lptr));
3265 * Duplicate the cursor so our btree manipulations here won't
3266 * disrupt the next level up.
3268 error = xfs_btree_dup_cursor(cur, &tcur);
3269 if (error)
3270 goto error0;
3273 * If there's a right sibling, see if it's ok to shift an entry
3274 * out of it.
3276 if (!xfs_btree_ptr_is_null(cur, &rptr)) {
3278 * Move the temp cursor to the last entry in the next block.
3279 * Actually any entry but the first would suffice.
3281 i = xfs_btree_lastrec(tcur, level);
3282 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
3284 error = xfs_btree_increment(tcur, level, &i);
3285 if (error)
3286 goto error0;
3287 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
3289 i = xfs_btree_lastrec(tcur, level);
3290 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
3292 /* Grab a pointer to the block. */
3293 right = xfs_btree_get_block(tcur, level, &rbp);
3294 #ifdef DEBUG
3295 error = xfs_btree_check_block(tcur, right, level, rbp);
3296 if (error)
3297 goto error0;
3298 #endif
3299 /* Grab the current block number, for future use. */
3300 xfs_btree_get_sibling(tcur, right, &cptr, XFS_BB_LEFTSIB);
3303 * If right block is full enough so that removing one entry
3304 * won't make it too empty, and left-shifting an entry out
3305 * of right to us works, we're done.
3307 if (xfs_btree_get_numrecs(right) - 1 >=
3308 cur->bc_ops->get_minrecs(tcur, level)) {
3309 error = xfs_btree_lshift(tcur, level, &i);
3310 if (error)
3311 goto error0;
3312 if (i) {
3313 ASSERT(xfs_btree_get_numrecs(block) >=
3314 cur->bc_ops->get_minrecs(tcur, level));
3316 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
3317 tcur = NULL;
3319 error = xfs_btree_dec_cursor(cur, level, stat);
3320 if (error)
3321 goto error0;
3322 return 0;
3327 * Otherwise, grab the number of records in right for
3328 * future reference, and fix up the temp cursor to point
3329 * to our block again (last record).
3331 rrecs = xfs_btree_get_numrecs(right);
3332 if (!xfs_btree_ptr_is_null(cur, &lptr)) {
3333 i = xfs_btree_firstrec(tcur, level);
3334 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
3336 error = xfs_btree_decrement(tcur, level, &i);
3337 if (error)
3338 goto error0;
3339 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
3344 * If there's a left sibling, see if it's ok to shift an entry
3345 * out of it.
3347 if (!xfs_btree_ptr_is_null(cur, &lptr)) {
3349 * Move the temp cursor to the first entry in the
3350 * previous block.
3352 i = xfs_btree_firstrec(tcur, level);
3353 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
3355 error = xfs_btree_decrement(tcur, level, &i);
3356 if (error)
3357 goto error0;
3358 i = xfs_btree_firstrec(tcur, level);
3359 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
3361 /* Grab a pointer to the block. */
3362 left = xfs_btree_get_block(tcur, level, &lbp);
3363 #ifdef DEBUG
3364 error = xfs_btree_check_block(cur, left, level, lbp);
3365 if (error)
3366 goto error0;
3367 #endif
3368 /* Grab the current block number, for future use. */
3369 xfs_btree_get_sibling(tcur, left, &cptr, XFS_BB_RIGHTSIB);
3372 * If left block is full enough so that removing one entry
3373 * won't make it too empty, and right-shifting an entry out
3374 * of left to us works, we're done.
3376 if (xfs_btree_get_numrecs(left) - 1 >=
3377 cur->bc_ops->get_minrecs(tcur, level)) {
3378 error = xfs_btree_rshift(tcur, level, &i);
3379 if (error)
3380 goto error0;
3381 if (i) {
3382 ASSERT(xfs_btree_get_numrecs(block) >=
3383 cur->bc_ops->get_minrecs(tcur, level));
3384 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
3385 tcur = NULL;
3386 if (level == 0)
3387 cur->bc_ptrs[0]++;
3388 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3389 *stat = 1;
3390 return 0;
3395 * Otherwise, grab the number of records in right for
3396 * future reference.
3398 lrecs = xfs_btree_get_numrecs(left);
3401 /* Delete the temp cursor, we're done with it. */
3402 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
3403 tcur = NULL;
3405 /* If here, we need to do a join to keep the tree balanced. */
3406 ASSERT(!xfs_btree_ptr_is_null(cur, &cptr));
3408 if (!xfs_btree_ptr_is_null(cur, &lptr) &&
3409 lrecs + xfs_btree_get_numrecs(block) <=
3410 cur->bc_ops->get_maxrecs(cur, level)) {
3412 * Set "right" to be the starting block,
3413 * "left" to be the left neighbor.
3415 rptr = cptr;
3416 right = block;
3417 rbp = bp;
3418 error = xfs_btree_read_buf_block(cur, &lptr, level,
3419 0, &left, &lbp);
3420 if (error)
3421 goto error0;
3424 * If that won't work, see if we can join with the right neighbor block.
3426 } else if (!xfs_btree_ptr_is_null(cur, &rptr) &&
3427 rrecs + xfs_btree_get_numrecs(block) <=
3428 cur->bc_ops->get_maxrecs(cur, level)) {
3430 * Set "left" to be the starting block,
3431 * "right" to be the right neighbor.
3433 lptr = cptr;
3434 left = block;
3435 lbp = bp;
3436 error = xfs_btree_read_buf_block(cur, &rptr, level,
3437 0, &right, &rbp);
3438 if (error)
3439 goto error0;
3442 * Otherwise, we can't fix the imbalance.
3443 * Just return. This is probably a logic error, but it's not fatal.
3445 } else {
3446 error = xfs_btree_dec_cursor(cur, level, stat);
3447 if (error)
3448 goto error0;
3449 return 0;
3452 rrecs = xfs_btree_get_numrecs(right);
3453 lrecs = xfs_btree_get_numrecs(left);
3456 * We're now going to join "left" and "right" by moving all the stuff
3457 * in "right" to "left" and deleting "right".
3459 XFS_BTREE_STATS_ADD(cur, moves, rrecs);
3460 if (level > 0) {
3461 /* It's a non-leaf. Move keys and pointers. */
3462 union xfs_btree_key *lkp; /* left btree key */
3463 union xfs_btree_ptr *lpp; /* left address pointer */
3464 union xfs_btree_key *rkp; /* right btree key */
3465 union xfs_btree_ptr *rpp; /* right address pointer */
3467 lkp = xfs_btree_key_addr(cur, lrecs + 1, left);
3468 lpp = xfs_btree_ptr_addr(cur, lrecs + 1, left);
3469 rkp = xfs_btree_key_addr(cur, 1, right);
3470 rpp = xfs_btree_ptr_addr(cur, 1, right);
3471 #ifdef DEBUG
3472 for (i = 1; i < rrecs; i++) {
3473 error = xfs_btree_check_ptr(cur, rpp, i, level);
3474 if (error)
3475 goto error0;
3477 #endif
3478 xfs_btree_copy_keys(cur, lkp, rkp, rrecs);
3479 xfs_btree_copy_ptrs(cur, lpp, rpp, rrecs);
3481 xfs_btree_log_keys(cur, lbp, lrecs + 1, lrecs + rrecs);
3482 xfs_btree_log_ptrs(cur, lbp, lrecs + 1, lrecs + rrecs);
3483 } else {
3484 /* It's a leaf. Move records. */
3485 union xfs_btree_rec *lrp; /* left record pointer */
3486 union xfs_btree_rec *rrp; /* right record pointer */
3488 lrp = xfs_btree_rec_addr(cur, lrecs + 1, left);
3489 rrp = xfs_btree_rec_addr(cur, 1, right);
3491 xfs_btree_copy_recs(cur, lrp, rrp, rrecs);
3492 xfs_btree_log_recs(cur, lbp, lrecs + 1, lrecs + rrecs);
3495 XFS_BTREE_STATS_INC(cur, join);
3498 * Fix up the number of records and right block pointer in the
3499 * surviving block, and log it.
3501 xfs_btree_set_numrecs(left, lrecs + rrecs);
3502 xfs_btree_get_sibling(cur, right, &cptr, XFS_BB_RIGHTSIB),
3503 xfs_btree_set_sibling(cur, left, &cptr, XFS_BB_RIGHTSIB);
3504 xfs_btree_log_block(cur, lbp, XFS_BB_NUMRECS | XFS_BB_RIGHTSIB);
3506 /* If there is a right sibling, point it to the remaining block. */
3507 xfs_btree_get_sibling(cur, left, &cptr, XFS_BB_RIGHTSIB);
3508 if (!xfs_btree_ptr_is_null(cur, &cptr)) {
3509 error = xfs_btree_read_buf_block(cur, &cptr, level,
3510 0, &rrblock, &rrbp);
3511 if (error)
3512 goto error0;
3513 xfs_btree_set_sibling(cur, rrblock, &lptr, XFS_BB_LEFTSIB);
3514 xfs_btree_log_block(cur, rrbp, XFS_BB_LEFTSIB);
3517 /* Free the deleted block. */
3518 error = cur->bc_ops->free_block(cur, rbp);
3519 if (error)
3520 goto error0;
3521 XFS_BTREE_STATS_INC(cur, free);
3524 * If we joined with the left neighbor, set the buffer in the
3525 * cursor to the left block, and fix up the index.
3527 if (bp != lbp) {
3528 cur->bc_bufs[level] = lbp;
3529 cur->bc_ptrs[level] += lrecs;
3530 cur->bc_ra[level] = 0;
3533 * If we joined with the right neighbor and there's a level above
3534 * us, increment the cursor at that level.
3536 else if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) ||
3537 (level + 1 < cur->bc_nlevels)) {
3538 error = xfs_btree_increment(cur, level + 1, &i);
3539 if (error)
3540 goto error0;
3544 * Readjust the ptr at this level if it's not a leaf, since it's
3545 * still pointing at the deletion point, which makes the cursor
3546 * inconsistent. If this makes the ptr 0, the caller fixes it up.
3547 * We can't use decrement because it would change the next level up.
3549 if (level > 0)
3550 cur->bc_ptrs[level]--;
3552 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3553 /* Return value means the next level up has something to do. */
3554 *stat = 2;
3555 return 0;
3557 error0:
3558 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
3559 if (tcur)
3560 xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
3561 return error;
3565 * Delete the record pointed to by cur.
3566 * The cursor refers to the place where the record was (could be inserted)
3567 * when the operation returns.
3569 int /* error */
3570 xfs_btree_delete(
3571 struct xfs_btree_cur *cur,
3572 int *stat) /* success/failure */
3574 int error; /* error return value */
3575 int level;
3576 int i;
3578 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
3581 * Go up the tree, starting at leaf level.
3583 * If 2 is returned then a join was done; go to the next level.
3584 * Otherwise we are done.
3586 for (level = 0, i = 2; i == 2; level++) {
3587 error = xfs_btree_delrec(cur, level, &i);
3588 if (error)
3589 goto error0;
3592 if (i == 0) {
3593 for (level = 1; level < cur->bc_nlevels; level++) {
3594 if (cur->bc_ptrs[level] == 0) {
3595 error = xfs_btree_decrement(cur, level, &i);
3596 if (error)
3597 goto error0;
3598 break;
3603 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3604 *stat = i;
3605 return 0;
3606 error0:
3607 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
3608 return error;
3612 * Get the data from the pointed-to record.
3614 int /* error */
3615 xfs_btree_get_rec(
3616 struct xfs_btree_cur *cur, /* btree cursor */
3617 union xfs_btree_rec **recp, /* output: btree record */
3618 int *stat) /* output: success/failure */
3620 struct xfs_btree_block *block; /* btree block */
3621 struct xfs_buf *bp; /* buffer pointer */
3622 int ptr; /* record number */
3623 #ifdef DEBUG
3624 int error; /* error return value */
3625 #endif
3627 ptr = cur->bc_ptrs[0];
3628 block = xfs_btree_get_block(cur, 0, &bp);
3630 #ifdef DEBUG
3631 error = xfs_btree_check_block(cur, block, 0, bp);
3632 if (error)
3633 return error;
3634 #endif
3637 * Off the right end or left end, return failure.
3639 if (ptr > xfs_btree_get_numrecs(block) || ptr <= 0) {
3640 *stat = 0;
3641 return 0;
3645 * Point to the record and extract its data.
3647 *recp = xfs_btree_rec_addr(cur, ptr, block);
3648 *stat = 1;
3649 return 0;