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 69f05c4746SWang Shilong static void free_inode_elem_list(struct extent_inode_elem *eie) 70f05c4746SWang Shilong { 71f05c4746SWang Shilong struct extent_inode_elem *eie_next; 72f05c4746SWang Shilong 73f05c4746SWang Shilong for (; eie; eie = eie_next) { 74f05c4746SWang Shilong eie_next = eie->next; 75f05c4746SWang Shilong kfree(eie); 76f05c4746SWang Shilong } 77f05c4746SWang Shilong } 78f05c4746SWang Shilong 79976b1908SJan Schmidt static int find_extent_in_eb(struct extent_buffer *eb, u64 wanted_disk_byte, 80976b1908SJan Schmidt u64 extent_item_pos, 81976b1908SJan Schmidt struct extent_inode_elem **eie) 82976b1908SJan Schmidt { 83976b1908SJan Schmidt u64 disk_byte; 84976b1908SJan Schmidt struct btrfs_key key; 85976b1908SJan Schmidt struct btrfs_file_extent_item *fi; 86976b1908SJan Schmidt int slot; 87976b1908SJan Schmidt int nritems; 88976b1908SJan Schmidt int extent_type; 89976b1908SJan Schmidt int ret; 90976b1908SJan Schmidt 91976b1908SJan Schmidt /* 92976b1908SJan Schmidt * from the shared data ref, we only have the leaf but we need 93976b1908SJan Schmidt * the key. thus, we must look into all items and see that we 94976b1908SJan Schmidt * find one (some) with a reference to our extent item. 95976b1908SJan Schmidt */ 96976b1908SJan Schmidt nritems = btrfs_header_nritems(eb); 97976b1908SJan Schmidt for (slot = 0; slot < nritems; ++slot) { 98976b1908SJan Schmidt btrfs_item_key_to_cpu(eb, &key, slot); 99976b1908SJan Schmidt if (key.type != BTRFS_EXTENT_DATA_KEY) 100976b1908SJan Schmidt continue; 101976b1908SJan Schmidt fi = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item); 102976b1908SJan Schmidt extent_type = btrfs_file_extent_type(eb, fi); 103976b1908SJan Schmidt if (extent_type == BTRFS_FILE_EXTENT_INLINE) 104976b1908SJan Schmidt continue; 105976b1908SJan Schmidt /* don't skip BTRFS_FILE_EXTENT_PREALLOC, we can handle that */ 106976b1908SJan Schmidt disk_byte = btrfs_file_extent_disk_bytenr(eb, fi); 107976b1908SJan Schmidt if (disk_byte != wanted_disk_byte) 108976b1908SJan Schmidt continue; 109976b1908SJan Schmidt 110976b1908SJan Schmidt ret = check_extent_in_eb(&key, eb, fi, extent_item_pos, eie); 111976b1908SJan Schmidt if (ret < 0) 112976b1908SJan Schmidt return ret; 113976b1908SJan Schmidt } 114976b1908SJan Schmidt 115976b1908SJan Schmidt return 0; 116976b1908SJan Schmidt } 117976b1908SJan Schmidt 1188da6d581SJan Schmidt /* 1198da6d581SJan Schmidt * this structure records all encountered refs on the way up to the root 1208da6d581SJan Schmidt */ 1218da6d581SJan Schmidt struct __prelim_ref { 1228da6d581SJan Schmidt struct list_head list; 1238da6d581SJan Schmidt u64 root_id; 124d5c88b73SJan Schmidt struct btrfs_key key_for_search; 1258da6d581SJan Schmidt int level; 1268da6d581SJan Schmidt int count; 1273301958bSJan Schmidt struct extent_inode_elem *inode_list; 1288da6d581SJan Schmidt u64 parent; 1298da6d581SJan Schmidt u64 wanted_disk_byte; 1308da6d581SJan Schmidt }; 1318da6d581SJan Schmidt 132b9e9a6cbSWang Shilong static struct kmem_cache *btrfs_prelim_ref_cache; 133b9e9a6cbSWang Shilong 134b9e9a6cbSWang Shilong int __init btrfs_prelim_ref_init(void) 135b9e9a6cbSWang Shilong { 136b9e9a6cbSWang Shilong btrfs_prelim_ref_cache = kmem_cache_create("btrfs_prelim_ref", 137b9e9a6cbSWang Shilong sizeof(struct __prelim_ref), 138b9e9a6cbSWang Shilong 0, 139b9e9a6cbSWang Shilong SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, 140b9e9a6cbSWang Shilong NULL); 141b9e9a6cbSWang Shilong if (!btrfs_prelim_ref_cache) 142b9e9a6cbSWang Shilong return -ENOMEM; 143b9e9a6cbSWang Shilong return 0; 144b9e9a6cbSWang Shilong } 145b9e9a6cbSWang Shilong 146b9e9a6cbSWang Shilong void btrfs_prelim_ref_exit(void) 147b9e9a6cbSWang Shilong { 148b9e9a6cbSWang Shilong if (btrfs_prelim_ref_cache) 149b9e9a6cbSWang Shilong kmem_cache_destroy(btrfs_prelim_ref_cache); 150b9e9a6cbSWang Shilong } 151b9e9a6cbSWang Shilong 152d5c88b73SJan Schmidt /* 153d5c88b73SJan Schmidt * the rules for all callers of this function are: 154d5c88b73SJan Schmidt * - obtaining the parent is the goal 155d5c88b73SJan Schmidt * - if you add a key, you must know that it is a correct key 156d5c88b73SJan Schmidt * - if you cannot add the parent or a correct key, then we will look into the 157d5c88b73SJan Schmidt * block later to set a correct key 158d5c88b73SJan Schmidt * 159d5c88b73SJan Schmidt * delayed refs 160d5c88b73SJan Schmidt * ============ 161d5c88b73SJan Schmidt * backref type | shared | indirect | shared | indirect 162d5c88b73SJan Schmidt * information | tree | tree | data | data 163d5c88b73SJan Schmidt * --------------------+--------+----------+--------+---------- 164d5c88b73SJan Schmidt * parent logical | y | - | - | - 165d5c88b73SJan Schmidt * key to resolve | - | y | y | y 166d5c88b73SJan Schmidt * tree block logical | - | - | - | - 167d5c88b73SJan Schmidt * root for resolving | y | y | y | y 168d5c88b73SJan Schmidt * 169d5c88b73SJan Schmidt * - column 1: we've the parent -> done 170d5c88b73SJan Schmidt * - column 2, 3, 4: we use the key to find the parent 171d5c88b73SJan Schmidt * 172d5c88b73SJan Schmidt * on disk refs (inline or keyed) 173d5c88b73SJan Schmidt * ============================== 174d5c88b73SJan Schmidt * backref type | shared | indirect | shared | indirect 175d5c88b73SJan Schmidt * information | tree | tree | data | data 176d5c88b73SJan Schmidt * --------------------+--------+----------+--------+---------- 177d5c88b73SJan Schmidt * parent logical | y | - | y | - 178d5c88b73SJan Schmidt * key to resolve | - | - | - | y 179d5c88b73SJan Schmidt * tree block logical | y | y | y | y 180d5c88b73SJan Schmidt * root for resolving | - | y | y | y 181d5c88b73SJan Schmidt * 182d5c88b73SJan Schmidt * - column 1, 3: we've the parent -> done 183d5c88b73SJan Schmidt * - column 2: we take the first key from the block to find the parent 184d5c88b73SJan Schmidt * (see __add_missing_keys) 185d5c88b73SJan Schmidt * - column 4: we use the key to find the parent 186d5c88b73SJan Schmidt * 187d5c88b73SJan Schmidt * additional information that's available but not required to find the parent 188d5c88b73SJan Schmidt * block might help in merging entries to gain some speed. 189d5c88b73SJan Schmidt */ 190d5c88b73SJan Schmidt 1918da6d581SJan Schmidt static int __add_prelim_ref(struct list_head *head, u64 root_id, 192d5c88b73SJan Schmidt struct btrfs_key *key, int level, 193742916b8SWang Shilong u64 parent, u64 wanted_disk_byte, int count, 194742916b8SWang Shilong gfp_t gfp_mask) 1958da6d581SJan Schmidt { 1968da6d581SJan Schmidt struct __prelim_ref *ref; 1978da6d581SJan Schmidt 19848ec4736SLiu Bo if (root_id == BTRFS_DATA_RELOC_TREE_OBJECTID) 19948ec4736SLiu Bo return 0; 20048ec4736SLiu Bo 201b9e9a6cbSWang Shilong ref = kmem_cache_alloc(btrfs_prelim_ref_cache, gfp_mask); 2028da6d581SJan Schmidt if (!ref) 2038da6d581SJan Schmidt return -ENOMEM; 2048da6d581SJan Schmidt 2058da6d581SJan Schmidt ref->root_id = root_id; 2068da6d581SJan Schmidt if (key) 207d5c88b73SJan Schmidt ref->key_for_search = *key; 2088da6d581SJan Schmidt else 209d5c88b73SJan Schmidt memset(&ref->key_for_search, 0, sizeof(ref->key_for_search)); 2108da6d581SJan Schmidt 2113301958bSJan Schmidt ref->inode_list = NULL; 2128da6d581SJan Schmidt ref->level = level; 2138da6d581SJan Schmidt ref->count = count; 2148da6d581SJan Schmidt ref->parent = parent; 2158da6d581SJan Schmidt ref->wanted_disk_byte = wanted_disk_byte; 2168da6d581SJan Schmidt list_add_tail(&ref->list, head); 2178da6d581SJan Schmidt 2188da6d581SJan Schmidt return 0; 2198da6d581SJan Schmidt } 2208da6d581SJan Schmidt 2218da6d581SJan Schmidt static int add_all_parents(struct btrfs_root *root, struct btrfs_path *path, 2227ef81ac8SJosef Bacik struct ulist *parents, struct __prelim_ref *ref, 22344853868SJosef Bacik int level, u64 time_seq, const u64 *extent_item_pos, 22444853868SJosef Bacik u64 total_refs) 2258da6d581SJan Schmidt { 22669bca40dSAlexander Block int ret = 0; 22769bca40dSAlexander Block int slot; 22869bca40dSAlexander Block struct extent_buffer *eb; 22969bca40dSAlexander Block struct btrfs_key key; 2307ef81ac8SJosef Bacik struct btrfs_key *key_for_search = &ref->key_for_search; 2318da6d581SJan Schmidt struct btrfs_file_extent_item *fi; 232ed8c4913SJosef Bacik struct extent_inode_elem *eie = NULL, *old = NULL; 2338da6d581SJan Schmidt u64 disk_byte; 2347ef81ac8SJosef Bacik u64 wanted_disk_byte = ref->wanted_disk_byte; 2357ef81ac8SJosef Bacik u64 count = 0; 2368da6d581SJan Schmidt 23769bca40dSAlexander Block if (level != 0) { 23869bca40dSAlexander Block eb = path->nodes[level]; 23969bca40dSAlexander Block ret = ulist_add(parents, eb->start, 0, GFP_NOFS); 2403301958bSJan Schmidt if (ret < 0) 2413301958bSJan Schmidt return ret; 2428da6d581SJan Schmidt return 0; 24369bca40dSAlexander Block } 2448da6d581SJan Schmidt 2458da6d581SJan Schmidt /* 24669bca40dSAlexander Block * We normally enter this function with the path already pointing to 24769bca40dSAlexander Block * the first item to check. But sometimes, we may enter it with 24869bca40dSAlexander Block * slot==nritems. In that case, go to the next leaf before we continue. 2498da6d581SJan Schmidt */ 25069bca40dSAlexander Block if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) 2513d7806ecSJan Schmidt ret = btrfs_next_old_leaf(root, path, time_seq); 2528da6d581SJan Schmidt 25344853868SJosef Bacik while (!ret && count < total_refs) { 2548da6d581SJan Schmidt eb = path->nodes[0]; 25569bca40dSAlexander Block slot = path->slots[0]; 25669bca40dSAlexander Block 25769bca40dSAlexander Block btrfs_item_key_to_cpu(eb, &key, slot); 25869bca40dSAlexander Block 25969bca40dSAlexander Block if (key.objectid != key_for_search->objectid || 26069bca40dSAlexander Block key.type != BTRFS_EXTENT_DATA_KEY) 26169bca40dSAlexander Block break; 26269bca40dSAlexander Block 26369bca40dSAlexander Block fi = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item); 2648da6d581SJan Schmidt disk_byte = btrfs_file_extent_disk_bytenr(eb, fi); 26569bca40dSAlexander Block 26669bca40dSAlexander Block if (disk_byte == wanted_disk_byte) { 26769bca40dSAlexander Block eie = NULL; 268ed8c4913SJosef Bacik old = NULL; 2697ef81ac8SJosef Bacik count++; 27069bca40dSAlexander Block if (extent_item_pos) { 27169bca40dSAlexander Block ret = check_extent_in_eb(&key, eb, fi, 27269bca40dSAlexander Block *extent_item_pos, 27369bca40dSAlexander Block &eie); 27469bca40dSAlexander Block if (ret < 0) 27569bca40dSAlexander Block break; 2768da6d581SJan Schmidt } 277ed8c4913SJosef Bacik if (ret > 0) 278ed8c4913SJosef Bacik goto next; 279ed8c4913SJosef Bacik ret = ulist_add_merge(parents, eb->start, 280ed8c4913SJosef Bacik (uintptr_t)eie, 281ed8c4913SJosef Bacik (u64 *)&old, GFP_NOFS); 28269bca40dSAlexander Block if (ret < 0) 28369bca40dSAlexander Block break; 284ed8c4913SJosef Bacik if (!ret && extent_item_pos) { 285ed8c4913SJosef Bacik while (old->next) 286ed8c4913SJosef Bacik old = old->next; 287ed8c4913SJosef Bacik old->next = eie; 28869bca40dSAlexander Block } 289f05c4746SWang Shilong eie = NULL; 29069bca40dSAlexander Block } 291ed8c4913SJosef Bacik next: 29269bca40dSAlexander Block ret = btrfs_next_old_item(root, path, time_seq); 2938da6d581SJan Schmidt } 2948da6d581SJan Schmidt 29569bca40dSAlexander Block if (ret > 0) 29669bca40dSAlexander Block ret = 0; 297f05c4746SWang Shilong else if (ret < 0) 298f05c4746SWang Shilong free_inode_elem_list(eie); 29969bca40dSAlexander Block return ret; 3008da6d581SJan Schmidt } 3018da6d581SJan Schmidt 3028da6d581SJan Schmidt /* 3038da6d581SJan Schmidt * resolve an indirect backref in the form (root_id, key, level) 3048da6d581SJan Schmidt * to a logical address 3058da6d581SJan Schmidt */ 3068da6d581SJan Schmidt static int __resolve_indirect_ref(struct btrfs_fs_info *fs_info, 307da61d31aSJosef Bacik struct btrfs_path *path, u64 time_seq, 3088da6d581SJan Schmidt struct __prelim_ref *ref, 309976b1908SJan Schmidt struct ulist *parents, 31044853868SJosef Bacik const u64 *extent_item_pos, u64 total_refs) 3118da6d581SJan Schmidt { 3128da6d581SJan Schmidt struct btrfs_root *root; 3138da6d581SJan Schmidt struct btrfs_key root_key; 3148da6d581SJan Schmidt struct extent_buffer *eb; 3158da6d581SJan Schmidt int ret = 0; 3168da6d581SJan Schmidt int root_level; 3178da6d581SJan Schmidt int level = ref->level; 318538f72cdSWang Shilong int index; 3198da6d581SJan Schmidt 3208da6d581SJan Schmidt root_key.objectid = ref->root_id; 3218da6d581SJan Schmidt root_key.type = BTRFS_ROOT_ITEM_KEY; 3228da6d581SJan Schmidt root_key.offset = (u64)-1; 323538f72cdSWang Shilong 324538f72cdSWang Shilong index = srcu_read_lock(&fs_info->subvol_srcu); 325538f72cdSWang Shilong 3268da6d581SJan Schmidt root = btrfs_read_fs_root_no_name(fs_info, &root_key); 3278da6d581SJan Schmidt if (IS_ERR(root)) { 328538f72cdSWang Shilong srcu_read_unlock(&fs_info->subvol_srcu, index); 3298da6d581SJan Schmidt ret = PTR_ERR(root); 3308da6d581SJan Schmidt goto out; 3318da6d581SJan Schmidt } 3328da6d581SJan Schmidt 333*9e351cc8SJosef Bacik if (path->search_commit_root) 334*9e351cc8SJosef Bacik root_level = btrfs_header_level(root->commit_root); 335*9e351cc8SJosef Bacik else 3365b6602e7SJan Schmidt root_level = btrfs_old_root_level(root, time_seq); 3378da6d581SJan Schmidt 338538f72cdSWang Shilong if (root_level + 1 == level) { 339538f72cdSWang Shilong srcu_read_unlock(&fs_info->subvol_srcu, index); 3408da6d581SJan Schmidt goto out; 341538f72cdSWang Shilong } 3428da6d581SJan Schmidt 3438da6d581SJan Schmidt path->lowest_level = level; 3448445f61cSJan Schmidt ret = btrfs_search_old_slot(root, &ref->key_for_search, path, time_seq); 345538f72cdSWang Shilong 346538f72cdSWang Shilong /* root node has been locked, we can release @subvol_srcu safely here */ 347538f72cdSWang Shilong srcu_read_unlock(&fs_info->subvol_srcu, index); 348538f72cdSWang Shilong 3498da6d581SJan Schmidt pr_debug("search slot in root %llu (level %d, ref count %d) returned " 3508da6d581SJan Schmidt "%d for key (%llu %u %llu)\n", 351c1c9ff7cSGeert Uytterhoeven ref->root_id, level, ref->count, ret, 352c1c9ff7cSGeert Uytterhoeven ref->key_for_search.objectid, ref->key_for_search.type, 353c1c9ff7cSGeert Uytterhoeven ref->key_for_search.offset); 3548da6d581SJan Schmidt if (ret < 0) 3558da6d581SJan Schmidt goto out; 3568da6d581SJan Schmidt 3578da6d581SJan Schmidt eb = path->nodes[level]; 3589345457fSJan Schmidt while (!eb) { 359fae7f21cSDulshani Gunawardhana if (WARN_ON(!level)) { 3608da6d581SJan Schmidt ret = 1; 3618da6d581SJan Schmidt goto out; 3628da6d581SJan Schmidt } 3639345457fSJan Schmidt level--; 3649345457fSJan Schmidt eb = path->nodes[level]; 3659345457fSJan Schmidt } 3668da6d581SJan Schmidt 3677ef81ac8SJosef Bacik ret = add_all_parents(root, path, parents, ref, level, time_seq, 36844853868SJosef Bacik extent_item_pos, total_refs); 3698da6d581SJan Schmidt out: 370da61d31aSJosef Bacik path->lowest_level = 0; 371da61d31aSJosef Bacik btrfs_release_path(path); 3728da6d581SJan Schmidt return ret; 3738da6d581SJan Schmidt } 3748da6d581SJan Schmidt 3758da6d581SJan Schmidt /* 3768da6d581SJan Schmidt * resolve all indirect backrefs from the list 3778da6d581SJan Schmidt */ 3788da6d581SJan Schmidt static int __resolve_indirect_refs(struct btrfs_fs_info *fs_info, 379da61d31aSJosef Bacik struct btrfs_path *path, u64 time_seq, 380976b1908SJan Schmidt struct list_head *head, 38144853868SJosef Bacik const u64 *extent_item_pos, u64 total_refs) 3828da6d581SJan Schmidt { 3838da6d581SJan Schmidt int err; 3848da6d581SJan Schmidt int ret = 0; 3858da6d581SJan Schmidt struct __prelim_ref *ref; 3868da6d581SJan Schmidt struct __prelim_ref *ref_safe; 3878da6d581SJan Schmidt struct __prelim_ref *new_ref; 3888da6d581SJan Schmidt struct ulist *parents; 3898da6d581SJan Schmidt struct ulist_node *node; 390cd1b413cSJan Schmidt struct ulist_iterator uiter; 3918da6d581SJan Schmidt 3928da6d581SJan Schmidt parents = ulist_alloc(GFP_NOFS); 3938da6d581SJan Schmidt if (!parents) 3948da6d581SJan Schmidt return -ENOMEM; 3958da6d581SJan Schmidt 3968da6d581SJan Schmidt /* 3978da6d581SJan Schmidt * _safe allows us to insert directly after the current item without 3988da6d581SJan Schmidt * iterating over the newly inserted items. 3998da6d581SJan Schmidt * we're also allowed to re-assign ref during iteration. 4008da6d581SJan Schmidt */ 4018da6d581SJan Schmidt list_for_each_entry_safe(ref, ref_safe, head, list) { 4028da6d581SJan Schmidt if (ref->parent) /* already direct */ 4038da6d581SJan Schmidt continue; 4048da6d581SJan Schmidt if (ref->count == 0) 4058da6d581SJan Schmidt continue; 406da61d31aSJosef Bacik err = __resolve_indirect_ref(fs_info, path, time_seq, ref, 40744853868SJosef Bacik parents, extent_item_pos, 40844853868SJosef Bacik total_refs); 40995def2edSWang Shilong /* 41095def2edSWang Shilong * we can only tolerate ENOENT,otherwise,we should catch error 41195def2edSWang Shilong * and return directly. 41295def2edSWang Shilong */ 41395def2edSWang Shilong if (err == -ENOENT) { 4148da6d581SJan Schmidt continue; 41595def2edSWang Shilong } else if (err) { 41695def2edSWang Shilong ret = err; 41795def2edSWang Shilong goto out; 41895def2edSWang Shilong } 4198da6d581SJan Schmidt 4208da6d581SJan Schmidt /* we put the first parent into the ref at hand */ 421cd1b413cSJan Schmidt ULIST_ITER_INIT(&uiter); 422cd1b413cSJan Schmidt node = ulist_next(parents, &uiter); 4238da6d581SJan Schmidt ref->parent = node ? node->val : 0; 424995e01b7SJan Schmidt ref->inode_list = node ? 42535a3621bSStefan Behrens (struct extent_inode_elem *)(uintptr_t)node->aux : NULL; 4268da6d581SJan Schmidt 4278da6d581SJan Schmidt /* additional parents require new refs being added here */ 428cd1b413cSJan Schmidt while ((node = ulist_next(parents, &uiter))) { 429b9e9a6cbSWang Shilong new_ref = kmem_cache_alloc(btrfs_prelim_ref_cache, 430b9e9a6cbSWang Shilong GFP_NOFS); 4318da6d581SJan Schmidt if (!new_ref) { 4328da6d581SJan Schmidt ret = -ENOMEM; 433e36902d4SWang Shilong goto out; 4348da6d581SJan Schmidt } 4358da6d581SJan Schmidt memcpy(new_ref, ref, sizeof(*ref)); 4368da6d581SJan Schmidt new_ref->parent = node->val; 437995e01b7SJan Schmidt new_ref->inode_list = (struct extent_inode_elem *) 438995e01b7SJan Schmidt (uintptr_t)node->aux; 4398da6d581SJan Schmidt list_add(&new_ref->list, &ref->list); 4408da6d581SJan Schmidt } 4418da6d581SJan Schmidt ulist_reinit(parents); 4428da6d581SJan Schmidt } 443e36902d4SWang Shilong out: 4448da6d581SJan Schmidt ulist_free(parents); 4458da6d581SJan Schmidt return ret; 4468da6d581SJan Schmidt } 4478da6d581SJan Schmidt 448d5c88b73SJan Schmidt static inline int ref_for_same_block(struct __prelim_ref *ref1, 449d5c88b73SJan Schmidt struct __prelim_ref *ref2) 450d5c88b73SJan Schmidt { 451d5c88b73SJan Schmidt if (ref1->level != ref2->level) 452d5c88b73SJan Schmidt return 0; 453d5c88b73SJan Schmidt if (ref1->root_id != ref2->root_id) 454d5c88b73SJan Schmidt return 0; 455d5c88b73SJan Schmidt if (ref1->key_for_search.type != ref2->key_for_search.type) 456d5c88b73SJan Schmidt return 0; 457d5c88b73SJan Schmidt if (ref1->key_for_search.objectid != ref2->key_for_search.objectid) 458d5c88b73SJan Schmidt return 0; 459d5c88b73SJan Schmidt if (ref1->key_for_search.offset != ref2->key_for_search.offset) 460d5c88b73SJan Schmidt return 0; 461d5c88b73SJan Schmidt if (ref1->parent != ref2->parent) 462d5c88b73SJan Schmidt return 0; 463d5c88b73SJan Schmidt 464d5c88b73SJan Schmidt return 1; 465d5c88b73SJan Schmidt } 466d5c88b73SJan Schmidt 467d5c88b73SJan Schmidt /* 468d5c88b73SJan Schmidt * read tree blocks and add keys where required. 469d5c88b73SJan Schmidt */ 470d5c88b73SJan Schmidt static int __add_missing_keys(struct btrfs_fs_info *fs_info, 471d5c88b73SJan Schmidt struct list_head *head) 472d5c88b73SJan Schmidt { 473d5c88b73SJan Schmidt struct list_head *pos; 474d5c88b73SJan Schmidt struct extent_buffer *eb; 475d5c88b73SJan Schmidt 476d5c88b73SJan Schmidt list_for_each(pos, head) { 477d5c88b73SJan Schmidt struct __prelim_ref *ref; 478d5c88b73SJan Schmidt ref = list_entry(pos, struct __prelim_ref, list); 479d5c88b73SJan Schmidt 480d5c88b73SJan Schmidt if (ref->parent) 481d5c88b73SJan Schmidt continue; 482d5c88b73SJan Schmidt if (ref->key_for_search.type) 483d5c88b73SJan Schmidt continue; 484d5c88b73SJan Schmidt BUG_ON(!ref->wanted_disk_byte); 485d5c88b73SJan Schmidt eb = read_tree_block(fs_info->tree_root, ref->wanted_disk_byte, 486d5c88b73SJan Schmidt fs_info->tree_root->leafsize, 0); 487416bc658SJosef Bacik if (!eb || !extent_buffer_uptodate(eb)) { 488416bc658SJosef Bacik free_extent_buffer(eb); 489416bc658SJosef Bacik return -EIO; 490416bc658SJosef Bacik } 491d5c88b73SJan Schmidt btrfs_tree_read_lock(eb); 492d5c88b73SJan Schmidt if (btrfs_header_level(eb) == 0) 493d5c88b73SJan Schmidt btrfs_item_key_to_cpu(eb, &ref->key_for_search, 0); 494d5c88b73SJan Schmidt else 495d5c88b73SJan Schmidt btrfs_node_key_to_cpu(eb, &ref->key_for_search, 0); 496d5c88b73SJan Schmidt btrfs_tree_read_unlock(eb); 497d5c88b73SJan Schmidt free_extent_buffer(eb); 498d5c88b73SJan Schmidt } 499d5c88b73SJan Schmidt return 0; 500d5c88b73SJan Schmidt } 501d5c88b73SJan Schmidt 5028da6d581SJan Schmidt /* 5038da6d581SJan Schmidt * merge two lists of backrefs and adjust counts accordingly 5048da6d581SJan Schmidt * 5058da6d581SJan Schmidt * mode = 1: merge identical keys, if key is set 506d5c88b73SJan Schmidt * FIXME: if we add more keys in __add_prelim_ref, we can merge more here. 507d5c88b73SJan Schmidt * additionally, we could even add a key range for the blocks we 508d5c88b73SJan Schmidt * looked into to merge even more (-> replace unresolved refs by those 509d5c88b73SJan Schmidt * having a parent). 5108da6d581SJan Schmidt * mode = 2: merge identical parents 5118da6d581SJan Schmidt */ 512692206b1SWang Shilong static void __merge_refs(struct list_head *head, int mode) 5138da6d581SJan Schmidt { 5148da6d581SJan Schmidt struct list_head *pos1; 5158da6d581SJan Schmidt 5168da6d581SJan Schmidt list_for_each(pos1, head) { 5178da6d581SJan Schmidt struct list_head *n2; 5188da6d581SJan Schmidt struct list_head *pos2; 5198da6d581SJan Schmidt struct __prelim_ref *ref1; 5208da6d581SJan Schmidt 5218da6d581SJan Schmidt ref1 = list_entry(pos1, struct __prelim_ref, list); 5228da6d581SJan Schmidt 5238da6d581SJan Schmidt for (pos2 = pos1->next, n2 = pos2->next; pos2 != head; 5248da6d581SJan Schmidt pos2 = n2, n2 = pos2->next) { 5258da6d581SJan Schmidt struct __prelim_ref *ref2; 526d5c88b73SJan Schmidt struct __prelim_ref *xchg; 5273ef5969cSAlexander Block struct extent_inode_elem *eie; 5288da6d581SJan Schmidt 5298da6d581SJan Schmidt ref2 = list_entry(pos2, struct __prelim_ref, list); 5308da6d581SJan Schmidt 5318da6d581SJan Schmidt if (mode == 1) { 532d5c88b73SJan Schmidt if (!ref_for_same_block(ref1, ref2)) 5338da6d581SJan Schmidt continue; 534d5c88b73SJan Schmidt if (!ref1->parent && ref2->parent) { 535d5c88b73SJan Schmidt xchg = ref1; 536d5c88b73SJan Schmidt ref1 = ref2; 537d5c88b73SJan Schmidt ref2 = xchg; 538d5c88b73SJan Schmidt } 5398da6d581SJan Schmidt } else { 5408da6d581SJan Schmidt if (ref1->parent != ref2->parent) 5418da6d581SJan Schmidt continue; 5428da6d581SJan Schmidt } 5433ef5969cSAlexander Block 5443ef5969cSAlexander Block eie = ref1->inode_list; 5453ef5969cSAlexander Block while (eie && eie->next) 5463ef5969cSAlexander Block eie = eie->next; 5473ef5969cSAlexander Block if (eie) 5483ef5969cSAlexander Block eie->next = ref2->inode_list; 5493ef5969cSAlexander Block else 5503ef5969cSAlexander Block ref1->inode_list = ref2->inode_list; 5513ef5969cSAlexander Block ref1->count += ref2->count; 5523ef5969cSAlexander Block 5538da6d581SJan Schmidt list_del(&ref2->list); 554b9e9a6cbSWang Shilong kmem_cache_free(btrfs_prelim_ref_cache, ref2); 5558da6d581SJan Schmidt } 5568da6d581SJan Schmidt 5578da6d581SJan Schmidt } 5588da6d581SJan Schmidt } 5598da6d581SJan Schmidt 5608da6d581SJan Schmidt /* 5618da6d581SJan Schmidt * add all currently queued delayed refs from this head whose seq nr is 5628da6d581SJan Schmidt * smaller or equal that seq to the list 5638da6d581SJan Schmidt */ 5648da6d581SJan Schmidt static int __add_delayed_refs(struct btrfs_delayed_ref_head *head, u64 seq, 56544853868SJosef Bacik struct list_head *prefs, u64 *total_refs) 5668da6d581SJan Schmidt { 5678da6d581SJan Schmidt struct btrfs_delayed_extent_op *extent_op = head->extent_op; 5688da6d581SJan Schmidt struct rb_node *n = &head->node.rb_node; 569d5c88b73SJan Schmidt struct btrfs_key key; 570d5c88b73SJan Schmidt struct btrfs_key op_key = {0}; 5718da6d581SJan Schmidt int sgn; 572b1375d64SJan Schmidt int ret = 0; 5738da6d581SJan Schmidt 5748da6d581SJan Schmidt if (extent_op && extent_op->update_key) 575d5c88b73SJan Schmidt btrfs_disk_key_to_cpu(&op_key, &extent_op->key); 5768da6d581SJan Schmidt 577d7df2c79SJosef Bacik spin_lock(&head->lock); 578d7df2c79SJosef Bacik n = rb_first(&head->ref_root); 579d7df2c79SJosef Bacik while (n) { 5808da6d581SJan Schmidt struct btrfs_delayed_ref_node *node; 5818da6d581SJan Schmidt node = rb_entry(n, struct btrfs_delayed_ref_node, 5828da6d581SJan Schmidt rb_node); 583d7df2c79SJosef Bacik n = rb_next(n); 5848da6d581SJan Schmidt if (node->seq > seq) 5858da6d581SJan Schmidt continue; 5868da6d581SJan Schmidt 5878da6d581SJan Schmidt switch (node->action) { 5888da6d581SJan Schmidt case BTRFS_ADD_DELAYED_EXTENT: 5898da6d581SJan Schmidt case BTRFS_UPDATE_DELAYED_HEAD: 5908da6d581SJan Schmidt WARN_ON(1); 5918da6d581SJan Schmidt continue; 5928da6d581SJan Schmidt case BTRFS_ADD_DELAYED_REF: 5938da6d581SJan Schmidt sgn = 1; 5948da6d581SJan Schmidt break; 5958da6d581SJan Schmidt case BTRFS_DROP_DELAYED_REF: 5968da6d581SJan Schmidt sgn = -1; 5978da6d581SJan Schmidt break; 5988da6d581SJan Schmidt default: 5998da6d581SJan Schmidt BUG_ON(1); 6008da6d581SJan Schmidt } 60144853868SJosef Bacik *total_refs += (node->ref_mod * sgn); 6028da6d581SJan Schmidt switch (node->type) { 6038da6d581SJan Schmidt case BTRFS_TREE_BLOCK_REF_KEY: { 6048da6d581SJan Schmidt struct btrfs_delayed_tree_ref *ref; 6058da6d581SJan Schmidt 6068da6d581SJan Schmidt ref = btrfs_delayed_node_to_tree_ref(node); 607d5c88b73SJan Schmidt ret = __add_prelim_ref(prefs, ref->root, &op_key, 6088da6d581SJan Schmidt ref->level + 1, 0, node->bytenr, 609742916b8SWang Shilong node->ref_mod * sgn, GFP_ATOMIC); 6108da6d581SJan Schmidt break; 6118da6d581SJan Schmidt } 6128da6d581SJan Schmidt case BTRFS_SHARED_BLOCK_REF_KEY: { 6138da6d581SJan Schmidt struct btrfs_delayed_tree_ref *ref; 6148da6d581SJan Schmidt 6158da6d581SJan Schmidt ref = btrfs_delayed_node_to_tree_ref(node); 616d5c88b73SJan Schmidt ret = __add_prelim_ref(prefs, ref->root, NULL, 6178da6d581SJan Schmidt ref->level + 1, ref->parent, 6188da6d581SJan Schmidt node->bytenr, 619742916b8SWang Shilong node->ref_mod * sgn, GFP_ATOMIC); 6208da6d581SJan Schmidt break; 6218da6d581SJan Schmidt } 6228da6d581SJan Schmidt case BTRFS_EXTENT_DATA_REF_KEY: { 6238da6d581SJan Schmidt struct btrfs_delayed_data_ref *ref; 6248da6d581SJan Schmidt ref = btrfs_delayed_node_to_data_ref(node); 6258da6d581SJan Schmidt 6268da6d581SJan Schmidt key.objectid = ref->objectid; 6278da6d581SJan Schmidt key.type = BTRFS_EXTENT_DATA_KEY; 6288da6d581SJan Schmidt key.offset = ref->offset; 6298da6d581SJan Schmidt ret = __add_prelim_ref(prefs, ref->root, &key, 0, 0, 6308da6d581SJan Schmidt node->bytenr, 631742916b8SWang Shilong node->ref_mod * sgn, GFP_ATOMIC); 6328da6d581SJan Schmidt break; 6338da6d581SJan Schmidt } 6348da6d581SJan Schmidt case BTRFS_SHARED_DATA_REF_KEY: { 6358da6d581SJan Schmidt struct btrfs_delayed_data_ref *ref; 6368da6d581SJan Schmidt 6378da6d581SJan Schmidt ref = btrfs_delayed_node_to_data_ref(node); 6388da6d581SJan Schmidt 6398da6d581SJan Schmidt key.objectid = ref->objectid; 6408da6d581SJan Schmidt key.type = BTRFS_EXTENT_DATA_KEY; 6418da6d581SJan Schmidt key.offset = ref->offset; 6428da6d581SJan Schmidt ret = __add_prelim_ref(prefs, ref->root, &key, 0, 6438da6d581SJan Schmidt ref->parent, node->bytenr, 644742916b8SWang Shilong node->ref_mod * sgn, GFP_ATOMIC); 6458da6d581SJan Schmidt break; 6468da6d581SJan Schmidt } 6478da6d581SJan Schmidt default: 6488da6d581SJan Schmidt WARN_ON(1); 6498da6d581SJan Schmidt } 6501149ab6bSWang Shilong if (ret) 651d7df2c79SJosef Bacik break; 6528da6d581SJan Schmidt } 653d7df2c79SJosef Bacik spin_unlock(&head->lock); 654d7df2c79SJosef Bacik return ret; 6558da6d581SJan Schmidt } 6568da6d581SJan Schmidt 6578da6d581SJan Schmidt /* 6588da6d581SJan Schmidt * add all inline backrefs for bytenr to the list 6598da6d581SJan Schmidt */ 6608da6d581SJan Schmidt static int __add_inline_refs(struct btrfs_fs_info *fs_info, 6618da6d581SJan Schmidt struct btrfs_path *path, u64 bytenr, 66244853868SJosef Bacik int *info_level, struct list_head *prefs, 66344853868SJosef Bacik u64 *total_refs) 6648da6d581SJan Schmidt { 665b1375d64SJan Schmidt int ret = 0; 6668da6d581SJan Schmidt int slot; 6678da6d581SJan Schmidt struct extent_buffer *leaf; 6688da6d581SJan Schmidt struct btrfs_key key; 669261c84b6SJosef Bacik struct btrfs_key found_key; 6708da6d581SJan Schmidt unsigned long ptr; 6718da6d581SJan Schmidt unsigned long end; 6728da6d581SJan Schmidt struct btrfs_extent_item *ei; 6738da6d581SJan Schmidt u64 flags; 6748da6d581SJan Schmidt u64 item_size; 6758da6d581SJan Schmidt 6768da6d581SJan Schmidt /* 6778da6d581SJan Schmidt * enumerate all inline refs 6788da6d581SJan Schmidt */ 6798da6d581SJan Schmidt leaf = path->nodes[0]; 680dadcaf78SJan Schmidt slot = path->slots[0]; 6818da6d581SJan Schmidt 6828da6d581SJan Schmidt item_size = btrfs_item_size_nr(leaf, slot); 6838da6d581SJan Schmidt BUG_ON(item_size < sizeof(*ei)); 6848da6d581SJan Schmidt 6858da6d581SJan Schmidt ei = btrfs_item_ptr(leaf, slot, struct btrfs_extent_item); 6868da6d581SJan Schmidt flags = btrfs_extent_flags(leaf, ei); 68744853868SJosef Bacik *total_refs += btrfs_extent_refs(leaf, ei); 688261c84b6SJosef Bacik btrfs_item_key_to_cpu(leaf, &found_key, slot); 6898da6d581SJan Schmidt 6908da6d581SJan Schmidt ptr = (unsigned long)(ei + 1); 6918da6d581SJan Schmidt end = (unsigned long)ei + item_size; 6928da6d581SJan Schmidt 693261c84b6SJosef Bacik if (found_key.type == BTRFS_EXTENT_ITEM_KEY && 694261c84b6SJosef Bacik flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) { 6958da6d581SJan Schmidt struct btrfs_tree_block_info *info; 6968da6d581SJan Schmidt 6978da6d581SJan Schmidt info = (struct btrfs_tree_block_info *)ptr; 6988da6d581SJan Schmidt *info_level = btrfs_tree_block_level(leaf, info); 6998da6d581SJan Schmidt ptr += sizeof(struct btrfs_tree_block_info); 7008da6d581SJan Schmidt BUG_ON(ptr > end); 701261c84b6SJosef Bacik } else if (found_key.type == BTRFS_METADATA_ITEM_KEY) { 702261c84b6SJosef Bacik *info_level = found_key.offset; 7038da6d581SJan Schmidt } else { 7048da6d581SJan Schmidt BUG_ON(!(flags & BTRFS_EXTENT_FLAG_DATA)); 7058da6d581SJan Schmidt } 7068da6d581SJan Schmidt 7078da6d581SJan Schmidt while (ptr < end) { 7088da6d581SJan Schmidt struct btrfs_extent_inline_ref *iref; 7098da6d581SJan Schmidt u64 offset; 7108da6d581SJan Schmidt int type; 7118da6d581SJan Schmidt 7128da6d581SJan Schmidt iref = (struct btrfs_extent_inline_ref *)ptr; 7138da6d581SJan Schmidt type = btrfs_extent_inline_ref_type(leaf, iref); 7148da6d581SJan Schmidt offset = btrfs_extent_inline_ref_offset(leaf, iref); 7158da6d581SJan Schmidt 7168da6d581SJan Schmidt switch (type) { 7178da6d581SJan Schmidt case BTRFS_SHARED_BLOCK_REF_KEY: 718d5c88b73SJan Schmidt ret = __add_prelim_ref(prefs, 0, NULL, 7198da6d581SJan Schmidt *info_level + 1, offset, 720742916b8SWang Shilong bytenr, 1, GFP_NOFS); 7218da6d581SJan Schmidt break; 7228da6d581SJan Schmidt case BTRFS_SHARED_DATA_REF_KEY: { 7238da6d581SJan Schmidt struct btrfs_shared_data_ref *sdref; 7248da6d581SJan Schmidt int count; 7258da6d581SJan Schmidt 7268da6d581SJan Schmidt sdref = (struct btrfs_shared_data_ref *)(iref + 1); 7278da6d581SJan Schmidt count = btrfs_shared_data_ref_count(leaf, sdref); 7288da6d581SJan Schmidt ret = __add_prelim_ref(prefs, 0, NULL, 0, offset, 729742916b8SWang Shilong bytenr, count, GFP_NOFS); 7308da6d581SJan Schmidt break; 7318da6d581SJan Schmidt } 7328da6d581SJan Schmidt case BTRFS_TREE_BLOCK_REF_KEY: 733d5c88b73SJan Schmidt ret = __add_prelim_ref(prefs, offset, NULL, 734d5c88b73SJan Schmidt *info_level + 1, 0, 735742916b8SWang Shilong bytenr, 1, GFP_NOFS); 7368da6d581SJan Schmidt break; 7378da6d581SJan Schmidt case BTRFS_EXTENT_DATA_REF_KEY: { 7388da6d581SJan Schmidt struct btrfs_extent_data_ref *dref; 7398da6d581SJan Schmidt int count; 7408da6d581SJan Schmidt u64 root; 7418da6d581SJan Schmidt 7428da6d581SJan Schmidt dref = (struct btrfs_extent_data_ref *)(&iref->offset); 7438da6d581SJan Schmidt count = btrfs_extent_data_ref_count(leaf, dref); 7448da6d581SJan Schmidt key.objectid = btrfs_extent_data_ref_objectid(leaf, 7458da6d581SJan Schmidt dref); 7468da6d581SJan Schmidt key.type = BTRFS_EXTENT_DATA_KEY; 7478da6d581SJan Schmidt key.offset = btrfs_extent_data_ref_offset(leaf, dref); 7488da6d581SJan Schmidt root = btrfs_extent_data_ref_root(leaf, dref); 749d5c88b73SJan Schmidt ret = __add_prelim_ref(prefs, root, &key, 0, 0, 750742916b8SWang Shilong bytenr, count, GFP_NOFS); 7518da6d581SJan Schmidt break; 7528da6d581SJan Schmidt } 7538da6d581SJan Schmidt default: 7548da6d581SJan Schmidt WARN_ON(1); 7558da6d581SJan Schmidt } 7561149ab6bSWang Shilong if (ret) 7571149ab6bSWang Shilong return ret; 7588da6d581SJan Schmidt ptr += btrfs_extent_inline_ref_size(type); 7598da6d581SJan Schmidt } 7608da6d581SJan Schmidt 7618da6d581SJan Schmidt return 0; 7628da6d581SJan Schmidt } 7638da6d581SJan Schmidt 7648da6d581SJan Schmidt /* 7658da6d581SJan Schmidt * add all non-inline backrefs for bytenr to the list 7668da6d581SJan Schmidt */ 7678da6d581SJan Schmidt static int __add_keyed_refs(struct btrfs_fs_info *fs_info, 7688da6d581SJan Schmidt struct btrfs_path *path, u64 bytenr, 769d5c88b73SJan Schmidt int info_level, struct list_head *prefs) 7708da6d581SJan Schmidt { 7718da6d581SJan Schmidt struct btrfs_root *extent_root = fs_info->extent_root; 7728da6d581SJan Schmidt int ret; 7738da6d581SJan Schmidt int slot; 7748da6d581SJan Schmidt struct extent_buffer *leaf; 7758da6d581SJan Schmidt struct btrfs_key key; 7768da6d581SJan Schmidt 7778da6d581SJan Schmidt while (1) { 7788da6d581SJan Schmidt ret = btrfs_next_item(extent_root, path); 7798da6d581SJan Schmidt if (ret < 0) 7808da6d581SJan Schmidt break; 7818da6d581SJan Schmidt if (ret) { 7828da6d581SJan Schmidt ret = 0; 7838da6d581SJan Schmidt break; 7848da6d581SJan Schmidt } 7858da6d581SJan Schmidt 7868da6d581SJan Schmidt slot = path->slots[0]; 7878da6d581SJan Schmidt leaf = path->nodes[0]; 7888da6d581SJan Schmidt btrfs_item_key_to_cpu(leaf, &key, slot); 7898da6d581SJan Schmidt 7908da6d581SJan Schmidt if (key.objectid != bytenr) 7918da6d581SJan Schmidt break; 7928da6d581SJan Schmidt if (key.type < BTRFS_TREE_BLOCK_REF_KEY) 7938da6d581SJan Schmidt continue; 7948da6d581SJan Schmidt if (key.type > BTRFS_SHARED_DATA_REF_KEY) 7958da6d581SJan Schmidt break; 7968da6d581SJan Schmidt 7978da6d581SJan Schmidt switch (key.type) { 7988da6d581SJan Schmidt case BTRFS_SHARED_BLOCK_REF_KEY: 799d5c88b73SJan Schmidt ret = __add_prelim_ref(prefs, 0, NULL, 8008da6d581SJan Schmidt info_level + 1, key.offset, 801742916b8SWang Shilong bytenr, 1, GFP_NOFS); 8028da6d581SJan Schmidt break; 8038da6d581SJan Schmidt case BTRFS_SHARED_DATA_REF_KEY: { 8048da6d581SJan Schmidt struct btrfs_shared_data_ref *sdref; 8058da6d581SJan Schmidt int count; 8068da6d581SJan Schmidt 8078da6d581SJan Schmidt sdref = btrfs_item_ptr(leaf, slot, 8088da6d581SJan Schmidt struct btrfs_shared_data_ref); 8098da6d581SJan Schmidt count = btrfs_shared_data_ref_count(leaf, sdref); 8108da6d581SJan Schmidt ret = __add_prelim_ref(prefs, 0, NULL, 0, key.offset, 811742916b8SWang Shilong bytenr, count, GFP_NOFS); 8128da6d581SJan Schmidt break; 8138da6d581SJan Schmidt } 8148da6d581SJan Schmidt case BTRFS_TREE_BLOCK_REF_KEY: 815d5c88b73SJan Schmidt ret = __add_prelim_ref(prefs, key.offset, NULL, 816d5c88b73SJan Schmidt info_level + 1, 0, 817742916b8SWang Shilong bytenr, 1, GFP_NOFS); 8188da6d581SJan Schmidt break; 8198da6d581SJan Schmidt case BTRFS_EXTENT_DATA_REF_KEY: { 8208da6d581SJan Schmidt struct btrfs_extent_data_ref *dref; 8218da6d581SJan Schmidt int count; 8228da6d581SJan Schmidt u64 root; 8238da6d581SJan Schmidt 8248da6d581SJan Schmidt dref = btrfs_item_ptr(leaf, slot, 8258da6d581SJan Schmidt struct btrfs_extent_data_ref); 8268da6d581SJan Schmidt count = btrfs_extent_data_ref_count(leaf, dref); 8278da6d581SJan Schmidt key.objectid = btrfs_extent_data_ref_objectid(leaf, 8288da6d581SJan Schmidt dref); 8298da6d581SJan Schmidt key.type = BTRFS_EXTENT_DATA_KEY; 8308da6d581SJan Schmidt key.offset = btrfs_extent_data_ref_offset(leaf, dref); 8318da6d581SJan Schmidt root = btrfs_extent_data_ref_root(leaf, dref); 8328da6d581SJan Schmidt ret = __add_prelim_ref(prefs, root, &key, 0, 0, 833742916b8SWang Shilong bytenr, count, GFP_NOFS); 8348da6d581SJan Schmidt break; 8358da6d581SJan Schmidt } 8368da6d581SJan Schmidt default: 8378da6d581SJan Schmidt WARN_ON(1); 8388da6d581SJan Schmidt } 8391149ab6bSWang Shilong if (ret) 8401149ab6bSWang Shilong return ret; 8411149ab6bSWang Shilong 8428da6d581SJan Schmidt } 8438da6d581SJan Schmidt 8448da6d581SJan Schmidt return ret; 8458da6d581SJan Schmidt } 8468da6d581SJan Schmidt 8478da6d581SJan Schmidt /* 8488da6d581SJan Schmidt * this adds all existing backrefs (inline backrefs, backrefs and delayed 8498da6d581SJan Schmidt * refs) for the given bytenr to the refs list, merges duplicates and resolves 8508da6d581SJan Schmidt * indirect refs to their parent bytenr. 8518da6d581SJan Schmidt * When roots are found, they're added to the roots list 8528da6d581SJan Schmidt * 8538da6d581SJan Schmidt * FIXME some caching might speed things up 8548da6d581SJan Schmidt */ 8558da6d581SJan Schmidt static int find_parent_nodes(struct btrfs_trans_handle *trans, 8568da6d581SJan Schmidt struct btrfs_fs_info *fs_info, u64 bytenr, 857097b8a7cSJan Schmidt u64 time_seq, struct ulist *refs, 858097b8a7cSJan Schmidt struct ulist *roots, const u64 *extent_item_pos) 8598da6d581SJan Schmidt { 8608da6d581SJan Schmidt struct btrfs_key key; 8618da6d581SJan Schmidt struct btrfs_path *path; 8628da6d581SJan Schmidt struct btrfs_delayed_ref_root *delayed_refs = NULL; 863d3b01064SLi Zefan struct btrfs_delayed_ref_head *head; 8648da6d581SJan Schmidt int info_level = 0; 8658da6d581SJan Schmidt int ret; 8668da6d581SJan Schmidt struct list_head prefs_delayed; 8678da6d581SJan Schmidt struct list_head prefs; 8688da6d581SJan Schmidt struct __prelim_ref *ref; 869f05c4746SWang Shilong struct extent_inode_elem *eie = NULL; 87044853868SJosef Bacik u64 total_refs = 0; 8718da6d581SJan Schmidt 8728da6d581SJan Schmidt INIT_LIST_HEAD(&prefs); 8738da6d581SJan Schmidt INIT_LIST_HEAD(&prefs_delayed); 8748da6d581SJan Schmidt 8758da6d581SJan Schmidt key.objectid = bytenr; 8768da6d581SJan Schmidt key.offset = (u64)-1; 877261c84b6SJosef Bacik if (btrfs_fs_incompat(fs_info, SKINNY_METADATA)) 878261c84b6SJosef Bacik key.type = BTRFS_METADATA_ITEM_KEY; 879261c84b6SJosef Bacik else 880261c84b6SJosef Bacik key.type = BTRFS_EXTENT_ITEM_KEY; 8818da6d581SJan Schmidt 8828da6d581SJan Schmidt path = btrfs_alloc_path(); 8838da6d581SJan Schmidt if (!path) 8848da6d581SJan Schmidt return -ENOMEM; 885e84752d4SWang Shilong if (!trans) { 886da61d31aSJosef Bacik path->search_commit_root = 1; 887e84752d4SWang Shilong path->skip_locking = 1; 888e84752d4SWang Shilong } 8898da6d581SJan Schmidt 8908da6d581SJan Schmidt /* 8918da6d581SJan Schmidt * grab both a lock on the path and a lock on the delayed ref head. 8928da6d581SJan Schmidt * We need both to get a consistent picture of how the refs look 8938da6d581SJan Schmidt * at a specified point in time 8948da6d581SJan Schmidt */ 8958da6d581SJan Schmidt again: 896d3b01064SLi Zefan head = NULL; 897d3b01064SLi Zefan 8988da6d581SJan Schmidt ret = btrfs_search_slot(trans, fs_info->extent_root, &key, path, 0, 0); 8998da6d581SJan Schmidt if (ret < 0) 9008da6d581SJan Schmidt goto out; 9018da6d581SJan Schmidt BUG_ON(ret == 0); 9028da6d581SJan Schmidt 903da61d31aSJosef Bacik if (trans) { 9048da6d581SJan Schmidt /* 9057a3ae2f8SJan Schmidt * look if there are updates for this ref queued and lock the 9067a3ae2f8SJan Schmidt * head 9078da6d581SJan Schmidt */ 9088da6d581SJan Schmidt delayed_refs = &trans->transaction->delayed_refs; 9098da6d581SJan Schmidt spin_lock(&delayed_refs->lock); 9108da6d581SJan Schmidt head = btrfs_find_delayed_ref_head(trans, bytenr); 9118da6d581SJan Schmidt if (head) { 9128da6d581SJan Schmidt if (!mutex_trylock(&head->mutex)) { 9138da6d581SJan Schmidt atomic_inc(&head->node.refs); 9148da6d581SJan Schmidt spin_unlock(&delayed_refs->lock); 9158da6d581SJan Schmidt 9168da6d581SJan Schmidt btrfs_release_path(path); 9178da6d581SJan Schmidt 9188da6d581SJan Schmidt /* 9198da6d581SJan Schmidt * Mutex was contended, block until it's 9208da6d581SJan Schmidt * released and try again 9218da6d581SJan Schmidt */ 9228da6d581SJan Schmidt mutex_lock(&head->mutex); 9238da6d581SJan Schmidt mutex_unlock(&head->mutex); 9248da6d581SJan Schmidt btrfs_put_delayed_ref(&head->node); 9258da6d581SJan Schmidt goto again; 9268da6d581SJan Schmidt } 927d7df2c79SJosef Bacik spin_unlock(&delayed_refs->lock); 928097b8a7cSJan Schmidt ret = __add_delayed_refs(head, time_seq, 92944853868SJosef Bacik &prefs_delayed, &total_refs); 930155725c9SJan Schmidt mutex_unlock(&head->mutex); 931d7df2c79SJosef Bacik if (ret) 9328da6d581SJan Schmidt goto out; 933d7df2c79SJosef Bacik } else { 9348da6d581SJan Schmidt spin_unlock(&delayed_refs->lock); 9357a3ae2f8SJan Schmidt } 936d7df2c79SJosef Bacik } 9378da6d581SJan Schmidt 9388da6d581SJan Schmidt if (path->slots[0]) { 9398da6d581SJan Schmidt struct extent_buffer *leaf; 9408da6d581SJan Schmidt int slot; 9418da6d581SJan Schmidt 942dadcaf78SJan Schmidt path->slots[0]--; 9438da6d581SJan Schmidt leaf = path->nodes[0]; 944dadcaf78SJan Schmidt slot = path->slots[0]; 9458da6d581SJan Schmidt btrfs_item_key_to_cpu(leaf, &key, slot); 9468da6d581SJan Schmidt if (key.objectid == bytenr && 947261c84b6SJosef Bacik (key.type == BTRFS_EXTENT_ITEM_KEY || 948261c84b6SJosef Bacik key.type == BTRFS_METADATA_ITEM_KEY)) { 9498da6d581SJan Schmidt ret = __add_inline_refs(fs_info, path, bytenr, 95044853868SJosef Bacik &info_level, &prefs, 95144853868SJosef Bacik &total_refs); 9528da6d581SJan Schmidt if (ret) 9538da6d581SJan Schmidt goto out; 954d5c88b73SJan Schmidt ret = __add_keyed_refs(fs_info, path, bytenr, 9558da6d581SJan Schmidt info_level, &prefs); 9568da6d581SJan Schmidt if (ret) 9578da6d581SJan Schmidt goto out; 9588da6d581SJan Schmidt } 9598da6d581SJan Schmidt } 9608da6d581SJan Schmidt btrfs_release_path(path); 9618da6d581SJan Schmidt 9628da6d581SJan Schmidt list_splice_init(&prefs_delayed, &prefs); 9638da6d581SJan Schmidt 964d5c88b73SJan Schmidt ret = __add_missing_keys(fs_info, &prefs); 965d5c88b73SJan Schmidt if (ret) 966d5c88b73SJan Schmidt goto out; 967d5c88b73SJan Schmidt 968692206b1SWang Shilong __merge_refs(&prefs, 1); 9698da6d581SJan Schmidt 970da61d31aSJosef Bacik ret = __resolve_indirect_refs(fs_info, path, time_seq, &prefs, 97144853868SJosef Bacik extent_item_pos, total_refs); 9728da6d581SJan Schmidt if (ret) 9738da6d581SJan Schmidt goto out; 9748da6d581SJan Schmidt 975692206b1SWang Shilong __merge_refs(&prefs, 2); 9768da6d581SJan Schmidt 9778da6d581SJan Schmidt while (!list_empty(&prefs)) { 9788da6d581SJan Schmidt ref = list_first_entry(&prefs, struct __prelim_ref, list); 9796c1500f2SJulia Lawall WARN_ON(ref->count < 0); 98098cfee21SWang Shilong if (roots && ref->count && ref->root_id && ref->parent == 0) { 9818da6d581SJan Schmidt /* no parent == root of tree */ 9828da6d581SJan Schmidt ret = ulist_add(roots, ref->root_id, 0, GFP_NOFS); 983f1723939SWang Shilong if (ret < 0) 984f1723939SWang Shilong goto out; 9858da6d581SJan Schmidt } 9868da6d581SJan Schmidt if (ref->count && ref->parent) { 9873301958bSJan Schmidt if (extent_item_pos && !ref->inode_list) { 988976b1908SJan Schmidt u32 bsz; 989976b1908SJan Schmidt struct extent_buffer *eb; 990976b1908SJan Schmidt bsz = btrfs_level_size(fs_info->extent_root, 991976b1908SJan Schmidt info_level); 992976b1908SJan Schmidt eb = read_tree_block(fs_info->extent_root, 993976b1908SJan Schmidt ref->parent, bsz, 0); 994416bc658SJosef Bacik if (!eb || !extent_buffer_uptodate(eb)) { 995416bc658SJosef Bacik free_extent_buffer(eb); 996c16c2e2eSWang Shilong ret = -EIO; 997c16c2e2eSWang Shilong goto out; 998416bc658SJosef Bacik } 999976b1908SJan Schmidt ret = find_extent_in_eb(eb, bytenr, 1000976b1908SJan Schmidt *extent_item_pos, &eie); 1001976b1908SJan Schmidt free_extent_buffer(eb); 1002f5929cd8SFilipe David Borba Manana if (ret < 0) 1003f5929cd8SFilipe David Borba Manana goto out; 1004f5929cd8SFilipe David Borba Manana ref->inode_list = eie; 1005976b1908SJan Schmidt } 10063301958bSJan Schmidt ret = ulist_add_merge(refs, ref->parent, 1007995e01b7SJan Schmidt (uintptr_t)ref->inode_list, 100834d73f54SAlexander Block (u64 *)&eie, GFP_NOFS); 1009f1723939SWang Shilong if (ret < 0) 1010f1723939SWang Shilong goto out; 10113301958bSJan Schmidt if (!ret && extent_item_pos) { 10123301958bSJan Schmidt /* 10133301958bSJan Schmidt * we've recorded that parent, so we must extend 10143301958bSJan Schmidt * its inode list here 10153301958bSJan Schmidt */ 10163301958bSJan Schmidt BUG_ON(!eie); 10173301958bSJan Schmidt while (eie->next) 10183301958bSJan Schmidt eie = eie->next; 10193301958bSJan Schmidt eie->next = ref->inode_list; 10203301958bSJan Schmidt } 1021f05c4746SWang Shilong eie = NULL; 10228da6d581SJan Schmidt } 1023a4fdb61eSWang Shilong list_del(&ref->list); 1024b9e9a6cbSWang Shilong kmem_cache_free(btrfs_prelim_ref_cache, ref); 10258da6d581SJan Schmidt } 10268da6d581SJan Schmidt 10278da6d581SJan Schmidt out: 10288da6d581SJan Schmidt btrfs_free_path(path); 10298da6d581SJan Schmidt while (!list_empty(&prefs)) { 10308da6d581SJan Schmidt ref = list_first_entry(&prefs, struct __prelim_ref, list); 10318da6d581SJan Schmidt list_del(&ref->list); 1032b9e9a6cbSWang Shilong kmem_cache_free(btrfs_prelim_ref_cache, ref); 10338da6d581SJan Schmidt } 10348da6d581SJan Schmidt while (!list_empty(&prefs_delayed)) { 10358da6d581SJan Schmidt ref = list_first_entry(&prefs_delayed, struct __prelim_ref, 10368da6d581SJan Schmidt list); 10378da6d581SJan Schmidt list_del(&ref->list); 1038b9e9a6cbSWang Shilong kmem_cache_free(btrfs_prelim_ref_cache, ref); 10398da6d581SJan Schmidt } 1040f05c4746SWang Shilong if (ret < 0) 1041f05c4746SWang Shilong free_inode_elem_list(eie); 10428da6d581SJan Schmidt return ret; 10438da6d581SJan Schmidt } 10448da6d581SJan Schmidt 1045976b1908SJan Schmidt static void free_leaf_list(struct ulist *blocks) 1046976b1908SJan Schmidt { 1047976b1908SJan Schmidt struct ulist_node *node = NULL; 1048976b1908SJan Schmidt struct extent_inode_elem *eie; 1049976b1908SJan Schmidt struct ulist_iterator uiter; 1050976b1908SJan Schmidt 1051976b1908SJan Schmidt ULIST_ITER_INIT(&uiter); 1052976b1908SJan Schmidt while ((node = ulist_next(blocks, &uiter))) { 1053976b1908SJan Schmidt if (!node->aux) 1054976b1908SJan Schmidt continue; 1055995e01b7SJan Schmidt eie = (struct extent_inode_elem *)(uintptr_t)node->aux; 1056f05c4746SWang Shilong free_inode_elem_list(eie); 1057976b1908SJan Schmidt node->aux = 0; 1058976b1908SJan Schmidt } 1059976b1908SJan Schmidt 1060976b1908SJan Schmidt ulist_free(blocks); 1061976b1908SJan Schmidt } 1062976b1908SJan Schmidt 10638da6d581SJan Schmidt /* 10648da6d581SJan Schmidt * Finds all leafs with a reference to the specified combination of bytenr and 10658da6d581SJan Schmidt * offset. key_list_head will point to a list of corresponding keys (caller must 10668da6d581SJan Schmidt * free each list element). The leafs will be stored in the leafs ulist, which 10678da6d581SJan Schmidt * must be freed with ulist_free. 10688da6d581SJan Schmidt * 10698da6d581SJan Schmidt * returns 0 on success, <0 on error 10708da6d581SJan Schmidt */ 10718da6d581SJan Schmidt static int btrfs_find_all_leafs(struct btrfs_trans_handle *trans, 10728da6d581SJan Schmidt struct btrfs_fs_info *fs_info, u64 bytenr, 1073097b8a7cSJan Schmidt u64 time_seq, struct ulist **leafs, 1074976b1908SJan Schmidt const u64 *extent_item_pos) 10758da6d581SJan Schmidt { 10768da6d581SJan Schmidt int ret; 10778da6d581SJan Schmidt 10788da6d581SJan Schmidt *leafs = ulist_alloc(GFP_NOFS); 107998cfee21SWang Shilong if (!*leafs) 10808da6d581SJan Schmidt return -ENOMEM; 10818da6d581SJan Schmidt 1082097b8a7cSJan Schmidt ret = find_parent_nodes(trans, fs_info, bytenr, 108398cfee21SWang Shilong time_seq, *leafs, NULL, extent_item_pos); 10848da6d581SJan Schmidt if (ret < 0 && ret != -ENOENT) { 1085976b1908SJan Schmidt free_leaf_list(*leafs); 10868da6d581SJan Schmidt return ret; 10878da6d581SJan Schmidt } 10888da6d581SJan Schmidt 10898da6d581SJan Schmidt return 0; 10908da6d581SJan Schmidt } 10918da6d581SJan Schmidt 10928da6d581SJan Schmidt /* 10938da6d581SJan Schmidt * walk all backrefs for a given extent to find all roots that reference this 10948da6d581SJan Schmidt * extent. Walking a backref means finding all extents that reference this 10958da6d581SJan Schmidt * extent and in turn walk the backrefs of those, too. Naturally this is a 10968da6d581SJan Schmidt * recursive process, but here it is implemented in an iterative fashion: We 10978da6d581SJan Schmidt * find all referencing extents for the extent in question and put them on a 10988da6d581SJan Schmidt * list. In turn, we find all referencing extents for those, further appending 10998da6d581SJan Schmidt * to the list. The way we iterate the list allows adding more elements after 11008da6d581SJan Schmidt * the current while iterating. The process stops when we reach the end of the 11018da6d581SJan Schmidt * list. Found roots are added to the roots list. 11028da6d581SJan Schmidt * 11038da6d581SJan Schmidt * returns 0 on success, < 0 on error. 11048da6d581SJan Schmidt */ 1105*9e351cc8SJosef Bacik static int __btrfs_find_all_roots(struct btrfs_trans_handle *trans, 11068da6d581SJan Schmidt struct btrfs_fs_info *fs_info, u64 bytenr, 1107097b8a7cSJan Schmidt u64 time_seq, struct ulist **roots) 11088da6d581SJan Schmidt { 11098da6d581SJan Schmidt struct ulist *tmp; 11108da6d581SJan Schmidt struct ulist_node *node = NULL; 1111cd1b413cSJan Schmidt struct ulist_iterator uiter; 11128da6d581SJan Schmidt int ret; 11138da6d581SJan Schmidt 11148da6d581SJan Schmidt tmp = ulist_alloc(GFP_NOFS); 11158da6d581SJan Schmidt if (!tmp) 11168da6d581SJan Schmidt return -ENOMEM; 11178da6d581SJan Schmidt *roots = ulist_alloc(GFP_NOFS); 11188da6d581SJan Schmidt if (!*roots) { 11198da6d581SJan Schmidt ulist_free(tmp); 11208da6d581SJan Schmidt return -ENOMEM; 11218da6d581SJan Schmidt } 11228da6d581SJan Schmidt 1123cd1b413cSJan Schmidt ULIST_ITER_INIT(&uiter); 11248da6d581SJan Schmidt while (1) { 1125097b8a7cSJan Schmidt ret = find_parent_nodes(trans, fs_info, bytenr, 11268445f61cSJan Schmidt time_seq, tmp, *roots, NULL); 11278da6d581SJan Schmidt if (ret < 0 && ret != -ENOENT) { 11288da6d581SJan Schmidt ulist_free(tmp); 11298da6d581SJan Schmidt ulist_free(*roots); 11308da6d581SJan Schmidt return ret; 11318da6d581SJan Schmidt } 1132cd1b413cSJan Schmidt node = ulist_next(tmp, &uiter); 11338da6d581SJan Schmidt if (!node) 11348da6d581SJan Schmidt break; 11358da6d581SJan Schmidt bytenr = node->val; 1136bca1a290SWang Shilong cond_resched(); 11378da6d581SJan Schmidt } 11388da6d581SJan Schmidt 11398da6d581SJan Schmidt ulist_free(tmp); 11408da6d581SJan Schmidt return 0; 11418da6d581SJan Schmidt } 11428da6d581SJan Schmidt 1143*9e351cc8SJosef Bacik int btrfs_find_all_roots(struct btrfs_trans_handle *trans, 1144*9e351cc8SJosef Bacik struct btrfs_fs_info *fs_info, u64 bytenr, 1145*9e351cc8SJosef Bacik u64 time_seq, struct ulist **roots) 1146*9e351cc8SJosef Bacik { 1147*9e351cc8SJosef Bacik int ret; 1148*9e351cc8SJosef Bacik 1149*9e351cc8SJosef Bacik if (!trans) 1150*9e351cc8SJosef Bacik down_read(&fs_info->commit_root_sem); 1151*9e351cc8SJosef Bacik ret = __btrfs_find_all_roots(trans, fs_info, bytenr, time_seq, roots); 1152*9e351cc8SJosef Bacik if (!trans) 1153*9e351cc8SJosef Bacik up_read(&fs_info->commit_root_sem); 1154*9e351cc8SJosef Bacik return ret; 1155*9e351cc8SJosef Bacik } 1156*9e351cc8SJosef Bacik 1157a542ad1bSJan Schmidt /* 1158a542ad1bSJan Schmidt * this makes the path point to (inum INODE_ITEM ioff) 1159a542ad1bSJan Schmidt */ 1160a542ad1bSJan Schmidt int inode_item_info(u64 inum, u64 ioff, struct btrfs_root *fs_root, 1161a542ad1bSJan Schmidt struct btrfs_path *path) 1162a542ad1bSJan Schmidt { 1163a542ad1bSJan Schmidt struct btrfs_key key; 1164e33d5c3dSKelley Nielsen return btrfs_find_item(fs_root, path, inum, ioff, 1165e33d5c3dSKelley Nielsen BTRFS_INODE_ITEM_KEY, &key); 1166a542ad1bSJan Schmidt } 1167a542ad1bSJan Schmidt 1168a542ad1bSJan Schmidt static int inode_ref_info(u64 inum, u64 ioff, struct btrfs_root *fs_root, 1169a542ad1bSJan Schmidt struct btrfs_path *path, 1170a542ad1bSJan Schmidt struct btrfs_key *found_key) 1171a542ad1bSJan Schmidt { 1172e33d5c3dSKelley Nielsen return btrfs_find_item(fs_root, path, inum, ioff, 1173e33d5c3dSKelley Nielsen BTRFS_INODE_REF_KEY, found_key); 1174a542ad1bSJan Schmidt } 1175a542ad1bSJan Schmidt 1176f186373fSMark Fasheh int btrfs_find_one_extref(struct btrfs_root *root, u64 inode_objectid, 1177f186373fSMark Fasheh u64 start_off, struct btrfs_path *path, 1178f186373fSMark Fasheh struct btrfs_inode_extref **ret_extref, 1179f186373fSMark Fasheh u64 *found_off) 1180f186373fSMark Fasheh { 1181f186373fSMark Fasheh int ret, slot; 1182f186373fSMark Fasheh struct btrfs_key key; 1183f186373fSMark Fasheh struct btrfs_key found_key; 1184f186373fSMark Fasheh struct btrfs_inode_extref *extref; 1185f186373fSMark Fasheh struct extent_buffer *leaf; 1186f186373fSMark Fasheh unsigned long ptr; 1187f186373fSMark Fasheh 1188f186373fSMark Fasheh key.objectid = inode_objectid; 1189f186373fSMark Fasheh btrfs_set_key_type(&key, BTRFS_INODE_EXTREF_KEY); 1190f186373fSMark Fasheh key.offset = start_off; 1191f186373fSMark Fasheh 1192f186373fSMark Fasheh ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); 1193f186373fSMark Fasheh if (ret < 0) 1194f186373fSMark Fasheh return ret; 1195f186373fSMark Fasheh 1196f186373fSMark Fasheh while (1) { 1197f186373fSMark Fasheh leaf = path->nodes[0]; 1198f186373fSMark Fasheh slot = path->slots[0]; 1199f186373fSMark Fasheh if (slot >= btrfs_header_nritems(leaf)) { 1200f186373fSMark Fasheh /* 1201f186373fSMark Fasheh * If the item at offset is not found, 1202f186373fSMark Fasheh * btrfs_search_slot will point us to the slot 1203f186373fSMark Fasheh * where it should be inserted. In our case 1204f186373fSMark Fasheh * that will be the slot directly before the 1205f186373fSMark Fasheh * next INODE_REF_KEY_V2 item. In the case 1206f186373fSMark Fasheh * that we're pointing to the last slot in a 1207f186373fSMark Fasheh * leaf, we must move one leaf over. 1208f186373fSMark Fasheh */ 1209f186373fSMark Fasheh ret = btrfs_next_leaf(root, path); 1210f186373fSMark Fasheh if (ret) { 1211f186373fSMark Fasheh if (ret >= 1) 1212f186373fSMark Fasheh ret = -ENOENT; 1213f186373fSMark Fasheh break; 1214f186373fSMark Fasheh } 1215f186373fSMark Fasheh continue; 1216f186373fSMark Fasheh } 1217f186373fSMark Fasheh 1218f186373fSMark Fasheh btrfs_item_key_to_cpu(leaf, &found_key, slot); 1219f186373fSMark Fasheh 1220f186373fSMark Fasheh /* 1221f186373fSMark Fasheh * Check that we're still looking at an extended ref key for 1222f186373fSMark Fasheh * this particular objectid. If we have different 1223f186373fSMark Fasheh * objectid or type then there are no more to be found 1224f186373fSMark Fasheh * in the tree and we can exit. 1225f186373fSMark Fasheh */ 1226f186373fSMark Fasheh ret = -ENOENT; 1227f186373fSMark Fasheh if (found_key.objectid != inode_objectid) 1228f186373fSMark Fasheh break; 1229f186373fSMark Fasheh if (btrfs_key_type(&found_key) != BTRFS_INODE_EXTREF_KEY) 1230f186373fSMark Fasheh break; 1231f186373fSMark Fasheh 1232f186373fSMark Fasheh ret = 0; 1233f186373fSMark Fasheh ptr = btrfs_item_ptr_offset(leaf, path->slots[0]); 1234f186373fSMark Fasheh extref = (struct btrfs_inode_extref *)ptr; 1235f186373fSMark Fasheh *ret_extref = extref; 1236f186373fSMark Fasheh if (found_off) 1237f186373fSMark Fasheh *found_off = found_key.offset; 1238f186373fSMark Fasheh break; 1239f186373fSMark Fasheh } 1240f186373fSMark Fasheh 1241f186373fSMark Fasheh return ret; 1242f186373fSMark Fasheh } 1243f186373fSMark Fasheh 124448a3b636SEric Sandeen /* 124548a3b636SEric Sandeen * this iterates to turn a name (from iref/extref) into a full filesystem path. 124648a3b636SEric Sandeen * Elements of the path are separated by '/' and the path is guaranteed to be 124748a3b636SEric Sandeen * 0-terminated. the path is only given within the current file system. 124848a3b636SEric Sandeen * Therefore, it never starts with a '/'. the caller is responsible to provide 124948a3b636SEric Sandeen * "size" bytes in "dest". the dest buffer will be filled backwards. finally, 125048a3b636SEric Sandeen * the start point of the resulting string is returned. this pointer is within 125148a3b636SEric Sandeen * dest, normally. 125248a3b636SEric Sandeen * in case the path buffer would overflow, the pointer is decremented further 125348a3b636SEric Sandeen * as if output was written to the buffer, though no more output is actually 125448a3b636SEric Sandeen * generated. that way, the caller can determine how much space would be 125548a3b636SEric Sandeen * required for the path to fit into the buffer. in that case, the returned 125648a3b636SEric Sandeen * value will be smaller than dest. callers must check this! 125748a3b636SEric Sandeen */ 125896b5bd77SJan Schmidt char *btrfs_ref_to_path(struct btrfs_root *fs_root, struct btrfs_path *path, 1259d24bec3aSMark Fasheh u32 name_len, unsigned long name_off, 1260a542ad1bSJan Schmidt struct extent_buffer *eb_in, u64 parent, 1261a542ad1bSJan Schmidt char *dest, u32 size) 1262a542ad1bSJan Schmidt { 1263a542ad1bSJan Schmidt int slot; 1264a542ad1bSJan Schmidt u64 next_inum; 1265a542ad1bSJan Schmidt int ret; 1266661bec6bSGabriel de Perthuis s64 bytes_left = ((s64)size) - 1; 1267a542ad1bSJan Schmidt struct extent_buffer *eb = eb_in; 1268a542ad1bSJan Schmidt struct btrfs_key found_key; 1269b916a59aSJan Schmidt int leave_spinning = path->leave_spinning; 1270d24bec3aSMark Fasheh struct btrfs_inode_ref *iref; 1271a542ad1bSJan Schmidt 1272a542ad1bSJan Schmidt if (bytes_left >= 0) 1273a542ad1bSJan Schmidt dest[bytes_left] = '\0'; 1274a542ad1bSJan Schmidt 1275b916a59aSJan Schmidt path->leave_spinning = 1; 1276a542ad1bSJan Schmidt while (1) { 1277d24bec3aSMark Fasheh bytes_left -= name_len; 1278a542ad1bSJan Schmidt if (bytes_left >= 0) 1279a542ad1bSJan Schmidt read_extent_buffer(eb, dest + bytes_left, 1280d24bec3aSMark Fasheh name_off, name_len); 1281b916a59aSJan Schmidt if (eb != eb_in) { 1282b916a59aSJan Schmidt btrfs_tree_read_unlock_blocking(eb); 1283a542ad1bSJan Schmidt free_extent_buffer(eb); 1284b916a59aSJan Schmidt } 1285a542ad1bSJan Schmidt ret = inode_ref_info(parent, 0, fs_root, path, &found_key); 12868f24b496SJan Schmidt if (ret > 0) 12878f24b496SJan Schmidt ret = -ENOENT; 1288a542ad1bSJan Schmidt if (ret) 1289a542ad1bSJan Schmidt break; 1290d24bec3aSMark Fasheh 1291a542ad1bSJan Schmidt next_inum = found_key.offset; 1292a542ad1bSJan Schmidt 1293a542ad1bSJan Schmidt /* regular exit ahead */ 1294a542ad1bSJan Schmidt if (parent == next_inum) 1295a542ad1bSJan Schmidt break; 1296a542ad1bSJan Schmidt 1297a542ad1bSJan Schmidt slot = path->slots[0]; 1298a542ad1bSJan Schmidt eb = path->nodes[0]; 1299a542ad1bSJan Schmidt /* make sure we can use eb after releasing the path */ 1300b916a59aSJan Schmidt if (eb != eb_in) { 1301a542ad1bSJan Schmidt atomic_inc(&eb->refs); 1302b916a59aSJan Schmidt btrfs_tree_read_lock(eb); 1303b916a59aSJan Schmidt btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK); 1304b916a59aSJan Schmidt } 1305a542ad1bSJan Schmidt btrfs_release_path(path); 1306a542ad1bSJan Schmidt iref = btrfs_item_ptr(eb, slot, struct btrfs_inode_ref); 1307d24bec3aSMark Fasheh 1308d24bec3aSMark Fasheh name_len = btrfs_inode_ref_name_len(eb, iref); 1309d24bec3aSMark Fasheh name_off = (unsigned long)(iref + 1); 1310d24bec3aSMark Fasheh 1311a542ad1bSJan Schmidt parent = next_inum; 1312a542ad1bSJan Schmidt --bytes_left; 1313a542ad1bSJan Schmidt if (bytes_left >= 0) 1314a542ad1bSJan Schmidt dest[bytes_left] = '/'; 1315a542ad1bSJan Schmidt } 1316a542ad1bSJan Schmidt 1317a542ad1bSJan Schmidt btrfs_release_path(path); 1318b916a59aSJan Schmidt path->leave_spinning = leave_spinning; 1319a542ad1bSJan Schmidt 1320a542ad1bSJan Schmidt if (ret) 1321a542ad1bSJan Schmidt return ERR_PTR(ret); 1322a542ad1bSJan Schmidt 1323a542ad1bSJan Schmidt return dest + bytes_left; 1324a542ad1bSJan Schmidt } 1325a542ad1bSJan Schmidt 1326a542ad1bSJan Schmidt /* 1327a542ad1bSJan Schmidt * this makes the path point to (logical EXTENT_ITEM *) 1328a542ad1bSJan Schmidt * returns BTRFS_EXTENT_FLAG_DATA for data, BTRFS_EXTENT_FLAG_TREE_BLOCK for 1329a542ad1bSJan Schmidt * tree blocks and <0 on error. 1330a542ad1bSJan Schmidt */ 1331a542ad1bSJan Schmidt int extent_from_logical(struct btrfs_fs_info *fs_info, u64 logical, 133269917e43SLiu Bo struct btrfs_path *path, struct btrfs_key *found_key, 133369917e43SLiu Bo u64 *flags_ret) 1334a542ad1bSJan Schmidt { 1335a542ad1bSJan Schmidt int ret; 1336a542ad1bSJan Schmidt u64 flags; 1337261c84b6SJosef Bacik u64 size = 0; 1338a542ad1bSJan Schmidt u32 item_size; 1339a542ad1bSJan Schmidt struct extent_buffer *eb; 1340a542ad1bSJan Schmidt struct btrfs_extent_item *ei; 1341a542ad1bSJan Schmidt struct btrfs_key key; 1342a542ad1bSJan Schmidt 1343261c84b6SJosef Bacik if (btrfs_fs_incompat(fs_info, SKINNY_METADATA)) 1344261c84b6SJosef Bacik key.type = BTRFS_METADATA_ITEM_KEY; 1345261c84b6SJosef Bacik else 1346a542ad1bSJan Schmidt key.type = BTRFS_EXTENT_ITEM_KEY; 1347a542ad1bSJan Schmidt key.objectid = logical; 1348a542ad1bSJan Schmidt key.offset = (u64)-1; 1349a542ad1bSJan Schmidt 1350a542ad1bSJan Schmidt ret = btrfs_search_slot(NULL, fs_info->extent_root, &key, path, 0, 0); 1351a542ad1bSJan Schmidt if (ret < 0) 1352a542ad1bSJan Schmidt return ret; 1353a542ad1bSJan Schmidt 1354850a8cdfSWang Shilong ret = btrfs_previous_extent_item(fs_info->extent_root, path, 0); 1355850a8cdfSWang Shilong if (ret) { 1356850a8cdfSWang Shilong if (ret > 0) 1357580f0a67SJosef Bacik ret = -ENOENT; 1358580f0a67SJosef Bacik return ret; 1359580f0a67SJosef Bacik } 1360850a8cdfSWang Shilong btrfs_item_key_to_cpu(path->nodes[0], found_key, path->slots[0]); 1361261c84b6SJosef Bacik if (found_key->type == BTRFS_METADATA_ITEM_KEY) 1362261c84b6SJosef Bacik size = fs_info->extent_root->leafsize; 1363261c84b6SJosef Bacik else if (found_key->type == BTRFS_EXTENT_ITEM_KEY) 1364261c84b6SJosef Bacik size = found_key->offset; 1365261c84b6SJosef Bacik 1366580f0a67SJosef Bacik if (found_key->objectid > logical || 1367261c84b6SJosef Bacik found_key->objectid + size <= logical) { 1368c1c9ff7cSGeert Uytterhoeven pr_debug("logical %llu is not within any extent\n", logical); 1369a542ad1bSJan Schmidt return -ENOENT; 13704692cf58SJan Schmidt } 1371a542ad1bSJan Schmidt 1372a542ad1bSJan Schmidt eb = path->nodes[0]; 1373a542ad1bSJan Schmidt item_size = btrfs_item_size_nr(eb, path->slots[0]); 1374a542ad1bSJan Schmidt BUG_ON(item_size < sizeof(*ei)); 1375a542ad1bSJan Schmidt 1376a542ad1bSJan Schmidt ei = btrfs_item_ptr(eb, path->slots[0], struct btrfs_extent_item); 1377a542ad1bSJan Schmidt flags = btrfs_extent_flags(eb, ei); 1378a542ad1bSJan Schmidt 13794692cf58SJan Schmidt pr_debug("logical %llu is at position %llu within the extent (%llu " 13804692cf58SJan Schmidt "EXTENT_ITEM %llu) flags %#llx size %u\n", 1381c1c9ff7cSGeert Uytterhoeven logical, logical - found_key->objectid, found_key->objectid, 1382c1c9ff7cSGeert Uytterhoeven found_key->offset, flags, item_size); 138369917e43SLiu Bo 138469917e43SLiu Bo WARN_ON(!flags_ret); 138569917e43SLiu Bo if (flags_ret) { 1386a542ad1bSJan Schmidt if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) 138769917e43SLiu Bo *flags_ret = BTRFS_EXTENT_FLAG_TREE_BLOCK; 138869917e43SLiu Bo else if (flags & BTRFS_EXTENT_FLAG_DATA) 138969917e43SLiu Bo *flags_ret = BTRFS_EXTENT_FLAG_DATA; 139069917e43SLiu Bo else 139169917e43SLiu Bo BUG_ON(1); 139269917e43SLiu Bo return 0; 139369917e43SLiu Bo } 1394a542ad1bSJan Schmidt 1395a542ad1bSJan Schmidt return -EIO; 1396a542ad1bSJan Schmidt } 1397a542ad1bSJan Schmidt 1398a542ad1bSJan Schmidt /* 1399a542ad1bSJan Schmidt * helper function to iterate extent inline refs. ptr must point to a 0 value 1400a542ad1bSJan Schmidt * for the first call and may be modified. it is used to track state. 1401a542ad1bSJan Schmidt * if more refs exist, 0 is returned and the next call to 1402a542ad1bSJan Schmidt * __get_extent_inline_ref must pass the modified ptr parameter to get the 1403a542ad1bSJan Schmidt * next ref. after the last ref was processed, 1 is returned. 1404a542ad1bSJan Schmidt * returns <0 on error 1405a542ad1bSJan Schmidt */ 1406a542ad1bSJan Schmidt static int __get_extent_inline_ref(unsigned long *ptr, struct extent_buffer *eb, 1407a542ad1bSJan Schmidt struct btrfs_extent_item *ei, u32 item_size, 1408a542ad1bSJan Schmidt struct btrfs_extent_inline_ref **out_eiref, 1409a542ad1bSJan Schmidt int *out_type) 1410a542ad1bSJan Schmidt { 1411a542ad1bSJan Schmidt unsigned long end; 1412a542ad1bSJan Schmidt u64 flags; 1413a542ad1bSJan Schmidt struct btrfs_tree_block_info *info; 1414a542ad1bSJan Schmidt 1415a542ad1bSJan Schmidt if (!*ptr) { 1416a542ad1bSJan Schmidt /* first call */ 1417a542ad1bSJan Schmidt flags = btrfs_extent_flags(eb, ei); 1418a542ad1bSJan Schmidt if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) { 1419a542ad1bSJan Schmidt info = (struct btrfs_tree_block_info *)(ei + 1); 1420a542ad1bSJan Schmidt *out_eiref = 1421a542ad1bSJan Schmidt (struct btrfs_extent_inline_ref *)(info + 1); 1422a542ad1bSJan Schmidt } else { 1423a542ad1bSJan Schmidt *out_eiref = (struct btrfs_extent_inline_ref *)(ei + 1); 1424a542ad1bSJan Schmidt } 1425a542ad1bSJan Schmidt *ptr = (unsigned long)*out_eiref; 1426a542ad1bSJan Schmidt if ((void *)*ptr >= (void *)ei + item_size) 1427a542ad1bSJan Schmidt return -ENOENT; 1428a542ad1bSJan Schmidt } 1429a542ad1bSJan Schmidt 1430a542ad1bSJan Schmidt end = (unsigned long)ei + item_size; 1431a542ad1bSJan Schmidt *out_eiref = (struct btrfs_extent_inline_ref *)*ptr; 1432a542ad1bSJan Schmidt *out_type = btrfs_extent_inline_ref_type(eb, *out_eiref); 1433a542ad1bSJan Schmidt 1434a542ad1bSJan Schmidt *ptr += btrfs_extent_inline_ref_size(*out_type); 1435a542ad1bSJan Schmidt WARN_ON(*ptr > end); 1436a542ad1bSJan Schmidt if (*ptr == end) 1437a542ad1bSJan Schmidt return 1; /* last */ 1438a542ad1bSJan Schmidt 1439a542ad1bSJan Schmidt return 0; 1440a542ad1bSJan Schmidt } 1441a542ad1bSJan Schmidt 1442a542ad1bSJan Schmidt /* 1443a542ad1bSJan Schmidt * reads the tree block backref for an extent. tree level and root are returned 1444a542ad1bSJan Schmidt * through out_level and out_root. ptr must point to a 0 value for the first 1445a542ad1bSJan Schmidt * call and may be modified (see __get_extent_inline_ref comment). 1446a542ad1bSJan Schmidt * returns 0 if data was provided, 1 if there was no more data to provide or 1447a542ad1bSJan Schmidt * <0 on error. 1448a542ad1bSJan Schmidt */ 1449a542ad1bSJan Schmidt int tree_backref_for_extent(unsigned long *ptr, struct extent_buffer *eb, 1450a542ad1bSJan Schmidt struct btrfs_extent_item *ei, u32 item_size, 1451a542ad1bSJan Schmidt u64 *out_root, u8 *out_level) 1452a542ad1bSJan Schmidt { 1453a542ad1bSJan Schmidt int ret; 1454a542ad1bSJan Schmidt int type; 1455a542ad1bSJan Schmidt struct btrfs_tree_block_info *info; 1456a542ad1bSJan Schmidt struct btrfs_extent_inline_ref *eiref; 1457a542ad1bSJan Schmidt 1458a542ad1bSJan Schmidt if (*ptr == (unsigned long)-1) 1459a542ad1bSJan Schmidt return 1; 1460a542ad1bSJan Schmidt 1461a542ad1bSJan Schmidt while (1) { 1462a542ad1bSJan Schmidt ret = __get_extent_inline_ref(ptr, eb, ei, item_size, 1463a542ad1bSJan Schmidt &eiref, &type); 1464a542ad1bSJan Schmidt if (ret < 0) 1465a542ad1bSJan Schmidt return ret; 1466a542ad1bSJan Schmidt 1467a542ad1bSJan Schmidt if (type == BTRFS_TREE_BLOCK_REF_KEY || 1468a542ad1bSJan Schmidt type == BTRFS_SHARED_BLOCK_REF_KEY) 1469a542ad1bSJan Schmidt break; 1470a542ad1bSJan Schmidt 1471a542ad1bSJan Schmidt if (ret == 1) 1472a542ad1bSJan Schmidt return 1; 1473a542ad1bSJan Schmidt } 1474a542ad1bSJan Schmidt 1475a542ad1bSJan Schmidt /* we can treat both ref types equally here */ 1476a542ad1bSJan Schmidt info = (struct btrfs_tree_block_info *)(ei + 1); 1477a542ad1bSJan Schmidt *out_root = btrfs_extent_inline_ref_offset(eb, eiref); 1478a542ad1bSJan Schmidt *out_level = btrfs_tree_block_level(eb, info); 1479a542ad1bSJan Schmidt 1480a542ad1bSJan Schmidt if (ret == 1) 1481a542ad1bSJan Schmidt *ptr = (unsigned long)-1; 1482a542ad1bSJan Schmidt 1483a542ad1bSJan Schmidt return 0; 1484a542ad1bSJan Schmidt } 1485a542ad1bSJan Schmidt 1486976b1908SJan Schmidt static int iterate_leaf_refs(struct extent_inode_elem *inode_list, 1487976b1908SJan Schmidt u64 root, u64 extent_item_objectid, 14884692cf58SJan Schmidt iterate_extent_inodes_t *iterate, void *ctx) 1489a542ad1bSJan Schmidt { 1490976b1908SJan Schmidt struct extent_inode_elem *eie; 14914692cf58SJan Schmidt int ret = 0; 1492a542ad1bSJan Schmidt 1493976b1908SJan Schmidt for (eie = inode_list; eie; eie = eie->next) { 14944692cf58SJan Schmidt pr_debug("ref for %llu resolved, key (%llu EXTEND_DATA %llu), " 1495976b1908SJan Schmidt "root %llu\n", extent_item_objectid, 1496976b1908SJan Schmidt eie->inum, eie->offset, root); 1497976b1908SJan Schmidt ret = iterate(eie->inum, eie->offset, root, ctx); 14984692cf58SJan Schmidt if (ret) { 1499976b1908SJan Schmidt pr_debug("stopping iteration for %llu due to ret=%d\n", 1500976b1908SJan Schmidt extent_item_objectid, ret); 1501a542ad1bSJan Schmidt break; 1502a542ad1bSJan Schmidt } 1503a542ad1bSJan Schmidt } 1504a542ad1bSJan Schmidt 1505a542ad1bSJan Schmidt return ret; 1506a542ad1bSJan Schmidt } 1507a542ad1bSJan Schmidt 1508a542ad1bSJan Schmidt /* 1509a542ad1bSJan Schmidt * calls iterate() for every inode that references the extent identified by 15104692cf58SJan Schmidt * the given parameters. 1511a542ad1bSJan Schmidt * when the iterator function returns a non-zero value, iteration stops. 1512a542ad1bSJan Schmidt */ 1513a542ad1bSJan Schmidt int iterate_extent_inodes(struct btrfs_fs_info *fs_info, 15144692cf58SJan Schmidt u64 extent_item_objectid, u64 extent_item_pos, 15157a3ae2f8SJan Schmidt int search_commit_root, 1516a542ad1bSJan Schmidt iterate_extent_inodes_t *iterate, void *ctx) 1517a542ad1bSJan Schmidt { 1518a542ad1bSJan Schmidt int ret; 1519da61d31aSJosef Bacik struct btrfs_trans_handle *trans = NULL; 15207a3ae2f8SJan Schmidt struct ulist *refs = NULL; 15217a3ae2f8SJan Schmidt struct ulist *roots = NULL; 15224692cf58SJan Schmidt struct ulist_node *ref_node = NULL; 15234692cf58SJan Schmidt struct ulist_node *root_node = NULL; 15248445f61cSJan Schmidt struct seq_list tree_mod_seq_elem = {}; 1525cd1b413cSJan Schmidt struct ulist_iterator ref_uiter; 1526cd1b413cSJan Schmidt struct ulist_iterator root_uiter; 1527a542ad1bSJan Schmidt 15284692cf58SJan Schmidt pr_debug("resolving all inodes for extent %llu\n", 15294692cf58SJan Schmidt extent_item_objectid); 15304692cf58SJan Schmidt 1531da61d31aSJosef Bacik if (!search_commit_root) { 15327a3ae2f8SJan Schmidt trans = btrfs_join_transaction(fs_info->extent_root); 15337a3ae2f8SJan Schmidt if (IS_ERR(trans)) 15347a3ae2f8SJan Schmidt return PTR_ERR(trans); 15358445f61cSJan Schmidt btrfs_get_tree_mod_seq(fs_info, &tree_mod_seq_elem); 1536*9e351cc8SJosef Bacik } else { 1537*9e351cc8SJosef Bacik down_read(&fs_info->commit_root_sem); 15387a3ae2f8SJan Schmidt } 15394692cf58SJan Schmidt 15404692cf58SJan Schmidt ret = btrfs_find_all_leafs(trans, fs_info, extent_item_objectid, 1541097b8a7cSJan Schmidt tree_mod_seq_elem.seq, &refs, 15428445f61cSJan Schmidt &extent_item_pos); 15434692cf58SJan Schmidt if (ret) 15444692cf58SJan Schmidt goto out; 15454692cf58SJan Schmidt 1546cd1b413cSJan Schmidt ULIST_ITER_INIT(&ref_uiter); 1547cd1b413cSJan Schmidt while (!ret && (ref_node = ulist_next(refs, &ref_uiter))) { 1548*9e351cc8SJosef Bacik ret = __btrfs_find_all_roots(trans, fs_info, ref_node->val, 15498445f61cSJan Schmidt tree_mod_seq_elem.seq, &roots); 15504692cf58SJan Schmidt if (ret) 1551a542ad1bSJan Schmidt break; 1552cd1b413cSJan Schmidt ULIST_ITER_INIT(&root_uiter); 1553cd1b413cSJan Schmidt while (!ret && (root_node = ulist_next(roots, &root_uiter))) { 1554976b1908SJan Schmidt pr_debug("root %llu references leaf %llu, data list " 155534d73f54SAlexander Block "%#llx\n", root_node->val, ref_node->val, 1556c1c9ff7cSGeert Uytterhoeven ref_node->aux); 1557995e01b7SJan Schmidt ret = iterate_leaf_refs((struct extent_inode_elem *) 1558995e01b7SJan Schmidt (uintptr_t)ref_node->aux, 1559995e01b7SJan Schmidt root_node->val, 1560995e01b7SJan Schmidt extent_item_objectid, 1561a542ad1bSJan Schmidt iterate, ctx); 15624692cf58SJan Schmidt } 1563976b1908SJan Schmidt ulist_free(roots); 1564a542ad1bSJan Schmidt } 1565a542ad1bSJan Schmidt 1566976b1908SJan Schmidt free_leaf_list(refs); 15674692cf58SJan Schmidt out: 15687a3ae2f8SJan Schmidt if (!search_commit_root) { 15698445f61cSJan Schmidt btrfs_put_tree_mod_seq(fs_info, &tree_mod_seq_elem); 15704692cf58SJan Schmidt btrfs_end_transaction(trans, fs_info->extent_root); 1571*9e351cc8SJosef Bacik } else { 1572*9e351cc8SJosef Bacik up_read(&fs_info->commit_root_sem); 15737a3ae2f8SJan Schmidt } 15747a3ae2f8SJan Schmidt 1575a542ad1bSJan Schmidt return ret; 1576a542ad1bSJan Schmidt } 1577a542ad1bSJan Schmidt 1578a542ad1bSJan Schmidt int iterate_inodes_from_logical(u64 logical, struct btrfs_fs_info *fs_info, 1579a542ad1bSJan Schmidt struct btrfs_path *path, 1580a542ad1bSJan Schmidt iterate_extent_inodes_t *iterate, void *ctx) 1581a542ad1bSJan Schmidt { 1582a542ad1bSJan Schmidt int ret; 15834692cf58SJan Schmidt u64 extent_item_pos; 158469917e43SLiu Bo u64 flags = 0; 1585a542ad1bSJan Schmidt struct btrfs_key found_key; 15867a3ae2f8SJan Schmidt int search_commit_root = path->search_commit_root; 1587a542ad1bSJan Schmidt 158869917e43SLiu Bo ret = extent_from_logical(fs_info, logical, path, &found_key, &flags); 15894692cf58SJan Schmidt btrfs_release_path(path); 1590a542ad1bSJan Schmidt if (ret < 0) 1591a542ad1bSJan Schmidt return ret; 159269917e43SLiu Bo if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) 15933627bf45SStefan Behrens return -EINVAL; 1594a542ad1bSJan Schmidt 15954692cf58SJan Schmidt extent_item_pos = logical - found_key.objectid; 15967a3ae2f8SJan Schmidt ret = iterate_extent_inodes(fs_info, found_key.objectid, 15977a3ae2f8SJan Schmidt extent_item_pos, search_commit_root, 15987a3ae2f8SJan Schmidt iterate, ctx); 1599a542ad1bSJan Schmidt 1600a542ad1bSJan Schmidt return ret; 1601a542ad1bSJan Schmidt } 1602a542ad1bSJan Schmidt 1603d24bec3aSMark Fasheh typedef int (iterate_irefs_t)(u64 parent, u32 name_len, unsigned long name_off, 1604d24bec3aSMark Fasheh struct extent_buffer *eb, void *ctx); 1605d24bec3aSMark Fasheh 1606d24bec3aSMark Fasheh static int iterate_inode_refs(u64 inum, struct btrfs_root *fs_root, 1607a542ad1bSJan Schmidt struct btrfs_path *path, 1608a542ad1bSJan Schmidt iterate_irefs_t *iterate, void *ctx) 1609a542ad1bSJan Schmidt { 1610aefc1eb1SJan Schmidt int ret = 0; 1611a542ad1bSJan Schmidt int slot; 1612a542ad1bSJan Schmidt u32 cur; 1613a542ad1bSJan Schmidt u32 len; 1614a542ad1bSJan Schmidt u32 name_len; 1615a542ad1bSJan Schmidt u64 parent = 0; 1616a542ad1bSJan Schmidt int found = 0; 1617a542ad1bSJan Schmidt struct extent_buffer *eb; 1618a542ad1bSJan Schmidt struct btrfs_item *item; 1619a542ad1bSJan Schmidt struct btrfs_inode_ref *iref; 1620a542ad1bSJan Schmidt struct btrfs_key found_key; 1621a542ad1bSJan Schmidt 1622aefc1eb1SJan Schmidt while (!ret) { 1623a542ad1bSJan Schmidt ret = inode_ref_info(inum, parent ? parent+1 : 0, fs_root, path, 1624a542ad1bSJan Schmidt &found_key); 1625a542ad1bSJan Schmidt if (ret < 0) 1626a542ad1bSJan Schmidt break; 1627a542ad1bSJan Schmidt if (ret) { 1628a542ad1bSJan Schmidt ret = found ? 0 : -ENOENT; 1629a542ad1bSJan Schmidt break; 1630a542ad1bSJan Schmidt } 1631a542ad1bSJan Schmidt ++found; 1632a542ad1bSJan Schmidt 1633a542ad1bSJan Schmidt parent = found_key.offset; 1634a542ad1bSJan Schmidt slot = path->slots[0]; 16353fe81ce2SFilipe David Borba Manana eb = btrfs_clone_extent_buffer(path->nodes[0]); 16363fe81ce2SFilipe David Borba Manana if (!eb) { 16373fe81ce2SFilipe David Borba Manana ret = -ENOMEM; 16383fe81ce2SFilipe David Borba Manana break; 16393fe81ce2SFilipe David Borba Manana } 16403fe81ce2SFilipe David Borba Manana extent_buffer_get(eb); 1641b916a59aSJan Schmidt btrfs_tree_read_lock(eb); 1642b916a59aSJan Schmidt btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK); 1643a542ad1bSJan Schmidt btrfs_release_path(path); 1644a542ad1bSJan Schmidt 1645dd3cc16bSRoss Kirk item = btrfs_item_nr(slot); 1646a542ad1bSJan Schmidt iref = btrfs_item_ptr(eb, slot, struct btrfs_inode_ref); 1647a542ad1bSJan Schmidt 1648a542ad1bSJan Schmidt for (cur = 0; cur < btrfs_item_size(eb, item); cur += len) { 1649a542ad1bSJan Schmidt name_len = btrfs_inode_ref_name_len(eb, iref); 1650a542ad1bSJan Schmidt /* path must be released before calling iterate()! */ 16514692cf58SJan Schmidt pr_debug("following ref at offset %u for inode %llu in " 1652c1c9ff7cSGeert Uytterhoeven "tree %llu\n", cur, found_key.objectid, 1653c1c9ff7cSGeert Uytterhoeven fs_root->objectid); 1654d24bec3aSMark Fasheh ret = iterate(parent, name_len, 1655d24bec3aSMark Fasheh (unsigned long)(iref + 1), eb, ctx); 1656aefc1eb1SJan Schmidt if (ret) 1657a542ad1bSJan Schmidt break; 1658a542ad1bSJan Schmidt len = sizeof(*iref) + name_len; 1659a542ad1bSJan Schmidt iref = (struct btrfs_inode_ref *)((char *)iref + len); 1660a542ad1bSJan Schmidt } 1661b916a59aSJan Schmidt btrfs_tree_read_unlock_blocking(eb); 1662a542ad1bSJan Schmidt free_extent_buffer(eb); 1663a542ad1bSJan Schmidt } 1664a542ad1bSJan Schmidt 1665a542ad1bSJan Schmidt btrfs_release_path(path); 1666a542ad1bSJan Schmidt 1667a542ad1bSJan Schmidt return ret; 1668a542ad1bSJan Schmidt } 1669a542ad1bSJan Schmidt 1670d24bec3aSMark Fasheh static int iterate_inode_extrefs(u64 inum, struct btrfs_root *fs_root, 1671d24bec3aSMark Fasheh struct btrfs_path *path, 1672d24bec3aSMark Fasheh iterate_irefs_t *iterate, void *ctx) 1673d24bec3aSMark Fasheh { 1674d24bec3aSMark Fasheh int ret; 1675d24bec3aSMark Fasheh int slot; 1676d24bec3aSMark Fasheh u64 offset = 0; 1677d24bec3aSMark Fasheh u64 parent; 1678d24bec3aSMark Fasheh int found = 0; 1679d24bec3aSMark Fasheh struct extent_buffer *eb; 1680d24bec3aSMark Fasheh struct btrfs_inode_extref *extref; 1681d24bec3aSMark Fasheh struct extent_buffer *leaf; 1682d24bec3aSMark Fasheh u32 item_size; 1683d24bec3aSMark Fasheh u32 cur_offset; 1684d24bec3aSMark Fasheh unsigned long ptr; 1685d24bec3aSMark Fasheh 1686d24bec3aSMark Fasheh while (1) { 1687d24bec3aSMark Fasheh ret = btrfs_find_one_extref(fs_root, inum, offset, path, &extref, 1688d24bec3aSMark Fasheh &offset); 1689d24bec3aSMark Fasheh if (ret < 0) 1690d24bec3aSMark Fasheh break; 1691d24bec3aSMark Fasheh if (ret) { 1692d24bec3aSMark Fasheh ret = found ? 0 : -ENOENT; 1693d24bec3aSMark Fasheh break; 1694d24bec3aSMark Fasheh } 1695d24bec3aSMark Fasheh ++found; 1696d24bec3aSMark Fasheh 1697d24bec3aSMark Fasheh slot = path->slots[0]; 16983fe81ce2SFilipe David Borba Manana eb = btrfs_clone_extent_buffer(path->nodes[0]); 16993fe81ce2SFilipe David Borba Manana if (!eb) { 17003fe81ce2SFilipe David Borba Manana ret = -ENOMEM; 17013fe81ce2SFilipe David Borba Manana break; 17023fe81ce2SFilipe David Borba Manana } 17033fe81ce2SFilipe David Borba Manana extent_buffer_get(eb); 1704d24bec3aSMark Fasheh 1705d24bec3aSMark Fasheh btrfs_tree_read_lock(eb); 1706d24bec3aSMark Fasheh btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK); 1707d24bec3aSMark Fasheh btrfs_release_path(path); 1708d24bec3aSMark Fasheh 1709d24bec3aSMark Fasheh leaf = path->nodes[0]; 1710e94acd86SValentina Giusti item_size = btrfs_item_size_nr(leaf, slot); 1711e94acd86SValentina Giusti ptr = btrfs_item_ptr_offset(leaf, slot); 1712d24bec3aSMark Fasheh cur_offset = 0; 1713d24bec3aSMark Fasheh 1714d24bec3aSMark Fasheh while (cur_offset < item_size) { 1715d24bec3aSMark Fasheh u32 name_len; 1716d24bec3aSMark Fasheh 1717d24bec3aSMark Fasheh extref = (struct btrfs_inode_extref *)(ptr + cur_offset); 1718d24bec3aSMark Fasheh parent = btrfs_inode_extref_parent(eb, extref); 1719d24bec3aSMark Fasheh name_len = btrfs_inode_extref_name_len(eb, extref); 1720d24bec3aSMark Fasheh ret = iterate(parent, name_len, 1721d24bec3aSMark Fasheh (unsigned long)&extref->name, eb, ctx); 1722d24bec3aSMark Fasheh if (ret) 1723d24bec3aSMark Fasheh break; 1724d24bec3aSMark Fasheh 1725d24bec3aSMark Fasheh cur_offset += btrfs_inode_extref_name_len(leaf, extref); 1726d24bec3aSMark Fasheh cur_offset += sizeof(*extref); 1727d24bec3aSMark Fasheh } 1728d24bec3aSMark Fasheh btrfs_tree_read_unlock_blocking(eb); 1729d24bec3aSMark Fasheh free_extent_buffer(eb); 1730d24bec3aSMark Fasheh 1731d24bec3aSMark Fasheh offset++; 1732d24bec3aSMark Fasheh } 1733d24bec3aSMark Fasheh 1734d24bec3aSMark Fasheh btrfs_release_path(path); 1735d24bec3aSMark Fasheh 1736d24bec3aSMark Fasheh return ret; 1737d24bec3aSMark Fasheh } 1738d24bec3aSMark Fasheh 1739d24bec3aSMark Fasheh static int iterate_irefs(u64 inum, struct btrfs_root *fs_root, 1740d24bec3aSMark Fasheh struct btrfs_path *path, iterate_irefs_t *iterate, 1741d24bec3aSMark Fasheh void *ctx) 1742d24bec3aSMark Fasheh { 1743d24bec3aSMark Fasheh int ret; 1744d24bec3aSMark Fasheh int found_refs = 0; 1745d24bec3aSMark Fasheh 1746d24bec3aSMark Fasheh ret = iterate_inode_refs(inum, fs_root, path, iterate, ctx); 1747d24bec3aSMark Fasheh if (!ret) 1748d24bec3aSMark Fasheh ++found_refs; 1749d24bec3aSMark Fasheh else if (ret != -ENOENT) 1750d24bec3aSMark Fasheh return ret; 1751d24bec3aSMark Fasheh 1752d24bec3aSMark Fasheh ret = iterate_inode_extrefs(inum, fs_root, path, iterate, ctx); 1753d24bec3aSMark Fasheh if (ret == -ENOENT && found_refs) 1754d24bec3aSMark Fasheh return 0; 1755d24bec3aSMark Fasheh 1756d24bec3aSMark Fasheh return ret; 1757d24bec3aSMark Fasheh } 1758d24bec3aSMark Fasheh 1759a542ad1bSJan Schmidt /* 1760a542ad1bSJan Schmidt * returns 0 if the path could be dumped (probably truncated) 1761a542ad1bSJan Schmidt * returns <0 in case of an error 1762a542ad1bSJan Schmidt */ 1763d24bec3aSMark Fasheh static int inode_to_path(u64 inum, u32 name_len, unsigned long name_off, 1764a542ad1bSJan Schmidt struct extent_buffer *eb, void *ctx) 1765a542ad1bSJan Schmidt { 1766a542ad1bSJan Schmidt struct inode_fs_paths *ipath = ctx; 1767a542ad1bSJan Schmidt char *fspath; 1768a542ad1bSJan Schmidt char *fspath_min; 1769a542ad1bSJan Schmidt int i = ipath->fspath->elem_cnt; 1770a542ad1bSJan Schmidt const int s_ptr = sizeof(char *); 1771a542ad1bSJan Schmidt u32 bytes_left; 1772a542ad1bSJan Schmidt 1773a542ad1bSJan Schmidt bytes_left = ipath->fspath->bytes_left > s_ptr ? 1774a542ad1bSJan Schmidt ipath->fspath->bytes_left - s_ptr : 0; 1775a542ad1bSJan Schmidt 1776740c3d22SChris Mason fspath_min = (char *)ipath->fspath->val + (i + 1) * s_ptr; 177796b5bd77SJan Schmidt fspath = btrfs_ref_to_path(ipath->fs_root, ipath->btrfs_path, name_len, 177896b5bd77SJan Schmidt name_off, eb, inum, fspath_min, bytes_left); 1779a542ad1bSJan Schmidt if (IS_ERR(fspath)) 1780a542ad1bSJan Schmidt return PTR_ERR(fspath); 1781a542ad1bSJan Schmidt 1782a542ad1bSJan Schmidt if (fspath > fspath_min) { 1783745c4d8eSJeff Mahoney ipath->fspath->val[i] = (u64)(unsigned long)fspath; 1784a542ad1bSJan Schmidt ++ipath->fspath->elem_cnt; 1785a542ad1bSJan Schmidt ipath->fspath->bytes_left = fspath - fspath_min; 1786a542ad1bSJan Schmidt } else { 1787a542ad1bSJan Schmidt ++ipath->fspath->elem_missed; 1788a542ad1bSJan Schmidt ipath->fspath->bytes_missing += fspath_min - fspath; 1789a542ad1bSJan Schmidt ipath->fspath->bytes_left = 0; 1790a542ad1bSJan Schmidt } 1791a542ad1bSJan Schmidt 1792a542ad1bSJan Schmidt return 0; 1793a542ad1bSJan Schmidt } 1794a542ad1bSJan Schmidt 1795a542ad1bSJan Schmidt /* 1796a542ad1bSJan Schmidt * this dumps all file system paths to the inode into the ipath struct, provided 1797a542ad1bSJan Schmidt * is has been created large enough. each path is zero-terminated and accessed 1798740c3d22SChris Mason * from ipath->fspath->val[i]. 1799a542ad1bSJan Schmidt * when it returns, there are ipath->fspath->elem_cnt number of paths available 1800740c3d22SChris Mason * in ipath->fspath->val[]. when the allocated space wasn't sufficient, the 1801a542ad1bSJan Schmidt * number of missed paths in recored in ipath->fspath->elem_missed, otherwise, 1802a542ad1bSJan Schmidt * it's zero. ipath->fspath->bytes_missing holds the number of bytes that would 1803a542ad1bSJan Schmidt * have been needed to return all paths. 1804a542ad1bSJan Schmidt */ 1805a542ad1bSJan Schmidt int paths_from_inode(u64 inum, struct inode_fs_paths *ipath) 1806a542ad1bSJan Schmidt { 1807a542ad1bSJan Schmidt return iterate_irefs(inum, ipath->fs_root, ipath->btrfs_path, 1808a542ad1bSJan Schmidt inode_to_path, ipath); 1809a542ad1bSJan Schmidt } 1810a542ad1bSJan Schmidt 1811a542ad1bSJan Schmidt struct btrfs_data_container *init_data_container(u32 total_bytes) 1812a542ad1bSJan Schmidt { 1813a542ad1bSJan Schmidt struct btrfs_data_container *data; 1814a542ad1bSJan Schmidt size_t alloc_bytes; 1815a542ad1bSJan Schmidt 1816a542ad1bSJan Schmidt alloc_bytes = max_t(size_t, total_bytes, sizeof(*data)); 1817425d17a2SLiu Bo data = vmalloc(alloc_bytes); 1818a542ad1bSJan Schmidt if (!data) 1819a542ad1bSJan Schmidt return ERR_PTR(-ENOMEM); 1820a542ad1bSJan Schmidt 1821a542ad1bSJan Schmidt if (total_bytes >= sizeof(*data)) { 1822a542ad1bSJan Schmidt data->bytes_left = total_bytes - sizeof(*data); 1823a542ad1bSJan Schmidt data->bytes_missing = 0; 1824a542ad1bSJan Schmidt } else { 1825a542ad1bSJan Schmidt data->bytes_missing = sizeof(*data) - total_bytes; 1826a542ad1bSJan Schmidt data->bytes_left = 0; 1827a542ad1bSJan Schmidt } 1828a542ad1bSJan Schmidt 1829a542ad1bSJan Schmidt data->elem_cnt = 0; 1830a542ad1bSJan Schmidt data->elem_missed = 0; 1831a542ad1bSJan Schmidt 1832a542ad1bSJan Schmidt return data; 1833a542ad1bSJan Schmidt } 1834a542ad1bSJan Schmidt 1835a542ad1bSJan Schmidt /* 1836a542ad1bSJan Schmidt * allocates space to return multiple file system paths for an inode. 1837a542ad1bSJan Schmidt * total_bytes to allocate are passed, note that space usable for actual path 1838a542ad1bSJan Schmidt * information will be total_bytes - sizeof(struct inode_fs_paths). 1839a542ad1bSJan Schmidt * the returned pointer must be freed with free_ipath() in the end. 1840a542ad1bSJan Schmidt */ 1841a542ad1bSJan Schmidt struct inode_fs_paths *init_ipath(s32 total_bytes, struct btrfs_root *fs_root, 1842a542ad1bSJan Schmidt struct btrfs_path *path) 1843a542ad1bSJan Schmidt { 1844a542ad1bSJan Schmidt struct inode_fs_paths *ifp; 1845a542ad1bSJan Schmidt struct btrfs_data_container *fspath; 1846a542ad1bSJan Schmidt 1847a542ad1bSJan Schmidt fspath = init_data_container(total_bytes); 1848a542ad1bSJan Schmidt if (IS_ERR(fspath)) 1849a542ad1bSJan Schmidt return (void *)fspath; 1850a542ad1bSJan Schmidt 1851a542ad1bSJan Schmidt ifp = kmalloc(sizeof(*ifp), GFP_NOFS); 1852a542ad1bSJan Schmidt if (!ifp) { 1853a542ad1bSJan Schmidt kfree(fspath); 1854a542ad1bSJan Schmidt return ERR_PTR(-ENOMEM); 1855a542ad1bSJan Schmidt } 1856a542ad1bSJan Schmidt 1857a542ad1bSJan Schmidt ifp->btrfs_path = path; 1858a542ad1bSJan Schmidt ifp->fspath = fspath; 1859a542ad1bSJan Schmidt ifp->fs_root = fs_root; 1860a542ad1bSJan Schmidt 1861a542ad1bSJan Schmidt return ifp; 1862a542ad1bSJan Schmidt } 1863a542ad1bSJan Schmidt 1864a542ad1bSJan Schmidt void free_ipath(struct inode_fs_paths *ipath) 1865a542ad1bSJan Schmidt { 18664735fb28SJesper Juhl if (!ipath) 18674735fb28SJesper Juhl return; 1868425d17a2SLiu Bo vfree(ipath->fspath); 1869a542ad1bSJan Schmidt kfree(ipath); 1870a542ad1bSJan Schmidt } 1871