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