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 19f54de068SDavid Sterba #include <linux/mm.h> 20afce772eSLu Fengqi #include <linux/rbtree.h> 21a542ad1bSJan Schmidt #include "ctree.h" 22a542ad1bSJan Schmidt #include "disk-io.h" 23a542ad1bSJan Schmidt #include "backref.h" 248da6d581SJan Schmidt #include "ulist.h" 258da6d581SJan Schmidt #include "transaction.h" 268da6d581SJan Schmidt #include "delayed-ref.h" 27b916a59aSJan Schmidt #include "locking.h" 28a542ad1bSJan Schmidt 29dc046b10SJosef Bacik /* Just an arbitrary number so we can be sure this happened */ 30dc046b10SJosef Bacik #define BACKREF_FOUND_SHARED 6 31dc046b10SJosef Bacik 32976b1908SJan Schmidt struct extent_inode_elem { 33976b1908SJan Schmidt u64 inum; 34976b1908SJan Schmidt u64 offset; 35976b1908SJan Schmidt struct extent_inode_elem *next; 36976b1908SJan Schmidt }; 37976b1908SJan Schmidt 3873980becSJeff Mahoney static int check_extent_in_eb(const struct btrfs_key *key, 3973980becSJeff Mahoney const struct extent_buffer *eb, 4073980becSJeff Mahoney const struct btrfs_file_extent_item *fi, 41976b1908SJan Schmidt u64 extent_item_pos, 42976b1908SJan Schmidt struct extent_inode_elem **eie) 43976b1908SJan Schmidt { 448ca15e05SJosef Bacik u64 offset = 0; 458ca15e05SJosef Bacik struct extent_inode_elem *e; 468ca15e05SJosef Bacik 478ca15e05SJosef Bacik if (!btrfs_file_extent_compression(eb, fi) && 488ca15e05SJosef Bacik !btrfs_file_extent_encryption(eb, fi) && 498ca15e05SJosef Bacik !btrfs_file_extent_other_encoding(eb, fi)) { 50976b1908SJan Schmidt u64 data_offset; 51976b1908SJan Schmidt u64 data_len; 52976b1908SJan Schmidt 53976b1908SJan Schmidt data_offset = btrfs_file_extent_offset(eb, fi); 54976b1908SJan Schmidt data_len = btrfs_file_extent_num_bytes(eb, fi); 55976b1908SJan Schmidt 56976b1908SJan Schmidt if (extent_item_pos < data_offset || 57976b1908SJan Schmidt extent_item_pos >= data_offset + data_len) 58976b1908SJan Schmidt return 1; 598ca15e05SJosef Bacik offset = extent_item_pos - data_offset; 608ca15e05SJosef Bacik } 61976b1908SJan Schmidt 62976b1908SJan Schmidt e = kmalloc(sizeof(*e), GFP_NOFS); 63976b1908SJan Schmidt if (!e) 64976b1908SJan Schmidt return -ENOMEM; 65976b1908SJan Schmidt 66976b1908SJan Schmidt e->next = *eie; 67976b1908SJan Schmidt e->inum = key->objectid; 688ca15e05SJosef Bacik e->offset = key->offset + offset; 69976b1908SJan Schmidt *eie = e; 70976b1908SJan Schmidt 71976b1908SJan Schmidt return 0; 72976b1908SJan Schmidt } 73976b1908SJan Schmidt 74f05c4746SWang Shilong static void free_inode_elem_list(struct extent_inode_elem *eie) 75f05c4746SWang Shilong { 76f05c4746SWang Shilong struct extent_inode_elem *eie_next; 77f05c4746SWang Shilong 78f05c4746SWang Shilong for (; eie; eie = eie_next) { 79f05c4746SWang Shilong eie_next = eie->next; 80f05c4746SWang Shilong kfree(eie); 81f05c4746SWang Shilong } 82f05c4746SWang Shilong } 83f05c4746SWang Shilong 8473980becSJeff Mahoney static int find_extent_in_eb(const struct extent_buffer *eb, 8573980becSJeff Mahoney u64 wanted_disk_byte, u64 extent_item_pos, 86976b1908SJan Schmidt struct extent_inode_elem **eie) 87976b1908SJan Schmidt { 88976b1908SJan Schmidt u64 disk_byte; 89976b1908SJan Schmidt struct btrfs_key key; 90976b1908SJan Schmidt struct btrfs_file_extent_item *fi; 91976b1908SJan Schmidt int slot; 92976b1908SJan Schmidt int nritems; 93976b1908SJan Schmidt int extent_type; 94976b1908SJan Schmidt int ret; 95976b1908SJan Schmidt 96976b1908SJan Schmidt /* 97976b1908SJan Schmidt * from the shared data ref, we only have the leaf but we need 98976b1908SJan Schmidt * the key. thus, we must look into all items and see that we 99976b1908SJan Schmidt * find one (some) with a reference to our extent item. 100976b1908SJan Schmidt */ 101976b1908SJan Schmidt nritems = btrfs_header_nritems(eb); 102976b1908SJan Schmidt for (slot = 0; slot < nritems; ++slot) { 103976b1908SJan Schmidt btrfs_item_key_to_cpu(eb, &key, slot); 104976b1908SJan Schmidt if (key.type != BTRFS_EXTENT_DATA_KEY) 105976b1908SJan Schmidt continue; 106976b1908SJan Schmidt fi = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item); 107976b1908SJan Schmidt extent_type = btrfs_file_extent_type(eb, fi); 108976b1908SJan Schmidt if (extent_type == BTRFS_FILE_EXTENT_INLINE) 109976b1908SJan Schmidt continue; 110976b1908SJan Schmidt /* don't skip BTRFS_FILE_EXTENT_PREALLOC, we can handle that */ 111976b1908SJan Schmidt disk_byte = btrfs_file_extent_disk_bytenr(eb, fi); 112976b1908SJan Schmidt if (disk_byte != wanted_disk_byte) 113976b1908SJan Schmidt continue; 114976b1908SJan Schmidt 115976b1908SJan Schmidt ret = check_extent_in_eb(&key, eb, fi, extent_item_pos, eie); 116976b1908SJan Schmidt if (ret < 0) 117976b1908SJan Schmidt return ret; 118976b1908SJan Schmidt } 119976b1908SJan Schmidt 120976b1908SJan Schmidt return 0; 121976b1908SJan Schmidt } 122976b1908SJan Schmidt 1238da6d581SJan Schmidt /* 1248da6d581SJan Schmidt * this structure records all encountered refs on the way up to the root 1258da6d581SJan Schmidt */ 126e0c476b1SJeff Mahoney struct prelim_ref { 12786d5f994SEdmund Nadolski struct rb_node rbnode; 1288da6d581SJan Schmidt u64 root_id; 129d5c88b73SJan Schmidt struct btrfs_key key_for_search; 1308da6d581SJan Schmidt int level; 1318da6d581SJan Schmidt int count; 1323301958bSJan Schmidt struct extent_inode_elem *inode_list; 1338da6d581SJan Schmidt u64 parent; 1348da6d581SJan Schmidt u64 wanted_disk_byte; 1358da6d581SJan Schmidt }; 1368da6d581SJan Schmidt 13786d5f994SEdmund Nadolski struct preftree { 13886d5f994SEdmund Nadolski struct rb_root root; 139*6c336b21SJeff Mahoney unsigned int count; 14086d5f994SEdmund Nadolski }; 14186d5f994SEdmund Nadolski 142*6c336b21SJeff Mahoney #define PREFTREE_INIT { .root = RB_ROOT, .count = 0 } 14386d5f994SEdmund Nadolski 14486d5f994SEdmund Nadolski struct preftrees { 14586d5f994SEdmund Nadolski struct preftree direct; /* BTRFS_SHARED_[DATA|BLOCK]_REF_KEY */ 14686d5f994SEdmund Nadolski struct preftree indirect; /* BTRFS_[TREE_BLOCK|EXTENT_DATA]_REF_KEY */ 14786d5f994SEdmund Nadolski struct preftree indirect_missing_keys; 14886d5f994SEdmund Nadolski }; 14986d5f994SEdmund Nadolski 150b9e9a6cbSWang Shilong static struct kmem_cache *btrfs_prelim_ref_cache; 151b9e9a6cbSWang Shilong 152b9e9a6cbSWang Shilong int __init btrfs_prelim_ref_init(void) 153b9e9a6cbSWang Shilong { 154b9e9a6cbSWang Shilong btrfs_prelim_ref_cache = kmem_cache_create("btrfs_prelim_ref", 155e0c476b1SJeff Mahoney sizeof(struct prelim_ref), 156b9e9a6cbSWang Shilong 0, 157fba4b697SNikolay Borisov SLAB_MEM_SPREAD, 158b9e9a6cbSWang Shilong NULL); 159b9e9a6cbSWang Shilong if (!btrfs_prelim_ref_cache) 160b9e9a6cbSWang Shilong return -ENOMEM; 161b9e9a6cbSWang Shilong return 0; 162b9e9a6cbSWang Shilong } 163b9e9a6cbSWang Shilong 164b9e9a6cbSWang Shilong void btrfs_prelim_ref_exit(void) 165b9e9a6cbSWang Shilong { 166b9e9a6cbSWang Shilong kmem_cache_destroy(btrfs_prelim_ref_cache); 167b9e9a6cbSWang Shilong } 168b9e9a6cbSWang Shilong 16986d5f994SEdmund Nadolski static void free_pref(struct prelim_ref *ref) 17086d5f994SEdmund Nadolski { 17186d5f994SEdmund Nadolski kmem_cache_free(btrfs_prelim_ref_cache, ref); 17286d5f994SEdmund Nadolski } 17386d5f994SEdmund Nadolski 17486d5f994SEdmund Nadolski /* 17586d5f994SEdmund Nadolski * Return 0 when both refs are for the same block (and can be merged). 17686d5f994SEdmund Nadolski * A -1 return indicates ref1 is a 'lower' block than ref2, while 1 17786d5f994SEdmund Nadolski * indicates a 'higher' block. 17886d5f994SEdmund Nadolski */ 17986d5f994SEdmund Nadolski static int prelim_ref_compare(struct prelim_ref *ref1, 18086d5f994SEdmund Nadolski struct prelim_ref *ref2) 18186d5f994SEdmund Nadolski { 18286d5f994SEdmund Nadolski if (ref1->level < ref2->level) 18386d5f994SEdmund Nadolski return -1; 18486d5f994SEdmund Nadolski if (ref1->level > ref2->level) 18586d5f994SEdmund Nadolski return 1; 18686d5f994SEdmund Nadolski if (ref1->root_id < ref2->root_id) 18786d5f994SEdmund Nadolski return -1; 18886d5f994SEdmund Nadolski if (ref1->root_id > ref2->root_id) 18986d5f994SEdmund Nadolski return 1; 19086d5f994SEdmund Nadolski if (ref1->key_for_search.type < ref2->key_for_search.type) 19186d5f994SEdmund Nadolski return -1; 19286d5f994SEdmund Nadolski if (ref1->key_for_search.type > ref2->key_for_search.type) 19386d5f994SEdmund Nadolski return 1; 19486d5f994SEdmund Nadolski if (ref1->key_for_search.objectid < ref2->key_for_search.objectid) 19586d5f994SEdmund Nadolski return -1; 19686d5f994SEdmund Nadolski if (ref1->key_for_search.objectid > ref2->key_for_search.objectid) 19786d5f994SEdmund Nadolski return 1; 19886d5f994SEdmund Nadolski if (ref1->key_for_search.offset < ref2->key_for_search.offset) 19986d5f994SEdmund Nadolski return -1; 20086d5f994SEdmund Nadolski if (ref1->key_for_search.offset > ref2->key_for_search.offset) 20186d5f994SEdmund Nadolski return 1; 20286d5f994SEdmund Nadolski if (ref1->parent < ref2->parent) 20386d5f994SEdmund Nadolski return -1; 20486d5f994SEdmund Nadolski if (ref1->parent > ref2->parent) 20586d5f994SEdmund Nadolski return 1; 20686d5f994SEdmund Nadolski 20786d5f994SEdmund Nadolski return 0; 20886d5f994SEdmund Nadolski } 20986d5f994SEdmund Nadolski 21086d5f994SEdmund Nadolski /* 21186d5f994SEdmund Nadolski * Add @newref to the @root rbtree, merging identical refs. 21286d5f994SEdmund Nadolski * 21386d5f994SEdmund Nadolski * Callers should assumed that newref has been freed after calling. 21486d5f994SEdmund Nadolski */ 21586d5f994SEdmund Nadolski static void prelim_ref_insert(struct preftree *preftree, 21686d5f994SEdmund Nadolski struct prelim_ref *newref) 21786d5f994SEdmund Nadolski { 21886d5f994SEdmund Nadolski struct rb_root *root; 21986d5f994SEdmund Nadolski struct rb_node **p; 22086d5f994SEdmund Nadolski struct rb_node *parent = NULL; 22186d5f994SEdmund Nadolski struct prelim_ref *ref; 22286d5f994SEdmund Nadolski int result; 22386d5f994SEdmund Nadolski 22486d5f994SEdmund Nadolski root = &preftree->root; 22586d5f994SEdmund Nadolski p = &root->rb_node; 22686d5f994SEdmund Nadolski 22786d5f994SEdmund Nadolski while (*p) { 22886d5f994SEdmund Nadolski parent = *p; 22986d5f994SEdmund Nadolski ref = rb_entry(parent, struct prelim_ref, rbnode); 23086d5f994SEdmund Nadolski result = prelim_ref_compare(ref, newref); 23186d5f994SEdmund Nadolski if (result < 0) { 23286d5f994SEdmund Nadolski p = &(*p)->rb_left; 23386d5f994SEdmund Nadolski } else if (result > 0) { 23486d5f994SEdmund Nadolski p = &(*p)->rb_right; 23586d5f994SEdmund Nadolski } else { 23686d5f994SEdmund Nadolski /* Identical refs, merge them and free @newref */ 23786d5f994SEdmund Nadolski struct extent_inode_elem *eie = ref->inode_list; 23886d5f994SEdmund Nadolski 23986d5f994SEdmund Nadolski while (eie && eie->next) 24086d5f994SEdmund Nadolski eie = eie->next; 24186d5f994SEdmund Nadolski 24286d5f994SEdmund Nadolski if (!eie) 24386d5f994SEdmund Nadolski ref->inode_list = newref->inode_list; 24486d5f994SEdmund Nadolski else 24586d5f994SEdmund Nadolski eie->next = newref->inode_list; 24686d5f994SEdmund Nadolski ref->count += newref->count; 24786d5f994SEdmund Nadolski free_pref(newref); 24886d5f994SEdmund Nadolski return; 24986d5f994SEdmund Nadolski } 25086d5f994SEdmund Nadolski } 25186d5f994SEdmund Nadolski 252*6c336b21SJeff Mahoney preftree->count++; 25386d5f994SEdmund Nadolski rb_link_node(&newref->rbnode, parent, p); 25486d5f994SEdmund Nadolski rb_insert_color(&newref->rbnode, root); 25586d5f994SEdmund Nadolski } 25686d5f994SEdmund Nadolski 25786d5f994SEdmund Nadolski /* 25886d5f994SEdmund Nadolski * Release the entire tree. We don't care about internal consistency so 25986d5f994SEdmund Nadolski * just free everything and then reset the tree root. 26086d5f994SEdmund Nadolski */ 26186d5f994SEdmund Nadolski static void prelim_release(struct preftree *preftree) 26286d5f994SEdmund Nadolski { 26386d5f994SEdmund Nadolski struct prelim_ref *ref, *next_ref; 26486d5f994SEdmund Nadolski 26586d5f994SEdmund Nadolski rbtree_postorder_for_each_entry_safe(ref, next_ref, &preftree->root, 26686d5f994SEdmund Nadolski rbnode) 26786d5f994SEdmund Nadolski free_pref(ref); 26886d5f994SEdmund Nadolski 26986d5f994SEdmund Nadolski preftree->root = RB_ROOT; 270*6c336b21SJeff Mahoney preftree->count = 0; 27186d5f994SEdmund Nadolski } 27286d5f994SEdmund Nadolski 273d5c88b73SJan Schmidt /* 274d5c88b73SJan Schmidt * the rules for all callers of this function are: 275d5c88b73SJan Schmidt * - obtaining the parent is the goal 276d5c88b73SJan Schmidt * - if you add a key, you must know that it is a correct key 277d5c88b73SJan Schmidt * - if you cannot add the parent or a correct key, then we will look into the 278d5c88b73SJan Schmidt * block later to set a correct key 279d5c88b73SJan Schmidt * 280d5c88b73SJan Schmidt * delayed refs 281d5c88b73SJan Schmidt * ============ 282d5c88b73SJan Schmidt * backref type | shared | indirect | shared | indirect 283d5c88b73SJan Schmidt * information | tree | tree | data | data 284d5c88b73SJan Schmidt * --------------------+--------+----------+--------+---------- 285d5c88b73SJan Schmidt * parent logical | y | - | - | - 286d5c88b73SJan Schmidt * key to resolve | - | y | y | y 287d5c88b73SJan Schmidt * tree block logical | - | - | - | - 288d5c88b73SJan Schmidt * root for resolving | y | y | y | y 289d5c88b73SJan Schmidt * 290d5c88b73SJan Schmidt * - column 1: we've the parent -> done 291d5c88b73SJan Schmidt * - column 2, 3, 4: we use the key to find the parent 292d5c88b73SJan Schmidt * 293d5c88b73SJan Schmidt * on disk refs (inline or keyed) 294d5c88b73SJan Schmidt * ============================== 295d5c88b73SJan Schmidt * backref type | shared | indirect | shared | indirect 296d5c88b73SJan Schmidt * information | tree | tree | data | data 297d5c88b73SJan Schmidt * --------------------+--------+----------+--------+---------- 298d5c88b73SJan Schmidt * parent logical | y | - | y | - 299d5c88b73SJan Schmidt * key to resolve | - | - | - | y 300d5c88b73SJan Schmidt * tree block logical | y | y | y | y 301d5c88b73SJan Schmidt * root for resolving | - | y | y | y 302d5c88b73SJan Schmidt * 303d5c88b73SJan Schmidt * - column 1, 3: we've the parent -> done 304d5c88b73SJan Schmidt * - column 2: we take the first key from the block to find the parent 305e0c476b1SJeff Mahoney * (see add_missing_keys) 306d5c88b73SJan Schmidt * - column 4: we use the key to find the parent 307d5c88b73SJan Schmidt * 308d5c88b73SJan Schmidt * additional information that's available but not required to find the parent 309d5c88b73SJan Schmidt * block might help in merging entries to gain some speed. 310d5c88b73SJan Schmidt */ 31186d5f994SEdmund Nadolski static int add_prelim_ref(struct preftree *preftree, u64 root_id, 312e0c476b1SJeff Mahoney const struct btrfs_key *key, int level, u64 parent, 313e0c476b1SJeff Mahoney u64 wanted_disk_byte, int count, gfp_t gfp_mask) 3148da6d581SJan Schmidt { 315e0c476b1SJeff Mahoney struct prelim_ref *ref; 3168da6d581SJan Schmidt 31748ec4736SLiu Bo if (root_id == BTRFS_DATA_RELOC_TREE_OBJECTID) 31848ec4736SLiu Bo return 0; 31948ec4736SLiu Bo 320b9e9a6cbSWang Shilong ref = kmem_cache_alloc(btrfs_prelim_ref_cache, gfp_mask); 3218da6d581SJan Schmidt if (!ref) 3228da6d581SJan Schmidt return -ENOMEM; 3238da6d581SJan Schmidt 3248da6d581SJan Schmidt ref->root_id = root_id; 325d6589101SFilipe Manana if (key) { 326d5c88b73SJan Schmidt ref->key_for_search = *key; 327d6589101SFilipe Manana /* 328d6589101SFilipe Manana * We can often find data backrefs with an offset that is too 329d6589101SFilipe Manana * large (>= LLONG_MAX, maximum allowed file offset) due to 330d6589101SFilipe Manana * underflows when subtracting a file's offset with the data 331d6589101SFilipe Manana * offset of its corresponding extent data item. This can 332d6589101SFilipe Manana * happen for example in the clone ioctl. 333d6589101SFilipe Manana * So if we detect such case we set the search key's offset to 334d6589101SFilipe Manana * zero to make sure we will find the matching file extent item 335d6589101SFilipe Manana * at add_all_parents(), otherwise we will miss it because the 336d6589101SFilipe Manana * offset taken form the backref is much larger then the offset 337d6589101SFilipe Manana * of the file extent item. This can make us scan a very large 338d6589101SFilipe Manana * number of file extent items, but at least it will not make 339d6589101SFilipe Manana * us miss any. 340d6589101SFilipe Manana * This is an ugly workaround for a behaviour that should have 341d6589101SFilipe Manana * never existed, but it does and a fix for the clone ioctl 342d6589101SFilipe Manana * would touch a lot of places, cause backwards incompatibility 343d6589101SFilipe Manana * and would not fix the problem for extents cloned with older 344d6589101SFilipe Manana * kernels. 345d6589101SFilipe Manana */ 346d6589101SFilipe Manana if (ref->key_for_search.type == BTRFS_EXTENT_DATA_KEY && 347d6589101SFilipe Manana ref->key_for_search.offset >= LLONG_MAX) 348d6589101SFilipe Manana ref->key_for_search.offset = 0; 349d6589101SFilipe Manana } else { 350d5c88b73SJan Schmidt memset(&ref->key_for_search, 0, sizeof(ref->key_for_search)); 351d6589101SFilipe Manana } 3528da6d581SJan Schmidt 3533301958bSJan Schmidt ref->inode_list = NULL; 3548da6d581SJan Schmidt ref->level = level; 3558da6d581SJan Schmidt ref->count = count; 3568da6d581SJan Schmidt ref->parent = parent; 3578da6d581SJan Schmidt ref->wanted_disk_byte = wanted_disk_byte; 35886d5f994SEdmund Nadolski prelim_ref_insert(preftree, ref); 3598da6d581SJan Schmidt 3608da6d581SJan Schmidt return 0; 3618da6d581SJan Schmidt } 3628da6d581SJan Schmidt 36386d5f994SEdmund Nadolski /* direct refs use root == 0, key == NULL */ 36486d5f994SEdmund Nadolski static int add_direct_ref(struct preftrees *preftrees, int level, u64 parent, 36586d5f994SEdmund Nadolski u64 wanted_disk_byte, int count, gfp_t gfp_mask) 36686d5f994SEdmund Nadolski { 36786d5f994SEdmund Nadolski return add_prelim_ref(&preftrees->direct, 0, NULL, level, parent, 36886d5f994SEdmund Nadolski wanted_disk_byte, count, gfp_mask); 36986d5f994SEdmund Nadolski } 37086d5f994SEdmund Nadolski 37186d5f994SEdmund Nadolski /* indirect refs use parent == 0 */ 37286d5f994SEdmund Nadolski static int add_indirect_ref(struct preftrees *preftrees, u64 root_id, 37386d5f994SEdmund Nadolski const struct btrfs_key *key, int level, 37486d5f994SEdmund Nadolski u64 wanted_disk_byte, int count, gfp_t gfp_mask) 37586d5f994SEdmund Nadolski { 37686d5f994SEdmund Nadolski struct preftree *tree = &preftrees->indirect; 37786d5f994SEdmund Nadolski 37886d5f994SEdmund Nadolski if (!key) 37986d5f994SEdmund Nadolski tree = &preftrees->indirect_missing_keys; 38086d5f994SEdmund Nadolski return add_prelim_ref(tree, root_id, key, level, 0, 38186d5f994SEdmund Nadolski wanted_disk_byte, count, gfp_mask); 38286d5f994SEdmund Nadolski } 38386d5f994SEdmund Nadolski 3848da6d581SJan Schmidt static int add_all_parents(struct btrfs_root *root, struct btrfs_path *path, 385e0c476b1SJeff Mahoney struct ulist *parents, struct prelim_ref *ref, 38644853868SJosef Bacik int level, u64 time_seq, const u64 *extent_item_pos, 38744853868SJosef Bacik u64 total_refs) 3888da6d581SJan Schmidt { 38969bca40dSAlexander Block int ret = 0; 39069bca40dSAlexander Block int slot; 39169bca40dSAlexander Block struct extent_buffer *eb; 39269bca40dSAlexander Block struct btrfs_key key; 3937ef81ac8SJosef Bacik struct btrfs_key *key_for_search = &ref->key_for_search; 3948da6d581SJan Schmidt struct btrfs_file_extent_item *fi; 395ed8c4913SJosef Bacik struct extent_inode_elem *eie = NULL, *old = NULL; 3968da6d581SJan Schmidt u64 disk_byte; 3977ef81ac8SJosef Bacik u64 wanted_disk_byte = ref->wanted_disk_byte; 3987ef81ac8SJosef Bacik u64 count = 0; 3998da6d581SJan Schmidt 40069bca40dSAlexander Block if (level != 0) { 40169bca40dSAlexander Block eb = path->nodes[level]; 40269bca40dSAlexander Block ret = ulist_add(parents, eb->start, 0, GFP_NOFS); 4033301958bSJan Schmidt if (ret < 0) 4043301958bSJan Schmidt return ret; 4058da6d581SJan Schmidt return 0; 40669bca40dSAlexander Block } 4078da6d581SJan Schmidt 4088da6d581SJan Schmidt /* 40969bca40dSAlexander Block * We normally enter this function with the path already pointing to 41069bca40dSAlexander Block * the first item to check. But sometimes, we may enter it with 41169bca40dSAlexander Block * slot==nritems. In that case, go to the next leaf before we continue. 4128da6d581SJan Schmidt */ 41321633fc6SQu Wenruo if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) { 414de47c9d3SEdmund Nadolski if (time_seq == SEQ_LAST) 41521633fc6SQu Wenruo ret = btrfs_next_leaf(root, path); 41621633fc6SQu Wenruo else 4173d7806ecSJan Schmidt ret = btrfs_next_old_leaf(root, path, time_seq); 41821633fc6SQu Wenruo } 4198da6d581SJan Schmidt 42044853868SJosef Bacik while (!ret && count < total_refs) { 4218da6d581SJan Schmidt eb = path->nodes[0]; 42269bca40dSAlexander Block slot = path->slots[0]; 42369bca40dSAlexander Block 42469bca40dSAlexander Block btrfs_item_key_to_cpu(eb, &key, slot); 42569bca40dSAlexander Block 42669bca40dSAlexander Block if (key.objectid != key_for_search->objectid || 42769bca40dSAlexander Block key.type != BTRFS_EXTENT_DATA_KEY) 42869bca40dSAlexander Block break; 42969bca40dSAlexander Block 43069bca40dSAlexander Block fi = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item); 4318da6d581SJan Schmidt disk_byte = btrfs_file_extent_disk_bytenr(eb, fi); 43269bca40dSAlexander Block 43369bca40dSAlexander Block if (disk_byte == wanted_disk_byte) { 43469bca40dSAlexander Block eie = NULL; 435ed8c4913SJosef Bacik old = NULL; 4367ef81ac8SJosef Bacik count++; 43769bca40dSAlexander Block if (extent_item_pos) { 43869bca40dSAlexander Block ret = check_extent_in_eb(&key, eb, fi, 43969bca40dSAlexander Block *extent_item_pos, 44069bca40dSAlexander Block &eie); 44169bca40dSAlexander Block if (ret < 0) 44269bca40dSAlexander Block break; 4438da6d581SJan Schmidt } 444ed8c4913SJosef Bacik if (ret > 0) 445ed8c4913SJosef Bacik goto next; 4464eb1f66dSTakashi Iwai ret = ulist_add_merge_ptr(parents, eb->start, 4474eb1f66dSTakashi Iwai eie, (void **)&old, GFP_NOFS); 44869bca40dSAlexander Block if (ret < 0) 44969bca40dSAlexander Block break; 450ed8c4913SJosef Bacik if (!ret && extent_item_pos) { 451ed8c4913SJosef Bacik while (old->next) 452ed8c4913SJosef Bacik old = old->next; 453ed8c4913SJosef Bacik old->next = eie; 45469bca40dSAlexander Block } 455f05c4746SWang Shilong eie = NULL; 45669bca40dSAlexander Block } 457ed8c4913SJosef Bacik next: 458de47c9d3SEdmund Nadolski if (time_seq == SEQ_LAST) 45921633fc6SQu Wenruo ret = btrfs_next_item(root, path); 46021633fc6SQu Wenruo else 46169bca40dSAlexander Block ret = btrfs_next_old_item(root, path, time_seq); 4628da6d581SJan Schmidt } 4638da6d581SJan Schmidt 46469bca40dSAlexander Block if (ret > 0) 46569bca40dSAlexander Block ret = 0; 466f05c4746SWang Shilong else if (ret < 0) 467f05c4746SWang Shilong free_inode_elem_list(eie); 46869bca40dSAlexander Block return ret; 4698da6d581SJan Schmidt } 4708da6d581SJan Schmidt 4718da6d581SJan Schmidt /* 4728da6d581SJan Schmidt * resolve an indirect backref in the form (root_id, key, level) 4738da6d581SJan Schmidt * to a logical address 4748da6d581SJan Schmidt */ 475e0c476b1SJeff Mahoney static int resolve_indirect_ref(struct btrfs_fs_info *fs_info, 476da61d31aSJosef Bacik struct btrfs_path *path, u64 time_seq, 477e0c476b1SJeff Mahoney struct prelim_ref *ref, struct ulist *parents, 47844853868SJosef Bacik const u64 *extent_item_pos, u64 total_refs) 4798da6d581SJan Schmidt { 4808da6d581SJan Schmidt struct btrfs_root *root; 4818da6d581SJan Schmidt struct btrfs_key root_key; 4828da6d581SJan Schmidt struct extent_buffer *eb; 4838da6d581SJan Schmidt int ret = 0; 4848da6d581SJan Schmidt int root_level; 4858da6d581SJan Schmidt int level = ref->level; 486538f72cdSWang Shilong int index; 4878da6d581SJan Schmidt 4888da6d581SJan Schmidt root_key.objectid = ref->root_id; 4898da6d581SJan Schmidt root_key.type = BTRFS_ROOT_ITEM_KEY; 4908da6d581SJan Schmidt root_key.offset = (u64)-1; 491538f72cdSWang Shilong 492538f72cdSWang Shilong index = srcu_read_lock(&fs_info->subvol_srcu); 493538f72cdSWang Shilong 4942d9e9776SJosef Bacik root = btrfs_get_fs_root(fs_info, &root_key, false); 4958da6d581SJan Schmidt if (IS_ERR(root)) { 496538f72cdSWang Shilong srcu_read_unlock(&fs_info->subvol_srcu, index); 4978da6d581SJan Schmidt ret = PTR_ERR(root); 4988da6d581SJan Schmidt goto out; 4998da6d581SJan Schmidt } 5008da6d581SJan Schmidt 501f5ee5c9aSJeff Mahoney if (btrfs_is_testing(fs_info)) { 502d9ee522bSJosef Bacik srcu_read_unlock(&fs_info->subvol_srcu, index); 503d9ee522bSJosef Bacik ret = -ENOENT; 504d9ee522bSJosef Bacik goto out; 505d9ee522bSJosef Bacik } 506d9ee522bSJosef Bacik 5079e351cc8SJosef Bacik if (path->search_commit_root) 5089e351cc8SJosef Bacik root_level = btrfs_header_level(root->commit_root); 509de47c9d3SEdmund Nadolski else if (time_seq == SEQ_LAST) 51021633fc6SQu Wenruo root_level = btrfs_header_level(root->node); 5119e351cc8SJosef Bacik else 5125b6602e7SJan Schmidt root_level = btrfs_old_root_level(root, time_seq); 5138da6d581SJan Schmidt 514538f72cdSWang Shilong if (root_level + 1 == level) { 515538f72cdSWang Shilong srcu_read_unlock(&fs_info->subvol_srcu, index); 5168da6d581SJan Schmidt goto out; 517538f72cdSWang Shilong } 5188da6d581SJan Schmidt 5198da6d581SJan Schmidt path->lowest_level = level; 520de47c9d3SEdmund Nadolski if (time_seq == SEQ_LAST) 52121633fc6SQu Wenruo ret = btrfs_search_slot(NULL, root, &ref->key_for_search, path, 52221633fc6SQu Wenruo 0, 0); 52321633fc6SQu Wenruo else 52421633fc6SQu Wenruo ret = btrfs_search_old_slot(root, &ref->key_for_search, path, 52521633fc6SQu Wenruo time_seq); 526538f72cdSWang Shilong 527538f72cdSWang Shilong /* root node has been locked, we can release @subvol_srcu safely here */ 528538f72cdSWang Shilong srcu_read_unlock(&fs_info->subvol_srcu, index); 529538f72cdSWang Shilong 530ab8d0fc4SJeff Mahoney btrfs_debug(fs_info, 531ab8d0fc4SJeff Mahoney "search slot in root %llu (level %d, ref count %d) returned %d for key (%llu %u %llu)", 532c1c9ff7cSGeert Uytterhoeven ref->root_id, level, ref->count, ret, 533c1c9ff7cSGeert Uytterhoeven ref->key_for_search.objectid, ref->key_for_search.type, 534c1c9ff7cSGeert Uytterhoeven ref->key_for_search.offset); 5358da6d581SJan Schmidt if (ret < 0) 5368da6d581SJan Schmidt goto out; 5378da6d581SJan Schmidt 5388da6d581SJan Schmidt eb = path->nodes[level]; 5399345457fSJan Schmidt while (!eb) { 540fae7f21cSDulshani Gunawardhana if (WARN_ON(!level)) { 5418da6d581SJan Schmidt ret = 1; 5428da6d581SJan Schmidt goto out; 5438da6d581SJan Schmidt } 5449345457fSJan Schmidt level--; 5459345457fSJan Schmidt eb = path->nodes[level]; 5469345457fSJan Schmidt } 5478da6d581SJan Schmidt 5487ef81ac8SJosef Bacik ret = add_all_parents(root, path, parents, ref, level, time_seq, 54944853868SJosef Bacik extent_item_pos, total_refs); 5508da6d581SJan Schmidt out: 551da61d31aSJosef Bacik path->lowest_level = 0; 552da61d31aSJosef Bacik btrfs_release_path(path); 5538da6d581SJan Schmidt return ret; 5548da6d581SJan Schmidt } 5558da6d581SJan Schmidt 5564dae077aSJeff Mahoney static struct extent_inode_elem * 5574dae077aSJeff Mahoney unode_aux_to_inode_list(struct ulist_node *node) 5584dae077aSJeff Mahoney { 5594dae077aSJeff Mahoney if (!node) 5604dae077aSJeff Mahoney return NULL; 5614dae077aSJeff Mahoney return (struct extent_inode_elem *)(uintptr_t)node->aux; 5624dae077aSJeff Mahoney } 5634dae077aSJeff Mahoney 5648da6d581SJan Schmidt /* 56586d5f994SEdmund Nadolski * We maintain three seperate rbtrees: one for direct refs, one for 56686d5f994SEdmund Nadolski * indirect refs which have a key, and one for indirect refs which do not 56786d5f994SEdmund Nadolski * have a key. Each tree does merge on insertion. 56886d5f994SEdmund Nadolski * 56986d5f994SEdmund Nadolski * Once all of the references are located, we iterate over the tree of 57086d5f994SEdmund Nadolski * indirect refs with missing keys. An appropriate key is located and 57186d5f994SEdmund Nadolski * the ref is moved onto the tree for indirect refs. After all missing 57286d5f994SEdmund Nadolski * keys are thus located, we iterate over the indirect ref tree, resolve 57386d5f994SEdmund Nadolski * each reference, and then insert the resolved reference onto the 57486d5f994SEdmund Nadolski * direct tree (merging there too). 57586d5f994SEdmund Nadolski * 57686d5f994SEdmund Nadolski * New backrefs (i.e., for parent nodes) are added to the appropriate 57786d5f994SEdmund Nadolski * rbtree as they are encountered. The new backrefs are subsequently 57886d5f994SEdmund Nadolski * resolved as above. 5798da6d581SJan Schmidt */ 580e0c476b1SJeff Mahoney static int resolve_indirect_refs(struct btrfs_fs_info *fs_info, 581da61d31aSJosef Bacik struct btrfs_path *path, u64 time_seq, 58286d5f994SEdmund Nadolski struct preftrees *preftrees, 583dc046b10SJosef Bacik const u64 *extent_item_pos, u64 total_refs, 584dc046b10SJosef Bacik u64 root_objectid) 5858da6d581SJan Schmidt { 5868da6d581SJan Schmidt int err; 5878da6d581SJan Schmidt int ret = 0; 5888da6d581SJan Schmidt struct ulist *parents; 5898da6d581SJan Schmidt struct ulist_node *node; 590cd1b413cSJan Schmidt struct ulist_iterator uiter; 59186d5f994SEdmund Nadolski struct rb_node *rnode; 5928da6d581SJan Schmidt 5938da6d581SJan Schmidt parents = ulist_alloc(GFP_NOFS); 5948da6d581SJan Schmidt if (!parents) 5958da6d581SJan Schmidt return -ENOMEM; 5968da6d581SJan Schmidt 5978da6d581SJan Schmidt /* 59886d5f994SEdmund Nadolski * We could trade memory usage for performance here by iterating 59986d5f994SEdmund Nadolski * the tree, allocating new refs for each insertion, and then 60086d5f994SEdmund Nadolski * freeing the entire indirect tree when we're done. In some test 60186d5f994SEdmund Nadolski * cases, the tree can grow quite large (~200k objects). 6028da6d581SJan Schmidt */ 60386d5f994SEdmund Nadolski while ((rnode = rb_first(&preftrees->indirect.root))) { 60486d5f994SEdmund Nadolski struct prelim_ref *ref; 60586d5f994SEdmund Nadolski 60686d5f994SEdmund Nadolski ref = rb_entry(rnode, struct prelim_ref, rbnode); 60786d5f994SEdmund Nadolski if (WARN(ref->parent, 60886d5f994SEdmund Nadolski "BUG: direct ref found in indirect tree")) { 60986d5f994SEdmund Nadolski ret = -EINVAL; 61086d5f994SEdmund Nadolski goto out; 61186d5f994SEdmund Nadolski } 61286d5f994SEdmund Nadolski 61386d5f994SEdmund Nadolski rb_erase(&ref->rbnode, &preftrees->indirect.root); 614*6c336b21SJeff Mahoney preftrees->indirect.count--; 61586d5f994SEdmund Nadolski 61686d5f994SEdmund Nadolski if (ref->count == 0) { 61786d5f994SEdmund Nadolski free_pref(ref); 6188da6d581SJan Schmidt continue; 61986d5f994SEdmund Nadolski } 62086d5f994SEdmund Nadolski 621dc046b10SJosef Bacik if (root_objectid && ref->root_id != root_objectid) { 62286d5f994SEdmund Nadolski free_pref(ref); 623dc046b10SJosef Bacik ret = BACKREF_FOUND_SHARED; 624dc046b10SJosef Bacik goto out; 625dc046b10SJosef Bacik } 626e0c476b1SJeff Mahoney err = resolve_indirect_ref(fs_info, path, time_seq, ref, 62744853868SJosef Bacik parents, extent_item_pos, 62844853868SJosef Bacik total_refs); 62995def2edSWang Shilong /* 63095def2edSWang Shilong * we can only tolerate ENOENT,otherwise,we should catch error 63195def2edSWang Shilong * and return directly. 63295def2edSWang Shilong */ 63395def2edSWang Shilong if (err == -ENOENT) { 63486d5f994SEdmund Nadolski prelim_ref_insert(&preftrees->direct, ref); 6358da6d581SJan Schmidt continue; 63695def2edSWang Shilong } else if (err) { 63786d5f994SEdmund Nadolski free_pref(ref); 63895def2edSWang Shilong ret = err; 63995def2edSWang Shilong goto out; 64095def2edSWang Shilong } 6418da6d581SJan Schmidt 6428da6d581SJan Schmidt /* we put the first parent into the ref at hand */ 643cd1b413cSJan Schmidt ULIST_ITER_INIT(&uiter); 644cd1b413cSJan Schmidt node = ulist_next(parents, &uiter); 6458da6d581SJan Schmidt ref->parent = node ? node->val : 0; 6464dae077aSJeff Mahoney ref->inode_list = unode_aux_to_inode_list(node); 6478da6d581SJan Schmidt 64886d5f994SEdmund Nadolski /* Add a prelim_ref(s) for any other parent(s). */ 649cd1b413cSJan Schmidt while ((node = ulist_next(parents, &uiter))) { 65086d5f994SEdmund Nadolski struct prelim_ref *new_ref; 65186d5f994SEdmund Nadolski 652b9e9a6cbSWang Shilong new_ref = kmem_cache_alloc(btrfs_prelim_ref_cache, 653b9e9a6cbSWang Shilong GFP_NOFS); 6548da6d581SJan Schmidt if (!new_ref) { 65586d5f994SEdmund Nadolski free_pref(ref); 6568da6d581SJan Schmidt ret = -ENOMEM; 657e36902d4SWang Shilong goto out; 6588da6d581SJan Schmidt } 6598da6d581SJan Schmidt memcpy(new_ref, ref, sizeof(*ref)); 6608da6d581SJan Schmidt new_ref->parent = node->val; 6614dae077aSJeff Mahoney new_ref->inode_list = unode_aux_to_inode_list(node); 66286d5f994SEdmund Nadolski prelim_ref_insert(&preftrees->direct, new_ref); 6638da6d581SJan Schmidt } 66486d5f994SEdmund Nadolski 66586d5f994SEdmund Nadolski /* Now it's a direct ref, put it in the the direct tree */ 66686d5f994SEdmund Nadolski prelim_ref_insert(&preftrees->direct, ref); 66786d5f994SEdmund Nadolski 6688da6d581SJan Schmidt ulist_reinit(parents); 6698da6d581SJan Schmidt } 670e36902d4SWang Shilong out: 6718da6d581SJan Schmidt ulist_free(parents); 6728da6d581SJan Schmidt return ret; 6738da6d581SJan Schmidt } 6748da6d581SJan Schmidt 675d5c88b73SJan Schmidt /* 676d5c88b73SJan Schmidt * read tree blocks and add keys where required. 677d5c88b73SJan Schmidt */ 678e0c476b1SJeff Mahoney static int add_missing_keys(struct btrfs_fs_info *fs_info, 67986d5f994SEdmund Nadolski struct preftrees *preftrees) 680d5c88b73SJan Schmidt { 681e0c476b1SJeff Mahoney struct prelim_ref *ref; 682d5c88b73SJan Schmidt struct extent_buffer *eb; 68386d5f994SEdmund Nadolski struct preftree *tree = &preftrees->indirect_missing_keys; 68486d5f994SEdmund Nadolski struct rb_node *node; 685d5c88b73SJan Schmidt 68686d5f994SEdmund Nadolski while ((node = rb_first(&tree->root))) { 68786d5f994SEdmund Nadolski ref = rb_entry(node, struct prelim_ref, rbnode); 68886d5f994SEdmund Nadolski rb_erase(node, &tree->root); 68986d5f994SEdmund Nadolski 69086d5f994SEdmund Nadolski BUG_ON(ref->parent); /* should not be a direct ref */ 69186d5f994SEdmund Nadolski BUG_ON(ref->key_for_search.type); 692d5c88b73SJan Schmidt BUG_ON(!ref->wanted_disk_byte); 69386d5f994SEdmund Nadolski 6942ff7e61eSJeff Mahoney eb = read_tree_block(fs_info, ref->wanted_disk_byte, 0); 69564c043deSLiu Bo if (IS_ERR(eb)) { 69686d5f994SEdmund Nadolski free_pref(ref); 69764c043deSLiu Bo return PTR_ERR(eb); 69864c043deSLiu Bo } else if (!extent_buffer_uptodate(eb)) { 69986d5f994SEdmund Nadolski free_pref(ref); 700416bc658SJosef Bacik free_extent_buffer(eb); 701416bc658SJosef Bacik return -EIO; 702416bc658SJosef Bacik } 703d5c88b73SJan Schmidt btrfs_tree_read_lock(eb); 704d5c88b73SJan Schmidt if (btrfs_header_level(eb) == 0) 705d5c88b73SJan Schmidt btrfs_item_key_to_cpu(eb, &ref->key_for_search, 0); 706d5c88b73SJan Schmidt else 707d5c88b73SJan Schmidt btrfs_node_key_to_cpu(eb, &ref->key_for_search, 0); 708d5c88b73SJan Schmidt btrfs_tree_read_unlock(eb); 709d5c88b73SJan Schmidt free_extent_buffer(eb); 71086d5f994SEdmund Nadolski prelim_ref_insert(&preftrees->indirect, ref); 711d5c88b73SJan Schmidt } 712d5c88b73SJan Schmidt return 0; 713d5c88b73SJan Schmidt } 714d5c88b73SJan Schmidt 7158da6d581SJan Schmidt /* 7168da6d581SJan Schmidt * add all currently queued delayed refs from this head whose seq nr is 7178da6d581SJan Schmidt * smaller or equal that seq to the list 7188da6d581SJan Schmidt */ 719e0c476b1SJeff Mahoney static int add_delayed_refs(struct btrfs_delayed_ref_head *head, u64 seq, 72086d5f994SEdmund Nadolski struct preftrees *preftrees, u64 *total_refs, 721dc046b10SJosef Bacik u64 inum) 7228da6d581SJan Schmidt { 723c6fc2454SQu Wenruo struct btrfs_delayed_ref_node *node; 7248da6d581SJan Schmidt struct btrfs_delayed_extent_op *extent_op = head->extent_op; 725d5c88b73SJan Schmidt struct btrfs_key key; 72686d5f994SEdmund Nadolski struct btrfs_key tmp_op_key; 72786d5f994SEdmund Nadolski struct btrfs_key *op_key = NULL; 7288da6d581SJan Schmidt int sgn; 729b1375d64SJan Schmidt int ret = 0; 7308da6d581SJan Schmidt 73186d5f994SEdmund Nadolski if (extent_op && extent_op->update_key) { 73286d5f994SEdmund Nadolski btrfs_disk_key_to_cpu(&tmp_op_key, &extent_op->key); 73386d5f994SEdmund Nadolski op_key = &tmp_op_key; 73486d5f994SEdmund Nadolski } 7358da6d581SJan Schmidt 736d7df2c79SJosef Bacik spin_lock(&head->lock); 737c6fc2454SQu Wenruo list_for_each_entry(node, &head->ref_list, list) { 7388da6d581SJan Schmidt if (node->seq > seq) 7398da6d581SJan Schmidt continue; 7408da6d581SJan Schmidt 7418da6d581SJan Schmidt switch (node->action) { 7428da6d581SJan Schmidt case BTRFS_ADD_DELAYED_EXTENT: 7438da6d581SJan Schmidt case BTRFS_UPDATE_DELAYED_HEAD: 7448da6d581SJan Schmidt WARN_ON(1); 7458da6d581SJan Schmidt continue; 7468da6d581SJan Schmidt case BTRFS_ADD_DELAYED_REF: 7478da6d581SJan Schmidt sgn = 1; 7488da6d581SJan Schmidt break; 7498da6d581SJan Schmidt case BTRFS_DROP_DELAYED_REF: 7508da6d581SJan Schmidt sgn = -1; 7518da6d581SJan Schmidt break; 7528da6d581SJan Schmidt default: 7538da6d581SJan Schmidt BUG_ON(1); 7548da6d581SJan Schmidt } 75544853868SJosef Bacik *total_refs += (node->ref_mod * sgn); 7568da6d581SJan Schmidt switch (node->type) { 7578da6d581SJan Schmidt case BTRFS_TREE_BLOCK_REF_KEY: { 75886d5f994SEdmund Nadolski /* NORMAL INDIRECT METADATA backref */ 7598da6d581SJan Schmidt struct btrfs_delayed_tree_ref *ref; 7608da6d581SJan Schmidt 7618da6d581SJan Schmidt ref = btrfs_delayed_node_to_tree_ref(node); 76286d5f994SEdmund Nadolski ret = add_indirect_ref(preftrees, ref->root, &tmp_op_key, 76386d5f994SEdmund Nadolski ref->level + 1, node->bytenr, 76486d5f994SEdmund Nadolski node->ref_mod * sgn, 76586d5f994SEdmund Nadolski GFP_ATOMIC); 7668da6d581SJan Schmidt break; 7678da6d581SJan Schmidt } 7688da6d581SJan Schmidt case BTRFS_SHARED_BLOCK_REF_KEY: { 76986d5f994SEdmund Nadolski /* SHARED DIRECT METADATA backref */ 7708da6d581SJan Schmidt struct btrfs_delayed_tree_ref *ref; 7718da6d581SJan Schmidt 7728da6d581SJan Schmidt ref = btrfs_delayed_node_to_tree_ref(node); 77386d5f994SEdmund Nadolski 77486d5f994SEdmund Nadolski ret = add_direct_ref(preftrees, ref->level + 1, 775e0c476b1SJeff Mahoney ref->parent, node->bytenr, 77686d5f994SEdmund Nadolski node->ref_mod * sgn, 77786d5f994SEdmund Nadolski GFP_ATOMIC); 7788da6d581SJan Schmidt break; 7798da6d581SJan Schmidt } 7808da6d581SJan Schmidt case BTRFS_EXTENT_DATA_REF_KEY: { 78186d5f994SEdmund Nadolski /* NORMAL INDIRECT DATA backref */ 7828da6d581SJan Schmidt struct btrfs_delayed_data_ref *ref; 7838da6d581SJan Schmidt ref = btrfs_delayed_node_to_data_ref(node); 7848da6d581SJan Schmidt 7858da6d581SJan Schmidt key.objectid = ref->objectid; 7868da6d581SJan Schmidt key.type = BTRFS_EXTENT_DATA_KEY; 7878da6d581SJan Schmidt key.offset = ref->offset; 788dc046b10SJosef Bacik 789dc046b10SJosef Bacik /* 790dc046b10SJosef Bacik * Found a inum that doesn't match our known inum, we 791dc046b10SJosef Bacik * know it's shared. 792dc046b10SJosef Bacik */ 793dc046b10SJosef Bacik if (inum && ref->objectid != inum) { 794dc046b10SJosef Bacik ret = BACKREF_FOUND_SHARED; 795dc046b10SJosef Bacik break; 796dc046b10SJosef Bacik } 797dc046b10SJosef Bacik 79886d5f994SEdmund Nadolski ret = add_indirect_ref(preftrees, ref->root, &key, 0, 79986d5f994SEdmund Nadolski node->bytenr, 80086d5f994SEdmund Nadolski node->ref_mod * sgn, 801e0c476b1SJeff Mahoney GFP_ATOMIC); 8028da6d581SJan Schmidt break; 8038da6d581SJan Schmidt } 8048da6d581SJan Schmidt case BTRFS_SHARED_DATA_REF_KEY: { 80586d5f994SEdmund Nadolski /* SHARED DIRECT FULL backref */ 8068da6d581SJan Schmidt struct btrfs_delayed_data_ref *ref; 8078da6d581SJan Schmidt 8088da6d581SJan Schmidt ref = btrfs_delayed_node_to_data_ref(node); 80986d5f994SEdmund Nadolski 81086d5f994SEdmund Nadolski ret = add_direct_ref(preftrees, 0, ref->parent, 81186d5f994SEdmund Nadolski node->bytenr, 81286d5f994SEdmund Nadolski node->ref_mod * sgn, 813e0c476b1SJeff Mahoney GFP_ATOMIC); 8148da6d581SJan Schmidt break; 8158da6d581SJan Schmidt } 8168da6d581SJan Schmidt default: 8178da6d581SJan Schmidt WARN_ON(1); 8188da6d581SJan Schmidt } 8191149ab6bSWang Shilong if (ret) 820d7df2c79SJosef Bacik break; 8218da6d581SJan Schmidt } 822d7df2c79SJosef Bacik spin_unlock(&head->lock); 823d7df2c79SJosef Bacik return ret; 8248da6d581SJan Schmidt } 8258da6d581SJan Schmidt 8268da6d581SJan Schmidt /* 8278da6d581SJan Schmidt * add all inline backrefs for bytenr to the list 8288da6d581SJan Schmidt */ 829e0c476b1SJeff Mahoney static int add_inline_refs(struct btrfs_path *path, u64 bytenr, 83086d5f994SEdmund Nadolski int *info_level, struct preftrees *preftrees, 831dc046b10SJosef Bacik u64 *total_refs, u64 inum) 8328da6d581SJan Schmidt { 833b1375d64SJan Schmidt int ret = 0; 8348da6d581SJan Schmidt int slot; 8358da6d581SJan Schmidt struct extent_buffer *leaf; 8368da6d581SJan Schmidt struct btrfs_key key; 837261c84b6SJosef Bacik struct btrfs_key found_key; 8388da6d581SJan Schmidt unsigned long ptr; 8398da6d581SJan Schmidt unsigned long end; 8408da6d581SJan Schmidt struct btrfs_extent_item *ei; 8418da6d581SJan Schmidt u64 flags; 8428da6d581SJan Schmidt u64 item_size; 8438da6d581SJan Schmidt 8448da6d581SJan Schmidt /* 8458da6d581SJan Schmidt * enumerate all inline refs 8468da6d581SJan Schmidt */ 8478da6d581SJan Schmidt leaf = path->nodes[0]; 848dadcaf78SJan Schmidt slot = path->slots[0]; 8498da6d581SJan Schmidt 8508da6d581SJan Schmidt item_size = btrfs_item_size_nr(leaf, slot); 8518da6d581SJan Schmidt BUG_ON(item_size < sizeof(*ei)); 8528da6d581SJan Schmidt 8538da6d581SJan Schmidt ei = btrfs_item_ptr(leaf, slot, struct btrfs_extent_item); 8548da6d581SJan Schmidt flags = btrfs_extent_flags(leaf, ei); 85544853868SJosef Bacik *total_refs += btrfs_extent_refs(leaf, ei); 856261c84b6SJosef Bacik btrfs_item_key_to_cpu(leaf, &found_key, slot); 8578da6d581SJan Schmidt 8588da6d581SJan Schmidt ptr = (unsigned long)(ei + 1); 8598da6d581SJan Schmidt end = (unsigned long)ei + item_size; 8608da6d581SJan Schmidt 861261c84b6SJosef Bacik if (found_key.type == BTRFS_EXTENT_ITEM_KEY && 862261c84b6SJosef Bacik flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) { 8638da6d581SJan Schmidt struct btrfs_tree_block_info *info; 8648da6d581SJan Schmidt 8658da6d581SJan Schmidt info = (struct btrfs_tree_block_info *)ptr; 8668da6d581SJan Schmidt *info_level = btrfs_tree_block_level(leaf, info); 8678da6d581SJan Schmidt ptr += sizeof(struct btrfs_tree_block_info); 8688da6d581SJan Schmidt BUG_ON(ptr > end); 869261c84b6SJosef Bacik } else if (found_key.type == BTRFS_METADATA_ITEM_KEY) { 870261c84b6SJosef Bacik *info_level = found_key.offset; 8718da6d581SJan Schmidt } else { 8728da6d581SJan Schmidt BUG_ON(!(flags & BTRFS_EXTENT_FLAG_DATA)); 8738da6d581SJan Schmidt } 8748da6d581SJan Schmidt 8758da6d581SJan Schmidt while (ptr < end) { 8768da6d581SJan Schmidt struct btrfs_extent_inline_ref *iref; 8778da6d581SJan Schmidt u64 offset; 8788da6d581SJan Schmidt int type; 8798da6d581SJan Schmidt 8808da6d581SJan Schmidt iref = (struct btrfs_extent_inline_ref *)ptr; 8818da6d581SJan Schmidt type = btrfs_extent_inline_ref_type(leaf, iref); 8828da6d581SJan Schmidt offset = btrfs_extent_inline_ref_offset(leaf, iref); 8838da6d581SJan Schmidt 8848da6d581SJan Schmidt switch (type) { 8858da6d581SJan Schmidt case BTRFS_SHARED_BLOCK_REF_KEY: 88686d5f994SEdmund Nadolski ret = add_direct_ref(preftrees, *info_level + 1, offset, 88786d5f994SEdmund Nadolski bytenr, 1, GFP_NOFS); 8888da6d581SJan Schmidt break; 8898da6d581SJan Schmidt case BTRFS_SHARED_DATA_REF_KEY: { 8908da6d581SJan Schmidt struct btrfs_shared_data_ref *sdref; 8918da6d581SJan Schmidt int count; 8928da6d581SJan Schmidt 8938da6d581SJan Schmidt sdref = (struct btrfs_shared_data_ref *)(iref + 1); 8948da6d581SJan Schmidt count = btrfs_shared_data_ref_count(leaf, sdref); 89586d5f994SEdmund Nadolski 89686d5f994SEdmund Nadolski ret = add_direct_ref(preftrees, 0, offset, 897742916b8SWang Shilong bytenr, count, GFP_NOFS); 8988da6d581SJan Schmidt break; 8998da6d581SJan Schmidt } 9008da6d581SJan Schmidt case BTRFS_TREE_BLOCK_REF_KEY: 90186d5f994SEdmund Nadolski ret = add_indirect_ref(preftrees, offset, NULL, 90286d5f994SEdmund Nadolski *info_level + 1, bytenr, 1, 90386d5f994SEdmund Nadolski GFP_NOFS); 9048da6d581SJan Schmidt break; 9058da6d581SJan Schmidt case BTRFS_EXTENT_DATA_REF_KEY: { 9068da6d581SJan Schmidt struct btrfs_extent_data_ref *dref; 9078da6d581SJan Schmidt int count; 9088da6d581SJan Schmidt u64 root; 9098da6d581SJan Schmidt 9108da6d581SJan Schmidt dref = (struct btrfs_extent_data_ref *)(&iref->offset); 9118da6d581SJan Schmidt count = btrfs_extent_data_ref_count(leaf, dref); 9128da6d581SJan Schmidt key.objectid = btrfs_extent_data_ref_objectid(leaf, 9138da6d581SJan Schmidt dref); 9148da6d581SJan Schmidt key.type = BTRFS_EXTENT_DATA_KEY; 9158da6d581SJan Schmidt key.offset = btrfs_extent_data_ref_offset(leaf, dref); 916dc046b10SJosef Bacik 917dc046b10SJosef Bacik if (inum && key.objectid != inum) { 918dc046b10SJosef Bacik ret = BACKREF_FOUND_SHARED; 919dc046b10SJosef Bacik break; 920dc046b10SJosef Bacik } 921dc046b10SJosef Bacik 9228da6d581SJan Schmidt root = btrfs_extent_data_ref_root(leaf, dref); 92386d5f994SEdmund Nadolski 92486d5f994SEdmund Nadolski ret = add_indirect_ref(preftrees, root, &key, 0, bytenr, 92586d5f994SEdmund Nadolski count, GFP_NOFS); 9268da6d581SJan Schmidt break; 9278da6d581SJan Schmidt } 9288da6d581SJan Schmidt default: 9298da6d581SJan Schmidt WARN_ON(1); 9308da6d581SJan Schmidt } 9311149ab6bSWang Shilong if (ret) 9321149ab6bSWang Shilong return ret; 9338da6d581SJan Schmidt ptr += btrfs_extent_inline_ref_size(type); 9348da6d581SJan Schmidt } 9358da6d581SJan Schmidt 9368da6d581SJan Schmidt return 0; 9378da6d581SJan Schmidt } 9388da6d581SJan Schmidt 9398da6d581SJan Schmidt /* 9408da6d581SJan Schmidt * add all non-inline backrefs for bytenr to the list 9418da6d581SJan Schmidt */ 942e0c476b1SJeff Mahoney static int add_keyed_refs(struct btrfs_fs_info *fs_info, 9438da6d581SJan Schmidt struct btrfs_path *path, u64 bytenr, 94486d5f994SEdmund Nadolski int info_level, struct preftrees *preftrees, 94586d5f994SEdmund Nadolski u64 inum) 9468da6d581SJan Schmidt { 9478da6d581SJan Schmidt struct btrfs_root *extent_root = fs_info->extent_root; 9488da6d581SJan Schmidt int ret; 9498da6d581SJan Schmidt int slot; 9508da6d581SJan Schmidt struct extent_buffer *leaf; 9518da6d581SJan Schmidt struct btrfs_key key; 9528da6d581SJan Schmidt 9538da6d581SJan Schmidt while (1) { 9548da6d581SJan Schmidt ret = btrfs_next_item(extent_root, path); 9558da6d581SJan Schmidt if (ret < 0) 9568da6d581SJan Schmidt break; 9578da6d581SJan Schmidt if (ret) { 9588da6d581SJan Schmidt ret = 0; 9598da6d581SJan Schmidt break; 9608da6d581SJan Schmidt } 9618da6d581SJan Schmidt 9628da6d581SJan Schmidt slot = path->slots[0]; 9638da6d581SJan Schmidt leaf = path->nodes[0]; 9648da6d581SJan Schmidt btrfs_item_key_to_cpu(leaf, &key, slot); 9658da6d581SJan Schmidt 9668da6d581SJan Schmidt if (key.objectid != bytenr) 9678da6d581SJan Schmidt break; 9688da6d581SJan Schmidt if (key.type < BTRFS_TREE_BLOCK_REF_KEY) 9698da6d581SJan Schmidt continue; 9708da6d581SJan Schmidt if (key.type > BTRFS_SHARED_DATA_REF_KEY) 9718da6d581SJan Schmidt break; 9728da6d581SJan Schmidt 9738da6d581SJan Schmidt switch (key.type) { 9748da6d581SJan Schmidt case BTRFS_SHARED_BLOCK_REF_KEY: 97586d5f994SEdmund Nadolski /* SHARED DIRECT METADATA backref */ 97686d5f994SEdmund Nadolski ret = add_direct_ref(preftrees, info_level + 1, 97786d5f994SEdmund Nadolski key.offset, bytenr, 1, 97886d5f994SEdmund Nadolski GFP_NOFS); 9798da6d581SJan Schmidt break; 9808da6d581SJan Schmidt case BTRFS_SHARED_DATA_REF_KEY: { 98186d5f994SEdmund Nadolski /* SHARED DIRECT FULL backref */ 9828da6d581SJan Schmidt struct btrfs_shared_data_ref *sdref; 9838da6d581SJan Schmidt int count; 9848da6d581SJan Schmidt 9858da6d581SJan Schmidt sdref = btrfs_item_ptr(leaf, slot, 9868da6d581SJan Schmidt struct btrfs_shared_data_ref); 9878da6d581SJan Schmidt count = btrfs_shared_data_ref_count(leaf, sdref); 98886d5f994SEdmund Nadolski ret = add_direct_ref(preftrees, 0, key.offset, bytenr, 98986d5f994SEdmund Nadolski count, GFP_NOFS); 9908da6d581SJan Schmidt break; 9918da6d581SJan Schmidt } 9928da6d581SJan Schmidt case BTRFS_TREE_BLOCK_REF_KEY: 99386d5f994SEdmund Nadolski /* NORMAL INDIRECT METADATA backref */ 99486d5f994SEdmund Nadolski ret = add_indirect_ref(preftrees, key.offset, NULL, 99586d5f994SEdmund Nadolski info_level + 1, bytenr, 1, 99686d5f994SEdmund Nadolski GFP_NOFS); 9978da6d581SJan Schmidt break; 9988da6d581SJan Schmidt case BTRFS_EXTENT_DATA_REF_KEY: { 99986d5f994SEdmund Nadolski /* NORMAL INDIRECT DATA backref */ 10008da6d581SJan Schmidt struct btrfs_extent_data_ref *dref; 10018da6d581SJan Schmidt int count; 10028da6d581SJan Schmidt u64 root; 10038da6d581SJan Schmidt 10048da6d581SJan Schmidt dref = btrfs_item_ptr(leaf, slot, 10058da6d581SJan Schmidt struct btrfs_extent_data_ref); 10068da6d581SJan Schmidt count = btrfs_extent_data_ref_count(leaf, dref); 10078da6d581SJan Schmidt key.objectid = btrfs_extent_data_ref_objectid(leaf, 10088da6d581SJan Schmidt dref); 10098da6d581SJan Schmidt key.type = BTRFS_EXTENT_DATA_KEY; 10108da6d581SJan Schmidt key.offset = btrfs_extent_data_ref_offset(leaf, dref); 1011dc046b10SJosef Bacik 1012dc046b10SJosef Bacik if (inum && key.objectid != inum) { 1013dc046b10SJosef Bacik ret = BACKREF_FOUND_SHARED; 1014dc046b10SJosef Bacik break; 1015dc046b10SJosef Bacik } 1016dc046b10SJosef Bacik 10178da6d581SJan Schmidt root = btrfs_extent_data_ref_root(leaf, dref); 101886d5f994SEdmund Nadolski ret = add_indirect_ref(preftrees, root, &key, 0, bytenr, 101986d5f994SEdmund Nadolski count, GFP_NOFS); 10208da6d581SJan Schmidt break; 10218da6d581SJan Schmidt } 10228da6d581SJan Schmidt default: 10238da6d581SJan Schmidt WARN_ON(1); 10248da6d581SJan Schmidt } 10251149ab6bSWang Shilong if (ret) 10261149ab6bSWang Shilong return ret; 10271149ab6bSWang Shilong 10288da6d581SJan Schmidt } 10298da6d581SJan Schmidt 10308da6d581SJan Schmidt return ret; 10318da6d581SJan Schmidt } 10328da6d581SJan Schmidt 10338da6d581SJan Schmidt /* 10348da6d581SJan Schmidt * this adds all existing backrefs (inline backrefs, backrefs and delayed 10358da6d581SJan Schmidt * refs) for the given bytenr to the refs list, merges duplicates and resolves 10368da6d581SJan Schmidt * indirect refs to their parent bytenr. 10378da6d581SJan Schmidt * When roots are found, they're added to the roots list 10388da6d581SJan Schmidt * 10392c2ed5aaSMark Fasheh * NOTE: This can return values > 0 10402c2ed5aaSMark Fasheh * 1041de47c9d3SEdmund Nadolski * If time_seq is set to SEQ_LAST, it will not search delayed_refs, and behave 104221633fc6SQu Wenruo * much like trans == NULL case, the difference only lies in it will not 104321633fc6SQu Wenruo * commit root. 104421633fc6SQu Wenruo * The special case is for qgroup to search roots in commit_transaction(). 104521633fc6SQu Wenruo * 10468da6d581SJan Schmidt * FIXME some caching might speed things up 10478da6d581SJan Schmidt */ 10488da6d581SJan Schmidt static int find_parent_nodes(struct btrfs_trans_handle *trans, 10498da6d581SJan Schmidt struct btrfs_fs_info *fs_info, u64 bytenr, 1050097b8a7cSJan Schmidt u64 time_seq, struct ulist *refs, 1051dc046b10SJosef Bacik struct ulist *roots, const u64 *extent_item_pos, 1052f6954245SEdmund Nadolski u64 root_objectid, u64 inum) 10538da6d581SJan Schmidt { 10548da6d581SJan Schmidt struct btrfs_key key; 10558da6d581SJan Schmidt struct btrfs_path *path; 10568da6d581SJan Schmidt struct btrfs_delayed_ref_root *delayed_refs = NULL; 1057d3b01064SLi Zefan struct btrfs_delayed_ref_head *head; 10588da6d581SJan Schmidt int info_level = 0; 10598da6d581SJan Schmidt int ret; 1060e0c476b1SJeff Mahoney struct prelim_ref *ref; 106186d5f994SEdmund Nadolski struct rb_node *node; 1062f05c4746SWang Shilong struct extent_inode_elem *eie = NULL; 106386d5f994SEdmund Nadolski /* total of both direct AND indirect refs! */ 106444853868SJosef Bacik u64 total_refs = 0; 106586d5f994SEdmund Nadolski struct preftrees preftrees = { 106686d5f994SEdmund Nadolski .direct = PREFTREE_INIT, 106786d5f994SEdmund Nadolski .indirect = PREFTREE_INIT, 106886d5f994SEdmund Nadolski .indirect_missing_keys = PREFTREE_INIT 106986d5f994SEdmund Nadolski }; 10708da6d581SJan Schmidt 10718da6d581SJan Schmidt key.objectid = bytenr; 10728da6d581SJan Schmidt key.offset = (u64)-1; 1073261c84b6SJosef Bacik if (btrfs_fs_incompat(fs_info, SKINNY_METADATA)) 1074261c84b6SJosef Bacik key.type = BTRFS_METADATA_ITEM_KEY; 1075261c84b6SJosef Bacik else 1076261c84b6SJosef Bacik key.type = BTRFS_EXTENT_ITEM_KEY; 10778da6d581SJan Schmidt 10788da6d581SJan Schmidt path = btrfs_alloc_path(); 10798da6d581SJan Schmidt if (!path) 10808da6d581SJan Schmidt return -ENOMEM; 1081e84752d4SWang Shilong if (!trans) { 1082da61d31aSJosef Bacik path->search_commit_root = 1; 1083e84752d4SWang Shilong path->skip_locking = 1; 1084e84752d4SWang Shilong } 10858da6d581SJan Schmidt 1086de47c9d3SEdmund Nadolski if (time_seq == SEQ_LAST) 108721633fc6SQu Wenruo path->skip_locking = 1; 108821633fc6SQu Wenruo 10898da6d581SJan Schmidt /* 10908da6d581SJan Schmidt * grab both a lock on the path and a lock on the delayed ref head. 10918da6d581SJan Schmidt * We need both to get a consistent picture of how the refs look 10928da6d581SJan Schmidt * at a specified point in time 10938da6d581SJan Schmidt */ 10948da6d581SJan Schmidt again: 1095d3b01064SLi Zefan head = NULL; 1096d3b01064SLi Zefan 10978da6d581SJan Schmidt ret = btrfs_search_slot(trans, fs_info->extent_root, &key, path, 0, 0); 10988da6d581SJan Schmidt if (ret < 0) 10998da6d581SJan Schmidt goto out; 11008da6d581SJan Schmidt BUG_ON(ret == 0); 11018da6d581SJan Schmidt 1102faa2dbf0SJosef Bacik #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS 110321633fc6SQu Wenruo if (trans && likely(trans->type != __TRANS_DUMMY) && 1104de47c9d3SEdmund Nadolski time_seq != SEQ_LAST) { 1105faa2dbf0SJosef Bacik #else 1106de47c9d3SEdmund Nadolski if (trans && time_seq != SEQ_LAST) { 1107faa2dbf0SJosef Bacik #endif 11088da6d581SJan Schmidt /* 11097a3ae2f8SJan Schmidt * look if there are updates for this ref queued and lock the 11107a3ae2f8SJan Schmidt * head 11118da6d581SJan Schmidt */ 11128da6d581SJan Schmidt delayed_refs = &trans->transaction->delayed_refs; 11138da6d581SJan Schmidt spin_lock(&delayed_refs->lock); 1114f72ad18eSLiu Bo head = btrfs_find_delayed_ref_head(delayed_refs, bytenr); 11158da6d581SJan Schmidt if (head) { 11168da6d581SJan Schmidt if (!mutex_trylock(&head->mutex)) { 11176df8cdf5SElena Reshetova refcount_inc(&head->node.refs); 11188da6d581SJan Schmidt spin_unlock(&delayed_refs->lock); 11198da6d581SJan Schmidt 11208da6d581SJan Schmidt btrfs_release_path(path); 11218da6d581SJan Schmidt 11228da6d581SJan Schmidt /* 11238da6d581SJan Schmidt * Mutex was contended, block until it's 11248da6d581SJan Schmidt * released and try again 11258da6d581SJan Schmidt */ 11268da6d581SJan Schmidt mutex_lock(&head->mutex); 11278da6d581SJan Schmidt mutex_unlock(&head->mutex); 11288da6d581SJan Schmidt btrfs_put_delayed_ref(&head->node); 11298da6d581SJan Schmidt goto again; 11308da6d581SJan Schmidt } 1131d7df2c79SJosef Bacik spin_unlock(&delayed_refs->lock); 113286d5f994SEdmund Nadolski ret = add_delayed_refs(head, time_seq, &preftrees, 113386d5f994SEdmund Nadolski &total_refs, inum); 1134155725c9SJan Schmidt mutex_unlock(&head->mutex); 1135d7df2c79SJosef Bacik if (ret) 11368da6d581SJan Schmidt goto out; 1137d7df2c79SJosef Bacik } else { 11388da6d581SJan Schmidt spin_unlock(&delayed_refs->lock); 11397a3ae2f8SJan Schmidt } 1140d7df2c79SJosef Bacik } 11418da6d581SJan Schmidt 11428da6d581SJan Schmidt if (path->slots[0]) { 11438da6d581SJan Schmidt struct extent_buffer *leaf; 11448da6d581SJan Schmidt int slot; 11458da6d581SJan Schmidt 1146dadcaf78SJan Schmidt path->slots[0]--; 11478da6d581SJan Schmidt leaf = path->nodes[0]; 1148dadcaf78SJan Schmidt slot = path->slots[0]; 11498da6d581SJan Schmidt btrfs_item_key_to_cpu(leaf, &key, slot); 11508da6d581SJan Schmidt if (key.objectid == bytenr && 1151261c84b6SJosef Bacik (key.type == BTRFS_EXTENT_ITEM_KEY || 1152261c84b6SJosef Bacik key.type == BTRFS_METADATA_ITEM_KEY)) { 1153e0c476b1SJeff Mahoney ret = add_inline_refs(path, bytenr, &info_level, 115486d5f994SEdmund Nadolski &preftrees, &total_refs, inum); 11558da6d581SJan Schmidt if (ret) 11568da6d581SJan Schmidt goto out; 1157e0c476b1SJeff Mahoney ret = add_keyed_refs(fs_info, path, bytenr, info_level, 115886d5f994SEdmund Nadolski &preftrees, inum); 11598da6d581SJan Schmidt if (ret) 11608da6d581SJan Schmidt goto out; 11618da6d581SJan Schmidt } 11628da6d581SJan Schmidt } 116386d5f994SEdmund Nadolski 11648da6d581SJan Schmidt btrfs_release_path(path); 11658da6d581SJan Schmidt 116686d5f994SEdmund Nadolski ret = add_missing_keys(fs_info, &preftrees); 1167d5c88b73SJan Schmidt if (ret) 1168d5c88b73SJan Schmidt goto out; 1169d5c88b73SJan Schmidt 117086d5f994SEdmund Nadolski WARN_ON(!RB_EMPTY_ROOT(&preftrees.indirect_missing_keys.root)); 11718da6d581SJan Schmidt 117286d5f994SEdmund Nadolski ret = resolve_indirect_refs(fs_info, path, time_seq, &preftrees, 1173dc046b10SJosef Bacik extent_item_pos, total_refs, 1174dc046b10SJosef Bacik root_objectid); 11758da6d581SJan Schmidt if (ret) 11768da6d581SJan Schmidt goto out; 11778da6d581SJan Schmidt 117886d5f994SEdmund Nadolski WARN_ON(!RB_EMPTY_ROOT(&preftrees.indirect.root)); 11798da6d581SJan Schmidt 118086d5f994SEdmund Nadolski /* 118186d5f994SEdmund Nadolski * This walks the tree of merged and resolved refs. Tree blocks are 118286d5f994SEdmund Nadolski * read in as needed. Unique entries are added to the ulist, and 118386d5f994SEdmund Nadolski * the list of found roots is updated. 118486d5f994SEdmund Nadolski * 118586d5f994SEdmund Nadolski * We release the entire tree in one go before returning. 118686d5f994SEdmund Nadolski */ 118786d5f994SEdmund Nadolski node = rb_first(&preftrees.direct.root); 118886d5f994SEdmund Nadolski while (node) { 118986d5f994SEdmund Nadolski ref = rb_entry(node, struct prelim_ref, rbnode); 119086d5f994SEdmund Nadolski node = rb_next(&ref->rbnode); 11916c1500f2SJulia Lawall WARN_ON(ref->count < 0); 119298cfee21SWang Shilong if (roots && ref->count && ref->root_id && ref->parent == 0) { 1193dc046b10SJosef Bacik if (root_objectid && ref->root_id != root_objectid) { 1194dc046b10SJosef Bacik ret = BACKREF_FOUND_SHARED; 1195dc046b10SJosef Bacik goto out; 1196dc046b10SJosef Bacik } 1197dc046b10SJosef Bacik 11988da6d581SJan Schmidt /* no parent == root of tree */ 11998da6d581SJan Schmidt ret = ulist_add(roots, ref->root_id, 0, GFP_NOFS); 1200f1723939SWang Shilong if (ret < 0) 1201f1723939SWang Shilong goto out; 12028da6d581SJan Schmidt } 12038da6d581SJan Schmidt if (ref->count && ref->parent) { 12048a56457fSJosef Bacik if (extent_item_pos && !ref->inode_list && 12058a56457fSJosef Bacik ref->level == 0) { 1206976b1908SJan Schmidt struct extent_buffer *eb; 1207707e8a07SDavid Sterba 12082ff7e61eSJeff Mahoney eb = read_tree_block(fs_info, ref->parent, 0); 120964c043deSLiu Bo if (IS_ERR(eb)) { 121064c043deSLiu Bo ret = PTR_ERR(eb); 121164c043deSLiu Bo goto out; 121264c043deSLiu Bo } else if (!extent_buffer_uptodate(eb)) { 1213416bc658SJosef Bacik free_extent_buffer(eb); 1214c16c2e2eSWang Shilong ret = -EIO; 1215c16c2e2eSWang Shilong goto out; 1216416bc658SJosef Bacik } 12176f7ff6d7SFilipe Manana btrfs_tree_read_lock(eb); 12186f7ff6d7SFilipe Manana btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK); 1219976b1908SJan Schmidt ret = find_extent_in_eb(eb, bytenr, 1220976b1908SJan Schmidt *extent_item_pos, &eie); 12216f7ff6d7SFilipe Manana btrfs_tree_read_unlock_blocking(eb); 1222976b1908SJan Schmidt free_extent_buffer(eb); 1223f5929cd8SFilipe David Borba Manana if (ret < 0) 1224f5929cd8SFilipe David Borba Manana goto out; 1225f5929cd8SFilipe David Borba Manana ref->inode_list = eie; 1226976b1908SJan Schmidt } 12274eb1f66dSTakashi Iwai ret = ulist_add_merge_ptr(refs, ref->parent, 12284eb1f66dSTakashi Iwai ref->inode_list, 12294eb1f66dSTakashi Iwai (void **)&eie, GFP_NOFS); 1230f1723939SWang Shilong if (ret < 0) 1231f1723939SWang Shilong goto out; 12323301958bSJan Schmidt if (!ret && extent_item_pos) { 12333301958bSJan Schmidt /* 12343301958bSJan Schmidt * we've recorded that parent, so we must extend 12353301958bSJan Schmidt * its inode list here 12363301958bSJan Schmidt */ 12373301958bSJan Schmidt BUG_ON(!eie); 12383301958bSJan Schmidt while (eie->next) 12393301958bSJan Schmidt eie = eie->next; 12403301958bSJan Schmidt eie->next = ref->inode_list; 12413301958bSJan Schmidt } 1242f05c4746SWang Shilong eie = NULL; 12438da6d581SJan Schmidt } 12448da6d581SJan Schmidt } 12458da6d581SJan Schmidt 12468da6d581SJan Schmidt out: 12478da6d581SJan Schmidt btrfs_free_path(path); 124886d5f994SEdmund Nadolski 124986d5f994SEdmund Nadolski prelim_release(&preftrees.direct); 125086d5f994SEdmund Nadolski prelim_release(&preftrees.indirect); 125186d5f994SEdmund Nadolski prelim_release(&preftrees.indirect_missing_keys); 125286d5f994SEdmund Nadolski 1253f05c4746SWang Shilong if (ret < 0) 1254f05c4746SWang Shilong free_inode_elem_list(eie); 12558da6d581SJan Schmidt return ret; 12568da6d581SJan Schmidt } 12578da6d581SJan Schmidt 1258976b1908SJan Schmidt static void free_leaf_list(struct ulist *blocks) 1259976b1908SJan Schmidt { 1260976b1908SJan Schmidt struct ulist_node *node = NULL; 1261976b1908SJan Schmidt struct extent_inode_elem *eie; 1262976b1908SJan Schmidt struct ulist_iterator uiter; 1263976b1908SJan Schmidt 1264976b1908SJan Schmidt ULIST_ITER_INIT(&uiter); 1265976b1908SJan Schmidt while ((node = ulist_next(blocks, &uiter))) { 1266976b1908SJan Schmidt if (!node->aux) 1267976b1908SJan Schmidt continue; 12684dae077aSJeff Mahoney eie = unode_aux_to_inode_list(node); 1269f05c4746SWang Shilong free_inode_elem_list(eie); 1270976b1908SJan Schmidt node->aux = 0; 1271976b1908SJan Schmidt } 1272976b1908SJan Schmidt 1273976b1908SJan Schmidt ulist_free(blocks); 1274976b1908SJan Schmidt } 1275976b1908SJan Schmidt 12768da6d581SJan Schmidt /* 12778da6d581SJan Schmidt * Finds all leafs with a reference to the specified combination of bytenr and 12788da6d581SJan Schmidt * offset. key_list_head will point to a list of corresponding keys (caller must 12798da6d581SJan Schmidt * free each list element). The leafs will be stored in the leafs ulist, which 12808da6d581SJan Schmidt * must be freed with ulist_free. 12818da6d581SJan Schmidt * 12828da6d581SJan Schmidt * returns 0 on success, <0 on error 12838da6d581SJan Schmidt */ 12848da6d581SJan Schmidt static int btrfs_find_all_leafs(struct btrfs_trans_handle *trans, 12858da6d581SJan Schmidt struct btrfs_fs_info *fs_info, u64 bytenr, 1286097b8a7cSJan Schmidt u64 time_seq, struct ulist **leafs, 1287976b1908SJan Schmidt const u64 *extent_item_pos) 12888da6d581SJan Schmidt { 12898da6d581SJan Schmidt int ret; 12908da6d581SJan Schmidt 12918da6d581SJan Schmidt *leafs = ulist_alloc(GFP_NOFS); 129298cfee21SWang Shilong if (!*leafs) 12938da6d581SJan Schmidt return -ENOMEM; 12948da6d581SJan Schmidt 1295afce772eSLu Fengqi ret = find_parent_nodes(trans, fs_info, bytenr, time_seq, 1296f6954245SEdmund Nadolski *leafs, NULL, extent_item_pos, 0, 0); 12978da6d581SJan Schmidt if (ret < 0 && ret != -ENOENT) { 1298976b1908SJan Schmidt free_leaf_list(*leafs); 12998da6d581SJan Schmidt return ret; 13008da6d581SJan Schmidt } 13018da6d581SJan Schmidt 13028da6d581SJan Schmidt return 0; 13038da6d581SJan Schmidt } 13048da6d581SJan Schmidt 13058da6d581SJan Schmidt /* 13068da6d581SJan Schmidt * walk all backrefs for a given extent to find all roots that reference this 13078da6d581SJan Schmidt * extent. Walking a backref means finding all extents that reference this 13088da6d581SJan Schmidt * extent and in turn walk the backrefs of those, too. Naturally this is a 13098da6d581SJan Schmidt * recursive process, but here it is implemented in an iterative fashion: We 13108da6d581SJan Schmidt * find all referencing extents for the extent in question and put them on a 13118da6d581SJan Schmidt * list. In turn, we find all referencing extents for those, further appending 13128da6d581SJan Schmidt * to the list. The way we iterate the list allows adding more elements after 13138da6d581SJan Schmidt * the current while iterating. The process stops when we reach the end of the 13148da6d581SJan Schmidt * list. Found roots are added to the roots list. 13158da6d581SJan Schmidt * 13168da6d581SJan Schmidt * returns 0 on success, < 0 on error. 13178da6d581SJan Schmidt */ 1318e0c476b1SJeff Mahoney static int btrfs_find_all_roots_safe(struct btrfs_trans_handle *trans, 13198da6d581SJan Schmidt struct btrfs_fs_info *fs_info, u64 bytenr, 1320097b8a7cSJan Schmidt u64 time_seq, struct ulist **roots) 13218da6d581SJan Schmidt { 13228da6d581SJan Schmidt struct ulist *tmp; 13238da6d581SJan Schmidt struct ulist_node *node = NULL; 1324cd1b413cSJan Schmidt struct ulist_iterator uiter; 13258da6d581SJan Schmidt int ret; 13268da6d581SJan Schmidt 13278da6d581SJan Schmidt tmp = ulist_alloc(GFP_NOFS); 13288da6d581SJan Schmidt if (!tmp) 13298da6d581SJan Schmidt return -ENOMEM; 13308da6d581SJan Schmidt *roots = ulist_alloc(GFP_NOFS); 13318da6d581SJan Schmidt if (!*roots) { 13328da6d581SJan Schmidt ulist_free(tmp); 13338da6d581SJan Schmidt return -ENOMEM; 13348da6d581SJan Schmidt } 13358da6d581SJan Schmidt 1336cd1b413cSJan Schmidt ULIST_ITER_INIT(&uiter); 13378da6d581SJan Schmidt while (1) { 1338afce772eSLu Fengqi ret = find_parent_nodes(trans, fs_info, bytenr, time_seq, 1339f6954245SEdmund Nadolski tmp, *roots, NULL, 0, 0); 13408da6d581SJan Schmidt if (ret < 0 && ret != -ENOENT) { 13418da6d581SJan Schmidt ulist_free(tmp); 13428da6d581SJan Schmidt ulist_free(*roots); 13438da6d581SJan Schmidt return ret; 13448da6d581SJan Schmidt } 1345cd1b413cSJan Schmidt node = ulist_next(tmp, &uiter); 13468da6d581SJan Schmidt if (!node) 13478da6d581SJan Schmidt break; 13488da6d581SJan Schmidt bytenr = node->val; 1349bca1a290SWang Shilong cond_resched(); 13508da6d581SJan Schmidt } 13518da6d581SJan Schmidt 13528da6d581SJan Schmidt ulist_free(tmp); 13538da6d581SJan Schmidt return 0; 13548da6d581SJan Schmidt } 13558da6d581SJan Schmidt 13569e351cc8SJosef Bacik int btrfs_find_all_roots(struct btrfs_trans_handle *trans, 13579e351cc8SJosef Bacik struct btrfs_fs_info *fs_info, u64 bytenr, 13589e351cc8SJosef Bacik u64 time_seq, struct ulist **roots) 13599e351cc8SJosef Bacik { 13609e351cc8SJosef Bacik int ret; 13619e351cc8SJosef Bacik 13629e351cc8SJosef Bacik if (!trans) 13639e351cc8SJosef Bacik down_read(&fs_info->commit_root_sem); 1364e0c476b1SJeff Mahoney ret = btrfs_find_all_roots_safe(trans, fs_info, bytenr, 1365e0c476b1SJeff Mahoney time_seq, roots); 13669e351cc8SJosef Bacik if (!trans) 13679e351cc8SJosef Bacik up_read(&fs_info->commit_root_sem); 13689e351cc8SJosef Bacik return ret; 13699e351cc8SJosef Bacik } 13709e351cc8SJosef Bacik 13712c2ed5aaSMark Fasheh /** 13722c2ed5aaSMark Fasheh * btrfs_check_shared - tell us whether an extent is shared 13732c2ed5aaSMark Fasheh * 13742c2ed5aaSMark Fasheh * btrfs_check_shared uses the backref walking code but will short 13752c2ed5aaSMark Fasheh * circuit as soon as it finds a root or inode that doesn't match the 13762c2ed5aaSMark Fasheh * one passed in. This provides a significant performance benefit for 13772c2ed5aaSMark Fasheh * callers (such as fiemap) which want to know whether the extent is 13782c2ed5aaSMark Fasheh * shared but do not need a ref count. 13792c2ed5aaSMark Fasheh * 1380bb739cf0SEdmund Nadolski * This attempts to allocate a transaction in order to account for 1381bb739cf0SEdmund Nadolski * delayed refs, but continues on even when the alloc fails. 1382bb739cf0SEdmund Nadolski * 13832c2ed5aaSMark Fasheh * Return: 0 if extent is not shared, 1 if it is shared, < 0 on error. 13842c2ed5aaSMark Fasheh */ 1385bb739cf0SEdmund Nadolski int btrfs_check_shared(struct btrfs_root *root, u64 inum, u64 bytenr) 1386dc046b10SJosef Bacik { 1387bb739cf0SEdmund Nadolski struct btrfs_fs_info *fs_info = root->fs_info; 1388bb739cf0SEdmund Nadolski struct btrfs_trans_handle *trans; 1389dc046b10SJosef Bacik struct ulist *tmp = NULL; 1390dc046b10SJosef Bacik struct ulist *roots = NULL; 1391dc046b10SJosef Bacik struct ulist_iterator uiter; 1392dc046b10SJosef Bacik struct ulist_node *node; 13933284da7bSDavid Sterba struct seq_list elem = SEQ_LIST_INIT(elem); 1394dc046b10SJosef Bacik int ret = 0; 1395dc046b10SJosef Bacik 1396dc046b10SJosef Bacik tmp = ulist_alloc(GFP_NOFS); 1397dc046b10SJosef Bacik roots = ulist_alloc(GFP_NOFS); 1398dc046b10SJosef Bacik if (!tmp || !roots) { 1399dc046b10SJosef Bacik ulist_free(tmp); 1400dc046b10SJosef Bacik ulist_free(roots); 1401dc046b10SJosef Bacik return -ENOMEM; 1402dc046b10SJosef Bacik } 1403dc046b10SJosef Bacik 1404bb739cf0SEdmund Nadolski trans = btrfs_join_transaction(root); 1405bb739cf0SEdmund Nadolski if (IS_ERR(trans)) { 1406bb739cf0SEdmund Nadolski trans = NULL; 1407dc046b10SJosef Bacik down_read(&fs_info->commit_root_sem); 1408bb739cf0SEdmund Nadolski } else { 1409bb739cf0SEdmund Nadolski btrfs_get_tree_mod_seq(fs_info, &elem); 1410bb739cf0SEdmund Nadolski } 1411bb739cf0SEdmund Nadolski 1412dc046b10SJosef Bacik ULIST_ITER_INIT(&uiter); 1413dc046b10SJosef Bacik while (1) { 1414dc046b10SJosef Bacik ret = find_parent_nodes(trans, fs_info, bytenr, elem.seq, tmp, 1415f6954245SEdmund Nadolski roots, NULL, root->objectid, inum); 1416dc046b10SJosef Bacik if (ret == BACKREF_FOUND_SHARED) { 14172c2ed5aaSMark Fasheh /* this is the only condition under which we return 1 */ 1418dc046b10SJosef Bacik ret = 1; 1419dc046b10SJosef Bacik break; 1420dc046b10SJosef Bacik } 1421dc046b10SJosef Bacik if (ret < 0 && ret != -ENOENT) 1422dc046b10SJosef Bacik break; 14232c2ed5aaSMark Fasheh ret = 0; 1424dc046b10SJosef Bacik node = ulist_next(tmp, &uiter); 1425dc046b10SJosef Bacik if (!node) 1426dc046b10SJosef Bacik break; 1427dc046b10SJosef Bacik bytenr = node->val; 1428dc046b10SJosef Bacik cond_resched(); 1429dc046b10SJosef Bacik } 1430bb739cf0SEdmund Nadolski 1431bb739cf0SEdmund Nadolski if (trans) { 1432dc046b10SJosef Bacik btrfs_put_tree_mod_seq(fs_info, &elem); 1433bb739cf0SEdmund Nadolski btrfs_end_transaction(trans); 1434bb739cf0SEdmund Nadolski } else { 1435dc046b10SJosef Bacik up_read(&fs_info->commit_root_sem); 1436bb739cf0SEdmund Nadolski } 1437dc046b10SJosef Bacik ulist_free(tmp); 1438dc046b10SJosef Bacik ulist_free(roots); 1439dc046b10SJosef Bacik return ret; 1440dc046b10SJosef Bacik } 1441dc046b10SJosef Bacik 1442f186373fSMark Fasheh int btrfs_find_one_extref(struct btrfs_root *root, u64 inode_objectid, 1443f186373fSMark Fasheh u64 start_off, struct btrfs_path *path, 1444f186373fSMark Fasheh struct btrfs_inode_extref **ret_extref, 1445f186373fSMark Fasheh u64 *found_off) 1446f186373fSMark Fasheh { 1447f186373fSMark Fasheh int ret, slot; 1448f186373fSMark Fasheh struct btrfs_key key; 1449f186373fSMark Fasheh struct btrfs_key found_key; 1450f186373fSMark Fasheh struct btrfs_inode_extref *extref; 145173980becSJeff Mahoney const struct extent_buffer *leaf; 1452f186373fSMark Fasheh unsigned long ptr; 1453f186373fSMark Fasheh 1454f186373fSMark Fasheh key.objectid = inode_objectid; 1455962a298fSDavid Sterba key.type = BTRFS_INODE_EXTREF_KEY; 1456f186373fSMark Fasheh key.offset = start_off; 1457f186373fSMark Fasheh 1458f186373fSMark Fasheh ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); 1459f186373fSMark Fasheh if (ret < 0) 1460f186373fSMark Fasheh return ret; 1461f186373fSMark Fasheh 1462f186373fSMark Fasheh while (1) { 1463f186373fSMark Fasheh leaf = path->nodes[0]; 1464f186373fSMark Fasheh slot = path->slots[0]; 1465f186373fSMark Fasheh if (slot >= btrfs_header_nritems(leaf)) { 1466f186373fSMark Fasheh /* 1467f186373fSMark Fasheh * If the item at offset is not found, 1468f186373fSMark Fasheh * btrfs_search_slot will point us to the slot 1469f186373fSMark Fasheh * where it should be inserted. In our case 1470f186373fSMark Fasheh * that will be the slot directly before the 1471f186373fSMark Fasheh * next INODE_REF_KEY_V2 item. In the case 1472f186373fSMark Fasheh * that we're pointing to the last slot in a 1473f186373fSMark Fasheh * leaf, we must move one leaf over. 1474f186373fSMark Fasheh */ 1475f186373fSMark Fasheh ret = btrfs_next_leaf(root, path); 1476f186373fSMark Fasheh if (ret) { 1477f186373fSMark Fasheh if (ret >= 1) 1478f186373fSMark Fasheh ret = -ENOENT; 1479f186373fSMark Fasheh break; 1480f186373fSMark Fasheh } 1481f186373fSMark Fasheh continue; 1482f186373fSMark Fasheh } 1483f186373fSMark Fasheh 1484f186373fSMark Fasheh btrfs_item_key_to_cpu(leaf, &found_key, slot); 1485f186373fSMark Fasheh 1486f186373fSMark Fasheh /* 1487f186373fSMark Fasheh * Check that we're still looking at an extended ref key for 1488f186373fSMark Fasheh * this particular objectid. If we have different 1489f186373fSMark Fasheh * objectid or type then there are no more to be found 1490f186373fSMark Fasheh * in the tree and we can exit. 1491f186373fSMark Fasheh */ 1492f186373fSMark Fasheh ret = -ENOENT; 1493f186373fSMark Fasheh if (found_key.objectid != inode_objectid) 1494f186373fSMark Fasheh break; 1495962a298fSDavid Sterba if (found_key.type != BTRFS_INODE_EXTREF_KEY) 1496f186373fSMark Fasheh break; 1497f186373fSMark Fasheh 1498f186373fSMark Fasheh ret = 0; 1499f186373fSMark Fasheh ptr = btrfs_item_ptr_offset(leaf, path->slots[0]); 1500f186373fSMark Fasheh extref = (struct btrfs_inode_extref *)ptr; 1501f186373fSMark Fasheh *ret_extref = extref; 1502f186373fSMark Fasheh if (found_off) 1503f186373fSMark Fasheh *found_off = found_key.offset; 1504f186373fSMark Fasheh break; 1505f186373fSMark Fasheh } 1506f186373fSMark Fasheh 1507f186373fSMark Fasheh return ret; 1508f186373fSMark Fasheh } 1509f186373fSMark Fasheh 151048a3b636SEric Sandeen /* 151148a3b636SEric Sandeen * this iterates to turn a name (from iref/extref) into a full filesystem path. 151248a3b636SEric Sandeen * Elements of the path are separated by '/' and the path is guaranteed to be 151348a3b636SEric Sandeen * 0-terminated. the path is only given within the current file system. 151448a3b636SEric Sandeen * Therefore, it never starts with a '/'. the caller is responsible to provide 151548a3b636SEric Sandeen * "size" bytes in "dest". the dest buffer will be filled backwards. finally, 151648a3b636SEric Sandeen * the start point of the resulting string is returned. this pointer is within 151748a3b636SEric Sandeen * dest, normally. 151848a3b636SEric Sandeen * in case the path buffer would overflow, the pointer is decremented further 151948a3b636SEric Sandeen * as if output was written to the buffer, though no more output is actually 152048a3b636SEric Sandeen * generated. that way, the caller can determine how much space would be 152148a3b636SEric Sandeen * required for the path to fit into the buffer. in that case, the returned 152248a3b636SEric Sandeen * value will be smaller than dest. callers must check this! 152348a3b636SEric Sandeen */ 152496b5bd77SJan Schmidt char *btrfs_ref_to_path(struct btrfs_root *fs_root, struct btrfs_path *path, 1525d24bec3aSMark Fasheh u32 name_len, unsigned long name_off, 1526a542ad1bSJan Schmidt struct extent_buffer *eb_in, u64 parent, 1527a542ad1bSJan Schmidt char *dest, u32 size) 1528a542ad1bSJan Schmidt { 1529a542ad1bSJan Schmidt int slot; 1530a542ad1bSJan Schmidt u64 next_inum; 1531a542ad1bSJan Schmidt int ret; 1532661bec6bSGabriel de Perthuis s64 bytes_left = ((s64)size) - 1; 1533a542ad1bSJan Schmidt struct extent_buffer *eb = eb_in; 1534a542ad1bSJan Schmidt struct btrfs_key found_key; 1535b916a59aSJan Schmidt int leave_spinning = path->leave_spinning; 1536d24bec3aSMark Fasheh struct btrfs_inode_ref *iref; 1537a542ad1bSJan Schmidt 1538a542ad1bSJan Schmidt if (bytes_left >= 0) 1539a542ad1bSJan Schmidt dest[bytes_left] = '\0'; 1540a542ad1bSJan Schmidt 1541b916a59aSJan Schmidt path->leave_spinning = 1; 1542a542ad1bSJan Schmidt while (1) { 1543d24bec3aSMark Fasheh bytes_left -= name_len; 1544a542ad1bSJan Schmidt if (bytes_left >= 0) 1545a542ad1bSJan Schmidt read_extent_buffer(eb, dest + bytes_left, 1546d24bec3aSMark Fasheh name_off, name_len); 1547b916a59aSJan Schmidt if (eb != eb_in) { 15480c0fe3b0SFilipe Manana if (!path->skip_locking) 1549b916a59aSJan Schmidt btrfs_tree_read_unlock_blocking(eb); 1550a542ad1bSJan Schmidt free_extent_buffer(eb); 1551b916a59aSJan Schmidt } 1552c234a24dSDavid Sterba ret = btrfs_find_item(fs_root, path, parent, 0, 1553c234a24dSDavid Sterba BTRFS_INODE_REF_KEY, &found_key); 15548f24b496SJan Schmidt if (ret > 0) 15558f24b496SJan Schmidt ret = -ENOENT; 1556a542ad1bSJan Schmidt if (ret) 1557a542ad1bSJan Schmidt break; 1558d24bec3aSMark Fasheh 1559a542ad1bSJan Schmidt next_inum = found_key.offset; 1560a542ad1bSJan Schmidt 1561a542ad1bSJan Schmidt /* regular exit ahead */ 1562a542ad1bSJan Schmidt if (parent == next_inum) 1563a542ad1bSJan Schmidt break; 1564a542ad1bSJan Schmidt 1565a542ad1bSJan Schmidt slot = path->slots[0]; 1566a542ad1bSJan Schmidt eb = path->nodes[0]; 1567a542ad1bSJan Schmidt /* make sure we can use eb after releasing the path */ 1568b916a59aSJan Schmidt if (eb != eb_in) { 15690c0fe3b0SFilipe Manana if (!path->skip_locking) 1570b916a59aSJan Schmidt btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK); 15710c0fe3b0SFilipe Manana path->nodes[0] = NULL; 15720c0fe3b0SFilipe Manana path->locks[0] = 0; 1573b916a59aSJan Schmidt } 1574a542ad1bSJan Schmidt btrfs_release_path(path); 1575a542ad1bSJan Schmidt iref = btrfs_item_ptr(eb, slot, struct btrfs_inode_ref); 1576d24bec3aSMark Fasheh 1577d24bec3aSMark Fasheh name_len = btrfs_inode_ref_name_len(eb, iref); 1578d24bec3aSMark Fasheh name_off = (unsigned long)(iref + 1); 1579d24bec3aSMark Fasheh 1580a542ad1bSJan Schmidt parent = next_inum; 1581a542ad1bSJan Schmidt --bytes_left; 1582a542ad1bSJan Schmidt if (bytes_left >= 0) 1583a542ad1bSJan Schmidt dest[bytes_left] = '/'; 1584a542ad1bSJan Schmidt } 1585a542ad1bSJan Schmidt 1586a542ad1bSJan Schmidt btrfs_release_path(path); 1587b916a59aSJan Schmidt path->leave_spinning = leave_spinning; 1588a542ad1bSJan Schmidt 1589a542ad1bSJan Schmidt if (ret) 1590a542ad1bSJan Schmidt return ERR_PTR(ret); 1591a542ad1bSJan Schmidt 1592a542ad1bSJan Schmidt return dest + bytes_left; 1593a542ad1bSJan Schmidt } 1594a542ad1bSJan Schmidt 1595a542ad1bSJan Schmidt /* 1596a542ad1bSJan Schmidt * this makes the path point to (logical EXTENT_ITEM *) 1597a542ad1bSJan Schmidt * returns BTRFS_EXTENT_FLAG_DATA for data, BTRFS_EXTENT_FLAG_TREE_BLOCK for 1598a542ad1bSJan Schmidt * tree blocks and <0 on error. 1599a542ad1bSJan Schmidt */ 1600a542ad1bSJan Schmidt int extent_from_logical(struct btrfs_fs_info *fs_info, u64 logical, 160169917e43SLiu Bo struct btrfs_path *path, struct btrfs_key *found_key, 160269917e43SLiu Bo u64 *flags_ret) 1603a542ad1bSJan Schmidt { 1604a542ad1bSJan Schmidt int ret; 1605a542ad1bSJan Schmidt u64 flags; 1606261c84b6SJosef Bacik u64 size = 0; 1607a542ad1bSJan Schmidt u32 item_size; 160873980becSJeff Mahoney const struct extent_buffer *eb; 1609a542ad1bSJan Schmidt struct btrfs_extent_item *ei; 1610a542ad1bSJan Schmidt struct btrfs_key key; 1611a542ad1bSJan Schmidt 1612261c84b6SJosef Bacik if (btrfs_fs_incompat(fs_info, SKINNY_METADATA)) 1613261c84b6SJosef Bacik key.type = BTRFS_METADATA_ITEM_KEY; 1614261c84b6SJosef Bacik else 1615a542ad1bSJan Schmidt key.type = BTRFS_EXTENT_ITEM_KEY; 1616a542ad1bSJan Schmidt key.objectid = logical; 1617a542ad1bSJan Schmidt key.offset = (u64)-1; 1618a542ad1bSJan Schmidt 1619a542ad1bSJan Schmidt ret = btrfs_search_slot(NULL, fs_info->extent_root, &key, path, 0, 0); 1620a542ad1bSJan Schmidt if (ret < 0) 1621a542ad1bSJan Schmidt return ret; 1622a542ad1bSJan Schmidt 1623850a8cdfSWang Shilong ret = btrfs_previous_extent_item(fs_info->extent_root, path, 0); 1624850a8cdfSWang Shilong if (ret) { 1625850a8cdfSWang Shilong if (ret > 0) 1626580f0a67SJosef Bacik ret = -ENOENT; 1627580f0a67SJosef Bacik return ret; 1628580f0a67SJosef Bacik } 1629850a8cdfSWang Shilong btrfs_item_key_to_cpu(path->nodes[0], found_key, path->slots[0]); 1630261c84b6SJosef Bacik if (found_key->type == BTRFS_METADATA_ITEM_KEY) 1631da17066cSJeff Mahoney size = fs_info->nodesize; 1632261c84b6SJosef Bacik else if (found_key->type == BTRFS_EXTENT_ITEM_KEY) 1633261c84b6SJosef Bacik size = found_key->offset; 1634261c84b6SJosef Bacik 1635580f0a67SJosef Bacik if (found_key->objectid > logical || 1636261c84b6SJosef Bacik found_key->objectid + size <= logical) { 1637ab8d0fc4SJeff Mahoney btrfs_debug(fs_info, 1638ab8d0fc4SJeff Mahoney "logical %llu is not within any extent", logical); 1639a542ad1bSJan Schmidt return -ENOENT; 16404692cf58SJan Schmidt } 1641a542ad1bSJan Schmidt 1642a542ad1bSJan Schmidt eb = path->nodes[0]; 1643a542ad1bSJan Schmidt item_size = btrfs_item_size_nr(eb, path->slots[0]); 1644a542ad1bSJan Schmidt BUG_ON(item_size < sizeof(*ei)); 1645a542ad1bSJan Schmidt 1646a542ad1bSJan Schmidt ei = btrfs_item_ptr(eb, path->slots[0], struct btrfs_extent_item); 1647a542ad1bSJan Schmidt flags = btrfs_extent_flags(eb, ei); 1648a542ad1bSJan Schmidt 1649ab8d0fc4SJeff Mahoney btrfs_debug(fs_info, 1650ab8d0fc4SJeff Mahoney "logical %llu is at position %llu within the extent (%llu EXTENT_ITEM %llu) flags %#llx size %u", 1651c1c9ff7cSGeert Uytterhoeven logical, logical - found_key->objectid, found_key->objectid, 1652c1c9ff7cSGeert Uytterhoeven found_key->offset, flags, item_size); 165369917e43SLiu Bo 165469917e43SLiu Bo WARN_ON(!flags_ret); 165569917e43SLiu Bo if (flags_ret) { 1656a542ad1bSJan Schmidt if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) 165769917e43SLiu Bo *flags_ret = BTRFS_EXTENT_FLAG_TREE_BLOCK; 165869917e43SLiu Bo else if (flags & BTRFS_EXTENT_FLAG_DATA) 165969917e43SLiu Bo *flags_ret = BTRFS_EXTENT_FLAG_DATA; 166069917e43SLiu Bo else 166169917e43SLiu Bo BUG_ON(1); 166269917e43SLiu Bo return 0; 166369917e43SLiu Bo } 1664a542ad1bSJan Schmidt 1665a542ad1bSJan Schmidt return -EIO; 1666a542ad1bSJan Schmidt } 1667a542ad1bSJan Schmidt 1668a542ad1bSJan Schmidt /* 1669a542ad1bSJan Schmidt * helper function to iterate extent inline refs. ptr must point to a 0 value 1670a542ad1bSJan Schmidt * for the first call and may be modified. it is used to track state. 1671a542ad1bSJan Schmidt * if more refs exist, 0 is returned and the next call to 1672e0c476b1SJeff Mahoney * get_extent_inline_ref must pass the modified ptr parameter to get the 1673a542ad1bSJan Schmidt * next ref. after the last ref was processed, 1 is returned. 1674a542ad1bSJan Schmidt * returns <0 on error 1675a542ad1bSJan Schmidt */ 1676e0c476b1SJeff Mahoney static int get_extent_inline_ref(unsigned long *ptr, 167773980becSJeff Mahoney const struct extent_buffer *eb, 167873980becSJeff Mahoney const struct btrfs_key *key, 167973980becSJeff Mahoney const struct btrfs_extent_item *ei, 168073980becSJeff Mahoney u32 item_size, 1681a542ad1bSJan Schmidt struct btrfs_extent_inline_ref **out_eiref, 1682a542ad1bSJan Schmidt int *out_type) 1683a542ad1bSJan Schmidt { 1684a542ad1bSJan Schmidt unsigned long end; 1685a542ad1bSJan Schmidt u64 flags; 1686a542ad1bSJan Schmidt struct btrfs_tree_block_info *info; 1687a542ad1bSJan Schmidt 1688a542ad1bSJan Schmidt if (!*ptr) { 1689a542ad1bSJan Schmidt /* first call */ 1690a542ad1bSJan Schmidt flags = btrfs_extent_flags(eb, ei); 1691a542ad1bSJan Schmidt if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) { 16926eda71d0SLiu Bo if (key->type == BTRFS_METADATA_ITEM_KEY) { 16936eda71d0SLiu Bo /* a skinny metadata extent */ 16946eda71d0SLiu Bo *out_eiref = 16956eda71d0SLiu Bo (struct btrfs_extent_inline_ref *)(ei + 1); 16966eda71d0SLiu Bo } else { 16976eda71d0SLiu Bo WARN_ON(key->type != BTRFS_EXTENT_ITEM_KEY); 1698a542ad1bSJan Schmidt info = (struct btrfs_tree_block_info *)(ei + 1); 1699a542ad1bSJan Schmidt *out_eiref = 1700a542ad1bSJan Schmidt (struct btrfs_extent_inline_ref *)(info + 1); 17016eda71d0SLiu Bo } 1702a542ad1bSJan Schmidt } else { 1703a542ad1bSJan Schmidt *out_eiref = (struct btrfs_extent_inline_ref *)(ei + 1); 1704a542ad1bSJan Schmidt } 1705a542ad1bSJan Schmidt *ptr = (unsigned long)*out_eiref; 1706cd857dd6SLiu Bo if ((unsigned long)(*ptr) >= (unsigned long)ei + item_size) 1707a542ad1bSJan Schmidt return -ENOENT; 1708a542ad1bSJan Schmidt } 1709a542ad1bSJan Schmidt 1710a542ad1bSJan Schmidt end = (unsigned long)ei + item_size; 17116eda71d0SLiu Bo *out_eiref = (struct btrfs_extent_inline_ref *)(*ptr); 1712a542ad1bSJan Schmidt *out_type = btrfs_extent_inline_ref_type(eb, *out_eiref); 1713a542ad1bSJan Schmidt 1714a542ad1bSJan Schmidt *ptr += btrfs_extent_inline_ref_size(*out_type); 1715a542ad1bSJan Schmidt WARN_ON(*ptr > end); 1716a542ad1bSJan Schmidt if (*ptr == end) 1717a542ad1bSJan Schmidt return 1; /* last */ 1718a542ad1bSJan Schmidt 1719a542ad1bSJan Schmidt return 0; 1720a542ad1bSJan Schmidt } 1721a542ad1bSJan Schmidt 1722a542ad1bSJan Schmidt /* 1723a542ad1bSJan Schmidt * reads the tree block backref for an extent. tree level and root are returned 1724a542ad1bSJan Schmidt * through out_level and out_root. ptr must point to a 0 value for the first 1725e0c476b1SJeff Mahoney * call and may be modified (see get_extent_inline_ref comment). 1726a542ad1bSJan Schmidt * returns 0 if data was provided, 1 if there was no more data to provide or 1727a542ad1bSJan Schmidt * <0 on error. 1728a542ad1bSJan Schmidt */ 1729a542ad1bSJan Schmidt int tree_backref_for_extent(unsigned long *ptr, struct extent_buffer *eb, 17306eda71d0SLiu Bo struct btrfs_key *key, struct btrfs_extent_item *ei, 17316eda71d0SLiu Bo u32 item_size, u64 *out_root, u8 *out_level) 1732a542ad1bSJan Schmidt { 1733a542ad1bSJan Schmidt int ret; 1734a542ad1bSJan Schmidt int type; 1735a542ad1bSJan Schmidt struct btrfs_extent_inline_ref *eiref; 1736a542ad1bSJan Schmidt 1737a542ad1bSJan Schmidt if (*ptr == (unsigned long)-1) 1738a542ad1bSJan Schmidt return 1; 1739a542ad1bSJan Schmidt 1740a542ad1bSJan Schmidt while (1) { 1741e0c476b1SJeff Mahoney ret = get_extent_inline_ref(ptr, eb, key, ei, item_size, 1742a542ad1bSJan Schmidt &eiref, &type); 1743a542ad1bSJan Schmidt if (ret < 0) 1744a542ad1bSJan Schmidt return ret; 1745a542ad1bSJan Schmidt 1746a542ad1bSJan Schmidt if (type == BTRFS_TREE_BLOCK_REF_KEY || 1747a542ad1bSJan Schmidt type == BTRFS_SHARED_BLOCK_REF_KEY) 1748a542ad1bSJan Schmidt break; 1749a542ad1bSJan Schmidt 1750a542ad1bSJan Schmidt if (ret == 1) 1751a542ad1bSJan Schmidt return 1; 1752a542ad1bSJan Schmidt } 1753a542ad1bSJan Schmidt 1754a542ad1bSJan Schmidt /* we can treat both ref types equally here */ 1755a542ad1bSJan Schmidt *out_root = btrfs_extent_inline_ref_offset(eb, eiref); 1756a1317f45SFilipe Manana 1757a1317f45SFilipe Manana if (key->type == BTRFS_EXTENT_ITEM_KEY) { 1758a1317f45SFilipe Manana struct btrfs_tree_block_info *info; 1759a1317f45SFilipe Manana 1760a1317f45SFilipe Manana info = (struct btrfs_tree_block_info *)(ei + 1); 1761a542ad1bSJan Schmidt *out_level = btrfs_tree_block_level(eb, info); 1762a1317f45SFilipe Manana } else { 1763a1317f45SFilipe Manana ASSERT(key->type == BTRFS_METADATA_ITEM_KEY); 1764a1317f45SFilipe Manana *out_level = (u8)key->offset; 1765a1317f45SFilipe Manana } 1766a542ad1bSJan Schmidt 1767a542ad1bSJan Schmidt if (ret == 1) 1768a542ad1bSJan Schmidt *ptr = (unsigned long)-1; 1769a542ad1bSJan Schmidt 1770a542ad1bSJan Schmidt return 0; 1771a542ad1bSJan Schmidt } 1772a542ad1bSJan Schmidt 1773ab8d0fc4SJeff Mahoney static int iterate_leaf_refs(struct btrfs_fs_info *fs_info, 1774ab8d0fc4SJeff Mahoney struct extent_inode_elem *inode_list, 1775976b1908SJan Schmidt u64 root, u64 extent_item_objectid, 17764692cf58SJan Schmidt iterate_extent_inodes_t *iterate, void *ctx) 1777a542ad1bSJan Schmidt { 1778976b1908SJan Schmidt struct extent_inode_elem *eie; 17794692cf58SJan Schmidt int ret = 0; 1780a542ad1bSJan Schmidt 1781976b1908SJan Schmidt for (eie = inode_list; eie; eie = eie->next) { 1782ab8d0fc4SJeff Mahoney btrfs_debug(fs_info, 1783ab8d0fc4SJeff Mahoney "ref for %llu resolved, key (%llu EXTEND_DATA %llu), root %llu", 1784ab8d0fc4SJeff Mahoney extent_item_objectid, eie->inum, 1785ab8d0fc4SJeff Mahoney eie->offset, root); 1786976b1908SJan Schmidt ret = iterate(eie->inum, eie->offset, root, ctx); 17874692cf58SJan Schmidt if (ret) { 1788ab8d0fc4SJeff Mahoney btrfs_debug(fs_info, 1789ab8d0fc4SJeff Mahoney "stopping iteration for %llu due to ret=%d", 1790976b1908SJan Schmidt extent_item_objectid, ret); 1791a542ad1bSJan Schmidt break; 1792a542ad1bSJan Schmidt } 1793a542ad1bSJan Schmidt } 1794a542ad1bSJan Schmidt 1795a542ad1bSJan Schmidt return ret; 1796a542ad1bSJan Schmidt } 1797a542ad1bSJan Schmidt 1798a542ad1bSJan Schmidt /* 1799a542ad1bSJan Schmidt * calls iterate() for every inode that references the extent identified by 18004692cf58SJan Schmidt * the given parameters. 1801a542ad1bSJan Schmidt * when the iterator function returns a non-zero value, iteration stops. 1802a542ad1bSJan Schmidt */ 1803a542ad1bSJan Schmidt int iterate_extent_inodes(struct btrfs_fs_info *fs_info, 18044692cf58SJan Schmidt u64 extent_item_objectid, u64 extent_item_pos, 18057a3ae2f8SJan Schmidt int search_commit_root, 1806a542ad1bSJan Schmidt iterate_extent_inodes_t *iterate, void *ctx) 1807a542ad1bSJan Schmidt { 1808a542ad1bSJan Schmidt int ret; 1809da61d31aSJosef Bacik struct btrfs_trans_handle *trans = NULL; 18107a3ae2f8SJan Schmidt struct ulist *refs = NULL; 18117a3ae2f8SJan Schmidt struct ulist *roots = NULL; 18124692cf58SJan Schmidt struct ulist_node *ref_node = NULL; 18134692cf58SJan Schmidt struct ulist_node *root_node = NULL; 18143284da7bSDavid Sterba struct seq_list tree_mod_seq_elem = SEQ_LIST_INIT(tree_mod_seq_elem); 1815cd1b413cSJan Schmidt struct ulist_iterator ref_uiter; 1816cd1b413cSJan Schmidt struct ulist_iterator root_uiter; 1817a542ad1bSJan Schmidt 1818ab8d0fc4SJeff Mahoney btrfs_debug(fs_info, "resolving all inodes for extent %llu", 18194692cf58SJan Schmidt extent_item_objectid); 18204692cf58SJan Schmidt 1821da61d31aSJosef Bacik if (!search_commit_root) { 18227a3ae2f8SJan Schmidt trans = btrfs_join_transaction(fs_info->extent_root); 18237a3ae2f8SJan Schmidt if (IS_ERR(trans)) 18247a3ae2f8SJan Schmidt return PTR_ERR(trans); 18258445f61cSJan Schmidt btrfs_get_tree_mod_seq(fs_info, &tree_mod_seq_elem); 18269e351cc8SJosef Bacik } else { 18279e351cc8SJosef Bacik down_read(&fs_info->commit_root_sem); 18287a3ae2f8SJan Schmidt } 18294692cf58SJan Schmidt 18304692cf58SJan Schmidt ret = btrfs_find_all_leafs(trans, fs_info, extent_item_objectid, 1831097b8a7cSJan Schmidt tree_mod_seq_elem.seq, &refs, 18328445f61cSJan Schmidt &extent_item_pos); 18334692cf58SJan Schmidt if (ret) 18344692cf58SJan Schmidt goto out; 18354692cf58SJan Schmidt 1836cd1b413cSJan Schmidt ULIST_ITER_INIT(&ref_uiter); 1837cd1b413cSJan Schmidt while (!ret && (ref_node = ulist_next(refs, &ref_uiter))) { 1838e0c476b1SJeff Mahoney ret = btrfs_find_all_roots_safe(trans, fs_info, ref_node->val, 18398445f61cSJan Schmidt tree_mod_seq_elem.seq, &roots); 18404692cf58SJan Schmidt if (ret) 1841a542ad1bSJan Schmidt break; 1842cd1b413cSJan Schmidt ULIST_ITER_INIT(&root_uiter); 1843cd1b413cSJan Schmidt while (!ret && (root_node = ulist_next(roots, &root_uiter))) { 1844ab8d0fc4SJeff Mahoney btrfs_debug(fs_info, 1845ab8d0fc4SJeff Mahoney "root %llu references leaf %llu, data list %#llx", 1846ab8d0fc4SJeff Mahoney root_node->val, ref_node->val, 1847c1c9ff7cSGeert Uytterhoeven ref_node->aux); 1848ab8d0fc4SJeff Mahoney ret = iterate_leaf_refs(fs_info, 1849ab8d0fc4SJeff Mahoney (struct extent_inode_elem *) 1850995e01b7SJan Schmidt (uintptr_t)ref_node->aux, 1851995e01b7SJan Schmidt root_node->val, 1852995e01b7SJan Schmidt extent_item_objectid, 1853a542ad1bSJan Schmidt iterate, ctx); 18544692cf58SJan Schmidt } 1855976b1908SJan Schmidt ulist_free(roots); 1856a542ad1bSJan Schmidt } 1857a542ad1bSJan Schmidt 1858976b1908SJan Schmidt free_leaf_list(refs); 18594692cf58SJan Schmidt out: 18607a3ae2f8SJan Schmidt if (!search_commit_root) { 18618445f61cSJan Schmidt btrfs_put_tree_mod_seq(fs_info, &tree_mod_seq_elem); 18623a45bb20SJeff Mahoney btrfs_end_transaction(trans); 18639e351cc8SJosef Bacik } else { 18649e351cc8SJosef Bacik up_read(&fs_info->commit_root_sem); 18657a3ae2f8SJan Schmidt } 18667a3ae2f8SJan Schmidt 1867a542ad1bSJan Schmidt return ret; 1868a542ad1bSJan Schmidt } 1869a542ad1bSJan Schmidt 1870a542ad1bSJan Schmidt int iterate_inodes_from_logical(u64 logical, struct btrfs_fs_info *fs_info, 1871a542ad1bSJan Schmidt struct btrfs_path *path, 1872a542ad1bSJan Schmidt iterate_extent_inodes_t *iterate, void *ctx) 1873a542ad1bSJan Schmidt { 1874a542ad1bSJan Schmidt int ret; 18754692cf58SJan Schmidt u64 extent_item_pos; 187669917e43SLiu Bo u64 flags = 0; 1877a542ad1bSJan Schmidt struct btrfs_key found_key; 18787a3ae2f8SJan Schmidt int search_commit_root = path->search_commit_root; 1879a542ad1bSJan Schmidt 188069917e43SLiu Bo ret = extent_from_logical(fs_info, logical, path, &found_key, &flags); 18814692cf58SJan Schmidt btrfs_release_path(path); 1882a542ad1bSJan Schmidt if (ret < 0) 1883a542ad1bSJan Schmidt return ret; 188469917e43SLiu Bo if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) 18853627bf45SStefan Behrens return -EINVAL; 1886a542ad1bSJan Schmidt 18874692cf58SJan Schmidt extent_item_pos = logical - found_key.objectid; 18887a3ae2f8SJan Schmidt ret = iterate_extent_inodes(fs_info, found_key.objectid, 18897a3ae2f8SJan Schmidt extent_item_pos, search_commit_root, 18907a3ae2f8SJan Schmidt iterate, ctx); 1891a542ad1bSJan Schmidt 1892a542ad1bSJan Schmidt return ret; 1893a542ad1bSJan Schmidt } 1894a542ad1bSJan Schmidt 1895d24bec3aSMark Fasheh typedef int (iterate_irefs_t)(u64 parent, u32 name_len, unsigned long name_off, 1896d24bec3aSMark Fasheh struct extent_buffer *eb, void *ctx); 1897d24bec3aSMark Fasheh 1898d24bec3aSMark Fasheh static int iterate_inode_refs(u64 inum, struct btrfs_root *fs_root, 1899a542ad1bSJan Schmidt struct btrfs_path *path, 1900a542ad1bSJan Schmidt iterate_irefs_t *iterate, void *ctx) 1901a542ad1bSJan Schmidt { 1902aefc1eb1SJan Schmidt int ret = 0; 1903a542ad1bSJan Schmidt int slot; 1904a542ad1bSJan Schmidt u32 cur; 1905a542ad1bSJan Schmidt u32 len; 1906a542ad1bSJan Schmidt u32 name_len; 1907a542ad1bSJan Schmidt u64 parent = 0; 1908a542ad1bSJan Schmidt int found = 0; 1909a542ad1bSJan Schmidt struct extent_buffer *eb; 1910a542ad1bSJan Schmidt struct btrfs_item *item; 1911a542ad1bSJan Schmidt struct btrfs_inode_ref *iref; 1912a542ad1bSJan Schmidt struct btrfs_key found_key; 1913a542ad1bSJan Schmidt 1914aefc1eb1SJan Schmidt while (!ret) { 1915c234a24dSDavid Sterba ret = btrfs_find_item(fs_root, path, inum, 1916c234a24dSDavid Sterba parent ? parent + 1 : 0, BTRFS_INODE_REF_KEY, 1917a542ad1bSJan Schmidt &found_key); 1918c234a24dSDavid Sterba 1919a542ad1bSJan Schmidt if (ret < 0) 1920a542ad1bSJan Schmidt break; 1921a542ad1bSJan Schmidt if (ret) { 1922a542ad1bSJan Schmidt ret = found ? 0 : -ENOENT; 1923a542ad1bSJan Schmidt break; 1924a542ad1bSJan Schmidt } 1925a542ad1bSJan Schmidt ++found; 1926a542ad1bSJan Schmidt 1927a542ad1bSJan Schmidt parent = found_key.offset; 1928a542ad1bSJan Schmidt slot = path->slots[0]; 19293fe81ce2SFilipe David Borba Manana eb = btrfs_clone_extent_buffer(path->nodes[0]); 19303fe81ce2SFilipe David Borba Manana if (!eb) { 19313fe81ce2SFilipe David Borba Manana ret = -ENOMEM; 19323fe81ce2SFilipe David Borba Manana break; 19333fe81ce2SFilipe David Borba Manana } 19343fe81ce2SFilipe David Borba Manana extent_buffer_get(eb); 1935b916a59aSJan Schmidt btrfs_tree_read_lock(eb); 1936b916a59aSJan Schmidt btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK); 1937a542ad1bSJan Schmidt btrfs_release_path(path); 1938a542ad1bSJan Schmidt 1939dd3cc16bSRoss Kirk item = btrfs_item_nr(slot); 1940a542ad1bSJan Schmidt iref = btrfs_item_ptr(eb, slot, struct btrfs_inode_ref); 1941a542ad1bSJan Schmidt 1942a542ad1bSJan Schmidt for (cur = 0; cur < btrfs_item_size(eb, item); cur += len) { 1943a542ad1bSJan Schmidt name_len = btrfs_inode_ref_name_len(eb, iref); 1944a542ad1bSJan Schmidt /* path must be released before calling iterate()! */ 1945ab8d0fc4SJeff Mahoney btrfs_debug(fs_root->fs_info, 1946ab8d0fc4SJeff Mahoney "following ref at offset %u for inode %llu in tree %llu", 1947ab8d0fc4SJeff Mahoney cur, found_key.objectid, fs_root->objectid); 1948d24bec3aSMark Fasheh ret = iterate(parent, name_len, 1949d24bec3aSMark Fasheh (unsigned long)(iref + 1), eb, ctx); 1950aefc1eb1SJan Schmidt if (ret) 1951a542ad1bSJan Schmidt break; 1952a542ad1bSJan Schmidt len = sizeof(*iref) + name_len; 1953a542ad1bSJan Schmidt iref = (struct btrfs_inode_ref *)((char *)iref + len); 1954a542ad1bSJan Schmidt } 1955b916a59aSJan Schmidt btrfs_tree_read_unlock_blocking(eb); 1956a542ad1bSJan Schmidt free_extent_buffer(eb); 1957a542ad1bSJan Schmidt } 1958a542ad1bSJan Schmidt 1959a542ad1bSJan Schmidt btrfs_release_path(path); 1960a542ad1bSJan Schmidt 1961a542ad1bSJan Schmidt return ret; 1962a542ad1bSJan Schmidt } 1963a542ad1bSJan Schmidt 1964d24bec3aSMark Fasheh static int iterate_inode_extrefs(u64 inum, struct btrfs_root *fs_root, 1965d24bec3aSMark Fasheh struct btrfs_path *path, 1966d24bec3aSMark Fasheh iterate_irefs_t *iterate, void *ctx) 1967d24bec3aSMark Fasheh { 1968d24bec3aSMark Fasheh int ret; 1969d24bec3aSMark Fasheh int slot; 1970d24bec3aSMark Fasheh u64 offset = 0; 1971d24bec3aSMark Fasheh u64 parent; 1972d24bec3aSMark Fasheh int found = 0; 1973d24bec3aSMark Fasheh struct extent_buffer *eb; 1974d24bec3aSMark Fasheh struct btrfs_inode_extref *extref; 1975d24bec3aSMark Fasheh u32 item_size; 1976d24bec3aSMark Fasheh u32 cur_offset; 1977d24bec3aSMark Fasheh unsigned long ptr; 1978d24bec3aSMark Fasheh 1979d24bec3aSMark Fasheh while (1) { 1980d24bec3aSMark Fasheh ret = btrfs_find_one_extref(fs_root, inum, offset, path, &extref, 1981d24bec3aSMark Fasheh &offset); 1982d24bec3aSMark Fasheh if (ret < 0) 1983d24bec3aSMark Fasheh break; 1984d24bec3aSMark Fasheh if (ret) { 1985d24bec3aSMark Fasheh ret = found ? 0 : -ENOENT; 1986d24bec3aSMark Fasheh break; 1987d24bec3aSMark Fasheh } 1988d24bec3aSMark Fasheh ++found; 1989d24bec3aSMark Fasheh 1990d24bec3aSMark Fasheh slot = path->slots[0]; 19913fe81ce2SFilipe David Borba Manana eb = btrfs_clone_extent_buffer(path->nodes[0]); 19923fe81ce2SFilipe David Borba Manana if (!eb) { 19933fe81ce2SFilipe David Borba Manana ret = -ENOMEM; 19943fe81ce2SFilipe David Borba Manana break; 19953fe81ce2SFilipe David Borba Manana } 19963fe81ce2SFilipe David Borba Manana extent_buffer_get(eb); 1997d24bec3aSMark Fasheh 1998d24bec3aSMark Fasheh btrfs_tree_read_lock(eb); 1999d24bec3aSMark Fasheh btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK); 2000d24bec3aSMark Fasheh btrfs_release_path(path); 2001d24bec3aSMark Fasheh 20022849a854SChris Mason item_size = btrfs_item_size_nr(eb, slot); 20032849a854SChris Mason ptr = btrfs_item_ptr_offset(eb, slot); 2004d24bec3aSMark Fasheh cur_offset = 0; 2005d24bec3aSMark Fasheh 2006d24bec3aSMark Fasheh while (cur_offset < item_size) { 2007d24bec3aSMark Fasheh u32 name_len; 2008d24bec3aSMark Fasheh 2009d24bec3aSMark Fasheh extref = (struct btrfs_inode_extref *)(ptr + cur_offset); 2010d24bec3aSMark Fasheh parent = btrfs_inode_extref_parent(eb, extref); 2011d24bec3aSMark Fasheh name_len = btrfs_inode_extref_name_len(eb, extref); 2012d24bec3aSMark Fasheh ret = iterate(parent, name_len, 2013d24bec3aSMark Fasheh (unsigned long)&extref->name, eb, ctx); 2014d24bec3aSMark Fasheh if (ret) 2015d24bec3aSMark Fasheh break; 2016d24bec3aSMark Fasheh 20172849a854SChris Mason cur_offset += btrfs_inode_extref_name_len(eb, extref); 2018d24bec3aSMark Fasheh cur_offset += sizeof(*extref); 2019d24bec3aSMark Fasheh } 2020d24bec3aSMark Fasheh btrfs_tree_read_unlock_blocking(eb); 2021d24bec3aSMark Fasheh free_extent_buffer(eb); 2022d24bec3aSMark Fasheh 2023d24bec3aSMark Fasheh offset++; 2024d24bec3aSMark Fasheh } 2025d24bec3aSMark Fasheh 2026d24bec3aSMark Fasheh btrfs_release_path(path); 2027d24bec3aSMark Fasheh 2028d24bec3aSMark Fasheh return ret; 2029d24bec3aSMark Fasheh } 2030d24bec3aSMark Fasheh 2031d24bec3aSMark Fasheh static int iterate_irefs(u64 inum, struct btrfs_root *fs_root, 2032d24bec3aSMark Fasheh struct btrfs_path *path, iterate_irefs_t *iterate, 2033d24bec3aSMark Fasheh void *ctx) 2034d24bec3aSMark Fasheh { 2035d24bec3aSMark Fasheh int ret; 2036d24bec3aSMark Fasheh int found_refs = 0; 2037d24bec3aSMark Fasheh 2038d24bec3aSMark Fasheh ret = iterate_inode_refs(inum, fs_root, path, iterate, ctx); 2039d24bec3aSMark Fasheh if (!ret) 2040d24bec3aSMark Fasheh ++found_refs; 2041d24bec3aSMark Fasheh else if (ret != -ENOENT) 2042d24bec3aSMark Fasheh return ret; 2043d24bec3aSMark Fasheh 2044d24bec3aSMark Fasheh ret = iterate_inode_extrefs(inum, fs_root, path, iterate, ctx); 2045d24bec3aSMark Fasheh if (ret == -ENOENT && found_refs) 2046d24bec3aSMark Fasheh return 0; 2047d24bec3aSMark Fasheh 2048d24bec3aSMark Fasheh return ret; 2049d24bec3aSMark Fasheh } 2050d24bec3aSMark Fasheh 2051a542ad1bSJan Schmidt /* 2052a542ad1bSJan Schmidt * returns 0 if the path could be dumped (probably truncated) 2053a542ad1bSJan Schmidt * returns <0 in case of an error 2054a542ad1bSJan Schmidt */ 2055d24bec3aSMark Fasheh static int inode_to_path(u64 inum, u32 name_len, unsigned long name_off, 2056a542ad1bSJan Schmidt struct extent_buffer *eb, void *ctx) 2057a542ad1bSJan Schmidt { 2058a542ad1bSJan Schmidt struct inode_fs_paths *ipath = ctx; 2059a542ad1bSJan Schmidt char *fspath; 2060a542ad1bSJan Schmidt char *fspath_min; 2061a542ad1bSJan Schmidt int i = ipath->fspath->elem_cnt; 2062a542ad1bSJan Schmidt const int s_ptr = sizeof(char *); 2063a542ad1bSJan Schmidt u32 bytes_left; 2064a542ad1bSJan Schmidt 2065a542ad1bSJan Schmidt bytes_left = ipath->fspath->bytes_left > s_ptr ? 2066a542ad1bSJan Schmidt ipath->fspath->bytes_left - s_ptr : 0; 2067a542ad1bSJan Schmidt 2068740c3d22SChris Mason fspath_min = (char *)ipath->fspath->val + (i + 1) * s_ptr; 206996b5bd77SJan Schmidt fspath = btrfs_ref_to_path(ipath->fs_root, ipath->btrfs_path, name_len, 207096b5bd77SJan Schmidt name_off, eb, inum, fspath_min, bytes_left); 2071a542ad1bSJan Schmidt if (IS_ERR(fspath)) 2072a542ad1bSJan Schmidt return PTR_ERR(fspath); 2073a542ad1bSJan Schmidt 2074a542ad1bSJan Schmidt if (fspath > fspath_min) { 2075745c4d8eSJeff Mahoney ipath->fspath->val[i] = (u64)(unsigned long)fspath; 2076a542ad1bSJan Schmidt ++ipath->fspath->elem_cnt; 2077a542ad1bSJan Schmidt ipath->fspath->bytes_left = fspath - fspath_min; 2078a542ad1bSJan Schmidt } else { 2079a542ad1bSJan Schmidt ++ipath->fspath->elem_missed; 2080a542ad1bSJan Schmidt ipath->fspath->bytes_missing += fspath_min - fspath; 2081a542ad1bSJan Schmidt ipath->fspath->bytes_left = 0; 2082a542ad1bSJan Schmidt } 2083a542ad1bSJan Schmidt 2084a542ad1bSJan Schmidt return 0; 2085a542ad1bSJan Schmidt } 2086a542ad1bSJan Schmidt 2087a542ad1bSJan Schmidt /* 2088a542ad1bSJan Schmidt * this dumps all file system paths to the inode into the ipath struct, provided 2089a542ad1bSJan Schmidt * is has been created large enough. each path is zero-terminated and accessed 2090740c3d22SChris Mason * from ipath->fspath->val[i]. 2091a542ad1bSJan Schmidt * when it returns, there are ipath->fspath->elem_cnt number of paths available 2092740c3d22SChris Mason * in ipath->fspath->val[]. when the allocated space wasn't sufficient, the 209301327610SNicholas D Steeves * number of missed paths is recorded in ipath->fspath->elem_missed, otherwise, 2094a542ad1bSJan Schmidt * it's zero. ipath->fspath->bytes_missing holds the number of bytes that would 2095a542ad1bSJan Schmidt * have been needed to return all paths. 2096a542ad1bSJan Schmidt */ 2097a542ad1bSJan Schmidt int paths_from_inode(u64 inum, struct inode_fs_paths *ipath) 2098a542ad1bSJan Schmidt { 2099a542ad1bSJan Schmidt return iterate_irefs(inum, ipath->fs_root, ipath->btrfs_path, 2100a542ad1bSJan Schmidt inode_to_path, ipath); 2101a542ad1bSJan Schmidt } 2102a542ad1bSJan Schmidt 2103a542ad1bSJan Schmidt struct btrfs_data_container *init_data_container(u32 total_bytes) 2104a542ad1bSJan Schmidt { 2105a542ad1bSJan Schmidt struct btrfs_data_container *data; 2106a542ad1bSJan Schmidt size_t alloc_bytes; 2107a542ad1bSJan Schmidt 2108a542ad1bSJan Schmidt alloc_bytes = max_t(size_t, total_bytes, sizeof(*data)); 2109f54de068SDavid Sterba data = kvmalloc(alloc_bytes, GFP_KERNEL); 2110a542ad1bSJan Schmidt if (!data) 2111a542ad1bSJan Schmidt return ERR_PTR(-ENOMEM); 2112a542ad1bSJan Schmidt 2113a542ad1bSJan Schmidt if (total_bytes >= sizeof(*data)) { 2114a542ad1bSJan Schmidt data->bytes_left = total_bytes - sizeof(*data); 2115a542ad1bSJan Schmidt data->bytes_missing = 0; 2116a542ad1bSJan Schmidt } else { 2117a542ad1bSJan Schmidt data->bytes_missing = sizeof(*data) - total_bytes; 2118a542ad1bSJan Schmidt data->bytes_left = 0; 2119a542ad1bSJan Schmidt } 2120a542ad1bSJan Schmidt 2121a542ad1bSJan Schmidt data->elem_cnt = 0; 2122a542ad1bSJan Schmidt data->elem_missed = 0; 2123a542ad1bSJan Schmidt 2124a542ad1bSJan Schmidt return data; 2125a542ad1bSJan Schmidt } 2126a542ad1bSJan Schmidt 2127a542ad1bSJan Schmidt /* 2128a542ad1bSJan Schmidt * allocates space to return multiple file system paths for an inode. 2129a542ad1bSJan Schmidt * total_bytes to allocate are passed, note that space usable for actual path 2130a542ad1bSJan Schmidt * information will be total_bytes - sizeof(struct inode_fs_paths). 2131a542ad1bSJan Schmidt * the returned pointer must be freed with free_ipath() in the end. 2132a542ad1bSJan Schmidt */ 2133a542ad1bSJan Schmidt struct inode_fs_paths *init_ipath(s32 total_bytes, struct btrfs_root *fs_root, 2134a542ad1bSJan Schmidt struct btrfs_path *path) 2135a542ad1bSJan Schmidt { 2136a542ad1bSJan Schmidt struct inode_fs_paths *ifp; 2137a542ad1bSJan Schmidt struct btrfs_data_container *fspath; 2138a542ad1bSJan Schmidt 2139a542ad1bSJan Schmidt fspath = init_data_container(total_bytes); 2140a542ad1bSJan Schmidt if (IS_ERR(fspath)) 2141a542ad1bSJan Schmidt return (void *)fspath; 2142a542ad1bSJan Schmidt 2143f54de068SDavid Sterba ifp = kmalloc(sizeof(*ifp), GFP_KERNEL); 2144a542ad1bSJan Schmidt if (!ifp) { 2145f54de068SDavid Sterba kvfree(fspath); 2146a542ad1bSJan Schmidt return ERR_PTR(-ENOMEM); 2147a542ad1bSJan Schmidt } 2148a542ad1bSJan Schmidt 2149a542ad1bSJan Schmidt ifp->btrfs_path = path; 2150a542ad1bSJan Schmidt ifp->fspath = fspath; 2151a542ad1bSJan Schmidt ifp->fs_root = fs_root; 2152a542ad1bSJan Schmidt 2153a542ad1bSJan Schmidt return ifp; 2154a542ad1bSJan Schmidt } 2155a542ad1bSJan Schmidt 2156a542ad1bSJan Schmidt void free_ipath(struct inode_fs_paths *ipath) 2157a542ad1bSJan Schmidt { 21584735fb28SJesper Juhl if (!ipath) 21594735fb28SJesper Juhl return; 2160f54de068SDavid Sterba kvfree(ipath->fspath); 2161a542ad1bSJan Schmidt kfree(ipath); 2162a542ad1bSJan Schmidt } 2163