1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
8 #include "xfs_shared.h"
9 #include "xfs_format.h"
10 #include "xfs_log_format.h"
11 #include "xfs_trans_resv.h"
13 #include "xfs_mount.h"
14 #include "xfs_inode.h"
15 #include "xfs_trans.h"
16 #include "xfs_buf_item.h"
17 #include "xfs_btree.h"
18 #include "xfs_errortag.h"
19 #include "xfs_error.h"
20 #include "xfs_trace.h"
21 #include "xfs_alloc.h"
25 * Cursor allocation zone.
27 kmem_zone_t *xfs_btree_cur_zone;
30 * Btree magic numbers.
32 static const uint32_t xfs_magics[2][XFS_BTNUM_MAX] = {
33 { XFS_ABTB_MAGIC, XFS_ABTC_MAGIC, 0, XFS_BMAP_MAGIC, XFS_IBT_MAGIC,
35 { XFS_ABTB_CRC_MAGIC, XFS_ABTC_CRC_MAGIC, XFS_RMAP_CRC_MAGIC,
36 XFS_BMAP_CRC_MAGIC, XFS_IBT_CRC_MAGIC, XFS_FIBT_CRC_MAGIC,
45 uint32_t magic = xfs_magics[crc][btnum];
47 /* Ensure we asked for crc for crc-only magics. */
53 * Check a long btree block header. Return the address of the failing check,
54 * or NULL if everything is ok.
57 __xfs_btree_check_lblock(
58 struct xfs_btree_cur *cur,
59 struct xfs_btree_block *block,
63 struct xfs_mount *mp = cur->bc_mp;
64 xfs_btnum_t btnum = cur->bc_btnum;
65 int crc = xfs_sb_version_hascrc(&mp->m_sb);
68 if (!uuid_equal(&block->bb_u.l.bb_uuid, &mp->m_sb.sb_meta_uuid))
69 return __this_address;
70 if (block->bb_u.l.bb_blkno !=
71 cpu_to_be64(bp ? bp->b_bn : XFS_BUF_DADDR_NULL))
72 return __this_address;
73 if (block->bb_u.l.bb_pad != cpu_to_be32(0))
74 return __this_address;
77 if (be32_to_cpu(block->bb_magic) != xfs_btree_magic(crc, btnum))
78 return __this_address;
79 if (be16_to_cpu(block->bb_level) != level)
80 return __this_address;
81 if (be16_to_cpu(block->bb_numrecs) >
82 cur->bc_ops->get_maxrecs(cur, level))
83 return __this_address;
84 if (block->bb_u.l.bb_leftsib != cpu_to_be64(NULLFSBLOCK) &&
85 !xfs_btree_check_lptr(cur, be64_to_cpu(block->bb_u.l.bb_leftsib),
87 return __this_address;
88 if (block->bb_u.l.bb_rightsib != cpu_to_be64(NULLFSBLOCK) &&
89 !xfs_btree_check_lptr(cur, be64_to_cpu(block->bb_u.l.bb_rightsib),
91 return __this_address;
96 /* Check a long btree block header. */
98 xfs_btree_check_lblock(
99 struct xfs_btree_cur *cur,
100 struct xfs_btree_block *block,
104 struct xfs_mount *mp = cur->bc_mp;
107 fa = __xfs_btree_check_lblock(cur, block, level, bp);
108 if (XFS_IS_CORRUPT(mp, fa != NULL) ||
109 XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BTREE_CHECK_LBLOCK)) {
111 trace_xfs_btree_corrupt(bp, _RET_IP_);
112 return -EFSCORRUPTED;
118 * Check a short btree block header. Return the address of the failing check,
119 * or NULL if everything is ok.
122 __xfs_btree_check_sblock(
123 struct xfs_btree_cur *cur,
124 struct xfs_btree_block *block,
128 struct xfs_mount *mp = cur->bc_mp;
129 xfs_btnum_t btnum = cur->bc_btnum;
130 int crc = xfs_sb_version_hascrc(&mp->m_sb);
133 if (!uuid_equal(&block->bb_u.s.bb_uuid, &mp->m_sb.sb_meta_uuid))
134 return __this_address;
135 if (block->bb_u.s.bb_blkno !=
136 cpu_to_be64(bp ? bp->b_bn : XFS_BUF_DADDR_NULL))
137 return __this_address;
140 if (be32_to_cpu(block->bb_magic) != xfs_btree_magic(crc, btnum))
141 return __this_address;
142 if (be16_to_cpu(block->bb_level) != level)
143 return __this_address;
144 if (be16_to_cpu(block->bb_numrecs) >
145 cur->bc_ops->get_maxrecs(cur, level))
146 return __this_address;
147 if (block->bb_u.s.bb_leftsib != cpu_to_be32(NULLAGBLOCK) &&
148 !xfs_btree_check_sptr(cur, be32_to_cpu(block->bb_u.s.bb_leftsib),
150 return __this_address;
151 if (block->bb_u.s.bb_rightsib != cpu_to_be32(NULLAGBLOCK) &&
152 !xfs_btree_check_sptr(cur, be32_to_cpu(block->bb_u.s.bb_rightsib),
154 return __this_address;
159 /* Check a short btree block header. */
161 xfs_btree_check_sblock(
162 struct xfs_btree_cur *cur,
163 struct xfs_btree_block *block,
167 struct xfs_mount *mp = cur->bc_mp;
170 fa = __xfs_btree_check_sblock(cur, block, level, bp);
171 if (XFS_IS_CORRUPT(mp, fa != NULL) ||
172 XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BTREE_CHECK_SBLOCK)) {
174 trace_xfs_btree_corrupt(bp, _RET_IP_);
175 return -EFSCORRUPTED;
181 * Debug routine: check that block header is ok.
184 xfs_btree_check_block(
185 struct xfs_btree_cur *cur, /* btree cursor */
186 struct xfs_btree_block *block, /* generic btree block pointer */
187 int level, /* level of the btree block */
188 struct xfs_buf *bp) /* buffer containing block, if any */
190 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
191 return xfs_btree_check_lblock(cur, block, level, bp);
193 return xfs_btree_check_sblock(cur, block, level, bp);
196 /* Check that this long pointer is valid and points within the fs. */
198 xfs_btree_check_lptr(
199 struct xfs_btree_cur *cur,
205 return xfs_verify_fsbno(cur->bc_mp, fsbno);
208 /* Check that this short pointer is valid and points within the AG. */
210 xfs_btree_check_sptr(
211 struct xfs_btree_cur *cur,
217 return xfs_verify_agbno(cur->bc_mp, cur->bc_private.a.agno, agbno);
221 * Check that a given (indexed) btree pointer at a certain level of a
222 * btree is valid and doesn't point past where it should.
226 struct xfs_btree_cur *cur,
227 union xfs_btree_ptr *ptr,
231 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
232 if (xfs_btree_check_lptr(cur, be64_to_cpu((&ptr->l)[index]),
236 "Inode %llu fork %d: Corrupt btree %d pointer at level %d index %d.",
237 cur->bc_private.b.ip->i_ino,
238 cur->bc_private.b.whichfork, cur->bc_btnum,
241 if (xfs_btree_check_sptr(cur, be32_to_cpu((&ptr->s)[index]),
245 "AG %u: Corrupt btree %d pointer at level %d index %d.",
246 cur->bc_private.a.agno, cur->bc_btnum,
250 return -EFSCORRUPTED;
254 # define xfs_btree_debug_check_ptr xfs_btree_check_ptr
256 # define xfs_btree_debug_check_ptr(...) (0)
260 * Calculate CRC on the whole btree block and stuff it into the
261 * long-form btree header.
263 * Prior to calculting the CRC, pull the LSN out of the buffer log item and put
264 * it into the buffer so recovery knows what the last modification was that made
268 xfs_btree_lblock_calc_crc(
271 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
272 struct xfs_buf_log_item *bip = bp->b_log_item;
274 if (!xfs_sb_version_hascrc(&bp->b_mount->m_sb))
277 block->bb_u.l.bb_lsn = cpu_to_be64(bip->bli_item.li_lsn);
278 xfs_buf_update_cksum(bp, XFS_BTREE_LBLOCK_CRC_OFF);
282 xfs_btree_lblock_verify_crc(
285 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
286 struct xfs_mount *mp = bp->b_mount;
288 if (xfs_sb_version_hascrc(&mp->m_sb)) {
289 if (!xfs_log_check_lsn(mp, be64_to_cpu(block->bb_u.l.bb_lsn)))
291 return xfs_buf_verify_cksum(bp, XFS_BTREE_LBLOCK_CRC_OFF);
298 * Calculate CRC on the whole btree block and stuff it into the
299 * short-form btree header.
301 * Prior to calculting the CRC, pull the LSN out of the buffer log item and put
302 * it into the buffer so recovery knows what the last modification was that made
306 xfs_btree_sblock_calc_crc(
309 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
310 struct xfs_buf_log_item *bip = bp->b_log_item;
312 if (!xfs_sb_version_hascrc(&bp->b_mount->m_sb))
315 block->bb_u.s.bb_lsn = cpu_to_be64(bip->bli_item.li_lsn);
316 xfs_buf_update_cksum(bp, XFS_BTREE_SBLOCK_CRC_OFF);
320 xfs_btree_sblock_verify_crc(
323 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
324 struct xfs_mount *mp = bp->b_mount;
326 if (xfs_sb_version_hascrc(&mp->m_sb)) {
327 if (!xfs_log_check_lsn(mp, be64_to_cpu(block->bb_u.s.bb_lsn)))
329 return xfs_buf_verify_cksum(bp, XFS_BTREE_SBLOCK_CRC_OFF);
336 xfs_btree_free_block(
337 struct xfs_btree_cur *cur,
342 error = cur->bc_ops->free_block(cur, bp);
344 xfs_trans_binval(cur->bc_tp, bp);
345 XFS_BTREE_STATS_INC(cur, free);
351 * Delete the btree cursor.
354 xfs_btree_del_cursor(
355 xfs_btree_cur_t *cur, /* btree cursor */
356 int error) /* del because of error */
358 int i; /* btree level */
361 * Clear the buffer pointers, and release the buffers.
362 * If we're doing this in the face of an error, we
363 * need to make sure to inspect all of the entries
364 * in the bc_bufs array for buffers to be unlocked.
365 * This is because some of the btree code works from
366 * level n down to 0, and if we get an error along
367 * the way we won't have initialized all the entries
370 for (i = 0; i < cur->bc_nlevels; i++) {
372 xfs_trans_brelse(cur->bc_tp, cur->bc_bufs[i]);
377 * Can't free a bmap cursor without having dealt with the
378 * allocated indirect blocks' accounting.
380 ASSERT(cur->bc_btnum != XFS_BTNUM_BMAP ||
381 cur->bc_private.b.allocated == 0);
385 kmem_cache_free(xfs_btree_cur_zone, cur);
389 * Duplicate the btree cursor.
390 * Allocate a new one, copy the record, re-get the buffers.
393 xfs_btree_dup_cursor(
394 xfs_btree_cur_t *cur, /* input cursor */
395 xfs_btree_cur_t **ncur) /* output cursor */
397 xfs_buf_t *bp; /* btree block's buffer pointer */
398 int error; /* error return value */
399 int i; /* level number of btree block */
400 xfs_mount_t *mp; /* mount structure for filesystem */
401 xfs_btree_cur_t *new; /* new cursor value */
402 xfs_trans_t *tp; /* transaction pointer, can be NULL */
408 * Allocate a new cursor like the old one.
410 new = cur->bc_ops->dup_cursor(cur);
413 * Copy the record currently in the cursor.
415 new->bc_rec = cur->bc_rec;
418 * For each level current, re-get the buffer and copy the ptr value.
420 for (i = 0; i < new->bc_nlevels; i++) {
421 new->bc_ptrs[i] = cur->bc_ptrs[i];
422 new->bc_ra[i] = cur->bc_ra[i];
423 bp = cur->bc_bufs[i];
425 error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp,
426 XFS_BUF_ADDR(bp), mp->m_bsize,
428 cur->bc_ops->buf_ops);
430 xfs_btree_del_cursor(new, error);
435 new->bc_bufs[i] = bp;
442 * XFS btree block layout and addressing:
444 * There are two types of blocks in the btree: leaf and non-leaf blocks.
446 * The leaf record start with a header then followed by records containing
447 * the values. A non-leaf block also starts with the same header, and
448 * then first contains lookup keys followed by an equal number of pointers
449 * to the btree blocks at the previous level.
451 * +--------+-------+-------+-------+-------+-------+-------+
452 * Leaf: | header | rec 1 | rec 2 | rec 3 | rec 4 | rec 5 | rec N |
453 * +--------+-------+-------+-------+-------+-------+-------+
455 * +--------+-------+-------+-------+-------+-------+-------+
456 * Non-Leaf: | header | key 1 | key 2 | key N | ptr 1 | ptr 2 | ptr N |
457 * +--------+-------+-------+-------+-------+-------+-------+
459 * The header is called struct xfs_btree_block for reasons better left unknown
460 * and comes in different versions for short (32bit) and long (64bit) block
461 * pointers. The record and key structures are defined by the btree instances
462 * and opaque to the btree core. The block pointers are simple disk endian
463 * integers, available in a short (32bit) and long (64bit) variant.
465 * The helpers below calculate the offset of a given record, key or pointer
466 * into a btree block (xfs_btree_*_offset) or return a pointer to the given
467 * record, key or pointer (xfs_btree_*_addr). Note that all addressing
468 * inside the btree block is done using indices starting at one, not zero!
470 * If XFS_BTREE_OVERLAPPING is set, then this btree supports keys containing
471 * overlapping intervals. In such a tree, records are still sorted lowest to
472 * highest and indexed by the smallest key value that refers to the record.
473 * However, nodes are different: each pointer has two associated keys -- one
474 * indexing the lowest key available in the block(s) below (the same behavior
475 * as the key in a regular btree) and another indexing the highest key
476 * available in the block(s) below. Because records are /not/ sorted by the
477 * highest key, all leaf block updates require us to compute the highest key
478 * that matches any record in the leaf and to recursively update the high keys
479 * in the nodes going further up in the tree, if necessary. Nodes look like
482 * +--------+-----+-----+-----+-----+-----+-------+-------+-----+
483 * Non-Leaf: | header | lo1 | hi1 | lo2 | hi2 | ... | ptr 1 | ptr 2 | ... |
484 * +--------+-----+-----+-----+-----+-----+-------+-------+-----+
486 * To perform an interval query on an overlapped tree, perform the usual
487 * depth-first search and use the low and high keys to decide if we can skip
488 * that particular node. If a leaf node is reached, return the records that
489 * intersect the interval. Note that an interval query may return numerous
490 * entries. For a non-overlapped tree, simply search for the record associated
491 * with the lowest key and iterate forward until a non-matching record is
492 * found. Section 14.3 ("Interval Trees") of _Introduction to Algorithms_ by
493 * Cormen, Leiserson, Rivest, and Stein (2nd or 3rd ed. only) discuss this in
496 * Why do we care about overlapping intervals? Let's say you have a bunch of
497 * reverse mapping records on a reflink filesystem:
499 * 1: +- file A startblock B offset C length D -----------+
500 * 2: +- file E startblock F offset G length H --------------+
501 * 3: +- file I startblock F offset J length K --+
502 * 4: +- file L... --+
504 * Now say we want to map block (B+D) into file A at offset (C+D). Ideally,
505 * we'd simply increment the length of record 1. But how do we find the record
506 * that ends at (B+D-1) (i.e. record 1)? A LE lookup of (B+D-1) would return
507 * record 3 because the keys are ordered first by startblock. An interval
508 * query would return records 1 and 2 because they both overlap (B+D-1), and
509 * from that we can pick out record 1 as the appropriate left neighbor.
511 * In the non-overlapped case you can do a LE lookup and decrement the cursor
512 * because a record's interval must end before the next record.
516 * Return size of the btree block header for this btree instance.
518 static inline size_t xfs_btree_block_len(struct xfs_btree_cur *cur)
520 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
521 if (cur->bc_flags & XFS_BTREE_CRC_BLOCKS)
522 return XFS_BTREE_LBLOCK_CRC_LEN;
523 return XFS_BTREE_LBLOCK_LEN;
525 if (cur->bc_flags & XFS_BTREE_CRC_BLOCKS)
526 return XFS_BTREE_SBLOCK_CRC_LEN;
527 return XFS_BTREE_SBLOCK_LEN;
531 * Return size of btree block pointers for this btree instance.
533 static inline size_t xfs_btree_ptr_len(struct xfs_btree_cur *cur)
535 return (cur->bc_flags & XFS_BTREE_LONG_PTRS) ?
536 sizeof(__be64) : sizeof(__be32);
540 * Calculate offset of the n-th record in a btree block.
543 xfs_btree_rec_offset(
544 struct xfs_btree_cur *cur,
547 return xfs_btree_block_len(cur) +
548 (n - 1) * cur->bc_ops->rec_len;
552 * Calculate offset of the n-th key in a btree block.
555 xfs_btree_key_offset(
556 struct xfs_btree_cur *cur,
559 return xfs_btree_block_len(cur) +
560 (n - 1) * cur->bc_ops->key_len;
564 * Calculate offset of the n-th high key in a btree block.
567 xfs_btree_high_key_offset(
568 struct xfs_btree_cur *cur,
571 return xfs_btree_block_len(cur) +
572 (n - 1) * cur->bc_ops->key_len + (cur->bc_ops->key_len / 2);
576 * Calculate offset of the n-th block pointer in a btree block.
579 xfs_btree_ptr_offset(
580 struct xfs_btree_cur *cur,
584 return xfs_btree_block_len(cur) +
585 cur->bc_ops->get_maxrecs(cur, level) * cur->bc_ops->key_len +
586 (n - 1) * xfs_btree_ptr_len(cur);
590 * Return a pointer to the n-th record in the btree block.
592 union xfs_btree_rec *
594 struct xfs_btree_cur *cur,
596 struct xfs_btree_block *block)
598 return (union xfs_btree_rec *)
599 ((char *)block + xfs_btree_rec_offset(cur, n));
603 * Return a pointer to the n-th key in the btree block.
605 union xfs_btree_key *
607 struct xfs_btree_cur *cur,
609 struct xfs_btree_block *block)
611 return (union xfs_btree_key *)
612 ((char *)block + xfs_btree_key_offset(cur, n));
616 * Return a pointer to the n-th high key in the btree block.
618 union xfs_btree_key *
619 xfs_btree_high_key_addr(
620 struct xfs_btree_cur *cur,
622 struct xfs_btree_block *block)
624 return (union xfs_btree_key *)
625 ((char *)block + xfs_btree_high_key_offset(cur, n));
629 * Return a pointer to the n-th block pointer in the btree block.
631 union xfs_btree_ptr *
633 struct xfs_btree_cur *cur,
635 struct xfs_btree_block *block)
637 int level = xfs_btree_get_level(block);
639 ASSERT(block->bb_level != 0);
641 return (union xfs_btree_ptr *)
642 ((char *)block + xfs_btree_ptr_offset(cur, n, level));
646 * Get the root block which is stored in the inode.
648 * For now this btree implementation assumes the btree root is always
649 * stored in the if_broot field of an inode fork.
651 STATIC struct xfs_btree_block *
653 struct xfs_btree_cur *cur)
655 struct xfs_ifork *ifp;
657 ifp = XFS_IFORK_PTR(cur->bc_private.b.ip, cur->bc_private.b.whichfork);
658 return (struct xfs_btree_block *)ifp->if_broot;
662 * Retrieve the block pointer from the cursor at the given level.
663 * This may be an inode btree root or from a buffer.
665 struct xfs_btree_block * /* generic btree block pointer */
667 struct xfs_btree_cur *cur, /* btree cursor */
668 int level, /* level in btree */
669 struct xfs_buf **bpp) /* buffer containing the block */
671 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
672 (level == cur->bc_nlevels - 1)) {
674 return xfs_btree_get_iroot(cur);
677 *bpp = cur->bc_bufs[level];
678 return XFS_BUF_TO_BLOCK(*bpp);
682 * Change the cursor to point to the first record at the given level.
683 * Other levels are unaffected.
685 STATIC int /* success=1, failure=0 */
687 xfs_btree_cur_t *cur, /* btree cursor */
688 int level) /* level to change */
690 struct xfs_btree_block *block; /* generic btree block pointer */
691 xfs_buf_t *bp; /* buffer containing block */
694 * Get the block pointer for this level.
696 block = xfs_btree_get_block(cur, level, &bp);
697 if (xfs_btree_check_block(cur, block, level, bp))
700 * It's empty, there is no such record.
702 if (!block->bb_numrecs)
705 * Set the ptr value to 1, that's the first record/key.
707 cur->bc_ptrs[level] = 1;
712 * Change the cursor to point to the last record in the current block
713 * at the given level. Other levels are unaffected.
715 STATIC int /* success=1, failure=0 */
717 xfs_btree_cur_t *cur, /* btree cursor */
718 int level) /* level to change */
720 struct xfs_btree_block *block; /* generic btree block pointer */
721 xfs_buf_t *bp; /* buffer containing block */
724 * Get the block pointer for this level.
726 block = xfs_btree_get_block(cur, level, &bp);
727 if (xfs_btree_check_block(cur, block, level, bp))
730 * It's empty, there is no such record.
732 if (!block->bb_numrecs)
735 * Set the ptr value to numrecs, that's the last record/key.
737 cur->bc_ptrs[level] = be16_to_cpu(block->bb_numrecs);
742 * Compute first and last byte offsets for the fields given.
743 * Interprets the offsets table, which contains struct field offsets.
747 int64_t fields, /* bitmask of fields */
748 const short *offsets, /* table of field offsets */
749 int nbits, /* number of bits to inspect */
750 int *first, /* output: first byte offset */
751 int *last) /* output: last byte offset */
753 int i; /* current bit number */
754 int64_t imask; /* mask for current bit number */
758 * Find the lowest bit, so the first byte offset.
760 for (i = 0, imask = 1LL; ; i++, imask <<= 1) {
761 if (imask & fields) {
767 * Find the highest bit, so the last byte offset.
769 for (i = nbits - 1, imask = 1LL << i; ; i--, imask >>= 1) {
770 if (imask & fields) {
771 *last = offsets[i + 1] - 1;
778 * Get a buffer for the block, return it read in.
779 * Long-form addressing.
783 struct xfs_mount *mp, /* file system mount point */
784 struct xfs_trans *tp, /* transaction pointer */
785 xfs_fsblock_t fsbno, /* file system block number */
786 struct xfs_buf **bpp, /* buffer for fsbno */
787 int refval, /* ref count value for buffer */
788 const struct xfs_buf_ops *ops)
790 struct xfs_buf *bp; /* return value */
791 xfs_daddr_t d; /* real disk block address */
794 if (!xfs_verify_fsbno(mp, fsbno))
795 return -EFSCORRUPTED;
796 d = XFS_FSB_TO_DADDR(mp, fsbno);
797 error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, d,
798 mp->m_bsize, 0, &bp, ops);
802 xfs_buf_set_ref(bp, refval);
808 * Read-ahead the block, don't wait for it, don't return a buffer.
809 * Long-form addressing.
813 xfs_btree_reada_bufl(
814 struct xfs_mount *mp, /* file system mount point */
815 xfs_fsblock_t fsbno, /* file system block number */
816 xfs_extlen_t count, /* count of filesystem blocks */
817 const struct xfs_buf_ops *ops)
821 ASSERT(fsbno != NULLFSBLOCK);
822 d = XFS_FSB_TO_DADDR(mp, fsbno);
823 xfs_buf_readahead(mp->m_ddev_targp, d, mp->m_bsize * count, ops);
827 * Read-ahead the block, don't wait for it, don't return a buffer.
828 * Short-form addressing.
832 xfs_btree_reada_bufs(
833 struct xfs_mount *mp, /* file system mount point */
834 xfs_agnumber_t agno, /* allocation group number */
835 xfs_agblock_t agbno, /* allocation group block number */
836 xfs_extlen_t count, /* count of filesystem blocks */
837 const struct xfs_buf_ops *ops)
841 ASSERT(agno != NULLAGNUMBER);
842 ASSERT(agbno != NULLAGBLOCK);
843 d = XFS_AGB_TO_DADDR(mp, agno, agbno);
844 xfs_buf_readahead(mp->m_ddev_targp, d, mp->m_bsize * count, ops);
848 xfs_btree_readahead_lblock(
849 struct xfs_btree_cur *cur,
851 struct xfs_btree_block *block)
854 xfs_fsblock_t left = be64_to_cpu(block->bb_u.l.bb_leftsib);
855 xfs_fsblock_t right = be64_to_cpu(block->bb_u.l.bb_rightsib);
857 if ((lr & XFS_BTCUR_LEFTRA) && left != NULLFSBLOCK) {
858 xfs_btree_reada_bufl(cur->bc_mp, left, 1,
859 cur->bc_ops->buf_ops);
863 if ((lr & XFS_BTCUR_RIGHTRA) && right != NULLFSBLOCK) {
864 xfs_btree_reada_bufl(cur->bc_mp, right, 1,
865 cur->bc_ops->buf_ops);
873 xfs_btree_readahead_sblock(
874 struct xfs_btree_cur *cur,
876 struct xfs_btree_block *block)
879 xfs_agblock_t left = be32_to_cpu(block->bb_u.s.bb_leftsib);
880 xfs_agblock_t right = be32_to_cpu(block->bb_u.s.bb_rightsib);
883 if ((lr & XFS_BTCUR_LEFTRA) && left != NULLAGBLOCK) {
884 xfs_btree_reada_bufs(cur->bc_mp, cur->bc_private.a.agno,
885 left, 1, cur->bc_ops->buf_ops);
889 if ((lr & XFS_BTCUR_RIGHTRA) && right != NULLAGBLOCK) {
890 xfs_btree_reada_bufs(cur->bc_mp, cur->bc_private.a.agno,
891 right, 1, cur->bc_ops->buf_ops);
899 * Read-ahead btree blocks, at the given level.
900 * Bits in lr are set from XFS_BTCUR_{LEFT,RIGHT}RA.
904 struct xfs_btree_cur *cur, /* btree cursor */
905 int lev, /* level in btree */
906 int lr) /* left/right bits */
908 struct xfs_btree_block *block;
911 * No readahead needed if we are at the root level and the
912 * btree root is stored in the inode.
914 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
915 (lev == cur->bc_nlevels - 1))
918 if ((cur->bc_ra[lev] | lr) == cur->bc_ra[lev])
921 cur->bc_ra[lev] |= lr;
922 block = XFS_BUF_TO_BLOCK(cur->bc_bufs[lev]);
924 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
925 return xfs_btree_readahead_lblock(cur, lr, block);
926 return xfs_btree_readahead_sblock(cur, lr, block);
930 xfs_btree_ptr_to_daddr(
931 struct xfs_btree_cur *cur,
932 union xfs_btree_ptr *ptr,
939 error = xfs_btree_check_ptr(cur, ptr, 0, 1);
943 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
944 fsbno = be64_to_cpu(ptr->l);
945 *daddr = XFS_FSB_TO_DADDR(cur->bc_mp, fsbno);
947 agbno = be32_to_cpu(ptr->s);
948 *daddr = XFS_AGB_TO_DADDR(cur->bc_mp, cur->bc_private.a.agno,
956 * Readahead @count btree blocks at the given @ptr location.
958 * We don't need to care about long or short form btrees here as we have a
959 * method of converting the ptr directly to a daddr available to us.
962 xfs_btree_readahead_ptr(
963 struct xfs_btree_cur *cur,
964 union xfs_btree_ptr *ptr,
969 if (xfs_btree_ptr_to_daddr(cur, ptr, &daddr))
971 xfs_buf_readahead(cur->bc_mp->m_ddev_targp, daddr,
972 cur->bc_mp->m_bsize * count, cur->bc_ops->buf_ops);
976 * Set the buffer for level "lev" in the cursor to bp, releasing
977 * any previous buffer.
981 xfs_btree_cur_t *cur, /* btree cursor */
982 int lev, /* level in btree */
983 xfs_buf_t *bp) /* new buffer to set */
985 struct xfs_btree_block *b; /* btree block */
987 if (cur->bc_bufs[lev])
988 xfs_trans_brelse(cur->bc_tp, cur->bc_bufs[lev]);
989 cur->bc_bufs[lev] = bp;
992 b = XFS_BUF_TO_BLOCK(bp);
993 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
994 if (b->bb_u.l.bb_leftsib == cpu_to_be64(NULLFSBLOCK))
995 cur->bc_ra[lev] |= XFS_BTCUR_LEFTRA;
996 if (b->bb_u.l.bb_rightsib == cpu_to_be64(NULLFSBLOCK))
997 cur->bc_ra[lev] |= XFS_BTCUR_RIGHTRA;
999 if (b->bb_u.s.bb_leftsib == cpu_to_be32(NULLAGBLOCK))
1000 cur->bc_ra[lev] |= XFS_BTCUR_LEFTRA;
1001 if (b->bb_u.s.bb_rightsib == cpu_to_be32(NULLAGBLOCK))
1002 cur->bc_ra[lev] |= XFS_BTCUR_RIGHTRA;
1007 xfs_btree_ptr_is_null(
1008 struct xfs_btree_cur *cur,
1009 union xfs_btree_ptr *ptr)
1011 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
1012 return ptr->l == cpu_to_be64(NULLFSBLOCK);
1014 return ptr->s == cpu_to_be32(NULLAGBLOCK);
1018 xfs_btree_set_ptr_null(
1019 struct xfs_btree_cur *cur,
1020 union xfs_btree_ptr *ptr)
1022 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
1023 ptr->l = cpu_to_be64(NULLFSBLOCK);
1025 ptr->s = cpu_to_be32(NULLAGBLOCK);
1029 * Get/set/init sibling pointers
1032 xfs_btree_get_sibling(
1033 struct xfs_btree_cur *cur,
1034 struct xfs_btree_block *block,
1035 union xfs_btree_ptr *ptr,
1038 ASSERT(lr == XFS_BB_LEFTSIB || lr == XFS_BB_RIGHTSIB);
1040 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
1041 if (lr == XFS_BB_RIGHTSIB)
1042 ptr->l = block->bb_u.l.bb_rightsib;
1044 ptr->l = block->bb_u.l.bb_leftsib;
1046 if (lr == XFS_BB_RIGHTSIB)
1047 ptr->s = block->bb_u.s.bb_rightsib;
1049 ptr->s = block->bb_u.s.bb_leftsib;
1054 xfs_btree_set_sibling(
1055 struct xfs_btree_cur *cur,
1056 struct xfs_btree_block *block,
1057 union xfs_btree_ptr *ptr,
1060 ASSERT(lr == XFS_BB_LEFTSIB || lr == XFS_BB_RIGHTSIB);
1062 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
1063 if (lr == XFS_BB_RIGHTSIB)
1064 block->bb_u.l.bb_rightsib = ptr->l;
1066 block->bb_u.l.bb_leftsib = ptr->l;
1068 if (lr == XFS_BB_RIGHTSIB)
1069 block->bb_u.s.bb_rightsib = ptr->s;
1071 block->bb_u.s.bb_leftsib = ptr->s;
1076 xfs_btree_init_block_int(
1077 struct xfs_mount *mp,
1078 struct xfs_btree_block *buf,
1086 int crc = xfs_sb_version_hascrc(&mp->m_sb);
1087 __u32 magic = xfs_btree_magic(crc, btnum);
1089 buf->bb_magic = cpu_to_be32(magic);
1090 buf->bb_level = cpu_to_be16(level);
1091 buf->bb_numrecs = cpu_to_be16(numrecs);
1093 if (flags & XFS_BTREE_LONG_PTRS) {
1094 buf->bb_u.l.bb_leftsib = cpu_to_be64(NULLFSBLOCK);
1095 buf->bb_u.l.bb_rightsib = cpu_to_be64(NULLFSBLOCK);
1097 buf->bb_u.l.bb_blkno = cpu_to_be64(blkno);
1098 buf->bb_u.l.bb_owner = cpu_to_be64(owner);
1099 uuid_copy(&buf->bb_u.l.bb_uuid, &mp->m_sb.sb_meta_uuid);
1100 buf->bb_u.l.bb_pad = 0;
1101 buf->bb_u.l.bb_lsn = 0;
1104 /* owner is a 32 bit value on short blocks */
1105 __u32 __owner = (__u32)owner;
1107 buf->bb_u.s.bb_leftsib = cpu_to_be32(NULLAGBLOCK);
1108 buf->bb_u.s.bb_rightsib = cpu_to_be32(NULLAGBLOCK);
1110 buf->bb_u.s.bb_blkno = cpu_to_be64(blkno);
1111 buf->bb_u.s.bb_owner = cpu_to_be32(__owner);
1112 uuid_copy(&buf->bb_u.s.bb_uuid, &mp->m_sb.sb_meta_uuid);
1113 buf->bb_u.s.bb_lsn = 0;
1119 xfs_btree_init_block(
1120 struct xfs_mount *mp,
1127 xfs_btree_init_block_int(mp, XFS_BUF_TO_BLOCK(bp), bp->b_bn,
1128 btnum, level, numrecs, owner, 0);
1132 xfs_btree_init_block_cur(
1133 struct xfs_btree_cur *cur,
1141 * we can pull the owner from the cursor right now as the different
1142 * owners align directly with the pointer size of the btree. This may
1143 * change in future, but is safe for current users of the generic btree
1146 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
1147 owner = cur->bc_private.b.ip->i_ino;
1149 owner = cur->bc_private.a.agno;
1151 xfs_btree_init_block_int(cur->bc_mp, XFS_BUF_TO_BLOCK(bp), bp->b_bn,
1152 cur->bc_btnum, level, numrecs,
1153 owner, cur->bc_flags);
1157 * Return true if ptr is the last record in the btree and
1158 * we need to track updates to this record. The decision
1159 * will be further refined in the update_lastrec method.
1162 xfs_btree_is_lastrec(
1163 struct xfs_btree_cur *cur,
1164 struct xfs_btree_block *block,
1167 union xfs_btree_ptr ptr;
1171 if (!(cur->bc_flags & XFS_BTREE_LASTREC_UPDATE))
1174 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_RIGHTSIB);
1175 if (!xfs_btree_ptr_is_null(cur, &ptr))
1181 xfs_btree_buf_to_ptr(
1182 struct xfs_btree_cur *cur,
1184 union xfs_btree_ptr *ptr)
1186 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
1187 ptr->l = cpu_to_be64(XFS_DADDR_TO_FSB(cur->bc_mp,
1190 ptr->s = cpu_to_be32(xfs_daddr_to_agbno(cur->bc_mp,
1197 struct xfs_btree_cur *cur,
1200 switch (cur->bc_btnum) {
1203 xfs_buf_set_ref(bp, XFS_ALLOC_BTREE_REF);
1206 case XFS_BTNUM_FINO:
1207 xfs_buf_set_ref(bp, XFS_INO_BTREE_REF);
1209 case XFS_BTNUM_BMAP:
1210 xfs_buf_set_ref(bp, XFS_BMAP_BTREE_REF);
1212 case XFS_BTNUM_RMAP:
1213 xfs_buf_set_ref(bp, XFS_RMAP_BTREE_REF);
1215 case XFS_BTNUM_REFC:
1216 xfs_buf_set_ref(bp, XFS_REFC_BTREE_REF);
1224 xfs_btree_get_buf_block(
1225 struct xfs_btree_cur *cur,
1226 union xfs_btree_ptr *ptr,
1227 struct xfs_btree_block **block,
1228 struct xfs_buf **bpp)
1230 struct xfs_mount *mp = cur->bc_mp;
1234 error = xfs_btree_ptr_to_daddr(cur, ptr, &d);
1237 error = xfs_trans_get_buf(cur->bc_tp, mp->m_ddev_targp, d, mp->m_bsize,
1242 (*bpp)->b_ops = cur->bc_ops->buf_ops;
1243 *block = XFS_BUF_TO_BLOCK(*bpp);
1248 * Read in the buffer at the given ptr and return the buffer and
1249 * the block pointer within the buffer.
1252 xfs_btree_read_buf_block(
1253 struct xfs_btree_cur *cur,
1254 union xfs_btree_ptr *ptr,
1256 struct xfs_btree_block **block,
1257 struct xfs_buf **bpp)
1259 struct xfs_mount *mp = cur->bc_mp;
1263 /* need to sort out how callers deal with failures first */
1264 ASSERT(!(flags & XBF_TRYLOCK));
1266 error = xfs_btree_ptr_to_daddr(cur, ptr, &d);
1269 error = xfs_trans_read_buf(mp, cur->bc_tp, mp->m_ddev_targp, d,
1270 mp->m_bsize, flags, bpp,
1271 cur->bc_ops->buf_ops);
1275 xfs_btree_set_refs(cur, *bpp);
1276 *block = XFS_BUF_TO_BLOCK(*bpp);
1281 * Copy keys from one btree block to another.
1284 xfs_btree_copy_keys(
1285 struct xfs_btree_cur *cur,
1286 union xfs_btree_key *dst_key,
1287 union xfs_btree_key *src_key,
1290 ASSERT(numkeys >= 0);
1291 memcpy(dst_key, src_key, numkeys * cur->bc_ops->key_len);
1295 * Copy records from one btree block to another.
1298 xfs_btree_copy_recs(
1299 struct xfs_btree_cur *cur,
1300 union xfs_btree_rec *dst_rec,
1301 union xfs_btree_rec *src_rec,
1304 ASSERT(numrecs >= 0);
1305 memcpy(dst_rec, src_rec, numrecs * cur->bc_ops->rec_len);
1309 * Copy block pointers from one btree block to another.
1312 xfs_btree_copy_ptrs(
1313 struct xfs_btree_cur *cur,
1314 union xfs_btree_ptr *dst_ptr,
1315 union xfs_btree_ptr *src_ptr,
1318 ASSERT(numptrs >= 0);
1319 memcpy(dst_ptr, src_ptr, numptrs * xfs_btree_ptr_len(cur));
1323 * Shift keys one index left/right inside a single btree block.
1326 xfs_btree_shift_keys(
1327 struct xfs_btree_cur *cur,
1328 union xfs_btree_key *key,
1334 ASSERT(numkeys >= 0);
1335 ASSERT(dir == 1 || dir == -1);
1337 dst_key = (char *)key + (dir * cur->bc_ops->key_len);
1338 memmove(dst_key, key, numkeys * cur->bc_ops->key_len);
1342 * Shift records one index left/right inside a single btree block.
1345 xfs_btree_shift_recs(
1346 struct xfs_btree_cur *cur,
1347 union xfs_btree_rec *rec,
1353 ASSERT(numrecs >= 0);
1354 ASSERT(dir == 1 || dir == -1);
1356 dst_rec = (char *)rec + (dir * cur->bc_ops->rec_len);
1357 memmove(dst_rec, rec, numrecs * cur->bc_ops->rec_len);
1361 * Shift block pointers one index left/right inside a single btree block.
1364 xfs_btree_shift_ptrs(
1365 struct xfs_btree_cur *cur,
1366 union xfs_btree_ptr *ptr,
1372 ASSERT(numptrs >= 0);
1373 ASSERT(dir == 1 || dir == -1);
1375 dst_ptr = (char *)ptr + (dir * xfs_btree_ptr_len(cur));
1376 memmove(dst_ptr, ptr, numptrs * xfs_btree_ptr_len(cur));
1380 * Log key values from the btree block.
1384 struct xfs_btree_cur *cur,
1391 xfs_trans_buf_set_type(cur->bc_tp, bp, XFS_BLFT_BTREE_BUF);
1392 xfs_trans_log_buf(cur->bc_tp, bp,
1393 xfs_btree_key_offset(cur, first),
1394 xfs_btree_key_offset(cur, last + 1) - 1);
1396 xfs_trans_log_inode(cur->bc_tp, cur->bc_private.b.ip,
1397 xfs_ilog_fbroot(cur->bc_private.b.whichfork));
1402 * Log record values from the btree block.
1406 struct xfs_btree_cur *cur,
1412 xfs_trans_buf_set_type(cur->bc_tp, bp, XFS_BLFT_BTREE_BUF);
1413 xfs_trans_log_buf(cur->bc_tp, bp,
1414 xfs_btree_rec_offset(cur, first),
1415 xfs_btree_rec_offset(cur, last + 1) - 1);
1420 * Log block pointer fields from a btree block (nonleaf).
1424 struct xfs_btree_cur *cur, /* btree cursor */
1425 struct xfs_buf *bp, /* buffer containing btree block */
1426 int first, /* index of first pointer to log */
1427 int last) /* index of last pointer to log */
1431 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
1432 int level = xfs_btree_get_level(block);
1434 xfs_trans_buf_set_type(cur->bc_tp, bp, XFS_BLFT_BTREE_BUF);
1435 xfs_trans_log_buf(cur->bc_tp, bp,
1436 xfs_btree_ptr_offset(cur, first, level),
1437 xfs_btree_ptr_offset(cur, last + 1, level) - 1);
1439 xfs_trans_log_inode(cur->bc_tp, cur->bc_private.b.ip,
1440 xfs_ilog_fbroot(cur->bc_private.b.whichfork));
1446 * Log fields from a btree block header.
1449 xfs_btree_log_block(
1450 struct xfs_btree_cur *cur, /* btree cursor */
1451 struct xfs_buf *bp, /* buffer containing btree block */
1452 int fields) /* mask of fields: XFS_BB_... */
1454 int first; /* first byte offset logged */
1455 int last; /* last byte offset logged */
1456 static const short soffsets[] = { /* table of offsets (short) */
1457 offsetof(struct xfs_btree_block, bb_magic),
1458 offsetof(struct xfs_btree_block, bb_level),
1459 offsetof(struct xfs_btree_block, bb_numrecs),
1460 offsetof(struct xfs_btree_block, bb_u.s.bb_leftsib),
1461 offsetof(struct xfs_btree_block, bb_u.s.bb_rightsib),
1462 offsetof(struct xfs_btree_block, bb_u.s.bb_blkno),
1463 offsetof(struct xfs_btree_block, bb_u.s.bb_lsn),
1464 offsetof(struct xfs_btree_block, bb_u.s.bb_uuid),
1465 offsetof(struct xfs_btree_block, bb_u.s.bb_owner),
1466 offsetof(struct xfs_btree_block, bb_u.s.bb_crc),
1467 XFS_BTREE_SBLOCK_CRC_LEN
1469 static const short loffsets[] = { /* table of offsets (long) */
1470 offsetof(struct xfs_btree_block, bb_magic),
1471 offsetof(struct xfs_btree_block, bb_level),
1472 offsetof(struct xfs_btree_block, bb_numrecs),
1473 offsetof(struct xfs_btree_block, bb_u.l.bb_leftsib),
1474 offsetof(struct xfs_btree_block, bb_u.l.bb_rightsib),
1475 offsetof(struct xfs_btree_block, bb_u.l.bb_blkno),
1476 offsetof(struct xfs_btree_block, bb_u.l.bb_lsn),
1477 offsetof(struct xfs_btree_block, bb_u.l.bb_uuid),
1478 offsetof(struct xfs_btree_block, bb_u.l.bb_owner),
1479 offsetof(struct xfs_btree_block, bb_u.l.bb_crc),
1480 offsetof(struct xfs_btree_block, bb_u.l.bb_pad),
1481 XFS_BTREE_LBLOCK_CRC_LEN
1487 if (cur->bc_flags & XFS_BTREE_CRC_BLOCKS) {
1489 * We don't log the CRC when updating a btree
1490 * block but instead recreate it during log
1491 * recovery. As the log buffers have checksums
1492 * of their own this is safe and avoids logging a crc
1493 * update in a lot of places.
1495 if (fields == XFS_BB_ALL_BITS)
1496 fields = XFS_BB_ALL_BITS_CRC;
1497 nbits = XFS_BB_NUM_BITS_CRC;
1499 nbits = XFS_BB_NUM_BITS;
1501 xfs_btree_offsets(fields,
1502 (cur->bc_flags & XFS_BTREE_LONG_PTRS) ?
1503 loffsets : soffsets,
1504 nbits, &first, &last);
1505 xfs_trans_buf_set_type(cur->bc_tp, bp, XFS_BLFT_BTREE_BUF);
1506 xfs_trans_log_buf(cur->bc_tp, bp, first, last);
1508 xfs_trans_log_inode(cur->bc_tp, cur->bc_private.b.ip,
1509 xfs_ilog_fbroot(cur->bc_private.b.whichfork));
1514 * Increment cursor by one record at the level.
1515 * For nonzero levels the leaf-ward information is untouched.
1518 xfs_btree_increment(
1519 struct xfs_btree_cur *cur,
1521 int *stat) /* success/failure */
1523 struct xfs_btree_block *block;
1524 union xfs_btree_ptr ptr;
1526 int error; /* error return value */
1529 ASSERT(level < cur->bc_nlevels);
1531 /* Read-ahead to the right at this level. */
1532 xfs_btree_readahead(cur, level, XFS_BTCUR_RIGHTRA);
1534 /* Get a pointer to the btree block. */
1535 block = xfs_btree_get_block(cur, level, &bp);
1538 error = xfs_btree_check_block(cur, block, level, bp);
1543 /* We're done if we remain in the block after the increment. */
1544 if (++cur->bc_ptrs[level] <= xfs_btree_get_numrecs(block))
1547 /* Fail if we just went off the right edge of the tree. */
1548 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_RIGHTSIB);
1549 if (xfs_btree_ptr_is_null(cur, &ptr))
1552 XFS_BTREE_STATS_INC(cur, increment);
1555 * March up the tree incrementing pointers.
1556 * Stop when we don't go off the right edge of a block.
1558 for (lev = level + 1; lev < cur->bc_nlevels; lev++) {
1559 block = xfs_btree_get_block(cur, lev, &bp);
1562 error = xfs_btree_check_block(cur, block, lev, bp);
1567 if (++cur->bc_ptrs[lev] <= xfs_btree_get_numrecs(block))
1570 /* Read-ahead the right block for the next loop. */
1571 xfs_btree_readahead(cur, lev, XFS_BTCUR_RIGHTRA);
1575 * If we went off the root then we are either seriously
1576 * confused or have the tree root in an inode.
1578 if (lev == cur->bc_nlevels) {
1579 if (cur->bc_flags & XFS_BTREE_ROOT_IN_INODE)
1582 error = -EFSCORRUPTED;
1585 ASSERT(lev < cur->bc_nlevels);
1588 * Now walk back down the tree, fixing up the cursor's buffer
1589 * pointers and key numbers.
1591 for (block = xfs_btree_get_block(cur, lev, &bp); lev > level; ) {
1592 union xfs_btree_ptr *ptrp;
1594 ptrp = xfs_btree_ptr_addr(cur, cur->bc_ptrs[lev], block);
1596 error = xfs_btree_read_buf_block(cur, ptrp, 0, &block, &bp);
1600 xfs_btree_setbuf(cur, lev, bp);
1601 cur->bc_ptrs[lev] = 1;
1616 * Decrement cursor by one record at the level.
1617 * For nonzero levels the leaf-ward information is untouched.
1620 xfs_btree_decrement(
1621 struct xfs_btree_cur *cur,
1623 int *stat) /* success/failure */
1625 struct xfs_btree_block *block;
1627 int error; /* error return value */
1629 union xfs_btree_ptr ptr;
1631 ASSERT(level < cur->bc_nlevels);
1633 /* Read-ahead to the left at this level. */
1634 xfs_btree_readahead(cur, level, XFS_BTCUR_LEFTRA);
1636 /* We're done if we remain in the block after the decrement. */
1637 if (--cur->bc_ptrs[level] > 0)
1640 /* Get a pointer to the btree block. */
1641 block = xfs_btree_get_block(cur, level, &bp);
1644 error = xfs_btree_check_block(cur, block, level, bp);
1649 /* Fail if we just went off the left edge of the tree. */
1650 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_LEFTSIB);
1651 if (xfs_btree_ptr_is_null(cur, &ptr))
1654 XFS_BTREE_STATS_INC(cur, decrement);
1657 * March up the tree decrementing pointers.
1658 * Stop when we don't go off the left edge of a block.
1660 for (lev = level + 1; lev < cur->bc_nlevels; lev++) {
1661 if (--cur->bc_ptrs[lev] > 0)
1663 /* Read-ahead the left block for the next loop. */
1664 xfs_btree_readahead(cur, lev, XFS_BTCUR_LEFTRA);
1668 * If we went off the root then we are seriously confused.
1669 * or the root of the tree is in an inode.
1671 if (lev == cur->bc_nlevels) {
1672 if (cur->bc_flags & XFS_BTREE_ROOT_IN_INODE)
1675 error = -EFSCORRUPTED;
1678 ASSERT(lev < cur->bc_nlevels);
1681 * Now walk back down the tree, fixing up the cursor's buffer
1682 * pointers and key numbers.
1684 for (block = xfs_btree_get_block(cur, lev, &bp); lev > level; ) {
1685 union xfs_btree_ptr *ptrp;
1687 ptrp = xfs_btree_ptr_addr(cur, cur->bc_ptrs[lev], block);
1689 error = xfs_btree_read_buf_block(cur, ptrp, 0, &block, &bp);
1692 xfs_btree_setbuf(cur, lev, bp);
1693 cur->bc_ptrs[lev] = xfs_btree_get_numrecs(block);
1708 xfs_btree_lookup_get_block(
1709 struct xfs_btree_cur *cur, /* btree cursor */
1710 int level, /* level in the btree */
1711 union xfs_btree_ptr *pp, /* ptr to btree block */
1712 struct xfs_btree_block **blkp) /* return btree block */
1714 struct xfs_buf *bp; /* buffer pointer for btree block */
1718 /* special case the root block if in an inode */
1719 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
1720 (level == cur->bc_nlevels - 1)) {
1721 *blkp = xfs_btree_get_iroot(cur);
1726 * If the old buffer at this level for the disk address we are
1727 * looking for re-use it.
1729 * Otherwise throw it away and get a new one.
1731 bp = cur->bc_bufs[level];
1732 error = xfs_btree_ptr_to_daddr(cur, pp, &daddr);
1735 if (bp && XFS_BUF_ADDR(bp) == daddr) {
1736 *blkp = XFS_BUF_TO_BLOCK(bp);
1740 error = xfs_btree_read_buf_block(cur, pp, 0, blkp, &bp);
1744 /* Check the inode owner since the verifiers don't. */
1745 if (xfs_sb_version_hascrc(&cur->bc_mp->m_sb) &&
1746 !(cur->bc_private.b.flags & XFS_BTCUR_BPRV_INVALID_OWNER) &&
1747 (cur->bc_flags & XFS_BTREE_LONG_PTRS) &&
1748 be64_to_cpu((*blkp)->bb_u.l.bb_owner) !=
1749 cur->bc_private.b.ip->i_ino)
1752 /* Did we get the level we were looking for? */
1753 if (be16_to_cpu((*blkp)->bb_level) != level)
1756 /* Check that internal nodes have at least one record. */
1757 if (level != 0 && be16_to_cpu((*blkp)->bb_numrecs) == 0)
1760 xfs_btree_setbuf(cur, level, bp);
1765 xfs_buf_corruption_error(bp);
1766 xfs_trans_brelse(cur->bc_tp, bp);
1767 return -EFSCORRUPTED;
1771 * Get current search key. For level 0 we don't actually have a key
1772 * structure so we make one up from the record. For all other levels
1773 * we just return the right key.
1775 STATIC union xfs_btree_key *
1776 xfs_lookup_get_search_key(
1777 struct xfs_btree_cur *cur,
1780 struct xfs_btree_block *block,
1781 union xfs_btree_key *kp)
1784 cur->bc_ops->init_key_from_rec(kp,
1785 xfs_btree_rec_addr(cur, keyno, block));
1789 return xfs_btree_key_addr(cur, keyno, block);
1793 * Lookup the record. The cursor is made to point to it, based on dir.
1794 * stat is set to 0 if can't find any such record, 1 for success.
1798 struct xfs_btree_cur *cur, /* btree cursor */
1799 xfs_lookup_t dir, /* <=, ==, or >= */
1800 int *stat) /* success/failure */
1802 struct xfs_btree_block *block; /* current btree block */
1803 int64_t diff; /* difference for the current key */
1804 int error; /* error return value */
1805 int keyno; /* current key number */
1806 int level; /* level in the btree */
1807 union xfs_btree_ptr *pp; /* ptr to btree block */
1808 union xfs_btree_ptr ptr; /* ptr to btree block */
1810 XFS_BTREE_STATS_INC(cur, lookup);
1812 /* No such thing as a zero-level tree. */
1813 if (XFS_IS_CORRUPT(cur->bc_mp, cur->bc_nlevels == 0))
1814 return -EFSCORRUPTED;
1819 /* initialise start pointer from cursor */
1820 cur->bc_ops->init_ptr_from_cur(cur, &ptr);
1824 * Iterate over each level in the btree, starting at the root.
1825 * For each level above the leaves, find the key we need, based
1826 * on the lookup record, then follow the corresponding block
1827 * pointer down to the next level.
1829 for (level = cur->bc_nlevels - 1, diff = 1; level >= 0; level--) {
1830 /* Get the block we need to do the lookup on. */
1831 error = xfs_btree_lookup_get_block(cur, level, pp, &block);
1837 * If we already had a key match at a higher level, we
1838 * know we need to use the first entry in this block.
1842 /* Otherwise search this block. Do a binary search. */
1844 int high; /* high entry number */
1845 int low; /* low entry number */
1847 /* Set low and high entry numbers, 1-based. */
1849 high = xfs_btree_get_numrecs(block);
1851 /* Block is empty, must be an empty leaf. */
1852 if (level != 0 || cur->bc_nlevels != 1) {
1853 XFS_CORRUPTION_ERROR(__func__,
1857 return -EFSCORRUPTED;
1860 cur->bc_ptrs[0] = dir != XFS_LOOKUP_LE;
1865 /* Binary search the block. */
1866 while (low <= high) {
1867 union xfs_btree_key key;
1868 union xfs_btree_key *kp;
1870 XFS_BTREE_STATS_INC(cur, compare);
1872 /* keyno is average of low and high. */
1873 keyno = (low + high) >> 1;
1875 /* Get current search key */
1876 kp = xfs_lookup_get_search_key(cur, level,
1877 keyno, block, &key);
1880 * Compute difference to get next direction:
1881 * - less than, move right
1882 * - greater than, move left
1883 * - equal, we're done
1885 diff = cur->bc_ops->key_diff(cur, kp);
1896 * If there are more levels, set up for the next level
1897 * by getting the block number and filling in the cursor.
1901 * If we moved left, need the previous key number,
1902 * unless there isn't one.
1904 if (diff > 0 && --keyno < 1)
1906 pp = xfs_btree_ptr_addr(cur, keyno, block);
1908 error = xfs_btree_debug_check_ptr(cur, pp, 0, level);
1912 cur->bc_ptrs[level] = keyno;
1916 /* Done with the search. See if we need to adjust the results. */
1917 if (dir != XFS_LOOKUP_LE && diff < 0) {
1920 * If ge search and we went off the end of the block, but it's
1921 * not the last block, we're in the wrong block.
1923 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_RIGHTSIB);
1924 if (dir == XFS_LOOKUP_GE &&
1925 keyno > xfs_btree_get_numrecs(block) &&
1926 !xfs_btree_ptr_is_null(cur, &ptr)) {
1929 cur->bc_ptrs[0] = keyno;
1930 error = xfs_btree_increment(cur, 0, &i);
1933 if (XFS_IS_CORRUPT(cur->bc_mp, i != 1))
1934 return -EFSCORRUPTED;
1938 } else if (dir == XFS_LOOKUP_LE && diff > 0)
1940 cur->bc_ptrs[0] = keyno;
1942 /* Return if we succeeded or not. */
1943 if (keyno == 0 || keyno > xfs_btree_get_numrecs(block))
1945 else if (dir != XFS_LOOKUP_EQ || diff == 0)
1955 /* Find the high key storage area from a regular key. */
1956 union xfs_btree_key *
1957 xfs_btree_high_key_from_key(
1958 struct xfs_btree_cur *cur,
1959 union xfs_btree_key *key)
1961 ASSERT(cur->bc_flags & XFS_BTREE_OVERLAPPING);
1962 return (union xfs_btree_key *)((char *)key +
1963 (cur->bc_ops->key_len / 2));
1966 /* Determine the low (and high if overlapped) keys of a leaf block */
1968 xfs_btree_get_leaf_keys(
1969 struct xfs_btree_cur *cur,
1970 struct xfs_btree_block *block,
1971 union xfs_btree_key *key)
1973 union xfs_btree_key max_hkey;
1974 union xfs_btree_key hkey;
1975 union xfs_btree_rec *rec;
1976 union xfs_btree_key *high;
1979 rec = xfs_btree_rec_addr(cur, 1, block);
1980 cur->bc_ops->init_key_from_rec(key, rec);
1982 if (cur->bc_flags & XFS_BTREE_OVERLAPPING) {
1984 cur->bc_ops->init_high_key_from_rec(&max_hkey, rec);
1985 for (n = 2; n <= xfs_btree_get_numrecs(block); n++) {
1986 rec = xfs_btree_rec_addr(cur, n, block);
1987 cur->bc_ops->init_high_key_from_rec(&hkey, rec);
1988 if (cur->bc_ops->diff_two_keys(cur, &hkey, &max_hkey)
1993 high = xfs_btree_high_key_from_key(cur, key);
1994 memcpy(high, &max_hkey, cur->bc_ops->key_len / 2);
1998 /* Determine the low (and high if overlapped) keys of a node block */
2000 xfs_btree_get_node_keys(
2001 struct xfs_btree_cur *cur,
2002 struct xfs_btree_block *block,
2003 union xfs_btree_key *key)
2005 union xfs_btree_key *hkey;
2006 union xfs_btree_key *max_hkey;
2007 union xfs_btree_key *high;
2010 if (cur->bc_flags & XFS_BTREE_OVERLAPPING) {
2011 memcpy(key, xfs_btree_key_addr(cur, 1, block),
2012 cur->bc_ops->key_len / 2);
2014 max_hkey = xfs_btree_high_key_addr(cur, 1, block);
2015 for (n = 2; n <= xfs_btree_get_numrecs(block); n++) {
2016 hkey = xfs_btree_high_key_addr(cur, n, block);
2017 if (cur->bc_ops->diff_two_keys(cur, hkey, max_hkey) > 0)
2021 high = xfs_btree_high_key_from_key(cur, key);
2022 memcpy(high, max_hkey, cur->bc_ops->key_len / 2);
2024 memcpy(key, xfs_btree_key_addr(cur, 1, block),
2025 cur->bc_ops->key_len);
2029 /* Derive the keys for any btree block. */
2032 struct xfs_btree_cur *cur,
2033 struct xfs_btree_block *block,
2034 union xfs_btree_key *key)
2036 if (be16_to_cpu(block->bb_level) == 0)
2037 xfs_btree_get_leaf_keys(cur, block, key);
2039 xfs_btree_get_node_keys(cur, block, key);
2043 * Decide if we need to update the parent keys of a btree block. For
2044 * a standard btree this is only necessary if we're updating the first
2045 * record/key. For an overlapping btree, we must always update the
2046 * keys because the highest key can be in any of the records or keys
2050 xfs_btree_needs_key_update(
2051 struct xfs_btree_cur *cur,
2054 return (cur->bc_flags & XFS_BTREE_OVERLAPPING) || ptr == 1;
2058 * Update the low and high parent keys of the given level, progressing
2059 * towards the root. If force_all is false, stop if the keys for a given
2060 * level do not need updating.
2063 __xfs_btree_updkeys(
2064 struct xfs_btree_cur *cur,
2066 struct xfs_btree_block *block,
2067 struct xfs_buf *bp0,
2070 union xfs_btree_key key; /* keys from current level */
2071 union xfs_btree_key *lkey; /* keys from the next level up */
2072 union xfs_btree_key *hkey;
2073 union xfs_btree_key *nlkey; /* keys from the next level up */
2074 union xfs_btree_key *nhkey;
2078 ASSERT(cur->bc_flags & XFS_BTREE_OVERLAPPING);
2080 /* Exit if there aren't any parent levels to update. */
2081 if (level + 1 >= cur->bc_nlevels)
2084 trace_xfs_btree_updkeys(cur, level, bp0);
2087 hkey = xfs_btree_high_key_from_key(cur, lkey);
2088 xfs_btree_get_keys(cur, block, lkey);
2089 for (level++; level < cur->bc_nlevels; level++) {
2093 block = xfs_btree_get_block(cur, level, &bp);
2094 trace_xfs_btree_updkeys(cur, level, bp);
2096 error = xfs_btree_check_block(cur, block, level, bp);
2100 ptr = cur->bc_ptrs[level];
2101 nlkey = xfs_btree_key_addr(cur, ptr, block);
2102 nhkey = xfs_btree_high_key_addr(cur, ptr, block);
2104 !(cur->bc_ops->diff_two_keys(cur, nlkey, lkey) != 0 ||
2105 cur->bc_ops->diff_two_keys(cur, nhkey, hkey) != 0))
2107 xfs_btree_copy_keys(cur, nlkey, lkey, 1);
2108 xfs_btree_log_keys(cur, bp, ptr, ptr);
2109 if (level + 1 >= cur->bc_nlevels)
2111 xfs_btree_get_node_keys(cur, block, lkey);
2117 /* Update all the keys from some level in cursor back to the root. */
2119 xfs_btree_updkeys_force(
2120 struct xfs_btree_cur *cur,
2124 struct xfs_btree_block *block;
2126 block = xfs_btree_get_block(cur, level, &bp);
2127 return __xfs_btree_updkeys(cur, level, block, bp, true);
2131 * Update the parent keys of the given level, progressing towards the root.
2134 xfs_btree_update_keys(
2135 struct xfs_btree_cur *cur,
2138 struct xfs_btree_block *block;
2140 union xfs_btree_key *kp;
2141 union xfs_btree_key key;
2146 block = xfs_btree_get_block(cur, level, &bp);
2147 if (cur->bc_flags & XFS_BTREE_OVERLAPPING)
2148 return __xfs_btree_updkeys(cur, level, block, bp, false);
2151 * Go up the tree from this level toward the root.
2152 * At each level, update the key value to the value input.
2153 * Stop when we reach a level where the cursor isn't pointing
2154 * at the first entry in the block.
2156 xfs_btree_get_keys(cur, block, &key);
2157 for (level++, ptr = 1; ptr == 1 && level < cur->bc_nlevels; level++) {
2161 block = xfs_btree_get_block(cur, level, &bp);
2163 error = xfs_btree_check_block(cur, block, level, bp);
2167 ptr = cur->bc_ptrs[level];
2168 kp = xfs_btree_key_addr(cur, ptr, block);
2169 xfs_btree_copy_keys(cur, kp, &key, 1);
2170 xfs_btree_log_keys(cur, bp, ptr, ptr);
2177 * Update the record referred to by cur to the value in the
2178 * given record. This either works (return 0) or gets an
2179 * EFSCORRUPTED error.
2183 struct xfs_btree_cur *cur,
2184 union xfs_btree_rec *rec)
2186 struct xfs_btree_block *block;
2190 union xfs_btree_rec *rp;
2192 /* Pick up the current block. */
2193 block = xfs_btree_get_block(cur, 0, &bp);
2196 error = xfs_btree_check_block(cur, block, 0, bp);
2200 /* Get the address of the rec to be updated. */
2201 ptr = cur->bc_ptrs[0];
2202 rp = xfs_btree_rec_addr(cur, ptr, block);
2204 /* Fill in the new contents and log them. */
2205 xfs_btree_copy_recs(cur, rp, rec, 1);
2206 xfs_btree_log_recs(cur, bp, ptr, ptr);
2209 * If we are tracking the last record in the tree and
2210 * we are at the far right edge of the tree, update it.
2212 if (xfs_btree_is_lastrec(cur, block, 0)) {
2213 cur->bc_ops->update_lastrec(cur, block, rec,
2214 ptr, LASTREC_UPDATE);
2217 /* Pass new key value up to our parent. */
2218 if (xfs_btree_needs_key_update(cur, ptr)) {
2219 error = xfs_btree_update_keys(cur, 0);
2231 * Move 1 record left from cur/level if possible.
2232 * Update cur to reflect the new path.
2234 STATIC int /* error */
2236 struct xfs_btree_cur *cur,
2238 int *stat) /* success/failure */
2240 struct xfs_buf *lbp; /* left buffer pointer */
2241 struct xfs_btree_block *left; /* left btree block */
2242 int lrecs; /* left record count */
2243 struct xfs_buf *rbp; /* right buffer pointer */
2244 struct xfs_btree_block *right; /* right btree block */
2245 struct xfs_btree_cur *tcur; /* temporary btree cursor */
2246 int rrecs; /* right record count */
2247 union xfs_btree_ptr lptr; /* left btree pointer */
2248 union xfs_btree_key *rkp = NULL; /* right btree key */
2249 union xfs_btree_ptr *rpp = NULL; /* right address pointer */
2250 union xfs_btree_rec *rrp = NULL; /* right record pointer */
2251 int error; /* error return value */
2254 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
2255 level == cur->bc_nlevels - 1)
2258 /* Set up variables for this block as "right". */
2259 right = xfs_btree_get_block(cur, level, &rbp);
2262 error = xfs_btree_check_block(cur, right, level, rbp);
2267 /* If we've got no left sibling then we can't shift an entry left. */
2268 xfs_btree_get_sibling(cur, right, &lptr, XFS_BB_LEFTSIB);
2269 if (xfs_btree_ptr_is_null(cur, &lptr))
2273 * If the cursor entry is the one that would be moved, don't
2274 * do it... it's too complicated.
2276 if (cur->bc_ptrs[level] <= 1)
2279 /* Set up the left neighbor as "left". */
2280 error = xfs_btree_read_buf_block(cur, &lptr, 0, &left, &lbp);
2284 /* If it's full, it can't take another entry. */
2285 lrecs = xfs_btree_get_numrecs(left);
2286 if (lrecs == cur->bc_ops->get_maxrecs(cur, level))
2289 rrecs = xfs_btree_get_numrecs(right);
2292 * We add one entry to the left side and remove one for the right side.
2293 * Account for it here, the changes will be updated on disk and logged
2299 XFS_BTREE_STATS_INC(cur, lshift);
2300 XFS_BTREE_STATS_ADD(cur, moves, 1);
2303 * If non-leaf, copy a key and a ptr to the left block.
2304 * Log the changes to the left block.
2307 /* It's a non-leaf. Move keys and pointers. */
2308 union xfs_btree_key *lkp; /* left btree key */
2309 union xfs_btree_ptr *lpp; /* left address pointer */
2311 lkp = xfs_btree_key_addr(cur, lrecs, left);
2312 rkp = xfs_btree_key_addr(cur, 1, right);
2314 lpp = xfs_btree_ptr_addr(cur, lrecs, left);
2315 rpp = xfs_btree_ptr_addr(cur, 1, right);
2317 error = xfs_btree_debug_check_ptr(cur, rpp, 0, level);
2321 xfs_btree_copy_keys(cur, lkp, rkp, 1);
2322 xfs_btree_copy_ptrs(cur, lpp, rpp, 1);
2324 xfs_btree_log_keys(cur, lbp, lrecs, lrecs);
2325 xfs_btree_log_ptrs(cur, lbp, lrecs, lrecs);
2327 ASSERT(cur->bc_ops->keys_inorder(cur,
2328 xfs_btree_key_addr(cur, lrecs - 1, left), lkp));
2330 /* It's a leaf. Move records. */
2331 union xfs_btree_rec *lrp; /* left record pointer */
2333 lrp = xfs_btree_rec_addr(cur, lrecs, left);
2334 rrp = xfs_btree_rec_addr(cur, 1, right);
2336 xfs_btree_copy_recs(cur, lrp, rrp, 1);
2337 xfs_btree_log_recs(cur, lbp, lrecs, lrecs);
2339 ASSERT(cur->bc_ops->recs_inorder(cur,
2340 xfs_btree_rec_addr(cur, lrecs - 1, left), lrp));
2343 xfs_btree_set_numrecs(left, lrecs);
2344 xfs_btree_log_block(cur, lbp, XFS_BB_NUMRECS);
2346 xfs_btree_set_numrecs(right, rrecs);
2347 xfs_btree_log_block(cur, rbp, XFS_BB_NUMRECS);
2350 * Slide the contents of right down one entry.
2352 XFS_BTREE_STATS_ADD(cur, moves, rrecs - 1);
2354 /* It's a nonleaf. operate on keys and ptrs */
2355 for (i = 0; i < rrecs; i++) {
2356 error = xfs_btree_debug_check_ptr(cur, rpp, i + 1, level);
2361 xfs_btree_shift_keys(cur,
2362 xfs_btree_key_addr(cur, 2, right),
2364 xfs_btree_shift_ptrs(cur,
2365 xfs_btree_ptr_addr(cur, 2, right),
2368 xfs_btree_log_keys(cur, rbp, 1, rrecs);
2369 xfs_btree_log_ptrs(cur, rbp, 1, rrecs);
2371 /* It's a leaf. operate on records */
2372 xfs_btree_shift_recs(cur,
2373 xfs_btree_rec_addr(cur, 2, right),
2375 xfs_btree_log_recs(cur, rbp, 1, rrecs);
2379 * Using a temporary cursor, update the parent key values of the
2380 * block on the left.
2382 if (cur->bc_flags & XFS_BTREE_OVERLAPPING) {
2383 error = xfs_btree_dup_cursor(cur, &tcur);
2386 i = xfs_btree_firstrec(tcur, level);
2387 if (XFS_IS_CORRUPT(tcur->bc_mp, i != 1)) {
2388 error = -EFSCORRUPTED;
2392 error = xfs_btree_decrement(tcur, level, &i);
2396 /* Update the parent high keys of the left block, if needed. */
2397 error = xfs_btree_update_keys(tcur, level);
2401 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
2404 /* Update the parent keys of the right block. */
2405 error = xfs_btree_update_keys(cur, level);
2409 /* Slide the cursor value left one. */
2410 cur->bc_ptrs[level]--;
2423 xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
2428 * Move 1 record right from cur/level if possible.
2429 * Update cur to reflect the new path.
2431 STATIC int /* error */
2433 struct xfs_btree_cur *cur,
2435 int *stat) /* success/failure */
2437 struct xfs_buf *lbp; /* left buffer pointer */
2438 struct xfs_btree_block *left; /* left btree block */
2439 struct xfs_buf *rbp; /* right buffer pointer */
2440 struct xfs_btree_block *right; /* right btree block */
2441 struct xfs_btree_cur *tcur; /* temporary btree cursor */
2442 union xfs_btree_ptr rptr; /* right block pointer */
2443 union xfs_btree_key *rkp; /* right btree key */
2444 int rrecs; /* right record count */
2445 int lrecs; /* left record count */
2446 int error; /* error return value */
2447 int i; /* loop counter */
2449 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
2450 (level == cur->bc_nlevels - 1))
2453 /* Set up variables for this block as "left". */
2454 left = xfs_btree_get_block(cur, level, &lbp);
2457 error = xfs_btree_check_block(cur, left, level, lbp);
2462 /* If we've got no right sibling then we can't shift an entry right. */
2463 xfs_btree_get_sibling(cur, left, &rptr, XFS_BB_RIGHTSIB);
2464 if (xfs_btree_ptr_is_null(cur, &rptr))
2468 * If the cursor entry is the one that would be moved, don't
2469 * do it... it's too complicated.
2471 lrecs = xfs_btree_get_numrecs(left);
2472 if (cur->bc_ptrs[level] >= lrecs)
2475 /* Set up the right neighbor as "right". */
2476 error = xfs_btree_read_buf_block(cur, &rptr, 0, &right, &rbp);
2480 /* If it's full, it can't take another entry. */
2481 rrecs = xfs_btree_get_numrecs(right);
2482 if (rrecs == cur->bc_ops->get_maxrecs(cur, level))
2485 XFS_BTREE_STATS_INC(cur, rshift);
2486 XFS_BTREE_STATS_ADD(cur, moves, rrecs);
2489 * Make a hole at the start of the right neighbor block, then
2490 * copy the last left block entry to the hole.
2493 /* It's a nonleaf. make a hole in the keys and ptrs */
2494 union xfs_btree_key *lkp;
2495 union xfs_btree_ptr *lpp;
2496 union xfs_btree_ptr *rpp;
2498 lkp = xfs_btree_key_addr(cur, lrecs, left);
2499 lpp = xfs_btree_ptr_addr(cur, lrecs, left);
2500 rkp = xfs_btree_key_addr(cur, 1, right);
2501 rpp = xfs_btree_ptr_addr(cur, 1, right);
2503 for (i = rrecs - 1; i >= 0; i--) {
2504 error = xfs_btree_debug_check_ptr(cur, rpp, i, level);
2509 xfs_btree_shift_keys(cur, rkp, 1, rrecs);
2510 xfs_btree_shift_ptrs(cur, rpp, 1, rrecs);
2512 error = xfs_btree_debug_check_ptr(cur, lpp, 0, level);
2516 /* Now put the new data in, and log it. */
2517 xfs_btree_copy_keys(cur, rkp, lkp, 1);
2518 xfs_btree_copy_ptrs(cur, rpp, lpp, 1);
2520 xfs_btree_log_keys(cur, rbp, 1, rrecs + 1);
2521 xfs_btree_log_ptrs(cur, rbp, 1, rrecs + 1);
2523 ASSERT(cur->bc_ops->keys_inorder(cur, rkp,
2524 xfs_btree_key_addr(cur, 2, right)));
2526 /* It's a leaf. make a hole in the records */
2527 union xfs_btree_rec *lrp;
2528 union xfs_btree_rec *rrp;
2530 lrp = xfs_btree_rec_addr(cur, lrecs, left);
2531 rrp = xfs_btree_rec_addr(cur, 1, right);
2533 xfs_btree_shift_recs(cur, rrp, 1, rrecs);
2535 /* Now put the new data in, and log it. */
2536 xfs_btree_copy_recs(cur, rrp, lrp, 1);
2537 xfs_btree_log_recs(cur, rbp, 1, rrecs + 1);
2541 * Decrement and log left's numrecs, bump and log right's numrecs.
2543 xfs_btree_set_numrecs(left, --lrecs);
2544 xfs_btree_log_block(cur, lbp, XFS_BB_NUMRECS);
2546 xfs_btree_set_numrecs(right, ++rrecs);
2547 xfs_btree_log_block(cur, rbp, XFS_BB_NUMRECS);
2550 * Using a temporary cursor, update the parent key values of the
2551 * block on the right.
2553 error = xfs_btree_dup_cursor(cur, &tcur);
2556 i = xfs_btree_lastrec(tcur, level);
2557 if (XFS_IS_CORRUPT(tcur->bc_mp, i != 1)) {
2558 error = -EFSCORRUPTED;
2562 error = xfs_btree_increment(tcur, level, &i);
2566 /* Update the parent high keys of the left block, if needed. */
2567 if (cur->bc_flags & XFS_BTREE_OVERLAPPING) {
2568 error = xfs_btree_update_keys(cur, level);
2573 /* Update the parent keys of the right block. */
2574 error = xfs_btree_update_keys(tcur, level);
2578 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
2591 xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
2596 * Split cur/level block in half.
2597 * Return new block number and the key to its first
2598 * record (to be inserted into parent).
2600 STATIC int /* error */
2602 struct xfs_btree_cur *cur,
2604 union xfs_btree_ptr *ptrp,
2605 union xfs_btree_key *key,
2606 struct xfs_btree_cur **curp,
2607 int *stat) /* success/failure */
2609 union xfs_btree_ptr lptr; /* left sibling block ptr */
2610 struct xfs_buf *lbp; /* left buffer pointer */
2611 struct xfs_btree_block *left; /* left btree block */
2612 union xfs_btree_ptr rptr; /* right sibling block ptr */
2613 struct xfs_buf *rbp; /* right buffer pointer */
2614 struct xfs_btree_block *right; /* right btree block */
2615 union xfs_btree_ptr rrptr; /* right-right sibling ptr */
2616 struct xfs_buf *rrbp; /* right-right buffer pointer */
2617 struct xfs_btree_block *rrblock; /* right-right btree block */
2621 int error; /* error return value */
2624 XFS_BTREE_STATS_INC(cur, split);
2626 /* Set up left block (current one). */
2627 left = xfs_btree_get_block(cur, level, &lbp);
2630 error = xfs_btree_check_block(cur, left, level, lbp);
2635 xfs_btree_buf_to_ptr(cur, lbp, &lptr);
2637 /* Allocate the new block. If we can't do it, we're toast. Give up. */
2638 error = cur->bc_ops->alloc_block(cur, &lptr, &rptr, stat);
2643 XFS_BTREE_STATS_INC(cur, alloc);
2645 /* Set up the new block as "right". */
2646 error = xfs_btree_get_buf_block(cur, &rptr, &right, &rbp);
2650 /* Fill in the btree header for the new right block. */
2651 xfs_btree_init_block_cur(cur, rbp, xfs_btree_get_level(left), 0);
2654 * Split the entries between the old and the new block evenly.
2655 * Make sure that if there's an odd number of entries now, that
2656 * each new block will have the same number of entries.
2658 lrecs = xfs_btree_get_numrecs(left);
2660 if ((lrecs & 1) && cur->bc_ptrs[level] <= rrecs + 1)
2662 src_index = (lrecs - rrecs + 1);
2664 XFS_BTREE_STATS_ADD(cur, moves, rrecs);
2666 /* Adjust numrecs for the later get_*_keys() calls. */
2668 xfs_btree_set_numrecs(left, lrecs);
2669 xfs_btree_set_numrecs(right, xfs_btree_get_numrecs(right) + rrecs);
2672 * Copy btree block entries from the left block over to the
2673 * new block, the right. Update the right block and log the
2677 /* It's a non-leaf. Move keys and pointers. */
2678 union xfs_btree_key *lkp; /* left btree key */
2679 union xfs_btree_ptr *lpp; /* left address pointer */
2680 union xfs_btree_key *rkp; /* right btree key */
2681 union xfs_btree_ptr *rpp; /* right address pointer */
2683 lkp = xfs_btree_key_addr(cur, src_index, left);
2684 lpp = xfs_btree_ptr_addr(cur, src_index, left);
2685 rkp = xfs_btree_key_addr(cur, 1, right);
2686 rpp = xfs_btree_ptr_addr(cur, 1, right);
2688 for (i = src_index; i < rrecs; i++) {
2689 error = xfs_btree_debug_check_ptr(cur, lpp, i, level);
2694 /* Copy the keys & pointers to the new block. */
2695 xfs_btree_copy_keys(cur, rkp, lkp, rrecs);
2696 xfs_btree_copy_ptrs(cur, rpp, lpp, rrecs);
2698 xfs_btree_log_keys(cur, rbp, 1, rrecs);
2699 xfs_btree_log_ptrs(cur, rbp, 1, rrecs);
2701 /* Stash the keys of the new block for later insertion. */
2702 xfs_btree_get_node_keys(cur, right, key);
2704 /* It's a leaf. Move records. */
2705 union xfs_btree_rec *lrp; /* left record pointer */
2706 union xfs_btree_rec *rrp; /* right record pointer */
2708 lrp = xfs_btree_rec_addr(cur, src_index, left);
2709 rrp = xfs_btree_rec_addr(cur, 1, right);
2711 /* Copy records to the new block. */
2712 xfs_btree_copy_recs(cur, rrp, lrp, rrecs);
2713 xfs_btree_log_recs(cur, rbp, 1, rrecs);
2715 /* Stash the keys of the new block for later insertion. */
2716 xfs_btree_get_leaf_keys(cur, right, key);
2720 * Find the left block number by looking in the buffer.
2721 * Adjust sibling pointers.
2723 xfs_btree_get_sibling(cur, left, &rrptr, XFS_BB_RIGHTSIB);
2724 xfs_btree_set_sibling(cur, right, &rrptr, XFS_BB_RIGHTSIB);
2725 xfs_btree_set_sibling(cur, right, &lptr, XFS_BB_LEFTSIB);
2726 xfs_btree_set_sibling(cur, left, &rptr, XFS_BB_RIGHTSIB);
2728 xfs_btree_log_block(cur, rbp, XFS_BB_ALL_BITS);
2729 xfs_btree_log_block(cur, lbp, XFS_BB_NUMRECS | XFS_BB_RIGHTSIB);
2732 * If there's a block to the new block's right, make that block
2733 * point back to right instead of to left.
2735 if (!xfs_btree_ptr_is_null(cur, &rrptr)) {
2736 error = xfs_btree_read_buf_block(cur, &rrptr,
2737 0, &rrblock, &rrbp);
2740 xfs_btree_set_sibling(cur, rrblock, &rptr, XFS_BB_LEFTSIB);
2741 xfs_btree_log_block(cur, rrbp, XFS_BB_LEFTSIB);
2744 /* Update the parent high keys of the left block, if needed. */
2745 if (cur->bc_flags & XFS_BTREE_OVERLAPPING) {
2746 error = xfs_btree_update_keys(cur, level);
2752 * If the cursor is really in the right block, move it there.
2753 * If it's just pointing past the last entry in left, then we'll
2754 * insert there, so don't change anything in that case.
2756 if (cur->bc_ptrs[level] > lrecs + 1) {
2757 xfs_btree_setbuf(cur, level, rbp);
2758 cur->bc_ptrs[level] -= lrecs;
2761 * If there are more levels, we'll need another cursor which refers
2762 * the right block, no matter where this cursor was.
2764 if (level + 1 < cur->bc_nlevels) {
2765 error = xfs_btree_dup_cursor(cur, curp);
2768 (*curp)->bc_ptrs[level + 1]++;
2781 struct xfs_btree_split_args {
2782 struct xfs_btree_cur *cur;
2784 union xfs_btree_ptr *ptrp;
2785 union xfs_btree_key *key;
2786 struct xfs_btree_cur **curp;
2787 int *stat; /* success/failure */
2789 bool kswapd; /* allocation in kswapd context */
2790 struct completion *done;
2791 struct work_struct work;
2795 * Stack switching interfaces for allocation
2798 xfs_btree_split_worker(
2799 struct work_struct *work)
2801 struct xfs_btree_split_args *args = container_of(work,
2802 struct xfs_btree_split_args, work);
2803 unsigned long pflags;
2804 unsigned long new_pflags = PF_MEMALLOC_NOFS;
2807 * we are in a transaction context here, but may also be doing work
2808 * in kswapd context, and hence we may need to inherit that state
2809 * temporarily to ensure that we don't block waiting for memory reclaim
2813 new_pflags |= PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD;
2815 current_set_flags_nested(&pflags, new_pflags);
2817 args->result = __xfs_btree_split(args->cur, args->level, args->ptrp,
2818 args->key, args->curp, args->stat);
2819 complete(args->done);
2821 current_restore_flags_nested(&pflags, new_pflags);
2825 * BMBT split requests often come in with little stack to work on. Push
2826 * them off to a worker thread so there is lots of stack to use. For the other
2827 * btree types, just call directly to avoid the context switch overhead here.
2829 STATIC int /* error */
2831 struct xfs_btree_cur *cur,
2833 union xfs_btree_ptr *ptrp,
2834 union xfs_btree_key *key,
2835 struct xfs_btree_cur **curp,
2836 int *stat) /* success/failure */
2838 struct xfs_btree_split_args args;
2839 DECLARE_COMPLETION_ONSTACK(done);
2841 if (cur->bc_btnum != XFS_BTNUM_BMAP)
2842 return __xfs_btree_split(cur, level, ptrp, key, curp, stat);
2851 args.kswapd = current_is_kswapd();
2852 INIT_WORK_ONSTACK(&args.work, xfs_btree_split_worker);
2853 queue_work(xfs_alloc_wq, &args.work);
2854 wait_for_completion(&done);
2855 destroy_work_on_stack(&args.work);
2861 * Copy the old inode root contents into a real block and make the
2862 * broot point to it.
2865 xfs_btree_new_iroot(
2866 struct xfs_btree_cur *cur, /* btree cursor */
2867 int *logflags, /* logging flags for inode */
2868 int *stat) /* return status - 0 fail */
2870 struct xfs_buf *cbp; /* buffer for cblock */
2871 struct xfs_btree_block *block; /* btree block */
2872 struct xfs_btree_block *cblock; /* child btree block */
2873 union xfs_btree_key *ckp; /* child key pointer */
2874 union xfs_btree_ptr *cpp; /* child ptr pointer */
2875 union xfs_btree_key *kp; /* pointer to btree key */
2876 union xfs_btree_ptr *pp; /* pointer to block addr */
2877 union xfs_btree_ptr nptr; /* new block addr */
2878 int level; /* btree level */
2879 int error; /* error return code */
2880 int i; /* loop counter */
2882 XFS_BTREE_STATS_INC(cur, newroot);
2884 ASSERT(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE);
2886 level = cur->bc_nlevels - 1;
2888 block = xfs_btree_get_iroot(cur);
2889 pp = xfs_btree_ptr_addr(cur, 1, block);
2891 /* Allocate the new block. If we can't do it, we're toast. Give up. */
2892 error = cur->bc_ops->alloc_block(cur, pp, &nptr, stat);
2898 XFS_BTREE_STATS_INC(cur, alloc);
2900 /* Copy the root into a real block. */
2901 error = xfs_btree_get_buf_block(cur, &nptr, &cblock, &cbp);
2906 * we can't just memcpy() the root in for CRC enabled btree blocks.
2907 * In that case have to also ensure the blkno remains correct
2909 memcpy(cblock, block, xfs_btree_block_len(cur));
2910 if (cur->bc_flags & XFS_BTREE_CRC_BLOCKS) {
2911 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
2912 cblock->bb_u.l.bb_blkno = cpu_to_be64(cbp->b_bn);
2914 cblock->bb_u.s.bb_blkno = cpu_to_be64(cbp->b_bn);
2917 be16_add_cpu(&block->bb_level, 1);
2918 xfs_btree_set_numrecs(block, 1);
2920 cur->bc_ptrs[level + 1] = 1;
2922 kp = xfs_btree_key_addr(cur, 1, block);
2923 ckp = xfs_btree_key_addr(cur, 1, cblock);
2924 xfs_btree_copy_keys(cur, ckp, kp, xfs_btree_get_numrecs(cblock));
2926 cpp = xfs_btree_ptr_addr(cur, 1, cblock);
2927 for (i = 0; i < be16_to_cpu(cblock->bb_numrecs); i++) {
2928 error = xfs_btree_debug_check_ptr(cur, pp, i, level);
2933 xfs_btree_copy_ptrs(cur, cpp, pp, xfs_btree_get_numrecs(cblock));
2935 error = xfs_btree_debug_check_ptr(cur, &nptr, 0, level);
2939 xfs_btree_copy_ptrs(cur, pp, &nptr, 1);
2941 xfs_iroot_realloc(cur->bc_private.b.ip,
2942 1 - xfs_btree_get_numrecs(cblock),
2943 cur->bc_private.b.whichfork);
2945 xfs_btree_setbuf(cur, level, cbp);
2948 * Do all this logging at the end so that
2949 * the root is at the right level.
2951 xfs_btree_log_block(cur, cbp, XFS_BB_ALL_BITS);
2952 xfs_btree_log_keys(cur, cbp, 1, be16_to_cpu(cblock->bb_numrecs));
2953 xfs_btree_log_ptrs(cur, cbp, 1, be16_to_cpu(cblock->bb_numrecs));
2956 XFS_ILOG_CORE | xfs_ilog_fbroot(cur->bc_private.b.whichfork);
2964 * Allocate a new root block, fill it in.
2966 STATIC int /* error */
2968 struct xfs_btree_cur *cur, /* btree cursor */
2969 int *stat) /* success/failure */
2971 struct xfs_btree_block *block; /* one half of the old root block */
2972 struct xfs_buf *bp; /* buffer containing block */
2973 int error; /* error return value */
2974 struct xfs_buf *lbp; /* left buffer pointer */
2975 struct xfs_btree_block *left; /* left btree block */
2976 struct xfs_buf *nbp; /* new (root) buffer */
2977 struct xfs_btree_block *new; /* new (root) btree block */
2978 int nptr; /* new value for key index, 1 or 2 */
2979 struct xfs_buf *rbp; /* right buffer pointer */
2980 struct xfs_btree_block *right; /* right btree block */
2981 union xfs_btree_ptr rptr;
2982 union xfs_btree_ptr lptr;
2984 XFS_BTREE_STATS_INC(cur, newroot);
2986 /* initialise our start point from the cursor */
2987 cur->bc_ops->init_ptr_from_cur(cur, &rptr);
2989 /* Allocate the new block. If we can't do it, we're toast. Give up. */
2990 error = cur->bc_ops->alloc_block(cur, &rptr, &lptr, stat);
2995 XFS_BTREE_STATS_INC(cur, alloc);
2997 /* Set up the new block. */
2998 error = xfs_btree_get_buf_block(cur, &lptr, &new, &nbp);
3002 /* Set the root in the holding structure increasing the level by 1. */
3003 cur->bc_ops->set_root(cur, &lptr, 1);
3006 * At the previous root level there are now two blocks: the old root,
3007 * and the new block generated when it was split. We don't know which
3008 * one the cursor is pointing at, so we set up variables "left" and
3009 * "right" for each case.
3011 block = xfs_btree_get_block(cur, cur->bc_nlevels - 1, &bp);
3014 error = xfs_btree_check_block(cur, block, cur->bc_nlevels - 1, bp);
3019 xfs_btree_get_sibling(cur, block, &rptr, XFS_BB_RIGHTSIB);
3020 if (!xfs_btree_ptr_is_null(cur, &rptr)) {
3021 /* Our block is left, pick up the right block. */
3023 xfs_btree_buf_to_ptr(cur, lbp, &lptr);
3025 error = xfs_btree_read_buf_block(cur, &rptr, 0, &right, &rbp);
3031 /* Our block is right, pick up the left block. */
3033 xfs_btree_buf_to_ptr(cur, rbp, &rptr);
3035 xfs_btree_get_sibling(cur, right, &lptr, XFS_BB_LEFTSIB);
3036 error = xfs_btree_read_buf_block(cur, &lptr, 0, &left, &lbp);
3043 /* Fill in the new block's btree header and log it. */
3044 xfs_btree_init_block_cur(cur, nbp, cur->bc_nlevels, 2);
3045 xfs_btree_log_block(cur, nbp, XFS_BB_ALL_BITS);
3046 ASSERT(!xfs_btree_ptr_is_null(cur, &lptr) &&
3047 !xfs_btree_ptr_is_null(cur, &rptr));
3049 /* Fill in the key data in the new root. */
3050 if (xfs_btree_get_level(left) > 0) {
3052 * Get the keys for the left block's keys and put them directly
3053 * in the parent block. Do the same for the right block.
3055 xfs_btree_get_node_keys(cur, left,
3056 xfs_btree_key_addr(cur, 1, new));
3057 xfs_btree_get_node_keys(cur, right,
3058 xfs_btree_key_addr(cur, 2, new));
3061 * Get the keys for the left block's records and put them
3062 * directly in the parent block. Do the same for the right
3065 xfs_btree_get_leaf_keys(cur, left,
3066 xfs_btree_key_addr(cur, 1, new));
3067 xfs_btree_get_leaf_keys(cur, right,
3068 xfs_btree_key_addr(cur, 2, new));
3070 xfs_btree_log_keys(cur, nbp, 1, 2);
3072 /* Fill in the pointer data in the new root. */
3073 xfs_btree_copy_ptrs(cur,
3074 xfs_btree_ptr_addr(cur, 1, new), &lptr, 1);
3075 xfs_btree_copy_ptrs(cur,
3076 xfs_btree_ptr_addr(cur, 2, new), &rptr, 1);
3077 xfs_btree_log_ptrs(cur, nbp, 1, 2);
3079 /* Fix up the cursor. */
3080 xfs_btree_setbuf(cur, cur->bc_nlevels, nbp);
3081 cur->bc_ptrs[cur->bc_nlevels] = nptr;
3093 xfs_btree_make_block_unfull(
3094 struct xfs_btree_cur *cur, /* btree cursor */
3095 int level, /* btree level */
3096 int numrecs,/* # of recs in block */
3097 int *oindex,/* old tree index */
3098 int *index, /* new tree index */
3099 union xfs_btree_ptr *nptr, /* new btree ptr */
3100 struct xfs_btree_cur **ncur, /* new btree cursor */
3101 union xfs_btree_key *key, /* key of new block */
3106 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
3107 level == cur->bc_nlevels - 1) {
3108 struct xfs_inode *ip = cur->bc_private.b.ip;
3110 if (numrecs < cur->bc_ops->get_dmaxrecs(cur, level)) {
3111 /* A root block that can be made bigger. */
3112 xfs_iroot_realloc(ip, 1, cur->bc_private.b.whichfork);
3115 /* A root block that needs replacing */
3118 error = xfs_btree_new_iroot(cur, &logflags, stat);
3119 if (error || *stat == 0)
3122 xfs_trans_log_inode(cur->bc_tp, ip, logflags);
3128 /* First, try shifting an entry to the right neighbor. */
3129 error = xfs_btree_rshift(cur, level, stat);
3133 /* Next, try shifting an entry to the left neighbor. */
3134 error = xfs_btree_lshift(cur, level, stat);
3139 *oindex = *index = cur->bc_ptrs[level];
3144 * Next, try splitting the current block in half.
3146 * If this works we have to re-set our variables because we
3147 * could be in a different block now.
3149 error = xfs_btree_split(cur, level, nptr, key, ncur, stat);
3150 if (error || *stat == 0)
3154 *index = cur->bc_ptrs[level];
3159 * Insert one record/level. Return information to the caller
3160 * allowing the next level up to proceed if necessary.
3164 struct xfs_btree_cur *cur, /* btree cursor */
3165 int level, /* level to insert record at */
3166 union xfs_btree_ptr *ptrp, /* i/o: block number inserted */
3167 union xfs_btree_rec *rec, /* record to insert */
3168 union xfs_btree_key *key, /* i/o: block key for ptrp */
3169 struct xfs_btree_cur **curp, /* output: new cursor replacing cur */
3170 int *stat) /* success/failure */
3172 struct xfs_btree_block *block; /* btree block */
3173 struct xfs_buf *bp; /* buffer for block */
3174 union xfs_btree_ptr nptr; /* new block ptr */
3175 struct xfs_btree_cur *ncur; /* new btree cursor */
3176 union xfs_btree_key nkey; /* new block key */
3177 union xfs_btree_key *lkey;
3178 int optr; /* old key/record index */
3179 int ptr; /* key/record index */
3180 int numrecs;/* number of records */
3181 int error; /* error return value */
3189 * If we have an external root pointer, and we've made it to the
3190 * root level, allocate a new root block and we're done.
3192 if (!(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
3193 (level >= cur->bc_nlevels)) {
3194 error = xfs_btree_new_root(cur, stat);
3195 xfs_btree_set_ptr_null(cur, ptrp);
3200 /* If we're off the left edge, return failure. */
3201 ptr = cur->bc_ptrs[level];
3209 XFS_BTREE_STATS_INC(cur, insrec);
3211 /* Get pointers to the btree buffer and block. */
3212 block = xfs_btree_get_block(cur, level, &bp);
3213 old_bn = bp ? bp->b_bn : XFS_BUF_DADDR_NULL;
3214 numrecs = xfs_btree_get_numrecs(block);
3217 error = xfs_btree_check_block(cur, block, level, bp);
3221 /* Check that the new entry is being inserted in the right place. */
3222 if (ptr <= numrecs) {
3224 ASSERT(cur->bc_ops->recs_inorder(cur, rec,
3225 xfs_btree_rec_addr(cur, ptr, block)));
3227 ASSERT(cur->bc_ops->keys_inorder(cur, key,
3228 xfs_btree_key_addr(cur, ptr, block)));
3234 * If the block is full, we can't insert the new entry until we
3235 * make the block un-full.
3237 xfs_btree_set_ptr_null(cur, &nptr);
3238 if (numrecs == cur->bc_ops->get_maxrecs(cur, level)) {
3239 error = xfs_btree_make_block_unfull(cur, level, numrecs,
3240 &optr, &ptr, &nptr, &ncur, lkey, stat);
3241 if (error || *stat == 0)
3246 * The current block may have changed if the block was
3247 * previously full and we have just made space in it.
3249 block = xfs_btree_get_block(cur, level, &bp);
3250 numrecs = xfs_btree_get_numrecs(block);
3253 error = xfs_btree_check_block(cur, block, level, bp);
3259 * At this point we know there's room for our new entry in the block
3260 * we're pointing at.
3262 XFS_BTREE_STATS_ADD(cur, moves, numrecs - ptr + 1);
3265 /* It's a nonleaf. make a hole in the keys and ptrs */
3266 union xfs_btree_key *kp;
3267 union xfs_btree_ptr *pp;
3269 kp = xfs_btree_key_addr(cur, ptr, block);
3270 pp = xfs_btree_ptr_addr(cur, ptr, block);
3272 for (i = numrecs - ptr; i >= 0; i--) {
3273 error = xfs_btree_debug_check_ptr(cur, pp, i, level);
3278 xfs_btree_shift_keys(cur, kp, 1, numrecs - ptr + 1);
3279 xfs_btree_shift_ptrs(cur, pp, 1, numrecs - ptr + 1);
3281 error = xfs_btree_debug_check_ptr(cur, ptrp, 0, level);
3285 /* Now put the new data in, bump numrecs and log it. */
3286 xfs_btree_copy_keys(cur, kp, key, 1);
3287 xfs_btree_copy_ptrs(cur, pp, ptrp, 1);
3289 xfs_btree_set_numrecs(block, numrecs);
3290 xfs_btree_log_ptrs(cur, bp, ptr, numrecs);
3291 xfs_btree_log_keys(cur, bp, ptr, numrecs);
3293 if (ptr < numrecs) {
3294 ASSERT(cur->bc_ops->keys_inorder(cur, kp,
3295 xfs_btree_key_addr(cur, ptr + 1, block)));
3299 /* It's a leaf. make a hole in the records */
3300 union xfs_btree_rec *rp;
3302 rp = xfs_btree_rec_addr(cur, ptr, block);
3304 xfs_btree_shift_recs(cur, rp, 1, numrecs - ptr + 1);
3306 /* Now put the new data in, bump numrecs and log it. */
3307 xfs_btree_copy_recs(cur, rp, rec, 1);
3308 xfs_btree_set_numrecs(block, ++numrecs);
3309 xfs_btree_log_recs(cur, bp, ptr, numrecs);
3311 if (ptr < numrecs) {
3312 ASSERT(cur->bc_ops->recs_inorder(cur, rp,
3313 xfs_btree_rec_addr(cur, ptr + 1, block)));
3318 /* Log the new number of records in the btree header. */
3319 xfs_btree_log_block(cur, bp, XFS_BB_NUMRECS);
3322 * If we just inserted into a new tree block, we have to
3323 * recalculate nkey here because nkey is out of date.
3325 * Otherwise we're just updating an existing block (having shoved
3326 * some records into the new tree block), so use the regular key
3329 if (bp && bp->b_bn != old_bn) {
3330 xfs_btree_get_keys(cur, block, lkey);
3331 } else if (xfs_btree_needs_key_update(cur, optr)) {
3332 error = xfs_btree_update_keys(cur, level);
3338 * If we are tracking the last record in the tree and
3339 * we are at the far right edge of the tree, update it.
3341 if (xfs_btree_is_lastrec(cur, block, level)) {
3342 cur->bc_ops->update_lastrec(cur, block, rec,
3343 ptr, LASTREC_INSREC);
3347 * Return the new block number, if any.
3348 * If there is one, give back a record value and a cursor too.
3351 if (!xfs_btree_ptr_is_null(cur, &nptr)) {
3352 xfs_btree_copy_keys(cur, key, lkey, 1);
3364 * Insert the record at the point referenced by cur.
3366 * A multi-level split of the tree on insert will invalidate the original
3367 * cursor. All callers of this function should assume that the cursor is
3368 * no longer valid and revalidate it.
3372 struct xfs_btree_cur *cur,
3375 int error; /* error return value */
3376 int i; /* result value, 0 for failure */
3377 int level; /* current level number in btree */
3378 union xfs_btree_ptr nptr; /* new block number (split result) */
3379 struct xfs_btree_cur *ncur; /* new cursor (split result) */
3380 struct xfs_btree_cur *pcur; /* previous level's cursor */
3381 union xfs_btree_key bkey; /* key of block to insert */
3382 union xfs_btree_key *key;
3383 union xfs_btree_rec rec; /* record to insert */
3390 xfs_btree_set_ptr_null(cur, &nptr);
3392 /* Make a key out of the record data to be inserted, and save it. */
3393 cur->bc_ops->init_rec_from_cur(cur, &rec);
3394 cur->bc_ops->init_key_from_rec(key, &rec);
3397 * Loop going up the tree, starting at the leaf level.
3398 * Stop when we don't get a split block, that must mean that
3399 * the insert is finished with this level.
3403 * Insert nrec/nptr into this level of the tree.
3404 * Note if we fail, nptr will be null.
3406 error = xfs_btree_insrec(pcur, level, &nptr, &rec, key,
3410 xfs_btree_del_cursor(pcur, XFS_BTREE_ERROR);
3414 if (XFS_IS_CORRUPT(cur->bc_mp, i != 1)) {
3415 error = -EFSCORRUPTED;
3421 * See if the cursor we just used is trash.
3422 * Can't trash the caller's cursor, but otherwise we should
3423 * if ncur is a new cursor or we're about to be done.
3426 (ncur || xfs_btree_ptr_is_null(cur, &nptr))) {
3427 /* Save the state from the cursor before we trash it */
3428 if (cur->bc_ops->update_cursor)
3429 cur->bc_ops->update_cursor(pcur, cur);
3430 cur->bc_nlevels = pcur->bc_nlevels;
3431 xfs_btree_del_cursor(pcur, XFS_BTREE_NOERROR);
3433 /* If we got a new cursor, switch to it. */
3438 } while (!xfs_btree_ptr_is_null(cur, &nptr));
3447 * Try to merge a non-leaf block back into the inode root.
3449 * Note: the killroot names comes from the fact that we're effectively
3450 * killing the old root block. But because we can't just delete the
3451 * inode we have to copy the single block it was pointing to into the
3455 xfs_btree_kill_iroot(
3456 struct xfs_btree_cur *cur)
3458 int whichfork = cur->bc_private.b.whichfork;
3459 struct xfs_inode *ip = cur->bc_private.b.ip;
3460 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
3461 struct xfs_btree_block *block;
3462 struct xfs_btree_block *cblock;
3463 union xfs_btree_key *kp;
3464 union xfs_btree_key *ckp;
3465 union xfs_btree_ptr *pp;
3466 union xfs_btree_ptr *cpp;
3467 struct xfs_buf *cbp;
3473 union xfs_btree_ptr ptr;
3477 ASSERT(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE);
3478 ASSERT(cur->bc_nlevels > 1);
3481 * Don't deal with the root block needs to be a leaf case.
3482 * We're just going to turn the thing back into extents anyway.
3484 level = cur->bc_nlevels - 1;
3489 * Give up if the root has multiple children.
3491 block = xfs_btree_get_iroot(cur);
3492 if (xfs_btree_get_numrecs(block) != 1)
3495 cblock = xfs_btree_get_block(cur, level - 1, &cbp);
3496 numrecs = xfs_btree_get_numrecs(cblock);
3499 * Only do this if the next level will fit.
3500 * Then the data must be copied up to the inode,
3501 * instead of freeing the root you free the next level.
3503 if (numrecs > cur->bc_ops->get_dmaxrecs(cur, level))
3506 XFS_BTREE_STATS_INC(cur, killroot);
3509 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_LEFTSIB);
3510 ASSERT(xfs_btree_ptr_is_null(cur, &ptr));
3511 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_RIGHTSIB);
3512 ASSERT(xfs_btree_ptr_is_null(cur, &ptr));
3515 index = numrecs - cur->bc_ops->get_maxrecs(cur, level);
3517 xfs_iroot_realloc(cur->bc_private.b.ip, index,
3518 cur->bc_private.b.whichfork);
3519 block = ifp->if_broot;
3522 be16_add_cpu(&block->bb_numrecs, index);
3523 ASSERT(block->bb_numrecs == cblock->bb_numrecs);
3525 kp = xfs_btree_key_addr(cur, 1, block);
3526 ckp = xfs_btree_key_addr(cur, 1, cblock);
3527 xfs_btree_copy_keys(cur, kp, ckp, numrecs);
3529 pp = xfs_btree_ptr_addr(cur, 1, block);
3530 cpp = xfs_btree_ptr_addr(cur, 1, cblock);
3532 for (i = 0; i < numrecs; i++) {
3533 error = xfs_btree_debug_check_ptr(cur, cpp, i, level - 1);
3538 xfs_btree_copy_ptrs(cur, pp, cpp, numrecs);
3540 error = xfs_btree_free_block(cur, cbp);
3544 cur->bc_bufs[level - 1] = NULL;
3545 be16_add_cpu(&block->bb_level, -1);
3546 xfs_trans_log_inode(cur->bc_tp, ip,
3547 XFS_ILOG_CORE | xfs_ilog_fbroot(cur->bc_private.b.whichfork));
3554 * Kill the current root node, and replace it with it's only child node.
3557 xfs_btree_kill_root(
3558 struct xfs_btree_cur *cur,
3561 union xfs_btree_ptr *newroot)
3565 XFS_BTREE_STATS_INC(cur, killroot);
3568 * Update the root pointer, decreasing the level by 1 and then
3569 * free the old root.
3571 cur->bc_ops->set_root(cur, newroot, -1);
3573 error = xfs_btree_free_block(cur, bp);
3577 cur->bc_bufs[level] = NULL;
3578 cur->bc_ra[level] = 0;
3585 xfs_btree_dec_cursor(
3586 struct xfs_btree_cur *cur,
3594 error = xfs_btree_decrement(cur, level, &i);
3604 * Single level of the btree record deletion routine.
3605 * Delete record pointed to by cur/level.
3606 * Remove the record from its block then rebalance the tree.
3607 * Return 0 for error, 1 for done, 2 to go on to the next level.
3609 STATIC int /* error */
3611 struct xfs_btree_cur *cur, /* btree cursor */
3612 int level, /* level removing record from */
3613 int *stat) /* fail/done/go-on */
3615 struct xfs_btree_block *block; /* btree block */
3616 union xfs_btree_ptr cptr; /* current block ptr */
3617 struct xfs_buf *bp; /* buffer for block */
3618 int error; /* error return value */
3619 int i; /* loop counter */
3620 union xfs_btree_ptr lptr; /* left sibling block ptr */
3621 struct xfs_buf *lbp; /* left buffer pointer */
3622 struct xfs_btree_block *left; /* left btree block */
3623 int lrecs = 0; /* left record count */
3624 int ptr; /* key/record index */
3625 union xfs_btree_ptr rptr; /* right sibling block ptr */
3626 struct xfs_buf *rbp; /* right buffer pointer */
3627 struct xfs_btree_block *right; /* right btree block */
3628 struct xfs_btree_block *rrblock; /* right-right btree block */
3629 struct xfs_buf *rrbp; /* right-right buffer pointer */
3630 int rrecs = 0; /* right record count */
3631 struct xfs_btree_cur *tcur; /* temporary btree cursor */
3632 int numrecs; /* temporary numrec count */
3636 /* Get the index of the entry being deleted, check for nothing there. */
3637 ptr = cur->bc_ptrs[level];
3643 /* Get the buffer & block containing the record or key/ptr. */
3644 block = xfs_btree_get_block(cur, level, &bp);
3645 numrecs = xfs_btree_get_numrecs(block);
3648 error = xfs_btree_check_block(cur, block, level, bp);
3653 /* Fail if we're off the end of the block. */
3654 if (ptr > numrecs) {
3659 XFS_BTREE_STATS_INC(cur, delrec);
3660 XFS_BTREE_STATS_ADD(cur, moves, numrecs - ptr);
3662 /* Excise the entries being deleted. */
3664 /* It's a nonleaf. operate on keys and ptrs */
3665 union xfs_btree_key *lkp;
3666 union xfs_btree_ptr *lpp;
3668 lkp = xfs_btree_key_addr(cur, ptr + 1, block);
3669 lpp = xfs_btree_ptr_addr(cur, ptr + 1, block);
3671 for (i = 0; i < numrecs - ptr; i++) {
3672 error = xfs_btree_debug_check_ptr(cur, lpp, i, level);
3677 if (ptr < numrecs) {
3678 xfs_btree_shift_keys(cur, lkp, -1, numrecs - ptr);
3679 xfs_btree_shift_ptrs(cur, lpp, -1, numrecs - ptr);
3680 xfs_btree_log_keys(cur, bp, ptr, numrecs - 1);
3681 xfs_btree_log_ptrs(cur, bp, ptr, numrecs - 1);
3684 /* It's a leaf. operate on records */
3685 if (ptr < numrecs) {
3686 xfs_btree_shift_recs(cur,
3687 xfs_btree_rec_addr(cur, ptr + 1, block),
3689 xfs_btree_log_recs(cur, bp, ptr, numrecs - 1);
3694 * Decrement and log the number of entries in the block.
3696 xfs_btree_set_numrecs(block, --numrecs);
3697 xfs_btree_log_block(cur, bp, XFS_BB_NUMRECS);
3700 * If we are tracking the last record in the tree and
3701 * we are at the far right edge of the tree, update it.
3703 if (xfs_btree_is_lastrec(cur, block, level)) {
3704 cur->bc_ops->update_lastrec(cur, block, NULL,
3705 ptr, LASTREC_DELREC);
3709 * We're at the root level. First, shrink the root block in-memory.
3710 * Try to get rid of the next level down. If we can't then there's
3711 * nothing left to do.
3713 if (level == cur->bc_nlevels - 1) {
3714 if (cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) {
3715 xfs_iroot_realloc(cur->bc_private.b.ip, -1,
3716 cur->bc_private.b.whichfork);
3718 error = xfs_btree_kill_iroot(cur);
3722 error = xfs_btree_dec_cursor(cur, level, stat);
3730 * If this is the root level, and there's only one entry left,
3731 * and it's NOT the leaf level, then we can get rid of this
3734 if (numrecs == 1 && level > 0) {
3735 union xfs_btree_ptr *pp;
3737 * pp is still set to the first pointer in the block.
3738 * Make it the new root of the btree.
3740 pp = xfs_btree_ptr_addr(cur, 1, block);
3741 error = xfs_btree_kill_root(cur, bp, level, pp);
3744 } else if (level > 0) {
3745 error = xfs_btree_dec_cursor(cur, level, stat);
3754 * If we deleted the leftmost entry in the block, update the
3755 * key values above us in the tree.
3757 if (xfs_btree_needs_key_update(cur, ptr)) {
3758 error = xfs_btree_update_keys(cur, level);
3764 * If the number of records remaining in the block is at least
3765 * the minimum, we're done.
3767 if (numrecs >= cur->bc_ops->get_minrecs(cur, level)) {
3768 error = xfs_btree_dec_cursor(cur, level, stat);
3775 * Otherwise, we have to move some records around to keep the
3776 * tree balanced. Look at the left and right sibling blocks to
3777 * see if we can re-balance by moving only one record.
3779 xfs_btree_get_sibling(cur, block, &rptr, XFS_BB_RIGHTSIB);
3780 xfs_btree_get_sibling(cur, block, &lptr, XFS_BB_LEFTSIB);
3782 if (cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) {
3784 * One child of root, need to get a chance to copy its contents
3785 * into the root and delete it. Can't go up to next level,
3786 * there's nothing to delete there.
3788 if (xfs_btree_ptr_is_null(cur, &rptr) &&
3789 xfs_btree_ptr_is_null(cur, &lptr) &&
3790 level == cur->bc_nlevels - 2) {
3791 error = xfs_btree_kill_iroot(cur);
3793 error = xfs_btree_dec_cursor(cur, level, stat);
3800 ASSERT(!xfs_btree_ptr_is_null(cur, &rptr) ||
3801 !xfs_btree_ptr_is_null(cur, &lptr));
3804 * Duplicate the cursor so our btree manipulations here won't
3805 * disrupt the next level up.
3807 error = xfs_btree_dup_cursor(cur, &tcur);
3812 * If there's a right sibling, see if it's ok to shift an entry
3815 if (!xfs_btree_ptr_is_null(cur, &rptr)) {
3817 * Move the temp cursor to the last entry in the next block.
3818 * Actually any entry but the first would suffice.
3820 i = xfs_btree_lastrec(tcur, level);
3821 if (XFS_IS_CORRUPT(cur->bc_mp, i != 1)) {
3822 error = -EFSCORRUPTED;
3826 error = xfs_btree_increment(tcur, level, &i);
3829 if (XFS_IS_CORRUPT(cur->bc_mp, i != 1)) {
3830 error = -EFSCORRUPTED;
3834 i = xfs_btree_lastrec(tcur, level);
3835 if (XFS_IS_CORRUPT(cur->bc_mp, i != 1)) {
3836 error = -EFSCORRUPTED;
3840 /* Grab a pointer to the block. */
3841 right = xfs_btree_get_block(tcur, level, &rbp);
3843 error = xfs_btree_check_block(tcur, right, level, rbp);
3847 /* Grab the current block number, for future use. */
3848 xfs_btree_get_sibling(tcur, right, &cptr, XFS_BB_LEFTSIB);
3851 * If right block is full enough so that removing one entry
3852 * won't make it too empty, and left-shifting an entry out
3853 * of right to us works, we're done.
3855 if (xfs_btree_get_numrecs(right) - 1 >=
3856 cur->bc_ops->get_minrecs(tcur, level)) {
3857 error = xfs_btree_lshift(tcur, level, &i);
3861 ASSERT(xfs_btree_get_numrecs(block) >=
3862 cur->bc_ops->get_minrecs(tcur, level));
3864 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
3867 error = xfs_btree_dec_cursor(cur, level, stat);
3875 * Otherwise, grab the number of records in right for
3876 * future reference, and fix up the temp cursor to point
3877 * to our block again (last record).
3879 rrecs = xfs_btree_get_numrecs(right);
3880 if (!xfs_btree_ptr_is_null(cur, &lptr)) {
3881 i = xfs_btree_firstrec(tcur, level);
3882 if (XFS_IS_CORRUPT(cur->bc_mp, i != 1)) {
3883 error = -EFSCORRUPTED;
3887 error = xfs_btree_decrement(tcur, level, &i);
3890 if (XFS_IS_CORRUPT(cur->bc_mp, i != 1)) {
3891 error = -EFSCORRUPTED;
3898 * If there's a left sibling, see if it's ok to shift an entry
3901 if (!xfs_btree_ptr_is_null(cur, &lptr)) {
3903 * Move the temp cursor to the first entry in the
3906 i = xfs_btree_firstrec(tcur, level);
3907 if (XFS_IS_CORRUPT(cur->bc_mp, i != 1)) {
3908 error = -EFSCORRUPTED;
3912 error = xfs_btree_decrement(tcur, level, &i);
3915 i = xfs_btree_firstrec(tcur, level);
3916 if (XFS_IS_CORRUPT(cur->bc_mp, i != 1)) {
3917 error = -EFSCORRUPTED;
3921 /* Grab a pointer to the block. */
3922 left = xfs_btree_get_block(tcur, level, &lbp);
3924 error = xfs_btree_check_block(cur, left, level, lbp);
3928 /* Grab the current block number, for future use. */
3929 xfs_btree_get_sibling(tcur, left, &cptr, XFS_BB_RIGHTSIB);
3932 * If left block is full enough so that removing one entry
3933 * won't make it too empty, and right-shifting an entry out
3934 * of left to us works, we're done.
3936 if (xfs_btree_get_numrecs(left) - 1 >=
3937 cur->bc_ops->get_minrecs(tcur, level)) {
3938 error = xfs_btree_rshift(tcur, level, &i);
3942 ASSERT(xfs_btree_get_numrecs(block) >=
3943 cur->bc_ops->get_minrecs(tcur, level));
3944 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
3955 * Otherwise, grab the number of records in right for
3958 lrecs = xfs_btree_get_numrecs(left);
3961 /* Delete the temp cursor, we're done with it. */
3962 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
3965 /* If here, we need to do a join to keep the tree balanced. */
3966 ASSERT(!xfs_btree_ptr_is_null(cur, &cptr));
3968 if (!xfs_btree_ptr_is_null(cur, &lptr) &&
3969 lrecs + xfs_btree_get_numrecs(block) <=
3970 cur->bc_ops->get_maxrecs(cur, level)) {
3972 * Set "right" to be the starting block,
3973 * "left" to be the left neighbor.
3978 error = xfs_btree_read_buf_block(cur, &lptr, 0, &left, &lbp);
3983 * If that won't work, see if we can join with the right neighbor block.
3985 } else if (!xfs_btree_ptr_is_null(cur, &rptr) &&
3986 rrecs + xfs_btree_get_numrecs(block) <=
3987 cur->bc_ops->get_maxrecs(cur, level)) {
3989 * Set "left" to be the starting block,
3990 * "right" to be the right neighbor.
3995 error = xfs_btree_read_buf_block(cur, &rptr, 0, &right, &rbp);
4000 * Otherwise, we can't fix the imbalance.
4001 * Just return. This is probably a logic error, but it's not fatal.
4004 error = xfs_btree_dec_cursor(cur, level, stat);
4010 rrecs = xfs_btree_get_numrecs(right);
4011 lrecs = xfs_btree_get_numrecs(left);
4014 * We're now going to join "left" and "right" by moving all the stuff
4015 * in "right" to "left" and deleting "right".
4017 XFS_BTREE_STATS_ADD(cur, moves, rrecs);
4019 /* It's a non-leaf. Move keys and pointers. */
4020 union xfs_btree_key *lkp; /* left btree key */
4021 union xfs_btree_ptr *lpp; /* left address pointer */
4022 union xfs_btree_key *rkp; /* right btree key */
4023 union xfs_btree_ptr *rpp; /* right address pointer */
4025 lkp = xfs_btree_key_addr(cur, lrecs + 1, left);
4026 lpp = xfs_btree_ptr_addr(cur, lrecs + 1, left);
4027 rkp = xfs_btree_key_addr(cur, 1, right);
4028 rpp = xfs_btree_ptr_addr(cur, 1, right);
4030 for (i = 1; i < rrecs; i++) {
4031 error = xfs_btree_debug_check_ptr(cur, rpp, i, level);
4036 xfs_btree_copy_keys(cur, lkp, rkp, rrecs);
4037 xfs_btree_copy_ptrs(cur, lpp, rpp, rrecs);
4039 xfs_btree_log_keys(cur, lbp, lrecs + 1, lrecs + rrecs);
4040 xfs_btree_log_ptrs(cur, lbp, lrecs + 1, lrecs + rrecs);
4042 /* It's a leaf. Move records. */
4043 union xfs_btree_rec *lrp; /* left record pointer */
4044 union xfs_btree_rec *rrp; /* right record pointer */
4046 lrp = xfs_btree_rec_addr(cur, lrecs + 1, left);
4047 rrp = xfs_btree_rec_addr(cur, 1, right);
4049 xfs_btree_copy_recs(cur, lrp, rrp, rrecs);
4050 xfs_btree_log_recs(cur, lbp, lrecs + 1, lrecs + rrecs);
4053 XFS_BTREE_STATS_INC(cur, join);
4056 * Fix up the number of records and right block pointer in the
4057 * surviving block, and log it.
4059 xfs_btree_set_numrecs(left, lrecs + rrecs);
4060 xfs_btree_get_sibling(cur, right, &cptr, XFS_BB_RIGHTSIB),
4061 xfs_btree_set_sibling(cur, left, &cptr, XFS_BB_RIGHTSIB);
4062 xfs_btree_log_block(cur, lbp, XFS_BB_NUMRECS | XFS_BB_RIGHTSIB);
4064 /* If there is a right sibling, point it to the remaining block. */
4065 xfs_btree_get_sibling(cur, left, &cptr, XFS_BB_RIGHTSIB);
4066 if (!xfs_btree_ptr_is_null(cur, &cptr)) {
4067 error = xfs_btree_read_buf_block(cur, &cptr, 0, &rrblock, &rrbp);
4070 xfs_btree_set_sibling(cur, rrblock, &lptr, XFS_BB_LEFTSIB);
4071 xfs_btree_log_block(cur, rrbp, XFS_BB_LEFTSIB);
4074 /* Free the deleted block. */
4075 error = xfs_btree_free_block(cur, rbp);
4080 * If we joined with the left neighbor, set the buffer in the
4081 * cursor to the left block, and fix up the index.
4084 cur->bc_bufs[level] = lbp;
4085 cur->bc_ptrs[level] += lrecs;
4086 cur->bc_ra[level] = 0;
4089 * If we joined with the right neighbor and there's a level above
4090 * us, increment the cursor at that level.
4092 else if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) ||
4093 (level + 1 < cur->bc_nlevels)) {
4094 error = xfs_btree_increment(cur, level + 1, &i);
4100 * Readjust the ptr at this level if it's not a leaf, since it's
4101 * still pointing at the deletion point, which makes the cursor
4102 * inconsistent. If this makes the ptr 0, the caller fixes it up.
4103 * We can't use decrement because it would change the next level up.
4106 cur->bc_ptrs[level]--;
4109 * We combined blocks, so we have to update the parent keys if the
4110 * btree supports overlapped intervals. However, bc_ptrs[level + 1]
4111 * points to the old block so that the caller knows which record to
4112 * delete. Therefore, the caller must be savvy enough to call updkeys
4113 * for us if we return stat == 2. The other exit points from this
4114 * function don't require deletions further up the tree, so they can
4115 * call updkeys directly.
4118 /* Return value means the next level up has something to do. */
4124 xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
4129 * Delete the record pointed to by cur.
4130 * The cursor refers to the place where the record was (could be inserted)
4131 * when the operation returns.
4135 struct xfs_btree_cur *cur,
4136 int *stat) /* success/failure */
4138 int error; /* error return value */
4141 bool joined = false;
4144 * Go up the tree, starting at leaf level.
4146 * If 2 is returned then a join was done; go to the next level.
4147 * Otherwise we are done.
4149 for (level = 0, i = 2; i == 2; level++) {
4150 error = xfs_btree_delrec(cur, level, &i);
4158 * If we combined blocks as part of deleting the record, delrec won't
4159 * have updated the parent high keys so we have to do that here.
4161 if (joined && (cur->bc_flags & XFS_BTREE_OVERLAPPING)) {
4162 error = xfs_btree_updkeys_force(cur, 0);
4168 for (level = 1; level < cur->bc_nlevels; level++) {
4169 if (cur->bc_ptrs[level] == 0) {
4170 error = xfs_btree_decrement(cur, level, &i);
4185 * Get the data from the pointed-to record.
4189 struct xfs_btree_cur *cur, /* btree cursor */
4190 union xfs_btree_rec **recp, /* output: btree record */
4191 int *stat) /* output: success/failure */
4193 struct xfs_btree_block *block; /* btree block */
4194 struct xfs_buf *bp; /* buffer pointer */
4195 int ptr; /* record number */
4197 int error; /* error return value */
4200 ptr = cur->bc_ptrs[0];
4201 block = xfs_btree_get_block(cur, 0, &bp);
4204 error = xfs_btree_check_block(cur, block, 0, bp);
4210 * Off the right end or left end, return failure.
4212 if (ptr > xfs_btree_get_numrecs(block) || ptr <= 0) {
4218 * Point to the record and extract its data.
4220 *recp = xfs_btree_rec_addr(cur, ptr, block);
4225 /* Visit a block in a btree. */
4227 xfs_btree_visit_block(
4228 struct xfs_btree_cur *cur,
4230 xfs_btree_visit_blocks_fn fn,
4233 struct xfs_btree_block *block;
4235 union xfs_btree_ptr rptr;
4238 /* do right sibling readahead */
4239 xfs_btree_readahead(cur, level, XFS_BTCUR_RIGHTRA);
4240 block = xfs_btree_get_block(cur, level, &bp);
4242 /* process the block */
4243 error = fn(cur, level, data);
4247 /* now read rh sibling block for next iteration */
4248 xfs_btree_get_sibling(cur, block, &rptr, XFS_BB_RIGHTSIB);
4249 if (xfs_btree_ptr_is_null(cur, &rptr))
4252 return xfs_btree_lookup_get_block(cur, level, &rptr, &block);
4256 /* Visit every block in a btree. */
4258 xfs_btree_visit_blocks(
4259 struct xfs_btree_cur *cur,
4260 xfs_btree_visit_blocks_fn fn,
4264 union xfs_btree_ptr lptr;
4266 struct xfs_btree_block *block = NULL;
4269 cur->bc_ops->init_ptr_from_cur(cur, &lptr);
4271 /* for each level */
4272 for (level = cur->bc_nlevels - 1; level >= 0; level--) {
4273 /* grab the left hand block */
4274 error = xfs_btree_lookup_get_block(cur, level, &lptr, &block);
4278 /* readahead the left most block for the next level down */
4280 union xfs_btree_ptr *ptr;
4282 ptr = xfs_btree_ptr_addr(cur, 1, block);
4283 xfs_btree_readahead_ptr(cur, ptr, 1);
4285 /* save for the next iteration of the loop */
4286 xfs_btree_copy_ptrs(cur, &lptr, ptr, 1);
4288 if (!(flags & XFS_BTREE_VISIT_LEAVES))
4290 } else if (!(flags & XFS_BTREE_VISIT_RECORDS)) {
4294 /* for each buffer in the level */
4296 error = xfs_btree_visit_block(cur, level, fn, data);
4299 if (error != -ENOENT)
4307 * Change the owner of a btree.
4309 * The mechanism we use here is ordered buffer logging. Because we don't know
4310 * how many buffers were are going to need to modify, we don't really want to
4311 * have to make transaction reservations for the worst case of every buffer in a
4312 * full size btree as that may be more space that we can fit in the log....
4314 * We do the btree walk in the most optimal manner possible - we have sibling
4315 * pointers so we can just walk all the blocks on each level from left to right
4316 * in a single pass, and then move to the next level and do the same. We can
4317 * also do readahead on the sibling pointers to get IO moving more quickly,
4318 * though for slow disks this is unlikely to make much difference to performance
4319 * as the amount of CPU work we have to do before moving to the next block is
4322 * For each btree block that we load, modify the owner appropriately, set the
4323 * buffer as an ordered buffer and log it appropriately. We need to ensure that
4324 * we mark the region we change dirty so that if the buffer is relogged in
4325 * a subsequent transaction the changes we make here as an ordered buffer are
4326 * correctly relogged in that transaction. If we are in recovery context, then
4327 * just queue the modified buffer as delayed write buffer so the transaction
4328 * recovery completion writes the changes to disk.
4330 struct xfs_btree_block_change_owner_info {
4332 struct list_head *buffer_list;
4336 xfs_btree_block_change_owner(
4337 struct xfs_btree_cur *cur,
4341 struct xfs_btree_block_change_owner_info *bbcoi = data;
4342 struct xfs_btree_block *block;
4345 /* modify the owner */
4346 block = xfs_btree_get_block(cur, level, &bp);
4347 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
4348 if (block->bb_u.l.bb_owner == cpu_to_be64(bbcoi->new_owner))
4350 block->bb_u.l.bb_owner = cpu_to_be64(bbcoi->new_owner);
4352 if (block->bb_u.s.bb_owner == cpu_to_be32(bbcoi->new_owner))
4354 block->bb_u.s.bb_owner = cpu_to_be32(bbcoi->new_owner);
4358 * If the block is a root block hosted in an inode, we might not have a
4359 * buffer pointer here and we shouldn't attempt to log the change as the
4360 * information is already held in the inode and discarded when the root
4361 * block is formatted into the on-disk inode fork. We still change it,
4362 * though, so everything is consistent in memory.
4365 ASSERT(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE);
4366 ASSERT(level == cur->bc_nlevels - 1);
4371 if (!xfs_trans_ordered_buf(cur->bc_tp, bp)) {
4372 xfs_btree_log_block(cur, bp, XFS_BB_OWNER);
4376 xfs_buf_delwri_queue(bp, bbcoi->buffer_list);
4383 xfs_btree_change_owner(
4384 struct xfs_btree_cur *cur,
4386 struct list_head *buffer_list)
4388 struct xfs_btree_block_change_owner_info bbcoi;
4390 bbcoi.new_owner = new_owner;
4391 bbcoi.buffer_list = buffer_list;
4393 return xfs_btree_visit_blocks(cur, xfs_btree_block_change_owner,
4394 XFS_BTREE_VISIT_ALL, &bbcoi);
4397 /* Verify the v5 fields of a long-format btree block. */
4399 xfs_btree_lblock_v5hdr_verify(
4403 struct xfs_mount *mp = bp->b_mount;
4404 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
4406 if (!xfs_sb_version_hascrc(&mp->m_sb))
4407 return __this_address;
4408 if (!uuid_equal(&block->bb_u.l.bb_uuid, &mp->m_sb.sb_meta_uuid))
4409 return __this_address;
4410 if (block->bb_u.l.bb_blkno != cpu_to_be64(bp->b_bn))
4411 return __this_address;
4412 if (owner != XFS_RMAP_OWN_UNKNOWN &&
4413 be64_to_cpu(block->bb_u.l.bb_owner) != owner)
4414 return __this_address;
4418 /* Verify a long-format btree block. */
4420 xfs_btree_lblock_verify(
4422 unsigned int max_recs)
4424 struct xfs_mount *mp = bp->b_mount;
4425 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
4427 /* numrecs verification */
4428 if (be16_to_cpu(block->bb_numrecs) > max_recs)
4429 return __this_address;
4431 /* sibling pointer verification */
4432 if (block->bb_u.l.bb_leftsib != cpu_to_be64(NULLFSBLOCK) &&
4433 !xfs_verify_fsbno(mp, be64_to_cpu(block->bb_u.l.bb_leftsib)))
4434 return __this_address;
4435 if (block->bb_u.l.bb_rightsib != cpu_to_be64(NULLFSBLOCK) &&
4436 !xfs_verify_fsbno(mp, be64_to_cpu(block->bb_u.l.bb_rightsib)))
4437 return __this_address;
4443 * xfs_btree_sblock_v5hdr_verify() -- verify the v5 fields of a short-format
4446 * @bp: buffer containing the btree block
4449 xfs_btree_sblock_v5hdr_verify(
4452 struct xfs_mount *mp = bp->b_mount;
4453 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
4454 struct xfs_perag *pag = bp->b_pag;
4456 if (!xfs_sb_version_hascrc(&mp->m_sb))
4457 return __this_address;
4458 if (!uuid_equal(&block->bb_u.s.bb_uuid, &mp->m_sb.sb_meta_uuid))
4459 return __this_address;
4460 if (block->bb_u.s.bb_blkno != cpu_to_be64(bp->b_bn))
4461 return __this_address;
4462 if (pag && be32_to_cpu(block->bb_u.s.bb_owner) != pag->pag_agno)
4463 return __this_address;
4468 * xfs_btree_sblock_verify() -- verify a short-format btree block
4470 * @bp: buffer containing the btree block
4471 * @max_recs: maximum records allowed in this btree node
4474 xfs_btree_sblock_verify(
4476 unsigned int max_recs)
4478 struct xfs_mount *mp = bp->b_mount;
4479 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
4482 /* numrecs verification */
4483 if (be16_to_cpu(block->bb_numrecs) > max_recs)
4484 return __this_address;
4486 /* sibling pointer verification */
4487 agno = xfs_daddr_to_agno(mp, XFS_BUF_ADDR(bp));
4488 if (block->bb_u.s.bb_leftsib != cpu_to_be32(NULLAGBLOCK) &&
4489 !xfs_verify_agbno(mp, agno, be32_to_cpu(block->bb_u.s.bb_leftsib)))
4490 return __this_address;
4491 if (block->bb_u.s.bb_rightsib != cpu_to_be32(NULLAGBLOCK) &&
4492 !xfs_verify_agbno(mp, agno, be32_to_cpu(block->bb_u.s.bb_rightsib)))
4493 return __this_address;
4499 * Calculate the number of btree levels needed to store a given number of
4500 * records in a short-format btree.
4503 xfs_btree_compute_maxlevels(
4508 unsigned long maxblocks;
4510 maxblocks = (len + limits[0] - 1) / limits[0];
4511 for (level = 1; maxblocks > 1; level++)
4512 maxblocks = (maxblocks + limits[1] - 1) / limits[1];
4517 * Query a regular btree for all records overlapping a given interval.
4518 * Start with a LE lookup of the key of low_rec and return all records
4519 * until we find a record with a key greater than the key of high_rec.
4522 xfs_btree_simple_query_range(
4523 struct xfs_btree_cur *cur,
4524 union xfs_btree_key *low_key,
4525 union xfs_btree_key *high_key,
4526 xfs_btree_query_range_fn fn,
4529 union xfs_btree_rec *recp;
4530 union xfs_btree_key rec_key;
4533 bool firstrec = true;
4536 ASSERT(cur->bc_ops->init_high_key_from_rec);
4537 ASSERT(cur->bc_ops->diff_two_keys);
4540 * Find the leftmost record. The btree cursor must be set
4541 * to the low record used to generate low_key.
4544 error = xfs_btree_lookup(cur, XFS_LOOKUP_LE, &stat);
4548 /* Nothing? See if there's anything to the right. */
4550 error = xfs_btree_increment(cur, 0, &stat);
4556 /* Find the record. */
4557 error = xfs_btree_get_rec(cur, &recp, &stat);
4561 /* Skip if high_key(rec) < low_key. */
4563 cur->bc_ops->init_high_key_from_rec(&rec_key, recp);
4565 diff = cur->bc_ops->diff_two_keys(cur, low_key,
4571 /* Stop if high_key < low_key(rec). */
4572 cur->bc_ops->init_key_from_rec(&rec_key, recp);
4573 diff = cur->bc_ops->diff_two_keys(cur, &rec_key, high_key);
4578 error = fn(cur, recp, priv);
4583 /* Move on to the next record. */
4584 error = xfs_btree_increment(cur, 0, &stat);
4594 * Query an overlapped interval btree for all records overlapping a given
4595 * interval. This function roughly follows the algorithm given in
4596 * "Interval Trees" of _Introduction to Algorithms_, which is section
4597 * 14.3 in the 2nd and 3rd editions.
4599 * First, generate keys for the low and high records passed in.
4601 * For any leaf node, generate the high and low keys for the record.
4602 * If the record keys overlap with the query low/high keys, pass the
4603 * record to the function iterator.
4605 * For any internal node, compare the low and high keys of each
4606 * pointer against the query low/high keys. If there's an overlap,
4607 * follow the pointer.
4609 * As an optimization, we stop scanning a block when we find a low key
4610 * that is greater than the query's high key.
4613 xfs_btree_overlapped_query_range(
4614 struct xfs_btree_cur *cur,
4615 union xfs_btree_key *low_key,
4616 union xfs_btree_key *high_key,
4617 xfs_btree_query_range_fn fn,
4620 union xfs_btree_ptr ptr;
4621 union xfs_btree_ptr *pp;
4622 union xfs_btree_key rec_key;
4623 union xfs_btree_key rec_hkey;
4624 union xfs_btree_key *lkp;
4625 union xfs_btree_key *hkp;
4626 union xfs_btree_rec *recp;
4627 struct xfs_btree_block *block;
4635 /* Load the root of the btree. */
4636 level = cur->bc_nlevels - 1;
4637 cur->bc_ops->init_ptr_from_cur(cur, &ptr);
4638 error = xfs_btree_lookup_get_block(cur, level, &ptr, &block);
4641 xfs_btree_get_block(cur, level, &bp);
4642 trace_xfs_btree_overlapped_query_range(cur, level, bp);
4644 error = xfs_btree_check_block(cur, block, level, bp);
4648 cur->bc_ptrs[level] = 1;
4650 while (level < cur->bc_nlevels) {
4651 block = xfs_btree_get_block(cur, level, &bp);
4653 /* End of node, pop back towards the root. */
4654 if (cur->bc_ptrs[level] > be16_to_cpu(block->bb_numrecs)) {
4656 if (level < cur->bc_nlevels - 1)
4657 cur->bc_ptrs[level + 1]++;
4663 /* Handle a leaf node. */
4664 recp = xfs_btree_rec_addr(cur, cur->bc_ptrs[0], block);
4666 cur->bc_ops->init_high_key_from_rec(&rec_hkey, recp);
4667 ldiff = cur->bc_ops->diff_two_keys(cur, &rec_hkey,
4670 cur->bc_ops->init_key_from_rec(&rec_key, recp);
4671 hdiff = cur->bc_ops->diff_two_keys(cur, high_key,
4675 * If (record's high key >= query's low key) and
4676 * (query's high key >= record's low key), then
4677 * this record overlaps the query range; callback.
4679 if (ldiff >= 0 && hdiff >= 0) {
4680 error = fn(cur, recp, priv);
4683 } else if (hdiff < 0) {
4684 /* Record is larger than high key; pop. */
4687 cur->bc_ptrs[level]++;
4691 /* Handle an internal node. */
4692 lkp = xfs_btree_key_addr(cur, cur->bc_ptrs[level], block);
4693 hkp = xfs_btree_high_key_addr(cur, cur->bc_ptrs[level], block);
4694 pp = xfs_btree_ptr_addr(cur, cur->bc_ptrs[level], block);
4696 ldiff = cur->bc_ops->diff_two_keys(cur, hkp, low_key);
4697 hdiff = cur->bc_ops->diff_two_keys(cur, high_key, lkp);
4700 * If (pointer's high key >= query's low key) and
4701 * (query's high key >= pointer's low key), then
4702 * this record overlaps the query range; follow pointer.
4704 if (ldiff >= 0 && hdiff >= 0) {
4706 error = xfs_btree_lookup_get_block(cur, level, pp,
4710 xfs_btree_get_block(cur, level, &bp);
4711 trace_xfs_btree_overlapped_query_range(cur, level, bp);
4713 error = xfs_btree_check_block(cur, block, level, bp);
4717 cur->bc_ptrs[level] = 1;
4719 } else if (hdiff < 0) {
4720 /* The low key is larger than the upper range; pop. */
4723 cur->bc_ptrs[level]++;
4728 * If we don't end this function with the cursor pointing at a record
4729 * block, a subsequent non-error cursor deletion will not release
4730 * node-level buffers, causing a buffer leak. This is quite possible
4731 * with a zero-results range query, so release the buffers if we
4732 * failed to return any results.
4734 if (cur->bc_bufs[0] == NULL) {
4735 for (i = 0; i < cur->bc_nlevels; i++) {
4736 if (cur->bc_bufs[i]) {
4737 xfs_trans_brelse(cur->bc_tp, cur->bc_bufs[i]);
4738 cur->bc_bufs[i] = NULL;
4739 cur->bc_ptrs[i] = 0;
4749 * Query a btree for all records overlapping a given interval of keys. The
4750 * supplied function will be called with each record found; return one of the
4751 * XFS_BTREE_QUERY_RANGE_{CONTINUE,ABORT} values or the usual negative error
4752 * code. This function returns -ECANCELED, zero, or a negative error code.
4755 xfs_btree_query_range(
4756 struct xfs_btree_cur *cur,
4757 union xfs_btree_irec *low_rec,
4758 union xfs_btree_irec *high_rec,
4759 xfs_btree_query_range_fn fn,
4762 union xfs_btree_rec rec;
4763 union xfs_btree_key low_key;
4764 union xfs_btree_key high_key;
4766 /* Find the keys of both ends of the interval. */
4767 cur->bc_rec = *high_rec;
4768 cur->bc_ops->init_rec_from_cur(cur, &rec);
4769 cur->bc_ops->init_key_from_rec(&high_key, &rec);
4771 cur->bc_rec = *low_rec;
4772 cur->bc_ops->init_rec_from_cur(cur, &rec);
4773 cur->bc_ops->init_key_from_rec(&low_key, &rec);
4775 /* Enforce low key < high key. */
4776 if (cur->bc_ops->diff_two_keys(cur, &low_key, &high_key) > 0)
4779 if (!(cur->bc_flags & XFS_BTREE_OVERLAPPING))
4780 return xfs_btree_simple_query_range(cur, &low_key,
4781 &high_key, fn, priv);
4782 return xfs_btree_overlapped_query_range(cur, &low_key, &high_key,
4786 /* Query a btree for all records. */
4788 xfs_btree_query_all(
4789 struct xfs_btree_cur *cur,
4790 xfs_btree_query_range_fn fn,
4793 union xfs_btree_key low_key;
4794 union xfs_btree_key high_key;
4796 memset(&cur->bc_rec, 0, sizeof(cur->bc_rec));
4797 memset(&low_key, 0, sizeof(low_key));
4798 memset(&high_key, 0xFF, sizeof(high_key));
4800 return xfs_btree_simple_query_range(cur, &low_key, &high_key, fn, priv);
4804 * Calculate the number of blocks needed to store a given number of records
4805 * in a short-format (per-AG metadata) btree.
4808 xfs_btree_calc_size(
4810 unsigned long long len)
4814 unsigned long long rval;
4816 maxrecs = limits[0];
4817 for (level = 0, rval = 0; len > 1; level++) {
4819 do_div(len, maxrecs);
4820 maxrecs = limits[1];
4827 xfs_btree_count_blocks_helper(
4828 struct xfs_btree_cur *cur,
4832 xfs_extlen_t *blocks = data;
4838 /* Count the blocks in a btree and return the result in *blocks. */
4840 xfs_btree_count_blocks(
4841 struct xfs_btree_cur *cur,
4842 xfs_extlen_t *blocks)
4845 return xfs_btree_visit_blocks(cur, xfs_btree_count_blocks_helper,
4846 XFS_BTREE_VISIT_ALL, blocks);
4849 /* Compare two btree pointers. */
4851 xfs_btree_diff_two_ptrs(
4852 struct xfs_btree_cur *cur,
4853 const union xfs_btree_ptr *a,
4854 const union xfs_btree_ptr *b)
4856 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
4857 return (int64_t)be64_to_cpu(a->l) - be64_to_cpu(b->l);
4858 return (int64_t)be32_to_cpu(a->s) - be32_to_cpu(b->s);
4861 /* If there's an extent, we're done. */
4863 xfs_btree_has_record_helper(
4864 struct xfs_btree_cur *cur,
4865 union xfs_btree_rec *rec,
4871 /* Is there a record covering a given range of keys? */
4873 xfs_btree_has_record(
4874 struct xfs_btree_cur *cur,
4875 union xfs_btree_irec *low,
4876 union xfs_btree_irec *high,
4881 error = xfs_btree_query_range(cur, low, high,
4882 &xfs_btree_has_record_helper, NULL);
4883 if (error == -ECANCELED) {
4891 /* Are there more records in this btree? */
4893 xfs_btree_has_more_records(
4894 struct xfs_btree_cur *cur)
4896 struct xfs_btree_block *block;
4899 block = xfs_btree_get_block(cur, 0, &bp);
4901 /* There are still records in this block. */
4902 if (cur->bc_ptrs[0] < xfs_btree_get_numrecs(block))
4905 /* There are more record blocks. */
4906 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
4907 return block->bb_u.l.bb_rightsib != cpu_to_be64(NULLFSBLOCK);
4909 return block->bb_u.s.bb_rightsib != cpu_to_be32(NULLAGBLOCK);