2 * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 #include "xfs_types.h"
23 #include "xfs_trans.h"
26 #include "xfs_mount.h"
27 #include "xfs_bmap_btree.h"
28 #include "xfs_alloc_btree.h"
29 #include "xfs_ialloc_btree.h"
30 #include "xfs_dinode.h"
31 #include "xfs_inode.h"
32 #include "xfs_inode_item.h"
33 #include "xfs_buf_item.h"
34 #include "xfs_btree.h"
35 #include "xfs_error.h"
36 #include "xfs_trace.h"
37 #include "xfs_cksum.h"
40 * Cursor allocation zone.
42 kmem_zone_t *xfs_btree_cur_zone;
45 * Btree magic numbers.
47 static const __uint32_t xfs_magics[2][XFS_BTNUM_MAX] = {
48 { XFS_ABTB_MAGIC, XFS_ABTC_MAGIC, XFS_BMAP_MAGIC, XFS_IBT_MAGIC },
49 { XFS_ABTB_CRC_MAGIC, XFS_ABTC_CRC_MAGIC,
50 XFS_BMAP_CRC_MAGIC, XFS_IBT_CRC_MAGIC }
52 #define xfs_btree_magic(cur) \
53 xfs_magics[!!((cur)->bc_flags & XFS_BTREE_CRC_BLOCKS)][cur->bc_btnum]
56 STATIC int /* error (0 or EFSCORRUPTED) */
57 xfs_btree_check_lblock(
58 struct xfs_btree_cur *cur, /* btree cursor */
59 struct xfs_btree_block *block, /* btree long form block pointer */
60 int level, /* level of the btree block */
61 struct xfs_buf *bp) /* buffer for block, if any */
63 int lblock_ok = 1; /* block passes checks */
64 struct xfs_mount *mp; /* file system mount point */
68 if (xfs_sb_version_hascrc(&mp->m_sb)) {
69 lblock_ok = lblock_ok &&
70 uuid_equal(&block->bb_u.l.bb_uuid, &mp->m_sb.sb_uuid) &&
71 block->bb_u.l.bb_blkno == cpu_to_be64(
72 bp ? bp->b_bn : XFS_BUF_DADDR_NULL);
75 lblock_ok = lblock_ok &&
76 be32_to_cpu(block->bb_magic) == xfs_btree_magic(cur) &&
77 be16_to_cpu(block->bb_level) == level &&
78 be16_to_cpu(block->bb_numrecs) <=
79 cur->bc_ops->get_maxrecs(cur, level) &&
80 block->bb_u.l.bb_leftsib &&
81 (block->bb_u.l.bb_leftsib == cpu_to_be64(NULLDFSBNO) ||
82 XFS_FSB_SANITY_CHECK(mp,
83 be64_to_cpu(block->bb_u.l.bb_leftsib))) &&
84 block->bb_u.l.bb_rightsib &&
85 (block->bb_u.l.bb_rightsib == cpu_to_be64(NULLDFSBNO) ||
86 XFS_FSB_SANITY_CHECK(mp,
87 be64_to_cpu(block->bb_u.l.bb_rightsib)));
89 if (unlikely(XFS_TEST_ERROR(!lblock_ok, mp,
90 XFS_ERRTAG_BTREE_CHECK_LBLOCK,
91 XFS_RANDOM_BTREE_CHECK_LBLOCK))) {
93 trace_xfs_btree_corrupt(bp, _RET_IP_);
94 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp);
95 return XFS_ERROR(EFSCORRUPTED);
100 STATIC int /* error (0 or EFSCORRUPTED) */
101 xfs_btree_check_sblock(
102 struct xfs_btree_cur *cur, /* btree cursor */
103 struct xfs_btree_block *block, /* btree short form block pointer */
104 int level, /* level of the btree block */
105 struct xfs_buf *bp) /* buffer containing block */
107 struct xfs_mount *mp; /* file system mount point */
108 struct xfs_buf *agbp; /* buffer for ag. freespace struct */
109 struct xfs_agf *agf; /* ag. freespace structure */
110 xfs_agblock_t agflen; /* native ag. freespace length */
111 int sblock_ok = 1; /* block passes checks */
114 agbp = cur->bc_private.a.agbp;
115 agf = XFS_BUF_TO_AGF(agbp);
116 agflen = be32_to_cpu(agf->agf_length);
118 if (xfs_sb_version_hascrc(&mp->m_sb)) {
119 sblock_ok = sblock_ok &&
120 uuid_equal(&block->bb_u.s.bb_uuid, &mp->m_sb.sb_uuid) &&
121 block->bb_u.s.bb_blkno == cpu_to_be64(
122 bp ? bp->b_bn : XFS_BUF_DADDR_NULL);
125 sblock_ok = sblock_ok &&
126 be32_to_cpu(block->bb_magic) == xfs_btree_magic(cur) &&
127 be16_to_cpu(block->bb_level) == level &&
128 be16_to_cpu(block->bb_numrecs) <=
129 cur->bc_ops->get_maxrecs(cur, level) &&
130 (block->bb_u.s.bb_leftsib == cpu_to_be32(NULLAGBLOCK) ||
131 be32_to_cpu(block->bb_u.s.bb_leftsib) < agflen) &&
132 block->bb_u.s.bb_leftsib &&
133 (block->bb_u.s.bb_rightsib == cpu_to_be32(NULLAGBLOCK) ||
134 be32_to_cpu(block->bb_u.s.bb_rightsib) < agflen) &&
135 block->bb_u.s.bb_rightsib;
137 if (unlikely(XFS_TEST_ERROR(!sblock_ok, mp,
138 XFS_ERRTAG_BTREE_CHECK_SBLOCK,
139 XFS_RANDOM_BTREE_CHECK_SBLOCK))) {
141 trace_xfs_btree_corrupt(bp, _RET_IP_);
142 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp);
143 return XFS_ERROR(EFSCORRUPTED);
149 * Debug routine: check that block header is ok.
152 xfs_btree_check_block(
153 struct xfs_btree_cur *cur, /* btree cursor */
154 struct xfs_btree_block *block, /* generic btree block pointer */
155 int level, /* level of the btree block */
156 struct xfs_buf *bp) /* buffer containing block, if any */
158 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
159 return xfs_btree_check_lblock(cur, block, level, bp);
161 return xfs_btree_check_sblock(cur, block, level, bp);
165 * Check that (long) pointer is ok.
167 int /* error (0 or EFSCORRUPTED) */
168 xfs_btree_check_lptr(
169 struct xfs_btree_cur *cur, /* btree cursor */
170 xfs_dfsbno_t bno, /* btree block disk address */
171 int level) /* btree block level */
173 XFS_WANT_CORRUPTED_RETURN(
176 XFS_FSB_SANITY_CHECK(cur->bc_mp, bno));
182 * Check that (short) pointer is ok.
184 STATIC int /* error (0 or EFSCORRUPTED) */
185 xfs_btree_check_sptr(
186 struct xfs_btree_cur *cur, /* btree cursor */
187 xfs_agblock_t bno, /* btree block disk address */
188 int level) /* btree block level */
190 xfs_agblock_t agblocks = cur->bc_mp->m_sb.sb_agblocks;
192 XFS_WANT_CORRUPTED_RETURN(
194 bno != NULLAGBLOCK &&
201 * Check that block ptr is ok.
203 STATIC int /* error (0 or EFSCORRUPTED) */
205 struct xfs_btree_cur *cur, /* btree cursor */
206 union xfs_btree_ptr *ptr, /* btree block disk address */
207 int index, /* offset from ptr to check */
208 int level) /* btree block level */
210 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
211 return xfs_btree_check_lptr(cur,
212 be64_to_cpu((&ptr->l)[index]), level);
214 return xfs_btree_check_sptr(cur,
215 be32_to_cpu((&ptr->s)[index]), level);
221 * Calculate CRC on the whole btree block and stuff it into the
222 * long-form btree header.
224 * Prior to calculting the CRC, pull the LSN out of the buffer log item and put
225 * it into the buffer so recovery knows what the last modifcation was that made
229 xfs_btree_lblock_calc_crc(
232 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
233 struct xfs_buf_log_item *bip = bp->b_fspriv;
235 if (!xfs_sb_version_hascrc(&bp->b_target->bt_mount->m_sb))
238 block->bb_u.l.bb_lsn = cpu_to_be64(bip->bli_item.li_lsn);
239 xfs_update_cksum(bp->b_addr, BBTOB(bp->b_length),
240 XFS_BTREE_LBLOCK_CRC_OFF);
244 xfs_btree_lblock_verify_crc(
247 if (xfs_sb_version_hascrc(&bp->b_target->bt_mount->m_sb))
248 return xfs_verify_cksum(bp->b_addr, BBTOB(bp->b_length),
249 XFS_BTREE_LBLOCK_CRC_OFF);
254 * Calculate CRC on the whole btree block and stuff it into the
255 * short-form btree header.
257 * Prior to calculting the CRC, pull the LSN out of the buffer log item and put
258 * it into the buffer so recovery knows what the last modifcation was that made
262 xfs_btree_sblock_calc_crc(
265 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
266 struct xfs_buf_log_item *bip = bp->b_fspriv;
268 if (!xfs_sb_version_hascrc(&bp->b_target->bt_mount->m_sb))
271 block->bb_u.s.bb_lsn = cpu_to_be64(bip->bli_item.li_lsn);
272 xfs_update_cksum(bp->b_addr, BBTOB(bp->b_length),
273 XFS_BTREE_SBLOCK_CRC_OFF);
277 xfs_btree_sblock_verify_crc(
280 if (xfs_sb_version_hascrc(&bp->b_target->bt_mount->m_sb))
281 return xfs_verify_cksum(bp->b_addr, BBTOB(bp->b_length),
282 XFS_BTREE_SBLOCK_CRC_OFF);
287 * Delete the btree cursor.
290 xfs_btree_del_cursor(
291 xfs_btree_cur_t *cur, /* btree cursor */
292 int error) /* del because of error */
294 int i; /* btree level */
297 * Clear the buffer pointers, and release the buffers.
298 * If we're doing this in the face of an error, we
299 * need to make sure to inspect all of the entries
300 * in the bc_bufs array for buffers to be unlocked.
301 * This is because some of the btree code works from
302 * level n down to 0, and if we get an error along
303 * the way we won't have initialized all the entries
306 for (i = 0; i < cur->bc_nlevels; i++) {
308 xfs_trans_brelse(cur->bc_tp, cur->bc_bufs[i]);
313 * Can't free a bmap cursor without having dealt with the
314 * allocated indirect blocks' accounting.
316 ASSERT(cur->bc_btnum != XFS_BTNUM_BMAP ||
317 cur->bc_private.b.allocated == 0);
321 kmem_zone_free(xfs_btree_cur_zone, cur);
325 * Duplicate the btree cursor.
326 * Allocate a new one, copy the record, re-get the buffers.
329 xfs_btree_dup_cursor(
330 xfs_btree_cur_t *cur, /* input cursor */
331 xfs_btree_cur_t **ncur) /* output cursor */
333 xfs_buf_t *bp; /* btree block's buffer pointer */
334 int error; /* error return value */
335 int i; /* level number of btree block */
336 xfs_mount_t *mp; /* mount structure for filesystem */
337 xfs_btree_cur_t *new; /* new cursor value */
338 xfs_trans_t *tp; /* transaction pointer, can be NULL */
344 * Allocate a new cursor like the old one.
346 new = cur->bc_ops->dup_cursor(cur);
349 * Copy the record currently in the cursor.
351 new->bc_rec = cur->bc_rec;
354 * For each level current, re-get the buffer and copy the ptr value.
356 for (i = 0; i < new->bc_nlevels; i++) {
357 new->bc_ptrs[i] = cur->bc_ptrs[i];
358 new->bc_ra[i] = cur->bc_ra[i];
359 bp = cur->bc_bufs[i];
361 error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp,
362 XFS_BUF_ADDR(bp), mp->m_bsize,
364 cur->bc_ops->buf_ops);
366 xfs_btree_del_cursor(new, error);
371 new->bc_bufs[i] = bp;
378 * XFS btree block layout and addressing:
380 * There are two types of blocks in the btree: leaf and non-leaf blocks.
382 * The leaf record start with a header then followed by records containing
383 * the values. A non-leaf block also starts with the same header, and
384 * then first contains lookup keys followed by an equal number of pointers
385 * to the btree blocks at the previous level.
387 * +--------+-------+-------+-------+-------+-------+-------+
388 * Leaf: | header | rec 1 | rec 2 | rec 3 | rec 4 | rec 5 | rec N |
389 * +--------+-------+-------+-------+-------+-------+-------+
391 * +--------+-------+-------+-------+-------+-------+-------+
392 * Non-Leaf: | header | key 1 | key 2 | key N | ptr 1 | ptr 2 | ptr N |
393 * +--------+-------+-------+-------+-------+-------+-------+
395 * The header is called struct xfs_btree_block for reasons better left unknown
396 * and comes in different versions for short (32bit) and long (64bit) block
397 * pointers. The record and key structures are defined by the btree instances
398 * and opaque to the btree core. The block pointers are simple disk endian
399 * integers, available in a short (32bit) and long (64bit) variant.
401 * The helpers below calculate the offset of a given record, key or pointer
402 * into a btree block (xfs_btree_*_offset) or return a pointer to the given
403 * record, key or pointer (xfs_btree_*_addr). Note that all addressing
404 * inside the btree block is done using indices starting at one, not zero!
408 * Return size of the btree block header for this btree instance.
410 static inline size_t xfs_btree_block_len(struct xfs_btree_cur *cur)
412 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
413 if (cur->bc_flags & XFS_BTREE_CRC_BLOCKS)
414 return XFS_BTREE_LBLOCK_CRC_LEN;
415 return XFS_BTREE_LBLOCK_LEN;
417 if (cur->bc_flags & XFS_BTREE_CRC_BLOCKS)
418 return XFS_BTREE_SBLOCK_CRC_LEN;
419 return XFS_BTREE_SBLOCK_LEN;
423 * Return size of btree block pointers for this btree instance.
425 static inline size_t xfs_btree_ptr_len(struct xfs_btree_cur *cur)
427 return (cur->bc_flags & XFS_BTREE_LONG_PTRS) ?
428 sizeof(__be64) : sizeof(__be32);
432 * Calculate offset of the n-th record in a btree block.
435 xfs_btree_rec_offset(
436 struct xfs_btree_cur *cur,
439 return xfs_btree_block_len(cur) +
440 (n - 1) * cur->bc_ops->rec_len;
444 * Calculate offset of the n-th key in a btree block.
447 xfs_btree_key_offset(
448 struct xfs_btree_cur *cur,
451 return xfs_btree_block_len(cur) +
452 (n - 1) * cur->bc_ops->key_len;
456 * Calculate offset of the n-th block pointer in a btree block.
459 xfs_btree_ptr_offset(
460 struct xfs_btree_cur *cur,
464 return xfs_btree_block_len(cur) +
465 cur->bc_ops->get_maxrecs(cur, level) * cur->bc_ops->key_len +
466 (n - 1) * xfs_btree_ptr_len(cur);
470 * Return a pointer to the n-th record in the btree block.
472 STATIC union xfs_btree_rec *
474 struct xfs_btree_cur *cur,
476 struct xfs_btree_block *block)
478 return (union xfs_btree_rec *)
479 ((char *)block + xfs_btree_rec_offset(cur, n));
483 * Return a pointer to the n-th key in the btree block.
485 STATIC union xfs_btree_key *
487 struct xfs_btree_cur *cur,
489 struct xfs_btree_block *block)
491 return (union xfs_btree_key *)
492 ((char *)block + xfs_btree_key_offset(cur, n));
496 * Return a pointer to the n-th block pointer in the btree block.
498 STATIC union xfs_btree_ptr *
500 struct xfs_btree_cur *cur,
502 struct xfs_btree_block *block)
504 int level = xfs_btree_get_level(block);
506 ASSERT(block->bb_level != 0);
508 return (union xfs_btree_ptr *)
509 ((char *)block + xfs_btree_ptr_offset(cur, n, level));
513 * Get the root block which is stored in the inode.
515 * For now this btree implementation assumes the btree root is always
516 * stored in the if_broot field of an inode fork.
518 STATIC struct xfs_btree_block *
520 struct xfs_btree_cur *cur)
522 struct xfs_ifork *ifp;
524 ifp = XFS_IFORK_PTR(cur->bc_private.b.ip, cur->bc_private.b.whichfork);
525 return (struct xfs_btree_block *)ifp->if_broot;
529 * Retrieve the block pointer from the cursor at the given level.
530 * This may be an inode btree root or from a buffer.
532 STATIC struct xfs_btree_block * /* generic btree block pointer */
534 struct xfs_btree_cur *cur, /* btree cursor */
535 int level, /* level in btree */
536 struct xfs_buf **bpp) /* buffer containing the block */
538 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
539 (level == cur->bc_nlevels - 1)) {
541 return xfs_btree_get_iroot(cur);
544 *bpp = cur->bc_bufs[level];
545 return XFS_BUF_TO_BLOCK(*bpp);
549 * Get a buffer for the block, return it with no data read.
550 * Long-form addressing.
552 xfs_buf_t * /* buffer for fsbno */
554 xfs_mount_t *mp, /* file system mount point */
555 xfs_trans_t *tp, /* transaction pointer */
556 xfs_fsblock_t fsbno, /* file system block number */
557 uint lock) /* lock flags for get_buf */
559 xfs_buf_t *bp; /* buffer pointer (return value) */
560 xfs_daddr_t d; /* real disk block address */
562 ASSERT(fsbno != NULLFSBLOCK);
563 d = XFS_FSB_TO_DADDR(mp, fsbno);
564 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d, mp->m_bsize, lock);
565 ASSERT(!xfs_buf_geterror(bp));
570 * Get a buffer for the block, return it with no data read.
571 * Short-form addressing.
573 xfs_buf_t * /* buffer for agno/agbno */
575 xfs_mount_t *mp, /* file system mount point */
576 xfs_trans_t *tp, /* transaction pointer */
577 xfs_agnumber_t agno, /* allocation group number */
578 xfs_agblock_t agbno, /* allocation group block number */
579 uint lock) /* lock flags for get_buf */
581 xfs_buf_t *bp; /* buffer pointer (return value) */
582 xfs_daddr_t d; /* real disk block address */
584 ASSERT(agno != NULLAGNUMBER);
585 ASSERT(agbno != NULLAGBLOCK);
586 d = XFS_AGB_TO_DADDR(mp, agno, agbno);
587 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d, mp->m_bsize, lock);
588 ASSERT(!xfs_buf_geterror(bp));
593 * Check for the cursor referring to the last block at the given level.
595 int /* 1=is last block, 0=not last block */
596 xfs_btree_islastblock(
597 xfs_btree_cur_t *cur, /* btree cursor */
598 int level) /* level to check */
600 struct xfs_btree_block *block; /* generic btree block pointer */
601 xfs_buf_t *bp; /* buffer containing block */
603 block = xfs_btree_get_block(cur, level, &bp);
604 xfs_btree_check_block(cur, block, level, bp);
605 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
606 return block->bb_u.l.bb_rightsib == cpu_to_be64(NULLDFSBNO);
608 return block->bb_u.s.bb_rightsib == cpu_to_be32(NULLAGBLOCK);
612 * Change the cursor to point to the first record at the given level.
613 * Other levels are unaffected.
615 STATIC int /* success=1, failure=0 */
617 xfs_btree_cur_t *cur, /* btree cursor */
618 int level) /* level to change */
620 struct xfs_btree_block *block; /* generic btree block pointer */
621 xfs_buf_t *bp; /* buffer containing block */
624 * Get the block pointer for this level.
626 block = xfs_btree_get_block(cur, level, &bp);
627 xfs_btree_check_block(cur, block, level, bp);
629 * It's empty, there is no such record.
631 if (!block->bb_numrecs)
634 * Set the ptr value to 1, that's the first record/key.
636 cur->bc_ptrs[level] = 1;
641 * Change the cursor to point to the last record in the current block
642 * at the given level. Other levels are unaffected.
644 STATIC int /* success=1, failure=0 */
646 xfs_btree_cur_t *cur, /* btree cursor */
647 int level) /* level to change */
649 struct xfs_btree_block *block; /* generic btree block pointer */
650 xfs_buf_t *bp; /* buffer containing block */
653 * Get the block pointer for this level.
655 block = xfs_btree_get_block(cur, level, &bp);
656 xfs_btree_check_block(cur, block, level, bp);
658 * It's empty, there is no such record.
660 if (!block->bb_numrecs)
663 * Set the ptr value to numrecs, that's the last record/key.
665 cur->bc_ptrs[level] = be16_to_cpu(block->bb_numrecs);
670 * Compute first and last byte offsets for the fields given.
671 * Interprets the offsets table, which contains struct field offsets.
675 __int64_t fields, /* bitmask of fields */
676 const short *offsets, /* table of field offsets */
677 int nbits, /* number of bits to inspect */
678 int *first, /* output: first byte offset */
679 int *last) /* output: last byte offset */
681 int i; /* current bit number */
682 __int64_t imask; /* mask for current bit number */
686 * Find the lowest bit, so the first byte offset.
688 for (i = 0, imask = 1LL; ; i++, imask <<= 1) {
689 if (imask & fields) {
695 * Find the highest bit, so the last byte offset.
697 for (i = nbits - 1, imask = 1LL << i; ; i--, imask >>= 1) {
698 if (imask & fields) {
699 *last = offsets[i + 1] - 1;
706 * Get a buffer for the block, return it read in.
707 * Long-form addressing.
711 struct xfs_mount *mp, /* file system mount point */
712 struct xfs_trans *tp, /* transaction pointer */
713 xfs_fsblock_t fsbno, /* file system block number */
714 uint lock, /* lock flags for read_buf */
715 struct xfs_buf **bpp, /* buffer for fsbno */
716 int refval, /* ref count value for buffer */
717 const struct xfs_buf_ops *ops)
719 struct xfs_buf *bp; /* return value */
720 xfs_daddr_t d; /* real disk block address */
723 ASSERT(fsbno != NULLFSBLOCK);
724 d = XFS_FSB_TO_DADDR(mp, fsbno);
725 error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, d,
726 mp->m_bsize, lock, &bp, ops);
729 ASSERT(!xfs_buf_geterror(bp));
731 xfs_buf_set_ref(bp, refval);
737 * Read-ahead the block, don't wait for it, don't return a buffer.
738 * Long-form addressing.
742 xfs_btree_reada_bufl(
743 struct xfs_mount *mp, /* file system mount point */
744 xfs_fsblock_t fsbno, /* file system block number */
745 xfs_extlen_t count, /* count of filesystem blocks */
746 const struct xfs_buf_ops *ops)
750 ASSERT(fsbno != NULLFSBLOCK);
751 d = XFS_FSB_TO_DADDR(mp, fsbno);
752 xfs_buf_readahead(mp->m_ddev_targp, d, mp->m_bsize * count, ops);
756 * Read-ahead the block, don't wait for it, don't return a buffer.
757 * Short-form addressing.
761 xfs_btree_reada_bufs(
762 struct xfs_mount *mp, /* file system mount point */
763 xfs_agnumber_t agno, /* allocation group number */
764 xfs_agblock_t agbno, /* allocation group block number */
765 xfs_extlen_t count, /* count of filesystem blocks */
766 const struct xfs_buf_ops *ops)
770 ASSERT(agno != NULLAGNUMBER);
771 ASSERT(agbno != NULLAGBLOCK);
772 d = XFS_AGB_TO_DADDR(mp, agno, agbno);
773 xfs_buf_readahead(mp->m_ddev_targp, d, mp->m_bsize * count, ops);
777 xfs_btree_readahead_lblock(
778 struct xfs_btree_cur *cur,
780 struct xfs_btree_block *block)
783 xfs_dfsbno_t left = be64_to_cpu(block->bb_u.l.bb_leftsib);
784 xfs_dfsbno_t right = be64_to_cpu(block->bb_u.l.bb_rightsib);
786 if ((lr & XFS_BTCUR_LEFTRA) && left != NULLDFSBNO) {
787 xfs_btree_reada_bufl(cur->bc_mp, left, 1,
788 cur->bc_ops->buf_ops);
792 if ((lr & XFS_BTCUR_RIGHTRA) && right != NULLDFSBNO) {
793 xfs_btree_reada_bufl(cur->bc_mp, right, 1,
794 cur->bc_ops->buf_ops);
802 xfs_btree_readahead_sblock(
803 struct xfs_btree_cur *cur,
805 struct xfs_btree_block *block)
808 xfs_agblock_t left = be32_to_cpu(block->bb_u.s.bb_leftsib);
809 xfs_agblock_t right = be32_to_cpu(block->bb_u.s.bb_rightsib);
812 if ((lr & XFS_BTCUR_LEFTRA) && left != NULLAGBLOCK) {
813 xfs_btree_reada_bufs(cur->bc_mp, cur->bc_private.a.agno,
814 left, 1, cur->bc_ops->buf_ops);
818 if ((lr & XFS_BTCUR_RIGHTRA) && right != NULLAGBLOCK) {
819 xfs_btree_reada_bufs(cur->bc_mp, cur->bc_private.a.agno,
820 right, 1, cur->bc_ops->buf_ops);
828 * Read-ahead btree blocks, at the given level.
829 * Bits in lr are set from XFS_BTCUR_{LEFT,RIGHT}RA.
833 struct xfs_btree_cur *cur, /* btree cursor */
834 int lev, /* level in btree */
835 int lr) /* left/right bits */
837 struct xfs_btree_block *block;
840 * No readahead needed if we are at the root level and the
841 * btree root is stored in the inode.
843 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
844 (lev == cur->bc_nlevels - 1))
847 if ((cur->bc_ra[lev] | lr) == cur->bc_ra[lev])
850 cur->bc_ra[lev] |= lr;
851 block = XFS_BUF_TO_BLOCK(cur->bc_bufs[lev]);
853 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
854 return xfs_btree_readahead_lblock(cur, lr, block);
855 return xfs_btree_readahead_sblock(cur, lr, block);
859 xfs_btree_ptr_to_daddr(
860 struct xfs_btree_cur *cur,
861 union xfs_btree_ptr *ptr)
863 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
864 ASSERT(ptr->l != cpu_to_be64(NULLDFSBNO));
866 return XFS_FSB_TO_DADDR(cur->bc_mp, be64_to_cpu(ptr->l));
868 ASSERT(cur->bc_private.a.agno != NULLAGNUMBER);
869 ASSERT(ptr->s != cpu_to_be32(NULLAGBLOCK));
871 return XFS_AGB_TO_DADDR(cur->bc_mp, cur->bc_private.a.agno,
872 be32_to_cpu(ptr->s));
877 * Readahead @count btree blocks at the given @ptr location.
879 * We don't need to care about long or short form btrees here as we have a
880 * method of converting the ptr directly to a daddr available to us.
883 xfs_btree_readahead_ptr(
884 struct xfs_btree_cur *cur,
885 union xfs_btree_ptr *ptr,
888 xfs_buf_readahead(cur->bc_mp->m_ddev_targp,
889 xfs_btree_ptr_to_daddr(cur, ptr),
890 cur->bc_mp->m_bsize * count, cur->bc_ops->buf_ops);
894 * Set the buffer for level "lev" in the cursor to bp, releasing
895 * any previous buffer.
899 xfs_btree_cur_t *cur, /* btree cursor */
900 int lev, /* level in btree */
901 xfs_buf_t *bp) /* new buffer to set */
903 struct xfs_btree_block *b; /* btree block */
905 if (cur->bc_bufs[lev])
906 xfs_trans_brelse(cur->bc_tp, cur->bc_bufs[lev]);
907 cur->bc_bufs[lev] = bp;
910 b = XFS_BUF_TO_BLOCK(bp);
911 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
912 if (b->bb_u.l.bb_leftsib == cpu_to_be64(NULLDFSBNO))
913 cur->bc_ra[lev] |= XFS_BTCUR_LEFTRA;
914 if (b->bb_u.l.bb_rightsib == cpu_to_be64(NULLDFSBNO))
915 cur->bc_ra[lev] |= XFS_BTCUR_RIGHTRA;
917 if (b->bb_u.s.bb_leftsib == cpu_to_be32(NULLAGBLOCK))
918 cur->bc_ra[lev] |= XFS_BTCUR_LEFTRA;
919 if (b->bb_u.s.bb_rightsib == cpu_to_be32(NULLAGBLOCK))
920 cur->bc_ra[lev] |= XFS_BTCUR_RIGHTRA;
925 xfs_btree_ptr_is_null(
926 struct xfs_btree_cur *cur,
927 union xfs_btree_ptr *ptr)
929 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
930 return ptr->l == cpu_to_be64(NULLDFSBNO);
932 return ptr->s == cpu_to_be32(NULLAGBLOCK);
936 xfs_btree_set_ptr_null(
937 struct xfs_btree_cur *cur,
938 union xfs_btree_ptr *ptr)
940 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
941 ptr->l = cpu_to_be64(NULLDFSBNO);
943 ptr->s = cpu_to_be32(NULLAGBLOCK);
947 * Get/set/init sibling pointers
950 xfs_btree_get_sibling(
951 struct xfs_btree_cur *cur,
952 struct xfs_btree_block *block,
953 union xfs_btree_ptr *ptr,
956 ASSERT(lr == XFS_BB_LEFTSIB || lr == XFS_BB_RIGHTSIB);
958 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
959 if (lr == XFS_BB_RIGHTSIB)
960 ptr->l = block->bb_u.l.bb_rightsib;
962 ptr->l = block->bb_u.l.bb_leftsib;
964 if (lr == XFS_BB_RIGHTSIB)
965 ptr->s = block->bb_u.s.bb_rightsib;
967 ptr->s = block->bb_u.s.bb_leftsib;
972 xfs_btree_set_sibling(
973 struct xfs_btree_cur *cur,
974 struct xfs_btree_block *block,
975 union xfs_btree_ptr *ptr,
978 ASSERT(lr == XFS_BB_LEFTSIB || lr == XFS_BB_RIGHTSIB);
980 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
981 if (lr == XFS_BB_RIGHTSIB)
982 block->bb_u.l.bb_rightsib = ptr->l;
984 block->bb_u.l.bb_leftsib = ptr->l;
986 if (lr == XFS_BB_RIGHTSIB)
987 block->bb_u.s.bb_rightsib = ptr->s;
989 block->bb_u.s.bb_leftsib = ptr->s;
994 xfs_btree_init_block_int(
995 struct xfs_mount *mp,
996 struct xfs_btree_block *buf,
1004 buf->bb_magic = cpu_to_be32(magic);
1005 buf->bb_level = cpu_to_be16(level);
1006 buf->bb_numrecs = cpu_to_be16(numrecs);
1008 if (flags & XFS_BTREE_LONG_PTRS) {
1009 buf->bb_u.l.bb_leftsib = cpu_to_be64(NULLDFSBNO);
1010 buf->bb_u.l.bb_rightsib = cpu_to_be64(NULLDFSBNO);
1011 if (flags & XFS_BTREE_CRC_BLOCKS) {
1012 buf->bb_u.l.bb_blkno = cpu_to_be64(blkno);
1013 buf->bb_u.l.bb_owner = cpu_to_be64(owner);
1014 uuid_copy(&buf->bb_u.l.bb_uuid, &mp->m_sb.sb_uuid);
1015 buf->bb_u.l.bb_pad = 0;
1016 buf->bb_u.l.bb_lsn = 0;
1019 /* owner is a 32 bit value on short blocks */
1020 __u32 __owner = (__u32)owner;
1022 buf->bb_u.s.bb_leftsib = cpu_to_be32(NULLAGBLOCK);
1023 buf->bb_u.s.bb_rightsib = cpu_to_be32(NULLAGBLOCK);
1024 if (flags & XFS_BTREE_CRC_BLOCKS) {
1025 buf->bb_u.s.bb_blkno = cpu_to_be64(blkno);
1026 buf->bb_u.s.bb_owner = cpu_to_be32(__owner);
1027 uuid_copy(&buf->bb_u.s.bb_uuid, &mp->m_sb.sb_uuid);
1028 buf->bb_u.s.bb_lsn = 0;
1034 xfs_btree_init_block(
1035 struct xfs_mount *mp,
1043 xfs_btree_init_block_int(mp, XFS_BUF_TO_BLOCK(bp), bp->b_bn,
1044 magic, level, numrecs, owner, flags);
1048 xfs_btree_init_block_cur(
1049 struct xfs_btree_cur *cur,
1057 * we can pull the owner from the cursor right now as the different
1058 * owners align directly with the pointer size of the btree. This may
1059 * change in future, but is safe for current users of the generic btree
1062 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
1063 owner = cur->bc_private.b.ip->i_ino;
1065 owner = cur->bc_private.a.agno;
1067 xfs_btree_init_block_int(cur->bc_mp, XFS_BUF_TO_BLOCK(bp), bp->b_bn,
1068 xfs_btree_magic(cur), level, numrecs,
1069 owner, cur->bc_flags);
1073 * Return true if ptr is the last record in the btree and
1074 * we need to track updates to this record. The decision
1075 * will be further refined in the update_lastrec method.
1078 xfs_btree_is_lastrec(
1079 struct xfs_btree_cur *cur,
1080 struct xfs_btree_block *block,
1083 union xfs_btree_ptr ptr;
1087 if (!(cur->bc_flags & XFS_BTREE_LASTREC_UPDATE))
1090 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_RIGHTSIB);
1091 if (!xfs_btree_ptr_is_null(cur, &ptr))
1097 xfs_btree_buf_to_ptr(
1098 struct xfs_btree_cur *cur,
1100 union xfs_btree_ptr *ptr)
1102 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
1103 ptr->l = cpu_to_be64(XFS_DADDR_TO_FSB(cur->bc_mp,
1106 ptr->s = cpu_to_be32(xfs_daddr_to_agbno(cur->bc_mp,
1113 struct xfs_btree_cur *cur,
1116 switch (cur->bc_btnum) {
1119 xfs_buf_set_ref(bp, XFS_ALLOC_BTREE_REF);
1122 xfs_buf_set_ref(bp, XFS_INO_BTREE_REF);
1124 case XFS_BTNUM_BMAP:
1125 xfs_buf_set_ref(bp, XFS_BMAP_BTREE_REF);
1133 xfs_btree_get_buf_block(
1134 struct xfs_btree_cur *cur,
1135 union xfs_btree_ptr *ptr,
1137 struct xfs_btree_block **block,
1138 struct xfs_buf **bpp)
1140 struct xfs_mount *mp = cur->bc_mp;
1143 /* need to sort out how callers deal with failures first */
1144 ASSERT(!(flags & XBF_TRYLOCK));
1146 d = xfs_btree_ptr_to_daddr(cur, ptr);
1147 *bpp = xfs_trans_get_buf(cur->bc_tp, mp->m_ddev_targp, d,
1148 mp->m_bsize, flags);
1153 (*bpp)->b_ops = cur->bc_ops->buf_ops;
1154 *block = XFS_BUF_TO_BLOCK(*bpp);
1159 * Read in the buffer at the given ptr and return the buffer and
1160 * the block pointer within the buffer.
1163 xfs_btree_read_buf_block(
1164 struct xfs_btree_cur *cur,
1165 union xfs_btree_ptr *ptr,
1168 struct xfs_btree_block **block,
1169 struct xfs_buf **bpp)
1171 struct xfs_mount *mp = cur->bc_mp;
1175 /* need to sort out how callers deal with failures first */
1176 ASSERT(!(flags & XBF_TRYLOCK));
1178 d = xfs_btree_ptr_to_daddr(cur, ptr);
1179 error = xfs_trans_read_buf(mp, cur->bc_tp, mp->m_ddev_targp, d,
1180 mp->m_bsize, flags, bpp,
1181 cur->bc_ops->buf_ops);
1185 ASSERT(!xfs_buf_geterror(*bpp));
1186 xfs_btree_set_refs(cur, *bpp);
1187 *block = XFS_BUF_TO_BLOCK(*bpp);
1192 * Copy keys from one btree block to another.
1195 xfs_btree_copy_keys(
1196 struct xfs_btree_cur *cur,
1197 union xfs_btree_key *dst_key,
1198 union xfs_btree_key *src_key,
1201 ASSERT(numkeys >= 0);
1202 memcpy(dst_key, src_key, numkeys * cur->bc_ops->key_len);
1206 * Copy records from one btree block to another.
1209 xfs_btree_copy_recs(
1210 struct xfs_btree_cur *cur,
1211 union xfs_btree_rec *dst_rec,
1212 union xfs_btree_rec *src_rec,
1215 ASSERT(numrecs >= 0);
1216 memcpy(dst_rec, src_rec, numrecs * cur->bc_ops->rec_len);
1220 * Copy block pointers from one btree block to another.
1223 xfs_btree_copy_ptrs(
1224 struct xfs_btree_cur *cur,
1225 union xfs_btree_ptr *dst_ptr,
1226 union xfs_btree_ptr *src_ptr,
1229 ASSERT(numptrs >= 0);
1230 memcpy(dst_ptr, src_ptr, numptrs * xfs_btree_ptr_len(cur));
1234 * Shift keys one index left/right inside a single btree block.
1237 xfs_btree_shift_keys(
1238 struct xfs_btree_cur *cur,
1239 union xfs_btree_key *key,
1245 ASSERT(numkeys >= 0);
1246 ASSERT(dir == 1 || dir == -1);
1248 dst_key = (char *)key + (dir * cur->bc_ops->key_len);
1249 memmove(dst_key, key, numkeys * cur->bc_ops->key_len);
1253 * Shift records one index left/right inside a single btree block.
1256 xfs_btree_shift_recs(
1257 struct xfs_btree_cur *cur,
1258 union xfs_btree_rec *rec,
1264 ASSERT(numrecs >= 0);
1265 ASSERT(dir == 1 || dir == -1);
1267 dst_rec = (char *)rec + (dir * cur->bc_ops->rec_len);
1268 memmove(dst_rec, rec, numrecs * cur->bc_ops->rec_len);
1272 * Shift block pointers one index left/right inside a single btree block.
1275 xfs_btree_shift_ptrs(
1276 struct xfs_btree_cur *cur,
1277 union xfs_btree_ptr *ptr,
1283 ASSERT(numptrs >= 0);
1284 ASSERT(dir == 1 || dir == -1);
1286 dst_ptr = (char *)ptr + (dir * xfs_btree_ptr_len(cur));
1287 memmove(dst_ptr, ptr, numptrs * xfs_btree_ptr_len(cur));
1291 * Log key values from the btree block.
1295 struct xfs_btree_cur *cur,
1300 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1301 XFS_BTREE_TRACE_ARGBII(cur, bp, first, last);
1304 xfs_trans_buf_set_type(cur->bc_tp, bp, XFS_BLFT_BTREE_BUF);
1305 xfs_trans_log_buf(cur->bc_tp, bp,
1306 xfs_btree_key_offset(cur, first),
1307 xfs_btree_key_offset(cur, last + 1) - 1);
1309 xfs_trans_log_inode(cur->bc_tp, cur->bc_private.b.ip,
1310 xfs_ilog_fbroot(cur->bc_private.b.whichfork));
1313 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1317 * Log record values from the btree block.
1321 struct xfs_btree_cur *cur,
1326 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1327 XFS_BTREE_TRACE_ARGBII(cur, bp, first, last);
1329 xfs_trans_buf_set_type(cur->bc_tp, bp, XFS_BLFT_BTREE_BUF);
1330 xfs_trans_log_buf(cur->bc_tp, bp,
1331 xfs_btree_rec_offset(cur, first),
1332 xfs_btree_rec_offset(cur, last + 1) - 1);
1334 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1338 * Log block pointer fields from a btree block (nonleaf).
1342 struct xfs_btree_cur *cur, /* btree cursor */
1343 struct xfs_buf *bp, /* buffer containing btree block */
1344 int first, /* index of first pointer to log */
1345 int last) /* index of last pointer to log */
1347 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1348 XFS_BTREE_TRACE_ARGBII(cur, bp, first, last);
1351 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
1352 int level = xfs_btree_get_level(block);
1354 xfs_trans_buf_set_type(cur->bc_tp, bp, XFS_BLFT_BTREE_BUF);
1355 xfs_trans_log_buf(cur->bc_tp, bp,
1356 xfs_btree_ptr_offset(cur, first, level),
1357 xfs_btree_ptr_offset(cur, last + 1, level) - 1);
1359 xfs_trans_log_inode(cur->bc_tp, cur->bc_private.b.ip,
1360 xfs_ilog_fbroot(cur->bc_private.b.whichfork));
1363 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1367 * Log fields from a btree block header.
1370 xfs_btree_log_block(
1371 struct xfs_btree_cur *cur, /* btree cursor */
1372 struct xfs_buf *bp, /* buffer containing btree block */
1373 int fields) /* mask of fields: XFS_BB_... */
1375 int first; /* first byte offset logged */
1376 int last; /* last byte offset logged */
1377 static const short soffsets[] = { /* table of offsets (short) */
1378 offsetof(struct xfs_btree_block, bb_magic),
1379 offsetof(struct xfs_btree_block, bb_level),
1380 offsetof(struct xfs_btree_block, bb_numrecs),
1381 offsetof(struct xfs_btree_block, bb_u.s.bb_leftsib),
1382 offsetof(struct xfs_btree_block, bb_u.s.bb_rightsib),
1383 offsetof(struct xfs_btree_block, bb_u.s.bb_blkno),
1384 offsetof(struct xfs_btree_block, bb_u.s.bb_lsn),
1385 offsetof(struct xfs_btree_block, bb_u.s.bb_uuid),
1386 offsetof(struct xfs_btree_block, bb_u.s.bb_owner),
1387 offsetof(struct xfs_btree_block, bb_u.s.bb_crc),
1388 XFS_BTREE_SBLOCK_CRC_LEN
1390 static const short loffsets[] = { /* table of offsets (long) */
1391 offsetof(struct xfs_btree_block, bb_magic),
1392 offsetof(struct xfs_btree_block, bb_level),
1393 offsetof(struct xfs_btree_block, bb_numrecs),
1394 offsetof(struct xfs_btree_block, bb_u.l.bb_leftsib),
1395 offsetof(struct xfs_btree_block, bb_u.l.bb_rightsib),
1396 offsetof(struct xfs_btree_block, bb_u.l.bb_blkno),
1397 offsetof(struct xfs_btree_block, bb_u.l.bb_lsn),
1398 offsetof(struct xfs_btree_block, bb_u.l.bb_uuid),
1399 offsetof(struct xfs_btree_block, bb_u.l.bb_owner),
1400 offsetof(struct xfs_btree_block, bb_u.l.bb_crc),
1401 offsetof(struct xfs_btree_block, bb_u.l.bb_pad),
1402 XFS_BTREE_LBLOCK_CRC_LEN
1405 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1406 XFS_BTREE_TRACE_ARGBI(cur, bp, fields);
1411 if (cur->bc_flags & XFS_BTREE_CRC_BLOCKS) {
1413 * We don't log the CRC when updating a btree
1414 * block but instead recreate it during log
1415 * recovery. As the log buffers have checksums
1416 * of their own this is safe and avoids logging a crc
1417 * update in a lot of places.
1419 if (fields == XFS_BB_ALL_BITS)
1420 fields = XFS_BB_ALL_BITS_CRC;
1421 nbits = XFS_BB_NUM_BITS_CRC;
1423 nbits = XFS_BB_NUM_BITS;
1425 xfs_btree_offsets(fields,
1426 (cur->bc_flags & XFS_BTREE_LONG_PTRS) ?
1427 loffsets : soffsets,
1428 nbits, &first, &last);
1429 xfs_trans_buf_set_type(cur->bc_tp, bp, XFS_BLFT_BTREE_BUF);
1430 xfs_trans_log_buf(cur->bc_tp, bp, first, last);
1432 xfs_trans_log_inode(cur->bc_tp, cur->bc_private.b.ip,
1433 xfs_ilog_fbroot(cur->bc_private.b.whichfork));
1436 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1440 * Increment cursor by one record at the level.
1441 * For nonzero levels the leaf-ward information is untouched.
1444 xfs_btree_increment(
1445 struct xfs_btree_cur *cur,
1447 int *stat) /* success/failure */
1449 struct xfs_btree_block *block;
1450 union xfs_btree_ptr ptr;
1452 int error; /* error return value */
1455 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1456 XFS_BTREE_TRACE_ARGI(cur, level);
1458 ASSERT(level < cur->bc_nlevels);
1460 /* Read-ahead to the right at this level. */
1461 xfs_btree_readahead(cur, level, XFS_BTCUR_RIGHTRA);
1463 /* Get a pointer to the btree block. */
1464 block = xfs_btree_get_block(cur, level, &bp);
1467 error = xfs_btree_check_block(cur, block, level, bp);
1472 /* We're done if we remain in the block after the increment. */
1473 if (++cur->bc_ptrs[level] <= xfs_btree_get_numrecs(block))
1476 /* Fail if we just went off the right edge of the tree. */
1477 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_RIGHTSIB);
1478 if (xfs_btree_ptr_is_null(cur, &ptr))
1481 XFS_BTREE_STATS_INC(cur, increment);
1484 * March up the tree incrementing pointers.
1485 * Stop when we don't go off the right edge of a block.
1487 for (lev = level + 1; lev < cur->bc_nlevels; lev++) {
1488 block = xfs_btree_get_block(cur, lev, &bp);
1491 error = xfs_btree_check_block(cur, block, lev, bp);
1496 if (++cur->bc_ptrs[lev] <= xfs_btree_get_numrecs(block))
1499 /* Read-ahead the right block for the next loop. */
1500 xfs_btree_readahead(cur, lev, XFS_BTCUR_RIGHTRA);
1504 * If we went off the root then we are either seriously
1505 * confused or have the tree root in an inode.
1507 if (lev == cur->bc_nlevels) {
1508 if (cur->bc_flags & XFS_BTREE_ROOT_IN_INODE)
1511 error = EFSCORRUPTED;
1514 ASSERT(lev < cur->bc_nlevels);
1517 * Now walk back down the tree, fixing up the cursor's buffer
1518 * pointers and key numbers.
1520 for (block = xfs_btree_get_block(cur, lev, &bp); lev > level; ) {
1521 union xfs_btree_ptr *ptrp;
1523 ptrp = xfs_btree_ptr_addr(cur, cur->bc_ptrs[lev], block);
1524 error = xfs_btree_read_buf_block(cur, ptrp, --lev,
1529 xfs_btree_setbuf(cur, lev, bp);
1530 cur->bc_ptrs[lev] = 1;
1533 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1538 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1543 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
1548 * Decrement cursor by one record at the level.
1549 * For nonzero levels the leaf-ward information is untouched.
1552 xfs_btree_decrement(
1553 struct xfs_btree_cur *cur,
1555 int *stat) /* success/failure */
1557 struct xfs_btree_block *block;
1559 int error; /* error return value */
1561 union xfs_btree_ptr ptr;
1563 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1564 XFS_BTREE_TRACE_ARGI(cur, level);
1566 ASSERT(level < cur->bc_nlevels);
1568 /* Read-ahead to the left at this level. */
1569 xfs_btree_readahead(cur, level, XFS_BTCUR_LEFTRA);
1571 /* We're done if we remain in the block after the decrement. */
1572 if (--cur->bc_ptrs[level] > 0)
1575 /* Get a pointer to the btree block. */
1576 block = xfs_btree_get_block(cur, level, &bp);
1579 error = xfs_btree_check_block(cur, block, level, bp);
1584 /* Fail if we just went off the left edge of the tree. */
1585 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_LEFTSIB);
1586 if (xfs_btree_ptr_is_null(cur, &ptr))
1589 XFS_BTREE_STATS_INC(cur, decrement);
1592 * March up the tree decrementing pointers.
1593 * Stop when we don't go off the left edge of a block.
1595 for (lev = level + 1; lev < cur->bc_nlevels; lev++) {
1596 if (--cur->bc_ptrs[lev] > 0)
1598 /* Read-ahead the left block for the next loop. */
1599 xfs_btree_readahead(cur, lev, XFS_BTCUR_LEFTRA);
1603 * If we went off the root then we are seriously confused.
1604 * or the root of the tree is in an inode.
1606 if (lev == cur->bc_nlevels) {
1607 if (cur->bc_flags & XFS_BTREE_ROOT_IN_INODE)
1610 error = EFSCORRUPTED;
1613 ASSERT(lev < cur->bc_nlevels);
1616 * Now walk back down the tree, fixing up the cursor's buffer
1617 * pointers and key numbers.
1619 for (block = xfs_btree_get_block(cur, lev, &bp); lev > level; ) {
1620 union xfs_btree_ptr *ptrp;
1622 ptrp = xfs_btree_ptr_addr(cur, cur->bc_ptrs[lev], block);
1623 error = xfs_btree_read_buf_block(cur, ptrp, --lev,
1627 xfs_btree_setbuf(cur, lev, bp);
1628 cur->bc_ptrs[lev] = xfs_btree_get_numrecs(block);
1631 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1636 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1641 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
1646 xfs_btree_lookup_get_block(
1647 struct xfs_btree_cur *cur, /* btree cursor */
1648 int level, /* level in the btree */
1649 union xfs_btree_ptr *pp, /* ptr to btree block */
1650 struct xfs_btree_block **blkp) /* return btree block */
1652 struct xfs_buf *bp; /* buffer pointer for btree block */
1655 /* special case the root block if in an inode */
1656 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
1657 (level == cur->bc_nlevels - 1)) {
1658 *blkp = xfs_btree_get_iroot(cur);
1663 * If the old buffer at this level for the disk address we are
1664 * looking for re-use it.
1666 * Otherwise throw it away and get a new one.
1668 bp = cur->bc_bufs[level];
1669 if (bp && XFS_BUF_ADDR(bp) == xfs_btree_ptr_to_daddr(cur, pp)) {
1670 *blkp = XFS_BUF_TO_BLOCK(bp);
1674 error = xfs_btree_read_buf_block(cur, pp, level, 0, blkp, &bp);
1678 xfs_btree_setbuf(cur, level, bp);
1683 * Get current search key. For level 0 we don't actually have a key
1684 * structure so we make one up from the record. For all other levels
1685 * we just return the right key.
1687 STATIC union xfs_btree_key *
1688 xfs_lookup_get_search_key(
1689 struct xfs_btree_cur *cur,
1692 struct xfs_btree_block *block,
1693 union xfs_btree_key *kp)
1696 cur->bc_ops->init_key_from_rec(kp,
1697 xfs_btree_rec_addr(cur, keyno, block));
1701 return xfs_btree_key_addr(cur, keyno, block);
1705 * Lookup the record. The cursor is made to point to it, based on dir.
1706 * stat is set to 0 if can't find any such record, 1 for success.
1710 struct xfs_btree_cur *cur, /* btree cursor */
1711 xfs_lookup_t dir, /* <=, ==, or >= */
1712 int *stat) /* success/failure */
1714 struct xfs_btree_block *block; /* current btree block */
1715 __int64_t diff; /* difference for the current key */
1716 int error; /* error return value */
1717 int keyno; /* current key number */
1718 int level; /* level in the btree */
1719 union xfs_btree_ptr *pp; /* ptr to btree block */
1720 union xfs_btree_ptr ptr; /* ptr to btree block */
1722 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1723 XFS_BTREE_TRACE_ARGI(cur, dir);
1725 XFS_BTREE_STATS_INC(cur, lookup);
1730 /* initialise start pointer from cursor */
1731 cur->bc_ops->init_ptr_from_cur(cur, &ptr);
1735 * Iterate over each level in the btree, starting at the root.
1736 * For each level above the leaves, find the key we need, based
1737 * on the lookup record, then follow the corresponding block
1738 * pointer down to the next level.
1740 for (level = cur->bc_nlevels - 1, diff = 1; level >= 0; level--) {
1741 /* Get the block we need to do the lookup on. */
1742 error = xfs_btree_lookup_get_block(cur, level, pp, &block);
1748 * If we already had a key match at a higher level, we
1749 * know we need to use the first entry in this block.
1753 /* Otherwise search this block. Do a binary search. */
1755 int high; /* high entry number */
1756 int low; /* low entry number */
1758 /* Set low and high entry numbers, 1-based. */
1760 high = xfs_btree_get_numrecs(block);
1762 /* Block is empty, must be an empty leaf. */
1763 ASSERT(level == 0 && cur->bc_nlevels == 1);
1765 cur->bc_ptrs[0] = dir != XFS_LOOKUP_LE;
1766 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1771 /* Binary search the block. */
1772 while (low <= high) {
1773 union xfs_btree_key key;
1774 union xfs_btree_key *kp;
1776 XFS_BTREE_STATS_INC(cur, compare);
1778 /* keyno is average of low and high. */
1779 keyno = (low + high) >> 1;
1781 /* Get current search key */
1782 kp = xfs_lookup_get_search_key(cur, level,
1783 keyno, block, &key);
1786 * Compute difference to get next direction:
1787 * - less than, move right
1788 * - greater than, move left
1789 * - equal, we're done
1791 diff = cur->bc_ops->key_diff(cur, kp);
1802 * If there are more levels, set up for the next level
1803 * by getting the block number and filling in the cursor.
1807 * If we moved left, need the previous key number,
1808 * unless there isn't one.
1810 if (diff > 0 && --keyno < 1)
1812 pp = xfs_btree_ptr_addr(cur, keyno, block);
1815 error = xfs_btree_check_ptr(cur, pp, 0, level);
1819 cur->bc_ptrs[level] = keyno;
1823 /* Done with the search. See if we need to adjust the results. */
1824 if (dir != XFS_LOOKUP_LE && diff < 0) {
1827 * If ge search and we went off the end of the block, but it's
1828 * not the last block, we're in the wrong block.
1830 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_RIGHTSIB);
1831 if (dir == XFS_LOOKUP_GE &&
1832 keyno > xfs_btree_get_numrecs(block) &&
1833 !xfs_btree_ptr_is_null(cur, &ptr)) {
1836 cur->bc_ptrs[0] = keyno;
1837 error = xfs_btree_increment(cur, 0, &i);
1840 XFS_WANT_CORRUPTED_RETURN(i == 1);
1841 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1845 } else if (dir == XFS_LOOKUP_LE && diff > 0)
1847 cur->bc_ptrs[0] = keyno;
1849 /* Return if we succeeded or not. */
1850 if (keyno == 0 || keyno > xfs_btree_get_numrecs(block))
1852 else if (dir != XFS_LOOKUP_EQ || diff == 0)
1856 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1860 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
1865 * Update keys at all levels from here to the root along the cursor's path.
1869 struct xfs_btree_cur *cur,
1870 union xfs_btree_key *keyp,
1873 struct xfs_btree_block *block;
1875 union xfs_btree_key *kp;
1878 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1879 XFS_BTREE_TRACE_ARGIK(cur, level, keyp);
1881 ASSERT(!(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) || level >= 1);
1884 * Go up the tree from this level toward the root.
1885 * At each level, update the key value to the value input.
1886 * Stop when we reach a level where the cursor isn't pointing
1887 * at the first entry in the block.
1889 for (ptr = 1; ptr == 1 && level < cur->bc_nlevels; level++) {
1893 block = xfs_btree_get_block(cur, level, &bp);
1895 error = xfs_btree_check_block(cur, block, level, bp);
1897 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
1901 ptr = cur->bc_ptrs[level];
1902 kp = xfs_btree_key_addr(cur, ptr, block);
1903 xfs_btree_copy_keys(cur, kp, keyp, 1);
1904 xfs_btree_log_keys(cur, bp, ptr, ptr);
1907 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1912 * Update the record referred to by cur to the value in the
1913 * given record. This either works (return 0) or gets an
1914 * EFSCORRUPTED error.
1918 struct xfs_btree_cur *cur,
1919 union xfs_btree_rec *rec)
1921 struct xfs_btree_block *block;
1925 union xfs_btree_rec *rp;
1927 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1928 XFS_BTREE_TRACE_ARGR(cur, rec);
1930 /* Pick up the current block. */
1931 block = xfs_btree_get_block(cur, 0, &bp);
1934 error = xfs_btree_check_block(cur, block, 0, bp);
1938 /* Get the address of the rec to be updated. */
1939 ptr = cur->bc_ptrs[0];
1940 rp = xfs_btree_rec_addr(cur, ptr, block);
1942 /* Fill in the new contents and log them. */
1943 xfs_btree_copy_recs(cur, rp, rec, 1);
1944 xfs_btree_log_recs(cur, bp, ptr, ptr);
1947 * If we are tracking the last record in the tree and
1948 * we are at the far right edge of the tree, update it.
1950 if (xfs_btree_is_lastrec(cur, block, 0)) {
1951 cur->bc_ops->update_lastrec(cur, block, rec,
1952 ptr, LASTREC_UPDATE);
1955 /* Updating first rec in leaf. Pass new key value up to our parent. */
1957 union xfs_btree_key key;
1959 cur->bc_ops->init_key_from_rec(&key, rec);
1960 error = xfs_btree_updkey(cur, &key, 1);
1965 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1969 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
1974 * Move 1 record left from cur/level if possible.
1975 * Update cur to reflect the new path.
1977 STATIC int /* error */
1979 struct xfs_btree_cur *cur,
1981 int *stat) /* success/failure */
1983 union xfs_btree_key key; /* btree key */
1984 struct xfs_buf *lbp; /* left buffer pointer */
1985 struct xfs_btree_block *left; /* left btree block */
1986 int lrecs; /* left record count */
1987 struct xfs_buf *rbp; /* right buffer pointer */
1988 struct xfs_btree_block *right; /* right btree block */
1989 int rrecs; /* right record count */
1990 union xfs_btree_ptr lptr; /* left btree pointer */
1991 union xfs_btree_key *rkp = NULL; /* right btree key */
1992 union xfs_btree_ptr *rpp = NULL; /* right address pointer */
1993 union xfs_btree_rec *rrp = NULL; /* right record pointer */
1994 int error; /* error return value */
1996 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1997 XFS_BTREE_TRACE_ARGI(cur, level);
1999 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
2000 level == cur->bc_nlevels - 1)
2003 /* Set up variables for this block as "right". */
2004 right = xfs_btree_get_block(cur, level, &rbp);
2007 error = xfs_btree_check_block(cur, right, level, rbp);
2012 /* If we've got no left sibling then we can't shift an entry left. */
2013 xfs_btree_get_sibling(cur, right, &lptr, XFS_BB_LEFTSIB);
2014 if (xfs_btree_ptr_is_null(cur, &lptr))
2018 * If the cursor entry is the one that would be moved, don't
2019 * do it... it's too complicated.
2021 if (cur->bc_ptrs[level] <= 1)
2024 /* Set up the left neighbor as "left". */
2025 error = xfs_btree_read_buf_block(cur, &lptr, level, 0, &left, &lbp);
2029 /* If it's full, it can't take another entry. */
2030 lrecs = xfs_btree_get_numrecs(left);
2031 if (lrecs == cur->bc_ops->get_maxrecs(cur, level))
2034 rrecs = xfs_btree_get_numrecs(right);
2037 * We add one entry to the left side and remove one for the right side.
2038 * Account for it here, the changes will be updated on disk and logged
2044 XFS_BTREE_STATS_INC(cur, lshift);
2045 XFS_BTREE_STATS_ADD(cur, moves, 1);
2048 * If non-leaf, copy a key and a ptr to the left block.
2049 * Log the changes to the left block.
2052 /* It's a non-leaf. Move keys and pointers. */
2053 union xfs_btree_key *lkp; /* left btree key */
2054 union xfs_btree_ptr *lpp; /* left address pointer */
2056 lkp = xfs_btree_key_addr(cur, lrecs, left);
2057 rkp = xfs_btree_key_addr(cur, 1, right);
2059 lpp = xfs_btree_ptr_addr(cur, lrecs, left);
2060 rpp = xfs_btree_ptr_addr(cur, 1, right);
2062 error = xfs_btree_check_ptr(cur, rpp, 0, level);
2066 xfs_btree_copy_keys(cur, lkp, rkp, 1);
2067 xfs_btree_copy_ptrs(cur, lpp, rpp, 1);
2069 xfs_btree_log_keys(cur, lbp, lrecs, lrecs);
2070 xfs_btree_log_ptrs(cur, lbp, lrecs, lrecs);
2072 ASSERT(cur->bc_ops->keys_inorder(cur,
2073 xfs_btree_key_addr(cur, lrecs - 1, left), lkp));
2075 /* It's a leaf. Move records. */
2076 union xfs_btree_rec *lrp; /* left record pointer */
2078 lrp = xfs_btree_rec_addr(cur, lrecs, left);
2079 rrp = xfs_btree_rec_addr(cur, 1, right);
2081 xfs_btree_copy_recs(cur, lrp, rrp, 1);
2082 xfs_btree_log_recs(cur, lbp, lrecs, lrecs);
2084 ASSERT(cur->bc_ops->recs_inorder(cur,
2085 xfs_btree_rec_addr(cur, lrecs - 1, left), lrp));
2088 xfs_btree_set_numrecs(left, lrecs);
2089 xfs_btree_log_block(cur, lbp, XFS_BB_NUMRECS);
2091 xfs_btree_set_numrecs(right, rrecs);
2092 xfs_btree_log_block(cur, rbp, XFS_BB_NUMRECS);
2095 * Slide the contents of right down one entry.
2097 XFS_BTREE_STATS_ADD(cur, moves, rrecs - 1);
2099 /* It's a nonleaf. operate on keys and ptrs */
2101 int i; /* loop index */
2103 for (i = 0; i < rrecs; i++) {
2104 error = xfs_btree_check_ptr(cur, rpp, i + 1, level);
2109 xfs_btree_shift_keys(cur,
2110 xfs_btree_key_addr(cur, 2, right),
2112 xfs_btree_shift_ptrs(cur,
2113 xfs_btree_ptr_addr(cur, 2, right),
2116 xfs_btree_log_keys(cur, rbp, 1, rrecs);
2117 xfs_btree_log_ptrs(cur, rbp, 1, rrecs);
2119 /* It's a leaf. operate on records */
2120 xfs_btree_shift_recs(cur,
2121 xfs_btree_rec_addr(cur, 2, right),
2123 xfs_btree_log_recs(cur, rbp, 1, rrecs);
2126 * If it's the first record in the block, we'll need a key
2127 * structure to pass up to the next level (updkey).
2129 cur->bc_ops->init_key_from_rec(&key,
2130 xfs_btree_rec_addr(cur, 1, right));
2134 /* Update the parent key values of right. */
2135 error = xfs_btree_updkey(cur, rkp, level + 1);
2139 /* Slide the cursor value left one. */
2140 cur->bc_ptrs[level]--;
2142 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2147 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2152 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
2157 * Move 1 record right from cur/level if possible.
2158 * Update cur to reflect the new path.
2160 STATIC int /* error */
2162 struct xfs_btree_cur *cur,
2164 int *stat) /* success/failure */
2166 union xfs_btree_key key; /* btree key */
2167 struct xfs_buf *lbp; /* left buffer pointer */
2168 struct xfs_btree_block *left; /* left btree block */
2169 struct xfs_buf *rbp; /* right buffer pointer */
2170 struct xfs_btree_block *right; /* right btree block */
2171 struct xfs_btree_cur *tcur; /* temporary btree cursor */
2172 union xfs_btree_ptr rptr; /* right block pointer */
2173 union xfs_btree_key *rkp; /* right btree key */
2174 int rrecs; /* right record count */
2175 int lrecs; /* left record count */
2176 int error; /* error return value */
2177 int i; /* loop counter */
2179 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
2180 XFS_BTREE_TRACE_ARGI(cur, level);
2182 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
2183 (level == cur->bc_nlevels - 1))
2186 /* Set up variables for this block as "left". */
2187 left = xfs_btree_get_block(cur, level, &lbp);
2190 error = xfs_btree_check_block(cur, left, level, lbp);
2195 /* If we've got no right sibling then we can't shift an entry right. */
2196 xfs_btree_get_sibling(cur, left, &rptr, XFS_BB_RIGHTSIB);
2197 if (xfs_btree_ptr_is_null(cur, &rptr))
2201 * If the cursor entry is the one that would be moved, don't
2202 * do it... it's too complicated.
2204 lrecs = xfs_btree_get_numrecs(left);
2205 if (cur->bc_ptrs[level] >= lrecs)
2208 /* Set up the right neighbor as "right". */
2209 error = xfs_btree_read_buf_block(cur, &rptr, level, 0, &right, &rbp);
2213 /* If it's full, it can't take another entry. */
2214 rrecs = xfs_btree_get_numrecs(right);
2215 if (rrecs == cur->bc_ops->get_maxrecs(cur, level))
2218 XFS_BTREE_STATS_INC(cur, rshift);
2219 XFS_BTREE_STATS_ADD(cur, moves, rrecs);
2222 * Make a hole at the start of the right neighbor block, then
2223 * copy the last left block entry to the hole.
2226 /* It's a nonleaf. make a hole in the keys and ptrs */
2227 union xfs_btree_key *lkp;
2228 union xfs_btree_ptr *lpp;
2229 union xfs_btree_ptr *rpp;
2231 lkp = xfs_btree_key_addr(cur, lrecs, left);
2232 lpp = xfs_btree_ptr_addr(cur, lrecs, left);
2233 rkp = xfs_btree_key_addr(cur, 1, right);
2234 rpp = xfs_btree_ptr_addr(cur, 1, right);
2237 for (i = rrecs - 1; i >= 0; i--) {
2238 error = xfs_btree_check_ptr(cur, rpp, i, level);
2244 xfs_btree_shift_keys(cur, rkp, 1, rrecs);
2245 xfs_btree_shift_ptrs(cur, rpp, 1, rrecs);
2248 error = xfs_btree_check_ptr(cur, lpp, 0, level);
2253 /* Now put the new data in, and log it. */
2254 xfs_btree_copy_keys(cur, rkp, lkp, 1);
2255 xfs_btree_copy_ptrs(cur, rpp, lpp, 1);
2257 xfs_btree_log_keys(cur, rbp, 1, rrecs + 1);
2258 xfs_btree_log_ptrs(cur, rbp, 1, rrecs + 1);
2260 ASSERT(cur->bc_ops->keys_inorder(cur, rkp,
2261 xfs_btree_key_addr(cur, 2, right)));
2263 /* It's a leaf. make a hole in the records */
2264 union xfs_btree_rec *lrp;
2265 union xfs_btree_rec *rrp;
2267 lrp = xfs_btree_rec_addr(cur, lrecs, left);
2268 rrp = xfs_btree_rec_addr(cur, 1, right);
2270 xfs_btree_shift_recs(cur, rrp, 1, rrecs);
2272 /* Now put the new data in, and log it. */
2273 xfs_btree_copy_recs(cur, rrp, lrp, 1);
2274 xfs_btree_log_recs(cur, rbp, 1, rrecs + 1);
2276 cur->bc_ops->init_key_from_rec(&key, rrp);
2279 ASSERT(cur->bc_ops->recs_inorder(cur, rrp,
2280 xfs_btree_rec_addr(cur, 2, right)));
2284 * Decrement and log left's numrecs, bump and log right's numrecs.
2286 xfs_btree_set_numrecs(left, --lrecs);
2287 xfs_btree_log_block(cur, lbp, XFS_BB_NUMRECS);
2289 xfs_btree_set_numrecs(right, ++rrecs);
2290 xfs_btree_log_block(cur, rbp, XFS_BB_NUMRECS);
2293 * Using a temporary cursor, update the parent key values of the
2294 * block on the right.
2296 error = xfs_btree_dup_cursor(cur, &tcur);
2299 i = xfs_btree_lastrec(tcur, level);
2300 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
2302 error = xfs_btree_increment(tcur, level, &i);
2306 error = xfs_btree_updkey(tcur, rkp, level + 1);
2310 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
2312 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2317 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2322 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
2326 XFS_BTREE_TRACE_CURSOR(tcur, XBT_ERROR);
2327 xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
2332 * Split cur/level block in half.
2333 * Return new block number and the key to its first
2334 * record (to be inserted into parent).
2336 STATIC int /* error */
2338 struct xfs_btree_cur *cur,
2340 union xfs_btree_ptr *ptrp,
2341 union xfs_btree_key *key,
2342 struct xfs_btree_cur **curp,
2343 int *stat) /* success/failure */
2345 union xfs_btree_ptr lptr; /* left sibling block ptr */
2346 struct xfs_buf *lbp; /* left buffer pointer */
2347 struct xfs_btree_block *left; /* left btree block */
2348 union xfs_btree_ptr rptr; /* right sibling block ptr */
2349 struct xfs_buf *rbp; /* right buffer pointer */
2350 struct xfs_btree_block *right; /* right btree block */
2351 union xfs_btree_ptr rrptr; /* right-right sibling ptr */
2352 struct xfs_buf *rrbp; /* right-right buffer pointer */
2353 struct xfs_btree_block *rrblock; /* right-right btree block */
2357 int error; /* error return value */
2362 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
2363 XFS_BTREE_TRACE_ARGIPK(cur, level, *ptrp, key);
2365 XFS_BTREE_STATS_INC(cur, split);
2367 /* Set up left block (current one). */
2368 left = xfs_btree_get_block(cur, level, &lbp);
2371 error = xfs_btree_check_block(cur, left, level, lbp);
2376 xfs_btree_buf_to_ptr(cur, lbp, &lptr);
2378 /* Allocate the new block. If we can't do it, we're toast. Give up. */
2379 error = cur->bc_ops->alloc_block(cur, &lptr, &rptr, 1, stat);
2384 XFS_BTREE_STATS_INC(cur, alloc);
2386 /* Set up the new block as "right". */
2387 error = xfs_btree_get_buf_block(cur, &rptr, 0, &right, &rbp);
2391 /* Fill in the btree header for the new right block. */
2392 xfs_btree_init_block_cur(cur, rbp, xfs_btree_get_level(left), 0);
2395 * Split the entries between the old and the new block evenly.
2396 * Make sure that if there's an odd number of entries now, that
2397 * each new block will have the same number of entries.
2399 lrecs = xfs_btree_get_numrecs(left);
2401 if ((lrecs & 1) && cur->bc_ptrs[level] <= rrecs + 1)
2403 src_index = (lrecs - rrecs + 1);
2405 XFS_BTREE_STATS_ADD(cur, moves, rrecs);
2408 * Copy btree block entries from the left block over to the
2409 * new block, the right. Update the right block and log the
2413 /* It's a non-leaf. Move keys and pointers. */
2414 union xfs_btree_key *lkp; /* left btree key */
2415 union xfs_btree_ptr *lpp; /* left address pointer */
2416 union xfs_btree_key *rkp; /* right btree key */
2417 union xfs_btree_ptr *rpp; /* right address pointer */
2419 lkp = xfs_btree_key_addr(cur, src_index, left);
2420 lpp = xfs_btree_ptr_addr(cur, src_index, left);
2421 rkp = xfs_btree_key_addr(cur, 1, right);
2422 rpp = xfs_btree_ptr_addr(cur, 1, right);
2425 for (i = src_index; i < rrecs; i++) {
2426 error = xfs_btree_check_ptr(cur, lpp, i, level);
2432 xfs_btree_copy_keys(cur, rkp, lkp, rrecs);
2433 xfs_btree_copy_ptrs(cur, rpp, lpp, rrecs);
2435 xfs_btree_log_keys(cur, rbp, 1, rrecs);
2436 xfs_btree_log_ptrs(cur, rbp, 1, rrecs);
2438 /* Grab the keys to the entries moved to the right block */
2439 xfs_btree_copy_keys(cur, key, rkp, 1);
2441 /* It's a leaf. Move records. */
2442 union xfs_btree_rec *lrp; /* left record pointer */
2443 union xfs_btree_rec *rrp; /* right record pointer */
2445 lrp = xfs_btree_rec_addr(cur, src_index, left);
2446 rrp = xfs_btree_rec_addr(cur, 1, right);
2448 xfs_btree_copy_recs(cur, rrp, lrp, rrecs);
2449 xfs_btree_log_recs(cur, rbp, 1, rrecs);
2451 cur->bc_ops->init_key_from_rec(key,
2452 xfs_btree_rec_addr(cur, 1, right));
2457 * Find the left block number by looking in the buffer.
2458 * Adjust numrecs, sibling pointers.
2460 xfs_btree_get_sibling(cur, left, &rrptr, XFS_BB_RIGHTSIB);
2461 xfs_btree_set_sibling(cur, right, &rrptr, XFS_BB_RIGHTSIB);
2462 xfs_btree_set_sibling(cur, right, &lptr, XFS_BB_LEFTSIB);
2463 xfs_btree_set_sibling(cur, left, &rptr, XFS_BB_RIGHTSIB);
2466 xfs_btree_set_numrecs(left, lrecs);
2467 xfs_btree_set_numrecs(right, xfs_btree_get_numrecs(right) + rrecs);
2469 xfs_btree_log_block(cur, rbp, XFS_BB_ALL_BITS);
2470 xfs_btree_log_block(cur, lbp, XFS_BB_NUMRECS | XFS_BB_RIGHTSIB);
2473 * If there's a block to the new block's right, make that block
2474 * point back to right instead of to left.
2476 if (!xfs_btree_ptr_is_null(cur, &rrptr)) {
2477 error = xfs_btree_read_buf_block(cur, &rrptr, level,
2478 0, &rrblock, &rrbp);
2481 xfs_btree_set_sibling(cur, rrblock, &rptr, XFS_BB_LEFTSIB);
2482 xfs_btree_log_block(cur, rrbp, XFS_BB_LEFTSIB);
2485 * If the cursor is really in the right block, move it there.
2486 * If it's just pointing past the last entry in left, then we'll
2487 * insert there, so don't change anything in that case.
2489 if (cur->bc_ptrs[level] > lrecs + 1) {
2490 xfs_btree_setbuf(cur, level, rbp);
2491 cur->bc_ptrs[level] -= lrecs;
2494 * If there are more levels, we'll need another cursor which refers
2495 * the right block, no matter where this cursor was.
2497 if (level + 1 < cur->bc_nlevels) {
2498 error = xfs_btree_dup_cursor(cur, curp);
2501 (*curp)->bc_ptrs[level + 1]++;
2504 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2508 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2513 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
2518 * Copy the old inode root contents into a real block and make the
2519 * broot point to it.
2522 xfs_btree_new_iroot(
2523 struct xfs_btree_cur *cur, /* btree cursor */
2524 int *logflags, /* logging flags for inode */
2525 int *stat) /* return status - 0 fail */
2527 struct xfs_buf *cbp; /* buffer for cblock */
2528 struct xfs_btree_block *block; /* btree block */
2529 struct xfs_btree_block *cblock; /* child btree block */
2530 union xfs_btree_key *ckp; /* child key pointer */
2531 union xfs_btree_ptr *cpp; /* child ptr pointer */
2532 union xfs_btree_key *kp; /* pointer to btree key */
2533 union xfs_btree_ptr *pp; /* pointer to block addr */
2534 union xfs_btree_ptr nptr; /* new block addr */
2535 int level; /* btree level */
2536 int error; /* error return code */
2538 int i; /* loop counter */
2541 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
2542 XFS_BTREE_STATS_INC(cur, newroot);
2544 ASSERT(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE);
2546 level = cur->bc_nlevels - 1;
2548 block = xfs_btree_get_iroot(cur);
2549 pp = xfs_btree_ptr_addr(cur, 1, block);
2551 /* Allocate the new block. If we can't do it, we're toast. Give up. */
2552 error = cur->bc_ops->alloc_block(cur, pp, &nptr, 1, stat);
2556 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2559 XFS_BTREE_STATS_INC(cur, alloc);
2561 /* Copy the root into a real block. */
2562 error = xfs_btree_get_buf_block(cur, &nptr, 0, &cblock, &cbp);
2567 * we can't just memcpy() the root in for CRC enabled btree blocks.
2568 * In that case have to also ensure the blkno remains correct
2570 memcpy(cblock, block, xfs_btree_block_len(cur));
2571 if (cur->bc_flags & XFS_BTREE_CRC_BLOCKS) {
2572 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
2573 cblock->bb_u.l.bb_blkno = cpu_to_be64(cbp->b_bn);
2575 cblock->bb_u.s.bb_blkno = cpu_to_be64(cbp->b_bn);
2578 be16_add_cpu(&block->bb_level, 1);
2579 xfs_btree_set_numrecs(block, 1);
2581 cur->bc_ptrs[level + 1] = 1;
2583 kp = xfs_btree_key_addr(cur, 1, block);
2584 ckp = xfs_btree_key_addr(cur, 1, cblock);
2585 xfs_btree_copy_keys(cur, ckp, kp, xfs_btree_get_numrecs(cblock));
2587 cpp = xfs_btree_ptr_addr(cur, 1, cblock);
2589 for (i = 0; i < be16_to_cpu(cblock->bb_numrecs); i++) {
2590 error = xfs_btree_check_ptr(cur, pp, i, level);
2595 xfs_btree_copy_ptrs(cur, cpp, pp, xfs_btree_get_numrecs(cblock));
2598 error = xfs_btree_check_ptr(cur, &nptr, 0, level);
2602 xfs_btree_copy_ptrs(cur, pp, &nptr, 1);
2604 xfs_iroot_realloc(cur->bc_private.b.ip,
2605 1 - xfs_btree_get_numrecs(cblock),
2606 cur->bc_private.b.whichfork);
2608 xfs_btree_setbuf(cur, level, cbp);
2611 * Do all this logging at the end so that
2612 * the root is at the right level.
2614 xfs_btree_log_block(cur, cbp, XFS_BB_ALL_BITS);
2615 xfs_btree_log_keys(cur, cbp, 1, be16_to_cpu(cblock->bb_numrecs));
2616 xfs_btree_log_ptrs(cur, cbp, 1, be16_to_cpu(cblock->bb_numrecs));
2619 XFS_ILOG_CORE | xfs_ilog_fbroot(cur->bc_private.b.whichfork);
2621 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2624 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
2629 * Allocate a new root block, fill it in.
2631 STATIC int /* error */
2633 struct xfs_btree_cur *cur, /* btree cursor */
2634 int *stat) /* success/failure */
2636 struct xfs_btree_block *block; /* one half of the old root block */
2637 struct xfs_buf *bp; /* buffer containing block */
2638 int error; /* error return value */
2639 struct xfs_buf *lbp; /* left buffer pointer */
2640 struct xfs_btree_block *left; /* left btree block */
2641 struct xfs_buf *nbp; /* new (root) buffer */
2642 struct xfs_btree_block *new; /* new (root) btree block */
2643 int nptr; /* new value for key index, 1 or 2 */
2644 struct xfs_buf *rbp; /* right buffer pointer */
2645 struct xfs_btree_block *right; /* right btree block */
2646 union xfs_btree_ptr rptr;
2647 union xfs_btree_ptr lptr;
2649 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
2650 XFS_BTREE_STATS_INC(cur, newroot);
2652 /* initialise our start point from the cursor */
2653 cur->bc_ops->init_ptr_from_cur(cur, &rptr);
2655 /* Allocate the new block. If we can't do it, we're toast. Give up. */
2656 error = cur->bc_ops->alloc_block(cur, &rptr, &lptr, 1, stat);
2661 XFS_BTREE_STATS_INC(cur, alloc);
2663 /* Set up the new block. */
2664 error = xfs_btree_get_buf_block(cur, &lptr, 0, &new, &nbp);
2668 /* Set the root in the holding structure increasing the level by 1. */
2669 cur->bc_ops->set_root(cur, &lptr, 1);
2672 * At the previous root level there are now two blocks: the old root,
2673 * and the new block generated when it was split. We don't know which
2674 * one the cursor is pointing at, so we set up variables "left" and
2675 * "right" for each case.
2677 block = xfs_btree_get_block(cur, cur->bc_nlevels - 1, &bp);
2680 error = xfs_btree_check_block(cur, block, cur->bc_nlevels - 1, bp);
2685 xfs_btree_get_sibling(cur, block, &rptr, XFS_BB_RIGHTSIB);
2686 if (!xfs_btree_ptr_is_null(cur, &rptr)) {
2687 /* Our block is left, pick up the right block. */
2689 xfs_btree_buf_to_ptr(cur, lbp, &lptr);
2691 error = xfs_btree_read_buf_block(cur, &rptr,
2692 cur->bc_nlevels - 1, 0, &right, &rbp);
2698 /* Our block is right, pick up the left block. */
2700 xfs_btree_buf_to_ptr(cur, rbp, &rptr);
2702 xfs_btree_get_sibling(cur, right, &lptr, XFS_BB_LEFTSIB);
2703 error = xfs_btree_read_buf_block(cur, &lptr,
2704 cur->bc_nlevels - 1, 0, &left, &lbp);
2710 /* Fill in the new block's btree header and log it. */
2711 xfs_btree_init_block_cur(cur, nbp, cur->bc_nlevels, 2);
2712 xfs_btree_log_block(cur, nbp, XFS_BB_ALL_BITS);
2713 ASSERT(!xfs_btree_ptr_is_null(cur, &lptr) &&
2714 !xfs_btree_ptr_is_null(cur, &rptr));
2716 /* Fill in the key data in the new root. */
2717 if (xfs_btree_get_level(left) > 0) {
2718 xfs_btree_copy_keys(cur,
2719 xfs_btree_key_addr(cur, 1, new),
2720 xfs_btree_key_addr(cur, 1, left), 1);
2721 xfs_btree_copy_keys(cur,
2722 xfs_btree_key_addr(cur, 2, new),
2723 xfs_btree_key_addr(cur, 1, right), 1);
2725 cur->bc_ops->init_key_from_rec(
2726 xfs_btree_key_addr(cur, 1, new),
2727 xfs_btree_rec_addr(cur, 1, left));
2728 cur->bc_ops->init_key_from_rec(
2729 xfs_btree_key_addr(cur, 2, new),
2730 xfs_btree_rec_addr(cur, 1, right));
2732 xfs_btree_log_keys(cur, nbp, 1, 2);
2734 /* Fill in the pointer data in the new root. */
2735 xfs_btree_copy_ptrs(cur,
2736 xfs_btree_ptr_addr(cur, 1, new), &lptr, 1);
2737 xfs_btree_copy_ptrs(cur,
2738 xfs_btree_ptr_addr(cur, 2, new), &rptr, 1);
2739 xfs_btree_log_ptrs(cur, nbp, 1, 2);
2741 /* Fix up the cursor. */
2742 xfs_btree_setbuf(cur, cur->bc_nlevels, nbp);
2743 cur->bc_ptrs[cur->bc_nlevels] = nptr;
2745 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2749 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
2752 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2758 xfs_btree_make_block_unfull(
2759 struct xfs_btree_cur *cur, /* btree cursor */
2760 int level, /* btree level */
2761 int numrecs,/* # of recs in block */
2762 int *oindex,/* old tree index */
2763 int *index, /* new tree index */
2764 union xfs_btree_ptr *nptr, /* new btree ptr */
2765 struct xfs_btree_cur **ncur, /* new btree cursor */
2766 union xfs_btree_rec *nrec, /* new record */
2769 union xfs_btree_key key; /* new btree key value */
2772 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
2773 level == cur->bc_nlevels - 1) {
2774 struct xfs_inode *ip = cur->bc_private.b.ip;
2776 if (numrecs < cur->bc_ops->get_dmaxrecs(cur, level)) {
2777 /* A root block that can be made bigger. */
2778 xfs_iroot_realloc(ip, 1, cur->bc_private.b.whichfork);
2780 /* A root block that needs replacing */
2783 error = xfs_btree_new_iroot(cur, &logflags, stat);
2784 if (error || *stat == 0)
2787 xfs_trans_log_inode(cur->bc_tp, ip, logflags);
2793 /* First, try shifting an entry to the right neighbor. */
2794 error = xfs_btree_rshift(cur, level, stat);
2798 /* Next, try shifting an entry to the left neighbor. */
2799 error = xfs_btree_lshift(cur, level, stat);
2804 *oindex = *index = cur->bc_ptrs[level];
2809 * Next, try splitting the current block in half.
2811 * If this works we have to re-set our variables because we
2812 * could be in a different block now.
2814 error = xfs_btree_split(cur, level, nptr, &key, ncur, stat);
2815 if (error || *stat == 0)
2819 *index = cur->bc_ptrs[level];
2820 cur->bc_ops->init_rec_from_key(&key, nrec);
2825 * Insert one record/level. Return information to the caller
2826 * allowing the next level up to proceed if necessary.
2830 struct xfs_btree_cur *cur, /* btree cursor */
2831 int level, /* level to insert record at */
2832 union xfs_btree_ptr *ptrp, /* i/o: block number inserted */
2833 union xfs_btree_rec *recp, /* i/o: record data inserted */
2834 struct xfs_btree_cur **curp, /* output: new cursor replacing cur */
2835 int *stat) /* success/failure */
2837 struct xfs_btree_block *block; /* btree block */
2838 struct xfs_buf *bp; /* buffer for block */
2839 union xfs_btree_key key; /* btree key */
2840 union xfs_btree_ptr nptr; /* new block ptr */
2841 struct xfs_btree_cur *ncur; /* new btree cursor */
2842 union xfs_btree_rec nrec; /* new record count */
2843 int optr; /* old key/record index */
2844 int ptr; /* key/record index */
2845 int numrecs;/* number of records */
2846 int error; /* error return value */
2851 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
2852 XFS_BTREE_TRACE_ARGIPR(cur, level, *ptrp, recp);
2857 * If we have an external root pointer, and we've made it to the
2858 * root level, allocate a new root block and we're done.
2860 if (!(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
2861 (level >= cur->bc_nlevels)) {
2862 error = xfs_btree_new_root(cur, stat);
2863 xfs_btree_set_ptr_null(cur, ptrp);
2865 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2869 /* If we're off the left edge, return failure. */
2870 ptr = cur->bc_ptrs[level];
2872 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2877 /* Make a key out of the record data to be inserted, and save it. */
2878 cur->bc_ops->init_key_from_rec(&key, recp);
2882 XFS_BTREE_STATS_INC(cur, insrec);
2884 /* Get pointers to the btree buffer and block. */
2885 block = xfs_btree_get_block(cur, level, &bp);
2886 numrecs = xfs_btree_get_numrecs(block);
2889 error = xfs_btree_check_block(cur, block, level, bp);
2893 /* Check that the new entry is being inserted in the right place. */
2894 if (ptr <= numrecs) {
2896 ASSERT(cur->bc_ops->recs_inorder(cur, recp,
2897 xfs_btree_rec_addr(cur, ptr, block)));
2899 ASSERT(cur->bc_ops->keys_inorder(cur, &key,
2900 xfs_btree_key_addr(cur, ptr, block)));
2906 * If the block is full, we can't insert the new entry until we
2907 * make the block un-full.
2909 xfs_btree_set_ptr_null(cur, &nptr);
2910 if (numrecs == cur->bc_ops->get_maxrecs(cur, level)) {
2911 error = xfs_btree_make_block_unfull(cur, level, numrecs,
2912 &optr, &ptr, &nptr, &ncur, &nrec, stat);
2913 if (error || *stat == 0)
2918 * The current block may have changed if the block was
2919 * previously full and we have just made space in it.
2921 block = xfs_btree_get_block(cur, level, &bp);
2922 numrecs = xfs_btree_get_numrecs(block);
2925 error = xfs_btree_check_block(cur, block, level, bp);
2931 * At this point we know there's room for our new entry in the block
2932 * we're pointing at.
2934 XFS_BTREE_STATS_ADD(cur, moves, numrecs - ptr + 1);
2937 /* It's a nonleaf. make a hole in the keys and ptrs */
2938 union xfs_btree_key *kp;
2939 union xfs_btree_ptr *pp;
2941 kp = xfs_btree_key_addr(cur, ptr, block);
2942 pp = xfs_btree_ptr_addr(cur, ptr, block);
2945 for (i = numrecs - ptr; i >= 0; i--) {
2946 error = xfs_btree_check_ptr(cur, pp, i, level);
2952 xfs_btree_shift_keys(cur, kp, 1, numrecs - ptr + 1);
2953 xfs_btree_shift_ptrs(cur, pp, 1, numrecs - ptr + 1);
2956 error = xfs_btree_check_ptr(cur, ptrp, 0, level);
2961 /* Now put the new data in, bump numrecs and log it. */
2962 xfs_btree_copy_keys(cur, kp, &key, 1);
2963 xfs_btree_copy_ptrs(cur, pp, ptrp, 1);
2965 xfs_btree_set_numrecs(block, numrecs);
2966 xfs_btree_log_ptrs(cur, bp, ptr, numrecs);
2967 xfs_btree_log_keys(cur, bp, ptr, numrecs);
2969 if (ptr < numrecs) {
2970 ASSERT(cur->bc_ops->keys_inorder(cur, kp,
2971 xfs_btree_key_addr(cur, ptr + 1, block)));
2975 /* It's a leaf. make a hole in the records */
2976 union xfs_btree_rec *rp;
2978 rp = xfs_btree_rec_addr(cur, ptr, block);
2980 xfs_btree_shift_recs(cur, rp, 1, numrecs - ptr + 1);
2982 /* Now put the new data in, bump numrecs and log it. */
2983 xfs_btree_copy_recs(cur, rp, recp, 1);
2984 xfs_btree_set_numrecs(block, ++numrecs);
2985 xfs_btree_log_recs(cur, bp, ptr, numrecs);
2987 if (ptr < numrecs) {
2988 ASSERT(cur->bc_ops->recs_inorder(cur, rp,
2989 xfs_btree_rec_addr(cur, ptr + 1, block)));
2994 /* Log the new number of records in the btree header. */
2995 xfs_btree_log_block(cur, bp, XFS_BB_NUMRECS);
2997 /* If we inserted at the start of a block, update the parents' keys. */
2999 error = xfs_btree_updkey(cur, &key, level + 1);
3005 * If we are tracking the last record in the tree and
3006 * we are at the far right edge of the tree, update it.
3008 if (xfs_btree_is_lastrec(cur, block, level)) {
3009 cur->bc_ops->update_lastrec(cur, block, recp,
3010 ptr, LASTREC_INSREC);
3014 * Return the new block number, if any.
3015 * If there is one, give back a record value and a cursor too.
3018 if (!xfs_btree_ptr_is_null(cur, &nptr)) {
3023 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3028 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
3033 * Insert the record at the point referenced by cur.
3035 * A multi-level split of the tree on insert will invalidate the original
3036 * cursor. All callers of this function should assume that the cursor is
3037 * no longer valid and revalidate it.
3041 struct xfs_btree_cur *cur,
3044 int error; /* error return value */
3045 int i; /* result value, 0 for failure */
3046 int level; /* current level number in btree */
3047 union xfs_btree_ptr nptr; /* new block number (split result) */
3048 struct xfs_btree_cur *ncur; /* new cursor (split result) */
3049 struct xfs_btree_cur *pcur; /* previous level's cursor */
3050 union xfs_btree_rec rec; /* record to insert */
3056 xfs_btree_set_ptr_null(cur, &nptr);
3057 cur->bc_ops->init_rec_from_cur(cur, &rec);
3060 * Loop going up the tree, starting at the leaf level.
3061 * Stop when we don't get a split block, that must mean that
3062 * the insert is finished with this level.
3066 * Insert nrec/nptr into this level of the tree.
3067 * Note if we fail, nptr will be null.
3069 error = xfs_btree_insrec(pcur, level, &nptr, &rec, &ncur, &i);
3072 xfs_btree_del_cursor(pcur, XFS_BTREE_ERROR);
3076 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
3080 * See if the cursor we just used is trash.
3081 * Can't trash the caller's cursor, but otherwise we should
3082 * if ncur is a new cursor or we're about to be done.
3085 (ncur || xfs_btree_ptr_is_null(cur, &nptr))) {
3086 /* Save the state from the cursor before we trash it */
3087 if (cur->bc_ops->update_cursor)
3088 cur->bc_ops->update_cursor(pcur, cur);
3089 cur->bc_nlevels = pcur->bc_nlevels;
3090 xfs_btree_del_cursor(pcur, XFS_BTREE_NOERROR);
3092 /* If we got a new cursor, switch to it. */
3097 } while (!xfs_btree_ptr_is_null(cur, &nptr));
3099 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3103 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
3108 * Try to merge a non-leaf block back into the inode root.
3110 * Note: the killroot names comes from the fact that we're effectively
3111 * killing the old root block. But because we can't just delete the
3112 * inode we have to copy the single block it was pointing to into the
3116 xfs_btree_kill_iroot(
3117 struct xfs_btree_cur *cur)
3119 int whichfork = cur->bc_private.b.whichfork;
3120 struct xfs_inode *ip = cur->bc_private.b.ip;
3121 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
3122 struct xfs_btree_block *block;
3123 struct xfs_btree_block *cblock;
3124 union xfs_btree_key *kp;
3125 union xfs_btree_key *ckp;
3126 union xfs_btree_ptr *pp;
3127 union xfs_btree_ptr *cpp;
3128 struct xfs_buf *cbp;
3133 union xfs_btree_ptr ptr;
3137 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
3139 ASSERT(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE);
3140 ASSERT(cur->bc_nlevels > 1);
3143 * Don't deal with the root block needs to be a leaf case.
3144 * We're just going to turn the thing back into extents anyway.
3146 level = cur->bc_nlevels - 1;
3151 * Give up if the root has multiple children.
3153 block = xfs_btree_get_iroot(cur);
3154 if (xfs_btree_get_numrecs(block) != 1)
3157 cblock = xfs_btree_get_block(cur, level - 1, &cbp);
3158 numrecs = xfs_btree_get_numrecs(cblock);
3161 * Only do this if the next level will fit.
3162 * Then the data must be copied up to the inode,
3163 * instead of freeing the root you free the next level.
3165 if (numrecs > cur->bc_ops->get_dmaxrecs(cur, level))
3168 XFS_BTREE_STATS_INC(cur, killroot);
3171 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_LEFTSIB);
3172 ASSERT(xfs_btree_ptr_is_null(cur, &ptr));
3173 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_RIGHTSIB);
3174 ASSERT(xfs_btree_ptr_is_null(cur, &ptr));
3177 index = numrecs - cur->bc_ops->get_maxrecs(cur, level);
3179 xfs_iroot_realloc(cur->bc_private.b.ip, index,
3180 cur->bc_private.b.whichfork);
3181 block = ifp->if_broot;
3184 be16_add_cpu(&block->bb_numrecs, index);
3185 ASSERT(block->bb_numrecs == cblock->bb_numrecs);
3187 kp = xfs_btree_key_addr(cur, 1, block);
3188 ckp = xfs_btree_key_addr(cur, 1, cblock);
3189 xfs_btree_copy_keys(cur, kp, ckp, numrecs);
3191 pp = xfs_btree_ptr_addr(cur, 1, block);
3192 cpp = xfs_btree_ptr_addr(cur, 1, cblock);
3194 for (i = 0; i < numrecs; i++) {
3197 error = xfs_btree_check_ptr(cur, cpp, i, level - 1);
3199 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
3204 xfs_btree_copy_ptrs(cur, pp, cpp, numrecs);
3206 cur->bc_ops->free_block(cur, cbp);
3207 XFS_BTREE_STATS_INC(cur, free);
3209 cur->bc_bufs[level - 1] = NULL;
3210 be16_add_cpu(&block->bb_level, -1);
3211 xfs_trans_log_inode(cur->bc_tp, ip,
3212 XFS_ILOG_CORE | xfs_ilog_fbroot(cur->bc_private.b.whichfork));
3215 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3220 * Kill the current root node, and replace it with it's only child node.
3223 xfs_btree_kill_root(
3224 struct xfs_btree_cur *cur,
3227 union xfs_btree_ptr *newroot)
3231 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
3232 XFS_BTREE_STATS_INC(cur, killroot);
3235 * Update the root pointer, decreasing the level by 1 and then
3236 * free the old root.
3238 cur->bc_ops->set_root(cur, newroot, -1);
3240 error = cur->bc_ops->free_block(cur, bp);
3242 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
3246 XFS_BTREE_STATS_INC(cur, free);
3248 cur->bc_bufs[level] = NULL;
3249 cur->bc_ra[level] = 0;
3252 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3257 xfs_btree_dec_cursor(
3258 struct xfs_btree_cur *cur,
3266 error = xfs_btree_decrement(cur, level, &i);
3271 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3277 * Single level of the btree record deletion routine.
3278 * Delete record pointed to by cur/level.
3279 * Remove the record from its block then rebalance the tree.
3280 * Return 0 for error, 1 for done, 2 to go on to the next level.
3282 STATIC int /* error */
3284 struct xfs_btree_cur *cur, /* btree cursor */
3285 int level, /* level removing record from */
3286 int *stat) /* fail/done/go-on */
3288 struct xfs_btree_block *block; /* btree block */
3289 union xfs_btree_ptr cptr; /* current block ptr */
3290 struct xfs_buf *bp; /* buffer for block */
3291 int error; /* error return value */
3292 int i; /* loop counter */
3293 union xfs_btree_key key; /* storage for keyp */
3294 union xfs_btree_key *keyp = &key; /* passed to the next level */
3295 union xfs_btree_ptr lptr; /* left sibling block ptr */
3296 struct xfs_buf *lbp; /* left buffer pointer */
3297 struct xfs_btree_block *left; /* left btree block */
3298 int lrecs = 0; /* left record count */
3299 int ptr; /* key/record index */
3300 union xfs_btree_ptr rptr; /* right sibling block ptr */
3301 struct xfs_buf *rbp; /* right buffer pointer */
3302 struct xfs_btree_block *right; /* right btree block */
3303 struct xfs_btree_block *rrblock; /* right-right btree block */
3304 struct xfs_buf *rrbp; /* right-right buffer pointer */
3305 int rrecs = 0; /* right record count */
3306 struct xfs_btree_cur *tcur; /* temporary btree cursor */
3307 int numrecs; /* temporary numrec count */
3309 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
3310 XFS_BTREE_TRACE_ARGI(cur, level);
3314 /* Get the index of the entry being deleted, check for nothing there. */
3315 ptr = cur->bc_ptrs[level];
3317 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3322 /* Get the buffer & block containing the record or key/ptr. */
3323 block = xfs_btree_get_block(cur, level, &bp);
3324 numrecs = xfs_btree_get_numrecs(block);
3327 error = xfs_btree_check_block(cur, block, level, bp);
3332 /* Fail if we're off the end of the block. */
3333 if (ptr > numrecs) {
3334 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3339 XFS_BTREE_STATS_INC(cur, delrec);
3340 XFS_BTREE_STATS_ADD(cur, moves, numrecs - ptr);
3342 /* Excise the entries being deleted. */
3344 /* It's a nonleaf. operate on keys and ptrs */
3345 union xfs_btree_key *lkp;
3346 union xfs_btree_ptr *lpp;
3348 lkp = xfs_btree_key_addr(cur, ptr + 1, block);
3349 lpp = xfs_btree_ptr_addr(cur, ptr + 1, block);
3352 for (i = 0; i < numrecs - ptr; i++) {
3353 error = xfs_btree_check_ptr(cur, lpp, i, level);
3359 if (ptr < numrecs) {
3360 xfs_btree_shift_keys(cur, lkp, -1, numrecs - ptr);
3361 xfs_btree_shift_ptrs(cur, lpp, -1, numrecs - ptr);
3362 xfs_btree_log_keys(cur, bp, ptr, numrecs - 1);
3363 xfs_btree_log_ptrs(cur, bp, ptr, numrecs - 1);
3367 * If it's the first record in the block, we'll need to pass a
3368 * key up to the next level (updkey).
3371 keyp = xfs_btree_key_addr(cur, 1, block);
3373 /* It's a leaf. operate on records */
3374 if (ptr < numrecs) {
3375 xfs_btree_shift_recs(cur,
3376 xfs_btree_rec_addr(cur, ptr + 1, block),
3378 xfs_btree_log_recs(cur, bp, ptr, numrecs - 1);
3382 * If it's the first record in the block, we'll need a key
3383 * structure to pass up to the next level (updkey).
3386 cur->bc_ops->init_key_from_rec(&key,
3387 xfs_btree_rec_addr(cur, 1, block));
3393 * Decrement and log the number of entries in the block.
3395 xfs_btree_set_numrecs(block, --numrecs);
3396 xfs_btree_log_block(cur, bp, XFS_BB_NUMRECS);
3399 * If we are tracking the last record in the tree and
3400 * we are at the far right edge of the tree, update it.
3402 if (xfs_btree_is_lastrec(cur, block, level)) {
3403 cur->bc_ops->update_lastrec(cur, block, NULL,
3404 ptr, LASTREC_DELREC);
3408 * We're at the root level. First, shrink the root block in-memory.
3409 * Try to get rid of the next level down. If we can't then there's
3410 * nothing left to do.
3412 if (level == cur->bc_nlevels - 1) {
3413 if (cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) {
3414 xfs_iroot_realloc(cur->bc_private.b.ip, -1,
3415 cur->bc_private.b.whichfork);
3417 error = xfs_btree_kill_iroot(cur);
3421 error = xfs_btree_dec_cursor(cur, level, stat);
3429 * If this is the root level, and there's only one entry left,
3430 * and it's NOT the leaf level, then we can get rid of this
3433 if (numrecs == 1 && level > 0) {
3434 union xfs_btree_ptr *pp;
3436 * pp is still set to the first pointer in the block.
3437 * Make it the new root of the btree.
3439 pp = xfs_btree_ptr_addr(cur, 1, block);
3440 error = xfs_btree_kill_root(cur, bp, level, pp);
3443 } else if (level > 0) {
3444 error = xfs_btree_dec_cursor(cur, level, stat);
3453 * If we deleted the leftmost entry in the block, update the
3454 * key values above us in the tree.
3457 error = xfs_btree_updkey(cur, keyp, level + 1);
3463 * If the number of records remaining in the block is at least
3464 * the minimum, we're done.
3466 if (numrecs >= cur->bc_ops->get_minrecs(cur, level)) {
3467 error = xfs_btree_dec_cursor(cur, level, stat);
3474 * Otherwise, we have to move some records around to keep the
3475 * tree balanced. Look at the left and right sibling blocks to
3476 * see if we can re-balance by moving only one record.
3478 xfs_btree_get_sibling(cur, block, &rptr, XFS_BB_RIGHTSIB);
3479 xfs_btree_get_sibling(cur, block, &lptr, XFS_BB_LEFTSIB);
3481 if (cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) {
3483 * One child of root, need to get a chance to copy its contents
3484 * into the root and delete it. Can't go up to next level,
3485 * there's nothing to delete there.
3487 if (xfs_btree_ptr_is_null(cur, &rptr) &&
3488 xfs_btree_ptr_is_null(cur, &lptr) &&
3489 level == cur->bc_nlevels - 2) {
3490 error = xfs_btree_kill_iroot(cur);
3492 error = xfs_btree_dec_cursor(cur, level, stat);
3499 ASSERT(!xfs_btree_ptr_is_null(cur, &rptr) ||
3500 !xfs_btree_ptr_is_null(cur, &lptr));
3503 * Duplicate the cursor so our btree manipulations here won't
3504 * disrupt the next level up.
3506 error = xfs_btree_dup_cursor(cur, &tcur);
3511 * If there's a right sibling, see if it's ok to shift an entry
3514 if (!xfs_btree_ptr_is_null(cur, &rptr)) {
3516 * Move the temp cursor to the last entry in the next block.
3517 * Actually any entry but the first would suffice.
3519 i = xfs_btree_lastrec(tcur, level);
3520 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
3522 error = xfs_btree_increment(tcur, level, &i);
3525 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
3527 i = xfs_btree_lastrec(tcur, level);
3528 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
3530 /* Grab a pointer to the block. */
3531 right = xfs_btree_get_block(tcur, level, &rbp);
3533 error = xfs_btree_check_block(tcur, right, level, rbp);
3537 /* Grab the current block number, for future use. */
3538 xfs_btree_get_sibling(tcur, right, &cptr, XFS_BB_LEFTSIB);
3541 * If right block is full enough so that removing one entry
3542 * won't make it too empty, and left-shifting an entry out
3543 * of right to us works, we're done.
3545 if (xfs_btree_get_numrecs(right) - 1 >=
3546 cur->bc_ops->get_minrecs(tcur, level)) {
3547 error = xfs_btree_lshift(tcur, level, &i);
3551 ASSERT(xfs_btree_get_numrecs(block) >=
3552 cur->bc_ops->get_minrecs(tcur, level));
3554 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
3557 error = xfs_btree_dec_cursor(cur, level, stat);
3565 * Otherwise, grab the number of records in right for
3566 * future reference, and fix up the temp cursor to point
3567 * to our block again (last record).
3569 rrecs = xfs_btree_get_numrecs(right);
3570 if (!xfs_btree_ptr_is_null(cur, &lptr)) {
3571 i = xfs_btree_firstrec(tcur, level);
3572 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
3574 error = xfs_btree_decrement(tcur, level, &i);
3577 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
3582 * If there's a left sibling, see if it's ok to shift an entry
3585 if (!xfs_btree_ptr_is_null(cur, &lptr)) {
3587 * Move the temp cursor to the first entry in the
3590 i = xfs_btree_firstrec(tcur, level);
3591 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
3593 error = xfs_btree_decrement(tcur, level, &i);
3596 i = xfs_btree_firstrec(tcur, level);
3597 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
3599 /* Grab a pointer to the block. */
3600 left = xfs_btree_get_block(tcur, level, &lbp);
3602 error = xfs_btree_check_block(cur, left, level, lbp);
3606 /* Grab the current block number, for future use. */
3607 xfs_btree_get_sibling(tcur, left, &cptr, XFS_BB_RIGHTSIB);
3610 * If left block is full enough so that removing one entry
3611 * won't make it too empty, and right-shifting an entry out
3612 * of left to us works, we're done.
3614 if (xfs_btree_get_numrecs(left) - 1 >=
3615 cur->bc_ops->get_minrecs(tcur, level)) {
3616 error = xfs_btree_rshift(tcur, level, &i);
3620 ASSERT(xfs_btree_get_numrecs(block) >=
3621 cur->bc_ops->get_minrecs(tcur, level));
3622 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
3626 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3633 * Otherwise, grab the number of records in right for
3636 lrecs = xfs_btree_get_numrecs(left);
3639 /* Delete the temp cursor, we're done with it. */
3640 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
3643 /* If here, we need to do a join to keep the tree balanced. */
3644 ASSERT(!xfs_btree_ptr_is_null(cur, &cptr));
3646 if (!xfs_btree_ptr_is_null(cur, &lptr) &&
3647 lrecs + xfs_btree_get_numrecs(block) <=
3648 cur->bc_ops->get_maxrecs(cur, level)) {
3650 * Set "right" to be the starting block,
3651 * "left" to be the left neighbor.
3656 error = xfs_btree_read_buf_block(cur, &lptr, level,
3662 * If that won't work, see if we can join with the right neighbor block.
3664 } else if (!xfs_btree_ptr_is_null(cur, &rptr) &&
3665 rrecs + xfs_btree_get_numrecs(block) <=
3666 cur->bc_ops->get_maxrecs(cur, level)) {
3668 * Set "left" to be the starting block,
3669 * "right" to be the right neighbor.
3674 error = xfs_btree_read_buf_block(cur, &rptr, level,
3680 * Otherwise, we can't fix the imbalance.
3681 * Just return. This is probably a logic error, but it's not fatal.
3684 error = xfs_btree_dec_cursor(cur, level, stat);
3690 rrecs = xfs_btree_get_numrecs(right);
3691 lrecs = xfs_btree_get_numrecs(left);
3694 * We're now going to join "left" and "right" by moving all the stuff
3695 * in "right" to "left" and deleting "right".
3697 XFS_BTREE_STATS_ADD(cur, moves, rrecs);
3699 /* It's a non-leaf. Move keys and pointers. */
3700 union xfs_btree_key *lkp; /* left btree key */
3701 union xfs_btree_ptr *lpp; /* left address pointer */
3702 union xfs_btree_key *rkp; /* right btree key */
3703 union xfs_btree_ptr *rpp; /* right address pointer */
3705 lkp = xfs_btree_key_addr(cur, lrecs + 1, left);
3706 lpp = xfs_btree_ptr_addr(cur, lrecs + 1, left);
3707 rkp = xfs_btree_key_addr(cur, 1, right);
3708 rpp = xfs_btree_ptr_addr(cur, 1, right);
3710 for (i = 1; i < rrecs; i++) {
3711 error = xfs_btree_check_ptr(cur, rpp, i, level);
3716 xfs_btree_copy_keys(cur, lkp, rkp, rrecs);
3717 xfs_btree_copy_ptrs(cur, lpp, rpp, rrecs);
3719 xfs_btree_log_keys(cur, lbp, lrecs + 1, lrecs + rrecs);
3720 xfs_btree_log_ptrs(cur, lbp, lrecs + 1, lrecs + rrecs);
3722 /* It's a leaf. Move records. */
3723 union xfs_btree_rec *lrp; /* left record pointer */
3724 union xfs_btree_rec *rrp; /* right record pointer */
3726 lrp = xfs_btree_rec_addr(cur, lrecs + 1, left);
3727 rrp = xfs_btree_rec_addr(cur, 1, right);
3729 xfs_btree_copy_recs(cur, lrp, rrp, rrecs);
3730 xfs_btree_log_recs(cur, lbp, lrecs + 1, lrecs + rrecs);
3733 XFS_BTREE_STATS_INC(cur, join);
3736 * Fix up the number of records and right block pointer in the
3737 * surviving block, and log it.
3739 xfs_btree_set_numrecs(left, lrecs + rrecs);
3740 xfs_btree_get_sibling(cur, right, &cptr, XFS_BB_RIGHTSIB),
3741 xfs_btree_set_sibling(cur, left, &cptr, XFS_BB_RIGHTSIB);
3742 xfs_btree_log_block(cur, lbp, XFS_BB_NUMRECS | XFS_BB_RIGHTSIB);
3744 /* If there is a right sibling, point it to the remaining block. */
3745 xfs_btree_get_sibling(cur, left, &cptr, XFS_BB_RIGHTSIB);
3746 if (!xfs_btree_ptr_is_null(cur, &cptr)) {
3747 error = xfs_btree_read_buf_block(cur, &cptr, level,
3748 0, &rrblock, &rrbp);
3751 xfs_btree_set_sibling(cur, rrblock, &lptr, XFS_BB_LEFTSIB);
3752 xfs_btree_log_block(cur, rrbp, XFS_BB_LEFTSIB);
3755 /* Free the deleted block. */
3756 error = cur->bc_ops->free_block(cur, rbp);
3759 XFS_BTREE_STATS_INC(cur, free);
3762 * If we joined with the left neighbor, set the buffer in the
3763 * cursor to the left block, and fix up the index.
3766 cur->bc_bufs[level] = lbp;
3767 cur->bc_ptrs[level] += lrecs;
3768 cur->bc_ra[level] = 0;
3771 * If we joined with the right neighbor and there's a level above
3772 * us, increment the cursor at that level.
3774 else if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) ||
3775 (level + 1 < cur->bc_nlevels)) {
3776 error = xfs_btree_increment(cur, level + 1, &i);
3782 * Readjust the ptr at this level if it's not a leaf, since it's
3783 * still pointing at the deletion point, which makes the cursor
3784 * inconsistent. If this makes the ptr 0, the caller fixes it up.
3785 * We can't use decrement because it would change the next level up.
3788 cur->bc_ptrs[level]--;
3790 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3791 /* Return value means the next level up has something to do. */
3796 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
3798 xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
3803 * Delete the record pointed to by cur.
3804 * The cursor refers to the place where the record was (could be inserted)
3805 * when the operation returns.
3809 struct xfs_btree_cur *cur,
3810 int *stat) /* success/failure */
3812 int error; /* error return value */
3816 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
3819 * Go up the tree, starting at leaf level.
3821 * If 2 is returned then a join was done; go to the next level.
3822 * Otherwise we are done.
3824 for (level = 0, i = 2; i == 2; level++) {
3825 error = xfs_btree_delrec(cur, level, &i);
3831 for (level = 1; level < cur->bc_nlevels; level++) {
3832 if (cur->bc_ptrs[level] == 0) {
3833 error = xfs_btree_decrement(cur, level, &i);
3841 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3845 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
3850 * Get the data from the pointed-to record.
3854 struct xfs_btree_cur *cur, /* btree cursor */
3855 union xfs_btree_rec **recp, /* output: btree record */
3856 int *stat) /* output: success/failure */
3858 struct xfs_btree_block *block; /* btree block */
3859 struct xfs_buf *bp; /* buffer pointer */
3860 int ptr; /* record number */
3862 int error; /* error return value */
3865 ptr = cur->bc_ptrs[0];
3866 block = xfs_btree_get_block(cur, 0, &bp);
3869 error = xfs_btree_check_block(cur, block, 0, bp);
3875 * Off the right end or left end, return failure.
3877 if (ptr > xfs_btree_get_numrecs(block) || ptr <= 0) {
3883 * Point to the record and extract its data.
3885 *recp = xfs_btree_rec_addr(cur, ptr, block);
3891 * Change the owner of a btree.
3893 * The mechanism we use here is ordered buffer logging. Because we don't know
3894 * how many buffers were are going to need to modify, we don't really want to
3895 * have to make transaction reservations for the worst case of every buffer in a
3896 * full size btree as that may be more space that we can fit in the log....
3898 * We do the btree walk in the most optimal manner possible - we have sibling
3899 * pointers so we can just walk all the blocks on each level from left to right
3900 * in a single pass, and then move to the next level and do the same. We can
3901 * also do readahead on the sibling pointers to get IO moving more quickly,
3902 * though for slow disks this is unlikely to make much difference to performance
3903 * as the amount of CPU work we have to do before moving to the next block is
3906 * For each btree block that we load, modify the owner appropriately, set the
3907 * buffer as an ordered buffer and log it appropriately. We need to ensure that
3908 * we mark the region we change dirty so that if the buffer is relogged in
3909 * a subsequent transaction the changes we make here as an ordered buffer are
3910 * correctly relogged in that transaction. If we are in recovery context, then
3911 * just queue the modified buffer as delayed write buffer so the transaction
3912 * recovery completion writes the changes to disk.
3915 xfs_btree_block_change_owner(
3916 struct xfs_btree_cur *cur,
3918 __uint64_t new_owner,
3919 struct list_head *buffer_list)
3921 struct xfs_btree_block *block;
3923 union xfs_btree_ptr rptr;
3925 /* do right sibling readahead */
3926 xfs_btree_readahead(cur, level, XFS_BTCUR_RIGHTRA);
3928 /* modify the owner */
3929 block = xfs_btree_get_block(cur, level, &bp);
3930 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
3931 block->bb_u.l.bb_owner = cpu_to_be64(new_owner);
3933 block->bb_u.s.bb_owner = cpu_to_be32(new_owner);
3936 * If the block is a root block hosted in an inode, we might not have a
3937 * buffer pointer here and we shouldn't attempt to log the change as the
3938 * information is already held in the inode and discarded when the root
3939 * block is formatted into the on-disk inode fork. We still change it,
3940 * though, so everything is consistent in memory.
3944 xfs_trans_ordered_buf(cur->bc_tp, bp);
3945 xfs_btree_log_block(cur, bp, XFS_BB_OWNER);
3947 xfs_buf_delwri_queue(bp, buffer_list);
3950 ASSERT(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE);
3951 ASSERT(level == cur->bc_nlevels - 1);
3954 /* now read rh sibling block for next iteration */
3955 xfs_btree_get_sibling(cur, block, &rptr, XFS_BB_RIGHTSIB);
3956 if (xfs_btree_ptr_is_null(cur, &rptr))
3959 return xfs_btree_lookup_get_block(cur, level, &rptr, &block);
3963 xfs_btree_change_owner(
3964 struct xfs_btree_cur *cur,
3965 __uint64_t new_owner,
3966 struct list_head *buffer_list)
3968 union xfs_btree_ptr lptr;
3970 struct xfs_btree_block *block = NULL;
3973 cur->bc_ops->init_ptr_from_cur(cur, &lptr);
3975 /* for each level */
3976 for (level = cur->bc_nlevels - 1; level >= 0; level--) {
3977 /* grab the left hand block */
3978 error = xfs_btree_lookup_get_block(cur, level, &lptr, &block);
3982 /* readahead the left most block for the next level down */
3984 union xfs_btree_ptr *ptr;
3986 ptr = xfs_btree_ptr_addr(cur, 1, block);
3987 xfs_btree_readahead_ptr(cur, ptr, 1);
3989 /* save for the next iteration of the loop */
3993 /* for each buffer in the level */
3995 error = xfs_btree_block_change_owner(cur, level,
4000 if (error != ENOENT)