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