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