1 /* 2 * Copyright (C) 2011 STRATO. All rights reserved. 3 * 4 * This program is free software; you can redistribute it and/or 5 * modify it under the terms of the GNU General Public 6 * License v2 as published by the Free Software Foundation. 7 * 8 * This program is distributed in the hope that it will be useful, 9 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 11 * General Public License for more details. 12 * 13 * You should have received a copy of the GNU General Public 14 * License along with this program; if not, write to the 15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330, 16 * Boston, MA 021110-1307, USA. 17 */ 18 19 #include <linux/mm.h> 20 #include <linux/rbtree.h> 21 #include "ctree.h" 22 #include "disk-io.h" 23 #include "backref.h" 24 #include "ulist.h" 25 #include "transaction.h" 26 #include "delayed-ref.h" 27 #include "locking.h" 28 29 /* Just an arbitrary number so we can be sure this happened */ 30 #define BACKREF_FOUND_SHARED 6 31 32 struct extent_inode_elem { 33 u64 inum; 34 u64 offset; 35 struct extent_inode_elem *next; 36 }; 37 38 static int check_extent_in_eb(const struct btrfs_key *key, 39 const struct extent_buffer *eb, 40 const struct btrfs_file_extent_item *fi, 41 u64 extent_item_pos, 42 struct extent_inode_elem **eie) 43 { 44 u64 offset = 0; 45 struct extent_inode_elem *e; 46 47 if (!btrfs_file_extent_compression(eb, fi) && 48 !btrfs_file_extent_encryption(eb, fi) && 49 !btrfs_file_extent_other_encoding(eb, fi)) { 50 u64 data_offset; 51 u64 data_len; 52 53 data_offset = btrfs_file_extent_offset(eb, fi); 54 data_len = btrfs_file_extent_num_bytes(eb, fi); 55 56 if (extent_item_pos < data_offset || 57 extent_item_pos >= data_offset + data_len) 58 return 1; 59 offset = extent_item_pos - data_offset; 60 } 61 62 e = kmalloc(sizeof(*e), GFP_NOFS); 63 if (!e) 64 return -ENOMEM; 65 66 e->next = *eie; 67 e->inum = key->objectid; 68 e->offset = key->offset + offset; 69 *eie = e; 70 71 return 0; 72 } 73 74 static void free_inode_elem_list(struct extent_inode_elem *eie) 75 { 76 struct extent_inode_elem *eie_next; 77 78 for (; eie; eie = eie_next) { 79 eie_next = eie->next; 80 kfree(eie); 81 } 82 } 83 84 static int find_extent_in_eb(const struct extent_buffer *eb, 85 u64 wanted_disk_byte, u64 extent_item_pos, 86 struct extent_inode_elem **eie) 87 { 88 u64 disk_byte; 89 struct btrfs_key key; 90 struct btrfs_file_extent_item *fi; 91 int slot; 92 int nritems; 93 int extent_type; 94 int ret; 95 96 /* 97 * from the shared data ref, we only have the leaf but we need 98 * the key. thus, we must look into all items and see that we 99 * find one (some) with a reference to our extent item. 100 */ 101 nritems = btrfs_header_nritems(eb); 102 for (slot = 0; slot < nritems; ++slot) { 103 btrfs_item_key_to_cpu(eb, &key, slot); 104 if (key.type != BTRFS_EXTENT_DATA_KEY) 105 continue; 106 fi = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item); 107 extent_type = btrfs_file_extent_type(eb, fi); 108 if (extent_type == BTRFS_FILE_EXTENT_INLINE) 109 continue; 110 /* don't skip BTRFS_FILE_EXTENT_PREALLOC, we can handle that */ 111 disk_byte = btrfs_file_extent_disk_bytenr(eb, fi); 112 if (disk_byte != wanted_disk_byte) 113 continue; 114 115 ret = check_extent_in_eb(&key, eb, fi, extent_item_pos, eie); 116 if (ret < 0) 117 return ret; 118 } 119 120 return 0; 121 } 122 123 /* 124 * this structure records all encountered refs on the way up to the root 125 */ 126 struct prelim_ref { 127 struct rb_node rbnode; 128 u64 root_id; 129 struct btrfs_key key_for_search; 130 int level; 131 int count; 132 struct extent_inode_elem *inode_list; 133 u64 parent; 134 u64 wanted_disk_byte; 135 }; 136 137 struct preftree { 138 struct rb_root root; 139 unsigned int count; 140 }; 141 142 #define PREFTREE_INIT { .root = RB_ROOT, .count = 0 } 143 144 struct preftrees { 145 struct preftree direct; /* BTRFS_SHARED_[DATA|BLOCK]_REF_KEY */ 146 struct preftree indirect; /* BTRFS_[TREE_BLOCK|EXTENT_DATA]_REF_KEY */ 147 struct preftree indirect_missing_keys; 148 }; 149 150 static struct kmem_cache *btrfs_prelim_ref_cache; 151 152 int __init btrfs_prelim_ref_init(void) 153 { 154 btrfs_prelim_ref_cache = kmem_cache_create("btrfs_prelim_ref", 155 sizeof(struct prelim_ref), 156 0, 157 SLAB_MEM_SPREAD, 158 NULL); 159 if (!btrfs_prelim_ref_cache) 160 return -ENOMEM; 161 return 0; 162 } 163 164 void btrfs_prelim_ref_exit(void) 165 { 166 kmem_cache_destroy(btrfs_prelim_ref_cache); 167 } 168 169 static void free_pref(struct prelim_ref *ref) 170 { 171 kmem_cache_free(btrfs_prelim_ref_cache, ref); 172 } 173 174 /* 175 * Return 0 when both refs are for the same block (and can be merged). 176 * A -1 return indicates ref1 is a 'lower' block than ref2, while 1 177 * indicates a 'higher' block. 178 */ 179 static int prelim_ref_compare(struct prelim_ref *ref1, 180 struct prelim_ref *ref2) 181 { 182 if (ref1->level < ref2->level) 183 return -1; 184 if (ref1->level > ref2->level) 185 return 1; 186 if (ref1->root_id < ref2->root_id) 187 return -1; 188 if (ref1->root_id > ref2->root_id) 189 return 1; 190 if (ref1->key_for_search.type < ref2->key_for_search.type) 191 return -1; 192 if (ref1->key_for_search.type > ref2->key_for_search.type) 193 return 1; 194 if (ref1->key_for_search.objectid < ref2->key_for_search.objectid) 195 return -1; 196 if (ref1->key_for_search.objectid > ref2->key_for_search.objectid) 197 return 1; 198 if (ref1->key_for_search.offset < ref2->key_for_search.offset) 199 return -1; 200 if (ref1->key_for_search.offset > ref2->key_for_search.offset) 201 return 1; 202 if (ref1->parent < ref2->parent) 203 return -1; 204 if (ref1->parent > ref2->parent) 205 return 1; 206 207 return 0; 208 } 209 210 /* 211 * Add @newref to the @root rbtree, merging identical refs. 212 * 213 * Callers should assumed that newref has been freed after calling. 214 */ 215 static void prelim_ref_insert(struct preftree *preftree, 216 struct prelim_ref *newref) 217 { 218 struct rb_root *root; 219 struct rb_node **p; 220 struct rb_node *parent = NULL; 221 struct prelim_ref *ref; 222 int result; 223 224 root = &preftree->root; 225 p = &root->rb_node; 226 227 while (*p) { 228 parent = *p; 229 ref = rb_entry(parent, struct prelim_ref, rbnode); 230 result = prelim_ref_compare(ref, newref); 231 if (result < 0) { 232 p = &(*p)->rb_left; 233 } else if (result > 0) { 234 p = &(*p)->rb_right; 235 } else { 236 /* Identical refs, merge them and free @newref */ 237 struct extent_inode_elem *eie = ref->inode_list; 238 239 while (eie && eie->next) 240 eie = eie->next; 241 242 if (!eie) 243 ref->inode_list = newref->inode_list; 244 else 245 eie->next = newref->inode_list; 246 ref->count += newref->count; 247 free_pref(newref); 248 return; 249 } 250 } 251 252 preftree->count++; 253 rb_link_node(&newref->rbnode, parent, p); 254 rb_insert_color(&newref->rbnode, root); 255 } 256 257 /* 258 * Release the entire tree. We don't care about internal consistency so 259 * just free everything and then reset the tree root. 260 */ 261 static void prelim_release(struct preftree *preftree) 262 { 263 struct prelim_ref *ref, *next_ref; 264 265 rbtree_postorder_for_each_entry_safe(ref, next_ref, &preftree->root, 266 rbnode) 267 free_pref(ref); 268 269 preftree->root = RB_ROOT; 270 preftree->count = 0; 271 } 272 273 /* 274 * the rules for all callers of this function are: 275 * - obtaining the parent is the goal 276 * - if you add a key, you must know that it is a correct key 277 * - if you cannot add the parent or a correct key, then we will look into the 278 * block later to set a correct key 279 * 280 * delayed refs 281 * ============ 282 * backref type | shared | indirect | shared | indirect 283 * information | tree | tree | data | data 284 * --------------------+--------+----------+--------+---------- 285 * parent logical | y | - | - | - 286 * key to resolve | - | y | y | y 287 * tree block logical | - | - | - | - 288 * root for resolving | y | y | y | y 289 * 290 * - column 1: we've the parent -> done 291 * - column 2, 3, 4: we use the key to find the parent 292 * 293 * on disk refs (inline or keyed) 294 * ============================== 295 * backref type | shared | indirect | shared | indirect 296 * information | tree | tree | data | data 297 * --------------------+--------+----------+--------+---------- 298 * parent logical | y | - | y | - 299 * key to resolve | - | - | - | y 300 * tree block logical | y | y | y | y 301 * root for resolving | - | y | y | y 302 * 303 * - column 1, 3: we've the parent -> done 304 * - column 2: we take the first key from the block to find the parent 305 * (see add_missing_keys) 306 * - column 4: we use the key to find the parent 307 * 308 * additional information that's available but not required to find the parent 309 * block might help in merging entries to gain some speed. 310 */ 311 static int add_prelim_ref(struct preftree *preftree, u64 root_id, 312 const struct btrfs_key *key, int level, u64 parent, 313 u64 wanted_disk_byte, int count, gfp_t gfp_mask) 314 { 315 struct prelim_ref *ref; 316 317 if (root_id == BTRFS_DATA_RELOC_TREE_OBJECTID) 318 return 0; 319 320 ref = kmem_cache_alloc(btrfs_prelim_ref_cache, gfp_mask); 321 if (!ref) 322 return -ENOMEM; 323 324 ref->root_id = root_id; 325 if (key) { 326 ref->key_for_search = *key; 327 /* 328 * We can often find data backrefs with an offset that is too 329 * large (>= LLONG_MAX, maximum allowed file offset) due to 330 * underflows when subtracting a file's offset with the data 331 * offset of its corresponding extent data item. This can 332 * happen for example in the clone ioctl. 333 * So if we detect such case we set the search key's offset to 334 * zero to make sure we will find the matching file extent item 335 * at add_all_parents(), otherwise we will miss it because the 336 * offset taken form the backref is much larger then the offset 337 * of the file extent item. This can make us scan a very large 338 * number of file extent items, but at least it will not make 339 * us miss any. 340 * This is an ugly workaround for a behaviour that should have 341 * never existed, but it does and a fix for the clone ioctl 342 * would touch a lot of places, cause backwards incompatibility 343 * and would not fix the problem for extents cloned with older 344 * kernels. 345 */ 346 if (ref->key_for_search.type == BTRFS_EXTENT_DATA_KEY && 347 ref->key_for_search.offset >= LLONG_MAX) 348 ref->key_for_search.offset = 0; 349 } else { 350 memset(&ref->key_for_search, 0, sizeof(ref->key_for_search)); 351 } 352 353 ref->inode_list = NULL; 354 ref->level = level; 355 ref->count = count; 356 ref->parent = parent; 357 ref->wanted_disk_byte = wanted_disk_byte; 358 prelim_ref_insert(preftree, ref); 359 360 return 0; 361 } 362 363 /* direct refs use root == 0, key == NULL */ 364 static int add_direct_ref(struct preftrees *preftrees, int level, u64 parent, 365 u64 wanted_disk_byte, int count, gfp_t gfp_mask) 366 { 367 return add_prelim_ref(&preftrees->direct, 0, NULL, level, parent, 368 wanted_disk_byte, count, gfp_mask); 369 } 370 371 /* indirect refs use parent == 0 */ 372 static int add_indirect_ref(struct preftrees *preftrees, u64 root_id, 373 const struct btrfs_key *key, int level, 374 u64 wanted_disk_byte, int count, gfp_t gfp_mask) 375 { 376 struct preftree *tree = &preftrees->indirect; 377 378 if (!key) 379 tree = &preftrees->indirect_missing_keys; 380 return add_prelim_ref(tree, root_id, key, level, 0, 381 wanted_disk_byte, count, gfp_mask); 382 } 383 384 static int add_all_parents(struct btrfs_root *root, struct btrfs_path *path, 385 struct ulist *parents, struct prelim_ref *ref, 386 int level, u64 time_seq, const u64 *extent_item_pos, 387 u64 total_refs) 388 { 389 int ret = 0; 390 int slot; 391 struct extent_buffer *eb; 392 struct btrfs_key key; 393 struct btrfs_key *key_for_search = &ref->key_for_search; 394 struct btrfs_file_extent_item *fi; 395 struct extent_inode_elem *eie = NULL, *old = NULL; 396 u64 disk_byte; 397 u64 wanted_disk_byte = ref->wanted_disk_byte; 398 u64 count = 0; 399 400 if (level != 0) { 401 eb = path->nodes[level]; 402 ret = ulist_add(parents, eb->start, 0, GFP_NOFS); 403 if (ret < 0) 404 return ret; 405 return 0; 406 } 407 408 /* 409 * We normally enter this function with the path already pointing to 410 * the first item to check. But sometimes, we may enter it with 411 * slot==nritems. In that case, go to the next leaf before we continue. 412 */ 413 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) { 414 if (time_seq == SEQ_LAST) 415 ret = btrfs_next_leaf(root, path); 416 else 417 ret = btrfs_next_old_leaf(root, path, time_seq); 418 } 419 420 while (!ret && count < total_refs) { 421 eb = path->nodes[0]; 422 slot = path->slots[0]; 423 424 btrfs_item_key_to_cpu(eb, &key, slot); 425 426 if (key.objectid != key_for_search->objectid || 427 key.type != BTRFS_EXTENT_DATA_KEY) 428 break; 429 430 fi = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item); 431 disk_byte = btrfs_file_extent_disk_bytenr(eb, fi); 432 433 if (disk_byte == wanted_disk_byte) { 434 eie = NULL; 435 old = NULL; 436 count++; 437 if (extent_item_pos) { 438 ret = check_extent_in_eb(&key, eb, fi, 439 *extent_item_pos, 440 &eie); 441 if (ret < 0) 442 break; 443 } 444 if (ret > 0) 445 goto next; 446 ret = ulist_add_merge_ptr(parents, eb->start, 447 eie, (void **)&old, GFP_NOFS); 448 if (ret < 0) 449 break; 450 if (!ret && extent_item_pos) { 451 while (old->next) 452 old = old->next; 453 old->next = eie; 454 } 455 eie = NULL; 456 } 457 next: 458 if (time_seq == SEQ_LAST) 459 ret = btrfs_next_item(root, path); 460 else 461 ret = btrfs_next_old_item(root, path, time_seq); 462 } 463 464 if (ret > 0) 465 ret = 0; 466 else if (ret < 0) 467 free_inode_elem_list(eie); 468 return ret; 469 } 470 471 /* 472 * resolve an indirect backref in the form (root_id, key, level) 473 * to a logical address 474 */ 475 static int resolve_indirect_ref(struct btrfs_fs_info *fs_info, 476 struct btrfs_path *path, u64 time_seq, 477 struct prelim_ref *ref, struct ulist *parents, 478 const u64 *extent_item_pos, u64 total_refs) 479 { 480 struct btrfs_root *root; 481 struct btrfs_key root_key; 482 struct extent_buffer *eb; 483 int ret = 0; 484 int root_level; 485 int level = ref->level; 486 int index; 487 488 root_key.objectid = ref->root_id; 489 root_key.type = BTRFS_ROOT_ITEM_KEY; 490 root_key.offset = (u64)-1; 491 492 index = srcu_read_lock(&fs_info->subvol_srcu); 493 494 root = btrfs_get_fs_root(fs_info, &root_key, false); 495 if (IS_ERR(root)) { 496 srcu_read_unlock(&fs_info->subvol_srcu, index); 497 ret = PTR_ERR(root); 498 goto out; 499 } 500 501 if (btrfs_is_testing(fs_info)) { 502 srcu_read_unlock(&fs_info->subvol_srcu, index); 503 ret = -ENOENT; 504 goto out; 505 } 506 507 if (path->search_commit_root) 508 root_level = btrfs_header_level(root->commit_root); 509 else if (time_seq == SEQ_LAST) 510 root_level = btrfs_header_level(root->node); 511 else 512 root_level = btrfs_old_root_level(root, time_seq); 513 514 if (root_level + 1 == level) { 515 srcu_read_unlock(&fs_info->subvol_srcu, index); 516 goto out; 517 } 518 519 path->lowest_level = level; 520 if (time_seq == SEQ_LAST) 521 ret = btrfs_search_slot(NULL, root, &ref->key_for_search, path, 522 0, 0); 523 else 524 ret = btrfs_search_old_slot(root, &ref->key_for_search, path, 525 time_seq); 526 527 /* root node has been locked, we can release @subvol_srcu safely here */ 528 srcu_read_unlock(&fs_info->subvol_srcu, index); 529 530 btrfs_debug(fs_info, 531 "search slot in root %llu (level %d, ref count %d) returned %d for key (%llu %u %llu)", 532 ref->root_id, level, ref->count, ret, 533 ref->key_for_search.objectid, ref->key_for_search.type, 534 ref->key_for_search.offset); 535 if (ret < 0) 536 goto out; 537 538 eb = path->nodes[level]; 539 while (!eb) { 540 if (WARN_ON(!level)) { 541 ret = 1; 542 goto out; 543 } 544 level--; 545 eb = path->nodes[level]; 546 } 547 548 ret = add_all_parents(root, path, parents, ref, level, time_seq, 549 extent_item_pos, total_refs); 550 out: 551 path->lowest_level = 0; 552 btrfs_release_path(path); 553 return ret; 554 } 555 556 static struct extent_inode_elem * 557 unode_aux_to_inode_list(struct ulist_node *node) 558 { 559 if (!node) 560 return NULL; 561 return (struct extent_inode_elem *)(uintptr_t)node->aux; 562 } 563 564 /* 565 * We maintain three seperate rbtrees: one for direct refs, one for 566 * indirect refs which have a key, and one for indirect refs which do not 567 * have a key. Each tree does merge on insertion. 568 * 569 * Once all of the references are located, we iterate over the tree of 570 * indirect refs with missing keys. An appropriate key is located and 571 * the ref is moved onto the tree for indirect refs. After all missing 572 * keys are thus located, we iterate over the indirect ref tree, resolve 573 * each reference, and then insert the resolved reference onto the 574 * direct tree (merging there too). 575 * 576 * New backrefs (i.e., for parent nodes) are added to the appropriate 577 * rbtree as they are encountered. The new backrefs are subsequently 578 * resolved as above. 579 */ 580 static int resolve_indirect_refs(struct btrfs_fs_info *fs_info, 581 struct btrfs_path *path, u64 time_seq, 582 struct preftrees *preftrees, 583 const u64 *extent_item_pos, u64 total_refs, 584 u64 root_objectid) 585 { 586 int err; 587 int ret = 0; 588 struct ulist *parents; 589 struct ulist_node *node; 590 struct ulist_iterator uiter; 591 struct rb_node *rnode; 592 593 parents = ulist_alloc(GFP_NOFS); 594 if (!parents) 595 return -ENOMEM; 596 597 /* 598 * We could trade memory usage for performance here by iterating 599 * the tree, allocating new refs for each insertion, and then 600 * freeing the entire indirect tree when we're done. In some test 601 * cases, the tree can grow quite large (~200k objects). 602 */ 603 while ((rnode = rb_first(&preftrees->indirect.root))) { 604 struct prelim_ref *ref; 605 606 ref = rb_entry(rnode, struct prelim_ref, rbnode); 607 if (WARN(ref->parent, 608 "BUG: direct ref found in indirect tree")) { 609 ret = -EINVAL; 610 goto out; 611 } 612 613 rb_erase(&ref->rbnode, &preftrees->indirect.root); 614 preftrees->indirect.count--; 615 616 if (ref->count == 0) { 617 free_pref(ref); 618 continue; 619 } 620 621 if (root_objectid && ref->root_id != root_objectid) { 622 free_pref(ref); 623 ret = BACKREF_FOUND_SHARED; 624 goto out; 625 } 626 err = resolve_indirect_ref(fs_info, path, time_seq, ref, 627 parents, extent_item_pos, 628 total_refs); 629 /* 630 * we can only tolerate ENOENT,otherwise,we should catch error 631 * and return directly. 632 */ 633 if (err == -ENOENT) { 634 prelim_ref_insert(&preftrees->direct, ref); 635 continue; 636 } else if (err) { 637 free_pref(ref); 638 ret = err; 639 goto out; 640 } 641 642 /* we put the first parent into the ref at hand */ 643 ULIST_ITER_INIT(&uiter); 644 node = ulist_next(parents, &uiter); 645 ref->parent = node ? node->val : 0; 646 ref->inode_list = unode_aux_to_inode_list(node); 647 648 /* Add a prelim_ref(s) for any other parent(s). */ 649 while ((node = ulist_next(parents, &uiter))) { 650 struct prelim_ref *new_ref; 651 652 new_ref = kmem_cache_alloc(btrfs_prelim_ref_cache, 653 GFP_NOFS); 654 if (!new_ref) { 655 free_pref(ref); 656 ret = -ENOMEM; 657 goto out; 658 } 659 memcpy(new_ref, ref, sizeof(*ref)); 660 new_ref->parent = node->val; 661 new_ref->inode_list = unode_aux_to_inode_list(node); 662 prelim_ref_insert(&preftrees->direct, new_ref); 663 } 664 665 /* Now it's a direct ref, put it in the the direct tree */ 666 prelim_ref_insert(&preftrees->direct, ref); 667 668 ulist_reinit(parents); 669 } 670 out: 671 ulist_free(parents); 672 return ret; 673 } 674 675 /* 676 * read tree blocks and add keys where required. 677 */ 678 static int add_missing_keys(struct btrfs_fs_info *fs_info, 679 struct preftrees *preftrees) 680 { 681 struct prelim_ref *ref; 682 struct extent_buffer *eb; 683 struct preftree *tree = &preftrees->indirect_missing_keys; 684 struct rb_node *node; 685 686 while ((node = rb_first(&tree->root))) { 687 ref = rb_entry(node, struct prelim_ref, rbnode); 688 rb_erase(node, &tree->root); 689 690 BUG_ON(ref->parent); /* should not be a direct ref */ 691 BUG_ON(ref->key_for_search.type); 692 BUG_ON(!ref->wanted_disk_byte); 693 694 eb = read_tree_block(fs_info, ref->wanted_disk_byte, 0); 695 if (IS_ERR(eb)) { 696 free_pref(ref); 697 return PTR_ERR(eb); 698 } else if (!extent_buffer_uptodate(eb)) { 699 free_pref(ref); 700 free_extent_buffer(eb); 701 return -EIO; 702 } 703 btrfs_tree_read_lock(eb); 704 if (btrfs_header_level(eb) == 0) 705 btrfs_item_key_to_cpu(eb, &ref->key_for_search, 0); 706 else 707 btrfs_node_key_to_cpu(eb, &ref->key_for_search, 0); 708 btrfs_tree_read_unlock(eb); 709 free_extent_buffer(eb); 710 prelim_ref_insert(&preftrees->indirect, ref); 711 } 712 return 0; 713 } 714 715 /* 716 * add all currently queued delayed refs from this head whose seq nr is 717 * smaller or equal that seq to the list 718 */ 719 static int add_delayed_refs(struct btrfs_delayed_ref_head *head, u64 seq, 720 struct preftrees *preftrees, u64 *total_refs, 721 u64 inum) 722 { 723 struct btrfs_delayed_ref_node *node; 724 struct btrfs_delayed_extent_op *extent_op = head->extent_op; 725 struct btrfs_key key; 726 struct btrfs_key tmp_op_key; 727 struct btrfs_key *op_key = NULL; 728 int sgn; 729 int ret = 0; 730 731 if (extent_op && extent_op->update_key) { 732 btrfs_disk_key_to_cpu(&tmp_op_key, &extent_op->key); 733 op_key = &tmp_op_key; 734 } 735 736 spin_lock(&head->lock); 737 list_for_each_entry(node, &head->ref_list, list) { 738 if (node->seq > seq) 739 continue; 740 741 switch (node->action) { 742 case BTRFS_ADD_DELAYED_EXTENT: 743 case BTRFS_UPDATE_DELAYED_HEAD: 744 WARN_ON(1); 745 continue; 746 case BTRFS_ADD_DELAYED_REF: 747 sgn = 1; 748 break; 749 case BTRFS_DROP_DELAYED_REF: 750 sgn = -1; 751 break; 752 default: 753 BUG_ON(1); 754 } 755 *total_refs += (node->ref_mod * sgn); 756 switch (node->type) { 757 case BTRFS_TREE_BLOCK_REF_KEY: { 758 /* NORMAL INDIRECT METADATA backref */ 759 struct btrfs_delayed_tree_ref *ref; 760 761 ref = btrfs_delayed_node_to_tree_ref(node); 762 ret = add_indirect_ref(preftrees, ref->root, &tmp_op_key, 763 ref->level + 1, node->bytenr, 764 node->ref_mod * sgn, 765 GFP_ATOMIC); 766 break; 767 } 768 case BTRFS_SHARED_BLOCK_REF_KEY: { 769 /* SHARED DIRECT METADATA backref */ 770 struct btrfs_delayed_tree_ref *ref; 771 772 ref = btrfs_delayed_node_to_tree_ref(node); 773 774 ret = add_direct_ref(preftrees, ref->level + 1, 775 ref->parent, node->bytenr, 776 node->ref_mod * sgn, 777 GFP_ATOMIC); 778 break; 779 } 780 case BTRFS_EXTENT_DATA_REF_KEY: { 781 /* NORMAL INDIRECT DATA backref */ 782 struct btrfs_delayed_data_ref *ref; 783 ref = btrfs_delayed_node_to_data_ref(node); 784 785 key.objectid = ref->objectid; 786 key.type = BTRFS_EXTENT_DATA_KEY; 787 key.offset = ref->offset; 788 789 /* 790 * Found a inum that doesn't match our known inum, we 791 * know it's shared. 792 */ 793 if (inum && ref->objectid != inum) { 794 ret = BACKREF_FOUND_SHARED; 795 break; 796 } 797 798 ret = add_indirect_ref(preftrees, ref->root, &key, 0, 799 node->bytenr, 800 node->ref_mod * sgn, 801 GFP_ATOMIC); 802 break; 803 } 804 case BTRFS_SHARED_DATA_REF_KEY: { 805 /* SHARED DIRECT FULL backref */ 806 struct btrfs_delayed_data_ref *ref; 807 808 ref = btrfs_delayed_node_to_data_ref(node); 809 810 ret = add_direct_ref(preftrees, 0, ref->parent, 811 node->bytenr, 812 node->ref_mod * sgn, 813 GFP_ATOMIC); 814 break; 815 } 816 default: 817 WARN_ON(1); 818 } 819 if (ret) 820 break; 821 } 822 spin_unlock(&head->lock); 823 return ret; 824 } 825 826 /* 827 * add all inline backrefs for bytenr to the list 828 */ 829 static int add_inline_refs(struct btrfs_path *path, u64 bytenr, 830 int *info_level, struct preftrees *preftrees, 831 u64 *total_refs, u64 inum) 832 { 833 int ret = 0; 834 int slot; 835 struct extent_buffer *leaf; 836 struct btrfs_key key; 837 struct btrfs_key found_key; 838 unsigned long ptr; 839 unsigned long end; 840 struct btrfs_extent_item *ei; 841 u64 flags; 842 u64 item_size; 843 844 /* 845 * enumerate all inline refs 846 */ 847 leaf = path->nodes[0]; 848 slot = path->slots[0]; 849 850 item_size = btrfs_item_size_nr(leaf, slot); 851 BUG_ON(item_size < sizeof(*ei)); 852 853 ei = btrfs_item_ptr(leaf, slot, struct btrfs_extent_item); 854 flags = btrfs_extent_flags(leaf, ei); 855 *total_refs += btrfs_extent_refs(leaf, ei); 856 btrfs_item_key_to_cpu(leaf, &found_key, slot); 857 858 ptr = (unsigned long)(ei + 1); 859 end = (unsigned long)ei + item_size; 860 861 if (found_key.type == BTRFS_EXTENT_ITEM_KEY && 862 flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) { 863 struct btrfs_tree_block_info *info; 864 865 info = (struct btrfs_tree_block_info *)ptr; 866 *info_level = btrfs_tree_block_level(leaf, info); 867 ptr += sizeof(struct btrfs_tree_block_info); 868 BUG_ON(ptr > end); 869 } else if (found_key.type == BTRFS_METADATA_ITEM_KEY) { 870 *info_level = found_key.offset; 871 } else { 872 BUG_ON(!(flags & BTRFS_EXTENT_FLAG_DATA)); 873 } 874 875 while (ptr < end) { 876 struct btrfs_extent_inline_ref *iref; 877 u64 offset; 878 int type; 879 880 iref = (struct btrfs_extent_inline_ref *)ptr; 881 type = btrfs_extent_inline_ref_type(leaf, iref); 882 offset = btrfs_extent_inline_ref_offset(leaf, iref); 883 884 switch (type) { 885 case BTRFS_SHARED_BLOCK_REF_KEY: 886 ret = add_direct_ref(preftrees, *info_level + 1, offset, 887 bytenr, 1, GFP_NOFS); 888 break; 889 case BTRFS_SHARED_DATA_REF_KEY: { 890 struct btrfs_shared_data_ref *sdref; 891 int count; 892 893 sdref = (struct btrfs_shared_data_ref *)(iref + 1); 894 count = btrfs_shared_data_ref_count(leaf, sdref); 895 896 ret = add_direct_ref(preftrees, 0, offset, 897 bytenr, count, GFP_NOFS); 898 break; 899 } 900 case BTRFS_TREE_BLOCK_REF_KEY: 901 ret = add_indirect_ref(preftrees, offset, NULL, 902 *info_level + 1, bytenr, 1, 903 GFP_NOFS); 904 break; 905 case BTRFS_EXTENT_DATA_REF_KEY: { 906 struct btrfs_extent_data_ref *dref; 907 int count; 908 u64 root; 909 910 dref = (struct btrfs_extent_data_ref *)(&iref->offset); 911 count = btrfs_extent_data_ref_count(leaf, dref); 912 key.objectid = btrfs_extent_data_ref_objectid(leaf, 913 dref); 914 key.type = BTRFS_EXTENT_DATA_KEY; 915 key.offset = btrfs_extent_data_ref_offset(leaf, dref); 916 917 if (inum && key.objectid != inum) { 918 ret = BACKREF_FOUND_SHARED; 919 break; 920 } 921 922 root = btrfs_extent_data_ref_root(leaf, dref); 923 924 ret = add_indirect_ref(preftrees, root, &key, 0, bytenr, 925 count, GFP_NOFS); 926 break; 927 } 928 default: 929 WARN_ON(1); 930 } 931 if (ret) 932 return ret; 933 ptr += btrfs_extent_inline_ref_size(type); 934 } 935 936 return 0; 937 } 938 939 /* 940 * add all non-inline backrefs for bytenr to the list 941 */ 942 static int add_keyed_refs(struct btrfs_fs_info *fs_info, 943 struct btrfs_path *path, u64 bytenr, 944 int info_level, struct preftrees *preftrees, 945 u64 inum) 946 { 947 struct btrfs_root *extent_root = fs_info->extent_root; 948 int ret; 949 int slot; 950 struct extent_buffer *leaf; 951 struct btrfs_key key; 952 953 while (1) { 954 ret = btrfs_next_item(extent_root, path); 955 if (ret < 0) 956 break; 957 if (ret) { 958 ret = 0; 959 break; 960 } 961 962 slot = path->slots[0]; 963 leaf = path->nodes[0]; 964 btrfs_item_key_to_cpu(leaf, &key, slot); 965 966 if (key.objectid != bytenr) 967 break; 968 if (key.type < BTRFS_TREE_BLOCK_REF_KEY) 969 continue; 970 if (key.type > BTRFS_SHARED_DATA_REF_KEY) 971 break; 972 973 switch (key.type) { 974 case BTRFS_SHARED_BLOCK_REF_KEY: 975 /* SHARED DIRECT METADATA backref */ 976 ret = add_direct_ref(preftrees, info_level + 1, 977 key.offset, bytenr, 1, 978 GFP_NOFS); 979 break; 980 case BTRFS_SHARED_DATA_REF_KEY: { 981 /* SHARED DIRECT FULL backref */ 982 struct btrfs_shared_data_ref *sdref; 983 int count; 984 985 sdref = btrfs_item_ptr(leaf, slot, 986 struct btrfs_shared_data_ref); 987 count = btrfs_shared_data_ref_count(leaf, sdref); 988 ret = add_direct_ref(preftrees, 0, key.offset, bytenr, 989 count, GFP_NOFS); 990 break; 991 } 992 case BTRFS_TREE_BLOCK_REF_KEY: 993 /* NORMAL INDIRECT METADATA backref */ 994 ret = add_indirect_ref(preftrees, key.offset, NULL, 995 info_level + 1, bytenr, 1, 996 GFP_NOFS); 997 break; 998 case BTRFS_EXTENT_DATA_REF_KEY: { 999 /* NORMAL INDIRECT DATA backref */ 1000 struct btrfs_extent_data_ref *dref; 1001 int count; 1002 u64 root; 1003 1004 dref = btrfs_item_ptr(leaf, slot, 1005 struct btrfs_extent_data_ref); 1006 count = btrfs_extent_data_ref_count(leaf, dref); 1007 key.objectid = btrfs_extent_data_ref_objectid(leaf, 1008 dref); 1009 key.type = BTRFS_EXTENT_DATA_KEY; 1010 key.offset = btrfs_extent_data_ref_offset(leaf, dref); 1011 1012 if (inum && key.objectid != inum) { 1013 ret = BACKREF_FOUND_SHARED; 1014 break; 1015 } 1016 1017 root = btrfs_extent_data_ref_root(leaf, dref); 1018 ret = add_indirect_ref(preftrees, root, &key, 0, bytenr, 1019 count, GFP_NOFS); 1020 break; 1021 } 1022 default: 1023 WARN_ON(1); 1024 } 1025 if (ret) 1026 return ret; 1027 1028 } 1029 1030 return ret; 1031 } 1032 1033 /* 1034 * this adds all existing backrefs (inline backrefs, backrefs and delayed 1035 * refs) for the given bytenr to the refs list, merges duplicates and resolves 1036 * indirect refs to their parent bytenr. 1037 * When roots are found, they're added to the roots list 1038 * 1039 * NOTE: This can return values > 0 1040 * 1041 * If time_seq is set to SEQ_LAST, it will not search delayed_refs, and behave 1042 * much like trans == NULL case, the difference only lies in it will not 1043 * commit root. 1044 * The special case is for qgroup to search roots in commit_transaction(). 1045 * 1046 * FIXME some caching might speed things up 1047 */ 1048 static int find_parent_nodes(struct btrfs_trans_handle *trans, 1049 struct btrfs_fs_info *fs_info, u64 bytenr, 1050 u64 time_seq, struct ulist *refs, 1051 struct ulist *roots, const u64 *extent_item_pos, 1052 u64 root_objectid, u64 inum) 1053 { 1054 struct btrfs_key key; 1055 struct btrfs_path *path; 1056 struct btrfs_delayed_ref_root *delayed_refs = NULL; 1057 struct btrfs_delayed_ref_head *head; 1058 int info_level = 0; 1059 int ret; 1060 struct prelim_ref *ref; 1061 struct rb_node *node; 1062 struct extent_inode_elem *eie = NULL; 1063 /* total of both direct AND indirect refs! */ 1064 u64 total_refs = 0; 1065 struct preftrees preftrees = { 1066 .direct = PREFTREE_INIT, 1067 .indirect = PREFTREE_INIT, 1068 .indirect_missing_keys = PREFTREE_INIT 1069 }; 1070 1071 key.objectid = bytenr; 1072 key.offset = (u64)-1; 1073 if (btrfs_fs_incompat(fs_info, SKINNY_METADATA)) 1074 key.type = BTRFS_METADATA_ITEM_KEY; 1075 else 1076 key.type = BTRFS_EXTENT_ITEM_KEY; 1077 1078 path = btrfs_alloc_path(); 1079 if (!path) 1080 return -ENOMEM; 1081 if (!trans) { 1082 path->search_commit_root = 1; 1083 path->skip_locking = 1; 1084 } 1085 1086 if (time_seq == SEQ_LAST) 1087 path->skip_locking = 1; 1088 1089 /* 1090 * grab both a lock on the path and a lock on the delayed ref head. 1091 * We need both to get a consistent picture of how the refs look 1092 * at a specified point in time 1093 */ 1094 again: 1095 head = NULL; 1096 1097 ret = btrfs_search_slot(trans, fs_info->extent_root, &key, path, 0, 0); 1098 if (ret < 0) 1099 goto out; 1100 BUG_ON(ret == 0); 1101 1102 #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS 1103 if (trans && likely(trans->type != __TRANS_DUMMY) && 1104 time_seq != SEQ_LAST) { 1105 #else 1106 if (trans && time_seq != SEQ_LAST) { 1107 #endif 1108 /* 1109 * look if there are updates for this ref queued and lock the 1110 * head 1111 */ 1112 delayed_refs = &trans->transaction->delayed_refs; 1113 spin_lock(&delayed_refs->lock); 1114 head = btrfs_find_delayed_ref_head(delayed_refs, bytenr); 1115 if (head) { 1116 if (!mutex_trylock(&head->mutex)) { 1117 refcount_inc(&head->node.refs); 1118 spin_unlock(&delayed_refs->lock); 1119 1120 btrfs_release_path(path); 1121 1122 /* 1123 * Mutex was contended, block until it's 1124 * released and try again 1125 */ 1126 mutex_lock(&head->mutex); 1127 mutex_unlock(&head->mutex); 1128 btrfs_put_delayed_ref(&head->node); 1129 goto again; 1130 } 1131 spin_unlock(&delayed_refs->lock); 1132 ret = add_delayed_refs(head, time_seq, &preftrees, 1133 &total_refs, inum); 1134 mutex_unlock(&head->mutex); 1135 if (ret) 1136 goto out; 1137 } else { 1138 spin_unlock(&delayed_refs->lock); 1139 } 1140 } 1141 1142 if (path->slots[0]) { 1143 struct extent_buffer *leaf; 1144 int slot; 1145 1146 path->slots[0]--; 1147 leaf = path->nodes[0]; 1148 slot = path->slots[0]; 1149 btrfs_item_key_to_cpu(leaf, &key, slot); 1150 if (key.objectid == bytenr && 1151 (key.type == BTRFS_EXTENT_ITEM_KEY || 1152 key.type == BTRFS_METADATA_ITEM_KEY)) { 1153 ret = add_inline_refs(path, bytenr, &info_level, 1154 &preftrees, &total_refs, inum); 1155 if (ret) 1156 goto out; 1157 ret = add_keyed_refs(fs_info, path, bytenr, info_level, 1158 &preftrees, inum); 1159 if (ret) 1160 goto out; 1161 } 1162 } 1163 1164 btrfs_release_path(path); 1165 1166 ret = add_missing_keys(fs_info, &preftrees); 1167 if (ret) 1168 goto out; 1169 1170 WARN_ON(!RB_EMPTY_ROOT(&preftrees.indirect_missing_keys.root)); 1171 1172 ret = resolve_indirect_refs(fs_info, path, time_seq, &preftrees, 1173 extent_item_pos, total_refs, 1174 root_objectid); 1175 if (ret) 1176 goto out; 1177 1178 WARN_ON(!RB_EMPTY_ROOT(&preftrees.indirect.root)); 1179 1180 /* 1181 * This walks the tree of merged and resolved refs. Tree blocks are 1182 * read in as needed. Unique entries are added to the ulist, and 1183 * the list of found roots is updated. 1184 * 1185 * We release the entire tree in one go before returning. 1186 */ 1187 node = rb_first(&preftrees.direct.root); 1188 while (node) { 1189 ref = rb_entry(node, struct prelim_ref, rbnode); 1190 node = rb_next(&ref->rbnode); 1191 WARN_ON(ref->count < 0); 1192 if (roots && ref->count && ref->root_id && ref->parent == 0) { 1193 if (root_objectid && ref->root_id != root_objectid) { 1194 ret = BACKREF_FOUND_SHARED; 1195 goto out; 1196 } 1197 1198 /* no parent == root of tree */ 1199 ret = ulist_add(roots, ref->root_id, 0, GFP_NOFS); 1200 if (ret < 0) 1201 goto out; 1202 } 1203 if (ref->count && ref->parent) { 1204 if (extent_item_pos && !ref->inode_list && 1205 ref->level == 0) { 1206 struct extent_buffer *eb; 1207 1208 eb = read_tree_block(fs_info, ref->parent, 0); 1209 if (IS_ERR(eb)) { 1210 ret = PTR_ERR(eb); 1211 goto out; 1212 } else if (!extent_buffer_uptodate(eb)) { 1213 free_extent_buffer(eb); 1214 ret = -EIO; 1215 goto out; 1216 } 1217 btrfs_tree_read_lock(eb); 1218 btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK); 1219 ret = find_extent_in_eb(eb, bytenr, 1220 *extent_item_pos, &eie); 1221 btrfs_tree_read_unlock_blocking(eb); 1222 free_extent_buffer(eb); 1223 if (ret < 0) 1224 goto out; 1225 ref->inode_list = eie; 1226 } 1227 ret = ulist_add_merge_ptr(refs, ref->parent, 1228 ref->inode_list, 1229 (void **)&eie, GFP_NOFS); 1230 if (ret < 0) 1231 goto out; 1232 if (!ret && extent_item_pos) { 1233 /* 1234 * we've recorded that parent, so we must extend 1235 * its inode list here 1236 */ 1237 BUG_ON(!eie); 1238 while (eie->next) 1239 eie = eie->next; 1240 eie->next = ref->inode_list; 1241 } 1242 eie = NULL; 1243 } 1244 } 1245 1246 out: 1247 btrfs_free_path(path); 1248 1249 prelim_release(&preftrees.direct); 1250 prelim_release(&preftrees.indirect); 1251 prelim_release(&preftrees.indirect_missing_keys); 1252 1253 if (ret < 0) 1254 free_inode_elem_list(eie); 1255 return ret; 1256 } 1257 1258 static void free_leaf_list(struct ulist *blocks) 1259 { 1260 struct ulist_node *node = NULL; 1261 struct extent_inode_elem *eie; 1262 struct ulist_iterator uiter; 1263 1264 ULIST_ITER_INIT(&uiter); 1265 while ((node = ulist_next(blocks, &uiter))) { 1266 if (!node->aux) 1267 continue; 1268 eie = unode_aux_to_inode_list(node); 1269 free_inode_elem_list(eie); 1270 node->aux = 0; 1271 } 1272 1273 ulist_free(blocks); 1274 } 1275 1276 /* 1277 * Finds all leafs with a reference to the specified combination of bytenr and 1278 * offset. key_list_head will point to a list of corresponding keys (caller must 1279 * free each list element). The leafs will be stored in the leafs ulist, which 1280 * must be freed with ulist_free. 1281 * 1282 * returns 0 on success, <0 on error 1283 */ 1284 static int btrfs_find_all_leafs(struct btrfs_trans_handle *trans, 1285 struct btrfs_fs_info *fs_info, u64 bytenr, 1286 u64 time_seq, struct ulist **leafs, 1287 const u64 *extent_item_pos) 1288 { 1289 int ret; 1290 1291 *leafs = ulist_alloc(GFP_NOFS); 1292 if (!*leafs) 1293 return -ENOMEM; 1294 1295 ret = find_parent_nodes(trans, fs_info, bytenr, time_seq, 1296 *leafs, NULL, extent_item_pos, 0, 0); 1297 if (ret < 0 && ret != -ENOENT) { 1298 free_leaf_list(*leafs); 1299 return ret; 1300 } 1301 1302 return 0; 1303 } 1304 1305 /* 1306 * walk all backrefs for a given extent to find all roots that reference this 1307 * extent. Walking a backref means finding all extents that reference this 1308 * extent and in turn walk the backrefs of those, too. Naturally this is a 1309 * recursive process, but here it is implemented in an iterative fashion: We 1310 * find all referencing extents for the extent in question and put them on a 1311 * list. In turn, we find all referencing extents for those, further appending 1312 * to the list. The way we iterate the list allows adding more elements after 1313 * the current while iterating. The process stops when we reach the end of the 1314 * list. Found roots are added to the roots list. 1315 * 1316 * returns 0 on success, < 0 on error. 1317 */ 1318 static int btrfs_find_all_roots_safe(struct btrfs_trans_handle *trans, 1319 struct btrfs_fs_info *fs_info, u64 bytenr, 1320 u64 time_seq, struct ulist **roots) 1321 { 1322 struct ulist *tmp; 1323 struct ulist_node *node = NULL; 1324 struct ulist_iterator uiter; 1325 int ret; 1326 1327 tmp = ulist_alloc(GFP_NOFS); 1328 if (!tmp) 1329 return -ENOMEM; 1330 *roots = ulist_alloc(GFP_NOFS); 1331 if (!*roots) { 1332 ulist_free(tmp); 1333 return -ENOMEM; 1334 } 1335 1336 ULIST_ITER_INIT(&uiter); 1337 while (1) { 1338 ret = find_parent_nodes(trans, fs_info, bytenr, time_seq, 1339 tmp, *roots, NULL, 0, 0); 1340 if (ret < 0 && ret != -ENOENT) { 1341 ulist_free(tmp); 1342 ulist_free(*roots); 1343 return ret; 1344 } 1345 node = ulist_next(tmp, &uiter); 1346 if (!node) 1347 break; 1348 bytenr = node->val; 1349 cond_resched(); 1350 } 1351 1352 ulist_free(tmp); 1353 return 0; 1354 } 1355 1356 int btrfs_find_all_roots(struct btrfs_trans_handle *trans, 1357 struct btrfs_fs_info *fs_info, u64 bytenr, 1358 u64 time_seq, struct ulist **roots) 1359 { 1360 int ret; 1361 1362 if (!trans) 1363 down_read(&fs_info->commit_root_sem); 1364 ret = btrfs_find_all_roots_safe(trans, fs_info, bytenr, 1365 time_seq, roots); 1366 if (!trans) 1367 up_read(&fs_info->commit_root_sem); 1368 return ret; 1369 } 1370 1371 /** 1372 * btrfs_check_shared - tell us whether an extent is shared 1373 * 1374 * btrfs_check_shared uses the backref walking code but will short 1375 * circuit as soon as it finds a root or inode that doesn't match the 1376 * one passed in. This provides a significant performance benefit for 1377 * callers (such as fiemap) which want to know whether the extent is 1378 * shared but do not need a ref count. 1379 * 1380 * This attempts to allocate a transaction in order to account for 1381 * delayed refs, but continues on even when the alloc fails. 1382 * 1383 * Return: 0 if extent is not shared, 1 if it is shared, < 0 on error. 1384 */ 1385 int btrfs_check_shared(struct btrfs_root *root, u64 inum, u64 bytenr) 1386 { 1387 struct btrfs_fs_info *fs_info = root->fs_info; 1388 struct btrfs_trans_handle *trans; 1389 struct ulist *tmp = NULL; 1390 struct ulist *roots = NULL; 1391 struct ulist_iterator uiter; 1392 struct ulist_node *node; 1393 struct seq_list elem = SEQ_LIST_INIT(elem); 1394 int ret = 0; 1395 1396 tmp = ulist_alloc(GFP_NOFS); 1397 roots = ulist_alloc(GFP_NOFS); 1398 if (!tmp || !roots) { 1399 ulist_free(tmp); 1400 ulist_free(roots); 1401 return -ENOMEM; 1402 } 1403 1404 trans = btrfs_join_transaction(root); 1405 if (IS_ERR(trans)) { 1406 trans = NULL; 1407 down_read(&fs_info->commit_root_sem); 1408 } else { 1409 btrfs_get_tree_mod_seq(fs_info, &elem); 1410 } 1411 1412 ULIST_ITER_INIT(&uiter); 1413 while (1) { 1414 ret = find_parent_nodes(trans, fs_info, bytenr, elem.seq, tmp, 1415 roots, NULL, root->objectid, inum); 1416 if (ret == BACKREF_FOUND_SHARED) { 1417 /* this is the only condition under which we return 1 */ 1418 ret = 1; 1419 break; 1420 } 1421 if (ret < 0 && ret != -ENOENT) 1422 break; 1423 ret = 0; 1424 node = ulist_next(tmp, &uiter); 1425 if (!node) 1426 break; 1427 bytenr = node->val; 1428 cond_resched(); 1429 } 1430 1431 if (trans) { 1432 btrfs_put_tree_mod_seq(fs_info, &elem); 1433 btrfs_end_transaction(trans); 1434 } else { 1435 up_read(&fs_info->commit_root_sem); 1436 } 1437 ulist_free(tmp); 1438 ulist_free(roots); 1439 return ret; 1440 } 1441 1442 int btrfs_find_one_extref(struct btrfs_root *root, u64 inode_objectid, 1443 u64 start_off, struct btrfs_path *path, 1444 struct btrfs_inode_extref **ret_extref, 1445 u64 *found_off) 1446 { 1447 int ret, slot; 1448 struct btrfs_key key; 1449 struct btrfs_key found_key; 1450 struct btrfs_inode_extref *extref; 1451 const struct extent_buffer *leaf; 1452 unsigned long ptr; 1453 1454 key.objectid = inode_objectid; 1455 key.type = BTRFS_INODE_EXTREF_KEY; 1456 key.offset = start_off; 1457 1458 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); 1459 if (ret < 0) 1460 return ret; 1461 1462 while (1) { 1463 leaf = path->nodes[0]; 1464 slot = path->slots[0]; 1465 if (slot >= btrfs_header_nritems(leaf)) { 1466 /* 1467 * If the item at offset is not found, 1468 * btrfs_search_slot will point us to the slot 1469 * where it should be inserted. In our case 1470 * that will be the slot directly before the 1471 * next INODE_REF_KEY_V2 item. In the case 1472 * that we're pointing to the last slot in a 1473 * leaf, we must move one leaf over. 1474 */ 1475 ret = btrfs_next_leaf(root, path); 1476 if (ret) { 1477 if (ret >= 1) 1478 ret = -ENOENT; 1479 break; 1480 } 1481 continue; 1482 } 1483 1484 btrfs_item_key_to_cpu(leaf, &found_key, slot); 1485 1486 /* 1487 * Check that we're still looking at an extended ref key for 1488 * this particular objectid. If we have different 1489 * objectid or type then there are no more to be found 1490 * in the tree and we can exit. 1491 */ 1492 ret = -ENOENT; 1493 if (found_key.objectid != inode_objectid) 1494 break; 1495 if (found_key.type != BTRFS_INODE_EXTREF_KEY) 1496 break; 1497 1498 ret = 0; 1499 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]); 1500 extref = (struct btrfs_inode_extref *)ptr; 1501 *ret_extref = extref; 1502 if (found_off) 1503 *found_off = found_key.offset; 1504 break; 1505 } 1506 1507 return ret; 1508 } 1509 1510 /* 1511 * this iterates to turn a name (from iref/extref) into a full filesystem path. 1512 * Elements of the path are separated by '/' and the path is guaranteed to be 1513 * 0-terminated. the path is only given within the current file system. 1514 * Therefore, it never starts with a '/'. the caller is responsible to provide 1515 * "size" bytes in "dest". the dest buffer will be filled backwards. finally, 1516 * the start point of the resulting string is returned. this pointer is within 1517 * dest, normally. 1518 * in case the path buffer would overflow, the pointer is decremented further 1519 * as if output was written to the buffer, though no more output is actually 1520 * generated. that way, the caller can determine how much space would be 1521 * required for the path to fit into the buffer. in that case, the returned 1522 * value will be smaller than dest. callers must check this! 1523 */ 1524 char *btrfs_ref_to_path(struct btrfs_root *fs_root, struct btrfs_path *path, 1525 u32 name_len, unsigned long name_off, 1526 struct extent_buffer *eb_in, u64 parent, 1527 char *dest, u32 size) 1528 { 1529 int slot; 1530 u64 next_inum; 1531 int ret; 1532 s64 bytes_left = ((s64)size) - 1; 1533 struct extent_buffer *eb = eb_in; 1534 struct btrfs_key found_key; 1535 int leave_spinning = path->leave_spinning; 1536 struct btrfs_inode_ref *iref; 1537 1538 if (bytes_left >= 0) 1539 dest[bytes_left] = '\0'; 1540 1541 path->leave_spinning = 1; 1542 while (1) { 1543 bytes_left -= name_len; 1544 if (bytes_left >= 0) 1545 read_extent_buffer(eb, dest + bytes_left, 1546 name_off, name_len); 1547 if (eb != eb_in) { 1548 if (!path->skip_locking) 1549 btrfs_tree_read_unlock_blocking(eb); 1550 free_extent_buffer(eb); 1551 } 1552 ret = btrfs_find_item(fs_root, path, parent, 0, 1553 BTRFS_INODE_REF_KEY, &found_key); 1554 if (ret > 0) 1555 ret = -ENOENT; 1556 if (ret) 1557 break; 1558 1559 next_inum = found_key.offset; 1560 1561 /* regular exit ahead */ 1562 if (parent == next_inum) 1563 break; 1564 1565 slot = path->slots[0]; 1566 eb = path->nodes[0]; 1567 /* make sure we can use eb after releasing the path */ 1568 if (eb != eb_in) { 1569 if (!path->skip_locking) 1570 btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK); 1571 path->nodes[0] = NULL; 1572 path->locks[0] = 0; 1573 } 1574 btrfs_release_path(path); 1575 iref = btrfs_item_ptr(eb, slot, struct btrfs_inode_ref); 1576 1577 name_len = btrfs_inode_ref_name_len(eb, iref); 1578 name_off = (unsigned long)(iref + 1); 1579 1580 parent = next_inum; 1581 --bytes_left; 1582 if (bytes_left >= 0) 1583 dest[bytes_left] = '/'; 1584 } 1585 1586 btrfs_release_path(path); 1587 path->leave_spinning = leave_spinning; 1588 1589 if (ret) 1590 return ERR_PTR(ret); 1591 1592 return dest + bytes_left; 1593 } 1594 1595 /* 1596 * this makes the path point to (logical EXTENT_ITEM *) 1597 * returns BTRFS_EXTENT_FLAG_DATA for data, BTRFS_EXTENT_FLAG_TREE_BLOCK for 1598 * tree blocks and <0 on error. 1599 */ 1600 int extent_from_logical(struct btrfs_fs_info *fs_info, u64 logical, 1601 struct btrfs_path *path, struct btrfs_key *found_key, 1602 u64 *flags_ret) 1603 { 1604 int ret; 1605 u64 flags; 1606 u64 size = 0; 1607 u32 item_size; 1608 const struct extent_buffer *eb; 1609 struct btrfs_extent_item *ei; 1610 struct btrfs_key key; 1611 1612 if (btrfs_fs_incompat(fs_info, SKINNY_METADATA)) 1613 key.type = BTRFS_METADATA_ITEM_KEY; 1614 else 1615 key.type = BTRFS_EXTENT_ITEM_KEY; 1616 key.objectid = logical; 1617 key.offset = (u64)-1; 1618 1619 ret = btrfs_search_slot(NULL, fs_info->extent_root, &key, path, 0, 0); 1620 if (ret < 0) 1621 return ret; 1622 1623 ret = btrfs_previous_extent_item(fs_info->extent_root, path, 0); 1624 if (ret) { 1625 if (ret > 0) 1626 ret = -ENOENT; 1627 return ret; 1628 } 1629 btrfs_item_key_to_cpu(path->nodes[0], found_key, path->slots[0]); 1630 if (found_key->type == BTRFS_METADATA_ITEM_KEY) 1631 size = fs_info->nodesize; 1632 else if (found_key->type == BTRFS_EXTENT_ITEM_KEY) 1633 size = found_key->offset; 1634 1635 if (found_key->objectid > logical || 1636 found_key->objectid + size <= logical) { 1637 btrfs_debug(fs_info, 1638 "logical %llu is not within any extent", logical); 1639 return -ENOENT; 1640 } 1641 1642 eb = path->nodes[0]; 1643 item_size = btrfs_item_size_nr(eb, path->slots[0]); 1644 BUG_ON(item_size < sizeof(*ei)); 1645 1646 ei = btrfs_item_ptr(eb, path->slots[0], struct btrfs_extent_item); 1647 flags = btrfs_extent_flags(eb, ei); 1648 1649 btrfs_debug(fs_info, 1650 "logical %llu is at position %llu within the extent (%llu EXTENT_ITEM %llu) flags %#llx size %u", 1651 logical, logical - found_key->objectid, found_key->objectid, 1652 found_key->offset, flags, item_size); 1653 1654 WARN_ON(!flags_ret); 1655 if (flags_ret) { 1656 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) 1657 *flags_ret = BTRFS_EXTENT_FLAG_TREE_BLOCK; 1658 else if (flags & BTRFS_EXTENT_FLAG_DATA) 1659 *flags_ret = BTRFS_EXTENT_FLAG_DATA; 1660 else 1661 BUG_ON(1); 1662 return 0; 1663 } 1664 1665 return -EIO; 1666 } 1667 1668 /* 1669 * helper function to iterate extent inline refs. ptr must point to a 0 value 1670 * for the first call and may be modified. it is used to track state. 1671 * if more refs exist, 0 is returned and the next call to 1672 * get_extent_inline_ref must pass the modified ptr parameter to get the 1673 * next ref. after the last ref was processed, 1 is returned. 1674 * returns <0 on error 1675 */ 1676 static int get_extent_inline_ref(unsigned long *ptr, 1677 const struct extent_buffer *eb, 1678 const struct btrfs_key *key, 1679 const struct btrfs_extent_item *ei, 1680 u32 item_size, 1681 struct btrfs_extent_inline_ref **out_eiref, 1682 int *out_type) 1683 { 1684 unsigned long end; 1685 u64 flags; 1686 struct btrfs_tree_block_info *info; 1687 1688 if (!*ptr) { 1689 /* first call */ 1690 flags = btrfs_extent_flags(eb, ei); 1691 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) { 1692 if (key->type == BTRFS_METADATA_ITEM_KEY) { 1693 /* a skinny metadata extent */ 1694 *out_eiref = 1695 (struct btrfs_extent_inline_ref *)(ei + 1); 1696 } else { 1697 WARN_ON(key->type != BTRFS_EXTENT_ITEM_KEY); 1698 info = (struct btrfs_tree_block_info *)(ei + 1); 1699 *out_eiref = 1700 (struct btrfs_extent_inline_ref *)(info + 1); 1701 } 1702 } else { 1703 *out_eiref = (struct btrfs_extent_inline_ref *)(ei + 1); 1704 } 1705 *ptr = (unsigned long)*out_eiref; 1706 if ((unsigned long)(*ptr) >= (unsigned long)ei + item_size) 1707 return -ENOENT; 1708 } 1709 1710 end = (unsigned long)ei + item_size; 1711 *out_eiref = (struct btrfs_extent_inline_ref *)(*ptr); 1712 *out_type = btrfs_extent_inline_ref_type(eb, *out_eiref); 1713 1714 *ptr += btrfs_extent_inline_ref_size(*out_type); 1715 WARN_ON(*ptr > end); 1716 if (*ptr == end) 1717 return 1; /* last */ 1718 1719 return 0; 1720 } 1721 1722 /* 1723 * reads the tree block backref for an extent. tree level and root are returned 1724 * through out_level and out_root. ptr must point to a 0 value for the first 1725 * call and may be modified (see get_extent_inline_ref comment). 1726 * returns 0 if data was provided, 1 if there was no more data to provide or 1727 * <0 on error. 1728 */ 1729 int tree_backref_for_extent(unsigned long *ptr, struct extent_buffer *eb, 1730 struct btrfs_key *key, struct btrfs_extent_item *ei, 1731 u32 item_size, u64 *out_root, u8 *out_level) 1732 { 1733 int ret; 1734 int type; 1735 struct btrfs_extent_inline_ref *eiref; 1736 1737 if (*ptr == (unsigned long)-1) 1738 return 1; 1739 1740 while (1) { 1741 ret = get_extent_inline_ref(ptr, eb, key, ei, item_size, 1742 &eiref, &type); 1743 if (ret < 0) 1744 return ret; 1745 1746 if (type == BTRFS_TREE_BLOCK_REF_KEY || 1747 type == BTRFS_SHARED_BLOCK_REF_KEY) 1748 break; 1749 1750 if (ret == 1) 1751 return 1; 1752 } 1753 1754 /* we can treat both ref types equally here */ 1755 *out_root = btrfs_extent_inline_ref_offset(eb, eiref); 1756 1757 if (key->type == BTRFS_EXTENT_ITEM_KEY) { 1758 struct btrfs_tree_block_info *info; 1759 1760 info = (struct btrfs_tree_block_info *)(ei + 1); 1761 *out_level = btrfs_tree_block_level(eb, info); 1762 } else { 1763 ASSERT(key->type == BTRFS_METADATA_ITEM_KEY); 1764 *out_level = (u8)key->offset; 1765 } 1766 1767 if (ret == 1) 1768 *ptr = (unsigned long)-1; 1769 1770 return 0; 1771 } 1772 1773 static int iterate_leaf_refs(struct btrfs_fs_info *fs_info, 1774 struct extent_inode_elem *inode_list, 1775 u64 root, u64 extent_item_objectid, 1776 iterate_extent_inodes_t *iterate, void *ctx) 1777 { 1778 struct extent_inode_elem *eie; 1779 int ret = 0; 1780 1781 for (eie = inode_list; eie; eie = eie->next) { 1782 btrfs_debug(fs_info, 1783 "ref for %llu resolved, key (%llu EXTEND_DATA %llu), root %llu", 1784 extent_item_objectid, eie->inum, 1785 eie->offset, root); 1786 ret = iterate(eie->inum, eie->offset, root, ctx); 1787 if (ret) { 1788 btrfs_debug(fs_info, 1789 "stopping iteration for %llu due to ret=%d", 1790 extent_item_objectid, ret); 1791 break; 1792 } 1793 } 1794 1795 return ret; 1796 } 1797 1798 /* 1799 * calls iterate() for every inode that references the extent identified by 1800 * the given parameters. 1801 * when the iterator function returns a non-zero value, iteration stops. 1802 */ 1803 int iterate_extent_inodes(struct btrfs_fs_info *fs_info, 1804 u64 extent_item_objectid, u64 extent_item_pos, 1805 int search_commit_root, 1806 iterate_extent_inodes_t *iterate, void *ctx) 1807 { 1808 int ret; 1809 struct btrfs_trans_handle *trans = NULL; 1810 struct ulist *refs = NULL; 1811 struct ulist *roots = NULL; 1812 struct ulist_node *ref_node = NULL; 1813 struct ulist_node *root_node = NULL; 1814 struct seq_list tree_mod_seq_elem = SEQ_LIST_INIT(tree_mod_seq_elem); 1815 struct ulist_iterator ref_uiter; 1816 struct ulist_iterator root_uiter; 1817 1818 btrfs_debug(fs_info, "resolving all inodes for extent %llu", 1819 extent_item_objectid); 1820 1821 if (!search_commit_root) { 1822 trans = btrfs_join_transaction(fs_info->extent_root); 1823 if (IS_ERR(trans)) 1824 return PTR_ERR(trans); 1825 btrfs_get_tree_mod_seq(fs_info, &tree_mod_seq_elem); 1826 } else { 1827 down_read(&fs_info->commit_root_sem); 1828 } 1829 1830 ret = btrfs_find_all_leafs(trans, fs_info, extent_item_objectid, 1831 tree_mod_seq_elem.seq, &refs, 1832 &extent_item_pos); 1833 if (ret) 1834 goto out; 1835 1836 ULIST_ITER_INIT(&ref_uiter); 1837 while (!ret && (ref_node = ulist_next(refs, &ref_uiter))) { 1838 ret = btrfs_find_all_roots_safe(trans, fs_info, ref_node->val, 1839 tree_mod_seq_elem.seq, &roots); 1840 if (ret) 1841 break; 1842 ULIST_ITER_INIT(&root_uiter); 1843 while (!ret && (root_node = ulist_next(roots, &root_uiter))) { 1844 btrfs_debug(fs_info, 1845 "root %llu references leaf %llu, data list %#llx", 1846 root_node->val, ref_node->val, 1847 ref_node->aux); 1848 ret = iterate_leaf_refs(fs_info, 1849 (struct extent_inode_elem *) 1850 (uintptr_t)ref_node->aux, 1851 root_node->val, 1852 extent_item_objectid, 1853 iterate, ctx); 1854 } 1855 ulist_free(roots); 1856 } 1857 1858 free_leaf_list(refs); 1859 out: 1860 if (!search_commit_root) { 1861 btrfs_put_tree_mod_seq(fs_info, &tree_mod_seq_elem); 1862 btrfs_end_transaction(trans); 1863 } else { 1864 up_read(&fs_info->commit_root_sem); 1865 } 1866 1867 return ret; 1868 } 1869 1870 int iterate_inodes_from_logical(u64 logical, struct btrfs_fs_info *fs_info, 1871 struct btrfs_path *path, 1872 iterate_extent_inodes_t *iterate, void *ctx) 1873 { 1874 int ret; 1875 u64 extent_item_pos; 1876 u64 flags = 0; 1877 struct btrfs_key found_key; 1878 int search_commit_root = path->search_commit_root; 1879 1880 ret = extent_from_logical(fs_info, logical, path, &found_key, &flags); 1881 btrfs_release_path(path); 1882 if (ret < 0) 1883 return ret; 1884 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) 1885 return -EINVAL; 1886 1887 extent_item_pos = logical - found_key.objectid; 1888 ret = iterate_extent_inodes(fs_info, found_key.objectid, 1889 extent_item_pos, search_commit_root, 1890 iterate, ctx); 1891 1892 return ret; 1893 } 1894 1895 typedef int (iterate_irefs_t)(u64 parent, u32 name_len, unsigned long name_off, 1896 struct extent_buffer *eb, void *ctx); 1897 1898 static int iterate_inode_refs(u64 inum, struct btrfs_root *fs_root, 1899 struct btrfs_path *path, 1900 iterate_irefs_t *iterate, void *ctx) 1901 { 1902 int ret = 0; 1903 int slot; 1904 u32 cur; 1905 u32 len; 1906 u32 name_len; 1907 u64 parent = 0; 1908 int found = 0; 1909 struct extent_buffer *eb; 1910 struct btrfs_item *item; 1911 struct btrfs_inode_ref *iref; 1912 struct btrfs_key found_key; 1913 1914 while (!ret) { 1915 ret = btrfs_find_item(fs_root, path, inum, 1916 parent ? parent + 1 : 0, BTRFS_INODE_REF_KEY, 1917 &found_key); 1918 1919 if (ret < 0) 1920 break; 1921 if (ret) { 1922 ret = found ? 0 : -ENOENT; 1923 break; 1924 } 1925 ++found; 1926 1927 parent = found_key.offset; 1928 slot = path->slots[0]; 1929 eb = btrfs_clone_extent_buffer(path->nodes[0]); 1930 if (!eb) { 1931 ret = -ENOMEM; 1932 break; 1933 } 1934 extent_buffer_get(eb); 1935 btrfs_tree_read_lock(eb); 1936 btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK); 1937 btrfs_release_path(path); 1938 1939 item = btrfs_item_nr(slot); 1940 iref = btrfs_item_ptr(eb, slot, struct btrfs_inode_ref); 1941 1942 for (cur = 0; cur < btrfs_item_size(eb, item); cur += len) { 1943 name_len = btrfs_inode_ref_name_len(eb, iref); 1944 /* path must be released before calling iterate()! */ 1945 btrfs_debug(fs_root->fs_info, 1946 "following ref at offset %u for inode %llu in tree %llu", 1947 cur, found_key.objectid, fs_root->objectid); 1948 ret = iterate(parent, name_len, 1949 (unsigned long)(iref + 1), eb, ctx); 1950 if (ret) 1951 break; 1952 len = sizeof(*iref) + name_len; 1953 iref = (struct btrfs_inode_ref *)((char *)iref + len); 1954 } 1955 btrfs_tree_read_unlock_blocking(eb); 1956 free_extent_buffer(eb); 1957 } 1958 1959 btrfs_release_path(path); 1960 1961 return ret; 1962 } 1963 1964 static int iterate_inode_extrefs(u64 inum, struct btrfs_root *fs_root, 1965 struct btrfs_path *path, 1966 iterate_irefs_t *iterate, void *ctx) 1967 { 1968 int ret; 1969 int slot; 1970 u64 offset = 0; 1971 u64 parent; 1972 int found = 0; 1973 struct extent_buffer *eb; 1974 struct btrfs_inode_extref *extref; 1975 u32 item_size; 1976 u32 cur_offset; 1977 unsigned long ptr; 1978 1979 while (1) { 1980 ret = btrfs_find_one_extref(fs_root, inum, offset, path, &extref, 1981 &offset); 1982 if (ret < 0) 1983 break; 1984 if (ret) { 1985 ret = found ? 0 : -ENOENT; 1986 break; 1987 } 1988 ++found; 1989 1990 slot = path->slots[0]; 1991 eb = btrfs_clone_extent_buffer(path->nodes[0]); 1992 if (!eb) { 1993 ret = -ENOMEM; 1994 break; 1995 } 1996 extent_buffer_get(eb); 1997 1998 btrfs_tree_read_lock(eb); 1999 btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK); 2000 btrfs_release_path(path); 2001 2002 item_size = btrfs_item_size_nr(eb, slot); 2003 ptr = btrfs_item_ptr_offset(eb, slot); 2004 cur_offset = 0; 2005 2006 while (cur_offset < item_size) { 2007 u32 name_len; 2008 2009 extref = (struct btrfs_inode_extref *)(ptr + cur_offset); 2010 parent = btrfs_inode_extref_parent(eb, extref); 2011 name_len = btrfs_inode_extref_name_len(eb, extref); 2012 ret = iterate(parent, name_len, 2013 (unsigned long)&extref->name, eb, ctx); 2014 if (ret) 2015 break; 2016 2017 cur_offset += btrfs_inode_extref_name_len(eb, extref); 2018 cur_offset += sizeof(*extref); 2019 } 2020 btrfs_tree_read_unlock_blocking(eb); 2021 free_extent_buffer(eb); 2022 2023 offset++; 2024 } 2025 2026 btrfs_release_path(path); 2027 2028 return ret; 2029 } 2030 2031 static int iterate_irefs(u64 inum, struct btrfs_root *fs_root, 2032 struct btrfs_path *path, iterate_irefs_t *iterate, 2033 void *ctx) 2034 { 2035 int ret; 2036 int found_refs = 0; 2037 2038 ret = iterate_inode_refs(inum, fs_root, path, iterate, ctx); 2039 if (!ret) 2040 ++found_refs; 2041 else if (ret != -ENOENT) 2042 return ret; 2043 2044 ret = iterate_inode_extrefs(inum, fs_root, path, iterate, ctx); 2045 if (ret == -ENOENT && found_refs) 2046 return 0; 2047 2048 return ret; 2049 } 2050 2051 /* 2052 * returns 0 if the path could be dumped (probably truncated) 2053 * returns <0 in case of an error 2054 */ 2055 static int inode_to_path(u64 inum, u32 name_len, unsigned long name_off, 2056 struct extent_buffer *eb, void *ctx) 2057 { 2058 struct inode_fs_paths *ipath = ctx; 2059 char *fspath; 2060 char *fspath_min; 2061 int i = ipath->fspath->elem_cnt; 2062 const int s_ptr = sizeof(char *); 2063 u32 bytes_left; 2064 2065 bytes_left = ipath->fspath->bytes_left > s_ptr ? 2066 ipath->fspath->bytes_left - s_ptr : 0; 2067 2068 fspath_min = (char *)ipath->fspath->val + (i + 1) * s_ptr; 2069 fspath = btrfs_ref_to_path(ipath->fs_root, ipath->btrfs_path, name_len, 2070 name_off, eb, inum, fspath_min, bytes_left); 2071 if (IS_ERR(fspath)) 2072 return PTR_ERR(fspath); 2073 2074 if (fspath > fspath_min) { 2075 ipath->fspath->val[i] = (u64)(unsigned long)fspath; 2076 ++ipath->fspath->elem_cnt; 2077 ipath->fspath->bytes_left = fspath - fspath_min; 2078 } else { 2079 ++ipath->fspath->elem_missed; 2080 ipath->fspath->bytes_missing += fspath_min - fspath; 2081 ipath->fspath->bytes_left = 0; 2082 } 2083 2084 return 0; 2085 } 2086 2087 /* 2088 * this dumps all file system paths to the inode into the ipath struct, provided 2089 * is has been created large enough. each path is zero-terminated and accessed 2090 * from ipath->fspath->val[i]. 2091 * when it returns, there are ipath->fspath->elem_cnt number of paths available 2092 * in ipath->fspath->val[]. when the allocated space wasn't sufficient, the 2093 * number of missed paths is recorded in ipath->fspath->elem_missed, otherwise, 2094 * it's zero. ipath->fspath->bytes_missing holds the number of bytes that would 2095 * have been needed to return all paths. 2096 */ 2097 int paths_from_inode(u64 inum, struct inode_fs_paths *ipath) 2098 { 2099 return iterate_irefs(inum, ipath->fs_root, ipath->btrfs_path, 2100 inode_to_path, ipath); 2101 } 2102 2103 struct btrfs_data_container *init_data_container(u32 total_bytes) 2104 { 2105 struct btrfs_data_container *data; 2106 size_t alloc_bytes; 2107 2108 alloc_bytes = max_t(size_t, total_bytes, sizeof(*data)); 2109 data = kvmalloc(alloc_bytes, GFP_KERNEL); 2110 if (!data) 2111 return ERR_PTR(-ENOMEM); 2112 2113 if (total_bytes >= sizeof(*data)) { 2114 data->bytes_left = total_bytes - sizeof(*data); 2115 data->bytes_missing = 0; 2116 } else { 2117 data->bytes_missing = sizeof(*data) - total_bytes; 2118 data->bytes_left = 0; 2119 } 2120 2121 data->elem_cnt = 0; 2122 data->elem_missed = 0; 2123 2124 return data; 2125 } 2126 2127 /* 2128 * allocates space to return multiple file system paths for an inode. 2129 * total_bytes to allocate are passed, note that space usable for actual path 2130 * information will be total_bytes - sizeof(struct inode_fs_paths). 2131 * the returned pointer must be freed with free_ipath() in the end. 2132 */ 2133 struct inode_fs_paths *init_ipath(s32 total_bytes, struct btrfs_root *fs_root, 2134 struct btrfs_path *path) 2135 { 2136 struct inode_fs_paths *ifp; 2137 struct btrfs_data_container *fspath; 2138 2139 fspath = init_data_container(total_bytes); 2140 if (IS_ERR(fspath)) 2141 return (void *)fspath; 2142 2143 ifp = kmalloc(sizeof(*ifp), GFP_KERNEL); 2144 if (!ifp) { 2145 kvfree(fspath); 2146 return ERR_PTR(-ENOMEM); 2147 } 2148 2149 ifp->btrfs_path = path; 2150 ifp->fspath = fspath; 2151 ifp->fs_root = fs_root; 2152 2153 return ifp; 2154 } 2155 2156 void free_ipath(struct inode_fs_paths *ipath) 2157 { 2158 if (!ipath) 2159 return; 2160 kvfree(ipath->fspath); 2161 kfree(ipath); 2162 } 2163