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> 2100142756SJeff Mahoney #include <trace/events/btrfs.h> 22a542ad1bSJan Schmidt #include "ctree.h" 23a542ad1bSJan Schmidt #include "disk-io.h" 24a542ad1bSJan Schmidt #include "backref.h" 258da6d581SJan Schmidt #include "ulist.h" 268da6d581SJan Schmidt #include "transaction.h" 278da6d581SJan Schmidt #include "delayed-ref.h" 28b916a59aSJan Schmidt #include "locking.h" 29a542ad1bSJan Schmidt 30dc046b10SJosef Bacik /* Just an arbitrary number so we can be sure this happened */ 31dc046b10SJosef Bacik #define BACKREF_FOUND_SHARED 6 32dc046b10SJosef Bacik 33976b1908SJan Schmidt struct extent_inode_elem { 34976b1908SJan Schmidt u64 inum; 35976b1908SJan Schmidt u64 offset; 36976b1908SJan Schmidt struct extent_inode_elem *next; 37976b1908SJan Schmidt }; 38976b1908SJan Schmidt 3973980becSJeff Mahoney static int check_extent_in_eb(const struct btrfs_key *key, 4073980becSJeff Mahoney const struct extent_buffer *eb, 4173980becSJeff Mahoney const struct btrfs_file_extent_item *fi, 42976b1908SJan Schmidt u64 extent_item_pos, 43976b1908SJan Schmidt struct extent_inode_elem **eie) 44976b1908SJan Schmidt { 458ca15e05SJosef Bacik u64 offset = 0; 468ca15e05SJosef Bacik struct extent_inode_elem *e; 478ca15e05SJosef Bacik 488ca15e05SJosef Bacik if (!btrfs_file_extent_compression(eb, fi) && 498ca15e05SJosef Bacik !btrfs_file_extent_encryption(eb, fi) && 508ca15e05SJosef Bacik !btrfs_file_extent_other_encoding(eb, fi)) { 51976b1908SJan Schmidt u64 data_offset; 52976b1908SJan Schmidt u64 data_len; 53976b1908SJan Schmidt 54976b1908SJan Schmidt data_offset = btrfs_file_extent_offset(eb, fi); 55976b1908SJan Schmidt data_len = btrfs_file_extent_num_bytes(eb, fi); 56976b1908SJan Schmidt 57976b1908SJan Schmidt if (extent_item_pos < data_offset || 58976b1908SJan Schmidt extent_item_pos >= data_offset + data_len) 59976b1908SJan Schmidt return 1; 608ca15e05SJosef Bacik offset = extent_item_pos - data_offset; 618ca15e05SJosef Bacik } 62976b1908SJan Schmidt 63976b1908SJan Schmidt e = kmalloc(sizeof(*e), GFP_NOFS); 64976b1908SJan Schmidt if (!e) 65976b1908SJan Schmidt return -ENOMEM; 66976b1908SJan Schmidt 67976b1908SJan Schmidt e->next = *eie; 68976b1908SJan Schmidt e->inum = key->objectid; 698ca15e05SJosef Bacik e->offset = key->offset + offset; 70976b1908SJan Schmidt *eie = e; 71976b1908SJan Schmidt 72976b1908SJan Schmidt return 0; 73976b1908SJan Schmidt } 74976b1908SJan Schmidt 75f05c4746SWang Shilong static void free_inode_elem_list(struct extent_inode_elem *eie) 76f05c4746SWang Shilong { 77f05c4746SWang Shilong struct extent_inode_elem *eie_next; 78f05c4746SWang Shilong 79f05c4746SWang Shilong for (; eie; eie = eie_next) { 80f05c4746SWang Shilong eie_next = eie->next; 81f05c4746SWang Shilong kfree(eie); 82f05c4746SWang Shilong } 83f05c4746SWang Shilong } 84f05c4746SWang Shilong 8573980becSJeff Mahoney static int find_extent_in_eb(const struct extent_buffer *eb, 8673980becSJeff Mahoney u64 wanted_disk_byte, u64 extent_item_pos, 87976b1908SJan Schmidt struct extent_inode_elem **eie) 88976b1908SJan Schmidt { 89976b1908SJan Schmidt u64 disk_byte; 90976b1908SJan Schmidt struct btrfs_key key; 91976b1908SJan Schmidt struct btrfs_file_extent_item *fi; 92976b1908SJan Schmidt int slot; 93976b1908SJan Schmidt int nritems; 94976b1908SJan Schmidt int extent_type; 95976b1908SJan Schmidt int ret; 96976b1908SJan Schmidt 97976b1908SJan Schmidt /* 98976b1908SJan Schmidt * from the shared data ref, we only have the leaf but we need 99976b1908SJan Schmidt * the key. thus, we must look into all items and see that we 100976b1908SJan Schmidt * find one (some) with a reference to our extent item. 101976b1908SJan Schmidt */ 102976b1908SJan Schmidt nritems = btrfs_header_nritems(eb); 103976b1908SJan Schmidt for (slot = 0; slot < nritems; ++slot) { 104976b1908SJan Schmidt btrfs_item_key_to_cpu(eb, &key, slot); 105976b1908SJan Schmidt if (key.type != BTRFS_EXTENT_DATA_KEY) 106976b1908SJan Schmidt continue; 107976b1908SJan Schmidt fi = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item); 108976b1908SJan Schmidt extent_type = btrfs_file_extent_type(eb, fi); 109976b1908SJan Schmidt if (extent_type == BTRFS_FILE_EXTENT_INLINE) 110976b1908SJan Schmidt continue; 111976b1908SJan Schmidt /* don't skip BTRFS_FILE_EXTENT_PREALLOC, we can handle that */ 112976b1908SJan Schmidt disk_byte = btrfs_file_extent_disk_bytenr(eb, fi); 113976b1908SJan Schmidt if (disk_byte != wanted_disk_byte) 114976b1908SJan Schmidt continue; 115976b1908SJan Schmidt 116976b1908SJan Schmidt ret = check_extent_in_eb(&key, eb, fi, extent_item_pos, eie); 117976b1908SJan Schmidt if (ret < 0) 118976b1908SJan Schmidt return ret; 119976b1908SJan Schmidt } 120976b1908SJan Schmidt 121976b1908SJan Schmidt return 0; 122976b1908SJan Schmidt } 123976b1908SJan Schmidt 12486d5f994SEdmund Nadolski struct preftree { 12586d5f994SEdmund Nadolski struct rb_root root; 1266c336b21SJeff Mahoney unsigned int count; 12786d5f994SEdmund Nadolski }; 12886d5f994SEdmund Nadolski 1296c336b21SJeff Mahoney #define PREFTREE_INIT { .root = RB_ROOT, .count = 0 } 13086d5f994SEdmund Nadolski 13186d5f994SEdmund Nadolski struct preftrees { 13286d5f994SEdmund Nadolski struct preftree direct; /* BTRFS_SHARED_[DATA|BLOCK]_REF_KEY */ 13386d5f994SEdmund Nadolski struct preftree indirect; /* BTRFS_[TREE_BLOCK|EXTENT_DATA]_REF_KEY */ 13486d5f994SEdmund Nadolski struct preftree indirect_missing_keys; 13586d5f994SEdmund Nadolski }; 13686d5f994SEdmund Nadolski 137*3ec4d323SEdmund Nadolski /* 138*3ec4d323SEdmund Nadolski * Checks for a shared extent during backref search. 139*3ec4d323SEdmund Nadolski * 140*3ec4d323SEdmund Nadolski * The share_count tracks prelim_refs (direct and indirect) having a 141*3ec4d323SEdmund Nadolski * ref->count >0: 142*3ec4d323SEdmund Nadolski * - incremented when a ref->count transitions to >0 143*3ec4d323SEdmund Nadolski * - decremented when a ref->count transitions to <1 144*3ec4d323SEdmund Nadolski */ 145*3ec4d323SEdmund Nadolski struct share_check { 146*3ec4d323SEdmund Nadolski u64 root_objectid; 147*3ec4d323SEdmund Nadolski u64 inum; 148*3ec4d323SEdmund Nadolski int share_count; 149*3ec4d323SEdmund Nadolski }; 150*3ec4d323SEdmund Nadolski 151*3ec4d323SEdmund Nadolski static inline int extent_is_shared(struct share_check *sc) 152*3ec4d323SEdmund Nadolski { 153*3ec4d323SEdmund Nadolski return (sc && sc->share_count > 1) ? BACKREF_FOUND_SHARED : 0; 154*3ec4d323SEdmund Nadolski } 155*3ec4d323SEdmund Nadolski 156b9e9a6cbSWang Shilong static struct kmem_cache *btrfs_prelim_ref_cache; 157b9e9a6cbSWang Shilong 158b9e9a6cbSWang Shilong int __init btrfs_prelim_ref_init(void) 159b9e9a6cbSWang Shilong { 160b9e9a6cbSWang Shilong btrfs_prelim_ref_cache = kmem_cache_create("btrfs_prelim_ref", 161e0c476b1SJeff Mahoney sizeof(struct prelim_ref), 162b9e9a6cbSWang Shilong 0, 163fba4b697SNikolay Borisov SLAB_MEM_SPREAD, 164b9e9a6cbSWang Shilong NULL); 165b9e9a6cbSWang Shilong if (!btrfs_prelim_ref_cache) 166b9e9a6cbSWang Shilong return -ENOMEM; 167b9e9a6cbSWang Shilong return 0; 168b9e9a6cbSWang Shilong } 169b9e9a6cbSWang Shilong 170b9e9a6cbSWang Shilong void btrfs_prelim_ref_exit(void) 171b9e9a6cbSWang Shilong { 172b9e9a6cbSWang Shilong kmem_cache_destroy(btrfs_prelim_ref_cache); 173b9e9a6cbSWang Shilong } 174b9e9a6cbSWang Shilong 17586d5f994SEdmund Nadolski static void free_pref(struct prelim_ref *ref) 17686d5f994SEdmund Nadolski { 17786d5f994SEdmund Nadolski kmem_cache_free(btrfs_prelim_ref_cache, ref); 17886d5f994SEdmund Nadolski } 17986d5f994SEdmund Nadolski 18086d5f994SEdmund Nadolski /* 18186d5f994SEdmund Nadolski * Return 0 when both refs are for the same block (and can be merged). 18286d5f994SEdmund Nadolski * A -1 return indicates ref1 is a 'lower' block than ref2, while 1 18386d5f994SEdmund Nadolski * indicates a 'higher' block. 18486d5f994SEdmund Nadolski */ 18586d5f994SEdmund Nadolski static int prelim_ref_compare(struct prelim_ref *ref1, 18686d5f994SEdmund Nadolski struct prelim_ref *ref2) 18786d5f994SEdmund Nadolski { 18886d5f994SEdmund Nadolski if (ref1->level < ref2->level) 18986d5f994SEdmund Nadolski return -1; 19086d5f994SEdmund Nadolski if (ref1->level > ref2->level) 19186d5f994SEdmund Nadolski return 1; 19286d5f994SEdmund Nadolski if (ref1->root_id < ref2->root_id) 19386d5f994SEdmund Nadolski return -1; 19486d5f994SEdmund Nadolski if (ref1->root_id > ref2->root_id) 19586d5f994SEdmund Nadolski return 1; 19686d5f994SEdmund Nadolski if (ref1->key_for_search.type < ref2->key_for_search.type) 19786d5f994SEdmund Nadolski return -1; 19886d5f994SEdmund Nadolski if (ref1->key_for_search.type > ref2->key_for_search.type) 19986d5f994SEdmund Nadolski return 1; 20086d5f994SEdmund Nadolski if (ref1->key_for_search.objectid < ref2->key_for_search.objectid) 20186d5f994SEdmund Nadolski return -1; 20286d5f994SEdmund Nadolski if (ref1->key_for_search.objectid > ref2->key_for_search.objectid) 20386d5f994SEdmund Nadolski return 1; 20486d5f994SEdmund Nadolski if (ref1->key_for_search.offset < ref2->key_for_search.offset) 20586d5f994SEdmund Nadolski return -1; 20686d5f994SEdmund Nadolski if (ref1->key_for_search.offset > ref2->key_for_search.offset) 20786d5f994SEdmund Nadolski return 1; 20886d5f994SEdmund Nadolski if (ref1->parent < ref2->parent) 20986d5f994SEdmund Nadolski return -1; 21086d5f994SEdmund Nadolski if (ref1->parent > ref2->parent) 21186d5f994SEdmund Nadolski return 1; 21286d5f994SEdmund Nadolski 21386d5f994SEdmund Nadolski return 0; 21486d5f994SEdmund Nadolski } 21586d5f994SEdmund Nadolski 216*3ec4d323SEdmund Nadolski void update_share_count(struct share_check *sc, int oldcount, int newcount) 217*3ec4d323SEdmund Nadolski { 218*3ec4d323SEdmund Nadolski if ((!sc) || (oldcount == 0 && newcount < 1)) 219*3ec4d323SEdmund Nadolski return; 220*3ec4d323SEdmund Nadolski 221*3ec4d323SEdmund Nadolski if (oldcount > 0 && newcount < 1) 222*3ec4d323SEdmund Nadolski sc->share_count--; 223*3ec4d323SEdmund Nadolski else if (oldcount < 1 && newcount > 0) 224*3ec4d323SEdmund Nadolski sc->share_count++; 225*3ec4d323SEdmund Nadolski } 226*3ec4d323SEdmund Nadolski 22786d5f994SEdmund Nadolski /* 22886d5f994SEdmund Nadolski * Add @newref to the @root rbtree, merging identical refs. 22986d5f994SEdmund Nadolski * 230*3ec4d323SEdmund Nadolski * Callers should assume that newref has been freed after calling. 23186d5f994SEdmund Nadolski */ 23200142756SJeff Mahoney static void prelim_ref_insert(const struct btrfs_fs_info *fs_info, 23300142756SJeff Mahoney struct preftree *preftree, 234*3ec4d323SEdmund Nadolski struct prelim_ref *newref, 235*3ec4d323SEdmund Nadolski struct share_check *sc) 23686d5f994SEdmund Nadolski { 23786d5f994SEdmund Nadolski struct rb_root *root; 23886d5f994SEdmund Nadolski struct rb_node **p; 23986d5f994SEdmund Nadolski struct rb_node *parent = NULL; 24086d5f994SEdmund Nadolski struct prelim_ref *ref; 24186d5f994SEdmund Nadolski int result; 24286d5f994SEdmund Nadolski 24386d5f994SEdmund Nadolski root = &preftree->root; 24486d5f994SEdmund Nadolski p = &root->rb_node; 24586d5f994SEdmund Nadolski 24686d5f994SEdmund Nadolski while (*p) { 24786d5f994SEdmund Nadolski parent = *p; 24886d5f994SEdmund Nadolski ref = rb_entry(parent, struct prelim_ref, rbnode); 24986d5f994SEdmund Nadolski result = prelim_ref_compare(ref, newref); 25086d5f994SEdmund Nadolski if (result < 0) { 25186d5f994SEdmund Nadolski p = &(*p)->rb_left; 25286d5f994SEdmund Nadolski } else if (result > 0) { 25386d5f994SEdmund Nadolski p = &(*p)->rb_right; 25486d5f994SEdmund Nadolski } else { 25586d5f994SEdmund Nadolski /* Identical refs, merge them and free @newref */ 25686d5f994SEdmund Nadolski struct extent_inode_elem *eie = ref->inode_list; 25786d5f994SEdmund Nadolski 25886d5f994SEdmund Nadolski while (eie && eie->next) 25986d5f994SEdmund Nadolski eie = eie->next; 26086d5f994SEdmund Nadolski 26186d5f994SEdmund Nadolski if (!eie) 26286d5f994SEdmund Nadolski ref->inode_list = newref->inode_list; 26386d5f994SEdmund Nadolski else 26486d5f994SEdmund Nadolski eie->next = newref->inode_list; 26500142756SJeff Mahoney trace_btrfs_prelim_ref_merge(fs_info, ref, newref, 26600142756SJeff Mahoney preftree->count); 267*3ec4d323SEdmund Nadolski /* 268*3ec4d323SEdmund Nadolski * A delayed ref can have newref->count < 0. 269*3ec4d323SEdmund Nadolski * The ref->count is updated to follow any 270*3ec4d323SEdmund Nadolski * BTRFS_[ADD|DROP]_DELAYED_REF actions. 271*3ec4d323SEdmund Nadolski */ 272*3ec4d323SEdmund Nadolski update_share_count(sc, ref->count, 273*3ec4d323SEdmund Nadolski ref->count + newref->count); 27486d5f994SEdmund Nadolski ref->count += newref->count; 27586d5f994SEdmund Nadolski free_pref(newref); 27686d5f994SEdmund Nadolski return; 27786d5f994SEdmund Nadolski } 27886d5f994SEdmund Nadolski } 27986d5f994SEdmund Nadolski 280*3ec4d323SEdmund Nadolski update_share_count(sc, 0, newref->count); 2816c336b21SJeff Mahoney preftree->count++; 28200142756SJeff Mahoney trace_btrfs_prelim_ref_insert(fs_info, newref, NULL, preftree->count); 28386d5f994SEdmund Nadolski rb_link_node(&newref->rbnode, parent, p); 28486d5f994SEdmund Nadolski rb_insert_color(&newref->rbnode, root); 28586d5f994SEdmund Nadolski } 28686d5f994SEdmund Nadolski 28786d5f994SEdmund Nadolski /* 28886d5f994SEdmund Nadolski * Release the entire tree. We don't care about internal consistency so 28986d5f994SEdmund Nadolski * just free everything and then reset the tree root. 29086d5f994SEdmund Nadolski */ 29186d5f994SEdmund Nadolski static void prelim_release(struct preftree *preftree) 29286d5f994SEdmund Nadolski { 29386d5f994SEdmund Nadolski struct prelim_ref *ref, *next_ref; 29486d5f994SEdmund Nadolski 29586d5f994SEdmund Nadolski rbtree_postorder_for_each_entry_safe(ref, next_ref, &preftree->root, 29686d5f994SEdmund Nadolski rbnode) 29786d5f994SEdmund Nadolski free_pref(ref); 29886d5f994SEdmund Nadolski 29986d5f994SEdmund Nadolski preftree->root = RB_ROOT; 3006c336b21SJeff Mahoney preftree->count = 0; 30186d5f994SEdmund Nadolski } 30286d5f994SEdmund Nadolski 303d5c88b73SJan Schmidt /* 304d5c88b73SJan Schmidt * the rules for all callers of this function are: 305d5c88b73SJan Schmidt * - obtaining the parent is the goal 306d5c88b73SJan Schmidt * - if you add a key, you must know that it is a correct key 307d5c88b73SJan Schmidt * - if you cannot add the parent or a correct key, then we will look into the 308d5c88b73SJan Schmidt * block later to set a correct key 309d5c88b73SJan Schmidt * 310d5c88b73SJan Schmidt * delayed refs 311d5c88b73SJan Schmidt * ============ 312d5c88b73SJan Schmidt * backref type | shared | indirect | shared | indirect 313d5c88b73SJan Schmidt * information | tree | tree | data | data 314d5c88b73SJan Schmidt * --------------------+--------+----------+--------+---------- 315d5c88b73SJan Schmidt * parent logical | y | - | - | - 316d5c88b73SJan Schmidt * key to resolve | - | y | y | y 317d5c88b73SJan Schmidt * tree block logical | - | - | - | - 318d5c88b73SJan Schmidt * root for resolving | y | y | y | y 319d5c88b73SJan Schmidt * 320d5c88b73SJan Schmidt * - column 1: we've the parent -> done 321d5c88b73SJan Schmidt * - column 2, 3, 4: we use the key to find the parent 322d5c88b73SJan Schmidt * 323d5c88b73SJan Schmidt * on disk refs (inline or keyed) 324d5c88b73SJan Schmidt * ============================== 325d5c88b73SJan Schmidt * backref type | shared | indirect | shared | indirect 326d5c88b73SJan Schmidt * information | tree | tree | data | data 327d5c88b73SJan Schmidt * --------------------+--------+----------+--------+---------- 328d5c88b73SJan Schmidt * parent logical | y | - | y | - 329d5c88b73SJan Schmidt * key to resolve | - | - | - | y 330d5c88b73SJan Schmidt * tree block logical | y | y | y | y 331d5c88b73SJan Schmidt * root for resolving | - | y | y | y 332d5c88b73SJan Schmidt * 333d5c88b73SJan Schmidt * - column 1, 3: we've the parent -> done 334d5c88b73SJan Schmidt * - column 2: we take the first key from the block to find the parent 335e0c476b1SJeff Mahoney * (see add_missing_keys) 336d5c88b73SJan Schmidt * - column 4: we use the key to find the parent 337d5c88b73SJan Schmidt * 338d5c88b73SJan Schmidt * additional information that's available but not required to find the parent 339d5c88b73SJan Schmidt * block might help in merging entries to gain some speed. 340d5c88b73SJan Schmidt */ 34100142756SJeff Mahoney static int add_prelim_ref(const struct btrfs_fs_info *fs_info, 34200142756SJeff Mahoney struct preftree *preftree, u64 root_id, 343e0c476b1SJeff Mahoney const struct btrfs_key *key, int level, u64 parent, 344*3ec4d323SEdmund Nadolski u64 wanted_disk_byte, int count, 345*3ec4d323SEdmund Nadolski struct share_check *sc, gfp_t gfp_mask) 3468da6d581SJan Schmidt { 347e0c476b1SJeff Mahoney struct prelim_ref *ref; 3488da6d581SJan Schmidt 34948ec4736SLiu Bo if (root_id == BTRFS_DATA_RELOC_TREE_OBJECTID) 35048ec4736SLiu Bo return 0; 35148ec4736SLiu Bo 352b9e9a6cbSWang Shilong ref = kmem_cache_alloc(btrfs_prelim_ref_cache, gfp_mask); 3538da6d581SJan Schmidt if (!ref) 3548da6d581SJan Schmidt return -ENOMEM; 3558da6d581SJan Schmidt 3568da6d581SJan Schmidt ref->root_id = root_id; 357d6589101SFilipe Manana if (key) { 358d5c88b73SJan Schmidt ref->key_for_search = *key; 359d6589101SFilipe Manana /* 360d6589101SFilipe Manana * We can often find data backrefs with an offset that is too 361d6589101SFilipe Manana * large (>= LLONG_MAX, maximum allowed file offset) due to 362d6589101SFilipe Manana * underflows when subtracting a file's offset with the data 363d6589101SFilipe Manana * offset of its corresponding extent data item. This can 364d6589101SFilipe Manana * happen for example in the clone ioctl. 365d6589101SFilipe Manana * So if we detect such case we set the search key's offset to 366d6589101SFilipe Manana * zero to make sure we will find the matching file extent item 367d6589101SFilipe Manana * at add_all_parents(), otherwise we will miss it because the 368d6589101SFilipe Manana * offset taken form the backref is much larger then the offset 369d6589101SFilipe Manana * of the file extent item. This can make us scan a very large 370d6589101SFilipe Manana * number of file extent items, but at least it will not make 371d6589101SFilipe Manana * us miss any. 372d6589101SFilipe Manana * This is an ugly workaround for a behaviour that should have 373d6589101SFilipe Manana * never existed, but it does and a fix for the clone ioctl 374d6589101SFilipe Manana * would touch a lot of places, cause backwards incompatibility 375d6589101SFilipe Manana * and would not fix the problem for extents cloned with older 376d6589101SFilipe Manana * kernels. 377d6589101SFilipe Manana */ 378d6589101SFilipe Manana if (ref->key_for_search.type == BTRFS_EXTENT_DATA_KEY && 379d6589101SFilipe Manana ref->key_for_search.offset >= LLONG_MAX) 380d6589101SFilipe Manana ref->key_for_search.offset = 0; 381d6589101SFilipe Manana } else { 382d5c88b73SJan Schmidt memset(&ref->key_for_search, 0, sizeof(ref->key_for_search)); 383d6589101SFilipe Manana } 3848da6d581SJan Schmidt 3853301958bSJan Schmidt ref->inode_list = NULL; 3868da6d581SJan Schmidt ref->level = level; 3878da6d581SJan Schmidt ref->count = count; 3888da6d581SJan Schmidt ref->parent = parent; 3898da6d581SJan Schmidt ref->wanted_disk_byte = wanted_disk_byte; 390*3ec4d323SEdmund Nadolski prelim_ref_insert(fs_info, preftree, ref, sc); 391*3ec4d323SEdmund Nadolski return extent_is_shared(sc); 3928da6d581SJan Schmidt } 3938da6d581SJan Schmidt 39486d5f994SEdmund Nadolski /* direct refs use root == 0, key == NULL */ 39500142756SJeff Mahoney static int add_direct_ref(const struct btrfs_fs_info *fs_info, 39600142756SJeff Mahoney struct preftrees *preftrees, int level, u64 parent, 397*3ec4d323SEdmund Nadolski u64 wanted_disk_byte, int count, 398*3ec4d323SEdmund Nadolski struct share_check *sc, gfp_t gfp_mask) 39986d5f994SEdmund Nadolski { 40000142756SJeff Mahoney return add_prelim_ref(fs_info, &preftrees->direct, 0, NULL, level, 401*3ec4d323SEdmund Nadolski parent, wanted_disk_byte, count, sc, gfp_mask); 40286d5f994SEdmund Nadolski } 40386d5f994SEdmund Nadolski 40486d5f994SEdmund Nadolski /* indirect refs use parent == 0 */ 40500142756SJeff Mahoney static int add_indirect_ref(const struct btrfs_fs_info *fs_info, 40600142756SJeff Mahoney struct preftrees *preftrees, u64 root_id, 40786d5f994SEdmund Nadolski const struct btrfs_key *key, int level, 408*3ec4d323SEdmund Nadolski u64 wanted_disk_byte, int count, 409*3ec4d323SEdmund Nadolski struct share_check *sc, gfp_t gfp_mask) 41086d5f994SEdmund Nadolski { 41186d5f994SEdmund Nadolski struct preftree *tree = &preftrees->indirect; 41286d5f994SEdmund Nadolski 41386d5f994SEdmund Nadolski if (!key) 41486d5f994SEdmund Nadolski tree = &preftrees->indirect_missing_keys; 41500142756SJeff Mahoney return add_prelim_ref(fs_info, tree, root_id, key, level, 0, 416*3ec4d323SEdmund Nadolski wanted_disk_byte, count, sc, gfp_mask); 41786d5f994SEdmund Nadolski } 41886d5f994SEdmund Nadolski 4198da6d581SJan Schmidt static int add_all_parents(struct btrfs_root *root, struct btrfs_path *path, 420e0c476b1SJeff Mahoney struct ulist *parents, struct prelim_ref *ref, 42144853868SJosef Bacik int level, u64 time_seq, const u64 *extent_item_pos, 42244853868SJosef Bacik u64 total_refs) 4238da6d581SJan Schmidt { 42469bca40dSAlexander Block int ret = 0; 42569bca40dSAlexander Block int slot; 42669bca40dSAlexander Block struct extent_buffer *eb; 42769bca40dSAlexander Block struct btrfs_key key; 4287ef81ac8SJosef Bacik struct btrfs_key *key_for_search = &ref->key_for_search; 4298da6d581SJan Schmidt struct btrfs_file_extent_item *fi; 430ed8c4913SJosef Bacik struct extent_inode_elem *eie = NULL, *old = NULL; 4318da6d581SJan Schmidt u64 disk_byte; 4327ef81ac8SJosef Bacik u64 wanted_disk_byte = ref->wanted_disk_byte; 4337ef81ac8SJosef Bacik u64 count = 0; 4348da6d581SJan Schmidt 43569bca40dSAlexander Block if (level != 0) { 43669bca40dSAlexander Block eb = path->nodes[level]; 43769bca40dSAlexander Block ret = ulist_add(parents, eb->start, 0, GFP_NOFS); 4383301958bSJan Schmidt if (ret < 0) 4393301958bSJan Schmidt return ret; 4408da6d581SJan Schmidt return 0; 44169bca40dSAlexander Block } 4428da6d581SJan Schmidt 4438da6d581SJan Schmidt /* 44469bca40dSAlexander Block * We normally enter this function with the path already pointing to 44569bca40dSAlexander Block * the first item to check. But sometimes, we may enter it with 44669bca40dSAlexander Block * slot==nritems. In that case, go to the next leaf before we continue. 4478da6d581SJan Schmidt */ 44821633fc6SQu Wenruo if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) { 449de47c9d3SEdmund Nadolski if (time_seq == SEQ_LAST) 45021633fc6SQu Wenruo ret = btrfs_next_leaf(root, path); 45121633fc6SQu Wenruo else 4523d7806ecSJan Schmidt ret = btrfs_next_old_leaf(root, path, time_seq); 45321633fc6SQu Wenruo } 4548da6d581SJan Schmidt 45544853868SJosef Bacik while (!ret && count < total_refs) { 4568da6d581SJan Schmidt eb = path->nodes[0]; 45769bca40dSAlexander Block slot = path->slots[0]; 45869bca40dSAlexander Block 45969bca40dSAlexander Block btrfs_item_key_to_cpu(eb, &key, slot); 46069bca40dSAlexander Block 46169bca40dSAlexander Block if (key.objectid != key_for_search->objectid || 46269bca40dSAlexander Block key.type != BTRFS_EXTENT_DATA_KEY) 46369bca40dSAlexander Block break; 46469bca40dSAlexander Block 46569bca40dSAlexander Block fi = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item); 4668da6d581SJan Schmidt disk_byte = btrfs_file_extent_disk_bytenr(eb, fi); 46769bca40dSAlexander Block 46869bca40dSAlexander Block if (disk_byte == wanted_disk_byte) { 46969bca40dSAlexander Block eie = NULL; 470ed8c4913SJosef Bacik old = NULL; 4717ef81ac8SJosef Bacik count++; 47269bca40dSAlexander Block if (extent_item_pos) { 47369bca40dSAlexander Block ret = check_extent_in_eb(&key, eb, fi, 47469bca40dSAlexander Block *extent_item_pos, 47569bca40dSAlexander Block &eie); 47669bca40dSAlexander Block if (ret < 0) 47769bca40dSAlexander Block break; 4788da6d581SJan Schmidt } 479ed8c4913SJosef Bacik if (ret > 0) 480ed8c4913SJosef Bacik goto next; 4814eb1f66dSTakashi Iwai ret = ulist_add_merge_ptr(parents, eb->start, 4824eb1f66dSTakashi Iwai eie, (void **)&old, GFP_NOFS); 48369bca40dSAlexander Block if (ret < 0) 48469bca40dSAlexander Block break; 485ed8c4913SJosef Bacik if (!ret && extent_item_pos) { 486ed8c4913SJosef Bacik while (old->next) 487ed8c4913SJosef Bacik old = old->next; 488ed8c4913SJosef Bacik old->next = eie; 48969bca40dSAlexander Block } 490f05c4746SWang Shilong eie = NULL; 49169bca40dSAlexander Block } 492ed8c4913SJosef Bacik next: 493de47c9d3SEdmund Nadolski if (time_seq == SEQ_LAST) 49421633fc6SQu Wenruo ret = btrfs_next_item(root, path); 49521633fc6SQu Wenruo else 49669bca40dSAlexander Block ret = btrfs_next_old_item(root, path, time_seq); 4978da6d581SJan Schmidt } 4988da6d581SJan Schmidt 49969bca40dSAlexander Block if (ret > 0) 50069bca40dSAlexander Block ret = 0; 501f05c4746SWang Shilong else if (ret < 0) 502f05c4746SWang Shilong free_inode_elem_list(eie); 50369bca40dSAlexander Block return ret; 5048da6d581SJan Schmidt } 5058da6d581SJan Schmidt 5068da6d581SJan Schmidt /* 5078da6d581SJan Schmidt * resolve an indirect backref in the form (root_id, key, level) 5088da6d581SJan Schmidt * to a logical address 5098da6d581SJan Schmidt */ 510e0c476b1SJeff Mahoney static int resolve_indirect_ref(struct btrfs_fs_info *fs_info, 511da61d31aSJosef Bacik struct btrfs_path *path, u64 time_seq, 512e0c476b1SJeff Mahoney struct prelim_ref *ref, struct ulist *parents, 51344853868SJosef Bacik const u64 *extent_item_pos, u64 total_refs) 5148da6d581SJan Schmidt { 5158da6d581SJan Schmidt struct btrfs_root *root; 5168da6d581SJan Schmidt struct btrfs_key root_key; 5178da6d581SJan Schmidt struct extent_buffer *eb; 5188da6d581SJan Schmidt int ret = 0; 5198da6d581SJan Schmidt int root_level; 5208da6d581SJan Schmidt int level = ref->level; 521538f72cdSWang Shilong int index; 5228da6d581SJan Schmidt 5238da6d581SJan Schmidt root_key.objectid = ref->root_id; 5248da6d581SJan Schmidt root_key.type = BTRFS_ROOT_ITEM_KEY; 5258da6d581SJan Schmidt root_key.offset = (u64)-1; 526538f72cdSWang Shilong 527538f72cdSWang Shilong index = srcu_read_lock(&fs_info->subvol_srcu); 528538f72cdSWang Shilong 5292d9e9776SJosef Bacik root = btrfs_get_fs_root(fs_info, &root_key, false); 5308da6d581SJan Schmidt if (IS_ERR(root)) { 531538f72cdSWang Shilong srcu_read_unlock(&fs_info->subvol_srcu, index); 5328da6d581SJan Schmidt ret = PTR_ERR(root); 5338da6d581SJan Schmidt goto out; 5348da6d581SJan Schmidt } 5358da6d581SJan Schmidt 536f5ee5c9aSJeff Mahoney if (btrfs_is_testing(fs_info)) { 537d9ee522bSJosef Bacik srcu_read_unlock(&fs_info->subvol_srcu, index); 538d9ee522bSJosef Bacik ret = -ENOENT; 539d9ee522bSJosef Bacik goto out; 540d9ee522bSJosef Bacik } 541d9ee522bSJosef Bacik 5429e351cc8SJosef Bacik if (path->search_commit_root) 5439e351cc8SJosef Bacik root_level = btrfs_header_level(root->commit_root); 544de47c9d3SEdmund Nadolski else if (time_seq == SEQ_LAST) 54521633fc6SQu Wenruo root_level = btrfs_header_level(root->node); 5469e351cc8SJosef Bacik else 5475b6602e7SJan Schmidt root_level = btrfs_old_root_level(root, time_seq); 5488da6d581SJan Schmidt 549538f72cdSWang Shilong if (root_level + 1 == level) { 550538f72cdSWang Shilong srcu_read_unlock(&fs_info->subvol_srcu, index); 5518da6d581SJan Schmidt goto out; 552538f72cdSWang Shilong } 5538da6d581SJan Schmidt 5548da6d581SJan Schmidt path->lowest_level = level; 555de47c9d3SEdmund Nadolski if (time_seq == SEQ_LAST) 55621633fc6SQu Wenruo ret = btrfs_search_slot(NULL, root, &ref->key_for_search, path, 55721633fc6SQu Wenruo 0, 0); 55821633fc6SQu Wenruo else 55921633fc6SQu Wenruo ret = btrfs_search_old_slot(root, &ref->key_for_search, path, 56021633fc6SQu Wenruo time_seq); 561538f72cdSWang Shilong 562538f72cdSWang Shilong /* root node has been locked, we can release @subvol_srcu safely here */ 563538f72cdSWang Shilong srcu_read_unlock(&fs_info->subvol_srcu, index); 564538f72cdSWang Shilong 565ab8d0fc4SJeff Mahoney btrfs_debug(fs_info, 566ab8d0fc4SJeff Mahoney "search slot in root %llu (level %d, ref count %d) returned %d for key (%llu %u %llu)", 567c1c9ff7cSGeert Uytterhoeven ref->root_id, level, ref->count, ret, 568c1c9ff7cSGeert Uytterhoeven ref->key_for_search.objectid, ref->key_for_search.type, 569c1c9ff7cSGeert Uytterhoeven ref->key_for_search.offset); 5708da6d581SJan Schmidt if (ret < 0) 5718da6d581SJan Schmidt goto out; 5728da6d581SJan Schmidt 5738da6d581SJan Schmidt eb = path->nodes[level]; 5749345457fSJan Schmidt while (!eb) { 575fae7f21cSDulshani Gunawardhana if (WARN_ON(!level)) { 5768da6d581SJan Schmidt ret = 1; 5778da6d581SJan Schmidt goto out; 5788da6d581SJan Schmidt } 5799345457fSJan Schmidt level--; 5809345457fSJan Schmidt eb = path->nodes[level]; 5819345457fSJan Schmidt } 5828da6d581SJan Schmidt 5837ef81ac8SJosef Bacik ret = add_all_parents(root, path, parents, ref, level, time_seq, 58444853868SJosef Bacik extent_item_pos, total_refs); 5858da6d581SJan Schmidt out: 586da61d31aSJosef Bacik path->lowest_level = 0; 587da61d31aSJosef Bacik btrfs_release_path(path); 5888da6d581SJan Schmidt return ret; 5898da6d581SJan Schmidt } 5908da6d581SJan Schmidt 5914dae077aSJeff Mahoney static struct extent_inode_elem * 5924dae077aSJeff Mahoney unode_aux_to_inode_list(struct ulist_node *node) 5934dae077aSJeff Mahoney { 5944dae077aSJeff Mahoney if (!node) 5954dae077aSJeff Mahoney return NULL; 5964dae077aSJeff Mahoney return (struct extent_inode_elem *)(uintptr_t)node->aux; 5974dae077aSJeff Mahoney } 5984dae077aSJeff Mahoney 5998da6d581SJan Schmidt /* 60086d5f994SEdmund Nadolski * We maintain three seperate rbtrees: one for direct refs, one for 60186d5f994SEdmund Nadolski * indirect refs which have a key, and one for indirect refs which do not 60286d5f994SEdmund Nadolski * have a key. Each tree does merge on insertion. 60386d5f994SEdmund Nadolski * 60486d5f994SEdmund Nadolski * Once all of the references are located, we iterate over the tree of 60586d5f994SEdmund Nadolski * indirect refs with missing keys. An appropriate key is located and 60686d5f994SEdmund Nadolski * the ref is moved onto the tree for indirect refs. After all missing 60786d5f994SEdmund Nadolski * keys are thus located, we iterate over the indirect ref tree, resolve 60886d5f994SEdmund Nadolski * each reference, and then insert the resolved reference onto the 60986d5f994SEdmund Nadolski * direct tree (merging there too). 61086d5f994SEdmund Nadolski * 61186d5f994SEdmund Nadolski * New backrefs (i.e., for parent nodes) are added to the appropriate 61286d5f994SEdmund Nadolski * rbtree as they are encountered. The new backrefs are subsequently 61386d5f994SEdmund Nadolski * resolved as above. 6148da6d581SJan Schmidt */ 615e0c476b1SJeff Mahoney static int resolve_indirect_refs(struct btrfs_fs_info *fs_info, 616da61d31aSJosef Bacik struct btrfs_path *path, u64 time_seq, 61786d5f994SEdmund Nadolski struct preftrees *preftrees, 618dc046b10SJosef Bacik const u64 *extent_item_pos, u64 total_refs, 619*3ec4d323SEdmund Nadolski struct share_check *sc) 6208da6d581SJan Schmidt { 6218da6d581SJan Schmidt int err; 6228da6d581SJan Schmidt int ret = 0; 6238da6d581SJan Schmidt struct ulist *parents; 6248da6d581SJan Schmidt struct ulist_node *node; 625cd1b413cSJan Schmidt struct ulist_iterator uiter; 62686d5f994SEdmund Nadolski struct rb_node *rnode; 6278da6d581SJan Schmidt 6288da6d581SJan Schmidt parents = ulist_alloc(GFP_NOFS); 6298da6d581SJan Schmidt if (!parents) 6308da6d581SJan Schmidt return -ENOMEM; 6318da6d581SJan Schmidt 6328da6d581SJan Schmidt /* 63386d5f994SEdmund Nadolski * We could trade memory usage for performance here by iterating 63486d5f994SEdmund Nadolski * the tree, allocating new refs for each insertion, and then 63586d5f994SEdmund Nadolski * freeing the entire indirect tree when we're done. In some test 63686d5f994SEdmund Nadolski * cases, the tree can grow quite large (~200k objects). 6378da6d581SJan Schmidt */ 63886d5f994SEdmund Nadolski while ((rnode = rb_first(&preftrees->indirect.root))) { 63986d5f994SEdmund Nadolski struct prelim_ref *ref; 64086d5f994SEdmund Nadolski 64186d5f994SEdmund Nadolski ref = rb_entry(rnode, struct prelim_ref, rbnode); 64286d5f994SEdmund Nadolski if (WARN(ref->parent, 64386d5f994SEdmund Nadolski "BUG: direct ref found in indirect tree")) { 64486d5f994SEdmund Nadolski ret = -EINVAL; 64586d5f994SEdmund Nadolski goto out; 64686d5f994SEdmund Nadolski } 64786d5f994SEdmund Nadolski 64886d5f994SEdmund Nadolski rb_erase(&ref->rbnode, &preftrees->indirect.root); 6496c336b21SJeff Mahoney preftrees->indirect.count--; 65086d5f994SEdmund Nadolski 65186d5f994SEdmund Nadolski if (ref->count == 0) { 65286d5f994SEdmund Nadolski free_pref(ref); 6538da6d581SJan Schmidt continue; 65486d5f994SEdmund Nadolski } 65586d5f994SEdmund Nadolski 656*3ec4d323SEdmund Nadolski if (sc && sc->root_objectid && 657*3ec4d323SEdmund Nadolski ref->root_id != sc->root_objectid) { 65886d5f994SEdmund Nadolski free_pref(ref); 659dc046b10SJosef Bacik ret = BACKREF_FOUND_SHARED; 660dc046b10SJosef Bacik goto out; 661dc046b10SJosef Bacik } 662e0c476b1SJeff Mahoney err = resolve_indirect_ref(fs_info, path, time_seq, ref, 66344853868SJosef Bacik parents, extent_item_pos, 66444853868SJosef Bacik total_refs); 66595def2edSWang Shilong /* 66695def2edSWang Shilong * we can only tolerate ENOENT,otherwise,we should catch error 66795def2edSWang Shilong * and return directly. 66895def2edSWang Shilong */ 66995def2edSWang Shilong if (err == -ENOENT) { 670*3ec4d323SEdmund Nadolski prelim_ref_insert(fs_info, &preftrees->direct, ref, 671*3ec4d323SEdmund Nadolski NULL); 6728da6d581SJan Schmidt continue; 67395def2edSWang Shilong } else if (err) { 67486d5f994SEdmund Nadolski free_pref(ref); 67595def2edSWang Shilong ret = err; 67695def2edSWang Shilong goto out; 67795def2edSWang Shilong } 6788da6d581SJan Schmidt 6798da6d581SJan Schmidt /* we put the first parent into the ref at hand */ 680cd1b413cSJan Schmidt ULIST_ITER_INIT(&uiter); 681cd1b413cSJan Schmidt node = ulist_next(parents, &uiter); 6828da6d581SJan Schmidt ref->parent = node ? node->val : 0; 6834dae077aSJeff Mahoney ref->inode_list = unode_aux_to_inode_list(node); 6848da6d581SJan Schmidt 68586d5f994SEdmund Nadolski /* Add a prelim_ref(s) for any other parent(s). */ 686cd1b413cSJan Schmidt while ((node = ulist_next(parents, &uiter))) { 68786d5f994SEdmund Nadolski struct prelim_ref *new_ref; 68886d5f994SEdmund Nadolski 689b9e9a6cbSWang Shilong new_ref = kmem_cache_alloc(btrfs_prelim_ref_cache, 690b9e9a6cbSWang Shilong GFP_NOFS); 6918da6d581SJan Schmidt if (!new_ref) { 69286d5f994SEdmund Nadolski free_pref(ref); 6938da6d581SJan Schmidt ret = -ENOMEM; 694e36902d4SWang Shilong goto out; 6958da6d581SJan Schmidt } 6968da6d581SJan Schmidt memcpy(new_ref, ref, sizeof(*ref)); 6978da6d581SJan Schmidt new_ref->parent = node->val; 6984dae077aSJeff Mahoney new_ref->inode_list = unode_aux_to_inode_list(node); 699*3ec4d323SEdmund Nadolski prelim_ref_insert(fs_info, &preftrees->direct, 700*3ec4d323SEdmund Nadolski new_ref, NULL); 7018da6d581SJan Schmidt } 70286d5f994SEdmund Nadolski 703*3ec4d323SEdmund Nadolski /* 704*3ec4d323SEdmund Nadolski * Now it's a direct ref, put it in the the direct tree. We must 705*3ec4d323SEdmund Nadolski * do this last because the ref could be merged/freed here. 706*3ec4d323SEdmund Nadolski */ 707*3ec4d323SEdmund Nadolski prelim_ref_insert(fs_info, &preftrees->direct, ref, NULL); 70886d5f994SEdmund Nadolski 7098da6d581SJan Schmidt ulist_reinit(parents); 7109dd14fd6SEdmund Nadolski cond_resched(); 7118da6d581SJan Schmidt } 712e36902d4SWang Shilong out: 7138da6d581SJan Schmidt ulist_free(parents); 7148da6d581SJan Schmidt return ret; 7158da6d581SJan Schmidt } 7168da6d581SJan Schmidt 717d5c88b73SJan Schmidt /* 718d5c88b73SJan Schmidt * read tree blocks and add keys where required. 719d5c88b73SJan Schmidt */ 720e0c476b1SJeff Mahoney static int add_missing_keys(struct btrfs_fs_info *fs_info, 72186d5f994SEdmund Nadolski struct preftrees *preftrees) 722d5c88b73SJan Schmidt { 723e0c476b1SJeff Mahoney struct prelim_ref *ref; 724d5c88b73SJan Schmidt struct extent_buffer *eb; 72586d5f994SEdmund Nadolski struct preftree *tree = &preftrees->indirect_missing_keys; 72686d5f994SEdmund Nadolski struct rb_node *node; 727d5c88b73SJan Schmidt 72886d5f994SEdmund Nadolski while ((node = rb_first(&tree->root))) { 72986d5f994SEdmund Nadolski ref = rb_entry(node, struct prelim_ref, rbnode); 73086d5f994SEdmund Nadolski rb_erase(node, &tree->root); 73186d5f994SEdmund Nadolski 73286d5f994SEdmund Nadolski BUG_ON(ref->parent); /* should not be a direct ref */ 73386d5f994SEdmund Nadolski BUG_ON(ref->key_for_search.type); 734d5c88b73SJan Schmidt BUG_ON(!ref->wanted_disk_byte); 73586d5f994SEdmund Nadolski 7362ff7e61eSJeff Mahoney eb = read_tree_block(fs_info, ref->wanted_disk_byte, 0); 73764c043deSLiu Bo if (IS_ERR(eb)) { 73886d5f994SEdmund Nadolski free_pref(ref); 73964c043deSLiu Bo return PTR_ERR(eb); 74064c043deSLiu Bo } else if (!extent_buffer_uptodate(eb)) { 74186d5f994SEdmund Nadolski free_pref(ref); 742416bc658SJosef Bacik free_extent_buffer(eb); 743416bc658SJosef Bacik return -EIO; 744416bc658SJosef Bacik } 745d5c88b73SJan Schmidt btrfs_tree_read_lock(eb); 746d5c88b73SJan Schmidt if (btrfs_header_level(eb) == 0) 747d5c88b73SJan Schmidt btrfs_item_key_to_cpu(eb, &ref->key_for_search, 0); 748d5c88b73SJan Schmidt else 749d5c88b73SJan Schmidt btrfs_node_key_to_cpu(eb, &ref->key_for_search, 0); 750d5c88b73SJan Schmidt btrfs_tree_read_unlock(eb); 751d5c88b73SJan Schmidt free_extent_buffer(eb); 752*3ec4d323SEdmund Nadolski prelim_ref_insert(fs_info, &preftrees->indirect, ref, NULL); 7539dd14fd6SEdmund Nadolski cond_resched(); 754d5c88b73SJan Schmidt } 755d5c88b73SJan Schmidt return 0; 756d5c88b73SJan Schmidt } 757d5c88b73SJan Schmidt 7588da6d581SJan Schmidt /* 7598da6d581SJan Schmidt * add all currently queued delayed refs from this head whose seq nr is 7608da6d581SJan Schmidt * smaller or equal that seq to the list 7618da6d581SJan Schmidt */ 76200142756SJeff Mahoney static int add_delayed_refs(const struct btrfs_fs_info *fs_info, 76300142756SJeff Mahoney struct btrfs_delayed_ref_head *head, u64 seq, 76486d5f994SEdmund Nadolski struct preftrees *preftrees, u64 *total_refs, 765*3ec4d323SEdmund Nadolski struct share_check *sc) 7668da6d581SJan Schmidt { 767c6fc2454SQu Wenruo struct btrfs_delayed_ref_node *node; 7688da6d581SJan Schmidt struct btrfs_delayed_extent_op *extent_op = head->extent_op; 769d5c88b73SJan Schmidt struct btrfs_key key; 77086d5f994SEdmund Nadolski struct btrfs_key tmp_op_key; 77186d5f994SEdmund Nadolski struct btrfs_key *op_key = NULL; 7728da6d581SJan Schmidt int sgn; 773b1375d64SJan Schmidt int ret = 0; 7748da6d581SJan Schmidt 77586d5f994SEdmund Nadolski if (extent_op && extent_op->update_key) { 77686d5f994SEdmund Nadolski btrfs_disk_key_to_cpu(&tmp_op_key, &extent_op->key); 77786d5f994SEdmund Nadolski op_key = &tmp_op_key; 77886d5f994SEdmund Nadolski } 7798da6d581SJan Schmidt 780d7df2c79SJosef Bacik spin_lock(&head->lock); 781c6fc2454SQu Wenruo list_for_each_entry(node, &head->ref_list, list) { 7828da6d581SJan Schmidt if (node->seq > seq) 7838da6d581SJan Schmidt continue; 7848da6d581SJan Schmidt 7858da6d581SJan Schmidt switch (node->action) { 7868da6d581SJan Schmidt case BTRFS_ADD_DELAYED_EXTENT: 7878da6d581SJan Schmidt case BTRFS_UPDATE_DELAYED_HEAD: 7888da6d581SJan Schmidt WARN_ON(1); 7898da6d581SJan Schmidt continue; 7908da6d581SJan Schmidt case BTRFS_ADD_DELAYED_REF: 7918da6d581SJan Schmidt sgn = 1; 7928da6d581SJan Schmidt break; 7938da6d581SJan Schmidt case BTRFS_DROP_DELAYED_REF: 7948da6d581SJan Schmidt sgn = -1; 7958da6d581SJan Schmidt break; 7968da6d581SJan Schmidt default: 7978da6d581SJan Schmidt BUG_ON(1); 7988da6d581SJan Schmidt } 79944853868SJosef Bacik *total_refs += (node->ref_mod * sgn); 8008da6d581SJan Schmidt switch (node->type) { 8018da6d581SJan Schmidt case BTRFS_TREE_BLOCK_REF_KEY: { 80286d5f994SEdmund Nadolski /* NORMAL INDIRECT METADATA backref */ 8038da6d581SJan Schmidt struct btrfs_delayed_tree_ref *ref; 8048da6d581SJan Schmidt 8058da6d581SJan Schmidt ref = btrfs_delayed_node_to_tree_ref(node); 80600142756SJeff Mahoney ret = add_indirect_ref(fs_info, preftrees, ref->root, 80700142756SJeff Mahoney &tmp_op_key, ref->level + 1, 80800142756SJeff Mahoney node->bytenr, 80986d5f994SEdmund Nadolski node->ref_mod * sgn, 810*3ec4d323SEdmund Nadolski sc, GFP_ATOMIC); 8118da6d581SJan Schmidt break; 8128da6d581SJan Schmidt } 8138da6d581SJan Schmidt case BTRFS_SHARED_BLOCK_REF_KEY: { 81486d5f994SEdmund Nadolski /* SHARED DIRECT METADATA backref */ 8158da6d581SJan Schmidt struct btrfs_delayed_tree_ref *ref; 8168da6d581SJan Schmidt 8178da6d581SJan Schmidt ref = btrfs_delayed_node_to_tree_ref(node); 81886d5f994SEdmund Nadolski 81900142756SJeff Mahoney ret = add_direct_ref(fs_info, preftrees, 82000142756SJeff Mahoney ref->level + 1, ref->parent, 82100142756SJeff Mahoney node->bytenr, node->ref_mod * sgn, 822*3ec4d323SEdmund Nadolski sc, GFP_ATOMIC); 8238da6d581SJan Schmidt break; 8248da6d581SJan Schmidt } 8258da6d581SJan Schmidt case BTRFS_EXTENT_DATA_REF_KEY: { 82686d5f994SEdmund Nadolski /* NORMAL INDIRECT DATA backref */ 8278da6d581SJan Schmidt struct btrfs_delayed_data_ref *ref; 8288da6d581SJan Schmidt ref = btrfs_delayed_node_to_data_ref(node); 8298da6d581SJan Schmidt 8308da6d581SJan Schmidt key.objectid = ref->objectid; 8318da6d581SJan Schmidt key.type = BTRFS_EXTENT_DATA_KEY; 8328da6d581SJan Schmidt key.offset = ref->offset; 833dc046b10SJosef Bacik 834dc046b10SJosef Bacik /* 835dc046b10SJosef Bacik * Found a inum that doesn't match our known inum, we 836dc046b10SJosef Bacik * know it's shared. 837dc046b10SJosef Bacik */ 838*3ec4d323SEdmund Nadolski if (sc && sc->inum && ref->objectid != sc->inum) { 839dc046b10SJosef Bacik ret = BACKREF_FOUND_SHARED; 840*3ec4d323SEdmund Nadolski goto out; 841dc046b10SJosef Bacik } 842dc046b10SJosef Bacik 84300142756SJeff Mahoney ret = add_indirect_ref(fs_info, preftrees, ref->root, 84400142756SJeff Mahoney &key, 0, node->bytenr, 84586d5f994SEdmund Nadolski node->ref_mod * sgn, 846*3ec4d323SEdmund Nadolski sc, GFP_ATOMIC); 8478da6d581SJan Schmidt break; 8488da6d581SJan Schmidt } 8498da6d581SJan Schmidt case BTRFS_SHARED_DATA_REF_KEY: { 85086d5f994SEdmund Nadolski /* SHARED DIRECT FULL backref */ 8518da6d581SJan Schmidt struct btrfs_delayed_data_ref *ref; 8528da6d581SJan Schmidt 8538da6d581SJan Schmidt ref = btrfs_delayed_node_to_data_ref(node); 85486d5f994SEdmund Nadolski 85500142756SJeff Mahoney ret = add_direct_ref(fs_info, preftrees, 0, 85600142756SJeff Mahoney ref->parent, node->bytenr, 85786d5f994SEdmund Nadolski node->ref_mod * sgn, 858*3ec4d323SEdmund Nadolski sc, GFP_ATOMIC); 8598da6d581SJan Schmidt break; 8608da6d581SJan Schmidt } 8618da6d581SJan Schmidt default: 8628da6d581SJan Schmidt WARN_ON(1); 8638da6d581SJan Schmidt } 864*3ec4d323SEdmund Nadolski /* 865*3ec4d323SEdmund Nadolski * We must ignore BACKREF_FOUND_SHARED until all delayed 866*3ec4d323SEdmund Nadolski * refs have been checked. 867*3ec4d323SEdmund Nadolski */ 868*3ec4d323SEdmund Nadolski if (ret && (ret != BACKREF_FOUND_SHARED)) 869d7df2c79SJosef Bacik break; 8708da6d581SJan Schmidt } 871*3ec4d323SEdmund Nadolski if (!ret) 872*3ec4d323SEdmund Nadolski ret = extent_is_shared(sc); 873*3ec4d323SEdmund Nadolski out: 874d7df2c79SJosef Bacik spin_unlock(&head->lock); 875d7df2c79SJosef Bacik return ret; 8768da6d581SJan Schmidt } 8778da6d581SJan Schmidt 8788da6d581SJan Schmidt /* 8798da6d581SJan Schmidt * add all inline backrefs for bytenr to the list 880*3ec4d323SEdmund Nadolski * 881*3ec4d323SEdmund Nadolski * Returns 0 on success, <0 on error, or BACKREF_FOUND_SHARED. 8828da6d581SJan Schmidt */ 88300142756SJeff Mahoney static int add_inline_refs(const struct btrfs_fs_info *fs_info, 88400142756SJeff Mahoney struct btrfs_path *path, u64 bytenr, 88586d5f994SEdmund Nadolski int *info_level, struct preftrees *preftrees, 886*3ec4d323SEdmund Nadolski u64 *total_refs, struct share_check *sc) 8878da6d581SJan Schmidt { 888b1375d64SJan Schmidt int ret = 0; 8898da6d581SJan Schmidt int slot; 8908da6d581SJan Schmidt struct extent_buffer *leaf; 8918da6d581SJan Schmidt struct btrfs_key key; 892261c84b6SJosef Bacik struct btrfs_key found_key; 8938da6d581SJan Schmidt unsigned long ptr; 8948da6d581SJan Schmidt unsigned long end; 8958da6d581SJan Schmidt struct btrfs_extent_item *ei; 8968da6d581SJan Schmidt u64 flags; 8978da6d581SJan Schmidt u64 item_size; 8988da6d581SJan Schmidt 8998da6d581SJan Schmidt /* 9008da6d581SJan Schmidt * enumerate all inline refs 9018da6d581SJan Schmidt */ 9028da6d581SJan Schmidt leaf = path->nodes[0]; 903dadcaf78SJan Schmidt slot = path->slots[0]; 9048da6d581SJan Schmidt 9058da6d581SJan Schmidt item_size = btrfs_item_size_nr(leaf, slot); 9068da6d581SJan Schmidt BUG_ON(item_size < sizeof(*ei)); 9078da6d581SJan Schmidt 9088da6d581SJan Schmidt ei = btrfs_item_ptr(leaf, slot, struct btrfs_extent_item); 9098da6d581SJan Schmidt flags = btrfs_extent_flags(leaf, ei); 91044853868SJosef Bacik *total_refs += btrfs_extent_refs(leaf, ei); 911261c84b6SJosef Bacik btrfs_item_key_to_cpu(leaf, &found_key, slot); 9128da6d581SJan Schmidt 9138da6d581SJan Schmidt ptr = (unsigned long)(ei + 1); 9148da6d581SJan Schmidt end = (unsigned long)ei + item_size; 9158da6d581SJan Schmidt 916261c84b6SJosef Bacik if (found_key.type == BTRFS_EXTENT_ITEM_KEY && 917261c84b6SJosef Bacik flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) { 9188da6d581SJan Schmidt struct btrfs_tree_block_info *info; 9198da6d581SJan Schmidt 9208da6d581SJan Schmidt info = (struct btrfs_tree_block_info *)ptr; 9218da6d581SJan Schmidt *info_level = btrfs_tree_block_level(leaf, info); 9228da6d581SJan Schmidt ptr += sizeof(struct btrfs_tree_block_info); 9238da6d581SJan Schmidt BUG_ON(ptr > end); 924261c84b6SJosef Bacik } else if (found_key.type == BTRFS_METADATA_ITEM_KEY) { 925261c84b6SJosef Bacik *info_level = found_key.offset; 9268da6d581SJan Schmidt } else { 9278da6d581SJan Schmidt BUG_ON(!(flags & BTRFS_EXTENT_FLAG_DATA)); 9288da6d581SJan Schmidt } 9298da6d581SJan Schmidt 9308da6d581SJan Schmidt while (ptr < end) { 9318da6d581SJan Schmidt struct btrfs_extent_inline_ref *iref; 9328da6d581SJan Schmidt u64 offset; 9338da6d581SJan Schmidt int type; 9348da6d581SJan Schmidt 9358da6d581SJan Schmidt iref = (struct btrfs_extent_inline_ref *)ptr; 9368da6d581SJan Schmidt type = btrfs_extent_inline_ref_type(leaf, iref); 9378da6d581SJan Schmidt offset = btrfs_extent_inline_ref_offset(leaf, iref); 9388da6d581SJan Schmidt 9398da6d581SJan Schmidt switch (type) { 9408da6d581SJan Schmidt case BTRFS_SHARED_BLOCK_REF_KEY: 94100142756SJeff Mahoney ret = add_direct_ref(fs_info, preftrees, 94200142756SJeff Mahoney *info_level + 1, offset, 943*3ec4d323SEdmund Nadolski bytenr, 1, NULL, GFP_NOFS); 9448da6d581SJan Schmidt break; 9458da6d581SJan Schmidt case BTRFS_SHARED_DATA_REF_KEY: { 9468da6d581SJan Schmidt struct btrfs_shared_data_ref *sdref; 9478da6d581SJan Schmidt int count; 9488da6d581SJan Schmidt 9498da6d581SJan Schmidt sdref = (struct btrfs_shared_data_ref *)(iref + 1); 9508da6d581SJan Schmidt count = btrfs_shared_data_ref_count(leaf, sdref); 95186d5f994SEdmund Nadolski 95200142756SJeff Mahoney ret = add_direct_ref(fs_info, preftrees, 0, offset, 953*3ec4d323SEdmund Nadolski bytenr, count, sc, GFP_NOFS); 9548da6d581SJan Schmidt break; 9558da6d581SJan Schmidt } 9568da6d581SJan Schmidt case BTRFS_TREE_BLOCK_REF_KEY: 95700142756SJeff Mahoney ret = add_indirect_ref(fs_info, preftrees, offset, 95800142756SJeff Mahoney NULL, *info_level + 1, 959*3ec4d323SEdmund Nadolski bytenr, 1, NULL, GFP_NOFS); 9608da6d581SJan Schmidt break; 9618da6d581SJan Schmidt case BTRFS_EXTENT_DATA_REF_KEY: { 9628da6d581SJan Schmidt struct btrfs_extent_data_ref *dref; 9638da6d581SJan Schmidt int count; 9648da6d581SJan Schmidt u64 root; 9658da6d581SJan Schmidt 9668da6d581SJan Schmidt dref = (struct btrfs_extent_data_ref *)(&iref->offset); 9678da6d581SJan Schmidt count = btrfs_extent_data_ref_count(leaf, dref); 9688da6d581SJan Schmidt key.objectid = btrfs_extent_data_ref_objectid(leaf, 9698da6d581SJan Schmidt dref); 9708da6d581SJan Schmidt key.type = BTRFS_EXTENT_DATA_KEY; 9718da6d581SJan Schmidt key.offset = btrfs_extent_data_ref_offset(leaf, dref); 972dc046b10SJosef Bacik 973*3ec4d323SEdmund Nadolski if (sc && sc->inum && key.objectid != sc->inum) { 974dc046b10SJosef Bacik ret = BACKREF_FOUND_SHARED; 975dc046b10SJosef Bacik break; 976dc046b10SJosef Bacik } 977dc046b10SJosef Bacik 9788da6d581SJan Schmidt root = btrfs_extent_data_ref_root(leaf, dref); 97986d5f994SEdmund Nadolski 98000142756SJeff Mahoney ret = add_indirect_ref(fs_info, preftrees, root, 98100142756SJeff Mahoney &key, 0, bytenr, count, 982*3ec4d323SEdmund Nadolski sc, GFP_NOFS); 9838da6d581SJan Schmidt break; 9848da6d581SJan Schmidt } 9858da6d581SJan Schmidt default: 9868da6d581SJan Schmidt WARN_ON(1); 9878da6d581SJan Schmidt } 9881149ab6bSWang Shilong if (ret) 9891149ab6bSWang Shilong return ret; 9908da6d581SJan Schmidt ptr += btrfs_extent_inline_ref_size(type); 9918da6d581SJan Schmidt } 9928da6d581SJan Schmidt 9938da6d581SJan Schmidt return 0; 9948da6d581SJan Schmidt } 9958da6d581SJan Schmidt 9968da6d581SJan Schmidt /* 9978da6d581SJan Schmidt * add all non-inline backrefs for bytenr to the list 998*3ec4d323SEdmund Nadolski * 999*3ec4d323SEdmund Nadolski * Returns 0 on success, <0 on error, or BACKREF_FOUND_SHARED. 10008da6d581SJan Schmidt */ 1001e0c476b1SJeff Mahoney static int add_keyed_refs(struct btrfs_fs_info *fs_info, 10028da6d581SJan Schmidt struct btrfs_path *path, u64 bytenr, 100386d5f994SEdmund Nadolski int info_level, struct preftrees *preftrees, 1004*3ec4d323SEdmund Nadolski struct share_check *sc) 10058da6d581SJan Schmidt { 10068da6d581SJan Schmidt struct btrfs_root *extent_root = fs_info->extent_root; 10078da6d581SJan Schmidt int ret; 10088da6d581SJan Schmidt int slot; 10098da6d581SJan Schmidt struct extent_buffer *leaf; 10108da6d581SJan Schmidt struct btrfs_key key; 10118da6d581SJan Schmidt 10128da6d581SJan Schmidt while (1) { 10138da6d581SJan Schmidt ret = btrfs_next_item(extent_root, path); 10148da6d581SJan Schmidt if (ret < 0) 10158da6d581SJan Schmidt break; 10168da6d581SJan Schmidt if (ret) { 10178da6d581SJan Schmidt ret = 0; 10188da6d581SJan Schmidt break; 10198da6d581SJan Schmidt } 10208da6d581SJan Schmidt 10218da6d581SJan Schmidt slot = path->slots[0]; 10228da6d581SJan Schmidt leaf = path->nodes[0]; 10238da6d581SJan Schmidt btrfs_item_key_to_cpu(leaf, &key, slot); 10248da6d581SJan Schmidt 10258da6d581SJan Schmidt if (key.objectid != bytenr) 10268da6d581SJan Schmidt break; 10278da6d581SJan Schmidt if (key.type < BTRFS_TREE_BLOCK_REF_KEY) 10288da6d581SJan Schmidt continue; 10298da6d581SJan Schmidt if (key.type > BTRFS_SHARED_DATA_REF_KEY) 10308da6d581SJan Schmidt break; 10318da6d581SJan Schmidt 10328da6d581SJan Schmidt switch (key.type) { 10338da6d581SJan Schmidt case BTRFS_SHARED_BLOCK_REF_KEY: 103486d5f994SEdmund Nadolski /* SHARED DIRECT METADATA backref */ 103500142756SJeff Mahoney ret = add_direct_ref(fs_info, preftrees, 103600142756SJeff Mahoney info_level + 1, key.offset, 1037*3ec4d323SEdmund Nadolski bytenr, 1, NULL, GFP_NOFS); 10388da6d581SJan Schmidt break; 10398da6d581SJan Schmidt case BTRFS_SHARED_DATA_REF_KEY: { 104086d5f994SEdmund Nadolski /* SHARED DIRECT FULL backref */ 10418da6d581SJan Schmidt struct btrfs_shared_data_ref *sdref; 10428da6d581SJan Schmidt int count; 10438da6d581SJan Schmidt 10448da6d581SJan Schmidt sdref = btrfs_item_ptr(leaf, slot, 10458da6d581SJan Schmidt struct btrfs_shared_data_ref); 10468da6d581SJan Schmidt count = btrfs_shared_data_ref_count(leaf, sdref); 104700142756SJeff Mahoney ret = add_direct_ref(fs_info, preftrees, 0, 104800142756SJeff Mahoney key.offset, bytenr, count, 1049*3ec4d323SEdmund Nadolski sc, GFP_NOFS); 10508da6d581SJan Schmidt break; 10518da6d581SJan Schmidt } 10528da6d581SJan Schmidt case BTRFS_TREE_BLOCK_REF_KEY: 105386d5f994SEdmund Nadolski /* NORMAL INDIRECT METADATA backref */ 105400142756SJeff Mahoney ret = add_indirect_ref(fs_info, preftrees, key.offset, 105500142756SJeff Mahoney NULL, info_level + 1, bytenr, 1056*3ec4d323SEdmund Nadolski 1, NULL, GFP_NOFS); 10578da6d581SJan Schmidt break; 10588da6d581SJan Schmidt case BTRFS_EXTENT_DATA_REF_KEY: { 105986d5f994SEdmund Nadolski /* NORMAL INDIRECT DATA backref */ 10608da6d581SJan Schmidt struct btrfs_extent_data_ref *dref; 10618da6d581SJan Schmidt int count; 10628da6d581SJan Schmidt u64 root; 10638da6d581SJan Schmidt 10648da6d581SJan Schmidt dref = btrfs_item_ptr(leaf, slot, 10658da6d581SJan Schmidt struct btrfs_extent_data_ref); 10668da6d581SJan Schmidt count = btrfs_extent_data_ref_count(leaf, dref); 10678da6d581SJan Schmidt key.objectid = btrfs_extent_data_ref_objectid(leaf, 10688da6d581SJan Schmidt dref); 10698da6d581SJan Schmidt key.type = BTRFS_EXTENT_DATA_KEY; 10708da6d581SJan Schmidt key.offset = btrfs_extent_data_ref_offset(leaf, dref); 1071dc046b10SJosef Bacik 1072*3ec4d323SEdmund Nadolski if (sc && sc->inum && key.objectid != sc->inum) { 1073dc046b10SJosef Bacik ret = BACKREF_FOUND_SHARED; 1074dc046b10SJosef Bacik break; 1075dc046b10SJosef Bacik } 1076dc046b10SJosef Bacik 10778da6d581SJan Schmidt root = btrfs_extent_data_ref_root(leaf, dref); 107800142756SJeff Mahoney ret = add_indirect_ref(fs_info, preftrees, root, 107900142756SJeff Mahoney &key, 0, bytenr, count, 1080*3ec4d323SEdmund Nadolski sc, GFP_NOFS); 10818da6d581SJan Schmidt break; 10828da6d581SJan Schmidt } 10838da6d581SJan Schmidt default: 10848da6d581SJan Schmidt WARN_ON(1); 10858da6d581SJan Schmidt } 10861149ab6bSWang Shilong if (ret) 10871149ab6bSWang Shilong return ret; 10881149ab6bSWang Shilong 10898da6d581SJan Schmidt } 10908da6d581SJan Schmidt 10918da6d581SJan Schmidt return ret; 10928da6d581SJan Schmidt } 10938da6d581SJan Schmidt 10948da6d581SJan Schmidt /* 10958da6d581SJan Schmidt * this adds all existing backrefs (inline backrefs, backrefs and delayed 10968da6d581SJan Schmidt * refs) for the given bytenr to the refs list, merges duplicates and resolves 10978da6d581SJan Schmidt * indirect refs to their parent bytenr. 10988da6d581SJan Schmidt * When roots are found, they're added to the roots list 10998da6d581SJan Schmidt * 1100de47c9d3SEdmund Nadolski * If time_seq is set to SEQ_LAST, it will not search delayed_refs, and behave 110121633fc6SQu Wenruo * much like trans == NULL case, the difference only lies in it will not 110221633fc6SQu Wenruo * commit root. 110321633fc6SQu Wenruo * The special case is for qgroup to search roots in commit_transaction(). 110421633fc6SQu Wenruo * 1105*3ec4d323SEdmund Nadolski * @sc - if !NULL, then immediately return BACKREF_FOUND_SHARED when a 1106*3ec4d323SEdmund Nadolski * shared extent is detected. 1107*3ec4d323SEdmund Nadolski * 1108*3ec4d323SEdmund Nadolski * Otherwise this returns 0 for success and <0 for an error. 1109*3ec4d323SEdmund Nadolski * 11108da6d581SJan Schmidt * FIXME some caching might speed things up 11118da6d581SJan Schmidt */ 11128da6d581SJan Schmidt static int find_parent_nodes(struct btrfs_trans_handle *trans, 11138da6d581SJan Schmidt struct btrfs_fs_info *fs_info, u64 bytenr, 1114097b8a7cSJan Schmidt u64 time_seq, struct ulist *refs, 1115dc046b10SJosef Bacik struct ulist *roots, const u64 *extent_item_pos, 1116*3ec4d323SEdmund Nadolski struct share_check *sc) 11178da6d581SJan Schmidt { 11188da6d581SJan Schmidt struct btrfs_key key; 11198da6d581SJan Schmidt struct btrfs_path *path; 11208da6d581SJan Schmidt struct btrfs_delayed_ref_root *delayed_refs = NULL; 1121d3b01064SLi Zefan struct btrfs_delayed_ref_head *head; 11228da6d581SJan Schmidt int info_level = 0; 11238da6d581SJan Schmidt int ret; 1124e0c476b1SJeff Mahoney struct prelim_ref *ref; 112586d5f994SEdmund Nadolski struct rb_node *node; 1126f05c4746SWang Shilong struct extent_inode_elem *eie = NULL; 112786d5f994SEdmund Nadolski /* total of both direct AND indirect refs! */ 112844853868SJosef Bacik u64 total_refs = 0; 112986d5f994SEdmund Nadolski struct preftrees preftrees = { 113086d5f994SEdmund Nadolski .direct = PREFTREE_INIT, 113186d5f994SEdmund Nadolski .indirect = PREFTREE_INIT, 113286d5f994SEdmund Nadolski .indirect_missing_keys = PREFTREE_INIT 113386d5f994SEdmund Nadolski }; 11348da6d581SJan Schmidt 11358da6d581SJan Schmidt key.objectid = bytenr; 11368da6d581SJan Schmidt key.offset = (u64)-1; 1137261c84b6SJosef Bacik if (btrfs_fs_incompat(fs_info, SKINNY_METADATA)) 1138261c84b6SJosef Bacik key.type = BTRFS_METADATA_ITEM_KEY; 1139261c84b6SJosef Bacik else 1140261c84b6SJosef Bacik key.type = BTRFS_EXTENT_ITEM_KEY; 11418da6d581SJan Schmidt 11428da6d581SJan Schmidt path = btrfs_alloc_path(); 11438da6d581SJan Schmidt if (!path) 11448da6d581SJan Schmidt return -ENOMEM; 1145e84752d4SWang Shilong if (!trans) { 1146da61d31aSJosef Bacik path->search_commit_root = 1; 1147e84752d4SWang Shilong path->skip_locking = 1; 1148e84752d4SWang Shilong } 11498da6d581SJan Schmidt 1150de47c9d3SEdmund Nadolski if (time_seq == SEQ_LAST) 115121633fc6SQu Wenruo path->skip_locking = 1; 115221633fc6SQu Wenruo 11538da6d581SJan Schmidt /* 11548da6d581SJan Schmidt * grab both a lock on the path and a lock on the delayed ref head. 11558da6d581SJan Schmidt * We need both to get a consistent picture of how the refs look 11568da6d581SJan Schmidt * at a specified point in time 11578da6d581SJan Schmidt */ 11588da6d581SJan Schmidt again: 1159d3b01064SLi Zefan head = NULL; 1160d3b01064SLi Zefan 11618da6d581SJan Schmidt ret = btrfs_search_slot(trans, fs_info->extent_root, &key, path, 0, 0); 11628da6d581SJan Schmidt if (ret < 0) 11638da6d581SJan Schmidt goto out; 11648da6d581SJan Schmidt BUG_ON(ret == 0); 11658da6d581SJan Schmidt 1166faa2dbf0SJosef Bacik #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS 116721633fc6SQu Wenruo if (trans && likely(trans->type != __TRANS_DUMMY) && 1168de47c9d3SEdmund Nadolski time_seq != SEQ_LAST) { 1169faa2dbf0SJosef Bacik #else 1170de47c9d3SEdmund Nadolski if (trans && time_seq != SEQ_LAST) { 1171faa2dbf0SJosef Bacik #endif 11728da6d581SJan Schmidt /* 11737a3ae2f8SJan Schmidt * look if there are updates for this ref queued and lock the 11747a3ae2f8SJan Schmidt * head 11758da6d581SJan Schmidt */ 11768da6d581SJan Schmidt delayed_refs = &trans->transaction->delayed_refs; 11778da6d581SJan Schmidt spin_lock(&delayed_refs->lock); 1178f72ad18eSLiu Bo head = btrfs_find_delayed_ref_head(delayed_refs, bytenr); 11798da6d581SJan Schmidt if (head) { 11808da6d581SJan Schmidt if (!mutex_trylock(&head->mutex)) { 11816df8cdf5SElena Reshetova refcount_inc(&head->node.refs); 11828da6d581SJan Schmidt spin_unlock(&delayed_refs->lock); 11838da6d581SJan Schmidt 11848da6d581SJan Schmidt btrfs_release_path(path); 11858da6d581SJan Schmidt 11868da6d581SJan Schmidt /* 11878da6d581SJan Schmidt * Mutex was contended, block until it's 11888da6d581SJan Schmidt * released and try again 11898da6d581SJan Schmidt */ 11908da6d581SJan Schmidt mutex_lock(&head->mutex); 11918da6d581SJan Schmidt mutex_unlock(&head->mutex); 11928da6d581SJan Schmidt btrfs_put_delayed_ref(&head->node); 11938da6d581SJan Schmidt goto again; 11948da6d581SJan Schmidt } 1195d7df2c79SJosef Bacik spin_unlock(&delayed_refs->lock); 119600142756SJeff Mahoney ret = add_delayed_refs(fs_info, head, time_seq, 1197*3ec4d323SEdmund Nadolski &preftrees, &total_refs, sc); 1198155725c9SJan Schmidt mutex_unlock(&head->mutex); 1199d7df2c79SJosef Bacik if (ret) 12008da6d581SJan Schmidt goto out; 1201d7df2c79SJosef Bacik } else { 12028da6d581SJan Schmidt spin_unlock(&delayed_refs->lock); 12037a3ae2f8SJan Schmidt } 1204d7df2c79SJosef Bacik } 12058da6d581SJan Schmidt 12068da6d581SJan Schmidt if (path->slots[0]) { 12078da6d581SJan Schmidt struct extent_buffer *leaf; 12088da6d581SJan Schmidt int slot; 12098da6d581SJan Schmidt 1210dadcaf78SJan Schmidt path->slots[0]--; 12118da6d581SJan Schmidt leaf = path->nodes[0]; 1212dadcaf78SJan Schmidt slot = path->slots[0]; 12138da6d581SJan Schmidt btrfs_item_key_to_cpu(leaf, &key, slot); 12148da6d581SJan Schmidt if (key.objectid == bytenr && 1215261c84b6SJosef Bacik (key.type == BTRFS_EXTENT_ITEM_KEY || 1216261c84b6SJosef Bacik key.type == BTRFS_METADATA_ITEM_KEY)) { 121700142756SJeff Mahoney ret = add_inline_refs(fs_info, path, bytenr, 121800142756SJeff Mahoney &info_level, &preftrees, 1219*3ec4d323SEdmund Nadolski &total_refs, sc); 12208da6d581SJan Schmidt if (ret) 12218da6d581SJan Schmidt goto out; 1222e0c476b1SJeff Mahoney ret = add_keyed_refs(fs_info, path, bytenr, info_level, 1223*3ec4d323SEdmund Nadolski &preftrees, sc); 12248da6d581SJan Schmidt if (ret) 12258da6d581SJan Schmidt goto out; 12268da6d581SJan Schmidt } 12278da6d581SJan Schmidt } 122886d5f994SEdmund Nadolski 12298da6d581SJan Schmidt btrfs_release_path(path); 12308da6d581SJan Schmidt 123186d5f994SEdmund Nadolski ret = add_missing_keys(fs_info, &preftrees); 1232d5c88b73SJan Schmidt if (ret) 1233d5c88b73SJan Schmidt goto out; 1234d5c88b73SJan Schmidt 123586d5f994SEdmund Nadolski WARN_ON(!RB_EMPTY_ROOT(&preftrees.indirect_missing_keys.root)); 12368da6d581SJan Schmidt 123786d5f994SEdmund Nadolski ret = resolve_indirect_refs(fs_info, path, time_seq, &preftrees, 1238*3ec4d323SEdmund Nadolski extent_item_pos, total_refs, sc); 12398da6d581SJan Schmidt if (ret) 12408da6d581SJan Schmidt goto out; 12418da6d581SJan Schmidt 124286d5f994SEdmund Nadolski WARN_ON(!RB_EMPTY_ROOT(&preftrees.indirect.root)); 12438da6d581SJan Schmidt 124486d5f994SEdmund Nadolski /* 124586d5f994SEdmund Nadolski * This walks the tree of merged and resolved refs. Tree blocks are 124686d5f994SEdmund Nadolski * read in as needed. Unique entries are added to the ulist, and 124786d5f994SEdmund Nadolski * the list of found roots is updated. 124886d5f994SEdmund Nadolski * 124986d5f994SEdmund Nadolski * We release the entire tree in one go before returning. 125086d5f994SEdmund Nadolski */ 125186d5f994SEdmund Nadolski node = rb_first(&preftrees.direct.root); 125286d5f994SEdmund Nadolski while (node) { 125386d5f994SEdmund Nadolski ref = rb_entry(node, struct prelim_ref, rbnode); 125486d5f994SEdmund Nadolski node = rb_next(&ref->rbnode); 12556c1500f2SJulia Lawall WARN_ON(ref->count < 0); 125698cfee21SWang Shilong if (roots && ref->count && ref->root_id && ref->parent == 0) { 1257*3ec4d323SEdmund Nadolski if (sc && sc->root_objectid && 1258*3ec4d323SEdmund Nadolski ref->root_id != sc->root_objectid) { 1259dc046b10SJosef Bacik ret = BACKREF_FOUND_SHARED; 1260dc046b10SJosef Bacik goto out; 1261dc046b10SJosef Bacik } 1262dc046b10SJosef Bacik 12638da6d581SJan Schmidt /* no parent == root of tree */ 12648da6d581SJan Schmidt ret = ulist_add(roots, ref->root_id, 0, GFP_NOFS); 1265f1723939SWang Shilong if (ret < 0) 1266f1723939SWang Shilong goto out; 12678da6d581SJan Schmidt } 12688da6d581SJan Schmidt if (ref->count && ref->parent) { 12698a56457fSJosef Bacik if (extent_item_pos && !ref->inode_list && 12708a56457fSJosef Bacik ref->level == 0) { 1271976b1908SJan Schmidt struct extent_buffer *eb; 1272707e8a07SDavid Sterba 12732ff7e61eSJeff Mahoney eb = read_tree_block(fs_info, ref->parent, 0); 127464c043deSLiu Bo if (IS_ERR(eb)) { 127564c043deSLiu Bo ret = PTR_ERR(eb); 127664c043deSLiu Bo goto out; 127764c043deSLiu Bo } else if (!extent_buffer_uptodate(eb)) { 1278416bc658SJosef Bacik free_extent_buffer(eb); 1279c16c2e2eSWang Shilong ret = -EIO; 1280c16c2e2eSWang Shilong goto out; 1281416bc658SJosef Bacik } 12826f7ff6d7SFilipe Manana btrfs_tree_read_lock(eb); 12836f7ff6d7SFilipe Manana btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK); 1284976b1908SJan Schmidt ret = find_extent_in_eb(eb, bytenr, 1285976b1908SJan Schmidt *extent_item_pos, &eie); 12866f7ff6d7SFilipe Manana btrfs_tree_read_unlock_blocking(eb); 1287976b1908SJan Schmidt free_extent_buffer(eb); 1288f5929cd8SFilipe David Borba Manana if (ret < 0) 1289f5929cd8SFilipe David Borba Manana goto out; 1290f5929cd8SFilipe David Borba Manana ref->inode_list = eie; 1291976b1908SJan Schmidt } 12924eb1f66dSTakashi Iwai ret = ulist_add_merge_ptr(refs, ref->parent, 12934eb1f66dSTakashi Iwai ref->inode_list, 12944eb1f66dSTakashi Iwai (void **)&eie, GFP_NOFS); 1295f1723939SWang Shilong if (ret < 0) 1296f1723939SWang Shilong goto out; 12973301958bSJan Schmidt if (!ret && extent_item_pos) { 12983301958bSJan Schmidt /* 12993301958bSJan Schmidt * we've recorded that parent, so we must extend 13003301958bSJan Schmidt * its inode list here 13013301958bSJan Schmidt */ 13023301958bSJan Schmidt BUG_ON(!eie); 13033301958bSJan Schmidt while (eie->next) 13043301958bSJan Schmidt eie = eie->next; 13053301958bSJan Schmidt eie->next = ref->inode_list; 13063301958bSJan Schmidt } 1307f05c4746SWang Shilong eie = NULL; 13088da6d581SJan Schmidt } 13099dd14fd6SEdmund Nadolski cond_resched(); 13108da6d581SJan Schmidt } 13118da6d581SJan Schmidt 13128da6d581SJan Schmidt out: 13138da6d581SJan Schmidt btrfs_free_path(path); 131486d5f994SEdmund Nadolski 131586d5f994SEdmund Nadolski prelim_release(&preftrees.direct); 131686d5f994SEdmund Nadolski prelim_release(&preftrees.indirect); 131786d5f994SEdmund Nadolski prelim_release(&preftrees.indirect_missing_keys); 131886d5f994SEdmund Nadolski 1319f05c4746SWang Shilong if (ret < 0) 1320f05c4746SWang Shilong free_inode_elem_list(eie); 13218da6d581SJan Schmidt return ret; 13228da6d581SJan Schmidt } 13238da6d581SJan Schmidt 1324976b1908SJan Schmidt static void free_leaf_list(struct ulist *blocks) 1325976b1908SJan Schmidt { 1326976b1908SJan Schmidt struct ulist_node *node = NULL; 1327976b1908SJan Schmidt struct extent_inode_elem *eie; 1328976b1908SJan Schmidt struct ulist_iterator uiter; 1329976b1908SJan Schmidt 1330976b1908SJan Schmidt ULIST_ITER_INIT(&uiter); 1331976b1908SJan Schmidt while ((node = ulist_next(blocks, &uiter))) { 1332976b1908SJan Schmidt if (!node->aux) 1333976b1908SJan Schmidt continue; 13344dae077aSJeff Mahoney eie = unode_aux_to_inode_list(node); 1335f05c4746SWang Shilong free_inode_elem_list(eie); 1336976b1908SJan Schmidt node->aux = 0; 1337976b1908SJan Schmidt } 1338976b1908SJan Schmidt 1339976b1908SJan Schmidt ulist_free(blocks); 1340976b1908SJan Schmidt } 1341976b1908SJan Schmidt 13428da6d581SJan Schmidt /* 13438da6d581SJan Schmidt * Finds all leafs with a reference to the specified combination of bytenr and 13448da6d581SJan Schmidt * offset. key_list_head will point to a list of corresponding keys (caller must 13458da6d581SJan Schmidt * free each list element). The leafs will be stored in the leafs ulist, which 13468da6d581SJan Schmidt * must be freed with ulist_free. 13478da6d581SJan Schmidt * 13488da6d581SJan Schmidt * returns 0 on success, <0 on error 13498da6d581SJan Schmidt */ 13508da6d581SJan Schmidt static int btrfs_find_all_leafs(struct btrfs_trans_handle *trans, 13518da6d581SJan Schmidt struct btrfs_fs_info *fs_info, u64 bytenr, 1352097b8a7cSJan Schmidt u64 time_seq, struct ulist **leafs, 1353976b1908SJan Schmidt const u64 *extent_item_pos) 13548da6d581SJan Schmidt { 13558da6d581SJan Schmidt int ret; 13568da6d581SJan Schmidt 13578da6d581SJan Schmidt *leafs = ulist_alloc(GFP_NOFS); 135898cfee21SWang Shilong if (!*leafs) 13598da6d581SJan Schmidt return -ENOMEM; 13608da6d581SJan Schmidt 1361afce772eSLu Fengqi ret = find_parent_nodes(trans, fs_info, bytenr, time_seq, 1362*3ec4d323SEdmund Nadolski *leafs, NULL, extent_item_pos, NULL); 13638da6d581SJan Schmidt if (ret < 0 && ret != -ENOENT) { 1364976b1908SJan Schmidt free_leaf_list(*leafs); 13658da6d581SJan Schmidt return ret; 13668da6d581SJan Schmidt } 13678da6d581SJan Schmidt 13688da6d581SJan Schmidt return 0; 13698da6d581SJan Schmidt } 13708da6d581SJan Schmidt 13718da6d581SJan Schmidt /* 13728da6d581SJan Schmidt * walk all backrefs for a given extent to find all roots that reference this 13738da6d581SJan Schmidt * extent. Walking a backref means finding all extents that reference this 13748da6d581SJan Schmidt * extent and in turn walk the backrefs of those, too. Naturally this is a 13758da6d581SJan Schmidt * recursive process, but here it is implemented in an iterative fashion: We 13768da6d581SJan Schmidt * find all referencing extents for the extent in question and put them on a 13778da6d581SJan Schmidt * list. In turn, we find all referencing extents for those, further appending 13788da6d581SJan Schmidt * to the list. The way we iterate the list allows adding more elements after 13798da6d581SJan Schmidt * the current while iterating. The process stops when we reach the end of the 13808da6d581SJan Schmidt * list. Found roots are added to the roots list. 13818da6d581SJan Schmidt * 13828da6d581SJan Schmidt * returns 0 on success, < 0 on error. 13838da6d581SJan Schmidt */ 1384e0c476b1SJeff Mahoney static int btrfs_find_all_roots_safe(struct btrfs_trans_handle *trans, 13858da6d581SJan Schmidt struct btrfs_fs_info *fs_info, u64 bytenr, 1386097b8a7cSJan Schmidt u64 time_seq, struct ulist **roots) 13878da6d581SJan Schmidt { 13888da6d581SJan Schmidt struct ulist *tmp; 13898da6d581SJan Schmidt struct ulist_node *node = NULL; 1390cd1b413cSJan Schmidt struct ulist_iterator uiter; 13918da6d581SJan Schmidt int ret; 13928da6d581SJan Schmidt 13938da6d581SJan Schmidt tmp = ulist_alloc(GFP_NOFS); 13948da6d581SJan Schmidt if (!tmp) 13958da6d581SJan Schmidt return -ENOMEM; 13968da6d581SJan Schmidt *roots = ulist_alloc(GFP_NOFS); 13978da6d581SJan Schmidt if (!*roots) { 13988da6d581SJan Schmidt ulist_free(tmp); 13998da6d581SJan Schmidt return -ENOMEM; 14008da6d581SJan Schmidt } 14018da6d581SJan Schmidt 1402cd1b413cSJan Schmidt ULIST_ITER_INIT(&uiter); 14038da6d581SJan Schmidt while (1) { 1404afce772eSLu Fengqi ret = find_parent_nodes(trans, fs_info, bytenr, time_seq, 1405*3ec4d323SEdmund Nadolski tmp, *roots, NULL, NULL); 14068da6d581SJan Schmidt if (ret < 0 && ret != -ENOENT) { 14078da6d581SJan Schmidt ulist_free(tmp); 14088da6d581SJan Schmidt ulist_free(*roots); 14098da6d581SJan Schmidt return ret; 14108da6d581SJan Schmidt } 1411cd1b413cSJan Schmidt node = ulist_next(tmp, &uiter); 14128da6d581SJan Schmidt if (!node) 14138da6d581SJan Schmidt break; 14148da6d581SJan Schmidt bytenr = node->val; 1415bca1a290SWang Shilong cond_resched(); 14168da6d581SJan Schmidt } 14178da6d581SJan Schmidt 14188da6d581SJan Schmidt ulist_free(tmp); 14198da6d581SJan Schmidt return 0; 14208da6d581SJan Schmidt } 14218da6d581SJan Schmidt 14229e351cc8SJosef Bacik int btrfs_find_all_roots(struct btrfs_trans_handle *trans, 14239e351cc8SJosef Bacik struct btrfs_fs_info *fs_info, u64 bytenr, 14249e351cc8SJosef Bacik u64 time_seq, struct ulist **roots) 14259e351cc8SJosef Bacik { 14269e351cc8SJosef Bacik int ret; 14279e351cc8SJosef Bacik 14289e351cc8SJosef Bacik if (!trans) 14299e351cc8SJosef Bacik down_read(&fs_info->commit_root_sem); 1430e0c476b1SJeff Mahoney ret = btrfs_find_all_roots_safe(trans, fs_info, bytenr, 1431e0c476b1SJeff Mahoney time_seq, roots); 14329e351cc8SJosef Bacik if (!trans) 14339e351cc8SJosef Bacik up_read(&fs_info->commit_root_sem); 14349e351cc8SJosef Bacik return ret; 14359e351cc8SJosef Bacik } 14369e351cc8SJosef Bacik 14372c2ed5aaSMark Fasheh /** 14382c2ed5aaSMark Fasheh * btrfs_check_shared - tell us whether an extent is shared 14392c2ed5aaSMark Fasheh * 14402c2ed5aaSMark Fasheh * btrfs_check_shared uses the backref walking code but will short 14412c2ed5aaSMark Fasheh * circuit as soon as it finds a root or inode that doesn't match the 14422c2ed5aaSMark Fasheh * one passed in. This provides a significant performance benefit for 14432c2ed5aaSMark Fasheh * callers (such as fiemap) which want to know whether the extent is 14442c2ed5aaSMark Fasheh * shared but do not need a ref count. 14452c2ed5aaSMark Fasheh * 1446bb739cf0SEdmund Nadolski * This attempts to allocate a transaction in order to account for 1447bb739cf0SEdmund Nadolski * delayed refs, but continues on even when the alloc fails. 1448bb739cf0SEdmund Nadolski * 14492c2ed5aaSMark Fasheh * Return: 0 if extent is not shared, 1 if it is shared, < 0 on error. 14502c2ed5aaSMark Fasheh */ 1451bb739cf0SEdmund Nadolski int btrfs_check_shared(struct btrfs_root *root, u64 inum, u64 bytenr) 1452dc046b10SJosef Bacik { 1453bb739cf0SEdmund Nadolski struct btrfs_fs_info *fs_info = root->fs_info; 1454bb739cf0SEdmund Nadolski struct btrfs_trans_handle *trans; 1455dc046b10SJosef Bacik struct ulist *tmp = NULL; 1456dc046b10SJosef Bacik struct ulist *roots = NULL; 1457dc046b10SJosef Bacik struct ulist_iterator uiter; 1458dc046b10SJosef Bacik struct ulist_node *node; 14593284da7bSDavid Sterba struct seq_list elem = SEQ_LIST_INIT(elem); 1460dc046b10SJosef Bacik int ret = 0; 1461*3ec4d323SEdmund Nadolski struct share_check shared = { 1462*3ec4d323SEdmund Nadolski .root_objectid = root->objectid, 1463*3ec4d323SEdmund Nadolski .inum = inum, 1464*3ec4d323SEdmund Nadolski .share_count = 0, 1465*3ec4d323SEdmund Nadolski }; 1466dc046b10SJosef Bacik 1467dc046b10SJosef Bacik tmp = ulist_alloc(GFP_NOFS); 1468dc046b10SJosef Bacik roots = ulist_alloc(GFP_NOFS); 1469dc046b10SJosef Bacik if (!tmp || !roots) { 1470dc046b10SJosef Bacik ulist_free(tmp); 1471dc046b10SJosef Bacik ulist_free(roots); 1472dc046b10SJosef Bacik return -ENOMEM; 1473dc046b10SJosef Bacik } 1474dc046b10SJosef Bacik 1475bb739cf0SEdmund Nadolski trans = btrfs_join_transaction(root); 1476bb739cf0SEdmund Nadolski if (IS_ERR(trans)) { 1477bb739cf0SEdmund Nadolski trans = NULL; 1478dc046b10SJosef Bacik down_read(&fs_info->commit_root_sem); 1479bb739cf0SEdmund Nadolski } else { 1480bb739cf0SEdmund Nadolski btrfs_get_tree_mod_seq(fs_info, &elem); 1481bb739cf0SEdmund Nadolski } 1482bb739cf0SEdmund Nadolski 1483dc046b10SJosef Bacik ULIST_ITER_INIT(&uiter); 1484dc046b10SJosef Bacik while (1) { 1485dc046b10SJosef Bacik ret = find_parent_nodes(trans, fs_info, bytenr, elem.seq, tmp, 1486*3ec4d323SEdmund Nadolski roots, NULL, &shared); 1487dc046b10SJosef Bacik if (ret == BACKREF_FOUND_SHARED) { 14882c2ed5aaSMark Fasheh /* this is the only condition under which we return 1 */ 1489dc046b10SJosef Bacik ret = 1; 1490dc046b10SJosef Bacik break; 1491dc046b10SJosef Bacik } 1492dc046b10SJosef Bacik if (ret < 0 && ret != -ENOENT) 1493dc046b10SJosef Bacik break; 14942c2ed5aaSMark Fasheh ret = 0; 1495dc046b10SJosef Bacik node = ulist_next(tmp, &uiter); 1496dc046b10SJosef Bacik if (!node) 1497dc046b10SJosef Bacik break; 1498dc046b10SJosef Bacik bytenr = node->val; 1499dc046b10SJosef Bacik cond_resched(); 1500dc046b10SJosef Bacik } 1501bb739cf0SEdmund Nadolski 1502bb739cf0SEdmund Nadolski if (trans) { 1503dc046b10SJosef Bacik btrfs_put_tree_mod_seq(fs_info, &elem); 1504bb739cf0SEdmund Nadolski btrfs_end_transaction(trans); 1505bb739cf0SEdmund Nadolski } else { 1506dc046b10SJosef Bacik up_read(&fs_info->commit_root_sem); 1507bb739cf0SEdmund Nadolski } 1508dc046b10SJosef Bacik ulist_free(tmp); 1509dc046b10SJosef Bacik ulist_free(roots); 1510dc046b10SJosef Bacik return ret; 1511dc046b10SJosef Bacik } 1512dc046b10SJosef Bacik 1513f186373fSMark Fasheh int btrfs_find_one_extref(struct btrfs_root *root, u64 inode_objectid, 1514f186373fSMark Fasheh u64 start_off, struct btrfs_path *path, 1515f186373fSMark Fasheh struct btrfs_inode_extref **ret_extref, 1516f186373fSMark Fasheh u64 *found_off) 1517f186373fSMark Fasheh { 1518f186373fSMark Fasheh int ret, slot; 1519f186373fSMark Fasheh struct btrfs_key key; 1520f186373fSMark Fasheh struct btrfs_key found_key; 1521f186373fSMark Fasheh struct btrfs_inode_extref *extref; 152273980becSJeff Mahoney const struct extent_buffer *leaf; 1523f186373fSMark Fasheh unsigned long ptr; 1524f186373fSMark Fasheh 1525f186373fSMark Fasheh key.objectid = inode_objectid; 1526962a298fSDavid Sterba key.type = BTRFS_INODE_EXTREF_KEY; 1527f186373fSMark Fasheh key.offset = start_off; 1528f186373fSMark Fasheh 1529f186373fSMark Fasheh ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); 1530f186373fSMark Fasheh if (ret < 0) 1531f186373fSMark Fasheh return ret; 1532f186373fSMark Fasheh 1533f186373fSMark Fasheh while (1) { 1534f186373fSMark Fasheh leaf = path->nodes[0]; 1535f186373fSMark Fasheh slot = path->slots[0]; 1536f186373fSMark Fasheh if (slot >= btrfs_header_nritems(leaf)) { 1537f186373fSMark Fasheh /* 1538f186373fSMark Fasheh * If the item at offset is not found, 1539f186373fSMark Fasheh * btrfs_search_slot will point us to the slot 1540f186373fSMark Fasheh * where it should be inserted. In our case 1541f186373fSMark Fasheh * that will be the slot directly before the 1542f186373fSMark Fasheh * next INODE_REF_KEY_V2 item. In the case 1543f186373fSMark Fasheh * that we're pointing to the last slot in a 1544f186373fSMark Fasheh * leaf, we must move one leaf over. 1545f186373fSMark Fasheh */ 1546f186373fSMark Fasheh ret = btrfs_next_leaf(root, path); 1547f186373fSMark Fasheh if (ret) { 1548f186373fSMark Fasheh if (ret >= 1) 1549f186373fSMark Fasheh ret = -ENOENT; 1550f186373fSMark Fasheh break; 1551f186373fSMark Fasheh } 1552f186373fSMark Fasheh continue; 1553f186373fSMark Fasheh } 1554f186373fSMark Fasheh 1555f186373fSMark Fasheh btrfs_item_key_to_cpu(leaf, &found_key, slot); 1556f186373fSMark Fasheh 1557f186373fSMark Fasheh /* 1558f186373fSMark Fasheh * Check that we're still looking at an extended ref key for 1559f186373fSMark Fasheh * this particular objectid. If we have different 1560f186373fSMark Fasheh * objectid or type then there are no more to be found 1561f186373fSMark Fasheh * in the tree and we can exit. 1562f186373fSMark Fasheh */ 1563f186373fSMark Fasheh ret = -ENOENT; 1564f186373fSMark Fasheh if (found_key.objectid != inode_objectid) 1565f186373fSMark Fasheh break; 1566962a298fSDavid Sterba if (found_key.type != BTRFS_INODE_EXTREF_KEY) 1567f186373fSMark Fasheh break; 1568f186373fSMark Fasheh 1569f186373fSMark Fasheh ret = 0; 1570f186373fSMark Fasheh ptr = btrfs_item_ptr_offset(leaf, path->slots[0]); 1571f186373fSMark Fasheh extref = (struct btrfs_inode_extref *)ptr; 1572f186373fSMark Fasheh *ret_extref = extref; 1573f186373fSMark Fasheh if (found_off) 1574f186373fSMark Fasheh *found_off = found_key.offset; 1575f186373fSMark Fasheh break; 1576f186373fSMark Fasheh } 1577f186373fSMark Fasheh 1578f186373fSMark Fasheh return ret; 1579f186373fSMark Fasheh } 1580f186373fSMark Fasheh 158148a3b636SEric Sandeen /* 158248a3b636SEric Sandeen * this iterates to turn a name (from iref/extref) into a full filesystem path. 158348a3b636SEric Sandeen * Elements of the path are separated by '/' and the path is guaranteed to be 158448a3b636SEric Sandeen * 0-terminated. the path is only given within the current file system. 158548a3b636SEric Sandeen * Therefore, it never starts with a '/'. the caller is responsible to provide 158648a3b636SEric Sandeen * "size" bytes in "dest". the dest buffer will be filled backwards. finally, 158748a3b636SEric Sandeen * the start point of the resulting string is returned. this pointer is within 158848a3b636SEric Sandeen * dest, normally. 158948a3b636SEric Sandeen * in case the path buffer would overflow, the pointer is decremented further 159048a3b636SEric Sandeen * as if output was written to the buffer, though no more output is actually 159148a3b636SEric Sandeen * generated. that way, the caller can determine how much space would be 159248a3b636SEric Sandeen * required for the path to fit into the buffer. in that case, the returned 159348a3b636SEric Sandeen * value will be smaller than dest. callers must check this! 159448a3b636SEric Sandeen */ 159596b5bd77SJan Schmidt char *btrfs_ref_to_path(struct btrfs_root *fs_root, struct btrfs_path *path, 1596d24bec3aSMark Fasheh u32 name_len, unsigned long name_off, 1597a542ad1bSJan Schmidt struct extent_buffer *eb_in, u64 parent, 1598a542ad1bSJan Schmidt char *dest, u32 size) 1599a542ad1bSJan Schmidt { 1600a542ad1bSJan Schmidt int slot; 1601a542ad1bSJan Schmidt u64 next_inum; 1602a542ad1bSJan Schmidt int ret; 1603661bec6bSGabriel de Perthuis s64 bytes_left = ((s64)size) - 1; 1604a542ad1bSJan Schmidt struct extent_buffer *eb = eb_in; 1605a542ad1bSJan Schmidt struct btrfs_key found_key; 1606b916a59aSJan Schmidt int leave_spinning = path->leave_spinning; 1607d24bec3aSMark Fasheh struct btrfs_inode_ref *iref; 1608a542ad1bSJan Schmidt 1609a542ad1bSJan Schmidt if (bytes_left >= 0) 1610a542ad1bSJan Schmidt dest[bytes_left] = '\0'; 1611a542ad1bSJan Schmidt 1612b916a59aSJan Schmidt path->leave_spinning = 1; 1613a542ad1bSJan Schmidt while (1) { 1614d24bec3aSMark Fasheh bytes_left -= name_len; 1615a542ad1bSJan Schmidt if (bytes_left >= 0) 1616a542ad1bSJan Schmidt read_extent_buffer(eb, dest + bytes_left, 1617d24bec3aSMark Fasheh name_off, name_len); 1618b916a59aSJan Schmidt if (eb != eb_in) { 16190c0fe3b0SFilipe Manana if (!path->skip_locking) 1620b916a59aSJan Schmidt btrfs_tree_read_unlock_blocking(eb); 1621a542ad1bSJan Schmidt free_extent_buffer(eb); 1622b916a59aSJan Schmidt } 1623c234a24dSDavid Sterba ret = btrfs_find_item(fs_root, path, parent, 0, 1624c234a24dSDavid Sterba BTRFS_INODE_REF_KEY, &found_key); 16258f24b496SJan Schmidt if (ret > 0) 16268f24b496SJan Schmidt ret = -ENOENT; 1627a542ad1bSJan Schmidt if (ret) 1628a542ad1bSJan Schmidt break; 1629d24bec3aSMark Fasheh 1630a542ad1bSJan Schmidt next_inum = found_key.offset; 1631a542ad1bSJan Schmidt 1632a542ad1bSJan Schmidt /* regular exit ahead */ 1633a542ad1bSJan Schmidt if (parent == next_inum) 1634a542ad1bSJan Schmidt break; 1635a542ad1bSJan Schmidt 1636a542ad1bSJan Schmidt slot = path->slots[0]; 1637a542ad1bSJan Schmidt eb = path->nodes[0]; 1638a542ad1bSJan Schmidt /* make sure we can use eb after releasing the path */ 1639b916a59aSJan Schmidt if (eb != eb_in) { 16400c0fe3b0SFilipe Manana if (!path->skip_locking) 1641b916a59aSJan Schmidt btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK); 16420c0fe3b0SFilipe Manana path->nodes[0] = NULL; 16430c0fe3b0SFilipe Manana path->locks[0] = 0; 1644b916a59aSJan Schmidt } 1645a542ad1bSJan Schmidt btrfs_release_path(path); 1646a542ad1bSJan Schmidt iref = btrfs_item_ptr(eb, slot, struct btrfs_inode_ref); 1647d24bec3aSMark Fasheh 1648d24bec3aSMark Fasheh name_len = btrfs_inode_ref_name_len(eb, iref); 1649d24bec3aSMark Fasheh name_off = (unsigned long)(iref + 1); 1650d24bec3aSMark Fasheh 1651a542ad1bSJan Schmidt parent = next_inum; 1652a542ad1bSJan Schmidt --bytes_left; 1653a542ad1bSJan Schmidt if (bytes_left >= 0) 1654a542ad1bSJan Schmidt dest[bytes_left] = '/'; 1655a542ad1bSJan Schmidt } 1656a542ad1bSJan Schmidt 1657a542ad1bSJan Schmidt btrfs_release_path(path); 1658b916a59aSJan Schmidt path->leave_spinning = leave_spinning; 1659a542ad1bSJan Schmidt 1660a542ad1bSJan Schmidt if (ret) 1661a542ad1bSJan Schmidt return ERR_PTR(ret); 1662a542ad1bSJan Schmidt 1663a542ad1bSJan Schmidt return dest + bytes_left; 1664a542ad1bSJan Schmidt } 1665a542ad1bSJan Schmidt 1666a542ad1bSJan Schmidt /* 1667a542ad1bSJan Schmidt * this makes the path point to (logical EXTENT_ITEM *) 1668a542ad1bSJan Schmidt * returns BTRFS_EXTENT_FLAG_DATA for data, BTRFS_EXTENT_FLAG_TREE_BLOCK for 1669a542ad1bSJan Schmidt * tree blocks and <0 on error. 1670a542ad1bSJan Schmidt */ 1671a542ad1bSJan Schmidt int extent_from_logical(struct btrfs_fs_info *fs_info, u64 logical, 167269917e43SLiu Bo struct btrfs_path *path, struct btrfs_key *found_key, 167369917e43SLiu Bo u64 *flags_ret) 1674a542ad1bSJan Schmidt { 1675a542ad1bSJan Schmidt int ret; 1676a542ad1bSJan Schmidt u64 flags; 1677261c84b6SJosef Bacik u64 size = 0; 1678a542ad1bSJan Schmidt u32 item_size; 167973980becSJeff Mahoney const struct extent_buffer *eb; 1680a542ad1bSJan Schmidt struct btrfs_extent_item *ei; 1681a542ad1bSJan Schmidt struct btrfs_key key; 1682a542ad1bSJan Schmidt 1683261c84b6SJosef Bacik if (btrfs_fs_incompat(fs_info, SKINNY_METADATA)) 1684261c84b6SJosef Bacik key.type = BTRFS_METADATA_ITEM_KEY; 1685261c84b6SJosef Bacik else 1686a542ad1bSJan Schmidt key.type = BTRFS_EXTENT_ITEM_KEY; 1687a542ad1bSJan Schmidt key.objectid = logical; 1688a542ad1bSJan Schmidt key.offset = (u64)-1; 1689a542ad1bSJan Schmidt 1690a542ad1bSJan Schmidt ret = btrfs_search_slot(NULL, fs_info->extent_root, &key, path, 0, 0); 1691a542ad1bSJan Schmidt if (ret < 0) 1692a542ad1bSJan Schmidt return ret; 1693a542ad1bSJan Schmidt 1694850a8cdfSWang Shilong ret = btrfs_previous_extent_item(fs_info->extent_root, path, 0); 1695850a8cdfSWang Shilong if (ret) { 1696850a8cdfSWang Shilong if (ret > 0) 1697580f0a67SJosef Bacik ret = -ENOENT; 1698580f0a67SJosef Bacik return ret; 1699580f0a67SJosef Bacik } 1700850a8cdfSWang Shilong btrfs_item_key_to_cpu(path->nodes[0], found_key, path->slots[0]); 1701261c84b6SJosef Bacik if (found_key->type == BTRFS_METADATA_ITEM_KEY) 1702da17066cSJeff Mahoney size = fs_info->nodesize; 1703261c84b6SJosef Bacik else if (found_key->type == BTRFS_EXTENT_ITEM_KEY) 1704261c84b6SJosef Bacik size = found_key->offset; 1705261c84b6SJosef Bacik 1706580f0a67SJosef Bacik if (found_key->objectid > logical || 1707261c84b6SJosef Bacik found_key->objectid + size <= logical) { 1708ab8d0fc4SJeff Mahoney btrfs_debug(fs_info, 1709ab8d0fc4SJeff Mahoney "logical %llu is not within any extent", logical); 1710a542ad1bSJan Schmidt return -ENOENT; 17114692cf58SJan Schmidt } 1712a542ad1bSJan Schmidt 1713a542ad1bSJan Schmidt eb = path->nodes[0]; 1714a542ad1bSJan Schmidt item_size = btrfs_item_size_nr(eb, path->slots[0]); 1715a542ad1bSJan Schmidt BUG_ON(item_size < sizeof(*ei)); 1716a542ad1bSJan Schmidt 1717a542ad1bSJan Schmidt ei = btrfs_item_ptr(eb, path->slots[0], struct btrfs_extent_item); 1718a542ad1bSJan Schmidt flags = btrfs_extent_flags(eb, ei); 1719a542ad1bSJan Schmidt 1720ab8d0fc4SJeff Mahoney btrfs_debug(fs_info, 1721ab8d0fc4SJeff Mahoney "logical %llu is at position %llu within the extent (%llu EXTENT_ITEM %llu) flags %#llx size %u", 1722c1c9ff7cSGeert Uytterhoeven logical, logical - found_key->objectid, found_key->objectid, 1723c1c9ff7cSGeert Uytterhoeven found_key->offset, flags, item_size); 172469917e43SLiu Bo 172569917e43SLiu Bo WARN_ON(!flags_ret); 172669917e43SLiu Bo if (flags_ret) { 1727a542ad1bSJan Schmidt if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) 172869917e43SLiu Bo *flags_ret = BTRFS_EXTENT_FLAG_TREE_BLOCK; 172969917e43SLiu Bo else if (flags & BTRFS_EXTENT_FLAG_DATA) 173069917e43SLiu Bo *flags_ret = BTRFS_EXTENT_FLAG_DATA; 173169917e43SLiu Bo else 173269917e43SLiu Bo BUG_ON(1); 173369917e43SLiu Bo return 0; 173469917e43SLiu Bo } 1735a542ad1bSJan Schmidt 1736a542ad1bSJan Schmidt return -EIO; 1737a542ad1bSJan Schmidt } 1738a542ad1bSJan Schmidt 1739a542ad1bSJan Schmidt /* 1740a542ad1bSJan Schmidt * helper function to iterate extent inline refs. ptr must point to a 0 value 1741a542ad1bSJan Schmidt * for the first call and may be modified. it is used to track state. 1742a542ad1bSJan Schmidt * if more refs exist, 0 is returned and the next call to 1743e0c476b1SJeff Mahoney * get_extent_inline_ref must pass the modified ptr parameter to get the 1744a542ad1bSJan Schmidt * next ref. after the last ref was processed, 1 is returned. 1745a542ad1bSJan Schmidt * returns <0 on error 1746a542ad1bSJan Schmidt */ 1747e0c476b1SJeff Mahoney static int get_extent_inline_ref(unsigned long *ptr, 174873980becSJeff Mahoney const struct extent_buffer *eb, 174973980becSJeff Mahoney const struct btrfs_key *key, 175073980becSJeff Mahoney const struct btrfs_extent_item *ei, 175173980becSJeff Mahoney u32 item_size, 1752a542ad1bSJan Schmidt struct btrfs_extent_inline_ref **out_eiref, 1753a542ad1bSJan Schmidt int *out_type) 1754a542ad1bSJan Schmidt { 1755a542ad1bSJan Schmidt unsigned long end; 1756a542ad1bSJan Schmidt u64 flags; 1757a542ad1bSJan Schmidt struct btrfs_tree_block_info *info; 1758a542ad1bSJan Schmidt 1759a542ad1bSJan Schmidt if (!*ptr) { 1760a542ad1bSJan Schmidt /* first call */ 1761a542ad1bSJan Schmidt flags = btrfs_extent_flags(eb, ei); 1762a542ad1bSJan Schmidt if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) { 17636eda71d0SLiu Bo if (key->type == BTRFS_METADATA_ITEM_KEY) { 17646eda71d0SLiu Bo /* a skinny metadata extent */ 17656eda71d0SLiu Bo *out_eiref = 17666eda71d0SLiu Bo (struct btrfs_extent_inline_ref *)(ei + 1); 17676eda71d0SLiu Bo } else { 17686eda71d0SLiu Bo WARN_ON(key->type != BTRFS_EXTENT_ITEM_KEY); 1769a542ad1bSJan Schmidt info = (struct btrfs_tree_block_info *)(ei + 1); 1770a542ad1bSJan Schmidt *out_eiref = 1771a542ad1bSJan Schmidt (struct btrfs_extent_inline_ref *)(info + 1); 17726eda71d0SLiu Bo } 1773a542ad1bSJan Schmidt } else { 1774a542ad1bSJan Schmidt *out_eiref = (struct btrfs_extent_inline_ref *)(ei + 1); 1775a542ad1bSJan Schmidt } 1776a542ad1bSJan Schmidt *ptr = (unsigned long)*out_eiref; 1777cd857dd6SLiu Bo if ((unsigned long)(*ptr) >= (unsigned long)ei + item_size) 1778a542ad1bSJan Schmidt return -ENOENT; 1779a542ad1bSJan Schmidt } 1780a542ad1bSJan Schmidt 1781a542ad1bSJan Schmidt end = (unsigned long)ei + item_size; 17826eda71d0SLiu Bo *out_eiref = (struct btrfs_extent_inline_ref *)(*ptr); 1783a542ad1bSJan Schmidt *out_type = btrfs_extent_inline_ref_type(eb, *out_eiref); 1784a542ad1bSJan Schmidt 1785a542ad1bSJan Schmidt *ptr += btrfs_extent_inline_ref_size(*out_type); 1786a542ad1bSJan Schmidt WARN_ON(*ptr > end); 1787a542ad1bSJan Schmidt if (*ptr == end) 1788a542ad1bSJan Schmidt return 1; /* last */ 1789a542ad1bSJan Schmidt 1790a542ad1bSJan Schmidt return 0; 1791a542ad1bSJan Schmidt } 1792a542ad1bSJan Schmidt 1793a542ad1bSJan Schmidt /* 1794a542ad1bSJan Schmidt * reads the tree block backref for an extent. tree level and root are returned 1795a542ad1bSJan Schmidt * through out_level and out_root. ptr must point to a 0 value for the first 1796e0c476b1SJeff Mahoney * call and may be modified (see get_extent_inline_ref comment). 1797a542ad1bSJan Schmidt * returns 0 if data was provided, 1 if there was no more data to provide or 1798a542ad1bSJan Schmidt * <0 on error. 1799a542ad1bSJan Schmidt */ 1800a542ad1bSJan Schmidt int tree_backref_for_extent(unsigned long *ptr, struct extent_buffer *eb, 18016eda71d0SLiu Bo struct btrfs_key *key, struct btrfs_extent_item *ei, 18026eda71d0SLiu Bo u32 item_size, u64 *out_root, u8 *out_level) 1803a542ad1bSJan Schmidt { 1804a542ad1bSJan Schmidt int ret; 1805a542ad1bSJan Schmidt int type; 1806a542ad1bSJan Schmidt struct btrfs_extent_inline_ref *eiref; 1807a542ad1bSJan Schmidt 1808a542ad1bSJan Schmidt if (*ptr == (unsigned long)-1) 1809a542ad1bSJan Schmidt return 1; 1810a542ad1bSJan Schmidt 1811a542ad1bSJan Schmidt while (1) { 1812e0c476b1SJeff Mahoney ret = get_extent_inline_ref(ptr, eb, key, ei, item_size, 1813a542ad1bSJan Schmidt &eiref, &type); 1814a542ad1bSJan Schmidt if (ret < 0) 1815a542ad1bSJan Schmidt return ret; 1816a542ad1bSJan Schmidt 1817a542ad1bSJan Schmidt if (type == BTRFS_TREE_BLOCK_REF_KEY || 1818a542ad1bSJan Schmidt type == BTRFS_SHARED_BLOCK_REF_KEY) 1819a542ad1bSJan Schmidt break; 1820a542ad1bSJan Schmidt 1821a542ad1bSJan Schmidt if (ret == 1) 1822a542ad1bSJan Schmidt return 1; 1823a542ad1bSJan Schmidt } 1824a542ad1bSJan Schmidt 1825a542ad1bSJan Schmidt /* we can treat both ref types equally here */ 1826a542ad1bSJan Schmidt *out_root = btrfs_extent_inline_ref_offset(eb, eiref); 1827a1317f45SFilipe Manana 1828a1317f45SFilipe Manana if (key->type == BTRFS_EXTENT_ITEM_KEY) { 1829a1317f45SFilipe Manana struct btrfs_tree_block_info *info; 1830a1317f45SFilipe Manana 1831a1317f45SFilipe Manana info = (struct btrfs_tree_block_info *)(ei + 1); 1832a542ad1bSJan Schmidt *out_level = btrfs_tree_block_level(eb, info); 1833a1317f45SFilipe Manana } else { 1834a1317f45SFilipe Manana ASSERT(key->type == BTRFS_METADATA_ITEM_KEY); 1835a1317f45SFilipe Manana *out_level = (u8)key->offset; 1836a1317f45SFilipe Manana } 1837a542ad1bSJan Schmidt 1838a542ad1bSJan Schmidt if (ret == 1) 1839a542ad1bSJan Schmidt *ptr = (unsigned long)-1; 1840a542ad1bSJan Schmidt 1841a542ad1bSJan Schmidt return 0; 1842a542ad1bSJan Schmidt } 1843a542ad1bSJan Schmidt 1844ab8d0fc4SJeff Mahoney static int iterate_leaf_refs(struct btrfs_fs_info *fs_info, 1845ab8d0fc4SJeff Mahoney struct extent_inode_elem *inode_list, 1846976b1908SJan Schmidt u64 root, u64 extent_item_objectid, 18474692cf58SJan Schmidt iterate_extent_inodes_t *iterate, void *ctx) 1848a542ad1bSJan Schmidt { 1849976b1908SJan Schmidt struct extent_inode_elem *eie; 18504692cf58SJan Schmidt int ret = 0; 1851a542ad1bSJan Schmidt 1852976b1908SJan Schmidt for (eie = inode_list; eie; eie = eie->next) { 1853ab8d0fc4SJeff Mahoney btrfs_debug(fs_info, 1854ab8d0fc4SJeff Mahoney "ref for %llu resolved, key (%llu EXTEND_DATA %llu), root %llu", 1855ab8d0fc4SJeff Mahoney extent_item_objectid, eie->inum, 1856ab8d0fc4SJeff Mahoney eie->offset, root); 1857976b1908SJan Schmidt ret = iterate(eie->inum, eie->offset, root, ctx); 18584692cf58SJan Schmidt if (ret) { 1859ab8d0fc4SJeff Mahoney btrfs_debug(fs_info, 1860ab8d0fc4SJeff Mahoney "stopping iteration for %llu due to ret=%d", 1861976b1908SJan Schmidt extent_item_objectid, ret); 1862a542ad1bSJan Schmidt break; 1863a542ad1bSJan Schmidt } 1864a542ad1bSJan Schmidt } 1865a542ad1bSJan Schmidt 1866a542ad1bSJan Schmidt return ret; 1867a542ad1bSJan Schmidt } 1868a542ad1bSJan Schmidt 1869a542ad1bSJan Schmidt /* 1870a542ad1bSJan Schmidt * calls iterate() for every inode that references the extent identified by 18714692cf58SJan Schmidt * the given parameters. 1872a542ad1bSJan Schmidt * when the iterator function returns a non-zero value, iteration stops. 1873a542ad1bSJan Schmidt */ 1874a542ad1bSJan Schmidt int iterate_extent_inodes(struct btrfs_fs_info *fs_info, 18754692cf58SJan Schmidt u64 extent_item_objectid, u64 extent_item_pos, 18767a3ae2f8SJan Schmidt int search_commit_root, 1877a542ad1bSJan Schmidt iterate_extent_inodes_t *iterate, void *ctx) 1878a542ad1bSJan Schmidt { 1879a542ad1bSJan Schmidt int ret; 1880da61d31aSJosef Bacik struct btrfs_trans_handle *trans = NULL; 18817a3ae2f8SJan Schmidt struct ulist *refs = NULL; 18827a3ae2f8SJan Schmidt struct ulist *roots = NULL; 18834692cf58SJan Schmidt struct ulist_node *ref_node = NULL; 18844692cf58SJan Schmidt struct ulist_node *root_node = NULL; 18853284da7bSDavid Sterba struct seq_list tree_mod_seq_elem = SEQ_LIST_INIT(tree_mod_seq_elem); 1886cd1b413cSJan Schmidt struct ulist_iterator ref_uiter; 1887cd1b413cSJan Schmidt struct ulist_iterator root_uiter; 1888a542ad1bSJan Schmidt 1889ab8d0fc4SJeff Mahoney btrfs_debug(fs_info, "resolving all inodes for extent %llu", 18904692cf58SJan Schmidt extent_item_objectid); 18914692cf58SJan Schmidt 1892da61d31aSJosef Bacik if (!search_commit_root) { 18937a3ae2f8SJan Schmidt trans = btrfs_join_transaction(fs_info->extent_root); 18947a3ae2f8SJan Schmidt if (IS_ERR(trans)) 18957a3ae2f8SJan Schmidt return PTR_ERR(trans); 18968445f61cSJan Schmidt btrfs_get_tree_mod_seq(fs_info, &tree_mod_seq_elem); 18979e351cc8SJosef Bacik } else { 18989e351cc8SJosef Bacik down_read(&fs_info->commit_root_sem); 18997a3ae2f8SJan Schmidt } 19004692cf58SJan Schmidt 19014692cf58SJan Schmidt ret = btrfs_find_all_leafs(trans, fs_info, extent_item_objectid, 1902097b8a7cSJan Schmidt tree_mod_seq_elem.seq, &refs, 19038445f61cSJan Schmidt &extent_item_pos); 19044692cf58SJan Schmidt if (ret) 19054692cf58SJan Schmidt goto out; 19064692cf58SJan Schmidt 1907cd1b413cSJan Schmidt ULIST_ITER_INIT(&ref_uiter); 1908cd1b413cSJan Schmidt while (!ret && (ref_node = ulist_next(refs, &ref_uiter))) { 1909e0c476b1SJeff Mahoney ret = btrfs_find_all_roots_safe(trans, fs_info, ref_node->val, 19108445f61cSJan Schmidt tree_mod_seq_elem.seq, &roots); 19114692cf58SJan Schmidt if (ret) 1912a542ad1bSJan Schmidt break; 1913cd1b413cSJan Schmidt ULIST_ITER_INIT(&root_uiter); 1914cd1b413cSJan Schmidt while (!ret && (root_node = ulist_next(roots, &root_uiter))) { 1915ab8d0fc4SJeff Mahoney btrfs_debug(fs_info, 1916ab8d0fc4SJeff Mahoney "root %llu references leaf %llu, data list %#llx", 1917ab8d0fc4SJeff Mahoney root_node->val, ref_node->val, 1918c1c9ff7cSGeert Uytterhoeven ref_node->aux); 1919ab8d0fc4SJeff Mahoney ret = iterate_leaf_refs(fs_info, 1920ab8d0fc4SJeff Mahoney (struct extent_inode_elem *) 1921995e01b7SJan Schmidt (uintptr_t)ref_node->aux, 1922995e01b7SJan Schmidt root_node->val, 1923995e01b7SJan Schmidt extent_item_objectid, 1924a542ad1bSJan Schmidt iterate, ctx); 19254692cf58SJan Schmidt } 1926976b1908SJan Schmidt ulist_free(roots); 1927a542ad1bSJan Schmidt } 1928a542ad1bSJan Schmidt 1929976b1908SJan Schmidt free_leaf_list(refs); 19304692cf58SJan Schmidt out: 19317a3ae2f8SJan Schmidt if (!search_commit_root) { 19328445f61cSJan Schmidt btrfs_put_tree_mod_seq(fs_info, &tree_mod_seq_elem); 19333a45bb20SJeff Mahoney btrfs_end_transaction(trans); 19349e351cc8SJosef Bacik } else { 19359e351cc8SJosef Bacik up_read(&fs_info->commit_root_sem); 19367a3ae2f8SJan Schmidt } 19377a3ae2f8SJan Schmidt 1938a542ad1bSJan Schmidt return ret; 1939a542ad1bSJan Schmidt } 1940a542ad1bSJan Schmidt 1941a542ad1bSJan Schmidt int iterate_inodes_from_logical(u64 logical, struct btrfs_fs_info *fs_info, 1942a542ad1bSJan Schmidt struct btrfs_path *path, 1943a542ad1bSJan Schmidt iterate_extent_inodes_t *iterate, void *ctx) 1944a542ad1bSJan Schmidt { 1945a542ad1bSJan Schmidt int ret; 19464692cf58SJan Schmidt u64 extent_item_pos; 194769917e43SLiu Bo u64 flags = 0; 1948a542ad1bSJan Schmidt struct btrfs_key found_key; 19497a3ae2f8SJan Schmidt int search_commit_root = path->search_commit_root; 1950a542ad1bSJan Schmidt 195169917e43SLiu Bo ret = extent_from_logical(fs_info, logical, path, &found_key, &flags); 19524692cf58SJan Schmidt btrfs_release_path(path); 1953a542ad1bSJan Schmidt if (ret < 0) 1954a542ad1bSJan Schmidt return ret; 195569917e43SLiu Bo if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) 19563627bf45SStefan Behrens return -EINVAL; 1957a542ad1bSJan Schmidt 19584692cf58SJan Schmidt extent_item_pos = logical - found_key.objectid; 19597a3ae2f8SJan Schmidt ret = iterate_extent_inodes(fs_info, found_key.objectid, 19607a3ae2f8SJan Schmidt extent_item_pos, search_commit_root, 19617a3ae2f8SJan Schmidt iterate, ctx); 1962a542ad1bSJan Schmidt 1963a542ad1bSJan Schmidt return ret; 1964a542ad1bSJan Schmidt } 1965a542ad1bSJan Schmidt 1966d24bec3aSMark Fasheh typedef int (iterate_irefs_t)(u64 parent, u32 name_len, unsigned long name_off, 1967d24bec3aSMark Fasheh struct extent_buffer *eb, void *ctx); 1968d24bec3aSMark Fasheh 1969d24bec3aSMark Fasheh static int iterate_inode_refs(u64 inum, struct btrfs_root *fs_root, 1970a542ad1bSJan Schmidt struct btrfs_path *path, 1971a542ad1bSJan Schmidt iterate_irefs_t *iterate, void *ctx) 1972a542ad1bSJan Schmidt { 1973aefc1eb1SJan Schmidt int ret = 0; 1974a542ad1bSJan Schmidt int slot; 1975a542ad1bSJan Schmidt u32 cur; 1976a542ad1bSJan Schmidt u32 len; 1977a542ad1bSJan Schmidt u32 name_len; 1978a542ad1bSJan Schmidt u64 parent = 0; 1979a542ad1bSJan Schmidt int found = 0; 1980a542ad1bSJan Schmidt struct extent_buffer *eb; 1981a542ad1bSJan Schmidt struct btrfs_item *item; 1982a542ad1bSJan Schmidt struct btrfs_inode_ref *iref; 1983a542ad1bSJan Schmidt struct btrfs_key found_key; 1984a542ad1bSJan Schmidt 1985aefc1eb1SJan Schmidt while (!ret) { 1986c234a24dSDavid Sterba ret = btrfs_find_item(fs_root, path, inum, 1987c234a24dSDavid Sterba parent ? parent + 1 : 0, BTRFS_INODE_REF_KEY, 1988a542ad1bSJan Schmidt &found_key); 1989c234a24dSDavid Sterba 1990a542ad1bSJan Schmidt if (ret < 0) 1991a542ad1bSJan Schmidt break; 1992a542ad1bSJan Schmidt if (ret) { 1993a542ad1bSJan Schmidt ret = found ? 0 : -ENOENT; 1994a542ad1bSJan Schmidt break; 1995a542ad1bSJan Schmidt } 1996a542ad1bSJan Schmidt ++found; 1997a542ad1bSJan Schmidt 1998a542ad1bSJan Schmidt parent = found_key.offset; 1999a542ad1bSJan Schmidt slot = path->slots[0]; 20003fe81ce2SFilipe David Borba Manana eb = btrfs_clone_extent_buffer(path->nodes[0]); 20013fe81ce2SFilipe David Borba Manana if (!eb) { 20023fe81ce2SFilipe David Borba Manana ret = -ENOMEM; 20033fe81ce2SFilipe David Borba Manana break; 20043fe81ce2SFilipe David Borba Manana } 20053fe81ce2SFilipe David Borba Manana extent_buffer_get(eb); 2006b916a59aSJan Schmidt btrfs_tree_read_lock(eb); 2007b916a59aSJan Schmidt btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK); 2008a542ad1bSJan Schmidt btrfs_release_path(path); 2009a542ad1bSJan Schmidt 2010dd3cc16bSRoss Kirk item = btrfs_item_nr(slot); 2011a542ad1bSJan Schmidt iref = btrfs_item_ptr(eb, slot, struct btrfs_inode_ref); 2012a542ad1bSJan Schmidt 2013a542ad1bSJan Schmidt for (cur = 0; cur < btrfs_item_size(eb, item); cur += len) { 2014a542ad1bSJan Schmidt name_len = btrfs_inode_ref_name_len(eb, iref); 2015a542ad1bSJan Schmidt /* path must be released before calling iterate()! */ 2016ab8d0fc4SJeff Mahoney btrfs_debug(fs_root->fs_info, 2017ab8d0fc4SJeff Mahoney "following ref at offset %u for inode %llu in tree %llu", 2018ab8d0fc4SJeff Mahoney cur, found_key.objectid, fs_root->objectid); 2019d24bec3aSMark Fasheh ret = iterate(parent, name_len, 2020d24bec3aSMark Fasheh (unsigned long)(iref + 1), eb, ctx); 2021aefc1eb1SJan Schmidt if (ret) 2022a542ad1bSJan Schmidt break; 2023a542ad1bSJan Schmidt len = sizeof(*iref) + name_len; 2024a542ad1bSJan Schmidt iref = (struct btrfs_inode_ref *)((char *)iref + len); 2025a542ad1bSJan Schmidt } 2026b916a59aSJan Schmidt btrfs_tree_read_unlock_blocking(eb); 2027a542ad1bSJan Schmidt free_extent_buffer(eb); 2028a542ad1bSJan Schmidt } 2029a542ad1bSJan Schmidt 2030a542ad1bSJan Schmidt btrfs_release_path(path); 2031a542ad1bSJan Schmidt 2032a542ad1bSJan Schmidt return ret; 2033a542ad1bSJan Schmidt } 2034a542ad1bSJan Schmidt 2035d24bec3aSMark Fasheh static int iterate_inode_extrefs(u64 inum, struct btrfs_root *fs_root, 2036d24bec3aSMark Fasheh struct btrfs_path *path, 2037d24bec3aSMark Fasheh iterate_irefs_t *iterate, void *ctx) 2038d24bec3aSMark Fasheh { 2039d24bec3aSMark Fasheh int ret; 2040d24bec3aSMark Fasheh int slot; 2041d24bec3aSMark Fasheh u64 offset = 0; 2042d24bec3aSMark Fasheh u64 parent; 2043d24bec3aSMark Fasheh int found = 0; 2044d24bec3aSMark Fasheh struct extent_buffer *eb; 2045d24bec3aSMark Fasheh struct btrfs_inode_extref *extref; 2046d24bec3aSMark Fasheh u32 item_size; 2047d24bec3aSMark Fasheh u32 cur_offset; 2048d24bec3aSMark Fasheh unsigned long ptr; 2049d24bec3aSMark Fasheh 2050d24bec3aSMark Fasheh while (1) { 2051d24bec3aSMark Fasheh ret = btrfs_find_one_extref(fs_root, inum, offset, path, &extref, 2052d24bec3aSMark Fasheh &offset); 2053d24bec3aSMark Fasheh if (ret < 0) 2054d24bec3aSMark Fasheh break; 2055d24bec3aSMark Fasheh if (ret) { 2056d24bec3aSMark Fasheh ret = found ? 0 : -ENOENT; 2057d24bec3aSMark Fasheh break; 2058d24bec3aSMark Fasheh } 2059d24bec3aSMark Fasheh ++found; 2060d24bec3aSMark Fasheh 2061d24bec3aSMark Fasheh slot = path->slots[0]; 20623fe81ce2SFilipe David Borba Manana eb = btrfs_clone_extent_buffer(path->nodes[0]); 20633fe81ce2SFilipe David Borba Manana if (!eb) { 20643fe81ce2SFilipe David Borba Manana ret = -ENOMEM; 20653fe81ce2SFilipe David Borba Manana break; 20663fe81ce2SFilipe David Borba Manana } 20673fe81ce2SFilipe David Borba Manana extent_buffer_get(eb); 2068d24bec3aSMark Fasheh 2069d24bec3aSMark Fasheh btrfs_tree_read_lock(eb); 2070d24bec3aSMark Fasheh btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK); 2071d24bec3aSMark Fasheh btrfs_release_path(path); 2072d24bec3aSMark Fasheh 20732849a854SChris Mason item_size = btrfs_item_size_nr(eb, slot); 20742849a854SChris Mason ptr = btrfs_item_ptr_offset(eb, slot); 2075d24bec3aSMark Fasheh cur_offset = 0; 2076d24bec3aSMark Fasheh 2077d24bec3aSMark Fasheh while (cur_offset < item_size) { 2078d24bec3aSMark Fasheh u32 name_len; 2079d24bec3aSMark Fasheh 2080d24bec3aSMark Fasheh extref = (struct btrfs_inode_extref *)(ptr + cur_offset); 2081d24bec3aSMark Fasheh parent = btrfs_inode_extref_parent(eb, extref); 2082d24bec3aSMark Fasheh name_len = btrfs_inode_extref_name_len(eb, extref); 2083d24bec3aSMark Fasheh ret = iterate(parent, name_len, 2084d24bec3aSMark Fasheh (unsigned long)&extref->name, eb, ctx); 2085d24bec3aSMark Fasheh if (ret) 2086d24bec3aSMark Fasheh break; 2087d24bec3aSMark Fasheh 20882849a854SChris Mason cur_offset += btrfs_inode_extref_name_len(eb, extref); 2089d24bec3aSMark Fasheh cur_offset += sizeof(*extref); 2090d24bec3aSMark Fasheh } 2091d24bec3aSMark Fasheh btrfs_tree_read_unlock_blocking(eb); 2092d24bec3aSMark Fasheh free_extent_buffer(eb); 2093d24bec3aSMark Fasheh 2094d24bec3aSMark Fasheh offset++; 2095d24bec3aSMark Fasheh } 2096d24bec3aSMark Fasheh 2097d24bec3aSMark Fasheh btrfs_release_path(path); 2098d24bec3aSMark Fasheh 2099d24bec3aSMark Fasheh return ret; 2100d24bec3aSMark Fasheh } 2101d24bec3aSMark Fasheh 2102d24bec3aSMark Fasheh static int iterate_irefs(u64 inum, struct btrfs_root *fs_root, 2103d24bec3aSMark Fasheh struct btrfs_path *path, iterate_irefs_t *iterate, 2104d24bec3aSMark Fasheh void *ctx) 2105d24bec3aSMark Fasheh { 2106d24bec3aSMark Fasheh int ret; 2107d24bec3aSMark Fasheh int found_refs = 0; 2108d24bec3aSMark Fasheh 2109d24bec3aSMark Fasheh ret = iterate_inode_refs(inum, fs_root, path, iterate, ctx); 2110d24bec3aSMark Fasheh if (!ret) 2111d24bec3aSMark Fasheh ++found_refs; 2112d24bec3aSMark Fasheh else if (ret != -ENOENT) 2113d24bec3aSMark Fasheh return ret; 2114d24bec3aSMark Fasheh 2115d24bec3aSMark Fasheh ret = iterate_inode_extrefs(inum, fs_root, path, iterate, ctx); 2116d24bec3aSMark Fasheh if (ret == -ENOENT && found_refs) 2117d24bec3aSMark Fasheh return 0; 2118d24bec3aSMark Fasheh 2119d24bec3aSMark Fasheh return ret; 2120d24bec3aSMark Fasheh } 2121d24bec3aSMark Fasheh 2122a542ad1bSJan Schmidt /* 2123a542ad1bSJan Schmidt * returns 0 if the path could be dumped (probably truncated) 2124a542ad1bSJan Schmidt * returns <0 in case of an error 2125a542ad1bSJan Schmidt */ 2126d24bec3aSMark Fasheh static int inode_to_path(u64 inum, u32 name_len, unsigned long name_off, 2127a542ad1bSJan Schmidt struct extent_buffer *eb, void *ctx) 2128a542ad1bSJan Schmidt { 2129a542ad1bSJan Schmidt struct inode_fs_paths *ipath = ctx; 2130a542ad1bSJan Schmidt char *fspath; 2131a542ad1bSJan Schmidt char *fspath_min; 2132a542ad1bSJan Schmidt int i = ipath->fspath->elem_cnt; 2133a542ad1bSJan Schmidt const int s_ptr = sizeof(char *); 2134a542ad1bSJan Schmidt u32 bytes_left; 2135a542ad1bSJan Schmidt 2136a542ad1bSJan Schmidt bytes_left = ipath->fspath->bytes_left > s_ptr ? 2137a542ad1bSJan Schmidt ipath->fspath->bytes_left - s_ptr : 0; 2138a542ad1bSJan Schmidt 2139740c3d22SChris Mason fspath_min = (char *)ipath->fspath->val + (i + 1) * s_ptr; 214096b5bd77SJan Schmidt fspath = btrfs_ref_to_path(ipath->fs_root, ipath->btrfs_path, name_len, 214196b5bd77SJan Schmidt name_off, eb, inum, fspath_min, bytes_left); 2142a542ad1bSJan Schmidt if (IS_ERR(fspath)) 2143a542ad1bSJan Schmidt return PTR_ERR(fspath); 2144a542ad1bSJan Schmidt 2145a542ad1bSJan Schmidt if (fspath > fspath_min) { 2146745c4d8eSJeff Mahoney ipath->fspath->val[i] = (u64)(unsigned long)fspath; 2147a542ad1bSJan Schmidt ++ipath->fspath->elem_cnt; 2148a542ad1bSJan Schmidt ipath->fspath->bytes_left = fspath - fspath_min; 2149a542ad1bSJan Schmidt } else { 2150a542ad1bSJan Schmidt ++ipath->fspath->elem_missed; 2151a542ad1bSJan Schmidt ipath->fspath->bytes_missing += fspath_min - fspath; 2152a542ad1bSJan Schmidt ipath->fspath->bytes_left = 0; 2153a542ad1bSJan Schmidt } 2154a542ad1bSJan Schmidt 2155a542ad1bSJan Schmidt return 0; 2156a542ad1bSJan Schmidt } 2157a542ad1bSJan Schmidt 2158a542ad1bSJan Schmidt /* 2159a542ad1bSJan Schmidt * this dumps all file system paths to the inode into the ipath struct, provided 2160a542ad1bSJan Schmidt * is has been created large enough. each path is zero-terminated and accessed 2161740c3d22SChris Mason * from ipath->fspath->val[i]. 2162a542ad1bSJan Schmidt * when it returns, there are ipath->fspath->elem_cnt number of paths available 2163740c3d22SChris Mason * in ipath->fspath->val[]. when the allocated space wasn't sufficient, the 216401327610SNicholas D Steeves * number of missed paths is recorded in ipath->fspath->elem_missed, otherwise, 2165a542ad1bSJan Schmidt * it's zero. ipath->fspath->bytes_missing holds the number of bytes that would 2166a542ad1bSJan Schmidt * have been needed to return all paths. 2167a542ad1bSJan Schmidt */ 2168a542ad1bSJan Schmidt int paths_from_inode(u64 inum, struct inode_fs_paths *ipath) 2169a542ad1bSJan Schmidt { 2170a542ad1bSJan Schmidt return iterate_irefs(inum, ipath->fs_root, ipath->btrfs_path, 2171a542ad1bSJan Schmidt inode_to_path, ipath); 2172a542ad1bSJan Schmidt } 2173a542ad1bSJan Schmidt 2174a542ad1bSJan Schmidt struct btrfs_data_container *init_data_container(u32 total_bytes) 2175a542ad1bSJan Schmidt { 2176a542ad1bSJan Schmidt struct btrfs_data_container *data; 2177a542ad1bSJan Schmidt size_t alloc_bytes; 2178a542ad1bSJan Schmidt 2179a542ad1bSJan Schmidt alloc_bytes = max_t(size_t, total_bytes, sizeof(*data)); 2180f54de068SDavid Sterba data = kvmalloc(alloc_bytes, GFP_KERNEL); 2181a542ad1bSJan Schmidt if (!data) 2182a542ad1bSJan Schmidt return ERR_PTR(-ENOMEM); 2183a542ad1bSJan Schmidt 2184a542ad1bSJan Schmidt if (total_bytes >= sizeof(*data)) { 2185a542ad1bSJan Schmidt data->bytes_left = total_bytes - sizeof(*data); 2186a542ad1bSJan Schmidt data->bytes_missing = 0; 2187a542ad1bSJan Schmidt } else { 2188a542ad1bSJan Schmidt data->bytes_missing = sizeof(*data) - total_bytes; 2189a542ad1bSJan Schmidt data->bytes_left = 0; 2190a542ad1bSJan Schmidt } 2191a542ad1bSJan Schmidt 2192a542ad1bSJan Schmidt data->elem_cnt = 0; 2193a542ad1bSJan Schmidt data->elem_missed = 0; 2194a542ad1bSJan Schmidt 2195a542ad1bSJan Schmidt return data; 2196a542ad1bSJan Schmidt } 2197a542ad1bSJan Schmidt 2198a542ad1bSJan Schmidt /* 2199a542ad1bSJan Schmidt * allocates space to return multiple file system paths for an inode. 2200a542ad1bSJan Schmidt * total_bytes to allocate are passed, note that space usable for actual path 2201a542ad1bSJan Schmidt * information will be total_bytes - sizeof(struct inode_fs_paths). 2202a542ad1bSJan Schmidt * the returned pointer must be freed with free_ipath() in the end. 2203a542ad1bSJan Schmidt */ 2204a542ad1bSJan Schmidt struct inode_fs_paths *init_ipath(s32 total_bytes, struct btrfs_root *fs_root, 2205a542ad1bSJan Schmidt struct btrfs_path *path) 2206a542ad1bSJan Schmidt { 2207a542ad1bSJan Schmidt struct inode_fs_paths *ifp; 2208a542ad1bSJan Schmidt struct btrfs_data_container *fspath; 2209a542ad1bSJan Schmidt 2210a542ad1bSJan Schmidt fspath = init_data_container(total_bytes); 2211a542ad1bSJan Schmidt if (IS_ERR(fspath)) 2212a542ad1bSJan Schmidt return (void *)fspath; 2213a542ad1bSJan Schmidt 2214f54de068SDavid Sterba ifp = kmalloc(sizeof(*ifp), GFP_KERNEL); 2215a542ad1bSJan Schmidt if (!ifp) { 2216f54de068SDavid Sterba kvfree(fspath); 2217a542ad1bSJan Schmidt return ERR_PTR(-ENOMEM); 2218a542ad1bSJan Schmidt } 2219a542ad1bSJan Schmidt 2220a542ad1bSJan Schmidt ifp->btrfs_path = path; 2221a542ad1bSJan Schmidt ifp->fspath = fspath; 2222a542ad1bSJan Schmidt ifp->fs_root = fs_root; 2223a542ad1bSJan Schmidt 2224a542ad1bSJan Schmidt return ifp; 2225a542ad1bSJan Schmidt } 2226a542ad1bSJan Schmidt 2227a542ad1bSJan Schmidt void free_ipath(struct inode_fs_paths *ipath) 2228a542ad1bSJan Schmidt { 22294735fb28SJesper Juhl if (!ipath) 22304735fb28SJesper Juhl return; 2231f54de068SDavid Sterba kvfree(ipath->fspath); 2232a542ad1bSJan Schmidt kfree(ipath); 2233a542ad1bSJan Schmidt } 2234