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"
27 #include "xfs_mount.h"
28 #include "xfs_inode.h"
29 #include "xfs_trans.h"
30 #include "xfs_inode_item.h"
31 #include "xfs_buf_item.h"
32 #include "xfs_btree.h"
33 #include "xfs_error.h"
34 #include "xfs_trace.h"
35 #include "xfs_cksum.h"
36 #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, XFS_BMAP_MAGIC, XFS_IBT_MAGIC,
49 { XFS_ABTB_CRC_MAGIC, XFS_ABTC_CRC_MAGIC,
50 XFS_BMAP_CRC_MAGIC, XFS_IBT_CRC_MAGIC, XFS_FIBT_CRC_MAGIC }
52 #define xfs_btree_magic(cur) \
53 xfs_magics[!!((cur)->bc_flags & XFS_BTREE_CRC_BLOCKS)][cur->bc_btnum]
56 STATIC int /* error (0 or EFSCORRUPTED) */
57 xfs_btree_check_lblock(
58 struct xfs_btree_cur *cur, /* btree cursor */
59 struct xfs_btree_block *block, /* btree long form block pointer */
60 int level, /* level of the btree block */
61 struct xfs_buf *bp) /* buffer for block, if any */
63 int lblock_ok = 1; /* block passes checks */
64 struct xfs_mount *mp; /* file system mount point */
68 if (xfs_sb_version_hascrc(&mp->m_sb)) {
69 lblock_ok = lblock_ok &&
70 uuid_equal(&block->bb_u.l.bb_uuid, &mp->m_sb.sb_uuid) &&
71 block->bb_u.l.bb_blkno == cpu_to_be64(
72 bp ? bp->b_bn : XFS_BUF_DADDR_NULL);
75 lblock_ok = lblock_ok &&
76 be32_to_cpu(block->bb_magic) == xfs_btree_magic(cur) &&
77 be16_to_cpu(block->bb_level) == level &&
78 be16_to_cpu(block->bb_numrecs) <=
79 cur->bc_ops->get_maxrecs(cur, level) &&
80 block->bb_u.l.bb_leftsib &&
81 (block->bb_u.l.bb_leftsib == cpu_to_be64(NULLDFSBNO) ||
82 XFS_FSB_SANITY_CHECK(mp,
83 be64_to_cpu(block->bb_u.l.bb_leftsib))) &&
84 block->bb_u.l.bb_rightsib &&
85 (block->bb_u.l.bb_rightsib == cpu_to_be64(NULLDFSBNO) ||
86 XFS_FSB_SANITY_CHECK(mp,
87 be64_to_cpu(block->bb_u.l.bb_rightsib)));
89 if (unlikely(XFS_TEST_ERROR(!lblock_ok, mp,
90 XFS_ERRTAG_BTREE_CHECK_LBLOCK,
91 XFS_RANDOM_BTREE_CHECK_LBLOCK))) {
93 trace_xfs_btree_corrupt(bp, _RET_IP_);
94 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp);
95 return XFS_ERROR(EFSCORRUPTED);
100 STATIC int /* error (0 or EFSCORRUPTED) */
101 xfs_btree_check_sblock(
102 struct xfs_btree_cur *cur, /* btree cursor */
103 struct xfs_btree_block *block, /* btree short form block pointer */
104 int level, /* level of the btree block */
105 struct xfs_buf *bp) /* buffer containing block */
107 struct xfs_mount *mp; /* file system mount point */
108 struct xfs_buf *agbp; /* buffer for ag. freespace struct */
109 struct xfs_agf *agf; /* ag. freespace structure */
110 xfs_agblock_t agflen; /* native ag. freespace length */
111 int sblock_ok = 1; /* block passes checks */
114 agbp = cur->bc_private.a.agbp;
115 agf = XFS_BUF_TO_AGF(agbp);
116 agflen = be32_to_cpu(agf->agf_length);
118 if (xfs_sb_version_hascrc(&mp->m_sb)) {
119 sblock_ok = sblock_ok &&
120 uuid_equal(&block->bb_u.s.bb_uuid, &mp->m_sb.sb_uuid) &&
121 block->bb_u.s.bb_blkno == cpu_to_be64(
122 bp ? bp->b_bn : XFS_BUF_DADDR_NULL);
125 sblock_ok = sblock_ok &&
126 be32_to_cpu(block->bb_magic) == xfs_btree_magic(cur) &&
127 be16_to_cpu(block->bb_level) == level &&
128 be16_to_cpu(block->bb_numrecs) <=
129 cur->bc_ops->get_maxrecs(cur, level) &&
130 (block->bb_u.s.bb_leftsib == cpu_to_be32(NULLAGBLOCK) ||
131 be32_to_cpu(block->bb_u.s.bb_leftsib) < agflen) &&
132 block->bb_u.s.bb_leftsib &&
133 (block->bb_u.s.bb_rightsib == cpu_to_be32(NULLAGBLOCK) ||
134 be32_to_cpu(block->bb_u.s.bb_rightsib) < agflen) &&
135 block->bb_u.s.bb_rightsib;
137 if (unlikely(XFS_TEST_ERROR(!sblock_ok, mp,
138 XFS_ERRTAG_BTREE_CHECK_SBLOCK,
139 XFS_RANDOM_BTREE_CHECK_SBLOCK))) {
141 trace_xfs_btree_corrupt(bp, _RET_IP_);
142 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp);
143 return XFS_ERROR(EFSCORRUPTED);
149 * Debug routine: check that block header is ok.
152 xfs_btree_check_block(
153 struct xfs_btree_cur *cur, /* btree cursor */
154 struct xfs_btree_block *block, /* generic btree block pointer */
155 int level, /* level of the btree block */
156 struct xfs_buf *bp) /* buffer containing block, if any */
158 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
159 return xfs_btree_check_lblock(cur, block, level, bp);
161 return xfs_btree_check_sblock(cur, block, level, bp);
165 * Check that (long) pointer is ok.
167 int /* error (0 or EFSCORRUPTED) */
168 xfs_btree_check_lptr(
169 struct xfs_btree_cur *cur, /* btree cursor */
170 xfs_dfsbno_t bno, /* btree block disk address */
171 int level) /* btree block level */
173 XFS_WANT_CORRUPTED_RETURN(
176 XFS_FSB_SANITY_CHECK(cur->bc_mp, bno));
182 * Check that (short) pointer is ok.
184 STATIC int /* error (0 or EFSCORRUPTED) */
185 xfs_btree_check_sptr(
186 struct xfs_btree_cur *cur, /* btree cursor */
187 xfs_agblock_t bno, /* btree block disk address */
188 int level) /* btree block level */
190 xfs_agblock_t agblocks = cur->bc_mp->m_sb.sb_agblocks;
192 XFS_WANT_CORRUPTED_RETURN(
194 bno != NULLAGBLOCK &&
201 * Check that block ptr is ok.
203 STATIC int /* error (0 or EFSCORRUPTED) */
205 struct xfs_btree_cur *cur, /* btree cursor */
206 union xfs_btree_ptr *ptr, /* btree block disk address */
207 int index, /* offset from ptr to check */
208 int level) /* btree block level */
210 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
211 return xfs_btree_check_lptr(cur,
212 be64_to_cpu((&ptr->l)[index]), level);
214 return xfs_btree_check_sptr(cur,
215 be32_to_cpu((&ptr->s)[index]), level);
221 * Calculate CRC on the whole btree block and stuff it into the
222 * long-form btree header.
224 * Prior to calculting the CRC, pull the LSN out of the buffer log item and put
225 * it into the buffer so recovery knows what the last modifcation was that made
229 xfs_btree_lblock_calc_crc(
232 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
233 struct xfs_buf_log_item *bip = bp->b_fspriv;
235 if (!xfs_sb_version_hascrc(&bp->b_target->bt_mount->m_sb))
238 block->bb_u.l.bb_lsn = cpu_to_be64(bip->bli_item.li_lsn);
239 xfs_buf_update_cksum(bp, XFS_BTREE_LBLOCK_CRC_OFF);
243 xfs_btree_lblock_verify_crc(
246 if (xfs_sb_version_hascrc(&bp->b_target->bt_mount->m_sb))
247 return xfs_buf_verify_cksum(bp, XFS_BTREE_LBLOCK_CRC_OFF);
253 * Calculate CRC on the whole btree block and stuff it into the
254 * short-form btree header.
256 * Prior to calculting the CRC, pull the LSN out of the buffer log item and put
257 * it into the buffer so recovery knows what the last modifcation was that made
261 xfs_btree_sblock_calc_crc(
264 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
265 struct xfs_buf_log_item *bip = bp->b_fspriv;
267 if (!xfs_sb_version_hascrc(&bp->b_target->bt_mount->m_sb))
270 block->bb_u.s.bb_lsn = cpu_to_be64(bip->bli_item.li_lsn);
271 xfs_buf_update_cksum(bp, XFS_BTREE_SBLOCK_CRC_OFF);
275 xfs_btree_sblock_verify_crc(
278 if (xfs_sb_version_hascrc(&bp->b_target->bt_mount->m_sb))
279 return xfs_buf_verify_cksum(bp, XFS_BTREE_SBLOCK_CRC_OFF);
285 * Delete the btree cursor.
288 xfs_btree_del_cursor(
289 xfs_btree_cur_t *cur, /* btree cursor */
290 int error) /* del because of error */
292 int i; /* btree level */
295 * Clear the buffer pointers, and release the buffers.
296 * If we're doing this in the face of an error, we
297 * need to make sure to inspect all of the entries
298 * in the bc_bufs array for buffers to be unlocked.
299 * This is because some of the btree code works from
300 * level n down to 0, and if we get an error along
301 * the way we won't have initialized all the entries
304 for (i = 0; i < cur->bc_nlevels; i++) {
306 xfs_trans_brelse(cur->bc_tp, cur->bc_bufs[i]);
311 * Can't free a bmap cursor without having dealt with the
312 * allocated indirect blocks' accounting.
314 ASSERT(cur->bc_btnum != XFS_BTNUM_BMAP ||
315 cur->bc_private.b.allocated == 0);
319 kmem_zone_free(xfs_btree_cur_zone, cur);
323 * Duplicate the btree cursor.
324 * Allocate a new one, copy the record, re-get the buffers.
327 xfs_btree_dup_cursor(
328 xfs_btree_cur_t *cur, /* input cursor */
329 xfs_btree_cur_t **ncur) /* output cursor */
331 xfs_buf_t *bp; /* btree block's buffer pointer */
332 int error; /* error return value */
333 int i; /* level number of btree block */
334 xfs_mount_t *mp; /* mount structure for filesystem */
335 xfs_btree_cur_t *new; /* new cursor value */
336 xfs_trans_t *tp; /* transaction pointer, can be NULL */
342 * Allocate a new cursor like the old one.
344 new = cur->bc_ops->dup_cursor(cur);
347 * Copy the record currently in the cursor.
349 new->bc_rec = cur->bc_rec;
352 * For each level current, re-get the buffer and copy the ptr value.
354 for (i = 0; i < new->bc_nlevels; i++) {
355 new->bc_ptrs[i] = cur->bc_ptrs[i];
356 new->bc_ra[i] = cur->bc_ra[i];
357 bp = cur->bc_bufs[i];
359 error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp,
360 XFS_BUF_ADDR(bp), mp->m_bsize,
362 cur->bc_ops->buf_ops);
364 xfs_btree_del_cursor(new, error);
369 new->bc_bufs[i] = bp;
376 * XFS btree block layout and addressing:
378 * There are two types of blocks in the btree: leaf and non-leaf blocks.
380 * The leaf record start with a header then followed by records containing
381 * the values. A non-leaf block also starts with the same header, and
382 * then first contains lookup keys followed by an equal number of pointers
383 * to the btree blocks at the previous level.
385 * +--------+-------+-------+-------+-------+-------+-------+
386 * Leaf: | header | rec 1 | rec 2 | rec 3 | rec 4 | rec 5 | rec N |
387 * +--------+-------+-------+-------+-------+-------+-------+
389 * +--------+-------+-------+-------+-------+-------+-------+
390 * Non-Leaf: | header | key 1 | key 2 | key N | ptr 1 | ptr 2 | ptr N |
391 * +--------+-------+-------+-------+-------+-------+-------+
393 * The header is called struct xfs_btree_block for reasons better left unknown
394 * and comes in different versions for short (32bit) and long (64bit) block
395 * pointers. The record and key structures are defined by the btree instances
396 * and opaque to the btree core. The block pointers are simple disk endian
397 * integers, available in a short (32bit) and long (64bit) variant.
399 * The helpers below calculate the offset of a given record, key or pointer
400 * into a btree block (xfs_btree_*_offset) or return a pointer to the given
401 * record, key or pointer (xfs_btree_*_addr). Note that all addressing
402 * inside the btree block is done using indices starting at one, not zero!
406 * Return size of the btree block header for this btree instance.
408 static inline size_t xfs_btree_block_len(struct xfs_btree_cur *cur)
410 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
411 if (cur->bc_flags & XFS_BTREE_CRC_BLOCKS)
412 return XFS_BTREE_LBLOCK_CRC_LEN;
413 return XFS_BTREE_LBLOCK_LEN;
415 if (cur->bc_flags & XFS_BTREE_CRC_BLOCKS)
416 return XFS_BTREE_SBLOCK_CRC_LEN;
417 return XFS_BTREE_SBLOCK_LEN;
421 * Return size of btree block pointers for this btree instance.
423 static inline size_t xfs_btree_ptr_len(struct xfs_btree_cur *cur)
425 return (cur->bc_flags & XFS_BTREE_LONG_PTRS) ?
426 sizeof(__be64) : sizeof(__be32);
430 * Calculate offset of the n-th record in a btree block.
433 xfs_btree_rec_offset(
434 struct xfs_btree_cur *cur,
437 return xfs_btree_block_len(cur) +
438 (n - 1) * cur->bc_ops->rec_len;
442 * Calculate offset of the n-th key in a btree block.
445 xfs_btree_key_offset(
446 struct xfs_btree_cur *cur,
449 return xfs_btree_block_len(cur) +
450 (n - 1) * cur->bc_ops->key_len;
454 * Calculate offset of the n-th block pointer in a btree block.
457 xfs_btree_ptr_offset(
458 struct xfs_btree_cur *cur,
462 return xfs_btree_block_len(cur) +
463 cur->bc_ops->get_maxrecs(cur, level) * cur->bc_ops->key_len +
464 (n - 1) * xfs_btree_ptr_len(cur);
468 * Return a pointer to the n-th record in the btree block.
470 STATIC union xfs_btree_rec *
472 struct xfs_btree_cur *cur,
474 struct xfs_btree_block *block)
476 return (union xfs_btree_rec *)
477 ((char *)block + xfs_btree_rec_offset(cur, n));
481 * Return a pointer to the n-th key in the btree block.
483 STATIC union xfs_btree_key *
485 struct xfs_btree_cur *cur,
487 struct xfs_btree_block *block)
489 return (union xfs_btree_key *)
490 ((char *)block + xfs_btree_key_offset(cur, n));
494 * Return a pointer to the n-th block pointer in the btree block.
496 STATIC union xfs_btree_ptr *
498 struct xfs_btree_cur *cur,
500 struct xfs_btree_block *block)
502 int level = xfs_btree_get_level(block);
504 ASSERT(block->bb_level != 0);
506 return (union xfs_btree_ptr *)
507 ((char *)block + xfs_btree_ptr_offset(cur, n, level));
511 * Get the root block which is stored in the inode.
513 * For now this btree implementation assumes the btree root is always
514 * stored in the if_broot field of an inode fork.
516 STATIC struct xfs_btree_block *
518 struct xfs_btree_cur *cur)
520 struct xfs_ifork *ifp;
522 ifp = XFS_IFORK_PTR(cur->bc_private.b.ip, cur->bc_private.b.whichfork);
523 return (struct xfs_btree_block *)ifp->if_broot;
527 * Retrieve the block pointer from the cursor at the given level.
528 * This may be an inode btree root or from a buffer.
530 STATIC struct xfs_btree_block * /* generic btree block pointer */
532 struct xfs_btree_cur *cur, /* btree cursor */
533 int level, /* level in btree */
534 struct xfs_buf **bpp) /* buffer containing the block */
536 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
537 (level == cur->bc_nlevels - 1)) {
539 return xfs_btree_get_iroot(cur);
542 *bpp = cur->bc_bufs[level];
543 return XFS_BUF_TO_BLOCK(*bpp);
547 * Get a buffer for the block, return it with no data read.
548 * Long-form addressing.
550 xfs_buf_t * /* buffer for fsbno */
552 xfs_mount_t *mp, /* file system mount point */
553 xfs_trans_t *tp, /* transaction pointer */
554 xfs_fsblock_t fsbno, /* file system block number */
555 uint lock) /* lock flags for get_buf */
557 xfs_daddr_t d; /* real disk block address */
559 ASSERT(fsbno != NULLFSBLOCK);
560 d = XFS_FSB_TO_DADDR(mp, fsbno);
561 return xfs_trans_get_buf(tp, mp->m_ddev_targp, d, mp->m_bsize, lock);
565 * Get a buffer for the block, return it with no data read.
566 * Short-form addressing.
568 xfs_buf_t * /* buffer for agno/agbno */
570 xfs_mount_t *mp, /* file system mount point */
571 xfs_trans_t *tp, /* transaction pointer */
572 xfs_agnumber_t agno, /* allocation group number */
573 xfs_agblock_t agbno, /* allocation group block number */
574 uint lock) /* lock flags for get_buf */
576 xfs_daddr_t d; /* real disk block address */
578 ASSERT(agno != NULLAGNUMBER);
579 ASSERT(agbno != NULLAGBLOCK);
580 d = XFS_AGB_TO_DADDR(mp, agno, agbno);
581 return xfs_trans_get_buf(tp, mp->m_ddev_targp, d, mp->m_bsize, lock);
585 * Check for the cursor referring to the last block at the given level.
587 int /* 1=is last block, 0=not last block */
588 xfs_btree_islastblock(
589 xfs_btree_cur_t *cur, /* btree cursor */
590 int level) /* level to check */
592 struct xfs_btree_block *block; /* generic btree block pointer */
593 xfs_buf_t *bp; /* buffer containing block */
595 block = xfs_btree_get_block(cur, level, &bp);
596 xfs_btree_check_block(cur, block, level, bp);
597 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
598 return block->bb_u.l.bb_rightsib == cpu_to_be64(NULLDFSBNO);
600 return block->bb_u.s.bb_rightsib == cpu_to_be32(NULLAGBLOCK);
604 * Change the cursor to point to the first record at the given level.
605 * Other levels are unaffected.
607 STATIC int /* success=1, failure=0 */
609 xfs_btree_cur_t *cur, /* btree cursor */
610 int level) /* level to change */
612 struct xfs_btree_block *block; /* generic btree block pointer */
613 xfs_buf_t *bp; /* buffer containing block */
616 * Get the block pointer for this level.
618 block = xfs_btree_get_block(cur, level, &bp);
619 xfs_btree_check_block(cur, block, level, bp);
621 * It's empty, there is no such record.
623 if (!block->bb_numrecs)
626 * Set the ptr value to 1, that's the first record/key.
628 cur->bc_ptrs[level] = 1;
633 * Change the cursor to point to the last record in the current block
634 * at the given level. Other levels are unaffected.
636 STATIC int /* success=1, failure=0 */
638 xfs_btree_cur_t *cur, /* btree cursor */
639 int level) /* level to change */
641 struct xfs_btree_block *block; /* generic btree block pointer */
642 xfs_buf_t *bp; /* buffer containing block */
645 * Get the block pointer for this level.
647 block = xfs_btree_get_block(cur, level, &bp);
648 xfs_btree_check_block(cur, block, level, bp);
650 * It's empty, there is no such record.
652 if (!block->bb_numrecs)
655 * Set the ptr value to numrecs, that's the last record/key.
657 cur->bc_ptrs[level] = be16_to_cpu(block->bb_numrecs);
662 * Compute first and last byte offsets for the fields given.
663 * Interprets the offsets table, which contains struct field offsets.
667 __int64_t fields, /* bitmask of fields */
668 const short *offsets, /* table of field offsets */
669 int nbits, /* number of bits to inspect */
670 int *first, /* output: first byte offset */
671 int *last) /* output: last byte offset */
673 int i; /* current bit number */
674 __int64_t imask; /* mask for current bit number */
678 * Find the lowest bit, so the first byte offset.
680 for (i = 0, imask = 1LL; ; i++, imask <<= 1) {
681 if (imask & fields) {
687 * Find the highest bit, so the last byte offset.
689 for (i = nbits - 1, imask = 1LL << i; ; i--, imask >>= 1) {
690 if (imask & fields) {
691 *last = offsets[i + 1] - 1;
698 * Get a buffer for the block, return it read in.
699 * Long-form addressing.
703 struct xfs_mount *mp, /* file system mount point */
704 struct xfs_trans *tp, /* transaction pointer */
705 xfs_fsblock_t fsbno, /* file system block number */
706 uint lock, /* lock flags for read_buf */
707 struct xfs_buf **bpp, /* buffer for fsbno */
708 int refval, /* ref count value for buffer */
709 const struct xfs_buf_ops *ops)
711 struct xfs_buf *bp; /* return value */
712 xfs_daddr_t d; /* real disk block address */
715 ASSERT(fsbno != NULLFSBLOCK);
716 d = XFS_FSB_TO_DADDR(mp, fsbno);
717 error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, d,
718 mp->m_bsize, lock, &bp, ops);
722 xfs_buf_set_ref(bp, refval);
728 * Read-ahead the block, don't wait for it, don't return a buffer.
729 * Long-form addressing.
733 xfs_btree_reada_bufl(
734 struct xfs_mount *mp, /* file system mount point */
735 xfs_fsblock_t fsbno, /* file system block number */
736 xfs_extlen_t count, /* count of filesystem blocks */
737 const struct xfs_buf_ops *ops)
741 ASSERT(fsbno != NULLFSBLOCK);
742 d = XFS_FSB_TO_DADDR(mp, fsbno);
743 xfs_buf_readahead(mp->m_ddev_targp, d, mp->m_bsize * count, ops);
747 * Read-ahead the block, don't wait for it, don't return a buffer.
748 * Short-form addressing.
752 xfs_btree_reada_bufs(
753 struct xfs_mount *mp, /* file system mount point */
754 xfs_agnumber_t agno, /* allocation group number */
755 xfs_agblock_t agbno, /* allocation group block number */
756 xfs_extlen_t count, /* count of filesystem blocks */
757 const struct xfs_buf_ops *ops)
761 ASSERT(agno != NULLAGNUMBER);
762 ASSERT(agbno != NULLAGBLOCK);
763 d = XFS_AGB_TO_DADDR(mp, agno, agbno);
764 xfs_buf_readahead(mp->m_ddev_targp, d, mp->m_bsize * count, ops);
768 xfs_btree_readahead_lblock(
769 struct xfs_btree_cur *cur,
771 struct xfs_btree_block *block)
774 xfs_dfsbno_t left = be64_to_cpu(block->bb_u.l.bb_leftsib);
775 xfs_dfsbno_t right = be64_to_cpu(block->bb_u.l.bb_rightsib);
777 if ((lr & XFS_BTCUR_LEFTRA) && left != NULLDFSBNO) {
778 xfs_btree_reada_bufl(cur->bc_mp, left, 1,
779 cur->bc_ops->buf_ops);
783 if ((lr & XFS_BTCUR_RIGHTRA) && right != NULLDFSBNO) {
784 xfs_btree_reada_bufl(cur->bc_mp, right, 1,
785 cur->bc_ops->buf_ops);
793 xfs_btree_readahead_sblock(
794 struct xfs_btree_cur *cur,
796 struct xfs_btree_block *block)
799 xfs_agblock_t left = be32_to_cpu(block->bb_u.s.bb_leftsib);
800 xfs_agblock_t right = be32_to_cpu(block->bb_u.s.bb_rightsib);
803 if ((lr & XFS_BTCUR_LEFTRA) && left != NULLAGBLOCK) {
804 xfs_btree_reada_bufs(cur->bc_mp, cur->bc_private.a.agno,
805 left, 1, cur->bc_ops->buf_ops);
809 if ((lr & XFS_BTCUR_RIGHTRA) && right != NULLAGBLOCK) {
810 xfs_btree_reada_bufs(cur->bc_mp, cur->bc_private.a.agno,
811 right, 1, cur->bc_ops->buf_ops);
819 * Read-ahead btree blocks, at the given level.
820 * Bits in lr are set from XFS_BTCUR_{LEFT,RIGHT}RA.
824 struct xfs_btree_cur *cur, /* btree cursor */
825 int lev, /* level in btree */
826 int lr) /* left/right bits */
828 struct xfs_btree_block *block;
831 * No readahead needed if we are at the root level and the
832 * btree root is stored in the inode.
834 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
835 (lev == cur->bc_nlevels - 1))
838 if ((cur->bc_ra[lev] | lr) == cur->bc_ra[lev])
841 cur->bc_ra[lev] |= lr;
842 block = XFS_BUF_TO_BLOCK(cur->bc_bufs[lev]);
844 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
845 return xfs_btree_readahead_lblock(cur, lr, block);
846 return xfs_btree_readahead_sblock(cur, lr, block);
850 xfs_btree_ptr_to_daddr(
851 struct xfs_btree_cur *cur,
852 union xfs_btree_ptr *ptr)
854 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
855 ASSERT(ptr->l != cpu_to_be64(NULLDFSBNO));
857 return XFS_FSB_TO_DADDR(cur->bc_mp, be64_to_cpu(ptr->l));
859 ASSERT(cur->bc_private.a.agno != NULLAGNUMBER);
860 ASSERT(ptr->s != cpu_to_be32(NULLAGBLOCK));
862 return XFS_AGB_TO_DADDR(cur->bc_mp, cur->bc_private.a.agno,
863 be32_to_cpu(ptr->s));
868 * Readahead @count btree blocks at the given @ptr location.
870 * We don't need to care about long or short form btrees here as we have a
871 * method of converting the ptr directly to a daddr available to us.
874 xfs_btree_readahead_ptr(
875 struct xfs_btree_cur *cur,
876 union xfs_btree_ptr *ptr,
879 xfs_buf_readahead(cur->bc_mp->m_ddev_targp,
880 xfs_btree_ptr_to_daddr(cur, ptr),
881 cur->bc_mp->m_bsize * count, cur->bc_ops->buf_ops);
885 * Set the buffer for level "lev" in the cursor to bp, releasing
886 * any previous buffer.
890 xfs_btree_cur_t *cur, /* btree cursor */
891 int lev, /* level in btree */
892 xfs_buf_t *bp) /* new buffer to set */
894 struct xfs_btree_block *b; /* btree block */
896 if (cur->bc_bufs[lev])
897 xfs_trans_brelse(cur->bc_tp, cur->bc_bufs[lev]);
898 cur->bc_bufs[lev] = bp;
901 b = XFS_BUF_TO_BLOCK(bp);
902 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
903 if (b->bb_u.l.bb_leftsib == cpu_to_be64(NULLDFSBNO))
904 cur->bc_ra[lev] |= XFS_BTCUR_LEFTRA;
905 if (b->bb_u.l.bb_rightsib == cpu_to_be64(NULLDFSBNO))
906 cur->bc_ra[lev] |= XFS_BTCUR_RIGHTRA;
908 if (b->bb_u.s.bb_leftsib == cpu_to_be32(NULLAGBLOCK))
909 cur->bc_ra[lev] |= XFS_BTCUR_LEFTRA;
910 if (b->bb_u.s.bb_rightsib == cpu_to_be32(NULLAGBLOCK))
911 cur->bc_ra[lev] |= XFS_BTCUR_RIGHTRA;
916 xfs_btree_ptr_is_null(
917 struct xfs_btree_cur *cur,
918 union xfs_btree_ptr *ptr)
920 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
921 return ptr->l == cpu_to_be64(NULLDFSBNO);
923 return ptr->s == cpu_to_be32(NULLAGBLOCK);
927 xfs_btree_set_ptr_null(
928 struct xfs_btree_cur *cur,
929 union xfs_btree_ptr *ptr)
931 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
932 ptr->l = cpu_to_be64(NULLDFSBNO);
934 ptr->s = cpu_to_be32(NULLAGBLOCK);
938 * Get/set/init sibling pointers
941 xfs_btree_get_sibling(
942 struct xfs_btree_cur *cur,
943 struct xfs_btree_block *block,
944 union xfs_btree_ptr *ptr,
947 ASSERT(lr == XFS_BB_LEFTSIB || lr == XFS_BB_RIGHTSIB);
949 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
950 if (lr == XFS_BB_RIGHTSIB)
951 ptr->l = block->bb_u.l.bb_rightsib;
953 ptr->l = block->bb_u.l.bb_leftsib;
955 if (lr == XFS_BB_RIGHTSIB)
956 ptr->s = block->bb_u.s.bb_rightsib;
958 ptr->s = block->bb_u.s.bb_leftsib;
963 xfs_btree_set_sibling(
964 struct xfs_btree_cur *cur,
965 struct xfs_btree_block *block,
966 union xfs_btree_ptr *ptr,
969 ASSERT(lr == XFS_BB_LEFTSIB || lr == XFS_BB_RIGHTSIB);
971 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
972 if (lr == XFS_BB_RIGHTSIB)
973 block->bb_u.l.bb_rightsib = ptr->l;
975 block->bb_u.l.bb_leftsib = ptr->l;
977 if (lr == XFS_BB_RIGHTSIB)
978 block->bb_u.s.bb_rightsib = ptr->s;
980 block->bb_u.s.bb_leftsib = ptr->s;
985 xfs_btree_init_block_int(
986 struct xfs_mount *mp,
987 struct xfs_btree_block *buf,
995 buf->bb_magic = cpu_to_be32(magic);
996 buf->bb_level = cpu_to_be16(level);
997 buf->bb_numrecs = cpu_to_be16(numrecs);
999 if (flags & XFS_BTREE_LONG_PTRS) {
1000 buf->bb_u.l.bb_leftsib = cpu_to_be64(NULLDFSBNO);
1001 buf->bb_u.l.bb_rightsib = cpu_to_be64(NULLDFSBNO);
1002 if (flags & XFS_BTREE_CRC_BLOCKS) {
1003 buf->bb_u.l.bb_blkno = cpu_to_be64(blkno);
1004 buf->bb_u.l.bb_owner = cpu_to_be64(owner);
1005 uuid_copy(&buf->bb_u.l.bb_uuid, &mp->m_sb.sb_uuid);
1006 buf->bb_u.l.bb_pad = 0;
1007 buf->bb_u.l.bb_lsn = 0;
1010 /* owner is a 32 bit value on short blocks */
1011 __u32 __owner = (__u32)owner;
1013 buf->bb_u.s.bb_leftsib = cpu_to_be32(NULLAGBLOCK);
1014 buf->bb_u.s.bb_rightsib = cpu_to_be32(NULLAGBLOCK);
1015 if (flags & XFS_BTREE_CRC_BLOCKS) {
1016 buf->bb_u.s.bb_blkno = cpu_to_be64(blkno);
1017 buf->bb_u.s.bb_owner = cpu_to_be32(__owner);
1018 uuid_copy(&buf->bb_u.s.bb_uuid, &mp->m_sb.sb_uuid);
1019 buf->bb_u.s.bb_lsn = 0;
1025 xfs_btree_init_block(
1026 struct xfs_mount *mp,
1034 xfs_btree_init_block_int(mp, XFS_BUF_TO_BLOCK(bp), bp->b_bn,
1035 magic, level, numrecs, owner, flags);
1039 xfs_btree_init_block_cur(
1040 struct xfs_btree_cur *cur,
1048 * we can pull the owner from the cursor right now as the different
1049 * owners align directly with the pointer size of the btree. This may
1050 * change in future, but is safe for current users of the generic btree
1053 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
1054 owner = cur->bc_private.b.ip->i_ino;
1056 owner = cur->bc_private.a.agno;
1058 xfs_btree_init_block_int(cur->bc_mp, XFS_BUF_TO_BLOCK(bp), bp->b_bn,
1059 xfs_btree_magic(cur), level, numrecs,
1060 owner, cur->bc_flags);
1064 * Return true if ptr is the last record in the btree and
1065 * we need to track updates to this record. The decision
1066 * will be further refined in the update_lastrec method.
1069 xfs_btree_is_lastrec(
1070 struct xfs_btree_cur *cur,
1071 struct xfs_btree_block *block,
1074 union xfs_btree_ptr ptr;
1078 if (!(cur->bc_flags & XFS_BTREE_LASTREC_UPDATE))
1081 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_RIGHTSIB);
1082 if (!xfs_btree_ptr_is_null(cur, &ptr))
1088 xfs_btree_buf_to_ptr(
1089 struct xfs_btree_cur *cur,
1091 union xfs_btree_ptr *ptr)
1093 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
1094 ptr->l = cpu_to_be64(XFS_DADDR_TO_FSB(cur->bc_mp,
1097 ptr->s = cpu_to_be32(xfs_daddr_to_agbno(cur->bc_mp,
1104 struct xfs_btree_cur *cur,
1107 switch (cur->bc_btnum) {
1110 xfs_buf_set_ref(bp, XFS_ALLOC_BTREE_REF);
1113 case XFS_BTNUM_FINO:
1114 xfs_buf_set_ref(bp, XFS_INO_BTREE_REF);
1116 case XFS_BTNUM_BMAP:
1117 xfs_buf_set_ref(bp, XFS_BMAP_BTREE_REF);
1125 xfs_btree_get_buf_block(
1126 struct xfs_btree_cur *cur,
1127 union xfs_btree_ptr *ptr,
1129 struct xfs_btree_block **block,
1130 struct xfs_buf **bpp)
1132 struct xfs_mount *mp = cur->bc_mp;
1135 /* need to sort out how callers deal with failures first */
1136 ASSERT(!(flags & XBF_TRYLOCK));
1138 d = xfs_btree_ptr_to_daddr(cur, ptr);
1139 *bpp = xfs_trans_get_buf(cur->bc_tp, mp->m_ddev_targp, d,
1140 mp->m_bsize, flags);
1145 (*bpp)->b_ops = cur->bc_ops->buf_ops;
1146 *block = XFS_BUF_TO_BLOCK(*bpp);
1151 * Read in the buffer at the given ptr and return the buffer and
1152 * the block pointer within the buffer.
1155 xfs_btree_read_buf_block(
1156 struct xfs_btree_cur *cur,
1157 union xfs_btree_ptr *ptr,
1159 struct xfs_btree_block **block,
1160 struct xfs_buf **bpp)
1162 struct xfs_mount *mp = cur->bc_mp;
1166 /* need to sort out how callers deal with failures first */
1167 ASSERT(!(flags & XBF_TRYLOCK));
1169 d = xfs_btree_ptr_to_daddr(cur, ptr);
1170 error = xfs_trans_read_buf(mp, cur->bc_tp, mp->m_ddev_targp, d,
1171 mp->m_bsize, flags, bpp,
1172 cur->bc_ops->buf_ops);
1176 xfs_btree_set_refs(cur, *bpp);
1177 *block = XFS_BUF_TO_BLOCK(*bpp);
1182 * Copy keys from one btree block to another.
1185 xfs_btree_copy_keys(
1186 struct xfs_btree_cur *cur,
1187 union xfs_btree_key *dst_key,
1188 union xfs_btree_key *src_key,
1191 ASSERT(numkeys >= 0);
1192 memcpy(dst_key, src_key, numkeys * cur->bc_ops->key_len);
1196 * Copy records from one btree block to another.
1199 xfs_btree_copy_recs(
1200 struct xfs_btree_cur *cur,
1201 union xfs_btree_rec *dst_rec,
1202 union xfs_btree_rec *src_rec,
1205 ASSERT(numrecs >= 0);
1206 memcpy(dst_rec, src_rec, numrecs * cur->bc_ops->rec_len);
1210 * Copy block pointers from one btree block to another.
1213 xfs_btree_copy_ptrs(
1214 struct xfs_btree_cur *cur,
1215 union xfs_btree_ptr *dst_ptr,
1216 union xfs_btree_ptr *src_ptr,
1219 ASSERT(numptrs >= 0);
1220 memcpy(dst_ptr, src_ptr, numptrs * xfs_btree_ptr_len(cur));
1224 * Shift keys one index left/right inside a single btree block.
1227 xfs_btree_shift_keys(
1228 struct xfs_btree_cur *cur,
1229 union xfs_btree_key *key,
1235 ASSERT(numkeys >= 0);
1236 ASSERT(dir == 1 || dir == -1);
1238 dst_key = (char *)key + (dir * cur->bc_ops->key_len);
1239 memmove(dst_key, key, numkeys * cur->bc_ops->key_len);
1243 * Shift records one index left/right inside a single btree block.
1246 xfs_btree_shift_recs(
1247 struct xfs_btree_cur *cur,
1248 union xfs_btree_rec *rec,
1254 ASSERT(numrecs >= 0);
1255 ASSERT(dir == 1 || dir == -1);
1257 dst_rec = (char *)rec + (dir * cur->bc_ops->rec_len);
1258 memmove(dst_rec, rec, numrecs * cur->bc_ops->rec_len);
1262 * Shift block pointers one index left/right inside a single btree block.
1265 xfs_btree_shift_ptrs(
1266 struct xfs_btree_cur *cur,
1267 union xfs_btree_ptr *ptr,
1273 ASSERT(numptrs >= 0);
1274 ASSERT(dir == 1 || dir == -1);
1276 dst_ptr = (char *)ptr + (dir * xfs_btree_ptr_len(cur));
1277 memmove(dst_ptr, ptr, numptrs * xfs_btree_ptr_len(cur));
1281 * Log key values from the btree block.
1285 struct xfs_btree_cur *cur,
1290 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1291 XFS_BTREE_TRACE_ARGBII(cur, bp, first, last);
1294 xfs_trans_buf_set_type(cur->bc_tp, bp, XFS_BLFT_BTREE_BUF);
1295 xfs_trans_log_buf(cur->bc_tp, bp,
1296 xfs_btree_key_offset(cur, first),
1297 xfs_btree_key_offset(cur, last + 1) - 1);
1299 xfs_trans_log_inode(cur->bc_tp, cur->bc_private.b.ip,
1300 xfs_ilog_fbroot(cur->bc_private.b.whichfork));
1303 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1307 * Log record values from the btree block.
1311 struct xfs_btree_cur *cur,
1316 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1317 XFS_BTREE_TRACE_ARGBII(cur, bp, first, last);
1319 xfs_trans_buf_set_type(cur->bc_tp, bp, XFS_BLFT_BTREE_BUF);
1320 xfs_trans_log_buf(cur->bc_tp, bp,
1321 xfs_btree_rec_offset(cur, first),
1322 xfs_btree_rec_offset(cur, last + 1) - 1);
1324 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1328 * Log block pointer fields from a btree block (nonleaf).
1332 struct xfs_btree_cur *cur, /* btree cursor */
1333 struct xfs_buf *bp, /* buffer containing btree block */
1334 int first, /* index of first pointer to log */
1335 int last) /* index of last pointer to log */
1337 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1338 XFS_BTREE_TRACE_ARGBII(cur, bp, first, last);
1341 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
1342 int level = xfs_btree_get_level(block);
1344 xfs_trans_buf_set_type(cur->bc_tp, bp, XFS_BLFT_BTREE_BUF);
1345 xfs_trans_log_buf(cur->bc_tp, bp,
1346 xfs_btree_ptr_offset(cur, first, level),
1347 xfs_btree_ptr_offset(cur, last + 1, level) - 1);
1349 xfs_trans_log_inode(cur->bc_tp, cur->bc_private.b.ip,
1350 xfs_ilog_fbroot(cur->bc_private.b.whichfork));
1353 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1357 * Log fields from a btree block header.
1360 xfs_btree_log_block(
1361 struct xfs_btree_cur *cur, /* btree cursor */
1362 struct xfs_buf *bp, /* buffer containing btree block */
1363 int fields) /* mask of fields: XFS_BB_... */
1365 int first; /* first byte offset logged */
1366 int last; /* last byte offset logged */
1367 static const short soffsets[] = { /* table of offsets (short) */
1368 offsetof(struct xfs_btree_block, bb_magic),
1369 offsetof(struct xfs_btree_block, bb_level),
1370 offsetof(struct xfs_btree_block, bb_numrecs),
1371 offsetof(struct xfs_btree_block, bb_u.s.bb_leftsib),
1372 offsetof(struct xfs_btree_block, bb_u.s.bb_rightsib),
1373 offsetof(struct xfs_btree_block, bb_u.s.bb_blkno),
1374 offsetof(struct xfs_btree_block, bb_u.s.bb_lsn),
1375 offsetof(struct xfs_btree_block, bb_u.s.bb_uuid),
1376 offsetof(struct xfs_btree_block, bb_u.s.bb_owner),
1377 offsetof(struct xfs_btree_block, bb_u.s.bb_crc),
1378 XFS_BTREE_SBLOCK_CRC_LEN
1380 static const short loffsets[] = { /* table of offsets (long) */
1381 offsetof(struct xfs_btree_block, bb_magic),
1382 offsetof(struct xfs_btree_block, bb_level),
1383 offsetof(struct xfs_btree_block, bb_numrecs),
1384 offsetof(struct xfs_btree_block, bb_u.l.bb_leftsib),
1385 offsetof(struct xfs_btree_block, bb_u.l.bb_rightsib),
1386 offsetof(struct xfs_btree_block, bb_u.l.bb_blkno),
1387 offsetof(struct xfs_btree_block, bb_u.l.bb_lsn),
1388 offsetof(struct xfs_btree_block, bb_u.l.bb_uuid),
1389 offsetof(struct xfs_btree_block, bb_u.l.bb_owner),
1390 offsetof(struct xfs_btree_block, bb_u.l.bb_crc),
1391 offsetof(struct xfs_btree_block, bb_u.l.bb_pad),
1392 XFS_BTREE_LBLOCK_CRC_LEN
1395 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1396 XFS_BTREE_TRACE_ARGBI(cur, bp, fields);
1401 if (cur->bc_flags & XFS_BTREE_CRC_BLOCKS) {
1403 * We don't log the CRC when updating a btree
1404 * block but instead recreate it during log
1405 * recovery. As the log buffers have checksums
1406 * of their own this is safe and avoids logging a crc
1407 * update in a lot of places.
1409 if (fields == XFS_BB_ALL_BITS)
1410 fields = XFS_BB_ALL_BITS_CRC;
1411 nbits = XFS_BB_NUM_BITS_CRC;
1413 nbits = XFS_BB_NUM_BITS;
1415 xfs_btree_offsets(fields,
1416 (cur->bc_flags & XFS_BTREE_LONG_PTRS) ?
1417 loffsets : soffsets,
1418 nbits, &first, &last);
1419 xfs_trans_buf_set_type(cur->bc_tp, bp, XFS_BLFT_BTREE_BUF);
1420 xfs_trans_log_buf(cur->bc_tp, bp, first, last);
1422 xfs_trans_log_inode(cur->bc_tp, cur->bc_private.b.ip,
1423 xfs_ilog_fbroot(cur->bc_private.b.whichfork));
1426 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1430 * Increment cursor by one record at the level.
1431 * For nonzero levels the leaf-ward information is untouched.
1434 xfs_btree_increment(
1435 struct xfs_btree_cur *cur,
1437 int *stat) /* success/failure */
1439 struct xfs_btree_block *block;
1440 union xfs_btree_ptr ptr;
1442 int error; /* error return value */
1445 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1446 XFS_BTREE_TRACE_ARGI(cur, level);
1448 ASSERT(level < cur->bc_nlevels);
1450 /* Read-ahead to the right at this level. */
1451 xfs_btree_readahead(cur, level, XFS_BTCUR_RIGHTRA);
1453 /* Get a pointer to the btree block. */
1454 block = xfs_btree_get_block(cur, level, &bp);
1457 error = xfs_btree_check_block(cur, block, level, bp);
1462 /* We're done if we remain in the block after the increment. */
1463 if (++cur->bc_ptrs[level] <= xfs_btree_get_numrecs(block))
1466 /* Fail if we just went off the right edge of the tree. */
1467 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_RIGHTSIB);
1468 if (xfs_btree_ptr_is_null(cur, &ptr))
1471 XFS_BTREE_STATS_INC(cur, increment);
1474 * March up the tree incrementing pointers.
1475 * Stop when we don't go off the right edge of a block.
1477 for (lev = level + 1; lev < cur->bc_nlevels; lev++) {
1478 block = xfs_btree_get_block(cur, lev, &bp);
1481 error = xfs_btree_check_block(cur, block, lev, bp);
1486 if (++cur->bc_ptrs[lev] <= xfs_btree_get_numrecs(block))
1489 /* Read-ahead the right block for the next loop. */
1490 xfs_btree_readahead(cur, lev, XFS_BTCUR_RIGHTRA);
1494 * If we went off the root then we are either seriously
1495 * confused or have the tree root in an inode.
1497 if (lev == cur->bc_nlevels) {
1498 if (cur->bc_flags & XFS_BTREE_ROOT_IN_INODE)
1501 error = EFSCORRUPTED;
1504 ASSERT(lev < cur->bc_nlevels);
1507 * Now walk back down the tree, fixing up the cursor's buffer
1508 * pointers and key numbers.
1510 for (block = xfs_btree_get_block(cur, lev, &bp); lev > level; ) {
1511 union xfs_btree_ptr *ptrp;
1513 ptrp = xfs_btree_ptr_addr(cur, cur->bc_ptrs[lev], block);
1515 error = xfs_btree_read_buf_block(cur, ptrp, 0, &block, &bp);
1519 xfs_btree_setbuf(cur, lev, bp);
1520 cur->bc_ptrs[lev] = 1;
1523 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1528 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1533 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
1538 * Decrement cursor by one record at the level.
1539 * For nonzero levels the leaf-ward information is untouched.
1542 xfs_btree_decrement(
1543 struct xfs_btree_cur *cur,
1545 int *stat) /* success/failure */
1547 struct xfs_btree_block *block;
1549 int error; /* error return value */
1551 union xfs_btree_ptr ptr;
1553 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1554 XFS_BTREE_TRACE_ARGI(cur, level);
1556 ASSERT(level < cur->bc_nlevels);
1558 /* Read-ahead to the left at this level. */
1559 xfs_btree_readahead(cur, level, XFS_BTCUR_LEFTRA);
1561 /* We're done if we remain in the block after the decrement. */
1562 if (--cur->bc_ptrs[level] > 0)
1565 /* Get a pointer to the btree block. */
1566 block = xfs_btree_get_block(cur, level, &bp);
1569 error = xfs_btree_check_block(cur, block, level, bp);
1574 /* Fail if we just went off the left edge of the tree. */
1575 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_LEFTSIB);
1576 if (xfs_btree_ptr_is_null(cur, &ptr))
1579 XFS_BTREE_STATS_INC(cur, decrement);
1582 * March up the tree decrementing pointers.
1583 * Stop when we don't go off the left edge of a block.
1585 for (lev = level + 1; lev < cur->bc_nlevels; lev++) {
1586 if (--cur->bc_ptrs[lev] > 0)
1588 /* Read-ahead the left block for the next loop. */
1589 xfs_btree_readahead(cur, lev, XFS_BTCUR_LEFTRA);
1593 * If we went off the root then we are seriously confused.
1594 * or the root of the tree is in an inode.
1596 if (lev == cur->bc_nlevels) {
1597 if (cur->bc_flags & XFS_BTREE_ROOT_IN_INODE)
1600 error = EFSCORRUPTED;
1603 ASSERT(lev < cur->bc_nlevels);
1606 * Now walk back down the tree, fixing up the cursor's buffer
1607 * pointers and key numbers.
1609 for (block = xfs_btree_get_block(cur, lev, &bp); lev > level; ) {
1610 union xfs_btree_ptr *ptrp;
1612 ptrp = xfs_btree_ptr_addr(cur, cur->bc_ptrs[lev], block);
1614 error = xfs_btree_read_buf_block(cur, ptrp, 0, &block, &bp);
1617 xfs_btree_setbuf(cur, lev, bp);
1618 cur->bc_ptrs[lev] = xfs_btree_get_numrecs(block);
1621 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1626 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1631 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
1636 xfs_btree_lookup_get_block(
1637 struct xfs_btree_cur *cur, /* btree cursor */
1638 int level, /* level in the btree */
1639 union xfs_btree_ptr *pp, /* ptr to btree block */
1640 struct xfs_btree_block **blkp) /* return btree block */
1642 struct xfs_buf *bp; /* buffer pointer for btree block */
1645 /* special case the root block if in an inode */
1646 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
1647 (level == cur->bc_nlevels - 1)) {
1648 *blkp = xfs_btree_get_iroot(cur);
1653 * If the old buffer at this level for the disk address we are
1654 * looking for re-use it.
1656 * Otherwise throw it away and get a new one.
1658 bp = cur->bc_bufs[level];
1659 if (bp && XFS_BUF_ADDR(bp) == xfs_btree_ptr_to_daddr(cur, pp)) {
1660 *blkp = XFS_BUF_TO_BLOCK(bp);
1664 error = xfs_btree_read_buf_block(cur, pp, 0, blkp, &bp);
1668 xfs_btree_setbuf(cur, level, bp);
1673 * Get current search key. For level 0 we don't actually have a key
1674 * structure so we make one up from the record. For all other levels
1675 * we just return the right key.
1677 STATIC union xfs_btree_key *
1678 xfs_lookup_get_search_key(
1679 struct xfs_btree_cur *cur,
1682 struct xfs_btree_block *block,
1683 union xfs_btree_key *kp)
1686 cur->bc_ops->init_key_from_rec(kp,
1687 xfs_btree_rec_addr(cur, keyno, block));
1691 return xfs_btree_key_addr(cur, keyno, block);
1695 * Lookup the record. The cursor is made to point to it, based on dir.
1696 * stat is set to 0 if can't find any such record, 1 for success.
1700 struct xfs_btree_cur *cur, /* btree cursor */
1701 xfs_lookup_t dir, /* <=, ==, or >= */
1702 int *stat) /* success/failure */
1704 struct xfs_btree_block *block; /* current btree block */
1705 __int64_t diff; /* difference for the current key */
1706 int error; /* error return value */
1707 int keyno; /* current key number */
1708 int level; /* level in the btree */
1709 union xfs_btree_ptr *pp; /* ptr to btree block */
1710 union xfs_btree_ptr ptr; /* ptr to btree block */
1712 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1713 XFS_BTREE_TRACE_ARGI(cur, dir);
1715 XFS_BTREE_STATS_INC(cur, lookup);
1720 /* initialise start pointer from cursor */
1721 cur->bc_ops->init_ptr_from_cur(cur, &ptr);
1725 * Iterate over each level in the btree, starting at the root.
1726 * For each level above the leaves, find the key we need, based
1727 * on the lookup record, then follow the corresponding block
1728 * pointer down to the next level.
1730 for (level = cur->bc_nlevels - 1, diff = 1; level >= 0; level--) {
1731 /* Get the block we need to do the lookup on. */
1732 error = xfs_btree_lookup_get_block(cur, level, pp, &block);
1738 * If we already had a key match at a higher level, we
1739 * know we need to use the first entry in this block.
1743 /* Otherwise search this block. Do a binary search. */
1745 int high; /* high entry number */
1746 int low; /* low entry number */
1748 /* Set low and high entry numbers, 1-based. */
1750 high = xfs_btree_get_numrecs(block);
1752 /* Block is empty, must be an empty leaf. */
1753 ASSERT(level == 0 && cur->bc_nlevels == 1);
1755 cur->bc_ptrs[0] = dir != XFS_LOOKUP_LE;
1756 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1761 /* Binary search the block. */
1762 while (low <= high) {
1763 union xfs_btree_key key;
1764 union xfs_btree_key *kp;
1766 XFS_BTREE_STATS_INC(cur, compare);
1768 /* keyno is average of low and high. */
1769 keyno = (low + high) >> 1;
1771 /* Get current search key */
1772 kp = xfs_lookup_get_search_key(cur, level,
1773 keyno, block, &key);
1776 * Compute difference to get next direction:
1777 * - less than, move right
1778 * - greater than, move left
1779 * - equal, we're done
1781 diff = cur->bc_ops->key_diff(cur, kp);
1792 * If there are more levels, set up for the next level
1793 * by getting the block number and filling in the cursor.
1797 * If we moved left, need the previous key number,
1798 * unless there isn't one.
1800 if (diff > 0 && --keyno < 1)
1802 pp = xfs_btree_ptr_addr(cur, keyno, block);
1805 error = xfs_btree_check_ptr(cur, pp, 0, level);
1809 cur->bc_ptrs[level] = keyno;
1813 /* Done with the search. See if we need to adjust the results. */
1814 if (dir != XFS_LOOKUP_LE && diff < 0) {
1817 * If ge search and we went off the end of the block, but it's
1818 * not the last block, we're in the wrong block.
1820 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_RIGHTSIB);
1821 if (dir == XFS_LOOKUP_GE &&
1822 keyno > xfs_btree_get_numrecs(block) &&
1823 !xfs_btree_ptr_is_null(cur, &ptr)) {
1826 cur->bc_ptrs[0] = keyno;
1827 error = xfs_btree_increment(cur, 0, &i);
1830 XFS_WANT_CORRUPTED_RETURN(i == 1);
1831 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1835 } else if (dir == XFS_LOOKUP_LE && diff > 0)
1837 cur->bc_ptrs[0] = keyno;
1839 /* Return if we succeeded or not. */
1840 if (keyno == 0 || keyno > xfs_btree_get_numrecs(block))
1842 else if (dir != XFS_LOOKUP_EQ || diff == 0)
1846 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1850 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
1855 * Update keys at all levels from here to the root along the cursor's path.
1859 struct xfs_btree_cur *cur,
1860 union xfs_btree_key *keyp,
1863 struct xfs_btree_block *block;
1865 union xfs_btree_key *kp;
1868 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1869 XFS_BTREE_TRACE_ARGIK(cur, level, keyp);
1871 ASSERT(!(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) || level >= 1);
1874 * Go up the tree from this level toward the root.
1875 * At each level, update the key value to the value input.
1876 * Stop when we reach a level where the cursor isn't pointing
1877 * at the first entry in the block.
1879 for (ptr = 1; ptr == 1 && level < cur->bc_nlevels; level++) {
1883 block = xfs_btree_get_block(cur, level, &bp);
1885 error = xfs_btree_check_block(cur, block, level, bp);
1887 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
1891 ptr = cur->bc_ptrs[level];
1892 kp = xfs_btree_key_addr(cur, ptr, block);
1893 xfs_btree_copy_keys(cur, kp, keyp, 1);
1894 xfs_btree_log_keys(cur, bp, ptr, ptr);
1897 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1902 * Update the record referred to by cur to the value in the
1903 * given record. This either works (return 0) or gets an
1904 * EFSCORRUPTED error.
1908 struct xfs_btree_cur *cur,
1909 union xfs_btree_rec *rec)
1911 struct xfs_btree_block *block;
1915 union xfs_btree_rec *rp;
1917 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1918 XFS_BTREE_TRACE_ARGR(cur, rec);
1920 /* Pick up the current block. */
1921 block = xfs_btree_get_block(cur, 0, &bp);
1924 error = xfs_btree_check_block(cur, block, 0, bp);
1928 /* Get the address of the rec to be updated. */
1929 ptr = cur->bc_ptrs[0];
1930 rp = xfs_btree_rec_addr(cur, ptr, block);
1932 /* Fill in the new contents and log them. */
1933 xfs_btree_copy_recs(cur, rp, rec, 1);
1934 xfs_btree_log_recs(cur, bp, ptr, ptr);
1937 * If we are tracking the last record in the tree and
1938 * we are at the far right edge of the tree, update it.
1940 if (xfs_btree_is_lastrec(cur, block, 0)) {
1941 cur->bc_ops->update_lastrec(cur, block, rec,
1942 ptr, LASTREC_UPDATE);
1945 /* Updating first rec in leaf. Pass new key value up to our parent. */
1947 union xfs_btree_key key;
1949 cur->bc_ops->init_key_from_rec(&key, rec);
1950 error = xfs_btree_updkey(cur, &key, 1);
1955 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1959 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
1964 * Move 1 record left from cur/level if possible.
1965 * Update cur to reflect the new path.
1967 STATIC int /* error */
1969 struct xfs_btree_cur *cur,
1971 int *stat) /* success/failure */
1973 union xfs_btree_key key; /* btree key */
1974 struct xfs_buf *lbp; /* left buffer pointer */
1975 struct xfs_btree_block *left; /* left btree block */
1976 int lrecs; /* left record count */
1977 struct xfs_buf *rbp; /* right buffer pointer */
1978 struct xfs_btree_block *right; /* right btree block */
1979 int rrecs; /* right record count */
1980 union xfs_btree_ptr lptr; /* left btree pointer */
1981 union xfs_btree_key *rkp = NULL; /* right btree key */
1982 union xfs_btree_ptr *rpp = NULL; /* right address pointer */
1983 union xfs_btree_rec *rrp = NULL; /* right record pointer */
1984 int error; /* error return value */
1986 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1987 XFS_BTREE_TRACE_ARGI(cur, level);
1989 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
1990 level == cur->bc_nlevels - 1)
1993 /* Set up variables for this block as "right". */
1994 right = xfs_btree_get_block(cur, level, &rbp);
1997 error = xfs_btree_check_block(cur, right, level, rbp);
2002 /* If we've got no left sibling then we can't shift an entry left. */
2003 xfs_btree_get_sibling(cur, right, &lptr, XFS_BB_LEFTSIB);
2004 if (xfs_btree_ptr_is_null(cur, &lptr))
2008 * If the cursor entry is the one that would be moved, don't
2009 * do it... it's too complicated.
2011 if (cur->bc_ptrs[level] <= 1)
2014 /* Set up the left neighbor as "left". */
2015 error = xfs_btree_read_buf_block(cur, &lptr, 0, &left, &lbp);
2019 /* If it's full, it can't take another entry. */
2020 lrecs = xfs_btree_get_numrecs(left);
2021 if (lrecs == cur->bc_ops->get_maxrecs(cur, level))
2024 rrecs = xfs_btree_get_numrecs(right);
2027 * We add one entry to the left side and remove one for the right side.
2028 * Account for it here, the changes will be updated on disk and logged
2034 XFS_BTREE_STATS_INC(cur, lshift);
2035 XFS_BTREE_STATS_ADD(cur, moves, 1);
2038 * If non-leaf, copy a key and a ptr to the left block.
2039 * Log the changes to the left block.
2042 /* It's a non-leaf. Move keys and pointers. */
2043 union xfs_btree_key *lkp; /* left btree key */
2044 union xfs_btree_ptr *lpp; /* left address pointer */
2046 lkp = xfs_btree_key_addr(cur, lrecs, left);
2047 rkp = xfs_btree_key_addr(cur, 1, right);
2049 lpp = xfs_btree_ptr_addr(cur, lrecs, left);
2050 rpp = xfs_btree_ptr_addr(cur, 1, right);
2052 error = xfs_btree_check_ptr(cur, rpp, 0, level);
2056 xfs_btree_copy_keys(cur, lkp, rkp, 1);
2057 xfs_btree_copy_ptrs(cur, lpp, rpp, 1);
2059 xfs_btree_log_keys(cur, lbp, lrecs, lrecs);
2060 xfs_btree_log_ptrs(cur, lbp, lrecs, lrecs);
2062 ASSERT(cur->bc_ops->keys_inorder(cur,
2063 xfs_btree_key_addr(cur, lrecs - 1, left), lkp));
2065 /* It's a leaf. Move records. */
2066 union xfs_btree_rec *lrp; /* left record pointer */
2068 lrp = xfs_btree_rec_addr(cur, lrecs, left);
2069 rrp = xfs_btree_rec_addr(cur, 1, right);
2071 xfs_btree_copy_recs(cur, lrp, rrp, 1);
2072 xfs_btree_log_recs(cur, lbp, lrecs, lrecs);
2074 ASSERT(cur->bc_ops->recs_inorder(cur,
2075 xfs_btree_rec_addr(cur, lrecs - 1, left), lrp));
2078 xfs_btree_set_numrecs(left, lrecs);
2079 xfs_btree_log_block(cur, lbp, XFS_BB_NUMRECS);
2081 xfs_btree_set_numrecs(right, rrecs);
2082 xfs_btree_log_block(cur, rbp, XFS_BB_NUMRECS);
2085 * Slide the contents of right down one entry.
2087 XFS_BTREE_STATS_ADD(cur, moves, rrecs - 1);
2089 /* It's a nonleaf. operate on keys and ptrs */
2091 int i; /* loop index */
2093 for (i = 0; i < rrecs; i++) {
2094 error = xfs_btree_check_ptr(cur, rpp, i + 1, level);
2099 xfs_btree_shift_keys(cur,
2100 xfs_btree_key_addr(cur, 2, right),
2102 xfs_btree_shift_ptrs(cur,
2103 xfs_btree_ptr_addr(cur, 2, right),
2106 xfs_btree_log_keys(cur, rbp, 1, rrecs);
2107 xfs_btree_log_ptrs(cur, rbp, 1, rrecs);
2109 /* It's a leaf. operate on records */
2110 xfs_btree_shift_recs(cur,
2111 xfs_btree_rec_addr(cur, 2, right),
2113 xfs_btree_log_recs(cur, rbp, 1, rrecs);
2116 * If it's the first record in the block, we'll need a key
2117 * structure to pass up to the next level (updkey).
2119 cur->bc_ops->init_key_from_rec(&key,
2120 xfs_btree_rec_addr(cur, 1, right));
2124 /* Update the parent key values of right. */
2125 error = xfs_btree_updkey(cur, rkp, level + 1);
2129 /* Slide the cursor value left one. */
2130 cur->bc_ptrs[level]--;
2132 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2137 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2142 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
2147 * Move 1 record right from cur/level if possible.
2148 * Update cur to reflect the new path.
2150 STATIC int /* error */
2152 struct xfs_btree_cur *cur,
2154 int *stat) /* success/failure */
2156 union xfs_btree_key key; /* btree key */
2157 struct xfs_buf *lbp; /* left buffer pointer */
2158 struct xfs_btree_block *left; /* left btree block */
2159 struct xfs_buf *rbp; /* right buffer pointer */
2160 struct xfs_btree_block *right; /* right btree block */
2161 struct xfs_btree_cur *tcur; /* temporary btree cursor */
2162 union xfs_btree_ptr rptr; /* right block pointer */
2163 union xfs_btree_key *rkp; /* right btree key */
2164 int rrecs; /* right record count */
2165 int lrecs; /* left record count */
2166 int error; /* error return value */
2167 int i; /* loop counter */
2169 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
2170 XFS_BTREE_TRACE_ARGI(cur, level);
2172 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
2173 (level == cur->bc_nlevels - 1))
2176 /* Set up variables for this block as "left". */
2177 left = xfs_btree_get_block(cur, level, &lbp);
2180 error = xfs_btree_check_block(cur, left, level, lbp);
2185 /* If we've got no right sibling then we can't shift an entry right. */
2186 xfs_btree_get_sibling(cur, left, &rptr, XFS_BB_RIGHTSIB);
2187 if (xfs_btree_ptr_is_null(cur, &rptr))
2191 * If the cursor entry is the one that would be moved, don't
2192 * do it... it's too complicated.
2194 lrecs = xfs_btree_get_numrecs(left);
2195 if (cur->bc_ptrs[level] >= lrecs)
2198 /* Set up the right neighbor as "right". */
2199 error = xfs_btree_read_buf_block(cur, &rptr, 0, &right, &rbp);
2203 /* If it's full, it can't take another entry. */
2204 rrecs = xfs_btree_get_numrecs(right);
2205 if (rrecs == cur->bc_ops->get_maxrecs(cur, level))
2208 XFS_BTREE_STATS_INC(cur, rshift);
2209 XFS_BTREE_STATS_ADD(cur, moves, rrecs);
2212 * Make a hole at the start of the right neighbor block, then
2213 * copy the last left block entry to the hole.
2216 /* It's a nonleaf. make a hole in the keys and ptrs */
2217 union xfs_btree_key *lkp;
2218 union xfs_btree_ptr *lpp;
2219 union xfs_btree_ptr *rpp;
2221 lkp = xfs_btree_key_addr(cur, lrecs, left);
2222 lpp = xfs_btree_ptr_addr(cur, lrecs, left);
2223 rkp = xfs_btree_key_addr(cur, 1, right);
2224 rpp = xfs_btree_ptr_addr(cur, 1, right);
2227 for (i = rrecs - 1; i >= 0; i--) {
2228 error = xfs_btree_check_ptr(cur, rpp, i, level);
2234 xfs_btree_shift_keys(cur, rkp, 1, rrecs);
2235 xfs_btree_shift_ptrs(cur, rpp, 1, rrecs);
2238 error = xfs_btree_check_ptr(cur, lpp, 0, level);
2243 /* Now put the new data in, and log it. */
2244 xfs_btree_copy_keys(cur, rkp, lkp, 1);
2245 xfs_btree_copy_ptrs(cur, rpp, lpp, 1);
2247 xfs_btree_log_keys(cur, rbp, 1, rrecs + 1);
2248 xfs_btree_log_ptrs(cur, rbp, 1, rrecs + 1);
2250 ASSERT(cur->bc_ops->keys_inorder(cur, rkp,
2251 xfs_btree_key_addr(cur, 2, right)));
2253 /* It's a leaf. make a hole in the records */
2254 union xfs_btree_rec *lrp;
2255 union xfs_btree_rec *rrp;
2257 lrp = xfs_btree_rec_addr(cur, lrecs, left);
2258 rrp = xfs_btree_rec_addr(cur, 1, right);
2260 xfs_btree_shift_recs(cur, rrp, 1, rrecs);
2262 /* Now put the new data in, and log it. */
2263 xfs_btree_copy_recs(cur, rrp, lrp, 1);
2264 xfs_btree_log_recs(cur, rbp, 1, rrecs + 1);
2266 cur->bc_ops->init_key_from_rec(&key, rrp);
2269 ASSERT(cur->bc_ops->recs_inorder(cur, rrp,
2270 xfs_btree_rec_addr(cur, 2, right)));
2274 * Decrement and log left's numrecs, bump and log right's numrecs.
2276 xfs_btree_set_numrecs(left, --lrecs);
2277 xfs_btree_log_block(cur, lbp, XFS_BB_NUMRECS);
2279 xfs_btree_set_numrecs(right, ++rrecs);
2280 xfs_btree_log_block(cur, rbp, XFS_BB_NUMRECS);
2283 * Using a temporary cursor, update the parent key values of the
2284 * block on the right.
2286 error = xfs_btree_dup_cursor(cur, &tcur);
2289 i = xfs_btree_lastrec(tcur, level);
2290 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
2292 error = xfs_btree_increment(tcur, level, &i);
2296 error = xfs_btree_updkey(tcur, rkp, level + 1);
2300 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
2302 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2307 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2312 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
2316 XFS_BTREE_TRACE_CURSOR(tcur, XBT_ERROR);
2317 xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
2322 * Split cur/level block in half.
2323 * Return new block number and the key to its first
2324 * record (to be inserted into parent).
2326 STATIC int /* error */
2328 struct xfs_btree_cur *cur,
2330 union xfs_btree_ptr *ptrp,
2331 union xfs_btree_key *key,
2332 struct xfs_btree_cur **curp,
2333 int *stat) /* success/failure */
2335 union xfs_btree_ptr lptr; /* left sibling block ptr */
2336 struct xfs_buf *lbp; /* left buffer pointer */
2337 struct xfs_btree_block *left; /* left btree block */
2338 union xfs_btree_ptr rptr; /* right sibling block ptr */
2339 struct xfs_buf *rbp; /* right buffer pointer */
2340 struct xfs_btree_block *right; /* right btree block */
2341 union xfs_btree_ptr rrptr; /* right-right sibling ptr */
2342 struct xfs_buf *rrbp; /* right-right buffer pointer */
2343 struct xfs_btree_block *rrblock; /* right-right btree block */
2347 int error; /* error return value */
2352 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
2353 XFS_BTREE_TRACE_ARGIPK(cur, level, *ptrp, key);
2355 XFS_BTREE_STATS_INC(cur, split);
2357 /* Set up left block (current one). */
2358 left = xfs_btree_get_block(cur, level, &lbp);
2361 error = xfs_btree_check_block(cur, left, level, lbp);
2366 xfs_btree_buf_to_ptr(cur, lbp, &lptr);
2368 /* Allocate the new block. If we can't do it, we're toast. Give up. */
2369 error = cur->bc_ops->alloc_block(cur, &lptr, &rptr, stat);
2374 XFS_BTREE_STATS_INC(cur, alloc);
2376 /* Set up the new block as "right". */
2377 error = xfs_btree_get_buf_block(cur, &rptr, 0, &right, &rbp);
2381 /* Fill in the btree header for the new right block. */
2382 xfs_btree_init_block_cur(cur, rbp, xfs_btree_get_level(left), 0);
2385 * Split the entries between the old and the new block evenly.
2386 * Make sure that if there's an odd number of entries now, that
2387 * each new block will have the same number of entries.
2389 lrecs = xfs_btree_get_numrecs(left);
2391 if ((lrecs & 1) && cur->bc_ptrs[level] <= rrecs + 1)
2393 src_index = (lrecs - rrecs + 1);
2395 XFS_BTREE_STATS_ADD(cur, moves, rrecs);
2398 * Copy btree block entries from the left block over to the
2399 * new block, the right. Update the right block and log the
2403 /* It's a non-leaf. Move keys and pointers. */
2404 union xfs_btree_key *lkp; /* left btree key */
2405 union xfs_btree_ptr *lpp; /* left address pointer */
2406 union xfs_btree_key *rkp; /* right btree key */
2407 union xfs_btree_ptr *rpp; /* right address pointer */
2409 lkp = xfs_btree_key_addr(cur, src_index, left);
2410 lpp = xfs_btree_ptr_addr(cur, src_index, left);
2411 rkp = xfs_btree_key_addr(cur, 1, right);
2412 rpp = xfs_btree_ptr_addr(cur, 1, right);
2415 for (i = src_index; i < rrecs; i++) {
2416 error = xfs_btree_check_ptr(cur, lpp, i, level);
2422 xfs_btree_copy_keys(cur, rkp, lkp, rrecs);
2423 xfs_btree_copy_ptrs(cur, rpp, lpp, rrecs);
2425 xfs_btree_log_keys(cur, rbp, 1, rrecs);
2426 xfs_btree_log_ptrs(cur, rbp, 1, rrecs);
2428 /* Grab the keys to the entries moved to the right block */
2429 xfs_btree_copy_keys(cur, key, rkp, 1);
2431 /* It's a leaf. Move records. */
2432 union xfs_btree_rec *lrp; /* left record pointer */
2433 union xfs_btree_rec *rrp; /* right record pointer */
2435 lrp = xfs_btree_rec_addr(cur, src_index, left);
2436 rrp = xfs_btree_rec_addr(cur, 1, right);
2438 xfs_btree_copy_recs(cur, rrp, lrp, rrecs);
2439 xfs_btree_log_recs(cur, rbp, 1, rrecs);
2441 cur->bc_ops->init_key_from_rec(key,
2442 xfs_btree_rec_addr(cur, 1, right));
2447 * Find the left block number by looking in the buffer.
2448 * Adjust numrecs, sibling pointers.
2450 xfs_btree_get_sibling(cur, left, &rrptr, XFS_BB_RIGHTSIB);
2451 xfs_btree_set_sibling(cur, right, &rrptr, XFS_BB_RIGHTSIB);
2452 xfs_btree_set_sibling(cur, right, &lptr, XFS_BB_LEFTSIB);
2453 xfs_btree_set_sibling(cur, left, &rptr, XFS_BB_RIGHTSIB);
2456 xfs_btree_set_numrecs(left, lrecs);
2457 xfs_btree_set_numrecs(right, xfs_btree_get_numrecs(right) + rrecs);
2459 xfs_btree_log_block(cur, rbp, XFS_BB_ALL_BITS);
2460 xfs_btree_log_block(cur, lbp, XFS_BB_NUMRECS | XFS_BB_RIGHTSIB);
2463 * If there's a block to the new block's right, make that block
2464 * point back to right instead of to left.
2466 if (!xfs_btree_ptr_is_null(cur, &rrptr)) {
2467 error = xfs_btree_read_buf_block(cur, &rrptr,
2468 0, &rrblock, &rrbp);
2471 xfs_btree_set_sibling(cur, rrblock, &rptr, XFS_BB_LEFTSIB);
2472 xfs_btree_log_block(cur, rrbp, XFS_BB_LEFTSIB);
2475 * If the cursor is really in the right block, move it there.
2476 * If it's just pointing past the last entry in left, then we'll
2477 * insert there, so don't change anything in that case.
2479 if (cur->bc_ptrs[level] > lrecs + 1) {
2480 xfs_btree_setbuf(cur, level, rbp);
2481 cur->bc_ptrs[level] -= lrecs;
2484 * If there are more levels, we'll need another cursor which refers
2485 * the right block, no matter where this cursor was.
2487 if (level + 1 < cur->bc_nlevels) {
2488 error = xfs_btree_dup_cursor(cur, curp);
2491 (*curp)->bc_ptrs[level + 1]++;
2494 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2498 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2503 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
2507 struct xfs_btree_split_args {
2508 struct xfs_btree_cur *cur;
2510 union xfs_btree_ptr *ptrp;
2511 union xfs_btree_key *key;
2512 struct xfs_btree_cur **curp;
2513 int *stat; /* success/failure */
2515 bool kswapd; /* allocation in kswapd context */
2516 struct completion *done;
2517 struct work_struct work;
2521 * Stack switching interfaces for allocation
2524 xfs_btree_split_worker(
2525 struct work_struct *work)
2527 struct xfs_btree_split_args *args = container_of(work,
2528 struct xfs_btree_split_args, work);
2529 unsigned long pflags;
2530 unsigned long new_pflags = PF_FSTRANS;
2533 * we are in a transaction context here, but may also be doing work
2534 * in kswapd context, and hence we may need to inherit that state
2535 * temporarily to ensure that we don't block waiting for memory reclaim
2539 new_pflags |= PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD;
2541 current_set_flags_nested(&pflags, new_pflags);
2543 args->result = __xfs_btree_split(args->cur, args->level, args->ptrp,
2544 args->key, args->curp, args->stat);
2545 complete(args->done);
2547 current_restore_flags_nested(&pflags, new_pflags);
2551 * BMBT split requests often come in with little stack to work on. Push
2552 * them off to a worker thread so there is lots of stack to use. For the other
2553 * btree types, just call directly to avoid the context switch overhead here.
2555 STATIC int /* error */
2557 struct xfs_btree_cur *cur,
2559 union xfs_btree_ptr *ptrp,
2560 union xfs_btree_key *key,
2561 struct xfs_btree_cur **curp,
2562 int *stat) /* success/failure */
2564 struct xfs_btree_split_args args;
2565 DECLARE_COMPLETION_ONSTACK(done);
2567 if (cur->bc_btnum != XFS_BTNUM_BMAP)
2568 return __xfs_btree_split(cur, level, ptrp, key, curp, stat);
2577 args.kswapd = current_is_kswapd();
2578 INIT_WORK_ONSTACK(&args.work, xfs_btree_split_worker);
2579 queue_work(xfs_alloc_wq, &args.work);
2580 wait_for_completion(&done);
2581 destroy_work_on_stack(&args.work);
2587 * Copy the old inode root contents into a real block and make the
2588 * broot point to it.
2591 xfs_btree_new_iroot(
2592 struct xfs_btree_cur *cur, /* btree cursor */
2593 int *logflags, /* logging flags for inode */
2594 int *stat) /* return status - 0 fail */
2596 struct xfs_buf *cbp; /* buffer for cblock */
2597 struct xfs_btree_block *block; /* btree block */
2598 struct xfs_btree_block *cblock; /* child btree block */
2599 union xfs_btree_key *ckp; /* child key pointer */
2600 union xfs_btree_ptr *cpp; /* child ptr pointer */
2601 union xfs_btree_key *kp; /* pointer to btree key */
2602 union xfs_btree_ptr *pp; /* pointer to block addr */
2603 union xfs_btree_ptr nptr; /* new block addr */
2604 int level; /* btree level */
2605 int error; /* error return code */
2607 int i; /* loop counter */
2610 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
2611 XFS_BTREE_STATS_INC(cur, newroot);
2613 ASSERT(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE);
2615 level = cur->bc_nlevels - 1;
2617 block = xfs_btree_get_iroot(cur);
2618 pp = xfs_btree_ptr_addr(cur, 1, block);
2620 /* Allocate the new block. If we can't do it, we're toast. Give up. */
2621 error = cur->bc_ops->alloc_block(cur, pp, &nptr, stat);
2625 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2628 XFS_BTREE_STATS_INC(cur, alloc);
2630 /* Copy the root into a real block. */
2631 error = xfs_btree_get_buf_block(cur, &nptr, 0, &cblock, &cbp);
2636 * we can't just memcpy() the root in for CRC enabled btree blocks.
2637 * In that case have to also ensure the blkno remains correct
2639 memcpy(cblock, block, xfs_btree_block_len(cur));
2640 if (cur->bc_flags & XFS_BTREE_CRC_BLOCKS) {
2641 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
2642 cblock->bb_u.l.bb_blkno = cpu_to_be64(cbp->b_bn);
2644 cblock->bb_u.s.bb_blkno = cpu_to_be64(cbp->b_bn);
2647 be16_add_cpu(&block->bb_level, 1);
2648 xfs_btree_set_numrecs(block, 1);
2650 cur->bc_ptrs[level + 1] = 1;
2652 kp = xfs_btree_key_addr(cur, 1, block);
2653 ckp = xfs_btree_key_addr(cur, 1, cblock);
2654 xfs_btree_copy_keys(cur, ckp, kp, xfs_btree_get_numrecs(cblock));
2656 cpp = xfs_btree_ptr_addr(cur, 1, cblock);
2658 for (i = 0; i < be16_to_cpu(cblock->bb_numrecs); i++) {
2659 error = xfs_btree_check_ptr(cur, pp, i, level);
2664 xfs_btree_copy_ptrs(cur, cpp, pp, xfs_btree_get_numrecs(cblock));
2667 error = xfs_btree_check_ptr(cur, &nptr, 0, level);
2671 xfs_btree_copy_ptrs(cur, pp, &nptr, 1);
2673 xfs_iroot_realloc(cur->bc_private.b.ip,
2674 1 - xfs_btree_get_numrecs(cblock),
2675 cur->bc_private.b.whichfork);
2677 xfs_btree_setbuf(cur, level, cbp);
2680 * Do all this logging at the end so that
2681 * the root is at the right level.
2683 xfs_btree_log_block(cur, cbp, XFS_BB_ALL_BITS);
2684 xfs_btree_log_keys(cur, cbp, 1, be16_to_cpu(cblock->bb_numrecs));
2685 xfs_btree_log_ptrs(cur, cbp, 1, be16_to_cpu(cblock->bb_numrecs));
2688 XFS_ILOG_CORE | xfs_ilog_fbroot(cur->bc_private.b.whichfork);
2690 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2693 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
2698 * Allocate a new root block, fill it in.
2700 STATIC int /* error */
2702 struct xfs_btree_cur *cur, /* btree cursor */
2703 int *stat) /* success/failure */
2705 struct xfs_btree_block *block; /* one half of the old root block */
2706 struct xfs_buf *bp; /* buffer containing block */
2707 int error; /* error return value */
2708 struct xfs_buf *lbp; /* left buffer pointer */
2709 struct xfs_btree_block *left; /* left btree block */
2710 struct xfs_buf *nbp; /* new (root) buffer */
2711 struct xfs_btree_block *new; /* new (root) btree block */
2712 int nptr; /* new value for key index, 1 or 2 */
2713 struct xfs_buf *rbp; /* right buffer pointer */
2714 struct xfs_btree_block *right; /* right btree block */
2715 union xfs_btree_ptr rptr;
2716 union xfs_btree_ptr lptr;
2718 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
2719 XFS_BTREE_STATS_INC(cur, newroot);
2721 /* initialise our start point from the cursor */
2722 cur->bc_ops->init_ptr_from_cur(cur, &rptr);
2724 /* Allocate the new block. If we can't do it, we're toast. Give up. */
2725 error = cur->bc_ops->alloc_block(cur, &rptr, &lptr, stat);
2730 XFS_BTREE_STATS_INC(cur, alloc);
2732 /* Set up the new block. */
2733 error = xfs_btree_get_buf_block(cur, &lptr, 0, &new, &nbp);
2737 /* Set the root in the holding structure increasing the level by 1. */
2738 cur->bc_ops->set_root(cur, &lptr, 1);
2741 * At the previous root level there are now two blocks: the old root,
2742 * and the new block generated when it was split. We don't know which
2743 * one the cursor is pointing at, so we set up variables "left" and
2744 * "right" for each case.
2746 block = xfs_btree_get_block(cur, cur->bc_nlevels - 1, &bp);
2749 error = xfs_btree_check_block(cur, block, cur->bc_nlevels - 1, bp);
2754 xfs_btree_get_sibling(cur, block, &rptr, XFS_BB_RIGHTSIB);
2755 if (!xfs_btree_ptr_is_null(cur, &rptr)) {
2756 /* Our block is left, pick up the right block. */
2758 xfs_btree_buf_to_ptr(cur, lbp, &lptr);
2760 error = xfs_btree_read_buf_block(cur, &rptr, 0, &right, &rbp);
2766 /* Our block is right, pick up the left block. */
2768 xfs_btree_buf_to_ptr(cur, rbp, &rptr);
2770 xfs_btree_get_sibling(cur, right, &lptr, XFS_BB_LEFTSIB);
2771 error = xfs_btree_read_buf_block(cur, &lptr, 0, &left, &lbp);
2777 /* Fill in the new block's btree header and log it. */
2778 xfs_btree_init_block_cur(cur, nbp, cur->bc_nlevels, 2);
2779 xfs_btree_log_block(cur, nbp, XFS_BB_ALL_BITS);
2780 ASSERT(!xfs_btree_ptr_is_null(cur, &lptr) &&
2781 !xfs_btree_ptr_is_null(cur, &rptr));
2783 /* Fill in the key data in the new root. */
2784 if (xfs_btree_get_level(left) > 0) {
2785 xfs_btree_copy_keys(cur,
2786 xfs_btree_key_addr(cur, 1, new),
2787 xfs_btree_key_addr(cur, 1, left), 1);
2788 xfs_btree_copy_keys(cur,
2789 xfs_btree_key_addr(cur, 2, new),
2790 xfs_btree_key_addr(cur, 1, right), 1);
2792 cur->bc_ops->init_key_from_rec(
2793 xfs_btree_key_addr(cur, 1, new),
2794 xfs_btree_rec_addr(cur, 1, left));
2795 cur->bc_ops->init_key_from_rec(
2796 xfs_btree_key_addr(cur, 2, new),
2797 xfs_btree_rec_addr(cur, 1, right));
2799 xfs_btree_log_keys(cur, nbp, 1, 2);
2801 /* Fill in the pointer data in the new root. */
2802 xfs_btree_copy_ptrs(cur,
2803 xfs_btree_ptr_addr(cur, 1, new), &lptr, 1);
2804 xfs_btree_copy_ptrs(cur,
2805 xfs_btree_ptr_addr(cur, 2, new), &rptr, 1);
2806 xfs_btree_log_ptrs(cur, nbp, 1, 2);
2808 /* Fix up the cursor. */
2809 xfs_btree_setbuf(cur, cur->bc_nlevels, nbp);
2810 cur->bc_ptrs[cur->bc_nlevels] = nptr;
2812 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2816 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
2819 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2825 xfs_btree_make_block_unfull(
2826 struct xfs_btree_cur *cur, /* btree cursor */
2827 int level, /* btree level */
2828 int numrecs,/* # of recs in block */
2829 int *oindex,/* old tree index */
2830 int *index, /* new tree index */
2831 union xfs_btree_ptr *nptr, /* new btree ptr */
2832 struct xfs_btree_cur **ncur, /* new btree cursor */
2833 union xfs_btree_rec *nrec, /* new record */
2836 union xfs_btree_key key; /* new btree key value */
2839 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
2840 level == cur->bc_nlevels - 1) {
2841 struct xfs_inode *ip = cur->bc_private.b.ip;
2843 if (numrecs < cur->bc_ops->get_dmaxrecs(cur, level)) {
2844 /* A root block that can be made bigger. */
2845 xfs_iroot_realloc(ip, 1, cur->bc_private.b.whichfork);
2847 /* A root block that needs replacing */
2850 error = xfs_btree_new_iroot(cur, &logflags, stat);
2851 if (error || *stat == 0)
2854 xfs_trans_log_inode(cur->bc_tp, ip, logflags);
2860 /* First, try shifting an entry to the right neighbor. */
2861 error = xfs_btree_rshift(cur, level, stat);
2865 /* Next, try shifting an entry to the left neighbor. */
2866 error = xfs_btree_lshift(cur, level, stat);
2871 *oindex = *index = cur->bc_ptrs[level];
2876 * Next, try splitting the current block in half.
2878 * If this works we have to re-set our variables because we
2879 * could be in a different block now.
2881 error = xfs_btree_split(cur, level, nptr, &key, ncur, stat);
2882 if (error || *stat == 0)
2886 *index = cur->bc_ptrs[level];
2887 cur->bc_ops->init_rec_from_key(&key, nrec);
2892 * Insert one record/level. Return information to the caller
2893 * allowing the next level up to proceed if necessary.
2897 struct xfs_btree_cur *cur, /* btree cursor */
2898 int level, /* level to insert record at */
2899 union xfs_btree_ptr *ptrp, /* i/o: block number inserted */
2900 union xfs_btree_rec *recp, /* i/o: record data inserted */
2901 struct xfs_btree_cur **curp, /* output: new cursor replacing cur */
2902 int *stat) /* success/failure */
2904 struct xfs_btree_block *block; /* btree block */
2905 struct xfs_buf *bp; /* buffer for block */
2906 union xfs_btree_key key; /* btree key */
2907 union xfs_btree_ptr nptr; /* new block ptr */
2908 struct xfs_btree_cur *ncur; /* new btree cursor */
2909 union xfs_btree_rec nrec; /* new record count */
2910 int optr; /* old key/record index */
2911 int ptr; /* key/record index */
2912 int numrecs;/* number of records */
2913 int error; /* error return value */
2918 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
2919 XFS_BTREE_TRACE_ARGIPR(cur, level, *ptrp, recp);
2924 * If we have an external root pointer, and we've made it to the
2925 * root level, allocate a new root block and we're done.
2927 if (!(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
2928 (level >= cur->bc_nlevels)) {
2929 error = xfs_btree_new_root(cur, stat);
2930 xfs_btree_set_ptr_null(cur, ptrp);
2932 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2936 /* If we're off the left edge, return failure. */
2937 ptr = cur->bc_ptrs[level];
2939 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2944 /* Make a key out of the record data to be inserted, and save it. */
2945 cur->bc_ops->init_key_from_rec(&key, recp);
2949 XFS_BTREE_STATS_INC(cur, insrec);
2951 /* Get pointers to the btree buffer and block. */
2952 block = xfs_btree_get_block(cur, level, &bp);
2953 numrecs = xfs_btree_get_numrecs(block);
2956 error = xfs_btree_check_block(cur, block, level, bp);
2960 /* Check that the new entry is being inserted in the right place. */
2961 if (ptr <= numrecs) {
2963 ASSERT(cur->bc_ops->recs_inorder(cur, recp,
2964 xfs_btree_rec_addr(cur, ptr, block)));
2966 ASSERT(cur->bc_ops->keys_inorder(cur, &key,
2967 xfs_btree_key_addr(cur, ptr, block)));
2973 * If the block is full, we can't insert the new entry until we
2974 * make the block un-full.
2976 xfs_btree_set_ptr_null(cur, &nptr);
2977 if (numrecs == cur->bc_ops->get_maxrecs(cur, level)) {
2978 error = xfs_btree_make_block_unfull(cur, level, numrecs,
2979 &optr, &ptr, &nptr, &ncur, &nrec, stat);
2980 if (error || *stat == 0)
2985 * The current block may have changed if the block was
2986 * previously full and we have just made space in it.
2988 block = xfs_btree_get_block(cur, level, &bp);
2989 numrecs = xfs_btree_get_numrecs(block);
2992 error = xfs_btree_check_block(cur, block, level, bp);
2998 * At this point we know there's room for our new entry in the block
2999 * we're pointing at.
3001 XFS_BTREE_STATS_ADD(cur, moves, numrecs - ptr + 1);
3004 /* It's a nonleaf. make a hole in the keys and ptrs */
3005 union xfs_btree_key *kp;
3006 union xfs_btree_ptr *pp;
3008 kp = xfs_btree_key_addr(cur, ptr, block);
3009 pp = xfs_btree_ptr_addr(cur, ptr, block);
3012 for (i = numrecs - ptr; i >= 0; i--) {
3013 error = xfs_btree_check_ptr(cur, pp, i, level);
3019 xfs_btree_shift_keys(cur, kp, 1, numrecs - ptr + 1);
3020 xfs_btree_shift_ptrs(cur, pp, 1, numrecs - ptr + 1);
3023 error = xfs_btree_check_ptr(cur, ptrp, 0, level);
3028 /* Now put the new data in, bump numrecs and log it. */
3029 xfs_btree_copy_keys(cur, kp, &key, 1);
3030 xfs_btree_copy_ptrs(cur, pp, ptrp, 1);
3032 xfs_btree_set_numrecs(block, numrecs);
3033 xfs_btree_log_ptrs(cur, bp, ptr, numrecs);
3034 xfs_btree_log_keys(cur, bp, ptr, numrecs);
3036 if (ptr < numrecs) {
3037 ASSERT(cur->bc_ops->keys_inorder(cur, kp,
3038 xfs_btree_key_addr(cur, ptr + 1, block)));
3042 /* It's a leaf. make a hole in the records */
3043 union xfs_btree_rec *rp;
3045 rp = xfs_btree_rec_addr(cur, ptr, block);
3047 xfs_btree_shift_recs(cur, rp, 1, numrecs - ptr + 1);
3049 /* Now put the new data in, bump numrecs and log it. */
3050 xfs_btree_copy_recs(cur, rp, recp, 1);
3051 xfs_btree_set_numrecs(block, ++numrecs);
3052 xfs_btree_log_recs(cur, bp, ptr, numrecs);
3054 if (ptr < numrecs) {
3055 ASSERT(cur->bc_ops->recs_inorder(cur, rp,
3056 xfs_btree_rec_addr(cur, ptr + 1, block)));
3061 /* Log the new number of records in the btree header. */
3062 xfs_btree_log_block(cur, bp, XFS_BB_NUMRECS);
3064 /* If we inserted at the start of a block, update the parents' keys. */
3066 error = xfs_btree_updkey(cur, &key, level + 1);
3072 * If we are tracking the last record in the tree and
3073 * we are at the far right edge of the tree, update it.
3075 if (xfs_btree_is_lastrec(cur, block, level)) {
3076 cur->bc_ops->update_lastrec(cur, block, recp,
3077 ptr, LASTREC_INSREC);
3081 * Return the new block number, if any.
3082 * If there is one, give back a record value and a cursor too.
3085 if (!xfs_btree_ptr_is_null(cur, &nptr)) {
3090 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3095 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
3100 * Insert the record at the point referenced by cur.
3102 * A multi-level split of the tree on insert will invalidate the original
3103 * cursor. All callers of this function should assume that the cursor is
3104 * no longer valid and revalidate it.
3108 struct xfs_btree_cur *cur,
3111 int error; /* error return value */
3112 int i; /* result value, 0 for failure */
3113 int level; /* current level number in btree */
3114 union xfs_btree_ptr nptr; /* new block number (split result) */
3115 struct xfs_btree_cur *ncur; /* new cursor (split result) */
3116 struct xfs_btree_cur *pcur; /* previous level's cursor */
3117 union xfs_btree_rec rec; /* record to insert */
3123 xfs_btree_set_ptr_null(cur, &nptr);
3124 cur->bc_ops->init_rec_from_cur(cur, &rec);
3127 * Loop going up the tree, starting at the leaf level.
3128 * Stop when we don't get a split block, that must mean that
3129 * the insert is finished with this level.
3133 * Insert nrec/nptr into this level of the tree.
3134 * Note if we fail, nptr will be null.
3136 error = xfs_btree_insrec(pcur, level, &nptr, &rec, &ncur, &i);
3139 xfs_btree_del_cursor(pcur, XFS_BTREE_ERROR);
3143 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
3147 * See if the cursor we just used is trash.
3148 * Can't trash the caller's cursor, but otherwise we should
3149 * if ncur is a new cursor or we're about to be done.
3152 (ncur || xfs_btree_ptr_is_null(cur, &nptr))) {
3153 /* Save the state from the cursor before we trash it */
3154 if (cur->bc_ops->update_cursor)
3155 cur->bc_ops->update_cursor(pcur, cur);
3156 cur->bc_nlevels = pcur->bc_nlevels;
3157 xfs_btree_del_cursor(pcur, XFS_BTREE_NOERROR);
3159 /* If we got a new cursor, switch to it. */
3164 } while (!xfs_btree_ptr_is_null(cur, &nptr));
3166 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3170 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
3175 * Try to merge a non-leaf block back into the inode root.
3177 * Note: the killroot names comes from the fact that we're effectively
3178 * killing the old root block. But because we can't just delete the
3179 * inode we have to copy the single block it was pointing to into the
3183 xfs_btree_kill_iroot(
3184 struct xfs_btree_cur *cur)
3186 int whichfork = cur->bc_private.b.whichfork;
3187 struct xfs_inode *ip = cur->bc_private.b.ip;
3188 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
3189 struct xfs_btree_block *block;
3190 struct xfs_btree_block *cblock;
3191 union xfs_btree_key *kp;
3192 union xfs_btree_key *ckp;
3193 union xfs_btree_ptr *pp;
3194 union xfs_btree_ptr *cpp;
3195 struct xfs_buf *cbp;
3200 union xfs_btree_ptr ptr;
3204 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
3206 ASSERT(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE);
3207 ASSERT(cur->bc_nlevels > 1);
3210 * Don't deal with the root block needs to be a leaf case.
3211 * We're just going to turn the thing back into extents anyway.
3213 level = cur->bc_nlevels - 1;
3218 * Give up if the root has multiple children.
3220 block = xfs_btree_get_iroot(cur);
3221 if (xfs_btree_get_numrecs(block) != 1)
3224 cblock = xfs_btree_get_block(cur, level - 1, &cbp);
3225 numrecs = xfs_btree_get_numrecs(cblock);
3228 * Only do this if the next level will fit.
3229 * Then the data must be copied up to the inode,
3230 * instead of freeing the root you free the next level.
3232 if (numrecs > cur->bc_ops->get_dmaxrecs(cur, level))
3235 XFS_BTREE_STATS_INC(cur, killroot);
3238 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_LEFTSIB);
3239 ASSERT(xfs_btree_ptr_is_null(cur, &ptr));
3240 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_RIGHTSIB);
3241 ASSERT(xfs_btree_ptr_is_null(cur, &ptr));
3244 index = numrecs - cur->bc_ops->get_maxrecs(cur, level);
3246 xfs_iroot_realloc(cur->bc_private.b.ip, index,
3247 cur->bc_private.b.whichfork);
3248 block = ifp->if_broot;
3251 be16_add_cpu(&block->bb_numrecs, index);
3252 ASSERT(block->bb_numrecs == cblock->bb_numrecs);
3254 kp = xfs_btree_key_addr(cur, 1, block);
3255 ckp = xfs_btree_key_addr(cur, 1, cblock);
3256 xfs_btree_copy_keys(cur, kp, ckp, numrecs);
3258 pp = xfs_btree_ptr_addr(cur, 1, block);
3259 cpp = xfs_btree_ptr_addr(cur, 1, cblock);
3261 for (i = 0; i < numrecs; i++) {
3264 error = xfs_btree_check_ptr(cur, cpp, i, level - 1);
3266 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
3271 xfs_btree_copy_ptrs(cur, pp, cpp, numrecs);
3273 cur->bc_ops->free_block(cur, cbp);
3274 XFS_BTREE_STATS_INC(cur, free);
3276 cur->bc_bufs[level - 1] = NULL;
3277 be16_add_cpu(&block->bb_level, -1);
3278 xfs_trans_log_inode(cur->bc_tp, ip,
3279 XFS_ILOG_CORE | xfs_ilog_fbroot(cur->bc_private.b.whichfork));
3282 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3287 * Kill the current root node, and replace it with it's only child node.
3290 xfs_btree_kill_root(
3291 struct xfs_btree_cur *cur,
3294 union xfs_btree_ptr *newroot)
3298 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
3299 XFS_BTREE_STATS_INC(cur, killroot);
3302 * Update the root pointer, decreasing the level by 1 and then
3303 * free the old root.
3305 cur->bc_ops->set_root(cur, newroot, -1);
3307 error = cur->bc_ops->free_block(cur, bp);
3309 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
3313 XFS_BTREE_STATS_INC(cur, free);
3315 cur->bc_bufs[level] = NULL;
3316 cur->bc_ra[level] = 0;
3319 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3324 xfs_btree_dec_cursor(
3325 struct xfs_btree_cur *cur,
3333 error = xfs_btree_decrement(cur, level, &i);
3338 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3344 * Single level of the btree record deletion routine.
3345 * Delete record pointed to by cur/level.
3346 * Remove the record from its block then rebalance the tree.
3347 * Return 0 for error, 1 for done, 2 to go on to the next level.
3349 STATIC int /* error */
3351 struct xfs_btree_cur *cur, /* btree cursor */
3352 int level, /* level removing record from */
3353 int *stat) /* fail/done/go-on */
3355 struct xfs_btree_block *block; /* btree block */
3356 union xfs_btree_ptr cptr; /* current block ptr */
3357 struct xfs_buf *bp; /* buffer for block */
3358 int error; /* error return value */
3359 int i; /* loop counter */
3360 union xfs_btree_key key; /* storage for keyp */
3361 union xfs_btree_key *keyp = &key; /* passed to the next level */
3362 union xfs_btree_ptr lptr; /* left sibling block ptr */
3363 struct xfs_buf *lbp; /* left buffer pointer */
3364 struct xfs_btree_block *left; /* left btree block */
3365 int lrecs = 0; /* left record count */
3366 int ptr; /* key/record index */
3367 union xfs_btree_ptr rptr; /* right sibling block ptr */
3368 struct xfs_buf *rbp; /* right buffer pointer */
3369 struct xfs_btree_block *right; /* right btree block */
3370 struct xfs_btree_block *rrblock; /* right-right btree block */
3371 struct xfs_buf *rrbp; /* right-right buffer pointer */
3372 int rrecs = 0; /* right record count */
3373 struct xfs_btree_cur *tcur; /* temporary btree cursor */
3374 int numrecs; /* temporary numrec count */
3376 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
3377 XFS_BTREE_TRACE_ARGI(cur, level);
3381 /* Get the index of the entry being deleted, check for nothing there. */
3382 ptr = cur->bc_ptrs[level];
3384 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3389 /* Get the buffer & block containing the record or key/ptr. */
3390 block = xfs_btree_get_block(cur, level, &bp);
3391 numrecs = xfs_btree_get_numrecs(block);
3394 error = xfs_btree_check_block(cur, block, level, bp);
3399 /* Fail if we're off the end of the block. */
3400 if (ptr > numrecs) {
3401 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3406 XFS_BTREE_STATS_INC(cur, delrec);
3407 XFS_BTREE_STATS_ADD(cur, moves, numrecs - ptr);
3409 /* Excise the entries being deleted. */
3411 /* It's a nonleaf. operate on keys and ptrs */
3412 union xfs_btree_key *lkp;
3413 union xfs_btree_ptr *lpp;
3415 lkp = xfs_btree_key_addr(cur, ptr + 1, block);
3416 lpp = xfs_btree_ptr_addr(cur, ptr + 1, block);
3419 for (i = 0; i < numrecs - ptr; i++) {
3420 error = xfs_btree_check_ptr(cur, lpp, i, level);
3426 if (ptr < numrecs) {
3427 xfs_btree_shift_keys(cur, lkp, -1, numrecs - ptr);
3428 xfs_btree_shift_ptrs(cur, lpp, -1, numrecs - ptr);
3429 xfs_btree_log_keys(cur, bp, ptr, numrecs - 1);
3430 xfs_btree_log_ptrs(cur, bp, ptr, numrecs - 1);
3434 * If it's the first record in the block, we'll need to pass a
3435 * key up to the next level (updkey).
3438 keyp = xfs_btree_key_addr(cur, 1, block);
3440 /* It's a leaf. operate on records */
3441 if (ptr < numrecs) {
3442 xfs_btree_shift_recs(cur,
3443 xfs_btree_rec_addr(cur, ptr + 1, block),
3445 xfs_btree_log_recs(cur, bp, ptr, numrecs - 1);
3449 * If it's the first record in the block, we'll need a key
3450 * structure to pass up to the next level (updkey).
3453 cur->bc_ops->init_key_from_rec(&key,
3454 xfs_btree_rec_addr(cur, 1, block));
3460 * Decrement and log the number of entries in the block.
3462 xfs_btree_set_numrecs(block, --numrecs);
3463 xfs_btree_log_block(cur, bp, XFS_BB_NUMRECS);
3466 * If we are tracking the last record in the tree and
3467 * we are at the far right edge of the tree, update it.
3469 if (xfs_btree_is_lastrec(cur, block, level)) {
3470 cur->bc_ops->update_lastrec(cur, block, NULL,
3471 ptr, LASTREC_DELREC);
3475 * We're at the root level. First, shrink the root block in-memory.
3476 * Try to get rid of the next level down. If we can't then there's
3477 * nothing left to do.
3479 if (level == cur->bc_nlevels - 1) {
3480 if (cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) {
3481 xfs_iroot_realloc(cur->bc_private.b.ip, -1,
3482 cur->bc_private.b.whichfork);
3484 error = xfs_btree_kill_iroot(cur);
3488 error = xfs_btree_dec_cursor(cur, level, stat);
3496 * If this is the root level, and there's only one entry left,
3497 * and it's NOT the leaf level, then we can get rid of this
3500 if (numrecs == 1 && level > 0) {
3501 union xfs_btree_ptr *pp;
3503 * pp is still set to the first pointer in the block.
3504 * Make it the new root of the btree.
3506 pp = xfs_btree_ptr_addr(cur, 1, block);
3507 error = xfs_btree_kill_root(cur, bp, level, pp);
3510 } else if (level > 0) {
3511 error = xfs_btree_dec_cursor(cur, level, stat);
3520 * If we deleted the leftmost entry in the block, update the
3521 * key values above us in the tree.
3524 error = xfs_btree_updkey(cur, keyp, level + 1);
3530 * If the number of records remaining in the block is at least
3531 * the minimum, we're done.
3533 if (numrecs >= cur->bc_ops->get_minrecs(cur, level)) {
3534 error = xfs_btree_dec_cursor(cur, level, stat);
3541 * Otherwise, we have to move some records around to keep the
3542 * tree balanced. Look at the left and right sibling blocks to
3543 * see if we can re-balance by moving only one record.
3545 xfs_btree_get_sibling(cur, block, &rptr, XFS_BB_RIGHTSIB);
3546 xfs_btree_get_sibling(cur, block, &lptr, XFS_BB_LEFTSIB);
3548 if (cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) {
3550 * One child of root, need to get a chance to copy its contents
3551 * into the root and delete it. Can't go up to next level,
3552 * there's nothing to delete there.
3554 if (xfs_btree_ptr_is_null(cur, &rptr) &&
3555 xfs_btree_ptr_is_null(cur, &lptr) &&
3556 level == cur->bc_nlevels - 2) {
3557 error = xfs_btree_kill_iroot(cur);
3559 error = xfs_btree_dec_cursor(cur, level, stat);
3566 ASSERT(!xfs_btree_ptr_is_null(cur, &rptr) ||
3567 !xfs_btree_ptr_is_null(cur, &lptr));
3570 * Duplicate the cursor so our btree manipulations here won't
3571 * disrupt the next level up.
3573 error = xfs_btree_dup_cursor(cur, &tcur);
3578 * If there's a right sibling, see if it's ok to shift an entry
3581 if (!xfs_btree_ptr_is_null(cur, &rptr)) {
3583 * Move the temp cursor to the last entry in the next block.
3584 * Actually any entry but the first would suffice.
3586 i = xfs_btree_lastrec(tcur, level);
3587 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
3589 error = xfs_btree_increment(tcur, level, &i);
3592 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
3594 i = xfs_btree_lastrec(tcur, level);
3595 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
3597 /* Grab a pointer to the block. */
3598 right = xfs_btree_get_block(tcur, level, &rbp);
3600 error = xfs_btree_check_block(tcur, right, level, rbp);
3604 /* Grab the current block number, for future use. */
3605 xfs_btree_get_sibling(tcur, right, &cptr, XFS_BB_LEFTSIB);
3608 * If right block is full enough so that removing one entry
3609 * won't make it too empty, and left-shifting an entry out
3610 * of right to us works, we're done.
3612 if (xfs_btree_get_numrecs(right) - 1 >=
3613 cur->bc_ops->get_minrecs(tcur, level)) {
3614 error = xfs_btree_lshift(tcur, level, &i);
3618 ASSERT(xfs_btree_get_numrecs(block) >=
3619 cur->bc_ops->get_minrecs(tcur, level));
3621 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
3624 error = xfs_btree_dec_cursor(cur, level, stat);
3632 * Otherwise, grab the number of records in right for
3633 * future reference, and fix up the temp cursor to point
3634 * to our block again (last record).
3636 rrecs = xfs_btree_get_numrecs(right);
3637 if (!xfs_btree_ptr_is_null(cur, &lptr)) {
3638 i = xfs_btree_firstrec(tcur, level);
3639 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
3641 error = xfs_btree_decrement(tcur, level, &i);
3644 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
3649 * If there's a left sibling, see if it's ok to shift an entry
3652 if (!xfs_btree_ptr_is_null(cur, &lptr)) {
3654 * Move the temp cursor to the first entry in the
3657 i = xfs_btree_firstrec(tcur, level);
3658 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
3660 error = xfs_btree_decrement(tcur, level, &i);
3663 i = xfs_btree_firstrec(tcur, level);
3664 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
3666 /* Grab a pointer to the block. */
3667 left = xfs_btree_get_block(tcur, level, &lbp);
3669 error = xfs_btree_check_block(cur, left, level, lbp);
3673 /* Grab the current block number, for future use. */
3674 xfs_btree_get_sibling(tcur, left, &cptr, XFS_BB_RIGHTSIB);
3677 * If left block is full enough so that removing one entry
3678 * won't make it too empty, and right-shifting an entry out
3679 * of left to us works, we're done.
3681 if (xfs_btree_get_numrecs(left) - 1 >=
3682 cur->bc_ops->get_minrecs(tcur, level)) {
3683 error = xfs_btree_rshift(tcur, level, &i);
3687 ASSERT(xfs_btree_get_numrecs(block) >=
3688 cur->bc_ops->get_minrecs(tcur, level));
3689 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
3693 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3700 * Otherwise, grab the number of records in right for
3703 lrecs = xfs_btree_get_numrecs(left);
3706 /* Delete the temp cursor, we're done with it. */
3707 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
3710 /* If here, we need to do a join to keep the tree balanced. */
3711 ASSERT(!xfs_btree_ptr_is_null(cur, &cptr));
3713 if (!xfs_btree_ptr_is_null(cur, &lptr) &&
3714 lrecs + xfs_btree_get_numrecs(block) <=
3715 cur->bc_ops->get_maxrecs(cur, level)) {
3717 * Set "right" to be the starting block,
3718 * "left" to be the left neighbor.
3723 error = xfs_btree_read_buf_block(cur, &lptr, 0, &left, &lbp);
3728 * If that won't work, see if we can join with the right neighbor block.
3730 } else if (!xfs_btree_ptr_is_null(cur, &rptr) &&
3731 rrecs + xfs_btree_get_numrecs(block) <=
3732 cur->bc_ops->get_maxrecs(cur, level)) {
3734 * Set "left" to be the starting block,
3735 * "right" to be the right neighbor.
3740 error = xfs_btree_read_buf_block(cur, &rptr, 0, &right, &rbp);
3745 * Otherwise, we can't fix the imbalance.
3746 * Just return. This is probably a logic error, but it's not fatal.
3749 error = xfs_btree_dec_cursor(cur, level, stat);
3755 rrecs = xfs_btree_get_numrecs(right);
3756 lrecs = xfs_btree_get_numrecs(left);
3759 * We're now going to join "left" and "right" by moving all the stuff
3760 * in "right" to "left" and deleting "right".
3762 XFS_BTREE_STATS_ADD(cur, moves, rrecs);
3764 /* It's a non-leaf. Move keys and pointers. */
3765 union xfs_btree_key *lkp; /* left btree key */
3766 union xfs_btree_ptr *lpp; /* left address pointer */
3767 union xfs_btree_key *rkp; /* right btree key */
3768 union xfs_btree_ptr *rpp; /* right address pointer */
3770 lkp = xfs_btree_key_addr(cur, lrecs + 1, left);
3771 lpp = xfs_btree_ptr_addr(cur, lrecs + 1, left);
3772 rkp = xfs_btree_key_addr(cur, 1, right);
3773 rpp = xfs_btree_ptr_addr(cur, 1, right);
3775 for (i = 1; i < rrecs; i++) {
3776 error = xfs_btree_check_ptr(cur, rpp, i, level);
3781 xfs_btree_copy_keys(cur, lkp, rkp, rrecs);
3782 xfs_btree_copy_ptrs(cur, lpp, rpp, rrecs);
3784 xfs_btree_log_keys(cur, lbp, lrecs + 1, lrecs + rrecs);
3785 xfs_btree_log_ptrs(cur, lbp, lrecs + 1, lrecs + rrecs);
3787 /* It's a leaf. Move records. */
3788 union xfs_btree_rec *lrp; /* left record pointer */
3789 union xfs_btree_rec *rrp; /* right record pointer */
3791 lrp = xfs_btree_rec_addr(cur, lrecs + 1, left);
3792 rrp = xfs_btree_rec_addr(cur, 1, right);
3794 xfs_btree_copy_recs(cur, lrp, rrp, rrecs);
3795 xfs_btree_log_recs(cur, lbp, lrecs + 1, lrecs + rrecs);
3798 XFS_BTREE_STATS_INC(cur, join);
3801 * Fix up the number of records and right block pointer in the
3802 * surviving block, and log it.
3804 xfs_btree_set_numrecs(left, lrecs + rrecs);
3805 xfs_btree_get_sibling(cur, right, &cptr, XFS_BB_RIGHTSIB),
3806 xfs_btree_set_sibling(cur, left, &cptr, XFS_BB_RIGHTSIB);
3807 xfs_btree_log_block(cur, lbp, XFS_BB_NUMRECS | XFS_BB_RIGHTSIB);
3809 /* If there is a right sibling, point it to the remaining block. */
3810 xfs_btree_get_sibling(cur, left, &cptr, XFS_BB_RIGHTSIB);
3811 if (!xfs_btree_ptr_is_null(cur, &cptr)) {
3812 error = xfs_btree_read_buf_block(cur, &cptr, 0, &rrblock, &rrbp);
3815 xfs_btree_set_sibling(cur, rrblock, &lptr, XFS_BB_LEFTSIB);
3816 xfs_btree_log_block(cur, rrbp, XFS_BB_LEFTSIB);
3819 /* Free the deleted block. */
3820 error = cur->bc_ops->free_block(cur, rbp);
3823 XFS_BTREE_STATS_INC(cur, free);
3826 * If we joined with the left neighbor, set the buffer in the
3827 * cursor to the left block, and fix up the index.
3830 cur->bc_bufs[level] = lbp;
3831 cur->bc_ptrs[level] += lrecs;
3832 cur->bc_ra[level] = 0;
3835 * If we joined with the right neighbor and there's a level above
3836 * us, increment the cursor at that level.
3838 else if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) ||
3839 (level + 1 < cur->bc_nlevels)) {
3840 error = xfs_btree_increment(cur, level + 1, &i);
3846 * Readjust the ptr at this level if it's not a leaf, since it's
3847 * still pointing at the deletion point, which makes the cursor
3848 * inconsistent. If this makes the ptr 0, the caller fixes it up.
3849 * We can't use decrement because it would change the next level up.
3852 cur->bc_ptrs[level]--;
3854 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3855 /* Return value means the next level up has something to do. */
3860 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
3862 xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
3867 * Delete the record pointed to by cur.
3868 * The cursor refers to the place where the record was (could be inserted)
3869 * when the operation returns.
3873 struct xfs_btree_cur *cur,
3874 int *stat) /* success/failure */
3876 int error; /* error return value */
3880 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
3883 * Go up the tree, starting at leaf level.
3885 * If 2 is returned then a join was done; go to the next level.
3886 * Otherwise we are done.
3888 for (level = 0, i = 2; i == 2; level++) {
3889 error = xfs_btree_delrec(cur, level, &i);
3895 for (level = 1; level < cur->bc_nlevels; level++) {
3896 if (cur->bc_ptrs[level] == 0) {
3897 error = xfs_btree_decrement(cur, level, &i);
3905 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3909 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
3914 * Get the data from the pointed-to record.
3918 struct xfs_btree_cur *cur, /* btree cursor */
3919 union xfs_btree_rec **recp, /* output: btree record */
3920 int *stat) /* output: success/failure */
3922 struct xfs_btree_block *block; /* btree block */
3923 struct xfs_buf *bp; /* buffer pointer */
3924 int ptr; /* record number */
3926 int error; /* error return value */
3929 ptr = cur->bc_ptrs[0];
3930 block = xfs_btree_get_block(cur, 0, &bp);
3933 error = xfs_btree_check_block(cur, block, 0, bp);
3939 * Off the right end or left end, return failure.
3941 if (ptr > xfs_btree_get_numrecs(block) || ptr <= 0) {
3947 * Point to the record and extract its data.
3949 *recp = xfs_btree_rec_addr(cur, ptr, block);
3955 * Change the owner of a btree.
3957 * The mechanism we use here is ordered buffer logging. Because we don't know
3958 * how many buffers were are going to need to modify, we don't really want to
3959 * have to make transaction reservations for the worst case of every buffer in a
3960 * full size btree as that may be more space that we can fit in the log....
3962 * We do the btree walk in the most optimal manner possible - we have sibling
3963 * pointers so we can just walk all the blocks on each level from left to right
3964 * in a single pass, and then move to the next level and do the same. We can
3965 * also do readahead on the sibling pointers to get IO moving more quickly,
3966 * though for slow disks this is unlikely to make much difference to performance
3967 * as the amount of CPU work we have to do before moving to the next block is
3970 * For each btree block that we load, modify the owner appropriately, set the
3971 * buffer as an ordered buffer and log it appropriately. We need to ensure that
3972 * we mark the region we change dirty so that if the buffer is relogged in
3973 * a subsequent transaction the changes we make here as an ordered buffer are
3974 * correctly relogged in that transaction. If we are in recovery context, then
3975 * just queue the modified buffer as delayed write buffer so the transaction
3976 * recovery completion writes the changes to disk.
3979 xfs_btree_block_change_owner(
3980 struct xfs_btree_cur *cur,
3982 __uint64_t new_owner,
3983 struct list_head *buffer_list)
3985 struct xfs_btree_block *block;
3987 union xfs_btree_ptr rptr;
3989 /* do right sibling readahead */
3990 xfs_btree_readahead(cur, level, XFS_BTCUR_RIGHTRA);
3992 /* modify the owner */
3993 block = xfs_btree_get_block(cur, level, &bp);
3994 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
3995 block->bb_u.l.bb_owner = cpu_to_be64(new_owner);
3997 block->bb_u.s.bb_owner = cpu_to_be32(new_owner);
4000 * If the block is a root block hosted in an inode, we might not have a
4001 * buffer pointer here and we shouldn't attempt to log the change as the
4002 * information is already held in the inode and discarded when the root
4003 * block is formatted into the on-disk inode fork. We still change it,
4004 * though, so everything is consistent in memory.
4008 xfs_trans_ordered_buf(cur->bc_tp, bp);
4009 xfs_btree_log_block(cur, bp, XFS_BB_OWNER);
4011 xfs_buf_delwri_queue(bp, buffer_list);
4014 ASSERT(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE);
4015 ASSERT(level == cur->bc_nlevels - 1);
4018 /* now read rh sibling block for next iteration */
4019 xfs_btree_get_sibling(cur, block, &rptr, XFS_BB_RIGHTSIB);
4020 if (xfs_btree_ptr_is_null(cur, &rptr))
4023 return xfs_btree_lookup_get_block(cur, level, &rptr, &block);
4027 xfs_btree_change_owner(
4028 struct xfs_btree_cur *cur,
4029 __uint64_t new_owner,
4030 struct list_head *buffer_list)
4032 union xfs_btree_ptr lptr;
4034 struct xfs_btree_block *block = NULL;
4037 cur->bc_ops->init_ptr_from_cur(cur, &lptr);
4039 /* for each level */
4040 for (level = cur->bc_nlevels - 1; level >= 0; level--) {
4041 /* grab the left hand block */
4042 error = xfs_btree_lookup_get_block(cur, level, &lptr, &block);
4046 /* readahead the left most block for the next level down */
4048 union xfs_btree_ptr *ptr;
4050 ptr = xfs_btree_ptr_addr(cur, 1, block);
4051 xfs_btree_readahead_ptr(cur, ptr, 1);
4053 /* save for the next iteration of the loop */
4057 /* for each buffer in the level */
4059 error = xfs_btree_block_change_owner(cur, level,
4064 if (error != ENOENT)