]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blame - libxfs/xfs_btree.c
libxfs: refactor manage_zones()
[thirdparty/xfsprogs-dev.git] / libxfs / xfs_btree.c
CommitLineData
37b3b4d6 1// SPDX-License-Identifier: GPL-2.0
2bd0ea18 2/*
da23017d
NS
3 * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
4 * All Rights Reserved.
2bd0ea18 5 */
9c799827 6#include "libxfs_priv.h"
b626fb59
DC
7#include "xfs_fs.h"
8#include "xfs_shared.h"
9#include "xfs_format.h"
10#include "xfs_log_format.h"
11#include "xfs_trans_resv.h"
12#include "xfs_bit.h"
13#include "xfs_mount.h"
f944d3d0 14#include "xfs_defer.h"
b626fb59
DC
15#include "xfs_inode.h"
16#include "xfs_trans.h"
17#include "xfs_btree.h"
56d3fc2b 18#include "xfs_errortag.h"
b626fb59
DC
19#include "xfs_trace.h"
20#include "xfs_cksum.h"
21#include "xfs_alloc.h"
2bd0ea18
NS
22
23/*
24 * Cursor allocation zone.
25 */
5e656dbb 26kmem_zone_t *xfs_btree_cur_zone;
2bd0ea18
NS
27
28/*
29 * Btree magic numbers.
30 */
4a492e72 31static const uint32_t xfs_magics[2][XFS_BTNUM_MAX] = {
e37838e5 32 { XFS_ABTB_MAGIC, XFS_ABTC_MAGIC, 0, XFS_BMAP_MAGIC, XFS_IBT_MAGIC,
88ce0792 33 XFS_FIBT_MAGIC, 0 },
e37838e5 34 { XFS_ABTB_CRC_MAGIC, XFS_ABTC_CRC_MAGIC, XFS_RMAP_CRC_MAGIC,
88ce0792
DW
35 XFS_BMAP_CRC_MAGIC, XFS_IBT_CRC_MAGIC, XFS_FIBT_CRC_MAGIC,
36 XFS_REFC_CRC_MAGIC }
2bd0ea18 37};
4c6b3277 38
4a492e72 39uint32_t
4c6b3277
ES
40xfs_btree_magic(
41 int crc,
42 xfs_btnum_t btnum)
43{
4a492e72 44 uint32_t magic = xfs_magics[crc][btnum];
4c6b3277
ES
45
46 /* Ensure we asked for crc for crc-only magics. */
47 ASSERT(magic != 0);
48 return magic;
49}
2bd0ea18 50
b47beff8
DW
51/*
52 * Check a long btree block header. Return the address of the failing check,
53 * or NULL if everything is ok.
54 */
55xfs_failaddr_t
56__xfs_btree_check_lblock(
57 struct xfs_btree_cur *cur,
58 struct xfs_btree_block *block,
59 int level,
60 struct xfs_buf *bp)
2bd0ea18 61{
b47beff8 62 struct xfs_mount *mp = cur->bc_mp;
4c6b3277 63 xfs_btnum_t btnum = cur->bc_btnum;
b47beff8 64 int crc = xfs_sb_version_hascrc(&mp->m_sb);
5dfa5cd2 65
4c6b3277 66 if (crc) {
b47beff8
DW
67 if (!uuid_equal(&block->bb_u.l.bb_uuid, &mp->m_sb.sb_meta_uuid))
68 return __this_address;
69 if (block->bb_u.l.bb_blkno !=
70 cpu_to_be64(bp ? bp->b_bn : XFS_BUF_DADDR_NULL))
71 return __this_address;
72 if (block->bb_u.l.bb_pad != cpu_to_be32(0))
73 return __this_address;
5dfa5cd2
DC
74 }
75
b47beff8
DW
76 if (be32_to_cpu(block->bb_magic) != xfs_btree_magic(crc, btnum))
77 return __this_address;
78 if (be16_to_cpu(block->bb_level) != level)
79 return __this_address;
80 if (be16_to_cpu(block->bb_numrecs) >
81 cur->bc_ops->get_maxrecs(cur, level))
82 return __this_address;
83 if (block->bb_u.l.bb_leftsib != cpu_to_be64(NULLFSBLOCK) &&
84 !xfs_btree_check_lptr(cur, be64_to_cpu(block->bb_u.l.bb_leftsib),
85 level + 1))
86 return __this_address;
87 if (block->bb_u.l.bb_rightsib != cpu_to_be64(NULLFSBLOCK) &&
88 !xfs_btree_check_lptr(cur, be64_to_cpu(block->bb_u.l.bb_rightsib),
89 level + 1))
90 return __this_address;
91
92 return NULL;
93}
94
95/* Check a long btree block header. */
246486c1 96static int
b47beff8
DW
97xfs_btree_check_lblock(
98 struct xfs_btree_cur *cur,
99 struct xfs_btree_block *block,
100 int level,
101 struct xfs_buf *bp)
102{
103 struct xfs_mount *mp = cur->bc_mp;
104 xfs_failaddr_t fa;
105
106 fa = __xfs_btree_check_lblock(cur, block, level, bp);
107 if (unlikely(XFS_TEST_ERROR(fa != NULL, mp,
e2a190dd 108 XFS_ERRTAG_BTREE_CHECK_LBLOCK))) {
2bd0ea18 109 if (bp)
56b2de80 110 trace_xfs_btree_corrupt(bp, _RET_IP_);
5dfa5cd2 111 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp);
12b53197 112 return -EFSCORRUPTED;
2bd0ea18
NS
113 }
114 return 0;
115}
116
b47beff8
DW
117/*
118 * Check a short btree block header. Return the address of the failing check,
119 * or NULL if everything is ok.
120 */
121xfs_failaddr_t
122__xfs_btree_check_sblock(
123 struct xfs_btree_cur *cur,
124 struct xfs_btree_block *block,
125 int level,
126 struct xfs_buf *bp)
2bd0ea18 127{
b47beff8 128 struct xfs_mount *mp = cur->bc_mp;
4c6b3277 129 xfs_btnum_t btnum = cur->bc_btnum;
b47beff8 130 int crc = xfs_sb_version_hascrc(&mp->m_sb);
5dfa5cd2 131
4c6b3277 132 if (crc) {
b47beff8
DW
133 if (!uuid_equal(&block->bb_u.s.bb_uuid, &mp->m_sb.sb_meta_uuid))
134 return __this_address;
135 if (block->bb_u.s.bb_blkno !=
136 cpu_to_be64(bp ? bp->b_bn : XFS_BUF_DADDR_NULL))
137 return __this_address;
5dfa5cd2
DC
138 }
139
b47beff8
DW
140 if (be32_to_cpu(block->bb_magic) != xfs_btree_magic(crc, btnum))
141 return __this_address;
142 if (be16_to_cpu(block->bb_level) != level)
143 return __this_address;
144 if (be16_to_cpu(block->bb_numrecs) >
145 cur->bc_ops->get_maxrecs(cur, level))
146 return __this_address;
147 if (block->bb_u.s.bb_leftsib != cpu_to_be32(NULLAGBLOCK) &&
148 !xfs_btree_check_sptr(cur, be32_to_cpu(block->bb_u.s.bb_leftsib),
149 level + 1))
150 return __this_address;
151 if (block->bb_u.s.bb_rightsib != cpu_to_be32(NULLAGBLOCK) &&
152 !xfs_btree_check_sptr(cur, be32_to_cpu(block->bb_u.s.bb_rightsib),
153 level + 1))
154 return __this_address;
155
156 return NULL;
157}
158
159/* Check a short btree block header. */
160STATIC int
161xfs_btree_check_sblock(
162 struct xfs_btree_cur *cur,
163 struct xfs_btree_block *block,
164 int level,
165 struct xfs_buf *bp)
166{
167 struct xfs_mount *mp = cur->bc_mp;
168 xfs_failaddr_t fa;
169
170 fa = __xfs_btree_check_sblock(cur, block, level, bp);
171 if (unlikely(XFS_TEST_ERROR(fa != NULL, mp,
e2a190dd 172 XFS_ERRTAG_BTREE_CHECK_SBLOCK))) {
2bd0ea18 173 if (bp)
56b2de80 174 trace_xfs_btree_corrupt(bp, _RET_IP_);
5dfa5cd2 175 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp);
12b53197 176 return -EFSCORRUPTED;
2bd0ea18
NS
177 }
178 return 0;
179}
180
181/*
b194c7d8
BN
182 * Debug routine: check that block header is ok.
183 */
184int
185xfs_btree_check_block(
186 struct xfs_btree_cur *cur, /* btree cursor */
187 struct xfs_btree_block *block, /* generic btree block pointer */
188 int level, /* level of the btree block */
189 struct xfs_buf *bp) /* buffer containing block, if any */
190{
b3563c19
BN
191 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
192 return xfs_btree_check_lblock(cur, block, level, bp);
193 else
194 return xfs_btree_check_sblock(cur, block, level, bp);
b194c7d8
BN
195}
196
d5c546df
DW
197/* Check that this long pointer is valid and points within the fs. */
198bool
b194c7d8 199xfs_btree_check_lptr(
d5c546df
DW
200 struct xfs_btree_cur *cur,
201 xfs_fsblock_t fsbno,
202 int level)
b194c7d8 203{
d5c546df
DW
204 if (level <= 0)
205 return false;
206 return xfs_verify_fsbno(cur->bc_mp, fsbno);
b194c7d8
BN
207}
208
d5c546df
DW
209/* Check that this short pointer is valid and points within the AG. */
210bool
2bd0ea18 211xfs_btree_check_sptr(
d5c546df
DW
212 struct xfs_btree_cur *cur,
213 xfs_agblock_t agbno,
214 int level)
2bd0ea18 215{
d5c546df
DW
216 if (level <= 0)
217 return false;
218 return xfs_verify_agbno(cur->bc_mp, cur->bc_private.a.agno, agbno);
2bd0ea18
NS
219}
220
b194c7d8 221/*
d5c546df
DW
222 * Check that a given (indexed) btree pointer at a certain level of a
223 * btree is valid and doesn't point past where it should.
b194c7d8 224 */
246486c1 225static int
b194c7d8 226xfs_btree_check_ptr(
d5c546df
DW
227 struct xfs_btree_cur *cur,
228 union xfs_btree_ptr *ptr,
229 int index,
230 int level)
b194c7d8
BN
231{
232 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
7cf9f816
DW
233 if (xfs_btree_check_lptr(cur, be64_to_cpu((&ptr->l)[index]),
234 level))
235 return 0;
236 xfs_err(cur->bc_mp,
237"Inode %llu fork %d: Corrupt btree %d pointer at level %d index %d.",
238 cur->bc_private.b.ip->i_ino,
239 cur->bc_private.b.whichfork, cur->bc_btnum,
240 level, index);
b194c7d8 241 } else {
7cf9f816
DW
242 if (xfs_btree_check_sptr(cur, be32_to_cpu((&ptr->s)[index]),
243 level))
244 return 0;
245 xfs_err(cur->bc_mp,
246"AG %u: Corrupt btree %d pointer at level %d index %d.",
247 cur->bc_private.a.agno, cur->bc_btnum,
248 level, index);
b194c7d8 249 }
d5c546df 250
7cf9f816 251 return -EFSCORRUPTED;
b194c7d8 252}
efa7dddb
DW
253
254#ifdef DEBUG
255# define xfs_btree_debug_check_ptr xfs_btree_check_ptr
256#else
257# define xfs_btree_debug_check_ptr(...) (0)
870d4cbc 258#endif
b194c7d8 259
5dfa5cd2
DC
260/*
261 * Calculate CRC on the whole btree block and stuff it into the
262 * long-form btree header.
263 *
264 * Prior to calculting the CRC, pull the LSN out of the buffer log item and put
eab16f4c 265 * it into the buffer so recovery knows what the last modification was that made
5dfa5cd2
DC
266 * it to disk.
267 */
268void
269xfs_btree_lblock_calc_crc(
270 struct xfs_buf *bp)
271{
272 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
37d086ca 273 struct xfs_buf_log_item *bip = bp->b_log_item;
5dfa5cd2
DC
274
275 if (!xfs_sb_version_hascrc(&bp->b_target->bt_mount->m_sb))
276 return;
277 if (bip)
278 block->bb_u.l.bb_lsn = cpu_to_be64(bip->bli_item.li_lsn);
43b5aeed 279 xfs_buf_update_cksum(bp, XFS_BTREE_LBLOCK_CRC_OFF);
5dfa5cd2
DC
280}
281
282bool
283xfs_btree_lblock_verify_crc(
284 struct xfs_buf *bp)
285{
a65d8d29
BF
286 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
287 struct xfs_mount *mp = bp->b_target->bt_mount;
288
4ca4eb89 289 if (xfs_sb_version_hascrc(&mp->m_sb)) {
a65d8d29
BF
290 if (!xfs_log_check_lsn(mp, be64_to_cpu(block->bb_u.l.bb_lsn)))
291 return false;
d21ca64d 292 return xfs_buf_verify_cksum(bp, XFS_BTREE_LBLOCK_CRC_OFF);
a65d8d29 293 }
d21ca64d 294
5dfa5cd2
DC
295 return true;
296}
297
298/*
299 * Calculate CRC on the whole btree block and stuff it into the
300 * short-form btree header.
301 *
302 * Prior to calculting the CRC, pull the LSN out of the buffer log item and put
eab16f4c 303 * it into the buffer so recovery knows what the last modification was that made
5dfa5cd2
DC
304 * it to disk.
305 */
306void
307xfs_btree_sblock_calc_crc(
308 struct xfs_buf *bp)
309{
310 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
37d086ca 311 struct xfs_buf_log_item *bip = bp->b_log_item;
5dfa5cd2
DC
312
313 if (!xfs_sb_version_hascrc(&bp->b_target->bt_mount->m_sb))
314 return;
315 if (bip)
316 block->bb_u.s.bb_lsn = cpu_to_be64(bip->bli_item.li_lsn);
43b5aeed 317 xfs_buf_update_cksum(bp, XFS_BTREE_SBLOCK_CRC_OFF);
5dfa5cd2
DC
318}
319
320bool
321xfs_btree_sblock_verify_crc(
322 struct xfs_buf *bp)
323{
a65d8d29
BF
324 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
325 struct xfs_mount *mp = bp->b_target->bt_mount;
326
4ca4eb89 327 if (xfs_sb_version_hascrc(&mp->m_sb)) {
a65d8d29 328 if (!xfs_log_check_lsn(mp, be64_to_cpu(block->bb_u.s.bb_lsn)))
dbd3d464 329 return false;
d21ca64d 330 return xfs_buf_verify_cksum(bp, XFS_BTREE_SBLOCK_CRC_OFF);
a65d8d29 331 }
d21ca64d 332
5dfa5cd2
DC
333 return true;
334}
335
c261f8c0
CH
336static int
337xfs_btree_free_block(
338 struct xfs_btree_cur *cur,
339 struct xfs_buf *bp)
340{
341 int error;
342
343 error = cur->bc_ops->free_block(cur, bp);
08caf14f
CH
344 if (!error) {
345 xfs_trans_binval(cur->bc_tp, bp);
c261f8c0 346 XFS_BTREE_STATS_INC(cur, free);
08caf14f 347 }
c261f8c0
CH
348 return error;
349}
350
2bd0ea18
NS
351/*
352 * Delete the btree cursor.
353 */
354void
355xfs_btree_del_cursor(
dfc130f3 356 xfs_btree_cur_t *cur, /* btree cursor */
2bd0ea18
NS
357 int error) /* del because of error */
358{
359 int i; /* btree level */
360
361 /*
362 * Clear the buffer pointers, and release the buffers.
363 * If we're doing this in the face of an error, we
364 * need to make sure to inspect all of the entries
365 * in the bc_bufs array for buffers to be unlocked.
366 * This is because some of the btree code works from
367 * level n down to 0, and if we get an error along
368 * the way we won't have initialized all the entries
369 * down to 0.
370 */
371 for (i = 0; i < cur->bc_nlevels; i++) {
372 if (cur->bc_bufs[i])
56b2de80 373 xfs_trans_brelse(cur->bc_tp, cur->bc_bufs[i]);
2bd0ea18
NS
374 else if (!error)
375 break;
376 }
377 /*
5000d01d 378 * Can't free a bmap cursor without having dealt with the
2bd0ea18
NS
379 * allocated indirect blocks' accounting.
380 */
381 ASSERT(cur->bc_btnum != XFS_BTNUM_BMAP ||
382 cur->bc_private.b.allocated == 0);
383 /*
384 * Free the cursor.
385 */
386 kmem_zone_free(xfs_btree_cur_zone, cur);
387}
388
389/*
390 * Duplicate the btree cursor.
391 * Allocate a new one, copy the record, re-get the buffers.
392 */
393int /* error */
394xfs_btree_dup_cursor(
dfc130f3
RC
395 xfs_btree_cur_t *cur, /* input cursor */
396 xfs_btree_cur_t **ncur) /* output cursor */
2bd0ea18
NS
397{
398 xfs_buf_t *bp; /* btree block's buffer pointer */
5000d01d 399 int error; /* error return value */
2bd0ea18
NS
400 int i; /* level number of btree block */
401 xfs_mount_t *mp; /* mount structure for filesystem */
dfc130f3 402 xfs_btree_cur_t *new; /* new cursor value */
2bd0ea18
NS
403 xfs_trans_t *tp; /* transaction pointer, can be NULL */
404
405 tp = cur->bc_tp;
406 mp = cur->bc_mp;
b194c7d8 407
2bd0ea18
NS
408 /*
409 * Allocate a new cursor like the old one.
410 */
b194c7d8
BN
411 new = cur->bc_ops->dup_cursor(cur);
412
2bd0ea18
NS
413 /*
414 * Copy the record currently in the cursor.
415 */
416 new->bc_rec = cur->bc_rec;
b194c7d8 417
2bd0ea18
NS
418 /*
419 * For each level current, re-get the buffer and copy the ptr value.
420 */
421 for (i = 0; i < new->bc_nlevels; i++) {
422 new->bc_ptrs[i] = cur->bc_ptrs[i];
423 new->bc_ra[i] = cur->bc_ra[i];
a2ceac1f
DC
424 bp = cur->bc_bufs[i];
425 if (bp) {
426 error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp,
427 XFS_BUF_ADDR(bp), mp->m_bsize,
428 0, &bp,
429 cur->bc_ops->buf_ops);
430 if (error) {
2bd0ea18
NS
431 xfs_btree_del_cursor(new, error);
432 *ncur = NULL;
433 return error;
434 }
5dfa5cd2
DC
435 }
436 new->bc_bufs[i] = bp;
2bd0ea18 437 }
2bd0ea18
NS
438 *ncur = new;
439 return 0;
440}
441
b194c7d8
BN
442/*
443 * XFS btree block layout and addressing:
444 *
445 * There are two types of blocks in the btree: leaf and non-leaf blocks.
446 *
447 * The leaf record start with a header then followed by records containing
448 * the values. A non-leaf block also starts with the same header, and
449 * then first contains lookup keys followed by an equal number of pointers
450 * to the btree blocks at the previous level.
451 *
452 * +--------+-------+-------+-------+-------+-------+-------+
453 * Leaf: | header | rec 1 | rec 2 | rec 3 | rec 4 | rec 5 | rec N |
454 * +--------+-------+-------+-------+-------+-------+-------+
455 *
456 * +--------+-------+-------+-------+-------+-------+-------+
457 * Non-Leaf: | header | key 1 | key 2 | key N | ptr 1 | ptr 2 | ptr N |
458 * +--------+-------+-------+-------+-------+-------+-------+
459 *
460 * The header is called struct xfs_btree_block for reasons better left unknown
461 * and comes in different versions for short (32bit) and long (64bit) block
462 * pointers. The record and key structures are defined by the btree instances
463 * and opaque to the btree core. The block pointers are simple disk endian
464 * integers, available in a short (32bit) and long (64bit) variant.
465 *
466 * The helpers below calculate the offset of a given record, key or pointer
467 * into a btree block (xfs_btree_*_offset) or return a pointer to the given
468 * record, key or pointer (xfs_btree_*_addr). Note that all addressing
469 * inside the btree block is done using indices starting at one, not zero!
13e831e0
DW
470 *
471 * If XFS_BTREE_OVERLAPPING is set, then this btree supports keys containing
472 * overlapping intervals. In such a tree, records are still sorted lowest to
473 * highest and indexed by the smallest key value that refers to the record.
474 * However, nodes are different: each pointer has two associated keys -- one
475 * indexing the lowest key available in the block(s) below (the same behavior
476 * as the key in a regular btree) and another indexing the highest key
477 * available in the block(s) below. Because records are /not/ sorted by the
478 * highest key, all leaf block updates require us to compute the highest key
479 * that matches any record in the leaf and to recursively update the high keys
480 * in the nodes going further up in the tree, if necessary. Nodes look like
481 * this:
482 *
483 * +--------+-----+-----+-----+-----+-----+-------+-------+-----+
484 * Non-Leaf: | header | lo1 | hi1 | lo2 | hi2 | ... | ptr 1 | ptr 2 | ... |
485 * +--------+-----+-----+-----+-----+-----+-------+-------+-----+
486 *
487 * To perform an interval query on an overlapped tree, perform the usual
488 * depth-first search and use the low and high keys to decide if we can skip
489 * that particular node. If a leaf node is reached, return the records that
490 * intersect the interval. Note that an interval query may return numerous
491 * entries. For a non-overlapped tree, simply search for the record associated
492 * with the lowest key and iterate forward until a non-matching record is
493 * found. Section 14.3 ("Interval Trees") of _Introduction to Algorithms_ by
494 * Cormen, Leiserson, Rivest, and Stein (2nd or 3rd ed. only) discuss this in
495 * more detail.
496 *
497 * Why do we care about overlapping intervals? Let's say you have a bunch of
498 * reverse mapping records on a reflink filesystem:
499 *
500 * 1: +- file A startblock B offset C length D -----------+
501 * 2: +- file E startblock F offset G length H --------------+
502 * 3: +- file I startblock F offset J length K --+
503 * 4: +- file L... --+
504 *
505 * Now say we want to map block (B+D) into file A at offset (C+D). Ideally,
506 * we'd simply increment the length of record 1. But how do we find the record
507 * that ends at (B+D-1) (i.e. record 1)? A LE lookup of (B+D-1) would return
508 * record 3 because the keys are ordered first by startblock. An interval
509 * query would return records 1 and 2 because they both overlap (B+D-1), and
510 * from that we can pick out record 1 as the appropriate left neighbor.
511 *
512 * In the non-overlapped case you can do a LE lookup and decrement the cursor
513 * because a record's interval must end before the next record.
b194c7d8
BN
514 */
515
516/*
517 * Return size of the btree block header for this btree instance.
518 */
519static inline size_t xfs_btree_block_len(struct xfs_btree_cur *cur)
520{
e0607266
DC
521 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
522 if (cur->bc_flags & XFS_BTREE_CRC_BLOCKS)
523 return XFS_BTREE_LBLOCK_CRC_LEN;
524 return XFS_BTREE_LBLOCK_LEN;
525 }
5dfa5cd2 526 if (cur->bc_flags & XFS_BTREE_CRC_BLOCKS)
e0607266
DC
527 return XFS_BTREE_SBLOCK_CRC_LEN;
528 return XFS_BTREE_SBLOCK_LEN;
b194c7d8
BN
529}
530
531/*
532 * Return size of btree block pointers for this btree instance.
533 */
534static inline size_t xfs_btree_ptr_len(struct xfs_btree_cur *cur)
535{
536 return (cur->bc_flags & XFS_BTREE_LONG_PTRS) ?
537 sizeof(__be64) : sizeof(__be32);
538}
539
540/*
541 * Calculate offset of the n-th record in a btree block.
542 */
543STATIC size_t
544xfs_btree_rec_offset(
545 struct xfs_btree_cur *cur,
546 int n)
547{
548 return xfs_btree_block_len(cur) +
549 (n - 1) * cur->bc_ops->rec_len;
550}
551
552/*
553 * Calculate offset of the n-th key in a btree block.
554 */
555STATIC size_t
556xfs_btree_key_offset(
557 struct xfs_btree_cur *cur,
558 int n)
559{
560 return xfs_btree_block_len(cur) +
561 (n - 1) * cur->bc_ops->key_len;
562}
563
13e831e0
DW
564/*
565 * Calculate offset of the n-th high key in a btree block.
566 */
567STATIC size_t
568xfs_btree_high_key_offset(
569 struct xfs_btree_cur *cur,
570 int n)
571{
572 return xfs_btree_block_len(cur) +
573 (n - 1) * cur->bc_ops->key_len + (cur->bc_ops->key_len / 2);
574}
575
b194c7d8
BN
576/*
577 * Calculate offset of the n-th block pointer in a btree block.
578 */
579STATIC size_t
580xfs_btree_ptr_offset(
581 struct xfs_btree_cur *cur,
582 int n,
583 int level)
584{
585 return xfs_btree_block_len(cur) +
586 cur->bc_ops->get_maxrecs(cur, level) * cur->bc_ops->key_len +
587 (n - 1) * xfs_btree_ptr_len(cur);
588}
589
590/*
591 * Return a pointer to the n-th record in the btree block.
592 */
50bb67d6 593union xfs_btree_rec *
b194c7d8
BN
594xfs_btree_rec_addr(
595 struct xfs_btree_cur *cur,
596 int n,
597 struct xfs_btree_block *block)
598{
599 return (union xfs_btree_rec *)
600 ((char *)block + xfs_btree_rec_offset(cur, n));
601}
602
603/*
604 * Return a pointer to the n-th key in the btree block.
605 */
50bb67d6 606union xfs_btree_key *
b194c7d8
BN
607xfs_btree_key_addr(
608 struct xfs_btree_cur *cur,
609 int n,
610 struct xfs_btree_block *block)
611{
612 return (union xfs_btree_key *)
613 ((char *)block + xfs_btree_key_offset(cur, n));
614}
615
13e831e0
DW
616/*
617 * Return a pointer to the n-th high key in the btree block.
618 */
50bb67d6 619union xfs_btree_key *
13e831e0
DW
620xfs_btree_high_key_addr(
621 struct xfs_btree_cur *cur,
622 int n,
623 struct xfs_btree_block *block)
624{
625 return (union xfs_btree_key *)
626 ((char *)block + xfs_btree_high_key_offset(cur, n));
627}
628
b194c7d8
BN
629/*
630 * Return a pointer to the n-th block pointer in the btree block.
631 */
50bb67d6 632union xfs_btree_ptr *
b194c7d8
BN
633xfs_btree_ptr_addr(
634 struct xfs_btree_cur *cur,
635 int n,
636 struct xfs_btree_block *block)
637{
638 int level = xfs_btree_get_level(block);
639
640 ASSERT(block->bb_level != 0);
641
642 return (union xfs_btree_ptr *)
643 ((char *)block + xfs_btree_ptr_offset(cur, n, level));
644}
645
646/*
10851b18 647 * Get the root block which is stored in the inode.
b194c7d8
BN
648 *
649 * For now this btree implementation assumes the btree root is always
650 * stored in the if_broot field of an inode fork.
651 */
652STATIC struct xfs_btree_block *
653xfs_btree_get_iroot(
dcaff8ac 654 struct xfs_btree_cur *cur)
b194c7d8 655{
dcaff8ac 656 struct xfs_ifork *ifp;
b194c7d8 657
dcaff8ac
KN
658 ifp = XFS_IFORK_PTR(cur->bc_private.b.ip, cur->bc_private.b.whichfork);
659 return (struct xfs_btree_block *)ifp->if_broot;
b194c7d8
BN
660}
661
5000d01d 662/*
2bd0ea18 663 * Retrieve the block pointer from the cursor at the given level.
b194c7d8 664 * This may be an inode btree root or from a buffer.
2bd0ea18 665 */
50bb67d6 666struct xfs_btree_block * /* generic btree block pointer */
2bd0ea18 667xfs_btree_get_block(
b194c7d8 668 struct xfs_btree_cur *cur, /* btree cursor */
2bd0ea18 669 int level, /* level in btree */
b194c7d8
BN
670 struct xfs_buf **bpp) /* buffer containing the block */
671{
672 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
673 (level == cur->bc_nlevels - 1)) {
674 *bpp = NULL;
675 return xfs_btree_get_iroot(cur);
2bd0ea18 676 }
b194c7d8
BN
677
678 *bpp = cur->bc_bufs[level];
679 return XFS_BUF_TO_BLOCK(*bpp);
2bd0ea18
NS
680}
681
682/*
683 * Get a buffer for the block, return it with no data read.
684 * Long-form addressing.
685 */
686xfs_buf_t * /* buffer for fsbno */
687xfs_btree_get_bufl(
688 xfs_mount_t *mp, /* file system mount point */
689 xfs_trans_t *tp, /* transaction pointer */
690 xfs_fsblock_t fsbno, /* file system block number */
691 uint lock) /* lock flags for get_buf */
692{
2bd0ea18
NS
693 xfs_daddr_t d; /* real disk block address */
694
695 ASSERT(fsbno != NULLFSBLOCK);
696 d = XFS_FSB_TO_DADDR(mp, fsbno);
ff105f75 697 return xfs_trans_get_buf(tp, mp->m_ddev_targp, d, mp->m_bsize, lock);
2bd0ea18
NS
698}
699
700/*
701 * Get a buffer for the block, return it with no data read.
702 * Short-form addressing.
703 */
704xfs_buf_t * /* buffer for agno/agbno */
705xfs_btree_get_bufs(
706 xfs_mount_t *mp, /* file system mount point */
707 xfs_trans_t *tp, /* transaction pointer */
708 xfs_agnumber_t agno, /* allocation group number */
709 xfs_agblock_t agbno, /* allocation group block number */
710 uint lock) /* lock flags for get_buf */
711{
2bd0ea18
NS
712 xfs_daddr_t d; /* real disk block address */
713
714 ASSERT(agno != NULLAGNUMBER);
715 ASSERT(agbno != NULLAGBLOCK);
716 d = XFS_AGB_TO_DADDR(mp, agno, agbno);
ff105f75 717 return xfs_trans_get_buf(tp, mp->m_ddev_targp, d, mp->m_bsize, lock);
2bd0ea18
NS
718}
719
2bd0ea18
NS
720/*
721 * Check for the cursor referring to the last block at the given level.
722 */
723int /* 1=is last block, 0=not last block */
724xfs_btree_islastblock(
725 xfs_btree_cur_t *cur, /* btree cursor */
726 int level) /* level to check */
727{
b3563c19 728 struct xfs_btree_block *block; /* generic btree block pointer */
2bd0ea18
NS
729 xfs_buf_t *bp; /* buffer containing block */
730
731 block = xfs_btree_get_block(cur, level, &bp);
732 xfs_btree_check_block(cur, block, level, bp);
b194c7d8 733 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
5a35bf2c 734 return block->bb_u.l.bb_rightsib == cpu_to_be64(NULLFSBLOCK);
2bd0ea18 735 else
a2ceac1f 736 return block->bb_u.s.bb_rightsib == cpu_to_be32(NULLAGBLOCK);
2bd0ea18
NS
737}
738
5e656dbb
BN
739/*
740 * Change the cursor to point to the first record at the given level.
741 * Other levels are unaffected.
742 */
b194c7d8 743STATIC int /* success=1, failure=0 */
5e656dbb
BN
744xfs_btree_firstrec(
745 xfs_btree_cur_t *cur, /* btree cursor */
746 int level) /* level to change */
747{
b3563c19 748 struct xfs_btree_block *block; /* generic btree block pointer */
5e656dbb
BN
749 xfs_buf_t *bp; /* buffer containing block */
750
751 /*
752 * Get the block pointer for this level.
753 */
754 block = xfs_btree_get_block(cur, level, &bp);
c3d6d07a
DW
755 if (xfs_btree_check_block(cur, block, level, bp))
756 return 0;
5e656dbb
BN
757 /*
758 * It's empty, there is no such record.
759 */
b194c7d8 760 if (!block->bb_numrecs)
5e656dbb
BN
761 return 0;
762 /*
763 * Set the ptr value to 1, that's the first record/key.
764 */
765 cur->bc_ptrs[level] = 1;
766 return 1;
767}
768
2bd0ea18
NS
769/*
770 * Change the cursor to point to the last record in the current block
dfc130f3 771 * at the given level. Other levels are unaffected.
2bd0ea18 772 */
b194c7d8 773STATIC int /* success=1, failure=0 */
2bd0ea18
NS
774xfs_btree_lastrec(
775 xfs_btree_cur_t *cur, /* btree cursor */
776 int level) /* level to change */
777{
b3563c19 778 struct xfs_btree_block *block; /* generic btree block pointer */
2bd0ea18
NS
779 xfs_buf_t *bp; /* buffer containing block */
780
781 /*
782 * Get the block pointer for this level.
783 */
784 block = xfs_btree_get_block(cur, level, &bp);
c3d6d07a
DW
785 if (xfs_btree_check_block(cur, block, level, bp))
786 return 0;
2bd0ea18
NS
787 /*
788 * It's empty, there is no such record.
789 */
b194c7d8 790 if (!block->bb_numrecs)
2bd0ea18
NS
791 return 0;
792 /*
793 * Set the ptr value to numrecs, that's the last record/key.
794 */
b194c7d8 795 cur->bc_ptrs[level] = be16_to_cpu(block->bb_numrecs);
2bd0ea18
NS
796 return 1;
797}
798
799/*
800 * Compute first and last byte offsets for the fields given.
801 * Interprets the offsets table, which contains struct field offsets.
802 */
803void
804xfs_btree_offsets(
4a492e72 805 int64_t fields, /* bitmask of fields */
2bd0ea18
NS
806 const short *offsets, /* table of field offsets */
807 int nbits, /* number of bits to inspect */
808 int *first, /* output: first byte offset */
809 int *last) /* output: last byte offset */
810{
811 int i; /* current bit number */
4a492e72 812 int64_t imask; /* mask for current bit number */
2bd0ea18
NS
813
814 ASSERT(fields != 0);
815 /*
816 * Find the lowest bit, so the first byte offset.
817 */
818 for (i = 0, imask = 1LL; ; i++, imask <<= 1) {
819 if (imask & fields) {
820 *first = offsets[i];
821 break;
822 }
823 }
824 /*
825 * Find the highest bit, so the last byte offset.
826 */
827 for (i = nbits - 1, imask = 1LL << i; ; i--, imask >>= 1) {
828 if (imask & fields) {
829 *last = offsets[i + 1] - 1;
830 break;
831 }
832 }
833}
834
835/*
836 * Get a buffer for the block, return it read in.
837 * Long-form addressing.
838 */
a2ceac1f 839int
2bd0ea18 840xfs_btree_read_bufl(
a2ceac1f
DC
841 struct xfs_mount *mp, /* file system mount point */
842 struct xfs_trans *tp, /* transaction pointer */
843 xfs_fsblock_t fsbno, /* file system block number */
844 uint lock, /* lock flags for read_buf */
845 struct xfs_buf **bpp, /* buffer for fsbno */
846 int refval, /* ref count value for buffer */
847 const struct xfs_buf_ops *ops)
2bd0ea18 848{
a2ceac1f 849 struct xfs_buf *bp; /* return value */
2bd0ea18 850 xfs_daddr_t d; /* real disk block address */
a2ceac1f 851 int error;
2bd0ea18 852
ecdc52ff 853 if (!xfs_verify_fsbno(mp, fsbno))
e2376544 854 return -EFSCORRUPTED;
2bd0ea18 855 d = XFS_FSB_TO_DADDR(mp, fsbno);
a2ceac1f
DC
856 error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, d,
857 mp->m_bsize, lock, &bp, ops);
858 if (error)
2bd0ea18 859 return error;
a2ceac1f
DC
860 if (bp)
861 xfs_buf_set_ref(bp, refval);
2bd0ea18
NS
862 *bpp = bp;
863 return 0;
864}
865
10851b18
DC
866/*
867 * Read-ahead the block, don't wait for it, don't return a buffer.
868 * Long-form addressing.
869 */
870/* ARGSUSED */
871void
872xfs_btree_reada_bufl(
873 struct xfs_mount *mp, /* file system mount point */
874 xfs_fsblock_t fsbno, /* file system block number */
875 xfs_extlen_t count, /* count of filesystem blocks */
876 const struct xfs_buf_ops *ops)
877{
878 xfs_daddr_t d;
879
880 ASSERT(fsbno != NULLFSBLOCK);
881 d = XFS_FSB_TO_DADDR(mp, fsbno);
882 xfs_buf_readahead(mp->m_ddev_targp, d, mp->m_bsize * count, ops);
883}
884
885/*
886 * Read-ahead the block, don't wait for it, don't return a buffer.
887 * Short-form addressing.
888 */
889/* ARGSUSED */
890void
891xfs_btree_reada_bufs(
892 struct xfs_mount *mp, /* file system mount point */
893 xfs_agnumber_t agno, /* allocation group number */
894 xfs_agblock_t agbno, /* allocation group block number */
895 xfs_extlen_t count, /* count of filesystem blocks */
896 const struct xfs_buf_ops *ops)
897{
898 xfs_daddr_t d;
899
900 ASSERT(agno != NULLAGNUMBER);
901 ASSERT(agbno != NULLAGBLOCK);
902 d = XFS_AGB_TO_DADDR(mp, agno, agbno);
903 xfs_buf_readahead(mp->m_ddev_targp, d, mp->m_bsize * count, ops);
904}
905
b194c7d8
BN
906STATIC int
907xfs_btree_readahead_lblock(
908 struct xfs_btree_cur *cur,
909 int lr,
910 struct xfs_btree_block *block)
2bd0ea18 911{
2bd0ea18 912 int rval = 0;
5a35bf2c
DC
913 xfs_fsblock_t left = be64_to_cpu(block->bb_u.l.bb_leftsib);
914 xfs_fsblock_t right = be64_to_cpu(block->bb_u.l.bb_rightsib);
2bd0ea18 915
5a35bf2c 916 if ((lr & XFS_BTCUR_LEFTRA) && left != NULLFSBLOCK) {
a2ceac1f
DC
917 xfs_btree_reada_bufl(cur->bc_mp, left, 1,
918 cur->bc_ops->buf_ops);
b194c7d8
BN
919 rval++;
920 }
921
5a35bf2c 922 if ((lr & XFS_BTCUR_RIGHTRA) && right != NULLFSBLOCK) {
a2ceac1f
DC
923 xfs_btree_reada_bufl(cur->bc_mp, right, 1,
924 cur->bc_ops->buf_ops);
b194c7d8 925 rval++;
2bd0ea18 926 }
b194c7d8 927
2bd0ea18
NS
928 return rval;
929}
930
b194c7d8
BN
931STATIC int
932xfs_btree_readahead_sblock(
933 struct xfs_btree_cur *cur,
934 int lr,
935 struct xfs_btree_block *block)
2bd0ea18 936{
b194c7d8
BN
937 int rval = 0;
938 xfs_agblock_t left = be32_to_cpu(block->bb_u.s.bb_leftsib);
939 xfs_agblock_t right = be32_to_cpu(block->bb_u.s.bb_rightsib);
2bd0ea18 940
b194c7d8
BN
941
942 if ((lr & XFS_BTCUR_LEFTRA) && left != NULLAGBLOCK) {
943 xfs_btree_reada_bufs(cur->bc_mp, cur->bc_private.a.agno,
a2ceac1f 944 left, 1, cur->bc_ops->buf_ops);
b194c7d8
BN
945 rval++;
946 }
947
948 if ((lr & XFS_BTCUR_RIGHTRA) && right != NULLAGBLOCK) {
949 xfs_btree_reada_bufs(cur->bc_mp, cur->bc_private.a.agno,
a2ceac1f 950 right, 1, cur->bc_ops->buf_ops);
b194c7d8
BN
951 rval++;
952 }
953
954 return rval;
955}
956
957/*
958 * Read-ahead btree blocks, at the given level.
959 * Bits in lr are set from XFS_BTCUR_{LEFT,RIGHT}RA.
960 */
961STATIC int
962xfs_btree_readahead(
963 struct xfs_btree_cur *cur, /* btree cursor */
964 int lev, /* level in btree */
965 int lr) /* left/right bits */
966{
967 struct xfs_btree_block *block;
968
969 /*
970 * No readahead needed if we are at the root level and the
971 * btree root is stored in the inode.
972 */
973 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
974 (lev == cur->bc_nlevels - 1))
975 return 0;
976
977 if ((cur->bc_ra[lev] | lr) == cur->bc_ra[lev])
978 return 0;
979
980 cur->bc_ra[lev] |= lr;
981 block = XFS_BUF_TO_BLOCK(cur->bc_bufs[lev]);
982
983 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
984 return xfs_btree_readahead_lblock(cur, lr, block);
985 return xfs_btree_readahead_sblock(cur, lr, block);
986}
987
7ab54cd6 988STATIC int
9c6ebc42
DC
989xfs_btree_ptr_to_daddr(
990 struct xfs_btree_cur *cur,
7ab54cd6
DW
991 union xfs_btree_ptr *ptr,
992 xfs_daddr_t *daddr)
9c6ebc42 993{
7ab54cd6
DW
994 xfs_fsblock_t fsbno;
995 xfs_agblock_t agbno;
996 int error;
9c6ebc42 997
7ab54cd6
DW
998 error = xfs_btree_check_ptr(cur, ptr, 0, 1);
999 if (error)
1000 return error;
9c6ebc42 1001
7ab54cd6
DW
1002 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
1003 fsbno = be64_to_cpu(ptr->l);
1004 *daddr = XFS_FSB_TO_DADDR(cur->bc_mp, fsbno);
1005 } else {
1006 agbno = be32_to_cpu(ptr->s);
1007 *daddr = XFS_AGB_TO_DADDR(cur->bc_mp, cur->bc_private.a.agno,
1008 agbno);
9c6ebc42 1009 }
7ab54cd6
DW
1010
1011 return 0;
9c6ebc42
DC
1012}
1013
1014/*
1015 * Readahead @count btree blocks at the given @ptr location.
1016 *
1017 * We don't need to care about long or short form btrees here as we have a
1018 * method of converting the ptr directly to a daddr available to us.
1019 */
1020STATIC void
1021xfs_btree_readahead_ptr(
1022 struct xfs_btree_cur *cur,
1023 union xfs_btree_ptr *ptr,
1024 xfs_extlen_t count)
1025{
7ab54cd6
DW
1026 xfs_daddr_t daddr;
1027
1028 if (xfs_btree_ptr_to_daddr(cur, ptr, &daddr))
1029 return;
1030 xfs_buf_readahead(cur->bc_mp->m_ddev_targp, daddr,
9c6ebc42
DC
1031 cur->bc_mp->m_bsize * count, cur->bc_ops->buf_ops);
1032}
1033
b194c7d8
BN
1034/*
1035 * Set the buffer for level "lev" in the cursor to bp, releasing
1036 * any previous buffer.
1037 */
56b2de80 1038STATIC void
b194c7d8
BN
1039xfs_btree_setbuf(
1040 xfs_btree_cur_t *cur, /* btree cursor */
1041 int lev, /* level in btree */
1042 xfs_buf_t *bp) /* new buffer to set */
1043{
b3563c19 1044 struct xfs_btree_block *b; /* btree block */
b194c7d8 1045
56b2de80
DC
1046 if (cur->bc_bufs[lev])
1047 xfs_trans_brelse(cur->bc_tp, cur->bc_bufs[lev]);
2bd0ea18
NS
1048 cur->bc_bufs[lev] = bp;
1049 cur->bc_ra[lev] = 0;
56b2de80 1050
2bd0ea18 1051 b = XFS_BUF_TO_BLOCK(bp);
b194c7d8 1052 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
5a35bf2c 1053 if (b->bb_u.l.bb_leftsib == cpu_to_be64(NULLFSBLOCK))
2bd0ea18 1054 cur->bc_ra[lev] |= XFS_BTCUR_LEFTRA;
5a35bf2c 1055 if (b->bb_u.l.bb_rightsib == cpu_to_be64(NULLFSBLOCK))
2bd0ea18
NS
1056 cur->bc_ra[lev] |= XFS_BTCUR_RIGHTRA;
1057 } else {
a2ceac1f 1058 if (b->bb_u.s.bb_leftsib == cpu_to_be32(NULLAGBLOCK))
2bd0ea18 1059 cur->bc_ra[lev] |= XFS_BTCUR_LEFTRA;
a2ceac1f 1060 if (b->bb_u.s.bb_rightsib == cpu_to_be32(NULLAGBLOCK))
2bd0ea18
NS
1061 cur->bc_ra[lev] |= XFS_BTCUR_RIGHTRA;
1062 }
1063}
b194c7d8 1064
15c4e6e4 1065bool
b194c7d8
BN
1066xfs_btree_ptr_is_null(
1067 struct xfs_btree_cur *cur,
1068 union xfs_btree_ptr *ptr)
1069{
1070 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
5a35bf2c 1071 return ptr->l == cpu_to_be64(NULLFSBLOCK);
b194c7d8 1072 else
a2ceac1f 1073 return ptr->s == cpu_to_be32(NULLAGBLOCK);
b194c7d8
BN
1074}
1075
1076STATIC void
1077xfs_btree_set_ptr_null(
1078 struct xfs_btree_cur *cur,
1079 union xfs_btree_ptr *ptr)
1080{
1081 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
5a35bf2c 1082 ptr->l = cpu_to_be64(NULLFSBLOCK);
b194c7d8
BN
1083 else
1084 ptr->s = cpu_to_be32(NULLAGBLOCK);
1085}
1086
1087/*
1088 * Get/set/init sibling pointers
1089 */
15c4e6e4 1090void
b194c7d8
BN
1091xfs_btree_get_sibling(
1092 struct xfs_btree_cur *cur,
1093 struct xfs_btree_block *block,
1094 union xfs_btree_ptr *ptr,
1095 int lr)
1096{
1097 ASSERT(lr == XFS_BB_LEFTSIB || lr == XFS_BB_RIGHTSIB);
1098
1099 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
1100 if (lr == XFS_BB_RIGHTSIB)
1101 ptr->l = block->bb_u.l.bb_rightsib;
1102 else
1103 ptr->l = block->bb_u.l.bb_leftsib;
1104 } else {
1105 if (lr == XFS_BB_RIGHTSIB)
1106 ptr->s = block->bb_u.s.bb_rightsib;
1107 else
1108 ptr->s = block->bb_u.s.bb_leftsib;
1109 }
1110}
1111
1112STATIC void
1113xfs_btree_set_sibling(
1114 struct xfs_btree_cur *cur,
1115 struct xfs_btree_block *block,
1116 union xfs_btree_ptr *ptr,
1117 int lr)
1118{
1119 ASSERT(lr == XFS_BB_LEFTSIB || lr == XFS_BB_RIGHTSIB);
1120
1121 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
1122 if (lr == XFS_BB_RIGHTSIB)
1123 block->bb_u.l.bb_rightsib = ptr->l;
1124 else
1125 block->bb_u.l.bb_leftsib = ptr->l;
1126 } else {
1127 if (lr == XFS_BB_RIGHTSIB)
1128 block->bb_u.s.bb_rightsib = ptr->s;
1129 else
1130 block->bb_u.s.bb_leftsib = ptr->s;
1131 }
1132}
1133
5dfa5cd2
DC
1134void
1135xfs_btree_init_block_int(
1136 struct xfs_mount *mp,
1137 struct xfs_btree_block *buf,
1138 xfs_daddr_t blkno,
e394a4b1 1139 xfs_btnum_t btnum,
5dfa5cd2
DC
1140 __u16 level,
1141 __u16 numrecs,
1142 __u64 owner,
1143 unsigned int flags)
1144{
f4241a08 1145 int crc = xfs_sb_version_hascrc(&mp->m_sb);
e394a4b1 1146 __u32 magic = xfs_btree_magic(crc, btnum);
f4241a08 1147
5dfa5cd2
DC
1148 buf->bb_magic = cpu_to_be32(magic);
1149 buf->bb_level = cpu_to_be16(level);
1150 buf->bb_numrecs = cpu_to_be16(numrecs);
1151
1152 if (flags & XFS_BTREE_LONG_PTRS) {
5a35bf2c
DC
1153 buf->bb_u.l.bb_leftsib = cpu_to_be64(NULLFSBLOCK);
1154 buf->bb_u.l.bb_rightsib = cpu_to_be64(NULLFSBLOCK);
f4241a08 1155 if (crc) {
5dfa5cd2
DC
1156 buf->bb_u.l.bb_blkno = cpu_to_be64(blkno);
1157 buf->bb_u.l.bb_owner = cpu_to_be64(owner);
9c4e12fb 1158 uuid_copy(&buf->bb_u.l.bb_uuid, &mp->m_sb.sb_meta_uuid);
5dfa5cd2 1159 buf->bb_u.l.bb_pad = 0;
6f9ea829 1160 buf->bb_u.l.bb_lsn = 0;
5dfa5cd2
DC
1161 }
1162 } else {
1163 /* owner is a 32 bit value on short blocks */
1164 __u32 __owner = (__u32)owner;
1165
1166 buf->bb_u.s.bb_leftsib = cpu_to_be32(NULLAGBLOCK);
1167 buf->bb_u.s.bb_rightsib = cpu_to_be32(NULLAGBLOCK);
f4241a08 1168 if (crc) {
5dfa5cd2
DC
1169 buf->bb_u.s.bb_blkno = cpu_to_be64(blkno);
1170 buf->bb_u.s.bb_owner = cpu_to_be32(__owner);
9c4e12fb 1171 uuid_copy(&buf->bb_u.s.bb_uuid, &mp->m_sb.sb_meta_uuid);
6f9ea829 1172 buf->bb_u.s.bb_lsn = 0;
5dfa5cd2
DC
1173 }
1174 }
1175}
1176
a2ceac1f 1177void
b194c7d8 1178xfs_btree_init_block(
a2ceac1f
DC
1179 struct xfs_mount *mp,
1180 struct xfs_buf *bp,
e394a4b1 1181 xfs_btnum_t btnum,
a2ceac1f
DC
1182 __u16 level,
1183 __u16 numrecs,
5dfa5cd2 1184 __u64 owner,
a2ceac1f 1185 unsigned int flags)
b194c7d8 1186{
5dfa5cd2 1187 xfs_btree_init_block_int(mp, XFS_BUF_TO_BLOCK(bp), bp->b_bn,
e394a4b1 1188 btnum, level, numrecs, owner, flags);
b194c7d8
BN
1189}
1190
a2ceac1f
DC
1191STATIC void
1192xfs_btree_init_block_cur(
1193 struct xfs_btree_cur *cur,
5dfa5cd2 1194 struct xfs_buf *bp,
a2ceac1f 1195 int level,
5dfa5cd2 1196 int numrecs)
a2ceac1f 1197{
4c6b3277 1198 __u64 owner;
5dfa5cd2
DC
1199
1200 /*
1201 * we can pull the owner from the cursor right now as the different
1202 * owners align directly with the pointer size of the btree. This may
1203 * change in future, but is safe for current users of the generic btree
1204 * code.
1205 */
1206 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
1207 owner = cur->bc_private.b.ip->i_ino;
1208 else
1209 owner = cur->bc_private.a.agno;
1210
1211 xfs_btree_init_block_int(cur->bc_mp, XFS_BUF_TO_BLOCK(bp), bp->b_bn,
e394a4b1 1212 cur->bc_btnum, level, numrecs,
5dfa5cd2 1213 owner, cur->bc_flags);
a2ceac1f
DC
1214}
1215
b194c7d8
BN
1216/*
1217 * Return true if ptr is the last record in the btree and
5dfa5cd2 1218 * we need to track updates to this record. The decision
b194c7d8
BN
1219 * will be further refined in the update_lastrec method.
1220 */
1221STATIC int
1222xfs_btree_is_lastrec(
1223 struct xfs_btree_cur *cur,
1224 struct xfs_btree_block *block,
1225 int level)
1226{
1227 union xfs_btree_ptr ptr;
1228
1229 if (level > 0)
1230 return 0;
1231 if (!(cur->bc_flags & XFS_BTREE_LASTREC_UPDATE))
1232 return 0;
1233
1234 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_RIGHTSIB);
1235 if (!xfs_btree_ptr_is_null(cur, &ptr))
1236 return 0;
1237 return 1;
1238}
1239
1240STATIC void
1241xfs_btree_buf_to_ptr(
1242 struct xfs_btree_cur *cur,
1243 struct xfs_buf *bp,
1244 union xfs_btree_ptr *ptr)
1245{
1246 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
1247 ptr->l = cpu_to_be64(XFS_DADDR_TO_FSB(cur->bc_mp,
1248 XFS_BUF_ADDR(bp)));
1249 else {
56b2de80 1250 ptr->s = cpu_to_be32(xfs_daddr_to_agbno(cur->bc_mp,
b194c7d8
BN
1251 XFS_BUF_ADDR(bp)));
1252 }
1253}
1254
b194c7d8
BN
1255STATIC void
1256xfs_btree_set_refs(
1257 struct xfs_btree_cur *cur,
1258 struct xfs_buf *bp)
1259{
1260 switch (cur->bc_btnum) {
1261 case XFS_BTNUM_BNO:
1262 case XFS_BTNUM_CNT:
a2ceac1f 1263 xfs_buf_set_ref(bp, XFS_ALLOC_BTREE_REF);
b194c7d8
BN
1264 break;
1265 case XFS_BTNUM_INO:
c0a4c227 1266 case XFS_BTNUM_FINO:
a2ceac1f 1267 xfs_buf_set_ref(bp, XFS_INO_BTREE_REF);
b194c7d8
BN
1268 break;
1269 case XFS_BTNUM_BMAP:
a2ceac1f 1270 xfs_buf_set_ref(bp, XFS_BMAP_BTREE_REF);
b194c7d8 1271 break;
b3a96b46
DW
1272 case XFS_BTNUM_RMAP:
1273 xfs_buf_set_ref(bp, XFS_RMAP_BTREE_REF);
1274 break;
d8079fe0
DW
1275 case XFS_BTNUM_REFC:
1276 xfs_buf_set_ref(bp, XFS_REFC_BTREE_REF);
1277 break;
b194c7d8
BN
1278 default:
1279 ASSERT(0);
1280 }
1281}
1282
1283STATIC int
1284xfs_btree_get_buf_block(
1285 struct xfs_btree_cur *cur,
1286 union xfs_btree_ptr *ptr,
1287 int flags,
1288 struct xfs_btree_block **block,
1289 struct xfs_buf **bpp)
1290{
1291 struct xfs_mount *mp = cur->bc_mp;
1292 xfs_daddr_t d;
7ab54cd6 1293 int error;
b194c7d8
BN
1294
1295 /* need to sort out how callers deal with failures first */
56b2de80 1296 ASSERT(!(flags & XBF_TRYLOCK));
b194c7d8 1297
7ab54cd6
DW
1298 error = xfs_btree_ptr_to_daddr(cur, ptr, &d);
1299 if (error)
1300 return error;
b194c7d8
BN
1301 *bpp = xfs_trans_get_buf(cur->bc_tp, mp->m_ddev_targp, d,
1302 mp->m_bsize, flags);
1303
a2ceac1f 1304 if (!*bpp)
12b53197 1305 return -ENOMEM;
b194c7d8 1306
a2ceac1f 1307 (*bpp)->b_ops = cur->bc_ops->buf_ops;
b194c7d8
BN
1308 *block = XFS_BUF_TO_BLOCK(*bpp);
1309 return 0;
1310}
1311
1312/*
1313 * Read in the buffer at the given ptr and return the buffer and
1314 * the block pointer within the buffer.
1315 */
1316STATIC int
1317xfs_btree_read_buf_block(
1318 struct xfs_btree_cur *cur,
1319 union xfs_btree_ptr *ptr,
b194c7d8
BN
1320 int flags,
1321 struct xfs_btree_block **block,
1322 struct xfs_buf **bpp)
1323{
1324 struct xfs_mount *mp = cur->bc_mp;
1325 xfs_daddr_t d;
1326 int error;
1327
1328 /* need to sort out how callers deal with failures first */
56b2de80 1329 ASSERT(!(flags & XBF_TRYLOCK));
b194c7d8 1330
7ab54cd6
DW
1331 error = xfs_btree_ptr_to_daddr(cur, ptr, &d);
1332 if (error)
1333 return error;
b194c7d8 1334 error = xfs_trans_read_buf(mp, cur->bc_tp, mp->m_ddev_targp, d,
a2ceac1f
DC
1335 mp->m_bsize, flags, bpp,
1336 cur->bc_ops->buf_ops);
b194c7d8
BN
1337 if (error)
1338 return error;
1339
b194c7d8
BN
1340 xfs_btree_set_refs(cur, *bpp);
1341 *block = XFS_BUF_TO_BLOCK(*bpp);
a2ceac1f 1342 return 0;
b194c7d8
BN
1343}
1344
1345/*
1346 * Copy keys from one btree block to another.
1347 */
1348STATIC void
1349xfs_btree_copy_keys(
1350 struct xfs_btree_cur *cur,
1351 union xfs_btree_key *dst_key,
1352 union xfs_btree_key *src_key,
1353 int numkeys)
1354{
1355 ASSERT(numkeys >= 0);
1356 memcpy(dst_key, src_key, numkeys * cur->bc_ops->key_len);
1357}
1358
1359/*
1360 * Copy records from one btree block to another.
1361 */
1362STATIC void
1363xfs_btree_copy_recs(
1364 struct xfs_btree_cur *cur,
1365 union xfs_btree_rec *dst_rec,
1366 union xfs_btree_rec *src_rec,
1367 int numrecs)
1368{
1369 ASSERT(numrecs >= 0);
1370 memcpy(dst_rec, src_rec, numrecs * cur->bc_ops->rec_len);
1371}
1372
1373/*
1374 * Copy block pointers from one btree block to another.
1375 */
1376STATIC void
1377xfs_btree_copy_ptrs(
1378 struct xfs_btree_cur *cur,
1379 union xfs_btree_ptr *dst_ptr,
1380 union xfs_btree_ptr *src_ptr,
1381 int numptrs)
1382{
1383 ASSERT(numptrs >= 0);
1384 memcpy(dst_ptr, src_ptr, numptrs * xfs_btree_ptr_len(cur));
1385}
1386
1387/*
1388 * Shift keys one index left/right inside a single btree block.
1389 */
1390STATIC void
1391xfs_btree_shift_keys(
1392 struct xfs_btree_cur *cur,
1393 union xfs_btree_key *key,
1394 int dir,
1395 int numkeys)
1396{
1397 char *dst_key;
1398
1399 ASSERT(numkeys >= 0);
1400 ASSERT(dir == 1 || dir == -1);
1401
1402 dst_key = (char *)key + (dir * cur->bc_ops->key_len);
1403 memmove(dst_key, key, numkeys * cur->bc_ops->key_len);
1404}
1405
1406/*
1407 * Shift records one index left/right inside a single btree block.
1408 */
1409STATIC void
1410xfs_btree_shift_recs(
1411 struct xfs_btree_cur *cur,
1412 union xfs_btree_rec *rec,
1413 int dir,
1414 int numrecs)
1415{
1416 char *dst_rec;
1417
1418 ASSERT(numrecs >= 0);
1419 ASSERT(dir == 1 || dir == -1);
1420
1421 dst_rec = (char *)rec + (dir * cur->bc_ops->rec_len);
1422 memmove(dst_rec, rec, numrecs * cur->bc_ops->rec_len);
1423}
1424
1425/*
1426 * Shift block pointers one index left/right inside a single btree block.
1427 */
1428STATIC void
1429xfs_btree_shift_ptrs(
1430 struct xfs_btree_cur *cur,
1431 union xfs_btree_ptr *ptr,
1432 int dir,
1433 int numptrs)
1434{
1435 char *dst_ptr;
1436
1437 ASSERT(numptrs >= 0);
1438 ASSERT(dir == 1 || dir == -1);
1439
1440 dst_ptr = (char *)ptr + (dir * xfs_btree_ptr_len(cur));
1441 memmove(dst_ptr, ptr, numptrs * xfs_btree_ptr_len(cur));
1442}
1443
1444/*
1445 * Log key values from the btree block.
1446 */
1447STATIC void
1448xfs_btree_log_keys(
1449 struct xfs_btree_cur *cur,
1450 struct xfs_buf *bp,
1451 int first,
1452 int last)
1453{
b194c7d8
BN
1454
1455 if (bp) {
bdc16ee5 1456 xfs_trans_buf_set_type(cur->bc_tp, bp, XFS_BLFT_BTREE_BUF);
b194c7d8
BN
1457 xfs_trans_log_buf(cur->bc_tp, bp,
1458 xfs_btree_key_offset(cur, first),
1459 xfs_btree_key_offset(cur, last + 1) - 1);
1460 } else {
1461 xfs_trans_log_inode(cur->bc_tp, cur->bc_private.b.ip,
1462 xfs_ilog_fbroot(cur->bc_private.b.whichfork));
1463 }
b194c7d8
BN
1464}
1465
1466/*
1467 * Log record values from the btree block.
1468 */
1469void
1470xfs_btree_log_recs(
1471 struct xfs_btree_cur *cur,
1472 struct xfs_buf *bp,
1473 int first,
1474 int last)
1475{
b194c7d8 1476
bdc16ee5 1477 xfs_trans_buf_set_type(cur->bc_tp, bp, XFS_BLFT_BTREE_BUF);
b194c7d8
BN
1478 xfs_trans_log_buf(cur->bc_tp, bp,
1479 xfs_btree_rec_offset(cur, first),
1480 xfs_btree_rec_offset(cur, last + 1) - 1);
1481
b194c7d8
BN
1482}
1483
1484/*
1485 * Log block pointer fields from a btree block (nonleaf).
1486 */
1487STATIC void
1488xfs_btree_log_ptrs(
1489 struct xfs_btree_cur *cur, /* btree cursor */
1490 struct xfs_buf *bp, /* buffer containing btree block */
1491 int first, /* index of first pointer to log */
1492 int last) /* index of last pointer to log */
1493{
b194c7d8
BN
1494
1495 if (bp) {
1496 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
1497 int level = xfs_btree_get_level(block);
1498
bdc16ee5 1499 xfs_trans_buf_set_type(cur->bc_tp, bp, XFS_BLFT_BTREE_BUF);
b194c7d8
BN
1500 xfs_trans_log_buf(cur->bc_tp, bp,
1501 xfs_btree_ptr_offset(cur, first, level),
1502 xfs_btree_ptr_offset(cur, last + 1, level) - 1);
1503 } else {
1504 xfs_trans_log_inode(cur->bc_tp, cur->bc_private.b.ip,
1505 xfs_ilog_fbroot(cur->bc_private.b.whichfork));
1506 }
1507
b194c7d8
BN
1508}
1509
1510/*
1511 * Log fields from a btree block header.
1512 */
1513void
1514xfs_btree_log_block(
1515 struct xfs_btree_cur *cur, /* btree cursor */
1516 struct xfs_buf *bp, /* buffer containing btree block */
1517 int fields) /* mask of fields: XFS_BB_... */
1518{
1519 int first; /* first byte offset logged */
1520 int last; /* last byte offset logged */
1521 static const short soffsets[] = { /* table of offsets (short) */
b3563c19
BN
1522 offsetof(struct xfs_btree_block, bb_magic),
1523 offsetof(struct xfs_btree_block, bb_level),
1524 offsetof(struct xfs_btree_block, bb_numrecs),
1525 offsetof(struct xfs_btree_block, bb_u.s.bb_leftsib),
1526 offsetof(struct xfs_btree_block, bb_u.s.bb_rightsib),
5dfa5cd2
DC
1527 offsetof(struct xfs_btree_block, bb_u.s.bb_blkno),
1528 offsetof(struct xfs_btree_block, bb_u.s.bb_lsn),
1529 offsetof(struct xfs_btree_block, bb_u.s.bb_uuid),
1530 offsetof(struct xfs_btree_block, bb_u.s.bb_owner),
1531 offsetof(struct xfs_btree_block, bb_u.s.bb_crc),
e0607266 1532 XFS_BTREE_SBLOCK_CRC_LEN
b194c7d8
BN
1533 };
1534 static const short loffsets[] = { /* table of offsets (long) */
b3563c19
BN
1535 offsetof(struct xfs_btree_block, bb_magic),
1536 offsetof(struct xfs_btree_block, bb_level),
1537 offsetof(struct xfs_btree_block, bb_numrecs),
1538 offsetof(struct xfs_btree_block, bb_u.l.bb_leftsib),
1539 offsetof(struct xfs_btree_block, bb_u.l.bb_rightsib),
5dfa5cd2
DC
1540 offsetof(struct xfs_btree_block, bb_u.l.bb_blkno),
1541 offsetof(struct xfs_btree_block, bb_u.l.bb_lsn),
1542 offsetof(struct xfs_btree_block, bb_u.l.bb_uuid),
1543 offsetof(struct xfs_btree_block, bb_u.l.bb_owner),
1544 offsetof(struct xfs_btree_block, bb_u.l.bb_crc),
1545 offsetof(struct xfs_btree_block, bb_u.l.bb_pad),
e0607266 1546 XFS_BTREE_LBLOCK_CRC_LEN
b194c7d8
BN
1547 };
1548
b194c7d8 1549 if (bp) {
5dfa5cd2
DC
1550 int nbits;
1551
1552 if (cur->bc_flags & XFS_BTREE_CRC_BLOCKS) {
1553 /*
1554 * We don't log the CRC when updating a btree
1555 * block but instead recreate it during log
1556 * recovery. As the log buffers have checksums
10851b18 1557 * of their own this is safe and avoids logging a crc
5dfa5cd2
DC
1558 * update in a lot of places.
1559 */
1560 if (fields == XFS_BB_ALL_BITS)
1561 fields = XFS_BB_ALL_BITS_CRC;
1562 nbits = XFS_BB_NUM_BITS_CRC;
1563 } else {
1564 nbits = XFS_BB_NUM_BITS;
1565 }
b194c7d8
BN
1566 xfs_btree_offsets(fields,
1567 (cur->bc_flags & XFS_BTREE_LONG_PTRS) ?
1568 loffsets : soffsets,
5dfa5cd2 1569 nbits, &first, &last);
bdc16ee5 1570 xfs_trans_buf_set_type(cur->bc_tp, bp, XFS_BLFT_BTREE_BUF);
b194c7d8
BN
1571 xfs_trans_log_buf(cur->bc_tp, bp, first, last);
1572 } else {
1573 xfs_trans_log_inode(cur->bc_tp, cur->bc_private.b.ip,
1574 xfs_ilog_fbroot(cur->bc_private.b.whichfork));
1575 }
b194c7d8
BN
1576}
1577
1578/*
1579 * Increment cursor by one record at the level.
1580 * For nonzero levels the leaf-ward information is untouched.
1581 */
1582int /* error */
1583xfs_btree_increment(
1584 struct xfs_btree_cur *cur,
1585 int level,
1586 int *stat) /* success/failure */
1587{
1588 struct xfs_btree_block *block;
1589 union xfs_btree_ptr ptr;
1590 struct xfs_buf *bp;
1591 int error; /* error return value */
1592 int lev;
1593
b194c7d8
BN
1594 ASSERT(level < cur->bc_nlevels);
1595
1596 /* Read-ahead to the right at this level. */
1597 xfs_btree_readahead(cur, level, XFS_BTCUR_RIGHTRA);
1598
1599 /* Get a pointer to the btree block. */
1600 block = xfs_btree_get_block(cur, level, &bp);
1601
1602#ifdef DEBUG
1603 error = xfs_btree_check_block(cur, block, level, bp);
1604 if (error)
1605 goto error0;
1606#endif
1607
1608 /* We're done if we remain in the block after the increment. */
1609 if (++cur->bc_ptrs[level] <= xfs_btree_get_numrecs(block))
1610 goto out1;
1611
1612 /* Fail if we just went off the right edge of the tree. */
1613 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_RIGHTSIB);
1614 if (xfs_btree_ptr_is_null(cur, &ptr))
1615 goto out0;
1616
1617 XFS_BTREE_STATS_INC(cur, increment);
1618
1619 /*
1620 * March up the tree incrementing pointers.
1621 * Stop when we don't go off the right edge of a block.
1622 */
1623 for (lev = level + 1; lev < cur->bc_nlevels; lev++) {
1624 block = xfs_btree_get_block(cur, lev, &bp);
1625
1626#ifdef DEBUG
1627 error = xfs_btree_check_block(cur, block, lev, bp);
1628 if (error)
1629 goto error0;
1630#endif
1631
1632 if (++cur->bc_ptrs[lev] <= xfs_btree_get_numrecs(block))
1633 break;
1634
1635 /* Read-ahead the right block for the next loop. */
1636 xfs_btree_readahead(cur, lev, XFS_BTCUR_RIGHTRA);
1637 }
1638
1639 /*
1640 * If we went off the root then we are either seriously
1641 * confused or have the tree root in an inode.
1642 */
1643 if (lev == cur->bc_nlevels) {
1644 if (cur->bc_flags & XFS_BTREE_ROOT_IN_INODE)
1645 goto out0;
1646 ASSERT(0);
12b53197 1647 error = -EFSCORRUPTED;
b194c7d8
BN
1648 goto error0;
1649 }
1650 ASSERT(lev < cur->bc_nlevels);
1651
1652 /*
1653 * Now walk back down the tree, fixing up the cursor's buffer
1654 * pointers and key numbers.
1655 */
1656 for (block = xfs_btree_get_block(cur, lev, &bp); lev > level; ) {
1657 union xfs_btree_ptr *ptrp;
1658
1659 ptrp = xfs_btree_ptr_addr(cur, cur->bc_ptrs[lev], block);
ff105f75
DC
1660 --lev;
1661 error = xfs_btree_read_buf_block(cur, ptrp, 0, &block, &bp);
b194c7d8
BN
1662 if (error)
1663 goto error0;
1664
1665 xfs_btree_setbuf(cur, lev, bp);
1666 cur->bc_ptrs[lev] = 1;
1667 }
1668out1:
b194c7d8
BN
1669 *stat = 1;
1670 return 0;
1671
1672out0:
b194c7d8
BN
1673 *stat = 0;
1674 return 0;
1675
1676error0:
b194c7d8
BN
1677 return error;
1678}
1679
1680/*
1681 * Decrement cursor by one record at the level.
1682 * For nonzero levels the leaf-ward information is untouched.
1683 */
1684int /* error */
1685xfs_btree_decrement(
1686 struct xfs_btree_cur *cur,
1687 int level,
1688 int *stat) /* success/failure */
1689{
1690 struct xfs_btree_block *block;
1691 xfs_buf_t *bp;
1692 int error; /* error return value */
1693 int lev;
1694 union xfs_btree_ptr ptr;
1695
b194c7d8
BN
1696 ASSERT(level < cur->bc_nlevels);
1697
1698 /* Read-ahead to the left at this level. */
1699 xfs_btree_readahead(cur, level, XFS_BTCUR_LEFTRA);
1700
1701 /* We're done if we remain in the block after the decrement. */
1702 if (--cur->bc_ptrs[level] > 0)
1703 goto out1;
1704
1705 /* Get a pointer to the btree block. */
1706 block = xfs_btree_get_block(cur, level, &bp);
1707
1708#ifdef DEBUG
1709 error = xfs_btree_check_block(cur, block, level, bp);
1710 if (error)
1711 goto error0;
1712#endif
1713
1714 /* Fail if we just went off the left edge of the tree. */
1715 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_LEFTSIB);
1716 if (xfs_btree_ptr_is_null(cur, &ptr))
1717 goto out0;
1718
1719 XFS_BTREE_STATS_INC(cur, decrement);
1720
1721 /*
1722 * March up the tree decrementing pointers.
1723 * Stop when we don't go off the left edge of a block.
1724 */
1725 for (lev = level + 1; lev < cur->bc_nlevels; lev++) {
1726 if (--cur->bc_ptrs[lev] > 0)
1727 break;
1728 /* Read-ahead the left block for the next loop. */
1729 xfs_btree_readahead(cur, lev, XFS_BTCUR_LEFTRA);
1730 }
1731
1732 /*
1733 * If we went off the root then we are seriously confused.
1734 * or the root of the tree is in an inode.
1735 */
1736 if (lev == cur->bc_nlevels) {
1737 if (cur->bc_flags & XFS_BTREE_ROOT_IN_INODE)
1738 goto out0;
1739 ASSERT(0);
12b53197 1740 error = -EFSCORRUPTED;
b194c7d8
BN
1741 goto error0;
1742 }
1743 ASSERT(lev < cur->bc_nlevels);
1744
1745 /*
1746 * Now walk back down the tree, fixing up the cursor's buffer
1747 * pointers and key numbers.
1748 */
1749 for (block = xfs_btree_get_block(cur, lev, &bp); lev > level; ) {
1750 union xfs_btree_ptr *ptrp;
1751
1752 ptrp = xfs_btree_ptr_addr(cur, cur->bc_ptrs[lev], block);
ff105f75
DC
1753 --lev;
1754 error = xfs_btree_read_buf_block(cur, ptrp, 0, &block, &bp);
b194c7d8
BN
1755 if (error)
1756 goto error0;
1757 xfs_btree_setbuf(cur, lev, bp);
1758 cur->bc_ptrs[lev] = xfs_btree_get_numrecs(block);
1759 }
1760out1:
b194c7d8
BN
1761 *stat = 1;
1762 return 0;
1763
1764out0:
b194c7d8
BN
1765 *stat = 0;
1766 return 0;
1767
1768error0:
b194c7d8
BN
1769 return error;
1770}
1771
50bb67d6 1772int
b194c7d8
BN
1773xfs_btree_lookup_get_block(
1774 struct xfs_btree_cur *cur, /* btree cursor */
1775 int level, /* level in the btree */
1776 union xfs_btree_ptr *pp, /* ptr to btree block */
1777 struct xfs_btree_block **blkp) /* return btree block */
1778{
1779 struct xfs_buf *bp; /* buffer pointer for btree block */
7ab54cd6 1780 xfs_daddr_t daddr;
b194c7d8
BN
1781 int error = 0;
1782
1783 /* special case the root block if in an inode */
1784 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
1785 (level == cur->bc_nlevels - 1)) {
1786 *blkp = xfs_btree_get_iroot(cur);
1787 return 0;
1788 }
1789
1790 /*
1791 * If the old buffer at this level for the disk address we are
1792 * looking for re-use it.
1793 *
1794 * Otherwise throw it away and get a new one.
1795 */
1796 bp = cur->bc_bufs[level];
7ab54cd6
DW
1797 error = xfs_btree_ptr_to_daddr(cur, pp, &daddr);
1798 if (error)
1799 return error;
1800 if (bp && XFS_BUF_ADDR(bp) == daddr) {
b194c7d8
BN
1801 *blkp = XFS_BUF_TO_BLOCK(bp);
1802 return 0;
1803 }
1804
ff105f75 1805 error = xfs_btree_read_buf_block(cur, pp, 0, blkp, &bp);
b194c7d8
BN
1806 if (error)
1807 return error;
1808
312eea24
DW
1809 /* Check the inode owner since the verifiers don't. */
1810 if (xfs_sb_version_hascrc(&cur->bc_mp->m_sb) &&
c7ba1731 1811 !(cur->bc_private.b.flags & XFS_BTCUR_BPRV_INVALID_OWNER) &&
312eea24
DW
1812 (cur->bc_flags & XFS_BTREE_LONG_PTRS) &&
1813 be64_to_cpu((*blkp)->bb_u.l.bb_owner) !=
1814 cur->bc_private.b.ip->i_ino)
1815 goto out_bad;
1816
1817 /* Did we get the level we were looking for? */
1818 if (be16_to_cpu((*blkp)->bb_level) != level)
1819 goto out_bad;
1820
1821 /* Check that internal nodes have at least one record. */
1822 if (level != 0 && be16_to_cpu((*blkp)->bb_numrecs) == 0)
1823 goto out_bad;
1824
b194c7d8
BN
1825 xfs_btree_setbuf(cur, level, bp);
1826 return 0;
312eea24
DW
1827
1828out_bad:
1829 *blkp = NULL;
1830 xfs_trans_brelse(cur->bc_tp, bp);
1831 return -EFSCORRUPTED;
b194c7d8
BN
1832}
1833
1834/*
1835 * Get current search key. For level 0 we don't actually have a key
1836 * structure so we make one up from the record. For all other levels
1837 * we just return the right key.
1838 */
1839STATIC union xfs_btree_key *
1840xfs_lookup_get_search_key(
1841 struct xfs_btree_cur *cur,
1842 int level,
1843 int keyno,
1844 struct xfs_btree_block *block,
1845 union xfs_btree_key *kp)
1846{
1847 if (level == 0) {
1848 cur->bc_ops->init_key_from_rec(kp,
1849 xfs_btree_rec_addr(cur, keyno, block));
1850 return kp;
1851 }
1852
1853 return xfs_btree_key_addr(cur, keyno, block);
1854}
1855
1856/*
1857 * Lookup the record. The cursor is made to point to it, based on dir.
10851b18 1858 * stat is set to 0 if can't find any such record, 1 for success.
b194c7d8
BN
1859 */
1860int /* error */
1861xfs_btree_lookup(
1862 struct xfs_btree_cur *cur, /* btree cursor */
1863 xfs_lookup_t dir, /* <=, ==, or >= */
1864 int *stat) /* success/failure */
1865{
1866 struct xfs_btree_block *block; /* current btree block */
4a492e72 1867 int64_t diff; /* difference for the current key */
b194c7d8
BN
1868 int error; /* error return value */
1869 int keyno; /* current key number */
1870 int level; /* level in the btree */
1871 union xfs_btree_ptr *pp; /* ptr to btree block */
1872 union xfs_btree_ptr ptr; /* ptr to btree block */
1873
b194c7d8
BN
1874 XFS_BTREE_STATS_INC(cur, lookup);
1875
574b4153
DW
1876 /* No such thing as a zero-level tree. */
1877 if (cur->bc_nlevels == 0)
1878 return -EFSCORRUPTED;
1879
b194c7d8
BN
1880 block = NULL;
1881 keyno = 0;
1882
1883 /* initialise start pointer from cursor */
1884 cur->bc_ops->init_ptr_from_cur(cur, &ptr);
1885 pp = &ptr;
1886
1887 /*
1888 * Iterate over each level in the btree, starting at the root.
1889 * For each level above the leaves, find the key we need, based
1890 * on the lookup record, then follow the corresponding block
1891 * pointer down to the next level.
1892 */
1893 for (level = cur->bc_nlevels - 1, diff = 1; level >= 0; level--) {
1894 /* Get the block we need to do the lookup on. */
1895 error = xfs_btree_lookup_get_block(cur, level, pp, &block);
1896 if (error)
1897 goto error0;
1898
1899 if (diff == 0) {
1900 /*
1901 * If we already had a key match at a higher level, we
1902 * know we need to use the first entry in this block.
1903 */
1904 keyno = 1;
1905 } else {
1906 /* Otherwise search this block. Do a binary search. */
1907
1908 int high; /* high entry number */
1909 int low; /* low entry number */
1910
1911 /* Set low and high entry numbers, 1-based. */
1912 low = 1;
1913 high = xfs_btree_get_numrecs(block);
1914 if (!high) {
1915 /* Block is empty, must be an empty leaf. */
0e64ec0d
DW
1916 if (level != 0 || cur->bc_nlevels != 1) {
1917 XFS_CORRUPTION_ERROR(__func__,
1918 XFS_ERRLEVEL_LOW,
3177b915
DW
1919 cur->bc_mp, block,
1920 sizeof(*block));
0e64ec0d
DW
1921 return -EFSCORRUPTED;
1922 }
b194c7d8
BN
1923
1924 cur->bc_ptrs[0] = dir != XFS_LOOKUP_LE;
b194c7d8
BN
1925 *stat = 0;
1926 return 0;
1927 }
1928
1929 /* Binary search the block. */
1930 while (low <= high) {
1931 union xfs_btree_key key;
1932 union xfs_btree_key *kp;
1933
1934 XFS_BTREE_STATS_INC(cur, compare);
1935
1936 /* keyno is average of low and high. */
1937 keyno = (low + high) >> 1;
1938
1939 /* Get current search key */
1940 kp = xfs_lookup_get_search_key(cur, level,
1941 keyno, block, &key);
1942
1943 /*
1944 * Compute difference to get next direction:
1945 * - less than, move right
1946 * - greater than, move left
1947 * - equal, we're done
1948 */
1949 diff = cur->bc_ops->key_diff(cur, kp);
1950 if (diff < 0)
1951 low = keyno + 1;
1952 else if (diff > 0)
1953 high = keyno - 1;
1954 else
1955 break;
1956 }
1957 }
1958
1959 /*
1960 * If there are more levels, set up for the next level
1961 * by getting the block number and filling in the cursor.
1962 */
1963 if (level > 0) {
1964 /*
1965 * If we moved left, need the previous key number,
1966 * unless there isn't one.
1967 */
1968 if (diff > 0 && --keyno < 1)
1969 keyno = 1;
1970 pp = xfs_btree_ptr_addr(cur, keyno, block);
1971
efa7dddb 1972 error = xfs_btree_debug_check_ptr(cur, pp, 0, level);
b194c7d8
BN
1973 if (error)
1974 goto error0;
efa7dddb 1975
b194c7d8
BN
1976 cur->bc_ptrs[level] = keyno;
1977 }
1978 }
1979
1980 /* Done with the search. See if we need to adjust the results. */
1981 if (dir != XFS_LOOKUP_LE && diff < 0) {
1982 keyno++;
1983 /*
1984 * If ge search and we went off the end of the block, but it's
1985 * not the last block, we're in the wrong block.
1986 */
1987 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_RIGHTSIB);
1988 if (dir == XFS_LOOKUP_GE &&
1989 keyno > xfs_btree_get_numrecs(block) &&
1990 !xfs_btree_ptr_is_null(cur, &ptr)) {
1991 int i;
1992
1993 cur->bc_ptrs[0] = keyno;
1994 error = xfs_btree_increment(cur, 0, &i);
1995 if (error)
1996 goto error0;
19ebedcf 1997 XFS_WANT_CORRUPTED_RETURN(cur->bc_mp, i == 1);
b194c7d8
BN
1998 *stat = 1;
1999 return 0;
2000 }
2001 } else if (dir == XFS_LOOKUP_LE && diff > 0)
2002 keyno--;
2003 cur->bc_ptrs[0] = keyno;
2004
2005 /* Return if we succeeded or not. */
2006 if (keyno == 0 || keyno > xfs_btree_get_numrecs(block))
2007 *stat = 0;
2008 else if (dir != XFS_LOOKUP_EQ || diff == 0)
2009 *stat = 1;
2010 else
2011 *stat = 0;
b194c7d8
BN
2012 return 0;
2013
2014error0:
b194c7d8
BN
2015 return error;
2016}
2017
13e831e0 2018/* Find the high key storage area from a regular key. */
0ad12e7f 2019union xfs_btree_key *
13e831e0
DW
2020xfs_btree_high_key_from_key(
2021 struct xfs_btree_cur *cur,
2022 union xfs_btree_key *key)
2023{
2024 ASSERT(cur->bc_flags & XFS_BTREE_OVERLAPPING);
2025 return (union xfs_btree_key *)((char *)key +
2026 (cur->bc_ops->key_len / 2));
2027}
2028
64dbe047
DW
2029/* Determine the low (and high if overlapped) keys of a leaf block */
2030STATIC void
2031xfs_btree_get_leaf_keys(
13e831e0
DW
2032 struct xfs_btree_cur *cur,
2033 struct xfs_btree_block *block,
2034 union xfs_btree_key *key)
2035{
13e831e0
DW
2036 union xfs_btree_key max_hkey;
2037 union xfs_btree_key hkey;
64dbe047 2038 union xfs_btree_rec *rec;
13e831e0 2039 union xfs_btree_key *high;
64dbe047 2040 int n;
13e831e0 2041
13e831e0
DW
2042 rec = xfs_btree_rec_addr(cur, 1, block);
2043 cur->bc_ops->init_key_from_rec(key, rec);
2044
64dbe047
DW
2045 if (cur->bc_flags & XFS_BTREE_OVERLAPPING) {
2046
2047 cur->bc_ops->init_high_key_from_rec(&max_hkey, rec);
2048 for (n = 2; n <= xfs_btree_get_numrecs(block); n++) {
2049 rec = xfs_btree_rec_addr(cur, n, block);
2050 cur->bc_ops->init_high_key_from_rec(&hkey, rec);
2051 if (cur->bc_ops->diff_two_keys(cur, &hkey, &max_hkey)
2052 > 0)
2053 max_hkey = hkey;
2054 }
13e831e0 2055
64dbe047
DW
2056 high = xfs_btree_high_key_from_key(cur, key);
2057 memcpy(high, &max_hkey, cur->bc_ops->key_len / 2);
2058 }
13e831e0
DW
2059}
2060
64dbe047
DW
2061/* Determine the low (and high if overlapped) keys of a node block */
2062STATIC void
2063xfs_btree_get_node_keys(
13e831e0
DW
2064 struct xfs_btree_cur *cur,
2065 struct xfs_btree_block *block,
2066 union xfs_btree_key *key)
2067{
13e831e0
DW
2068 union xfs_btree_key *hkey;
2069 union xfs_btree_key *max_hkey;
2070 union xfs_btree_key *high;
64dbe047 2071 int n;
13e831e0 2072
64dbe047
DW
2073 if (cur->bc_flags & XFS_BTREE_OVERLAPPING) {
2074 memcpy(key, xfs_btree_key_addr(cur, 1, block),
2075 cur->bc_ops->key_len / 2);
2076
2077 max_hkey = xfs_btree_high_key_addr(cur, 1, block);
2078 for (n = 2; n <= xfs_btree_get_numrecs(block); n++) {
2079 hkey = xfs_btree_high_key_addr(cur, n, block);
2080 if (cur->bc_ops->diff_two_keys(cur, hkey, max_hkey) > 0)
2081 max_hkey = hkey;
2082 }
13e831e0 2083
64dbe047
DW
2084 high = xfs_btree_high_key_from_key(cur, key);
2085 memcpy(high, max_hkey, cur->bc_ops->key_len / 2);
2086 } else {
2087 memcpy(key, xfs_btree_key_addr(cur, 1, block),
2088 cur->bc_ops->key_len);
13e831e0 2089 }
13e831e0
DW
2090}
2091
a3c9cb10 2092/* Derive the keys for any btree block. */
0ad12e7f 2093void
a3c9cb10
DW
2094xfs_btree_get_keys(
2095 struct xfs_btree_cur *cur,
2096 struct xfs_btree_block *block,
2097 union xfs_btree_key *key)
2098{
2099 if (be16_to_cpu(block->bb_level) == 0)
64dbe047 2100 xfs_btree_get_leaf_keys(cur, block, key);
a3c9cb10 2101 else
64dbe047 2102 xfs_btree_get_node_keys(cur, block, key);
a3c9cb10
DW
2103}
2104
b194c7d8 2105/*
a3c9cb10
DW
2106 * Decide if we need to update the parent keys of a btree block. For
2107 * a standard btree this is only necessary if we're updating the first
13e831e0
DW
2108 * record/key. For an overlapping btree, we must always update the
2109 * keys because the highest key can be in any of the records or keys
2110 * in the block.
b194c7d8 2111 */
a3c9cb10
DW
2112static inline bool
2113xfs_btree_needs_key_update(
2114 struct xfs_btree_cur *cur,
2115 int ptr)
2116{
13e831e0
DW
2117 return (cur->bc_flags & XFS_BTREE_OVERLAPPING) || ptr == 1;
2118}
2119
2120/*
2121 * Update the low and high parent keys of the given level, progressing
2122 * towards the root. If force_all is false, stop if the keys for a given
2123 * level do not need updating.
2124 */
2125STATIC int
2126__xfs_btree_updkeys(
2127 struct xfs_btree_cur *cur,
2128 int level,
2129 struct xfs_btree_block *block,
2130 struct xfs_buf *bp0,
2131 bool force_all)
2132{
45413937 2133 union xfs_btree_key key; /* keys from current level */
13e831e0
DW
2134 union xfs_btree_key *lkey; /* keys from the next level up */
2135 union xfs_btree_key *hkey;
2136 union xfs_btree_key *nlkey; /* keys from the next level up */
2137 union xfs_btree_key *nhkey;
2138 struct xfs_buf *bp;
2139 int ptr;
2140
2141 ASSERT(cur->bc_flags & XFS_BTREE_OVERLAPPING);
2142
2143 /* Exit if there aren't any parent levels to update. */
2144 if (level + 1 >= cur->bc_nlevels)
2145 return 0;
2146
2147 trace_xfs_btree_updkeys(cur, level, bp0);
2148
45413937 2149 lkey = &key;
13e831e0
DW
2150 hkey = xfs_btree_high_key_from_key(cur, lkey);
2151 xfs_btree_get_keys(cur, block, lkey);
2152 for (level++; level < cur->bc_nlevels; level++) {
2153#ifdef DEBUG
2154 int error;
2155#endif
2156 block = xfs_btree_get_block(cur, level, &bp);
2157 trace_xfs_btree_updkeys(cur, level, bp);
2158#ifdef DEBUG
2159 error = xfs_btree_check_block(cur, block, level, bp);
97b3ffd0 2160 if (error)
13e831e0 2161 return error;
13e831e0
DW
2162#endif
2163 ptr = cur->bc_ptrs[level];
2164 nlkey = xfs_btree_key_addr(cur, ptr, block);
2165 nhkey = xfs_btree_high_key_addr(cur, ptr, block);
2166 if (!force_all &&
2167 !(cur->bc_ops->diff_two_keys(cur, nlkey, lkey) != 0 ||
2168 cur->bc_ops->diff_two_keys(cur, nhkey, hkey) != 0))
2169 break;
2170 xfs_btree_copy_keys(cur, nlkey, lkey, 1);
2171 xfs_btree_log_keys(cur, bp, ptr, ptr);
2172 if (level + 1 >= cur->bc_nlevels)
2173 break;
64dbe047 2174 xfs_btree_get_node_keys(cur, block, lkey);
13e831e0
DW
2175 }
2176
2177 return 0;
2178}
2179
13e831e0
DW
2180/* Update all the keys from some level in cursor back to the root. */
2181STATIC int
2182xfs_btree_updkeys_force(
2183 struct xfs_btree_cur *cur,
2184 int level)
2185{
2186 struct xfs_buf *bp;
2187 struct xfs_btree_block *block;
2188
2189 block = xfs_btree_get_block(cur, level, &bp);
2190 return __xfs_btree_updkeys(cur, level, block, bp, true);
a3c9cb10
DW
2191}
2192
2193/*
2194 * Update the parent keys of the given level, progressing towards the root.
2195 */
64dbe047 2196STATIC int
a3c9cb10 2197xfs_btree_update_keys(
b194c7d8 2198 struct xfs_btree_cur *cur,
b194c7d8
BN
2199 int level)
2200{
2201 struct xfs_btree_block *block;
2202 struct xfs_buf *bp;
2203 union xfs_btree_key *kp;
a3c9cb10 2204 union xfs_btree_key key;
b194c7d8
BN
2205 int ptr;
2206
64dbe047
DW
2207 ASSERT(level >= 0);
2208
2209 block = xfs_btree_get_block(cur, level, &bp);
2210 if (cur->bc_flags & XFS_BTREE_OVERLAPPING)
2211 return __xfs_btree_updkeys(cur, level, block, bp, false);
13e831e0 2212
b194c7d8
BN
2213 /*
2214 * Go up the tree from this level toward the root.
2215 * At each level, update the key value to the value input.
2216 * Stop when we reach a level where the cursor isn't pointing
2217 * at the first entry in the block.
2218 */
a3c9cb10
DW
2219 xfs_btree_get_keys(cur, block, &key);
2220 for (level++, ptr = 1; ptr == 1 && level < cur->bc_nlevels; level++) {
b194c7d8
BN
2221#ifdef DEBUG
2222 int error;
2223#endif
2224 block = xfs_btree_get_block(cur, level, &bp);
2225#ifdef DEBUG
2226 error = xfs_btree_check_block(cur, block, level, bp);
97b3ffd0 2227 if (error)
b194c7d8 2228 return error;
b194c7d8
BN
2229#endif
2230 ptr = cur->bc_ptrs[level];
2231 kp = xfs_btree_key_addr(cur, ptr, block);
a3c9cb10 2232 xfs_btree_copy_keys(cur, kp, &key, 1);
b194c7d8
BN
2233 xfs_btree_log_keys(cur, bp, ptr, ptr);
2234 }
2235
b194c7d8
BN
2236 return 0;
2237}
2238
2239/*
2240 * Update the record referred to by cur to the value in the
2241 * given record. This either works (return 0) or gets an
2242 * EFSCORRUPTED error.
2243 */
2244int
2245xfs_btree_update(
2246 struct xfs_btree_cur *cur,
2247 union xfs_btree_rec *rec)
2248{
2249 struct xfs_btree_block *block;
2250 struct xfs_buf *bp;
2251 int error;
2252 int ptr;
2253 union xfs_btree_rec *rp;
2254
b194c7d8
BN
2255 /* Pick up the current block. */
2256 block = xfs_btree_get_block(cur, 0, &bp);
2257
2258#ifdef DEBUG
2259 error = xfs_btree_check_block(cur, block, 0, bp);
2260 if (error)
2261 goto error0;
2262#endif
2263 /* Get the address of the rec to be updated. */
2264 ptr = cur->bc_ptrs[0];
2265 rp = xfs_btree_rec_addr(cur, ptr, block);
2266
2267 /* Fill in the new contents and log them. */
2268 xfs_btree_copy_recs(cur, rp, rec, 1);
2269 xfs_btree_log_recs(cur, bp, ptr, ptr);
2270
2271 /*
2272 * If we are tracking the last record in the tree and
2273 * we are at the far right edge of the tree, update it.
2274 */
2275 if (xfs_btree_is_lastrec(cur, block, 0)) {
2276 cur->bc_ops->update_lastrec(cur, block, rec,
2277 ptr, LASTREC_UPDATE);
2278 }
2279
13e831e0 2280 /* Pass new key value up to our parent. */
a3c9cb10 2281 if (xfs_btree_needs_key_update(cur, ptr)) {
64dbe047 2282 error = xfs_btree_update_keys(cur, 0);
b194c7d8
BN
2283 if (error)
2284 goto error0;
2285 }
2286
b194c7d8
BN
2287 return 0;
2288
2289error0:
b194c7d8
BN
2290 return error;
2291}
2292
2293/*
2294 * Move 1 record left from cur/level if possible.
2295 * Update cur to reflect the new path.
2296 */
2297STATIC int /* error */
2298xfs_btree_lshift(
2299 struct xfs_btree_cur *cur,
2300 int level,
2301 int *stat) /* success/failure */
2302{
b194c7d8
BN
2303 struct xfs_buf *lbp; /* left buffer pointer */
2304 struct xfs_btree_block *left; /* left btree block */
2305 int lrecs; /* left record count */
2306 struct xfs_buf *rbp; /* right buffer pointer */
2307 struct xfs_btree_block *right; /* right btree block */
13e831e0 2308 struct xfs_btree_cur *tcur; /* temporary btree cursor */
b194c7d8
BN
2309 int rrecs; /* right record count */
2310 union xfs_btree_ptr lptr; /* left btree pointer */
2311 union xfs_btree_key *rkp = NULL; /* right btree key */
2312 union xfs_btree_ptr *rpp = NULL; /* right address pointer */
2313 union xfs_btree_rec *rrp = NULL; /* right record pointer */
2314 int error; /* error return value */
13e831e0 2315 int i;
b194c7d8 2316
b194c7d8
BN
2317 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
2318 level == cur->bc_nlevels - 1)
2319 goto out0;
2320
2321 /* Set up variables for this block as "right". */
2322 right = xfs_btree_get_block(cur, level, &rbp);
2323
2324#ifdef DEBUG
2325 error = xfs_btree_check_block(cur, right, level, rbp);
2326 if (error)
2327 goto error0;
2328#endif
2329
2330 /* If we've got no left sibling then we can't shift an entry left. */
2331 xfs_btree_get_sibling(cur, right, &lptr, XFS_BB_LEFTSIB);
2332 if (xfs_btree_ptr_is_null(cur, &lptr))
2333 goto out0;
2334
2335 /*
2336 * If the cursor entry is the one that would be moved, don't
2337 * do it... it's too complicated.
2338 */
2339 if (cur->bc_ptrs[level] <= 1)
2340 goto out0;
2341
2342 /* Set up the left neighbor as "left". */
ff105f75 2343 error = xfs_btree_read_buf_block(cur, &lptr, 0, &left, &lbp);
b194c7d8
BN
2344 if (error)
2345 goto error0;
2346
2347 /* If it's full, it can't take another entry. */
2348 lrecs = xfs_btree_get_numrecs(left);
2349 if (lrecs == cur->bc_ops->get_maxrecs(cur, level))
2350 goto out0;
2351
2352 rrecs = xfs_btree_get_numrecs(right);
2353
2354 /*
2355 * We add one entry to the left side and remove one for the right side.
56b2de80 2356 * Account for it here, the changes will be updated on disk and logged
b194c7d8
BN
2357 * later.
2358 */
2359 lrecs++;
2360 rrecs--;
2361
2362 XFS_BTREE_STATS_INC(cur, lshift);
2363 XFS_BTREE_STATS_ADD(cur, moves, 1);
2364
2365 /*
2366 * If non-leaf, copy a key and a ptr to the left block.
2367 * Log the changes to the left block.
2368 */
2369 if (level > 0) {
2370 /* It's a non-leaf. Move keys and pointers. */
2371 union xfs_btree_key *lkp; /* left btree key */
2372 union xfs_btree_ptr *lpp; /* left address pointer */
2373
2374 lkp = xfs_btree_key_addr(cur, lrecs, left);
2375 rkp = xfs_btree_key_addr(cur, 1, right);
2376
2377 lpp = xfs_btree_ptr_addr(cur, lrecs, left);
2378 rpp = xfs_btree_ptr_addr(cur, 1, right);
efa7dddb
DW
2379
2380 error = xfs_btree_debug_check_ptr(cur, rpp, 0, level);
b194c7d8
BN
2381 if (error)
2382 goto error0;
efa7dddb 2383
b194c7d8
BN
2384 xfs_btree_copy_keys(cur, lkp, rkp, 1);
2385 xfs_btree_copy_ptrs(cur, lpp, rpp, 1);
2386
2387 xfs_btree_log_keys(cur, lbp, lrecs, lrecs);
2388 xfs_btree_log_ptrs(cur, lbp, lrecs, lrecs);
2389
2390 ASSERT(cur->bc_ops->keys_inorder(cur,
2391 xfs_btree_key_addr(cur, lrecs - 1, left), lkp));
2392 } else {
2393 /* It's a leaf. Move records. */
2394 union xfs_btree_rec *lrp; /* left record pointer */
2395
2396 lrp = xfs_btree_rec_addr(cur, lrecs, left);
2397 rrp = xfs_btree_rec_addr(cur, 1, right);
2398
2399 xfs_btree_copy_recs(cur, lrp, rrp, 1);
2400 xfs_btree_log_recs(cur, lbp, lrecs, lrecs);
2401
2402 ASSERT(cur->bc_ops->recs_inorder(cur,
2403 xfs_btree_rec_addr(cur, lrecs - 1, left), lrp));
2404 }
2405
2406 xfs_btree_set_numrecs(left, lrecs);
2407 xfs_btree_log_block(cur, lbp, XFS_BB_NUMRECS);
2408
2409 xfs_btree_set_numrecs(right, rrecs);
2410 xfs_btree_log_block(cur, rbp, XFS_BB_NUMRECS);
2411
2412 /*
2413 * Slide the contents of right down one entry.
2414 */
2415 XFS_BTREE_STATS_ADD(cur, moves, rrecs - 1);
2416 if (level > 0) {
2417 /* It's a nonleaf. operate on keys and ptrs */
b194c7d8
BN
2418 int i; /* loop index */
2419
2420 for (i = 0; i < rrecs; i++) {
efa7dddb 2421 error = xfs_btree_debug_check_ptr(cur, rpp, i + 1, level);
b194c7d8
BN
2422 if (error)
2423 goto error0;
2424 }
efa7dddb 2425
b194c7d8
BN
2426 xfs_btree_shift_keys(cur,
2427 xfs_btree_key_addr(cur, 2, right),
2428 -1, rrecs);
2429 xfs_btree_shift_ptrs(cur,
2430 xfs_btree_ptr_addr(cur, 2, right),
2431 -1, rrecs);
2432
2433 xfs_btree_log_keys(cur, rbp, 1, rrecs);
2434 xfs_btree_log_ptrs(cur, rbp, 1, rrecs);
2435 } else {
2436 /* It's a leaf. operate on records */
2437 xfs_btree_shift_recs(cur,
2438 xfs_btree_rec_addr(cur, 2, right),
2439 -1, rrecs);
2440 xfs_btree_log_recs(cur, rbp, 1, rrecs);
b194c7d8
BN
2441 }
2442
13e831e0
DW
2443 /*
2444 * Using a temporary cursor, update the parent key values of the
2445 * block on the left.
2446 */
e6358021
DW
2447 if (cur->bc_flags & XFS_BTREE_OVERLAPPING) {
2448 error = xfs_btree_dup_cursor(cur, &tcur);
2449 if (error)
2450 goto error0;
2451 i = xfs_btree_firstrec(tcur, level);
2452 XFS_WANT_CORRUPTED_GOTO(tcur->bc_mp, i == 1, error0);
13e831e0 2453
e6358021
DW
2454 error = xfs_btree_decrement(tcur, level, &i);
2455 if (error)
2456 goto error1;
13e831e0 2457
e6358021 2458 /* Update the parent high keys of the left block, if needed. */
64dbe047 2459 error = xfs_btree_update_keys(tcur, level);
13e831e0
DW
2460 if (error)
2461 goto error1;
e6358021
DW
2462
2463 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
13e831e0
DW
2464 }
2465
e6358021
DW
2466 /* Update the parent keys of the right block. */
2467 error = xfs_btree_update_keys(cur, level);
2468 if (error)
2469 goto error0;
b194c7d8
BN
2470
2471 /* Slide the cursor value left one. */
2472 cur->bc_ptrs[level]--;
2473
b194c7d8
BN
2474 *stat = 1;
2475 return 0;
2476
2477out0:
b194c7d8
BN
2478 *stat = 0;
2479 return 0;
2480
2481error0:
b194c7d8 2482 return error;
13e831e0
DW
2483
2484error1:
13e831e0
DW
2485 xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
2486 return error;
b194c7d8
BN
2487}
2488
2489/*
2490 * Move 1 record right from cur/level if possible.
2491 * Update cur to reflect the new path.
2492 */
2493STATIC int /* error */
2494xfs_btree_rshift(
2495 struct xfs_btree_cur *cur,
2496 int level,
2497 int *stat) /* success/failure */
2498{
b194c7d8
BN
2499 struct xfs_buf *lbp; /* left buffer pointer */
2500 struct xfs_btree_block *left; /* left btree block */
2501 struct xfs_buf *rbp; /* right buffer pointer */
2502 struct xfs_btree_block *right; /* right btree block */
2503 struct xfs_btree_cur *tcur; /* temporary btree cursor */
2504 union xfs_btree_ptr rptr; /* right block pointer */
2505 union xfs_btree_key *rkp; /* right btree key */
2506 int rrecs; /* right record count */
2507 int lrecs; /* left record count */
2508 int error; /* error return value */
2509 int i; /* loop counter */
2510
b194c7d8
BN
2511 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
2512 (level == cur->bc_nlevels - 1))
2513 goto out0;
2514
2515 /* Set up variables for this block as "left". */
2516 left = xfs_btree_get_block(cur, level, &lbp);
2517
2518#ifdef DEBUG
2519 error = xfs_btree_check_block(cur, left, level, lbp);
2520 if (error)
2521 goto error0;
2522#endif
2523
2524 /* If we've got no right sibling then we can't shift an entry right. */
2525 xfs_btree_get_sibling(cur, left, &rptr, XFS_BB_RIGHTSIB);
2526 if (xfs_btree_ptr_is_null(cur, &rptr))
2527 goto out0;
2528
2529 /*
2530 * If the cursor entry is the one that would be moved, don't
2531 * do it... it's too complicated.
2532 */
2533 lrecs = xfs_btree_get_numrecs(left);
2534 if (cur->bc_ptrs[level] >= lrecs)
2535 goto out0;
2536
2537 /* Set up the right neighbor as "right". */
ff105f75 2538 error = xfs_btree_read_buf_block(cur, &rptr, 0, &right, &rbp);
b194c7d8
BN
2539 if (error)
2540 goto error0;
2541
2542 /* If it's full, it can't take another entry. */
2543 rrecs = xfs_btree_get_numrecs(right);
2544 if (rrecs == cur->bc_ops->get_maxrecs(cur, level))
2545 goto out0;
2546
2547 XFS_BTREE_STATS_INC(cur, rshift);
2548 XFS_BTREE_STATS_ADD(cur, moves, rrecs);
2549
2550 /*
2551 * Make a hole at the start of the right neighbor block, then
2552 * copy the last left block entry to the hole.
2553 */
2554 if (level > 0) {
2555 /* It's a nonleaf. make a hole in the keys and ptrs */
2556 union xfs_btree_key *lkp;
2557 union xfs_btree_ptr *lpp;
2558 union xfs_btree_ptr *rpp;
2559
2560 lkp = xfs_btree_key_addr(cur, lrecs, left);
2561 lpp = xfs_btree_ptr_addr(cur, lrecs, left);
2562 rkp = xfs_btree_key_addr(cur, 1, right);
2563 rpp = xfs_btree_ptr_addr(cur, 1, right);
2564
b194c7d8 2565 for (i = rrecs - 1; i >= 0; i--) {
efa7dddb 2566 error = xfs_btree_debug_check_ptr(cur, rpp, i, level);
b194c7d8
BN
2567 if (error)
2568 goto error0;
2569 }
b194c7d8
BN
2570
2571 xfs_btree_shift_keys(cur, rkp, 1, rrecs);
2572 xfs_btree_shift_ptrs(cur, rpp, 1, rrecs);
2573
efa7dddb 2574 error = xfs_btree_debug_check_ptr(cur, lpp, 0, level);
b194c7d8
BN
2575 if (error)
2576 goto error0;
b194c7d8
BN
2577
2578 /* Now put the new data in, and log it. */
2579 xfs_btree_copy_keys(cur, rkp, lkp, 1);
2580 xfs_btree_copy_ptrs(cur, rpp, lpp, 1);
2581
2582 xfs_btree_log_keys(cur, rbp, 1, rrecs + 1);
2583 xfs_btree_log_ptrs(cur, rbp, 1, rrecs + 1);
2584
2585 ASSERT(cur->bc_ops->keys_inorder(cur, rkp,
2586 xfs_btree_key_addr(cur, 2, right)));
2587 } else {
2588 /* It's a leaf. make a hole in the records */
2589 union xfs_btree_rec *lrp;
2590 union xfs_btree_rec *rrp;
2591
2592 lrp = xfs_btree_rec_addr(cur, lrecs, left);
2593 rrp = xfs_btree_rec_addr(cur, 1, right);
2594
2595 xfs_btree_shift_recs(cur, rrp, 1, rrecs);
2596
2597 /* Now put the new data in, and log it. */
2598 xfs_btree_copy_recs(cur, rrp, lrp, 1);
2599 xfs_btree_log_recs(cur, rbp, 1, rrecs + 1);
b194c7d8
BN
2600 }
2601
2602 /*
2603 * Decrement and log left's numrecs, bump and log right's numrecs.
2604 */
2605 xfs_btree_set_numrecs(left, --lrecs);
2606 xfs_btree_log_block(cur, lbp, XFS_BB_NUMRECS);
2607
2608 xfs_btree_set_numrecs(right, ++rrecs);
2609 xfs_btree_log_block(cur, rbp, XFS_BB_NUMRECS);
2610
2611 /*
2612 * Using a temporary cursor, update the parent key values of the
2613 * block on the right.
2614 */
2615 error = xfs_btree_dup_cursor(cur, &tcur);
2616 if (error)
2617 goto error0;
2618 i = xfs_btree_lastrec(tcur, level);
e6358021 2619 XFS_WANT_CORRUPTED_GOTO(tcur->bc_mp, i == 1, error0);
b194c7d8
BN
2620
2621 error = xfs_btree_increment(tcur, level, &i);
2622 if (error)
2623 goto error1;
2624
13e831e0
DW
2625 /* Update the parent high keys of the left block, if needed. */
2626 if (cur->bc_flags & XFS_BTREE_OVERLAPPING) {
64dbe047 2627 error = xfs_btree_update_keys(cur, level);
13e831e0
DW
2628 if (error)
2629 goto error1;
2630 }
2631
a3c9cb10 2632 /* Update the parent keys of the right block. */
64dbe047 2633 error = xfs_btree_update_keys(tcur, level);
b194c7d8
BN
2634 if (error)
2635 goto error1;
2636
2637 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
2638
b194c7d8
BN
2639 *stat = 1;
2640 return 0;
2641
2642out0:
b194c7d8
BN
2643 *stat = 0;
2644 return 0;
2645
2646error0:
b194c7d8
BN
2647 return error;
2648
2649error1:
b194c7d8
BN
2650 xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
2651 return error;
2652}
2653
2654/*
2655 * Split cur/level block in half.
2656 * Return new block number and the key to its first
2657 * record (to be inserted into parent).
2658 */
2659STATIC int /* error */
ff105f75 2660__xfs_btree_split(
b194c7d8
BN
2661 struct xfs_btree_cur *cur,
2662 int level,
2663 union xfs_btree_ptr *ptrp,
2664 union xfs_btree_key *key,
2665 struct xfs_btree_cur **curp,
2666 int *stat) /* success/failure */
2667{
2668 union xfs_btree_ptr lptr; /* left sibling block ptr */
2669 struct xfs_buf *lbp; /* left buffer pointer */
2670 struct xfs_btree_block *left; /* left btree block */
2671 union xfs_btree_ptr rptr; /* right sibling block ptr */
2672 struct xfs_buf *rbp; /* right buffer pointer */
2673 struct xfs_btree_block *right; /* right btree block */
2674 union xfs_btree_ptr rrptr; /* right-right sibling ptr */
2675 struct xfs_buf *rrbp; /* right-right buffer pointer */
2676 struct xfs_btree_block *rrblock; /* right-right btree block */
2677 int lrecs;
2678 int rrecs;
2679 int src_index;
2680 int error; /* error return value */
b194c7d8 2681 int i;
b194c7d8 2682
b194c7d8
BN
2683 XFS_BTREE_STATS_INC(cur, split);
2684
2685 /* Set up left block (current one). */
2686 left = xfs_btree_get_block(cur, level, &lbp);
2687
2688#ifdef DEBUG
2689 error = xfs_btree_check_block(cur, left, level, lbp);
2690 if (error)
2691 goto error0;
2692#endif
2693
2694 xfs_btree_buf_to_ptr(cur, lbp, &lptr);
2695
2696 /* Allocate the new block. If we can't do it, we're toast. Give up. */
ff105f75 2697 error = cur->bc_ops->alloc_block(cur, &lptr, &rptr, stat);
b194c7d8
BN
2698 if (error)
2699 goto error0;
2700 if (*stat == 0)
2701 goto out0;
2702 XFS_BTREE_STATS_INC(cur, alloc);
2703
2704 /* Set up the new block as "right". */
2705 error = xfs_btree_get_buf_block(cur, &rptr, 0, &right, &rbp);
2706 if (error)
2707 goto error0;
2708
2709 /* Fill in the btree header for the new right block. */
5dfa5cd2 2710 xfs_btree_init_block_cur(cur, rbp, xfs_btree_get_level(left), 0);
b194c7d8
BN
2711
2712 /*
2713 * Split the entries between the old and the new block evenly.
2714 * Make sure that if there's an odd number of entries now, that
2715 * each new block will have the same number of entries.
2716 */
2717 lrecs = xfs_btree_get_numrecs(left);
2718 rrecs = lrecs / 2;
2719 if ((lrecs & 1) && cur->bc_ptrs[level] <= rrecs + 1)
2720 rrecs++;
2721 src_index = (lrecs - rrecs + 1);
2722
2723 XFS_BTREE_STATS_ADD(cur, moves, rrecs);
2724
a3c9cb10
DW
2725 /* Adjust numrecs for the later get_*_keys() calls. */
2726 lrecs -= rrecs;
2727 xfs_btree_set_numrecs(left, lrecs);
2728 xfs_btree_set_numrecs(right, xfs_btree_get_numrecs(right) + rrecs);
2729
b194c7d8
BN
2730 /*
2731 * Copy btree block entries from the left block over to the
2732 * new block, the right. Update the right block and log the
2733 * changes.
2734 */
2735 if (level > 0) {
2736 /* It's a non-leaf. Move keys and pointers. */
2737 union xfs_btree_key *lkp; /* left btree key */
2738 union xfs_btree_ptr *lpp; /* left address pointer */
2739 union xfs_btree_key *rkp; /* right btree key */
2740 union xfs_btree_ptr *rpp; /* right address pointer */
2741
2742 lkp = xfs_btree_key_addr(cur, src_index, left);
2743 lpp = xfs_btree_ptr_addr(cur, src_index, left);
2744 rkp = xfs_btree_key_addr(cur, 1, right);
2745 rpp = xfs_btree_ptr_addr(cur, 1, right);
2746
b194c7d8 2747 for (i = src_index; i < rrecs; i++) {
efa7dddb 2748 error = xfs_btree_debug_check_ptr(cur, lpp, i, level);
b194c7d8
BN
2749 if (error)
2750 goto error0;
2751 }
b194c7d8 2752
a3c9cb10 2753 /* Copy the keys & pointers to the new block. */
b194c7d8
BN
2754 xfs_btree_copy_keys(cur, rkp, lkp, rrecs);
2755 xfs_btree_copy_ptrs(cur, rpp, lpp, rrecs);
2756
2757 xfs_btree_log_keys(cur, rbp, 1, rrecs);
2758 xfs_btree_log_ptrs(cur, rbp, 1, rrecs);
2759
a3c9cb10 2760 /* Stash the keys of the new block for later insertion. */
64dbe047 2761 xfs_btree_get_node_keys(cur, right, key);
b194c7d8
BN
2762 } else {
2763 /* It's a leaf. Move records. */
2764 union xfs_btree_rec *lrp; /* left record pointer */
2765 union xfs_btree_rec *rrp; /* right record pointer */
2766
2767 lrp = xfs_btree_rec_addr(cur, src_index, left);
2768 rrp = xfs_btree_rec_addr(cur, 1, right);
2769
a3c9cb10 2770 /* Copy records to the new block. */
b194c7d8
BN
2771 xfs_btree_copy_recs(cur, rrp, lrp, rrecs);
2772 xfs_btree_log_recs(cur, rbp, 1, rrecs);
2773
a3c9cb10 2774 /* Stash the keys of the new block for later insertion. */
64dbe047 2775 xfs_btree_get_leaf_keys(cur, right, key);
b194c7d8
BN
2776 }
2777
b194c7d8
BN
2778 /*
2779 * Find the left block number by looking in the buffer.
a3c9cb10 2780 * Adjust sibling pointers.
b194c7d8
BN
2781 */
2782 xfs_btree_get_sibling(cur, left, &rrptr, XFS_BB_RIGHTSIB);
2783 xfs_btree_set_sibling(cur, right, &rrptr, XFS_BB_RIGHTSIB);
2784 xfs_btree_set_sibling(cur, right, &lptr, XFS_BB_LEFTSIB);
2785 xfs_btree_set_sibling(cur, left, &rptr, XFS_BB_RIGHTSIB);
2786
b194c7d8
BN
2787 xfs_btree_log_block(cur, rbp, XFS_BB_ALL_BITS);
2788 xfs_btree_log_block(cur, lbp, XFS_BB_NUMRECS | XFS_BB_RIGHTSIB);
2789
2790 /*
2791 * If there's a block to the new block's right, make that block
2792 * point back to right instead of to left.
2793 */
2794 if (!xfs_btree_ptr_is_null(cur, &rrptr)) {
ff105f75 2795 error = xfs_btree_read_buf_block(cur, &rrptr,
b194c7d8
BN
2796 0, &rrblock, &rrbp);
2797 if (error)
2798 goto error0;
2799 xfs_btree_set_sibling(cur, rrblock, &rptr, XFS_BB_LEFTSIB);
2800 xfs_btree_log_block(cur, rrbp, XFS_BB_LEFTSIB);
2801 }
13e831e0
DW
2802
2803 /* Update the parent high keys of the left block, if needed. */
2804 if (cur->bc_flags & XFS_BTREE_OVERLAPPING) {
64dbe047 2805 error = xfs_btree_update_keys(cur, level);
13e831e0
DW
2806 if (error)
2807 goto error0;
2808 }
2809
b194c7d8
BN
2810 /*
2811 * If the cursor is really in the right block, move it there.
2812 * If it's just pointing past the last entry in left, then we'll
2813 * insert there, so don't change anything in that case.
2814 */
2815 if (cur->bc_ptrs[level] > lrecs + 1) {
2816 xfs_btree_setbuf(cur, level, rbp);
2817 cur->bc_ptrs[level] -= lrecs;
2818 }
2819 /*
2820 * If there are more levels, we'll need another cursor which refers
2821 * the right block, no matter where this cursor was.
2822 */
2823 if (level + 1 < cur->bc_nlevels) {
2824 error = xfs_btree_dup_cursor(cur, curp);
2825 if (error)
2826 goto error0;
2827 (*curp)->bc_ptrs[level + 1]++;
2828 }
2829 *ptrp = rptr;
b194c7d8
BN
2830 *stat = 1;
2831 return 0;
2832out0:
b194c7d8
BN
2833 *stat = 0;
2834 return 0;
2835
2836error0:
b194c7d8
BN
2837 return error;
2838}
2839
19ebedcf 2840#ifdef __KERNEL__
ff105f75
DC
2841struct xfs_btree_split_args {
2842 struct xfs_btree_cur *cur;
2843 int level;
2844 union xfs_btree_ptr *ptrp;
2845 union xfs_btree_key *key;
2846 struct xfs_btree_cur **curp;
2847 int *stat; /* success/failure */
2848 int result;
2849 bool kswapd; /* allocation in kswapd context */
2850 struct completion *done;
2851 struct work_struct work;
2852};
2853
2854/*
2855 * Stack switching interfaces for allocation
2856 */
2857static void
2858xfs_btree_split_worker(
2859 struct work_struct *work)
2860{
2861 struct xfs_btree_split_args *args = container_of(work,
2862 struct xfs_btree_split_args, work);
2863 unsigned long pflags;
b455713b 2864 unsigned long new_pflags = PF_MEMALLOC_NOFS;
ff105f75
DC
2865
2866 /*
2867 * we are in a transaction context here, but may also be doing work
2868 * in kswapd context, and hence we may need to inherit that state
2869 * temporarily to ensure that we don't block waiting for memory reclaim
2870 * in any way.
2871 */
2872 if (args->kswapd)
2873 new_pflags |= PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD;
2874
2875 current_set_flags_nested(&pflags, new_pflags);
2876
2877 args->result = __xfs_btree_split(args->cur, args->level, args->ptrp,
2878 args->key, args->curp, args->stat);
2879 complete(args->done);
2880
2881 current_restore_flags_nested(&pflags, new_pflags);
2882}
ff105f75
DC
2883
2884/*
2885 * BMBT split requests often come in with little stack to work on. Push
2886 * them off to a worker thread so there is lots of stack to use. For the other
2887 * btree types, just call directly to avoid the context switch overhead here.
2888 */
2889STATIC int /* error */
2890xfs_btree_split(
2891 struct xfs_btree_cur *cur,
2892 int level,
2893 union xfs_btree_ptr *ptrp,
2894 union xfs_btree_key *key,
2895 struct xfs_btree_cur **curp,
2896 int *stat) /* success/failure */
2897{
ff105f75
DC
2898 struct xfs_btree_split_args args;
2899 DECLARE_COMPLETION_ONSTACK(done);
2900
2901 if (cur->bc_btnum != XFS_BTNUM_BMAP)
ff105f75
DC
2902 return __xfs_btree_split(cur, level, ptrp, key, curp, stat);
2903
ff105f75
DC
2904 args.cur = cur;
2905 args.level = level;
2906 args.ptrp = ptrp;
2907 args.key = key;
2908 args.curp = curp;
2909 args.stat = stat;
2910 args.done = &done;
2911 args.kswapd = current_is_kswapd();
2912 INIT_WORK_ONSTACK(&args.work, xfs_btree_split_worker);
2913 queue_work(xfs_alloc_wq, &args.work);
2914 wait_for_completion(&done);
2915 destroy_work_on_stack(&args.work);
2916 return args.result;
ff105f75 2917}
19ebedcf
DC
2918#else /* !KERNEL */
2919#define xfs_btree_split __xfs_btree_split
2920#endif
ff105f75
DC
2921
2922
b194c7d8
BN
2923/*
2924 * Copy the old inode root contents into a real block and make the
2925 * broot point to it.
2926 */
2927int /* error */
2928xfs_btree_new_iroot(
2929 struct xfs_btree_cur *cur, /* btree cursor */
2930 int *logflags, /* logging flags for inode */
2931 int *stat) /* return status - 0 fail */
2932{
2933 struct xfs_buf *cbp; /* buffer for cblock */
2934 struct xfs_btree_block *block; /* btree block */
2935 struct xfs_btree_block *cblock; /* child btree block */
2936 union xfs_btree_key *ckp; /* child key pointer */
2937 union xfs_btree_ptr *cpp; /* child ptr pointer */
2938 union xfs_btree_key *kp; /* pointer to btree key */
2939 union xfs_btree_ptr *pp; /* pointer to block addr */
2940 union xfs_btree_ptr nptr; /* new block addr */
2941 int level; /* btree level */
2942 int error; /* error return code */
b194c7d8 2943 int i; /* loop counter */
b194c7d8 2944
b194c7d8
BN
2945 XFS_BTREE_STATS_INC(cur, newroot);
2946
2947 ASSERT(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE);
2948
2949 level = cur->bc_nlevels - 1;
2950
2951 block = xfs_btree_get_iroot(cur);
2952 pp = xfs_btree_ptr_addr(cur, 1, block);
2953
2954 /* Allocate the new block. If we can't do it, we're toast. Give up. */
ff105f75 2955 error = cur->bc_ops->alloc_block(cur, pp, &nptr, stat);
b194c7d8
BN
2956 if (error)
2957 goto error0;
97b3ffd0 2958 if (*stat == 0)
b194c7d8 2959 return 0;
97b3ffd0 2960
b194c7d8
BN
2961 XFS_BTREE_STATS_INC(cur, alloc);
2962
2963 /* Copy the root into a real block. */
2964 error = xfs_btree_get_buf_block(cur, &nptr, 0, &cblock, &cbp);
2965 if (error)
2966 goto error0;
2967
77ec5ff4
DC
2968 /*
2969 * we can't just memcpy() the root in for CRC enabled btree blocks.
2970 * In that case have to also ensure the blkno remains correct
2971 */
b194c7d8 2972 memcpy(cblock, block, xfs_btree_block_len(cur));
77ec5ff4
DC
2973 if (cur->bc_flags & XFS_BTREE_CRC_BLOCKS) {
2974 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
2975 cblock->bb_u.l.bb_blkno = cpu_to_be64(cbp->b_bn);
2976 else
2977 cblock->bb_u.s.bb_blkno = cpu_to_be64(cbp->b_bn);
2978 }
b194c7d8
BN
2979
2980 be16_add_cpu(&block->bb_level, 1);
2981 xfs_btree_set_numrecs(block, 1);
2982 cur->bc_nlevels++;
2983 cur->bc_ptrs[level + 1] = 1;
2984
2985 kp = xfs_btree_key_addr(cur, 1, block);
2986 ckp = xfs_btree_key_addr(cur, 1, cblock);
2987 xfs_btree_copy_keys(cur, ckp, kp, xfs_btree_get_numrecs(cblock));
2988
2989 cpp = xfs_btree_ptr_addr(cur, 1, cblock);
b194c7d8 2990 for (i = 0; i < be16_to_cpu(cblock->bb_numrecs); i++) {
efa7dddb 2991 error = xfs_btree_debug_check_ptr(cur, pp, i, level);
b194c7d8
BN
2992 if (error)
2993 goto error0;
2994 }
efa7dddb 2995
b194c7d8
BN
2996 xfs_btree_copy_ptrs(cur, cpp, pp, xfs_btree_get_numrecs(cblock));
2997
efa7dddb 2998 error = xfs_btree_debug_check_ptr(cur, &nptr, 0, level);
b194c7d8
BN
2999 if (error)
3000 goto error0;
efa7dddb 3001
b194c7d8
BN
3002 xfs_btree_copy_ptrs(cur, pp, &nptr, 1);
3003
3004 xfs_iroot_realloc(cur->bc_private.b.ip,
3005 1 - xfs_btree_get_numrecs(cblock),
3006 cur->bc_private.b.whichfork);
3007
3008 xfs_btree_setbuf(cur, level, cbp);
3009
3010 /*
3011 * Do all this logging at the end so that
3012 * the root is at the right level.
3013 */
3014 xfs_btree_log_block(cur, cbp, XFS_BB_ALL_BITS);
3015 xfs_btree_log_keys(cur, cbp, 1, be16_to_cpu(cblock->bb_numrecs));
3016 xfs_btree_log_ptrs(cur, cbp, 1, be16_to_cpu(cblock->bb_numrecs));
3017
3018 *logflags |=
56b2de80 3019 XFS_ILOG_CORE | xfs_ilog_fbroot(cur->bc_private.b.whichfork);
b194c7d8 3020 *stat = 1;
b194c7d8
BN
3021 return 0;
3022error0:
b194c7d8
BN
3023 return error;
3024}
3025
3026/*
3027 * Allocate a new root block, fill it in.
3028 */
3029STATIC int /* error */
3030xfs_btree_new_root(
3031 struct xfs_btree_cur *cur, /* btree cursor */
3032 int *stat) /* success/failure */
3033{
3034 struct xfs_btree_block *block; /* one half of the old root block */
3035 struct xfs_buf *bp; /* buffer containing block */
3036 int error; /* error return value */
3037 struct xfs_buf *lbp; /* left buffer pointer */
3038 struct xfs_btree_block *left; /* left btree block */
3039 struct xfs_buf *nbp; /* new (root) buffer */
3040 struct xfs_btree_block *new; /* new (root) btree block */
3041 int nptr; /* new value for key index, 1 or 2 */
3042 struct xfs_buf *rbp; /* right buffer pointer */
3043 struct xfs_btree_block *right; /* right btree block */
3044 union xfs_btree_ptr rptr;
3045 union xfs_btree_ptr lptr;
3046
b194c7d8
BN
3047 XFS_BTREE_STATS_INC(cur, newroot);
3048
3049 /* initialise our start point from the cursor */
3050 cur->bc_ops->init_ptr_from_cur(cur, &rptr);
3051
3052 /* Allocate the new block. If we can't do it, we're toast. Give up. */
ff105f75 3053 error = cur->bc_ops->alloc_block(cur, &rptr, &lptr, stat);
b194c7d8
BN
3054 if (error)
3055 goto error0;
3056 if (*stat == 0)
3057 goto out0;
3058 XFS_BTREE_STATS_INC(cur, alloc);
3059
3060 /* Set up the new block. */
3061 error = xfs_btree_get_buf_block(cur, &lptr, 0, &new, &nbp);
3062 if (error)
3063 goto error0;
3064
3065 /* Set the root in the holding structure increasing the level by 1. */
3066 cur->bc_ops->set_root(cur, &lptr, 1);
3067
3068 /*
3069 * At the previous root level there are now two blocks: the old root,
3070 * and the new block generated when it was split. We don't know which
3071 * one the cursor is pointing at, so we set up variables "left" and
3072 * "right" for each case.
3073 */
3074 block = xfs_btree_get_block(cur, cur->bc_nlevels - 1, &bp);
3075
3076#ifdef DEBUG
3077 error = xfs_btree_check_block(cur, block, cur->bc_nlevels - 1, bp);
3078 if (error)
3079 goto error0;
3080#endif
3081
3082 xfs_btree_get_sibling(cur, block, &rptr, XFS_BB_RIGHTSIB);
3083 if (!xfs_btree_ptr_is_null(cur, &rptr)) {
3084 /* Our block is left, pick up the right block. */
3085 lbp = bp;
3086 xfs_btree_buf_to_ptr(cur, lbp, &lptr);
3087 left = block;
ff105f75 3088 error = xfs_btree_read_buf_block(cur, &rptr, 0, &right, &rbp);
b194c7d8
BN
3089 if (error)
3090 goto error0;
3091 bp = rbp;
3092 nptr = 1;
3093 } else {
3094 /* Our block is right, pick up the left block. */
3095 rbp = bp;
3096 xfs_btree_buf_to_ptr(cur, rbp, &rptr);
3097 right = block;
3098 xfs_btree_get_sibling(cur, right, &lptr, XFS_BB_LEFTSIB);
ff105f75 3099 error = xfs_btree_read_buf_block(cur, &lptr, 0, &left, &lbp);
b194c7d8
BN
3100 if (error)
3101 goto error0;
3102 bp = lbp;
3103 nptr = 2;
3104 }
a3c9cb10 3105
b194c7d8 3106 /* Fill in the new block's btree header and log it. */
5dfa5cd2 3107 xfs_btree_init_block_cur(cur, nbp, cur->bc_nlevels, 2);
b194c7d8
BN
3108 xfs_btree_log_block(cur, nbp, XFS_BB_ALL_BITS);
3109 ASSERT(!xfs_btree_ptr_is_null(cur, &lptr) &&
3110 !xfs_btree_ptr_is_null(cur, &rptr));
3111
3112 /* Fill in the key data in the new root. */
3113 if (xfs_btree_get_level(left) > 0) {
a3c9cb10
DW
3114 /*
3115 * Get the keys for the left block's keys and put them directly
3116 * in the parent block. Do the same for the right block.
3117 */
64dbe047 3118 xfs_btree_get_node_keys(cur, left,
a3c9cb10 3119 xfs_btree_key_addr(cur, 1, new));
64dbe047 3120 xfs_btree_get_node_keys(cur, right,
a3c9cb10 3121 xfs_btree_key_addr(cur, 2, new));
b194c7d8 3122 } else {
a3c9cb10
DW
3123 /*
3124 * Get the keys for the left block's records and put them
3125 * directly in the parent block. Do the same for the right
3126 * block.
3127 */
64dbe047 3128 xfs_btree_get_leaf_keys(cur, left,
a3c9cb10 3129 xfs_btree_key_addr(cur, 1, new));
64dbe047 3130 xfs_btree_get_leaf_keys(cur, right,
a3c9cb10 3131 xfs_btree_key_addr(cur, 2, new));
b194c7d8
BN
3132 }
3133 xfs_btree_log_keys(cur, nbp, 1, 2);
3134
3135 /* Fill in the pointer data in the new root. */
3136 xfs_btree_copy_ptrs(cur,
3137 xfs_btree_ptr_addr(cur, 1, new), &lptr, 1);
3138 xfs_btree_copy_ptrs(cur,
3139 xfs_btree_ptr_addr(cur, 2, new), &rptr, 1);
3140 xfs_btree_log_ptrs(cur, nbp, 1, 2);
3141
3142 /* Fix up the cursor. */
3143 xfs_btree_setbuf(cur, cur->bc_nlevels, nbp);
3144 cur->bc_ptrs[cur->bc_nlevels] = nptr;
3145 cur->bc_nlevels++;
b194c7d8
BN
3146 *stat = 1;
3147 return 0;
3148error0:
b194c7d8
BN
3149 return error;
3150out0:
b194c7d8
BN
3151 *stat = 0;
3152 return 0;
3153}
3154
3155STATIC int
3156xfs_btree_make_block_unfull(
3157 struct xfs_btree_cur *cur, /* btree cursor */
3158 int level, /* btree level */
3159 int numrecs,/* # of recs in block */
3160 int *oindex,/* old tree index */
3161 int *index, /* new tree index */
3162 union xfs_btree_ptr *nptr, /* new btree ptr */
3163 struct xfs_btree_cur **ncur, /* new btree cursor */
a3c9cb10 3164 union xfs_btree_key *key, /* key of new block */
b194c7d8
BN
3165 int *stat)
3166{
b194c7d8
BN
3167 int error = 0;
3168
3169 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
3170 level == cur->bc_nlevels - 1) {
97b3ffd0 3171 struct xfs_inode *ip = cur->bc_private.b.ip;
b194c7d8
BN
3172
3173 if (numrecs < cur->bc_ops->get_dmaxrecs(cur, level)) {
3174 /* A root block that can be made bigger. */
b194c7d8 3175 xfs_iroot_realloc(ip, 1, cur->bc_private.b.whichfork);
cff8bf94 3176 *stat = 1;
b194c7d8
BN
3177 } else {
3178 /* A root block that needs replacing */
3179 int logflags = 0;
3180
3181 error = xfs_btree_new_iroot(cur, &logflags, stat);
3182 if (error || *stat == 0)
3183 return error;
3184
3185 xfs_trans_log_inode(cur->bc_tp, ip, logflags);
3186 }
3187
3188 return 0;
3189 }
3190
3191 /* First, try shifting an entry to the right neighbor. */
3192 error = xfs_btree_rshift(cur, level, stat);
3193 if (error || *stat)
3194 return error;
3195
3196 /* Next, try shifting an entry to the left neighbor. */
3197 error = xfs_btree_lshift(cur, level, stat);
3198 if (error)
3199 return error;
3200
3201 if (*stat) {
3202 *oindex = *index = cur->bc_ptrs[level];
3203 return 0;
3204 }
3205
3206 /*
3207 * Next, try splitting the current block in half.
3208 *
3209 * If this works we have to re-set our variables because we
3210 * could be in a different block now.
3211 */
d3cd7a27 3212 error = xfs_btree_split(cur, level, nptr, key, ncur, stat);
b194c7d8
BN
3213 if (error || *stat == 0)
3214 return error;
3215
3216
3217 *index = cur->bc_ptrs[level];
b194c7d8
BN
3218 return 0;
3219}
3220
3221/*
3222 * Insert one record/level. Return information to the caller
3223 * allowing the next level up to proceed if necessary.
3224 */
3225STATIC int
3226xfs_btree_insrec(
3227 struct xfs_btree_cur *cur, /* btree cursor */
3228 int level, /* level to insert record at */
3229 union xfs_btree_ptr *ptrp, /* i/o: block number inserted */
d3cd7a27
DW
3230 union xfs_btree_rec *rec, /* record to insert */
3231 union xfs_btree_key *key, /* i/o: block key for ptrp */
b194c7d8
BN
3232 struct xfs_btree_cur **curp, /* output: new cursor replacing cur */
3233 int *stat) /* success/failure */
3234{
3235 struct xfs_btree_block *block; /* btree block */
3236 struct xfs_buf *bp; /* buffer for block */
b194c7d8
BN
3237 union xfs_btree_ptr nptr; /* new block ptr */
3238 struct xfs_btree_cur *ncur; /* new btree cursor */
45413937 3239 union xfs_btree_key nkey; /* new block key */
13e831e0 3240 union xfs_btree_key *lkey;
b194c7d8
BN
3241 int optr; /* old key/record index */
3242 int ptr; /* key/record index */
3243 int numrecs;/* number of records */
3244 int error; /* error return value */
b194c7d8 3245 int i;
13e831e0 3246 xfs_daddr_t old_bn;
b194c7d8 3247
b194c7d8 3248 ncur = NULL;
45413937 3249 lkey = &nkey;
b194c7d8
BN
3250
3251 /*
3252 * If we have an external root pointer, and we've made it to the
3253 * root level, allocate a new root block and we're done.
3254 */
3255 if (!(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
3256 (level >= cur->bc_nlevels)) {
3257 error = xfs_btree_new_root(cur, stat);
3258 xfs_btree_set_ptr_null(cur, ptrp);
3259
b194c7d8
BN
3260 return error;
3261 }
3262
3263 /* If we're off the left edge, return failure. */
3264 ptr = cur->bc_ptrs[level];
3265 if (ptr == 0) {
b194c7d8
BN
3266 *stat = 0;
3267 return 0;
3268 }
3269
b194c7d8
BN
3270 optr = ptr;
3271
3272 XFS_BTREE_STATS_INC(cur, insrec);
3273
3274 /* Get pointers to the btree buffer and block. */
3275 block = xfs_btree_get_block(cur, level, &bp);
13e831e0 3276 old_bn = bp ? bp->b_bn : XFS_BUF_DADDR_NULL;
b194c7d8
BN
3277 numrecs = xfs_btree_get_numrecs(block);
3278
3279#ifdef DEBUG
3280 error = xfs_btree_check_block(cur, block, level, bp);
3281 if (error)
3282 goto error0;
3283
3284 /* Check that the new entry is being inserted in the right place. */
3285 if (ptr <= numrecs) {
3286 if (level == 0) {
d3cd7a27 3287 ASSERT(cur->bc_ops->recs_inorder(cur, rec,
b194c7d8
BN
3288 xfs_btree_rec_addr(cur, ptr, block)));
3289 } else {
d3cd7a27 3290 ASSERT(cur->bc_ops->keys_inorder(cur, key,
b194c7d8
BN
3291 xfs_btree_key_addr(cur, ptr, block)));
3292 }
3293 }
3294#endif
3295
3296 /*
3297 * If the block is full, we can't insert the new entry until we
3298 * make the block un-full.
3299 */
3300 xfs_btree_set_ptr_null(cur, &nptr);
3301 if (numrecs == cur->bc_ops->get_maxrecs(cur, level)) {
3302 error = xfs_btree_make_block_unfull(cur, level, numrecs,
13e831e0 3303 &optr, &ptr, &nptr, &ncur, lkey, stat);
b194c7d8
BN
3304 if (error || *stat == 0)
3305 goto error0;
3306 }
3307
3308 /*
3309 * The current block may have changed if the block was
3310 * previously full and we have just made space in it.
3311 */
3312 block = xfs_btree_get_block(cur, level, &bp);
3313 numrecs = xfs_btree_get_numrecs(block);
3314
3315#ifdef DEBUG
3316 error = xfs_btree_check_block(cur, block, level, bp);
3317 if (error)
3318 return error;
3319#endif
3320
3321 /*
3322 * At this point we know there's room for our new entry in the block
3323 * we're pointing at.
3324 */
3325 XFS_BTREE_STATS_ADD(cur, moves, numrecs - ptr + 1);
3326
3327 if (level > 0) {
3328 /* It's a nonleaf. make a hole in the keys and ptrs */
3329 union xfs_btree_key *kp;
3330 union xfs_btree_ptr *pp;
3331
3332 kp = xfs_btree_key_addr(cur, ptr, block);
3333 pp = xfs_btree_ptr_addr(cur, ptr, block);
3334
b194c7d8 3335 for (i = numrecs - ptr; i >= 0; i--) {
efa7dddb 3336 error = xfs_btree_debug_check_ptr(cur, pp, i, level);
b194c7d8
BN
3337 if (error)
3338 return error;
3339 }
b194c7d8
BN
3340
3341 xfs_btree_shift_keys(cur, kp, 1, numrecs - ptr + 1);
3342 xfs_btree_shift_ptrs(cur, pp, 1, numrecs - ptr + 1);
3343
efa7dddb 3344 error = xfs_btree_debug_check_ptr(cur, ptrp, 0, level);
b194c7d8
BN
3345 if (error)
3346 goto error0;
b194c7d8
BN
3347
3348 /* Now put the new data in, bump numrecs and log it. */
d3cd7a27 3349 xfs_btree_copy_keys(cur, kp, key, 1);
b194c7d8
BN
3350 xfs_btree_copy_ptrs(cur, pp, ptrp, 1);
3351 numrecs++;
3352 xfs_btree_set_numrecs(block, numrecs);
3353 xfs_btree_log_ptrs(cur, bp, ptr, numrecs);
3354 xfs_btree_log_keys(cur, bp, ptr, numrecs);
3355#ifdef DEBUG
3356 if (ptr < numrecs) {
3357 ASSERT(cur->bc_ops->keys_inorder(cur, kp,
3358 xfs_btree_key_addr(cur, ptr + 1, block)));
3359 }
3360#endif
3361 } else {
3362 /* It's a leaf. make a hole in the records */
3363 union xfs_btree_rec *rp;
3364
3365 rp = xfs_btree_rec_addr(cur, ptr, block);
3366
3367 xfs_btree_shift_recs(cur, rp, 1, numrecs - ptr + 1);
3368
3369 /* Now put the new data in, bump numrecs and log it. */
d3cd7a27 3370 xfs_btree_copy_recs(cur, rp, rec, 1);
b194c7d8
BN
3371 xfs_btree_set_numrecs(block, ++numrecs);
3372 xfs_btree_log_recs(cur, bp, ptr, numrecs);
3373#ifdef DEBUG
3374 if (ptr < numrecs) {
3375 ASSERT(cur->bc_ops->recs_inorder(cur, rp,
3376 xfs_btree_rec_addr(cur, ptr + 1, block)));
3377 }
3378#endif
3379 }
3380
3381 /* Log the new number of records in the btree header. */
3382 xfs_btree_log_block(cur, bp, XFS_BB_NUMRECS);
3383
13e831e0
DW
3384 /*
3385 * If we just inserted into a new tree block, we have to
3386 * recalculate nkey here because nkey is out of date.
3387 *
3388 * Otherwise we're just updating an existing block (having shoved
3389 * some records into the new tree block), so use the regular key
3390 * update mechanism.
3391 */
3392 if (bp && bp->b_bn != old_bn) {
3393 xfs_btree_get_keys(cur, block, lkey);
3394 } else if (xfs_btree_needs_key_update(cur, optr)) {
64dbe047 3395 error = xfs_btree_update_keys(cur, level);
b194c7d8
BN
3396 if (error)
3397 goto error0;
3398 }
3399
3400 /*
3401 * If we are tracking the last record in the tree and
3402 * we are at the far right edge of the tree, update it.
3403 */
3404 if (xfs_btree_is_lastrec(cur, block, level)) {
d3cd7a27 3405 cur->bc_ops->update_lastrec(cur, block, rec,
b194c7d8
BN
3406 ptr, LASTREC_INSREC);
3407 }
3408
3409 /*
3410 * Return the new block number, if any.
3411 * If there is one, give back a record value and a cursor too.
3412 */
3413 *ptrp = nptr;
3414 if (!xfs_btree_ptr_is_null(cur, &nptr)) {
13e831e0 3415 xfs_btree_copy_keys(cur, key, lkey, 1);
b194c7d8
BN
3416 *curp = ncur;
3417 }
3418
b194c7d8
BN
3419 *stat = 1;
3420 return 0;
3421
3422error0:
b194c7d8
BN
3423 return error;
3424}
3425
3426/*
3427 * Insert the record at the point referenced by cur.
3428 *
3429 * A multi-level split of the tree on insert will invalidate the original
3430 * cursor. All callers of this function should assume that the cursor is
3431 * no longer valid and revalidate it.
3432 */
3433int
3434xfs_btree_insert(
3435 struct xfs_btree_cur *cur,
3436 int *stat)
3437{
3438 int error; /* error return value */
3439 int i; /* result value, 0 for failure */
3440 int level; /* current level number in btree */
3441 union xfs_btree_ptr nptr; /* new block number (split result) */
3442 struct xfs_btree_cur *ncur; /* new cursor (split result) */
3443 struct xfs_btree_cur *pcur; /* previous level's cursor */
45413937 3444 union xfs_btree_key bkey; /* key of block to insert */
13e831e0 3445 union xfs_btree_key *key;
b194c7d8
BN
3446 union xfs_btree_rec rec; /* record to insert */
3447
3448 level = 0;
3449 ncur = NULL;
3450 pcur = cur;
45413937 3451 key = &bkey;
b194c7d8
BN
3452
3453 xfs_btree_set_ptr_null(cur, &nptr);
d3cd7a27
DW
3454
3455 /* Make a key out of the record data to be inserted, and save it. */
b194c7d8 3456 cur->bc_ops->init_rec_from_cur(cur, &rec);
13e831e0 3457 cur->bc_ops->init_key_from_rec(key, &rec);
b194c7d8
BN
3458
3459 /*
3460 * Loop going up the tree, starting at the leaf level.
3461 * Stop when we don't get a split block, that must mean that
3462 * the insert is finished with this level.
3463 */
3464 do {
3465 /*
3466 * Insert nrec/nptr into this level of the tree.
3467 * Note if we fail, nptr will be null.
3468 */
13e831e0 3469 error = xfs_btree_insrec(pcur, level, &nptr, &rec, key,
d3cd7a27 3470 &ncur, &i);
b194c7d8
BN
3471 if (error) {
3472 if (pcur != cur)
3473 xfs_btree_del_cursor(pcur, XFS_BTREE_ERROR);
3474 goto error0;
3475 }
3476
19ebedcf 3477 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, i == 1, error0);
b194c7d8
BN
3478 level++;
3479
3480 /*
3481 * See if the cursor we just used is trash.
3482 * Can't trash the caller's cursor, but otherwise we should
3483 * if ncur is a new cursor or we're about to be done.
3484 */
3485 if (pcur != cur &&
3486 (ncur || xfs_btree_ptr_is_null(cur, &nptr))) {
3487 /* Save the state from the cursor before we trash it */
3488 if (cur->bc_ops->update_cursor)
3489 cur->bc_ops->update_cursor(pcur, cur);
3490 cur->bc_nlevels = pcur->bc_nlevels;
3491 xfs_btree_del_cursor(pcur, XFS_BTREE_NOERROR);
3492 }
3493 /* If we got a new cursor, switch to it. */
3494 if (ncur) {
3495 pcur = ncur;
3496 ncur = NULL;
3497 }
3498 } while (!xfs_btree_ptr_is_null(cur, &nptr));
3499
b194c7d8
BN
3500 *stat = i;
3501 return 0;
3502error0:
b194c7d8
BN
3503 return error;
3504}
3505
3506/*
3507 * Try to merge a non-leaf block back into the inode root.
3508 *
3509 * Note: the killroot names comes from the fact that we're effectively
3510 * killing the old root block. But because we can't just delete the
3511 * inode we have to copy the single block it was pointing to into the
3512 * inode.
3513 */
56b2de80 3514STATIC int
b194c7d8
BN
3515xfs_btree_kill_iroot(
3516 struct xfs_btree_cur *cur)
3517{
3518 int whichfork = cur->bc_private.b.whichfork;
3519 struct xfs_inode *ip = cur->bc_private.b.ip;
3520 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
3521 struct xfs_btree_block *block;
3522 struct xfs_btree_block *cblock;
3523 union xfs_btree_key *kp;
3524 union xfs_btree_key *ckp;
3525 union xfs_btree_ptr *pp;
3526 union xfs_btree_ptr *cpp;
3527 struct xfs_buf *cbp;
3528 int level;
3529 int index;
3530 int numrecs;
410c3de5 3531 int error;
b194c7d8
BN
3532#ifdef DEBUG
3533 union xfs_btree_ptr ptr;
b194c7d8 3534#endif
efa7dddb 3535 int i;
b194c7d8 3536
b194c7d8
BN
3537 ASSERT(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE);
3538 ASSERT(cur->bc_nlevels > 1);
3539
3540 /*
3541 * Don't deal with the root block needs to be a leaf case.
3542 * We're just going to turn the thing back into extents anyway.
3543 */
3544 level = cur->bc_nlevels - 1;
3545 if (level == 1)
3546 goto out0;
3547
3548 /*
3549 * Give up if the root has multiple children.
3550 */
3551 block = xfs_btree_get_iroot(cur);
3552 if (xfs_btree_get_numrecs(block) != 1)
3553 goto out0;
3554
3555 cblock = xfs_btree_get_block(cur, level - 1, &cbp);
3556 numrecs = xfs_btree_get_numrecs(cblock);
3557
3558 /*
3559 * Only do this if the next level will fit.
3560 * Then the data must be copied up to the inode,
3561 * instead of freeing the root you free the next level.
3562 */
3563 if (numrecs > cur->bc_ops->get_dmaxrecs(cur, level))
3564 goto out0;
3565
3566 XFS_BTREE_STATS_INC(cur, killroot);
3567
3568#ifdef DEBUG
3569 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_LEFTSIB);
3570 ASSERT(xfs_btree_ptr_is_null(cur, &ptr));
3571 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_RIGHTSIB);
3572 ASSERT(xfs_btree_ptr_is_null(cur, &ptr));
3573#endif
3574
3575 index = numrecs - cur->bc_ops->get_maxrecs(cur, level);
3576 if (index) {
3577 xfs_iroot_realloc(cur->bc_private.b.ip, index,
3578 cur->bc_private.b.whichfork);
b3563c19 3579 block = ifp->if_broot;
b194c7d8
BN
3580 }
3581
3582 be16_add_cpu(&block->bb_numrecs, index);
3583 ASSERT(block->bb_numrecs == cblock->bb_numrecs);
3584
3585 kp = xfs_btree_key_addr(cur, 1, block);
3586 ckp = xfs_btree_key_addr(cur, 1, cblock);
3587 xfs_btree_copy_keys(cur, kp, ckp, numrecs);
3588
3589 pp = xfs_btree_ptr_addr(cur, 1, block);
3590 cpp = xfs_btree_ptr_addr(cur, 1, cblock);
efa7dddb 3591
b194c7d8 3592 for (i = 0; i < numrecs; i++) {
efa7dddb 3593 error = xfs_btree_debug_check_ptr(cur, cpp, i, level - 1);
97b3ffd0 3594 if (error)
b194c7d8 3595 return error;
b194c7d8 3596 }
efa7dddb 3597
b194c7d8
BN
3598 xfs_btree_copy_ptrs(cur, pp, cpp, numrecs);
3599
c261f8c0 3600 error = xfs_btree_free_block(cur, cbp);
97b3ffd0 3601 if (error)
410c3de5 3602 return error;
b194c7d8
BN
3603
3604 cur->bc_bufs[level - 1] = NULL;
3605 be16_add_cpu(&block->bb_level, -1);
3606 xfs_trans_log_inode(cur->bc_tp, ip,
56b2de80 3607 XFS_ILOG_CORE | xfs_ilog_fbroot(cur->bc_private.b.whichfork));
b194c7d8
BN
3608 cur->bc_nlevels--;
3609out0:
b194c7d8
BN
3610 return 0;
3611}
3612
56b2de80
DC
3613/*
3614 * Kill the current root node, and replace it with it's only child node.
3615 */
3616STATIC int
3617xfs_btree_kill_root(
3618 struct xfs_btree_cur *cur,
3619 struct xfs_buf *bp,
3620 int level,
3621 union xfs_btree_ptr *newroot)
3622{
3623 int error;
3624
56b2de80
DC
3625 XFS_BTREE_STATS_INC(cur, killroot);
3626
3627 /*
3628 * Update the root pointer, decreasing the level by 1 and then
3629 * free the old root.
3630 */
3631 cur->bc_ops->set_root(cur, newroot, -1);
3632
c261f8c0 3633 error = xfs_btree_free_block(cur, bp);
97b3ffd0 3634 if (error)
56b2de80 3635 return error;
56b2de80 3636
56b2de80
DC
3637 cur->bc_bufs[level] = NULL;
3638 cur->bc_ra[level] = 0;
3639 cur->bc_nlevels--;
3640
56b2de80
DC
3641 return 0;
3642}
3643
b194c7d8
BN
3644STATIC int
3645xfs_btree_dec_cursor(
3646 struct xfs_btree_cur *cur,
3647 int level,
3648 int *stat)
3649{
3650 int error;
3651 int i;
3652
3653 if (level > 0) {
3654 error = xfs_btree_decrement(cur, level, &i);
3655 if (error)
3656 return error;
3657 }
3658
b194c7d8
BN
3659 *stat = 1;
3660 return 0;
3661}
3662
3663/*
3664 * Single level of the btree record deletion routine.
3665 * Delete record pointed to by cur/level.
3666 * Remove the record from its block then rebalance the tree.
3667 * Return 0 for error, 1 for done, 2 to go on to the next level.
3668 */
3669STATIC int /* error */
3670xfs_btree_delrec(
3671 struct xfs_btree_cur *cur, /* btree cursor */
3672 int level, /* level removing record from */
3673 int *stat) /* fail/done/go-on */
3674{
3675 struct xfs_btree_block *block; /* btree block */
3676 union xfs_btree_ptr cptr; /* current block ptr */
3677 struct xfs_buf *bp; /* buffer for block */
3678 int error; /* error return value */
3679 int i; /* loop counter */
b194c7d8
BN
3680 union xfs_btree_ptr lptr; /* left sibling block ptr */
3681 struct xfs_buf *lbp; /* left buffer pointer */
3682 struct xfs_btree_block *left; /* left btree block */
3683 int lrecs = 0; /* left record count */
3684 int ptr; /* key/record index */
3685 union xfs_btree_ptr rptr; /* right sibling block ptr */
3686 struct xfs_buf *rbp; /* right buffer pointer */
3687 struct xfs_btree_block *right; /* right btree block */
3688 struct xfs_btree_block *rrblock; /* right-right btree block */
3689 struct xfs_buf *rrbp; /* right-right buffer pointer */
3690 int rrecs = 0; /* right record count */
3691 struct xfs_btree_cur *tcur; /* temporary btree cursor */
3692 int numrecs; /* temporary numrec count */
3693
b194c7d8
BN
3694 tcur = NULL;
3695
3696 /* Get the index of the entry being deleted, check for nothing there. */
3697 ptr = cur->bc_ptrs[level];
3698 if (ptr == 0) {
b194c7d8
BN
3699 *stat = 0;
3700 return 0;
3701 }
3702
3703 /* Get the buffer & block containing the record or key/ptr. */
3704 block = xfs_btree_get_block(cur, level, &bp);
3705 numrecs = xfs_btree_get_numrecs(block);
3706
3707#ifdef DEBUG
3708 error = xfs_btree_check_block(cur, block, level, bp);
3709 if (error)
3710 goto error0;
3711#endif
3712
3713 /* Fail if we're off the end of the block. */
3714 if (ptr > numrecs) {
b194c7d8
BN
3715 *stat = 0;
3716 return 0;
3717 }
3718
3719 XFS_BTREE_STATS_INC(cur, delrec);
3720 XFS_BTREE_STATS_ADD(cur, moves, numrecs - ptr);
3721
3722 /* Excise the entries being deleted. */
3723 if (level > 0) {
3724 /* It's a nonleaf. operate on keys and ptrs */
3725 union xfs_btree_key *lkp;
3726 union xfs_btree_ptr *lpp;
3727
3728 lkp = xfs_btree_key_addr(cur, ptr + 1, block);
3729 lpp = xfs_btree_ptr_addr(cur, ptr + 1, block);
3730
b194c7d8 3731 for (i = 0; i < numrecs - ptr; i++) {
efa7dddb 3732 error = xfs_btree_debug_check_ptr(cur, lpp, i, level);
b194c7d8
BN
3733 if (error)
3734 goto error0;
3735 }
b194c7d8
BN
3736
3737 if (ptr < numrecs) {
3738 xfs_btree_shift_keys(cur, lkp, -1, numrecs - ptr);
3739 xfs_btree_shift_ptrs(cur, lpp, -1, numrecs - ptr);
3740 xfs_btree_log_keys(cur, bp, ptr, numrecs - 1);
3741 xfs_btree_log_ptrs(cur, bp, ptr, numrecs - 1);
3742 }
b194c7d8
BN
3743 } else {
3744 /* It's a leaf. operate on records */
3745 if (ptr < numrecs) {
3746 xfs_btree_shift_recs(cur,
3747 xfs_btree_rec_addr(cur, ptr + 1, block),
3748 -1, numrecs - ptr);
3749 xfs_btree_log_recs(cur, bp, ptr, numrecs - 1);
3750 }
b194c7d8
BN
3751 }
3752
3753 /*
3754 * Decrement and log the number of entries in the block.
3755 */
3756 xfs_btree_set_numrecs(block, --numrecs);
3757 xfs_btree_log_block(cur, bp, XFS_BB_NUMRECS);
3758
3759 /*
3760 * If we are tracking the last record in the tree and
3761 * we are at the far right edge of the tree, update it.
3762 */
3763 if (xfs_btree_is_lastrec(cur, block, level)) {
3764 cur->bc_ops->update_lastrec(cur, block, NULL,
3765 ptr, LASTREC_DELREC);
3766 }
3767
3768 /*
3769 * We're at the root level. First, shrink the root block in-memory.
3770 * Try to get rid of the next level down. If we can't then there's
3771 * nothing left to do.
3772 */
3773 if (level == cur->bc_nlevels - 1) {
3774 if (cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) {
3775 xfs_iroot_realloc(cur->bc_private.b.ip, -1,
3776 cur->bc_private.b.whichfork);
3777
3778 error = xfs_btree_kill_iroot(cur);
3779 if (error)
3780 goto error0;
3781
3782 error = xfs_btree_dec_cursor(cur, level, stat);
3783 if (error)
3784 goto error0;
3785 *stat = 1;
3786 return 0;
3787 }
3788
3789 /*
3790 * If this is the root level, and there's only one entry left,
3791 * and it's NOT the leaf level, then we can get rid of this
3792 * level.
3793 */
3794 if (numrecs == 1 && level > 0) {
3795 union xfs_btree_ptr *pp;
3796 /*
3797 * pp is still set to the first pointer in the block.
3798 * Make it the new root of the btree.
3799 */
3800 pp = xfs_btree_ptr_addr(cur, 1, block);
56b2de80 3801 error = xfs_btree_kill_root(cur, bp, level, pp);
b194c7d8
BN
3802 if (error)
3803 goto error0;
3804 } else if (level > 0) {
3805 error = xfs_btree_dec_cursor(cur, level, stat);
3806 if (error)
3807 goto error0;
3808 }
3809 *stat = 1;
3810 return 0;
3811 }
3812
3813 /*
3814 * If we deleted the leftmost entry in the block, update the
3815 * key values above us in the tree.
3816 */
a3c9cb10 3817 if (xfs_btree_needs_key_update(cur, ptr)) {
64dbe047 3818 error = xfs_btree_update_keys(cur, level);
b194c7d8
BN
3819 if (error)
3820 goto error0;
3821 }
3822
3823 /*
3824 * If the number of records remaining in the block is at least
3825 * the minimum, we're done.
3826 */
3827 if (numrecs >= cur->bc_ops->get_minrecs(cur, level)) {
3828 error = xfs_btree_dec_cursor(cur, level, stat);
3829 if (error)
3830 goto error0;
3831 return 0;
3832 }
3833
3834 /*
3835 * Otherwise, we have to move some records around to keep the
3836 * tree balanced. Look at the left and right sibling blocks to
3837 * see if we can re-balance by moving only one record.
3838 */
3839 xfs_btree_get_sibling(cur, block, &rptr, XFS_BB_RIGHTSIB);
3840 xfs_btree_get_sibling(cur, block, &lptr, XFS_BB_LEFTSIB);
3841
3842 if (cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) {
3843 /*
3844 * One child of root, need to get a chance to copy its contents
3845 * into the root and delete it. Can't go up to next level,
3846 * there's nothing to delete there.
3847 */
3848 if (xfs_btree_ptr_is_null(cur, &rptr) &&
3849 xfs_btree_ptr_is_null(cur, &lptr) &&
3850 level == cur->bc_nlevels - 2) {
3851 error = xfs_btree_kill_iroot(cur);
3852 if (!error)
3853 error = xfs_btree_dec_cursor(cur, level, stat);
3854 if (error)
3855 goto error0;
3856 return 0;
3857 }
3858 }
3859
3860 ASSERT(!xfs_btree_ptr_is_null(cur, &rptr) ||
3861 !xfs_btree_ptr_is_null(cur, &lptr));
3862
3863 /*
3864 * Duplicate the cursor so our btree manipulations here won't
3865 * disrupt the next level up.
3866 */
3867 error = xfs_btree_dup_cursor(cur, &tcur);
3868 if (error)
3869 goto error0;
3870
3871 /*
3872 * If there's a right sibling, see if it's ok to shift an entry
3873 * out of it.
3874 */
3875 if (!xfs_btree_ptr_is_null(cur, &rptr)) {
3876 /*
3877 * Move the temp cursor to the last entry in the next block.
3878 * Actually any entry but the first would suffice.
3879 */
3880 i = xfs_btree_lastrec(tcur, level);
19ebedcf 3881 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, i == 1, error0);
b194c7d8
BN
3882
3883 error = xfs_btree_increment(tcur, level, &i);
3884 if (error)
3885 goto error0;
19ebedcf 3886 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, i == 1, error0);
b194c7d8
BN
3887
3888 i = xfs_btree_lastrec(tcur, level);
19ebedcf 3889 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, i == 1, error0);
b194c7d8
BN
3890
3891 /* Grab a pointer to the block. */
3892 right = xfs_btree_get_block(tcur, level, &rbp);
3893#ifdef DEBUG
3894 error = xfs_btree_check_block(tcur, right, level, rbp);
3895 if (error)
3896 goto error0;
3897#endif
3898 /* Grab the current block number, for future use. */
3899 xfs_btree_get_sibling(tcur, right, &cptr, XFS_BB_LEFTSIB);
3900
3901 /*
3902 * If right block is full enough so that removing one entry
3903 * won't make it too empty, and left-shifting an entry out
3904 * of right to us works, we're done.
3905 */
3906 if (xfs_btree_get_numrecs(right) - 1 >=
3907 cur->bc_ops->get_minrecs(tcur, level)) {
3908 error = xfs_btree_lshift(tcur, level, &i);
3909 if (error)
3910 goto error0;
3911 if (i) {
3912 ASSERT(xfs_btree_get_numrecs(block) >=
3913 cur->bc_ops->get_minrecs(tcur, level));
3914
3915 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
3916 tcur = NULL;
3917
3918 error = xfs_btree_dec_cursor(cur, level, stat);
3919 if (error)
3920 goto error0;
3921 return 0;
3922 }
3923 }
3924
3925 /*
3926 * Otherwise, grab the number of records in right for
3927 * future reference, and fix up the temp cursor to point
3928 * to our block again (last record).
3929 */
3930 rrecs = xfs_btree_get_numrecs(right);
3931 if (!xfs_btree_ptr_is_null(cur, &lptr)) {
3932 i = xfs_btree_firstrec(tcur, level);
19ebedcf 3933 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, i == 1, error0);
b194c7d8
BN
3934
3935 error = xfs_btree_decrement(tcur, level, &i);
3936 if (error)
3937 goto error0;
19ebedcf 3938 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, i == 1, error0);
b194c7d8
BN
3939 }
3940 }
3941
3942 /*
3943 * If there's a left sibling, see if it's ok to shift an entry
3944 * out of it.
3945 */
3946 if (!xfs_btree_ptr_is_null(cur, &lptr)) {
3947 /*
3948 * Move the temp cursor to the first entry in the
3949 * previous block.
3950 */
3951 i = xfs_btree_firstrec(tcur, level);
19ebedcf 3952 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, i == 1, error0);
b194c7d8
BN
3953
3954 error = xfs_btree_decrement(tcur, level, &i);
3955 if (error)
3956 goto error0;
3957 i = xfs_btree_firstrec(tcur, level);
19ebedcf 3958 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, i == 1, error0);
b194c7d8
BN
3959
3960 /* Grab a pointer to the block. */
3961 left = xfs_btree_get_block(tcur, level, &lbp);
3962#ifdef DEBUG
3963 error = xfs_btree_check_block(cur, left, level, lbp);
3964 if (error)
3965 goto error0;
3966#endif
3967 /* Grab the current block number, for future use. */
3968 xfs_btree_get_sibling(tcur, left, &cptr, XFS_BB_RIGHTSIB);
3969
3970 /*
3971 * If left block is full enough so that removing one entry
3972 * won't make it too empty, and right-shifting an entry out
3973 * of left to us works, we're done.
3974 */
3975 if (xfs_btree_get_numrecs(left) - 1 >=
3976 cur->bc_ops->get_minrecs(tcur, level)) {
3977 error = xfs_btree_rshift(tcur, level, &i);
3978 if (error)
3979 goto error0;
3980 if (i) {
3981 ASSERT(xfs_btree_get_numrecs(block) >=
3982 cur->bc_ops->get_minrecs(tcur, level));
3983 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
3984 tcur = NULL;
3985 if (level == 0)
3986 cur->bc_ptrs[0]++;
97b3ffd0 3987
b194c7d8
BN
3988 *stat = 1;
3989 return 0;
3990 }
3991 }
3992
3993 /*
3994 * Otherwise, grab the number of records in right for
3995 * future reference.
3996 */
3997 lrecs = xfs_btree_get_numrecs(left);
3998 }
3999
4000 /* Delete the temp cursor, we're done with it. */
4001 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
4002 tcur = NULL;
4003
4004 /* If here, we need to do a join to keep the tree balanced. */
4005 ASSERT(!xfs_btree_ptr_is_null(cur, &cptr));
4006
4007 if (!xfs_btree_ptr_is_null(cur, &lptr) &&
4008 lrecs + xfs_btree_get_numrecs(block) <=
4009 cur->bc_ops->get_maxrecs(cur, level)) {
4010 /*
4011 * Set "right" to be the starting block,
4012 * "left" to be the left neighbor.
4013 */
4014 rptr = cptr;
4015 right = block;
4016 rbp = bp;
ff105f75 4017 error = xfs_btree_read_buf_block(cur, &lptr, 0, &left, &lbp);
b194c7d8
BN
4018 if (error)
4019 goto error0;
4020
4021 /*
4022 * If that won't work, see if we can join with the right neighbor block.
4023 */
4024 } else if (!xfs_btree_ptr_is_null(cur, &rptr) &&
4025 rrecs + xfs_btree_get_numrecs(block) <=
4026 cur->bc_ops->get_maxrecs(cur, level)) {
4027 /*
4028 * Set "left" to be the starting block,
4029 * "right" to be the right neighbor.
4030 */
4031 lptr = cptr;
4032 left = block;
4033 lbp = bp;
ff105f75 4034 error = xfs_btree_read_buf_block(cur, &rptr, 0, &right, &rbp);
b194c7d8
BN
4035 if (error)
4036 goto error0;
4037
4038 /*
4039 * Otherwise, we can't fix the imbalance.
4040 * Just return. This is probably a logic error, but it's not fatal.
4041 */
4042 } else {
4043 error = xfs_btree_dec_cursor(cur, level, stat);
4044 if (error)
4045 goto error0;
4046 return 0;
4047 }
4048
4049 rrecs = xfs_btree_get_numrecs(right);
4050 lrecs = xfs_btree_get_numrecs(left);
4051
4052 /*
4053 * We're now going to join "left" and "right" by moving all the stuff
4054 * in "right" to "left" and deleting "right".
4055 */
4056 XFS_BTREE_STATS_ADD(cur, moves, rrecs);
4057 if (level > 0) {
4058 /* It's a non-leaf. Move keys and pointers. */
4059 union xfs_btree_key *lkp; /* left btree key */
4060 union xfs_btree_ptr *lpp; /* left address pointer */
4061 union xfs_btree_key *rkp; /* right btree key */
4062 union xfs_btree_ptr *rpp; /* right address pointer */
4063
4064 lkp = xfs_btree_key_addr(cur, lrecs + 1, left);
4065 lpp = xfs_btree_ptr_addr(cur, lrecs + 1, left);
4066 rkp = xfs_btree_key_addr(cur, 1, right);
4067 rpp = xfs_btree_ptr_addr(cur, 1, right);
efa7dddb 4068
b194c7d8 4069 for (i = 1; i < rrecs; i++) {
efa7dddb 4070 error = xfs_btree_debug_check_ptr(cur, rpp, i, level);
b194c7d8
BN
4071 if (error)
4072 goto error0;
4073 }
efa7dddb 4074
b194c7d8
BN
4075 xfs_btree_copy_keys(cur, lkp, rkp, rrecs);
4076 xfs_btree_copy_ptrs(cur, lpp, rpp, rrecs);
4077
4078 xfs_btree_log_keys(cur, lbp, lrecs + 1, lrecs + rrecs);
4079 xfs_btree_log_ptrs(cur, lbp, lrecs + 1, lrecs + rrecs);
4080 } else {
4081 /* It's a leaf. Move records. */
4082 union xfs_btree_rec *lrp; /* left record pointer */
4083 union xfs_btree_rec *rrp; /* right record pointer */
4084
4085 lrp = xfs_btree_rec_addr(cur, lrecs + 1, left);
4086 rrp = xfs_btree_rec_addr(cur, 1, right);
4087
4088 xfs_btree_copy_recs(cur, lrp, rrp, rrecs);
4089 xfs_btree_log_recs(cur, lbp, lrecs + 1, lrecs + rrecs);
4090 }
4091
4092 XFS_BTREE_STATS_INC(cur, join);
4093
4094 /*
56b2de80 4095 * Fix up the number of records and right block pointer in the
b194c7d8
BN
4096 * surviving block, and log it.
4097 */
4098 xfs_btree_set_numrecs(left, lrecs + rrecs);
4099 xfs_btree_get_sibling(cur, right, &cptr, XFS_BB_RIGHTSIB),
4100 xfs_btree_set_sibling(cur, left, &cptr, XFS_BB_RIGHTSIB);
4101 xfs_btree_log_block(cur, lbp, XFS_BB_NUMRECS | XFS_BB_RIGHTSIB);
4102
4103 /* If there is a right sibling, point it to the remaining block. */
4104 xfs_btree_get_sibling(cur, left, &cptr, XFS_BB_RIGHTSIB);
4105 if (!xfs_btree_ptr_is_null(cur, &cptr)) {
ff105f75 4106 error = xfs_btree_read_buf_block(cur, &cptr, 0, &rrblock, &rrbp);
b194c7d8
BN
4107 if (error)
4108 goto error0;
4109 xfs_btree_set_sibling(cur, rrblock, &lptr, XFS_BB_LEFTSIB);
4110 xfs_btree_log_block(cur, rrbp, XFS_BB_LEFTSIB);
4111 }
4112
4113 /* Free the deleted block. */
c261f8c0 4114 error = xfs_btree_free_block(cur, rbp);
b194c7d8
BN
4115 if (error)
4116 goto error0;
b194c7d8
BN
4117
4118 /*
4119 * If we joined with the left neighbor, set the buffer in the
4120 * cursor to the left block, and fix up the index.
4121 */
4122 if (bp != lbp) {
4123 cur->bc_bufs[level] = lbp;
4124 cur->bc_ptrs[level] += lrecs;
4125 cur->bc_ra[level] = 0;
4126 }
4127 /*
4128 * If we joined with the right neighbor and there's a level above
4129 * us, increment the cursor at that level.
4130 */
4131 else if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) ||
4132 (level + 1 < cur->bc_nlevels)) {
4133 error = xfs_btree_increment(cur, level + 1, &i);
4134 if (error)
4135 goto error0;
4136 }
4137
4138 /*
4139 * Readjust the ptr at this level if it's not a leaf, since it's
4140 * still pointing at the deletion point, which makes the cursor
4141 * inconsistent. If this makes the ptr 0, the caller fixes it up.
4142 * We can't use decrement because it would change the next level up.
4143 */
4144 if (level > 0)
4145 cur->bc_ptrs[level]--;
4146
13e831e0
DW
4147 /*
4148 * We combined blocks, so we have to update the parent keys if the
4149 * btree supports overlapped intervals. However, bc_ptrs[level + 1]
4150 * points to the old block so that the caller knows which record to
4151 * delete. Therefore, the caller must be savvy enough to call updkeys
4152 * for us if we return stat == 2. The other exit points from this
4153 * function don't require deletions further up the tree, so they can
4154 * call updkeys directly.
4155 */
4156
b194c7d8
BN
4157 /* Return value means the next level up has something to do. */
4158 *stat = 2;
4159 return 0;
4160
4161error0:
b194c7d8
BN
4162 if (tcur)
4163 xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
4164 return error;
4165}
4166
4167/*
4168 * Delete the record pointed to by cur.
4169 * The cursor refers to the place where the record was (could be inserted)
4170 * when the operation returns.
4171 */
4172int /* error */
4173xfs_btree_delete(
4174 struct xfs_btree_cur *cur,
4175 int *stat) /* success/failure */
4176{
4177 int error; /* error return value */
4178 int level;
4179 int i;
13e831e0 4180 bool joined = false;
b194c7d8 4181
b194c7d8
BN
4182 /*
4183 * Go up the tree, starting at leaf level.
4184 *
4185 * If 2 is returned then a join was done; go to the next level.
4186 * Otherwise we are done.
4187 */
4188 for (level = 0, i = 2; i == 2; level++) {
4189 error = xfs_btree_delrec(cur, level, &i);
4190 if (error)
4191 goto error0;
13e831e0
DW
4192 if (i == 2)
4193 joined = true;
4194 }
4195
4196 /*
4197 * If we combined blocks as part of deleting the record, delrec won't
4198 * have updated the parent high keys so we have to do that here.
4199 */
4200 if (joined && (cur->bc_flags & XFS_BTREE_OVERLAPPING)) {
4201 error = xfs_btree_updkeys_force(cur, 0);
4202 if (error)
4203 goto error0;
b194c7d8
BN
4204 }
4205
4206 if (i == 0) {
4207 for (level = 1; level < cur->bc_nlevels; level++) {
4208 if (cur->bc_ptrs[level] == 0) {
4209 error = xfs_btree_decrement(cur, level, &i);
4210 if (error)
4211 goto error0;
4212 break;
4213 }
4214 }
4215 }
4216
b194c7d8
BN
4217 *stat = i;
4218 return 0;
4219error0:
b194c7d8
BN
4220 return error;
4221}
4222
4223/*
4224 * Get the data from the pointed-to record.
4225 */
4226int /* error */
4227xfs_btree_get_rec(
4228 struct xfs_btree_cur *cur, /* btree cursor */
4229 union xfs_btree_rec **recp, /* output: btree record */
4230 int *stat) /* output: success/failure */
4231{
4232 struct xfs_btree_block *block; /* btree block */
4233 struct xfs_buf *bp; /* buffer pointer */
4234 int ptr; /* record number */
4235#ifdef DEBUG
4236 int error; /* error return value */
4237#endif
4238
4239 ptr = cur->bc_ptrs[0];
4240 block = xfs_btree_get_block(cur, 0, &bp);
4241
4242#ifdef DEBUG
4243 error = xfs_btree_check_block(cur, block, 0, bp);
4244 if (error)
4245 return error;
4246#endif
4247
4248 /*
4249 * Off the right end or left end, return failure.
4250 */
4251 if (ptr > xfs_btree_get_numrecs(block) || ptr <= 0) {
4252 *stat = 0;
4253 return 0;
4254 }
4255
4256 /*
4257 * Point to the record and extract its data.
4258 */
4259 *recp = xfs_btree_rec_addr(cur, ptr, block);
4260 *stat = 1;
4261 return 0;
4262}
9c6ebc42 4263
f31736bf
DW
4264/* Visit a block in a btree. */
4265STATIC int
4266xfs_btree_visit_block(
4267 struct xfs_btree_cur *cur,
4268 int level,
4269 xfs_btree_visit_blocks_fn fn,
4270 void *data)
4271{
4272 struct xfs_btree_block *block;
4273 struct xfs_buf *bp;
4274 union xfs_btree_ptr rptr;
4275 int error;
4276
4277 /* do right sibling readahead */
4278 xfs_btree_readahead(cur, level, XFS_BTCUR_RIGHTRA);
4279 block = xfs_btree_get_block(cur, level, &bp);
4280
4281 /* process the block */
4282 error = fn(cur, level, data);
4283 if (error)
4284 return error;
4285
4286 /* now read rh sibling block for next iteration */
4287 xfs_btree_get_sibling(cur, block, &rptr, XFS_BB_RIGHTSIB);
4288 if (xfs_btree_ptr_is_null(cur, &rptr))
4289 return -ENOENT;
4290
4291 return xfs_btree_lookup_get_block(cur, level, &rptr, &block);
4292}
4293
4294
4295/* Visit every block in a btree. */
4296int
4297xfs_btree_visit_blocks(
4298 struct xfs_btree_cur *cur,
4299 xfs_btree_visit_blocks_fn fn,
4300 void *data)
4301{
4302 union xfs_btree_ptr lptr;
4303 int level;
4304 struct xfs_btree_block *block = NULL;
4305 int error = 0;
4306
4307 cur->bc_ops->init_ptr_from_cur(cur, &lptr);
4308
4309 /* for each level */
4310 for (level = cur->bc_nlevels - 1; level >= 0; level--) {
4311 /* grab the left hand block */
4312 error = xfs_btree_lookup_get_block(cur, level, &lptr, &block);
4313 if (error)
4314 return error;
4315
4316 /* readahead the left most block for the next level down */
4317 if (level > 0) {
4318 union xfs_btree_ptr *ptr;
4319
4320 ptr = xfs_btree_ptr_addr(cur, 1, block);
4321 xfs_btree_readahead_ptr(cur, ptr, 1);
4322
4323 /* save for the next iteration of the loop */
f9a6d642 4324 xfs_btree_copy_ptrs(cur, &lptr, ptr, 1);
f31736bf
DW
4325 }
4326
4327 /* for each buffer in the level */
4328 do {
4329 error = xfs_btree_visit_block(cur, level, fn, data);
4330 } while (!error);
4331
4332 if (error != -ENOENT)
4333 return error;
4334 }
4335
4336 return 0;
4337}
4338
9c6ebc42
DC
4339/*
4340 * Change the owner of a btree.
4341 *
4342 * The mechanism we use here is ordered buffer logging. Because we don't know
4343 * how many buffers were are going to need to modify, we don't really want to
4344 * have to make transaction reservations for the worst case of every buffer in a
4345 * full size btree as that may be more space that we can fit in the log....
4346 *
4347 * We do the btree walk in the most optimal manner possible - we have sibling
4348 * pointers so we can just walk all the blocks on each level from left to right
4349 * in a single pass, and then move to the next level and do the same. We can
4350 * also do readahead on the sibling pointers to get IO moving more quickly,
4351 * though for slow disks this is unlikely to make much difference to performance
4352 * as the amount of CPU work we have to do before moving to the next block is
4353 * relatively small.
4354 *
4355 * For each btree block that we load, modify the owner appropriately, set the
4356 * buffer as an ordered buffer and log it appropriately. We need to ensure that
4357 * we mark the region we change dirty so that if the buffer is relogged in
4358 * a subsequent transaction the changes we make here as an ordered buffer are
4359 * correctly relogged in that transaction. If we are in recovery context, then
4360 * just queue the modified buffer as delayed write buffer so the transaction
4361 * recovery completion writes the changes to disk.
4362 */
f31736bf 4363struct xfs_btree_block_change_owner_info {
4a492e72 4364 uint64_t new_owner;
f31736bf
DW
4365 struct list_head *buffer_list;
4366};
4367
9c6ebc42
DC
4368static int
4369xfs_btree_block_change_owner(
4370 struct xfs_btree_cur *cur,
4371 int level,
f31736bf 4372 void *data)
9c6ebc42 4373{
f31736bf 4374 struct xfs_btree_block_change_owner_info *bbcoi = data;
9c6ebc42
DC
4375 struct xfs_btree_block *block;
4376 struct xfs_buf *bp;
9c6ebc42
DC
4377
4378 /* modify the owner */
4379 block = xfs_btree_get_block(cur, level, &bp);
38fa71a7
BF
4380 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
4381 if (block->bb_u.l.bb_owner == cpu_to_be64(bbcoi->new_owner))
4382 return 0;
f31736bf 4383 block->bb_u.l.bb_owner = cpu_to_be64(bbcoi->new_owner);
38fa71a7
BF
4384 } else {
4385 if (block->bb_u.s.bb_owner == cpu_to_be32(bbcoi->new_owner))
4386 return 0;
f31736bf 4387 block->bb_u.s.bb_owner = cpu_to_be32(bbcoi->new_owner);
38fa71a7 4388 }
9c6ebc42
DC
4389
4390 /*
4391 * If the block is a root block hosted in an inode, we might not have a
4392 * buffer pointer here and we shouldn't attempt to log the change as the
4393 * information is already held in the inode and discarded when the root
4394 * block is formatted into the on-disk inode fork. We still change it,
4395 * though, so everything is consistent in memory.
4396 */
38fa71a7 4397 if (!bp) {
9c6ebc42
DC
4398 ASSERT(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE);
4399 ASSERT(level == cur->bc_nlevels - 1);
38fa71a7
BF
4400 return 0;
4401 }
4402
4403 if (cur->bc_tp) {
4404 if (!xfs_trans_ordered_buf(cur->bc_tp, bp)) {
4405 xfs_btree_log_block(cur, bp, XFS_BB_OWNER);
4406 return -EAGAIN;
4407 }
4408 } else {
4409 xfs_buf_delwri_queue(bp, bbcoi->buffer_list);
9c6ebc42
DC
4410 }
4411
f31736bf 4412 return 0;
9c6ebc42
DC
4413}
4414
4415int
4416xfs_btree_change_owner(
4417 struct xfs_btree_cur *cur,
4a492e72 4418 uint64_t new_owner,
9c6ebc42
DC
4419 struct list_head *buffer_list)
4420{
f31736bf 4421 struct xfs_btree_block_change_owner_info bbcoi;
9c6ebc42 4422
f31736bf
DW
4423 bbcoi.new_owner = new_owner;
4424 bbcoi.buffer_list = buffer_list;
9c6ebc42 4425
f31736bf
DW
4426 return xfs_btree_visit_blocks(cur, xfs_btree_block_change_owner,
4427 &bbcoi);
9c6ebc42 4428}
dbca0167 4429
fb75464f 4430/* Verify the v5 fields of a long-format btree block. */
bc01119d 4431xfs_failaddr_t
fb75464f
DW
4432xfs_btree_lblock_v5hdr_verify(
4433 struct xfs_buf *bp,
4434 uint64_t owner)
4435{
4436 struct xfs_mount *mp = bp->b_target->bt_mount;
4437 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
4438
4439 if (!xfs_sb_version_hascrc(&mp->m_sb))
bc01119d 4440 return __this_address;
fb75464f 4441 if (!uuid_equal(&block->bb_u.l.bb_uuid, &mp->m_sb.sb_meta_uuid))
bc01119d 4442 return __this_address;
fb75464f 4443 if (block->bb_u.l.bb_blkno != cpu_to_be64(bp->b_bn))
bc01119d 4444 return __this_address;
fb75464f
DW
4445 if (owner != XFS_RMAP_OWN_UNKNOWN &&
4446 be64_to_cpu(block->bb_u.l.bb_owner) != owner)
bc01119d
DW
4447 return __this_address;
4448 return NULL;
fb75464f
DW
4449}
4450
4451/* Verify a long-format btree block. */
bc01119d 4452xfs_failaddr_t
fb75464f
DW
4453xfs_btree_lblock_verify(
4454 struct xfs_buf *bp,
4455 unsigned int max_recs)
4456{
4457 struct xfs_mount *mp = bp->b_target->bt_mount;
4458 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
4459
4460 /* numrecs verification */
4461 if (be16_to_cpu(block->bb_numrecs) > max_recs)
bc01119d 4462 return __this_address;
fb75464f
DW
4463
4464 /* sibling pointer verification */
4465 if (block->bb_u.l.bb_leftsib != cpu_to_be64(NULLFSBLOCK) &&
4466 !xfs_verify_fsbno(mp, be64_to_cpu(block->bb_u.l.bb_leftsib)))
bc01119d 4467 return __this_address;
fb75464f
DW
4468 if (block->bb_u.l.bb_rightsib != cpu_to_be64(NULLFSBLOCK) &&
4469 !xfs_verify_fsbno(mp, be64_to_cpu(block->bb_u.l.bb_rightsib)))
bc01119d 4470 return __this_address;
fb75464f 4471
bc01119d 4472 return NULL;
fb75464f
DW
4473}
4474
dbca0167
DW
4475/**
4476 * xfs_btree_sblock_v5hdr_verify() -- verify the v5 fields of a short-format
4477 * btree block
4478 *
4479 * @bp: buffer containing the btree block
4480 * @max_recs: pointer to the m_*_mxr max records field in the xfs mount
4481 * @pag_max_level: pointer to the per-ag max level field
4482 */
bc01119d 4483xfs_failaddr_t
dbca0167
DW
4484xfs_btree_sblock_v5hdr_verify(
4485 struct xfs_buf *bp)
4486{
4487 struct xfs_mount *mp = bp->b_target->bt_mount;
4488 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
4489 struct xfs_perag *pag = bp->b_pag;
4490
4491 if (!xfs_sb_version_hascrc(&mp->m_sb))
bc01119d 4492 return __this_address;
dbca0167 4493 if (!uuid_equal(&block->bb_u.s.bb_uuid, &mp->m_sb.sb_meta_uuid))
bc01119d 4494 return __this_address;
dbca0167 4495 if (block->bb_u.s.bb_blkno != cpu_to_be64(bp->b_bn))
bc01119d 4496 return __this_address;
dbca0167 4497 if (pag && be32_to_cpu(block->bb_u.s.bb_owner) != pag->pag_agno)
bc01119d
DW
4498 return __this_address;
4499 return NULL;
dbca0167
DW
4500}
4501
4502/**
4503 * xfs_btree_sblock_verify() -- verify a short-format btree block
4504 *
4505 * @bp: buffer containing the btree block
4506 * @max_recs: maximum records allowed in this btree node
4507 */
bc01119d 4508xfs_failaddr_t
dbca0167
DW
4509xfs_btree_sblock_verify(
4510 struct xfs_buf *bp,
4511 unsigned int max_recs)
4512{
4513 struct xfs_mount *mp = bp->b_target->bt_mount;
4514 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
bb2a125a 4515 xfs_agblock_t agno;
dbca0167
DW
4516
4517 /* numrecs verification */
4518 if (be16_to_cpu(block->bb_numrecs) > max_recs)
bc01119d 4519 return __this_address;
dbca0167
DW
4520
4521 /* sibling pointer verification */
bb2a125a
DW
4522 agno = xfs_daddr_to_agno(mp, XFS_BUF_ADDR(bp));
4523 if (block->bb_u.s.bb_leftsib != cpu_to_be32(NULLAGBLOCK) &&
4524 !xfs_verify_agbno(mp, agno, be32_to_cpu(block->bb_u.s.bb_leftsib)))
bc01119d 4525 return __this_address;
bb2a125a
DW
4526 if (block->bb_u.s.bb_rightsib != cpu_to_be32(NULLAGBLOCK) &&
4527 !xfs_verify_agbno(mp, agno, be32_to_cpu(block->bb_u.s.bb_rightsib)))
bc01119d 4528 return __this_address;
dbca0167 4529
bc01119d 4530 return NULL;
dbca0167 4531}
730e2a19
DW
4532
4533/*
4534 * Calculate the number of btree levels needed to store a given number of
4535 * records in a short-format btree.
4536 */
4537uint
4538xfs_btree_compute_maxlevels(
730e2a19
DW
4539 uint *limits,
4540 unsigned long len)
4541{
4542 uint level;
4543 unsigned long maxblocks;
4544
4545 maxblocks = (len + limits[0] - 1) / limits[0];
4546 for (level = 1; maxblocks > 1; level++)
4547 maxblocks = (maxblocks + limits[1] - 1) / limits[1];
4548 return level;
4549}
5d3b7fe1
DW
4550
4551/*
4552 * Query a regular btree for all records overlapping a given interval.
4553 * Start with a LE lookup of the key of low_rec and return all records
4554 * until we find a record with a key greater than the key of high_rec.
4555 */
4556STATIC int
4557xfs_btree_simple_query_range(
4558 struct xfs_btree_cur *cur,
4559 union xfs_btree_key *low_key,
4560 union xfs_btree_key *high_key,
4561 xfs_btree_query_range_fn fn,
4562 void *priv)
4563{
4564 union xfs_btree_rec *recp;
4565 union xfs_btree_key rec_key;
4a492e72 4566 int64_t diff;
5d3b7fe1
DW
4567 int stat;
4568 bool firstrec = true;
4569 int error;
4570
4571 ASSERT(cur->bc_ops->init_high_key_from_rec);
4572 ASSERT(cur->bc_ops->diff_two_keys);
4573
4574 /*
4575 * Find the leftmost record. The btree cursor must be set
4576 * to the low record used to generate low_key.
4577 */
4578 stat = 0;
4579 error = xfs_btree_lookup(cur, XFS_LOOKUP_LE, &stat);
4580 if (error)
4581 goto out;
4582
a3654981
DW
4583 /* Nothing? See if there's anything to the right. */
4584 if (!stat) {
4585 error = xfs_btree_increment(cur, 0, &stat);
4586 if (error)
4587 goto out;
4588 }
4589
5d3b7fe1
DW
4590 while (stat) {
4591 /* Find the record. */
4592 error = xfs_btree_get_rec(cur, &recp, &stat);
4593 if (error || !stat)
4594 break;
5d3b7fe1
DW
4595
4596 /* Skip if high_key(rec) < low_key. */
4597 if (firstrec) {
94a22dec 4598 cur->bc_ops->init_high_key_from_rec(&rec_key, recp);
5d3b7fe1
DW
4599 firstrec = false;
4600 diff = cur->bc_ops->diff_two_keys(cur, low_key,
4601 &rec_key);
4602 if (diff > 0)
4603 goto advloop;
4604 }
4605
4606 /* Stop if high_key < low_key(rec). */
94a22dec 4607 cur->bc_ops->init_key_from_rec(&rec_key, recp);
5d3b7fe1
DW
4608 diff = cur->bc_ops->diff_two_keys(cur, &rec_key, high_key);
4609 if (diff > 0)
4610 break;
4611
4612 /* Callback */
4613 error = fn(cur, recp, priv);
4614 if (error < 0 || error == XFS_BTREE_QUERY_RANGE_ABORT)
4615 break;
4616
4617advloop:
4618 /* Move on to the next record. */
4619 error = xfs_btree_increment(cur, 0, &stat);
4620 if (error)
4621 break;
4622 }
4623
4624out:
4625 return error;
4626}
4627
4628/*
4629 * Query an overlapped interval btree for all records overlapping a given
4630 * interval. This function roughly follows the algorithm given in
4631 * "Interval Trees" of _Introduction to Algorithms_, which is section
4632 * 14.3 in the 2nd and 3rd editions.
4633 *
4634 * First, generate keys for the low and high records passed in.
4635 *
4636 * For any leaf node, generate the high and low keys for the record.
4637 * If the record keys overlap with the query low/high keys, pass the
4638 * record to the function iterator.
4639 *
4640 * For any internal node, compare the low and high keys of each
4641 * pointer against the query low/high keys. If there's an overlap,
4642 * follow the pointer.
4643 *
4644 * As an optimization, we stop scanning a block when we find a low key
4645 * that is greater than the query's high key.
4646 */
4647STATIC int
4648xfs_btree_overlapped_query_range(
4649 struct xfs_btree_cur *cur,
4650 union xfs_btree_key *low_key,
4651 union xfs_btree_key *high_key,
4652 xfs_btree_query_range_fn fn,
4653 void *priv)
4654{
4655 union xfs_btree_ptr ptr;
4656 union xfs_btree_ptr *pp;
4657 union xfs_btree_key rec_key;
4658 union xfs_btree_key rec_hkey;
4659 union xfs_btree_key *lkp;
4660 union xfs_btree_key *hkp;
4661 union xfs_btree_rec *recp;
4662 struct xfs_btree_block *block;
4a492e72
DW
4663 int64_t ldiff;
4664 int64_t hdiff;
5d3b7fe1
DW
4665 int level;
4666 struct xfs_buf *bp;
4667 int i;
4668 int error;
4669
4670 /* Load the root of the btree. */
4671 level = cur->bc_nlevels - 1;
4672 cur->bc_ops->init_ptr_from_cur(cur, &ptr);
4673 error = xfs_btree_lookup_get_block(cur, level, &ptr, &block);
4674 if (error)
4675 return error;
4676 xfs_btree_get_block(cur, level, &bp);
4677 trace_xfs_btree_overlapped_query_range(cur, level, bp);
4678#ifdef DEBUG
4679 error = xfs_btree_check_block(cur, block, level, bp);
4680 if (error)
4681 goto out;
4682#endif
4683 cur->bc_ptrs[level] = 1;
4684
4685 while (level < cur->bc_nlevels) {
4686 block = xfs_btree_get_block(cur, level, &bp);
4687
4688 /* End of node, pop back towards the root. */
4689 if (cur->bc_ptrs[level] > be16_to_cpu(block->bb_numrecs)) {
4690pop_up:
4691 if (level < cur->bc_nlevels - 1)
4692 cur->bc_ptrs[level + 1]++;
4693 level++;
4694 continue;
4695 }
4696
4697 if (level == 0) {
4698 /* Handle a leaf node. */
4699 recp = xfs_btree_rec_addr(cur, cur->bc_ptrs[0], block);
4700
4701 cur->bc_ops->init_high_key_from_rec(&rec_hkey, recp);
4702 ldiff = cur->bc_ops->diff_two_keys(cur, &rec_hkey,
4703 low_key);
4704
4705 cur->bc_ops->init_key_from_rec(&rec_key, recp);
4706 hdiff = cur->bc_ops->diff_two_keys(cur, high_key,
4707 &rec_key);
4708
4709 /*
4710 * If (record's high key >= query's low key) and
4711 * (query's high key >= record's low key), then
4712 * this record overlaps the query range; callback.
4713 */
4714 if (ldiff >= 0 && hdiff >= 0) {
4715 error = fn(cur, recp, priv);
4716 if (error < 0 ||
4717 error == XFS_BTREE_QUERY_RANGE_ABORT)
4718 break;
4719 } else if (hdiff < 0) {
4720 /* Record is larger than high key; pop. */
4721 goto pop_up;
4722 }
4723 cur->bc_ptrs[level]++;
4724 continue;
4725 }
4726
4727 /* Handle an internal node. */
4728 lkp = xfs_btree_key_addr(cur, cur->bc_ptrs[level], block);
4729 hkp = xfs_btree_high_key_addr(cur, cur->bc_ptrs[level], block);
4730 pp = xfs_btree_ptr_addr(cur, cur->bc_ptrs[level], block);
4731
4732 ldiff = cur->bc_ops->diff_two_keys(cur, hkp, low_key);
4733 hdiff = cur->bc_ops->diff_two_keys(cur, high_key, lkp);
4734
4735 /*
4736 * If (pointer's high key >= query's low key) and
4737 * (query's high key >= pointer's low key), then
4738 * this record overlaps the query range; follow pointer.
4739 */
4740 if (ldiff >= 0 && hdiff >= 0) {
4741 level--;
4742 error = xfs_btree_lookup_get_block(cur, level, pp,
4743 &block);
4744 if (error)
4745 goto out;
4746 xfs_btree_get_block(cur, level, &bp);
4747 trace_xfs_btree_overlapped_query_range(cur, level, bp);
4748#ifdef DEBUG
4749 error = xfs_btree_check_block(cur, block, level, bp);
4750 if (error)
4751 goto out;
4752#endif
4753 cur->bc_ptrs[level] = 1;
4754 continue;
4755 } else if (hdiff < 0) {
4756 /* The low key is larger than the upper range; pop. */
4757 goto pop_up;
4758 }
4759 cur->bc_ptrs[level]++;
4760 }
4761
4762out:
4763 /*
4764 * If we don't end this function with the cursor pointing at a record
4765 * block, a subsequent non-error cursor deletion will not release
4766 * node-level buffers, causing a buffer leak. This is quite possible
4767 * with a zero-results range query, so release the buffers if we
4768 * failed to return any results.
4769 */
4770 if (cur->bc_bufs[0] == NULL) {
4771 for (i = 0; i < cur->bc_nlevels; i++) {
4772 if (cur->bc_bufs[i]) {
4773 xfs_trans_brelse(cur->bc_tp, cur->bc_bufs[i]);
4774 cur->bc_bufs[i] = NULL;
4775 cur->bc_ptrs[i] = 0;
4776 cur->bc_ra[i] = 0;
4777 }
4778 }
4779 }
4780
4781 return error;
4782}
4783
4784/*
4785 * Query a btree for all records overlapping a given interval of keys. The
4786 * supplied function will be called with each record found; return one of the
4787 * XFS_BTREE_QUERY_RANGE_{CONTINUE,ABORT} values or the usual negative error
4788 * code. This function returns XFS_BTREE_QUERY_RANGE_ABORT, zero, or a
4789 * negative error code.
4790 */
4791int
4792xfs_btree_query_range(
4793 struct xfs_btree_cur *cur,
4794 union xfs_btree_irec *low_rec,
4795 union xfs_btree_irec *high_rec,
4796 xfs_btree_query_range_fn fn,
4797 void *priv)
4798{
4799 union xfs_btree_rec rec;
4800 union xfs_btree_key low_key;
4801 union xfs_btree_key high_key;
4802
4803 /* Find the keys of both ends of the interval. */
4804 cur->bc_rec = *high_rec;
4805 cur->bc_ops->init_rec_from_cur(cur, &rec);
4806 cur->bc_ops->init_key_from_rec(&high_key, &rec);
4807
4808 cur->bc_rec = *low_rec;
4809 cur->bc_ops->init_rec_from_cur(cur, &rec);
4810 cur->bc_ops->init_key_from_rec(&low_key, &rec);
4811
4812 /* Enforce low key < high key. */
4813 if (cur->bc_ops->diff_two_keys(cur, &low_key, &high_key) > 0)
4814 return -EINVAL;
4815
4816 if (!(cur->bc_flags & XFS_BTREE_OVERLAPPING))
4817 return xfs_btree_simple_query_range(cur, &low_key,
4818 &high_key, fn, priv);
4819 return xfs_btree_overlapped_query_range(cur, &low_key, &high_key,
4820 fn, priv);
4821}
fc8c849d 4822
7e05e856
DW
4823/* Query a btree for all records. */
4824int
4825xfs_btree_query_all(
4826 struct xfs_btree_cur *cur,
4827 xfs_btree_query_range_fn fn,
4828 void *priv)
4829{
f296858d
DW
4830 union xfs_btree_key low_key;
4831 union xfs_btree_key high_key;
4832
4833 memset(&cur->bc_rec, 0, sizeof(cur->bc_rec));
4834 memset(&low_key, 0, sizeof(low_key));
4835 memset(&high_key, 0xFF, sizeof(high_key));
7e05e856 4836
f296858d 4837 return xfs_btree_simple_query_range(cur, &low_key, &high_key, fn, priv);
7e05e856
DW
4838}
4839
fc8c849d
DW
4840/*
4841 * Calculate the number of blocks needed to store a given number of records
4842 * in a short-format (per-AG metadata) btree.
4843 */
5f728383 4844unsigned long long
fc8c849d 4845xfs_btree_calc_size(
fc8c849d
DW
4846 uint *limits,
4847 unsigned long long len)
4848{
4849 int level;
4850 int maxrecs;
5f728383 4851 unsigned long long rval;
fc8c849d
DW
4852
4853 maxrecs = limits[0];
4854 for (level = 0, rval = 0; len > 1; level++) {
4855 len += maxrecs - 1;
4856 do_div(len, maxrecs);
4857 maxrecs = limits[1];
4858 rval += len;
4859 }
4860 return rval;
4861}
2cccc8ce 4862
1e85c20d 4863static int
2cccc8ce
DW
4864xfs_btree_count_blocks_helper(
4865 struct xfs_btree_cur *cur,
4866 int level,
4867 void *data)
4868{
4869 xfs_extlen_t *blocks = data;
4870 (*blocks)++;
4871
4872 return 0;
4873}
4874
4875/* Count the blocks in a btree and return the result in *blocks. */
4876int
4877xfs_btree_count_blocks(
4878 struct xfs_btree_cur *cur,
4879 xfs_extlen_t *blocks)
4880{
4881 *blocks = 0;
4882 return xfs_btree_visit_blocks(cur, xfs_btree_count_blocks_helper,
4883 blocks);
4884}
15c4e6e4
DW
4885
4886/* Compare two btree pointers. */
4887int64_t
4888xfs_btree_diff_two_ptrs(
4889 struct xfs_btree_cur *cur,
4890 const union xfs_btree_ptr *a,
4891 const union xfs_btree_ptr *b)
4892{
4893 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
4894 return (int64_t)be64_to_cpu(a->l) - be64_to_cpu(b->l);
4895 return (int64_t)be32_to_cpu(a->s) - be32_to_cpu(b->s);
4896}
1fe41a73
DW
4897
4898/* If there's an extent, we're done. */
4899STATIC int
4900xfs_btree_has_record_helper(
4901 struct xfs_btree_cur *cur,
4902 union xfs_btree_rec *rec,
4903 void *priv)
4904{
4905 return XFS_BTREE_QUERY_RANGE_ABORT;
4906}
4907
4908/* Is there a record covering a given range of keys? */
4909int
4910xfs_btree_has_record(
4911 struct xfs_btree_cur *cur,
4912 union xfs_btree_irec *low,
4913 union xfs_btree_irec *high,
4914 bool *exists)
4915{
4916 int error;
4917
4918 error = xfs_btree_query_range(cur, low, high,
4919 &xfs_btree_has_record_helper, NULL);
4920 if (error == XFS_BTREE_QUERY_RANGE_ABORT) {
4921 *exists = true;
4922 return 0;
4923 }
4924 *exists = false;
4925 return error;
4926}
43e73fb0
DW
4927
4928/* Are there more records in this btree? */
4929bool
4930xfs_btree_has_more_records(
4931 struct xfs_btree_cur *cur)
4932{
4933 struct xfs_btree_block *block;
4934 struct xfs_buf *bp;
4935
4936 block = xfs_btree_get_block(cur, 0, &bp);
4937
4938 /* There are still records in this block. */
4939 if (cur->bc_ptrs[0] < xfs_btree_get_numrecs(block))
4940 return true;
4941
4942 /* There are more record blocks. */
4943 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
4944 return block->bb_u.l.bb_rightsib != cpu_to_be64(NULLFSBLOCK);
4945 else
4946 return block->bb_u.s.bb_rightsib != cpu_to_be32(NULLAGBLOCK);
4947}