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 19a542ad1bSJan Schmidt #include "ctree.h" 20a542ad1bSJan Schmidt #include "disk-io.h" 21a542ad1bSJan Schmidt #include "backref.h" 228da6d581SJan Schmidt #include "ulist.h" 238da6d581SJan Schmidt #include "transaction.h" 248da6d581SJan Schmidt #include "delayed-ref.h" 25b916a59aSJan Schmidt #include "locking.h" 26a542ad1bSJan Schmidt 27*976b1908SJan Schmidt struct extent_inode_elem { 28*976b1908SJan Schmidt u64 inum; 29*976b1908SJan Schmidt u64 offset; 30*976b1908SJan Schmidt struct extent_inode_elem *next; 31*976b1908SJan Schmidt }; 32*976b1908SJan Schmidt 33*976b1908SJan Schmidt static int check_extent_in_eb(struct btrfs_key *key, struct extent_buffer *eb, 34*976b1908SJan Schmidt struct btrfs_file_extent_item *fi, 35*976b1908SJan Schmidt u64 extent_item_pos, 36*976b1908SJan Schmidt struct extent_inode_elem **eie) 37*976b1908SJan Schmidt { 38*976b1908SJan Schmidt u64 data_offset; 39*976b1908SJan Schmidt u64 data_len; 40*976b1908SJan Schmidt struct extent_inode_elem *e; 41*976b1908SJan Schmidt 42*976b1908SJan Schmidt data_offset = btrfs_file_extent_offset(eb, fi); 43*976b1908SJan Schmidt data_len = btrfs_file_extent_num_bytes(eb, fi); 44*976b1908SJan Schmidt 45*976b1908SJan Schmidt if (extent_item_pos < data_offset || 46*976b1908SJan Schmidt extent_item_pos >= data_offset + data_len) 47*976b1908SJan Schmidt return 1; 48*976b1908SJan Schmidt 49*976b1908SJan Schmidt e = kmalloc(sizeof(*e), GFP_NOFS); 50*976b1908SJan Schmidt if (!e) 51*976b1908SJan Schmidt return -ENOMEM; 52*976b1908SJan Schmidt 53*976b1908SJan Schmidt e->next = *eie; 54*976b1908SJan Schmidt e->inum = key->objectid; 55*976b1908SJan Schmidt e->offset = key->offset + (extent_item_pos - data_offset); 56*976b1908SJan Schmidt *eie = e; 57*976b1908SJan Schmidt 58*976b1908SJan Schmidt return 0; 59*976b1908SJan Schmidt } 60*976b1908SJan Schmidt 61*976b1908SJan Schmidt static int find_extent_in_eb(struct extent_buffer *eb, u64 wanted_disk_byte, 62*976b1908SJan Schmidt u64 extent_item_pos, 63*976b1908SJan Schmidt struct extent_inode_elem **eie) 64*976b1908SJan Schmidt { 65*976b1908SJan Schmidt u64 disk_byte; 66*976b1908SJan Schmidt struct btrfs_key key; 67*976b1908SJan Schmidt struct btrfs_file_extent_item *fi; 68*976b1908SJan Schmidt int slot; 69*976b1908SJan Schmidt int nritems; 70*976b1908SJan Schmidt int extent_type; 71*976b1908SJan Schmidt int ret; 72*976b1908SJan Schmidt 73*976b1908SJan Schmidt /* 74*976b1908SJan Schmidt * from the shared data ref, we only have the leaf but we need 75*976b1908SJan Schmidt * the key. thus, we must look into all items and see that we 76*976b1908SJan Schmidt * find one (some) with a reference to our extent item. 77*976b1908SJan Schmidt */ 78*976b1908SJan Schmidt nritems = btrfs_header_nritems(eb); 79*976b1908SJan Schmidt for (slot = 0; slot < nritems; ++slot) { 80*976b1908SJan Schmidt btrfs_item_key_to_cpu(eb, &key, slot); 81*976b1908SJan Schmidt if (key.type != BTRFS_EXTENT_DATA_KEY) 82*976b1908SJan Schmidt continue; 83*976b1908SJan Schmidt fi = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item); 84*976b1908SJan Schmidt extent_type = btrfs_file_extent_type(eb, fi); 85*976b1908SJan Schmidt if (extent_type == BTRFS_FILE_EXTENT_INLINE) 86*976b1908SJan Schmidt continue; 87*976b1908SJan Schmidt /* don't skip BTRFS_FILE_EXTENT_PREALLOC, we can handle that */ 88*976b1908SJan Schmidt disk_byte = btrfs_file_extent_disk_bytenr(eb, fi); 89*976b1908SJan Schmidt if (disk_byte != wanted_disk_byte) 90*976b1908SJan Schmidt continue; 91*976b1908SJan Schmidt 92*976b1908SJan Schmidt ret = check_extent_in_eb(&key, eb, fi, extent_item_pos, eie); 93*976b1908SJan Schmidt if (ret < 0) 94*976b1908SJan Schmidt return ret; 95*976b1908SJan Schmidt } 96*976b1908SJan Schmidt 97*976b1908SJan Schmidt return 0; 98*976b1908SJan Schmidt } 99*976b1908SJan Schmidt 1008da6d581SJan Schmidt /* 1018da6d581SJan Schmidt * this structure records all encountered refs on the way up to the root 1028da6d581SJan Schmidt */ 1038da6d581SJan Schmidt struct __prelim_ref { 1048da6d581SJan Schmidt struct list_head list; 1058da6d581SJan Schmidt u64 root_id; 106d5c88b73SJan Schmidt struct btrfs_key key_for_search; 1078da6d581SJan Schmidt int level; 1088da6d581SJan Schmidt int count; 1098da6d581SJan Schmidt u64 parent; 1108da6d581SJan Schmidt u64 wanted_disk_byte; 1118da6d581SJan Schmidt }; 1128da6d581SJan Schmidt 113d5c88b73SJan Schmidt /* 114d5c88b73SJan Schmidt * the rules for all callers of this function are: 115d5c88b73SJan Schmidt * - obtaining the parent is the goal 116d5c88b73SJan Schmidt * - if you add a key, you must know that it is a correct key 117d5c88b73SJan Schmidt * - if you cannot add the parent or a correct key, then we will look into the 118d5c88b73SJan Schmidt * block later to set a correct key 119d5c88b73SJan Schmidt * 120d5c88b73SJan Schmidt * delayed refs 121d5c88b73SJan Schmidt * ============ 122d5c88b73SJan Schmidt * backref type | shared | indirect | shared | indirect 123d5c88b73SJan Schmidt * information | tree | tree | data | data 124d5c88b73SJan Schmidt * --------------------+--------+----------+--------+---------- 125d5c88b73SJan Schmidt * parent logical | y | - | - | - 126d5c88b73SJan Schmidt * key to resolve | - | y | y | y 127d5c88b73SJan Schmidt * tree block logical | - | - | - | - 128d5c88b73SJan Schmidt * root for resolving | y | y | y | y 129d5c88b73SJan Schmidt * 130d5c88b73SJan Schmidt * - column 1: we've the parent -> done 131d5c88b73SJan Schmidt * - column 2, 3, 4: we use the key to find the parent 132d5c88b73SJan Schmidt * 133d5c88b73SJan Schmidt * on disk refs (inline or keyed) 134d5c88b73SJan Schmidt * ============================== 135d5c88b73SJan Schmidt * backref type | shared | indirect | shared | indirect 136d5c88b73SJan Schmidt * information | tree | tree | data | data 137d5c88b73SJan Schmidt * --------------------+--------+----------+--------+---------- 138d5c88b73SJan Schmidt * parent logical | y | - | y | - 139d5c88b73SJan Schmidt * key to resolve | - | - | - | y 140d5c88b73SJan Schmidt * tree block logical | y | y | y | y 141d5c88b73SJan Schmidt * root for resolving | - | y | y | y 142d5c88b73SJan Schmidt * 143d5c88b73SJan Schmidt * - column 1, 3: we've the parent -> done 144d5c88b73SJan Schmidt * - column 2: we take the first key from the block to find the parent 145d5c88b73SJan Schmidt * (see __add_missing_keys) 146d5c88b73SJan Schmidt * - column 4: we use the key to find the parent 147d5c88b73SJan Schmidt * 148d5c88b73SJan Schmidt * additional information that's available but not required to find the parent 149d5c88b73SJan Schmidt * block might help in merging entries to gain some speed. 150d5c88b73SJan Schmidt */ 151d5c88b73SJan Schmidt 1528da6d581SJan Schmidt static int __add_prelim_ref(struct list_head *head, u64 root_id, 153d5c88b73SJan Schmidt struct btrfs_key *key, int level, 154d5c88b73SJan Schmidt u64 parent, u64 wanted_disk_byte, int count) 1558da6d581SJan Schmidt { 1568da6d581SJan Schmidt struct __prelim_ref *ref; 1578da6d581SJan Schmidt 1588da6d581SJan Schmidt /* in case we're adding delayed refs, we're holding the refs spinlock */ 1598da6d581SJan Schmidt ref = kmalloc(sizeof(*ref), GFP_ATOMIC); 1608da6d581SJan Schmidt if (!ref) 1618da6d581SJan Schmidt return -ENOMEM; 1628da6d581SJan Schmidt 1638da6d581SJan Schmidt ref->root_id = root_id; 1648da6d581SJan Schmidt if (key) 165d5c88b73SJan Schmidt ref->key_for_search = *key; 1668da6d581SJan Schmidt else 167d5c88b73SJan Schmidt memset(&ref->key_for_search, 0, sizeof(ref->key_for_search)); 1688da6d581SJan Schmidt 1698da6d581SJan Schmidt ref->level = level; 1708da6d581SJan Schmidt ref->count = count; 1718da6d581SJan Schmidt ref->parent = parent; 1728da6d581SJan Schmidt ref->wanted_disk_byte = wanted_disk_byte; 1738da6d581SJan Schmidt list_add_tail(&ref->list, head); 1748da6d581SJan Schmidt 1758da6d581SJan Schmidt return 0; 1768da6d581SJan Schmidt } 1778da6d581SJan Schmidt 1788da6d581SJan Schmidt static int add_all_parents(struct btrfs_root *root, struct btrfs_path *path, 179*976b1908SJan Schmidt struct ulist *parents, int level, 180*976b1908SJan Schmidt struct btrfs_key *key, u64 wanted_disk_byte, 181*976b1908SJan Schmidt const u64 *extent_item_pos) 1828da6d581SJan Schmidt { 1838da6d581SJan Schmidt int ret; 1848da6d581SJan Schmidt int slot; 185*976b1908SJan Schmidt struct extent_buffer *eb = path->nodes[level]; 1868da6d581SJan Schmidt struct btrfs_file_extent_item *fi; 1878da6d581SJan Schmidt u64 disk_byte; 188*976b1908SJan Schmidt u64 wanted_objectid = key->objectid; 1898da6d581SJan Schmidt 1908da6d581SJan Schmidt add_parent: 1918da6d581SJan Schmidt ret = ulist_add(parents, eb->start, 0, GFP_NOFS); 1928da6d581SJan Schmidt if (ret < 0) 1938da6d581SJan Schmidt return ret; 1948da6d581SJan Schmidt 1958da6d581SJan Schmidt if (level != 0) 1968da6d581SJan Schmidt return 0; 1978da6d581SJan Schmidt 1988da6d581SJan Schmidt /* 1998da6d581SJan Schmidt * if the current leaf is full with EXTENT_DATA items, we must 2008da6d581SJan Schmidt * check the next one if that holds a reference as well. 2018da6d581SJan Schmidt * ref->count cannot be used to skip this check. 2028da6d581SJan Schmidt * repeat this until we don't find any additional EXTENT_DATA items. 2038da6d581SJan Schmidt */ 2048da6d581SJan Schmidt while (1) { 2058da6d581SJan Schmidt ret = btrfs_next_leaf(root, path); 2068da6d581SJan Schmidt if (ret < 0) 2078da6d581SJan Schmidt return ret; 2088da6d581SJan Schmidt if (ret) 2098da6d581SJan Schmidt return 0; 2108da6d581SJan Schmidt 2118da6d581SJan Schmidt eb = path->nodes[0]; 2128da6d581SJan Schmidt for (slot = 0; slot < btrfs_header_nritems(eb); ++slot) { 213*976b1908SJan Schmidt btrfs_item_key_to_cpu(eb, key, slot); 214*976b1908SJan Schmidt if (key->objectid != wanted_objectid || 215*976b1908SJan Schmidt key->type != BTRFS_EXTENT_DATA_KEY) 2168da6d581SJan Schmidt return 0; 2178da6d581SJan Schmidt fi = btrfs_item_ptr(eb, slot, 2188da6d581SJan Schmidt struct btrfs_file_extent_item); 2198da6d581SJan Schmidt disk_byte = btrfs_file_extent_disk_bytenr(eb, fi); 2208da6d581SJan Schmidt if (disk_byte == wanted_disk_byte) 2218da6d581SJan Schmidt goto add_parent; 2228da6d581SJan Schmidt } 2238da6d581SJan Schmidt } 2248da6d581SJan Schmidt 2258da6d581SJan Schmidt return 0; 2268da6d581SJan Schmidt } 2278da6d581SJan Schmidt 2288da6d581SJan Schmidt /* 2298da6d581SJan Schmidt * resolve an indirect backref in the form (root_id, key, level) 2308da6d581SJan Schmidt * to a logical address 2318da6d581SJan Schmidt */ 2328da6d581SJan Schmidt static int __resolve_indirect_ref(struct btrfs_fs_info *fs_info, 2337a3ae2f8SJan Schmidt int search_commit_root, 2348da6d581SJan Schmidt struct __prelim_ref *ref, 235*976b1908SJan Schmidt struct ulist *parents, 236*976b1908SJan Schmidt const u64 *extent_item_pos) 2378da6d581SJan Schmidt { 2388da6d581SJan Schmidt struct btrfs_path *path; 2398da6d581SJan Schmidt struct btrfs_root *root; 2408da6d581SJan Schmidt struct btrfs_key root_key; 2418da6d581SJan Schmidt struct btrfs_key key = {0}; 2428da6d581SJan Schmidt struct extent_buffer *eb; 2438da6d581SJan Schmidt int ret = 0; 2448da6d581SJan Schmidt int root_level; 2458da6d581SJan Schmidt int level = ref->level; 2468da6d581SJan Schmidt 2478da6d581SJan Schmidt path = btrfs_alloc_path(); 2488da6d581SJan Schmidt if (!path) 2498da6d581SJan Schmidt return -ENOMEM; 2507a3ae2f8SJan Schmidt path->search_commit_root = !!search_commit_root; 2518da6d581SJan Schmidt 2528da6d581SJan Schmidt root_key.objectid = ref->root_id; 2538da6d581SJan Schmidt root_key.type = BTRFS_ROOT_ITEM_KEY; 2548da6d581SJan Schmidt root_key.offset = (u64)-1; 2558da6d581SJan Schmidt root = btrfs_read_fs_root_no_name(fs_info, &root_key); 2568da6d581SJan Schmidt if (IS_ERR(root)) { 2578da6d581SJan Schmidt ret = PTR_ERR(root); 2588da6d581SJan Schmidt goto out; 2598da6d581SJan Schmidt } 2608da6d581SJan Schmidt 2618da6d581SJan Schmidt rcu_read_lock(); 2628da6d581SJan Schmidt root_level = btrfs_header_level(root->node); 2638da6d581SJan Schmidt rcu_read_unlock(); 2648da6d581SJan Schmidt 2658da6d581SJan Schmidt if (root_level + 1 == level) 2668da6d581SJan Schmidt goto out; 2678da6d581SJan Schmidt 2688da6d581SJan Schmidt path->lowest_level = level; 269d5c88b73SJan Schmidt ret = btrfs_search_slot(NULL, root, &ref->key_for_search, path, 0, 0); 2708da6d581SJan Schmidt pr_debug("search slot in root %llu (level %d, ref count %d) returned " 2718da6d581SJan Schmidt "%d for key (%llu %u %llu)\n", 2728da6d581SJan Schmidt (unsigned long long)ref->root_id, level, ref->count, ret, 273d5c88b73SJan Schmidt (unsigned long long)ref->key_for_search.objectid, 274d5c88b73SJan Schmidt ref->key_for_search.type, 275d5c88b73SJan Schmidt (unsigned long long)ref->key_for_search.offset); 2768da6d581SJan Schmidt if (ret < 0) 2778da6d581SJan Schmidt goto out; 2788da6d581SJan Schmidt 2798da6d581SJan Schmidt eb = path->nodes[level]; 2808da6d581SJan Schmidt if (!eb) { 2818da6d581SJan Schmidt WARN_ON(1); 2828da6d581SJan Schmidt ret = 1; 2838da6d581SJan Schmidt goto out; 2848da6d581SJan Schmidt } 2858da6d581SJan Schmidt 2868da6d581SJan Schmidt if (level == 0) { 2878da6d581SJan Schmidt if (ret == 1 && path->slots[0] >= btrfs_header_nritems(eb)) { 2888da6d581SJan Schmidt ret = btrfs_next_leaf(root, path); 2898da6d581SJan Schmidt if (ret) 2908da6d581SJan Schmidt goto out; 2918da6d581SJan Schmidt eb = path->nodes[0]; 2928da6d581SJan Schmidt } 2938da6d581SJan Schmidt 2948da6d581SJan Schmidt btrfs_item_key_to_cpu(eb, &key, path->slots[0]); 2958da6d581SJan Schmidt } 2968da6d581SJan Schmidt 297*976b1908SJan Schmidt ret = add_all_parents(root, path, parents, level, &key, 298*976b1908SJan Schmidt ref->wanted_disk_byte, extent_item_pos); 2998da6d581SJan Schmidt out: 3008da6d581SJan Schmidt btrfs_free_path(path); 3018da6d581SJan Schmidt return ret; 3028da6d581SJan Schmidt } 3038da6d581SJan Schmidt 3048da6d581SJan Schmidt /* 3058da6d581SJan Schmidt * resolve all indirect backrefs from the list 3068da6d581SJan Schmidt */ 3078da6d581SJan Schmidt static int __resolve_indirect_refs(struct btrfs_fs_info *fs_info, 3087a3ae2f8SJan Schmidt int search_commit_root, 309*976b1908SJan Schmidt struct list_head *head, 310*976b1908SJan Schmidt const u64 *extent_item_pos) 3118da6d581SJan Schmidt { 3128da6d581SJan Schmidt int err; 3138da6d581SJan Schmidt int ret = 0; 3148da6d581SJan Schmidt struct __prelim_ref *ref; 3158da6d581SJan Schmidt struct __prelim_ref *ref_safe; 3168da6d581SJan Schmidt struct __prelim_ref *new_ref; 3178da6d581SJan Schmidt struct ulist *parents; 3188da6d581SJan Schmidt struct ulist_node *node; 319cd1b413cSJan Schmidt struct ulist_iterator uiter; 3208da6d581SJan Schmidt 3218da6d581SJan Schmidt parents = ulist_alloc(GFP_NOFS); 3228da6d581SJan Schmidt if (!parents) 3238da6d581SJan Schmidt return -ENOMEM; 3248da6d581SJan Schmidt 3258da6d581SJan Schmidt /* 3268da6d581SJan Schmidt * _safe allows us to insert directly after the current item without 3278da6d581SJan Schmidt * iterating over the newly inserted items. 3288da6d581SJan Schmidt * we're also allowed to re-assign ref during iteration. 3298da6d581SJan Schmidt */ 3308da6d581SJan Schmidt list_for_each_entry_safe(ref, ref_safe, head, list) { 3318da6d581SJan Schmidt if (ref->parent) /* already direct */ 3328da6d581SJan Schmidt continue; 3338da6d581SJan Schmidt if (ref->count == 0) 3348da6d581SJan Schmidt continue; 3357a3ae2f8SJan Schmidt err = __resolve_indirect_ref(fs_info, search_commit_root, 336*976b1908SJan Schmidt ref, parents, extent_item_pos); 3378da6d581SJan Schmidt if (err) { 3388da6d581SJan Schmidt if (ret == 0) 3398da6d581SJan Schmidt ret = err; 3408da6d581SJan Schmidt continue; 3418da6d581SJan Schmidt } 3428da6d581SJan Schmidt 3438da6d581SJan Schmidt /* we put the first parent into the ref at hand */ 344cd1b413cSJan Schmidt ULIST_ITER_INIT(&uiter); 345cd1b413cSJan Schmidt node = ulist_next(parents, &uiter); 3468da6d581SJan Schmidt ref->parent = node ? node->val : 0; 3478da6d581SJan Schmidt 3488da6d581SJan Schmidt /* additional parents require new refs being added here */ 349cd1b413cSJan Schmidt while ((node = ulist_next(parents, &uiter))) { 3508da6d581SJan Schmidt new_ref = kmalloc(sizeof(*new_ref), GFP_NOFS); 3518da6d581SJan Schmidt if (!new_ref) { 3528da6d581SJan Schmidt ret = -ENOMEM; 3538da6d581SJan Schmidt break; 3548da6d581SJan Schmidt } 3558da6d581SJan Schmidt memcpy(new_ref, ref, sizeof(*ref)); 3568da6d581SJan Schmidt new_ref->parent = node->val; 3578da6d581SJan Schmidt list_add(&new_ref->list, &ref->list); 3588da6d581SJan Schmidt } 3598da6d581SJan Schmidt ulist_reinit(parents); 3608da6d581SJan Schmidt } 3618da6d581SJan Schmidt 3628da6d581SJan Schmidt ulist_free(parents); 3638da6d581SJan Schmidt return ret; 3648da6d581SJan Schmidt } 3658da6d581SJan Schmidt 366d5c88b73SJan Schmidt static inline int ref_for_same_block(struct __prelim_ref *ref1, 367d5c88b73SJan Schmidt struct __prelim_ref *ref2) 368d5c88b73SJan Schmidt { 369d5c88b73SJan Schmidt if (ref1->level != ref2->level) 370d5c88b73SJan Schmidt return 0; 371d5c88b73SJan Schmidt if (ref1->root_id != ref2->root_id) 372d5c88b73SJan Schmidt return 0; 373d5c88b73SJan Schmidt if (ref1->key_for_search.type != ref2->key_for_search.type) 374d5c88b73SJan Schmidt return 0; 375d5c88b73SJan Schmidt if (ref1->key_for_search.objectid != ref2->key_for_search.objectid) 376d5c88b73SJan Schmidt return 0; 377d5c88b73SJan Schmidt if (ref1->key_for_search.offset != ref2->key_for_search.offset) 378d5c88b73SJan Schmidt return 0; 379d5c88b73SJan Schmidt if (ref1->parent != ref2->parent) 380d5c88b73SJan Schmidt return 0; 381d5c88b73SJan Schmidt 382d5c88b73SJan Schmidt return 1; 383d5c88b73SJan Schmidt } 384d5c88b73SJan Schmidt 385d5c88b73SJan Schmidt /* 386d5c88b73SJan Schmidt * read tree blocks and add keys where required. 387d5c88b73SJan Schmidt */ 388d5c88b73SJan Schmidt static int __add_missing_keys(struct btrfs_fs_info *fs_info, 389d5c88b73SJan Schmidt struct list_head *head) 390d5c88b73SJan Schmidt { 391d5c88b73SJan Schmidt struct list_head *pos; 392d5c88b73SJan Schmidt struct extent_buffer *eb; 393d5c88b73SJan Schmidt 394d5c88b73SJan Schmidt list_for_each(pos, head) { 395d5c88b73SJan Schmidt struct __prelim_ref *ref; 396d5c88b73SJan Schmidt ref = list_entry(pos, struct __prelim_ref, list); 397d5c88b73SJan Schmidt 398d5c88b73SJan Schmidt if (ref->parent) 399d5c88b73SJan Schmidt continue; 400d5c88b73SJan Schmidt if (ref->key_for_search.type) 401d5c88b73SJan Schmidt continue; 402d5c88b73SJan Schmidt BUG_ON(!ref->wanted_disk_byte); 403d5c88b73SJan Schmidt eb = read_tree_block(fs_info->tree_root, ref->wanted_disk_byte, 404d5c88b73SJan Schmidt fs_info->tree_root->leafsize, 0); 405d5c88b73SJan Schmidt BUG_ON(!eb); 406d5c88b73SJan Schmidt btrfs_tree_read_lock(eb); 407d5c88b73SJan Schmidt if (btrfs_header_level(eb) == 0) 408d5c88b73SJan Schmidt btrfs_item_key_to_cpu(eb, &ref->key_for_search, 0); 409d5c88b73SJan Schmidt else 410d5c88b73SJan Schmidt btrfs_node_key_to_cpu(eb, &ref->key_for_search, 0); 411d5c88b73SJan Schmidt btrfs_tree_read_unlock(eb); 412d5c88b73SJan Schmidt free_extent_buffer(eb); 413d5c88b73SJan Schmidt } 414d5c88b73SJan Schmidt return 0; 415d5c88b73SJan Schmidt } 416d5c88b73SJan Schmidt 4178da6d581SJan Schmidt /* 4188da6d581SJan Schmidt * merge two lists of backrefs and adjust counts accordingly 4198da6d581SJan Schmidt * 4208da6d581SJan Schmidt * mode = 1: merge identical keys, if key is set 421d5c88b73SJan Schmidt * FIXME: if we add more keys in __add_prelim_ref, we can merge more here. 422d5c88b73SJan Schmidt * additionally, we could even add a key range for the blocks we 423d5c88b73SJan Schmidt * looked into to merge even more (-> replace unresolved refs by those 424d5c88b73SJan Schmidt * having a parent). 4258da6d581SJan Schmidt * mode = 2: merge identical parents 4268da6d581SJan Schmidt */ 4278da6d581SJan Schmidt static int __merge_refs(struct list_head *head, int mode) 4288da6d581SJan Schmidt { 4298da6d581SJan Schmidt struct list_head *pos1; 4308da6d581SJan Schmidt 4318da6d581SJan Schmidt list_for_each(pos1, head) { 4328da6d581SJan Schmidt struct list_head *n2; 4338da6d581SJan Schmidt struct list_head *pos2; 4348da6d581SJan Schmidt struct __prelim_ref *ref1; 4358da6d581SJan Schmidt 4368da6d581SJan Schmidt ref1 = list_entry(pos1, struct __prelim_ref, list); 4378da6d581SJan Schmidt 4388da6d581SJan Schmidt for (pos2 = pos1->next, n2 = pos2->next; pos2 != head; 4398da6d581SJan Schmidt pos2 = n2, n2 = pos2->next) { 4408da6d581SJan Schmidt struct __prelim_ref *ref2; 441d5c88b73SJan Schmidt struct __prelim_ref *xchg; 4428da6d581SJan Schmidt 4438da6d581SJan Schmidt ref2 = list_entry(pos2, struct __prelim_ref, list); 4448da6d581SJan Schmidt 4458da6d581SJan Schmidt if (mode == 1) { 446d5c88b73SJan Schmidt if (!ref_for_same_block(ref1, ref2)) 4478da6d581SJan Schmidt continue; 448d5c88b73SJan Schmidt if (!ref1->parent && ref2->parent) { 449d5c88b73SJan Schmidt xchg = ref1; 450d5c88b73SJan Schmidt ref1 = ref2; 451d5c88b73SJan Schmidt ref2 = xchg; 452d5c88b73SJan Schmidt } 4538da6d581SJan Schmidt ref1->count += ref2->count; 4548da6d581SJan Schmidt } else { 4558da6d581SJan Schmidt if (ref1->parent != ref2->parent) 4568da6d581SJan Schmidt continue; 4578da6d581SJan Schmidt ref1->count += ref2->count; 4588da6d581SJan Schmidt } 4598da6d581SJan Schmidt list_del(&ref2->list); 4608da6d581SJan Schmidt kfree(ref2); 4618da6d581SJan Schmidt } 4628da6d581SJan Schmidt 4638da6d581SJan Schmidt } 4648da6d581SJan Schmidt return 0; 4658da6d581SJan Schmidt } 4668da6d581SJan Schmidt 4678da6d581SJan Schmidt /* 4688da6d581SJan Schmidt * add all currently queued delayed refs from this head whose seq nr is 4698da6d581SJan Schmidt * smaller or equal that seq to the list 4708da6d581SJan Schmidt */ 4718da6d581SJan Schmidt static int __add_delayed_refs(struct btrfs_delayed_ref_head *head, u64 seq, 4728da6d581SJan Schmidt struct list_head *prefs) 4738da6d581SJan Schmidt { 4748da6d581SJan Schmidt struct btrfs_delayed_extent_op *extent_op = head->extent_op; 4758da6d581SJan Schmidt struct rb_node *n = &head->node.rb_node; 476d5c88b73SJan Schmidt struct btrfs_key key; 477d5c88b73SJan Schmidt struct btrfs_key op_key = {0}; 4788da6d581SJan Schmidt int sgn; 479b1375d64SJan Schmidt int ret = 0; 4808da6d581SJan Schmidt 4818da6d581SJan Schmidt if (extent_op && extent_op->update_key) 482d5c88b73SJan Schmidt btrfs_disk_key_to_cpu(&op_key, &extent_op->key); 4838da6d581SJan Schmidt 4848da6d581SJan Schmidt while ((n = rb_prev(n))) { 4858da6d581SJan Schmidt struct btrfs_delayed_ref_node *node; 4868da6d581SJan Schmidt node = rb_entry(n, struct btrfs_delayed_ref_node, 4878da6d581SJan Schmidt rb_node); 4888da6d581SJan Schmidt if (node->bytenr != head->node.bytenr) 4898da6d581SJan Schmidt break; 4908da6d581SJan Schmidt WARN_ON(node->is_head); 4918da6d581SJan Schmidt 4928da6d581SJan Schmidt if (node->seq > seq) 4938da6d581SJan Schmidt continue; 4948da6d581SJan Schmidt 4958da6d581SJan Schmidt switch (node->action) { 4968da6d581SJan Schmidt case BTRFS_ADD_DELAYED_EXTENT: 4978da6d581SJan Schmidt case BTRFS_UPDATE_DELAYED_HEAD: 4988da6d581SJan Schmidt WARN_ON(1); 4998da6d581SJan Schmidt continue; 5008da6d581SJan Schmidt case BTRFS_ADD_DELAYED_REF: 5018da6d581SJan Schmidt sgn = 1; 5028da6d581SJan Schmidt break; 5038da6d581SJan Schmidt case BTRFS_DROP_DELAYED_REF: 5048da6d581SJan Schmidt sgn = -1; 5058da6d581SJan Schmidt break; 5068da6d581SJan Schmidt default: 5078da6d581SJan Schmidt BUG_ON(1); 5088da6d581SJan Schmidt } 5098da6d581SJan Schmidt switch (node->type) { 5108da6d581SJan Schmidt case BTRFS_TREE_BLOCK_REF_KEY: { 5118da6d581SJan Schmidt struct btrfs_delayed_tree_ref *ref; 5128da6d581SJan Schmidt 5138da6d581SJan Schmidt ref = btrfs_delayed_node_to_tree_ref(node); 514d5c88b73SJan Schmidt ret = __add_prelim_ref(prefs, ref->root, &op_key, 5158da6d581SJan Schmidt ref->level + 1, 0, node->bytenr, 5168da6d581SJan Schmidt node->ref_mod * sgn); 5178da6d581SJan Schmidt break; 5188da6d581SJan Schmidt } 5198da6d581SJan Schmidt case BTRFS_SHARED_BLOCK_REF_KEY: { 5208da6d581SJan Schmidt struct btrfs_delayed_tree_ref *ref; 5218da6d581SJan Schmidt 5228da6d581SJan Schmidt ref = btrfs_delayed_node_to_tree_ref(node); 523d5c88b73SJan Schmidt ret = __add_prelim_ref(prefs, ref->root, NULL, 5248da6d581SJan Schmidt ref->level + 1, ref->parent, 5258da6d581SJan Schmidt node->bytenr, 5268da6d581SJan Schmidt node->ref_mod * sgn); 5278da6d581SJan Schmidt break; 5288da6d581SJan Schmidt } 5298da6d581SJan Schmidt case BTRFS_EXTENT_DATA_REF_KEY: { 5308da6d581SJan Schmidt struct btrfs_delayed_data_ref *ref; 5318da6d581SJan Schmidt ref = btrfs_delayed_node_to_data_ref(node); 5328da6d581SJan Schmidt 5338da6d581SJan Schmidt key.objectid = ref->objectid; 5348da6d581SJan Schmidt key.type = BTRFS_EXTENT_DATA_KEY; 5358da6d581SJan Schmidt key.offset = ref->offset; 5368da6d581SJan Schmidt ret = __add_prelim_ref(prefs, ref->root, &key, 0, 0, 5378da6d581SJan Schmidt node->bytenr, 5388da6d581SJan Schmidt node->ref_mod * sgn); 5398da6d581SJan Schmidt break; 5408da6d581SJan Schmidt } 5418da6d581SJan Schmidt case BTRFS_SHARED_DATA_REF_KEY: { 5428da6d581SJan Schmidt struct btrfs_delayed_data_ref *ref; 5438da6d581SJan Schmidt 5448da6d581SJan Schmidt ref = btrfs_delayed_node_to_data_ref(node); 5458da6d581SJan Schmidt 5468da6d581SJan Schmidt key.objectid = ref->objectid; 5478da6d581SJan Schmidt key.type = BTRFS_EXTENT_DATA_KEY; 5488da6d581SJan Schmidt key.offset = ref->offset; 5498da6d581SJan Schmidt ret = __add_prelim_ref(prefs, ref->root, &key, 0, 5508da6d581SJan Schmidt ref->parent, node->bytenr, 5518da6d581SJan Schmidt node->ref_mod * sgn); 5528da6d581SJan Schmidt break; 5538da6d581SJan Schmidt } 5548da6d581SJan Schmidt default: 5558da6d581SJan Schmidt WARN_ON(1); 5568da6d581SJan Schmidt } 5578da6d581SJan Schmidt BUG_ON(ret); 5588da6d581SJan Schmidt } 5598da6d581SJan Schmidt 5608da6d581SJan Schmidt return 0; 5618da6d581SJan Schmidt } 5628da6d581SJan Schmidt 5638da6d581SJan Schmidt /* 5648da6d581SJan Schmidt * add all inline backrefs for bytenr to the list 5658da6d581SJan Schmidt */ 5668da6d581SJan Schmidt static int __add_inline_refs(struct btrfs_fs_info *fs_info, 5678da6d581SJan Schmidt struct btrfs_path *path, u64 bytenr, 568d5c88b73SJan Schmidt int *info_level, struct list_head *prefs) 5698da6d581SJan Schmidt { 570b1375d64SJan Schmidt int ret = 0; 5718da6d581SJan Schmidt int slot; 5728da6d581SJan Schmidt struct extent_buffer *leaf; 5738da6d581SJan Schmidt struct btrfs_key key; 5748da6d581SJan Schmidt unsigned long ptr; 5758da6d581SJan Schmidt unsigned long end; 5768da6d581SJan Schmidt struct btrfs_extent_item *ei; 5778da6d581SJan Schmidt u64 flags; 5788da6d581SJan Schmidt u64 item_size; 5798da6d581SJan Schmidt 5808da6d581SJan Schmidt /* 5818da6d581SJan Schmidt * enumerate all inline refs 5828da6d581SJan Schmidt */ 5838da6d581SJan Schmidt leaf = path->nodes[0]; 584dadcaf78SJan Schmidt slot = path->slots[0]; 5858da6d581SJan Schmidt 5868da6d581SJan Schmidt item_size = btrfs_item_size_nr(leaf, slot); 5878da6d581SJan Schmidt BUG_ON(item_size < sizeof(*ei)); 5888da6d581SJan Schmidt 5898da6d581SJan Schmidt ei = btrfs_item_ptr(leaf, slot, struct btrfs_extent_item); 5908da6d581SJan Schmidt flags = btrfs_extent_flags(leaf, ei); 5918da6d581SJan Schmidt 5928da6d581SJan Schmidt ptr = (unsigned long)(ei + 1); 5938da6d581SJan Schmidt end = (unsigned long)ei + item_size; 5948da6d581SJan Schmidt 5958da6d581SJan Schmidt if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) { 5968da6d581SJan Schmidt struct btrfs_tree_block_info *info; 5978da6d581SJan Schmidt 5988da6d581SJan Schmidt info = (struct btrfs_tree_block_info *)ptr; 5998da6d581SJan Schmidt *info_level = btrfs_tree_block_level(leaf, info); 6008da6d581SJan Schmidt ptr += sizeof(struct btrfs_tree_block_info); 6018da6d581SJan Schmidt BUG_ON(ptr > end); 6028da6d581SJan Schmidt } else { 6038da6d581SJan Schmidt BUG_ON(!(flags & BTRFS_EXTENT_FLAG_DATA)); 6048da6d581SJan Schmidt } 6058da6d581SJan Schmidt 6068da6d581SJan Schmidt while (ptr < end) { 6078da6d581SJan Schmidt struct btrfs_extent_inline_ref *iref; 6088da6d581SJan Schmidt u64 offset; 6098da6d581SJan Schmidt int type; 6108da6d581SJan Schmidt 6118da6d581SJan Schmidt iref = (struct btrfs_extent_inline_ref *)ptr; 6128da6d581SJan Schmidt type = btrfs_extent_inline_ref_type(leaf, iref); 6138da6d581SJan Schmidt offset = btrfs_extent_inline_ref_offset(leaf, iref); 6148da6d581SJan Schmidt 6158da6d581SJan Schmidt switch (type) { 6168da6d581SJan Schmidt case BTRFS_SHARED_BLOCK_REF_KEY: 617d5c88b73SJan Schmidt ret = __add_prelim_ref(prefs, 0, NULL, 6188da6d581SJan Schmidt *info_level + 1, offset, 6198da6d581SJan Schmidt bytenr, 1); 6208da6d581SJan Schmidt break; 6218da6d581SJan Schmidt case BTRFS_SHARED_DATA_REF_KEY: { 6228da6d581SJan Schmidt struct btrfs_shared_data_ref *sdref; 6238da6d581SJan Schmidt int count; 6248da6d581SJan Schmidt 6258da6d581SJan Schmidt sdref = (struct btrfs_shared_data_ref *)(iref + 1); 6268da6d581SJan Schmidt count = btrfs_shared_data_ref_count(leaf, sdref); 6278da6d581SJan Schmidt ret = __add_prelim_ref(prefs, 0, NULL, 0, offset, 6288da6d581SJan Schmidt bytenr, count); 6298da6d581SJan Schmidt break; 6308da6d581SJan Schmidt } 6318da6d581SJan Schmidt case BTRFS_TREE_BLOCK_REF_KEY: 632d5c88b73SJan Schmidt ret = __add_prelim_ref(prefs, offset, NULL, 633d5c88b73SJan Schmidt *info_level + 1, 0, 634d5c88b73SJan Schmidt bytenr, 1); 6358da6d581SJan Schmidt break; 6368da6d581SJan Schmidt case BTRFS_EXTENT_DATA_REF_KEY: { 6378da6d581SJan Schmidt struct btrfs_extent_data_ref *dref; 6388da6d581SJan Schmidt int count; 6398da6d581SJan Schmidt u64 root; 6408da6d581SJan Schmidt 6418da6d581SJan Schmidt dref = (struct btrfs_extent_data_ref *)(&iref->offset); 6428da6d581SJan Schmidt count = btrfs_extent_data_ref_count(leaf, dref); 6438da6d581SJan Schmidt key.objectid = btrfs_extent_data_ref_objectid(leaf, 6448da6d581SJan Schmidt dref); 6458da6d581SJan Schmidt key.type = BTRFS_EXTENT_DATA_KEY; 6468da6d581SJan Schmidt key.offset = btrfs_extent_data_ref_offset(leaf, dref); 6478da6d581SJan Schmidt root = btrfs_extent_data_ref_root(leaf, dref); 648d5c88b73SJan Schmidt ret = __add_prelim_ref(prefs, root, &key, 0, 0, 649d5c88b73SJan Schmidt bytenr, count); 6508da6d581SJan Schmidt break; 6518da6d581SJan Schmidt } 6528da6d581SJan Schmidt default: 6538da6d581SJan Schmidt WARN_ON(1); 6548da6d581SJan Schmidt } 6558da6d581SJan Schmidt BUG_ON(ret); 6568da6d581SJan Schmidt ptr += btrfs_extent_inline_ref_size(type); 6578da6d581SJan Schmidt } 6588da6d581SJan Schmidt 6598da6d581SJan Schmidt return 0; 6608da6d581SJan Schmidt } 6618da6d581SJan Schmidt 6628da6d581SJan Schmidt /* 6638da6d581SJan Schmidt * add all non-inline backrefs for bytenr to the list 6648da6d581SJan Schmidt */ 6658da6d581SJan Schmidt static int __add_keyed_refs(struct btrfs_fs_info *fs_info, 6668da6d581SJan Schmidt struct btrfs_path *path, u64 bytenr, 667d5c88b73SJan Schmidt int info_level, struct list_head *prefs) 6688da6d581SJan Schmidt { 6698da6d581SJan Schmidt struct btrfs_root *extent_root = fs_info->extent_root; 6708da6d581SJan Schmidt int ret; 6718da6d581SJan Schmidt int slot; 6728da6d581SJan Schmidt struct extent_buffer *leaf; 6738da6d581SJan Schmidt struct btrfs_key key; 6748da6d581SJan Schmidt 6758da6d581SJan Schmidt while (1) { 6768da6d581SJan Schmidt ret = btrfs_next_item(extent_root, path); 6778da6d581SJan Schmidt if (ret < 0) 6788da6d581SJan Schmidt break; 6798da6d581SJan Schmidt if (ret) { 6808da6d581SJan Schmidt ret = 0; 6818da6d581SJan Schmidt break; 6828da6d581SJan Schmidt } 6838da6d581SJan Schmidt 6848da6d581SJan Schmidt slot = path->slots[0]; 6858da6d581SJan Schmidt leaf = path->nodes[0]; 6868da6d581SJan Schmidt btrfs_item_key_to_cpu(leaf, &key, slot); 6878da6d581SJan Schmidt 6888da6d581SJan Schmidt if (key.objectid != bytenr) 6898da6d581SJan Schmidt break; 6908da6d581SJan Schmidt if (key.type < BTRFS_TREE_BLOCK_REF_KEY) 6918da6d581SJan Schmidt continue; 6928da6d581SJan Schmidt if (key.type > BTRFS_SHARED_DATA_REF_KEY) 6938da6d581SJan Schmidt break; 6948da6d581SJan Schmidt 6958da6d581SJan Schmidt switch (key.type) { 6968da6d581SJan Schmidt case BTRFS_SHARED_BLOCK_REF_KEY: 697d5c88b73SJan Schmidt ret = __add_prelim_ref(prefs, 0, NULL, 6988da6d581SJan Schmidt info_level + 1, key.offset, 6998da6d581SJan Schmidt bytenr, 1); 7008da6d581SJan Schmidt break; 7018da6d581SJan Schmidt case BTRFS_SHARED_DATA_REF_KEY: { 7028da6d581SJan Schmidt struct btrfs_shared_data_ref *sdref; 7038da6d581SJan Schmidt int count; 7048da6d581SJan Schmidt 7058da6d581SJan Schmidt sdref = btrfs_item_ptr(leaf, slot, 7068da6d581SJan Schmidt struct btrfs_shared_data_ref); 7078da6d581SJan Schmidt count = btrfs_shared_data_ref_count(leaf, sdref); 7088da6d581SJan Schmidt ret = __add_prelim_ref(prefs, 0, NULL, 0, key.offset, 7098da6d581SJan Schmidt bytenr, count); 7108da6d581SJan Schmidt break; 7118da6d581SJan Schmidt } 7128da6d581SJan Schmidt case BTRFS_TREE_BLOCK_REF_KEY: 713d5c88b73SJan Schmidt ret = __add_prelim_ref(prefs, key.offset, NULL, 714d5c88b73SJan Schmidt info_level + 1, 0, 715d5c88b73SJan Schmidt bytenr, 1); 7168da6d581SJan Schmidt break; 7178da6d581SJan Schmidt case BTRFS_EXTENT_DATA_REF_KEY: { 7188da6d581SJan Schmidt struct btrfs_extent_data_ref *dref; 7198da6d581SJan Schmidt int count; 7208da6d581SJan Schmidt u64 root; 7218da6d581SJan Schmidt 7228da6d581SJan Schmidt dref = btrfs_item_ptr(leaf, slot, 7238da6d581SJan Schmidt struct btrfs_extent_data_ref); 7248da6d581SJan Schmidt count = btrfs_extent_data_ref_count(leaf, dref); 7258da6d581SJan Schmidt key.objectid = btrfs_extent_data_ref_objectid(leaf, 7268da6d581SJan Schmidt dref); 7278da6d581SJan Schmidt key.type = BTRFS_EXTENT_DATA_KEY; 7288da6d581SJan Schmidt key.offset = btrfs_extent_data_ref_offset(leaf, dref); 7298da6d581SJan Schmidt root = btrfs_extent_data_ref_root(leaf, dref); 7308da6d581SJan Schmidt ret = __add_prelim_ref(prefs, root, &key, 0, 0, 7318da6d581SJan Schmidt bytenr, count); 7328da6d581SJan Schmidt break; 7338da6d581SJan Schmidt } 7348da6d581SJan Schmidt default: 7358da6d581SJan Schmidt WARN_ON(1); 7368da6d581SJan Schmidt } 7378da6d581SJan Schmidt BUG_ON(ret); 7388da6d581SJan Schmidt } 7398da6d581SJan Schmidt 7408da6d581SJan Schmidt return ret; 7418da6d581SJan Schmidt } 7428da6d581SJan Schmidt 7438da6d581SJan Schmidt /* 7448da6d581SJan Schmidt * this adds all existing backrefs (inline backrefs, backrefs and delayed 7458da6d581SJan Schmidt * refs) for the given bytenr to the refs list, merges duplicates and resolves 7468da6d581SJan Schmidt * indirect refs to their parent bytenr. 7478da6d581SJan Schmidt * When roots are found, they're added to the roots list 7488da6d581SJan Schmidt * 7498da6d581SJan Schmidt * FIXME some caching might speed things up 7508da6d581SJan Schmidt */ 7518da6d581SJan Schmidt static int find_parent_nodes(struct btrfs_trans_handle *trans, 7528da6d581SJan Schmidt struct btrfs_fs_info *fs_info, u64 bytenr, 753*976b1908SJan Schmidt u64 seq, struct ulist *refs, struct ulist *roots, 754*976b1908SJan Schmidt const u64 *extent_item_pos) 7558da6d581SJan Schmidt { 7568da6d581SJan Schmidt struct btrfs_key key; 7578da6d581SJan Schmidt struct btrfs_path *path; 7588da6d581SJan Schmidt struct btrfs_delayed_ref_root *delayed_refs = NULL; 759d3b01064SLi Zefan struct btrfs_delayed_ref_head *head; 7608da6d581SJan Schmidt int info_level = 0; 7618da6d581SJan Schmidt int ret; 7627a3ae2f8SJan Schmidt int search_commit_root = (trans == BTRFS_BACKREF_SEARCH_COMMIT_ROOT); 7638da6d581SJan Schmidt struct list_head prefs_delayed; 7648da6d581SJan Schmidt struct list_head prefs; 7658da6d581SJan Schmidt struct __prelim_ref *ref; 7668da6d581SJan Schmidt 7678da6d581SJan Schmidt INIT_LIST_HEAD(&prefs); 7688da6d581SJan Schmidt INIT_LIST_HEAD(&prefs_delayed); 7698da6d581SJan Schmidt 7708da6d581SJan Schmidt key.objectid = bytenr; 7718da6d581SJan Schmidt key.type = BTRFS_EXTENT_ITEM_KEY; 7728da6d581SJan Schmidt key.offset = (u64)-1; 7738da6d581SJan Schmidt 7748da6d581SJan Schmidt path = btrfs_alloc_path(); 7758da6d581SJan Schmidt if (!path) 7768da6d581SJan Schmidt return -ENOMEM; 7777a3ae2f8SJan Schmidt path->search_commit_root = !!search_commit_root; 7788da6d581SJan Schmidt 7798da6d581SJan Schmidt /* 7808da6d581SJan Schmidt * grab both a lock on the path and a lock on the delayed ref head. 7818da6d581SJan Schmidt * We need both to get a consistent picture of how the refs look 7828da6d581SJan Schmidt * at a specified point in time 7838da6d581SJan Schmidt */ 7848da6d581SJan Schmidt again: 785d3b01064SLi Zefan head = NULL; 786d3b01064SLi Zefan 7878da6d581SJan Schmidt ret = btrfs_search_slot(trans, fs_info->extent_root, &key, path, 0, 0); 7888da6d581SJan Schmidt if (ret < 0) 7898da6d581SJan Schmidt goto out; 7908da6d581SJan Schmidt BUG_ON(ret == 0); 7918da6d581SJan Schmidt 7927a3ae2f8SJan Schmidt if (trans != BTRFS_BACKREF_SEARCH_COMMIT_ROOT) { 7938da6d581SJan Schmidt /* 7947a3ae2f8SJan Schmidt * look if there are updates for this ref queued and lock the 7957a3ae2f8SJan Schmidt * head 7968da6d581SJan Schmidt */ 7978da6d581SJan Schmidt delayed_refs = &trans->transaction->delayed_refs; 7988da6d581SJan Schmidt spin_lock(&delayed_refs->lock); 7998da6d581SJan Schmidt head = btrfs_find_delayed_ref_head(trans, bytenr); 8008da6d581SJan Schmidt if (head) { 8018da6d581SJan Schmidt if (!mutex_trylock(&head->mutex)) { 8028da6d581SJan Schmidt atomic_inc(&head->node.refs); 8038da6d581SJan Schmidt spin_unlock(&delayed_refs->lock); 8048da6d581SJan Schmidt 8058da6d581SJan Schmidt btrfs_release_path(path); 8068da6d581SJan Schmidt 8078da6d581SJan Schmidt /* 8088da6d581SJan Schmidt * Mutex was contended, block until it's 8098da6d581SJan Schmidt * released and try again 8108da6d581SJan Schmidt */ 8118da6d581SJan Schmidt mutex_lock(&head->mutex); 8128da6d581SJan Schmidt mutex_unlock(&head->mutex); 8138da6d581SJan Schmidt btrfs_put_delayed_ref(&head->node); 8148da6d581SJan Schmidt goto again; 8158da6d581SJan Schmidt } 816d5c88b73SJan Schmidt ret = __add_delayed_refs(head, seq, &prefs_delayed); 817d3b01064SLi Zefan if (ret) { 818d3b01064SLi Zefan spin_unlock(&delayed_refs->lock); 8198da6d581SJan Schmidt goto out; 8208da6d581SJan Schmidt } 821d3b01064SLi Zefan } 8228da6d581SJan Schmidt spin_unlock(&delayed_refs->lock); 8237a3ae2f8SJan Schmidt } 8248da6d581SJan Schmidt 8258da6d581SJan Schmidt if (path->slots[0]) { 8268da6d581SJan Schmidt struct extent_buffer *leaf; 8278da6d581SJan Schmidt int slot; 8288da6d581SJan Schmidt 829dadcaf78SJan Schmidt path->slots[0]--; 8308da6d581SJan Schmidt leaf = path->nodes[0]; 831dadcaf78SJan Schmidt slot = path->slots[0]; 8328da6d581SJan Schmidt btrfs_item_key_to_cpu(leaf, &key, slot); 8338da6d581SJan Schmidt if (key.objectid == bytenr && 8348da6d581SJan Schmidt key.type == BTRFS_EXTENT_ITEM_KEY) { 8358da6d581SJan Schmidt ret = __add_inline_refs(fs_info, path, bytenr, 836d5c88b73SJan Schmidt &info_level, &prefs); 8378da6d581SJan Schmidt if (ret) 8388da6d581SJan Schmidt goto out; 839d5c88b73SJan Schmidt ret = __add_keyed_refs(fs_info, path, bytenr, 8408da6d581SJan Schmidt info_level, &prefs); 8418da6d581SJan Schmidt if (ret) 8428da6d581SJan Schmidt goto out; 8438da6d581SJan Schmidt } 8448da6d581SJan Schmidt } 8458da6d581SJan Schmidt btrfs_release_path(path); 8468da6d581SJan Schmidt 8478da6d581SJan Schmidt list_splice_init(&prefs_delayed, &prefs); 8488da6d581SJan Schmidt 849d5c88b73SJan Schmidt ret = __add_missing_keys(fs_info, &prefs); 850d5c88b73SJan Schmidt if (ret) 851d5c88b73SJan Schmidt goto out; 852d5c88b73SJan Schmidt 8538da6d581SJan Schmidt ret = __merge_refs(&prefs, 1); 8548da6d581SJan Schmidt if (ret) 8558da6d581SJan Schmidt goto out; 8568da6d581SJan Schmidt 857*976b1908SJan Schmidt ret = __resolve_indirect_refs(fs_info, search_commit_root, &prefs, 858*976b1908SJan Schmidt extent_item_pos); 8598da6d581SJan Schmidt if (ret) 8608da6d581SJan Schmidt goto out; 8618da6d581SJan Schmidt 8628da6d581SJan Schmidt ret = __merge_refs(&prefs, 2); 8638da6d581SJan Schmidt if (ret) 8648da6d581SJan Schmidt goto out; 8658da6d581SJan Schmidt 8668da6d581SJan Schmidt while (!list_empty(&prefs)) { 8678da6d581SJan Schmidt ref = list_first_entry(&prefs, struct __prelim_ref, list); 8688da6d581SJan Schmidt list_del(&ref->list); 8698da6d581SJan Schmidt if (ref->count < 0) 8708da6d581SJan Schmidt WARN_ON(1); 8718da6d581SJan Schmidt if (ref->count && ref->root_id && ref->parent == 0) { 8728da6d581SJan Schmidt /* no parent == root of tree */ 8738da6d581SJan Schmidt ret = ulist_add(roots, ref->root_id, 0, GFP_NOFS); 8748da6d581SJan Schmidt BUG_ON(ret < 0); 8758da6d581SJan Schmidt } 8768da6d581SJan Schmidt if (ref->count && ref->parent) { 877*976b1908SJan Schmidt struct extent_inode_elem *eie = NULL; 878*976b1908SJan Schmidt if (extent_item_pos) { 879*976b1908SJan Schmidt u32 bsz; 880*976b1908SJan Schmidt struct extent_buffer *eb; 881*976b1908SJan Schmidt bsz = btrfs_level_size(fs_info->extent_root, 882*976b1908SJan Schmidt info_level); 883*976b1908SJan Schmidt eb = read_tree_block(fs_info->extent_root, 884*976b1908SJan Schmidt ref->parent, bsz, 0); 885*976b1908SJan Schmidt BUG_ON(!eb); 886*976b1908SJan Schmidt ret = find_extent_in_eb(eb, bytenr, 887*976b1908SJan Schmidt *extent_item_pos, &eie); 888*976b1908SJan Schmidt free_extent_buffer(eb); 889*976b1908SJan Schmidt } 890*976b1908SJan Schmidt ret = ulist_add(refs, ref->parent, 891*976b1908SJan Schmidt (unsigned long)eie, GFP_NOFS); 8928da6d581SJan Schmidt BUG_ON(ret < 0); 8938da6d581SJan Schmidt } 8948da6d581SJan Schmidt kfree(ref); 8958da6d581SJan Schmidt } 8968da6d581SJan Schmidt 8978da6d581SJan Schmidt out: 8988da6d581SJan Schmidt if (head) 8998da6d581SJan Schmidt mutex_unlock(&head->mutex); 9008da6d581SJan Schmidt btrfs_free_path(path); 9018da6d581SJan Schmidt while (!list_empty(&prefs)) { 9028da6d581SJan Schmidt ref = list_first_entry(&prefs, struct __prelim_ref, list); 9038da6d581SJan Schmidt list_del(&ref->list); 9048da6d581SJan Schmidt kfree(ref); 9058da6d581SJan Schmidt } 9068da6d581SJan Schmidt while (!list_empty(&prefs_delayed)) { 9078da6d581SJan Schmidt ref = list_first_entry(&prefs_delayed, struct __prelim_ref, 9088da6d581SJan Schmidt list); 9098da6d581SJan Schmidt list_del(&ref->list); 9108da6d581SJan Schmidt kfree(ref); 9118da6d581SJan Schmidt } 9128da6d581SJan Schmidt 9138da6d581SJan Schmidt return ret; 9148da6d581SJan Schmidt } 9158da6d581SJan Schmidt 916*976b1908SJan Schmidt static void free_leaf_list(struct ulist *blocks) 917*976b1908SJan Schmidt { 918*976b1908SJan Schmidt struct ulist_node *node = NULL; 919*976b1908SJan Schmidt struct extent_inode_elem *eie; 920*976b1908SJan Schmidt struct extent_inode_elem *eie_next; 921*976b1908SJan Schmidt struct ulist_iterator uiter; 922*976b1908SJan Schmidt 923*976b1908SJan Schmidt ULIST_ITER_INIT(&uiter); 924*976b1908SJan Schmidt while ((node = ulist_next(blocks, &uiter))) { 925*976b1908SJan Schmidt if (!node->aux) 926*976b1908SJan Schmidt continue; 927*976b1908SJan Schmidt eie = (struct extent_inode_elem *)node->aux; 928*976b1908SJan Schmidt for (; eie; eie = eie_next) { 929*976b1908SJan Schmidt eie_next = eie->next; 930*976b1908SJan Schmidt kfree(eie); 931*976b1908SJan Schmidt } 932*976b1908SJan Schmidt node->aux = 0; 933*976b1908SJan Schmidt } 934*976b1908SJan Schmidt 935*976b1908SJan Schmidt ulist_free(blocks); 936*976b1908SJan Schmidt } 937*976b1908SJan Schmidt 9388da6d581SJan Schmidt /* 9398da6d581SJan Schmidt * Finds all leafs with a reference to the specified combination of bytenr and 9408da6d581SJan Schmidt * offset. key_list_head will point to a list of corresponding keys (caller must 9418da6d581SJan Schmidt * free each list element). The leafs will be stored in the leafs ulist, which 9428da6d581SJan Schmidt * must be freed with ulist_free. 9438da6d581SJan Schmidt * 9448da6d581SJan Schmidt * returns 0 on success, <0 on error 9458da6d581SJan Schmidt */ 9468da6d581SJan Schmidt static int btrfs_find_all_leafs(struct btrfs_trans_handle *trans, 9478da6d581SJan Schmidt struct btrfs_fs_info *fs_info, u64 bytenr, 948*976b1908SJan Schmidt u64 seq, struct ulist **leafs, 949*976b1908SJan Schmidt const u64 *extent_item_pos) 9508da6d581SJan Schmidt { 9518da6d581SJan Schmidt struct ulist *tmp; 9528da6d581SJan Schmidt int ret; 9538da6d581SJan Schmidt 9548da6d581SJan Schmidt tmp = ulist_alloc(GFP_NOFS); 9558da6d581SJan Schmidt if (!tmp) 9568da6d581SJan Schmidt return -ENOMEM; 9578da6d581SJan Schmidt *leafs = ulist_alloc(GFP_NOFS); 9588da6d581SJan Schmidt if (!*leafs) { 9598da6d581SJan Schmidt ulist_free(tmp); 9608da6d581SJan Schmidt return -ENOMEM; 9618da6d581SJan Schmidt } 9628da6d581SJan Schmidt 963*976b1908SJan Schmidt ret = find_parent_nodes(trans, fs_info, bytenr, seq, *leafs, tmp, 964*976b1908SJan Schmidt extent_item_pos); 9658da6d581SJan Schmidt ulist_free(tmp); 9668da6d581SJan Schmidt 9678da6d581SJan Schmidt if (ret < 0 && ret != -ENOENT) { 968*976b1908SJan Schmidt free_leaf_list(*leafs); 9698da6d581SJan Schmidt return ret; 9708da6d581SJan Schmidt } 9718da6d581SJan Schmidt 9728da6d581SJan Schmidt return 0; 9738da6d581SJan Schmidt } 9748da6d581SJan Schmidt 9758da6d581SJan Schmidt /* 9768da6d581SJan Schmidt * walk all backrefs for a given extent to find all roots that reference this 9778da6d581SJan Schmidt * extent. Walking a backref means finding all extents that reference this 9788da6d581SJan Schmidt * extent and in turn walk the backrefs of those, too. Naturally this is a 9798da6d581SJan Schmidt * recursive process, but here it is implemented in an iterative fashion: We 9808da6d581SJan Schmidt * find all referencing extents for the extent in question and put them on a 9818da6d581SJan Schmidt * list. In turn, we find all referencing extents for those, further appending 9828da6d581SJan Schmidt * to the list. The way we iterate the list allows adding more elements after 9838da6d581SJan Schmidt * the current while iterating. The process stops when we reach the end of the 9848da6d581SJan Schmidt * list. Found roots are added to the roots list. 9858da6d581SJan Schmidt * 9868da6d581SJan Schmidt * returns 0 on success, < 0 on error. 9878da6d581SJan Schmidt */ 9888da6d581SJan Schmidt int btrfs_find_all_roots(struct btrfs_trans_handle *trans, 9898da6d581SJan Schmidt struct btrfs_fs_info *fs_info, u64 bytenr, 990*976b1908SJan Schmidt u64 seq, struct ulist **roots) 9918da6d581SJan Schmidt { 9928da6d581SJan Schmidt struct ulist *tmp; 9938da6d581SJan Schmidt struct ulist_node *node = NULL; 994cd1b413cSJan Schmidt struct ulist_iterator uiter; 9958da6d581SJan Schmidt int ret; 9968da6d581SJan Schmidt 9978da6d581SJan Schmidt tmp = ulist_alloc(GFP_NOFS); 9988da6d581SJan Schmidt if (!tmp) 9998da6d581SJan Schmidt return -ENOMEM; 10008da6d581SJan Schmidt *roots = ulist_alloc(GFP_NOFS); 10018da6d581SJan Schmidt if (!*roots) { 10028da6d581SJan Schmidt ulist_free(tmp); 10038da6d581SJan Schmidt return -ENOMEM; 10048da6d581SJan Schmidt } 10058da6d581SJan Schmidt 1006cd1b413cSJan Schmidt ULIST_ITER_INIT(&uiter); 10078da6d581SJan Schmidt while (1) { 10088da6d581SJan Schmidt ret = find_parent_nodes(trans, fs_info, bytenr, seq, 1009*976b1908SJan Schmidt tmp, *roots, NULL); 10108da6d581SJan Schmidt if (ret < 0 && ret != -ENOENT) { 10118da6d581SJan Schmidt ulist_free(tmp); 10128da6d581SJan Schmidt ulist_free(*roots); 10138da6d581SJan Schmidt return ret; 10148da6d581SJan Schmidt } 1015cd1b413cSJan Schmidt node = ulist_next(tmp, &uiter); 10168da6d581SJan Schmidt if (!node) 10178da6d581SJan Schmidt break; 10188da6d581SJan Schmidt bytenr = node->val; 10198da6d581SJan Schmidt } 10208da6d581SJan Schmidt 10218da6d581SJan Schmidt ulist_free(tmp); 10228da6d581SJan Schmidt return 0; 10238da6d581SJan Schmidt } 10248da6d581SJan Schmidt 10258da6d581SJan Schmidt 1026a542ad1bSJan Schmidt static int __inode_info(u64 inum, u64 ioff, u8 key_type, 1027a542ad1bSJan Schmidt struct btrfs_root *fs_root, struct btrfs_path *path, 1028a542ad1bSJan Schmidt struct btrfs_key *found_key) 1029a542ad1bSJan Schmidt { 1030a542ad1bSJan Schmidt int ret; 1031a542ad1bSJan Schmidt struct btrfs_key key; 1032a542ad1bSJan Schmidt struct extent_buffer *eb; 1033a542ad1bSJan Schmidt 1034a542ad1bSJan Schmidt key.type = key_type; 1035a542ad1bSJan Schmidt key.objectid = inum; 1036a542ad1bSJan Schmidt key.offset = ioff; 1037a542ad1bSJan Schmidt 1038a542ad1bSJan Schmidt ret = btrfs_search_slot(NULL, fs_root, &key, path, 0, 0); 1039a542ad1bSJan Schmidt if (ret < 0) 1040a542ad1bSJan Schmidt return ret; 1041a542ad1bSJan Schmidt 1042a542ad1bSJan Schmidt eb = path->nodes[0]; 1043a542ad1bSJan Schmidt if (ret && path->slots[0] >= btrfs_header_nritems(eb)) { 1044a542ad1bSJan Schmidt ret = btrfs_next_leaf(fs_root, path); 1045a542ad1bSJan Schmidt if (ret) 1046a542ad1bSJan Schmidt return ret; 1047a542ad1bSJan Schmidt eb = path->nodes[0]; 1048a542ad1bSJan Schmidt } 1049a542ad1bSJan Schmidt 1050a542ad1bSJan Schmidt btrfs_item_key_to_cpu(eb, found_key, path->slots[0]); 1051a542ad1bSJan Schmidt if (found_key->type != key.type || found_key->objectid != key.objectid) 1052a542ad1bSJan Schmidt return 1; 1053a542ad1bSJan Schmidt 1054a542ad1bSJan Schmidt return 0; 1055a542ad1bSJan Schmidt } 1056a542ad1bSJan Schmidt 1057a542ad1bSJan Schmidt /* 1058a542ad1bSJan Schmidt * this makes the path point to (inum INODE_ITEM ioff) 1059a542ad1bSJan Schmidt */ 1060a542ad1bSJan Schmidt int inode_item_info(u64 inum, u64 ioff, struct btrfs_root *fs_root, 1061a542ad1bSJan Schmidt struct btrfs_path *path) 1062a542ad1bSJan Schmidt { 1063a542ad1bSJan Schmidt struct btrfs_key key; 1064a542ad1bSJan Schmidt return __inode_info(inum, ioff, BTRFS_INODE_ITEM_KEY, fs_root, path, 1065a542ad1bSJan Schmidt &key); 1066a542ad1bSJan Schmidt } 1067a542ad1bSJan Schmidt 1068a542ad1bSJan Schmidt static int inode_ref_info(u64 inum, u64 ioff, struct btrfs_root *fs_root, 1069a542ad1bSJan Schmidt struct btrfs_path *path, 1070a542ad1bSJan Schmidt struct btrfs_key *found_key) 1071a542ad1bSJan Schmidt { 1072a542ad1bSJan Schmidt return __inode_info(inum, ioff, BTRFS_INODE_REF_KEY, fs_root, path, 1073a542ad1bSJan Schmidt found_key); 1074a542ad1bSJan Schmidt } 1075a542ad1bSJan Schmidt 1076a542ad1bSJan Schmidt /* 1077a542ad1bSJan Schmidt * this iterates to turn a btrfs_inode_ref into a full filesystem path. elements 1078a542ad1bSJan Schmidt * of the path are separated by '/' and the path is guaranteed to be 1079a542ad1bSJan Schmidt * 0-terminated. the path is only given within the current file system. 1080a542ad1bSJan Schmidt * Therefore, it never starts with a '/'. the caller is responsible to provide 1081a542ad1bSJan Schmidt * "size" bytes in "dest". the dest buffer will be filled backwards. finally, 1082a542ad1bSJan Schmidt * the start point of the resulting string is returned. this pointer is within 1083a542ad1bSJan Schmidt * dest, normally. 1084a542ad1bSJan Schmidt * in case the path buffer would overflow, the pointer is decremented further 1085a542ad1bSJan Schmidt * as if output was written to the buffer, though no more output is actually 1086a542ad1bSJan Schmidt * generated. that way, the caller can determine how much space would be 1087a542ad1bSJan Schmidt * required for the path to fit into the buffer. in that case, the returned 1088a542ad1bSJan Schmidt * value will be smaller than dest. callers must check this! 1089a542ad1bSJan Schmidt */ 1090a542ad1bSJan Schmidt static char *iref_to_path(struct btrfs_root *fs_root, struct btrfs_path *path, 1091a542ad1bSJan Schmidt struct btrfs_inode_ref *iref, 1092a542ad1bSJan Schmidt struct extent_buffer *eb_in, u64 parent, 1093a542ad1bSJan Schmidt char *dest, u32 size) 1094a542ad1bSJan Schmidt { 1095a542ad1bSJan Schmidt u32 len; 1096a542ad1bSJan Schmidt int slot; 1097a542ad1bSJan Schmidt u64 next_inum; 1098a542ad1bSJan Schmidt int ret; 1099a542ad1bSJan Schmidt s64 bytes_left = size - 1; 1100a542ad1bSJan Schmidt struct extent_buffer *eb = eb_in; 1101a542ad1bSJan Schmidt struct btrfs_key found_key; 1102b916a59aSJan Schmidt int leave_spinning = path->leave_spinning; 1103a542ad1bSJan Schmidt 1104a542ad1bSJan Schmidt if (bytes_left >= 0) 1105a542ad1bSJan Schmidt dest[bytes_left] = '\0'; 1106a542ad1bSJan Schmidt 1107b916a59aSJan Schmidt path->leave_spinning = 1; 1108a542ad1bSJan Schmidt while (1) { 1109a542ad1bSJan Schmidt len = btrfs_inode_ref_name_len(eb, iref); 1110a542ad1bSJan Schmidt bytes_left -= len; 1111a542ad1bSJan Schmidt if (bytes_left >= 0) 1112a542ad1bSJan Schmidt read_extent_buffer(eb, dest + bytes_left, 1113a542ad1bSJan Schmidt (unsigned long)(iref + 1), len); 1114b916a59aSJan Schmidt if (eb != eb_in) { 1115b916a59aSJan Schmidt btrfs_tree_read_unlock_blocking(eb); 1116a542ad1bSJan Schmidt free_extent_buffer(eb); 1117b916a59aSJan Schmidt } 1118a542ad1bSJan Schmidt ret = inode_ref_info(parent, 0, fs_root, path, &found_key); 11198f24b496SJan Schmidt if (ret > 0) 11208f24b496SJan Schmidt ret = -ENOENT; 1121a542ad1bSJan Schmidt if (ret) 1122a542ad1bSJan Schmidt break; 1123a542ad1bSJan Schmidt next_inum = found_key.offset; 1124a542ad1bSJan Schmidt 1125a542ad1bSJan Schmidt /* regular exit ahead */ 1126a542ad1bSJan Schmidt if (parent == next_inum) 1127a542ad1bSJan Schmidt break; 1128a542ad1bSJan Schmidt 1129a542ad1bSJan Schmidt slot = path->slots[0]; 1130a542ad1bSJan Schmidt eb = path->nodes[0]; 1131a542ad1bSJan Schmidt /* make sure we can use eb after releasing the path */ 1132b916a59aSJan Schmidt if (eb != eb_in) { 1133a542ad1bSJan Schmidt atomic_inc(&eb->refs); 1134b916a59aSJan Schmidt btrfs_tree_read_lock(eb); 1135b916a59aSJan Schmidt btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK); 1136b916a59aSJan Schmidt } 1137a542ad1bSJan Schmidt btrfs_release_path(path); 1138a542ad1bSJan Schmidt 1139a542ad1bSJan Schmidt iref = btrfs_item_ptr(eb, slot, struct btrfs_inode_ref); 1140a542ad1bSJan Schmidt parent = next_inum; 1141a542ad1bSJan Schmidt --bytes_left; 1142a542ad1bSJan Schmidt if (bytes_left >= 0) 1143a542ad1bSJan Schmidt dest[bytes_left] = '/'; 1144a542ad1bSJan Schmidt } 1145a542ad1bSJan Schmidt 1146a542ad1bSJan Schmidt btrfs_release_path(path); 1147b916a59aSJan Schmidt path->leave_spinning = leave_spinning; 1148a542ad1bSJan Schmidt 1149a542ad1bSJan Schmidt if (ret) 1150a542ad1bSJan Schmidt return ERR_PTR(ret); 1151a542ad1bSJan Schmidt 1152a542ad1bSJan Schmidt return dest + bytes_left; 1153a542ad1bSJan Schmidt } 1154a542ad1bSJan Schmidt 1155a542ad1bSJan Schmidt /* 1156a542ad1bSJan Schmidt * this makes the path point to (logical EXTENT_ITEM *) 1157a542ad1bSJan Schmidt * returns BTRFS_EXTENT_FLAG_DATA for data, BTRFS_EXTENT_FLAG_TREE_BLOCK for 1158a542ad1bSJan Schmidt * tree blocks and <0 on error. 1159a542ad1bSJan Schmidt */ 1160a542ad1bSJan Schmidt int extent_from_logical(struct btrfs_fs_info *fs_info, u64 logical, 1161a542ad1bSJan Schmidt struct btrfs_path *path, struct btrfs_key *found_key) 1162a542ad1bSJan Schmidt { 1163a542ad1bSJan Schmidt int ret; 1164a542ad1bSJan Schmidt u64 flags; 1165a542ad1bSJan Schmidt u32 item_size; 1166a542ad1bSJan Schmidt struct extent_buffer *eb; 1167a542ad1bSJan Schmidt struct btrfs_extent_item *ei; 1168a542ad1bSJan Schmidt struct btrfs_key key; 1169a542ad1bSJan Schmidt 1170a542ad1bSJan Schmidt key.type = BTRFS_EXTENT_ITEM_KEY; 1171a542ad1bSJan Schmidt key.objectid = logical; 1172a542ad1bSJan Schmidt key.offset = (u64)-1; 1173a542ad1bSJan Schmidt 1174a542ad1bSJan Schmidt ret = btrfs_search_slot(NULL, fs_info->extent_root, &key, path, 0, 0); 1175a542ad1bSJan Schmidt if (ret < 0) 1176a542ad1bSJan Schmidt return ret; 1177a542ad1bSJan Schmidt ret = btrfs_previous_item(fs_info->extent_root, path, 1178a542ad1bSJan Schmidt 0, BTRFS_EXTENT_ITEM_KEY); 1179a542ad1bSJan Schmidt if (ret < 0) 1180a542ad1bSJan Schmidt return ret; 1181a542ad1bSJan Schmidt 1182a542ad1bSJan Schmidt btrfs_item_key_to_cpu(path->nodes[0], found_key, path->slots[0]); 1183a542ad1bSJan Schmidt if (found_key->type != BTRFS_EXTENT_ITEM_KEY || 1184a542ad1bSJan Schmidt found_key->objectid > logical || 11854692cf58SJan Schmidt found_key->objectid + found_key->offset <= logical) { 11864692cf58SJan Schmidt pr_debug("logical %llu is not within any extent\n", 11874692cf58SJan Schmidt (unsigned long long)logical); 1188a542ad1bSJan Schmidt return -ENOENT; 11894692cf58SJan Schmidt } 1190a542ad1bSJan Schmidt 1191a542ad1bSJan Schmidt eb = path->nodes[0]; 1192a542ad1bSJan Schmidt item_size = btrfs_item_size_nr(eb, path->slots[0]); 1193a542ad1bSJan Schmidt BUG_ON(item_size < sizeof(*ei)); 1194a542ad1bSJan Schmidt 1195a542ad1bSJan Schmidt ei = btrfs_item_ptr(eb, path->slots[0], struct btrfs_extent_item); 1196a542ad1bSJan Schmidt flags = btrfs_extent_flags(eb, ei); 1197a542ad1bSJan Schmidt 11984692cf58SJan Schmidt pr_debug("logical %llu is at position %llu within the extent (%llu " 11994692cf58SJan Schmidt "EXTENT_ITEM %llu) flags %#llx size %u\n", 12004692cf58SJan Schmidt (unsigned long long)logical, 12014692cf58SJan Schmidt (unsigned long long)(logical - found_key->objectid), 12024692cf58SJan Schmidt (unsigned long long)found_key->objectid, 12034692cf58SJan Schmidt (unsigned long long)found_key->offset, 12044692cf58SJan Schmidt (unsigned long long)flags, item_size); 1205a542ad1bSJan Schmidt if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) 1206a542ad1bSJan Schmidt return BTRFS_EXTENT_FLAG_TREE_BLOCK; 1207a542ad1bSJan Schmidt if (flags & BTRFS_EXTENT_FLAG_DATA) 1208a542ad1bSJan Schmidt return BTRFS_EXTENT_FLAG_DATA; 1209a542ad1bSJan Schmidt 1210a542ad1bSJan Schmidt return -EIO; 1211a542ad1bSJan Schmidt } 1212a542ad1bSJan Schmidt 1213a542ad1bSJan Schmidt /* 1214a542ad1bSJan Schmidt * helper function to iterate extent inline refs. ptr must point to a 0 value 1215a542ad1bSJan Schmidt * for the first call and may be modified. it is used to track state. 1216a542ad1bSJan Schmidt * if more refs exist, 0 is returned and the next call to 1217a542ad1bSJan Schmidt * __get_extent_inline_ref must pass the modified ptr parameter to get the 1218a542ad1bSJan Schmidt * next ref. after the last ref was processed, 1 is returned. 1219a542ad1bSJan Schmidt * returns <0 on error 1220a542ad1bSJan Schmidt */ 1221a542ad1bSJan Schmidt static int __get_extent_inline_ref(unsigned long *ptr, struct extent_buffer *eb, 1222a542ad1bSJan Schmidt struct btrfs_extent_item *ei, u32 item_size, 1223a542ad1bSJan Schmidt struct btrfs_extent_inline_ref **out_eiref, 1224a542ad1bSJan Schmidt int *out_type) 1225a542ad1bSJan Schmidt { 1226a542ad1bSJan Schmidt unsigned long end; 1227a542ad1bSJan Schmidt u64 flags; 1228a542ad1bSJan Schmidt struct btrfs_tree_block_info *info; 1229a542ad1bSJan Schmidt 1230a542ad1bSJan Schmidt if (!*ptr) { 1231a542ad1bSJan Schmidt /* first call */ 1232a542ad1bSJan Schmidt flags = btrfs_extent_flags(eb, ei); 1233a542ad1bSJan Schmidt if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) { 1234a542ad1bSJan Schmidt info = (struct btrfs_tree_block_info *)(ei + 1); 1235a542ad1bSJan Schmidt *out_eiref = 1236a542ad1bSJan Schmidt (struct btrfs_extent_inline_ref *)(info + 1); 1237a542ad1bSJan Schmidt } else { 1238a542ad1bSJan Schmidt *out_eiref = (struct btrfs_extent_inline_ref *)(ei + 1); 1239a542ad1bSJan Schmidt } 1240a542ad1bSJan Schmidt *ptr = (unsigned long)*out_eiref; 1241a542ad1bSJan Schmidt if ((void *)*ptr >= (void *)ei + item_size) 1242a542ad1bSJan Schmidt return -ENOENT; 1243a542ad1bSJan Schmidt } 1244a542ad1bSJan Schmidt 1245a542ad1bSJan Schmidt end = (unsigned long)ei + item_size; 1246a542ad1bSJan Schmidt *out_eiref = (struct btrfs_extent_inline_ref *)*ptr; 1247a542ad1bSJan Schmidt *out_type = btrfs_extent_inline_ref_type(eb, *out_eiref); 1248a542ad1bSJan Schmidt 1249a542ad1bSJan Schmidt *ptr += btrfs_extent_inline_ref_size(*out_type); 1250a542ad1bSJan Schmidt WARN_ON(*ptr > end); 1251a542ad1bSJan Schmidt if (*ptr == end) 1252a542ad1bSJan Schmidt return 1; /* last */ 1253a542ad1bSJan Schmidt 1254a542ad1bSJan Schmidt return 0; 1255a542ad1bSJan Schmidt } 1256a542ad1bSJan Schmidt 1257a542ad1bSJan Schmidt /* 1258a542ad1bSJan Schmidt * reads the tree block backref for an extent. tree level and root are returned 1259a542ad1bSJan Schmidt * through out_level and out_root. ptr must point to a 0 value for the first 1260a542ad1bSJan Schmidt * call and may be modified (see __get_extent_inline_ref comment). 1261a542ad1bSJan Schmidt * returns 0 if data was provided, 1 if there was no more data to provide or 1262a542ad1bSJan Schmidt * <0 on error. 1263a542ad1bSJan Schmidt */ 1264a542ad1bSJan Schmidt int tree_backref_for_extent(unsigned long *ptr, struct extent_buffer *eb, 1265a542ad1bSJan Schmidt struct btrfs_extent_item *ei, u32 item_size, 1266a542ad1bSJan Schmidt u64 *out_root, u8 *out_level) 1267a542ad1bSJan Schmidt { 1268a542ad1bSJan Schmidt int ret; 1269a542ad1bSJan Schmidt int type; 1270a542ad1bSJan Schmidt struct btrfs_tree_block_info *info; 1271a542ad1bSJan Schmidt struct btrfs_extent_inline_ref *eiref; 1272a542ad1bSJan Schmidt 1273a542ad1bSJan Schmidt if (*ptr == (unsigned long)-1) 1274a542ad1bSJan Schmidt return 1; 1275a542ad1bSJan Schmidt 1276a542ad1bSJan Schmidt while (1) { 1277a542ad1bSJan Schmidt ret = __get_extent_inline_ref(ptr, eb, ei, item_size, 1278a542ad1bSJan Schmidt &eiref, &type); 1279a542ad1bSJan Schmidt if (ret < 0) 1280a542ad1bSJan Schmidt return ret; 1281a542ad1bSJan Schmidt 1282a542ad1bSJan Schmidt if (type == BTRFS_TREE_BLOCK_REF_KEY || 1283a542ad1bSJan Schmidt type == BTRFS_SHARED_BLOCK_REF_KEY) 1284a542ad1bSJan Schmidt break; 1285a542ad1bSJan Schmidt 1286a542ad1bSJan Schmidt if (ret == 1) 1287a542ad1bSJan Schmidt return 1; 1288a542ad1bSJan Schmidt } 1289a542ad1bSJan Schmidt 1290a542ad1bSJan Schmidt /* we can treat both ref types equally here */ 1291a542ad1bSJan Schmidt info = (struct btrfs_tree_block_info *)(ei + 1); 1292a542ad1bSJan Schmidt *out_root = btrfs_extent_inline_ref_offset(eb, eiref); 1293a542ad1bSJan Schmidt *out_level = btrfs_tree_block_level(eb, info); 1294a542ad1bSJan Schmidt 1295a542ad1bSJan Schmidt if (ret == 1) 1296a542ad1bSJan Schmidt *ptr = (unsigned long)-1; 1297a542ad1bSJan Schmidt 1298a542ad1bSJan Schmidt return 0; 1299a542ad1bSJan Schmidt } 1300a542ad1bSJan Schmidt 1301*976b1908SJan Schmidt static int iterate_leaf_refs(struct extent_inode_elem *inode_list, 1302*976b1908SJan Schmidt u64 root, u64 extent_item_objectid, 13034692cf58SJan Schmidt iterate_extent_inodes_t *iterate, void *ctx) 1304a542ad1bSJan Schmidt { 1305*976b1908SJan Schmidt struct extent_inode_elem *eie; 13064692cf58SJan Schmidt int ret = 0; 1307a542ad1bSJan Schmidt 1308*976b1908SJan Schmidt for (eie = inode_list; eie; eie = eie->next) { 13094692cf58SJan Schmidt pr_debug("ref for %llu resolved, key (%llu EXTEND_DATA %llu), " 1310*976b1908SJan Schmidt "root %llu\n", extent_item_objectid, 1311*976b1908SJan Schmidt eie->inum, eie->offset, root); 1312*976b1908SJan Schmidt ret = iterate(eie->inum, eie->offset, root, ctx); 13134692cf58SJan Schmidt if (ret) { 1314*976b1908SJan Schmidt pr_debug("stopping iteration for %llu due to ret=%d\n", 1315*976b1908SJan Schmidt extent_item_objectid, ret); 1316a542ad1bSJan Schmidt break; 1317a542ad1bSJan Schmidt } 1318a542ad1bSJan Schmidt } 1319a542ad1bSJan Schmidt 1320a542ad1bSJan Schmidt return ret; 1321a542ad1bSJan Schmidt } 1322a542ad1bSJan Schmidt 1323a542ad1bSJan Schmidt /* 1324a542ad1bSJan Schmidt * calls iterate() for every inode that references the extent identified by 13254692cf58SJan Schmidt * the given parameters. 1326a542ad1bSJan Schmidt * when the iterator function returns a non-zero value, iteration stops. 1327a542ad1bSJan Schmidt */ 1328a542ad1bSJan Schmidt int iterate_extent_inodes(struct btrfs_fs_info *fs_info, 13294692cf58SJan Schmidt u64 extent_item_objectid, u64 extent_item_pos, 13307a3ae2f8SJan Schmidt int search_commit_root, 1331a542ad1bSJan Schmidt iterate_extent_inodes_t *iterate, void *ctx) 1332a542ad1bSJan Schmidt { 1333a542ad1bSJan Schmidt int ret; 1334a542ad1bSJan Schmidt struct list_head data_refs = LIST_HEAD_INIT(data_refs); 1335a542ad1bSJan Schmidt struct list_head shared_refs = LIST_HEAD_INIT(shared_refs); 13364692cf58SJan Schmidt struct btrfs_trans_handle *trans; 13377a3ae2f8SJan Schmidt struct ulist *refs = NULL; 13387a3ae2f8SJan Schmidt struct ulist *roots = NULL; 13394692cf58SJan Schmidt struct ulist_node *ref_node = NULL; 13404692cf58SJan Schmidt struct ulist_node *root_node = NULL; 13414692cf58SJan Schmidt struct seq_list seq_elem; 1342cd1b413cSJan Schmidt struct ulist_iterator ref_uiter; 1343cd1b413cSJan Schmidt struct ulist_iterator root_uiter; 13447a3ae2f8SJan Schmidt struct btrfs_delayed_ref_root *delayed_refs = NULL; 1345a542ad1bSJan Schmidt 13464692cf58SJan Schmidt pr_debug("resolving all inodes for extent %llu\n", 13474692cf58SJan Schmidt extent_item_objectid); 13484692cf58SJan Schmidt 13497a3ae2f8SJan Schmidt if (search_commit_root) { 13507a3ae2f8SJan Schmidt trans = BTRFS_BACKREF_SEARCH_COMMIT_ROOT; 13517a3ae2f8SJan Schmidt } else { 13527a3ae2f8SJan Schmidt trans = btrfs_join_transaction(fs_info->extent_root); 13537a3ae2f8SJan Schmidt if (IS_ERR(trans)) 13547a3ae2f8SJan Schmidt return PTR_ERR(trans); 13557a3ae2f8SJan Schmidt 13564692cf58SJan Schmidt delayed_refs = &trans->transaction->delayed_refs; 13574692cf58SJan Schmidt spin_lock(&delayed_refs->lock); 13584692cf58SJan Schmidt btrfs_get_delayed_seq(delayed_refs, &seq_elem); 13594692cf58SJan Schmidt spin_unlock(&delayed_refs->lock); 13607a3ae2f8SJan Schmidt } 13614692cf58SJan Schmidt 13624692cf58SJan Schmidt ret = btrfs_find_all_leafs(trans, fs_info, extent_item_objectid, 1363*976b1908SJan Schmidt seq_elem.seq, &refs, &extent_item_pos); 13644692cf58SJan Schmidt if (ret) 13654692cf58SJan Schmidt goto out; 13664692cf58SJan Schmidt 1367cd1b413cSJan Schmidt ULIST_ITER_INIT(&ref_uiter); 1368cd1b413cSJan Schmidt while (!ret && (ref_node = ulist_next(refs, &ref_uiter))) { 1369*976b1908SJan Schmidt ret = btrfs_find_all_roots(trans, fs_info, ref_node->val, 13704692cf58SJan Schmidt seq_elem.seq, &roots); 13714692cf58SJan Schmidt if (ret) 1372a542ad1bSJan Schmidt break; 1373cd1b413cSJan Schmidt ULIST_ITER_INIT(&root_uiter); 1374cd1b413cSJan Schmidt while (!ret && (root_node = ulist_next(roots, &root_uiter))) { 1375*976b1908SJan Schmidt pr_debug("root %llu references leaf %llu, data list " 1376*976b1908SJan Schmidt "%#lx\n", root_node->val, ref_node->val, 1377*976b1908SJan Schmidt ref_node->aux); 1378*976b1908SJan Schmidt ret = iterate_leaf_refs( 1379*976b1908SJan Schmidt (struct extent_inode_elem *)ref_node->aux, 1380*976b1908SJan Schmidt root_node->val, extent_item_objectid, 1381a542ad1bSJan Schmidt iterate, ctx); 13824692cf58SJan Schmidt } 1383*976b1908SJan Schmidt ulist_free(roots); 1384*976b1908SJan Schmidt roots = NULL; 1385a542ad1bSJan Schmidt } 1386a542ad1bSJan Schmidt 1387*976b1908SJan Schmidt free_leaf_list(refs); 13884692cf58SJan Schmidt ulist_free(roots); 13894692cf58SJan Schmidt out: 13907a3ae2f8SJan Schmidt if (!search_commit_root) { 13914692cf58SJan Schmidt btrfs_put_delayed_seq(delayed_refs, &seq_elem); 13924692cf58SJan Schmidt btrfs_end_transaction(trans, fs_info->extent_root); 13937a3ae2f8SJan Schmidt } 13947a3ae2f8SJan Schmidt 1395a542ad1bSJan Schmidt return ret; 1396a542ad1bSJan Schmidt } 1397a542ad1bSJan Schmidt 1398a542ad1bSJan Schmidt int iterate_inodes_from_logical(u64 logical, struct btrfs_fs_info *fs_info, 1399a542ad1bSJan Schmidt struct btrfs_path *path, 1400a542ad1bSJan Schmidt iterate_extent_inodes_t *iterate, void *ctx) 1401a542ad1bSJan Schmidt { 1402a542ad1bSJan Schmidt int ret; 14034692cf58SJan Schmidt u64 extent_item_pos; 1404a542ad1bSJan Schmidt struct btrfs_key found_key; 14057a3ae2f8SJan Schmidt int search_commit_root = path->search_commit_root; 1406a542ad1bSJan Schmidt 1407a542ad1bSJan Schmidt ret = extent_from_logical(fs_info, logical, path, 1408a542ad1bSJan Schmidt &found_key); 14094692cf58SJan Schmidt btrfs_release_path(path); 1410a542ad1bSJan Schmidt if (ret & BTRFS_EXTENT_FLAG_TREE_BLOCK) 1411a542ad1bSJan Schmidt ret = -EINVAL; 1412a542ad1bSJan Schmidt if (ret < 0) 1413a542ad1bSJan Schmidt return ret; 1414a542ad1bSJan Schmidt 14154692cf58SJan Schmidt extent_item_pos = logical - found_key.objectid; 14167a3ae2f8SJan Schmidt ret = iterate_extent_inodes(fs_info, found_key.objectid, 14177a3ae2f8SJan Schmidt extent_item_pos, search_commit_root, 14187a3ae2f8SJan Schmidt iterate, ctx); 1419a542ad1bSJan Schmidt 1420a542ad1bSJan Schmidt return ret; 1421a542ad1bSJan Schmidt } 1422a542ad1bSJan Schmidt 1423a542ad1bSJan Schmidt static int iterate_irefs(u64 inum, struct btrfs_root *fs_root, 1424a542ad1bSJan Schmidt struct btrfs_path *path, 1425a542ad1bSJan Schmidt iterate_irefs_t *iterate, void *ctx) 1426a542ad1bSJan Schmidt { 1427aefc1eb1SJan Schmidt int ret = 0; 1428a542ad1bSJan Schmidt int slot; 1429a542ad1bSJan Schmidt u32 cur; 1430a542ad1bSJan Schmidt u32 len; 1431a542ad1bSJan Schmidt u32 name_len; 1432a542ad1bSJan Schmidt u64 parent = 0; 1433a542ad1bSJan Schmidt int found = 0; 1434a542ad1bSJan Schmidt struct extent_buffer *eb; 1435a542ad1bSJan Schmidt struct btrfs_item *item; 1436a542ad1bSJan Schmidt struct btrfs_inode_ref *iref; 1437a542ad1bSJan Schmidt struct btrfs_key found_key; 1438a542ad1bSJan Schmidt 1439aefc1eb1SJan Schmidt while (!ret) { 1440b916a59aSJan Schmidt path->leave_spinning = 1; 1441a542ad1bSJan Schmidt ret = inode_ref_info(inum, parent ? parent+1 : 0, fs_root, path, 1442a542ad1bSJan Schmidt &found_key); 1443a542ad1bSJan Schmidt if (ret < 0) 1444a542ad1bSJan Schmidt break; 1445a542ad1bSJan Schmidt if (ret) { 1446a542ad1bSJan Schmidt ret = found ? 0 : -ENOENT; 1447a542ad1bSJan Schmidt break; 1448a542ad1bSJan Schmidt } 1449a542ad1bSJan Schmidt ++found; 1450a542ad1bSJan Schmidt 1451a542ad1bSJan Schmidt parent = found_key.offset; 1452a542ad1bSJan Schmidt slot = path->slots[0]; 1453a542ad1bSJan Schmidt eb = path->nodes[0]; 1454a542ad1bSJan Schmidt /* make sure we can use eb after releasing the path */ 1455a542ad1bSJan Schmidt atomic_inc(&eb->refs); 1456b916a59aSJan Schmidt btrfs_tree_read_lock(eb); 1457b916a59aSJan Schmidt btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK); 1458a542ad1bSJan Schmidt btrfs_release_path(path); 1459a542ad1bSJan Schmidt 1460a542ad1bSJan Schmidt item = btrfs_item_nr(eb, slot); 1461a542ad1bSJan Schmidt iref = btrfs_item_ptr(eb, slot, struct btrfs_inode_ref); 1462a542ad1bSJan Schmidt 1463a542ad1bSJan Schmidt for (cur = 0; cur < btrfs_item_size(eb, item); cur += len) { 1464a542ad1bSJan Schmidt name_len = btrfs_inode_ref_name_len(eb, iref); 1465a542ad1bSJan Schmidt /* path must be released before calling iterate()! */ 14664692cf58SJan Schmidt pr_debug("following ref at offset %u for inode %llu in " 14674692cf58SJan Schmidt "tree %llu\n", cur, 14684692cf58SJan Schmidt (unsigned long long)found_key.objectid, 14694692cf58SJan Schmidt (unsigned long long)fs_root->objectid); 1470a542ad1bSJan Schmidt ret = iterate(parent, iref, eb, ctx); 1471aefc1eb1SJan Schmidt if (ret) 1472a542ad1bSJan Schmidt break; 1473a542ad1bSJan Schmidt len = sizeof(*iref) + name_len; 1474a542ad1bSJan Schmidt iref = (struct btrfs_inode_ref *)((char *)iref + len); 1475a542ad1bSJan Schmidt } 1476b916a59aSJan Schmidt btrfs_tree_read_unlock_blocking(eb); 1477a542ad1bSJan Schmidt free_extent_buffer(eb); 1478a542ad1bSJan Schmidt } 1479a542ad1bSJan Schmidt 1480a542ad1bSJan Schmidt btrfs_release_path(path); 1481a542ad1bSJan Schmidt 1482a542ad1bSJan Schmidt return ret; 1483a542ad1bSJan Schmidt } 1484a542ad1bSJan Schmidt 1485a542ad1bSJan Schmidt /* 1486a542ad1bSJan Schmidt * returns 0 if the path could be dumped (probably truncated) 1487a542ad1bSJan Schmidt * returns <0 in case of an error 1488a542ad1bSJan Schmidt */ 1489a542ad1bSJan Schmidt static int inode_to_path(u64 inum, struct btrfs_inode_ref *iref, 1490a542ad1bSJan Schmidt struct extent_buffer *eb, void *ctx) 1491a542ad1bSJan Schmidt { 1492a542ad1bSJan Schmidt struct inode_fs_paths *ipath = ctx; 1493a542ad1bSJan Schmidt char *fspath; 1494a542ad1bSJan Schmidt char *fspath_min; 1495a542ad1bSJan Schmidt int i = ipath->fspath->elem_cnt; 1496a542ad1bSJan Schmidt const int s_ptr = sizeof(char *); 1497a542ad1bSJan Schmidt u32 bytes_left; 1498a542ad1bSJan Schmidt 1499a542ad1bSJan Schmidt bytes_left = ipath->fspath->bytes_left > s_ptr ? 1500a542ad1bSJan Schmidt ipath->fspath->bytes_left - s_ptr : 0; 1501a542ad1bSJan Schmidt 1502740c3d22SChris Mason fspath_min = (char *)ipath->fspath->val + (i + 1) * s_ptr; 1503a542ad1bSJan Schmidt fspath = iref_to_path(ipath->fs_root, ipath->btrfs_path, iref, eb, 1504a542ad1bSJan Schmidt inum, fspath_min, bytes_left); 1505a542ad1bSJan Schmidt if (IS_ERR(fspath)) 1506a542ad1bSJan Schmidt return PTR_ERR(fspath); 1507a542ad1bSJan Schmidt 1508a542ad1bSJan Schmidt if (fspath > fspath_min) { 15094692cf58SJan Schmidt pr_debug("path resolved: %s\n", fspath); 1510745c4d8eSJeff Mahoney ipath->fspath->val[i] = (u64)(unsigned long)fspath; 1511a542ad1bSJan Schmidt ++ipath->fspath->elem_cnt; 1512a542ad1bSJan Schmidt ipath->fspath->bytes_left = fspath - fspath_min; 1513a542ad1bSJan Schmidt } else { 15144692cf58SJan Schmidt pr_debug("missed path, not enough space. missing bytes: %lu, " 15154692cf58SJan Schmidt "constructed so far: %s\n", 15164692cf58SJan Schmidt (unsigned long)(fspath_min - fspath), fspath_min); 1517a542ad1bSJan Schmidt ++ipath->fspath->elem_missed; 1518a542ad1bSJan Schmidt ipath->fspath->bytes_missing += fspath_min - fspath; 1519a542ad1bSJan Schmidt ipath->fspath->bytes_left = 0; 1520a542ad1bSJan Schmidt } 1521a542ad1bSJan Schmidt 1522a542ad1bSJan Schmidt return 0; 1523a542ad1bSJan Schmidt } 1524a542ad1bSJan Schmidt 1525a542ad1bSJan Schmidt /* 1526a542ad1bSJan Schmidt * this dumps all file system paths to the inode into the ipath struct, provided 1527a542ad1bSJan Schmidt * is has been created large enough. each path is zero-terminated and accessed 1528740c3d22SChris Mason * from ipath->fspath->val[i]. 1529a542ad1bSJan Schmidt * when it returns, there are ipath->fspath->elem_cnt number of paths available 1530740c3d22SChris Mason * in ipath->fspath->val[]. when the allocated space wasn't sufficient, the 1531a542ad1bSJan Schmidt * number of missed paths in recored in ipath->fspath->elem_missed, otherwise, 1532a542ad1bSJan Schmidt * it's zero. ipath->fspath->bytes_missing holds the number of bytes that would 1533a542ad1bSJan Schmidt * have been needed to return all paths. 1534a542ad1bSJan Schmidt */ 1535a542ad1bSJan Schmidt int paths_from_inode(u64 inum, struct inode_fs_paths *ipath) 1536a542ad1bSJan Schmidt { 1537a542ad1bSJan Schmidt return iterate_irefs(inum, ipath->fs_root, ipath->btrfs_path, 1538a542ad1bSJan Schmidt inode_to_path, ipath); 1539a542ad1bSJan Schmidt } 1540a542ad1bSJan Schmidt 1541a542ad1bSJan Schmidt struct btrfs_data_container *init_data_container(u32 total_bytes) 1542a542ad1bSJan Schmidt { 1543a542ad1bSJan Schmidt struct btrfs_data_container *data; 1544a542ad1bSJan Schmidt size_t alloc_bytes; 1545a542ad1bSJan Schmidt 1546a542ad1bSJan Schmidt alloc_bytes = max_t(size_t, total_bytes, sizeof(*data)); 1547a542ad1bSJan Schmidt data = kmalloc(alloc_bytes, GFP_NOFS); 1548a542ad1bSJan Schmidt if (!data) 1549a542ad1bSJan Schmidt return ERR_PTR(-ENOMEM); 1550a542ad1bSJan Schmidt 1551a542ad1bSJan Schmidt if (total_bytes >= sizeof(*data)) { 1552a542ad1bSJan Schmidt data->bytes_left = total_bytes - sizeof(*data); 1553a542ad1bSJan Schmidt data->bytes_missing = 0; 1554a542ad1bSJan Schmidt } else { 1555a542ad1bSJan Schmidt data->bytes_missing = sizeof(*data) - total_bytes; 1556a542ad1bSJan Schmidt data->bytes_left = 0; 1557a542ad1bSJan Schmidt } 1558a542ad1bSJan Schmidt 1559a542ad1bSJan Schmidt data->elem_cnt = 0; 1560a542ad1bSJan Schmidt data->elem_missed = 0; 1561a542ad1bSJan Schmidt 1562a542ad1bSJan Schmidt return data; 1563a542ad1bSJan Schmidt } 1564a542ad1bSJan Schmidt 1565a542ad1bSJan Schmidt /* 1566a542ad1bSJan Schmidt * allocates space to return multiple file system paths for an inode. 1567a542ad1bSJan Schmidt * total_bytes to allocate are passed, note that space usable for actual path 1568a542ad1bSJan Schmidt * information will be total_bytes - sizeof(struct inode_fs_paths). 1569a542ad1bSJan Schmidt * the returned pointer must be freed with free_ipath() in the end. 1570a542ad1bSJan Schmidt */ 1571a542ad1bSJan Schmidt struct inode_fs_paths *init_ipath(s32 total_bytes, struct btrfs_root *fs_root, 1572a542ad1bSJan Schmidt struct btrfs_path *path) 1573a542ad1bSJan Schmidt { 1574a542ad1bSJan Schmidt struct inode_fs_paths *ifp; 1575a542ad1bSJan Schmidt struct btrfs_data_container *fspath; 1576a542ad1bSJan Schmidt 1577a542ad1bSJan Schmidt fspath = init_data_container(total_bytes); 1578a542ad1bSJan Schmidt if (IS_ERR(fspath)) 1579a542ad1bSJan Schmidt return (void *)fspath; 1580a542ad1bSJan Schmidt 1581a542ad1bSJan Schmidt ifp = kmalloc(sizeof(*ifp), GFP_NOFS); 1582a542ad1bSJan Schmidt if (!ifp) { 1583a542ad1bSJan Schmidt kfree(fspath); 1584a542ad1bSJan Schmidt return ERR_PTR(-ENOMEM); 1585a542ad1bSJan Schmidt } 1586a542ad1bSJan Schmidt 1587a542ad1bSJan Schmidt ifp->btrfs_path = path; 1588a542ad1bSJan Schmidt ifp->fspath = fspath; 1589a542ad1bSJan Schmidt ifp->fs_root = fs_root; 1590a542ad1bSJan Schmidt 1591a542ad1bSJan Schmidt return ifp; 1592a542ad1bSJan Schmidt } 1593a542ad1bSJan Schmidt 1594a542ad1bSJan Schmidt void free_ipath(struct inode_fs_paths *ipath) 1595a542ad1bSJan Schmidt { 15964735fb28SJesper Juhl if (!ipath) 15974735fb28SJesper Juhl return; 15985eb56d25SIlya Dryomov kfree(ipath->fspath); 1599a542ad1bSJan Schmidt kfree(ipath); 1600a542ad1bSJan Schmidt } 1601