1a542ad1bSJan Schmidt /* 2a542ad1bSJan Schmidt * Copyright (C) 2011 STRATO. All rights reserved. 3a542ad1bSJan Schmidt * 4a542ad1bSJan Schmidt * This program is free software; you can redistribute it and/or 5a542ad1bSJan Schmidt * modify it under the terms of the GNU General Public 6a542ad1bSJan Schmidt * License v2 as published by the Free Software Foundation. 7a542ad1bSJan Schmidt * 8a542ad1bSJan Schmidt * This program is distributed in the hope that it will be useful, 9a542ad1bSJan Schmidt * but WITHOUT ANY WARRANTY; without even the implied warranty of 10a542ad1bSJan Schmidt * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 11a542ad1bSJan Schmidt * General Public License for more details. 12a542ad1bSJan Schmidt * 13a542ad1bSJan Schmidt * You should have received a copy of the GNU General Public 14a542ad1bSJan Schmidt * License along with this program; if not, write to the 15a542ad1bSJan Schmidt * Free Software Foundation, Inc., 59 Temple Place - Suite 330, 16a542ad1bSJan Schmidt * Boston, MA 021110-1307, USA. 17a542ad1bSJan Schmidt */ 18a542ad1bSJan Schmidt 19425d17a2SLiu Bo #include <linux/vmalloc.h> 20a542ad1bSJan Schmidt #include "ctree.h" 21a542ad1bSJan Schmidt #include "disk-io.h" 22a542ad1bSJan Schmidt #include "backref.h" 238da6d581SJan Schmidt #include "ulist.h" 248da6d581SJan Schmidt #include "transaction.h" 258da6d581SJan Schmidt #include "delayed-ref.h" 26b916a59aSJan Schmidt #include "locking.h" 27a542ad1bSJan Schmidt 28976b1908SJan Schmidt struct extent_inode_elem { 29976b1908SJan Schmidt u64 inum; 30976b1908SJan Schmidt u64 offset; 31976b1908SJan Schmidt struct extent_inode_elem *next; 32976b1908SJan Schmidt }; 33976b1908SJan Schmidt 34976b1908SJan Schmidt static int check_extent_in_eb(struct btrfs_key *key, struct extent_buffer *eb, 35976b1908SJan Schmidt struct btrfs_file_extent_item *fi, 36976b1908SJan Schmidt u64 extent_item_pos, 37976b1908SJan Schmidt struct extent_inode_elem **eie) 38976b1908SJan Schmidt { 39976b1908SJan Schmidt u64 data_offset; 40976b1908SJan Schmidt u64 data_len; 41976b1908SJan Schmidt struct extent_inode_elem *e; 42976b1908SJan Schmidt 43976b1908SJan Schmidt data_offset = btrfs_file_extent_offset(eb, fi); 44976b1908SJan Schmidt data_len = btrfs_file_extent_num_bytes(eb, fi); 45976b1908SJan Schmidt 46976b1908SJan Schmidt if (extent_item_pos < data_offset || 47976b1908SJan Schmidt extent_item_pos >= data_offset + data_len) 48976b1908SJan Schmidt return 1; 49976b1908SJan Schmidt 50976b1908SJan Schmidt e = kmalloc(sizeof(*e), GFP_NOFS); 51976b1908SJan Schmidt if (!e) 52976b1908SJan Schmidt return -ENOMEM; 53976b1908SJan Schmidt 54976b1908SJan Schmidt e->next = *eie; 55976b1908SJan Schmidt e->inum = key->objectid; 56976b1908SJan Schmidt e->offset = key->offset + (extent_item_pos - data_offset); 57976b1908SJan Schmidt *eie = e; 58976b1908SJan Schmidt 59976b1908SJan Schmidt return 0; 60976b1908SJan Schmidt } 61976b1908SJan Schmidt 62976b1908SJan Schmidt static int find_extent_in_eb(struct extent_buffer *eb, u64 wanted_disk_byte, 63976b1908SJan Schmidt u64 extent_item_pos, 64976b1908SJan Schmidt struct extent_inode_elem **eie) 65976b1908SJan Schmidt { 66976b1908SJan Schmidt u64 disk_byte; 67976b1908SJan Schmidt struct btrfs_key key; 68976b1908SJan Schmidt struct btrfs_file_extent_item *fi; 69976b1908SJan Schmidt int slot; 70976b1908SJan Schmidt int nritems; 71976b1908SJan Schmidt int extent_type; 72976b1908SJan Schmidt int ret; 73976b1908SJan Schmidt 74976b1908SJan Schmidt /* 75976b1908SJan Schmidt * from the shared data ref, we only have the leaf but we need 76976b1908SJan Schmidt * the key. thus, we must look into all items and see that we 77976b1908SJan Schmidt * find one (some) with a reference to our extent item. 78976b1908SJan Schmidt */ 79976b1908SJan Schmidt nritems = btrfs_header_nritems(eb); 80976b1908SJan Schmidt for (slot = 0; slot < nritems; ++slot) { 81976b1908SJan Schmidt btrfs_item_key_to_cpu(eb, &key, slot); 82976b1908SJan Schmidt if (key.type != BTRFS_EXTENT_DATA_KEY) 83976b1908SJan Schmidt continue; 84976b1908SJan Schmidt fi = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item); 85976b1908SJan Schmidt extent_type = btrfs_file_extent_type(eb, fi); 86976b1908SJan Schmidt if (extent_type == BTRFS_FILE_EXTENT_INLINE) 87976b1908SJan Schmidt continue; 88976b1908SJan Schmidt /* don't skip BTRFS_FILE_EXTENT_PREALLOC, we can handle that */ 89976b1908SJan Schmidt disk_byte = btrfs_file_extent_disk_bytenr(eb, fi); 90976b1908SJan Schmidt if (disk_byte != wanted_disk_byte) 91976b1908SJan Schmidt continue; 92976b1908SJan Schmidt 93976b1908SJan Schmidt ret = check_extent_in_eb(&key, eb, fi, extent_item_pos, eie); 94976b1908SJan Schmidt if (ret < 0) 95976b1908SJan Schmidt return ret; 96976b1908SJan Schmidt } 97976b1908SJan Schmidt 98976b1908SJan Schmidt return 0; 99976b1908SJan Schmidt } 100976b1908SJan Schmidt 1018da6d581SJan Schmidt /* 1028da6d581SJan Schmidt * this structure records all encountered refs on the way up to the root 1038da6d581SJan Schmidt */ 1048da6d581SJan Schmidt struct __prelim_ref { 1058da6d581SJan Schmidt struct list_head list; 1068da6d581SJan Schmidt u64 root_id; 107d5c88b73SJan Schmidt struct btrfs_key key_for_search; 1088da6d581SJan Schmidt int level; 1098da6d581SJan Schmidt int count; 1103301958bSJan Schmidt struct extent_inode_elem *inode_list; 1118da6d581SJan Schmidt u64 parent; 1128da6d581SJan Schmidt u64 wanted_disk_byte; 1138da6d581SJan Schmidt }; 1148da6d581SJan Schmidt 115d5c88b73SJan Schmidt /* 116d5c88b73SJan Schmidt * the rules for all callers of this function are: 117d5c88b73SJan Schmidt * - obtaining the parent is the goal 118d5c88b73SJan Schmidt * - if you add a key, you must know that it is a correct key 119d5c88b73SJan Schmidt * - if you cannot add the parent or a correct key, then we will look into the 120d5c88b73SJan Schmidt * block later to set a correct key 121d5c88b73SJan Schmidt * 122d5c88b73SJan Schmidt * delayed refs 123d5c88b73SJan Schmidt * ============ 124d5c88b73SJan Schmidt * backref type | shared | indirect | shared | indirect 125d5c88b73SJan Schmidt * information | tree | tree | data | data 126d5c88b73SJan Schmidt * --------------------+--------+----------+--------+---------- 127d5c88b73SJan Schmidt * parent logical | y | - | - | - 128d5c88b73SJan Schmidt * key to resolve | - | y | y | y 129d5c88b73SJan Schmidt * tree block logical | - | - | - | - 130d5c88b73SJan Schmidt * root for resolving | y | y | y | y 131d5c88b73SJan Schmidt * 132d5c88b73SJan Schmidt * - column 1: we've the parent -> done 133d5c88b73SJan Schmidt * - column 2, 3, 4: we use the key to find the parent 134d5c88b73SJan Schmidt * 135d5c88b73SJan Schmidt * on disk refs (inline or keyed) 136d5c88b73SJan Schmidt * ============================== 137d5c88b73SJan Schmidt * backref type | shared | indirect | shared | indirect 138d5c88b73SJan Schmidt * information | tree | tree | data | data 139d5c88b73SJan Schmidt * --------------------+--------+----------+--------+---------- 140d5c88b73SJan Schmidt * parent logical | y | - | y | - 141d5c88b73SJan Schmidt * key to resolve | - | - | - | y 142d5c88b73SJan Schmidt * tree block logical | y | y | y | y 143d5c88b73SJan Schmidt * root for resolving | - | y | y | y 144d5c88b73SJan Schmidt * 145d5c88b73SJan Schmidt * - column 1, 3: we've the parent -> done 146d5c88b73SJan Schmidt * - column 2: we take the first key from the block to find the parent 147d5c88b73SJan Schmidt * (see __add_missing_keys) 148d5c88b73SJan Schmidt * - column 4: we use the key to find the parent 149d5c88b73SJan Schmidt * 150d5c88b73SJan Schmidt * additional information that's available but not required to find the parent 151d5c88b73SJan Schmidt * block might help in merging entries to gain some speed. 152d5c88b73SJan Schmidt */ 153d5c88b73SJan Schmidt 1548da6d581SJan Schmidt static int __add_prelim_ref(struct list_head *head, u64 root_id, 155d5c88b73SJan Schmidt struct btrfs_key *key, int level, 156d5c88b73SJan Schmidt u64 parent, u64 wanted_disk_byte, int count) 1578da6d581SJan Schmidt { 1588da6d581SJan Schmidt struct __prelim_ref *ref; 1598da6d581SJan Schmidt 1608da6d581SJan Schmidt /* in case we're adding delayed refs, we're holding the refs spinlock */ 1618da6d581SJan Schmidt ref = kmalloc(sizeof(*ref), GFP_ATOMIC); 1628da6d581SJan Schmidt if (!ref) 1638da6d581SJan Schmidt return -ENOMEM; 1648da6d581SJan Schmidt 1658da6d581SJan Schmidt ref->root_id = root_id; 1668da6d581SJan Schmidt if (key) 167d5c88b73SJan Schmidt ref->key_for_search = *key; 1688da6d581SJan Schmidt else 169d5c88b73SJan Schmidt memset(&ref->key_for_search, 0, sizeof(ref->key_for_search)); 1708da6d581SJan Schmidt 1713301958bSJan Schmidt ref->inode_list = NULL; 1728da6d581SJan Schmidt ref->level = level; 1738da6d581SJan Schmidt ref->count = count; 1748da6d581SJan Schmidt ref->parent = parent; 1758da6d581SJan Schmidt ref->wanted_disk_byte = wanted_disk_byte; 1768da6d581SJan Schmidt list_add_tail(&ref->list, head); 1778da6d581SJan Schmidt 1788da6d581SJan Schmidt return 0; 1798da6d581SJan Schmidt } 1808da6d581SJan Schmidt 1818da6d581SJan Schmidt static int add_all_parents(struct btrfs_root *root, struct btrfs_path *path, 182976b1908SJan Schmidt struct ulist *parents, int level, 18369bca40dSAlexander Block struct btrfs_key *key_for_search, u64 time_seq, 1843d7806ecSJan Schmidt u64 wanted_disk_byte, 185976b1908SJan Schmidt const u64 *extent_item_pos) 1868da6d581SJan Schmidt { 18769bca40dSAlexander Block int ret = 0; 18869bca40dSAlexander Block int slot; 18969bca40dSAlexander Block struct extent_buffer *eb; 19069bca40dSAlexander Block struct btrfs_key key; 1918da6d581SJan Schmidt struct btrfs_file_extent_item *fi; 1923301958bSJan Schmidt struct extent_inode_elem *eie = NULL; 1938da6d581SJan Schmidt u64 disk_byte; 1948da6d581SJan Schmidt 19569bca40dSAlexander Block if (level != 0) { 19669bca40dSAlexander Block eb = path->nodes[level]; 19769bca40dSAlexander Block ret = ulist_add(parents, eb->start, 0, GFP_NOFS); 1983301958bSJan Schmidt if (ret < 0) 1993301958bSJan Schmidt return ret; 2008da6d581SJan Schmidt return 0; 20169bca40dSAlexander Block } 2028da6d581SJan Schmidt 2038da6d581SJan Schmidt /* 20469bca40dSAlexander Block * We normally enter this function with the path already pointing to 20569bca40dSAlexander Block * the first item to check. But sometimes, we may enter it with 20669bca40dSAlexander Block * slot==nritems. In that case, go to the next leaf before we continue. 2078da6d581SJan Schmidt */ 20869bca40dSAlexander Block if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) 2093d7806ecSJan Schmidt ret = btrfs_next_old_leaf(root, path, time_seq); 2108da6d581SJan Schmidt 21169bca40dSAlexander Block while (!ret) { 2128da6d581SJan Schmidt eb = path->nodes[0]; 21369bca40dSAlexander Block slot = path->slots[0]; 21469bca40dSAlexander Block 21569bca40dSAlexander Block btrfs_item_key_to_cpu(eb, &key, slot); 21669bca40dSAlexander Block 21769bca40dSAlexander Block if (key.objectid != key_for_search->objectid || 21869bca40dSAlexander Block key.type != BTRFS_EXTENT_DATA_KEY) 21969bca40dSAlexander Block break; 22069bca40dSAlexander Block 22169bca40dSAlexander Block fi = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item); 2228da6d581SJan Schmidt disk_byte = btrfs_file_extent_disk_bytenr(eb, fi); 22369bca40dSAlexander Block 22469bca40dSAlexander Block if (disk_byte == wanted_disk_byte) { 22569bca40dSAlexander Block eie = NULL; 22669bca40dSAlexander Block if (extent_item_pos) { 22769bca40dSAlexander Block ret = check_extent_in_eb(&key, eb, fi, 22869bca40dSAlexander Block *extent_item_pos, 22969bca40dSAlexander Block &eie); 23069bca40dSAlexander Block if (ret < 0) 23169bca40dSAlexander Block break; 2328da6d581SJan Schmidt } 23369bca40dSAlexander Block if (!ret) { 23469bca40dSAlexander Block ret = ulist_add(parents, eb->start, 235995e01b7SJan Schmidt (uintptr_t)eie, GFP_NOFS); 23669bca40dSAlexander Block if (ret < 0) 23769bca40dSAlexander Block break; 23869bca40dSAlexander Block if (!extent_item_pos) { 23969bca40dSAlexander Block ret = btrfs_next_old_leaf(root, path, 24069bca40dSAlexander Block time_seq); 24169bca40dSAlexander Block continue; 24269bca40dSAlexander Block } 24369bca40dSAlexander Block } 24469bca40dSAlexander Block } 24569bca40dSAlexander Block ret = btrfs_next_old_item(root, path, time_seq); 2468da6d581SJan Schmidt } 2478da6d581SJan Schmidt 24869bca40dSAlexander Block if (ret > 0) 24969bca40dSAlexander Block ret = 0; 25069bca40dSAlexander Block return ret; 2518da6d581SJan Schmidt } 2528da6d581SJan Schmidt 2538da6d581SJan Schmidt /* 2548da6d581SJan Schmidt * resolve an indirect backref in the form (root_id, key, level) 2558da6d581SJan Schmidt * to a logical address 2568da6d581SJan Schmidt */ 2578da6d581SJan Schmidt static int __resolve_indirect_ref(struct btrfs_fs_info *fs_info, 2587a3ae2f8SJan Schmidt int search_commit_root, 2598445f61cSJan Schmidt u64 time_seq, 2608da6d581SJan Schmidt struct __prelim_ref *ref, 261976b1908SJan Schmidt struct ulist *parents, 262976b1908SJan Schmidt const u64 *extent_item_pos) 2638da6d581SJan Schmidt { 2648da6d581SJan Schmidt struct btrfs_path *path; 2658da6d581SJan Schmidt struct btrfs_root *root; 2668da6d581SJan Schmidt struct btrfs_key root_key; 2678da6d581SJan Schmidt struct extent_buffer *eb; 2688da6d581SJan Schmidt int ret = 0; 2698da6d581SJan Schmidt int root_level; 2708da6d581SJan Schmidt int level = ref->level; 2718da6d581SJan Schmidt 2728da6d581SJan Schmidt path = btrfs_alloc_path(); 2738da6d581SJan Schmidt if (!path) 2748da6d581SJan Schmidt return -ENOMEM; 2757a3ae2f8SJan Schmidt path->search_commit_root = !!search_commit_root; 2768da6d581SJan Schmidt 2778da6d581SJan Schmidt root_key.objectid = ref->root_id; 2788da6d581SJan Schmidt root_key.type = BTRFS_ROOT_ITEM_KEY; 2798da6d581SJan Schmidt root_key.offset = (u64)-1; 2808da6d581SJan Schmidt root = btrfs_read_fs_root_no_name(fs_info, &root_key); 2818da6d581SJan Schmidt if (IS_ERR(root)) { 2828da6d581SJan Schmidt ret = PTR_ERR(root); 2838da6d581SJan Schmidt goto out; 2848da6d581SJan Schmidt } 2858da6d581SJan Schmidt 286*5b6602e7SJan Schmidt root_level = btrfs_old_root_level(root, time_seq); 2878da6d581SJan Schmidt 2888da6d581SJan Schmidt if (root_level + 1 == level) 2898da6d581SJan Schmidt goto out; 2908da6d581SJan Schmidt 2918da6d581SJan Schmidt path->lowest_level = level; 2928445f61cSJan Schmidt ret = btrfs_search_old_slot(root, &ref->key_for_search, path, time_seq); 2938da6d581SJan Schmidt pr_debug("search slot in root %llu (level %d, ref count %d) returned " 2948da6d581SJan Schmidt "%d for key (%llu %u %llu)\n", 2958da6d581SJan Schmidt (unsigned long long)ref->root_id, level, ref->count, ret, 296d5c88b73SJan Schmidt (unsigned long long)ref->key_for_search.objectid, 297d5c88b73SJan Schmidt ref->key_for_search.type, 298d5c88b73SJan Schmidt (unsigned long long)ref->key_for_search.offset); 2998da6d581SJan Schmidt if (ret < 0) 3008da6d581SJan Schmidt goto out; 3018da6d581SJan Schmidt 3028da6d581SJan Schmidt eb = path->nodes[level]; 3039345457fSJan Schmidt while (!eb) { 3049345457fSJan Schmidt if (!level) { 3058da6d581SJan Schmidt WARN_ON(1); 3068da6d581SJan Schmidt ret = 1; 3078da6d581SJan Schmidt goto out; 3088da6d581SJan Schmidt } 3099345457fSJan Schmidt level--; 3109345457fSJan Schmidt eb = path->nodes[level]; 3119345457fSJan Schmidt } 3128da6d581SJan Schmidt 31369bca40dSAlexander Block ret = add_all_parents(root, path, parents, level, &ref->key_for_search, 31469bca40dSAlexander Block time_seq, ref->wanted_disk_byte, 31569bca40dSAlexander Block extent_item_pos); 3168da6d581SJan Schmidt out: 3178da6d581SJan Schmidt btrfs_free_path(path); 3188da6d581SJan Schmidt return ret; 3198da6d581SJan Schmidt } 3208da6d581SJan Schmidt 3218da6d581SJan Schmidt /* 3228da6d581SJan Schmidt * resolve all indirect backrefs from the list 3238da6d581SJan Schmidt */ 3248da6d581SJan Schmidt static int __resolve_indirect_refs(struct btrfs_fs_info *fs_info, 3258445f61cSJan Schmidt int search_commit_root, u64 time_seq, 326976b1908SJan Schmidt struct list_head *head, 327976b1908SJan Schmidt const u64 *extent_item_pos) 3288da6d581SJan Schmidt { 3298da6d581SJan Schmidt int err; 3308da6d581SJan Schmidt int ret = 0; 3318da6d581SJan Schmidt struct __prelim_ref *ref; 3328da6d581SJan Schmidt struct __prelim_ref *ref_safe; 3338da6d581SJan Schmidt struct __prelim_ref *new_ref; 3348da6d581SJan Schmidt struct ulist *parents; 3358da6d581SJan Schmidt struct ulist_node *node; 336cd1b413cSJan Schmidt struct ulist_iterator uiter; 3378da6d581SJan Schmidt 3388da6d581SJan Schmidt parents = ulist_alloc(GFP_NOFS); 3398da6d581SJan Schmidt if (!parents) 3408da6d581SJan Schmidt return -ENOMEM; 3418da6d581SJan Schmidt 3428da6d581SJan Schmidt /* 3438da6d581SJan Schmidt * _safe allows us to insert directly after the current item without 3448da6d581SJan Schmidt * iterating over the newly inserted items. 3458da6d581SJan Schmidt * we're also allowed to re-assign ref during iteration. 3468da6d581SJan Schmidt */ 3478da6d581SJan Schmidt list_for_each_entry_safe(ref, ref_safe, head, list) { 3488da6d581SJan Schmidt if (ref->parent) /* already direct */ 3498da6d581SJan Schmidt continue; 3508da6d581SJan Schmidt if (ref->count == 0) 3518da6d581SJan Schmidt continue; 3527a3ae2f8SJan Schmidt err = __resolve_indirect_ref(fs_info, search_commit_root, 3538445f61cSJan Schmidt time_seq, ref, parents, 3548445f61cSJan Schmidt extent_item_pos); 3558da6d581SJan Schmidt if (err) { 3568da6d581SJan Schmidt if (ret == 0) 3578da6d581SJan Schmidt ret = err; 3588da6d581SJan Schmidt continue; 3598da6d581SJan Schmidt } 3608da6d581SJan Schmidt 3618da6d581SJan Schmidt /* we put the first parent into the ref at hand */ 362cd1b413cSJan Schmidt ULIST_ITER_INIT(&uiter); 363cd1b413cSJan Schmidt node = ulist_next(parents, &uiter); 3648da6d581SJan Schmidt ref->parent = node ? node->val : 0; 365995e01b7SJan Schmidt ref->inode_list = node ? 366995e01b7SJan Schmidt (struct extent_inode_elem *)(uintptr_t)node->aux : 0; 3678da6d581SJan Schmidt 3688da6d581SJan Schmidt /* additional parents require new refs being added here */ 369cd1b413cSJan Schmidt while ((node = ulist_next(parents, &uiter))) { 3708da6d581SJan Schmidt new_ref = kmalloc(sizeof(*new_ref), GFP_NOFS); 3718da6d581SJan Schmidt if (!new_ref) { 3728da6d581SJan Schmidt ret = -ENOMEM; 3738da6d581SJan Schmidt break; 3748da6d581SJan Schmidt } 3758da6d581SJan Schmidt memcpy(new_ref, ref, sizeof(*ref)); 3768da6d581SJan Schmidt new_ref->parent = node->val; 377995e01b7SJan Schmidt new_ref->inode_list = (struct extent_inode_elem *) 378995e01b7SJan Schmidt (uintptr_t)node->aux; 3798da6d581SJan Schmidt list_add(&new_ref->list, &ref->list); 3808da6d581SJan Schmidt } 3818da6d581SJan Schmidt ulist_reinit(parents); 3828da6d581SJan Schmidt } 3838da6d581SJan Schmidt 3848da6d581SJan Schmidt ulist_free(parents); 3858da6d581SJan Schmidt return ret; 3868da6d581SJan Schmidt } 3878da6d581SJan Schmidt 388d5c88b73SJan Schmidt static inline int ref_for_same_block(struct __prelim_ref *ref1, 389d5c88b73SJan Schmidt struct __prelim_ref *ref2) 390d5c88b73SJan Schmidt { 391d5c88b73SJan Schmidt if (ref1->level != ref2->level) 392d5c88b73SJan Schmidt return 0; 393d5c88b73SJan Schmidt if (ref1->root_id != ref2->root_id) 394d5c88b73SJan Schmidt return 0; 395d5c88b73SJan Schmidt if (ref1->key_for_search.type != ref2->key_for_search.type) 396d5c88b73SJan Schmidt return 0; 397d5c88b73SJan Schmidt if (ref1->key_for_search.objectid != ref2->key_for_search.objectid) 398d5c88b73SJan Schmidt return 0; 399d5c88b73SJan Schmidt if (ref1->key_for_search.offset != ref2->key_for_search.offset) 400d5c88b73SJan Schmidt return 0; 401d5c88b73SJan Schmidt if (ref1->parent != ref2->parent) 402d5c88b73SJan Schmidt return 0; 403d5c88b73SJan Schmidt 404d5c88b73SJan Schmidt return 1; 405d5c88b73SJan Schmidt } 406d5c88b73SJan Schmidt 407d5c88b73SJan Schmidt /* 408d5c88b73SJan Schmidt * read tree blocks and add keys where required. 409d5c88b73SJan Schmidt */ 410d5c88b73SJan Schmidt static int __add_missing_keys(struct btrfs_fs_info *fs_info, 411d5c88b73SJan Schmidt struct list_head *head) 412d5c88b73SJan Schmidt { 413d5c88b73SJan Schmidt struct list_head *pos; 414d5c88b73SJan Schmidt struct extent_buffer *eb; 415d5c88b73SJan Schmidt 416d5c88b73SJan Schmidt list_for_each(pos, head) { 417d5c88b73SJan Schmidt struct __prelim_ref *ref; 418d5c88b73SJan Schmidt ref = list_entry(pos, struct __prelim_ref, list); 419d5c88b73SJan Schmidt 420d5c88b73SJan Schmidt if (ref->parent) 421d5c88b73SJan Schmidt continue; 422d5c88b73SJan Schmidt if (ref->key_for_search.type) 423d5c88b73SJan Schmidt continue; 424d5c88b73SJan Schmidt BUG_ON(!ref->wanted_disk_byte); 425d5c88b73SJan Schmidt eb = read_tree_block(fs_info->tree_root, ref->wanted_disk_byte, 426d5c88b73SJan Schmidt fs_info->tree_root->leafsize, 0); 427d5c88b73SJan Schmidt BUG_ON(!eb); 428d5c88b73SJan Schmidt btrfs_tree_read_lock(eb); 429d5c88b73SJan Schmidt if (btrfs_header_level(eb) == 0) 430d5c88b73SJan Schmidt btrfs_item_key_to_cpu(eb, &ref->key_for_search, 0); 431d5c88b73SJan Schmidt else 432d5c88b73SJan Schmidt btrfs_node_key_to_cpu(eb, &ref->key_for_search, 0); 433d5c88b73SJan Schmidt btrfs_tree_read_unlock(eb); 434d5c88b73SJan Schmidt free_extent_buffer(eb); 435d5c88b73SJan Schmidt } 436d5c88b73SJan Schmidt return 0; 437d5c88b73SJan Schmidt } 438d5c88b73SJan Schmidt 4398da6d581SJan Schmidt /* 4408da6d581SJan Schmidt * merge two lists of backrefs and adjust counts accordingly 4418da6d581SJan Schmidt * 4428da6d581SJan Schmidt * mode = 1: merge identical keys, if key is set 443d5c88b73SJan Schmidt * FIXME: if we add more keys in __add_prelim_ref, we can merge more here. 444d5c88b73SJan Schmidt * additionally, we could even add a key range for the blocks we 445d5c88b73SJan Schmidt * looked into to merge even more (-> replace unresolved refs by those 446d5c88b73SJan Schmidt * having a parent). 4478da6d581SJan Schmidt * mode = 2: merge identical parents 4488da6d581SJan Schmidt */ 4498da6d581SJan Schmidt static int __merge_refs(struct list_head *head, int mode) 4508da6d581SJan Schmidt { 4518da6d581SJan Schmidt struct list_head *pos1; 4528da6d581SJan Schmidt 4538da6d581SJan Schmidt list_for_each(pos1, head) { 4548da6d581SJan Schmidt struct list_head *n2; 4558da6d581SJan Schmidt struct list_head *pos2; 4568da6d581SJan Schmidt struct __prelim_ref *ref1; 4578da6d581SJan Schmidt 4588da6d581SJan Schmidt ref1 = list_entry(pos1, struct __prelim_ref, list); 4598da6d581SJan Schmidt 4608da6d581SJan Schmidt for (pos2 = pos1->next, n2 = pos2->next; pos2 != head; 4618da6d581SJan Schmidt pos2 = n2, n2 = pos2->next) { 4628da6d581SJan Schmidt struct __prelim_ref *ref2; 463d5c88b73SJan Schmidt struct __prelim_ref *xchg; 4648da6d581SJan Schmidt 4658da6d581SJan Schmidt ref2 = list_entry(pos2, struct __prelim_ref, list); 4668da6d581SJan Schmidt 4678da6d581SJan Schmidt if (mode == 1) { 468d5c88b73SJan Schmidt if (!ref_for_same_block(ref1, ref2)) 4698da6d581SJan Schmidt continue; 470d5c88b73SJan Schmidt if (!ref1->parent && ref2->parent) { 471d5c88b73SJan Schmidt xchg = ref1; 472d5c88b73SJan Schmidt ref1 = ref2; 473d5c88b73SJan Schmidt ref2 = xchg; 474d5c88b73SJan Schmidt } 4758da6d581SJan Schmidt ref1->count += ref2->count; 4768da6d581SJan Schmidt } else { 4778da6d581SJan Schmidt if (ref1->parent != ref2->parent) 4788da6d581SJan Schmidt continue; 4798da6d581SJan Schmidt ref1->count += ref2->count; 4808da6d581SJan Schmidt } 4818da6d581SJan Schmidt list_del(&ref2->list); 4828da6d581SJan Schmidt kfree(ref2); 4838da6d581SJan Schmidt } 4848da6d581SJan Schmidt 4858da6d581SJan Schmidt } 4868da6d581SJan Schmidt return 0; 4878da6d581SJan Schmidt } 4888da6d581SJan Schmidt 4898da6d581SJan Schmidt /* 4908da6d581SJan Schmidt * add all currently queued delayed refs from this head whose seq nr is 4918da6d581SJan Schmidt * smaller or equal that seq to the list 4928da6d581SJan Schmidt */ 4938da6d581SJan Schmidt static int __add_delayed_refs(struct btrfs_delayed_ref_head *head, u64 seq, 4948da6d581SJan Schmidt struct list_head *prefs) 4958da6d581SJan Schmidt { 4968da6d581SJan Schmidt struct btrfs_delayed_extent_op *extent_op = head->extent_op; 4978da6d581SJan Schmidt struct rb_node *n = &head->node.rb_node; 498d5c88b73SJan Schmidt struct btrfs_key key; 499d5c88b73SJan Schmidt struct btrfs_key op_key = {0}; 5008da6d581SJan Schmidt int sgn; 501b1375d64SJan Schmidt int ret = 0; 5028da6d581SJan Schmidt 5038da6d581SJan Schmidt if (extent_op && extent_op->update_key) 504d5c88b73SJan Schmidt btrfs_disk_key_to_cpu(&op_key, &extent_op->key); 5058da6d581SJan Schmidt 5068da6d581SJan Schmidt while ((n = rb_prev(n))) { 5078da6d581SJan Schmidt struct btrfs_delayed_ref_node *node; 5088da6d581SJan Schmidt node = rb_entry(n, struct btrfs_delayed_ref_node, 5098da6d581SJan Schmidt rb_node); 5108da6d581SJan Schmidt if (node->bytenr != head->node.bytenr) 5118da6d581SJan Schmidt break; 5128da6d581SJan Schmidt WARN_ON(node->is_head); 5138da6d581SJan Schmidt 5148da6d581SJan Schmidt if (node->seq > seq) 5158da6d581SJan Schmidt continue; 5168da6d581SJan Schmidt 5178da6d581SJan Schmidt switch (node->action) { 5188da6d581SJan Schmidt case BTRFS_ADD_DELAYED_EXTENT: 5198da6d581SJan Schmidt case BTRFS_UPDATE_DELAYED_HEAD: 5208da6d581SJan Schmidt WARN_ON(1); 5218da6d581SJan Schmidt continue; 5228da6d581SJan Schmidt case BTRFS_ADD_DELAYED_REF: 5238da6d581SJan Schmidt sgn = 1; 5248da6d581SJan Schmidt break; 5258da6d581SJan Schmidt case BTRFS_DROP_DELAYED_REF: 5268da6d581SJan Schmidt sgn = -1; 5278da6d581SJan Schmidt break; 5288da6d581SJan Schmidt default: 5298da6d581SJan Schmidt BUG_ON(1); 5308da6d581SJan Schmidt } 5318da6d581SJan Schmidt switch (node->type) { 5328da6d581SJan Schmidt case BTRFS_TREE_BLOCK_REF_KEY: { 5338da6d581SJan Schmidt struct btrfs_delayed_tree_ref *ref; 5348da6d581SJan Schmidt 5358da6d581SJan Schmidt ref = btrfs_delayed_node_to_tree_ref(node); 536d5c88b73SJan Schmidt ret = __add_prelim_ref(prefs, ref->root, &op_key, 5378da6d581SJan Schmidt ref->level + 1, 0, node->bytenr, 5388da6d581SJan Schmidt node->ref_mod * sgn); 5398da6d581SJan Schmidt break; 5408da6d581SJan Schmidt } 5418da6d581SJan Schmidt case BTRFS_SHARED_BLOCK_REF_KEY: { 5428da6d581SJan Schmidt struct btrfs_delayed_tree_ref *ref; 5438da6d581SJan Schmidt 5448da6d581SJan Schmidt ref = btrfs_delayed_node_to_tree_ref(node); 545d5c88b73SJan Schmidt ret = __add_prelim_ref(prefs, ref->root, NULL, 5468da6d581SJan Schmidt ref->level + 1, ref->parent, 5478da6d581SJan Schmidt node->bytenr, 5488da6d581SJan Schmidt node->ref_mod * sgn); 5498da6d581SJan Schmidt break; 5508da6d581SJan Schmidt } 5518da6d581SJan Schmidt case BTRFS_EXTENT_DATA_REF_KEY: { 5528da6d581SJan Schmidt struct btrfs_delayed_data_ref *ref; 5538da6d581SJan Schmidt ref = btrfs_delayed_node_to_data_ref(node); 5548da6d581SJan Schmidt 5558da6d581SJan Schmidt key.objectid = ref->objectid; 5568da6d581SJan Schmidt key.type = BTRFS_EXTENT_DATA_KEY; 5578da6d581SJan Schmidt key.offset = ref->offset; 5588da6d581SJan Schmidt ret = __add_prelim_ref(prefs, ref->root, &key, 0, 0, 5598da6d581SJan Schmidt node->bytenr, 5608da6d581SJan Schmidt node->ref_mod * sgn); 5618da6d581SJan Schmidt break; 5628da6d581SJan Schmidt } 5638da6d581SJan Schmidt case BTRFS_SHARED_DATA_REF_KEY: { 5648da6d581SJan Schmidt struct btrfs_delayed_data_ref *ref; 5658da6d581SJan Schmidt 5668da6d581SJan Schmidt ref = btrfs_delayed_node_to_data_ref(node); 5678da6d581SJan Schmidt 5688da6d581SJan Schmidt key.objectid = ref->objectid; 5698da6d581SJan Schmidt key.type = BTRFS_EXTENT_DATA_KEY; 5708da6d581SJan Schmidt key.offset = ref->offset; 5718da6d581SJan Schmidt ret = __add_prelim_ref(prefs, ref->root, &key, 0, 5728da6d581SJan Schmidt ref->parent, node->bytenr, 5738da6d581SJan Schmidt node->ref_mod * sgn); 5748da6d581SJan Schmidt break; 5758da6d581SJan Schmidt } 5768da6d581SJan Schmidt default: 5778da6d581SJan Schmidt WARN_ON(1); 5788da6d581SJan Schmidt } 5798da6d581SJan Schmidt BUG_ON(ret); 5808da6d581SJan Schmidt } 5818da6d581SJan Schmidt 5828da6d581SJan Schmidt return 0; 5838da6d581SJan Schmidt } 5848da6d581SJan Schmidt 5858da6d581SJan Schmidt /* 5868da6d581SJan Schmidt * add all inline backrefs for bytenr to the list 5878da6d581SJan Schmidt */ 5888da6d581SJan Schmidt static int __add_inline_refs(struct btrfs_fs_info *fs_info, 5898da6d581SJan Schmidt struct btrfs_path *path, u64 bytenr, 590d5c88b73SJan Schmidt int *info_level, struct list_head *prefs) 5918da6d581SJan Schmidt { 592b1375d64SJan Schmidt int ret = 0; 5938da6d581SJan Schmidt int slot; 5948da6d581SJan Schmidt struct extent_buffer *leaf; 5958da6d581SJan Schmidt struct btrfs_key key; 5968da6d581SJan Schmidt unsigned long ptr; 5978da6d581SJan Schmidt unsigned long end; 5988da6d581SJan Schmidt struct btrfs_extent_item *ei; 5998da6d581SJan Schmidt u64 flags; 6008da6d581SJan Schmidt u64 item_size; 6018da6d581SJan Schmidt 6028da6d581SJan Schmidt /* 6038da6d581SJan Schmidt * enumerate all inline refs 6048da6d581SJan Schmidt */ 6058da6d581SJan Schmidt leaf = path->nodes[0]; 606dadcaf78SJan Schmidt slot = path->slots[0]; 6078da6d581SJan Schmidt 6088da6d581SJan Schmidt item_size = btrfs_item_size_nr(leaf, slot); 6098da6d581SJan Schmidt BUG_ON(item_size < sizeof(*ei)); 6108da6d581SJan Schmidt 6118da6d581SJan Schmidt ei = btrfs_item_ptr(leaf, slot, struct btrfs_extent_item); 6128da6d581SJan Schmidt flags = btrfs_extent_flags(leaf, ei); 6138da6d581SJan Schmidt 6148da6d581SJan Schmidt ptr = (unsigned long)(ei + 1); 6158da6d581SJan Schmidt end = (unsigned long)ei + item_size; 6168da6d581SJan Schmidt 6178da6d581SJan Schmidt if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) { 6188da6d581SJan Schmidt struct btrfs_tree_block_info *info; 6198da6d581SJan Schmidt 6208da6d581SJan Schmidt info = (struct btrfs_tree_block_info *)ptr; 6218da6d581SJan Schmidt *info_level = btrfs_tree_block_level(leaf, info); 6228da6d581SJan Schmidt ptr += sizeof(struct btrfs_tree_block_info); 6238da6d581SJan Schmidt BUG_ON(ptr > end); 6248da6d581SJan Schmidt } else { 6258da6d581SJan Schmidt BUG_ON(!(flags & BTRFS_EXTENT_FLAG_DATA)); 6268da6d581SJan Schmidt } 6278da6d581SJan Schmidt 6288da6d581SJan Schmidt while (ptr < end) { 6298da6d581SJan Schmidt struct btrfs_extent_inline_ref *iref; 6308da6d581SJan Schmidt u64 offset; 6318da6d581SJan Schmidt int type; 6328da6d581SJan Schmidt 6338da6d581SJan Schmidt iref = (struct btrfs_extent_inline_ref *)ptr; 6348da6d581SJan Schmidt type = btrfs_extent_inline_ref_type(leaf, iref); 6358da6d581SJan Schmidt offset = btrfs_extent_inline_ref_offset(leaf, iref); 6368da6d581SJan Schmidt 6378da6d581SJan Schmidt switch (type) { 6388da6d581SJan Schmidt case BTRFS_SHARED_BLOCK_REF_KEY: 639d5c88b73SJan Schmidt ret = __add_prelim_ref(prefs, 0, NULL, 6408da6d581SJan Schmidt *info_level + 1, offset, 6418da6d581SJan Schmidt bytenr, 1); 6428da6d581SJan Schmidt break; 6438da6d581SJan Schmidt case BTRFS_SHARED_DATA_REF_KEY: { 6448da6d581SJan Schmidt struct btrfs_shared_data_ref *sdref; 6458da6d581SJan Schmidt int count; 6468da6d581SJan Schmidt 6478da6d581SJan Schmidt sdref = (struct btrfs_shared_data_ref *)(iref + 1); 6488da6d581SJan Schmidt count = btrfs_shared_data_ref_count(leaf, sdref); 6498da6d581SJan Schmidt ret = __add_prelim_ref(prefs, 0, NULL, 0, offset, 6508da6d581SJan Schmidt bytenr, count); 6518da6d581SJan Schmidt break; 6528da6d581SJan Schmidt } 6538da6d581SJan Schmidt case BTRFS_TREE_BLOCK_REF_KEY: 654d5c88b73SJan Schmidt ret = __add_prelim_ref(prefs, offset, NULL, 655d5c88b73SJan Schmidt *info_level + 1, 0, 656d5c88b73SJan Schmidt bytenr, 1); 6578da6d581SJan Schmidt break; 6588da6d581SJan Schmidt case BTRFS_EXTENT_DATA_REF_KEY: { 6598da6d581SJan Schmidt struct btrfs_extent_data_ref *dref; 6608da6d581SJan Schmidt int count; 6618da6d581SJan Schmidt u64 root; 6628da6d581SJan Schmidt 6638da6d581SJan Schmidt dref = (struct btrfs_extent_data_ref *)(&iref->offset); 6648da6d581SJan Schmidt count = btrfs_extent_data_ref_count(leaf, dref); 6658da6d581SJan Schmidt key.objectid = btrfs_extent_data_ref_objectid(leaf, 6668da6d581SJan Schmidt dref); 6678da6d581SJan Schmidt key.type = BTRFS_EXTENT_DATA_KEY; 6688da6d581SJan Schmidt key.offset = btrfs_extent_data_ref_offset(leaf, dref); 6698da6d581SJan Schmidt root = btrfs_extent_data_ref_root(leaf, dref); 670d5c88b73SJan Schmidt ret = __add_prelim_ref(prefs, root, &key, 0, 0, 671d5c88b73SJan Schmidt bytenr, count); 6728da6d581SJan Schmidt break; 6738da6d581SJan Schmidt } 6748da6d581SJan Schmidt default: 6758da6d581SJan Schmidt WARN_ON(1); 6768da6d581SJan Schmidt } 6778da6d581SJan Schmidt BUG_ON(ret); 6788da6d581SJan Schmidt ptr += btrfs_extent_inline_ref_size(type); 6798da6d581SJan Schmidt } 6808da6d581SJan Schmidt 6818da6d581SJan Schmidt return 0; 6828da6d581SJan Schmidt } 6838da6d581SJan Schmidt 6848da6d581SJan Schmidt /* 6858da6d581SJan Schmidt * add all non-inline backrefs for bytenr to the list 6868da6d581SJan Schmidt */ 6878da6d581SJan Schmidt static int __add_keyed_refs(struct btrfs_fs_info *fs_info, 6888da6d581SJan Schmidt struct btrfs_path *path, u64 bytenr, 689d5c88b73SJan Schmidt int info_level, struct list_head *prefs) 6908da6d581SJan Schmidt { 6918da6d581SJan Schmidt struct btrfs_root *extent_root = fs_info->extent_root; 6928da6d581SJan Schmidt int ret; 6938da6d581SJan Schmidt int slot; 6948da6d581SJan Schmidt struct extent_buffer *leaf; 6958da6d581SJan Schmidt struct btrfs_key key; 6968da6d581SJan Schmidt 6978da6d581SJan Schmidt while (1) { 6988da6d581SJan Schmidt ret = btrfs_next_item(extent_root, path); 6998da6d581SJan Schmidt if (ret < 0) 7008da6d581SJan Schmidt break; 7018da6d581SJan Schmidt if (ret) { 7028da6d581SJan Schmidt ret = 0; 7038da6d581SJan Schmidt break; 7048da6d581SJan Schmidt } 7058da6d581SJan Schmidt 7068da6d581SJan Schmidt slot = path->slots[0]; 7078da6d581SJan Schmidt leaf = path->nodes[0]; 7088da6d581SJan Schmidt btrfs_item_key_to_cpu(leaf, &key, slot); 7098da6d581SJan Schmidt 7108da6d581SJan Schmidt if (key.objectid != bytenr) 7118da6d581SJan Schmidt break; 7128da6d581SJan Schmidt if (key.type < BTRFS_TREE_BLOCK_REF_KEY) 7138da6d581SJan Schmidt continue; 7148da6d581SJan Schmidt if (key.type > BTRFS_SHARED_DATA_REF_KEY) 7158da6d581SJan Schmidt break; 7168da6d581SJan Schmidt 7178da6d581SJan Schmidt switch (key.type) { 7188da6d581SJan Schmidt case BTRFS_SHARED_BLOCK_REF_KEY: 719d5c88b73SJan Schmidt ret = __add_prelim_ref(prefs, 0, NULL, 7208da6d581SJan Schmidt info_level + 1, key.offset, 7218da6d581SJan Schmidt bytenr, 1); 7228da6d581SJan Schmidt break; 7238da6d581SJan Schmidt case BTRFS_SHARED_DATA_REF_KEY: { 7248da6d581SJan Schmidt struct btrfs_shared_data_ref *sdref; 7258da6d581SJan Schmidt int count; 7268da6d581SJan Schmidt 7278da6d581SJan Schmidt sdref = btrfs_item_ptr(leaf, slot, 7288da6d581SJan Schmidt struct btrfs_shared_data_ref); 7298da6d581SJan Schmidt count = btrfs_shared_data_ref_count(leaf, sdref); 7308da6d581SJan Schmidt ret = __add_prelim_ref(prefs, 0, NULL, 0, key.offset, 7318da6d581SJan Schmidt bytenr, count); 7328da6d581SJan Schmidt break; 7338da6d581SJan Schmidt } 7348da6d581SJan Schmidt case BTRFS_TREE_BLOCK_REF_KEY: 735d5c88b73SJan Schmidt ret = __add_prelim_ref(prefs, key.offset, NULL, 736d5c88b73SJan Schmidt info_level + 1, 0, 737d5c88b73SJan Schmidt bytenr, 1); 7388da6d581SJan Schmidt break; 7398da6d581SJan Schmidt case BTRFS_EXTENT_DATA_REF_KEY: { 7408da6d581SJan Schmidt struct btrfs_extent_data_ref *dref; 7418da6d581SJan Schmidt int count; 7428da6d581SJan Schmidt u64 root; 7438da6d581SJan Schmidt 7448da6d581SJan Schmidt dref = btrfs_item_ptr(leaf, slot, 7458da6d581SJan Schmidt struct btrfs_extent_data_ref); 7468da6d581SJan Schmidt count = btrfs_extent_data_ref_count(leaf, dref); 7478da6d581SJan Schmidt key.objectid = btrfs_extent_data_ref_objectid(leaf, 7488da6d581SJan Schmidt dref); 7498da6d581SJan Schmidt key.type = BTRFS_EXTENT_DATA_KEY; 7508da6d581SJan Schmidt key.offset = btrfs_extent_data_ref_offset(leaf, dref); 7518da6d581SJan Schmidt root = btrfs_extent_data_ref_root(leaf, dref); 7528da6d581SJan Schmidt ret = __add_prelim_ref(prefs, root, &key, 0, 0, 7538da6d581SJan Schmidt bytenr, count); 7548da6d581SJan Schmidt break; 7558da6d581SJan Schmidt } 7568da6d581SJan Schmidt default: 7578da6d581SJan Schmidt WARN_ON(1); 7588da6d581SJan Schmidt } 7598da6d581SJan Schmidt BUG_ON(ret); 7608da6d581SJan Schmidt } 7618da6d581SJan Schmidt 7628da6d581SJan Schmidt return ret; 7638da6d581SJan Schmidt } 7648da6d581SJan Schmidt 7658da6d581SJan Schmidt /* 7668da6d581SJan Schmidt * this adds all existing backrefs (inline backrefs, backrefs and delayed 7678da6d581SJan Schmidt * refs) for the given bytenr to the refs list, merges duplicates and resolves 7688da6d581SJan Schmidt * indirect refs to their parent bytenr. 7698da6d581SJan Schmidt * When roots are found, they're added to the roots list 7708da6d581SJan Schmidt * 7718da6d581SJan Schmidt * FIXME some caching might speed things up 7728da6d581SJan Schmidt */ 7738da6d581SJan Schmidt static int find_parent_nodes(struct btrfs_trans_handle *trans, 7748da6d581SJan Schmidt struct btrfs_fs_info *fs_info, u64 bytenr, 775097b8a7cSJan Schmidt u64 time_seq, struct ulist *refs, 776097b8a7cSJan Schmidt struct ulist *roots, const u64 *extent_item_pos) 7778da6d581SJan Schmidt { 7788da6d581SJan Schmidt struct btrfs_key key; 7798da6d581SJan Schmidt struct btrfs_path *path; 7808da6d581SJan Schmidt struct btrfs_delayed_ref_root *delayed_refs = NULL; 781d3b01064SLi Zefan struct btrfs_delayed_ref_head *head; 7828da6d581SJan Schmidt int info_level = 0; 7838da6d581SJan Schmidt int ret; 7847a3ae2f8SJan Schmidt int search_commit_root = (trans == BTRFS_BACKREF_SEARCH_COMMIT_ROOT); 7858da6d581SJan Schmidt struct list_head prefs_delayed; 7868da6d581SJan Schmidt struct list_head prefs; 7878da6d581SJan Schmidt struct __prelim_ref *ref; 7888da6d581SJan Schmidt 7898da6d581SJan Schmidt INIT_LIST_HEAD(&prefs); 7908da6d581SJan Schmidt INIT_LIST_HEAD(&prefs_delayed); 7918da6d581SJan Schmidt 7928da6d581SJan Schmidt key.objectid = bytenr; 7938da6d581SJan Schmidt key.type = BTRFS_EXTENT_ITEM_KEY; 7948da6d581SJan Schmidt key.offset = (u64)-1; 7958da6d581SJan Schmidt 7968da6d581SJan Schmidt path = btrfs_alloc_path(); 7978da6d581SJan Schmidt if (!path) 7988da6d581SJan Schmidt return -ENOMEM; 7997a3ae2f8SJan Schmidt path->search_commit_root = !!search_commit_root; 8008da6d581SJan Schmidt 8018da6d581SJan Schmidt /* 8028da6d581SJan Schmidt * grab both a lock on the path and a lock on the delayed ref head. 8038da6d581SJan Schmidt * We need both to get a consistent picture of how the refs look 8048da6d581SJan Schmidt * at a specified point in time 8058da6d581SJan Schmidt */ 8068da6d581SJan Schmidt again: 807d3b01064SLi Zefan head = NULL; 808d3b01064SLi Zefan 8098da6d581SJan Schmidt ret = btrfs_search_slot(trans, fs_info->extent_root, &key, path, 0, 0); 8108da6d581SJan Schmidt if (ret < 0) 8118da6d581SJan Schmidt goto out; 8128da6d581SJan Schmidt BUG_ON(ret == 0); 8138da6d581SJan Schmidt 8147a3ae2f8SJan Schmidt if (trans != BTRFS_BACKREF_SEARCH_COMMIT_ROOT) { 8158da6d581SJan Schmidt /* 8167a3ae2f8SJan Schmidt * look if there are updates for this ref queued and lock the 8177a3ae2f8SJan Schmidt * head 8188da6d581SJan Schmidt */ 8198da6d581SJan Schmidt delayed_refs = &trans->transaction->delayed_refs; 8208da6d581SJan Schmidt spin_lock(&delayed_refs->lock); 8218da6d581SJan Schmidt head = btrfs_find_delayed_ref_head(trans, bytenr); 8228da6d581SJan Schmidt if (head) { 8238da6d581SJan Schmidt if (!mutex_trylock(&head->mutex)) { 8248da6d581SJan Schmidt atomic_inc(&head->node.refs); 8258da6d581SJan Schmidt spin_unlock(&delayed_refs->lock); 8268da6d581SJan Schmidt 8278da6d581SJan Schmidt btrfs_release_path(path); 8288da6d581SJan Schmidt 8298da6d581SJan Schmidt /* 8308da6d581SJan Schmidt * Mutex was contended, block until it's 8318da6d581SJan Schmidt * released and try again 8328da6d581SJan Schmidt */ 8338da6d581SJan Schmidt mutex_lock(&head->mutex); 8348da6d581SJan Schmidt mutex_unlock(&head->mutex); 8358da6d581SJan Schmidt btrfs_put_delayed_ref(&head->node); 8368da6d581SJan Schmidt goto again; 8378da6d581SJan Schmidt } 838097b8a7cSJan Schmidt ret = __add_delayed_refs(head, time_seq, 8398445f61cSJan Schmidt &prefs_delayed); 840155725c9SJan Schmidt mutex_unlock(&head->mutex); 841d3b01064SLi Zefan if (ret) { 842d3b01064SLi Zefan spin_unlock(&delayed_refs->lock); 8438da6d581SJan Schmidt goto out; 8448da6d581SJan Schmidt } 845d3b01064SLi Zefan } 8468da6d581SJan Schmidt spin_unlock(&delayed_refs->lock); 8477a3ae2f8SJan Schmidt } 8488da6d581SJan Schmidt 8498da6d581SJan Schmidt if (path->slots[0]) { 8508da6d581SJan Schmidt struct extent_buffer *leaf; 8518da6d581SJan Schmidt int slot; 8528da6d581SJan Schmidt 853dadcaf78SJan Schmidt path->slots[0]--; 8548da6d581SJan Schmidt leaf = path->nodes[0]; 855dadcaf78SJan Schmidt slot = path->slots[0]; 8568da6d581SJan Schmidt btrfs_item_key_to_cpu(leaf, &key, slot); 8578da6d581SJan Schmidt if (key.objectid == bytenr && 8588da6d581SJan Schmidt key.type == BTRFS_EXTENT_ITEM_KEY) { 8598da6d581SJan Schmidt ret = __add_inline_refs(fs_info, path, bytenr, 860d5c88b73SJan Schmidt &info_level, &prefs); 8618da6d581SJan Schmidt if (ret) 8628da6d581SJan Schmidt goto out; 863d5c88b73SJan Schmidt ret = __add_keyed_refs(fs_info, path, bytenr, 8648da6d581SJan Schmidt info_level, &prefs); 8658da6d581SJan Schmidt if (ret) 8668da6d581SJan Schmidt goto out; 8678da6d581SJan Schmidt } 8688da6d581SJan Schmidt } 8698da6d581SJan Schmidt btrfs_release_path(path); 8708da6d581SJan Schmidt 8718da6d581SJan Schmidt list_splice_init(&prefs_delayed, &prefs); 8728da6d581SJan Schmidt 873d5c88b73SJan Schmidt ret = __add_missing_keys(fs_info, &prefs); 874d5c88b73SJan Schmidt if (ret) 875d5c88b73SJan Schmidt goto out; 876d5c88b73SJan Schmidt 8778da6d581SJan Schmidt ret = __merge_refs(&prefs, 1); 8788da6d581SJan Schmidt if (ret) 8798da6d581SJan Schmidt goto out; 8808da6d581SJan Schmidt 8818445f61cSJan Schmidt ret = __resolve_indirect_refs(fs_info, search_commit_root, time_seq, 8828445f61cSJan Schmidt &prefs, extent_item_pos); 8838da6d581SJan Schmidt if (ret) 8848da6d581SJan Schmidt goto out; 8858da6d581SJan Schmidt 8868da6d581SJan Schmidt ret = __merge_refs(&prefs, 2); 8878da6d581SJan Schmidt if (ret) 8888da6d581SJan Schmidt goto out; 8898da6d581SJan Schmidt 8908da6d581SJan Schmidt while (!list_empty(&prefs)) { 8918da6d581SJan Schmidt ref = list_first_entry(&prefs, struct __prelim_ref, list); 8928da6d581SJan Schmidt list_del(&ref->list); 8938da6d581SJan Schmidt if (ref->count < 0) 8948da6d581SJan Schmidt WARN_ON(1); 8958da6d581SJan Schmidt if (ref->count && ref->root_id && ref->parent == 0) { 8968da6d581SJan Schmidt /* no parent == root of tree */ 8978da6d581SJan Schmidt ret = ulist_add(roots, ref->root_id, 0, GFP_NOFS); 8988da6d581SJan Schmidt BUG_ON(ret < 0); 8998da6d581SJan Schmidt } 9008da6d581SJan Schmidt if (ref->count && ref->parent) { 901976b1908SJan Schmidt struct extent_inode_elem *eie = NULL; 9023301958bSJan Schmidt if (extent_item_pos && !ref->inode_list) { 903976b1908SJan Schmidt u32 bsz; 904976b1908SJan Schmidt struct extent_buffer *eb; 905976b1908SJan Schmidt bsz = btrfs_level_size(fs_info->extent_root, 906976b1908SJan Schmidt info_level); 907976b1908SJan Schmidt eb = read_tree_block(fs_info->extent_root, 908976b1908SJan Schmidt ref->parent, bsz, 0); 909976b1908SJan Schmidt BUG_ON(!eb); 910976b1908SJan Schmidt ret = find_extent_in_eb(eb, bytenr, 911976b1908SJan Schmidt *extent_item_pos, &eie); 9123301958bSJan Schmidt ref->inode_list = eie; 913976b1908SJan Schmidt free_extent_buffer(eb); 914976b1908SJan Schmidt } 9153301958bSJan Schmidt ret = ulist_add_merge(refs, ref->parent, 916995e01b7SJan Schmidt (uintptr_t)ref->inode_list, 91734d73f54SAlexander Block (u64 *)&eie, GFP_NOFS); 9183301958bSJan Schmidt if (!ret && extent_item_pos) { 9193301958bSJan Schmidt /* 9203301958bSJan Schmidt * we've recorded that parent, so we must extend 9213301958bSJan Schmidt * its inode list here 9223301958bSJan Schmidt */ 9233301958bSJan Schmidt BUG_ON(!eie); 9243301958bSJan Schmidt while (eie->next) 9253301958bSJan Schmidt eie = eie->next; 9263301958bSJan Schmidt eie->next = ref->inode_list; 9273301958bSJan Schmidt } 9288da6d581SJan Schmidt BUG_ON(ret < 0); 9298da6d581SJan Schmidt } 9308da6d581SJan Schmidt kfree(ref); 9318da6d581SJan Schmidt } 9328da6d581SJan Schmidt 9338da6d581SJan Schmidt out: 9348da6d581SJan Schmidt btrfs_free_path(path); 9358da6d581SJan Schmidt while (!list_empty(&prefs)) { 9368da6d581SJan Schmidt ref = list_first_entry(&prefs, struct __prelim_ref, list); 9378da6d581SJan Schmidt list_del(&ref->list); 9388da6d581SJan Schmidt kfree(ref); 9398da6d581SJan Schmidt } 9408da6d581SJan Schmidt while (!list_empty(&prefs_delayed)) { 9418da6d581SJan Schmidt ref = list_first_entry(&prefs_delayed, struct __prelim_ref, 9428da6d581SJan Schmidt list); 9438da6d581SJan Schmidt list_del(&ref->list); 9448da6d581SJan Schmidt kfree(ref); 9458da6d581SJan Schmidt } 9468da6d581SJan Schmidt 9478da6d581SJan Schmidt return ret; 9488da6d581SJan Schmidt } 9498da6d581SJan Schmidt 950976b1908SJan Schmidt static void free_leaf_list(struct ulist *blocks) 951976b1908SJan Schmidt { 952976b1908SJan Schmidt struct ulist_node *node = NULL; 953976b1908SJan Schmidt struct extent_inode_elem *eie; 954976b1908SJan Schmidt struct extent_inode_elem *eie_next; 955976b1908SJan Schmidt struct ulist_iterator uiter; 956976b1908SJan Schmidt 957976b1908SJan Schmidt ULIST_ITER_INIT(&uiter); 958976b1908SJan Schmidt while ((node = ulist_next(blocks, &uiter))) { 959976b1908SJan Schmidt if (!node->aux) 960976b1908SJan Schmidt continue; 961995e01b7SJan Schmidt eie = (struct extent_inode_elem *)(uintptr_t)node->aux; 962976b1908SJan Schmidt for (; eie; eie = eie_next) { 963976b1908SJan Schmidt eie_next = eie->next; 964976b1908SJan Schmidt kfree(eie); 965976b1908SJan Schmidt } 966976b1908SJan Schmidt node->aux = 0; 967976b1908SJan Schmidt } 968976b1908SJan Schmidt 969976b1908SJan Schmidt ulist_free(blocks); 970976b1908SJan Schmidt } 971976b1908SJan Schmidt 9728da6d581SJan Schmidt /* 9738da6d581SJan Schmidt * Finds all leafs with a reference to the specified combination of bytenr and 9748da6d581SJan Schmidt * offset. key_list_head will point to a list of corresponding keys (caller must 9758da6d581SJan Schmidt * free each list element). The leafs will be stored in the leafs ulist, which 9768da6d581SJan Schmidt * must be freed with ulist_free. 9778da6d581SJan Schmidt * 9788da6d581SJan Schmidt * returns 0 on success, <0 on error 9798da6d581SJan Schmidt */ 9808da6d581SJan Schmidt static int btrfs_find_all_leafs(struct btrfs_trans_handle *trans, 9818da6d581SJan Schmidt struct btrfs_fs_info *fs_info, u64 bytenr, 982097b8a7cSJan Schmidt u64 time_seq, struct ulist **leafs, 983976b1908SJan Schmidt const u64 *extent_item_pos) 9848da6d581SJan Schmidt { 9858da6d581SJan Schmidt struct ulist *tmp; 9868da6d581SJan Schmidt int ret; 9878da6d581SJan Schmidt 9888da6d581SJan Schmidt tmp = ulist_alloc(GFP_NOFS); 9898da6d581SJan Schmidt if (!tmp) 9908da6d581SJan Schmidt return -ENOMEM; 9918da6d581SJan Schmidt *leafs = ulist_alloc(GFP_NOFS); 9928da6d581SJan Schmidt if (!*leafs) { 9938da6d581SJan Schmidt ulist_free(tmp); 9948da6d581SJan Schmidt return -ENOMEM; 9958da6d581SJan Schmidt } 9968da6d581SJan Schmidt 997097b8a7cSJan Schmidt ret = find_parent_nodes(trans, fs_info, bytenr, 9988445f61cSJan Schmidt time_seq, *leafs, tmp, extent_item_pos); 9998da6d581SJan Schmidt ulist_free(tmp); 10008da6d581SJan Schmidt 10018da6d581SJan Schmidt if (ret < 0 && ret != -ENOENT) { 1002976b1908SJan Schmidt free_leaf_list(*leafs); 10038da6d581SJan Schmidt return ret; 10048da6d581SJan Schmidt } 10058da6d581SJan Schmidt 10068da6d581SJan Schmidt return 0; 10078da6d581SJan Schmidt } 10088da6d581SJan Schmidt 10098da6d581SJan Schmidt /* 10108da6d581SJan Schmidt * walk all backrefs for a given extent to find all roots that reference this 10118da6d581SJan Schmidt * extent. Walking a backref means finding all extents that reference this 10128da6d581SJan Schmidt * extent and in turn walk the backrefs of those, too. Naturally this is a 10138da6d581SJan Schmidt * recursive process, but here it is implemented in an iterative fashion: We 10148da6d581SJan Schmidt * find all referencing extents for the extent in question and put them on a 10158da6d581SJan Schmidt * list. In turn, we find all referencing extents for those, further appending 10168da6d581SJan Schmidt * to the list. The way we iterate the list allows adding more elements after 10178da6d581SJan Schmidt * the current while iterating. The process stops when we reach the end of the 10188da6d581SJan Schmidt * list. Found roots are added to the roots list. 10198da6d581SJan Schmidt * 10208da6d581SJan Schmidt * returns 0 on success, < 0 on error. 10218da6d581SJan Schmidt */ 10228da6d581SJan Schmidt int btrfs_find_all_roots(struct btrfs_trans_handle *trans, 10238da6d581SJan Schmidt struct btrfs_fs_info *fs_info, u64 bytenr, 1024097b8a7cSJan Schmidt u64 time_seq, struct ulist **roots) 10258da6d581SJan Schmidt { 10268da6d581SJan Schmidt struct ulist *tmp; 10278da6d581SJan Schmidt struct ulist_node *node = NULL; 1028cd1b413cSJan Schmidt struct ulist_iterator uiter; 10298da6d581SJan Schmidt int ret; 10308da6d581SJan Schmidt 10318da6d581SJan Schmidt tmp = ulist_alloc(GFP_NOFS); 10328da6d581SJan Schmidt if (!tmp) 10338da6d581SJan Schmidt return -ENOMEM; 10348da6d581SJan Schmidt *roots = ulist_alloc(GFP_NOFS); 10358da6d581SJan Schmidt if (!*roots) { 10368da6d581SJan Schmidt ulist_free(tmp); 10378da6d581SJan Schmidt return -ENOMEM; 10388da6d581SJan Schmidt } 10398da6d581SJan Schmidt 1040cd1b413cSJan Schmidt ULIST_ITER_INIT(&uiter); 10418da6d581SJan Schmidt while (1) { 1042097b8a7cSJan Schmidt ret = find_parent_nodes(trans, fs_info, bytenr, 10438445f61cSJan Schmidt time_seq, tmp, *roots, NULL); 10448da6d581SJan Schmidt if (ret < 0 && ret != -ENOENT) { 10458da6d581SJan Schmidt ulist_free(tmp); 10468da6d581SJan Schmidt ulist_free(*roots); 10478da6d581SJan Schmidt return ret; 10488da6d581SJan Schmidt } 1049cd1b413cSJan Schmidt node = ulist_next(tmp, &uiter); 10508da6d581SJan Schmidt if (!node) 10518da6d581SJan Schmidt break; 10528da6d581SJan Schmidt bytenr = node->val; 10538da6d581SJan Schmidt } 10548da6d581SJan Schmidt 10558da6d581SJan Schmidt ulist_free(tmp); 10568da6d581SJan Schmidt return 0; 10578da6d581SJan Schmidt } 10588da6d581SJan Schmidt 10598da6d581SJan Schmidt 1060a542ad1bSJan Schmidt static int __inode_info(u64 inum, u64 ioff, u8 key_type, 1061a542ad1bSJan Schmidt struct btrfs_root *fs_root, struct btrfs_path *path, 1062a542ad1bSJan Schmidt struct btrfs_key *found_key) 1063a542ad1bSJan Schmidt { 1064a542ad1bSJan Schmidt int ret; 1065a542ad1bSJan Schmidt struct btrfs_key key; 1066a542ad1bSJan Schmidt struct extent_buffer *eb; 1067a542ad1bSJan Schmidt 1068a542ad1bSJan Schmidt key.type = key_type; 1069a542ad1bSJan Schmidt key.objectid = inum; 1070a542ad1bSJan Schmidt key.offset = ioff; 1071a542ad1bSJan Schmidt 1072a542ad1bSJan Schmidt ret = btrfs_search_slot(NULL, fs_root, &key, path, 0, 0); 1073a542ad1bSJan Schmidt if (ret < 0) 1074a542ad1bSJan Schmidt return ret; 1075a542ad1bSJan Schmidt 1076a542ad1bSJan Schmidt eb = path->nodes[0]; 1077a542ad1bSJan Schmidt if (ret && path->slots[0] >= btrfs_header_nritems(eb)) { 1078a542ad1bSJan Schmidt ret = btrfs_next_leaf(fs_root, path); 1079a542ad1bSJan Schmidt if (ret) 1080a542ad1bSJan Schmidt return ret; 1081a542ad1bSJan Schmidt eb = path->nodes[0]; 1082a542ad1bSJan Schmidt } 1083a542ad1bSJan Schmidt 1084a542ad1bSJan Schmidt btrfs_item_key_to_cpu(eb, found_key, path->slots[0]); 1085a542ad1bSJan Schmidt if (found_key->type != key.type || found_key->objectid != key.objectid) 1086a542ad1bSJan Schmidt return 1; 1087a542ad1bSJan Schmidt 1088a542ad1bSJan Schmidt return 0; 1089a542ad1bSJan Schmidt } 1090a542ad1bSJan Schmidt 1091a542ad1bSJan Schmidt /* 1092a542ad1bSJan Schmidt * this makes the path point to (inum INODE_ITEM ioff) 1093a542ad1bSJan Schmidt */ 1094a542ad1bSJan Schmidt int inode_item_info(u64 inum, u64 ioff, struct btrfs_root *fs_root, 1095a542ad1bSJan Schmidt struct btrfs_path *path) 1096a542ad1bSJan Schmidt { 1097a542ad1bSJan Schmidt struct btrfs_key key; 1098a542ad1bSJan Schmidt return __inode_info(inum, ioff, BTRFS_INODE_ITEM_KEY, fs_root, path, 1099a542ad1bSJan Schmidt &key); 1100a542ad1bSJan Schmidt } 1101a542ad1bSJan Schmidt 1102a542ad1bSJan Schmidt static int inode_ref_info(u64 inum, u64 ioff, struct btrfs_root *fs_root, 1103a542ad1bSJan Schmidt struct btrfs_path *path, 1104a542ad1bSJan Schmidt struct btrfs_key *found_key) 1105a542ad1bSJan Schmidt { 1106a542ad1bSJan Schmidt return __inode_info(inum, ioff, BTRFS_INODE_REF_KEY, fs_root, path, 1107a542ad1bSJan Schmidt found_key); 1108a542ad1bSJan Schmidt } 1109a542ad1bSJan Schmidt 1110f186373fSMark Fasheh int btrfs_find_one_extref(struct btrfs_root *root, u64 inode_objectid, 1111f186373fSMark Fasheh u64 start_off, struct btrfs_path *path, 1112f186373fSMark Fasheh struct btrfs_inode_extref **ret_extref, 1113f186373fSMark Fasheh u64 *found_off) 1114f186373fSMark Fasheh { 1115f186373fSMark Fasheh int ret, slot; 1116f186373fSMark Fasheh struct btrfs_key key; 1117f186373fSMark Fasheh struct btrfs_key found_key; 1118f186373fSMark Fasheh struct btrfs_inode_extref *extref; 1119f186373fSMark Fasheh struct extent_buffer *leaf; 1120f186373fSMark Fasheh unsigned long ptr; 1121f186373fSMark Fasheh 1122f186373fSMark Fasheh key.objectid = inode_objectid; 1123f186373fSMark Fasheh btrfs_set_key_type(&key, BTRFS_INODE_EXTREF_KEY); 1124f186373fSMark Fasheh key.offset = start_off; 1125f186373fSMark Fasheh 1126f186373fSMark Fasheh ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); 1127f186373fSMark Fasheh if (ret < 0) 1128f186373fSMark Fasheh return ret; 1129f186373fSMark Fasheh 1130f186373fSMark Fasheh while (1) { 1131f186373fSMark Fasheh leaf = path->nodes[0]; 1132f186373fSMark Fasheh slot = path->slots[0]; 1133f186373fSMark Fasheh if (slot >= btrfs_header_nritems(leaf)) { 1134f186373fSMark Fasheh /* 1135f186373fSMark Fasheh * If the item at offset is not found, 1136f186373fSMark Fasheh * btrfs_search_slot will point us to the slot 1137f186373fSMark Fasheh * where it should be inserted. In our case 1138f186373fSMark Fasheh * that will be the slot directly before the 1139f186373fSMark Fasheh * next INODE_REF_KEY_V2 item. In the case 1140f186373fSMark Fasheh * that we're pointing to the last slot in a 1141f186373fSMark Fasheh * leaf, we must move one leaf over. 1142f186373fSMark Fasheh */ 1143f186373fSMark Fasheh ret = btrfs_next_leaf(root, path); 1144f186373fSMark Fasheh if (ret) { 1145f186373fSMark Fasheh if (ret >= 1) 1146f186373fSMark Fasheh ret = -ENOENT; 1147f186373fSMark Fasheh break; 1148f186373fSMark Fasheh } 1149f186373fSMark Fasheh continue; 1150f186373fSMark Fasheh } 1151f186373fSMark Fasheh 1152f186373fSMark Fasheh btrfs_item_key_to_cpu(leaf, &found_key, slot); 1153f186373fSMark Fasheh 1154f186373fSMark Fasheh /* 1155f186373fSMark Fasheh * Check that we're still looking at an extended ref key for 1156f186373fSMark Fasheh * this particular objectid. If we have different 1157f186373fSMark Fasheh * objectid or type then there are no more to be found 1158f186373fSMark Fasheh * in the tree and we can exit. 1159f186373fSMark Fasheh */ 1160f186373fSMark Fasheh ret = -ENOENT; 1161f186373fSMark Fasheh if (found_key.objectid != inode_objectid) 1162f186373fSMark Fasheh break; 1163f186373fSMark Fasheh if (btrfs_key_type(&found_key) != BTRFS_INODE_EXTREF_KEY) 1164f186373fSMark Fasheh break; 1165f186373fSMark Fasheh 1166f186373fSMark Fasheh ret = 0; 1167f186373fSMark Fasheh ptr = btrfs_item_ptr_offset(leaf, path->slots[0]); 1168f186373fSMark Fasheh extref = (struct btrfs_inode_extref *)ptr; 1169f186373fSMark Fasheh *ret_extref = extref; 1170f186373fSMark Fasheh if (found_off) 1171f186373fSMark Fasheh *found_off = found_key.offset; 1172f186373fSMark Fasheh break; 1173f186373fSMark Fasheh } 1174f186373fSMark Fasheh 1175f186373fSMark Fasheh return ret; 1176f186373fSMark Fasheh } 1177f186373fSMark Fasheh 1178d24bec3aSMark Fasheh static char *ref_to_path(struct btrfs_root *fs_root, 1179d24bec3aSMark Fasheh struct btrfs_path *path, 1180d24bec3aSMark Fasheh u32 name_len, unsigned long name_off, 1181a542ad1bSJan Schmidt struct extent_buffer *eb_in, u64 parent, 1182a542ad1bSJan Schmidt char *dest, u32 size) 1183a542ad1bSJan Schmidt { 1184a542ad1bSJan Schmidt int slot; 1185a542ad1bSJan Schmidt u64 next_inum; 1186a542ad1bSJan Schmidt int ret; 1187a542ad1bSJan Schmidt s64 bytes_left = size - 1; 1188a542ad1bSJan Schmidt struct extent_buffer *eb = eb_in; 1189a542ad1bSJan Schmidt struct btrfs_key found_key; 1190b916a59aSJan Schmidt int leave_spinning = path->leave_spinning; 1191d24bec3aSMark Fasheh struct btrfs_inode_ref *iref; 1192a542ad1bSJan Schmidt 1193a542ad1bSJan Schmidt if (bytes_left >= 0) 1194a542ad1bSJan Schmidt dest[bytes_left] = '\0'; 1195a542ad1bSJan Schmidt 1196b916a59aSJan Schmidt path->leave_spinning = 1; 1197a542ad1bSJan Schmidt while (1) { 1198d24bec3aSMark Fasheh bytes_left -= name_len; 1199a542ad1bSJan Schmidt if (bytes_left >= 0) 1200a542ad1bSJan Schmidt read_extent_buffer(eb, dest + bytes_left, 1201d24bec3aSMark Fasheh name_off, name_len); 1202b916a59aSJan Schmidt if (eb != eb_in) { 1203b916a59aSJan Schmidt btrfs_tree_read_unlock_blocking(eb); 1204a542ad1bSJan Schmidt free_extent_buffer(eb); 1205b916a59aSJan Schmidt } 1206a542ad1bSJan Schmidt ret = inode_ref_info(parent, 0, fs_root, path, &found_key); 12078f24b496SJan Schmidt if (ret > 0) 12088f24b496SJan Schmidt ret = -ENOENT; 1209a542ad1bSJan Schmidt if (ret) 1210a542ad1bSJan Schmidt break; 1211d24bec3aSMark Fasheh 1212a542ad1bSJan Schmidt next_inum = found_key.offset; 1213a542ad1bSJan Schmidt 1214a542ad1bSJan Schmidt /* regular exit ahead */ 1215a542ad1bSJan Schmidt if (parent == next_inum) 1216a542ad1bSJan Schmidt break; 1217a542ad1bSJan Schmidt 1218a542ad1bSJan Schmidt slot = path->slots[0]; 1219a542ad1bSJan Schmidt eb = path->nodes[0]; 1220a542ad1bSJan Schmidt /* make sure we can use eb after releasing the path */ 1221b916a59aSJan Schmidt if (eb != eb_in) { 1222a542ad1bSJan Schmidt atomic_inc(&eb->refs); 1223b916a59aSJan Schmidt btrfs_tree_read_lock(eb); 1224b916a59aSJan Schmidt btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK); 1225b916a59aSJan Schmidt } 1226a542ad1bSJan Schmidt btrfs_release_path(path); 1227a542ad1bSJan Schmidt iref = btrfs_item_ptr(eb, slot, struct btrfs_inode_ref); 1228d24bec3aSMark Fasheh 1229d24bec3aSMark Fasheh name_len = btrfs_inode_ref_name_len(eb, iref); 1230d24bec3aSMark Fasheh name_off = (unsigned long)(iref + 1); 1231d24bec3aSMark Fasheh 1232a542ad1bSJan Schmidt parent = next_inum; 1233a542ad1bSJan Schmidt --bytes_left; 1234a542ad1bSJan Schmidt if (bytes_left >= 0) 1235a542ad1bSJan Schmidt dest[bytes_left] = '/'; 1236a542ad1bSJan Schmidt } 1237a542ad1bSJan Schmidt 1238a542ad1bSJan Schmidt btrfs_release_path(path); 1239b916a59aSJan Schmidt path->leave_spinning = leave_spinning; 1240a542ad1bSJan Schmidt 1241a542ad1bSJan Schmidt if (ret) 1242a542ad1bSJan Schmidt return ERR_PTR(ret); 1243a542ad1bSJan Schmidt 1244a542ad1bSJan Schmidt return dest + bytes_left; 1245a542ad1bSJan Schmidt } 1246a542ad1bSJan Schmidt 1247a542ad1bSJan Schmidt /* 1248d24bec3aSMark Fasheh * this iterates to turn a btrfs_inode_ref into a full filesystem path. elements 1249d24bec3aSMark Fasheh * of the path are separated by '/' and the path is guaranteed to be 1250d24bec3aSMark Fasheh * 0-terminated. the path is only given within the current file system. 1251d24bec3aSMark Fasheh * Therefore, it never starts with a '/'. the caller is responsible to provide 1252d24bec3aSMark Fasheh * "size" bytes in "dest". the dest buffer will be filled backwards. finally, 1253d24bec3aSMark Fasheh * the start point of the resulting string is returned. this pointer is within 1254d24bec3aSMark Fasheh * dest, normally. 1255d24bec3aSMark Fasheh * in case the path buffer would overflow, the pointer is decremented further 1256d24bec3aSMark Fasheh * as if output was written to the buffer, though no more output is actually 1257d24bec3aSMark Fasheh * generated. that way, the caller can determine how much space would be 1258d24bec3aSMark Fasheh * required for the path to fit into the buffer. in that case, the returned 1259d24bec3aSMark Fasheh * value will be smaller than dest. callers must check this! 1260d24bec3aSMark Fasheh */ 1261d24bec3aSMark Fasheh char *btrfs_iref_to_path(struct btrfs_root *fs_root, 1262d24bec3aSMark Fasheh struct btrfs_path *path, 1263d24bec3aSMark Fasheh struct btrfs_inode_ref *iref, 1264d24bec3aSMark Fasheh struct extent_buffer *eb_in, u64 parent, 1265d24bec3aSMark Fasheh char *dest, u32 size) 1266d24bec3aSMark Fasheh { 1267d24bec3aSMark Fasheh return ref_to_path(fs_root, path, 1268d24bec3aSMark Fasheh btrfs_inode_ref_name_len(eb_in, iref), 1269d24bec3aSMark Fasheh (unsigned long)(iref + 1), 1270d24bec3aSMark Fasheh eb_in, parent, dest, size); 1271d24bec3aSMark Fasheh } 1272d24bec3aSMark Fasheh 1273d24bec3aSMark Fasheh /* 1274a542ad1bSJan Schmidt * this makes the path point to (logical EXTENT_ITEM *) 1275a542ad1bSJan Schmidt * returns BTRFS_EXTENT_FLAG_DATA for data, BTRFS_EXTENT_FLAG_TREE_BLOCK for 1276a542ad1bSJan Schmidt * tree blocks and <0 on error. 1277a542ad1bSJan Schmidt */ 1278a542ad1bSJan Schmidt int extent_from_logical(struct btrfs_fs_info *fs_info, u64 logical, 127969917e43SLiu Bo struct btrfs_path *path, struct btrfs_key *found_key, 128069917e43SLiu Bo u64 *flags_ret) 1281a542ad1bSJan Schmidt { 1282a542ad1bSJan Schmidt int ret; 1283a542ad1bSJan Schmidt u64 flags; 1284a542ad1bSJan Schmidt u32 item_size; 1285a542ad1bSJan Schmidt struct extent_buffer *eb; 1286a542ad1bSJan Schmidt struct btrfs_extent_item *ei; 1287a542ad1bSJan Schmidt struct btrfs_key key; 1288a542ad1bSJan Schmidt 1289a542ad1bSJan Schmidt key.type = BTRFS_EXTENT_ITEM_KEY; 1290a542ad1bSJan Schmidt key.objectid = logical; 1291a542ad1bSJan Schmidt key.offset = (u64)-1; 1292a542ad1bSJan Schmidt 1293a542ad1bSJan Schmidt ret = btrfs_search_slot(NULL, fs_info->extent_root, &key, path, 0, 0); 1294a542ad1bSJan Schmidt if (ret < 0) 1295a542ad1bSJan Schmidt return ret; 1296a542ad1bSJan Schmidt ret = btrfs_previous_item(fs_info->extent_root, path, 1297a542ad1bSJan Schmidt 0, BTRFS_EXTENT_ITEM_KEY); 1298a542ad1bSJan Schmidt if (ret < 0) 1299a542ad1bSJan Schmidt return ret; 1300a542ad1bSJan Schmidt 1301a542ad1bSJan Schmidt btrfs_item_key_to_cpu(path->nodes[0], found_key, path->slots[0]); 1302a542ad1bSJan Schmidt if (found_key->type != BTRFS_EXTENT_ITEM_KEY || 1303a542ad1bSJan Schmidt found_key->objectid > logical || 13044692cf58SJan Schmidt found_key->objectid + found_key->offset <= logical) { 13054692cf58SJan Schmidt pr_debug("logical %llu is not within any extent\n", 13064692cf58SJan Schmidt (unsigned long long)logical); 1307a542ad1bSJan Schmidt return -ENOENT; 13084692cf58SJan Schmidt } 1309a542ad1bSJan Schmidt 1310a542ad1bSJan Schmidt eb = path->nodes[0]; 1311a542ad1bSJan Schmidt item_size = btrfs_item_size_nr(eb, path->slots[0]); 1312a542ad1bSJan Schmidt BUG_ON(item_size < sizeof(*ei)); 1313a542ad1bSJan Schmidt 1314a542ad1bSJan Schmidt ei = btrfs_item_ptr(eb, path->slots[0], struct btrfs_extent_item); 1315a542ad1bSJan Schmidt flags = btrfs_extent_flags(eb, ei); 1316a542ad1bSJan Schmidt 13174692cf58SJan Schmidt pr_debug("logical %llu is at position %llu within the extent (%llu " 13184692cf58SJan Schmidt "EXTENT_ITEM %llu) flags %#llx size %u\n", 13194692cf58SJan Schmidt (unsigned long long)logical, 13204692cf58SJan Schmidt (unsigned long long)(logical - found_key->objectid), 13214692cf58SJan Schmidt (unsigned long long)found_key->objectid, 13224692cf58SJan Schmidt (unsigned long long)found_key->offset, 13234692cf58SJan Schmidt (unsigned long long)flags, item_size); 132469917e43SLiu Bo 132569917e43SLiu Bo WARN_ON(!flags_ret); 132669917e43SLiu Bo if (flags_ret) { 1327a542ad1bSJan Schmidt if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) 132869917e43SLiu Bo *flags_ret = BTRFS_EXTENT_FLAG_TREE_BLOCK; 132969917e43SLiu Bo else if (flags & BTRFS_EXTENT_FLAG_DATA) 133069917e43SLiu Bo *flags_ret = BTRFS_EXTENT_FLAG_DATA; 133169917e43SLiu Bo else 133269917e43SLiu Bo BUG_ON(1); 133369917e43SLiu Bo return 0; 133469917e43SLiu Bo } 1335a542ad1bSJan Schmidt 1336a542ad1bSJan Schmidt return -EIO; 1337a542ad1bSJan Schmidt } 1338a542ad1bSJan Schmidt 1339a542ad1bSJan Schmidt /* 1340a542ad1bSJan Schmidt * helper function to iterate extent inline refs. ptr must point to a 0 value 1341a542ad1bSJan Schmidt * for the first call and may be modified. it is used to track state. 1342a542ad1bSJan Schmidt * if more refs exist, 0 is returned and the next call to 1343a542ad1bSJan Schmidt * __get_extent_inline_ref must pass the modified ptr parameter to get the 1344a542ad1bSJan Schmidt * next ref. after the last ref was processed, 1 is returned. 1345a542ad1bSJan Schmidt * returns <0 on error 1346a542ad1bSJan Schmidt */ 1347a542ad1bSJan Schmidt static int __get_extent_inline_ref(unsigned long *ptr, struct extent_buffer *eb, 1348a542ad1bSJan Schmidt struct btrfs_extent_item *ei, u32 item_size, 1349a542ad1bSJan Schmidt struct btrfs_extent_inline_ref **out_eiref, 1350a542ad1bSJan Schmidt int *out_type) 1351a542ad1bSJan Schmidt { 1352a542ad1bSJan Schmidt unsigned long end; 1353a542ad1bSJan Schmidt u64 flags; 1354a542ad1bSJan Schmidt struct btrfs_tree_block_info *info; 1355a542ad1bSJan Schmidt 1356a542ad1bSJan Schmidt if (!*ptr) { 1357a542ad1bSJan Schmidt /* first call */ 1358a542ad1bSJan Schmidt flags = btrfs_extent_flags(eb, ei); 1359a542ad1bSJan Schmidt if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) { 1360a542ad1bSJan Schmidt info = (struct btrfs_tree_block_info *)(ei + 1); 1361a542ad1bSJan Schmidt *out_eiref = 1362a542ad1bSJan Schmidt (struct btrfs_extent_inline_ref *)(info + 1); 1363a542ad1bSJan Schmidt } else { 1364a542ad1bSJan Schmidt *out_eiref = (struct btrfs_extent_inline_ref *)(ei + 1); 1365a542ad1bSJan Schmidt } 1366a542ad1bSJan Schmidt *ptr = (unsigned long)*out_eiref; 1367a542ad1bSJan Schmidt if ((void *)*ptr >= (void *)ei + item_size) 1368a542ad1bSJan Schmidt return -ENOENT; 1369a542ad1bSJan Schmidt } 1370a542ad1bSJan Schmidt 1371a542ad1bSJan Schmidt end = (unsigned long)ei + item_size; 1372a542ad1bSJan Schmidt *out_eiref = (struct btrfs_extent_inline_ref *)*ptr; 1373a542ad1bSJan Schmidt *out_type = btrfs_extent_inline_ref_type(eb, *out_eiref); 1374a542ad1bSJan Schmidt 1375a542ad1bSJan Schmidt *ptr += btrfs_extent_inline_ref_size(*out_type); 1376a542ad1bSJan Schmidt WARN_ON(*ptr > end); 1377a542ad1bSJan Schmidt if (*ptr == end) 1378a542ad1bSJan Schmidt return 1; /* last */ 1379a542ad1bSJan Schmidt 1380a542ad1bSJan Schmidt return 0; 1381a542ad1bSJan Schmidt } 1382a542ad1bSJan Schmidt 1383a542ad1bSJan Schmidt /* 1384a542ad1bSJan Schmidt * reads the tree block backref for an extent. tree level and root are returned 1385a542ad1bSJan Schmidt * through out_level and out_root. ptr must point to a 0 value for the first 1386a542ad1bSJan Schmidt * call and may be modified (see __get_extent_inline_ref comment). 1387a542ad1bSJan Schmidt * returns 0 if data was provided, 1 if there was no more data to provide or 1388a542ad1bSJan Schmidt * <0 on error. 1389a542ad1bSJan Schmidt */ 1390a542ad1bSJan Schmidt int tree_backref_for_extent(unsigned long *ptr, struct extent_buffer *eb, 1391a542ad1bSJan Schmidt struct btrfs_extent_item *ei, u32 item_size, 1392a542ad1bSJan Schmidt u64 *out_root, u8 *out_level) 1393a542ad1bSJan Schmidt { 1394a542ad1bSJan Schmidt int ret; 1395a542ad1bSJan Schmidt int type; 1396a542ad1bSJan Schmidt struct btrfs_tree_block_info *info; 1397a542ad1bSJan Schmidt struct btrfs_extent_inline_ref *eiref; 1398a542ad1bSJan Schmidt 1399a542ad1bSJan Schmidt if (*ptr == (unsigned long)-1) 1400a542ad1bSJan Schmidt return 1; 1401a542ad1bSJan Schmidt 1402a542ad1bSJan Schmidt while (1) { 1403a542ad1bSJan Schmidt ret = __get_extent_inline_ref(ptr, eb, ei, item_size, 1404a542ad1bSJan Schmidt &eiref, &type); 1405a542ad1bSJan Schmidt if (ret < 0) 1406a542ad1bSJan Schmidt return ret; 1407a542ad1bSJan Schmidt 1408a542ad1bSJan Schmidt if (type == BTRFS_TREE_BLOCK_REF_KEY || 1409a542ad1bSJan Schmidt type == BTRFS_SHARED_BLOCK_REF_KEY) 1410a542ad1bSJan Schmidt break; 1411a542ad1bSJan Schmidt 1412a542ad1bSJan Schmidt if (ret == 1) 1413a542ad1bSJan Schmidt return 1; 1414a542ad1bSJan Schmidt } 1415a542ad1bSJan Schmidt 1416a542ad1bSJan Schmidt /* we can treat both ref types equally here */ 1417a542ad1bSJan Schmidt info = (struct btrfs_tree_block_info *)(ei + 1); 1418a542ad1bSJan Schmidt *out_root = btrfs_extent_inline_ref_offset(eb, eiref); 1419a542ad1bSJan Schmidt *out_level = btrfs_tree_block_level(eb, info); 1420a542ad1bSJan Schmidt 1421a542ad1bSJan Schmidt if (ret == 1) 1422a542ad1bSJan Schmidt *ptr = (unsigned long)-1; 1423a542ad1bSJan Schmidt 1424a542ad1bSJan Schmidt return 0; 1425a542ad1bSJan Schmidt } 1426a542ad1bSJan Schmidt 1427976b1908SJan Schmidt static int iterate_leaf_refs(struct extent_inode_elem *inode_list, 1428976b1908SJan Schmidt u64 root, u64 extent_item_objectid, 14294692cf58SJan Schmidt iterate_extent_inodes_t *iterate, void *ctx) 1430a542ad1bSJan Schmidt { 1431976b1908SJan Schmidt struct extent_inode_elem *eie; 14324692cf58SJan Schmidt int ret = 0; 1433a542ad1bSJan Schmidt 1434976b1908SJan Schmidt for (eie = inode_list; eie; eie = eie->next) { 14354692cf58SJan Schmidt pr_debug("ref for %llu resolved, key (%llu EXTEND_DATA %llu), " 1436976b1908SJan Schmidt "root %llu\n", extent_item_objectid, 1437976b1908SJan Schmidt eie->inum, eie->offset, root); 1438976b1908SJan Schmidt ret = iterate(eie->inum, eie->offset, root, ctx); 14394692cf58SJan Schmidt if (ret) { 1440976b1908SJan Schmidt pr_debug("stopping iteration for %llu due to ret=%d\n", 1441976b1908SJan Schmidt extent_item_objectid, ret); 1442a542ad1bSJan Schmidt break; 1443a542ad1bSJan Schmidt } 1444a542ad1bSJan Schmidt } 1445a542ad1bSJan Schmidt 1446a542ad1bSJan Schmidt return ret; 1447a542ad1bSJan Schmidt } 1448a542ad1bSJan Schmidt 1449a542ad1bSJan Schmidt /* 1450a542ad1bSJan Schmidt * calls iterate() for every inode that references the extent identified by 14514692cf58SJan Schmidt * the given parameters. 1452a542ad1bSJan Schmidt * when the iterator function returns a non-zero value, iteration stops. 1453a542ad1bSJan Schmidt */ 1454a542ad1bSJan Schmidt int iterate_extent_inodes(struct btrfs_fs_info *fs_info, 14554692cf58SJan Schmidt u64 extent_item_objectid, u64 extent_item_pos, 14567a3ae2f8SJan Schmidt int search_commit_root, 1457a542ad1bSJan Schmidt iterate_extent_inodes_t *iterate, void *ctx) 1458a542ad1bSJan Schmidt { 1459a542ad1bSJan Schmidt int ret; 1460a542ad1bSJan Schmidt struct list_head data_refs = LIST_HEAD_INIT(data_refs); 1461a542ad1bSJan Schmidt struct list_head shared_refs = LIST_HEAD_INIT(shared_refs); 14624692cf58SJan Schmidt struct btrfs_trans_handle *trans; 14637a3ae2f8SJan Schmidt struct ulist *refs = NULL; 14647a3ae2f8SJan Schmidt struct ulist *roots = NULL; 14654692cf58SJan Schmidt struct ulist_node *ref_node = NULL; 14664692cf58SJan Schmidt struct ulist_node *root_node = NULL; 14678445f61cSJan Schmidt struct seq_list tree_mod_seq_elem = {}; 1468cd1b413cSJan Schmidt struct ulist_iterator ref_uiter; 1469cd1b413cSJan Schmidt struct ulist_iterator root_uiter; 1470a542ad1bSJan Schmidt 14714692cf58SJan Schmidt pr_debug("resolving all inodes for extent %llu\n", 14724692cf58SJan Schmidt extent_item_objectid); 14734692cf58SJan Schmidt 14747a3ae2f8SJan Schmidt if (search_commit_root) { 14757a3ae2f8SJan Schmidt trans = BTRFS_BACKREF_SEARCH_COMMIT_ROOT; 14767a3ae2f8SJan Schmidt } else { 14777a3ae2f8SJan Schmidt trans = btrfs_join_transaction(fs_info->extent_root); 14787a3ae2f8SJan Schmidt if (IS_ERR(trans)) 14797a3ae2f8SJan Schmidt return PTR_ERR(trans); 14808445f61cSJan Schmidt btrfs_get_tree_mod_seq(fs_info, &tree_mod_seq_elem); 14817a3ae2f8SJan Schmidt } 14824692cf58SJan Schmidt 14834692cf58SJan Schmidt ret = btrfs_find_all_leafs(trans, fs_info, extent_item_objectid, 1484097b8a7cSJan Schmidt tree_mod_seq_elem.seq, &refs, 14858445f61cSJan Schmidt &extent_item_pos); 14864692cf58SJan Schmidt if (ret) 14874692cf58SJan Schmidt goto out; 14884692cf58SJan Schmidt 1489cd1b413cSJan Schmidt ULIST_ITER_INIT(&ref_uiter); 1490cd1b413cSJan Schmidt while (!ret && (ref_node = ulist_next(refs, &ref_uiter))) { 1491976b1908SJan Schmidt ret = btrfs_find_all_roots(trans, fs_info, ref_node->val, 14928445f61cSJan Schmidt tree_mod_seq_elem.seq, &roots); 14934692cf58SJan Schmidt if (ret) 1494a542ad1bSJan Schmidt break; 1495cd1b413cSJan Schmidt ULIST_ITER_INIT(&root_uiter); 1496cd1b413cSJan Schmidt while (!ret && (root_node = ulist_next(roots, &root_uiter))) { 1497976b1908SJan Schmidt pr_debug("root %llu references leaf %llu, data list " 149834d73f54SAlexander Block "%#llx\n", root_node->val, ref_node->val, 1499995e01b7SJan Schmidt (long long)ref_node->aux); 1500995e01b7SJan Schmidt ret = iterate_leaf_refs((struct extent_inode_elem *) 1501995e01b7SJan Schmidt (uintptr_t)ref_node->aux, 1502995e01b7SJan Schmidt root_node->val, 1503995e01b7SJan Schmidt extent_item_objectid, 1504a542ad1bSJan Schmidt iterate, ctx); 15054692cf58SJan Schmidt } 1506976b1908SJan Schmidt ulist_free(roots); 1507976b1908SJan Schmidt roots = NULL; 1508a542ad1bSJan Schmidt } 1509a542ad1bSJan Schmidt 1510976b1908SJan Schmidt free_leaf_list(refs); 15114692cf58SJan Schmidt ulist_free(roots); 15124692cf58SJan Schmidt out: 15137a3ae2f8SJan Schmidt if (!search_commit_root) { 15148445f61cSJan Schmidt btrfs_put_tree_mod_seq(fs_info, &tree_mod_seq_elem); 15154692cf58SJan Schmidt btrfs_end_transaction(trans, fs_info->extent_root); 15167a3ae2f8SJan Schmidt } 15177a3ae2f8SJan Schmidt 1518a542ad1bSJan Schmidt return ret; 1519a542ad1bSJan Schmidt } 1520a542ad1bSJan Schmidt 1521a542ad1bSJan Schmidt int iterate_inodes_from_logical(u64 logical, struct btrfs_fs_info *fs_info, 1522a542ad1bSJan Schmidt struct btrfs_path *path, 1523a542ad1bSJan Schmidt iterate_extent_inodes_t *iterate, void *ctx) 1524a542ad1bSJan Schmidt { 1525a542ad1bSJan Schmidt int ret; 15264692cf58SJan Schmidt u64 extent_item_pos; 152769917e43SLiu Bo u64 flags = 0; 1528a542ad1bSJan Schmidt struct btrfs_key found_key; 15297a3ae2f8SJan Schmidt int search_commit_root = path->search_commit_root; 1530a542ad1bSJan Schmidt 153169917e43SLiu Bo ret = extent_from_logical(fs_info, logical, path, &found_key, &flags); 15324692cf58SJan Schmidt btrfs_release_path(path); 1533a542ad1bSJan Schmidt if (ret < 0) 1534a542ad1bSJan Schmidt return ret; 153569917e43SLiu Bo if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) 15363627bf45SStefan Behrens return -EINVAL; 1537a542ad1bSJan Schmidt 15384692cf58SJan Schmidt extent_item_pos = logical - found_key.objectid; 15397a3ae2f8SJan Schmidt ret = iterate_extent_inodes(fs_info, found_key.objectid, 15407a3ae2f8SJan Schmidt extent_item_pos, search_commit_root, 15417a3ae2f8SJan Schmidt iterate, ctx); 1542a542ad1bSJan Schmidt 1543a542ad1bSJan Schmidt return ret; 1544a542ad1bSJan Schmidt } 1545a542ad1bSJan Schmidt 1546d24bec3aSMark Fasheh typedef int (iterate_irefs_t)(u64 parent, u32 name_len, unsigned long name_off, 1547d24bec3aSMark Fasheh struct extent_buffer *eb, void *ctx); 1548d24bec3aSMark Fasheh 1549d24bec3aSMark Fasheh static int iterate_inode_refs(u64 inum, struct btrfs_root *fs_root, 1550a542ad1bSJan Schmidt struct btrfs_path *path, 1551a542ad1bSJan Schmidt iterate_irefs_t *iterate, void *ctx) 1552a542ad1bSJan Schmidt { 1553aefc1eb1SJan Schmidt int ret = 0; 1554a542ad1bSJan Schmidt int slot; 1555a542ad1bSJan Schmidt u32 cur; 1556a542ad1bSJan Schmidt u32 len; 1557a542ad1bSJan Schmidt u32 name_len; 1558a542ad1bSJan Schmidt u64 parent = 0; 1559a542ad1bSJan Schmidt int found = 0; 1560a542ad1bSJan Schmidt struct extent_buffer *eb; 1561a542ad1bSJan Schmidt struct btrfs_item *item; 1562a542ad1bSJan Schmidt struct btrfs_inode_ref *iref; 1563a542ad1bSJan Schmidt struct btrfs_key found_key; 1564a542ad1bSJan Schmidt 1565aefc1eb1SJan Schmidt while (!ret) { 1566b916a59aSJan Schmidt path->leave_spinning = 1; 1567a542ad1bSJan Schmidt ret = inode_ref_info(inum, parent ? parent+1 : 0, fs_root, path, 1568a542ad1bSJan Schmidt &found_key); 1569a542ad1bSJan Schmidt if (ret < 0) 1570a542ad1bSJan Schmidt break; 1571a542ad1bSJan Schmidt if (ret) { 1572a542ad1bSJan Schmidt ret = found ? 0 : -ENOENT; 1573a542ad1bSJan Schmidt break; 1574a542ad1bSJan Schmidt } 1575a542ad1bSJan Schmidt ++found; 1576a542ad1bSJan Schmidt 1577a542ad1bSJan Schmidt parent = found_key.offset; 1578a542ad1bSJan Schmidt slot = path->slots[0]; 1579a542ad1bSJan Schmidt eb = path->nodes[0]; 1580a542ad1bSJan Schmidt /* make sure we can use eb after releasing the path */ 1581a542ad1bSJan Schmidt atomic_inc(&eb->refs); 1582b916a59aSJan Schmidt btrfs_tree_read_lock(eb); 1583b916a59aSJan Schmidt btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK); 1584a542ad1bSJan Schmidt btrfs_release_path(path); 1585a542ad1bSJan Schmidt 1586a542ad1bSJan Schmidt item = btrfs_item_nr(eb, slot); 1587a542ad1bSJan Schmidt iref = btrfs_item_ptr(eb, slot, struct btrfs_inode_ref); 1588a542ad1bSJan Schmidt 1589a542ad1bSJan Schmidt for (cur = 0; cur < btrfs_item_size(eb, item); cur += len) { 1590a542ad1bSJan Schmidt name_len = btrfs_inode_ref_name_len(eb, iref); 1591a542ad1bSJan Schmidt /* path must be released before calling iterate()! */ 15924692cf58SJan Schmidt pr_debug("following ref at offset %u for inode %llu in " 15934692cf58SJan Schmidt "tree %llu\n", cur, 15944692cf58SJan Schmidt (unsigned long long)found_key.objectid, 15954692cf58SJan Schmidt (unsigned long long)fs_root->objectid); 1596d24bec3aSMark Fasheh ret = iterate(parent, name_len, 1597d24bec3aSMark Fasheh (unsigned long)(iref + 1), eb, ctx); 1598aefc1eb1SJan Schmidt if (ret) 1599a542ad1bSJan Schmidt break; 1600a542ad1bSJan Schmidt len = sizeof(*iref) + name_len; 1601a542ad1bSJan Schmidt iref = (struct btrfs_inode_ref *)((char *)iref + len); 1602a542ad1bSJan Schmidt } 1603b916a59aSJan Schmidt btrfs_tree_read_unlock_blocking(eb); 1604a542ad1bSJan Schmidt free_extent_buffer(eb); 1605a542ad1bSJan Schmidt } 1606a542ad1bSJan Schmidt 1607a542ad1bSJan Schmidt btrfs_release_path(path); 1608a542ad1bSJan Schmidt 1609a542ad1bSJan Schmidt return ret; 1610a542ad1bSJan Schmidt } 1611a542ad1bSJan Schmidt 1612d24bec3aSMark Fasheh static int iterate_inode_extrefs(u64 inum, struct btrfs_root *fs_root, 1613d24bec3aSMark Fasheh struct btrfs_path *path, 1614d24bec3aSMark Fasheh iterate_irefs_t *iterate, void *ctx) 1615d24bec3aSMark Fasheh { 1616d24bec3aSMark Fasheh int ret; 1617d24bec3aSMark Fasheh int slot; 1618d24bec3aSMark Fasheh u64 offset = 0; 1619d24bec3aSMark Fasheh u64 parent; 1620d24bec3aSMark Fasheh int found = 0; 1621d24bec3aSMark Fasheh struct extent_buffer *eb; 1622d24bec3aSMark Fasheh struct btrfs_inode_extref *extref; 1623d24bec3aSMark Fasheh struct extent_buffer *leaf; 1624d24bec3aSMark Fasheh u32 item_size; 1625d24bec3aSMark Fasheh u32 cur_offset; 1626d24bec3aSMark Fasheh unsigned long ptr; 1627d24bec3aSMark Fasheh 1628d24bec3aSMark Fasheh while (1) { 1629d24bec3aSMark Fasheh ret = btrfs_find_one_extref(fs_root, inum, offset, path, &extref, 1630d24bec3aSMark Fasheh &offset); 1631d24bec3aSMark Fasheh if (ret < 0) 1632d24bec3aSMark Fasheh break; 1633d24bec3aSMark Fasheh if (ret) { 1634d24bec3aSMark Fasheh ret = found ? 0 : -ENOENT; 1635d24bec3aSMark Fasheh break; 1636d24bec3aSMark Fasheh } 1637d24bec3aSMark Fasheh ++found; 1638d24bec3aSMark Fasheh 1639d24bec3aSMark Fasheh slot = path->slots[0]; 1640d24bec3aSMark Fasheh eb = path->nodes[0]; 1641d24bec3aSMark Fasheh /* make sure we can use eb after releasing the path */ 1642d24bec3aSMark Fasheh atomic_inc(&eb->refs); 1643d24bec3aSMark Fasheh 1644d24bec3aSMark Fasheh btrfs_tree_read_lock(eb); 1645d24bec3aSMark Fasheh btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK); 1646d24bec3aSMark Fasheh btrfs_release_path(path); 1647d24bec3aSMark Fasheh 1648d24bec3aSMark Fasheh leaf = path->nodes[0]; 1649d24bec3aSMark Fasheh item_size = btrfs_item_size_nr(leaf, path->slots[0]); 1650d24bec3aSMark Fasheh ptr = btrfs_item_ptr_offset(leaf, path->slots[0]); 1651d24bec3aSMark Fasheh cur_offset = 0; 1652d24bec3aSMark Fasheh 1653d24bec3aSMark Fasheh while (cur_offset < item_size) { 1654d24bec3aSMark Fasheh u32 name_len; 1655d24bec3aSMark Fasheh 1656d24bec3aSMark Fasheh extref = (struct btrfs_inode_extref *)(ptr + cur_offset); 1657d24bec3aSMark Fasheh parent = btrfs_inode_extref_parent(eb, extref); 1658d24bec3aSMark Fasheh name_len = btrfs_inode_extref_name_len(eb, extref); 1659d24bec3aSMark Fasheh ret = iterate(parent, name_len, 1660d24bec3aSMark Fasheh (unsigned long)&extref->name, eb, ctx); 1661d24bec3aSMark Fasheh if (ret) 1662d24bec3aSMark Fasheh break; 1663d24bec3aSMark Fasheh 1664d24bec3aSMark Fasheh cur_offset += btrfs_inode_extref_name_len(leaf, extref); 1665d24bec3aSMark Fasheh cur_offset += sizeof(*extref); 1666d24bec3aSMark Fasheh } 1667d24bec3aSMark Fasheh btrfs_tree_read_unlock_blocking(eb); 1668d24bec3aSMark Fasheh free_extent_buffer(eb); 1669d24bec3aSMark Fasheh 1670d24bec3aSMark Fasheh offset++; 1671d24bec3aSMark Fasheh } 1672d24bec3aSMark Fasheh 1673d24bec3aSMark Fasheh btrfs_release_path(path); 1674d24bec3aSMark Fasheh 1675d24bec3aSMark Fasheh return ret; 1676d24bec3aSMark Fasheh } 1677d24bec3aSMark Fasheh 1678d24bec3aSMark Fasheh static int iterate_irefs(u64 inum, struct btrfs_root *fs_root, 1679d24bec3aSMark Fasheh struct btrfs_path *path, iterate_irefs_t *iterate, 1680d24bec3aSMark Fasheh void *ctx) 1681d24bec3aSMark Fasheh { 1682d24bec3aSMark Fasheh int ret; 1683d24bec3aSMark Fasheh int found_refs = 0; 1684d24bec3aSMark Fasheh 1685d24bec3aSMark Fasheh ret = iterate_inode_refs(inum, fs_root, path, iterate, ctx); 1686d24bec3aSMark Fasheh if (!ret) 1687d24bec3aSMark Fasheh ++found_refs; 1688d24bec3aSMark Fasheh else if (ret != -ENOENT) 1689d24bec3aSMark Fasheh return ret; 1690d24bec3aSMark Fasheh 1691d24bec3aSMark Fasheh ret = iterate_inode_extrefs(inum, fs_root, path, iterate, ctx); 1692d24bec3aSMark Fasheh if (ret == -ENOENT && found_refs) 1693d24bec3aSMark Fasheh return 0; 1694d24bec3aSMark Fasheh 1695d24bec3aSMark Fasheh return ret; 1696d24bec3aSMark Fasheh } 1697d24bec3aSMark Fasheh 1698a542ad1bSJan Schmidt /* 1699a542ad1bSJan Schmidt * returns 0 if the path could be dumped (probably truncated) 1700a542ad1bSJan Schmidt * returns <0 in case of an error 1701a542ad1bSJan Schmidt */ 1702d24bec3aSMark Fasheh static int inode_to_path(u64 inum, u32 name_len, unsigned long name_off, 1703a542ad1bSJan Schmidt struct extent_buffer *eb, void *ctx) 1704a542ad1bSJan Schmidt { 1705a542ad1bSJan Schmidt struct inode_fs_paths *ipath = ctx; 1706a542ad1bSJan Schmidt char *fspath; 1707a542ad1bSJan Schmidt char *fspath_min; 1708a542ad1bSJan Schmidt int i = ipath->fspath->elem_cnt; 1709a542ad1bSJan Schmidt const int s_ptr = sizeof(char *); 1710a542ad1bSJan Schmidt u32 bytes_left; 1711a542ad1bSJan Schmidt 1712a542ad1bSJan Schmidt bytes_left = ipath->fspath->bytes_left > s_ptr ? 1713a542ad1bSJan Schmidt ipath->fspath->bytes_left - s_ptr : 0; 1714a542ad1bSJan Schmidt 1715740c3d22SChris Mason fspath_min = (char *)ipath->fspath->val + (i + 1) * s_ptr; 1716d24bec3aSMark Fasheh fspath = ref_to_path(ipath->fs_root, ipath->btrfs_path, name_len, 1717d24bec3aSMark Fasheh name_off, eb, inum, fspath_min, 1718d24bec3aSMark Fasheh bytes_left); 1719a542ad1bSJan Schmidt if (IS_ERR(fspath)) 1720a542ad1bSJan Schmidt return PTR_ERR(fspath); 1721a542ad1bSJan Schmidt 1722a542ad1bSJan Schmidt if (fspath > fspath_min) { 1723745c4d8eSJeff Mahoney ipath->fspath->val[i] = (u64)(unsigned long)fspath; 1724a542ad1bSJan Schmidt ++ipath->fspath->elem_cnt; 1725a542ad1bSJan Schmidt ipath->fspath->bytes_left = fspath - fspath_min; 1726a542ad1bSJan Schmidt } else { 1727a542ad1bSJan Schmidt ++ipath->fspath->elem_missed; 1728a542ad1bSJan Schmidt ipath->fspath->bytes_missing += fspath_min - fspath; 1729a542ad1bSJan Schmidt ipath->fspath->bytes_left = 0; 1730a542ad1bSJan Schmidt } 1731a542ad1bSJan Schmidt 1732a542ad1bSJan Schmidt return 0; 1733a542ad1bSJan Schmidt } 1734a542ad1bSJan Schmidt 1735a542ad1bSJan Schmidt /* 1736a542ad1bSJan Schmidt * this dumps all file system paths to the inode into the ipath struct, provided 1737a542ad1bSJan Schmidt * is has been created large enough. each path is zero-terminated and accessed 1738740c3d22SChris Mason * from ipath->fspath->val[i]. 1739a542ad1bSJan Schmidt * when it returns, there are ipath->fspath->elem_cnt number of paths available 1740740c3d22SChris Mason * in ipath->fspath->val[]. when the allocated space wasn't sufficient, the 1741a542ad1bSJan Schmidt * number of missed paths in recored in ipath->fspath->elem_missed, otherwise, 1742a542ad1bSJan Schmidt * it's zero. ipath->fspath->bytes_missing holds the number of bytes that would 1743a542ad1bSJan Schmidt * have been needed to return all paths. 1744a542ad1bSJan Schmidt */ 1745a542ad1bSJan Schmidt int paths_from_inode(u64 inum, struct inode_fs_paths *ipath) 1746a542ad1bSJan Schmidt { 1747a542ad1bSJan Schmidt return iterate_irefs(inum, ipath->fs_root, ipath->btrfs_path, 1748a542ad1bSJan Schmidt inode_to_path, ipath); 1749a542ad1bSJan Schmidt } 1750a542ad1bSJan Schmidt 1751a542ad1bSJan Schmidt struct btrfs_data_container *init_data_container(u32 total_bytes) 1752a542ad1bSJan Schmidt { 1753a542ad1bSJan Schmidt struct btrfs_data_container *data; 1754a542ad1bSJan Schmidt size_t alloc_bytes; 1755a542ad1bSJan Schmidt 1756a542ad1bSJan Schmidt alloc_bytes = max_t(size_t, total_bytes, sizeof(*data)); 1757425d17a2SLiu Bo data = vmalloc(alloc_bytes); 1758a542ad1bSJan Schmidt if (!data) 1759a542ad1bSJan Schmidt return ERR_PTR(-ENOMEM); 1760a542ad1bSJan Schmidt 1761a542ad1bSJan Schmidt if (total_bytes >= sizeof(*data)) { 1762a542ad1bSJan Schmidt data->bytes_left = total_bytes - sizeof(*data); 1763a542ad1bSJan Schmidt data->bytes_missing = 0; 1764a542ad1bSJan Schmidt } else { 1765a542ad1bSJan Schmidt data->bytes_missing = sizeof(*data) - total_bytes; 1766a542ad1bSJan Schmidt data->bytes_left = 0; 1767a542ad1bSJan Schmidt } 1768a542ad1bSJan Schmidt 1769a542ad1bSJan Schmidt data->elem_cnt = 0; 1770a542ad1bSJan Schmidt data->elem_missed = 0; 1771a542ad1bSJan Schmidt 1772a542ad1bSJan Schmidt return data; 1773a542ad1bSJan Schmidt } 1774a542ad1bSJan Schmidt 1775a542ad1bSJan Schmidt /* 1776a542ad1bSJan Schmidt * allocates space to return multiple file system paths for an inode. 1777a542ad1bSJan Schmidt * total_bytes to allocate are passed, note that space usable for actual path 1778a542ad1bSJan Schmidt * information will be total_bytes - sizeof(struct inode_fs_paths). 1779a542ad1bSJan Schmidt * the returned pointer must be freed with free_ipath() in the end. 1780a542ad1bSJan Schmidt */ 1781a542ad1bSJan Schmidt struct inode_fs_paths *init_ipath(s32 total_bytes, struct btrfs_root *fs_root, 1782a542ad1bSJan Schmidt struct btrfs_path *path) 1783a542ad1bSJan Schmidt { 1784a542ad1bSJan Schmidt struct inode_fs_paths *ifp; 1785a542ad1bSJan Schmidt struct btrfs_data_container *fspath; 1786a542ad1bSJan Schmidt 1787a542ad1bSJan Schmidt fspath = init_data_container(total_bytes); 1788a542ad1bSJan Schmidt if (IS_ERR(fspath)) 1789a542ad1bSJan Schmidt return (void *)fspath; 1790a542ad1bSJan Schmidt 1791a542ad1bSJan Schmidt ifp = kmalloc(sizeof(*ifp), GFP_NOFS); 1792a542ad1bSJan Schmidt if (!ifp) { 1793a542ad1bSJan Schmidt kfree(fspath); 1794a542ad1bSJan Schmidt return ERR_PTR(-ENOMEM); 1795a542ad1bSJan Schmidt } 1796a542ad1bSJan Schmidt 1797a542ad1bSJan Schmidt ifp->btrfs_path = path; 1798a542ad1bSJan Schmidt ifp->fspath = fspath; 1799a542ad1bSJan Schmidt ifp->fs_root = fs_root; 1800a542ad1bSJan Schmidt 1801a542ad1bSJan Schmidt return ifp; 1802a542ad1bSJan Schmidt } 1803a542ad1bSJan Schmidt 1804a542ad1bSJan Schmidt void free_ipath(struct inode_fs_paths *ipath) 1805a542ad1bSJan Schmidt { 18064735fb28SJesper Juhl if (!ipath) 18074735fb28SJesper Juhl return; 1808425d17a2SLiu Bo vfree(ipath->fspath); 1809a542ad1bSJan Schmidt kfree(ipath); 1810a542ad1bSJan Schmidt } 1811