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_shared.h"
21 #include "xfs_format.h"
22 #include "xfs_log_format.h"
23 #include "xfs_trans_resv.h"
27 #include "xfs_mount.h"
28 #include "xfs_inode.h"
29 #include "xfs_trans.h"
30 #include "xfs_inode_item.h"
31 #include "xfs_buf_item.h"
32 #include "xfs_btree.h"
33 #include "xfs_error.h"
34 #include "xfs_trace.h"
35 #include "xfs_cksum.h"
38 * Cursor allocation zone.
40 kmem_zone_t *xfs_btree_cur_zone;
43 * Btree magic numbers.
45 static const __uint32_t xfs_magics[2][XFS_BTNUM_MAX] = {
46 { XFS_ABTB_MAGIC, XFS_ABTC_MAGIC, XFS_BMAP_MAGIC, XFS_IBT_MAGIC,
48 { XFS_ABTB_CRC_MAGIC, XFS_ABTC_CRC_MAGIC,
49 XFS_BMAP_CRC_MAGIC, XFS_IBT_CRC_MAGIC, XFS_FIBT_CRC_MAGIC }
51 #define xfs_btree_magic(cur) \
52 xfs_magics[!!((cur)->bc_flags & XFS_BTREE_CRC_BLOCKS)][cur->bc_btnum]
55 STATIC int /* error (0 or EFSCORRUPTED) */
56 xfs_btree_check_lblock(
57 struct xfs_btree_cur *cur, /* btree cursor */
58 struct xfs_btree_block *block, /* btree long form block pointer */
59 int level, /* level of the btree block */
60 struct xfs_buf *bp) /* buffer for block, if any */
62 int lblock_ok = 1; /* block passes checks */
63 struct xfs_mount *mp; /* file system mount point */
67 if (xfs_sb_version_hascrc(&mp->m_sb)) {
68 lblock_ok = lblock_ok &&
69 uuid_equal(&block->bb_u.l.bb_uuid, &mp->m_sb.sb_uuid) &&
70 block->bb_u.l.bb_blkno == cpu_to_be64(
71 bp ? bp->b_bn : XFS_BUF_DADDR_NULL);
74 lblock_ok = lblock_ok &&
75 be32_to_cpu(block->bb_magic) == xfs_btree_magic(cur) &&
76 be16_to_cpu(block->bb_level) == level &&
77 be16_to_cpu(block->bb_numrecs) <=
78 cur->bc_ops->get_maxrecs(cur, level) &&
79 block->bb_u.l.bb_leftsib &&
80 (block->bb_u.l.bb_leftsib == cpu_to_be64(NULLDFSBNO) ||
81 XFS_FSB_SANITY_CHECK(mp,
82 be64_to_cpu(block->bb_u.l.bb_leftsib))) &&
83 block->bb_u.l.bb_rightsib &&
84 (block->bb_u.l.bb_rightsib == cpu_to_be64(NULLDFSBNO) ||
85 XFS_FSB_SANITY_CHECK(mp,
86 be64_to_cpu(block->bb_u.l.bb_rightsib)));
88 if (unlikely(XFS_TEST_ERROR(!lblock_ok, mp,
89 XFS_ERRTAG_BTREE_CHECK_LBLOCK,
90 XFS_RANDOM_BTREE_CHECK_LBLOCK))) {
92 trace_xfs_btree_corrupt(bp, _RET_IP_);
93 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp);
94 return XFS_ERROR(EFSCORRUPTED);
99 STATIC int /* error (0 or EFSCORRUPTED) */
100 xfs_btree_check_sblock(
101 struct xfs_btree_cur *cur, /* btree cursor */
102 struct xfs_btree_block *block, /* btree short form block pointer */
103 int level, /* level of the btree block */
104 struct xfs_buf *bp) /* buffer containing block */
106 struct xfs_mount *mp; /* file system mount point */
107 struct xfs_buf *agbp; /* buffer for ag. freespace struct */
108 struct xfs_agf *agf; /* ag. freespace structure */
109 xfs_agblock_t agflen; /* native ag. freespace length */
110 int sblock_ok = 1; /* block passes checks */
113 agbp = cur->bc_private.a.agbp;
114 agf = XFS_BUF_TO_AGF(agbp);
115 agflen = be32_to_cpu(agf->agf_length);
117 if (xfs_sb_version_hascrc(&mp->m_sb)) {
118 sblock_ok = sblock_ok &&
119 uuid_equal(&block->bb_u.s.bb_uuid, &mp->m_sb.sb_uuid) &&
120 block->bb_u.s.bb_blkno == cpu_to_be64(
121 bp ? bp->b_bn : XFS_BUF_DADDR_NULL);
124 sblock_ok = sblock_ok &&
125 be32_to_cpu(block->bb_magic) == xfs_btree_magic(cur) &&
126 be16_to_cpu(block->bb_level) == level &&
127 be16_to_cpu(block->bb_numrecs) <=
128 cur->bc_ops->get_maxrecs(cur, level) &&
129 (block->bb_u.s.bb_leftsib == cpu_to_be32(NULLAGBLOCK) ||
130 be32_to_cpu(block->bb_u.s.bb_leftsib) < agflen) &&
131 block->bb_u.s.bb_leftsib &&
132 (block->bb_u.s.bb_rightsib == cpu_to_be32(NULLAGBLOCK) ||
133 be32_to_cpu(block->bb_u.s.bb_rightsib) < agflen) &&
134 block->bb_u.s.bb_rightsib;
136 if (unlikely(XFS_TEST_ERROR(!sblock_ok, mp,
137 XFS_ERRTAG_BTREE_CHECK_SBLOCK,
138 XFS_RANDOM_BTREE_CHECK_SBLOCK))) {
140 trace_xfs_btree_corrupt(bp, _RET_IP_);
141 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp);
142 return XFS_ERROR(EFSCORRUPTED);
148 * Debug routine: check that block header is ok.
151 xfs_btree_check_block(
152 struct xfs_btree_cur *cur, /* btree cursor */
153 struct xfs_btree_block *block, /* generic btree block pointer */
154 int level, /* level of the btree block */
155 struct xfs_buf *bp) /* buffer containing block, if any */
157 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
158 return xfs_btree_check_lblock(cur, block, level, bp);
160 return xfs_btree_check_sblock(cur, block, level, bp);
164 * Check that (long) pointer is ok.
166 int /* error (0 or EFSCORRUPTED) */
167 xfs_btree_check_lptr(
168 struct xfs_btree_cur *cur, /* btree cursor */
169 xfs_dfsbno_t bno, /* btree block disk address */
170 int level) /* btree block level */
172 XFS_WANT_CORRUPTED_RETURN(
175 XFS_FSB_SANITY_CHECK(cur->bc_mp, bno));
181 * Check that (short) pointer is ok.
183 STATIC int /* error (0 or EFSCORRUPTED) */
184 xfs_btree_check_sptr(
185 struct xfs_btree_cur *cur, /* btree cursor */
186 xfs_agblock_t bno, /* btree block disk address */
187 int level) /* btree block level */
189 xfs_agblock_t agblocks = cur->bc_mp->m_sb.sb_agblocks;
191 XFS_WANT_CORRUPTED_RETURN(
193 bno != NULLAGBLOCK &&
200 * Check that block ptr is ok.
202 STATIC int /* error (0 or EFSCORRUPTED) */
204 struct xfs_btree_cur *cur, /* btree cursor */
205 union xfs_btree_ptr *ptr, /* btree block disk address */
206 int index, /* offset from ptr to check */
207 int level) /* btree block level */
209 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
210 return xfs_btree_check_lptr(cur,
211 be64_to_cpu((&ptr->l)[index]), level);
213 return xfs_btree_check_sptr(cur,
214 be32_to_cpu((&ptr->s)[index]), level);
220 * Calculate CRC on the whole btree block and stuff it into the
221 * long-form btree header.
223 * Prior to calculting the CRC, pull the LSN out of the buffer log item and put
224 * it into the buffer so recovery knows what the last modifcation was that made
228 xfs_btree_lblock_calc_crc(
231 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
232 struct xfs_buf_log_item *bip = bp->b_fspriv;
234 if (!xfs_sb_version_hascrc(&bp->b_target->bt_mount->m_sb))
237 block->bb_u.l.bb_lsn = cpu_to_be64(bip->bli_item.li_lsn);
238 xfs_buf_update_cksum(bp, XFS_BTREE_LBLOCK_CRC_OFF);
242 xfs_btree_lblock_verify_crc(
245 if (xfs_sb_version_hascrc(&bp->b_target->bt_mount->m_sb))
246 return xfs_buf_verify_cksum(bp, XFS_BTREE_LBLOCK_CRC_OFF);
252 * Calculate CRC on the whole btree block and stuff it into the
253 * short-form btree header.
255 * Prior to calculting the CRC, pull the LSN out of the buffer log item and put
256 * it into the buffer so recovery knows what the last modifcation was that made
260 xfs_btree_sblock_calc_crc(
263 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
264 struct xfs_buf_log_item *bip = bp->b_fspriv;
266 if (!xfs_sb_version_hascrc(&bp->b_target->bt_mount->m_sb))
269 block->bb_u.s.bb_lsn = cpu_to_be64(bip->bli_item.li_lsn);
270 xfs_buf_update_cksum(bp, XFS_BTREE_SBLOCK_CRC_OFF);
274 xfs_btree_sblock_verify_crc(
277 if (xfs_sb_version_hascrc(&bp->b_target->bt_mount->m_sb))
278 return xfs_buf_verify_cksum(bp, XFS_BTREE_SBLOCK_CRC_OFF);
284 * Delete the btree cursor.
287 xfs_btree_del_cursor(
288 xfs_btree_cur_t *cur, /* btree cursor */
289 int error) /* del because of error */
291 int i; /* btree level */
294 * Clear the buffer pointers, and release the buffers.
295 * If we're doing this in the face of an error, we
296 * need to make sure to inspect all of the entries
297 * in the bc_bufs array for buffers to be unlocked.
298 * This is because some of the btree code works from
299 * level n down to 0, and if we get an error along
300 * the way we won't have initialized all the entries
303 for (i = 0; i < cur->bc_nlevels; i++) {
305 xfs_trans_brelse(cur->bc_tp, cur->bc_bufs[i]);
310 * Can't free a bmap cursor without having dealt with the
311 * allocated indirect blocks' accounting.
313 ASSERT(cur->bc_btnum != XFS_BTNUM_BMAP ||
314 cur->bc_private.b.allocated == 0);
318 kmem_zone_free(xfs_btree_cur_zone, cur);
322 * Duplicate the btree cursor.
323 * Allocate a new one, copy the record, re-get the buffers.
326 xfs_btree_dup_cursor(
327 xfs_btree_cur_t *cur, /* input cursor */
328 xfs_btree_cur_t **ncur) /* output cursor */
330 xfs_buf_t *bp; /* btree block's buffer pointer */
331 int error; /* error return value */
332 int i; /* level number of btree block */
333 xfs_mount_t *mp; /* mount structure for filesystem */
334 xfs_btree_cur_t *new; /* new cursor value */
335 xfs_trans_t *tp; /* transaction pointer, can be NULL */
341 * Allocate a new cursor like the old one.
343 new = cur->bc_ops->dup_cursor(cur);
346 * Copy the record currently in the cursor.
348 new->bc_rec = cur->bc_rec;
351 * For each level current, re-get the buffer and copy the ptr value.
353 for (i = 0; i < new->bc_nlevels; i++) {
354 new->bc_ptrs[i] = cur->bc_ptrs[i];
355 new->bc_ra[i] = cur->bc_ra[i];
356 bp = cur->bc_bufs[i];
358 error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp,
359 XFS_BUF_ADDR(bp), mp->m_bsize,
361 cur->bc_ops->buf_ops);
363 xfs_btree_del_cursor(new, error);
368 new->bc_bufs[i] = bp;
375 * XFS btree block layout and addressing:
377 * There are two types of blocks in the btree: leaf and non-leaf blocks.
379 * The leaf record start with a header then followed by records containing
380 * the values. A non-leaf block also starts with the same header, and
381 * then first contains lookup keys followed by an equal number of pointers
382 * to the btree blocks at the previous level.
384 * +--------+-------+-------+-------+-------+-------+-------+
385 * Leaf: | header | rec 1 | rec 2 | rec 3 | rec 4 | rec 5 | rec N |
386 * +--------+-------+-------+-------+-------+-------+-------+
388 * +--------+-------+-------+-------+-------+-------+-------+
389 * Non-Leaf: | header | key 1 | key 2 | key N | ptr 1 | ptr 2 | ptr N |
390 * +--------+-------+-------+-------+-------+-------+-------+
392 * The header is called struct xfs_btree_block for reasons better left unknown
393 * and comes in different versions for short (32bit) and long (64bit) block
394 * pointers. The record and key structures are defined by the btree instances
395 * and opaque to the btree core. The block pointers are simple disk endian
396 * integers, available in a short (32bit) and long (64bit) variant.
398 * The helpers below calculate the offset of a given record, key or pointer
399 * into a btree block (xfs_btree_*_offset) or return a pointer to the given
400 * record, key or pointer (xfs_btree_*_addr). Note that all addressing
401 * inside the btree block is done using indices starting at one, not zero!
405 * Return size of the btree block header for this btree instance.
407 static inline size_t xfs_btree_block_len(struct xfs_btree_cur *cur)
409 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
410 if (cur->bc_flags & XFS_BTREE_CRC_BLOCKS)
411 return XFS_BTREE_LBLOCK_CRC_LEN;
412 return XFS_BTREE_LBLOCK_LEN;
414 if (cur->bc_flags & XFS_BTREE_CRC_BLOCKS)
415 return XFS_BTREE_SBLOCK_CRC_LEN;
416 return XFS_BTREE_SBLOCK_LEN;
420 * Return size of btree block pointers for this btree instance.
422 static inline size_t xfs_btree_ptr_len(struct xfs_btree_cur *cur)
424 return (cur->bc_flags & XFS_BTREE_LONG_PTRS) ?
425 sizeof(__be64) : sizeof(__be32);
429 * Calculate offset of the n-th record in a btree block.
432 xfs_btree_rec_offset(
433 struct xfs_btree_cur *cur,
436 return xfs_btree_block_len(cur) +
437 (n - 1) * cur->bc_ops->rec_len;
441 * Calculate offset of the n-th key in a btree block.
444 xfs_btree_key_offset(
445 struct xfs_btree_cur *cur,
448 return xfs_btree_block_len(cur) +
449 (n - 1) * cur->bc_ops->key_len;
453 * Calculate offset of the n-th block pointer in a btree block.
456 xfs_btree_ptr_offset(
457 struct xfs_btree_cur *cur,
461 return xfs_btree_block_len(cur) +
462 cur->bc_ops->get_maxrecs(cur, level) * cur->bc_ops->key_len +
463 (n - 1) * xfs_btree_ptr_len(cur);
467 * Return a pointer to the n-th record in the btree block.
469 STATIC union xfs_btree_rec *
471 struct xfs_btree_cur *cur,
473 struct xfs_btree_block *block)
475 return (union xfs_btree_rec *)
476 ((char *)block + xfs_btree_rec_offset(cur, n));
480 * Return a pointer to the n-th key in the btree block.
482 STATIC union xfs_btree_key *
484 struct xfs_btree_cur *cur,
486 struct xfs_btree_block *block)
488 return (union xfs_btree_key *)
489 ((char *)block + xfs_btree_key_offset(cur, n));
493 * Return a pointer to the n-th block pointer in the btree block.
495 STATIC union xfs_btree_ptr *
497 struct xfs_btree_cur *cur,
499 struct xfs_btree_block *block)
501 int level = xfs_btree_get_level(block);
503 ASSERT(block->bb_level != 0);
505 return (union xfs_btree_ptr *)
506 ((char *)block + xfs_btree_ptr_offset(cur, n, level));
510 * Get the root block which is stored in the inode.
512 * For now this btree implementation assumes the btree root is always
513 * stored in the if_broot field of an inode fork.
515 STATIC struct xfs_btree_block *
517 struct xfs_btree_cur *cur)
519 struct xfs_ifork *ifp;
521 ifp = XFS_IFORK_PTR(cur->bc_private.b.ip, cur->bc_private.b.whichfork);
522 return (struct xfs_btree_block *)ifp->if_broot;
526 * Retrieve the block pointer from the cursor at the given level.
527 * This may be an inode btree root or from a buffer.
529 STATIC struct xfs_btree_block * /* generic btree block pointer */
531 struct xfs_btree_cur *cur, /* btree cursor */
532 int level, /* level in btree */
533 struct xfs_buf **bpp) /* buffer containing the block */
535 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
536 (level == cur->bc_nlevels - 1)) {
538 return xfs_btree_get_iroot(cur);
541 *bpp = cur->bc_bufs[level];
542 return XFS_BUF_TO_BLOCK(*bpp);
546 * Get a buffer for the block, return it with no data read.
547 * Long-form addressing.
549 xfs_buf_t * /* buffer for fsbno */
551 xfs_mount_t *mp, /* file system mount point */
552 xfs_trans_t *tp, /* transaction pointer */
553 xfs_fsblock_t fsbno, /* file system block number */
554 uint lock) /* lock flags for get_buf */
556 xfs_daddr_t d; /* real disk block address */
558 ASSERT(fsbno != NULLFSBLOCK);
559 d = XFS_FSB_TO_DADDR(mp, fsbno);
560 return xfs_trans_get_buf(tp, mp->m_ddev_targp, d, mp->m_bsize, lock);
564 * Get a buffer for the block, return it with no data read.
565 * Short-form addressing.
567 xfs_buf_t * /* buffer for agno/agbno */
569 xfs_mount_t *mp, /* file system mount point */
570 xfs_trans_t *tp, /* transaction pointer */
571 xfs_agnumber_t agno, /* allocation group number */
572 xfs_agblock_t agbno, /* allocation group block number */
573 uint lock) /* lock flags for get_buf */
575 xfs_daddr_t d; /* real disk block address */
577 ASSERT(agno != NULLAGNUMBER);
578 ASSERT(agbno != NULLAGBLOCK);
579 d = XFS_AGB_TO_DADDR(mp, agno, agbno);
580 return xfs_trans_get_buf(tp, mp->m_ddev_targp, d, mp->m_bsize, lock);
584 * Check for the cursor referring to the last block at the given level.
586 int /* 1=is last block, 0=not last block */
587 xfs_btree_islastblock(
588 xfs_btree_cur_t *cur, /* btree cursor */
589 int level) /* level to check */
591 struct xfs_btree_block *block; /* generic btree block pointer */
592 xfs_buf_t *bp; /* buffer containing block */
594 block = xfs_btree_get_block(cur, level, &bp);
595 xfs_btree_check_block(cur, block, level, bp);
596 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
597 return block->bb_u.l.bb_rightsib == cpu_to_be64(NULLDFSBNO);
599 return block->bb_u.s.bb_rightsib == cpu_to_be32(NULLAGBLOCK);
603 * Change the cursor to point to the first record at the given level.
604 * Other levels are unaffected.
606 STATIC int /* success=1, failure=0 */
608 xfs_btree_cur_t *cur, /* btree cursor */
609 int level) /* level to change */
611 struct xfs_btree_block *block; /* generic btree block pointer */
612 xfs_buf_t *bp; /* buffer containing block */
615 * Get the block pointer for this level.
617 block = xfs_btree_get_block(cur, level, &bp);
618 xfs_btree_check_block(cur, block, level, bp);
620 * It's empty, there is no such record.
622 if (!block->bb_numrecs)
625 * Set the ptr value to 1, that's the first record/key.
627 cur->bc_ptrs[level] = 1;
632 * Change the cursor to point to the last record in the current block
633 * at the given level. Other levels are unaffected.
635 STATIC int /* success=1, failure=0 */
637 xfs_btree_cur_t *cur, /* btree cursor */
638 int level) /* level to change */
640 struct xfs_btree_block *block; /* generic btree block pointer */
641 xfs_buf_t *bp; /* buffer containing block */
644 * Get the block pointer for this level.
646 block = xfs_btree_get_block(cur, level, &bp);
647 xfs_btree_check_block(cur, block, level, bp);
649 * It's empty, there is no such record.
651 if (!block->bb_numrecs)
654 * Set the ptr value to numrecs, that's the last record/key.
656 cur->bc_ptrs[level] = be16_to_cpu(block->bb_numrecs);
661 * Compute first and last byte offsets for the fields given.
662 * Interprets the offsets table, which contains struct field offsets.
666 __int64_t fields, /* bitmask of fields */
667 const short *offsets, /* table of field offsets */
668 int nbits, /* number of bits to inspect */
669 int *first, /* output: first byte offset */
670 int *last) /* output: last byte offset */
672 int i; /* current bit number */
673 __int64_t imask; /* mask for current bit number */
677 * Find the lowest bit, so the first byte offset.
679 for (i = 0, imask = 1LL; ; i++, imask <<= 1) {
680 if (imask & fields) {
686 * Find the highest bit, so the last byte offset.
688 for (i = nbits - 1, imask = 1LL << i; ; i--, imask >>= 1) {
689 if (imask & fields) {
690 *last = offsets[i + 1] - 1;
697 * Get a buffer for the block, return it read in.
698 * Long-form addressing.
702 struct xfs_mount *mp, /* file system mount point */
703 struct xfs_trans *tp, /* transaction pointer */
704 xfs_fsblock_t fsbno, /* file system block number */
705 uint lock, /* lock flags for read_buf */
706 struct xfs_buf **bpp, /* buffer for fsbno */
707 int refval, /* ref count value for buffer */
708 const struct xfs_buf_ops *ops)
710 struct xfs_buf *bp; /* return value */
711 xfs_daddr_t d; /* real disk block address */
714 ASSERT(fsbno != NULLFSBLOCK);
715 d = XFS_FSB_TO_DADDR(mp, fsbno);
716 error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, d,
717 mp->m_bsize, lock, &bp, ops);
721 xfs_buf_set_ref(bp, refval);
727 * Read-ahead the block, don't wait for it, don't return a buffer.
728 * Long-form addressing.
732 xfs_btree_reada_bufl(
733 struct xfs_mount *mp, /* file system mount point */
734 xfs_fsblock_t fsbno, /* file system block number */
735 xfs_extlen_t count, /* count of filesystem blocks */
736 const struct xfs_buf_ops *ops)
740 ASSERT(fsbno != NULLFSBLOCK);
741 d = XFS_FSB_TO_DADDR(mp, fsbno);
742 xfs_buf_readahead(mp->m_ddev_targp, d, mp->m_bsize * count, ops);
746 * Read-ahead the block, don't wait for it, don't return a buffer.
747 * Short-form addressing.
751 xfs_btree_reada_bufs(
752 struct xfs_mount *mp, /* file system mount point */
753 xfs_agnumber_t agno, /* allocation group number */
754 xfs_agblock_t agbno, /* allocation group block number */
755 xfs_extlen_t count, /* count of filesystem blocks */
756 const struct xfs_buf_ops *ops)
760 ASSERT(agno != NULLAGNUMBER);
761 ASSERT(agbno != NULLAGBLOCK);
762 d = XFS_AGB_TO_DADDR(mp, agno, agbno);
763 xfs_buf_readahead(mp->m_ddev_targp, d, mp->m_bsize * count, ops);
767 xfs_btree_readahead_lblock(
768 struct xfs_btree_cur *cur,
770 struct xfs_btree_block *block)
773 xfs_dfsbno_t left = be64_to_cpu(block->bb_u.l.bb_leftsib);
774 xfs_dfsbno_t right = be64_to_cpu(block->bb_u.l.bb_rightsib);
776 if ((lr & XFS_BTCUR_LEFTRA) && left != NULLDFSBNO) {
777 xfs_btree_reada_bufl(cur->bc_mp, left, 1,
778 cur->bc_ops->buf_ops);
782 if ((lr & XFS_BTCUR_RIGHTRA) && right != NULLDFSBNO) {
783 xfs_btree_reada_bufl(cur->bc_mp, right, 1,
784 cur->bc_ops->buf_ops);
792 xfs_btree_readahead_sblock(
793 struct xfs_btree_cur *cur,
795 struct xfs_btree_block *block)
798 xfs_agblock_t left = be32_to_cpu(block->bb_u.s.bb_leftsib);
799 xfs_agblock_t right = be32_to_cpu(block->bb_u.s.bb_rightsib);
802 if ((lr & XFS_BTCUR_LEFTRA) && left != NULLAGBLOCK) {
803 xfs_btree_reada_bufs(cur->bc_mp, cur->bc_private.a.agno,
804 left, 1, cur->bc_ops->buf_ops);
808 if ((lr & XFS_BTCUR_RIGHTRA) && right != NULLAGBLOCK) {
809 xfs_btree_reada_bufs(cur->bc_mp, cur->bc_private.a.agno,
810 right, 1, cur->bc_ops->buf_ops);
818 * Read-ahead btree blocks, at the given level.
819 * Bits in lr are set from XFS_BTCUR_{LEFT,RIGHT}RA.
823 struct xfs_btree_cur *cur, /* btree cursor */
824 int lev, /* level in btree */
825 int lr) /* left/right bits */
827 struct xfs_btree_block *block;
830 * No readahead needed if we are at the root level and the
831 * btree root is stored in the inode.
833 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
834 (lev == cur->bc_nlevels - 1))
837 if ((cur->bc_ra[lev] | lr) == cur->bc_ra[lev])
840 cur->bc_ra[lev] |= lr;
841 block = XFS_BUF_TO_BLOCK(cur->bc_bufs[lev]);
843 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
844 return xfs_btree_readahead_lblock(cur, lr, block);
845 return xfs_btree_readahead_sblock(cur, lr, block);
849 xfs_btree_ptr_to_daddr(
850 struct xfs_btree_cur *cur,
851 union xfs_btree_ptr *ptr)
853 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
854 ASSERT(ptr->l != cpu_to_be64(NULLDFSBNO));
856 return XFS_FSB_TO_DADDR(cur->bc_mp, be64_to_cpu(ptr->l));
858 ASSERT(cur->bc_private.a.agno != NULLAGNUMBER);
859 ASSERT(ptr->s != cpu_to_be32(NULLAGBLOCK));
861 return XFS_AGB_TO_DADDR(cur->bc_mp, cur->bc_private.a.agno,
862 be32_to_cpu(ptr->s));
867 * Readahead @count btree blocks at the given @ptr location.
869 * We don't need to care about long or short form btrees here as we have a
870 * method of converting the ptr directly to a daddr available to us.
873 xfs_btree_readahead_ptr(
874 struct xfs_btree_cur *cur,
875 union xfs_btree_ptr *ptr,
878 xfs_buf_readahead(cur->bc_mp->m_ddev_targp,
879 xfs_btree_ptr_to_daddr(cur, ptr),
880 cur->bc_mp->m_bsize * count, cur->bc_ops->buf_ops);
884 * Set the buffer for level "lev" in the cursor to bp, releasing
885 * any previous buffer.
889 xfs_btree_cur_t *cur, /* btree cursor */
890 int lev, /* level in btree */
891 xfs_buf_t *bp) /* new buffer to set */
893 struct xfs_btree_block *b; /* btree block */
895 if (cur->bc_bufs[lev])
896 xfs_trans_brelse(cur->bc_tp, cur->bc_bufs[lev]);
897 cur->bc_bufs[lev] = bp;
900 b = XFS_BUF_TO_BLOCK(bp);
901 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
902 if (b->bb_u.l.bb_leftsib == cpu_to_be64(NULLDFSBNO))
903 cur->bc_ra[lev] |= XFS_BTCUR_LEFTRA;
904 if (b->bb_u.l.bb_rightsib == cpu_to_be64(NULLDFSBNO))
905 cur->bc_ra[lev] |= XFS_BTCUR_RIGHTRA;
907 if (b->bb_u.s.bb_leftsib == cpu_to_be32(NULLAGBLOCK))
908 cur->bc_ra[lev] |= XFS_BTCUR_LEFTRA;
909 if (b->bb_u.s.bb_rightsib == cpu_to_be32(NULLAGBLOCK))
910 cur->bc_ra[lev] |= XFS_BTCUR_RIGHTRA;
915 xfs_btree_ptr_is_null(
916 struct xfs_btree_cur *cur,
917 union xfs_btree_ptr *ptr)
919 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
920 return ptr->l == cpu_to_be64(NULLDFSBNO);
922 return ptr->s == cpu_to_be32(NULLAGBLOCK);
926 xfs_btree_set_ptr_null(
927 struct xfs_btree_cur *cur,
928 union xfs_btree_ptr *ptr)
930 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
931 ptr->l = cpu_to_be64(NULLDFSBNO);
933 ptr->s = cpu_to_be32(NULLAGBLOCK);
937 * Get/set/init sibling pointers
940 xfs_btree_get_sibling(
941 struct xfs_btree_cur *cur,
942 struct xfs_btree_block *block,
943 union xfs_btree_ptr *ptr,
946 ASSERT(lr == XFS_BB_LEFTSIB || lr == XFS_BB_RIGHTSIB);
948 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
949 if (lr == XFS_BB_RIGHTSIB)
950 ptr->l = block->bb_u.l.bb_rightsib;
952 ptr->l = block->bb_u.l.bb_leftsib;
954 if (lr == XFS_BB_RIGHTSIB)
955 ptr->s = block->bb_u.s.bb_rightsib;
957 ptr->s = block->bb_u.s.bb_leftsib;
962 xfs_btree_set_sibling(
963 struct xfs_btree_cur *cur,
964 struct xfs_btree_block *block,
965 union xfs_btree_ptr *ptr,
968 ASSERT(lr == XFS_BB_LEFTSIB || lr == XFS_BB_RIGHTSIB);
970 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
971 if (lr == XFS_BB_RIGHTSIB)
972 block->bb_u.l.bb_rightsib = ptr->l;
974 block->bb_u.l.bb_leftsib = ptr->l;
976 if (lr == XFS_BB_RIGHTSIB)
977 block->bb_u.s.bb_rightsib = ptr->s;
979 block->bb_u.s.bb_leftsib = ptr->s;
984 xfs_btree_init_block_int(
985 struct xfs_mount *mp,
986 struct xfs_btree_block *buf,
994 buf->bb_magic = cpu_to_be32(magic);
995 buf->bb_level = cpu_to_be16(level);
996 buf->bb_numrecs = cpu_to_be16(numrecs);
998 if (flags & XFS_BTREE_LONG_PTRS) {
999 buf->bb_u.l.bb_leftsib = cpu_to_be64(NULLDFSBNO);
1000 buf->bb_u.l.bb_rightsib = cpu_to_be64(NULLDFSBNO);
1001 if (flags & XFS_BTREE_CRC_BLOCKS) {
1002 buf->bb_u.l.bb_blkno = cpu_to_be64(blkno);
1003 buf->bb_u.l.bb_owner = cpu_to_be64(owner);
1004 uuid_copy(&buf->bb_u.l.bb_uuid, &mp->m_sb.sb_uuid);
1005 buf->bb_u.l.bb_pad = 0;
1006 buf->bb_u.l.bb_lsn = 0;
1009 /* owner is a 32 bit value on short blocks */
1010 __u32 __owner = (__u32)owner;
1012 buf->bb_u.s.bb_leftsib = cpu_to_be32(NULLAGBLOCK);
1013 buf->bb_u.s.bb_rightsib = cpu_to_be32(NULLAGBLOCK);
1014 if (flags & XFS_BTREE_CRC_BLOCKS) {
1015 buf->bb_u.s.bb_blkno = cpu_to_be64(blkno);
1016 buf->bb_u.s.bb_owner = cpu_to_be32(__owner);
1017 uuid_copy(&buf->bb_u.s.bb_uuid, &mp->m_sb.sb_uuid);
1018 buf->bb_u.s.bb_lsn = 0;
1024 xfs_btree_init_block(
1025 struct xfs_mount *mp,
1033 xfs_btree_init_block_int(mp, XFS_BUF_TO_BLOCK(bp), bp->b_bn,
1034 magic, level, numrecs, owner, flags);
1038 xfs_btree_init_block_cur(
1039 struct xfs_btree_cur *cur,
1047 * we can pull the owner from the cursor right now as the different
1048 * owners align directly with the pointer size of the btree. This may
1049 * change in future, but is safe for current users of the generic btree
1052 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
1053 owner = cur->bc_private.b.ip->i_ino;
1055 owner = cur->bc_private.a.agno;
1057 xfs_btree_init_block_int(cur->bc_mp, XFS_BUF_TO_BLOCK(bp), bp->b_bn,
1058 xfs_btree_magic(cur), level, numrecs,
1059 owner, cur->bc_flags);
1063 * Return true if ptr is the last record in the btree and
1064 * we need to track updates to this record. The decision
1065 * will be further refined in the update_lastrec method.
1068 xfs_btree_is_lastrec(
1069 struct xfs_btree_cur *cur,
1070 struct xfs_btree_block *block,
1073 union xfs_btree_ptr ptr;
1077 if (!(cur->bc_flags & XFS_BTREE_LASTREC_UPDATE))
1080 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_RIGHTSIB);
1081 if (!xfs_btree_ptr_is_null(cur, &ptr))
1087 xfs_btree_buf_to_ptr(
1088 struct xfs_btree_cur *cur,
1090 union xfs_btree_ptr *ptr)
1092 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
1093 ptr->l = cpu_to_be64(XFS_DADDR_TO_FSB(cur->bc_mp,
1096 ptr->s = cpu_to_be32(xfs_daddr_to_agbno(cur->bc_mp,
1103 struct xfs_btree_cur *cur,
1106 switch (cur->bc_btnum) {
1109 xfs_buf_set_ref(bp, XFS_ALLOC_BTREE_REF);
1112 case XFS_BTNUM_FINO:
1113 xfs_buf_set_ref(bp, XFS_INO_BTREE_REF);
1115 case XFS_BTNUM_BMAP:
1116 xfs_buf_set_ref(bp, XFS_BMAP_BTREE_REF);
1124 xfs_btree_get_buf_block(
1125 struct xfs_btree_cur *cur,
1126 union xfs_btree_ptr *ptr,
1128 struct xfs_btree_block **block,
1129 struct xfs_buf **bpp)
1131 struct xfs_mount *mp = cur->bc_mp;
1134 /* need to sort out how callers deal with failures first */
1135 ASSERT(!(flags & XBF_TRYLOCK));
1137 d = xfs_btree_ptr_to_daddr(cur, ptr);
1138 *bpp = xfs_trans_get_buf(cur->bc_tp, mp->m_ddev_targp, d,
1139 mp->m_bsize, flags);
1144 (*bpp)->b_ops = cur->bc_ops->buf_ops;
1145 *block = XFS_BUF_TO_BLOCK(*bpp);
1150 * Read in the buffer at the given ptr and return the buffer and
1151 * the block pointer within the buffer.
1154 xfs_btree_read_buf_block(
1155 struct xfs_btree_cur *cur,
1156 union xfs_btree_ptr *ptr,
1158 struct xfs_btree_block **block,
1159 struct xfs_buf **bpp)
1161 struct xfs_mount *mp = cur->bc_mp;
1165 /* need to sort out how callers deal with failures first */
1166 ASSERT(!(flags & XBF_TRYLOCK));
1168 d = xfs_btree_ptr_to_daddr(cur, ptr);
1169 error = xfs_trans_read_buf(mp, cur->bc_tp, mp->m_ddev_targp, d,
1170 mp->m_bsize, flags, bpp,
1171 cur->bc_ops->buf_ops);
1175 xfs_btree_set_refs(cur, *bpp);
1176 *block = XFS_BUF_TO_BLOCK(*bpp);
1181 * Copy keys from one btree block to another.
1184 xfs_btree_copy_keys(
1185 struct xfs_btree_cur *cur,
1186 union xfs_btree_key *dst_key,
1187 union xfs_btree_key *src_key,
1190 ASSERT(numkeys >= 0);
1191 memcpy(dst_key, src_key, numkeys * cur->bc_ops->key_len);
1195 * Copy records from one btree block to another.
1198 xfs_btree_copy_recs(
1199 struct xfs_btree_cur *cur,
1200 union xfs_btree_rec *dst_rec,
1201 union xfs_btree_rec *src_rec,
1204 ASSERT(numrecs >= 0);
1205 memcpy(dst_rec, src_rec, numrecs * cur->bc_ops->rec_len);
1209 * Copy block pointers from one btree block to another.
1212 xfs_btree_copy_ptrs(
1213 struct xfs_btree_cur *cur,
1214 union xfs_btree_ptr *dst_ptr,
1215 union xfs_btree_ptr *src_ptr,
1218 ASSERT(numptrs >= 0);
1219 memcpy(dst_ptr, src_ptr, numptrs * xfs_btree_ptr_len(cur));
1223 * Shift keys one index left/right inside a single btree block.
1226 xfs_btree_shift_keys(
1227 struct xfs_btree_cur *cur,
1228 union xfs_btree_key *key,
1234 ASSERT(numkeys >= 0);
1235 ASSERT(dir == 1 || dir == -1);
1237 dst_key = (char *)key + (dir * cur->bc_ops->key_len);
1238 memmove(dst_key, key, numkeys * cur->bc_ops->key_len);
1242 * Shift records one index left/right inside a single btree block.
1245 xfs_btree_shift_recs(
1246 struct xfs_btree_cur *cur,
1247 union xfs_btree_rec *rec,
1253 ASSERT(numrecs >= 0);
1254 ASSERT(dir == 1 || dir == -1);
1256 dst_rec = (char *)rec + (dir * cur->bc_ops->rec_len);
1257 memmove(dst_rec, rec, numrecs * cur->bc_ops->rec_len);
1261 * Shift block pointers one index left/right inside a single btree block.
1264 xfs_btree_shift_ptrs(
1265 struct xfs_btree_cur *cur,
1266 union xfs_btree_ptr *ptr,
1272 ASSERT(numptrs >= 0);
1273 ASSERT(dir == 1 || dir == -1);
1275 dst_ptr = (char *)ptr + (dir * xfs_btree_ptr_len(cur));
1276 memmove(dst_ptr, ptr, numptrs * xfs_btree_ptr_len(cur));
1280 * Log key values from the btree block.
1284 struct xfs_btree_cur *cur,
1289 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1290 XFS_BTREE_TRACE_ARGBII(cur, bp, first, last);
1293 xfs_trans_buf_set_type(cur->bc_tp, bp, XFS_BLFT_BTREE_BUF);
1294 xfs_trans_log_buf(cur->bc_tp, bp,
1295 xfs_btree_key_offset(cur, first),
1296 xfs_btree_key_offset(cur, last + 1) - 1);
1298 xfs_trans_log_inode(cur->bc_tp, cur->bc_private.b.ip,
1299 xfs_ilog_fbroot(cur->bc_private.b.whichfork));
1302 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1306 * Log record values from the btree block.
1310 struct xfs_btree_cur *cur,
1315 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1316 XFS_BTREE_TRACE_ARGBII(cur, bp, first, last);
1318 xfs_trans_buf_set_type(cur->bc_tp, bp, XFS_BLFT_BTREE_BUF);
1319 xfs_trans_log_buf(cur->bc_tp, bp,
1320 xfs_btree_rec_offset(cur, first),
1321 xfs_btree_rec_offset(cur, last + 1) - 1);
1323 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1327 * Log block pointer fields from a btree block (nonleaf).
1331 struct xfs_btree_cur *cur, /* btree cursor */
1332 struct xfs_buf *bp, /* buffer containing btree block */
1333 int first, /* index of first pointer to log */
1334 int last) /* index of last pointer to log */
1336 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1337 XFS_BTREE_TRACE_ARGBII(cur, bp, first, last);
1340 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
1341 int level = xfs_btree_get_level(block);
1343 xfs_trans_buf_set_type(cur->bc_tp, bp, XFS_BLFT_BTREE_BUF);
1344 xfs_trans_log_buf(cur->bc_tp, bp,
1345 xfs_btree_ptr_offset(cur, first, level),
1346 xfs_btree_ptr_offset(cur, last + 1, level) - 1);
1348 xfs_trans_log_inode(cur->bc_tp, cur->bc_private.b.ip,
1349 xfs_ilog_fbroot(cur->bc_private.b.whichfork));
1352 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1356 * Log fields from a btree block header.
1359 xfs_btree_log_block(
1360 struct xfs_btree_cur *cur, /* btree cursor */
1361 struct xfs_buf *bp, /* buffer containing btree block */
1362 int fields) /* mask of fields: XFS_BB_... */
1364 int first; /* first byte offset logged */
1365 int last; /* last byte offset logged */
1366 static const short soffsets[] = { /* table of offsets (short) */
1367 offsetof(struct xfs_btree_block, bb_magic),
1368 offsetof(struct xfs_btree_block, bb_level),
1369 offsetof(struct xfs_btree_block, bb_numrecs),
1370 offsetof(struct xfs_btree_block, bb_u.s.bb_leftsib),
1371 offsetof(struct xfs_btree_block, bb_u.s.bb_rightsib),
1372 offsetof(struct xfs_btree_block, bb_u.s.bb_blkno),
1373 offsetof(struct xfs_btree_block, bb_u.s.bb_lsn),
1374 offsetof(struct xfs_btree_block, bb_u.s.bb_uuid),
1375 offsetof(struct xfs_btree_block, bb_u.s.bb_owner),
1376 offsetof(struct xfs_btree_block, bb_u.s.bb_crc),
1377 XFS_BTREE_SBLOCK_CRC_LEN
1379 static const short loffsets[] = { /* table of offsets (long) */
1380 offsetof(struct xfs_btree_block, bb_magic),
1381 offsetof(struct xfs_btree_block, bb_level),
1382 offsetof(struct xfs_btree_block, bb_numrecs),
1383 offsetof(struct xfs_btree_block, bb_u.l.bb_leftsib),
1384 offsetof(struct xfs_btree_block, bb_u.l.bb_rightsib),
1385 offsetof(struct xfs_btree_block, bb_u.l.bb_blkno),
1386 offsetof(struct xfs_btree_block, bb_u.l.bb_lsn),
1387 offsetof(struct xfs_btree_block, bb_u.l.bb_uuid),
1388 offsetof(struct xfs_btree_block, bb_u.l.bb_owner),
1389 offsetof(struct xfs_btree_block, bb_u.l.bb_crc),
1390 offsetof(struct xfs_btree_block, bb_u.l.bb_pad),
1391 XFS_BTREE_LBLOCK_CRC_LEN
1394 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1395 XFS_BTREE_TRACE_ARGBI(cur, bp, fields);
1400 if (cur->bc_flags & XFS_BTREE_CRC_BLOCKS) {
1402 * We don't log the CRC when updating a btree
1403 * block but instead recreate it during log
1404 * recovery. As the log buffers have checksums
1405 * of their own this is safe and avoids logging a crc
1406 * update in a lot of places.
1408 if (fields == XFS_BB_ALL_BITS)
1409 fields = XFS_BB_ALL_BITS_CRC;
1410 nbits = XFS_BB_NUM_BITS_CRC;
1412 nbits = XFS_BB_NUM_BITS;
1414 xfs_btree_offsets(fields,
1415 (cur->bc_flags & XFS_BTREE_LONG_PTRS) ?
1416 loffsets : soffsets,
1417 nbits, &first, &last);
1418 xfs_trans_buf_set_type(cur->bc_tp, bp, XFS_BLFT_BTREE_BUF);
1419 xfs_trans_log_buf(cur->bc_tp, bp, first, last);
1421 xfs_trans_log_inode(cur->bc_tp, cur->bc_private.b.ip,
1422 xfs_ilog_fbroot(cur->bc_private.b.whichfork));
1425 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1429 * Increment cursor by one record at the level.
1430 * For nonzero levels the leaf-ward information is untouched.
1433 xfs_btree_increment(
1434 struct xfs_btree_cur *cur,
1436 int *stat) /* success/failure */
1438 struct xfs_btree_block *block;
1439 union xfs_btree_ptr ptr;
1441 int error; /* error return value */
1444 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1445 XFS_BTREE_TRACE_ARGI(cur, level);
1447 ASSERT(level < cur->bc_nlevels);
1449 /* Read-ahead to the right at this level. */
1450 xfs_btree_readahead(cur, level, XFS_BTCUR_RIGHTRA);
1452 /* Get a pointer to the btree block. */
1453 block = xfs_btree_get_block(cur, level, &bp);
1456 error = xfs_btree_check_block(cur, block, level, bp);
1461 /* We're done if we remain in the block after the increment. */
1462 if (++cur->bc_ptrs[level] <= xfs_btree_get_numrecs(block))
1465 /* Fail if we just went off the right edge of the tree. */
1466 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_RIGHTSIB);
1467 if (xfs_btree_ptr_is_null(cur, &ptr))
1470 XFS_BTREE_STATS_INC(cur, increment);
1473 * March up the tree incrementing pointers.
1474 * Stop when we don't go off the right edge of a block.
1476 for (lev = level + 1; lev < cur->bc_nlevels; lev++) {
1477 block = xfs_btree_get_block(cur, lev, &bp);
1480 error = xfs_btree_check_block(cur, block, lev, bp);
1485 if (++cur->bc_ptrs[lev] <= xfs_btree_get_numrecs(block))
1488 /* Read-ahead the right block for the next loop. */
1489 xfs_btree_readahead(cur, lev, XFS_BTCUR_RIGHTRA);
1493 * If we went off the root then we are either seriously
1494 * confused or have the tree root in an inode.
1496 if (lev == cur->bc_nlevels) {
1497 if (cur->bc_flags & XFS_BTREE_ROOT_IN_INODE)
1500 error = EFSCORRUPTED;
1503 ASSERT(lev < cur->bc_nlevels);
1506 * Now walk back down the tree, fixing up the cursor's buffer
1507 * pointers and key numbers.
1509 for (block = xfs_btree_get_block(cur, lev, &bp); lev > level; ) {
1510 union xfs_btree_ptr *ptrp;
1512 ptrp = xfs_btree_ptr_addr(cur, cur->bc_ptrs[lev], block);
1514 error = xfs_btree_read_buf_block(cur, ptrp, 0, &block, &bp);
1518 xfs_btree_setbuf(cur, lev, bp);
1519 cur->bc_ptrs[lev] = 1;
1522 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1527 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1532 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
1537 * Decrement cursor by one record at the level.
1538 * For nonzero levels the leaf-ward information is untouched.
1541 xfs_btree_decrement(
1542 struct xfs_btree_cur *cur,
1544 int *stat) /* success/failure */
1546 struct xfs_btree_block *block;
1548 int error; /* error return value */
1550 union xfs_btree_ptr ptr;
1552 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1553 XFS_BTREE_TRACE_ARGI(cur, level);
1555 ASSERT(level < cur->bc_nlevels);
1557 /* Read-ahead to the left at this level. */
1558 xfs_btree_readahead(cur, level, XFS_BTCUR_LEFTRA);
1560 /* We're done if we remain in the block after the decrement. */
1561 if (--cur->bc_ptrs[level] > 0)
1564 /* Get a pointer to the btree block. */
1565 block = xfs_btree_get_block(cur, level, &bp);
1568 error = xfs_btree_check_block(cur, block, level, bp);
1573 /* Fail if we just went off the left edge of the tree. */
1574 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_LEFTSIB);
1575 if (xfs_btree_ptr_is_null(cur, &ptr))
1578 XFS_BTREE_STATS_INC(cur, decrement);
1581 * March up the tree decrementing pointers.
1582 * Stop when we don't go off the left edge of a block.
1584 for (lev = level + 1; lev < cur->bc_nlevels; lev++) {
1585 if (--cur->bc_ptrs[lev] > 0)
1587 /* Read-ahead the left block for the next loop. */
1588 xfs_btree_readahead(cur, lev, XFS_BTCUR_LEFTRA);
1592 * If we went off the root then we are seriously confused.
1593 * or the root of the tree is in an inode.
1595 if (lev == cur->bc_nlevels) {
1596 if (cur->bc_flags & XFS_BTREE_ROOT_IN_INODE)
1599 error = EFSCORRUPTED;
1602 ASSERT(lev < cur->bc_nlevels);
1605 * Now walk back down the tree, fixing up the cursor's buffer
1606 * pointers and key numbers.
1608 for (block = xfs_btree_get_block(cur, lev, &bp); lev > level; ) {
1609 union xfs_btree_ptr *ptrp;
1611 ptrp = xfs_btree_ptr_addr(cur, cur->bc_ptrs[lev], block);
1613 error = xfs_btree_read_buf_block(cur, ptrp, 0, &block, &bp);
1616 xfs_btree_setbuf(cur, lev, bp);
1617 cur->bc_ptrs[lev] = xfs_btree_get_numrecs(block);
1620 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1625 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1630 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
1635 xfs_btree_lookup_get_block(
1636 struct xfs_btree_cur *cur, /* btree cursor */
1637 int level, /* level in the btree */
1638 union xfs_btree_ptr *pp, /* ptr to btree block */
1639 struct xfs_btree_block **blkp) /* return btree block */
1641 struct xfs_buf *bp; /* buffer pointer for btree block */
1644 /* special case the root block if in an inode */
1645 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
1646 (level == cur->bc_nlevels - 1)) {
1647 *blkp = xfs_btree_get_iroot(cur);
1652 * If the old buffer at this level for the disk address we are
1653 * looking for re-use it.
1655 * Otherwise throw it away and get a new one.
1657 bp = cur->bc_bufs[level];
1658 if (bp && XFS_BUF_ADDR(bp) == xfs_btree_ptr_to_daddr(cur, pp)) {
1659 *blkp = XFS_BUF_TO_BLOCK(bp);
1663 error = xfs_btree_read_buf_block(cur, pp, 0, blkp, &bp);
1667 xfs_btree_setbuf(cur, level, bp);
1672 * Get current search key. For level 0 we don't actually have a key
1673 * structure so we make one up from the record. For all other levels
1674 * we just return the right key.
1676 STATIC union xfs_btree_key *
1677 xfs_lookup_get_search_key(
1678 struct xfs_btree_cur *cur,
1681 struct xfs_btree_block *block,
1682 union xfs_btree_key *kp)
1685 cur->bc_ops->init_key_from_rec(kp,
1686 xfs_btree_rec_addr(cur, keyno, block));
1690 return xfs_btree_key_addr(cur, keyno, block);
1694 * Lookup the record. The cursor is made to point to it, based on dir.
1695 * stat is set to 0 if can't find any such record, 1 for success.
1699 struct xfs_btree_cur *cur, /* btree cursor */
1700 xfs_lookup_t dir, /* <=, ==, or >= */
1701 int *stat) /* success/failure */
1703 struct xfs_btree_block *block; /* current btree block */
1704 __int64_t diff; /* difference for the current key */
1705 int error; /* error return value */
1706 int keyno; /* current key number */
1707 int level; /* level in the btree */
1708 union xfs_btree_ptr *pp; /* ptr to btree block */
1709 union xfs_btree_ptr ptr; /* ptr to btree block */
1711 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1712 XFS_BTREE_TRACE_ARGI(cur, dir);
1714 XFS_BTREE_STATS_INC(cur, lookup);
1719 /* initialise start pointer from cursor */
1720 cur->bc_ops->init_ptr_from_cur(cur, &ptr);
1724 * Iterate over each level in the btree, starting at the root.
1725 * For each level above the leaves, find the key we need, based
1726 * on the lookup record, then follow the corresponding block
1727 * pointer down to the next level.
1729 for (level = cur->bc_nlevels - 1, diff = 1; level >= 0; level--) {
1730 /* Get the block we need to do the lookup on. */
1731 error = xfs_btree_lookup_get_block(cur, level, pp, &block);
1737 * If we already had a key match at a higher level, we
1738 * know we need to use the first entry in this block.
1742 /* Otherwise search this block. Do a binary search. */
1744 int high; /* high entry number */
1745 int low; /* low entry number */
1747 /* Set low and high entry numbers, 1-based. */
1749 high = xfs_btree_get_numrecs(block);
1751 /* Block is empty, must be an empty leaf. */
1752 ASSERT(level == 0 && cur->bc_nlevels == 1);
1754 cur->bc_ptrs[0] = dir != XFS_LOOKUP_LE;
1755 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1760 /* Binary search the block. */
1761 while (low <= high) {
1762 union xfs_btree_key key;
1763 union xfs_btree_key *kp;
1765 XFS_BTREE_STATS_INC(cur, compare);
1767 /* keyno is average of low and high. */
1768 keyno = (low + high) >> 1;
1770 /* Get current search key */
1771 kp = xfs_lookup_get_search_key(cur, level,
1772 keyno, block, &key);
1775 * Compute difference to get next direction:
1776 * - less than, move right
1777 * - greater than, move left
1778 * - equal, we're done
1780 diff = cur->bc_ops->key_diff(cur, kp);
1791 * If there are more levels, set up for the next level
1792 * by getting the block number and filling in the cursor.
1796 * If we moved left, need the previous key number,
1797 * unless there isn't one.
1799 if (diff > 0 && --keyno < 1)
1801 pp = xfs_btree_ptr_addr(cur, keyno, block);
1804 error = xfs_btree_check_ptr(cur, pp, 0, level);
1808 cur->bc_ptrs[level] = keyno;
1812 /* Done with the search. See if we need to adjust the results. */
1813 if (dir != XFS_LOOKUP_LE && diff < 0) {
1816 * If ge search and we went off the end of the block, but it's
1817 * not the last block, we're in the wrong block.
1819 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_RIGHTSIB);
1820 if (dir == XFS_LOOKUP_GE &&
1821 keyno > xfs_btree_get_numrecs(block) &&
1822 !xfs_btree_ptr_is_null(cur, &ptr)) {
1825 cur->bc_ptrs[0] = keyno;
1826 error = xfs_btree_increment(cur, 0, &i);
1829 XFS_WANT_CORRUPTED_RETURN(i == 1);
1830 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1834 } else if (dir == XFS_LOOKUP_LE && diff > 0)
1836 cur->bc_ptrs[0] = keyno;
1838 /* Return if we succeeded or not. */
1839 if (keyno == 0 || keyno > xfs_btree_get_numrecs(block))
1841 else if (dir != XFS_LOOKUP_EQ || diff == 0)
1845 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1849 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
1854 * Update keys at all levels from here to the root along the cursor's path.
1858 struct xfs_btree_cur *cur,
1859 union xfs_btree_key *keyp,
1862 struct xfs_btree_block *block;
1864 union xfs_btree_key *kp;
1867 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1868 XFS_BTREE_TRACE_ARGIK(cur, level, keyp);
1870 ASSERT(!(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) || level >= 1);
1873 * Go up the tree from this level toward the root.
1874 * At each level, update the key value to the value input.
1875 * Stop when we reach a level where the cursor isn't pointing
1876 * at the first entry in the block.
1878 for (ptr = 1; ptr == 1 && level < cur->bc_nlevels; level++) {
1882 block = xfs_btree_get_block(cur, level, &bp);
1884 error = xfs_btree_check_block(cur, block, level, bp);
1886 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
1890 ptr = cur->bc_ptrs[level];
1891 kp = xfs_btree_key_addr(cur, ptr, block);
1892 xfs_btree_copy_keys(cur, kp, keyp, 1);
1893 xfs_btree_log_keys(cur, bp, ptr, ptr);
1896 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1901 * Update the record referred to by cur to the value in the
1902 * given record. This either works (return 0) or gets an
1903 * EFSCORRUPTED error.
1907 struct xfs_btree_cur *cur,
1908 union xfs_btree_rec *rec)
1910 struct xfs_btree_block *block;
1914 union xfs_btree_rec *rp;
1916 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1917 XFS_BTREE_TRACE_ARGR(cur, rec);
1919 /* Pick up the current block. */
1920 block = xfs_btree_get_block(cur, 0, &bp);
1923 error = xfs_btree_check_block(cur, block, 0, bp);
1927 /* Get the address of the rec to be updated. */
1928 ptr = cur->bc_ptrs[0];
1929 rp = xfs_btree_rec_addr(cur, ptr, block);
1931 /* Fill in the new contents and log them. */
1932 xfs_btree_copy_recs(cur, rp, rec, 1);
1933 xfs_btree_log_recs(cur, bp, ptr, ptr);
1936 * If we are tracking the last record in the tree and
1937 * we are at the far right edge of the tree, update it.
1939 if (xfs_btree_is_lastrec(cur, block, 0)) {
1940 cur->bc_ops->update_lastrec(cur, block, rec,
1941 ptr, LASTREC_UPDATE);
1944 /* Updating first rec in leaf. Pass new key value up to our parent. */
1946 union xfs_btree_key key;
1948 cur->bc_ops->init_key_from_rec(&key, rec);
1949 error = xfs_btree_updkey(cur, &key, 1);
1954 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1958 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
1963 * Move 1 record left from cur/level if possible.
1964 * Update cur to reflect the new path.
1966 STATIC int /* error */
1968 struct xfs_btree_cur *cur,
1970 int *stat) /* success/failure */
1972 union xfs_btree_key key; /* btree key */
1973 struct xfs_buf *lbp; /* left buffer pointer */
1974 struct xfs_btree_block *left; /* left btree block */
1975 int lrecs; /* left record count */
1976 struct xfs_buf *rbp; /* right buffer pointer */
1977 struct xfs_btree_block *right; /* right btree block */
1978 int rrecs; /* right record count */
1979 union xfs_btree_ptr lptr; /* left btree pointer */
1980 union xfs_btree_key *rkp = NULL; /* right btree key */
1981 union xfs_btree_ptr *rpp = NULL; /* right address pointer */
1982 union xfs_btree_rec *rrp = NULL; /* right record pointer */
1983 int error; /* error return value */
1985 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1986 XFS_BTREE_TRACE_ARGI(cur, level);
1988 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
1989 level == cur->bc_nlevels - 1)
1992 /* Set up variables for this block as "right". */
1993 right = xfs_btree_get_block(cur, level, &rbp);
1996 error = xfs_btree_check_block(cur, right, level, rbp);
2001 /* If we've got no left sibling then we can't shift an entry left. */
2002 xfs_btree_get_sibling(cur, right, &lptr, XFS_BB_LEFTSIB);
2003 if (xfs_btree_ptr_is_null(cur, &lptr))
2007 * If the cursor entry is the one that would be moved, don't
2008 * do it... it's too complicated.
2010 if (cur->bc_ptrs[level] <= 1)
2013 /* Set up the left neighbor as "left". */
2014 error = xfs_btree_read_buf_block(cur, &lptr, 0, &left, &lbp);
2018 /* If it's full, it can't take another entry. */
2019 lrecs = xfs_btree_get_numrecs(left);
2020 if (lrecs == cur->bc_ops->get_maxrecs(cur, level))
2023 rrecs = xfs_btree_get_numrecs(right);
2026 * We add one entry to the left side and remove one for the right side.
2027 * Account for it here, the changes will be updated on disk and logged
2033 XFS_BTREE_STATS_INC(cur, lshift);
2034 XFS_BTREE_STATS_ADD(cur, moves, 1);
2037 * If non-leaf, copy a key and a ptr to the left block.
2038 * Log the changes to the left block.
2041 /* It's a non-leaf. Move keys and pointers. */
2042 union xfs_btree_key *lkp; /* left btree key */
2043 union xfs_btree_ptr *lpp; /* left address pointer */
2045 lkp = xfs_btree_key_addr(cur, lrecs, left);
2046 rkp = xfs_btree_key_addr(cur, 1, right);
2048 lpp = xfs_btree_ptr_addr(cur, lrecs, left);
2049 rpp = xfs_btree_ptr_addr(cur, 1, right);
2051 error = xfs_btree_check_ptr(cur, rpp, 0, level);
2055 xfs_btree_copy_keys(cur, lkp, rkp, 1);
2056 xfs_btree_copy_ptrs(cur, lpp, rpp, 1);
2058 xfs_btree_log_keys(cur, lbp, lrecs, lrecs);
2059 xfs_btree_log_ptrs(cur, lbp, lrecs, lrecs);
2061 ASSERT(cur->bc_ops->keys_inorder(cur,
2062 xfs_btree_key_addr(cur, lrecs - 1, left), lkp));
2064 /* It's a leaf. Move records. */
2065 union xfs_btree_rec *lrp; /* left record pointer */
2067 lrp = xfs_btree_rec_addr(cur, lrecs, left);
2068 rrp = xfs_btree_rec_addr(cur, 1, right);
2070 xfs_btree_copy_recs(cur, lrp, rrp, 1);
2071 xfs_btree_log_recs(cur, lbp, lrecs, lrecs);
2073 ASSERT(cur->bc_ops->recs_inorder(cur,
2074 xfs_btree_rec_addr(cur, lrecs - 1, left), lrp));
2077 xfs_btree_set_numrecs(left, lrecs);
2078 xfs_btree_log_block(cur, lbp, XFS_BB_NUMRECS);
2080 xfs_btree_set_numrecs(right, rrecs);
2081 xfs_btree_log_block(cur, rbp, XFS_BB_NUMRECS);
2084 * Slide the contents of right down one entry.
2086 XFS_BTREE_STATS_ADD(cur, moves, rrecs - 1);
2088 /* It's a nonleaf. operate on keys and ptrs */
2090 int i; /* loop index */
2092 for (i = 0; i < rrecs; i++) {
2093 error = xfs_btree_check_ptr(cur, rpp, i + 1, level);
2098 xfs_btree_shift_keys(cur,
2099 xfs_btree_key_addr(cur, 2, right),
2101 xfs_btree_shift_ptrs(cur,
2102 xfs_btree_ptr_addr(cur, 2, right),
2105 xfs_btree_log_keys(cur, rbp, 1, rrecs);
2106 xfs_btree_log_ptrs(cur, rbp, 1, rrecs);
2108 /* It's a leaf. operate on records */
2109 xfs_btree_shift_recs(cur,
2110 xfs_btree_rec_addr(cur, 2, right),
2112 xfs_btree_log_recs(cur, rbp, 1, rrecs);
2115 * If it's the first record in the block, we'll need a key
2116 * structure to pass up to the next level (updkey).
2118 cur->bc_ops->init_key_from_rec(&key,
2119 xfs_btree_rec_addr(cur, 1, right));
2123 /* Update the parent key values of right. */
2124 error = xfs_btree_updkey(cur, rkp, level + 1);
2128 /* Slide the cursor value left one. */
2129 cur->bc_ptrs[level]--;
2131 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2136 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2141 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
2146 * Move 1 record right from cur/level if possible.
2147 * Update cur to reflect the new path.
2149 STATIC int /* error */
2151 struct xfs_btree_cur *cur,
2153 int *stat) /* success/failure */
2155 union xfs_btree_key key; /* btree key */
2156 struct xfs_buf *lbp; /* left buffer pointer */
2157 struct xfs_btree_block *left; /* left btree block */
2158 struct xfs_buf *rbp; /* right buffer pointer */
2159 struct xfs_btree_block *right; /* right btree block */
2160 struct xfs_btree_cur *tcur; /* temporary btree cursor */
2161 union xfs_btree_ptr rptr; /* right block pointer */
2162 union xfs_btree_key *rkp; /* right btree key */
2163 int rrecs; /* right record count */
2164 int lrecs; /* left record count */
2165 int error; /* error return value */
2166 int i; /* loop counter */
2168 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
2169 XFS_BTREE_TRACE_ARGI(cur, level);
2171 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
2172 (level == cur->bc_nlevels - 1))
2175 /* Set up variables for this block as "left". */
2176 left = xfs_btree_get_block(cur, level, &lbp);
2179 error = xfs_btree_check_block(cur, left, level, lbp);
2184 /* If we've got no right sibling then we can't shift an entry right. */
2185 xfs_btree_get_sibling(cur, left, &rptr, XFS_BB_RIGHTSIB);
2186 if (xfs_btree_ptr_is_null(cur, &rptr))
2190 * If the cursor entry is the one that would be moved, don't
2191 * do it... it's too complicated.
2193 lrecs = xfs_btree_get_numrecs(left);
2194 if (cur->bc_ptrs[level] >= lrecs)
2197 /* Set up the right neighbor as "right". */
2198 error = xfs_btree_read_buf_block(cur, &rptr, 0, &right, &rbp);
2202 /* If it's full, it can't take another entry. */
2203 rrecs = xfs_btree_get_numrecs(right);
2204 if (rrecs == cur->bc_ops->get_maxrecs(cur, level))
2207 XFS_BTREE_STATS_INC(cur, rshift);
2208 XFS_BTREE_STATS_ADD(cur, moves, rrecs);
2211 * Make a hole at the start of the right neighbor block, then
2212 * copy the last left block entry to the hole.
2215 /* It's a nonleaf. make a hole in the keys and ptrs */
2216 union xfs_btree_key *lkp;
2217 union xfs_btree_ptr *lpp;
2218 union xfs_btree_ptr *rpp;
2220 lkp = xfs_btree_key_addr(cur, lrecs, left);
2221 lpp = xfs_btree_ptr_addr(cur, lrecs, left);
2222 rkp = xfs_btree_key_addr(cur, 1, right);
2223 rpp = xfs_btree_ptr_addr(cur, 1, right);
2226 for (i = rrecs - 1; i >= 0; i--) {
2227 error = xfs_btree_check_ptr(cur, rpp, i, level);
2233 xfs_btree_shift_keys(cur, rkp, 1, rrecs);
2234 xfs_btree_shift_ptrs(cur, rpp, 1, rrecs);
2237 error = xfs_btree_check_ptr(cur, lpp, 0, level);
2242 /* Now put the new data in, and log it. */
2243 xfs_btree_copy_keys(cur, rkp, lkp, 1);
2244 xfs_btree_copy_ptrs(cur, rpp, lpp, 1);
2246 xfs_btree_log_keys(cur, rbp, 1, rrecs + 1);
2247 xfs_btree_log_ptrs(cur, rbp, 1, rrecs + 1);
2249 ASSERT(cur->bc_ops->keys_inorder(cur, rkp,
2250 xfs_btree_key_addr(cur, 2, right)));
2252 /* It's a leaf. make a hole in the records */
2253 union xfs_btree_rec *lrp;
2254 union xfs_btree_rec *rrp;
2256 lrp = xfs_btree_rec_addr(cur, lrecs, left);
2257 rrp = xfs_btree_rec_addr(cur, 1, right);
2259 xfs_btree_shift_recs(cur, rrp, 1, rrecs);
2261 /* Now put the new data in, and log it. */
2262 xfs_btree_copy_recs(cur, rrp, lrp, 1);
2263 xfs_btree_log_recs(cur, rbp, 1, rrecs + 1);
2265 cur->bc_ops->init_key_from_rec(&key, rrp);
2268 ASSERT(cur->bc_ops->recs_inorder(cur, rrp,
2269 xfs_btree_rec_addr(cur, 2, right)));
2273 * Decrement and log left's numrecs, bump and log right's numrecs.
2275 xfs_btree_set_numrecs(left, --lrecs);
2276 xfs_btree_log_block(cur, lbp, XFS_BB_NUMRECS);
2278 xfs_btree_set_numrecs(right, ++rrecs);
2279 xfs_btree_log_block(cur, rbp, XFS_BB_NUMRECS);
2282 * Using a temporary cursor, update the parent key values of the
2283 * block on the right.
2285 error = xfs_btree_dup_cursor(cur, &tcur);
2288 i = xfs_btree_lastrec(tcur, level);
2289 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
2291 error = xfs_btree_increment(tcur, level, &i);
2295 error = xfs_btree_updkey(tcur, rkp, level + 1);
2299 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
2301 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2306 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2311 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
2315 XFS_BTREE_TRACE_CURSOR(tcur, XBT_ERROR);
2316 xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
2321 * Split cur/level block in half.
2322 * Return new block number and the key to its first
2323 * record (to be inserted into parent).
2325 STATIC int /* error */
2327 struct xfs_btree_cur *cur,
2329 union xfs_btree_ptr *ptrp,
2330 union xfs_btree_key *key,
2331 struct xfs_btree_cur **curp,
2332 int *stat) /* success/failure */
2334 union xfs_btree_ptr lptr; /* left sibling block ptr */
2335 struct xfs_buf *lbp; /* left buffer pointer */
2336 struct xfs_btree_block *left; /* left btree block */
2337 union xfs_btree_ptr rptr; /* right sibling block ptr */
2338 struct xfs_buf *rbp; /* right buffer pointer */
2339 struct xfs_btree_block *right; /* right btree block */
2340 union xfs_btree_ptr rrptr; /* right-right sibling ptr */
2341 struct xfs_buf *rrbp; /* right-right buffer pointer */
2342 struct xfs_btree_block *rrblock; /* right-right btree block */
2346 int error; /* error return value */
2351 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
2352 XFS_BTREE_TRACE_ARGIPK(cur, level, *ptrp, key);
2354 XFS_BTREE_STATS_INC(cur, split);
2356 /* Set up left block (current one). */
2357 left = xfs_btree_get_block(cur, level, &lbp);
2360 error = xfs_btree_check_block(cur, left, level, lbp);
2365 xfs_btree_buf_to_ptr(cur, lbp, &lptr);
2367 /* Allocate the new block. If we can't do it, we're toast. Give up. */
2368 error = cur->bc_ops->alloc_block(cur, &lptr, &rptr, stat);
2373 XFS_BTREE_STATS_INC(cur, alloc);
2375 /* Set up the new block as "right". */
2376 error = xfs_btree_get_buf_block(cur, &rptr, 0, &right, &rbp);
2380 /* Fill in the btree header for the new right block. */
2381 xfs_btree_init_block_cur(cur, rbp, xfs_btree_get_level(left), 0);
2384 * Split the entries between the old and the new block evenly.
2385 * Make sure that if there's an odd number of entries now, that
2386 * each new block will have the same number of entries.
2388 lrecs = xfs_btree_get_numrecs(left);
2390 if ((lrecs & 1) && cur->bc_ptrs[level] <= rrecs + 1)
2392 src_index = (lrecs - rrecs + 1);
2394 XFS_BTREE_STATS_ADD(cur, moves, rrecs);
2397 * Copy btree block entries from the left block over to the
2398 * new block, the right. Update the right block and log the
2402 /* It's a non-leaf. Move keys and pointers. */
2403 union xfs_btree_key *lkp; /* left btree key */
2404 union xfs_btree_ptr *lpp; /* left address pointer */
2405 union xfs_btree_key *rkp; /* right btree key */
2406 union xfs_btree_ptr *rpp; /* right address pointer */
2408 lkp = xfs_btree_key_addr(cur, src_index, left);
2409 lpp = xfs_btree_ptr_addr(cur, src_index, left);
2410 rkp = xfs_btree_key_addr(cur, 1, right);
2411 rpp = xfs_btree_ptr_addr(cur, 1, right);
2414 for (i = src_index; i < rrecs; i++) {
2415 error = xfs_btree_check_ptr(cur, lpp, i, level);
2421 xfs_btree_copy_keys(cur, rkp, lkp, rrecs);
2422 xfs_btree_copy_ptrs(cur, rpp, lpp, rrecs);
2424 xfs_btree_log_keys(cur, rbp, 1, rrecs);
2425 xfs_btree_log_ptrs(cur, rbp, 1, rrecs);
2427 /* Grab the keys to the entries moved to the right block */
2428 xfs_btree_copy_keys(cur, key, rkp, 1);
2430 /* It's a leaf. Move records. */
2431 union xfs_btree_rec *lrp; /* left record pointer */
2432 union xfs_btree_rec *rrp; /* right record pointer */
2434 lrp = xfs_btree_rec_addr(cur, src_index, left);
2435 rrp = xfs_btree_rec_addr(cur, 1, right);
2437 xfs_btree_copy_recs(cur, rrp, lrp, rrecs);
2438 xfs_btree_log_recs(cur, rbp, 1, rrecs);
2440 cur->bc_ops->init_key_from_rec(key,
2441 xfs_btree_rec_addr(cur, 1, right));
2446 * Find the left block number by looking in the buffer.
2447 * Adjust numrecs, sibling pointers.
2449 xfs_btree_get_sibling(cur, left, &rrptr, XFS_BB_RIGHTSIB);
2450 xfs_btree_set_sibling(cur, right, &rrptr, XFS_BB_RIGHTSIB);
2451 xfs_btree_set_sibling(cur, right, &lptr, XFS_BB_LEFTSIB);
2452 xfs_btree_set_sibling(cur, left, &rptr, XFS_BB_RIGHTSIB);
2455 xfs_btree_set_numrecs(left, lrecs);
2456 xfs_btree_set_numrecs(right, xfs_btree_get_numrecs(right) + rrecs);
2458 xfs_btree_log_block(cur, rbp, XFS_BB_ALL_BITS);
2459 xfs_btree_log_block(cur, lbp, XFS_BB_NUMRECS | XFS_BB_RIGHTSIB);
2462 * If there's a block to the new block's right, make that block
2463 * point back to right instead of to left.
2465 if (!xfs_btree_ptr_is_null(cur, &rrptr)) {
2466 error = xfs_btree_read_buf_block(cur, &rrptr,
2467 0, &rrblock, &rrbp);
2470 xfs_btree_set_sibling(cur, rrblock, &rptr, XFS_BB_LEFTSIB);
2471 xfs_btree_log_block(cur, rrbp, XFS_BB_LEFTSIB);
2474 * If the cursor is really in the right block, move it there.
2475 * If it's just pointing past the last entry in left, then we'll
2476 * insert there, so don't change anything in that case.
2478 if (cur->bc_ptrs[level] > lrecs + 1) {
2479 xfs_btree_setbuf(cur, level, rbp);
2480 cur->bc_ptrs[level] -= lrecs;
2483 * If there are more levels, we'll need another cursor which refers
2484 * the right block, no matter where this cursor was.
2486 if (level + 1 < cur->bc_nlevels) {
2487 error = xfs_btree_dup_cursor(cur, curp);
2490 (*curp)->bc_ptrs[level + 1]++;
2493 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2497 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2502 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
2507 * Copy the old inode root contents into a real block and make the
2508 * broot point to it.
2511 xfs_btree_new_iroot(
2512 struct xfs_btree_cur *cur, /* btree cursor */
2513 int *logflags, /* logging flags for inode */
2514 int *stat) /* return status - 0 fail */
2516 struct xfs_buf *cbp; /* buffer for cblock */
2517 struct xfs_btree_block *block; /* btree block */
2518 struct xfs_btree_block *cblock; /* child btree block */
2519 union xfs_btree_key *ckp; /* child key pointer */
2520 union xfs_btree_ptr *cpp; /* child ptr pointer */
2521 union xfs_btree_key *kp; /* pointer to btree key */
2522 union xfs_btree_ptr *pp; /* pointer to block addr */
2523 union xfs_btree_ptr nptr; /* new block addr */
2524 int level; /* btree level */
2525 int error; /* error return code */
2527 int i; /* loop counter */
2530 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
2531 XFS_BTREE_STATS_INC(cur, newroot);
2533 ASSERT(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE);
2535 level = cur->bc_nlevels - 1;
2537 block = xfs_btree_get_iroot(cur);
2538 pp = xfs_btree_ptr_addr(cur, 1, block);
2540 /* Allocate the new block. If we can't do it, we're toast. Give up. */
2541 error = cur->bc_ops->alloc_block(cur, pp, &nptr, stat);
2545 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2548 XFS_BTREE_STATS_INC(cur, alloc);
2550 /* Copy the root into a real block. */
2551 error = xfs_btree_get_buf_block(cur, &nptr, 0, &cblock, &cbp);
2556 * we can't just memcpy() the root in for CRC enabled btree blocks.
2557 * In that case have to also ensure the blkno remains correct
2559 memcpy(cblock, block, xfs_btree_block_len(cur));
2560 if (cur->bc_flags & XFS_BTREE_CRC_BLOCKS) {
2561 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
2562 cblock->bb_u.l.bb_blkno = cpu_to_be64(cbp->b_bn);
2564 cblock->bb_u.s.bb_blkno = cpu_to_be64(cbp->b_bn);
2567 be16_add_cpu(&block->bb_level, 1);
2568 xfs_btree_set_numrecs(block, 1);
2570 cur->bc_ptrs[level + 1] = 1;
2572 kp = xfs_btree_key_addr(cur, 1, block);
2573 ckp = xfs_btree_key_addr(cur, 1, cblock);
2574 xfs_btree_copy_keys(cur, ckp, kp, xfs_btree_get_numrecs(cblock));
2576 cpp = xfs_btree_ptr_addr(cur, 1, cblock);
2578 for (i = 0; i < be16_to_cpu(cblock->bb_numrecs); i++) {
2579 error = xfs_btree_check_ptr(cur, pp, i, level);
2584 xfs_btree_copy_ptrs(cur, cpp, pp, xfs_btree_get_numrecs(cblock));
2587 error = xfs_btree_check_ptr(cur, &nptr, 0, level);
2591 xfs_btree_copy_ptrs(cur, pp, &nptr, 1);
2593 xfs_iroot_realloc(cur->bc_private.b.ip,
2594 1 - xfs_btree_get_numrecs(cblock),
2595 cur->bc_private.b.whichfork);
2597 xfs_btree_setbuf(cur, level, cbp);
2600 * Do all this logging at the end so that
2601 * the root is at the right level.
2603 xfs_btree_log_block(cur, cbp, XFS_BB_ALL_BITS);
2604 xfs_btree_log_keys(cur, cbp, 1, be16_to_cpu(cblock->bb_numrecs));
2605 xfs_btree_log_ptrs(cur, cbp, 1, be16_to_cpu(cblock->bb_numrecs));
2608 XFS_ILOG_CORE | xfs_ilog_fbroot(cur->bc_private.b.whichfork);
2610 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2613 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
2618 * Allocate a new root block, fill it in.
2620 STATIC int /* error */
2622 struct xfs_btree_cur *cur, /* btree cursor */
2623 int *stat) /* success/failure */
2625 struct xfs_btree_block *block; /* one half of the old root block */
2626 struct xfs_buf *bp; /* buffer containing block */
2627 int error; /* error return value */
2628 struct xfs_buf *lbp; /* left buffer pointer */
2629 struct xfs_btree_block *left; /* left btree block */
2630 struct xfs_buf *nbp; /* new (root) buffer */
2631 struct xfs_btree_block *new; /* new (root) btree block */
2632 int nptr; /* new value for key index, 1 or 2 */
2633 struct xfs_buf *rbp; /* right buffer pointer */
2634 struct xfs_btree_block *right; /* right btree block */
2635 union xfs_btree_ptr rptr;
2636 union xfs_btree_ptr lptr;
2638 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
2639 XFS_BTREE_STATS_INC(cur, newroot);
2641 /* initialise our start point from the cursor */
2642 cur->bc_ops->init_ptr_from_cur(cur, &rptr);
2644 /* Allocate the new block. If we can't do it, we're toast. Give up. */
2645 error = cur->bc_ops->alloc_block(cur, &rptr, &lptr, stat);
2650 XFS_BTREE_STATS_INC(cur, alloc);
2652 /* Set up the new block. */
2653 error = xfs_btree_get_buf_block(cur, &lptr, 0, &new, &nbp);
2657 /* Set the root in the holding structure increasing the level by 1. */
2658 cur->bc_ops->set_root(cur, &lptr, 1);
2661 * At the previous root level there are now two blocks: the old root,
2662 * and the new block generated when it was split. We don't know which
2663 * one the cursor is pointing at, so we set up variables "left" and
2664 * "right" for each case.
2666 block = xfs_btree_get_block(cur, cur->bc_nlevels - 1, &bp);
2669 error = xfs_btree_check_block(cur, block, cur->bc_nlevels - 1, bp);
2674 xfs_btree_get_sibling(cur, block, &rptr, XFS_BB_RIGHTSIB);
2675 if (!xfs_btree_ptr_is_null(cur, &rptr)) {
2676 /* Our block is left, pick up the right block. */
2678 xfs_btree_buf_to_ptr(cur, lbp, &lptr);
2680 error = xfs_btree_read_buf_block(cur, &rptr, 0, &right, &rbp);
2686 /* Our block is right, pick up the left block. */
2688 xfs_btree_buf_to_ptr(cur, rbp, &rptr);
2690 xfs_btree_get_sibling(cur, right, &lptr, XFS_BB_LEFTSIB);
2691 error = xfs_btree_read_buf_block(cur, &lptr, 0, &left, &lbp);
2697 /* Fill in the new block's btree header and log it. */
2698 xfs_btree_init_block_cur(cur, nbp, cur->bc_nlevels, 2);
2699 xfs_btree_log_block(cur, nbp, XFS_BB_ALL_BITS);
2700 ASSERT(!xfs_btree_ptr_is_null(cur, &lptr) &&
2701 !xfs_btree_ptr_is_null(cur, &rptr));
2703 /* Fill in the key data in the new root. */
2704 if (xfs_btree_get_level(left) > 0) {
2705 xfs_btree_copy_keys(cur,
2706 xfs_btree_key_addr(cur, 1, new),
2707 xfs_btree_key_addr(cur, 1, left), 1);
2708 xfs_btree_copy_keys(cur,
2709 xfs_btree_key_addr(cur, 2, new),
2710 xfs_btree_key_addr(cur, 1, right), 1);
2712 cur->bc_ops->init_key_from_rec(
2713 xfs_btree_key_addr(cur, 1, new),
2714 xfs_btree_rec_addr(cur, 1, left));
2715 cur->bc_ops->init_key_from_rec(
2716 xfs_btree_key_addr(cur, 2, new),
2717 xfs_btree_rec_addr(cur, 1, right));
2719 xfs_btree_log_keys(cur, nbp, 1, 2);
2721 /* Fill in the pointer data in the new root. */
2722 xfs_btree_copy_ptrs(cur,
2723 xfs_btree_ptr_addr(cur, 1, new), &lptr, 1);
2724 xfs_btree_copy_ptrs(cur,
2725 xfs_btree_ptr_addr(cur, 2, new), &rptr, 1);
2726 xfs_btree_log_ptrs(cur, nbp, 1, 2);
2728 /* Fix up the cursor. */
2729 xfs_btree_setbuf(cur, cur->bc_nlevels, nbp);
2730 cur->bc_ptrs[cur->bc_nlevels] = nptr;
2732 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2736 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
2739 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2745 xfs_btree_make_block_unfull(
2746 struct xfs_btree_cur *cur, /* btree cursor */
2747 int level, /* btree level */
2748 int numrecs,/* # of recs in block */
2749 int *oindex,/* old tree index */
2750 int *index, /* new tree index */
2751 union xfs_btree_ptr *nptr, /* new btree ptr */
2752 struct xfs_btree_cur **ncur, /* new btree cursor */
2753 union xfs_btree_rec *nrec, /* new record */
2756 union xfs_btree_key key; /* new btree key value */
2759 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
2760 level == cur->bc_nlevels - 1) {
2761 struct xfs_inode *ip = cur->bc_private.b.ip;
2763 if (numrecs < cur->bc_ops->get_dmaxrecs(cur, level)) {
2764 /* A root block that can be made bigger. */
2765 xfs_iroot_realloc(ip, 1, cur->bc_private.b.whichfork);
2767 /* A root block that needs replacing */
2770 error = xfs_btree_new_iroot(cur, &logflags, stat);
2771 if (error || *stat == 0)
2774 xfs_trans_log_inode(cur->bc_tp, ip, logflags);
2780 /* First, try shifting an entry to the right neighbor. */
2781 error = xfs_btree_rshift(cur, level, stat);
2785 /* Next, try shifting an entry to the left neighbor. */
2786 error = xfs_btree_lshift(cur, level, stat);
2791 *oindex = *index = cur->bc_ptrs[level];
2796 * Next, try splitting the current block in half.
2798 * If this works we have to re-set our variables because we
2799 * could be in a different block now.
2801 error = xfs_btree_split(cur, level, nptr, &key, ncur, stat);
2802 if (error || *stat == 0)
2806 *index = cur->bc_ptrs[level];
2807 cur->bc_ops->init_rec_from_key(&key, nrec);
2812 * Insert one record/level. Return information to the caller
2813 * allowing the next level up to proceed if necessary.
2817 struct xfs_btree_cur *cur, /* btree cursor */
2818 int level, /* level to insert record at */
2819 union xfs_btree_ptr *ptrp, /* i/o: block number inserted */
2820 union xfs_btree_rec *recp, /* i/o: record data inserted */
2821 struct xfs_btree_cur **curp, /* output: new cursor replacing cur */
2822 int *stat) /* success/failure */
2824 struct xfs_btree_block *block; /* btree block */
2825 struct xfs_buf *bp; /* buffer for block */
2826 union xfs_btree_key key; /* btree key */
2827 union xfs_btree_ptr nptr; /* new block ptr */
2828 struct xfs_btree_cur *ncur; /* new btree cursor */
2829 union xfs_btree_rec nrec; /* new record count */
2830 int optr; /* old key/record index */
2831 int ptr; /* key/record index */
2832 int numrecs;/* number of records */
2833 int error; /* error return value */
2838 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
2839 XFS_BTREE_TRACE_ARGIPR(cur, level, *ptrp, recp);
2844 * If we have an external root pointer, and we've made it to the
2845 * root level, allocate a new root block and we're done.
2847 if (!(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
2848 (level >= cur->bc_nlevels)) {
2849 error = xfs_btree_new_root(cur, stat);
2850 xfs_btree_set_ptr_null(cur, ptrp);
2852 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2856 /* If we're off the left edge, return failure. */
2857 ptr = cur->bc_ptrs[level];
2859 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2864 /* Make a key out of the record data to be inserted, and save it. */
2865 cur->bc_ops->init_key_from_rec(&key, recp);
2869 XFS_BTREE_STATS_INC(cur, insrec);
2871 /* Get pointers to the btree buffer and block. */
2872 block = xfs_btree_get_block(cur, level, &bp);
2873 numrecs = xfs_btree_get_numrecs(block);
2876 error = xfs_btree_check_block(cur, block, level, bp);
2880 /* Check that the new entry is being inserted in the right place. */
2881 if (ptr <= numrecs) {
2883 ASSERT(cur->bc_ops->recs_inorder(cur, recp,
2884 xfs_btree_rec_addr(cur, ptr, block)));
2886 ASSERT(cur->bc_ops->keys_inorder(cur, &key,
2887 xfs_btree_key_addr(cur, ptr, block)));
2893 * If the block is full, we can't insert the new entry until we
2894 * make the block un-full.
2896 xfs_btree_set_ptr_null(cur, &nptr);
2897 if (numrecs == cur->bc_ops->get_maxrecs(cur, level)) {
2898 error = xfs_btree_make_block_unfull(cur, level, numrecs,
2899 &optr, &ptr, &nptr, &ncur, &nrec, stat);
2900 if (error || *stat == 0)
2905 * The current block may have changed if the block was
2906 * previously full and we have just made space in it.
2908 block = xfs_btree_get_block(cur, level, &bp);
2909 numrecs = xfs_btree_get_numrecs(block);
2912 error = xfs_btree_check_block(cur, block, level, bp);
2918 * At this point we know there's room for our new entry in the block
2919 * we're pointing at.
2921 XFS_BTREE_STATS_ADD(cur, moves, numrecs - ptr + 1);
2924 /* It's a nonleaf. make a hole in the keys and ptrs */
2925 union xfs_btree_key *kp;
2926 union xfs_btree_ptr *pp;
2928 kp = xfs_btree_key_addr(cur, ptr, block);
2929 pp = xfs_btree_ptr_addr(cur, ptr, block);
2932 for (i = numrecs - ptr; i >= 0; i--) {
2933 error = xfs_btree_check_ptr(cur, pp, i, level);
2939 xfs_btree_shift_keys(cur, kp, 1, numrecs - ptr + 1);
2940 xfs_btree_shift_ptrs(cur, pp, 1, numrecs - ptr + 1);
2943 error = xfs_btree_check_ptr(cur, ptrp, 0, level);
2948 /* Now put the new data in, bump numrecs and log it. */
2949 xfs_btree_copy_keys(cur, kp, &key, 1);
2950 xfs_btree_copy_ptrs(cur, pp, ptrp, 1);
2952 xfs_btree_set_numrecs(block, numrecs);
2953 xfs_btree_log_ptrs(cur, bp, ptr, numrecs);
2954 xfs_btree_log_keys(cur, bp, ptr, numrecs);
2956 if (ptr < numrecs) {
2957 ASSERT(cur->bc_ops->keys_inorder(cur, kp,
2958 xfs_btree_key_addr(cur, ptr + 1, block)));
2962 /* It's a leaf. make a hole in the records */
2963 union xfs_btree_rec *rp;
2965 rp = xfs_btree_rec_addr(cur, ptr, block);
2967 xfs_btree_shift_recs(cur, rp, 1, numrecs - ptr + 1);
2969 /* Now put the new data in, bump numrecs and log it. */
2970 xfs_btree_copy_recs(cur, rp, recp, 1);
2971 xfs_btree_set_numrecs(block, ++numrecs);
2972 xfs_btree_log_recs(cur, bp, ptr, numrecs);
2974 if (ptr < numrecs) {
2975 ASSERT(cur->bc_ops->recs_inorder(cur, rp,
2976 xfs_btree_rec_addr(cur, ptr + 1, block)));
2981 /* Log the new number of records in the btree header. */
2982 xfs_btree_log_block(cur, bp, XFS_BB_NUMRECS);
2984 /* If we inserted at the start of a block, update the parents' keys. */
2986 error = xfs_btree_updkey(cur, &key, level + 1);
2992 * If we are tracking the last record in the tree and
2993 * we are at the far right edge of the tree, update it.
2995 if (xfs_btree_is_lastrec(cur, block, level)) {
2996 cur->bc_ops->update_lastrec(cur, block, recp,
2997 ptr, LASTREC_INSREC);
3001 * Return the new block number, if any.
3002 * If there is one, give back a record value and a cursor too.
3005 if (!xfs_btree_ptr_is_null(cur, &nptr)) {
3010 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3015 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
3020 * Insert the record at the point referenced by cur.
3022 * A multi-level split of the tree on insert will invalidate the original
3023 * cursor. All callers of this function should assume that the cursor is
3024 * no longer valid and revalidate it.
3028 struct xfs_btree_cur *cur,
3031 int error; /* error return value */
3032 int i; /* result value, 0 for failure */
3033 int level; /* current level number in btree */
3034 union xfs_btree_ptr nptr; /* new block number (split result) */
3035 struct xfs_btree_cur *ncur; /* new cursor (split result) */
3036 struct xfs_btree_cur *pcur; /* previous level's cursor */
3037 union xfs_btree_rec rec; /* record to insert */
3043 xfs_btree_set_ptr_null(cur, &nptr);
3044 cur->bc_ops->init_rec_from_cur(cur, &rec);
3047 * Loop going up the tree, starting at the leaf level.
3048 * Stop when we don't get a split block, that must mean that
3049 * the insert is finished with this level.
3053 * Insert nrec/nptr into this level of the tree.
3054 * Note if we fail, nptr will be null.
3056 error = xfs_btree_insrec(pcur, level, &nptr, &rec, &ncur, &i);
3059 xfs_btree_del_cursor(pcur, XFS_BTREE_ERROR);
3063 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
3067 * See if the cursor we just used is trash.
3068 * Can't trash the caller's cursor, but otherwise we should
3069 * if ncur is a new cursor or we're about to be done.
3072 (ncur || xfs_btree_ptr_is_null(cur, &nptr))) {
3073 /* Save the state from the cursor before we trash it */
3074 if (cur->bc_ops->update_cursor)
3075 cur->bc_ops->update_cursor(pcur, cur);
3076 cur->bc_nlevels = pcur->bc_nlevels;
3077 xfs_btree_del_cursor(pcur, XFS_BTREE_NOERROR);
3079 /* If we got a new cursor, switch to it. */
3084 } while (!xfs_btree_ptr_is_null(cur, &nptr));
3086 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3090 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
3095 * Try to merge a non-leaf block back into the inode root.
3097 * Note: the killroot names comes from the fact that we're effectively
3098 * killing the old root block. But because we can't just delete the
3099 * inode we have to copy the single block it was pointing to into the
3103 xfs_btree_kill_iroot(
3104 struct xfs_btree_cur *cur)
3106 int whichfork = cur->bc_private.b.whichfork;
3107 struct xfs_inode *ip = cur->bc_private.b.ip;
3108 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
3109 struct xfs_btree_block *block;
3110 struct xfs_btree_block *cblock;
3111 union xfs_btree_key *kp;
3112 union xfs_btree_key *ckp;
3113 union xfs_btree_ptr *pp;
3114 union xfs_btree_ptr *cpp;
3115 struct xfs_buf *cbp;
3120 union xfs_btree_ptr ptr;
3124 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
3126 ASSERT(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE);
3127 ASSERT(cur->bc_nlevels > 1);
3130 * Don't deal with the root block needs to be a leaf case.
3131 * We're just going to turn the thing back into extents anyway.
3133 level = cur->bc_nlevels - 1;
3138 * Give up if the root has multiple children.
3140 block = xfs_btree_get_iroot(cur);
3141 if (xfs_btree_get_numrecs(block) != 1)
3144 cblock = xfs_btree_get_block(cur, level - 1, &cbp);
3145 numrecs = xfs_btree_get_numrecs(cblock);
3148 * Only do this if the next level will fit.
3149 * Then the data must be copied up to the inode,
3150 * instead of freeing the root you free the next level.
3152 if (numrecs > cur->bc_ops->get_dmaxrecs(cur, level))
3155 XFS_BTREE_STATS_INC(cur, killroot);
3158 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_LEFTSIB);
3159 ASSERT(xfs_btree_ptr_is_null(cur, &ptr));
3160 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_RIGHTSIB);
3161 ASSERT(xfs_btree_ptr_is_null(cur, &ptr));
3164 index = numrecs - cur->bc_ops->get_maxrecs(cur, level);
3166 xfs_iroot_realloc(cur->bc_private.b.ip, index,
3167 cur->bc_private.b.whichfork);
3168 block = ifp->if_broot;
3171 be16_add_cpu(&block->bb_numrecs, index);
3172 ASSERT(block->bb_numrecs == cblock->bb_numrecs);
3174 kp = xfs_btree_key_addr(cur, 1, block);
3175 ckp = xfs_btree_key_addr(cur, 1, cblock);
3176 xfs_btree_copy_keys(cur, kp, ckp, numrecs);
3178 pp = xfs_btree_ptr_addr(cur, 1, block);
3179 cpp = xfs_btree_ptr_addr(cur, 1, cblock);
3181 for (i = 0; i < numrecs; i++) {
3184 error = xfs_btree_check_ptr(cur, cpp, i, level - 1);
3186 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
3191 xfs_btree_copy_ptrs(cur, pp, cpp, numrecs);
3193 cur->bc_ops->free_block(cur, cbp);
3194 XFS_BTREE_STATS_INC(cur, free);
3196 cur->bc_bufs[level - 1] = NULL;
3197 be16_add_cpu(&block->bb_level, -1);
3198 xfs_trans_log_inode(cur->bc_tp, ip,
3199 XFS_ILOG_CORE | xfs_ilog_fbroot(cur->bc_private.b.whichfork));
3202 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3207 * Kill the current root node, and replace it with it's only child node.
3210 xfs_btree_kill_root(
3211 struct xfs_btree_cur *cur,
3214 union xfs_btree_ptr *newroot)
3218 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
3219 XFS_BTREE_STATS_INC(cur, killroot);
3222 * Update the root pointer, decreasing the level by 1 and then
3223 * free the old root.
3225 cur->bc_ops->set_root(cur, newroot, -1);
3227 error = cur->bc_ops->free_block(cur, bp);
3229 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
3233 XFS_BTREE_STATS_INC(cur, free);
3235 cur->bc_bufs[level] = NULL;
3236 cur->bc_ra[level] = 0;
3239 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3244 xfs_btree_dec_cursor(
3245 struct xfs_btree_cur *cur,
3253 error = xfs_btree_decrement(cur, level, &i);
3258 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3264 * Single level of the btree record deletion routine.
3265 * Delete record pointed to by cur/level.
3266 * Remove the record from its block then rebalance the tree.
3267 * Return 0 for error, 1 for done, 2 to go on to the next level.
3269 STATIC int /* error */
3271 struct xfs_btree_cur *cur, /* btree cursor */
3272 int level, /* level removing record from */
3273 int *stat) /* fail/done/go-on */
3275 struct xfs_btree_block *block; /* btree block */
3276 union xfs_btree_ptr cptr; /* current block ptr */
3277 struct xfs_buf *bp; /* buffer for block */
3278 int error; /* error return value */
3279 int i; /* loop counter */
3280 union xfs_btree_key key; /* storage for keyp */
3281 union xfs_btree_key *keyp = &key; /* passed to the next level */
3282 union xfs_btree_ptr lptr; /* left sibling block ptr */
3283 struct xfs_buf *lbp; /* left buffer pointer */
3284 struct xfs_btree_block *left; /* left btree block */
3285 int lrecs = 0; /* left record count */
3286 int ptr; /* key/record index */
3287 union xfs_btree_ptr rptr; /* right sibling block ptr */
3288 struct xfs_buf *rbp; /* right buffer pointer */
3289 struct xfs_btree_block *right; /* right btree block */
3290 struct xfs_btree_block *rrblock; /* right-right btree block */
3291 struct xfs_buf *rrbp; /* right-right buffer pointer */
3292 int rrecs = 0; /* right record count */
3293 struct xfs_btree_cur *tcur; /* temporary btree cursor */
3294 int numrecs; /* temporary numrec count */
3296 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
3297 XFS_BTREE_TRACE_ARGI(cur, level);
3301 /* Get the index of the entry being deleted, check for nothing there. */
3302 ptr = cur->bc_ptrs[level];
3304 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3309 /* Get the buffer & block containing the record or key/ptr. */
3310 block = xfs_btree_get_block(cur, level, &bp);
3311 numrecs = xfs_btree_get_numrecs(block);
3314 error = xfs_btree_check_block(cur, block, level, bp);
3319 /* Fail if we're off the end of the block. */
3320 if (ptr > numrecs) {
3321 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3326 XFS_BTREE_STATS_INC(cur, delrec);
3327 XFS_BTREE_STATS_ADD(cur, moves, numrecs - ptr);
3329 /* Excise the entries being deleted. */
3331 /* It's a nonleaf. operate on keys and ptrs */
3332 union xfs_btree_key *lkp;
3333 union xfs_btree_ptr *lpp;
3335 lkp = xfs_btree_key_addr(cur, ptr + 1, block);
3336 lpp = xfs_btree_ptr_addr(cur, ptr + 1, block);
3339 for (i = 0; i < numrecs - ptr; i++) {
3340 error = xfs_btree_check_ptr(cur, lpp, i, level);
3346 if (ptr < numrecs) {
3347 xfs_btree_shift_keys(cur, lkp, -1, numrecs - ptr);
3348 xfs_btree_shift_ptrs(cur, lpp, -1, numrecs - ptr);
3349 xfs_btree_log_keys(cur, bp, ptr, numrecs - 1);
3350 xfs_btree_log_ptrs(cur, bp, ptr, numrecs - 1);
3354 * If it's the first record in the block, we'll need to pass a
3355 * key up to the next level (updkey).
3358 keyp = xfs_btree_key_addr(cur, 1, block);
3360 /* It's a leaf. operate on records */
3361 if (ptr < numrecs) {
3362 xfs_btree_shift_recs(cur,
3363 xfs_btree_rec_addr(cur, ptr + 1, block),
3365 xfs_btree_log_recs(cur, bp, ptr, numrecs - 1);
3369 * If it's the first record in the block, we'll need a key
3370 * structure to pass up to the next level (updkey).
3373 cur->bc_ops->init_key_from_rec(&key,
3374 xfs_btree_rec_addr(cur, 1, block));
3380 * Decrement and log the number of entries in the block.
3382 xfs_btree_set_numrecs(block, --numrecs);
3383 xfs_btree_log_block(cur, bp, XFS_BB_NUMRECS);
3386 * If we are tracking the last record in the tree and
3387 * we are at the far right edge of the tree, update it.
3389 if (xfs_btree_is_lastrec(cur, block, level)) {
3390 cur->bc_ops->update_lastrec(cur, block, NULL,
3391 ptr, LASTREC_DELREC);
3395 * We're at the root level. First, shrink the root block in-memory.
3396 * Try to get rid of the next level down. If we can't then there's
3397 * nothing left to do.
3399 if (level == cur->bc_nlevels - 1) {
3400 if (cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) {
3401 xfs_iroot_realloc(cur->bc_private.b.ip, -1,
3402 cur->bc_private.b.whichfork);
3404 error = xfs_btree_kill_iroot(cur);
3408 error = xfs_btree_dec_cursor(cur, level, stat);
3416 * If this is the root level, and there's only one entry left,
3417 * and it's NOT the leaf level, then we can get rid of this
3420 if (numrecs == 1 && level > 0) {
3421 union xfs_btree_ptr *pp;
3423 * pp is still set to the first pointer in the block.
3424 * Make it the new root of the btree.
3426 pp = xfs_btree_ptr_addr(cur, 1, block);
3427 error = xfs_btree_kill_root(cur, bp, level, pp);
3430 } else if (level > 0) {
3431 error = xfs_btree_dec_cursor(cur, level, stat);
3440 * If we deleted the leftmost entry in the block, update the
3441 * key values above us in the tree.
3444 error = xfs_btree_updkey(cur, keyp, level + 1);
3450 * If the number of records remaining in the block is at least
3451 * the minimum, we're done.
3453 if (numrecs >= cur->bc_ops->get_minrecs(cur, level)) {
3454 error = xfs_btree_dec_cursor(cur, level, stat);
3461 * Otherwise, we have to move some records around to keep the
3462 * tree balanced. Look at the left and right sibling blocks to
3463 * see if we can re-balance by moving only one record.
3465 xfs_btree_get_sibling(cur, block, &rptr, XFS_BB_RIGHTSIB);
3466 xfs_btree_get_sibling(cur, block, &lptr, XFS_BB_LEFTSIB);
3468 if (cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) {
3470 * One child of root, need to get a chance to copy its contents
3471 * into the root and delete it. Can't go up to next level,
3472 * there's nothing to delete there.
3474 if (xfs_btree_ptr_is_null(cur, &rptr) &&
3475 xfs_btree_ptr_is_null(cur, &lptr) &&
3476 level == cur->bc_nlevels - 2) {
3477 error = xfs_btree_kill_iroot(cur);
3479 error = xfs_btree_dec_cursor(cur, level, stat);
3486 ASSERT(!xfs_btree_ptr_is_null(cur, &rptr) ||
3487 !xfs_btree_ptr_is_null(cur, &lptr));
3490 * Duplicate the cursor so our btree manipulations here won't
3491 * disrupt the next level up.
3493 error = xfs_btree_dup_cursor(cur, &tcur);
3498 * If there's a right sibling, see if it's ok to shift an entry
3501 if (!xfs_btree_ptr_is_null(cur, &rptr)) {
3503 * Move the temp cursor to the last entry in the next block.
3504 * Actually any entry but the first would suffice.
3506 i = xfs_btree_lastrec(tcur, level);
3507 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
3509 error = xfs_btree_increment(tcur, level, &i);
3512 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
3514 i = xfs_btree_lastrec(tcur, level);
3515 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
3517 /* Grab a pointer to the block. */
3518 right = xfs_btree_get_block(tcur, level, &rbp);
3520 error = xfs_btree_check_block(tcur, right, level, rbp);
3524 /* Grab the current block number, for future use. */
3525 xfs_btree_get_sibling(tcur, right, &cptr, XFS_BB_LEFTSIB);
3528 * If right block is full enough so that removing one entry
3529 * won't make it too empty, and left-shifting an entry out
3530 * of right to us works, we're done.
3532 if (xfs_btree_get_numrecs(right) - 1 >=
3533 cur->bc_ops->get_minrecs(tcur, level)) {
3534 error = xfs_btree_lshift(tcur, level, &i);
3538 ASSERT(xfs_btree_get_numrecs(block) >=
3539 cur->bc_ops->get_minrecs(tcur, level));
3541 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
3544 error = xfs_btree_dec_cursor(cur, level, stat);
3552 * Otherwise, grab the number of records in right for
3553 * future reference, and fix up the temp cursor to point
3554 * to our block again (last record).
3556 rrecs = xfs_btree_get_numrecs(right);
3557 if (!xfs_btree_ptr_is_null(cur, &lptr)) {
3558 i = xfs_btree_firstrec(tcur, level);
3559 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
3561 error = xfs_btree_decrement(tcur, level, &i);
3564 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
3569 * If there's a left sibling, see if it's ok to shift an entry
3572 if (!xfs_btree_ptr_is_null(cur, &lptr)) {
3574 * Move the temp cursor to the first entry in the
3577 i = xfs_btree_firstrec(tcur, level);
3578 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
3580 error = xfs_btree_decrement(tcur, level, &i);
3583 i = xfs_btree_firstrec(tcur, level);
3584 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
3586 /* Grab a pointer to the block. */
3587 left = xfs_btree_get_block(tcur, level, &lbp);
3589 error = xfs_btree_check_block(cur, left, level, lbp);
3593 /* Grab the current block number, for future use. */
3594 xfs_btree_get_sibling(tcur, left, &cptr, XFS_BB_RIGHTSIB);
3597 * If left block is full enough so that removing one entry
3598 * won't make it too empty, and right-shifting an entry out
3599 * of left to us works, we're done.
3601 if (xfs_btree_get_numrecs(left) - 1 >=
3602 cur->bc_ops->get_minrecs(tcur, level)) {
3603 error = xfs_btree_rshift(tcur, level, &i);
3607 ASSERT(xfs_btree_get_numrecs(block) >=
3608 cur->bc_ops->get_minrecs(tcur, level));
3609 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
3613 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3620 * Otherwise, grab the number of records in right for
3623 lrecs = xfs_btree_get_numrecs(left);
3626 /* Delete the temp cursor, we're done with it. */
3627 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
3630 /* If here, we need to do a join to keep the tree balanced. */
3631 ASSERT(!xfs_btree_ptr_is_null(cur, &cptr));
3633 if (!xfs_btree_ptr_is_null(cur, &lptr) &&
3634 lrecs + xfs_btree_get_numrecs(block) <=
3635 cur->bc_ops->get_maxrecs(cur, level)) {
3637 * Set "right" to be the starting block,
3638 * "left" to be the left neighbor.
3643 error = xfs_btree_read_buf_block(cur, &lptr, 0, &left, &lbp);
3648 * If that won't work, see if we can join with the right neighbor block.
3650 } else if (!xfs_btree_ptr_is_null(cur, &rptr) &&
3651 rrecs + xfs_btree_get_numrecs(block) <=
3652 cur->bc_ops->get_maxrecs(cur, level)) {
3654 * Set "left" to be the starting block,
3655 * "right" to be the right neighbor.
3660 error = xfs_btree_read_buf_block(cur, &rptr, 0, &right, &rbp);
3665 * Otherwise, we can't fix the imbalance.
3666 * Just return. This is probably a logic error, but it's not fatal.
3669 error = xfs_btree_dec_cursor(cur, level, stat);
3675 rrecs = xfs_btree_get_numrecs(right);
3676 lrecs = xfs_btree_get_numrecs(left);
3679 * We're now going to join "left" and "right" by moving all the stuff
3680 * in "right" to "left" and deleting "right".
3682 XFS_BTREE_STATS_ADD(cur, moves, rrecs);
3684 /* It's a non-leaf. Move keys and pointers. */
3685 union xfs_btree_key *lkp; /* left btree key */
3686 union xfs_btree_ptr *lpp; /* left address pointer */
3687 union xfs_btree_key *rkp; /* right btree key */
3688 union xfs_btree_ptr *rpp; /* right address pointer */
3690 lkp = xfs_btree_key_addr(cur, lrecs + 1, left);
3691 lpp = xfs_btree_ptr_addr(cur, lrecs + 1, left);
3692 rkp = xfs_btree_key_addr(cur, 1, right);
3693 rpp = xfs_btree_ptr_addr(cur, 1, right);
3695 for (i = 1; i < rrecs; i++) {
3696 error = xfs_btree_check_ptr(cur, rpp, i, level);
3701 xfs_btree_copy_keys(cur, lkp, rkp, rrecs);
3702 xfs_btree_copy_ptrs(cur, lpp, rpp, rrecs);
3704 xfs_btree_log_keys(cur, lbp, lrecs + 1, lrecs + rrecs);
3705 xfs_btree_log_ptrs(cur, lbp, lrecs + 1, lrecs + rrecs);
3707 /* It's a leaf. Move records. */
3708 union xfs_btree_rec *lrp; /* left record pointer */
3709 union xfs_btree_rec *rrp; /* right record pointer */
3711 lrp = xfs_btree_rec_addr(cur, lrecs + 1, left);
3712 rrp = xfs_btree_rec_addr(cur, 1, right);
3714 xfs_btree_copy_recs(cur, lrp, rrp, rrecs);
3715 xfs_btree_log_recs(cur, lbp, lrecs + 1, lrecs + rrecs);
3718 XFS_BTREE_STATS_INC(cur, join);
3721 * Fix up the number of records and right block pointer in the
3722 * surviving block, and log it.
3724 xfs_btree_set_numrecs(left, lrecs + rrecs);
3725 xfs_btree_get_sibling(cur, right, &cptr, XFS_BB_RIGHTSIB),
3726 xfs_btree_set_sibling(cur, left, &cptr, XFS_BB_RIGHTSIB);
3727 xfs_btree_log_block(cur, lbp, XFS_BB_NUMRECS | XFS_BB_RIGHTSIB);
3729 /* If there is a right sibling, point it to the remaining block. */
3730 xfs_btree_get_sibling(cur, left, &cptr, XFS_BB_RIGHTSIB);
3731 if (!xfs_btree_ptr_is_null(cur, &cptr)) {
3732 error = xfs_btree_read_buf_block(cur, &cptr, 0, &rrblock, &rrbp);
3735 xfs_btree_set_sibling(cur, rrblock, &lptr, XFS_BB_LEFTSIB);
3736 xfs_btree_log_block(cur, rrbp, XFS_BB_LEFTSIB);
3739 /* Free the deleted block. */
3740 error = cur->bc_ops->free_block(cur, rbp);
3743 XFS_BTREE_STATS_INC(cur, free);
3746 * If we joined with the left neighbor, set the buffer in the
3747 * cursor to the left block, and fix up the index.
3750 cur->bc_bufs[level] = lbp;
3751 cur->bc_ptrs[level] += lrecs;
3752 cur->bc_ra[level] = 0;
3755 * If we joined with the right neighbor and there's a level above
3756 * us, increment the cursor at that level.
3758 else if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) ||
3759 (level + 1 < cur->bc_nlevels)) {
3760 error = xfs_btree_increment(cur, level + 1, &i);
3766 * Readjust the ptr at this level if it's not a leaf, since it's
3767 * still pointing at the deletion point, which makes the cursor
3768 * inconsistent. If this makes the ptr 0, the caller fixes it up.
3769 * We can't use decrement because it would change the next level up.
3772 cur->bc_ptrs[level]--;
3774 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3775 /* Return value means the next level up has something to do. */
3780 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
3782 xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
3787 * Delete the record pointed to by cur.
3788 * The cursor refers to the place where the record was (could be inserted)
3789 * when the operation returns.
3793 struct xfs_btree_cur *cur,
3794 int *stat) /* success/failure */
3796 int error; /* error return value */
3800 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
3803 * Go up the tree, starting at leaf level.
3805 * If 2 is returned then a join was done; go to the next level.
3806 * Otherwise we are done.
3808 for (level = 0, i = 2; i == 2; level++) {
3809 error = xfs_btree_delrec(cur, level, &i);
3815 for (level = 1; level < cur->bc_nlevels; level++) {
3816 if (cur->bc_ptrs[level] == 0) {
3817 error = xfs_btree_decrement(cur, level, &i);
3825 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3829 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
3834 * Get the data from the pointed-to record.
3838 struct xfs_btree_cur *cur, /* btree cursor */
3839 union xfs_btree_rec **recp, /* output: btree record */
3840 int *stat) /* output: success/failure */
3842 struct xfs_btree_block *block; /* btree block */
3843 struct xfs_buf *bp; /* buffer pointer */
3844 int ptr; /* record number */
3846 int error; /* error return value */
3849 ptr = cur->bc_ptrs[0];
3850 block = xfs_btree_get_block(cur, 0, &bp);
3853 error = xfs_btree_check_block(cur, block, 0, bp);
3859 * Off the right end or left end, return failure.
3861 if (ptr > xfs_btree_get_numrecs(block) || ptr <= 0) {
3867 * Point to the record and extract its data.
3869 *recp = xfs_btree_rec_addr(cur, ptr, block);
3875 * Change the owner of a btree.
3877 * The mechanism we use here is ordered buffer logging. Because we don't know
3878 * how many buffers were are going to need to modify, we don't really want to
3879 * have to make transaction reservations for the worst case of every buffer in a
3880 * full size btree as that may be more space that we can fit in the log....
3882 * We do the btree walk in the most optimal manner possible - we have sibling
3883 * pointers so we can just walk all the blocks on each level from left to right
3884 * in a single pass, and then move to the next level and do the same. We can
3885 * also do readahead on the sibling pointers to get IO moving more quickly,
3886 * though for slow disks this is unlikely to make much difference to performance
3887 * as the amount of CPU work we have to do before moving to the next block is
3890 * For each btree block that we load, modify the owner appropriately, set the
3891 * buffer as an ordered buffer and log it appropriately. We need to ensure that
3892 * we mark the region we change dirty so that if the buffer is relogged in
3893 * a subsequent transaction the changes we make here as an ordered buffer are
3894 * correctly relogged in that transaction. If we are in recovery context, then
3895 * just queue the modified buffer as delayed write buffer so the transaction
3896 * recovery completion writes the changes to disk.
3899 xfs_btree_block_change_owner(
3900 struct xfs_btree_cur *cur,
3902 __uint64_t new_owner,
3903 struct list_head *buffer_list)
3905 struct xfs_btree_block *block;
3907 union xfs_btree_ptr rptr;
3909 /* do right sibling readahead */
3910 xfs_btree_readahead(cur, level, XFS_BTCUR_RIGHTRA);
3912 /* modify the owner */
3913 block = xfs_btree_get_block(cur, level, &bp);
3914 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
3915 block->bb_u.l.bb_owner = cpu_to_be64(new_owner);
3917 block->bb_u.s.bb_owner = cpu_to_be32(new_owner);
3920 * If the block is a root block hosted in an inode, we might not have a
3921 * buffer pointer here and we shouldn't attempt to log the change as the
3922 * information is already held in the inode and discarded when the root
3923 * block is formatted into the on-disk inode fork. We still change it,
3924 * though, so everything is consistent in memory.
3928 xfs_trans_ordered_buf(cur->bc_tp, bp);
3929 xfs_btree_log_block(cur, bp, XFS_BB_OWNER);
3931 xfs_buf_delwri_queue(bp, buffer_list);
3934 ASSERT(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE);
3935 ASSERT(level == cur->bc_nlevels - 1);
3938 /* now read rh sibling block for next iteration */
3939 xfs_btree_get_sibling(cur, block, &rptr, XFS_BB_RIGHTSIB);
3940 if (xfs_btree_ptr_is_null(cur, &rptr))
3943 return xfs_btree_lookup_get_block(cur, level, &rptr, &block);
3947 xfs_btree_change_owner(
3948 struct xfs_btree_cur *cur,
3949 __uint64_t new_owner,
3950 struct list_head *buffer_list)
3952 union xfs_btree_ptr lptr;
3954 struct xfs_btree_block *block = NULL;
3957 cur->bc_ops->init_ptr_from_cur(cur, &lptr);
3959 /* for each level */
3960 for (level = cur->bc_nlevels - 1; level >= 0; level--) {
3961 /* grab the left hand block */
3962 error = xfs_btree_lookup_get_block(cur, level, &lptr, &block);
3966 /* readahead the left most block for the next level down */
3968 union xfs_btree_ptr *ptr;
3970 ptr = xfs_btree_ptr_addr(cur, 1, block);
3971 xfs_btree_readahead_ptr(cur, ptr, 1);
3973 /* save for the next iteration of the loop */
3977 /* for each buffer in the level */
3979 error = xfs_btree_block_change_owner(cur, level,
3984 if (error != ENOENT)