udf: Remove BKL from free space counting functions
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / xfs / xfs_btree.c
blob04f9cca8da7e6dd0f15e409aa86ac9ab1c6305ff
1 /*
2 * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 #include "xfs.h"
19 #include "xfs_fs.h"
20 #include "xfs_types.h"
21 #include "xfs_bit.h"
22 #include "xfs_log.h"
23 #include "xfs_inum.h"
24 #include "xfs_trans.h"
25 #include "xfs_sb.h"
26 #include "xfs_ag.h"
27 #include "xfs_mount.h"
28 #include "xfs_bmap_btree.h"
29 #include "xfs_alloc_btree.h"
30 #include "xfs_ialloc_btree.h"
31 #include "xfs_dinode.h"
32 #include "xfs_inode.h"
33 #include "xfs_inode_item.h"
34 #include "xfs_btree.h"
35 #include "xfs_btree_trace.h"
36 #include "xfs_error.h"
37 #include "xfs_trace.h"
40 * Cursor allocation zone.
42 kmem_zone_t *xfs_btree_cur_zone;
45 * Btree magic numbers.
47 const __uint32_t xfs_magics[XFS_BTNUM_MAX] = {
48 XFS_ABTB_MAGIC, XFS_ABTC_MAGIC, XFS_BMAP_MAGIC, XFS_IBT_MAGIC
52 STATIC int /* error (0 or EFSCORRUPTED) */
53 xfs_btree_check_lblock(
54 struct xfs_btree_cur *cur, /* btree cursor */
55 struct xfs_btree_block *block, /* btree long form block pointer */
56 int level, /* level of the btree block */
57 struct xfs_buf *bp) /* buffer for block, if any */
59 int lblock_ok; /* block passes checks */
60 struct xfs_mount *mp; /* file system mount point */
62 mp = cur->bc_mp;
63 lblock_ok =
64 be32_to_cpu(block->bb_magic) == xfs_magics[cur->bc_btnum] &&
65 be16_to_cpu(block->bb_level) == level &&
66 be16_to_cpu(block->bb_numrecs) <=
67 cur->bc_ops->get_maxrecs(cur, level) &&
68 block->bb_u.l.bb_leftsib &&
69 (be64_to_cpu(block->bb_u.l.bb_leftsib) == NULLDFSBNO ||
70 XFS_FSB_SANITY_CHECK(mp,
71 be64_to_cpu(block->bb_u.l.bb_leftsib))) &&
72 block->bb_u.l.bb_rightsib &&
73 (be64_to_cpu(block->bb_u.l.bb_rightsib) == NULLDFSBNO ||
74 XFS_FSB_SANITY_CHECK(mp,
75 be64_to_cpu(block->bb_u.l.bb_rightsib)));
76 if (unlikely(XFS_TEST_ERROR(!lblock_ok, mp,
77 XFS_ERRTAG_BTREE_CHECK_LBLOCK,
78 XFS_RANDOM_BTREE_CHECK_LBLOCK))) {
79 if (bp)
80 trace_xfs_btree_corrupt(bp, _RET_IP_);
81 XFS_ERROR_REPORT("xfs_btree_check_lblock", XFS_ERRLEVEL_LOW,
82 mp);
83 return XFS_ERROR(EFSCORRUPTED);
85 return 0;
88 STATIC int /* error (0 or EFSCORRUPTED) */
89 xfs_btree_check_sblock(
90 struct xfs_btree_cur *cur, /* btree cursor */
91 struct xfs_btree_block *block, /* btree short form block pointer */
92 int level, /* level of the btree block */
93 struct xfs_buf *bp) /* buffer containing block */
95 struct xfs_buf *agbp; /* buffer for ag. freespace struct */
96 struct xfs_agf *agf; /* ag. freespace structure */
97 xfs_agblock_t agflen; /* native ag. freespace length */
98 int sblock_ok; /* block passes checks */
100 agbp = cur->bc_private.a.agbp;
101 agf = XFS_BUF_TO_AGF(agbp);
102 agflen = be32_to_cpu(agf->agf_length);
103 sblock_ok =
104 be32_to_cpu(block->bb_magic) == xfs_magics[cur->bc_btnum] &&
105 be16_to_cpu(block->bb_level) == level &&
106 be16_to_cpu(block->bb_numrecs) <=
107 cur->bc_ops->get_maxrecs(cur, level) &&
108 (be32_to_cpu(block->bb_u.s.bb_leftsib) == NULLAGBLOCK ||
109 be32_to_cpu(block->bb_u.s.bb_leftsib) < agflen) &&
110 block->bb_u.s.bb_leftsib &&
111 (be32_to_cpu(block->bb_u.s.bb_rightsib) == NULLAGBLOCK ||
112 be32_to_cpu(block->bb_u.s.bb_rightsib) < agflen) &&
113 block->bb_u.s.bb_rightsib;
114 if (unlikely(XFS_TEST_ERROR(!sblock_ok, cur->bc_mp,
115 XFS_ERRTAG_BTREE_CHECK_SBLOCK,
116 XFS_RANDOM_BTREE_CHECK_SBLOCK))) {
117 if (bp)
118 trace_xfs_btree_corrupt(bp, _RET_IP_);
119 XFS_CORRUPTION_ERROR("xfs_btree_check_sblock",
120 XFS_ERRLEVEL_LOW, cur->bc_mp, block);
121 return XFS_ERROR(EFSCORRUPTED);
123 return 0;
127 * Debug routine: check that block header is ok.
130 xfs_btree_check_block(
131 struct xfs_btree_cur *cur, /* btree cursor */
132 struct xfs_btree_block *block, /* generic btree block pointer */
133 int level, /* level of the btree block */
134 struct xfs_buf *bp) /* buffer containing block, if any */
136 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
137 return xfs_btree_check_lblock(cur, block, level, bp);
138 else
139 return xfs_btree_check_sblock(cur, block, level, bp);
143 * Check that (long) pointer is ok.
145 int /* error (0 or EFSCORRUPTED) */
146 xfs_btree_check_lptr(
147 struct xfs_btree_cur *cur, /* btree cursor */
148 xfs_dfsbno_t bno, /* btree block disk address */
149 int level) /* btree block level */
151 XFS_WANT_CORRUPTED_RETURN(
152 level > 0 &&
153 bno != NULLDFSBNO &&
154 XFS_FSB_SANITY_CHECK(cur->bc_mp, bno));
155 return 0;
158 #ifdef DEBUG
160 * Check that (short) pointer is ok.
162 STATIC int /* error (0 or EFSCORRUPTED) */
163 xfs_btree_check_sptr(
164 struct xfs_btree_cur *cur, /* btree cursor */
165 xfs_agblock_t bno, /* btree block disk address */
166 int level) /* btree block level */
168 xfs_agblock_t agblocks = cur->bc_mp->m_sb.sb_agblocks;
170 XFS_WANT_CORRUPTED_RETURN(
171 level > 0 &&
172 bno != NULLAGBLOCK &&
173 bno != 0 &&
174 bno < agblocks);
175 return 0;
179 * Check that block ptr is ok.
181 STATIC int /* error (0 or EFSCORRUPTED) */
182 xfs_btree_check_ptr(
183 struct xfs_btree_cur *cur, /* btree cursor */
184 union xfs_btree_ptr *ptr, /* btree block disk address */
185 int index, /* offset from ptr to check */
186 int level) /* btree block level */
188 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
189 return xfs_btree_check_lptr(cur,
190 be64_to_cpu((&ptr->l)[index]), level);
191 } else {
192 return xfs_btree_check_sptr(cur,
193 be32_to_cpu((&ptr->s)[index]), level);
196 #endif
199 * Delete the btree cursor.
201 void
202 xfs_btree_del_cursor(
203 xfs_btree_cur_t *cur, /* btree cursor */
204 int error) /* del because of error */
206 int i; /* btree level */
209 * Clear the buffer pointers, and release the buffers.
210 * If we're doing this in the face of an error, we
211 * need to make sure to inspect all of the entries
212 * in the bc_bufs array for buffers to be unlocked.
213 * This is because some of the btree code works from
214 * level n down to 0, and if we get an error along
215 * the way we won't have initialized all the entries
216 * down to 0.
218 for (i = 0; i < cur->bc_nlevels; i++) {
219 if (cur->bc_bufs[i])
220 xfs_trans_brelse(cur->bc_tp, cur->bc_bufs[i]);
221 else if (!error)
222 break;
225 * Can't free a bmap cursor without having dealt with the
226 * allocated indirect blocks' accounting.
228 ASSERT(cur->bc_btnum != XFS_BTNUM_BMAP ||
229 cur->bc_private.b.allocated == 0);
231 * Free the cursor.
233 kmem_zone_free(xfs_btree_cur_zone, cur);
237 * Duplicate the btree cursor.
238 * Allocate a new one, copy the record, re-get the buffers.
240 int /* error */
241 xfs_btree_dup_cursor(
242 xfs_btree_cur_t *cur, /* input cursor */
243 xfs_btree_cur_t **ncur) /* output cursor */
245 xfs_buf_t *bp; /* btree block's buffer pointer */
246 int error; /* error return value */
247 int i; /* level number of btree block */
248 xfs_mount_t *mp; /* mount structure for filesystem */
249 xfs_btree_cur_t *new; /* new cursor value */
250 xfs_trans_t *tp; /* transaction pointer, can be NULL */
252 tp = cur->bc_tp;
253 mp = cur->bc_mp;
256 * Allocate a new cursor like the old one.
258 new = cur->bc_ops->dup_cursor(cur);
261 * Copy the record currently in the cursor.
263 new->bc_rec = cur->bc_rec;
266 * For each level current, re-get the buffer and copy the ptr value.
268 for (i = 0; i < new->bc_nlevels; i++) {
269 new->bc_ptrs[i] = cur->bc_ptrs[i];
270 new->bc_ra[i] = cur->bc_ra[i];
271 if ((bp = cur->bc_bufs[i])) {
272 if ((error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp,
273 XFS_BUF_ADDR(bp), mp->m_bsize, 0, &bp))) {
274 xfs_btree_del_cursor(new, error);
275 *ncur = NULL;
276 return error;
278 new->bc_bufs[i] = bp;
279 ASSERT(bp);
280 ASSERT(!XFS_BUF_GETERROR(bp));
281 } else
282 new->bc_bufs[i] = NULL;
284 *ncur = new;
285 return 0;
289 * XFS btree block layout and addressing:
291 * There are two types of blocks in the btree: leaf and non-leaf blocks.
293 * The leaf record start with a header then followed by records containing
294 * the values. A non-leaf block also starts with the same header, and
295 * then first contains lookup keys followed by an equal number of pointers
296 * to the btree blocks at the previous level.
298 * +--------+-------+-------+-------+-------+-------+-------+
299 * Leaf: | header | rec 1 | rec 2 | rec 3 | rec 4 | rec 5 | rec N |
300 * +--------+-------+-------+-------+-------+-------+-------+
302 * +--------+-------+-------+-------+-------+-------+-------+
303 * Non-Leaf: | header | key 1 | key 2 | key N | ptr 1 | ptr 2 | ptr N |
304 * +--------+-------+-------+-------+-------+-------+-------+
306 * The header is called struct xfs_btree_block for reasons better left unknown
307 * and comes in different versions for short (32bit) and long (64bit) block
308 * pointers. The record and key structures are defined by the btree instances
309 * and opaque to the btree core. The block pointers are simple disk endian
310 * integers, available in a short (32bit) and long (64bit) variant.
312 * The helpers below calculate the offset of a given record, key or pointer
313 * into a btree block (xfs_btree_*_offset) or return a pointer to the given
314 * record, key or pointer (xfs_btree_*_addr). Note that all addressing
315 * inside the btree block is done using indices starting at one, not zero!
319 * Return size of the btree block header for this btree instance.
321 static inline size_t xfs_btree_block_len(struct xfs_btree_cur *cur)
323 return (cur->bc_flags & XFS_BTREE_LONG_PTRS) ?
324 XFS_BTREE_LBLOCK_LEN :
325 XFS_BTREE_SBLOCK_LEN;
329 * Return size of btree block pointers for this btree instance.
331 static inline size_t xfs_btree_ptr_len(struct xfs_btree_cur *cur)
333 return (cur->bc_flags & XFS_BTREE_LONG_PTRS) ?
334 sizeof(__be64) : sizeof(__be32);
338 * Calculate offset of the n-th record in a btree block.
340 STATIC size_t
341 xfs_btree_rec_offset(
342 struct xfs_btree_cur *cur,
343 int n)
345 return xfs_btree_block_len(cur) +
346 (n - 1) * cur->bc_ops->rec_len;
350 * Calculate offset of the n-th key in a btree block.
352 STATIC size_t
353 xfs_btree_key_offset(
354 struct xfs_btree_cur *cur,
355 int n)
357 return xfs_btree_block_len(cur) +
358 (n - 1) * cur->bc_ops->key_len;
362 * Calculate offset of the n-th block pointer in a btree block.
364 STATIC size_t
365 xfs_btree_ptr_offset(
366 struct xfs_btree_cur *cur,
367 int n,
368 int level)
370 return xfs_btree_block_len(cur) +
371 cur->bc_ops->get_maxrecs(cur, level) * cur->bc_ops->key_len +
372 (n - 1) * xfs_btree_ptr_len(cur);
376 * Return a pointer to the n-th record in the btree block.
378 STATIC union xfs_btree_rec *
379 xfs_btree_rec_addr(
380 struct xfs_btree_cur *cur,
381 int n,
382 struct xfs_btree_block *block)
384 return (union xfs_btree_rec *)
385 ((char *)block + xfs_btree_rec_offset(cur, n));
389 * Return a pointer to the n-th key in the btree block.
391 STATIC union xfs_btree_key *
392 xfs_btree_key_addr(
393 struct xfs_btree_cur *cur,
394 int n,
395 struct xfs_btree_block *block)
397 return (union xfs_btree_key *)
398 ((char *)block + xfs_btree_key_offset(cur, n));
402 * Return a pointer to the n-th block pointer in the btree block.
404 STATIC union xfs_btree_ptr *
405 xfs_btree_ptr_addr(
406 struct xfs_btree_cur *cur,
407 int n,
408 struct xfs_btree_block *block)
410 int level = xfs_btree_get_level(block);
412 ASSERT(block->bb_level != 0);
414 return (union xfs_btree_ptr *)
415 ((char *)block + xfs_btree_ptr_offset(cur, n, level));
419 * Get a the root block which is stored in the inode.
421 * For now this btree implementation assumes the btree root is always
422 * stored in the if_broot field of an inode fork.
424 STATIC struct xfs_btree_block *
425 xfs_btree_get_iroot(
426 struct xfs_btree_cur *cur)
428 struct xfs_ifork *ifp;
430 ifp = XFS_IFORK_PTR(cur->bc_private.b.ip, cur->bc_private.b.whichfork);
431 return (struct xfs_btree_block *)ifp->if_broot;
435 * Retrieve the block pointer from the cursor at the given level.
436 * This may be an inode btree root or from a buffer.
438 STATIC struct xfs_btree_block * /* generic btree block pointer */
439 xfs_btree_get_block(
440 struct xfs_btree_cur *cur, /* btree cursor */
441 int level, /* level in btree */
442 struct xfs_buf **bpp) /* buffer containing the block */
444 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
445 (level == cur->bc_nlevels - 1)) {
446 *bpp = NULL;
447 return xfs_btree_get_iroot(cur);
450 *bpp = cur->bc_bufs[level];
451 return XFS_BUF_TO_BLOCK(*bpp);
455 * Get a buffer for the block, return it with no data read.
456 * Long-form addressing.
458 xfs_buf_t * /* buffer for fsbno */
459 xfs_btree_get_bufl(
460 xfs_mount_t *mp, /* file system mount point */
461 xfs_trans_t *tp, /* transaction pointer */
462 xfs_fsblock_t fsbno, /* file system block number */
463 uint lock) /* lock flags for get_buf */
465 xfs_buf_t *bp; /* buffer pointer (return value) */
466 xfs_daddr_t d; /* real disk block address */
468 ASSERT(fsbno != NULLFSBLOCK);
469 d = XFS_FSB_TO_DADDR(mp, fsbno);
470 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d, mp->m_bsize, lock);
471 ASSERT(bp);
472 ASSERT(!XFS_BUF_GETERROR(bp));
473 return bp;
477 * Get a buffer for the block, return it with no data read.
478 * Short-form addressing.
480 xfs_buf_t * /* buffer for agno/agbno */
481 xfs_btree_get_bufs(
482 xfs_mount_t *mp, /* file system mount point */
483 xfs_trans_t *tp, /* transaction pointer */
484 xfs_agnumber_t agno, /* allocation group number */
485 xfs_agblock_t agbno, /* allocation group block number */
486 uint lock) /* lock flags for get_buf */
488 xfs_buf_t *bp; /* buffer pointer (return value) */
489 xfs_daddr_t d; /* real disk block address */
491 ASSERT(agno != NULLAGNUMBER);
492 ASSERT(agbno != NULLAGBLOCK);
493 d = XFS_AGB_TO_DADDR(mp, agno, agbno);
494 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d, mp->m_bsize, lock);
495 ASSERT(bp);
496 ASSERT(!XFS_BUF_GETERROR(bp));
497 return bp;
501 * Check for the cursor referring to the last block at the given level.
503 int /* 1=is last block, 0=not last block */
504 xfs_btree_islastblock(
505 xfs_btree_cur_t *cur, /* btree cursor */
506 int level) /* level to check */
508 struct xfs_btree_block *block; /* generic btree block pointer */
509 xfs_buf_t *bp; /* buffer containing block */
511 block = xfs_btree_get_block(cur, level, &bp);
512 xfs_btree_check_block(cur, block, level, bp);
513 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
514 return be64_to_cpu(block->bb_u.l.bb_rightsib) == NULLDFSBNO;
515 else
516 return be32_to_cpu(block->bb_u.s.bb_rightsib) == NULLAGBLOCK;
520 * Change the cursor to point to the first record at the given level.
521 * Other levels are unaffected.
523 STATIC int /* success=1, failure=0 */
524 xfs_btree_firstrec(
525 xfs_btree_cur_t *cur, /* btree cursor */
526 int level) /* level to change */
528 struct xfs_btree_block *block; /* generic btree block pointer */
529 xfs_buf_t *bp; /* buffer containing block */
532 * Get the block pointer for this level.
534 block = xfs_btree_get_block(cur, level, &bp);
535 xfs_btree_check_block(cur, block, level, bp);
537 * It's empty, there is no such record.
539 if (!block->bb_numrecs)
540 return 0;
542 * Set the ptr value to 1, that's the first record/key.
544 cur->bc_ptrs[level] = 1;
545 return 1;
549 * Change the cursor to point to the last record in the current block
550 * at the given level. Other levels are unaffected.
552 STATIC int /* success=1, failure=0 */
553 xfs_btree_lastrec(
554 xfs_btree_cur_t *cur, /* btree cursor */
555 int level) /* level to change */
557 struct xfs_btree_block *block; /* generic btree block pointer */
558 xfs_buf_t *bp; /* buffer containing block */
561 * Get the block pointer for this level.
563 block = xfs_btree_get_block(cur, level, &bp);
564 xfs_btree_check_block(cur, block, level, bp);
566 * It's empty, there is no such record.
568 if (!block->bb_numrecs)
569 return 0;
571 * Set the ptr value to numrecs, that's the last record/key.
573 cur->bc_ptrs[level] = be16_to_cpu(block->bb_numrecs);
574 return 1;
578 * Compute first and last byte offsets for the fields given.
579 * Interprets the offsets table, which contains struct field offsets.
581 void
582 xfs_btree_offsets(
583 __int64_t fields, /* bitmask of fields */
584 const short *offsets, /* table of field offsets */
585 int nbits, /* number of bits to inspect */
586 int *first, /* output: first byte offset */
587 int *last) /* output: last byte offset */
589 int i; /* current bit number */
590 __int64_t imask; /* mask for current bit number */
592 ASSERT(fields != 0);
594 * Find the lowest bit, so the first byte offset.
596 for (i = 0, imask = 1LL; ; i++, imask <<= 1) {
597 if (imask & fields) {
598 *first = offsets[i];
599 break;
603 * Find the highest bit, so the last byte offset.
605 for (i = nbits - 1, imask = 1LL << i; ; i--, imask >>= 1) {
606 if (imask & fields) {
607 *last = offsets[i + 1] - 1;
608 break;
614 * Get a buffer for the block, return it read in.
615 * Long-form addressing.
617 int /* error */
618 xfs_btree_read_bufl(
619 xfs_mount_t *mp, /* file system mount point */
620 xfs_trans_t *tp, /* transaction pointer */
621 xfs_fsblock_t fsbno, /* file system block number */
622 uint lock, /* lock flags for read_buf */
623 xfs_buf_t **bpp, /* buffer for fsbno */
624 int refval) /* ref count value for buffer */
626 xfs_buf_t *bp; /* return value */
627 xfs_daddr_t d; /* real disk block address */
628 int error;
630 ASSERT(fsbno != NULLFSBLOCK);
631 d = XFS_FSB_TO_DADDR(mp, fsbno);
632 if ((error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, d,
633 mp->m_bsize, lock, &bp))) {
634 return error;
636 ASSERT(!bp || !XFS_BUF_GETERROR(bp));
637 if (bp != NULL) {
638 XFS_BUF_SET_VTYPE_REF(bp, B_FS_MAP, refval);
640 *bpp = bp;
641 return 0;
645 * Read-ahead the block, don't wait for it, don't return a buffer.
646 * Long-form addressing.
648 /* ARGSUSED */
649 void
650 xfs_btree_reada_bufl(
651 xfs_mount_t *mp, /* file system mount point */
652 xfs_fsblock_t fsbno, /* file system block number */
653 xfs_extlen_t count) /* count of filesystem blocks */
655 xfs_daddr_t d;
657 ASSERT(fsbno != NULLFSBLOCK);
658 d = XFS_FSB_TO_DADDR(mp, fsbno);
659 xfs_buf_readahead(mp->m_ddev_targp, d, mp->m_bsize * count);
663 * Read-ahead the block, don't wait for it, don't return a buffer.
664 * Short-form addressing.
666 /* ARGSUSED */
667 void
668 xfs_btree_reada_bufs(
669 xfs_mount_t *mp, /* file system mount point */
670 xfs_agnumber_t agno, /* allocation group number */
671 xfs_agblock_t agbno, /* allocation group block number */
672 xfs_extlen_t count) /* count of filesystem blocks */
674 xfs_daddr_t d;
676 ASSERT(agno != NULLAGNUMBER);
677 ASSERT(agbno != NULLAGBLOCK);
678 d = XFS_AGB_TO_DADDR(mp, agno, agbno);
679 xfs_buf_readahead(mp->m_ddev_targp, d, mp->m_bsize * count);
682 STATIC int
683 xfs_btree_readahead_lblock(
684 struct xfs_btree_cur *cur,
685 int lr,
686 struct xfs_btree_block *block)
688 int rval = 0;
689 xfs_dfsbno_t left = be64_to_cpu(block->bb_u.l.bb_leftsib);
690 xfs_dfsbno_t right = be64_to_cpu(block->bb_u.l.bb_rightsib);
692 if ((lr & XFS_BTCUR_LEFTRA) && left != NULLDFSBNO) {
693 xfs_btree_reada_bufl(cur->bc_mp, left, 1);
694 rval++;
697 if ((lr & XFS_BTCUR_RIGHTRA) && right != NULLDFSBNO) {
698 xfs_btree_reada_bufl(cur->bc_mp, right, 1);
699 rval++;
702 return rval;
705 STATIC int
706 xfs_btree_readahead_sblock(
707 struct xfs_btree_cur *cur,
708 int lr,
709 struct xfs_btree_block *block)
711 int rval = 0;
712 xfs_agblock_t left = be32_to_cpu(block->bb_u.s.bb_leftsib);
713 xfs_agblock_t right = be32_to_cpu(block->bb_u.s.bb_rightsib);
716 if ((lr & XFS_BTCUR_LEFTRA) && left != NULLAGBLOCK) {
717 xfs_btree_reada_bufs(cur->bc_mp, cur->bc_private.a.agno,
718 left, 1);
719 rval++;
722 if ((lr & XFS_BTCUR_RIGHTRA) && right != NULLAGBLOCK) {
723 xfs_btree_reada_bufs(cur->bc_mp, cur->bc_private.a.agno,
724 right, 1);
725 rval++;
728 return rval;
732 * Read-ahead btree blocks, at the given level.
733 * Bits in lr are set from XFS_BTCUR_{LEFT,RIGHT}RA.
735 STATIC int
736 xfs_btree_readahead(
737 struct xfs_btree_cur *cur, /* btree cursor */
738 int lev, /* level in btree */
739 int lr) /* left/right bits */
741 struct xfs_btree_block *block;
744 * No readahead needed if we are at the root level and the
745 * btree root is stored in the inode.
747 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
748 (lev == cur->bc_nlevels - 1))
749 return 0;
751 if ((cur->bc_ra[lev] | lr) == cur->bc_ra[lev])
752 return 0;
754 cur->bc_ra[lev] |= lr;
755 block = XFS_BUF_TO_BLOCK(cur->bc_bufs[lev]);
757 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
758 return xfs_btree_readahead_lblock(cur, lr, block);
759 return xfs_btree_readahead_sblock(cur, lr, block);
763 * Set the buffer for level "lev" in the cursor to bp, releasing
764 * any previous buffer.
766 STATIC void
767 xfs_btree_setbuf(
768 xfs_btree_cur_t *cur, /* btree cursor */
769 int lev, /* level in btree */
770 xfs_buf_t *bp) /* new buffer to set */
772 struct xfs_btree_block *b; /* btree block */
774 if (cur->bc_bufs[lev])
775 xfs_trans_brelse(cur->bc_tp, cur->bc_bufs[lev]);
776 cur->bc_bufs[lev] = bp;
777 cur->bc_ra[lev] = 0;
779 b = XFS_BUF_TO_BLOCK(bp);
780 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
781 if (be64_to_cpu(b->bb_u.l.bb_leftsib) == NULLDFSBNO)
782 cur->bc_ra[lev] |= XFS_BTCUR_LEFTRA;
783 if (be64_to_cpu(b->bb_u.l.bb_rightsib) == NULLDFSBNO)
784 cur->bc_ra[lev] |= XFS_BTCUR_RIGHTRA;
785 } else {
786 if (be32_to_cpu(b->bb_u.s.bb_leftsib) == NULLAGBLOCK)
787 cur->bc_ra[lev] |= XFS_BTCUR_LEFTRA;
788 if (be32_to_cpu(b->bb_u.s.bb_rightsib) == NULLAGBLOCK)
789 cur->bc_ra[lev] |= XFS_BTCUR_RIGHTRA;
793 STATIC int
794 xfs_btree_ptr_is_null(
795 struct xfs_btree_cur *cur,
796 union xfs_btree_ptr *ptr)
798 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
799 return be64_to_cpu(ptr->l) == NULLDFSBNO;
800 else
801 return be32_to_cpu(ptr->s) == NULLAGBLOCK;
804 STATIC void
805 xfs_btree_set_ptr_null(
806 struct xfs_btree_cur *cur,
807 union xfs_btree_ptr *ptr)
809 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
810 ptr->l = cpu_to_be64(NULLDFSBNO);
811 else
812 ptr->s = cpu_to_be32(NULLAGBLOCK);
816 * Get/set/init sibling pointers
818 STATIC void
819 xfs_btree_get_sibling(
820 struct xfs_btree_cur *cur,
821 struct xfs_btree_block *block,
822 union xfs_btree_ptr *ptr,
823 int lr)
825 ASSERT(lr == XFS_BB_LEFTSIB || lr == XFS_BB_RIGHTSIB);
827 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
828 if (lr == XFS_BB_RIGHTSIB)
829 ptr->l = block->bb_u.l.bb_rightsib;
830 else
831 ptr->l = block->bb_u.l.bb_leftsib;
832 } else {
833 if (lr == XFS_BB_RIGHTSIB)
834 ptr->s = block->bb_u.s.bb_rightsib;
835 else
836 ptr->s = block->bb_u.s.bb_leftsib;
840 STATIC void
841 xfs_btree_set_sibling(
842 struct xfs_btree_cur *cur,
843 struct xfs_btree_block *block,
844 union xfs_btree_ptr *ptr,
845 int lr)
847 ASSERT(lr == XFS_BB_LEFTSIB || lr == XFS_BB_RIGHTSIB);
849 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
850 if (lr == XFS_BB_RIGHTSIB)
851 block->bb_u.l.bb_rightsib = ptr->l;
852 else
853 block->bb_u.l.bb_leftsib = ptr->l;
854 } else {
855 if (lr == XFS_BB_RIGHTSIB)
856 block->bb_u.s.bb_rightsib = ptr->s;
857 else
858 block->bb_u.s.bb_leftsib = ptr->s;
862 STATIC void
863 xfs_btree_init_block(
864 struct xfs_btree_cur *cur,
865 int level,
866 int numrecs,
867 struct xfs_btree_block *new) /* new block */
869 new->bb_magic = cpu_to_be32(xfs_magics[cur->bc_btnum]);
870 new->bb_level = cpu_to_be16(level);
871 new->bb_numrecs = cpu_to_be16(numrecs);
873 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
874 new->bb_u.l.bb_leftsib = cpu_to_be64(NULLDFSBNO);
875 new->bb_u.l.bb_rightsib = cpu_to_be64(NULLDFSBNO);
876 } else {
877 new->bb_u.s.bb_leftsib = cpu_to_be32(NULLAGBLOCK);
878 new->bb_u.s.bb_rightsib = cpu_to_be32(NULLAGBLOCK);
883 * Return true if ptr is the last record in the btree and
884 * we need to track updateѕ to this record. The decision
885 * will be further refined in the update_lastrec method.
887 STATIC int
888 xfs_btree_is_lastrec(
889 struct xfs_btree_cur *cur,
890 struct xfs_btree_block *block,
891 int level)
893 union xfs_btree_ptr ptr;
895 if (level > 0)
896 return 0;
897 if (!(cur->bc_flags & XFS_BTREE_LASTREC_UPDATE))
898 return 0;
900 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_RIGHTSIB);
901 if (!xfs_btree_ptr_is_null(cur, &ptr))
902 return 0;
903 return 1;
906 STATIC void
907 xfs_btree_buf_to_ptr(
908 struct xfs_btree_cur *cur,
909 struct xfs_buf *bp,
910 union xfs_btree_ptr *ptr)
912 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
913 ptr->l = cpu_to_be64(XFS_DADDR_TO_FSB(cur->bc_mp,
914 XFS_BUF_ADDR(bp)));
915 else {
916 ptr->s = cpu_to_be32(xfs_daddr_to_agbno(cur->bc_mp,
917 XFS_BUF_ADDR(bp)));
921 STATIC xfs_daddr_t
922 xfs_btree_ptr_to_daddr(
923 struct xfs_btree_cur *cur,
924 union xfs_btree_ptr *ptr)
926 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
927 ASSERT(be64_to_cpu(ptr->l) != NULLDFSBNO);
929 return XFS_FSB_TO_DADDR(cur->bc_mp, be64_to_cpu(ptr->l));
930 } else {
931 ASSERT(cur->bc_private.a.agno != NULLAGNUMBER);
932 ASSERT(be32_to_cpu(ptr->s) != NULLAGBLOCK);
934 return XFS_AGB_TO_DADDR(cur->bc_mp, cur->bc_private.a.agno,
935 be32_to_cpu(ptr->s));
939 STATIC void
940 xfs_btree_set_refs(
941 struct xfs_btree_cur *cur,
942 struct xfs_buf *bp)
944 switch (cur->bc_btnum) {
945 case XFS_BTNUM_BNO:
946 case XFS_BTNUM_CNT:
947 XFS_BUF_SET_VTYPE_REF(*bpp, B_FS_MAP, XFS_ALLOC_BTREE_REF);
948 break;
949 case XFS_BTNUM_INO:
950 XFS_BUF_SET_VTYPE_REF(*bpp, B_FS_INOMAP, XFS_INO_BTREE_REF);
951 break;
952 case XFS_BTNUM_BMAP:
953 XFS_BUF_SET_VTYPE_REF(*bpp, B_FS_MAP, XFS_BMAP_BTREE_REF);
954 break;
955 default:
956 ASSERT(0);
960 STATIC int
961 xfs_btree_get_buf_block(
962 struct xfs_btree_cur *cur,
963 union xfs_btree_ptr *ptr,
964 int flags,
965 struct xfs_btree_block **block,
966 struct xfs_buf **bpp)
968 struct xfs_mount *mp = cur->bc_mp;
969 xfs_daddr_t d;
971 /* need to sort out how callers deal with failures first */
972 ASSERT(!(flags & XBF_TRYLOCK));
974 d = xfs_btree_ptr_to_daddr(cur, ptr);
975 *bpp = xfs_trans_get_buf(cur->bc_tp, mp->m_ddev_targp, d,
976 mp->m_bsize, flags);
978 ASSERT(*bpp);
979 ASSERT(!XFS_BUF_GETERROR(*bpp));
981 *block = XFS_BUF_TO_BLOCK(*bpp);
982 return 0;
986 * Read in the buffer at the given ptr and return the buffer and
987 * the block pointer within the buffer.
989 STATIC int
990 xfs_btree_read_buf_block(
991 struct xfs_btree_cur *cur,
992 union xfs_btree_ptr *ptr,
993 int level,
994 int flags,
995 struct xfs_btree_block **block,
996 struct xfs_buf **bpp)
998 struct xfs_mount *mp = cur->bc_mp;
999 xfs_daddr_t d;
1000 int error;
1002 /* need to sort out how callers deal with failures first */
1003 ASSERT(!(flags & XBF_TRYLOCK));
1005 d = xfs_btree_ptr_to_daddr(cur, ptr);
1006 error = xfs_trans_read_buf(mp, cur->bc_tp, mp->m_ddev_targp, d,
1007 mp->m_bsize, flags, bpp);
1008 if (error)
1009 return error;
1011 ASSERT(*bpp != NULL);
1012 ASSERT(!XFS_BUF_GETERROR(*bpp));
1014 xfs_btree_set_refs(cur, *bpp);
1015 *block = XFS_BUF_TO_BLOCK(*bpp);
1017 error = xfs_btree_check_block(cur, *block, level, *bpp);
1018 if (error)
1019 xfs_trans_brelse(cur->bc_tp, *bpp);
1020 return error;
1024 * Copy keys from one btree block to another.
1026 STATIC void
1027 xfs_btree_copy_keys(
1028 struct xfs_btree_cur *cur,
1029 union xfs_btree_key *dst_key,
1030 union xfs_btree_key *src_key,
1031 int numkeys)
1033 ASSERT(numkeys >= 0);
1034 memcpy(dst_key, src_key, numkeys * cur->bc_ops->key_len);
1038 * Copy records from one btree block to another.
1040 STATIC void
1041 xfs_btree_copy_recs(
1042 struct xfs_btree_cur *cur,
1043 union xfs_btree_rec *dst_rec,
1044 union xfs_btree_rec *src_rec,
1045 int numrecs)
1047 ASSERT(numrecs >= 0);
1048 memcpy(dst_rec, src_rec, numrecs * cur->bc_ops->rec_len);
1052 * Copy block pointers from one btree block to another.
1054 STATIC void
1055 xfs_btree_copy_ptrs(
1056 struct xfs_btree_cur *cur,
1057 union xfs_btree_ptr *dst_ptr,
1058 union xfs_btree_ptr *src_ptr,
1059 int numptrs)
1061 ASSERT(numptrs >= 0);
1062 memcpy(dst_ptr, src_ptr, numptrs * xfs_btree_ptr_len(cur));
1066 * Shift keys one index left/right inside a single btree block.
1068 STATIC void
1069 xfs_btree_shift_keys(
1070 struct xfs_btree_cur *cur,
1071 union xfs_btree_key *key,
1072 int dir,
1073 int numkeys)
1075 char *dst_key;
1077 ASSERT(numkeys >= 0);
1078 ASSERT(dir == 1 || dir == -1);
1080 dst_key = (char *)key + (dir * cur->bc_ops->key_len);
1081 memmove(dst_key, key, numkeys * cur->bc_ops->key_len);
1085 * Shift records one index left/right inside a single btree block.
1087 STATIC void
1088 xfs_btree_shift_recs(
1089 struct xfs_btree_cur *cur,
1090 union xfs_btree_rec *rec,
1091 int dir,
1092 int numrecs)
1094 char *dst_rec;
1096 ASSERT(numrecs >= 0);
1097 ASSERT(dir == 1 || dir == -1);
1099 dst_rec = (char *)rec + (dir * cur->bc_ops->rec_len);
1100 memmove(dst_rec, rec, numrecs * cur->bc_ops->rec_len);
1104 * Shift block pointers one index left/right inside a single btree block.
1106 STATIC void
1107 xfs_btree_shift_ptrs(
1108 struct xfs_btree_cur *cur,
1109 union xfs_btree_ptr *ptr,
1110 int dir,
1111 int numptrs)
1113 char *dst_ptr;
1115 ASSERT(numptrs >= 0);
1116 ASSERT(dir == 1 || dir == -1);
1118 dst_ptr = (char *)ptr + (dir * xfs_btree_ptr_len(cur));
1119 memmove(dst_ptr, ptr, numptrs * xfs_btree_ptr_len(cur));
1123 * Log key values from the btree block.
1125 STATIC void
1126 xfs_btree_log_keys(
1127 struct xfs_btree_cur *cur,
1128 struct xfs_buf *bp,
1129 int first,
1130 int last)
1132 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1133 XFS_BTREE_TRACE_ARGBII(cur, bp, first, last);
1135 if (bp) {
1136 xfs_trans_log_buf(cur->bc_tp, bp,
1137 xfs_btree_key_offset(cur, first),
1138 xfs_btree_key_offset(cur, last + 1) - 1);
1139 } else {
1140 xfs_trans_log_inode(cur->bc_tp, cur->bc_private.b.ip,
1141 xfs_ilog_fbroot(cur->bc_private.b.whichfork));
1144 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1148 * Log record values from the btree block.
1150 void
1151 xfs_btree_log_recs(
1152 struct xfs_btree_cur *cur,
1153 struct xfs_buf *bp,
1154 int first,
1155 int last)
1157 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1158 XFS_BTREE_TRACE_ARGBII(cur, bp, first, last);
1160 xfs_trans_log_buf(cur->bc_tp, bp,
1161 xfs_btree_rec_offset(cur, first),
1162 xfs_btree_rec_offset(cur, last + 1) - 1);
1164 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1168 * Log block pointer fields from a btree block (nonleaf).
1170 STATIC void
1171 xfs_btree_log_ptrs(
1172 struct xfs_btree_cur *cur, /* btree cursor */
1173 struct xfs_buf *bp, /* buffer containing btree block */
1174 int first, /* index of first pointer to log */
1175 int last) /* index of last pointer to log */
1177 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1178 XFS_BTREE_TRACE_ARGBII(cur, bp, first, last);
1180 if (bp) {
1181 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
1182 int level = xfs_btree_get_level(block);
1184 xfs_trans_log_buf(cur->bc_tp, bp,
1185 xfs_btree_ptr_offset(cur, first, level),
1186 xfs_btree_ptr_offset(cur, last + 1, level) - 1);
1187 } else {
1188 xfs_trans_log_inode(cur->bc_tp, cur->bc_private.b.ip,
1189 xfs_ilog_fbroot(cur->bc_private.b.whichfork));
1192 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1196 * Log fields from a btree block header.
1198 void
1199 xfs_btree_log_block(
1200 struct xfs_btree_cur *cur, /* btree cursor */
1201 struct xfs_buf *bp, /* buffer containing btree block */
1202 int fields) /* mask of fields: XFS_BB_... */
1204 int first; /* first byte offset logged */
1205 int last; /* last byte offset logged */
1206 static const short soffsets[] = { /* table of offsets (short) */
1207 offsetof(struct xfs_btree_block, bb_magic),
1208 offsetof(struct xfs_btree_block, bb_level),
1209 offsetof(struct xfs_btree_block, bb_numrecs),
1210 offsetof(struct xfs_btree_block, bb_u.s.bb_leftsib),
1211 offsetof(struct xfs_btree_block, bb_u.s.bb_rightsib),
1212 XFS_BTREE_SBLOCK_LEN
1214 static const short loffsets[] = { /* table of offsets (long) */
1215 offsetof(struct xfs_btree_block, bb_magic),
1216 offsetof(struct xfs_btree_block, bb_level),
1217 offsetof(struct xfs_btree_block, bb_numrecs),
1218 offsetof(struct xfs_btree_block, bb_u.l.bb_leftsib),
1219 offsetof(struct xfs_btree_block, bb_u.l.bb_rightsib),
1220 XFS_BTREE_LBLOCK_LEN
1223 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1224 XFS_BTREE_TRACE_ARGBI(cur, bp, fields);
1226 if (bp) {
1227 xfs_btree_offsets(fields,
1228 (cur->bc_flags & XFS_BTREE_LONG_PTRS) ?
1229 loffsets : soffsets,
1230 XFS_BB_NUM_BITS, &first, &last);
1231 xfs_trans_log_buf(cur->bc_tp, bp, first, last);
1232 } else {
1233 xfs_trans_log_inode(cur->bc_tp, cur->bc_private.b.ip,
1234 xfs_ilog_fbroot(cur->bc_private.b.whichfork));
1237 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1241 * Increment cursor by one record at the level.
1242 * For nonzero levels the leaf-ward information is untouched.
1244 int /* error */
1245 xfs_btree_increment(
1246 struct xfs_btree_cur *cur,
1247 int level,
1248 int *stat) /* success/failure */
1250 struct xfs_btree_block *block;
1251 union xfs_btree_ptr ptr;
1252 struct xfs_buf *bp;
1253 int error; /* error return value */
1254 int lev;
1256 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1257 XFS_BTREE_TRACE_ARGI(cur, level);
1259 ASSERT(level < cur->bc_nlevels);
1261 /* Read-ahead to the right at this level. */
1262 xfs_btree_readahead(cur, level, XFS_BTCUR_RIGHTRA);
1264 /* Get a pointer to the btree block. */
1265 block = xfs_btree_get_block(cur, level, &bp);
1267 #ifdef DEBUG
1268 error = xfs_btree_check_block(cur, block, level, bp);
1269 if (error)
1270 goto error0;
1271 #endif
1273 /* We're done if we remain in the block after the increment. */
1274 if (++cur->bc_ptrs[level] <= xfs_btree_get_numrecs(block))
1275 goto out1;
1277 /* Fail if we just went off the right edge of the tree. */
1278 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_RIGHTSIB);
1279 if (xfs_btree_ptr_is_null(cur, &ptr))
1280 goto out0;
1282 XFS_BTREE_STATS_INC(cur, increment);
1285 * March up the tree incrementing pointers.
1286 * Stop when we don't go off the right edge of a block.
1288 for (lev = level + 1; lev < cur->bc_nlevels; lev++) {
1289 block = xfs_btree_get_block(cur, lev, &bp);
1291 #ifdef DEBUG
1292 error = xfs_btree_check_block(cur, block, lev, bp);
1293 if (error)
1294 goto error0;
1295 #endif
1297 if (++cur->bc_ptrs[lev] <= xfs_btree_get_numrecs(block))
1298 break;
1300 /* Read-ahead the right block for the next loop. */
1301 xfs_btree_readahead(cur, lev, XFS_BTCUR_RIGHTRA);
1305 * If we went off the root then we are either seriously
1306 * confused or have the tree root in an inode.
1308 if (lev == cur->bc_nlevels) {
1309 if (cur->bc_flags & XFS_BTREE_ROOT_IN_INODE)
1310 goto out0;
1311 ASSERT(0);
1312 error = EFSCORRUPTED;
1313 goto error0;
1315 ASSERT(lev < cur->bc_nlevels);
1318 * Now walk back down the tree, fixing up the cursor's buffer
1319 * pointers and key numbers.
1321 for (block = xfs_btree_get_block(cur, lev, &bp); lev > level; ) {
1322 union xfs_btree_ptr *ptrp;
1324 ptrp = xfs_btree_ptr_addr(cur, cur->bc_ptrs[lev], block);
1325 error = xfs_btree_read_buf_block(cur, ptrp, --lev,
1326 0, &block, &bp);
1327 if (error)
1328 goto error0;
1330 xfs_btree_setbuf(cur, lev, bp);
1331 cur->bc_ptrs[lev] = 1;
1333 out1:
1334 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1335 *stat = 1;
1336 return 0;
1338 out0:
1339 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1340 *stat = 0;
1341 return 0;
1343 error0:
1344 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
1345 return error;
1349 * Decrement cursor by one record at the level.
1350 * For nonzero levels the leaf-ward information is untouched.
1352 int /* error */
1353 xfs_btree_decrement(
1354 struct xfs_btree_cur *cur,
1355 int level,
1356 int *stat) /* success/failure */
1358 struct xfs_btree_block *block;
1359 xfs_buf_t *bp;
1360 int error; /* error return value */
1361 int lev;
1362 union xfs_btree_ptr ptr;
1364 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1365 XFS_BTREE_TRACE_ARGI(cur, level);
1367 ASSERT(level < cur->bc_nlevels);
1369 /* Read-ahead to the left at this level. */
1370 xfs_btree_readahead(cur, level, XFS_BTCUR_LEFTRA);
1372 /* We're done if we remain in the block after the decrement. */
1373 if (--cur->bc_ptrs[level] > 0)
1374 goto out1;
1376 /* Get a pointer to the btree block. */
1377 block = xfs_btree_get_block(cur, level, &bp);
1379 #ifdef DEBUG
1380 error = xfs_btree_check_block(cur, block, level, bp);
1381 if (error)
1382 goto error0;
1383 #endif
1385 /* Fail if we just went off the left edge of the tree. */
1386 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_LEFTSIB);
1387 if (xfs_btree_ptr_is_null(cur, &ptr))
1388 goto out0;
1390 XFS_BTREE_STATS_INC(cur, decrement);
1393 * March up the tree decrementing pointers.
1394 * Stop when we don't go off the left edge of a block.
1396 for (lev = level + 1; lev < cur->bc_nlevels; lev++) {
1397 if (--cur->bc_ptrs[lev] > 0)
1398 break;
1399 /* Read-ahead the left block for the next loop. */
1400 xfs_btree_readahead(cur, lev, XFS_BTCUR_LEFTRA);
1404 * If we went off the root then we are seriously confused.
1405 * or the root of the tree is in an inode.
1407 if (lev == cur->bc_nlevels) {
1408 if (cur->bc_flags & XFS_BTREE_ROOT_IN_INODE)
1409 goto out0;
1410 ASSERT(0);
1411 error = EFSCORRUPTED;
1412 goto error0;
1414 ASSERT(lev < cur->bc_nlevels);
1417 * Now walk back down the tree, fixing up the cursor's buffer
1418 * pointers and key numbers.
1420 for (block = xfs_btree_get_block(cur, lev, &bp); lev > level; ) {
1421 union xfs_btree_ptr *ptrp;
1423 ptrp = xfs_btree_ptr_addr(cur, cur->bc_ptrs[lev], block);
1424 error = xfs_btree_read_buf_block(cur, ptrp, --lev,
1425 0, &block, &bp);
1426 if (error)
1427 goto error0;
1428 xfs_btree_setbuf(cur, lev, bp);
1429 cur->bc_ptrs[lev] = xfs_btree_get_numrecs(block);
1431 out1:
1432 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1433 *stat = 1;
1434 return 0;
1436 out0:
1437 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1438 *stat = 0;
1439 return 0;
1441 error0:
1442 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
1443 return error;
1446 STATIC int
1447 xfs_btree_lookup_get_block(
1448 struct xfs_btree_cur *cur, /* btree cursor */
1449 int level, /* level in the btree */
1450 union xfs_btree_ptr *pp, /* ptr to btree block */
1451 struct xfs_btree_block **blkp) /* return btree block */
1453 struct xfs_buf *bp; /* buffer pointer for btree block */
1454 int error = 0;
1456 /* special case the root block if in an inode */
1457 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
1458 (level == cur->bc_nlevels - 1)) {
1459 *blkp = xfs_btree_get_iroot(cur);
1460 return 0;
1464 * If the old buffer at this level for the disk address we are
1465 * looking for re-use it.
1467 * Otherwise throw it away and get a new one.
1469 bp = cur->bc_bufs[level];
1470 if (bp && XFS_BUF_ADDR(bp) == xfs_btree_ptr_to_daddr(cur, pp)) {
1471 *blkp = XFS_BUF_TO_BLOCK(bp);
1472 return 0;
1475 error = xfs_btree_read_buf_block(cur, pp, level, 0, blkp, &bp);
1476 if (error)
1477 return error;
1479 xfs_btree_setbuf(cur, level, bp);
1480 return 0;
1484 * Get current search key. For level 0 we don't actually have a key
1485 * structure so we make one up from the record. For all other levels
1486 * we just return the right key.
1488 STATIC union xfs_btree_key *
1489 xfs_lookup_get_search_key(
1490 struct xfs_btree_cur *cur,
1491 int level,
1492 int keyno,
1493 struct xfs_btree_block *block,
1494 union xfs_btree_key *kp)
1496 if (level == 0) {
1497 cur->bc_ops->init_key_from_rec(kp,
1498 xfs_btree_rec_addr(cur, keyno, block));
1499 return kp;
1502 return xfs_btree_key_addr(cur, keyno, block);
1506 * Lookup the record. The cursor is made to point to it, based on dir.
1507 * Return 0 if can't find any such record, 1 for success.
1509 int /* error */
1510 xfs_btree_lookup(
1511 struct xfs_btree_cur *cur, /* btree cursor */
1512 xfs_lookup_t dir, /* <=, ==, or >= */
1513 int *stat) /* success/failure */
1515 struct xfs_btree_block *block; /* current btree block */
1516 __int64_t diff; /* difference for the current key */
1517 int error; /* error return value */
1518 int keyno; /* current key number */
1519 int level; /* level in the btree */
1520 union xfs_btree_ptr *pp; /* ptr to btree block */
1521 union xfs_btree_ptr ptr; /* ptr to btree block */
1523 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1524 XFS_BTREE_TRACE_ARGI(cur, dir);
1526 XFS_BTREE_STATS_INC(cur, lookup);
1528 block = NULL;
1529 keyno = 0;
1531 /* initialise start pointer from cursor */
1532 cur->bc_ops->init_ptr_from_cur(cur, &ptr);
1533 pp = &ptr;
1536 * Iterate over each level in the btree, starting at the root.
1537 * For each level above the leaves, find the key we need, based
1538 * on the lookup record, then follow the corresponding block
1539 * pointer down to the next level.
1541 for (level = cur->bc_nlevels - 1, diff = 1; level >= 0; level--) {
1542 /* Get the block we need to do the lookup on. */
1543 error = xfs_btree_lookup_get_block(cur, level, pp, &block);
1544 if (error)
1545 goto error0;
1547 if (diff == 0) {
1549 * If we already had a key match at a higher level, we
1550 * know we need to use the first entry in this block.
1552 keyno = 1;
1553 } else {
1554 /* Otherwise search this block. Do a binary search. */
1556 int high; /* high entry number */
1557 int low; /* low entry number */
1559 /* Set low and high entry numbers, 1-based. */
1560 low = 1;
1561 high = xfs_btree_get_numrecs(block);
1562 if (!high) {
1563 /* Block is empty, must be an empty leaf. */
1564 ASSERT(level == 0 && cur->bc_nlevels == 1);
1566 cur->bc_ptrs[0] = dir != XFS_LOOKUP_LE;
1567 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1568 *stat = 0;
1569 return 0;
1572 /* Binary search the block. */
1573 while (low <= high) {
1574 union xfs_btree_key key;
1575 union xfs_btree_key *kp;
1577 XFS_BTREE_STATS_INC(cur, compare);
1579 /* keyno is average of low and high. */
1580 keyno = (low + high) >> 1;
1582 /* Get current search key */
1583 kp = xfs_lookup_get_search_key(cur, level,
1584 keyno, block, &key);
1587 * Compute difference to get next direction:
1588 * - less than, move right
1589 * - greater than, move left
1590 * - equal, we're done
1592 diff = cur->bc_ops->key_diff(cur, kp);
1593 if (diff < 0)
1594 low = keyno + 1;
1595 else if (diff > 0)
1596 high = keyno - 1;
1597 else
1598 break;
1603 * If there are more levels, set up for the next level
1604 * by getting the block number and filling in the cursor.
1606 if (level > 0) {
1608 * If we moved left, need the previous key number,
1609 * unless there isn't one.
1611 if (diff > 0 && --keyno < 1)
1612 keyno = 1;
1613 pp = xfs_btree_ptr_addr(cur, keyno, block);
1615 #ifdef DEBUG
1616 error = xfs_btree_check_ptr(cur, pp, 0, level);
1617 if (error)
1618 goto error0;
1619 #endif
1620 cur->bc_ptrs[level] = keyno;
1624 /* Done with the search. See if we need to adjust the results. */
1625 if (dir != XFS_LOOKUP_LE && diff < 0) {
1626 keyno++;
1628 * If ge search and we went off the end of the block, but it's
1629 * not the last block, we're in the wrong block.
1631 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_RIGHTSIB);
1632 if (dir == XFS_LOOKUP_GE &&
1633 keyno > xfs_btree_get_numrecs(block) &&
1634 !xfs_btree_ptr_is_null(cur, &ptr)) {
1635 int i;
1637 cur->bc_ptrs[0] = keyno;
1638 error = xfs_btree_increment(cur, 0, &i);
1639 if (error)
1640 goto error0;
1641 XFS_WANT_CORRUPTED_RETURN(i == 1);
1642 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1643 *stat = 1;
1644 return 0;
1646 } else if (dir == XFS_LOOKUP_LE && diff > 0)
1647 keyno--;
1648 cur->bc_ptrs[0] = keyno;
1650 /* Return if we succeeded or not. */
1651 if (keyno == 0 || keyno > xfs_btree_get_numrecs(block))
1652 *stat = 0;
1653 else if (dir != XFS_LOOKUP_EQ || diff == 0)
1654 *stat = 1;
1655 else
1656 *stat = 0;
1657 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1658 return 0;
1660 error0:
1661 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
1662 return error;
1666 * Update keys at all levels from here to the root along the cursor's path.
1668 STATIC int
1669 xfs_btree_updkey(
1670 struct xfs_btree_cur *cur,
1671 union xfs_btree_key *keyp,
1672 int level)
1674 struct xfs_btree_block *block;
1675 struct xfs_buf *bp;
1676 union xfs_btree_key *kp;
1677 int ptr;
1679 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1680 XFS_BTREE_TRACE_ARGIK(cur, level, keyp);
1682 ASSERT(!(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) || level >= 1);
1685 * Go up the tree from this level toward the root.
1686 * At each level, update the key value to the value input.
1687 * Stop when we reach a level where the cursor isn't pointing
1688 * at the first entry in the block.
1690 for (ptr = 1; ptr == 1 && level < cur->bc_nlevels; level++) {
1691 #ifdef DEBUG
1692 int error;
1693 #endif
1694 block = xfs_btree_get_block(cur, level, &bp);
1695 #ifdef DEBUG
1696 error = xfs_btree_check_block(cur, block, level, bp);
1697 if (error) {
1698 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
1699 return error;
1701 #endif
1702 ptr = cur->bc_ptrs[level];
1703 kp = xfs_btree_key_addr(cur, ptr, block);
1704 xfs_btree_copy_keys(cur, kp, keyp, 1);
1705 xfs_btree_log_keys(cur, bp, ptr, ptr);
1708 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1709 return 0;
1713 * Update the record referred to by cur to the value in the
1714 * given record. This either works (return 0) or gets an
1715 * EFSCORRUPTED error.
1718 xfs_btree_update(
1719 struct xfs_btree_cur *cur,
1720 union xfs_btree_rec *rec)
1722 struct xfs_btree_block *block;
1723 struct xfs_buf *bp;
1724 int error;
1725 int ptr;
1726 union xfs_btree_rec *rp;
1728 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1729 XFS_BTREE_TRACE_ARGR(cur, rec);
1731 /* Pick up the current block. */
1732 block = xfs_btree_get_block(cur, 0, &bp);
1734 #ifdef DEBUG
1735 error = xfs_btree_check_block(cur, block, 0, bp);
1736 if (error)
1737 goto error0;
1738 #endif
1739 /* Get the address of the rec to be updated. */
1740 ptr = cur->bc_ptrs[0];
1741 rp = xfs_btree_rec_addr(cur, ptr, block);
1743 /* Fill in the new contents and log them. */
1744 xfs_btree_copy_recs(cur, rp, rec, 1);
1745 xfs_btree_log_recs(cur, bp, ptr, ptr);
1748 * If we are tracking the last record in the tree and
1749 * we are at the far right edge of the tree, update it.
1751 if (xfs_btree_is_lastrec(cur, block, 0)) {
1752 cur->bc_ops->update_lastrec(cur, block, rec,
1753 ptr, LASTREC_UPDATE);
1756 /* Updating first rec in leaf. Pass new key value up to our parent. */
1757 if (ptr == 1) {
1758 union xfs_btree_key key;
1760 cur->bc_ops->init_key_from_rec(&key, rec);
1761 error = xfs_btree_updkey(cur, &key, 1);
1762 if (error)
1763 goto error0;
1766 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1767 return 0;
1769 error0:
1770 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
1771 return error;
1775 * Move 1 record left from cur/level if possible.
1776 * Update cur to reflect the new path.
1778 STATIC int /* error */
1779 xfs_btree_lshift(
1780 struct xfs_btree_cur *cur,
1781 int level,
1782 int *stat) /* success/failure */
1784 union xfs_btree_key key; /* btree key */
1785 struct xfs_buf *lbp; /* left buffer pointer */
1786 struct xfs_btree_block *left; /* left btree block */
1787 int lrecs; /* left record count */
1788 struct xfs_buf *rbp; /* right buffer pointer */
1789 struct xfs_btree_block *right; /* right btree block */
1790 int rrecs; /* right record count */
1791 union xfs_btree_ptr lptr; /* left btree pointer */
1792 union xfs_btree_key *rkp = NULL; /* right btree key */
1793 union xfs_btree_ptr *rpp = NULL; /* right address pointer */
1794 union xfs_btree_rec *rrp = NULL; /* right record pointer */
1795 int error; /* error return value */
1797 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1798 XFS_BTREE_TRACE_ARGI(cur, level);
1800 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
1801 level == cur->bc_nlevels - 1)
1802 goto out0;
1804 /* Set up variables for this block as "right". */
1805 right = xfs_btree_get_block(cur, level, &rbp);
1807 #ifdef DEBUG
1808 error = xfs_btree_check_block(cur, right, level, rbp);
1809 if (error)
1810 goto error0;
1811 #endif
1813 /* If we've got no left sibling then we can't shift an entry left. */
1814 xfs_btree_get_sibling(cur, right, &lptr, XFS_BB_LEFTSIB);
1815 if (xfs_btree_ptr_is_null(cur, &lptr))
1816 goto out0;
1819 * If the cursor entry is the one that would be moved, don't
1820 * do it... it's too complicated.
1822 if (cur->bc_ptrs[level] <= 1)
1823 goto out0;
1825 /* Set up the left neighbor as "left". */
1826 error = xfs_btree_read_buf_block(cur, &lptr, level, 0, &left, &lbp);
1827 if (error)
1828 goto error0;
1830 /* If it's full, it can't take another entry. */
1831 lrecs = xfs_btree_get_numrecs(left);
1832 if (lrecs == cur->bc_ops->get_maxrecs(cur, level))
1833 goto out0;
1835 rrecs = xfs_btree_get_numrecs(right);
1838 * We add one entry to the left side and remove one for the right side.
1839 * Account for it here, the changes will be updated on disk and logged
1840 * later.
1842 lrecs++;
1843 rrecs--;
1845 XFS_BTREE_STATS_INC(cur, lshift);
1846 XFS_BTREE_STATS_ADD(cur, moves, 1);
1849 * If non-leaf, copy a key and a ptr to the left block.
1850 * Log the changes to the left block.
1852 if (level > 0) {
1853 /* It's a non-leaf. Move keys and pointers. */
1854 union xfs_btree_key *lkp; /* left btree key */
1855 union xfs_btree_ptr *lpp; /* left address pointer */
1857 lkp = xfs_btree_key_addr(cur, lrecs, left);
1858 rkp = xfs_btree_key_addr(cur, 1, right);
1860 lpp = xfs_btree_ptr_addr(cur, lrecs, left);
1861 rpp = xfs_btree_ptr_addr(cur, 1, right);
1862 #ifdef DEBUG
1863 error = xfs_btree_check_ptr(cur, rpp, 0, level);
1864 if (error)
1865 goto error0;
1866 #endif
1867 xfs_btree_copy_keys(cur, lkp, rkp, 1);
1868 xfs_btree_copy_ptrs(cur, lpp, rpp, 1);
1870 xfs_btree_log_keys(cur, lbp, lrecs, lrecs);
1871 xfs_btree_log_ptrs(cur, lbp, lrecs, lrecs);
1873 ASSERT(cur->bc_ops->keys_inorder(cur,
1874 xfs_btree_key_addr(cur, lrecs - 1, left), lkp));
1875 } else {
1876 /* It's a leaf. Move records. */
1877 union xfs_btree_rec *lrp; /* left record pointer */
1879 lrp = xfs_btree_rec_addr(cur, lrecs, left);
1880 rrp = xfs_btree_rec_addr(cur, 1, right);
1882 xfs_btree_copy_recs(cur, lrp, rrp, 1);
1883 xfs_btree_log_recs(cur, lbp, lrecs, lrecs);
1885 ASSERT(cur->bc_ops->recs_inorder(cur,
1886 xfs_btree_rec_addr(cur, lrecs - 1, left), lrp));
1889 xfs_btree_set_numrecs(left, lrecs);
1890 xfs_btree_log_block(cur, lbp, XFS_BB_NUMRECS);
1892 xfs_btree_set_numrecs(right, rrecs);
1893 xfs_btree_log_block(cur, rbp, XFS_BB_NUMRECS);
1896 * Slide the contents of right down one entry.
1898 XFS_BTREE_STATS_ADD(cur, moves, rrecs - 1);
1899 if (level > 0) {
1900 /* It's a nonleaf. operate on keys and ptrs */
1901 #ifdef DEBUG
1902 int i; /* loop index */
1904 for (i = 0; i < rrecs; i++) {
1905 error = xfs_btree_check_ptr(cur, rpp, i + 1, level);
1906 if (error)
1907 goto error0;
1909 #endif
1910 xfs_btree_shift_keys(cur,
1911 xfs_btree_key_addr(cur, 2, right),
1912 -1, rrecs);
1913 xfs_btree_shift_ptrs(cur,
1914 xfs_btree_ptr_addr(cur, 2, right),
1915 -1, rrecs);
1917 xfs_btree_log_keys(cur, rbp, 1, rrecs);
1918 xfs_btree_log_ptrs(cur, rbp, 1, rrecs);
1919 } else {
1920 /* It's a leaf. operate on records */
1921 xfs_btree_shift_recs(cur,
1922 xfs_btree_rec_addr(cur, 2, right),
1923 -1, rrecs);
1924 xfs_btree_log_recs(cur, rbp, 1, rrecs);
1927 * If it's the first record in the block, we'll need a key
1928 * structure to pass up to the next level (updkey).
1930 cur->bc_ops->init_key_from_rec(&key,
1931 xfs_btree_rec_addr(cur, 1, right));
1932 rkp = &key;
1935 /* Update the parent key values of right. */
1936 error = xfs_btree_updkey(cur, rkp, level + 1);
1937 if (error)
1938 goto error0;
1940 /* Slide the cursor value left one. */
1941 cur->bc_ptrs[level]--;
1943 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1944 *stat = 1;
1945 return 0;
1947 out0:
1948 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1949 *stat = 0;
1950 return 0;
1952 error0:
1953 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
1954 return error;
1958 * Move 1 record right from cur/level if possible.
1959 * Update cur to reflect the new path.
1961 STATIC int /* error */
1962 xfs_btree_rshift(
1963 struct xfs_btree_cur *cur,
1964 int level,
1965 int *stat) /* success/failure */
1967 union xfs_btree_key key; /* btree key */
1968 struct xfs_buf *lbp; /* left buffer pointer */
1969 struct xfs_btree_block *left; /* left btree block */
1970 struct xfs_buf *rbp; /* right buffer pointer */
1971 struct xfs_btree_block *right; /* right btree block */
1972 struct xfs_btree_cur *tcur; /* temporary btree cursor */
1973 union xfs_btree_ptr rptr; /* right block pointer */
1974 union xfs_btree_key *rkp; /* right btree key */
1975 int rrecs; /* right record count */
1976 int lrecs; /* left record count */
1977 int error; /* error return value */
1978 int i; /* loop counter */
1980 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1981 XFS_BTREE_TRACE_ARGI(cur, level);
1983 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
1984 (level == cur->bc_nlevels - 1))
1985 goto out0;
1987 /* Set up variables for this block as "left". */
1988 left = xfs_btree_get_block(cur, level, &lbp);
1990 #ifdef DEBUG
1991 error = xfs_btree_check_block(cur, left, level, lbp);
1992 if (error)
1993 goto error0;
1994 #endif
1996 /* If we've got no right sibling then we can't shift an entry right. */
1997 xfs_btree_get_sibling(cur, left, &rptr, XFS_BB_RIGHTSIB);
1998 if (xfs_btree_ptr_is_null(cur, &rptr))
1999 goto out0;
2002 * If the cursor entry is the one that would be moved, don't
2003 * do it... it's too complicated.
2005 lrecs = xfs_btree_get_numrecs(left);
2006 if (cur->bc_ptrs[level] >= lrecs)
2007 goto out0;
2009 /* Set up the right neighbor as "right". */
2010 error = xfs_btree_read_buf_block(cur, &rptr, level, 0, &right, &rbp);
2011 if (error)
2012 goto error0;
2014 /* If it's full, it can't take another entry. */
2015 rrecs = xfs_btree_get_numrecs(right);
2016 if (rrecs == cur->bc_ops->get_maxrecs(cur, level))
2017 goto out0;
2019 XFS_BTREE_STATS_INC(cur, rshift);
2020 XFS_BTREE_STATS_ADD(cur, moves, rrecs);
2023 * Make a hole at the start of the right neighbor block, then
2024 * copy the last left block entry to the hole.
2026 if (level > 0) {
2027 /* It's a nonleaf. make a hole in the keys and ptrs */
2028 union xfs_btree_key *lkp;
2029 union xfs_btree_ptr *lpp;
2030 union xfs_btree_ptr *rpp;
2032 lkp = xfs_btree_key_addr(cur, lrecs, left);
2033 lpp = xfs_btree_ptr_addr(cur, lrecs, left);
2034 rkp = xfs_btree_key_addr(cur, 1, right);
2035 rpp = xfs_btree_ptr_addr(cur, 1, right);
2037 #ifdef DEBUG
2038 for (i = rrecs - 1; i >= 0; i--) {
2039 error = xfs_btree_check_ptr(cur, rpp, i, level);
2040 if (error)
2041 goto error0;
2043 #endif
2045 xfs_btree_shift_keys(cur, rkp, 1, rrecs);
2046 xfs_btree_shift_ptrs(cur, rpp, 1, rrecs);
2048 #ifdef DEBUG
2049 error = xfs_btree_check_ptr(cur, lpp, 0, level);
2050 if (error)
2051 goto error0;
2052 #endif
2054 /* Now put the new data in, and log it. */
2055 xfs_btree_copy_keys(cur, rkp, lkp, 1);
2056 xfs_btree_copy_ptrs(cur, rpp, lpp, 1);
2058 xfs_btree_log_keys(cur, rbp, 1, rrecs + 1);
2059 xfs_btree_log_ptrs(cur, rbp, 1, rrecs + 1);
2061 ASSERT(cur->bc_ops->keys_inorder(cur, rkp,
2062 xfs_btree_key_addr(cur, 2, right)));
2063 } else {
2064 /* It's a leaf. make a hole in the records */
2065 union xfs_btree_rec *lrp;
2066 union xfs_btree_rec *rrp;
2068 lrp = xfs_btree_rec_addr(cur, lrecs, left);
2069 rrp = xfs_btree_rec_addr(cur, 1, right);
2071 xfs_btree_shift_recs(cur, rrp, 1, rrecs);
2073 /* Now put the new data in, and log it. */
2074 xfs_btree_copy_recs(cur, rrp, lrp, 1);
2075 xfs_btree_log_recs(cur, rbp, 1, rrecs + 1);
2077 cur->bc_ops->init_key_from_rec(&key, rrp);
2078 rkp = &key;
2080 ASSERT(cur->bc_ops->recs_inorder(cur, rrp,
2081 xfs_btree_rec_addr(cur, 2, right)));
2085 * Decrement and log left's numrecs, bump and log right's numrecs.
2087 xfs_btree_set_numrecs(left, --lrecs);
2088 xfs_btree_log_block(cur, lbp, XFS_BB_NUMRECS);
2090 xfs_btree_set_numrecs(right, ++rrecs);
2091 xfs_btree_log_block(cur, rbp, XFS_BB_NUMRECS);
2094 * Using a temporary cursor, update the parent key values of the
2095 * block on the right.
2097 error = xfs_btree_dup_cursor(cur, &tcur);
2098 if (error)
2099 goto error0;
2100 i = xfs_btree_lastrec(tcur, level);
2101 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
2103 error = xfs_btree_increment(tcur, level, &i);
2104 if (error)
2105 goto error1;
2107 error = xfs_btree_updkey(tcur, rkp, level + 1);
2108 if (error)
2109 goto error1;
2111 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
2113 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2114 *stat = 1;
2115 return 0;
2117 out0:
2118 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2119 *stat = 0;
2120 return 0;
2122 error0:
2123 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
2124 return error;
2126 error1:
2127 XFS_BTREE_TRACE_CURSOR(tcur, XBT_ERROR);
2128 xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
2129 return error;
2133 * Split cur/level block in half.
2134 * Return new block number and the key to its first
2135 * record (to be inserted into parent).
2137 STATIC int /* error */
2138 xfs_btree_split(
2139 struct xfs_btree_cur *cur,
2140 int level,
2141 union xfs_btree_ptr *ptrp,
2142 union xfs_btree_key *key,
2143 struct xfs_btree_cur **curp,
2144 int *stat) /* success/failure */
2146 union xfs_btree_ptr lptr; /* left sibling block ptr */
2147 struct xfs_buf *lbp; /* left buffer pointer */
2148 struct xfs_btree_block *left; /* left btree block */
2149 union xfs_btree_ptr rptr; /* right sibling block ptr */
2150 struct xfs_buf *rbp; /* right buffer pointer */
2151 struct xfs_btree_block *right; /* right btree block */
2152 union xfs_btree_ptr rrptr; /* right-right sibling ptr */
2153 struct xfs_buf *rrbp; /* right-right buffer pointer */
2154 struct xfs_btree_block *rrblock; /* right-right btree block */
2155 int lrecs;
2156 int rrecs;
2157 int src_index;
2158 int error; /* error return value */
2159 #ifdef DEBUG
2160 int i;
2161 #endif
2163 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
2164 XFS_BTREE_TRACE_ARGIPK(cur, level, *ptrp, key);
2166 XFS_BTREE_STATS_INC(cur, split);
2168 /* Set up left block (current one). */
2169 left = xfs_btree_get_block(cur, level, &lbp);
2171 #ifdef DEBUG
2172 error = xfs_btree_check_block(cur, left, level, lbp);
2173 if (error)
2174 goto error0;
2175 #endif
2177 xfs_btree_buf_to_ptr(cur, lbp, &lptr);
2179 /* Allocate the new block. If we can't do it, we're toast. Give up. */
2180 error = cur->bc_ops->alloc_block(cur, &lptr, &rptr, 1, stat);
2181 if (error)
2182 goto error0;
2183 if (*stat == 0)
2184 goto out0;
2185 XFS_BTREE_STATS_INC(cur, alloc);
2187 /* Set up the new block as "right". */
2188 error = xfs_btree_get_buf_block(cur, &rptr, 0, &right, &rbp);
2189 if (error)
2190 goto error0;
2192 /* Fill in the btree header for the new right block. */
2193 xfs_btree_init_block(cur, xfs_btree_get_level(left), 0, right);
2196 * Split the entries between the old and the new block evenly.
2197 * Make sure that if there's an odd number of entries now, that
2198 * each new block will have the same number of entries.
2200 lrecs = xfs_btree_get_numrecs(left);
2201 rrecs = lrecs / 2;
2202 if ((lrecs & 1) && cur->bc_ptrs[level] <= rrecs + 1)
2203 rrecs++;
2204 src_index = (lrecs - rrecs + 1);
2206 XFS_BTREE_STATS_ADD(cur, moves, rrecs);
2209 * Copy btree block entries from the left block over to the
2210 * new block, the right. Update the right block and log the
2211 * changes.
2213 if (level > 0) {
2214 /* It's a non-leaf. Move keys and pointers. */
2215 union xfs_btree_key *lkp; /* left btree key */
2216 union xfs_btree_ptr *lpp; /* left address pointer */
2217 union xfs_btree_key *rkp; /* right btree key */
2218 union xfs_btree_ptr *rpp; /* right address pointer */
2220 lkp = xfs_btree_key_addr(cur, src_index, left);
2221 lpp = xfs_btree_ptr_addr(cur, src_index, left);
2222 rkp = xfs_btree_key_addr(cur, 1, right);
2223 rpp = xfs_btree_ptr_addr(cur, 1, right);
2225 #ifdef DEBUG
2226 for (i = src_index; i < rrecs; i++) {
2227 error = xfs_btree_check_ptr(cur, lpp, i, level);
2228 if (error)
2229 goto error0;
2231 #endif
2233 xfs_btree_copy_keys(cur, rkp, lkp, rrecs);
2234 xfs_btree_copy_ptrs(cur, rpp, lpp, rrecs);
2236 xfs_btree_log_keys(cur, rbp, 1, rrecs);
2237 xfs_btree_log_ptrs(cur, rbp, 1, rrecs);
2239 /* Grab the keys to the entries moved to the right block */
2240 xfs_btree_copy_keys(cur, key, rkp, 1);
2241 } else {
2242 /* It's a leaf. Move records. */
2243 union xfs_btree_rec *lrp; /* left record pointer */
2244 union xfs_btree_rec *rrp; /* right record pointer */
2246 lrp = xfs_btree_rec_addr(cur, src_index, left);
2247 rrp = xfs_btree_rec_addr(cur, 1, right);
2249 xfs_btree_copy_recs(cur, rrp, lrp, rrecs);
2250 xfs_btree_log_recs(cur, rbp, 1, rrecs);
2252 cur->bc_ops->init_key_from_rec(key,
2253 xfs_btree_rec_addr(cur, 1, right));
2258 * Find the left block number by looking in the buffer.
2259 * Adjust numrecs, sibling pointers.
2261 xfs_btree_get_sibling(cur, left, &rrptr, XFS_BB_RIGHTSIB);
2262 xfs_btree_set_sibling(cur, right, &rrptr, XFS_BB_RIGHTSIB);
2263 xfs_btree_set_sibling(cur, right, &lptr, XFS_BB_LEFTSIB);
2264 xfs_btree_set_sibling(cur, left, &rptr, XFS_BB_RIGHTSIB);
2266 lrecs -= rrecs;
2267 xfs_btree_set_numrecs(left, lrecs);
2268 xfs_btree_set_numrecs(right, xfs_btree_get_numrecs(right) + rrecs);
2270 xfs_btree_log_block(cur, rbp, XFS_BB_ALL_BITS);
2271 xfs_btree_log_block(cur, lbp, XFS_BB_NUMRECS | XFS_BB_RIGHTSIB);
2274 * If there's a block to the new block's right, make that block
2275 * point back to right instead of to left.
2277 if (!xfs_btree_ptr_is_null(cur, &rrptr)) {
2278 error = xfs_btree_read_buf_block(cur, &rrptr, level,
2279 0, &rrblock, &rrbp);
2280 if (error)
2281 goto error0;
2282 xfs_btree_set_sibling(cur, rrblock, &rptr, XFS_BB_LEFTSIB);
2283 xfs_btree_log_block(cur, rrbp, XFS_BB_LEFTSIB);
2286 * If the cursor is really in the right block, move it there.
2287 * If it's just pointing past the last entry in left, then we'll
2288 * insert there, so don't change anything in that case.
2290 if (cur->bc_ptrs[level] > lrecs + 1) {
2291 xfs_btree_setbuf(cur, level, rbp);
2292 cur->bc_ptrs[level] -= lrecs;
2295 * If there are more levels, we'll need another cursor which refers
2296 * the right block, no matter where this cursor was.
2298 if (level + 1 < cur->bc_nlevels) {
2299 error = xfs_btree_dup_cursor(cur, curp);
2300 if (error)
2301 goto error0;
2302 (*curp)->bc_ptrs[level + 1]++;
2304 *ptrp = rptr;
2305 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2306 *stat = 1;
2307 return 0;
2308 out0:
2309 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2310 *stat = 0;
2311 return 0;
2313 error0:
2314 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
2315 return error;
2319 * Copy the old inode root contents into a real block and make the
2320 * broot point to it.
2322 int /* error */
2323 xfs_btree_new_iroot(
2324 struct xfs_btree_cur *cur, /* btree cursor */
2325 int *logflags, /* logging flags for inode */
2326 int *stat) /* return status - 0 fail */
2328 struct xfs_buf *cbp; /* buffer for cblock */
2329 struct xfs_btree_block *block; /* btree block */
2330 struct xfs_btree_block *cblock; /* child btree block */
2331 union xfs_btree_key *ckp; /* child key pointer */
2332 union xfs_btree_ptr *cpp; /* child ptr pointer */
2333 union xfs_btree_key *kp; /* pointer to btree key */
2334 union xfs_btree_ptr *pp; /* pointer to block addr */
2335 union xfs_btree_ptr nptr; /* new block addr */
2336 int level; /* btree level */
2337 int error; /* error return code */
2338 #ifdef DEBUG
2339 int i; /* loop counter */
2340 #endif
2342 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
2343 XFS_BTREE_STATS_INC(cur, newroot);
2345 ASSERT(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE);
2347 level = cur->bc_nlevels - 1;
2349 block = xfs_btree_get_iroot(cur);
2350 pp = xfs_btree_ptr_addr(cur, 1, block);
2352 /* Allocate the new block. If we can't do it, we're toast. Give up. */
2353 error = cur->bc_ops->alloc_block(cur, pp, &nptr, 1, stat);
2354 if (error)
2355 goto error0;
2356 if (*stat == 0) {
2357 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2358 return 0;
2360 XFS_BTREE_STATS_INC(cur, alloc);
2362 /* Copy the root into a real block. */
2363 error = xfs_btree_get_buf_block(cur, &nptr, 0, &cblock, &cbp);
2364 if (error)
2365 goto error0;
2367 memcpy(cblock, block, xfs_btree_block_len(cur));
2369 be16_add_cpu(&block->bb_level, 1);
2370 xfs_btree_set_numrecs(block, 1);
2371 cur->bc_nlevels++;
2372 cur->bc_ptrs[level + 1] = 1;
2374 kp = xfs_btree_key_addr(cur, 1, block);
2375 ckp = xfs_btree_key_addr(cur, 1, cblock);
2376 xfs_btree_copy_keys(cur, ckp, kp, xfs_btree_get_numrecs(cblock));
2378 cpp = xfs_btree_ptr_addr(cur, 1, cblock);
2379 #ifdef DEBUG
2380 for (i = 0; i < be16_to_cpu(cblock->bb_numrecs); i++) {
2381 error = xfs_btree_check_ptr(cur, pp, i, level);
2382 if (error)
2383 goto error0;
2385 #endif
2386 xfs_btree_copy_ptrs(cur, cpp, pp, xfs_btree_get_numrecs(cblock));
2388 #ifdef DEBUG
2389 error = xfs_btree_check_ptr(cur, &nptr, 0, level);
2390 if (error)
2391 goto error0;
2392 #endif
2393 xfs_btree_copy_ptrs(cur, pp, &nptr, 1);
2395 xfs_iroot_realloc(cur->bc_private.b.ip,
2396 1 - xfs_btree_get_numrecs(cblock),
2397 cur->bc_private.b.whichfork);
2399 xfs_btree_setbuf(cur, level, cbp);
2402 * Do all this logging at the end so that
2403 * the root is at the right level.
2405 xfs_btree_log_block(cur, cbp, XFS_BB_ALL_BITS);
2406 xfs_btree_log_keys(cur, cbp, 1, be16_to_cpu(cblock->bb_numrecs));
2407 xfs_btree_log_ptrs(cur, cbp, 1, be16_to_cpu(cblock->bb_numrecs));
2409 *logflags |=
2410 XFS_ILOG_CORE | xfs_ilog_fbroot(cur->bc_private.b.whichfork);
2411 *stat = 1;
2412 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2413 return 0;
2414 error0:
2415 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
2416 return error;
2420 * Allocate a new root block, fill it in.
2422 STATIC int /* error */
2423 xfs_btree_new_root(
2424 struct xfs_btree_cur *cur, /* btree cursor */
2425 int *stat) /* success/failure */
2427 struct xfs_btree_block *block; /* one half of the old root block */
2428 struct xfs_buf *bp; /* buffer containing block */
2429 int error; /* error return value */
2430 struct xfs_buf *lbp; /* left buffer pointer */
2431 struct xfs_btree_block *left; /* left btree block */
2432 struct xfs_buf *nbp; /* new (root) buffer */
2433 struct xfs_btree_block *new; /* new (root) btree block */
2434 int nptr; /* new value for key index, 1 or 2 */
2435 struct xfs_buf *rbp; /* right buffer pointer */
2436 struct xfs_btree_block *right; /* right btree block */
2437 union xfs_btree_ptr rptr;
2438 union xfs_btree_ptr lptr;
2440 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
2441 XFS_BTREE_STATS_INC(cur, newroot);
2443 /* initialise our start point from the cursor */
2444 cur->bc_ops->init_ptr_from_cur(cur, &rptr);
2446 /* Allocate the new block. If we can't do it, we're toast. Give up. */
2447 error = cur->bc_ops->alloc_block(cur, &rptr, &lptr, 1, stat);
2448 if (error)
2449 goto error0;
2450 if (*stat == 0)
2451 goto out0;
2452 XFS_BTREE_STATS_INC(cur, alloc);
2454 /* Set up the new block. */
2455 error = xfs_btree_get_buf_block(cur, &lptr, 0, &new, &nbp);
2456 if (error)
2457 goto error0;
2459 /* Set the root in the holding structure increasing the level by 1. */
2460 cur->bc_ops->set_root(cur, &lptr, 1);
2463 * At the previous root level there are now two blocks: the old root,
2464 * and the new block generated when it was split. We don't know which
2465 * one the cursor is pointing at, so we set up variables "left" and
2466 * "right" for each case.
2468 block = xfs_btree_get_block(cur, cur->bc_nlevels - 1, &bp);
2470 #ifdef DEBUG
2471 error = xfs_btree_check_block(cur, block, cur->bc_nlevels - 1, bp);
2472 if (error)
2473 goto error0;
2474 #endif
2476 xfs_btree_get_sibling(cur, block, &rptr, XFS_BB_RIGHTSIB);
2477 if (!xfs_btree_ptr_is_null(cur, &rptr)) {
2478 /* Our block is left, pick up the right block. */
2479 lbp = bp;
2480 xfs_btree_buf_to_ptr(cur, lbp, &lptr);
2481 left = block;
2482 error = xfs_btree_read_buf_block(cur, &rptr,
2483 cur->bc_nlevels - 1, 0, &right, &rbp);
2484 if (error)
2485 goto error0;
2486 bp = rbp;
2487 nptr = 1;
2488 } else {
2489 /* Our block is right, pick up the left block. */
2490 rbp = bp;
2491 xfs_btree_buf_to_ptr(cur, rbp, &rptr);
2492 right = block;
2493 xfs_btree_get_sibling(cur, right, &lptr, XFS_BB_LEFTSIB);
2494 error = xfs_btree_read_buf_block(cur, &lptr,
2495 cur->bc_nlevels - 1, 0, &left, &lbp);
2496 if (error)
2497 goto error0;
2498 bp = lbp;
2499 nptr = 2;
2501 /* Fill in the new block's btree header and log it. */
2502 xfs_btree_init_block(cur, cur->bc_nlevels, 2, new);
2503 xfs_btree_log_block(cur, nbp, XFS_BB_ALL_BITS);
2504 ASSERT(!xfs_btree_ptr_is_null(cur, &lptr) &&
2505 !xfs_btree_ptr_is_null(cur, &rptr));
2507 /* Fill in the key data in the new root. */
2508 if (xfs_btree_get_level(left) > 0) {
2509 xfs_btree_copy_keys(cur,
2510 xfs_btree_key_addr(cur, 1, new),
2511 xfs_btree_key_addr(cur, 1, left), 1);
2512 xfs_btree_copy_keys(cur,
2513 xfs_btree_key_addr(cur, 2, new),
2514 xfs_btree_key_addr(cur, 1, right), 1);
2515 } else {
2516 cur->bc_ops->init_key_from_rec(
2517 xfs_btree_key_addr(cur, 1, new),
2518 xfs_btree_rec_addr(cur, 1, left));
2519 cur->bc_ops->init_key_from_rec(
2520 xfs_btree_key_addr(cur, 2, new),
2521 xfs_btree_rec_addr(cur, 1, right));
2523 xfs_btree_log_keys(cur, nbp, 1, 2);
2525 /* Fill in the pointer data in the new root. */
2526 xfs_btree_copy_ptrs(cur,
2527 xfs_btree_ptr_addr(cur, 1, new), &lptr, 1);
2528 xfs_btree_copy_ptrs(cur,
2529 xfs_btree_ptr_addr(cur, 2, new), &rptr, 1);
2530 xfs_btree_log_ptrs(cur, nbp, 1, 2);
2532 /* Fix up the cursor. */
2533 xfs_btree_setbuf(cur, cur->bc_nlevels, nbp);
2534 cur->bc_ptrs[cur->bc_nlevels] = nptr;
2535 cur->bc_nlevels++;
2536 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2537 *stat = 1;
2538 return 0;
2539 error0:
2540 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
2541 return error;
2542 out0:
2543 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2544 *stat = 0;
2545 return 0;
2548 STATIC int
2549 xfs_btree_make_block_unfull(
2550 struct xfs_btree_cur *cur, /* btree cursor */
2551 int level, /* btree level */
2552 int numrecs,/* # of recs in block */
2553 int *oindex,/* old tree index */
2554 int *index, /* new tree index */
2555 union xfs_btree_ptr *nptr, /* new btree ptr */
2556 struct xfs_btree_cur **ncur, /* new btree cursor */
2557 union xfs_btree_rec *nrec, /* new record */
2558 int *stat)
2560 union xfs_btree_key key; /* new btree key value */
2561 int error = 0;
2563 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
2564 level == cur->bc_nlevels - 1) {
2565 struct xfs_inode *ip = cur->bc_private.b.ip;
2567 if (numrecs < cur->bc_ops->get_dmaxrecs(cur, level)) {
2568 /* A root block that can be made bigger. */
2570 xfs_iroot_realloc(ip, 1, cur->bc_private.b.whichfork);
2571 } else {
2572 /* A root block that needs replacing */
2573 int logflags = 0;
2575 error = xfs_btree_new_iroot(cur, &logflags, stat);
2576 if (error || *stat == 0)
2577 return error;
2579 xfs_trans_log_inode(cur->bc_tp, ip, logflags);
2582 return 0;
2585 /* First, try shifting an entry to the right neighbor. */
2586 error = xfs_btree_rshift(cur, level, stat);
2587 if (error || *stat)
2588 return error;
2590 /* Next, try shifting an entry to the left neighbor. */
2591 error = xfs_btree_lshift(cur, level, stat);
2592 if (error)
2593 return error;
2595 if (*stat) {
2596 *oindex = *index = cur->bc_ptrs[level];
2597 return 0;
2601 * Next, try splitting the current block in half.
2603 * If this works we have to re-set our variables because we
2604 * could be in a different block now.
2606 error = xfs_btree_split(cur, level, nptr, &key, ncur, stat);
2607 if (error || *stat == 0)
2608 return error;
2611 *index = cur->bc_ptrs[level];
2612 cur->bc_ops->init_rec_from_key(&key, nrec);
2613 return 0;
2617 * Insert one record/level. Return information to the caller
2618 * allowing the next level up to proceed if necessary.
2620 STATIC int
2621 xfs_btree_insrec(
2622 struct xfs_btree_cur *cur, /* btree cursor */
2623 int level, /* level to insert record at */
2624 union xfs_btree_ptr *ptrp, /* i/o: block number inserted */
2625 union xfs_btree_rec *recp, /* i/o: record data inserted */
2626 struct xfs_btree_cur **curp, /* output: new cursor replacing cur */
2627 int *stat) /* success/failure */
2629 struct xfs_btree_block *block; /* btree block */
2630 struct xfs_buf *bp; /* buffer for block */
2631 union xfs_btree_key key; /* btree key */
2632 union xfs_btree_ptr nptr; /* new block ptr */
2633 struct xfs_btree_cur *ncur; /* new btree cursor */
2634 union xfs_btree_rec nrec; /* new record count */
2635 int optr; /* old key/record index */
2636 int ptr; /* key/record index */
2637 int numrecs;/* number of records */
2638 int error; /* error return value */
2639 #ifdef DEBUG
2640 int i;
2641 #endif
2643 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
2644 XFS_BTREE_TRACE_ARGIPR(cur, level, *ptrp, recp);
2646 ncur = NULL;
2649 * If we have an external root pointer, and we've made it to the
2650 * root level, allocate a new root block and we're done.
2652 if (!(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
2653 (level >= cur->bc_nlevels)) {
2654 error = xfs_btree_new_root(cur, stat);
2655 xfs_btree_set_ptr_null(cur, ptrp);
2657 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2658 return error;
2661 /* If we're off the left edge, return failure. */
2662 ptr = cur->bc_ptrs[level];
2663 if (ptr == 0) {
2664 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2665 *stat = 0;
2666 return 0;
2669 /* Make a key out of the record data to be inserted, and save it. */
2670 cur->bc_ops->init_key_from_rec(&key, recp);
2672 optr = ptr;
2674 XFS_BTREE_STATS_INC(cur, insrec);
2676 /* Get pointers to the btree buffer and block. */
2677 block = xfs_btree_get_block(cur, level, &bp);
2678 numrecs = xfs_btree_get_numrecs(block);
2680 #ifdef DEBUG
2681 error = xfs_btree_check_block(cur, block, level, bp);
2682 if (error)
2683 goto error0;
2685 /* Check that the new entry is being inserted in the right place. */
2686 if (ptr <= numrecs) {
2687 if (level == 0) {
2688 ASSERT(cur->bc_ops->recs_inorder(cur, recp,
2689 xfs_btree_rec_addr(cur, ptr, block)));
2690 } else {
2691 ASSERT(cur->bc_ops->keys_inorder(cur, &key,
2692 xfs_btree_key_addr(cur, ptr, block)));
2695 #endif
2698 * If the block is full, we can't insert the new entry until we
2699 * make the block un-full.
2701 xfs_btree_set_ptr_null(cur, &nptr);
2702 if (numrecs == cur->bc_ops->get_maxrecs(cur, level)) {
2703 error = xfs_btree_make_block_unfull(cur, level, numrecs,
2704 &optr, &ptr, &nptr, &ncur, &nrec, stat);
2705 if (error || *stat == 0)
2706 goto error0;
2710 * The current block may have changed if the block was
2711 * previously full and we have just made space in it.
2713 block = xfs_btree_get_block(cur, level, &bp);
2714 numrecs = xfs_btree_get_numrecs(block);
2716 #ifdef DEBUG
2717 error = xfs_btree_check_block(cur, block, level, bp);
2718 if (error)
2719 return error;
2720 #endif
2723 * At this point we know there's room for our new entry in the block
2724 * we're pointing at.
2726 XFS_BTREE_STATS_ADD(cur, moves, numrecs - ptr + 1);
2728 if (level > 0) {
2729 /* It's a nonleaf. make a hole in the keys and ptrs */
2730 union xfs_btree_key *kp;
2731 union xfs_btree_ptr *pp;
2733 kp = xfs_btree_key_addr(cur, ptr, block);
2734 pp = xfs_btree_ptr_addr(cur, ptr, block);
2736 #ifdef DEBUG
2737 for (i = numrecs - ptr; i >= 0; i--) {
2738 error = xfs_btree_check_ptr(cur, pp, i, level);
2739 if (error)
2740 return error;
2742 #endif
2744 xfs_btree_shift_keys(cur, kp, 1, numrecs - ptr + 1);
2745 xfs_btree_shift_ptrs(cur, pp, 1, numrecs - ptr + 1);
2747 #ifdef DEBUG
2748 error = xfs_btree_check_ptr(cur, ptrp, 0, level);
2749 if (error)
2750 goto error0;
2751 #endif
2753 /* Now put the new data in, bump numrecs and log it. */
2754 xfs_btree_copy_keys(cur, kp, &key, 1);
2755 xfs_btree_copy_ptrs(cur, pp, ptrp, 1);
2756 numrecs++;
2757 xfs_btree_set_numrecs(block, numrecs);
2758 xfs_btree_log_ptrs(cur, bp, ptr, numrecs);
2759 xfs_btree_log_keys(cur, bp, ptr, numrecs);
2760 #ifdef DEBUG
2761 if (ptr < numrecs) {
2762 ASSERT(cur->bc_ops->keys_inorder(cur, kp,
2763 xfs_btree_key_addr(cur, ptr + 1, block)));
2765 #endif
2766 } else {
2767 /* It's a leaf. make a hole in the records */
2768 union xfs_btree_rec *rp;
2770 rp = xfs_btree_rec_addr(cur, ptr, block);
2772 xfs_btree_shift_recs(cur, rp, 1, numrecs - ptr + 1);
2774 /* Now put the new data in, bump numrecs and log it. */
2775 xfs_btree_copy_recs(cur, rp, recp, 1);
2776 xfs_btree_set_numrecs(block, ++numrecs);
2777 xfs_btree_log_recs(cur, bp, ptr, numrecs);
2778 #ifdef DEBUG
2779 if (ptr < numrecs) {
2780 ASSERT(cur->bc_ops->recs_inorder(cur, rp,
2781 xfs_btree_rec_addr(cur, ptr + 1, block)));
2783 #endif
2786 /* Log the new number of records in the btree header. */
2787 xfs_btree_log_block(cur, bp, XFS_BB_NUMRECS);
2789 /* If we inserted at the start of a block, update the parents' keys. */
2790 if (optr == 1) {
2791 error = xfs_btree_updkey(cur, &key, level + 1);
2792 if (error)
2793 goto error0;
2797 * If we are tracking the last record in the tree and
2798 * we are at the far right edge of the tree, update it.
2800 if (xfs_btree_is_lastrec(cur, block, level)) {
2801 cur->bc_ops->update_lastrec(cur, block, recp,
2802 ptr, LASTREC_INSREC);
2806 * Return the new block number, if any.
2807 * If there is one, give back a record value and a cursor too.
2809 *ptrp = nptr;
2810 if (!xfs_btree_ptr_is_null(cur, &nptr)) {
2811 *recp = nrec;
2812 *curp = ncur;
2815 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2816 *stat = 1;
2817 return 0;
2819 error0:
2820 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
2821 return error;
2825 * Insert the record at the point referenced by cur.
2827 * A multi-level split of the tree on insert will invalidate the original
2828 * cursor. All callers of this function should assume that the cursor is
2829 * no longer valid and revalidate it.
2832 xfs_btree_insert(
2833 struct xfs_btree_cur *cur,
2834 int *stat)
2836 int error; /* error return value */
2837 int i; /* result value, 0 for failure */
2838 int level; /* current level number in btree */
2839 union xfs_btree_ptr nptr; /* new block number (split result) */
2840 struct xfs_btree_cur *ncur; /* new cursor (split result) */
2841 struct xfs_btree_cur *pcur; /* previous level's cursor */
2842 union xfs_btree_rec rec; /* record to insert */
2844 level = 0;
2845 ncur = NULL;
2846 pcur = cur;
2848 xfs_btree_set_ptr_null(cur, &nptr);
2849 cur->bc_ops->init_rec_from_cur(cur, &rec);
2852 * Loop going up the tree, starting at the leaf level.
2853 * Stop when we don't get a split block, that must mean that
2854 * the insert is finished with this level.
2856 do {
2858 * Insert nrec/nptr into this level of the tree.
2859 * Note if we fail, nptr will be null.
2861 error = xfs_btree_insrec(pcur, level, &nptr, &rec, &ncur, &i);
2862 if (error) {
2863 if (pcur != cur)
2864 xfs_btree_del_cursor(pcur, XFS_BTREE_ERROR);
2865 goto error0;
2868 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
2869 level++;
2872 * See if the cursor we just used is trash.
2873 * Can't trash the caller's cursor, but otherwise we should
2874 * if ncur is a new cursor or we're about to be done.
2876 if (pcur != cur &&
2877 (ncur || xfs_btree_ptr_is_null(cur, &nptr))) {
2878 /* Save the state from the cursor before we trash it */
2879 if (cur->bc_ops->update_cursor)
2880 cur->bc_ops->update_cursor(pcur, cur);
2881 cur->bc_nlevels = pcur->bc_nlevels;
2882 xfs_btree_del_cursor(pcur, XFS_BTREE_NOERROR);
2884 /* If we got a new cursor, switch to it. */
2885 if (ncur) {
2886 pcur = ncur;
2887 ncur = NULL;
2889 } while (!xfs_btree_ptr_is_null(cur, &nptr));
2891 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2892 *stat = i;
2893 return 0;
2894 error0:
2895 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
2896 return error;
2900 * Try to merge a non-leaf block back into the inode root.
2902 * Note: the killroot names comes from the fact that we're effectively
2903 * killing the old root block. But because we can't just delete the
2904 * inode we have to copy the single block it was pointing to into the
2905 * inode.
2907 STATIC int
2908 xfs_btree_kill_iroot(
2909 struct xfs_btree_cur *cur)
2911 int whichfork = cur->bc_private.b.whichfork;
2912 struct xfs_inode *ip = cur->bc_private.b.ip;
2913 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
2914 struct xfs_btree_block *block;
2915 struct xfs_btree_block *cblock;
2916 union xfs_btree_key *kp;
2917 union xfs_btree_key *ckp;
2918 union xfs_btree_ptr *pp;
2919 union xfs_btree_ptr *cpp;
2920 struct xfs_buf *cbp;
2921 int level;
2922 int index;
2923 int numrecs;
2924 #ifdef DEBUG
2925 union xfs_btree_ptr ptr;
2926 int i;
2927 #endif
2929 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
2931 ASSERT(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE);
2932 ASSERT(cur->bc_nlevels > 1);
2935 * Don't deal with the root block needs to be a leaf case.
2936 * We're just going to turn the thing back into extents anyway.
2938 level = cur->bc_nlevels - 1;
2939 if (level == 1)
2940 goto out0;
2943 * Give up if the root has multiple children.
2945 block = xfs_btree_get_iroot(cur);
2946 if (xfs_btree_get_numrecs(block) != 1)
2947 goto out0;
2949 cblock = xfs_btree_get_block(cur, level - 1, &cbp);
2950 numrecs = xfs_btree_get_numrecs(cblock);
2953 * Only do this if the next level will fit.
2954 * Then the data must be copied up to the inode,
2955 * instead of freeing the root you free the next level.
2957 if (numrecs > cur->bc_ops->get_dmaxrecs(cur, level))
2958 goto out0;
2960 XFS_BTREE_STATS_INC(cur, killroot);
2962 #ifdef DEBUG
2963 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_LEFTSIB);
2964 ASSERT(xfs_btree_ptr_is_null(cur, &ptr));
2965 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_RIGHTSIB);
2966 ASSERT(xfs_btree_ptr_is_null(cur, &ptr));
2967 #endif
2969 index = numrecs - cur->bc_ops->get_maxrecs(cur, level);
2970 if (index) {
2971 xfs_iroot_realloc(cur->bc_private.b.ip, index,
2972 cur->bc_private.b.whichfork);
2973 block = ifp->if_broot;
2976 be16_add_cpu(&block->bb_numrecs, index);
2977 ASSERT(block->bb_numrecs == cblock->bb_numrecs);
2979 kp = xfs_btree_key_addr(cur, 1, block);
2980 ckp = xfs_btree_key_addr(cur, 1, cblock);
2981 xfs_btree_copy_keys(cur, kp, ckp, numrecs);
2983 pp = xfs_btree_ptr_addr(cur, 1, block);
2984 cpp = xfs_btree_ptr_addr(cur, 1, cblock);
2985 #ifdef DEBUG
2986 for (i = 0; i < numrecs; i++) {
2987 int error;
2989 error = xfs_btree_check_ptr(cur, cpp, i, level - 1);
2990 if (error) {
2991 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
2992 return error;
2995 #endif
2996 xfs_btree_copy_ptrs(cur, pp, cpp, numrecs);
2998 cur->bc_ops->free_block(cur, cbp);
2999 XFS_BTREE_STATS_INC(cur, free);
3001 cur->bc_bufs[level - 1] = NULL;
3002 be16_add_cpu(&block->bb_level, -1);
3003 xfs_trans_log_inode(cur->bc_tp, ip,
3004 XFS_ILOG_CORE | xfs_ilog_fbroot(cur->bc_private.b.whichfork));
3005 cur->bc_nlevels--;
3006 out0:
3007 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3008 return 0;
3012 * Kill the current root node, and replace it with it's only child node.
3014 STATIC int
3015 xfs_btree_kill_root(
3016 struct xfs_btree_cur *cur,
3017 struct xfs_buf *bp,
3018 int level,
3019 union xfs_btree_ptr *newroot)
3021 int error;
3023 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
3024 XFS_BTREE_STATS_INC(cur, killroot);
3027 * Update the root pointer, decreasing the level by 1 and then
3028 * free the old root.
3030 cur->bc_ops->set_root(cur, newroot, -1);
3032 error = cur->bc_ops->free_block(cur, bp);
3033 if (error) {
3034 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
3035 return error;
3038 XFS_BTREE_STATS_INC(cur, free);
3040 cur->bc_bufs[level] = NULL;
3041 cur->bc_ra[level] = 0;
3042 cur->bc_nlevels--;
3044 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3045 return 0;
3048 STATIC int
3049 xfs_btree_dec_cursor(
3050 struct xfs_btree_cur *cur,
3051 int level,
3052 int *stat)
3054 int error;
3055 int i;
3057 if (level > 0) {
3058 error = xfs_btree_decrement(cur, level, &i);
3059 if (error)
3060 return error;
3063 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3064 *stat = 1;
3065 return 0;
3069 * Single level of the btree record deletion routine.
3070 * Delete record pointed to by cur/level.
3071 * Remove the record from its block then rebalance the tree.
3072 * Return 0 for error, 1 for done, 2 to go on to the next level.
3074 STATIC int /* error */
3075 xfs_btree_delrec(
3076 struct xfs_btree_cur *cur, /* btree cursor */
3077 int level, /* level removing record from */
3078 int *stat) /* fail/done/go-on */
3080 struct xfs_btree_block *block; /* btree block */
3081 union xfs_btree_ptr cptr; /* current block ptr */
3082 struct xfs_buf *bp; /* buffer for block */
3083 int error; /* error return value */
3084 int i; /* loop counter */
3085 union xfs_btree_key key; /* storage for keyp */
3086 union xfs_btree_key *keyp = &key; /* passed to the next level */
3087 union xfs_btree_ptr lptr; /* left sibling block ptr */
3088 struct xfs_buf *lbp; /* left buffer pointer */
3089 struct xfs_btree_block *left; /* left btree block */
3090 int lrecs = 0; /* left record count */
3091 int ptr; /* key/record index */
3092 union xfs_btree_ptr rptr; /* right sibling block ptr */
3093 struct xfs_buf *rbp; /* right buffer pointer */
3094 struct xfs_btree_block *right; /* right btree block */
3095 struct xfs_btree_block *rrblock; /* right-right btree block */
3096 struct xfs_buf *rrbp; /* right-right buffer pointer */
3097 int rrecs = 0; /* right record count */
3098 struct xfs_btree_cur *tcur; /* temporary btree cursor */
3099 int numrecs; /* temporary numrec count */
3101 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
3102 XFS_BTREE_TRACE_ARGI(cur, level);
3104 tcur = NULL;
3106 /* Get the index of the entry being deleted, check for nothing there. */
3107 ptr = cur->bc_ptrs[level];
3108 if (ptr == 0) {
3109 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3110 *stat = 0;
3111 return 0;
3114 /* Get the buffer & block containing the record or key/ptr. */
3115 block = xfs_btree_get_block(cur, level, &bp);
3116 numrecs = xfs_btree_get_numrecs(block);
3118 #ifdef DEBUG
3119 error = xfs_btree_check_block(cur, block, level, bp);
3120 if (error)
3121 goto error0;
3122 #endif
3124 /* Fail if we're off the end of the block. */
3125 if (ptr > numrecs) {
3126 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3127 *stat = 0;
3128 return 0;
3131 XFS_BTREE_STATS_INC(cur, delrec);
3132 XFS_BTREE_STATS_ADD(cur, moves, numrecs - ptr);
3134 /* Excise the entries being deleted. */
3135 if (level > 0) {
3136 /* It's a nonleaf. operate on keys and ptrs */
3137 union xfs_btree_key *lkp;
3138 union xfs_btree_ptr *lpp;
3140 lkp = xfs_btree_key_addr(cur, ptr + 1, block);
3141 lpp = xfs_btree_ptr_addr(cur, ptr + 1, block);
3143 #ifdef DEBUG
3144 for (i = 0; i < numrecs - ptr; i++) {
3145 error = xfs_btree_check_ptr(cur, lpp, i, level);
3146 if (error)
3147 goto error0;
3149 #endif
3151 if (ptr < numrecs) {
3152 xfs_btree_shift_keys(cur, lkp, -1, numrecs - ptr);
3153 xfs_btree_shift_ptrs(cur, lpp, -1, numrecs - ptr);
3154 xfs_btree_log_keys(cur, bp, ptr, numrecs - 1);
3155 xfs_btree_log_ptrs(cur, bp, ptr, numrecs - 1);
3159 * If it's the first record in the block, we'll need to pass a
3160 * key up to the next level (updkey).
3162 if (ptr == 1)
3163 keyp = xfs_btree_key_addr(cur, 1, block);
3164 } else {
3165 /* It's a leaf. operate on records */
3166 if (ptr < numrecs) {
3167 xfs_btree_shift_recs(cur,
3168 xfs_btree_rec_addr(cur, ptr + 1, block),
3169 -1, numrecs - ptr);
3170 xfs_btree_log_recs(cur, bp, ptr, numrecs - 1);
3174 * If it's the first record in the block, we'll need a key
3175 * structure to pass up to the next level (updkey).
3177 if (ptr == 1) {
3178 cur->bc_ops->init_key_from_rec(&key,
3179 xfs_btree_rec_addr(cur, 1, block));
3180 keyp = &key;
3185 * Decrement and log the number of entries in the block.
3187 xfs_btree_set_numrecs(block, --numrecs);
3188 xfs_btree_log_block(cur, bp, XFS_BB_NUMRECS);
3191 * If we are tracking the last record in the tree and
3192 * we are at the far right edge of the tree, update it.
3194 if (xfs_btree_is_lastrec(cur, block, level)) {
3195 cur->bc_ops->update_lastrec(cur, block, NULL,
3196 ptr, LASTREC_DELREC);
3200 * We're at the root level. First, shrink the root block in-memory.
3201 * Try to get rid of the next level down. If we can't then there's
3202 * nothing left to do.
3204 if (level == cur->bc_nlevels - 1) {
3205 if (cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) {
3206 xfs_iroot_realloc(cur->bc_private.b.ip, -1,
3207 cur->bc_private.b.whichfork);
3209 error = xfs_btree_kill_iroot(cur);
3210 if (error)
3211 goto error0;
3213 error = xfs_btree_dec_cursor(cur, level, stat);
3214 if (error)
3215 goto error0;
3216 *stat = 1;
3217 return 0;
3221 * If this is the root level, and there's only one entry left,
3222 * and it's NOT the leaf level, then we can get rid of this
3223 * level.
3225 if (numrecs == 1 && level > 0) {
3226 union xfs_btree_ptr *pp;
3228 * pp is still set to the first pointer in the block.
3229 * Make it the new root of the btree.
3231 pp = xfs_btree_ptr_addr(cur, 1, block);
3232 error = xfs_btree_kill_root(cur, bp, level, pp);
3233 if (error)
3234 goto error0;
3235 } else if (level > 0) {
3236 error = xfs_btree_dec_cursor(cur, level, stat);
3237 if (error)
3238 goto error0;
3240 *stat = 1;
3241 return 0;
3245 * If we deleted the leftmost entry in the block, update the
3246 * key values above us in the tree.
3248 if (ptr == 1) {
3249 error = xfs_btree_updkey(cur, keyp, level + 1);
3250 if (error)
3251 goto error0;
3255 * If the number of records remaining in the block is at least
3256 * the minimum, we're done.
3258 if (numrecs >= cur->bc_ops->get_minrecs(cur, level)) {
3259 error = xfs_btree_dec_cursor(cur, level, stat);
3260 if (error)
3261 goto error0;
3262 return 0;
3266 * Otherwise, we have to move some records around to keep the
3267 * tree balanced. Look at the left and right sibling blocks to
3268 * see if we can re-balance by moving only one record.
3270 xfs_btree_get_sibling(cur, block, &rptr, XFS_BB_RIGHTSIB);
3271 xfs_btree_get_sibling(cur, block, &lptr, XFS_BB_LEFTSIB);
3273 if (cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) {
3275 * One child of root, need to get a chance to copy its contents
3276 * into the root and delete it. Can't go up to next level,
3277 * there's nothing to delete there.
3279 if (xfs_btree_ptr_is_null(cur, &rptr) &&
3280 xfs_btree_ptr_is_null(cur, &lptr) &&
3281 level == cur->bc_nlevels - 2) {
3282 error = xfs_btree_kill_iroot(cur);
3283 if (!error)
3284 error = xfs_btree_dec_cursor(cur, level, stat);
3285 if (error)
3286 goto error0;
3287 return 0;
3291 ASSERT(!xfs_btree_ptr_is_null(cur, &rptr) ||
3292 !xfs_btree_ptr_is_null(cur, &lptr));
3295 * Duplicate the cursor so our btree manipulations here won't
3296 * disrupt the next level up.
3298 error = xfs_btree_dup_cursor(cur, &tcur);
3299 if (error)
3300 goto error0;
3303 * If there's a right sibling, see if it's ok to shift an entry
3304 * out of it.
3306 if (!xfs_btree_ptr_is_null(cur, &rptr)) {
3308 * Move the temp cursor to the last entry in the next block.
3309 * Actually any entry but the first would suffice.
3311 i = xfs_btree_lastrec(tcur, level);
3312 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
3314 error = xfs_btree_increment(tcur, level, &i);
3315 if (error)
3316 goto error0;
3317 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
3319 i = xfs_btree_lastrec(tcur, level);
3320 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
3322 /* Grab a pointer to the block. */
3323 right = xfs_btree_get_block(tcur, level, &rbp);
3324 #ifdef DEBUG
3325 error = xfs_btree_check_block(tcur, right, level, rbp);
3326 if (error)
3327 goto error0;
3328 #endif
3329 /* Grab the current block number, for future use. */
3330 xfs_btree_get_sibling(tcur, right, &cptr, XFS_BB_LEFTSIB);
3333 * If right block is full enough so that removing one entry
3334 * won't make it too empty, and left-shifting an entry out
3335 * of right to us works, we're done.
3337 if (xfs_btree_get_numrecs(right) - 1 >=
3338 cur->bc_ops->get_minrecs(tcur, level)) {
3339 error = xfs_btree_lshift(tcur, level, &i);
3340 if (error)
3341 goto error0;
3342 if (i) {
3343 ASSERT(xfs_btree_get_numrecs(block) >=
3344 cur->bc_ops->get_minrecs(tcur, level));
3346 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
3347 tcur = NULL;
3349 error = xfs_btree_dec_cursor(cur, level, stat);
3350 if (error)
3351 goto error0;
3352 return 0;
3357 * Otherwise, grab the number of records in right for
3358 * future reference, and fix up the temp cursor to point
3359 * to our block again (last record).
3361 rrecs = xfs_btree_get_numrecs(right);
3362 if (!xfs_btree_ptr_is_null(cur, &lptr)) {
3363 i = xfs_btree_firstrec(tcur, level);
3364 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
3366 error = xfs_btree_decrement(tcur, level, &i);
3367 if (error)
3368 goto error0;
3369 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
3374 * If there's a left sibling, see if it's ok to shift an entry
3375 * out of it.
3377 if (!xfs_btree_ptr_is_null(cur, &lptr)) {
3379 * Move the temp cursor to the first entry in the
3380 * previous block.
3382 i = xfs_btree_firstrec(tcur, level);
3383 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
3385 error = xfs_btree_decrement(tcur, level, &i);
3386 if (error)
3387 goto error0;
3388 i = xfs_btree_firstrec(tcur, level);
3389 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
3391 /* Grab a pointer to the block. */
3392 left = xfs_btree_get_block(tcur, level, &lbp);
3393 #ifdef DEBUG
3394 error = xfs_btree_check_block(cur, left, level, lbp);
3395 if (error)
3396 goto error0;
3397 #endif
3398 /* Grab the current block number, for future use. */
3399 xfs_btree_get_sibling(tcur, left, &cptr, XFS_BB_RIGHTSIB);
3402 * If left block is full enough so that removing one entry
3403 * won't make it too empty, and right-shifting an entry out
3404 * of left to us works, we're done.
3406 if (xfs_btree_get_numrecs(left) - 1 >=
3407 cur->bc_ops->get_minrecs(tcur, level)) {
3408 error = xfs_btree_rshift(tcur, level, &i);
3409 if (error)
3410 goto error0;
3411 if (i) {
3412 ASSERT(xfs_btree_get_numrecs(block) >=
3413 cur->bc_ops->get_minrecs(tcur, level));
3414 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
3415 tcur = NULL;
3416 if (level == 0)
3417 cur->bc_ptrs[0]++;
3418 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3419 *stat = 1;
3420 return 0;
3425 * Otherwise, grab the number of records in right for
3426 * future reference.
3428 lrecs = xfs_btree_get_numrecs(left);
3431 /* Delete the temp cursor, we're done with it. */
3432 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
3433 tcur = NULL;
3435 /* If here, we need to do a join to keep the tree balanced. */
3436 ASSERT(!xfs_btree_ptr_is_null(cur, &cptr));
3438 if (!xfs_btree_ptr_is_null(cur, &lptr) &&
3439 lrecs + xfs_btree_get_numrecs(block) <=
3440 cur->bc_ops->get_maxrecs(cur, level)) {
3442 * Set "right" to be the starting block,
3443 * "left" to be the left neighbor.
3445 rptr = cptr;
3446 right = block;
3447 rbp = bp;
3448 error = xfs_btree_read_buf_block(cur, &lptr, level,
3449 0, &left, &lbp);
3450 if (error)
3451 goto error0;
3454 * If that won't work, see if we can join with the right neighbor block.
3456 } else if (!xfs_btree_ptr_is_null(cur, &rptr) &&
3457 rrecs + xfs_btree_get_numrecs(block) <=
3458 cur->bc_ops->get_maxrecs(cur, level)) {
3460 * Set "left" to be the starting block,
3461 * "right" to be the right neighbor.
3463 lptr = cptr;
3464 left = block;
3465 lbp = bp;
3466 error = xfs_btree_read_buf_block(cur, &rptr, level,
3467 0, &right, &rbp);
3468 if (error)
3469 goto error0;
3472 * Otherwise, we can't fix the imbalance.
3473 * Just return. This is probably a logic error, but it's not fatal.
3475 } else {
3476 error = xfs_btree_dec_cursor(cur, level, stat);
3477 if (error)
3478 goto error0;
3479 return 0;
3482 rrecs = xfs_btree_get_numrecs(right);
3483 lrecs = xfs_btree_get_numrecs(left);
3486 * We're now going to join "left" and "right" by moving all the stuff
3487 * in "right" to "left" and deleting "right".
3489 XFS_BTREE_STATS_ADD(cur, moves, rrecs);
3490 if (level > 0) {
3491 /* It's a non-leaf. Move keys and pointers. */
3492 union xfs_btree_key *lkp; /* left btree key */
3493 union xfs_btree_ptr *lpp; /* left address pointer */
3494 union xfs_btree_key *rkp; /* right btree key */
3495 union xfs_btree_ptr *rpp; /* right address pointer */
3497 lkp = xfs_btree_key_addr(cur, lrecs + 1, left);
3498 lpp = xfs_btree_ptr_addr(cur, lrecs + 1, left);
3499 rkp = xfs_btree_key_addr(cur, 1, right);
3500 rpp = xfs_btree_ptr_addr(cur, 1, right);
3501 #ifdef DEBUG
3502 for (i = 1; i < rrecs; i++) {
3503 error = xfs_btree_check_ptr(cur, rpp, i, level);
3504 if (error)
3505 goto error0;
3507 #endif
3508 xfs_btree_copy_keys(cur, lkp, rkp, rrecs);
3509 xfs_btree_copy_ptrs(cur, lpp, rpp, rrecs);
3511 xfs_btree_log_keys(cur, lbp, lrecs + 1, lrecs + rrecs);
3512 xfs_btree_log_ptrs(cur, lbp, lrecs + 1, lrecs + rrecs);
3513 } else {
3514 /* It's a leaf. Move records. */
3515 union xfs_btree_rec *lrp; /* left record pointer */
3516 union xfs_btree_rec *rrp; /* right record pointer */
3518 lrp = xfs_btree_rec_addr(cur, lrecs + 1, left);
3519 rrp = xfs_btree_rec_addr(cur, 1, right);
3521 xfs_btree_copy_recs(cur, lrp, rrp, rrecs);
3522 xfs_btree_log_recs(cur, lbp, lrecs + 1, lrecs + rrecs);
3525 XFS_BTREE_STATS_INC(cur, join);
3528 * Fix up the number of records and right block pointer in the
3529 * surviving block, and log it.
3531 xfs_btree_set_numrecs(left, lrecs + rrecs);
3532 xfs_btree_get_sibling(cur, right, &cptr, XFS_BB_RIGHTSIB),
3533 xfs_btree_set_sibling(cur, left, &cptr, XFS_BB_RIGHTSIB);
3534 xfs_btree_log_block(cur, lbp, XFS_BB_NUMRECS | XFS_BB_RIGHTSIB);
3536 /* If there is a right sibling, point it to the remaining block. */
3537 xfs_btree_get_sibling(cur, left, &cptr, XFS_BB_RIGHTSIB);
3538 if (!xfs_btree_ptr_is_null(cur, &cptr)) {
3539 error = xfs_btree_read_buf_block(cur, &cptr, level,
3540 0, &rrblock, &rrbp);
3541 if (error)
3542 goto error0;
3543 xfs_btree_set_sibling(cur, rrblock, &lptr, XFS_BB_LEFTSIB);
3544 xfs_btree_log_block(cur, rrbp, XFS_BB_LEFTSIB);
3547 /* Free the deleted block. */
3548 error = cur->bc_ops->free_block(cur, rbp);
3549 if (error)
3550 goto error0;
3551 XFS_BTREE_STATS_INC(cur, free);
3554 * If we joined with the left neighbor, set the buffer in the
3555 * cursor to the left block, and fix up the index.
3557 if (bp != lbp) {
3558 cur->bc_bufs[level] = lbp;
3559 cur->bc_ptrs[level] += lrecs;
3560 cur->bc_ra[level] = 0;
3563 * If we joined with the right neighbor and there's a level above
3564 * us, increment the cursor at that level.
3566 else if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) ||
3567 (level + 1 < cur->bc_nlevels)) {
3568 error = xfs_btree_increment(cur, level + 1, &i);
3569 if (error)
3570 goto error0;
3574 * Readjust the ptr at this level if it's not a leaf, since it's
3575 * still pointing at the deletion point, which makes the cursor
3576 * inconsistent. If this makes the ptr 0, the caller fixes it up.
3577 * We can't use decrement because it would change the next level up.
3579 if (level > 0)
3580 cur->bc_ptrs[level]--;
3582 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3583 /* Return value means the next level up has something to do. */
3584 *stat = 2;
3585 return 0;
3587 error0:
3588 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
3589 if (tcur)
3590 xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
3591 return error;
3595 * Delete the record pointed to by cur.
3596 * The cursor refers to the place where the record was (could be inserted)
3597 * when the operation returns.
3599 int /* error */
3600 xfs_btree_delete(
3601 struct xfs_btree_cur *cur,
3602 int *stat) /* success/failure */
3604 int error; /* error return value */
3605 int level;
3606 int i;
3608 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
3611 * Go up the tree, starting at leaf level.
3613 * If 2 is returned then a join was done; go to the next level.
3614 * Otherwise we are done.
3616 for (level = 0, i = 2; i == 2; level++) {
3617 error = xfs_btree_delrec(cur, level, &i);
3618 if (error)
3619 goto error0;
3622 if (i == 0) {
3623 for (level = 1; level < cur->bc_nlevels; level++) {
3624 if (cur->bc_ptrs[level] == 0) {
3625 error = xfs_btree_decrement(cur, level, &i);
3626 if (error)
3627 goto error0;
3628 break;
3633 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3634 *stat = i;
3635 return 0;
3636 error0:
3637 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
3638 return error;
3642 * Get the data from the pointed-to record.
3644 int /* error */
3645 xfs_btree_get_rec(
3646 struct xfs_btree_cur *cur, /* btree cursor */
3647 union xfs_btree_rec **recp, /* output: btree record */
3648 int *stat) /* output: success/failure */
3650 struct xfs_btree_block *block; /* btree block */
3651 struct xfs_buf *bp; /* buffer pointer */
3652 int ptr; /* record number */
3653 #ifdef DEBUG
3654 int error; /* error return value */
3655 #endif
3657 ptr = cur->bc_ptrs[0];
3658 block = xfs_btree_get_block(cur, 0, &bp);
3660 #ifdef DEBUG
3661 error = xfs_btree_check_block(cur, block, 0, bp);
3662 if (error)
3663 return error;
3664 #endif
3667 * Off the right end or left end, return failure.
3669 if (ptr > xfs_btree_get_numrecs(block) || ptr <= 0) {
3670 *stat = 0;
3671 return 0;
3675 * Point to the record and extract its data.
3677 *recp = xfs_btree_rec_addr(cur, ptr, block);
3678 *stat = 1;
3679 return 0;