]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blame - libxfs/xfs_btree.c
Sync up libxfs to latest kernel code
[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
NS
17 */
18
2bd0ea18
NS
19#include <xfs.h>
20
21/*
22 * Cursor allocation zone.
23 */
5e656dbb 24kmem_zone_t *xfs_btree_cur_zone;
2bd0ea18
NS
25
26/*
27 * Btree magic numbers.
28 */
5e656dbb 29const __uint32_t xfs_magics[XFS_BTNUM_MAX] = {
2bd0ea18
NS
30 XFS_ABTB_MAGIC, XFS_ABTC_MAGIC, XFS_BMAP_MAGIC, XFS_IBT_MAGIC
31};
32
2bd0ea18 33
b3563c19 34STATIC int /* error (0 or EFSCORRUPTED) */
2bd0ea18 35xfs_btree_check_lblock(
b194c7d8 36 struct xfs_btree_cur *cur, /* btree cursor */
b3563c19 37 struct xfs_btree_block *block, /* btree long form block pointer */
2bd0ea18 38 int level, /* level of the btree block */
b194c7d8 39 struct xfs_buf *bp) /* buffer for block, if any */
2bd0ea18
NS
40{
41 int lblock_ok; /* block passes checks */
b194c7d8 42 struct xfs_mount *mp; /* file system mount point */
2bd0ea18
NS
43
44 mp = cur->bc_mp;
45 lblock_ok =
6e3140c7
NS
46 be32_to_cpu(block->bb_magic) == xfs_magics[cur->bc_btnum] &&
47 be16_to_cpu(block->bb_level) == level &&
48 be16_to_cpu(block->bb_numrecs) <=
b194c7d8 49 cur->bc_ops->get_maxrecs(cur, level) &&
b3563c19
BN
50 block->bb_u.l.bb_leftsib &&
51 (be64_to_cpu(block->bb_u.l.bb_leftsib) == NULLDFSBNO ||
52 XFS_FSB_SANITY_CHECK(mp,
53 be64_to_cpu(block->bb_u.l.bb_leftsib))) &&
54 block->bb_u.l.bb_rightsib &&
55 (be64_to_cpu(block->bb_u.l.bb_rightsib) == NULLDFSBNO ||
56 XFS_FSB_SANITY_CHECK(mp,
57 be64_to_cpu(block->bb_u.l.bb_rightsib)));
b194c7d8
BN
58 if (unlikely(XFS_TEST_ERROR(!lblock_ok, mp,
59 XFS_ERRTAG_BTREE_CHECK_LBLOCK,
4ca431fc 60 XFS_RANDOM_BTREE_CHECK_LBLOCK))) {
2bd0ea18
NS
61 if (bp)
62 xfs_buftrace("LBTREE ERROR", bp);
4ca431fc
NS
63 XFS_ERROR_REPORT("xfs_btree_check_lblock", XFS_ERRLEVEL_LOW,
64 mp);
2bd0ea18
NS
65 return XFS_ERROR(EFSCORRUPTED);
66 }
67 return 0;
68}
69
b194c7d8 70STATIC int /* error (0 or EFSCORRUPTED) */
2bd0ea18 71xfs_btree_check_sblock(
b194c7d8 72 struct xfs_btree_cur *cur, /* btree cursor */
b3563c19 73 struct xfs_btree_block *block, /* btree short form block pointer */
2bd0ea18 74 int level, /* level of the btree block */
b194c7d8 75 struct xfs_buf *bp) /* buffer containing block */
2bd0ea18 76{
b194c7d8
BN
77 struct xfs_buf *agbp; /* buffer for ag. freespace struct */
78 struct xfs_agf *agf; /* ag. freespace structure */
dfc130f3 79 xfs_agblock_t agflen; /* native ag. freespace length */
2bd0ea18
NS
80 int sblock_ok; /* block passes checks */
81
82 agbp = cur->bc_private.a.agbp;
83 agf = XFS_BUF_TO_AGF(agbp);
6e3140c7 84 agflen = be32_to_cpu(agf->agf_length);
2bd0ea18 85 sblock_ok =
6e3140c7
NS
86 be32_to_cpu(block->bb_magic) == xfs_magics[cur->bc_btnum] &&
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
BN
90 (be32_to_cpu(block->bb_u.s.bb_leftsib) == NULLAGBLOCK ||
91 be32_to_cpu(block->bb_u.s.bb_leftsib) < agflen) &&
92 block->bb_u.s.bb_leftsib &&
93 (be32_to_cpu(block->bb_u.s.bb_rightsib) == NULLAGBLOCK ||
94 be32_to_cpu(block->bb_u.s.bb_rightsib) < agflen) &&
95 block->bb_u.s.bb_rightsib;
4ca431fc 96 if (unlikely(XFS_TEST_ERROR(!sblock_ok, cur->bc_mp,
2bd0ea18 97 XFS_ERRTAG_BTREE_CHECK_SBLOCK,
4ca431fc 98 XFS_RANDOM_BTREE_CHECK_SBLOCK))) {
2bd0ea18
NS
99 if (bp)
100 xfs_buftrace("SBTREE ERROR", bp);
4ca431fc
NS
101 XFS_ERROR_REPORT("xfs_btree_check_sblock", XFS_ERRLEVEL_LOW,
102 cur->bc_mp);
2bd0ea18
NS
103 return XFS_ERROR(EFSCORRUPTED);
104 }
105 return 0;
106}
107
108/*
b194c7d8
BN
109 * Debug routine: check that block header is ok.
110 */
111int
112xfs_btree_check_block(
113 struct xfs_btree_cur *cur, /* btree cursor */
114 struct xfs_btree_block *block, /* generic btree block pointer */
115 int level, /* level of the btree block */
116 struct xfs_buf *bp) /* buffer containing block, if any */
117{
b3563c19
BN
118 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
119 return xfs_btree_check_lblock(cur, block, level, bp);
120 else
121 return xfs_btree_check_sblock(cur, block, level, bp);
b194c7d8
BN
122}
123
124/*
125 * Check that (long) pointer is ok.
2bd0ea18
NS
126 */
127int /* error (0 or EFSCORRUPTED) */
b194c7d8
BN
128xfs_btree_check_lptr(
129 struct xfs_btree_cur *cur, /* btree cursor */
130 xfs_dfsbno_t bno, /* btree block disk address */
131 int level) /* btree block level */
132{
133 XFS_WANT_CORRUPTED_RETURN(
134 level > 0 &&
135 bno != NULLDFSBNO &&
136 XFS_FSB_SANITY_CHECK(cur->bc_mp, bno));
137 return 0;
138}
139
870d4cbc 140#ifdef DEBUG
b194c7d8
BN
141/*
142 * Check that (short) pointer is ok.
143 */
144STATIC int /* error (0 or EFSCORRUPTED) */
2bd0ea18 145xfs_btree_check_sptr(
b194c7d8
BN
146 struct xfs_btree_cur *cur, /* btree cursor */
147 xfs_agblock_t bno, /* btree block disk address */
148 int level) /* btree block level */
2bd0ea18 149{
b194c7d8 150 xfs_agblock_t agblocks = cur->bc_mp->m_sb.sb_agblocks;
2bd0ea18 151
2bd0ea18
NS
152 XFS_WANT_CORRUPTED_RETURN(
153 level > 0 &&
b194c7d8
BN
154 bno != NULLAGBLOCK &&
155 bno != 0 &&
156 bno < agblocks);
2bd0ea18
NS
157 return 0;
158}
159
b194c7d8
BN
160/*
161 * Check that block ptr is ok.
162 */
163STATIC int /* error (0 or EFSCORRUPTED) */
164xfs_btree_check_ptr(
165 struct xfs_btree_cur *cur, /* btree cursor */
166 union xfs_btree_ptr *ptr, /* btree block disk address */
167 int index, /* offset from ptr to check */
168 int level) /* btree block level */
169{
170 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
171 return xfs_btree_check_lptr(cur,
172 be64_to_cpu((&ptr->l)[index]), level);
173 } else {
174 return xfs_btree_check_sptr(cur,
175 be32_to_cpu((&ptr->s)[index]), level);
176 }
177}
870d4cbc 178#endif
b194c7d8 179
2bd0ea18
NS
180/*
181 * Delete the btree cursor.
182 */
183void
184xfs_btree_del_cursor(
dfc130f3 185 xfs_btree_cur_t *cur, /* btree cursor */
2bd0ea18
NS
186 int error) /* del because of error */
187{
188 int i; /* btree level */
189
190 /*
191 * Clear the buffer pointers, and release the buffers.
192 * If we're doing this in the face of an error, we
193 * need to make sure to inspect all of the entries
194 * in the bc_bufs array for buffers to be unlocked.
195 * This is because some of the btree code works from
196 * level n down to 0, and if we get an error along
197 * the way we won't have initialized all the entries
198 * down to 0.
199 */
200 for (i = 0; i < cur->bc_nlevels; i++) {
201 if (cur->bc_bufs[i])
202 xfs_btree_setbuf(cur, i, NULL);
203 else if (!error)
204 break;
205 }
206 /*
5000d01d 207 * Can't free a bmap cursor without having dealt with the
2bd0ea18
NS
208 * allocated indirect blocks' accounting.
209 */
210 ASSERT(cur->bc_btnum != XFS_BTNUM_BMAP ||
211 cur->bc_private.b.allocated == 0);
212 /*
213 * Free the cursor.
214 */
215 kmem_zone_free(xfs_btree_cur_zone, cur);
216}
217
218/*
219 * Duplicate the btree cursor.
220 * Allocate a new one, copy the record, re-get the buffers.
221 */
222int /* error */
223xfs_btree_dup_cursor(
dfc130f3
RC
224 xfs_btree_cur_t *cur, /* input cursor */
225 xfs_btree_cur_t **ncur) /* output cursor */
2bd0ea18
NS
226{
227 xfs_buf_t *bp; /* btree block's buffer pointer */
5000d01d 228 int error; /* error return value */
2bd0ea18
NS
229 int i; /* level number of btree block */
230 xfs_mount_t *mp; /* mount structure for filesystem */
dfc130f3 231 xfs_btree_cur_t *new; /* new cursor value */
2bd0ea18
NS
232 xfs_trans_t *tp; /* transaction pointer, can be NULL */
233
234 tp = cur->bc_tp;
235 mp = cur->bc_mp;
b194c7d8 236
2bd0ea18
NS
237 /*
238 * Allocate a new cursor like the old one.
239 */
b194c7d8
BN
240 new = cur->bc_ops->dup_cursor(cur);
241
2bd0ea18
NS
242 /*
243 * Copy the record currently in the cursor.
244 */
245 new->bc_rec = cur->bc_rec;
b194c7d8 246
2bd0ea18
NS
247 /*
248 * For each level current, re-get the buffer and copy the ptr value.
249 */
250 for (i = 0; i < new->bc_nlevels; i++) {
251 new->bc_ptrs[i] = cur->bc_ptrs[i];
252 new->bc_ra[i] = cur->bc_ra[i];
0e266570
NS
253 if ((bp = cur->bc_bufs[i])) {
254 if ((error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp,
255 XFS_BUF_ADDR(bp), mp->m_bsize, 0, &bp))) {
2bd0ea18
NS
256 xfs_btree_del_cursor(new, error);
257 *ncur = NULL;
258 return error;
259 }
260 new->bc_bufs[i] = bp;
261 ASSERT(bp);
262 ASSERT(!XFS_BUF_GETERROR(bp));
263 } else
264 new->bc_bufs[i] = NULL;
265 }
2bd0ea18
NS
266 *ncur = new;
267 return 0;
268}
269
b194c7d8
BN
270/*
271 * XFS btree block layout and addressing:
272 *
273 * There are two types of blocks in the btree: leaf and non-leaf blocks.
274 *
275 * The leaf record start with a header then followed by records containing
276 * the values. A non-leaf block also starts with the same header, and
277 * then first contains lookup keys followed by an equal number of pointers
278 * to the btree blocks at the previous level.
279 *
280 * +--------+-------+-------+-------+-------+-------+-------+
281 * Leaf: | header | rec 1 | rec 2 | rec 3 | rec 4 | rec 5 | rec N |
282 * +--------+-------+-------+-------+-------+-------+-------+
283 *
284 * +--------+-------+-------+-------+-------+-------+-------+
285 * Non-Leaf: | header | key 1 | key 2 | key N | ptr 1 | ptr 2 | ptr N |
286 * +--------+-------+-------+-------+-------+-------+-------+
287 *
288 * The header is called struct xfs_btree_block for reasons better left unknown
289 * and comes in different versions for short (32bit) and long (64bit) block
290 * pointers. The record and key structures are defined by the btree instances
291 * and opaque to the btree core. The block pointers are simple disk endian
292 * integers, available in a short (32bit) and long (64bit) variant.
293 *
294 * The helpers below calculate the offset of a given record, key or pointer
295 * into a btree block (xfs_btree_*_offset) or return a pointer to the given
296 * record, key or pointer (xfs_btree_*_addr). Note that all addressing
297 * inside the btree block is done using indices starting at one, not zero!
298 */
299
300/*
301 * Return size of the btree block header for this btree instance.
302 */
303static inline size_t xfs_btree_block_len(struct xfs_btree_cur *cur)
304{
305 return (cur->bc_flags & XFS_BTREE_LONG_PTRS) ?
b3563c19
BN
306 XFS_BTREE_LBLOCK_LEN :
307 XFS_BTREE_SBLOCK_LEN;
b194c7d8
BN
308}
309
310/*
311 * Return size of btree block pointers for this btree instance.
312 */
313static inline size_t xfs_btree_ptr_len(struct xfs_btree_cur *cur)
314{
315 return (cur->bc_flags & XFS_BTREE_LONG_PTRS) ?
316 sizeof(__be64) : sizeof(__be32);
317}
318
319/*
320 * Calculate offset of the n-th record in a btree block.
321 */
322STATIC size_t
323xfs_btree_rec_offset(
324 struct xfs_btree_cur *cur,
325 int n)
326{
327 return xfs_btree_block_len(cur) +
328 (n - 1) * cur->bc_ops->rec_len;
329}
330
331/*
332 * Calculate offset of the n-th key in a btree block.
333 */
334STATIC size_t
335xfs_btree_key_offset(
336 struct xfs_btree_cur *cur,
337 int n)
338{
339 return xfs_btree_block_len(cur) +
340 (n - 1) * cur->bc_ops->key_len;
341}
342
343/*
344 * Calculate offset of the n-th block pointer in a btree block.
345 */
346STATIC size_t
347xfs_btree_ptr_offset(
348 struct xfs_btree_cur *cur,
349 int n,
350 int level)
351{
352 return xfs_btree_block_len(cur) +
353 cur->bc_ops->get_maxrecs(cur, level) * cur->bc_ops->key_len +
354 (n - 1) * xfs_btree_ptr_len(cur);
355}
356
357/*
358 * Return a pointer to the n-th record in the btree block.
359 */
360STATIC union xfs_btree_rec *
361xfs_btree_rec_addr(
362 struct xfs_btree_cur *cur,
363 int n,
364 struct xfs_btree_block *block)
365{
366 return (union xfs_btree_rec *)
367 ((char *)block + xfs_btree_rec_offset(cur, n));
368}
369
370/*
371 * Return a pointer to the n-th key in the btree block.
372 */
373STATIC union xfs_btree_key *
374xfs_btree_key_addr(
375 struct xfs_btree_cur *cur,
376 int n,
377 struct xfs_btree_block *block)
378{
379 return (union xfs_btree_key *)
380 ((char *)block + xfs_btree_key_offset(cur, n));
381}
382
383/*
384 * Return a pointer to the n-th block pointer in the btree block.
385 */
386STATIC union xfs_btree_ptr *
387xfs_btree_ptr_addr(
388 struct xfs_btree_cur *cur,
389 int n,
390 struct xfs_btree_block *block)
391{
392 int level = xfs_btree_get_level(block);
393
394 ASSERT(block->bb_level != 0);
395
396 return (union xfs_btree_ptr *)
397 ((char *)block + xfs_btree_ptr_offset(cur, n, level));
398}
399
400/*
401 * Get a the root block which is stored in the inode.
402 *
403 * For now this btree implementation assumes the btree root is always
404 * stored in the if_broot field of an inode fork.
405 */
406STATIC struct xfs_btree_block *
407xfs_btree_get_iroot(
408 struct xfs_btree_cur *cur)
409{
410 struct xfs_ifork *ifp;
411
412 ifp = XFS_IFORK_PTR(cur->bc_private.b.ip, cur->bc_private.b.whichfork);
413 return (struct xfs_btree_block *)ifp->if_broot;
414}
415
5000d01d 416/*
2bd0ea18 417 * Retrieve the block pointer from the cursor at the given level.
b194c7d8 418 * This may be an inode btree root or from a buffer.
2bd0ea18 419 */
b194c7d8 420STATIC struct xfs_btree_block * /* generic btree block pointer */
2bd0ea18 421xfs_btree_get_block(
b194c7d8 422 struct xfs_btree_cur *cur, /* btree cursor */
2bd0ea18 423 int level, /* level in btree */
b194c7d8
BN
424 struct xfs_buf **bpp) /* buffer containing the block */
425{
426 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
427 (level == cur->bc_nlevels - 1)) {
428 *bpp = NULL;
429 return xfs_btree_get_iroot(cur);
2bd0ea18 430 }
b194c7d8
BN
431
432 *bpp = cur->bc_bufs[level];
433 return XFS_BUF_TO_BLOCK(*bpp);
2bd0ea18
NS
434}
435
436/*
437 * Get a buffer for the block, return it with no data read.
438 * Long-form addressing.
439 */
440xfs_buf_t * /* buffer for fsbno */
441xfs_btree_get_bufl(
442 xfs_mount_t *mp, /* file system mount point */
443 xfs_trans_t *tp, /* transaction pointer */
444 xfs_fsblock_t fsbno, /* file system block number */
445 uint lock) /* lock flags for get_buf */
446{
447 xfs_buf_t *bp; /* buffer pointer (return value) */
448 xfs_daddr_t d; /* real disk block address */
449
450 ASSERT(fsbno != NULLFSBLOCK);
451 d = XFS_FSB_TO_DADDR(mp, fsbno);
452 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d, mp->m_bsize, lock);
453 ASSERT(bp);
454 ASSERT(!XFS_BUF_GETERROR(bp));
455 return bp;
456}
457
458/*
459 * Get a buffer for the block, return it with no data read.
460 * Short-form addressing.
461 */
462xfs_buf_t * /* buffer for agno/agbno */
463xfs_btree_get_bufs(
464 xfs_mount_t *mp, /* file system mount point */
465 xfs_trans_t *tp, /* transaction pointer */
466 xfs_agnumber_t agno, /* allocation group number */
467 xfs_agblock_t agbno, /* allocation group block number */
468 uint lock) /* lock flags for get_buf */
469{
470 xfs_buf_t *bp; /* buffer pointer (return value) */
471 xfs_daddr_t d; /* real disk block address */
472
473 ASSERT(agno != NULLAGNUMBER);
474 ASSERT(agbno != NULLAGBLOCK);
475 d = XFS_AGB_TO_DADDR(mp, agno, agbno);
476 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d, mp->m_bsize, lock);
477 ASSERT(bp);
478 ASSERT(!XFS_BUF_GETERROR(bp));
479 return bp;
480}
481
2bd0ea18
NS
482/*
483 * Check for the cursor referring to the last block at the given level.
484 */
485int /* 1=is last block, 0=not last block */
486xfs_btree_islastblock(
487 xfs_btree_cur_t *cur, /* btree cursor */
488 int level) /* level to check */
489{
b3563c19 490 struct xfs_btree_block *block; /* generic btree block pointer */
2bd0ea18
NS
491 xfs_buf_t *bp; /* buffer containing block */
492
493 block = xfs_btree_get_block(cur, level, &bp);
494 xfs_btree_check_block(cur, block, level, bp);
b194c7d8 495 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
6e3140c7 496 return be64_to_cpu(block->bb_u.l.bb_rightsib) == NULLDFSBNO;
2bd0ea18 497 else
6e3140c7 498 return be32_to_cpu(block->bb_u.s.bb_rightsib) == NULLAGBLOCK;
2bd0ea18
NS
499}
500
5e656dbb
BN
501/*
502 * Change the cursor to point to the first record at the given level.
503 * Other levels are unaffected.
504 */
b194c7d8 505STATIC int /* success=1, failure=0 */
5e656dbb
BN
506xfs_btree_firstrec(
507 xfs_btree_cur_t *cur, /* btree cursor */
508 int level) /* level to change */
509{
b3563c19 510 struct xfs_btree_block *block; /* generic btree block pointer */
5e656dbb
BN
511 xfs_buf_t *bp; /* buffer containing block */
512
513 /*
514 * Get the block pointer for this level.
515 */
516 block = xfs_btree_get_block(cur, level, &bp);
517 xfs_btree_check_block(cur, block, level, bp);
518 /*
519 * It's empty, there is no such record.
520 */
b194c7d8 521 if (!block->bb_numrecs)
5e656dbb
BN
522 return 0;
523 /*
524 * Set the ptr value to 1, that's the first record/key.
525 */
526 cur->bc_ptrs[level] = 1;
527 return 1;
528}
529
2bd0ea18
NS
530/*
531 * Change the cursor to point to the last record in the current block
dfc130f3 532 * at the given level. Other levels are unaffected.
2bd0ea18 533 */
b194c7d8 534STATIC int /* success=1, failure=0 */
2bd0ea18
NS
535xfs_btree_lastrec(
536 xfs_btree_cur_t *cur, /* btree cursor */
537 int level) /* level to change */
538{
b3563c19 539 struct xfs_btree_block *block; /* generic btree block pointer */
2bd0ea18
NS
540 xfs_buf_t *bp; /* buffer containing block */
541
542 /*
543 * Get the block pointer for this level.
544 */
545 block = xfs_btree_get_block(cur, level, &bp);
546 xfs_btree_check_block(cur, block, level, bp);
547 /*
548 * It's empty, there is no such record.
549 */
b194c7d8 550 if (!block->bb_numrecs)
2bd0ea18
NS
551 return 0;
552 /*
553 * Set the ptr value to numrecs, that's the last record/key.
554 */
b194c7d8 555 cur->bc_ptrs[level] = be16_to_cpu(block->bb_numrecs);
2bd0ea18
NS
556 return 1;
557}
558
559/*
560 * Compute first and last byte offsets for the fields given.
561 * Interprets the offsets table, which contains struct field offsets.
562 */
563void
564xfs_btree_offsets(
565 __int64_t fields, /* bitmask of fields */
566 const short *offsets, /* table of field offsets */
567 int nbits, /* number of bits to inspect */
568 int *first, /* output: first byte offset */
569 int *last) /* output: last byte offset */
570{
571 int i; /* current bit number */
572 __int64_t imask; /* mask for current bit number */
573
574 ASSERT(fields != 0);
575 /*
576 * Find the lowest bit, so the first byte offset.
577 */
578 for (i = 0, imask = 1LL; ; i++, imask <<= 1) {
579 if (imask & fields) {
580 *first = offsets[i];
581 break;
582 }
583 }
584 /*
585 * Find the highest bit, so the last byte offset.
586 */
587 for (i = nbits - 1, imask = 1LL << i; ; i--, imask >>= 1) {
588 if (imask & fields) {
589 *last = offsets[i + 1] - 1;
590 break;
591 }
592 }
593}
594
595/*
596 * Get a buffer for the block, return it read in.
597 * Long-form addressing.
598 */
599int /* error */
600xfs_btree_read_bufl(
601 xfs_mount_t *mp, /* file system mount point */
602 xfs_trans_t *tp, /* transaction pointer */
603 xfs_fsblock_t fsbno, /* file system block number */
604 uint lock, /* lock flags for read_buf */
605 xfs_buf_t **bpp, /* buffer for fsbno */
606 int refval) /* ref count value for buffer */
607{
608 xfs_buf_t *bp; /* return value */
609 xfs_daddr_t d; /* real disk block address */
610 int error;
611
612 ASSERT(fsbno != NULLFSBLOCK);
613 d = XFS_FSB_TO_DADDR(mp, fsbno);
0e266570
NS
614 if ((error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, d,
615 mp->m_bsize, lock, &bp))) {
2bd0ea18
NS
616 return error;
617 }
618 ASSERT(!bp || !XFS_BUF_GETERROR(bp));
619 if (bp != NULL) {
620 XFS_BUF_SET_VTYPE_REF(bp, B_FS_MAP, refval);
621 }
622 *bpp = bp;
623 return 0;
624}
625
626/*
627 * Get a buffer for the block, return it read in.
628 * Short-form addressing.
629 */
630int /* error */
631xfs_btree_read_bufs(
632 xfs_mount_t *mp, /* file system mount point */
633 xfs_trans_t *tp, /* transaction pointer */
634 xfs_agnumber_t agno, /* allocation group number */
635 xfs_agblock_t agbno, /* allocation group block number */
636 uint lock, /* lock flags for read_buf */
637 xfs_buf_t **bpp, /* buffer for agno/agbno */
638 int refval) /* ref count value for buffer */
639{
640 xfs_buf_t *bp; /* return value */
0e266570 641 xfs_daddr_t d; /* real disk block address */
2bd0ea18
NS
642 int error;
643
644 ASSERT(agno != NULLAGNUMBER);
645 ASSERT(agbno != NULLAGBLOCK);
646 d = XFS_AGB_TO_DADDR(mp, agno, agbno);
0e266570
NS
647 if ((error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, d,
648 mp->m_bsize, lock, &bp))) {
2bd0ea18
NS
649 return error;
650 }
651 ASSERT(!bp || !XFS_BUF_GETERROR(bp));
6c5da395 652 if (bp != NULL) {
2bd0ea18
NS
653 switch (refval) {
654 case XFS_ALLOC_BTREE_REF:
655 XFS_BUF_SET_VTYPE_REF(bp, B_FS_MAP, refval);
656 break;
657 case XFS_INO_BTREE_REF:
658 XFS_BUF_SET_VTYPE_REF(bp, B_FS_INOMAP, refval);
659 break;
660 }
6c5da395 661 }
2bd0ea18
NS
662 *bpp = bp;
663 return 0;
664}
665
b194c7d8
BN
666STATIC int
667xfs_btree_readahead_lblock(
668 struct xfs_btree_cur *cur,
669 int lr,
670 struct xfs_btree_block *block)
2bd0ea18 671{
2bd0ea18 672 int rval = 0;
b194c7d8
BN
673 xfs_fsblock_t left = be64_to_cpu(block->bb_u.l.bb_leftsib);
674 xfs_fsblock_t right = be64_to_cpu(block->bb_u.l.bb_rightsib);
2bd0ea18 675
b194c7d8
BN
676 if ((lr & XFS_BTCUR_LEFTRA) && left != NULLDFSBNO) {
677 xfs_btree_reada_bufl(cur->bc_mp, left, 1);
678 rval++;
679 }
680
681 if ((lr & XFS_BTCUR_RIGHTRA) && right != NULLDFSBNO) {
682 xfs_btree_reada_bufl(cur->bc_mp, right, 1);
683 rval++;
2bd0ea18 684 }
b194c7d8 685
2bd0ea18
NS
686 return rval;
687}
688
b194c7d8
BN
689STATIC int
690xfs_btree_readahead_sblock(
691 struct xfs_btree_cur *cur,
692 int lr,
693 struct xfs_btree_block *block)
2bd0ea18 694{
b194c7d8
BN
695 int rval = 0;
696 xfs_agblock_t left = be32_to_cpu(block->bb_u.s.bb_leftsib);
697 xfs_agblock_t right = be32_to_cpu(block->bb_u.s.bb_rightsib);
2bd0ea18 698
b194c7d8
BN
699
700 if ((lr & XFS_BTCUR_LEFTRA) && left != NULLAGBLOCK) {
701 xfs_btree_reada_bufs(cur->bc_mp, cur->bc_private.a.agno,
702 left, 1);
703 rval++;
704 }
705
706 if ((lr & XFS_BTCUR_RIGHTRA) && right != NULLAGBLOCK) {
707 xfs_btree_reada_bufs(cur->bc_mp, cur->bc_private.a.agno,
708 right, 1);
709 rval++;
710 }
711
712 return rval;
713}
714
715/*
716 * Read-ahead btree blocks, at the given level.
717 * Bits in lr are set from XFS_BTCUR_{LEFT,RIGHT}RA.
718 */
719STATIC int
720xfs_btree_readahead(
721 struct xfs_btree_cur *cur, /* btree cursor */
722 int lev, /* level in btree */
723 int lr) /* left/right bits */
724{
725 struct xfs_btree_block *block;
726
727 /*
728 * No readahead needed if we are at the root level and the
729 * btree root is stored in the inode.
730 */
731 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
732 (lev == cur->bc_nlevels - 1))
733 return 0;
734
735 if ((cur->bc_ra[lev] | lr) == cur->bc_ra[lev])
736 return 0;
737
738 cur->bc_ra[lev] |= lr;
739 block = XFS_BUF_TO_BLOCK(cur->bc_bufs[lev]);
740
741 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
742 return xfs_btree_readahead_lblock(cur, lr, block);
743 return xfs_btree_readahead_sblock(cur, lr, block);
744}
745
746/*
747 * Set the buffer for level "lev" in the cursor to bp, releasing
748 * any previous buffer.
749 */
750void
751xfs_btree_setbuf(
752 xfs_btree_cur_t *cur, /* btree cursor */
753 int lev, /* level in btree */
754 xfs_buf_t *bp) /* new buffer to set */
755{
b3563c19 756 struct xfs_btree_block *b; /* btree block */
b194c7d8
BN
757 xfs_buf_t *obp; /* old buffer pointer */
758
759 obp = cur->bc_bufs[lev];
760 if (obp)
2bd0ea18
NS
761 xfs_trans_brelse(cur->bc_tp, obp);
762 cur->bc_bufs[lev] = bp;
763 cur->bc_ra[lev] = 0;
764 if (!bp)
765 return;
766 b = XFS_BUF_TO_BLOCK(bp);
b194c7d8 767 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
6e3140c7 768 if (be64_to_cpu(b->bb_u.l.bb_leftsib) == NULLDFSBNO)
2bd0ea18 769 cur->bc_ra[lev] |= XFS_BTCUR_LEFTRA;
6e3140c7 770 if (be64_to_cpu(b->bb_u.l.bb_rightsib) == NULLDFSBNO)
2bd0ea18
NS
771 cur->bc_ra[lev] |= XFS_BTCUR_RIGHTRA;
772 } else {
6e3140c7 773 if (be32_to_cpu(b->bb_u.s.bb_leftsib) == NULLAGBLOCK)
2bd0ea18 774 cur->bc_ra[lev] |= XFS_BTCUR_LEFTRA;
6e3140c7 775 if (be32_to_cpu(b->bb_u.s.bb_rightsib) == NULLAGBLOCK)
2bd0ea18
NS
776 cur->bc_ra[lev] |= XFS_BTCUR_RIGHTRA;
777 }
778}
b194c7d8
BN
779
780STATIC int
781xfs_btree_ptr_is_null(
782 struct xfs_btree_cur *cur,
783 union xfs_btree_ptr *ptr)
784{
785 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
786 return be64_to_cpu(ptr->l) == NULLFSBLOCK;
787 else
788 return be32_to_cpu(ptr->s) == NULLAGBLOCK;
789}
790
791STATIC void
792xfs_btree_set_ptr_null(
793 struct xfs_btree_cur *cur,
794 union xfs_btree_ptr *ptr)
795{
796 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
797 ptr->l = cpu_to_be64(NULLFSBLOCK);
798 else
799 ptr->s = cpu_to_be32(NULLAGBLOCK);
800}
801
802/*
803 * Get/set/init sibling pointers
804 */
805STATIC void
806xfs_btree_get_sibling(
807 struct xfs_btree_cur *cur,
808 struct xfs_btree_block *block,
809 union xfs_btree_ptr *ptr,
810 int lr)
811{
812 ASSERT(lr == XFS_BB_LEFTSIB || lr == XFS_BB_RIGHTSIB);
813
814 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
815 if (lr == XFS_BB_RIGHTSIB)
816 ptr->l = block->bb_u.l.bb_rightsib;
817 else
818 ptr->l = block->bb_u.l.bb_leftsib;
819 } else {
820 if (lr == XFS_BB_RIGHTSIB)
821 ptr->s = block->bb_u.s.bb_rightsib;
822 else
823 ptr->s = block->bb_u.s.bb_leftsib;
824 }
825}
826
827STATIC void
828xfs_btree_set_sibling(
829 struct xfs_btree_cur *cur,
830 struct xfs_btree_block *block,
831 union xfs_btree_ptr *ptr,
832 int lr)
833{
834 ASSERT(lr == XFS_BB_LEFTSIB || lr == XFS_BB_RIGHTSIB);
835
836 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
837 if (lr == XFS_BB_RIGHTSIB)
838 block->bb_u.l.bb_rightsib = ptr->l;
839 else
840 block->bb_u.l.bb_leftsib = ptr->l;
841 } else {
842 if (lr == XFS_BB_RIGHTSIB)
843 block->bb_u.s.bb_rightsib = ptr->s;
844 else
845 block->bb_u.s.bb_leftsib = ptr->s;
846 }
847}
848
849STATIC void
850xfs_btree_init_block(
851 struct xfs_btree_cur *cur,
852 int level,
853 int numrecs,
854 struct xfs_btree_block *new) /* new block */
855{
856 new->bb_magic = cpu_to_be32(xfs_magics[cur->bc_btnum]);
857 new->bb_level = cpu_to_be16(level);
858 new->bb_numrecs = cpu_to_be16(numrecs);
859
860 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
861 new->bb_u.l.bb_leftsib = cpu_to_be64(NULLFSBLOCK);
862 new->bb_u.l.bb_rightsib = cpu_to_be64(NULLFSBLOCK);
863 } else {
864 new->bb_u.s.bb_leftsib = cpu_to_be32(NULLAGBLOCK);
865 new->bb_u.s.bb_rightsib = cpu_to_be32(NULLAGBLOCK);
866 }
867}
868
869/*
870 * Return true if ptr is the last record in the btree and
871 * we need to track updateѕ to this record. The decision
872 * will be further refined in the update_lastrec method.
873 */
874STATIC int
875xfs_btree_is_lastrec(
876 struct xfs_btree_cur *cur,
877 struct xfs_btree_block *block,
878 int level)
879{
880 union xfs_btree_ptr ptr;
881
882 if (level > 0)
883 return 0;
884 if (!(cur->bc_flags & XFS_BTREE_LASTREC_UPDATE))
885 return 0;
886
887 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_RIGHTSIB);
888 if (!xfs_btree_ptr_is_null(cur, &ptr))
889 return 0;
890 return 1;
891}
892
893STATIC void
894xfs_btree_buf_to_ptr(
895 struct xfs_btree_cur *cur,
896 struct xfs_buf *bp,
897 union xfs_btree_ptr *ptr)
898{
899 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
900 ptr->l = cpu_to_be64(XFS_DADDR_TO_FSB(cur->bc_mp,
901 XFS_BUF_ADDR(bp)));
902 else {
903 ptr->s = cpu_to_be32(XFS_DADDR_TO_AGBNO(cur->bc_mp,
904 XFS_BUF_ADDR(bp)));
905 }
906}
907
908STATIC xfs_daddr_t
909xfs_btree_ptr_to_daddr(
910 struct xfs_btree_cur *cur,
911 union xfs_btree_ptr *ptr)
912{
913 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
914 ASSERT(be64_to_cpu(ptr->l) != NULLFSBLOCK);
915
916 return XFS_FSB_TO_DADDR(cur->bc_mp, be64_to_cpu(ptr->l));
917 } else {
918 ASSERT(cur->bc_private.a.agno != NULLAGNUMBER);
919 ASSERT(be32_to_cpu(ptr->s) != NULLAGBLOCK);
920
921 return XFS_AGB_TO_DADDR(cur->bc_mp, cur->bc_private.a.agno,
922 be32_to_cpu(ptr->s));
923 }
924}
925
926STATIC void
927xfs_btree_set_refs(
928 struct xfs_btree_cur *cur,
929 struct xfs_buf *bp)
930{
931 switch (cur->bc_btnum) {
932 case XFS_BTNUM_BNO:
933 case XFS_BTNUM_CNT:
934 XFS_BUF_SET_VTYPE_REF(*bpp, B_FS_MAP, XFS_ALLOC_BTREE_REF);
935 break;
936 case XFS_BTNUM_INO:
937 XFS_BUF_SET_VTYPE_REF(*bpp, B_FS_INOMAP, XFS_INO_BTREE_REF);
938 break;
939 case XFS_BTNUM_BMAP:
940 XFS_BUF_SET_VTYPE_REF(*bpp, B_FS_MAP, XFS_BMAP_BTREE_REF);
941 break;
942 default:
943 ASSERT(0);
944 }
945}
946
947STATIC int
948xfs_btree_get_buf_block(
949 struct xfs_btree_cur *cur,
950 union xfs_btree_ptr *ptr,
951 int flags,
952 struct xfs_btree_block **block,
953 struct xfs_buf **bpp)
954{
955 struct xfs_mount *mp = cur->bc_mp;
956 xfs_daddr_t d;
957
958 /* need to sort out how callers deal with failures first */
959 ASSERT(!(flags & XFS_BUF_TRYLOCK));
960
961 d = xfs_btree_ptr_to_daddr(cur, ptr);
962 *bpp = xfs_trans_get_buf(cur->bc_tp, mp->m_ddev_targp, d,
963 mp->m_bsize, flags);
964
965 ASSERT(*bpp);
966 ASSERT(!XFS_BUF_GETERROR(*bpp));
967
968 *block = XFS_BUF_TO_BLOCK(*bpp);
969 return 0;
970}
971
972/*
973 * Read in the buffer at the given ptr and return the buffer and
974 * the block pointer within the buffer.
975 */
976STATIC int
977xfs_btree_read_buf_block(
978 struct xfs_btree_cur *cur,
979 union xfs_btree_ptr *ptr,
980 int level,
981 int flags,
982 struct xfs_btree_block **block,
983 struct xfs_buf **bpp)
984{
985 struct xfs_mount *mp = cur->bc_mp;
986 xfs_daddr_t d;
987 int error;
988
989 /* need to sort out how callers deal with failures first */
990 ASSERT(!(flags & XFS_BUF_TRYLOCK));
991
992 d = xfs_btree_ptr_to_daddr(cur, ptr);
993 error = xfs_trans_read_buf(mp, cur->bc_tp, mp->m_ddev_targp, d,
994 mp->m_bsize, flags, bpp);
995 if (error)
996 return error;
997
998 ASSERT(*bpp != NULL);
999 ASSERT(!XFS_BUF_GETERROR(*bpp));
1000
1001 xfs_btree_set_refs(cur, *bpp);
1002 *block = XFS_BUF_TO_BLOCK(*bpp);
1003
1004 error = xfs_btree_check_block(cur, *block, level, *bpp);
1005 if (error)
1006 xfs_trans_brelse(cur->bc_tp, *bpp);
1007 return error;
1008}
1009
1010/*
1011 * Copy keys from one btree block to another.
1012 */
1013STATIC void
1014xfs_btree_copy_keys(
1015 struct xfs_btree_cur *cur,
1016 union xfs_btree_key *dst_key,
1017 union xfs_btree_key *src_key,
1018 int numkeys)
1019{
1020 ASSERT(numkeys >= 0);
1021 memcpy(dst_key, src_key, numkeys * cur->bc_ops->key_len);
1022}
1023
1024/*
1025 * Copy records from one btree block to another.
1026 */
1027STATIC void
1028xfs_btree_copy_recs(
1029 struct xfs_btree_cur *cur,
1030 union xfs_btree_rec *dst_rec,
1031 union xfs_btree_rec *src_rec,
1032 int numrecs)
1033{
1034 ASSERT(numrecs >= 0);
1035 memcpy(dst_rec, src_rec, numrecs * cur->bc_ops->rec_len);
1036}
1037
1038/*
1039 * Copy block pointers from one btree block to another.
1040 */
1041STATIC void
1042xfs_btree_copy_ptrs(
1043 struct xfs_btree_cur *cur,
1044 union xfs_btree_ptr *dst_ptr,
1045 union xfs_btree_ptr *src_ptr,
1046 int numptrs)
1047{
1048 ASSERT(numptrs >= 0);
1049 memcpy(dst_ptr, src_ptr, numptrs * xfs_btree_ptr_len(cur));
1050}
1051
1052/*
1053 * Shift keys one index left/right inside a single btree block.
1054 */
1055STATIC void
1056xfs_btree_shift_keys(
1057 struct xfs_btree_cur *cur,
1058 union xfs_btree_key *key,
1059 int dir,
1060 int numkeys)
1061{
1062 char *dst_key;
1063
1064 ASSERT(numkeys >= 0);
1065 ASSERT(dir == 1 || dir == -1);
1066
1067 dst_key = (char *)key + (dir * cur->bc_ops->key_len);
1068 memmove(dst_key, key, numkeys * cur->bc_ops->key_len);
1069}
1070
1071/*
1072 * Shift records one index left/right inside a single btree block.
1073 */
1074STATIC void
1075xfs_btree_shift_recs(
1076 struct xfs_btree_cur *cur,
1077 union xfs_btree_rec *rec,
1078 int dir,
1079 int numrecs)
1080{
1081 char *dst_rec;
1082
1083 ASSERT(numrecs >= 0);
1084 ASSERT(dir == 1 || dir == -1);
1085
1086 dst_rec = (char *)rec + (dir * cur->bc_ops->rec_len);
1087 memmove(dst_rec, rec, numrecs * cur->bc_ops->rec_len);
1088}
1089
1090/*
1091 * Shift block pointers one index left/right inside a single btree block.
1092 */
1093STATIC void
1094xfs_btree_shift_ptrs(
1095 struct xfs_btree_cur *cur,
1096 union xfs_btree_ptr *ptr,
1097 int dir,
1098 int numptrs)
1099{
1100 char *dst_ptr;
1101
1102 ASSERT(numptrs >= 0);
1103 ASSERT(dir == 1 || dir == -1);
1104
1105 dst_ptr = (char *)ptr + (dir * xfs_btree_ptr_len(cur));
1106 memmove(dst_ptr, ptr, numptrs * xfs_btree_ptr_len(cur));
1107}
1108
1109/*
1110 * Log key values from the btree block.
1111 */
1112STATIC void
1113xfs_btree_log_keys(
1114 struct xfs_btree_cur *cur,
1115 struct xfs_buf *bp,
1116 int first,
1117 int last)
1118{
1119 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1120 XFS_BTREE_TRACE_ARGBII(cur, bp, first, last);
1121
1122 if (bp) {
1123 xfs_trans_log_buf(cur->bc_tp, bp,
1124 xfs_btree_key_offset(cur, first),
1125 xfs_btree_key_offset(cur, last + 1) - 1);
1126 } else {
1127 xfs_trans_log_inode(cur->bc_tp, cur->bc_private.b.ip,
1128 xfs_ilog_fbroot(cur->bc_private.b.whichfork));
1129 }
1130
1131 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1132}
1133
1134/*
1135 * Log record values from the btree block.
1136 */
1137void
1138xfs_btree_log_recs(
1139 struct xfs_btree_cur *cur,
1140 struct xfs_buf *bp,
1141 int first,
1142 int last)
1143{
1144 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1145 XFS_BTREE_TRACE_ARGBII(cur, bp, first, last);
1146
1147 xfs_trans_log_buf(cur->bc_tp, bp,
1148 xfs_btree_rec_offset(cur, first),
1149 xfs_btree_rec_offset(cur, last + 1) - 1);
1150
1151 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1152}
1153
1154/*
1155 * Log block pointer fields from a btree block (nonleaf).
1156 */
1157STATIC void
1158xfs_btree_log_ptrs(
1159 struct xfs_btree_cur *cur, /* btree cursor */
1160 struct xfs_buf *bp, /* buffer containing btree block */
1161 int first, /* index of first pointer to log */
1162 int last) /* index of last pointer to log */
1163{
1164 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1165 XFS_BTREE_TRACE_ARGBII(cur, bp, first, last);
1166
1167 if (bp) {
1168 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
1169 int level = xfs_btree_get_level(block);
1170
1171 xfs_trans_log_buf(cur->bc_tp, bp,
1172 xfs_btree_ptr_offset(cur, first, level),
1173 xfs_btree_ptr_offset(cur, last + 1, level) - 1);
1174 } else {
1175 xfs_trans_log_inode(cur->bc_tp, cur->bc_private.b.ip,
1176 xfs_ilog_fbroot(cur->bc_private.b.whichfork));
1177 }
1178
1179 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1180}
1181
1182/*
1183 * Log fields from a btree block header.
1184 */
1185void
1186xfs_btree_log_block(
1187 struct xfs_btree_cur *cur, /* btree cursor */
1188 struct xfs_buf *bp, /* buffer containing btree block */
1189 int fields) /* mask of fields: XFS_BB_... */
1190{
1191 int first; /* first byte offset logged */
1192 int last; /* last byte offset logged */
1193 static const short soffsets[] = { /* table of offsets (short) */
b3563c19
BN
1194 offsetof(struct xfs_btree_block, bb_magic),
1195 offsetof(struct xfs_btree_block, bb_level),
1196 offsetof(struct xfs_btree_block, bb_numrecs),
1197 offsetof(struct xfs_btree_block, bb_u.s.bb_leftsib),
1198 offsetof(struct xfs_btree_block, bb_u.s.bb_rightsib),
1199 XFS_BTREE_SBLOCK_LEN
b194c7d8
BN
1200 };
1201 static const short loffsets[] = { /* table of offsets (long) */
b3563c19
BN
1202 offsetof(struct xfs_btree_block, bb_magic),
1203 offsetof(struct xfs_btree_block, bb_level),
1204 offsetof(struct xfs_btree_block, bb_numrecs),
1205 offsetof(struct xfs_btree_block, bb_u.l.bb_leftsib),
1206 offsetof(struct xfs_btree_block, bb_u.l.bb_rightsib),
1207 XFS_BTREE_LBLOCK_LEN
b194c7d8
BN
1208 };
1209
1210 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1211 XFS_BTREE_TRACE_ARGBI(cur, bp, fields);
1212
1213 if (bp) {
1214 xfs_btree_offsets(fields,
1215 (cur->bc_flags & XFS_BTREE_LONG_PTRS) ?
1216 loffsets : soffsets,
1217 XFS_BB_NUM_BITS, &first, &last);
1218 xfs_trans_log_buf(cur->bc_tp, bp, first, last);
1219 } else {
1220 xfs_trans_log_inode(cur->bc_tp, cur->bc_private.b.ip,
1221 xfs_ilog_fbroot(cur->bc_private.b.whichfork));
1222 }
1223
1224 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1225}
1226
1227/*
1228 * Increment cursor by one record at the level.
1229 * For nonzero levels the leaf-ward information is untouched.
1230 */
1231int /* error */
1232xfs_btree_increment(
1233 struct xfs_btree_cur *cur,
1234 int level,
1235 int *stat) /* success/failure */
1236{
1237 struct xfs_btree_block *block;
1238 union xfs_btree_ptr ptr;
1239 struct xfs_buf *bp;
1240 int error; /* error return value */
1241 int lev;
1242
1243 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1244 XFS_BTREE_TRACE_ARGI(cur, level);
1245
1246 ASSERT(level < cur->bc_nlevels);
1247
1248 /* Read-ahead to the right at this level. */
1249 xfs_btree_readahead(cur, level, XFS_BTCUR_RIGHTRA);
1250
1251 /* Get a pointer to the btree block. */
1252 block = xfs_btree_get_block(cur, level, &bp);
1253
1254#ifdef DEBUG
1255 error = xfs_btree_check_block(cur, block, level, bp);
1256 if (error)
1257 goto error0;
1258#endif
1259
1260 /* We're done if we remain in the block after the increment. */
1261 if (++cur->bc_ptrs[level] <= xfs_btree_get_numrecs(block))
1262 goto out1;
1263
1264 /* Fail if we just went off the right edge of the tree. */
1265 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_RIGHTSIB);
1266 if (xfs_btree_ptr_is_null(cur, &ptr))
1267 goto out0;
1268
1269 XFS_BTREE_STATS_INC(cur, increment);
1270
1271 /*
1272 * March up the tree incrementing pointers.
1273 * Stop when we don't go off the right edge of a block.
1274 */
1275 for (lev = level + 1; lev < cur->bc_nlevels; lev++) {
1276 block = xfs_btree_get_block(cur, lev, &bp);
1277
1278#ifdef DEBUG
1279 error = xfs_btree_check_block(cur, block, lev, bp);
1280 if (error)
1281 goto error0;
1282#endif
1283
1284 if (++cur->bc_ptrs[lev] <= xfs_btree_get_numrecs(block))
1285 break;
1286
1287 /* Read-ahead the right block for the next loop. */
1288 xfs_btree_readahead(cur, lev, XFS_BTCUR_RIGHTRA);
1289 }
1290
1291 /*
1292 * If we went off the root then we are either seriously
1293 * confused or have the tree root in an inode.
1294 */
1295 if (lev == cur->bc_nlevels) {
1296 if (cur->bc_flags & XFS_BTREE_ROOT_IN_INODE)
1297 goto out0;
1298 ASSERT(0);
1299 error = EFSCORRUPTED;
1300 goto error0;
1301 }
1302 ASSERT(lev < cur->bc_nlevels);
1303
1304 /*
1305 * Now walk back down the tree, fixing up the cursor's buffer
1306 * pointers and key numbers.
1307 */
1308 for (block = xfs_btree_get_block(cur, lev, &bp); lev > level; ) {
1309 union xfs_btree_ptr *ptrp;
1310
1311 ptrp = xfs_btree_ptr_addr(cur, cur->bc_ptrs[lev], block);
1312 error = xfs_btree_read_buf_block(cur, ptrp, --lev,
1313 0, &block, &bp);
1314 if (error)
1315 goto error0;
1316
1317 xfs_btree_setbuf(cur, lev, bp);
1318 cur->bc_ptrs[lev] = 1;
1319 }
1320out1:
1321 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1322 *stat = 1;
1323 return 0;
1324
1325out0:
1326 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1327 *stat = 0;
1328 return 0;
1329
1330error0:
1331 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
1332 return error;
1333}
1334
1335/*
1336 * Decrement cursor by one record at the level.
1337 * For nonzero levels the leaf-ward information is untouched.
1338 */
1339int /* error */
1340xfs_btree_decrement(
1341 struct xfs_btree_cur *cur,
1342 int level,
1343 int *stat) /* success/failure */
1344{
1345 struct xfs_btree_block *block;
1346 xfs_buf_t *bp;
1347 int error; /* error return value */
1348 int lev;
1349 union xfs_btree_ptr ptr;
1350
1351 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1352 XFS_BTREE_TRACE_ARGI(cur, level);
1353
1354 ASSERT(level < cur->bc_nlevels);
1355
1356 /* Read-ahead to the left at this level. */
1357 xfs_btree_readahead(cur, level, XFS_BTCUR_LEFTRA);
1358
1359 /* We're done if we remain in the block after the decrement. */
1360 if (--cur->bc_ptrs[level] > 0)
1361 goto out1;
1362
1363 /* Get a pointer to the btree block. */
1364 block = xfs_btree_get_block(cur, level, &bp);
1365
1366#ifdef DEBUG
1367 error = xfs_btree_check_block(cur, block, level, bp);
1368 if (error)
1369 goto error0;
1370#endif
1371
1372 /* Fail if we just went off the left edge of the tree. */
1373 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_LEFTSIB);
1374 if (xfs_btree_ptr_is_null(cur, &ptr))
1375 goto out0;
1376
1377 XFS_BTREE_STATS_INC(cur, decrement);
1378
1379 /*
1380 * March up the tree decrementing pointers.
1381 * Stop when we don't go off the left edge of a block.
1382 */
1383 for (lev = level + 1; lev < cur->bc_nlevels; lev++) {
1384 if (--cur->bc_ptrs[lev] > 0)
1385 break;
1386 /* Read-ahead the left block for the next loop. */
1387 xfs_btree_readahead(cur, lev, XFS_BTCUR_LEFTRA);
1388 }
1389
1390 /*
1391 * If we went off the root then we are seriously confused.
1392 * or the root of the tree is in an inode.
1393 */
1394 if (lev == cur->bc_nlevels) {
1395 if (cur->bc_flags & XFS_BTREE_ROOT_IN_INODE)
1396 goto out0;
1397 ASSERT(0);
1398 error = EFSCORRUPTED;
1399 goto error0;
1400 }
1401 ASSERT(lev < cur->bc_nlevels);
1402
1403 /*
1404 * Now walk back down the tree, fixing up the cursor's buffer
1405 * pointers and key numbers.
1406 */
1407 for (block = xfs_btree_get_block(cur, lev, &bp); lev > level; ) {
1408 union xfs_btree_ptr *ptrp;
1409
1410 ptrp = xfs_btree_ptr_addr(cur, cur->bc_ptrs[lev], block);
1411 error = xfs_btree_read_buf_block(cur, ptrp, --lev,
1412 0, &block, &bp);
1413 if (error)
1414 goto error0;
1415 xfs_btree_setbuf(cur, lev, bp);
1416 cur->bc_ptrs[lev] = xfs_btree_get_numrecs(block);
1417 }
1418out1:
1419 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1420 *stat = 1;
1421 return 0;
1422
1423out0:
1424 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1425 *stat = 0;
1426 return 0;
1427
1428error0:
1429 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
1430 return error;
1431}
1432
1433STATIC int
1434xfs_btree_lookup_get_block(
1435 struct xfs_btree_cur *cur, /* btree cursor */
1436 int level, /* level in the btree */
1437 union xfs_btree_ptr *pp, /* ptr to btree block */
1438 struct xfs_btree_block **blkp) /* return btree block */
1439{
1440 struct xfs_buf *bp; /* buffer pointer for btree block */
1441 int error = 0;
1442
1443 /* special case the root block if in an inode */
1444 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
1445 (level == cur->bc_nlevels - 1)) {
1446 *blkp = xfs_btree_get_iroot(cur);
1447 return 0;
1448 }
1449
1450 /*
1451 * If the old buffer at this level for the disk address we are
1452 * looking for re-use it.
1453 *
1454 * Otherwise throw it away and get a new one.
1455 */
1456 bp = cur->bc_bufs[level];
1457 if (bp && XFS_BUF_ADDR(bp) == xfs_btree_ptr_to_daddr(cur, pp)) {
1458 *blkp = XFS_BUF_TO_BLOCK(bp);
1459 return 0;
1460 }
1461
1462 error = xfs_btree_read_buf_block(cur, pp, level, 0, blkp, &bp);
1463 if (error)
1464 return error;
1465
1466 xfs_btree_setbuf(cur, level, bp);
1467 return 0;
1468}
1469
1470/*
1471 * Get current search key. For level 0 we don't actually have a key
1472 * structure so we make one up from the record. For all other levels
1473 * we just return the right key.
1474 */
1475STATIC union xfs_btree_key *
1476xfs_lookup_get_search_key(
1477 struct xfs_btree_cur *cur,
1478 int level,
1479 int keyno,
1480 struct xfs_btree_block *block,
1481 union xfs_btree_key *kp)
1482{
1483 if (level == 0) {
1484 cur->bc_ops->init_key_from_rec(kp,
1485 xfs_btree_rec_addr(cur, keyno, block));
1486 return kp;
1487 }
1488
1489 return xfs_btree_key_addr(cur, keyno, block);
1490}
1491
1492/*
1493 * Lookup the record. The cursor is made to point to it, based on dir.
1494 * Return 0 if can't find any such record, 1 for success.
1495 */
1496int /* error */
1497xfs_btree_lookup(
1498 struct xfs_btree_cur *cur, /* btree cursor */
1499 xfs_lookup_t dir, /* <=, ==, or >= */
1500 int *stat) /* success/failure */
1501{
1502 struct xfs_btree_block *block; /* current btree block */
1503 __int64_t diff; /* difference for the current key */
1504 int error; /* error return value */
1505 int keyno; /* current key number */
1506 int level; /* level in the btree */
1507 union xfs_btree_ptr *pp; /* ptr to btree block */
1508 union xfs_btree_ptr ptr; /* ptr to btree block */
1509
1510 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1511 XFS_BTREE_TRACE_ARGI(cur, dir);
1512
1513 XFS_BTREE_STATS_INC(cur, lookup);
1514
1515 block = NULL;
1516 keyno = 0;
1517
1518 /* initialise start pointer from cursor */
1519 cur->bc_ops->init_ptr_from_cur(cur, &ptr);
1520 pp = &ptr;
1521
1522 /*
1523 * Iterate over each level in the btree, starting at the root.
1524 * For each level above the leaves, find the key we need, based
1525 * on the lookup record, then follow the corresponding block
1526 * pointer down to the next level.
1527 */
1528 for (level = cur->bc_nlevels - 1, diff = 1; level >= 0; level--) {
1529 /* Get the block we need to do the lookup on. */
1530 error = xfs_btree_lookup_get_block(cur, level, pp, &block);
1531 if (error)
1532 goto error0;
1533
1534 if (diff == 0) {
1535 /*
1536 * If we already had a key match at a higher level, we
1537 * know we need to use the first entry in this block.
1538 */
1539 keyno = 1;
1540 } else {
1541 /* Otherwise search this block. Do a binary search. */
1542
1543 int high; /* high entry number */
1544 int low; /* low entry number */
1545
1546 /* Set low and high entry numbers, 1-based. */
1547 low = 1;
1548 high = xfs_btree_get_numrecs(block);
1549 if (!high) {
1550 /* Block is empty, must be an empty leaf. */
1551 ASSERT(level == 0 && cur->bc_nlevels == 1);
1552
1553 cur->bc_ptrs[0] = dir != XFS_LOOKUP_LE;
1554 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1555 *stat = 0;
1556 return 0;
1557 }
1558
1559 /* Binary search the block. */
1560 while (low <= high) {
1561 union xfs_btree_key key;
1562 union xfs_btree_key *kp;
1563
1564 XFS_BTREE_STATS_INC(cur, compare);
1565
1566 /* keyno is average of low and high. */
1567 keyno = (low + high) >> 1;
1568
1569 /* Get current search key */
1570 kp = xfs_lookup_get_search_key(cur, level,
1571 keyno, block, &key);
1572
1573 /*
1574 * Compute difference to get next direction:
1575 * - less than, move right
1576 * - greater than, move left
1577 * - equal, we're done
1578 */
1579 diff = cur->bc_ops->key_diff(cur, kp);
1580 if (diff < 0)
1581 low = keyno + 1;
1582 else if (diff > 0)
1583 high = keyno - 1;
1584 else
1585 break;
1586 }
1587 }
1588
1589 /*
1590 * If there are more levels, set up for the next level
1591 * by getting the block number and filling in the cursor.
1592 */
1593 if (level > 0) {
1594 /*
1595 * If we moved left, need the previous key number,
1596 * unless there isn't one.
1597 */
1598 if (diff > 0 && --keyno < 1)
1599 keyno = 1;
1600 pp = xfs_btree_ptr_addr(cur, keyno, block);
1601
1602#ifdef DEBUG
1603 error = xfs_btree_check_ptr(cur, pp, 0, level);
1604 if (error)
1605 goto error0;
1606#endif
1607 cur->bc_ptrs[level] = keyno;
1608 }
1609 }
1610
1611 /* Done with the search. See if we need to adjust the results. */
1612 if (dir != XFS_LOOKUP_LE && diff < 0) {
1613 keyno++;
1614 /*
1615 * If ge search and we went off the end of the block, but it's
1616 * not the last block, we're in the wrong block.
1617 */
1618 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_RIGHTSIB);
1619 if (dir == XFS_LOOKUP_GE &&
1620 keyno > xfs_btree_get_numrecs(block) &&
1621 !xfs_btree_ptr_is_null(cur, &ptr)) {
1622 int i;
1623
1624 cur->bc_ptrs[0] = keyno;
1625 error = xfs_btree_increment(cur, 0, &i);
1626 if (error)
1627 goto error0;
1628 XFS_WANT_CORRUPTED_RETURN(i == 1);
1629 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1630 *stat = 1;
1631 return 0;
1632 }
1633 } else if (dir == XFS_LOOKUP_LE && diff > 0)
1634 keyno--;
1635 cur->bc_ptrs[0] = keyno;
1636
1637 /* Return if we succeeded or not. */
1638 if (keyno == 0 || keyno > xfs_btree_get_numrecs(block))
1639 *stat = 0;
1640 else if (dir != XFS_LOOKUP_EQ || diff == 0)
1641 *stat = 1;
1642 else
1643 *stat = 0;
1644 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1645 return 0;
1646
1647error0:
1648 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
1649 return error;
1650}
1651
1652/*
1653 * Update keys at all levels from here to the root along the cursor's path.
1654 */
1655STATIC int
1656xfs_btree_updkey(
1657 struct xfs_btree_cur *cur,
1658 union xfs_btree_key *keyp,
1659 int level)
1660{
1661 struct xfs_btree_block *block;
1662 struct xfs_buf *bp;
1663 union xfs_btree_key *kp;
1664 int ptr;
1665
1666 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1667 XFS_BTREE_TRACE_ARGIK(cur, level, keyp);
1668
1669 ASSERT(!(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) || level >= 1);
1670
1671 /*
1672 * Go up the tree from this level toward the root.
1673 * At each level, update the key value to the value input.
1674 * Stop when we reach a level where the cursor isn't pointing
1675 * at the first entry in the block.
1676 */
1677 for (ptr = 1; ptr == 1 && level < cur->bc_nlevels; level++) {
1678#ifdef DEBUG
1679 int error;
1680#endif
1681 block = xfs_btree_get_block(cur, level, &bp);
1682#ifdef DEBUG
1683 error = xfs_btree_check_block(cur, block, level, bp);
1684 if (error) {
1685 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
1686 return error;
1687 }
1688#endif
1689 ptr = cur->bc_ptrs[level];
1690 kp = xfs_btree_key_addr(cur, ptr, block);
1691 xfs_btree_copy_keys(cur, kp, keyp, 1);
1692 xfs_btree_log_keys(cur, bp, ptr, ptr);
1693 }
1694
1695 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1696 return 0;
1697}
1698
1699/*
1700 * Update the record referred to by cur to the value in the
1701 * given record. This either works (return 0) or gets an
1702 * EFSCORRUPTED error.
1703 */
1704int
1705xfs_btree_update(
1706 struct xfs_btree_cur *cur,
1707 union xfs_btree_rec *rec)
1708{
1709 struct xfs_btree_block *block;
1710 struct xfs_buf *bp;
1711 int error;
1712 int ptr;
1713 union xfs_btree_rec *rp;
1714
1715 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1716 XFS_BTREE_TRACE_ARGR(cur, rec);
1717
1718 /* Pick up the current block. */
1719 block = xfs_btree_get_block(cur, 0, &bp);
1720
1721#ifdef DEBUG
1722 error = xfs_btree_check_block(cur, block, 0, bp);
1723 if (error)
1724 goto error0;
1725#endif
1726 /* Get the address of the rec to be updated. */
1727 ptr = cur->bc_ptrs[0];
1728 rp = xfs_btree_rec_addr(cur, ptr, block);
1729
1730 /* Fill in the new contents and log them. */
1731 xfs_btree_copy_recs(cur, rp, rec, 1);
1732 xfs_btree_log_recs(cur, bp, ptr, ptr);
1733
1734 /*
1735 * If we are tracking the last record in the tree and
1736 * we are at the far right edge of the tree, update it.
1737 */
1738 if (xfs_btree_is_lastrec(cur, block, 0)) {
1739 cur->bc_ops->update_lastrec(cur, block, rec,
1740 ptr, LASTREC_UPDATE);
1741 }
1742
1743 /* Updating first rec in leaf. Pass new key value up to our parent. */
1744 if (ptr == 1) {
1745 union xfs_btree_key key;
1746
1747 cur->bc_ops->init_key_from_rec(&key, rec);
1748 error = xfs_btree_updkey(cur, &key, 1);
1749 if (error)
1750 goto error0;
1751 }
1752
1753 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1754 return 0;
1755
1756error0:
1757 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
1758 return error;
1759}
1760
1761/*
1762 * Move 1 record left from cur/level if possible.
1763 * Update cur to reflect the new path.
1764 */
1765STATIC int /* error */
1766xfs_btree_lshift(
1767 struct xfs_btree_cur *cur,
1768 int level,
1769 int *stat) /* success/failure */
1770{
1771 union xfs_btree_key key; /* btree key */
1772 struct xfs_buf *lbp; /* left buffer pointer */
1773 struct xfs_btree_block *left; /* left btree block */
1774 int lrecs; /* left record count */
1775 struct xfs_buf *rbp; /* right buffer pointer */
1776 struct xfs_btree_block *right; /* right btree block */
1777 int rrecs; /* right record count */
1778 union xfs_btree_ptr lptr; /* left btree pointer */
1779 union xfs_btree_key *rkp = NULL; /* right btree key */
1780 union xfs_btree_ptr *rpp = NULL; /* right address pointer */
1781 union xfs_btree_rec *rrp = NULL; /* right record pointer */
1782 int error; /* error return value */
1783
1784 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1785 XFS_BTREE_TRACE_ARGI(cur, level);
1786
1787 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
1788 level == cur->bc_nlevels - 1)
1789 goto out0;
1790
1791 /* Set up variables for this block as "right". */
1792 right = xfs_btree_get_block(cur, level, &rbp);
1793
1794#ifdef DEBUG
1795 error = xfs_btree_check_block(cur, right, level, rbp);
1796 if (error)
1797 goto error0;
1798#endif
1799
1800 /* If we've got no left sibling then we can't shift an entry left. */
1801 xfs_btree_get_sibling(cur, right, &lptr, XFS_BB_LEFTSIB);
1802 if (xfs_btree_ptr_is_null(cur, &lptr))
1803 goto out0;
1804
1805 /*
1806 * If the cursor entry is the one that would be moved, don't
1807 * do it... it's too complicated.
1808 */
1809 if (cur->bc_ptrs[level] <= 1)
1810 goto out0;
1811
1812 /* Set up the left neighbor as "left". */
1813 error = xfs_btree_read_buf_block(cur, &lptr, level, 0, &left, &lbp);
1814 if (error)
1815 goto error0;
1816
1817 /* If it's full, it can't take another entry. */
1818 lrecs = xfs_btree_get_numrecs(left);
1819 if (lrecs == cur->bc_ops->get_maxrecs(cur, level))
1820 goto out0;
1821
1822 rrecs = xfs_btree_get_numrecs(right);
1823
1824 /*
1825 * We add one entry to the left side and remove one for the right side.
1826 * Accout for it here, the changes will be updated on disk and logged
1827 * later.
1828 */
1829 lrecs++;
1830 rrecs--;
1831
1832 XFS_BTREE_STATS_INC(cur, lshift);
1833 XFS_BTREE_STATS_ADD(cur, moves, 1);
1834
1835 /*
1836 * If non-leaf, copy a key and a ptr to the left block.
1837 * Log the changes to the left block.
1838 */
1839 if (level > 0) {
1840 /* It's a non-leaf. Move keys and pointers. */
1841 union xfs_btree_key *lkp; /* left btree key */
1842 union xfs_btree_ptr *lpp; /* left address pointer */
1843
1844 lkp = xfs_btree_key_addr(cur, lrecs, left);
1845 rkp = xfs_btree_key_addr(cur, 1, right);
1846
1847 lpp = xfs_btree_ptr_addr(cur, lrecs, left);
1848 rpp = xfs_btree_ptr_addr(cur, 1, right);
1849#ifdef DEBUG
1850 error = xfs_btree_check_ptr(cur, rpp, 0, level);
1851 if (error)
1852 goto error0;
1853#endif
1854 xfs_btree_copy_keys(cur, lkp, rkp, 1);
1855 xfs_btree_copy_ptrs(cur, lpp, rpp, 1);
1856
1857 xfs_btree_log_keys(cur, lbp, lrecs, lrecs);
1858 xfs_btree_log_ptrs(cur, lbp, lrecs, lrecs);
1859
1860 ASSERT(cur->bc_ops->keys_inorder(cur,
1861 xfs_btree_key_addr(cur, lrecs - 1, left), lkp));
1862 } else {
1863 /* It's a leaf. Move records. */
1864 union xfs_btree_rec *lrp; /* left record pointer */
1865
1866 lrp = xfs_btree_rec_addr(cur, lrecs, left);
1867 rrp = xfs_btree_rec_addr(cur, 1, right);
1868
1869 xfs_btree_copy_recs(cur, lrp, rrp, 1);
1870 xfs_btree_log_recs(cur, lbp, lrecs, lrecs);
1871
1872 ASSERT(cur->bc_ops->recs_inorder(cur,
1873 xfs_btree_rec_addr(cur, lrecs - 1, left), lrp));
1874 }
1875
1876 xfs_btree_set_numrecs(left, lrecs);
1877 xfs_btree_log_block(cur, lbp, XFS_BB_NUMRECS);
1878
1879 xfs_btree_set_numrecs(right, rrecs);
1880 xfs_btree_log_block(cur, rbp, XFS_BB_NUMRECS);
1881
1882 /*
1883 * Slide the contents of right down one entry.
1884 */
1885 XFS_BTREE_STATS_ADD(cur, moves, rrecs - 1);
1886 if (level > 0) {
1887 /* It's a nonleaf. operate on keys and ptrs */
1888#ifdef DEBUG
1889 int i; /* loop index */
1890
1891 for (i = 0; i < rrecs; i++) {
1892 error = xfs_btree_check_ptr(cur, rpp, i + 1, level);
1893 if (error)
1894 goto error0;
1895 }
1896#endif
1897 xfs_btree_shift_keys(cur,
1898 xfs_btree_key_addr(cur, 2, right),
1899 -1, rrecs);
1900 xfs_btree_shift_ptrs(cur,
1901 xfs_btree_ptr_addr(cur, 2, right),
1902 -1, rrecs);
1903
1904 xfs_btree_log_keys(cur, rbp, 1, rrecs);
1905 xfs_btree_log_ptrs(cur, rbp, 1, rrecs);
1906 } else {
1907 /* It's a leaf. operate on records */
1908 xfs_btree_shift_recs(cur,
1909 xfs_btree_rec_addr(cur, 2, right),
1910 -1, rrecs);
1911 xfs_btree_log_recs(cur, rbp, 1, rrecs);
1912
1913 /*
1914 * If it's the first record in the block, we'll need a key
1915 * structure to pass up to the next level (updkey).
1916 */
1917 cur->bc_ops->init_key_from_rec(&key,
1918 xfs_btree_rec_addr(cur, 1, right));
1919 rkp = &key;
1920 }
1921
1922 /* Update the parent key values of right. */
1923 error = xfs_btree_updkey(cur, rkp, level + 1);
1924 if (error)
1925 goto error0;
1926
1927 /* Slide the cursor value left one. */
1928 cur->bc_ptrs[level]--;
1929
1930 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1931 *stat = 1;
1932 return 0;
1933
1934out0:
1935 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1936 *stat = 0;
1937 return 0;
1938
1939error0:
1940 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
1941 return error;
1942}
1943
1944/*
1945 * Move 1 record right from cur/level if possible.
1946 * Update cur to reflect the new path.
1947 */
1948STATIC int /* error */
1949xfs_btree_rshift(
1950 struct xfs_btree_cur *cur,
1951 int level,
1952 int *stat) /* success/failure */
1953{
1954 union xfs_btree_key key; /* btree key */
1955 struct xfs_buf *lbp; /* left buffer pointer */
1956 struct xfs_btree_block *left; /* left btree block */
1957 struct xfs_buf *rbp; /* right buffer pointer */
1958 struct xfs_btree_block *right; /* right btree block */
1959 struct xfs_btree_cur *tcur; /* temporary btree cursor */
1960 union xfs_btree_ptr rptr; /* right block pointer */
1961 union xfs_btree_key *rkp; /* right btree key */
1962 int rrecs; /* right record count */
1963 int lrecs; /* left record count */
1964 int error; /* error return value */
1965 int i; /* loop counter */
1966
1967 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1968 XFS_BTREE_TRACE_ARGI(cur, level);
1969
1970 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
1971 (level == cur->bc_nlevels - 1))
1972 goto out0;
1973
1974 /* Set up variables for this block as "left". */
1975 left = xfs_btree_get_block(cur, level, &lbp);
1976
1977#ifdef DEBUG
1978 error = xfs_btree_check_block(cur, left, level, lbp);
1979 if (error)
1980 goto error0;
1981#endif
1982
1983 /* If we've got no right sibling then we can't shift an entry right. */
1984 xfs_btree_get_sibling(cur, left, &rptr, XFS_BB_RIGHTSIB);
1985 if (xfs_btree_ptr_is_null(cur, &rptr))
1986 goto out0;
1987
1988 /*
1989 * If the cursor entry is the one that would be moved, don't
1990 * do it... it's too complicated.
1991 */
1992 lrecs = xfs_btree_get_numrecs(left);
1993 if (cur->bc_ptrs[level] >= lrecs)
1994 goto out0;
1995
1996 /* Set up the right neighbor as "right". */
1997 error = xfs_btree_read_buf_block(cur, &rptr, level, 0, &right, &rbp);
1998 if (error)
1999 goto error0;
2000
2001 /* If it's full, it can't take another entry. */
2002 rrecs = xfs_btree_get_numrecs(right);
2003 if (rrecs == cur->bc_ops->get_maxrecs(cur, level))
2004 goto out0;
2005
2006 XFS_BTREE_STATS_INC(cur, rshift);
2007 XFS_BTREE_STATS_ADD(cur, moves, rrecs);
2008
2009 /*
2010 * Make a hole at the start of the right neighbor block, then
2011 * copy the last left block entry to the hole.
2012 */
2013 if (level > 0) {
2014 /* It's a nonleaf. make a hole in the keys and ptrs */
2015 union xfs_btree_key *lkp;
2016 union xfs_btree_ptr *lpp;
2017 union xfs_btree_ptr *rpp;
2018
2019 lkp = xfs_btree_key_addr(cur, lrecs, left);
2020 lpp = xfs_btree_ptr_addr(cur, lrecs, left);
2021 rkp = xfs_btree_key_addr(cur, 1, right);
2022 rpp = xfs_btree_ptr_addr(cur, 1, right);
2023
2024#ifdef DEBUG
2025 for (i = rrecs - 1; i >= 0; i--) {
2026 error = xfs_btree_check_ptr(cur, rpp, i, level);
2027 if (error)
2028 goto error0;
2029 }
2030#endif
2031
2032 xfs_btree_shift_keys(cur, rkp, 1, rrecs);
2033 xfs_btree_shift_ptrs(cur, rpp, 1, rrecs);
2034
2035#ifdef DEBUG
2036 error = xfs_btree_check_ptr(cur, lpp, 0, level);
2037 if (error)
2038 goto error0;
2039#endif
2040
2041 /* Now put the new data in, and log it. */
2042 xfs_btree_copy_keys(cur, rkp, lkp, 1);
2043 xfs_btree_copy_ptrs(cur, rpp, lpp, 1);
2044
2045 xfs_btree_log_keys(cur, rbp, 1, rrecs + 1);
2046 xfs_btree_log_ptrs(cur, rbp, 1, rrecs + 1);
2047
2048 ASSERT(cur->bc_ops->keys_inorder(cur, rkp,
2049 xfs_btree_key_addr(cur, 2, right)));
2050 } else {
2051 /* It's a leaf. make a hole in the records */
2052 union xfs_btree_rec *lrp;
2053 union xfs_btree_rec *rrp;
2054
2055 lrp = xfs_btree_rec_addr(cur, lrecs, left);
2056 rrp = xfs_btree_rec_addr(cur, 1, right);
2057
2058 xfs_btree_shift_recs(cur, rrp, 1, rrecs);
2059
2060 /* Now put the new data in, and log it. */
2061 xfs_btree_copy_recs(cur, rrp, lrp, 1);
2062 xfs_btree_log_recs(cur, rbp, 1, rrecs + 1);
2063
2064 cur->bc_ops->init_key_from_rec(&key, rrp);
2065 rkp = &key;
2066
2067 ASSERT(cur->bc_ops->recs_inorder(cur, rrp,
2068 xfs_btree_rec_addr(cur, 2, right)));
2069 }
2070
2071 /*
2072 * Decrement and log left's numrecs, bump and log right's numrecs.
2073 */
2074 xfs_btree_set_numrecs(left, --lrecs);
2075 xfs_btree_log_block(cur, lbp, XFS_BB_NUMRECS);
2076
2077 xfs_btree_set_numrecs(right, ++rrecs);
2078 xfs_btree_log_block(cur, rbp, XFS_BB_NUMRECS);
2079
2080 /*
2081 * Using a temporary cursor, update the parent key values of the
2082 * block on the right.
2083 */
2084 error = xfs_btree_dup_cursor(cur, &tcur);
2085 if (error)
2086 goto error0;
2087 i = xfs_btree_lastrec(tcur, level);
2088 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
2089
2090 error = xfs_btree_increment(tcur, level, &i);
2091 if (error)
2092 goto error1;
2093
2094 error = xfs_btree_updkey(tcur, rkp, level + 1);
2095 if (error)
2096 goto error1;
2097
2098 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
2099
2100 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2101 *stat = 1;
2102 return 0;
2103
2104out0:
2105 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2106 *stat = 0;
2107 return 0;
2108
2109error0:
2110 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
2111 return error;
2112
2113error1:
2114 XFS_BTREE_TRACE_CURSOR(tcur, XBT_ERROR);
2115 xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
2116 return error;
2117}
2118
2119/*
2120 * Split cur/level block in half.
2121 * Return new block number and the key to its first
2122 * record (to be inserted into parent).
2123 */
2124STATIC int /* error */
2125xfs_btree_split(
2126 struct xfs_btree_cur *cur,
2127 int level,
2128 union xfs_btree_ptr *ptrp,
2129 union xfs_btree_key *key,
2130 struct xfs_btree_cur **curp,
2131 int *stat) /* success/failure */
2132{
2133 union xfs_btree_ptr lptr; /* left sibling block ptr */
2134 struct xfs_buf *lbp; /* left buffer pointer */
2135 struct xfs_btree_block *left; /* left btree block */
2136 union xfs_btree_ptr rptr; /* right sibling block ptr */
2137 struct xfs_buf *rbp; /* right buffer pointer */
2138 struct xfs_btree_block *right; /* right btree block */
2139 union xfs_btree_ptr rrptr; /* right-right sibling ptr */
2140 struct xfs_buf *rrbp; /* right-right buffer pointer */
2141 struct xfs_btree_block *rrblock; /* right-right btree block */
2142 int lrecs;
2143 int rrecs;
2144 int src_index;
2145 int error; /* error return value */
2146#ifdef DEBUG
2147 int i;
2148#endif
2149
2150 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
2151 XFS_BTREE_TRACE_ARGIPK(cur, level, *ptrp, key);
2152
2153 XFS_BTREE_STATS_INC(cur, split);
2154
2155 /* Set up left block (current one). */
2156 left = xfs_btree_get_block(cur, level, &lbp);
2157
2158#ifdef DEBUG
2159 error = xfs_btree_check_block(cur, left, level, lbp);
2160 if (error)
2161 goto error0;
2162#endif
2163
2164 xfs_btree_buf_to_ptr(cur, lbp, &lptr);
2165
2166 /* Allocate the new block. If we can't do it, we're toast. Give up. */
2167 error = cur->bc_ops->alloc_block(cur, &lptr, &rptr, 1, stat);
2168 if (error)
2169 goto error0;
2170 if (*stat == 0)
2171 goto out0;
2172 XFS_BTREE_STATS_INC(cur, alloc);
2173
2174 /* Set up the new block as "right". */
2175 error = xfs_btree_get_buf_block(cur, &rptr, 0, &right, &rbp);
2176 if (error)
2177 goto error0;
2178
2179 /* Fill in the btree header for the new right block. */
2180 xfs_btree_init_block(cur, xfs_btree_get_level(left), 0, right);
2181
2182 /*
2183 * Split the entries between the old and the new block evenly.
2184 * Make sure that if there's an odd number of entries now, that
2185 * each new block will have the same number of entries.
2186 */
2187 lrecs = xfs_btree_get_numrecs(left);
2188 rrecs = lrecs / 2;
2189 if ((lrecs & 1) && cur->bc_ptrs[level] <= rrecs + 1)
2190 rrecs++;
2191 src_index = (lrecs - rrecs + 1);
2192
2193 XFS_BTREE_STATS_ADD(cur, moves, rrecs);
2194
2195 /*
2196 * Copy btree block entries from the left block over to the
2197 * new block, the right. Update the right block and log the
2198 * changes.
2199 */
2200 if (level > 0) {
2201 /* It's a non-leaf. Move keys and pointers. */
2202 union xfs_btree_key *lkp; /* left btree key */
2203 union xfs_btree_ptr *lpp; /* left address pointer */
2204 union xfs_btree_key *rkp; /* right btree key */
2205 union xfs_btree_ptr *rpp; /* right address pointer */
2206
2207 lkp = xfs_btree_key_addr(cur, src_index, left);
2208 lpp = xfs_btree_ptr_addr(cur, src_index, left);
2209 rkp = xfs_btree_key_addr(cur, 1, right);
2210 rpp = xfs_btree_ptr_addr(cur, 1, right);
2211
2212#ifdef DEBUG
2213 for (i = src_index; i < rrecs; i++) {
2214 error = xfs_btree_check_ptr(cur, lpp, i, level);
2215 if (error)
2216 goto error0;
2217 }
2218#endif
2219
2220 xfs_btree_copy_keys(cur, rkp, lkp, rrecs);
2221 xfs_btree_copy_ptrs(cur, rpp, lpp, rrecs);
2222
2223 xfs_btree_log_keys(cur, rbp, 1, rrecs);
2224 xfs_btree_log_ptrs(cur, rbp, 1, rrecs);
2225
2226 /* Grab the keys to the entries moved to the right block */
2227 xfs_btree_copy_keys(cur, key, rkp, 1);
2228 } else {
2229 /* It's a leaf. Move records. */
2230 union xfs_btree_rec *lrp; /* left record pointer */
2231 union xfs_btree_rec *rrp; /* right record pointer */
2232
2233 lrp = xfs_btree_rec_addr(cur, src_index, left);
2234 rrp = xfs_btree_rec_addr(cur, 1, right);
2235
2236 xfs_btree_copy_recs(cur, rrp, lrp, rrecs);
2237 xfs_btree_log_recs(cur, rbp, 1, rrecs);
2238
2239 cur->bc_ops->init_key_from_rec(key,
2240 xfs_btree_rec_addr(cur, 1, right));
2241 }
2242
2243
2244 /*
2245 * Find the left block number by looking in the buffer.
2246 * Adjust numrecs, sibling pointers.
2247 */
2248 xfs_btree_get_sibling(cur, left, &rrptr, XFS_BB_RIGHTSIB);
2249 xfs_btree_set_sibling(cur, right, &rrptr, XFS_BB_RIGHTSIB);
2250 xfs_btree_set_sibling(cur, right, &lptr, XFS_BB_LEFTSIB);
2251 xfs_btree_set_sibling(cur, left, &rptr, XFS_BB_RIGHTSIB);
2252
2253 lrecs -= rrecs;
2254 xfs_btree_set_numrecs(left, lrecs);
2255 xfs_btree_set_numrecs(right, xfs_btree_get_numrecs(right) + rrecs);
2256
2257 xfs_btree_log_block(cur, rbp, XFS_BB_ALL_BITS);
2258 xfs_btree_log_block(cur, lbp, XFS_BB_NUMRECS | XFS_BB_RIGHTSIB);
2259
2260 /*
2261 * If there's a block to the new block's right, make that block
2262 * point back to right instead of to left.
2263 */
2264 if (!xfs_btree_ptr_is_null(cur, &rrptr)) {
2265 error = xfs_btree_read_buf_block(cur, &rrptr, level,
2266 0, &rrblock, &rrbp);
2267 if (error)
2268 goto error0;
2269 xfs_btree_set_sibling(cur, rrblock, &rptr, XFS_BB_LEFTSIB);
2270 xfs_btree_log_block(cur, rrbp, XFS_BB_LEFTSIB);
2271 }
2272 /*
2273 * If the cursor is really in the right block, move it there.
2274 * If it's just pointing past the last entry in left, then we'll
2275 * insert there, so don't change anything in that case.
2276 */
2277 if (cur->bc_ptrs[level] > lrecs + 1) {
2278 xfs_btree_setbuf(cur, level, rbp);
2279 cur->bc_ptrs[level] -= lrecs;
2280 }
2281 /*
2282 * If there are more levels, we'll need another cursor which refers
2283 * the right block, no matter where this cursor was.
2284 */
2285 if (level + 1 < cur->bc_nlevels) {
2286 error = xfs_btree_dup_cursor(cur, curp);
2287 if (error)
2288 goto error0;
2289 (*curp)->bc_ptrs[level + 1]++;
2290 }
2291 *ptrp = rptr;
2292 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2293 *stat = 1;
2294 return 0;
2295out0:
2296 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2297 *stat = 0;
2298 return 0;
2299
2300error0:
2301 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
2302 return error;
2303}
2304
2305/*
2306 * Copy the old inode root contents into a real block and make the
2307 * broot point to it.
2308 */
2309int /* error */
2310xfs_btree_new_iroot(
2311 struct xfs_btree_cur *cur, /* btree cursor */
2312 int *logflags, /* logging flags for inode */
2313 int *stat) /* return status - 0 fail */
2314{
2315 struct xfs_buf *cbp; /* buffer for cblock */
2316 struct xfs_btree_block *block; /* btree block */
2317 struct xfs_btree_block *cblock; /* child btree block */
2318 union xfs_btree_key *ckp; /* child key pointer */
2319 union xfs_btree_ptr *cpp; /* child ptr pointer */
2320 union xfs_btree_key *kp; /* pointer to btree key */
2321 union xfs_btree_ptr *pp; /* pointer to block addr */
2322 union xfs_btree_ptr nptr; /* new block addr */
2323 int level; /* btree level */
2324 int error; /* error return code */
2325#ifdef DEBUG
2326 int i; /* loop counter */
2327#endif
2328
2329 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
2330 XFS_BTREE_STATS_INC(cur, newroot);
2331
2332 ASSERT(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE);
2333
2334 level = cur->bc_nlevels - 1;
2335
2336 block = xfs_btree_get_iroot(cur);
2337 pp = xfs_btree_ptr_addr(cur, 1, block);
2338
2339 /* Allocate the new block. If we can't do it, we're toast. Give up. */
2340 error = cur->bc_ops->alloc_block(cur, pp, &nptr, 1, stat);
2341 if (error)
2342 goto error0;
2343 if (*stat == 0) {
2344 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2345 return 0;
2346 }
2347 XFS_BTREE_STATS_INC(cur, alloc);
2348
2349 /* Copy the root into a real block. */
2350 error = xfs_btree_get_buf_block(cur, &nptr, 0, &cblock, &cbp);
2351 if (error)
2352 goto error0;
2353
2354 memcpy(cblock, block, xfs_btree_block_len(cur));
2355
2356 be16_add_cpu(&block->bb_level, 1);
2357 xfs_btree_set_numrecs(block, 1);
2358 cur->bc_nlevels++;
2359 cur->bc_ptrs[level + 1] = 1;
2360
2361 kp = xfs_btree_key_addr(cur, 1, block);
2362 ckp = xfs_btree_key_addr(cur, 1, cblock);
2363 xfs_btree_copy_keys(cur, ckp, kp, xfs_btree_get_numrecs(cblock));
2364
2365 cpp = xfs_btree_ptr_addr(cur, 1, cblock);
2366#ifdef DEBUG
2367 for (i = 0; i < be16_to_cpu(cblock->bb_numrecs); i++) {
2368 error = xfs_btree_check_ptr(cur, pp, i, level);
2369 if (error)
2370 goto error0;
2371 }
2372#endif
2373 xfs_btree_copy_ptrs(cur, cpp, pp, xfs_btree_get_numrecs(cblock));
2374
2375#ifdef DEBUG
2376 error = xfs_btree_check_ptr(cur, &nptr, 0, level);
2377 if (error)
2378 goto error0;
2379#endif
2380 xfs_btree_copy_ptrs(cur, pp, &nptr, 1);
2381
2382 xfs_iroot_realloc(cur->bc_private.b.ip,
2383 1 - xfs_btree_get_numrecs(cblock),
2384 cur->bc_private.b.whichfork);
2385
2386 xfs_btree_setbuf(cur, level, cbp);
2387
2388 /*
2389 * Do all this logging at the end so that
2390 * the root is at the right level.
2391 */
2392 xfs_btree_log_block(cur, cbp, XFS_BB_ALL_BITS);
2393 xfs_btree_log_keys(cur, cbp, 1, be16_to_cpu(cblock->bb_numrecs));
2394 xfs_btree_log_ptrs(cur, cbp, 1, be16_to_cpu(cblock->bb_numrecs));
2395
2396 *logflags |=
2397 XFS_ILOG_CORE | XFS_ILOG_FBROOT(cur->bc_private.b.whichfork);
2398 *stat = 1;
2399 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2400 return 0;
2401error0:
2402 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
2403 return error;
2404}
2405
2406/*
2407 * Allocate a new root block, fill it in.
2408 */
2409STATIC int /* error */
2410xfs_btree_new_root(
2411 struct xfs_btree_cur *cur, /* btree cursor */
2412 int *stat) /* success/failure */
2413{
2414 struct xfs_btree_block *block; /* one half of the old root block */
2415 struct xfs_buf *bp; /* buffer containing block */
2416 int error; /* error return value */
2417 struct xfs_buf *lbp; /* left buffer pointer */
2418 struct xfs_btree_block *left; /* left btree block */
2419 struct xfs_buf *nbp; /* new (root) buffer */
2420 struct xfs_btree_block *new; /* new (root) btree block */
2421 int nptr; /* new value for key index, 1 or 2 */
2422 struct xfs_buf *rbp; /* right buffer pointer */
2423 struct xfs_btree_block *right; /* right btree block */
2424 union xfs_btree_ptr rptr;
2425 union xfs_btree_ptr lptr;
2426
2427 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
2428 XFS_BTREE_STATS_INC(cur, newroot);
2429
2430 /* initialise our start point from the cursor */
2431 cur->bc_ops->init_ptr_from_cur(cur, &rptr);
2432
2433 /* Allocate the new block. If we can't do it, we're toast. Give up. */
2434 error = cur->bc_ops->alloc_block(cur, &rptr, &lptr, 1, stat);
2435 if (error)
2436 goto error0;
2437 if (*stat == 0)
2438 goto out0;
2439 XFS_BTREE_STATS_INC(cur, alloc);
2440
2441 /* Set up the new block. */
2442 error = xfs_btree_get_buf_block(cur, &lptr, 0, &new, &nbp);
2443 if (error)
2444 goto error0;
2445
2446 /* Set the root in the holding structure increasing the level by 1. */
2447 cur->bc_ops->set_root(cur, &lptr, 1);
2448
2449 /*
2450 * At the previous root level there are now two blocks: the old root,
2451 * and the new block generated when it was split. We don't know which
2452 * one the cursor is pointing at, so we set up variables "left" and
2453 * "right" for each case.
2454 */
2455 block = xfs_btree_get_block(cur, cur->bc_nlevels - 1, &bp);
2456
2457#ifdef DEBUG
2458 error = xfs_btree_check_block(cur, block, cur->bc_nlevels - 1, bp);
2459 if (error)
2460 goto error0;
2461#endif
2462
2463 xfs_btree_get_sibling(cur, block, &rptr, XFS_BB_RIGHTSIB);
2464 if (!xfs_btree_ptr_is_null(cur, &rptr)) {
2465 /* Our block is left, pick up the right block. */
2466 lbp = bp;
2467 xfs_btree_buf_to_ptr(cur, lbp, &lptr);
2468 left = block;
2469 error = xfs_btree_read_buf_block(cur, &rptr,
2470 cur->bc_nlevels - 1, 0, &right, &rbp);
2471 if (error)
2472 goto error0;
2473 bp = rbp;
2474 nptr = 1;
2475 } else {
2476 /* Our block is right, pick up the left block. */
2477 rbp = bp;
2478 xfs_btree_buf_to_ptr(cur, rbp, &rptr);
2479 right = block;
2480 xfs_btree_get_sibling(cur, right, &lptr, XFS_BB_LEFTSIB);
2481 error = xfs_btree_read_buf_block(cur, &lptr,
2482 cur->bc_nlevels - 1, 0, &left, &lbp);
2483 if (error)
2484 goto error0;
2485 bp = lbp;
2486 nptr = 2;
2487 }
2488 /* Fill in the new block's btree header and log it. */
2489 xfs_btree_init_block(cur, cur->bc_nlevels, 2, new);
2490 xfs_btree_log_block(cur, nbp, XFS_BB_ALL_BITS);
2491 ASSERT(!xfs_btree_ptr_is_null(cur, &lptr) &&
2492 !xfs_btree_ptr_is_null(cur, &rptr));
2493
2494 /* Fill in the key data in the new root. */
2495 if (xfs_btree_get_level(left) > 0) {
2496 xfs_btree_copy_keys(cur,
2497 xfs_btree_key_addr(cur, 1, new),
2498 xfs_btree_key_addr(cur, 1, left), 1);
2499 xfs_btree_copy_keys(cur,
2500 xfs_btree_key_addr(cur, 2, new),
2501 xfs_btree_key_addr(cur, 1, right), 1);
2502 } else {
2503 cur->bc_ops->init_key_from_rec(
2504 xfs_btree_key_addr(cur, 1, new),
2505 xfs_btree_rec_addr(cur, 1, left));
2506 cur->bc_ops->init_key_from_rec(
2507 xfs_btree_key_addr(cur, 2, new),
2508 xfs_btree_rec_addr(cur, 1, right));
2509 }
2510 xfs_btree_log_keys(cur, nbp, 1, 2);
2511
2512 /* Fill in the pointer data in the new root. */
2513 xfs_btree_copy_ptrs(cur,
2514 xfs_btree_ptr_addr(cur, 1, new), &lptr, 1);
2515 xfs_btree_copy_ptrs(cur,
2516 xfs_btree_ptr_addr(cur, 2, new), &rptr, 1);
2517 xfs_btree_log_ptrs(cur, nbp, 1, 2);
2518
2519 /* Fix up the cursor. */
2520 xfs_btree_setbuf(cur, cur->bc_nlevels, nbp);
2521 cur->bc_ptrs[cur->bc_nlevels] = nptr;
2522 cur->bc_nlevels++;
2523 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2524 *stat = 1;
2525 return 0;
2526error0:
2527 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
2528 return error;
2529out0:
2530 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2531 *stat = 0;
2532 return 0;
2533}
2534
2535STATIC int
2536xfs_btree_make_block_unfull(
2537 struct xfs_btree_cur *cur, /* btree cursor */
2538 int level, /* btree level */
2539 int numrecs,/* # of recs in block */
2540 int *oindex,/* old tree index */
2541 int *index, /* new tree index */
2542 union xfs_btree_ptr *nptr, /* new btree ptr */
2543 struct xfs_btree_cur **ncur, /* new btree cursor */
2544 union xfs_btree_rec *nrec, /* new record */
2545 int *stat)
2546{
2547 union xfs_btree_key key; /* new btree key value */
2548 int error = 0;
2549
2550 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
2551 level == cur->bc_nlevels - 1) {
2552 struct xfs_inode *ip = cur->bc_private.b.ip;
2553
2554 if (numrecs < cur->bc_ops->get_dmaxrecs(cur, level)) {
2555 /* A root block that can be made bigger. */
2556
2557 xfs_iroot_realloc(ip, 1, cur->bc_private.b.whichfork);
2558 } else {
2559 /* A root block that needs replacing */
2560 int logflags = 0;
2561
2562 error = xfs_btree_new_iroot(cur, &logflags, stat);
2563 if (error || *stat == 0)
2564 return error;
2565
2566 xfs_trans_log_inode(cur->bc_tp, ip, logflags);
2567 }
2568
2569 return 0;
2570 }
2571
2572 /* First, try shifting an entry to the right neighbor. */
2573 error = xfs_btree_rshift(cur, level, stat);
2574 if (error || *stat)
2575 return error;
2576
2577 /* Next, try shifting an entry to the left neighbor. */
2578 error = xfs_btree_lshift(cur, level, stat);
2579 if (error)
2580 return error;
2581
2582 if (*stat) {
2583 *oindex = *index = cur->bc_ptrs[level];
2584 return 0;
2585 }
2586
2587 /*
2588 * Next, try splitting the current block in half.
2589 *
2590 * If this works we have to re-set our variables because we
2591 * could be in a different block now.
2592 */
2593 error = xfs_btree_split(cur, level, nptr, &key, ncur, stat);
2594 if (error || *stat == 0)
2595 return error;
2596
2597
2598 *index = cur->bc_ptrs[level];
2599 cur->bc_ops->init_rec_from_key(&key, nrec);
2600 return 0;
2601}
2602
2603/*
2604 * Insert one record/level. Return information to the caller
2605 * allowing the next level up to proceed if necessary.
2606 */
2607STATIC int
2608xfs_btree_insrec(
2609 struct xfs_btree_cur *cur, /* btree cursor */
2610 int level, /* level to insert record at */
2611 union xfs_btree_ptr *ptrp, /* i/o: block number inserted */
2612 union xfs_btree_rec *recp, /* i/o: record data inserted */
2613 struct xfs_btree_cur **curp, /* output: new cursor replacing cur */
2614 int *stat) /* success/failure */
2615{
2616 struct xfs_btree_block *block; /* btree block */
2617 struct xfs_buf *bp; /* buffer for block */
2618 union xfs_btree_key key; /* btree key */
2619 union xfs_btree_ptr nptr; /* new block ptr */
2620 struct xfs_btree_cur *ncur; /* new btree cursor */
2621 union xfs_btree_rec nrec; /* new record count */
2622 int optr; /* old key/record index */
2623 int ptr; /* key/record index */
2624 int numrecs;/* number of records */
2625 int error; /* error return value */
2626#ifdef DEBUG
2627 int i;
2628#endif
2629
2630 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
2631 XFS_BTREE_TRACE_ARGIPR(cur, level, *ptrp, recp);
2632
2633 ncur = NULL;
2634
2635 /*
2636 * If we have an external root pointer, and we've made it to the
2637 * root level, allocate a new root block and we're done.
2638 */
2639 if (!(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
2640 (level >= cur->bc_nlevels)) {
2641 error = xfs_btree_new_root(cur, stat);
2642 xfs_btree_set_ptr_null(cur, ptrp);
2643
2644 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2645 return error;
2646 }
2647
2648 /* If we're off the left edge, return failure. */
2649 ptr = cur->bc_ptrs[level];
2650 if (ptr == 0) {
2651 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2652 *stat = 0;
2653 return 0;
2654 }
2655
2656 /* Make a key out of the record data to be inserted, and save it. */
2657 cur->bc_ops->init_key_from_rec(&key, recp);
2658
2659 optr = ptr;
2660
2661 XFS_BTREE_STATS_INC(cur, insrec);
2662
2663 /* Get pointers to the btree buffer and block. */
2664 block = xfs_btree_get_block(cur, level, &bp);
2665 numrecs = xfs_btree_get_numrecs(block);
2666
2667#ifdef DEBUG
2668 error = xfs_btree_check_block(cur, block, level, bp);
2669 if (error)
2670 goto error0;
2671
2672 /* Check that the new entry is being inserted in the right place. */
2673 if (ptr <= numrecs) {
2674 if (level == 0) {
2675 ASSERT(cur->bc_ops->recs_inorder(cur, recp,
2676 xfs_btree_rec_addr(cur, ptr, block)));
2677 } else {
2678 ASSERT(cur->bc_ops->keys_inorder(cur, &key,
2679 xfs_btree_key_addr(cur, ptr, block)));
2680 }
2681 }
2682#endif
2683
2684 /*
2685 * If the block is full, we can't insert the new entry until we
2686 * make the block un-full.
2687 */
2688 xfs_btree_set_ptr_null(cur, &nptr);
2689 if (numrecs == cur->bc_ops->get_maxrecs(cur, level)) {
2690 error = xfs_btree_make_block_unfull(cur, level, numrecs,
2691 &optr, &ptr, &nptr, &ncur, &nrec, stat);
2692 if (error || *stat == 0)
2693 goto error0;
2694 }
2695
2696 /*
2697 * The current block may have changed if the block was
2698 * previously full and we have just made space in it.
2699 */
2700 block = xfs_btree_get_block(cur, level, &bp);
2701 numrecs = xfs_btree_get_numrecs(block);
2702
2703#ifdef DEBUG
2704 error = xfs_btree_check_block(cur, block, level, bp);
2705 if (error)
2706 return error;
2707#endif
2708
2709 /*
2710 * At this point we know there's room for our new entry in the block
2711 * we're pointing at.
2712 */
2713 XFS_BTREE_STATS_ADD(cur, moves, numrecs - ptr + 1);
2714
2715 if (level > 0) {
2716 /* It's a nonleaf. make a hole in the keys and ptrs */
2717 union xfs_btree_key *kp;
2718 union xfs_btree_ptr *pp;
2719
2720 kp = xfs_btree_key_addr(cur, ptr, block);
2721 pp = xfs_btree_ptr_addr(cur, ptr, block);
2722
2723#ifdef DEBUG
2724 for (i = numrecs - ptr; i >= 0; i--) {
2725 error = xfs_btree_check_ptr(cur, pp, i, level);
2726 if (error)
2727 return error;
2728 }
2729#endif
2730
2731 xfs_btree_shift_keys(cur, kp, 1, numrecs - ptr + 1);
2732 xfs_btree_shift_ptrs(cur, pp, 1, numrecs - ptr + 1);
2733
2734#ifdef DEBUG
2735 error = xfs_btree_check_ptr(cur, ptrp, 0, level);
2736 if (error)
2737 goto error0;
2738#endif
2739
2740 /* Now put the new data in, bump numrecs and log it. */
2741 xfs_btree_copy_keys(cur, kp, &key, 1);
2742 xfs_btree_copy_ptrs(cur, pp, ptrp, 1);
2743 numrecs++;
2744 xfs_btree_set_numrecs(block, numrecs);
2745 xfs_btree_log_ptrs(cur, bp, ptr, numrecs);
2746 xfs_btree_log_keys(cur, bp, ptr, numrecs);
2747#ifdef DEBUG
2748 if (ptr < numrecs) {
2749 ASSERT(cur->bc_ops->keys_inorder(cur, kp,
2750 xfs_btree_key_addr(cur, ptr + 1, block)));
2751 }
2752#endif
2753 } else {
2754 /* It's a leaf. make a hole in the records */
2755 union xfs_btree_rec *rp;
2756
2757 rp = xfs_btree_rec_addr(cur, ptr, block);
2758
2759 xfs_btree_shift_recs(cur, rp, 1, numrecs - ptr + 1);
2760
2761 /* Now put the new data in, bump numrecs and log it. */
2762 xfs_btree_copy_recs(cur, rp, recp, 1);
2763 xfs_btree_set_numrecs(block, ++numrecs);
2764 xfs_btree_log_recs(cur, bp, ptr, numrecs);
2765#ifdef DEBUG
2766 if (ptr < numrecs) {
2767 ASSERT(cur->bc_ops->recs_inorder(cur, rp,
2768 xfs_btree_rec_addr(cur, ptr + 1, block)));
2769 }
2770#endif
2771 }
2772
2773 /* Log the new number of records in the btree header. */
2774 xfs_btree_log_block(cur, bp, XFS_BB_NUMRECS);
2775
2776 /* If we inserted at the start of a block, update the parents' keys. */
2777 if (optr == 1) {
2778 error = xfs_btree_updkey(cur, &key, level + 1);
2779 if (error)
2780 goto error0;
2781 }
2782
2783 /*
2784 * If we are tracking the last record in the tree and
2785 * we are at the far right edge of the tree, update it.
2786 */
2787 if (xfs_btree_is_lastrec(cur, block, level)) {
2788 cur->bc_ops->update_lastrec(cur, block, recp,
2789 ptr, LASTREC_INSREC);
2790 }
2791
2792 /*
2793 * Return the new block number, if any.
2794 * If there is one, give back a record value and a cursor too.
2795 */
2796 *ptrp = nptr;
2797 if (!xfs_btree_ptr_is_null(cur, &nptr)) {
2798 *recp = nrec;
2799 *curp = ncur;
2800 }
2801
2802 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2803 *stat = 1;
2804 return 0;
2805
2806error0:
2807 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
2808 return error;
2809}
2810
2811/*
2812 * Insert the record at the point referenced by cur.
2813 *
2814 * A multi-level split of the tree on insert will invalidate the original
2815 * cursor. All callers of this function should assume that the cursor is
2816 * no longer valid and revalidate it.
2817 */
2818int
2819xfs_btree_insert(
2820 struct xfs_btree_cur *cur,
2821 int *stat)
2822{
2823 int error; /* error return value */
2824 int i; /* result value, 0 for failure */
2825 int level; /* current level number in btree */
2826 union xfs_btree_ptr nptr; /* new block number (split result) */
2827 struct xfs_btree_cur *ncur; /* new cursor (split result) */
2828 struct xfs_btree_cur *pcur; /* previous level's cursor */
2829 union xfs_btree_rec rec; /* record to insert */
2830
2831 level = 0;
2832 ncur = NULL;
2833 pcur = cur;
2834
2835 xfs_btree_set_ptr_null(cur, &nptr);
2836 cur->bc_ops->init_rec_from_cur(cur, &rec);
2837
2838 /*
2839 * Loop going up the tree, starting at the leaf level.
2840 * Stop when we don't get a split block, that must mean that
2841 * the insert is finished with this level.
2842 */
2843 do {
2844 /*
2845 * Insert nrec/nptr into this level of the tree.
2846 * Note if we fail, nptr will be null.
2847 */
2848 error = xfs_btree_insrec(pcur, level, &nptr, &rec, &ncur, &i);
2849 if (error) {
2850 if (pcur != cur)
2851 xfs_btree_del_cursor(pcur, XFS_BTREE_ERROR);
2852 goto error0;
2853 }
2854
2855 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
2856 level++;
2857
2858 /*
2859 * See if the cursor we just used is trash.
2860 * Can't trash the caller's cursor, but otherwise we should
2861 * if ncur is a new cursor or we're about to be done.
2862 */
2863 if (pcur != cur &&
2864 (ncur || xfs_btree_ptr_is_null(cur, &nptr))) {
2865 /* Save the state from the cursor before we trash it */
2866 if (cur->bc_ops->update_cursor)
2867 cur->bc_ops->update_cursor(pcur, cur);
2868 cur->bc_nlevels = pcur->bc_nlevels;
2869 xfs_btree_del_cursor(pcur, XFS_BTREE_NOERROR);
2870 }
2871 /* If we got a new cursor, switch to it. */
2872 if (ncur) {
2873 pcur = ncur;
2874 ncur = NULL;
2875 }
2876 } while (!xfs_btree_ptr_is_null(cur, &nptr));
2877
2878 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2879 *stat = i;
2880 return 0;
2881error0:
2882 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
2883 return error;
2884}
2885
2886/*
2887 * Try to merge a non-leaf block back into the inode root.
2888 *
2889 * Note: the killroot names comes from the fact that we're effectively
2890 * killing the old root block. But because we can't just delete the
2891 * inode we have to copy the single block it was pointing to into the
2892 * inode.
2893 */
2894int
2895xfs_btree_kill_iroot(
2896 struct xfs_btree_cur *cur)
2897{
2898 int whichfork = cur->bc_private.b.whichfork;
2899 struct xfs_inode *ip = cur->bc_private.b.ip;
2900 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
2901 struct xfs_btree_block *block;
2902 struct xfs_btree_block *cblock;
2903 union xfs_btree_key *kp;
2904 union xfs_btree_key *ckp;
2905 union xfs_btree_ptr *pp;
2906 union xfs_btree_ptr *cpp;
2907 struct xfs_buf *cbp;
2908 int level;
2909 int index;
2910 int numrecs;
2911#ifdef DEBUG
2912 union xfs_btree_ptr ptr;
2913 int i;
2914#endif
2915
2916 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
2917
2918 ASSERT(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE);
2919 ASSERT(cur->bc_nlevels > 1);
2920
2921 /*
2922 * Don't deal with the root block needs to be a leaf case.
2923 * We're just going to turn the thing back into extents anyway.
2924 */
2925 level = cur->bc_nlevels - 1;
2926 if (level == 1)
2927 goto out0;
2928
2929 /*
2930 * Give up if the root has multiple children.
2931 */
2932 block = xfs_btree_get_iroot(cur);
2933 if (xfs_btree_get_numrecs(block) != 1)
2934 goto out0;
2935
2936 cblock = xfs_btree_get_block(cur, level - 1, &cbp);
2937 numrecs = xfs_btree_get_numrecs(cblock);
2938
2939 /*
2940 * Only do this if the next level will fit.
2941 * Then the data must be copied up to the inode,
2942 * instead of freeing the root you free the next level.
2943 */
2944 if (numrecs > cur->bc_ops->get_dmaxrecs(cur, level))
2945 goto out0;
2946
2947 XFS_BTREE_STATS_INC(cur, killroot);
2948
2949#ifdef DEBUG
2950 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_LEFTSIB);
2951 ASSERT(xfs_btree_ptr_is_null(cur, &ptr));
2952 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_RIGHTSIB);
2953 ASSERT(xfs_btree_ptr_is_null(cur, &ptr));
2954#endif
2955
2956 index = numrecs - cur->bc_ops->get_maxrecs(cur, level);
2957 if (index) {
2958 xfs_iroot_realloc(cur->bc_private.b.ip, index,
2959 cur->bc_private.b.whichfork);
b3563c19 2960 block = ifp->if_broot;
b194c7d8
BN
2961 }
2962
2963 be16_add_cpu(&block->bb_numrecs, index);
2964 ASSERT(block->bb_numrecs == cblock->bb_numrecs);
2965
2966 kp = xfs_btree_key_addr(cur, 1, block);
2967 ckp = xfs_btree_key_addr(cur, 1, cblock);
2968 xfs_btree_copy_keys(cur, kp, ckp, numrecs);
2969
2970 pp = xfs_btree_ptr_addr(cur, 1, block);
2971 cpp = xfs_btree_ptr_addr(cur, 1, cblock);
2972#ifdef DEBUG
2973 for (i = 0; i < numrecs; i++) {
2974 int error;
2975
2976 error = xfs_btree_check_ptr(cur, cpp, i, level - 1);
2977 if (error) {
2978 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
2979 return error;
2980 }
2981 }
2982#endif
2983 xfs_btree_copy_ptrs(cur, pp, cpp, numrecs);
2984
2985 cur->bc_ops->free_block(cur, cbp);
2986 XFS_BTREE_STATS_INC(cur, free);
2987
2988 cur->bc_bufs[level - 1] = NULL;
2989 be16_add_cpu(&block->bb_level, -1);
2990 xfs_trans_log_inode(cur->bc_tp, ip,
2991 XFS_ILOG_CORE | XFS_ILOG_FBROOT(cur->bc_private.b.whichfork));
2992 cur->bc_nlevels--;
2993out0:
2994 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2995 return 0;
2996}
2997
2998STATIC int
2999xfs_btree_dec_cursor(
3000 struct xfs_btree_cur *cur,
3001 int level,
3002 int *stat)
3003{
3004 int error;
3005 int i;
3006
3007 if (level > 0) {
3008 error = xfs_btree_decrement(cur, level, &i);
3009 if (error)
3010 return error;
3011 }
3012
3013 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3014 *stat = 1;
3015 return 0;
3016}
3017
3018/*
3019 * Single level of the btree record deletion routine.
3020 * Delete record pointed to by cur/level.
3021 * Remove the record from its block then rebalance the tree.
3022 * Return 0 for error, 1 for done, 2 to go on to the next level.
3023 */
3024STATIC int /* error */
3025xfs_btree_delrec(
3026 struct xfs_btree_cur *cur, /* btree cursor */
3027 int level, /* level removing record from */
3028 int *stat) /* fail/done/go-on */
3029{
3030 struct xfs_btree_block *block; /* btree block */
3031 union xfs_btree_ptr cptr; /* current block ptr */
3032 struct xfs_buf *bp; /* buffer for block */
3033 int error; /* error return value */
3034 int i; /* loop counter */
3035 union xfs_btree_key key; /* storage for keyp */
3036 union xfs_btree_key *keyp = &key; /* passed to the next level */
3037 union xfs_btree_ptr lptr; /* left sibling block ptr */
3038 struct xfs_buf *lbp; /* left buffer pointer */
3039 struct xfs_btree_block *left; /* left btree block */
3040 int lrecs = 0; /* left record count */
3041 int ptr; /* key/record index */
3042 union xfs_btree_ptr rptr; /* right sibling block ptr */
3043 struct xfs_buf *rbp; /* right buffer pointer */
3044 struct xfs_btree_block *right; /* right btree block */
3045 struct xfs_btree_block *rrblock; /* right-right btree block */
3046 struct xfs_buf *rrbp; /* right-right buffer pointer */
3047 int rrecs = 0; /* right record count */
3048 struct xfs_btree_cur *tcur; /* temporary btree cursor */
3049 int numrecs; /* temporary numrec count */
3050
3051 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
3052 XFS_BTREE_TRACE_ARGI(cur, level);
3053
3054 tcur = NULL;
3055
3056 /* Get the index of the entry being deleted, check for nothing there. */
3057 ptr = cur->bc_ptrs[level];
3058 if (ptr == 0) {
3059 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3060 *stat = 0;
3061 return 0;
3062 }
3063
3064 /* Get the buffer & block containing the record or key/ptr. */
3065 block = xfs_btree_get_block(cur, level, &bp);
3066 numrecs = xfs_btree_get_numrecs(block);
3067
3068#ifdef DEBUG
3069 error = xfs_btree_check_block(cur, block, level, bp);
3070 if (error)
3071 goto error0;
3072#endif
3073
3074 /* Fail if we're off the end of the block. */
3075 if (ptr > numrecs) {
3076 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3077 *stat = 0;
3078 return 0;
3079 }
3080
3081 XFS_BTREE_STATS_INC(cur, delrec);
3082 XFS_BTREE_STATS_ADD(cur, moves, numrecs - ptr);
3083
3084 /* Excise the entries being deleted. */
3085 if (level > 0) {
3086 /* It's a nonleaf. operate on keys and ptrs */
3087 union xfs_btree_key *lkp;
3088 union xfs_btree_ptr *lpp;
3089
3090 lkp = xfs_btree_key_addr(cur, ptr + 1, block);
3091 lpp = xfs_btree_ptr_addr(cur, ptr + 1, block);
3092
3093#ifdef DEBUG
3094 for (i = 0; i < numrecs - ptr; i++) {
3095 error = xfs_btree_check_ptr(cur, lpp, i, level);
3096 if (error)
3097 goto error0;
3098 }
3099#endif
3100
3101 if (ptr < numrecs) {
3102 xfs_btree_shift_keys(cur, lkp, -1, numrecs - ptr);
3103 xfs_btree_shift_ptrs(cur, lpp, -1, numrecs - ptr);
3104 xfs_btree_log_keys(cur, bp, ptr, numrecs - 1);
3105 xfs_btree_log_ptrs(cur, bp, ptr, numrecs - 1);
3106 }
3107
3108 /*
3109 * If it's the first record in the block, we'll need to pass a
3110 * key up to the next level (updkey).
3111 */
3112 if (ptr == 1)
3113 keyp = xfs_btree_key_addr(cur, 1, block);
3114 } else {
3115 /* It's a leaf. operate on records */
3116 if (ptr < numrecs) {
3117 xfs_btree_shift_recs(cur,
3118 xfs_btree_rec_addr(cur, ptr + 1, block),
3119 -1, numrecs - ptr);
3120 xfs_btree_log_recs(cur, bp, ptr, numrecs - 1);
3121 }
3122
3123 /*
3124 * If it's the first record in the block, we'll need a key
3125 * structure to pass up to the next level (updkey).
3126 */
3127 if (ptr == 1) {
3128 cur->bc_ops->init_key_from_rec(&key,
3129 xfs_btree_rec_addr(cur, 1, block));
3130 keyp = &key;
3131 }
3132 }
3133
3134 /*
3135 * Decrement and log the number of entries in the block.
3136 */
3137 xfs_btree_set_numrecs(block, --numrecs);
3138 xfs_btree_log_block(cur, bp, XFS_BB_NUMRECS);
3139
3140 /*
3141 * If we are tracking the last record in the tree and
3142 * we are at the far right edge of the tree, update it.
3143 */
3144 if (xfs_btree_is_lastrec(cur, block, level)) {
3145 cur->bc_ops->update_lastrec(cur, block, NULL,
3146 ptr, LASTREC_DELREC);
3147 }
3148
3149 /*
3150 * We're at the root level. First, shrink the root block in-memory.
3151 * Try to get rid of the next level down. If we can't then there's
3152 * nothing left to do.
3153 */
3154 if (level == cur->bc_nlevels - 1) {
3155 if (cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) {
3156 xfs_iroot_realloc(cur->bc_private.b.ip, -1,
3157 cur->bc_private.b.whichfork);
3158
3159 error = xfs_btree_kill_iroot(cur);
3160 if (error)
3161 goto error0;
3162
3163 error = xfs_btree_dec_cursor(cur, level, stat);
3164 if (error)
3165 goto error0;
3166 *stat = 1;
3167 return 0;
3168 }
3169
3170 /*
3171 * If this is the root level, and there's only one entry left,
3172 * and it's NOT the leaf level, then we can get rid of this
3173 * level.
3174 */
3175 if (numrecs == 1 && level > 0) {
3176 union xfs_btree_ptr *pp;
3177 /*
3178 * pp is still set to the first pointer in the block.
3179 * Make it the new root of the btree.
3180 */
3181 pp = xfs_btree_ptr_addr(cur, 1, block);
3182 error = cur->bc_ops->kill_root(cur, bp, level, pp);
3183 if (error)
3184 goto error0;
3185 } else if (level > 0) {
3186 error = xfs_btree_dec_cursor(cur, level, stat);
3187 if (error)
3188 goto error0;
3189 }
3190 *stat = 1;
3191 return 0;
3192 }
3193
3194 /*
3195 * If we deleted the leftmost entry in the block, update the
3196 * key values above us in the tree.
3197 */
3198 if (ptr == 1) {
3199 error = xfs_btree_updkey(cur, keyp, level + 1);
3200 if (error)
3201 goto error0;
3202 }
3203
3204 /*
3205 * If the number of records remaining in the block is at least
3206 * the minimum, we're done.
3207 */
3208 if (numrecs >= cur->bc_ops->get_minrecs(cur, level)) {
3209 error = xfs_btree_dec_cursor(cur, level, stat);
3210 if (error)
3211 goto error0;
3212 return 0;
3213 }
3214
3215 /*
3216 * Otherwise, we have to move some records around to keep the
3217 * tree balanced. Look at the left and right sibling blocks to
3218 * see if we can re-balance by moving only one record.
3219 */
3220 xfs_btree_get_sibling(cur, block, &rptr, XFS_BB_RIGHTSIB);
3221 xfs_btree_get_sibling(cur, block, &lptr, XFS_BB_LEFTSIB);
3222
3223 if (cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) {
3224 /*
3225 * One child of root, need to get a chance to copy its contents
3226 * into the root and delete it. Can't go up to next level,
3227 * there's nothing to delete there.
3228 */
3229 if (xfs_btree_ptr_is_null(cur, &rptr) &&
3230 xfs_btree_ptr_is_null(cur, &lptr) &&
3231 level == cur->bc_nlevels - 2) {
3232 error = xfs_btree_kill_iroot(cur);
3233 if (!error)
3234 error = xfs_btree_dec_cursor(cur, level, stat);
3235 if (error)
3236 goto error0;
3237 return 0;
3238 }
3239 }
3240
3241 ASSERT(!xfs_btree_ptr_is_null(cur, &rptr) ||
3242 !xfs_btree_ptr_is_null(cur, &lptr));
3243
3244 /*
3245 * Duplicate the cursor so our btree manipulations here won't
3246 * disrupt the next level up.
3247 */
3248 error = xfs_btree_dup_cursor(cur, &tcur);
3249 if (error)
3250 goto error0;
3251
3252 /*
3253 * If there's a right sibling, see if it's ok to shift an entry
3254 * out of it.
3255 */
3256 if (!xfs_btree_ptr_is_null(cur, &rptr)) {
3257 /*
3258 * Move the temp cursor to the last entry in the next block.
3259 * Actually any entry but the first would suffice.
3260 */
3261 i = xfs_btree_lastrec(tcur, level);
3262 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
3263
3264 error = xfs_btree_increment(tcur, level, &i);
3265 if (error)
3266 goto error0;
3267 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
3268
3269 i = xfs_btree_lastrec(tcur, level);
3270 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
3271
3272 /* Grab a pointer to the block. */
3273 right = xfs_btree_get_block(tcur, level, &rbp);
3274#ifdef DEBUG
3275 error = xfs_btree_check_block(tcur, right, level, rbp);
3276 if (error)
3277 goto error0;
3278#endif
3279 /* Grab the current block number, for future use. */
3280 xfs_btree_get_sibling(tcur, right, &cptr, XFS_BB_LEFTSIB);
3281
3282 /*
3283 * If right block is full enough so that removing one entry
3284 * won't make it too empty, and left-shifting an entry out
3285 * of right to us works, we're done.
3286 */
3287 if (xfs_btree_get_numrecs(right) - 1 >=
3288 cur->bc_ops->get_minrecs(tcur, level)) {
3289 error = xfs_btree_lshift(tcur, level, &i);
3290 if (error)
3291 goto error0;
3292 if (i) {
3293 ASSERT(xfs_btree_get_numrecs(block) >=
3294 cur->bc_ops->get_minrecs(tcur, level));
3295
3296 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
3297 tcur = NULL;
3298
3299 error = xfs_btree_dec_cursor(cur, level, stat);
3300 if (error)
3301 goto error0;
3302 return 0;
3303 }
3304 }
3305
3306 /*
3307 * Otherwise, grab the number of records in right for
3308 * future reference, and fix up the temp cursor to point
3309 * to our block again (last record).
3310 */
3311 rrecs = xfs_btree_get_numrecs(right);
3312 if (!xfs_btree_ptr_is_null(cur, &lptr)) {
3313 i = xfs_btree_firstrec(tcur, level);
3314 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
3315
3316 error = xfs_btree_decrement(tcur, level, &i);
3317 if (error)
3318 goto error0;
3319 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
3320 }
3321 }
3322
3323 /*
3324 * If there's a left sibling, see if it's ok to shift an entry
3325 * out of it.
3326 */
3327 if (!xfs_btree_ptr_is_null(cur, &lptr)) {
3328 /*
3329 * Move the temp cursor to the first entry in the
3330 * previous block.
3331 */
3332 i = xfs_btree_firstrec(tcur, level);
3333 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
3334
3335 error = xfs_btree_decrement(tcur, level, &i);
3336 if (error)
3337 goto error0;
3338 i = xfs_btree_firstrec(tcur, level);
3339 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
3340
3341 /* Grab a pointer to the block. */
3342 left = xfs_btree_get_block(tcur, level, &lbp);
3343#ifdef DEBUG
3344 error = xfs_btree_check_block(cur, left, level, lbp);
3345 if (error)
3346 goto error0;
3347#endif
3348 /* Grab the current block number, for future use. */
3349 xfs_btree_get_sibling(tcur, left, &cptr, XFS_BB_RIGHTSIB);
3350
3351 /*
3352 * If left block is full enough so that removing one entry
3353 * won't make it too empty, and right-shifting an entry out
3354 * of left to us works, we're done.
3355 */
3356 if (xfs_btree_get_numrecs(left) - 1 >=
3357 cur->bc_ops->get_minrecs(tcur, level)) {
3358 error = xfs_btree_rshift(tcur, level, &i);
3359 if (error)
3360 goto error0;
3361 if (i) {
3362 ASSERT(xfs_btree_get_numrecs(block) >=
3363 cur->bc_ops->get_minrecs(tcur, level));
3364 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
3365 tcur = NULL;
3366 if (level == 0)
3367 cur->bc_ptrs[0]++;
3368 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3369 *stat = 1;
3370 return 0;
3371 }
3372 }
3373
3374 /*
3375 * Otherwise, grab the number of records in right for
3376 * future reference.
3377 */
3378 lrecs = xfs_btree_get_numrecs(left);
3379 }
3380
3381 /* Delete the temp cursor, we're done with it. */
3382 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
3383 tcur = NULL;
3384
3385 /* If here, we need to do a join to keep the tree balanced. */
3386 ASSERT(!xfs_btree_ptr_is_null(cur, &cptr));
3387
3388 if (!xfs_btree_ptr_is_null(cur, &lptr) &&
3389 lrecs + xfs_btree_get_numrecs(block) <=
3390 cur->bc_ops->get_maxrecs(cur, level)) {
3391 /*
3392 * Set "right" to be the starting block,
3393 * "left" to be the left neighbor.
3394 */
3395 rptr = cptr;
3396 right = block;
3397 rbp = bp;
3398 error = xfs_btree_read_buf_block(cur, &lptr, level,
3399 0, &left, &lbp);
3400 if (error)
3401 goto error0;
3402
3403 /*
3404 * If that won't work, see if we can join with the right neighbor block.
3405 */
3406 } else if (!xfs_btree_ptr_is_null(cur, &rptr) &&
3407 rrecs + xfs_btree_get_numrecs(block) <=
3408 cur->bc_ops->get_maxrecs(cur, level)) {
3409 /*
3410 * Set "left" to be the starting block,
3411 * "right" to be the right neighbor.
3412 */
3413 lptr = cptr;
3414 left = block;
3415 lbp = bp;
3416 error = xfs_btree_read_buf_block(cur, &rptr, level,
3417 0, &right, &rbp);
3418 if (error)
3419 goto error0;
3420
3421 /*
3422 * Otherwise, we can't fix the imbalance.
3423 * Just return. This is probably a logic error, but it's not fatal.
3424 */
3425 } else {
3426 error = xfs_btree_dec_cursor(cur, level, stat);
3427 if (error)
3428 goto error0;
3429 return 0;
3430 }
3431
3432 rrecs = xfs_btree_get_numrecs(right);
3433 lrecs = xfs_btree_get_numrecs(left);
3434
3435 /*
3436 * We're now going to join "left" and "right" by moving all the stuff
3437 * in "right" to "left" and deleting "right".
3438 */
3439 XFS_BTREE_STATS_ADD(cur, moves, rrecs);
3440 if (level > 0) {
3441 /* It's a non-leaf. Move keys and pointers. */
3442 union xfs_btree_key *lkp; /* left btree key */
3443 union xfs_btree_ptr *lpp; /* left address pointer */
3444 union xfs_btree_key *rkp; /* right btree key */
3445 union xfs_btree_ptr *rpp; /* right address pointer */
3446
3447 lkp = xfs_btree_key_addr(cur, lrecs + 1, left);
3448 lpp = xfs_btree_ptr_addr(cur, lrecs + 1, left);
3449 rkp = xfs_btree_key_addr(cur, 1, right);
3450 rpp = xfs_btree_ptr_addr(cur, 1, right);
3451#ifdef DEBUG
3452 for (i = 1; i < rrecs; i++) {
3453 error = xfs_btree_check_ptr(cur, rpp, i, level);
3454 if (error)
3455 goto error0;
3456 }
3457#endif
3458 xfs_btree_copy_keys(cur, lkp, rkp, rrecs);
3459 xfs_btree_copy_ptrs(cur, lpp, rpp, rrecs);
3460
3461 xfs_btree_log_keys(cur, lbp, lrecs + 1, lrecs + rrecs);
3462 xfs_btree_log_ptrs(cur, lbp, lrecs + 1, lrecs + rrecs);
3463 } else {
3464 /* It's a leaf. Move records. */
3465 union xfs_btree_rec *lrp; /* left record pointer */
3466 union xfs_btree_rec *rrp; /* right record pointer */
3467
3468 lrp = xfs_btree_rec_addr(cur, lrecs + 1, left);
3469 rrp = xfs_btree_rec_addr(cur, 1, right);
3470
3471 xfs_btree_copy_recs(cur, lrp, rrp, rrecs);
3472 xfs_btree_log_recs(cur, lbp, lrecs + 1, lrecs + rrecs);
3473 }
3474
3475 XFS_BTREE_STATS_INC(cur, join);
3476
3477 /*
3478 * Fix up the the number of records and right block pointer in the
3479 * surviving block, and log it.
3480 */
3481 xfs_btree_set_numrecs(left, lrecs + rrecs);
3482 xfs_btree_get_sibling(cur, right, &cptr, XFS_BB_RIGHTSIB),
3483 xfs_btree_set_sibling(cur, left, &cptr, XFS_BB_RIGHTSIB);
3484 xfs_btree_log_block(cur, lbp, XFS_BB_NUMRECS | XFS_BB_RIGHTSIB);
3485
3486 /* If there is a right sibling, point it to the remaining block. */
3487 xfs_btree_get_sibling(cur, left, &cptr, XFS_BB_RIGHTSIB);
3488 if (!xfs_btree_ptr_is_null(cur, &cptr)) {
3489 error = xfs_btree_read_buf_block(cur, &cptr, level,
3490 0, &rrblock, &rrbp);
3491 if (error)
3492 goto error0;
3493 xfs_btree_set_sibling(cur, rrblock, &lptr, XFS_BB_LEFTSIB);
3494 xfs_btree_log_block(cur, rrbp, XFS_BB_LEFTSIB);
3495 }
3496
3497 /* Free the deleted block. */
3498 error = cur->bc_ops->free_block(cur, rbp);
3499 if (error)
3500 goto error0;
3501 XFS_BTREE_STATS_INC(cur, free);
3502
3503 /*
3504 * If we joined with the left neighbor, set the buffer in the
3505 * cursor to the left block, and fix up the index.
3506 */
3507 if (bp != lbp) {
3508 cur->bc_bufs[level] = lbp;
3509 cur->bc_ptrs[level] += lrecs;
3510 cur->bc_ra[level] = 0;
3511 }
3512 /*
3513 * If we joined with the right neighbor and there's a level above
3514 * us, increment the cursor at that level.
3515 */
3516 else if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) ||
3517 (level + 1 < cur->bc_nlevels)) {
3518 error = xfs_btree_increment(cur, level + 1, &i);
3519 if (error)
3520 goto error0;
3521 }
3522
3523 /*
3524 * Readjust the ptr at this level if it's not a leaf, since it's
3525 * still pointing at the deletion point, which makes the cursor
3526 * inconsistent. If this makes the ptr 0, the caller fixes it up.
3527 * We can't use decrement because it would change the next level up.
3528 */
3529 if (level > 0)
3530 cur->bc_ptrs[level]--;
3531
3532 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3533 /* Return value means the next level up has something to do. */
3534 *stat = 2;
3535 return 0;
3536
3537error0:
3538 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
3539 if (tcur)
3540 xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
3541 return error;
3542}
3543
3544/*
3545 * Delete the record pointed to by cur.
3546 * The cursor refers to the place where the record was (could be inserted)
3547 * when the operation returns.
3548 */
3549int /* error */
3550xfs_btree_delete(
3551 struct xfs_btree_cur *cur,
3552 int *stat) /* success/failure */
3553{
3554 int error; /* error return value */
3555 int level;
3556 int i;
3557
3558 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
3559
3560 /*
3561 * Go up the tree, starting at leaf level.
3562 *
3563 * If 2 is returned then a join was done; go to the next level.
3564 * Otherwise we are done.
3565 */
3566 for (level = 0, i = 2; i == 2; level++) {
3567 error = xfs_btree_delrec(cur, level, &i);
3568 if (error)
3569 goto error0;
3570 }
3571
3572 if (i == 0) {
3573 for (level = 1; level < cur->bc_nlevels; level++) {
3574 if (cur->bc_ptrs[level] == 0) {
3575 error = xfs_btree_decrement(cur, level, &i);
3576 if (error)
3577 goto error0;
3578 break;
3579 }
3580 }
3581 }
3582
3583 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3584 *stat = i;
3585 return 0;
3586error0:
3587 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
3588 return error;
3589}
3590
3591/*
3592 * Get the data from the pointed-to record.
3593 */
3594int /* error */
3595xfs_btree_get_rec(
3596 struct xfs_btree_cur *cur, /* btree cursor */
3597 union xfs_btree_rec **recp, /* output: btree record */
3598 int *stat) /* output: success/failure */
3599{
3600 struct xfs_btree_block *block; /* btree block */
3601 struct xfs_buf *bp; /* buffer pointer */
3602 int ptr; /* record number */
3603#ifdef DEBUG
3604 int error; /* error return value */
3605#endif
3606
3607 ptr = cur->bc_ptrs[0];
3608 block = xfs_btree_get_block(cur, 0, &bp);
3609
3610#ifdef DEBUG
3611 error = xfs_btree_check_block(cur, block, 0, bp);
3612 if (error)
3613 return error;
3614#endif
3615
3616 /*
3617 * Off the right end or left end, return failure.
3618 */
3619 if (ptr > xfs_btree_get_numrecs(block) || ptr <= 0) {
3620 *stat = 0;
3621 return 0;
3622 }
3623
3624 /*
3625 * Point to the record and extract its data.
3626 */
3627 *recp = xfs_btree_rec_addr(cur, ptr, block);
3628 *stat = 1;
3629 return 0;
3630}