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
18 #include "libxfs_priv.h"
20 #include "xfs_shared.h"
21 #include "xfs_format.h"
22 #include "xfs_log_format.h"
23 #include "xfs_trans_resv.h"
25 #include "xfs_mount.h"
26 #include "xfs_defer.h"
27 #include "xfs_inode.h"
28 #include "xfs_trans.h"
29 #include "xfs_btree.h"
30 #include "xfs_trace.h"
31 #include "xfs_cksum.h"
32 #include "xfs_alloc.h"
35 * Cursor allocation zone.
37 kmem_zone_t
*xfs_btree_cur_zone
;
40 * Btree magic numbers.
42 static const uint32_t xfs_magics
[2][XFS_BTNUM_MAX
] = {
43 { XFS_ABTB_MAGIC
, XFS_ABTC_MAGIC
, 0, XFS_BMAP_MAGIC
, XFS_IBT_MAGIC
,
45 { XFS_ABTB_CRC_MAGIC
, XFS_ABTC_CRC_MAGIC
, XFS_RMAP_CRC_MAGIC
,
46 XFS_BMAP_CRC_MAGIC
, XFS_IBT_CRC_MAGIC
, XFS_FIBT_CRC_MAGIC
,
55 uint32_t magic
= xfs_magics
[crc
][btnum
];
57 /* Ensure we asked for crc for crc-only magics. */
62 STATIC
int /* error (0 or EFSCORRUPTED) */
63 xfs_btree_check_lblock(
64 struct xfs_btree_cur
*cur
, /* btree cursor */
65 struct xfs_btree_block
*block
, /* btree long form block pointer */
66 int level
, /* level of the btree block */
67 struct xfs_buf
*bp
) /* buffer for block, if any */
69 int lblock_ok
= 1; /* block passes checks */
70 struct xfs_mount
*mp
; /* file system mount point */
71 xfs_btnum_t btnum
= cur
->bc_btnum
;
75 crc
= xfs_sb_version_hascrc(&mp
->m_sb
);
78 lblock_ok
= lblock_ok
&&
79 uuid_equal(&block
->bb_u
.l
.bb_uuid
,
80 &mp
->m_sb
.sb_meta_uuid
) &&
81 block
->bb_u
.l
.bb_blkno
== cpu_to_be64(
82 bp
? bp
->b_bn
: XFS_BUF_DADDR_NULL
);
85 lblock_ok
= lblock_ok
&&
86 be32_to_cpu(block
->bb_magic
) == xfs_btree_magic(crc
, btnum
) &&
87 be16_to_cpu(block
->bb_level
) == level
&&
88 be16_to_cpu(block
->bb_numrecs
) <=
89 cur
->bc_ops
->get_maxrecs(cur
, level
) &&
90 block
->bb_u
.l
.bb_leftsib
&&
91 (block
->bb_u
.l
.bb_leftsib
== cpu_to_be64(NULLFSBLOCK
) ||
92 XFS_FSB_SANITY_CHECK(mp
,
93 be64_to_cpu(block
->bb_u
.l
.bb_leftsib
))) &&
94 block
->bb_u
.l
.bb_rightsib
&&
95 (block
->bb_u
.l
.bb_rightsib
== cpu_to_be64(NULLFSBLOCK
) ||
96 XFS_FSB_SANITY_CHECK(mp
,
97 be64_to_cpu(block
->bb_u
.l
.bb_rightsib
)));
99 if (unlikely(XFS_TEST_ERROR(!lblock_ok
, mp
,
100 XFS_ERRTAG_BTREE_CHECK_LBLOCK
))) {
102 trace_xfs_btree_corrupt(bp
, _RET_IP_
);
103 XFS_ERROR_REPORT(__func__
, XFS_ERRLEVEL_LOW
, mp
);
104 return -EFSCORRUPTED
;
109 STATIC
int /* error (0 or EFSCORRUPTED) */
110 xfs_btree_check_sblock(
111 struct xfs_btree_cur
*cur
, /* btree cursor */
112 struct xfs_btree_block
*block
, /* btree short form block pointer */
113 int level
, /* level of the btree block */
114 struct xfs_buf
*bp
) /* buffer containing block */
116 struct xfs_mount
*mp
; /* file system mount point */
117 struct xfs_buf
*agbp
; /* buffer for ag. freespace struct */
118 struct xfs_agf
*agf
; /* ag. freespace structure */
119 xfs_agblock_t agflen
; /* native ag. freespace length */
120 int sblock_ok
= 1; /* block passes checks */
121 xfs_btnum_t btnum
= cur
->bc_btnum
;
125 crc
= xfs_sb_version_hascrc(&mp
->m_sb
);
126 agbp
= cur
->bc_private
.a
.agbp
;
127 agf
= XFS_BUF_TO_AGF(agbp
);
128 agflen
= be32_to_cpu(agf
->agf_length
);
131 sblock_ok
= sblock_ok
&&
132 uuid_equal(&block
->bb_u
.s
.bb_uuid
,
133 &mp
->m_sb
.sb_meta_uuid
) &&
134 block
->bb_u
.s
.bb_blkno
== cpu_to_be64(
135 bp
? bp
->b_bn
: XFS_BUF_DADDR_NULL
);
138 sblock_ok
= sblock_ok
&&
139 be32_to_cpu(block
->bb_magic
) == xfs_btree_magic(crc
, btnum
) &&
140 be16_to_cpu(block
->bb_level
) == level
&&
141 be16_to_cpu(block
->bb_numrecs
) <=
142 cur
->bc_ops
->get_maxrecs(cur
, level
) &&
143 (block
->bb_u
.s
.bb_leftsib
== cpu_to_be32(NULLAGBLOCK
) ||
144 be32_to_cpu(block
->bb_u
.s
.bb_leftsib
) < agflen
) &&
145 block
->bb_u
.s
.bb_leftsib
&&
146 (block
->bb_u
.s
.bb_rightsib
== cpu_to_be32(NULLAGBLOCK
) ||
147 be32_to_cpu(block
->bb_u
.s
.bb_rightsib
) < agflen
) &&
148 block
->bb_u
.s
.bb_rightsib
;
150 if (unlikely(XFS_TEST_ERROR(!sblock_ok
, mp
,
151 XFS_ERRTAG_BTREE_CHECK_SBLOCK
))) {
153 trace_xfs_btree_corrupt(bp
, _RET_IP_
);
154 XFS_ERROR_REPORT(__func__
, XFS_ERRLEVEL_LOW
, mp
);
155 return -EFSCORRUPTED
;
161 * Debug routine: check that block header is ok.
164 xfs_btree_check_block(
165 struct xfs_btree_cur
*cur
, /* btree cursor */
166 struct xfs_btree_block
*block
, /* generic btree block pointer */
167 int level
, /* level of the btree block */
168 struct xfs_buf
*bp
) /* buffer containing block, if any */
170 if (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
)
171 return xfs_btree_check_lblock(cur
, block
, level
, bp
);
173 return xfs_btree_check_sblock(cur
, block
, level
, bp
);
177 * Check that (long) pointer is ok.
179 int /* error (0 or EFSCORRUPTED) */
180 xfs_btree_check_lptr(
181 struct xfs_btree_cur
*cur
, /* btree cursor */
182 xfs_fsblock_t bno
, /* btree block disk address */
183 int level
) /* btree block level */
185 XFS_WANT_CORRUPTED_RETURN(cur
->bc_mp
,
187 bno
!= NULLFSBLOCK
&&
188 XFS_FSB_SANITY_CHECK(cur
->bc_mp
, bno
));
194 * Check that (short) pointer is ok.
196 STATIC
int /* error (0 or EFSCORRUPTED) */
197 xfs_btree_check_sptr(
198 struct xfs_btree_cur
*cur
, /* btree cursor */
199 xfs_agblock_t bno
, /* btree block disk address */
200 int level
) /* btree block level */
202 xfs_agblock_t agblocks
= cur
->bc_mp
->m_sb
.sb_agblocks
;
204 XFS_WANT_CORRUPTED_RETURN(cur
->bc_mp
,
206 bno
!= NULLAGBLOCK
&&
213 * Check that block ptr is ok.
215 STATIC
int /* error (0 or EFSCORRUPTED) */
217 struct xfs_btree_cur
*cur
, /* btree cursor */
218 union xfs_btree_ptr
*ptr
, /* btree block disk address */
219 int index
, /* offset from ptr to check */
220 int level
) /* btree block level */
222 if (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
) {
223 return xfs_btree_check_lptr(cur
,
224 be64_to_cpu((&ptr
->l
)[index
]), level
);
226 return xfs_btree_check_sptr(cur
,
227 be32_to_cpu((&ptr
->s
)[index
]), level
);
233 * Calculate CRC on the whole btree block and stuff it into the
234 * long-form btree header.
236 * Prior to calculting the CRC, pull the LSN out of the buffer log item and put
237 * it into the buffer so recovery knows what the last modification was that made
241 xfs_btree_lblock_calc_crc(
244 struct xfs_btree_block
*block
= XFS_BUF_TO_BLOCK(bp
);
245 struct xfs_buf_log_item
*bip
= bp
->b_fspriv
;
247 if (!xfs_sb_version_hascrc(&bp
->b_target
->bt_mount
->m_sb
))
250 block
->bb_u
.l
.bb_lsn
= cpu_to_be64(bip
->bli_item
.li_lsn
);
251 xfs_buf_update_cksum(bp
, XFS_BTREE_LBLOCK_CRC_OFF
);
255 xfs_btree_lblock_verify_crc(
258 struct xfs_btree_block
*block
= XFS_BUF_TO_BLOCK(bp
);
259 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
261 if (xfs_sb_version_hascrc(&mp
->m_sb
)) {
262 if (!xfs_log_check_lsn(mp
, be64_to_cpu(block
->bb_u
.l
.bb_lsn
)))
264 return xfs_buf_verify_cksum(bp
, XFS_BTREE_LBLOCK_CRC_OFF
);
271 * Calculate CRC on the whole btree block and stuff it into the
272 * short-form btree header.
274 * Prior to calculting the CRC, pull the LSN out of the buffer log item and put
275 * it into the buffer so recovery knows what the last modification was that made
279 xfs_btree_sblock_calc_crc(
282 struct xfs_btree_block
*block
= XFS_BUF_TO_BLOCK(bp
);
283 struct xfs_buf_log_item
*bip
= bp
->b_fspriv
;
285 if (!xfs_sb_version_hascrc(&bp
->b_target
->bt_mount
->m_sb
))
288 block
->bb_u
.s
.bb_lsn
= cpu_to_be64(bip
->bli_item
.li_lsn
);
289 xfs_buf_update_cksum(bp
, XFS_BTREE_SBLOCK_CRC_OFF
);
293 xfs_btree_sblock_verify_crc(
296 struct xfs_btree_block
*block
= XFS_BUF_TO_BLOCK(bp
);
297 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
299 if (xfs_sb_version_hascrc(&mp
->m_sb
)) {
300 if (!xfs_log_check_lsn(mp
, be64_to_cpu(block
->bb_u
.s
.bb_lsn
)))
302 return xfs_buf_verify_cksum(bp
, XFS_BTREE_SBLOCK_CRC_OFF
);
309 xfs_btree_free_block(
310 struct xfs_btree_cur
*cur
,
315 error
= cur
->bc_ops
->free_block(cur
, bp
);
317 xfs_trans_binval(cur
->bc_tp
, bp
);
318 XFS_BTREE_STATS_INC(cur
, free
);
324 * Delete the btree cursor.
327 xfs_btree_del_cursor(
328 xfs_btree_cur_t
*cur
, /* btree cursor */
329 int error
) /* del because of error */
331 int i
; /* btree level */
334 * Clear the buffer pointers, and release the buffers.
335 * If we're doing this in the face of an error, we
336 * need to make sure to inspect all of the entries
337 * in the bc_bufs array for buffers to be unlocked.
338 * This is because some of the btree code works from
339 * level n down to 0, and if we get an error along
340 * the way we won't have initialized all the entries
343 for (i
= 0; i
< cur
->bc_nlevels
; i
++) {
345 xfs_trans_brelse(cur
->bc_tp
, cur
->bc_bufs
[i
]);
350 * Can't free a bmap cursor without having dealt with the
351 * allocated indirect blocks' accounting.
353 ASSERT(cur
->bc_btnum
!= XFS_BTNUM_BMAP
||
354 cur
->bc_private
.b
.allocated
== 0);
358 kmem_zone_free(xfs_btree_cur_zone
, cur
);
362 * Duplicate the btree cursor.
363 * Allocate a new one, copy the record, re-get the buffers.
366 xfs_btree_dup_cursor(
367 xfs_btree_cur_t
*cur
, /* input cursor */
368 xfs_btree_cur_t
**ncur
) /* output cursor */
370 xfs_buf_t
*bp
; /* btree block's buffer pointer */
371 int error
; /* error return value */
372 int i
; /* level number of btree block */
373 xfs_mount_t
*mp
; /* mount structure for filesystem */
374 xfs_btree_cur_t
*new; /* new cursor value */
375 xfs_trans_t
*tp
; /* transaction pointer, can be NULL */
381 * Allocate a new cursor like the old one.
383 new = cur
->bc_ops
->dup_cursor(cur
);
386 * Copy the record currently in the cursor.
388 new->bc_rec
= cur
->bc_rec
;
391 * For each level current, re-get the buffer and copy the ptr value.
393 for (i
= 0; i
< new->bc_nlevels
; i
++) {
394 new->bc_ptrs
[i
] = cur
->bc_ptrs
[i
];
395 new->bc_ra
[i
] = cur
->bc_ra
[i
];
396 bp
= cur
->bc_bufs
[i
];
398 error
= xfs_trans_read_buf(mp
, tp
, mp
->m_ddev_targp
,
399 XFS_BUF_ADDR(bp
), mp
->m_bsize
,
401 cur
->bc_ops
->buf_ops
);
403 xfs_btree_del_cursor(new, error
);
408 new->bc_bufs
[i
] = bp
;
415 * XFS btree block layout and addressing:
417 * There are two types of blocks in the btree: leaf and non-leaf blocks.
419 * The leaf record start with a header then followed by records containing
420 * the values. A non-leaf block also starts with the same header, and
421 * then first contains lookup keys followed by an equal number of pointers
422 * to the btree blocks at the previous level.
424 * +--------+-------+-------+-------+-------+-------+-------+
425 * Leaf: | header | rec 1 | rec 2 | rec 3 | rec 4 | rec 5 | rec N |
426 * +--------+-------+-------+-------+-------+-------+-------+
428 * +--------+-------+-------+-------+-------+-------+-------+
429 * Non-Leaf: | header | key 1 | key 2 | key N | ptr 1 | ptr 2 | ptr N |
430 * +--------+-------+-------+-------+-------+-------+-------+
432 * The header is called struct xfs_btree_block for reasons better left unknown
433 * and comes in different versions for short (32bit) and long (64bit) block
434 * pointers. The record and key structures are defined by the btree instances
435 * and opaque to the btree core. The block pointers are simple disk endian
436 * integers, available in a short (32bit) and long (64bit) variant.
438 * The helpers below calculate the offset of a given record, key or pointer
439 * into a btree block (xfs_btree_*_offset) or return a pointer to the given
440 * record, key or pointer (xfs_btree_*_addr). Note that all addressing
441 * inside the btree block is done using indices starting at one, not zero!
443 * If XFS_BTREE_OVERLAPPING is set, then this btree supports keys containing
444 * overlapping intervals. In such a tree, records are still sorted lowest to
445 * highest and indexed by the smallest key value that refers to the record.
446 * However, nodes are different: each pointer has two associated keys -- one
447 * indexing the lowest key available in the block(s) below (the same behavior
448 * as the key in a regular btree) and another indexing the highest key
449 * available in the block(s) below. Because records are /not/ sorted by the
450 * highest key, all leaf block updates require us to compute the highest key
451 * that matches any record in the leaf and to recursively update the high keys
452 * in the nodes going further up in the tree, if necessary. Nodes look like
455 * +--------+-----+-----+-----+-----+-----+-------+-------+-----+
456 * Non-Leaf: | header | lo1 | hi1 | lo2 | hi2 | ... | ptr 1 | ptr 2 | ... |
457 * +--------+-----+-----+-----+-----+-----+-------+-------+-----+
459 * To perform an interval query on an overlapped tree, perform the usual
460 * depth-first search and use the low and high keys to decide if we can skip
461 * that particular node. If a leaf node is reached, return the records that
462 * intersect the interval. Note that an interval query may return numerous
463 * entries. For a non-overlapped tree, simply search for the record associated
464 * with the lowest key and iterate forward until a non-matching record is
465 * found. Section 14.3 ("Interval Trees") of _Introduction to Algorithms_ by
466 * Cormen, Leiserson, Rivest, and Stein (2nd or 3rd ed. only) discuss this in
469 * Why do we care about overlapping intervals? Let's say you have a bunch of
470 * reverse mapping records on a reflink filesystem:
472 * 1: +- file A startblock B offset C length D -----------+
473 * 2: +- file E startblock F offset G length H --------------+
474 * 3: +- file I startblock F offset J length K --+
475 * 4: +- file L... --+
477 * Now say we want to map block (B+D) into file A at offset (C+D). Ideally,
478 * we'd simply increment the length of record 1. But how do we find the record
479 * that ends at (B+D-1) (i.e. record 1)? A LE lookup of (B+D-1) would return
480 * record 3 because the keys are ordered first by startblock. An interval
481 * query would return records 1 and 2 because they both overlap (B+D-1), and
482 * from that we can pick out record 1 as the appropriate left neighbor.
484 * In the non-overlapped case you can do a LE lookup and decrement the cursor
485 * because a record's interval must end before the next record.
489 * Return size of the btree block header for this btree instance.
491 static inline size_t xfs_btree_block_len(struct xfs_btree_cur
*cur
)
493 if (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
) {
494 if (cur
->bc_flags
& XFS_BTREE_CRC_BLOCKS
)
495 return XFS_BTREE_LBLOCK_CRC_LEN
;
496 return XFS_BTREE_LBLOCK_LEN
;
498 if (cur
->bc_flags
& XFS_BTREE_CRC_BLOCKS
)
499 return XFS_BTREE_SBLOCK_CRC_LEN
;
500 return XFS_BTREE_SBLOCK_LEN
;
504 * Return size of btree block pointers for this btree instance.
506 static inline size_t xfs_btree_ptr_len(struct xfs_btree_cur
*cur
)
508 return (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
) ?
509 sizeof(__be64
) : sizeof(__be32
);
513 * Calculate offset of the n-th record in a btree block.
516 xfs_btree_rec_offset(
517 struct xfs_btree_cur
*cur
,
520 return xfs_btree_block_len(cur
) +
521 (n
- 1) * cur
->bc_ops
->rec_len
;
525 * Calculate offset of the n-th key in a btree block.
528 xfs_btree_key_offset(
529 struct xfs_btree_cur
*cur
,
532 return xfs_btree_block_len(cur
) +
533 (n
- 1) * cur
->bc_ops
->key_len
;
537 * Calculate offset of the n-th high key in a btree block.
540 xfs_btree_high_key_offset(
541 struct xfs_btree_cur
*cur
,
544 return xfs_btree_block_len(cur
) +
545 (n
- 1) * cur
->bc_ops
->key_len
+ (cur
->bc_ops
->key_len
/ 2);
549 * Calculate offset of the n-th block pointer in a btree block.
552 xfs_btree_ptr_offset(
553 struct xfs_btree_cur
*cur
,
557 return xfs_btree_block_len(cur
) +
558 cur
->bc_ops
->get_maxrecs(cur
, level
) * cur
->bc_ops
->key_len
+
559 (n
- 1) * xfs_btree_ptr_len(cur
);
563 * Return a pointer to the n-th record in the btree block.
565 union xfs_btree_rec
*
567 struct xfs_btree_cur
*cur
,
569 struct xfs_btree_block
*block
)
571 return (union xfs_btree_rec
*)
572 ((char *)block
+ xfs_btree_rec_offset(cur
, n
));
576 * Return a pointer to the n-th key in the btree block.
578 union xfs_btree_key
*
580 struct xfs_btree_cur
*cur
,
582 struct xfs_btree_block
*block
)
584 return (union xfs_btree_key
*)
585 ((char *)block
+ xfs_btree_key_offset(cur
, n
));
589 * Return a pointer to the n-th high key in the btree block.
591 union xfs_btree_key
*
592 xfs_btree_high_key_addr(
593 struct xfs_btree_cur
*cur
,
595 struct xfs_btree_block
*block
)
597 return (union xfs_btree_key
*)
598 ((char *)block
+ xfs_btree_high_key_offset(cur
, n
));
602 * Return a pointer to the n-th block pointer in the btree block.
604 union xfs_btree_ptr
*
606 struct xfs_btree_cur
*cur
,
608 struct xfs_btree_block
*block
)
610 int level
= xfs_btree_get_level(block
);
612 ASSERT(block
->bb_level
!= 0);
614 return (union xfs_btree_ptr
*)
615 ((char *)block
+ xfs_btree_ptr_offset(cur
, n
, level
));
619 * Get the root block which is stored in the inode.
621 * For now this btree implementation assumes the btree root is always
622 * stored in the if_broot field of an inode fork.
624 STATIC
struct xfs_btree_block
*
626 struct xfs_btree_cur
*cur
)
628 struct xfs_ifork
*ifp
;
630 ifp
= XFS_IFORK_PTR(cur
->bc_private
.b
.ip
, cur
->bc_private
.b
.whichfork
);
631 return (struct xfs_btree_block
*)ifp
->if_broot
;
635 * Retrieve the block pointer from the cursor at the given level.
636 * This may be an inode btree root or from a buffer.
638 struct xfs_btree_block
* /* generic btree block pointer */
640 struct xfs_btree_cur
*cur
, /* btree cursor */
641 int level
, /* level in btree */
642 struct xfs_buf
**bpp
) /* buffer containing the block */
644 if ((cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
) &&
645 (level
== cur
->bc_nlevels
- 1)) {
647 return xfs_btree_get_iroot(cur
);
650 *bpp
= cur
->bc_bufs
[level
];
651 return XFS_BUF_TO_BLOCK(*bpp
);
655 * Get a buffer for the block, return it with no data read.
656 * Long-form addressing.
658 xfs_buf_t
* /* buffer for fsbno */
660 xfs_mount_t
*mp
, /* file system mount point */
661 xfs_trans_t
*tp
, /* transaction pointer */
662 xfs_fsblock_t fsbno
, /* file system block number */
663 uint lock
) /* lock flags for get_buf */
665 xfs_daddr_t d
; /* real disk block address */
667 ASSERT(fsbno
!= NULLFSBLOCK
);
668 d
= XFS_FSB_TO_DADDR(mp
, fsbno
);
669 return xfs_trans_get_buf(tp
, mp
->m_ddev_targp
, d
, mp
->m_bsize
, lock
);
673 * Get a buffer for the block, return it with no data read.
674 * Short-form addressing.
676 xfs_buf_t
* /* buffer for agno/agbno */
678 xfs_mount_t
*mp
, /* file system mount point */
679 xfs_trans_t
*tp
, /* transaction pointer */
680 xfs_agnumber_t agno
, /* allocation group number */
681 xfs_agblock_t agbno
, /* allocation group block number */
682 uint lock
) /* lock flags for get_buf */
684 xfs_daddr_t d
; /* real disk block address */
686 ASSERT(agno
!= NULLAGNUMBER
);
687 ASSERT(agbno
!= NULLAGBLOCK
);
688 d
= XFS_AGB_TO_DADDR(mp
, agno
, agbno
);
689 return xfs_trans_get_buf(tp
, mp
->m_ddev_targp
, d
, mp
->m_bsize
, lock
);
693 * Check for the cursor referring to the last block at the given level.
695 int /* 1=is last block, 0=not last block */
696 xfs_btree_islastblock(
697 xfs_btree_cur_t
*cur
, /* btree cursor */
698 int level
) /* level to check */
700 struct xfs_btree_block
*block
; /* generic btree block pointer */
701 xfs_buf_t
*bp
; /* buffer containing block */
703 block
= xfs_btree_get_block(cur
, level
, &bp
);
704 xfs_btree_check_block(cur
, block
, level
, bp
);
705 if (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
)
706 return block
->bb_u
.l
.bb_rightsib
== cpu_to_be64(NULLFSBLOCK
);
708 return block
->bb_u
.s
.bb_rightsib
== cpu_to_be32(NULLAGBLOCK
);
712 * Change the cursor to point to the first record at the given level.
713 * Other levels are unaffected.
715 STATIC
int /* success=1, failure=0 */
717 xfs_btree_cur_t
*cur
, /* btree cursor */
718 int level
) /* level to change */
720 struct xfs_btree_block
*block
; /* generic btree block pointer */
721 xfs_buf_t
*bp
; /* buffer containing block */
724 * Get the block pointer for this level.
726 block
= xfs_btree_get_block(cur
, level
, &bp
);
727 xfs_btree_check_block(cur
, block
, level
, bp
);
729 * It's empty, there is no such record.
731 if (!block
->bb_numrecs
)
734 * Set the ptr value to 1, that's the first record/key.
736 cur
->bc_ptrs
[level
] = 1;
741 * Change the cursor to point to the last record in the current block
742 * at the given level. Other levels are unaffected.
744 STATIC
int /* success=1, failure=0 */
746 xfs_btree_cur_t
*cur
, /* btree cursor */
747 int level
) /* level to change */
749 struct xfs_btree_block
*block
; /* generic btree block pointer */
750 xfs_buf_t
*bp
; /* buffer containing block */
753 * Get the block pointer for this level.
755 block
= xfs_btree_get_block(cur
, level
, &bp
);
756 xfs_btree_check_block(cur
, block
, level
, bp
);
758 * It's empty, there is no such record.
760 if (!block
->bb_numrecs
)
763 * Set the ptr value to numrecs, that's the last record/key.
765 cur
->bc_ptrs
[level
] = be16_to_cpu(block
->bb_numrecs
);
770 * Compute first and last byte offsets for the fields given.
771 * Interprets the offsets table, which contains struct field offsets.
775 int64_t fields
, /* bitmask of fields */
776 const short *offsets
, /* table of field offsets */
777 int nbits
, /* number of bits to inspect */
778 int *first
, /* output: first byte offset */
779 int *last
) /* output: last byte offset */
781 int i
; /* current bit number */
782 int64_t imask
; /* mask for current bit number */
786 * Find the lowest bit, so the first byte offset.
788 for (i
= 0, imask
= 1LL; ; i
++, imask
<<= 1) {
789 if (imask
& fields
) {
795 * Find the highest bit, so the last byte offset.
797 for (i
= nbits
- 1, imask
= 1LL << i
; ; i
--, imask
>>= 1) {
798 if (imask
& fields
) {
799 *last
= offsets
[i
+ 1] - 1;
806 * Get a buffer for the block, return it read in.
807 * Long-form addressing.
811 struct xfs_mount
*mp
, /* file system mount point */
812 struct xfs_trans
*tp
, /* transaction pointer */
813 xfs_fsblock_t fsbno
, /* file system block number */
814 uint lock
, /* lock flags for read_buf */
815 struct xfs_buf
**bpp
, /* buffer for fsbno */
816 int refval
, /* ref count value for buffer */
817 const struct xfs_buf_ops
*ops
)
819 struct xfs_buf
*bp
; /* return value */
820 xfs_daddr_t d
; /* real disk block address */
823 if (!XFS_FSB_SANITY_CHECK(mp
, fsbno
))
824 return -EFSCORRUPTED
;
825 d
= XFS_FSB_TO_DADDR(mp
, fsbno
);
826 error
= xfs_trans_read_buf(mp
, tp
, mp
->m_ddev_targp
, d
,
827 mp
->m_bsize
, lock
, &bp
, ops
);
831 xfs_buf_set_ref(bp
, refval
);
837 * Read-ahead the block, don't wait for it, don't return a buffer.
838 * Long-form addressing.
842 xfs_btree_reada_bufl(
843 struct xfs_mount
*mp
, /* file system mount point */
844 xfs_fsblock_t fsbno
, /* file system block number */
845 xfs_extlen_t count
, /* count of filesystem blocks */
846 const struct xfs_buf_ops
*ops
)
850 ASSERT(fsbno
!= NULLFSBLOCK
);
851 d
= XFS_FSB_TO_DADDR(mp
, fsbno
);
852 xfs_buf_readahead(mp
->m_ddev_targp
, d
, mp
->m_bsize
* count
, ops
);
856 * Read-ahead the block, don't wait for it, don't return a buffer.
857 * Short-form addressing.
861 xfs_btree_reada_bufs(
862 struct xfs_mount
*mp
, /* file system mount point */
863 xfs_agnumber_t agno
, /* allocation group number */
864 xfs_agblock_t agbno
, /* allocation group block number */
865 xfs_extlen_t count
, /* count of filesystem blocks */
866 const struct xfs_buf_ops
*ops
)
870 ASSERT(agno
!= NULLAGNUMBER
);
871 ASSERT(agbno
!= NULLAGBLOCK
);
872 d
= XFS_AGB_TO_DADDR(mp
, agno
, agbno
);
873 xfs_buf_readahead(mp
->m_ddev_targp
, d
, mp
->m_bsize
* count
, ops
);
877 xfs_btree_readahead_lblock(
878 struct xfs_btree_cur
*cur
,
880 struct xfs_btree_block
*block
)
883 xfs_fsblock_t left
= be64_to_cpu(block
->bb_u
.l
.bb_leftsib
);
884 xfs_fsblock_t right
= be64_to_cpu(block
->bb_u
.l
.bb_rightsib
);
886 if ((lr
& XFS_BTCUR_LEFTRA
) && left
!= NULLFSBLOCK
) {
887 xfs_btree_reada_bufl(cur
->bc_mp
, left
, 1,
888 cur
->bc_ops
->buf_ops
);
892 if ((lr
& XFS_BTCUR_RIGHTRA
) && right
!= NULLFSBLOCK
) {
893 xfs_btree_reada_bufl(cur
->bc_mp
, right
, 1,
894 cur
->bc_ops
->buf_ops
);
902 xfs_btree_readahead_sblock(
903 struct xfs_btree_cur
*cur
,
905 struct xfs_btree_block
*block
)
908 xfs_agblock_t left
= be32_to_cpu(block
->bb_u
.s
.bb_leftsib
);
909 xfs_agblock_t right
= be32_to_cpu(block
->bb_u
.s
.bb_rightsib
);
912 if ((lr
& XFS_BTCUR_LEFTRA
) && left
!= NULLAGBLOCK
) {
913 xfs_btree_reada_bufs(cur
->bc_mp
, cur
->bc_private
.a
.agno
,
914 left
, 1, cur
->bc_ops
->buf_ops
);
918 if ((lr
& XFS_BTCUR_RIGHTRA
) && right
!= NULLAGBLOCK
) {
919 xfs_btree_reada_bufs(cur
->bc_mp
, cur
->bc_private
.a
.agno
,
920 right
, 1, cur
->bc_ops
->buf_ops
);
928 * Read-ahead btree blocks, at the given level.
929 * Bits in lr are set from XFS_BTCUR_{LEFT,RIGHT}RA.
933 struct xfs_btree_cur
*cur
, /* btree cursor */
934 int lev
, /* level in btree */
935 int lr
) /* left/right bits */
937 struct xfs_btree_block
*block
;
940 * No readahead needed if we are at the root level and the
941 * btree root is stored in the inode.
943 if ((cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
) &&
944 (lev
== cur
->bc_nlevels
- 1))
947 if ((cur
->bc_ra
[lev
] | lr
) == cur
->bc_ra
[lev
])
950 cur
->bc_ra
[lev
] |= lr
;
951 block
= XFS_BUF_TO_BLOCK(cur
->bc_bufs
[lev
]);
953 if (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
)
954 return xfs_btree_readahead_lblock(cur
, lr
, block
);
955 return xfs_btree_readahead_sblock(cur
, lr
, block
);
959 xfs_btree_ptr_to_daddr(
960 struct xfs_btree_cur
*cur
,
961 union xfs_btree_ptr
*ptr
)
963 if (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
) {
964 ASSERT(ptr
->l
!= cpu_to_be64(NULLFSBLOCK
));
966 return XFS_FSB_TO_DADDR(cur
->bc_mp
, be64_to_cpu(ptr
->l
));
968 ASSERT(cur
->bc_private
.a
.agno
!= NULLAGNUMBER
);
969 ASSERT(ptr
->s
!= cpu_to_be32(NULLAGBLOCK
));
971 return XFS_AGB_TO_DADDR(cur
->bc_mp
, cur
->bc_private
.a
.agno
,
972 be32_to_cpu(ptr
->s
));
977 * Readahead @count btree blocks at the given @ptr location.
979 * We don't need to care about long or short form btrees here as we have a
980 * method of converting the ptr directly to a daddr available to us.
983 xfs_btree_readahead_ptr(
984 struct xfs_btree_cur
*cur
,
985 union xfs_btree_ptr
*ptr
,
988 xfs_buf_readahead(cur
->bc_mp
->m_ddev_targp
,
989 xfs_btree_ptr_to_daddr(cur
, ptr
),
990 cur
->bc_mp
->m_bsize
* count
, cur
->bc_ops
->buf_ops
);
994 * Set the buffer for level "lev" in the cursor to bp, releasing
995 * any previous buffer.
999 xfs_btree_cur_t
*cur
, /* btree cursor */
1000 int lev
, /* level in btree */
1001 xfs_buf_t
*bp
) /* new buffer to set */
1003 struct xfs_btree_block
*b
; /* btree block */
1005 if (cur
->bc_bufs
[lev
])
1006 xfs_trans_brelse(cur
->bc_tp
, cur
->bc_bufs
[lev
]);
1007 cur
->bc_bufs
[lev
] = bp
;
1008 cur
->bc_ra
[lev
] = 0;
1010 b
= XFS_BUF_TO_BLOCK(bp
);
1011 if (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
) {
1012 if (b
->bb_u
.l
.bb_leftsib
== cpu_to_be64(NULLFSBLOCK
))
1013 cur
->bc_ra
[lev
] |= XFS_BTCUR_LEFTRA
;
1014 if (b
->bb_u
.l
.bb_rightsib
== cpu_to_be64(NULLFSBLOCK
))
1015 cur
->bc_ra
[lev
] |= XFS_BTCUR_RIGHTRA
;
1017 if (b
->bb_u
.s
.bb_leftsib
== cpu_to_be32(NULLAGBLOCK
))
1018 cur
->bc_ra
[lev
] |= XFS_BTCUR_LEFTRA
;
1019 if (b
->bb_u
.s
.bb_rightsib
== cpu_to_be32(NULLAGBLOCK
))
1020 cur
->bc_ra
[lev
] |= XFS_BTCUR_RIGHTRA
;
1025 xfs_btree_ptr_is_null(
1026 struct xfs_btree_cur
*cur
,
1027 union xfs_btree_ptr
*ptr
)
1029 if (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
)
1030 return ptr
->l
== cpu_to_be64(NULLFSBLOCK
);
1032 return ptr
->s
== cpu_to_be32(NULLAGBLOCK
);
1036 xfs_btree_set_ptr_null(
1037 struct xfs_btree_cur
*cur
,
1038 union xfs_btree_ptr
*ptr
)
1040 if (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
)
1041 ptr
->l
= cpu_to_be64(NULLFSBLOCK
);
1043 ptr
->s
= cpu_to_be32(NULLAGBLOCK
);
1047 * Get/set/init sibling pointers
1050 xfs_btree_get_sibling(
1051 struct xfs_btree_cur
*cur
,
1052 struct xfs_btree_block
*block
,
1053 union xfs_btree_ptr
*ptr
,
1056 ASSERT(lr
== XFS_BB_LEFTSIB
|| lr
== XFS_BB_RIGHTSIB
);
1058 if (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
) {
1059 if (lr
== XFS_BB_RIGHTSIB
)
1060 ptr
->l
= block
->bb_u
.l
.bb_rightsib
;
1062 ptr
->l
= block
->bb_u
.l
.bb_leftsib
;
1064 if (lr
== XFS_BB_RIGHTSIB
)
1065 ptr
->s
= block
->bb_u
.s
.bb_rightsib
;
1067 ptr
->s
= block
->bb_u
.s
.bb_leftsib
;
1072 xfs_btree_set_sibling(
1073 struct xfs_btree_cur
*cur
,
1074 struct xfs_btree_block
*block
,
1075 union xfs_btree_ptr
*ptr
,
1078 ASSERT(lr
== XFS_BB_LEFTSIB
|| lr
== XFS_BB_RIGHTSIB
);
1080 if (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
) {
1081 if (lr
== XFS_BB_RIGHTSIB
)
1082 block
->bb_u
.l
.bb_rightsib
= ptr
->l
;
1084 block
->bb_u
.l
.bb_leftsib
= ptr
->l
;
1086 if (lr
== XFS_BB_RIGHTSIB
)
1087 block
->bb_u
.s
.bb_rightsib
= ptr
->s
;
1089 block
->bb_u
.s
.bb_leftsib
= ptr
->s
;
1094 xfs_btree_init_block_int(
1095 struct xfs_mount
*mp
,
1096 struct xfs_btree_block
*buf
,
1104 int crc
= xfs_sb_version_hascrc(&mp
->m_sb
);
1105 __u32 magic
= xfs_btree_magic(crc
, btnum
);
1107 buf
->bb_magic
= cpu_to_be32(magic
);
1108 buf
->bb_level
= cpu_to_be16(level
);
1109 buf
->bb_numrecs
= cpu_to_be16(numrecs
);
1111 if (flags
& XFS_BTREE_LONG_PTRS
) {
1112 buf
->bb_u
.l
.bb_leftsib
= cpu_to_be64(NULLFSBLOCK
);
1113 buf
->bb_u
.l
.bb_rightsib
= cpu_to_be64(NULLFSBLOCK
);
1115 buf
->bb_u
.l
.bb_blkno
= cpu_to_be64(blkno
);
1116 buf
->bb_u
.l
.bb_owner
= cpu_to_be64(owner
);
1117 uuid_copy(&buf
->bb_u
.l
.bb_uuid
, &mp
->m_sb
.sb_meta_uuid
);
1118 buf
->bb_u
.l
.bb_pad
= 0;
1119 buf
->bb_u
.l
.bb_lsn
= 0;
1122 /* owner is a 32 bit value on short blocks */
1123 __u32 __owner
= (__u32
)owner
;
1125 buf
->bb_u
.s
.bb_leftsib
= cpu_to_be32(NULLAGBLOCK
);
1126 buf
->bb_u
.s
.bb_rightsib
= cpu_to_be32(NULLAGBLOCK
);
1128 buf
->bb_u
.s
.bb_blkno
= cpu_to_be64(blkno
);
1129 buf
->bb_u
.s
.bb_owner
= cpu_to_be32(__owner
);
1130 uuid_copy(&buf
->bb_u
.s
.bb_uuid
, &mp
->m_sb
.sb_meta_uuid
);
1131 buf
->bb_u
.s
.bb_lsn
= 0;
1137 xfs_btree_init_block(
1138 struct xfs_mount
*mp
,
1146 xfs_btree_init_block_int(mp
, XFS_BUF_TO_BLOCK(bp
), bp
->b_bn
,
1147 btnum
, level
, numrecs
, owner
, flags
);
1151 xfs_btree_init_block_cur(
1152 struct xfs_btree_cur
*cur
,
1160 * we can pull the owner from the cursor right now as the different
1161 * owners align directly with the pointer size of the btree. This may
1162 * change in future, but is safe for current users of the generic btree
1165 if (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
)
1166 owner
= cur
->bc_private
.b
.ip
->i_ino
;
1168 owner
= cur
->bc_private
.a
.agno
;
1170 xfs_btree_init_block_int(cur
->bc_mp
, XFS_BUF_TO_BLOCK(bp
), bp
->b_bn
,
1171 cur
->bc_btnum
, level
, numrecs
,
1172 owner
, cur
->bc_flags
);
1176 * Return true if ptr is the last record in the btree and
1177 * we need to track updates to this record. The decision
1178 * will be further refined in the update_lastrec method.
1181 xfs_btree_is_lastrec(
1182 struct xfs_btree_cur
*cur
,
1183 struct xfs_btree_block
*block
,
1186 union xfs_btree_ptr ptr
;
1190 if (!(cur
->bc_flags
& XFS_BTREE_LASTREC_UPDATE
))
1193 xfs_btree_get_sibling(cur
, block
, &ptr
, XFS_BB_RIGHTSIB
);
1194 if (!xfs_btree_ptr_is_null(cur
, &ptr
))
1200 xfs_btree_buf_to_ptr(
1201 struct xfs_btree_cur
*cur
,
1203 union xfs_btree_ptr
*ptr
)
1205 if (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
)
1206 ptr
->l
= cpu_to_be64(XFS_DADDR_TO_FSB(cur
->bc_mp
,
1209 ptr
->s
= cpu_to_be32(xfs_daddr_to_agbno(cur
->bc_mp
,
1216 struct xfs_btree_cur
*cur
,
1219 switch (cur
->bc_btnum
) {
1222 xfs_buf_set_ref(bp
, XFS_ALLOC_BTREE_REF
);
1225 case XFS_BTNUM_FINO
:
1226 xfs_buf_set_ref(bp
, XFS_INO_BTREE_REF
);
1228 case XFS_BTNUM_BMAP
:
1229 xfs_buf_set_ref(bp
, XFS_BMAP_BTREE_REF
);
1231 case XFS_BTNUM_RMAP
:
1232 xfs_buf_set_ref(bp
, XFS_RMAP_BTREE_REF
);
1234 case XFS_BTNUM_REFC
:
1235 xfs_buf_set_ref(bp
, XFS_REFC_BTREE_REF
);
1243 xfs_btree_get_buf_block(
1244 struct xfs_btree_cur
*cur
,
1245 union xfs_btree_ptr
*ptr
,
1247 struct xfs_btree_block
**block
,
1248 struct xfs_buf
**bpp
)
1250 struct xfs_mount
*mp
= cur
->bc_mp
;
1253 /* need to sort out how callers deal with failures first */
1254 ASSERT(!(flags
& XBF_TRYLOCK
));
1256 d
= xfs_btree_ptr_to_daddr(cur
, ptr
);
1257 *bpp
= xfs_trans_get_buf(cur
->bc_tp
, mp
->m_ddev_targp
, d
,
1258 mp
->m_bsize
, flags
);
1263 (*bpp
)->b_ops
= cur
->bc_ops
->buf_ops
;
1264 *block
= XFS_BUF_TO_BLOCK(*bpp
);
1269 * Read in the buffer at the given ptr and return the buffer and
1270 * the block pointer within the buffer.
1273 xfs_btree_read_buf_block(
1274 struct xfs_btree_cur
*cur
,
1275 union xfs_btree_ptr
*ptr
,
1277 struct xfs_btree_block
**block
,
1278 struct xfs_buf
**bpp
)
1280 struct xfs_mount
*mp
= cur
->bc_mp
;
1284 /* need to sort out how callers deal with failures first */
1285 ASSERT(!(flags
& XBF_TRYLOCK
));
1287 d
= xfs_btree_ptr_to_daddr(cur
, ptr
);
1288 error
= xfs_trans_read_buf(mp
, cur
->bc_tp
, mp
->m_ddev_targp
, d
,
1289 mp
->m_bsize
, flags
, bpp
,
1290 cur
->bc_ops
->buf_ops
);
1294 xfs_btree_set_refs(cur
, *bpp
);
1295 *block
= XFS_BUF_TO_BLOCK(*bpp
);
1300 * Copy keys from one btree block to another.
1303 xfs_btree_copy_keys(
1304 struct xfs_btree_cur
*cur
,
1305 union xfs_btree_key
*dst_key
,
1306 union xfs_btree_key
*src_key
,
1309 ASSERT(numkeys
>= 0);
1310 memcpy(dst_key
, src_key
, numkeys
* cur
->bc_ops
->key_len
);
1314 * Copy records from one btree block to another.
1317 xfs_btree_copy_recs(
1318 struct xfs_btree_cur
*cur
,
1319 union xfs_btree_rec
*dst_rec
,
1320 union xfs_btree_rec
*src_rec
,
1323 ASSERT(numrecs
>= 0);
1324 memcpy(dst_rec
, src_rec
, numrecs
* cur
->bc_ops
->rec_len
);
1328 * Copy block pointers from one btree block to another.
1331 xfs_btree_copy_ptrs(
1332 struct xfs_btree_cur
*cur
,
1333 union xfs_btree_ptr
*dst_ptr
,
1334 union xfs_btree_ptr
*src_ptr
,
1337 ASSERT(numptrs
>= 0);
1338 memcpy(dst_ptr
, src_ptr
, numptrs
* xfs_btree_ptr_len(cur
));
1342 * Shift keys one index left/right inside a single btree block.
1345 xfs_btree_shift_keys(
1346 struct xfs_btree_cur
*cur
,
1347 union xfs_btree_key
*key
,
1353 ASSERT(numkeys
>= 0);
1354 ASSERT(dir
== 1 || dir
== -1);
1356 dst_key
= (char *)key
+ (dir
* cur
->bc_ops
->key_len
);
1357 memmove(dst_key
, key
, numkeys
* cur
->bc_ops
->key_len
);
1361 * Shift records one index left/right inside a single btree block.
1364 xfs_btree_shift_recs(
1365 struct xfs_btree_cur
*cur
,
1366 union xfs_btree_rec
*rec
,
1372 ASSERT(numrecs
>= 0);
1373 ASSERT(dir
== 1 || dir
== -1);
1375 dst_rec
= (char *)rec
+ (dir
* cur
->bc_ops
->rec_len
);
1376 memmove(dst_rec
, rec
, numrecs
* cur
->bc_ops
->rec_len
);
1380 * Shift block pointers one index left/right inside a single btree block.
1383 xfs_btree_shift_ptrs(
1384 struct xfs_btree_cur
*cur
,
1385 union xfs_btree_ptr
*ptr
,
1391 ASSERT(numptrs
>= 0);
1392 ASSERT(dir
== 1 || dir
== -1);
1394 dst_ptr
= (char *)ptr
+ (dir
* xfs_btree_ptr_len(cur
));
1395 memmove(dst_ptr
, ptr
, numptrs
* xfs_btree_ptr_len(cur
));
1399 * Log key values from the btree block.
1403 struct xfs_btree_cur
*cur
,
1408 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
1409 XFS_BTREE_TRACE_ARGBII(cur
, bp
, first
, last
);
1412 xfs_trans_buf_set_type(cur
->bc_tp
, bp
, XFS_BLFT_BTREE_BUF
);
1413 xfs_trans_log_buf(cur
->bc_tp
, bp
,
1414 xfs_btree_key_offset(cur
, first
),
1415 xfs_btree_key_offset(cur
, last
+ 1) - 1);
1417 xfs_trans_log_inode(cur
->bc_tp
, cur
->bc_private
.b
.ip
,
1418 xfs_ilog_fbroot(cur
->bc_private
.b
.whichfork
));
1421 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
1425 * Log record values from the btree block.
1429 struct xfs_btree_cur
*cur
,
1434 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
1435 XFS_BTREE_TRACE_ARGBII(cur
, bp
, first
, last
);
1437 xfs_trans_buf_set_type(cur
->bc_tp
, bp
, XFS_BLFT_BTREE_BUF
);
1438 xfs_trans_log_buf(cur
->bc_tp
, bp
,
1439 xfs_btree_rec_offset(cur
, first
),
1440 xfs_btree_rec_offset(cur
, last
+ 1) - 1);
1442 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
1446 * Log block pointer fields from a btree block (nonleaf).
1450 struct xfs_btree_cur
*cur
, /* btree cursor */
1451 struct xfs_buf
*bp
, /* buffer containing btree block */
1452 int first
, /* index of first pointer to log */
1453 int last
) /* index of last pointer to log */
1455 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
1456 XFS_BTREE_TRACE_ARGBII(cur
, bp
, first
, last
);
1459 struct xfs_btree_block
*block
= XFS_BUF_TO_BLOCK(bp
);
1460 int level
= xfs_btree_get_level(block
);
1462 xfs_trans_buf_set_type(cur
->bc_tp
, bp
, XFS_BLFT_BTREE_BUF
);
1463 xfs_trans_log_buf(cur
->bc_tp
, bp
,
1464 xfs_btree_ptr_offset(cur
, first
, level
),
1465 xfs_btree_ptr_offset(cur
, last
+ 1, level
) - 1);
1467 xfs_trans_log_inode(cur
->bc_tp
, cur
->bc_private
.b
.ip
,
1468 xfs_ilog_fbroot(cur
->bc_private
.b
.whichfork
));
1471 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
1475 * Log fields from a btree block header.
1478 xfs_btree_log_block(
1479 struct xfs_btree_cur
*cur
, /* btree cursor */
1480 struct xfs_buf
*bp
, /* buffer containing btree block */
1481 int fields
) /* mask of fields: XFS_BB_... */
1483 int first
; /* first byte offset logged */
1484 int last
; /* last byte offset logged */
1485 static const short soffsets
[] = { /* table of offsets (short) */
1486 offsetof(struct xfs_btree_block
, bb_magic
),
1487 offsetof(struct xfs_btree_block
, bb_level
),
1488 offsetof(struct xfs_btree_block
, bb_numrecs
),
1489 offsetof(struct xfs_btree_block
, bb_u
.s
.bb_leftsib
),
1490 offsetof(struct xfs_btree_block
, bb_u
.s
.bb_rightsib
),
1491 offsetof(struct xfs_btree_block
, bb_u
.s
.bb_blkno
),
1492 offsetof(struct xfs_btree_block
, bb_u
.s
.bb_lsn
),
1493 offsetof(struct xfs_btree_block
, bb_u
.s
.bb_uuid
),
1494 offsetof(struct xfs_btree_block
, bb_u
.s
.bb_owner
),
1495 offsetof(struct xfs_btree_block
, bb_u
.s
.bb_crc
),
1496 XFS_BTREE_SBLOCK_CRC_LEN
1498 static const short loffsets
[] = { /* table of offsets (long) */
1499 offsetof(struct xfs_btree_block
, bb_magic
),
1500 offsetof(struct xfs_btree_block
, bb_level
),
1501 offsetof(struct xfs_btree_block
, bb_numrecs
),
1502 offsetof(struct xfs_btree_block
, bb_u
.l
.bb_leftsib
),
1503 offsetof(struct xfs_btree_block
, bb_u
.l
.bb_rightsib
),
1504 offsetof(struct xfs_btree_block
, bb_u
.l
.bb_blkno
),
1505 offsetof(struct xfs_btree_block
, bb_u
.l
.bb_lsn
),
1506 offsetof(struct xfs_btree_block
, bb_u
.l
.bb_uuid
),
1507 offsetof(struct xfs_btree_block
, bb_u
.l
.bb_owner
),
1508 offsetof(struct xfs_btree_block
, bb_u
.l
.bb_crc
),
1509 offsetof(struct xfs_btree_block
, bb_u
.l
.bb_pad
),
1510 XFS_BTREE_LBLOCK_CRC_LEN
1513 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
1514 XFS_BTREE_TRACE_ARGBI(cur
, bp
, fields
);
1519 if (cur
->bc_flags
& XFS_BTREE_CRC_BLOCKS
) {
1521 * We don't log the CRC when updating a btree
1522 * block but instead recreate it during log
1523 * recovery. As the log buffers have checksums
1524 * of their own this is safe and avoids logging a crc
1525 * update in a lot of places.
1527 if (fields
== XFS_BB_ALL_BITS
)
1528 fields
= XFS_BB_ALL_BITS_CRC
;
1529 nbits
= XFS_BB_NUM_BITS_CRC
;
1531 nbits
= XFS_BB_NUM_BITS
;
1533 xfs_btree_offsets(fields
,
1534 (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
) ?
1535 loffsets
: soffsets
,
1536 nbits
, &first
, &last
);
1537 xfs_trans_buf_set_type(cur
->bc_tp
, bp
, XFS_BLFT_BTREE_BUF
);
1538 xfs_trans_log_buf(cur
->bc_tp
, bp
, first
, last
);
1540 xfs_trans_log_inode(cur
->bc_tp
, cur
->bc_private
.b
.ip
,
1541 xfs_ilog_fbroot(cur
->bc_private
.b
.whichfork
));
1544 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
1548 * Increment cursor by one record at the level.
1549 * For nonzero levels the leaf-ward information is untouched.
1552 xfs_btree_increment(
1553 struct xfs_btree_cur
*cur
,
1555 int *stat
) /* success/failure */
1557 struct xfs_btree_block
*block
;
1558 union xfs_btree_ptr ptr
;
1560 int error
; /* error return value */
1563 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
1564 XFS_BTREE_TRACE_ARGI(cur
, level
);
1566 ASSERT(level
< cur
->bc_nlevels
);
1568 /* Read-ahead to the right at this level. */
1569 xfs_btree_readahead(cur
, level
, XFS_BTCUR_RIGHTRA
);
1571 /* Get a pointer to the btree block. */
1572 block
= xfs_btree_get_block(cur
, level
, &bp
);
1575 error
= xfs_btree_check_block(cur
, block
, level
, bp
);
1580 /* We're done if we remain in the block after the increment. */
1581 if (++cur
->bc_ptrs
[level
] <= xfs_btree_get_numrecs(block
))
1584 /* Fail if we just went off the right edge of the tree. */
1585 xfs_btree_get_sibling(cur
, block
, &ptr
, XFS_BB_RIGHTSIB
);
1586 if (xfs_btree_ptr_is_null(cur
, &ptr
))
1589 XFS_BTREE_STATS_INC(cur
, increment
);
1592 * March up the tree incrementing pointers.
1593 * Stop when we don't go off the right edge of a block.
1595 for (lev
= level
+ 1; lev
< cur
->bc_nlevels
; lev
++) {
1596 block
= xfs_btree_get_block(cur
, lev
, &bp
);
1599 error
= xfs_btree_check_block(cur
, block
, lev
, bp
);
1604 if (++cur
->bc_ptrs
[lev
] <= xfs_btree_get_numrecs(block
))
1607 /* Read-ahead the right block for the next loop. */
1608 xfs_btree_readahead(cur
, lev
, XFS_BTCUR_RIGHTRA
);
1612 * If we went off the root then we are either seriously
1613 * confused or have the tree root in an inode.
1615 if (lev
== cur
->bc_nlevels
) {
1616 if (cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
)
1619 error
= -EFSCORRUPTED
;
1622 ASSERT(lev
< cur
->bc_nlevels
);
1625 * Now walk back down the tree, fixing up the cursor's buffer
1626 * pointers and key numbers.
1628 for (block
= xfs_btree_get_block(cur
, lev
, &bp
); lev
> level
; ) {
1629 union xfs_btree_ptr
*ptrp
;
1631 ptrp
= xfs_btree_ptr_addr(cur
, cur
->bc_ptrs
[lev
], block
);
1633 error
= xfs_btree_read_buf_block(cur
, ptrp
, 0, &block
, &bp
);
1637 xfs_btree_setbuf(cur
, lev
, bp
);
1638 cur
->bc_ptrs
[lev
] = 1;
1641 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
1646 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
1651 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
1656 * Decrement cursor by one record at the level.
1657 * For nonzero levels the leaf-ward information is untouched.
1660 xfs_btree_decrement(
1661 struct xfs_btree_cur
*cur
,
1663 int *stat
) /* success/failure */
1665 struct xfs_btree_block
*block
;
1667 int error
; /* error return value */
1669 union xfs_btree_ptr ptr
;
1671 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
1672 XFS_BTREE_TRACE_ARGI(cur
, level
);
1674 ASSERT(level
< cur
->bc_nlevels
);
1676 /* Read-ahead to the left at this level. */
1677 xfs_btree_readahead(cur
, level
, XFS_BTCUR_LEFTRA
);
1679 /* We're done if we remain in the block after the decrement. */
1680 if (--cur
->bc_ptrs
[level
] > 0)
1683 /* Get a pointer to the btree block. */
1684 block
= xfs_btree_get_block(cur
, level
, &bp
);
1687 error
= xfs_btree_check_block(cur
, block
, level
, bp
);
1692 /* Fail if we just went off the left edge of the tree. */
1693 xfs_btree_get_sibling(cur
, block
, &ptr
, XFS_BB_LEFTSIB
);
1694 if (xfs_btree_ptr_is_null(cur
, &ptr
))
1697 XFS_BTREE_STATS_INC(cur
, decrement
);
1700 * March up the tree decrementing pointers.
1701 * Stop when we don't go off the left edge of a block.
1703 for (lev
= level
+ 1; lev
< cur
->bc_nlevels
; lev
++) {
1704 if (--cur
->bc_ptrs
[lev
] > 0)
1706 /* Read-ahead the left block for the next loop. */
1707 xfs_btree_readahead(cur
, lev
, XFS_BTCUR_LEFTRA
);
1711 * If we went off the root then we are seriously confused.
1712 * or the root of the tree is in an inode.
1714 if (lev
== cur
->bc_nlevels
) {
1715 if (cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
)
1718 error
= -EFSCORRUPTED
;
1721 ASSERT(lev
< cur
->bc_nlevels
);
1724 * Now walk back down the tree, fixing up the cursor's buffer
1725 * pointers and key numbers.
1727 for (block
= xfs_btree_get_block(cur
, lev
, &bp
); lev
> level
; ) {
1728 union xfs_btree_ptr
*ptrp
;
1730 ptrp
= xfs_btree_ptr_addr(cur
, cur
->bc_ptrs
[lev
], block
);
1732 error
= xfs_btree_read_buf_block(cur
, ptrp
, 0, &block
, &bp
);
1735 xfs_btree_setbuf(cur
, lev
, bp
);
1736 cur
->bc_ptrs
[lev
] = xfs_btree_get_numrecs(block
);
1739 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
1744 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
1749 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
1754 xfs_btree_lookup_get_block(
1755 struct xfs_btree_cur
*cur
, /* btree cursor */
1756 int level
, /* level in the btree */
1757 union xfs_btree_ptr
*pp
, /* ptr to btree block */
1758 struct xfs_btree_block
**blkp
) /* return btree block */
1760 struct xfs_buf
*bp
; /* buffer pointer for btree block */
1763 /* special case the root block if in an inode */
1764 if ((cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
) &&
1765 (level
== cur
->bc_nlevels
- 1)) {
1766 *blkp
= xfs_btree_get_iroot(cur
);
1771 * If the old buffer at this level for the disk address we are
1772 * looking for re-use it.
1774 * Otherwise throw it away and get a new one.
1776 bp
= cur
->bc_bufs
[level
];
1777 if (bp
&& XFS_BUF_ADDR(bp
) == xfs_btree_ptr_to_daddr(cur
, pp
)) {
1778 *blkp
= XFS_BUF_TO_BLOCK(bp
);
1782 error
= xfs_btree_read_buf_block(cur
, pp
, 0, blkp
, &bp
);
1786 /* Check the inode owner since the verifiers don't. */
1787 if (xfs_sb_version_hascrc(&cur
->bc_mp
->m_sb
) &&
1788 (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
) &&
1789 be64_to_cpu((*blkp
)->bb_u
.l
.bb_owner
) !=
1790 cur
->bc_private
.b
.ip
->i_ino
)
1793 /* Did we get the level we were looking for? */
1794 if (be16_to_cpu((*blkp
)->bb_level
) != level
)
1797 /* Check that internal nodes have at least one record. */
1798 if (level
!= 0 && be16_to_cpu((*blkp
)->bb_numrecs
) == 0)
1801 xfs_btree_setbuf(cur
, level
, bp
);
1806 xfs_trans_brelse(cur
->bc_tp
, bp
);
1807 return -EFSCORRUPTED
;
1811 * Get current search key. For level 0 we don't actually have a key
1812 * structure so we make one up from the record. For all other levels
1813 * we just return the right key.
1815 STATIC
union xfs_btree_key
*
1816 xfs_lookup_get_search_key(
1817 struct xfs_btree_cur
*cur
,
1820 struct xfs_btree_block
*block
,
1821 union xfs_btree_key
*kp
)
1824 cur
->bc_ops
->init_key_from_rec(kp
,
1825 xfs_btree_rec_addr(cur
, keyno
, block
));
1829 return xfs_btree_key_addr(cur
, keyno
, block
);
1833 * Lookup the record. The cursor is made to point to it, based on dir.
1834 * stat is set to 0 if can't find any such record, 1 for success.
1838 struct xfs_btree_cur
*cur
, /* btree cursor */
1839 xfs_lookup_t dir
, /* <=, ==, or >= */
1840 int *stat
) /* success/failure */
1842 struct xfs_btree_block
*block
; /* current btree block */
1843 int64_t diff
; /* difference for the current key */
1844 int error
; /* error return value */
1845 int keyno
; /* current key number */
1846 int level
; /* level in the btree */
1847 union xfs_btree_ptr
*pp
; /* ptr to btree block */
1848 union xfs_btree_ptr ptr
; /* ptr to btree block */
1850 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
1851 XFS_BTREE_TRACE_ARGI(cur
, dir
);
1853 XFS_BTREE_STATS_INC(cur
, lookup
);
1855 /* No such thing as a zero-level tree. */
1856 if (cur
->bc_nlevels
== 0)
1857 return -EFSCORRUPTED
;
1862 /* initialise start pointer from cursor */
1863 cur
->bc_ops
->init_ptr_from_cur(cur
, &ptr
);
1867 * Iterate over each level in the btree, starting at the root.
1868 * For each level above the leaves, find the key we need, based
1869 * on the lookup record, then follow the corresponding block
1870 * pointer down to the next level.
1872 for (level
= cur
->bc_nlevels
- 1, diff
= 1; level
>= 0; level
--) {
1873 /* Get the block we need to do the lookup on. */
1874 error
= xfs_btree_lookup_get_block(cur
, level
, pp
, &block
);
1880 * If we already had a key match at a higher level, we
1881 * know we need to use the first entry in this block.
1885 /* Otherwise search this block. Do a binary search. */
1887 int high
; /* high entry number */
1888 int low
; /* low entry number */
1890 /* Set low and high entry numbers, 1-based. */
1892 high
= xfs_btree_get_numrecs(block
);
1894 /* Block is empty, must be an empty leaf. */
1895 ASSERT(level
== 0 && cur
->bc_nlevels
== 1);
1897 cur
->bc_ptrs
[0] = dir
!= XFS_LOOKUP_LE
;
1898 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
1903 /* Binary search the block. */
1904 while (low
<= high
) {
1905 union xfs_btree_key key
;
1906 union xfs_btree_key
*kp
;
1908 XFS_BTREE_STATS_INC(cur
, compare
);
1910 /* keyno is average of low and high. */
1911 keyno
= (low
+ high
) >> 1;
1913 /* Get current search key */
1914 kp
= xfs_lookup_get_search_key(cur
, level
,
1915 keyno
, block
, &key
);
1918 * Compute difference to get next direction:
1919 * - less than, move right
1920 * - greater than, move left
1921 * - equal, we're done
1923 diff
= cur
->bc_ops
->key_diff(cur
, kp
);
1934 * If there are more levels, set up for the next level
1935 * by getting the block number and filling in the cursor.
1939 * If we moved left, need the previous key number,
1940 * unless there isn't one.
1942 if (diff
> 0 && --keyno
< 1)
1944 pp
= xfs_btree_ptr_addr(cur
, keyno
, block
);
1947 error
= xfs_btree_check_ptr(cur
, pp
, 0, level
);
1951 cur
->bc_ptrs
[level
] = keyno
;
1955 /* Done with the search. See if we need to adjust the results. */
1956 if (dir
!= XFS_LOOKUP_LE
&& diff
< 0) {
1959 * If ge search and we went off the end of the block, but it's
1960 * not the last block, we're in the wrong block.
1962 xfs_btree_get_sibling(cur
, block
, &ptr
, XFS_BB_RIGHTSIB
);
1963 if (dir
== XFS_LOOKUP_GE
&&
1964 keyno
> xfs_btree_get_numrecs(block
) &&
1965 !xfs_btree_ptr_is_null(cur
, &ptr
)) {
1968 cur
->bc_ptrs
[0] = keyno
;
1969 error
= xfs_btree_increment(cur
, 0, &i
);
1972 XFS_WANT_CORRUPTED_RETURN(cur
->bc_mp
, i
== 1);
1973 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
1977 } else if (dir
== XFS_LOOKUP_LE
&& diff
> 0)
1979 cur
->bc_ptrs
[0] = keyno
;
1981 /* Return if we succeeded or not. */
1982 if (keyno
== 0 || keyno
> xfs_btree_get_numrecs(block
))
1984 else if (dir
!= XFS_LOOKUP_EQ
|| diff
== 0)
1988 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
1992 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
1996 /* Find the high key storage area from a regular key. */
1997 STATIC
union xfs_btree_key
*
1998 xfs_btree_high_key_from_key(
1999 struct xfs_btree_cur
*cur
,
2000 union xfs_btree_key
*key
)
2002 ASSERT(cur
->bc_flags
& XFS_BTREE_OVERLAPPING
);
2003 return (union xfs_btree_key
*)((char *)key
+
2004 (cur
->bc_ops
->key_len
/ 2));
2007 /* Determine the low (and high if overlapped) keys of a leaf block */
2009 xfs_btree_get_leaf_keys(
2010 struct xfs_btree_cur
*cur
,
2011 struct xfs_btree_block
*block
,
2012 union xfs_btree_key
*key
)
2014 union xfs_btree_key max_hkey
;
2015 union xfs_btree_key hkey
;
2016 union xfs_btree_rec
*rec
;
2017 union xfs_btree_key
*high
;
2020 rec
= xfs_btree_rec_addr(cur
, 1, block
);
2021 cur
->bc_ops
->init_key_from_rec(key
, rec
);
2023 if (cur
->bc_flags
& XFS_BTREE_OVERLAPPING
) {
2025 cur
->bc_ops
->init_high_key_from_rec(&max_hkey
, rec
);
2026 for (n
= 2; n
<= xfs_btree_get_numrecs(block
); n
++) {
2027 rec
= xfs_btree_rec_addr(cur
, n
, block
);
2028 cur
->bc_ops
->init_high_key_from_rec(&hkey
, rec
);
2029 if (cur
->bc_ops
->diff_two_keys(cur
, &hkey
, &max_hkey
)
2034 high
= xfs_btree_high_key_from_key(cur
, key
);
2035 memcpy(high
, &max_hkey
, cur
->bc_ops
->key_len
/ 2);
2039 /* Determine the low (and high if overlapped) keys of a node block */
2041 xfs_btree_get_node_keys(
2042 struct xfs_btree_cur
*cur
,
2043 struct xfs_btree_block
*block
,
2044 union xfs_btree_key
*key
)
2046 union xfs_btree_key
*hkey
;
2047 union xfs_btree_key
*max_hkey
;
2048 union xfs_btree_key
*high
;
2051 if (cur
->bc_flags
& XFS_BTREE_OVERLAPPING
) {
2052 memcpy(key
, xfs_btree_key_addr(cur
, 1, block
),
2053 cur
->bc_ops
->key_len
/ 2);
2055 max_hkey
= xfs_btree_high_key_addr(cur
, 1, block
);
2056 for (n
= 2; n
<= xfs_btree_get_numrecs(block
); n
++) {
2057 hkey
= xfs_btree_high_key_addr(cur
, n
, block
);
2058 if (cur
->bc_ops
->diff_two_keys(cur
, hkey
, max_hkey
) > 0)
2062 high
= xfs_btree_high_key_from_key(cur
, key
);
2063 memcpy(high
, max_hkey
, cur
->bc_ops
->key_len
/ 2);
2065 memcpy(key
, xfs_btree_key_addr(cur
, 1, block
),
2066 cur
->bc_ops
->key_len
);
2070 /* Derive the keys for any btree block. */
2073 struct xfs_btree_cur
*cur
,
2074 struct xfs_btree_block
*block
,
2075 union xfs_btree_key
*key
)
2077 if (be16_to_cpu(block
->bb_level
) == 0)
2078 xfs_btree_get_leaf_keys(cur
, block
, key
);
2080 xfs_btree_get_node_keys(cur
, block
, key
);
2084 * Decide if we need to update the parent keys of a btree block. For
2085 * a standard btree this is only necessary if we're updating the first
2086 * record/key. For an overlapping btree, we must always update the
2087 * keys because the highest key can be in any of the records or keys
2091 xfs_btree_needs_key_update(
2092 struct xfs_btree_cur
*cur
,
2095 return (cur
->bc_flags
& XFS_BTREE_OVERLAPPING
) || ptr
== 1;
2099 * Update the low and high parent keys of the given level, progressing
2100 * towards the root. If force_all is false, stop if the keys for a given
2101 * level do not need updating.
2104 __xfs_btree_updkeys(
2105 struct xfs_btree_cur
*cur
,
2107 struct xfs_btree_block
*block
,
2108 struct xfs_buf
*bp0
,
2111 union xfs_btree_key key
; /* keys from current level */
2112 union xfs_btree_key
*lkey
; /* keys from the next level up */
2113 union xfs_btree_key
*hkey
;
2114 union xfs_btree_key
*nlkey
; /* keys from the next level up */
2115 union xfs_btree_key
*nhkey
;
2119 ASSERT(cur
->bc_flags
& XFS_BTREE_OVERLAPPING
);
2121 /* Exit if there aren't any parent levels to update. */
2122 if (level
+ 1 >= cur
->bc_nlevels
)
2125 trace_xfs_btree_updkeys(cur
, level
, bp0
);
2128 hkey
= xfs_btree_high_key_from_key(cur
, lkey
);
2129 xfs_btree_get_keys(cur
, block
, lkey
);
2130 for (level
++; level
< cur
->bc_nlevels
; level
++) {
2134 block
= xfs_btree_get_block(cur
, level
, &bp
);
2135 trace_xfs_btree_updkeys(cur
, level
, bp
);
2137 error
= xfs_btree_check_block(cur
, block
, level
, bp
);
2139 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
2143 ptr
= cur
->bc_ptrs
[level
];
2144 nlkey
= xfs_btree_key_addr(cur
, ptr
, block
);
2145 nhkey
= xfs_btree_high_key_addr(cur
, ptr
, block
);
2147 !(cur
->bc_ops
->diff_two_keys(cur
, nlkey
, lkey
) != 0 ||
2148 cur
->bc_ops
->diff_two_keys(cur
, nhkey
, hkey
) != 0))
2150 xfs_btree_copy_keys(cur
, nlkey
, lkey
, 1);
2151 xfs_btree_log_keys(cur
, bp
, ptr
, ptr
);
2152 if (level
+ 1 >= cur
->bc_nlevels
)
2154 xfs_btree_get_node_keys(cur
, block
, lkey
);
2160 /* Update all the keys from some level in cursor back to the root. */
2162 xfs_btree_updkeys_force(
2163 struct xfs_btree_cur
*cur
,
2167 struct xfs_btree_block
*block
;
2169 block
= xfs_btree_get_block(cur
, level
, &bp
);
2170 return __xfs_btree_updkeys(cur
, level
, block
, bp
, true);
2174 * Update the parent keys of the given level, progressing towards the root.
2177 xfs_btree_update_keys(
2178 struct xfs_btree_cur
*cur
,
2181 struct xfs_btree_block
*block
;
2183 union xfs_btree_key
*kp
;
2184 union xfs_btree_key key
;
2189 block
= xfs_btree_get_block(cur
, level
, &bp
);
2190 if (cur
->bc_flags
& XFS_BTREE_OVERLAPPING
)
2191 return __xfs_btree_updkeys(cur
, level
, block
, bp
, false);
2193 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
2194 XFS_BTREE_TRACE_ARGIK(cur
, level
, keyp
);
2197 * Go up the tree from this level toward the root.
2198 * At each level, update the key value to the value input.
2199 * Stop when we reach a level where the cursor isn't pointing
2200 * at the first entry in the block.
2202 xfs_btree_get_keys(cur
, block
, &key
);
2203 for (level
++, ptr
= 1; ptr
== 1 && level
< cur
->bc_nlevels
; level
++) {
2207 block
= xfs_btree_get_block(cur
, level
, &bp
);
2209 error
= xfs_btree_check_block(cur
, block
, level
, bp
);
2211 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
2215 ptr
= cur
->bc_ptrs
[level
];
2216 kp
= xfs_btree_key_addr(cur
, ptr
, block
);
2217 xfs_btree_copy_keys(cur
, kp
, &key
, 1);
2218 xfs_btree_log_keys(cur
, bp
, ptr
, ptr
);
2221 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
2226 * Update the record referred to by cur to the value in the
2227 * given record. This either works (return 0) or gets an
2228 * EFSCORRUPTED error.
2232 struct xfs_btree_cur
*cur
,
2233 union xfs_btree_rec
*rec
)
2235 struct xfs_btree_block
*block
;
2239 union xfs_btree_rec
*rp
;
2241 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
2242 XFS_BTREE_TRACE_ARGR(cur
, rec
);
2244 /* Pick up the current block. */
2245 block
= xfs_btree_get_block(cur
, 0, &bp
);
2248 error
= xfs_btree_check_block(cur
, block
, 0, bp
);
2252 /* Get the address of the rec to be updated. */
2253 ptr
= cur
->bc_ptrs
[0];
2254 rp
= xfs_btree_rec_addr(cur
, ptr
, block
);
2256 /* Fill in the new contents and log them. */
2257 xfs_btree_copy_recs(cur
, rp
, rec
, 1);
2258 xfs_btree_log_recs(cur
, bp
, ptr
, ptr
);
2261 * If we are tracking the last record in the tree and
2262 * we are at the far right edge of the tree, update it.
2264 if (xfs_btree_is_lastrec(cur
, block
, 0)) {
2265 cur
->bc_ops
->update_lastrec(cur
, block
, rec
,
2266 ptr
, LASTREC_UPDATE
);
2269 /* Pass new key value up to our parent. */
2270 if (xfs_btree_needs_key_update(cur
, ptr
)) {
2271 error
= xfs_btree_update_keys(cur
, 0);
2276 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
2280 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
2285 * Move 1 record left from cur/level if possible.
2286 * Update cur to reflect the new path.
2288 STATIC
int /* error */
2290 struct xfs_btree_cur
*cur
,
2292 int *stat
) /* success/failure */
2294 struct xfs_buf
*lbp
; /* left buffer pointer */
2295 struct xfs_btree_block
*left
; /* left btree block */
2296 int lrecs
; /* left record count */
2297 struct xfs_buf
*rbp
; /* right buffer pointer */
2298 struct xfs_btree_block
*right
; /* right btree block */
2299 struct xfs_btree_cur
*tcur
; /* temporary btree cursor */
2300 int rrecs
; /* right record count */
2301 union xfs_btree_ptr lptr
; /* left btree pointer */
2302 union xfs_btree_key
*rkp
= NULL
; /* right btree key */
2303 union xfs_btree_ptr
*rpp
= NULL
; /* right address pointer */
2304 union xfs_btree_rec
*rrp
= NULL
; /* right record pointer */
2305 int error
; /* error return value */
2308 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
2309 XFS_BTREE_TRACE_ARGI(cur
, level
);
2311 if ((cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
) &&
2312 level
== cur
->bc_nlevels
- 1)
2315 /* Set up variables for this block as "right". */
2316 right
= xfs_btree_get_block(cur
, level
, &rbp
);
2319 error
= xfs_btree_check_block(cur
, right
, level
, rbp
);
2324 /* If we've got no left sibling then we can't shift an entry left. */
2325 xfs_btree_get_sibling(cur
, right
, &lptr
, XFS_BB_LEFTSIB
);
2326 if (xfs_btree_ptr_is_null(cur
, &lptr
))
2330 * If the cursor entry is the one that would be moved, don't
2331 * do it... it's too complicated.
2333 if (cur
->bc_ptrs
[level
] <= 1)
2336 /* Set up the left neighbor as "left". */
2337 error
= xfs_btree_read_buf_block(cur
, &lptr
, 0, &left
, &lbp
);
2341 /* If it's full, it can't take another entry. */
2342 lrecs
= xfs_btree_get_numrecs(left
);
2343 if (lrecs
== cur
->bc_ops
->get_maxrecs(cur
, level
))
2346 rrecs
= xfs_btree_get_numrecs(right
);
2349 * We add one entry to the left side and remove one for the right side.
2350 * Account for it here, the changes will be updated on disk and logged
2356 XFS_BTREE_STATS_INC(cur
, lshift
);
2357 XFS_BTREE_STATS_ADD(cur
, moves
, 1);
2360 * If non-leaf, copy a key and a ptr to the left block.
2361 * Log the changes to the left block.
2364 /* It's a non-leaf. Move keys and pointers. */
2365 union xfs_btree_key
*lkp
; /* left btree key */
2366 union xfs_btree_ptr
*lpp
; /* left address pointer */
2368 lkp
= xfs_btree_key_addr(cur
, lrecs
, left
);
2369 rkp
= xfs_btree_key_addr(cur
, 1, right
);
2371 lpp
= xfs_btree_ptr_addr(cur
, lrecs
, left
);
2372 rpp
= xfs_btree_ptr_addr(cur
, 1, right
);
2374 error
= xfs_btree_check_ptr(cur
, rpp
, 0, level
);
2378 xfs_btree_copy_keys(cur
, lkp
, rkp
, 1);
2379 xfs_btree_copy_ptrs(cur
, lpp
, rpp
, 1);
2381 xfs_btree_log_keys(cur
, lbp
, lrecs
, lrecs
);
2382 xfs_btree_log_ptrs(cur
, lbp
, lrecs
, lrecs
);
2384 ASSERT(cur
->bc_ops
->keys_inorder(cur
,
2385 xfs_btree_key_addr(cur
, lrecs
- 1, left
), lkp
));
2387 /* It's a leaf. Move records. */
2388 union xfs_btree_rec
*lrp
; /* left record pointer */
2390 lrp
= xfs_btree_rec_addr(cur
, lrecs
, left
);
2391 rrp
= xfs_btree_rec_addr(cur
, 1, right
);
2393 xfs_btree_copy_recs(cur
, lrp
, rrp
, 1);
2394 xfs_btree_log_recs(cur
, lbp
, lrecs
, lrecs
);
2396 ASSERT(cur
->bc_ops
->recs_inorder(cur
,
2397 xfs_btree_rec_addr(cur
, lrecs
- 1, left
), lrp
));
2400 xfs_btree_set_numrecs(left
, lrecs
);
2401 xfs_btree_log_block(cur
, lbp
, XFS_BB_NUMRECS
);
2403 xfs_btree_set_numrecs(right
, rrecs
);
2404 xfs_btree_log_block(cur
, rbp
, XFS_BB_NUMRECS
);
2407 * Slide the contents of right down one entry.
2409 XFS_BTREE_STATS_ADD(cur
, moves
, rrecs
- 1);
2411 /* It's a nonleaf. operate on keys and ptrs */
2413 int i
; /* loop index */
2415 for (i
= 0; i
< rrecs
; i
++) {
2416 error
= xfs_btree_check_ptr(cur
, rpp
, i
+ 1, level
);
2421 xfs_btree_shift_keys(cur
,
2422 xfs_btree_key_addr(cur
, 2, right
),
2424 xfs_btree_shift_ptrs(cur
,
2425 xfs_btree_ptr_addr(cur
, 2, right
),
2428 xfs_btree_log_keys(cur
, rbp
, 1, rrecs
);
2429 xfs_btree_log_ptrs(cur
, rbp
, 1, rrecs
);
2431 /* It's a leaf. operate on records */
2432 xfs_btree_shift_recs(cur
,
2433 xfs_btree_rec_addr(cur
, 2, right
),
2435 xfs_btree_log_recs(cur
, rbp
, 1, rrecs
);
2439 * Using a temporary cursor, update the parent key values of the
2440 * block on the left.
2442 if (cur
->bc_flags
& XFS_BTREE_OVERLAPPING
) {
2443 error
= xfs_btree_dup_cursor(cur
, &tcur
);
2446 i
= xfs_btree_firstrec(tcur
, level
);
2447 XFS_WANT_CORRUPTED_GOTO(tcur
->bc_mp
, i
== 1, error0
);
2449 error
= xfs_btree_decrement(tcur
, level
, &i
);
2453 /* Update the parent high keys of the left block, if needed. */
2454 error
= xfs_btree_update_keys(tcur
, level
);
2458 xfs_btree_del_cursor(tcur
, XFS_BTREE_NOERROR
);
2461 /* Update the parent keys of the right block. */
2462 error
= xfs_btree_update_keys(cur
, level
);
2466 /* Slide the cursor value left one. */
2467 cur
->bc_ptrs
[level
]--;
2469 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
2474 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
2479 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
2483 XFS_BTREE_TRACE_CURSOR(tcur
, XBT_ERROR
);
2484 xfs_btree_del_cursor(tcur
, XFS_BTREE_ERROR
);
2489 * Move 1 record right from cur/level if possible.
2490 * Update cur to reflect the new path.
2492 STATIC
int /* error */
2494 struct xfs_btree_cur
*cur
,
2496 int *stat
) /* success/failure */
2498 struct xfs_buf
*lbp
; /* left buffer pointer */
2499 struct xfs_btree_block
*left
; /* left btree block */
2500 struct xfs_buf
*rbp
; /* right buffer pointer */
2501 struct xfs_btree_block
*right
; /* right btree block */
2502 struct xfs_btree_cur
*tcur
; /* temporary btree cursor */
2503 union xfs_btree_ptr rptr
; /* right block pointer */
2504 union xfs_btree_key
*rkp
; /* right btree key */
2505 int rrecs
; /* right record count */
2506 int lrecs
; /* left record count */
2507 int error
; /* error return value */
2508 int i
; /* loop counter */
2510 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
2511 XFS_BTREE_TRACE_ARGI(cur
, level
);
2513 if ((cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
) &&
2514 (level
== cur
->bc_nlevels
- 1))
2517 /* Set up variables for this block as "left". */
2518 left
= xfs_btree_get_block(cur
, level
, &lbp
);
2521 error
= xfs_btree_check_block(cur
, left
, level
, lbp
);
2526 /* If we've got no right sibling then we can't shift an entry right. */
2527 xfs_btree_get_sibling(cur
, left
, &rptr
, XFS_BB_RIGHTSIB
);
2528 if (xfs_btree_ptr_is_null(cur
, &rptr
))
2532 * If the cursor entry is the one that would be moved, don't
2533 * do it... it's too complicated.
2535 lrecs
= xfs_btree_get_numrecs(left
);
2536 if (cur
->bc_ptrs
[level
] >= lrecs
)
2539 /* Set up the right neighbor as "right". */
2540 error
= xfs_btree_read_buf_block(cur
, &rptr
, 0, &right
, &rbp
);
2544 /* If it's full, it can't take another entry. */
2545 rrecs
= xfs_btree_get_numrecs(right
);
2546 if (rrecs
== cur
->bc_ops
->get_maxrecs(cur
, level
))
2549 XFS_BTREE_STATS_INC(cur
, rshift
);
2550 XFS_BTREE_STATS_ADD(cur
, moves
, rrecs
);
2553 * Make a hole at the start of the right neighbor block, then
2554 * copy the last left block entry to the hole.
2557 /* It's a nonleaf. make a hole in the keys and ptrs */
2558 union xfs_btree_key
*lkp
;
2559 union xfs_btree_ptr
*lpp
;
2560 union xfs_btree_ptr
*rpp
;
2562 lkp
= xfs_btree_key_addr(cur
, lrecs
, left
);
2563 lpp
= xfs_btree_ptr_addr(cur
, lrecs
, left
);
2564 rkp
= xfs_btree_key_addr(cur
, 1, right
);
2565 rpp
= xfs_btree_ptr_addr(cur
, 1, right
);
2568 for (i
= rrecs
- 1; i
>= 0; i
--) {
2569 error
= xfs_btree_check_ptr(cur
, rpp
, i
, level
);
2575 xfs_btree_shift_keys(cur
, rkp
, 1, rrecs
);
2576 xfs_btree_shift_ptrs(cur
, rpp
, 1, rrecs
);
2579 error
= xfs_btree_check_ptr(cur
, lpp
, 0, level
);
2584 /* Now put the new data in, and log it. */
2585 xfs_btree_copy_keys(cur
, rkp
, lkp
, 1);
2586 xfs_btree_copy_ptrs(cur
, rpp
, lpp
, 1);
2588 xfs_btree_log_keys(cur
, rbp
, 1, rrecs
+ 1);
2589 xfs_btree_log_ptrs(cur
, rbp
, 1, rrecs
+ 1);
2591 ASSERT(cur
->bc_ops
->keys_inorder(cur
, rkp
,
2592 xfs_btree_key_addr(cur
, 2, right
)));
2594 /* It's a leaf. make a hole in the records */
2595 union xfs_btree_rec
*lrp
;
2596 union xfs_btree_rec
*rrp
;
2598 lrp
= xfs_btree_rec_addr(cur
, lrecs
, left
);
2599 rrp
= xfs_btree_rec_addr(cur
, 1, right
);
2601 xfs_btree_shift_recs(cur
, rrp
, 1, rrecs
);
2603 /* Now put the new data in, and log it. */
2604 xfs_btree_copy_recs(cur
, rrp
, lrp
, 1);
2605 xfs_btree_log_recs(cur
, rbp
, 1, rrecs
+ 1);
2609 * Decrement and log left's numrecs, bump and log right's numrecs.
2611 xfs_btree_set_numrecs(left
, --lrecs
);
2612 xfs_btree_log_block(cur
, lbp
, XFS_BB_NUMRECS
);
2614 xfs_btree_set_numrecs(right
, ++rrecs
);
2615 xfs_btree_log_block(cur
, rbp
, XFS_BB_NUMRECS
);
2618 * Using a temporary cursor, update the parent key values of the
2619 * block on the right.
2621 error
= xfs_btree_dup_cursor(cur
, &tcur
);
2624 i
= xfs_btree_lastrec(tcur
, level
);
2625 XFS_WANT_CORRUPTED_GOTO(tcur
->bc_mp
, i
== 1, error0
);
2627 error
= xfs_btree_increment(tcur
, level
, &i
);
2631 /* Update the parent high keys of the left block, if needed. */
2632 if (cur
->bc_flags
& XFS_BTREE_OVERLAPPING
) {
2633 error
= xfs_btree_update_keys(cur
, level
);
2638 /* Update the parent keys of the right block. */
2639 error
= xfs_btree_update_keys(tcur
, level
);
2643 xfs_btree_del_cursor(tcur
, XFS_BTREE_NOERROR
);
2645 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
2650 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
2655 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
2659 XFS_BTREE_TRACE_CURSOR(tcur
, XBT_ERROR
);
2660 xfs_btree_del_cursor(tcur
, XFS_BTREE_ERROR
);
2665 * Split cur/level block in half.
2666 * Return new block number and the key to its first
2667 * record (to be inserted into parent).
2669 STATIC
int /* error */
2671 struct xfs_btree_cur
*cur
,
2673 union xfs_btree_ptr
*ptrp
,
2674 union xfs_btree_key
*key
,
2675 struct xfs_btree_cur
**curp
,
2676 int *stat
) /* success/failure */
2678 union xfs_btree_ptr lptr
; /* left sibling block ptr */
2679 struct xfs_buf
*lbp
; /* left buffer pointer */
2680 struct xfs_btree_block
*left
; /* left btree block */
2681 union xfs_btree_ptr rptr
; /* right sibling block ptr */
2682 struct xfs_buf
*rbp
; /* right buffer pointer */
2683 struct xfs_btree_block
*right
; /* right btree block */
2684 union xfs_btree_ptr rrptr
; /* right-right sibling ptr */
2685 struct xfs_buf
*rrbp
; /* right-right buffer pointer */
2686 struct xfs_btree_block
*rrblock
; /* right-right btree block */
2690 int error
; /* error return value */
2695 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
2696 XFS_BTREE_TRACE_ARGIPK(cur
, level
, *ptrp
, key
);
2698 XFS_BTREE_STATS_INC(cur
, split
);
2700 /* Set up left block (current one). */
2701 left
= xfs_btree_get_block(cur
, level
, &lbp
);
2704 error
= xfs_btree_check_block(cur
, left
, level
, lbp
);
2709 xfs_btree_buf_to_ptr(cur
, lbp
, &lptr
);
2711 /* Allocate the new block. If we can't do it, we're toast. Give up. */
2712 error
= cur
->bc_ops
->alloc_block(cur
, &lptr
, &rptr
, stat
);
2717 XFS_BTREE_STATS_INC(cur
, alloc
);
2719 /* Set up the new block as "right". */
2720 error
= xfs_btree_get_buf_block(cur
, &rptr
, 0, &right
, &rbp
);
2724 /* Fill in the btree header for the new right block. */
2725 xfs_btree_init_block_cur(cur
, rbp
, xfs_btree_get_level(left
), 0);
2728 * Split the entries between the old and the new block evenly.
2729 * Make sure that if there's an odd number of entries now, that
2730 * each new block will have the same number of entries.
2732 lrecs
= xfs_btree_get_numrecs(left
);
2734 if ((lrecs
& 1) && cur
->bc_ptrs
[level
] <= rrecs
+ 1)
2736 src_index
= (lrecs
- rrecs
+ 1);
2738 XFS_BTREE_STATS_ADD(cur
, moves
, rrecs
);
2740 /* Adjust numrecs for the later get_*_keys() calls. */
2742 xfs_btree_set_numrecs(left
, lrecs
);
2743 xfs_btree_set_numrecs(right
, xfs_btree_get_numrecs(right
) + rrecs
);
2746 * Copy btree block entries from the left block over to the
2747 * new block, the right. Update the right block and log the
2751 /* It's a non-leaf. Move keys and pointers. */
2752 union xfs_btree_key
*lkp
; /* left btree key */
2753 union xfs_btree_ptr
*lpp
; /* left address pointer */
2754 union xfs_btree_key
*rkp
; /* right btree key */
2755 union xfs_btree_ptr
*rpp
; /* right address pointer */
2757 lkp
= xfs_btree_key_addr(cur
, src_index
, left
);
2758 lpp
= xfs_btree_ptr_addr(cur
, src_index
, left
);
2759 rkp
= xfs_btree_key_addr(cur
, 1, right
);
2760 rpp
= xfs_btree_ptr_addr(cur
, 1, right
);
2763 for (i
= src_index
; i
< rrecs
; i
++) {
2764 error
= xfs_btree_check_ptr(cur
, lpp
, i
, level
);
2770 /* Copy the keys & pointers to the new block. */
2771 xfs_btree_copy_keys(cur
, rkp
, lkp
, rrecs
);
2772 xfs_btree_copy_ptrs(cur
, rpp
, lpp
, rrecs
);
2774 xfs_btree_log_keys(cur
, rbp
, 1, rrecs
);
2775 xfs_btree_log_ptrs(cur
, rbp
, 1, rrecs
);
2777 /* Stash the keys of the new block for later insertion. */
2778 xfs_btree_get_node_keys(cur
, right
, key
);
2780 /* It's a leaf. Move records. */
2781 union xfs_btree_rec
*lrp
; /* left record pointer */
2782 union xfs_btree_rec
*rrp
; /* right record pointer */
2784 lrp
= xfs_btree_rec_addr(cur
, src_index
, left
);
2785 rrp
= xfs_btree_rec_addr(cur
, 1, right
);
2787 /* Copy records to the new block. */
2788 xfs_btree_copy_recs(cur
, rrp
, lrp
, rrecs
);
2789 xfs_btree_log_recs(cur
, rbp
, 1, rrecs
);
2791 /* Stash the keys of the new block for later insertion. */
2792 xfs_btree_get_leaf_keys(cur
, right
, key
);
2796 * Find the left block number by looking in the buffer.
2797 * Adjust sibling pointers.
2799 xfs_btree_get_sibling(cur
, left
, &rrptr
, XFS_BB_RIGHTSIB
);
2800 xfs_btree_set_sibling(cur
, right
, &rrptr
, XFS_BB_RIGHTSIB
);
2801 xfs_btree_set_sibling(cur
, right
, &lptr
, XFS_BB_LEFTSIB
);
2802 xfs_btree_set_sibling(cur
, left
, &rptr
, XFS_BB_RIGHTSIB
);
2804 xfs_btree_log_block(cur
, rbp
, XFS_BB_ALL_BITS
);
2805 xfs_btree_log_block(cur
, lbp
, XFS_BB_NUMRECS
| XFS_BB_RIGHTSIB
);
2808 * If there's a block to the new block's right, make that block
2809 * point back to right instead of to left.
2811 if (!xfs_btree_ptr_is_null(cur
, &rrptr
)) {
2812 error
= xfs_btree_read_buf_block(cur
, &rrptr
,
2813 0, &rrblock
, &rrbp
);
2816 xfs_btree_set_sibling(cur
, rrblock
, &rptr
, XFS_BB_LEFTSIB
);
2817 xfs_btree_log_block(cur
, rrbp
, XFS_BB_LEFTSIB
);
2820 /* Update the parent high keys of the left block, if needed. */
2821 if (cur
->bc_flags
& XFS_BTREE_OVERLAPPING
) {
2822 error
= xfs_btree_update_keys(cur
, level
);
2828 * If the cursor is really in the right block, move it there.
2829 * If it's just pointing past the last entry in left, then we'll
2830 * insert there, so don't change anything in that case.
2832 if (cur
->bc_ptrs
[level
] > lrecs
+ 1) {
2833 xfs_btree_setbuf(cur
, level
, rbp
);
2834 cur
->bc_ptrs
[level
] -= lrecs
;
2837 * If there are more levels, we'll need another cursor which refers
2838 * the right block, no matter where this cursor was.
2840 if (level
+ 1 < cur
->bc_nlevels
) {
2841 error
= xfs_btree_dup_cursor(cur
, curp
);
2844 (*curp
)->bc_ptrs
[level
+ 1]++;
2847 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
2851 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
2856 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
2861 struct xfs_btree_split_args
{
2862 struct xfs_btree_cur
*cur
;
2864 union xfs_btree_ptr
*ptrp
;
2865 union xfs_btree_key
*key
;
2866 struct xfs_btree_cur
**curp
;
2867 int *stat
; /* success/failure */
2869 bool kswapd
; /* allocation in kswapd context */
2870 struct completion
*done
;
2871 struct work_struct work
;
2875 * Stack switching interfaces for allocation
2878 xfs_btree_split_worker(
2879 struct work_struct
*work
)
2881 struct xfs_btree_split_args
*args
= container_of(work
,
2882 struct xfs_btree_split_args
, work
);
2883 unsigned long pflags
;
2884 unsigned long new_pflags
= PF_MEMALLOC_NOFS
;
2887 * we are in a transaction context here, but may also be doing work
2888 * in kswapd context, and hence we may need to inherit that state
2889 * temporarily to ensure that we don't block waiting for memory reclaim
2893 new_pflags
|= PF_MEMALLOC
| PF_SWAPWRITE
| PF_KSWAPD
;
2895 current_set_flags_nested(&pflags
, new_pflags
);
2897 args
->result
= __xfs_btree_split(args
->cur
, args
->level
, args
->ptrp
,
2898 args
->key
, args
->curp
, args
->stat
);
2899 complete(args
->done
);
2901 current_restore_flags_nested(&pflags
, new_pflags
);
2905 * BMBT split requests often come in with little stack to work on. Push
2906 * them off to a worker thread so there is lots of stack to use. For the other
2907 * btree types, just call directly to avoid the context switch overhead here.
2909 STATIC
int /* error */
2911 struct xfs_btree_cur
*cur
,
2913 union xfs_btree_ptr
*ptrp
,
2914 union xfs_btree_key
*key
,
2915 struct xfs_btree_cur
**curp
,
2916 int *stat
) /* success/failure */
2918 struct xfs_btree_split_args args
;
2919 DECLARE_COMPLETION_ONSTACK(done
);
2921 if (cur
->bc_btnum
!= XFS_BTNUM_BMAP
)
2922 return __xfs_btree_split(cur
, level
, ptrp
, key
, curp
, stat
);
2931 args
.kswapd
= current_is_kswapd();
2932 INIT_WORK_ONSTACK(&args
.work
, xfs_btree_split_worker
);
2933 queue_work(xfs_alloc_wq
, &args
.work
);
2934 wait_for_completion(&done
);
2935 destroy_work_on_stack(&args
.work
);
2939 #define xfs_btree_split __xfs_btree_split
2944 * Copy the old inode root contents into a real block and make the
2945 * broot point to it.
2948 xfs_btree_new_iroot(
2949 struct xfs_btree_cur
*cur
, /* btree cursor */
2950 int *logflags
, /* logging flags for inode */
2951 int *stat
) /* return status - 0 fail */
2953 struct xfs_buf
*cbp
; /* buffer for cblock */
2954 struct xfs_btree_block
*block
; /* btree block */
2955 struct xfs_btree_block
*cblock
; /* child btree block */
2956 union xfs_btree_key
*ckp
; /* child key pointer */
2957 union xfs_btree_ptr
*cpp
; /* child ptr pointer */
2958 union xfs_btree_key
*kp
; /* pointer to btree key */
2959 union xfs_btree_ptr
*pp
; /* pointer to block addr */
2960 union xfs_btree_ptr nptr
; /* new block addr */
2961 int level
; /* btree level */
2962 int error
; /* error return code */
2964 int i
; /* loop counter */
2967 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
2968 XFS_BTREE_STATS_INC(cur
, newroot
);
2970 ASSERT(cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
);
2972 level
= cur
->bc_nlevels
- 1;
2974 block
= xfs_btree_get_iroot(cur
);
2975 pp
= xfs_btree_ptr_addr(cur
, 1, block
);
2977 /* Allocate the new block. If we can't do it, we're toast. Give up. */
2978 error
= cur
->bc_ops
->alloc_block(cur
, pp
, &nptr
, stat
);
2982 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
2985 XFS_BTREE_STATS_INC(cur
, alloc
);
2987 /* Copy the root into a real block. */
2988 error
= xfs_btree_get_buf_block(cur
, &nptr
, 0, &cblock
, &cbp
);
2993 * we can't just memcpy() the root in for CRC enabled btree blocks.
2994 * In that case have to also ensure the blkno remains correct
2996 memcpy(cblock
, block
, xfs_btree_block_len(cur
));
2997 if (cur
->bc_flags
& XFS_BTREE_CRC_BLOCKS
) {
2998 if (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
)
2999 cblock
->bb_u
.l
.bb_blkno
= cpu_to_be64(cbp
->b_bn
);
3001 cblock
->bb_u
.s
.bb_blkno
= cpu_to_be64(cbp
->b_bn
);
3004 be16_add_cpu(&block
->bb_level
, 1);
3005 xfs_btree_set_numrecs(block
, 1);
3007 cur
->bc_ptrs
[level
+ 1] = 1;
3009 kp
= xfs_btree_key_addr(cur
, 1, block
);
3010 ckp
= xfs_btree_key_addr(cur
, 1, cblock
);
3011 xfs_btree_copy_keys(cur
, ckp
, kp
, xfs_btree_get_numrecs(cblock
));
3013 cpp
= xfs_btree_ptr_addr(cur
, 1, cblock
);
3015 for (i
= 0; i
< be16_to_cpu(cblock
->bb_numrecs
); i
++) {
3016 error
= xfs_btree_check_ptr(cur
, pp
, i
, level
);
3021 xfs_btree_copy_ptrs(cur
, cpp
, pp
, xfs_btree_get_numrecs(cblock
));
3024 error
= xfs_btree_check_ptr(cur
, &nptr
, 0, level
);
3028 xfs_btree_copy_ptrs(cur
, pp
, &nptr
, 1);
3030 xfs_iroot_realloc(cur
->bc_private
.b
.ip
,
3031 1 - xfs_btree_get_numrecs(cblock
),
3032 cur
->bc_private
.b
.whichfork
);
3034 xfs_btree_setbuf(cur
, level
, cbp
);
3037 * Do all this logging at the end so that
3038 * the root is at the right level.
3040 xfs_btree_log_block(cur
, cbp
, XFS_BB_ALL_BITS
);
3041 xfs_btree_log_keys(cur
, cbp
, 1, be16_to_cpu(cblock
->bb_numrecs
));
3042 xfs_btree_log_ptrs(cur
, cbp
, 1, be16_to_cpu(cblock
->bb_numrecs
));
3045 XFS_ILOG_CORE
| xfs_ilog_fbroot(cur
->bc_private
.b
.whichfork
);
3047 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
3050 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
3055 * Allocate a new root block, fill it in.
3057 STATIC
int /* error */
3059 struct xfs_btree_cur
*cur
, /* btree cursor */
3060 int *stat
) /* success/failure */
3062 struct xfs_btree_block
*block
; /* one half of the old root block */
3063 struct xfs_buf
*bp
; /* buffer containing block */
3064 int error
; /* error return value */
3065 struct xfs_buf
*lbp
; /* left buffer pointer */
3066 struct xfs_btree_block
*left
; /* left btree block */
3067 struct xfs_buf
*nbp
; /* new (root) buffer */
3068 struct xfs_btree_block
*new; /* new (root) btree block */
3069 int nptr
; /* new value for key index, 1 or 2 */
3070 struct xfs_buf
*rbp
; /* right buffer pointer */
3071 struct xfs_btree_block
*right
; /* right btree block */
3072 union xfs_btree_ptr rptr
;
3073 union xfs_btree_ptr lptr
;
3075 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
3076 XFS_BTREE_STATS_INC(cur
, newroot
);
3078 /* initialise our start point from the cursor */
3079 cur
->bc_ops
->init_ptr_from_cur(cur
, &rptr
);
3081 /* Allocate the new block. If we can't do it, we're toast. Give up. */
3082 error
= cur
->bc_ops
->alloc_block(cur
, &rptr
, &lptr
, stat
);
3087 XFS_BTREE_STATS_INC(cur
, alloc
);
3089 /* Set up the new block. */
3090 error
= xfs_btree_get_buf_block(cur
, &lptr
, 0, &new, &nbp
);
3094 /* Set the root in the holding structure increasing the level by 1. */
3095 cur
->bc_ops
->set_root(cur
, &lptr
, 1);
3098 * At the previous root level there are now two blocks: the old root,
3099 * and the new block generated when it was split. We don't know which
3100 * one the cursor is pointing at, so we set up variables "left" and
3101 * "right" for each case.
3103 block
= xfs_btree_get_block(cur
, cur
->bc_nlevels
- 1, &bp
);
3106 error
= xfs_btree_check_block(cur
, block
, cur
->bc_nlevels
- 1, bp
);
3111 xfs_btree_get_sibling(cur
, block
, &rptr
, XFS_BB_RIGHTSIB
);
3112 if (!xfs_btree_ptr_is_null(cur
, &rptr
)) {
3113 /* Our block is left, pick up the right block. */
3115 xfs_btree_buf_to_ptr(cur
, lbp
, &lptr
);
3117 error
= xfs_btree_read_buf_block(cur
, &rptr
, 0, &right
, &rbp
);
3123 /* Our block is right, pick up the left block. */
3125 xfs_btree_buf_to_ptr(cur
, rbp
, &rptr
);
3127 xfs_btree_get_sibling(cur
, right
, &lptr
, XFS_BB_LEFTSIB
);
3128 error
= xfs_btree_read_buf_block(cur
, &lptr
, 0, &left
, &lbp
);
3135 /* Fill in the new block's btree header and log it. */
3136 xfs_btree_init_block_cur(cur
, nbp
, cur
->bc_nlevels
, 2);
3137 xfs_btree_log_block(cur
, nbp
, XFS_BB_ALL_BITS
);
3138 ASSERT(!xfs_btree_ptr_is_null(cur
, &lptr
) &&
3139 !xfs_btree_ptr_is_null(cur
, &rptr
));
3141 /* Fill in the key data in the new root. */
3142 if (xfs_btree_get_level(left
) > 0) {
3144 * Get the keys for the left block's keys and put them directly
3145 * in the parent block. Do the same for the right block.
3147 xfs_btree_get_node_keys(cur
, left
,
3148 xfs_btree_key_addr(cur
, 1, new));
3149 xfs_btree_get_node_keys(cur
, right
,
3150 xfs_btree_key_addr(cur
, 2, new));
3153 * Get the keys for the left block's records and put them
3154 * directly in the parent block. Do the same for the right
3157 xfs_btree_get_leaf_keys(cur
, left
,
3158 xfs_btree_key_addr(cur
, 1, new));
3159 xfs_btree_get_leaf_keys(cur
, right
,
3160 xfs_btree_key_addr(cur
, 2, new));
3162 xfs_btree_log_keys(cur
, nbp
, 1, 2);
3164 /* Fill in the pointer data in the new root. */
3165 xfs_btree_copy_ptrs(cur
,
3166 xfs_btree_ptr_addr(cur
, 1, new), &lptr
, 1);
3167 xfs_btree_copy_ptrs(cur
,
3168 xfs_btree_ptr_addr(cur
, 2, new), &rptr
, 1);
3169 xfs_btree_log_ptrs(cur
, nbp
, 1, 2);
3171 /* Fix up the cursor. */
3172 xfs_btree_setbuf(cur
, cur
->bc_nlevels
, nbp
);
3173 cur
->bc_ptrs
[cur
->bc_nlevels
] = nptr
;
3175 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
3179 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
3182 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
3188 xfs_btree_make_block_unfull(
3189 struct xfs_btree_cur
*cur
, /* btree cursor */
3190 int level
, /* btree level */
3191 int numrecs
,/* # of recs in block */
3192 int *oindex
,/* old tree index */
3193 int *index
, /* new tree index */
3194 union xfs_btree_ptr
*nptr
, /* new btree ptr */
3195 struct xfs_btree_cur
**ncur
, /* new btree cursor */
3196 union xfs_btree_key
*key
, /* key of new block */
3201 if ((cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
) &&
3202 level
== cur
->bc_nlevels
- 1) {
3203 struct xfs_inode
*ip
= cur
->bc_private
.b
.ip
;
3205 if (numrecs
< cur
->bc_ops
->get_dmaxrecs(cur
, level
)) {
3206 /* A root block that can be made bigger. */
3207 xfs_iroot_realloc(ip
, 1, cur
->bc_private
.b
.whichfork
);
3210 /* A root block that needs replacing */
3213 error
= xfs_btree_new_iroot(cur
, &logflags
, stat
);
3214 if (error
|| *stat
== 0)
3217 xfs_trans_log_inode(cur
->bc_tp
, ip
, logflags
);
3223 /* First, try shifting an entry to the right neighbor. */
3224 error
= xfs_btree_rshift(cur
, level
, stat
);
3228 /* Next, try shifting an entry to the left neighbor. */
3229 error
= xfs_btree_lshift(cur
, level
, stat
);
3234 *oindex
= *index
= cur
->bc_ptrs
[level
];
3239 * Next, try splitting the current block in half.
3241 * If this works we have to re-set our variables because we
3242 * could be in a different block now.
3244 error
= xfs_btree_split(cur
, level
, nptr
, key
, ncur
, stat
);
3245 if (error
|| *stat
== 0)
3249 *index
= cur
->bc_ptrs
[level
];
3254 * Insert one record/level. Return information to the caller
3255 * allowing the next level up to proceed if necessary.
3259 struct xfs_btree_cur
*cur
, /* btree cursor */
3260 int level
, /* level to insert record at */
3261 union xfs_btree_ptr
*ptrp
, /* i/o: block number inserted */
3262 union xfs_btree_rec
*rec
, /* record to insert */
3263 union xfs_btree_key
*key
, /* i/o: block key for ptrp */
3264 struct xfs_btree_cur
**curp
, /* output: new cursor replacing cur */
3265 int *stat
) /* success/failure */
3267 struct xfs_btree_block
*block
; /* btree block */
3268 struct xfs_buf
*bp
; /* buffer for block */
3269 union xfs_btree_ptr nptr
; /* new block ptr */
3270 struct xfs_btree_cur
*ncur
; /* new btree cursor */
3271 union xfs_btree_key nkey
; /* new block key */
3272 union xfs_btree_key
*lkey
;
3273 int optr
; /* old key/record index */
3274 int ptr
; /* key/record index */
3275 int numrecs
;/* number of records */
3276 int error
; /* error return value */
3282 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
3283 XFS_BTREE_TRACE_ARGIPR(cur
, level
, *ptrp
, &rec
);
3289 * If we have an external root pointer, and we've made it to the
3290 * root level, allocate a new root block and we're done.
3292 if (!(cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
) &&
3293 (level
>= cur
->bc_nlevels
)) {
3294 error
= xfs_btree_new_root(cur
, stat
);
3295 xfs_btree_set_ptr_null(cur
, ptrp
);
3297 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
3301 /* If we're off the left edge, return failure. */
3302 ptr
= cur
->bc_ptrs
[level
];
3304 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
3311 XFS_BTREE_STATS_INC(cur
, insrec
);
3313 /* Get pointers to the btree buffer and block. */
3314 block
= xfs_btree_get_block(cur
, level
, &bp
);
3315 old_bn
= bp
? bp
->b_bn
: XFS_BUF_DADDR_NULL
;
3316 numrecs
= xfs_btree_get_numrecs(block
);
3319 error
= xfs_btree_check_block(cur
, block
, level
, bp
);
3323 /* Check that the new entry is being inserted in the right place. */
3324 if (ptr
<= numrecs
) {
3326 ASSERT(cur
->bc_ops
->recs_inorder(cur
, rec
,
3327 xfs_btree_rec_addr(cur
, ptr
, block
)));
3329 ASSERT(cur
->bc_ops
->keys_inorder(cur
, key
,
3330 xfs_btree_key_addr(cur
, ptr
, block
)));
3336 * If the block is full, we can't insert the new entry until we
3337 * make the block un-full.
3339 xfs_btree_set_ptr_null(cur
, &nptr
);
3340 if (numrecs
== cur
->bc_ops
->get_maxrecs(cur
, level
)) {
3341 error
= xfs_btree_make_block_unfull(cur
, level
, numrecs
,
3342 &optr
, &ptr
, &nptr
, &ncur
, lkey
, stat
);
3343 if (error
|| *stat
== 0)
3348 * The current block may have changed if the block was
3349 * previously full and we have just made space in it.
3351 block
= xfs_btree_get_block(cur
, level
, &bp
);
3352 numrecs
= xfs_btree_get_numrecs(block
);
3355 error
= xfs_btree_check_block(cur
, block
, level
, bp
);
3361 * At this point we know there's room for our new entry in the block
3362 * we're pointing at.
3364 XFS_BTREE_STATS_ADD(cur
, moves
, numrecs
- ptr
+ 1);
3367 /* It's a nonleaf. make a hole in the keys and ptrs */
3368 union xfs_btree_key
*kp
;
3369 union xfs_btree_ptr
*pp
;
3371 kp
= xfs_btree_key_addr(cur
, ptr
, block
);
3372 pp
= xfs_btree_ptr_addr(cur
, ptr
, block
);
3375 for (i
= numrecs
- ptr
; i
>= 0; i
--) {
3376 error
= xfs_btree_check_ptr(cur
, pp
, i
, level
);
3382 xfs_btree_shift_keys(cur
, kp
, 1, numrecs
- ptr
+ 1);
3383 xfs_btree_shift_ptrs(cur
, pp
, 1, numrecs
- ptr
+ 1);
3386 error
= xfs_btree_check_ptr(cur
, ptrp
, 0, level
);
3391 /* Now put the new data in, bump numrecs and log it. */
3392 xfs_btree_copy_keys(cur
, kp
, key
, 1);
3393 xfs_btree_copy_ptrs(cur
, pp
, ptrp
, 1);
3395 xfs_btree_set_numrecs(block
, numrecs
);
3396 xfs_btree_log_ptrs(cur
, bp
, ptr
, numrecs
);
3397 xfs_btree_log_keys(cur
, bp
, ptr
, numrecs
);
3399 if (ptr
< numrecs
) {
3400 ASSERT(cur
->bc_ops
->keys_inorder(cur
, kp
,
3401 xfs_btree_key_addr(cur
, ptr
+ 1, block
)));
3405 /* It's a leaf. make a hole in the records */
3406 union xfs_btree_rec
*rp
;
3408 rp
= xfs_btree_rec_addr(cur
, ptr
, block
);
3410 xfs_btree_shift_recs(cur
, rp
, 1, numrecs
- ptr
+ 1);
3412 /* Now put the new data in, bump numrecs and log it. */
3413 xfs_btree_copy_recs(cur
, rp
, rec
, 1);
3414 xfs_btree_set_numrecs(block
, ++numrecs
);
3415 xfs_btree_log_recs(cur
, bp
, ptr
, numrecs
);
3417 if (ptr
< numrecs
) {
3418 ASSERT(cur
->bc_ops
->recs_inorder(cur
, rp
,
3419 xfs_btree_rec_addr(cur
, ptr
+ 1, block
)));
3424 /* Log the new number of records in the btree header. */
3425 xfs_btree_log_block(cur
, bp
, XFS_BB_NUMRECS
);
3428 * If we just inserted into a new tree block, we have to
3429 * recalculate nkey here because nkey is out of date.
3431 * Otherwise we're just updating an existing block (having shoved
3432 * some records into the new tree block), so use the regular key
3435 if (bp
&& bp
->b_bn
!= old_bn
) {
3436 xfs_btree_get_keys(cur
, block
, lkey
);
3437 } else if (xfs_btree_needs_key_update(cur
, optr
)) {
3438 error
= xfs_btree_update_keys(cur
, level
);
3444 * If we are tracking the last record in the tree and
3445 * we are at the far right edge of the tree, update it.
3447 if (xfs_btree_is_lastrec(cur
, block
, level
)) {
3448 cur
->bc_ops
->update_lastrec(cur
, block
, rec
,
3449 ptr
, LASTREC_INSREC
);
3453 * Return the new block number, if any.
3454 * If there is one, give back a record value and a cursor too.
3457 if (!xfs_btree_ptr_is_null(cur
, &nptr
)) {
3458 xfs_btree_copy_keys(cur
, key
, lkey
, 1);
3462 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
3467 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
3472 * Insert the record at the point referenced by cur.
3474 * A multi-level split of the tree on insert will invalidate the original
3475 * cursor. All callers of this function should assume that the cursor is
3476 * no longer valid and revalidate it.
3480 struct xfs_btree_cur
*cur
,
3483 int error
; /* error return value */
3484 int i
; /* result value, 0 for failure */
3485 int level
; /* current level number in btree */
3486 union xfs_btree_ptr nptr
; /* new block number (split result) */
3487 struct xfs_btree_cur
*ncur
; /* new cursor (split result) */
3488 struct xfs_btree_cur
*pcur
; /* previous level's cursor */
3489 union xfs_btree_key bkey
; /* key of block to insert */
3490 union xfs_btree_key
*key
;
3491 union xfs_btree_rec rec
; /* record to insert */
3498 xfs_btree_set_ptr_null(cur
, &nptr
);
3500 /* Make a key out of the record data to be inserted, and save it. */
3501 cur
->bc_ops
->init_rec_from_cur(cur
, &rec
);
3502 cur
->bc_ops
->init_key_from_rec(key
, &rec
);
3505 * Loop going up the tree, starting at the leaf level.
3506 * Stop when we don't get a split block, that must mean that
3507 * the insert is finished with this level.
3511 * Insert nrec/nptr into this level of the tree.
3512 * Note if we fail, nptr will be null.
3514 error
= xfs_btree_insrec(pcur
, level
, &nptr
, &rec
, key
,
3518 xfs_btree_del_cursor(pcur
, XFS_BTREE_ERROR
);
3522 XFS_WANT_CORRUPTED_GOTO(cur
->bc_mp
, i
== 1, error0
);
3526 * See if the cursor we just used is trash.
3527 * Can't trash the caller's cursor, but otherwise we should
3528 * if ncur is a new cursor or we're about to be done.
3531 (ncur
|| xfs_btree_ptr_is_null(cur
, &nptr
))) {
3532 /* Save the state from the cursor before we trash it */
3533 if (cur
->bc_ops
->update_cursor
)
3534 cur
->bc_ops
->update_cursor(pcur
, cur
);
3535 cur
->bc_nlevels
= pcur
->bc_nlevels
;
3536 xfs_btree_del_cursor(pcur
, XFS_BTREE_NOERROR
);
3538 /* If we got a new cursor, switch to it. */
3543 } while (!xfs_btree_ptr_is_null(cur
, &nptr
));
3545 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
3549 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
3554 * Try to merge a non-leaf block back into the inode root.
3556 * Note: the killroot names comes from the fact that we're effectively
3557 * killing the old root block. But because we can't just delete the
3558 * inode we have to copy the single block it was pointing to into the
3562 xfs_btree_kill_iroot(
3563 struct xfs_btree_cur
*cur
)
3565 int whichfork
= cur
->bc_private
.b
.whichfork
;
3566 struct xfs_inode
*ip
= cur
->bc_private
.b
.ip
;
3567 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
3568 struct xfs_btree_block
*block
;
3569 struct xfs_btree_block
*cblock
;
3570 union xfs_btree_key
*kp
;
3571 union xfs_btree_key
*ckp
;
3572 union xfs_btree_ptr
*pp
;
3573 union xfs_btree_ptr
*cpp
;
3574 struct xfs_buf
*cbp
;
3580 union xfs_btree_ptr ptr
;
3584 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
3586 ASSERT(cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
);
3587 ASSERT(cur
->bc_nlevels
> 1);
3590 * Don't deal with the root block needs to be a leaf case.
3591 * We're just going to turn the thing back into extents anyway.
3593 level
= cur
->bc_nlevels
- 1;
3598 * Give up if the root has multiple children.
3600 block
= xfs_btree_get_iroot(cur
);
3601 if (xfs_btree_get_numrecs(block
) != 1)
3604 cblock
= xfs_btree_get_block(cur
, level
- 1, &cbp
);
3605 numrecs
= xfs_btree_get_numrecs(cblock
);
3608 * Only do this if the next level will fit.
3609 * Then the data must be copied up to the inode,
3610 * instead of freeing the root you free the next level.
3612 if (numrecs
> cur
->bc_ops
->get_dmaxrecs(cur
, level
))
3615 XFS_BTREE_STATS_INC(cur
, killroot
);
3618 xfs_btree_get_sibling(cur
, block
, &ptr
, XFS_BB_LEFTSIB
);
3619 ASSERT(xfs_btree_ptr_is_null(cur
, &ptr
));
3620 xfs_btree_get_sibling(cur
, block
, &ptr
, XFS_BB_RIGHTSIB
);
3621 ASSERT(xfs_btree_ptr_is_null(cur
, &ptr
));
3624 index
= numrecs
- cur
->bc_ops
->get_maxrecs(cur
, level
);
3626 xfs_iroot_realloc(cur
->bc_private
.b
.ip
, index
,
3627 cur
->bc_private
.b
.whichfork
);
3628 block
= ifp
->if_broot
;
3631 be16_add_cpu(&block
->bb_numrecs
, index
);
3632 ASSERT(block
->bb_numrecs
== cblock
->bb_numrecs
);
3634 kp
= xfs_btree_key_addr(cur
, 1, block
);
3635 ckp
= xfs_btree_key_addr(cur
, 1, cblock
);
3636 xfs_btree_copy_keys(cur
, kp
, ckp
, numrecs
);
3638 pp
= xfs_btree_ptr_addr(cur
, 1, block
);
3639 cpp
= xfs_btree_ptr_addr(cur
, 1, cblock
);
3641 for (i
= 0; i
< numrecs
; i
++) {
3642 error
= xfs_btree_check_ptr(cur
, cpp
, i
, level
- 1);
3644 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
3649 xfs_btree_copy_ptrs(cur
, pp
, cpp
, numrecs
);
3651 error
= xfs_btree_free_block(cur
, cbp
);
3653 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
3657 cur
->bc_bufs
[level
- 1] = NULL
;
3658 be16_add_cpu(&block
->bb_level
, -1);
3659 xfs_trans_log_inode(cur
->bc_tp
, ip
,
3660 XFS_ILOG_CORE
| xfs_ilog_fbroot(cur
->bc_private
.b
.whichfork
));
3663 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
3668 * Kill the current root node, and replace it with it's only child node.
3671 xfs_btree_kill_root(
3672 struct xfs_btree_cur
*cur
,
3675 union xfs_btree_ptr
*newroot
)
3679 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
3680 XFS_BTREE_STATS_INC(cur
, killroot
);
3683 * Update the root pointer, decreasing the level by 1 and then
3684 * free the old root.
3686 cur
->bc_ops
->set_root(cur
, newroot
, -1);
3688 error
= xfs_btree_free_block(cur
, bp
);
3690 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
3694 cur
->bc_bufs
[level
] = NULL
;
3695 cur
->bc_ra
[level
] = 0;
3698 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
3703 xfs_btree_dec_cursor(
3704 struct xfs_btree_cur
*cur
,
3712 error
= xfs_btree_decrement(cur
, level
, &i
);
3717 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
3723 * Single level of the btree record deletion routine.
3724 * Delete record pointed to by cur/level.
3725 * Remove the record from its block then rebalance the tree.
3726 * Return 0 for error, 1 for done, 2 to go on to the next level.
3728 STATIC
int /* error */
3730 struct xfs_btree_cur
*cur
, /* btree cursor */
3731 int level
, /* level removing record from */
3732 int *stat
) /* fail/done/go-on */
3734 struct xfs_btree_block
*block
; /* btree block */
3735 union xfs_btree_ptr cptr
; /* current block ptr */
3736 struct xfs_buf
*bp
; /* buffer for block */
3737 int error
; /* error return value */
3738 int i
; /* loop counter */
3739 union xfs_btree_ptr lptr
; /* left sibling block ptr */
3740 struct xfs_buf
*lbp
; /* left buffer pointer */
3741 struct xfs_btree_block
*left
; /* left btree block */
3742 int lrecs
= 0; /* left record count */
3743 int ptr
; /* key/record index */
3744 union xfs_btree_ptr rptr
; /* right sibling block ptr */
3745 struct xfs_buf
*rbp
; /* right buffer pointer */
3746 struct xfs_btree_block
*right
; /* right btree block */
3747 struct xfs_btree_block
*rrblock
; /* right-right btree block */
3748 struct xfs_buf
*rrbp
; /* right-right buffer pointer */
3749 int rrecs
= 0; /* right record count */
3750 struct xfs_btree_cur
*tcur
; /* temporary btree cursor */
3751 int numrecs
; /* temporary numrec count */
3753 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
3754 XFS_BTREE_TRACE_ARGI(cur
, level
);
3758 /* Get the index of the entry being deleted, check for nothing there. */
3759 ptr
= cur
->bc_ptrs
[level
];
3761 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
3766 /* Get the buffer & block containing the record or key/ptr. */
3767 block
= xfs_btree_get_block(cur
, level
, &bp
);
3768 numrecs
= xfs_btree_get_numrecs(block
);
3771 error
= xfs_btree_check_block(cur
, block
, level
, bp
);
3776 /* Fail if we're off the end of the block. */
3777 if (ptr
> numrecs
) {
3778 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
3783 XFS_BTREE_STATS_INC(cur
, delrec
);
3784 XFS_BTREE_STATS_ADD(cur
, moves
, numrecs
- ptr
);
3786 /* Excise the entries being deleted. */
3788 /* It's a nonleaf. operate on keys and ptrs */
3789 union xfs_btree_key
*lkp
;
3790 union xfs_btree_ptr
*lpp
;
3792 lkp
= xfs_btree_key_addr(cur
, ptr
+ 1, block
);
3793 lpp
= xfs_btree_ptr_addr(cur
, ptr
+ 1, block
);
3796 for (i
= 0; i
< numrecs
- ptr
; i
++) {
3797 error
= xfs_btree_check_ptr(cur
, lpp
, i
, level
);
3803 if (ptr
< numrecs
) {
3804 xfs_btree_shift_keys(cur
, lkp
, -1, numrecs
- ptr
);
3805 xfs_btree_shift_ptrs(cur
, lpp
, -1, numrecs
- ptr
);
3806 xfs_btree_log_keys(cur
, bp
, ptr
, numrecs
- 1);
3807 xfs_btree_log_ptrs(cur
, bp
, ptr
, numrecs
- 1);
3810 /* It's a leaf. operate on records */
3811 if (ptr
< numrecs
) {
3812 xfs_btree_shift_recs(cur
,
3813 xfs_btree_rec_addr(cur
, ptr
+ 1, block
),
3815 xfs_btree_log_recs(cur
, bp
, ptr
, numrecs
- 1);
3820 * Decrement and log the number of entries in the block.
3822 xfs_btree_set_numrecs(block
, --numrecs
);
3823 xfs_btree_log_block(cur
, bp
, XFS_BB_NUMRECS
);
3826 * If we are tracking the last record in the tree and
3827 * we are at the far right edge of the tree, update it.
3829 if (xfs_btree_is_lastrec(cur
, block
, level
)) {
3830 cur
->bc_ops
->update_lastrec(cur
, block
, NULL
,
3831 ptr
, LASTREC_DELREC
);
3835 * We're at the root level. First, shrink the root block in-memory.
3836 * Try to get rid of the next level down. If we can't then there's
3837 * nothing left to do.
3839 if (level
== cur
->bc_nlevels
- 1) {
3840 if (cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
) {
3841 xfs_iroot_realloc(cur
->bc_private
.b
.ip
, -1,
3842 cur
->bc_private
.b
.whichfork
);
3844 error
= xfs_btree_kill_iroot(cur
);
3848 error
= xfs_btree_dec_cursor(cur
, level
, stat
);
3856 * If this is the root level, and there's only one entry left,
3857 * and it's NOT the leaf level, then we can get rid of this
3860 if (numrecs
== 1 && level
> 0) {
3861 union xfs_btree_ptr
*pp
;
3863 * pp is still set to the first pointer in the block.
3864 * Make it the new root of the btree.
3866 pp
= xfs_btree_ptr_addr(cur
, 1, block
);
3867 error
= xfs_btree_kill_root(cur
, bp
, level
, pp
);
3870 } else if (level
> 0) {
3871 error
= xfs_btree_dec_cursor(cur
, level
, stat
);
3880 * If we deleted the leftmost entry in the block, update the
3881 * key values above us in the tree.
3883 if (xfs_btree_needs_key_update(cur
, ptr
)) {
3884 error
= xfs_btree_update_keys(cur
, level
);
3890 * If the number of records remaining in the block is at least
3891 * the minimum, we're done.
3893 if (numrecs
>= cur
->bc_ops
->get_minrecs(cur
, level
)) {
3894 error
= xfs_btree_dec_cursor(cur
, level
, stat
);
3901 * Otherwise, we have to move some records around to keep the
3902 * tree balanced. Look at the left and right sibling blocks to
3903 * see if we can re-balance by moving only one record.
3905 xfs_btree_get_sibling(cur
, block
, &rptr
, XFS_BB_RIGHTSIB
);
3906 xfs_btree_get_sibling(cur
, block
, &lptr
, XFS_BB_LEFTSIB
);
3908 if (cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
) {
3910 * One child of root, need to get a chance to copy its contents
3911 * into the root and delete it. Can't go up to next level,
3912 * there's nothing to delete there.
3914 if (xfs_btree_ptr_is_null(cur
, &rptr
) &&
3915 xfs_btree_ptr_is_null(cur
, &lptr
) &&
3916 level
== cur
->bc_nlevels
- 2) {
3917 error
= xfs_btree_kill_iroot(cur
);
3919 error
= xfs_btree_dec_cursor(cur
, level
, stat
);
3926 ASSERT(!xfs_btree_ptr_is_null(cur
, &rptr
) ||
3927 !xfs_btree_ptr_is_null(cur
, &lptr
));
3930 * Duplicate the cursor so our btree manipulations here won't
3931 * disrupt the next level up.
3933 error
= xfs_btree_dup_cursor(cur
, &tcur
);
3938 * If there's a right sibling, see if it's ok to shift an entry
3941 if (!xfs_btree_ptr_is_null(cur
, &rptr
)) {
3943 * Move the temp cursor to the last entry in the next block.
3944 * Actually any entry but the first would suffice.
3946 i
= xfs_btree_lastrec(tcur
, level
);
3947 XFS_WANT_CORRUPTED_GOTO(cur
->bc_mp
, i
== 1, error0
);
3949 error
= xfs_btree_increment(tcur
, level
, &i
);
3952 XFS_WANT_CORRUPTED_GOTO(cur
->bc_mp
, i
== 1, error0
);
3954 i
= xfs_btree_lastrec(tcur
, level
);
3955 XFS_WANT_CORRUPTED_GOTO(cur
->bc_mp
, i
== 1, error0
);
3957 /* Grab a pointer to the block. */
3958 right
= xfs_btree_get_block(tcur
, level
, &rbp
);
3960 error
= xfs_btree_check_block(tcur
, right
, level
, rbp
);
3964 /* Grab the current block number, for future use. */
3965 xfs_btree_get_sibling(tcur
, right
, &cptr
, XFS_BB_LEFTSIB
);
3968 * If right block is full enough so that removing one entry
3969 * won't make it too empty, and left-shifting an entry out
3970 * of right to us works, we're done.
3972 if (xfs_btree_get_numrecs(right
) - 1 >=
3973 cur
->bc_ops
->get_minrecs(tcur
, level
)) {
3974 error
= xfs_btree_lshift(tcur
, level
, &i
);
3978 ASSERT(xfs_btree_get_numrecs(block
) >=
3979 cur
->bc_ops
->get_minrecs(tcur
, level
));
3981 xfs_btree_del_cursor(tcur
, XFS_BTREE_NOERROR
);
3984 error
= xfs_btree_dec_cursor(cur
, level
, stat
);
3992 * Otherwise, grab the number of records in right for
3993 * future reference, and fix up the temp cursor to point
3994 * to our block again (last record).
3996 rrecs
= xfs_btree_get_numrecs(right
);
3997 if (!xfs_btree_ptr_is_null(cur
, &lptr
)) {
3998 i
= xfs_btree_firstrec(tcur
, level
);
3999 XFS_WANT_CORRUPTED_GOTO(cur
->bc_mp
, i
== 1, error0
);
4001 error
= xfs_btree_decrement(tcur
, level
, &i
);
4004 XFS_WANT_CORRUPTED_GOTO(cur
->bc_mp
, i
== 1, error0
);
4009 * If there's a left sibling, see if it's ok to shift an entry
4012 if (!xfs_btree_ptr_is_null(cur
, &lptr
)) {
4014 * Move the temp cursor to the first entry in the
4017 i
= xfs_btree_firstrec(tcur
, level
);
4018 XFS_WANT_CORRUPTED_GOTO(cur
->bc_mp
, i
== 1, error0
);
4020 error
= xfs_btree_decrement(tcur
, level
, &i
);
4023 i
= xfs_btree_firstrec(tcur
, level
);
4024 XFS_WANT_CORRUPTED_GOTO(cur
->bc_mp
, i
== 1, error0
);
4026 /* Grab a pointer to the block. */
4027 left
= xfs_btree_get_block(tcur
, level
, &lbp
);
4029 error
= xfs_btree_check_block(cur
, left
, level
, lbp
);
4033 /* Grab the current block number, for future use. */
4034 xfs_btree_get_sibling(tcur
, left
, &cptr
, XFS_BB_RIGHTSIB
);
4037 * If left block is full enough so that removing one entry
4038 * won't make it too empty, and right-shifting an entry out
4039 * of left to us works, we're done.
4041 if (xfs_btree_get_numrecs(left
) - 1 >=
4042 cur
->bc_ops
->get_minrecs(tcur
, level
)) {
4043 error
= xfs_btree_rshift(tcur
, level
, &i
);
4047 ASSERT(xfs_btree_get_numrecs(block
) >=
4048 cur
->bc_ops
->get_minrecs(tcur
, level
));
4049 xfs_btree_del_cursor(tcur
, XFS_BTREE_NOERROR
);
4053 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
4060 * Otherwise, grab the number of records in right for
4063 lrecs
= xfs_btree_get_numrecs(left
);
4066 /* Delete the temp cursor, we're done with it. */
4067 xfs_btree_del_cursor(tcur
, XFS_BTREE_NOERROR
);
4070 /* If here, we need to do a join to keep the tree balanced. */
4071 ASSERT(!xfs_btree_ptr_is_null(cur
, &cptr
));
4073 if (!xfs_btree_ptr_is_null(cur
, &lptr
) &&
4074 lrecs
+ xfs_btree_get_numrecs(block
) <=
4075 cur
->bc_ops
->get_maxrecs(cur
, level
)) {
4077 * Set "right" to be the starting block,
4078 * "left" to be the left neighbor.
4083 error
= xfs_btree_read_buf_block(cur
, &lptr
, 0, &left
, &lbp
);
4088 * If that won't work, see if we can join with the right neighbor block.
4090 } else if (!xfs_btree_ptr_is_null(cur
, &rptr
) &&
4091 rrecs
+ xfs_btree_get_numrecs(block
) <=
4092 cur
->bc_ops
->get_maxrecs(cur
, level
)) {
4094 * Set "left" to be the starting block,
4095 * "right" to be the right neighbor.
4100 error
= xfs_btree_read_buf_block(cur
, &rptr
, 0, &right
, &rbp
);
4105 * Otherwise, we can't fix the imbalance.
4106 * Just return. This is probably a logic error, but it's not fatal.
4109 error
= xfs_btree_dec_cursor(cur
, level
, stat
);
4115 rrecs
= xfs_btree_get_numrecs(right
);
4116 lrecs
= xfs_btree_get_numrecs(left
);
4119 * We're now going to join "left" and "right" by moving all the stuff
4120 * in "right" to "left" and deleting "right".
4122 XFS_BTREE_STATS_ADD(cur
, moves
, rrecs
);
4124 /* It's a non-leaf. Move keys and pointers. */
4125 union xfs_btree_key
*lkp
; /* left btree key */
4126 union xfs_btree_ptr
*lpp
; /* left address pointer */
4127 union xfs_btree_key
*rkp
; /* right btree key */
4128 union xfs_btree_ptr
*rpp
; /* right address pointer */
4130 lkp
= xfs_btree_key_addr(cur
, lrecs
+ 1, left
);
4131 lpp
= xfs_btree_ptr_addr(cur
, lrecs
+ 1, left
);
4132 rkp
= xfs_btree_key_addr(cur
, 1, right
);
4133 rpp
= xfs_btree_ptr_addr(cur
, 1, right
);
4135 for (i
= 1; i
< rrecs
; i
++) {
4136 error
= xfs_btree_check_ptr(cur
, rpp
, i
, level
);
4141 xfs_btree_copy_keys(cur
, lkp
, rkp
, rrecs
);
4142 xfs_btree_copy_ptrs(cur
, lpp
, rpp
, rrecs
);
4144 xfs_btree_log_keys(cur
, lbp
, lrecs
+ 1, lrecs
+ rrecs
);
4145 xfs_btree_log_ptrs(cur
, lbp
, lrecs
+ 1, lrecs
+ rrecs
);
4147 /* It's a leaf. Move records. */
4148 union xfs_btree_rec
*lrp
; /* left record pointer */
4149 union xfs_btree_rec
*rrp
; /* right record pointer */
4151 lrp
= xfs_btree_rec_addr(cur
, lrecs
+ 1, left
);
4152 rrp
= xfs_btree_rec_addr(cur
, 1, right
);
4154 xfs_btree_copy_recs(cur
, lrp
, rrp
, rrecs
);
4155 xfs_btree_log_recs(cur
, lbp
, lrecs
+ 1, lrecs
+ rrecs
);
4158 XFS_BTREE_STATS_INC(cur
, join
);
4161 * Fix up the number of records and right block pointer in the
4162 * surviving block, and log it.
4164 xfs_btree_set_numrecs(left
, lrecs
+ rrecs
);
4165 xfs_btree_get_sibling(cur
, right
, &cptr
, XFS_BB_RIGHTSIB
),
4166 xfs_btree_set_sibling(cur
, left
, &cptr
, XFS_BB_RIGHTSIB
);
4167 xfs_btree_log_block(cur
, lbp
, XFS_BB_NUMRECS
| XFS_BB_RIGHTSIB
);
4169 /* If there is a right sibling, point it to the remaining block. */
4170 xfs_btree_get_sibling(cur
, left
, &cptr
, XFS_BB_RIGHTSIB
);
4171 if (!xfs_btree_ptr_is_null(cur
, &cptr
)) {
4172 error
= xfs_btree_read_buf_block(cur
, &cptr
, 0, &rrblock
, &rrbp
);
4175 xfs_btree_set_sibling(cur
, rrblock
, &lptr
, XFS_BB_LEFTSIB
);
4176 xfs_btree_log_block(cur
, rrbp
, XFS_BB_LEFTSIB
);
4179 /* Free the deleted block. */
4180 error
= xfs_btree_free_block(cur
, rbp
);
4185 * If we joined with the left neighbor, set the buffer in the
4186 * cursor to the left block, and fix up the index.
4189 cur
->bc_bufs
[level
] = lbp
;
4190 cur
->bc_ptrs
[level
] += lrecs
;
4191 cur
->bc_ra
[level
] = 0;
4194 * If we joined with the right neighbor and there's a level above
4195 * us, increment the cursor at that level.
4197 else if ((cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
) ||
4198 (level
+ 1 < cur
->bc_nlevels
)) {
4199 error
= xfs_btree_increment(cur
, level
+ 1, &i
);
4205 * Readjust the ptr at this level if it's not a leaf, since it's
4206 * still pointing at the deletion point, which makes the cursor
4207 * inconsistent. If this makes the ptr 0, the caller fixes it up.
4208 * We can't use decrement because it would change the next level up.
4211 cur
->bc_ptrs
[level
]--;
4214 * We combined blocks, so we have to update the parent keys if the
4215 * btree supports overlapped intervals. However, bc_ptrs[level + 1]
4216 * points to the old block so that the caller knows which record to
4217 * delete. Therefore, the caller must be savvy enough to call updkeys
4218 * for us if we return stat == 2. The other exit points from this
4219 * function don't require deletions further up the tree, so they can
4220 * call updkeys directly.
4223 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
4224 /* Return value means the next level up has something to do. */
4229 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
4231 xfs_btree_del_cursor(tcur
, XFS_BTREE_ERROR
);
4236 * Delete the record pointed to by cur.
4237 * The cursor refers to the place where the record was (could be inserted)
4238 * when the operation returns.
4242 struct xfs_btree_cur
*cur
,
4243 int *stat
) /* success/failure */
4245 int error
; /* error return value */
4248 bool joined
= false;
4250 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
4253 * Go up the tree, starting at leaf level.
4255 * If 2 is returned then a join was done; go to the next level.
4256 * Otherwise we are done.
4258 for (level
= 0, i
= 2; i
== 2; level
++) {
4259 error
= xfs_btree_delrec(cur
, level
, &i
);
4267 * If we combined blocks as part of deleting the record, delrec won't
4268 * have updated the parent high keys so we have to do that here.
4270 if (joined
&& (cur
->bc_flags
& XFS_BTREE_OVERLAPPING
)) {
4271 error
= xfs_btree_updkeys_force(cur
, 0);
4277 for (level
= 1; level
< cur
->bc_nlevels
; level
++) {
4278 if (cur
->bc_ptrs
[level
] == 0) {
4279 error
= xfs_btree_decrement(cur
, level
, &i
);
4287 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
4291 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
4296 * Get the data from the pointed-to record.
4300 struct xfs_btree_cur
*cur
, /* btree cursor */
4301 union xfs_btree_rec
**recp
, /* output: btree record */
4302 int *stat
) /* output: success/failure */
4304 struct xfs_btree_block
*block
; /* btree block */
4305 struct xfs_buf
*bp
; /* buffer pointer */
4306 int ptr
; /* record number */
4308 int error
; /* error return value */
4311 ptr
= cur
->bc_ptrs
[0];
4312 block
= xfs_btree_get_block(cur
, 0, &bp
);
4315 error
= xfs_btree_check_block(cur
, block
, 0, bp
);
4321 * Off the right end or left end, return failure.
4323 if (ptr
> xfs_btree_get_numrecs(block
) || ptr
<= 0) {
4329 * Point to the record and extract its data.
4331 *recp
= xfs_btree_rec_addr(cur
, ptr
, block
);
4336 /* Visit a block in a btree. */
4338 xfs_btree_visit_block(
4339 struct xfs_btree_cur
*cur
,
4341 xfs_btree_visit_blocks_fn fn
,
4344 struct xfs_btree_block
*block
;
4346 union xfs_btree_ptr rptr
;
4349 /* do right sibling readahead */
4350 xfs_btree_readahead(cur
, level
, XFS_BTCUR_RIGHTRA
);
4351 block
= xfs_btree_get_block(cur
, level
, &bp
);
4353 /* process the block */
4354 error
= fn(cur
, level
, data
);
4358 /* now read rh sibling block for next iteration */
4359 xfs_btree_get_sibling(cur
, block
, &rptr
, XFS_BB_RIGHTSIB
);
4360 if (xfs_btree_ptr_is_null(cur
, &rptr
))
4363 return xfs_btree_lookup_get_block(cur
, level
, &rptr
, &block
);
4367 /* Visit every block in a btree. */
4369 xfs_btree_visit_blocks(
4370 struct xfs_btree_cur
*cur
,
4371 xfs_btree_visit_blocks_fn fn
,
4374 union xfs_btree_ptr lptr
;
4376 struct xfs_btree_block
*block
= NULL
;
4379 cur
->bc_ops
->init_ptr_from_cur(cur
, &lptr
);
4381 /* for each level */
4382 for (level
= cur
->bc_nlevels
- 1; level
>= 0; level
--) {
4383 /* grab the left hand block */
4384 error
= xfs_btree_lookup_get_block(cur
, level
, &lptr
, &block
);
4388 /* readahead the left most block for the next level down */
4390 union xfs_btree_ptr
*ptr
;
4392 ptr
= xfs_btree_ptr_addr(cur
, 1, block
);
4393 xfs_btree_readahead_ptr(cur
, ptr
, 1);
4395 /* save for the next iteration of the loop */
4396 xfs_btree_copy_ptrs(cur
, &lptr
, ptr
, 1);
4399 /* for each buffer in the level */
4401 error
= xfs_btree_visit_block(cur
, level
, fn
, data
);
4404 if (error
!= -ENOENT
)
4412 * Change the owner of a btree.
4414 * The mechanism we use here is ordered buffer logging. Because we don't know
4415 * how many buffers were are going to need to modify, we don't really want to
4416 * have to make transaction reservations for the worst case of every buffer in a
4417 * full size btree as that may be more space that we can fit in the log....
4419 * We do the btree walk in the most optimal manner possible - we have sibling
4420 * pointers so we can just walk all the blocks on each level from left to right
4421 * in a single pass, and then move to the next level and do the same. We can
4422 * also do readahead on the sibling pointers to get IO moving more quickly,
4423 * though for slow disks this is unlikely to make much difference to performance
4424 * as the amount of CPU work we have to do before moving to the next block is
4427 * For each btree block that we load, modify the owner appropriately, set the
4428 * buffer as an ordered buffer and log it appropriately. We need to ensure that
4429 * we mark the region we change dirty so that if the buffer is relogged in
4430 * a subsequent transaction the changes we make here as an ordered buffer are
4431 * correctly relogged in that transaction. If we are in recovery context, then
4432 * just queue the modified buffer as delayed write buffer so the transaction
4433 * recovery completion writes the changes to disk.
4435 struct xfs_btree_block_change_owner_info
{
4437 struct list_head
*buffer_list
;
4441 xfs_btree_block_change_owner(
4442 struct xfs_btree_cur
*cur
,
4446 struct xfs_btree_block_change_owner_info
*bbcoi
= data
;
4447 struct xfs_btree_block
*block
;
4450 /* modify the owner */
4451 block
= xfs_btree_get_block(cur
, level
, &bp
);
4452 if (cur
->bc_flags
& XFS_BTREE_LONG_PTRS
)
4453 block
->bb_u
.l
.bb_owner
= cpu_to_be64(bbcoi
->new_owner
);
4455 block
->bb_u
.s
.bb_owner
= cpu_to_be32(bbcoi
->new_owner
);
4458 * If the block is a root block hosted in an inode, we might not have a
4459 * buffer pointer here and we shouldn't attempt to log the change as the
4460 * information is already held in the inode and discarded when the root
4461 * block is formatted into the on-disk inode fork. We still change it,
4462 * though, so everything is consistent in memory.
4466 xfs_trans_ordered_buf(cur
->bc_tp
, bp
);
4467 xfs_btree_log_block(cur
, bp
, XFS_BB_OWNER
);
4469 xfs_buf_delwri_queue(bp
, bbcoi
->buffer_list
);
4472 ASSERT(cur
->bc_flags
& XFS_BTREE_ROOT_IN_INODE
);
4473 ASSERT(level
== cur
->bc_nlevels
- 1);
4480 xfs_btree_change_owner(
4481 struct xfs_btree_cur
*cur
,
4483 struct list_head
*buffer_list
)
4485 struct xfs_btree_block_change_owner_info bbcoi
;
4487 bbcoi
.new_owner
= new_owner
;
4488 bbcoi
.buffer_list
= buffer_list
;
4490 return xfs_btree_visit_blocks(cur
, xfs_btree_block_change_owner
,
4495 * xfs_btree_sblock_v5hdr_verify() -- verify the v5 fields of a short-format
4498 * @bp: buffer containing the btree block
4499 * @max_recs: pointer to the m_*_mxr max records field in the xfs mount
4500 * @pag_max_level: pointer to the per-ag max level field
4503 xfs_btree_sblock_v5hdr_verify(
4506 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
4507 struct xfs_btree_block
*block
= XFS_BUF_TO_BLOCK(bp
);
4508 struct xfs_perag
*pag
= bp
->b_pag
;
4510 if (!xfs_sb_version_hascrc(&mp
->m_sb
))
4512 if (!uuid_equal(&block
->bb_u
.s
.bb_uuid
, &mp
->m_sb
.sb_meta_uuid
))
4514 if (block
->bb_u
.s
.bb_blkno
!= cpu_to_be64(bp
->b_bn
))
4516 if (pag
&& be32_to_cpu(block
->bb_u
.s
.bb_owner
) != pag
->pag_agno
)
4522 * xfs_btree_sblock_verify() -- verify a short-format btree block
4524 * @bp: buffer containing the btree block
4525 * @max_recs: maximum records allowed in this btree node
4528 xfs_btree_sblock_verify(
4530 unsigned int max_recs
)
4532 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
4533 struct xfs_btree_block
*block
= XFS_BUF_TO_BLOCK(bp
);
4535 /* numrecs verification */
4536 if (be16_to_cpu(block
->bb_numrecs
) > max_recs
)
4539 /* sibling pointer verification */
4540 if (!block
->bb_u
.s
.bb_leftsib
||
4541 (be32_to_cpu(block
->bb_u
.s
.bb_leftsib
) >= mp
->m_sb
.sb_agblocks
&&
4542 block
->bb_u
.s
.bb_leftsib
!= cpu_to_be32(NULLAGBLOCK
)))
4544 if (!block
->bb_u
.s
.bb_rightsib
||
4545 (be32_to_cpu(block
->bb_u
.s
.bb_rightsib
) >= mp
->m_sb
.sb_agblocks
&&
4546 block
->bb_u
.s
.bb_rightsib
!= cpu_to_be32(NULLAGBLOCK
)))
4553 * Calculate the number of btree levels needed to store a given number of
4554 * records in a short-format btree.
4557 xfs_btree_compute_maxlevels(
4558 struct xfs_mount
*mp
,
4563 unsigned long maxblocks
;
4565 maxblocks
= (len
+ limits
[0] - 1) / limits
[0];
4566 for (level
= 1; maxblocks
> 1; level
++)
4567 maxblocks
= (maxblocks
+ limits
[1] - 1) / limits
[1];
4572 * Query a regular btree for all records overlapping a given interval.
4573 * Start with a LE lookup of the key of low_rec and return all records
4574 * until we find a record with a key greater than the key of high_rec.
4577 xfs_btree_simple_query_range(
4578 struct xfs_btree_cur
*cur
,
4579 union xfs_btree_key
*low_key
,
4580 union xfs_btree_key
*high_key
,
4581 xfs_btree_query_range_fn fn
,
4584 union xfs_btree_rec
*recp
;
4585 union xfs_btree_key rec_key
;
4588 bool firstrec
= true;
4591 ASSERT(cur
->bc_ops
->init_high_key_from_rec
);
4592 ASSERT(cur
->bc_ops
->diff_two_keys
);
4595 * Find the leftmost record. The btree cursor must be set
4596 * to the low record used to generate low_key.
4599 error
= xfs_btree_lookup(cur
, XFS_LOOKUP_LE
, &stat
);
4603 /* Nothing? See if there's anything to the right. */
4605 error
= xfs_btree_increment(cur
, 0, &stat
);
4611 /* Find the record. */
4612 error
= xfs_btree_get_rec(cur
, &recp
, &stat
);
4616 /* Skip if high_key(rec) < low_key. */
4618 cur
->bc_ops
->init_high_key_from_rec(&rec_key
, recp
);
4620 diff
= cur
->bc_ops
->diff_two_keys(cur
, low_key
,
4626 /* Stop if high_key < low_key(rec). */
4627 cur
->bc_ops
->init_key_from_rec(&rec_key
, recp
);
4628 diff
= cur
->bc_ops
->diff_two_keys(cur
, &rec_key
, high_key
);
4633 error
= fn(cur
, recp
, priv
);
4634 if (error
< 0 || error
== XFS_BTREE_QUERY_RANGE_ABORT
)
4638 /* Move on to the next record. */
4639 error
= xfs_btree_increment(cur
, 0, &stat
);
4649 * Query an overlapped interval btree for all records overlapping a given
4650 * interval. This function roughly follows the algorithm given in
4651 * "Interval Trees" of _Introduction to Algorithms_, which is section
4652 * 14.3 in the 2nd and 3rd editions.
4654 * First, generate keys for the low and high records passed in.
4656 * For any leaf node, generate the high and low keys for the record.
4657 * If the record keys overlap with the query low/high keys, pass the
4658 * record to the function iterator.
4660 * For any internal node, compare the low and high keys of each
4661 * pointer against the query low/high keys. If there's an overlap,
4662 * follow the pointer.
4664 * As an optimization, we stop scanning a block when we find a low key
4665 * that is greater than the query's high key.
4668 xfs_btree_overlapped_query_range(
4669 struct xfs_btree_cur
*cur
,
4670 union xfs_btree_key
*low_key
,
4671 union xfs_btree_key
*high_key
,
4672 xfs_btree_query_range_fn fn
,
4675 union xfs_btree_ptr ptr
;
4676 union xfs_btree_ptr
*pp
;
4677 union xfs_btree_key rec_key
;
4678 union xfs_btree_key rec_hkey
;
4679 union xfs_btree_key
*lkp
;
4680 union xfs_btree_key
*hkp
;
4681 union xfs_btree_rec
*recp
;
4682 struct xfs_btree_block
*block
;
4690 /* Load the root of the btree. */
4691 level
= cur
->bc_nlevels
- 1;
4692 cur
->bc_ops
->init_ptr_from_cur(cur
, &ptr
);
4693 error
= xfs_btree_lookup_get_block(cur
, level
, &ptr
, &block
);
4696 xfs_btree_get_block(cur
, level
, &bp
);
4697 trace_xfs_btree_overlapped_query_range(cur
, level
, bp
);
4699 error
= xfs_btree_check_block(cur
, block
, level
, bp
);
4703 cur
->bc_ptrs
[level
] = 1;
4705 while (level
< cur
->bc_nlevels
) {
4706 block
= xfs_btree_get_block(cur
, level
, &bp
);
4708 /* End of node, pop back towards the root. */
4709 if (cur
->bc_ptrs
[level
] > be16_to_cpu(block
->bb_numrecs
)) {
4711 if (level
< cur
->bc_nlevels
- 1)
4712 cur
->bc_ptrs
[level
+ 1]++;
4718 /* Handle a leaf node. */
4719 recp
= xfs_btree_rec_addr(cur
, cur
->bc_ptrs
[0], block
);
4721 cur
->bc_ops
->init_high_key_from_rec(&rec_hkey
, recp
);
4722 ldiff
= cur
->bc_ops
->diff_two_keys(cur
, &rec_hkey
,
4725 cur
->bc_ops
->init_key_from_rec(&rec_key
, recp
);
4726 hdiff
= cur
->bc_ops
->diff_two_keys(cur
, high_key
,
4730 * If (record's high key >= query's low key) and
4731 * (query's high key >= record's low key), then
4732 * this record overlaps the query range; callback.
4734 if (ldiff
>= 0 && hdiff
>= 0) {
4735 error
= fn(cur
, recp
, priv
);
4737 error
== XFS_BTREE_QUERY_RANGE_ABORT
)
4739 } else if (hdiff
< 0) {
4740 /* Record is larger than high key; pop. */
4743 cur
->bc_ptrs
[level
]++;
4747 /* Handle an internal node. */
4748 lkp
= xfs_btree_key_addr(cur
, cur
->bc_ptrs
[level
], block
);
4749 hkp
= xfs_btree_high_key_addr(cur
, cur
->bc_ptrs
[level
], block
);
4750 pp
= xfs_btree_ptr_addr(cur
, cur
->bc_ptrs
[level
], block
);
4752 ldiff
= cur
->bc_ops
->diff_two_keys(cur
, hkp
, low_key
);
4753 hdiff
= cur
->bc_ops
->diff_two_keys(cur
, high_key
, lkp
);
4756 * If (pointer's high key >= query's low key) and
4757 * (query's high key >= pointer's low key), then
4758 * this record overlaps the query range; follow pointer.
4760 if (ldiff
>= 0 && hdiff
>= 0) {
4762 error
= xfs_btree_lookup_get_block(cur
, level
, pp
,
4766 xfs_btree_get_block(cur
, level
, &bp
);
4767 trace_xfs_btree_overlapped_query_range(cur
, level
, bp
);
4769 error
= xfs_btree_check_block(cur
, block
, level
, bp
);
4773 cur
->bc_ptrs
[level
] = 1;
4775 } else if (hdiff
< 0) {
4776 /* The low key is larger than the upper range; pop. */
4779 cur
->bc_ptrs
[level
]++;
4784 * If we don't end this function with the cursor pointing at a record
4785 * block, a subsequent non-error cursor deletion will not release
4786 * node-level buffers, causing a buffer leak. This is quite possible
4787 * with a zero-results range query, so release the buffers if we
4788 * failed to return any results.
4790 if (cur
->bc_bufs
[0] == NULL
) {
4791 for (i
= 0; i
< cur
->bc_nlevels
; i
++) {
4792 if (cur
->bc_bufs
[i
]) {
4793 xfs_trans_brelse(cur
->bc_tp
, cur
->bc_bufs
[i
]);
4794 cur
->bc_bufs
[i
] = NULL
;
4795 cur
->bc_ptrs
[i
] = 0;
4805 * Query a btree for all records overlapping a given interval of keys. The
4806 * supplied function will be called with each record found; return one of the
4807 * XFS_BTREE_QUERY_RANGE_{CONTINUE,ABORT} values or the usual negative error
4808 * code. This function returns XFS_BTREE_QUERY_RANGE_ABORT, zero, or a
4809 * negative error code.
4812 xfs_btree_query_range(
4813 struct xfs_btree_cur
*cur
,
4814 union xfs_btree_irec
*low_rec
,
4815 union xfs_btree_irec
*high_rec
,
4816 xfs_btree_query_range_fn fn
,
4819 union xfs_btree_rec rec
;
4820 union xfs_btree_key low_key
;
4821 union xfs_btree_key high_key
;
4823 /* Find the keys of both ends of the interval. */
4824 cur
->bc_rec
= *high_rec
;
4825 cur
->bc_ops
->init_rec_from_cur(cur
, &rec
);
4826 cur
->bc_ops
->init_key_from_rec(&high_key
, &rec
);
4828 cur
->bc_rec
= *low_rec
;
4829 cur
->bc_ops
->init_rec_from_cur(cur
, &rec
);
4830 cur
->bc_ops
->init_key_from_rec(&low_key
, &rec
);
4832 /* Enforce low key < high key. */
4833 if (cur
->bc_ops
->diff_two_keys(cur
, &low_key
, &high_key
) > 0)
4836 if (!(cur
->bc_flags
& XFS_BTREE_OVERLAPPING
))
4837 return xfs_btree_simple_query_range(cur
, &low_key
,
4838 &high_key
, fn
, priv
);
4839 return xfs_btree_overlapped_query_range(cur
, &low_key
, &high_key
,
4843 /* Query a btree for all records. */
4845 xfs_btree_query_all(
4846 struct xfs_btree_cur
*cur
,
4847 xfs_btree_query_range_fn fn
,
4850 union xfs_btree_key low_key
;
4851 union xfs_btree_key high_key
;
4853 memset(&cur
->bc_rec
, 0, sizeof(cur
->bc_rec
));
4854 memset(&low_key
, 0, sizeof(low_key
));
4855 memset(&high_key
, 0xFF, sizeof(high_key
));
4857 return xfs_btree_simple_query_range(cur
, &low_key
, &high_key
, fn
, priv
);
4861 * Calculate the number of blocks needed to store a given number of records
4862 * in a short-format (per-AG metadata) btree.
4865 xfs_btree_calc_size(
4866 struct xfs_mount
*mp
,
4868 unsigned long long len
)
4874 maxrecs
= limits
[0];
4875 for (level
= 0, rval
= 0; len
> 1; level
++) {
4877 do_div(len
, maxrecs
);
4878 maxrecs
= limits
[1];
4885 xfs_btree_count_blocks_helper(
4886 struct xfs_btree_cur
*cur
,
4890 xfs_extlen_t
*blocks
= data
;
4896 /* Count the blocks in a btree and return the result in *blocks. */
4898 xfs_btree_count_blocks(
4899 struct xfs_btree_cur
*cur
,
4900 xfs_extlen_t
*blocks
)
4903 return xfs_btree_visit_blocks(cur
, xfs_btree_count_blocks_helper
,