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