ASoC: update for removeal of slab.h from percpu.h
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / xfs / xfs_btree.c
blob96be4b0f249613fef3b0cc8ab0ae443dafc3d2f4
1 /*
2 * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 #include "xfs.h"
19 #include "xfs_fs.h"
20 #include "xfs_types.h"
21 #include "xfs_bit.h"
22 #include "xfs_log.h"
23 #include "xfs_inum.h"
24 #include "xfs_trans.h"
25 #include "xfs_sb.h"
26 #include "xfs_ag.h"
27 #include "xfs_dir2.h"
28 #include "xfs_dmapi.h"
29 #include "xfs_mount.h"
30 #include "xfs_bmap_btree.h"
31 #include "xfs_alloc_btree.h"
32 #include "xfs_ialloc_btree.h"
33 #include "xfs_dir2_sf.h"
34 #include "xfs_attr_sf.h"
35 #include "xfs_dinode.h"
36 #include "xfs_inode.h"
37 #include "xfs_inode_item.h"
38 #include "xfs_btree.h"
39 #include "xfs_btree_trace.h"
40 #include "xfs_ialloc.h"
41 #include "xfs_error.h"
42 #include "xfs_trace.h"
45 * Cursor allocation zone.
47 kmem_zone_t *xfs_btree_cur_zone;
50 * Btree magic numbers.
52 const __uint32_t xfs_magics[XFS_BTNUM_MAX] = {
53 XFS_ABTB_MAGIC, XFS_ABTC_MAGIC, XFS_BMAP_MAGIC, XFS_IBT_MAGIC
57 STATIC int /* error (0 or EFSCORRUPTED) */
58 xfs_btree_check_lblock(
59 struct xfs_btree_cur *cur, /* btree cursor */
60 struct xfs_btree_block *block, /* btree long form block pointer */
61 int level, /* level of the btree block */
62 struct xfs_buf *bp) /* buffer for block, if any */
64 int lblock_ok; /* block passes checks */
65 struct xfs_mount *mp; /* file system mount point */
67 mp = cur->bc_mp;
68 lblock_ok =
69 be32_to_cpu(block->bb_magic) == xfs_magics[cur->bc_btnum] &&
70 be16_to_cpu(block->bb_level) == level &&
71 be16_to_cpu(block->bb_numrecs) <=
72 cur->bc_ops->get_maxrecs(cur, level) &&
73 block->bb_u.l.bb_leftsib &&
74 (be64_to_cpu(block->bb_u.l.bb_leftsib) == NULLDFSBNO ||
75 XFS_FSB_SANITY_CHECK(mp,
76 be64_to_cpu(block->bb_u.l.bb_leftsib))) &&
77 block->bb_u.l.bb_rightsib &&
78 (be64_to_cpu(block->bb_u.l.bb_rightsib) == NULLDFSBNO ||
79 XFS_FSB_SANITY_CHECK(mp,
80 be64_to_cpu(block->bb_u.l.bb_rightsib)));
81 if (unlikely(XFS_TEST_ERROR(!lblock_ok, mp,
82 XFS_ERRTAG_BTREE_CHECK_LBLOCK,
83 XFS_RANDOM_BTREE_CHECK_LBLOCK))) {
84 if (bp)
85 trace_xfs_btree_corrupt(bp, _RET_IP_);
86 XFS_ERROR_REPORT("xfs_btree_check_lblock", XFS_ERRLEVEL_LOW,
87 mp);
88 return XFS_ERROR(EFSCORRUPTED);
90 return 0;
93 STATIC int /* error (0 or EFSCORRUPTED) */
94 xfs_btree_check_sblock(
95 struct xfs_btree_cur *cur, /* btree cursor */
96 struct xfs_btree_block *block, /* btree short form block pointer */
97 int level, /* level of the btree block */
98 struct xfs_buf *bp) /* buffer containing block */
100 struct xfs_buf *agbp; /* buffer for ag. freespace struct */
101 struct xfs_agf *agf; /* ag. freespace structure */
102 xfs_agblock_t agflen; /* native ag. freespace length */
103 int sblock_ok; /* block passes checks */
105 agbp = cur->bc_private.a.agbp;
106 agf = XFS_BUF_TO_AGF(agbp);
107 agflen = be32_to_cpu(agf->agf_length);
108 sblock_ok =
109 be32_to_cpu(block->bb_magic) == xfs_magics[cur->bc_btnum] &&
110 be16_to_cpu(block->bb_level) == level &&
111 be16_to_cpu(block->bb_numrecs) <=
112 cur->bc_ops->get_maxrecs(cur, level) &&
113 (be32_to_cpu(block->bb_u.s.bb_leftsib) == NULLAGBLOCK ||
114 be32_to_cpu(block->bb_u.s.bb_leftsib) < agflen) &&
115 block->bb_u.s.bb_leftsib &&
116 (be32_to_cpu(block->bb_u.s.bb_rightsib) == NULLAGBLOCK ||
117 be32_to_cpu(block->bb_u.s.bb_rightsib) < agflen) &&
118 block->bb_u.s.bb_rightsib;
119 if (unlikely(XFS_TEST_ERROR(!sblock_ok, cur->bc_mp,
120 XFS_ERRTAG_BTREE_CHECK_SBLOCK,
121 XFS_RANDOM_BTREE_CHECK_SBLOCK))) {
122 if (bp)
123 trace_xfs_btree_corrupt(bp, _RET_IP_);
124 XFS_CORRUPTION_ERROR("xfs_btree_check_sblock",
125 XFS_ERRLEVEL_LOW, cur->bc_mp, block);
126 return XFS_ERROR(EFSCORRUPTED);
128 return 0;
132 * Debug routine: check that block header is ok.
135 xfs_btree_check_block(
136 struct xfs_btree_cur *cur, /* btree cursor */
137 struct xfs_btree_block *block, /* generic btree block pointer */
138 int level, /* level of the btree block */
139 struct xfs_buf *bp) /* buffer containing block, if any */
141 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
142 return xfs_btree_check_lblock(cur, block, level, bp);
143 else
144 return xfs_btree_check_sblock(cur, block, level, bp);
148 * Check that (long) pointer is ok.
150 int /* error (0 or EFSCORRUPTED) */
151 xfs_btree_check_lptr(
152 struct xfs_btree_cur *cur, /* btree cursor */
153 xfs_dfsbno_t bno, /* btree block disk address */
154 int level) /* btree block level */
156 XFS_WANT_CORRUPTED_RETURN(
157 level > 0 &&
158 bno != NULLDFSBNO &&
159 XFS_FSB_SANITY_CHECK(cur->bc_mp, bno));
160 return 0;
163 #ifdef DEBUG
165 * Check that (short) pointer is ok.
167 STATIC int /* error (0 or EFSCORRUPTED) */
168 xfs_btree_check_sptr(
169 struct xfs_btree_cur *cur, /* btree cursor */
170 xfs_agblock_t bno, /* btree block disk address */
171 int level) /* btree block level */
173 xfs_agblock_t agblocks = cur->bc_mp->m_sb.sb_agblocks;
175 XFS_WANT_CORRUPTED_RETURN(
176 level > 0 &&
177 bno != NULLAGBLOCK &&
178 bno != 0 &&
179 bno < agblocks);
180 return 0;
184 * Check that block ptr is ok.
186 STATIC int /* error (0 or EFSCORRUPTED) */
187 xfs_btree_check_ptr(
188 struct xfs_btree_cur *cur, /* btree cursor */
189 union xfs_btree_ptr *ptr, /* btree block disk address */
190 int index, /* offset from ptr to check */
191 int level) /* btree block level */
193 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
194 return xfs_btree_check_lptr(cur,
195 be64_to_cpu((&ptr->l)[index]), level);
196 } else {
197 return xfs_btree_check_sptr(cur,
198 be32_to_cpu((&ptr->s)[index]), level);
201 #endif
204 * Delete the btree cursor.
206 void
207 xfs_btree_del_cursor(
208 xfs_btree_cur_t *cur, /* btree cursor */
209 int error) /* del because of error */
211 int i; /* btree level */
214 * Clear the buffer pointers, and release the buffers.
215 * If we're doing this in the face of an error, we
216 * need to make sure to inspect all of the entries
217 * in the bc_bufs array for buffers to be unlocked.
218 * This is because some of the btree code works from
219 * level n down to 0, and if we get an error along
220 * the way we won't have initialized all the entries
221 * down to 0.
223 for (i = 0; i < cur->bc_nlevels; i++) {
224 if (cur->bc_bufs[i])
225 xfs_btree_setbuf(cur, i, NULL);
226 else if (!error)
227 break;
230 * Can't free a bmap cursor without having dealt with the
231 * allocated indirect blocks' accounting.
233 ASSERT(cur->bc_btnum != XFS_BTNUM_BMAP ||
234 cur->bc_private.b.allocated == 0);
236 * Free the cursor.
238 kmem_zone_free(xfs_btree_cur_zone, cur);
242 * Duplicate the btree cursor.
243 * Allocate a new one, copy the record, re-get the buffers.
245 int /* error */
246 xfs_btree_dup_cursor(
247 xfs_btree_cur_t *cur, /* input cursor */
248 xfs_btree_cur_t **ncur) /* output cursor */
250 xfs_buf_t *bp; /* btree block's buffer pointer */
251 int error; /* error return value */
252 int i; /* level number of btree block */
253 xfs_mount_t *mp; /* mount structure for filesystem */
254 xfs_btree_cur_t *new; /* new cursor value */
255 xfs_trans_t *tp; /* transaction pointer, can be NULL */
257 tp = cur->bc_tp;
258 mp = cur->bc_mp;
261 * Allocate a new cursor like the old one.
263 new = cur->bc_ops->dup_cursor(cur);
266 * Copy the record currently in the cursor.
268 new->bc_rec = cur->bc_rec;
271 * For each level current, re-get the buffer and copy the ptr value.
273 for (i = 0; i < new->bc_nlevels; i++) {
274 new->bc_ptrs[i] = cur->bc_ptrs[i];
275 new->bc_ra[i] = cur->bc_ra[i];
276 if ((bp = cur->bc_bufs[i])) {
277 if ((error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp,
278 XFS_BUF_ADDR(bp), mp->m_bsize, 0, &bp))) {
279 xfs_btree_del_cursor(new, error);
280 *ncur = NULL;
281 return error;
283 new->bc_bufs[i] = bp;
284 ASSERT(bp);
285 ASSERT(!XFS_BUF_GETERROR(bp));
286 } else
287 new->bc_bufs[i] = NULL;
289 *ncur = new;
290 return 0;
294 * XFS btree block layout and addressing:
296 * There are two types of blocks in the btree: leaf and non-leaf blocks.
298 * The leaf record start with a header then followed by records containing
299 * the values. A non-leaf block also starts with the same header, and
300 * then first contains lookup keys followed by an equal number of pointers
301 * to the btree blocks at the previous level.
303 * +--------+-------+-------+-------+-------+-------+-------+
304 * Leaf: | header | rec 1 | rec 2 | rec 3 | rec 4 | rec 5 | rec N |
305 * +--------+-------+-------+-------+-------+-------+-------+
307 * +--------+-------+-------+-------+-------+-------+-------+
308 * Non-Leaf: | header | key 1 | key 2 | key N | ptr 1 | ptr 2 | ptr N |
309 * +--------+-------+-------+-------+-------+-------+-------+
311 * The header is called struct xfs_btree_block for reasons better left unknown
312 * and comes in different versions for short (32bit) and long (64bit) block
313 * pointers. The record and key structures are defined by the btree instances
314 * and opaque to the btree core. The block pointers are simple disk endian
315 * integers, available in a short (32bit) and long (64bit) variant.
317 * The helpers below calculate the offset of a given record, key or pointer
318 * into a btree block (xfs_btree_*_offset) or return a pointer to the given
319 * record, key or pointer (xfs_btree_*_addr). Note that all addressing
320 * inside the btree block is done using indices starting at one, not zero!
324 * Return size of the btree block header for this btree instance.
326 static inline size_t xfs_btree_block_len(struct xfs_btree_cur *cur)
328 return (cur->bc_flags & XFS_BTREE_LONG_PTRS) ?
329 XFS_BTREE_LBLOCK_LEN :
330 XFS_BTREE_SBLOCK_LEN;
334 * Return size of btree block pointers for this btree instance.
336 static inline size_t xfs_btree_ptr_len(struct xfs_btree_cur *cur)
338 return (cur->bc_flags & XFS_BTREE_LONG_PTRS) ?
339 sizeof(__be64) : sizeof(__be32);
343 * Calculate offset of the n-th record in a btree block.
345 STATIC size_t
346 xfs_btree_rec_offset(
347 struct xfs_btree_cur *cur,
348 int n)
350 return xfs_btree_block_len(cur) +
351 (n - 1) * cur->bc_ops->rec_len;
355 * Calculate offset of the n-th key in a btree block.
357 STATIC size_t
358 xfs_btree_key_offset(
359 struct xfs_btree_cur *cur,
360 int n)
362 return xfs_btree_block_len(cur) +
363 (n - 1) * cur->bc_ops->key_len;
367 * Calculate offset of the n-th block pointer in a btree block.
369 STATIC size_t
370 xfs_btree_ptr_offset(
371 struct xfs_btree_cur *cur,
372 int n,
373 int level)
375 return xfs_btree_block_len(cur) +
376 cur->bc_ops->get_maxrecs(cur, level) * cur->bc_ops->key_len +
377 (n - 1) * xfs_btree_ptr_len(cur);
381 * Return a pointer to the n-th record in the btree block.
383 STATIC union xfs_btree_rec *
384 xfs_btree_rec_addr(
385 struct xfs_btree_cur *cur,
386 int n,
387 struct xfs_btree_block *block)
389 return (union xfs_btree_rec *)
390 ((char *)block + xfs_btree_rec_offset(cur, n));
394 * Return a pointer to the n-th key in the btree block.
396 STATIC union xfs_btree_key *
397 xfs_btree_key_addr(
398 struct xfs_btree_cur *cur,
399 int n,
400 struct xfs_btree_block *block)
402 return (union xfs_btree_key *)
403 ((char *)block + xfs_btree_key_offset(cur, n));
407 * Return a pointer to the n-th block pointer in the btree block.
409 STATIC union xfs_btree_ptr *
410 xfs_btree_ptr_addr(
411 struct xfs_btree_cur *cur,
412 int n,
413 struct xfs_btree_block *block)
415 int level = xfs_btree_get_level(block);
417 ASSERT(block->bb_level != 0);
419 return (union xfs_btree_ptr *)
420 ((char *)block + xfs_btree_ptr_offset(cur, n, level));
424 * Get a the root block which is stored in the inode.
426 * For now this btree implementation assumes the btree root is always
427 * stored in the if_broot field of an inode fork.
429 STATIC struct xfs_btree_block *
430 xfs_btree_get_iroot(
431 struct xfs_btree_cur *cur)
433 struct xfs_ifork *ifp;
435 ifp = XFS_IFORK_PTR(cur->bc_private.b.ip, cur->bc_private.b.whichfork);
436 return (struct xfs_btree_block *)ifp->if_broot;
440 * Retrieve the block pointer from the cursor at the given level.
441 * This may be an inode btree root or from a buffer.
443 STATIC struct xfs_btree_block * /* generic btree block pointer */
444 xfs_btree_get_block(
445 struct xfs_btree_cur *cur, /* btree cursor */
446 int level, /* level in btree */
447 struct xfs_buf **bpp) /* buffer containing the block */
449 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
450 (level == cur->bc_nlevels - 1)) {
451 *bpp = NULL;
452 return xfs_btree_get_iroot(cur);
455 *bpp = cur->bc_bufs[level];
456 return XFS_BUF_TO_BLOCK(*bpp);
460 * Get a buffer for the block, return it with no data read.
461 * Long-form addressing.
463 xfs_buf_t * /* buffer for fsbno */
464 xfs_btree_get_bufl(
465 xfs_mount_t *mp, /* file system mount point */
466 xfs_trans_t *tp, /* transaction pointer */
467 xfs_fsblock_t fsbno, /* file system block number */
468 uint lock) /* lock flags for get_buf */
470 xfs_buf_t *bp; /* buffer pointer (return value) */
471 xfs_daddr_t d; /* real disk block address */
473 ASSERT(fsbno != NULLFSBLOCK);
474 d = XFS_FSB_TO_DADDR(mp, fsbno);
475 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d, mp->m_bsize, lock);
476 ASSERT(bp);
477 ASSERT(!XFS_BUF_GETERROR(bp));
478 return bp;
482 * Get a buffer for the block, return it with no data read.
483 * Short-form addressing.
485 xfs_buf_t * /* buffer for agno/agbno */
486 xfs_btree_get_bufs(
487 xfs_mount_t *mp, /* file system mount point */
488 xfs_trans_t *tp, /* transaction pointer */
489 xfs_agnumber_t agno, /* allocation group number */
490 xfs_agblock_t agbno, /* allocation group block number */
491 uint lock) /* lock flags for get_buf */
493 xfs_buf_t *bp; /* buffer pointer (return value) */
494 xfs_daddr_t d; /* real disk block address */
496 ASSERT(agno != NULLAGNUMBER);
497 ASSERT(agbno != NULLAGBLOCK);
498 d = XFS_AGB_TO_DADDR(mp, agno, agbno);
499 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d, mp->m_bsize, lock);
500 ASSERT(bp);
501 ASSERT(!XFS_BUF_GETERROR(bp));
502 return bp;
506 * Check for the cursor referring to the last block at the given level.
508 int /* 1=is last block, 0=not last block */
509 xfs_btree_islastblock(
510 xfs_btree_cur_t *cur, /* btree cursor */
511 int level) /* level to check */
513 struct xfs_btree_block *block; /* generic btree block pointer */
514 xfs_buf_t *bp; /* buffer containing block */
516 block = xfs_btree_get_block(cur, level, &bp);
517 xfs_btree_check_block(cur, block, level, bp);
518 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
519 return be64_to_cpu(block->bb_u.l.bb_rightsib) == NULLDFSBNO;
520 else
521 return be32_to_cpu(block->bb_u.s.bb_rightsib) == NULLAGBLOCK;
525 * Change the cursor to point to the first record at the given level.
526 * Other levels are unaffected.
528 STATIC int /* success=1, failure=0 */
529 xfs_btree_firstrec(
530 xfs_btree_cur_t *cur, /* btree cursor */
531 int level) /* level to change */
533 struct xfs_btree_block *block; /* generic btree block pointer */
534 xfs_buf_t *bp; /* buffer containing block */
537 * Get the block pointer for this level.
539 block = xfs_btree_get_block(cur, level, &bp);
540 xfs_btree_check_block(cur, block, level, bp);
542 * It's empty, there is no such record.
544 if (!block->bb_numrecs)
545 return 0;
547 * Set the ptr value to 1, that's the first record/key.
549 cur->bc_ptrs[level] = 1;
550 return 1;
554 * Change the cursor to point to the last record in the current block
555 * at the given level. Other levels are unaffected.
557 STATIC int /* success=1, failure=0 */
558 xfs_btree_lastrec(
559 xfs_btree_cur_t *cur, /* btree cursor */
560 int level) /* level to change */
562 struct xfs_btree_block *block; /* generic btree block pointer */
563 xfs_buf_t *bp; /* buffer containing block */
566 * Get the block pointer for this level.
568 block = xfs_btree_get_block(cur, level, &bp);
569 xfs_btree_check_block(cur, block, level, bp);
571 * It's empty, there is no such record.
573 if (!block->bb_numrecs)
574 return 0;
576 * Set the ptr value to numrecs, that's the last record/key.
578 cur->bc_ptrs[level] = be16_to_cpu(block->bb_numrecs);
579 return 1;
583 * Compute first and last byte offsets for the fields given.
584 * Interprets the offsets table, which contains struct field offsets.
586 void
587 xfs_btree_offsets(
588 __int64_t fields, /* bitmask of fields */
589 const short *offsets, /* table of field offsets */
590 int nbits, /* number of bits to inspect */
591 int *first, /* output: first byte offset */
592 int *last) /* output: last byte offset */
594 int i; /* current bit number */
595 __int64_t imask; /* mask for current bit number */
597 ASSERT(fields != 0);
599 * Find the lowest bit, so the first byte offset.
601 for (i = 0, imask = 1LL; ; i++, imask <<= 1) {
602 if (imask & fields) {
603 *first = offsets[i];
604 break;
608 * Find the highest bit, so the last byte offset.
610 for (i = nbits - 1, imask = 1LL << i; ; i--, imask >>= 1) {
611 if (imask & fields) {
612 *last = offsets[i + 1] - 1;
613 break;
619 * Get a buffer for the block, return it read in.
620 * Long-form addressing.
622 int /* error */
623 xfs_btree_read_bufl(
624 xfs_mount_t *mp, /* file system mount point */
625 xfs_trans_t *tp, /* transaction pointer */
626 xfs_fsblock_t fsbno, /* file system block number */
627 uint lock, /* lock flags for read_buf */
628 xfs_buf_t **bpp, /* buffer for fsbno */
629 int refval) /* ref count value for buffer */
631 xfs_buf_t *bp; /* return value */
632 xfs_daddr_t d; /* real disk block address */
633 int error;
635 ASSERT(fsbno != NULLFSBLOCK);
636 d = XFS_FSB_TO_DADDR(mp, fsbno);
637 if ((error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, d,
638 mp->m_bsize, lock, &bp))) {
639 return error;
641 ASSERT(!bp || !XFS_BUF_GETERROR(bp));
642 if (bp != NULL) {
643 XFS_BUF_SET_VTYPE_REF(bp, B_FS_MAP, refval);
645 *bpp = bp;
646 return 0;
650 * Read-ahead the block, don't wait for it, don't return a buffer.
651 * Long-form addressing.
653 /* ARGSUSED */
654 void
655 xfs_btree_reada_bufl(
656 xfs_mount_t *mp, /* file system mount point */
657 xfs_fsblock_t fsbno, /* file system block number */
658 xfs_extlen_t count) /* count of filesystem blocks */
660 xfs_daddr_t d;
662 ASSERT(fsbno != NULLFSBLOCK);
663 d = XFS_FSB_TO_DADDR(mp, fsbno);
664 xfs_baread(mp->m_ddev_targp, d, mp->m_bsize * count);
668 * Read-ahead the block, don't wait for it, don't return a buffer.
669 * Short-form addressing.
671 /* ARGSUSED */
672 void
673 xfs_btree_reada_bufs(
674 xfs_mount_t *mp, /* file system mount point */
675 xfs_agnumber_t agno, /* allocation group number */
676 xfs_agblock_t agbno, /* allocation group block number */
677 xfs_extlen_t count) /* count of filesystem blocks */
679 xfs_daddr_t d;
681 ASSERT(agno != NULLAGNUMBER);
682 ASSERT(agbno != NULLAGBLOCK);
683 d = XFS_AGB_TO_DADDR(mp, agno, agbno);
684 xfs_baread(mp->m_ddev_targp, d, mp->m_bsize * count);
687 STATIC int
688 xfs_btree_readahead_lblock(
689 struct xfs_btree_cur *cur,
690 int lr,
691 struct xfs_btree_block *block)
693 int rval = 0;
694 xfs_dfsbno_t left = be64_to_cpu(block->bb_u.l.bb_leftsib);
695 xfs_dfsbno_t right = be64_to_cpu(block->bb_u.l.bb_rightsib);
697 if ((lr & XFS_BTCUR_LEFTRA) && left != NULLDFSBNO) {
698 xfs_btree_reada_bufl(cur->bc_mp, left, 1);
699 rval++;
702 if ((lr & XFS_BTCUR_RIGHTRA) && right != NULLDFSBNO) {
703 xfs_btree_reada_bufl(cur->bc_mp, right, 1);
704 rval++;
707 return rval;
710 STATIC int
711 xfs_btree_readahead_sblock(
712 struct xfs_btree_cur *cur,
713 int lr,
714 struct xfs_btree_block *block)
716 int rval = 0;
717 xfs_agblock_t left = be32_to_cpu(block->bb_u.s.bb_leftsib);
718 xfs_agblock_t right = be32_to_cpu(block->bb_u.s.bb_rightsib);
721 if ((lr & XFS_BTCUR_LEFTRA) && left != NULLAGBLOCK) {
722 xfs_btree_reada_bufs(cur->bc_mp, cur->bc_private.a.agno,
723 left, 1);
724 rval++;
727 if ((lr & XFS_BTCUR_RIGHTRA) && right != NULLAGBLOCK) {
728 xfs_btree_reada_bufs(cur->bc_mp, cur->bc_private.a.agno,
729 right, 1);
730 rval++;
733 return rval;
737 * Read-ahead btree blocks, at the given level.
738 * Bits in lr are set from XFS_BTCUR_{LEFT,RIGHT}RA.
740 STATIC int
741 xfs_btree_readahead(
742 struct xfs_btree_cur *cur, /* btree cursor */
743 int lev, /* level in btree */
744 int lr) /* left/right bits */
746 struct xfs_btree_block *block;
749 * No readahead needed if we are at the root level and the
750 * btree root is stored in the inode.
752 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
753 (lev == cur->bc_nlevels - 1))
754 return 0;
756 if ((cur->bc_ra[lev] | lr) == cur->bc_ra[lev])
757 return 0;
759 cur->bc_ra[lev] |= lr;
760 block = XFS_BUF_TO_BLOCK(cur->bc_bufs[lev]);
762 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
763 return xfs_btree_readahead_lblock(cur, lr, block);
764 return xfs_btree_readahead_sblock(cur, lr, block);
768 * Set the buffer for level "lev" in the cursor to bp, releasing
769 * any previous buffer.
771 void
772 xfs_btree_setbuf(
773 xfs_btree_cur_t *cur, /* btree cursor */
774 int lev, /* level in btree */
775 xfs_buf_t *bp) /* new buffer to set */
777 struct xfs_btree_block *b; /* btree block */
778 xfs_buf_t *obp; /* old buffer pointer */
780 obp = cur->bc_bufs[lev];
781 if (obp)
782 xfs_trans_brelse(cur->bc_tp, obp);
783 cur->bc_bufs[lev] = bp;
784 cur->bc_ra[lev] = 0;
785 if (!bp)
786 return;
787 b = XFS_BUF_TO_BLOCK(bp);
788 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
789 if (be64_to_cpu(b->bb_u.l.bb_leftsib) == NULLDFSBNO)
790 cur->bc_ra[lev] |= XFS_BTCUR_LEFTRA;
791 if (be64_to_cpu(b->bb_u.l.bb_rightsib) == NULLDFSBNO)
792 cur->bc_ra[lev] |= XFS_BTCUR_RIGHTRA;
793 } else {
794 if (be32_to_cpu(b->bb_u.s.bb_leftsib) == NULLAGBLOCK)
795 cur->bc_ra[lev] |= XFS_BTCUR_LEFTRA;
796 if (be32_to_cpu(b->bb_u.s.bb_rightsib) == NULLAGBLOCK)
797 cur->bc_ra[lev] |= XFS_BTCUR_RIGHTRA;
801 STATIC int
802 xfs_btree_ptr_is_null(
803 struct xfs_btree_cur *cur,
804 union xfs_btree_ptr *ptr)
806 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
807 return be64_to_cpu(ptr->l) == NULLDFSBNO;
808 else
809 return be32_to_cpu(ptr->s) == NULLAGBLOCK;
812 STATIC void
813 xfs_btree_set_ptr_null(
814 struct xfs_btree_cur *cur,
815 union xfs_btree_ptr *ptr)
817 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
818 ptr->l = cpu_to_be64(NULLDFSBNO);
819 else
820 ptr->s = cpu_to_be32(NULLAGBLOCK);
824 * Get/set/init sibling pointers
826 STATIC void
827 xfs_btree_get_sibling(
828 struct xfs_btree_cur *cur,
829 struct xfs_btree_block *block,
830 union xfs_btree_ptr *ptr,
831 int lr)
833 ASSERT(lr == XFS_BB_LEFTSIB || lr == XFS_BB_RIGHTSIB);
835 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
836 if (lr == XFS_BB_RIGHTSIB)
837 ptr->l = block->bb_u.l.bb_rightsib;
838 else
839 ptr->l = block->bb_u.l.bb_leftsib;
840 } else {
841 if (lr == XFS_BB_RIGHTSIB)
842 ptr->s = block->bb_u.s.bb_rightsib;
843 else
844 ptr->s = block->bb_u.s.bb_leftsib;
848 STATIC void
849 xfs_btree_set_sibling(
850 struct xfs_btree_cur *cur,
851 struct xfs_btree_block *block,
852 union xfs_btree_ptr *ptr,
853 int lr)
855 ASSERT(lr == XFS_BB_LEFTSIB || lr == XFS_BB_RIGHTSIB);
857 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
858 if (lr == XFS_BB_RIGHTSIB)
859 block->bb_u.l.bb_rightsib = ptr->l;
860 else
861 block->bb_u.l.bb_leftsib = ptr->l;
862 } else {
863 if (lr == XFS_BB_RIGHTSIB)
864 block->bb_u.s.bb_rightsib = ptr->s;
865 else
866 block->bb_u.s.bb_leftsib = ptr->s;
870 STATIC void
871 xfs_btree_init_block(
872 struct xfs_btree_cur *cur,
873 int level,
874 int numrecs,
875 struct xfs_btree_block *new) /* new block */
877 new->bb_magic = cpu_to_be32(xfs_magics[cur->bc_btnum]);
878 new->bb_level = cpu_to_be16(level);
879 new->bb_numrecs = cpu_to_be16(numrecs);
881 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
882 new->bb_u.l.bb_leftsib = cpu_to_be64(NULLDFSBNO);
883 new->bb_u.l.bb_rightsib = cpu_to_be64(NULLDFSBNO);
884 } else {
885 new->bb_u.s.bb_leftsib = cpu_to_be32(NULLAGBLOCK);
886 new->bb_u.s.bb_rightsib = cpu_to_be32(NULLAGBLOCK);
891 * Return true if ptr is the last record in the btree and
892 * we need to track updateѕ to this record. The decision
893 * will be further refined in the update_lastrec method.
895 STATIC int
896 xfs_btree_is_lastrec(
897 struct xfs_btree_cur *cur,
898 struct xfs_btree_block *block,
899 int level)
901 union xfs_btree_ptr ptr;
903 if (level > 0)
904 return 0;
905 if (!(cur->bc_flags & XFS_BTREE_LASTREC_UPDATE))
906 return 0;
908 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_RIGHTSIB);
909 if (!xfs_btree_ptr_is_null(cur, &ptr))
910 return 0;
911 return 1;
914 STATIC void
915 xfs_btree_buf_to_ptr(
916 struct xfs_btree_cur *cur,
917 struct xfs_buf *bp,
918 union xfs_btree_ptr *ptr)
920 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
921 ptr->l = cpu_to_be64(XFS_DADDR_TO_FSB(cur->bc_mp,
922 XFS_BUF_ADDR(bp)));
923 else {
924 ptr->s = cpu_to_be32(xfs_daddr_to_agbno(cur->bc_mp,
925 XFS_BUF_ADDR(bp)));
929 STATIC xfs_daddr_t
930 xfs_btree_ptr_to_daddr(
931 struct xfs_btree_cur *cur,
932 union xfs_btree_ptr *ptr)
934 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
935 ASSERT(be64_to_cpu(ptr->l) != NULLDFSBNO);
937 return XFS_FSB_TO_DADDR(cur->bc_mp, be64_to_cpu(ptr->l));
938 } else {
939 ASSERT(cur->bc_private.a.agno != NULLAGNUMBER);
940 ASSERT(be32_to_cpu(ptr->s) != NULLAGBLOCK);
942 return XFS_AGB_TO_DADDR(cur->bc_mp, cur->bc_private.a.agno,
943 be32_to_cpu(ptr->s));
947 STATIC void
948 xfs_btree_set_refs(
949 struct xfs_btree_cur *cur,
950 struct xfs_buf *bp)
952 switch (cur->bc_btnum) {
953 case XFS_BTNUM_BNO:
954 case XFS_BTNUM_CNT:
955 XFS_BUF_SET_VTYPE_REF(*bpp, B_FS_MAP, XFS_ALLOC_BTREE_REF);
956 break;
957 case XFS_BTNUM_INO:
958 XFS_BUF_SET_VTYPE_REF(*bpp, B_FS_INOMAP, XFS_INO_BTREE_REF);
959 break;
960 case XFS_BTNUM_BMAP:
961 XFS_BUF_SET_VTYPE_REF(*bpp, B_FS_MAP, XFS_BMAP_BTREE_REF);
962 break;
963 default:
964 ASSERT(0);
968 STATIC int
969 xfs_btree_get_buf_block(
970 struct xfs_btree_cur *cur,
971 union xfs_btree_ptr *ptr,
972 int flags,
973 struct xfs_btree_block **block,
974 struct xfs_buf **bpp)
976 struct xfs_mount *mp = cur->bc_mp;
977 xfs_daddr_t d;
979 /* need to sort out how callers deal with failures first */
980 ASSERT(!(flags & XBF_TRYLOCK));
982 d = xfs_btree_ptr_to_daddr(cur, ptr);
983 *bpp = xfs_trans_get_buf(cur->bc_tp, mp->m_ddev_targp, d,
984 mp->m_bsize, flags);
986 ASSERT(*bpp);
987 ASSERT(!XFS_BUF_GETERROR(*bpp));
989 *block = XFS_BUF_TO_BLOCK(*bpp);
990 return 0;
994 * Read in the buffer at the given ptr and return the buffer and
995 * the block pointer within the buffer.
997 STATIC int
998 xfs_btree_read_buf_block(
999 struct xfs_btree_cur *cur,
1000 union xfs_btree_ptr *ptr,
1001 int level,
1002 int flags,
1003 struct xfs_btree_block **block,
1004 struct xfs_buf **bpp)
1006 struct xfs_mount *mp = cur->bc_mp;
1007 xfs_daddr_t d;
1008 int error;
1010 /* need to sort out how callers deal with failures first */
1011 ASSERT(!(flags & XBF_TRYLOCK));
1013 d = xfs_btree_ptr_to_daddr(cur, ptr);
1014 error = xfs_trans_read_buf(mp, cur->bc_tp, mp->m_ddev_targp, d,
1015 mp->m_bsize, flags, bpp);
1016 if (error)
1017 return error;
1019 ASSERT(*bpp != NULL);
1020 ASSERT(!XFS_BUF_GETERROR(*bpp));
1022 xfs_btree_set_refs(cur, *bpp);
1023 *block = XFS_BUF_TO_BLOCK(*bpp);
1025 error = xfs_btree_check_block(cur, *block, level, *bpp);
1026 if (error)
1027 xfs_trans_brelse(cur->bc_tp, *bpp);
1028 return error;
1032 * Copy keys from one btree block to another.
1034 STATIC void
1035 xfs_btree_copy_keys(
1036 struct xfs_btree_cur *cur,
1037 union xfs_btree_key *dst_key,
1038 union xfs_btree_key *src_key,
1039 int numkeys)
1041 ASSERT(numkeys >= 0);
1042 memcpy(dst_key, src_key, numkeys * cur->bc_ops->key_len);
1046 * Copy records from one btree block to another.
1048 STATIC void
1049 xfs_btree_copy_recs(
1050 struct xfs_btree_cur *cur,
1051 union xfs_btree_rec *dst_rec,
1052 union xfs_btree_rec *src_rec,
1053 int numrecs)
1055 ASSERT(numrecs >= 0);
1056 memcpy(dst_rec, src_rec, numrecs * cur->bc_ops->rec_len);
1060 * Copy block pointers from one btree block to another.
1062 STATIC void
1063 xfs_btree_copy_ptrs(
1064 struct xfs_btree_cur *cur,
1065 union xfs_btree_ptr *dst_ptr,
1066 union xfs_btree_ptr *src_ptr,
1067 int numptrs)
1069 ASSERT(numptrs >= 0);
1070 memcpy(dst_ptr, src_ptr, numptrs * xfs_btree_ptr_len(cur));
1074 * Shift keys one index left/right inside a single btree block.
1076 STATIC void
1077 xfs_btree_shift_keys(
1078 struct xfs_btree_cur *cur,
1079 union xfs_btree_key *key,
1080 int dir,
1081 int numkeys)
1083 char *dst_key;
1085 ASSERT(numkeys >= 0);
1086 ASSERT(dir == 1 || dir == -1);
1088 dst_key = (char *)key + (dir * cur->bc_ops->key_len);
1089 memmove(dst_key, key, numkeys * cur->bc_ops->key_len);
1093 * Shift records one index left/right inside a single btree block.
1095 STATIC void
1096 xfs_btree_shift_recs(
1097 struct xfs_btree_cur *cur,
1098 union xfs_btree_rec *rec,
1099 int dir,
1100 int numrecs)
1102 char *dst_rec;
1104 ASSERT(numrecs >= 0);
1105 ASSERT(dir == 1 || dir == -1);
1107 dst_rec = (char *)rec + (dir * cur->bc_ops->rec_len);
1108 memmove(dst_rec, rec, numrecs * cur->bc_ops->rec_len);
1112 * Shift block pointers one index left/right inside a single btree block.
1114 STATIC void
1115 xfs_btree_shift_ptrs(
1116 struct xfs_btree_cur *cur,
1117 union xfs_btree_ptr *ptr,
1118 int dir,
1119 int numptrs)
1121 char *dst_ptr;
1123 ASSERT(numptrs >= 0);
1124 ASSERT(dir == 1 || dir == -1);
1126 dst_ptr = (char *)ptr + (dir * xfs_btree_ptr_len(cur));
1127 memmove(dst_ptr, ptr, numptrs * xfs_btree_ptr_len(cur));
1131 * Log key values from the btree block.
1133 STATIC void
1134 xfs_btree_log_keys(
1135 struct xfs_btree_cur *cur,
1136 struct xfs_buf *bp,
1137 int first,
1138 int last)
1140 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1141 XFS_BTREE_TRACE_ARGBII(cur, bp, first, last);
1143 if (bp) {
1144 xfs_trans_log_buf(cur->bc_tp, bp,
1145 xfs_btree_key_offset(cur, first),
1146 xfs_btree_key_offset(cur, last + 1) - 1);
1147 } else {
1148 xfs_trans_log_inode(cur->bc_tp, cur->bc_private.b.ip,
1149 xfs_ilog_fbroot(cur->bc_private.b.whichfork));
1152 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1156 * Log record values from the btree block.
1158 void
1159 xfs_btree_log_recs(
1160 struct xfs_btree_cur *cur,
1161 struct xfs_buf *bp,
1162 int first,
1163 int last)
1165 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1166 XFS_BTREE_TRACE_ARGBII(cur, bp, first, last);
1168 xfs_trans_log_buf(cur->bc_tp, bp,
1169 xfs_btree_rec_offset(cur, first),
1170 xfs_btree_rec_offset(cur, last + 1) - 1);
1172 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1176 * Log block pointer fields from a btree block (nonleaf).
1178 STATIC void
1179 xfs_btree_log_ptrs(
1180 struct xfs_btree_cur *cur, /* btree cursor */
1181 struct xfs_buf *bp, /* buffer containing btree block */
1182 int first, /* index of first pointer to log */
1183 int last) /* index of last pointer to log */
1185 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1186 XFS_BTREE_TRACE_ARGBII(cur, bp, first, last);
1188 if (bp) {
1189 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
1190 int level = xfs_btree_get_level(block);
1192 xfs_trans_log_buf(cur->bc_tp, bp,
1193 xfs_btree_ptr_offset(cur, first, level),
1194 xfs_btree_ptr_offset(cur, last + 1, level) - 1);
1195 } else {
1196 xfs_trans_log_inode(cur->bc_tp, cur->bc_private.b.ip,
1197 xfs_ilog_fbroot(cur->bc_private.b.whichfork));
1200 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1204 * Log fields from a btree block header.
1206 void
1207 xfs_btree_log_block(
1208 struct xfs_btree_cur *cur, /* btree cursor */
1209 struct xfs_buf *bp, /* buffer containing btree block */
1210 int fields) /* mask of fields: XFS_BB_... */
1212 int first; /* first byte offset logged */
1213 int last; /* last byte offset logged */
1214 static const short soffsets[] = { /* table of offsets (short) */
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.s.bb_leftsib),
1219 offsetof(struct xfs_btree_block, bb_u.s.bb_rightsib),
1220 XFS_BTREE_SBLOCK_LEN
1222 static const short loffsets[] = { /* table of offsets (long) */
1223 offsetof(struct xfs_btree_block, bb_magic),
1224 offsetof(struct xfs_btree_block, bb_level),
1225 offsetof(struct xfs_btree_block, bb_numrecs),
1226 offsetof(struct xfs_btree_block, bb_u.l.bb_leftsib),
1227 offsetof(struct xfs_btree_block, bb_u.l.bb_rightsib),
1228 XFS_BTREE_LBLOCK_LEN
1231 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1232 XFS_BTREE_TRACE_ARGBI(cur, bp, fields);
1234 if (bp) {
1235 xfs_btree_offsets(fields,
1236 (cur->bc_flags & XFS_BTREE_LONG_PTRS) ?
1237 loffsets : soffsets,
1238 XFS_BB_NUM_BITS, &first, &last);
1239 xfs_trans_log_buf(cur->bc_tp, bp, first, last);
1240 } else {
1241 xfs_trans_log_inode(cur->bc_tp, cur->bc_private.b.ip,
1242 xfs_ilog_fbroot(cur->bc_private.b.whichfork));
1245 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1249 * Increment cursor by one record at the level.
1250 * For nonzero levels the leaf-ward information is untouched.
1252 int /* error */
1253 xfs_btree_increment(
1254 struct xfs_btree_cur *cur,
1255 int level,
1256 int *stat) /* success/failure */
1258 struct xfs_btree_block *block;
1259 union xfs_btree_ptr ptr;
1260 struct xfs_buf *bp;
1261 int error; /* error return value */
1262 int lev;
1264 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1265 XFS_BTREE_TRACE_ARGI(cur, level);
1267 ASSERT(level < cur->bc_nlevels);
1269 /* Read-ahead to the right at this level. */
1270 xfs_btree_readahead(cur, level, XFS_BTCUR_RIGHTRA);
1272 /* Get a pointer to the btree block. */
1273 block = xfs_btree_get_block(cur, level, &bp);
1275 #ifdef DEBUG
1276 error = xfs_btree_check_block(cur, block, level, bp);
1277 if (error)
1278 goto error0;
1279 #endif
1281 /* We're done if we remain in the block after the increment. */
1282 if (++cur->bc_ptrs[level] <= xfs_btree_get_numrecs(block))
1283 goto out1;
1285 /* Fail if we just went off the right edge of the tree. */
1286 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_RIGHTSIB);
1287 if (xfs_btree_ptr_is_null(cur, &ptr))
1288 goto out0;
1290 XFS_BTREE_STATS_INC(cur, increment);
1293 * March up the tree incrementing pointers.
1294 * Stop when we don't go off the right edge of a block.
1296 for (lev = level + 1; lev < cur->bc_nlevels; lev++) {
1297 block = xfs_btree_get_block(cur, lev, &bp);
1299 #ifdef DEBUG
1300 error = xfs_btree_check_block(cur, block, lev, bp);
1301 if (error)
1302 goto error0;
1303 #endif
1305 if (++cur->bc_ptrs[lev] <= xfs_btree_get_numrecs(block))
1306 break;
1308 /* Read-ahead the right block for the next loop. */
1309 xfs_btree_readahead(cur, lev, XFS_BTCUR_RIGHTRA);
1313 * If we went off the root then we are either seriously
1314 * confused or have the tree root in an inode.
1316 if (lev == cur->bc_nlevels) {
1317 if (cur->bc_flags & XFS_BTREE_ROOT_IN_INODE)
1318 goto out0;
1319 ASSERT(0);
1320 error = EFSCORRUPTED;
1321 goto error0;
1323 ASSERT(lev < cur->bc_nlevels);
1326 * Now walk back down the tree, fixing up the cursor's buffer
1327 * pointers and key numbers.
1329 for (block = xfs_btree_get_block(cur, lev, &bp); lev > level; ) {
1330 union xfs_btree_ptr *ptrp;
1332 ptrp = xfs_btree_ptr_addr(cur, cur->bc_ptrs[lev], block);
1333 error = xfs_btree_read_buf_block(cur, ptrp, --lev,
1334 0, &block, &bp);
1335 if (error)
1336 goto error0;
1338 xfs_btree_setbuf(cur, lev, bp);
1339 cur->bc_ptrs[lev] = 1;
1341 out1:
1342 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1343 *stat = 1;
1344 return 0;
1346 out0:
1347 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1348 *stat = 0;
1349 return 0;
1351 error0:
1352 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
1353 return error;
1357 * Decrement cursor by one record at the level.
1358 * For nonzero levels the leaf-ward information is untouched.
1360 int /* error */
1361 xfs_btree_decrement(
1362 struct xfs_btree_cur *cur,
1363 int level,
1364 int *stat) /* success/failure */
1366 struct xfs_btree_block *block;
1367 xfs_buf_t *bp;
1368 int error; /* error return value */
1369 int lev;
1370 union xfs_btree_ptr ptr;
1372 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1373 XFS_BTREE_TRACE_ARGI(cur, level);
1375 ASSERT(level < cur->bc_nlevels);
1377 /* Read-ahead to the left at this level. */
1378 xfs_btree_readahead(cur, level, XFS_BTCUR_LEFTRA);
1380 /* We're done if we remain in the block after the decrement. */
1381 if (--cur->bc_ptrs[level] > 0)
1382 goto out1;
1384 /* Get a pointer to the btree block. */
1385 block = xfs_btree_get_block(cur, level, &bp);
1387 #ifdef DEBUG
1388 error = xfs_btree_check_block(cur, block, level, bp);
1389 if (error)
1390 goto error0;
1391 #endif
1393 /* Fail if we just went off the left edge of the tree. */
1394 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_LEFTSIB);
1395 if (xfs_btree_ptr_is_null(cur, &ptr))
1396 goto out0;
1398 XFS_BTREE_STATS_INC(cur, decrement);
1401 * March up the tree decrementing pointers.
1402 * Stop when we don't go off the left edge of a block.
1404 for (lev = level + 1; lev < cur->bc_nlevels; lev++) {
1405 if (--cur->bc_ptrs[lev] > 0)
1406 break;
1407 /* Read-ahead the left block for the next loop. */
1408 xfs_btree_readahead(cur, lev, XFS_BTCUR_LEFTRA);
1412 * If we went off the root then we are seriously confused.
1413 * or the root of the tree is in an inode.
1415 if (lev == cur->bc_nlevels) {
1416 if (cur->bc_flags & XFS_BTREE_ROOT_IN_INODE)
1417 goto out0;
1418 ASSERT(0);
1419 error = EFSCORRUPTED;
1420 goto error0;
1422 ASSERT(lev < cur->bc_nlevels);
1425 * Now walk back down the tree, fixing up the cursor's buffer
1426 * pointers and key numbers.
1428 for (block = xfs_btree_get_block(cur, lev, &bp); lev > level; ) {
1429 union xfs_btree_ptr *ptrp;
1431 ptrp = xfs_btree_ptr_addr(cur, cur->bc_ptrs[lev], block);
1432 error = xfs_btree_read_buf_block(cur, ptrp, --lev,
1433 0, &block, &bp);
1434 if (error)
1435 goto error0;
1436 xfs_btree_setbuf(cur, lev, bp);
1437 cur->bc_ptrs[lev] = xfs_btree_get_numrecs(block);
1439 out1:
1440 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1441 *stat = 1;
1442 return 0;
1444 out0:
1445 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1446 *stat = 0;
1447 return 0;
1449 error0:
1450 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
1451 return error;
1454 STATIC int
1455 xfs_btree_lookup_get_block(
1456 struct xfs_btree_cur *cur, /* btree cursor */
1457 int level, /* level in the btree */
1458 union xfs_btree_ptr *pp, /* ptr to btree block */
1459 struct xfs_btree_block **blkp) /* return btree block */
1461 struct xfs_buf *bp; /* buffer pointer for btree block */
1462 int error = 0;
1464 /* special case the root block if in an inode */
1465 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
1466 (level == cur->bc_nlevels - 1)) {
1467 *blkp = xfs_btree_get_iroot(cur);
1468 return 0;
1472 * If the old buffer at this level for the disk address we are
1473 * looking for re-use it.
1475 * Otherwise throw it away and get a new one.
1477 bp = cur->bc_bufs[level];
1478 if (bp && XFS_BUF_ADDR(bp) == xfs_btree_ptr_to_daddr(cur, pp)) {
1479 *blkp = XFS_BUF_TO_BLOCK(bp);
1480 return 0;
1483 error = xfs_btree_read_buf_block(cur, pp, level, 0, blkp, &bp);
1484 if (error)
1485 return error;
1487 xfs_btree_setbuf(cur, level, bp);
1488 return 0;
1492 * Get current search key. For level 0 we don't actually have a key
1493 * structure so we make one up from the record. For all other levels
1494 * we just return the right key.
1496 STATIC union xfs_btree_key *
1497 xfs_lookup_get_search_key(
1498 struct xfs_btree_cur *cur,
1499 int level,
1500 int keyno,
1501 struct xfs_btree_block *block,
1502 union xfs_btree_key *kp)
1504 if (level == 0) {
1505 cur->bc_ops->init_key_from_rec(kp,
1506 xfs_btree_rec_addr(cur, keyno, block));
1507 return kp;
1510 return xfs_btree_key_addr(cur, keyno, block);
1514 * Lookup the record. The cursor is made to point to it, based on dir.
1515 * Return 0 if can't find any such record, 1 for success.
1517 int /* error */
1518 xfs_btree_lookup(
1519 struct xfs_btree_cur *cur, /* btree cursor */
1520 xfs_lookup_t dir, /* <=, ==, or >= */
1521 int *stat) /* success/failure */
1523 struct xfs_btree_block *block; /* current btree block */
1524 __int64_t diff; /* difference for the current key */
1525 int error; /* error return value */
1526 int keyno; /* current key number */
1527 int level; /* level in the btree */
1528 union xfs_btree_ptr *pp; /* ptr to btree block */
1529 union xfs_btree_ptr ptr; /* ptr to btree block */
1531 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1532 XFS_BTREE_TRACE_ARGI(cur, dir);
1534 XFS_BTREE_STATS_INC(cur, lookup);
1536 block = NULL;
1537 keyno = 0;
1539 /* initialise start pointer from cursor */
1540 cur->bc_ops->init_ptr_from_cur(cur, &ptr);
1541 pp = &ptr;
1544 * Iterate over each level in the btree, starting at the root.
1545 * For each level above the leaves, find the key we need, based
1546 * on the lookup record, then follow the corresponding block
1547 * pointer down to the next level.
1549 for (level = cur->bc_nlevels - 1, diff = 1; level >= 0; level--) {
1550 /* Get the block we need to do the lookup on. */
1551 error = xfs_btree_lookup_get_block(cur, level, pp, &block);
1552 if (error)
1553 goto error0;
1555 if (diff == 0) {
1557 * If we already had a key match at a higher level, we
1558 * know we need to use the first entry in this block.
1560 keyno = 1;
1561 } else {
1562 /* Otherwise search this block. Do a binary search. */
1564 int high; /* high entry number */
1565 int low; /* low entry number */
1567 /* Set low and high entry numbers, 1-based. */
1568 low = 1;
1569 high = xfs_btree_get_numrecs(block);
1570 if (!high) {
1571 /* Block is empty, must be an empty leaf. */
1572 ASSERT(level == 0 && cur->bc_nlevels == 1);
1574 cur->bc_ptrs[0] = dir != XFS_LOOKUP_LE;
1575 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1576 *stat = 0;
1577 return 0;
1580 /* Binary search the block. */
1581 while (low <= high) {
1582 union xfs_btree_key key;
1583 union xfs_btree_key *kp;
1585 XFS_BTREE_STATS_INC(cur, compare);
1587 /* keyno is average of low and high. */
1588 keyno = (low + high) >> 1;
1590 /* Get current search key */
1591 kp = xfs_lookup_get_search_key(cur, level,
1592 keyno, block, &key);
1595 * Compute difference to get next direction:
1596 * - less than, move right
1597 * - greater than, move left
1598 * - equal, we're done
1600 diff = cur->bc_ops->key_diff(cur, kp);
1601 if (diff < 0)
1602 low = keyno + 1;
1603 else if (diff > 0)
1604 high = keyno - 1;
1605 else
1606 break;
1611 * If there are more levels, set up for the next level
1612 * by getting the block number and filling in the cursor.
1614 if (level > 0) {
1616 * If we moved left, need the previous key number,
1617 * unless there isn't one.
1619 if (diff > 0 && --keyno < 1)
1620 keyno = 1;
1621 pp = xfs_btree_ptr_addr(cur, keyno, block);
1623 #ifdef DEBUG
1624 error = xfs_btree_check_ptr(cur, pp, 0, level);
1625 if (error)
1626 goto error0;
1627 #endif
1628 cur->bc_ptrs[level] = keyno;
1632 /* Done with the search. See if we need to adjust the results. */
1633 if (dir != XFS_LOOKUP_LE && diff < 0) {
1634 keyno++;
1636 * If ge search and we went off the end of the block, but it's
1637 * not the last block, we're in the wrong block.
1639 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_RIGHTSIB);
1640 if (dir == XFS_LOOKUP_GE &&
1641 keyno > xfs_btree_get_numrecs(block) &&
1642 !xfs_btree_ptr_is_null(cur, &ptr)) {
1643 int i;
1645 cur->bc_ptrs[0] = keyno;
1646 error = xfs_btree_increment(cur, 0, &i);
1647 if (error)
1648 goto error0;
1649 XFS_WANT_CORRUPTED_RETURN(i == 1);
1650 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1651 *stat = 1;
1652 return 0;
1654 } else if (dir == XFS_LOOKUP_LE && diff > 0)
1655 keyno--;
1656 cur->bc_ptrs[0] = keyno;
1658 /* Return if we succeeded or not. */
1659 if (keyno == 0 || keyno > xfs_btree_get_numrecs(block))
1660 *stat = 0;
1661 else if (dir != XFS_LOOKUP_EQ || diff == 0)
1662 *stat = 1;
1663 else
1664 *stat = 0;
1665 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1666 return 0;
1668 error0:
1669 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
1670 return error;
1674 * Update keys at all levels from here to the root along the cursor's path.
1676 STATIC int
1677 xfs_btree_updkey(
1678 struct xfs_btree_cur *cur,
1679 union xfs_btree_key *keyp,
1680 int level)
1682 struct xfs_btree_block *block;
1683 struct xfs_buf *bp;
1684 union xfs_btree_key *kp;
1685 int ptr;
1687 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1688 XFS_BTREE_TRACE_ARGIK(cur, level, keyp);
1690 ASSERT(!(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) || level >= 1);
1693 * Go up the tree from this level toward the root.
1694 * At each level, update the key value to the value input.
1695 * Stop when we reach a level where the cursor isn't pointing
1696 * at the first entry in the block.
1698 for (ptr = 1; ptr == 1 && level < cur->bc_nlevels; level++) {
1699 #ifdef DEBUG
1700 int error;
1701 #endif
1702 block = xfs_btree_get_block(cur, level, &bp);
1703 #ifdef DEBUG
1704 error = xfs_btree_check_block(cur, block, level, bp);
1705 if (error) {
1706 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
1707 return error;
1709 #endif
1710 ptr = cur->bc_ptrs[level];
1711 kp = xfs_btree_key_addr(cur, ptr, block);
1712 xfs_btree_copy_keys(cur, kp, keyp, 1);
1713 xfs_btree_log_keys(cur, bp, ptr, ptr);
1716 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1717 return 0;
1721 * Update the record referred to by cur to the value in the
1722 * given record. This either works (return 0) or gets an
1723 * EFSCORRUPTED error.
1726 xfs_btree_update(
1727 struct xfs_btree_cur *cur,
1728 union xfs_btree_rec *rec)
1730 struct xfs_btree_block *block;
1731 struct xfs_buf *bp;
1732 int error;
1733 int ptr;
1734 union xfs_btree_rec *rp;
1736 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1737 XFS_BTREE_TRACE_ARGR(cur, rec);
1739 /* Pick up the current block. */
1740 block = xfs_btree_get_block(cur, 0, &bp);
1742 #ifdef DEBUG
1743 error = xfs_btree_check_block(cur, block, 0, bp);
1744 if (error)
1745 goto error0;
1746 #endif
1747 /* Get the address of the rec to be updated. */
1748 ptr = cur->bc_ptrs[0];
1749 rp = xfs_btree_rec_addr(cur, ptr, block);
1751 /* Fill in the new contents and log them. */
1752 xfs_btree_copy_recs(cur, rp, rec, 1);
1753 xfs_btree_log_recs(cur, bp, ptr, ptr);
1756 * If we are tracking the last record in the tree and
1757 * we are at the far right edge of the tree, update it.
1759 if (xfs_btree_is_lastrec(cur, block, 0)) {
1760 cur->bc_ops->update_lastrec(cur, block, rec,
1761 ptr, LASTREC_UPDATE);
1764 /* Updating first rec in leaf. Pass new key value up to our parent. */
1765 if (ptr == 1) {
1766 union xfs_btree_key key;
1768 cur->bc_ops->init_key_from_rec(&key, rec);
1769 error = xfs_btree_updkey(cur, &key, 1);
1770 if (error)
1771 goto error0;
1774 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1775 return 0;
1777 error0:
1778 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
1779 return error;
1783 * Move 1 record left from cur/level if possible.
1784 * Update cur to reflect the new path.
1786 STATIC int /* error */
1787 xfs_btree_lshift(
1788 struct xfs_btree_cur *cur,
1789 int level,
1790 int *stat) /* success/failure */
1792 union xfs_btree_key key; /* btree key */
1793 struct xfs_buf *lbp; /* left buffer pointer */
1794 struct xfs_btree_block *left; /* left btree block */
1795 int lrecs; /* left record count */
1796 struct xfs_buf *rbp; /* right buffer pointer */
1797 struct xfs_btree_block *right; /* right btree block */
1798 int rrecs; /* right record count */
1799 union xfs_btree_ptr lptr; /* left btree pointer */
1800 union xfs_btree_key *rkp = NULL; /* right btree key */
1801 union xfs_btree_ptr *rpp = NULL; /* right address pointer */
1802 union xfs_btree_rec *rrp = NULL; /* right record pointer */
1803 int error; /* error return value */
1805 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1806 XFS_BTREE_TRACE_ARGI(cur, level);
1808 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
1809 level == cur->bc_nlevels - 1)
1810 goto out0;
1812 /* Set up variables for this block as "right". */
1813 right = xfs_btree_get_block(cur, level, &rbp);
1815 #ifdef DEBUG
1816 error = xfs_btree_check_block(cur, right, level, rbp);
1817 if (error)
1818 goto error0;
1819 #endif
1821 /* If we've got no left sibling then we can't shift an entry left. */
1822 xfs_btree_get_sibling(cur, right, &lptr, XFS_BB_LEFTSIB);
1823 if (xfs_btree_ptr_is_null(cur, &lptr))
1824 goto out0;
1827 * If the cursor entry is the one that would be moved, don't
1828 * do it... it's too complicated.
1830 if (cur->bc_ptrs[level] <= 1)
1831 goto out0;
1833 /* Set up the left neighbor as "left". */
1834 error = xfs_btree_read_buf_block(cur, &lptr, level, 0, &left, &lbp);
1835 if (error)
1836 goto error0;
1838 /* If it's full, it can't take another entry. */
1839 lrecs = xfs_btree_get_numrecs(left);
1840 if (lrecs == cur->bc_ops->get_maxrecs(cur, level))
1841 goto out0;
1843 rrecs = xfs_btree_get_numrecs(right);
1846 * We add one entry to the left side and remove one for the right side.
1847 * Account for it here, the changes will be updated on disk and logged
1848 * later.
1850 lrecs++;
1851 rrecs--;
1853 XFS_BTREE_STATS_INC(cur, lshift);
1854 XFS_BTREE_STATS_ADD(cur, moves, 1);
1857 * If non-leaf, copy a key and a ptr to the left block.
1858 * Log the changes to the left block.
1860 if (level > 0) {
1861 /* It's a non-leaf. Move keys and pointers. */
1862 union xfs_btree_key *lkp; /* left btree key */
1863 union xfs_btree_ptr *lpp; /* left address pointer */
1865 lkp = xfs_btree_key_addr(cur, lrecs, left);
1866 rkp = xfs_btree_key_addr(cur, 1, right);
1868 lpp = xfs_btree_ptr_addr(cur, lrecs, left);
1869 rpp = xfs_btree_ptr_addr(cur, 1, right);
1870 #ifdef DEBUG
1871 error = xfs_btree_check_ptr(cur, rpp, 0, level);
1872 if (error)
1873 goto error0;
1874 #endif
1875 xfs_btree_copy_keys(cur, lkp, rkp, 1);
1876 xfs_btree_copy_ptrs(cur, lpp, rpp, 1);
1878 xfs_btree_log_keys(cur, lbp, lrecs, lrecs);
1879 xfs_btree_log_ptrs(cur, lbp, lrecs, lrecs);
1881 ASSERT(cur->bc_ops->keys_inorder(cur,
1882 xfs_btree_key_addr(cur, lrecs - 1, left), lkp));
1883 } else {
1884 /* It's a leaf. Move records. */
1885 union xfs_btree_rec *lrp; /* left record pointer */
1887 lrp = xfs_btree_rec_addr(cur, lrecs, left);
1888 rrp = xfs_btree_rec_addr(cur, 1, right);
1890 xfs_btree_copy_recs(cur, lrp, rrp, 1);
1891 xfs_btree_log_recs(cur, lbp, lrecs, lrecs);
1893 ASSERT(cur->bc_ops->recs_inorder(cur,
1894 xfs_btree_rec_addr(cur, lrecs - 1, left), lrp));
1897 xfs_btree_set_numrecs(left, lrecs);
1898 xfs_btree_log_block(cur, lbp, XFS_BB_NUMRECS);
1900 xfs_btree_set_numrecs(right, rrecs);
1901 xfs_btree_log_block(cur, rbp, XFS_BB_NUMRECS);
1904 * Slide the contents of right down one entry.
1906 XFS_BTREE_STATS_ADD(cur, moves, rrecs - 1);
1907 if (level > 0) {
1908 /* It's a nonleaf. operate on keys and ptrs */
1909 #ifdef DEBUG
1910 int i; /* loop index */
1912 for (i = 0; i < rrecs; i++) {
1913 error = xfs_btree_check_ptr(cur, rpp, i + 1, level);
1914 if (error)
1915 goto error0;
1917 #endif
1918 xfs_btree_shift_keys(cur,
1919 xfs_btree_key_addr(cur, 2, right),
1920 -1, rrecs);
1921 xfs_btree_shift_ptrs(cur,
1922 xfs_btree_ptr_addr(cur, 2, right),
1923 -1, rrecs);
1925 xfs_btree_log_keys(cur, rbp, 1, rrecs);
1926 xfs_btree_log_ptrs(cur, rbp, 1, rrecs);
1927 } else {
1928 /* It's a leaf. operate on records */
1929 xfs_btree_shift_recs(cur,
1930 xfs_btree_rec_addr(cur, 2, right),
1931 -1, rrecs);
1932 xfs_btree_log_recs(cur, rbp, 1, rrecs);
1935 * If it's the first record in the block, we'll need a key
1936 * structure to pass up to the next level (updkey).
1938 cur->bc_ops->init_key_from_rec(&key,
1939 xfs_btree_rec_addr(cur, 1, right));
1940 rkp = &key;
1943 /* Update the parent key values of right. */
1944 error = xfs_btree_updkey(cur, rkp, level + 1);
1945 if (error)
1946 goto error0;
1948 /* Slide the cursor value left one. */
1949 cur->bc_ptrs[level]--;
1951 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1952 *stat = 1;
1953 return 0;
1955 out0:
1956 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1957 *stat = 0;
1958 return 0;
1960 error0:
1961 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
1962 return error;
1966 * Move 1 record right from cur/level if possible.
1967 * Update cur to reflect the new path.
1969 STATIC int /* error */
1970 xfs_btree_rshift(
1971 struct xfs_btree_cur *cur,
1972 int level,
1973 int *stat) /* success/failure */
1975 union xfs_btree_key key; /* btree key */
1976 struct xfs_buf *lbp; /* left buffer pointer */
1977 struct xfs_btree_block *left; /* left btree block */
1978 struct xfs_buf *rbp; /* right buffer pointer */
1979 struct xfs_btree_block *right; /* right btree block */
1980 struct xfs_btree_cur *tcur; /* temporary btree cursor */
1981 union xfs_btree_ptr rptr; /* right block pointer */
1982 union xfs_btree_key *rkp; /* right btree key */
1983 int rrecs; /* right record count */
1984 int lrecs; /* left record count */
1985 int error; /* error return value */
1986 int i; /* loop counter */
1988 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1989 XFS_BTREE_TRACE_ARGI(cur, level);
1991 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
1992 (level == cur->bc_nlevels - 1))
1993 goto out0;
1995 /* Set up variables for this block as "left". */
1996 left = xfs_btree_get_block(cur, level, &lbp);
1998 #ifdef DEBUG
1999 error = xfs_btree_check_block(cur, left, level, lbp);
2000 if (error)
2001 goto error0;
2002 #endif
2004 /* If we've got no right sibling then we can't shift an entry right. */
2005 xfs_btree_get_sibling(cur, left, &rptr, XFS_BB_RIGHTSIB);
2006 if (xfs_btree_ptr_is_null(cur, &rptr))
2007 goto out0;
2010 * If the cursor entry is the one that would be moved, don't
2011 * do it... it's too complicated.
2013 lrecs = xfs_btree_get_numrecs(left);
2014 if (cur->bc_ptrs[level] >= lrecs)
2015 goto out0;
2017 /* Set up the right neighbor as "right". */
2018 error = xfs_btree_read_buf_block(cur, &rptr, level, 0, &right, &rbp);
2019 if (error)
2020 goto error0;
2022 /* If it's full, it can't take another entry. */
2023 rrecs = xfs_btree_get_numrecs(right);
2024 if (rrecs == cur->bc_ops->get_maxrecs(cur, level))
2025 goto out0;
2027 XFS_BTREE_STATS_INC(cur, rshift);
2028 XFS_BTREE_STATS_ADD(cur, moves, rrecs);
2031 * Make a hole at the start of the right neighbor block, then
2032 * copy the last left block entry to the hole.
2034 if (level > 0) {
2035 /* It's a nonleaf. make a hole in the keys and ptrs */
2036 union xfs_btree_key *lkp;
2037 union xfs_btree_ptr *lpp;
2038 union xfs_btree_ptr *rpp;
2040 lkp = xfs_btree_key_addr(cur, lrecs, left);
2041 lpp = xfs_btree_ptr_addr(cur, lrecs, left);
2042 rkp = xfs_btree_key_addr(cur, 1, right);
2043 rpp = xfs_btree_ptr_addr(cur, 1, right);
2045 #ifdef DEBUG
2046 for (i = rrecs - 1; i >= 0; i--) {
2047 error = xfs_btree_check_ptr(cur, rpp, i, level);
2048 if (error)
2049 goto error0;
2051 #endif
2053 xfs_btree_shift_keys(cur, rkp, 1, rrecs);
2054 xfs_btree_shift_ptrs(cur, rpp, 1, rrecs);
2056 #ifdef DEBUG
2057 error = xfs_btree_check_ptr(cur, lpp, 0, level);
2058 if (error)
2059 goto error0;
2060 #endif
2062 /* Now put the new data in, and log it. */
2063 xfs_btree_copy_keys(cur, rkp, lkp, 1);
2064 xfs_btree_copy_ptrs(cur, rpp, lpp, 1);
2066 xfs_btree_log_keys(cur, rbp, 1, rrecs + 1);
2067 xfs_btree_log_ptrs(cur, rbp, 1, rrecs + 1);
2069 ASSERT(cur->bc_ops->keys_inorder(cur, rkp,
2070 xfs_btree_key_addr(cur, 2, right)));
2071 } else {
2072 /* It's a leaf. make a hole in the records */
2073 union xfs_btree_rec *lrp;
2074 union xfs_btree_rec *rrp;
2076 lrp = xfs_btree_rec_addr(cur, lrecs, left);
2077 rrp = xfs_btree_rec_addr(cur, 1, right);
2079 xfs_btree_shift_recs(cur, rrp, 1, rrecs);
2081 /* Now put the new data in, and log it. */
2082 xfs_btree_copy_recs(cur, rrp, lrp, 1);
2083 xfs_btree_log_recs(cur, rbp, 1, rrecs + 1);
2085 cur->bc_ops->init_key_from_rec(&key, rrp);
2086 rkp = &key;
2088 ASSERT(cur->bc_ops->recs_inorder(cur, rrp,
2089 xfs_btree_rec_addr(cur, 2, right)));
2093 * Decrement and log left's numrecs, bump and log right's numrecs.
2095 xfs_btree_set_numrecs(left, --lrecs);
2096 xfs_btree_log_block(cur, lbp, XFS_BB_NUMRECS);
2098 xfs_btree_set_numrecs(right, ++rrecs);
2099 xfs_btree_log_block(cur, rbp, XFS_BB_NUMRECS);
2102 * Using a temporary cursor, update the parent key values of the
2103 * block on the right.
2105 error = xfs_btree_dup_cursor(cur, &tcur);
2106 if (error)
2107 goto error0;
2108 i = xfs_btree_lastrec(tcur, level);
2109 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
2111 error = xfs_btree_increment(tcur, level, &i);
2112 if (error)
2113 goto error1;
2115 error = xfs_btree_updkey(tcur, rkp, level + 1);
2116 if (error)
2117 goto error1;
2119 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
2121 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2122 *stat = 1;
2123 return 0;
2125 out0:
2126 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2127 *stat = 0;
2128 return 0;
2130 error0:
2131 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
2132 return error;
2134 error1:
2135 XFS_BTREE_TRACE_CURSOR(tcur, XBT_ERROR);
2136 xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
2137 return error;
2141 * Split cur/level block in half.
2142 * Return new block number and the key to its first
2143 * record (to be inserted into parent).
2145 STATIC int /* error */
2146 xfs_btree_split(
2147 struct xfs_btree_cur *cur,
2148 int level,
2149 union xfs_btree_ptr *ptrp,
2150 union xfs_btree_key *key,
2151 struct xfs_btree_cur **curp,
2152 int *stat) /* success/failure */
2154 union xfs_btree_ptr lptr; /* left sibling block ptr */
2155 struct xfs_buf *lbp; /* left buffer pointer */
2156 struct xfs_btree_block *left; /* left btree block */
2157 union xfs_btree_ptr rptr; /* right sibling block ptr */
2158 struct xfs_buf *rbp; /* right buffer pointer */
2159 struct xfs_btree_block *right; /* right btree block */
2160 union xfs_btree_ptr rrptr; /* right-right sibling ptr */
2161 struct xfs_buf *rrbp; /* right-right buffer pointer */
2162 struct xfs_btree_block *rrblock; /* right-right btree block */
2163 int lrecs;
2164 int rrecs;
2165 int src_index;
2166 int error; /* error return value */
2167 #ifdef DEBUG
2168 int i;
2169 #endif
2171 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
2172 XFS_BTREE_TRACE_ARGIPK(cur, level, *ptrp, key);
2174 XFS_BTREE_STATS_INC(cur, split);
2176 /* Set up left block (current one). */
2177 left = xfs_btree_get_block(cur, level, &lbp);
2179 #ifdef DEBUG
2180 error = xfs_btree_check_block(cur, left, level, lbp);
2181 if (error)
2182 goto error0;
2183 #endif
2185 xfs_btree_buf_to_ptr(cur, lbp, &lptr);
2187 /* Allocate the new block. If we can't do it, we're toast. Give up. */
2188 error = cur->bc_ops->alloc_block(cur, &lptr, &rptr, 1, stat);
2189 if (error)
2190 goto error0;
2191 if (*stat == 0)
2192 goto out0;
2193 XFS_BTREE_STATS_INC(cur, alloc);
2195 /* Set up the new block as "right". */
2196 error = xfs_btree_get_buf_block(cur, &rptr, 0, &right, &rbp);
2197 if (error)
2198 goto error0;
2200 /* Fill in the btree header for the new right block. */
2201 xfs_btree_init_block(cur, xfs_btree_get_level(left), 0, right);
2204 * Split the entries between the old and the new block evenly.
2205 * Make sure that if there's an odd number of entries now, that
2206 * each new block will have the same number of entries.
2208 lrecs = xfs_btree_get_numrecs(left);
2209 rrecs = lrecs / 2;
2210 if ((lrecs & 1) && cur->bc_ptrs[level] <= rrecs + 1)
2211 rrecs++;
2212 src_index = (lrecs - rrecs + 1);
2214 XFS_BTREE_STATS_ADD(cur, moves, rrecs);
2217 * Copy btree block entries from the left block over to the
2218 * new block, the right. Update the right block and log the
2219 * changes.
2221 if (level > 0) {
2222 /* It's a non-leaf. Move keys and pointers. */
2223 union xfs_btree_key *lkp; /* left btree key */
2224 union xfs_btree_ptr *lpp; /* left address pointer */
2225 union xfs_btree_key *rkp; /* right btree key */
2226 union xfs_btree_ptr *rpp; /* right address pointer */
2228 lkp = xfs_btree_key_addr(cur, src_index, left);
2229 lpp = xfs_btree_ptr_addr(cur, src_index, left);
2230 rkp = xfs_btree_key_addr(cur, 1, right);
2231 rpp = xfs_btree_ptr_addr(cur, 1, right);
2233 #ifdef DEBUG
2234 for (i = src_index; i < rrecs; i++) {
2235 error = xfs_btree_check_ptr(cur, lpp, i, level);
2236 if (error)
2237 goto error0;
2239 #endif
2241 xfs_btree_copy_keys(cur, rkp, lkp, rrecs);
2242 xfs_btree_copy_ptrs(cur, rpp, lpp, rrecs);
2244 xfs_btree_log_keys(cur, rbp, 1, rrecs);
2245 xfs_btree_log_ptrs(cur, rbp, 1, rrecs);
2247 /* Grab the keys to the entries moved to the right block */
2248 xfs_btree_copy_keys(cur, key, rkp, 1);
2249 } else {
2250 /* It's a leaf. Move records. */
2251 union xfs_btree_rec *lrp; /* left record pointer */
2252 union xfs_btree_rec *rrp; /* right record pointer */
2254 lrp = xfs_btree_rec_addr(cur, src_index, left);
2255 rrp = xfs_btree_rec_addr(cur, 1, right);
2257 xfs_btree_copy_recs(cur, rrp, lrp, rrecs);
2258 xfs_btree_log_recs(cur, rbp, 1, rrecs);
2260 cur->bc_ops->init_key_from_rec(key,
2261 xfs_btree_rec_addr(cur, 1, right));
2266 * Find the left block number by looking in the buffer.
2267 * Adjust numrecs, sibling pointers.
2269 xfs_btree_get_sibling(cur, left, &rrptr, XFS_BB_RIGHTSIB);
2270 xfs_btree_set_sibling(cur, right, &rrptr, XFS_BB_RIGHTSIB);
2271 xfs_btree_set_sibling(cur, right, &lptr, XFS_BB_LEFTSIB);
2272 xfs_btree_set_sibling(cur, left, &rptr, XFS_BB_RIGHTSIB);
2274 lrecs -= rrecs;
2275 xfs_btree_set_numrecs(left, lrecs);
2276 xfs_btree_set_numrecs(right, xfs_btree_get_numrecs(right) + rrecs);
2278 xfs_btree_log_block(cur, rbp, XFS_BB_ALL_BITS);
2279 xfs_btree_log_block(cur, lbp, XFS_BB_NUMRECS | XFS_BB_RIGHTSIB);
2282 * If there's a block to the new block's right, make that block
2283 * point back to right instead of to left.
2285 if (!xfs_btree_ptr_is_null(cur, &rrptr)) {
2286 error = xfs_btree_read_buf_block(cur, &rrptr, level,
2287 0, &rrblock, &rrbp);
2288 if (error)
2289 goto error0;
2290 xfs_btree_set_sibling(cur, rrblock, &rptr, XFS_BB_LEFTSIB);
2291 xfs_btree_log_block(cur, rrbp, XFS_BB_LEFTSIB);
2294 * If the cursor is really in the right block, move it there.
2295 * If it's just pointing past the last entry in left, then we'll
2296 * insert there, so don't change anything in that case.
2298 if (cur->bc_ptrs[level] > lrecs + 1) {
2299 xfs_btree_setbuf(cur, level, rbp);
2300 cur->bc_ptrs[level] -= lrecs;
2303 * If there are more levels, we'll need another cursor which refers
2304 * the right block, no matter where this cursor was.
2306 if (level + 1 < cur->bc_nlevels) {
2307 error = xfs_btree_dup_cursor(cur, curp);
2308 if (error)
2309 goto error0;
2310 (*curp)->bc_ptrs[level + 1]++;
2312 *ptrp = rptr;
2313 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2314 *stat = 1;
2315 return 0;
2316 out0:
2317 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2318 *stat = 0;
2319 return 0;
2321 error0:
2322 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
2323 return error;
2327 * Copy the old inode root contents into a real block and make the
2328 * broot point to it.
2330 int /* error */
2331 xfs_btree_new_iroot(
2332 struct xfs_btree_cur *cur, /* btree cursor */
2333 int *logflags, /* logging flags for inode */
2334 int *stat) /* return status - 0 fail */
2336 struct xfs_buf *cbp; /* buffer for cblock */
2337 struct xfs_btree_block *block; /* btree block */
2338 struct xfs_btree_block *cblock; /* child btree block */
2339 union xfs_btree_key *ckp; /* child key pointer */
2340 union xfs_btree_ptr *cpp; /* child ptr pointer */
2341 union xfs_btree_key *kp; /* pointer to btree key */
2342 union xfs_btree_ptr *pp; /* pointer to block addr */
2343 union xfs_btree_ptr nptr; /* new block addr */
2344 int level; /* btree level */
2345 int error; /* error return code */
2346 #ifdef DEBUG
2347 int i; /* loop counter */
2348 #endif
2350 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
2351 XFS_BTREE_STATS_INC(cur, newroot);
2353 ASSERT(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE);
2355 level = cur->bc_nlevels - 1;
2357 block = xfs_btree_get_iroot(cur);
2358 pp = xfs_btree_ptr_addr(cur, 1, block);
2360 /* Allocate the new block. If we can't do it, we're toast. Give up. */
2361 error = cur->bc_ops->alloc_block(cur, pp, &nptr, 1, stat);
2362 if (error)
2363 goto error0;
2364 if (*stat == 0) {
2365 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2366 return 0;
2368 XFS_BTREE_STATS_INC(cur, alloc);
2370 /* Copy the root into a real block. */
2371 error = xfs_btree_get_buf_block(cur, &nptr, 0, &cblock, &cbp);
2372 if (error)
2373 goto error0;
2375 memcpy(cblock, block, xfs_btree_block_len(cur));
2377 be16_add_cpu(&block->bb_level, 1);
2378 xfs_btree_set_numrecs(block, 1);
2379 cur->bc_nlevels++;
2380 cur->bc_ptrs[level + 1] = 1;
2382 kp = xfs_btree_key_addr(cur, 1, block);
2383 ckp = xfs_btree_key_addr(cur, 1, cblock);
2384 xfs_btree_copy_keys(cur, ckp, kp, xfs_btree_get_numrecs(cblock));
2386 cpp = xfs_btree_ptr_addr(cur, 1, cblock);
2387 #ifdef DEBUG
2388 for (i = 0; i < be16_to_cpu(cblock->bb_numrecs); i++) {
2389 error = xfs_btree_check_ptr(cur, pp, i, level);
2390 if (error)
2391 goto error0;
2393 #endif
2394 xfs_btree_copy_ptrs(cur, cpp, pp, xfs_btree_get_numrecs(cblock));
2396 #ifdef DEBUG
2397 error = xfs_btree_check_ptr(cur, &nptr, 0, level);
2398 if (error)
2399 goto error0;
2400 #endif
2401 xfs_btree_copy_ptrs(cur, pp, &nptr, 1);
2403 xfs_iroot_realloc(cur->bc_private.b.ip,
2404 1 - xfs_btree_get_numrecs(cblock),
2405 cur->bc_private.b.whichfork);
2407 xfs_btree_setbuf(cur, level, cbp);
2410 * Do all this logging at the end so that
2411 * the root is at the right level.
2413 xfs_btree_log_block(cur, cbp, XFS_BB_ALL_BITS);
2414 xfs_btree_log_keys(cur, cbp, 1, be16_to_cpu(cblock->bb_numrecs));
2415 xfs_btree_log_ptrs(cur, cbp, 1, be16_to_cpu(cblock->bb_numrecs));
2417 *logflags |=
2418 XFS_ILOG_CORE | xfs_ilog_fbroot(cur->bc_private.b.whichfork);
2419 *stat = 1;
2420 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2421 return 0;
2422 error0:
2423 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
2424 return error;
2428 * Allocate a new root block, fill it in.
2430 STATIC int /* error */
2431 xfs_btree_new_root(
2432 struct xfs_btree_cur *cur, /* btree cursor */
2433 int *stat) /* success/failure */
2435 struct xfs_btree_block *block; /* one half of the old root block */
2436 struct xfs_buf *bp; /* buffer containing block */
2437 int error; /* error return value */
2438 struct xfs_buf *lbp; /* left buffer pointer */
2439 struct xfs_btree_block *left; /* left btree block */
2440 struct xfs_buf *nbp; /* new (root) buffer */
2441 struct xfs_btree_block *new; /* new (root) btree block */
2442 int nptr; /* new value for key index, 1 or 2 */
2443 struct xfs_buf *rbp; /* right buffer pointer */
2444 struct xfs_btree_block *right; /* right btree block */
2445 union xfs_btree_ptr rptr;
2446 union xfs_btree_ptr lptr;
2448 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
2449 XFS_BTREE_STATS_INC(cur, newroot);
2451 /* initialise our start point from the cursor */
2452 cur->bc_ops->init_ptr_from_cur(cur, &rptr);
2454 /* Allocate the new block. If we can't do it, we're toast. Give up. */
2455 error = cur->bc_ops->alloc_block(cur, &rptr, &lptr, 1, stat);
2456 if (error)
2457 goto error0;
2458 if (*stat == 0)
2459 goto out0;
2460 XFS_BTREE_STATS_INC(cur, alloc);
2462 /* Set up the new block. */
2463 error = xfs_btree_get_buf_block(cur, &lptr, 0, &new, &nbp);
2464 if (error)
2465 goto error0;
2467 /* Set the root in the holding structure increasing the level by 1. */
2468 cur->bc_ops->set_root(cur, &lptr, 1);
2471 * At the previous root level there are now two blocks: the old root,
2472 * and the new block generated when it was split. We don't know which
2473 * one the cursor is pointing at, so we set up variables "left" and
2474 * "right" for each case.
2476 block = xfs_btree_get_block(cur, cur->bc_nlevels - 1, &bp);
2478 #ifdef DEBUG
2479 error = xfs_btree_check_block(cur, block, cur->bc_nlevels - 1, bp);
2480 if (error)
2481 goto error0;
2482 #endif
2484 xfs_btree_get_sibling(cur, block, &rptr, XFS_BB_RIGHTSIB);
2485 if (!xfs_btree_ptr_is_null(cur, &rptr)) {
2486 /* Our block is left, pick up the right block. */
2487 lbp = bp;
2488 xfs_btree_buf_to_ptr(cur, lbp, &lptr);
2489 left = block;
2490 error = xfs_btree_read_buf_block(cur, &rptr,
2491 cur->bc_nlevels - 1, 0, &right, &rbp);
2492 if (error)
2493 goto error0;
2494 bp = rbp;
2495 nptr = 1;
2496 } else {
2497 /* Our block is right, pick up the left block. */
2498 rbp = bp;
2499 xfs_btree_buf_to_ptr(cur, rbp, &rptr);
2500 right = block;
2501 xfs_btree_get_sibling(cur, right, &lptr, XFS_BB_LEFTSIB);
2502 error = xfs_btree_read_buf_block(cur, &lptr,
2503 cur->bc_nlevels - 1, 0, &left, &lbp);
2504 if (error)
2505 goto error0;
2506 bp = lbp;
2507 nptr = 2;
2509 /* Fill in the new block's btree header and log it. */
2510 xfs_btree_init_block(cur, cur->bc_nlevels, 2, new);
2511 xfs_btree_log_block(cur, nbp, XFS_BB_ALL_BITS);
2512 ASSERT(!xfs_btree_ptr_is_null(cur, &lptr) &&
2513 !xfs_btree_ptr_is_null(cur, &rptr));
2515 /* Fill in the key data in the new root. */
2516 if (xfs_btree_get_level(left) > 0) {
2517 xfs_btree_copy_keys(cur,
2518 xfs_btree_key_addr(cur, 1, new),
2519 xfs_btree_key_addr(cur, 1, left), 1);
2520 xfs_btree_copy_keys(cur,
2521 xfs_btree_key_addr(cur, 2, new),
2522 xfs_btree_key_addr(cur, 1, right), 1);
2523 } else {
2524 cur->bc_ops->init_key_from_rec(
2525 xfs_btree_key_addr(cur, 1, new),
2526 xfs_btree_rec_addr(cur, 1, left));
2527 cur->bc_ops->init_key_from_rec(
2528 xfs_btree_key_addr(cur, 2, new),
2529 xfs_btree_rec_addr(cur, 1, right));
2531 xfs_btree_log_keys(cur, nbp, 1, 2);
2533 /* Fill in the pointer data in the new root. */
2534 xfs_btree_copy_ptrs(cur,
2535 xfs_btree_ptr_addr(cur, 1, new), &lptr, 1);
2536 xfs_btree_copy_ptrs(cur,
2537 xfs_btree_ptr_addr(cur, 2, new), &rptr, 1);
2538 xfs_btree_log_ptrs(cur, nbp, 1, 2);
2540 /* Fix up the cursor. */
2541 xfs_btree_setbuf(cur, cur->bc_nlevels, nbp);
2542 cur->bc_ptrs[cur->bc_nlevels] = nptr;
2543 cur->bc_nlevels++;
2544 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2545 *stat = 1;
2546 return 0;
2547 error0:
2548 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
2549 return error;
2550 out0:
2551 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2552 *stat = 0;
2553 return 0;
2556 STATIC int
2557 xfs_btree_make_block_unfull(
2558 struct xfs_btree_cur *cur, /* btree cursor */
2559 int level, /* btree level */
2560 int numrecs,/* # of recs in block */
2561 int *oindex,/* old tree index */
2562 int *index, /* new tree index */
2563 union xfs_btree_ptr *nptr, /* new btree ptr */
2564 struct xfs_btree_cur **ncur, /* new btree cursor */
2565 union xfs_btree_rec *nrec, /* new record */
2566 int *stat)
2568 union xfs_btree_key key; /* new btree key value */
2569 int error = 0;
2571 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
2572 level == cur->bc_nlevels - 1) {
2573 struct xfs_inode *ip = cur->bc_private.b.ip;
2575 if (numrecs < cur->bc_ops->get_dmaxrecs(cur, level)) {
2576 /* A root block that can be made bigger. */
2578 xfs_iroot_realloc(ip, 1, cur->bc_private.b.whichfork);
2579 } else {
2580 /* A root block that needs replacing */
2581 int logflags = 0;
2583 error = xfs_btree_new_iroot(cur, &logflags, stat);
2584 if (error || *stat == 0)
2585 return error;
2587 xfs_trans_log_inode(cur->bc_tp, ip, logflags);
2590 return 0;
2593 /* First, try shifting an entry to the right neighbor. */
2594 error = xfs_btree_rshift(cur, level, stat);
2595 if (error || *stat)
2596 return error;
2598 /* Next, try shifting an entry to the left neighbor. */
2599 error = xfs_btree_lshift(cur, level, stat);
2600 if (error)
2601 return error;
2603 if (*stat) {
2604 *oindex = *index = cur->bc_ptrs[level];
2605 return 0;
2609 * Next, try splitting the current block in half.
2611 * If this works we have to re-set our variables because we
2612 * could be in a different block now.
2614 error = xfs_btree_split(cur, level, nptr, &key, ncur, stat);
2615 if (error || *stat == 0)
2616 return error;
2619 *index = cur->bc_ptrs[level];
2620 cur->bc_ops->init_rec_from_key(&key, nrec);
2621 return 0;
2625 * Insert one record/level. Return information to the caller
2626 * allowing the next level up to proceed if necessary.
2628 STATIC int
2629 xfs_btree_insrec(
2630 struct xfs_btree_cur *cur, /* btree cursor */
2631 int level, /* level to insert record at */
2632 union xfs_btree_ptr *ptrp, /* i/o: block number inserted */
2633 union xfs_btree_rec *recp, /* i/o: record data inserted */
2634 struct xfs_btree_cur **curp, /* output: new cursor replacing cur */
2635 int *stat) /* success/failure */
2637 struct xfs_btree_block *block; /* btree block */
2638 struct xfs_buf *bp; /* buffer for block */
2639 union xfs_btree_key key; /* btree key */
2640 union xfs_btree_ptr nptr; /* new block ptr */
2641 struct xfs_btree_cur *ncur; /* new btree cursor */
2642 union xfs_btree_rec nrec; /* new record count */
2643 int optr; /* old key/record index */
2644 int ptr; /* key/record index */
2645 int numrecs;/* number of records */
2646 int error; /* error return value */
2647 #ifdef DEBUG
2648 int i;
2649 #endif
2651 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
2652 XFS_BTREE_TRACE_ARGIPR(cur, level, *ptrp, recp);
2654 ncur = NULL;
2657 * If we have an external root pointer, and we've made it to the
2658 * root level, allocate a new root block and we're done.
2660 if (!(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
2661 (level >= cur->bc_nlevels)) {
2662 error = xfs_btree_new_root(cur, stat);
2663 xfs_btree_set_ptr_null(cur, ptrp);
2665 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2666 return error;
2669 /* If we're off the left edge, return failure. */
2670 ptr = cur->bc_ptrs[level];
2671 if (ptr == 0) {
2672 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2673 *stat = 0;
2674 return 0;
2677 /* Make a key out of the record data to be inserted, and save it. */
2678 cur->bc_ops->init_key_from_rec(&key, recp);
2680 optr = ptr;
2682 XFS_BTREE_STATS_INC(cur, insrec);
2684 /* Get pointers to the btree buffer and block. */
2685 block = xfs_btree_get_block(cur, level, &bp);
2686 numrecs = xfs_btree_get_numrecs(block);
2688 #ifdef DEBUG
2689 error = xfs_btree_check_block(cur, block, level, bp);
2690 if (error)
2691 goto error0;
2693 /* Check that the new entry is being inserted in the right place. */
2694 if (ptr <= numrecs) {
2695 if (level == 0) {
2696 ASSERT(cur->bc_ops->recs_inorder(cur, recp,
2697 xfs_btree_rec_addr(cur, ptr, block)));
2698 } else {
2699 ASSERT(cur->bc_ops->keys_inorder(cur, &key,
2700 xfs_btree_key_addr(cur, ptr, block)));
2703 #endif
2706 * If the block is full, we can't insert the new entry until we
2707 * make the block un-full.
2709 xfs_btree_set_ptr_null(cur, &nptr);
2710 if (numrecs == cur->bc_ops->get_maxrecs(cur, level)) {
2711 error = xfs_btree_make_block_unfull(cur, level, numrecs,
2712 &optr, &ptr, &nptr, &ncur, &nrec, stat);
2713 if (error || *stat == 0)
2714 goto error0;
2718 * The current block may have changed if the block was
2719 * previously full and we have just made space in it.
2721 block = xfs_btree_get_block(cur, level, &bp);
2722 numrecs = xfs_btree_get_numrecs(block);
2724 #ifdef DEBUG
2725 error = xfs_btree_check_block(cur, block, level, bp);
2726 if (error)
2727 return error;
2728 #endif
2731 * At this point we know there's room for our new entry in the block
2732 * we're pointing at.
2734 XFS_BTREE_STATS_ADD(cur, moves, numrecs - ptr + 1);
2736 if (level > 0) {
2737 /* It's a nonleaf. make a hole in the keys and ptrs */
2738 union xfs_btree_key *kp;
2739 union xfs_btree_ptr *pp;
2741 kp = xfs_btree_key_addr(cur, ptr, block);
2742 pp = xfs_btree_ptr_addr(cur, ptr, block);
2744 #ifdef DEBUG
2745 for (i = numrecs - ptr; i >= 0; i--) {
2746 error = xfs_btree_check_ptr(cur, pp, i, level);
2747 if (error)
2748 return error;
2750 #endif
2752 xfs_btree_shift_keys(cur, kp, 1, numrecs - ptr + 1);
2753 xfs_btree_shift_ptrs(cur, pp, 1, numrecs - ptr + 1);
2755 #ifdef DEBUG
2756 error = xfs_btree_check_ptr(cur, ptrp, 0, level);
2757 if (error)
2758 goto error0;
2759 #endif
2761 /* Now put the new data in, bump numrecs and log it. */
2762 xfs_btree_copy_keys(cur, kp, &key, 1);
2763 xfs_btree_copy_ptrs(cur, pp, ptrp, 1);
2764 numrecs++;
2765 xfs_btree_set_numrecs(block, numrecs);
2766 xfs_btree_log_ptrs(cur, bp, ptr, numrecs);
2767 xfs_btree_log_keys(cur, bp, ptr, numrecs);
2768 #ifdef DEBUG
2769 if (ptr < numrecs) {
2770 ASSERT(cur->bc_ops->keys_inorder(cur, kp,
2771 xfs_btree_key_addr(cur, ptr + 1, block)));
2773 #endif
2774 } else {
2775 /* It's a leaf. make a hole in the records */
2776 union xfs_btree_rec *rp;
2778 rp = xfs_btree_rec_addr(cur, ptr, block);
2780 xfs_btree_shift_recs(cur, rp, 1, numrecs - ptr + 1);
2782 /* Now put the new data in, bump numrecs and log it. */
2783 xfs_btree_copy_recs(cur, rp, recp, 1);
2784 xfs_btree_set_numrecs(block, ++numrecs);
2785 xfs_btree_log_recs(cur, bp, ptr, numrecs);
2786 #ifdef DEBUG
2787 if (ptr < numrecs) {
2788 ASSERT(cur->bc_ops->recs_inorder(cur, rp,
2789 xfs_btree_rec_addr(cur, ptr + 1, block)));
2791 #endif
2794 /* Log the new number of records in the btree header. */
2795 xfs_btree_log_block(cur, bp, XFS_BB_NUMRECS);
2797 /* If we inserted at the start of a block, update the parents' keys. */
2798 if (optr == 1) {
2799 error = xfs_btree_updkey(cur, &key, level + 1);
2800 if (error)
2801 goto error0;
2805 * If we are tracking the last record in the tree and
2806 * we are at the far right edge of the tree, update it.
2808 if (xfs_btree_is_lastrec(cur, block, level)) {
2809 cur->bc_ops->update_lastrec(cur, block, recp,
2810 ptr, LASTREC_INSREC);
2814 * Return the new block number, if any.
2815 * If there is one, give back a record value and a cursor too.
2817 *ptrp = nptr;
2818 if (!xfs_btree_ptr_is_null(cur, &nptr)) {
2819 *recp = nrec;
2820 *curp = ncur;
2823 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2824 *stat = 1;
2825 return 0;
2827 error0:
2828 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
2829 return error;
2833 * Insert the record at the point referenced by cur.
2835 * A multi-level split of the tree on insert will invalidate the original
2836 * cursor. All callers of this function should assume that the cursor is
2837 * no longer valid and revalidate it.
2840 xfs_btree_insert(
2841 struct xfs_btree_cur *cur,
2842 int *stat)
2844 int error; /* error return value */
2845 int i; /* result value, 0 for failure */
2846 int level; /* current level number in btree */
2847 union xfs_btree_ptr nptr; /* new block number (split result) */
2848 struct xfs_btree_cur *ncur; /* new cursor (split result) */
2849 struct xfs_btree_cur *pcur; /* previous level's cursor */
2850 union xfs_btree_rec rec; /* record to insert */
2852 level = 0;
2853 ncur = NULL;
2854 pcur = cur;
2856 xfs_btree_set_ptr_null(cur, &nptr);
2857 cur->bc_ops->init_rec_from_cur(cur, &rec);
2860 * Loop going up the tree, starting at the leaf level.
2861 * Stop when we don't get a split block, that must mean that
2862 * the insert is finished with this level.
2864 do {
2866 * Insert nrec/nptr into this level of the tree.
2867 * Note if we fail, nptr will be null.
2869 error = xfs_btree_insrec(pcur, level, &nptr, &rec, &ncur, &i);
2870 if (error) {
2871 if (pcur != cur)
2872 xfs_btree_del_cursor(pcur, XFS_BTREE_ERROR);
2873 goto error0;
2876 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
2877 level++;
2880 * See if the cursor we just used is trash.
2881 * Can't trash the caller's cursor, but otherwise we should
2882 * if ncur is a new cursor or we're about to be done.
2884 if (pcur != cur &&
2885 (ncur || xfs_btree_ptr_is_null(cur, &nptr))) {
2886 /* Save the state from the cursor before we trash it */
2887 if (cur->bc_ops->update_cursor)
2888 cur->bc_ops->update_cursor(pcur, cur);
2889 cur->bc_nlevels = pcur->bc_nlevels;
2890 xfs_btree_del_cursor(pcur, XFS_BTREE_NOERROR);
2892 /* If we got a new cursor, switch to it. */
2893 if (ncur) {
2894 pcur = ncur;
2895 ncur = NULL;
2897 } while (!xfs_btree_ptr_is_null(cur, &nptr));
2899 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2900 *stat = i;
2901 return 0;
2902 error0:
2903 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
2904 return error;
2908 * Try to merge a non-leaf block back into the inode root.
2910 * Note: the killroot names comes from the fact that we're effectively
2911 * killing the old root block. But because we can't just delete the
2912 * inode we have to copy the single block it was pointing to into the
2913 * inode.
2915 STATIC int
2916 xfs_btree_kill_iroot(
2917 struct xfs_btree_cur *cur)
2919 int whichfork = cur->bc_private.b.whichfork;
2920 struct xfs_inode *ip = cur->bc_private.b.ip;
2921 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
2922 struct xfs_btree_block *block;
2923 struct xfs_btree_block *cblock;
2924 union xfs_btree_key *kp;
2925 union xfs_btree_key *ckp;
2926 union xfs_btree_ptr *pp;
2927 union xfs_btree_ptr *cpp;
2928 struct xfs_buf *cbp;
2929 int level;
2930 int index;
2931 int numrecs;
2932 #ifdef DEBUG
2933 union xfs_btree_ptr ptr;
2934 int i;
2935 #endif
2937 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
2939 ASSERT(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE);
2940 ASSERT(cur->bc_nlevels > 1);
2943 * Don't deal with the root block needs to be a leaf case.
2944 * We're just going to turn the thing back into extents anyway.
2946 level = cur->bc_nlevels - 1;
2947 if (level == 1)
2948 goto out0;
2951 * Give up if the root has multiple children.
2953 block = xfs_btree_get_iroot(cur);
2954 if (xfs_btree_get_numrecs(block) != 1)
2955 goto out0;
2957 cblock = xfs_btree_get_block(cur, level - 1, &cbp);
2958 numrecs = xfs_btree_get_numrecs(cblock);
2961 * Only do this if the next level will fit.
2962 * Then the data must be copied up to the inode,
2963 * instead of freeing the root you free the next level.
2965 if (numrecs > cur->bc_ops->get_dmaxrecs(cur, level))
2966 goto out0;
2968 XFS_BTREE_STATS_INC(cur, killroot);
2970 #ifdef DEBUG
2971 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_LEFTSIB);
2972 ASSERT(xfs_btree_ptr_is_null(cur, &ptr));
2973 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_RIGHTSIB);
2974 ASSERT(xfs_btree_ptr_is_null(cur, &ptr));
2975 #endif
2977 index = numrecs - cur->bc_ops->get_maxrecs(cur, level);
2978 if (index) {
2979 xfs_iroot_realloc(cur->bc_private.b.ip, index,
2980 cur->bc_private.b.whichfork);
2981 block = ifp->if_broot;
2984 be16_add_cpu(&block->bb_numrecs, index);
2985 ASSERT(block->bb_numrecs == cblock->bb_numrecs);
2987 kp = xfs_btree_key_addr(cur, 1, block);
2988 ckp = xfs_btree_key_addr(cur, 1, cblock);
2989 xfs_btree_copy_keys(cur, kp, ckp, numrecs);
2991 pp = xfs_btree_ptr_addr(cur, 1, block);
2992 cpp = xfs_btree_ptr_addr(cur, 1, cblock);
2993 #ifdef DEBUG
2994 for (i = 0; i < numrecs; i++) {
2995 int error;
2997 error = xfs_btree_check_ptr(cur, cpp, i, level - 1);
2998 if (error) {
2999 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
3000 return error;
3003 #endif
3004 xfs_btree_copy_ptrs(cur, pp, cpp, numrecs);
3006 cur->bc_ops->free_block(cur, cbp);
3007 XFS_BTREE_STATS_INC(cur, free);
3009 cur->bc_bufs[level - 1] = NULL;
3010 be16_add_cpu(&block->bb_level, -1);
3011 xfs_trans_log_inode(cur->bc_tp, ip,
3012 XFS_ILOG_CORE | xfs_ilog_fbroot(cur->bc_private.b.whichfork));
3013 cur->bc_nlevels--;
3014 out0:
3015 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3016 return 0;
3019 STATIC int
3020 xfs_btree_dec_cursor(
3021 struct xfs_btree_cur *cur,
3022 int level,
3023 int *stat)
3025 int error;
3026 int i;
3028 if (level > 0) {
3029 error = xfs_btree_decrement(cur, level, &i);
3030 if (error)
3031 return error;
3034 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3035 *stat = 1;
3036 return 0;
3040 * Single level of the btree record deletion routine.
3041 * Delete record pointed to by cur/level.
3042 * Remove the record from its block then rebalance the tree.
3043 * Return 0 for error, 1 for done, 2 to go on to the next level.
3045 STATIC int /* error */
3046 xfs_btree_delrec(
3047 struct xfs_btree_cur *cur, /* btree cursor */
3048 int level, /* level removing record from */
3049 int *stat) /* fail/done/go-on */
3051 struct xfs_btree_block *block; /* btree block */
3052 union xfs_btree_ptr cptr; /* current block ptr */
3053 struct xfs_buf *bp; /* buffer for block */
3054 int error; /* error return value */
3055 int i; /* loop counter */
3056 union xfs_btree_key key; /* storage for keyp */
3057 union xfs_btree_key *keyp = &key; /* passed to the next level */
3058 union xfs_btree_ptr lptr; /* left sibling block ptr */
3059 struct xfs_buf *lbp; /* left buffer pointer */
3060 struct xfs_btree_block *left; /* left btree block */
3061 int lrecs = 0; /* left record count */
3062 int ptr; /* key/record index */
3063 union xfs_btree_ptr rptr; /* right sibling block ptr */
3064 struct xfs_buf *rbp; /* right buffer pointer */
3065 struct xfs_btree_block *right; /* right btree block */
3066 struct xfs_btree_block *rrblock; /* right-right btree block */
3067 struct xfs_buf *rrbp; /* right-right buffer pointer */
3068 int rrecs = 0; /* right record count */
3069 struct xfs_btree_cur *tcur; /* temporary btree cursor */
3070 int numrecs; /* temporary numrec count */
3072 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
3073 XFS_BTREE_TRACE_ARGI(cur, level);
3075 tcur = NULL;
3077 /* Get the index of the entry being deleted, check for nothing there. */
3078 ptr = cur->bc_ptrs[level];
3079 if (ptr == 0) {
3080 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3081 *stat = 0;
3082 return 0;
3085 /* Get the buffer & block containing the record or key/ptr. */
3086 block = xfs_btree_get_block(cur, level, &bp);
3087 numrecs = xfs_btree_get_numrecs(block);
3089 #ifdef DEBUG
3090 error = xfs_btree_check_block(cur, block, level, bp);
3091 if (error)
3092 goto error0;
3093 #endif
3095 /* Fail if we're off the end of the block. */
3096 if (ptr > numrecs) {
3097 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3098 *stat = 0;
3099 return 0;
3102 XFS_BTREE_STATS_INC(cur, delrec);
3103 XFS_BTREE_STATS_ADD(cur, moves, numrecs - ptr);
3105 /* Excise the entries being deleted. */
3106 if (level > 0) {
3107 /* It's a nonleaf. operate on keys and ptrs */
3108 union xfs_btree_key *lkp;
3109 union xfs_btree_ptr *lpp;
3111 lkp = xfs_btree_key_addr(cur, ptr + 1, block);
3112 lpp = xfs_btree_ptr_addr(cur, ptr + 1, block);
3114 #ifdef DEBUG
3115 for (i = 0; i < numrecs - ptr; i++) {
3116 error = xfs_btree_check_ptr(cur, lpp, i, level);
3117 if (error)
3118 goto error0;
3120 #endif
3122 if (ptr < numrecs) {
3123 xfs_btree_shift_keys(cur, lkp, -1, numrecs - ptr);
3124 xfs_btree_shift_ptrs(cur, lpp, -1, numrecs - ptr);
3125 xfs_btree_log_keys(cur, bp, ptr, numrecs - 1);
3126 xfs_btree_log_ptrs(cur, bp, ptr, numrecs - 1);
3130 * If it's the first record in the block, we'll need to pass a
3131 * key up to the next level (updkey).
3133 if (ptr == 1)
3134 keyp = xfs_btree_key_addr(cur, 1, block);
3135 } else {
3136 /* It's a leaf. operate on records */
3137 if (ptr < numrecs) {
3138 xfs_btree_shift_recs(cur,
3139 xfs_btree_rec_addr(cur, ptr + 1, block),
3140 -1, numrecs - ptr);
3141 xfs_btree_log_recs(cur, bp, ptr, numrecs - 1);
3145 * If it's the first record in the block, we'll need a key
3146 * structure to pass up to the next level (updkey).
3148 if (ptr == 1) {
3149 cur->bc_ops->init_key_from_rec(&key,
3150 xfs_btree_rec_addr(cur, 1, block));
3151 keyp = &key;
3156 * Decrement and log the number of entries in the block.
3158 xfs_btree_set_numrecs(block, --numrecs);
3159 xfs_btree_log_block(cur, bp, XFS_BB_NUMRECS);
3162 * If we are tracking the last record in the tree and
3163 * we are at the far right edge of the tree, update it.
3165 if (xfs_btree_is_lastrec(cur, block, level)) {
3166 cur->bc_ops->update_lastrec(cur, block, NULL,
3167 ptr, LASTREC_DELREC);
3171 * We're at the root level. First, shrink the root block in-memory.
3172 * Try to get rid of the next level down. If we can't then there's
3173 * nothing left to do.
3175 if (level == cur->bc_nlevels - 1) {
3176 if (cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) {
3177 xfs_iroot_realloc(cur->bc_private.b.ip, -1,
3178 cur->bc_private.b.whichfork);
3180 error = xfs_btree_kill_iroot(cur);
3181 if (error)
3182 goto error0;
3184 error = xfs_btree_dec_cursor(cur, level, stat);
3185 if (error)
3186 goto error0;
3187 *stat = 1;
3188 return 0;
3192 * If this is the root level, and there's only one entry left,
3193 * and it's NOT the leaf level, then we can get rid of this
3194 * level.
3196 if (numrecs == 1 && level > 0) {
3197 union xfs_btree_ptr *pp;
3199 * pp is still set to the first pointer in the block.
3200 * Make it the new root of the btree.
3202 pp = xfs_btree_ptr_addr(cur, 1, block);
3203 error = cur->bc_ops->kill_root(cur, bp, level, pp);
3204 if (error)
3205 goto error0;
3206 } else if (level > 0) {
3207 error = xfs_btree_dec_cursor(cur, level, stat);
3208 if (error)
3209 goto error0;
3211 *stat = 1;
3212 return 0;
3216 * If we deleted the leftmost entry in the block, update the
3217 * key values above us in the tree.
3219 if (ptr == 1) {
3220 error = xfs_btree_updkey(cur, keyp, level + 1);
3221 if (error)
3222 goto error0;
3226 * If the number of records remaining in the block is at least
3227 * the minimum, we're done.
3229 if (numrecs >= cur->bc_ops->get_minrecs(cur, level)) {
3230 error = xfs_btree_dec_cursor(cur, level, stat);
3231 if (error)
3232 goto error0;
3233 return 0;
3237 * Otherwise, we have to move some records around to keep the
3238 * tree balanced. Look at the left and right sibling blocks to
3239 * see if we can re-balance by moving only one record.
3241 xfs_btree_get_sibling(cur, block, &rptr, XFS_BB_RIGHTSIB);
3242 xfs_btree_get_sibling(cur, block, &lptr, XFS_BB_LEFTSIB);
3244 if (cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) {
3246 * One child of root, need to get a chance to copy its contents
3247 * into the root and delete it. Can't go up to next level,
3248 * there's nothing to delete there.
3250 if (xfs_btree_ptr_is_null(cur, &rptr) &&
3251 xfs_btree_ptr_is_null(cur, &lptr) &&
3252 level == cur->bc_nlevels - 2) {
3253 error = xfs_btree_kill_iroot(cur);
3254 if (!error)
3255 error = xfs_btree_dec_cursor(cur, level, stat);
3256 if (error)
3257 goto error0;
3258 return 0;
3262 ASSERT(!xfs_btree_ptr_is_null(cur, &rptr) ||
3263 !xfs_btree_ptr_is_null(cur, &lptr));
3266 * Duplicate the cursor so our btree manipulations here won't
3267 * disrupt the next level up.
3269 error = xfs_btree_dup_cursor(cur, &tcur);
3270 if (error)
3271 goto error0;
3274 * If there's a right sibling, see if it's ok to shift an entry
3275 * out of it.
3277 if (!xfs_btree_ptr_is_null(cur, &rptr)) {
3279 * Move the temp cursor to the last entry in the next block.
3280 * Actually any entry but the first would suffice.
3282 i = xfs_btree_lastrec(tcur, level);
3283 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
3285 error = xfs_btree_increment(tcur, level, &i);
3286 if (error)
3287 goto error0;
3288 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
3290 i = xfs_btree_lastrec(tcur, level);
3291 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
3293 /* Grab a pointer to the block. */
3294 right = xfs_btree_get_block(tcur, level, &rbp);
3295 #ifdef DEBUG
3296 error = xfs_btree_check_block(tcur, right, level, rbp);
3297 if (error)
3298 goto error0;
3299 #endif
3300 /* Grab the current block number, for future use. */
3301 xfs_btree_get_sibling(tcur, right, &cptr, XFS_BB_LEFTSIB);
3304 * If right block is full enough so that removing one entry
3305 * won't make it too empty, and left-shifting an entry out
3306 * of right to us works, we're done.
3308 if (xfs_btree_get_numrecs(right) - 1 >=
3309 cur->bc_ops->get_minrecs(tcur, level)) {
3310 error = xfs_btree_lshift(tcur, level, &i);
3311 if (error)
3312 goto error0;
3313 if (i) {
3314 ASSERT(xfs_btree_get_numrecs(block) >=
3315 cur->bc_ops->get_minrecs(tcur, level));
3317 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
3318 tcur = NULL;
3320 error = xfs_btree_dec_cursor(cur, level, stat);
3321 if (error)
3322 goto error0;
3323 return 0;
3328 * Otherwise, grab the number of records in right for
3329 * future reference, and fix up the temp cursor to point
3330 * to our block again (last record).
3332 rrecs = xfs_btree_get_numrecs(right);
3333 if (!xfs_btree_ptr_is_null(cur, &lptr)) {
3334 i = xfs_btree_firstrec(tcur, level);
3335 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
3337 error = xfs_btree_decrement(tcur, level, &i);
3338 if (error)
3339 goto error0;
3340 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
3345 * If there's a left sibling, see if it's ok to shift an entry
3346 * out of it.
3348 if (!xfs_btree_ptr_is_null(cur, &lptr)) {
3350 * Move the temp cursor to the first entry in the
3351 * previous block.
3353 i = xfs_btree_firstrec(tcur, level);
3354 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
3356 error = xfs_btree_decrement(tcur, level, &i);
3357 if (error)
3358 goto error0;
3359 i = xfs_btree_firstrec(tcur, level);
3360 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
3362 /* Grab a pointer to the block. */
3363 left = xfs_btree_get_block(tcur, level, &lbp);
3364 #ifdef DEBUG
3365 error = xfs_btree_check_block(cur, left, level, lbp);
3366 if (error)
3367 goto error0;
3368 #endif
3369 /* Grab the current block number, for future use. */
3370 xfs_btree_get_sibling(tcur, left, &cptr, XFS_BB_RIGHTSIB);
3373 * If left block is full enough so that removing one entry
3374 * won't make it too empty, and right-shifting an entry out
3375 * of left to us works, we're done.
3377 if (xfs_btree_get_numrecs(left) - 1 >=
3378 cur->bc_ops->get_minrecs(tcur, level)) {
3379 error = xfs_btree_rshift(tcur, level, &i);
3380 if (error)
3381 goto error0;
3382 if (i) {
3383 ASSERT(xfs_btree_get_numrecs(block) >=
3384 cur->bc_ops->get_minrecs(tcur, level));
3385 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
3386 tcur = NULL;
3387 if (level == 0)
3388 cur->bc_ptrs[0]++;
3389 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3390 *stat = 1;
3391 return 0;
3396 * Otherwise, grab the number of records in right for
3397 * future reference.
3399 lrecs = xfs_btree_get_numrecs(left);
3402 /* Delete the temp cursor, we're done with it. */
3403 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
3404 tcur = NULL;
3406 /* If here, we need to do a join to keep the tree balanced. */
3407 ASSERT(!xfs_btree_ptr_is_null(cur, &cptr));
3409 if (!xfs_btree_ptr_is_null(cur, &lptr) &&
3410 lrecs + xfs_btree_get_numrecs(block) <=
3411 cur->bc_ops->get_maxrecs(cur, level)) {
3413 * Set "right" to be the starting block,
3414 * "left" to be the left neighbor.
3416 rptr = cptr;
3417 right = block;
3418 rbp = bp;
3419 error = xfs_btree_read_buf_block(cur, &lptr, level,
3420 0, &left, &lbp);
3421 if (error)
3422 goto error0;
3425 * If that won't work, see if we can join with the right neighbor block.
3427 } else if (!xfs_btree_ptr_is_null(cur, &rptr) &&
3428 rrecs + xfs_btree_get_numrecs(block) <=
3429 cur->bc_ops->get_maxrecs(cur, level)) {
3431 * Set "left" to be the starting block,
3432 * "right" to be the right neighbor.
3434 lptr = cptr;
3435 left = block;
3436 lbp = bp;
3437 error = xfs_btree_read_buf_block(cur, &rptr, level,
3438 0, &right, &rbp);
3439 if (error)
3440 goto error0;
3443 * Otherwise, we can't fix the imbalance.
3444 * Just return. This is probably a logic error, but it's not fatal.
3446 } else {
3447 error = xfs_btree_dec_cursor(cur, level, stat);
3448 if (error)
3449 goto error0;
3450 return 0;
3453 rrecs = xfs_btree_get_numrecs(right);
3454 lrecs = xfs_btree_get_numrecs(left);
3457 * We're now going to join "left" and "right" by moving all the stuff
3458 * in "right" to "left" and deleting "right".
3460 XFS_BTREE_STATS_ADD(cur, moves, rrecs);
3461 if (level > 0) {
3462 /* It's a non-leaf. Move keys and pointers. */
3463 union xfs_btree_key *lkp; /* left btree key */
3464 union xfs_btree_ptr *lpp; /* left address pointer */
3465 union xfs_btree_key *rkp; /* right btree key */
3466 union xfs_btree_ptr *rpp; /* right address pointer */
3468 lkp = xfs_btree_key_addr(cur, lrecs + 1, left);
3469 lpp = xfs_btree_ptr_addr(cur, lrecs + 1, left);
3470 rkp = xfs_btree_key_addr(cur, 1, right);
3471 rpp = xfs_btree_ptr_addr(cur, 1, right);
3472 #ifdef DEBUG
3473 for (i = 1; i < rrecs; i++) {
3474 error = xfs_btree_check_ptr(cur, rpp, i, level);
3475 if (error)
3476 goto error0;
3478 #endif
3479 xfs_btree_copy_keys(cur, lkp, rkp, rrecs);
3480 xfs_btree_copy_ptrs(cur, lpp, rpp, rrecs);
3482 xfs_btree_log_keys(cur, lbp, lrecs + 1, lrecs + rrecs);
3483 xfs_btree_log_ptrs(cur, lbp, lrecs + 1, lrecs + rrecs);
3484 } else {
3485 /* It's a leaf. Move records. */
3486 union xfs_btree_rec *lrp; /* left record pointer */
3487 union xfs_btree_rec *rrp; /* right record pointer */
3489 lrp = xfs_btree_rec_addr(cur, lrecs + 1, left);
3490 rrp = xfs_btree_rec_addr(cur, 1, right);
3492 xfs_btree_copy_recs(cur, lrp, rrp, rrecs);
3493 xfs_btree_log_recs(cur, lbp, lrecs + 1, lrecs + rrecs);
3496 XFS_BTREE_STATS_INC(cur, join);
3499 * Fix up the number of records and right block pointer in the
3500 * surviving block, and log it.
3502 xfs_btree_set_numrecs(left, lrecs + rrecs);
3503 xfs_btree_get_sibling(cur, right, &cptr, XFS_BB_RIGHTSIB),
3504 xfs_btree_set_sibling(cur, left, &cptr, XFS_BB_RIGHTSIB);
3505 xfs_btree_log_block(cur, lbp, XFS_BB_NUMRECS | XFS_BB_RIGHTSIB);
3507 /* If there is a right sibling, point it to the remaining block. */
3508 xfs_btree_get_sibling(cur, left, &cptr, XFS_BB_RIGHTSIB);
3509 if (!xfs_btree_ptr_is_null(cur, &cptr)) {
3510 error = xfs_btree_read_buf_block(cur, &cptr, level,
3511 0, &rrblock, &rrbp);
3512 if (error)
3513 goto error0;
3514 xfs_btree_set_sibling(cur, rrblock, &lptr, XFS_BB_LEFTSIB);
3515 xfs_btree_log_block(cur, rrbp, XFS_BB_LEFTSIB);
3518 /* Free the deleted block. */
3519 error = cur->bc_ops->free_block(cur, rbp);
3520 if (error)
3521 goto error0;
3522 XFS_BTREE_STATS_INC(cur, free);
3525 * If we joined with the left neighbor, set the buffer in the
3526 * cursor to the left block, and fix up the index.
3528 if (bp != lbp) {
3529 cur->bc_bufs[level] = lbp;
3530 cur->bc_ptrs[level] += lrecs;
3531 cur->bc_ra[level] = 0;
3534 * If we joined with the right neighbor and there's a level above
3535 * us, increment the cursor at that level.
3537 else if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) ||
3538 (level + 1 < cur->bc_nlevels)) {
3539 error = xfs_btree_increment(cur, level + 1, &i);
3540 if (error)
3541 goto error0;
3545 * Readjust the ptr at this level if it's not a leaf, since it's
3546 * still pointing at the deletion point, which makes the cursor
3547 * inconsistent. If this makes the ptr 0, the caller fixes it up.
3548 * We can't use decrement because it would change the next level up.
3550 if (level > 0)
3551 cur->bc_ptrs[level]--;
3553 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3554 /* Return value means the next level up has something to do. */
3555 *stat = 2;
3556 return 0;
3558 error0:
3559 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
3560 if (tcur)
3561 xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
3562 return error;
3566 * Delete the record pointed to by cur.
3567 * The cursor refers to the place where the record was (could be inserted)
3568 * when the operation returns.
3570 int /* error */
3571 xfs_btree_delete(
3572 struct xfs_btree_cur *cur,
3573 int *stat) /* success/failure */
3575 int error; /* error return value */
3576 int level;
3577 int i;
3579 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
3582 * Go up the tree, starting at leaf level.
3584 * If 2 is returned then a join was done; go to the next level.
3585 * Otherwise we are done.
3587 for (level = 0, i = 2; i == 2; level++) {
3588 error = xfs_btree_delrec(cur, level, &i);
3589 if (error)
3590 goto error0;
3593 if (i == 0) {
3594 for (level = 1; level < cur->bc_nlevels; level++) {
3595 if (cur->bc_ptrs[level] == 0) {
3596 error = xfs_btree_decrement(cur, level, &i);
3597 if (error)
3598 goto error0;
3599 break;
3604 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3605 *stat = i;
3606 return 0;
3607 error0:
3608 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
3609 return error;
3613 * Get the data from the pointed-to record.
3615 int /* error */
3616 xfs_btree_get_rec(
3617 struct xfs_btree_cur *cur, /* btree cursor */
3618 union xfs_btree_rec **recp, /* output: btree record */
3619 int *stat) /* output: success/failure */
3621 struct xfs_btree_block *block; /* btree block */
3622 struct xfs_buf *bp; /* buffer pointer */
3623 int ptr; /* record number */
3624 #ifdef DEBUG
3625 int error; /* error return value */
3626 #endif
3628 ptr = cur->bc_ptrs[0];
3629 block = xfs_btree_get_block(cur, 0, &bp);
3631 #ifdef DEBUG
3632 error = xfs_btree_check_block(cur, block, 0, bp);
3633 if (error)
3634 return error;
3635 #endif
3638 * Off the right end or left end, return failure.
3640 if (ptr > xfs_btree_get_numrecs(block) || ptr <= 0) {
3641 *stat = 0;
3642 return 0;
3646 * Point to the record and extract its data.
3648 *recp = xfs_btree_rec_addr(cur, ptr, block);
3649 *stat = 1;
3650 return 0;