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 { 398ca15e05SJosef Bacik u64 offset = 0; 408ca15e05SJosef Bacik struct extent_inode_elem *e; 418ca15e05SJosef Bacik 428ca15e05SJosef Bacik if (!btrfs_file_extent_compression(eb, fi) && 438ca15e05SJosef Bacik !btrfs_file_extent_encryption(eb, fi) && 448ca15e05SJosef Bacik !btrfs_file_extent_other_encoding(eb, fi)) { 45976b1908SJan Schmidt u64 data_offset; 46976b1908SJan Schmidt u64 data_len; 47976b1908SJan Schmidt 48976b1908SJan Schmidt data_offset = btrfs_file_extent_offset(eb, fi); 49976b1908SJan Schmidt data_len = btrfs_file_extent_num_bytes(eb, fi); 50976b1908SJan Schmidt 51976b1908SJan Schmidt if (extent_item_pos < data_offset || 52976b1908SJan Schmidt extent_item_pos >= data_offset + data_len) 53976b1908SJan Schmidt return 1; 548ca15e05SJosef Bacik offset = extent_item_pos - data_offset; 558ca15e05SJosef Bacik } 56976b1908SJan Schmidt 57976b1908SJan Schmidt e = kmalloc(sizeof(*e), GFP_NOFS); 58976b1908SJan Schmidt if (!e) 59976b1908SJan Schmidt return -ENOMEM; 60976b1908SJan Schmidt 61976b1908SJan Schmidt e->next = *eie; 62976b1908SJan Schmidt e->inum = key->objectid; 638ca15e05SJosef Bacik e->offset = key->offset + offset; 64976b1908SJan Schmidt *eie = e; 65976b1908SJan Schmidt 66976b1908SJan Schmidt return 0; 67976b1908SJan Schmidt } 68976b1908SJan Schmidt 69976b1908SJan Schmidt static int find_extent_in_eb(struct extent_buffer *eb, u64 wanted_disk_byte, 70976b1908SJan Schmidt u64 extent_item_pos, 71976b1908SJan Schmidt struct extent_inode_elem **eie) 72976b1908SJan Schmidt { 73976b1908SJan Schmidt u64 disk_byte; 74976b1908SJan Schmidt struct btrfs_key key; 75976b1908SJan Schmidt struct btrfs_file_extent_item *fi; 76976b1908SJan Schmidt int slot; 77976b1908SJan Schmidt int nritems; 78976b1908SJan Schmidt int extent_type; 79976b1908SJan Schmidt int ret; 80976b1908SJan Schmidt 81976b1908SJan Schmidt /* 82976b1908SJan Schmidt * from the shared data ref, we only have the leaf but we need 83976b1908SJan Schmidt * the key. thus, we must look into all items and see that we 84976b1908SJan Schmidt * find one (some) with a reference to our extent item. 85976b1908SJan Schmidt */ 86976b1908SJan Schmidt nritems = btrfs_header_nritems(eb); 87976b1908SJan Schmidt for (slot = 0; slot < nritems; ++slot) { 88976b1908SJan Schmidt btrfs_item_key_to_cpu(eb, &key, slot); 89976b1908SJan Schmidt if (key.type != BTRFS_EXTENT_DATA_KEY) 90976b1908SJan Schmidt continue; 91976b1908SJan Schmidt fi = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item); 92976b1908SJan Schmidt extent_type = btrfs_file_extent_type(eb, fi); 93976b1908SJan Schmidt if (extent_type == BTRFS_FILE_EXTENT_INLINE) 94976b1908SJan Schmidt continue; 95976b1908SJan Schmidt /* don't skip BTRFS_FILE_EXTENT_PREALLOC, we can handle that */ 96976b1908SJan Schmidt disk_byte = btrfs_file_extent_disk_bytenr(eb, fi); 97976b1908SJan Schmidt if (disk_byte != wanted_disk_byte) 98976b1908SJan Schmidt continue; 99976b1908SJan Schmidt 100976b1908SJan Schmidt ret = check_extent_in_eb(&key, eb, fi, extent_item_pos, eie); 101976b1908SJan Schmidt if (ret < 0) 102976b1908SJan Schmidt return ret; 103976b1908SJan Schmidt } 104976b1908SJan Schmidt 105976b1908SJan Schmidt return 0; 106976b1908SJan Schmidt } 107976b1908SJan Schmidt 1088da6d581SJan Schmidt /* 1098da6d581SJan Schmidt * this structure records all encountered refs on the way up to the root 1108da6d581SJan Schmidt */ 1118da6d581SJan Schmidt struct __prelim_ref { 1128da6d581SJan Schmidt struct list_head list; 1138da6d581SJan Schmidt u64 root_id; 114d5c88b73SJan Schmidt struct btrfs_key key_for_search; 1158da6d581SJan Schmidt int level; 1168da6d581SJan Schmidt int count; 1173301958bSJan Schmidt struct extent_inode_elem *inode_list; 1188da6d581SJan Schmidt u64 parent; 1198da6d581SJan Schmidt u64 wanted_disk_byte; 1208da6d581SJan Schmidt }; 1218da6d581SJan Schmidt 122d5c88b73SJan Schmidt /* 123d5c88b73SJan Schmidt * the rules for all callers of this function are: 124d5c88b73SJan Schmidt * - obtaining the parent is the goal 125d5c88b73SJan Schmidt * - if you add a key, you must know that it is a correct key 126d5c88b73SJan Schmidt * - if you cannot add the parent or a correct key, then we will look into the 127d5c88b73SJan Schmidt * block later to set a correct key 128d5c88b73SJan Schmidt * 129d5c88b73SJan Schmidt * delayed refs 130d5c88b73SJan Schmidt * ============ 131d5c88b73SJan Schmidt * backref type | shared | indirect | shared | indirect 132d5c88b73SJan Schmidt * information | tree | tree | data | data 133d5c88b73SJan Schmidt * --------------------+--------+----------+--------+---------- 134d5c88b73SJan Schmidt * parent logical | y | - | - | - 135d5c88b73SJan Schmidt * key to resolve | - | y | y | y 136d5c88b73SJan Schmidt * tree block logical | - | - | - | - 137d5c88b73SJan Schmidt * root for resolving | y | y | y | y 138d5c88b73SJan Schmidt * 139d5c88b73SJan Schmidt * - column 1: we've the parent -> done 140d5c88b73SJan Schmidt * - column 2, 3, 4: we use the key to find the parent 141d5c88b73SJan Schmidt * 142d5c88b73SJan Schmidt * on disk refs (inline or keyed) 143d5c88b73SJan Schmidt * ============================== 144d5c88b73SJan Schmidt * backref type | shared | indirect | shared | indirect 145d5c88b73SJan Schmidt * information | tree | tree | data | data 146d5c88b73SJan Schmidt * --------------------+--------+----------+--------+---------- 147d5c88b73SJan Schmidt * parent logical | y | - | y | - 148d5c88b73SJan Schmidt * key to resolve | - | - | - | y 149d5c88b73SJan Schmidt * tree block logical | y | y | y | y 150d5c88b73SJan Schmidt * root for resolving | - | y | y | y 151d5c88b73SJan Schmidt * 152d5c88b73SJan Schmidt * - column 1, 3: we've the parent -> done 153d5c88b73SJan Schmidt * - column 2: we take the first key from the block to find the parent 154d5c88b73SJan Schmidt * (see __add_missing_keys) 155d5c88b73SJan Schmidt * - column 4: we use the key to find the parent 156d5c88b73SJan Schmidt * 157d5c88b73SJan Schmidt * additional information that's available but not required to find the parent 158d5c88b73SJan Schmidt * block might help in merging entries to gain some speed. 159d5c88b73SJan Schmidt */ 160d5c88b73SJan Schmidt 1618da6d581SJan Schmidt static int __add_prelim_ref(struct list_head *head, u64 root_id, 162d5c88b73SJan Schmidt struct btrfs_key *key, int level, 163*742916b8SWang Shilong u64 parent, u64 wanted_disk_byte, int count, 164*742916b8SWang Shilong gfp_t gfp_mask) 1658da6d581SJan Schmidt { 1668da6d581SJan Schmidt struct __prelim_ref *ref; 1678da6d581SJan Schmidt 168*742916b8SWang Shilong ref = kmalloc(sizeof(*ref), gfp_mask); 1698da6d581SJan Schmidt if (!ref) 1708da6d581SJan Schmidt return -ENOMEM; 1718da6d581SJan Schmidt 1728da6d581SJan Schmidt ref->root_id = root_id; 1738da6d581SJan Schmidt if (key) 174d5c88b73SJan Schmidt ref->key_for_search = *key; 1758da6d581SJan Schmidt else 176d5c88b73SJan Schmidt memset(&ref->key_for_search, 0, sizeof(ref->key_for_search)); 1778da6d581SJan Schmidt 1783301958bSJan Schmidt ref->inode_list = NULL; 1798da6d581SJan Schmidt ref->level = level; 1808da6d581SJan Schmidt ref->count = count; 1818da6d581SJan Schmidt ref->parent = parent; 1828da6d581SJan Schmidt ref->wanted_disk_byte = wanted_disk_byte; 1838da6d581SJan Schmidt list_add_tail(&ref->list, head); 1848da6d581SJan Schmidt 1858da6d581SJan Schmidt return 0; 1868da6d581SJan Schmidt } 1878da6d581SJan Schmidt 1888da6d581SJan Schmidt static int add_all_parents(struct btrfs_root *root, struct btrfs_path *path, 189976b1908SJan Schmidt struct ulist *parents, int level, 19069bca40dSAlexander Block struct btrfs_key *key_for_search, u64 time_seq, 1913d7806ecSJan Schmidt u64 wanted_disk_byte, 192976b1908SJan Schmidt const u64 *extent_item_pos) 1938da6d581SJan Schmidt { 19469bca40dSAlexander Block int ret = 0; 19569bca40dSAlexander Block int slot; 19669bca40dSAlexander Block struct extent_buffer *eb; 19769bca40dSAlexander Block struct btrfs_key key; 1988da6d581SJan Schmidt struct btrfs_file_extent_item *fi; 199ed8c4913SJosef Bacik struct extent_inode_elem *eie = NULL, *old = NULL; 2008da6d581SJan Schmidt u64 disk_byte; 2018da6d581SJan Schmidt 20269bca40dSAlexander Block if (level != 0) { 20369bca40dSAlexander Block eb = path->nodes[level]; 20469bca40dSAlexander Block ret = ulist_add(parents, eb->start, 0, GFP_NOFS); 2053301958bSJan Schmidt if (ret < 0) 2063301958bSJan Schmidt return ret; 2078da6d581SJan Schmidt return 0; 20869bca40dSAlexander Block } 2098da6d581SJan Schmidt 2108da6d581SJan Schmidt /* 21169bca40dSAlexander Block * We normally enter this function with the path already pointing to 21269bca40dSAlexander Block * the first item to check. But sometimes, we may enter it with 21369bca40dSAlexander Block * slot==nritems. In that case, go to the next leaf before we continue. 2148da6d581SJan Schmidt */ 21569bca40dSAlexander Block if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) 2163d7806ecSJan Schmidt ret = btrfs_next_old_leaf(root, path, time_seq); 2178da6d581SJan Schmidt 21869bca40dSAlexander Block while (!ret) { 2198da6d581SJan Schmidt eb = path->nodes[0]; 22069bca40dSAlexander Block slot = path->slots[0]; 22169bca40dSAlexander Block 22269bca40dSAlexander Block btrfs_item_key_to_cpu(eb, &key, slot); 22369bca40dSAlexander Block 22469bca40dSAlexander Block if (key.objectid != key_for_search->objectid || 22569bca40dSAlexander Block key.type != BTRFS_EXTENT_DATA_KEY) 22669bca40dSAlexander Block break; 22769bca40dSAlexander Block 22869bca40dSAlexander Block fi = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item); 2298da6d581SJan Schmidt disk_byte = btrfs_file_extent_disk_bytenr(eb, fi); 23069bca40dSAlexander Block 23169bca40dSAlexander Block if (disk_byte == wanted_disk_byte) { 23269bca40dSAlexander Block eie = NULL; 233ed8c4913SJosef Bacik old = NULL; 23469bca40dSAlexander Block if (extent_item_pos) { 23569bca40dSAlexander Block ret = check_extent_in_eb(&key, eb, fi, 23669bca40dSAlexander Block *extent_item_pos, 23769bca40dSAlexander Block &eie); 23869bca40dSAlexander Block if (ret < 0) 23969bca40dSAlexander Block break; 2408da6d581SJan Schmidt } 241ed8c4913SJosef Bacik if (ret > 0) 242ed8c4913SJosef Bacik goto next; 243ed8c4913SJosef Bacik ret = ulist_add_merge(parents, eb->start, 244ed8c4913SJosef Bacik (uintptr_t)eie, 245ed8c4913SJosef Bacik (u64 *)&old, GFP_NOFS); 24669bca40dSAlexander Block if (ret < 0) 24769bca40dSAlexander Block break; 248ed8c4913SJosef Bacik if (!ret && extent_item_pos) { 249ed8c4913SJosef Bacik while (old->next) 250ed8c4913SJosef Bacik old = old->next; 251ed8c4913SJosef Bacik old->next = eie; 25269bca40dSAlexander Block } 25369bca40dSAlexander Block } 254ed8c4913SJosef Bacik next: 25569bca40dSAlexander Block ret = btrfs_next_old_item(root, path, time_seq); 2568da6d581SJan Schmidt } 2578da6d581SJan Schmidt 25869bca40dSAlexander Block if (ret > 0) 25969bca40dSAlexander Block ret = 0; 26069bca40dSAlexander Block return ret; 2618da6d581SJan Schmidt } 2628da6d581SJan Schmidt 2638da6d581SJan Schmidt /* 2648da6d581SJan Schmidt * resolve an indirect backref in the form (root_id, key, level) 2658da6d581SJan Schmidt * to a logical address 2668da6d581SJan Schmidt */ 2678da6d581SJan Schmidt static int __resolve_indirect_ref(struct btrfs_fs_info *fs_info, 268da61d31aSJosef Bacik struct btrfs_path *path, u64 time_seq, 2698da6d581SJan Schmidt struct __prelim_ref *ref, 270976b1908SJan Schmidt struct ulist *parents, 271976b1908SJan Schmidt const u64 *extent_item_pos) 2728da6d581SJan Schmidt { 2738da6d581SJan Schmidt struct btrfs_root *root; 2748da6d581SJan Schmidt struct btrfs_key root_key; 2758da6d581SJan Schmidt struct extent_buffer *eb; 2768da6d581SJan Schmidt int ret = 0; 2778da6d581SJan Schmidt int root_level; 2788da6d581SJan Schmidt int level = ref->level; 2798da6d581SJan Schmidt 2808da6d581SJan Schmidt root_key.objectid = ref->root_id; 2818da6d581SJan Schmidt root_key.type = BTRFS_ROOT_ITEM_KEY; 2828da6d581SJan Schmidt root_key.offset = (u64)-1; 2838da6d581SJan Schmidt root = btrfs_read_fs_root_no_name(fs_info, &root_key); 2848da6d581SJan Schmidt if (IS_ERR(root)) { 2858da6d581SJan Schmidt ret = PTR_ERR(root); 2868da6d581SJan Schmidt goto out; 2878da6d581SJan Schmidt } 2888da6d581SJan Schmidt 2895b6602e7SJan Schmidt root_level = btrfs_old_root_level(root, time_seq); 2908da6d581SJan Schmidt 2918da6d581SJan Schmidt if (root_level + 1 == level) 2928da6d581SJan Schmidt goto out; 2938da6d581SJan Schmidt 2948da6d581SJan Schmidt path->lowest_level = level; 2958445f61cSJan Schmidt ret = btrfs_search_old_slot(root, &ref->key_for_search, path, time_seq); 2968da6d581SJan Schmidt pr_debug("search slot in root %llu (level %d, ref count %d) returned " 2978da6d581SJan Schmidt "%d for key (%llu %u %llu)\n", 298c1c9ff7cSGeert Uytterhoeven ref->root_id, level, ref->count, ret, 299c1c9ff7cSGeert Uytterhoeven ref->key_for_search.objectid, ref->key_for_search.type, 300c1c9ff7cSGeert Uytterhoeven ref->key_for_search.offset); 3018da6d581SJan Schmidt if (ret < 0) 3028da6d581SJan Schmidt goto out; 3038da6d581SJan Schmidt 3048da6d581SJan Schmidt eb = path->nodes[level]; 3059345457fSJan Schmidt while (!eb) { 3069345457fSJan Schmidt if (!level) { 3078da6d581SJan Schmidt WARN_ON(1); 3088da6d581SJan Schmidt ret = 1; 3098da6d581SJan Schmidt goto out; 3108da6d581SJan Schmidt } 3119345457fSJan Schmidt level--; 3129345457fSJan Schmidt eb = path->nodes[level]; 3139345457fSJan Schmidt } 3148da6d581SJan Schmidt 31569bca40dSAlexander Block ret = add_all_parents(root, path, parents, level, &ref->key_for_search, 31669bca40dSAlexander Block time_seq, ref->wanted_disk_byte, 31769bca40dSAlexander Block extent_item_pos); 3188da6d581SJan Schmidt out: 319da61d31aSJosef Bacik path->lowest_level = 0; 320da61d31aSJosef Bacik btrfs_release_path(path); 3218da6d581SJan Schmidt return ret; 3228da6d581SJan Schmidt } 3238da6d581SJan Schmidt 3248da6d581SJan Schmidt /* 3258da6d581SJan Schmidt * resolve all indirect backrefs from the list 3268da6d581SJan Schmidt */ 3278da6d581SJan Schmidt static int __resolve_indirect_refs(struct btrfs_fs_info *fs_info, 328da61d31aSJosef Bacik struct btrfs_path *path, u64 time_seq, 329976b1908SJan Schmidt struct list_head *head, 330976b1908SJan Schmidt const u64 *extent_item_pos) 3318da6d581SJan Schmidt { 3328da6d581SJan Schmidt int err; 3338da6d581SJan Schmidt int ret = 0; 3348da6d581SJan Schmidt struct __prelim_ref *ref; 3358da6d581SJan Schmidt struct __prelim_ref *ref_safe; 3368da6d581SJan Schmidt struct __prelim_ref *new_ref; 3378da6d581SJan Schmidt struct ulist *parents; 3388da6d581SJan Schmidt struct ulist_node *node; 339cd1b413cSJan Schmidt struct ulist_iterator uiter; 3408da6d581SJan Schmidt 3418da6d581SJan Schmidt parents = ulist_alloc(GFP_NOFS); 3428da6d581SJan Schmidt if (!parents) 3438da6d581SJan Schmidt return -ENOMEM; 3448da6d581SJan Schmidt 3458da6d581SJan Schmidt /* 3468da6d581SJan Schmidt * _safe allows us to insert directly after the current item without 3478da6d581SJan Schmidt * iterating over the newly inserted items. 3488da6d581SJan Schmidt * we're also allowed to re-assign ref during iteration. 3498da6d581SJan Schmidt */ 3508da6d581SJan Schmidt list_for_each_entry_safe(ref, ref_safe, head, list) { 3518da6d581SJan Schmidt if (ref->parent) /* already direct */ 3528da6d581SJan Schmidt continue; 3538da6d581SJan Schmidt if (ref->count == 0) 3548da6d581SJan Schmidt continue; 355da61d31aSJosef Bacik err = __resolve_indirect_ref(fs_info, path, time_seq, ref, 356da61d31aSJosef Bacik parents, extent_item_pos); 357e36902d4SWang Shilong if (err == -ENOMEM) 358e36902d4SWang Shilong goto out; 359ca60ebfaSJan Schmidt if (err) 3608da6d581SJan Schmidt continue; 3618da6d581SJan Schmidt 3628da6d581SJan Schmidt /* we put the first parent into the ref at hand */ 363cd1b413cSJan Schmidt ULIST_ITER_INIT(&uiter); 364cd1b413cSJan Schmidt node = ulist_next(parents, &uiter); 3658da6d581SJan Schmidt ref->parent = node ? node->val : 0; 366995e01b7SJan Schmidt ref->inode_list = node ? 36735a3621bSStefan Behrens (struct extent_inode_elem *)(uintptr_t)node->aux : NULL; 3688da6d581SJan Schmidt 3698da6d581SJan Schmidt /* additional parents require new refs being added here */ 370cd1b413cSJan Schmidt while ((node = ulist_next(parents, &uiter))) { 3718da6d581SJan Schmidt new_ref = kmalloc(sizeof(*new_ref), GFP_NOFS); 3728da6d581SJan Schmidt if (!new_ref) { 3738da6d581SJan Schmidt ret = -ENOMEM; 374e36902d4SWang Shilong goto out; 3758da6d581SJan Schmidt } 3768da6d581SJan Schmidt memcpy(new_ref, ref, sizeof(*ref)); 3778da6d581SJan Schmidt new_ref->parent = node->val; 378995e01b7SJan Schmidt new_ref->inode_list = (struct extent_inode_elem *) 379995e01b7SJan Schmidt (uintptr_t)node->aux; 3808da6d581SJan Schmidt list_add(&new_ref->list, &ref->list); 3818da6d581SJan Schmidt } 3828da6d581SJan Schmidt ulist_reinit(parents); 3838da6d581SJan Schmidt } 384e36902d4SWang Shilong out: 3858da6d581SJan Schmidt ulist_free(parents); 3868da6d581SJan Schmidt return ret; 3878da6d581SJan Schmidt } 3888da6d581SJan Schmidt 389d5c88b73SJan Schmidt static inline int ref_for_same_block(struct __prelim_ref *ref1, 390d5c88b73SJan Schmidt struct __prelim_ref *ref2) 391d5c88b73SJan Schmidt { 392d5c88b73SJan Schmidt if (ref1->level != ref2->level) 393d5c88b73SJan Schmidt return 0; 394d5c88b73SJan Schmidt if (ref1->root_id != ref2->root_id) 395d5c88b73SJan Schmidt return 0; 396d5c88b73SJan Schmidt if (ref1->key_for_search.type != ref2->key_for_search.type) 397d5c88b73SJan Schmidt return 0; 398d5c88b73SJan Schmidt if (ref1->key_for_search.objectid != ref2->key_for_search.objectid) 399d5c88b73SJan Schmidt return 0; 400d5c88b73SJan Schmidt if (ref1->key_for_search.offset != ref2->key_for_search.offset) 401d5c88b73SJan Schmidt return 0; 402d5c88b73SJan Schmidt if (ref1->parent != ref2->parent) 403d5c88b73SJan Schmidt return 0; 404d5c88b73SJan Schmidt 405d5c88b73SJan Schmidt return 1; 406d5c88b73SJan Schmidt } 407d5c88b73SJan Schmidt 408d5c88b73SJan Schmidt /* 409d5c88b73SJan Schmidt * read tree blocks and add keys where required. 410d5c88b73SJan Schmidt */ 411d5c88b73SJan Schmidt static int __add_missing_keys(struct btrfs_fs_info *fs_info, 412d5c88b73SJan Schmidt struct list_head *head) 413d5c88b73SJan Schmidt { 414d5c88b73SJan Schmidt struct list_head *pos; 415d5c88b73SJan Schmidt struct extent_buffer *eb; 416d5c88b73SJan Schmidt 417d5c88b73SJan Schmidt list_for_each(pos, head) { 418d5c88b73SJan Schmidt struct __prelim_ref *ref; 419d5c88b73SJan Schmidt ref = list_entry(pos, struct __prelim_ref, list); 420d5c88b73SJan Schmidt 421d5c88b73SJan Schmidt if (ref->parent) 422d5c88b73SJan Schmidt continue; 423d5c88b73SJan Schmidt if (ref->key_for_search.type) 424d5c88b73SJan Schmidt continue; 425d5c88b73SJan Schmidt BUG_ON(!ref->wanted_disk_byte); 426d5c88b73SJan Schmidt eb = read_tree_block(fs_info->tree_root, ref->wanted_disk_byte, 427d5c88b73SJan Schmidt fs_info->tree_root->leafsize, 0); 428416bc658SJosef Bacik if (!eb || !extent_buffer_uptodate(eb)) { 429416bc658SJosef Bacik free_extent_buffer(eb); 430416bc658SJosef Bacik return -EIO; 431416bc658SJosef Bacik } 432d5c88b73SJan Schmidt btrfs_tree_read_lock(eb); 433d5c88b73SJan Schmidt if (btrfs_header_level(eb) == 0) 434d5c88b73SJan Schmidt btrfs_item_key_to_cpu(eb, &ref->key_for_search, 0); 435d5c88b73SJan Schmidt else 436d5c88b73SJan Schmidt btrfs_node_key_to_cpu(eb, &ref->key_for_search, 0); 437d5c88b73SJan Schmidt btrfs_tree_read_unlock(eb); 438d5c88b73SJan Schmidt free_extent_buffer(eb); 439d5c88b73SJan Schmidt } 440d5c88b73SJan Schmidt return 0; 441d5c88b73SJan Schmidt } 442d5c88b73SJan Schmidt 4438da6d581SJan Schmidt /* 4448da6d581SJan Schmidt * merge two lists of backrefs and adjust counts accordingly 4458da6d581SJan Schmidt * 4468da6d581SJan Schmidt * mode = 1: merge identical keys, if key is set 447d5c88b73SJan Schmidt * FIXME: if we add more keys in __add_prelim_ref, we can merge more here. 448d5c88b73SJan Schmidt * additionally, we could even add a key range for the blocks we 449d5c88b73SJan Schmidt * looked into to merge even more (-> replace unresolved refs by those 450d5c88b73SJan Schmidt * having a parent). 4518da6d581SJan Schmidt * mode = 2: merge identical parents 4528da6d581SJan Schmidt */ 453692206b1SWang Shilong static void __merge_refs(struct list_head *head, int mode) 4548da6d581SJan Schmidt { 4558da6d581SJan Schmidt struct list_head *pos1; 4568da6d581SJan Schmidt 4578da6d581SJan Schmidt list_for_each(pos1, head) { 4588da6d581SJan Schmidt struct list_head *n2; 4598da6d581SJan Schmidt struct list_head *pos2; 4608da6d581SJan Schmidt struct __prelim_ref *ref1; 4618da6d581SJan Schmidt 4628da6d581SJan Schmidt ref1 = list_entry(pos1, struct __prelim_ref, list); 4638da6d581SJan Schmidt 4648da6d581SJan Schmidt for (pos2 = pos1->next, n2 = pos2->next; pos2 != head; 4658da6d581SJan Schmidt pos2 = n2, n2 = pos2->next) { 4668da6d581SJan Schmidt struct __prelim_ref *ref2; 467d5c88b73SJan Schmidt struct __prelim_ref *xchg; 4683ef5969cSAlexander Block struct extent_inode_elem *eie; 4698da6d581SJan Schmidt 4708da6d581SJan Schmidt ref2 = list_entry(pos2, struct __prelim_ref, list); 4718da6d581SJan Schmidt 4728da6d581SJan Schmidt if (mode == 1) { 473d5c88b73SJan Schmidt if (!ref_for_same_block(ref1, ref2)) 4748da6d581SJan Schmidt continue; 475d5c88b73SJan Schmidt if (!ref1->parent && ref2->parent) { 476d5c88b73SJan Schmidt xchg = ref1; 477d5c88b73SJan Schmidt ref1 = ref2; 478d5c88b73SJan Schmidt ref2 = xchg; 479d5c88b73SJan Schmidt } 4808da6d581SJan Schmidt } else { 4818da6d581SJan Schmidt if (ref1->parent != ref2->parent) 4828da6d581SJan Schmidt continue; 4838da6d581SJan Schmidt } 4843ef5969cSAlexander Block 4853ef5969cSAlexander Block eie = ref1->inode_list; 4863ef5969cSAlexander Block while (eie && eie->next) 4873ef5969cSAlexander Block eie = eie->next; 4883ef5969cSAlexander Block if (eie) 4893ef5969cSAlexander Block eie->next = ref2->inode_list; 4903ef5969cSAlexander Block else 4913ef5969cSAlexander Block ref1->inode_list = ref2->inode_list; 4923ef5969cSAlexander Block ref1->count += ref2->count; 4933ef5969cSAlexander Block 4948da6d581SJan Schmidt list_del(&ref2->list); 4958da6d581SJan Schmidt kfree(ref2); 4968da6d581SJan Schmidt } 4978da6d581SJan Schmidt 4988da6d581SJan Schmidt } 4998da6d581SJan Schmidt } 5008da6d581SJan Schmidt 5018da6d581SJan Schmidt /* 5028da6d581SJan Schmidt * add all currently queued delayed refs from this head whose seq nr is 5038da6d581SJan Schmidt * smaller or equal that seq to the list 5048da6d581SJan Schmidt */ 5058da6d581SJan Schmidt static int __add_delayed_refs(struct btrfs_delayed_ref_head *head, u64 seq, 5068da6d581SJan Schmidt struct list_head *prefs) 5078da6d581SJan Schmidt { 5088da6d581SJan Schmidt struct btrfs_delayed_extent_op *extent_op = head->extent_op; 5098da6d581SJan Schmidt struct rb_node *n = &head->node.rb_node; 510d5c88b73SJan Schmidt struct btrfs_key key; 511d5c88b73SJan Schmidt struct btrfs_key op_key = {0}; 5128da6d581SJan Schmidt int sgn; 513b1375d64SJan Schmidt int ret = 0; 5148da6d581SJan Schmidt 5158da6d581SJan Schmidt if (extent_op && extent_op->update_key) 516d5c88b73SJan Schmidt btrfs_disk_key_to_cpu(&op_key, &extent_op->key); 5178da6d581SJan Schmidt 5188da6d581SJan Schmidt while ((n = rb_prev(n))) { 5198da6d581SJan Schmidt struct btrfs_delayed_ref_node *node; 5208da6d581SJan Schmidt node = rb_entry(n, struct btrfs_delayed_ref_node, 5218da6d581SJan Schmidt rb_node); 5228da6d581SJan Schmidt if (node->bytenr != head->node.bytenr) 5238da6d581SJan Schmidt break; 5248da6d581SJan Schmidt WARN_ON(node->is_head); 5258da6d581SJan Schmidt 5268da6d581SJan Schmidt if (node->seq > seq) 5278da6d581SJan Schmidt continue; 5288da6d581SJan Schmidt 5298da6d581SJan Schmidt switch (node->action) { 5308da6d581SJan Schmidt case BTRFS_ADD_DELAYED_EXTENT: 5318da6d581SJan Schmidt case BTRFS_UPDATE_DELAYED_HEAD: 5328da6d581SJan Schmidt WARN_ON(1); 5338da6d581SJan Schmidt continue; 5348da6d581SJan Schmidt case BTRFS_ADD_DELAYED_REF: 5358da6d581SJan Schmidt sgn = 1; 5368da6d581SJan Schmidt break; 5378da6d581SJan Schmidt case BTRFS_DROP_DELAYED_REF: 5388da6d581SJan Schmidt sgn = -1; 5398da6d581SJan Schmidt break; 5408da6d581SJan Schmidt default: 5418da6d581SJan Schmidt BUG_ON(1); 5428da6d581SJan Schmidt } 5438da6d581SJan Schmidt switch (node->type) { 5448da6d581SJan Schmidt case BTRFS_TREE_BLOCK_REF_KEY: { 5458da6d581SJan Schmidt struct btrfs_delayed_tree_ref *ref; 5468da6d581SJan Schmidt 5478da6d581SJan Schmidt ref = btrfs_delayed_node_to_tree_ref(node); 548d5c88b73SJan Schmidt ret = __add_prelim_ref(prefs, ref->root, &op_key, 5498da6d581SJan Schmidt ref->level + 1, 0, node->bytenr, 550*742916b8SWang Shilong node->ref_mod * sgn, GFP_ATOMIC); 5518da6d581SJan Schmidt break; 5528da6d581SJan Schmidt } 5538da6d581SJan Schmidt case BTRFS_SHARED_BLOCK_REF_KEY: { 5548da6d581SJan Schmidt struct btrfs_delayed_tree_ref *ref; 5558da6d581SJan Schmidt 5568da6d581SJan Schmidt ref = btrfs_delayed_node_to_tree_ref(node); 557d5c88b73SJan Schmidt ret = __add_prelim_ref(prefs, ref->root, NULL, 5588da6d581SJan Schmidt ref->level + 1, ref->parent, 5598da6d581SJan Schmidt node->bytenr, 560*742916b8SWang Shilong node->ref_mod * sgn, GFP_ATOMIC); 5618da6d581SJan Schmidt break; 5628da6d581SJan Schmidt } 5638da6d581SJan Schmidt case BTRFS_EXTENT_DATA_REF_KEY: { 5648da6d581SJan Schmidt struct btrfs_delayed_data_ref *ref; 5658da6d581SJan Schmidt ref = btrfs_delayed_node_to_data_ref(node); 5668da6d581SJan Schmidt 5678da6d581SJan Schmidt key.objectid = ref->objectid; 5688da6d581SJan Schmidt key.type = BTRFS_EXTENT_DATA_KEY; 5698da6d581SJan Schmidt key.offset = ref->offset; 5708da6d581SJan Schmidt ret = __add_prelim_ref(prefs, ref->root, &key, 0, 0, 5718da6d581SJan Schmidt node->bytenr, 572*742916b8SWang Shilong node->ref_mod * sgn, GFP_ATOMIC); 5738da6d581SJan Schmidt break; 5748da6d581SJan Schmidt } 5758da6d581SJan Schmidt case BTRFS_SHARED_DATA_REF_KEY: { 5768da6d581SJan Schmidt struct btrfs_delayed_data_ref *ref; 5778da6d581SJan Schmidt 5788da6d581SJan Schmidt ref = btrfs_delayed_node_to_data_ref(node); 5798da6d581SJan Schmidt 5808da6d581SJan Schmidt key.objectid = ref->objectid; 5818da6d581SJan Schmidt key.type = BTRFS_EXTENT_DATA_KEY; 5828da6d581SJan Schmidt key.offset = ref->offset; 5838da6d581SJan Schmidt ret = __add_prelim_ref(prefs, ref->root, &key, 0, 5848da6d581SJan Schmidt ref->parent, node->bytenr, 585*742916b8SWang Shilong node->ref_mod * sgn, GFP_ATOMIC); 5868da6d581SJan Schmidt break; 5878da6d581SJan Schmidt } 5888da6d581SJan Schmidt default: 5898da6d581SJan Schmidt WARN_ON(1); 5908da6d581SJan Schmidt } 5911149ab6bSWang Shilong if (ret) 5921149ab6bSWang Shilong return ret; 5938da6d581SJan Schmidt } 5948da6d581SJan Schmidt 5958da6d581SJan Schmidt return 0; 5968da6d581SJan Schmidt } 5978da6d581SJan Schmidt 5988da6d581SJan Schmidt /* 5998da6d581SJan Schmidt * add all inline backrefs for bytenr to the list 6008da6d581SJan Schmidt */ 6018da6d581SJan Schmidt static int __add_inline_refs(struct btrfs_fs_info *fs_info, 6028da6d581SJan Schmidt struct btrfs_path *path, u64 bytenr, 603d5c88b73SJan Schmidt int *info_level, struct list_head *prefs) 6048da6d581SJan Schmidt { 605b1375d64SJan Schmidt int ret = 0; 6068da6d581SJan Schmidt int slot; 6078da6d581SJan Schmidt struct extent_buffer *leaf; 6088da6d581SJan Schmidt struct btrfs_key key; 609261c84b6SJosef Bacik struct btrfs_key found_key; 6108da6d581SJan Schmidt unsigned long ptr; 6118da6d581SJan Schmidt unsigned long end; 6128da6d581SJan Schmidt struct btrfs_extent_item *ei; 6138da6d581SJan Schmidt u64 flags; 6148da6d581SJan Schmidt u64 item_size; 6158da6d581SJan Schmidt 6168da6d581SJan Schmidt /* 6178da6d581SJan Schmidt * enumerate all inline refs 6188da6d581SJan Schmidt */ 6198da6d581SJan Schmidt leaf = path->nodes[0]; 620dadcaf78SJan Schmidt slot = path->slots[0]; 6218da6d581SJan Schmidt 6228da6d581SJan Schmidt item_size = btrfs_item_size_nr(leaf, slot); 6238da6d581SJan Schmidt BUG_ON(item_size < sizeof(*ei)); 6248da6d581SJan Schmidt 6258da6d581SJan Schmidt ei = btrfs_item_ptr(leaf, slot, struct btrfs_extent_item); 6268da6d581SJan Schmidt flags = btrfs_extent_flags(leaf, ei); 627261c84b6SJosef Bacik btrfs_item_key_to_cpu(leaf, &found_key, slot); 6288da6d581SJan Schmidt 6298da6d581SJan Schmidt ptr = (unsigned long)(ei + 1); 6308da6d581SJan Schmidt end = (unsigned long)ei + item_size; 6318da6d581SJan Schmidt 632261c84b6SJosef Bacik if (found_key.type == BTRFS_EXTENT_ITEM_KEY && 633261c84b6SJosef Bacik flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) { 6348da6d581SJan Schmidt struct btrfs_tree_block_info *info; 6358da6d581SJan Schmidt 6368da6d581SJan Schmidt info = (struct btrfs_tree_block_info *)ptr; 6378da6d581SJan Schmidt *info_level = btrfs_tree_block_level(leaf, info); 6388da6d581SJan Schmidt ptr += sizeof(struct btrfs_tree_block_info); 6398da6d581SJan Schmidt BUG_ON(ptr > end); 640261c84b6SJosef Bacik } else if (found_key.type == BTRFS_METADATA_ITEM_KEY) { 641261c84b6SJosef Bacik *info_level = found_key.offset; 6428da6d581SJan Schmidt } else { 6438da6d581SJan Schmidt BUG_ON(!(flags & BTRFS_EXTENT_FLAG_DATA)); 6448da6d581SJan Schmidt } 6458da6d581SJan Schmidt 6468da6d581SJan Schmidt while (ptr < end) { 6478da6d581SJan Schmidt struct btrfs_extent_inline_ref *iref; 6488da6d581SJan Schmidt u64 offset; 6498da6d581SJan Schmidt int type; 6508da6d581SJan Schmidt 6518da6d581SJan Schmidt iref = (struct btrfs_extent_inline_ref *)ptr; 6528da6d581SJan Schmidt type = btrfs_extent_inline_ref_type(leaf, iref); 6538da6d581SJan Schmidt offset = btrfs_extent_inline_ref_offset(leaf, iref); 6548da6d581SJan Schmidt 6558da6d581SJan Schmidt switch (type) { 6568da6d581SJan Schmidt case BTRFS_SHARED_BLOCK_REF_KEY: 657d5c88b73SJan Schmidt ret = __add_prelim_ref(prefs, 0, NULL, 6588da6d581SJan Schmidt *info_level + 1, offset, 659*742916b8SWang Shilong bytenr, 1, GFP_NOFS); 6608da6d581SJan Schmidt break; 6618da6d581SJan Schmidt case BTRFS_SHARED_DATA_REF_KEY: { 6628da6d581SJan Schmidt struct btrfs_shared_data_ref *sdref; 6638da6d581SJan Schmidt int count; 6648da6d581SJan Schmidt 6658da6d581SJan Schmidt sdref = (struct btrfs_shared_data_ref *)(iref + 1); 6668da6d581SJan Schmidt count = btrfs_shared_data_ref_count(leaf, sdref); 6678da6d581SJan Schmidt ret = __add_prelim_ref(prefs, 0, NULL, 0, offset, 668*742916b8SWang Shilong bytenr, count, GFP_NOFS); 6698da6d581SJan Schmidt break; 6708da6d581SJan Schmidt } 6718da6d581SJan Schmidt case BTRFS_TREE_BLOCK_REF_KEY: 672d5c88b73SJan Schmidt ret = __add_prelim_ref(prefs, offset, NULL, 673d5c88b73SJan Schmidt *info_level + 1, 0, 674*742916b8SWang Shilong bytenr, 1, GFP_NOFS); 6758da6d581SJan Schmidt break; 6768da6d581SJan Schmidt case BTRFS_EXTENT_DATA_REF_KEY: { 6778da6d581SJan Schmidt struct btrfs_extent_data_ref *dref; 6788da6d581SJan Schmidt int count; 6798da6d581SJan Schmidt u64 root; 6808da6d581SJan Schmidt 6818da6d581SJan Schmidt dref = (struct btrfs_extent_data_ref *)(&iref->offset); 6828da6d581SJan Schmidt count = btrfs_extent_data_ref_count(leaf, dref); 6838da6d581SJan Schmidt key.objectid = btrfs_extent_data_ref_objectid(leaf, 6848da6d581SJan Schmidt dref); 6858da6d581SJan Schmidt key.type = BTRFS_EXTENT_DATA_KEY; 6868da6d581SJan Schmidt key.offset = btrfs_extent_data_ref_offset(leaf, dref); 6878da6d581SJan Schmidt root = btrfs_extent_data_ref_root(leaf, dref); 688d5c88b73SJan Schmidt ret = __add_prelim_ref(prefs, root, &key, 0, 0, 689*742916b8SWang Shilong bytenr, count, GFP_NOFS); 6908da6d581SJan Schmidt break; 6918da6d581SJan Schmidt } 6928da6d581SJan Schmidt default: 6938da6d581SJan Schmidt WARN_ON(1); 6948da6d581SJan Schmidt } 6951149ab6bSWang Shilong if (ret) 6961149ab6bSWang Shilong return ret; 6978da6d581SJan Schmidt ptr += btrfs_extent_inline_ref_size(type); 6988da6d581SJan Schmidt } 6998da6d581SJan Schmidt 7008da6d581SJan Schmidt return 0; 7018da6d581SJan Schmidt } 7028da6d581SJan Schmidt 7038da6d581SJan Schmidt /* 7048da6d581SJan Schmidt * add all non-inline backrefs for bytenr to the list 7058da6d581SJan Schmidt */ 7068da6d581SJan Schmidt static int __add_keyed_refs(struct btrfs_fs_info *fs_info, 7078da6d581SJan Schmidt struct btrfs_path *path, u64 bytenr, 708d5c88b73SJan Schmidt int info_level, struct list_head *prefs) 7098da6d581SJan Schmidt { 7108da6d581SJan Schmidt struct btrfs_root *extent_root = fs_info->extent_root; 7118da6d581SJan Schmidt int ret; 7128da6d581SJan Schmidt int slot; 7138da6d581SJan Schmidt struct extent_buffer *leaf; 7148da6d581SJan Schmidt struct btrfs_key key; 7158da6d581SJan Schmidt 7168da6d581SJan Schmidt while (1) { 7178da6d581SJan Schmidt ret = btrfs_next_item(extent_root, path); 7188da6d581SJan Schmidt if (ret < 0) 7198da6d581SJan Schmidt break; 7208da6d581SJan Schmidt if (ret) { 7218da6d581SJan Schmidt ret = 0; 7228da6d581SJan Schmidt break; 7238da6d581SJan Schmidt } 7248da6d581SJan Schmidt 7258da6d581SJan Schmidt slot = path->slots[0]; 7268da6d581SJan Schmidt leaf = path->nodes[0]; 7278da6d581SJan Schmidt btrfs_item_key_to_cpu(leaf, &key, slot); 7288da6d581SJan Schmidt 7298da6d581SJan Schmidt if (key.objectid != bytenr) 7308da6d581SJan Schmidt break; 7318da6d581SJan Schmidt if (key.type < BTRFS_TREE_BLOCK_REF_KEY) 7328da6d581SJan Schmidt continue; 7338da6d581SJan Schmidt if (key.type > BTRFS_SHARED_DATA_REF_KEY) 7348da6d581SJan Schmidt break; 7358da6d581SJan Schmidt 7368da6d581SJan Schmidt switch (key.type) { 7378da6d581SJan Schmidt case BTRFS_SHARED_BLOCK_REF_KEY: 738d5c88b73SJan Schmidt ret = __add_prelim_ref(prefs, 0, NULL, 7398da6d581SJan Schmidt info_level + 1, key.offset, 740*742916b8SWang Shilong bytenr, 1, GFP_NOFS); 7418da6d581SJan Schmidt break; 7428da6d581SJan Schmidt case BTRFS_SHARED_DATA_REF_KEY: { 7438da6d581SJan Schmidt struct btrfs_shared_data_ref *sdref; 7448da6d581SJan Schmidt int count; 7458da6d581SJan Schmidt 7468da6d581SJan Schmidt sdref = btrfs_item_ptr(leaf, slot, 7478da6d581SJan Schmidt struct btrfs_shared_data_ref); 7488da6d581SJan Schmidt count = btrfs_shared_data_ref_count(leaf, sdref); 7498da6d581SJan Schmidt ret = __add_prelim_ref(prefs, 0, NULL, 0, key.offset, 750*742916b8SWang Shilong bytenr, count, GFP_NOFS); 7518da6d581SJan Schmidt break; 7528da6d581SJan Schmidt } 7538da6d581SJan Schmidt case BTRFS_TREE_BLOCK_REF_KEY: 754d5c88b73SJan Schmidt ret = __add_prelim_ref(prefs, key.offset, NULL, 755d5c88b73SJan Schmidt info_level + 1, 0, 756*742916b8SWang Shilong bytenr, 1, GFP_NOFS); 7578da6d581SJan Schmidt break; 7588da6d581SJan Schmidt case BTRFS_EXTENT_DATA_REF_KEY: { 7598da6d581SJan Schmidt struct btrfs_extent_data_ref *dref; 7608da6d581SJan Schmidt int count; 7618da6d581SJan Schmidt u64 root; 7628da6d581SJan Schmidt 7638da6d581SJan Schmidt dref = btrfs_item_ptr(leaf, slot, 7648da6d581SJan Schmidt struct btrfs_extent_data_ref); 7658da6d581SJan Schmidt count = btrfs_extent_data_ref_count(leaf, dref); 7668da6d581SJan Schmidt key.objectid = btrfs_extent_data_ref_objectid(leaf, 7678da6d581SJan Schmidt dref); 7688da6d581SJan Schmidt key.type = BTRFS_EXTENT_DATA_KEY; 7698da6d581SJan Schmidt key.offset = btrfs_extent_data_ref_offset(leaf, dref); 7708da6d581SJan Schmidt root = btrfs_extent_data_ref_root(leaf, dref); 7718da6d581SJan Schmidt ret = __add_prelim_ref(prefs, root, &key, 0, 0, 772*742916b8SWang Shilong bytenr, count, GFP_NOFS); 7738da6d581SJan Schmidt break; 7748da6d581SJan Schmidt } 7758da6d581SJan Schmidt default: 7768da6d581SJan Schmidt WARN_ON(1); 7778da6d581SJan Schmidt } 7781149ab6bSWang Shilong if (ret) 7791149ab6bSWang Shilong return ret; 7801149ab6bSWang Shilong 7818da6d581SJan Schmidt } 7828da6d581SJan Schmidt 7838da6d581SJan Schmidt return ret; 7848da6d581SJan Schmidt } 7858da6d581SJan Schmidt 7868da6d581SJan Schmidt /* 7878da6d581SJan Schmidt * this adds all existing backrefs (inline backrefs, backrefs and delayed 7888da6d581SJan Schmidt * refs) for the given bytenr to the refs list, merges duplicates and resolves 7898da6d581SJan Schmidt * indirect refs to their parent bytenr. 7908da6d581SJan Schmidt * When roots are found, they're added to the roots list 7918da6d581SJan Schmidt * 7928da6d581SJan Schmidt * FIXME some caching might speed things up 7938da6d581SJan Schmidt */ 7948da6d581SJan Schmidt static int find_parent_nodes(struct btrfs_trans_handle *trans, 7958da6d581SJan Schmidt struct btrfs_fs_info *fs_info, u64 bytenr, 796097b8a7cSJan Schmidt u64 time_seq, struct ulist *refs, 797097b8a7cSJan Schmidt struct ulist *roots, const u64 *extent_item_pos) 7988da6d581SJan Schmidt { 7998da6d581SJan Schmidt struct btrfs_key key; 8008da6d581SJan Schmidt struct btrfs_path *path; 8018da6d581SJan Schmidt struct btrfs_delayed_ref_root *delayed_refs = NULL; 802d3b01064SLi Zefan struct btrfs_delayed_ref_head *head; 8038da6d581SJan Schmidt int info_level = 0; 8048da6d581SJan Schmidt int ret; 8058da6d581SJan Schmidt struct list_head prefs_delayed; 8068da6d581SJan Schmidt struct list_head prefs; 8078da6d581SJan Schmidt struct __prelim_ref *ref; 8088da6d581SJan Schmidt 8098da6d581SJan Schmidt INIT_LIST_HEAD(&prefs); 8108da6d581SJan Schmidt INIT_LIST_HEAD(&prefs_delayed); 8118da6d581SJan Schmidt 8128da6d581SJan Schmidt key.objectid = bytenr; 8138da6d581SJan Schmidt key.offset = (u64)-1; 814261c84b6SJosef Bacik if (btrfs_fs_incompat(fs_info, SKINNY_METADATA)) 815261c84b6SJosef Bacik key.type = BTRFS_METADATA_ITEM_KEY; 816261c84b6SJosef Bacik else 817261c84b6SJosef Bacik key.type = BTRFS_EXTENT_ITEM_KEY; 8188da6d581SJan Schmidt 8198da6d581SJan Schmidt path = btrfs_alloc_path(); 8208da6d581SJan Schmidt if (!path) 8218da6d581SJan Schmidt return -ENOMEM; 822da61d31aSJosef Bacik if (!trans) 823da61d31aSJosef Bacik path->search_commit_root = 1; 8248da6d581SJan Schmidt 8258da6d581SJan Schmidt /* 8268da6d581SJan Schmidt * grab both a lock on the path and a lock on the delayed ref head. 8278da6d581SJan Schmidt * We need both to get a consistent picture of how the refs look 8288da6d581SJan Schmidt * at a specified point in time 8298da6d581SJan Schmidt */ 8308da6d581SJan Schmidt again: 831d3b01064SLi Zefan head = NULL; 832d3b01064SLi Zefan 8338da6d581SJan Schmidt ret = btrfs_search_slot(trans, fs_info->extent_root, &key, path, 0, 0); 8348da6d581SJan Schmidt if (ret < 0) 8358da6d581SJan Schmidt goto out; 8368da6d581SJan Schmidt BUG_ON(ret == 0); 8378da6d581SJan Schmidt 838da61d31aSJosef Bacik if (trans) { 8398da6d581SJan Schmidt /* 8407a3ae2f8SJan Schmidt * look if there are updates for this ref queued and lock the 8417a3ae2f8SJan Schmidt * head 8428da6d581SJan Schmidt */ 8438da6d581SJan Schmidt delayed_refs = &trans->transaction->delayed_refs; 8448da6d581SJan Schmidt spin_lock(&delayed_refs->lock); 8458da6d581SJan Schmidt head = btrfs_find_delayed_ref_head(trans, bytenr); 8468da6d581SJan Schmidt if (head) { 8478da6d581SJan Schmidt if (!mutex_trylock(&head->mutex)) { 8488da6d581SJan Schmidt atomic_inc(&head->node.refs); 8498da6d581SJan Schmidt spin_unlock(&delayed_refs->lock); 8508da6d581SJan Schmidt 8518da6d581SJan Schmidt btrfs_release_path(path); 8528da6d581SJan Schmidt 8538da6d581SJan Schmidt /* 8548da6d581SJan Schmidt * Mutex was contended, block until it's 8558da6d581SJan Schmidt * released and try again 8568da6d581SJan Schmidt */ 8578da6d581SJan Schmidt mutex_lock(&head->mutex); 8588da6d581SJan Schmidt mutex_unlock(&head->mutex); 8598da6d581SJan Schmidt btrfs_put_delayed_ref(&head->node); 8608da6d581SJan Schmidt goto again; 8618da6d581SJan Schmidt } 862097b8a7cSJan Schmidt ret = __add_delayed_refs(head, time_seq, 8638445f61cSJan Schmidt &prefs_delayed); 864155725c9SJan Schmidt mutex_unlock(&head->mutex); 865d3b01064SLi Zefan if (ret) { 866d3b01064SLi Zefan spin_unlock(&delayed_refs->lock); 8678da6d581SJan Schmidt goto out; 8688da6d581SJan Schmidt } 869d3b01064SLi Zefan } 8708da6d581SJan Schmidt spin_unlock(&delayed_refs->lock); 8717a3ae2f8SJan Schmidt } 8728da6d581SJan Schmidt 8738da6d581SJan Schmidt if (path->slots[0]) { 8748da6d581SJan Schmidt struct extent_buffer *leaf; 8758da6d581SJan Schmidt int slot; 8768da6d581SJan Schmidt 877dadcaf78SJan Schmidt path->slots[0]--; 8788da6d581SJan Schmidt leaf = path->nodes[0]; 879dadcaf78SJan Schmidt slot = path->slots[0]; 8808da6d581SJan Schmidt btrfs_item_key_to_cpu(leaf, &key, slot); 8818da6d581SJan Schmidt if (key.objectid == bytenr && 882261c84b6SJosef Bacik (key.type == BTRFS_EXTENT_ITEM_KEY || 883261c84b6SJosef Bacik key.type == BTRFS_METADATA_ITEM_KEY)) { 8848da6d581SJan Schmidt ret = __add_inline_refs(fs_info, path, bytenr, 885d5c88b73SJan Schmidt &info_level, &prefs); 8868da6d581SJan Schmidt if (ret) 8878da6d581SJan Schmidt goto out; 888d5c88b73SJan Schmidt ret = __add_keyed_refs(fs_info, path, bytenr, 8898da6d581SJan Schmidt info_level, &prefs); 8908da6d581SJan Schmidt if (ret) 8918da6d581SJan Schmidt goto out; 8928da6d581SJan Schmidt } 8938da6d581SJan Schmidt } 8948da6d581SJan Schmidt btrfs_release_path(path); 8958da6d581SJan Schmidt 8968da6d581SJan Schmidt list_splice_init(&prefs_delayed, &prefs); 8978da6d581SJan Schmidt 898d5c88b73SJan Schmidt ret = __add_missing_keys(fs_info, &prefs); 899d5c88b73SJan Schmidt if (ret) 900d5c88b73SJan Schmidt goto out; 901d5c88b73SJan Schmidt 902692206b1SWang Shilong __merge_refs(&prefs, 1); 9038da6d581SJan Schmidt 904da61d31aSJosef Bacik ret = __resolve_indirect_refs(fs_info, path, time_seq, &prefs, 905da61d31aSJosef Bacik extent_item_pos); 9068da6d581SJan Schmidt if (ret) 9078da6d581SJan Schmidt goto out; 9088da6d581SJan Schmidt 909692206b1SWang Shilong __merge_refs(&prefs, 2); 9108da6d581SJan Schmidt 9118da6d581SJan Schmidt while (!list_empty(&prefs)) { 9128da6d581SJan Schmidt ref = list_first_entry(&prefs, struct __prelim_ref, list); 9136c1500f2SJulia Lawall WARN_ON(ref->count < 0); 9148da6d581SJan Schmidt if (ref->count && ref->root_id && ref->parent == 0) { 9158da6d581SJan Schmidt /* no parent == root of tree */ 9168da6d581SJan Schmidt ret = ulist_add(roots, ref->root_id, 0, GFP_NOFS); 917f1723939SWang Shilong if (ret < 0) 918f1723939SWang Shilong goto out; 9198da6d581SJan Schmidt } 9208da6d581SJan Schmidt if (ref->count && ref->parent) { 921976b1908SJan Schmidt struct extent_inode_elem *eie = NULL; 9223301958bSJan Schmidt if (extent_item_pos && !ref->inode_list) { 923976b1908SJan Schmidt u32 bsz; 924976b1908SJan Schmidt struct extent_buffer *eb; 925976b1908SJan Schmidt bsz = btrfs_level_size(fs_info->extent_root, 926976b1908SJan Schmidt info_level); 927976b1908SJan Schmidt eb = read_tree_block(fs_info->extent_root, 928976b1908SJan Schmidt ref->parent, bsz, 0); 929416bc658SJosef Bacik if (!eb || !extent_buffer_uptodate(eb)) { 930416bc658SJosef Bacik free_extent_buffer(eb); 931c16c2e2eSWang Shilong ret = -EIO; 932c16c2e2eSWang Shilong goto out; 933416bc658SJosef Bacik } 934976b1908SJan Schmidt ret = find_extent_in_eb(eb, bytenr, 935976b1908SJan Schmidt *extent_item_pos, &eie); 936976b1908SJan Schmidt free_extent_buffer(eb); 937f5929cd8SFilipe David Borba Manana if (ret < 0) 938f5929cd8SFilipe David Borba Manana goto out; 939f5929cd8SFilipe David Borba Manana ref->inode_list = eie; 940976b1908SJan Schmidt } 9413301958bSJan Schmidt ret = ulist_add_merge(refs, ref->parent, 942995e01b7SJan Schmidt (uintptr_t)ref->inode_list, 94334d73f54SAlexander Block (u64 *)&eie, GFP_NOFS); 944f1723939SWang Shilong if (ret < 0) 945f1723939SWang Shilong goto out; 9463301958bSJan Schmidt if (!ret && extent_item_pos) { 9473301958bSJan Schmidt /* 9483301958bSJan Schmidt * we've recorded that parent, so we must extend 9493301958bSJan Schmidt * its inode list here 9503301958bSJan Schmidt */ 9513301958bSJan Schmidt BUG_ON(!eie); 9523301958bSJan Schmidt while (eie->next) 9533301958bSJan Schmidt eie = eie->next; 9543301958bSJan Schmidt eie->next = ref->inode_list; 9553301958bSJan Schmidt } 9568da6d581SJan Schmidt } 957a4fdb61eSWang Shilong list_del(&ref->list); 9588da6d581SJan Schmidt kfree(ref); 9598da6d581SJan Schmidt } 9608da6d581SJan Schmidt 9618da6d581SJan Schmidt out: 9628da6d581SJan Schmidt btrfs_free_path(path); 9638da6d581SJan Schmidt while (!list_empty(&prefs)) { 9648da6d581SJan Schmidt ref = list_first_entry(&prefs, struct __prelim_ref, list); 9658da6d581SJan Schmidt list_del(&ref->list); 9668da6d581SJan Schmidt kfree(ref); 9678da6d581SJan Schmidt } 9688da6d581SJan Schmidt while (!list_empty(&prefs_delayed)) { 9698da6d581SJan Schmidt ref = list_first_entry(&prefs_delayed, struct __prelim_ref, 9708da6d581SJan Schmidt list); 9718da6d581SJan Schmidt list_del(&ref->list); 9728da6d581SJan Schmidt kfree(ref); 9738da6d581SJan Schmidt } 9748da6d581SJan Schmidt 9758da6d581SJan Schmidt return ret; 9768da6d581SJan Schmidt } 9778da6d581SJan Schmidt 978976b1908SJan Schmidt static void free_leaf_list(struct ulist *blocks) 979976b1908SJan Schmidt { 980976b1908SJan Schmidt struct ulist_node *node = NULL; 981976b1908SJan Schmidt struct extent_inode_elem *eie; 982976b1908SJan Schmidt struct extent_inode_elem *eie_next; 983976b1908SJan Schmidt struct ulist_iterator uiter; 984976b1908SJan Schmidt 985976b1908SJan Schmidt ULIST_ITER_INIT(&uiter); 986976b1908SJan Schmidt while ((node = ulist_next(blocks, &uiter))) { 987976b1908SJan Schmidt if (!node->aux) 988976b1908SJan Schmidt continue; 989995e01b7SJan Schmidt eie = (struct extent_inode_elem *)(uintptr_t)node->aux; 990976b1908SJan Schmidt for (; eie; eie = eie_next) { 991976b1908SJan Schmidt eie_next = eie->next; 992976b1908SJan Schmidt kfree(eie); 993976b1908SJan Schmidt } 994976b1908SJan Schmidt node->aux = 0; 995976b1908SJan Schmidt } 996976b1908SJan Schmidt 997976b1908SJan Schmidt ulist_free(blocks); 998976b1908SJan Schmidt } 999976b1908SJan Schmidt 10008da6d581SJan Schmidt /* 10018da6d581SJan Schmidt * Finds all leafs with a reference to the specified combination of bytenr and 10028da6d581SJan Schmidt * offset. key_list_head will point to a list of corresponding keys (caller must 10038da6d581SJan Schmidt * free each list element). The leafs will be stored in the leafs ulist, which 10048da6d581SJan Schmidt * must be freed with ulist_free. 10058da6d581SJan Schmidt * 10068da6d581SJan Schmidt * returns 0 on success, <0 on error 10078da6d581SJan Schmidt */ 10088da6d581SJan Schmidt static int btrfs_find_all_leafs(struct btrfs_trans_handle *trans, 10098da6d581SJan Schmidt struct btrfs_fs_info *fs_info, u64 bytenr, 1010097b8a7cSJan Schmidt u64 time_seq, struct ulist **leafs, 1011976b1908SJan Schmidt const u64 *extent_item_pos) 10128da6d581SJan Schmidt { 10138da6d581SJan Schmidt struct ulist *tmp; 10148da6d581SJan Schmidt int ret; 10158da6d581SJan Schmidt 10168da6d581SJan Schmidt tmp = ulist_alloc(GFP_NOFS); 10178da6d581SJan Schmidt if (!tmp) 10188da6d581SJan Schmidt return -ENOMEM; 10198da6d581SJan Schmidt *leafs = ulist_alloc(GFP_NOFS); 10208da6d581SJan Schmidt if (!*leafs) { 10218da6d581SJan Schmidt ulist_free(tmp); 10228da6d581SJan Schmidt return -ENOMEM; 10238da6d581SJan Schmidt } 10248da6d581SJan Schmidt 1025097b8a7cSJan Schmidt ret = find_parent_nodes(trans, fs_info, bytenr, 10268445f61cSJan Schmidt time_seq, *leafs, tmp, extent_item_pos); 10278da6d581SJan Schmidt ulist_free(tmp); 10288da6d581SJan Schmidt 10298da6d581SJan Schmidt if (ret < 0 && ret != -ENOENT) { 1030976b1908SJan Schmidt free_leaf_list(*leafs); 10318da6d581SJan Schmidt return ret; 10328da6d581SJan Schmidt } 10338da6d581SJan Schmidt 10348da6d581SJan Schmidt return 0; 10358da6d581SJan Schmidt } 10368da6d581SJan Schmidt 10378da6d581SJan Schmidt /* 10388da6d581SJan Schmidt * walk all backrefs for a given extent to find all roots that reference this 10398da6d581SJan Schmidt * extent. Walking a backref means finding all extents that reference this 10408da6d581SJan Schmidt * extent and in turn walk the backrefs of those, too. Naturally this is a 10418da6d581SJan Schmidt * recursive process, but here it is implemented in an iterative fashion: We 10428da6d581SJan Schmidt * find all referencing extents for the extent in question and put them on a 10438da6d581SJan Schmidt * list. In turn, we find all referencing extents for those, further appending 10448da6d581SJan Schmidt * to the list. The way we iterate the list allows adding more elements after 10458da6d581SJan Schmidt * the current while iterating. The process stops when we reach the end of the 10468da6d581SJan Schmidt * list. Found roots are added to the roots list. 10478da6d581SJan Schmidt * 10488da6d581SJan Schmidt * returns 0 on success, < 0 on error. 10498da6d581SJan Schmidt */ 10508da6d581SJan Schmidt int btrfs_find_all_roots(struct btrfs_trans_handle *trans, 10518da6d581SJan Schmidt struct btrfs_fs_info *fs_info, u64 bytenr, 1052097b8a7cSJan Schmidt u64 time_seq, struct ulist **roots) 10538da6d581SJan Schmidt { 10548da6d581SJan Schmidt struct ulist *tmp; 10558da6d581SJan Schmidt struct ulist_node *node = NULL; 1056cd1b413cSJan Schmidt struct ulist_iterator uiter; 10578da6d581SJan Schmidt int ret; 10588da6d581SJan Schmidt 10598da6d581SJan Schmidt tmp = ulist_alloc(GFP_NOFS); 10608da6d581SJan Schmidt if (!tmp) 10618da6d581SJan Schmidt return -ENOMEM; 10628da6d581SJan Schmidt *roots = ulist_alloc(GFP_NOFS); 10638da6d581SJan Schmidt if (!*roots) { 10648da6d581SJan Schmidt ulist_free(tmp); 10658da6d581SJan Schmidt return -ENOMEM; 10668da6d581SJan Schmidt } 10678da6d581SJan Schmidt 1068cd1b413cSJan Schmidt ULIST_ITER_INIT(&uiter); 10698da6d581SJan Schmidt while (1) { 1070097b8a7cSJan Schmidt ret = find_parent_nodes(trans, fs_info, bytenr, 10718445f61cSJan Schmidt time_seq, tmp, *roots, NULL); 10728da6d581SJan Schmidt if (ret < 0 && ret != -ENOENT) { 10738da6d581SJan Schmidt ulist_free(tmp); 10748da6d581SJan Schmidt ulist_free(*roots); 10758da6d581SJan Schmidt return ret; 10768da6d581SJan Schmidt } 1077cd1b413cSJan Schmidt node = ulist_next(tmp, &uiter); 10788da6d581SJan Schmidt if (!node) 10798da6d581SJan Schmidt break; 10808da6d581SJan Schmidt bytenr = node->val; 10818da6d581SJan Schmidt } 10828da6d581SJan Schmidt 10838da6d581SJan Schmidt ulist_free(tmp); 10848da6d581SJan Schmidt return 0; 10858da6d581SJan Schmidt } 10868da6d581SJan Schmidt 10878da6d581SJan Schmidt 1088a542ad1bSJan Schmidt static int __inode_info(u64 inum, u64 ioff, u8 key_type, 1089a542ad1bSJan Schmidt struct btrfs_root *fs_root, struct btrfs_path *path, 1090a542ad1bSJan Schmidt struct btrfs_key *found_key) 1091a542ad1bSJan Schmidt { 1092a542ad1bSJan Schmidt int ret; 1093a542ad1bSJan Schmidt struct btrfs_key key; 1094a542ad1bSJan Schmidt struct extent_buffer *eb; 1095a542ad1bSJan Schmidt 1096a542ad1bSJan Schmidt key.type = key_type; 1097a542ad1bSJan Schmidt key.objectid = inum; 1098a542ad1bSJan Schmidt key.offset = ioff; 1099a542ad1bSJan Schmidt 1100a542ad1bSJan Schmidt ret = btrfs_search_slot(NULL, fs_root, &key, path, 0, 0); 1101a542ad1bSJan Schmidt if (ret < 0) 1102a542ad1bSJan Schmidt return ret; 1103a542ad1bSJan Schmidt 1104a542ad1bSJan Schmidt eb = path->nodes[0]; 1105a542ad1bSJan Schmidt if (ret && path->slots[0] >= btrfs_header_nritems(eb)) { 1106a542ad1bSJan Schmidt ret = btrfs_next_leaf(fs_root, path); 1107a542ad1bSJan Schmidt if (ret) 1108a542ad1bSJan Schmidt return ret; 1109a542ad1bSJan Schmidt eb = path->nodes[0]; 1110a542ad1bSJan Schmidt } 1111a542ad1bSJan Schmidt 1112a542ad1bSJan Schmidt btrfs_item_key_to_cpu(eb, found_key, path->slots[0]); 1113a542ad1bSJan Schmidt if (found_key->type != key.type || found_key->objectid != key.objectid) 1114a542ad1bSJan Schmidt return 1; 1115a542ad1bSJan Schmidt 1116a542ad1bSJan Schmidt return 0; 1117a542ad1bSJan Schmidt } 1118a542ad1bSJan Schmidt 1119a542ad1bSJan Schmidt /* 1120a542ad1bSJan Schmidt * this makes the path point to (inum INODE_ITEM ioff) 1121a542ad1bSJan Schmidt */ 1122a542ad1bSJan Schmidt int inode_item_info(u64 inum, u64 ioff, struct btrfs_root *fs_root, 1123a542ad1bSJan Schmidt struct btrfs_path *path) 1124a542ad1bSJan Schmidt { 1125a542ad1bSJan Schmidt struct btrfs_key key; 1126a542ad1bSJan Schmidt return __inode_info(inum, ioff, BTRFS_INODE_ITEM_KEY, fs_root, path, 1127a542ad1bSJan Schmidt &key); 1128a542ad1bSJan Schmidt } 1129a542ad1bSJan Schmidt 1130a542ad1bSJan Schmidt static int inode_ref_info(u64 inum, u64 ioff, struct btrfs_root *fs_root, 1131a542ad1bSJan Schmidt struct btrfs_path *path, 1132a542ad1bSJan Schmidt struct btrfs_key *found_key) 1133a542ad1bSJan Schmidt { 1134a542ad1bSJan Schmidt return __inode_info(inum, ioff, BTRFS_INODE_REF_KEY, fs_root, path, 1135a542ad1bSJan Schmidt found_key); 1136a542ad1bSJan Schmidt } 1137a542ad1bSJan Schmidt 1138f186373fSMark Fasheh int btrfs_find_one_extref(struct btrfs_root *root, u64 inode_objectid, 1139f186373fSMark Fasheh u64 start_off, struct btrfs_path *path, 1140f186373fSMark Fasheh struct btrfs_inode_extref **ret_extref, 1141f186373fSMark Fasheh u64 *found_off) 1142f186373fSMark Fasheh { 1143f186373fSMark Fasheh int ret, slot; 1144f186373fSMark Fasheh struct btrfs_key key; 1145f186373fSMark Fasheh struct btrfs_key found_key; 1146f186373fSMark Fasheh struct btrfs_inode_extref *extref; 1147f186373fSMark Fasheh struct extent_buffer *leaf; 1148f186373fSMark Fasheh unsigned long ptr; 1149f186373fSMark Fasheh 1150f186373fSMark Fasheh key.objectid = inode_objectid; 1151f186373fSMark Fasheh btrfs_set_key_type(&key, BTRFS_INODE_EXTREF_KEY); 1152f186373fSMark Fasheh key.offset = start_off; 1153f186373fSMark Fasheh 1154f186373fSMark Fasheh ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); 1155f186373fSMark Fasheh if (ret < 0) 1156f186373fSMark Fasheh return ret; 1157f186373fSMark Fasheh 1158f186373fSMark Fasheh while (1) { 1159f186373fSMark Fasheh leaf = path->nodes[0]; 1160f186373fSMark Fasheh slot = path->slots[0]; 1161f186373fSMark Fasheh if (slot >= btrfs_header_nritems(leaf)) { 1162f186373fSMark Fasheh /* 1163f186373fSMark Fasheh * If the item at offset is not found, 1164f186373fSMark Fasheh * btrfs_search_slot will point us to the slot 1165f186373fSMark Fasheh * where it should be inserted. In our case 1166f186373fSMark Fasheh * that will be the slot directly before the 1167f186373fSMark Fasheh * next INODE_REF_KEY_V2 item. In the case 1168f186373fSMark Fasheh * that we're pointing to the last slot in a 1169f186373fSMark Fasheh * leaf, we must move one leaf over. 1170f186373fSMark Fasheh */ 1171f186373fSMark Fasheh ret = btrfs_next_leaf(root, path); 1172f186373fSMark Fasheh if (ret) { 1173f186373fSMark Fasheh if (ret >= 1) 1174f186373fSMark Fasheh ret = -ENOENT; 1175f186373fSMark Fasheh break; 1176f186373fSMark Fasheh } 1177f186373fSMark Fasheh continue; 1178f186373fSMark Fasheh } 1179f186373fSMark Fasheh 1180f186373fSMark Fasheh btrfs_item_key_to_cpu(leaf, &found_key, slot); 1181f186373fSMark Fasheh 1182f186373fSMark Fasheh /* 1183f186373fSMark Fasheh * Check that we're still looking at an extended ref key for 1184f186373fSMark Fasheh * this particular objectid. If we have different 1185f186373fSMark Fasheh * objectid or type then there are no more to be found 1186f186373fSMark Fasheh * in the tree and we can exit. 1187f186373fSMark Fasheh */ 1188f186373fSMark Fasheh ret = -ENOENT; 1189f186373fSMark Fasheh if (found_key.objectid != inode_objectid) 1190f186373fSMark Fasheh break; 1191f186373fSMark Fasheh if (btrfs_key_type(&found_key) != BTRFS_INODE_EXTREF_KEY) 1192f186373fSMark Fasheh break; 1193f186373fSMark Fasheh 1194f186373fSMark Fasheh ret = 0; 1195f186373fSMark Fasheh ptr = btrfs_item_ptr_offset(leaf, path->slots[0]); 1196f186373fSMark Fasheh extref = (struct btrfs_inode_extref *)ptr; 1197f186373fSMark Fasheh *ret_extref = extref; 1198f186373fSMark Fasheh if (found_off) 1199f186373fSMark Fasheh *found_off = found_key.offset; 1200f186373fSMark Fasheh break; 1201f186373fSMark Fasheh } 1202f186373fSMark Fasheh 1203f186373fSMark Fasheh return ret; 1204f186373fSMark Fasheh } 1205f186373fSMark Fasheh 120648a3b636SEric Sandeen /* 120748a3b636SEric Sandeen * this iterates to turn a name (from iref/extref) into a full filesystem path. 120848a3b636SEric Sandeen * Elements of the path are separated by '/' and the path is guaranteed to be 120948a3b636SEric Sandeen * 0-terminated. the path is only given within the current file system. 121048a3b636SEric Sandeen * Therefore, it never starts with a '/'. the caller is responsible to provide 121148a3b636SEric Sandeen * "size" bytes in "dest". the dest buffer will be filled backwards. finally, 121248a3b636SEric Sandeen * the start point of the resulting string is returned. this pointer is within 121348a3b636SEric Sandeen * dest, normally. 121448a3b636SEric Sandeen * in case the path buffer would overflow, the pointer is decremented further 121548a3b636SEric Sandeen * as if output was written to the buffer, though no more output is actually 121648a3b636SEric Sandeen * generated. that way, the caller can determine how much space would be 121748a3b636SEric Sandeen * required for the path to fit into the buffer. in that case, the returned 121848a3b636SEric Sandeen * value will be smaller than dest. callers must check this! 121948a3b636SEric Sandeen */ 122096b5bd77SJan Schmidt char *btrfs_ref_to_path(struct btrfs_root *fs_root, struct btrfs_path *path, 1221d24bec3aSMark Fasheh u32 name_len, unsigned long name_off, 1222a542ad1bSJan Schmidt struct extent_buffer *eb_in, u64 parent, 1223a542ad1bSJan Schmidt char *dest, u32 size) 1224a542ad1bSJan Schmidt { 1225a542ad1bSJan Schmidt int slot; 1226a542ad1bSJan Schmidt u64 next_inum; 1227a542ad1bSJan Schmidt int ret; 1228661bec6bSGabriel de Perthuis s64 bytes_left = ((s64)size) - 1; 1229a542ad1bSJan Schmidt struct extent_buffer *eb = eb_in; 1230a542ad1bSJan Schmidt struct btrfs_key found_key; 1231b916a59aSJan Schmidt int leave_spinning = path->leave_spinning; 1232d24bec3aSMark Fasheh struct btrfs_inode_ref *iref; 1233a542ad1bSJan Schmidt 1234a542ad1bSJan Schmidt if (bytes_left >= 0) 1235a542ad1bSJan Schmidt dest[bytes_left] = '\0'; 1236a542ad1bSJan Schmidt 1237b916a59aSJan Schmidt path->leave_spinning = 1; 1238a542ad1bSJan Schmidt while (1) { 1239d24bec3aSMark Fasheh bytes_left -= name_len; 1240a542ad1bSJan Schmidt if (bytes_left >= 0) 1241a542ad1bSJan Schmidt read_extent_buffer(eb, dest + bytes_left, 1242d24bec3aSMark Fasheh name_off, name_len); 1243b916a59aSJan Schmidt if (eb != eb_in) { 1244b916a59aSJan Schmidt btrfs_tree_read_unlock_blocking(eb); 1245a542ad1bSJan Schmidt free_extent_buffer(eb); 1246b916a59aSJan Schmidt } 1247a542ad1bSJan Schmidt ret = inode_ref_info(parent, 0, fs_root, path, &found_key); 12488f24b496SJan Schmidt if (ret > 0) 12498f24b496SJan Schmidt ret = -ENOENT; 1250a542ad1bSJan Schmidt if (ret) 1251a542ad1bSJan Schmidt break; 1252d24bec3aSMark Fasheh 1253a542ad1bSJan Schmidt next_inum = found_key.offset; 1254a542ad1bSJan Schmidt 1255a542ad1bSJan Schmidt /* regular exit ahead */ 1256a542ad1bSJan Schmidt if (parent == next_inum) 1257a542ad1bSJan Schmidt break; 1258a542ad1bSJan Schmidt 1259a542ad1bSJan Schmidt slot = path->slots[0]; 1260a542ad1bSJan Schmidt eb = path->nodes[0]; 1261a542ad1bSJan Schmidt /* make sure we can use eb after releasing the path */ 1262b916a59aSJan Schmidt if (eb != eb_in) { 1263a542ad1bSJan Schmidt atomic_inc(&eb->refs); 1264b916a59aSJan Schmidt btrfs_tree_read_lock(eb); 1265b916a59aSJan Schmidt btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK); 1266b916a59aSJan Schmidt } 1267a542ad1bSJan Schmidt btrfs_release_path(path); 1268a542ad1bSJan Schmidt iref = btrfs_item_ptr(eb, slot, struct btrfs_inode_ref); 1269d24bec3aSMark Fasheh 1270d24bec3aSMark Fasheh name_len = btrfs_inode_ref_name_len(eb, iref); 1271d24bec3aSMark Fasheh name_off = (unsigned long)(iref + 1); 1272d24bec3aSMark Fasheh 1273a542ad1bSJan Schmidt parent = next_inum; 1274a542ad1bSJan Schmidt --bytes_left; 1275a542ad1bSJan Schmidt if (bytes_left >= 0) 1276a542ad1bSJan Schmidt dest[bytes_left] = '/'; 1277a542ad1bSJan Schmidt } 1278a542ad1bSJan Schmidt 1279a542ad1bSJan Schmidt btrfs_release_path(path); 1280b916a59aSJan Schmidt path->leave_spinning = leave_spinning; 1281a542ad1bSJan Schmidt 1282a542ad1bSJan Schmidt if (ret) 1283a542ad1bSJan Schmidt return ERR_PTR(ret); 1284a542ad1bSJan Schmidt 1285a542ad1bSJan Schmidt return dest + bytes_left; 1286a542ad1bSJan Schmidt } 1287a542ad1bSJan Schmidt 1288a542ad1bSJan Schmidt /* 1289a542ad1bSJan Schmidt * this makes the path point to (logical EXTENT_ITEM *) 1290a542ad1bSJan Schmidt * returns BTRFS_EXTENT_FLAG_DATA for data, BTRFS_EXTENT_FLAG_TREE_BLOCK for 1291a542ad1bSJan Schmidt * tree blocks and <0 on error. 1292a542ad1bSJan Schmidt */ 1293a542ad1bSJan Schmidt int extent_from_logical(struct btrfs_fs_info *fs_info, u64 logical, 129469917e43SLiu Bo struct btrfs_path *path, struct btrfs_key *found_key, 129569917e43SLiu Bo u64 *flags_ret) 1296a542ad1bSJan Schmidt { 1297a542ad1bSJan Schmidt int ret; 1298a542ad1bSJan Schmidt u64 flags; 1299261c84b6SJosef Bacik u64 size = 0; 1300a542ad1bSJan Schmidt u32 item_size; 1301a542ad1bSJan Schmidt struct extent_buffer *eb; 1302a542ad1bSJan Schmidt struct btrfs_extent_item *ei; 1303a542ad1bSJan Schmidt struct btrfs_key key; 1304a542ad1bSJan Schmidt 1305261c84b6SJosef Bacik if (btrfs_fs_incompat(fs_info, SKINNY_METADATA)) 1306261c84b6SJosef Bacik key.type = BTRFS_METADATA_ITEM_KEY; 1307261c84b6SJosef Bacik else 1308a542ad1bSJan Schmidt key.type = BTRFS_EXTENT_ITEM_KEY; 1309a542ad1bSJan Schmidt key.objectid = logical; 1310a542ad1bSJan Schmidt key.offset = (u64)-1; 1311a542ad1bSJan Schmidt 1312a542ad1bSJan Schmidt ret = btrfs_search_slot(NULL, fs_info->extent_root, &key, path, 0, 0); 1313a542ad1bSJan Schmidt if (ret < 0) 1314a542ad1bSJan Schmidt return ret; 1315a542ad1bSJan Schmidt ret = btrfs_previous_item(fs_info->extent_root, path, 1316a542ad1bSJan Schmidt 0, BTRFS_EXTENT_ITEM_KEY); 1317a542ad1bSJan Schmidt if (ret < 0) 1318a542ad1bSJan Schmidt return ret; 1319a542ad1bSJan Schmidt 1320a542ad1bSJan Schmidt btrfs_item_key_to_cpu(path->nodes[0], found_key, path->slots[0]); 1321261c84b6SJosef Bacik if (found_key->type == BTRFS_METADATA_ITEM_KEY) 1322261c84b6SJosef Bacik size = fs_info->extent_root->leafsize; 1323261c84b6SJosef Bacik else if (found_key->type == BTRFS_EXTENT_ITEM_KEY) 1324261c84b6SJosef Bacik size = found_key->offset; 1325261c84b6SJosef Bacik 1326261c84b6SJosef Bacik if ((found_key->type != BTRFS_EXTENT_ITEM_KEY && 1327261c84b6SJosef Bacik found_key->type != BTRFS_METADATA_ITEM_KEY) || 1328a542ad1bSJan Schmidt found_key->objectid > logical || 1329261c84b6SJosef Bacik found_key->objectid + size <= logical) { 1330c1c9ff7cSGeert Uytterhoeven pr_debug("logical %llu is not within any extent\n", logical); 1331a542ad1bSJan Schmidt return -ENOENT; 13324692cf58SJan Schmidt } 1333a542ad1bSJan Schmidt 1334a542ad1bSJan Schmidt eb = path->nodes[0]; 1335a542ad1bSJan Schmidt item_size = btrfs_item_size_nr(eb, path->slots[0]); 1336a542ad1bSJan Schmidt BUG_ON(item_size < sizeof(*ei)); 1337a542ad1bSJan Schmidt 1338a542ad1bSJan Schmidt ei = btrfs_item_ptr(eb, path->slots[0], struct btrfs_extent_item); 1339a542ad1bSJan Schmidt flags = btrfs_extent_flags(eb, ei); 1340a542ad1bSJan Schmidt 13414692cf58SJan Schmidt pr_debug("logical %llu is at position %llu within the extent (%llu " 13424692cf58SJan Schmidt "EXTENT_ITEM %llu) flags %#llx size %u\n", 1343c1c9ff7cSGeert Uytterhoeven logical, logical - found_key->objectid, found_key->objectid, 1344c1c9ff7cSGeert Uytterhoeven found_key->offset, flags, item_size); 134569917e43SLiu Bo 134669917e43SLiu Bo WARN_ON(!flags_ret); 134769917e43SLiu Bo if (flags_ret) { 1348a542ad1bSJan Schmidt if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) 134969917e43SLiu Bo *flags_ret = BTRFS_EXTENT_FLAG_TREE_BLOCK; 135069917e43SLiu Bo else if (flags & BTRFS_EXTENT_FLAG_DATA) 135169917e43SLiu Bo *flags_ret = BTRFS_EXTENT_FLAG_DATA; 135269917e43SLiu Bo else 135369917e43SLiu Bo BUG_ON(1); 135469917e43SLiu Bo return 0; 135569917e43SLiu Bo } 1356a542ad1bSJan Schmidt 1357a542ad1bSJan Schmidt return -EIO; 1358a542ad1bSJan Schmidt } 1359a542ad1bSJan Schmidt 1360a542ad1bSJan Schmidt /* 1361a542ad1bSJan Schmidt * helper function to iterate extent inline refs. ptr must point to a 0 value 1362a542ad1bSJan Schmidt * for the first call and may be modified. it is used to track state. 1363a542ad1bSJan Schmidt * if more refs exist, 0 is returned and the next call to 1364a542ad1bSJan Schmidt * __get_extent_inline_ref must pass the modified ptr parameter to get the 1365a542ad1bSJan Schmidt * next ref. after the last ref was processed, 1 is returned. 1366a542ad1bSJan Schmidt * returns <0 on error 1367a542ad1bSJan Schmidt */ 1368a542ad1bSJan Schmidt static int __get_extent_inline_ref(unsigned long *ptr, struct extent_buffer *eb, 1369a542ad1bSJan Schmidt struct btrfs_extent_item *ei, u32 item_size, 1370a542ad1bSJan Schmidt struct btrfs_extent_inline_ref **out_eiref, 1371a542ad1bSJan Schmidt int *out_type) 1372a542ad1bSJan Schmidt { 1373a542ad1bSJan Schmidt unsigned long end; 1374a542ad1bSJan Schmidt u64 flags; 1375a542ad1bSJan Schmidt struct btrfs_tree_block_info *info; 1376a542ad1bSJan Schmidt 1377a542ad1bSJan Schmidt if (!*ptr) { 1378a542ad1bSJan Schmidt /* first call */ 1379a542ad1bSJan Schmidt flags = btrfs_extent_flags(eb, ei); 1380a542ad1bSJan Schmidt if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) { 1381a542ad1bSJan Schmidt info = (struct btrfs_tree_block_info *)(ei + 1); 1382a542ad1bSJan Schmidt *out_eiref = 1383a542ad1bSJan Schmidt (struct btrfs_extent_inline_ref *)(info + 1); 1384a542ad1bSJan Schmidt } else { 1385a542ad1bSJan Schmidt *out_eiref = (struct btrfs_extent_inline_ref *)(ei + 1); 1386a542ad1bSJan Schmidt } 1387a542ad1bSJan Schmidt *ptr = (unsigned long)*out_eiref; 1388a542ad1bSJan Schmidt if ((void *)*ptr >= (void *)ei + item_size) 1389a542ad1bSJan Schmidt return -ENOENT; 1390a542ad1bSJan Schmidt } 1391a542ad1bSJan Schmidt 1392a542ad1bSJan Schmidt end = (unsigned long)ei + item_size; 1393a542ad1bSJan Schmidt *out_eiref = (struct btrfs_extent_inline_ref *)*ptr; 1394a542ad1bSJan Schmidt *out_type = btrfs_extent_inline_ref_type(eb, *out_eiref); 1395a542ad1bSJan Schmidt 1396a542ad1bSJan Schmidt *ptr += btrfs_extent_inline_ref_size(*out_type); 1397a542ad1bSJan Schmidt WARN_ON(*ptr > end); 1398a542ad1bSJan Schmidt if (*ptr == end) 1399a542ad1bSJan Schmidt return 1; /* last */ 1400a542ad1bSJan Schmidt 1401a542ad1bSJan Schmidt return 0; 1402a542ad1bSJan Schmidt } 1403a542ad1bSJan Schmidt 1404a542ad1bSJan Schmidt /* 1405a542ad1bSJan Schmidt * reads the tree block backref for an extent. tree level and root are returned 1406a542ad1bSJan Schmidt * through out_level and out_root. ptr must point to a 0 value for the first 1407a542ad1bSJan Schmidt * call and may be modified (see __get_extent_inline_ref comment). 1408a542ad1bSJan Schmidt * returns 0 if data was provided, 1 if there was no more data to provide or 1409a542ad1bSJan Schmidt * <0 on error. 1410a542ad1bSJan Schmidt */ 1411a542ad1bSJan Schmidt int tree_backref_for_extent(unsigned long *ptr, struct extent_buffer *eb, 1412a542ad1bSJan Schmidt struct btrfs_extent_item *ei, u32 item_size, 1413a542ad1bSJan Schmidt u64 *out_root, u8 *out_level) 1414a542ad1bSJan Schmidt { 1415a542ad1bSJan Schmidt int ret; 1416a542ad1bSJan Schmidt int type; 1417a542ad1bSJan Schmidt struct btrfs_tree_block_info *info; 1418a542ad1bSJan Schmidt struct btrfs_extent_inline_ref *eiref; 1419a542ad1bSJan Schmidt 1420a542ad1bSJan Schmidt if (*ptr == (unsigned long)-1) 1421a542ad1bSJan Schmidt return 1; 1422a542ad1bSJan Schmidt 1423a542ad1bSJan Schmidt while (1) { 1424a542ad1bSJan Schmidt ret = __get_extent_inline_ref(ptr, eb, ei, item_size, 1425a542ad1bSJan Schmidt &eiref, &type); 1426a542ad1bSJan Schmidt if (ret < 0) 1427a542ad1bSJan Schmidt return ret; 1428a542ad1bSJan Schmidt 1429a542ad1bSJan Schmidt if (type == BTRFS_TREE_BLOCK_REF_KEY || 1430a542ad1bSJan Schmidt type == BTRFS_SHARED_BLOCK_REF_KEY) 1431a542ad1bSJan Schmidt break; 1432a542ad1bSJan Schmidt 1433a542ad1bSJan Schmidt if (ret == 1) 1434a542ad1bSJan Schmidt return 1; 1435a542ad1bSJan Schmidt } 1436a542ad1bSJan Schmidt 1437a542ad1bSJan Schmidt /* we can treat both ref types equally here */ 1438a542ad1bSJan Schmidt info = (struct btrfs_tree_block_info *)(ei + 1); 1439a542ad1bSJan Schmidt *out_root = btrfs_extent_inline_ref_offset(eb, eiref); 1440a542ad1bSJan Schmidt *out_level = btrfs_tree_block_level(eb, info); 1441a542ad1bSJan Schmidt 1442a542ad1bSJan Schmidt if (ret == 1) 1443a542ad1bSJan Schmidt *ptr = (unsigned long)-1; 1444a542ad1bSJan Schmidt 1445a542ad1bSJan Schmidt return 0; 1446a542ad1bSJan Schmidt } 1447a542ad1bSJan Schmidt 1448976b1908SJan Schmidt static int iterate_leaf_refs(struct extent_inode_elem *inode_list, 1449976b1908SJan Schmidt u64 root, u64 extent_item_objectid, 14504692cf58SJan Schmidt iterate_extent_inodes_t *iterate, void *ctx) 1451a542ad1bSJan Schmidt { 1452976b1908SJan Schmidt struct extent_inode_elem *eie; 14534692cf58SJan Schmidt int ret = 0; 1454a542ad1bSJan Schmidt 1455976b1908SJan Schmidt for (eie = inode_list; eie; eie = eie->next) { 14564692cf58SJan Schmidt pr_debug("ref for %llu resolved, key (%llu EXTEND_DATA %llu), " 1457976b1908SJan Schmidt "root %llu\n", extent_item_objectid, 1458976b1908SJan Schmidt eie->inum, eie->offset, root); 1459976b1908SJan Schmidt ret = iterate(eie->inum, eie->offset, root, ctx); 14604692cf58SJan Schmidt if (ret) { 1461976b1908SJan Schmidt pr_debug("stopping iteration for %llu due to ret=%d\n", 1462976b1908SJan Schmidt extent_item_objectid, ret); 1463a542ad1bSJan Schmidt break; 1464a542ad1bSJan Schmidt } 1465a542ad1bSJan Schmidt } 1466a542ad1bSJan Schmidt 1467a542ad1bSJan Schmidt return ret; 1468a542ad1bSJan Schmidt } 1469a542ad1bSJan Schmidt 1470a542ad1bSJan Schmidt /* 1471a542ad1bSJan Schmidt * calls iterate() for every inode that references the extent identified by 14724692cf58SJan Schmidt * the given parameters. 1473a542ad1bSJan Schmidt * when the iterator function returns a non-zero value, iteration stops. 1474a542ad1bSJan Schmidt */ 1475a542ad1bSJan Schmidt int iterate_extent_inodes(struct btrfs_fs_info *fs_info, 14764692cf58SJan Schmidt u64 extent_item_objectid, u64 extent_item_pos, 14777a3ae2f8SJan Schmidt int search_commit_root, 1478a542ad1bSJan Schmidt iterate_extent_inodes_t *iterate, void *ctx) 1479a542ad1bSJan Schmidt { 1480a542ad1bSJan Schmidt int ret; 1481da61d31aSJosef Bacik struct btrfs_trans_handle *trans = NULL; 14827a3ae2f8SJan Schmidt struct ulist *refs = NULL; 14837a3ae2f8SJan Schmidt struct ulist *roots = NULL; 14844692cf58SJan Schmidt struct ulist_node *ref_node = NULL; 14854692cf58SJan Schmidt struct ulist_node *root_node = NULL; 14868445f61cSJan Schmidt struct seq_list tree_mod_seq_elem = {}; 1487cd1b413cSJan Schmidt struct ulist_iterator ref_uiter; 1488cd1b413cSJan Schmidt struct ulist_iterator root_uiter; 1489a542ad1bSJan Schmidt 14904692cf58SJan Schmidt pr_debug("resolving all inodes for extent %llu\n", 14914692cf58SJan Schmidt extent_item_objectid); 14924692cf58SJan Schmidt 1493da61d31aSJosef Bacik if (!search_commit_root) { 14947a3ae2f8SJan Schmidt trans = btrfs_join_transaction(fs_info->extent_root); 14957a3ae2f8SJan Schmidt if (IS_ERR(trans)) 14967a3ae2f8SJan Schmidt return PTR_ERR(trans); 14978445f61cSJan Schmidt btrfs_get_tree_mod_seq(fs_info, &tree_mod_seq_elem); 14987a3ae2f8SJan Schmidt } 14994692cf58SJan Schmidt 15004692cf58SJan Schmidt ret = btrfs_find_all_leafs(trans, fs_info, extent_item_objectid, 1501097b8a7cSJan Schmidt tree_mod_seq_elem.seq, &refs, 15028445f61cSJan Schmidt &extent_item_pos); 15034692cf58SJan Schmidt if (ret) 15044692cf58SJan Schmidt goto out; 15054692cf58SJan Schmidt 1506cd1b413cSJan Schmidt ULIST_ITER_INIT(&ref_uiter); 1507cd1b413cSJan Schmidt while (!ret && (ref_node = ulist_next(refs, &ref_uiter))) { 1508976b1908SJan Schmidt ret = btrfs_find_all_roots(trans, fs_info, ref_node->val, 15098445f61cSJan Schmidt tree_mod_seq_elem.seq, &roots); 15104692cf58SJan Schmidt if (ret) 1511a542ad1bSJan Schmidt break; 1512cd1b413cSJan Schmidt ULIST_ITER_INIT(&root_uiter); 1513cd1b413cSJan Schmidt while (!ret && (root_node = ulist_next(roots, &root_uiter))) { 1514976b1908SJan Schmidt pr_debug("root %llu references leaf %llu, data list " 151534d73f54SAlexander Block "%#llx\n", root_node->val, ref_node->val, 1516c1c9ff7cSGeert Uytterhoeven ref_node->aux); 1517995e01b7SJan Schmidt ret = iterate_leaf_refs((struct extent_inode_elem *) 1518995e01b7SJan Schmidt (uintptr_t)ref_node->aux, 1519995e01b7SJan Schmidt root_node->val, 1520995e01b7SJan Schmidt extent_item_objectid, 1521a542ad1bSJan Schmidt iterate, ctx); 15224692cf58SJan Schmidt } 1523976b1908SJan Schmidt ulist_free(roots); 1524a542ad1bSJan Schmidt } 1525a542ad1bSJan Schmidt 1526976b1908SJan Schmidt free_leaf_list(refs); 15274692cf58SJan Schmidt out: 15287a3ae2f8SJan Schmidt if (!search_commit_root) { 15298445f61cSJan Schmidt btrfs_put_tree_mod_seq(fs_info, &tree_mod_seq_elem); 15304692cf58SJan Schmidt btrfs_end_transaction(trans, fs_info->extent_root); 15317a3ae2f8SJan Schmidt } 15327a3ae2f8SJan Schmidt 1533a542ad1bSJan Schmidt return ret; 1534a542ad1bSJan Schmidt } 1535a542ad1bSJan Schmidt 1536a542ad1bSJan Schmidt int iterate_inodes_from_logical(u64 logical, struct btrfs_fs_info *fs_info, 1537a542ad1bSJan Schmidt struct btrfs_path *path, 1538a542ad1bSJan Schmidt iterate_extent_inodes_t *iterate, void *ctx) 1539a542ad1bSJan Schmidt { 1540a542ad1bSJan Schmidt int ret; 15414692cf58SJan Schmidt u64 extent_item_pos; 154269917e43SLiu Bo u64 flags = 0; 1543a542ad1bSJan Schmidt struct btrfs_key found_key; 15447a3ae2f8SJan Schmidt int search_commit_root = path->search_commit_root; 1545a542ad1bSJan Schmidt 154669917e43SLiu Bo ret = extent_from_logical(fs_info, logical, path, &found_key, &flags); 15474692cf58SJan Schmidt btrfs_release_path(path); 1548a542ad1bSJan Schmidt if (ret < 0) 1549a542ad1bSJan Schmidt return ret; 155069917e43SLiu Bo if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) 15513627bf45SStefan Behrens return -EINVAL; 1552a542ad1bSJan Schmidt 15534692cf58SJan Schmidt extent_item_pos = logical - found_key.objectid; 15547a3ae2f8SJan Schmidt ret = iterate_extent_inodes(fs_info, found_key.objectid, 15557a3ae2f8SJan Schmidt extent_item_pos, search_commit_root, 15567a3ae2f8SJan Schmidt iterate, ctx); 1557a542ad1bSJan Schmidt 1558a542ad1bSJan Schmidt return ret; 1559a542ad1bSJan Schmidt } 1560a542ad1bSJan Schmidt 1561d24bec3aSMark Fasheh typedef int (iterate_irefs_t)(u64 parent, u32 name_len, unsigned long name_off, 1562d24bec3aSMark Fasheh struct extent_buffer *eb, void *ctx); 1563d24bec3aSMark Fasheh 1564d24bec3aSMark Fasheh static int iterate_inode_refs(u64 inum, struct btrfs_root *fs_root, 1565a542ad1bSJan Schmidt struct btrfs_path *path, 1566a542ad1bSJan Schmidt iterate_irefs_t *iterate, void *ctx) 1567a542ad1bSJan Schmidt { 1568aefc1eb1SJan Schmidt int ret = 0; 1569a542ad1bSJan Schmidt int slot; 1570a542ad1bSJan Schmidt u32 cur; 1571a542ad1bSJan Schmidt u32 len; 1572a542ad1bSJan Schmidt u32 name_len; 1573a542ad1bSJan Schmidt u64 parent = 0; 1574a542ad1bSJan Schmidt int found = 0; 1575a542ad1bSJan Schmidt struct extent_buffer *eb; 1576a542ad1bSJan Schmidt struct btrfs_item *item; 1577a542ad1bSJan Schmidt struct btrfs_inode_ref *iref; 1578a542ad1bSJan Schmidt struct btrfs_key found_key; 1579a542ad1bSJan Schmidt 1580aefc1eb1SJan Schmidt while (!ret) { 1581b916a59aSJan Schmidt path->leave_spinning = 1; 1582a542ad1bSJan Schmidt ret = inode_ref_info(inum, parent ? parent+1 : 0, fs_root, path, 1583a542ad1bSJan Schmidt &found_key); 1584a542ad1bSJan Schmidt if (ret < 0) 1585a542ad1bSJan Schmidt break; 1586a542ad1bSJan Schmidt if (ret) { 1587a542ad1bSJan Schmidt ret = found ? 0 : -ENOENT; 1588a542ad1bSJan Schmidt break; 1589a542ad1bSJan Schmidt } 1590a542ad1bSJan Schmidt ++found; 1591a542ad1bSJan Schmidt 1592a542ad1bSJan Schmidt parent = found_key.offset; 1593a542ad1bSJan Schmidt slot = path->slots[0]; 1594a542ad1bSJan Schmidt eb = path->nodes[0]; 1595a542ad1bSJan Schmidt /* make sure we can use eb after releasing the path */ 1596a542ad1bSJan Schmidt atomic_inc(&eb->refs); 1597b916a59aSJan Schmidt btrfs_tree_read_lock(eb); 1598b916a59aSJan Schmidt btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK); 1599a542ad1bSJan Schmidt btrfs_release_path(path); 1600a542ad1bSJan Schmidt 1601a542ad1bSJan Schmidt item = btrfs_item_nr(eb, slot); 1602a542ad1bSJan Schmidt iref = btrfs_item_ptr(eb, slot, struct btrfs_inode_ref); 1603a542ad1bSJan Schmidt 1604a542ad1bSJan Schmidt for (cur = 0; cur < btrfs_item_size(eb, item); cur += len) { 1605a542ad1bSJan Schmidt name_len = btrfs_inode_ref_name_len(eb, iref); 1606a542ad1bSJan Schmidt /* path must be released before calling iterate()! */ 16074692cf58SJan Schmidt pr_debug("following ref at offset %u for inode %llu in " 1608c1c9ff7cSGeert Uytterhoeven "tree %llu\n", cur, found_key.objectid, 1609c1c9ff7cSGeert Uytterhoeven fs_root->objectid); 1610d24bec3aSMark Fasheh ret = iterate(parent, name_len, 1611d24bec3aSMark Fasheh (unsigned long)(iref + 1), eb, ctx); 1612aefc1eb1SJan Schmidt if (ret) 1613a542ad1bSJan Schmidt break; 1614a542ad1bSJan Schmidt len = sizeof(*iref) + name_len; 1615a542ad1bSJan Schmidt iref = (struct btrfs_inode_ref *)((char *)iref + len); 1616a542ad1bSJan Schmidt } 1617b916a59aSJan Schmidt btrfs_tree_read_unlock_blocking(eb); 1618a542ad1bSJan Schmidt free_extent_buffer(eb); 1619a542ad1bSJan Schmidt } 1620a542ad1bSJan Schmidt 1621a542ad1bSJan Schmidt btrfs_release_path(path); 1622a542ad1bSJan Schmidt 1623a542ad1bSJan Schmidt return ret; 1624a542ad1bSJan Schmidt } 1625a542ad1bSJan Schmidt 1626d24bec3aSMark Fasheh static int iterate_inode_extrefs(u64 inum, struct btrfs_root *fs_root, 1627d24bec3aSMark Fasheh struct btrfs_path *path, 1628d24bec3aSMark Fasheh iterate_irefs_t *iterate, void *ctx) 1629d24bec3aSMark Fasheh { 1630d24bec3aSMark Fasheh int ret; 1631d24bec3aSMark Fasheh int slot; 1632d24bec3aSMark Fasheh u64 offset = 0; 1633d24bec3aSMark Fasheh u64 parent; 1634d24bec3aSMark Fasheh int found = 0; 1635d24bec3aSMark Fasheh struct extent_buffer *eb; 1636d24bec3aSMark Fasheh struct btrfs_inode_extref *extref; 1637d24bec3aSMark Fasheh struct extent_buffer *leaf; 1638d24bec3aSMark Fasheh u32 item_size; 1639d24bec3aSMark Fasheh u32 cur_offset; 1640d24bec3aSMark Fasheh unsigned long ptr; 1641d24bec3aSMark Fasheh 1642d24bec3aSMark Fasheh while (1) { 1643d24bec3aSMark Fasheh ret = btrfs_find_one_extref(fs_root, inum, offset, path, &extref, 1644d24bec3aSMark Fasheh &offset); 1645d24bec3aSMark Fasheh if (ret < 0) 1646d24bec3aSMark Fasheh break; 1647d24bec3aSMark Fasheh if (ret) { 1648d24bec3aSMark Fasheh ret = found ? 0 : -ENOENT; 1649d24bec3aSMark Fasheh break; 1650d24bec3aSMark Fasheh } 1651d24bec3aSMark Fasheh ++found; 1652d24bec3aSMark Fasheh 1653d24bec3aSMark Fasheh slot = path->slots[0]; 1654d24bec3aSMark Fasheh eb = path->nodes[0]; 1655d24bec3aSMark Fasheh /* make sure we can use eb after releasing the path */ 1656d24bec3aSMark Fasheh atomic_inc(&eb->refs); 1657d24bec3aSMark Fasheh 1658d24bec3aSMark Fasheh btrfs_tree_read_lock(eb); 1659d24bec3aSMark Fasheh btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK); 1660d24bec3aSMark Fasheh btrfs_release_path(path); 1661d24bec3aSMark Fasheh 1662d24bec3aSMark Fasheh leaf = path->nodes[0]; 1663d24bec3aSMark Fasheh item_size = btrfs_item_size_nr(leaf, path->slots[0]); 1664d24bec3aSMark Fasheh ptr = btrfs_item_ptr_offset(leaf, path->slots[0]); 1665d24bec3aSMark Fasheh cur_offset = 0; 1666d24bec3aSMark Fasheh 1667d24bec3aSMark Fasheh while (cur_offset < item_size) { 1668d24bec3aSMark Fasheh u32 name_len; 1669d24bec3aSMark Fasheh 1670d24bec3aSMark Fasheh extref = (struct btrfs_inode_extref *)(ptr + cur_offset); 1671d24bec3aSMark Fasheh parent = btrfs_inode_extref_parent(eb, extref); 1672d24bec3aSMark Fasheh name_len = btrfs_inode_extref_name_len(eb, extref); 1673d24bec3aSMark Fasheh ret = iterate(parent, name_len, 1674d24bec3aSMark Fasheh (unsigned long)&extref->name, eb, ctx); 1675d24bec3aSMark Fasheh if (ret) 1676d24bec3aSMark Fasheh break; 1677d24bec3aSMark Fasheh 1678d24bec3aSMark Fasheh cur_offset += btrfs_inode_extref_name_len(leaf, extref); 1679d24bec3aSMark Fasheh cur_offset += sizeof(*extref); 1680d24bec3aSMark Fasheh } 1681d24bec3aSMark Fasheh btrfs_tree_read_unlock_blocking(eb); 1682d24bec3aSMark Fasheh free_extent_buffer(eb); 1683d24bec3aSMark Fasheh 1684d24bec3aSMark Fasheh offset++; 1685d24bec3aSMark Fasheh } 1686d24bec3aSMark Fasheh 1687d24bec3aSMark Fasheh btrfs_release_path(path); 1688d24bec3aSMark Fasheh 1689d24bec3aSMark Fasheh return ret; 1690d24bec3aSMark Fasheh } 1691d24bec3aSMark Fasheh 1692d24bec3aSMark Fasheh static int iterate_irefs(u64 inum, struct btrfs_root *fs_root, 1693d24bec3aSMark Fasheh struct btrfs_path *path, iterate_irefs_t *iterate, 1694d24bec3aSMark Fasheh void *ctx) 1695d24bec3aSMark Fasheh { 1696d24bec3aSMark Fasheh int ret; 1697d24bec3aSMark Fasheh int found_refs = 0; 1698d24bec3aSMark Fasheh 1699d24bec3aSMark Fasheh ret = iterate_inode_refs(inum, fs_root, path, iterate, ctx); 1700d24bec3aSMark Fasheh if (!ret) 1701d24bec3aSMark Fasheh ++found_refs; 1702d24bec3aSMark Fasheh else if (ret != -ENOENT) 1703d24bec3aSMark Fasheh return ret; 1704d24bec3aSMark Fasheh 1705d24bec3aSMark Fasheh ret = iterate_inode_extrefs(inum, fs_root, path, iterate, ctx); 1706d24bec3aSMark Fasheh if (ret == -ENOENT && found_refs) 1707d24bec3aSMark Fasheh return 0; 1708d24bec3aSMark Fasheh 1709d24bec3aSMark Fasheh return ret; 1710d24bec3aSMark Fasheh } 1711d24bec3aSMark Fasheh 1712a542ad1bSJan Schmidt /* 1713a542ad1bSJan Schmidt * returns 0 if the path could be dumped (probably truncated) 1714a542ad1bSJan Schmidt * returns <0 in case of an error 1715a542ad1bSJan Schmidt */ 1716d24bec3aSMark Fasheh static int inode_to_path(u64 inum, u32 name_len, unsigned long name_off, 1717a542ad1bSJan Schmidt struct extent_buffer *eb, void *ctx) 1718a542ad1bSJan Schmidt { 1719a542ad1bSJan Schmidt struct inode_fs_paths *ipath = ctx; 1720a542ad1bSJan Schmidt char *fspath; 1721a542ad1bSJan Schmidt char *fspath_min; 1722a542ad1bSJan Schmidt int i = ipath->fspath->elem_cnt; 1723a542ad1bSJan Schmidt const int s_ptr = sizeof(char *); 1724a542ad1bSJan Schmidt u32 bytes_left; 1725a542ad1bSJan Schmidt 1726a542ad1bSJan Schmidt bytes_left = ipath->fspath->bytes_left > s_ptr ? 1727a542ad1bSJan Schmidt ipath->fspath->bytes_left - s_ptr : 0; 1728a542ad1bSJan Schmidt 1729740c3d22SChris Mason fspath_min = (char *)ipath->fspath->val + (i + 1) * s_ptr; 173096b5bd77SJan Schmidt fspath = btrfs_ref_to_path(ipath->fs_root, ipath->btrfs_path, name_len, 173196b5bd77SJan Schmidt name_off, eb, inum, fspath_min, bytes_left); 1732a542ad1bSJan Schmidt if (IS_ERR(fspath)) 1733a542ad1bSJan Schmidt return PTR_ERR(fspath); 1734a542ad1bSJan Schmidt 1735a542ad1bSJan Schmidt if (fspath > fspath_min) { 1736745c4d8eSJeff Mahoney ipath->fspath->val[i] = (u64)(unsigned long)fspath; 1737a542ad1bSJan Schmidt ++ipath->fspath->elem_cnt; 1738a542ad1bSJan Schmidt ipath->fspath->bytes_left = fspath - fspath_min; 1739a542ad1bSJan Schmidt } else { 1740a542ad1bSJan Schmidt ++ipath->fspath->elem_missed; 1741a542ad1bSJan Schmidt ipath->fspath->bytes_missing += fspath_min - fspath; 1742a542ad1bSJan Schmidt ipath->fspath->bytes_left = 0; 1743a542ad1bSJan Schmidt } 1744a542ad1bSJan Schmidt 1745a542ad1bSJan Schmidt return 0; 1746a542ad1bSJan Schmidt } 1747a542ad1bSJan Schmidt 1748a542ad1bSJan Schmidt /* 1749a542ad1bSJan Schmidt * this dumps all file system paths to the inode into the ipath struct, provided 1750a542ad1bSJan Schmidt * is has been created large enough. each path is zero-terminated and accessed 1751740c3d22SChris Mason * from ipath->fspath->val[i]. 1752a542ad1bSJan Schmidt * when it returns, there are ipath->fspath->elem_cnt number of paths available 1753740c3d22SChris Mason * in ipath->fspath->val[]. when the allocated space wasn't sufficient, the 1754a542ad1bSJan Schmidt * number of missed paths in recored in ipath->fspath->elem_missed, otherwise, 1755a542ad1bSJan Schmidt * it's zero. ipath->fspath->bytes_missing holds the number of bytes that would 1756a542ad1bSJan Schmidt * have been needed to return all paths. 1757a542ad1bSJan Schmidt */ 1758a542ad1bSJan Schmidt int paths_from_inode(u64 inum, struct inode_fs_paths *ipath) 1759a542ad1bSJan Schmidt { 1760a542ad1bSJan Schmidt return iterate_irefs(inum, ipath->fs_root, ipath->btrfs_path, 1761a542ad1bSJan Schmidt inode_to_path, ipath); 1762a542ad1bSJan Schmidt } 1763a542ad1bSJan Schmidt 1764a542ad1bSJan Schmidt struct btrfs_data_container *init_data_container(u32 total_bytes) 1765a542ad1bSJan Schmidt { 1766a542ad1bSJan Schmidt struct btrfs_data_container *data; 1767a542ad1bSJan Schmidt size_t alloc_bytes; 1768a542ad1bSJan Schmidt 1769a542ad1bSJan Schmidt alloc_bytes = max_t(size_t, total_bytes, sizeof(*data)); 1770425d17a2SLiu Bo data = vmalloc(alloc_bytes); 1771a542ad1bSJan Schmidt if (!data) 1772a542ad1bSJan Schmidt return ERR_PTR(-ENOMEM); 1773a542ad1bSJan Schmidt 1774a542ad1bSJan Schmidt if (total_bytes >= sizeof(*data)) { 1775a542ad1bSJan Schmidt data->bytes_left = total_bytes - sizeof(*data); 1776a542ad1bSJan Schmidt data->bytes_missing = 0; 1777a542ad1bSJan Schmidt } else { 1778a542ad1bSJan Schmidt data->bytes_missing = sizeof(*data) - total_bytes; 1779a542ad1bSJan Schmidt data->bytes_left = 0; 1780a542ad1bSJan Schmidt } 1781a542ad1bSJan Schmidt 1782a542ad1bSJan Schmidt data->elem_cnt = 0; 1783a542ad1bSJan Schmidt data->elem_missed = 0; 1784a542ad1bSJan Schmidt 1785a542ad1bSJan Schmidt return data; 1786a542ad1bSJan Schmidt } 1787a542ad1bSJan Schmidt 1788a542ad1bSJan Schmidt /* 1789a542ad1bSJan Schmidt * allocates space to return multiple file system paths for an inode. 1790a542ad1bSJan Schmidt * total_bytes to allocate are passed, note that space usable for actual path 1791a542ad1bSJan Schmidt * information will be total_bytes - sizeof(struct inode_fs_paths). 1792a542ad1bSJan Schmidt * the returned pointer must be freed with free_ipath() in the end. 1793a542ad1bSJan Schmidt */ 1794a542ad1bSJan Schmidt struct inode_fs_paths *init_ipath(s32 total_bytes, struct btrfs_root *fs_root, 1795a542ad1bSJan Schmidt struct btrfs_path *path) 1796a542ad1bSJan Schmidt { 1797a542ad1bSJan Schmidt struct inode_fs_paths *ifp; 1798a542ad1bSJan Schmidt struct btrfs_data_container *fspath; 1799a542ad1bSJan Schmidt 1800a542ad1bSJan Schmidt fspath = init_data_container(total_bytes); 1801a542ad1bSJan Schmidt if (IS_ERR(fspath)) 1802a542ad1bSJan Schmidt return (void *)fspath; 1803a542ad1bSJan Schmidt 1804a542ad1bSJan Schmidt ifp = kmalloc(sizeof(*ifp), GFP_NOFS); 1805a542ad1bSJan Schmidt if (!ifp) { 1806a542ad1bSJan Schmidt kfree(fspath); 1807a542ad1bSJan Schmidt return ERR_PTR(-ENOMEM); 1808a542ad1bSJan Schmidt } 1809a542ad1bSJan Schmidt 1810a542ad1bSJan Schmidt ifp->btrfs_path = path; 1811a542ad1bSJan Schmidt ifp->fspath = fspath; 1812a542ad1bSJan Schmidt ifp->fs_root = fs_root; 1813a542ad1bSJan Schmidt 1814a542ad1bSJan Schmidt return ifp; 1815a542ad1bSJan Schmidt } 1816a542ad1bSJan Schmidt 1817a542ad1bSJan Schmidt void free_ipath(struct inode_fs_paths *ipath) 1818a542ad1bSJan Schmidt { 18194735fb28SJesper Juhl if (!ipath) 18204735fb28SJesper Juhl return; 1821425d17a2SLiu Bo vfree(ipath->fspath); 1822a542ad1bSJan Schmidt kfree(ipath); 1823a542ad1bSJan Schmidt } 1824