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"
23 #include "xfs_btree_staging.h"
26 * Cursor allocation zone.
28 kmem_zone_t *xfs_btree_cur_zone;
31 * Btree magic numbers.
33 static const uint32_t xfs_magics[2][XFS_BTNUM_MAX] = {
34 { XFS_ABTB_MAGIC, XFS_ABTC_MAGIC, 0, XFS_BMAP_MAGIC, XFS_IBT_MAGIC,
36 { XFS_ABTB_CRC_MAGIC, XFS_ABTC_CRC_MAGIC, XFS_RMAP_CRC_MAGIC,
37 XFS_BMAP_CRC_MAGIC, XFS_IBT_CRC_MAGIC, XFS_FIBT_CRC_MAGIC,
46 uint32_t magic = xfs_magics[crc][btnum];
48 /* Ensure we asked for crc for crc-only magics. */
54 * Check a long btree block header. Return the address of the failing check,
55 * or NULL if everything is ok.
58 __xfs_btree_check_lblock(
59 struct xfs_btree_cur *cur,
60 struct xfs_btree_block *block,
64 struct xfs_mount *mp = cur->bc_mp;
65 xfs_btnum_t btnum = cur->bc_btnum;
66 int crc = xfs_sb_version_hascrc(&mp->m_sb);
69 if (!uuid_equal(&block->bb_u.l.bb_uuid, &mp->m_sb.sb_meta_uuid))
70 return __this_address;
71 if (block->bb_u.l.bb_blkno !=
72 cpu_to_be64(bp ? bp->b_bn : XFS_BUF_DADDR_NULL))
73 return __this_address;
74 if (block->bb_u.l.bb_pad != cpu_to_be32(0))
75 return __this_address;
78 if (be32_to_cpu(block->bb_magic) != xfs_btree_magic(crc, btnum))
79 return __this_address;
80 if (be16_to_cpu(block->bb_level) != level)
81 return __this_address;
82 if (be16_to_cpu(block->bb_numrecs) >
83 cur->bc_ops->get_maxrecs(cur, level))
84 return __this_address;
85 if (block->bb_u.l.bb_leftsib != cpu_to_be64(NULLFSBLOCK) &&
86 !xfs_btree_check_lptr(cur, be64_to_cpu(block->bb_u.l.bb_leftsib),
88 return __this_address;
89 if (block->bb_u.l.bb_rightsib != cpu_to_be64(NULLFSBLOCK) &&
90 !xfs_btree_check_lptr(cur, be64_to_cpu(block->bb_u.l.bb_rightsib),
92 return __this_address;
97 /* Check a long btree block header. */
99 xfs_btree_check_lblock(
100 struct xfs_btree_cur *cur,
101 struct xfs_btree_block *block,
105 struct xfs_mount *mp = cur->bc_mp;
108 fa = __xfs_btree_check_lblock(cur, block, level, bp);
109 if (XFS_IS_CORRUPT(mp, fa != NULL) ||
110 XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BTREE_CHECK_LBLOCK)) {
112 trace_xfs_btree_corrupt(bp, _RET_IP_);
113 return -EFSCORRUPTED;
119 * Check a short btree block header. Return the address of the failing check,
120 * or NULL if everything is ok.
123 __xfs_btree_check_sblock(
124 struct xfs_btree_cur *cur,
125 struct xfs_btree_block *block,
129 struct xfs_mount *mp = cur->bc_mp;
130 xfs_btnum_t btnum = cur->bc_btnum;
131 int crc = xfs_sb_version_hascrc(&mp->m_sb);
134 if (!uuid_equal(&block->bb_u.s.bb_uuid, &mp->m_sb.sb_meta_uuid))
135 return __this_address;
136 if (block->bb_u.s.bb_blkno !=
137 cpu_to_be64(bp ? bp->b_bn : XFS_BUF_DADDR_NULL))
138 return __this_address;
141 if (be32_to_cpu(block->bb_magic) != xfs_btree_magic(crc, btnum))
142 return __this_address;
143 if (be16_to_cpu(block->bb_level) != level)
144 return __this_address;
145 if (be16_to_cpu(block->bb_numrecs) >
146 cur->bc_ops->get_maxrecs(cur, level))
147 return __this_address;
148 if (block->bb_u.s.bb_leftsib != cpu_to_be32(NULLAGBLOCK) &&
149 !xfs_btree_check_sptr(cur, be32_to_cpu(block->bb_u.s.bb_leftsib),
151 return __this_address;
152 if (block->bb_u.s.bb_rightsib != cpu_to_be32(NULLAGBLOCK) &&
153 !xfs_btree_check_sptr(cur, be32_to_cpu(block->bb_u.s.bb_rightsib),
155 return __this_address;
160 /* Check a short btree block header. */
162 xfs_btree_check_sblock(
163 struct xfs_btree_cur *cur,
164 struct xfs_btree_block *block,
168 struct xfs_mount *mp = cur->bc_mp;
171 fa = __xfs_btree_check_sblock(cur, block, level, bp);
172 if (XFS_IS_CORRUPT(mp, fa != NULL) ||
173 XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BTREE_CHECK_SBLOCK)) {
175 trace_xfs_btree_corrupt(bp, _RET_IP_);
176 return -EFSCORRUPTED;
182 * Debug routine: check that block header is ok.
185 xfs_btree_check_block(
186 struct xfs_btree_cur *cur, /* btree cursor */
187 struct xfs_btree_block *block, /* generic btree block pointer */
188 int level, /* level of the btree block */
189 struct xfs_buf *bp) /* buffer containing block, if any */
191 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
192 return xfs_btree_check_lblock(cur, block, level, bp);
194 return xfs_btree_check_sblock(cur, block, level, bp);
197 /* Check that this long pointer is valid and points within the fs. */
199 xfs_btree_check_lptr(
200 struct xfs_btree_cur *cur,
206 return xfs_verify_fsbno(cur->bc_mp, fsbno);
209 /* Check that this short pointer is valid and points within the AG. */
211 xfs_btree_check_sptr(
212 struct xfs_btree_cur *cur,
218 return xfs_verify_agbno(cur->bc_mp, cur->bc_ag.agno, agbno);
222 * Check that a given (indexed) btree pointer at a certain level of a
223 * btree is valid and doesn't point past where it should.
227 struct xfs_btree_cur *cur,
228 union xfs_btree_ptr *ptr,
232 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
233 if (xfs_btree_check_lptr(cur, be64_to_cpu((&ptr->l)[index]),
237 "Inode %llu fork %d: Corrupt btree %d pointer at level %d index %d.",
238 cur->bc_ino.ip->i_ino,
239 cur->bc_ino.whichfork, cur->bc_btnum,
242 if (xfs_btree_check_sptr(cur, be32_to_cpu((&ptr->s)[index]),
246 "AG %u: Corrupt btree %d pointer at level %d index %d.",
247 cur->bc_ag.agno, cur->bc_btnum,
251 return -EFSCORRUPTED;
255 # define xfs_btree_debug_check_ptr xfs_btree_check_ptr
257 # define xfs_btree_debug_check_ptr(...) (0)
261 * Calculate CRC on the whole btree block and stuff it into the
262 * long-form btree header.
264 * Prior to calculting the CRC, pull the LSN out of the buffer log item and put
265 * it into the buffer so recovery knows what the last modification was that made
269 xfs_btree_lblock_calc_crc(
272 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
273 struct xfs_buf_log_item *bip = bp->b_log_item;
275 if (!xfs_sb_version_hascrc(&bp->b_mount->m_sb))
278 block->bb_u.l.bb_lsn = cpu_to_be64(bip->bli_item.li_lsn);
279 xfs_buf_update_cksum(bp, XFS_BTREE_LBLOCK_CRC_OFF);
283 xfs_btree_lblock_verify_crc(
286 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
287 struct xfs_mount *mp = bp->b_mount;
289 if (xfs_sb_version_hascrc(&mp->m_sb)) {
290 if (!xfs_log_check_lsn(mp, be64_to_cpu(block->bb_u.l.bb_lsn)))
292 return xfs_buf_verify_cksum(bp, XFS_BTREE_LBLOCK_CRC_OFF);
299 * Calculate CRC on the whole btree block and stuff it into the
300 * short-form btree header.
302 * Prior to calculting the CRC, pull the LSN out of the buffer log item and put
303 * it into the buffer so recovery knows what the last modification was that made
307 xfs_btree_sblock_calc_crc(
310 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
311 struct xfs_buf_log_item *bip = bp->b_log_item;
313 if (!xfs_sb_version_hascrc(&bp->b_mount->m_sb))
316 block->bb_u.s.bb_lsn = cpu_to_be64(bip->bli_item.li_lsn);
317 xfs_buf_update_cksum(bp, XFS_BTREE_SBLOCK_CRC_OFF);
321 xfs_btree_sblock_verify_crc(
324 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
325 struct xfs_mount *mp = bp->b_mount;
327 if (xfs_sb_version_hascrc(&mp->m_sb)) {
328 if (!xfs_log_check_lsn(mp, be64_to_cpu(block->bb_u.s.bb_lsn)))
330 return xfs_buf_verify_cksum(bp, XFS_BTREE_SBLOCK_CRC_OFF);
337 xfs_btree_free_block(
338 struct xfs_btree_cur *cur,
343 error = cur->bc_ops->free_block(cur, bp);
345 xfs_trans_binval(cur->bc_tp, bp);
346 XFS_BTREE_STATS_INC(cur, free);
352 * Delete the btree cursor.
355 xfs_btree_del_cursor(
356 struct xfs_btree_cur *cur, /* btree cursor */
357 int error) /* del because of error */
359 int i; /* btree level */
362 * Clear the buffer pointers and release the buffers. If we're doing
363 * this because of an error, inspect all of the entries in the bc_bufs
364 * array for buffers to be unlocked. This is because some of the btree
365 * code works from level n down to 0, and if we get an error along the
366 * way we won't have initialized all the entries down to 0.
368 for (i = 0; i < cur->bc_nlevels; i++) {
370 xfs_trans_brelse(cur->bc_tp, cur->bc_bufs[i]);
375 ASSERT(cur->bc_btnum != XFS_BTNUM_BMAP || cur->bc_ino.allocated == 0 ||
376 XFS_FORCED_SHUTDOWN(cur->bc_mp));
377 if (unlikely(cur->bc_flags & XFS_BTREE_STAGING))
378 kmem_free(cur->bc_ops);
379 kmem_cache_free(xfs_btree_cur_zone, cur);
383 * Duplicate the btree cursor.
384 * Allocate a new one, copy the record, re-get the buffers.
387 xfs_btree_dup_cursor(
388 xfs_btree_cur_t *cur, /* input cursor */
389 xfs_btree_cur_t **ncur) /* output cursor */
391 struct xfs_buf *bp; /* btree block's buffer pointer */
392 int error; /* error return value */
393 int i; /* level number of btree block */
394 xfs_mount_t *mp; /* mount structure for filesystem */
395 xfs_btree_cur_t *new; /* new cursor value */
396 xfs_trans_t *tp; /* transaction pointer, can be NULL */
402 * Allocate a new cursor like the old one.
404 new = cur->bc_ops->dup_cursor(cur);
407 * Copy the record currently in the cursor.
409 new->bc_rec = cur->bc_rec;
412 * For each level current, re-get the buffer and copy the ptr value.
414 for (i = 0; i < new->bc_nlevels; i++) {
415 new->bc_ptrs[i] = cur->bc_ptrs[i];
416 new->bc_ra[i] = cur->bc_ra[i];
417 bp = cur->bc_bufs[i];
419 error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp,
420 XFS_BUF_ADDR(bp), mp->m_bsize,
422 cur->bc_ops->buf_ops);
424 xfs_btree_del_cursor(new, error);
429 new->bc_bufs[i] = bp;
436 * XFS btree block layout and addressing:
438 * There are two types of blocks in the btree: leaf and non-leaf blocks.
440 * The leaf record start with a header then followed by records containing
441 * the values. A non-leaf block also starts with the same header, and
442 * then first contains lookup keys followed by an equal number of pointers
443 * to the btree blocks at the previous level.
445 * +--------+-------+-------+-------+-------+-------+-------+
446 * Leaf: | header | rec 1 | rec 2 | rec 3 | rec 4 | rec 5 | rec N |
447 * +--------+-------+-------+-------+-------+-------+-------+
449 * +--------+-------+-------+-------+-------+-------+-------+
450 * Non-Leaf: | header | key 1 | key 2 | key N | ptr 1 | ptr 2 | ptr N |
451 * +--------+-------+-------+-------+-------+-------+-------+
453 * The header is called struct xfs_btree_block for reasons better left unknown
454 * and comes in different versions for short (32bit) and long (64bit) block
455 * pointers. The record and key structures are defined by the btree instances
456 * and opaque to the btree core. The block pointers are simple disk endian
457 * integers, available in a short (32bit) and long (64bit) variant.
459 * The helpers below calculate the offset of a given record, key or pointer
460 * into a btree block (xfs_btree_*_offset) or return a pointer to the given
461 * record, key or pointer (xfs_btree_*_addr). Note that all addressing
462 * inside the btree block is done using indices starting at one, not zero!
464 * If XFS_BTREE_OVERLAPPING is set, then this btree supports keys containing
465 * overlapping intervals. In such a tree, records are still sorted lowest to
466 * highest and indexed by the smallest key value that refers to the record.
467 * However, nodes are different: each pointer has two associated keys -- one
468 * indexing the lowest key available in the block(s) below (the same behavior
469 * as the key in a regular btree) and another indexing the highest key
470 * available in the block(s) below. Because records are /not/ sorted by the
471 * highest key, all leaf block updates require us to compute the highest key
472 * that matches any record in the leaf and to recursively update the high keys
473 * in the nodes going further up in the tree, if necessary. Nodes look like
476 * +--------+-----+-----+-----+-----+-----+-------+-------+-----+
477 * Non-Leaf: | header | lo1 | hi1 | lo2 | hi2 | ... | ptr 1 | ptr 2 | ... |
478 * +--------+-----+-----+-----+-----+-----+-------+-------+-----+
480 * To perform an interval query on an overlapped tree, perform the usual
481 * depth-first search and use the low and high keys to decide if we can skip
482 * that particular node. If a leaf node is reached, return the records that
483 * intersect the interval. Note that an interval query may return numerous
484 * entries. For a non-overlapped tree, simply search for the record associated
485 * with the lowest key and iterate forward until a non-matching record is
486 * found. Section 14.3 ("Interval Trees") of _Introduction to Algorithms_ by
487 * Cormen, Leiserson, Rivest, and Stein (2nd or 3rd ed. only) discuss this in
490 * Why do we care about overlapping intervals? Let's say you have a bunch of
491 * reverse mapping records on a reflink filesystem:
493 * 1: +- file A startblock B offset C length D -----------+
494 * 2: +- file E startblock F offset G length H --------------+
495 * 3: +- file I startblock F offset J length K --+
496 * 4: +- file L... --+
498 * Now say we want to map block (B+D) into file A at offset (C+D). Ideally,
499 * we'd simply increment the length of record 1. But how do we find the record
500 * that ends at (B+D-1) (i.e. record 1)? A LE lookup of (B+D-1) would return
501 * record 3 because the keys are ordered first by startblock. An interval
502 * query would return records 1 and 2 because they both overlap (B+D-1), and
503 * from that we can pick out record 1 as the appropriate left neighbor.
505 * In the non-overlapped case you can do a LE lookup and decrement the cursor
506 * because a record's interval must end before the next record.
510 * Return size of the btree block header for this btree instance.
512 static inline size_t xfs_btree_block_len(struct xfs_btree_cur *cur)
514 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
515 if (cur->bc_flags & XFS_BTREE_CRC_BLOCKS)
516 return XFS_BTREE_LBLOCK_CRC_LEN;
517 return XFS_BTREE_LBLOCK_LEN;
519 if (cur->bc_flags & XFS_BTREE_CRC_BLOCKS)
520 return XFS_BTREE_SBLOCK_CRC_LEN;
521 return XFS_BTREE_SBLOCK_LEN;
525 * Return size of btree block pointers for this btree instance.
527 static inline size_t xfs_btree_ptr_len(struct xfs_btree_cur *cur)
529 return (cur->bc_flags & XFS_BTREE_LONG_PTRS) ?
530 sizeof(__be64) : sizeof(__be32);
534 * Calculate offset of the n-th record in a btree block.
537 xfs_btree_rec_offset(
538 struct xfs_btree_cur *cur,
541 return xfs_btree_block_len(cur) +
542 (n - 1) * cur->bc_ops->rec_len;
546 * Calculate offset of the n-th key in a btree block.
549 xfs_btree_key_offset(
550 struct xfs_btree_cur *cur,
553 return xfs_btree_block_len(cur) +
554 (n - 1) * cur->bc_ops->key_len;
558 * Calculate offset of the n-th high key in a btree block.
561 xfs_btree_high_key_offset(
562 struct xfs_btree_cur *cur,
565 return xfs_btree_block_len(cur) +
566 (n - 1) * cur->bc_ops->key_len + (cur->bc_ops->key_len / 2);
570 * Calculate offset of the n-th block pointer in a btree block.
573 xfs_btree_ptr_offset(
574 struct xfs_btree_cur *cur,
578 return xfs_btree_block_len(cur) +
579 cur->bc_ops->get_maxrecs(cur, level) * cur->bc_ops->key_len +
580 (n - 1) * xfs_btree_ptr_len(cur);
584 * Return a pointer to the n-th record in the btree block.
586 union xfs_btree_rec *
588 struct xfs_btree_cur *cur,
590 struct xfs_btree_block *block)
592 return (union xfs_btree_rec *)
593 ((char *)block + xfs_btree_rec_offset(cur, n));
597 * Return a pointer to the n-th key in the btree block.
599 union xfs_btree_key *
601 struct xfs_btree_cur *cur,
603 struct xfs_btree_block *block)
605 return (union xfs_btree_key *)
606 ((char *)block + xfs_btree_key_offset(cur, n));
610 * Return a pointer to the n-th high key in the btree block.
612 union xfs_btree_key *
613 xfs_btree_high_key_addr(
614 struct xfs_btree_cur *cur,
616 struct xfs_btree_block *block)
618 return (union xfs_btree_key *)
619 ((char *)block + xfs_btree_high_key_offset(cur, n));
623 * Return a pointer to the n-th block pointer in the btree block.
625 union xfs_btree_ptr *
627 struct xfs_btree_cur *cur,
629 struct xfs_btree_block *block)
631 int level = xfs_btree_get_level(block);
633 ASSERT(block->bb_level != 0);
635 return (union xfs_btree_ptr *)
636 ((char *)block + xfs_btree_ptr_offset(cur, n, level));
641 struct xfs_btree_cur *cur)
643 ASSERT(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE);
645 if (cur->bc_flags & XFS_BTREE_STAGING)
646 return cur->bc_ino.ifake->if_fork;
647 return XFS_IFORK_PTR(cur->bc_ino.ip, cur->bc_ino.whichfork);
651 * Get the root block which is stored in the inode.
653 * For now this btree implementation assumes the btree root is always
654 * stored in the if_broot field of an inode fork.
656 STATIC struct xfs_btree_block *
658 struct xfs_btree_cur *cur)
660 struct xfs_ifork *ifp = xfs_btree_ifork_ptr(cur);
662 return (struct xfs_btree_block *)ifp->if_broot;
666 * Retrieve the block pointer from the cursor at the given level.
667 * This may be an inode btree root or from a buffer.
669 struct xfs_btree_block * /* generic btree block pointer */
671 struct xfs_btree_cur *cur, /* btree cursor */
672 int level, /* level in btree */
673 struct xfs_buf **bpp) /* buffer containing the block */
675 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
676 (level == cur->bc_nlevels - 1)) {
678 return xfs_btree_get_iroot(cur);
681 *bpp = cur->bc_bufs[level];
682 return XFS_BUF_TO_BLOCK(*bpp);
686 * Change the cursor to point to the first record at the given level.
687 * Other levels are unaffected.
689 STATIC int /* success=1, failure=0 */
691 xfs_btree_cur_t *cur, /* btree cursor */
692 int level) /* level to change */
694 struct xfs_btree_block *block; /* generic btree block pointer */
695 struct xfs_buf *bp; /* buffer containing block */
698 * Get the block pointer for this level.
700 block = xfs_btree_get_block(cur, level, &bp);
701 if (xfs_btree_check_block(cur, block, level, bp))
704 * It's empty, there is no such record.
706 if (!block->bb_numrecs)
709 * Set the ptr value to 1, that's the first record/key.
711 cur->bc_ptrs[level] = 1;
716 * Change the cursor to point to the last record in the current block
717 * at the given level. Other levels are unaffected.
719 STATIC int /* success=1, failure=0 */
721 xfs_btree_cur_t *cur, /* btree cursor */
722 int level) /* level to change */
724 struct xfs_btree_block *block; /* generic btree block pointer */
725 struct xfs_buf *bp; /* buffer containing block */
728 * Get the block pointer for this level.
730 block = xfs_btree_get_block(cur, level, &bp);
731 if (xfs_btree_check_block(cur, block, level, bp))
734 * It's empty, there is no such record.
736 if (!block->bb_numrecs)
739 * Set the ptr value to numrecs, that's the last record/key.
741 cur->bc_ptrs[level] = be16_to_cpu(block->bb_numrecs);
746 * Compute first and last byte offsets for the fields given.
747 * Interprets the offsets table, which contains struct field offsets.
751 int64_t fields, /* bitmask of fields */
752 const short *offsets, /* table of field offsets */
753 int nbits, /* number of bits to inspect */
754 int *first, /* output: first byte offset */
755 int *last) /* output: last byte offset */
757 int i; /* current bit number */
758 int64_t imask; /* mask for current bit number */
762 * Find the lowest bit, so the first byte offset.
764 for (i = 0, imask = 1LL; ; i++, imask <<= 1) {
765 if (imask & fields) {
771 * Find the highest bit, so the last byte offset.
773 for (i = nbits - 1, imask = 1LL << i; ; i--, imask >>= 1) {
774 if (imask & fields) {
775 *last = offsets[i + 1] - 1;
782 * Get a buffer for the block, return it read in.
783 * Long-form addressing.
787 struct xfs_mount *mp, /* file system mount point */
788 struct xfs_trans *tp, /* transaction pointer */
789 xfs_fsblock_t fsbno, /* file system block number */
790 struct xfs_buf **bpp, /* buffer for fsbno */
791 int refval, /* ref count value for buffer */
792 const struct xfs_buf_ops *ops)
794 struct xfs_buf *bp; /* return value */
795 xfs_daddr_t d; /* real disk block address */
798 if (!xfs_verify_fsbno(mp, fsbno))
799 return -EFSCORRUPTED;
800 d = XFS_FSB_TO_DADDR(mp, fsbno);
801 error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, d,
802 mp->m_bsize, 0, &bp, ops);
806 xfs_buf_set_ref(bp, refval);
812 * Read-ahead the block, don't wait for it, don't return a buffer.
813 * Long-form addressing.
817 xfs_btree_reada_bufl(
818 struct xfs_mount *mp, /* file system mount point */
819 xfs_fsblock_t fsbno, /* file system block number */
820 xfs_extlen_t count, /* count of filesystem blocks */
821 const struct xfs_buf_ops *ops)
825 ASSERT(fsbno != NULLFSBLOCK);
826 d = XFS_FSB_TO_DADDR(mp, fsbno);
827 xfs_buf_readahead(mp->m_ddev_targp, d, mp->m_bsize * count, ops);
831 * Read-ahead the block, don't wait for it, don't return a buffer.
832 * Short-form addressing.
836 xfs_btree_reada_bufs(
837 struct xfs_mount *mp, /* file system mount point */
838 xfs_agnumber_t agno, /* allocation group number */
839 xfs_agblock_t agbno, /* allocation group block number */
840 xfs_extlen_t count, /* count of filesystem blocks */
841 const struct xfs_buf_ops *ops)
845 ASSERT(agno != NULLAGNUMBER);
846 ASSERT(agbno != NULLAGBLOCK);
847 d = XFS_AGB_TO_DADDR(mp, agno, agbno);
848 xfs_buf_readahead(mp->m_ddev_targp, d, mp->m_bsize * count, ops);
852 xfs_btree_readahead_lblock(
853 struct xfs_btree_cur *cur,
855 struct xfs_btree_block *block)
858 xfs_fsblock_t left = be64_to_cpu(block->bb_u.l.bb_leftsib);
859 xfs_fsblock_t right = be64_to_cpu(block->bb_u.l.bb_rightsib);
861 if ((lr & XFS_BTCUR_LEFTRA) && left != NULLFSBLOCK) {
862 xfs_btree_reada_bufl(cur->bc_mp, left, 1,
863 cur->bc_ops->buf_ops);
867 if ((lr & XFS_BTCUR_RIGHTRA) && right != NULLFSBLOCK) {
868 xfs_btree_reada_bufl(cur->bc_mp, right, 1,
869 cur->bc_ops->buf_ops);
877 xfs_btree_readahead_sblock(
878 struct xfs_btree_cur *cur,
880 struct xfs_btree_block *block)
883 xfs_agblock_t left = be32_to_cpu(block->bb_u.s.bb_leftsib);
884 xfs_agblock_t right = be32_to_cpu(block->bb_u.s.bb_rightsib);
887 if ((lr & XFS_BTCUR_LEFTRA) && left != NULLAGBLOCK) {
888 xfs_btree_reada_bufs(cur->bc_mp, cur->bc_ag.agno,
889 left, 1, cur->bc_ops->buf_ops);
893 if ((lr & XFS_BTCUR_RIGHTRA) && right != NULLAGBLOCK) {
894 xfs_btree_reada_bufs(cur->bc_mp, cur->bc_ag.agno,
895 right, 1, cur->bc_ops->buf_ops);
903 * Read-ahead btree blocks, at the given level.
904 * Bits in lr are set from XFS_BTCUR_{LEFT,RIGHT}RA.
908 struct xfs_btree_cur *cur, /* btree cursor */
909 int lev, /* level in btree */
910 int lr) /* left/right bits */
912 struct xfs_btree_block *block;
915 * No readahead needed if we are at the root level and the
916 * btree root is stored in the inode.
918 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
919 (lev == cur->bc_nlevels - 1))
922 if ((cur->bc_ra[lev] | lr) == cur->bc_ra[lev])
925 cur->bc_ra[lev] |= lr;
926 block = XFS_BUF_TO_BLOCK(cur->bc_bufs[lev]);
928 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
929 return xfs_btree_readahead_lblock(cur, lr, block);
930 return xfs_btree_readahead_sblock(cur, lr, block);
934 xfs_btree_ptr_to_daddr(
935 struct xfs_btree_cur *cur,
936 union xfs_btree_ptr *ptr,
943 error = xfs_btree_check_ptr(cur, ptr, 0, 1);
947 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
948 fsbno = be64_to_cpu(ptr->l);
949 *daddr = XFS_FSB_TO_DADDR(cur->bc_mp, fsbno);
951 agbno = be32_to_cpu(ptr->s);
952 *daddr = XFS_AGB_TO_DADDR(cur->bc_mp, cur->bc_ag.agno,
960 * Readahead @count btree blocks at the given @ptr location.
962 * We don't need to care about long or short form btrees here as we have a
963 * method of converting the ptr directly to a daddr available to us.
966 xfs_btree_readahead_ptr(
967 struct xfs_btree_cur *cur,
968 union xfs_btree_ptr *ptr,
973 if (xfs_btree_ptr_to_daddr(cur, ptr, &daddr))
975 xfs_buf_readahead(cur->bc_mp->m_ddev_targp, daddr,
976 cur->bc_mp->m_bsize * count, cur->bc_ops->buf_ops);
980 * Set the buffer for level "lev" in the cursor to bp, releasing
981 * any previous buffer.
985 xfs_btree_cur_t *cur, /* btree cursor */
986 int lev, /* level in btree */
987 struct xfs_buf *bp) /* new buffer to set */
989 struct xfs_btree_block *b; /* btree block */
991 if (cur->bc_bufs[lev])
992 xfs_trans_brelse(cur->bc_tp, cur->bc_bufs[lev]);
993 cur->bc_bufs[lev] = bp;
996 b = XFS_BUF_TO_BLOCK(bp);
997 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
998 if (b->bb_u.l.bb_leftsib == cpu_to_be64(NULLFSBLOCK))
999 cur->bc_ra[lev] |= XFS_BTCUR_LEFTRA;
1000 if (b->bb_u.l.bb_rightsib == cpu_to_be64(NULLFSBLOCK))
1001 cur->bc_ra[lev] |= XFS_BTCUR_RIGHTRA;
1003 if (b->bb_u.s.bb_leftsib == cpu_to_be32(NULLAGBLOCK))
1004 cur->bc_ra[lev] |= XFS_BTCUR_LEFTRA;
1005 if (b->bb_u.s.bb_rightsib == cpu_to_be32(NULLAGBLOCK))
1006 cur->bc_ra[lev] |= XFS_BTCUR_RIGHTRA;
1011 xfs_btree_ptr_is_null(
1012 struct xfs_btree_cur *cur,
1013 union xfs_btree_ptr *ptr)
1015 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
1016 return ptr->l == cpu_to_be64(NULLFSBLOCK);
1018 return ptr->s == cpu_to_be32(NULLAGBLOCK);
1022 xfs_btree_set_ptr_null(
1023 struct xfs_btree_cur *cur,
1024 union xfs_btree_ptr *ptr)
1026 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
1027 ptr->l = cpu_to_be64(NULLFSBLOCK);
1029 ptr->s = cpu_to_be32(NULLAGBLOCK);
1033 * Get/set/init sibling pointers
1036 xfs_btree_get_sibling(
1037 struct xfs_btree_cur *cur,
1038 struct xfs_btree_block *block,
1039 union xfs_btree_ptr *ptr,
1042 ASSERT(lr == XFS_BB_LEFTSIB || lr == XFS_BB_RIGHTSIB);
1044 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
1045 if (lr == XFS_BB_RIGHTSIB)
1046 ptr->l = block->bb_u.l.bb_rightsib;
1048 ptr->l = block->bb_u.l.bb_leftsib;
1050 if (lr == XFS_BB_RIGHTSIB)
1051 ptr->s = block->bb_u.s.bb_rightsib;
1053 ptr->s = block->bb_u.s.bb_leftsib;
1058 xfs_btree_set_sibling(
1059 struct xfs_btree_cur *cur,
1060 struct xfs_btree_block *block,
1061 union xfs_btree_ptr *ptr,
1064 ASSERT(lr == XFS_BB_LEFTSIB || lr == XFS_BB_RIGHTSIB);
1066 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
1067 if (lr == XFS_BB_RIGHTSIB)
1068 block->bb_u.l.bb_rightsib = ptr->l;
1070 block->bb_u.l.bb_leftsib = ptr->l;
1072 if (lr == XFS_BB_RIGHTSIB)
1073 block->bb_u.s.bb_rightsib = ptr->s;
1075 block->bb_u.s.bb_leftsib = ptr->s;
1080 xfs_btree_init_block_int(
1081 struct xfs_mount *mp,
1082 struct xfs_btree_block *buf,
1090 int crc = xfs_sb_version_hascrc(&mp->m_sb);
1091 __u32 magic = xfs_btree_magic(crc, btnum);
1093 buf->bb_magic = cpu_to_be32(magic);
1094 buf->bb_level = cpu_to_be16(level);
1095 buf->bb_numrecs = cpu_to_be16(numrecs);
1097 if (flags & XFS_BTREE_LONG_PTRS) {
1098 buf->bb_u.l.bb_leftsib = cpu_to_be64(NULLFSBLOCK);
1099 buf->bb_u.l.bb_rightsib = cpu_to_be64(NULLFSBLOCK);
1101 buf->bb_u.l.bb_blkno = cpu_to_be64(blkno);
1102 buf->bb_u.l.bb_owner = cpu_to_be64(owner);
1103 uuid_copy(&buf->bb_u.l.bb_uuid, &mp->m_sb.sb_meta_uuid);
1104 buf->bb_u.l.bb_pad = 0;
1105 buf->bb_u.l.bb_lsn = 0;
1108 /* owner is a 32 bit value on short blocks */
1109 __u32 __owner = (__u32)owner;
1111 buf->bb_u.s.bb_leftsib = cpu_to_be32(NULLAGBLOCK);
1112 buf->bb_u.s.bb_rightsib = cpu_to_be32(NULLAGBLOCK);
1114 buf->bb_u.s.bb_blkno = cpu_to_be64(blkno);
1115 buf->bb_u.s.bb_owner = cpu_to_be32(__owner);
1116 uuid_copy(&buf->bb_u.s.bb_uuid, &mp->m_sb.sb_meta_uuid);
1117 buf->bb_u.s.bb_lsn = 0;
1123 xfs_btree_init_block(
1124 struct xfs_mount *mp,
1131 xfs_btree_init_block_int(mp, XFS_BUF_TO_BLOCK(bp), bp->b_bn,
1132 btnum, level, numrecs, owner, 0);
1136 xfs_btree_init_block_cur(
1137 struct xfs_btree_cur *cur,
1145 * we can pull the owner from the cursor right now as the different
1146 * owners align directly with the pointer size of the btree. This may
1147 * change in future, but is safe for current users of the generic btree
1150 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
1151 owner = cur->bc_ino.ip->i_ino;
1153 owner = cur->bc_ag.agno;
1155 xfs_btree_init_block_int(cur->bc_mp, XFS_BUF_TO_BLOCK(bp), bp->b_bn,
1156 cur->bc_btnum, level, numrecs,
1157 owner, cur->bc_flags);
1161 * Return true if ptr is the last record in the btree and
1162 * we need to track updates to this record. The decision
1163 * will be further refined in the update_lastrec method.
1166 xfs_btree_is_lastrec(
1167 struct xfs_btree_cur *cur,
1168 struct xfs_btree_block *block,
1171 union xfs_btree_ptr ptr;
1175 if (!(cur->bc_flags & XFS_BTREE_LASTREC_UPDATE))
1178 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_RIGHTSIB);
1179 if (!xfs_btree_ptr_is_null(cur, &ptr))
1185 xfs_btree_buf_to_ptr(
1186 struct xfs_btree_cur *cur,
1188 union xfs_btree_ptr *ptr)
1190 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
1191 ptr->l = cpu_to_be64(XFS_DADDR_TO_FSB(cur->bc_mp,
1194 ptr->s = cpu_to_be32(xfs_daddr_to_agbno(cur->bc_mp,
1201 struct xfs_btree_cur *cur,
1204 switch (cur->bc_btnum) {
1207 xfs_buf_set_ref(bp, XFS_ALLOC_BTREE_REF);
1210 case XFS_BTNUM_FINO:
1211 xfs_buf_set_ref(bp, XFS_INO_BTREE_REF);
1213 case XFS_BTNUM_BMAP:
1214 xfs_buf_set_ref(bp, XFS_BMAP_BTREE_REF);
1216 case XFS_BTNUM_RMAP:
1217 xfs_buf_set_ref(bp, XFS_RMAP_BTREE_REF);
1219 case XFS_BTNUM_REFC:
1220 xfs_buf_set_ref(bp, XFS_REFC_BTREE_REF);
1228 xfs_btree_get_buf_block(
1229 struct xfs_btree_cur *cur,
1230 union xfs_btree_ptr *ptr,
1231 struct xfs_btree_block **block,
1232 struct xfs_buf **bpp)
1234 struct xfs_mount *mp = cur->bc_mp;
1238 error = xfs_btree_ptr_to_daddr(cur, ptr, &d);
1241 error = xfs_trans_get_buf(cur->bc_tp, mp->m_ddev_targp, d, mp->m_bsize,
1246 (*bpp)->b_ops = cur->bc_ops->buf_ops;
1247 *block = XFS_BUF_TO_BLOCK(*bpp);
1252 * Read in the buffer at the given ptr and return the buffer and
1253 * the block pointer within the buffer.
1256 xfs_btree_read_buf_block(
1257 struct xfs_btree_cur *cur,
1258 union xfs_btree_ptr *ptr,
1260 struct xfs_btree_block **block,
1261 struct xfs_buf **bpp)
1263 struct xfs_mount *mp = cur->bc_mp;
1267 /* need to sort out how callers deal with failures first */
1268 ASSERT(!(flags & XBF_TRYLOCK));
1270 error = xfs_btree_ptr_to_daddr(cur, ptr, &d);
1273 error = xfs_trans_read_buf(mp, cur->bc_tp, mp->m_ddev_targp, d,
1274 mp->m_bsize, flags, bpp,
1275 cur->bc_ops->buf_ops);
1279 xfs_btree_set_refs(cur, *bpp);
1280 *block = XFS_BUF_TO_BLOCK(*bpp);
1285 * Copy keys from one btree block to another.
1288 xfs_btree_copy_keys(
1289 struct xfs_btree_cur *cur,
1290 union xfs_btree_key *dst_key,
1291 union xfs_btree_key *src_key,
1294 ASSERT(numkeys >= 0);
1295 memcpy(dst_key, src_key, numkeys * cur->bc_ops->key_len);
1299 * Copy records from one btree block to another.
1302 xfs_btree_copy_recs(
1303 struct xfs_btree_cur *cur,
1304 union xfs_btree_rec *dst_rec,
1305 union xfs_btree_rec *src_rec,
1308 ASSERT(numrecs >= 0);
1309 memcpy(dst_rec, src_rec, numrecs * cur->bc_ops->rec_len);
1313 * Copy block pointers from one btree block to another.
1316 xfs_btree_copy_ptrs(
1317 struct xfs_btree_cur *cur,
1318 union xfs_btree_ptr *dst_ptr,
1319 const union xfs_btree_ptr *src_ptr,
1322 ASSERT(numptrs >= 0);
1323 memcpy(dst_ptr, src_ptr, numptrs * xfs_btree_ptr_len(cur));
1327 * Shift keys one index left/right inside a single btree block.
1330 xfs_btree_shift_keys(
1331 struct xfs_btree_cur *cur,
1332 union xfs_btree_key *key,
1338 ASSERT(numkeys >= 0);
1339 ASSERT(dir == 1 || dir == -1);
1341 dst_key = (char *)key + (dir * cur->bc_ops->key_len);
1342 memmove(dst_key, key, numkeys * cur->bc_ops->key_len);
1346 * Shift records one index left/right inside a single btree block.
1349 xfs_btree_shift_recs(
1350 struct xfs_btree_cur *cur,
1351 union xfs_btree_rec *rec,
1357 ASSERT(numrecs >= 0);
1358 ASSERT(dir == 1 || dir == -1);
1360 dst_rec = (char *)rec + (dir * cur->bc_ops->rec_len);
1361 memmove(dst_rec, rec, numrecs * cur->bc_ops->rec_len);
1365 * Shift block pointers one index left/right inside a single btree block.
1368 xfs_btree_shift_ptrs(
1369 struct xfs_btree_cur *cur,
1370 union xfs_btree_ptr *ptr,
1376 ASSERT(numptrs >= 0);
1377 ASSERT(dir == 1 || dir == -1);
1379 dst_ptr = (char *)ptr + (dir * xfs_btree_ptr_len(cur));
1380 memmove(dst_ptr, ptr, numptrs * xfs_btree_ptr_len(cur));
1384 * Log key values from the btree block.
1388 struct xfs_btree_cur *cur,
1395 xfs_trans_buf_set_type(cur->bc_tp, bp, XFS_BLFT_BTREE_BUF);
1396 xfs_trans_log_buf(cur->bc_tp, bp,
1397 xfs_btree_key_offset(cur, first),
1398 xfs_btree_key_offset(cur, last + 1) - 1);
1400 xfs_trans_log_inode(cur->bc_tp, cur->bc_ino.ip,
1401 xfs_ilog_fbroot(cur->bc_ino.whichfork));
1406 * Log record values from the btree block.
1410 struct xfs_btree_cur *cur,
1416 xfs_trans_buf_set_type(cur->bc_tp, bp, XFS_BLFT_BTREE_BUF);
1417 xfs_trans_log_buf(cur->bc_tp, bp,
1418 xfs_btree_rec_offset(cur, first),
1419 xfs_btree_rec_offset(cur, last + 1) - 1);
1424 * Log block pointer fields from a btree block (nonleaf).
1428 struct xfs_btree_cur *cur, /* btree cursor */
1429 struct xfs_buf *bp, /* buffer containing btree block */
1430 int first, /* index of first pointer to log */
1431 int last) /* index of last pointer to log */
1435 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
1436 int level = xfs_btree_get_level(block);
1438 xfs_trans_buf_set_type(cur->bc_tp, bp, XFS_BLFT_BTREE_BUF);
1439 xfs_trans_log_buf(cur->bc_tp, bp,
1440 xfs_btree_ptr_offset(cur, first, level),
1441 xfs_btree_ptr_offset(cur, last + 1, level) - 1);
1443 xfs_trans_log_inode(cur->bc_tp, cur->bc_ino.ip,
1444 xfs_ilog_fbroot(cur->bc_ino.whichfork));
1450 * Log fields from a btree block header.
1453 xfs_btree_log_block(
1454 struct xfs_btree_cur *cur, /* btree cursor */
1455 struct xfs_buf *bp, /* buffer containing btree block */
1456 int fields) /* mask of fields: XFS_BB_... */
1458 int first; /* first byte offset logged */
1459 int last; /* last byte offset logged */
1460 static const short soffsets[] = { /* table of offsets (short) */
1461 offsetof(struct xfs_btree_block, bb_magic),
1462 offsetof(struct xfs_btree_block, bb_level),
1463 offsetof(struct xfs_btree_block, bb_numrecs),
1464 offsetof(struct xfs_btree_block, bb_u.s.bb_leftsib),
1465 offsetof(struct xfs_btree_block, bb_u.s.bb_rightsib),
1466 offsetof(struct xfs_btree_block, bb_u.s.bb_blkno),
1467 offsetof(struct xfs_btree_block, bb_u.s.bb_lsn),
1468 offsetof(struct xfs_btree_block, bb_u.s.bb_uuid),
1469 offsetof(struct xfs_btree_block, bb_u.s.bb_owner),
1470 offsetof(struct xfs_btree_block, bb_u.s.bb_crc),
1471 XFS_BTREE_SBLOCK_CRC_LEN
1473 static const short loffsets[] = { /* table of offsets (long) */
1474 offsetof(struct xfs_btree_block, bb_magic),
1475 offsetof(struct xfs_btree_block, bb_level),
1476 offsetof(struct xfs_btree_block, bb_numrecs),
1477 offsetof(struct xfs_btree_block, bb_u.l.bb_leftsib),
1478 offsetof(struct xfs_btree_block, bb_u.l.bb_rightsib),
1479 offsetof(struct xfs_btree_block, bb_u.l.bb_blkno),
1480 offsetof(struct xfs_btree_block, bb_u.l.bb_lsn),
1481 offsetof(struct xfs_btree_block, bb_u.l.bb_uuid),
1482 offsetof(struct xfs_btree_block, bb_u.l.bb_owner),
1483 offsetof(struct xfs_btree_block, bb_u.l.bb_crc),
1484 offsetof(struct xfs_btree_block, bb_u.l.bb_pad),
1485 XFS_BTREE_LBLOCK_CRC_LEN
1491 if (cur->bc_flags & XFS_BTREE_CRC_BLOCKS) {
1493 * We don't log the CRC when updating a btree
1494 * block but instead recreate it during log
1495 * recovery. As the log buffers have checksums
1496 * of their own this is safe and avoids logging a crc
1497 * update in a lot of places.
1499 if (fields == XFS_BB_ALL_BITS)
1500 fields = XFS_BB_ALL_BITS_CRC;
1501 nbits = XFS_BB_NUM_BITS_CRC;
1503 nbits = XFS_BB_NUM_BITS;
1505 xfs_btree_offsets(fields,
1506 (cur->bc_flags & XFS_BTREE_LONG_PTRS) ?
1507 loffsets : soffsets,
1508 nbits, &first, &last);
1509 xfs_trans_buf_set_type(cur->bc_tp, bp, XFS_BLFT_BTREE_BUF);
1510 xfs_trans_log_buf(cur->bc_tp, bp, first, last);
1512 xfs_trans_log_inode(cur->bc_tp, cur->bc_ino.ip,
1513 xfs_ilog_fbroot(cur->bc_ino.whichfork));
1518 * Increment cursor by one record at the level.
1519 * For nonzero levels the leaf-ward information is untouched.
1522 xfs_btree_increment(
1523 struct xfs_btree_cur *cur,
1525 int *stat) /* success/failure */
1527 struct xfs_btree_block *block;
1528 union xfs_btree_ptr ptr;
1530 int error; /* error return value */
1533 ASSERT(level < cur->bc_nlevels);
1535 /* Read-ahead to the right at this level. */
1536 xfs_btree_readahead(cur, level, XFS_BTCUR_RIGHTRA);
1538 /* Get a pointer to the btree block. */
1539 block = xfs_btree_get_block(cur, level, &bp);
1542 error = xfs_btree_check_block(cur, block, level, bp);
1547 /* We're done if we remain in the block after the increment. */
1548 if (++cur->bc_ptrs[level] <= xfs_btree_get_numrecs(block))
1551 /* Fail if we just went off the right edge of the tree. */
1552 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_RIGHTSIB);
1553 if (xfs_btree_ptr_is_null(cur, &ptr))
1556 XFS_BTREE_STATS_INC(cur, increment);
1559 * March up the tree incrementing pointers.
1560 * Stop when we don't go off the right edge of a block.
1562 for (lev = level + 1; lev < cur->bc_nlevels; lev++) {
1563 block = xfs_btree_get_block(cur, lev, &bp);
1566 error = xfs_btree_check_block(cur, block, lev, bp);
1571 if (++cur->bc_ptrs[lev] <= xfs_btree_get_numrecs(block))
1574 /* Read-ahead the right block for the next loop. */
1575 xfs_btree_readahead(cur, lev, XFS_BTCUR_RIGHTRA);
1579 * If we went off the root then we are either seriously
1580 * confused or have the tree root in an inode.
1582 if (lev == cur->bc_nlevels) {
1583 if (cur->bc_flags & XFS_BTREE_ROOT_IN_INODE)
1586 error = -EFSCORRUPTED;
1589 ASSERT(lev < cur->bc_nlevels);
1592 * Now walk back down the tree, fixing up the cursor's buffer
1593 * pointers and key numbers.
1595 for (block = xfs_btree_get_block(cur, lev, &bp); lev > level; ) {
1596 union xfs_btree_ptr *ptrp;
1598 ptrp = xfs_btree_ptr_addr(cur, cur->bc_ptrs[lev], block);
1600 error = xfs_btree_read_buf_block(cur, ptrp, 0, &block, &bp);
1604 xfs_btree_setbuf(cur, lev, bp);
1605 cur->bc_ptrs[lev] = 1;
1620 * Decrement cursor by one record at the level.
1621 * For nonzero levels the leaf-ward information is untouched.
1624 xfs_btree_decrement(
1625 struct xfs_btree_cur *cur,
1627 int *stat) /* success/failure */
1629 struct xfs_btree_block *block;
1631 int error; /* error return value */
1633 union xfs_btree_ptr ptr;
1635 ASSERT(level < cur->bc_nlevels);
1637 /* Read-ahead to the left at this level. */
1638 xfs_btree_readahead(cur, level, XFS_BTCUR_LEFTRA);
1640 /* We're done if we remain in the block after the decrement. */
1641 if (--cur->bc_ptrs[level] > 0)
1644 /* Get a pointer to the btree block. */
1645 block = xfs_btree_get_block(cur, level, &bp);
1648 error = xfs_btree_check_block(cur, block, level, bp);
1653 /* Fail if we just went off the left edge of the tree. */
1654 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_LEFTSIB);
1655 if (xfs_btree_ptr_is_null(cur, &ptr))
1658 XFS_BTREE_STATS_INC(cur, decrement);
1661 * March up the tree decrementing pointers.
1662 * Stop when we don't go off the left edge of a block.
1664 for (lev = level + 1; lev < cur->bc_nlevels; lev++) {
1665 if (--cur->bc_ptrs[lev] > 0)
1667 /* Read-ahead the left block for the next loop. */
1668 xfs_btree_readahead(cur, lev, XFS_BTCUR_LEFTRA);
1672 * If we went off the root then we are seriously confused.
1673 * or the root of the tree is in an inode.
1675 if (lev == cur->bc_nlevels) {
1676 if (cur->bc_flags & XFS_BTREE_ROOT_IN_INODE)
1679 error = -EFSCORRUPTED;
1682 ASSERT(lev < cur->bc_nlevels);
1685 * Now walk back down the tree, fixing up the cursor's buffer
1686 * pointers and key numbers.
1688 for (block = xfs_btree_get_block(cur, lev, &bp); lev > level; ) {
1689 union xfs_btree_ptr *ptrp;
1691 ptrp = xfs_btree_ptr_addr(cur, cur->bc_ptrs[lev], block);
1693 error = xfs_btree_read_buf_block(cur, ptrp, 0, &block, &bp);
1696 xfs_btree_setbuf(cur, lev, bp);
1697 cur->bc_ptrs[lev] = xfs_btree_get_numrecs(block);
1712 xfs_btree_lookup_get_block(
1713 struct xfs_btree_cur *cur, /* btree cursor */
1714 int level, /* level in the btree */
1715 union xfs_btree_ptr *pp, /* ptr to btree block */
1716 struct xfs_btree_block **blkp) /* return btree block */
1718 struct xfs_buf *bp; /* buffer pointer for btree block */
1722 /* special case the root block if in an inode */
1723 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
1724 (level == cur->bc_nlevels - 1)) {
1725 *blkp = xfs_btree_get_iroot(cur);
1730 * If the old buffer at this level for the disk address we are
1731 * looking for re-use it.
1733 * Otherwise throw it away and get a new one.
1735 bp = cur->bc_bufs[level];
1736 error = xfs_btree_ptr_to_daddr(cur, pp, &daddr);
1739 if (bp && XFS_BUF_ADDR(bp) == daddr) {
1740 *blkp = XFS_BUF_TO_BLOCK(bp);
1744 error = xfs_btree_read_buf_block(cur, pp, 0, blkp, &bp);
1748 /* Check the inode owner since the verifiers don't. */
1749 if (xfs_sb_version_hascrc(&cur->bc_mp->m_sb) &&
1750 !(cur->bc_ino.flags & XFS_BTCUR_BMBT_INVALID_OWNER) &&
1751 (cur->bc_flags & XFS_BTREE_LONG_PTRS) &&
1752 be64_to_cpu((*blkp)->bb_u.l.bb_owner) !=
1753 cur->bc_ino.ip->i_ino)
1756 /* Did we get the level we were looking for? */
1757 if (be16_to_cpu((*blkp)->bb_level) != level)
1760 /* Check that internal nodes have at least one record. */
1761 if (level != 0 && be16_to_cpu((*blkp)->bb_numrecs) == 0)
1764 xfs_btree_setbuf(cur, level, bp);
1769 xfs_buf_mark_corrupt(bp);
1770 xfs_trans_brelse(cur->bc_tp, bp);
1771 return -EFSCORRUPTED;
1775 * Get current search key. For level 0 we don't actually have a key
1776 * structure so we make one up from the record. For all other levels
1777 * we just return the right key.
1779 STATIC union xfs_btree_key *
1780 xfs_lookup_get_search_key(
1781 struct xfs_btree_cur *cur,
1784 struct xfs_btree_block *block,
1785 union xfs_btree_key *kp)
1788 cur->bc_ops->init_key_from_rec(kp,
1789 xfs_btree_rec_addr(cur, keyno, block));
1793 return xfs_btree_key_addr(cur, keyno, block);
1797 * Lookup the record. The cursor is made to point to it, based on dir.
1798 * stat is set to 0 if can't find any such record, 1 for success.
1802 struct xfs_btree_cur *cur, /* btree cursor */
1803 xfs_lookup_t dir, /* <=, ==, or >= */
1804 int *stat) /* success/failure */
1806 struct xfs_btree_block *block; /* current btree block */
1807 int64_t diff; /* difference for the current key */
1808 int error; /* error return value */
1809 int keyno; /* current key number */
1810 int level; /* level in the btree */
1811 union xfs_btree_ptr *pp; /* ptr to btree block */
1812 union xfs_btree_ptr ptr; /* ptr to btree block */
1814 XFS_BTREE_STATS_INC(cur, lookup);
1816 /* No such thing as a zero-level tree. */
1817 if (XFS_IS_CORRUPT(cur->bc_mp, cur->bc_nlevels == 0))
1818 return -EFSCORRUPTED;
1823 /* initialise start pointer from cursor */
1824 cur->bc_ops->init_ptr_from_cur(cur, &ptr);
1828 * Iterate over each level in the btree, starting at the root.
1829 * For each level above the leaves, find the key we need, based
1830 * on the lookup record, then follow the corresponding block
1831 * pointer down to the next level.
1833 for (level = cur->bc_nlevels - 1, diff = 1; level >= 0; level--) {
1834 /* Get the block we need to do the lookup on. */
1835 error = xfs_btree_lookup_get_block(cur, level, pp, &block);
1841 * If we already had a key match at a higher level, we
1842 * know we need to use the first entry in this block.
1846 /* Otherwise search this block. Do a binary search. */
1848 int high; /* high entry number */
1849 int low; /* low entry number */
1851 /* Set low and high entry numbers, 1-based. */
1853 high = xfs_btree_get_numrecs(block);
1855 /* Block is empty, must be an empty leaf. */
1856 if (level != 0 || cur->bc_nlevels != 1) {
1857 XFS_CORRUPTION_ERROR(__func__,
1861 return -EFSCORRUPTED;
1864 cur->bc_ptrs[0] = dir != XFS_LOOKUP_LE;
1869 /* Binary search the block. */
1870 while (low <= high) {
1871 union xfs_btree_key key;
1872 union xfs_btree_key *kp;
1874 XFS_BTREE_STATS_INC(cur, compare);
1876 /* keyno is average of low and high. */
1877 keyno = (low + high) >> 1;
1879 /* Get current search key */
1880 kp = xfs_lookup_get_search_key(cur, level,
1881 keyno, block, &key);
1884 * Compute difference to get next direction:
1885 * - less than, move right
1886 * - greater than, move left
1887 * - equal, we're done
1889 diff = cur->bc_ops->key_diff(cur, kp);
1900 * If there are more levels, set up for the next level
1901 * by getting the block number and filling in the cursor.
1905 * If we moved left, need the previous key number,
1906 * unless there isn't one.
1908 if (diff > 0 && --keyno < 1)
1910 pp = xfs_btree_ptr_addr(cur, keyno, block);
1912 error = xfs_btree_debug_check_ptr(cur, pp, 0, level);
1916 cur->bc_ptrs[level] = keyno;
1920 /* Done with the search. See if we need to adjust the results. */
1921 if (dir != XFS_LOOKUP_LE && diff < 0) {
1924 * If ge search and we went off the end of the block, but it's
1925 * not the last block, we're in the wrong block.
1927 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_RIGHTSIB);
1928 if (dir == XFS_LOOKUP_GE &&
1929 keyno > xfs_btree_get_numrecs(block) &&
1930 !xfs_btree_ptr_is_null(cur, &ptr)) {
1933 cur->bc_ptrs[0] = keyno;
1934 error = xfs_btree_increment(cur, 0, &i);
1937 if (XFS_IS_CORRUPT(cur->bc_mp, i != 1))
1938 return -EFSCORRUPTED;
1942 } else if (dir == XFS_LOOKUP_LE && diff > 0)
1944 cur->bc_ptrs[0] = keyno;
1946 /* Return if we succeeded or not. */
1947 if (keyno == 0 || keyno > xfs_btree_get_numrecs(block))
1949 else if (dir != XFS_LOOKUP_EQ || diff == 0)
1959 /* Find the high key storage area from a regular key. */
1960 union xfs_btree_key *
1961 xfs_btree_high_key_from_key(
1962 struct xfs_btree_cur *cur,
1963 union xfs_btree_key *key)
1965 ASSERT(cur->bc_flags & XFS_BTREE_OVERLAPPING);
1966 return (union xfs_btree_key *)((char *)key +
1967 (cur->bc_ops->key_len / 2));
1970 /* Determine the low (and high if overlapped) keys of a leaf block */
1972 xfs_btree_get_leaf_keys(
1973 struct xfs_btree_cur *cur,
1974 struct xfs_btree_block *block,
1975 union xfs_btree_key *key)
1977 union xfs_btree_key max_hkey;
1978 union xfs_btree_key hkey;
1979 union xfs_btree_rec *rec;
1980 union xfs_btree_key *high;
1983 rec = xfs_btree_rec_addr(cur, 1, block);
1984 cur->bc_ops->init_key_from_rec(key, rec);
1986 if (cur->bc_flags & XFS_BTREE_OVERLAPPING) {
1988 cur->bc_ops->init_high_key_from_rec(&max_hkey, rec);
1989 for (n = 2; n <= xfs_btree_get_numrecs(block); n++) {
1990 rec = xfs_btree_rec_addr(cur, n, block);
1991 cur->bc_ops->init_high_key_from_rec(&hkey, rec);
1992 if (cur->bc_ops->diff_two_keys(cur, &hkey, &max_hkey)
1997 high = xfs_btree_high_key_from_key(cur, key);
1998 memcpy(high, &max_hkey, cur->bc_ops->key_len / 2);
2002 /* Determine the low (and high if overlapped) keys of a node block */
2004 xfs_btree_get_node_keys(
2005 struct xfs_btree_cur *cur,
2006 struct xfs_btree_block *block,
2007 union xfs_btree_key *key)
2009 union xfs_btree_key *hkey;
2010 union xfs_btree_key *max_hkey;
2011 union xfs_btree_key *high;
2014 if (cur->bc_flags & XFS_BTREE_OVERLAPPING) {
2015 memcpy(key, xfs_btree_key_addr(cur, 1, block),
2016 cur->bc_ops->key_len / 2);
2018 max_hkey = xfs_btree_high_key_addr(cur, 1, block);
2019 for (n = 2; n <= xfs_btree_get_numrecs(block); n++) {
2020 hkey = xfs_btree_high_key_addr(cur, n, block);
2021 if (cur->bc_ops->diff_two_keys(cur, hkey, max_hkey) > 0)
2025 high = xfs_btree_high_key_from_key(cur, key);
2026 memcpy(high, max_hkey, cur->bc_ops->key_len / 2);
2028 memcpy(key, xfs_btree_key_addr(cur, 1, block),
2029 cur->bc_ops->key_len);
2033 /* Derive the keys for any btree block. */
2036 struct xfs_btree_cur *cur,
2037 struct xfs_btree_block *block,
2038 union xfs_btree_key *key)
2040 if (be16_to_cpu(block->bb_level) == 0)
2041 xfs_btree_get_leaf_keys(cur, block, key);
2043 xfs_btree_get_node_keys(cur, block, key);
2047 * Decide if we need to update the parent keys of a btree block. For
2048 * a standard btree this is only necessary if we're updating the first
2049 * record/key. For an overlapping btree, we must always update the
2050 * keys because the highest key can be in any of the records or keys
2054 xfs_btree_needs_key_update(
2055 struct xfs_btree_cur *cur,
2058 return (cur->bc_flags & XFS_BTREE_OVERLAPPING) || ptr == 1;
2062 * Update the low and high parent keys of the given level, progressing
2063 * towards the root. If force_all is false, stop if the keys for a given
2064 * level do not need updating.
2067 __xfs_btree_updkeys(
2068 struct xfs_btree_cur *cur,
2070 struct xfs_btree_block *block,
2071 struct xfs_buf *bp0,
2074 union xfs_btree_key key; /* keys from current level */
2075 union xfs_btree_key *lkey; /* keys from the next level up */
2076 union xfs_btree_key *hkey;
2077 union xfs_btree_key *nlkey; /* keys from the next level up */
2078 union xfs_btree_key *nhkey;
2082 ASSERT(cur->bc_flags & XFS_BTREE_OVERLAPPING);
2084 /* Exit if there aren't any parent levels to update. */
2085 if (level + 1 >= cur->bc_nlevels)
2088 trace_xfs_btree_updkeys(cur, level, bp0);
2091 hkey = xfs_btree_high_key_from_key(cur, lkey);
2092 xfs_btree_get_keys(cur, block, lkey);
2093 for (level++; level < cur->bc_nlevels; level++) {
2097 block = xfs_btree_get_block(cur, level, &bp);
2098 trace_xfs_btree_updkeys(cur, level, bp);
2100 error = xfs_btree_check_block(cur, block, level, bp);
2104 ptr = cur->bc_ptrs[level];
2105 nlkey = xfs_btree_key_addr(cur, ptr, block);
2106 nhkey = xfs_btree_high_key_addr(cur, ptr, block);
2108 !(cur->bc_ops->diff_two_keys(cur, nlkey, lkey) != 0 ||
2109 cur->bc_ops->diff_two_keys(cur, nhkey, hkey) != 0))
2111 xfs_btree_copy_keys(cur, nlkey, lkey, 1);
2112 xfs_btree_log_keys(cur, bp, ptr, ptr);
2113 if (level + 1 >= cur->bc_nlevels)
2115 xfs_btree_get_node_keys(cur, block, lkey);
2121 /* Update all the keys from some level in cursor back to the root. */
2123 xfs_btree_updkeys_force(
2124 struct xfs_btree_cur *cur,
2128 struct xfs_btree_block *block;
2130 block = xfs_btree_get_block(cur, level, &bp);
2131 return __xfs_btree_updkeys(cur, level, block, bp, true);
2135 * Update the parent keys of the given level, progressing towards the root.
2138 xfs_btree_update_keys(
2139 struct xfs_btree_cur *cur,
2142 struct xfs_btree_block *block;
2144 union xfs_btree_key *kp;
2145 union xfs_btree_key key;
2150 block = xfs_btree_get_block(cur, level, &bp);
2151 if (cur->bc_flags & XFS_BTREE_OVERLAPPING)
2152 return __xfs_btree_updkeys(cur, level, block, bp, false);
2155 * Go up the tree from this level toward the root.
2156 * At each level, update the key value to the value input.
2157 * Stop when we reach a level where the cursor isn't pointing
2158 * at the first entry in the block.
2160 xfs_btree_get_keys(cur, block, &key);
2161 for (level++, ptr = 1; ptr == 1 && level < cur->bc_nlevels; level++) {
2165 block = xfs_btree_get_block(cur, level, &bp);
2167 error = xfs_btree_check_block(cur, block, level, bp);
2171 ptr = cur->bc_ptrs[level];
2172 kp = xfs_btree_key_addr(cur, ptr, block);
2173 xfs_btree_copy_keys(cur, kp, &key, 1);
2174 xfs_btree_log_keys(cur, bp, ptr, ptr);
2181 * Update the record referred to by cur to the value in the
2182 * given record. This either works (return 0) or gets an
2183 * EFSCORRUPTED error.
2187 struct xfs_btree_cur *cur,
2188 union xfs_btree_rec *rec)
2190 struct xfs_btree_block *block;
2194 union xfs_btree_rec *rp;
2196 /* Pick up the current block. */
2197 block = xfs_btree_get_block(cur, 0, &bp);
2200 error = xfs_btree_check_block(cur, block, 0, bp);
2204 /* Get the address of the rec to be updated. */
2205 ptr = cur->bc_ptrs[0];
2206 rp = xfs_btree_rec_addr(cur, ptr, block);
2208 /* Fill in the new contents and log them. */
2209 xfs_btree_copy_recs(cur, rp, rec, 1);
2210 xfs_btree_log_recs(cur, bp, ptr, ptr);
2213 * If we are tracking the last record in the tree and
2214 * we are at the far right edge of the tree, update it.
2216 if (xfs_btree_is_lastrec(cur, block, 0)) {
2217 cur->bc_ops->update_lastrec(cur, block, rec,
2218 ptr, LASTREC_UPDATE);
2221 /* Pass new key value up to our parent. */
2222 if (xfs_btree_needs_key_update(cur, ptr)) {
2223 error = xfs_btree_update_keys(cur, 0);
2235 * Move 1 record left from cur/level if possible.
2236 * Update cur to reflect the new path.
2238 STATIC int /* error */
2240 struct xfs_btree_cur *cur,
2242 int *stat) /* success/failure */
2244 struct xfs_buf *lbp; /* left buffer pointer */
2245 struct xfs_btree_block *left; /* left btree block */
2246 int lrecs; /* left record count */
2247 struct xfs_buf *rbp; /* right buffer pointer */
2248 struct xfs_btree_block *right; /* right btree block */
2249 struct xfs_btree_cur *tcur; /* temporary btree cursor */
2250 int rrecs; /* right record count */
2251 union xfs_btree_ptr lptr; /* left btree pointer */
2252 union xfs_btree_key *rkp = NULL; /* right btree key */
2253 union xfs_btree_ptr *rpp = NULL; /* right address pointer */
2254 union xfs_btree_rec *rrp = NULL; /* right record pointer */
2255 int error; /* error return value */
2258 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
2259 level == cur->bc_nlevels - 1)
2262 /* Set up variables for this block as "right". */
2263 right = xfs_btree_get_block(cur, level, &rbp);
2266 error = xfs_btree_check_block(cur, right, level, rbp);
2271 /* If we've got no left sibling then we can't shift an entry left. */
2272 xfs_btree_get_sibling(cur, right, &lptr, XFS_BB_LEFTSIB);
2273 if (xfs_btree_ptr_is_null(cur, &lptr))
2277 * If the cursor entry is the one that would be moved, don't
2278 * do it... it's too complicated.
2280 if (cur->bc_ptrs[level] <= 1)
2283 /* Set up the left neighbor as "left". */
2284 error = xfs_btree_read_buf_block(cur, &lptr, 0, &left, &lbp);
2288 /* If it's full, it can't take another entry. */
2289 lrecs = xfs_btree_get_numrecs(left);
2290 if (lrecs == cur->bc_ops->get_maxrecs(cur, level))
2293 rrecs = xfs_btree_get_numrecs(right);
2296 * We add one entry to the left side and remove one for the right side.
2297 * Account for it here, the changes will be updated on disk and logged
2303 XFS_BTREE_STATS_INC(cur, lshift);
2304 XFS_BTREE_STATS_ADD(cur, moves, 1);
2307 * If non-leaf, copy a key and a ptr to the left block.
2308 * Log the changes to the left block.
2311 /* It's a non-leaf. Move keys and pointers. */
2312 union xfs_btree_key *lkp; /* left btree key */
2313 union xfs_btree_ptr *lpp; /* left address pointer */
2315 lkp = xfs_btree_key_addr(cur, lrecs, left);
2316 rkp = xfs_btree_key_addr(cur, 1, right);
2318 lpp = xfs_btree_ptr_addr(cur, lrecs, left);
2319 rpp = xfs_btree_ptr_addr(cur, 1, right);
2321 error = xfs_btree_debug_check_ptr(cur, rpp, 0, level);
2325 xfs_btree_copy_keys(cur, lkp, rkp, 1);
2326 xfs_btree_copy_ptrs(cur, lpp, rpp, 1);
2328 xfs_btree_log_keys(cur, lbp, lrecs, lrecs);
2329 xfs_btree_log_ptrs(cur, lbp, lrecs, lrecs);
2331 ASSERT(cur->bc_ops->keys_inorder(cur,
2332 xfs_btree_key_addr(cur, lrecs - 1, left), lkp));
2334 /* It's a leaf. Move records. */
2335 union xfs_btree_rec *lrp; /* left record pointer */
2337 lrp = xfs_btree_rec_addr(cur, lrecs, left);
2338 rrp = xfs_btree_rec_addr(cur, 1, right);
2340 xfs_btree_copy_recs(cur, lrp, rrp, 1);
2341 xfs_btree_log_recs(cur, lbp, lrecs, lrecs);
2343 ASSERT(cur->bc_ops->recs_inorder(cur,
2344 xfs_btree_rec_addr(cur, lrecs - 1, left), lrp));
2347 xfs_btree_set_numrecs(left, lrecs);
2348 xfs_btree_log_block(cur, lbp, XFS_BB_NUMRECS);
2350 xfs_btree_set_numrecs(right, rrecs);
2351 xfs_btree_log_block(cur, rbp, XFS_BB_NUMRECS);
2354 * Slide the contents of right down one entry.
2356 XFS_BTREE_STATS_ADD(cur, moves, rrecs - 1);
2358 /* It's a nonleaf. operate on keys and ptrs */
2359 for (i = 0; i < rrecs; i++) {
2360 error = xfs_btree_debug_check_ptr(cur, rpp, i + 1, level);
2365 xfs_btree_shift_keys(cur,
2366 xfs_btree_key_addr(cur, 2, right),
2368 xfs_btree_shift_ptrs(cur,
2369 xfs_btree_ptr_addr(cur, 2, right),
2372 xfs_btree_log_keys(cur, rbp, 1, rrecs);
2373 xfs_btree_log_ptrs(cur, rbp, 1, rrecs);
2375 /* It's a leaf. operate on records */
2376 xfs_btree_shift_recs(cur,
2377 xfs_btree_rec_addr(cur, 2, right),
2379 xfs_btree_log_recs(cur, rbp, 1, rrecs);
2383 * Using a temporary cursor, update the parent key values of the
2384 * block on the left.
2386 if (cur->bc_flags & XFS_BTREE_OVERLAPPING) {
2387 error = xfs_btree_dup_cursor(cur, &tcur);
2390 i = xfs_btree_firstrec(tcur, level);
2391 if (XFS_IS_CORRUPT(tcur->bc_mp, i != 1)) {
2392 error = -EFSCORRUPTED;
2396 error = xfs_btree_decrement(tcur, level, &i);
2400 /* Update the parent high keys of the left block, if needed. */
2401 error = xfs_btree_update_keys(tcur, level);
2405 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
2408 /* Update the parent keys of the right block. */
2409 error = xfs_btree_update_keys(cur, level);
2413 /* Slide the cursor value left one. */
2414 cur->bc_ptrs[level]--;
2427 xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
2432 * Move 1 record right from cur/level if possible.
2433 * Update cur to reflect the new path.
2435 STATIC int /* error */
2437 struct xfs_btree_cur *cur,
2439 int *stat) /* success/failure */
2441 struct xfs_buf *lbp; /* left buffer pointer */
2442 struct xfs_btree_block *left; /* left btree block */
2443 struct xfs_buf *rbp; /* right buffer pointer */
2444 struct xfs_btree_block *right; /* right btree block */
2445 struct xfs_btree_cur *tcur; /* temporary btree cursor */
2446 union xfs_btree_ptr rptr; /* right block pointer */
2447 union xfs_btree_key *rkp; /* right btree key */
2448 int rrecs; /* right record count */
2449 int lrecs; /* left record count */
2450 int error; /* error return value */
2451 int i; /* loop counter */
2453 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
2454 (level == cur->bc_nlevels - 1))
2457 /* Set up variables for this block as "left". */
2458 left = xfs_btree_get_block(cur, level, &lbp);
2461 error = xfs_btree_check_block(cur, left, level, lbp);
2466 /* If we've got no right sibling then we can't shift an entry right. */
2467 xfs_btree_get_sibling(cur, left, &rptr, XFS_BB_RIGHTSIB);
2468 if (xfs_btree_ptr_is_null(cur, &rptr))
2472 * If the cursor entry is the one that would be moved, don't
2473 * do it... it's too complicated.
2475 lrecs = xfs_btree_get_numrecs(left);
2476 if (cur->bc_ptrs[level] >= lrecs)
2479 /* Set up the right neighbor as "right". */
2480 error = xfs_btree_read_buf_block(cur, &rptr, 0, &right, &rbp);
2484 /* If it's full, it can't take another entry. */
2485 rrecs = xfs_btree_get_numrecs(right);
2486 if (rrecs == cur->bc_ops->get_maxrecs(cur, level))
2489 XFS_BTREE_STATS_INC(cur, rshift);
2490 XFS_BTREE_STATS_ADD(cur, moves, rrecs);
2493 * Make a hole at the start of the right neighbor block, then
2494 * copy the last left block entry to the hole.
2497 /* It's a nonleaf. make a hole in the keys and ptrs */
2498 union xfs_btree_key *lkp;
2499 union xfs_btree_ptr *lpp;
2500 union xfs_btree_ptr *rpp;
2502 lkp = xfs_btree_key_addr(cur, lrecs, left);
2503 lpp = xfs_btree_ptr_addr(cur, lrecs, left);
2504 rkp = xfs_btree_key_addr(cur, 1, right);
2505 rpp = xfs_btree_ptr_addr(cur, 1, right);
2507 for (i = rrecs - 1; i >= 0; i--) {
2508 error = xfs_btree_debug_check_ptr(cur, rpp, i, level);
2513 xfs_btree_shift_keys(cur, rkp, 1, rrecs);
2514 xfs_btree_shift_ptrs(cur, rpp, 1, rrecs);
2516 error = xfs_btree_debug_check_ptr(cur, lpp, 0, level);
2520 /* Now put the new data in, and log it. */
2521 xfs_btree_copy_keys(cur, rkp, lkp, 1);
2522 xfs_btree_copy_ptrs(cur, rpp, lpp, 1);
2524 xfs_btree_log_keys(cur, rbp, 1, rrecs + 1);
2525 xfs_btree_log_ptrs(cur, rbp, 1, rrecs + 1);
2527 ASSERT(cur->bc_ops->keys_inorder(cur, rkp,
2528 xfs_btree_key_addr(cur, 2, right)));
2530 /* It's a leaf. make a hole in the records */
2531 union xfs_btree_rec *lrp;
2532 union xfs_btree_rec *rrp;
2534 lrp = xfs_btree_rec_addr(cur, lrecs, left);
2535 rrp = xfs_btree_rec_addr(cur, 1, right);
2537 xfs_btree_shift_recs(cur, rrp, 1, rrecs);
2539 /* Now put the new data in, and log it. */
2540 xfs_btree_copy_recs(cur, rrp, lrp, 1);
2541 xfs_btree_log_recs(cur, rbp, 1, rrecs + 1);
2545 * Decrement and log left's numrecs, bump and log right's numrecs.
2547 xfs_btree_set_numrecs(left, --lrecs);
2548 xfs_btree_log_block(cur, lbp, XFS_BB_NUMRECS);
2550 xfs_btree_set_numrecs(right, ++rrecs);
2551 xfs_btree_log_block(cur, rbp, XFS_BB_NUMRECS);
2554 * Using a temporary cursor, update the parent key values of the
2555 * block on the right.
2557 error = xfs_btree_dup_cursor(cur, &tcur);
2560 i = xfs_btree_lastrec(tcur, level);
2561 if (XFS_IS_CORRUPT(tcur->bc_mp, i != 1)) {
2562 error = -EFSCORRUPTED;
2566 error = xfs_btree_increment(tcur, level, &i);
2570 /* Update the parent high keys of the left block, if needed. */
2571 if (cur->bc_flags & XFS_BTREE_OVERLAPPING) {
2572 error = xfs_btree_update_keys(cur, level);
2577 /* Update the parent keys of the right block. */
2578 error = xfs_btree_update_keys(tcur, level);
2582 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
2595 xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
2600 * Split cur/level block in half.
2601 * Return new block number and the key to its first
2602 * record (to be inserted into parent).
2604 STATIC int /* error */
2606 struct xfs_btree_cur *cur,
2608 union xfs_btree_ptr *ptrp,
2609 union xfs_btree_key *key,
2610 struct xfs_btree_cur **curp,
2611 int *stat) /* success/failure */
2613 union xfs_btree_ptr lptr; /* left sibling block ptr */
2614 struct xfs_buf *lbp; /* left buffer pointer */
2615 struct xfs_btree_block *left; /* left btree block */
2616 union xfs_btree_ptr rptr; /* right sibling block ptr */
2617 struct xfs_buf *rbp; /* right buffer pointer */
2618 struct xfs_btree_block *right; /* right btree block */
2619 union xfs_btree_ptr rrptr; /* right-right sibling ptr */
2620 struct xfs_buf *rrbp; /* right-right buffer pointer */
2621 struct xfs_btree_block *rrblock; /* right-right btree block */
2625 int error; /* error return value */
2628 XFS_BTREE_STATS_INC(cur, split);
2630 /* Set up left block (current one). */
2631 left = xfs_btree_get_block(cur, level, &lbp);
2634 error = xfs_btree_check_block(cur, left, level, lbp);
2639 xfs_btree_buf_to_ptr(cur, lbp, &lptr);
2641 /* Allocate the new block. If we can't do it, we're toast. Give up. */
2642 error = cur->bc_ops->alloc_block(cur, &lptr, &rptr, stat);
2647 XFS_BTREE_STATS_INC(cur, alloc);
2649 /* Set up the new block as "right". */
2650 error = xfs_btree_get_buf_block(cur, &rptr, &right, &rbp);
2654 /* Fill in the btree header for the new right block. */
2655 xfs_btree_init_block_cur(cur, rbp, xfs_btree_get_level(left), 0);
2658 * Split the entries between the old and the new block evenly.
2659 * Make sure that if there's an odd number of entries now, that
2660 * each new block will have the same number of entries.
2662 lrecs = xfs_btree_get_numrecs(left);
2664 if ((lrecs & 1) && cur->bc_ptrs[level] <= rrecs + 1)
2666 src_index = (lrecs - rrecs + 1);
2668 XFS_BTREE_STATS_ADD(cur, moves, rrecs);
2670 /* Adjust numrecs for the later get_*_keys() calls. */
2672 xfs_btree_set_numrecs(left, lrecs);
2673 xfs_btree_set_numrecs(right, xfs_btree_get_numrecs(right) + rrecs);
2676 * Copy btree block entries from the left block over to the
2677 * new block, the right. Update the right block and log the
2681 /* It's a non-leaf. Move keys and pointers. */
2682 union xfs_btree_key *lkp; /* left btree key */
2683 union xfs_btree_ptr *lpp; /* left address pointer */
2684 union xfs_btree_key *rkp; /* right btree key */
2685 union xfs_btree_ptr *rpp; /* right address pointer */
2687 lkp = xfs_btree_key_addr(cur, src_index, left);
2688 lpp = xfs_btree_ptr_addr(cur, src_index, left);
2689 rkp = xfs_btree_key_addr(cur, 1, right);
2690 rpp = xfs_btree_ptr_addr(cur, 1, right);
2692 for (i = src_index; i < rrecs; i++) {
2693 error = xfs_btree_debug_check_ptr(cur, lpp, i, level);
2698 /* Copy the keys & pointers to the new block. */
2699 xfs_btree_copy_keys(cur, rkp, lkp, rrecs);
2700 xfs_btree_copy_ptrs(cur, rpp, lpp, rrecs);
2702 xfs_btree_log_keys(cur, rbp, 1, rrecs);
2703 xfs_btree_log_ptrs(cur, rbp, 1, rrecs);
2705 /* Stash the keys of the new block for later insertion. */
2706 xfs_btree_get_node_keys(cur, right, key);
2708 /* It's a leaf. Move records. */
2709 union xfs_btree_rec *lrp; /* left record pointer */
2710 union xfs_btree_rec *rrp; /* right record pointer */
2712 lrp = xfs_btree_rec_addr(cur, src_index, left);
2713 rrp = xfs_btree_rec_addr(cur, 1, right);
2715 /* Copy records to the new block. */
2716 xfs_btree_copy_recs(cur, rrp, lrp, rrecs);
2717 xfs_btree_log_recs(cur, rbp, 1, rrecs);
2719 /* Stash the keys of the new block for later insertion. */
2720 xfs_btree_get_leaf_keys(cur, right, key);
2724 * Find the left block number by looking in the buffer.
2725 * Adjust sibling pointers.
2727 xfs_btree_get_sibling(cur, left, &rrptr, XFS_BB_RIGHTSIB);
2728 xfs_btree_set_sibling(cur, right, &rrptr, XFS_BB_RIGHTSIB);
2729 xfs_btree_set_sibling(cur, right, &lptr, XFS_BB_LEFTSIB);
2730 xfs_btree_set_sibling(cur, left, &rptr, XFS_BB_RIGHTSIB);
2732 xfs_btree_log_block(cur, rbp, XFS_BB_ALL_BITS);
2733 xfs_btree_log_block(cur, lbp, XFS_BB_NUMRECS | XFS_BB_RIGHTSIB);
2736 * If there's a block to the new block's right, make that block
2737 * point back to right instead of to left.
2739 if (!xfs_btree_ptr_is_null(cur, &rrptr)) {
2740 error = xfs_btree_read_buf_block(cur, &rrptr,
2741 0, &rrblock, &rrbp);
2744 xfs_btree_set_sibling(cur, rrblock, &rptr, XFS_BB_LEFTSIB);
2745 xfs_btree_log_block(cur, rrbp, XFS_BB_LEFTSIB);
2748 /* Update the parent high keys of the left block, if needed. */
2749 if (cur->bc_flags & XFS_BTREE_OVERLAPPING) {
2750 error = xfs_btree_update_keys(cur, level);
2756 * If the cursor is really in the right block, move it there.
2757 * If it's just pointing past the last entry in left, then we'll
2758 * insert there, so don't change anything in that case.
2760 if (cur->bc_ptrs[level] > lrecs + 1) {
2761 xfs_btree_setbuf(cur, level, rbp);
2762 cur->bc_ptrs[level] -= lrecs;
2765 * If there are more levels, we'll need another cursor which refers
2766 * the right block, no matter where this cursor was.
2768 if (level + 1 < cur->bc_nlevels) {
2769 error = xfs_btree_dup_cursor(cur, curp);
2772 (*curp)->bc_ptrs[level + 1]++;
2785 struct xfs_btree_split_args {
2786 struct xfs_btree_cur *cur;
2788 union xfs_btree_ptr *ptrp;
2789 union xfs_btree_key *key;
2790 struct xfs_btree_cur **curp;
2791 int *stat; /* success/failure */
2793 bool kswapd; /* allocation in kswapd context */
2794 struct completion *done;
2795 struct work_struct work;
2799 * Stack switching interfaces for allocation
2802 xfs_btree_split_worker(
2803 struct work_struct *work)
2805 struct xfs_btree_split_args *args = container_of(work,
2806 struct xfs_btree_split_args, work);
2807 unsigned long pflags;
2808 unsigned long new_pflags = 0;
2811 * we are in a transaction context here, but may also be doing work
2812 * in kswapd context, and hence we may need to inherit that state
2813 * temporarily to ensure that we don't block waiting for memory reclaim
2817 new_pflags |= PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD;
2819 current_set_flags_nested(&pflags, new_pflags);
2820 xfs_trans_set_context(args->cur->bc_tp);
2822 args->result = __xfs_btree_split(args->cur, args->level, args->ptrp,
2823 args->key, args->curp, args->stat);
2825 xfs_trans_clear_context(args->cur->bc_tp);
2826 current_restore_flags_nested(&pflags, new_pflags);
2829 * Do not access args after complete() has run here. We don't own args
2830 * and the owner may run and free args before we return here.
2832 complete(args->done);
2837 * BMBT split requests often come in with little stack to work on. Push
2838 * them off to a worker thread so there is lots of stack to use. For the other
2839 * btree types, just call directly to avoid the context switch overhead here.
2841 STATIC int /* error */
2843 struct xfs_btree_cur *cur,
2845 union xfs_btree_ptr *ptrp,
2846 union xfs_btree_key *key,
2847 struct xfs_btree_cur **curp,
2848 int *stat) /* success/failure */
2850 struct xfs_btree_split_args args;
2851 DECLARE_COMPLETION_ONSTACK(done);
2853 if (cur->bc_btnum != XFS_BTNUM_BMAP)
2854 return __xfs_btree_split(cur, level, ptrp, key, curp, stat);
2863 args.kswapd = current_is_kswapd();
2864 INIT_WORK_ONSTACK(&args.work, xfs_btree_split_worker);
2865 queue_work(xfs_alloc_wq, &args.work);
2866 wait_for_completion(&done);
2867 destroy_work_on_stack(&args.work);
2873 * Copy the old inode root contents into a real block and make the
2874 * broot point to it.
2877 xfs_btree_new_iroot(
2878 struct xfs_btree_cur *cur, /* btree cursor */
2879 int *logflags, /* logging flags for inode */
2880 int *stat) /* return status - 0 fail */
2882 struct xfs_buf *cbp; /* buffer for cblock */
2883 struct xfs_btree_block *block; /* btree block */
2884 struct xfs_btree_block *cblock; /* child btree block */
2885 union xfs_btree_key *ckp; /* child key pointer */
2886 union xfs_btree_ptr *cpp; /* child ptr pointer */
2887 union xfs_btree_key *kp; /* pointer to btree key */
2888 union xfs_btree_ptr *pp; /* pointer to block addr */
2889 union xfs_btree_ptr nptr; /* new block addr */
2890 int level; /* btree level */
2891 int error; /* error return code */
2892 int i; /* loop counter */
2894 XFS_BTREE_STATS_INC(cur, newroot);
2896 ASSERT(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE);
2898 level = cur->bc_nlevels - 1;
2900 block = xfs_btree_get_iroot(cur);
2901 pp = xfs_btree_ptr_addr(cur, 1, block);
2903 /* Allocate the new block. If we can't do it, we're toast. Give up. */
2904 error = cur->bc_ops->alloc_block(cur, pp, &nptr, stat);
2910 XFS_BTREE_STATS_INC(cur, alloc);
2912 /* Copy the root into a real block. */
2913 error = xfs_btree_get_buf_block(cur, &nptr, &cblock, &cbp);
2918 * we can't just memcpy() the root in for CRC enabled btree blocks.
2919 * In that case have to also ensure the blkno remains correct
2921 memcpy(cblock, block, xfs_btree_block_len(cur));
2922 if (cur->bc_flags & XFS_BTREE_CRC_BLOCKS) {
2923 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
2924 cblock->bb_u.l.bb_blkno = cpu_to_be64(cbp->b_bn);
2926 cblock->bb_u.s.bb_blkno = cpu_to_be64(cbp->b_bn);
2929 be16_add_cpu(&block->bb_level, 1);
2930 xfs_btree_set_numrecs(block, 1);
2932 cur->bc_ptrs[level + 1] = 1;
2934 kp = xfs_btree_key_addr(cur, 1, block);
2935 ckp = xfs_btree_key_addr(cur, 1, cblock);
2936 xfs_btree_copy_keys(cur, ckp, kp, xfs_btree_get_numrecs(cblock));
2938 cpp = xfs_btree_ptr_addr(cur, 1, cblock);
2939 for (i = 0; i < be16_to_cpu(cblock->bb_numrecs); i++) {
2940 error = xfs_btree_debug_check_ptr(cur, pp, i, level);
2945 xfs_btree_copy_ptrs(cur, cpp, pp, xfs_btree_get_numrecs(cblock));
2947 error = xfs_btree_debug_check_ptr(cur, &nptr, 0, level);
2951 xfs_btree_copy_ptrs(cur, pp, &nptr, 1);
2953 xfs_iroot_realloc(cur->bc_ino.ip,
2954 1 - xfs_btree_get_numrecs(cblock),
2955 cur->bc_ino.whichfork);
2957 xfs_btree_setbuf(cur, level, cbp);
2960 * Do all this logging at the end so that
2961 * the root is at the right level.
2963 xfs_btree_log_block(cur, cbp, XFS_BB_ALL_BITS);
2964 xfs_btree_log_keys(cur, cbp, 1, be16_to_cpu(cblock->bb_numrecs));
2965 xfs_btree_log_ptrs(cur, cbp, 1, be16_to_cpu(cblock->bb_numrecs));
2968 XFS_ILOG_CORE | xfs_ilog_fbroot(cur->bc_ino.whichfork);
2976 * Allocate a new root block, fill it in.
2978 STATIC int /* error */
2980 struct xfs_btree_cur *cur, /* btree cursor */
2981 int *stat) /* success/failure */
2983 struct xfs_btree_block *block; /* one half of the old root block */
2984 struct xfs_buf *bp; /* buffer containing block */
2985 int error; /* error return value */
2986 struct xfs_buf *lbp; /* left buffer pointer */
2987 struct xfs_btree_block *left; /* left btree block */
2988 struct xfs_buf *nbp; /* new (root) buffer */
2989 struct xfs_btree_block *new; /* new (root) btree block */
2990 int nptr; /* new value for key index, 1 or 2 */
2991 struct xfs_buf *rbp; /* right buffer pointer */
2992 struct xfs_btree_block *right; /* right btree block */
2993 union xfs_btree_ptr rptr;
2994 union xfs_btree_ptr lptr;
2996 XFS_BTREE_STATS_INC(cur, newroot);
2998 /* initialise our start point from the cursor */
2999 cur->bc_ops->init_ptr_from_cur(cur, &rptr);
3001 /* Allocate the new block. If we can't do it, we're toast. Give up. */
3002 error = cur->bc_ops->alloc_block(cur, &rptr, &lptr, stat);
3007 XFS_BTREE_STATS_INC(cur, alloc);
3009 /* Set up the new block. */
3010 error = xfs_btree_get_buf_block(cur, &lptr, &new, &nbp);
3014 /* Set the root in the holding structure increasing the level by 1. */
3015 cur->bc_ops->set_root(cur, &lptr, 1);
3018 * At the previous root level there are now two blocks: the old root,
3019 * and the new block generated when it was split. We don't know which
3020 * one the cursor is pointing at, so we set up variables "left" and
3021 * "right" for each case.
3023 block = xfs_btree_get_block(cur, cur->bc_nlevels - 1, &bp);
3026 error = xfs_btree_check_block(cur, block, cur->bc_nlevels - 1, bp);
3031 xfs_btree_get_sibling(cur, block, &rptr, XFS_BB_RIGHTSIB);
3032 if (!xfs_btree_ptr_is_null(cur, &rptr)) {
3033 /* Our block is left, pick up the right block. */
3035 xfs_btree_buf_to_ptr(cur, lbp, &lptr);
3037 error = xfs_btree_read_buf_block(cur, &rptr, 0, &right, &rbp);
3043 /* Our block is right, pick up the left block. */
3045 xfs_btree_buf_to_ptr(cur, rbp, &rptr);
3047 xfs_btree_get_sibling(cur, right, &lptr, XFS_BB_LEFTSIB);
3048 error = xfs_btree_read_buf_block(cur, &lptr, 0, &left, &lbp);
3055 /* Fill in the new block's btree header and log it. */
3056 xfs_btree_init_block_cur(cur, nbp, cur->bc_nlevels, 2);
3057 xfs_btree_log_block(cur, nbp, XFS_BB_ALL_BITS);
3058 ASSERT(!xfs_btree_ptr_is_null(cur, &lptr) &&
3059 !xfs_btree_ptr_is_null(cur, &rptr));
3061 /* Fill in the key data in the new root. */
3062 if (xfs_btree_get_level(left) > 0) {
3064 * Get the keys for the left block's keys and put them directly
3065 * in the parent block. Do the same for the right block.
3067 xfs_btree_get_node_keys(cur, left,
3068 xfs_btree_key_addr(cur, 1, new));
3069 xfs_btree_get_node_keys(cur, right,
3070 xfs_btree_key_addr(cur, 2, new));
3073 * Get the keys for the left block's records and put them
3074 * directly in the parent block. Do the same for the right
3077 xfs_btree_get_leaf_keys(cur, left,
3078 xfs_btree_key_addr(cur, 1, new));
3079 xfs_btree_get_leaf_keys(cur, right,
3080 xfs_btree_key_addr(cur, 2, new));
3082 xfs_btree_log_keys(cur, nbp, 1, 2);
3084 /* Fill in the pointer data in the new root. */
3085 xfs_btree_copy_ptrs(cur,
3086 xfs_btree_ptr_addr(cur, 1, new), &lptr, 1);
3087 xfs_btree_copy_ptrs(cur,
3088 xfs_btree_ptr_addr(cur, 2, new), &rptr, 1);
3089 xfs_btree_log_ptrs(cur, nbp, 1, 2);
3091 /* Fix up the cursor. */
3092 xfs_btree_setbuf(cur, cur->bc_nlevels, nbp);
3093 cur->bc_ptrs[cur->bc_nlevels] = nptr;
3105 xfs_btree_make_block_unfull(
3106 struct xfs_btree_cur *cur, /* btree cursor */
3107 int level, /* btree level */
3108 int numrecs,/* # of recs in block */
3109 int *oindex,/* old tree index */
3110 int *index, /* new tree index */
3111 union xfs_btree_ptr *nptr, /* new btree ptr */
3112 struct xfs_btree_cur **ncur, /* new btree cursor */
3113 union xfs_btree_key *key, /* key of new block */
3118 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
3119 level == cur->bc_nlevels - 1) {
3120 struct xfs_inode *ip = cur->bc_ino.ip;
3122 if (numrecs < cur->bc_ops->get_dmaxrecs(cur, level)) {
3123 /* A root block that can be made bigger. */
3124 xfs_iroot_realloc(ip, 1, cur->bc_ino.whichfork);
3127 /* A root block that needs replacing */
3130 error = xfs_btree_new_iroot(cur, &logflags, stat);
3131 if (error || *stat == 0)
3134 xfs_trans_log_inode(cur->bc_tp, ip, logflags);
3140 /* First, try shifting an entry to the right neighbor. */
3141 error = xfs_btree_rshift(cur, level, stat);
3145 /* Next, try shifting an entry to the left neighbor. */
3146 error = xfs_btree_lshift(cur, level, stat);
3151 *oindex = *index = cur->bc_ptrs[level];
3156 * Next, try splitting the current block in half.
3158 * If this works we have to re-set our variables because we
3159 * could be in a different block now.
3161 error = xfs_btree_split(cur, level, nptr, key, ncur, stat);
3162 if (error || *stat == 0)
3166 *index = cur->bc_ptrs[level];
3171 * Insert one record/level. Return information to the caller
3172 * allowing the next level up to proceed if necessary.
3176 struct xfs_btree_cur *cur, /* btree cursor */
3177 int level, /* level to insert record at */
3178 union xfs_btree_ptr *ptrp, /* i/o: block number inserted */
3179 union xfs_btree_rec *rec, /* record to insert */
3180 union xfs_btree_key *key, /* i/o: block key for ptrp */
3181 struct xfs_btree_cur **curp, /* output: new cursor replacing cur */
3182 int *stat) /* success/failure */
3184 struct xfs_btree_block *block; /* btree block */
3185 struct xfs_buf *bp; /* buffer for block */
3186 union xfs_btree_ptr nptr; /* new block ptr */
3187 struct xfs_btree_cur *ncur; /* new btree cursor */
3188 union xfs_btree_key nkey; /* new block key */
3189 union xfs_btree_key *lkey;
3190 int optr; /* old key/record index */
3191 int ptr; /* key/record index */
3192 int numrecs;/* number of records */
3193 int error; /* error return value */
3201 * If we have an external root pointer, and we've made it to the
3202 * root level, allocate a new root block and we're done.
3204 if (!(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
3205 (level >= cur->bc_nlevels)) {
3206 error = xfs_btree_new_root(cur, stat);
3207 xfs_btree_set_ptr_null(cur, ptrp);
3212 /* If we're off the left edge, return failure. */
3213 ptr = cur->bc_ptrs[level];
3221 XFS_BTREE_STATS_INC(cur, insrec);
3223 /* Get pointers to the btree buffer and block. */
3224 block = xfs_btree_get_block(cur, level, &bp);
3225 old_bn = bp ? bp->b_bn : XFS_BUF_DADDR_NULL;
3226 numrecs = xfs_btree_get_numrecs(block);
3229 error = xfs_btree_check_block(cur, block, level, bp);
3233 /* Check that the new entry is being inserted in the right place. */
3234 if (ptr <= numrecs) {
3236 ASSERT(cur->bc_ops->recs_inorder(cur, rec,
3237 xfs_btree_rec_addr(cur, ptr, block)));
3239 ASSERT(cur->bc_ops->keys_inorder(cur, key,
3240 xfs_btree_key_addr(cur, ptr, block)));
3246 * If the block is full, we can't insert the new entry until we
3247 * make the block un-full.
3249 xfs_btree_set_ptr_null(cur, &nptr);
3250 if (numrecs == cur->bc_ops->get_maxrecs(cur, level)) {
3251 error = xfs_btree_make_block_unfull(cur, level, numrecs,
3252 &optr, &ptr, &nptr, &ncur, lkey, stat);
3253 if (error || *stat == 0)
3258 * The current block may have changed if the block was
3259 * previously full and we have just made space in it.
3261 block = xfs_btree_get_block(cur, level, &bp);
3262 numrecs = xfs_btree_get_numrecs(block);
3265 error = xfs_btree_check_block(cur, block, level, bp);
3271 * At this point we know there's room for our new entry in the block
3272 * we're pointing at.
3274 XFS_BTREE_STATS_ADD(cur, moves, numrecs - ptr + 1);
3277 /* It's a nonleaf. make a hole in the keys and ptrs */
3278 union xfs_btree_key *kp;
3279 union xfs_btree_ptr *pp;
3281 kp = xfs_btree_key_addr(cur, ptr, block);
3282 pp = xfs_btree_ptr_addr(cur, ptr, block);
3284 for (i = numrecs - ptr; i >= 0; i--) {
3285 error = xfs_btree_debug_check_ptr(cur, pp, i, level);
3290 xfs_btree_shift_keys(cur, kp, 1, numrecs - ptr + 1);
3291 xfs_btree_shift_ptrs(cur, pp, 1, numrecs - ptr + 1);
3293 error = xfs_btree_debug_check_ptr(cur, ptrp, 0, level);
3297 /* Now put the new data in, bump numrecs and log it. */
3298 xfs_btree_copy_keys(cur, kp, key, 1);
3299 xfs_btree_copy_ptrs(cur, pp, ptrp, 1);
3301 xfs_btree_set_numrecs(block, numrecs);
3302 xfs_btree_log_ptrs(cur, bp, ptr, numrecs);
3303 xfs_btree_log_keys(cur, bp, ptr, numrecs);
3305 if (ptr < numrecs) {
3306 ASSERT(cur->bc_ops->keys_inorder(cur, kp,
3307 xfs_btree_key_addr(cur, ptr + 1, block)));
3311 /* It's a leaf. make a hole in the records */
3312 union xfs_btree_rec *rp;
3314 rp = xfs_btree_rec_addr(cur, ptr, block);
3316 xfs_btree_shift_recs(cur, rp, 1, numrecs - ptr + 1);
3318 /* Now put the new data in, bump numrecs and log it. */
3319 xfs_btree_copy_recs(cur, rp, rec, 1);
3320 xfs_btree_set_numrecs(block, ++numrecs);
3321 xfs_btree_log_recs(cur, bp, ptr, numrecs);
3323 if (ptr < numrecs) {
3324 ASSERT(cur->bc_ops->recs_inorder(cur, rp,
3325 xfs_btree_rec_addr(cur, ptr + 1, block)));
3330 /* Log the new number of records in the btree header. */
3331 xfs_btree_log_block(cur, bp, XFS_BB_NUMRECS);
3334 * If we just inserted into a new tree block, we have to
3335 * recalculate nkey here because nkey is out of date.
3337 * Otherwise we're just updating an existing block (having shoved
3338 * some records into the new tree block), so use the regular key
3341 if (bp && bp->b_bn != old_bn) {
3342 xfs_btree_get_keys(cur, block, lkey);
3343 } else if (xfs_btree_needs_key_update(cur, optr)) {
3344 error = xfs_btree_update_keys(cur, level);
3350 * If we are tracking the last record in the tree and
3351 * we are at the far right edge of the tree, update it.
3353 if (xfs_btree_is_lastrec(cur, block, level)) {
3354 cur->bc_ops->update_lastrec(cur, block, rec,
3355 ptr, LASTREC_INSREC);
3359 * Return the new block number, if any.
3360 * If there is one, give back a record value and a cursor too.
3363 if (!xfs_btree_ptr_is_null(cur, &nptr)) {
3364 xfs_btree_copy_keys(cur, key, lkey, 1);
3376 * Insert the record at the point referenced by cur.
3378 * A multi-level split of the tree on insert will invalidate the original
3379 * cursor. All callers of this function should assume that the cursor is
3380 * no longer valid and revalidate it.
3384 struct xfs_btree_cur *cur,
3387 int error; /* error return value */
3388 int i; /* result value, 0 for failure */
3389 int level; /* current level number in btree */
3390 union xfs_btree_ptr nptr; /* new block number (split result) */
3391 struct xfs_btree_cur *ncur; /* new cursor (split result) */
3392 struct xfs_btree_cur *pcur; /* previous level's cursor */
3393 union xfs_btree_key bkey; /* key of block to insert */
3394 union xfs_btree_key *key;
3395 union xfs_btree_rec rec; /* record to insert */
3402 xfs_btree_set_ptr_null(cur, &nptr);
3404 /* Make a key out of the record data to be inserted, and save it. */
3405 cur->bc_ops->init_rec_from_cur(cur, &rec);
3406 cur->bc_ops->init_key_from_rec(key, &rec);
3409 * Loop going up the tree, starting at the leaf level.
3410 * Stop when we don't get a split block, that must mean that
3411 * the insert is finished with this level.
3415 * Insert nrec/nptr into this level of the tree.
3416 * Note if we fail, nptr will be null.
3418 error = xfs_btree_insrec(pcur, level, &nptr, &rec, key,
3422 xfs_btree_del_cursor(pcur, XFS_BTREE_ERROR);
3426 if (XFS_IS_CORRUPT(cur->bc_mp, i != 1)) {
3427 error = -EFSCORRUPTED;
3433 * See if the cursor we just used is trash.
3434 * Can't trash the caller's cursor, but otherwise we should
3435 * if ncur is a new cursor or we're about to be done.
3438 (ncur || xfs_btree_ptr_is_null(cur, &nptr))) {
3439 /* Save the state from the cursor before we trash it */
3440 if (cur->bc_ops->update_cursor)
3441 cur->bc_ops->update_cursor(pcur, cur);
3442 cur->bc_nlevels = pcur->bc_nlevels;
3443 xfs_btree_del_cursor(pcur, XFS_BTREE_NOERROR);
3445 /* If we got a new cursor, switch to it. */
3450 } while (!xfs_btree_ptr_is_null(cur, &nptr));
3459 * Try to merge a non-leaf block back into the inode root.
3461 * Note: the killroot names comes from the fact that we're effectively
3462 * killing the old root block. But because we can't just delete the
3463 * inode we have to copy the single block it was pointing to into the
3467 xfs_btree_kill_iroot(
3468 struct xfs_btree_cur *cur)
3470 int whichfork = cur->bc_ino.whichfork;
3471 struct xfs_inode *ip = cur->bc_ino.ip;
3472 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
3473 struct xfs_btree_block *block;
3474 struct xfs_btree_block *cblock;
3475 union xfs_btree_key *kp;
3476 union xfs_btree_key *ckp;
3477 union xfs_btree_ptr *pp;
3478 union xfs_btree_ptr *cpp;
3479 struct xfs_buf *cbp;
3485 union xfs_btree_ptr ptr;
3489 ASSERT(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE);
3490 ASSERT(cur->bc_nlevels > 1);
3493 * Don't deal with the root block needs to be a leaf case.
3494 * We're just going to turn the thing back into extents anyway.
3496 level = cur->bc_nlevels - 1;
3501 * Give up if the root has multiple children.
3503 block = xfs_btree_get_iroot(cur);
3504 if (xfs_btree_get_numrecs(block) != 1)
3507 cblock = xfs_btree_get_block(cur, level - 1, &cbp);
3508 numrecs = xfs_btree_get_numrecs(cblock);
3511 * Only do this if the next level will fit.
3512 * Then the data must be copied up to the inode,
3513 * instead of freeing the root you free the next level.
3515 if (numrecs > cur->bc_ops->get_dmaxrecs(cur, level))
3518 XFS_BTREE_STATS_INC(cur, killroot);
3521 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_LEFTSIB);
3522 ASSERT(xfs_btree_ptr_is_null(cur, &ptr));
3523 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_RIGHTSIB);
3524 ASSERT(xfs_btree_ptr_is_null(cur, &ptr));
3527 index = numrecs - cur->bc_ops->get_maxrecs(cur, level);
3529 xfs_iroot_realloc(cur->bc_ino.ip, index,
3530 cur->bc_ino.whichfork);
3531 block = ifp->if_broot;
3534 be16_add_cpu(&block->bb_numrecs, index);
3535 ASSERT(block->bb_numrecs == cblock->bb_numrecs);
3537 kp = xfs_btree_key_addr(cur, 1, block);
3538 ckp = xfs_btree_key_addr(cur, 1, cblock);
3539 xfs_btree_copy_keys(cur, kp, ckp, numrecs);
3541 pp = xfs_btree_ptr_addr(cur, 1, block);
3542 cpp = xfs_btree_ptr_addr(cur, 1, cblock);
3544 for (i = 0; i < numrecs; i++) {
3545 error = xfs_btree_debug_check_ptr(cur, cpp, i, level - 1);
3550 xfs_btree_copy_ptrs(cur, pp, cpp, numrecs);
3552 error = xfs_btree_free_block(cur, cbp);
3556 cur->bc_bufs[level - 1] = NULL;
3557 be16_add_cpu(&block->bb_level, -1);
3558 xfs_trans_log_inode(cur->bc_tp, ip,
3559 XFS_ILOG_CORE | xfs_ilog_fbroot(cur->bc_ino.whichfork));
3566 * Kill the current root node, and replace it with it's only child node.
3569 xfs_btree_kill_root(
3570 struct xfs_btree_cur *cur,
3573 union xfs_btree_ptr *newroot)
3577 XFS_BTREE_STATS_INC(cur, killroot);
3580 * Update the root pointer, decreasing the level by 1 and then
3581 * free the old root.
3583 cur->bc_ops->set_root(cur, newroot, -1);
3585 error = xfs_btree_free_block(cur, bp);
3589 cur->bc_bufs[level] = NULL;
3590 cur->bc_ra[level] = 0;
3597 xfs_btree_dec_cursor(
3598 struct xfs_btree_cur *cur,
3606 error = xfs_btree_decrement(cur, level, &i);
3616 * Single level of the btree record deletion routine.
3617 * Delete record pointed to by cur/level.
3618 * Remove the record from its block then rebalance the tree.
3619 * Return 0 for error, 1 for done, 2 to go on to the next level.
3621 STATIC int /* error */
3623 struct xfs_btree_cur *cur, /* btree cursor */
3624 int level, /* level removing record from */
3625 int *stat) /* fail/done/go-on */
3627 struct xfs_btree_block *block; /* btree block */
3628 union xfs_btree_ptr cptr; /* current block ptr */
3629 struct xfs_buf *bp; /* buffer for block */
3630 int error; /* error return value */
3631 int i; /* loop counter */
3632 union xfs_btree_ptr lptr; /* left sibling block ptr */
3633 struct xfs_buf *lbp; /* left buffer pointer */
3634 struct xfs_btree_block *left; /* left btree block */
3635 int lrecs = 0; /* left record count */
3636 int ptr; /* key/record index */
3637 union xfs_btree_ptr rptr; /* right sibling block ptr */
3638 struct xfs_buf *rbp; /* right buffer pointer */
3639 struct xfs_btree_block *right; /* right btree block */
3640 struct xfs_btree_block *rrblock; /* right-right btree block */
3641 struct xfs_buf *rrbp; /* right-right buffer pointer */
3642 int rrecs = 0; /* right record count */
3643 struct xfs_btree_cur *tcur; /* temporary btree cursor */
3644 int numrecs; /* temporary numrec count */
3648 /* Get the index of the entry being deleted, check for nothing there. */
3649 ptr = cur->bc_ptrs[level];
3655 /* Get the buffer & block containing the record or key/ptr. */
3656 block = xfs_btree_get_block(cur, level, &bp);
3657 numrecs = xfs_btree_get_numrecs(block);
3660 error = xfs_btree_check_block(cur, block, level, bp);
3665 /* Fail if we're off the end of the block. */
3666 if (ptr > numrecs) {
3671 XFS_BTREE_STATS_INC(cur, delrec);
3672 XFS_BTREE_STATS_ADD(cur, moves, numrecs - ptr);
3674 /* Excise the entries being deleted. */
3676 /* It's a nonleaf. operate on keys and ptrs */
3677 union xfs_btree_key *lkp;
3678 union xfs_btree_ptr *lpp;
3680 lkp = xfs_btree_key_addr(cur, ptr + 1, block);
3681 lpp = xfs_btree_ptr_addr(cur, ptr + 1, block);
3683 for (i = 0; i < numrecs - ptr; i++) {
3684 error = xfs_btree_debug_check_ptr(cur, lpp, i, level);
3689 if (ptr < numrecs) {
3690 xfs_btree_shift_keys(cur, lkp, -1, numrecs - ptr);
3691 xfs_btree_shift_ptrs(cur, lpp, -1, numrecs - ptr);
3692 xfs_btree_log_keys(cur, bp, ptr, numrecs - 1);
3693 xfs_btree_log_ptrs(cur, bp, ptr, numrecs - 1);
3696 /* It's a leaf. operate on records */
3697 if (ptr < numrecs) {
3698 xfs_btree_shift_recs(cur,
3699 xfs_btree_rec_addr(cur, ptr + 1, block),
3701 xfs_btree_log_recs(cur, bp, ptr, numrecs - 1);
3706 * Decrement and log the number of entries in the block.
3708 xfs_btree_set_numrecs(block, --numrecs);
3709 xfs_btree_log_block(cur, bp, XFS_BB_NUMRECS);
3712 * If we are tracking the last record in the tree and
3713 * we are at the far right edge of the tree, update it.
3715 if (xfs_btree_is_lastrec(cur, block, level)) {
3716 cur->bc_ops->update_lastrec(cur, block, NULL,
3717 ptr, LASTREC_DELREC);
3721 * We're at the root level. First, shrink the root block in-memory.
3722 * Try to get rid of the next level down. If we can't then there's
3723 * nothing left to do.
3725 if (level == cur->bc_nlevels - 1) {
3726 if (cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) {
3727 xfs_iroot_realloc(cur->bc_ino.ip, -1,
3728 cur->bc_ino.whichfork);
3730 error = xfs_btree_kill_iroot(cur);
3734 error = xfs_btree_dec_cursor(cur, level, stat);
3742 * If this is the root level, and there's only one entry left,
3743 * and it's NOT the leaf level, then we can get rid of this
3746 if (numrecs == 1 && level > 0) {
3747 union xfs_btree_ptr *pp;
3749 * pp is still set to the first pointer in the block.
3750 * Make it the new root of the btree.
3752 pp = xfs_btree_ptr_addr(cur, 1, block);
3753 error = xfs_btree_kill_root(cur, bp, level, pp);
3756 } else if (level > 0) {
3757 error = xfs_btree_dec_cursor(cur, level, stat);
3766 * If we deleted the leftmost entry in the block, update the
3767 * key values above us in the tree.
3769 if (xfs_btree_needs_key_update(cur, ptr)) {
3770 error = xfs_btree_update_keys(cur, level);
3776 * If the number of records remaining in the block is at least
3777 * the minimum, we're done.
3779 if (numrecs >= cur->bc_ops->get_minrecs(cur, level)) {
3780 error = xfs_btree_dec_cursor(cur, level, stat);
3787 * Otherwise, we have to move some records around to keep the
3788 * tree balanced. Look at the left and right sibling blocks to
3789 * see if we can re-balance by moving only one record.
3791 xfs_btree_get_sibling(cur, block, &rptr, XFS_BB_RIGHTSIB);
3792 xfs_btree_get_sibling(cur, block, &lptr, XFS_BB_LEFTSIB);
3794 if (cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) {
3796 * One child of root, need to get a chance to copy its contents
3797 * into the root and delete it. Can't go up to next level,
3798 * there's nothing to delete there.
3800 if (xfs_btree_ptr_is_null(cur, &rptr) &&
3801 xfs_btree_ptr_is_null(cur, &lptr) &&
3802 level == cur->bc_nlevels - 2) {
3803 error = xfs_btree_kill_iroot(cur);
3805 error = xfs_btree_dec_cursor(cur, level, stat);
3812 ASSERT(!xfs_btree_ptr_is_null(cur, &rptr) ||
3813 !xfs_btree_ptr_is_null(cur, &lptr));
3816 * Duplicate the cursor so our btree manipulations here won't
3817 * disrupt the next level up.
3819 error = xfs_btree_dup_cursor(cur, &tcur);
3824 * If there's a right sibling, see if it's ok to shift an entry
3827 if (!xfs_btree_ptr_is_null(cur, &rptr)) {
3829 * Move the temp cursor to the last entry in the next block.
3830 * Actually any entry but the first would suffice.
3832 i = xfs_btree_lastrec(tcur, level);
3833 if (XFS_IS_CORRUPT(cur->bc_mp, i != 1)) {
3834 error = -EFSCORRUPTED;
3838 error = xfs_btree_increment(tcur, level, &i);
3841 if (XFS_IS_CORRUPT(cur->bc_mp, i != 1)) {
3842 error = -EFSCORRUPTED;
3846 i = xfs_btree_lastrec(tcur, level);
3847 if (XFS_IS_CORRUPT(cur->bc_mp, i != 1)) {
3848 error = -EFSCORRUPTED;
3852 /* Grab a pointer to the block. */
3853 right = xfs_btree_get_block(tcur, level, &rbp);
3855 error = xfs_btree_check_block(tcur, right, level, rbp);
3859 /* Grab the current block number, for future use. */
3860 xfs_btree_get_sibling(tcur, right, &cptr, XFS_BB_LEFTSIB);
3863 * If right block is full enough so that removing one entry
3864 * won't make it too empty, and left-shifting an entry out
3865 * of right to us works, we're done.
3867 if (xfs_btree_get_numrecs(right) - 1 >=
3868 cur->bc_ops->get_minrecs(tcur, level)) {
3869 error = xfs_btree_lshift(tcur, level, &i);
3873 ASSERT(xfs_btree_get_numrecs(block) >=
3874 cur->bc_ops->get_minrecs(tcur, level));
3876 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
3879 error = xfs_btree_dec_cursor(cur, level, stat);
3887 * Otherwise, grab the number of records in right for
3888 * future reference, and fix up the temp cursor to point
3889 * to our block again (last record).
3891 rrecs = xfs_btree_get_numrecs(right);
3892 if (!xfs_btree_ptr_is_null(cur, &lptr)) {
3893 i = xfs_btree_firstrec(tcur, level);
3894 if (XFS_IS_CORRUPT(cur->bc_mp, i != 1)) {
3895 error = -EFSCORRUPTED;
3899 error = xfs_btree_decrement(tcur, level, &i);
3902 if (XFS_IS_CORRUPT(cur->bc_mp, i != 1)) {
3903 error = -EFSCORRUPTED;
3910 * If there's a left sibling, see if it's ok to shift an entry
3913 if (!xfs_btree_ptr_is_null(cur, &lptr)) {
3915 * Move the temp cursor to the first entry in the
3918 i = xfs_btree_firstrec(tcur, level);
3919 if (XFS_IS_CORRUPT(cur->bc_mp, i != 1)) {
3920 error = -EFSCORRUPTED;
3924 error = xfs_btree_decrement(tcur, level, &i);
3927 i = xfs_btree_firstrec(tcur, level);
3928 if (XFS_IS_CORRUPT(cur->bc_mp, i != 1)) {
3929 error = -EFSCORRUPTED;
3933 /* Grab a pointer to the block. */
3934 left = xfs_btree_get_block(tcur, level, &lbp);
3936 error = xfs_btree_check_block(cur, left, level, lbp);
3940 /* Grab the current block number, for future use. */
3941 xfs_btree_get_sibling(tcur, left, &cptr, XFS_BB_RIGHTSIB);
3944 * If left block is full enough so that removing one entry
3945 * won't make it too empty, and right-shifting an entry out
3946 * of left to us works, we're done.
3948 if (xfs_btree_get_numrecs(left) - 1 >=
3949 cur->bc_ops->get_minrecs(tcur, level)) {
3950 error = xfs_btree_rshift(tcur, level, &i);
3954 ASSERT(xfs_btree_get_numrecs(block) >=
3955 cur->bc_ops->get_minrecs(tcur, level));
3956 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
3967 * Otherwise, grab the number of records in right for
3970 lrecs = xfs_btree_get_numrecs(left);
3973 /* Delete the temp cursor, we're done with it. */
3974 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
3977 /* If here, we need to do a join to keep the tree balanced. */
3978 ASSERT(!xfs_btree_ptr_is_null(cur, &cptr));
3980 if (!xfs_btree_ptr_is_null(cur, &lptr) &&
3981 lrecs + xfs_btree_get_numrecs(block) <=
3982 cur->bc_ops->get_maxrecs(cur, level)) {
3984 * Set "right" to be the starting block,
3985 * "left" to be the left neighbor.
3990 error = xfs_btree_read_buf_block(cur, &lptr, 0, &left, &lbp);
3995 * If that won't work, see if we can join with the right neighbor block.
3997 } else if (!xfs_btree_ptr_is_null(cur, &rptr) &&
3998 rrecs + xfs_btree_get_numrecs(block) <=
3999 cur->bc_ops->get_maxrecs(cur, level)) {
4001 * Set "left" to be the starting block,
4002 * "right" to be the right neighbor.
4007 error = xfs_btree_read_buf_block(cur, &rptr, 0, &right, &rbp);
4012 * Otherwise, we can't fix the imbalance.
4013 * Just return. This is probably a logic error, but it's not fatal.
4016 error = xfs_btree_dec_cursor(cur, level, stat);
4022 rrecs = xfs_btree_get_numrecs(right);
4023 lrecs = xfs_btree_get_numrecs(left);
4026 * We're now going to join "left" and "right" by moving all the stuff
4027 * in "right" to "left" and deleting "right".
4029 XFS_BTREE_STATS_ADD(cur, moves, rrecs);
4031 /* It's a non-leaf. Move keys and pointers. */
4032 union xfs_btree_key *lkp; /* left btree key */
4033 union xfs_btree_ptr *lpp; /* left address pointer */
4034 union xfs_btree_key *rkp; /* right btree key */
4035 union xfs_btree_ptr *rpp; /* right address pointer */
4037 lkp = xfs_btree_key_addr(cur, lrecs + 1, left);
4038 lpp = xfs_btree_ptr_addr(cur, lrecs + 1, left);
4039 rkp = xfs_btree_key_addr(cur, 1, right);
4040 rpp = xfs_btree_ptr_addr(cur, 1, right);
4042 for (i = 1; i < rrecs; i++) {
4043 error = xfs_btree_debug_check_ptr(cur, rpp, i, level);
4048 xfs_btree_copy_keys(cur, lkp, rkp, rrecs);
4049 xfs_btree_copy_ptrs(cur, lpp, rpp, rrecs);
4051 xfs_btree_log_keys(cur, lbp, lrecs + 1, lrecs + rrecs);
4052 xfs_btree_log_ptrs(cur, lbp, lrecs + 1, lrecs + rrecs);
4054 /* It's a leaf. Move records. */
4055 union xfs_btree_rec *lrp; /* left record pointer */
4056 union xfs_btree_rec *rrp; /* right record pointer */
4058 lrp = xfs_btree_rec_addr(cur, lrecs + 1, left);
4059 rrp = xfs_btree_rec_addr(cur, 1, right);
4061 xfs_btree_copy_recs(cur, lrp, rrp, rrecs);
4062 xfs_btree_log_recs(cur, lbp, lrecs + 1, lrecs + rrecs);
4065 XFS_BTREE_STATS_INC(cur, join);
4068 * Fix up the number of records and right block pointer in the
4069 * surviving block, and log it.
4071 xfs_btree_set_numrecs(left, lrecs + rrecs);
4072 xfs_btree_get_sibling(cur, right, &cptr, XFS_BB_RIGHTSIB);
4073 xfs_btree_set_sibling(cur, left, &cptr, XFS_BB_RIGHTSIB);
4074 xfs_btree_log_block(cur, lbp, XFS_BB_NUMRECS | XFS_BB_RIGHTSIB);
4076 /* If there is a right sibling, point it to the remaining block. */
4077 xfs_btree_get_sibling(cur, left, &cptr, XFS_BB_RIGHTSIB);
4078 if (!xfs_btree_ptr_is_null(cur, &cptr)) {
4079 error = xfs_btree_read_buf_block(cur, &cptr, 0, &rrblock, &rrbp);
4082 xfs_btree_set_sibling(cur, rrblock, &lptr, XFS_BB_LEFTSIB);
4083 xfs_btree_log_block(cur, rrbp, XFS_BB_LEFTSIB);
4086 /* Free the deleted block. */
4087 error = xfs_btree_free_block(cur, rbp);
4092 * If we joined with the left neighbor, set the buffer in the
4093 * cursor to the left block, and fix up the index.
4096 cur->bc_bufs[level] = lbp;
4097 cur->bc_ptrs[level] += lrecs;
4098 cur->bc_ra[level] = 0;
4101 * If we joined with the right neighbor and there's a level above
4102 * us, increment the cursor at that level.
4104 else if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) ||
4105 (level + 1 < cur->bc_nlevels)) {
4106 error = xfs_btree_increment(cur, level + 1, &i);
4112 * Readjust the ptr at this level if it's not a leaf, since it's
4113 * still pointing at the deletion point, which makes the cursor
4114 * inconsistent. If this makes the ptr 0, the caller fixes it up.
4115 * We can't use decrement because it would change the next level up.
4118 cur->bc_ptrs[level]--;
4121 * We combined blocks, so we have to update the parent keys if the
4122 * btree supports overlapped intervals. However, bc_ptrs[level + 1]
4123 * points to the old block so that the caller knows which record to
4124 * delete. Therefore, the caller must be savvy enough to call updkeys
4125 * for us if we return stat == 2. The other exit points from this
4126 * function don't require deletions further up the tree, so they can
4127 * call updkeys directly.
4130 /* Return value means the next level up has something to do. */
4136 xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
4141 * Delete the record pointed to by cur.
4142 * The cursor refers to the place where the record was (could be inserted)
4143 * when the operation returns.
4147 struct xfs_btree_cur *cur,
4148 int *stat) /* success/failure */
4150 int error; /* error return value */
4153 bool joined = false;
4156 * Go up the tree, starting at leaf level.
4158 * If 2 is returned then a join was done; go to the next level.
4159 * Otherwise we are done.
4161 for (level = 0, i = 2; i == 2; level++) {
4162 error = xfs_btree_delrec(cur, level, &i);
4170 * If we combined blocks as part of deleting the record, delrec won't
4171 * have updated the parent high keys so we have to do that here.
4173 if (joined && (cur->bc_flags & XFS_BTREE_OVERLAPPING)) {
4174 error = xfs_btree_updkeys_force(cur, 0);
4180 for (level = 1; level < cur->bc_nlevels; level++) {
4181 if (cur->bc_ptrs[level] == 0) {
4182 error = xfs_btree_decrement(cur, level, &i);
4197 * Get the data from the pointed-to record.
4201 struct xfs_btree_cur *cur, /* btree cursor */
4202 union xfs_btree_rec **recp, /* output: btree record */
4203 int *stat) /* output: success/failure */
4205 struct xfs_btree_block *block; /* btree block */
4206 struct xfs_buf *bp; /* buffer pointer */
4207 int ptr; /* record number */
4209 int error; /* error return value */
4212 ptr = cur->bc_ptrs[0];
4213 block = xfs_btree_get_block(cur, 0, &bp);
4216 error = xfs_btree_check_block(cur, block, 0, bp);
4222 * Off the right end or left end, return failure.
4224 if (ptr > xfs_btree_get_numrecs(block) || ptr <= 0) {
4230 * Point to the record and extract its data.
4232 *recp = xfs_btree_rec_addr(cur, ptr, block);
4237 /* Visit a block in a btree. */
4239 xfs_btree_visit_block(
4240 struct xfs_btree_cur *cur,
4242 xfs_btree_visit_blocks_fn fn,
4245 struct xfs_btree_block *block;
4247 union xfs_btree_ptr rptr;
4250 /* do right sibling readahead */
4251 xfs_btree_readahead(cur, level, XFS_BTCUR_RIGHTRA);
4252 block = xfs_btree_get_block(cur, level, &bp);
4254 /* process the block */
4255 error = fn(cur, level, data);
4259 /* now read rh sibling block for next iteration */
4260 xfs_btree_get_sibling(cur, block, &rptr, XFS_BB_RIGHTSIB);
4261 if (xfs_btree_ptr_is_null(cur, &rptr))
4264 return xfs_btree_lookup_get_block(cur, level, &rptr, &block);
4268 /* Visit every block in a btree. */
4270 xfs_btree_visit_blocks(
4271 struct xfs_btree_cur *cur,
4272 xfs_btree_visit_blocks_fn fn,
4276 union xfs_btree_ptr lptr;
4278 struct xfs_btree_block *block = NULL;
4281 cur->bc_ops->init_ptr_from_cur(cur, &lptr);
4283 /* for each level */
4284 for (level = cur->bc_nlevels - 1; level >= 0; level--) {
4285 /* grab the left hand block */
4286 error = xfs_btree_lookup_get_block(cur, level, &lptr, &block);
4290 /* readahead the left most block for the next level down */
4292 union xfs_btree_ptr *ptr;
4294 ptr = xfs_btree_ptr_addr(cur, 1, block);
4295 xfs_btree_readahead_ptr(cur, ptr, 1);
4297 /* save for the next iteration of the loop */
4298 xfs_btree_copy_ptrs(cur, &lptr, ptr, 1);
4300 if (!(flags & XFS_BTREE_VISIT_LEAVES))
4302 } else if (!(flags & XFS_BTREE_VISIT_RECORDS)) {
4306 /* for each buffer in the level */
4308 error = xfs_btree_visit_block(cur, level, fn, data);
4311 if (error != -ENOENT)
4319 * Change the owner of a btree.
4321 * The mechanism we use here is ordered buffer logging. Because we don't know
4322 * how many buffers were are going to need to modify, we don't really want to
4323 * have to make transaction reservations for the worst case of every buffer in a
4324 * full size btree as that may be more space that we can fit in the log....
4326 * We do the btree walk in the most optimal manner possible - we have sibling
4327 * pointers so we can just walk all the blocks on each level from left to right
4328 * in a single pass, and then move to the next level and do the same. We can
4329 * also do readahead on the sibling pointers to get IO moving more quickly,
4330 * though for slow disks this is unlikely to make much difference to performance
4331 * as the amount of CPU work we have to do before moving to the next block is
4334 * For each btree block that we load, modify the owner appropriately, set the
4335 * buffer as an ordered buffer and log it appropriately. We need to ensure that
4336 * we mark the region we change dirty so that if the buffer is relogged in
4337 * a subsequent transaction the changes we make here as an ordered buffer are
4338 * correctly relogged in that transaction. If we are in recovery context, then
4339 * just queue the modified buffer as delayed write buffer so the transaction
4340 * recovery completion writes the changes to disk.
4342 struct xfs_btree_block_change_owner_info {
4344 struct list_head *buffer_list;
4348 xfs_btree_block_change_owner(
4349 struct xfs_btree_cur *cur,
4353 struct xfs_btree_block_change_owner_info *bbcoi = data;
4354 struct xfs_btree_block *block;
4357 /* modify the owner */
4358 block = xfs_btree_get_block(cur, level, &bp);
4359 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
4360 if (block->bb_u.l.bb_owner == cpu_to_be64(bbcoi->new_owner))
4362 block->bb_u.l.bb_owner = cpu_to_be64(bbcoi->new_owner);
4364 if (block->bb_u.s.bb_owner == cpu_to_be32(bbcoi->new_owner))
4366 block->bb_u.s.bb_owner = cpu_to_be32(bbcoi->new_owner);
4370 * If the block is a root block hosted in an inode, we might not have a
4371 * buffer pointer here and we shouldn't attempt to log the change as the
4372 * information is already held in the inode and discarded when the root
4373 * block is formatted into the on-disk inode fork. We still change it,
4374 * though, so everything is consistent in memory.
4377 ASSERT(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE);
4378 ASSERT(level == cur->bc_nlevels - 1);
4383 if (!xfs_trans_ordered_buf(cur->bc_tp, bp)) {
4384 xfs_btree_log_block(cur, bp, XFS_BB_OWNER);
4388 xfs_buf_delwri_queue(bp, bbcoi->buffer_list);
4395 xfs_btree_change_owner(
4396 struct xfs_btree_cur *cur,
4398 struct list_head *buffer_list)
4400 struct xfs_btree_block_change_owner_info bbcoi;
4402 bbcoi.new_owner = new_owner;
4403 bbcoi.buffer_list = buffer_list;
4405 return xfs_btree_visit_blocks(cur, xfs_btree_block_change_owner,
4406 XFS_BTREE_VISIT_ALL, &bbcoi);
4409 /* Verify the v5 fields of a long-format btree block. */
4411 xfs_btree_lblock_v5hdr_verify(
4415 struct xfs_mount *mp = bp->b_mount;
4416 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
4418 if (!xfs_sb_version_hascrc(&mp->m_sb))
4419 return __this_address;
4420 if (!uuid_equal(&block->bb_u.l.bb_uuid, &mp->m_sb.sb_meta_uuid))
4421 return __this_address;
4422 if (block->bb_u.l.bb_blkno != cpu_to_be64(bp->b_bn))
4423 return __this_address;
4424 if (owner != XFS_RMAP_OWN_UNKNOWN &&
4425 be64_to_cpu(block->bb_u.l.bb_owner) != owner)
4426 return __this_address;
4430 /* Verify a long-format btree block. */
4432 xfs_btree_lblock_verify(
4434 unsigned int max_recs)
4436 struct xfs_mount *mp = bp->b_mount;
4437 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
4439 /* numrecs verification */
4440 if (be16_to_cpu(block->bb_numrecs) > max_recs)
4441 return __this_address;
4443 /* sibling pointer verification */
4444 if (block->bb_u.l.bb_leftsib != cpu_to_be64(NULLFSBLOCK) &&
4445 !xfs_verify_fsbno(mp, be64_to_cpu(block->bb_u.l.bb_leftsib)))
4446 return __this_address;
4447 if (block->bb_u.l.bb_rightsib != cpu_to_be64(NULLFSBLOCK) &&
4448 !xfs_verify_fsbno(mp, be64_to_cpu(block->bb_u.l.bb_rightsib)))
4449 return __this_address;
4455 * xfs_btree_sblock_v5hdr_verify() -- verify the v5 fields of a short-format
4458 * @bp: buffer containing the btree block
4461 xfs_btree_sblock_v5hdr_verify(
4464 struct xfs_mount *mp = bp->b_mount;
4465 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
4466 struct xfs_perag *pag = bp->b_pag;
4468 if (!xfs_sb_version_hascrc(&mp->m_sb))
4469 return __this_address;
4470 if (!uuid_equal(&block->bb_u.s.bb_uuid, &mp->m_sb.sb_meta_uuid))
4471 return __this_address;
4472 if (block->bb_u.s.bb_blkno != cpu_to_be64(bp->b_bn))
4473 return __this_address;
4474 if (pag && be32_to_cpu(block->bb_u.s.bb_owner) != pag->pag_agno)
4475 return __this_address;
4480 * xfs_btree_sblock_verify() -- verify a short-format btree block
4482 * @bp: buffer containing the btree block
4483 * @max_recs: maximum records allowed in this btree node
4486 xfs_btree_sblock_verify(
4488 unsigned int max_recs)
4490 struct xfs_mount *mp = bp->b_mount;
4491 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
4494 /* numrecs verification */
4495 if (be16_to_cpu(block->bb_numrecs) > max_recs)
4496 return __this_address;
4498 /* sibling pointer verification */
4499 agno = xfs_daddr_to_agno(mp, XFS_BUF_ADDR(bp));
4500 if (block->bb_u.s.bb_leftsib != cpu_to_be32(NULLAGBLOCK) &&
4501 !xfs_verify_agbno(mp, agno, be32_to_cpu(block->bb_u.s.bb_leftsib)))
4502 return __this_address;
4503 if (block->bb_u.s.bb_rightsib != cpu_to_be32(NULLAGBLOCK) &&
4504 !xfs_verify_agbno(mp, agno, be32_to_cpu(block->bb_u.s.bb_rightsib)))
4505 return __this_address;
4511 * Calculate the number of btree levels needed to store a given number of
4512 * records in a short-format btree.
4515 xfs_btree_compute_maxlevels(
4520 unsigned long maxblocks;
4522 maxblocks = (len + limits[0] - 1) / limits[0];
4523 for (level = 1; maxblocks > 1; level++)
4524 maxblocks = (maxblocks + limits[1] - 1) / limits[1];
4529 * Query a regular btree for all records overlapping a given interval.
4530 * Start with a LE lookup of the key of low_rec and return all records
4531 * until we find a record with a key greater than the key of high_rec.
4534 xfs_btree_simple_query_range(
4535 struct xfs_btree_cur *cur,
4536 union xfs_btree_key *low_key,
4537 union xfs_btree_key *high_key,
4538 xfs_btree_query_range_fn fn,
4541 union xfs_btree_rec *recp;
4542 union xfs_btree_key rec_key;
4545 bool firstrec = true;
4548 ASSERT(cur->bc_ops->init_high_key_from_rec);
4549 ASSERT(cur->bc_ops->diff_two_keys);
4552 * Find the leftmost record. The btree cursor must be set
4553 * to the low record used to generate low_key.
4556 error = xfs_btree_lookup(cur, XFS_LOOKUP_LE, &stat);
4560 /* Nothing? See if there's anything to the right. */
4562 error = xfs_btree_increment(cur, 0, &stat);
4568 /* Find the record. */
4569 error = xfs_btree_get_rec(cur, &recp, &stat);
4573 /* Skip if high_key(rec) < low_key. */
4575 cur->bc_ops->init_high_key_from_rec(&rec_key, recp);
4577 diff = cur->bc_ops->diff_two_keys(cur, low_key,
4583 /* Stop if high_key < low_key(rec). */
4584 cur->bc_ops->init_key_from_rec(&rec_key, recp);
4585 diff = cur->bc_ops->diff_two_keys(cur, &rec_key, high_key);
4590 error = fn(cur, recp, priv);
4595 /* Move on to the next record. */
4596 error = xfs_btree_increment(cur, 0, &stat);
4606 * Query an overlapped interval btree for all records overlapping a given
4607 * interval. This function roughly follows the algorithm given in
4608 * "Interval Trees" of _Introduction to Algorithms_, which is section
4609 * 14.3 in the 2nd and 3rd editions.
4611 * First, generate keys for the low and high records passed in.
4613 * For any leaf node, generate the high and low keys for the record.
4614 * If the record keys overlap with the query low/high keys, pass the
4615 * record to the function iterator.
4617 * For any internal node, compare the low and high keys of each
4618 * pointer against the query low/high keys. If there's an overlap,
4619 * follow the pointer.
4621 * As an optimization, we stop scanning a block when we find a low key
4622 * that is greater than the query's high key.
4625 xfs_btree_overlapped_query_range(
4626 struct xfs_btree_cur *cur,
4627 union xfs_btree_key *low_key,
4628 union xfs_btree_key *high_key,
4629 xfs_btree_query_range_fn fn,
4632 union xfs_btree_ptr ptr;
4633 union xfs_btree_ptr *pp;
4634 union xfs_btree_key rec_key;
4635 union xfs_btree_key rec_hkey;
4636 union xfs_btree_key *lkp;
4637 union xfs_btree_key *hkp;
4638 union xfs_btree_rec *recp;
4639 struct xfs_btree_block *block;
4647 /* Load the root of the btree. */
4648 level = cur->bc_nlevels - 1;
4649 cur->bc_ops->init_ptr_from_cur(cur, &ptr);
4650 error = xfs_btree_lookup_get_block(cur, level, &ptr, &block);
4653 xfs_btree_get_block(cur, level, &bp);
4654 trace_xfs_btree_overlapped_query_range(cur, level, bp);
4656 error = xfs_btree_check_block(cur, block, level, bp);
4660 cur->bc_ptrs[level] = 1;
4662 while (level < cur->bc_nlevels) {
4663 block = xfs_btree_get_block(cur, level, &bp);
4665 /* End of node, pop back towards the root. */
4666 if (cur->bc_ptrs[level] > be16_to_cpu(block->bb_numrecs)) {
4668 if (level < cur->bc_nlevels - 1)
4669 cur->bc_ptrs[level + 1]++;
4675 /* Handle a leaf node. */
4676 recp = xfs_btree_rec_addr(cur, cur->bc_ptrs[0], block);
4678 cur->bc_ops->init_high_key_from_rec(&rec_hkey, recp);
4679 ldiff = cur->bc_ops->diff_two_keys(cur, &rec_hkey,
4682 cur->bc_ops->init_key_from_rec(&rec_key, recp);
4683 hdiff = cur->bc_ops->diff_two_keys(cur, high_key,
4687 * If (record's high key >= query's low key) and
4688 * (query's high key >= record's low key), then
4689 * this record overlaps the query range; callback.
4691 if (ldiff >= 0 && hdiff >= 0) {
4692 error = fn(cur, recp, priv);
4695 } else if (hdiff < 0) {
4696 /* Record is larger than high key; pop. */
4699 cur->bc_ptrs[level]++;
4703 /* Handle an internal node. */
4704 lkp = xfs_btree_key_addr(cur, cur->bc_ptrs[level], block);
4705 hkp = xfs_btree_high_key_addr(cur, cur->bc_ptrs[level], block);
4706 pp = xfs_btree_ptr_addr(cur, cur->bc_ptrs[level], block);
4708 ldiff = cur->bc_ops->diff_two_keys(cur, hkp, low_key);
4709 hdiff = cur->bc_ops->diff_two_keys(cur, high_key, lkp);
4712 * If (pointer's high key >= query's low key) and
4713 * (query's high key >= pointer's low key), then
4714 * this record overlaps the query range; follow pointer.
4716 if (ldiff >= 0 && hdiff >= 0) {
4718 error = xfs_btree_lookup_get_block(cur, level, pp,
4722 xfs_btree_get_block(cur, level, &bp);
4723 trace_xfs_btree_overlapped_query_range(cur, level, bp);
4725 error = xfs_btree_check_block(cur, block, level, bp);
4729 cur->bc_ptrs[level] = 1;
4731 } else if (hdiff < 0) {
4732 /* The low key is larger than the upper range; pop. */
4735 cur->bc_ptrs[level]++;
4740 * If we don't end this function with the cursor pointing at a record
4741 * block, a subsequent non-error cursor deletion will not release
4742 * node-level buffers, causing a buffer leak. This is quite possible
4743 * with a zero-results range query, so release the buffers if we
4744 * failed to return any results.
4746 if (cur->bc_bufs[0] == NULL) {
4747 for (i = 0; i < cur->bc_nlevels; i++) {
4748 if (cur->bc_bufs[i]) {
4749 xfs_trans_brelse(cur->bc_tp, cur->bc_bufs[i]);
4750 cur->bc_bufs[i] = NULL;
4751 cur->bc_ptrs[i] = 0;
4761 * Query a btree for all records overlapping a given interval of keys. The
4762 * supplied function will be called with each record found; return one of the
4763 * XFS_BTREE_QUERY_RANGE_{CONTINUE,ABORT} values or the usual negative error
4764 * code. This function returns -ECANCELED, zero, or a negative error code.
4767 xfs_btree_query_range(
4768 struct xfs_btree_cur *cur,
4769 union xfs_btree_irec *low_rec,
4770 union xfs_btree_irec *high_rec,
4771 xfs_btree_query_range_fn fn,
4774 union xfs_btree_rec rec;
4775 union xfs_btree_key low_key;
4776 union xfs_btree_key high_key;
4778 /* Find the keys of both ends of the interval. */
4779 cur->bc_rec = *high_rec;
4780 cur->bc_ops->init_rec_from_cur(cur, &rec);
4781 cur->bc_ops->init_key_from_rec(&high_key, &rec);
4783 cur->bc_rec = *low_rec;
4784 cur->bc_ops->init_rec_from_cur(cur, &rec);
4785 cur->bc_ops->init_key_from_rec(&low_key, &rec);
4787 /* Enforce low key < high key. */
4788 if (cur->bc_ops->diff_two_keys(cur, &low_key, &high_key) > 0)
4791 if (!(cur->bc_flags & XFS_BTREE_OVERLAPPING))
4792 return xfs_btree_simple_query_range(cur, &low_key,
4793 &high_key, fn, priv);
4794 return xfs_btree_overlapped_query_range(cur, &low_key, &high_key,
4798 /* Query a btree for all records. */
4800 xfs_btree_query_all(
4801 struct xfs_btree_cur *cur,
4802 xfs_btree_query_range_fn fn,
4805 union xfs_btree_key low_key;
4806 union xfs_btree_key high_key;
4808 memset(&cur->bc_rec, 0, sizeof(cur->bc_rec));
4809 memset(&low_key, 0, sizeof(low_key));
4810 memset(&high_key, 0xFF, sizeof(high_key));
4812 return xfs_btree_simple_query_range(cur, &low_key, &high_key, fn, priv);
4816 * Calculate the number of blocks needed to store a given number of records
4817 * in a short-format (per-AG metadata) btree.
4820 xfs_btree_calc_size(
4822 unsigned long long len)
4826 unsigned long long rval;
4828 maxrecs = limits[0];
4829 for (level = 0, rval = 0; len > 1; level++) {
4831 do_div(len, maxrecs);
4832 maxrecs = limits[1];
4839 xfs_btree_count_blocks_helper(
4840 struct xfs_btree_cur *cur,
4844 xfs_extlen_t *blocks = data;
4850 /* Count the blocks in a btree and return the result in *blocks. */
4852 xfs_btree_count_blocks(
4853 struct xfs_btree_cur *cur,
4854 xfs_extlen_t *blocks)
4857 return xfs_btree_visit_blocks(cur, xfs_btree_count_blocks_helper,
4858 XFS_BTREE_VISIT_ALL, blocks);
4861 /* Compare two btree pointers. */
4863 xfs_btree_diff_two_ptrs(
4864 struct xfs_btree_cur *cur,
4865 const union xfs_btree_ptr *a,
4866 const union xfs_btree_ptr *b)
4868 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
4869 return (int64_t)be64_to_cpu(a->l) - be64_to_cpu(b->l);
4870 return (int64_t)be32_to_cpu(a->s) - be32_to_cpu(b->s);
4873 /* If there's an extent, we're done. */
4875 xfs_btree_has_record_helper(
4876 struct xfs_btree_cur *cur,
4877 union xfs_btree_rec *rec,
4883 /* Is there a record covering a given range of keys? */
4885 xfs_btree_has_record(
4886 struct xfs_btree_cur *cur,
4887 union xfs_btree_irec *low,
4888 union xfs_btree_irec *high,
4893 error = xfs_btree_query_range(cur, low, high,
4894 &xfs_btree_has_record_helper, NULL);
4895 if (error == -ECANCELED) {
4903 /* Are there more records in this btree? */
4905 xfs_btree_has_more_records(
4906 struct xfs_btree_cur *cur)
4908 struct xfs_btree_block *block;
4911 block = xfs_btree_get_block(cur, 0, &bp);
4913 /* There are still records in this block. */
4914 if (cur->bc_ptrs[0] < xfs_btree_get_numrecs(block))
4917 /* There are more record blocks. */
4918 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
4919 return block->bb_u.l.bb_rightsib != cpu_to_be64(NULLFSBLOCK);
4921 return block->bb_u.s.bb_rightsib != cpu_to_be32(NULLAGBLOCK);