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 = PF_MEMALLOC_NOFS;
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);
2821 args->result = __xfs_btree_split(args->cur, args->level, args->ptrp,
2822 args->key, args->curp, args->stat);
2823 complete(args->done);
2825 current_restore_flags_nested(&pflags, new_pflags);
2829 * BMBT split requests often come in with little stack to work on. Push
2830 * them off to a worker thread so there is lots of stack to use. For the other
2831 * btree types, just call directly to avoid the context switch overhead here.
2833 STATIC int /* error */
2835 struct xfs_btree_cur *cur,
2837 union xfs_btree_ptr *ptrp,
2838 union xfs_btree_key *key,
2839 struct xfs_btree_cur **curp,
2840 int *stat) /* success/failure */
2842 struct xfs_btree_split_args args;
2843 DECLARE_COMPLETION_ONSTACK(done);
2845 if (cur->bc_btnum != XFS_BTNUM_BMAP)
2846 return __xfs_btree_split(cur, level, ptrp, key, curp, stat);
2855 args.kswapd = current_is_kswapd();
2856 INIT_WORK_ONSTACK(&args.work, xfs_btree_split_worker);
2857 queue_work(xfs_alloc_wq, &args.work);
2858 wait_for_completion(&done);
2859 destroy_work_on_stack(&args.work);
2865 * Copy the old inode root contents into a real block and make the
2866 * broot point to it.
2869 xfs_btree_new_iroot(
2870 struct xfs_btree_cur *cur, /* btree cursor */
2871 int *logflags, /* logging flags for inode */
2872 int *stat) /* return status - 0 fail */
2874 struct xfs_buf *cbp; /* buffer for cblock */
2875 struct xfs_btree_block *block; /* btree block */
2876 struct xfs_btree_block *cblock; /* child btree block */
2877 union xfs_btree_key *ckp; /* child key pointer */
2878 union xfs_btree_ptr *cpp; /* child ptr pointer */
2879 union xfs_btree_key *kp; /* pointer to btree key */
2880 union xfs_btree_ptr *pp; /* pointer to block addr */
2881 union xfs_btree_ptr nptr; /* new block addr */
2882 int level; /* btree level */
2883 int error; /* error return code */
2884 int i; /* loop counter */
2886 XFS_BTREE_STATS_INC(cur, newroot);
2888 ASSERT(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE);
2890 level = cur->bc_nlevels - 1;
2892 block = xfs_btree_get_iroot(cur);
2893 pp = xfs_btree_ptr_addr(cur, 1, block);
2895 /* Allocate the new block. If we can't do it, we're toast. Give up. */
2896 error = cur->bc_ops->alloc_block(cur, pp, &nptr, stat);
2902 XFS_BTREE_STATS_INC(cur, alloc);
2904 /* Copy the root into a real block. */
2905 error = xfs_btree_get_buf_block(cur, &nptr, &cblock, &cbp);
2910 * we can't just memcpy() the root in for CRC enabled btree blocks.
2911 * In that case have to also ensure the blkno remains correct
2913 memcpy(cblock, block, xfs_btree_block_len(cur));
2914 if (cur->bc_flags & XFS_BTREE_CRC_BLOCKS) {
2915 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
2916 cblock->bb_u.l.bb_blkno = cpu_to_be64(cbp->b_bn);
2918 cblock->bb_u.s.bb_blkno = cpu_to_be64(cbp->b_bn);
2921 be16_add_cpu(&block->bb_level, 1);
2922 xfs_btree_set_numrecs(block, 1);
2924 cur->bc_ptrs[level + 1] = 1;
2926 kp = xfs_btree_key_addr(cur, 1, block);
2927 ckp = xfs_btree_key_addr(cur, 1, cblock);
2928 xfs_btree_copy_keys(cur, ckp, kp, xfs_btree_get_numrecs(cblock));
2930 cpp = xfs_btree_ptr_addr(cur, 1, cblock);
2931 for (i = 0; i < be16_to_cpu(cblock->bb_numrecs); i++) {
2932 error = xfs_btree_debug_check_ptr(cur, pp, i, level);
2937 xfs_btree_copy_ptrs(cur, cpp, pp, xfs_btree_get_numrecs(cblock));
2939 error = xfs_btree_debug_check_ptr(cur, &nptr, 0, level);
2943 xfs_btree_copy_ptrs(cur, pp, &nptr, 1);
2945 xfs_iroot_realloc(cur->bc_ino.ip,
2946 1 - xfs_btree_get_numrecs(cblock),
2947 cur->bc_ino.whichfork);
2949 xfs_btree_setbuf(cur, level, cbp);
2952 * Do all this logging at the end so that
2953 * the root is at the right level.
2955 xfs_btree_log_block(cur, cbp, XFS_BB_ALL_BITS);
2956 xfs_btree_log_keys(cur, cbp, 1, be16_to_cpu(cblock->bb_numrecs));
2957 xfs_btree_log_ptrs(cur, cbp, 1, be16_to_cpu(cblock->bb_numrecs));
2960 XFS_ILOG_CORE | xfs_ilog_fbroot(cur->bc_ino.whichfork);
2968 * Allocate a new root block, fill it in.
2970 STATIC int /* error */
2972 struct xfs_btree_cur *cur, /* btree cursor */
2973 int *stat) /* success/failure */
2975 struct xfs_btree_block *block; /* one half of the old root block */
2976 struct xfs_buf *bp; /* buffer containing block */
2977 int error; /* error return value */
2978 struct xfs_buf *lbp; /* left buffer pointer */
2979 struct xfs_btree_block *left; /* left btree block */
2980 struct xfs_buf *nbp; /* new (root) buffer */
2981 struct xfs_btree_block *new; /* new (root) btree block */
2982 int nptr; /* new value for key index, 1 or 2 */
2983 struct xfs_buf *rbp; /* right buffer pointer */
2984 struct xfs_btree_block *right; /* right btree block */
2985 union xfs_btree_ptr rptr;
2986 union xfs_btree_ptr lptr;
2988 XFS_BTREE_STATS_INC(cur, newroot);
2990 /* initialise our start point from the cursor */
2991 cur->bc_ops->init_ptr_from_cur(cur, &rptr);
2993 /* Allocate the new block. If we can't do it, we're toast. Give up. */
2994 error = cur->bc_ops->alloc_block(cur, &rptr, &lptr, stat);
2999 XFS_BTREE_STATS_INC(cur, alloc);
3001 /* Set up the new block. */
3002 error = xfs_btree_get_buf_block(cur, &lptr, &new, &nbp);
3006 /* Set the root in the holding structure increasing the level by 1. */
3007 cur->bc_ops->set_root(cur, &lptr, 1);
3010 * At the previous root level there are now two blocks: the old root,
3011 * and the new block generated when it was split. We don't know which
3012 * one the cursor is pointing at, so we set up variables "left" and
3013 * "right" for each case.
3015 block = xfs_btree_get_block(cur, cur->bc_nlevels - 1, &bp);
3018 error = xfs_btree_check_block(cur, block, cur->bc_nlevels - 1, bp);
3023 xfs_btree_get_sibling(cur, block, &rptr, XFS_BB_RIGHTSIB);
3024 if (!xfs_btree_ptr_is_null(cur, &rptr)) {
3025 /* Our block is left, pick up the right block. */
3027 xfs_btree_buf_to_ptr(cur, lbp, &lptr);
3029 error = xfs_btree_read_buf_block(cur, &rptr, 0, &right, &rbp);
3035 /* Our block is right, pick up the left block. */
3037 xfs_btree_buf_to_ptr(cur, rbp, &rptr);
3039 xfs_btree_get_sibling(cur, right, &lptr, XFS_BB_LEFTSIB);
3040 error = xfs_btree_read_buf_block(cur, &lptr, 0, &left, &lbp);
3047 /* Fill in the new block's btree header and log it. */
3048 xfs_btree_init_block_cur(cur, nbp, cur->bc_nlevels, 2);
3049 xfs_btree_log_block(cur, nbp, XFS_BB_ALL_BITS);
3050 ASSERT(!xfs_btree_ptr_is_null(cur, &lptr) &&
3051 !xfs_btree_ptr_is_null(cur, &rptr));
3053 /* Fill in the key data in the new root. */
3054 if (xfs_btree_get_level(left) > 0) {
3056 * Get the keys for the left block's keys and put them directly
3057 * in the parent block. Do the same for the right block.
3059 xfs_btree_get_node_keys(cur, left,
3060 xfs_btree_key_addr(cur, 1, new));
3061 xfs_btree_get_node_keys(cur, right,
3062 xfs_btree_key_addr(cur, 2, new));
3065 * Get the keys for the left block's records and put them
3066 * directly in the parent block. Do the same for the right
3069 xfs_btree_get_leaf_keys(cur, left,
3070 xfs_btree_key_addr(cur, 1, new));
3071 xfs_btree_get_leaf_keys(cur, right,
3072 xfs_btree_key_addr(cur, 2, new));
3074 xfs_btree_log_keys(cur, nbp, 1, 2);
3076 /* Fill in the pointer data in the new root. */
3077 xfs_btree_copy_ptrs(cur,
3078 xfs_btree_ptr_addr(cur, 1, new), &lptr, 1);
3079 xfs_btree_copy_ptrs(cur,
3080 xfs_btree_ptr_addr(cur, 2, new), &rptr, 1);
3081 xfs_btree_log_ptrs(cur, nbp, 1, 2);
3083 /* Fix up the cursor. */
3084 xfs_btree_setbuf(cur, cur->bc_nlevels, nbp);
3085 cur->bc_ptrs[cur->bc_nlevels] = nptr;
3097 xfs_btree_make_block_unfull(
3098 struct xfs_btree_cur *cur, /* btree cursor */
3099 int level, /* btree level */
3100 int numrecs,/* # of recs in block */
3101 int *oindex,/* old tree index */
3102 int *index, /* new tree index */
3103 union xfs_btree_ptr *nptr, /* new btree ptr */
3104 struct xfs_btree_cur **ncur, /* new btree cursor */
3105 union xfs_btree_key *key, /* key of new block */
3110 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
3111 level == cur->bc_nlevels - 1) {
3112 struct xfs_inode *ip = cur->bc_ino.ip;
3114 if (numrecs < cur->bc_ops->get_dmaxrecs(cur, level)) {
3115 /* A root block that can be made bigger. */
3116 xfs_iroot_realloc(ip, 1, cur->bc_ino.whichfork);
3119 /* A root block that needs replacing */
3122 error = xfs_btree_new_iroot(cur, &logflags, stat);
3123 if (error || *stat == 0)
3126 xfs_trans_log_inode(cur->bc_tp, ip, logflags);
3132 /* First, try shifting an entry to the right neighbor. */
3133 error = xfs_btree_rshift(cur, level, stat);
3137 /* Next, try shifting an entry to the left neighbor. */
3138 error = xfs_btree_lshift(cur, level, stat);
3143 *oindex = *index = cur->bc_ptrs[level];
3148 * Next, try splitting the current block in half.
3150 * If this works we have to re-set our variables because we
3151 * could be in a different block now.
3153 error = xfs_btree_split(cur, level, nptr, key, ncur, stat);
3154 if (error || *stat == 0)
3158 *index = cur->bc_ptrs[level];
3163 * Insert one record/level. Return information to the caller
3164 * allowing the next level up to proceed if necessary.
3168 struct xfs_btree_cur *cur, /* btree cursor */
3169 int level, /* level to insert record at */
3170 union xfs_btree_ptr *ptrp, /* i/o: block number inserted */
3171 union xfs_btree_rec *rec, /* record to insert */
3172 union xfs_btree_key *key, /* i/o: block key for ptrp */
3173 struct xfs_btree_cur **curp, /* output: new cursor replacing cur */
3174 int *stat) /* success/failure */
3176 struct xfs_btree_block *block; /* btree block */
3177 struct xfs_buf *bp; /* buffer for block */
3178 union xfs_btree_ptr nptr; /* new block ptr */
3179 struct xfs_btree_cur *ncur; /* new btree cursor */
3180 union xfs_btree_key nkey; /* new block key */
3181 union xfs_btree_key *lkey;
3182 int optr; /* old key/record index */
3183 int ptr; /* key/record index */
3184 int numrecs;/* number of records */
3185 int error; /* error return value */
3193 * If we have an external root pointer, and we've made it to the
3194 * root level, allocate a new root block and we're done.
3196 if (!(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
3197 (level >= cur->bc_nlevels)) {
3198 error = xfs_btree_new_root(cur, stat);
3199 xfs_btree_set_ptr_null(cur, ptrp);
3204 /* If we're off the left edge, return failure. */
3205 ptr = cur->bc_ptrs[level];
3213 XFS_BTREE_STATS_INC(cur, insrec);
3215 /* Get pointers to the btree buffer and block. */
3216 block = xfs_btree_get_block(cur, level, &bp);
3217 old_bn = bp ? bp->b_bn : XFS_BUF_DADDR_NULL;
3218 numrecs = xfs_btree_get_numrecs(block);
3221 error = xfs_btree_check_block(cur, block, level, bp);
3225 /* Check that the new entry is being inserted in the right place. */
3226 if (ptr <= numrecs) {
3228 ASSERT(cur->bc_ops->recs_inorder(cur, rec,
3229 xfs_btree_rec_addr(cur, ptr, block)));
3231 ASSERT(cur->bc_ops->keys_inorder(cur, key,
3232 xfs_btree_key_addr(cur, ptr, block)));
3238 * If the block is full, we can't insert the new entry until we
3239 * make the block un-full.
3241 xfs_btree_set_ptr_null(cur, &nptr);
3242 if (numrecs == cur->bc_ops->get_maxrecs(cur, level)) {
3243 error = xfs_btree_make_block_unfull(cur, level, numrecs,
3244 &optr, &ptr, &nptr, &ncur, lkey, stat);
3245 if (error || *stat == 0)
3250 * The current block may have changed if the block was
3251 * previously full and we have just made space in it.
3253 block = xfs_btree_get_block(cur, level, &bp);
3254 numrecs = xfs_btree_get_numrecs(block);
3257 error = xfs_btree_check_block(cur, block, level, bp);
3263 * At this point we know there's room for our new entry in the block
3264 * we're pointing at.
3266 XFS_BTREE_STATS_ADD(cur, moves, numrecs - ptr + 1);
3269 /* It's a nonleaf. make a hole in the keys and ptrs */
3270 union xfs_btree_key *kp;
3271 union xfs_btree_ptr *pp;
3273 kp = xfs_btree_key_addr(cur, ptr, block);
3274 pp = xfs_btree_ptr_addr(cur, ptr, block);
3276 for (i = numrecs - ptr; i >= 0; i--) {
3277 error = xfs_btree_debug_check_ptr(cur, pp, i, level);
3282 xfs_btree_shift_keys(cur, kp, 1, numrecs - ptr + 1);
3283 xfs_btree_shift_ptrs(cur, pp, 1, numrecs - ptr + 1);
3285 error = xfs_btree_debug_check_ptr(cur, ptrp, 0, level);
3289 /* Now put the new data in, bump numrecs and log it. */
3290 xfs_btree_copy_keys(cur, kp, key, 1);
3291 xfs_btree_copy_ptrs(cur, pp, ptrp, 1);
3293 xfs_btree_set_numrecs(block, numrecs);
3294 xfs_btree_log_ptrs(cur, bp, ptr, numrecs);
3295 xfs_btree_log_keys(cur, bp, ptr, numrecs);
3297 if (ptr < numrecs) {
3298 ASSERT(cur->bc_ops->keys_inorder(cur, kp,
3299 xfs_btree_key_addr(cur, ptr + 1, block)));
3303 /* It's a leaf. make a hole in the records */
3304 union xfs_btree_rec *rp;
3306 rp = xfs_btree_rec_addr(cur, ptr, block);
3308 xfs_btree_shift_recs(cur, rp, 1, numrecs - ptr + 1);
3310 /* Now put the new data in, bump numrecs and log it. */
3311 xfs_btree_copy_recs(cur, rp, rec, 1);
3312 xfs_btree_set_numrecs(block, ++numrecs);
3313 xfs_btree_log_recs(cur, bp, ptr, numrecs);
3315 if (ptr < numrecs) {
3316 ASSERT(cur->bc_ops->recs_inorder(cur, rp,
3317 xfs_btree_rec_addr(cur, ptr + 1, block)));
3322 /* Log the new number of records in the btree header. */
3323 xfs_btree_log_block(cur, bp, XFS_BB_NUMRECS);
3326 * If we just inserted into a new tree block, we have to
3327 * recalculate nkey here because nkey is out of date.
3329 * Otherwise we're just updating an existing block (having shoved
3330 * some records into the new tree block), so use the regular key
3333 if (bp && bp->b_bn != old_bn) {
3334 xfs_btree_get_keys(cur, block, lkey);
3335 } else if (xfs_btree_needs_key_update(cur, optr)) {
3336 error = xfs_btree_update_keys(cur, level);
3342 * If we are tracking the last record in the tree and
3343 * we are at the far right edge of the tree, update it.
3345 if (xfs_btree_is_lastrec(cur, block, level)) {
3346 cur->bc_ops->update_lastrec(cur, block, rec,
3347 ptr, LASTREC_INSREC);
3351 * Return the new block number, if any.
3352 * If there is one, give back a record value and a cursor too.
3355 if (!xfs_btree_ptr_is_null(cur, &nptr)) {
3356 xfs_btree_copy_keys(cur, key, lkey, 1);
3368 * Insert the record at the point referenced by cur.
3370 * A multi-level split of the tree on insert will invalidate the original
3371 * cursor. All callers of this function should assume that the cursor is
3372 * no longer valid and revalidate it.
3376 struct xfs_btree_cur *cur,
3379 int error; /* error return value */
3380 int i; /* result value, 0 for failure */
3381 int level; /* current level number in btree */
3382 union xfs_btree_ptr nptr; /* new block number (split result) */
3383 struct xfs_btree_cur *ncur; /* new cursor (split result) */
3384 struct xfs_btree_cur *pcur; /* previous level's cursor */
3385 union xfs_btree_key bkey; /* key of block to insert */
3386 union xfs_btree_key *key;
3387 union xfs_btree_rec rec; /* record to insert */
3394 xfs_btree_set_ptr_null(cur, &nptr);
3396 /* Make a key out of the record data to be inserted, and save it. */
3397 cur->bc_ops->init_rec_from_cur(cur, &rec);
3398 cur->bc_ops->init_key_from_rec(key, &rec);
3401 * Loop going up the tree, starting at the leaf level.
3402 * Stop when we don't get a split block, that must mean that
3403 * the insert is finished with this level.
3407 * Insert nrec/nptr into this level of the tree.
3408 * Note if we fail, nptr will be null.
3410 error = xfs_btree_insrec(pcur, level, &nptr, &rec, key,
3414 xfs_btree_del_cursor(pcur, XFS_BTREE_ERROR);
3418 if (XFS_IS_CORRUPT(cur->bc_mp, i != 1)) {
3419 error = -EFSCORRUPTED;
3425 * See if the cursor we just used is trash.
3426 * Can't trash the caller's cursor, but otherwise we should
3427 * if ncur is a new cursor or we're about to be done.
3430 (ncur || xfs_btree_ptr_is_null(cur, &nptr))) {
3431 /* Save the state from the cursor before we trash it */
3432 if (cur->bc_ops->update_cursor)
3433 cur->bc_ops->update_cursor(pcur, cur);
3434 cur->bc_nlevels = pcur->bc_nlevels;
3435 xfs_btree_del_cursor(pcur, XFS_BTREE_NOERROR);
3437 /* If we got a new cursor, switch to it. */
3442 } while (!xfs_btree_ptr_is_null(cur, &nptr));
3451 * Try to merge a non-leaf block back into the inode root.
3453 * Note: the killroot names comes from the fact that we're effectively
3454 * killing the old root block. But because we can't just delete the
3455 * inode we have to copy the single block it was pointing to into the
3459 xfs_btree_kill_iroot(
3460 struct xfs_btree_cur *cur)
3462 int whichfork = cur->bc_ino.whichfork;
3463 struct xfs_inode *ip = cur->bc_ino.ip;
3464 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
3465 struct xfs_btree_block *block;
3466 struct xfs_btree_block *cblock;
3467 union xfs_btree_key *kp;
3468 union xfs_btree_key *ckp;
3469 union xfs_btree_ptr *pp;
3470 union xfs_btree_ptr *cpp;
3471 struct xfs_buf *cbp;
3477 union xfs_btree_ptr ptr;
3481 ASSERT(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE);
3482 ASSERT(cur->bc_nlevels > 1);
3485 * Don't deal with the root block needs to be a leaf case.
3486 * We're just going to turn the thing back into extents anyway.
3488 level = cur->bc_nlevels - 1;
3493 * Give up if the root has multiple children.
3495 block = xfs_btree_get_iroot(cur);
3496 if (xfs_btree_get_numrecs(block) != 1)
3499 cblock = xfs_btree_get_block(cur, level - 1, &cbp);
3500 numrecs = xfs_btree_get_numrecs(cblock);
3503 * Only do this if the next level will fit.
3504 * Then the data must be copied up to the inode,
3505 * instead of freeing the root you free the next level.
3507 if (numrecs > cur->bc_ops->get_dmaxrecs(cur, level))
3510 XFS_BTREE_STATS_INC(cur, killroot);
3513 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_LEFTSIB);
3514 ASSERT(xfs_btree_ptr_is_null(cur, &ptr));
3515 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_RIGHTSIB);
3516 ASSERT(xfs_btree_ptr_is_null(cur, &ptr));
3519 index = numrecs - cur->bc_ops->get_maxrecs(cur, level);
3521 xfs_iroot_realloc(cur->bc_ino.ip, index,
3522 cur->bc_ino.whichfork);
3523 block = ifp->if_broot;
3526 be16_add_cpu(&block->bb_numrecs, index);
3527 ASSERT(block->bb_numrecs == cblock->bb_numrecs);
3529 kp = xfs_btree_key_addr(cur, 1, block);
3530 ckp = xfs_btree_key_addr(cur, 1, cblock);
3531 xfs_btree_copy_keys(cur, kp, ckp, numrecs);
3533 pp = xfs_btree_ptr_addr(cur, 1, block);
3534 cpp = xfs_btree_ptr_addr(cur, 1, cblock);
3536 for (i = 0; i < numrecs; i++) {
3537 error = xfs_btree_debug_check_ptr(cur, cpp, i, level - 1);
3542 xfs_btree_copy_ptrs(cur, pp, cpp, numrecs);
3544 error = xfs_btree_free_block(cur, cbp);
3548 cur->bc_bufs[level - 1] = NULL;
3549 be16_add_cpu(&block->bb_level, -1);
3550 xfs_trans_log_inode(cur->bc_tp, ip,
3551 XFS_ILOG_CORE | xfs_ilog_fbroot(cur->bc_ino.whichfork));
3558 * Kill the current root node, and replace it with it's only child node.
3561 xfs_btree_kill_root(
3562 struct xfs_btree_cur *cur,
3565 union xfs_btree_ptr *newroot)
3569 XFS_BTREE_STATS_INC(cur, killroot);
3572 * Update the root pointer, decreasing the level by 1 and then
3573 * free the old root.
3575 cur->bc_ops->set_root(cur, newroot, -1);
3577 error = xfs_btree_free_block(cur, bp);
3581 cur->bc_bufs[level] = NULL;
3582 cur->bc_ra[level] = 0;
3589 xfs_btree_dec_cursor(
3590 struct xfs_btree_cur *cur,
3598 error = xfs_btree_decrement(cur, level, &i);
3608 * Single level of the btree record deletion routine.
3609 * Delete record pointed to by cur/level.
3610 * Remove the record from its block then rebalance the tree.
3611 * Return 0 for error, 1 for done, 2 to go on to the next level.
3613 STATIC int /* error */
3615 struct xfs_btree_cur *cur, /* btree cursor */
3616 int level, /* level removing record from */
3617 int *stat) /* fail/done/go-on */
3619 struct xfs_btree_block *block; /* btree block */
3620 union xfs_btree_ptr cptr; /* current block ptr */
3621 struct xfs_buf *bp; /* buffer for block */
3622 int error; /* error return value */
3623 int i; /* loop counter */
3624 union xfs_btree_ptr lptr; /* left sibling block ptr */
3625 struct xfs_buf *lbp; /* left buffer pointer */
3626 struct xfs_btree_block *left; /* left btree block */
3627 int lrecs = 0; /* left record count */
3628 int ptr; /* key/record index */
3629 union xfs_btree_ptr rptr; /* right sibling block ptr */
3630 struct xfs_buf *rbp; /* right buffer pointer */
3631 struct xfs_btree_block *right; /* right btree block */
3632 struct xfs_btree_block *rrblock; /* right-right btree block */
3633 struct xfs_buf *rrbp; /* right-right buffer pointer */
3634 int rrecs = 0; /* right record count */
3635 struct xfs_btree_cur *tcur; /* temporary btree cursor */
3636 int numrecs; /* temporary numrec count */
3640 /* Get the index of the entry being deleted, check for nothing there. */
3641 ptr = cur->bc_ptrs[level];
3647 /* Get the buffer & block containing the record or key/ptr. */
3648 block = xfs_btree_get_block(cur, level, &bp);
3649 numrecs = xfs_btree_get_numrecs(block);
3652 error = xfs_btree_check_block(cur, block, level, bp);
3657 /* Fail if we're off the end of the block. */
3658 if (ptr > numrecs) {
3663 XFS_BTREE_STATS_INC(cur, delrec);
3664 XFS_BTREE_STATS_ADD(cur, moves, numrecs - ptr);
3666 /* Excise the entries being deleted. */
3668 /* It's a nonleaf. operate on keys and ptrs */
3669 union xfs_btree_key *lkp;
3670 union xfs_btree_ptr *lpp;
3672 lkp = xfs_btree_key_addr(cur, ptr + 1, block);
3673 lpp = xfs_btree_ptr_addr(cur, ptr + 1, block);
3675 for (i = 0; i < numrecs - ptr; i++) {
3676 error = xfs_btree_debug_check_ptr(cur, lpp, i, level);
3681 if (ptr < numrecs) {
3682 xfs_btree_shift_keys(cur, lkp, -1, numrecs - ptr);
3683 xfs_btree_shift_ptrs(cur, lpp, -1, numrecs - ptr);
3684 xfs_btree_log_keys(cur, bp, ptr, numrecs - 1);
3685 xfs_btree_log_ptrs(cur, bp, ptr, numrecs - 1);
3688 /* It's a leaf. operate on records */
3689 if (ptr < numrecs) {
3690 xfs_btree_shift_recs(cur,
3691 xfs_btree_rec_addr(cur, ptr + 1, block),
3693 xfs_btree_log_recs(cur, bp, ptr, numrecs - 1);
3698 * Decrement and log the number of entries in the block.
3700 xfs_btree_set_numrecs(block, --numrecs);
3701 xfs_btree_log_block(cur, bp, XFS_BB_NUMRECS);
3704 * If we are tracking the last record in the tree and
3705 * we are at the far right edge of the tree, update it.
3707 if (xfs_btree_is_lastrec(cur, block, level)) {
3708 cur->bc_ops->update_lastrec(cur, block, NULL,
3709 ptr, LASTREC_DELREC);
3713 * We're at the root level. First, shrink the root block in-memory.
3714 * Try to get rid of the next level down. If we can't then there's
3715 * nothing left to do.
3717 if (level == cur->bc_nlevels - 1) {
3718 if (cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) {
3719 xfs_iroot_realloc(cur->bc_ino.ip, -1,
3720 cur->bc_ino.whichfork);
3722 error = xfs_btree_kill_iroot(cur);
3726 error = xfs_btree_dec_cursor(cur, level, stat);
3734 * If this is the root level, and there's only one entry left,
3735 * and it's NOT the leaf level, then we can get rid of this
3738 if (numrecs == 1 && level > 0) {
3739 union xfs_btree_ptr *pp;
3741 * pp is still set to the first pointer in the block.
3742 * Make it the new root of the btree.
3744 pp = xfs_btree_ptr_addr(cur, 1, block);
3745 error = xfs_btree_kill_root(cur, bp, level, pp);
3748 } else if (level > 0) {
3749 error = xfs_btree_dec_cursor(cur, level, stat);
3758 * If we deleted the leftmost entry in the block, update the
3759 * key values above us in the tree.
3761 if (xfs_btree_needs_key_update(cur, ptr)) {
3762 error = xfs_btree_update_keys(cur, level);
3768 * If the number of records remaining in the block is at least
3769 * the minimum, we're done.
3771 if (numrecs >= cur->bc_ops->get_minrecs(cur, level)) {
3772 error = xfs_btree_dec_cursor(cur, level, stat);
3779 * Otherwise, we have to move some records around to keep the
3780 * tree balanced. Look at the left and right sibling blocks to
3781 * see if we can re-balance by moving only one record.
3783 xfs_btree_get_sibling(cur, block, &rptr, XFS_BB_RIGHTSIB);
3784 xfs_btree_get_sibling(cur, block, &lptr, XFS_BB_LEFTSIB);
3786 if (cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) {
3788 * One child of root, need to get a chance to copy its contents
3789 * into the root and delete it. Can't go up to next level,
3790 * there's nothing to delete there.
3792 if (xfs_btree_ptr_is_null(cur, &rptr) &&
3793 xfs_btree_ptr_is_null(cur, &lptr) &&
3794 level == cur->bc_nlevels - 2) {
3795 error = xfs_btree_kill_iroot(cur);
3797 error = xfs_btree_dec_cursor(cur, level, stat);
3804 ASSERT(!xfs_btree_ptr_is_null(cur, &rptr) ||
3805 !xfs_btree_ptr_is_null(cur, &lptr));
3808 * Duplicate the cursor so our btree manipulations here won't
3809 * disrupt the next level up.
3811 error = xfs_btree_dup_cursor(cur, &tcur);
3816 * If there's a right sibling, see if it's ok to shift an entry
3819 if (!xfs_btree_ptr_is_null(cur, &rptr)) {
3821 * Move the temp cursor to the last entry in the next block.
3822 * Actually any entry but the first would suffice.
3824 i = xfs_btree_lastrec(tcur, level);
3825 if (XFS_IS_CORRUPT(cur->bc_mp, i != 1)) {
3826 error = -EFSCORRUPTED;
3830 error = xfs_btree_increment(tcur, level, &i);
3833 if (XFS_IS_CORRUPT(cur->bc_mp, i != 1)) {
3834 error = -EFSCORRUPTED;
3838 i = xfs_btree_lastrec(tcur, level);
3839 if (XFS_IS_CORRUPT(cur->bc_mp, i != 1)) {
3840 error = -EFSCORRUPTED;
3844 /* Grab a pointer to the block. */
3845 right = xfs_btree_get_block(tcur, level, &rbp);
3847 error = xfs_btree_check_block(tcur, right, level, rbp);
3851 /* Grab the current block number, for future use. */
3852 xfs_btree_get_sibling(tcur, right, &cptr, XFS_BB_LEFTSIB);
3855 * If right block is full enough so that removing one entry
3856 * won't make it too empty, and left-shifting an entry out
3857 * of right to us works, we're done.
3859 if (xfs_btree_get_numrecs(right) - 1 >=
3860 cur->bc_ops->get_minrecs(tcur, level)) {
3861 error = xfs_btree_lshift(tcur, level, &i);
3865 ASSERT(xfs_btree_get_numrecs(block) >=
3866 cur->bc_ops->get_minrecs(tcur, level));
3868 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
3871 error = xfs_btree_dec_cursor(cur, level, stat);
3879 * Otherwise, grab the number of records in right for
3880 * future reference, and fix up the temp cursor to point
3881 * to our block again (last record).
3883 rrecs = xfs_btree_get_numrecs(right);
3884 if (!xfs_btree_ptr_is_null(cur, &lptr)) {
3885 i = xfs_btree_firstrec(tcur, level);
3886 if (XFS_IS_CORRUPT(cur->bc_mp, i != 1)) {
3887 error = -EFSCORRUPTED;
3891 error = xfs_btree_decrement(tcur, level, &i);
3894 if (XFS_IS_CORRUPT(cur->bc_mp, i != 1)) {
3895 error = -EFSCORRUPTED;
3902 * If there's a left sibling, see if it's ok to shift an entry
3905 if (!xfs_btree_ptr_is_null(cur, &lptr)) {
3907 * Move the temp cursor to the first entry in the
3910 i = xfs_btree_firstrec(tcur, level);
3911 if (XFS_IS_CORRUPT(cur->bc_mp, i != 1)) {
3912 error = -EFSCORRUPTED;
3916 error = xfs_btree_decrement(tcur, level, &i);
3919 i = xfs_btree_firstrec(tcur, level);
3920 if (XFS_IS_CORRUPT(cur->bc_mp, i != 1)) {
3921 error = -EFSCORRUPTED;
3925 /* Grab a pointer to the block. */
3926 left = xfs_btree_get_block(tcur, level, &lbp);
3928 error = xfs_btree_check_block(cur, left, level, lbp);
3932 /* Grab the current block number, for future use. */
3933 xfs_btree_get_sibling(tcur, left, &cptr, XFS_BB_RIGHTSIB);
3936 * If left block is full enough so that removing one entry
3937 * won't make it too empty, and right-shifting an entry out
3938 * of left to us works, we're done.
3940 if (xfs_btree_get_numrecs(left) - 1 >=
3941 cur->bc_ops->get_minrecs(tcur, level)) {
3942 error = xfs_btree_rshift(tcur, level, &i);
3946 ASSERT(xfs_btree_get_numrecs(block) >=
3947 cur->bc_ops->get_minrecs(tcur, level));
3948 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
3959 * Otherwise, grab the number of records in right for
3962 lrecs = xfs_btree_get_numrecs(left);
3965 /* Delete the temp cursor, we're done with it. */
3966 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
3969 /* If here, we need to do a join to keep the tree balanced. */
3970 ASSERT(!xfs_btree_ptr_is_null(cur, &cptr));
3972 if (!xfs_btree_ptr_is_null(cur, &lptr) &&
3973 lrecs + xfs_btree_get_numrecs(block) <=
3974 cur->bc_ops->get_maxrecs(cur, level)) {
3976 * Set "right" to be the starting block,
3977 * "left" to be the left neighbor.
3982 error = xfs_btree_read_buf_block(cur, &lptr, 0, &left, &lbp);
3987 * If that won't work, see if we can join with the right neighbor block.
3989 } else if (!xfs_btree_ptr_is_null(cur, &rptr) &&
3990 rrecs + xfs_btree_get_numrecs(block) <=
3991 cur->bc_ops->get_maxrecs(cur, level)) {
3993 * Set "left" to be the starting block,
3994 * "right" to be the right neighbor.
3999 error = xfs_btree_read_buf_block(cur, &rptr, 0, &right, &rbp);
4004 * Otherwise, we can't fix the imbalance.
4005 * Just return. This is probably a logic error, but it's not fatal.
4008 error = xfs_btree_dec_cursor(cur, level, stat);
4014 rrecs = xfs_btree_get_numrecs(right);
4015 lrecs = xfs_btree_get_numrecs(left);
4018 * We're now going to join "left" and "right" by moving all the stuff
4019 * in "right" to "left" and deleting "right".
4021 XFS_BTREE_STATS_ADD(cur, moves, rrecs);
4023 /* It's a non-leaf. Move keys and pointers. */
4024 union xfs_btree_key *lkp; /* left btree key */
4025 union xfs_btree_ptr *lpp; /* left address pointer */
4026 union xfs_btree_key *rkp; /* right btree key */
4027 union xfs_btree_ptr *rpp; /* right address pointer */
4029 lkp = xfs_btree_key_addr(cur, lrecs + 1, left);
4030 lpp = xfs_btree_ptr_addr(cur, lrecs + 1, left);
4031 rkp = xfs_btree_key_addr(cur, 1, right);
4032 rpp = xfs_btree_ptr_addr(cur, 1, right);
4034 for (i = 1; i < rrecs; i++) {
4035 error = xfs_btree_debug_check_ptr(cur, rpp, i, level);
4040 xfs_btree_copy_keys(cur, lkp, rkp, rrecs);
4041 xfs_btree_copy_ptrs(cur, lpp, rpp, rrecs);
4043 xfs_btree_log_keys(cur, lbp, lrecs + 1, lrecs + rrecs);
4044 xfs_btree_log_ptrs(cur, lbp, lrecs + 1, lrecs + rrecs);
4046 /* It's a leaf. Move records. */
4047 union xfs_btree_rec *lrp; /* left record pointer */
4048 union xfs_btree_rec *rrp; /* right record pointer */
4050 lrp = xfs_btree_rec_addr(cur, lrecs + 1, left);
4051 rrp = xfs_btree_rec_addr(cur, 1, right);
4053 xfs_btree_copy_recs(cur, lrp, rrp, rrecs);
4054 xfs_btree_log_recs(cur, lbp, lrecs + 1, lrecs + rrecs);
4057 XFS_BTREE_STATS_INC(cur, join);
4060 * Fix up the number of records and right block pointer in the
4061 * surviving block, and log it.
4063 xfs_btree_set_numrecs(left, lrecs + rrecs);
4064 xfs_btree_get_sibling(cur, right, &cptr, XFS_BB_RIGHTSIB);
4065 xfs_btree_set_sibling(cur, left, &cptr, XFS_BB_RIGHTSIB);
4066 xfs_btree_log_block(cur, lbp, XFS_BB_NUMRECS | XFS_BB_RIGHTSIB);
4068 /* If there is a right sibling, point it to the remaining block. */
4069 xfs_btree_get_sibling(cur, left, &cptr, XFS_BB_RIGHTSIB);
4070 if (!xfs_btree_ptr_is_null(cur, &cptr)) {
4071 error = xfs_btree_read_buf_block(cur, &cptr, 0, &rrblock, &rrbp);
4074 xfs_btree_set_sibling(cur, rrblock, &lptr, XFS_BB_LEFTSIB);
4075 xfs_btree_log_block(cur, rrbp, XFS_BB_LEFTSIB);
4078 /* Free the deleted block. */
4079 error = xfs_btree_free_block(cur, rbp);
4084 * If we joined with the left neighbor, set the buffer in the
4085 * cursor to the left block, and fix up the index.
4088 cur->bc_bufs[level] = lbp;
4089 cur->bc_ptrs[level] += lrecs;
4090 cur->bc_ra[level] = 0;
4093 * If we joined with the right neighbor and there's a level above
4094 * us, increment the cursor at that level.
4096 else if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) ||
4097 (level + 1 < cur->bc_nlevels)) {
4098 error = xfs_btree_increment(cur, level + 1, &i);
4104 * Readjust the ptr at this level if it's not a leaf, since it's
4105 * still pointing at the deletion point, which makes the cursor
4106 * inconsistent. If this makes the ptr 0, the caller fixes it up.
4107 * We can't use decrement because it would change the next level up.
4110 cur->bc_ptrs[level]--;
4113 * We combined blocks, so we have to update the parent keys if the
4114 * btree supports overlapped intervals. However, bc_ptrs[level + 1]
4115 * points to the old block so that the caller knows which record to
4116 * delete. Therefore, the caller must be savvy enough to call updkeys
4117 * for us if we return stat == 2. The other exit points from this
4118 * function don't require deletions further up the tree, so they can
4119 * call updkeys directly.
4122 /* Return value means the next level up has something to do. */
4128 xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
4133 * Delete the record pointed to by cur.
4134 * The cursor refers to the place where the record was (could be inserted)
4135 * when the operation returns.
4139 struct xfs_btree_cur *cur,
4140 int *stat) /* success/failure */
4142 int error; /* error return value */
4145 bool joined = false;
4148 * Go up the tree, starting at leaf level.
4150 * If 2 is returned then a join was done; go to the next level.
4151 * Otherwise we are done.
4153 for (level = 0, i = 2; i == 2; level++) {
4154 error = xfs_btree_delrec(cur, level, &i);
4162 * If we combined blocks as part of deleting the record, delrec won't
4163 * have updated the parent high keys so we have to do that here.
4165 if (joined && (cur->bc_flags & XFS_BTREE_OVERLAPPING)) {
4166 error = xfs_btree_updkeys_force(cur, 0);
4172 for (level = 1; level < cur->bc_nlevels; level++) {
4173 if (cur->bc_ptrs[level] == 0) {
4174 error = xfs_btree_decrement(cur, level, &i);
4189 * Get the data from the pointed-to record.
4193 struct xfs_btree_cur *cur, /* btree cursor */
4194 union xfs_btree_rec **recp, /* output: btree record */
4195 int *stat) /* output: success/failure */
4197 struct xfs_btree_block *block; /* btree block */
4198 struct xfs_buf *bp; /* buffer pointer */
4199 int ptr; /* record number */
4201 int error; /* error return value */
4204 ptr = cur->bc_ptrs[0];
4205 block = xfs_btree_get_block(cur, 0, &bp);
4208 error = xfs_btree_check_block(cur, block, 0, bp);
4214 * Off the right end or left end, return failure.
4216 if (ptr > xfs_btree_get_numrecs(block) || ptr <= 0) {
4222 * Point to the record and extract its data.
4224 *recp = xfs_btree_rec_addr(cur, ptr, block);
4229 /* Visit a block in a btree. */
4231 xfs_btree_visit_block(
4232 struct xfs_btree_cur *cur,
4234 xfs_btree_visit_blocks_fn fn,
4237 struct xfs_btree_block *block;
4239 union xfs_btree_ptr rptr;
4242 /* do right sibling readahead */
4243 xfs_btree_readahead(cur, level, XFS_BTCUR_RIGHTRA);
4244 block = xfs_btree_get_block(cur, level, &bp);
4246 /* process the block */
4247 error = fn(cur, level, data);
4251 /* now read rh sibling block for next iteration */
4252 xfs_btree_get_sibling(cur, block, &rptr, XFS_BB_RIGHTSIB);
4253 if (xfs_btree_ptr_is_null(cur, &rptr))
4256 return xfs_btree_lookup_get_block(cur, level, &rptr, &block);
4260 /* Visit every block in a btree. */
4262 xfs_btree_visit_blocks(
4263 struct xfs_btree_cur *cur,
4264 xfs_btree_visit_blocks_fn fn,
4268 union xfs_btree_ptr lptr;
4270 struct xfs_btree_block *block = NULL;
4273 cur->bc_ops->init_ptr_from_cur(cur, &lptr);
4275 /* for each level */
4276 for (level = cur->bc_nlevels - 1; level >= 0; level--) {
4277 /* grab the left hand block */
4278 error = xfs_btree_lookup_get_block(cur, level, &lptr, &block);
4282 /* readahead the left most block for the next level down */
4284 union xfs_btree_ptr *ptr;
4286 ptr = xfs_btree_ptr_addr(cur, 1, block);
4287 xfs_btree_readahead_ptr(cur, ptr, 1);
4289 /* save for the next iteration of the loop */
4290 xfs_btree_copy_ptrs(cur, &lptr, ptr, 1);
4292 if (!(flags & XFS_BTREE_VISIT_LEAVES))
4294 } else if (!(flags & XFS_BTREE_VISIT_RECORDS)) {
4298 /* for each buffer in the level */
4300 error = xfs_btree_visit_block(cur, level, fn, data);
4303 if (error != -ENOENT)
4311 * Change the owner of a btree.
4313 * The mechanism we use here is ordered buffer logging. Because we don't know
4314 * how many buffers were are going to need to modify, we don't really want to
4315 * have to make transaction reservations for the worst case of every buffer in a
4316 * full size btree as that may be more space that we can fit in the log....
4318 * We do the btree walk in the most optimal manner possible - we have sibling
4319 * pointers so we can just walk all the blocks on each level from left to right
4320 * in a single pass, and then move to the next level and do the same. We can
4321 * also do readahead on the sibling pointers to get IO moving more quickly,
4322 * though for slow disks this is unlikely to make much difference to performance
4323 * as the amount of CPU work we have to do before moving to the next block is
4326 * For each btree block that we load, modify the owner appropriately, set the
4327 * buffer as an ordered buffer and log it appropriately. We need to ensure that
4328 * we mark the region we change dirty so that if the buffer is relogged in
4329 * a subsequent transaction the changes we make here as an ordered buffer are
4330 * correctly relogged in that transaction. If we are in recovery context, then
4331 * just queue the modified buffer as delayed write buffer so the transaction
4332 * recovery completion writes the changes to disk.
4334 struct xfs_btree_block_change_owner_info {
4336 struct list_head *buffer_list;
4340 xfs_btree_block_change_owner(
4341 struct xfs_btree_cur *cur,
4345 struct xfs_btree_block_change_owner_info *bbcoi = data;
4346 struct xfs_btree_block *block;
4349 /* modify the owner */
4350 block = xfs_btree_get_block(cur, level, &bp);
4351 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
4352 if (block->bb_u.l.bb_owner == cpu_to_be64(bbcoi->new_owner))
4354 block->bb_u.l.bb_owner = cpu_to_be64(bbcoi->new_owner);
4356 if (block->bb_u.s.bb_owner == cpu_to_be32(bbcoi->new_owner))
4358 block->bb_u.s.bb_owner = cpu_to_be32(bbcoi->new_owner);
4362 * If the block is a root block hosted in an inode, we might not have a
4363 * buffer pointer here and we shouldn't attempt to log the change as the
4364 * information is already held in the inode and discarded when the root
4365 * block is formatted into the on-disk inode fork. We still change it,
4366 * though, so everything is consistent in memory.
4369 ASSERT(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE);
4370 ASSERT(level == cur->bc_nlevels - 1);
4375 if (!xfs_trans_ordered_buf(cur->bc_tp, bp)) {
4376 xfs_btree_log_block(cur, bp, XFS_BB_OWNER);
4380 xfs_buf_delwri_queue(bp, bbcoi->buffer_list);
4387 xfs_btree_change_owner(
4388 struct xfs_btree_cur *cur,
4390 struct list_head *buffer_list)
4392 struct xfs_btree_block_change_owner_info bbcoi;
4394 bbcoi.new_owner = new_owner;
4395 bbcoi.buffer_list = buffer_list;
4397 return xfs_btree_visit_blocks(cur, xfs_btree_block_change_owner,
4398 XFS_BTREE_VISIT_ALL, &bbcoi);
4401 /* Verify the v5 fields of a long-format btree block. */
4403 xfs_btree_lblock_v5hdr_verify(
4407 struct xfs_mount *mp = bp->b_mount;
4408 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
4410 if (!xfs_sb_version_hascrc(&mp->m_sb))
4411 return __this_address;
4412 if (!uuid_equal(&block->bb_u.l.bb_uuid, &mp->m_sb.sb_meta_uuid))
4413 return __this_address;
4414 if (block->bb_u.l.bb_blkno != cpu_to_be64(bp->b_bn))
4415 return __this_address;
4416 if (owner != XFS_RMAP_OWN_UNKNOWN &&
4417 be64_to_cpu(block->bb_u.l.bb_owner) != owner)
4418 return __this_address;
4422 /* Verify a long-format btree block. */
4424 xfs_btree_lblock_verify(
4426 unsigned int max_recs)
4428 struct xfs_mount *mp = bp->b_mount;
4429 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
4431 /* numrecs verification */
4432 if (be16_to_cpu(block->bb_numrecs) > max_recs)
4433 return __this_address;
4435 /* sibling pointer verification */
4436 if (block->bb_u.l.bb_leftsib != cpu_to_be64(NULLFSBLOCK) &&
4437 !xfs_verify_fsbno(mp, be64_to_cpu(block->bb_u.l.bb_leftsib)))
4438 return __this_address;
4439 if (block->bb_u.l.bb_rightsib != cpu_to_be64(NULLFSBLOCK) &&
4440 !xfs_verify_fsbno(mp, be64_to_cpu(block->bb_u.l.bb_rightsib)))
4441 return __this_address;
4447 * xfs_btree_sblock_v5hdr_verify() -- verify the v5 fields of a short-format
4450 * @bp: buffer containing the btree block
4453 xfs_btree_sblock_v5hdr_verify(
4456 struct xfs_mount *mp = bp->b_mount;
4457 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
4458 struct xfs_perag *pag = bp->b_pag;
4460 if (!xfs_sb_version_hascrc(&mp->m_sb))
4461 return __this_address;
4462 if (!uuid_equal(&block->bb_u.s.bb_uuid, &mp->m_sb.sb_meta_uuid))
4463 return __this_address;
4464 if (block->bb_u.s.bb_blkno != cpu_to_be64(bp->b_bn))
4465 return __this_address;
4466 if (pag && be32_to_cpu(block->bb_u.s.bb_owner) != pag->pag_agno)
4467 return __this_address;
4472 * xfs_btree_sblock_verify() -- verify a short-format btree block
4474 * @bp: buffer containing the btree block
4475 * @max_recs: maximum records allowed in this btree node
4478 xfs_btree_sblock_verify(
4480 unsigned int max_recs)
4482 struct xfs_mount *mp = bp->b_mount;
4483 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
4486 /* numrecs verification */
4487 if (be16_to_cpu(block->bb_numrecs) > max_recs)
4488 return __this_address;
4490 /* sibling pointer verification */
4491 agno = xfs_daddr_to_agno(mp, XFS_BUF_ADDR(bp));
4492 if (block->bb_u.s.bb_leftsib != cpu_to_be32(NULLAGBLOCK) &&
4493 !xfs_verify_agbno(mp, agno, be32_to_cpu(block->bb_u.s.bb_leftsib)))
4494 return __this_address;
4495 if (block->bb_u.s.bb_rightsib != cpu_to_be32(NULLAGBLOCK) &&
4496 !xfs_verify_agbno(mp, agno, be32_to_cpu(block->bb_u.s.bb_rightsib)))
4497 return __this_address;
4503 * Calculate the number of btree levels needed to store a given number of
4504 * records in a short-format btree.
4507 xfs_btree_compute_maxlevels(
4512 unsigned long maxblocks;
4514 maxblocks = (len + limits[0] - 1) / limits[0];
4515 for (level = 1; maxblocks > 1; level++)
4516 maxblocks = (maxblocks + limits[1] - 1) / limits[1];
4521 * Query a regular btree for all records overlapping a given interval.
4522 * Start with a LE lookup of the key of low_rec and return all records
4523 * until we find a record with a key greater than the key of high_rec.
4526 xfs_btree_simple_query_range(
4527 struct xfs_btree_cur *cur,
4528 union xfs_btree_key *low_key,
4529 union xfs_btree_key *high_key,
4530 xfs_btree_query_range_fn fn,
4533 union xfs_btree_rec *recp;
4534 union xfs_btree_key rec_key;
4537 bool firstrec = true;
4540 ASSERT(cur->bc_ops->init_high_key_from_rec);
4541 ASSERT(cur->bc_ops->diff_two_keys);
4544 * Find the leftmost record. The btree cursor must be set
4545 * to the low record used to generate low_key.
4548 error = xfs_btree_lookup(cur, XFS_LOOKUP_LE, &stat);
4552 /* Nothing? See if there's anything to the right. */
4554 error = xfs_btree_increment(cur, 0, &stat);
4560 /* Find the record. */
4561 error = xfs_btree_get_rec(cur, &recp, &stat);
4565 /* Skip if high_key(rec) < low_key. */
4567 cur->bc_ops->init_high_key_from_rec(&rec_key, recp);
4569 diff = cur->bc_ops->diff_two_keys(cur, low_key,
4575 /* Stop if high_key < low_key(rec). */
4576 cur->bc_ops->init_key_from_rec(&rec_key, recp);
4577 diff = cur->bc_ops->diff_two_keys(cur, &rec_key, high_key);
4582 error = fn(cur, recp, priv);
4587 /* Move on to the next record. */
4588 error = xfs_btree_increment(cur, 0, &stat);
4598 * Query an overlapped interval btree for all records overlapping a given
4599 * interval. This function roughly follows the algorithm given in
4600 * "Interval Trees" of _Introduction to Algorithms_, which is section
4601 * 14.3 in the 2nd and 3rd editions.
4603 * First, generate keys for the low and high records passed in.
4605 * For any leaf node, generate the high and low keys for the record.
4606 * If the record keys overlap with the query low/high keys, pass the
4607 * record to the function iterator.
4609 * For any internal node, compare the low and high keys of each
4610 * pointer against the query low/high keys. If there's an overlap,
4611 * follow the pointer.
4613 * As an optimization, we stop scanning a block when we find a low key
4614 * that is greater than the query's high key.
4617 xfs_btree_overlapped_query_range(
4618 struct xfs_btree_cur *cur,
4619 union xfs_btree_key *low_key,
4620 union xfs_btree_key *high_key,
4621 xfs_btree_query_range_fn fn,
4624 union xfs_btree_ptr ptr;
4625 union xfs_btree_ptr *pp;
4626 union xfs_btree_key rec_key;
4627 union xfs_btree_key rec_hkey;
4628 union xfs_btree_key *lkp;
4629 union xfs_btree_key *hkp;
4630 union xfs_btree_rec *recp;
4631 struct xfs_btree_block *block;
4639 /* Load the root of the btree. */
4640 level = cur->bc_nlevels - 1;
4641 cur->bc_ops->init_ptr_from_cur(cur, &ptr);
4642 error = xfs_btree_lookup_get_block(cur, level, &ptr, &block);
4645 xfs_btree_get_block(cur, level, &bp);
4646 trace_xfs_btree_overlapped_query_range(cur, level, bp);
4648 error = xfs_btree_check_block(cur, block, level, bp);
4652 cur->bc_ptrs[level] = 1;
4654 while (level < cur->bc_nlevels) {
4655 block = xfs_btree_get_block(cur, level, &bp);
4657 /* End of node, pop back towards the root. */
4658 if (cur->bc_ptrs[level] > be16_to_cpu(block->bb_numrecs)) {
4660 if (level < cur->bc_nlevels - 1)
4661 cur->bc_ptrs[level + 1]++;
4667 /* Handle a leaf node. */
4668 recp = xfs_btree_rec_addr(cur, cur->bc_ptrs[0], block);
4670 cur->bc_ops->init_high_key_from_rec(&rec_hkey, recp);
4671 ldiff = cur->bc_ops->diff_two_keys(cur, &rec_hkey,
4674 cur->bc_ops->init_key_from_rec(&rec_key, recp);
4675 hdiff = cur->bc_ops->diff_two_keys(cur, high_key,
4679 * If (record's high key >= query's low key) and
4680 * (query's high key >= record's low key), then
4681 * this record overlaps the query range; callback.
4683 if (ldiff >= 0 && hdiff >= 0) {
4684 error = fn(cur, recp, priv);
4687 } else if (hdiff < 0) {
4688 /* Record is larger than high key; pop. */
4691 cur->bc_ptrs[level]++;
4695 /* Handle an internal node. */
4696 lkp = xfs_btree_key_addr(cur, cur->bc_ptrs[level], block);
4697 hkp = xfs_btree_high_key_addr(cur, cur->bc_ptrs[level], block);
4698 pp = xfs_btree_ptr_addr(cur, cur->bc_ptrs[level], block);
4700 ldiff = cur->bc_ops->diff_two_keys(cur, hkp, low_key);
4701 hdiff = cur->bc_ops->diff_two_keys(cur, high_key, lkp);
4704 * If (pointer's high key >= query's low key) and
4705 * (query's high key >= pointer's low key), then
4706 * this record overlaps the query range; follow pointer.
4708 if (ldiff >= 0 && hdiff >= 0) {
4710 error = xfs_btree_lookup_get_block(cur, level, pp,
4714 xfs_btree_get_block(cur, level, &bp);
4715 trace_xfs_btree_overlapped_query_range(cur, level, bp);
4717 error = xfs_btree_check_block(cur, block, level, bp);
4721 cur->bc_ptrs[level] = 1;
4723 } else if (hdiff < 0) {
4724 /* The low key is larger than the upper range; pop. */
4727 cur->bc_ptrs[level]++;
4732 * If we don't end this function with the cursor pointing at a record
4733 * block, a subsequent non-error cursor deletion will not release
4734 * node-level buffers, causing a buffer leak. This is quite possible
4735 * with a zero-results range query, so release the buffers if we
4736 * failed to return any results.
4738 if (cur->bc_bufs[0] == NULL) {
4739 for (i = 0; i < cur->bc_nlevels; i++) {
4740 if (cur->bc_bufs[i]) {
4741 xfs_trans_brelse(cur->bc_tp, cur->bc_bufs[i]);
4742 cur->bc_bufs[i] = NULL;
4743 cur->bc_ptrs[i] = 0;
4753 * Query a btree for all records overlapping a given interval of keys. The
4754 * supplied function will be called with each record found; return one of the
4755 * XFS_BTREE_QUERY_RANGE_{CONTINUE,ABORT} values or the usual negative error
4756 * code. This function returns -ECANCELED, zero, or a negative error code.
4759 xfs_btree_query_range(
4760 struct xfs_btree_cur *cur,
4761 union xfs_btree_irec *low_rec,
4762 union xfs_btree_irec *high_rec,
4763 xfs_btree_query_range_fn fn,
4766 union xfs_btree_rec rec;
4767 union xfs_btree_key low_key;
4768 union xfs_btree_key high_key;
4770 /* Find the keys of both ends of the interval. */
4771 cur->bc_rec = *high_rec;
4772 cur->bc_ops->init_rec_from_cur(cur, &rec);
4773 cur->bc_ops->init_key_from_rec(&high_key, &rec);
4775 cur->bc_rec = *low_rec;
4776 cur->bc_ops->init_rec_from_cur(cur, &rec);
4777 cur->bc_ops->init_key_from_rec(&low_key, &rec);
4779 /* Enforce low key < high key. */
4780 if (cur->bc_ops->diff_two_keys(cur, &low_key, &high_key) > 0)
4783 if (!(cur->bc_flags & XFS_BTREE_OVERLAPPING))
4784 return xfs_btree_simple_query_range(cur, &low_key,
4785 &high_key, fn, priv);
4786 return xfs_btree_overlapped_query_range(cur, &low_key, &high_key,
4790 /* Query a btree for all records. */
4792 xfs_btree_query_all(
4793 struct xfs_btree_cur *cur,
4794 xfs_btree_query_range_fn fn,
4797 union xfs_btree_key low_key;
4798 union xfs_btree_key high_key;
4800 memset(&cur->bc_rec, 0, sizeof(cur->bc_rec));
4801 memset(&low_key, 0, sizeof(low_key));
4802 memset(&high_key, 0xFF, sizeof(high_key));
4804 return xfs_btree_simple_query_range(cur, &low_key, &high_key, fn, priv);
4808 * Calculate the number of blocks needed to store a given number of records
4809 * in a short-format (per-AG metadata) btree.
4812 xfs_btree_calc_size(
4814 unsigned long long len)
4818 unsigned long long rval;
4820 maxrecs = limits[0];
4821 for (level = 0, rval = 0; len > 1; level++) {
4823 do_div(len, maxrecs);
4824 maxrecs = limits[1];
4831 xfs_btree_count_blocks_helper(
4832 struct xfs_btree_cur *cur,
4836 xfs_extlen_t *blocks = data;
4842 /* Count the blocks in a btree and return the result in *blocks. */
4844 xfs_btree_count_blocks(
4845 struct xfs_btree_cur *cur,
4846 xfs_extlen_t *blocks)
4849 return xfs_btree_visit_blocks(cur, xfs_btree_count_blocks_helper,
4850 XFS_BTREE_VISIT_ALL, blocks);
4853 /* Compare two btree pointers. */
4855 xfs_btree_diff_two_ptrs(
4856 struct xfs_btree_cur *cur,
4857 const union xfs_btree_ptr *a,
4858 const union xfs_btree_ptr *b)
4860 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
4861 return (int64_t)be64_to_cpu(a->l) - be64_to_cpu(b->l);
4862 return (int64_t)be32_to_cpu(a->s) - be32_to_cpu(b->s);
4865 /* If there's an extent, we're done. */
4867 xfs_btree_has_record_helper(
4868 struct xfs_btree_cur *cur,
4869 union xfs_btree_rec *rec,
4875 /* Is there a record covering a given range of keys? */
4877 xfs_btree_has_record(
4878 struct xfs_btree_cur *cur,
4879 union xfs_btree_irec *low,
4880 union xfs_btree_irec *high,
4885 error = xfs_btree_query_range(cur, low, high,
4886 &xfs_btree_has_record_helper, NULL);
4887 if (error == -ECANCELED) {
4895 /* Are there more records in this btree? */
4897 xfs_btree_has_more_records(
4898 struct xfs_btree_cur *cur)
4900 struct xfs_btree_block *block;
4903 block = xfs_btree_get_block(cur, 0, &bp);
4905 /* There are still records in this block. */
4906 if (cur->bc_ptrs[0] < xfs_btree_get_numrecs(block))
4909 /* There are more record blocks. */
4910 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
4911 return block->bb_u.l.bb_rightsib != cpu_to_be64(NULLFSBLOCK);
4913 return block->bb_u.s.bb_rightsib != cpu_to_be32(NULLAGBLOCK);