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