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"
25 #include "xfs_alloc_btree.h"
26 #include "xfs_ialloc_btree.h"
27 #include "xfs_bmap_btree.h"
28 #include "xfs_rmap_btree.h"
29 #include "xfs_refcount_btree.h"
30 #include "xfs_health.h"
31 #include "xfs_buf_mem.h"
32 #include "xfs_btree_mem.h"
33 #include "xfs_rtrmap_btree.h"
36 #include "xfs_quota.h"
37 #include "xfs_metafile.h"
38 #include "xfs_rtrefcount_btree.h"
41 * Btree magic numbers.
46 const struct xfs_btree_ops *ops)
48 int idx = xfs_has_crc(mp) ? 1 : 0;
49 __be32 magic = ops->buf_ops->magic[idx];
51 /* Ensure we asked for crc for crc-only magics. */
53 return be32_to_cpu(magic);
57 * These sibling pointer checks are optimised for null sibling pointers. This
58 * happens a lot, and we don't need to byte swap at runtime if the sibling
61 * These are explicitly marked at inline because the cost of calling them as
62 * functions instead of inlining them is about 36 bytes extra code per call site
63 * on x86-64. Yes, gcc-11 fails to inline them, and explicit inlining of these
64 * two sibling check functions reduces the compiled code size by over 300
67 static inline xfs_failaddr_t
68 xfs_btree_check_fsblock_siblings(
73 xfs_fsblock_t sibling;
75 if (dsibling == cpu_to_be64(NULLFSBLOCK))
78 sibling = be64_to_cpu(dsibling);
80 return __this_address;
81 if (!xfs_verify_fsbno(mp, sibling))
82 return __this_address;
86 static inline xfs_failaddr_t
87 xfs_btree_check_memblock_siblings(
88 struct xfs_buftarg *btp,
94 if (dsibling == cpu_to_be64(NULLFSBLOCK))
97 sibling = be64_to_cpu(dsibling);
99 return __this_address;
100 if (!xmbuf_verify_daddr(btp, xfbno_to_daddr(sibling)))
101 return __this_address;
105 static inline xfs_failaddr_t
106 xfs_btree_check_agblock_siblings(
107 struct xfs_perag *pag,
111 xfs_agblock_t sibling;
113 if (dsibling == cpu_to_be32(NULLAGBLOCK))
116 sibling = be32_to_cpu(dsibling);
117 if (sibling == agbno)
118 return __this_address;
119 if (!xfs_verify_agbno(pag, sibling))
120 return __this_address;
124 static xfs_failaddr_t
125 __xfs_btree_check_lblock_hdr(
126 struct xfs_btree_cur *cur,
127 struct xfs_btree_block *block,
131 struct xfs_mount *mp = cur->bc_mp;
133 if (xfs_has_crc(mp)) {
134 if (!uuid_equal(&block->bb_u.l.bb_uuid, &mp->m_sb.sb_meta_uuid))
135 return __this_address;
136 if (block->bb_u.l.bb_blkno !=
137 cpu_to_be64(bp ? xfs_buf_daddr(bp) : XFS_BUF_DADDR_NULL))
138 return __this_address;
139 if (block->bb_u.l.bb_pad != cpu_to_be32(0))
140 return __this_address;
143 if (be32_to_cpu(block->bb_magic) != xfs_btree_magic(mp, cur->bc_ops))
144 return __this_address;
145 if (be16_to_cpu(block->bb_level) != level)
146 return __this_address;
147 if (be16_to_cpu(block->bb_numrecs) >
148 cur->bc_ops->get_maxrecs(cur, level))
149 return __this_address;
155 * Check a long btree block header. Return the address of the failing check,
156 * or NULL if everything is ok.
158 static xfs_failaddr_t
159 __xfs_btree_check_fsblock(
160 struct xfs_btree_cur *cur,
161 struct xfs_btree_block *block,
165 struct xfs_mount *mp = cur->bc_mp;
169 fa = __xfs_btree_check_lblock_hdr(cur, block, level, bp);
174 * For inode-rooted btrees, the root block sits in the inode fork. In
175 * that case bp is NULL, and the block must not have any siblings.
178 if (block->bb_u.l.bb_leftsib != cpu_to_be64(NULLFSBLOCK))
179 return __this_address;
180 if (block->bb_u.l.bb_rightsib != cpu_to_be64(NULLFSBLOCK))
181 return __this_address;
185 fsb = XFS_DADDR_TO_FSB(mp, xfs_buf_daddr(bp));
186 fa = xfs_btree_check_fsblock_siblings(mp, fsb,
187 block->bb_u.l.bb_leftsib);
189 fa = xfs_btree_check_fsblock_siblings(mp, fsb,
190 block->bb_u.l.bb_rightsib);
195 * Check an in-memory btree block header. Return the address of the failing
196 * check, or NULL if everything is ok.
198 static xfs_failaddr_t
199 __xfs_btree_check_memblock(
200 struct xfs_btree_cur *cur,
201 struct xfs_btree_block *block,
205 struct xfs_buftarg *btp = cur->bc_mem.xfbtree->target;
209 fa = __xfs_btree_check_lblock_hdr(cur, block, level, bp);
213 bno = xfs_daddr_to_xfbno(xfs_buf_daddr(bp));
214 fa = xfs_btree_check_memblock_siblings(btp, bno,
215 block->bb_u.l.bb_leftsib);
217 fa = xfs_btree_check_memblock_siblings(btp, bno,
218 block->bb_u.l.bb_rightsib);
223 * Check a short btree block header. Return the address of the failing check,
224 * or NULL if everything is ok.
226 static xfs_failaddr_t
227 __xfs_btree_check_agblock(
228 struct xfs_btree_cur *cur,
229 struct xfs_btree_block *block,
233 struct xfs_mount *mp = cur->bc_mp;
234 struct xfs_perag *pag = to_perag(cur->bc_group);
238 if (xfs_has_crc(mp)) {
239 if (!uuid_equal(&block->bb_u.s.bb_uuid, &mp->m_sb.sb_meta_uuid))
240 return __this_address;
241 if (block->bb_u.s.bb_blkno != cpu_to_be64(xfs_buf_daddr(bp)))
242 return __this_address;
245 if (be32_to_cpu(block->bb_magic) != xfs_btree_magic(mp, cur->bc_ops))
246 return __this_address;
247 if (be16_to_cpu(block->bb_level) != level)
248 return __this_address;
249 if (be16_to_cpu(block->bb_numrecs) >
250 cur->bc_ops->get_maxrecs(cur, level))
251 return __this_address;
253 agbno = xfs_daddr_to_agbno(mp, xfs_buf_daddr(bp));
254 fa = xfs_btree_check_agblock_siblings(pag, agbno,
255 block->bb_u.s.bb_leftsib);
257 fa = xfs_btree_check_agblock_siblings(pag, agbno,
258 block->bb_u.s.bb_rightsib);
263 * Internal btree block check.
265 * Return NULL if the block is ok or the address of the failed check otherwise.
268 __xfs_btree_check_block(
269 struct xfs_btree_cur *cur,
270 struct xfs_btree_block *block,
274 switch (cur->bc_ops->type) {
275 case XFS_BTREE_TYPE_MEM:
276 return __xfs_btree_check_memblock(cur, block, level, bp);
277 case XFS_BTREE_TYPE_AG:
278 return __xfs_btree_check_agblock(cur, block, level, bp);
279 case XFS_BTREE_TYPE_INODE:
280 return __xfs_btree_check_fsblock(cur, block, level, bp);
283 return __this_address;
287 static inline unsigned int xfs_btree_block_errtag(struct xfs_btree_cur *cur)
289 if (cur->bc_ops->ptr_len == XFS_BTREE_SHORT_PTR_LEN)
290 return XFS_ERRTAG_BTREE_CHECK_SBLOCK;
291 return XFS_ERRTAG_BTREE_CHECK_LBLOCK;
295 * Debug routine: check that block header is ok.
298 xfs_btree_check_block(
299 struct xfs_btree_cur *cur, /* btree cursor */
300 struct xfs_btree_block *block, /* generic btree block pointer */
301 int level, /* level of the btree block */
302 struct xfs_buf *bp) /* buffer containing block, if any */
304 struct xfs_mount *mp = cur->bc_mp;
307 fa = __xfs_btree_check_block(cur, block, level, bp);
308 if (XFS_IS_CORRUPT(mp, fa != NULL) ||
309 XFS_TEST_ERROR(false, mp, xfs_btree_block_errtag(cur))) {
311 trace_xfs_btree_corrupt(bp, _RET_IP_);
312 xfs_btree_mark_sick(cur);
313 return -EFSCORRUPTED;
319 __xfs_btree_check_ptr(
320 struct xfs_btree_cur *cur,
321 const union xfs_btree_ptr *ptr,
326 return -EFSCORRUPTED;
328 switch (cur->bc_ops->type) {
329 case XFS_BTREE_TYPE_MEM:
330 if (!xfbtree_verify_bno(cur->bc_mem.xfbtree,
331 be64_to_cpu((&ptr->l)[index])))
332 return -EFSCORRUPTED;
334 case XFS_BTREE_TYPE_INODE:
335 if (!xfs_verify_fsbno(cur->bc_mp,
336 be64_to_cpu((&ptr->l)[index])))
337 return -EFSCORRUPTED;
339 case XFS_BTREE_TYPE_AG:
340 if (!xfs_verify_agbno(to_perag(cur->bc_group),
341 be32_to_cpu((&ptr->s)[index])))
342 return -EFSCORRUPTED;
350 * Check that a given (indexed) btree pointer at a certain level of a
351 * btree is valid and doesn't point past where it should.
355 struct xfs_btree_cur *cur,
356 const union xfs_btree_ptr *ptr,
362 error = __xfs_btree_check_ptr(cur, ptr, index, level);
364 switch (cur->bc_ops->type) {
365 case XFS_BTREE_TYPE_MEM:
367 "In-memory: Corrupt %sbt flags 0x%x pointer at level %d index %d fa %pS.",
368 cur->bc_ops->name, cur->bc_flags, level, index,
371 case XFS_BTREE_TYPE_INODE:
373 "Inode %llu fork %d: Corrupt %sbt pointer at level %d index %d.",
374 cur->bc_ino.ip->i_ino,
375 cur->bc_ino.whichfork, cur->bc_ops->name,
378 case XFS_BTREE_TYPE_AG:
380 "AG %u: Corrupt %sbt pointer at level %d index %d.",
381 cur->bc_group->xg_gno, cur->bc_ops->name,
385 xfs_btree_mark_sick(cur);
392 # define xfs_btree_debug_check_ptr xfs_btree_check_ptr
394 # define xfs_btree_debug_check_ptr(...) (0)
398 * Calculate CRC on the whole btree block and stuff it into the
399 * long-form btree header.
401 * Prior to calculting the CRC, pull the LSN out of the buffer log item and put
402 * it into the buffer so recovery knows what the last modification was that made
406 xfs_btree_fsblock_calc_crc(
409 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
410 struct xfs_buf_log_item *bip = bp->b_log_item;
412 if (!xfs_has_crc(bp->b_mount))
415 block->bb_u.l.bb_lsn = cpu_to_be64(bip->bli_item.li_lsn);
416 xfs_buf_update_cksum(bp, XFS_BTREE_LBLOCK_CRC_OFF);
420 xfs_btree_fsblock_verify_crc(
423 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
424 struct xfs_mount *mp = bp->b_mount;
426 if (xfs_has_crc(mp)) {
427 if (!xfs_log_check_lsn(mp, be64_to_cpu(block->bb_u.l.bb_lsn)))
429 return xfs_buf_verify_cksum(bp, XFS_BTREE_LBLOCK_CRC_OFF);
436 * Calculate CRC on the whole btree block and stuff it into the
437 * short-form btree header.
439 * Prior to calculting the CRC, pull the LSN out of the buffer log item and put
440 * it into the buffer so recovery knows what the last modification was that made
444 xfs_btree_agblock_calc_crc(
447 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
448 struct xfs_buf_log_item *bip = bp->b_log_item;
450 if (!xfs_has_crc(bp->b_mount))
453 block->bb_u.s.bb_lsn = cpu_to_be64(bip->bli_item.li_lsn);
454 xfs_buf_update_cksum(bp, XFS_BTREE_SBLOCK_CRC_OFF);
458 xfs_btree_agblock_verify_crc(
461 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
462 struct xfs_mount *mp = bp->b_mount;
464 if (xfs_has_crc(mp)) {
465 if (!xfs_log_check_lsn(mp, be64_to_cpu(block->bb_u.s.bb_lsn)))
467 return xfs_buf_verify_cksum(bp, XFS_BTREE_SBLOCK_CRC_OFF);
474 xfs_btree_free_block(
475 struct xfs_btree_cur *cur,
480 trace_xfs_btree_free_block(cur, bp);
483 * Don't allow block freeing for a staging cursor, because staging
484 * cursors do not support regular btree modifications.
486 if (unlikely(cur->bc_flags & XFS_BTREE_STAGING)) {
488 return -EFSCORRUPTED;
491 error = cur->bc_ops->free_block(cur, bp);
493 xfs_trans_binval(cur->bc_tp, bp);
494 XFS_BTREE_STATS_INC(cur, free);
500 * Delete the btree cursor.
503 xfs_btree_del_cursor(
504 struct xfs_btree_cur *cur, /* btree cursor */
505 int error) /* del because of error */
507 int i; /* btree level */
510 * Clear the buffer pointers and release the buffers. If we're doing
511 * this because of an error, inspect all of the entries in the bc_bufs
512 * array for buffers to be unlocked. This is because some of the btree
513 * code works from level n down to 0, and if we get an error along the
514 * way we won't have initialized all the entries down to 0.
516 for (i = 0; i < cur->bc_nlevels; i++) {
517 if (cur->bc_levels[i].bp)
518 xfs_trans_brelse(cur->bc_tp, cur->bc_levels[i].bp);
524 * If we are doing a BMBT update, the number of unaccounted blocks
525 * allocated during this cursor life time should be zero. If it's not
526 * zero, then we should be shut down or on our way to shutdown due to
527 * cancelling a dirty transaction on error.
529 ASSERT(!xfs_btree_is_bmap(cur->bc_ops) || cur->bc_bmap.allocated == 0 ||
530 xfs_is_shutdown(cur->bc_mp) || error != 0);
533 xfs_group_put(cur->bc_group);
534 kmem_cache_free(cur->bc_cache, cur);
537 /* Return the buffer target for this btree's buffer. */
538 static inline struct xfs_buftarg *
540 struct xfs_btree_cur *cur)
542 if (cur->bc_ops->type == XFS_BTREE_TYPE_MEM)
543 return cur->bc_mem.xfbtree->target;
544 return cur->bc_mp->m_ddev_targp;
547 /* Return the block size (in units of 512b sectors) for this btree. */
548 static inline unsigned int
550 struct xfs_btree_cur *cur)
552 if (cur->bc_ops->type == XFS_BTREE_TYPE_MEM)
554 return cur->bc_mp->m_bsize;
558 * Duplicate the btree cursor.
559 * Allocate a new one, copy the record, re-get the buffers.
562 xfs_btree_dup_cursor(
563 struct xfs_btree_cur *cur, /* input cursor */
564 struct xfs_btree_cur **ncur) /* output cursor */
566 struct xfs_mount *mp = cur->bc_mp;
567 struct xfs_trans *tp = cur->bc_tp;
569 struct xfs_btree_cur *new;
574 * Don't allow staging cursors to be duplicated because they're supposed
575 * to be kept private to a single thread.
577 if (unlikely(cur->bc_flags & XFS_BTREE_STAGING)) {
579 return -EFSCORRUPTED;
583 * Allocate a new cursor like the old one.
585 new = cur->bc_ops->dup_cursor(cur);
588 * Copy the record currently in the cursor.
590 new->bc_rec = cur->bc_rec;
593 * For each level current, re-get the buffer and copy the ptr value.
595 for (i = 0; i < new->bc_nlevels; i++) {
596 new->bc_levels[i].ptr = cur->bc_levels[i].ptr;
597 new->bc_levels[i].ra = cur->bc_levels[i].ra;
598 bp = cur->bc_levels[i].bp;
600 error = xfs_trans_read_buf(mp, tp,
601 xfs_btree_buftarg(cur),
603 xfs_btree_bbsize(cur), 0, &bp,
604 cur->bc_ops->buf_ops);
605 if (xfs_metadata_is_sick(error))
606 xfs_btree_mark_sick(new);
608 xfs_btree_del_cursor(new, error);
613 new->bc_levels[i].bp = bp;
620 * XFS btree block layout and addressing:
622 * There are two types of blocks in the btree: leaf and non-leaf blocks.
624 * The leaf record start with a header then followed by records containing
625 * the values. A non-leaf block also starts with the same header, and
626 * then first contains lookup keys followed by an equal number of pointers
627 * to the btree blocks at the previous level.
629 * +--------+-------+-------+-------+-------+-------+-------+
630 * Leaf: | header | rec 1 | rec 2 | rec 3 | rec 4 | rec 5 | rec N |
631 * +--------+-------+-------+-------+-------+-------+-------+
633 * +--------+-------+-------+-------+-------+-------+-------+
634 * Non-Leaf: | header | key 1 | key 2 | key N | ptr 1 | ptr 2 | ptr N |
635 * +--------+-------+-------+-------+-------+-------+-------+
637 * The header is called struct xfs_btree_block for reasons better left unknown
638 * and comes in different versions for short (32bit) and long (64bit) block
639 * pointers. The record and key structures are defined by the btree instances
640 * and opaque to the btree core. The block pointers are simple disk endian
641 * integers, available in a short (32bit) and long (64bit) variant.
643 * The helpers below calculate the offset of a given record, key or pointer
644 * into a btree block (xfs_btree_*_offset) or return a pointer to the given
645 * record, key or pointer (xfs_btree_*_addr). Note that all addressing
646 * inside the btree block is done using indices starting at one, not zero!
648 * If XFS_BTGEO_OVERLAPPING is set, then this btree supports keys containing
649 * overlapping intervals. In such a tree, records are still sorted lowest to
650 * highest and indexed by the smallest key value that refers to the record.
651 * However, nodes are different: each pointer has two associated keys -- one
652 * indexing the lowest key available in the block(s) below (the same behavior
653 * as the key in a regular btree) and another indexing the highest key
654 * available in the block(s) below. Because records are /not/ sorted by the
655 * highest key, all leaf block updates require us to compute the highest key
656 * that matches any record in the leaf and to recursively update the high keys
657 * in the nodes going further up in the tree, if necessary. Nodes look like
660 * +--------+-----+-----+-----+-----+-----+-------+-------+-----+
661 * Non-Leaf: | header | lo1 | hi1 | lo2 | hi2 | ... | ptr 1 | ptr 2 | ... |
662 * +--------+-----+-----+-----+-----+-----+-------+-------+-----+
664 * To perform an interval query on an overlapped tree, perform the usual
665 * depth-first search and use the low and high keys to decide if we can skip
666 * that particular node. If a leaf node is reached, return the records that
667 * intersect the interval. Note that an interval query may return numerous
668 * entries. For a non-overlapped tree, simply search for the record associated
669 * with the lowest key and iterate forward until a non-matching record is
670 * found. Section 14.3 ("Interval Trees") of _Introduction to Algorithms_ by
671 * Cormen, Leiserson, Rivest, and Stein (2nd or 3rd ed. only) discuss this in
674 * Why do we care about overlapping intervals? Let's say you have a bunch of
675 * reverse mapping records on a reflink filesystem:
677 * 1: +- file A startblock B offset C length D -----------+
678 * 2: +- file E startblock F offset G length H --------------+
679 * 3: +- file I startblock F offset J length K --+
680 * 4: +- file L... --+
682 * Now say we want to map block (B+D) into file A at offset (C+D). Ideally,
683 * we'd simply increment the length of record 1. But how do we find the record
684 * that ends at (B+D-1) (i.e. record 1)? A LE lookup of (B+D-1) would return
685 * record 3 because the keys are ordered first by startblock. An interval
686 * query would return records 1 and 2 because they both overlap (B+D-1), and
687 * from that we can pick out record 1 as the appropriate left neighbor.
689 * In the non-overlapped case you can do a LE lookup and decrement the cursor
690 * because a record's interval must end before the next record.
694 * Return size of the btree block header for this btree instance.
696 static inline size_t xfs_btree_block_len(struct xfs_btree_cur *cur)
698 if (cur->bc_ops->ptr_len == XFS_BTREE_LONG_PTR_LEN) {
699 if (xfs_has_crc(cur->bc_mp))
700 return XFS_BTREE_LBLOCK_CRC_LEN;
701 return XFS_BTREE_LBLOCK_LEN;
703 if (xfs_has_crc(cur->bc_mp))
704 return XFS_BTREE_SBLOCK_CRC_LEN;
705 return XFS_BTREE_SBLOCK_LEN;
709 * Calculate offset of the n-th record in a btree block.
712 xfs_btree_rec_offset(
713 struct xfs_btree_cur *cur,
716 return xfs_btree_block_len(cur) +
717 (n - 1) * cur->bc_ops->rec_len;
721 * Calculate offset of the n-th key in a btree block.
724 xfs_btree_key_offset(
725 struct xfs_btree_cur *cur,
728 return xfs_btree_block_len(cur) +
729 (n - 1) * cur->bc_ops->key_len;
733 * Calculate offset of the n-th high key in a btree block.
736 xfs_btree_high_key_offset(
737 struct xfs_btree_cur *cur,
740 return xfs_btree_block_len(cur) +
741 (n - 1) * cur->bc_ops->key_len + (cur->bc_ops->key_len / 2);
745 * Calculate offset of the n-th block pointer in a btree block.
748 xfs_btree_ptr_offset(
749 struct xfs_btree_cur *cur,
753 return xfs_btree_block_len(cur) +
754 cur->bc_ops->get_maxrecs(cur, level) * cur->bc_ops->key_len +
755 (n - 1) * cur->bc_ops->ptr_len;
759 * Return a pointer to the n-th record in the btree block.
761 union xfs_btree_rec *
763 struct xfs_btree_cur *cur,
765 struct xfs_btree_block *block)
767 return (union xfs_btree_rec *)
768 ((char *)block + xfs_btree_rec_offset(cur, n));
772 * Return a pointer to the n-th key in the btree block.
774 union xfs_btree_key *
776 struct xfs_btree_cur *cur,
778 struct xfs_btree_block *block)
780 return (union xfs_btree_key *)
781 ((char *)block + xfs_btree_key_offset(cur, n));
785 * Return a pointer to the n-th high key in the btree block.
787 union xfs_btree_key *
788 xfs_btree_high_key_addr(
789 struct xfs_btree_cur *cur,
791 struct xfs_btree_block *block)
793 return (union xfs_btree_key *)
794 ((char *)block + xfs_btree_high_key_offset(cur, n));
798 * Return a pointer to the n-th block pointer in the btree block.
800 union xfs_btree_ptr *
802 struct xfs_btree_cur *cur,
804 struct xfs_btree_block *block)
806 int level = xfs_btree_get_level(block);
808 ASSERT(block->bb_level != 0);
810 return (union xfs_btree_ptr *)
811 ((char *)block + xfs_btree_ptr_offset(cur, n, level));
816 struct xfs_btree_cur *cur)
818 ASSERT(cur->bc_ops->type == XFS_BTREE_TYPE_INODE);
820 if (cur->bc_flags & XFS_BTREE_STAGING)
821 return cur->bc_ino.ifake->if_fork;
822 return xfs_ifork_ptr(cur->bc_ino.ip, cur->bc_ino.whichfork);
826 * Get the root block which is stored in the inode.
828 * For now this btree implementation assumes the btree root is always
829 * stored in the if_broot field of an inode fork.
831 STATIC struct xfs_btree_block *
833 struct xfs_btree_cur *cur)
835 struct xfs_ifork *ifp = xfs_btree_ifork_ptr(cur);
837 return (struct xfs_btree_block *)ifp->if_broot;
841 * Retrieve the block pointer from the cursor at the given level.
842 * This may be an inode btree root or from a buffer.
844 struct xfs_btree_block * /* generic btree block pointer */
846 struct xfs_btree_cur *cur, /* btree cursor */
847 int level, /* level in btree */
848 struct xfs_buf **bpp) /* buffer containing the block */
850 if (xfs_btree_at_iroot(cur, level)) {
852 return xfs_btree_get_iroot(cur);
855 *bpp = cur->bc_levels[level].bp;
856 return XFS_BUF_TO_BLOCK(*bpp);
860 * Change the cursor to point to the first record at the given level.
861 * Other levels are unaffected.
863 STATIC int /* success=1, failure=0 */
865 struct xfs_btree_cur *cur, /* btree cursor */
866 int level) /* level to change */
868 struct xfs_btree_block *block; /* generic btree block pointer */
869 struct xfs_buf *bp; /* buffer containing block */
872 * Get the block pointer for this level.
874 block = xfs_btree_get_block(cur, level, &bp);
875 if (xfs_btree_check_block(cur, block, level, bp))
878 * It's empty, there is no such record.
880 if (!block->bb_numrecs)
883 * Set the ptr value to 1, that's the first record/key.
885 cur->bc_levels[level].ptr = 1;
890 * Change the cursor to point to the last record in the current block
891 * at the given level. Other levels are unaffected.
893 STATIC int /* success=1, failure=0 */
895 struct xfs_btree_cur *cur, /* btree cursor */
896 int level) /* level to change */
898 struct xfs_btree_block *block; /* generic btree block pointer */
899 struct xfs_buf *bp; /* buffer containing block */
902 * Get the block pointer for this level.
904 block = xfs_btree_get_block(cur, level, &bp);
905 if (xfs_btree_check_block(cur, block, level, bp))
908 * It's empty, there is no such record.
910 if (!block->bb_numrecs)
913 * Set the ptr value to numrecs, that's the last record/key.
915 cur->bc_levels[level].ptr = be16_to_cpu(block->bb_numrecs);
920 * Compute first and last byte offsets for the fields given.
921 * Interprets the offsets table, which contains struct field offsets.
925 uint32_t fields, /* bitmask of fields */
926 const short *offsets, /* table of field offsets */
927 int nbits, /* number of bits to inspect */
928 int *first, /* output: first byte offset */
929 int *last) /* output: last byte offset */
931 int i; /* current bit number */
932 uint32_t imask; /* mask for current bit number */
936 * Find the lowest bit, so the first byte offset.
938 for (i = 0, imask = 1u; ; i++, imask <<= 1) {
939 if (imask & fields) {
945 * Find the highest bit, so the last byte offset.
947 for (i = nbits - 1, imask = 1u << i; ; i--, imask >>= 1) {
948 if (imask & fields) {
949 *last = offsets[i + 1] - 1;
956 xfs_btree_readahead_fsblock(
957 struct xfs_btree_cur *cur,
959 struct xfs_btree_block *block)
961 struct xfs_mount *mp = cur->bc_mp;
962 xfs_fsblock_t left = be64_to_cpu(block->bb_u.l.bb_leftsib);
963 xfs_fsblock_t right = be64_to_cpu(block->bb_u.l.bb_rightsib);
966 if ((lr & XFS_BTCUR_LEFTRA) && left != NULLFSBLOCK) {
967 xfs_buf_readahead(mp->m_ddev_targp, XFS_FSB_TO_DADDR(mp, left),
968 mp->m_bsize, cur->bc_ops->buf_ops);
972 if ((lr & XFS_BTCUR_RIGHTRA) && right != NULLFSBLOCK) {
973 xfs_buf_readahead(mp->m_ddev_targp, XFS_FSB_TO_DADDR(mp, right),
974 mp->m_bsize, cur->bc_ops->buf_ops);
982 xfs_btree_readahead_memblock(
983 struct xfs_btree_cur *cur,
985 struct xfs_btree_block *block)
987 struct xfs_buftarg *btp = cur->bc_mem.xfbtree->target;
988 xfbno_t left = be64_to_cpu(block->bb_u.l.bb_leftsib);
989 xfbno_t right = be64_to_cpu(block->bb_u.l.bb_rightsib);
992 if ((lr & XFS_BTCUR_LEFTRA) && left != NULLFSBLOCK) {
993 xfs_buf_readahead(btp, xfbno_to_daddr(left), XFBNO_BBSIZE,
994 cur->bc_ops->buf_ops);
998 if ((lr & XFS_BTCUR_RIGHTRA) && right != NULLFSBLOCK) {
999 xfs_buf_readahead(btp, xfbno_to_daddr(right), XFBNO_BBSIZE,
1000 cur->bc_ops->buf_ops);
1008 xfs_btree_readahead_agblock(
1009 struct xfs_btree_cur *cur,
1011 struct xfs_btree_block *block)
1013 struct xfs_mount *mp = cur->bc_mp;
1014 struct xfs_perag *pag = to_perag(cur->bc_group);
1015 xfs_agblock_t left = be32_to_cpu(block->bb_u.s.bb_leftsib);
1016 xfs_agblock_t right = be32_to_cpu(block->bb_u.s.bb_rightsib);
1019 if ((lr & XFS_BTCUR_LEFTRA) && left != NULLAGBLOCK) {
1020 xfs_buf_readahead(mp->m_ddev_targp,
1021 xfs_agbno_to_daddr(pag, left), mp->m_bsize,
1022 cur->bc_ops->buf_ops);
1026 if ((lr & XFS_BTCUR_RIGHTRA) && right != NULLAGBLOCK) {
1027 xfs_buf_readahead(mp->m_ddev_targp,
1028 xfs_agbno_to_daddr(pag, right), mp->m_bsize,
1029 cur->bc_ops->buf_ops);
1037 * Read-ahead btree blocks, at the given level.
1038 * Bits in lr are set from XFS_BTCUR_{LEFT,RIGHT}RA.
1041 xfs_btree_readahead(
1042 struct xfs_btree_cur *cur, /* btree cursor */
1043 int lev, /* level in btree */
1044 int lr) /* left/right bits */
1046 struct xfs_btree_block *block;
1049 * No readahead needed if we are at the root level and the
1050 * btree root is stored in the inode.
1052 if (xfs_btree_at_iroot(cur, lev))
1055 if ((cur->bc_levels[lev].ra | lr) == cur->bc_levels[lev].ra)
1058 cur->bc_levels[lev].ra |= lr;
1059 block = XFS_BUF_TO_BLOCK(cur->bc_levels[lev].bp);
1061 switch (cur->bc_ops->type) {
1062 case XFS_BTREE_TYPE_AG:
1063 return xfs_btree_readahead_agblock(cur, lr, block);
1064 case XFS_BTREE_TYPE_INODE:
1065 return xfs_btree_readahead_fsblock(cur, lr, block);
1066 case XFS_BTREE_TYPE_MEM:
1067 return xfs_btree_readahead_memblock(cur, lr, block);
1075 xfs_btree_ptr_to_daddr(
1076 struct xfs_btree_cur *cur,
1077 const union xfs_btree_ptr *ptr,
1082 error = xfs_btree_check_ptr(cur, ptr, 0, 1);
1086 switch (cur->bc_ops->type) {
1087 case XFS_BTREE_TYPE_AG:
1088 *daddr = xfs_agbno_to_daddr(to_perag(cur->bc_group),
1089 be32_to_cpu(ptr->s));
1091 case XFS_BTREE_TYPE_INODE:
1092 *daddr = XFS_FSB_TO_DADDR(cur->bc_mp, be64_to_cpu(ptr->l));
1094 case XFS_BTREE_TYPE_MEM:
1095 *daddr = xfbno_to_daddr(be64_to_cpu(ptr->l));
1102 * Readahead @count btree blocks at the given @ptr location.
1104 * We don't need to care about long or short form btrees here as we have a
1105 * method of converting the ptr directly to a daddr available to us.
1108 xfs_btree_readahead_ptr(
1109 struct xfs_btree_cur *cur,
1110 union xfs_btree_ptr *ptr,
1115 if (xfs_btree_ptr_to_daddr(cur, ptr, &daddr))
1117 xfs_buf_readahead(xfs_btree_buftarg(cur), daddr,
1118 xfs_btree_bbsize(cur) * count,
1119 cur->bc_ops->buf_ops);
1123 * Set the buffer for level "lev" in the cursor to bp, releasing
1124 * any previous buffer.
1128 struct xfs_btree_cur *cur, /* btree cursor */
1129 int lev, /* level in btree */
1130 struct xfs_buf *bp) /* new buffer to set */
1132 struct xfs_btree_block *b; /* btree block */
1134 if (cur->bc_levels[lev].bp)
1135 xfs_trans_brelse(cur->bc_tp, cur->bc_levels[lev].bp);
1136 cur->bc_levels[lev].bp = bp;
1137 cur->bc_levels[lev].ra = 0;
1139 b = XFS_BUF_TO_BLOCK(bp);
1140 if (cur->bc_ops->ptr_len == XFS_BTREE_LONG_PTR_LEN) {
1141 if (b->bb_u.l.bb_leftsib == cpu_to_be64(NULLFSBLOCK))
1142 cur->bc_levels[lev].ra |= XFS_BTCUR_LEFTRA;
1143 if (b->bb_u.l.bb_rightsib == cpu_to_be64(NULLFSBLOCK))
1144 cur->bc_levels[lev].ra |= XFS_BTCUR_RIGHTRA;
1146 if (b->bb_u.s.bb_leftsib == cpu_to_be32(NULLAGBLOCK))
1147 cur->bc_levels[lev].ra |= XFS_BTCUR_LEFTRA;
1148 if (b->bb_u.s.bb_rightsib == cpu_to_be32(NULLAGBLOCK))
1149 cur->bc_levels[lev].ra |= XFS_BTCUR_RIGHTRA;
1154 xfs_btree_ptr_is_null(
1155 struct xfs_btree_cur *cur,
1156 const union xfs_btree_ptr *ptr)
1158 if (cur->bc_ops->ptr_len == XFS_BTREE_LONG_PTR_LEN)
1159 return ptr->l == cpu_to_be64(NULLFSBLOCK);
1161 return ptr->s == cpu_to_be32(NULLAGBLOCK);
1165 xfs_btree_set_ptr_null(
1166 struct xfs_btree_cur *cur,
1167 union xfs_btree_ptr *ptr)
1169 if (cur->bc_ops->ptr_len == XFS_BTREE_LONG_PTR_LEN)
1170 ptr->l = cpu_to_be64(NULLFSBLOCK);
1172 ptr->s = cpu_to_be32(NULLAGBLOCK);
1176 xfs_btree_ptrs_equal(
1177 struct xfs_btree_cur *cur,
1178 union xfs_btree_ptr *ptr1,
1179 union xfs_btree_ptr *ptr2)
1181 if (cur->bc_ops->ptr_len == XFS_BTREE_LONG_PTR_LEN)
1182 return ptr1->l == ptr2->l;
1183 return ptr1->s == ptr2->s;
1187 * Get/set/init sibling pointers
1190 xfs_btree_get_sibling(
1191 struct xfs_btree_cur *cur,
1192 struct xfs_btree_block *block,
1193 union xfs_btree_ptr *ptr,
1196 ASSERT(lr == XFS_BB_LEFTSIB || lr == XFS_BB_RIGHTSIB);
1198 if (cur->bc_ops->ptr_len == XFS_BTREE_LONG_PTR_LEN) {
1199 if (lr == XFS_BB_RIGHTSIB)
1200 ptr->l = block->bb_u.l.bb_rightsib;
1202 ptr->l = block->bb_u.l.bb_leftsib;
1204 if (lr == XFS_BB_RIGHTSIB)
1205 ptr->s = block->bb_u.s.bb_rightsib;
1207 ptr->s = block->bb_u.s.bb_leftsib;
1212 xfs_btree_set_sibling(
1213 struct xfs_btree_cur *cur,
1214 struct xfs_btree_block *block,
1215 const union xfs_btree_ptr *ptr,
1218 ASSERT(lr == XFS_BB_LEFTSIB || lr == XFS_BB_RIGHTSIB);
1220 if (cur->bc_ops->ptr_len == XFS_BTREE_LONG_PTR_LEN) {
1221 if (lr == XFS_BB_RIGHTSIB)
1222 block->bb_u.l.bb_rightsib = ptr->l;
1224 block->bb_u.l.bb_leftsib = ptr->l;
1226 if (lr == XFS_BB_RIGHTSIB)
1227 block->bb_u.s.bb_rightsib = ptr->s;
1229 block->bb_u.s.bb_leftsib = ptr->s;
1234 __xfs_btree_init_block(
1235 struct xfs_mount *mp,
1236 struct xfs_btree_block *buf,
1237 const struct xfs_btree_ops *ops,
1243 bool crc = xfs_has_crc(mp);
1244 __u32 magic = xfs_btree_magic(mp, ops);
1246 buf->bb_magic = cpu_to_be32(magic);
1247 buf->bb_level = cpu_to_be16(level);
1248 buf->bb_numrecs = cpu_to_be16(numrecs);
1250 if (ops->ptr_len == XFS_BTREE_LONG_PTR_LEN) {
1251 buf->bb_u.l.bb_leftsib = cpu_to_be64(NULLFSBLOCK);
1252 buf->bb_u.l.bb_rightsib = cpu_to_be64(NULLFSBLOCK);
1254 buf->bb_u.l.bb_blkno = cpu_to_be64(blkno);
1255 buf->bb_u.l.bb_owner = cpu_to_be64(owner);
1256 uuid_copy(&buf->bb_u.l.bb_uuid, &mp->m_sb.sb_meta_uuid);
1257 buf->bb_u.l.bb_pad = 0;
1258 buf->bb_u.l.bb_lsn = 0;
1261 buf->bb_u.s.bb_leftsib = cpu_to_be32(NULLAGBLOCK);
1262 buf->bb_u.s.bb_rightsib = cpu_to_be32(NULLAGBLOCK);
1264 buf->bb_u.s.bb_blkno = cpu_to_be64(blkno);
1265 /* owner is a 32 bit value on short blocks */
1266 buf->bb_u.s.bb_owner = cpu_to_be32((__u32)owner);
1267 uuid_copy(&buf->bb_u.s.bb_uuid, &mp->m_sb.sb_meta_uuid);
1268 buf->bb_u.s.bb_lsn = 0;
1274 xfs_btree_init_block(
1275 struct xfs_mount *mp,
1276 struct xfs_btree_block *block,
1277 const struct xfs_btree_ops *ops,
1282 __xfs_btree_init_block(mp, block, ops, XFS_BUF_DADDR_NULL, level,
1288 struct xfs_mount *mp,
1290 const struct xfs_btree_ops *ops,
1295 __xfs_btree_init_block(mp, XFS_BUF_TO_BLOCK(bp), ops,
1296 xfs_buf_daddr(bp), level, numrecs, owner);
1297 bp->b_ops = ops->buf_ops;
1302 struct xfs_btree_cur *cur)
1304 switch (cur->bc_ops->type) {
1305 case XFS_BTREE_TYPE_MEM:
1306 return cur->bc_mem.xfbtree->owner;
1307 case XFS_BTREE_TYPE_INODE:
1308 return cur->bc_ino.ip->i_ino;
1309 case XFS_BTREE_TYPE_AG:
1310 return cur->bc_group->xg_gno;
1318 xfs_btree_init_block_cur(
1319 struct xfs_btree_cur *cur,
1324 xfs_btree_init_buf(cur->bc_mp, bp, cur->bc_ops, level, numrecs,
1325 xfs_btree_owner(cur));
1329 xfs_btree_buf_to_ptr(
1330 struct xfs_btree_cur *cur,
1332 union xfs_btree_ptr *ptr)
1334 switch (cur->bc_ops->type) {
1335 case XFS_BTREE_TYPE_AG:
1336 ptr->s = cpu_to_be32(xfs_daddr_to_agbno(cur->bc_mp,
1337 xfs_buf_daddr(bp)));
1339 case XFS_BTREE_TYPE_INODE:
1340 ptr->l = cpu_to_be64(XFS_DADDR_TO_FSB(cur->bc_mp,
1341 xfs_buf_daddr(bp)));
1343 case XFS_BTREE_TYPE_MEM:
1344 ptr->l = cpu_to_be64(xfs_daddr_to_xfbno(xfs_buf_daddr(bp)));
1351 struct xfs_btree_cur *cur,
1354 xfs_buf_set_ref(bp, cur->bc_ops->lru_refs);
1358 xfs_btree_get_buf_block(
1359 struct xfs_btree_cur *cur,
1360 const union xfs_btree_ptr *ptr,
1361 struct xfs_btree_block **block,
1362 struct xfs_buf **bpp)
1367 error = xfs_btree_ptr_to_daddr(cur, ptr, &d);
1370 error = xfs_trans_get_buf(cur->bc_tp, xfs_btree_buftarg(cur), d,
1371 xfs_btree_bbsize(cur), 0, bpp);
1375 (*bpp)->b_ops = cur->bc_ops->buf_ops;
1376 *block = XFS_BUF_TO_BLOCK(*bpp);
1381 * Read in the buffer at the given ptr and return the buffer and
1382 * the block pointer within the buffer.
1385 xfs_btree_read_buf_block(
1386 struct xfs_btree_cur *cur,
1387 const union xfs_btree_ptr *ptr,
1389 struct xfs_btree_block **block,
1390 struct xfs_buf **bpp)
1392 struct xfs_mount *mp = cur->bc_mp;
1396 /* need to sort out how callers deal with failures first */
1397 ASSERT(!(flags & XBF_TRYLOCK));
1399 error = xfs_btree_ptr_to_daddr(cur, ptr, &d);
1402 error = xfs_trans_read_buf(mp, cur->bc_tp, xfs_btree_buftarg(cur), d,
1403 xfs_btree_bbsize(cur), flags, bpp,
1404 cur->bc_ops->buf_ops);
1405 if (xfs_metadata_is_sick(error))
1406 xfs_btree_mark_sick(cur);
1410 xfs_btree_set_refs(cur, *bpp);
1411 *block = XFS_BUF_TO_BLOCK(*bpp);
1416 * Copy keys from one btree block to another.
1419 xfs_btree_copy_keys(
1420 struct xfs_btree_cur *cur,
1421 union xfs_btree_key *dst_key,
1422 const union xfs_btree_key *src_key,
1425 ASSERT(numkeys >= 0);
1426 memcpy(dst_key, src_key, numkeys * cur->bc_ops->key_len);
1430 * Copy records from one btree block to another.
1433 xfs_btree_copy_recs(
1434 struct xfs_btree_cur *cur,
1435 union xfs_btree_rec *dst_rec,
1436 union xfs_btree_rec *src_rec,
1439 ASSERT(numrecs >= 0);
1440 memcpy(dst_rec, src_rec, numrecs * cur->bc_ops->rec_len);
1444 * Copy block pointers from one btree block to another.
1447 xfs_btree_copy_ptrs(
1448 struct xfs_btree_cur *cur,
1449 union xfs_btree_ptr *dst_ptr,
1450 const union xfs_btree_ptr *src_ptr,
1453 ASSERT(numptrs >= 0);
1454 memcpy(dst_ptr, src_ptr, numptrs * cur->bc_ops->ptr_len);
1458 * Shift keys one index left/right inside a single btree block.
1461 xfs_btree_shift_keys(
1462 struct xfs_btree_cur *cur,
1463 union xfs_btree_key *key,
1469 ASSERT(numkeys >= 0);
1470 ASSERT(dir == 1 || dir == -1);
1472 dst_key = (char *)key + (dir * cur->bc_ops->key_len);
1473 memmove(dst_key, key, numkeys * cur->bc_ops->key_len);
1477 * Shift records one index left/right inside a single btree block.
1480 xfs_btree_shift_recs(
1481 struct xfs_btree_cur *cur,
1482 union xfs_btree_rec *rec,
1488 ASSERT(numrecs >= 0);
1489 ASSERT(dir == 1 || dir == -1);
1491 dst_rec = (char *)rec + (dir * cur->bc_ops->rec_len);
1492 memmove(dst_rec, rec, numrecs * cur->bc_ops->rec_len);
1496 * Shift block pointers one index left/right inside a single btree block.
1499 xfs_btree_shift_ptrs(
1500 struct xfs_btree_cur *cur,
1501 union xfs_btree_ptr *ptr,
1507 ASSERT(numptrs >= 0);
1508 ASSERT(dir == 1 || dir == -1);
1510 dst_ptr = (char *)ptr + (dir * cur->bc_ops->ptr_len);
1511 memmove(dst_ptr, ptr, numptrs * cur->bc_ops->ptr_len);
1515 * Log key values from the btree block.
1519 struct xfs_btree_cur *cur,
1526 xfs_trans_buf_set_type(cur->bc_tp, bp, XFS_BLFT_BTREE_BUF);
1527 xfs_trans_log_buf(cur->bc_tp, bp,
1528 xfs_btree_key_offset(cur, first),
1529 xfs_btree_key_offset(cur, last + 1) - 1);
1531 xfs_trans_log_inode(cur->bc_tp, cur->bc_ino.ip,
1532 xfs_ilog_fbroot(cur->bc_ino.whichfork));
1537 * Log record values from the btree block.
1541 struct xfs_btree_cur *cur,
1547 xfs_trans_log_inode(cur->bc_tp, cur->bc_ino.ip,
1548 xfs_ilog_fbroot(cur->bc_ino.whichfork));
1552 xfs_trans_buf_set_type(cur->bc_tp, bp, XFS_BLFT_BTREE_BUF);
1553 xfs_trans_log_buf(cur->bc_tp, bp,
1554 xfs_btree_rec_offset(cur, first),
1555 xfs_btree_rec_offset(cur, last + 1) - 1);
1559 * Log block pointer fields from a btree block (nonleaf).
1563 struct xfs_btree_cur *cur, /* btree cursor */
1564 struct xfs_buf *bp, /* buffer containing btree block */
1565 int first, /* index of first pointer to log */
1566 int last) /* index of last pointer to log */
1570 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
1571 int level = xfs_btree_get_level(block);
1573 xfs_trans_buf_set_type(cur->bc_tp, bp, XFS_BLFT_BTREE_BUF);
1574 xfs_trans_log_buf(cur->bc_tp, bp,
1575 xfs_btree_ptr_offset(cur, first, level),
1576 xfs_btree_ptr_offset(cur, last + 1, level) - 1);
1578 xfs_trans_log_inode(cur->bc_tp, cur->bc_ino.ip,
1579 xfs_ilog_fbroot(cur->bc_ino.whichfork));
1585 * Log fields from a btree block header.
1588 xfs_btree_log_block(
1589 struct xfs_btree_cur *cur, /* btree cursor */
1590 struct xfs_buf *bp, /* buffer containing btree block */
1591 uint32_t fields) /* mask of fields: XFS_BB_... */
1593 int first; /* first byte offset logged */
1594 int last; /* last byte offset logged */
1595 static const short soffsets[] = { /* table of offsets (short) */
1596 offsetof(struct xfs_btree_block, bb_magic),
1597 offsetof(struct xfs_btree_block, bb_level),
1598 offsetof(struct xfs_btree_block, bb_numrecs),
1599 offsetof(struct xfs_btree_block, bb_u.s.bb_leftsib),
1600 offsetof(struct xfs_btree_block, bb_u.s.bb_rightsib),
1601 offsetof(struct xfs_btree_block, bb_u.s.bb_blkno),
1602 offsetof(struct xfs_btree_block, bb_u.s.bb_lsn),
1603 offsetof(struct xfs_btree_block, bb_u.s.bb_uuid),
1604 offsetof(struct xfs_btree_block, bb_u.s.bb_owner),
1605 offsetof(struct xfs_btree_block, bb_u.s.bb_crc),
1606 XFS_BTREE_SBLOCK_CRC_LEN
1608 static const short loffsets[] = { /* table of offsets (long) */
1609 offsetof(struct xfs_btree_block, bb_magic),
1610 offsetof(struct xfs_btree_block, bb_level),
1611 offsetof(struct xfs_btree_block, bb_numrecs),
1612 offsetof(struct xfs_btree_block, bb_u.l.bb_leftsib),
1613 offsetof(struct xfs_btree_block, bb_u.l.bb_rightsib),
1614 offsetof(struct xfs_btree_block, bb_u.l.bb_blkno),
1615 offsetof(struct xfs_btree_block, bb_u.l.bb_lsn),
1616 offsetof(struct xfs_btree_block, bb_u.l.bb_uuid),
1617 offsetof(struct xfs_btree_block, bb_u.l.bb_owner),
1618 offsetof(struct xfs_btree_block, bb_u.l.bb_crc),
1619 offsetof(struct xfs_btree_block, bb_u.l.bb_pad),
1620 XFS_BTREE_LBLOCK_CRC_LEN
1626 if (xfs_has_crc(cur->bc_mp)) {
1628 * We don't log the CRC when updating a btree
1629 * block but instead recreate it during log
1630 * recovery. As the log buffers have checksums
1631 * of their own this is safe and avoids logging a crc
1632 * update in a lot of places.
1634 if (fields == XFS_BB_ALL_BITS)
1635 fields = XFS_BB_ALL_BITS_CRC;
1636 nbits = XFS_BB_NUM_BITS_CRC;
1638 nbits = XFS_BB_NUM_BITS;
1640 xfs_btree_offsets(fields,
1641 (cur->bc_ops->ptr_len == XFS_BTREE_LONG_PTR_LEN) ?
1642 loffsets : soffsets,
1643 nbits, &first, &last);
1644 xfs_trans_buf_set_type(cur->bc_tp, bp, XFS_BLFT_BTREE_BUF);
1645 xfs_trans_log_buf(cur->bc_tp, bp, first, last);
1647 xfs_trans_log_inode(cur->bc_tp, cur->bc_ino.ip,
1648 xfs_ilog_fbroot(cur->bc_ino.whichfork));
1653 * Increment cursor by one record at the level.
1654 * For nonzero levels the leaf-ward information is untouched.
1657 xfs_btree_increment(
1658 struct xfs_btree_cur *cur,
1660 int *stat) /* success/failure */
1662 struct xfs_btree_block *block;
1663 union xfs_btree_ptr ptr;
1665 int error; /* error return value */
1668 ASSERT(level < cur->bc_nlevels);
1670 /* Read-ahead to the right at this level. */
1671 xfs_btree_readahead(cur, level, XFS_BTCUR_RIGHTRA);
1673 /* Get a pointer to the btree block. */
1674 block = xfs_btree_get_block(cur, level, &bp);
1677 error = xfs_btree_check_block(cur, block, level, bp);
1682 /* We're done if we remain in the block after the increment. */
1683 if (++cur->bc_levels[level].ptr <= xfs_btree_get_numrecs(block))
1686 /* Fail if we just went off the right edge of the tree. */
1687 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_RIGHTSIB);
1688 if (xfs_btree_ptr_is_null(cur, &ptr))
1691 XFS_BTREE_STATS_INC(cur, increment);
1694 * March up the tree incrementing pointers.
1695 * Stop when we don't go off the right edge of a block.
1697 for (lev = level + 1; lev < cur->bc_nlevels; lev++) {
1698 block = xfs_btree_get_block(cur, lev, &bp);
1701 error = xfs_btree_check_block(cur, block, lev, bp);
1706 if (++cur->bc_levels[lev].ptr <= xfs_btree_get_numrecs(block))
1709 /* Read-ahead the right block for the next loop. */
1710 xfs_btree_readahead(cur, lev, XFS_BTCUR_RIGHTRA);
1714 * If we went off the root then we are either seriously
1715 * confused or have the tree root in an inode.
1717 if (lev == cur->bc_nlevels) {
1718 if (cur->bc_ops->type == XFS_BTREE_TYPE_INODE)
1721 xfs_btree_mark_sick(cur);
1722 error = -EFSCORRUPTED;
1725 ASSERT(lev < cur->bc_nlevels);
1728 * Now walk back down the tree, fixing up the cursor's buffer
1729 * pointers and key numbers.
1731 for (block = xfs_btree_get_block(cur, lev, &bp); lev > level; ) {
1732 union xfs_btree_ptr *ptrp;
1734 ptrp = xfs_btree_ptr_addr(cur, cur->bc_levels[lev].ptr, block);
1736 error = xfs_btree_read_buf_block(cur, ptrp, 0, &block, &bp);
1740 xfs_btree_setbuf(cur, lev, bp);
1741 cur->bc_levels[lev].ptr = 1;
1756 * Decrement cursor by one record at the level.
1757 * For nonzero levels the leaf-ward information is untouched.
1760 xfs_btree_decrement(
1761 struct xfs_btree_cur *cur,
1763 int *stat) /* success/failure */
1765 struct xfs_btree_block *block;
1767 int error; /* error return value */
1769 union xfs_btree_ptr ptr;
1771 ASSERT(level < cur->bc_nlevels);
1773 /* Read-ahead to the left at this level. */
1774 xfs_btree_readahead(cur, level, XFS_BTCUR_LEFTRA);
1776 /* We're done if we remain in the block after the decrement. */
1777 if (--cur->bc_levels[level].ptr > 0)
1780 /* Get a pointer to the btree block. */
1781 block = xfs_btree_get_block(cur, level, &bp);
1784 error = xfs_btree_check_block(cur, block, level, bp);
1789 /* Fail if we just went off the left edge of the tree. */
1790 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_LEFTSIB);
1791 if (xfs_btree_ptr_is_null(cur, &ptr))
1794 XFS_BTREE_STATS_INC(cur, decrement);
1797 * March up the tree decrementing pointers.
1798 * Stop when we don't go off the left edge of a block.
1800 for (lev = level + 1; lev < cur->bc_nlevels; lev++) {
1801 if (--cur->bc_levels[lev].ptr > 0)
1803 /* Read-ahead the left block for the next loop. */
1804 xfs_btree_readahead(cur, lev, XFS_BTCUR_LEFTRA);
1808 * If we went off the root then we are seriously confused.
1809 * or the root of the tree is in an inode.
1811 if (lev == cur->bc_nlevels) {
1812 if (cur->bc_ops->type == XFS_BTREE_TYPE_INODE)
1815 xfs_btree_mark_sick(cur);
1816 error = -EFSCORRUPTED;
1819 ASSERT(lev < cur->bc_nlevels);
1822 * Now walk back down the tree, fixing up the cursor's buffer
1823 * pointers and key numbers.
1825 for (block = xfs_btree_get_block(cur, lev, &bp); lev > level; ) {
1826 union xfs_btree_ptr *ptrp;
1828 ptrp = xfs_btree_ptr_addr(cur, cur->bc_levels[lev].ptr, block);
1830 error = xfs_btree_read_buf_block(cur, ptrp, 0, &block, &bp);
1833 xfs_btree_setbuf(cur, lev, bp);
1834 cur->bc_levels[lev].ptr = xfs_btree_get_numrecs(block);
1849 * Check the btree block owner now that we have the context to know who the
1852 static inline xfs_failaddr_t
1853 xfs_btree_check_block_owner(
1854 struct xfs_btree_cur *cur,
1855 struct xfs_btree_block *block)
1859 if (!xfs_has_crc(cur->bc_mp) ||
1860 (cur->bc_flags & XFS_BTREE_BMBT_INVALID_OWNER))
1863 owner = xfs_btree_owner(cur);
1864 if (cur->bc_ops->ptr_len == XFS_BTREE_LONG_PTR_LEN) {
1865 if (be64_to_cpu(block->bb_u.l.bb_owner) != owner)
1866 return __this_address;
1868 if (be32_to_cpu(block->bb_u.s.bb_owner) != owner)
1869 return __this_address;
1876 xfs_btree_lookup_get_block(
1877 struct xfs_btree_cur *cur, /* btree cursor */
1878 int level, /* level in the btree */
1879 const union xfs_btree_ptr *pp, /* ptr to btree block */
1880 struct xfs_btree_block **blkp) /* return btree block */
1882 struct xfs_buf *bp; /* buffer pointer for btree block */
1886 /* special case the root block if in an inode */
1887 if (xfs_btree_at_iroot(cur, level)) {
1888 *blkp = xfs_btree_get_iroot(cur);
1893 * If the old buffer at this level for the disk address we are
1894 * looking for re-use it.
1896 * Otherwise throw it away and get a new one.
1898 bp = cur->bc_levels[level].bp;
1899 error = xfs_btree_ptr_to_daddr(cur, pp, &daddr);
1902 if (bp && xfs_buf_daddr(bp) == daddr) {
1903 *blkp = XFS_BUF_TO_BLOCK(bp);
1907 error = xfs_btree_read_buf_block(cur, pp, 0, blkp, &bp);
1911 /* Check the inode owner since the verifiers don't. */
1912 if (xfs_btree_check_block_owner(cur, *blkp) != NULL)
1915 /* Did we get the level we were looking for? */
1916 if (be16_to_cpu((*blkp)->bb_level) != level)
1919 /* Check that internal nodes have at least one record. */
1920 if (level != 0 && be16_to_cpu((*blkp)->bb_numrecs) == 0)
1923 xfs_btree_setbuf(cur, level, bp);
1928 xfs_buf_mark_corrupt(bp);
1929 xfs_trans_brelse(cur->bc_tp, bp);
1930 xfs_btree_mark_sick(cur);
1931 return -EFSCORRUPTED;
1935 * Get current search key. For level 0 we don't actually have a key
1936 * structure so we make one up from the record. For all other levels
1937 * we just return the right key.
1939 STATIC union xfs_btree_key *
1940 xfs_lookup_get_search_key(
1941 struct xfs_btree_cur *cur,
1944 struct xfs_btree_block *block,
1945 union xfs_btree_key *kp)
1948 cur->bc_ops->init_key_from_rec(kp,
1949 xfs_btree_rec_addr(cur, keyno, block));
1953 return xfs_btree_key_addr(cur, keyno, block);
1957 * Initialize a pointer to the root block.
1960 xfs_btree_init_ptr_from_cur(
1961 struct xfs_btree_cur *cur,
1962 union xfs_btree_ptr *ptr)
1964 if (cur->bc_ops->type == XFS_BTREE_TYPE_INODE) {
1966 * Inode-rooted btrees call xfs_btree_get_iroot to find the root
1967 * in xfs_btree_lookup_get_block and don't need a pointer here.
1970 } else if (cur->bc_flags & XFS_BTREE_STAGING) {
1971 ptr->s = cpu_to_be32(cur->bc_ag.afake->af_root);
1973 cur->bc_ops->init_ptr_from_cur(cur, ptr);
1978 * Lookup the record. The cursor is made to point to it, based on dir.
1979 * stat is set to 0 if can't find any such record, 1 for success.
1983 struct xfs_btree_cur *cur, /* btree cursor */
1984 xfs_lookup_t dir, /* <=, ==, or >= */
1985 int *stat) /* success/failure */
1987 struct xfs_btree_block *block; /* current btree block */
1988 int64_t diff; /* difference for the current key */
1989 int error; /* error return value */
1990 int keyno; /* current key number */
1991 int level; /* level in the btree */
1992 union xfs_btree_ptr *pp; /* ptr to btree block */
1993 union xfs_btree_ptr ptr; /* ptr to btree block */
1995 XFS_BTREE_STATS_INC(cur, lookup);
1997 /* No such thing as a zero-level tree. */
1998 if (XFS_IS_CORRUPT(cur->bc_mp, cur->bc_nlevels == 0)) {
1999 xfs_btree_mark_sick(cur);
2000 return -EFSCORRUPTED;
2006 /* initialise start pointer from cursor */
2007 xfs_btree_init_ptr_from_cur(cur, &ptr);
2011 * Iterate over each level in the btree, starting at the root.
2012 * For each level above the leaves, find the key we need, based
2013 * on the lookup record, then follow the corresponding block
2014 * pointer down to the next level.
2016 for (level = cur->bc_nlevels - 1, diff = 1; level >= 0; level--) {
2017 /* Get the block we need to do the lookup on. */
2018 error = xfs_btree_lookup_get_block(cur, level, pp, &block);
2024 * If we already had a key match at a higher level, we
2025 * know we need to use the first entry in this block.
2029 /* Otherwise search this block. Do a binary search. */
2031 int high; /* high entry number */
2032 int low; /* low entry number */
2034 /* Set low and high entry numbers, 1-based. */
2036 high = xfs_btree_get_numrecs(block);
2038 /* Block is empty, must be an empty leaf. */
2039 if (level != 0 || cur->bc_nlevels != 1) {
2040 XFS_CORRUPTION_ERROR(__func__,
2044 xfs_btree_mark_sick(cur);
2045 return -EFSCORRUPTED;
2048 cur->bc_levels[0].ptr = dir != XFS_LOOKUP_LE;
2053 /* Binary search the block. */
2054 while (low <= high) {
2055 union xfs_btree_key key;
2056 union xfs_btree_key *kp;
2058 XFS_BTREE_STATS_INC(cur, compare);
2060 /* keyno is average of low and high. */
2061 keyno = (low + high) >> 1;
2063 /* Get current search key */
2064 kp = xfs_lookup_get_search_key(cur, level,
2065 keyno, block, &key);
2068 * Compute difference to get next direction:
2069 * - less than, move right
2070 * - greater than, move left
2071 * - equal, we're done
2073 diff = cur->bc_ops->key_diff(cur, kp);
2084 * If there are more levels, set up for the next level
2085 * by getting the block number and filling in the cursor.
2089 * If we moved left, need the previous key number,
2090 * unless there isn't one.
2092 if (diff > 0 && --keyno < 1)
2094 pp = xfs_btree_ptr_addr(cur, keyno, block);
2096 error = xfs_btree_debug_check_ptr(cur, pp, 0, level);
2100 cur->bc_levels[level].ptr = keyno;
2104 /* Done with the search. See if we need to adjust the results. */
2105 if (dir != XFS_LOOKUP_LE && diff < 0) {
2108 * If ge search and we went off the end of the block, but it's
2109 * not the last block, we're in the wrong block.
2111 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_RIGHTSIB);
2112 if (dir == XFS_LOOKUP_GE &&
2113 keyno > xfs_btree_get_numrecs(block) &&
2114 !xfs_btree_ptr_is_null(cur, &ptr)) {
2117 cur->bc_levels[0].ptr = keyno;
2118 error = xfs_btree_increment(cur, 0, &i);
2121 if (XFS_IS_CORRUPT(cur->bc_mp, i != 1)) {
2122 xfs_btree_mark_sick(cur);
2123 return -EFSCORRUPTED;
2128 } else if (dir == XFS_LOOKUP_LE && diff > 0)
2130 cur->bc_levels[0].ptr = keyno;
2132 /* Return if we succeeded or not. */
2133 if (keyno == 0 || keyno > xfs_btree_get_numrecs(block))
2135 else if (dir != XFS_LOOKUP_EQ || diff == 0)
2145 /* Find the high key storage area from a regular key. */
2146 union xfs_btree_key *
2147 xfs_btree_high_key_from_key(
2148 struct xfs_btree_cur *cur,
2149 union xfs_btree_key *key)
2151 ASSERT(cur->bc_ops->geom_flags & XFS_BTGEO_OVERLAPPING);
2152 return (union xfs_btree_key *)((char *)key +
2153 (cur->bc_ops->key_len / 2));
2156 /* Determine the low (and high if overlapped) keys of a leaf block */
2158 xfs_btree_get_leaf_keys(
2159 struct xfs_btree_cur *cur,
2160 struct xfs_btree_block *block,
2161 union xfs_btree_key *key)
2163 union xfs_btree_key max_hkey;
2164 union xfs_btree_key hkey;
2165 union xfs_btree_rec *rec;
2166 union xfs_btree_key *high;
2169 rec = xfs_btree_rec_addr(cur, 1, block);
2170 cur->bc_ops->init_key_from_rec(key, rec);
2172 if (cur->bc_ops->geom_flags & XFS_BTGEO_OVERLAPPING) {
2174 cur->bc_ops->init_high_key_from_rec(&max_hkey, rec);
2175 for (n = 2; n <= xfs_btree_get_numrecs(block); n++) {
2176 rec = xfs_btree_rec_addr(cur, n, block);
2177 cur->bc_ops->init_high_key_from_rec(&hkey, rec);
2178 if (xfs_btree_keycmp_gt(cur, &hkey, &max_hkey))
2182 high = xfs_btree_high_key_from_key(cur, key);
2183 memcpy(high, &max_hkey, cur->bc_ops->key_len / 2);
2187 /* Determine the low (and high if overlapped) keys of a node block */
2189 xfs_btree_get_node_keys(
2190 struct xfs_btree_cur *cur,
2191 struct xfs_btree_block *block,
2192 union xfs_btree_key *key)
2194 union xfs_btree_key *hkey;
2195 union xfs_btree_key *max_hkey;
2196 union xfs_btree_key *high;
2199 if (cur->bc_ops->geom_flags & XFS_BTGEO_OVERLAPPING) {
2200 memcpy(key, xfs_btree_key_addr(cur, 1, block),
2201 cur->bc_ops->key_len / 2);
2203 max_hkey = xfs_btree_high_key_addr(cur, 1, block);
2204 for (n = 2; n <= xfs_btree_get_numrecs(block); n++) {
2205 hkey = xfs_btree_high_key_addr(cur, n, block);
2206 if (xfs_btree_keycmp_gt(cur, hkey, max_hkey))
2210 high = xfs_btree_high_key_from_key(cur, key);
2211 memcpy(high, max_hkey, cur->bc_ops->key_len / 2);
2213 memcpy(key, xfs_btree_key_addr(cur, 1, block),
2214 cur->bc_ops->key_len);
2218 /* Derive the keys for any btree block. */
2221 struct xfs_btree_cur *cur,
2222 struct xfs_btree_block *block,
2223 union xfs_btree_key *key)
2225 if (be16_to_cpu(block->bb_level) == 0)
2226 xfs_btree_get_leaf_keys(cur, block, key);
2228 xfs_btree_get_node_keys(cur, block, key);
2232 * Decide if we need to update the parent keys of a btree block. For
2233 * a standard btree this is only necessary if we're updating the first
2234 * record/key. For an overlapping btree, we must always update the
2235 * keys because the highest key can be in any of the records or keys
2239 xfs_btree_needs_key_update(
2240 struct xfs_btree_cur *cur,
2243 return (cur->bc_ops->geom_flags & XFS_BTGEO_OVERLAPPING) || ptr == 1;
2247 * Update the low and high parent keys of the given level, progressing
2248 * towards the root. If force_all is false, stop if the keys for a given
2249 * level do not need updating.
2252 __xfs_btree_updkeys(
2253 struct xfs_btree_cur *cur,
2255 struct xfs_btree_block *block,
2256 struct xfs_buf *bp0,
2259 union xfs_btree_key key; /* keys from current level */
2260 union xfs_btree_key *lkey; /* keys from the next level up */
2261 union xfs_btree_key *hkey;
2262 union xfs_btree_key *nlkey; /* keys from the next level up */
2263 union xfs_btree_key *nhkey;
2267 ASSERT(cur->bc_ops->geom_flags & XFS_BTGEO_OVERLAPPING);
2269 /* Exit if there aren't any parent levels to update. */
2270 if (level + 1 >= cur->bc_nlevels)
2273 trace_xfs_btree_updkeys(cur, level, bp0);
2276 hkey = xfs_btree_high_key_from_key(cur, lkey);
2277 xfs_btree_get_keys(cur, block, lkey);
2278 for (level++; level < cur->bc_nlevels; level++) {
2282 block = xfs_btree_get_block(cur, level, &bp);
2283 trace_xfs_btree_updkeys(cur, level, bp);
2285 error = xfs_btree_check_block(cur, block, level, bp);
2289 ptr = cur->bc_levels[level].ptr;
2290 nlkey = xfs_btree_key_addr(cur, ptr, block);
2291 nhkey = xfs_btree_high_key_addr(cur, ptr, block);
2293 xfs_btree_keycmp_eq(cur, nlkey, lkey) &&
2294 xfs_btree_keycmp_eq(cur, nhkey, hkey))
2296 xfs_btree_copy_keys(cur, nlkey, lkey, 1);
2297 xfs_btree_log_keys(cur, bp, ptr, ptr);
2298 if (level + 1 >= cur->bc_nlevels)
2300 xfs_btree_get_node_keys(cur, block, lkey);
2306 /* Update all the keys from some level in cursor back to the root. */
2308 xfs_btree_updkeys_force(
2309 struct xfs_btree_cur *cur,
2313 struct xfs_btree_block *block;
2315 block = xfs_btree_get_block(cur, level, &bp);
2316 return __xfs_btree_updkeys(cur, level, block, bp, true);
2320 * Update the parent keys of the given level, progressing towards the root.
2323 xfs_btree_update_keys(
2324 struct xfs_btree_cur *cur,
2327 struct xfs_btree_block *block;
2329 union xfs_btree_key *kp;
2330 union xfs_btree_key key;
2335 block = xfs_btree_get_block(cur, level, &bp);
2336 if (cur->bc_ops->geom_flags & XFS_BTGEO_OVERLAPPING)
2337 return __xfs_btree_updkeys(cur, level, block, bp, false);
2340 * Go up the tree from this level toward the root.
2341 * At each level, update the key value to the value input.
2342 * Stop when we reach a level where the cursor isn't pointing
2343 * at the first entry in the block.
2345 xfs_btree_get_keys(cur, block, &key);
2346 for (level++, ptr = 1; ptr == 1 && level < cur->bc_nlevels; level++) {
2350 block = xfs_btree_get_block(cur, level, &bp);
2352 error = xfs_btree_check_block(cur, block, level, bp);
2356 ptr = cur->bc_levels[level].ptr;
2357 kp = xfs_btree_key_addr(cur, ptr, block);
2358 xfs_btree_copy_keys(cur, kp, &key, 1);
2359 xfs_btree_log_keys(cur, bp, ptr, ptr);
2366 * Update the record referred to by cur to the value in the
2367 * given record. This either works (return 0) or gets an
2368 * EFSCORRUPTED error.
2372 struct xfs_btree_cur *cur,
2373 union xfs_btree_rec *rec)
2375 struct xfs_btree_block *block;
2379 union xfs_btree_rec *rp;
2381 /* Pick up the current block. */
2382 block = xfs_btree_get_block(cur, 0, &bp);
2385 error = xfs_btree_check_block(cur, block, 0, bp);
2389 /* Get the address of the rec to be updated. */
2390 ptr = cur->bc_levels[0].ptr;
2391 rp = xfs_btree_rec_addr(cur, ptr, block);
2393 /* Fill in the new contents and log them. */
2394 xfs_btree_copy_recs(cur, rp, rec, 1);
2395 xfs_btree_log_recs(cur, bp, ptr, ptr);
2397 /* Pass new key value up to our parent. */
2398 if (xfs_btree_needs_key_update(cur, ptr)) {
2399 error = xfs_btree_update_keys(cur, 0);
2411 * Move 1 record left from cur/level if possible.
2412 * Update cur to reflect the new path.
2414 STATIC int /* error */
2416 struct xfs_btree_cur *cur,
2418 int *stat) /* success/failure */
2420 struct xfs_buf *lbp; /* left buffer pointer */
2421 struct xfs_btree_block *left; /* left btree block */
2422 int lrecs; /* left record count */
2423 struct xfs_buf *rbp; /* right buffer pointer */
2424 struct xfs_btree_block *right; /* right btree block */
2425 struct xfs_btree_cur *tcur; /* temporary btree cursor */
2426 int rrecs; /* right record count */
2427 union xfs_btree_ptr lptr; /* left btree pointer */
2428 union xfs_btree_key *rkp = NULL; /* right btree key */
2429 union xfs_btree_ptr *rpp = NULL; /* right address pointer */
2430 union xfs_btree_rec *rrp = NULL; /* right record pointer */
2431 int error; /* error return value */
2434 if (xfs_btree_at_iroot(cur, level))
2437 /* Set up variables for this block as "right". */
2438 right = xfs_btree_get_block(cur, level, &rbp);
2441 error = xfs_btree_check_block(cur, right, level, rbp);
2446 /* If we've got no left sibling then we can't shift an entry left. */
2447 xfs_btree_get_sibling(cur, right, &lptr, XFS_BB_LEFTSIB);
2448 if (xfs_btree_ptr_is_null(cur, &lptr))
2452 * If the cursor entry is the one that would be moved, don't
2453 * do it... it's too complicated.
2455 if (cur->bc_levels[level].ptr <= 1)
2458 /* Set up the left neighbor as "left". */
2459 error = xfs_btree_read_buf_block(cur, &lptr, 0, &left, &lbp);
2463 /* If it's full, it can't take another entry. */
2464 lrecs = xfs_btree_get_numrecs(left);
2465 if (lrecs == cur->bc_ops->get_maxrecs(cur, level))
2468 rrecs = xfs_btree_get_numrecs(right);
2471 * We add one entry to the left side and remove one for the right side.
2472 * Account for it here, the changes will be updated on disk and logged
2478 XFS_BTREE_STATS_INC(cur, lshift);
2479 XFS_BTREE_STATS_ADD(cur, moves, 1);
2482 * If non-leaf, copy a key and a ptr to the left block.
2483 * Log the changes to the left block.
2486 /* It's a non-leaf. Move keys and pointers. */
2487 union xfs_btree_key *lkp; /* left btree key */
2488 union xfs_btree_ptr *lpp; /* left address pointer */
2490 lkp = xfs_btree_key_addr(cur, lrecs, left);
2491 rkp = xfs_btree_key_addr(cur, 1, right);
2493 lpp = xfs_btree_ptr_addr(cur, lrecs, left);
2494 rpp = xfs_btree_ptr_addr(cur, 1, right);
2496 error = xfs_btree_debug_check_ptr(cur, rpp, 0, level);
2500 xfs_btree_copy_keys(cur, lkp, rkp, 1);
2501 xfs_btree_copy_ptrs(cur, lpp, rpp, 1);
2503 xfs_btree_log_keys(cur, lbp, lrecs, lrecs);
2504 xfs_btree_log_ptrs(cur, lbp, lrecs, lrecs);
2506 ASSERT(cur->bc_ops->keys_inorder(cur,
2507 xfs_btree_key_addr(cur, lrecs - 1, left), lkp));
2509 /* It's a leaf. Move records. */
2510 union xfs_btree_rec *lrp; /* left record pointer */
2512 lrp = xfs_btree_rec_addr(cur, lrecs, left);
2513 rrp = xfs_btree_rec_addr(cur, 1, right);
2515 xfs_btree_copy_recs(cur, lrp, rrp, 1);
2516 xfs_btree_log_recs(cur, lbp, lrecs, lrecs);
2518 ASSERT(cur->bc_ops->recs_inorder(cur,
2519 xfs_btree_rec_addr(cur, lrecs - 1, left), lrp));
2522 xfs_btree_set_numrecs(left, lrecs);
2523 xfs_btree_log_block(cur, lbp, XFS_BB_NUMRECS);
2525 xfs_btree_set_numrecs(right, rrecs);
2526 xfs_btree_log_block(cur, rbp, XFS_BB_NUMRECS);
2529 * Slide the contents of right down one entry.
2531 XFS_BTREE_STATS_ADD(cur, moves, rrecs - 1);
2533 /* It's a nonleaf. operate on keys and ptrs */
2534 for (i = 0; i < rrecs; i++) {
2535 error = xfs_btree_debug_check_ptr(cur, rpp, i + 1, level);
2540 xfs_btree_shift_keys(cur,
2541 xfs_btree_key_addr(cur, 2, right),
2543 xfs_btree_shift_ptrs(cur,
2544 xfs_btree_ptr_addr(cur, 2, right),
2547 xfs_btree_log_keys(cur, rbp, 1, rrecs);
2548 xfs_btree_log_ptrs(cur, rbp, 1, rrecs);
2550 /* It's a leaf. operate on records */
2551 xfs_btree_shift_recs(cur,
2552 xfs_btree_rec_addr(cur, 2, right),
2554 xfs_btree_log_recs(cur, rbp, 1, rrecs);
2558 * Using a temporary cursor, update the parent key values of the
2559 * block on the left.
2561 if (cur->bc_ops->geom_flags & XFS_BTGEO_OVERLAPPING) {
2562 error = xfs_btree_dup_cursor(cur, &tcur);
2565 i = xfs_btree_firstrec(tcur, level);
2566 if (XFS_IS_CORRUPT(tcur->bc_mp, i != 1)) {
2567 xfs_btree_mark_sick(cur);
2568 error = -EFSCORRUPTED;
2572 error = xfs_btree_decrement(tcur, level, &i);
2576 /* Update the parent high keys of the left block, if needed. */
2577 error = xfs_btree_update_keys(tcur, level);
2581 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
2584 /* Update the parent keys of the right block. */
2585 error = xfs_btree_update_keys(cur, level);
2589 /* Slide the cursor value left one. */
2590 cur->bc_levels[level].ptr--;
2603 xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
2608 * Move 1 record right from cur/level if possible.
2609 * Update cur to reflect the new path.
2611 STATIC int /* error */
2613 struct xfs_btree_cur *cur,
2615 int *stat) /* success/failure */
2617 struct xfs_buf *lbp; /* left buffer pointer */
2618 struct xfs_btree_block *left; /* left btree block */
2619 struct xfs_buf *rbp; /* right buffer pointer */
2620 struct xfs_btree_block *right; /* right btree block */
2621 struct xfs_btree_cur *tcur; /* temporary btree cursor */
2622 union xfs_btree_ptr rptr; /* right block pointer */
2623 union xfs_btree_key *rkp; /* right btree key */
2624 int rrecs; /* right record count */
2625 int lrecs; /* left record count */
2626 int error; /* error return value */
2627 int i; /* loop counter */
2629 if (xfs_btree_at_iroot(cur, level))
2632 /* Set up variables for this block as "left". */
2633 left = xfs_btree_get_block(cur, level, &lbp);
2636 error = xfs_btree_check_block(cur, left, level, lbp);
2641 /* If we've got no right sibling then we can't shift an entry right. */
2642 xfs_btree_get_sibling(cur, left, &rptr, XFS_BB_RIGHTSIB);
2643 if (xfs_btree_ptr_is_null(cur, &rptr))
2647 * If the cursor entry is the one that would be moved, don't
2648 * do it... it's too complicated.
2650 lrecs = xfs_btree_get_numrecs(left);
2651 if (cur->bc_levels[level].ptr >= lrecs)
2654 /* Set up the right neighbor as "right". */
2655 error = xfs_btree_read_buf_block(cur, &rptr, 0, &right, &rbp);
2659 /* If it's full, it can't take another entry. */
2660 rrecs = xfs_btree_get_numrecs(right);
2661 if (rrecs == cur->bc_ops->get_maxrecs(cur, level))
2664 XFS_BTREE_STATS_INC(cur, rshift);
2665 XFS_BTREE_STATS_ADD(cur, moves, rrecs);
2668 * Make a hole at the start of the right neighbor block, then
2669 * copy the last left block entry to the hole.
2672 /* It's a nonleaf. make a hole in the keys and ptrs */
2673 union xfs_btree_key *lkp;
2674 union xfs_btree_ptr *lpp;
2675 union xfs_btree_ptr *rpp;
2677 lkp = xfs_btree_key_addr(cur, lrecs, left);
2678 lpp = xfs_btree_ptr_addr(cur, lrecs, left);
2679 rkp = xfs_btree_key_addr(cur, 1, right);
2680 rpp = xfs_btree_ptr_addr(cur, 1, right);
2682 for (i = rrecs - 1; i >= 0; i--) {
2683 error = xfs_btree_debug_check_ptr(cur, rpp, i, level);
2688 xfs_btree_shift_keys(cur, rkp, 1, rrecs);
2689 xfs_btree_shift_ptrs(cur, rpp, 1, rrecs);
2691 error = xfs_btree_debug_check_ptr(cur, lpp, 0, level);
2695 /* Now put the new data in, and log it. */
2696 xfs_btree_copy_keys(cur, rkp, lkp, 1);
2697 xfs_btree_copy_ptrs(cur, rpp, lpp, 1);
2699 xfs_btree_log_keys(cur, rbp, 1, rrecs + 1);
2700 xfs_btree_log_ptrs(cur, rbp, 1, rrecs + 1);
2702 ASSERT(cur->bc_ops->keys_inorder(cur, rkp,
2703 xfs_btree_key_addr(cur, 2, right)));
2705 /* It's a leaf. make a hole in the records */
2706 union xfs_btree_rec *lrp;
2707 union xfs_btree_rec *rrp;
2709 lrp = xfs_btree_rec_addr(cur, lrecs, left);
2710 rrp = xfs_btree_rec_addr(cur, 1, right);
2712 xfs_btree_shift_recs(cur, rrp, 1, rrecs);
2714 /* Now put the new data in, and log it. */
2715 xfs_btree_copy_recs(cur, rrp, lrp, 1);
2716 xfs_btree_log_recs(cur, rbp, 1, rrecs + 1);
2720 * Decrement and log left's numrecs, bump and log right's numrecs.
2722 xfs_btree_set_numrecs(left, --lrecs);
2723 xfs_btree_log_block(cur, lbp, XFS_BB_NUMRECS);
2725 xfs_btree_set_numrecs(right, ++rrecs);
2726 xfs_btree_log_block(cur, rbp, XFS_BB_NUMRECS);
2729 * Using a temporary cursor, update the parent key values of the
2730 * block on the right.
2732 error = xfs_btree_dup_cursor(cur, &tcur);
2735 i = xfs_btree_lastrec(tcur, level);
2736 if (XFS_IS_CORRUPT(tcur->bc_mp, i != 1)) {
2737 xfs_btree_mark_sick(cur);
2738 error = -EFSCORRUPTED;
2742 error = xfs_btree_increment(tcur, level, &i);
2746 /* Update the parent high keys of the left block, if needed. */
2747 if (cur->bc_ops->geom_flags & XFS_BTGEO_OVERLAPPING) {
2748 error = xfs_btree_update_keys(cur, level);
2753 /* Update the parent keys of the right block. */
2754 error = xfs_btree_update_keys(tcur, level);
2758 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
2771 xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
2776 xfs_btree_alloc_block(
2777 struct xfs_btree_cur *cur,
2778 const union xfs_btree_ptr *hint_block,
2779 union xfs_btree_ptr *new_block,
2785 * Don't allow block allocation for a staging cursor, because staging
2786 * cursors do not support regular btree modifications.
2788 * Bulk loading uses a separate callback to obtain new blocks from a
2789 * preallocated list, which prevents ENOSPC failures during loading.
2791 if (unlikely(cur->bc_flags & XFS_BTREE_STAGING)) {
2793 return -EFSCORRUPTED;
2796 error = cur->bc_ops->alloc_block(cur, hint_block, new_block, stat);
2797 trace_xfs_btree_alloc_block(cur, new_block, *stat, error);
2802 * Split cur/level block in half.
2803 * Return new block number and the key to its first
2804 * record (to be inserted into parent).
2806 STATIC int /* error */
2808 struct xfs_btree_cur *cur,
2810 union xfs_btree_ptr *ptrp,
2811 union xfs_btree_key *key,
2812 struct xfs_btree_cur **curp,
2813 int *stat) /* success/failure */
2815 union xfs_btree_ptr lptr; /* left sibling block ptr */
2816 struct xfs_buf *lbp; /* left buffer pointer */
2817 struct xfs_btree_block *left; /* left btree block */
2818 union xfs_btree_ptr rptr; /* right sibling block ptr */
2819 struct xfs_buf *rbp; /* right buffer pointer */
2820 struct xfs_btree_block *right; /* right btree block */
2821 union xfs_btree_ptr rrptr; /* right-right sibling ptr */
2822 struct xfs_buf *rrbp; /* right-right buffer pointer */
2823 struct xfs_btree_block *rrblock; /* right-right btree block */
2827 int error; /* error return value */
2830 XFS_BTREE_STATS_INC(cur, split);
2832 /* Set up left block (current one). */
2833 left = xfs_btree_get_block(cur, level, &lbp);
2836 error = xfs_btree_check_block(cur, left, level, lbp);
2841 xfs_btree_buf_to_ptr(cur, lbp, &lptr);
2843 /* Allocate the new block. If we can't do it, we're toast. Give up. */
2844 error = xfs_btree_alloc_block(cur, &lptr, &rptr, stat);
2849 XFS_BTREE_STATS_INC(cur, alloc);
2851 /* Set up the new block as "right". */
2852 error = xfs_btree_get_buf_block(cur, &rptr, &right, &rbp);
2856 /* Fill in the btree header for the new right block. */
2857 xfs_btree_init_block_cur(cur, rbp, xfs_btree_get_level(left), 0);
2860 * Split the entries between the old and the new block evenly.
2861 * Make sure that if there's an odd number of entries now, that
2862 * each new block will have the same number of entries.
2864 lrecs = xfs_btree_get_numrecs(left);
2866 if ((lrecs & 1) && cur->bc_levels[level].ptr <= rrecs + 1)
2868 src_index = (lrecs - rrecs + 1);
2870 XFS_BTREE_STATS_ADD(cur, moves, rrecs);
2872 /* Adjust numrecs for the later get_*_keys() calls. */
2874 xfs_btree_set_numrecs(left, lrecs);
2875 xfs_btree_set_numrecs(right, xfs_btree_get_numrecs(right) + rrecs);
2878 * Copy btree block entries from the left block over to the
2879 * new block, the right. Update the right block and log the
2883 /* It's a non-leaf. Move keys and pointers. */
2884 union xfs_btree_key *lkp; /* left btree key */
2885 union xfs_btree_ptr *lpp; /* left address pointer */
2886 union xfs_btree_key *rkp; /* right btree key */
2887 union xfs_btree_ptr *rpp; /* right address pointer */
2889 lkp = xfs_btree_key_addr(cur, src_index, left);
2890 lpp = xfs_btree_ptr_addr(cur, src_index, left);
2891 rkp = xfs_btree_key_addr(cur, 1, right);
2892 rpp = xfs_btree_ptr_addr(cur, 1, right);
2894 for (i = src_index; i < rrecs; i++) {
2895 error = xfs_btree_debug_check_ptr(cur, lpp, i, level);
2900 /* Copy the keys & pointers to the new block. */
2901 xfs_btree_copy_keys(cur, rkp, lkp, rrecs);
2902 xfs_btree_copy_ptrs(cur, rpp, lpp, rrecs);
2904 xfs_btree_log_keys(cur, rbp, 1, rrecs);
2905 xfs_btree_log_ptrs(cur, rbp, 1, rrecs);
2907 /* Stash the keys of the new block for later insertion. */
2908 xfs_btree_get_node_keys(cur, right, key);
2910 /* It's a leaf. Move records. */
2911 union xfs_btree_rec *lrp; /* left record pointer */
2912 union xfs_btree_rec *rrp; /* right record pointer */
2914 lrp = xfs_btree_rec_addr(cur, src_index, left);
2915 rrp = xfs_btree_rec_addr(cur, 1, right);
2917 /* Copy records to the new block. */
2918 xfs_btree_copy_recs(cur, rrp, lrp, rrecs);
2919 xfs_btree_log_recs(cur, rbp, 1, rrecs);
2921 /* Stash the keys of the new block for later insertion. */
2922 xfs_btree_get_leaf_keys(cur, right, key);
2926 * Find the left block number by looking in the buffer.
2927 * Adjust sibling pointers.
2929 xfs_btree_get_sibling(cur, left, &rrptr, XFS_BB_RIGHTSIB);
2930 xfs_btree_set_sibling(cur, right, &rrptr, XFS_BB_RIGHTSIB);
2931 xfs_btree_set_sibling(cur, right, &lptr, XFS_BB_LEFTSIB);
2932 xfs_btree_set_sibling(cur, left, &rptr, XFS_BB_RIGHTSIB);
2934 xfs_btree_log_block(cur, rbp, XFS_BB_ALL_BITS);
2935 xfs_btree_log_block(cur, lbp, XFS_BB_NUMRECS | XFS_BB_RIGHTSIB);
2938 * If there's a block to the new block's right, make that block
2939 * point back to right instead of to left.
2941 if (!xfs_btree_ptr_is_null(cur, &rrptr)) {
2942 error = xfs_btree_read_buf_block(cur, &rrptr,
2943 0, &rrblock, &rrbp);
2946 xfs_btree_set_sibling(cur, rrblock, &rptr, XFS_BB_LEFTSIB);
2947 xfs_btree_log_block(cur, rrbp, XFS_BB_LEFTSIB);
2950 /* Update the parent high keys of the left block, if needed. */
2951 if (cur->bc_ops->geom_flags & XFS_BTGEO_OVERLAPPING) {
2952 error = xfs_btree_update_keys(cur, level);
2958 * If the cursor is really in the right block, move it there.
2959 * If it's just pointing past the last entry in left, then we'll
2960 * insert there, so don't change anything in that case.
2962 if (cur->bc_levels[level].ptr > lrecs + 1) {
2963 xfs_btree_setbuf(cur, level, rbp);
2964 cur->bc_levels[level].ptr -= lrecs;
2967 * If there are more levels, we'll need another cursor which refers
2968 * the right block, no matter where this cursor was.
2970 if (level + 1 < cur->bc_nlevels) {
2971 error = xfs_btree_dup_cursor(cur, curp);
2974 (*curp)->bc_levels[level + 1].ptr++;
2988 struct xfs_btree_split_args {
2989 struct xfs_btree_cur *cur;
2991 union xfs_btree_ptr *ptrp;
2992 union xfs_btree_key *key;
2993 struct xfs_btree_cur **curp;
2994 int *stat; /* success/failure */
2996 bool kswapd; /* allocation in kswapd context */
2997 struct completion *done;
2998 struct work_struct work;
3002 * Stack switching interfaces for allocation
3005 xfs_btree_split_worker(
3006 struct work_struct *work)
3008 struct xfs_btree_split_args *args = container_of(work,
3009 struct xfs_btree_split_args, work);
3010 unsigned long pflags;
3011 unsigned long new_pflags = 0;
3014 * we are in a transaction context here, but may also be doing work
3015 * in kswapd context, and hence we may need to inherit that state
3016 * temporarily to ensure that we don't block waiting for memory reclaim
3020 new_pflags |= PF_MEMALLOC | PF_KSWAPD;
3022 current_set_flags_nested(&pflags, new_pflags);
3023 xfs_trans_set_context(args->cur->bc_tp);
3025 args->result = __xfs_btree_split(args->cur, args->level, args->ptrp,
3026 args->key, args->curp, args->stat);
3028 xfs_trans_clear_context(args->cur->bc_tp);
3029 current_restore_flags_nested(&pflags, new_pflags);
3032 * Do not access args after complete() has run here. We don't own args
3033 * and the owner may run and free args before we return here.
3035 complete(args->done);
3040 * BMBT split requests often come in with little stack to work on so we push
3041 * them off to a worker thread so there is lots of stack to use. For the other
3042 * btree types, just call directly to avoid the context switch overhead here.
3044 * Care must be taken here - the work queue rescuer thread introduces potential
3045 * AGF <> worker queue deadlocks if the BMBT block allocation has to lock new
3046 * AGFs to allocate blocks. A task being run by the rescuer could attempt to
3047 * lock an AGF that is already locked by a task queued to run by the rescuer,
3048 * resulting in an ABBA deadlock as the rescuer cannot run the lock holder to
3049 * release it until the current thread it is running gains the lock.
3051 * To avoid this issue, we only ever queue BMBT splits that don't have an AGF
3052 * already locked to allocate from. The only place that doesn't hold an AGF
3053 * locked is unwritten extent conversion at IO completion, but that has already
3054 * been offloaded to a worker thread and hence has no stack consumption issues
3055 * we have to worry about.
3057 STATIC int /* error */
3059 struct xfs_btree_cur *cur,
3061 union xfs_btree_ptr *ptrp,
3062 union xfs_btree_key *key,
3063 struct xfs_btree_cur **curp,
3064 int *stat) /* success/failure */
3066 struct xfs_btree_split_args args;
3067 DECLARE_COMPLETION_ONSTACK(done);
3069 if (!xfs_btree_is_bmap(cur->bc_ops) ||
3070 cur->bc_tp->t_highest_agno == NULLAGNUMBER)
3071 return __xfs_btree_split(cur, level, ptrp, key, curp, stat);
3080 args.kswapd = current_is_kswapd();
3081 INIT_WORK_ONSTACK(&args.work, xfs_btree_split_worker);
3082 queue_work(xfs_alloc_wq, &args.work);
3083 wait_for_completion(&done);
3084 destroy_work_on_stack(&args.work);
3088 #define xfs_btree_split __xfs_btree_split
3089 #endif /* __KERNEL__ */
3091 /* Move the records from a root leaf block to a separate block. */
3093 xfs_btree_promote_leaf_iroot(
3094 struct xfs_btree_cur *cur,
3095 struct xfs_btree_block *block,
3096 struct xfs_buf *cbp,
3097 union xfs_btree_ptr *cptr,
3098 struct xfs_btree_block *cblock)
3100 union xfs_btree_rec *rp;
3101 union xfs_btree_rec *crp;
3102 union xfs_btree_key *kp;
3103 union xfs_btree_ptr *pp;
3104 struct xfs_btree_block *broot;
3105 int numrecs = xfs_btree_get_numrecs(block);
3107 /* Copy the records from the leaf broot into the new child block. */
3108 rp = xfs_btree_rec_addr(cur, 1, block);
3109 crp = xfs_btree_rec_addr(cur, 1, cblock);
3110 xfs_btree_copy_recs(cur, crp, rp, numrecs);
3113 * Increment the tree height.
3115 * Trickery here: The amount of memory that we need per record for the
3116 * ifork's btree root block may change when we convert the broot from a
3117 * leaf to a node block. Free the existing leaf broot so that nobody
3118 * thinks we need to migrate node pointers when we realloc the broot
3119 * buffer after bumping nlevels.
3121 cur->bc_ops->broot_realloc(cur, 0);
3123 cur->bc_levels[1].ptr = 1;
3126 * Allocate a new node broot and initialize it to point to the new
3129 broot = cur->bc_ops->broot_realloc(cur, 1);
3130 xfs_btree_init_block(cur->bc_mp, broot, cur->bc_ops,
3131 cur->bc_nlevels - 1, 1, cur->bc_ino.ip->i_ino);
3133 pp = xfs_btree_ptr_addr(cur, 1, broot);
3134 kp = xfs_btree_key_addr(cur, 1, broot);
3135 xfs_btree_copy_ptrs(cur, pp, cptr, 1);
3136 xfs_btree_get_keys(cur, cblock, kp);
3138 /* Attach the new block to the cursor and log it. */
3139 xfs_btree_setbuf(cur, 0, cbp);
3140 xfs_btree_log_block(cur, cbp, XFS_BB_ALL_BITS);
3141 xfs_btree_log_recs(cur, cbp, 1, numrecs);
3145 * Move the keys and pointers from a root block to a separate block.
3147 * Since the keyptr size does not change, all we have to do is increase the
3148 * tree height, copy the keyptrs to the new internal node (cblock), shrink
3149 * the root, and copy the pointers there.
3152 xfs_btree_promote_node_iroot(
3153 struct xfs_btree_cur *cur,
3154 struct xfs_btree_block *block,
3156 struct xfs_buf *cbp,
3157 union xfs_btree_ptr *cptr,
3158 struct xfs_btree_block *cblock)
3160 union xfs_btree_key *ckp;
3161 union xfs_btree_key *kp;
3162 union xfs_btree_ptr *cpp;
3163 union xfs_btree_ptr *pp;
3166 int numrecs = xfs_btree_get_numrecs(block);
3169 * Increase tree height, adjusting the root block level to match.
3170 * We cannot change the root btree node size until we've copied the
3171 * block contents to the new child block.
3173 be16_add_cpu(&block->bb_level, 1);
3175 cur->bc_levels[level + 1].ptr = 1;
3178 * Adjust the root btree record count, then copy the keys from the old
3179 * root to the new child block.
3181 xfs_btree_set_numrecs(block, 1);
3182 kp = xfs_btree_key_addr(cur, 1, block);
3183 ckp = xfs_btree_key_addr(cur, 1, cblock);
3184 xfs_btree_copy_keys(cur, ckp, kp, numrecs);
3186 /* Check the pointers and copy them to the new child block. */
3187 pp = xfs_btree_ptr_addr(cur, 1, block);
3188 cpp = xfs_btree_ptr_addr(cur, 1, cblock);
3189 for (i = 0; i < numrecs; i++) {
3190 error = xfs_btree_debug_check_ptr(cur, pp, i, level);
3194 xfs_btree_copy_ptrs(cur, cpp, pp, numrecs);
3197 * Set the first keyptr to point to the new child block, then shrink
3198 * the memory buffer for the root block.
3200 error = xfs_btree_debug_check_ptr(cur, cptr, 0, level);
3203 xfs_btree_copy_ptrs(cur, pp, cptr, 1);
3204 xfs_btree_get_keys(cur, cblock, kp);
3206 cur->bc_ops->broot_realloc(cur, 1);
3208 /* Attach the new block to the cursor and log it. */
3209 xfs_btree_setbuf(cur, level, cbp);
3210 xfs_btree_log_block(cur, cbp, XFS_BB_ALL_BITS);
3211 xfs_btree_log_keys(cur, cbp, 1, numrecs);
3212 xfs_btree_log_ptrs(cur, cbp, 1, numrecs);
3217 * Copy the old inode root contents into a real block and make the
3218 * broot point to it.
3221 xfs_btree_new_iroot(
3222 struct xfs_btree_cur *cur, /* btree cursor */
3223 int *logflags, /* logging flags for inode */
3224 int *stat) /* return status - 0 fail */
3226 struct xfs_buf *cbp; /* buffer for cblock */
3227 struct xfs_btree_block *block; /* btree block */
3228 struct xfs_btree_block *cblock; /* child btree block */
3229 union xfs_btree_ptr aptr;
3230 union xfs_btree_ptr nptr; /* new block addr */
3231 int level; /* btree level */
3232 int error; /* error return code */
3234 XFS_BTREE_STATS_INC(cur, newroot);
3236 ASSERT(cur->bc_ops->type == XFS_BTREE_TYPE_INODE);
3238 level = cur->bc_nlevels - 1;
3240 block = xfs_btree_get_iroot(cur);
3241 ASSERT(level > 0 || (cur->bc_ops->geom_flags & XFS_BTGEO_IROOT_RECORDS));
3243 aptr = *xfs_btree_ptr_addr(cur, 1, block);
3245 aptr.l = cpu_to_be64(XFS_INO_TO_FSB(cur->bc_mp,
3246 cur->bc_ino.ip->i_ino));
3248 /* Allocate the new block. If we can't do it, we're toast. Give up. */
3249 error = xfs_btree_alloc_block(cur, &aptr, &nptr, stat);
3255 XFS_BTREE_STATS_INC(cur, alloc);
3257 /* Copy the root into a real block. */
3258 error = xfs_btree_get_buf_block(cur, &nptr, &cblock, &cbp);
3263 * we can't just memcpy() the root in for CRC enabled btree blocks.
3264 * In that case have to also ensure the blkno remains correct
3266 memcpy(cblock, block, xfs_btree_block_len(cur));
3267 if (xfs_has_crc(cur->bc_mp)) {
3268 __be64 bno = cpu_to_be64(xfs_buf_daddr(cbp));
3269 if (cur->bc_ops->ptr_len == XFS_BTREE_LONG_PTR_LEN)
3270 cblock->bb_u.l.bb_blkno = bno;
3272 cblock->bb_u.s.bb_blkno = bno;
3276 error = xfs_btree_promote_node_iroot(cur, block, level, cbp,
3281 xfs_btree_promote_leaf_iroot(cur, block, cbp, &nptr, cblock);
3284 *logflags |= XFS_ILOG_CORE | xfs_ilog_fbroot(cur->bc_ino.whichfork);
3293 struct xfs_btree_cur *cur,
3294 const union xfs_btree_ptr *ptr,
3297 if (cur->bc_flags & XFS_BTREE_STAGING) {
3298 /* Update the btree root information for a per-AG fake root. */
3299 cur->bc_ag.afake->af_root = be32_to_cpu(ptr->s);
3300 cur->bc_ag.afake->af_levels += inc;
3302 cur->bc_ops->set_root(cur, ptr, inc);
3307 * Allocate a new root block, fill it in.
3309 STATIC int /* error */
3311 struct xfs_btree_cur *cur, /* btree cursor */
3312 int *stat) /* success/failure */
3314 struct xfs_btree_block *block; /* one half of the old root block */
3315 struct xfs_buf *bp; /* buffer containing block */
3316 int error; /* error return value */
3317 struct xfs_buf *lbp; /* left buffer pointer */
3318 struct xfs_btree_block *left; /* left btree block */
3319 struct xfs_buf *nbp; /* new (root) buffer */
3320 struct xfs_btree_block *new; /* new (root) btree block */
3321 int nptr; /* new value for key index, 1 or 2 */
3322 struct xfs_buf *rbp; /* right buffer pointer */
3323 struct xfs_btree_block *right; /* right btree block */
3324 union xfs_btree_ptr rptr;
3325 union xfs_btree_ptr lptr;
3327 XFS_BTREE_STATS_INC(cur, newroot);
3329 /* initialise our start point from the cursor */
3330 xfs_btree_init_ptr_from_cur(cur, &rptr);
3332 /* Allocate the new block. If we can't do it, we're toast. Give up. */
3333 error = xfs_btree_alloc_block(cur, &rptr, &lptr, stat);
3338 XFS_BTREE_STATS_INC(cur, alloc);
3340 /* Set up the new block. */
3341 error = xfs_btree_get_buf_block(cur, &lptr, &new, &nbp);
3345 /* Set the root in the holding structure increasing the level by 1. */
3346 xfs_btree_set_root(cur, &lptr, 1);
3349 * At the previous root level there are now two blocks: the old root,
3350 * and the new block generated when it was split. We don't know which
3351 * one the cursor is pointing at, so we set up variables "left" and
3352 * "right" for each case.
3354 block = xfs_btree_get_block(cur, cur->bc_nlevels - 1, &bp);
3357 error = xfs_btree_check_block(cur, block, cur->bc_nlevels - 1, bp);
3362 xfs_btree_get_sibling(cur, block, &rptr, XFS_BB_RIGHTSIB);
3363 if (!xfs_btree_ptr_is_null(cur, &rptr)) {
3364 /* Our block is left, pick up the right block. */
3366 xfs_btree_buf_to_ptr(cur, lbp, &lptr);
3368 error = xfs_btree_read_buf_block(cur, &rptr, 0, &right, &rbp);
3374 /* Our block is right, pick up the left block. */
3376 xfs_btree_buf_to_ptr(cur, rbp, &rptr);
3378 xfs_btree_get_sibling(cur, right, &lptr, XFS_BB_LEFTSIB);
3379 error = xfs_btree_read_buf_block(cur, &lptr, 0, &left, &lbp);
3386 /* Fill in the new block's btree header and log it. */
3387 xfs_btree_init_block_cur(cur, nbp, cur->bc_nlevels, 2);
3388 xfs_btree_log_block(cur, nbp, XFS_BB_ALL_BITS);
3389 ASSERT(!xfs_btree_ptr_is_null(cur, &lptr) &&
3390 !xfs_btree_ptr_is_null(cur, &rptr));
3392 /* Fill in the key data in the new root. */
3393 if (xfs_btree_get_level(left) > 0) {
3395 * Get the keys for the left block's keys and put them directly
3396 * in the parent block. Do the same for the right block.
3398 xfs_btree_get_node_keys(cur, left,
3399 xfs_btree_key_addr(cur, 1, new));
3400 xfs_btree_get_node_keys(cur, right,
3401 xfs_btree_key_addr(cur, 2, new));
3404 * Get the keys for the left block's records and put them
3405 * directly in the parent block. Do the same for the right
3408 xfs_btree_get_leaf_keys(cur, left,
3409 xfs_btree_key_addr(cur, 1, new));
3410 xfs_btree_get_leaf_keys(cur, right,
3411 xfs_btree_key_addr(cur, 2, new));
3413 xfs_btree_log_keys(cur, nbp, 1, 2);
3415 /* Fill in the pointer data in the new root. */
3416 xfs_btree_copy_ptrs(cur,
3417 xfs_btree_ptr_addr(cur, 1, new), &lptr, 1);
3418 xfs_btree_copy_ptrs(cur,
3419 xfs_btree_ptr_addr(cur, 2, new), &rptr, 1);
3420 xfs_btree_log_ptrs(cur, nbp, 1, 2);
3422 /* Fix up the cursor. */
3423 xfs_btree_setbuf(cur, cur->bc_nlevels, nbp);
3424 cur->bc_levels[cur->bc_nlevels].ptr = nptr;
3426 ASSERT(cur->bc_nlevels <= cur->bc_maxlevels);
3437 xfs_btree_make_block_unfull(
3438 struct xfs_btree_cur *cur, /* btree cursor */
3439 int level, /* btree level */
3440 int numrecs,/* # of recs in block */
3441 int *oindex,/* old tree index */
3442 int *index, /* new tree index */
3443 union xfs_btree_ptr *nptr, /* new btree ptr */
3444 struct xfs_btree_cur **ncur, /* new btree cursor */
3445 union xfs_btree_key *key, /* key of new block */
3450 if (xfs_btree_at_iroot(cur, level)) {
3451 struct xfs_inode *ip = cur->bc_ino.ip;
3453 if (numrecs < cur->bc_ops->get_dmaxrecs(cur, level)) {
3454 /* A root block that can be made bigger. */
3455 cur->bc_ops->broot_realloc(cur, numrecs + 1);
3458 /* A root block that needs replacing */
3461 error = xfs_btree_new_iroot(cur, &logflags, stat);
3462 if (error || *stat == 0)
3465 xfs_trans_log_inode(cur->bc_tp, ip, logflags);
3471 /* First, try shifting an entry to the right neighbor. */
3472 error = xfs_btree_rshift(cur, level, stat);
3476 /* Next, try shifting an entry to the left neighbor. */
3477 error = xfs_btree_lshift(cur, level, stat);
3482 *oindex = *index = cur->bc_levels[level].ptr;
3487 * Next, try splitting the current block in half.
3489 * If this works we have to re-set our variables because we
3490 * could be in a different block now.
3492 error = xfs_btree_split(cur, level, nptr, key, ncur, stat);
3493 if (error || *stat == 0)
3497 *index = cur->bc_levels[level].ptr;
3502 * Insert one record/level. Return information to the caller
3503 * allowing the next level up to proceed if necessary.
3507 struct xfs_btree_cur *cur, /* btree cursor */
3508 int level, /* level to insert record at */
3509 union xfs_btree_ptr *ptrp, /* i/o: block number inserted */
3510 union xfs_btree_rec *rec, /* record to insert */
3511 union xfs_btree_key *key, /* i/o: block key for ptrp */
3512 struct xfs_btree_cur **curp, /* output: new cursor replacing cur */
3513 int *stat) /* success/failure */
3515 struct xfs_btree_block *block; /* btree block */
3516 struct xfs_buf *bp; /* buffer for block */
3517 union xfs_btree_ptr nptr; /* new block ptr */
3518 struct xfs_btree_cur *ncur = NULL; /* new btree cursor */
3519 union xfs_btree_key nkey; /* new block key */
3520 union xfs_btree_key *lkey;
3521 int optr; /* old key/record index */
3522 int ptr; /* key/record index */
3523 int numrecs;/* number of records */
3524 int error; /* error return value */
3532 * If we have an external root pointer, and we've made it to the
3533 * root level, allocate a new root block and we're done.
3535 if (cur->bc_ops->type != XFS_BTREE_TYPE_INODE &&
3536 level >= cur->bc_nlevels) {
3537 error = xfs_btree_new_root(cur, stat);
3538 xfs_btree_set_ptr_null(cur, ptrp);
3543 /* If we're off the left edge, return failure. */
3544 ptr = cur->bc_levels[level].ptr;
3552 XFS_BTREE_STATS_INC(cur, insrec);
3554 /* Get pointers to the btree buffer and block. */
3555 block = xfs_btree_get_block(cur, level, &bp);
3556 old_bn = bp ? xfs_buf_daddr(bp) : XFS_BUF_DADDR_NULL;
3557 numrecs = xfs_btree_get_numrecs(block);
3560 error = xfs_btree_check_block(cur, block, level, bp);
3564 /* Check that the new entry is being inserted in the right place. */
3565 if (ptr <= numrecs) {
3567 ASSERT(cur->bc_ops->recs_inorder(cur, rec,
3568 xfs_btree_rec_addr(cur, ptr, block)));
3570 ASSERT(cur->bc_ops->keys_inorder(cur, key,
3571 xfs_btree_key_addr(cur, ptr, block)));
3577 * If the block is full, we can't insert the new entry until we
3578 * make the block un-full.
3580 xfs_btree_set_ptr_null(cur, &nptr);
3581 if (numrecs == cur->bc_ops->get_maxrecs(cur, level)) {
3582 error = xfs_btree_make_block_unfull(cur, level, numrecs,
3583 &optr, &ptr, &nptr, &ncur, lkey, stat);
3584 if (error || *stat == 0)
3589 * The current block may have changed if the block was
3590 * previously full and we have just made space in it.
3592 block = xfs_btree_get_block(cur, level, &bp);
3593 numrecs = xfs_btree_get_numrecs(block);
3596 error = xfs_btree_check_block(cur, block, level, bp);
3602 * At this point we know there's room for our new entry in the block
3603 * we're pointing at.
3605 XFS_BTREE_STATS_ADD(cur, moves, numrecs - ptr + 1);
3608 /* It's a nonleaf. make a hole in the keys and ptrs */
3609 union xfs_btree_key *kp;
3610 union xfs_btree_ptr *pp;
3612 kp = xfs_btree_key_addr(cur, ptr, block);
3613 pp = xfs_btree_ptr_addr(cur, ptr, block);
3615 for (i = numrecs - ptr; i >= 0; i--) {
3616 error = xfs_btree_debug_check_ptr(cur, pp, i, level);
3621 xfs_btree_shift_keys(cur, kp, 1, numrecs - ptr + 1);
3622 xfs_btree_shift_ptrs(cur, pp, 1, numrecs - ptr + 1);
3624 error = xfs_btree_debug_check_ptr(cur, ptrp, 0, level);
3628 /* Now put the new data in, bump numrecs and log it. */
3629 xfs_btree_copy_keys(cur, kp, key, 1);
3630 xfs_btree_copy_ptrs(cur, pp, ptrp, 1);
3632 xfs_btree_set_numrecs(block, numrecs);
3633 xfs_btree_log_ptrs(cur, bp, ptr, numrecs);
3634 xfs_btree_log_keys(cur, bp, ptr, numrecs);
3636 if (ptr < numrecs) {
3637 ASSERT(cur->bc_ops->keys_inorder(cur, kp,
3638 xfs_btree_key_addr(cur, ptr + 1, block)));
3642 /* It's a leaf. make a hole in the records */
3643 union xfs_btree_rec *rp;
3645 rp = xfs_btree_rec_addr(cur, ptr, block);
3647 xfs_btree_shift_recs(cur, rp, 1, numrecs - ptr + 1);
3649 /* Now put the new data in, bump numrecs and log it. */
3650 xfs_btree_copy_recs(cur, rp, rec, 1);
3651 xfs_btree_set_numrecs(block, ++numrecs);
3652 xfs_btree_log_recs(cur, bp, ptr, numrecs);
3654 if (ptr < numrecs) {
3655 ASSERT(cur->bc_ops->recs_inorder(cur, rp,
3656 xfs_btree_rec_addr(cur, ptr + 1, block)));
3661 /* Log the new number of records in the btree header. */
3662 xfs_btree_log_block(cur, bp, XFS_BB_NUMRECS);
3665 * Update btree keys to reflect the newly added record or keyptr.
3666 * There are three cases here to be aware of. Normally, all we have to
3667 * do is walk towards the root, updating keys as necessary.
3669 * If the caller had us target a full block for the insertion, we dealt
3670 * with that by calling the _make_block_unfull function. If the
3671 * "make unfull" function splits the block, it'll hand us back the key
3672 * and pointer of the new block. We haven't yet added the new block to
3673 * the next level up, so if we decide to add the new record to the new
3674 * block (bp->b_bn != old_bn), we have to update the caller's pointer
3675 * so that the caller adds the new block with the correct key.
3677 * However, there is a third possibility-- if the selected block is the
3678 * root block of an inode-rooted btree and cannot be expanded further,
3679 * the "make unfull" function moves the root block contents to a new
3680 * block and updates the root block to point to the new block. In this
3681 * case, no block pointer is passed back because the block has already
3682 * been added to the btree. In this case, we need to use the regular
3683 * key update function, just like the first case. This is critical for
3684 * overlapping btrees, because the high key must be updated to reflect
3685 * the entire tree, not just the subtree accessible through the first
3686 * child of the root (which is now two levels down from the root).
3688 if (!xfs_btree_ptr_is_null(cur, &nptr) &&
3689 bp && xfs_buf_daddr(bp) != old_bn) {
3690 xfs_btree_get_keys(cur, block, lkey);
3691 } else if (xfs_btree_needs_key_update(cur, optr)) {
3692 error = xfs_btree_update_keys(cur, level);
3698 * Return the new block number, if any.
3699 * If there is one, give back a record value and a cursor too.
3702 if (!xfs_btree_ptr_is_null(cur, &nptr)) {
3703 xfs_btree_copy_keys(cur, key, lkey, 1);
3712 xfs_btree_del_cursor(ncur, error);
3717 * Insert the record at the point referenced by cur.
3719 * A multi-level split of the tree on insert will invalidate the original
3720 * cursor. All callers of this function should assume that the cursor is
3721 * no longer valid and revalidate it.
3725 struct xfs_btree_cur *cur,
3728 int error; /* error return value */
3729 int i; /* result value, 0 for failure */
3730 int level; /* current level number in btree */
3731 union xfs_btree_ptr nptr; /* new block number (split result) */
3732 struct xfs_btree_cur *ncur; /* new cursor (split result) */
3733 struct xfs_btree_cur *pcur; /* previous level's cursor */
3734 union xfs_btree_key bkey; /* key of block to insert */
3735 union xfs_btree_key *key;
3736 union xfs_btree_rec rec; /* record to insert */
3743 xfs_btree_set_ptr_null(cur, &nptr);
3745 /* Make a key out of the record data to be inserted, and save it. */
3746 cur->bc_ops->init_rec_from_cur(cur, &rec);
3747 cur->bc_ops->init_key_from_rec(key, &rec);
3750 * Loop going up the tree, starting at the leaf level.
3751 * Stop when we don't get a split block, that must mean that
3752 * the insert is finished with this level.
3756 * Insert nrec/nptr into this level of the tree.
3757 * Note if we fail, nptr will be null.
3759 error = xfs_btree_insrec(pcur, level, &nptr, &rec, key,
3763 xfs_btree_del_cursor(pcur, XFS_BTREE_ERROR);
3767 if (XFS_IS_CORRUPT(cur->bc_mp, i != 1)) {
3768 xfs_btree_mark_sick(cur);
3769 error = -EFSCORRUPTED;
3775 * See if the cursor we just used is trash.
3776 * Can't trash the caller's cursor, but otherwise we should
3777 * if ncur is a new cursor or we're about to be done.
3780 (ncur || xfs_btree_ptr_is_null(cur, &nptr))) {
3781 /* Save the state from the cursor before we trash it */
3782 if (cur->bc_ops->update_cursor &&
3783 !(cur->bc_flags & XFS_BTREE_STAGING))
3784 cur->bc_ops->update_cursor(pcur, cur);
3785 cur->bc_nlevels = pcur->bc_nlevels;
3786 xfs_btree_del_cursor(pcur, XFS_BTREE_NOERROR);
3788 /* If we got a new cursor, switch to it. */
3793 } while (!xfs_btree_ptr_is_null(cur, &nptr));
3801 /* Move the records from a child leaf block to the root block. */
3803 xfs_btree_demote_leaf_child(
3804 struct xfs_btree_cur *cur,
3805 struct xfs_btree_block *cblock,
3808 union xfs_btree_rec *rp;
3809 union xfs_btree_rec *crp;
3810 struct xfs_btree_block *broot;
3813 * Decrease the tree height.
3815 * Trickery here: The amount of memory that we need per record for the
3816 * ifork's btree root block may change when we convert the broot from a
3817 * node to a leaf. Free the old node broot so that we can get a fresh
3820 cur->bc_ops->broot_realloc(cur, 0);
3824 * Allocate a new leaf broot and copy the records from the old child.
3825 * Detach the old child from the cursor.
3827 broot = cur->bc_ops->broot_realloc(cur, numrecs);
3828 xfs_btree_init_block(cur->bc_mp, broot, cur->bc_ops, 0, numrecs,
3829 cur->bc_ino.ip->i_ino);
3831 rp = xfs_btree_rec_addr(cur, 1, broot);
3832 crp = xfs_btree_rec_addr(cur, 1, cblock);
3833 xfs_btree_copy_recs(cur, rp, crp, numrecs);
3835 cur->bc_levels[0].bp = NULL;
3839 * Move the keyptrs from a child node block to the root block.
3841 * Since the keyptr size does not change, all we have to do is increase the
3842 * tree height, copy the keyptrs to the new internal node (cblock), shrink
3843 * the root, and copy the pointers there.
3846 xfs_btree_demote_node_child(
3847 struct xfs_btree_cur *cur,
3848 struct xfs_btree_block *cblock,
3852 struct xfs_btree_block *block;
3853 union xfs_btree_key *ckp;
3854 union xfs_btree_key *kp;
3855 union xfs_btree_ptr *cpp;
3856 union xfs_btree_ptr *pp;
3861 * Adjust the root btree node size and the record count to match the
3862 * doomed child so that we can copy the keyptrs ahead of changing the
3865 block = cur->bc_ops->broot_realloc(cur, numrecs);
3867 xfs_btree_set_numrecs(block, numrecs);
3868 ASSERT(block->bb_numrecs == cblock->bb_numrecs);
3870 /* Copy keys from the doomed block. */
3871 kp = xfs_btree_key_addr(cur, 1, block);
3872 ckp = xfs_btree_key_addr(cur, 1, cblock);
3873 xfs_btree_copy_keys(cur, kp, ckp, numrecs);
3875 /* Copy pointers from the doomed block. */
3876 pp = xfs_btree_ptr_addr(cur, 1, block);
3877 cpp = xfs_btree_ptr_addr(cur, 1, cblock);
3878 for (i = 0; i < numrecs; i++) {
3879 error = xfs_btree_debug_check_ptr(cur, cpp, i, level - 1);
3883 xfs_btree_copy_ptrs(cur, pp, cpp, numrecs);
3885 /* Decrease tree height, adjusting the root block level to match. */
3886 cur->bc_levels[level - 1].bp = NULL;
3887 be16_add_cpu(&block->bb_level, -1);
3893 * Try to merge a non-leaf block back into the inode root.
3895 * Note: the killroot names comes from the fact that we're effectively
3896 * killing the old root block. But because we can't just delete the
3897 * inode we have to copy the single block it was pointing to into the
3901 xfs_btree_kill_iroot(
3902 struct xfs_btree_cur *cur)
3904 struct xfs_inode *ip = cur->bc_ino.ip;
3905 struct xfs_btree_block *block;
3906 struct xfs_btree_block *cblock;
3907 struct xfs_buf *cbp;
3912 union xfs_btree_ptr ptr;
3915 ASSERT(cur->bc_ops->type == XFS_BTREE_TYPE_INODE);
3916 ASSERT((cur->bc_ops->geom_flags & XFS_BTGEO_IROOT_RECORDS) ||
3917 cur->bc_nlevels > 1);
3920 * Don't deal with the root block needs to be a leaf case.
3921 * We're just going to turn the thing back into extents anyway.
3923 level = cur->bc_nlevels - 1;
3924 if (level == 1 && !(cur->bc_ops->geom_flags & XFS_BTGEO_IROOT_RECORDS))
3927 /* If we're already a leaf, jump out. */
3932 * Give up if the root has multiple children.
3934 block = xfs_btree_get_iroot(cur);
3935 if (xfs_btree_get_numrecs(block) != 1)
3938 cblock = xfs_btree_get_block(cur, level - 1, &cbp);
3939 numrecs = xfs_btree_get_numrecs(cblock);
3942 * Only do this if the next level will fit.
3943 * Then the data must be copied up to the inode,
3944 * instead of freeing the root you free the next level.
3946 if (numrecs > cur->bc_ops->get_dmaxrecs(cur, level))
3949 XFS_BTREE_STATS_INC(cur, killroot);
3952 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_LEFTSIB);
3953 ASSERT(xfs_btree_ptr_is_null(cur, &ptr));
3954 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_RIGHTSIB);
3955 ASSERT(xfs_btree_ptr_is_null(cur, &ptr));
3959 error = xfs_btree_demote_node_child(cur, cblock, level,
3964 xfs_btree_demote_leaf_child(cur, cblock, numrecs);
3966 error = xfs_btree_free_block(cur, cbp);
3970 xfs_trans_log_inode(cur->bc_tp, ip,
3971 XFS_ILOG_CORE | xfs_ilog_fbroot(cur->bc_ino.whichfork));
3977 * Kill the current root node, and replace it with it's only child node.
3980 xfs_btree_kill_root(
3981 struct xfs_btree_cur *cur,
3984 union xfs_btree_ptr *newroot)
3988 XFS_BTREE_STATS_INC(cur, killroot);
3991 * Update the root pointer, decreasing the level by 1 and then
3992 * free the old root.
3994 xfs_btree_set_root(cur, newroot, -1);
3996 error = xfs_btree_free_block(cur, bp);
4000 cur->bc_levels[level].bp = NULL;
4001 cur->bc_levels[level].ra = 0;
4008 xfs_btree_dec_cursor(
4009 struct xfs_btree_cur *cur,
4017 error = xfs_btree_decrement(cur, level, &i);
4027 * Single level of the btree record deletion routine.
4028 * Delete record pointed to by cur/level.
4029 * Remove the record from its block then rebalance the tree.
4030 * Return 0 for error, 1 for done, 2 to go on to the next level.
4032 STATIC int /* error */
4034 struct xfs_btree_cur *cur, /* btree cursor */
4035 int level, /* level removing record from */
4036 int *stat) /* fail/done/go-on */
4038 struct xfs_btree_block *block; /* btree block */
4039 union xfs_btree_ptr cptr; /* current block ptr */
4040 struct xfs_buf *bp; /* buffer for block */
4041 int error; /* error return value */
4042 int i; /* loop counter */
4043 union xfs_btree_ptr lptr; /* left sibling block ptr */
4044 struct xfs_buf *lbp; /* left buffer pointer */
4045 struct xfs_btree_block *left; /* left btree block */
4046 int lrecs = 0; /* left record count */
4047 int ptr; /* key/record index */
4048 union xfs_btree_ptr rptr; /* right sibling block ptr */
4049 struct xfs_buf *rbp; /* right buffer pointer */
4050 struct xfs_btree_block *right; /* right btree block */
4051 struct xfs_btree_block *rrblock; /* right-right btree block */
4052 struct xfs_buf *rrbp; /* right-right buffer pointer */
4053 int rrecs = 0; /* right record count */
4054 struct xfs_btree_cur *tcur; /* temporary btree cursor */
4055 int numrecs; /* temporary numrec count */
4059 /* Get the index of the entry being deleted, check for nothing there. */
4060 ptr = cur->bc_levels[level].ptr;
4066 /* Get the buffer & block containing the record or key/ptr. */
4067 block = xfs_btree_get_block(cur, level, &bp);
4068 numrecs = xfs_btree_get_numrecs(block);
4071 error = xfs_btree_check_block(cur, block, level, bp);
4076 /* Fail if we're off the end of the block. */
4077 if (ptr > numrecs) {
4082 XFS_BTREE_STATS_INC(cur, delrec);
4083 XFS_BTREE_STATS_ADD(cur, moves, numrecs - ptr);
4085 /* Excise the entries being deleted. */
4087 /* It's a nonleaf. operate on keys and ptrs */
4088 union xfs_btree_key *lkp;
4089 union xfs_btree_ptr *lpp;
4091 lkp = xfs_btree_key_addr(cur, ptr + 1, block);
4092 lpp = xfs_btree_ptr_addr(cur, ptr + 1, block);
4094 for (i = 0; i < numrecs - ptr; i++) {
4095 error = xfs_btree_debug_check_ptr(cur, lpp, i, level);
4100 if (ptr < numrecs) {
4101 xfs_btree_shift_keys(cur, lkp, -1, numrecs - ptr);
4102 xfs_btree_shift_ptrs(cur, lpp, -1, numrecs - ptr);
4103 xfs_btree_log_keys(cur, bp, ptr, numrecs - 1);
4104 xfs_btree_log_ptrs(cur, bp, ptr, numrecs - 1);
4107 /* It's a leaf. operate on records */
4108 if (ptr < numrecs) {
4109 xfs_btree_shift_recs(cur,
4110 xfs_btree_rec_addr(cur, ptr + 1, block),
4112 xfs_btree_log_recs(cur, bp, ptr, numrecs - 1);
4117 * Decrement and log the number of entries in the block.
4119 xfs_btree_set_numrecs(block, --numrecs);
4120 xfs_btree_log_block(cur, bp, XFS_BB_NUMRECS);
4123 * We're at the root level. First, shrink the root block in-memory.
4124 * Try to get rid of the next level down. If we can't then there's
4125 * nothing left to do. numrecs was decremented above.
4127 if (xfs_btree_at_iroot(cur, level)) {
4128 cur->bc_ops->broot_realloc(cur, numrecs);
4130 error = xfs_btree_kill_iroot(cur);
4134 error = xfs_btree_dec_cursor(cur, level, stat);
4142 * If this is the root level, and there's only one entry left, and it's
4143 * NOT the leaf level, then we can get rid of this level.
4145 if (level == cur->bc_nlevels - 1) {
4146 if (numrecs == 1 && level > 0) {
4147 union xfs_btree_ptr *pp;
4149 * pp is still set to the first pointer in the block.
4150 * Make it the new root of the btree.
4152 pp = xfs_btree_ptr_addr(cur, 1, block);
4153 error = xfs_btree_kill_root(cur, bp, level, pp);
4156 } else if (level > 0) {
4157 error = xfs_btree_dec_cursor(cur, level, stat);
4166 * If we deleted the leftmost entry in the block, update the
4167 * key values above us in the tree.
4169 if (xfs_btree_needs_key_update(cur, ptr)) {
4170 error = xfs_btree_update_keys(cur, level);
4176 * If the number of records remaining in the block is at least
4177 * the minimum, we're done.
4179 if (numrecs >= cur->bc_ops->get_minrecs(cur, level)) {
4180 error = xfs_btree_dec_cursor(cur, level, stat);
4187 * Otherwise, we have to move some records around to keep the
4188 * tree balanced. Look at the left and right sibling blocks to
4189 * see if we can re-balance by moving only one record.
4191 xfs_btree_get_sibling(cur, block, &rptr, XFS_BB_RIGHTSIB);
4192 xfs_btree_get_sibling(cur, block, &lptr, XFS_BB_LEFTSIB);
4194 if (cur->bc_ops->type == XFS_BTREE_TYPE_INODE) {
4196 * One child of root, need to get a chance to copy its contents
4197 * into the root and delete it. Can't go up to next level,
4198 * there's nothing to delete there.
4200 if (xfs_btree_ptr_is_null(cur, &rptr) &&
4201 xfs_btree_ptr_is_null(cur, &lptr) &&
4202 level == cur->bc_nlevels - 2) {
4203 error = xfs_btree_kill_iroot(cur);
4205 error = xfs_btree_dec_cursor(cur, level, stat);
4212 ASSERT(!xfs_btree_ptr_is_null(cur, &rptr) ||
4213 !xfs_btree_ptr_is_null(cur, &lptr));
4216 * Duplicate the cursor so our btree manipulations here won't
4217 * disrupt the next level up.
4219 error = xfs_btree_dup_cursor(cur, &tcur);
4224 * If there's a right sibling, see if it's ok to shift an entry
4227 if (!xfs_btree_ptr_is_null(cur, &rptr)) {
4229 * Move the temp cursor to the last entry in the next block.
4230 * Actually any entry but the first would suffice.
4232 i = xfs_btree_lastrec(tcur, level);
4233 if (XFS_IS_CORRUPT(cur->bc_mp, i != 1)) {
4234 xfs_btree_mark_sick(cur);
4235 error = -EFSCORRUPTED;
4239 error = xfs_btree_increment(tcur, level, &i);
4242 if (XFS_IS_CORRUPT(cur->bc_mp, i != 1)) {
4243 xfs_btree_mark_sick(cur);
4244 error = -EFSCORRUPTED;
4248 i = xfs_btree_lastrec(tcur, level);
4249 if (XFS_IS_CORRUPT(cur->bc_mp, i != 1)) {
4250 xfs_btree_mark_sick(cur);
4251 error = -EFSCORRUPTED;
4255 /* Grab a pointer to the block. */
4256 right = xfs_btree_get_block(tcur, level, &rbp);
4258 error = xfs_btree_check_block(tcur, right, level, rbp);
4262 /* Grab the current block number, for future use. */
4263 xfs_btree_get_sibling(tcur, right, &cptr, XFS_BB_LEFTSIB);
4266 * If right block is full enough so that removing one entry
4267 * won't make it too empty, and left-shifting an entry out
4268 * of right to us works, we're done.
4270 if (xfs_btree_get_numrecs(right) - 1 >=
4271 cur->bc_ops->get_minrecs(tcur, level)) {
4272 error = xfs_btree_lshift(tcur, level, &i);
4276 ASSERT(xfs_btree_get_numrecs(block) >=
4277 cur->bc_ops->get_minrecs(tcur, level));
4279 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
4282 error = xfs_btree_dec_cursor(cur, level, stat);
4290 * Otherwise, grab the number of records in right for
4291 * future reference, and fix up the temp cursor to point
4292 * to our block again (last record).
4294 rrecs = xfs_btree_get_numrecs(right);
4295 if (!xfs_btree_ptr_is_null(cur, &lptr)) {
4296 i = xfs_btree_firstrec(tcur, level);
4297 if (XFS_IS_CORRUPT(cur->bc_mp, i != 1)) {
4298 xfs_btree_mark_sick(cur);
4299 error = -EFSCORRUPTED;
4303 error = xfs_btree_decrement(tcur, level, &i);
4306 if (XFS_IS_CORRUPT(cur->bc_mp, i != 1)) {
4307 xfs_btree_mark_sick(cur);
4308 error = -EFSCORRUPTED;
4315 * If there's a left sibling, see if it's ok to shift an entry
4318 if (!xfs_btree_ptr_is_null(cur, &lptr)) {
4320 * Move the temp cursor to the first entry in the
4323 i = xfs_btree_firstrec(tcur, level);
4324 if (XFS_IS_CORRUPT(cur->bc_mp, i != 1)) {
4325 xfs_btree_mark_sick(cur);
4326 error = -EFSCORRUPTED;
4330 error = xfs_btree_decrement(tcur, level, &i);
4333 i = xfs_btree_firstrec(tcur, level);
4334 if (XFS_IS_CORRUPT(cur->bc_mp, i != 1)) {
4335 xfs_btree_mark_sick(cur);
4336 error = -EFSCORRUPTED;
4340 /* Grab a pointer to the block. */
4341 left = xfs_btree_get_block(tcur, level, &lbp);
4343 error = xfs_btree_check_block(cur, left, level, lbp);
4347 /* Grab the current block number, for future use. */
4348 xfs_btree_get_sibling(tcur, left, &cptr, XFS_BB_RIGHTSIB);
4351 * If left block is full enough so that removing one entry
4352 * won't make it too empty, and right-shifting an entry out
4353 * of left to us works, we're done.
4355 if (xfs_btree_get_numrecs(left) - 1 >=
4356 cur->bc_ops->get_minrecs(tcur, level)) {
4357 error = xfs_btree_rshift(tcur, level, &i);
4361 ASSERT(xfs_btree_get_numrecs(block) >=
4362 cur->bc_ops->get_minrecs(tcur, level));
4363 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
4366 cur->bc_levels[0].ptr++;
4374 * Otherwise, grab the number of records in right for
4377 lrecs = xfs_btree_get_numrecs(left);
4380 /* Delete the temp cursor, we're done with it. */
4381 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
4384 /* If here, we need to do a join to keep the tree balanced. */
4385 ASSERT(!xfs_btree_ptr_is_null(cur, &cptr));
4387 if (!xfs_btree_ptr_is_null(cur, &lptr) &&
4388 lrecs + xfs_btree_get_numrecs(block) <=
4389 cur->bc_ops->get_maxrecs(cur, level)) {
4391 * Set "right" to be the starting block,
4392 * "left" to be the left neighbor.
4397 error = xfs_btree_read_buf_block(cur, &lptr, 0, &left, &lbp);
4402 * If that won't work, see if we can join with the right neighbor block.
4404 } else if (!xfs_btree_ptr_is_null(cur, &rptr) &&
4405 rrecs + xfs_btree_get_numrecs(block) <=
4406 cur->bc_ops->get_maxrecs(cur, level)) {
4408 * Set "left" to be the starting block,
4409 * "right" to be the right neighbor.
4414 error = xfs_btree_read_buf_block(cur, &rptr, 0, &right, &rbp);
4419 * Otherwise, we can't fix the imbalance.
4420 * Just return. This is probably a logic error, but it's not fatal.
4423 error = xfs_btree_dec_cursor(cur, level, stat);
4429 rrecs = xfs_btree_get_numrecs(right);
4430 lrecs = xfs_btree_get_numrecs(left);
4433 * We're now going to join "left" and "right" by moving all the stuff
4434 * in "right" to "left" and deleting "right".
4436 XFS_BTREE_STATS_ADD(cur, moves, rrecs);
4438 /* It's a non-leaf. Move keys and pointers. */
4439 union xfs_btree_key *lkp; /* left btree key */
4440 union xfs_btree_ptr *lpp; /* left address pointer */
4441 union xfs_btree_key *rkp; /* right btree key */
4442 union xfs_btree_ptr *rpp; /* right address pointer */
4444 lkp = xfs_btree_key_addr(cur, lrecs + 1, left);
4445 lpp = xfs_btree_ptr_addr(cur, lrecs + 1, left);
4446 rkp = xfs_btree_key_addr(cur, 1, right);
4447 rpp = xfs_btree_ptr_addr(cur, 1, right);
4449 for (i = 1; i < rrecs; i++) {
4450 error = xfs_btree_debug_check_ptr(cur, rpp, i, level);
4455 xfs_btree_copy_keys(cur, lkp, rkp, rrecs);
4456 xfs_btree_copy_ptrs(cur, lpp, rpp, rrecs);
4458 xfs_btree_log_keys(cur, lbp, lrecs + 1, lrecs + rrecs);
4459 xfs_btree_log_ptrs(cur, lbp, lrecs + 1, lrecs + rrecs);
4461 /* It's a leaf. Move records. */
4462 union xfs_btree_rec *lrp; /* left record pointer */
4463 union xfs_btree_rec *rrp; /* right record pointer */
4465 lrp = xfs_btree_rec_addr(cur, lrecs + 1, left);
4466 rrp = xfs_btree_rec_addr(cur, 1, right);
4468 xfs_btree_copy_recs(cur, lrp, rrp, rrecs);
4469 xfs_btree_log_recs(cur, lbp, lrecs + 1, lrecs + rrecs);
4472 XFS_BTREE_STATS_INC(cur, join);
4475 * Fix up the number of records and right block pointer in the
4476 * surviving block, and log it.
4478 xfs_btree_set_numrecs(left, lrecs + rrecs);
4479 xfs_btree_get_sibling(cur, right, &cptr, XFS_BB_RIGHTSIB);
4480 xfs_btree_set_sibling(cur, left, &cptr, XFS_BB_RIGHTSIB);
4481 xfs_btree_log_block(cur, lbp, XFS_BB_NUMRECS | XFS_BB_RIGHTSIB);
4483 /* If there is a right sibling, point it to the remaining block. */
4484 xfs_btree_get_sibling(cur, left, &cptr, XFS_BB_RIGHTSIB);
4485 if (!xfs_btree_ptr_is_null(cur, &cptr)) {
4486 error = xfs_btree_read_buf_block(cur, &cptr, 0, &rrblock, &rrbp);
4489 xfs_btree_set_sibling(cur, rrblock, &lptr, XFS_BB_LEFTSIB);
4490 xfs_btree_log_block(cur, rrbp, XFS_BB_LEFTSIB);
4493 /* Free the deleted block. */
4494 error = xfs_btree_free_block(cur, rbp);
4499 * If we joined with the left neighbor, set the buffer in the
4500 * cursor to the left block, and fix up the index.
4503 cur->bc_levels[level].bp = lbp;
4504 cur->bc_levels[level].ptr += lrecs;
4505 cur->bc_levels[level].ra = 0;
4508 * If we joined with the right neighbor and there's a level above
4509 * us, increment the cursor at that level.
4511 else if (cur->bc_ops->type == XFS_BTREE_TYPE_INODE ||
4512 level + 1 < cur->bc_nlevels) {
4513 error = xfs_btree_increment(cur, level + 1, &i);
4519 * Readjust the ptr at this level if it's not a leaf, since it's
4520 * still pointing at the deletion point, which makes the cursor
4521 * inconsistent. If this makes the ptr 0, the caller fixes it up.
4522 * We can't use decrement because it would change the next level up.
4525 cur->bc_levels[level].ptr--;
4528 * We combined blocks, so we have to update the parent keys if the
4529 * btree supports overlapped intervals. However,
4530 * bc_levels[level + 1].ptr points to the old block so that the caller
4531 * knows which record to delete. Therefore, the caller must be savvy
4532 * enough to call updkeys for us if we return stat == 2. The other
4533 * exit points from this function don't require deletions further up
4534 * the tree, so they can call updkeys directly.
4537 /* Return value means the next level up has something to do. */
4543 xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
4548 * Delete the record pointed to by cur.
4549 * The cursor refers to the place where the record was (could be inserted)
4550 * when the operation returns.
4554 struct xfs_btree_cur *cur,
4555 int *stat) /* success/failure */
4557 int error; /* error return value */
4560 bool joined = false;
4563 * Go up the tree, starting at leaf level.
4565 * If 2 is returned then a join was done; go to the next level.
4566 * Otherwise we are done.
4568 for (level = 0, i = 2; i == 2; level++) {
4569 error = xfs_btree_delrec(cur, level, &i);
4577 * If we combined blocks as part of deleting the record, delrec won't
4578 * have updated the parent high keys so we have to do that here.
4580 if (joined && (cur->bc_ops->geom_flags & XFS_BTGEO_OVERLAPPING)) {
4581 error = xfs_btree_updkeys_force(cur, 0);
4587 for (level = 1; level < cur->bc_nlevels; level++) {
4588 if (cur->bc_levels[level].ptr == 0) {
4589 error = xfs_btree_decrement(cur, level, &i);
4604 * Get the data from the pointed-to record.
4608 struct xfs_btree_cur *cur, /* btree cursor */
4609 union xfs_btree_rec **recp, /* output: btree record */
4610 int *stat) /* output: success/failure */
4612 struct xfs_btree_block *block; /* btree block */
4613 struct xfs_buf *bp; /* buffer pointer */
4614 int ptr; /* record number */
4616 int error; /* error return value */
4619 ptr = cur->bc_levels[0].ptr;
4620 block = xfs_btree_get_block(cur, 0, &bp);
4623 error = xfs_btree_check_block(cur, block, 0, bp);
4629 * Off the right end or left end, return failure.
4631 if (ptr > xfs_btree_get_numrecs(block) || ptr <= 0) {
4637 * Point to the record and extract its data.
4639 *recp = xfs_btree_rec_addr(cur, ptr, block);
4644 /* Visit a block in a btree. */
4646 xfs_btree_visit_block(
4647 struct xfs_btree_cur *cur,
4649 xfs_btree_visit_blocks_fn fn,
4652 struct xfs_btree_block *block;
4654 union xfs_btree_ptr rptr, bufptr;
4657 /* do right sibling readahead */
4658 xfs_btree_readahead(cur, level, XFS_BTCUR_RIGHTRA);
4659 block = xfs_btree_get_block(cur, level, &bp);
4661 /* process the block */
4662 error = fn(cur, level, data);
4666 /* now read rh sibling block for next iteration */
4667 xfs_btree_get_sibling(cur, block, &rptr, XFS_BB_RIGHTSIB);
4668 if (xfs_btree_ptr_is_null(cur, &rptr))
4672 * We only visit blocks once in this walk, so we have to avoid the
4673 * internal xfs_btree_lookup_get_block() optimisation where it will
4674 * return the same block without checking if the right sibling points
4675 * back to us and creates a cyclic reference in the btree.
4677 xfs_btree_buf_to_ptr(cur, bp, &bufptr);
4678 if (xfs_btree_ptrs_equal(cur, &rptr, &bufptr)) {
4679 xfs_btree_mark_sick(cur);
4680 return -EFSCORRUPTED;
4683 return xfs_btree_lookup_get_block(cur, level, &rptr, &block);
4687 /* Visit every block in a btree. */
4689 xfs_btree_visit_blocks(
4690 struct xfs_btree_cur *cur,
4691 xfs_btree_visit_blocks_fn fn,
4695 union xfs_btree_ptr lptr;
4697 struct xfs_btree_block *block = NULL;
4700 xfs_btree_init_ptr_from_cur(cur, &lptr);
4702 /* for each level */
4703 for (level = cur->bc_nlevels - 1; level >= 0; level--) {
4704 /* grab the left hand block */
4705 error = xfs_btree_lookup_get_block(cur, level, &lptr, &block);
4709 /* readahead the left most block for the next level down */
4711 union xfs_btree_ptr *ptr;
4713 ptr = xfs_btree_ptr_addr(cur, 1, block);
4714 xfs_btree_readahead_ptr(cur, ptr, 1);
4716 /* save for the next iteration of the loop */
4717 xfs_btree_copy_ptrs(cur, &lptr, ptr, 1);
4719 if (!(flags & XFS_BTREE_VISIT_LEAVES))
4721 } else if (!(flags & XFS_BTREE_VISIT_RECORDS)) {
4725 /* for each buffer in the level */
4727 error = xfs_btree_visit_block(cur, level, fn, data);
4730 if (error != -ENOENT)
4738 * Change the owner of a btree.
4740 * The mechanism we use here is ordered buffer logging. Because we don't know
4741 * how many buffers were are going to need to modify, we don't really want to
4742 * have to make transaction reservations for the worst case of every buffer in a
4743 * full size btree as that may be more space that we can fit in the log....
4745 * We do the btree walk in the most optimal manner possible - we have sibling
4746 * pointers so we can just walk all the blocks on each level from left to right
4747 * in a single pass, and then move to the next level and do the same. We can
4748 * also do readahead on the sibling pointers to get IO moving more quickly,
4749 * though for slow disks this is unlikely to make much difference to performance
4750 * as the amount of CPU work we have to do before moving to the next block is
4753 * For each btree block that we load, modify the owner appropriately, set the
4754 * buffer as an ordered buffer and log it appropriately. We need to ensure that
4755 * we mark the region we change dirty so that if the buffer is relogged in
4756 * a subsequent transaction the changes we make here as an ordered buffer are
4757 * correctly relogged in that transaction. If we are in recovery context, then
4758 * just queue the modified buffer as delayed write buffer so the transaction
4759 * recovery completion writes the changes to disk.
4761 struct xfs_btree_block_change_owner_info {
4763 struct list_head *buffer_list;
4767 xfs_btree_block_change_owner(
4768 struct xfs_btree_cur *cur,
4772 struct xfs_btree_block_change_owner_info *bbcoi = data;
4773 struct xfs_btree_block *block;
4776 /* modify the owner */
4777 block = xfs_btree_get_block(cur, level, &bp);
4778 if (cur->bc_ops->ptr_len == XFS_BTREE_LONG_PTR_LEN) {
4779 if (block->bb_u.l.bb_owner == cpu_to_be64(bbcoi->new_owner))
4781 block->bb_u.l.bb_owner = cpu_to_be64(bbcoi->new_owner);
4783 if (block->bb_u.s.bb_owner == cpu_to_be32(bbcoi->new_owner))
4785 block->bb_u.s.bb_owner = cpu_to_be32(bbcoi->new_owner);
4789 * If the block is a root block hosted in an inode, we might not have a
4790 * buffer pointer here and we shouldn't attempt to log the change as the
4791 * information is already held in the inode and discarded when the root
4792 * block is formatted into the on-disk inode fork. We still change it,
4793 * though, so everything is consistent in memory.
4796 ASSERT(cur->bc_ops->type == XFS_BTREE_TYPE_INODE);
4797 ASSERT(level == cur->bc_nlevels - 1);
4802 if (!xfs_trans_ordered_buf(cur->bc_tp, bp)) {
4803 xfs_btree_log_block(cur, bp, XFS_BB_OWNER);
4807 xfs_buf_delwri_queue(bp, bbcoi->buffer_list);
4814 xfs_btree_change_owner(
4815 struct xfs_btree_cur *cur,
4817 struct list_head *buffer_list)
4819 struct xfs_btree_block_change_owner_info bbcoi;
4821 bbcoi.new_owner = new_owner;
4822 bbcoi.buffer_list = buffer_list;
4824 return xfs_btree_visit_blocks(cur, xfs_btree_block_change_owner,
4825 XFS_BTREE_VISIT_ALL, &bbcoi);
4828 /* Verify the v5 fields of a long-format btree block. */
4830 xfs_btree_fsblock_v5hdr_verify(
4834 struct xfs_mount *mp = bp->b_mount;
4835 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
4837 if (!xfs_has_crc(mp))
4838 return __this_address;
4839 if (!uuid_equal(&block->bb_u.l.bb_uuid, &mp->m_sb.sb_meta_uuid))
4840 return __this_address;
4841 if (block->bb_u.l.bb_blkno != cpu_to_be64(xfs_buf_daddr(bp)))
4842 return __this_address;
4843 if (owner != XFS_RMAP_OWN_UNKNOWN &&
4844 be64_to_cpu(block->bb_u.l.bb_owner) != owner)
4845 return __this_address;
4849 /* Verify a long-format btree block. */
4851 xfs_btree_fsblock_verify(
4853 unsigned int max_recs)
4855 struct xfs_mount *mp = bp->b_mount;
4856 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
4860 ASSERT(!xfs_buftarg_is_mem(bp->b_target));
4862 /* numrecs verification */
4863 if (be16_to_cpu(block->bb_numrecs) > max_recs)
4864 return __this_address;
4866 /* sibling pointer verification */
4867 fsb = XFS_DADDR_TO_FSB(mp, xfs_buf_daddr(bp));
4868 fa = xfs_btree_check_fsblock_siblings(mp, fsb,
4869 block->bb_u.l.bb_leftsib);
4871 fa = xfs_btree_check_fsblock_siblings(mp, fsb,
4872 block->bb_u.l.bb_rightsib);
4876 /* Verify an in-memory btree block. */
4878 xfs_btree_memblock_verify(
4880 unsigned int max_recs)
4882 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
4883 struct xfs_buftarg *btp = bp->b_target;
4887 ASSERT(xfs_buftarg_is_mem(bp->b_target));
4889 /* numrecs verification */
4890 if (be16_to_cpu(block->bb_numrecs) > max_recs)
4891 return __this_address;
4893 /* sibling pointer verification */
4894 bno = xfs_daddr_to_xfbno(xfs_buf_daddr(bp));
4895 fa = xfs_btree_check_memblock_siblings(btp, bno,
4896 block->bb_u.l.bb_leftsib);
4899 fa = xfs_btree_check_memblock_siblings(btp, bno,
4900 block->bb_u.l.bb_rightsib);
4907 * xfs_btree_agblock_v5hdr_verify() -- verify the v5 fields of a short-format
4910 * @bp: buffer containing the btree block
4913 xfs_btree_agblock_v5hdr_verify(
4916 struct xfs_mount *mp = bp->b_mount;
4917 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
4918 struct xfs_perag *pag = bp->b_pag;
4920 if (!xfs_has_crc(mp))
4921 return __this_address;
4922 if (!uuid_equal(&block->bb_u.s.bb_uuid, &mp->m_sb.sb_meta_uuid))
4923 return __this_address;
4924 if (block->bb_u.s.bb_blkno != cpu_to_be64(xfs_buf_daddr(bp)))
4925 return __this_address;
4926 if (pag && be32_to_cpu(block->bb_u.s.bb_owner) != pag_agno(pag))
4927 return __this_address;
4932 * xfs_btree_agblock_verify() -- verify a short-format btree block
4934 * @bp: buffer containing the btree block
4935 * @max_recs: maximum records allowed in this btree node
4938 xfs_btree_agblock_verify(
4940 unsigned int max_recs)
4942 struct xfs_mount *mp = bp->b_mount;
4943 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
4944 xfs_agblock_t agbno;
4947 ASSERT(!xfs_buftarg_is_mem(bp->b_target));
4949 /* numrecs verification */
4950 if (be16_to_cpu(block->bb_numrecs) > max_recs)
4951 return __this_address;
4953 /* sibling pointer verification */
4954 agbno = xfs_daddr_to_agbno(mp, xfs_buf_daddr(bp));
4955 fa = xfs_btree_check_agblock_siblings(bp->b_pag, agbno,
4956 block->bb_u.s.bb_leftsib);
4958 fa = xfs_btree_check_agblock_siblings(bp->b_pag, agbno,
4959 block->bb_u.s.bb_rightsib);
4964 * For the given limits on leaf and keyptr records per block, calculate the
4965 * height of the tree needed to index the number of leaf records.
4968 xfs_btree_compute_maxlevels(
4969 const unsigned int *limits,
4970 unsigned long long records)
4972 unsigned long long level_blocks = howmany_64(records, limits[0]);
4973 unsigned int height = 1;
4975 while (level_blocks > 1) {
4976 level_blocks = howmany_64(level_blocks, limits[1]);
4984 * For the given limits on leaf and keyptr records per block, calculate the
4985 * number of blocks needed to index the given number of leaf records.
4988 xfs_btree_calc_size(
4989 const unsigned int *limits,
4990 unsigned long long records)
4992 unsigned long long level_blocks = howmany_64(records, limits[0]);
4993 unsigned long long blocks = level_blocks;
4995 while (level_blocks > 1) {
4996 level_blocks = howmany_64(level_blocks, limits[1]);
4997 blocks += level_blocks;
5004 * Given a number of available blocks for the btree to consume with records and
5005 * pointers, calculate the height of the tree needed to index all the records
5006 * that space can hold based on the number of pointers each interior node
5009 * We start by assuming a single level tree consumes a single block, then track
5010 * the number of blocks each node level consumes until we no longer have space
5011 * to store the next node level. At this point, we are indexing all the leaf
5012 * blocks in the space, and there's no more free space to split the tree any
5013 * further. That's our maximum btree height.
5016 xfs_btree_space_to_height(
5017 const unsigned int *limits,
5018 unsigned long long leaf_blocks)
5021 * The root btree block can have fewer than minrecs pointers in it
5022 * because the tree might not be big enough to require that amount of
5023 * fanout. Hence it has a minimum size of 2 pointers, not limits[1].
5025 unsigned long long node_blocks = 2;
5026 unsigned long long blocks_left = leaf_blocks - 1;
5027 unsigned int height = 1;
5029 if (leaf_blocks < 1)
5032 while (node_blocks < blocks_left) {
5033 blocks_left -= node_blocks;
5034 node_blocks *= limits[1];
5042 * Query a regular btree for all records overlapping a given interval.
5043 * Start with a LE lookup of the key of low_rec and return all records
5044 * until we find a record with a key greater than the key of high_rec.
5047 xfs_btree_simple_query_range(
5048 struct xfs_btree_cur *cur,
5049 const union xfs_btree_key *low_key,
5050 const union xfs_btree_key *high_key,
5051 xfs_btree_query_range_fn fn,
5054 union xfs_btree_rec *recp;
5055 union xfs_btree_key rec_key;
5057 bool firstrec = true;
5060 ASSERT(cur->bc_ops->init_high_key_from_rec);
5061 ASSERT(cur->bc_ops->diff_two_keys);
5064 * Find the leftmost record. The btree cursor must be set
5065 * to the low record used to generate low_key.
5068 error = xfs_btree_lookup(cur, XFS_LOOKUP_LE, &stat);
5072 /* Nothing? See if there's anything to the right. */
5074 error = xfs_btree_increment(cur, 0, &stat);
5080 /* Find the record. */
5081 error = xfs_btree_get_rec(cur, &recp, &stat);
5085 /* Skip if low_key > high_key(rec). */
5087 cur->bc_ops->init_high_key_from_rec(&rec_key, recp);
5089 if (xfs_btree_keycmp_gt(cur, low_key, &rec_key))
5093 /* Stop if low_key(rec) > high_key. */
5094 cur->bc_ops->init_key_from_rec(&rec_key, recp);
5095 if (xfs_btree_keycmp_gt(cur, &rec_key, high_key))
5099 error = fn(cur, recp, priv);
5104 /* Move on to the next record. */
5105 error = xfs_btree_increment(cur, 0, &stat);
5115 * Query an overlapped interval btree for all records overlapping a given
5116 * interval. This function roughly follows the algorithm given in
5117 * "Interval Trees" of _Introduction to Algorithms_, which is section
5118 * 14.3 in the 2nd and 3rd editions.
5120 * First, generate keys for the low and high records passed in.
5122 * For any leaf node, generate the high and low keys for the record.
5123 * If the record keys overlap with the query low/high keys, pass the
5124 * record to the function iterator.
5126 * For any internal node, compare the low and high keys of each
5127 * pointer against the query low/high keys. If there's an overlap,
5128 * follow the pointer.
5130 * As an optimization, we stop scanning a block when we find a low key
5131 * that is greater than the query's high key.
5134 xfs_btree_overlapped_query_range(
5135 struct xfs_btree_cur *cur,
5136 const union xfs_btree_key *low_key,
5137 const union xfs_btree_key *high_key,
5138 xfs_btree_query_range_fn fn,
5141 union xfs_btree_ptr ptr;
5142 union xfs_btree_ptr *pp;
5143 union xfs_btree_key rec_key;
5144 union xfs_btree_key rec_hkey;
5145 union xfs_btree_key *lkp;
5146 union xfs_btree_key *hkp;
5147 union xfs_btree_rec *recp;
5148 struct xfs_btree_block *block;
5154 /* Load the root of the btree. */
5155 level = cur->bc_nlevels - 1;
5156 xfs_btree_init_ptr_from_cur(cur, &ptr);
5157 error = xfs_btree_lookup_get_block(cur, level, &ptr, &block);
5160 xfs_btree_get_block(cur, level, &bp);
5161 trace_xfs_btree_overlapped_query_range(cur, level, bp);
5163 error = xfs_btree_check_block(cur, block, level, bp);
5167 cur->bc_levels[level].ptr = 1;
5169 while (level < cur->bc_nlevels) {
5170 block = xfs_btree_get_block(cur, level, &bp);
5172 /* End of node, pop back towards the root. */
5173 if (cur->bc_levels[level].ptr >
5174 be16_to_cpu(block->bb_numrecs)) {
5176 if (level < cur->bc_nlevels - 1)
5177 cur->bc_levels[level + 1].ptr++;
5183 /* Handle a leaf node. */
5184 recp = xfs_btree_rec_addr(cur, cur->bc_levels[0].ptr,
5187 cur->bc_ops->init_high_key_from_rec(&rec_hkey, recp);
5188 cur->bc_ops->init_key_from_rec(&rec_key, recp);
5191 * If (query's high key < record's low key), then there
5192 * are no more interesting records in this block. Pop
5193 * up to the leaf level to find more record blocks.
5195 * If (record's high key >= query's low key) and
5196 * (query's high key >= record's low key), then
5197 * this record overlaps the query range; callback.
5199 if (xfs_btree_keycmp_lt(cur, high_key, &rec_key))
5201 if (xfs_btree_keycmp_ge(cur, &rec_hkey, low_key)) {
5202 error = fn(cur, recp, priv);
5206 cur->bc_levels[level].ptr++;
5210 /* Handle an internal node. */
5211 lkp = xfs_btree_key_addr(cur, cur->bc_levels[level].ptr, block);
5212 hkp = xfs_btree_high_key_addr(cur, cur->bc_levels[level].ptr,
5214 pp = xfs_btree_ptr_addr(cur, cur->bc_levels[level].ptr, block);
5217 * If (query's high key < pointer's low key), then there are no
5218 * more interesting keys in this block. Pop up one leaf level
5219 * to continue looking for records.
5221 * If (pointer's high key >= query's low key) and
5222 * (query's high key >= pointer's low key), then
5223 * this record overlaps the query range; follow pointer.
5225 if (xfs_btree_keycmp_lt(cur, high_key, lkp))
5227 if (xfs_btree_keycmp_ge(cur, hkp, low_key)) {
5229 error = xfs_btree_lookup_get_block(cur, level, pp,
5233 xfs_btree_get_block(cur, level, &bp);
5234 trace_xfs_btree_overlapped_query_range(cur, level, bp);
5236 error = xfs_btree_check_block(cur, block, level, bp);
5240 cur->bc_levels[level].ptr = 1;
5243 cur->bc_levels[level].ptr++;
5248 * If we don't end this function with the cursor pointing at a record
5249 * block, a subsequent non-error cursor deletion will not release
5250 * node-level buffers, causing a buffer leak. This is quite possible
5251 * with a zero-results range query, so release the buffers if we
5252 * failed to return any results.
5254 if (cur->bc_levels[0].bp == NULL) {
5255 for (i = 0; i < cur->bc_nlevels; i++) {
5256 if (cur->bc_levels[i].bp) {
5257 xfs_trans_brelse(cur->bc_tp,
5258 cur->bc_levels[i].bp);
5259 cur->bc_levels[i].bp = NULL;
5260 cur->bc_levels[i].ptr = 0;
5261 cur->bc_levels[i].ra = 0;
5270 xfs_btree_key_from_irec(
5271 struct xfs_btree_cur *cur,
5272 union xfs_btree_key *key,
5273 const union xfs_btree_irec *irec)
5275 union xfs_btree_rec rec;
5277 cur->bc_rec = *irec;
5278 cur->bc_ops->init_rec_from_cur(cur, &rec);
5279 cur->bc_ops->init_key_from_rec(key, &rec);
5283 * Query a btree for all records overlapping a given interval of keys. The
5284 * supplied function will be called with each record found; return one of the
5285 * XFS_BTREE_QUERY_RANGE_{CONTINUE,ABORT} values or the usual negative error
5286 * code. This function returns -ECANCELED, zero, or a negative error code.
5289 xfs_btree_query_range(
5290 struct xfs_btree_cur *cur,
5291 const union xfs_btree_irec *low_rec,
5292 const union xfs_btree_irec *high_rec,
5293 xfs_btree_query_range_fn fn,
5296 union xfs_btree_key low_key;
5297 union xfs_btree_key high_key;
5299 /* Find the keys of both ends of the interval. */
5300 xfs_btree_key_from_irec(cur, &high_key, high_rec);
5301 xfs_btree_key_from_irec(cur, &low_key, low_rec);
5303 /* Enforce low key <= high key. */
5304 if (!xfs_btree_keycmp_le(cur, &low_key, &high_key))
5307 if (!(cur->bc_ops->geom_flags & XFS_BTGEO_OVERLAPPING))
5308 return xfs_btree_simple_query_range(cur, &low_key,
5309 &high_key, fn, priv);
5310 return xfs_btree_overlapped_query_range(cur, &low_key, &high_key,
5314 /* Query a btree for all records. */
5316 xfs_btree_query_all(
5317 struct xfs_btree_cur *cur,
5318 xfs_btree_query_range_fn fn,
5321 union xfs_btree_key low_key;
5322 union xfs_btree_key high_key;
5324 memset(&cur->bc_rec, 0, sizeof(cur->bc_rec));
5325 memset(&low_key, 0, sizeof(low_key));
5326 memset(&high_key, 0xFF, sizeof(high_key));
5328 return xfs_btree_simple_query_range(cur, &low_key, &high_key, fn, priv);
5332 xfs_btree_count_blocks_helper(
5333 struct xfs_btree_cur *cur,
5337 xfs_filblks_t *blocks = data;
5343 /* Count the blocks in a btree and return the result in *blocks. */
5345 xfs_btree_count_blocks(
5346 struct xfs_btree_cur *cur,
5347 xfs_filblks_t *blocks)
5350 return xfs_btree_visit_blocks(cur, xfs_btree_count_blocks_helper,
5351 XFS_BTREE_VISIT_ALL, blocks);
5354 /* Compare two btree pointers. */
5356 xfs_btree_diff_two_ptrs(
5357 struct xfs_btree_cur *cur,
5358 const union xfs_btree_ptr *a,
5359 const union xfs_btree_ptr *b)
5361 if (cur->bc_ops->ptr_len == XFS_BTREE_LONG_PTR_LEN)
5362 return (int64_t)be64_to_cpu(a->l) - be64_to_cpu(b->l);
5363 return (int64_t)be32_to_cpu(a->s) - be32_to_cpu(b->s);
5366 struct xfs_btree_has_records {
5367 /* Keys for the start and end of the range we want to know about. */
5368 union xfs_btree_key start_key;
5369 union xfs_btree_key end_key;
5371 /* Mask for key comparisons, if desired. */
5372 const union xfs_btree_key *key_mask;
5374 /* Highest record key we've seen so far. */
5375 union xfs_btree_key high_key;
5377 enum xbtree_recpacking outcome;
5381 xfs_btree_has_records_helper(
5382 struct xfs_btree_cur *cur,
5383 const union xfs_btree_rec *rec,
5386 union xfs_btree_key rec_key;
5387 union xfs_btree_key rec_high_key;
5388 struct xfs_btree_has_records *info = priv;
5389 enum xbtree_key_contig key_contig;
5391 cur->bc_ops->init_key_from_rec(&rec_key, rec);
5393 if (info->outcome == XBTREE_RECPACKING_EMPTY) {
5394 info->outcome = XBTREE_RECPACKING_SPARSE;
5397 * If the first record we find does not overlap the start key,
5398 * then there is a hole at the start of the search range.
5399 * Classify this as sparse and stop immediately.
5401 if (xfs_btree_masked_keycmp_lt(cur, &info->start_key, &rec_key,
5406 * If a subsequent record does not overlap with the any record
5407 * we've seen so far, there is a hole in the middle of the
5408 * search range. Classify this as sparse and stop.
5409 * If the keys overlap and this btree does not allow overlap,
5410 * signal corruption.
5412 key_contig = cur->bc_ops->keys_contiguous(cur, &info->high_key,
5413 &rec_key, info->key_mask);
5414 if (key_contig == XBTREE_KEY_OVERLAP &&
5415 !(cur->bc_ops->geom_flags & XFS_BTGEO_OVERLAPPING))
5416 return -EFSCORRUPTED;
5417 if (key_contig == XBTREE_KEY_GAP)
5422 * If high_key(rec) is larger than any other high key we've seen,
5423 * remember it for later.
5425 cur->bc_ops->init_high_key_from_rec(&rec_high_key, rec);
5426 if (xfs_btree_masked_keycmp_gt(cur, &rec_high_key, &info->high_key,
5428 info->high_key = rec_high_key; /* struct copy */
5434 * Scan part of the keyspace of a btree and tell us if that keyspace does not
5435 * map to any records; is fully mapped to records; or is partially mapped to
5436 * records. This is the btree record equivalent to determining if a file is
5439 * For most btree types, the record scan should use all available btree key
5440 * fields to compare the keys encountered. These callers should pass NULL for
5441 * @mask. However, some callers (e.g. scanning physical space in the rmapbt)
5442 * want to ignore some part of the btree record keyspace when performing the
5443 * comparison. These callers should pass in a union xfs_btree_key object with
5444 * the fields that *should* be a part of the comparison set to any nonzero
5445 * value, and the rest zeroed.
5448 xfs_btree_has_records(
5449 struct xfs_btree_cur *cur,
5450 const union xfs_btree_irec *low,
5451 const union xfs_btree_irec *high,
5452 const union xfs_btree_key *mask,
5453 enum xbtree_recpacking *outcome)
5455 struct xfs_btree_has_records info = {
5456 .outcome = XBTREE_RECPACKING_EMPTY,
5461 /* Not all btrees support this operation. */
5462 if (!cur->bc_ops->keys_contiguous) {
5467 xfs_btree_key_from_irec(cur, &info.start_key, low);
5468 xfs_btree_key_from_irec(cur, &info.end_key, high);
5470 error = xfs_btree_query_range(cur, low, high,
5471 xfs_btree_has_records_helper, &info);
5472 if (error == -ECANCELED)
5477 if (info.outcome == XBTREE_RECPACKING_EMPTY)
5481 * If the largest high_key(rec) we saw during the walk is greater than
5482 * the end of the search range, classify this as full. Otherwise,
5483 * there is a hole at the end of the search range.
5485 if (xfs_btree_masked_keycmp_ge(cur, &info.high_key, &info.end_key,
5487 info.outcome = XBTREE_RECPACKING_FULL;
5490 *outcome = info.outcome;
5494 /* Are there more records in this btree? */
5496 xfs_btree_has_more_records(
5497 struct xfs_btree_cur *cur)
5499 struct xfs_btree_block *block;
5502 block = xfs_btree_get_block(cur, 0, &bp);
5504 /* There are still records in this block. */
5505 if (cur->bc_levels[0].ptr < xfs_btree_get_numrecs(block))
5508 /* There are more record blocks. */
5509 if (cur->bc_ops->ptr_len == XFS_BTREE_LONG_PTR_LEN)
5510 return block->bb_u.l.bb_rightsib != cpu_to_be64(NULLFSBLOCK);
5512 return block->bb_u.s.bb_rightsib != cpu_to_be32(NULLAGBLOCK);
5515 /* Set up all the btree cursor caches. */
5517 xfs_btree_init_cur_caches(void)
5521 error = xfs_allocbt_init_cur_cache();
5524 error = xfs_inobt_init_cur_cache();
5527 error = xfs_bmbt_init_cur_cache();
5530 error = xfs_rmapbt_init_cur_cache();
5533 error = xfs_refcountbt_init_cur_cache();
5536 error = xfs_rtrmapbt_init_cur_cache();
5539 error = xfs_rtrefcountbt_init_cur_cache();
5545 xfs_btree_destroy_cur_caches();
5549 /* Destroy all the btree cursor caches, if they've been allocated. */
5551 xfs_btree_destroy_cur_caches(void)
5553 xfs_allocbt_destroy_cur_cache();
5554 xfs_inobt_destroy_cur_cache();
5555 xfs_bmbt_destroy_cur_cache();
5556 xfs_rmapbt_destroy_cur_cache();
5557 xfs_refcountbt_destroy_cur_cache();
5558 xfs_rtrmapbt_destroy_cur_cache();
5559 xfs_rtrefcountbt_destroy_cur_cache();
5562 /* Move the btree cursor before the first record. */
5564 xfs_btree_goto_left_edge(
5565 struct xfs_btree_cur *cur)
5570 memset(&cur->bc_rec, 0, sizeof(cur->bc_rec));
5571 error = xfs_btree_lookup(cur, XFS_LOOKUP_LE, &stat);
5577 error = xfs_btree_decrement(cur, 0, &stat);
5582 xfs_btree_mark_sick(cur);
5583 return -EFSCORRUPTED;
5589 /* Allocate a block for an inode-rooted metadata btree. */
5591 xfs_btree_alloc_metafile_block(
5592 struct xfs_btree_cur *cur,
5593 const union xfs_btree_ptr *start,
5594 union xfs_btree_ptr *new,
5597 struct xfs_alloc_arg args = {
5600 .resv = XFS_AG_RESV_METAFILE,
5605 struct xfs_inode *ip = cur->bc_ino.ip;
5608 ASSERT(xfs_is_metadir_inode(ip));
5610 xfs_rmap_ino_bmbt_owner(&args.oinfo, ip->i_ino, cur->bc_ino.whichfork);
5611 error = xfs_alloc_vextent_start_ag(&args,
5612 XFS_INO_TO_FSB(cur->bc_mp, ip->i_ino));
5615 if (args.fsbno == NULLFSBLOCK) {
5619 ASSERT(args.len == 1);
5621 xfs_metafile_resv_alloc_space(ip, &args);
5623 new->l = cpu_to_be64(args.fsbno);
5628 /* Free a block from an inode-rooted metadata btree. */
5630 xfs_btree_free_metafile_block(
5631 struct xfs_btree_cur *cur,
5634 struct xfs_owner_info oinfo;
5635 struct xfs_mount *mp = cur->bc_mp;
5636 struct xfs_inode *ip = cur->bc_ino.ip;
5637 struct xfs_trans *tp = cur->bc_tp;
5638 xfs_fsblock_t fsbno = XFS_DADDR_TO_FSB(mp, xfs_buf_daddr(bp));
5641 ASSERT(xfs_is_metadir_inode(ip));
5643 xfs_rmap_ino_bmbt_owner(&oinfo, ip->i_ino, cur->bc_ino.whichfork);
5644 error = xfs_free_extent_later(tp, fsbno, 1, &oinfo, XFS_AG_RESV_METAFILE,
5649 xfs_metafile_resv_free_space(ip, tp, 1);