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,
59 uint32_t magic = xfs_magics[crc][btnum];
61 /* Ensure we asked for crc for crc-only magics. */
66 STATIC int /* error (0 or EFSCORRUPTED) */
67 xfs_btree_check_lblock(
68 struct xfs_btree_cur *cur, /* btree cursor */
69 struct xfs_btree_block *block, /* btree long form block pointer */
70 int level, /* level of the btree block */
71 struct xfs_buf *bp) /* buffer for block, if any */
73 int lblock_ok = 1; /* block passes checks */
74 struct xfs_mount *mp; /* file system mount point */
75 xfs_btnum_t btnum = cur->bc_btnum;
79 crc = xfs_sb_version_hascrc(&mp->m_sb);
82 lblock_ok = lblock_ok &&
83 uuid_equal(&block->bb_u.l.bb_uuid,
84 &mp->m_sb.sb_meta_uuid) &&
85 block->bb_u.l.bb_blkno == cpu_to_be64(
86 bp ? bp->b_bn : XFS_BUF_DADDR_NULL);
89 lblock_ok = lblock_ok &&
90 be32_to_cpu(block->bb_magic) == xfs_btree_magic(crc, btnum) &&
91 be16_to_cpu(block->bb_level) == level &&
92 be16_to_cpu(block->bb_numrecs) <=
93 cur->bc_ops->get_maxrecs(cur, level) &&
94 block->bb_u.l.bb_leftsib &&
95 (block->bb_u.l.bb_leftsib == cpu_to_be64(NULLFSBLOCK) ||
96 XFS_FSB_SANITY_CHECK(mp,
97 be64_to_cpu(block->bb_u.l.bb_leftsib))) &&
98 block->bb_u.l.bb_rightsib &&
99 (block->bb_u.l.bb_rightsib == cpu_to_be64(NULLFSBLOCK) ||
100 XFS_FSB_SANITY_CHECK(mp,
101 be64_to_cpu(block->bb_u.l.bb_rightsib)));
103 if (unlikely(XFS_TEST_ERROR(!lblock_ok, mp,
104 XFS_ERRTAG_BTREE_CHECK_LBLOCK))) {
106 trace_xfs_btree_corrupt(bp, _RET_IP_);
107 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp);
108 return -EFSCORRUPTED;
113 STATIC int /* error (0 or EFSCORRUPTED) */
114 xfs_btree_check_sblock(
115 struct xfs_btree_cur *cur, /* btree cursor */
116 struct xfs_btree_block *block, /* btree short form block pointer */
117 int level, /* level of the btree block */
118 struct xfs_buf *bp) /* buffer containing block */
120 struct xfs_mount *mp; /* file system mount point */
121 struct xfs_buf *agbp; /* buffer for ag. freespace struct */
122 struct xfs_agf *agf; /* ag. freespace structure */
123 xfs_agblock_t agflen; /* native ag. freespace length */
124 int sblock_ok = 1; /* block passes checks */
125 xfs_btnum_t btnum = cur->bc_btnum;
129 crc = xfs_sb_version_hascrc(&mp->m_sb);
130 agbp = cur->bc_private.a.agbp;
131 agf = XFS_BUF_TO_AGF(agbp);
132 agflen = be32_to_cpu(agf->agf_length);
135 sblock_ok = sblock_ok &&
136 uuid_equal(&block->bb_u.s.bb_uuid,
137 &mp->m_sb.sb_meta_uuid) &&
138 block->bb_u.s.bb_blkno == cpu_to_be64(
139 bp ? bp->b_bn : XFS_BUF_DADDR_NULL);
142 sblock_ok = sblock_ok &&
143 be32_to_cpu(block->bb_magic) == xfs_btree_magic(crc, btnum) &&
144 be16_to_cpu(block->bb_level) == level &&
145 be16_to_cpu(block->bb_numrecs) <=
146 cur->bc_ops->get_maxrecs(cur, level) &&
147 (block->bb_u.s.bb_leftsib == cpu_to_be32(NULLAGBLOCK) ||
148 be32_to_cpu(block->bb_u.s.bb_leftsib) < agflen) &&
149 block->bb_u.s.bb_leftsib &&
150 (block->bb_u.s.bb_rightsib == cpu_to_be32(NULLAGBLOCK) ||
151 be32_to_cpu(block->bb_u.s.bb_rightsib) < agflen) &&
152 block->bb_u.s.bb_rightsib;
154 if (unlikely(XFS_TEST_ERROR(!sblock_ok, mp,
155 XFS_ERRTAG_BTREE_CHECK_SBLOCK))) {
157 trace_xfs_btree_corrupt(bp, _RET_IP_);
158 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp);
159 return -EFSCORRUPTED;
165 * Debug routine: check that block header is ok.
168 xfs_btree_check_block(
169 struct xfs_btree_cur *cur, /* btree cursor */
170 struct xfs_btree_block *block, /* generic btree block pointer */
171 int level, /* level of the btree block */
172 struct xfs_buf *bp) /* buffer containing block, if any */
174 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
175 return xfs_btree_check_lblock(cur, block, level, bp);
177 return xfs_btree_check_sblock(cur, block, level, bp);
181 * Check that (long) pointer is ok.
183 int /* error (0 or EFSCORRUPTED) */
184 xfs_btree_check_lptr(
185 struct xfs_btree_cur *cur, /* btree cursor */
186 xfs_fsblock_t bno, /* btree block disk address */
187 int level) /* btree block level */
189 XFS_WANT_CORRUPTED_RETURN(cur->bc_mp,
191 bno != NULLFSBLOCK &&
192 XFS_FSB_SANITY_CHECK(cur->bc_mp, bno));
198 * Check that (short) pointer is ok.
200 STATIC int /* error (0 or EFSCORRUPTED) */
201 xfs_btree_check_sptr(
202 struct xfs_btree_cur *cur, /* btree cursor */
203 xfs_agblock_t bno, /* btree block disk address */
204 int level) /* btree block level */
206 xfs_agblock_t agblocks = cur->bc_mp->m_sb.sb_agblocks;
208 XFS_WANT_CORRUPTED_RETURN(cur->bc_mp,
210 bno != NULLAGBLOCK &&
217 * Check that block ptr is ok.
219 STATIC int /* error (0 or EFSCORRUPTED) */
221 struct xfs_btree_cur *cur, /* btree cursor */
222 union xfs_btree_ptr *ptr, /* btree block disk address */
223 int index, /* offset from ptr to check */
224 int level) /* btree block level */
226 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
227 return xfs_btree_check_lptr(cur,
228 be64_to_cpu((&ptr->l)[index]), level);
230 return xfs_btree_check_sptr(cur,
231 be32_to_cpu((&ptr->s)[index]), level);
237 * Calculate CRC on the whole btree block and stuff it into the
238 * long-form btree header.
240 * Prior to calculting the CRC, pull the LSN out of the buffer log item and put
241 * it into the buffer so recovery knows what the last modification was that made
245 xfs_btree_lblock_calc_crc(
248 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
249 struct xfs_buf_log_item *bip = bp->b_fspriv;
251 if (!xfs_sb_version_hascrc(&bp->b_target->bt_mount->m_sb))
254 block->bb_u.l.bb_lsn = cpu_to_be64(bip->bli_item.li_lsn);
255 xfs_buf_update_cksum(bp, XFS_BTREE_LBLOCK_CRC_OFF);
259 xfs_btree_lblock_verify_crc(
262 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
263 struct xfs_mount *mp = bp->b_target->bt_mount;
265 if (xfs_sb_version_hascrc(&mp->m_sb)) {
266 if (!xfs_log_check_lsn(mp, be64_to_cpu(block->bb_u.l.bb_lsn)))
268 return xfs_buf_verify_cksum(bp, XFS_BTREE_LBLOCK_CRC_OFF);
275 * Calculate CRC on the whole btree block and stuff it into the
276 * short-form btree header.
278 * Prior to calculting the CRC, pull the LSN out of the buffer log item and put
279 * it into the buffer so recovery knows what the last modification was that made
283 xfs_btree_sblock_calc_crc(
286 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
287 struct xfs_buf_log_item *bip = bp->b_fspriv;
289 if (!xfs_sb_version_hascrc(&bp->b_target->bt_mount->m_sb))
292 block->bb_u.s.bb_lsn = cpu_to_be64(bip->bli_item.li_lsn);
293 xfs_buf_update_cksum(bp, XFS_BTREE_SBLOCK_CRC_OFF);
297 xfs_btree_sblock_verify_crc(
300 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
301 struct xfs_mount *mp = bp->b_target->bt_mount;
303 if (xfs_sb_version_hascrc(&mp->m_sb)) {
304 if (!xfs_log_check_lsn(mp, be64_to_cpu(block->bb_u.s.bb_lsn)))
306 return xfs_buf_verify_cksum(bp, XFS_BTREE_SBLOCK_CRC_OFF);
313 xfs_btree_free_block(
314 struct xfs_btree_cur *cur,
319 error = cur->bc_ops->free_block(cur, bp);
321 xfs_trans_binval(cur->bc_tp, bp);
322 XFS_BTREE_STATS_INC(cur, free);
328 * Delete the btree cursor.
331 xfs_btree_del_cursor(
332 xfs_btree_cur_t *cur, /* btree cursor */
333 int error) /* del because of error */
335 int i; /* btree level */
338 * Clear the buffer pointers, and release the buffers.
339 * If we're doing this in the face of an error, we
340 * need to make sure to inspect all of the entries
341 * in the bc_bufs array for buffers to be unlocked.
342 * This is because some of the btree code works from
343 * level n down to 0, and if we get an error along
344 * the way we won't have initialized all the entries
347 for (i = 0; i < cur->bc_nlevels; i++) {
349 xfs_trans_brelse(cur->bc_tp, cur->bc_bufs[i]);
354 * Can't free a bmap cursor without having dealt with the
355 * allocated indirect blocks' accounting.
357 ASSERT(cur->bc_btnum != XFS_BTNUM_BMAP ||
358 cur->bc_private.b.allocated == 0);
362 kmem_zone_free(xfs_btree_cur_zone, cur);
366 * Duplicate the btree cursor.
367 * Allocate a new one, copy the record, re-get the buffers.
370 xfs_btree_dup_cursor(
371 xfs_btree_cur_t *cur, /* input cursor */
372 xfs_btree_cur_t **ncur) /* output cursor */
374 xfs_buf_t *bp; /* btree block's buffer pointer */
375 int error; /* error return value */
376 int i; /* level number of btree block */
377 xfs_mount_t *mp; /* mount structure for filesystem */
378 xfs_btree_cur_t *new; /* new cursor value */
379 xfs_trans_t *tp; /* transaction pointer, can be NULL */
385 * Allocate a new cursor like the old one.
387 new = cur->bc_ops->dup_cursor(cur);
390 * Copy the record currently in the cursor.
392 new->bc_rec = cur->bc_rec;
395 * For each level current, re-get the buffer and copy the ptr value.
397 for (i = 0; i < new->bc_nlevels; i++) {
398 new->bc_ptrs[i] = cur->bc_ptrs[i];
399 new->bc_ra[i] = cur->bc_ra[i];
400 bp = cur->bc_bufs[i];
402 error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp,
403 XFS_BUF_ADDR(bp), mp->m_bsize,
405 cur->bc_ops->buf_ops);
407 xfs_btree_del_cursor(new, error);
412 new->bc_bufs[i] = bp;
419 * XFS btree block layout and addressing:
421 * There are two types of blocks in the btree: leaf and non-leaf blocks.
423 * The leaf record start with a header then followed by records containing
424 * the values. A non-leaf block also starts with the same header, and
425 * then first contains lookup keys followed by an equal number of pointers
426 * to the btree blocks at the previous level.
428 * +--------+-------+-------+-------+-------+-------+-------+
429 * Leaf: | header | rec 1 | rec 2 | rec 3 | rec 4 | rec 5 | rec N |
430 * +--------+-------+-------+-------+-------+-------+-------+
432 * +--------+-------+-------+-------+-------+-------+-------+
433 * Non-Leaf: | header | key 1 | key 2 | key N | ptr 1 | ptr 2 | ptr N |
434 * +--------+-------+-------+-------+-------+-------+-------+
436 * The header is called struct xfs_btree_block for reasons better left unknown
437 * and comes in different versions for short (32bit) and long (64bit) block
438 * pointers. The record and key structures are defined by the btree instances
439 * and opaque to the btree core. The block pointers are simple disk endian
440 * integers, available in a short (32bit) and long (64bit) variant.
442 * The helpers below calculate the offset of a given record, key or pointer
443 * into a btree block (xfs_btree_*_offset) or return a pointer to the given
444 * record, key or pointer (xfs_btree_*_addr). Note that all addressing
445 * inside the btree block is done using indices starting at one, not zero!
447 * If XFS_BTREE_OVERLAPPING is set, then this btree supports keys containing
448 * overlapping intervals. In such a tree, records are still sorted lowest to
449 * highest and indexed by the smallest key value that refers to the record.
450 * However, nodes are different: each pointer has two associated keys -- one
451 * indexing the lowest key available in the block(s) below (the same behavior
452 * as the key in a regular btree) and another indexing the highest key
453 * available in the block(s) below. Because records are /not/ sorted by the
454 * highest key, all leaf block updates require us to compute the highest key
455 * that matches any record in the leaf and to recursively update the high keys
456 * in the nodes going further up in the tree, if necessary. Nodes look like
459 * +--------+-----+-----+-----+-----+-----+-------+-------+-----+
460 * Non-Leaf: | header | lo1 | hi1 | lo2 | hi2 | ... | ptr 1 | ptr 2 | ... |
461 * +--------+-----+-----+-----+-----+-----+-------+-------+-----+
463 * To perform an interval query on an overlapped tree, perform the usual
464 * depth-first search and use the low and high keys to decide if we can skip
465 * that particular node. If a leaf node is reached, return the records that
466 * intersect the interval. Note that an interval query may return numerous
467 * entries. For a non-overlapped tree, simply search for the record associated
468 * with the lowest key and iterate forward until a non-matching record is
469 * found. Section 14.3 ("Interval Trees") of _Introduction to Algorithms_ by
470 * Cormen, Leiserson, Rivest, and Stein (2nd or 3rd ed. only) discuss this in
473 * Why do we care about overlapping intervals? Let's say you have a bunch of
474 * reverse mapping records on a reflink filesystem:
476 * 1: +- file A startblock B offset C length D -----------+
477 * 2: +- file E startblock F offset G length H --------------+
478 * 3: +- file I startblock F offset J length K --+
479 * 4: +- file L... --+
481 * Now say we want to map block (B+D) into file A at offset (C+D). Ideally,
482 * we'd simply increment the length of record 1. But how do we find the record
483 * that ends at (B+D-1) (i.e. record 1)? A LE lookup of (B+D-1) would return
484 * record 3 because the keys are ordered first by startblock. An interval
485 * query would return records 1 and 2 because they both overlap (B+D-1), and
486 * from that we can pick out record 1 as the appropriate left neighbor.
488 * In the non-overlapped case you can do a LE lookup and decrement the cursor
489 * because a record's interval must end before the next record.
493 * Return size of the btree block header for this btree instance.
495 static inline size_t xfs_btree_block_len(struct xfs_btree_cur *cur)
497 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
498 if (cur->bc_flags & XFS_BTREE_CRC_BLOCKS)
499 return XFS_BTREE_LBLOCK_CRC_LEN;
500 return XFS_BTREE_LBLOCK_LEN;
502 if (cur->bc_flags & XFS_BTREE_CRC_BLOCKS)
503 return XFS_BTREE_SBLOCK_CRC_LEN;
504 return XFS_BTREE_SBLOCK_LEN;
508 * Return size of btree block pointers for this btree instance.
510 static inline size_t xfs_btree_ptr_len(struct xfs_btree_cur *cur)
512 return (cur->bc_flags & XFS_BTREE_LONG_PTRS) ?
513 sizeof(__be64) : sizeof(__be32);
517 * Calculate offset of the n-th record in a btree block.
520 xfs_btree_rec_offset(
521 struct xfs_btree_cur *cur,
524 return xfs_btree_block_len(cur) +
525 (n - 1) * cur->bc_ops->rec_len;
529 * Calculate offset of the n-th key in a btree block.
532 xfs_btree_key_offset(
533 struct xfs_btree_cur *cur,
536 return xfs_btree_block_len(cur) +
537 (n - 1) * cur->bc_ops->key_len;
541 * Calculate offset of the n-th high key in a btree block.
544 xfs_btree_high_key_offset(
545 struct xfs_btree_cur *cur,
548 return xfs_btree_block_len(cur) +
549 (n - 1) * cur->bc_ops->key_len + (cur->bc_ops->key_len / 2);
553 * Calculate offset of the n-th block pointer in a btree block.
556 xfs_btree_ptr_offset(
557 struct xfs_btree_cur *cur,
561 return xfs_btree_block_len(cur) +
562 cur->bc_ops->get_maxrecs(cur, level) * cur->bc_ops->key_len +
563 (n - 1) * xfs_btree_ptr_len(cur);
567 * Return a pointer to the n-th record in the btree block.
569 union xfs_btree_rec *
571 struct xfs_btree_cur *cur,
573 struct xfs_btree_block *block)
575 return (union xfs_btree_rec *)
576 ((char *)block + xfs_btree_rec_offset(cur, n));
580 * Return a pointer to the n-th key in the btree block.
582 union xfs_btree_key *
584 struct xfs_btree_cur *cur,
586 struct xfs_btree_block *block)
588 return (union xfs_btree_key *)
589 ((char *)block + xfs_btree_key_offset(cur, n));
593 * Return a pointer to the n-th high key in the btree block.
595 union xfs_btree_key *
596 xfs_btree_high_key_addr(
597 struct xfs_btree_cur *cur,
599 struct xfs_btree_block *block)
601 return (union xfs_btree_key *)
602 ((char *)block + xfs_btree_high_key_offset(cur, n));
606 * Return a pointer to the n-th block pointer in the btree block.
608 union xfs_btree_ptr *
610 struct xfs_btree_cur *cur,
612 struct xfs_btree_block *block)
614 int level = xfs_btree_get_level(block);
616 ASSERT(block->bb_level != 0);
618 return (union xfs_btree_ptr *)
619 ((char *)block + xfs_btree_ptr_offset(cur, n, level));
623 * Get the root block which is stored in the inode.
625 * For now this btree implementation assumes the btree root is always
626 * stored in the if_broot field of an inode fork.
628 STATIC struct xfs_btree_block *
630 struct xfs_btree_cur *cur)
632 struct xfs_ifork *ifp;
634 ifp = XFS_IFORK_PTR(cur->bc_private.b.ip, cur->bc_private.b.whichfork);
635 return (struct xfs_btree_block *)ifp->if_broot;
639 * Retrieve the block pointer from the cursor at the given level.
640 * This may be an inode btree root or from a buffer.
642 struct xfs_btree_block * /* generic btree block pointer */
644 struct xfs_btree_cur *cur, /* btree cursor */
645 int level, /* level in btree */
646 struct xfs_buf **bpp) /* buffer containing the block */
648 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
649 (level == cur->bc_nlevels - 1)) {
651 return xfs_btree_get_iroot(cur);
654 *bpp = cur->bc_bufs[level];
655 return XFS_BUF_TO_BLOCK(*bpp);
659 * Get a buffer for the block, return it with no data read.
660 * Long-form addressing.
662 xfs_buf_t * /* buffer for fsbno */
664 xfs_mount_t *mp, /* file system mount point */
665 xfs_trans_t *tp, /* transaction pointer */
666 xfs_fsblock_t fsbno, /* file system block number */
667 uint lock) /* lock flags for get_buf */
669 xfs_daddr_t d; /* real disk block address */
671 ASSERT(fsbno != NULLFSBLOCK);
672 d = XFS_FSB_TO_DADDR(mp, fsbno);
673 return xfs_trans_get_buf(tp, mp->m_ddev_targp, d, mp->m_bsize, lock);
677 * Get a buffer for the block, return it with no data read.
678 * Short-form addressing.
680 xfs_buf_t * /* buffer for agno/agbno */
682 xfs_mount_t *mp, /* file system mount point */
683 xfs_trans_t *tp, /* transaction pointer */
684 xfs_agnumber_t agno, /* allocation group number */
685 xfs_agblock_t agbno, /* allocation group block number */
686 uint lock) /* lock flags for get_buf */
688 xfs_daddr_t d; /* real disk block address */
690 ASSERT(agno != NULLAGNUMBER);
691 ASSERT(agbno != NULLAGBLOCK);
692 d = XFS_AGB_TO_DADDR(mp, agno, agbno);
693 return xfs_trans_get_buf(tp, mp->m_ddev_targp, d, mp->m_bsize, lock);
697 * Check for the cursor referring to the last block at the given level.
699 int /* 1=is last block, 0=not last block */
700 xfs_btree_islastblock(
701 xfs_btree_cur_t *cur, /* btree cursor */
702 int level) /* level to check */
704 struct xfs_btree_block *block; /* generic btree block pointer */
705 xfs_buf_t *bp; /* buffer containing block */
707 block = xfs_btree_get_block(cur, level, &bp);
708 xfs_btree_check_block(cur, block, level, bp);
709 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
710 return block->bb_u.l.bb_rightsib == cpu_to_be64(NULLFSBLOCK);
712 return block->bb_u.s.bb_rightsib == cpu_to_be32(NULLAGBLOCK);
716 * Change the cursor to point to the first record at the given level.
717 * Other levels are unaffected.
719 STATIC int /* success=1, failure=0 */
721 xfs_btree_cur_t *cur, /* btree cursor */
722 int level) /* level to change */
724 struct xfs_btree_block *block; /* generic btree block pointer */
725 xfs_buf_t *bp; /* buffer containing block */
728 * Get the block pointer for this level.
730 block = xfs_btree_get_block(cur, level, &bp);
731 xfs_btree_check_block(cur, block, level, bp);
733 * It's empty, there is no such record.
735 if (!block->bb_numrecs)
738 * Set the ptr value to 1, that's the first record/key.
740 cur->bc_ptrs[level] = 1;
745 * Change the cursor to point to the last record in the current block
746 * at the given level. Other levels are unaffected.
748 STATIC int /* success=1, failure=0 */
750 xfs_btree_cur_t *cur, /* btree cursor */
751 int level) /* level to change */
753 struct xfs_btree_block *block; /* generic btree block pointer */
754 xfs_buf_t *bp; /* buffer containing block */
757 * Get the block pointer for this level.
759 block = xfs_btree_get_block(cur, level, &bp);
760 xfs_btree_check_block(cur, block, level, bp);
762 * It's empty, there is no such record.
764 if (!block->bb_numrecs)
767 * Set the ptr value to numrecs, that's the last record/key.
769 cur->bc_ptrs[level] = be16_to_cpu(block->bb_numrecs);
774 * Compute first and last byte offsets for the fields given.
775 * Interprets the offsets table, which contains struct field offsets.
779 int64_t fields, /* bitmask of fields */
780 const short *offsets, /* table of field offsets */
781 int nbits, /* number of bits to inspect */
782 int *first, /* output: first byte offset */
783 int *last) /* output: last byte offset */
785 int i; /* current bit number */
786 int64_t imask; /* mask for current bit number */
790 * Find the lowest bit, so the first byte offset.
792 for (i = 0, imask = 1LL; ; i++, imask <<= 1) {
793 if (imask & fields) {
799 * Find the highest bit, so the last byte offset.
801 for (i = nbits - 1, imask = 1LL << i; ; i--, imask >>= 1) {
802 if (imask & fields) {
803 *last = offsets[i + 1] - 1;
810 * Get a buffer for the block, return it read in.
811 * Long-form addressing.
815 struct xfs_mount *mp, /* file system mount point */
816 struct xfs_trans *tp, /* transaction pointer */
817 xfs_fsblock_t fsbno, /* file system block number */
818 uint lock, /* lock flags for read_buf */
819 struct xfs_buf **bpp, /* buffer for fsbno */
820 int refval, /* ref count value for buffer */
821 const struct xfs_buf_ops *ops)
823 struct xfs_buf *bp; /* return value */
824 xfs_daddr_t d; /* real disk block address */
827 if (!XFS_FSB_SANITY_CHECK(mp, fsbno))
828 return -EFSCORRUPTED;
829 d = XFS_FSB_TO_DADDR(mp, fsbno);
830 error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, d,
831 mp->m_bsize, lock, &bp, ops);
835 xfs_buf_set_ref(bp, refval);
841 * Read-ahead the block, don't wait for it, don't return a buffer.
842 * Long-form addressing.
846 xfs_btree_reada_bufl(
847 struct xfs_mount *mp, /* file system mount point */
848 xfs_fsblock_t fsbno, /* file system block number */
849 xfs_extlen_t count, /* count of filesystem blocks */
850 const struct xfs_buf_ops *ops)
854 ASSERT(fsbno != NULLFSBLOCK);
855 d = XFS_FSB_TO_DADDR(mp, fsbno);
856 xfs_buf_readahead(mp->m_ddev_targp, d, mp->m_bsize * count, ops);
860 * Read-ahead the block, don't wait for it, don't return a buffer.
861 * Short-form addressing.
865 xfs_btree_reada_bufs(
866 struct xfs_mount *mp, /* file system mount point */
867 xfs_agnumber_t agno, /* allocation group number */
868 xfs_agblock_t agbno, /* allocation group block number */
869 xfs_extlen_t count, /* count of filesystem blocks */
870 const struct xfs_buf_ops *ops)
874 ASSERT(agno != NULLAGNUMBER);
875 ASSERT(agbno != NULLAGBLOCK);
876 d = XFS_AGB_TO_DADDR(mp, agno, agbno);
877 xfs_buf_readahead(mp->m_ddev_targp, d, mp->m_bsize * count, ops);
881 xfs_btree_readahead_lblock(
882 struct xfs_btree_cur *cur,
884 struct xfs_btree_block *block)
887 xfs_fsblock_t left = be64_to_cpu(block->bb_u.l.bb_leftsib);
888 xfs_fsblock_t right = be64_to_cpu(block->bb_u.l.bb_rightsib);
890 if ((lr & XFS_BTCUR_LEFTRA) && left != NULLFSBLOCK) {
891 xfs_btree_reada_bufl(cur->bc_mp, left, 1,
892 cur->bc_ops->buf_ops);
896 if ((lr & XFS_BTCUR_RIGHTRA) && right != NULLFSBLOCK) {
897 xfs_btree_reada_bufl(cur->bc_mp, right, 1,
898 cur->bc_ops->buf_ops);
906 xfs_btree_readahead_sblock(
907 struct xfs_btree_cur *cur,
909 struct xfs_btree_block *block)
912 xfs_agblock_t left = be32_to_cpu(block->bb_u.s.bb_leftsib);
913 xfs_agblock_t right = be32_to_cpu(block->bb_u.s.bb_rightsib);
916 if ((lr & XFS_BTCUR_LEFTRA) && left != NULLAGBLOCK) {
917 xfs_btree_reada_bufs(cur->bc_mp, cur->bc_private.a.agno,
918 left, 1, cur->bc_ops->buf_ops);
922 if ((lr & XFS_BTCUR_RIGHTRA) && right != NULLAGBLOCK) {
923 xfs_btree_reada_bufs(cur->bc_mp, cur->bc_private.a.agno,
924 right, 1, cur->bc_ops->buf_ops);
932 * Read-ahead btree blocks, at the given level.
933 * Bits in lr are set from XFS_BTCUR_{LEFT,RIGHT}RA.
937 struct xfs_btree_cur *cur, /* btree cursor */
938 int lev, /* level in btree */
939 int lr) /* left/right bits */
941 struct xfs_btree_block *block;
944 * No readahead needed if we are at the root level and the
945 * btree root is stored in the inode.
947 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
948 (lev == cur->bc_nlevels - 1))
951 if ((cur->bc_ra[lev] | lr) == cur->bc_ra[lev])
954 cur->bc_ra[lev] |= lr;
955 block = XFS_BUF_TO_BLOCK(cur->bc_bufs[lev]);
957 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
958 return xfs_btree_readahead_lblock(cur, lr, block);
959 return xfs_btree_readahead_sblock(cur, lr, block);
963 xfs_btree_ptr_to_daddr(
964 struct xfs_btree_cur *cur,
965 union xfs_btree_ptr *ptr)
967 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
968 ASSERT(ptr->l != cpu_to_be64(NULLFSBLOCK));
970 return XFS_FSB_TO_DADDR(cur->bc_mp, be64_to_cpu(ptr->l));
972 ASSERT(cur->bc_private.a.agno != NULLAGNUMBER);
973 ASSERT(ptr->s != cpu_to_be32(NULLAGBLOCK));
975 return XFS_AGB_TO_DADDR(cur->bc_mp, cur->bc_private.a.agno,
976 be32_to_cpu(ptr->s));
981 * Readahead @count btree blocks at the given @ptr location.
983 * We don't need to care about long or short form btrees here as we have a
984 * method of converting the ptr directly to a daddr available to us.
987 xfs_btree_readahead_ptr(
988 struct xfs_btree_cur *cur,
989 union xfs_btree_ptr *ptr,
992 xfs_buf_readahead(cur->bc_mp->m_ddev_targp,
993 xfs_btree_ptr_to_daddr(cur, ptr),
994 cur->bc_mp->m_bsize * count, cur->bc_ops->buf_ops);
998 * Set the buffer for level "lev" in the cursor to bp, releasing
999 * any previous buffer.
1003 xfs_btree_cur_t *cur, /* btree cursor */
1004 int lev, /* level in btree */
1005 xfs_buf_t *bp) /* new buffer to set */
1007 struct xfs_btree_block *b; /* btree block */
1009 if (cur->bc_bufs[lev])
1010 xfs_trans_brelse(cur->bc_tp, cur->bc_bufs[lev]);
1011 cur->bc_bufs[lev] = bp;
1012 cur->bc_ra[lev] = 0;
1014 b = XFS_BUF_TO_BLOCK(bp);
1015 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
1016 if (b->bb_u.l.bb_leftsib == cpu_to_be64(NULLFSBLOCK))
1017 cur->bc_ra[lev] |= XFS_BTCUR_LEFTRA;
1018 if (b->bb_u.l.bb_rightsib == cpu_to_be64(NULLFSBLOCK))
1019 cur->bc_ra[lev] |= XFS_BTCUR_RIGHTRA;
1021 if (b->bb_u.s.bb_leftsib == cpu_to_be32(NULLAGBLOCK))
1022 cur->bc_ra[lev] |= XFS_BTCUR_LEFTRA;
1023 if (b->bb_u.s.bb_rightsib == cpu_to_be32(NULLAGBLOCK))
1024 cur->bc_ra[lev] |= XFS_BTCUR_RIGHTRA;
1029 xfs_btree_ptr_is_null(
1030 struct xfs_btree_cur *cur,
1031 union xfs_btree_ptr *ptr)
1033 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
1034 return ptr->l == cpu_to_be64(NULLFSBLOCK);
1036 return ptr->s == cpu_to_be32(NULLAGBLOCK);
1040 xfs_btree_set_ptr_null(
1041 struct xfs_btree_cur *cur,
1042 union xfs_btree_ptr *ptr)
1044 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
1045 ptr->l = cpu_to_be64(NULLFSBLOCK);
1047 ptr->s = cpu_to_be32(NULLAGBLOCK);
1051 * Get/set/init sibling pointers
1054 xfs_btree_get_sibling(
1055 struct xfs_btree_cur *cur,
1056 struct xfs_btree_block *block,
1057 union xfs_btree_ptr *ptr,
1060 ASSERT(lr == XFS_BB_LEFTSIB || lr == XFS_BB_RIGHTSIB);
1062 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
1063 if (lr == XFS_BB_RIGHTSIB)
1064 ptr->l = block->bb_u.l.bb_rightsib;
1066 ptr->l = block->bb_u.l.bb_leftsib;
1068 if (lr == XFS_BB_RIGHTSIB)
1069 ptr->s = block->bb_u.s.bb_rightsib;
1071 ptr->s = block->bb_u.s.bb_leftsib;
1076 xfs_btree_set_sibling(
1077 struct xfs_btree_cur *cur,
1078 struct xfs_btree_block *block,
1079 union xfs_btree_ptr *ptr,
1082 ASSERT(lr == XFS_BB_LEFTSIB || lr == XFS_BB_RIGHTSIB);
1084 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
1085 if (lr == XFS_BB_RIGHTSIB)
1086 block->bb_u.l.bb_rightsib = ptr->l;
1088 block->bb_u.l.bb_leftsib = ptr->l;
1090 if (lr == XFS_BB_RIGHTSIB)
1091 block->bb_u.s.bb_rightsib = ptr->s;
1093 block->bb_u.s.bb_leftsib = ptr->s;
1098 xfs_btree_init_block_int(
1099 struct xfs_mount *mp,
1100 struct xfs_btree_block *buf,
1108 int crc = xfs_sb_version_hascrc(&mp->m_sb);
1109 __u32 magic = xfs_btree_magic(crc, btnum);
1111 buf->bb_magic = cpu_to_be32(magic);
1112 buf->bb_level = cpu_to_be16(level);
1113 buf->bb_numrecs = cpu_to_be16(numrecs);
1115 if (flags & XFS_BTREE_LONG_PTRS) {
1116 buf->bb_u.l.bb_leftsib = cpu_to_be64(NULLFSBLOCK);
1117 buf->bb_u.l.bb_rightsib = cpu_to_be64(NULLFSBLOCK);
1119 buf->bb_u.l.bb_blkno = cpu_to_be64(blkno);
1120 buf->bb_u.l.bb_owner = cpu_to_be64(owner);
1121 uuid_copy(&buf->bb_u.l.bb_uuid, &mp->m_sb.sb_meta_uuid);
1122 buf->bb_u.l.bb_pad = 0;
1123 buf->bb_u.l.bb_lsn = 0;
1126 /* owner is a 32 bit value on short blocks */
1127 __u32 __owner = (__u32)owner;
1129 buf->bb_u.s.bb_leftsib = cpu_to_be32(NULLAGBLOCK);
1130 buf->bb_u.s.bb_rightsib = cpu_to_be32(NULLAGBLOCK);
1132 buf->bb_u.s.bb_blkno = cpu_to_be64(blkno);
1133 buf->bb_u.s.bb_owner = cpu_to_be32(__owner);
1134 uuid_copy(&buf->bb_u.s.bb_uuid, &mp->m_sb.sb_meta_uuid);
1135 buf->bb_u.s.bb_lsn = 0;
1141 xfs_btree_init_block(
1142 struct xfs_mount *mp,
1150 xfs_btree_init_block_int(mp, XFS_BUF_TO_BLOCK(bp), bp->b_bn,
1151 btnum, level, numrecs, owner, flags);
1155 xfs_btree_init_block_cur(
1156 struct xfs_btree_cur *cur,
1164 * we can pull the owner from the cursor right now as the different
1165 * owners align directly with the pointer size of the btree. This may
1166 * change in future, but is safe for current users of the generic btree
1169 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
1170 owner = cur->bc_private.b.ip->i_ino;
1172 owner = cur->bc_private.a.agno;
1174 xfs_btree_init_block_int(cur->bc_mp, XFS_BUF_TO_BLOCK(bp), bp->b_bn,
1175 cur->bc_btnum, level, numrecs,
1176 owner, cur->bc_flags);
1180 * Return true if ptr is the last record in the btree and
1181 * we need to track updates to this record. The decision
1182 * will be further refined in the update_lastrec method.
1185 xfs_btree_is_lastrec(
1186 struct xfs_btree_cur *cur,
1187 struct xfs_btree_block *block,
1190 union xfs_btree_ptr ptr;
1194 if (!(cur->bc_flags & XFS_BTREE_LASTREC_UPDATE))
1197 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_RIGHTSIB);
1198 if (!xfs_btree_ptr_is_null(cur, &ptr))
1204 xfs_btree_buf_to_ptr(
1205 struct xfs_btree_cur *cur,
1207 union xfs_btree_ptr *ptr)
1209 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
1210 ptr->l = cpu_to_be64(XFS_DADDR_TO_FSB(cur->bc_mp,
1213 ptr->s = cpu_to_be32(xfs_daddr_to_agbno(cur->bc_mp,
1220 struct xfs_btree_cur *cur,
1223 switch (cur->bc_btnum) {
1226 xfs_buf_set_ref(bp, XFS_ALLOC_BTREE_REF);
1229 case XFS_BTNUM_FINO:
1230 xfs_buf_set_ref(bp, XFS_INO_BTREE_REF);
1232 case XFS_BTNUM_BMAP:
1233 xfs_buf_set_ref(bp, XFS_BMAP_BTREE_REF);
1235 case XFS_BTNUM_RMAP:
1236 xfs_buf_set_ref(bp, XFS_RMAP_BTREE_REF);
1238 case XFS_BTNUM_REFC:
1239 xfs_buf_set_ref(bp, XFS_REFC_BTREE_REF);
1247 xfs_btree_get_buf_block(
1248 struct xfs_btree_cur *cur,
1249 union xfs_btree_ptr *ptr,
1251 struct xfs_btree_block **block,
1252 struct xfs_buf **bpp)
1254 struct xfs_mount *mp = cur->bc_mp;
1257 /* need to sort out how callers deal with failures first */
1258 ASSERT(!(flags & XBF_TRYLOCK));
1260 d = xfs_btree_ptr_to_daddr(cur, ptr);
1261 *bpp = xfs_trans_get_buf(cur->bc_tp, mp->m_ddev_targp, d,
1262 mp->m_bsize, flags);
1267 (*bpp)->b_ops = cur->bc_ops->buf_ops;
1268 *block = XFS_BUF_TO_BLOCK(*bpp);
1273 * Read in the buffer at the given ptr and return the buffer and
1274 * the block pointer within the buffer.
1277 xfs_btree_read_buf_block(
1278 struct xfs_btree_cur *cur,
1279 union xfs_btree_ptr *ptr,
1281 struct xfs_btree_block **block,
1282 struct xfs_buf **bpp)
1284 struct xfs_mount *mp = cur->bc_mp;
1288 /* need to sort out how callers deal with failures first */
1289 ASSERT(!(flags & XBF_TRYLOCK));
1291 d = xfs_btree_ptr_to_daddr(cur, ptr);
1292 error = xfs_trans_read_buf(mp, cur->bc_tp, mp->m_ddev_targp, d,
1293 mp->m_bsize, flags, bpp,
1294 cur->bc_ops->buf_ops);
1298 xfs_btree_set_refs(cur, *bpp);
1299 *block = XFS_BUF_TO_BLOCK(*bpp);
1304 * Copy keys from one btree block to another.
1307 xfs_btree_copy_keys(
1308 struct xfs_btree_cur *cur,
1309 union xfs_btree_key *dst_key,
1310 union xfs_btree_key *src_key,
1313 ASSERT(numkeys >= 0);
1314 memcpy(dst_key, src_key, numkeys * cur->bc_ops->key_len);
1318 * Copy records from one btree block to another.
1321 xfs_btree_copy_recs(
1322 struct xfs_btree_cur *cur,
1323 union xfs_btree_rec *dst_rec,
1324 union xfs_btree_rec *src_rec,
1327 ASSERT(numrecs >= 0);
1328 memcpy(dst_rec, src_rec, numrecs * cur->bc_ops->rec_len);
1332 * Copy block pointers from one btree block to another.
1335 xfs_btree_copy_ptrs(
1336 struct xfs_btree_cur *cur,
1337 union xfs_btree_ptr *dst_ptr,
1338 union xfs_btree_ptr *src_ptr,
1341 ASSERT(numptrs >= 0);
1342 memcpy(dst_ptr, src_ptr, numptrs * xfs_btree_ptr_len(cur));
1346 * Shift keys one index left/right inside a single btree block.
1349 xfs_btree_shift_keys(
1350 struct xfs_btree_cur *cur,
1351 union xfs_btree_key *key,
1357 ASSERT(numkeys >= 0);
1358 ASSERT(dir == 1 || dir == -1);
1360 dst_key = (char *)key + (dir * cur->bc_ops->key_len);
1361 memmove(dst_key, key, numkeys * cur->bc_ops->key_len);
1365 * Shift records one index left/right inside a single btree block.
1368 xfs_btree_shift_recs(
1369 struct xfs_btree_cur *cur,
1370 union xfs_btree_rec *rec,
1376 ASSERT(numrecs >= 0);
1377 ASSERT(dir == 1 || dir == -1);
1379 dst_rec = (char *)rec + (dir * cur->bc_ops->rec_len);
1380 memmove(dst_rec, rec, numrecs * cur->bc_ops->rec_len);
1384 * Shift block pointers one index left/right inside a single btree block.
1387 xfs_btree_shift_ptrs(
1388 struct xfs_btree_cur *cur,
1389 union xfs_btree_ptr *ptr,
1395 ASSERT(numptrs >= 0);
1396 ASSERT(dir == 1 || dir == -1);
1398 dst_ptr = (char *)ptr + (dir * xfs_btree_ptr_len(cur));
1399 memmove(dst_ptr, ptr, numptrs * xfs_btree_ptr_len(cur));
1403 * Log key values from the btree block.
1407 struct xfs_btree_cur *cur,
1412 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1413 XFS_BTREE_TRACE_ARGBII(cur, bp, first, last);
1416 xfs_trans_buf_set_type(cur->bc_tp, bp, XFS_BLFT_BTREE_BUF);
1417 xfs_trans_log_buf(cur->bc_tp, bp,
1418 xfs_btree_key_offset(cur, first),
1419 xfs_btree_key_offset(cur, last + 1) - 1);
1421 xfs_trans_log_inode(cur->bc_tp, cur->bc_private.b.ip,
1422 xfs_ilog_fbroot(cur->bc_private.b.whichfork));
1425 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1429 * Log record values from the btree block.
1433 struct xfs_btree_cur *cur,
1438 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1439 XFS_BTREE_TRACE_ARGBII(cur, bp, first, last);
1441 xfs_trans_buf_set_type(cur->bc_tp, bp, XFS_BLFT_BTREE_BUF);
1442 xfs_trans_log_buf(cur->bc_tp, bp,
1443 xfs_btree_rec_offset(cur, first),
1444 xfs_btree_rec_offset(cur, last + 1) - 1);
1446 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1450 * Log block pointer fields from a btree block (nonleaf).
1454 struct xfs_btree_cur *cur, /* btree cursor */
1455 struct xfs_buf *bp, /* buffer containing btree block */
1456 int first, /* index of first pointer to log */
1457 int last) /* index of last pointer to log */
1459 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1460 XFS_BTREE_TRACE_ARGBII(cur, bp, first, last);
1463 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
1464 int level = xfs_btree_get_level(block);
1466 xfs_trans_buf_set_type(cur->bc_tp, bp, XFS_BLFT_BTREE_BUF);
1467 xfs_trans_log_buf(cur->bc_tp, bp,
1468 xfs_btree_ptr_offset(cur, first, level),
1469 xfs_btree_ptr_offset(cur, last + 1, level) - 1);
1471 xfs_trans_log_inode(cur->bc_tp, cur->bc_private.b.ip,
1472 xfs_ilog_fbroot(cur->bc_private.b.whichfork));
1475 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1479 * Log fields from a btree block header.
1482 xfs_btree_log_block(
1483 struct xfs_btree_cur *cur, /* btree cursor */
1484 struct xfs_buf *bp, /* buffer containing btree block */
1485 int fields) /* mask of fields: XFS_BB_... */
1487 int first; /* first byte offset logged */
1488 int last; /* last byte offset logged */
1489 static const short soffsets[] = { /* table of offsets (short) */
1490 offsetof(struct xfs_btree_block, bb_magic),
1491 offsetof(struct xfs_btree_block, bb_level),
1492 offsetof(struct xfs_btree_block, bb_numrecs),
1493 offsetof(struct xfs_btree_block, bb_u.s.bb_leftsib),
1494 offsetof(struct xfs_btree_block, bb_u.s.bb_rightsib),
1495 offsetof(struct xfs_btree_block, bb_u.s.bb_blkno),
1496 offsetof(struct xfs_btree_block, bb_u.s.bb_lsn),
1497 offsetof(struct xfs_btree_block, bb_u.s.bb_uuid),
1498 offsetof(struct xfs_btree_block, bb_u.s.bb_owner),
1499 offsetof(struct xfs_btree_block, bb_u.s.bb_crc),
1500 XFS_BTREE_SBLOCK_CRC_LEN
1502 static const short loffsets[] = { /* table of offsets (long) */
1503 offsetof(struct xfs_btree_block, bb_magic),
1504 offsetof(struct xfs_btree_block, bb_level),
1505 offsetof(struct xfs_btree_block, bb_numrecs),
1506 offsetof(struct xfs_btree_block, bb_u.l.bb_leftsib),
1507 offsetof(struct xfs_btree_block, bb_u.l.bb_rightsib),
1508 offsetof(struct xfs_btree_block, bb_u.l.bb_blkno),
1509 offsetof(struct xfs_btree_block, bb_u.l.bb_lsn),
1510 offsetof(struct xfs_btree_block, bb_u.l.bb_uuid),
1511 offsetof(struct xfs_btree_block, bb_u.l.bb_owner),
1512 offsetof(struct xfs_btree_block, bb_u.l.bb_crc),
1513 offsetof(struct xfs_btree_block, bb_u.l.bb_pad),
1514 XFS_BTREE_LBLOCK_CRC_LEN
1517 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1518 XFS_BTREE_TRACE_ARGBI(cur, bp, fields);
1523 if (cur->bc_flags & XFS_BTREE_CRC_BLOCKS) {
1525 * We don't log the CRC when updating a btree
1526 * block but instead recreate it during log
1527 * recovery. As the log buffers have checksums
1528 * of their own this is safe and avoids logging a crc
1529 * update in a lot of places.
1531 if (fields == XFS_BB_ALL_BITS)
1532 fields = XFS_BB_ALL_BITS_CRC;
1533 nbits = XFS_BB_NUM_BITS_CRC;
1535 nbits = XFS_BB_NUM_BITS;
1537 xfs_btree_offsets(fields,
1538 (cur->bc_flags & XFS_BTREE_LONG_PTRS) ?
1539 loffsets : soffsets,
1540 nbits, &first, &last);
1541 xfs_trans_buf_set_type(cur->bc_tp, bp, XFS_BLFT_BTREE_BUF);
1542 xfs_trans_log_buf(cur->bc_tp, bp, first, last);
1544 xfs_trans_log_inode(cur->bc_tp, cur->bc_private.b.ip,
1545 xfs_ilog_fbroot(cur->bc_private.b.whichfork));
1548 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1552 * Increment cursor by one record at the level.
1553 * For nonzero levels the leaf-ward information is untouched.
1556 xfs_btree_increment(
1557 struct xfs_btree_cur *cur,
1559 int *stat) /* success/failure */
1561 struct xfs_btree_block *block;
1562 union xfs_btree_ptr ptr;
1564 int error; /* error return value */
1567 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1568 XFS_BTREE_TRACE_ARGI(cur, level);
1570 ASSERT(level < cur->bc_nlevels);
1572 /* Read-ahead to the right at this level. */
1573 xfs_btree_readahead(cur, level, XFS_BTCUR_RIGHTRA);
1575 /* Get a pointer to the btree block. */
1576 block = xfs_btree_get_block(cur, level, &bp);
1579 error = xfs_btree_check_block(cur, block, level, bp);
1584 /* We're done if we remain in the block after the increment. */
1585 if (++cur->bc_ptrs[level] <= xfs_btree_get_numrecs(block))
1588 /* Fail if we just went off the right edge of the tree. */
1589 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_RIGHTSIB);
1590 if (xfs_btree_ptr_is_null(cur, &ptr))
1593 XFS_BTREE_STATS_INC(cur, increment);
1596 * March up the tree incrementing pointers.
1597 * Stop when we don't go off the right edge of a block.
1599 for (lev = level + 1; lev < cur->bc_nlevels; lev++) {
1600 block = xfs_btree_get_block(cur, lev, &bp);
1603 error = xfs_btree_check_block(cur, block, lev, bp);
1608 if (++cur->bc_ptrs[lev] <= xfs_btree_get_numrecs(block))
1611 /* Read-ahead the right block for the next loop. */
1612 xfs_btree_readahead(cur, lev, XFS_BTCUR_RIGHTRA);
1616 * If we went off the root then we are either seriously
1617 * confused or have the tree root in an inode.
1619 if (lev == cur->bc_nlevels) {
1620 if (cur->bc_flags & XFS_BTREE_ROOT_IN_INODE)
1623 error = -EFSCORRUPTED;
1626 ASSERT(lev < cur->bc_nlevels);
1629 * Now walk back down the tree, fixing up the cursor's buffer
1630 * pointers and key numbers.
1632 for (block = xfs_btree_get_block(cur, lev, &bp); lev > level; ) {
1633 union xfs_btree_ptr *ptrp;
1635 ptrp = xfs_btree_ptr_addr(cur, cur->bc_ptrs[lev], block);
1637 error = xfs_btree_read_buf_block(cur, ptrp, 0, &block, &bp);
1641 xfs_btree_setbuf(cur, lev, bp);
1642 cur->bc_ptrs[lev] = 1;
1645 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1650 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1655 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
1660 * Decrement cursor by one record at the level.
1661 * For nonzero levels the leaf-ward information is untouched.
1664 xfs_btree_decrement(
1665 struct xfs_btree_cur *cur,
1667 int *stat) /* success/failure */
1669 struct xfs_btree_block *block;
1671 int error; /* error return value */
1673 union xfs_btree_ptr ptr;
1675 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1676 XFS_BTREE_TRACE_ARGI(cur, level);
1678 ASSERT(level < cur->bc_nlevels);
1680 /* Read-ahead to the left at this level. */
1681 xfs_btree_readahead(cur, level, XFS_BTCUR_LEFTRA);
1683 /* We're done if we remain in the block after the decrement. */
1684 if (--cur->bc_ptrs[level] > 0)
1687 /* Get a pointer to the btree block. */
1688 block = xfs_btree_get_block(cur, level, &bp);
1691 error = xfs_btree_check_block(cur, block, level, bp);
1696 /* Fail if we just went off the left edge of the tree. */
1697 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_LEFTSIB);
1698 if (xfs_btree_ptr_is_null(cur, &ptr))
1701 XFS_BTREE_STATS_INC(cur, decrement);
1704 * March up the tree decrementing pointers.
1705 * Stop when we don't go off the left edge of a block.
1707 for (lev = level + 1; lev < cur->bc_nlevels; lev++) {
1708 if (--cur->bc_ptrs[lev] > 0)
1710 /* Read-ahead the left block for the next loop. */
1711 xfs_btree_readahead(cur, lev, XFS_BTCUR_LEFTRA);
1715 * If we went off the root then we are seriously confused.
1716 * or the root of the tree is in an inode.
1718 if (lev == cur->bc_nlevels) {
1719 if (cur->bc_flags & XFS_BTREE_ROOT_IN_INODE)
1722 error = -EFSCORRUPTED;
1725 ASSERT(lev < cur->bc_nlevels);
1728 * Now walk back down the tree, fixing up the cursor's buffer
1729 * pointers and key numbers.
1731 for (block = xfs_btree_get_block(cur, lev, &bp); lev > level; ) {
1732 union xfs_btree_ptr *ptrp;
1734 ptrp = xfs_btree_ptr_addr(cur, cur->bc_ptrs[lev], block);
1736 error = xfs_btree_read_buf_block(cur, ptrp, 0, &block, &bp);
1739 xfs_btree_setbuf(cur, lev, bp);
1740 cur->bc_ptrs[lev] = xfs_btree_get_numrecs(block);
1743 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1748 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1753 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
1758 xfs_btree_lookup_get_block(
1759 struct xfs_btree_cur *cur, /* btree cursor */
1760 int level, /* level in the btree */
1761 union xfs_btree_ptr *pp, /* ptr to btree block */
1762 struct xfs_btree_block **blkp) /* return btree block */
1764 struct xfs_buf *bp; /* buffer pointer for btree block */
1767 /* special case the root block if in an inode */
1768 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
1769 (level == cur->bc_nlevels - 1)) {
1770 *blkp = xfs_btree_get_iroot(cur);
1775 * If the old buffer at this level for the disk address we are
1776 * looking for re-use it.
1778 * Otherwise throw it away and get a new one.
1780 bp = cur->bc_bufs[level];
1781 if (bp && XFS_BUF_ADDR(bp) == xfs_btree_ptr_to_daddr(cur, pp)) {
1782 *blkp = XFS_BUF_TO_BLOCK(bp);
1786 error = xfs_btree_read_buf_block(cur, pp, 0, blkp, &bp);
1790 /* Check the inode owner since the verifiers don't. */
1791 if (xfs_sb_version_hascrc(&cur->bc_mp->m_sb) &&
1792 (cur->bc_flags & XFS_BTREE_LONG_PTRS) &&
1793 be64_to_cpu((*blkp)->bb_u.l.bb_owner) !=
1794 cur->bc_private.b.ip->i_ino)
1797 /* Did we get the level we were looking for? */
1798 if (be16_to_cpu((*blkp)->bb_level) != level)
1801 /* Check that internal nodes have at least one record. */
1802 if (level != 0 && be16_to_cpu((*blkp)->bb_numrecs) == 0)
1805 xfs_btree_setbuf(cur, level, bp);
1810 xfs_trans_brelse(cur->bc_tp, bp);
1811 return -EFSCORRUPTED;
1815 * Get current search key. For level 0 we don't actually have a key
1816 * structure so we make one up from the record. For all other levels
1817 * we just return the right key.
1819 STATIC union xfs_btree_key *
1820 xfs_lookup_get_search_key(
1821 struct xfs_btree_cur *cur,
1824 struct xfs_btree_block *block,
1825 union xfs_btree_key *kp)
1828 cur->bc_ops->init_key_from_rec(kp,
1829 xfs_btree_rec_addr(cur, keyno, block));
1833 return xfs_btree_key_addr(cur, keyno, block);
1837 * Lookup the record. The cursor is made to point to it, based on dir.
1838 * stat is set to 0 if can't find any such record, 1 for success.
1842 struct xfs_btree_cur *cur, /* btree cursor */
1843 xfs_lookup_t dir, /* <=, ==, or >= */
1844 int *stat) /* success/failure */
1846 struct xfs_btree_block *block; /* current btree block */
1847 int64_t diff; /* difference for the current key */
1848 int error; /* error return value */
1849 int keyno; /* current key number */
1850 int level; /* level in the btree */
1851 union xfs_btree_ptr *pp; /* ptr to btree block */
1852 union xfs_btree_ptr ptr; /* ptr to btree block */
1854 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1855 XFS_BTREE_TRACE_ARGI(cur, dir);
1857 XFS_BTREE_STATS_INC(cur, lookup);
1859 /* No such thing as a zero-level tree. */
1860 if (cur->bc_nlevels == 0)
1861 return -EFSCORRUPTED;
1866 /* initialise start pointer from cursor */
1867 cur->bc_ops->init_ptr_from_cur(cur, &ptr);
1871 * Iterate over each level in the btree, starting at the root.
1872 * For each level above the leaves, find the key we need, based
1873 * on the lookup record, then follow the corresponding block
1874 * pointer down to the next level.
1876 for (level = cur->bc_nlevels - 1, diff = 1; level >= 0; level--) {
1877 /* Get the block we need to do the lookup on. */
1878 error = xfs_btree_lookup_get_block(cur, level, pp, &block);
1884 * If we already had a key match at a higher level, we
1885 * know we need to use the first entry in this block.
1889 /* Otherwise search this block. Do a binary search. */
1891 int high; /* high entry number */
1892 int low; /* low entry number */
1894 /* Set low and high entry numbers, 1-based. */
1896 high = xfs_btree_get_numrecs(block);
1898 /* Block is empty, must be an empty leaf. */
1899 ASSERT(level == 0 && cur->bc_nlevels == 1);
1901 cur->bc_ptrs[0] = dir != XFS_LOOKUP_LE;
1902 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1907 /* Binary search the block. */
1908 while (low <= high) {
1909 union xfs_btree_key key;
1910 union xfs_btree_key *kp;
1912 XFS_BTREE_STATS_INC(cur, compare);
1914 /* keyno is average of low and high. */
1915 keyno = (low + high) >> 1;
1917 /* Get current search key */
1918 kp = xfs_lookup_get_search_key(cur, level,
1919 keyno, block, &key);
1922 * Compute difference to get next direction:
1923 * - less than, move right
1924 * - greater than, move left
1925 * - equal, we're done
1927 diff = cur->bc_ops->key_diff(cur, kp);
1938 * If there are more levels, set up for the next level
1939 * by getting the block number and filling in the cursor.
1943 * If we moved left, need the previous key number,
1944 * unless there isn't one.
1946 if (diff > 0 && --keyno < 1)
1948 pp = xfs_btree_ptr_addr(cur, keyno, block);
1951 error = xfs_btree_check_ptr(cur, pp, 0, level);
1955 cur->bc_ptrs[level] = keyno;
1959 /* Done with the search. See if we need to adjust the results. */
1960 if (dir != XFS_LOOKUP_LE && diff < 0) {
1963 * If ge search and we went off the end of the block, but it's
1964 * not the last block, we're in the wrong block.
1966 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_RIGHTSIB);
1967 if (dir == XFS_LOOKUP_GE &&
1968 keyno > xfs_btree_get_numrecs(block) &&
1969 !xfs_btree_ptr_is_null(cur, &ptr)) {
1972 cur->bc_ptrs[0] = keyno;
1973 error = xfs_btree_increment(cur, 0, &i);
1976 XFS_WANT_CORRUPTED_RETURN(cur->bc_mp, i == 1);
1977 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1981 } else if (dir == XFS_LOOKUP_LE && diff > 0)
1983 cur->bc_ptrs[0] = keyno;
1985 /* Return if we succeeded or not. */
1986 if (keyno == 0 || keyno > xfs_btree_get_numrecs(block))
1988 else if (dir != XFS_LOOKUP_EQ || diff == 0)
1992 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1996 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
2000 /* Find the high key storage area from a regular key. */
2001 STATIC union xfs_btree_key *
2002 xfs_btree_high_key_from_key(
2003 struct xfs_btree_cur *cur,
2004 union xfs_btree_key *key)
2006 ASSERT(cur->bc_flags & XFS_BTREE_OVERLAPPING);
2007 return (union xfs_btree_key *)((char *)key +
2008 (cur->bc_ops->key_len / 2));
2011 /* Determine the low (and high if overlapped) keys of a leaf block */
2013 xfs_btree_get_leaf_keys(
2014 struct xfs_btree_cur *cur,
2015 struct xfs_btree_block *block,
2016 union xfs_btree_key *key)
2018 union xfs_btree_key max_hkey;
2019 union xfs_btree_key hkey;
2020 union xfs_btree_rec *rec;
2021 union xfs_btree_key *high;
2024 rec = xfs_btree_rec_addr(cur, 1, block);
2025 cur->bc_ops->init_key_from_rec(key, rec);
2027 if (cur->bc_flags & XFS_BTREE_OVERLAPPING) {
2029 cur->bc_ops->init_high_key_from_rec(&max_hkey, rec);
2030 for (n = 2; n <= xfs_btree_get_numrecs(block); n++) {
2031 rec = xfs_btree_rec_addr(cur, n, block);
2032 cur->bc_ops->init_high_key_from_rec(&hkey, rec);
2033 if (cur->bc_ops->diff_two_keys(cur, &hkey, &max_hkey)
2038 high = xfs_btree_high_key_from_key(cur, key);
2039 memcpy(high, &max_hkey, cur->bc_ops->key_len / 2);
2043 /* Determine the low (and high if overlapped) keys of a node block */
2045 xfs_btree_get_node_keys(
2046 struct xfs_btree_cur *cur,
2047 struct xfs_btree_block *block,
2048 union xfs_btree_key *key)
2050 union xfs_btree_key *hkey;
2051 union xfs_btree_key *max_hkey;
2052 union xfs_btree_key *high;
2055 if (cur->bc_flags & XFS_BTREE_OVERLAPPING) {
2056 memcpy(key, xfs_btree_key_addr(cur, 1, block),
2057 cur->bc_ops->key_len / 2);
2059 max_hkey = xfs_btree_high_key_addr(cur, 1, block);
2060 for (n = 2; n <= xfs_btree_get_numrecs(block); n++) {
2061 hkey = xfs_btree_high_key_addr(cur, n, block);
2062 if (cur->bc_ops->diff_two_keys(cur, hkey, max_hkey) > 0)
2066 high = xfs_btree_high_key_from_key(cur, key);
2067 memcpy(high, max_hkey, cur->bc_ops->key_len / 2);
2069 memcpy(key, xfs_btree_key_addr(cur, 1, block),
2070 cur->bc_ops->key_len);
2074 /* Derive the keys for any btree block. */
2077 struct xfs_btree_cur *cur,
2078 struct xfs_btree_block *block,
2079 union xfs_btree_key *key)
2081 if (be16_to_cpu(block->bb_level) == 0)
2082 xfs_btree_get_leaf_keys(cur, block, key);
2084 xfs_btree_get_node_keys(cur, block, key);
2088 * Decide if we need to update the parent keys of a btree block. For
2089 * a standard btree this is only necessary if we're updating the first
2090 * record/key. For an overlapping btree, we must always update the
2091 * keys because the highest key can be in any of the records or keys
2095 xfs_btree_needs_key_update(
2096 struct xfs_btree_cur *cur,
2099 return (cur->bc_flags & XFS_BTREE_OVERLAPPING) || ptr == 1;
2103 * Update the low and high parent keys of the given level, progressing
2104 * towards the root. If force_all is false, stop if the keys for a given
2105 * level do not need updating.
2108 __xfs_btree_updkeys(
2109 struct xfs_btree_cur *cur,
2111 struct xfs_btree_block *block,
2112 struct xfs_buf *bp0,
2115 union xfs_btree_key key; /* keys from current level */
2116 union xfs_btree_key *lkey; /* keys from the next level up */
2117 union xfs_btree_key *hkey;
2118 union xfs_btree_key *nlkey; /* keys from the next level up */
2119 union xfs_btree_key *nhkey;
2123 ASSERT(cur->bc_flags & XFS_BTREE_OVERLAPPING);
2125 /* Exit if there aren't any parent levels to update. */
2126 if (level + 1 >= cur->bc_nlevels)
2129 trace_xfs_btree_updkeys(cur, level, bp0);
2132 hkey = xfs_btree_high_key_from_key(cur, lkey);
2133 xfs_btree_get_keys(cur, block, lkey);
2134 for (level++; level < cur->bc_nlevels; level++) {
2138 block = xfs_btree_get_block(cur, level, &bp);
2139 trace_xfs_btree_updkeys(cur, level, bp);
2141 error = xfs_btree_check_block(cur, block, level, bp);
2143 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
2147 ptr = cur->bc_ptrs[level];
2148 nlkey = xfs_btree_key_addr(cur, ptr, block);
2149 nhkey = xfs_btree_high_key_addr(cur, ptr, block);
2151 !(cur->bc_ops->diff_two_keys(cur, nlkey, lkey) != 0 ||
2152 cur->bc_ops->diff_two_keys(cur, nhkey, hkey) != 0))
2154 xfs_btree_copy_keys(cur, nlkey, lkey, 1);
2155 xfs_btree_log_keys(cur, bp, ptr, ptr);
2156 if (level + 1 >= cur->bc_nlevels)
2158 xfs_btree_get_node_keys(cur, block, lkey);
2164 /* Update all the keys from some level in cursor back to the root. */
2166 xfs_btree_updkeys_force(
2167 struct xfs_btree_cur *cur,
2171 struct xfs_btree_block *block;
2173 block = xfs_btree_get_block(cur, level, &bp);
2174 return __xfs_btree_updkeys(cur, level, block, bp, true);
2178 * Update the parent keys of the given level, progressing towards the root.
2181 xfs_btree_update_keys(
2182 struct xfs_btree_cur *cur,
2185 struct xfs_btree_block *block;
2187 union xfs_btree_key *kp;
2188 union xfs_btree_key key;
2193 block = xfs_btree_get_block(cur, level, &bp);
2194 if (cur->bc_flags & XFS_BTREE_OVERLAPPING)
2195 return __xfs_btree_updkeys(cur, level, block, bp, false);
2197 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
2198 XFS_BTREE_TRACE_ARGIK(cur, level, keyp);
2201 * Go up the tree from this level toward the root.
2202 * At each level, update the key value to the value input.
2203 * Stop when we reach a level where the cursor isn't pointing
2204 * at the first entry in the block.
2206 xfs_btree_get_keys(cur, block, &key);
2207 for (level++, ptr = 1; ptr == 1 && level < cur->bc_nlevels; level++) {
2211 block = xfs_btree_get_block(cur, level, &bp);
2213 error = xfs_btree_check_block(cur, block, level, bp);
2215 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
2219 ptr = cur->bc_ptrs[level];
2220 kp = xfs_btree_key_addr(cur, ptr, block);
2221 xfs_btree_copy_keys(cur, kp, &key, 1);
2222 xfs_btree_log_keys(cur, bp, ptr, ptr);
2225 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2230 * Update the record referred to by cur to the value in the
2231 * given record. This either works (return 0) or gets an
2232 * EFSCORRUPTED error.
2236 struct xfs_btree_cur *cur,
2237 union xfs_btree_rec *rec)
2239 struct xfs_btree_block *block;
2243 union xfs_btree_rec *rp;
2245 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
2246 XFS_BTREE_TRACE_ARGR(cur, rec);
2248 /* Pick up the current block. */
2249 block = xfs_btree_get_block(cur, 0, &bp);
2252 error = xfs_btree_check_block(cur, block, 0, bp);
2256 /* Get the address of the rec to be updated. */
2257 ptr = cur->bc_ptrs[0];
2258 rp = xfs_btree_rec_addr(cur, ptr, block);
2260 /* Fill in the new contents and log them. */
2261 xfs_btree_copy_recs(cur, rp, rec, 1);
2262 xfs_btree_log_recs(cur, bp, ptr, ptr);
2265 * If we are tracking the last record in the tree and
2266 * we are at the far right edge of the tree, update it.
2268 if (xfs_btree_is_lastrec(cur, block, 0)) {
2269 cur->bc_ops->update_lastrec(cur, block, rec,
2270 ptr, LASTREC_UPDATE);
2273 /* Pass new key value up to our parent. */
2274 if (xfs_btree_needs_key_update(cur, ptr)) {
2275 error = xfs_btree_update_keys(cur, 0);
2280 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2284 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
2289 * Move 1 record left from cur/level if possible.
2290 * Update cur to reflect the new path.
2292 STATIC int /* error */
2294 struct xfs_btree_cur *cur,
2296 int *stat) /* success/failure */
2298 struct xfs_buf *lbp; /* left buffer pointer */
2299 struct xfs_btree_block *left; /* left btree block */
2300 int lrecs; /* left record count */
2301 struct xfs_buf *rbp; /* right buffer pointer */
2302 struct xfs_btree_block *right; /* right btree block */
2303 struct xfs_btree_cur *tcur; /* temporary btree cursor */
2304 int rrecs; /* right record count */
2305 union xfs_btree_ptr lptr; /* left btree pointer */
2306 union xfs_btree_key *rkp = NULL; /* right btree key */
2307 union xfs_btree_ptr *rpp = NULL; /* right address pointer */
2308 union xfs_btree_rec *rrp = NULL; /* right record pointer */
2309 int error; /* error return value */
2312 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
2313 XFS_BTREE_TRACE_ARGI(cur, level);
2315 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
2316 level == cur->bc_nlevels - 1)
2319 /* Set up variables for this block as "right". */
2320 right = xfs_btree_get_block(cur, level, &rbp);
2323 error = xfs_btree_check_block(cur, right, level, rbp);
2328 /* If we've got no left sibling then we can't shift an entry left. */
2329 xfs_btree_get_sibling(cur, right, &lptr, XFS_BB_LEFTSIB);
2330 if (xfs_btree_ptr_is_null(cur, &lptr))
2334 * If the cursor entry is the one that would be moved, don't
2335 * do it... it's too complicated.
2337 if (cur->bc_ptrs[level] <= 1)
2340 /* Set up the left neighbor as "left". */
2341 error = xfs_btree_read_buf_block(cur, &lptr, 0, &left, &lbp);
2345 /* If it's full, it can't take another entry. */
2346 lrecs = xfs_btree_get_numrecs(left);
2347 if (lrecs == cur->bc_ops->get_maxrecs(cur, level))
2350 rrecs = xfs_btree_get_numrecs(right);
2353 * We add one entry to the left side and remove one for the right side.
2354 * Account for it here, the changes will be updated on disk and logged
2360 XFS_BTREE_STATS_INC(cur, lshift);
2361 XFS_BTREE_STATS_ADD(cur, moves, 1);
2364 * If non-leaf, copy a key and a ptr to the left block.
2365 * Log the changes to the left block.
2368 /* It's a non-leaf. Move keys and pointers. */
2369 union xfs_btree_key *lkp; /* left btree key */
2370 union xfs_btree_ptr *lpp; /* left address pointer */
2372 lkp = xfs_btree_key_addr(cur, lrecs, left);
2373 rkp = xfs_btree_key_addr(cur, 1, right);
2375 lpp = xfs_btree_ptr_addr(cur, lrecs, left);
2376 rpp = xfs_btree_ptr_addr(cur, 1, right);
2378 error = xfs_btree_check_ptr(cur, rpp, 0, level);
2382 xfs_btree_copy_keys(cur, lkp, rkp, 1);
2383 xfs_btree_copy_ptrs(cur, lpp, rpp, 1);
2385 xfs_btree_log_keys(cur, lbp, lrecs, lrecs);
2386 xfs_btree_log_ptrs(cur, lbp, lrecs, lrecs);
2388 ASSERT(cur->bc_ops->keys_inorder(cur,
2389 xfs_btree_key_addr(cur, lrecs - 1, left), lkp));
2391 /* It's a leaf. Move records. */
2392 union xfs_btree_rec *lrp; /* left record pointer */
2394 lrp = xfs_btree_rec_addr(cur, lrecs, left);
2395 rrp = xfs_btree_rec_addr(cur, 1, right);
2397 xfs_btree_copy_recs(cur, lrp, rrp, 1);
2398 xfs_btree_log_recs(cur, lbp, lrecs, lrecs);
2400 ASSERT(cur->bc_ops->recs_inorder(cur,
2401 xfs_btree_rec_addr(cur, lrecs - 1, left), lrp));
2404 xfs_btree_set_numrecs(left, lrecs);
2405 xfs_btree_log_block(cur, lbp, XFS_BB_NUMRECS);
2407 xfs_btree_set_numrecs(right, rrecs);
2408 xfs_btree_log_block(cur, rbp, XFS_BB_NUMRECS);
2411 * Slide the contents of right down one entry.
2413 XFS_BTREE_STATS_ADD(cur, moves, rrecs - 1);
2415 /* It's a nonleaf. operate on keys and ptrs */
2417 int i; /* loop index */
2419 for (i = 0; i < rrecs; i++) {
2420 error = xfs_btree_check_ptr(cur, rpp, i + 1, level);
2425 xfs_btree_shift_keys(cur,
2426 xfs_btree_key_addr(cur, 2, right),
2428 xfs_btree_shift_ptrs(cur,
2429 xfs_btree_ptr_addr(cur, 2, right),
2432 xfs_btree_log_keys(cur, rbp, 1, rrecs);
2433 xfs_btree_log_ptrs(cur, rbp, 1, rrecs);
2435 /* It's a leaf. operate on records */
2436 xfs_btree_shift_recs(cur,
2437 xfs_btree_rec_addr(cur, 2, right),
2439 xfs_btree_log_recs(cur, rbp, 1, rrecs);
2443 * Using a temporary cursor, update the parent key values of the
2444 * block on the left.
2446 if (cur->bc_flags & XFS_BTREE_OVERLAPPING) {
2447 error = xfs_btree_dup_cursor(cur, &tcur);
2450 i = xfs_btree_firstrec(tcur, level);
2451 XFS_WANT_CORRUPTED_GOTO(tcur->bc_mp, i == 1, error0);
2453 error = xfs_btree_decrement(tcur, level, &i);
2457 /* Update the parent high keys of the left block, if needed. */
2458 error = xfs_btree_update_keys(tcur, level);
2462 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
2465 /* Update the parent keys of the right block. */
2466 error = xfs_btree_update_keys(cur, level);
2470 /* Slide the cursor value left one. */
2471 cur->bc_ptrs[level]--;
2473 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2478 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2483 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
2487 XFS_BTREE_TRACE_CURSOR(tcur, XBT_ERROR);
2488 xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
2493 * Move 1 record right from cur/level if possible.
2494 * Update cur to reflect the new path.
2496 STATIC int /* error */
2498 struct xfs_btree_cur *cur,
2500 int *stat) /* success/failure */
2502 struct xfs_buf *lbp; /* left buffer pointer */
2503 struct xfs_btree_block *left; /* left btree block */
2504 struct xfs_buf *rbp; /* right buffer pointer */
2505 struct xfs_btree_block *right; /* right btree block */
2506 struct xfs_btree_cur *tcur; /* temporary btree cursor */
2507 union xfs_btree_ptr rptr; /* right block pointer */
2508 union xfs_btree_key *rkp; /* right btree key */
2509 int rrecs; /* right record count */
2510 int lrecs; /* left record count */
2511 int error; /* error return value */
2512 int i; /* loop counter */
2514 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
2515 XFS_BTREE_TRACE_ARGI(cur, level);
2517 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
2518 (level == cur->bc_nlevels - 1))
2521 /* Set up variables for this block as "left". */
2522 left = xfs_btree_get_block(cur, level, &lbp);
2525 error = xfs_btree_check_block(cur, left, level, lbp);
2530 /* If we've got no right sibling then we can't shift an entry right. */
2531 xfs_btree_get_sibling(cur, left, &rptr, XFS_BB_RIGHTSIB);
2532 if (xfs_btree_ptr_is_null(cur, &rptr))
2536 * If the cursor entry is the one that would be moved, don't
2537 * do it... it's too complicated.
2539 lrecs = xfs_btree_get_numrecs(left);
2540 if (cur->bc_ptrs[level] >= lrecs)
2543 /* Set up the right neighbor as "right". */
2544 error = xfs_btree_read_buf_block(cur, &rptr, 0, &right, &rbp);
2548 /* If it's full, it can't take another entry. */
2549 rrecs = xfs_btree_get_numrecs(right);
2550 if (rrecs == cur->bc_ops->get_maxrecs(cur, level))
2553 XFS_BTREE_STATS_INC(cur, rshift);
2554 XFS_BTREE_STATS_ADD(cur, moves, rrecs);
2557 * Make a hole at the start of the right neighbor block, then
2558 * copy the last left block entry to the hole.
2561 /* It's a nonleaf. make a hole in the keys and ptrs */
2562 union xfs_btree_key *lkp;
2563 union xfs_btree_ptr *lpp;
2564 union xfs_btree_ptr *rpp;
2566 lkp = xfs_btree_key_addr(cur, lrecs, left);
2567 lpp = xfs_btree_ptr_addr(cur, lrecs, left);
2568 rkp = xfs_btree_key_addr(cur, 1, right);
2569 rpp = xfs_btree_ptr_addr(cur, 1, right);
2572 for (i = rrecs - 1; i >= 0; i--) {
2573 error = xfs_btree_check_ptr(cur, rpp, i, level);
2579 xfs_btree_shift_keys(cur, rkp, 1, rrecs);
2580 xfs_btree_shift_ptrs(cur, rpp, 1, rrecs);
2583 error = xfs_btree_check_ptr(cur, lpp, 0, level);
2588 /* Now put the new data in, and log it. */
2589 xfs_btree_copy_keys(cur, rkp, lkp, 1);
2590 xfs_btree_copy_ptrs(cur, rpp, lpp, 1);
2592 xfs_btree_log_keys(cur, rbp, 1, rrecs + 1);
2593 xfs_btree_log_ptrs(cur, rbp, 1, rrecs + 1);
2595 ASSERT(cur->bc_ops->keys_inorder(cur, rkp,
2596 xfs_btree_key_addr(cur, 2, right)));
2598 /* It's a leaf. make a hole in the records */
2599 union xfs_btree_rec *lrp;
2600 union xfs_btree_rec *rrp;
2602 lrp = xfs_btree_rec_addr(cur, lrecs, left);
2603 rrp = xfs_btree_rec_addr(cur, 1, right);
2605 xfs_btree_shift_recs(cur, rrp, 1, rrecs);
2607 /* Now put the new data in, and log it. */
2608 xfs_btree_copy_recs(cur, rrp, lrp, 1);
2609 xfs_btree_log_recs(cur, rbp, 1, rrecs + 1);
2613 * Decrement and log left's numrecs, bump and log right's numrecs.
2615 xfs_btree_set_numrecs(left, --lrecs);
2616 xfs_btree_log_block(cur, lbp, XFS_BB_NUMRECS);
2618 xfs_btree_set_numrecs(right, ++rrecs);
2619 xfs_btree_log_block(cur, rbp, XFS_BB_NUMRECS);
2622 * Using a temporary cursor, update the parent key values of the
2623 * block on the right.
2625 error = xfs_btree_dup_cursor(cur, &tcur);
2628 i = xfs_btree_lastrec(tcur, level);
2629 XFS_WANT_CORRUPTED_GOTO(tcur->bc_mp, i == 1, error0);
2631 error = xfs_btree_increment(tcur, level, &i);
2635 /* Update the parent high keys of the left block, if needed. */
2636 if (cur->bc_flags & XFS_BTREE_OVERLAPPING) {
2637 error = xfs_btree_update_keys(cur, level);
2642 /* Update the parent keys of the right block. */
2643 error = xfs_btree_update_keys(tcur, level);
2647 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
2649 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2654 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2659 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
2663 XFS_BTREE_TRACE_CURSOR(tcur, XBT_ERROR);
2664 xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
2669 * Split cur/level block in half.
2670 * Return new block number and the key to its first
2671 * record (to be inserted into parent).
2673 STATIC int /* error */
2675 struct xfs_btree_cur *cur,
2677 union xfs_btree_ptr *ptrp,
2678 union xfs_btree_key *key,
2679 struct xfs_btree_cur **curp,
2680 int *stat) /* success/failure */
2682 union xfs_btree_ptr lptr; /* left sibling block ptr */
2683 struct xfs_buf *lbp; /* left buffer pointer */
2684 struct xfs_btree_block *left; /* left btree block */
2685 union xfs_btree_ptr rptr; /* right sibling block ptr */
2686 struct xfs_buf *rbp; /* right buffer pointer */
2687 struct xfs_btree_block *right; /* right btree block */
2688 union xfs_btree_ptr rrptr; /* right-right sibling ptr */
2689 struct xfs_buf *rrbp; /* right-right buffer pointer */
2690 struct xfs_btree_block *rrblock; /* right-right btree block */
2694 int error; /* error return value */
2699 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
2700 XFS_BTREE_TRACE_ARGIPK(cur, level, *ptrp, key);
2702 XFS_BTREE_STATS_INC(cur, split);
2704 /* Set up left block (current one). */
2705 left = xfs_btree_get_block(cur, level, &lbp);
2708 error = xfs_btree_check_block(cur, left, level, lbp);
2713 xfs_btree_buf_to_ptr(cur, lbp, &lptr);
2715 /* Allocate the new block. If we can't do it, we're toast. Give up. */
2716 error = cur->bc_ops->alloc_block(cur, &lptr, &rptr, stat);
2721 XFS_BTREE_STATS_INC(cur, alloc);
2723 /* Set up the new block as "right". */
2724 error = xfs_btree_get_buf_block(cur, &rptr, 0, &right, &rbp);
2728 /* Fill in the btree header for the new right block. */
2729 xfs_btree_init_block_cur(cur, rbp, xfs_btree_get_level(left), 0);
2732 * Split the entries between the old and the new block evenly.
2733 * Make sure that if there's an odd number of entries now, that
2734 * each new block will have the same number of entries.
2736 lrecs = xfs_btree_get_numrecs(left);
2738 if ((lrecs & 1) && cur->bc_ptrs[level] <= rrecs + 1)
2740 src_index = (lrecs - rrecs + 1);
2742 XFS_BTREE_STATS_ADD(cur, moves, rrecs);
2744 /* Adjust numrecs for the later get_*_keys() calls. */
2746 xfs_btree_set_numrecs(left, lrecs);
2747 xfs_btree_set_numrecs(right, xfs_btree_get_numrecs(right) + rrecs);
2750 * Copy btree block entries from the left block over to the
2751 * new block, the right. Update the right block and log the
2755 /* It's a non-leaf. Move keys and pointers. */
2756 union xfs_btree_key *lkp; /* left btree key */
2757 union xfs_btree_ptr *lpp; /* left address pointer */
2758 union xfs_btree_key *rkp; /* right btree key */
2759 union xfs_btree_ptr *rpp; /* right address pointer */
2761 lkp = xfs_btree_key_addr(cur, src_index, left);
2762 lpp = xfs_btree_ptr_addr(cur, src_index, left);
2763 rkp = xfs_btree_key_addr(cur, 1, right);
2764 rpp = xfs_btree_ptr_addr(cur, 1, right);
2767 for (i = src_index; i < rrecs; i++) {
2768 error = xfs_btree_check_ptr(cur, lpp, i, level);
2774 /* Copy the keys & pointers to the new block. */
2775 xfs_btree_copy_keys(cur, rkp, lkp, rrecs);
2776 xfs_btree_copy_ptrs(cur, rpp, lpp, rrecs);
2778 xfs_btree_log_keys(cur, rbp, 1, rrecs);
2779 xfs_btree_log_ptrs(cur, rbp, 1, rrecs);
2781 /* Stash the keys of the new block for later insertion. */
2782 xfs_btree_get_node_keys(cur, right, key);
2784 /* It's a leaf. Move records. */
2785 union xfs_btree_rec *lrp; /* left record pointer */
2786 union xfs_btree_rec *rrp; /* right record pointer */
2788 lrp = xfs_btree_rec_addr(cur, src_index, left);
2789 rrp = xfs_btree_rec_addr(cur, 1, right);
2791 /* Copy records to the new block. */
2792 xfs_btree_copy_recs(cur, rrp, lrp, rrecs);
2793 xfs_btree_log_recs(cur, rbp, 1, rrecs);
2795 /* Stash the keys of the new block for later insertion. */
2796 xfs_btree_get_leaf_keys(cur, right, key);
2800 * Find the left block number by looking in the buffer.
2801 * Adjust sibling pointers.
2803 xfs_btree_get_sibling(cur, left, &rrptr, XFS_BB_RIGHTSIB);
2804 xfs_btree_set_sibling(cur, right, &rrptr, XFS_BB_RIGHTSIB);
2805 xfs_btree_set_sibling(cur, right, &lptr, XFS_BB_LEFTSIB);
2806 xfs_btree_set_sibling(cur, left, &rptr, XFS_BB_RIGHTSIB);
2808 xfs_btree_log_block(cur, rbp, XFS_BB_ALL_BITS);
2809 xfs_btree_log_block(cur, lbp, XFS_BB_NUMRECS | XFS_BB_RIGHTSIB);
2812 * If there's a block to the new block's right, make that block
2813 * point back to right instead of to left.
2815 if (!xfs_btree_ptr_is_null(cur, &rrptr)) {
2816 error = xfs_btree_read_buf_block(cur, &rrptr,
2817 0, &rrblock, &rrbp);
2820 xfs_btree_set_sibling(cur, rrblock, &rptr, XFS_BB_LEFTSIB);
2821 xfs_btree_log_block(cur, rrbp, XFS_BB_LEFTSIB);
2824 /* Update the parent high keys of the left block, if needed. */
2825 if (cur->bc_flags & XFS_BTREE_OVERLAPPING) {
2826 error = xfs_btree_update_keys(cur, level);
2832 * If the cursor is really in the right block, move it there.
2833 * If it's just pointing past the last entry in left, then we'll
2834 * insert there, so don't change anything in that case.
2836 if (cur->bc_ptrs[level] > lrecs + 1) {
2837 xfs_btree_setbuf(cur, level, rbp);
2838 cur->bc_ptrs[level] -= lrecs;
2841 * If there are more levels, we'll need another cursor which refers
2842 * the right block, no matter where this cursor was.
2844 if (level + 1 < cur->bc_nlevels) {
2845 error = xfs_btree_dup_cursor(cur, curp);
2848 (*curp)->bc_ptrs[level + 1]++;
2851 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2855 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2860 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
2864 struct xfs_btree_split_args {
2865 struct xfs_btree_cur *cur;
2867 union xfs_btree_ptr *ptrp;
2868 union xfs_btree_key *key;
2869 struct xfs_btree_cur **curp;
2870 int *stat; /* success/failure */
2872 bool kswapd; /* allocation in kswapd context */
2873 struct completion *done;
2874 struct work_struct work;
2878 * Stack switching interfaces for allocation
2881 xfs_btree_split_worker(
2882 struct work_struct *work)
2884 struct xfs_btree_split_args *args = container_of(work,
2885 struct xfs_btree_split_args, work);
2886 unsigned long pflags;
2887 unsigned long new_pflags = PF_MEMALLOC_NOFS;
2890 * we are in a transaction context here, but may also be doing work
2891 * in kswapd context, and hence we may need to inherit that state
2892 * temporarily to ensure that we don't block waiting for memory reclaim
2896 new_pflags |= PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD;
2898 current_set_flags_nested(&pflags, new_pflags);
2900 args->result = __xfs_btree_split(args->cur, args->level, args->ptrp,
2901 args->key, args->curp, args->stat);
2902 complete(args->done);
2904 current_restore_flags_nested(&pflags, new_pflags);
2908 * BMBT split requests often come in with little stack to work on. Push
2909 * them off to a worker thread so there is lots of stack to use. For the other
2910 * btree types, just call directly to avoid the context switch overhead here.
2912 STATIC int /* error */
2914 struct xfs_btree_cur *cur,
2916 union xfs_btree_ptr *ptrp,
2917 union xfs_btree_key *key,
2918 struct xfs_btree_cur **curp,
2919 int *stat) /* success/failure */
2921 struct xfs_btree_split_args args;
2922 DECLARE_COMPLETION_ONSTACK(done);
2924 if (cur->bc_btnum != XFS_BTNUM_BMAP)
2925 return __xfs_btree_split(cur, level, ptrp, key, curp, stat);
2934 args.kswapd = current_is_kswapd();
2935 INIT_WORK_ONSTACK(&args.work, xfs_btree_split_worker);
2936 queue_work(xfs_alloc_wq, &args.work);
2937 wait_for_completion(&done);
2938 destroy_work_on_stack(&args.work);
2944 * Copy the old inode root contents into a real block and make the
2945 * broot point to it.
2948 xfs_btree_new_iroot(
2949 struct xfs_btree_cur *cur, /* btree cursor */
2950 int *logflags, /* logging flags for inode */
2951 int *stat) /* return status - 0 fail */
2953 struct xfs_buf *cbp; /* buffer for cblock */
2954 struct xfs_btree_block *block; /* btree block */
2955 struct xfs_btree_block *cblock; /* child btree block */
2956 union xfs_btree_key *ckp; /* child key pointer */
2957 union xfs_btree_ptr *cpp; /* child ptr pointer */
2958 union xfs_btree_key *kp; /* pointer to btree key */
2959 union xfs_btree_ptr *pp; /* pointer to block addr */
2960 union xfs_btree_ptr nptr; /* new block addr */
2961 int level; /* btree level */
2962 int error; /* error return code */
2964 int i; /* loop counter */
2967 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
2968 XFS_BTREE_STATS_INC(cur, newroot);
2970 ASSERT(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE);
2972 level = cur->bc_nlevels - 1;
2974 block = xfs_btree_get_iroot(cur);
2975 pp = xfs_btree_ptr_addr(cur, 1, block);
2977 /* Allocate the new block. If we can't do it, we're toast. Give up. */
2978 error = cur->bc_ops->alloc_block(cur, pp, &nptr, stat);
2982 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2985 XFS_BTREE_STATS_INC(cur, alloc);
2987 /* Copy the root into a real block. */
2988 error = xfs_btree_get_buf_block(cur, &nptr, 0, &cblock, &cbp);
2993 * we can't just memcpy() the root in for CRC enabled btree blocks.
2994 * In that case have to also ensure the blkno remains correct
2996 memcpy(cblock, block, xfs_btree_block_len(cur));
2997 if (cur->bc_flags & XFS_BTREE_CRC_BLOCKS) {
2998 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
2999 cblock->bb_u.l.bb_blkno = cpu_to_be64(cbp->b_bn);
3001 cblock->bb_u.s.bb_blkno = cpu_to_be64(cbp->b_bn);
3004 be16_add_cpu(&block->bb_level, 1);
3005 xfs_btree_set_numrecs(block, 1);
3007 cur->bc_ptrs[level + 1] = 1;
3009 kp = xfs_btree_key_addr(cur, 1, block);
3010 ckp = xfs_btree_key_addr(cur, 1, cblock);
3011 xfs_btree_copy_keys(cur, ckp, kp, xfs_btree_get_numrecs(cblock));
3013 cpp = xfs_btree_ptr_addr(cur, 1, cblock);
3015 for (i = 0; i < be16_to_cpu(cblock->bb_numrecs); i++) {
3016 error = xfs_btree_check_ptr(cur, pp, i, level);
3021 xfs_btree_copy_ptrs(cur, cpp, pp, xfs_btree_get_numrecs(cblock));
3024 error = xfs_btree_check_ptr(cur, &nptr, 0, level);
3028 xfs_btree_copy_ptrs(cur, pp, &nptr, 1);
3030 xfs_iroot_realloc(cur->bc_private.b.ip,
3031 1 - xfs_btree_get_numrecs(cblock),
3032 cur->bc_private.b.whichfork);
3034 xfs_btree_setbuf(cur, level, cbp);
3037 * Do all this logging at the end so that
3038 * the root is at the right level.
3040 xfs_btree_log_block(cur, cbp, XFS_BB_ALL_BITS);
3041 xfs_btree_log_keys(cur, cbp, 1, be16_to_cpu(cblock->bb_numrecs));
3042 xfs_btree_log_ptrs(cur, cbp, 1, be16_to_cpu(cblock->bb_numrecs));
3045 XFS_ILOG_CORE | xfs_ilog_fbroot(cur->bc_private.b.whichfork);
3047 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3050 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
3055 * Allocate a new root block, fill it in.
3057 STATIC int /* error */
3059 struct xfs_btree_cur *cur, /* btree cursor */
3060 int *stat) /* success/failure */
3062 struct xfs_btree_block *block; /* one half of the old root block */
3063 struct xfs_buf *bp; /* buffer containing block */
3064 int error; /* error return value */
3065 struct xfs_buf *lbp; /* left buffer pointer */
3066 struct xfs_btree_block *left; /* left btree block */
3067 struct xfs_buf *nbp; /* new (root) buffer */
3068 struct xfs_btree_block *new; /* new (root) btree block */
3069 int nptr; /* new value for key index, 1 or 2 */
3070 struct xfs_buf *rbp; /* right buffer pointer */
3071 struct xfs_btree_block *right; /* right btree block */
3072 union xfs_btree_ptr rptr;
3073 union xfs_btree_ptr lptr;
3075 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
3076 XFS_BTREE_STATS_INC(cur, newroot);
3078 /* initialise our start point from the cursor */
3079 cur->bc_ops->init_ptr_from_cur(cur, &rptr);
3081 /* Allocate the new block. If we can't do it, we're toast. Give up. */
3082 error = cur->bc_ops->alloc_block(cur, &rptr, &lptr, stat);
3087 XFS_BTREE_STATS_INC(cur, alloc);
3089 /* Set up the new block. */
3090 error = xfs_btree_get_buf_block(cur, &lptr, 0, &new, &nbp);
3094 /* Set the root in the holding structure increasing the level by 1. */
3095 cur->bc_ops->set_root(cur, &lptr, 1);
3098 * At the previous root level there are now two blocks: the old root,
3099 * and the new block generated when it was split. We don't know which
3100 * one the cursor is pointing at, so we set up variables "left" and
3101 * "right" for each case.
3103 block = xfs_btree_get_block(cur, cur->bc_nlevels - 1, &bp);
3106 error = xfs_btree_check_block(cur, block, cur->bc_nlevels - 1, bp);
3111 xfs_btree_get_sibling(cur, block, &rptr, XFS_BB_RIGHTSIB);
3112 if (!xfs_btree_ptr_is_null(cur, &rptr)) {
3113 /* Our block is left, pick up the right block. */
3115 xfs_btree_buf_to_ptr(cur, lbp, &lptr);
3117 error = xfs_btree_read_buf_block(cur, &rptr, 0, &right, &rbp);
3123 /* Our block is right, pick up the left block. */
3125 xfs_btree_buf_to_ptr(cur, rbp, &rptr);
3127 xfs_btree_get_sibling(cur, right, &lptr, XFS_BB_LEFTSIB);
3128 error = xfs_btree_read_buf_block(cur, &lptr, 0, &left, &lbp);
3135 /* Fill in the new block's btree header and log it. */
3136 xfs_btree_init_block_cur(cur, nbp, cur->bc_nlevels, 2);
3137 xfs_btree_log_block(cur, nbp, XFS_BB_ALL_BITS);
3138 ASSERT(!xfs_btree_ptr_is_null(cur, &lptr) &&
3139 !xfs_btree_ptr_is_null(cur, &rptr));
3141 /* Fill in the key data in the new root. */
3142 if (xfs_btree_get_level(left) > 0) {
3144 * Get the keys for the left block's keys and put them directly
3145 * in the parent block. Do the same for the right block.
3147 xfs_btree_get_node_keys(cur, left,
3148 xfs_btree_key_addr(cur, 1, new));
3149 xfs_btree_get_node_keys(cur, right,
3150 xfs_btree_key_addr(cur, 2, new));
3153 * Get the keys for the left block's records and put them
3154 * directly in the parent block. Do the same for the right
3157 xfs_btree_get_leaf_keys(cur, left,
3158 xfs_btree_key_addr(cur, 1, new));
3159 xfs_btree_get_leaf_keys(cur, right,
3160 xfs_btree_key_addr(cur, 2, new));
3162 xfs_btree_log_keys(cur, nbp, 1, 2);
3164 /* Fill in the pointer data in the new root. */
3165 xfs_btree_copy_ptrs(cur,
3166 xfs_btree_ptr_addr(cur, 1, new), &lptr, 1);
3167 xfs_btree_copy_ptrs(cur,
3168 xfs_btree_ptr_addr(cur, 2, new), &rptr, 1);
3169 xfs_btree_log_ptrs(cur, nbp, 1, 2);
3171 /* Fix up the cursor. */
3172 xfs_btree_setbuf(cur, cur->bc_nlevels, nbp);
3173 cur->bc_ptrs[cur->bc_nlevels] = nptr;
3175 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3179 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
3182 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3188 xfs_btree_make_block_unfull(
3189 struct xfs_btree_cur *cur, /* btree cursor */
3190 int level, /* btree level */
3191 int numrecs,/* # of recs in block */
3192 int *oindex,/* old tree index */
3193 int *index, /* new tree index */
3194 union xfs_btree_ptr *nptr, /* new btree ptr */
3195 struct xfs_btree_cur **ncur, /* new btree cursor */
3196 union xfs_btree_key *key, /* key of new block */
3201 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
3202 level == cur->bc_nlevels - 1) {
3203 struct xfs_inode *ip = cur->bc_private.b.ip;
3205 if (numrecs < cur->bc_ops->get_dmaxrecs(cur, level)) {
3206 /* A root block that can be made bigger. */
3207 xfs_iroot_realloc(ip, 1, cur->bc_private.b.whichfork);
3210 /* A root block that needs replacing */
3213 error = xfs_btree_new_iroot(cur, &logflags, stat);
3214 if (error || *stat == 0)
3217 xfs_trans_log_inode(cur->bc_tp, ip, logflags);
3223 /* First, try shifting an entry to the right neighbor. */
3224 error = xfs_btree_rshift(cur, level, stat);
3228 /* Next, try shifting an entry to the left neighbor. */
3229 error = xfs_btree_lshift(cur, level, stat);
3234 *oindex = *index = cur->bc_ptrs[level];
3239 * Next, try splitting the current block in half.
3241 * If this works we have to re-set our variables because we
3242 * could be in a different block now.
3244 error = xfs_btree_split(cur, level, nptr, key, ncur, stat);
3245 if (error || *stat == 0)
3249 *index = cur->bc_ptrs[level];
3254 * Insert one record/level. Return information to the caller
3255 * allowing the next level up to proceed if necessary.
3259 struct xfs_btree_cur *cur, /* btree cursor */
3260 int level, /* level to insert record at */
3261 union xfs_btree_ptr *ptrp, /* i/o: block number inserted */
3262 union xfs_btree_rec *rec, /* record to insert */
3263 union xfs_btree_key *key, /* i/o: block key for ptrp */
3264 struct xfs_btree_cur **curp, /* output: new cursor replacing cur */
3265 int *stat) /* success/failure */
3267 struct xfs_btree_block *block; /* btree block */
3268 struct xfs_buf *bp; /* buffer for block */
3269 union xfs_btree_ptr nptr; /* new block ptr */
3270 struct xfs_btree_cur *ncur; /* new btree cursor */
3271 union xfs_btree_key nkey; /* new block key */
3272 union xfs_btree_key *lkey;
3273 int optr; /* old key/record index */
3274 int ptr; /* key/record index */
3275 int numrecs;/* number of records */
3276 int error; /* error return value */
3282 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
3283 XFS_BTREE_TRACE_ARGIPR(cur, level, *ptrp, &rec);
3289 * If we have an external root pointer, and we've made it to the
3290 * root level, allocate a new root block and we're done.
3292 if (!(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
3293 (level >= cur->bc_nlevels)) {
3294 error = xfs_btree_new_root(cur, stat);
3295 xfs_btree_set_ptr_null(cur, ptrp);
3297 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3301 /* If we're off the left edge, return failure. */
3302 ptr = cur->bc_ptrs[level];
3304 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3311 XFS_BTREE_STATS_INC(cur, insrec);
3313 /* Get pointers to the btree buffer and block. */
3314 block = xfs_btree_get_block(cur, level, &bp);
3315 old_bn = bp ? bp->b_bn : XFS_BUF_DADDR_NULL;
3316 numrecs = xfs_btree_get_numrecs(block);
3319 error = xfs_btree_check_block(cur, block, level, bp);
3323 /* Check that the new entry is being inserted in the right place. */
3324 if (ptr <= numrecs) {
3326 ASSERT(cur->bc_ops->recs_inorder(cur, rec,
3327 xfs_btree_rec_addr(cur, ptr, block)));
3329 ASSERT(cur->bc_ops->keys_inorder(cur, key,
3330 xfs_btree_key_addr(cur, ptr, block)));
3336 * If the block is full, we can't insert the new entry until we
3337 * make the block un-full.
3339 xfs_btree_set_ptr_null(cur, &nptr);
3340 if (numrecs == cur->bc_ops->get_maxrecs(cur, level)) {
3341 error = xfs_btree_make_block_unfull(cur, level, numrecs,
3342 &optr, &ptr, &nptr, &ncur, lkey, stat);
3343 if (error || *stat == 0)
3348 * The current block may have changed if the block was
3349 * previously full and we have just made space in it.
3351 block = xfs_btree_get_block(cur, level, &bp);
3352 numrecs = xfs_btree_get_numrecs(block);
3355 error = xfs_btree_check_block(cur, block, level, bp);
3361 * At this point we know there's room for our new entry in the block
3362 * we're pointing at.
3364 XFS_BTREE_STATS_ADD(cur, moves, numrecs - ptr + 1);
3367 /* It's a nonleaf. make a hole in the keys and ptrs */
3368 union xfs_btree_key *kp;
3369 union xfs_btree_ptr *pp;
3371 kp = xfs_btree_key_addr(cur, ptr, block);
3372 pp = xfs_btree_ptr_addr(cur, ptr, block);
3375 for (i = numrecs - ptr; i >= 0; i--) {
3376 error = xfs_btree_check_ptr(cur, pp, i, level);
3382 xfs_btree_shift_keys(cur, kp, 1, numrecs - ptr + 1);
3383 xfs_btree_shift_ptrs(cur, pp, 1, numrecs - ptr + 1);
3386 error = xfs_btree_check_ptr(cur, ptrp, 0, level);
3391 /* Now put the new data in, bump numrecs and log it. */
3392 xfs_btree_copy_keys(cur, kp, key, 1);
3393 xfs_btree_copy_ptrs(cur, pp, ptrp, 1);
3395 xfs_btree_set_numrecs(block, numrecs);
3396 xfs_btree_log_ptrs(cur, bp, ptr, numrecs);
3397 xfs_btree_log_keys(cur, bp, ptr, numrecs);
3399 if (ptr < numrecs) {
3400 ASSERT(cur->bc_ops->keys_inorder(cur, kp,
3401 xfs_btree_key_addr(cur, ptr + 1, block)));
3405 /* It's a leaf. make a hole in the records */
3406 union xfs_btree_rec *rp;
3408 rp = xfs_btree_rec_addr(cur, ptr, block);
3410 xfs_btree_shift_recs(cur, rp, 1, numrecs - ptr + 1);
3412 /* Now put the new data in, bump numrecs and log it. */
3413 xfs_btree_copy_recs(cur, rp, rec, 1);
3414 xfs_btree_set_numrecs(block, ++numrecs);
3415 xfs_btree_log_recs(cur, bp, ptr, numrecs);
3417 if (ptr < numrecs) {
3418 ASSERT(cur->bc_ops->recs_inorder(cur, rp,
3419 xfs_btree_rec_addr(cur, ptr + 1, block)));
3424 /* Log the new number of records in the btree header. */
3425 xfs_btree_log_block(cur, bp, XFS_BB_NUMRECS);
3428 * If we just inserted into a new tree block, we have to
3429 * recalculate nkey here because nkey is out of date.
3431 * Otherwise we're just updating an existing block (having shoved
3432 * some records into the new tree block), so use the regular key
3435 if (bp && bp->b_bn != old_bn) {
3436 xfs_btree_get_keys(cur, block, lkey);
3437 } else if (xfs_btree_needs_key_update(cur, optr)) {
3438 error = xfs_btree_update_keys(cur, level);
3444 * If we are tracking the last record in the tree and
3445 * we are at the far right edge of the tree, update it.
3447 if (xfs_btree_is_lastrec(cur, block, level)) {
3448 cur->bc_ops->update_lastrec(cur, block, rec,
3449 ptr, LASTREC_INSREC);
3453 * Return the new block number, if any.
3454 * If there is one, give back a record value and a cursor too.
3457 if (!xfs_btree_ptr_is_null(cur, &nptr)) {
3458 xfs_btree_copy_keys(cur, key, lkey, 1);
3462 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3467 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
3472 * Insert the record at the point referenced by cur.
3474 * A multi-level split of the tree on insert will invalidate the original
3475 * cursor. All callers of this function should assume that the cursor is
3476 * no longer valid and revalidate it.
3480 struct xfs_btree_cur *cur,
3483 int error; /* error return value */
3484 int i; /* result value, 0 for failure */
3485 int level; /* current level number in btree */
3486 union xfs_btree_ptr nptr; /* new block number (split result) */
3487 struct xfs_btree_cur *ncur; /* new cursor (split result) */
3488 struct xfs_btree_cur *pcur; /* previous level's cursor */
3489 union xfs_btree_key bkey; /* key of block to insert */
3490 union xfs_btree_key *key;
3491 union xfs_btree_rec rec; /* record to insert */
3498 xfs_btree_set_ptr_null(cur, &nptr);
3500 /* Make a key out of the record data to be inserted, and save it. */
3501 cur->bc_ops->init_rec_from_cur(cur, &rec);
3502 cur->bc_ops->init_key_from_rec(key, &rec);
3505 * Loop going up the tree, starting at the leaf level.
3506 * Stop when we don't get a split block, that must mean that
3507 * the insert is finished with this level.
3511 * Insert nrec/nptr into this level of the tree.
3512 * Note if we fail, nptr will be null.
3514 error = xfs_btree_insrec(pcur, level, &nptr, &rec, key,
3518 xfs_btree_del_cursor(pcur, XFS_BTREE_ERROR);
3522 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, i == 1, error0);
3526 * See if the cursor we just used is trash.
3527 * Can't trash the caller's cursor, but otherwise we should
3528 * if ncur is a new cursor or we're about to be done.
3531 (ncur || xfs_btree_ptr_is_null(cur, &nptr))) {
3532 /* Save the state from the cursor before we trash it */
3533 if (cur->bc_ops->update_cursor)
3534 cur->bc_ops->update_cursor(pcur, cur);
3535 cur->bc_nlevels = pcur->bc_nlevels;
3536 xfs_btree_del_cursor(pcur, XFS_BTREE_NOERROR);
3538 /* If we got a new cursor, switch to it. */
3543 } while (!xfs_btree_ptr_is_null(cur, &nptr));
3545 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3549 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
3554 * Try to merge a non-leaf block back into the inode root.
3556 * Note: the killroot names comes from the fact that we're effectively
3557 * killing the old root block. But because we can't just delete the
3558 * inode we have to copy the single block it was pointing to into the
3562 xfs_btree_kill_iroot(
3563 struct xfs_btree_cur *cur)
3565 int whichfork = cur->bc_private.b.whichfork;
3566 struct xfs_inode *ip = cur->bc_private.b.ip;
3567 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
3568 struct xfs_btree_block *block;
3569 struct xfs_btree_block *cblock;
3570 union xfs_btree_key *kp;
3571 union xfs_btree_key *ckp;
3572 union xfs_btree_ptr *pp;
3573 union xfs_btree_ptr *cpp;
3574 struct xfs_buf *cbp;
3580 union xfs_btree_ptr ptr;
3584 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
3586 ASSERT(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE);
3587 ASSERT(cur->bc_nlevels > 1);
3590 * Don't deal with the root block needs to be a leaf case.
3591 * We're just going to turn the thing back into extents anyway.
3593 level = cur->bc_nlevels - 1;
3598 * Give up if the root has multiple children.
3600 block = xfs_btree_get_iroot(cur);
3601 if (xfs_btree_get_numrecs(block) != 1)
3604 cblock = xfs_btree_get_block(cur, level - 1, &cbp);
3605 numrecs = xfs_btree_get_numrecs(cblock);
3608 * Only do this if the next level will fit.
3609 * Then the data must be copied up to the inode,
3610 * instead of freeing the root you free the next level.
3612 if (numrecs > cur->bc_ops->get_dmaxrecs(cur, level))
3615 XFS_BTREE_STATS_INC(cur, killroot);
3618 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_LEFTSIB);
3619 ASSERT(xfs_btree_ptr_is_null(cur, &ptr));
3620 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_RIGHTSIB);
3621 ASSERT(xfs_btree_ptr_is_null(cur, &ptr));
3624 index = numrecs - cur->bc_ops->get_maxrecs(cur, level);
3626 xfs_iroot_realloc(cur->bc_private.b.ip, index,
3627 cur->bc_private.b.whichfork);
3628 block = ifp->if_broot;
3631 be16_add_cpu(&block->bb_numrecs, index);
3632 ASSERT(block->bb_numrecs == cblock->bb_numrecs);
3634 kp = xfs_btree_key_addr(cur, 1, block);
3635 ckp = xfs_btree_key_addr(cur, 1, cblock);
3636 xfs_btree_copy_keys(cur, kp, ckp, numrecs);
3638 pp = xfs_btree_ptr_addr(cur, 1, block);
3639 cpp = xfs_btree_ptr_addr(cur, 1, cblock);
3641 for (i = 0; i < numrecs; i++) {
3642 error = xfs_btree_check_ptr(cur, cpp, i, level - 1);
3644 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
3649 xfs_btree_copy_ptrs(cur, pp, cpp, numrecs);
3651 error = xfs_btree_free_block(cur, cbp);
3653 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
3657 cur->bc_bufs[level - 1] = NULL;
3658 be16_add_cpu(&block->bb_level, -1);
3659 xfs_trans_log_inode(cur->bc_tp, ip,
3660 XFS_ILOG_CORE | xfs_ilog_fbroot(cur->bc_private.b.whichfork));
3663 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3668 * Kill the current root node, and replace it with it's only child node.
3671 xfs_btree_kill_root(
3672 struct xfs_btree_cur *cur,
3675 union xfs_btree_ptr *newroot)
3679 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
3680 XFS_BTREE_STATS_INC(cur, killroot);
3683 * Update the root pointer, decreasing the level by 1 and then
3684 * free the old root.
3686 cur->bc_ops->set_root(cur, newroot, -1);
3688 error = xfs_btree_free_block(cur, bp);
3690 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
3694 cur->bc_bufs[level] = NULL;
3695 cur->bc_ra[level] = 0;
3698 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3703 xfs_btree_dec_cursor(
3704 struct xfs_btree_cur *cur,
3712 error = xfs_btree_decrement(cur, level, &i);
3717 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3723 * Single level of the btree record deletion routine.
3724 * Delete record pointed to by cur/level.
3725 * Remove the record from its block then rebalance the tree.
3726 * Return 0 for error, 1 for done, 2 to go on to the next level.
3728 STATIC int /* error */
3730 struct xfs_btree_cur *cur, /* btree cursor */
3731 int level, /* level removing record from */
3732 int *stat) /* fail/done/go-on */
3734 struct xfs_btree_block *block; /* btree block */
3735 union xfs_btree_ptr cptr; /* current block ptr */
3736 struct xfs_buf *bp; /* buffer for block */
3737 int error; /* error return value */
3738 int i; /* loop counter */
3739 union xfs_btree_ptr lptr; /* left sibling block ptr */
3740 struct xfs_buf *lbp; /* left buffer pointer */
3741 struct xfs_btree_block *left; /* left btree block */
3742 int lrecs = 0; /* left record count */
3743 int ptr; /* key/record index */
3744 union xfs_btree_ptr rptr; /* right sibling block ptr */
3745 struct xfs_buf *rbp; /* right buffer pointer */
3746 struct xfs_btree_block *right; /* right btree block */
3747 struct xfs_btree_block *rrblock; /* right-right btree block */
3748 struct xfs_buf *rrbp; /* right-right buffer pointer */
3749 int rrecs = 0; /* right record count */
3750 struct xfs_btree_cur *tcur; /* temporary btree cursor */
3751 int numrecs; /* temporary numrec count */
3753 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
3754 XFS_BTREE_TRACE_ARGI(cur, level);
3758 /* Get the index of the entry being deleted, check for nothing there. */
3759 ptr = cur->bc_ptrs[level];
3761 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3766 /* Get the buffer & block containing the record or key/ptr. */
3767 block = xfs_btree_get_block(cur, level, &bp);
3768 numrecs = xfs_btree_get_numrecs(block);
3771 error = xfs_btree_check_block(cur, block, level, bp);
3776 /* Fail if we're off the end of the block. */
3777 if (ptr > numrecs) {
3778 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3783 XFS_BTREE_STATS_INC(cur, delrec);
3784 XFS_BTREE_STATS_ADD(cur, moves, numrecs - ptr);
3786 /* Excise the entries being deleted. */
3788 /* It's a nonleaf. operate on keys and ptrs */
3789 union xfs_btree_key *lkp;
3790 union xfs_btree_ptr *lpp;
3792 lkp = xfs_btree_key_addr(cur, ptr + 1, block);
3793 lpp = xfs_btree_ptr_addr(cur, ptr + 1, block);
3796 for (i = 0; i < numrecs - ptr; i++) {
3797 error = xfs_btree_check_ptr(cur, lpp, i, level);
3803 if (ptr < numrecs) {
3804 xfs_btree_shift_keys(cur, lkp, -1, numrecs - ptr);
3805 xfs_btree_shift_ptrs(cur, lpp, -1, numrecs - ptr);
3806 xfs_btree_log_keys(cur, bp, ptr, numrecs - 1);
3807 xfs_btree_log_ptrs(cur, bp, ptr, numrecs - 1);
3810 /* It's a leaf. operate on records */
3811 if (ptr < numrecs) {
3812 xfs_btree_shift_recs(cur,
3813 xfs_btree_rec_addr(cur, ptr + 1, block),
3815 xfs_btree_log_recs(cur, bp, ptr, numrecs - 1);
3820 * Decrement and log the number of entries in the block.
3822 xfs_btree_set_numrecs(block, --numrecs);
3823 xfs_btree_log_block(cur, bp, XFS_BB_NUMRECS);
3826 * If we are tracking the last record in the tree and
3827 * we are at the far right edge of the tree, update it.
3829 if (xfs_btree_is_lastrec(cur, block, level)) {
3830 cur->bc_ops->update_lastrec(cur, block, NULL,
3831 ptr, LASTREC_DELREC);
3835 * We're at the root level. First, shrink the root block in-memory.
3836 * Try to get rid of the next level down. If we can't then there's
3837 * nothing left to do.
3839 if (level == cur->bc_nlevels - 1) {
3840 if (cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) {
3841 xfs_iroot_realloc(cur->bc_private.b.ip, -1,
3842 cur->bc_private.b.whichfork);
3844 error = xfs_btree_kill_iroot(cur);
3848 error = xfs_btree_dec_cursor(cur, level, stat);
3856 * If this is the root level, and there's only one entry left,
3857 * and it's NOT the leaf level, then we can get rid of this
3860 if (numrecs == 1 && level > 0) {
3861 union xfs_btree_ptr *pp;
3863 * pp is still set to the first pointer in the block.
3864 * Make it the new root of the btree.
3866 pp = xfs_btree_ptr_addr(cur, 1, block);
3867 error = xfs_btree_kill_root(cur, bp, level, pp);
3870 } else if (level > 0) {
3871 error = xfs_btree_dec_cursor(cur, level, stat);
3880 * If we deleted the leftmost entry in the block, update the
3881 * key values above us in the tree.
3883 if (xfs_btree_needs_key_update(cur, ptr)) {
3884 error = xfs_btree_update_keys(cur, level);
3890 * If the number of records remaining in the block is at least
3891 * the minimum, we're done.
3893 if (numrecs >= cur->bc_ops->get_minrecs(cur, level)) {
3894 error = xfs_btree_dec_cursor(cur, level, stat);
3901 * Otherwise, we have to move some records around to keep the
3902 * tree balanced. Look at the left and right sibling blocks to
3903 * see if we can re-balance by moving only one record.
3905 xfs_btree_get_sibling(cur, block, &rptr, XFS_BB_RIGHTSIB);
3906 xfs_btree_get_sibling(cur, block, &lptr, XFS_BB_LEFTSIB);
3908 if (cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) {
3910 * One child of root, need to get a chance to copy its contents
3911 * into the root and delete it. Can't go up to next level,
3912 * there's nothing to delete there.
3914 if (xfs_btree_ptr_is_null(cur, &rptr) &&
3915 xfs_btree_ptr_is_null(cur, &lptr) &&
3916 level == cur->bc_nlevels - 2) {
3917 error = xfs_btree_kill_iroot(cur);
3919 error = xfs_btree_dec_cursor(cur, level, stat);
3926 ASSERT(!xfs_btree_ptr_is_null(cur, &rptr) ||
3927 !xfs_btree_ptr_is_null(cur, &lptr));
3930 * Duplicate the cursor so our btree manipulations here won't
3931 * disrupt the next level up.
3933 error = xfs_btree_dup_cursor(cur, &tcur);
3938 * If there's a right sibling, see if it's ok to shift an entry
3941 if (!xfs_btree_ptr_is_null(cur, &rptr)) {
3943 * Move the temp cursor to the last entry in the next block.
3944 * Actually any entry but the first would suffice.
3946 i = xfs_btree_lastrec(tcur, level);
3947 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, i == 1, error0);
3949 error = xfs_btree_increment(tcur, level, &i);
3952 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, i == 1, error0);
3954 i = xfs_btree_lastrec(tcur, level);
3955 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, i == 1, error0);
3957 /* Grab a pointer to the block. */
3958 right = xfs_btree_get_block(tcur, level, &rbp);
3960 error = xfs_btree_check_block(tcur, right, level, rbp);
3964 /* Grab the current block number, for future use. */
3965 xfs_btree_get_sibling(tcur, right, &cptr, XFS_BB_LEFTSIB);
3968 * If right block is full enough so that removing one entry
3969 * won't make it too empty, and left-shifting an entry out
3970 * of right to us works, we're done.
3972 if (xfs_btree_get_numrecs(right) - 1 >=
3973 cur->bc_ops->get_minrecs(tcur, level)) {
3974 error = xfs_btree_lshift(tcur, level, &i);
3978 ASSERT(xfs_btree_get_numrecs(block) >=
3979 cur->bc_ops->get_minrecs(tcur, level));
3981 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
3984 error = xfs_btree_dec_cursor(cur, level, stat);
3992 * Otherwise, grab the number of records in right for
3993 * future reference, and fix up the temp cursor to point
3994 * to our block again (last record).
3996 rrecs = xfs_btree_get_numrecs(right);
3997 if (!xfs_btree_ptr_is_null(cur, &lptr)) {
3998 i = xfs_btree_firstrec(tcur, level);
3999 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, i == 1, error0);
4001 error = xfs_btree_decrement(tcur, level, &i);
4004 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, i == 1, error0);
4009 * If there's a left sibling, see if it's ok to shift an entry
4012 if (!xfs_btree_ptr_is_null(cur, &lptr)) {
4014 * Move the temp cursor to the first entry in the
4017 i = xfs_btree_firstrec(tcur, level);
4018 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, i == 1, error0);
4020 error = xfs_btree_decrement(tcur, level, &i);
4023 i = xfs_btree_firstrec(tcur, level);
4024 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, i == 1, error0);
4026 /* Grab a pointer to the block. */
4027 left = xfs_btree_get_block(tcur, level, &lbp);
4029 error = xfs_btree_check_block(cur, left, level, lbp);
4033 /* Grab the current block number, for future use. */
4034 xfs_btree_get_sibling(tcur, left, &cptr, XFS_BB_RIGHTSIB);
4037 * If left block is full enough so that removing one entry
4038 * won't make it too empty, and right-shifting an entry out
4039 * of left to us works, we're done.
4041 if (xfs_btree_get_numrecs(left) - 1 >=
4042 cur->bc_ops->get_minrecs(tcur, level)) {
4043 error = xfs_btree_rshift(tcur, level, &i);
4047 ASSERT(xfs_btree_get_numrecs(block) >=
4048 cur->bc_ops->get_minrecs(tcur, level));
4049 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
4053 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
4060 * Otherwise, grab the number of records in right for
4063 lrecs = xfs_btree_get_numrecs(left);
4066 /* Delete the temp cursor, we're done with it. */
4067 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
4070 /* If here, we need to do a join to keep the tree balanced. */
4071 ASSERT(!xfs_btree_ptr_is_null(cur, &cptr));
4073 if (!xfs_btree_ptr_is_null(cur, &lptr) &&
4074 lrecs + xfs_btree_get_numrecs(block) <=
4075 cur->bc_ops->get_maxrecs(cur, level)) {
4077 * Set "right" to be the starting block,
4078 * "left" to be the left neighbor.
4083 error = xfs_btree_read_buf_block(cur, &lptr, 0, &left, &lbp);
4088 * If that won't work, see if we can join with the right neighbor block.
4090 } else if (!xfs_btree_ptr_is_null(cur, &rptr) &&
4091 rrecs + xfs_btree_get_numrecs(block) <=
4092 cur->bc_ops->get_maxrecs(cur, level)) {
4094 * Set "left" to be the starting block,
4095 * "right" to be the right neighbor.
4100 error = xfs_btree_read_buf_block(cur, &rptr, 0, &right, &rbp);
4105 * Otherwise, we can't fix the imbalance.
4106 * Just return. This is probably a logic error, but it's not fatal.
4109 error = xfs_btree_dec_cursor(cur, level, stat);
4115 rrecs = xfs_btree_get_numrecs(right);
4116 lrecs = xfs_btree_get_numrecs(left);
4119 * We're now going to join "left" and "right" by moving all the stuff
4120 * in "right" to "left" and deleting "right".
4122 XFS_BTREE_STATS_ADD(cur, moves, rrecs);
4124 /* It's a non-leaf. Move keys and pointers. */
4125 union xfs_btree_key *lkp; /* left btree key */
4126 union xfs_btree_ptr *lpp; /* left address pointer */
4127 union xfs_btree_key *rkp; /* right btree key */
4128 union xfs_btree_ptr *rpp; /* right address pointer */
4130 lkp = xfs_btree_key_addr(cur, lrecs + 1, left);
4131 lpp = xfs_btree_ptr_addr(cur, lrecs + 1, left);
4132 rkp = xfs_btree_key_addr(cur, 1, right);
4133 rpp = xfs_btree_ptr_addr(cur, 1, right);
4135 for (i = 1; i < rrecs; i++) {
4136 error = xfs_btree_check_ptr(cur, rpp, i, level);
4141 xfs_btree_copy_keys(cur, lkp, rkp, rrecs);
4142 xfs_btree_copy_ptrs(cur, lpp, rpp, rrecs);
4144 xfs_btree_log_keys(cur, lbp, lrecs + 1, lrecs + rrecs);
4145 xfs_btree_log_ptrs(cur, lbp, lrecs + 1, lrecs + rrecs);
4147 /* It's a leaf. Move records. */
4148 union xfs_btree_rec *lrp; /* left record pointer */
4149 union xfs_btree_rec *rrp; /* right record pointer */
4151 lrp = xfs_btree_rec_addr(cur, lrecs + 1, left);
4152 rrp = xfs_btree_rec_addr(cur, 1, right);
4154 xfs_btree_copy_recs(cur, lrp, rrp, rrecs);
4155 xfs_btree_log_recs(cur, lbp, lrecs + 1, lrecs + rrecs);
4158 XFS_BTREE_STATS_INC(cur, join);
4161 * Fix up the number of records and right block pointer in the
4162 * surviving block, and log it.
4164 xfs_btree_set_numrecs(left, lrecs + rrecs);
4165 xfs_btree_get_sibling(cur, right, &cptr, XFS_BB_RIGHTSIB),
4166 xfs_btree_set_sibling(cur, left, &cptr, XFS_BB_RIGHTSIB);
4167 xfs_btree_log_block(cur, lbp, XFS_BB_NUMRECS | XFS_BB_RIGHTSIB);
4169 /* If there is a right sibling, point it to the remaining block. */
4170 xfs_btree_get_sibling(cur, left, &cptr, XFS_BB_RIGHTSIB);
4171 if (!xfs_btree_ptr_is_null(cur, &cptr)) {
4172 error = xfs_btree_read_buf_block(cur, &cptr, 0, &rrblock, &rrbp);
4175 xfs_btree_set_sibling(cur, rrblock, &lptr, XFS_BB_LEFTSIB);
4176 xfs_btree_log_block(cur, rrbp, XFS_BB_LEFTSIB);
4179 /* Free the deleted block. */
4180 error = xfs_btree_free_block(cur, rbp);
4185 * If we joined with the left neighbor, set the buffer in the
4186 * cursor to the left block, and fix up the index.
4189 cur->bc_bufs[level] = lbp;
4190 cur->bc_ptrs[level] += lrecs;
4191 cur->bc_ra[level] = 0;
4194 * If we joined with the right neighbor and there's a level above
4195 * us, increment the cursor at that level.
4197 else if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) ||
4198 (level + 1 < cur->bc_nlevels)) {
4199 error = xfs_btree_increment(cur, level + 1, &i);
4205 * Readjust the ptr at this level if it's not a leaf, since it's
4206 * still pointing at the deletion point, which makes the cursor
4207 * inconsistent. If this makes the ptr 0, the caller fixes it up.
4208 * We can't use decrement because it would change the next level up.
4211 cur->bc_ptrs[level]--;
4214 * We combined blocks, so we have to update the parent keys if the
4215 * btree supports overlapped intervals. However, bc_ptrs[level + 1]
4216 * points to the old block so that the caller knows which record to
4217 * delete. Therefore, the caller must be savvy enough to call updkeys
4218 * for us if we return stat == 2. The other exit points from this
4219 * function don't require deletions further up the tree, so they can
4220 * call updkeys directly.
4223 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
4224 /* Return value means the next level up has something to do. */
4229 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
4231 xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
4236 * Delete the record pointed to by cur.
4237 * The cursor refers to the place where the record was (could be inserted)
4238 * when the operation returns.
4242 struct xfs_btree_cur *cur,
4243 int *stat) /* success/failure */
4245 int error; /* error return value */
4248 bool joined = false;
4250 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
4253 * Go up the tree, starting at leaf level.
4255 * If 2 is returned then a join was done; go to the next level.
4256 * Otherwise we are done.
4258 for (level = 0, i = 2; i == 2; level++) {
4259 error = xfs_btree_delrec(cur, level, &i);
4267 * If we combined blocks as part of deleting the record, delrec won't
4268 * have updated the parent high keys so we have to do that here.
4270 if (joined && (cur->bc_flags & XFS_BTREE_OVERLAPPING)) {
4271 error = xfs_btree_updkeys_force(cur, 0);
4277 for (level = 1; level < cur->bc_nlevels; level++) {
4278 if (cur->bc_ptrs[level] == 0) {
4279 error = xfs_btree_decrement(cur, level, &i);
4287 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
4291 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
4296 * Get the data from the pointed-to record.
4300 struct xfs_btree_cur *cur, /* btree cursor */
4301 union xfs_btree_rec **recp, /* output: btree record */
4302 int *stat) /* output: success/failure */
4304 struct xfs_btree_block *block; /* btree block */
4305 struct xfs_buf *bp; /* buffer pointer */
4306 int ptr; /* record number */
4308 int error; /* error return value */
4311 ptr = cur->bc_ptrs[0];
4312 block = xfs_btree_get_block(cur, 0, &bp);
4315 error = xfs_btree_check_block(cur, block, 0, bp);
4321 * Off the right end or left end, return failure.
4323 if (ptr > xfs_btree_get_numrecs(block) || ptr <= 0) {
4329 * Point to the record and extract its data.
4331 *recp = xfs_btree_rec_addr(cur, ptr, block);
4336 /* Visit a block in a btree. */
4338 xfs_btree_visit_block(
4339 struct xfs_btree_cur *cur,
4341 xfs_btree_visit_blocks_fn fn,
4344 struct xfs_btree_block *block;
4346 union xfs_btree_ptr rptr;
4349 /* do right sibling readahead */
4350 xfs_btree_readahead(cur, level, XFS_BTCUR_RIGHTRA);
4351 block = xfs_btree_get_block(cur, level, &bp);
4353 /* process the block */
4354 error = fn(cur, level, data);
4358 /* now read rh sibling block for next iteration */
4359 xfs_btree_get_sibling(cur, block, &rptr, XFS_BB_RIGHTSIB);
4360 if (xfs_btree_ptr_is_null(cur, &rptr))
4363 return xfs_btree_lookup_get_block(cur, level, &rptr, &block);
4367 /* Visit every block in a btree. */
4369 xfs_btree_visit_blocks(
4370 struct xfs_btree_cur *cur,
4371 xfs_btree_visit_blocks_fn fn,
4374 union xfs_btree_ptr lptr;
4376 struct xfs_btree_block *block = NULL;
4379 cur->bc_ops->init_ptr_from_cur(cur, &lptr);
4381 /* for each level */
4382 for (level = cur->bc_nlevels - 1; level >= 0; level--) {
4383 /* grab the left hand block */
4384 error = xfs_btree_lookup_get_block(cur, level, &lptr, &block);
4388 /* readahead the left most block for the next level down */
4390 union xfs_btree_ptr *ptr;
4392 ptr = xfs_btree_ptr_addr(cur, 1, block);
4393 xfs_btree_readahead_ptr(cur, ptr, 1);
4395 /* save for the next iteration of the loop */
4396 xfs_btree_copy_ptrs(cur, &lptr, ptr, 1);
4399 /* for each buffer in the level */
4401 error = xfs_btree_visit_block(cur, level, fn, data);
4404 if (error != -ENOENT)
4412 * Change the owner of a btree.
4414 * The mechanism we use here is ordered buffer logging. Because we don't know
4415 * how many buffers were are going to need to modify, we don't really want to
4416 * have to make transaction reservations for the worst case of every buffer in a
4417 * full size btree as that may be more space that we can fit in the log....
4419 * We do the btree walk in the most optimal manner possible - we have sibling
4420 * pointers so we can just walk all the blocks on each level from left to right
4421 * in a single pass, and then move to the next level and do the same. We can
4422 * also do readahead on the sibling pointers to get IO moving more quickly,
4423 * though for slow disks this is unlikely to make much difference to performance
4424 * as the amount of CPU work we have to do before moving to the next block is
4427 * For each btree block that we load, modify the owner appropriately, set the
4428 * buffer as an ordered buffer and log it appropriately. We need to ensure that
4429 * we mark the region we change dirty so that if the buffer is relogged in
4430 * a subsequent transaction the changes we make here as an ordered buffer are
4431 * correctly relogged in that transaction. If we are in recovery context, then
4432 * just queue the modified buffer as delayed write buffer so the transaction
4433 * recovery completion writes the changes to disk.
4435 struct xfs_btree_block_change_owner_info {
4437 struct list_head *buffer_list;
4441 xfs_btree_block_change_owner(
4442 struct xfs_btree_cur *cur,
4446 struct xfs_btree_block_change_owner_info *bbcoi = data;
4447 struct xfs_btree_block *block;
4450 /* modify the owner */
4451 block = xfs_btree_get_block(cur, level, &bp);
4452 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
4453 block->bb_u.l.bb_owner = cpu_to_be64(bbcoi->new_owner);
4455 block->bb_u.s.bb_owner = cpu_to_be32(bbcoi->new_owner);
4458 * If the block is a root block hosted in an inode, we might not have a
4459 * buffer pointer here and we shouldn't attempt to log the change as the
4460 * information is already held in the inode and discarded when the root
4461 * block is formatted into the on-disk inode fork. We still change it,
4462 * though, so everything is consistent in memory.
4466 xfs_trans_ordered_buf(cur->bc_tp, bp);
4467 xfs_btree_log_block(cur, bp, XFS_BB_OWNER);
4469 xfs_buf_delwri_queue(bp, bbcoi->buffer_list);
4472 ASSERT(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE);
4473 ASSERT(level == cur->bc_nlevels - 1);
4480 xfs_btree_change_owner(
4481 struct xfs_btree_cur *cur,
4483 struct list_head *buffer_list)
4485 struct xfs_btree_block_change_owner_info bbcoi;
4487 bbcoi.new_owner = new_owner;
4488 bbcoi.buffer_list = buffer_list;
4490 return xfs_btree_visit_blocks(cur, xfs_btree_block_change_owner,
4495 * xfs_btree_sblock_v5hdr_verify() -- verify the v5 fields of a short-format
4498 * @bp: buffer containing the btree block
4499 * @max_recs: pointer to the m_*_mxr max records field in the xfs mount
4500 * @pag_max_level: pointer to the per-ag max level field
4503 xfs_btree_sblock_v5hdr_verify(
4506 struct xfs_mount *mp = bp->b_target->bt_mount;
4507 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
4508 struct xfs_perag *pag = bp->b_pag;
4510 if (!xfs_sb_version_hascrc(&mp->m_sb))
4512 if (!uuid_equal(&block->bb_u.s.bb_uuid, &mp->m_sb.sb_meta_uuid))
4514 if (block->bb_u.s.bb_blkno != cpu_to_be64(bp->b_bn))
4516 if (pag && be32_to_cpu(block->bb_u.s.bb_owner) != pag->pag_agno)
4522 * xfs_btree_sblock_verify() -- verify a short-format btree block
4524 * @bp: buffer containing the btree block
4525 * @max_recs: maximum records allowed in this btree node
4528 xfs_btree_sblock_verify(
4530 unsigned int max_recs)
4532 struct xfs_mount *mp = bp->b_target->bt_mount;
4533 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
4535 /* numrecs verification */
4536 if (be16_to_cpu(block->bb_numrecs) > max_recs)
4539 /* sibling pointer verification */
4540 if (!block->bb_u.s.bb_leftsib ||
4541 (be32_to_cpu(block->bb_u.s.bb_leftsib) >= mp->m_sb.sb_agblocks &&
4542 block->bb_u.s.bb_leftsib != cpu_to_be32(NULLAGBLOCK)))
4544 if (!block->bb_u.s.bb_rightsib ||
4545 (be32_to_cpu(block->bb_u.s.bb_rightsib) >= mp->m_sb.sb_agblocks &&
4546 block->bb_u.s.bb_rightsib != cpu_to_be32(NULLAGBLOCK)))
4553 * Calculate the number of btree levels needed to store a given number of
4554 * records in a short-format btree.
4557 xfs_btree_compute_maxlevels(
4558 struct xfs_mount *mp,
4563 unsigned long maxblocks;
4565 maxblocks = (len + limits[0] - 1) / limits[0];
4566 for (level = 1; maxblocks > 1; level++)
4567 maxblocks = (maxblocks + limits[1] - 1) / limits[1];
4572 * Query a regular btree for all records overlapping a given interval.
4573 * Start with a LE lookup of the key of low_rec and return all records
4574 * until we find a record with a key greater than the key of high_rec.
4577 xfs_btree_simple_query_range(
4578 struct xfs_btree_cur *cur,
4579 union xfs_btree_key *low_key,
4580 union xfs_btree_key *high_key,
4581 xfs_btree_query_range_fn fn,
4584 union xfs_btree_rec *recp;
4585 union xfs_btree_key rec_key;
4588 bool firstrec = true;
4591 ASSERT(cur->bc_ops->init_high_key_from_rec);
4592 ASSERT(cur->bc_ops->diff_two_keys);
4595 * Find the leftmost record. The btree cursor must be set
4596 * to the low record used to generate low_key.
4599 error = xfs_btree_lookup(cur, XFS_LOOKUP_LE, &stat);
4603 /* Nothing? See if there's anything to the right. */
4605 error = xfs_btree_increment(cur, 0, &stat);
4611 /* Find the record. */
4612 error = xfs_btree_get_rec(cur, &recp, &stat);
4616 /* Skip if high_key(rec) < low_key. */
4618 cur->bc_ops->init_high_key_from_rec(&rec_key, recp);
4620 diff = cur->bc_ops->diff_two_keys(cur, low_key,
4626 /* Stop if high_key < low_key(rec). */
4627 cur->bc_ops->init_key_from_rec(&rec_key, recp);
4628 diff = cur->bc_ops->diff_two_keys(cur, &rec_key, high_key);
4633 error = fn(cur, recp, priv);
4634 if (error < 0 || error == XFS_BTREE_QUERY_RANGE_ABORT)
4638 /* Move on to the next record. */
4639 error = xfs_btree_increment(cur, 0, &stat);
4649 * Query an overlapped interval btree for all records overlapping a given
4650 * interval. This function roughly follows the algorithm given in
4651 * "Interval Trees" of _Introduction to Algorithms_, which is section
4652 * 14.3 in the 2nd and 3rd editions.
4654 * First, generate keys for the low and high records passed in.
4656 * For any leaf node, generate the high and low keys for the record.
4657 * If the record keys overlap with the query low/high keys, pass the
4658 * record to the function iterator.
4660 * For any internal node, compare the low and high keys of each
4661 * pointer against the query low/high keys. If there's an overlap,
4662 * follow the pointer.
4664 * As an optimization, we stop scanning a block when we find a low key
4665 * that is greater than the query's high key.
4668 xfs_btree_overlapped_query_range(
4669 struct xfs_btree_cur *cur,
4670 union xfs_btree_key *low_key,
4671 union xfs_btree_key *high_key,
4672 xfs_btree_query_range_fn fn,
4675 union xfs_btree_ptr ptr;
4676 union xfs_btree_ptr *pp;
4677 union xfs_btree_key rec_key;
4678 union xfs_btree_key rec_hkey;
4679 union xfs_btree_key *lkp;
4680 union xfs_btree_key *hkp;
4681 union xfs_btree_rec *recp;
4682 struct xfs_btree_block *block;
4690 /* Load the root of the btree. */
4691 level = cur->bc_nlevels - 1;
4692 cur->bc_ops->init_ptr_from_cur(cur, &ptr);
4693 error = xfs_btree_lookup_get_block(cur, level, &ptr, &block);
4696 xfs_btree_get_block(cur, level, &bp);
4697 trace_xfs_btree_overlapped_query_range(cur, level, bp);
4699 error = xfs_btree_check_block(cur, block, level, bp);
4703 cur->bc_ptrs[level] = 1;
4705 while (level < cur->bc_nlevels) {
4706 block = xfs_btree_get_block(cur, level, &bp);
4708 /* End of node, pop back towards the root. */
4709 if (cur->bc_ptrs[level] > be16_to_cpu(block->bb_numrecs)) {
4711 if (level < cur->bc_nlevels - 1)
4712 cur->bc_ptrs[level + 1]++;
4718 /* Handle a leaf node. */
4719 recp = xfs_btree_rec_addr(cur, cur->bc_ptrs[0], block);
4721 cur->bc_ops->init_high_key_from_rec(&rec_hkey, recp);
4722 ldiff = cur->bc_ops->diff_two_keys(cur, &rec_hkey,
4725 cur->bc_ops->init_key_from_rec(&rec_key, recp);
4726 hdiff = cur->bc_ops->diff_two_keys(cur, high_key,
4730 * If (record's high key >= query's low key) and
4731 * (query's high key >= record's low key), then
4732 * this record overlaps the query range; callback.
4734 if (ldiff >= 0 && hdiff >= 0) {
4735 error = fn(cur, recp, priv);
4737 error == XFS_BTREE_QUERY_RANGE_ABORT)
4739 } else if (hdiff < 0) {
4740 /* Record is larger than high key; pop. */
4743 cur->bc_ptrs[level]++;
4747 /* Handle an internal node. */
4748 lkp = xfs_btree_key_addr(cur, cur->bc_ptrs[level], block);
4749 hkp = xfs_btree_high_key_addr(cur, cur->bc_ptrs[level], block);
4750 pp = xfs_btree_ptr_addr(cur, cur->bc_ptrs[level], block);
4752 ldiff = cur->bc_ops->diff_two_keys(cur, hkp, low_key);
4753 hdiff = cur->bc_ops->diff_two_keys(cur, high_key, lkp);
4756 * If (pointer's high key >= query's low key) and
4757 * (query's high key >= pointer's low key), then
4758 * this record overlaps the query range; follow pointer.
4760 if (ldiff >= 0 && hdiff >= 0) {
4762 error = xfs_btree_lookup_get_block(cur, level, pp,
4766 xfs_btree_get_block(cur, level, &bp);
4767 trace_xfs_btree_overlapped_query_range(cur, level, bp);
4769 error = xfs_btree_check_block(cur, block, level, bp);
4773 cur->bc_ptrs[level] = 1;
4775 } else if (hdiff < 0) {
4776 /* The low key is larger than the upper range; pop. */
4779 cur->bc_ptrs[level]++;
4784 * If we don't end this function with the cursor pointing at a record
4785 * block, a subsequent non-error cursor deletion will not release
4786 * node-level buffers, causing a buffer leak. This is quite possible
4787 * with a zero-results range query, so release the buffers if we
4788 * failed to return any results.
4790 if (cur->bc_bufs[0] == NULL) {
4791 for (i = 0; i < cur->bc_nlevels; i++) {
4792 if (cur->bc_bufs[i]) {
4793 xfs_trans_brelse(cur->bc_tp, cur->bc_bufs[i]);
4794 cur->bc_bufs[i] = NULL;
4795 cur->bc_ptrs[i] = 0;
4805 * Query a btree for all records overlapping a given interval of keys. The
4806 * supplied function will be called with each record found; return one of the
4807 * XFS_BTREE_QUERY_RANGE_{CONTINUE,ABORT} values or the usual negative error
4808 * code. This function returns XFS_BTREE_QUERY_RANGE_ABORT, zero, or a
4809 * negative error code.
4812 xfs_btree_query_range(
4813 struct xfs_btree_cur *cur,
4814 union xfs_btree_irec *low_rec,
4815 union xfs_btree_irec *high_rec,
4816 xfs_btree_query_range_fn fn,
4819 union xfs_btree_rec rec;
4820 union xfs_btree_key low_key;
4821 union xfs_btree_key high_key;
4823 /* Find the keys of both ends of the interval. */
4824 cur->bc_rec = *high_rec;
4825 cur->bc_ops->init_rec_from_cur(cur, &rec);
4826 cur->bc_ops->init_key_from_rec(&high_key, &rec);
4828 cur->bc_rec = *low_rec;
4829 cur->bc_ops->init_rec_from_cur(cur, &rec);
4830 cur->bc_ops->init_key_from_rec(&low_key, &rec);
4832 /* Enforce low key < high key. */
4833 if (cur->bc_ops->diff_two_keys(cur, &low_key, &high_key) > 0)
4836 if (!(cur->bc_flags & XFS_BTREE_OVERLAPPING))
4837 return xfs_btree_simple_query_range(cur, &low_key,
4838 &high_key, fn, priv);
4839 return xfs_btree_overlapped_query_range(cur, &low_key, &high_key,
4843 /* Query a btree for all records. */
4845 xfs_btree_query_all(
4846 struct xfs_btree_cur *cur,
4847 xfs_btree_query_range_fn fn,
4850 union xfs_btree_key low_key;
4851 union xfs_btree_key high_key;
4853 memset(&cur->bc_rec, 0, sizeof(cur->bc_rec));
4854 memset(&low_key, 0, sizeof(low_key));
4855 memset(&high_key, 0xFF, sizeof(high_key));
4857 return xfs_btree_simple_query_range(cur, &low_key, &high_key, fn, priv);
4861 * Calculate the number of blocks needed to store a given number of records
4862 * in a short-format (per-AG metadata) btree.
4865 xfs_btree_calc_size(
4866 struct xfs_mount *mp,
4868 unsigned long long len)
4874 maxrecs = limits[0];
4875 for (level = 0, rval = 0; len > 1; level++) {
4877 do_div(len, maxrecs);
4878 maxrecs = limits[1];
4885 xfs_btree_count_blocks_helper(
4886 struct xfs_btree_cur *cur,
4890 xfs_extlen_t *blocks = data;
4896 /* Count the blocks in a btree and return the result in *blocks. */
4898 xfs_btree_count_blocks(
4899 struct xfs_btree_cur *cur,
4900 xfs_extlen_t *blocks)
4903 return xfs_btree_visit_blocks(cur, xfs_btree_count_blocks_helper,