2 * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 #include "xfs_shared.h"
21 #include "xfs_format.h"
22 #include "xfs_log_format.h"
23 #include "xfs_trans_resv.h"
25 #include "xfs_mount.h"
26 #include "xfs_defer.h"
27 #include "xfs_inode.h"
28 #include "xfs_trans.h"
29 #include "xfs_inode_item.h"
30 #include "xfs_buf_item.h"
31 #include "xfs_btree.h"
32 #include "xfs_error.h"
33 #include "xfs_trace.h"
34 #include "xfs_cksum.h"
35 #include "xfs_alloc.h"
39 * Cursor allocation zone.
41 kmem_zone_t *xfs_btree_cur_zone;
44 * Btree magic numbers.
46 static const __uint32_t xfs_magics[2][XFS_BTNUM_MAX] = {
47 { XFS_ABTB_MAGIC, XFS_ABTC_MAGIC, 0, XFS_BMAP_MAGIC, XFS_IBT_MAGIC,
49 { XFS_ABTB_CRC_MAGIC, XFS_ABTC_CRC_MAGIC, XFS_RMAP_CRC_MAGIC,
50 XFS_BMAP_CRC_MAGIC, XFS_IBT_CRC_MAGIC, XFS_FIBT_CRC_MAGIC,
53 #define xfs_btree_magic(cur) \
54 xfs_magics[!!((cur)->bc_flags & XFS_BTREE_CRC_BLOCKS)][cur->bc_btnum]
56 STATIC int /* error (0 or EFSCORRUPTED) */
57 xfs_btree_check_lblock(
58 struct xfs_btree_cur *cur, /* btree cursor */
59 struct xfs_btree_block *block, /* btree long form block pointer */
60 int level, /* level of the btree block */
61 struct xfs_buf *bp) /* buffer for block, if any */
63 int lblock_ok = 1; /* block passes checks */
64 struct xfs_mount *mp; /* file system mount point */
68 if (xfs_sb_version_hascrc(&mp->m_sb)) {
69 lblock_ok = lblock_ok &&
70 uuid_equal(&block->bb_u.l.bb_uuid,
71 &mp->m_sb.sb_meta_uuid) &&
72 block->bb_u.l.bb_blkno == cpu_to_be64(
73 bp ? bp->b_bn : XFS_BUF_DADDR_NULL);
76 lblock_ok = lblock_ok &&
77 be32_to_cpu(block->bb_magic) == xfs_btree_magic(cur) &&
78 be16_to_cpu(block->bb_level) == level &&
79 be16_to_cpu(block->bb_numrecs) <=
80 cur->bc_ops->get_maxrecs(cur, level) &&
81 block->bb_u.l.bb_leftsib &&
82 (block->bb_u.l.bb_leftsib == cpu_to_be64(NULLFSBLOCK) ||
83 XFS_FSB_SANITY_CHECK(mp,
84 be64_to_cpu(block->bb_u.l.bb_leftsib))) &&
85 block->bb_u.l.bb_rightsib &&
86 (block->bb_u.l.bb_rightsib == cpu_to_be64(NULLFSBLOCK) ||
87 XFS_FSB_SANITY_CHECK(mp,
88 be64_to_cpu(block->bb_u.l.bb_rightsib)));
90 if (unlikely(XFS_TEST_ERROR(!lblock_ok, mp,
91 XFS_ERRTAG_BTREE_CHECK_LBLOCK,
92 XFS_RANDOM_BTREE_CHECK_LBLOCK))) {
94 trace_xfs_btree_corrupt(bp, _RET_IP_);
95 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp);
101 STATIC int /* error (0 or EFSCORRUPTED) */
102 xfs_btree_check_sblock(
103 struct xfs_btree_cur *cur, /* btree cursor */
104 struct xfs_btree_block *block, /* btree short form block pointer */
105 int level, /* level of the btree block */
106 struct xfs_buf *bp) /* buffer containing block */
108 struct xfs_mount *mp; /* file system mount point */
109 struct xfs_buf *agbp; /* buffer for ag. freespace struct */
110 struct xfs_agf *agf; /* ag. freespace structure */
111 xfs_agblock_t agflen; /* native ag. freespace length */
112 int sblock_ok = 1; /* block passes checks */
115 agbp = cur->bc_private.a.agbp;
116 agf = XFS_BUF_TO_AGF(agbp);
117 agflen = be32_to_cpu(agf->agf_length);
119 if (xfs_sb_version_hascrc(&mp->m_sb)) {
120 sblock_ok = sblock_ok &&
121 uuid_equal(&block->bb_u.s.bb_uuid,
122 &mp->m_sb.sb_meta_uuid) &&
123 block->bb_u.s.bb_blkno == cpu_to_be64(
124 bp ? bp->b_bn : XFS_BUF_DADDR_NULL);
127 sblock_ok = sblock_ok &&
128 be32_to_cpu(block->bb_magic) == xfs_btree_magic(cur) &&
129 be16_to_cpu(block->bb_level) == level &&
130 be16_to_cpu(block->bb_numrecs) <=
131 cur->bc_ops->get_maxrecs(cur, level) &&
132 (block->bb_u.s.bb_leftsib == cpu_to_be32(NULLAGBLOCK) ||
133 be32_to_cpu(block->bb_u.s.bb_leftsib) < agflen) &&
134 block->bb_u.s.bb_leftsib &&
135 (block->bb_u.s.bb_rightsib == cpu_to_be32(NULLAGBLOCK) ||
136 be32_to_cpu(block->bb_u.s.bb_rightsib) < agflen) &&
137 block->bb_u.s.bb_rightsib;
139 if (unlikely(XFS_TEST_ERROR(!sblock_ok, mp,
140 XFS_ERRTAG_BTREE_CHECK_SBLOCK,
141 XFS_RANDOM_BTREE_CHECK_SBLOCK))) {
143 trace_xfs_btree_corrupt(bp, _RET_IP_);
144 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp);
145 return -EFSCORRUPTED;
151 * Debug routine: check that block header is ok.
154 xfs_btree_check_block(
155 struct xfs_btree_cur *cur, /* btree cursor */
156 struct xfs_btree_block *block, /* generic btree block pointer */
157 int level, /* level of the btree block */
158 struct xfs_buf *bp) /* buffer containing block, if any */
160 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
161 return xfs_btree_check_lblock(cur, block, level, bp);
163 return xfs_btree_check_sblock(cur, block, level, bp);
167 * Check that (long) pointer is ok.
169 int /* error (0 or EFSCORRUPTED) */
170 xfs_btree_check_lptr(
171 struct xfs_btree_cur *cur, /* btree cursor */
172 xfs_fsblock_t bno, /* btree block disk address */
173 int level) /* btree block level */
175 XFS_WANT_CORRUPTED_RETURN(cur->bc_mp,
177 bno != NULLFSBLOCK &&
178 XFS_FSB_SANITY_CHECK(cur->bc_mp, bno));
184 * Check that (short) pointer is ok.
186 STATIC int /* error (0 or EFSCORRUPTED) */
187 xfs_btree_check_sptr(
188 struct xfs_btree_cur *cur, /* btree cursor */
189 xfs_agblock_t bno, /* btree block disk address */
190 int level) /* btree block level */
192 xfs_agblock_t agblocks = cur->bc_mp->m_sb.sb_agblocks;
194 XFS_WANT_CORRUPTED_RETURN(cur->bc_mp,
196 bno != NULLAGBLOCK &&
203 * Check that block ptr is ok.
205 STATIC int /* error (0 or EFSCORRUPTED) */
207 struct xfs_btree_cur *cur, /* btree cursor */
208 union xfs_btree_ptr *ptr, /* btree block disk address */
209 int index, /* offset from ptr to check */
210 int level) /* btree block level */
212 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
213 return xfs_btree_check_lptr(cur,
214 be64_to_cpu((&ptr->l)[index]), level);
216 return xfs_btree_check_sptr(cur,
217 be32_to_cpu((&ptr->s)[index]), level);
223 * Calculate CRC on the whole btree block and stuff it into the
224 * long-form btree header.
226 * Prior to calculting the CRC, pull the LSN out of the buffer log item and put
227 * it into the buffer so recovery knows what the last modification was that made
231 xfs_btree_lblock_calc_crc(
234 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
235 struct xfs_buf_log_item *bip = bp->b_fspriv;
237 if (!xfs_sb_version_hascrc(&bp->b_target->bt_mount->m_sb))
240 block->bb_u.l.bb_lsn = cpu_to_be64(bip->bli_item.li_lsn);
241 xfs_buf_update_cksum(bp, XFS_BTREE_LBLOCK_CRC_OFF);
245 xfs_btree_lblock_verify_crc(
248 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
249 struct xfs_mount *mp = bp->b_target->bt_mount;
251 if (xfs_sb_version_hascrc(&mp->m_sb)) {
252 if (!xfs_log_check_lsn(mp, be64_to_cpu(block->bb_u.l.bb_lsn)))
254 return xfs_buf_verify_cksum(bp, XFS_BTREE_LBLOCK_CRC_OFF);
261 * Calculate CRC on the whole btree block and stuff it into the
262 * short-form btree header.
264 * Prior to calculting the CRC, pull the LSN out of the buffer log item and put
265 * it into the buffer so recovery knows what the last modification was that made
269 xfs_btree_sblock_calc_crc(
272 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
273 struct xfs_buf_log_item *bip = bp->b_fspriv;
275 if (!xfs_sb_version_hascrc(&bp->b_target->bt_mount->m_sb))
278 block->bb_u.s.bb_lsn = cpu_to_be64(bip->bli_item.li_lsn);
279 xfs_buf_update_cksum(bp, XFS_BTREE_SBLOCK_CRC_OFF);
283 xfs_btree_sblock_verify_crc(
286 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
287 struct xfs_mount *mp = bp->b_target->bt_mount;
289 if (xfs_sb_version_hascrc(&mp->m_sb)) {
290 if (!xfs_log_check_lsn(mp, be64_to_cpu(block->bb_u.s.bb_lsn)))
292 return xfs_buf_verify_cksum(bp, XFS_BTREE_SBLOCK_CRC_OFF);
299 xfs_btree_free_block(
300 struct xfs_btree_cur *cur,
305 error = cur->bc_ops->free_block(cur, bp);
307 xfs_trans_binval(cur->bc_tp, bp);
308 XFS_BTREE_STATS_INC(cur, free);
314 * Delete the btree cursor.
317 xfs_btree_del_cursor(
318 xfs_btree_cur_t *cur, /* btree cursor */
319 int error) /* del because of error */
321 int i; /* btree level */
324 * Clear the buffer pointers, and release the buffers.
325 * If we're doing this in the face of an error, we
326 * need to make sure to inspect all of the entries
327 * in the bc_bufs array for buffers to be unlocked.
328 * This is because some of the btree code works from
329 * level n down to 0, and if we get an error along
330 * the way we won't have initialized all the entries
333 for (i = 0; i < cur->bc_nlevels; i++) {
335 xfs_trans_brelse(cur->bc_tp, cur->bc_bufs[i]);
340 * Can't free a bmap cursor without having dealt with the
341 * allocated indirect blocks' accounting.
343 ASSERT(cur->bc_btnum != XFS_BTNUM_BMAP ||
344 cur->bc_private.b.allocated == 0);
348 kmem_zone_free(xfs_btree_cur_zone, cur);
352 * Duplicate the btree cursor.
353 * Allocate a new one, copy the record, re-get the buffers.
356 xfs_btree_dup_cursor(
357 xfs_btree_cur_t *cur, /* input cursor */
358 xfs_btree_cur_t **ncur) /* output cursor */
360 xfs_buf_t *bp; /* btree block's buffer pointer */
361 int error; /* error return value */
362 int i; /* level number of btree block */
363 xfs_mount_t *mp; /* mount structure for filesystem */
364 xfs_btree_cur_t *new; /* new cursor value */
365 xfs_trans_t *tp; /* transaction pointer, can be NULL */
371 * Allocate a new cursor like the old one.
373 new = cur->bc_ops->dup_cursor(cur);
376 * Copy the record currently in the cursor.
378 new->bc_rec = cur->bc_rec;
381 * For each level current, re-get the buffer and copy the ptr value.
383 for (i = 0; i < new->bc_nlevels; i++) {
384 new->bc_ptrs[i] = cur->bc_ptrs[i];
385 new->bc_ra[i] = cur->bc_ra[i];
386 bp = cur->bc_bufs[i];
388 error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp,
389 XFS_BUF_ADDR(bp), mp->m_bsize,
391 cur->bc_ops->buf_ops);
393 xfs_btree_del_cursor(new, error);
398 new->bc_bufs[i] = bp;
405 * XFS btree block layout and addressing:
407 * There are two types of blocks in the btree: leaf and non-leaf blocks.
409 * The leaf record start with a header then followed by records containing
410 * the values. A non-leaf block also starts with the same header, and
411 * then first contains lookup keys followed by an equal number of pointers
412 * to the btree blocks at the previous level.
414 * +--------+-------+-------+-------+-------+-------+-------+
415 * Leaf: | header | rec 1 | rec 2 | rec 3 | rec 4 | rec 5 | rec N |
416 * +--------+-------+-------+-------+-------+-------+-------+
418 * +--------+-------+-------+-------+-------+-------+-------+
419 * Non-Leaf: | header | key 1 | key 2 | key N | ptr 1 | ptr 2 | ptr N |
420 * +--------+-------+-------+-------+-------+-------+-------+
422 * The header is called struct xfs_btree_block for reasons better left unknown
423 * and comes in different versions for short (32bit) and long (64bit) block
424 * pointers. The record and key structures are defined by the btree instances
425 * and opaque to the btree core. The block pointers are simple disk endian
426 * integers, available in a short (32bit) and long (64bit) variant.
428 * The helpers below calculate the offset of a given record, key or pointer
429 * into a btree block (xfs_btree_*_offset) or return a pointer to the given
430 * record, key or pointer (xfs_btree_*_addr). Note that all addressing
431 * inside the btree block is done using indices starting at one, not zero!
433 * If XFS_BTREE_OVERLAPPING is set, then this btree supports keys containing
434 * overlapping intervals. In such a tree, records are still sorted lowest to
435 * highest and indexed by the smallest key value that refers to the record.
436 * However, nodes are different: each pointer has two associated keys -- one
437 * indexing the lowest key available in the block(s) below (the same behavior
438 * as the key in a regular btree) and another indexing the highest key
439 * available in the block(s) below. Because records are /not/ sorted by the
440 * highest key, all leaf block updates require us to compute the highest key
441 * that matches any record in the leaf and to recursively update the high keys
442 * in the nodes going further up in the tree, if necessary. Nodes look like
445 * +--------+-----+-----+-----+-----+-----+-------+-------+-----+
446 * Non-Leaf: | header | lo1 | hi1 | lo2 | hi2 | ... | ptr 1 | ptr 2 | ... |
447 * +--------+-----+-----+-----+-----+-----+-------+-------+-----+
449 * To perform an interval query on an overlapped tree, perform the usual
450 * depth-first search and use the low and high keys to decide if we can skip
451 * that particular node. If a leaf node is reached, return the records that
452 * intersect the interval. Note that an interval query may return numerous
453 * entries. For a non-overlapped tree, simply search for the record associated
454 * with the lowest key and iterate forward until a non-matching record is
455 * found. Section 14.3 ("Interval Trees") of _Introduction to Algorithms_ by
456 * Cormen, Leiserson, Rivest, and Stein (2nd or 3rd ed. only) discuss this in
459 * Why do we care about overlapping intervals? Let's say you have a bunch of
460 * reverse mapping records on a reflink filesystem:
462 * 1: +- file A startblock B offset C length D -----------+
463 * 2: +- file E startblock F offset G length H --------------+
464 * 3: +- file I startblock F offset J length K --+
465 * 4: +- file L... --+
467 * Now say we want to map block (B+D) into file A at offset (C+D). Ideally,
468 * we'd simply increment the length of record 1. But how do we find the record
469 * that ends at (B+D-1) (i.e. record 1)? A LE lookup of (B+D-1) would return
470 * record 3 because the keys are ordered first by startblock. An interval
471 * query would return records 1 and 2 because they both overlap (B+D-1), and
472 * from that we can pick out record 1 as the appropriate left neighbor.
474 * In the non-overlapped case you can do a LE lookup and decrement the cursor
475 * because a record's interval must end before the next record.
479 * Return size of the btree block header for this btree instance.
481 static inline size_t xfs_btree_block_len(struct xfs_btree_cur *cur)
483 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
484 if (cur->bc_flags & XFS_BTREE_CRC_BLOCKS)
485 return XFS_BTREE_LBLOCK_CRC_LEN;
486 return XFS_BTREE_LBLOCK_LEN;
488 if (cur->bc_flags & XFS_BTREE_CRC_BLOCKS)
489 return XFS_BTREE_SBLOCK_CRC_LEN;
490 return XFS_BTREE_SBLOCK_LEN;
494 * Return size of btree block pointers for this btree instance.
496 static inline size_t xfs_btree_ptr_len(struct xfs_btree_cur *cur)
498 return (cur->bc_flags & XFS_BTREE_LONG_PTRS) ?
499 sizeof(__be64) : sizeof(__be32);
503 * Calculate offset of the n-th record in a btree block.
506 xfs_btree_rec_offset(
507 struct xfs_btree_cur *cur,
510 return xfs_btree_block_len(cur) +
511 (n - 1) * cur->bc_ops->rec_len;
515 * Calculate offset of the n-th key in a btree block.
518 xfs_btree_key_offset(
519 struct xfs_btree_cur *cur,
522 return xfs_btree_block_len(cur) +
523 (n - 1) * cur->bc_ops->key_len;
527 * Calculate offset of the n-th high key in a btree block.
530 xfs_btree_high_key_offset(
531 struct xfs_btree_cur *cur,
534 return xfs_btree_block_len(cur) +
535 (n - 1) * cur->bc_ops->key_len + (cur->bc_ops->key_len / 2);
539 * Calculate offset of the n-th block pointer in a btree block.
542 xfs_btree_ptr_offset(
543 struct xfs_btree_cur *cur,
547 return xfs_btree_block_len(cur) +
548 cur->bc_ops->get_maxrecs(cur, level) * cur->bc_ops->key_len +
549 (n - 1) * xfs_btree_ptr_len(cur);
553 * Return a pointer to the n-th record in the btree block.
555 STATIC union xfs_btree_rec *
557 struct xfs_btree_cur *cur,
559 struct xfs_btree_block *block)
561 return (union xfs_btree_rec *)
562 ((char *)block + xfs_btree_rec_offset(cur, n));
566 * Return a pointer to the n-th key in the btree block.
568 STATIC union xfs_btree_key *
570 struct xfs_btree_cur *cur,
572 struct xfs_btree_block *block)
574 return (union xfs_btree_key *)
575 ((char *)block + xfs_btree_key_offset(cur, n));
579 * Return a pointer to the n-th high key in the btree block.
581 STATIC union xfs_btree_key *
582 xfs_btree_high_key_addr(
583 struct xfs_btree_cur *cur,
585 struct xfs_btree_block *block)
587 return (union xfs_btree_key *)
588 ((char *)block + xfs_btree_high_key_offset(cur, n));
592 * Return a pointer to the n-th block pointer in the btree block.
594 STATIC union xfs_btree_ptr *
596 struct xfs_btree_cur *cur,
598 struct xfs_btree_block *block)
600 int level = xfs_btree_get_level(block);
602 ASSERT(block->bb_level != 0);
604 return (union xfs_btree_ptr *)
605 ((char *)block + xfs_btree_ptr_offset(cur, n, level));
609 * Get the root block which is stored in the inode.
611 * For now this btree implementation assumes the btree root is always
612 * stored in the if_broot field of an inode fork.
614 STATIC struct xfs_btree_block *
616 struct xfs_btree_cur *cur)
618 struct xfs_ifork *ifp;
620 ifp = XFS_IFORK_PTR(cur->bc_private.b.ip, cur->bc_private.b.whichfork);
621 return (struct xfs_btree_block *)ifp->if_broot;
625 * Retrieve the block pointer from the cursor at the given level.
626 * This may be an inode btree root or from a buffer.
628 STATIC struct xfs_btree_block * /* generic btree block pointer */
630 struct xfs_btree_cur *cur, /* btree cursor */
631 int level, /* level in btree */
632 struct xfs_buf **bpp) /* buffer containing the block */
634 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
635 (level == cur->bc_nlevels - 1)) {
637 return xfs_btree_get_iroot(cur);
640 *bpp = cur->bc_bufs[level];
641 return XFS_BUF_TO_BLOCK(*bpp);
645 * Get a buffer for the block, return it with no data read.
646 * Long-form addressing.
648 xfs_buf_t * /* buffer for fsbno */
650 xfs_mount_t *mp, /* file system mount point */
651 xfs_trans_t *tp, /* transaction pointer */
652 xfs_fsblock_t fsbno, /* file system block number */
653 uint lock) /* lock flags for get_buf */
655 xfs_daddr_t d; /* real disk block address */
657 ASSERT(fsbno != NULLFSBLOCK);
658 d = XFS_FSB_TO_DADDR(mp, fsbno);
659 return xfs_trans_get_buf(tp, mp->m_ddev_targp, d, mp->m_bsize, lock);
663 * Get a buffer for the block, return it with no data read.
664 * Short-form addressing.
666 xfs_buf_t * /* buffer for agno/agbno */
668 xfs_mount_t *mp, /* file system mount point */
669 xfs_trans_t *tp, /* transaction pointer */
670 xfs_agnumber_t agno, /* allocation group number */
671 xfs_agblock_t agbno, /* allocation group block number */
672 uint lock) /* lock flags for get_buf */
674 xfs_daddr_t d; /* real disk block address */
676 ASSERT(agno != NULLAGNUMBER);
677 ASSERT(agbno != NULLAGBLOCK);
678 d = XFS_AGB_TO_DADDR(mp, agno, agbno);
679 return xfs_trans_get_buf(tp, mp->m_ddev_targp, d, mp->m_bsize, lock);
683 * Check for the cursor referring to the last block at the given level.
685 int /* 1=is last block, 0=not last block */
686 xfs_btree_islastblock(
687 xfs_btree_cur_t *cur, /* btree cursor */
688 int level) /* level to check */
690 struct xfs_btree_block *block; /* generic btree block pointer */
691 xfs_buf_t *bp; /* buffer containing block */
693 block = xfs_btree_get_block(cur, level, &bp);
694 xfs_btree_check_block(cur, block, level, bp);
695 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
696 return block->bb_u.l.bb_rightsib == cpu_to_be64(NULLFSBLOCK);
698 return block->bb_u.s.bb_rightsib == cpu_to_be32(NULLAGBLOCK);
702 * Change the cursor to point to the first record at the given level.
703 * Other levels are unaffected.
705 STATIC int /* success=1, failure=0 */
707 xfs_btree_cur_t *cur, /* btree cursor */
708 int level) /* level to change */
710 struct xfs_btree_block *block; /* generic btree block pointer */
711 xfs_buf_t *bp; /* buffer containing block */
714 * Get the block pointer for this level.
716 block = xfs_btree_get_block(cur, level, &bp);
717 xfs_btree_check_block(cur, block, level, bp);
719 * It's empty, there is no such record.
721 if (!block->bb_numrecs)
724 * Set the ptr value to 1, that's the first record/key.
726 cur->bc_ptrs[level] = 1;
731 * Change the cursor to point to the last record in the current block
732 * at the given level. Other levels are unaffected.
734 STATIC int /* success=1, failure=0 */
736 xfs_btree_cur_t *cur, /* btree cursor */
737 int level) /* level to change */
739 struct xfs_btree_block *block; /* generic btree block pointer */
740 xfs_buf_t *bp; /* buffer containing block */
743 * Get the block pointer for this level.
745 block = xfs_btree_get_block(cur, level, &bp);
746 xfs_btree_check_block(cur, block, level, bp);
748 * It's empty, there is no such record.
750 if (!block->bb_numrecs)
753 * Set the ptr value to numrecs, that's the last record/key.
755 cur->bc_ptrs[level] = be16_to_cpu(block->bb_numrecs);
760 * Compute first and last byte offsets for the fields given.
761 * Interprets the offsets table, which contains struct field offsets.
765 __int64_t fields, /* bitmask of fields */
766 const short *offsets, /* table of field offsets */
767 int nbits, /* number of bits to inspect */
768 int *first, /* output: first byte offset */
769 int *last) /* output: last byte offset */
771 int i; /* current bit number */
772 __int64_t imask; /* mask for current bit number */
776 * Find the lowest bit, so the first byte offset.
778 for (i = 0, imask = 1LL; ; i++, imask <<= 1) {
779 if (imask & fields) {
785 * Find the highest bit, so the last byte offset.
787 for (i = nbits - 1, imask = 1LL << i; ; i--, imask >>= 1) {
788 if (imask & fields) {
789 *last = offsets[i + 1] - 1;
796 * Get a buffer for the block, return it read in.
797 * Long-form addressing.
801 struct xfs_mount *mp, /* file system mount point */
802 struct xfs_trans *tp, /* transaction pointer */
803 xfs_fsblock_t fsbno, /* file system block number */
804 uint lock, /* lock flags for read_buf */
805 struct xfs_buf **bpp, /* buffer for fsbno */
806 int refval, /* ref count value for buffer */
807 const struct xfs_buf_ops *ops)
809 struct xfs_buf *bp; /* return value */
810 xfs_daddr_t d; /* real disk block address */
813 ASSERT(fsbno != NULLFSBLOCK);
814 d = XFS_FSB_TO_DADDR(mp, fsbno);
815 error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, d,
816 mp->m_bsize, lock, &bp, ops);
820 xfs_buf_set_ref(bp, refval);
826 * Read-ahead the block, don't wait for it, don't return a buffer.
827 * Long-form addressing.
831 xfs_btree_reada_bufl(
832 struct xfs_mount *mp, /* file system mount point */
833 xfs_fsblock_t fsbno, /* file system block number */
834 xfs_extlen_t count, /* count of filesystem blocks */
835 const struct xfs_buf_ops *ops)
839 ASSERT(fsbno != NULLFSBLOCK);
840 d = XFS_FSB_TO_DADDR(mp, fsbno);
841 xfs_buf_readahead(mp->m_ddev_targp, d, mp->m_bsize * count, ops);
845 * Read-ahead the block, don't wait for it, don't return a buffer.
846 * Short-form addressing.
850 xfs_btree_reada_bufs(
851 struct xfs_mount *mp, /* file system mount point */
852 xfs_agnumber_t agno, /* allocation group number */
853 xfs_agblock_t agbno, /* allocation group block number */
854 xfs_extlen_t count, /* count of filesystem blocks */
855 const struct xfs_buf_ops *ops)
859 ASSERT(agno != NULLAGNUMBER);
860 ASSERT(agbno != NULLAGBLOCK);
861 d = XFS_AGB_TO_DADDR(mp, agno, agbno);
862 xfs_buf_readahead(mp->m_ddev_targp, d, mp->m_bsize * count, ops);
866 xfs_btree_readahead_lblock(
867 struct xfs_btree_cur *cur,
869 struct xfs_btree_block *block)
872 xfs_fsblock_t left = be64_to_cpu(block->bb_u.l.bb_leftsib);
873 xfs_fsblock_t right = be64_to_cpu(block->bb_u.l.bb_rightsib);
875 if ((lr & XFS_BTCUR_LEFTRA) && left != NULLFSBLOCK) {
876 xfs_btree_reada_bufl(cur->bc_mp, left, 1,
877 cur->bc_ops->buf_ops);
881 if ((lr & XFS_BTCUR_RIGHTRA) && right != NULLFSBLOCK) {
882 xfs_btree_reada_bufl(cur->bc_mp, right, 1,
883 cur->bc_ops->buf_ops);
891 xfs_btree_readahead_sblock(
892 struct xfs_btree_cur *cur,
894 struct xfs_btree_block *block)
897 xfs_agblock_t left = be32_to_cpu(block->bb_u.s.bb_leftsib);
898 xfs_agblock_t right = be32_to_cpu(block->bb_u.s.bb_rightsib);
901 if ((lr & XFS_BTCUR_LEFTRA) && left != NULLAGBLOCK) {
902 xfs_btree_reada_bufs(cur->bc_mp, cur->bc_private.a.agno,
903 left, 1, cur->bc_ops->buf_ops);
907 if ((lr & XFS_BTCUR_RIGHTRA) && right != NULLAGBLOCK) {
908 xfs_btree_reada_bufs(cur->bc_mp, cur->bc_private.a.agno,
909 right, 1, cur->bc_ops->buf_ops);
917 * Read-ahead btree blocks, at the given level.
918 * Bits in lr are set from XFS_BTCUR_{LEFT,RIGHT}RA.
922 struct xfs_btree_cur *cur, /* btree cursor */
923 int lev, /* level in btree */
924 int lr) /* left/right bits */
926 struct xfs_btree_block *block;
929 * No readahead needed if we are at the root level and the
930 * btree root is stored in the inode.
932 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
933 (lev == cur->bc_nlevels - 1))
936 if ((cur->bc_ra[lev] | lr) == cur->bc_ra[lev])
939 cur->bc_ra[lev] |= lr;
940 block = XFS_BUF_TO_BLOCK(cur->bc_bufs[lev]);
942 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
943 return xfs_btree_readahead_lblock(cur, lr, block);
944 return xfs_btree_readahead_sblock(cur, lr, block);
948 xfs_btree_ptr_to_daddr(
949 struct xfs_btree_cur *cur,
950 union xfs_btree_ptr *ptr)
952 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
953 ASSERT(ptr->l != cpu_to_be64(NULLFSBLOCK));
955 return XFS_FSB_TO_DADDR(cur->bc_mp, be64_to_cpu(ptr->l));
957 ASSERT(cur->bc_private.a.agno != NULLAGNUMBER);
958 ASSERT(ptr->s != cpu_to_be32(NULLAGBLOCK));
960 return XFS_AGB_TO_DADDR(cur->bc_mp, cur->bc_private.a.agno,
961 be32_to_cpu(ptr->s));
966 * Readahead @count btree blocks at the given @ptr location.
968 * We don't need to care about long or short form btrees here as we have a
969 * method of converting the ptr directly to a daddr available to us.
972 xfs_btree_readahead_ptr(
973 struct xfs_btree_cur *cur,
974 union xfs_btree_ptr *ptr,
977 xfs_buf_readahead(cur->bc_mp->m_ddev_targp,
978 xfs_btree_ptr_to_daddr(cur, ptr),
979 cur->bc_mp->m_bsize * count, cur->bc_ops->buf_ops);
983 * Set the buffer for level "lev" in the cursor to bp, releasing
984 * any previous buffer.
988 xfs_btree_cur_t *cur, /* btree cursor */
989 int lev, /* level in btree */
990 xfs_buf_t *bp) /* new buffer to set */
992 struct xfs_btree_block *b; /* btree block */
994 if (cur->bc_bufs[lev])
995 xfs_trans_brelse(cur->bc_tp, cur->bc_bufs[lev]);
996 cur->bc_bufs[lev] = bp;
999 b = XFS_BUF_TO_BLOCK(bp);
1000 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
1001 if (b->bb_u.l.bb_leftsib == cpu_to_be64(NULLFSBLOCK))
1002 cur->bc_ra[lev] |= XFS_BTCUR_LEFTRA;
1003 if (b->bb_u.l.bb_rightsib == cpu_to_be64(NULLFSBLOCK))
1004 cur->bc_ra[lev] |= XFS_BTCUR_RIGHTRA;
1006 if (b->bb_u.s.bb_leftsib == cpu_to_be32(NULLAGBLOCK))
1007 cur->bc_ra[lev] |= XFS_BTCUR_LEFTRA;
1008 if (b->bb_u.s.bb_rightsib == cpu_to_be32(NULLAGBLOCK))
1009 cur->bc_ra[lev] |= XFS_BTCUR_RIGHTRA;
1014 xfs_btree_ptr_is_null(
1015 struct xfs_btree_cur *cur,
1016 union xfs_btree_ptr *ptr)
1018 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
1019 return ptr->l == cpu_to_be64(NULLFSBLOCK);
1021 return ptr->s == cpu_to_be32(NULLAGBLOCK);
1025 xfs_btree_set_ptr_null(
1026 struct xfs_btree_cur *cur,
1027 union xfs_btree_ptr *ptr)
1029 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
1030 ptr->l = cpu_to_be64(NULLFSBLOCK);
1032 ptr->s = cpu_to_be32(NULLAGBLOCK);
1036 * Get/set/init sibling pointers
1039 xfs_btree_get_sibling(
1040 struct xfs_btree_cur *cur,
1041 struct xfs_btree_block *block,
1042 union xfs_btree_ptr *ptr,
1045 ASSERT(lr == XFS_BB_LEFTSIB || lr == XFS_BB_RIGHTSIB);
1047 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
1048 if (lr == XFS_BB_RIGHTSIB)
1049 ptr->l = block->bb_u.l.bb_rightsib;
1051 ptr->l = block->bb_u.l.bb_leftsib;
1053 if (lr == XFS_BB_RIGHTSIB)
1054 ptr->s = block->bb_u.s.bb_rightsib;
1056 ptr->s = block->bb_u.s.bb_leftsib;
1061 xfs_btree_set_sibling(
1062 struct xfs_btree_cur *cur,
1063 struct xfs_btree_block *block,
1064 union xfs_btree_ptr *ptr,
1067 ASSERT(lr == XFS_BB_LEFTSIB || lr == XFS_BB_RIGHTSIB);
1069 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
1070 if (lr == XFS_BB_RIGHTSIB)
1071 block->bb_u.l.bb_rightsib = ptr->l;
1073 block->bb_u.l.bb_leftsib = ptr->l;
1075 if (lr == XFS_BB_RIGHTSIB)
1076 block->bb_u.s.bb_rightsib = ptr->s;
1078 block->bb_u.s.bb_leftsib = ptr->s;
1083 xfs_btree_init_block_int(
1084 struct xfs_mount *mp,
1085 struct xfs_btree_block *buf,
1093 buf->bb_magic = cpu_to_be32(magic);
1094 buf->bb_level = cpu_to_be16(level);
1095 buf->bb_numrecs = cpu_to_be16(numrecs);
1097 if (flags & XFS_BTREE_LONG_PTRS) {
1098 buf->bb_u.l.bb_leftsib = cpu_to_be64(NULLFSBLOCK);
1099 buf->bb_u.l.bb_rightsib = cpu_to_be64(NULLFSBLOCK);
1100 if (flags & XFS_BTREE_CRC_BLOCKS) {
1101 buf->bb_u.l.bb_blkno = cpu_to_be64(blkno);
1102 buf->bb_u.l.bb_owner = cpu_to_be64(owner);
1103 uuid_copy(&buf->bb_u.l.bb_uuid, &mp->m_sb.sb_meta_uuid);
1104 buf->bb_u.l.bb_pad = 0;
1105 buf->bb_u.l.bb_lsn = 0;
1108 /* owner is a 32 bit value on short blocks */
1109 __u32 __owner = (__u32)owner;
1111 buf->bb_u.s.bb_leftsib = cpu_to_be32(NULLAGBLOCK);
1112 buf->bb_u.s.bb_rightsib = cpu_to_be32(NULLAGBLOCK);
1113 if (flags & XFS_BTREE_CRC_BLOCKS) {
1114 buf->bb_u.s.bb_blkno = cpu_to_be64(blkno);
1115 buf->bb_u.s.bb_owner = cpu_to_be32(__owner);
1116 uuid_copy(&buf->bb_u.s.bb_uuid, &mp->m_sb.sb_meta_uuid);
1117 buf->bb_u.s.bb_lsn = 0;
1123 xfs_btree_init_block(
1124 struct xfs_mount *mp,
1132 xfs_btree_init_block_int(mp, XFS_BUF_TO_BLOCK(bp), bp->b_bn,
1133 magic, level, numrecs, owner, flags);
1137 xfs_btree_init_block_cur(
1138 struct xfs_btree_cur *cur,
1146 * we can pull the owner from the cursor right now as the different
1147 * owners align directly with the pointer size of the btree. This may
1148 * change in future, but is safe for current users of the generic btree
1151 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
1152 owner = cur->bc_private.b.ip->i_ino;
1154 owner = cur->bc_private.a.agno;
1156 xfs_btree_init_block_int(cur->bc_mp, XFS_BUF_TO_BLOCK(bp), bp->b_bn,
1157 xfs_btree_magic(cur), level, numrecs,
1158 owner, cur->bc_flags);
1162 * Return true if ptr is the last record in the btree and
1163 * we need to track updates to this record. The decision
1164 * will be further refined in the update_lastrec method.
1167 xfs_btree_is_lastrec(
1168 struct xfs_btree_cur *cur,
1169 struct xfs_btree_block *block,
1172 union xfs_btree_ptr ptr;
1176 if (!(cur->bc_flags & XFS_BTREE_LASTREC_UPDATE))
1179 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_RIGHTSIB);
1180 if (!xfs_btree_ptr_is_null(cur, &ptr))
1186 xfs_btree_buf_to_ptr(
1187 struct xfs_btree_cur *cur,
1189 union xfs_btree_ptr *ptr)
1191 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
1192 ptr->l = cpu_to_be64(XFS_DADDR_TO_FSB(cur->bc_mp,
1195 ptr->s = cpu_to_be32(xfs_daddr_to_agbno(cur->bc_mp,
1202 struct xfs_btree_cur *cur,
1205 switch (cur->bc_btnum) {
1208 xfs_buf_set_ref(bp, XFS_ALLOC_BTREE_REF);
1211 case XFS_BTNUM_FINO:
1212 xfs_buf_set_ref(bp, XFS_INO_BTREE_REF);
1214 case XFS_BTNUM_BMAP:
1215 xfs_buf_set_ref(bp, XFS_BMAP_BTREE_REF);
1217 case XFS_BTNUM_RMAP:
1218 xfs_buf_set_ref(bp, XFS_RMAP_BTREE_REF);
1220 case XFS_BTNUM_REFC:
1221 xfs_buf_set_ref(bp, XFS_REFC_BTREE_REF);
1229 xfs_btree_get_buf_block(
1230 struct xfs_btree_cur *cur,
1231 union xfs_btree_ptr *ptr,
1233 struct xfs_btree_block **block,
1234 struct xfs_buf **bpp)
1236 struct xfs_mount *mp = cur->bc_mp;
1239 /* need to sort out how callers deal with failures first */
1240 ASSERT(!(flags & XBF_TRYLOCK));
1242 d = xfs_btree_ptr_to_daddr(cur, ptr);
1243 *bpp = xfs_trans_get_buf(cur->bc_tp, mp->m_ddev_targp, d,
1244 mp->m_bsize, flags);
1249 (*bpp)->b_ops = cur->bc_ops->buf_ops;
1250 *block = XFS_BUF_TO_BLOCK(*bpp);
1255 * Read in the buffer at the given ptr and return the buffer and
1256 * the block pointer within the buffer.
1259 xfs_btree_read_buf_block(
1260 struct xfs_btree_cur *cur,
1261 union xfs_btree_ptr *ptr,
1263 struct xfs_btree_block **block,
1264 struct xfs_buf **bpp)
1266 struct xfs_mount *mp = cur->bc_mp;
1270 /* need to sort out how callers deal with failures first */
1271 ASSERT(!(flags & XBF_TRYLOCK));
1273 d = xfs_btree_ptr_to_daddr(cur, ptr);
1274 error = xfs_trans_read_buf(mp, cur->bc_tp, mp->m_ddev_targp, d,
1275 mp->m_bsize, flags, bpp,
1276 cur->bc_ops->buf_ops);
1280 xfs_btree_set_refs(cur, *bpp);
1281 *block = XFS_BUF_TO_BLOCK(*bpp);
1286 * Copy keys from one btree block to another.
1289 xfs_btree_copy_keys(
1290 struct xfs_btree_cur *cur,
1291 union xfs_btree_key *dst_key,
1292 union xfs_btree_key *src_key,
1295 ASSERT(numkeys >= 0);
1296 memcpy(dst_key, src_key, numkeys * cur->bc_ops->key_len);
1300 * Copy records from one btree block to another.
1303 xfs_btree_copy_recs(
1304 struct xfs_btree_cur *cur,
1305 union xfs_btree_rec *dst_rec,
1306 union xfs_btree_rec *src_rec,
1309 ASSERT(numrecs >= 0);
1310 memcpy(dst_rec, src_rec, numrecs * cur->bc_ops->rec_len);
1314 * Copy block pointers from one btree block to another.
1317 xfs_btree_copy_ptrs(
1318 struct xfs_btree_cur *cur,
1319 union xfs_btree_ptr *dst_ptr,
1320 union xfs_btree_ptr *src_ptr,
1323 ASSERT(numptrs >= 0);
1324 memcpy(dst_ptr, src_ptr, numptrs * xfs_btree_ptr_len(cur));
1328 * Shift keys one index left/right inside a single btree block.
1331 xfs_btree_shift_keys(
1332 struct xfs_btree_cur *cur,
1333 union xfs_btree_key *key,
1339 ASSERT(numkeys >= 0);
1340 ASSERT(dir == 1 || dir == -1);
1342 dst_key = (char *)key + (dir * cur->bc_ops->key_len);
1343 memmove(dst_key, key, numkeys * cur->bc_ops->key_len);
1347 * Shift records one index left/right inside a single btree block.
1350 xfs_btree_shift_recs(
1351 struct xfs_btree_cur *cur,
1352 union xfs_btree_rec *rec,
1358 ASSERT(numrecs >= 0);
1359 ASSERT(dir == 1 || dir == -1);
1361 dst_rec = (char *)rec + (dir * cur->bc_ops->rec_len);
1362 memmove(dst_rec, rec, numrecs * cur->bc_ops->rec_len);
1366 * Shift block pointers one index left/right inside a single btree block.
1369 xfs_btree_shift_ptrs(
1370 struct xfs_btree_cur *cur,
1371 union xfs_btree_ptr *ptr,
1377 ASSERT(numptrs >= 0);
1378 ASSERT(dir == 1 || dir == -1);
1380 dst_ptr = (char *)ptr + (dir * xfs_btree_ptr_len(cur));
1381 memmove(dst_ptr, ptr, numptrs * xfs_btree_ptr_len(cur));
1385 * Log key values from the btree block.
1389 struct xfs_btree_cur *cur,
1394 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1395 XFS_BTREE_TRACE_ARGBII(cur, bp, first, last);
1398 xfs_trans_buf_set_type(cur->bc_tp, bp, XFS_BLFT_BTREE_BUF);
1399 xfs_trans_log_buf(cur->bc_tp, bp,
1400 xfs_btree_key_offset(cur, first),
1401 xfs_btree_key_offset(cur, last + 1) - 1);
1403 xfs_trans_log_inode(cur->bc_tp, cur->bc_private.b.ip,
1404 xfs_ilog_fbroot(cur->bc_private.b.whichfork));
1407 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1411 * Log record values from the btree block.
1415 struct xfs_btree_cur *cur,
1420 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1421 XFS_BTREE_TRACE_ARGBII(cur, bp, first, last);
1423 xfs_trans_buf_set_type(cur->bc_tp, bp, XFS_BLFT_BTREE_BUF);
1424 xfs_trans_log_buf(cur->bc_tp, bp,
1425 xfs_btree_rec_offset(cur, first),
1426 xfs_btree_rec_offset(cur, last + 1) - 1);
1428 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1432 * Log block pointer fields from a btree block (nonleaf).
1436 struct xfs_btree_cur *cur, /* btree cursor */
1437 struct xfs_buf *bp, /* buffer containing btree block */
1438 int first, /* index of first pointer to log */
1439 int last) /* index of last pointer to log */
1441 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1442 XFS_BTREE_TRACE_ARGBII(cur, bp, first, last);
1445 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
1446 int level = xfs_btree_get_level(block);
1448 xfs_trans_buf_set_type(cur->bc_tp, bp, XFS_BLFT_BTREE_BUF);
1449 xfs_trans_log_buf(cur->bc_tp, bp,
1450 xfs_btree_ptr_offset(cur, first, level),
1451 xfs_btree_ptr_offset(cur, last + 1, level) - 1);
1453 xfs_trans_log_inode(cur->bc_tp, cur->bc_private.b.ip,
1454 xfs_ilog_fbroot(cur->bc_private.b.whichfork));
1457 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1461 * Log fields from a btree block header.
1464 xfs_btree_log_block(
1465 struct xfs_btree_cur *cur, /* btree cursor */
1466 struct xfs_buf *bp, /* buffer containing btree block */
1467 int fields) /* mask of fields: XFS_BB_... */
1469 int first; /* first byte offset logged */
1470 int last; /* last byte offset logged */
1471 static const short soffsets[] = { /* table of offsets (short) */
1472 offsetof(struct xfs_btree_block, bb_magic),
1473 offsetof(struct xfs_btree_block, bb_level),
1474 offsetof(struct xfs_btree_block, bb_numrecs),
1475 offsetof(struct xfs_btree_block, bb_u.s.bb_leftsib),
1476 offsetof(struct xfs_btree_block, bb_u.s.bb_rightsib),
1477 offsetof(struct xfs_btree_block, bb_u.s.bb_blkno),
1478 offsetof(struct xfs_btree_block, bb_u.s.bb_lsn),
1479 offsetof(struct xfs_btree_block, bb_u.s.bb_uuid),
1480 offsetof(struct xfs_btree_block, bb_u.s.bb_owner),
1481 offsetof(struct xfs_btree_block, bb_u.s.bb_crc),
1482 XFS_BTREE_SBLOCK_CRC_LEN
1484 static const short loffsets[] = { /* table of offsets (long) */
1485 offsetof(struct xfs_btree_block, bb_magic),
1486 offsetof(struct xfs_btree_block, bb_level),
1487 offsetof(struct xfs_btree_block, bb_numrecs),
1488 offsetof(struct xfs_btree_block, bb_u.l.bb_leftsib),
1489 offsetof(struct xfs_btree_block, bb_u.l.bb_rightsib),
1490 offsetof(struct xfs_btree_block, bb_u.l.bb_blkno),
1491 offsetof(struct xfs_btree_block, bb_u.l.bb_lsn),
1492 offsetof(struct xfs_btree_block, bb_u.l.bb_uuid),
1493 offsetof(struct xfs_btree_block, bb_u.l.bb_owner),
1494 offsetof(struct xfs_btree_block, bb_u.l.bb_crc),
1495 offsetof(struct xfs_btree_block, bb_u.l.bb_pad),
1496 XFS_BTREE_LBLOCK_CRC_LEN
1499 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1500 XFS_BTREE_TRACE_ARGBI(cur, bp, fields);
1505 if (cur->bc_flags & XFS_BTREE_CRC_BLOCKS) {
1507 * We don't log the CRC when updating a btree
1508 * block but instead recreate it during log
1509 * recovery. As the log buffers have checksums
1510 * of their own this is safe and avoids logging a crc
1511 * update in a lot of places.
1513 if (fields == XFS_BB_ALL_BITS)
1514 fields = XFS_BB_ALL_BITS_CRC;
1515 nbits = XFS_BB_NUM_BITS_CRC;
1517 nbits = XFS_BB_NUM_BITS;
1519 xfs_btree_offsets(fields,
1520 (cur->bc_flags & XFS_BTREE_LONG_PTRS) ?
1521 loffsets : soffsets,
1522 nbits, &first, &last);
1523 xfs_trans_buf_set_type(cur->bc_tp, bp, XFS_BLFT_BTREE_BUF);
1524 xfs_trans_log_buf(cur->bc_tp, bp, first, last);
1526 xfs_trans_log_inode(cur->bc_tp, cur->bc_private.b.ip,
1527 xfs_ilog_fbroot(cur->bc_private.b.whichfork));
1530 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1534 * Increment cursor by one record at the level.
1535 * For nonzero levels the leaf-ward information is untouched.
1538 xfs_btree_increment(
1539 struct xfs_btree_cur *cur,
1541 int *stat) /* success/failure */
1543 struct xfs_btree_block *block;
1544 union xfs_btree_ptr ptr;
1546 int error; /* error return value */
1549 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1550 XFS_BTREE_TRACE_ARGI(cur, level);
1552 ASSERT(level < cur->bc_nlevels);
1554 /* Read-ahead to the right at this level. */
1555 xfs_btree_readahead(cur, level, XFS_BTCUR_RIGHTRA);
1557 /* Get a pointer to the btree block. */
1558 block = xfs_btree_get_block(cur, level, &bp);
1561 error = xfs_btree_check_block(cur, block, level, bp);
1566 /* We're done if we remain in the block after the increment. */
1567 if (++cur->bc_ptrs[level] <= xfs_btree_get_numrecs(block))
1570 /* Fail if we just went off the right edge of the tree. */
1571 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_RIGHTSIB);
1572 if (xfs_btree_ptr_is_null(cur, &ptr))
1575 XFS_BTREE_STATS_INC(cur, increment);
1578 * March up the tree incrementing pointers.
1579 * Stop when we don't go off the right edge of a block.
1581 for (lev = level + 1; lev < cur->bc_nlevels; lev++) {
1582 block = xfs_btree_get_block(cur, lev, &bp);
1585 error = xfs_btree_check_block(cur, block, lev, bp);
1590 if (++cur->bc_ptrs[lev] <= xfs_btree_get_numrecs(block))
1593 /* Read-ahead the right block for the next loop. */
1594 xfs_btree_readahead(cur, lev, XFS_BTCUR_RIGHTRA);
1598 * If we went off the root then we are either seriously
1599 * confused or have the tree root in an inode.
1601 if (lev == cur->bc_nlevels) {
1602 if (cur->bc_flags & XFS_BTREE_ROOT_IN_INODE)
1605 error = -EFSCORRUPTED;
1608 ASSERT(lev < cur->bc_nlevels);
1611 * Now walk back down the tree, fixing up the cursor's buffer
1612 * pointers and key numbers.
1614 for (block = xfs_btree_get_block(cur, lev, &bp); lev > level; ) {
1615 union xfs_btree_ptr *ptrp;
1617 ptrp = xfs_btree_ptr_addr(cur, cur->bc_ptrs[lev], block);
1619 error = xfs_btree_read_buf_block(cur, ptrp, 0, &block, &bp);
1623 xfs_btree_setbuf(cur, lev, bp);
1624 cur->bc_ptrs[lev] = 1;
1627 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1632 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1637 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
1642 * Decrement cursor by one record at the level.
1643 * For nonzero levels the leaf-ward information is untouched.
1646 xfs_btree_decrement(
1647 struct xfs_btree_cur *cur,
1649 int *stat) /* success/failure */
1651 struct xfs_btree_block *block;
1653 int error; /* error return value */
1655 union xfs_btree_ptr ptr;
1657 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1658 XFS_BTREE_TRACE_ARGI(cur, level);
1660 ASSERT(level < cur->bc_nlevels);
1662 /* Read-ahead to the left at this level. */
1663 xfs_btree_readahead(cur, level, XFS_BTCUR_LEFTRA);
1665 /* We're done if we remain in the block after the decrement. */
1666 if (--cur->bc_ptrs[level] > 0)
1669 /* Get a pointer to the btree block. */
1670 block = xfs_btree_get_block(cur, level, &bp);
1673 error = xfs_btree_check_block(cur, block, level, bp);
1678 /* Fail if we just went off the left edge of the tree. */
1679 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_LEFTSIB);
1680 if (xfs_btree_ptr_is_null(cur, &ptr))
1683 XFS_BTREE_STATS_INC(cur, decrement);
1686 * March up the tree decrementing pointers.
1687 * Stop when we don't go off the left edge of a block.
1689 for (lev = level + 1; lev < cur->bc_nlevels; lev++) {
1690 if (--cur->bc_ptrs[lev] > 0)
1692 /* Read-ahead the left block for the next loop. */
1693 xfs_btree_readahead(cur, lev, XFS_BTCUR_LEFTRA);
1697 * If we went off the root then we are seriously confused.
1698 * or the root of the tree is in an inode.
1700 if (lev == cur->bc_nlevels) {
1701 if (cur->bc_flags & XFS_BTREE_ROOT_IN_INODE)
1704 error = -EFSCORRUPTED;
1707 ASSERT(lev < cur->bc_nlevels);
1710 * Now walk back down the tree, fixing up the cursor's buffer
1711 * pointers and key numbers.
1713 for (block = xfs_btree_get_block(cur, lev, &bp); lev > level; ) {
1714 union xfs_btree_ptr *ptrp;
1716 ptrp = xfs_btree_ptr_addr(cur, cur->bc_ptrs[lev], block);
1718 error = xfs_btree_read_buf_block(cur, ptrp, 0, &block, &bp);
1721 xfs_btree_setbuf(cur, lev, bp);
1722 cur->bc_ptrs[lev] = xfs_btree_get_numrecs(block);
1725 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1730 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1735 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
1740 xfs_btree_lookup_get_block(
1741 struct xfs_btree_cur *cur, /* btree cursor */
1742 int level, /* level in the btree */
1743 union xfs_btree_ptr *pp, /* ptr to btree block */
1744 struct xfs_btree_block **blkp) /* return btree block */
1746 struct xfs_buf *bp; /* buffer pointer for btree block */
1749 /* special case the root block if in an inode */
1750 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
1751 (level == cur->bc_nlevels - 1)) {
1752 *blkp = xfs_btree_get_iroot(cur);
1757 * If the old buffer at this level for the disk address we are
1758 * looking for re-use it.
1760 * Otherwise throw it away and get a new one.
1762 bp = cur->bc_bufs[level];
1763 if (bp && XFS_BUF_ADDR(bp) == xfs_btree_ptr_to_daddr(cur, pp)) {
1764 *blkp = XFS_BUF_TO_BLOCK(bp);
1768 error = xfs_btree_read_buf_block(cur, pp, 0, blkp, &bp);
1772 /* Check the inode owner since the verifiers don't. */
1773 if (xfs_sb_version_hascrc(&cur->bc_mp->m_sb) &&
1774 (cur->bc_flags & XFS_BTREE_LONG_PTRS) &&
1775 be64_to_cpu((*blkp)->bb_u.l.bb_owner) !=
1776 cur->bc_private.b.ip->i_ino)
1779 /* Did we get the level we were looking for? */
1780 if (be16_to_cpu((*blkp)->bb_level) != level)
1783 /* Check that internal nodes have at least one record. */
1784 if (level != 0 && be16_to_cpu((*blkp)->bb_numrecs) == 0)
1787 xfs_btree_setbuf(cur, level, bp);
1792 xfs_trans_brelse(cur->bc_tp, bp);
1793 return -EFSCORRUPTED;
1797 * Get current search key. For level 0 we don't actually have a key
1798 * structure so we make one up from the record. For all other levels
1799 * we just return the right key.
1801 STATIC union xfs_btree_key *
1802 xfs_lookup_get_search_key(
1803 struct xfs_btree_cur *cur,
1806 struct xfs_btree_block *block,
1807 union xfs_btree_key *kp)
1810 cur->bc_ops->init_key_from_rec(kp,
1811 xfs_btree_rec_addr(cur, keyno, block));
1815 return xfs_btree_key_addr(cur, keyno, block);
1819 * Lookup the record. The cursor is made to point to it, based on dir.
1820 * stat is set to 0 if can't find any such record, 1 for success.
1824 struct xfs_btree_cur *cur, /* btree cursor */
1825 xfs_lookup_t dir, /* <=, ==, or >= */
1826 int *stat) /* success/failure */
1828 struct xfs_btree_block *block; /* current btree block */
1829 __int64_t diff; /* difference for the current key */
1830 int error; /* error return value */
1831 int keyno; /* current key number */
1832 int level; /* level in the btree */
1833 union xfs_btree_ptr *pp; /* ptr to btree block */
1834 union xfs_btree_ptr ptr; /* ptr to btree block */
1836 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1837 XFS_BTREE_TRACE_ARGI(cur, dir);
1839 XFS_BTREE_STATS_INC(cur, lookup);
1841 /* No such thing as a zero-level tree. */
1842 if (cur->bc_nlevels == 0)
1843 return -EFSCORRUPTED;
1848 /* initialise start pointer from cursor */
1849 cur->bc_ops->init_ptr_from_cur(cur, &ptr);
1853 * Iterate over each level in the btree, starting at the root.
1854 * For each level above the leaves, find the key we need, based
1855 * on the lookup record, then follow the corresponding block
1856 * pointer down to the next level.
1858 for (level = cur->bc_nlevels - 1, diff = 1; level >= 0; level--) {
1859 /* Get the block we need to do the lookup on. */
1860 error = xfs_btree_lookup_get_block(cur, level, pp, &block);
1866 * If we already had a key match at a higher level, we
1867 * know we need to use the first entry in this block.
1871 /* Otherwise search this block. Do a binary search. */
1873 int high; /* high entry number */
1874 int low; /* low entry number */
1876 /* Set low and high entry numbers, 1-based. */
1878 high = xfs_btree_get_numrecs(block);
1880 /* Block is empty, must be an empty leaf. */
1881 ASSERT(level == 0 && cur->bc_nlevels == 1);
1883 cur->bc_ptrs[0] = dir != XFS_LOOKUP_LE;
1884 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1889 /* Binary search the block. */
1890 while (low <= high) {
1891 union xfs_btree_key key;
1892 union xfs_btree_key *kp;
1894 XFS_BTREE_STATS_INC(cur, compare);
1896 /* keyno is average of low and high. */
1897 keyno = (low + high) >> 1;
1899 /* Get current search key */
1900 kp = xfs_lookup_get_search_key(cur, level,
1901 keyno, block, &key);
1904 * Compute difference to get next direction:
1905 * - less than, move right
1906 * - greater than, move left
1907 * - equal, we're done
1909 diff = cur->bc_ops->key_diff(cur, kp);
1920 * If there are more levels, set up for the next level
1921 * by getting the block number and filling in the cursor.
1925 * If we moved left, need the previous key number,
1926 * unless there isn't one.
1928 if (diff > 0 && --keyno < 1)
1930 pp = xfs_btree_ptr_addr(cur, keyno, block);
1933 error = xfs_btree_check_ptr(cur, pp, 0, level);
1937 cur->bc_ptrs[level] = keyno;
1941 /* Done with the search. See if we need to adjust the results. */
1942 if (dir != XFS_LOOKUP_LE && diff < 0) {
1945 * If ge search and we went off the end of the block, but it's
1946 * not the last block, we're in the wrong block.
1948 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_RIGHTSIB);
1949 if (dir == XFS_LOOKUP_GE &&
1950 keyno > xfs_btree_get_numrecs(block) &&
1951 !xfs_btree_ptr_is_null(cur, &ptr)) {
1954 cur->bc_ptrs[0] = keyno;
1955 error = xfs_btree_increment(cur, 0, &i);
1958 XFS_WANT_CORRUPTED_RETURN(cur->bc_mp, i == 1);
1959 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1963 } else if (dir == XFS_LOOKUP_LE && diff > 0)
1965 cur->bc_ptrs[0] = keyno;
1967 /* Return if we succeeded or not. */
1968 if (keyno == 0 || keyno > xfs_btree_get_numrecs(block))
1970 else if (dir != XFS_LOOKUP_EQ || diff == 0)
1974 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1978 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
1982 /* Find the high key storage area from a regular key. */
1983 STATIC union xfs_btree_key *
1984 xfs_btree_high_key_from_key(
1985 struct xfs_btree_cur *cur,
1986 union xfs_btree_key *key)
1988 ASSERT(cur->bc_flags & XFS_BTREE_OVERLAPPING);
1989 return (union xfs_btree_key *)((char *)key +
1990 (cur->bc_ops->key_len / 2));
1993 /* Determine the low (and high if overlapped) keys of a leaf block */
1995 xfs_btree_get_leaf_keys(
1996 struct xfs_btree_cur *cur,
1997 struct xfs_btree_block *block,
1998 union xfs_btree_key *key)
2000 union xfs_btree_key max_hkey;
2001 union xfs_btree_key hkey;
2002 union xfs_btree_rec *rec;
2003 union xfs_btree_key *high;
2006 rec = xfs_btree_rec_addr(cur, 1, block);
2007 cur->bc_ops->init_key_from_rec(key, rec);
2009 if (cur->bc_flags & XFS_BTREE_OVERLAPPING) {
2011 cur->bc_ops->init_high_key_from_rec(&max_hkey, rec);
2012 for (n = 2; n <= xfs_btree_get_numrecs(block); n++) {
2013 rec = xfs_btree_rec_addr(cur, n, block);
2014 cur->bc_ops->init_high_key_from_rec(&hkey, rec);
2015 if (cur->bc_ops->diff_two_keys(cur, &hkey, &max_hkey)
2020 high = xfs_btree_high_key_from_key(cur, key);
2021 memcpy(high, &max_hkey, cur->bc_ops->key_len / 2);
2025 /* Determine the low (and high if overlapped) keys of a node block */
2027 xfs_btree_get_node_keys(
2028 struct xfs_btree_cur *cur,
2029 struct xfs_btree_block *block,
2030 union xfs_btree_key *key)
2032 union xfs_btree_key *hkey;
2033 union xfs_btree_key *max_hkey;
2034 union xfs_btree_key *high;
2037 if (cur->bc_flags & XFS_BTREE_OVERLAPPING) {
2038 memcpy(key, xfs_btree_key_addr(cur, 1, block),
2039 cur->bc_ops->key_len / 2);
2041 max_hkey = xfs_btree_high_key_addr(cur, 1, block);
2042 for (n = 2; n <= xfs_btree_get_numrecs(block); n++) {
2043 hkey = xfs_btree_high_key_addr(cur, n, block);
2044 if (cur->bc_ops->diff_two_keys(cur, hkey, max_hkey) > 0)
2048 high = xfs_btree_high_key_from_key(cur, key);
2049 memcpy(high, max_hkey, cur->bc_ops->key_len / 2);
2051 memcpy(key, xfs_btree_key_addr(cur, 1, block),
2052 cur->bc_ops->key_len);
2056 /* Derive the keys for any btree block. */
2059 struct xfs_btree_cur *cur,
2060 struct xfs_btree_block *block,
2061 union xfs_btree_key *key)
2063 if (be16_to_cpu(block->bb_level) == 0)
2064 xfs_btree_get_leaf_keys(cur, block, key);
2066 xfs_btree_get_node_keys(cur, block, key);
2070 * Decide if we need to update the parent keys of a btree block. For
2071 * a standard btree this is only necessary if we're updating the first
2072 * record/key. For an overlapping btree, we must always update the
2073 * keys because the highest key can be in any of the records or keys
2077 xfs_btree_needs_key_update(
2078 struct xfs_btree_cur *cur,
2081 return (cur->bc_flags & XFS_BTREE_OVERLAPPING) || ptr == 1;
2085 * Update the low and high parent keys of the given level, progressing
2086 * towards the root. If force_all is false, stop if the keys for a given
2087 * level do not need updating.
2090 __xfs_btree_updkeys(
2091 struct xfs_btree_cur *cur,
2093 struct xfs_btree_block *block,
2094 struct xfs_buf *bp0,
2097 union xfs_btree_key key; /* keys from current level */
2098 union xfs_btree_key *lkey; /* keys from the next level up */
2099 union xfs_btree_key *hkey;
2100 union xfs_btree_key *nlkey; /* keys from the next level up */
2101 union xfs_btree_key *nhkey;
2105 ASSERT(cur->bc_flags & XFS_BTREE_OVERLAPPING);
2107 /* Exit if there aren't any parent levels to update. */
2108 if (level + 1 >= cur->bc_nlevels)
2111 trace_xfs_btree_updkeys(cur, level, bp0);
2114 hkey = xfs_btree_high_key_from_key(cur, lkey);
2115 xfs_btree_get_keys(cur, block, lkey);
2116 for (level++; level < cur->bc_nlevels; level++) {
2120 block = xfs_btree_get_block(cur, level, &bp);
2121 trace_xfs_btree_updkeys(cur, level, bp);
2123 error = xfs_btree_check_block(cur, block, level, bp);
2125 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
2129 ptr = cur->bc_ptrs[level];
2130 nlkey = xfs_btree_key_addr(cur, ptr, block);
2131 nhkey = xfs_btree_high_key_addr(cur, ptr, block);
2133 !(cur->bc_ops->diff_two_keys(cur, nlkey, lkey) != 0 ||
2134 cur->bc_ops->diff_two_keys(cur, nhkey, hkey) != 0))
2136 xfs_btree_copy_keys(cur, nlkey, lkey, 1);
2137 xfs_btree_log_keys(cur, bp, ptr, ptr);
2138 if (level + 1 >= cur->bc_nlevels)
2140 xfs_btree_get_node_keys(cur, block, lkey);
2146 /* Update all the keys from some level in cursor back to the root. */
2148 xfs_btree_updkeys_force(
2149 struct xfs_btree_cur *cur,
2153 struct xfs_btree_block *block;
2155 block = xfs_btree_get_block(cur, level, &bp);
2156 return __xfs_btree_updkeys(cur, level, block, bp, true);
2160 * Update the parent keys of the given level, progressing towards the root.
2163 xfs_btree_update_keys(
2164 struct xfs_btree_cur *cur,
2167 struct xfs_btree_block *block;
2169 union xfs_btree_key *kp;
2170 union xfs_btree_key key;
2175 block = xfs_btree_get_block(cur, level, &bp);
2176 if (cur->bc_flags & XFS_BTREE_OVERLAPPING)
2177 return __xfs_btree_updkeys(cur, level, block, bp, false);
2179 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
2180 XFS_BTREE_TRACE_ARGIK(cur, level, keyp);
2183 * Go up the tree from this level toward the root.
2184 * At each level, update the key value to the value input.
2185 * Stop when we reach a level where the cursor isn't pointing
2186 * at the first entry in the block.
2188 xfs_btree_get_keys(cur, block, &key);
2189 for (level++, ptr = 1; ptr == 1 && level < cur->bc_nlevels; level++) {
2193 block = xfs_btree_get_block(cur, level, &bp);
2195 error = xfs_btree_check_block(cur, block, level, bp);
2197 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
2201 ptr = cur->bc_ptrs[level];
2202 kp = xfs_btree_key_addr(cur, ptr, block);
2203 xfs_btree_copy_keys(cur, kp, &key, 1);
2204 xfs_btree_log_keys(cur, bp, ptr, ptr);
2207 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2212 * Update the record referred to by cur to the value in the
2213 * given record. This either works (return 0) or gets an
2214 * EFSCORRUPTED error.
2218 struct xfs_btree_cur *cur,
2219 union xfs_btree_rec *rec)
2221 struct xfs_btree_block *block;
2225 union xfs_btree_rec *rp;
2227 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
2228 XFS_BTREE_TRACE_ARGR(cur, rec);
2230 /* Pick up the current block. */
2231 block = xfs_btree_get_block(cur, 0, &bp);
2234 error = xfs_btree_check_block(cur, block, 0, bp);
2238 /* Get the address of the rec to be updated. */
2239 ptr = cur->bc_ptrs[0];
2240 rp = xfs_btree_rec_addr(cur, ptr, block);
2242 /* Fill in the new contents and log them. */
2243 xfs_btree_copy_recs(cur, rp, rec, 1);
2244 xfs_btree_log_recs(cur, bp, ptr, ptr);
2247 * If we are tracking the last record in the tree and
2248 * we are at the far right edge of the tree, update it.
2250 if (xfs_btree_is_lastrec(cur, block, 0)) {
2251 cur->bc_ops->update_lastrec(cur, block, rec,
2252 ptr, LASTREC_UPDATE);
2255 /* Pass new key value up to our parent. */
2256 if (xfs_btree_needs_key_update(cur, ptr)) {
2257 error = xfs_btree_update_keys(cur, 0);
2262 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2266 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
2271 * Move 1 record left from cur/level if possible.
2272 * Update cur to reflect the new path.
2274 STATIC int /* error */
2276 struct xfs_btree_cur *cur,
2278 int *stat) /* success/failure */
2280 struct xfs_buf *lbp; /* left buffer pointer */
2281 struct xfs_btree_block *left; /* left btree block */
2282 int lrecs; /* left record count */
2283 struct xfs_buf *rbp; /* right buffer pointer */
2284 struct xfs_btree_block *right; /* right btree block */
2285 struct xfs_btree_cur *tcur; /* temporary btree cursor */
2286 int rrecs; /* right record count */
2287 union xfs_btree_ptr lptr; /* left btree pointer */
2288 union xfs_btree_key *rkp = NULL; /* right btree key */
2289 union xfs_btree_ptr *rpp = NULL; /* right address pointer */
2290 union xfs_btree_rec *rrp = NULL; /* right record pointer */
2291 int error; /* error return value */
2294 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
2295 XFS_BTREE_TRACE_ARGI(cur, level);
2297 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
2298 level == cur->bc_nlevels - 1)
2301 /* Set up variables for this block as "right". */
2302 right = xfs_btree_get_block(cur, level, &rbp);
2305 error = xfs_btree_check_block(cur, right, level, rbp);
2310 /* If we've got no left sibling then we can't shift an entry left. */
2311 xfs_btree_get_sibling(cur, right, &lptr, XFS_BB_LEFTSIB);
2312 if (xfs_btree_ptr_is_null(cur, &lptr))
2316 * If the cursor entry is the one that would be moved, don't
2317 * do it... it's too complicated.
2319 if (cur->bc_ptrs[level] <= 1)
2322 /* Set up the left neighbor as "left". */
2323 error = xfs_btree_read_buf_block(cur, &lptr, 0, &left, &lbp);
2327 /* If it's full, it can't take another entry. */
2328 lrecs = xfs_btree_get_numrecs(left);
2329 if (lrecs == cur->bc_ops->get_maxrecs(cur, level))
2332 rrecs = xfs_btree_get_numrecs(right);
2335 * We add one entry to the left side and remove one for the right side.
2336 * Account for it here, the changes will be updated on disk and logged
2342 XFS_BTREE_STATS_INC(cur, lshift);
2343 XFS_BTREE_STATS_ADD(cur, moves, 1);
2346 * If non-leaf, copy a key and a ptr to the left block.
2347 * Log the changes to the left block.
2350 /* It's a non-leaf. Move keys and pointers. */
2351 union xfs_btree_key *lkp; /* left btree key */
2352 union xfs_btree_ptr *lpp; /* left address pointer */
2354 lkp = xfs_btree_key_addr(cur, lrecs, left);
2355 rkp = xfs_btree_key_addr(cur, 1, right);
2357 lpp = xfs_btree_ptr_addr(cur, lrecs, left);
2358 rpp = xfs_btree_ptr_addr(cur, 1, right);
2360 error = xfs_btree_check_ptr(cur, rpp, 0, level);
2364 xfs_btree_copy_keys(cur, lkp, rkp, 1);
2365 xfs_btree_copy_ptrs(cur, lpp, rpp, 1);
2367 xfs_btree_log_keys(cur, lbp, lrecs, lrecs);
2368 xfs_btree_log_ptrs(cur, lbp, lrecs, lrecs);
2370 ASSERT(cur->bc_ops->keys_inorder(cur,
2371 xfs_btree_key_addr(cur, lrecs - 1, left), lkp));
2373 /* It's a leaf. Move records. */
2374 union xfs_btree_rec *lrp; /* left record pointer */
2376 lrp = xfs_btree_rec_addr(cur, lrecs, left);
2377 rrp = xfs_btree_rec_addr(cur, 1, right);
2379 xfs_btree_copy_recs(cur, lrp, rrp, 1);
2380 xfs_btree_log_recs(cur, lbp, lrecs, lrecs);
2382 ASSERT(cur->bc_ops->recs_inorder(cur,
2383 xfs_btree_rec_addr(cur, lrecs - 1, left), lrp));
2386 xfs_btree_set_numrecs(left, lrecs);
2387 xfs_btree_log_block(cur, lbp, XFS_BB_NUMRECS);
2389 xfs_btree_set_numrecs(right, rrecs);
2390 xfs_btree_log_block(cur, rbp, XFS_BB_NUMRECS);
2393 * Slide the contents of right down one entry.
2395 XFS_BTREE_STATS_ADD(cur, moves, rrecs - 1);
2397 /* It's a nonleaf. operate on keys and ptrs */
2399 int i; /* loop index */
2401 for (i = 0; i < rrecs; i++) {
2402 error = xfs_btree_check_ptr(cur, rpp, i + 1, level);
2407 xfs_btree_shift_keys(cur,
2408 xfs_btree_key_addr(cur, 2, right),
2410 xfs_btree_shift_ptrs(cur,
2411 xfs_btree_ptr_addr(cur, 2, right),
2414 xfs_btree_log_keys(cur, rbp, 1, rrecs);
2415 xfs_btree_log_ptrs(cur, rbp, 1, rrecs);
2417 /* It's a leaf. operate on records */
2418 xfs_btree_shift_recs(cur,
2419 xfs_btree_rec_addr(cur, 2, right),
2421 xfs_btree_log_recs(cur, rbp, 1, rrecs);
2425 * Using a temporary cursor, update the parent key values of the
2426 * block on the left.
2428 if (cur->bc_flags & XFS_BTREE_OVERLAPPING) {
2429 error = xfs_btree_dup_cursor(cur, &tcur);
2432 i = xfs_btree_firstrec(tcur, level);
2433 XFS_WANT_CORRUPTED_GOTO(tcur->bc_mp, i == 1, error0);
2435 error = xfs_btree_decrement(tcur, level, &i);
2439 /* Update the parent high keys of the left block, if needed. */
2440 error = xfs_btree_update_keys(tcur, level);
2444 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
2447 /* Update the parent keys of the right block. */
2448 error = xfs_btree_update_keys(cur, level);
2452 /* Slide the cursor value left one. */
2453 cur->bc_ptrs[level]--;
2455 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2460 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2465 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
2469 XFS_BTREE_TRACE_CURSOR(tcur, XBT_ERROR);
2470 xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
2475 * Move 1 record right from cur/level if possible.
2476 * Update cur to reflect the new path.
2478 STATIC int /* error */
2480 struct xfs_btree_cur *cur,
2482 int *stat) /* success/failure */
2484 struct xfs_buf *lbp; /* left buffer pointer */
2485 struct xfs_btree_block *left; /* left btree block */
2486 struct xfs_buf *rbp; /* right buffer pointer */
2487 struct xfs_btree_block *right; /* right btree block */
2488 struct xfs_btree_cur *tcur; /* temporary btree cursor */
2489 union xfs_btree_ptr rptr; /* right block pointer */
2490 union xfs_btree_key *rkp; /* right btree key */
2491 int rrecs; /* right record count */
2492 int lrecs; /* left record count */
2493 int error; /* error return value */
2494 int i; /* loop counter */
2496 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
2497 XFS_BTREE_TRACE_ARGI(cur, level);
2499 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
2500 (level == cur->bc_nlevels - 1))
2503 /* Set up variables for this block as "left". */
2504 left = xfs_btree_get_block(cur, level, &lbp);
2507 error = xfs_btree_check_block(cur, left, level, lbp);
2512 /* If we've got no right sibling then we can't shift an entry right. */
2513 xfs_btree_get_sibling(cur, left, &rptr, XFS_BB_RIGHTSIB);
2514 if (xfs_btree_ptr_is_null(cur, &rptr))
2518 * If the cursor entry is the one that would be moved, don't
2519 * do it... it's too complicated.
2521 lrecs = xfs_btree_get_numrecs(left);
2522 if (cur->bc_ptrs[level] >= lrecs)
2525 /* Set up the right neighbor as "right". */
2526 error = xfs_btree_read_buf_block(cur, &rptr, 0, &right, &rbp);
2530 /* If it's full, it can't take another entry. */
2531 rrecs = xfs_btree_get_numrecs(right);
2532 if (rrecs == cur->bc_ops->get_maxrecs(cur, level))
2535 XFS_BTREE_STATS_INC(cur, rshift);
2536 XFS_BTREE_STATS_ADD(cur, moves, rrecs);
2539 * Make a hole at the start of the right neighbor block, then
2540 * copy the last left block entry to the hole.
2543 /* It's a nonleaf. make a hole in the keys and ptrs */
2544 union xfs_btree_key *lkp;
2545 union xfs_btree_ptr *lpp;
2546 union xfs_btree_ptr *rpp;
2548 lkp = xfs_btree_key_addr(cur, lrecs, left);
2549 lpp = xfs_btree_ptr_addr(cur, lrecs, left);
2550 rkp = xfs_btree_key_addr(cur, 1, right);
2551 rpp = xfs_btree_ptr_addr(cur, 1, right);
2554 for (i = rrecs - 1; i >= 0; i--) {
2555 error = xfs_btree_check_ptr(cur, rpp, i, level);
2561 xfs_btree_shift_keys(cur, rkp, 1, rrecs);
2562 xfs_btree_shift_ptrs(cur, rpp, 1, rrecs);
2565 error = xfs_btree_check_ptr(cur, lpp, 0, level);
2570 /* Now put the new data in, and log it. */
2571 xfs_btree_copy_keys(cur, rkp, lkp, 1);
2572 xfs_btree_copy_ptrs(cur, rpp, lpp, 1);
2574 xfs_btree_log_keys(cur, rbp, 1, rrecs + 1);
2575 xfs_btree_log_ptrs(cur, rbp, 1, rrecs + 1);
2577 ASSERT(cur->bc_ops->keys_inorder(cur, rkp,
2578 xfs_btree_key_addr(cur, 2, right)));
2580 /* It's a leaf. make a hole in the records */
2581 union xfs_btree_rec *lrp;
2582 union xfs_btree_rec *rrp;
2584 lrp = xfs_btree_rec_addr(cur, lrecs, left);
2585 rrp = xfs_btree_rec_addr(cur, 1, right);
2587 xfs_btree_shift_recs(cur, rrp, 1, rrecs);
2589 /* Now put the new data in, and log it. */
2590 xfs_btree_copy_recs(cur, rrp, lrp, 1);
2591 xfs_btree_log_recs(cur, rbp, 1, rrecs + 1);
2595 * Decrement and log left's numrecs, bump and log right's numrecs.
2597 xfs_btree_set_numrecs(left, --lrecs);
2598 xfs_btree_log_block(cur, lbp, XFS_BB_NUMRECS);
2600 xfs_btree_set_numrecs(right, ++rrecs);
2601 xfs_btree_log_block(cur, rbp, XFS_BB_NUMRECS);
2604 * Using a temporary cursor, update the parent key values of the
2605 * block on the right.
2607 error = xfs_btree_dup_cursor(cur, &tcur);
2610 i = xfs_btree_lastrec(tcur, level);
2611 XFS_WANT_CORRUPTED_GOTO(tcur->bc_mp, i == 1, error0);
2613 error = xfs_btree_increment(tcur, level, &i);
2617 /* Update the parent high keys of the left block, if needed. */
2618 if (cur->bc_flags & XFS_BTREE_OVERLAPPING) {
2619 error = xfs_btree_update_keys(cur, level);
2624 /* Update the parent keys of the right block. */
2625 error = xfs_btree_update_keys(tcur, level);
2629 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
2631 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2636 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2641 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
2645 XFS_BTREE_TRACE_CURSOR(tcur, XBT_ERROR);
2646 xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
2651 * Split cur/level block in half.
2652 * Return new block number and the key to its first
2653 * record (to be inserted into parent).
2655 STATIC int /* error */
2657 struct xfs_btree_cur *cur,
2659 union xfs_btree_ptr *ptrp,
2660 union xfs_btree_key *key,
2661 struct xfs_btree_cur **curp,
2662 int *stat) /* success/failure */
2664 union xfs_btree_ptr lptr; /* left sibling block ptr */
2665 struct xfs_buf *lbp; /* left buffer pointer */
2666 struct xfs_btree_block *left; /* left btree block */
2667 union xfs_btree_ptr rptr; /* right sibling block ptr */
2668 struct xfs_buf *rbp; /* right buffer pointer */
2669 struct xfs_btree_block *right; /* right btree block */
2670 union xfs_btree_ptr rrptr; /* right-right sibling ptr */
2671 struct xfs_buf *rrbp; /* right-right buffer pointer */
2672 struct xfs_btree_block *rrblock; /* right-right btree block */
2676 int error; /* error return value */
2681 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
2682 XFS_BTREE_TRACE_ARGIPK(cur, level, *ptrp, key);
2684 XFS_BTREE_STATS_INC(cur, split);
2686 /* Set up left block (current one). */
2687 left = xfs_btree_get_block(cur, level, &lbp);
2690 error = xfs_btree_check_block(cur, left, level, lbp);
2695 xfs_btree_buf_to_ptr(cur, lbp, &lptr);
2697 /* Allocate the new block. If we can't do it, we're toast. Give up. */
2698 error = cur->bc_ops->alloc_block(cur, &lptr, &rptr, stat);
2703 XFS_BTREE_STATS_INC(cur, alloc);
2705 /* Set up the new block as "right". */
2706 error = xfs_btree_get_buf_block(cur, &rptr, 0, &right, &rbp);
2710 /* Fill in the btree header for the new right block. */
2711 xfs_btree_init_block_cur(cur, rbp, xfs_btree_get_level(left), 0);
2714 * Split the entries between the old and the new block evenly.
2715 * Make sure that if there's an odd number of entries now, that
2716 * each new block will have the same number of entries.
2718 lrecs = xfs_btree_get_numrecs(left);
2720 if ((lrecs & 1) && cur->bc_ptrs[level] <= rrecs + 1)
2722 src_index = (lrecs - rrecs + 1);
2724 XFS_BTREE_STATS_ADD(cur, moves, rrecs);
2726 /* Adjust numrecs for the later get_*_keys() calls. */
2728 xfs_btree_set_numrecs(left, lrecs);
2729 xfs_btree_set_numrecs(right, xfs_btree_get_numrecs(right) + rrecs);
2732 * Copy btree block entries from the left block over to the
2733 * new block, the right. Update the right block and log the
2737 /* It's a non-leaf. Move keys and pointers. */
2738 union xfs_btree_key *lkp; /* left btree key */
2739 union xfs_btree_ptr *lpp; /* left address pointer */
2740 union xfs_btree_key *rkp; /* right btree key */
2741 union xfs_btree_ptr *rpp; /* right address pointer */
2743 lkp = xfs_btree_key_addr(cur, src_index, left);
2744 lpp = xfs_btree_ptr_addr(cur, src_index, left);
2745 rkp = xfs_btree_key_addr(cur, 1, right);
2746 rpp = xfs_btree_ptr_addr(cur, 1, right);
2749 for (i = src_index; i < rrecs; i++) {
2750 error = xfs_btree_check_ptr(cur, lpp, i, level);
2756 /* Copy the keys & pointers to the new block. */
2757 xfs_btree_copy_keys(cur, rkp, lkp, rrecs);
2758 xfs_btree_copy_ptrs(cur, rpp, lpp, rrecs);
2760 xfs_btree_log_keys(cur, rbp, 1, rrecs);
2761 xfs_btree_log_ptrs(cur, rbp, 1, rrecs);
2763 /* Stash the keys of the new block for later insertion. */
2764 xfs_btree_get_node_keys(cur, right, key);
2766 /* It's a leaf. Move records. */
2767 union xfs_btree_rec *lrp; /* left record pointer */
2768 union xfs_btree_rec *rrp; /* right record pointer */
2770 lrp = xfs_btree_rec_addr(cur, src_index, left);
2771 rrp = xfs_btree_rec_addr(cur, 1, right);
2773 /* Copy records to the new block. */
2774 xfs_btree_copy_recs(cur, rrp, lrp, rrecs);
2775 xfs_btree_log_recs(cur, rbp, 1, rrecs);
2777 /* Stash the keys of the new block for later insertion. */
2778 xfs_btree_get_leaf_keys(cur, right, key);
2782 * Find the left block number by looking in the buffer.
2783 * Adjust sibling pointers.
2785 xfs_btree_get_sibling(cur, left, &rrptr, XFS_BB_RIGHTSIB);
2786 xfs_btree_set_sibling(cur, right, &rrptr, XFS_BB_RIGHTSIB);
2787 xfs_btree_set_sibling(cur, right, &lptr, XFS_BB_LEFTSIB);
2788 xfs_btree_set_sibling(cur, left, &rptr, XFS_BB_RIGHTSIB);
2790 xfs_btree_log_block(cur, rbp, XFS_BB_ALL_BITS);
2791 xfs_btree_log_block(cur, lbp, XFS_BB_NUMRECS | XFS_BB_RIGHTSIB);
2794 * If there's a block to the new block's right, make that block
2795 * point back to right instead of to left.
2797 if (!xfs_btree_ptr_is_null(cur, &rrptr)) {
2798 error = xfs_btree_read_buf_block(cur, &rrptr,
2799 0, &rrblock, &rrbp);
2802 xfs_btree_set_sibling(cur, rrblock, &rptr, XFS_BB_LEFTSIB);
2803 xfs_btree_log_block(cur, rrbp, XFS_BB_LEFTSIB);
2806 /* Update the parent high keys of the left block, if needed. */
2807 if (cur->bc_flags & XFS_BTREE_OVERLAPPING) {
2808 error = xfs_btree_update_keys(cur, level);
2814 * If the cursor is really in the right block, move it there.
2815 * If it's just pointing past the last entry in left, then we'll
2816 * insert there, so don't change anything in that case.
2818 if (cur->bc_ptrs[level] > lrecs + 1) {
2819 xfs_btree_setbuf(cur, level, rbp);
2820 cur->bc_ptrs[level] -= lrecs;
2823 * If there are more levels, we'll need another cursor which refers
2824 * the right block, no matter where this cursor was.
2826 if (level + 1 < cur->bc_nlevels) {
2827 error = xfs_btree_dup_cursor(cur, curp);
2830 (*curp)->bc_ptrs[level + 1]++;
2833 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2837 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2842 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
2846 struct xfs_btree_split_args {
2847 struct xfs_btree_cur *cur;
2849 union xfs_btree_ptr *ptrp;
2850 union xfs_btree_key *key;
2851 struct xfs_btree_cur **curp;
2852 int *stat; /* success/failure */
2854 bool kswapd; /* allocation in kswapd context */
2855 struct completion *done;
2856 struct work_struct work;
2860 * Stack switching interfaces for allocation
2863 xfs_btree_split_worker(
2864 struct work_struct *work)
2866 struct xfs_btree_split_args *args = container_of(work,
2867 struct xfs_btree_split_args, work);
2868 unsigned long pflags;
2869 unsigned long new_pflags = PF_FSTRANS;
2872 * we are in a transaction context here, but may also be doing work
2873 * in kswapd context, and hence we may need to inherit that state
2874 * temporarily to ensure that we don't block waiting for memory reclaim
2878 new_pflags |= PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD;
2880 current_set_flags_nested(&pflags, new_pflags);
2882 args->result = __xfs_btree_split(args->cur, args->level, args->ptrp,
2883 args->key, args->curp, args->stat);
2884 complete(args->done);
2886 current_restore_flags_nested(&pflags, new_pflags);
2890 * BMBT split requests often come in with little stack to work on. Push
2891 * them off to a worker thread so there is lots of stack to use. For the other
2892 * btree types, just call directly to avoid the context switch overhead here.
2894 STATIC int /* error */
2896 struct xfs_btree_cur *cur,
2898 union xfs_btree_ptr *ptrp,
2899 union xfs_btree_key *key,
2900 struct xfs_btree_cur **curp,
2901 int *stat) /* success/failure */
2903 struct xfs_btree_split_args args;
2904 DECLARE_COMPLETION_ONSTACK(done);
2906 if (cur->bc_btnum != XFS_BTNUM_BMAP)
2907 return __xfs_btree_split(cur, level, ptrp, key, curp, stat);
2916 args.kswapd = current_is_kswapd();
2917 INIT_WORK_ONSTACK(&args.work, xfs_btree_split_worker);
2918 queue_work(xfs_alloc_wq, &args.work);
2919 wait_for_completion(&done);
2920 destroy_work_on_stack(&args.work);
2926 * Copy the old inode root contents into a real block and make the
2927 * broot point to it.
2930 xfs_btree_new_iroot(
2931 struct xfs_btree_cur *cur, /* btree cursor */
2932 int *logflags, /* logging flags for inode */
2933 int *stat) /* return status - 0 fail */
2935 struct xfs_buf *cbp; /* buffer for cblock */
2936 struct xfs_btree_block *block; /* btree block */
2937 struct xfs_btree_block *cblock; /* child btree block */
2938 union xfs_btree_key *ckp; /* child key pointer */
2939 union xfs_btree_ptr *cpp; /* child ptr pointer */
2940 union xfs_btree_key *kp; /* pointer to btree key */
2941 union xfs_btree_ptr *pp; /* pointer to block addr */
2942 union xfs_btree_ptr nptr; /* new block addr */
2943 int level; /* btree level */
2944 int error; /* error return code */
2946 int i; /* loop counter */
2949 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
2950 XFS_BTREE_STATS_INC(cur, newroot);
2952 ASSERT(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE);
2954 level = cur->bc_nlevels - 1;
2956 block = xfs_btree_get_iroot(cur);
2957 pp = xfs_btree_ptr_addr(cur, 1, block);
2959 /* Allocate the new block. If we can't do it, we're toast. Give up. */
2960 error = cur->bc_ops->alloc_block(cur, pp, &nptr, stat);
2964 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2967 XFS_BTREE_STATS_INC(cur, alloc);
2969 /* Copy the root into a real block. */
2970 error = xfs_btree_get_buf_block(cur, &nptr, 0, &cblock, &cbp);
2975 * we can't just memcpy() the root in for CRC enabled btree blocks.
2976 * In that case have to also ensure the blkno remains correct
2978 memcpy(cblock, block, xfs_btree_block_len(cur));
2979 if (cur->bc_flags & XFS_BTREE_CRC_BLOCKS) {
2980 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
2981 cblock->bb_u.l.bb_blkno = cpu_to_be64(cbp->b_bn);
2983 cblock->bb_u.s.bb_blkno = cpu_to_be64(cbp->b_bn);
2986 be16_add_cpu(&block->bb_level, 1);
2987 xfs_btree_set_numrecs(block, 1);
2989 cur->bc_ptrs[level + 1] = 1;
2991 kp = xfs_btree_key_addr(cur, 1, block);
2992 ckp = xfs_btree_key_addr(cur, 1, cblock);
2993 xfs_btree_copy_keys(cur, ckp, kp, xfs_btree_get_numrecs(cblock));
2995 cpp = xfs_btree_ptr_addr(cur, 1, cblock);
2997 for (i = 0; i < be16_to_cpu(cblock->bb_numrecs); i++) {
2998 error = xfs_btree_check_ptr(cur, pp, i, level);
3003 xfs_btree_copy_ptrs(cur, cpp, pp, xfs_btree_get_numrecs(cblock));
3006 error = xfs_btree_check_ptr(cur, &nptr, 0, level);
3010 xfs_btree_copy_ptrs(cur, pp, &nptr, 1);
3012 xfs_iroot_realloc(cur->bc_private.b.ip,
3013 1 - xfs_btree_get_numrecs(cblock),
3014 cur->bc_private.b.whichfork);
3016 xfs_btree_setbuf(cur, level, cbp);
3019 * Do all this logging at the end so that
3020 * the root is at the right level.
3022 xfs_btree_log_block(cur, cbp, XFS_BB_ALL_BITS);
3023 xfs_btree_log_keys(cur, cbp, 1, be16_to_cpu(cblock->bb_numrecs));
3024 xfs_btree_log_ptrs(cur, cbp, 1, be16_to_cpu(cblock->bb_numrecs));
3027 XFS_ILOG_CORE | xfs_ilog_fbroot(cur->bc_private.b.whichfork);
3029 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3032 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
3037 * Allocate a new root block, fill it in.
3039 STATIC int /* error */
3041 struct xfs_btree_cur *cur, /* btree cursor */
3042 int *stat) /* success/failure */
3044 struct xfs_btree_block *block; /* one half of the old root block */
3045 struct xfs_buf *bp; /* buffer containing block */
3046 int error; /* error return value */
3047 struct xfs_buf *lbp; /* left buffer pointer */
3048 struct xfs_btree_block *left; /* left btree block */
3049 struct xfs_buf *nbp; /* new (root) buffer */
3050 struct xfs_btree_block *new; /* new (root) btree block */
3051 int nptr; /* new value for key index, 1 or 2 */
3052 struct xfs_buf *rbp; /* right buffer pointer */
3053 struct xfs_btree_block *right; /* right btree block */
3054 union xfs_btree_ptr rptr;
3055 union xfs_btree_ptr lptr;
3057 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
3058 XFS_BTREE_STATS_INC(cur, newroot);
3060 /* initialise our start point from the cursor */
3061 cur->bc_ops->init_ptr_from_cur(cur, &rptr);
3063 /* Allocate the new block. If we can't do it, we're toast. Give up. */
3064 error = cur->bc_ops->alloc_block(cur, &rptr, &lptr, stat);
3069 XFS_BTREE_STATS_INC(cur, alloc);
3071 /* Set up the new block. */
3072 error = xfs_btree_get_buf_block(cur, &lptr, 0, &new, &nbp);
3076 /* Set the root in the holding structure increasing the level by 1. */
3077 cur->bc_ops->set_root(cur, &lptr, 1);
3080 * At the previous root level there are now two blocks: the old root,
3081 * and the new block generated when it was split. We don't know which
3082 * one the cursor is pointing at, so we set up variables "left" and
3083 * "right" for each case.
3085 block = xfs_btree_get_block(cur, cur->bc_nlevels - 1, &bp);
3088 error = xfs_btree_check_block(cur, block, cur->bc_nlevels - 1, bp);
3093 xfs_btree_get_sibling(cur, block, &rptr, XFS_BB_RIGHTSIB);
3094 if (!xfs_btree_ptr_is_null(cur, &rptr)) {
3095 /* Our block is left, pick up the right block. */
3097 xfs_btree_buf_to_ptr(cur, lbp, &lptr);
3099 error = xfs_btree_read_buf_block(cur, &rptr, 0, &right, &rbp);
3105 /* Our block is right, pick up the left block. */
3107 xfs_btree_buf_to_ptr(cur, rbp, &rptr);
3109 xfs_btree_get_sibling(cur, right, &lptr, XFS_BB_LEFTSIB);
3110 error = xfs_btree_read_buf_block(cur, &lptr, 0, &left, &lbp);
3117 /* Fill in the new block's btree header and log it. */
3118 xfs_btree_init_block_cur(cur, nbp, cur->bc_nlevels, 2);
3119 xfs_btree_log_block(cur, nbp, XFS_BB_ALL_BITS);
3120 ASSERT(!xfs_btree_ptr_is_null(cur, &lptr) &&
3121 !xfs_btree_ptr_is_null(cur, &rptr));
3123 /* Fill in the key data in the new root. */
3124 if (xfs_btree_get_level(left) > 0) {
3126 * Get the keys for the left block's keys and put them directly
3127 * in the parent block. Do the same for the right block.
3129 xfs_btree_get_node_keys(cur, left,
3130 xfs_btree_key_addr(cur, 1, new));
3131 xfs_btree_get_node_keys(cur, right,
3132 xfs_btree_key_addr(cur, 2, new));
3135 * Get the keys for the left block's records and put them
3136 * directly in the parent block. Do the same for the right
3139 xfs_btree_get_leaf_keys(cur, left,
3140 xfs_btree_key_addr(cur, 1, new));
3141 xfs_btree_get_leaf_keys(cur, right,
3142 xfs_btree_key_addr(cur, 2, new));
3144 xfs_btree_log_keys(cur, nbp, 1, 2);
3146 /* Fill in the pointer data in the new root. */
3147 xfs_btree_copy_ptrs(cur,
3148 xfs_btree_ptr_addr(cur, 1, new), &lptr, 1);
3149 xfs_btree_copy_ptrs(cur,
3150 xfs_btree_ptr_addr(cur, 2, new), &rptr, 1);
3151 xfs_btree_log_ptrs(cur, nbp, 1, 2);
3153 /* Fix up the cursor. */
3154 xfs_btree_setbuf(cur, cur->bc_nlevels, nbp);
3155 cur->bc_ptrs[cur->bc_nlevels] = nptr;
3157 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3161 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
3164 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3170 xfs_btree_make_block_unfull(
3171 struct xfs_btree_cur *cur, /* btree cursor */
3172 int level, /* btree level */
3173 int numrecs,/* # of recs in block */
3174 int *oindex,/* old tree index */
3175 int *index, /* new tree index */
3176 union xfs_btree_ptr *nptr, /* new btree ptr */
3177 struct xfs_btree_cur **ncur, /* new btree cursor */
3178 union xfs_btree_key *key, /* key of new block */
3183 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
3184 level == cur->bc_nlevels - 1) {
3185 struct xfs_inode *ip = cur->bc_private.b.ip;
3187 if (numrecs < cur->bc_ops->get_dmaxrecs(cur, level)) {
3188 /* A root block that can be made bigger. */
3189 xfs_iroot_realloc(ip, 1, cur->bc_private.b.whichfork);
3192 /* A root block that needs replacing */
3195 error = xfs_btree_new_iroot(cur, &logflags, stat);
3196 if (error || *stat == 0)
3199 xfs_trans_log_inode(cur->bc_tp, ip, logflags);
3205 /* First, try shifting an entry to the right neighbor. */
3206 error = xfs_btree_rshift(cur, level, stat);
3210 /* Next, try shifting an entry to the left neighbor. */
3211 error = xfs_btree_lshift(cur, level, stat);
3216 *oindex = *index = cur->bc_ptrs[level];
3221 * Next, try splitting the current block in half.
3223 * If this works we have to re-set our variables because we
3224 * could be in a different block now.
3226 error = xfs_btree_split(cur, level, nptr, key, ncur, stat);
3227 if (error || *stat == 0)
3231 *index = cur->bc_ptrs[level];
3236 * Insert one record/level. Return information to the caller
3237 * allowing the next level up to proceed if necessary.
3241 struct xfs_btree_cur *cur, /* btree cursor */
3242 int level, /* level to insert record at */
3243 union xfs_btree_ptr *ptrp, /* i/o: block number inserted */
3244 union xfs_btree_rec *rec, /* record to insert */
3245 union xfs_btree_key *key, /* i/o: block key for ptrp */
3246 struct xfs_btree_cur **curp, /* output: new cursor replacing cur */
3247 int *stat) /* success/failure */
3249 struct xfs_btree_block *block; /* btree block */
3250 struct xfs_buf *bp; /* buffer for block */
3251 union xfs_btree_ptr nptr; /* new block ptr */
3252 struct xfs_btree_cur *ncur; /* new btree cursor */
3253 union xfs_btree_key nkey; /* new block key */
3254 union xfs_btree_key *lkey;
3255 int optr; /* old key/record index */
3256 int ptr; /* key/record index */
3257 int numrecs;/* number of records */
3258 int error; /* error return value */
3264 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
3265 XFS_BTREE_TRACE_ARGIPR(cur, level, *ptrp, &rec);
3271 * If we have an external root pointer, and we've made it to the
3272 * root level, allocate a new root block and we're done.
3274 if (!(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
3275 (level >= cur->bc_nlevels)) {
3276 error = xfs_btree_new_root(cur, stat);
3277 xfs_btree_set_ptr_null(cur, ptrp);
3279 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3283 /* If we're off the left edge, return failure. */
3284 ptr = cur->bc_ptrs[level];
3286 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3293 XFS_BTREE_STATS_INC(cur, insrec);
3295 /* Get pointers to the btree buffer and block. */
3296 block = xfs_btree_get_block(cur, level, &bp);
3297 old_bn = bp ? bp->b_bn : XFS_BUF_DADDR_NULL;
3298 numrecs = xfs_btree_get_numrecs(block);
3301 error = xfs_btree_check_block(cur, block, level, bp);
3305 /* Check that the new entry is being inserted in the right place. */
3306 if (ptr <= numrecs) {
3308 ASSERT(cur->bc_ops->recs_inorder(cur, rec,
3309 xfs_btree_rec_addr(cur, ptr, block)));
3311 ASSERT(cur->bc_ops->keys_inorder(cur, key,
3312 xfs_btree_key_addr(cur, ptr, block)));
3318 * If the block is full, we can't insert the new entry until we
3319 * make the block un-full.
3321 xfs_btree_set_ptr_null(cur, &nptr);
3322 if (numrecs == cur->bc_ops->get_maxrecs(cur, level)) {
3323 error = xfs_btree_make_block_unfull(cur, level, numrecs,
3324 &optr, &ptr, &nptr, &ncur, lkey, stat);
3325 if (error || *stat == 0)
3330 * The current block may have changed if the block was
3331 * previously full and we have just made space in it.
3333 block = xfs_btree_get_block(cur, level, &bp);
3334 numrecs = xfs_btree_get_numrecs(block);
3337 error = xfs_btree_check_block(cur, block, level, bp);
3343 * At this point we know there's room for our new entry in the block
3344 * we're pointing at.
3346 XFS_BTREE_STATS_ADD(cur, moves, numrecs - ptr + 1);
3349 /* It's a nonleaf. make a hole in the keys and ptrs */
3350 union xfs_btree_key *kp;
3351 union xfs_btree_ptr *pp;
3353 kp = xfs_btree_key_addr(cur, ptr, block);
3354 pp = xfs_btree_ptr_addr(cur, ptr, block);
3357 for (i = numrecs - ptr; i >= 0; i--) {
3358 error = xfs_btree_check_ptr(cur, pp, i, level);
3364 xfs_btree_shift_keys(cur, kp, 1, numrecs - ptr + 1);
3365 xfs_btree_shift_ptrs(cur, pp, 1, numrecs - ptr + 1);
3368 error = xfs_btree_check_ptr(cur, ptrp, 0, level);
3373 /* Now put the new data in, bump numrecs and log it. */
3374 xfs_btree_copy_keys(cur, kp, key, 1);
3375 xfs_btree_copy_ptrs(cur, pp, ptrp, 1);
3377 xfs_btree_set_numrecs(block, numrecs);
3378 xfs_btree_log_ptrs(cur, bp, ptr, numrecs);
3379 xfs_btree_log_keys(cur, bp, ptr, numrecs);
3381 if (ptr < numrecs) {
3382 ASSERT(cur->bc_ops->keys_inorder(cur, kp,
3383 xfs_btree_key_addr(cur, ptr + 1, block)));
3387 /* It's a leaf. make a hole in the records */
3388 union xfs_btree_rec *rp;
3390 rp = xfs_btree_rec_addr(cur, ptr, block);
3392 xfs_btree_shift_recs(cur, rp, 1, numrecs - ptr + 1);
3394 /* Now put the new data in, bump numrecs and log it. */
3395 xfs_btree_copy_recs(cur, rp, rec, 1);
3396 xfs_btree_set_numrecs(block, ++numrecs);
3397 xfs_btree_log_recs(cur, bp, ptr, numrecs);
3399 if (ptr < numrecs) {
3400 ASSERT(cur->bc_ops->recs_inorder(cur, rp,
3401 xfs_btree_rec_addr(cur, ptr + 1, block)));
3406 /* Log the new number of records in the btree header. */
3407 xfs_btree_log_block(cur, bp, XFS_BB_NUMRECS);
3410 * If we just inserted into a new tree block, we have to
3411 * recalculate nkey here because nkey is out of date.
3413 * Otherwise we're just updating an existing block (having shoved
3414 * some records into the new tree block), so use the regular key
3417 if (bp && bp->b_bn != old_bn) {
3418 xfs_btree_get_keys(cur, block, lkey);
3419 } else if (xfs_btree_needs_key_update(cur, optr)) {
3420 error = xfs_btree_update_keys(cur, level);
3426 * If we are tracking the last record in the tree and
3427 * we are at the far right edge of the tree, update it.
3429 if (xfs_btree_is_lastrec(cur, block, level)) {
3430 cur->bc_ops->update_lastrec(cur, block, rec,
3431 ptr, LASTREC_INSREC);
3435 * Return the new block number, if any.
3436 * If there is one, give back a record value and a cursor too.
3439 if (!xfs_btree_ptr_is_null(cur, &nptr)) {
3440 xfs_btree_copy_keys(cur, key, lkey, 1);
3444 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3449 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
3454 * Insert the record at the point referenced by cur.
3456 * A multi-level split of the tree on insert will invalidate the original
3457 * cursor. All callers of this function should assume that the cursor is
3458 * no longer valid and revalidate it.
3462 struct xfs_btree_cur *cur,
3465 int error; /* error return value */
3466 int i; /* result value, 0 for failure */
3467 int level; /* current level number in btree */
3468 union xfs_btree_ptr nptr; /* new block number (split result) */
3469 struct xfs_btree_cur *ncur; /* new cursor (split result) */
3470 struct xfs_btree_cur *pcur; /* previous level's cursor */
3471 union xfs_btree_key bkey; /* key of block to insert */
3472 union xfs_btree_key *key;
3473 union xfs_btree_rec rec; /* record to insert */
3480 xfs_btree_set_ptr_null(cur, &nptr);
3482 /* Make a key out of the record data to be inserted, and save it. */
3483 cur->bc_ops->init_rec_from_cur(cur, &rec);
3484 cur->bc_ops->init_key_from_rec(key, &rec);
3487 * Loop going up the tree, starting at the leaf level.
3488 * Stop when we don't get a split block, that must mean that
3489 * the insert is finished with this level.
3493 * Insert nrec/nptr into this level of the tree.
3494 * Note if we fail, nptr will be null.
3496 error = xfs_btree_insrec(pcur, level, &nptr, &rec, key,
3500 xfs_btree_del_cursor(pcur, XFS_BTREE_ERROR);
3504 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, i == 1, error0);
3508 * See if the cursor we just used is trash.
3509 * Can't trash the caller's cursor, but otherwise we should
3510 * if ncur is a new cursor or we're about to be done.
3513 (ncur || xfs_btree_ptr_is_null(cur, &nptr))) {
3514 /* Save the state from the cursor before we trash it */
3515 if (cur->bc_ops->update_cursor)
3516 cur->bc_ops->update_cursor(pcur, cur);
3517 cur->bc_nlevels = pcur->bc_nlevels;
3518 xfs_btree_del_cursor(pcur, XFS_BTREE_NOERROR);
3520 /* If we got a new cursor, switch to it. */
3525 } while (!xfs_btree_ptr_is_null(cur, &nptr));
3527 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3531 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
3536 * Try to merge a non-leaf block back into the inode root.
3538 * Note: the killroot names comes from the fact that we're effectively
3539 * killing the old root block. But because we can't just delete the
3540 * inode we have to copy the single block it was pointing to into the
3544 xfs_btree_kill_iroot(
3545 struct xfs_btree_cur *cur)
3547 int whichfork = cur->bc_private.b.whichfork;
3548 struct xfs_inode *ip = cur->bc_private.b.ip;
3549 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
3550 struct xfs_btree_block *block;
3551 struct xfs_btree_block *cblock;
3552 union xfs_btree_key *kp;
3553 union xfs_btree_key *ckp;
3554 union xfs_btree_ptr *pp;
3555 union xfs_btree_ptr *cpp;
3556 struct xfs_buf *cbp;
3562 union xfs_btree_ptr ptr;
3566 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
3568 ASSERT(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE);
3569 ASSERT(cur->bc_nlevels > 1);
3572 * Don't deal with the root block needs to be a leaf case.
3573 * We're just going to turn the thing back into extents anyway.
3575 level = cur->bc_nlevels - 1;
3580 * Give up if the root has multiple children.
3582 block = xfs_btree_get_iroot(cur);
3583 if (xfs_btree_get_numrecs(block) != 1)
3586 cblock = xfs_btree_get_block(cur, level - 1, &cbp);
3587 numrecs = xfs_btree_get_numrecs(cblock);
3590 * Only do this if the next level will fit.
3591 * Then the data must be copied up to the inode,
3592 * instead of freeing the root you free the next level.
3594 if (numrecs > cur->bc_ops->get_dmaxrecs(cur, level))
3597 XFS_BTREE_STATS_INC(cur, killroot);
3600 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_LEFTSIB);
3601 ASSERT(xfs_btree_ptr_is_null(cur, &ptr));
3602 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_RIGHTSIB);
3603 ASSERT(xfs_btree_ptr_is_null(cur, &ptr));
3606 index = numrecs - cur->bc_ops->get_maxrecs(cur, level);
3608 xfs_iroot_realloc(cur->bc_private.b.ip, index,
3609 cur->bc_private.b.whichfork);
3610 block = ifp->if_broot;
3613 be16_add_cpu(&block->bb_numrecs, index);
3614 ASSERT(block->bb_numrecs == cblock->bb_numrecs);
3616 kp = xfs_btree_key_addr(cur, 1, block);
3617 ckp = xfs_btree_key_addr(cur, 1, cblock);
3618 xfs_btree_copy_keys(cur, kp, ckp, numrecs);
3620 pp = xfs_btree_ptr_addr(cur, 1, block);
3621 cpp = xfs_btree_ptr_addr(cur, 1, cblock);
3623 for (i = 0; i < numrecs; i++) {
3624 error = xfs_btree_check_ptr(cur, cpp, i, level - 1);
3626 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
3631 xfs_btree_copy_ptrs(cur, pp, cpp, numrecs);
3633 error = xfs_btree_free_block(cur, cbp);
3635 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
3639 cur->bc_bufs[level - 1] = NULL;
3640 be16_add_cpu(&block->bb_level, -1);
3641 xfs_trans_log_inode(cur->bc_tp, ip,
3642 XFS_ILOG_CORE | xfs_ilog_fbroot(cur->bc_private.b.whichfork));
3645 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3650 * Kill the current root node, and replace it with it's only child node.
3653 xfs_btree_kill_root(
3654 struct xfs_btree_cur *cur,
3657 union xfs_btree_ptr *newroot)
3661 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
3662 XFS_BTREE_STATS_INC(cur, killroot);
3665 * Update the root pointer, decreasing the level by 1 and then
3666 * free the old root.
3668 cur->bc_ops->set_root(cur, newroot, -1);
3670 error = xfs_btree_free_block(cur, bp);
3672 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
3676 cur->bc_bufs[level] = NULL;
3677 cur->bc_ra[level] = 0;
3680 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3685 xfs_btree_dec_cursor(
3686 struct xfs_btree_cur *cur,
3694 error = xfs_btree_decrement(cur, level, &i);
3699 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3705 * Single level of the btree record deletion routine.
3706 * Delete record pointed to by cur/level.
3707 * Remove the record from its block then rebalance the tree.
3708 * Return 0 for error, 1 for done, 2 to go on to the next level.
3710 STATIC int /* error */
3712 struct xfs_btree_cur *cur, /* btree cursor */
3713 int level, /* level removing record from */
3714 int *stat) /* fail/done/go-on */
3716 struct xfs_btree_block *block; /* btree block */
3717 union xfs_btree_ptr cptr; /* current block ptr */
3718 struct xfs_buf *bp; /* buffer for block */
3719 int error; /* error return value */
3720 int i; /* loop counter */
3721 union xfs_btree_ptr lptr; /* left sibling block ptr */
3722 struct xfs_buf *lbp; /* left buffer pointer */
3723 struct xfs_btree_block *left; /* left btree block */
3724 int lrecs = 0; /* left record count */
3725 int ptr; /* key/record index */
3726 union xfs_btree_ptr rptr; /* right sibling block ptr */
3727 struct xfs_buf *rbp; /* right buffer pointer */
3728 struct xfs_btree_block *right; /* right btree block */
3729 struct xfs_btree_block *rrblock; /* right-right btree block */
3730 struct xfs_buf *rrbp; /* right-right buffer pointer */
3731 int rrecs = 0; /* right record count */
3732 struct xfs_btree_cur *tcur; /* temporary btree cursor */
3733 int numrecs; /* temporary numrec count */
3735 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
3736 XFS_BTREE_TRACE_ARGI(cur, level);
3740 /* Get the index of the entry being deleted, check for nothing there. */
3741 ptr = cur->bc_ptrs[level];
3743 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3748 /* Get the buffer & block containing the record or key/ptr. */
3749 block = xfs_btree_get_block(cur, level, &bp);
3750 numrecs = xfs_btree_get_numrecs(block);
3753 error = xfs_btree_check_block(cur, block, level, bp);
3758 /* Fail if we're off the end of the block. */
3759 if (ptr > numrecs) {
3760 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3765 XFS_BTREE_STATS_INC(cur, delrec);
3766 XFS_BTREE_STATS_ADD(cur, moves, numrecs - ptr);
3768 /* Excise the entries being deleted. */
3770 /* It's a nonleaf. operate on keys and ptrs */
3771 union xfs_btree_key *lkp;
3772 union xfs_btree_ptr *lpp;
3774 lkp = xfs_btree_key_addr(cur, ptr + 1, block);
3775 lpp = xfs_btree_ptr_addr(cur, ptr + 1, block);
3778 for (i = 0; i < numrecs - ptr; i++) {
3779 error = xfs_btree_check_ptr(cur, lpp, i, level);
3785 if (ptr < numrecs) {
3786 xfs_btree_shift_keys(cur, lkp, -1, numrecs - ptr);
3787 xfs_btree_shift_ptrs(cur, lpp, -1, numrecs - ptr);
3788 xfs_btree_log_keys(cur, bp, ptr, numrecs - 1);
3789 xfs_btree_log_ptrs(cur, bp, ptr, numrecs - 1);
3792 /* It's a leaf. operate on records */
3793 if (ptr < numrecs) {
3794 xfs_btree_shift_recs(cur,
3795 xfs_btree_rec_addr(cur, ptr + 1, block),
3797 xfs_btree_log_recs(cur, bp, ptr, numrecs - 1);
3802 * Decrement and log the number of entries in the block.
3804 xfs_btree_set_numrecs(block, --numrecs);
3805 xfs_btree_log_block(cur, bp, XFS_BB_NUMRECS);
3808 * If we are tracking the last record in the tree and
3809 * we are at the far right edge of the tree, update it.
3811 if (xfs_btree_is_lastrec(cur, block, level)) {
3812 cur->bc_ops->update_lastrec(cur, block, NULL,
3813 ptr, LASTREC_DELREC);
3817 * We're at the root level. First, shrink the root block in-memory.
3818 * Try to get rid of the next level down. If we can't then there's
3819 * nothing left to do.
3821 if (level == cur->bc_nlevels - 1) {
3822 if (cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) {
3823 xfs_iroot_realloc(cur->bc_private.b.ip, -1,
3824 cur->bc_private.b.whichfork);
3826 error = xfs_btree_kill_iroot(cur);
3830 error = xfs_btree_dec_cursor(cur, level, stat);
3838 * If this is the root level, and there's only one entry left,
3839 * and it's NOT the leaf level, then we can get rid of this
3842 if (numrecs == 1 && level > 0) {
3843 union xfs_btree_ptr *pp;
3845 * pp is still set to the first pointer in the block.
3846 * Make it the new root of the btree.
3848 pp = xfs_btree_ptr_addr(cur, 1, block);
3849 error = xfs_btree_kill_root(cur, bp, level, pp);
3852 } else if (level > 0) {
3853 error = xfs_btree_dec_cursor(cur, level, stat);
3862 * If we deleted the leftmost entry in the block, update the
3863 * key values above us in the tree.
3865 if (xfs_btree_needs_key_update(cur, ptr)) {
3866 error = xfs_btree_update_keys(cur, level);
3872 * If the number of records remaining in the block is at least
3873 * the minimum, we're done.
3875 if (numrecs >= cur->bc_ops->get_minrecs(cur, level)) {
3876 error = xfs_btree_dec_cursor(cur, level, stat);
3883 * Otherwise, we have to move some records around to keep the
3884 * tree balanced. Look at the left and right sibling blocks to
3885 * see if we can re-balance by moving only one record.
3887 xfs_btree_get_sibling(cur, block, &rptr, XFS_BB_RIGHTSIB);
3888 xfs_btree_get_sibling(cur, block, &lptr, XFS_BB_LEFTSIB);
3890 if (cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) {
3892 * One child of root, need to get a chance to copy its contents
3893 * into the root and delete it. Can't go up to next level,
3894 * there's nothing to delete there.
3896 if (xfs_btree_ptr_is_null(cur, &rptr) &&
3897 xfs_btree_ptr_is_null(cur, &lptr) &&
3898 level == cur->bc_nlevels - 2) {
3899 error = xfs_btree_kill_iroot(cur);
3901 error = xfs_btree_dec_cursor(cur, level, stat);
3908 ASSERT(!xfs_btree_ptr_is_null(cur, &rptr) ||
3909 !xfs_btree_ptr_is_null(cur, &lptr));
3912 * Duplicate the cursor so our btree manipulations here won't
3913 * disrupt the next level up.
3915 error = xfs_btree_dup_cursor(cur, &tcur);
3920 * If there's a right sibling, see if it's ok to shift an entry
3923 if (!xfs_btree_ptr_is_null(cur, &rptr)) {
3925 * Move the temp cursor to the last entry in the next block.
3926 * Actually any entry but the first would suffice.
3928 i = xfs_btree_lastrec(tcur, level);
3929 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, i == 1, error0);
3931 error = xfs_btree_increment(tcur, level, &i);
3934 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, i == 1, error0);
3936 i = xfs_btree_lastrec(tcur, level);
3937 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, i == 1, error0);
3939 /* Grab a pointer to the block. */
3940 right = xfs_btree_get_block(tcur, level, &rbp);
3942 error = xfs_btree_check_block(tcur, right, level, rbp);
3946 /* Grab the current block number, for future use. */
3947 xfs_btree_get_sibling(tcur, right, &cptr, XFS_BB_LEFTSIB);
3950 * If right block is full enough so that removing one entry
3951 * won't make it too empty, and left-shifting an entry out
3952 * of right to us works, we're done.
3954 if (xfs_btree_get_numrecs(right) - 1 >=
3955 cur->bc_ops->get_minrecs(tcur, level)) {
3956 error = xfs_btree_lshift(tcur, level, &i);
3960 ASSERT(xfs_btree_get_numrecs(block) >=
3961 cur->bc_ops->get_minrecs(tcur, level));
3963 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
3966 error = xfs_btree_dec_cursor(cur, level, stat);
3974 * Otherwise, grab the number of records in right for
3975 * future reference, and fix up the temp cursor to point
3976 * to our block again (last record).
3978 rrecs = xfs_btree_get_numrecs(right);
3979 if (!xfs_btree_ptr_is_null(cur, &lptr)) {
3980 i = xfs_btree_firstrec(tcur, level);
3981 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, i == 1, error0);
3983 error = xfs_btree_decrement(tcur, level, &i);
3986 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, i == 1, error0);
3991 * If there's a left sibling, see if it's ok to shift an entry
3994 if (!xfs_btree_ptr_is_null(cur, &lptr)) {
3996 * Move the temp cursor to the first entry in the
3999 i = xfs_btree_firstrec(tcur, level);
4000 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, i == 1, error0);
4002 error = xfs_btree_decrement(tcur, level, &i);
4005 i = xfs_btree_firstrec(tcur, level);
4006 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, i == 1, error0);
4008 /* Grab a pointer to the block. */
4009 left = xfs_btree_get_block(tcur, level, &lbp);
4011 error = xfs_btree_check_block(cur, left, level, lbp);
4015 /* Grab the current block number, for future use. */
4016 xfs_btree_get_sibling(tcur, left, &cptr, XFS_BB_RIGHTSIB);
4019 * If left block is full enough so that removing one entry
4020 * won't make it too empty, and right-shifting an entry out
4021 * of left to us works, we're done.
4023 if (xfs_btree_get_numrecs(left) - 1 >=
4024 cur->bc_ops->get_minrecs(tcur, level)) {
4025 error = xfs_btree_rshift(tcur, level, &i);
4029 ASSERT(xfs_btree_get_numrecs(block) >=
4030 cur->bc_ops->get_minrecs(tcur, level));
4031 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
4035 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
4042 * Otherwise, grab the number of records in right for
4045 lrecs = xfs_btree_get_numrecs(left);
4048 /* Delete the temp cursor, we're done with it. */
4049 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
4052 /* If here, we need to do a join to keep the tree balanced. */
4053 ASSERT(!xfs_btree_ptr_is_null(cur, &cptr));
4055 if (!xfs_btree_ptr_is_null(cur, &lptr) &&
4056 lrecs + xfs_btree_get_numrecs(block) <=
4057 cur->bc_ops->get_maxrecs(cur, level)) {
4059 * Set "right" to be the starting block,
4060 * "left" to be the left neighbor.
4065 error = xfs_btree_read_buf_block(cur, &lptr, 0, &left, &lbp);
4070 * If that won't work, see if we can join with the right neighbor block.
4072 } else if (!xfs_btree_ptr_is_null(cur, &rptr) &&
4073 rrecs + xfs_btree_get_numrecs(block) <=
4074 cur->bc_ops->get_maxrecs(cur, level)) {
4076 * Set "left" to be the starting block,
4077 * "right" to be the right neighbor.
4082 error = xfs_btree_read_buf_block(cur, &rptr, 0, &right, &rbp);
4087 * Otherwise, we can't fix the imbalance.
4088 * Just return. This is probably a logic error, but it's not fatal.
4091 error = xfs_btree_dec_cursor(cur, level, stat);
4097 rrecs = xfs_btree_get_numrecs(right);
4098 lrecs = xfs_btree_get_numrecs(left);
4101 * We're now going to join "left" and "right" by moving all the stuff
4102 * in "right" to "left" and deleting "right".
4104 XFS_BTREE_STATS_ADD(cur, moves, rrecs);
4106 /* It's a non-leaf. Move keys and pointers. */
4107 union xfs_btree_key *lkp; /* left btree key */
4108 union xfs_btree_ptr *lpp; /* left address pointer */
4109 union xfs_btree_key *rkp; /* right btree key */
4110 union xfs_btree_ptr *rpp; /* right address pointer */
4112 lkp = xfs_btree_key_addr(cur, lrecs + 1, left);
4113 lpp = xfs_btree_ptr_addr(cur, lrecs + 1, left);
4114 rkp = xfs_btree_key_addr(cur, 1, right);
4115 rpp = xfs_btree_ptr_addr(cur, 1, right);
4117 for (i = 1; i < rrecs; i++) {
4118 error = xfs_btree_check_ptr(cur, rpp, i, level);
4123 xfs_btree_copy_keys(cur, lkp, rkp, rrecs);
4124 xfs_btree_copy_ptrs(cur, lpp, rpp, rrecs);
4126 xfs_btree_log_keys(cur, lbp, lrecs + 1, lrecs + rrecs);
4127 xfs_btree_log_ptrs(cur, lbp, lrecs + 1, lrecs + rrecs);
4129 /* It's a leaf. Move records. */
4130 union xfs_btree_rec *lrp; /* left record pointer */
4131 union xfs_btree_rec *rrp; /* right record pointer */
4133 lrp = xfs_btree_rec_addr(cur, lrecs + 1, left);
4134 rrp = xfs_btree_rec_addr(cur, 1, right);
4136 xfs_btree_copy_recs(cur, lrp, rrp, rrecs);
4137 xfs_btree_log_recs(cur, lbp, lrecs + 1, lrecs + rrecs);
4140 XFS_BTREE_STATS_INC(cur, join);
4143 * Fix up the number of records and right block pointer in the
4144 * surviving block, and log it.
4146 xfs_btree_set_numrecs(left, lrecs + rrecs);
4147 xfs_btree_get_sibling(cur, right, &cptr, XFS_BB_RIGHTSIB),
4148 xfs_btree_set_sibling(cur, left, &cptr, XFS_BB_RIGHTSIB);
4149 xfs_btree_log_block(cur, lbp, XFS_BB_NUMRECS | XFS_BB_RIGHTSIB);
4151 /* If there is a right sibling, point it to the remaining block. */
4152 xfs_btree_get_sibling(cur, left, &cptr, XFS_BB_RIGHTSIB);
4153 if (!xfs_btree_ptr_is_null(cur, &cptr)) {
4154 error = xfs_btree_read_buf_block(cur, &cptr, 0, &rrblock, &rrbp);
4157 xfs_btree_set_sibling(cur, rrblock, &lptr, XFS_BB_LEFTSIB);
4158 xfs_btree_log_block(cur, rrbp, XFS_BB_LEFTSIB);
4161 /* Free the deleted block. */
4162 error = xfs_btree_free_block(cur, rbp);
4167 * If we joined with the left neighbor, set the buffer in the
4168 * cursor to the left block, and fix up the index.
4171 cur->bc_bufs[level] = lbp;
4172 cur->bc_ptrs[level] += lrecs;
4173 cur->bc_ra[level] = 0;
4176 * If we joined with the right neighbor and there's a level above
4177 * us, increment the cursor at that level.
4179 else if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) ||
4180 (level + 1 < cur->bc_nlevels)) {
4181 error = xfs_btree_increment(cur, level + 1, &i);
4187 * Readjust the ptr at this level if it's not a leaf, since it's
4188 * still pointing at the deletion point, which makes the cursor
4189 * inconsistent. If this makes the ptr 0, the caller fixes it up.
4190 * We can't use decrement because it would change the next level up.
4193 cur->bc_ptrs[level]--;
4196 * We combined blocks, so we have to update the parent keys if the
4197 * btree supports overlapped intervals. However, bc_ptrs[level + 1]
4198 * points to the old block so that the caller knows which record to
4199 * delete. Therefore, the caller must be savvy enough to call updkeys
4200 * for us if we return stat == 2. The other exit points from this
4201 * function don't require deletions further up the tree, so they can
4202 * call updkeys directly.
4205 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
4206 /* Return value means the next level up has something to do. */
4211 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
4213 xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
4218 * Delete the record pointed to by cur.
4219 * The cursor refers to the place where the record was (could be inserted)
4220 * when the operation returns.
4224 struct xfs_btree_cur *cur,
4225 int *stat) /* success/failure */
4227 int error; /* error return value */
4230 bool joined = false;
4232 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
4235 * Go up the tree, starting at leaf level.
4237 * If 2 is returned then a join was done; go to the next level.
4238 * Otherwise we are done.
4240 for (level = 0, i = 2; i == 2; level++) {
4241 error = xfs_btree_delrec(cur, level, &i);
4249 * If we combined blocks as part of deleting the record, delrec won't
4250 * have updated the parent high keys so we have to do that here.
4252 if (joined && (cur->bc_flags & XFS_BTREE_OVERLAPPING)) {
4253 error = xfs_btree_updkeys_force(cur, 0);
4259 for (level = 1; level < cur->bc_nlevels; level++) {
4260 if (cur->bc_ptrs[level] == 0) {
4261 error = xfs_btree_decrement(cur, level, &i);
4269 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
4273 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
4278 * Get the data from the pointed-to record.
4282 struct xfs_btree_cur *cur, /* btree cursor */
4283 union xfs_btree_rec **recp, /* output: btree record */
4284 int *stat) /* output: success/failure */
4286 struct xfs_btree_block *block; /* btree block */
4287 struct xfs_buf *bp; /* buffer pointer */
4288 int ptr; /* record number */
4290 int error; /* error return value */
4293 ptr = cur->bc_ptrs[0];
4294 block = xfs_btree_get_block(cur, 0, &bp);
4297 error = xfs_btree_check_block(cur, block, 0, bp);
4303 * Off the right end or left end, return failure.
4305 if (ptr > xfs_btree_get_numrecs(block) || ptr <= 0) {
4311 * Point to the record and extract its data.
4313 *recp = xfs_btree_rec_addr(cur, ptr, block);
4318 /* Visit a block in a btree. */
4320 xfs_btree_visit_block(
4321 struct xfs_btree_cur *cur,
4323 xfs_btree_visit_blocks_fn fn,
4326 struct xfs_btree_block *block;
4328 union xfs_btree_ptr rptr;
4331 /* do right sibling readahead */
4332 xfs_btree_readahead(cur, level, XFS_BTCUR_RIGHTRA);
4333 block = xfs_btree_get_block(cur, level, &bp);
4335 /* process the block */
4336 error = fn(cur, level, data);
4340 /* now read rh sibling block for next iteration */
4341 xfs_btree_get_sibling(cur, block, &rptr, XFS_BB_RIGHTSIB);
4342 if (xfs_btree_ptr_is_null(cur, &rptr))
4345 return xfs_btree_lookup_get_block(cur, level, &rptr, &block);
4349 /* Visit every block in a btree. */
4351 xfs_btree_visit_blocks(
4352 struct xfs_btree_cur *cur,
4353 xfs_btree_visit_blocks_fn fn,
4356 union xfs_btree_ptr lptr;
4358 struct xfs_btree_block *block = NULL;
4361 cur->bc_ops->init_ptr_from_cur(cur, &lptr);
4363 /* for each level */
4364 for (level = cur->bc_nlevels - 1; level >= 0; level--) {
4365 /* grab the left hand block */
4366 error = xfs_btree_lookup_get_block(cur, level, &lptr, &block);
4370 /* readahead the left most block for the next level down */
4372 union xfs_btree_ptr *ptr;
4374 ptr = xfs_btree_ptr_addr(cur, 1, block);
4375 xfs_btree_readahead_ptr(cur, ptr, 1);
4377 /* save for the next iteration of the loop */
4381 /* for each buffer in the level */
4383 error = xfs_btree_visit_block(cur, level, fn, data);
4386 if (error != -ENOENT)
4394 * Change the owner of a btree.
4396 * The mechanism we use here is ordered buffer logging. Because we don't know
4397 * how many buffers were are going to need to modify, we don't really want to
4398 * have to make transaction reservations for the worst case of every buffer in a
4399 * full size btree as that may be more space that we can fit in the log....
4401 * We do the btree walk in the most optimal manner possible - we have sibling
4402 * pointers so we can just walk all the blocks on each level from left to right
4403 * in a single pass, and then move to the next level and do the same. We can
4404 * also do readahead on the sibling pointers to get IO moving more quickly,
4405 * though for slow disks this is unlikely to make much difference to performance
4406 * as the amount of CPU work we have to do before moving to the next block is
4409 * For each btree block that we load, modify the owner appropriately, set the
4410 * buffer as an ordered buffer and log it appropriately. We need to ensure that
4411 * we mark the region we change dirty so that if the buffer is relogged in
4412 * a subsequent transaction the changes we make here as an ordered buffer are
4413 * correctly relogged in that transaction. If we are in recovery context, then
4414 * just queue the modified buffer as delayed write buffer so the transaction
4415 * recovery completion writes the changes to disk.
4417 struct xfs_btree_block_change_owner_info {
4418 __uint64_t new_owner;
4419 struct list_head *buffer_list;
4423 xfs_btree_block_change_owner(
4424 struct xfs_btree_cur *cur,
4428 struct xfs_btree_block_change_owner_info *bbcoi = data;
4429 struct xfs_btree_block *block;
4432 /* modify the owner */
4433 block = xfs_btree_get_block(cur, level, &bp);
4434 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
4435 block->bb_u.l.bb_owner = cpu_to_be64(bbcoi->new_owner);
4437 block->bb_u.s.bb_owner = cpu_to_be32(bbcoi->new_owner);
4440 * If the block is a root block hosted in an inode, we might not have a
4441 * buffer pointer here and we shouldn't attempt to log the change as the
4442 * information is already held in the inode and discarded when the root
4443 * block is formatted into the on-disk inode fork. We still change it,
4444 * though, so everything is consistent in memory.
4448 xfs_trans_ordered_buf(cur->bc_tp, bp);
4449 xfs_btree_log_block(cur, bp, XFS_BB_OWNER);
4451 xfs_buf_delwri_queue(bp, bbcoi->buffer_list);
4454 ASSERT(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE);
4455 ASSERT(level == cur->bc_nlevels - 1);
4462 xfs_btree_change_owner(
4463 struct xfs_btree_cur *cur,
4464 __uint64_t new_owner,
4465 struct list_head *buffer_list)
4467 struct xfs_btree_block_change_owner_info bbcoi;
4469 bbcoi.new_owner = new_owner;
4470 bbcoi.buffer_list = buffer_list;
4472 return xfs_btree_visit_blocks(cur, xfs_btree_block_change_owner,
4477 * xfs_btree_sblock_v5hdr_verify() -- verify the v5 fields of a short-format
4480 * @bp: buffer containing the btree block
4481 * @max_recs: pointer to the m_*_mxr max records field in the xfs mount
4482 * @pag_max_level: pointer to the per-ag max level field
4485 xfs_btree_sblock_v5hdr_verify(
4488 struct xfs_mount *mp = bp->b_target->bt_mount;
4489 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
4490 struct xfs_perag *pag = bp->b_pag;
4492 if (!xfs_sb_version_hascrc(&mp->m_sb))
4494 if (!uuid_equal(&block->bb_u.s.bb_uuid, &mp->m_sb.sb_meta_uuid))
4496 if (block->bb_u.s.bb_blkno != cpu_to_be64(bp->b_bn))
4498 if (pag && be32_to_cpu(block->bb_u.s.bb_owner) != pag->pag_agno)
4504 * xfs_btree_sblock_verify() -- verify a short-format btree block
4506 * @bp: buffer containing the btree block
4507 * @max_recs: maximum records allowed in this btree node
4510 xfs_btree_sblock_verify(
4512 unsigned int max_recs)
4514 struct xfs_mount *mp = bp->b_target->bt_mount;
4515 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
4517 /* numrecs verification */
4518 if (be16_to_cpu(block->bb_numrecs) > max_recs)
4521 /* sibling pointer verification */
4522 if (!block->bb_u.s.bb_leftsib ||
4523 (be32_to_cpu(block->bb_u.s.bb_leftsib) >= mp->m_sb.sb_agblocks &&
4524 block->bb_u.s.bb_leftsib != cpu_to_be32(NULLAGBLOCK)))
4526 if (!block->bb_u.s.bb_rightsib ||
4527 (be32_to_cpu(block->bb_u.s.bb_rightsib) >= mp->m_sb.sb_agblocks &&
4528 block->bb_u.s.bb_rightsib != cpu_to_be32(NULLAGBLOCK)))
4535 * Calculate the number of btree levels needed to store a given number of
4536 * records in a short-format btree.
4539 xfs_btree_compute_maxlevels(
4540 struct xfs_mount *mp,
4545 unsigned long maxblocks;
4547 maxblocks = (len + limits[0] - 1) / limits[0];
4548 for (level = 1; maxblocks > 1; level++)
4549 maxblocks = (maxblocks + limits[1] - 1) / limits[1];
4554 * Query a regular btree for all records overlapping a given interval.
4555 * Start with a LE lookup of the key of low_rec and return all records
4556 * until we find a record with a key greater than the key of high_rec.
4559 xfs_btree_simple_query_range(
4560 struct xfs_btree_cur *cur,
4561 union xfs_btree_key *low_key,
4562 union xfs_btree_key *high_key,
4563 xfs_btree_query_range_fn fn,
4566 union xfs_btree_rec *recp;
4567 union xfs_btree_key rec_key;
4570 bool firstrec = true;
4573 ASSERT(cur->bc_ops->init_high_key_from_rec);
4574 ASSERT(cur->bc_ops->diff_two_keys);
4577 * Find the leftmost record. The btree cursor must be set
4578 * to the low record used to generate low_key.
4581 error = xfs_btree_lookup(cur, XFS_LOOKUP_LE, &stat);
4585 /* Nothing? See if there's anything to the right. */
4587 error = xfs_btree_increment(cur, 0, &stat);
4593 /* Find the record. */
4594 error = xfs_btree_get_rec(cur, &recp, &stat);
4598 /* Skip if high_key(rec) < low_key. */
4600 cur->bc_ops->init_high_key_from_rec(&rec_key, recp);
4602 diff = cur->bc_ops->diff_two_keys(cur, low_key,
4608 /* Stop if high_key < low_key(rec). */
4609 cur->bc_ops->init_key_from_rec(&rec_key, recp);
4610 diff = cur->bc_ops->diff_two_keys(cur, &rec_key, high_key);
4615 error = fn(cur, recp, priv);
4616 if (error < 0 || error == XFS_BTREE_QUERY_RANGE_ABORT)
4620 /* Move on to the next record. */
4621 error = xfs_btree_increment(cur, 0, &stat);
4631 * Query an overlapped interval btree for all records overlapping a given
4632 * interval. This function roughly follows the algorithm given in
4633 * "Interval Trees" of _Introduction to Algorithms_, which is section
4634 * 14.3 in the 2nd and 3rd editions.
4636 * First, generate keys for the low and high records passed in.
4638 * For any leaf node, generate the high and low keys for the record.
4639 * If the record keys overlap with the query low/high keys, pass the
4640 * record to the function iterator.
4642 * For any internal node, compare the low and high keys of each
4643 * pointer against the query low/high keys. If there's an overlap,
4644 * follow the pointer.
4646 * As an optimization, we stop scanning a block when we find a low key
4647 * that is greater than the query's high key.
4650 xfs_btree_overlapped_query_range(
4651 struct xfs_btree_cur *cur,
4652 union xfs_btree_key *low_key,
4653 union xfs_btree_key *high_key,
4654 xfs_btree_query_range_fn fn,
4657 union xfs_btree_ptr ptr;
4658 union xfs_btree_ptr *pp;
4659 union xfs_btree_key rec_key;
4660 union xfs_btree_key rec_hkey;
4661 union xfs_btree_key *lkp;
4662 union xfs_btree_key *hkp;
4663 union xfs_btree_rec *recp;
4664 struct xfs_btree_block *block;
4672 /* Load the root of the btree. */
4673 level = cur->bc_nlevels - 1;
4674 cur->bc_ops->init_ptr_from_cur(cur, &ptr);
4675 error = xfs_btree_lookup_get_block(cur, level, &ptr, &block);
4678 xfs_btree_get_block(cur, level, &bp);
4679 trace_xfs_btree_overlapped_query_range(cur, level, bp);
4681 error = xfs_btree_check_block(cur, block, level, bp);
4685 cur->bc_ptrs[level] = 1;
4687 while (level < cur->bc_nlevels) {
4688 block = xfs_btree_get_block(cur, level, &bp);
4690 /* End of node, pop back towards the root. */
4691 if (cur->bc_ptrs[level] > be16_to_cpu(block->bb_numrecs)) {
4693 if (level < cur->bc_nlevels - 1)
4694 cur->bc_ptrs[level + 1]++;
4700 /* Handle a leaf node. */
4701 recp = xfs_btree_rec_addr(cur, cur->bc_ptrs[0], block);
4703 cur->bc_ops->init_high_key_from_rec(&rec_hkey, recp);
4704 ldiff = cur->bc_ops->diff_two_keys(cur, &rec_hkey,
4707 cur->bc_ops->init_key_from_rec(&rec_key, recp);
4708 hdiff = cur->bc_ops->diff_two_keys(cur, high_key,
4712 * If (record's high key >= query's low key) and
4713 * (query's high key >= record's low key), then
4714 * this record overlaps the query range; callback.
4716 if (ldiff >= 0 && hdiff >= 0) {
4717 error = fn(cur, recp, priv);
4719 error == XFS_BTREE_QUERY_RANGE_ABORT)
4721 } else if (hdiff < 0) {
4722 /* Record is larger than high key; pop. */
4725 cur->bc_ptrs[level]++;
4729 /* Handle an internal node. */
4730 lkp = xfs_btree_key_addr(cur, cur->bc_ptrs[level], block);
4731 hkp = xfs_btree_high_key_addr(cur, cur->bc_ptrs[level], block);
4732 pp = xfs_btree_ptr_addr(cur, cur->bc_ptrs[level], block);
4734 ldiff = cur->bc_ops->diff_two_keys(cur, hkp, low_key);
4735 hdiff = cur->bc_ops->diff_two_keys(cur, high_key, lkp);
4738 * If (pointer's high key >= query's low key) and
4739 * (query's high key >= pointer's low key), then
4740 * this record overlaps the query range; follow pointer.
4742 if (ldiff >= 0 && hdiff >= 0) {
4744 error = xfs_btree_lookup_get_block(cur, level, pp,
4748 xfs_btree_get_block(cur, level, &bp);
4749 trace_xfs_btree_overlapped_query_range(cur, level, bp);
4751 error = xfs_btree_check_block(cur, block, level, bp);
4755 cur->bc_ptrs[level] = 1;
4757 } else if (hdiff < 0) {
4758 /* The low key is larger than the upper range; pop. */
4761 cur->bc_ptrs[level]++;
4766 * If we don't end this function with the cursor pointing at a record
4767 * block, a subsequent non-error cursor deletion will not release
4768 * node-level buffers, causing a buffer leak. This is quite possible
4769 * with a zero-results range query, so release the buffers if we
4770 * failed to return any results.
4772 if (cur->bc_bufs[0] == NULL) {
4773 for (i = 0; i < cur->bc_nlevels; i++) {
4774 if (cur->bc_bufs[i]) {
4775 xfs_trans_brelse(cur->bc_tp, cur->bc_bufs[i]);
4776 cur->bc_bufs[i] = NULL;
4777 cur->bc_ptrs[i] = 0;
4787 * Query a btree for all records overlapping a given interval of keys. The
4788 * supplied function will be called with each record found; return one of the
4789 * XFS_BTREE_QUERY_RANGE_{CONTINUE,ABORT} values or the usual negative error
4790 * code. This function returns XFS_BTREE_QUERY_RANGE_ABORT, zero, or a
4791 * negative error code.
4794 xfs_btree_query_range(
4795 struct xfs_btree_cur *cur,
4796 union xfs_btree_irec *low_rec,
4797 union xfs_btree_irec *high_rec,
4798 xfs_btree_query_range_fn fn,
4801 union xfs_btree_rec rec;
4802 union xfs_btree_key low_key;
4803 union xfs_btree_key high_key;
4805 /* Find the keys of both ends of the interval. */
4806 cur->bc_rec = *high_rec;
4807 cur->bc_ops->init_rec_from_cur(cur, &rec);
4808 cur->bc_ops->init_key_from_rec(&high_key, &rec);
4810 cur->bc_rec = *low_rec;
4811 cur->bc_ops->init_rec_from_cur(cur, &rec);
4812 cur->bc_ops->init_key_from_rec(&low_key, &rec);
4814 /* Enforce low key < high key. */
4815 if (cur->bc_ops->diff_two_keys(cur, &low_key, &high_key) > 0)
4818 if (!(cur->bc_flags & XFS_BTREE_OVERLAPPING))
4819 return xfs_btree_simple_query_range(cur, &low_key,
4820 &high_key, fn, priv);
4821 return xfs_btree_overlapped_query_range(cur, &low_key, &high_key,
4826 * Calculate the number of blocks needed to store a given number of records
4827 * in a short-format (per-AG metadata) btree.
4830 xfs_btree_calc_size(
4831 struct xfs_mount *mp,
4833 unsigned long long len)
4839 maxrecs = limits[0];
4840 for (level = 0, rval = 0; len > 1; level++) {
4842 do_div(len, maxrecs);
4843 maxrecs = limits[1];
4850 xfs_btree_count_blocks_helper(
4851 struct xfs_btree_cur *cur,
4855 xfs_extlen_t *blocks = data;
4861 /* Count the blocks in a btree and return the result in *blocks. */
4863 xfs_btree_count_blocks(
4864 struct xfs_btree_cur *cur,
4865 xfs_extlen_t *blocks)
4868 return xfs_btree_visit_blocks(cur, xfs_btree_count_blocks_helper,