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