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