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