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_buf_t *bp; /* buffer pointer (return value) */
557 xfs_daddr_t d; /* real disk block address */
559 ASSERT(fsbno != NULLFSBLOCK);
560 d = XFS_FSB_TO_DADDR(mp, fsbno);
561 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d, mp->m_bsize, lock);
562 ASSERT(!xfs_buf_geterror(bp));
567 * Get a buffer for the block, return it with no data read.
568 * Short-form addressing.
570 xfs_buf_t * /* buffer for agno/agbno */
572 xfs_mount_t *mp, /* file system mount point */
573 xfs_trans_t *tp, /* transaction pointer */
574 xfs_agnumber_t agno, /* allocation group number */
575 xfs_agblock_t agbno, /* allocation group block number */
576 uint lock) /* lock flags for get_buf */
578 xfs_buf_t *bp; /* buffer pointer (return value) */
579 xfs_daddr_t d; /* real disk block address */
581 ASSERT(agno != NULLAGNUMBER);
582 ASSERT(agbno != NULLAGBLOCK);
583 d = XFS_AGB_TO_DADDR(mp, agno, agbno);
584 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d, mp->m_bsize, lock);
585 ASSERT(!xfs_buf_geterror(bp));
590 * Check for the cursor referring to the last block at the given level.
592 int /* 1=is last block, 0=not last block */
593 xfs_btree_islastblock(
594 xfs_btree_cur_t *cur, /* btree cursor */
595 int level) /* level to check */
597 struct xfs_btree_block *block; /* generic btree block pointer */
598 xfs_buf_t *bp; /* buffer containing block */
600 block = xfs_btree_get_block(cur, level, &bp);
601 xfs_btree_check_block(cur, block, level, bp);
602 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
603 return block->bb_u.l.bb_rightsib == cpu_to_be64(NULLDFSBNO);
605 return block->bb_u.s.bb_rightsib == cpu_to_be32(NULLAGBLOCK);
609 * Change the cursor to point to the first record at the given level.
610 * Other levels are unaffected.
612 STATIC int /* success=1, failure=0 */
614 xfs_btree_cur_t *cur, /* btree cursor */
615 int level) /* level to change */
617 struct xfs_btree_block *block; /* generic btree block pointer */
618 xfs_buf_t *bp; /* buffer containing block */
621 * Get the block pointer for this level.
623 block = xfs_btree_get_block(cur, level, &bp);
624 xfs_btree_check_block(cur, block, level, bp);
626 * It's empty, there is no such record.
628 if (!block->bb_numrecs)
631 * Set the ptr value to 1, that's the first record/key.
633 cur->bc_ptrs[level] = 1;
638 * Change the cursor to point to the last record in the current block
639 * at the given level. Other levels are unaffected.
641 STATIC int /* success=1, failure=0 */
643 xfs_btree_cur_t *cur, /* btree cursor */
644 int level) /* level to change */
646 struct xfs_btree_block *block; /* generic btree block pointer */
647 xfs_buf_t *bp; /* buffer containing block */
650 * Get the block pointer for this level.
652 block = xfs_btree_get_block(cur, level, &bp);
653 xfs_btree_check_block(cur, block, level, bp);
655 * It's empty, there is no such record.
657 if (!block->bb_numrecs)
660 * Set the ptr value to numrecs, that's the last record/key.
662 cur->bc_ptrs[level] = be16_to_cpu(block->bb_numrecs);
667 * Compute first and last byte offsets for the fields given.
668 * Interprets the offsets table, which contains struct field offsets.
672 __int64_t fields, /* bitmask of fields */
673 const short *offsets, /* table of field offsets */
674 int nbits, /* number of bits to inspect */
675 int *first, /* output: first byte offset */
676 int *last) /* output: last byte offset */
678 int i; /* current bit number */
679 __int64_t imask; /* mask for current bit number */
683 * Find the lowest bit, so the first byte offset.
685 for (i = 0, imask = 1LL; ; i++, imask <<= 1) {
686 if (imask & fields) {
692 * Find the highest bit, so the last byte offset.
694 for (i = nbits - 1, imask = 1LL << i; ; i--, imask >>= 1) {
695 if (imask & fields) {
696 *last = offsets[i + 1] - 1;
703 * Get a buffer for the block, return it read in.
704 * Long-form addressing.
708 struct xfs_mount *mp, /* file system mount point */
709 struct xfs_trans *tp, /* transaction pointer */
710 xfs_fsblock_t fsbno, /* file system block number */
711 uint lock, /* lock flags for read_buf */
712 struct xfs_buf **bpp, /* buffer for fsbno */
713 int refval, /* ref count value for buffer */
714 const struct xfs_buf_ops *ops)
716 struct xfs_buf *bp; /* return value */
717 xfs_daddr_t d; /* real disk block address */
720 ASSERT(fsbno != NULLFSBLOCK);
721 d = XFS_FSB_TO_DADDR(mp, fsbno);
722 error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, d,
723 mp->m_bsize, lock, &bp, ops);
726 ASSERT(!xfs_buf_geterror(bp));
728 xfs_buf_set_ref(bp, refval);
734 * Read-ahead the block, don't wait for it, don't return a buffer.
735 * Long-form addressing.
739 xfs_btree_reada_bufl(
740 struct xfs_mount *mp, /* file system mount point */
741 xfs_fsblock_t fsbno, /* file system block number */
742 xfs_extlen_t count, /* count of filesystem blocks */
743 const struct xfs_buf_ops *ops)
747 ASSERT(fsbno != NULLFSBLOCK);
748 d = XFS_FSB_TO_DADDR(mp, fsbno);
749 xfs_buf_readahead(mp->m_ddev_targp, d, mp->m_bsize * count, ops);
753 * Read-ahead the block, don't wait for it, don't return a buffer.
754 * Short-form addressing.
758 xfs_btree_reada_bufs(
759 struct xfs_mount *mp, /* file system mount point */
760 xfs_agnumber_t agno, /* allocation group number */
761 xfs_agblock_t agbno, /* allocation group block number */
762 xfs_extlen_t count, /* count of filesystem blocks */
763 const struct xfs_buf_ops *ops)
767 ASSERT(agno != NULLAGNUMBER);
768 ASSERT(agbno != NULLAGBLOCK);
769 d = XFS_AGB_TO_DADDR(mp, agno, agbno);
770 xfs_buf_readahead(mp->m_ddev_targp, d, mp->m_bsize * count, ops);
774 xfs_btree_readahead_lblock(
775 struct xfs_btree_cur *cur,
777 struct xfs_btree_block *block)
780 xfs_dfsbno_t left = be64_to_cpu(block->bb_u.l.bb_leftsib);
781 xfs_dfsbno_t right = be64_to_cpu(block->bb_u.l.bb_rightsib);
783 if ((lr & XFS_BTCUR_LEFTRA) && left != NULLDFSBNO) {
784 xfs_btree_reada_bufl(cur->bc_mp, left, 1,
785 cur->bc_ops->buf_ops);
789 if ((lr & XFS_BTCUR_RIGHTRA) && right != NULLDFSBNO) {
790 xfs_btree_reada_bufl(cur->bc_mp, right, 1,
791 cur->bc_ops->buf_ops);
799 xfs_btree_readahead_sblock(
800 struct xfs_btree_cur *cur,
802 struct xfs_btree_block *block)
805 xfs_agblock_t left = be32_to_cpu(block->bb_u.s.bb_leftsib);
806 xfs_agblock_t right = be32_to_cpu(block->bb_u.s.bb_rightsib);
809 if ((lr & XFS_BTCUR_LEFTRA) && left != NULLAGBLOCK) {
810 xfs_btree_reada_bufs(cur->bc_mp, cur->bc_private.a.agno,
811 left, 1, cur->bc_ops->buf_ops);
815 if ((lr & XFS_BTCUR_RIGHTRA) && right != NULLAGBLOCK) {
816 xfs_btree_reada_bufs(cur->bc_mp, cur->bc_private.a.agno,
817 right, 1, cur->bc_ops->buf_ops);
825 * Read-ahead btree blocks, at the given level.
826 * Bits in lr are set from XFS_BTCUR_{LEFT,RIGHT}RA.
830 struct xfs_btree_cur *cur, /* btree cursor */
831 int lev, /* level in btree */
832 int lr) /* left/right bits */
834 struct xfs_btree_block *block;
837 * No readahead needed if we are at the root level and the
838 * btree root is stored in the inode.
840 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
841 (lev == cur->bc_nlevels - 1))
844 if ((cur->bc_ra[lev] | lr) == cur->bc_ra[lev])
847 cur->bc_ra[lev] |= lr;
848 block = XFS_BUF_TO_BLOCK(cur->bc_bufs[lev]);
850 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
851 return xfs_btree_readahead_lblock(cur, lr, block);
852 return xfs_btree_readahead_sblock(cur, lr, block);
856 xfs_btree_ptr_to_daddr(
857 struct xfs_btree_cur *cur,
858 union xfs_btree_ptr *ptr)
860 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
861 ASSERT(ptr->l != cpu_to_be64(NULLDFSBNO));
863 return XFS_FSB_TO_DADDR(cur->bc_mp, be64_to_cpu(ptr->l));
865 ASSERT(cur->bc_private.a.agno != NULLAGNUMBER);
866 ASSERT(ptr->s != cpu_to_be32(NULLAGBLOCK));
868 return XFS_AGB_TO_DADDR(cur->bc_mp, cur->bc_private.a.agno,
869 be32_to_cpu(ptr->s));
874 * Readahead @count btree blocks at the given @ptr location.
876 * We don't need to care about long or short form btrees here as we have a
877 * method of converting the ptr directly to a daddr available to us.
880 xfs_btree_readahead_ptr(
881 struct xfs_btree_cur *cur,
882 union xfs_btree_ptr *ptr,
885 xfs_buf_readahead(cur->bc_mp->m_ddev_targp,
886 xfs_btree_ptr_to_daddr(cur, ptr),
887 cur->bc_mp->m_bsize * count, cur->bc_ops->buf_ops);
891 * Set the buffer for level "lev" in the cursor to bp, releasing
892 * any previous buffer.
896 xfs_btree_cur_t *cur, /* btree cursor */
897 int lev, /* level in btree */
898 xfs_buf_t *bp) /* new buffer to set */
900 struct xfs_btree_block *b; /* btree block */
902 if (cur->bc_bufs[lev])
903 xfs_trans_brelse(cur->bc_tp, cur->bc_bufs[lev]);
904 cur->bc_bufs[lev] = bp;
907 b = XFS_BUF_TO_BLOCK(bp);
908 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
909 if (b->bb_u.l.bb_leftsib == cpu_to_be64(NULLDFSBNO))
910 cur->bc_ra[lev] |= XFS_BTCUR_LEFTRA;
911 if (b->bb_u.l.bb_rightsib == cpu_to_be64(NULLDFSBNO))
912 cur->bc_ra[lev] |= XFS_BTCUR_RIGHTRA;
914 if (b->bb_u.s.bb_leftsib == cpu_to_be32(NULLAGBLOCK))
915 cur->bc_ra[lev] |= XFS_BTCUR_LEFTRA;
916 if (b->bb_u.s.bb_rightsib == cpu_to_be32(NULLAGBLOCK))
917 cur->bc_ra[lev] |= XFS_BTCUR_RIGHTRA;
922 xfs_btree_ptr_is_null(
923 struct xfs_btree_cur *cur,
924 union xfs_btree_ptr *ptr)
926 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
927 return ptr->l == cpu_to_be64(NULLDFSBNO);
929 return ptr->s == cpu_to_be32(NULLAGBLOCK);
933 xfs_btree_set_ptr_null(
934 struct xfs_btree_cur *cur,
935 union xfs_btree_ptr *ptr)
937 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
938 ptr->l = cpu_to_be64(NULLDFSBNO);
940 ptr->s = cpu_to_be32(NULLAGBLOCK);
944 * Get/set/init sibling pointers
947 xfs_btree_get_sibling(
948 struct xfs_btree_cur *cur,
949 struct xfs_btree_block *block,
950 union xfs_btree_ptr *ptr,
953 ASSERT(lr == XFS_BB_LEFTSIB || lr == XFS_BB_RIGHTSIB);
955 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
956 if (lr == XFS_BB_RIGHTSIB)
957 ptr->l = block->bb_u.l.bb_rightsib;
959 ptr->l = block->bb_u.l.bb_leftsib;
961 if (lr == XFS_BB_RIGHTSIB)
962 ptr->s = block->bb_u.s.bb_rightsib;
964 ptr->s = block->bb_u.s.bb_leftsib;
969 xfs_btree_set_sibling(
970 struct xfs_btree_cur *cur,
971 struct xfs_btree_block *block,
972 union xfs_btree_ptr *ptr,
975 ASSERT(lr == XFS_BB_LEFTSIB || lr == XFS_BB_RIGHTSIB);
977 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
978 if (lr == XFS_BB_RIGHTSIB)
979 block->bb_u.l.bb_rightsib = ptr->l;
981 block->bb_u.l.bb_leftsib = ptr->l;
983 if (lr == XFS_BB_RIGHTSIB)
984 block->bb_u.s.bb_rightsib = ptr->s;
986 block->bb_u.s.bb_leftsib = ptr->s;
991 xfs_btree_init_block_int(
992 struct xfs_mount *mp,
993 struct xfs_btree_block *buf,
1001 buf->bb_magic = cpu_to_be32(magic);
1002 buf->bb_level = cpu_to_be16(level);
1003 buf->bb_numrecs = cpu_to_be16(numrecs);
1005 if (flags & XFS_BTREE_LONG_PTRS) {
1006 buf->bb_u.l.bb_leftsib = cpu_to_be64(NULLDFSBNO);
1007 buf->bb_u.l.bb_rightsib = cpu_to_be64(NULLDFSBNO);
1008 if (flags & XFS_BTREE_CRC_BLOCKS) {
1009 buf->bb_u.l.bb_blkno = cpu_to_be64(blkno);
1010 buf->bb_u.l.bb_owner = cpu_to_be64(owner);
1011 uuid_copy(&buf->bb_u.l.bb_uuid, &mp->m_sb.sb_uuid);
1012 buf->bb_u.l.bb_pad = 0;
1013 buf->bb_u.l.bb_lsn = 0;
1016 /* owner is a 32 bit value on short blocks */
1017 __u32 __owner = (__u32)owner;
1019 buf->bb_u.s.bb_leftsib = cpu_to_be32(NULLAGBLOCK);
1020 buf->bb_u.s.bb_rightsib = cpu_to_be32(NULLAGBLOCK);
1021 if (flags & XFS_BTREE_CRC_BLOCKS) {
1022 buf->bb_u.s.bb_blkno = cpu_to_be64(blkno);
1023 buf->bb_u.s.bb_owner = cpu_to_be32(__owner);
1024 uuid_copy(&buf->bb_u.s.bb_uuid, &mp->m_sb.sb_uuid);
1025 buf->bb_u.s.bb_lsn = 0;
1031 xfs_btree_init_block(
1032 struct xfs_mount *mp,
1040 xfs_btree_init_block_int(mp, XFS_BUF_TO_BLOCK(bp), bp->b_bn,
1041 magic, level, numrecs, owner, flags);
1045 xfs_btree_init_block_cur(
1046 struct xfs_btree_cur *cur,
1054 * we can pull the owner from the cursor right now as the different
1055 * owners align directly with the pointer size of the btree. This may
1056 * change in future, but is safe for current users of the generic btree
1059 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
1060 owner = cur->bc_private.b.ip->i_ino;
1062 owner = cur->bc_private.a.agno;
1064 xfs_btree_init_block_int(cur->bc_mp, XFS_BUF_TO_BLOCK(bp), bp->b_bn,
1065 xfs_btree_magic(cur), level, numrecs,
1066 owner, cur->bc_flags);
1070 * Return true if ptr is the last record in the btree and
1071 * we need to track updates to this record. The decision
1072 * will be further refined in the update_lastrec method.
1075 xfs_btree_is_lastrec(
1076 struct xfs_btree_cur *cur,
1077 struct xfs_btree_block *block,
1080 union xfs_btree_ptr ptr;
1084 if (!(cur->bc_flags & XFS_BTREE_LASTREC_UPDATE))
1087 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_RIGHTSIB);
1088 if (!xfs_btree_ptr_is_null(cur, &ptr))
1094 xfs_btree_buf_to_ptr(
1095 struct xfs_btree_cur *cur,
1097 union xfs_btree_ptr *ptr)
1099 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
1100 ptr->l = cpu_to_be64(XFS_DADDR_TO_FSB(cur->bc_mp,
1103 ptr->s = cpu_to_be32(xfs_daddr_to_agbno(cur->bc_mp,
1110 struct xfs_btree_cur *cur,
1113 switch (cur->bc_btnum) {
1116 xfs_buf_set_ref(bp, XFS_ALLOC_BTREE_REF);
1119 case XFS_BTNUM_FINO:
1120 xfs_buf_set_ref(bp, XFS_INO_BTREE_REF);
1122 case XFS_BTNUM_BMAP:
1123 xfs_buf_set_ref(bp, XFS_BMAP_BTREE_REF);
1131 xfs_btree_get_buf_block(
1132 struct xfs_btree_cur *cur,
1133 union xfs_btree_ptr *ptr,
1135 struct xfs_btree_block **block,
1136 struct xfs_buf **bpp)
1138 struct xfs_mount *mp = cur->bc_mp;
1141 /* need to sort out how callers deal with failures first */
1142 ASSERT(!(flags & XBF_TRYLOCK));
1144 d = xfs_btree_ptr_to_daddr(cur, ptr);
1145 *bpp = xfs_trans_get_buf(cur->bc_tp, mp->m_ddev_targp, d,
1146 mp->m_bsize, flags);
1151 (*bpp)->b_ops = cur->bc_ops->buf_ops;
1152 *block = XFS_BUF_TO_BLOCK(*bpp);
1157 * Read in the buffer at the given ptr and return the buffer and
1158 * the block pointer within the buffer.
1161 xfs_btree_read_buf_block(
1162 struct xfs_btree_cur *cur,
1163 union xfs_btree_ptr *ptr,
1165 struct xfs_btree_block **block,
1166 struct xfs_buf **bpp)
1168 struct xfs_mount *mp = cur->bc_mp;
1172 /* need to sort out how callers deal with failures first */
1173 ASSERT(!(flags & XBF_TRYLOCK));
1175 d = xfs_btree_ptr_to_daddr(cur, ptr);
1176 error = xfs_trans_read_buf(mp, cur->bc_tp, mp->m_ddev_targp, d,
1177 mp->m_bsize, flags, bpp,
1178 cur->bc_ops->buf_ops);
1182 ASSERT(!xfs_buf_geterror(*bpp));
1183 xfs_btree_set_refs(cur, *bpp);
1184 *block = XFS_BUF_TO_BLOCK(*bpp);
1189 * Copy keys from one btree block to another.
1192 xfs_btree_copy_keys(
1193 struct xfs_btree_cur *cur,
1194 union xfs_btree_key *dst_key,
1195 union xfs_btree_key *src_key,
1198 ASSERT(numkeys >= 0);
1199 memcpy(dst_key, src_key, numkeys * cur->bc_ops->key_len);
1203 * Copy records from one btree block to another.
1206 xfs_btree_copy_recs(
1207 struct xfs_btree_cur *cur,
1208 union xfs_btree_rec *dst_rec,
1209 union xfs_btree_rec *src_rec,
1212 ASSERT(numrecs >= 0);
1213 memcpy(dst_rec, src_rec, numrecs * cur->bc_ops->rec_len);
1217 * Copy block pointers from one btree block to another.
1220 xfs_btree_copy_ptrs(
1221 struct xfs_btree_cur *cur,
1222 union xfs_btree_ptr *dst_ptr,
1223 union xfs_btree_ptr *src_ptr,
1226 ASSERT(numptrs >= 0);
1227 memcpy(dst_ptr, src_ptr, numptrs * xfs_btree_ptr_len(cur));
1231 * Shift keys one index left/right inside a single btree block.
1234 xfs_btree_shift_keys(
1235 struct xfs_btree_cur *cur,
1236 union xfs_btree_key *key,
1242 ASSERT(numkeys >= 0);
1243 ASSERT(dir == 1 || dir == -1);
1245 dst_key = (char *)key + (dir * cur->bc_ops->key_len);
1246 memmove(dst_key, key, numkeys * cur->bc_ops->key_len);
1250 * Shift records one index left/right inside a single btree block.
1253 xfs_btree_shift_recs(
1254 struct xfs_btree_cur *cur,
1255 union xfs_btree_rec *rec,
1261 ASSERT(numrecs >= 0);
1262 ASSERT(dir == 1 || dir == -1);
1264 dst_rec = (char *)rec + (dir * cur->bc_ops->rec_len);
1265 memmove(dst_rec, rec, numrecs * cur->bc_ops->rec_len);
1269 * Shift block pointers one index left/right inside a single btree block.
1272 xfs_btree_shift_ptrs(
1273 struct xfs_btree_cur *cur,
1274 union xfs_btree_ptr *ptr,
1280 ASSERT(numptrs >= 0);
1281 ASSERT(dir == 1 || dir == -1);
1283 dst_ptr = (char *)ptr + (dir * xfs_btree_ptr_len(cur));
1284 memmove(dst_ptr, ptr, numptrs * xfs_btree_ptr_len(cur));
1288 * Log key values from the btree block.
1292 struct xfs_btree_cur *cur,
1297 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1298 XFS_BTREE_TRACE_ARGBII(cur, bp, first, last);
1301 xfs_trans_buf_set_type(cur->bc_tp, bp, XFS_BLFT_BTREE_BUF);
1302 xfs_trans_log_buf(cur->bc_tp, bp,
1303 xfs_btree_key_offset(cur, first),
1304 xfs_btree_key_offset(cur, last + 1) - 1);
1306 xfs_trans_log_inode(cur->bc_tp, cur->bc_private.b.ip,
1307 xfs_ilog_fbroot(cur->bc_private.b.whichfork));
1310 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1314 * Log record values from the btree block.
1318 struct xfs_btree_cur *cur,
1323 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1324 XFS_BTREE_TRACE_ARGBII(cur, bp, first, last);
1326 xfs_trans_buf_set_type(cur->bc_tp, bp, XFS_BLFT_BTREE_BUF);
1327 xfs_trans_log_buf(cur->bc_tp, bp,
1328 xfs_btree_rec_offset(cur, first),
1329 xfs_btree_rec_offset(cur, last + 1) - 1);
1331 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1335 * Log block pointer fields from a btree block (nonleaf).
1339 struct xfs_btree_cur *cur, /* btree cursor */
1340 struct xfs_buf *bp, /* buffer containing btree block */
1341 int first, /* index of first pointer to log */
1342 int last) /* index of last pointer to log */
1344 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1345 XFS_BTREE_TRACE_ARGBII(cur, bp, first, last);
1348 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
1349 int level = xfs_btree_get_level(block);
1351 xfs_trans_buf_set_type(cur->bc_tp, bp, XFS_BLFT_BTREE_BUF);
1352 xfs_trans_log_buf(cur->bc_tp, bp,
1353 xfs_btree_ptr_offset(cur, first, level),
1354 xfs_btree_ptr_offset(cur, last + 1, level) - 1);
1356 xfs_trans_log_inode(cur->bc_tp, cur->bc_private.b.ip,
1357 xfs_ilog_fbroot(cur->bc_private.b.whichfork));
1360 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1364 * Log fields from a btree block header.
1367 xfs_btree_log_block(
1368 struct xfs_btree_cur *cur, /* btree cursor */
1369 struct xfs_buf *bp, /* buffer containing btree block */
1370 int fields) /* mask of fields: XFS_BB_... */
1372 int first; /* first byte offset logged */
1373 int last; /* last byte offset logged */
1374 static const short soffsets[] = { /* table of offsets (short) */
1375 offsetof(struct xfs_btree_block, bb_magic),
1376 offsetof(struct xfs_btree_block, bb_level),
1377 offsetof(struct xfs_btree_block, bb_numrecs),
1378 offsetof(struct xfs_btree_block, bb_u.s.bb_leftsib),
1379 offsetof(struct xfs_btree_block, bb_u.s.bb_rightsib),
1380 offsetof(struct xfs_btree_block, bb_u.s.bb_blkno),
1381 offsetof(struct xfs_btree_block, bb_u.s.bb_lsn),
1382 offsetof(struct xfs_btree_block, bb_u.s.bb_uuid),
1383 offsetof(struct xfs_btree_block, bb_u.s.bb_owner),
1384 offsetof(struct xfs_btree_block, bb_u.s.bb_crc),
1385 XFS_BTREE_SBLOCK_CRC_LEN
1387 static const short loffsets[] = { /* table of offsets (long) */
1388 offsetof(struct xfs_btree_block, bb_magic),
1389 offsetof(struct xfs_btree_block, bb_level),
1390 offsetof(struct xfs_btree_block, bb_numrecs),
1391 offsetof(struct xfs_btree_block, bb_u.l.bb_leftsib),
1392 offsetof(struct xfs_btree_block, bb_u.l.bb_rightsib),
1393 offsetof(struct xfs_btree_block, bb_u.l.bb_blkno),
1394 offsetof(struct xfs_btree_block, bb_u.l.bb_lsn),
1395 offsetof(struct xfs_btree_block, bb_u.l.bb_uuid),
1396 offsetof(struct xfs_btree_block, bb_u.l.bb_owner),
1397 offsetof(struct xfs_btree_block, bb_u.l.bb_crc),
1398 offsetof(struct xfs_btree_block, bb_u.l.bb_pad),
1399 XFS_BTREE_LBLOCK_CRC_LEN
1402 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1403 XFS_BTREE_TRACE_ARGBI(cur, bp, fields);
1408 if (cur->bc_flags & XFS_BTREE_CRC_BLOCKS) {
1410 * We don't log the CRC when updating a btree
1411 * block but instead recreate it during log
1412 * recovery. As the log buffers have checksums
1413 * of their own this is safe and avoids logging a crc
1414 * update in a lot of places.
1416 if (fields == XFS_BB_ALL_BITS)
1417 fields = XFS_BB_ALL_BITS_CRC;
1418 nbits = XFS_BB_NUM_BITS_CRC;
1420 nbits = XFS_BB_NUM_BITS;
1422 xfs_btree_offsets(fields,
1423 (cur->bc_flags & XFS_BTREE_LONG_PTRS) ?
1424 loffsets : soffsets,
1425 nbits, &first, &last);
1426 xfs_trans_buf_set_type(cur->bc_tp, bp, XFS_BLFT_BTREE_BUF);
1427 xfs_trans_log_buf(cur->bc_tp, bp, first, last);
1429 xfs_trans_log_inode(cur->bc_tp, cur->bc_private.b.ip,
1430 xfs_ilog_fbroot(cur->bc_private.b.whichfork));
1433 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1437 * Increment cursor by one record at the level.
1438 * For nonzero levels the leaf-ward information is untouched.
1441 xfs_btree_increment(
1442 struct xfs_btree_cur *cur,
1444 int *stat) /* success/failure */
1446 struct xfs_btree_block *block;
1447 union xfs_btree_ptr ptr;
1449 int error; /* error return value */
1452 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1453 XFS_BTREE_TRACE_ARGI(cur, level);
1455 ASSERT(level < cur->bc_nlevels);
1457 /* Read-ahead to the right at this level. */
1458 xfs_btree_readahead(cur, level, XFS_BTCUR_RIGHTRA);
1460 /* Get a pointer to the btree block. */
1461 block = xfs_btree_get_block(cur, level, &bp);
1464 error = xfs_btree_check_block(cur, block, level, bp);
1469 /* We're done if we remain in the block after the increment. */
1470 if (++cur->bc_ptrs[level] <= xfs_btree_get_numrecs(block))
1473 /* Fail if we just went off the right edge of the tree. */
1474 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_RIGHTSIB);
1475 if (xfs_btree_ptr_is_null(cur, &ptr))
1478 XFS_BTREE_STATS_INC(cur, increment);
1481 * March up the tree incrementing pointers.
1482 * Stop when we don't go off the right edge of a block.
1484 for (lev = level + 1; lev < cur->bc_nlevels; lev++) {
1485 block = xfs_btree_get_block(cur, lev, &bp);
1488 error = xfs_btree_check_block(cur, block, lev, bp);
1493 if (++cur->bc_ptrs[lev] <= xfs_btree_get_numrecs(block))
1496 /* Read-ahead the right block for the next loop. */
1497 xfs_btree_readahead(cur, lev, XFS_BTCUR_RIGHTRA);
1501 * If we went off the root then we are either seriously
1502 * confused or have the tree root in an inode.
1504 if (lev == cur->bc_nlevels) {
1505 if (cur->bc_flags & XFS_BTREE_ROOT_IN_INODE)
1508 error = EFSCORRUPTED;
1511 ASSERT(lev < cur->bc_nlevels);
1514 * Now walk back down the tree, fixing up the cursor's buffer
1515 * pointers and key numbers.
1517 for (block = xfs_btree_get_block(cur, lev, &bp); lev > level; ) {
1518 union xfs_btree_ptr *ptrp;
1520 ptrp = xfs_btree_ptr_addr(cur, cur->bc_ptrs[lev], block);
1522 error = xfs_btree_read_buf_block(cur, ptrp, 0, &block, &bp);
1526 xfs_btree_setbuf(cur, lev, bp);
1527 cur->bc_ptrs[lev] = 1;
1530 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1535 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1540 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
1545 * Decrement cursor by one record at the level.
1546 * For nonzero levels the leaf-ward information is untouched.
1549 xfs_btree_decrement(
1550 struct xfs_btree_cur *cur,
1552 int *stat) /* success/failure */
1554 struct xfs_btree_block *block;
1556 int error; /* error return value */
1558 union xfs_btree_ptr ptr;
1560 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1561 XFS_BTREE_TRACE_ARGI(cur, level);
1563 ASSERT(level < cur->bc_nlevels);
1565 /* Read-ahead to the left at this level. */
1566 xfs_btree_readahead(cur, level, XFS_BTCUR_LEFTRA);
1568 /* We're done if we remain in the block after the decrement. */
1569 if (--cur->bc_ptrs[level] > 0)
1572 /* Get a pointer to the btree block. */
1573 block = xfs_btree_get_block(cur, level, &bp);
1576 error = xfs_btree_check_block(cur, block, level, bp);
1581 /* Fail if we just went off the left edge of the tree. */
1582 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_LEFTSIB);
1583 if (xfs_btree_ptr_is_null(cur, &ptr))
1586 XFS_BTREE_STATS_INC(cur, decrement);
1589 * March up the tree decrementing pointers.
1590 * Stop when we don't go off the left edge of a block.
1592 for (lev = level + 1; lev < cur->bc_nlevels; lev++) {
1593 if (--cur->bc_ptrs[lev] > 0)
1595 /* Read-ahead the left block for the next loop. */
1596 xfs_btree_readahead(cur, lev, XFS_BTCUR_LEFTRA);
1600 * If we went off the root then we are seriously confused.
1601 * or the root of the tree is in an inode.
1603 if (lev == cur->bc_nlevels) {
1604 if (cur->bc_flags & XFS_BTREE_ROOT_IN_INODE)
1607 error = EFSCORRUPTED;
1610 ASSERT(lev < cur->bc_nlevels);
1613 * Now walk back down the tree, fixing up the cursor's buffer
1614 * pointers and key numbers.
1616 for (block = xfs_btree_get_block(cur, lev, &bp); lev > level; ) {
1617 union xfs_btree_ptr *ptrp;
1619 ptrp = xfs_btree_ptr_addr(cur, cur->bc_ptrs[lev], block);
1621 error = xfs_btree_read_buf_block(cur, ptrp, 0, &block, &bp);
1624 xfs_btree_setbuf(cur, lev, bp);
1625 cur->bc_ptrs[lev] = xfs_btree_get_numrecs(block);
1628 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1633 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1638 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
1643 xfs_btree_lookup_get_block(
1644 struct xfs_btree_cur *cur, /* btree cursor */
1645 int level, /* level in the btree */
1646 union xfs_btree_ptr *pp, /* ptr to btree block */
1647 struct xfs_btree_block **blkp) /* return btree block */
1649 struct xfs_buf *bp; /* buffer pointer for btree block */
1652 /* special case the root block if in an inode */
1653 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
1654 (level == cur->bc_nlevels - 1)) {
1655 *blkp = xfs_btree_get_iroot(cur);
1660 * If the old buffer at this level for the disk address we are
1661 * looking for re-use it.
1663 * Otherwise throw it away and get a new one.
1665 bp = cur->bc_bufs[level];
1666 if (bp && XFS_BUF_ADDR(bp) == xfs_btree_ptr_to_daddr(cur, pp)) {
1667 *blkp = XFS_BUF_TO_BLOCK(bp);
1671 error = xfs_btree_read_buf_block(cur, pp, 0, blkp, &bp);
1675 xfs_btree_setbuf(cur, level, bp);
1680 * Get current search key. For level 0 we don't actually have a key
1681 * structure so we make one up from the record. For all other levels
1682 * we just return the right key.
1684 STATIC union xfs_btree_key *
1685 xfs_lookup_get_search_key(
1686 struct xfs_btree_cur *cur,
1689 struct xfs_btree_block *block,
1690 union xfs_btree_key *kp)
1693 cur->bc_ops->init_key_from_rec(kp,
1694 xfs_btree_rec_addr(cur, keyno, block));
1698 return xfs_btree_key_addr(cur, keyno, block);
1702 * Lookup the record. The cursor is made to point to it, based on dir.
1703 * stat is set to 0 if can't find any such record, 1 for success.
1707 struct xfs_btree_cur *cur, /* btree cursor */
1708 xfs_lookup_t dir, /* <=, ==, or >= */
1709 int *stat) /* success/failure */
1711 struct xfs_btree_block *block; /* current btree block */
1712 __int64_t diff; /* difference for the current key */
1713 int error; /* error return value */
1714 int keyno; /* current key number */
1715 int level; /* level in the btree */
1716 union xfs_btree_ptr *pp; /* ptr to btree block */
1717 union xfs_btree_ptr ptr; /* ptr to btree block */
1719 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1720 XFS_BTREE_TRACE_ARGI(cur, dir);
1722 XFS_BTREE_STATS_INC(cur, lookup);
1727 /* initialise start pointer from cursor */
1728 cur->bc_ops->init_ptr_from_cur(cur, &ptr);
1732 * Iterate over each level in the btree, starting at the root.
1733 * For each level above the leaves, find the key we need, based
1734 * on the lookup record, then follow the corresponding block
1735 * pointer down to the next level.
1737 for (level = cur->bc_nlevels - 1, diff = 1; level >= 0; level--) {
1738 /* Get the block we need to do the lookup on. */
1739 error = xfs_btree_lookup_get_block(cur, level, pp, &block);
1745 * If we already had a key match at a higher level, we
1746 * know we need to use the first entry in this block.
1750 /* Otherwise search this block. Do a binary search. */
1752 int high; /* high entry number */
1753 int low; /* low entry number */
1755 /* Set low and high entry numbers, 1-based. */
1757 high = xfs_btree_get_numrecs(block);
1759 /* Block is empty, must be an empty leaf. */
1760 ASSERT(level == 0 && cur->bc_nlevels == 1);
1762 cur->bc_ptrs[0] = dir != XFS_LOOKUP_LE;
1763 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1768 /* Binary search the block. */
1769 while (low <= high) {
1770 union xfs_btree_key key;
1771 union xfs_btree_key *kp;
1773 XFS_BTREE_STATS_INC(cur, compare);
1775 /* keyno is average of low and high. */
1776 keyno = (low + high) >> 1;
1778 /* Get current search key */
1779 kp = xfs_lookup_get_search_key(cur, level,
1780 keyno, block, &key);
1783 * Compute difference to get next direction:
1784 * - less than, move right
1785 * - greater than, move left
1786 * - equal, we're done
1788 diff = cur->bc_ops->key_diff(cur, kp);
1799 * If there are more levels, set up for the next level
1800 * by getting the block number and filling in the cursor.
1804 * If we moved left, need the previous key number,
1805 * unless there isn't one.
1807 if (diff > 0 && --keyno < 1)
1809 pp = xfs_btree_ptr_addr(cur, keyno, block);
1812 error = xfs_btree_check_ptr(cur, pp, 0, level);
1816 cur->bc_ptrs[level] = keyno;
1820 /* Done with the search. See if we need to adjust the results. */
1821 if (dir != XFS_LOOKUP_LE && diff < 0) {
1824 * If ge search and we went off the end of the block, but it's
1825 * not the last block, we're in the wrong block.
1827 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_RIGHTSIB);
1828 if (dir == XFS_LOOKUP_GE &&
1829 keyno > xfs_btree_get_numrecs(block) &&
1830 !xfs_btree_ptr_is_null(cur, &ptr)) {
1833 cur->bc_ptrs[0] = keyno;
1834 error = xfs_btree_increment(cur, 0, &i);
1837 XFS_WANT_CORRUPTED_RETURN(i == 1);
1838 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1842 } else if (dir == XFS_LOOKUP_LE && diff > 0)
1844 cur->bc_ptrs[0] = keyno;
1846 /* Return if we succeeded or not. */
1847 if (keyno == 0 || keyno > xfs_btree_get_numrecs(block))
1849 else if (dir != XFS_LOOKUP_EQ || diff == 0)
1853 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1857 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
1862 * Update keys at all levels from here to the root along the cursor's path.
1866 struct xfs_btree_cur *cur,
1867 union xfs_btree_key *keyp,
1870 struct xfs_btree_block *block;
1872 union xfs_btree_key *kp;
1875 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1876 XFS_BTREE_TRACE_ARGIK(cur, level, keyp);
1878 ASSERT(!(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) || level >= 1);
1881 * Go up the tree from this level toward the root.
1882 * At each level, update the key value to the value input.
1883 * Stop when we reach a level where the cursor isn't pointing
1884 * at the first entry in the block.
1886 for (ptr = 1; ptr == 1 && level < cur->bc_nlevels; level++) {
1890 block = xfs_btree_get_block(cur, level, &bp);
1892 error = xfs_btree_check_block(cur, block, level, bp);
1894 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
1898 ptr = cur->bc_ptrs[level];
1899 kp = xfs_btree_key_addr(cur, ptr, block);
1900 xfs_btree_copy_keys(cur, kp, keyp, 1);
1901 xfs_btree_log_keys(cur, bp, ptr, ptr);
1904 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1909 * Update the record referred to by cur to the value in the
1910 * given record. This either works (return 0) or gets an
1911 * EFSCORRUPTED error.
1915 struct xfs_btree_cur *cur,
1916 union xfs_btree_rec *rec)
1918 struct xfs_btree_block *block;
1922 union xfs_btree_rec *rp;
1924 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1925 XFS_BTREE_TRACE_ARGR(cur, rec);
1927 /* Pick up the current block. */
1928 block = xfs_btree_get_block(cur, 0, &bp);
1931 error = xfs_btree_check_block(cur, block, 0, bp);
1935 /* Get the address of the rec to be updated. */
1936 ptr = cur->bc_ptrs[0];
1937 rp = xfs_btree_rec_addr(cur, ptr, block);
1939 /* Fill in the new contents and log them. */
1940 xfs_btree_copy_recs(cur, rp, rec, 1);
1941 xfs_btree_log_recs(cur, bp, ptr, ptr);
1944 * If we are tracking the last record in the tree and
1945 * we are at the far right edge of the tree, update it.
1947 if (xfs_btree_is_lastrec(cur, block, 0)) {
1948 cur->bc_ops->update_lastrec(cur, block, rec,
1949 ptr, LASTREC_UPDATE);
1952 /* Updating first rec in leaf. Pass new key value up to our parent. */
1954 union xfs_btree_key key;
1956 cur->bc_ops->init_key_from_rec(&key, rec);
1957 error = xfs_btree_updkey(cur, &key, 1);
1962 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1966 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
1971 * Move 1 record left from cur/level if possible.
1972 * Update cur to reflect the new path.
1974 STATIC int /* error */
1976 struct xfs_btree_cur *cur,
1978 int *stat) /* success/failure */
1980 union xfs_btree_key key; /* btree key */
1981 struct xfs_buf *lbp; /* left buffer pointer */
1982 struct xfs_btree_block *left; /* left btree block */
1983 int lrecs; /* left record count */
1984 struct xfs_buf *rbp; /* right buffer pointer */
1985 struct xfs_btree_block *right; /* right btree block */
1986 int rrecs; /* right record count */
1987 union xfs_btree_ptr lptr; /* left btree pointer */
1988 union xfs_btree_key *rkp = NULL; /* right btree key */
1989 union xfs_btree_ptr *rpp = NULL; /* right address pointer */
1990 union xfs_btree_rec *rrp = NULL; /* right record pointer */
1991 int error; /* error return value */
1993 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1994 XFS_BTREE_TRACE_ARGI(cur, level);
1996 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
1997 level == cur->bc_nlevels - 1)
2000 /* Set up variables for this block as "right". */
2001 right = xfs_btree_get_block(cur, level, &rbp);
2004 error = xfs_btree_check_block(cur, right, level, rbp);
2009 /* If we've got no left sibling then we can't shift an entry left. */
2010 xfs_btree_get_sibling(cur, right, &lptr, XFS_BB_LEFTSIB);
2011 if (xfs_btree_ptr_is_null(cur, &lptr))
2015 * If the cursor entry is the one that would be moved, don't
2016 * do it... it's too complicated.
2018 if (cur->bc_ptrs[level] <= 1)
2021 /* Set up the left neighbor as "left". */
2022 error = xfs_btree_read_buf_block(cur, &lptr, 0, &left, &lbp);
2026 /* If it's full, it can't take another entry. */
2027 lrecs = xfs_btree_get_numrecs(left);
2028 if (lrecs == cur->bc_ops->get_maxrecs(cur, level))
2031 rrecs = xfs_btree_get_numrecs(right);
2034 * We add one entry to the left side and remove one for the right side.
2035 * Account for it here, the changes will be updated on disk and logged
2041 XFS_BTREE_STATS_INC(cur, lshift);
2042 XFS_BTREE_STATS_ADD(cur, moves, 1);
2045 * If non-leaf, copy a key and a ptr to the left block.
2046 * Log the changes to the left block.
2049 /* It's a non-leaf. Move keys and pointers. */
2050 union xfs_btree_key *lkp; /* left btree key */
2051 union xfs_btree_ptr *lpp; /* left address pointer */
2053 lkp = xfs_btree_key_addr(cur, lrecs, left);
2054 rkp = xfs_btree_key_addr(cur, 1, right);
2056 lpp = xfs_btree_ptr_addr(cur, lrecs, left);
2057 rpp = xfs_btree_ptr_addr(cur, 1, right);
2059 error = xfs_btree_check_ptr(cur, rpp, 0, level);
2063 xfs_btree_copy_keys(cur, lkp, rkp, 1);
2064 xfs_btree_copy_ptrs(cur, lpp, rpp, 1);
2066 xfs_btree_log_keys(cur, lbp, lrecs, lrecs);
2067 xfs_btree_log_ptrs(cur, lbp, lrecs, lrecs);
2069 ASSERT(cur->bc_ops->keys_inorder(cur,
2070 xfs_btree_key_addr(cur, lrecs - 1, left), lkp));
2072 /* It's a leaf. Move records. */
2073 union xfs_btree_rec *lrp; /* left record pointer */
2075 lrp = xfs_btree_rec_addr(cur, lrecs, left);
2076 rrp = xfs_btree_rec_addr(cur, 1, right);
2078 xfs_btree_copy_recs(cur, lrp, rrp, 1);
2079 xfs_btree_log_recs(cur, lbp, lrecs, lrecs);
2081 ASSERT(cur->bc_ops->recs_inorder(cur,
2082 xfs_btree_rec_addr(cur, lrecs - 1, left), lrp));
2085 xfs_btree_set_numrecs(left, lrecs);
2086 xfs_btree_log_block(cur, lbp, XFS_BB_NUMRECS);
2088 xfs_btree_set_numrecs(right, rrecs);
2089 xfs_btree_log_block(cur, rbp, XFS_BB_NUMRECS);
2092 * Slide the contents of right down one entry.
2094 XFS_BTREE_STATS_ADD(cur, moves, rrecs - 1);
2096 /* It's a nonleaf. operate on keys and ptrs */
2098 int i; /* loop index */
2100 for (i = 0; i < rrecs; i++) {
2101 error = xfs_btree_check_ptr(cur, rpp, i + 1, level);
2106 xfs_btree_shift_keys(cur,
2107 xfs_btree_key_addr(cur, 2, right),
2109 xfs_btree_shift_ptrs(cur,
2110 xfs_btree_ptr_addr(cur, 2, right),
2113 xfs_btree_log_keys(cur, rbp, 1, rrecs);
2114 xfs_btree_log_ptrs(cur, rbp, 1, rrecs);
2116 /* It's a leaf. operate on records */
2117 xfs_btree_shift_recs(cur,
2118 xfs_btree_rec_addr(cur, 2, right),
2120 xfs_btree_log_recs(cur, rbp, 1, rrecs);
2123 * If it's the first record in the block, we'll need a key
2124 * structure to pass up to the next level (updkey).
2126 cur->bc_ops->init_key_from_rec(&key,
2127 xfs_btree_rec_addr(cur, 1, right));
2131 /* Update the parent key values of right. */
2132 error = xfs_btree_updkey(cur, rkp, level + 1);
2136 /* Slide the cursor value left one. */
2137 cur->bc_ptrs[level]--;
2139 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2144 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2149 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
2154 * Move 1 record right from cur/level if possible.
2155 * Update cur to reflect the new path.
2157 STATIC int /* error */
2159 struct xfs_btree_cur *cur,
2161 int *stat) /* success/failure */
2163 union xfs_btree_key key; /* btree key */
2164 struct xfs_buf *lbp; /* left buffer pointer */
2165 struct xfs_btree_block *left; /* left btree block */
2166 struct xfs_buf *rbp; /* right buffer pointer */
2167 struct xfs_btree_block *right; /* right btree block */
2168 struct xfs_btree_cur *tcur; /* temporary btree cursor */
2169 union xfs_btree_ptr rptr; /* right block pointer */
2170 union xfs_btree_key *rkp; /* right btree key */
2171 int rrecs; /* right record count */
2172 int lrecs; /* left record count */
2173 int error; /* error return value */
2174 int i; /* loop counter */
2176 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
2177 XFS_BTREE_TRACE_ARGI(cur, level);
2179 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
2180 (level == cur->bc_nlevels - 1))
2183 /* Set up variables for this block as "left". */
2184 left = xfs_btree_get_block(cur, level, &lbp);
2187 error = xfs_btree_check_block(cur, left, level, lbp);
2192 /* If we've got no right sibling then we can't shift an entry right. */
2193 xfs_btree_get_sibling(cur, left, &rptr, XFS_BB_RIGHTSIB);
2194 if (xfs_btree_ptr_is_null(cur, &rptr))
2198 * If the cursor entry is the one that would be moved, don't
2199 * do it... it's too complicated.
2201 lrecs = xfs_btree_get_numrecs(left);
2202 if (cur->bc_ptrs[level] >= lrecs)
2205 /* Set up the right neighbor as "right". */
2206 error = xfs_btree_read_buf_block(cur, &rptr, 0, &right, &rbp);
2210 /* If it's full, it can't take another entry. */
2211 rrecs = xfs_btree_get_numrecs(right);
2212 if (rrecs == cur->bc_ops->get_maxrecs(cur, level))
2215 XFS_BTREE_STATS_INC(cur, rshift);
2216 XFS_BTREE_STATS_ADD(cur, moves, rrecs);
2219 * Make a hole at the start of the right neighbor block, then
2220 * copy the last left block entry to the hole.
2223 /* It's a nonleaf. make a hole in the keys and ptrs */
2224 union xfs_btree_key *lkp;
2225 union xfs_btree_ptr *lpp;
2226 union xfs_btree_ptr *rpp;
2228 lkp = xfs_btree_key_addr(cur, lrecs, left);
2229 lpp = xfs_btree_ptr_addr(cur, lrecs, left);
2230 rkp = xfs_btree_key_addr(cur, 1, right);
2231 rpp = xfs_btree_ptr_addr(cur, 1, right);
2234 for (i = rrecs - 1; i >= 0; i--) {
2235 error = xfs_btree_check_ptr(cur, rpp, i, level);
2241 xfs_btree_shift_keys(cur, rkp, 1, rrecs);
2242 xfs_btree_shift_ptrs(cur, rpp, 1, rrecs);
2245 error = xfs_btree_check_ptr(cur, lpp, 0, level);
2250 /* Now put the new data in, and log it. */
2251 xfs_btree_copy_keys(cur, rkp, lkp, 1);
2252 xfs_btree_copy_ptrs(cur, rpp, lpp, 1);
2254 xfs_btree_log_keys(cur, rbp, 1, rrecs + 1);
2255 xfs_btree_log_ptrs(cur, rbp, 1, rrecs + 1);
2257 ASSERT(cur->bc_ops->keys_inorder(cur, rkp,
2258 xfs_btree_key_addr(cur, 2, right)));
2260 /* It's a leaf. make a hole in the records */
2261 union xfs_btree_rec *lrp;
2262 union xfs_btree_rec *rrp;
2264 lrp = xfs_btree_rec_addr(cur, lrecs, left);
2265 rrp = xfs_btree_rec_addr(cur, 1, right);
2267 xfs_btree_shift_recs(cur, rrp, 1, rrecs);
2269 /* Now put the new data in, and log it. */
2270 xfs_btree_copy_recs(cur, rrp, lrp, 1);
2271 xfs_btree_log_recs(cur, rbp, 1, rrecs + 1);
2273 cur->bc_ops->init_key_from_rec(&key, rrp);
2276 ASSERT(cur->bc_ops->recs_inorder(cur, rrp,
2277 xfs_btree_rec_addr(cur, 2, right)));
2281 * Decrement and log left's numrecs, bump and log right's numrecs.
2283 xfs_btree_set_numrecs(left, --lrecs);
2284 xfs_btree_log_block(cur, lbp, XFS_BB_NUMRECS);
2286 xfs_btree_set_numrecs(right, ++rrecs);
2287 xfs_btree_log_block(cur, rbp, XFS_BB_NUMRECS);
2290 * Using a temporary cursor, update the parent key values of the
2291 * block on the right.
2293 error = xfs_btree_dup_cursor(cur, &tcur);
2296 i = xfs_btree_lastrec(tcur, level);
2297 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
2299 error = xfs_btree_increment(tcur, level, &i);
2303 error = xfs_btree_updkey(tcur, rkp, level + 1);
2307 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
2309 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2314 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2319 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
2323 XFS_BTREE_TRACE_CURSOR(tcur, XBT_ERROR);
2324 xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
2329 * Split cur/level block in half.
2330 * Return new block number and the key to its first
2331 * record (to be inserted into parent).
2333 STATIC int /* error */
2335 struct xfs_btree_cur *cur,
2337 union xfs_btree_ptr *ptrp,
2338 union xfs_btree_key *key,
2339 struct xfs_btree_cur **curp,
2340 int *stat) /* success/failure */
2342 union xfs_btree_ptr lptr; /* left sibling block ptr */
2343 struct xfs_buf *lbp; /* left buffer pointer */
2344 struct xfs_btree_block *left; /* left btree block */
2345 union xfs_btree_ptr rptr; /* right sibling block ptr */
2346 struct xfs_buf *rbp; /* right buffer pointer */
2347 struct xfs_btree_block *right; /* right btree block */
2348 union xfs_btree_ptr rrptr; /* right-right sibling ptr */
2349 struct xfs_buf *rrbp; /* right-right buffer pointer */
2350 struct xfs_btree_block *rrblock; /* right-right btree block */
2354 int error; /* error return value */
2359 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
2360 XFS_BTREE_TRACE_ARGIPK(cur, level, *ptrp, key);
2362 XFS_BTREE_STATS_INC(cur, split);
2364 /* Set up left block (current one). */
2365 left = xfs_btree_get_block(cur, level, &lbp);
2368 error = xfs_btree_check_block(cur, left, level, lbp);
2373 xfs_btree_buf_to_ptr(cur, lbp, &lptr);
2375 /* Allocate the new block. If we can't do it, we're toast. Give up. */
2376 error = cur->bc_ops->alloc_block(cur, &lptr, &rptr, stat);
2381 XFS_BTREE_STATS_INC(cur, alloc);
2383 /* Set up the new block as "right". */
2384 error = xfs_btree_get_buf_block(cur, &rptr, 0, &right, &rbp);
2388 /* Fill in the btree header for the new right block. */
2389 xfs_btree_init_block_cur(cur, rbp, xfs_btree_get_level(left), 0);
2392 * Split the entries between the old and the new block evenly.
2393 * Make sure that if there's an odd number of entries now, that
2394 * each new block will have the same number of entries.
2396 lrecs = xfs_btree_get_numrecs(left);
2398 if ((lrecs & 1) && cur->bc_ptrs[level] <= rrecs + 1)
2400 src_index = (lrecs - rrecs + 1);
2402 XFS_BTREE_STATS_ADD(cur, moves, rrecs);
2405 * Copy btree block entries from the left block over to the
2406 * new block, the right. Update the right block and log the
2410 /* It's a non-leaf. Move keys and pointers. */
2411 union xfs_btree_key *lkp; /* left btree key */
2412 union xfs_btree_ptr *lpp; /* left address pointer */
2413 union xfs_btree_key *rkp; /* right btree key */
2414 union xfs_btree_ptr *rpp; /* right address pointer */
2416 lkp = xfs_btree_key_addr(cur, src_index, left);
2417 lpp = xfs_btree_ptr_addr(cur, src_index, left);
2418 rkp = xfs_btree_key_addr(cur, 1, right);
2419 rpp = xfs_btree_ptr_addr(cur, 1, right);
2422 for (i = src_index; i < rrecs; i++) {
2423 error = xfs_btree_check_ptr(cur, lpp, i, level);
2429 xfs_btree_copy_keys(cur, rkp, lkp, rrecs);
2430 xfs_btree_copy_ptrs(cur, rpp, lpp, rrecs);
2432 xfs_btree_log_keys(cur, rbp, 1, rrecs);
2433 xfs_btree_log_ptrs(cur, rbp, 1, rrecs);
2435 /* Grab the keys to the entries moved to the right block */
2436 xfs_btree_copy_keys(cur, key, rkp, 1);
2438 /* It's a leaf. Move records. */
2439 union xfs_btree_rec *lrp; /* left record pointer */
2440 union xfs_btree_rec *rrp; /* right record pointer */
2442 lrp = xfs_btree_rec_addr(cur, src_index, left);
2443 rrp = xfs_btree_rec_addr(cur, 1, right);
2445 xfs_btree_copy_recs(cur, rrp, lrp, rrecs);
2446 xfs_btree_log_recs(cur, rbp, 1, rrecs);
2448 cur->bc_ops->init_key_from_rec(key,
2449 xfs_btree_rec_addr(cur, 1, right));
2454 * Find the left block number by looking in the buffer.
2455 * Adjust numrecs, sibling pointers.
2457 xfs_btree_get_sibling(cur, left, &rrptr, XFS_BB_RIGHTSIB);
2458 xfs_btree_set_sibling(cur, right, &rrptr, XFS_BB_RIGHTSIB);
2459 xfs_btree_set_sibling(cur, right, &lptr, XFS_BB_LEFTSIB);
2460 xfs_btree_set_sibling(cur, left, &rptr, XFS_BB_RIGHTSIB);
2463 xfs_btree_set_numrecs(left, lrecs);
2464 xfs_btree_set_numrecs(right, xfs_btree_get_numrecs(right) + rrecs);
2466 xfs_btree_log_block(cur, rbp, XFS_BB_ALL_BITS);
2467 xfs_btree_log_block(cur, lbp, XFS_BB_NUMRECS | XFS_BB_RIGHTSIB);
2470 * If there's a block to the new block's right, make that block
2471 * point back to right instead of to left.
2473 if (!xfs_btree_ptr_is_null(cur, &rrptr)) {
2474 error = xfs_btree_read_buf_block(cur, &rrptr,
2475 0, &rrblock, &rrbp);
2478 xfs_btree_set_sibling(cur, rrblock, &rptr, XFS_BB_LEFTSIB);
2479 xfs_btree_log_block(cur, rrbp, XFS_BB_LEFTSIB);
2482 * If the cursor is really in the right block, move it there.
2483 * If it's just pointing past the last entry in left, then we'll
2484 * insert there, so don't change anything in that case.
2486 if (cur->bc_ptrs[level] > lrecs + 1) {
2487 xfs_btree_setbuf(cur, level, rbp);
2488 cur->bc_ptrs[level] -= lrecs;
2491 * If there are more levels, we'll need another cursor which refers
2492 * the right block, no matter where this cursor was.
2494 if (level + 1 < cur->bc_nlevels) {
2495 error = xfs_btree_dup_cursor(cur, curp);
2498 (*curp)->bc_ptrs[level + 1]++;
2501 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2505 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2510 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
2515 * Copy the old inode root contents into a real block and make the
2516 * broot point to it.
2519 xfs_btree_new_iroot(
2520 struct xfs_btree_cur *cur, /* btree cursor */
2521 int *logflags, /* logging flags for inode */
2522 int *stat) /* return status - 0 fail */
2524 struct xfs_buf *cbp; /* buffer for cblock */
2525 struct xfs_btree_block *block; /* btree block */
2526 struct xfs_btree_block *cblock; /* child btree block */
2527 union xfs_btree_key *ckp; /* child key pointer */
2528 union xfs_btree_ptr *cpp; /* child ptr pointer */
2529 union xfs_btree_key *kp; /* pointer to btree key */
2530 union xfs_btree_ptr *pp; /* pointer to block addr */
2531 union xfs_btree_ptr nptr; /* new block addr */
2532 int level; /* btree level */
2533 int error; /* error return code */
2535 int i; /* loop counter */
2538 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
2539 XFS_BTREE_STATS_INC(cur, newroot);
2541 ASSERT(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE);
2543 level = cur->bc_nlevels - 1;
2545 block = xfs_btree_get_iroot(cur);
2546 pp = xfs_btree_ptr_addr(cur, 1, block);
2548 /* Allocate the new block. If we can't do it, we're toast. Give up. */
2549 error = cur->bc_ops->alloc_block(cur, pp, &nptr, stat);
2553 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2556 XFS_BTREE_STATS_INC(cur, alloc);
2558 /* Copy the root into a real block. */
2559 error = xfs_btree_get_buf_block(cur, &nptr, 0, &cblock, &cbp);
2564 * we can't just memcpy() the root in for CRC enabled btree blocks.
2565 * In that case have to also ensure the blkno remains correct
2567 memcpy(cblock, block, xfs_btree_block_len(cur));
2568 if (cur->bc_flags & XFS_BTREE_CRC_BLOCKS) {
2569 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
2570 cblock->bb_u.l.bb_blkno = cpu_to_be64(cbp->b_bn);
2572 cblock->bb_u.s.bb_blkno = cpu_to_be64(cbp->b_bn);
2575 be16_add_cpu(&block->bb_level, 1);
2576 xfs_btree_set_numrecs(block, 1);
2578 cur->bc_ptrs[level + 1] = 1;
2580 kp = xfs_btree_key_addr(cur, 1, block);
2581 ckp = xfs_btree_key_addr(cur, 1, cblock);
2582 xfs_btree_copy_keys(cur, ckp, kp, xfs_btree_get_numrecs(cblock));
2584 cpp = xfs_btree_ptr_addr(cur, 1, cblock);
2586 for (i = 0; i < be16_to_cpu(cblock->bb_numrecs); i++) {
2587 error = xfs_btree_check_ptr(cur, pp, i, level);
2592 xfs_btree_copy_ptrs(cur, cpp, pp, xfs_btree_get_numrecs(cblock));
2595 error = xfs_btree_check_ptr(cur, &nptr, 0, level);
2599 xfs_btree_copy_ptrs(cur, pp, &nptr, 1);
2601 xfs_iroot_realloc(cur->bc_private.b.ip,
2602 1 - xfs_btree_get_numrecs(cblock),
2603 cur->bc_private.b.whichfork);
2605 xfs_btree_setbuf(cur, level, cbp);
2608 * Do all this logging at the end so that
2609 * the root is at the right level.
2611 xfs_btree_log_block(cur, cbp, XFS_BB_ALL_BITS);
2612 xfs_btree_log_keys(cur, cbp, 1, be16_to_cpu(cblock->bb_numrecs));
2613 xfs_btree_log_ptrs(cur, cbp, 1, be16_to_cpu(cblock->bb_numrecs));
2616 XFS_ILOG_CORE | xfs_ilog_fbroot(cur->bc_private.b.whichfork);
2618 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2621 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
2626 * Allocate a new root block, fill it in.
2628 STATIC int /* error */
2630 struct xfs_btree_cur *cur, /* btree cursor */
2631 int *stat) /* success/failure */
2633 struct xfs_btree_block *block; /* one half of the old root block */
2634 struct xfs_buf *bp; /* buffer containing block */
2635 int error; /* error return value */
2636 struct xfs_buf *lbp; /* left buffer pointer */
2637 struct xfs_btree_block *left; /* left btree block */
2638 struct xfs_buf *nbp; /* new (root) buffer */
2639 struct xfs_btree_block *new; /* new (root) btree block */
2640 int nptr; /* new value for key index, 1 or 2 */
2641 struct xfs_buf *rbp; /* right buffer pointer */
2642 struct xfs_btree_block *right; /* right btree block */
2643 union xfs_btree_ptr rptr;
2644 union xfs_btree_ptr lptr;
2646 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
2647 XFS_BTREE_STATS_INC(cur, newroot);
2649 /* initialise our start point from the cursor */
2650 cur->bc_ops->init_ptr_from_cur(cur, &rptr);
2652 /* Allocate the new block. If we can't do it, we're toast. Give up. */
2653 error = cur->bc_ops->alloc_block(cur, &rptr, &lptr, stat);
2658 XFS_BTREE_STATS_INC(cur, alloc);
2660 /* Set up the new block. */
2661 error = xfs_btree_get_buf_block(cur, &lptr, 0, &new, &nbp);
2665 /* Set the root in the holding structure increasing the level by 1. */
2666 cur->bc_ops->set_root(cur, &lptr, 1);
2669 * At the previous root level there are now two blocks: the old root,
2670 * and the new block generated when it was split. We don't know which
2671 * one the cursor is pointing at, so we set up variables "left" and
2672 * "right" for each case.
2674 block = xfs_btree_get_block(cur, cur->bc_nlevels - 1, &bp);
2677 error = xfs_btree_check_block(cur, block, cur->bc_nlevels - 1, bp);
2682 xfs_btree_get_sibling(cur, block, &rptr, XFS_BB_RIGHTSIB);
2683 if (!xfs_btree_ptr_is_null(cur, &rptr)) {
2684 /* Our block is left, pick up the right block. */
2686 xfs_btree_buf_to_ptr(cur, lbp, &lptr);
2688 error = xfs_btree_read_buf_block(cur, &rptr, 0, &right, &rbp);
2694 /* Our block is right, pick up the left block. */
2696 xfs_btree_buf_to_ptr(cur, rbp, &rptr);
2698 xfs_btree_get_sibling(cur, right, &lptr, XFS_BB_LEFTSIB);
2699 error = xfs_btree_read_buf_block(cur, &lptr, 0, &left, &lbp);
2705 /* Fill in the new block's btree header and log it. */
2706 xfs_btree_init_block_cur(cur, nbp, cur->bc_nlevels, 2);
2707 xfs_btree_log_block(cur, nbp, XFS_BB_ALL_BITS);
2708 ASSERT(!xfs_btree_ptr_is_null(cur, &lptr) &&
2709 !xfs_btree_ptr_is_null(cur, &rptr));
2711 /* Fill in the key data in the new root. */
2712 if (xfs_btree_get_level(left) > 0) {
2713 xfs_btree_copy_keys(cur,
2714 xfs_btree_key_addr(cur, 1, new),
2715 xfs_btree_key_addr(cur, 1, left), 1);
2716 xfs_btree_copy_keys(cur,
2717 xfs_btree_key_addr(cur, 2, new),
2718 xfs_btree_key_addr(cur, 1, right), 1);
2720 cur->bc_ops->init_key_from_rec(
2721 xfs_btree_key_addr(cur, 1, new),
2722 xfs_btree_rec_addr(cur, 1, left));
2723 cur->bc_ops->init_key_from_rec(
2724 xfs_btree_key_addr(cur, 2, new),
2725 xfs_btree_rec_addr(cur, 1, right));
2727 xfs_btree_log_keys(cur, nbp, 1, 2);
2729 /* Fill in the pointer data in the new root. */
2730 xfs_btree_copy_ptrs(cur,
2731 xfs_btree_ptr_addr(cur, 1, new), &lptr, 1);
2732 xfs_btree_copy_ptrs(cur,
2733 xfs_btree_ptr_addr(cur, 2, new), &rptr, 1);
2734 xfs_btree_log_ptrs(cur, nbp, 1, 2);
2736 /* Fix up the cursor. */
2737 xfs_btree_setbuf(cur, cur->bc_nlevels, nbp);
2738 cur->bc_ptrs[cur->bc_nlevels] = nptr;
2740 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2744 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
2747 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2753 xfs_btree_make_block_unfull(
2754 struct xfs_btree_cur *cur, /* btree cursor */
2755 int level, /* btree level */
2756 int numrecs,/* # of recs in block */
2757 int *oindex,/* old tree index */
2758 int *index, /* new tree index */
2759 union xfs_btree_ptr *nptr, /* new btree ptr */
2760 struct xfs_btree_cur **ncur, /* new btree cursor */
2761 union xfs_btree_rec *nrec, /* new record */
2764 union xfs_btree_key key; /* new btree key value */
2767 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
2768 level == cur->bc_nlevels - 1) {
2769 struct xfs_inode *ip = cur->bc_private.b.ip;
2771 if (numrecs < cur->bc_ops->get_dmaxrecs(cur, level)) {
2772 /* A root block that can be made bigger. */
2773 xfs_iroot_realloc(ip, 1, cur->bc_private.b.whichfork);
2775 /* A root block that needs replacing */
2778 error = xfs_btree_new_iroot(cur, &logflags, stat);
2779 if (error || *stat == 0)
2782 xfs_trans_log_inode(cur->bc_tp, ip, logflags);
2788 /* First, try shifting an entry to the right neighbor. */
2789 error = xfs_btree_rshift(cur, level, stat);
2793 /* Next, try shifting an entry to the left neighbor. */
2794 error = xfs_btree_lshift(cur, level, stat);
2799 *oindex = *index = cur->bc_ptrs[level];
2804 * Next, try splitting the current block in half.
2806 * If this works we have to re-set our variables because we
2807 * could be in a different block now.
2809 error = xfs_btree_split(cur, level, nptr, &key, ncur, stat);
2810 if (error || *stat == 0)
2814 *index = cur->bc_ptrs[level];
2815 cur->bc_ops->init_rec_from_key(&key, nrec);
2820 * Insert one record/level. Return information to the caller
2821 * allowing the next level up to proceed if necessary.
2825 struct xfs_btree_cur *cur, /* btree cursor */
2826 int level, /* level to insert record at */
2827 union xfs_btree_ptr *ptrp, /* i/o: block number inserted */
2828 union xfs_btree_rec *recp, /* i/o: record data inserted */
2829 struct xfs_btree_cur **curp, /* output: new cursor replacing cur */
2830 int *stat) /* success/failure */
2832 struct xfs_btree_block *block; /* btree block */
2833 struct xfs_buf *bp; /* buffer for block */
2834 union xfs_btree_key key; /* btree key */
2835 union xfs_btree_ptr nptr; /* new block ptr */
2836 struct xfs_btree_cur *ncur; /* new btree cursor */
2837 union xfs_btree_rec nrec; /* new record count */
2838 int optr; /* old key/record index */
2839 int ptr; /* key/record index */
2840 int numrecs;/* number of records */
2841 int error; /* error return value */
2846 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
2847 XFS_BTREE_TRACE_ARGIPR(cur, level, *ptrp, recp);
2852 * If we have an external root pointer, and we've made it to the
2853 * root level, allocate a new root block and we're done.
2855 if (!(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
2856 (level >= cur->bc_nlevels)) {
2857 error = xfs_btree_new_root(cur, stat);
2858 xfs_btree_set_ptr_null(cur, ptrp);
2860 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2864 /* If we're off the left edge, return failure. */
2865 ptr = cur->bc_ptrs[level];
2867 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2872 /* Make a key out of the record data to be inserted, and save it. */
2873 cur->bc_ops->init_key_from_rec(&key, recp);
2877 XFS_BTREE_STATS_INC(cur, insrec);
2879 /* Get pointers to the btree buffer and block. */
2880 block = xfs_btree_get_block(cur, level, &bp);
2881 numrecs = xfs_btree_get_numrecs(block);
2884 error = xfs_btree_check_block(cur, block, level, bp);
2888 /* Check that the new entry is being inserted in the right place. */
2889 if (ptr <= numrecs) {
2891 ASSERT(cur->bc_ops->recs_inorder(cur, recp,
2892 xfs_btree_rec_addr(cur, ptr, block)));
2894 ASSERT(cur->bc_ops->keys_inorder(cur, &key,
2895 xfs_btree_key_addr(cur, ptr, block)));
2901 * If the block is full, we can't insert the new entry until we
2902 * make the block un-full.
2904 xfs_btree_set_ptr_null(cur, &nptr);
2905 if (numrecs == cur->bc_ops->get_maxrecs(cur, level)) {
2906 error = xfs_btree_make_block_unfull(cur, level, numrecs,
2907 &optr, &ptr, &nptr, &ncur, &nrec, stat);
2908 if (error || *stat == 0)
2913 * The current block may have changed if the block was
2914 * previously full and we have just made space in it.
2916 block = xfs_btree_get_block(cur, level, &bp);
2917 numrecs = xfs_btree_get_numrecs(block);
2920 error = xfs_btree_check_block(cur, block, level, bp);
2926 * At this point we know there's room for our new entry in the block
2927 * we're pointing at.
2929 XFS_BTREE_STATS_ADD(cur, moves, numrecs - ptr + 1);
2932 /* It's a nonleaf. make a hole in the keys and ptrs */
2933 union xfs_btree_key *kp;
2934 union xfs_btree_ptr *pp;
2936 kp = xfs_btree_key_addr(cur, ptr, block);
2937 pp = xfs_btree_ptr_addr(cur, ptr, block);
2940 for (i = numrecs - ptr; i >= 0; i--) {
2941 error = xfs_btree_check_ptr(cur, pp, i, level);
2947 xfs_btree_shift_keys(cur, kp, 1, numrecs - ptr + 1);
2948 xfs_btree_shift_ptrs(cur, pp, 1, numrecs - ptr + 1);
2951 error = xfs_btree_check_ptr(cur, ptrp, 0, level);
2956 /* Now put the new data in, bump numrecs and log it. */
2957 xfs_btree_copy_keys(cur, kp, &key, 1);
2958 xfs_btree_copy_ptrs(cur, pp, ptrp, 1);
2960 xfs_btree_set_numrecs(block, numrecs);
2961 xfs_btree_log_ptrs(cur, bp, ptr, numrecs);
2962 xfs_btree_log_keys(cur, bp, ptr, numrecs);
2964 if (ptr < numrecs) {
2965 ASSERT(cur->bc_ops->keys_inorder(cur, kp,
2966 xfs_btree_key_addr(cur, ptr + 1, block)));
2970 /* It's a leaf. make a hole in the records */
2971 union xfs_btree_rec *rp;
2973 rp = xfs_btree_rec_addr(cur, ptr, block);
2975 xfs_btree_shift_recs(cur, rp, 1, numrecs - ptr + 1);
2977 /* Now put the new data in, bump numrecs and log it. */
2978 xfs_btree_copy_recs(cur, rp, recp, 1);
2979 xfs_btree_set_numrecs(block, ++numrecs);
2980 xfs_btree_log_recs(cur, bp, ptr, numrecs);
2982 if (ptr < numrecs) {
2983 ASSERT(cur->bc_ops->recs_inorder(cur, rp,
2984 xfs_btree_rec_addr(cur, ptr + 1, block)));
2989 /* Log the new number of records in the btree header. */
2990 xfs_btree_log_block(cur, bp, XFS_BB_NUMRECS);
2992 /* If we inserted at the start of a block, update the parents' keys. */
2994 error = xfs_btree_updkey(cur, &key, level + 1);
3000 * If we are tracking the last record in the tree and
3001 * we are at the far right edge of the tree, update it.
3003 if (xfs_btree_is_lastrec(cur, block, level)) {
3004 cur->bc_ops->update_lastrec(cur, block, recp,
3005 ptr, LASTREC_INSREC);
3009 * Return the new block number, if any.
3010 * If there is one, give back a record value and a cursor too.
3013 if (!xfs_btree_ptr_is_null(cur, &nptr)) {
3018 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3023 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
3028 * Insert the record at the point referenced by cur.
3030 * A multi-level split of the tree on insert will invalidate the original
3031 * cursor. All callers of this function should assume that the cursor is
3032 * no longer valid and revalidate it.
3036 struct xfs_btree_cur *cur,
3039 int error; /* error return value */
3040 int i; /* result value, 0 for failure */
3041 int level; /* current level number in btree */
3042 union xfs_btree_ptr nptr; /* new block number (split result) */
3043 struct xfs_btree_cur *ncur; /* new cursor (split result) */
3044 struct xfs_btree_cur *pcur; /* previous level's cursor */
3045 union xfs_btree_rec rec; /* record to insert */
3051 xfs_btree_set_ptr_null(cur, &nptr);
3052 cur->bc_ops->init_rec_from_cur(cur, &rec);
3055 * Loop going up the tree, starting at the leaf level.
3056 * Stop when we don't get a split block, that must mean that
3057 * the insert is finished with this level.
3061 * Insert nrec/nptr into this level of the tree.
3062 * Note if we fail, nptr will be null.
3064 error = xfs_btree_insrec(pcur, level, &nptr, &rec, &ncur, &i);
3067 xfs_btree_del_cursor(pcur, XFS_BTREE_ERROR);
3071 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
3075 * See if the cursor we just used is trash.
3076 * Can't trash the caller's cursor, but otherwise we should
3077 * if ncur is a new cursor or we're about to be done.
3080 (ncur || xfs_btree_ptr_is_null(cur, &nptr))) {
3081 /* Save the state from the cursor before we trash it */
3082 if (cur->bc_ops->update_cursor)
3083 cur->bc_ops->update_cursor(pcur, cur);
3084 cur->bc_nlevels = pcur->bc_nlevels;
3085 xfs_btree_del_cursor(pcur, XFS_BTREE_NOERROR);
3087 /* If we got a new cursor, switch to it. */
3092 } while (!xfs_btree_ptr_is_null(cur, &nptr));
3094 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3098 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
3103 * Try to merge a non-leaf block back into the inode root.
3105 * Note: the killroot names comes from the fact that we're effectively
3106 * killing the old root block. But because we can't just delete the
3107 * inode we have to copy the single block it was pointing to into the
3111 xfs_btree_kill_iroot(
3112 struct xfs_btree_cur *cur)
3114 int whichfork = cur->bc_private.b.whichfork;
3115 struct xfs_inode *ip = cur->bc_private.b.ip;
3116 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
3117 struct xfs_btree_block *block;
3118 struct xfs_btree_block *cblock;
3119 union xfs_btree_key *kp;
3120 union xfs_btree_key *ckp;
3121 union xfs_btree_ptr *pp;
3122 union xfs_btree_ptr *cpp;
3123 struct xfs_buf *cbp;
3128 union xfs_btree_ptr ptr;
3132 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
3134 ASSERT(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE);
3135 ASSERT(cur->bc_nlevels > 1);
3138 * Don't deal with the root block needs to be a leaf case.
3139 * We're just going to turn the thing back into extents anyway.
3141 level = cur->bc_nlevels - 1;
3146 * Give up if the root has multiple children.
3148 block = xfs_btree_get_iroot(cur);
3149 if (xfs_btree_get_numrecs(block) != 1)
3152 cblock = xfs_btree_get_block(cur, level - 1, &cbp);
3153 numrecs = xfs_btree_get_numrecs(cblock);
3156 * Only do this if the next level will fit.
3157 * Then the data must be copied up to the inode,
3158 * instead of freeing the root you free the next level.
3160 if (numrecs > cur->bc_ops->get_dmaxrecs(cur, level))
3163 XFS_BTREE_STATS_INC(cur, killroot);
3166 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_LEFTSIB);
3167 ASSERT(xfs_btree_ptr_is_null(cur, &ptr));
3168 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_RIGHTSIB);
3169 ASSERT(xfs_btree_ptr_is_null(cur, &ptr));
3172 index = numrecs - cur->bc_ops->get_maxrecs(cur, level);
3174 xfs_iroot_realloc(cur->bc_private.b.ip, index,
3175 cur->bc_private.b.whichfork);
3176 block = ifp->if_broot;
3179 be16_add_cpu(&block->bb_numrecs, index);
3180 ASSERT(block->bb_numrecs == cblock->bb_numrecs);
3182 kp = xfs_btree_key_addr(cur, 1, block);
3183 ckp = xfs_btree_key_addr(cur, 1, cblock);
3184 xfs_btree_copy_keys(cur, kp, ckp, numrecs);
3186 pp = xfs_btree_ptr_addr(cur, 1, block);
3187 cpp = xfs_btree_ptr_addr(cur, 1, cblock);
3189 for (i = 0; i < numrecs; i++) {
3192 error = xfs_btree_check_ptr(cur, cpp, i, level - 1);
3194 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
3199 xfs_btree_copy_ptrs(cur, pp, cpp, numrecs);
3201 cur->bc_ops->free_block(cur, cbp);
3202 XFS_BTREE_STATS_INC(cur, free);
3204 cur->bc_bufs[level - 1] = NULL;
3205 be16_add_cpu(&block->bb_level, -1);
3206 xfs_trans_log_inode(cur->bc_tp, ip,
3207 XFS_ILOG_CORE | xfs_ilog_fbroot(cur->bc_private.b.whichfork));
3210 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3215 * Kill the current root node, and replace it with it's only child node.
3218 xfs_btree_kill_root(
3219 struct xfs_btree_cur *cur,
3222 union xfs_btree_ptr *newroot)
3226 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
3227 XFS_BTREE_STATS_INC(cur, killroot);
3230 * Update the root pointer, decreasing the level by 1 and then
3231 * free the old root.
3233 cur->bc_ops->set_root(cur, newroot, -1);
3235 error = cur->bc_ops->free_block(cur, bp);
3237 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
3241 XFS_BTREE_STATS_INC(cur, free);
3243 cur->bc_bufs[level] = NULL;
3244 cur->bc_ra[level] = 0;
3247 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3252 xfs_btree_dec_cursor(
3253 struct xfs_btree_cur *cur,
3261 error = xfs_btree_decrement(cur, level, &i);
3266 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3272 * Single level of the btree record deletion routine.
3273 * Delete record pointed to by cur/level.
3274 * Remove the record from its block then rebalance the tree.
3275 * Return 0 for error, 1 for done, 2 to go on to the next level.
3277 STATIC int /* error */
3279 struct xfs_btree_cur *cur, /* btree cursor */
3280 int level, /* level removing record from */
3281 int *stat) /* fail/done/go-on */
3283 struct xfs_btree_block *block; /* btree block */
3284 union xfs_btree_ptr cptr; /* current block ptr */
3285 struct xfs_buf *bp; /* buffer for block */
3286 int error; /* error return value */
3287 int i; /* loop counter */
3288 union xfs_btree_key key; /* storage for keyp */
3289 union xfs_btree_key *keyp = &key; /* passed to the next level */
3290 union xfs_btree_ptr lptr; /* left sibling block ptr */
3291 struct xfs_buf *lbp; /* left buffer pointer */
3292 struct xfs_btree_block *left; /* left btree block */
3293 int lrecs = 0; /* left record count */
3294 int ptr; /* key/record index */
3295 union xfs_btree_ptr rptr; /* right sibling block ptr */
3296 struct xfs_buf *rbp; /* right buffer pointer */
3297 struct xfs_btree_block *right; /* right btree block */
3298 struct xfs_btree_block *rrblock; /* right-right btree block */
3299 struct xfs_buf *rrbp; /* right-right buffer pointer */
3300 int rrecs = 0; /* right record count */
3301 struct xfs_btree_cur *tcur; /* temporary btree cursor */
3302 int numrecs; /* temporary numrec count */
3304 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
3305 XFS_BTREE_TRACE_ARGI(cur, level);
3309 /* Get the index of the entry being deleted, check for nothing there. */
3310 ptr = cur->bc_ptrs[level];
3312 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3317 /* Get the buffer & block containing the record or key/ptr. */
3318 block = xfs_btree_get_block(cur, level, &bp);
3319 numrecs = xfs_btree_get_numrecs(block);
3322 error = xfs_btree_check_block(cur, block, level, bp);
3327 /* Fail if we're off the end of the block. */
3328 if (ptr > numrecs) {
3329 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3334 XFS_BTREE_STATS_INC(cur, delrec);
3335 XFS_BTREE_STATS_ADD(cur, moves, numrecs - ptr);
3337 /* Excise the entries being deleted. */
3339 /* It's a nonleaf. operate on keys and ptrs */
3340 union xfs_btree_key *lkp;
3341 union xfs_btree_ptr *lpp;
3343 lkp = xfs_btree_key_addr(cur, ptr + 1, block);
3344 lpp = xfs_btree_ptr_addr(cur, ptr + 1, block);
3347 for (i = 0; i < numrecs - ptr; i++) {
3348 error = xfs_btree_check_ptr(cur, lpp, i, level);
3354 if (ptr < numrecs) {
3355 xfs_btree_shift_keys(cur, lkp, -1, numrecs - ptr);
3356 xfs_btree_shift_ptrs(cur, lpp, -1, numrecs - ptr);
3357 xfs_btree_log_keys(cur, bp, ptr, numrecs - 1);
3358 xfs_btree_log_ptrs(cur, bp, ptr, numrecs - 1);
3362 * If it's the first record in the block, we'll need to pass a
3363 * key up to the next level (updkey).
3366 keyp = xfs_btree_key_addr(cur, 1, block);
3368 /* It's a leaf. operate on records */
3369 if (ptr < numrecs) {
3370 xfs_btree_shift_recs(cur,
3371 xfs_btree_rec_addr(cur, ptr + 1, block),
3373 xfs_btree_log_recs(cur, bp, ptr, numrecs - 1);
3377 * If it's the first record in the block, we'll need a key
3378 * structure to pass up to the next level (updkey).
3381 cur->bc_ops->init_key_from_rec(&key,
3382 xfs_btree_rec_addr(cur, 1, block));
3388 * Decrement and log the number of entries in the block.
3390 xfs_btree_set_numrecs(block, --numrecs);
3391 xfs_btree_log_block(cur, bp, XFS_BB_NUMRECS);
3394 * If we are tracking the last record in the tree and
3395 * we are at the far right edge of the tree, update it.
3397 if (xfs_btree_is_lastrec(cur, block, level)) {
3398 cur->bc_ops->update_lastrec(cur, block, NULL,
3399 ptr, LASTREC_DELREC);
3403 * We're at the root level. First, shrink the root block in-memory.
3404 * Try to get rid of the next level down. If we can't then there's
3405 * nothing left to do.
3407 if (level == cur->bc_nlevels - 1) {
3408 if (cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) {
3409 xfs_iroot_realloc(cur->bc_private.b.ip, -1,
3410 cur->bc_private.b.whichfork);
3412 error = xfs_btree_kill_iroot(cur);
3416 error = xfs_btree_dec_cursor(cur, level, stat);
3424 * If this is the root level, and there's only one entry left,
3425 * and it's NOT the leaf level, then we can get rid of this
3428 if (numrecs == 1 && level > 0) {
3429 union xfs_btree_ptr *pp;
3431 * pp is still set to the first pointer in the block.
3432 * Make it the new root of the btree.
3434 pp = xfs_btree_ptr_addr(cur, 1, block);
3435 error = xfs_btree_kill_root(cur, bp, level, pp);
3438 } else if (level > 0) {
3439 error = xfs_btree_dec_cursor(cur, level, stat);
3448 * If we deleted the leftmost entry in the block, update the
3449 * key values above us in the tree.
3452 error = xfs_btree_updkey(cur, keyp, level + 1);
3458 * If the number of records remaining in the block is at least
3459 * the minimum, we're done.
3461 if (numrecs >= cur->bc_ops->get_minrecs(cur, level)) {
3462 error = xfs_btree_dec_cursor(cur, level, stat);
3469 * Otherwise, we have to move some records around to keep the
3470 * tree balanced. Look at the left and right sibling blocks to
3471 * see if we can re-balance by moving only one record.
3473 xfs_btree_get_sibling(cur, block, &rptr, XFS_BB_RIGHTSIB);
3474 xfs_btree_get_sibling(cur, block, &lptr, XFS_BB_LEFTSIB);
3476 if (cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) {
3478 * One child of root, need to get a chance to copy its contents
3479 * into the root and delete it. Can't go up to next level,
3480 * there's nothing to delete there.
3482 if (xfs_btree_ptr_is_null(cur, &rptr) &&
3483 xfs_btree_ptr_is_null(cur, &lptr) &&
3484 level == cur->bc_nlevels - 2) {
3485 error = xfs_btree_kill_iroot(cur);
3487 error = xfs_btree_dec_cursor(cur, level, stat);
3494 ASSERT(!xfs_btree_ptr_is_null(cur, &rptr) ||
3495 !xfs_btree_ptr_is_null(cur, &lptr));
3498 * Duplicate the cursor so our btree manipulations here won't
3499 * disrupt the next level up.
3501 error = xfs_btree_dup_cursor(cur, &tcur);
3506 * If there's a right sibling, see if it's ok to shift an entry
3509 if (!xfs_btree_ptr_is_null(cur, &rptr)) {
3511 * Move the temp cursor to the last entry in the next block.
3512 * Actually any entry but the first would suffice.
3514 i = xfs_btree_lastrec(tcur, level);
3515 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
3517 error = xfs_btree_increment(tcur, level, &i);
3520 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
3522 i = xfs_btree_lastrec(tcur, level);
3523 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
3525 /* Grab a pointer to the block. */
3526 right = xfs_btree_get_block(tcur, level, &rbp);
3528 error = xfs_btree_check_block(tcur, right, level, rbp);
3532 /* Grab the current block number, for future use. */
3533 xfs_btree_get_sibling(tcur, right, &cptr, XFS_BB_LEFTSIB);
3536 * If right block is full enough so that removing one entry
3537 * won't make it too empty, and left-shifting an entry out
3538 * of right to us works, we're done.
3540 if (xfs_btree_get_numrecs(right) - 1 >=
3541 cur->bc_ops->get_minrecs(tcur, level)) {
3542 error = xfs_btree_lshift(tcur, level, &i);
3546 ASSERT(xfs_btree_get_numrecs(block) >=
3547 cur->bc_ops->get_minrecs(tcur, level));
3549 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
3552 error = xfs_btree_dec_cursor(cur, level, stat);
3560 * Otherwise, grab the number of records in right for
3561 * future reference, and fix up the temp cursor to point
3562 * to our block again (last record).
3564 rrecs = xfs_btree_get_numrecs(right);
3565 if (!xfs_btree_ptr_is_null(cur, &lptr)) {
3566 i = xfs_btree_firstrec(tcur, level);
3567 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
3569 error = xfs_btree_decrement(tcur, level, &i);
3572 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
3577 * If there's a left sibling, see if it's ok to shift an entry
3580 if (!xfs_btree_ptr_is_null(cur, &lptr)) {
3582 * Move the temp cursor to the first entry in the
3585 i = xfs_btree_firstrec(tcur, level);
3586 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
3588 error = xfs_btree_decrement(tcur, level, &i);
3591 i = xfs_btree_firstrec(tcur, level);
3592 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
3594 /* Grab a pointer to the block. */
3595 left = xfs_btree_get_block(tcur, level, &lbp);
3597 error = xfs_btree_check_block(cur, left, level, lbp);
3601 /* Grab the current block number, for future use. */
3602 xfs_btree_get_sibling(tcur, left, &cptr, XFS_BB_RIGHTSIB);
3605 * If left block is full enough so that removing one entry
3606 * won't make it too empty, and right-shifting an entry out
3607 * of left to us works, we're done.
3609 if (xfs_btree_get_numrecs(left) - 1 >=
3610 cur->bc_ops->get_minrecs(tcur, level)) {
3611 error = xfs_btree_rshift(tcur, level, &i);
3615 ASSERT(xfs_btree_get_numrecs(block) >=
3616 cur->bc_ops->get_minrecs(tcur, level));
3617 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
3621 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3628 * Otherwise, grab the number of records in right for
3631 lrecs = xfs_btree_get_numrecs(left);
3634 /* Delete the temp cursor, we're done with it. */
3635 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
3638 /* If here, we need to do a join to keep the tree balanced. */
3639 ASSERT(!xfs_btree_ptr_is_null(cur, &cptr));
3641 if (!xfs_btree_ptr_is_null(cur, &lptr) &&
3642 lrecs + xfs_btree_get_numrecs(block) <=
3643 cur->bc_ops->get_maxrecs(cur, level)) {
3645 * Set "right" to be the starting block,
3646 * "left" to be the left neighbor.
3651 error = xfs_btree_read_buf_block(cur, &lptr, 0, &left, &lbp);
3656 * If that won't work, see if we can join with the right neighbor block.
3658 } else if (!xfs_btree_ptr_is_null(cur, &rptr) &&
3659 rrecs + xfs_btree_get_numrecs(block) <=
3660 cur->bc_ops->get_maxrecs(cur, level)) {
3662 * Set "left" to be the starting block,
3663 * "right" to be the right neighbor.
3668 error = xfs_btree_read_buf_block(cur, &rptr, 0, &right, &rbp);
3673 * Otherwise, we can't fix the imbalance.
3674 * Just return. This is probably a logic error, but it's not fatal.
3677 error = xfs_btree_dec_cursor(cur, level, stat);
3683 rrecs = xfs_btree_get_numrecs(right);
3684 lrecs = xfs_btree_get_numrecs(left);
3687 * We're now going to join "left" and "right" by moving all the stuff
3688 * in "right" to "left" and deleting "right".
3690 XFS_BTREE_STATS_ADD(cur, moves, rrecs);
3692 /* It's a non-leaf. Move keys and pointers. */
3693 union xfs_btree_key *lkp; /* left btree key */
3694 union xfs_btree_ptr *lpp; /* left address pointer */
3695 union xfs_btree_key *rkp; /* right btree key */
3696 union xfs_btree_ptr *rpp; /* right address pointer */
3698 lkp = xfs_btree_key_addr(cur, lrecs + 1, left);
3699 lpp = xfs_btree_ptr_addr(cur, lrecs + 1, left);
3700 rkp = xfs_btree_key_addr(cur, 1, right);
3701 rpp = xfs_btree_ptr_addr(cur, 1, right);
3703 for (i = 1; i < rrecs; i++) {
3704 error = xfs_btree_check_ptr(cur, rpp, i, level);
3709 xfs_btree_copy_keys(cur, lkp, rkp, rrecs);
3710 xfs_btree_copy_ptrs(cur, lpp, rpp, rrecs);
3712 xfs_btree_log_keys(cur, lbp, lrecs + 1, lrecs + rrecs);
3713 xfs_btree_log_ptrs(cur, lbp, lrecs + 1, lrecs + rrecs);
3715 /* It's a leaf. Move records. */
3716 union xfs_btree_rec *lrp; /* left record pointer */
3717 union xfs_btree_rec *rrp; /* right record pointer */
3719 lrp = xfs_btree_rec_addr(cur, lrecs + 1, left);
3720 rrp = xfs_btree_rec_addr(cur, 1, right);
3722 xfs_btree_copy_recs(cur, lrp, rrp, rrecs);
3723 xfs_btree_log_recs(cur, lbp, lrecs + 1, lrecs + rrecs);
3726 XFS_BTREE_STATS_INC(cur, join);
3729 * Fix up the number of records and right block pointer in the
3730 * surviving block, and log it.
3732 xfs_btree_set_numrecs(left, lrecs + rrecs);
3733 xfs_btree_get_sibling(cur, right, &cptr, XFS_BB_RIGHTSIB),
3734 xfs_btree_set_sibling(cur, left, &cptr, XFS_BB_RIGHTSIB);
3735 xfs_btree_log_block(cur, lbp, XFS_BB_NUMRECS | XFS_BB_RIGHTSIB);
3737 /* If there is a right sibling, point it to the remaining block. */
3738 xfs_btree_get_sibling(cur, left, &cptr, XFS_BB_RIGHTSIB);
3739 if (!xfs_btree_ptr_is_null(cur, &cptr)) {
3740 error = xfs_btree_read_buf_block(cur, &cptr, 0, &rrblock, &rrbp);
3743 xfs_btree_set_sibling(cur, rrblock, &lptr, XFS_BB_LEFTSIB);
3744 xfs_btree_log_block(cur, rrbp, XFS_BB_LEFTSIB);
3747 /* Free the deleted block. */
3748 error = cur->bc_ops->free_block(cur, rbp);
3751 XFS_BTREE_STATS_INC(cur, free);
3754 * If we joined with the left neighbor, set the buffer in the
3755 * cursor to the left block, and fix up the index.
3758 cur->bc_bufs[level] = lbp;
3759 cur->bc_ptrs[level] += lrecs;
3760 cur->bc_ra[level] = 0;
3763 * If we joined with the right neighbor and there's a level above
3764 * us, increment the cursor at that level.
3766 else if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) ||
3767 (level + 1 < cur->bc_nlevels)) {
3768 error = xfs_btree_increment(cur, level + 1, &i);
3774 * Readjust the ptr at this level if it's not a leaf, since it's
3775 * still pointing at the deletion point, which makes the cursor
3776 * inconsistent. If this makes the ptr 0, the caller fixes it up.
3777 * We can't use decrement because it would change the next level up.
3780 cur->bc_ptrs[level]--;
3782 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3783 /* Return value means the next level up has something to do. */
3788 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
3790 xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
3795 * Delete the record pointed to by cur.
3796 * The cursor refers to the place where the record was (could be inserted)
3797 * when the operation returns.
3801 struct xfs_btree_cur *cur,
3802 int *stat) /* success/failure */
3804 int error; /* error return value */
3808 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
3811 * Go up the tree, starting at leaf level.
3813 * If 2 is returned then a join was done; go to the next level.
3814 * Otherwise we are done.
3816 for (level = 0, i = 2; i == 2; level++) {
3817 error = xfs_btree_delrec(cur, level, &i);
3823 for (level = 1; level < cur->bc_nlevels; level++) {
3824 if (cur->bc_ptrs[level] == 0) {
3825 error = xfs_btree_decrement(cur, level, &i);
3833 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3837 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
3842 * Get the data from the pointed-to record.
3846 struct xfs_btree_cur *cur, /* btree cursor */
3847 union xfs_btree_rec **recp, /* output: btree record */
3848 int *stat) /* output: success/failure */
3850 struct xfs_btree_block *block; /* btree block */
3851 struct xfs_buf *bp; /* buffer pointer */
3852 int ptr; /* record number */
3854 int error; /* error return value */
3857 ptr = cur->bc_ptrs[0];
3858 block = xfs_btree_get_block(cur, 0, &bp);
3861 error = xfs_btree_check_block(cur, block, 0, bp);
3867 * Off the right end or left end, return failure.
3869 if (ptr > xfs_btree_get_numrecs(block) || ptr <= 0) {
3875 * Point to the record and extract its data.
3877 *recp = xfs_btree_rec_addr(cur, ptr, block);
3883 * Change the owner of a btree.
3885 * The mechanism we use here is ordered buffer logging. Because we don't know
3886 * how many buffers were are going to need to modify, we don't really want to
3887 * have to make transaction reservations for the worst case of every buffer in a
3888 * full size btree as that may be more space that we can fit in the log....
3890 * We do the btree walk in the most optimal manner possible - we have sibling
3891 * pointers so we can just walk all the blocks on each level from left to right
3892 * in a single pass, and then move to the next level and do the same. We can
3893 * also do readahead on the sibling pointers to get IO moving more quickly,
3894 * though for slow disks this is unlikely to make much difference to performance
3895 * as the amount of CPU work we have to do before moving to the next block is
3898 * For each btree block that we load, modify the owner appropriately, set the
3899 * buffer as an ordered buffer and log it appropriately. We need to ensure that
3900 * we mark the region we change dirty so that if the buffer is relogged in
3901 * a subsequent transaction the changes we make here as an ordered buffer are
3902 * correctly relogged in that transaction. If we are in recovery context, then
3903 * just queue the modified buffer as delayed write buffer so the transaction
3904 * recovery completion writes the changes to disk.
3907 xfs_btree_block_change_owner(
3908 struct xfs_btree_cur *cur,
3910 __uint64_t new_owner,
3911 struct list_head *buffer_list)
3913 struct xfs_btree_block *block;
3915 union xfs_btree_ptr rptr;
3917 /* do right sibling readahead */
3918 xfs_btree_readahead(cur, level, XFS_BTCUR_RIGHTRA);
3920 /* modify the owner */
3921 block = xfs_btree_get_block(cur, level, &bp);
3922 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
3923 block->bb_u.l.bb_owner = cpu_to_be64(new_owner);
3925 block->bb_u.s.bb_owner = cpu_to_be32(new_owner);
3928 * If the block is a root block hosted in an inode, we might not have a
3929 * buffer pointer here and we shouldn't attempt to log the change as the
3930 * information is already held in the inode and discarded when the root
3931 * block is formatted into the on-disk inode fork. We still change it,
3932 * though, so everything is consistent in memory.
3936 xfs_trans_ordered_buf(cur->bc_tp, bp);
3937 xfs_btree_log_block(cur, bp, XFS_BB_OWNER);
3939 xfs_buf_delwri_queue(bp, buffer_list);
3942 ASSERT(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE);
3943 ASSERT(level == cur->bc_nlevels - 1);
3946 /* now read rh sibling block for next iteration */
3947 xfs_btree_get_sibling(cur, block, &rptr, XFS_BB_RIGHTSIB);
3948 if (xfs_btree_ptr_is_null(cur, &rptr))
3951 return xfs_btree_lookup_get_block(cur, level, &rptr, &block);
3955 xfs_btree_change_owner(
3956 struct xfs_btree_cur *cur,
3957 __uint64_t new_owner,
3958 struct list_head *buffer_list)
3960 union xfs_btree_ptr lptr;
3962 struct xfs_btree_block *block = NULL;
3965 cur->bc_ops->init_ptr_from_cur(cur, &lptr);
3967 /* for each level */
3968 for (level = cur->bc_nlevels - 1; level >= 0; level--) {
3969 /* grab the left hand block */
3970 error = xfs_btree_lookup_get_block(cur, level, &lptr, &block);
3974 /* readahead the left most block for the next level down */
3976 union xfs_btree_ptr *ptr;
3978 ptr = xfs_btree_ptr_addr(cur, 1, block);
3979 xfs_btree_readahead_ptr(cur, ptr, 1);
3981 /* save for the next iteration of the loop */
3985 /* for each buffer in the level */
3987 error = xfs_btree_block_change_owner(cur, level,
3992 if (error != ENOENT)