2 * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 #include "xfs_types.h"
23 #include "xfs_trans.h"
26 #include "xfs_mount.h"
27 #include "xfs_bmap_btree.h"
28 #include "xfs_alloc_btree.h"
29 #include "xfs_ialloc_btree.h"
30 #include "xfs_dinode.h"
31 #include "xfs_inode.h"
32 #include "xfs_inode_item.h"
33 #include "xfs_buf_item.h"
34 #include "xfs_btree.h"
35 #include "xfs_error.h"
36 #include "xfs_trace.h"
37 #include "xfs_cksum.h"
40 * Cursor allocation zone.
42 kmem_zone_t *xfs_btree_cur_zone;
45 * Btree magic numbers.
47 static const __uint32_t xfs_magics[2][XFS_BTNUM_MAX] = {
48 { XFS_ABTB_MAGIC, XFS_ABTC_MAGIC, XFS_BMAP_MAGIC, XFS_IBT_MAGIC },
49 { XFS_ABTB_CRC_MAGIC, XFS_ABTC_CRC_MAGIC,
50 XFS_BMAP_CRC_MAGIC, XFS_IBT_CRC_MAGIC }
52 #define xfs_btree_magic(cur) \
53 xfs_magics[!!((cur)->bc_flags & XFS_BTREE_CRC_BLOCKS)][cur->bc_btnum]
56 STATIC int /* error (0 or EFSCORRUPTED) */
57 xfs_btree_check_lblock(
58 struct xfs_btree_cur *cur, /* btree cursor */
59 struct xfs_btree_block *block, /* btree long form block pointer */
60 int level, /* level of the btree block */
61 struct xfs_buf *bp) /* buffer for block, if any */
63 int lblock_ok = 1; /* block passes checks */
64 struct xfs_mount *mp; /* file system mount point */
68 if (xfs_sb_version_hascrc(&mp->m_sb)) {
69 lblock_ok = lblock_ok &&
70 uuid_equal(&block->bb_u.l.bb_uuid, &mp->m_sb.sb_uuid) &&
71 block->bb_u.l.bb_blkno == cpu_to_be64(
72 bp ? bp->b_bn : XFS_BUF_DADDR_NULL);
75 lblock_ok = lblock_ok &&
76 be32_to_cpu(block->bb_magic) == xfs_btree_magic(cur) &&
77 be16_to_cpu(block->bb_level) == level &&
78 be16_to_cpu(block->bb_numrecs) <=
79 cur->bc_ops->get_maxrecs(cur, level) &&
80 block->bb_u.l.bb_leftsib &&
81 (block->bb_u.l.bb_leftsib == cpu_to_be64(NULLDFSBNO) ||
82 XFS_FSB_SANITY_CHECK(mp,
83 be64_to_cpu(block->bb_u.l.bb_leftsib))) &&
84 block->bb_u.l.bb_rightsib &&
85 (block->bb_u.l.bb_rightsib == cpu_to_be64(NULLDFSBNO) ||
86 XFS_FSB_SANITY_CHECK(mp,
87 be64_to_cpu(block->bb_u.l.bb_rightsib)));
89 if (unlikely(XFS_TEST_ERROR(!lblock_ok, mp,
90 XFS_ERRTAG_BTREE_CHECK_LBLOCK,
91 XFS_RANDOM_BTREE_CHECK_LBLOCK))) {
93 trace_xfs_btree_corrupt(bp, _RET_IP_);
94 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp);
95 return XFS_ERROR(EFSCORRUPTED);
100 STATIC int /* error (0 or EFSCORRUPTED) */
101 xfs_btree_check_sblock(
102 struct xfs_btree_cur *cur, /* btree cursor */
103 struct xfs_btree_block *block, /* btree short form block pointer */
104 int level, /* level of the btree block */
105 struct xfs_buf *bp) /* buffer containing block */
107 struct xfs_mount *mp; /* file system mount point */
108 struct xfs_buf *agbp; /* buffer for ag. freespace struct */
109 struct xfs_agf *agf; /* ag. freespace structure */
110 xfs_agblock_t agflen; /* native ag. freespace length */
111 int sblock_ok = 1; /* block passes checks */
114 agbp = cur->bc_private.a.agbp;
115 agf = XFS_BUF_TO_AGF(agbp);
116 agflen = be32_to_cpu(agf->agf_length);
118 if (xfs_sb_version_hascrc(&mp->m_sb)) {
119 sblock_ok = sblock_ok &&
120 uuid_equal(&block->bb_u.s.bb_uuid, &mp->m_sb.sb_uuid) &&
121 block->bb_u.s.bb_blkno == cpu_to_be64(
122 bp ? bp->b_bn : XFS_BUF_DADDR_NULL);
125 sblock_ok = sblock_ok &&
126 be32_to_cpu(block->bb_magic) == xfs_btree_magic(cur) &&
127 be16_to_cpu(block->bb_level) == level &&
128 be16_to_cpu(block->bb_numrecs) <=
129 cur->bc_ops->get_maxrecs(cur, level) &&
130 (block->bb_u.s.bb_leftsib == cpu_to_be32(NULLAGBLOCK) ||
131 be32_to_cpu(block->bb_u.s.bb_leftsib) < agflen) &&
132 block->bb_u.s.bb_leftsib &&
133 (block->bb_u.s.bb_rightsib == cpu_to_be32(NULLAGBLOCK) ||
134 be32_to_cpu(block->bb_u.s.bb_rightsib) < agflen) &&
135 block->bb_u.s.bb_rightsib;
137 if (unlikely(XFS_TEST_ERROR(!sblock_ok, mp,
138 XFS_ERRTAG_BTREE_CHECK_SBLOCK,
139 XFS_RANDOM_BTREE_CHECK_SBLOCK))) {
141 trace_xfs_btree_corrupt(bp, _RET_IP_);
142 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp);
143 return XFS_ERROR(EFSCORRUPTED);
149 * Debug routine: check that block header is ok.
152 xfs_btree_check_block(
153 struct xfs_btree_cur *cur, /* btree cursor */
154 struct xfs_btree_block *block, /* generic btree block pointer */
155 int level, /* level of the btree block */
156 struct xfs_buf *bp) /* buffer containing block, if any */
158 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
159 return xfs_btree_check_lblock(cur, block, level, bp);
161 return xfs_btree_check_sblock(cur, block, level, bp);
165 * Check that (long) pointer is ok.
167 int /* error (0 or EFSCORRUPTED) */
168 xfs_btree_check_lptr(
169 struct xfs_btree_cur *cur, /* btree cursor */
170 xfs_dfsbno_t bno, /* btree block disk address */
171 int level) /* btree block level */
173 XFS_WANT_CORRUPTED_RETURN(
176 XFS_FSB_SANITY_CHECK(cur->bc_mp, bno));
182 * Check that (short) pointer is ok.
184 STATIC int /* error (0 or EFSCORRUPTED) */
185 xfs_btree_check_sptr(
186 struct xfs_btree_cur *cur, /* btree cursor */
187 xfs_agblock_t bno, /* btree block disk address */
188 int level) /* btree block level */
190 xfs_agblock_t agblocks = cur->bc_mp->m_sb.sb_agblocks;
192 XFS_WANT_CORRUPTED_RETURN(
194 bno != NULLAGBLOCK &&
201 * Check that block ptr is ok.
203 STATIC int /* error (0 or EFSCORRUPTED) */
205 struct xfs_btree_cur *cur, /* btree cursor */
206 union xfs_btree_ptr *ptr, /* btree block disk address */
207 int index, /* offset from ptr to check */
208 int level) /* btree block level */
210 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
211 return xfs_btree_check_lptr(cur,
212 be64_to_cpu((&ptr->l)[index]), level);
214 return xfs_btree_check_sptr(cur,
215 be32_to_cpu((&ptr->s)[index]), level);
221 * Calculate CRC on the whole btree block and stuff it into the
222 * long-form btree header.
224 * Prior to calculting the CRC, pull the LSN out of the buffer log item and put
225 * it into the buffer so recovery knows what the last modifcation was that made
229 xfs_btree_lblock_calc_crc(
232 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
233 struct xfs_buf_log_item *bip = bp->b_fspriv;
235 if (!xfs_sb_version_hascrc(&bp->b_target->bt_mount->m_sb))
238 block->bb_u.l.bb_lsn = cpu_to_be64(bip->bli_item.li_lsn);
239 xfs_update_cksum(bp->b_addr, BBTOB(bp->b_length),
240 XFS_BTREE_LBLOCK_CRC_OFF);
244 xfs_btree_lblock_verify_crc(
247 if (xfs_sb_version_hascrc(&bp->b_target->bt_mount->m_sb))
248 return xfs_verify_cksum(bp->b_addr, BBTOB(bp->b_length),
249 XFS_BTREE_LBLOCK_CRC_OFF);
254 * Calculate CRC on the whole btree block and stuff it into the
255 * short-form btree header.
257 * Prior to calculting the CRC, pull the LSN out of the buffer log item and put
258 * it into the buffer so recovery knows what the last modifcation was that made
262 xfs_btree_sblock_calc_crc(
265 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
266 struct xfs_buf_log_item *bip = bp->b_fspriv;
268 if (!xfs_sb_version_hascrc(&bp->b_target->bt_mount->m_sb))
271 block->bb_u.s.bb_lsn = cpu_to_be64(bip->bli_item.li_lsn);
272 xfs_update_cksum(bp->b_addr, BBTOB(bp->b_length),
273 XFS_BTREE_SBLOCK_CRC_OFF);
277 xfs_btree_sblock_verify_crc(
280 if (xfs_sb_version_hascrc(&bp->b_target->bt_mount->m_sb))
281 return xfs_verify_cksum(bp->b_addr, BBTOB(bp->b_length),
282 XFS_BTREE_SBLOCK_CRC_OFF);
287 * Delete the btree cursor.
290 xfs_btree_del_cursor(
291 xfs_btree_cur_t *cur, /* btree cursor */
292 int error) /* del because of error */
294 int i; /* btree level */
297 * Clear the buffer pointers, and release the buffers.
298 * If we're doing this in the face of an error, we
299 * need to make sure to inspect all of the entries
300 * in the bc_bufs array for buffers to be unlocked.
301 * This is because some of the btree code works from
302 * level n down to 0, and if we get an error along
303 * the way we won't have initialized all the entries
306 for (i = 0; i < cur->bc_nlevels; i++) {
308 xfs_trans_brelse(cur->bc_tp, cur->bc_bufs[i]);
313 * Can't free a bmap cursor without having dealt with the
314 * allocated indirect blocks' accounting.
316 ASSERT(cur->bc_btnum != XFS_BTNUM_BMAP ||
317 cur->bc_private.b.allocated == 0);
321 kmem_zone_free(xfs_btree_cur_zone, cur);
325 * Duplicate the btree cursor.
326 * Allocate a new one, copy the record, re-get the buffers.
329 xfs_btree_dup_cursor(
330 xfs_btree_cur_t *cur, /* input cursor */
331 xfs_btree_cur_t **ncur) /* output cursor */
333 xfs_buf_t *bp; /* btree block's buffer pointer */
334 int error; /* error return value */
335 int i; /* level number of btree block */
336 xfs_mount_t *mp; /* mount structure for filesystem */
337 xfs_btree_cur_t *new; /* new cursor value */
338 xfs_trans_t *tp; /* transaction pointer, can be NULL */
344 * Allocate a new cursor like the old one.
346 new = cur->bc_ops->dup_cursor(cur);
349 * Copy the record currently in the cursor.
351 new->bc_rec = cur->bc_rec;
354 * For each level current, re-get the buffer and copy the ptr value.
356 for (i = 0; i < new->bc_nlevels; i++) {
357 new->bc_ptrs[i] = cur->bc_ptrs[i];
358 new->bc_ra[i] = cur->bc_ra[i];
359 bp = cur->bc_bufs[i];
361 error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp,
362 XFS_BUF_ADDR(bp), mp->m_bsize,
364 cur->bc_ops->buf_ops);
366 xfs_btree_del_cursor(new, error);
371 new->bc_bufs[i] = bp;
378 * XFS btree block layout and addressing:
380 * There are two types of blocks in the btree: leaf and non-leaf blocks.
382 * The leaf record start with a header then followed by records containing
383 * the values. A non-leaf block also starts with the same header, and
384 * then first contains lookup keys followed by an equal number of pointers
385 * to the btree blocks at the previous level.
387 * +--------+-------+-------+-------+-------+-------+-------+
388 * Leaf: | header | rec 1 | rec 2 | rec 3 | rec 4 | rec 5 | rec N |
389 * +--------+-------+-------+-------+-------+-------+-------+
391 * +--------+-------+-------+-------+-------+-------+-------+
392 * Non-Leaf: | header | key 1 | key 2 | key N | ptr 1 | ptr 2 | ptr N |
393 * +--------+-------+-------+-------+-------+-------+-------+
395 * The header is called struct xfs_btree_block for reasons better left unknown
396 * and comes in different versions for short (32bit) and long (64bit) block
397 * pointers. The record and key structures are defined by the btree instances
398 * and opaque to the btree core. The block pointers are simple disk endian
399 * integers, available in a short (32bit) and long (64bit) variant.
401 * The helpers below calculate the offset of a given record, key or pointer
402 * into a btree block (xfs_btree_*_offset) or return a pointer to the given
403 * record, key or pointer (xfs_btree_*_addr). Note that all addressing
404 * inside the btree block is done using indices starting at one, not zero!
408 * Return size of the btree block header for this btree instance.
410 static inline size_t xfs_btree_block_len(struct xfs_btree_cur *cur)
412 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
413 if (cur->bc_flags & XFS_BTREE_CRC_BLOCKS)
414 return XFS_BTREE_LBLOCK_CRC_LEN;
415 return XFS_BTREE_LBLOCK_LEN;
417 if (cur->bc_flags & XFS_BTREE_CRC_BLOCKS)
418 return XFS_BTREE_SBLOCK_CRC_LEN;
419 return XFS_BTREE_SBLOCK_LEN;
423 * Return size of btree block pointers for this btree instance.
425 static inline size_t xfs_btree_ptr_len(struct xfs_btree_cur *cur)
427 return (cur->bc_flags & XFS_BTREE_LONG_PTRS) ?
428 sizeof(__be64) : sizeof(__be32);
432 * Calculate offset of the n-th record in a btree block.
435 xfs_btree_rec_offset(
436 struct xfs_btree_cur *cur,
439 return xfs_btree_block_len(cur) +
440 (n - 1) * cur->bc_ops->rec_len;
444 * Calculate offset of the n-th key in a btree block.
447 xfs_btree_key_offset(
448 struct xfs_btree_cur *cur,
451 return xfs_btree_block_len(cur) +
452 (n - 1) * cur->bc_ops->key_len;
456 * Calculate offset of the n-th block pointer in a btree block.
459 xfs_btree_ptr_offset(
460 struct xfs_btree_cur *cur,
464 return xfs_btree_block_len(cur) +
465 cur->bc_ops->get_maxrecs(cur, level) * cur->bc_ops->key_len +
466 (n - 1) * xfs_btree_ptr_len(cur);
470 * Return a pointer to the n-th record in the btree block.
472 STATIC union xfs_btree_rec *
474 struct xfs_btree_cur *cur,
476 struct xfs_btree_block *block)
478 return (union xfs_btree_rec *)
479 ((char *)block + xfs_btree_rec_offset(cur, n));
483 * Return a pointer to the n-th key in the btree block.
485 STATIC union xfs_btree_key *
487 struct xfs_btree_cur *cur,
489 struct xfs_btree_block *block)
491 return (union xfs_btree_key *)
492 ((char *)block + xfs_btree_key_offset(cur, n));
496 * Return a pointer to the n-th block pointer in the btree block.
498 STATIC union xfs_btree_ptr *
500 struct xfs_btree_cur *cur,
502 struct xfs_btree_block *block)
504 int level = xfs_btree_get_level(block);
506 ASSERT(block->bb_level != 0);
508 return (union xfs_btree_ptr *)
509 ((char *)block + xfs_btree_ptr_offset(cur, n, level));
513 * Get the root block which is stored in the inode.
515 * For now this btree implementation assumes the btree root is always
516 * stored in the if_broot field of an inode fork.
518 STATIC struct xfs_btree_block *
520 struct xfs_btree_cur *cur)
522 struct xfs_ifork *ifp;
524 ifp = XFS_IFORK_PTR(cur->bc_private.b.ip, cur->bc_private.b.whichfork);
525 return (struct xfs_btree_block *)ifp->if_broot;
529 * Retrieve the block pointer from the cursor at the given level.
530 * This may be an inode btree root or from a buffer.
532 STATIC struct xfs_btree_block * /* generic btree block pointer */
534 struct xfs_btree_cur *cur, /* btree cursor */
535 int level, /* level in btree */
536 struct xfs_buf **bpp) /* buffer containing the block */
538 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
539 (level == cur->bc_nlevels - 1)) {
541 return xfs_btree_get_iroot(cur);
544 *bpp = cur->bc_bufs[level];
545 return XFS_BUF_TO_BLOCK(*bpp);
549 * Get a buffer for the block, return it with no data read.
550 * Long-form addressing.
552 xfs_buf_t * /* buffer for fsbno */
554 xfs_mount_t *mp, /* file system mount point */
555 xfs_trans_t *tp, /* transaction pointer */
556 xfs_fsblock_t fsbno, /* file system block number */
557 uint lock) /* lock flags for get_buf */
559 xfs_buf_t *bp; /* buffer pointer (return value) */
560 xfs_daddr_t d; /* real disk block address */
562 ASSERT(fsbno != NULLFSBLOCK);
563 d = XFS_FSB_TO_DADDR(mp, fsbno);
564 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d, mp->m_bsize, lock);
565 ASSERT(!xfs_buf_geterror(bp));
570 * Get a buffer for the block, return it with no data read.
571 * Short-form addressing.
573 xfs_buf_t * /* buffer for agno/agbno */
575 xfs_mount_t *mp, /* file system mount point */
576 xfs_trans_t *tp, /* transaction pointer */
577 xfs_agnumber_t agno, /* allocation group number */
578 xfs_agblock_t agbno, /* allocation group block number */
579 uint lock) /* lock flags for get_buf */
581 xfs_buf_t *bp; /* buffer pointer (return value) */
582 xfs_daddr_t d; /* real disk block address */
584 ASSERT(agno != NULLAGNUMBER);
585 ASSERT(agbno != NULLAGBLOCK);
586 d = XFS_AGB_TO_DADDR(mp, agno, agbno);
587 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d, mp->m_bsize, lock);
588 ASSERT(!xfs_buf_geterror(bp));
593 * Check for the cursor referring to the last block at the given level.
595 int /* 1=is last block, 0=not last block */
596 xfs_btree_islastblock(
597 xfs_btree_cur_t *cur, /* btree cursor */
598 int level) /* level to check */
600 struct xfs_btree_block *block; /* generic btree block pointer */
601 xfs_buf_t *bp; /* buffer containing block */
603 block = xfs_btree_get_block(cur, level, &bp);
604 xfs_btree_check_block(cur, block, level, bp);
605 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
606 return block->bb_u.l.bb_rightsib == cpu_to_be64(NULLDFSBNO);
608 return block->bb_u.s.bb_rightsib == cpu_to_be32(NULLAGBLOCK);
612 * Change the cursor to point to the first record at the given level.
613 * Other levels are unaffected.
615 STATIC int /* success=1, failure=0 */
617 xfs_btree_cur_t *cur, /* btree cursor */
618 int level) /* level to change */
620 struct xfs_btree_block *block; /* generic btree block pointer */
621 xfs_buf_t *bp; /* buffer containing block */
624 * Get the block pointer for this level.
626 block = xfs_btree_get_block(cur, level, &bp);
627 xfs_btree_check_block(cur, block, level, bp);
629 * It's empty, there is no such record.
631 if (!block->bb_numrecs)
634 * Set the ptr value to 1, that's the first record/key.
636 cur->bc_ptrs[level] = 1;
641 * Change the cursor to point to the last record in the current block
642 * at the given level. Other levels are unaffected.
644 STATIC int /* success=1, failure=0 */
646 xfs_btree_cur_t *cur, /* btree cursor */
647 int level) /* level to change */
649 struct xfs_btree_block *block; /* generic btree block pointer */
650 xfs_buf_t *bp; /* buffer containing block */
653 * Get the block pointer for this level.
655 block = xfs_btree_get_block(cur, level, &bp);
656 xfs_btree_check_block(cur, block, level, bp);
658 * It's empty, there is no such record.
660 if (!block->bb_numrecs)
663 * Set the ptr value to numrecs, that's the last record/key.
665 cur->bc_ptrs[level] = be16_to_cpu(block->bb_numrecs);
670 * Compute first and last byte offsets for the fields given.
671 * Interprets the offsets table, which contains struct field offsets.
675 __int64_t fields, /* bitmask of fields */
676 const short *offsets, /* table of field offsets */
677 int nbits, /* number of bits to inspect */
678 int *first, /* output: first byte offset */
679 int *last) /* output: last byte offset */
681 int i; /* current bit number */
682 __int64_t imask; /* mask for current bit number */
686 * Find the lowest bit, so the first byte offset.
688 for (i = 0, imask = 1LL; ; i++, imask <<= 1) {
689 if (imask & fields) {
695 * Find the highest bit, so the last byte offset.
697 for (i = nbits - 1, imask = 1LL << i; ; i--, imask >>= 1) {
698 if (imask & fields) {
699 *last = offsets[i + 1] - 1;
706 * Get a buffer for the block, return it read in.
707 * Long-form addressing.
711 struct xfs_mount *mp, /* file system mount point */
712 struct xfs_trans *tp, /* transaction pointer */
713 xfs_fsblock_t fsbno, /* file system block number */
714 uint lock, /* lock flags for read_buf */
715 struct xfs_buf **bpp, /* buffer for fsbno */
716 int refval, /* ref count value for buffer */
717 const struct xfs_buf_ops *ops)
719 struct xfs_buf *bp; /* return value */
720 xfs_daddr_t d; /* real disk block address */
723 ASSERT(fsbno != NULLFSBLOCK);
724 d = XFS_FSB_TO_DADDR(mp, fsbno);
725 error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, d,
726 mp->m_bsize, lock, &bp, ops);
729 ASSERT(!xfs_buf_geterror(bp));
731 xfs_buf_set_ref(bp, refval);
737 * Read-ahead the block, don't wait for it, don't return a buffer.
738 * Long-form addressing.
742 xfs_btree_reada_bufl(
743 struct xfs_mount *mp, /* file system mount point */
744 xfs_fsblock_t fsbno, /* file system block number */
745 xfs_extlen_t count, /* count of filesystem blocks */
746 const struct xfs_buf_ops *ops)
750 ASSERT(fsbno != NULLFSBLOCK);
751 d = XFS_FSB_TO_DADDR(mp, fsbno);
752 xfs_buf_readahead(mp->m_ddev_targp, d, mp->m_bsize * count, ops);
756 * Read-ahead the block, don't wait for it, don't return a buffer.
757 * Short-form addressing.
761 xfs_btree_reada_bufs(
762 struct xfs_mount *mp, /* file system mount point */
763 xfs_agnumber_t agno, /* allocation group number */
764 xfs_agblock_t agbno, /* allocation group block number */
765 xfs_extlen_t count, /* count of filesystem blocks */
766 const struct xfs_buf_ops *ops)
770 ASSERT(agno != NULLAGNUMBER);
771 ASSERT(agbno != NULLAGBLOCK);
772 d = XFS_AGB_TO_DADDR(mp, agno, agbno);
773 xfs_buf_readahead(mp->m_ddev_targp, d, mp->m_bsize * count, ops);
777 xfs_btree_readahead_lblock(
778 struct xfs_btree_cur *cur,
780 struct xfs_btree_block *block)
783 xfs_dfsbno_t left = be64_to_cpu(block->bb_u.l.bb_leftsib);
784 xfs_dfsbno_t right = be64_to_cpu(block->bb_u.l.bb_rightsib);
786 if ((lr & XFS_BTCUR_LEFTRA) && left != NULLDFSBNO) {
787 xfs_btree_reada_bufl(cur->bc_mp, left, 1,
788 cur->bc_ops->buf_ops);
792 if ((lr & XFS_BTCUR_RIGHTRA) && right != NULLDFSBNO) {
793 xfs_btree_reada_bufl(cur->bc_mp, right, 1,
794 cur->bc_ops->buf_ops);
802 xfs_btree_readahead_sblock(
803 struct xfs_btree_cur *cur,
805 struct xfs_btree_block *block)
808 xfs_agblock_t left = be32_to_cpu(block->bb_u.s.bb_leftsib);
809 xfs_agblock_t right = be32_to_cpu(block->bb_u.s.bb_rightsib);
812 if ((lr & XFS_BTCUR_LEFTRA) && left != NULLAGBLOCK) {
813 xfs_btree_reada_bufs(cur->bc_mp, cur->bc_private.a.agno,
814 left, 1, cur->bc_ops->buf_ops);
818 if ((lr & XFS_BTCUR_RIGHTRA) && right != NULLAGBLOCK) {
819 xfs_btree_reada_bufs(cur->bc_mp, cur->bc_private.a.agno,
820 right, 1, cur->bc_ops->buf_ops);
828 * Read-ahead btree blocks, at the given level.
829 * Bits in lr are set from XFS_BTCUR_{LEFT,RIGHT}RA.
833 struct xfs_btree_cur *cur, /* btree cursor */
834 int lev, /* level in btree */
835 int lr) /* left/right bits */
837 struct xfs_btree_block *block;
840 * No readahead needed if we are at the root level and the
841 * btree root is stored in the inode.
843 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
844 (lev == cur->bc_nlevels - 1))
847 if ((cur->bc_ra[lev] | lr) == cur->bc_ra[lev])
850 cur->bc_ra[lev] |= lr;
851 block = XFS_BUF_TO_BLOCK(cur->bc_bufs[lev]);
853 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
854 return xfs_btree_readahead_lblock(cur, lr, block);
855 return xfs_btree_readahead_sblock(cur, lr, block);
859 * Set the buffer for level "lev" in the cursor to bp, releasing
860 * any previous buffer.
864 xfs_btree_cur_t *cur, /* btree cursor */
865 int lev, /* level in btree */
866 xfs_buf_t *bp) /* new buffer to set */
868 struct xfs_btree_block *b; /* btree block */
870 if (cur->bc_bufs[lev])
871 xfs_trans_brelse(cur->bc_tp, cur->bc_bufs[lev]);
872 cur->bc_bufs[lev] = bp;
875 b = XFS_BUF_TO_BLOCK(bp);
876 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
877 if (b->bb_u.l.bb_leftsib == cpu_to_be64(NULLDFSBNO))
878 cur->bc_ra[lev] |= XFS_BTCUR_LEFTRA;
879 if (b->bb_u.l.bb_rightsib == cpu_to_be64(NULLDFSBNO))
880 cur->bc_ra[lev] |= XFS_BTCUR_RIGHTRA;
882 if (b->bb_u.s.bb_leftsib == cpu_to_be32(NULLAGBLOCK))
883 cur->bc_ra[lev] |= XFS_BTCUR_LEFTRA;
884 if (b->bb_u.s.bb_rightsib == cpu_to_be32(NULLAGBLOCK))
885 cur->bc_ra[lev] |= XFS_BTCUR_RIGHTRA;
890 xfs_btree_ptr_is_null(
891 struct xfs_btree_cur *cur,
892 union xfs_btree_ptr *ptr)
894 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
895 return ptr->l == cpu_to_be64(NULLDFSBNO);
897 return ptr->s == cpu_to_be32(NULLAGBLOCK);
901 xfs_btree_set_ptr_null(
902 struct xfs_btree_cur *cur,
903 union xfs_btree_ptr *ptr)
905 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
906 ptr->l = cpu_to_be64(NULLDFSBNO);
908 ptr->s = cpu_to_be32(NULLAGBLOCK);
912 * Get/set/init sibling pointers
915 xfs_btree_get_sibling(
916 struct xfs_btree_cur *cur,
917 struct xfs_btree_block *block,
918 union xfs_btree_ptr *ptr,
921 ASSERT(lr == XFS_BB_LEFTSIB || lr == XFS_BB_RIGHTSIB);
923 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
924 if (lr == XFS_BB_RIGHTSIB)
925 ptr->l = block->bb_u.l.bb_rightsib;
927 ptr->l = block->bb_u.l.bb_leftsib;
929 if (lr == XFS_BB_RIGHTSIB)
930 ptr->s = block->bb_u.s.bb_rightsib;
932 ptr->s = block->bb_u.s.bb_leftsib;
937 xfs_btree_set_sibling(
938 struct xfs_btree_cur *cur,
939 struct xfs_btree_block *block,
940 union xfs_btree_ptr *ptr,
943 ASSERT(lr == XFS_BB_LEFTSIB || lr == XFS_BB_RIGHTSIB);
945 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
946 if (lr == XFS_BB_RIGHTSIB)
947 block->bb_u.l.bb_rightsib = ptr->l;
949 block->bb_u.l.bb_leftsib = ptr->l;
951 if (lr == XFS_BB_RIGHTSIB)
952 block->bb_u.s.bb_rightsib = ptr->s;
954 block->bb_u.s.bb_leftsib = ptr->s;
959 xfs_btree_init_block_int(
960 struct xfs_mount *mp,
961 struct xfs_btree_block *buf,
969 buf->bb_magic = cpu_to_be32(magic);
970 buf->bb_level = cpu_to_be16(level);
971 buf->bb_numrecs = cpu_to_be16(numrecs);
973 if (flags & XFS_BTREE_LONG_PTRS) {
974 buf->bb_u.l.bb_leftsib = cpu_to_be64(NULLDFSBNO);
975 buf->bb_u.l.bb_rightsib = cpu_to_be64(NULLDFSBNO);
976 if (flags & XFS_BTREE_CRC_BLOCKS) {
977 buf->bb_u.l.bb_blkno = cpu_to_be64(blkno);
978 buf->bb_u.l.bb_owner = cpu_to_be64(owner);
979 uuid_copy(&buf->bb_u.l.bb_uuid, &mp->m_sb.sb_uuid);
980 buf->bb_u.l.bb_pad = 0;
981 buf->bb_u.l.bb_lsn = 0;
984 /* owner is a 32 bit value on short blocks */
985 __u32 __owner = (__u32)owner;
987 buf->bb_u.s.bb_leftsib = cpu_to_be32(NULLAGBLOCK);
988 buf->bb_u.s.bb_rightsib = cpu_to_be32(NULLAGBLOCK);
989 if (flags & XFS_BTREE_CRC_BLOCKS) {
990 buf->bb_u.s.bb_blkno = cpu_to_be64(blkno);
991 buf->bb_u.s.bb_owner = cpu_to_be32(__owner);
992 uuid_copy(&buf->bb_u.s.bb_uuid, &mp->m_sb.sb_uuid);
993 buf->bb_u.s.bb_lsn = 0;
999 xfs_btree_init_block(
1000 struct xfs_mount *mp,
1008 xfs_btree_init_block_int(mp, XFS_BUF_TO_BLOCK(bp), bp->b_bn,
1009 magic, level, numrecs, owner, flags);
1013 xfs_btree_init_block_cur(
1014 struct xfs_btree_cur *cur,
1022 * we can pull the owner from the cursor right now as the different
1023 * owners align directly with the pointer size of the btree. This may
1024 * change in future, but is safe for current users of the generic btree
1027 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
1028 owner = cur->bc_private.b.ip->i_ino;
1030 owner = cur->bc_private.a.agno;
1032 xfs_btree_init_block_int(cur->bc_mp, XFS_BUF_TO_BLOCK(bp), bp->b_bn,
1033 xfs_btree_magic(cur), level, numrecs,
1034 owner, cur->bc_flags);
1038 * Return true if ptr is the last record in the btree and
1039 * we need to track updates to this record. The decision
1040 * will be further refined in the update_lastrec method.
1043 xfs_btree_is_lastrec(
1044 struct xfs_btree_cur *cur,
1045 struct xfs_btree_block *block,
1048 union xfs_btree_ptr ptr;
1052 if (!(cur->bc_flags & XFS_BTREE_LASTREC_UPDATE))
1055 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_RIGHTSIB);
1056 if (!xfs_btree_ptr_is_null(cur, &ptr))
1062 xfs_btree_buf_to_ptr(
1063 struct xfs_btree_cur *cur,
1065 union xfs_btree_ptr *ptr)
1067 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
1068 ptr->l = cpu_to_be64(XFS_DADDR_TO_FSB(cur->bc_mp,
1071 ptr->s = cpu_to_be32(xfs_daddr_to_agbno(cur->bc_mp,
1077 xfs_btree_ptr_to_daddr(
1078 struct xfs_btree_cur *cur,
1079 union xfs_btree_ptr *ptr)
1081 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
1082 ASSERT(ptr->l != cpu_to_be64(NULLDFSBNO));
1084 return XFS_FSB_TO_DADDR(cur->bc_mp, be64_to_cpu(ptr->l));
1086 ASSERT(cur->bc_private.a.agno != NULLAGNUMBER);
1087 ASSERT(ptr->s != cpu_to_be32(NULLAGBLOCK));
1089 return XFS_AGB_TO_DADDR(cur->bc_mp, cur->bc_private.a.agno,
1090 be32_to_cpu(ptr->s));
1096 struct xfs_btree_cur *cur,
1099 switch (cur->bc_btnum) {
1102 xfs_buf_set_ref(bp, XFS_ALLOC_BTREE_REF);
1105 xfs_buf_set_ref(bp, XFS_INO_BTREE_REF);
1107 case XFS_BTNUM_BMAP:
1108 xfs_buf_set_ref(bp, XFS_BMAP_BTREE_REF);
1116 xfs_btree_get_buf_block(
1117 struct xfs_btree_cur *cur,
1118 union xfs_btree_ptr *ptr,
1120 struct xfs_btree_block **block,
1121 struct xfs_buf **bpp)
1123 struct xfs_mount *mp = cur->bc_mp;
1126 /* need to sort out how callers deal with failures first */
1127 ASSERT(!(flags & XBF_TRYLOCK));
1129 d = xfs_btree_ptr_to_daddr(cur, ptr);
1130 *bpp = xfs_trans_get_buf(cur->bc_tp, mp->m_ddev_targp, d,
1131 mp->m_bsize, flags);
1136 (*bpp)->b_ops = cur->bc_ops->buf_ops;
1137 *block = XFS_BUF_TO_BLOCK(*bpp);
1142 * Read in the buffer at the given ptr and return the buffer and
1143 * the block pointer within the buffer.
1146 xfs_btree_read_buf_block(
1147 struct xfs_btree_cur *cur,
1148 union xfs_btree_ptr *ptr,
1151 struct xfs_btree_block **block,
1152 struct xfs_buf **bpp)
1154 struct xfs_mount *mp = cur->bc_mp;
1158 /* need to sort out how callers deal with failures first */
1159 ASSERT(!(flags & XBF_TRYLOCK));
1161 d = xfs_btree_ptr_to_daddr(cur, ptr);
1162 error = xfs_trans_read_buf(mp, cur->bc_tp, mp->m_ddev_targp, d,
1163 mp->m_bsize, flags, bpp,
1164 cur->bc_ops->buf_ops);
1168 ASSERT(!xfs_buf_geterror(*bpp));
1169 xfs_btree_set_refs(cur, *bpp);
1170 *block = XFS_BUF_TO_BLOCK(*bpp);
1175 * Copy keys from one btree block to another.
1178 xfs_btree_copy_keys(
1179 struct xfs_btree_cur *cur,
1180 union xfs_btree_key *dst_key,
1181 union xfs_btree_key *src_key,
1184 ASSERT(numkeys >= 0);
1185 memcpy(dst_key, src_key, numkeys * cur->bc_ops->key_len);
1189 * Copy records from one btree block to another.
1192 xfs_btree_copy_recs(
1193 struct xfs_btree_cur *cur,
1194 union xfs_btree_rec *dst_rec,
1195 union xfs_btree_rec *src_rec,
1198 ASSERT(numrecs >= 0);
1199 memcpy(dst_rec, src_rec, numrecs * cur->bc_ops->rec_len);
1203 * Copy block pointers from one btree block to another.
1206 xfs_btree_copy_ptrs(
1207 struct xfs_btree_cur *cur,
1208 union xfs_btree_ptr *dst_ptr,
1209 union xfs_btree_ptr *src_ptr,
1212 ASSERT(numptrs >= 0);
1213 memcpy(dst_ptr, src_ptr, numptrs * xfs_btree_ptr_len(cur));
1217 * Shift keys one index left/right inside a single btree block.
1220 xfs_btree_shift_keys(
1221 struct xfs_btree_cur *cur,
1222 union xfs_btree_key *key,
1228 ASSERT(numkeys >= 0);
1229 ASSERT(dir == 1 || dir == -1);
1231 dst_key = (char *)key + (dir * cur->bc_ops->key_len);
1232 memmove(dst_key, key, numkeys * cur->bc_ops->key_len);
1236 * Shift records one index left/right inside a single btree block.
1239 xfs_btree_shift_recs(
1240 struct xfs_btree_cur *cur,
1241 union xfs_btree_rec *rec,
1247 ASSERT(numrecs >= 0);
1248 ASSERT(dir == 1 || dir == -1);
1250 dst_rec = (char *)rec + (dir * cur->bc_ops->rec_len);
1251 memmove(dst_rec, rec, numrecs * cur->bc_ops->rec_len);
1255 * Shift block pointers one index left/right inside a single btree block.
1258 xfs_btree_shift_ptrs(
1259 struct xfs_btree_cur *cur,
1260 union xfs_btree_ptr *ptr,
1266 ASSERT(numptrs >= 0);
1267 ASSERT(dir == 1 || dir == -1);
1269 dst_ptr = (char *)ptr + (dir * xfs_btree_ptr_len(cur));
1270 memmove(dst_ptr, ptr, numptrs * xfs_btree_ptr_len(cur));
1274 * Log key values from the btree block.
1278 struct xfs_btree_cur *cur,
1283 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1284 XFS_BTREE_TRACE_ARGBII(cur, bp, first, last);
1287 xfs_trans_buf_set_type(cur->bc_tp, bp, XFS_BLFT_BTREE_BUF);
1288 xfs_trans_log_buf(cur->bc_tp, bp,
1289 xfs_btree_key_offset(cur, first),
1290 xfs_btree_key_offset(cur, last + 1) - 1);
1292 xfs_trans_log_inode(cur->bc_tp, cur->bc_private.b.ip,
1293 xfs_ilog_fbroot(cur->bc_private.b.whichfork));
1296 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1300 * Log record values from the btree block.
1304 struct xfs_btree_cur *cur,
1309 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1310 XFS_BTREE_TRACE_ARGBII(cur, bp, first, last);
1312 xfs_trans_buf_set_type(cur->bc_tp, bp, XFS_BLFT_BTREE_BUF);
1313 xfs_trans_log_buf(cur->bc_tp, bp,
1314 xfs_btree_rec_offset(cur, first),
1315 xfs_btree_rec_offset(cur, last + 1) - 1);
1317 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1321 * Log block pointer fields from a btree block (nonleaf).
1325 struct xfs_btree_cur *cur, /* btree cursor */
1326 struct xfs_buf *bp, /* buffer containing btree block */
1327 int first, /* index of first pointer to log */
1328 int last) /* index of last pointer to log */
1330 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1331 XFS_BTREE_TRACE_ARGBII(cur, bp, first, last);
1334 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
1335 int level = xfs_btree_get_level(block);
1337 xfs_trans_buf_set_type(cur->bc_tp, bp, XFS_BLFT_BTREE_BUF);
1338 xfs_trans_log_buf(cur->bc_tp, bp,
1339 xfs_btree_ptr_offset(cur, first, level),
1340 xfs_btree_ptr_offset(cur, last + 1, level) - 1);
1342 xfs_trans_log_inode(cur->bc_tp, cur->bc_private.b.ip,
1343 xfs_ilog_fbroot(cur->bc_private.b.whichfork));
1346 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1350 * Log fields from a btree block header.
1353 xfs_btree_log_block(
1354 struct xfs_btree_cur *cur, /* btree cursor */
1355 struct xfs_buf *bp, /* buffer containing btree block */
1356 int fields) /* mask of fields: XFS_BB_... */
1358 int first; /* first byte offset logged */
1359 int last; /* last byte offset logged */
1360 static const short soffsets[] = { /* table of offsets (short) */
1361 offsetof(struct xfs_btree_block, bb_magic),
1362 offsetof(struct xfs_btree_block, bb_level),
1363 offsetof(struct xfs_btree_block, bb_numrecs),
1364 offsetof(struct xfs_btree_block, bb_u.s.bb_leftsib),
1365 offsetof(struct xfs_btree_block, bb_u.s.bb_rightsib),
1366 offsetof(struct xfs_btree_block, bb_u.s.bb_blkno),
1367 offsetof(struct xfs_btree_block, bb_u.s.bb_lsn),
1368 offsetof(struct xfs_btree_block, bb_u.s.bb_uuid),
1369 offsetof(struct xfs_btree_block, bb_u.s.bb_owner),
1370 offsetof(struct xfs_btree_block, bb_u.s.bb_crc),
1371 XFS_BTREE_SBLOCK_CRC_LEN
1373 static const short loffsets[] = { /* table of offsets (long) */
1374 offsetof(struct xfs_btree_block, bb_magic),
1375 offsetof(struct xfs_btree_block, bb_level),
1376 offsetof(struct xfs_btree_block, bb_numrecs),
1377 offsetof(struct xfs_btree_block, bb_u.l.bb_leftsib),
1378 offsetof(struct xfs_btree_block, bb_u.l.bb_rightsib),
1379 offsetof(struct xfs_btree_block, bb_u.l.bb_blkno),
1380 offsetof(struct xfs_btree_block, bb_u.l.bb_lsn),
1381 offsetof(struct xfs_btree_block, bb_u.l.bb_uuid),
1382 offsetof(struct xfs_btree_block, bb_u.l.bb_owner),
1383 offsetof(struct xfs_btree_block, bb_u.l.bb_crc),
1384 offsetof(struct xfs_btree_block, bb_u.l.bb_pad),
1385 XFS_BTREE_LBLOCK_CRC_LEN
1388 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1389 XFS_BTREE_TRACE_ARGBI(cur, bp, fields);
1394 if (cur->bc_flags & XFS_BTREE_CRC_BLOCKS) {
1396 * We don't log the CRC when updating a btree
1397 * block but instead recreate it during log
1398 * recovery. As the log buffers have checksums
1399 * of their own this is safe and avoids logging a crc
1400 * update in a lot of places.
1402 if (fields == XFS_BB_ALL_BITS)
1403 fields = XFS_BB_ALL_BITS_CRC;
1404 nbits = XFS_BB_NUM_BITS_CRC;
1406 nbits = XFS_BB_NUM_BITS;
1408 xfs_btree_offsets(fields,
1409 (cur->bc_flags & XFS_BTREE_LONG_PTRS) ?
1410 loffsets : soffsets,
1411 nbits, &first, &last);
1412 xfs_trans_buf_set_type(cur->bc_tp, bp, XFS_BLFT_BTREE_BUF);
1413 xfs_trans_log_buf(cur->bc_tp, bp, first, last);
1415 xfs_trans_log_inode(cur->bc_tp, cur->bc_private.b.ip,
1416 xfs_ilog_fbroot(cur->bc_private.b.whichfork));
1419 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1423 * Increment cursor by one record at the level.
1424 * For nonzero levels the leaf-ward information is untouched.
1427 xfs_btree_increment(
1428 struct xfs_btree_cur *cur,
1430 int *stat) /* success/failure */
1432 struct xfs_btree_block *block;
1433 union xfs_btree_ptr ptr;
1435 int error; /* error return value */
1438 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1439 XFS_BTREE_TRACE_ARGI(cur, level);
1441 ASSERT(level < cur->bc_nlevels);
1443 /* Read-ahead to the right at this level. */
1444 xfs_btree_readahead(cur, level, XFS_BTCUR_RIGHTRA);
1446 /* Get a pointer to the btree block. */
1447 block = xfs_btree_get_block(cur, level, &bp);
1450 error = xfs_btree_check_block(cur, block, level, bp);
1455 /* We're done if we remain in the block after the increment. */
1456 if (++cur->bc_ptrs[level] <= xfs_btree_get_numrecs(block))
1459 /* Fail if we just went off the right edge of the tree. */
1460 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_RIGHTSIB);
1461 if (xfs_btree_ptr_is_null(cur, &ptr))
1464 XFS_BTREE_STATS_INC(cur, increment);
1467 * March up the tree incrementing pointers.
1468 * Stop when we don't go off the right edge of a block.
1470 for (lev = level + 1; lev < cur->bc_nlevels; lev++) {
1471 block = xfs_btree_get_block(cur, lev, &bp);
1474 error = xfs_btree_check_block(cur, block, lev, bp);
1479 if (++cur->bc_ptrs[lev] <= xfs_btree_get_numrecs(block))
1482 /* Read-ahead the right block for the next loop. */
1483 xfs_btree_readahead(cur, lev, XFS_BTCUR_RIGHTRA);
1487 * If we went off the root then we are either seriously
1488 * confused or have the tree root in an inode.
1490 if (lev == cur->bc_nlevels) {
1491 if (cur->bc_flags & XFS_BTREE_ROOT_IN_INODE)
1494 error = EFSCORRUPTED;
1497 ASSERT(lev < cur->bc_nlevels);
1500 * Now walk back down the tree, fixing up the cursor's buffer
1501 * pointers and key numbers.
1503 for (block = xfs_btree_get_block(cur, lev, &bp); lev > level; ) {
1504 union xfs_btree_ptr *ptrp;
1506 ptrp = xfs_btree_ptr_addr(cur, cur->bc_ptrs[lev], block);
1507 error = xfs_btree_read_buf_block(cur, ptrp, --lev,
1512 xfs_btree_setbuf(cur, lev, bp);
1513 cur->bc_ptrs[lev] = 1;
1516 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1521 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1526 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
1531 * Decrement cursor by one record at the level.
1532 * For nonzero levels the leaf-ward information is untouched.
1535 xfs_btree_decrement(
1536 struct xfs_btree_cur *cur,
1538 int *stat) /* success/failure */
1540 struct xfs_btree_block *block;
1542 int error; /* error return value */
1544 union xfs_btree_ptr ptr;
1546 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1547 XFS_BTREE_TRACE_ARGI(cur, level);
1549 ASSERT(level < cur->bc_nlevels);
1551 /* Read-ahead to the left at this level. */
1552 xfs_btree_readahead(cur, level, XFS_BTCUR_LEFTRA);
1554 /* We're done if we remain in the block after the decrement. */
1555 if (--cur->bc_ptrs[level] > 0)
1558 /* Get a pointer to the btree block. */
1559 block = xfs_btree_get_block(cur, level, &bp);
1562 error = xfs_btree_check_block(cur, block, level, bp);
1567 /* Fail if we just went off the left edge of the tree. */
1568 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_LEFTSIB);
1569 if (xfs_btree_ptr_is_null(cur, &ptr))
1572 XFS_BTREE_STATS_INC(cur, decrement);
1575 * March up the tree decrementing pointers.
1576 * Stop when we don't go off the left edge of a block.
1578 for (lev = level + 1; lev < cur->bc_nlevels; lev++) {
1579 if (--cur->bc_ptrs[lev] > 0)
1581 /* Read-ahead the left block for the next loop. */
1582 xfs_btree_readahead(cur, lev, XFS_BTCUR_LEFTRA);
1586 * If we went off the root then we are seriously confused.
1587 * or the root of the tree is in an inode.
1589 if (lev == cur->bc_nlevels) {
1590 if (cur->bc_flags & XFS_BTREE_ROOT_IN_INODE)
1593 error = EFSCORRUPTED;
1596 ASSERT(lev < cur->bc_nlevels);
1599 * Now walk back down the tree, fixing up the cursor's buffer
1600 * pointers and key numbers.
1602 for (block = xfs_btree_get_block(cur, lev, &bp); lev > level; ) {
1603 union xfs_btree_ptr *ptrp;
1605 ptrp = xfs_btree_ptr_addr(cur, cur->bc_ptrs[lev], block);
1606 error = xfs_btree_read_buf_block(cur, ptrp, --lev,
1610 xfs_btree_setbuf(cur, lev, bp);
1611 cur->bc_ptrs[lev] = xfs_btree_get_numrecs(block);
1614 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1619 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1624 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
1629 xfs_btree_lookup_get_block(
1630 struct xfs_btree_cur *cur, /* btree cursor */
1631 int level, /* level in the btree */
1632 union xfs_btree_ptr *pp, /* ptr to btree block */
1633 struct xfs_btree_block **blkp) /* return btree block */
1635 struct xfs_buf *bp; /* buffer pointer for btree block */
1638 /* special case the root block if in an inode */
1639 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
1640 (level == cur->bc_nlevels - 1)) {
1641 *blkp = xfs_btree_get_iroot(cur);
1646 * If the old buffer at this level for the disk address we are
1647 * looking for re-use it.
1649 * Otherwise throw it away and get a new one.
1651 bp = cur->bc_bufs[level];
1652 if (bp && XFS_BUF_ADDR(bp) == xfs_btree_ptr_to_daddr(cur, pp)) {
1653 *blkp = XFS_BUF_TO_BLOCK(bp);
1657 error = xfs_btree_read_buf_block(cur, pp, level, 0, blkp, &bp);
1661 xfs_btree_setbuf(cur, level, bp);
1666 * Get current search key. For level 0 we don't actually have a key
1667 * structure so we make one up from the record. For all other levels
1668 * we just return the right key.
1670 STATIC union xfs_btree_key *
1671 xfs_lookup_get_search_key(
1672 struct xfs_btree_cur *cur,
1675 struct xfs_btree_block *block,
1676 union xfs_btree_key *kp)
1679 cur->bc_ops->init_key_from_rec(kp,
1680 xfs_btree_rec_addr(cur, keyno, block));
1684 return xfs_btree_key_addr(cur, keyno, block);
1688 * Lookup the record. The cursor is made to point to it, based on dir.
1689 * stat is set to 0 if can't find any such record, 1 for success.
1693 struct xfs_btree_cur *cur, /* btree cursor */
1694 xfs_lookup_t dir, /* <=, ==, or >= */
1695 int *stat) /* success/failure */
1697 struct xfs_btree_block *block; /* current btree block */
1698 __int64_t diff; /* difference for the current key */
1699 int error; /* error return value */
1700 int keyno; /* current key number */
1701 int level; /* level in the btree */
1702 union xfs_btree_ptr *pp; /* ptr to btree block */
1703 union xfs_btree_ptr ptr; /* ptr to btree block */
1705 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1706 XFS_BTREE_TRACE_ARGI(cur, dir);
1708 XFS_BTREE_STATS_INC(cur, lookup);
1713 /* initialise start pointer from cursor */
1714 cur->bc_ops->init_ptr_from_cur(cur, &ptr);
1718 * Iterate over each level in the btree, starting at the root.
1719 * For each level above the leaves, find the key we need, based
1720 * on the lookup record, then follow the corresponding block
1721 * pointer down to the next level.
1723 for (level = cur->bc_nlevels - 1, diff = 1; level >= 0; level--) {
1724 /* Get the block we need to do the lookup on. */
1725 error = xfs_btree_lookup_get_block(cur, level, pp, &block);
1731 * If we already had a key match at a higher level, we
1732 * know we need to use the first entry in this block.
1736 /* Otherwise search this block. Do a binary search. */
1738 int high; /* high entry number */
1739 int low; /* low entry number */
1741 /* Set low and high entry numbers, 1-based. */
1743 high = xfs_btree_get_numrecs(block);
1745 /* Block is empty, must be an empty leaf. */
1746 ASSERT(level == 0 && cur->bc_nlevels == 1);
1748 cur->bc_ptrs[0] = dir != XFS_LOOKUP_LE;
1749 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1754 /* Binary search the block. */
1755 while (low <= high) {
1756 union xfs_btree_key key;
1757 union xfs_btree_key *kp;
1759 XFS_BTREE_STATS_INC(cur, compare);
1761 /* keyno is average of low and high. */
1762 keyno = (low + high) >> 1;
1764 /* Get current search key */
1765 kp = xfs_lookup_get_search_key(cur, level,
1766 keyno, block, &key);
1769 * Compute difference to get next direction:
1770 * - less than, move right
1771 * - greater than, move left
1772 * - equal, we're done
1774 diff = cur->bc_ops->key_diff(cur, kp);
1785 * If there are more levels, set up for the next level
1786 * by getting the block number and filling in the cursor.
1790 * If we moved left, need the previous key number,
1791 * unless there isn't one.
1793 if (diff > 0 && --keyno < 1)
1795 pp = xfs_btree_ptr_addr(cur, keyno, block);
1798 error = xfs_btree_check_ptr(cur, pp, 0, level);
1802 cur->bc_ptrs[level] = keyno;
1806 /* Done with the search. See if we need to adjust the results. */
1807 if (dir != XFS_LOOKUP_LE && diff < 0) {
1810 * If ge search and we went off the end of the block, but it's
1811 * not the last block, we're in the wrong block.
1813 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_RIGHTSIB);
1814 if (dir == XFS_LOOKUP_GE &&
1815 keyno > xfs_btree_get_numrecs(block) &&
1816 !xfs_btree_ptr_is_null(cur, &ptr)) {
1819 cur->bc_ptrs[0] = keyno;
1820 error = xfs_btree_increment(cur, 0, &i);
1823 XFS_WANT_CORRUPTED_RETURN(i == 1);
1824 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1828 } else if (dir == XFS_LOOKUP_LE && diff > 0)
1830 cur->bc_ptrs[0] = keyno;
1832 /* Return if we succeeded or not. */
1833 if (keyno == 0 || keyno > xfs_btree_get_numrecs(block))
1835 else if (dir != XFS_LOOKUP_EQ || diff == 0)
1839 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1843 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
1848 * Update keys at all levels from here to the root along the cursor's path.
1852 struct xfs_btree_cur *cur,
1853 union xfs_btree_key *keyp,
1856 struct xfs_btree_block *block;
1858 union xfs_btree_key *kp;
1861 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1862 XFS_BTREE_TRACE_ARGIK(cur, level, keyp);
1864 ASSERT(!(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) || level >= 1);
1867 * Go up the tree from this level toward the root.
1868 * At each level, update the key value to the value input.
1869 * Stop when we reach a level where the cursor isn't pointing
1870 * at the first entry in the block.
1872 for (ptr = 1; ptr == 1 && level < cur->bc_nlevels; level++) {
1876 block = xfs_btree_get_block(cur, level, &bp);
1878 error = xfs_btree_check_block(cur, block, level, bp);
1880 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
1884 ptr = cur->bc_ptrs[level];
1885 kp = xfs_btree_key_addr(cur, ptr, block);
1886 xfs_btree_copy_keys(cur, kp, keyp, 1);
1887 xfs_btree_log_keys(cur, bp, ptr, ptr);
1890 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1895 * Update the record referred to by cur to the value in the
1896 * given record. This either works (return 0) or gets an
1897 * EFSCORRUPTED error.
1901 struct xfs_btree_cur *cur,
1902 union xfs_btree_rec *rec)
1904 struct xfs_btree_block *block;
1908 union xfs_btree_rec *rp;
1910 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1911 XFS_BTREE_TRACE_ARGR(cur, rec);
1913 /* Pick up the current block. */
1914 block = xfs_btree_get_block(cur, 0, &bp);
1917 error = xfs_btree_check_block(cur, block, 0, bp);
1921 /* Get the address of the rec to be updated. */
1922 ptr = cur->bc_ptrs[0];
1923 rp = xfs_btree_rec_addr(cur, ptr, block);
1925 /* Fill in the new contents and log them. */
1926 xfs_btree_copy_recs(cur, rp, rec, 1);
1927 xfs_btree_log_recs(cur, bp, ptr, ptr);
1930 * If we are tracking the last record in the tree and
1931 * we are at the far right edge of the tree, update it.
1933 if (xfs_btree_is_lastrec(cur, block, 0)) {
1934 cur->bc_ops->update_lastrec(cur, block, rec,
1935 ptr, LASTREC_UPDATE);
1938 /* Updating first rec in leaf. Pass new key value up to our parent. */
1940 union xfs_btree_key key;
1942 cur->bc_ops->init_key_from_rec(&key, rec);
1943 error = xfs_btree_updkey(cur, &key, 1);
1948 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1952 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
1957 * Move 1 record left from cur/level if possible.
1958 * Update cur to reflect the new path.
1960 STATIC int /* error */
1962 struct xfs_btree_cur *cur,
1964 int *stat) /* success/failure */
1966 union xfs_btree_key key; /* btree key */
1967 struct xfs_buf *lbp; /* left buffer pointer */
1968 struct xfs_btree_block *left; /* left btree block */
1969 int lrecs; /* left record count */
1970 struct xfs_buf *rbp; /* right buffer pointer */
1971 struct xfs_btree_block *right; /* right btree block */
1972 int rrecs; /* right record count */
1973 union xfs_btree_ptr lptr; /* left btree pointer */
1974 union xfs_btree_key *rkp = NULL; /* right btree key */
1975 union xfs_btree_ptr *rpp = NULL; /* right address pointer */
1976 union xfs_btree_rec *rrp = NULL; /* right record pointer */
1977 int error; /* error return value */
1979 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1980 XFS_BTREE_TRACE_ARGI(cur, level);
1982 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
1983 level == cur->bc_nlevels - 1)
1986 /* Set up variables for this block as "right". */
1987 right = xfs_btree_get_block(cur, level, &rbp);
1990 error = xfs_btree_check_block(cur, right, level, rbp);
1995 /* If we've got no left sibling then we can't shift an entry left. */
1996 xfs_btree_get_sibling(cur, right, &lptr, XFS_BB_LEFTSIB);
1997 if (xfs_btree_ptr_is_null(cur, &lptr))
2001 * If the cursor entry is the one that would be moved, don't
2002 * do it... it's too complicated.
2004 if (cur->bc_ptrs[level] <= 1)
2007 /* Set up the left neighbor as "left". */
2008 error = xfs_btree_read_buf_block(cur, &lptr, level, 0, &left, &lbp);
2012 /* If it's full, it can't take another entry. */
2013 lrecs = xfs_btree_get_numrecs(left);
2014 if (lrecs == cur->bc_ops->get_maxrecs(cur, level))
2017 rrecs = xfs_btree_get_numrecs(right);
2020 * We add one entry to the left side and remove one for the right side.
2021 * Account for it here, the changes will be updated on disk and logged
2027 XFS_BTREE_STATS_INC(cur, lshift);
2028 XFS_BTREE_STATS_ADD(cur, moves, 1);
2031 * If non-leaf, copy a key and a ptr to the left block.
2032 * Log the changes to the left block.
2035 /* It's a non-leaf. Move keys and pointers. */
2036 union xfs_btree_key *lkp; /* left btree key */
2037 union xfs_btree_ptr *lpp; /* left address pointer */
2039 lkp = xfs_btree_key_addr(cur, lrecs, left);
2040 rkp = xfs_btree_key_addr(cur, 1, right);
2042 lpp = xfs_btree_ptr_addr(cur, lrecs, left);
2043 rpp = xfs_btree_ptr_addr(cur, 1, right);
2045 error = xfs_btree_check_ptr(cur, rpp, 0, level);
2049 xfs_btree_copy_keys(cur, lkp, rkp, 1);
2050 xfs_btree_copy_ptrs(cur, lpp, rpp, 1);
2052 xfs_btree_log_keys(cur, lbp, lrecs, lrecs);
2053 xfs_btree_log_ptrs(cur, lbp, lrecs, lrecs);
2055 ASSERT(cur->bc_ops->keys_inorder(cur,
2056 xfs_btree_key_addr(cur, lrecs - 1, left), lkp));
2058 /* It's a leaf. Move records. */
2059 union xfs_btree_rec *lrp; /* left record pointer */
2061 lrp = xfs_btree_rec_addr(cur, lrecs, left);
2062 rrp = xfs_btree_rec_addr(cur, 1, right);
2064 xfs_btree_copy_recs(cur, lrp, rrp, 1);
2065 xfs_btree_log_recs(cur, lbp, lrecs, lrecs);
2067 ASSERT(cur->bc_ops->recs_inorder(cur,
2068 xfs_btree_rec_addr(cur, lrecs - 1, left), lrp));
2071 xfs_btree_set_numrecs(left, lrecs);
2072 xfs_btree_log_block(cur, lbp, XFS_BB_NUMRECS);
2074 xfs_btree_set_numrecs(right, rrecs);
2075 xfs_btree_log_block(cur, rbp, XFS_BB_NUMRECS);
2078 * Slide the contents of right down one entry.
2080 XFS_BTREE_STATS_ADD(cur, moves, rrecs - 1);
2082 /* It's a nonleaf. operate on keys and ptrs */
2084 int i; /* loop index */
2086 for (i = 0; i < rrecs; i++) {
2087 error = xfs_btree_check_ptr(cur, rpp, i + 1, level);
2092 xfs_btree_shift_keys(cur,
2093 xfs_btree_key_addr(cur, 2, right),
2095 xfs_btree_shift_ptrs(cur,
2096 xfs_btree_ptr_addr(cur, 2, right),
2099 xfs_btree_log_keys(cur, rbp, 1, rrecs);
2100 xfs_btree_log_ptrs(cur, rbp, 1, rrecs);
2102 /* It's a leaf. operate on records */
2103 xfs_btree_shift_recs(cur,
2104 xfs_btree_rec_addr(cur, 2, right),
2106 xfs_btree_log_recs(cur, rbp, 1, rrecs);
2109 * If it's the first record in the block, we'll need a key
2110 * structure to pass up to the next level (updkey).
2112 cur->bc_ops->init_key_from_rec(&key,
2113 xfs_btree_rec_addr(cur, 1, right));
2117 /* Update the parent key values of right. */
2118 error = xfs_btree_updkey(cur, rkp, level + 1);
2122 /* Slide the cursor value left one. */
2123 cur->bc_ptrs[level]--;
2125 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2130 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2135 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
2140 * Move 1 record right from cur/level if possible.
2141 * Update cur to reflect the new path.
2143 STATIC int /* error */
2145 struct xfs_btree_cur *cur,
2147 int *stat) /* success/failure */
2149 union xfs_btree_key key; /* btree key */
2150 struct xfs_buf *lbp; /* left buffer pointer */
2151 struct xfs_btree_block *left; /* left btree block */
2152 struct xfs_buf *rbp; /* right buffer pointer */
2153 struct xfs_btree_block *right; /* right btree block */
2154 struct xfs_btree_cur *tcur; /* temporary btree cursor */
2155 union xfs_btree_ptr rptr; /* right block pointer */
2156 union xfs_btree_key *rkp; /* right btree key */
2157 int rrecs; /* right record count */
2158 int lrecs; /* left record count */
2159 int error; /* error return value */
2160 int i; /* loop counter */
2162 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
2163 XFS_BTREE_TRACE_ARGI(cur, level);
2165 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
2166 (level == cur->bc_nlevels - 1))
2169 /* Set up variables for this block as "left". */
2170 left = xfs_btree_get_block(cur, level, &lbp);
2173 error = xfs_btree_check_block(cur, left, level, lbp);
2178 /* If we've got no right sibling then we can't shift an entry right. */
2179 xfs_btree_get_sibling(cur, left, &rptr, XFS_BB_RIGHTSIB);
2180 if (xfs_btree_ptr_is_null(cur, &rptr))
2184 * If the cursor entry is the one that would be moved, don't
2185 * do it... it's too complicated.
2187 lrecs = xfs_btree_get_numrecs(left);
2188 if (cur->bc_ptrs[level] >= lrecs)
2191 /* Set up the right neighbor as "right". */
2192 error = xfs_btree_read_buf_block(cur, &rptr, level, 0, &right, &rbp);
2196 /* If it's full, it can't take another entry. */
2197 rrecs = xfs_btree_get_numrecs(right);
2198 if (rrecs == cur->bc_ops->get_maxrecs(cur, level))
2201 XFS_BTREE_STATS_INC(cur, rshift);
2202 XFS_BTREE_STATS_ADD(cur, moves, rrecs);
2205 * Make a hole at the start of the right neighbor block, then
2206 * copy the last left block entry to the hole.
2209 /* It's a nonleaf. make a hole in the keys and ptrs */
2210 union xfs_btree_key *lkp;
2211 union xfs_btree_ptr *lpp;
2212 union xfs_btree_ptr *rpp;
2214 lkp = xfs_btree_key_addr(cur, lrecs, left);
2215 lpp = xfs_btree_ptr_addr(cur, lrecs, left);
2216 rkp = xfs_btree_key_addr(cur, 1, right);
2217 rpp = xfs_btree_ptr_addr(cur, 1, right);
2220 for (i = rrecs - 1; i >= 0; i--) {
2221 error = xfs_btree_check_ptr(cur, rpp, i, level);
2227 xfs_btree_shift_keys(cur, rkp, 1, rrecs);
2228 xfs_btree_shift_ptrs(cur, rpp, 1, rrecs);
2231 error = xfs_btree_check_ptr(cur, lpp, 0, level);
2236 /* Now put the new data in, and log it. */
2237 xfs_btree_copy_keys(cur, rkp, lkp, 1);
2238 xfs_btree_copy_ptrs(cur, rpp, lpp, 1);
2240 xfs_btree_log_keys(cur, rbp, 1, rrecs + 1);
2241 xfs_btree_log_ptrs(cur, rbp, 1, rrecs + 1);
2243 ASSERT(cur->bc_ops->keys_inorder(cur, rkp,
2244 xfs_btree_key_addr(cur, 2, right)));
2246 /* It's a leaf. make a hole in the records */
2247 union xfs_btree_rec *lrp;
2248 union xfs_btree_rec *rrp;
2250 lrp = xfs_btree_rec_addr(cur, lrecs, left);
2251 rrp = xfs_btree_rec_addr(cur, 1, right);
2253 xfs_btree_shift_recs(cur, rrp, 1, rrecs);
2255 /* Now put the new data in, and log it. */
2256 xfs_btree_copy_recs(cur, rrp, lrp, 1);
2257 xfs_btree_log_recs(cur, rbp, 1, rrecs + 1);
2259 cur->bc_ops->init_key_from_rec(&key, rrp);
2262 ASSERT(cur->bc_ops->recs_inorder(cur, rrp,
2263 xfs_btree_rec_addr(cur, 2, right)));
2267 * Decrement and log left's numrecs, bump and log right's numrecs.
2269 xfs_btree_set_numrecs(left, --lrecs);
2270 xfs_btree_log_block(cur, lbp, XFS_BB_NUMRECS);
2272 xfs_btree_set_numrecs(right, ++rrecs);
2273 xfs_btree_log_block(cur, rbp, XFS_BB_NUMRECS);
2276 * Using a temporary cursor, update the parent key values of the
2277 * block on the right.
2279 error = xfs_btree_dup_cursor(cur, &tcur);
2282 i = xfs_btree_lastrec(tcur, level);
2283 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
2285 error = xfs_btree_increment(tcur, level, &i);
2289 error = xfs_btree_updkey(tcur, rkp, level + 1);
2293 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
2295 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2300 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2305 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
2309 XFS_BTREE_TRACE_CURSOR(tcur, XBT_ERROR);
2310 xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
2315 * Split cur/level block in half.
2316 * Return new block number and the key to its first
2317 * record (to be inserted into parent).
2319 STATIC int /* error */
2321 struct xfs_btree_cur *cur,
2323 union xfs_btree_ptr *ptrp,
2324 union xfs_btree_key *key,
2325 struct xfs_btree_cur **curp,
2326 int *stat) /* success/failure */
2328 union xfs_btree_ptr lptr; /* left sibling block ptr */
2329 struct xfs_buf *lbp; /* left buffer pointer */
2330 struct xfs_btree_block *left; /* left btree block */
2331 union xfs_btree_ptr rptr; /* right sibling block ptr */
2332 struct xfs_buf *rbp; /* right buffer pointer */
2333 struct xfs_btree_block *right; /* right btree block */
2334 union xfs_btree_ptr rrptr; /* right-right sibling ptr */
2335 struct xfs_buf *rrbp; /* right-right buffer pointer */
2336 struct xfs_btree_block *rrblock; /* right-right btree block */
2340 int error; /* error return value */
2345 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
2346 XFS_BTREE_TRACE_ARGIPK(cur, level, *ptrp, key);
2348 XFS_BTREE_STATS_INC(cur, split);
2350 /* Set up left block (current one). */
2351 left = xfs_btree_get_block(cur, level, &lbp);
2354 error = xfs_btree_check_block(cur, left, level, lbp);
2359 xfs_btree_buf_to_ptr(cur, lbp, &lptr);
2361 /* Allocate the new block. If we can't do it, we're toast. Give up. */
2362 error = cur->bc_ops->alloc_block(cur, &lptr, &rptr, 1, stat);
2367 XFS_BTREE_STATS_INC(cur, alloc);
2369 /* Set up the new block as "right". */
2370 error = xfs_btree_get_buf_block(cur, &rptr, 0, &right, &rbp);
2374 /* Fill in the btree header for the new right block. */
2375 xfs_btree_init_block_cur(cur, rbp, xfs_btree_get_level(left), 0);
2378 * Split the entries between the old and the new block evenly.
2379 * Make sure that if there's an odd number of entries now, that
2380 * each new block will have the same number of entries.
2382 lrecs = xfs_btree_get_numrecs(left);
2384 if ((lrecs & 1) && cur->bc_ptrs[level] <= rrecs + 1)
2386 src_index = (lrecs - rrecs + 1);
2388 XFS_BTREE_STATS_ADD(cur, moves, rrecs);
2391 * Copy btree block entries from the left block over to the
2392 * new block, the right. Update the right block and log the
2396 /* It's a non-leaf. Move keys and pointers. */
2397 union xfs_btree_key *lkp; /* left btree key */
2398 union xfs_btree_ptr *lpp; /* left address pointer */
2399 union xfs_btree_key *rkp; /* right btree key */
2400 union xfs_btree_ptr *rpp; /* right address pointer */
2402 lkp = xfs_btree_key_addr(cur, src_index, left);
2403 lpp = xfs_btree_ptr_addr(cur, src_index, left);
2404 rkp = xfs_btree_key_addr(cur, 1, right);
2405 rpp = xfs_btree_ptr_addr(cur, 1, right);
2408 for (i = src_index; i < rrecs; i++) {
2409 error = xfs_btree_check_ptr(cur, lpp, i, level);
2415 xfs_btree_copy_keys(cur, rkp, lkp, rrecs);
2416 xfs_btree_copy_ptrs(cur, rpp, lpp, rrecs);
2418 xfs_btree_log_keys(cur, rbp, 1, rrecs);
2419 xfs_btree_log_ptrs(cur, rbp, 1, rrecs);
2421 /* Grab the keys to the entries moved to the right block */
2422 xfs_btree_copy_keys(cur, key, rkp, 1);
2424 /* It's a leaf. Move records. */
2425 union xfs_btree_rec *lrp; /* left record pointer */
2426 union xfs_btree_rec *rrp; /* right record pointer */
2428 lrp = xfs_btree_rec_addr(cur, src_index, left);
2429 rrp = xfs_btree_rec_addr(cur, 1, right);
2431 xfs_btree_copy_recs(cur, rrp, lrp, rrecs);
2432 xfs_btree_log_recs(cur, rbp, 1, rrecs);
2434 cur->bc_ops->init_key_from_rec(key,
2435 xfs_btree_rec_addr(cur, 1, right));
2440 * Find the left block number by looking in the buffer.
2441 * Adjust numrecs, sibling pointers.
2443 xfs_btree_get_sibling(cur, left, &rrptr, XFS_BB_RIGHTSIB);
2444 xfs_btree_set_sibling(cur, right, &rrptr, XFS_BB_RIGHTSIB);
2445 xfs_btree_set_sibling(cur, right, &lptr, XFS_BB_LEFTSIB);
2446 xfs_btree_set_sibling(cur, left, &rptr, XFS_BB_RIGHTSIB);
2449 xfs_btree_set_numrecs(left, lrecs);
2450 xfs_btree_set_numrecs(right, xfs_btree_get_numrecs(right) + rrecs);
2452 xfs_btree_log_block(cur, rbp, XFS_BB_ALL_BITS);
2453 xfs_btree_log_block(cur, lbp, XFS_BB_NUMRECS | XFS_BB_RIGHTSIB);
2456 * If there's a block to the new block's right, make that block
2457 * point back to right instead of to left.
2459 if (!xfs_btree_ptr_is_null(cur, &rrptr)) {
2460 error = xfs_btree_read_buf_block(cur, &rrptr, level,
2461 0, &rrblock, &rrbp);
2464 xfs_btree_set_sibling(cur, rrblock, &rptr, XFS_BB_LEFTSIB);
2465 xfs_btree_log_block(cur, rrbp, XFS_BB_LEFTSIB);
2468 * If the cursor is really in the right block, move it there.
2469 * If it's just pointing past the last entry in left, then we'll
2470 * insert there, so don't change anything in that case.
2472 if (cur->bc_ptrs[level] > lrecs + 1) {
2473 xfs_btree_setbuf(cur, level, rbp);
2474 cur->bc_ptrs[level] -= lrecs;
2477 * If there are more levels, we'll need another cursor which refers
2478 * the right block, no matter where this cursor was.
2480 if (level + 1 < cur->bc_nlevels) {
2481 error = xfs_btree_dup_cursor(cur, curp);
2484 (*curp)->bc_ptrs[level + 1]++;
2487 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2491 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2496 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
2501 * Copy the old inode root contents into a real block and make the
2502 * broot point to it.
2505 xfs_btree_new_iroot(
2506 struct xfs_btree_cur *cur, /* btree cursor */
2507 int *logflags, /* logging flags for inode */
2508 int *stat) /* return status - 0 fail */
2510 struct xfs_buf *cbp; /* buffer for cblock */
2511 struct xfs_btree_block *block; /* btree block */
2512 struct xfs_btree_block *cblock; /* child btree block */
2513 union xfs_btree_key *ckp; /* child key pointer */
2514 union xfs_btree_ptr *cpp; /* child ptr pointer */
2515 union xfs_btree_key *kp; /* pointer to btree key */
2516 union xfs_btree_ptr *pp; /* pointer to block addr */
2517 union xfs_btree_ptr nptr; /* new block addr */
2518 int level; /* btree level */
2519 int error; /* error return code */
2521 int i; /* loop counter */
2524 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
2525 XFS_BTREE_STATS_INC(cur, newroot);
2527 ASSERT(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE);
2529 level = cur->bc_nlevels - 1;
2531 block = xfs_btree_get_iroot(cur);
2532 pp = xfs_btree_ptr_addr(cur, 1, block);
2534 /* Allocate the new block. If we can't do it, we're toast. Give up. */
2535 error = cur->bc_ops->alloc_block(cur, pp, &nptr, 1, stat);
2539 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2542 XFS_BTREE_STATS_INC(cur, alloc);
2544 /* Copy the root into a real block. */
2545 error = xfs_btree_get_buf_block(cur, &nptr, 0, &cblock, &cbp);
2550 * we can't just memcpy() the root in for CRC enabled btree blocks.
2551 * In that case have to also ensure the blkno remains correct
2553 memcpy(cblock, block, xfs_btree_block_len(cur));
2554 if (cur->bc_flags & XFS_BTREE_CRC_BLOCKS) {
2555 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
2556 cblock->bb_u.l.bb_blkno = cpu_to_be64(cbp->b_bn);
2558 cblock->bb_u.s.bb_blkno = cpu_to_be64(cbp->b_bn);
2561 be16_add_cpu(&block->bb_level, 1);
2562 xfs_btree_set_numrecs(block, 1);
2564 cur->bc_ptrs[level + 1] = 1;
2566 kp = xfs_btree_key_addr(cur, 1, block);
2567 ckp = xfs_btree_key_addr(cur, 1, cblock);
2568 xfs_btree_copy_keys(cur, ckp, kp, xfs_btree_get_numrecs(cblock));
2570 cpp = xfs_btree_ptr_addr(cur, 1, cblock);
2572 for (i = 0; i < be16_to_cpu(cblock->bb_numrecs); i++) {
2573 error = xfs_btree_check_ptr(cur, pp, i, level);
2578 xfs_btree_copy_ptrs(cur, cpp, pp, xfs_btree_get_numrecs(cblock));
2581 error = xfs_btree_check_ptr(cur, &nptr, 0, level);
2585 xfs_btree_copy_ptrs(cur, pp, &nptr, 1);
2587 xfs_iroot_realloc(cur->bc_private.b.ip,
2588 1 - xfs_btree_get_numrecs(cblock),
2589 cur->bc_private.b.whichfork);
2591 xfs_btree_setbuf(cur, level, cbp);
2594 * Do all this logging at the end so that
2595 * the root is at the right level.
2597 xfs_btree_log_block(cur, cbp, XFS_BB_ALL_BITS);
2598 xfs_btree_log_keys(cur, cbp, 1, be16_to_cpu(cblock->bb_numrecs));
2599 xfs_btree_log_ptrs(cur, cbp, 1, be16_to_cpu(cblock->bb_numrecs));
2602 XFS_ILOG_CORE | xfs_ilog_fbroot(cur->bc_private.b.whichfork);
2604 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2607 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
2612 * Allocate a new root block, fill it in.
2614 STATIC int /* error */
2616 struct xfs_btree_cur *cur, /* btree cursor */
2617 int *stat) /* success/failure */
2619 struct xfs_btree_block *block; /* one half of the old root block */
2620 struct xfs_buf *bp; /* buffer containing block */
2621 int error; /* error return value */
2622 struct xfs_buf *lbp; /* left buffer pointer */
2623 struct xfs_btree_block *left; /* left btree block */
2624 struct xfs_buf *nbp; /* new (root) buffer */
2625 struct xfs_btree_block *new; /* new (root) btree block */
2626 int nptr; /* new value for key index, 1 or 2 */
2627 struct xfs_buf *rbp; /* right buffer pointer */
2628 struct xfs_btree_block *right; /* right btree block */
2629 union xfs_btree_ptr rptr;
2630 union xfs_btree_ptr lptr;
2632 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
2633 XFS_BTREE_STATS_INC(cur, newroot);
2635 /* initialise our start point from the cursor */
2636 cur->bc_ops->init_ptr_from_cur(cur, &rptr);
2638 /* Allocate the new block. If we can't do it, we're toast. Give up. */
2639 error = cur->bc_ops->alloc_block(cur, &rptr, &lptr, 1, stat);
2644 XFS_BTREE_STATS_INC(cur, alloc);
2646 /* Set up the new block. */
2647 error = xfs_btree_get_buf_block(cur, &lptr, 0, &new, &nbp);
2651 /* Set the root in the holding structure increasing the level by 1. */
2652 cur->bc_ops->set_root(cur, &lptr, 1);
2655 * At the previous root level there are now two blocks: the old root,
2656 * and the new block generated when it was split. We don't know which
2657 * one the cursor is pointing at, so we set up variables "left" and
2658 * "right" for each case.
2660 block = xfs_btree_get_block(cur, cur->bc_nlevels - 1, &bp);
2663 error = xfs_btree_check_block(cur, block, cur->bc_nlevels - 1, bp);
2668 xfs_btree_get_sibling(cur, block, &rptr, XFS_BB_RIGHTSIB);
2669 if (!xfs_btree_ptr_is_null(cur, &rptr)) {
2670 /* Our block is left, pick up the right block. */
2672 xfs_btree_buf_to_ptr(cur, lbp, &lptr);
2674 error = xfs_btree_read_buf_block(cur, &rptr,
2675 cur->bc_nlevels - 1, 0, &right, &rbp);
2681 /* Our block is right, pick up the left block. */
2683 xfs_btree_buf_to_ptr(cur, rbp, &rptr);
2685 xfs_btree_get_sibling(cur, right, &lptr, XFS_BB_LEFTSIB);
2686 error = xfs_btree_read_buf_block(cur, &lptr,
2687 cur->bc_nlevels - 1, 0, &left, &lbp);
2693 /* Fill in the new block's btree header and log it. */
2694 xfs_btree_init_block_cur(cur, nbp, cur->bc_nlevels, 2);
2695 xfs_btree_log_block(cur, nbp, XFS_BB_ALL_BITS);
2696 ASSERT(!xfs_btree_ptr_is_null(cur, &lptr) &&
2697 !xfs_btree_ptr_is_null(cur, &rptr));
2699 /* Fill in the key data in the new root. */
2700 if (xfs_btree_get_level(left) > 0) {
2701 xfs_btree_copy_keys(cur,
2702 xfs_btree_key_addr(cur, 1, new),
2703 xfs_btree_key_addr(cur, 1, left), 1);
2704 xfs_btree_copy_keys(cur,
2705 xfs_btree_key_addr(cur, 2, new),
2706 xfs_btree_key_addr(cur, 1, right), 1);
2708 cur->bc_ops->init_key_from_rec(
2709 xfs_btree_key_addr(cur, 1, new),
2710 xfs_btree_rec_addr(cur, 1, left));
2711 cur->bc_ops->init_key_from_rec(
2712 xfs_btree_key_addr(cur, 2, new),
2713 xfs_btree_rec_addr(cur, 1, right));
2715 xfs_btree_log_keys(cur, nbp, 1, 2);
2717 /* Fill in the pointer data in the new root. */
2718 xfs_btree_copy_ptrs(cur,
2719 xfs_btree_ptr_addr(cur, 1, new), &lptr, 1);
2720 xfs_btree_copy_ptrs(cur,
2721 xfs_btree_ptr_addr(cur, 2, new), &rptr, 1);
2722 xfs_btree_log_ptrs(cur, nbp, 1, 2);
2724 /* Fix up the cursor. */
2725 xfs_btree_setbuf(cur, cur->bc_nlevels, nbp);
2726 cur->bc_ptrs[cur->bc_nlevels] = nptr;
2728 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2732 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
2735 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2741 xfs_btree_make_block_unfull(
2742 struct xfs_btree_cur *cur, /* btree cursor */
2743 int level, /* btree level */
2744 int numrecs,/* # of recs in block */
2745 int *oindex,/* old tree index */
2746 int *index, /* new tree index */
2747 union xfs_btree_ptr *nptr, /* new btree ptr */
2748 struct xfs_btree_cur **ncur, /* new btree cursor */
2749 union xfs_btree_rec *nrec, /* new record */
2752 union xfs_btree_key key; /* new btree key value */
2755 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
2756 level == cur->bc_nlevels - 1) {
2757 struct xfs_inode *ip = cur->bc_private.b.ip;
2759 if (numrecs < cur->bc_ops->get_dmaxrecs(cur, level)) {
2760 /* A root block that can be made bigger. */
2761 xfs_iroot_realloc(ip, 1, cur->bc_private.b.whichfork);
2763 /* A root block that needs replacing */
2766 error = xfs_btree_new_iroot(cur, &logflags, stat);
2767 if (error || *stat == 0)
2770 xfs_trans_log_inode(cur->bc_tp, ip, logflags);
2776 /* First, try shifting an entry to the right neighbor. */
2777 error = xfs_btree_rshift(cur, level, stat);
2781 /* Next, try shifting an entry to the left neighbor. */
2782 error = xfs_btree_lshift(cur, level, stat);
2787 *oindex = *index = cur->bc_ptrs[level];
2792 * Next, try splitting the current block in half.
2794 * If this works we have to re-set our variables because we
2795 * could be in a different block now.
2797 error = xfs_btree_split(cur, level, nptr, &key, ncur, stat);
2798 if (error || *stat == 0)
2802 *index = cur->bc_ptrs[level];
2803 cur->bc_ops->init_rec_from_key(&key, nrec);
2808 * Insert one record/level. Return information to the caller
2809 * allowing the next level up to proceed if necessary.
2813 struct xfs_btree_cur *cur, /* btree cursor */
2814 int level, /* level to insert record at */
2815 union xfs_btree_ptr *ptrp, /* i/o: block number inserted */
2816 union xfs_btree_rec *recp, /* i/o: record data inserted */
2817 struct xfs_btree_cur **curp, /* output: new cursor replacing cur */
2818 int *stat) /* success/failure */
2820 struct xfs_btree_block *block; /* btree block */
2821 struct xfs_buf *bp; /* buffer for block */
2822 union xfs_btree_key key; /* btree key */
2823 union xfs_btree_ptr nptr; /* new block ptr */
2824 struct xfs_btree_cur *ncur; /* new btree cursor */
2825 union xfs_btree_rec nrec; /* new record count */
2826 int optr; /* old key/record index */
2827 int ptr; /* key/record index */
2828 int numrecs;/* number of records */
2829 int error; /* error return value */
2834 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
2835 XFS_BTREE_TRACE_ARGIPR(cur, level, *ptrp, recp);
2840 * If we have an external root pointer, and we've made it to the
2841 * root level, allocate a new root block and we're done.
2843 if (!(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
2844 (level >= cur->bc_nlevels)) {
2845 error = xfs_btree_new_root(cur, stat);
2846 xfs_btree_set_ptr_null(cur, ptrp);
2848 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2852 /* If we're off the left edge, return failure. */
2853 ptr = cur->bc_ptrs[level];
2855 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2860 /* Make a key out of the record data to be inserted, and save it. */
2861 cur->bc_ops->init_key_from_rec(&key, recp);
2865 XFS_BTREE_STATS_INC(cur, insrec);
2867 /* Get pointers to the btree buffer and block. */
2868 block = xfs_btree_get_block(cur, level, &bp);
2869 numrecs = xfs_btree_get_numrecs(block);
2872 error = xfs_btree_check_block(cur, block, level, bp);
2876 /* Check that the new entry is being inserted in the right place. */
2877 if (ptr <= numrecs) {
2879 ASSERT(cur->bc_ops->recs_inorder(cur, recp,
2880 xfs_btree_rec_addr(cur, ptr, block)));
2882 ASSERT(cur->bc_ops->keys_inorder(cur, &key,
2883 xfs_btree_key_addr(cur, ptr, block)));
2889 * If the block is full, we can't insert the new entry until we
2890 * make the block un-full.
2892 xfs_btree_set_ptr_null(cur, &nptr);
2893 if (numrecs == cur->bc_ops->get_maxrecs(cur, level)) {
2894 error = xfs_btree_make_block_unfull(cur, level, numrecs,
2895 &optr, &ptr, &nptr, &ncur, &nrec, stat);
2896 if (error || *stat == 0)
2901 * The current block may have changed if the block was
2902 * previously full and we have just made space in it.
2904 block = xfs_btree_get_block(cur, level, &bp);
2905 numrecs = xfs_btree_get_numrecs(block);
2908 error = xfs_btree_check_block(cur, block, level, bp);
2914 * At this point we know there's room for our new entry in the block
2915 * we're pointing at.
2917 XFS_BTREE_STATS_ADD(cur, moves, numrecs - ptr + 1);
2920 /* It's a nonleaf. make a hole in the keys and ptrs */
2921 union xfs_btree_key *kp;
2922 union xfs_btree_ptr *pp;
2924 kp = xfs_btree_key_addr(cur, ptr, block);
2925 pp = xfs_btree_ptr_addr(cur, ptr, block);
2928 for (i = numrecs - ptr; i >= 0; i--) {
2929 error = xfs_btree_check_ptr(cur, pp, i, level);
2935 xfs_btree_shift_keys(cur, kp, 1, numrecs - ptr + 1);
2936 xfs_btree_shift_ptrs(cur, pp, 1, numrecs - ptr + 1);
2939 error = xfs_btree_check_ptr(cur, ptrp, 0, level);
2944 /* Now put the new data in, bump numrecs and log it. */
2945 xfs_btree_copy_keys(cur, kp, &key, 1);
2946 xfs_btree_copy_ptrs(cur, pp, ptrp, 1);
2948 xfs_btree_set_numrecs(block, numrecs);
2949 xfs_btree_log_ptrs(cur, bp, ptr, numrecs);
2950 xfs_btree_log_keys(cur, bp, ptr, numrecs);
2952 if (ptr < numrecs) {
2953 ASSERT(cur->bc_ops->keys_inorder(cur, kp,
2954 xfs_btree_key_addr(cur, ptr + 1, block)));
2958 /* It's a leaf. make a hole in the records */
2959 union xfs_btree_rec *rp;
2961 rp = xfs_btree_rec_addr(cur, ptr, block);
2963 xfs_btree_shift_recs(cur, rp, 1, numrecs - ptr + 1);
2965 /* Now put the new data in, bump numrecs and log it. */
2966 xfs_btree_copy_recs(cur, rp, recp, 1);
2967 xfs_btree_set_numrecs(block, ++numrecs);
2968 xfs_btree_log_recs(cur, bp, ptr, numrecs);
2970 if (ptr < numrecs) {
2971 ASSERT(cur->bc_ops->recs_inorder(cur, rp,
2972 xfs_btree_rec_addr(cur, ptr + 1, block)));
2977 /* Log the new number of records in the btree header. */
2978 xfs_btree_log_block(cur, bp, XFS_BB_NUMRECS);
2980 /* If we inserted at the start of a block, update the parents' keys. */
2982 error = xfs_btree_updkey(cur, &key, level + 1);
2988 * If we are tracking the last record in the tree and
2989 * we are at the far right edge of the tree, update it.
2991 if (xfs_btree_is_lastrec(cur, block, level)) {
2992 cur->bc_ops->update_lastrec(cur, block, recp,
2993 ptr, LASTREC_INSREC);
2997 * Return the new block number, if any.
2998 * If there is one, give back a record value and a cursor too.
3001 if (!xfs_btree_ptr_is_null(cur, &nptr)) {
3006 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3011 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
3016 * Insert the record at the point referenced by cur.
3018 * A multi-level split of the tree on insert will invalidate the original
3019 * cursor. All callers of this function should assume that the cursor is
3020 * no longer valid and revalidate it.
3024 struct xfs_btree_cur *cur,
3027 int error; /* error return value */
3028 int i; /* result value, 0 for failure */
3029 int level; /* current level number in btree */
3030 union xfs_btree_ptr nptr; /* new block number (split result) */
3031 struct xfs_btree_cur *ncur; /* new cursor (split result) */
3032 struct xfs_btree_cur *pcur; /* previous level's cursor */
3033 union xfs_btree_rec rec; /* record to insert */
3039 xfs_btree_set_ptr_null(cur, &nptr);
3040 cur->bc_ops->init_rec_from_cur(cur, &rec);
3043 * Loop going up the tree, starting at the leaf level.
3044 * Stop when we don't get a split block, that must mean that
3045 * the insert is finished with this level.
3049 * Insert nrec/nptr into this level of the tree.
3050 * Note if we fail, nptr will be null.
3052 error = xfs_btree_insrec(pcur, level, &nptr, &rec, &ncur, &i);
3055 xfs_btree_del_cursor(pcur, XFS_BTREE_ERROR);
3059 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
3063 * See if the cursor we just used is trash.
3064 * Can't trash the caller's cursor, but otherwise we should
3065 * if ncur is a new cursor or we're about to be done.
3068 (ncur || xfs_btree_ptr_is_null(cur, &nptr))) {
3069 /* Save the state from the cursor before we trash it */
3070 if (cur->bc_ops->update_cursor)
3071 cur->bc_ops->update_cursor(pcur, cur);
3072 cur->bc_nlevels = pcur->bc_nlevels;
3073 xfs_btree_del_cursor(pcur, XFS_BTREE_NOERROR);
3075 /* If we got a new cursor, switch to it. */
3080 } while (!xfs_btree_ptr_is_null(cur, &nptr));
3082 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3086 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
3091 * Try to merge a non-leaf block back into the inode root.
3093 * Note: the killroot names comes from the fact that we're effectively
3094 * killing the old root block. But because we can't just delete the
3095 * inode we have to copy the single block it was pointing to into the
3099 xfs_btree_kill_iroot(
3100 struct xfs_btree_cur *cur)
3102 int whichfork = cur->bc_private.b.whichfork;
3103 struct xfs_inode *ip = cur->bc_private.b.ip;
3104 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
3105 struct xfs_btree_block *block;
3106 struct xfs_btree_block *cblock;
3107 union xfs_btree_key *kp;
3108 union xfs_btree_key *ckp;
3109 union xfs_btree_ptr *pp;
3110 union xfs_btree_ptr *cpp;
3111 struct xfs_buf *cbp;
3116 union xfs_btree_ptr ptr;
3120 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
3122 ASSERT(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE);
3123 ASSERT(cur->bc_nlevels > 1);
3126 * Don't deal with the root block needs to be a leaf case.
3127 * We're just going to turn the thing back into extents anyway.
3129 level = cur->bc_nlevels - 1;
3134 * Give up if the root has multiple children.
3136 block = xfs_btree_get_iroot(cur);
3137 if (xfs_btree_get_numrecs(block) != 1)
3140 cblock = xfs_btree_get_block(cur, level - 1, &cbp);
3141 numrecs = xfs_btree_get_numrecs(cblock);
3144 * Only do this if the next level will fit.
3145 * Then the data must be copied up to the inode,
3146 * instead of freeing the root you free the next level.
3148 if (numrecs > cur->bc_ops->get_dmaxrecs(cur, level))
3151 XFS_BTREE_STATS_INC(cur, killroot);
3154 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_LEFTSIB);
3155 ASSERT(xfs_btree_ptr_is_null(cur, &ptr));
3156 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_RIGHTSIB);
3157 ASSERT(xfs_btree_ptr_is_null(cur, &ptr));
3160 index = numrecs - cur->bc_ops->get_maxrecs(cur, level);
3162 xfs_iroot_realloc(cur->bc_private.b.ip, index,
3163 cur->bc_private.b.whichfork);
3164 block = ifp->if_broot;
3167 be16_add_cpu(&block->bb_numrecs, index);
3168 ASSERT(block->bb_numrecs == cblock->bb_numrecs);
3170 kp = xfs_btree_key_addr(cur, 1, block);
3171 ckp = xfs_btree_key_addr(cur, 1, cblock);
3172 xfs_btree_copy_keys(cur, kp, ckp, numrecs);
3174 pp = xfs_btree_ptr_addr(cur, 1, block);
3175 cpp = xfs_btree_ptr_addr(cur, 1, cblock);
3177 for (i = 0; i < numrecs; i++) {
3180 error = xfs_btree_check_ptr(cur, cpp, i, level - 1);
3182 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
3187 xfs_btree_copy_ptrs(cur, pp, cpp, numrecs);
3189 cur->bc_ops->free_block(cur, cbp);
3190 XFS_BTREE_STATS_INC(cur, free);
3192 cur->bc_bufs[level - 1] = NULL;
3193 be16_add_cpu(&block->bb_level, -1);
3194 xfs_trans_log_inode(cur->bc_tp, ip,
3195 XFS_ILOG_CORE | xfs_ilog_fbroot(cur->bc_private.b.whichfork));
3198 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3203 * Kill the current root node, and replace it with it's only child node.
3206 xfs_btree_kill_root(
3207 struct xfs_btree_cur *cur,
3210 union xfs_btree_ptr *newroot)
3214 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
3215 XFS_BTREE_STATS_INC(cur, killroot);
3218 * Update the root pointer, decreasing the level by 1 and then
3219 * free the old root.
3221 cur->bc_ops->set_root(cur, newroot, -1);
3223 error = cur->bc_ops->free_block(cur, bp);
3225 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
3229 XFS_BTREE_STATS_INC(cur, free);
3231 cur->bc_bufs[level] = NULL;
3232 cur->bc_ra[level] = 0;
3235 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3240 xfs_btree_dec_cursor(
3241 struct xfs_btree_cur *cur,
3249 error = xfs_btree_decrement(cur, level, &i);
3254 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3260 * Single level of the btree record deletion routine.
3261 * Delete record pointed to by cur/level.
3262 * Remove the record from its block then rebalance the tree.
3263 * Return 0 for error, 1 for done, 2 to go on to the next level.
3265 STATIC int /* error */
3267 struct xfs_btree_cur *cur, /* btree cursor */
3268 int level, /* level removing record from */
3269 int *stat) /* fail/done/go-on */
3271 struct xfs_btree_block *block; /* btree block */
3272 union xfs_btree_ptr cptr; /* current block ptr */
3273 struct xfs_buf *bp; /* buffer for block */
3274 int error; /* error return value */
3275 int i; /* loop counter */
3276 union xfs_btree_key key; /* storage for keyp */
3277 union xfs_btree_key *keyp = &key; /* passed to the next level */
3278 union xfs_btree_ptr lptr; /* left sibling block ptr */
3279 struct xfs_buf *lbp; /* left buffer pointer */
3280 struct xfs_btree_block *left; /* left btree block */
3281 int lrecs = 0; /* left record count */
3282 int ptr; /* key/record index */
3283 union xfs_btree_ptr rptr; /* right sibling block ptr */
3284 struct xfs_buf *rbp; /* right buffer pointer */
3285 struct xfs_btree_block *right; /* right btree block */
3286 struct xfs_btree_block *rrblock; /* right-right btree block */
3287 struct xfs_buf *rrbp; /* right-right buffer pointer */
3288 int rrecs = 0; /* right record count */
3289 struct xfs_btree_cur *tcur; /* temporary btree cursor */
3290 int numrecs; /* temporary numrec count */
3292 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
3293 XFS_BTREE_TRACE_ARGI(cur, level);
3297 /* Get the index of the entry being deleted, check for nothing there. */
3298 ptr = cur->bc_ptrs[level];
3300 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3305 /* Get the buffer & block containing the record or key/ptr. */
3306 block = xfs_btree_get_block(cur, level, &bp);
3307 numrecs = xfs_btree_get_numrecs(block);
3310 error = xfs_btree_check_block(cur, block, level, bp);
3315 /* Fail if we're off the end of the block. */
3316 if (ptr > numrecs) {
3317 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3322 XFS_BTREE_STATS_INC(cur, delrec);
3323 XFS_BTREE_STATS_ADD(cur, moves, numrecs - ptr);
3325 /* Excise the entries being deleted. */
3327 /* It's a nonleaf. operate on keys and ptrs */
3328 union xfs_btree_key *lkp;
3329 union xfs_btree_ptr *lpp;
3331 lkp = xfs_btree_key_addr(cur, ptr + 1, block);
3332 lpp = xfs_btree_ptr_addr(cur, ptr + 1, block);
3335 for (i = 0; i < numrecs - ptr; i++) {
3336 error = xfs_btree_check_ptr(cur, lpp, i, level);
3342 if (ptr < numrecs) {
3343 xfs_btree_shift_keys(cur, lkp, -1, numrecs - ptr);
3344 xfs_btree_shift_ptrs(cur, lpp, -1, numrecs - ptr);
3345 xfs_btree_log_keys(cur, bp, ptr, numrecs - 1);
3346 xfs_btree_log_ptrs(cur, bp, ptr, numrecs - 1);
3350 * If it's the first record in the block, we'll need to pass a
3351 * key up to the next level (updkey).
3354 keyp = xfs_btree_key_addr(cur, 1, block);
3356 /* It's a leaf. operate on records */
3357 if (ptr < numrecs) {
3358 xfs_btree_shift_recs(cur,
3359 xfs_btree_rec_addr(cur, ptr + 1, block),
3361 xfs_btree_log_recs(cur, bp, ptr, numrecs - 1);
3365 * If it's the first record in the block, we'll need a key
3366 * structure to pass up to the next level (updkey).
3369 cur->bc_ops->init_key_from_rec(&key,
3370 xfs_btree_rec_addr(cur, 1, block));
3376 * Decrement and log the number of entries in the block.
3378 xfs_btree_set_numrecs(block, --numrecs);
3379 xfs_btree_log_block(cur, bp, XFS_BB_NUMRECS);
3382 * If we are tracking the last record in the tree and
3383 * we are at the far right edge of the tree, update it.
3385 if (xfs_btree_is_lastrec(cur, block, level)) {
3386 cur->bc_ops->update_lastrec(cur, block, NULL,
3387 ptr, LASTREC_DELREC);
3391 * We're at the root level. First, shrink the root block in-memory.
3392 * Try to get rid of the next level down. If we can't then there's
3393 * nothing left to do.
3395 if (level == cur->bc_nlevels - 1) {
3396 if (cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) {
3397 xfs_iroot_realloc(cur->bc_private.b.ip, -1,
3398 cur->bc_private.b.whichfork);
3400 error = xfs_btree_kill_iroot(cur);
3404 error = xfs_btree_dec_cursor(cur, level, stat);
3412 * If this is the root level, and there's only one entry left,
3413 * and it's NOT the leaf level, then we can get rid of this
3416 if (numrecs == 1 && level > 0) {
3417 union xfs_btree_ptr *pp;
3419 * pp is still set to the first pointer in the block.
3420 * Make it the new root of the btree.
3422 pp = xfs_btree_ptr_addr(cur, 1, block);
3423 error = xfs_btree_kill_root(cur, bp, level, pp);
3426 } else if (level > 0) {
3427 error = xfs_btree_dec_cursor(cur, level, stat);
3436 * If we deleted the leftmost entry in the block, update the
3437 * key values above us in the tree.
3440 error = xfs_btree_updkey(cur, keyp, level + 1);
3446 * If the number of records remaining in the block is at least
3447 * the minimum, we're done.
3449 if (numrecs >= cur->bc_ops->get_minrecs(cur, level)) {
3450 error = xfs_btree_dec_cursor(cur, level, stat);
3457 * Otherwise, we have to move some records around to keep the
3458 * tree balanced. Look at the left and right sibling blocks to
3459 * see if we can re-balance by moving only one record.
3461 xfs_btree_get_sibling(cur, block, &rptr, XFS_BB_RIGHTSIB);
3462 xfs_btree_get_sibling(cur, block, &lptr, XFS_BB_LEFTSIB);
3464 if (cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) {
3466 * One child of root, need to get a chance to copy its contents
3467 * into the root and delete it. Can't go up to next level,
3468 * there's nothing to delete there.
3470 if (xfs_btree_ptr_is_null(cur, &rptr) &&
3471 xfs_btree_ptr_is_null(cur, &lptr) &&
3472 level == cur->bc_nlevels - 2) {
3473 error = xfs_btree_kill_iroot(cur);
3475 error = xfs_btree_dec_cursor(cur, level, stat);
3482 ASSERT(!xfs_btree_ptr_is_null(cur, &rptr) ||
3483 !xfs_btree_ptr_is_null(cur, &lptr));
3486 * Duplicate the cursor so our btree manipulations here won't
3487 * disrupt the next level up.
3489 error = xfs_btree_dup_cursor(cur, &tcur);
3494 * If there's a right sibling, see if it's ok to shift an entry
3497 if (!xfs_btree_ptr_is_null(cur, &rptr)) {
3499 * Move the temp cursor to the last entry in the next block.
3500 * Actually any entry but the first would suffice.
3502 i = xfs_btree_lastrec(tcur, level);
3503 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
3505 error = xfs_btree_increment(tcur, level, &i);
3508 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
3510 i = xfs_btree_lastrec(tcur, level);
3511 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
3513 /* Grab a pointer to the block. */
3514 right = xfs_btree_get_block(tcur, level, &rbp);
3516 error = xfs_btree_check_block(tcur, right, level, rbp);
3520 /* Grab the current block number, for future use. */
3521 xfs_btree_get_sibling(tcur, right, &cptr, XFS_BB_LEFTSIB);
3524 * If right block is full enough so that removing one entry
3525 * won't make it too empty, and left-shifting an entry out
3526 * of right to us works, we're done.
3528 if (xfs_btree_get_numrecs(right) - 1 >=
3529 cur->bc_ops->get_minrecs(tcur, level)) {
3530 error = xfs_btree_lshift(tcur, level, &i);
3534 ASSERT(xfs_btree_get_numrecs(block) >=
3535 cur->bc_ops->get_minrecs(tcur, level));
3537 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
3540 error = xfs_btree_dec_cursor(cur, level, stat);
3548 * Otherwise, grab the number of records in right for
3549 * future reference, and fix up the temp cursor to point
3550 * to our block again (last record).
3552 rrecs = xfs_btree_get_numrecs(right);
3553 if (!xfs_btree_ptr_is_null(cur, &lptr)) {
3554 i = xfs_btree_firstrec(tcur, level);
3555 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
3557 error = xfs_btree_decrement(tcur, level, &i);
3560 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
3565 * If there's a left sibling, see if it's ok to shift an entry
3568 if (!xfs_btree_ptr_is_null(cur, &lptr)) {
3570 * Move the temp cursor to the first entry in the
3573 i = xfs_btree_firstrec(tcur, level);
3574 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
3576 error = xfs_btree_decrement(tcur, level, &i);
3579 i = xfs_btree_firstrec(tcur, level);
3580 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
3582 /* Grab a pointer to the block. */
3583 left = xfs_btree_get_block(tcur, level, &lbp);
3585 error = xfs_btree_check_block(cur, left, level, lbp);
3589 /* Grab the current block number, for future use. */
3590 xfs_btree_get_sibling(tcur, left, &cptr, XFS_BB_RIGHTSIB);
3593 * If left block is full enough so that removing one entry
3594 * won't make it too empty, and right-shifting an entry out
3595 * of left to us works, we're done.
3597 if (xfs_btree_get_numrecs(left) - 1 >=
3598 cur->bc_ops->get_minrecs(tcur, level)) {
3599 error = xfs_btree_rshift(tcur, level, &i);
3603 ASSERT(xfs_btree_get_numrecs(block) >=
3604 cur->bc_ops->get_minrecs(tcur, level));
3605 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
3609 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3616 * Otherwise, grab the number of records in right for
3619 lrecs = xfs_btree_get_numrecs(left);
3622 /* Delete the temp cursor, we're done with it. */
3623 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
3626 /* If here, we need to do a join to keep the tree balanced. */
3627 ASSERT(!xfs_btree_ptr_is_null(cur, &cptr));
3629 if (!xfs_btree_ptr_is_null(cur, &lptr) &&
3630 lrecs + xfs_btree_get_numrecs(block) <=
3631 cur->bc_ops->get_maxrecs(cur, level)) {
3633 * Set "right" to be the starting block,
3634 * "left" to be the left neighbor.
3639 error = xfs_btree_read_buf_block(cur, &lptr, level,
3645 * If that won't work, see if we can join with the right neighbor block.
3647 } else if (!xfs_btree_ptr_is_null(cur, &rptr) &&
3648 rrecs + xfs_btree_get_numrecs(block) <=
3649 cur->bc_ops->get_maxrecs(cur, level)) {
3651 * Set "left" to be the starting block,
3652 * "right" to be the right neighbor.
3657 error = xfs_btree_read_buf_block(cur, &rptr, level,
3663 * Otherwise, we can't fix the imbalance.
3664 * Just return. This is probably a logic error, but it's not fatal.
3667 error = xfs_btree_dec_cursor(cur, level, stat);
3673 rrecs = xfs_btree_get_numrecs(right);
3674 lrecs = xfs_btree_get_numrecs(left);
3677 * We're now going to join "left" and "right" by moving all the stuff
3678 * in "right" to "left" and deleting "right".
3680 XFS_BTREE_STATS_ADD(cur, moves, rrecs);
3682 /* It's a non-leaf. Move keys and pointers. */
3683 union xfs_btree_key *lkp; /* left btree key */
3684 union xfs_btree_ptr *lpp; /* left address pointer */
3685 union xfs_btree_key *rkp; /* right btree key */
3686 union xfs_btree_ptr *rpp; /* right address pointer */
3688 lkp = xfs_btree_key_addr(cur, lrecs + 1, left);
3689 lpp = xfs_btree_ptr_addr(cur, lrecs + 1, left);
3690 rkp = xfs_btree_key_addr(cur, 1, right);
3691 rpp = xfs_btree_ptr_addr(cur, 1, right);
3693 for (i = 1; i < rrecs; i++) {
3694 error = xfs_btree_check_ptr(cur, rpp, i, level);
3699 xfs_btree_copy_keys(cur, lkp, rkp, rrecs);
3700 xfs_btree_copy_ptrs(cur, lpp, rpp, rrecs);
3702 xfs_btree_log_keys(cur, lbp, lrecs + 1, lrecs + rrecs);
3703 xfs_btree_log_ptrs(cur, lbp, lrecs + 1, lrecs + rrecs);
3705 /* It's a leaf. Move records. */
3706 union xfs_btree_rec *lrp; /* left record pointer */
3707 union xfs_btree_rec *rrp; /* right record pointer */
3709 lrp = xfs_btree_rec_addr(cur, lrecs + 1, left);
3710 rrp = xfs_btree_rec_addr(cur, 1, right);
3712 xfs_btree_copy_recs(cur, lrp, rrp, rrecs);
3713 xfs_btree_log_recs(cur, lbp, lrecs + 1, lrecs + rrecs);
3716 XFS_BTREE_STATS_INC(cur, join);
3719 * Fix up the number of records and right block pointer in the
3720 * surviving block, and log it.
3722 xfs_btree_set_numrecs(left, lrecs + rrecs);
3723 xfs_btree_get_sibling(cur, right, &cptr, XFS_BB_RIGHTSIB),
3724 xfs_btree_set_sibling(cur, left, &cptr, XFS_BB_RIGHTSIB);
3725 xfs_btree_log_block(cur, lbp, XFS_BB_NUMRECS | XFS_BB_RIGHTSIB);
3727 /* If there is a right sibling, point it to the remaining block. */
3728 xfs_btree_get_sibling(cur, left, &cptr, XFS_BB_RIGHTSIB);
3729 if (!xfs_btree_ptr_is_null(cur, &cptr)) {
3730 error = xfs_btree_read_buf_block(cur, &cptr, level,
3731 0, &rrblock, &rrbp);
3734 xfs_btree_set_sibling(cur, rrblock, &lptr, XFS_BB_LEFTSIB);
3735 xfs_btree_log_block(cur, rrbp, XFS_BB_LEFTSIB);
3738 /* Free the deleted block. */
3739 error = cur->bc_ops->free_block(cur, rbp);
3742 XFS_BTREE_STATS_INC(cur, free);
3745 * If we joined with the left neighbor, set the buffer in the
3746 * cursor to the left block, and fix up the index.
3749 cur->bc_bufs[level] = lbp;
3750 cur->bc_ptrs[level] += lrecs;
3751 cur->bc_ra[level] = 0;
3754 * If we joined with the right neighbor and there's a level above
3755 * us, increment the cursor at that level.
3757 else if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) ||
3758 (level + 1 < cur->bc_nlevels)) {
3759 error = xfs_btree_increment(cur, level + 1, &i);
3765 * Readjust the ptr at this level if it's not a leaf, since it's
3766 * still pointing at the deletion point, which makes the cursor
3767 * inconsistent. If this makes the ptr 0, the caller fixes it up.
3768 * We can't use decrement because it would change the next level up.
3771 cur->bc_ptrs[level]--;
3773 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3774 /* Return value means the next level up has something to do. */
3779 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
3781 xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
3786 * Delete the record pointed to by cur.
3787 * The cursor refers to the place where the record was (could be inserted)
3788 * when the operation returns.
3792 struct xfs_btree_cur *cur,
3793 int *stat) /* success/failure */
3795 int error; /* error return value */
3799 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
3802 * Go up the tree, starting at leaf level.
3804 * If 2 is returned then a join was done; go to the next level.
3805 * Otherwise we are done.
3807 for (level = 0, i = 2; i == 2; level++) {
3808 error = xfs_btree_delrec(cur, level, &i);
3814 for (level = 1; level < cur->bc_nlevels; level++) {
3815 if (cur->bc_ptrs[level] == 0) {
3816 error = xfs_btree_decrement(cur, level, &i);
3824 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3828 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
3833 * Get the data from the pointed-to record.
3837 struct xfs_btree_cur *cur, /* btree cursor */
3838 union xfs_btree_rec **recp, /* output: btree record */
3839 int *stat) /* output: success/failure */
3841 struct xfs_btree_block *block; /* btree block */
3842 struct xfs_buf *bp; /* buffer pointer */
3843 int ptr; /* record number */
3845 int error; /* error return value */
3848 ptr = cur->bc_ptrs[0];
3849 block = xfs_btree_get_block(cur, 0, &bp);
3852 error = xfs_btree_check_block(cur, block, 0, bp);
3858 * Off the right end or left end, return failure.
3860 if (ptr > xfs_btree_get_numrecs(block) || ptr <= 0) {
3866 * Point to the record and extract its data.
3868 *recp = xfs_btree_rec_addr(cur, ptr, block);