1c1d7c514SDavid Sterba // SPDX-License-Identifier: GPL-2.0 2a542ad1bSJan Schmidt /* 3a542ad1bSJan Schmidt * Copyright (C) 2011 STRATO. All rights reserved. 4a542ad1bSJan Schmidt */ 5a542ad1bSJan Schmidt 6f54de068SDavid Sterba #include <linux/mm.h> 7afce772eSLu Fengqi #include <linux/rbtree.h> 800142756SJeff Mahoney #include <trace/events/btrfs.h> 9a542ad1bSJan Schmidt #include "ctree.h" 10a542ad1bSJan Schmidt #include "disk-io.h" 11a542ad1bSJan Schmidt #include "backref.h" 128da6d581SJan Schmidt #include "ulist.h" 138da6d581SJan Schmidt #include "transaction.h" 148da6d581SJan Schmidt #include "delayed-ref.h" 15b916a59aSJan Schmidt #include "locking.h" 16a542ad1bSJan Schmidt 17dc046b10SJosef Bacik /* Just an arbitrary number so we can be sure this happened */ 18dc046b10SJosef Bacik #define BACKREF_FOUND_SHARED 6 19dc046b10SJosef Bacik 20976b1908SJan Schmidt struct extent_inode_elem { 21976b1908SJan Schmidt u64 inum; 22976b1908SJan Schmidt u64 offset; 23976b1908SJan Schmidt struct extent_inode_elem *next; 24976b1908SJan Schmidt }; 25976b1908SJan Schmidt 2673980becSJeff Mahoney static int check_extent_in_eb(const struct btrfs_key *key, 2773980becSJeff Mahoney const struct extent_buffer *eb, 2873980becSJeff Mahoney const struct btrfs_file_extent_item *fi, 29976b1908SJan Schmidt u64 extent_item_pos, 30c995ab3cSZygo Blaxell struct extent_inode_elem **eie, 31c995ab3cSZygo Blaxell bool ignore_offset) 32976b1908SJan Schmidt { 338ca15e05SJosef Bacik u64 offset = 0; 348ca15e05SJosef Bacik struct extent_inode_elem *e; 358ca15e05SJosef Bacik 36c995ab3cSZygo Blaxell if (!ignore_offset && 37c995ab3cSZygo Blaxell !btrfs_file_extent_compression(eb, fi) && 388ca15e05SJosef Bacik !btrfs_file_extent_encryption(eb, fi) && 398ca15e05SJosef Bacik !btrfs_file_extent_other_encoding(eb, fi)) { 40976b1908SJan Schmidt u64 data_offset; 41976b1908SJan Schmidt u64 data_len; 42976b1908SJan Schmidt 43976b1908SJan Schmidt data_offset = btrfs_file_extent_offset(eb, fi); 44976b1908SJan Schmidt data_len = btrfs_file_extent_num_bytes(eb, fi); 45976b1908SJan Schmidt 46976b1908SJan Schmidt if (extent_item_pos < data_offset || 47976b1908SJan Schmidt extent_item_pos >= data_offset + data_len) 48976b1908SJan Schmidt return 1; 498ca15e05SJosef Bacik offset = extent_item_pos - data_offset; 508ca15e05SJosef Bacik } 51976b1908SJan Schmidt 52976b1908SJan Schmidt e = kmalloc(sizeof(*e), GFP_NOFS); 53976b1908SJan Schmidt if (!e) 54976b1908SJan Schmidt return -ENOMEM; 55976b1908SJan Schmidt 56976b1908SJan Schmidt e->next = *eie; 57976b1908SJan Schmidt e->inum = key->objectid; 588ca15e05SJosef Bacik e->offset = key->offset + offset; 59976b1908SJan Schmidt *eie = e; 60976b1908SJan Schmidt 61976b1908SJan Schmidt return 0; 62976b1908SJan Schmidt } 63976b1908SJan Schmidt 64f05c4746SWang Shilong static void free_inode_elem_list(struct extent_inode_elem *eie) 65f05c4746SWang Shilong { 66f05c4746SWang Shilong struct extent_inode_elem *eie_next; 67f05c4746SWang Shilong 68f05c4746SWang Shilong for (; eie; eie = eie_next) { 69f05c4746SWang Shilong eie_next = eie->next; 70f05c4746SWang Shilong kfree(eie); 71f05c4746SWang Shilong } 72f05c4746SWang Shilong } 73f05c4746SWang Shilong 7473980becSJeff Mahoney static int find_extent_in_eb(const struct extent_buffer *eb, 7573980becSJeff Mahoney u64 wanted_disk_byte, u64 extent_item_pos, 76c995ab3cSZygo Blaxell struct extent_inode_elem **eie, 77c995ab3cSZygo Blaxell bool ignore_offset) 78976b1908SJan Schmidt { 79976b1908SJan Schmidt u64 disk_byte; 80976b1908SJan Schmidt struct btrfs_key key; 81976b1908SJan Schmidt struct btrfs_file_extent_item *fi; 82976b1908SJan Schmidt int slot; 83976b1908SJan Schmidt int nritems; 84976b1908SJan Schmidt int extent_type; 85976b1908SJan Schmidt int ret; 86976b1908SJan Schmidt 87976b1908SJan Schmidt /* 88976b1908SJan Schmidt * from the shared data ref, we only have the leaf but we need 89976b1908SJan Schmidt * the key. thus, we must look into all items and see that we 90976b1908SJan Schmidt * find one (some) with a reference to our extent item. 91976b1908SJan Schmidt */ 92976b1908SJan Schmidt nritems = btrfs_header_nritems(eb); 93976b1908SJan Schmidt for (slot = 0; slot < nritems; ++slot) { 94976b1908SJan Schmidt btrfs_item_key_to_cpu(eb, &key, slot); 95976b1908SJan Schmidt if (key.type != BTRFS_EXTENT_DATA_KEY) 96976b1908SJan Schmidt continue; 97976b1908SJan Schmidt fi = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item); 98976b1908SJan Schmidt extent_type = btrfs_file_extent_type(eb, fi); 99976b1908SJan Schmidt if (extent_type == BTRFS_FILE_EXTENT_INLINE) 100976b1908SJan Schmidt continue; 101976b1908SJan Schmidt /* don't skip BTRFS_FILE_EXTENT_PREALLOC, we can handle that */ 102976b1908SJan Schmidt disk_byte = btrfs_file_extent_disk_bytenr(eb, fi); 103976b1908SJan Schmidt if (disk_byte != wanted_disk_byte) 104976b1908SJan Schmidt continue; 105976b1908SJan Schmidt 106c995ab3cSZygo Blaxell ret = check_extent_in_eb(&key, eb, fi, extent_item_pos, eie, ignore_offset); 107976b1908SJan Schmidt if (ret < 0) 108976b1908SJan Schmidt return ret; 109976b1908SJan Schmidt } 110976b1908SJan Schmidt 111976b1908SJan Schmidt return 0; 112976b1908SJan Schmidt } 113976b1908SJan Schmidt 11486d5f994SEdmund Nadolski struct preftree { 115ecf160b4SLiu Bo struct rb_root_cached root; 1166c336b21SJeff Mahoney unsigned int count; 11786d5f994SEdmund Nadolski }; 11886d5f994SEdmund Nadolski 119ecf160b4SLiu Bo #define PREFTREE_INIT { .root = RB_ROOT_CACHED, .count = 0 } 12086d5f994SEdmund Nadolski 12186d5f994SEdmund Nadolski struct preftrees { 12286d5f994SEdmund Nadolski struct preftree direct; /* BTRFS_SHARED_[DATA|BLOCK]_REF_KEY */ 12386d5f994SEdmund Nadolski struct preftree indirect; /* BTRFS_[TREE_BLOCK|EXTENT_DATA]_REF_KEY */ 12486d5f994SEdmund Nadolski struct preftree indirect_missing_keys; 12586d5f994SEdmund Nadolski }; 12686d5f994SEdmund Nadolski 1273ec4d323SEdmund Nadolski /* 1283ec4d323SEdmund Nadolski * Checks for a shared extent during backref search. 1293ec4d323SEdmund Nadolski * 1303ec4d323SEdmund Nadolski * The share_count tracks prelim_refs (direct and indirect) having a 1313ec4d323SEdmund Nadolski * ref->count >0: 1323ec4d323SEdmund Nadolski * - incremented when a ref->count transitions to >0 1333ec4d323SEdmund Nadolski * - decremented when a ref->count transitions to <1 1343ec4d323SEdmund Nadolski */ 1353ec4d323SEdmund Nadolski struct share_check { 1363ec4d323SEdmund Nadolski u64 root_objectid; 1373ec4d323SEdmund Nadolski u64 inum; 1383ec4d323SEdmund Nadolski int share_count; 1393ec4d323SEdmund Nadolski }; 1403ec4d323SEdmund Nadolski 1413ec4d323SEdmund Nadolski static inline int extent_is_shared(struct share_check *sc) 1423ec4d323SEdmund Nadolski { 1433ec4d323SEdmund Nadolski return (sc && sc->share_count > 1) ? BACKREF_FOUND_SHARED : 0; 1443ec4d323SEdmund Nadolski } 1453ec4d323SEdmund Nadolski 146b9e9a6cbSWang Shilong static struct kmem_cache *btrfs_prelim_ref_cache; 147b9e9a6cbSWang Shilong 148b9e9a6cbSWang Shilong int __init btrfs_prelim_ref_init(void) 149b9e9a6cbSWang Shilong { 150b9e9a6cbSWang Shilong btrfs_prelim_ref_cache = kmem_cache_create("btrfs_prelim_ref", 151e0c476b1SJeff Mahoney sizeof(struct prelim_ref), 152b9e9a6cbSWang Shilong 0, 153fba4b697SNikolay Borisov SLAB_MEM_SPREAD, 154b9e9a6cbSWang Shilong NULL); 155b9e9a6cbSWang Shilong if (!btrfs_prelim_ref_cache) 156b9e9a6cbSWang Shilong return -ENOMEM; 157b9e9a6cbSWang Shilong return 0; 158b9e9a6cbSWang Shilong } 159b9e9a6cbSWang Shilong 160e67c718bSDavid Sterba void __cold btrfs_prelim_ref_exit(void) 161b9e9a6cbSWang Shilong { 162b9e9a6cbSWang Shilong kmem_cache_destroy(btrfs_prelim_ref_cache); 163b9e9a6cbSWang Shilong } 164b9e9a6cbSWang Shilong 16586d5f994SEdmund Nadolski static void free_pref(struct prelim_ref *ref) 16686d5f994SEdmund Nadolski { 16786d5f994SEdmund Nadolski kmem_cache_free(btrfs_prelim_ref_cache, ref); 16886d5f994SEdmund Nadolski } 16986d5f994SEdmund Nadolski 17086d5f994SEdmund Nadolski /* 17186d5f994SEdmund Nadolski * Return 0 when both refs are for the same block (and can be merged). 17286d5f994SEdmund Nadolski * A -1 return indicates ref1 is a 'lower' block than ref2, while 1 17386d5f994SEdmund Nadolski * indicates a 'higher' block. 17486d5f994SEdmund Nadolski */ 17586d5f994SEdmund Nadolski static int prelim_ref_compare(struct prelim_ref *ref1, 17686d5f994SEdmund Nadolski struct prelim_ref *ref2) 17786d5f994SEdmund Nadolski { 17886d5f994SEdmund Nadolski if (ref1->level < ref2->level) 17986d5f994SEdmund Nadolski return -1; 18086d5f994SEdmund Nadolski if (ref1->level > ref2->level) 18186d5f994SEdmund Nadolski return 1; 18286d5f994SEdmund Nadolski if (ref1->root_id < ref2->root_id) 18386d5f994SEdmund Nadolski return -1; 18486d5f994SEdmund Nadolski if (ref1->root_id > ref2->root_id) 18586d5f994SEdmund Nadolski return 1; 18686d5f994SEdmund Nadolski if (ref1->key_for_search.type < ref2->key_for_search.type) 18786d5f994SEdmund Nadolski return -1; 18886d5f994SEdmund Nadolski if (ref1->key_for_search.type > ref2->key_for_search.type) 18986d5f994SEdmund Nadolski return 1; 19086d5f994SEdmund Nadolski if (ref1->key_for_search.objectid < ref2->key_for_search.objectid) 19186d5f994SEdmund Nadolski return -1; 19286d5f994SEdmund Nadolski if (ref1->key_for_search.objectid > ref2->key_for_search.objectid) 19386d5f994SEdmund Nadolski return 1; 19486d5f994SEdmund Nadolski if (ref1->key_for_search.offset < ref2->key_for_search.offset) 19586d5f994SEdmund Nadolski return -1; 19686d5f994SEdmund Nadolski if (ref1->key_for_search.offset > ref2->key_for_search.offset) 19786d5f994SEdmund Nadolski return 1; 19886d5f994SEdmund Nadolski if (ref1->parent < ref2->parent) 19986d5f994SEdmund Nadolski return -1; 20086d5f994SEdmund Nadolski if (ref1->parent > ref2->parent) 20186d5f994SEdmund Nadolski return 1; 20286d5f994SEdmund Nadolski 20386d5f994SEdmund Nadolski return 0; 20486d5f994SEdmund Nadolski } 20586d5f994SEdmund Nadolski 206ccc8dc75SColin Ian King static void update_share_count(struct share_check *sc, int oldcount, 207ccc8dc75SColin Ian King int newcount) 2083ec4d323SEdmund Nadolski { 2093ec4d323SEdmund Nadolski if ((!sc) || (oldcount == 0 && newcount < 1)) 2103ec4d323SEdmund Nadolski return; 2113ec4d323SEdmund Nadolski 2123ec4d323SEdmund Nadolski if (oldcount > 0 && newcount < 1) 2133ec4d323SEdmund Nadolski sc->share_count--; 2143ec4d323SEdmund Nadolski else if (oldcount < 1 && newcount > 0) 2153ec4d323SEdmund Nadolski sc->share_count++; 2163ec4d323SEdmund Nadolski } 2173ec4d323SEdmund Nadolski 21886d5f994SEdmund Nadolski /* 21986d5f994SEdmund Nadolski * Add @newref to the @root rbtree, merging identical refs. 22086d5f994SEdmund Nadolski * 2213ec4d323SEdmund Nadolski * Callers should assume that newref has been freed after calling. 22286d5f994SEdmund Nadolski */ 22300142756SJeff Mahoney static void prelim_ref_insert(const struct btrfs_fs_info *fs_info, 22400142756SJeff Mahoney struct preftree *preftree, 2253ec4d323SEdmund Nadolski struct prelim_ref *newref, 2263ec4d323SEdmund Nadolski struct share_check *sc) 22786d5f994SEdmund Nadolski { 228ecf160b4SLiu Bo struct rb_root_cached *root; 22986d5f994SEdmund Nadolski struct rb_node **p; 23086d5f994SEdmund Nadolski struct rb_node *parent = NULL; 23186d5f994SEdmund Nadolski struct prelim_ref *ref; 23286d5f994SEdmund Nadolski int result; 233ecf160b4SLiu Bo bool leftmost = true; 23486d5f994SEdmund Nadolski 23586d5f994SEdmund Nadolski root = &preftree->root; 236ecf160b4SLiu Bo p = &root->rb_root.rb_node; 23786d5f994SEdmund Nadolski 23886d5f994SEdmund Nadolski while (*p) { 23986d5f994SEdmund Nadolski parent = *p; 24086d5f994SEdmund Nadolski ref = rb_entry(parent, struct prelim_ref, rbnode); 24186d5f994SEdmund Nadolski result = prelim_ref_compare(ref, newref); 24286d5f994SEdmund Nadolski if (result < 0) { 24386d5f994SEdmund Nadolski p = &(*p)->rb_left; 24486d5f994SEdmund Nadolski } else if (result > 0) { 24586d5f994SEdmund Nadolski p = &(*p)->rb_right; 246ecf160b4SLiu Bo leftmost = false; 24786d5f994SEdmund Nadolski } else { 24886d5f994SEdmund Nadolski /* Identical refs, merge them and free @newref */ 24986d5f994SEdmund Nadolski struct extent_inode_elem *eie = ref->inode_list; 25086d5f994SEdmund Nadolski 25186d5f994SEdmund Nadolski while (eie && eie->next) 25286d5f994SEdmund Nadolski eie = eie->next; 25386d5f994SEdmund Nadolski 25486d5f994SEdmund Nadolski if (!eie) 25586d5f994SEdmund Nadolski ref->inode_list = newref->inode_list; 25686d5f994SEdmund Nadolski else 25786d5f994SEdmund Nadolski eie->next = newref->inode_list; 25800142756SJeff Mahoney trace_btrfs_prelim_ref_merge(fs_info, ref, newref, 25900142756SJeff Mahoney preftree->count); 2603ec4d323SEdmund Nadolski /* 2613ec4d323SEdmund Nadolski * A delayed ref can have newref->count < 0. 2623ec4d323SEdmund Nadolski * The ref->count is updated to follow any 2633ec4d323SEdmund Nadolski * BTRFS_[ADD|DROP]_DELAYED_REF actions. 2643ec4d323SEdmund Nadolski */ 2653ec4d323SEdmund Nadolski update_share_count(sc, ref->count, 2663ec4d323SEdmund Nadolski ref->count + newref->count); 26786d5f994SEdmund Nadolski ref->count += newref->count; 26886d5f994SEdmund Nadolski free_pref(newref); 26986d5f994SEdmund Nadolski return; 27086d5f994SEdmund Nadolski } 27186d5f994SEdmund Nadolski } 27286d5f994SEdmund Nadolski 2733ec4d323SEdmund Nadolski update_share_count(sc, 0, newref->count); 2746c336b21SJeff Mahoney preftree->count++; 27500142756SJeff Mahoney trace_btrfs_prelim_ref_insert(fs_info, newref, NULL, preftree->count); 27686d5f994SEdmund Nadolski rb_link_node(&newref->rbnode, parent, p); 277ecf160b4SLiu Bo rb_insert_color_cached(&newref->rbnode, root, leftmost); 27886d5f994SEdmund Nadolski } 27986d5f994SEdmund Nadolski 28086d5f994SEdmund Nadolski /* 28186d5f994SEdmund Nadolski * Release the entire tree. We don't care about internal consistency so 28286d5f994SEdmund Nadolski * just free everything and then reset the tree root. 28386d5f994SEdmund Nadolski */ 28486d5f994SEdmund Nadolski static void prelim_release(struct preftree *preftree) 28586d5f994SEdmund Nadolski { 28686d5f994SEdmund Nadolski struct prelim_ref *ref, *next_ref; 28786d5f994SEdmund Nadolski 288ecf160b4SLiu Bo rbtree_postorder_for_each_entry_safe(ref, next_ref, 289ecf160b4SLiu Bo &preftree->root.rb_root, rbnode) 29086d5f994SEdmund Nadolski free_pref(ref); 29186d5f994SEdmund Nadolski 292ecf160b4SLiu Bo preftree->root = RB_ROOT_CACHED; 2936c336b21SJeff Mahoney preftree->count = 0; 29486d5f994SEdmund Nadolski } 29586d5f994SEdmund Nadolski 296d5c88b73SJan Schmidt /* 297d5c88b73SJan Schmidt * the rules for all callers of this function are: 298d5c88b73SJan Schmidt * - obtaining the parent is the goal 299d5c88b73SJan Schmidt * - if you add a key, you must know that it is a correct key 300d5c88b73SJan Schmidt * - if you cannot add the parent or a correct key, then we will look into the 301d5c88b73SJan Schmidt * block later to set a correct key 302d5c88b73SJan Schmidt * 303d5c88b73SJan Schmidt * delayed refs 304d5c88b73SJan Schmidt * ============ 305d5c88b73SJan Schmidt * backref type | shared | indirect | shared | indirect 306d5c88b73SJan Schmidt * information | tree | tree | data | data 307d5c88b73SJan Schmidt * --------------------+--------+----------+--------+---------- 308d5c88b73SJan Schmidt * parent logical | y | - | - | - 309d5c88b73SJan Schmidt * key to resolve | - | y | y | y 310d5c88b73SJan Schmidt * tree block logical | - | - | - | - 311d5c88b73SJan Schmidt * root for resolving | y | y | y | y 312d5c88b73SJan Schmidt * 313d5c88b73SJan Schmidt * - column 1: we've the parent -> done 314d5c88b73SJan Schmidt * - column 2, 3, 4: we use the key to find the parent 315d5c88b73SJan Schmidt * 316d5c88b73SJan Schmidt * on disk refs (inline or keyed) 317d5c88b73SJan Schmidt * ============================== 318d5c88b73SJan Schmidt * backref type | shared | indirect | shared | indirect 319d5c88b73SJan Schmidt * information | tree | tree | data | data 320d5c88b73SJan Schmidt * --------------------+--------+----------+--------+---------- 321d5c88b73SJan Schmidt * parent logical | y | - | y | - 322d5c88b73SJan Schmidt * key to resolve | - | - | - | y 323d5c88b73SJan Schmidt * tree block logical | y | y | y | y 324d5c88b73SJan Schmidt * root for resolving | - | y | y | y 325d5c88b73SJan Schmidt * 326d5c88b73SJan Schmidt * - column 1, 3: we've the parent -> done 327d5c88b73SJan Schmidt * - column 2: we take the first key from the block to find the parent 328e0c476b1SJeff Mahoney * (see add_missing_keys) 329d5c88b73SJan Schmidt * - column 4: we use the key to find the parent 330d5c88b73SJan Schmidt * 331d5c88b73SJan Schmidt * additional information that's available but not required to find the parent 332d5c88b73SJan Schmidt * block might help in merging entries to gain some speed. 333d5c88b73SJan Schmidt */ 33400142756SJeff Mahoney static int add_prelim_ref(const struct btrfs_fs_info *fs_info, 33500142756SJeff Mahoney struct preftree *preftree, u64 root_id, 336e0c476b1SJeff Mahoney const struct btrfs_key *key, int level, u64 parent, 3373ec4d323SEdmund Nadolski u64 wanted_disk_byte, int count, 3383ec4d323SEdmund Nadolski struct share_check *sc, gfp_t gfp_mask) 3398da6d581SJan Schmidt { 340e0c476b1SJeff Mahoney struct prelim_ref *ref; 3418da6d581SJan Schmidt 34248ec4736SLiu Bo if (root_id == BTRFS_DATA_RELOC_TREE_OBJECTID) 34348ec4736SLiu Bo return 0; 34448ec4736SLiu Bo 345b9e9a6cbSWang Shilong ref = kmem_cache_alloc(btrfs_prelim_ref_cache, gfp_mask); 3468da6d581SJan Schmidt if (!ref) 3478da6d581SJan Schmidt return -ENOMEM; 3488da6d581SJan Schmidt 3498da6d581SJan Schmidt ref->root_id = root_id; 3507ac8b88eSethanwu if (key) 351d5c88b73SJan Schmidt ref->key_for_search = *key; 3527ac8b88eSethanwu else 353d5c88b73SJan Schmidt memset(&ref->key_for_search, 0, sizeof(ref->key_for_search)); 3548da6d581SJan Schmidt 3553301958bSJan Schmidt ref->inode_list = NULL; 3568da6d581SJan Schmidt ref->level = level; 3578da6d581SJan Schmidt ref->count = count; 3588da6d581SJan Schmidt ref->parent = parent; 3598da6d581SJan Schmidt ref->wanted_disk_byte = wanted_disk_byte; 3603ec4d323SEdmund Nadolski prelim_ref_insert(fs_info, preftree, ref, sc); 3613ec4d323SEdmund Nadolski return extent_is_shared(sc); 3628da6d581SJan Schmidt } 3638da6d581SJan Schmidt 36486d5f994SEdmund Nadolski /* direct refs use root == 0, key == NULL */ 36500142756SJeff Mahoney static int add_direct_ref(const struct btrfs_fs_info *fs_info, 36600142756SJeff Mahoney struct preftrees *preftrees, int level, u64 parent, 3673ec4d323SEdmund Nadolski u64 wanted_disk_byte, int count, 3683ec4d323SEdmund Nadolski struct share_check *sc, gfp_t gfp_mask) 36986d5f994SEdmund Nadolski { 37000142756SJeff Mahoney return add_prelim_ref(fs_info, &preftrees->direct, 0, NULL, level, 3713ec4d323SEdmund Nadolski parent, wanted_disk_byte, count, sc, gfp_mask); 37286d5f994SEdmund Nadolski } 37386d5f994SEdmund Nadolski 37486d5f994SEdmund Nadolski /* indirect refs use parent == 0 */ 37500142756SJeff Mahoney static int add_indirect_ref(const struct btrfs_fs_info *fs_info, 37600142756SJeff Mahoney struct preftrees *preftrees, u64 root_id, 37786d5f994SEdmund Nadolski const struct btrfs_key *key, int level, 3783ec4d323SEdmund Nadolski u64 wanted_disk_byte, int count, 3793ec4d323SEdmund Nadolski struct share_check *sc, gfp_t gfp_mask) 38086d5f994SEdmund Nadolski { 38186d5f994SEdmund Nadolski struct preftree *tree = &preftrees->indirect; 38286d5f994SEdmund Nadolski 38386d5f994SEdmund Nadolski if (!key) 38486d5f994SEdmund Nadolski tree = &preftrees->indirect_missing_keys; 38500142756SJeff Mahoney return add_prelim_ref(fs_info, tree, root_id, key, level, 0, 3863ec4d323SEdmund Nadolski wanted_disk_byte, count, sc, gfp_mask); 38786d5f994SEdmund Nadolski } 38886d5f994SEdmund Nadolski 389*ed58f2e6Sethanwu static int is_shared_data_backref(struct preftrees *preftrees, u64 bytenr) 390*ed58f2e6Sethanwu { 391*ed58f2e6Sethanwu struct rb_node **p = &preftrees->direct.root.rb_root.rb_node; 392*ed58f2e6Sethanwu struct rb_node *parent = NULL; 393*ed58f2e6Sethanwu struct prelim_ref *ref = NULL; 394*ed58f2e6Sethanwu struct prelim_ref target = {0}; 395*ed58f2e6Sethanwu int result; 396*ed58f2e6Sethanwu 397*ed58f2e6Sethanwu target.parent = bytenr; 398*ed58f2e6Sethanwu 399*ed58f2e6Sethanwu while (*p) { 400*ed58f2e6Sethanwu parent = *p; 401*ed58f2e6Sethanwu ref = rb_entry(parent, struct prelim_ref, rbnode); 402*ed58f2e6Sethanwu result = prelim_ref_compare(ref, &target); 403*ed58f2e6Sethanwu 404*ed58f2e6Sethanwu if (result < 0) 405*ed58f2e6Sethanwu p = &(*p)->rb_left; 406*ed58f2e6Sethanwu else if (result > 0) 407*ed58f2e6Sethanwu p = &(*p)->rb_right; 408*ed58f2e6Sethanwu else 409*ed58f2e6Sethanwu return 1; 410*ed58f2e6Sethanwu } 411*ed58f2e6Sethanwu return 0; 412*ed58f2e6Sethanwu } 413*ed58f2e6Sethanwu 4148da6d581SJan Schmidt static int add_all_parents(struct btrfs_root *root, struct btrfs_path *path, 415*ed58f2e6Sethanwu struct ulist *parents, 416*ed58f2e6Sethanwu struct preftrees *preftrees, struct prelim_ref *ref, 41744853868SJosef Bacik int level, u64 time_seq, const u64 *extent_item_pos, 418c995ab3cSZygo Blaxell u64 total_refs, bool ignore_offset) 4198da6d581SJan Schmidt { 42069bca40dSAlexander Block int ret = 0; 42169bca40dSAlexander Block int slot; 42269bca40dSAlexander Block struct extent_buffer *eb; 42369bca40dSAlexander Block struct btrfs_key key; 4247ef81ac8SJosef Bacik struct btrfs_key *key_for_search = &ref->key_for_search; 4258da6d581SJan Schmidt struct btrfs_file_extent_item *fi; 426ed8c4913SJosef Bacik struct extent_inode_elem *eie = NULL, *old = NULL; 4278da6d581SJan Schmidt u64 disk_byte; 4287ef81ac8SJosef Bacik u64 wanted_disk_byte = ref->wanted_disk_byte; 4297ef81ac8SJosef Bacik u64 count = 0; 4307ac8b88eSethanwu u64 data_offset; 4318da6d581SJan Schmidt 43269bca40dSAlexander Block if (level != 0) { 43369bca40dSAlexander Block eb = path->nodes[level]; 43469bca40dSAlexander Block ret = ulist_add(parents, eb->start, 0, GFP_NOFS); 4353301958bSJan Schmidt if (ret < 0) 4363301958bSJan Schmidt return ret; 4378da6d581SJan Schmidt return 0; 43869bca40dSAlexander Block } 4398da6d581SJan Schmidt 4408da6d581SJan Schmidt /* 441*ed58f2e6Sethanwu * 1. We normally enter this function with the path already pointing to 44269bca40dSAlexander Block * the first item to check. But sometimes, we may enter it with 443*ed58f2e6Sethanwu * slot == nritems. 444*ed58f2e6Sethanwu * 2. We are searching for normal backref but bytenr of this leaf 445*ed58f2e6Sethanwu * matches shared data backref 446*ed58f2e6Sethanwu * For these cases, go to the next leaf before we continue. 4478da6d581SJan Schmidt */ 448*ed58f2e6Sethanwu eb = path->nodes[0]; 449*ed58f2e6Sethanwu if (path->slots[0] >= btrfs_header_nritems(eb) || 450*ed58f2e6Sethanwu is_shared_data_backref(preftrees, eb->start)) { 451de47c9d3SEdmund Nadolski if (time_seq == SEQ_LAST) 45221633fc6SQu Wenruo ret = btrfs_next_leaf(root, path); 45321633fc6SQu Wenruo else 4543d7806ecSJan Schmidt ret = btrfs_next_old_leaf(root, path, time_seq); 45521633fc6SQu Wenruo } 4568da6d581SJan Schmidt 45744853868SJosef Bacik while (!ret && count < total_refs) { 4588da6d581SJan Schmidt eb = path->nodes[0]; 45969bca40dSAlexander Block slot = path->slots[0]; 46069bca40dSAlexander Block 46169bca40dSAlexander Block btrfs_item_key_to_cpu(eb, &key, slot); 46269bca40dSAlexander Block 46369bca40dSAlexander Block if (key.objectid != key_for_search->objectid || 46469bca40dSAlexander Block key.type != BTRFS_EXTENT_DATA_KEY) 46569bca40dSAlexander Block break; 46669bca40dSAlexander Block 467*ed58f2e6Sethanwu /* 468*ed58f2e6Sethanwu * We are searching for normal backref but bytenr of this leaf 469*ed58f2e6Sethanwu * matches shared data backref. 470*ed58f2e6Sethanwu */ 471*ed58f2e6Sethanwu if (slot == 0 && is_shared_data_backref(preftrees, eb->start)) { 472*ed58f2e6Sethanwu if (time_seq == SEQ_LAST) 473*ed58f2e6Sethanwu ret = btrfs_next_leaf(root, path); 474*ed58f2e6Sethanwu else 475*ed58f2e6Sethanwu ret = btrfs_next_old_leaf(root, path, time_seq); 476*ed58f2e6Sethanwu continue; 477*ed58f2e6Sethanwu } 47869bca40dSAlexander Block fi = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item); 4798da6d581SJan Schmidt disk_byte = btrfs_file_extent_disk_bytenr(eb, fi); 4807ac8b88eSethanwu data_offset = btrfs_file_extent_offset(eb, fi); 48169bca40dSAlexander Block 48269bca40dSAlexander Block if (disk_byte == wanted_disk_byte) { 48369bca40dSAlexander Block eie = NULL; 484ed8c4913SJosef Bacik old = NULL; 4857ac8b88eSethanwu if (ref->key_for_search.offset == key.offset - data_offset) 4867ef81ac8SJosef Bacik count++; 4877ac8b88eSethanwu else 4887ac8b88eSethanwu goto next; 48969bca40dSAlexander Block if (extent_item_pos) { 49069bca40dSAlexander Block ret = check_extent_in_eb(&key, eb, fi, 49169bca40dSAlexander Block *extent_item_pos, 492c995ab3cSZygo Blaxell &eie, ignore_offset); 49369bca40dSAlexander Block if (ret < 0) 49469bca40dSAlexander Block break; 4958da6d581SJan Schmidt } 496ed8c4913SJosef Bacik if (ret > 0) 497ed8c4913SJosef Bacik goto next; 4984eb1f66dSTakashi Iwai ret = ulist_add_merge_ptr(parents, eb->start, 4994eb1f66dSTakashi Iwai eie, (void **)&old, GFP_NOFS); 50069bca40dSAlexander Block if (ret < 0) 50169bca40dSAlexander Block break; 502ed8c4913SJosef Bacik if (!ret && extent_item_pos) { 503ed8c4913SJosef Bacik while (old->next) 504ed8c4913SJosef Bacik old = old->next; 505ed8c4913SJosef Bacik old->next = eie; 50669bca40dSAlexander Block } 507f05c4746SWang Shilong eie = NULL; 50869bca40dSAlexander Block } 509ed8c4913SJosef Bacik next: 510de47c9d3SEdmund Nadolski if (time_seq == SEQ_LAST) 51121633fc6SQu Wenruo ret = btrfs_next_item(root, path); 51221633fc6SQu Wenruo else 51369bca40dSAlexander Block ret = btrfs_next_old_item(root, path, time_seq); 5148da6d581SJan Schmidt } 5158da6d581SJan Schmidt 51669bca40dSAlexander Block if (ret > 0) 51769bca40dSAlexander Block ret = 0; 518f05c4746SWang Shilong else if (ret < 0) 519f05c4746SWang Shilong free_inode_elem_list(eie); 52069bca40dSAlexander Block return ret; 5218da6d581SJan Schmidt } 5228da6d581SJan Schmidt 5238da6d581SJan Schmidt /* 5248da6d581SJan Schmidt * resolve an indirect backref in the form (root_id, key, level) 5258da6d581SJan Schmidt * to a logical address 5268da6d581SJan Schmidt */ 527e0c476b1SJeff Mahoney static int resolve_indirect_ref(struct btrfs_fs_info *fs_info, 528da61d31aSJosef Bacik struct btrfs_path *path, u64 time_seq, 529*ed58f2e6Sethanwu struct preftrees *preftrees, 530e0c476b1SJeff Mahoney struct prelim_ref *ref, struct ulist *parents, 531c995ab3cSZygo Blaxell const u64 *extent_item_pos, u64 total_refs, 532c995ab3cSZygo Blaxell bool ignore_offset) 5338da6d581SJan Schmidt { 5348da6d581SJan Schmidt struct btrfs_root *root; 5358da6d581SJan Schmidt struct btrfs_key root_key; 5368da6d581SJan Schmidt struct extent_buffer *eb; 5378da6d581SJan Schmidt int ret = 0; 5388da6d581SJan Schmidt int root_level; 5398da6d581SJan Schmidt int level = ref->level; 540538f72cdSWang Shilong int index; 5417ac8b88eSethanwu struct btrfs_key search_key = ref->key_for_search; 5428da6d581SJan Schmidt 5438da6d581SJan Schmidt root_key.objectid = ref->root_id; 5448da6d581SJan Schmidt root_key.type = BTRFS_ROOT_ITEM_KEY; 5458da6d581SJan Schmidt root_key.offset = (u64)-1; 546538f72cdSWang Shilong 547538f72cdSWang Shilong index = srcu_read_lock(&fs_info->subvol_srcu); 548538f72cdSWang Shilong 5492d9e9776SJosef Bacik root = btrfs_get_fs_root(fs_info, &root_key, false); 5508da6d581SJan Schmidt if (IS_ERR(root)) { 551538f72cdSWang Shilong srcu_read_unlock(&fs_info->subvol_srcu, index); 5528da6d581SJan Schmidt ret = PTR_ERR(root); 5539326f76fSJosef Bacik goto out_free; 5549326f76fSJosef Bacik } 5559326f76fSJosef Bacik 556f5ee5c9aSJeff Mahoney if (btrfs_is_testing(fs_info)) { 557d9ee522bSJosef Bacik srcu_read_unlock(&fs_info->subvol_srcu, index); 558d9ee522bSJosef Bacik ret = -ENOENT; 559d9ee522bSJosef Bacik goto out; 560d9ee522bSJosef Bacik } 561d9ee522bSJosef Bacik 5629e351cc8SJosef Bacik if (path->search_commit_root) 5639e351cc8SJosef Bacik root_level = btrfs_header_level(root->commit_root); 564de47c9d3SEdmund Nadolski else if (time_seq == SEQ_LAST) 56521633fc6SQu Wenruo root_level = btrfs_header_level(root->node); 5669e351cc8SJosef Bacik else 5675b6602e7SJan Schmidt root_level = btrfs_old_root_level(root, time_seq); 5688da6d581SJan Schmidt 569538f72cdSWang Shilong if (root_level + 1 == level) { 570538f72cdSWang Shilong srcu_read_unlock(&fs_info->subvol_srcu, index); 5718da6d581SJan Schmidt goto out; 572538f72cdSWang Shilong } 5738da6d581SJan Schmidt 5747ac8b88eSethanwu /* 5757ac8b88eSethanwu * We can often find data backrefs with an offset that is too large 5767ac8b88eSethanwu * (>= LLONG_MAX, maximum allowed file offset) due to underflows when 5777ac8b88eSethanwu * subtracting a file's offset with the data offset of its 5787ac8b88eSethanwu * corresponding extent data item. This can happen for example in the 5797ac8b88eSethanwu * clone ioctl. 5807ac8b88eSethanwu * 5817ac8b88eSethanwu * So if we detect such case we set the search key's offset to zero to 5827ac8b88eSethanwu * make sure we will find the matching file extent item at 5837ac8b88eSethanwu * add_all_parents(), otherwise we will miss it because the offset 5847ac8b88eSethanwu * taken form the backref is much larger then the offset of the file 5857ac8b88eSethanwu * extent item. This can make us scan a very large number of file 5867ac8b88eSethanwu * extent items, but at least it will not make us miss any. 5877ac8b88eSethanwu * 5887ac8b88eSethanwu * This is an ugly workaround for a behaviour that should have never 5897ac8b88eSethanwu * existed, but it does and a fix for the clone ioctl would touch a lot 5907ac8b88eSethanwu * of places, cause backwards incompatibility and would not fix the 5917ac8b88eSethanwu * problem for extents cloned with older kernels. 5927ac8b88eSethanwu */ 5937ac8b88eSethanwu if (search_key.type == BTRFS_EXTENT_DATA_KEY && 5947ac8b88eSethanwu search_key.offset >= LLONG_MAX) 5957ac8b88eSethanwu search_key.offset = 0; 5968da6d581SJan Schmidt path->lowest_level = level; 597de47c9d3SEdmund Nadolski if (time_seq == SEQ_LAST) 5987ac8b88eSethanwu ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0); 59921633fc6SQu Wenruo else 6007ac8b88eSethanwu ret = btrfs_search_old_slot(root, &search_key, path, time_seq); 601538f72cdSWang Shilong 602538f72cdSWang Shilong /* root node has been locked, we can release @subvol_srcu safely here */ 603538f72cdSWang Shilong srcu_read_unlock(&fs_info->subvol_srcu, index); 604538f72cdSWang Shilong 605ab8d0fc4SJeff Mahoney btrfs_debug(fs_info, 606ab8d0fc4SJeff Mahoney "search slot in root %llu (level %d, ref count %d) returned %d for key (%llu %u %llu)", 607c1c9ff7cSGeert Uytterhoeven ref->root_id, level, ref->count, ret, 608c1c9ff7cSGeert Uytterhoeven ref->key_for_search.objectid, ref->key_for_search.type, 609c1c9ff7cSGeert Uytterhoeven ref->key_for_search.offset); 6108da6d581SJan Schmidt if (ret < 0) 6118da6d581SJan Schmidt goto out; 6128da6d581SJan Schmidt 6138da6d581SJan Schmidt eb = path->nodes[level]; 6149345457fSJan Schmidt while (!eb) { 615fae7f21cSDulshani Gunawardhana if (WARN_ON(!level)) { 6168da6d581SJan Schmidt ret = 1; 6178da6d581SJan Schmidt goto out; 6188da6d581SJan Schmidt } 6199345457fSJan Schmidt level--; 6209345457fSJan Schmidt eb = path->nodes[level]; 6219345457fSJan Schmidt } 6228da6d581SJan Schmidt 623*ed58f2e6Sethanwu ret = add_all_parents(root, path, parents, preftrees, ref, level, 624*ed58f2e6Sethanwu time_seq, extent_item_pos, total_refs, ignore_offset); 6258da6d581SJan Schmidt out: 62600246528SJosef Bacik btrfs_put_root(root); 6279326f76fSJosef Bacik out_free: 628da61d31aSJosef Bacik path->lowest_level = 0; 629da61d31aSJosef Bacik btrfs_release_path(path); 6308da6d581SJan Schmidt return ret; 6318da6d581SJan Schmidt } 6328da6d581SJan Schmidt 6334dae077aSJeff Mahoney static struct extent_inode_elem * 6344dae077aSJeff Mahoney unode_aux_to_inode_list(struct ulist_node *node) 6354dae077aSJeff Mahoney { 6364dae077aSJeff Mahoney if (!node) 6374dae077aSJeff Mahoney return NULL; 6384dae077aSJeff Mahoney return (struct extent_inode_elem *)(uintptr_t)node->aux; 6394dae077aSJeff Mahoney } 6404dae077aSJeff Mahoney 6418da6d581SJan Schmidt /* 64252042d8eSAndrea Gelmini * We maintain three separate rbtrees: one for direct refs, one for 64386d5f994SEdmund Nadolski * indirect refs which have a key, and one for indirect refs which do not 64486d5f994SEdmund Nadolski * have a key. Each tree does merge on insertion. 64586d5f994SEdmund Nadolski * 64686d5f994SEdmund Nadolski * Once all of the references are located, we iterate over the tree of 64786d5f994SEdmund Nadolski * indirect refs with missing keys. An appropriate key is located and 64886d5f994SEdmund Nadolski * the ref is moved onto the tree for indirect refs. After all missing 64986d5f994SEdmund Nadolski * keys are thus located, we iterate over the indirect ref tree, resolve 65086d5f994SEdmund Nadolski * each reference, and then insert the resolved reference onto the 65186d5f994SEdmund Nadolski * direct tree (merging there too). 65286d5f994SEdmund Nadolski * 65386d5f994SEdmund Nadolski * New backrefs (i.e., for parent nodes) are added to the appropriate 65486d5f994SEdmund Nadolski * rbtree as they are encountered. The new backrefs are subsequently 65586d5f994SEdmund Nadolski * resolved as above. 6568da6d581SJan Schmidt */ 657e0c476b1SJeff Mahoney static int resolve_indirect_refs(struct btrfs_fs_info *fs_info, 658da61d31aSJosef Bacik struct btrfs_path *path, u64 time_seq, 65986d5f994SEdmund Nadolski struct preftrees *preftrees, 660dc046b10SJosef Bacik const u64 *extent_item_pos, u64 total_refs, 661c995ab3cSZygo Blaxell struct share_check *sc, bool ignore_offset) 6628da6d581SJan Schmidt { 6638da6d581SJan Schmidt int err; 6648da6d581SJan Schmidt int ret = 0; 6658da6d581SJan Schmidt struct ulist *parents; 6668da6d581SJan Schmidt struct ulist_node *node; 667cd1b413cSJan Schmidt struct ulist_iterator uiter; 66886d5f994SEdmund Nadolski struct rb_node *rnode; 6698da6d581SJan Schmidt 6708da6d581SJan Schmidt parents = ulist_alloc(GFP_NOFS); 6718da6d581SJan Schmidt if (!parents) 6728da6d581SJan Schmidt return -ENOMEM; 6738da6d581SJan Schmidt 6748da6d581SJan Schmidt /* 67586d5f994SEdmund Nadolski * We could trade memory usage for performance here by iterating 67686d5f994SEdmund Nadolski * the tree, allocating new refs for each insertion, and then 67786d5f994SEdmund Nadolski * freeing the entire indirect tree when we're done. In some test 67886d5f994SEdmund Nadolski * cases, the tree can grow quite large (~200k objects). 6798da6d581SJan Schmidt */ 680ecf160b4SLiu Bo while ((rnode = rb_first_cached(&preftrees->indirect.root))) { 68186d5f994SEdmund Nadolski struct prelim_ref *ref; 68286d5f994SEdmund Nadolski 68386d5f994SEdmund Nadolski ref = rb_entry(rnode, struct prelim_ref, rbnode); 68486d5f994SEdmund Nadolski if (WARN(ref->parent, 68586d5f994SEdmund Nadolski "BUG: direct ref found in indirect tree")) { 68686d5f994SEdmund Nadolski ret = -EINVAL; 68786d5f994SEdmund Nadolski goto out; 68886d5f994SEdmund Nadolski } 68986d5f994SEdmund Nadolski 690ecf160b4SLiu Bo rb_erase_cached(&ref->rbnode, &preftrees->indirect.root); 6916c336b21SJeff Mahoney preftrees->indirect.count--; 69286d5f994SEdmund Nadolski 69386d5f994SEdmund Nadolski if (ref->count == 0) { 69486d5f994SEdmund Nadolski free_pref(ref); 6958da6d581SJan Schmidt continue; 69686d5f994SEdmund Nadolski } 69786d5f994SEdmund Nadolski 6983ec4d323SEdmund Nadolski if (sc && sc->root_objectid && 6993ec4d323SEdmund Nadolski ref->root_id != sc->root_objectid) { 70086d5f994SEdmund Nadolski free_pref(ref); 701dc046b10SJosef Bacik ret = BACKREF_FOUND_SHARED; 702dc046b10SJosef Bacik goto out; 703dc046b10SJosef Bacik } 704*ed58f2e6Sethanwu err = resolve_indirect_ref(fs_info, path, time_seq, preftrees, 705*ed58f2e6Sethanwu ref, parents, extent_item_pos, 706c995ab3cSZygo Blaxell total_refs, ignore_offset); 70795def2edSWang Shilong /* 70895def2edSWang Shilong * we can only tolerate ENOENT,otherwise,we should catch error 70995def2edSWang Shilong * and return directly. 71095def2edSWang Shilong */ 71195def2edSWang Shilong if (err == -ENOENT) { 7123ec4d323SEdmund Nadolski prelim_ref_insert(fs_info, &preftrees->direct, ref, 7133ec4d323SEdmund Nadolski NULL); 7148da6d581SJan Schmidt continue; 71595def2edSWang Shilong } else if (err) { 71686d5f994SEdmund Nadolski free_pref(ref); 71795def2edSWang Shilong ret = err; 71895def2edSWang Shilong goto out; 71995def2edSWang Shilong } 7208da6d581SJan Schmidt 7218da6d581SJan Schmidt /* we put the first parent into the ref at hand */ 722cd1b413cSJan Schmidt ULIST_ITER_INIT(&uiter); 723cd1b413cSJan Schmidt node = ulist_next(parents, &uiter); 7248da6d581SJan Schmidt ref->parent = node ? node->val : 0; 7254dae077aSJeff Mahoney ref->inode_list = unode_aux_to_inode_list(node); 7268da6d581SJan Schmidt 72786d5f994SEdmund Nadolski /* Add a prelim_ref(s) for any other parent(s). */ 728cd1b413cSJan Schmidt while ((node = ulist_next(parents, &uiter))) { 72986d5f994SEdmund Nadolski struct prelim_ref *new_ref; 73086d5f994SEdmund Nadolski 731b9e9a6cbSWang Shilong new_ref = kmem_cache_alloc(btrfs_prelim_ref_cache, 732b9e9a6cbSWang Shilong GFP_NOFS); 7338da6d581SJan Schmidt if (!new_ref) { 73486d5f994SEdmund Nadolski free_pref(ref); 7358da6d581SJan Schmidt ret = -ENOMEM; 736e36902d4SWang Shilong goto out; 7378da6d581SJan Schmidt } 7388da6d581SJan Schmidt memcpy(new_ref, ref, sizeof(*ref)); 7398da6d581SJan Schmidt new_ref->parent = node->val; 7404dae077aSJeff Mahoney new_ref->inode_list = unode_aux_to_inode_list(node); 7413ec4d323SEdmund Nadolski prelim_ref_insert(fs_info, &preftrees->direct, 7423ec4d323SEdmund Nadolski new_ref, NULL); 7438da6d581SJan Schmidt } 74486d5f994SEdmund Nadolski 7453ec4d323SEdmund Nadolski /* 74652042d8eSAndrea Gelmini * Now it's a direct ref, put it in the direct tree. We must 7473ec4d323SEdmund Nadolski * do this last because the ref could be merged/freed here. 7483ec4d323SEdmund Nadolski */ 7493ec4d323SEdmund Nadolski prelim_ref_insert(fs_info, &preftrees->direct, ref, NULL); 75086d5f994SEdmund Nadolski 7518da6d581SJan Schmidt ulist_reinit(parents); 7529dd14fd6SEdmund Nadolski cond_resched(); 7538da6d581SJan Schmidt } 754e36902d4SWang Shilong out: 7558da6d581SJan Schmidt ulist_free(parents); 7568da6d581SJan Schmidt return ret; 7578da6d581SJan Schmidt } 7588da6d581SJan Schmidt 759d5c88b73SJan Schmidt /* 760d5c88b73SJan Schmidt * read tree blocks and add keys where required. 761d5c88b73SJan Schmidt */ 762e0c476b1SJeff Mahoney static int add_missing_keys(struct btrfs_fs_info *fs_info, 76338e3eebfSJosef Bacik struct preftrees *preftrees, bool lock) 764d5c88b73SJan Schmidt { 765e0c476b1SJeff Mahoney struct prelim_ref *ref; 766d5c88b73SJan Schmidt struct extent_buffer *eb; 76786d5f994SEdmund Nadolski struct preftree *tree = &preftrees->indirect_missing_keys; 76886d5f994SEdmund Nadolski struct rb_node *node; 769d5c88b73SJan Schmidt 770ecf160b4SLiu Bo while ((node = rb_first_cached(&tree->root))) { 77186d5f994SEdmund Nadolski ref = rb_entry(node, struct prelim_ref, rbnode); 772ecf160b4SLiu Bo rb_erase_cached(node, &tree->root); 77386d5f994SEdmund Nadolski 77486d5f994SEdmund Nadolski BUG_ON(ref->parent); /* should not be a direct ref */ 77586d5f994SEdmund Nadolski BUG_ON(ref->key_for_search.type); 776d5c88b73SJan Schmidt BUG_ON(!ref->wanted_disk_byte); 77786d5f994SEdmund Nadolski 778581c1760SQu Wenruo eb = read_tree_block(fs_info, ref->wanted_disk_byte, 0, 779581c1760SQu Wenruo ref->level - 1, NULL); 78064c043deSLiu Bo if (IS_ERR(eb)) { 78186d5f994SEdmund Nadolski free_pref(ref); 78264c043deSLiu Bo return PTR_ERR(eb); 78364c043deSLiu Bo } else if (!extent_buffer_uptodate(eb)) { 78486d5f994SEdmund Nadolski free_pref(ref); 785416bc658SJosef Bacik free_extent_buffer(eb); 786416bc658SJosef Bacik return -EIO; 787416bc658SJosef Bacik } 78838e3eebfSJosef Bacik if (lock) 789d5c88b73SJan Schmidt btrfs_tree_read_lock(eb); 790d5c88b73SJan Schmidt if (btrfs_header_level(eb) == 0) 791d5c88b73SJan Schmidt btrfs_item_key_to_cpu(eb, &ref->key_for_search, 0); 792d5c88b73SJan Schmidt else 793d5c88b73SJan Schmidt btrfs_node_key_to_cpu(eb, &ref->key_for_search, 0); 79438e3eebfSJosef Bacik if (lock) 795d5c88b73SJan Schmidt btrfs_tree_read_unlock(eb); 796d5c88b73SJan Schmidt free_extent_buffer(eb); 7973ec4d323SEdmund Nadolski prelim_ref_insert(fs_info, &preftrees->indirect, ref, NULL); 7989dd14fd6SEdmund Nadolski cond_resched(); 799d5c88b73SJan Schmidt } 800d5c88b73SJan Schmidt return 0; 801d5c88b73SJan Schmidt } 802d5c88b73SJan Schmidt 8038da6d581SJan Schmidt /* 8048da6d581SJan Schmidt * add all currently queued delayed refs from this head whose seq nr is 8058da6d581SJan Schmidt * smaller or equal that seq to the list 8068da6d581SJan Schmidt */ 80700142756SJeff Mahoney static int add_delayed_refs(const struct btrfs_fs_info *fs_info, 80800142756SJeff Mahoney struct btrfs_delayed_ref_head *head, u64 seq, 80986d5f994SEdmund Nadolski struct preftrees *preftrees, u64 *total_refs, 8103ec4d323SEdmund Nadolski struct share_check *sc) 8118da6d581SJan Schmidt { 812c6fc2454SQu Wenruo struct btrfs_delayed_ref_node *node; 8138da6d581SJan Schmidt struct btrfs_delayed_extent_op *extent_op = head->extent_op; 814d5c88b73SJan Schmidt struct btrfs_key key; 81586d5f994SEdmund Nadolski struct btrfs_key tmp_op_key; 8160e0adbcfSJosef Bacik struct rb_node *n; 81701747e92SEdmund Nadolski int count; 818b1375d64SJan Schmidt int ret = 0; 8198da6d581SJan Schmidt 820a6dbceafSNikolay Borisov if (extent_op && extent_op->update_key) 82186d5f994SEdmund Nadolski btrfs_disk_key_to_cpu(&tmp_op_key, &extent_op->key); 8228da6d581SJan Schmidt 823d7df2c79SJosef Bacik spin_lock(&head->lock); 824e3d03965SLiu Bo for (n = rb_first_cached(&head->ref_tree); n; n = rb_next(n)) { 8250e0adbcfSJosef Bacik node = rb_entry(n, struct btrfs_delayed_ref_node, 8260e0adbcfSJosef Bacik ref_node); 8278da6d581SJan Schmidt if (node->seq > seq) 8288da6d581SJan Schmidt continue; 8298da6d581SJan Schmidt 8308da6d581SJan Schmidt switch (node->action) { 8318da6d581SJan Schmidt case BTRFS_ADD_DELAYED_EXTENT: 8328da6d581SJan Schmidt case BTRFS_UPDATE_DELAYED_HEAD: 8338da6d581SJan Schmidt WARN_ON(1); 8348da6d581SJan Schmidt continue; 8358da6d581SJan Schmidt case BTRFS_ADD_DELAYED_REF: 83601747e92SEdmund Nadolski count = node->ref_mod; 8378da6d581SJan Schmidt break; 8388da6d581SJan Schmidt case BTRFS_DROP_DELAYED_REF: 83901747e92SEdmund Nadolski count = node->ref_mod * -1; 8408da6d581SJan Schmidt break; 8418da6d581SJan Schmidt default: 842290342f6SArnd Bergmann BUG(); 8438da6d581SJan Schmidt } 84401747e92SEdmund Nadolski *total_refs += count; 8458da6d581SJan Schmidt switch (node->type) { 8468da6d581SJan Schmidt case BTRFS_TREE_BLOCK_REF_KEY: { 84786d5f994SEdmund Nadolski /* NORMAL INDIRECT METADATA backref */ 8488da6d581SJan Schmidt struct btrfs_delayed_tree_ref *ref; 8498da6d581SJan Schmidt 8508da6d581SJan Schmidt ref = btrfs_delayed_node_to_tree_ref(node); 85100142756SJeff Mahoney ret = add_indirect_ref(fs_info, preftrees, ref->root, 85200142756SJeff Mahoney &tmp_op_key, ref->level + 1, 85301747e92SEdmund Nadolski node->bytenr, count, sc, 85401747e92SEdmund Nadolski GFP_ATOMIC); 8558da6d581SJan Schmidt break; 8568da6d581SJan Schmidt } 8578da6d581SJan Schmidt case BTRFS_SHARED_BLOCK_REF_KEY: { 85886d5f994SEdmund Nadolski /* SHARED DIRECT METADATA backref */ 8598da6d581SJan Schmidt struct btrfs_delayed_tree_ref *ref; 8608da6d581SJan Schmidt 8618da6d581SJan Schmidt ref = btrfs_delayed_node_to_tree_ref(node); 86286d5f994SEdmund Nadolski 86301747e92SEdmund Nadolski ret = add_direct_ref(fs_info, preftrees, ref->level + 1, 86401747e92SEdmund Nadolski ref->parent, node->bytenr, count, 8653ec4d323SEdmund Nadolski sc, GFP_ATOMIC); 8668da6d581SJan Schmidt break; 8678da6d581SJan Schmidt } 8688da6d581SJan Schmidt case BTRFS_EXTENT_DATA_REF_KEY: { 86986d5f994SEdmund Nadolski /* NORMAL INDIRECT DATA backref */ 8708da6d581SJan Schmidt struct btrfs_delayed_data_ref *ref; 8718da6d581SJan Schmidt ref = btrfs_delayed_node_to_data_ref(node); 8728da6d581SJan Schmidt 8738da6d581SJan Schmidt key.objectid = ref->objectid; 8748da6d581SJan Schmidt key.type = BTRFS_EXTENT_DATA_KEY; 8758da6d581SJan Schmidt key.offset = ref->offset; 876dc046b10SJosef Bacik 877dc046b10SJosef Bacik /* 878dc046b10SJosef Bacik * Found a inum that doesn't match our known inum, we 879dc046b10SJosef Bacik * know it's shared. 880dc046b10SJosef Bacik */ 8813ec4d323SEdmund Nadolski if (sc && sc->inum && ref->objectid != sc->inum) { 882dc046b10SJosef Bacik ret = BACKREF_FOUND_SHARED; 8833ec4d323SEdmund Nadolski goto out; 884dc046b10SJosef Bacik } 885dc046b10SJosef Bacik 88600142756SJeff Mahoney ret = add_indirect_ref(fs_info, preftrees, ref->root, 88701747e92SEdmund Nadolski &key, 0, node->bytenr, count, sc, 88801747e92SEdmund Nadolski GFP_ATOMIC); 8898da6d581SJan Schmidt break; 8908da6d581SJan Schmidt } 8918da6d581SJan Schmidt case BTRFS_SHARED_DATA_REF_KEY: { 89286d5f994SEdmund Nadolski /* SHARED DIRECT FULL backref */ 8938da6d581SJan Schmidt struct btrfs_delayed_data_ref *ref; 8948da6d581SJan Schmidt 8958da6d581SJan Schmidt ref = btrfs_delayed_node_to_data_ref(node); 89686d5f994SEdmund Nadolski 89701747e92SEdmund Nadolski ret = add_direct_ref(fs_info, preftrees, 0, ref->parent, 89801747e92SEdmund Nadolski node->bytenr, count, sc, 89901747e92SEdmund Nadolski GFP_ATOMIC); 9008da6d581SJan Schmidt break; 9018da6d581SJan Schmidt } 9028da6d581SJan Schmidt default: 9038da6d581SJan Schmidt WARN_ON(1); 9048da6d581SJan Schmidt } 9053ec4d323SEdmund Nadolski /* 9063ec4d323SEdmund Nadolski * We must ignore BACKREF_FOUND_SHARED until all delayed 9073ec4d323SEdmund Nadolski * refs have been checked. 9083ec4d323SEdmund Nadolski */ 9093ec4d323SEdmund Nadolski if (ret && (ret != BACKREF_FOUND_SHARED)) 910d7df2c79SJosef Bacik break; 9118da6d581SJan Schmidt } 9123ec4d323SEdmund Nadolski if (!ret) 9133ec4d323SEdmund Nadolski ret = extent_is_shared(sc); 9143ec4d323SEdmund Nadolski out: 915d7df2c79SJosef Bacik spin_unlock(&head->lock); 916d7df2c79SJosef Bacik return ret; 9178da6d581SJan Schmidt } 9188da6d581SJan Schmidt 9198da6d581SJan Schmidt /* 9208da6d581SJan Schmidt * add all inline backrefs for bytenr to the list 9213ec4d323SEdmund Nadolski * 9223ec4d323SEdmund Nadolski * Returns 0 on success, <0 on error, or BACKREF_FOUND_SHARED. 9238da6d581SJan Schmidt */ 92400142756SJeff Mahoney static int add_inline_refs(const struct btrfs_fs_info *fs_info, 92500142756SJeff Mahoney struct btrfs_path *path, u64 bytenr, 92686d5f994SEdmund Nadolski int *info_level, struct preftrees *preftrees, 9273ec4d323SEdmund Nadolski u64 *total_refs, struct share_check *sc) 9288da6d581SJan Schmidt { 929b1375d64SJan Schmidt int ret = 0; 9308da6d581SJan Schmidt int slot; 9318da6d581SJan Schmidt struct extent_buffer *leaf; 9328da6d581SJan Schmidt struct btrfs_key key; 933261c84b6SJosef Bacik struct btrfs_key found_key; 9348da6d581SJan Schmidt unsigned long ptr; 9358da6d581SJan Schmidt unsigned long end; 9368da6d581SJan Schmidt struct btrfs_extent_item *ei; 9378da6d581SJan Schmidt u64 flags; 9388da6d581SJan Schmidt u64 item_size; 9398da6d581SJan Schmidt 9408da6d581SJan Schmidt /* 9418da6d581SJan Schmidt * enumerate all inline refs 9428da6d581SJan Schmidt */ 9438da6d581SJan Schmidt leaf = path->nodes[0]; 944dadcaf78SJan Schmidt slot = path->slots[0]; 9458da6d581SJan Schmidt 9468da6d581SJan Schmidt item_size = btrfs_item_size_nr(leaf, slot); 9478da6d581SJan Schmidt BUG_ON(item_size < sizeof(*ei)); 9488da6d581SJan Schmidt 9498da6d581SJan Schmidt ei = btrfs_item_ptr(leaf, slot, struct btrfs_extent_item); 9508da6d581SJan Schmidt flags = btrfs_extent_flags(leaf, ei); 95144853868SJosef Bacik *total_refs += btrfs_extent_refs(leaf, ei); 952261c84b6SJosef Bacik btrfs_item_key_to_cpu(leaf, &found_key, slot); 9538da6d581SJan Schmidt 9548da6d581SJan Schmidt ptr = (unsigned long)(ei + 1); 9558da6d581SJan Schmidt end = (unsigned long)ei + item_size; 9568da6d581SJan Schmidt 957261c84b6SJosef Bacik if (found_key.type == BTRFS_EXTENT_ITEM_KEY && 958261c84b6SJosef Bacik flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) { 9598da6d581SJan Schmidt struct btrfs_tree_block_info *info; 9608da6d581SJan Schmidt 9618da6d581SJan Schmidt info = (struct btrfs_tree_block_info *)ptr; 9628da6d581SJan Schmidt *info_level = btrfs_tree_block_level(leaf, info); 9638da6d581SJan Schmidt ptr += sizeof(struct btrfs_tree_block_info); 9648da6d581SJan Schmidt BUG_ON(ptr > end); 965261c84b6SJosef Bacik } else if (found_key.type == BTRFS_METADATA_ITEM_KEY) { 966261c84b6SJosef Bacik *info_level = found_key.offset; 9678da6d581SJan Schmidt } else { 9688da6d581SJan Schmidt BUG_ON(!(flags & BTRFS_EXTENT_FLAG_DATA)); 9698da6d581SJan Schmidt } 9708da6d581SJan Schmidt 9718da6d581SJan Schmidt while (ptr < end) { 9728da6d581SJan Schmidt struct btrfs_extent_inline_ref *iref; 9738da6d581SJan Schmidt u64 offset; 9748da6d581SJan Schmidt int type; 9758da6d581SJan Schmidt 9768da6d581SJan Schmidt iref = (struct btrfs_extent_inline_ref *)ptr; 9773de28d57SLiu Bo type = btrfs_get_extent_inline_ref_type(leaf, iref, 9783de28d57SLiu Bo BTRFS_REF_TYPE_ANY); 9793de28d57SLiu Bo if (type == BTRFS_REF_TYPE_INVALID) 980af431dcbSSu Yue return -EUCLEAN; 9813de28d57SLiu Bo 9828da6d581SJan Schmidt offset = btrfs_extent_inline_ref_offset(leaf, iref); 9838da6d581SJan Schmidt 9848da6d581SJan Schmidt switch (type) { 9858da6d581SJan Schmidt case BTRFS_SHARED_BLOCK_REF_KEY: 98600142756SJeff Mahoney ret = add_direct_ref(fs_info, preftrees, 98700142756SJeff Mahoney *info_level + 1, offset, 9883ec4d323SEdmund Nadolski bytenr, 1, NULL, GFP_NOFS); 9898da6d581SJan Schmidt break; 9908da6d581SJan Schmidt case BTRFS_SHARED_DATA_REF_KEY: { 9918da6d581SJan Schmidt struct btrfs_shared_data_ref *sdref; 9928da6d581SJan Schmidt int count; 9938da6d581SJan Schmidt 9948da6d581SJan Schmidt sdref = (struct btrfs_shared_data_ref *)(iref + 1); 9958da6d581SJan Schmidt count = btrfs_shared_data_ref_count(leaf, sdref); 99686d5f994SEdmund Nadolski 99700142756SJeff Mahoney ret = add_direct_ref(fs_info, preftrees, 0, offset, 9983ec4d323SEdmund Nadolski bytenr, count, sc, GFP_NOFS); 9998da6d581SJan Schmidt break; 10008da6d581SJan Schmidt } 10018da6d581SJan Schmidt case BTRFS_TREE_BLOCK_REF_KEY: 100200142756SJeff Mahoney ret = add_indirect_ref(fs_info, preftrees, offset, 100300142756SJeff Mahoney NULL, *info_level + 1, 10043ec4d323SEdmund Nadolski bytenr, 1, NULL, GFP_NOFS); 10058da6d581SJan Schmidt break; 10068da6d581SJan Schmidt case BTRFS_EXTENT_DATA_REF_KEY: { 10078da6d581SJan Schmidt struct btrfs_extent_data_ref *dref; 10088da6d581SJan Schmidt int count; 10098da6d581SJan Schmidt u64 root; 10108da6d581SJan Schmidt 10118da6d581SJan Schmidt dref = (struct btrfs_extent_data_ref *)(&iref->offset); 10128da6d581SJan Schmidt count = btrfs_extent_data_ref_count(leaf, dref); 10138da6d581SJan Schmidt key.objectid = btrfs_extent_data_ref_objectid(leaf, 10148da6d581SJan Schmidt dref); 10158da6d581SJan Schmidt key.type = BTRFS_EXTENT_DATA_KEY; 10168da6d581SJan Schmidt key.offset = btrfs_extent_data_ref_offset(leaf, dref); 1017dc046b10SJosef Bacik 10183ec4d323SEdmund Nadolski if (sc && sc->inum && key.objectid != sc->inum) { 1019dc046b10SJosef Bacik ret = BACKREF_FOUND_SHARED; 1020dc046b10SJosef Bacik break; 1021dc046b10SJosef Bacik } 1022dc046b10SJosef Bacik 10238da6d581SJan Schmidt root = btrfs_extent_data_ref_root(leaf, dref); 102486d5f994SEdmund Nadolski 102500142756SJeff Mahoney ret = add_indirect_ref(fs_info, preftrees, root, 102600142756SJeff Mahoney &key, 0, bytenr, count, 10273ec4d323SEdmund Nadolski sc, GFP_NOFS); 10288da6d581SJan Schmidt break; 10298da6d581SJan Schmidt } 10308da6d581SJan Schmidt default: 10318da6d581SJan Schmidt WARN_ON(1); 10328da6d581SJan Schmidt } 10331149ab6bSWang Shilong if (ret) 10341149ab6bSWang Shilong return ret; 10358da6d581SJan Schmidt ptr += btrfs_extent_inline_ref_size(type); 10368da6d581SJan Schmidt } 10378da6d581SJan Schmidt 10388da6d581SJan Schmidt return 0; 10398da6d581SJan Schmidt } 10408da6d581SJan Schmidt 10418da6d581SJan Schmidt /* 10428da6d581SJan Schmidt * add all non-inline backrefs for bytenr to the list 10433ec4d323SEdmund Nadolski * 10443ec4d323SEdmund Nadolski * Returns 0 on success, <0 on error, or BACKREF_FOUND_SHARED. 10458da6d581SJan Schmidt */ 1046e0c476b1SJeff Mahoney static int add_keyed_refs(struct btrfs_fs_info *fs_info, 10478da6d581SJan Schmidt struct btrfs_path *path, u64 bytenr, 104886d5f994SEdmund Nadolski int info_level, struct preftrees *preftrees, 10493ec4d323SEdmund Nadolski struct share_check *sc) 10508da6d581SJan Schmidt { 10518da6d581SJan Schmidt struct btrfs_root *extent_root = fs_info->extent_root; 10528da6d581SJan Schmidt int ret; 10538da6d581SJan Schmidt int slot; 10548da6d581SJan Schmidt struct extent_buffer *leaf; 10558da6d581SJan Schmidt struct btrfs_key key; 10568da6d581SJan Schmidt 10578da6d581SJan Schmidt while (1) { 10588da6d581SJan Schmidt ret = btrfs_next_item(extent_root, path); 10598da6d581SJan Schmidt if (ret < 0) 10608da6d581SJan Schmidt break; 10618da6d581SJan Schmidt if (ret) { 10628da6d581SJan Schmidt ret = 0; 10638da6d581SJan Schmidt break; 10648da6d581SJan Schmidt } 10658da6d581SJan Schmidt 10668da6d581SJan Schmidt slot = path->slots[0]; 10678da6d581SJan Schmidt leaf = path->nodes[0]; 10688da6d581SJan Schmidt btrfs_item_key_to_cpu(leaf, &key, slot); 10698da6d581SJan Schmidt 10708da6d581SJan Schmidt if (key.objectid != bytenr) 10718da6d581SJan Schmidt break; 10728da6d581SJan Schmidt if (key.type < BTRFS_TREE_BLOCK_REF_KEY) 10738da6d581SJan Schmidt continue; 10748da6d581SJan Schmidt if (key.type > BTRFS_SHARED_DATA_REF_KEY) 10758da6d581SJan Schmidt break; 10768da6d581SJan Schmidt 10778da6d581SJan Schmidt switch (key.type) { 10788da6d581SJan Schmidt case BTRFS_SHARED_BLOCK_REF_KEY: 107986d5f994SEdmund Nadolski /* SHARED DIRECT METADATA backref */ 108000142756SJeff Mahoney ret = add_direct_ref(fs_info, preftrees, 108100142756SJeff Mahoney info_level + 1, key.offset, 10823ec4d323SEdmund Nadolski bytenr, 1, NULL, GFP_NOFS); 10838da6d581SJan Schmidt break; 10848da6d581SJan Schmidt case BTRFS_SHARED_DATA_REF_KEY: { 108586d5f994SEdmund Nadolski /* SHARED DIRECT FULL backref */ 10868da6d581SJan Schmidt struct btrfs_shared_data_ref *sdref; 10878da6d581SJan Schmidt int count; 10888da6d581SJan Schmidt 10898da6d581SJan Schmidt sdref = btrfs_item_ptr(leaf, slot, 10908da6d581SJan Schmidt struct btrfs_shared_data_ref); 10918da6d581SJan Schmidt count = btrfs_shared_data_ref_count(leaf, sdref); 109200142756SJeff Mahoney ret = add_direct_ref(fs_info, preftrees, 0, 109300142756SJeff Mahoney key.offset, bytenr, count, 10943ec4d323SEdmund Nadolski sc, GFP_NOFS); 10958da6d581SJan Schmidt break; 10968da6d581SJan Schmidt } 10978da6d581SJan Schmidt case BTRFS_TREE_BLOCK_REF_KEY: 109886d5f994SEdmund Nadolski /* NORMAL INDIRECT METADATA backref */ 109900142756SJeff Mahoney ret = add_indirect_ref(fs_info, preftrees, key.offset, 110000142756SJeff Mahoney NULL, info_level + 1, bytenr, 11013ec4d323SEdmund Nadolski 1, NULL, GFP_NOFS); 11028da6d581SJan Schmidt break; 11038da6d581SJan Schmidt case BTRFS_EXTENT_DATA_REF_KEY: { 110486d5f994SEdmund Nadolski /* NORMAL INDIRECT DATA backref */ 11058da6d581SJan Schmidt struct btrfs_extent_data_ref *dref; 11068da6d581SJan Schmidt int count; 11078da6d581SJan Schmidt u64 root; 11088da6d581SJan Schmidt 11098da6d581SJan Schmidt dref = btrfs_item_ptr(leaf, slot, 11108da6d581SJan Schmidt struct btrfs_extent_data_ref); 11118da6d581SJan Schmidt count = btrfs_extent_data_ref_count(leaf, dref); 11128da6d581SJan Schmidt key.objectid = btrfs_extent_data_ref_objectid(leaf, 11138da6d581SJan Schmidt dref); 11148da6d581SJan Schmidt key.type = BTRFS_EXTENT_DATA_KEY; 11158da6d581SJan Schmidt key.offset = btrfs_extent_data_ref_offset(leaf, dref); 1116dc046b10SJosef Bacik 11173ec4d323SEdmund Nadolski if (sc && sc->inum && key.objectid != sc->inum) { 1118dc046b10SJosef Bacik ret = BACKREF_FOUND_SHARED; 1119dc046b10SJosef Bacik break; 1120dc046b10SJosef Bacik } 1121dc046b10SJosef Bacik 11228da6d581SJan Schmidt root = btrfs_extent_data_ref_root(leaf, dref); 112300142756SJeff Mahoney ret = add_indirect_ref(fs_info, preftrees, root, 112400142756SJeff Mahoney &key, 0, bytenr, count, 11253ec4d323SEdmund Nadolski sc, GFP_NOFS); 11268da6d581SJan Schmidt break; 11278da6d581SJan Schmidt } 11288da6d581SJan Schmidt default: 11298da6d581SJan Schmidt WARN_ON(1); 11308da6d581SJan Schmidt } 11311149ab6bSWang Shilong if (ret) 11321149ab6bSWang Shilong return ret; 11331149ab6bSWang Shilong 11348da6d581SJan Schmidt } 11358da6d581SJan Schmidt 11368da6d581SJan Schmidt return ret; 11378da6d581SJan Schmidt } 11388da6d581SJan Schmidt 11398da6d581SJan Schmidt /* 11408da6d581SJan Schmidt * this adds all existing backrefs (inline backrefs, backrefs and delayed 11418da6d581SJan Schmidt * refs) for the given bytenr to the refs list, merges duplicates and resolves 11428da6d581SJan Schmidt * indirect refs to their parent bytenr. 11438da6d581SJan Schmidt * When roots are found, they're added to the roots list 11448da6d581SJan Schmidt * 1145de47c9d3SEdmund Nadolski * If time_seq is set to SEQ_LAST, it will not search delayed_refs, and behave 114621633fc6SQu Wenruo * much like trans == NULL case, the difference only lies in it will not 114721633fc6SQu Wenruo * commit root. 114821633fc6SQu Wenruo * The special case is for qgroup to search roots in commit_transaction(). 114921633fc6SQu Wenruo * 11503ec4d323SEdmund Nadolski * @sc - if !NULL, then immediately return BACKREF_FOUND_SHARED when a 11513ec4d323SEdmund Nadolski * shared extent is detected. 11523ec4d323SEdmund Nadolski * 11533ec4d323SEdmund Nadolski * Otherwise this returns 0 for success and <0 for an error. 11543ec4d323SEdmund Nadolski * 1155c995ab3cSZygo Blaxell * If ignore_offset is set to false, only extent refs whose offsets match 1156c995ab3cSZygo Blaxell * extent_item_pos are returned. If true, every extent ref is returned 1157c995ab3cSZygo Blaxell * and extent_item_pos is ignored. 1158c995ab3cSZygo Blaxell * 11598da6d581SJan Schmidt * FIXME some caching might speed things up 11608da6d581SJan Schmidt */ 11618da6d581SJan Schmidt static int find_parent_nodes(struct btrfs_trans_handle *trans, 11628da6d581SJan Schmidt struct btrfs_fs_info *fs_info, u64 bytenr, 1163097b8a7cSJan Schmidt u64 time_seq, struct ulist *refs, 1164dc046b10SJosef Bacik struct ulist *roots, const u64 *extent_item_pos, 1165c995ab3cSZygo Blaxell struct share_check *sc, bool ignore_offset) 11668da6d581SJan Schmidt { 11678da6d581SJan Schmidt struct btrfs_key key; 11688da6d581SJan Schmidt struct btrfs_path *path; 11698da6d581SJan Schmidt struct btrfs_delayed_ref_root *delayed_refs = NULL; 1170d3b01064SLi Zefan struct btrfs_delayed_ref_head *head; 11718da6d581SJan Schmidt int info_level = 0; 11728da6d581SJan Schmidt int ret; 1173e0c476b1SJeff Mahoney struct prelim_ref *ref; 117486d5f994SEdmund Nadolski struct rb_node *node; 1175f05c4746SWang Shilong struct extent_inode_elem *eie = NULL; 117686d5f994SEdmund Nadolski /* total of both direct AND indirect refs! */ 117744853868SJosef Bacik u64 total_refs = 0; 117886d5f994SEdmund Nadolski struct preftrees preftrees = { 117986d5f994SEdmund Nadolski .direct = PREFTREE_INIT, 118086d5f994SEdmund Nadolski .indirect = PREFTREE_INIT, 118186d5f994SEdmund Nadolski .indirect_missing_keys = PREFTREE_INIT 118286d5f994SEdmund Nadolski }; 11838da6d581SJan Schmidt 11848da6d581SJan Schmidt key.objectid = bytenr; 11858da6d581SJan Schmidt key.offset = (u64)-1; 1186261c84b6SJosef Bacik if (btrfs_fs_incompat(fs_info, SKINNY_METADATA)) 1187261c84b6SJosef Bacik key.type = BTRFS_METADATA_ITEM_KEY; 1188261c84b6SJosef Bacik else 1189261c84b6SJosef Bacik key.type = BTRFS_EXTENT_ITEM_KEY; 11908da6d581SJan Schmidt 11918da6d581SJan Schmidt path = btrfs_alloc_path(); 11928da6d581SJan Schmidt if (!path) 11938da6d581SJan Schmidt return -ENOMEM; 1194e84752d4SWang Shilong if (!trans) { 1195da61d31aSJosef Bacik path->search_commit_root = 1; 1196e84752d4SWang Shilong path->skip_locking = 1; 1197e84752d4SWang Shilong } 11988da6d581SJan Schmidt 1199de47c9d3SEdmund Nadolski if (time_seq == SEQ_LAST) 120021633fc6SQu Wenruo path->skip_locking = 1; 120121633fc6SQu Wenruo 12028da6d581SJan Schmidt /* 12038da6d581SJan Schmidt * grab both a lock on the path and a lock on the delayed ref head. 12048da6d581SJan Schmidt * We need both to get a consistent picture of how the refs look 12058da6d581SJan Schmidt * at a specified point in time 12068da6d581SJan Schmidt */ 12078da6d581SJan Schmidt again: 1208d3b01064SLi Zefan head = NULL; 1209d3b01064SLi Zefan 12108da6d581SJan Schmidt ret = btrfs_search_slot(trans, fs_info->extent_root, &key, path, 0, 0); 12118da6d581SJan Schmidt if (ret < 0) 12128da6d581SJan Schmidt goto out; 12138da6d581SJan Schmidt BUG_ON(ret == 0); 12148da6d581SJan Schmidt 1215faa2dbf0SJosef Bacik #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS 121621633fc6SQu Wenruo if (trans && likely(trans->type != __TRANS_DUMMY) && 1217de47c9d3SEdmund Nadolski time_seq != SEQ_LAST) { 1218faa2dbf0SJosef Bacik #else 1219de47c9d3SEdmund Nadolski if (trans && time_seq != SEQ_LAST) { 1220faa2dbf0SJosef Bacik #endif 12218da6d581SJan Schmidt /* 12227a3ae2f8SJan Schmidt * look if there are updates for this ref queued and lock the 12237a3ae2f8SJan Schmidt * head 12248da6d581SJan Schmidt */ 12258da6d581SJan Schmidt delayed_refs = &trans->transaction->delayed_refs; 12268da6d581SJan Schmidt spin_lock(&delayed_refs->lock); 1227f72ad18eSLiu Bo head = btrfs_find_delayed_ref_head(delayed_refs, bytenr); 12288da6d581SJan Schmidt if (head) { 12298da6d581SJan Schmidt if (!mutex_trylock(&head->mutex)) { 1230d278850eSJosef Bacik refcount_inc(&head->refs); 12318da6d581SJan Schmidt spin_unlock(&delayed_refs->lock); 12328da6d581SJan Schmidt 12338da6d581SJan Schmidt btrfs_release_path(path); 12348da6d581SJan Schmidt 12358da6d581SJan Schmidt /* 12368da6d581SJan Schmidt * Mutex was contended, block until it's 12378da6d581SJan Schmidt * released and try again 12388da6d581SJan Schmidt */ 12398da6d581SJan Schmidt mutex_lock(&head->mutex); 12408da6d581SJan Schmidt mutex_unlock(&head->mutex); 1241d278850eSJosef Bacik btrfs_put_delayed_ref_head(head); 12428da6d581SJan Schmidt goto again; 12438da6d581SJan Schmidt } 1244d7df2c79SJosef Bacik spin_unlock(&delayed_refs->lock); 124500142756SJeff Mahoney ret = add_delayed_refs(fs_info, head, time_seq, 12463ec4d323SEdmund Nadolski &preftrees, &total_refs, sc); 1247155725c9SJan Schmidt mutex_unlock(&head->mutex); 1248d7df2c79SJosef Bacik if (ret) 12498da6d581SJan Schmidt goto out; 1250d7df2c79SJosef Bacik } else { 12518da6d581SJan Schmidt spin_unlock(&delayed_refs->lock); 12527a3ae2f8SJan Schmidt } 1253d7df2c79SJosef Bacik } 12548da6d581SJan Schmidt 12558da6d581SJan Schmidt if (path->slots[0]) { 12568da6d581SJan Schmidt struct extent_buffer *leaf; 12578da6d581SJan Schmidt int slot; 12588da6d581SJan Schmidt 1259dadcaf78SJan Schmidt path->slots[0]--; 12608da6d581SJan Schmidt leaf = path->nodes[0]; 1261dadcaf78SJan Schmidt slot = path->slots[0]; 12628da6d581SJan Schmidt btrfs_item_key_to_cpu(leaf, &key, slot); 12638da6d581SJan Schmidt if (key.objectid == bytenr && 1264261c84b6SJosef Bacik (key.type == BTRFS_EXTENT_ITEM_KEY || 1265261c84b6SJosef Bacik key.type == BTRFS_METADATA_ITEM_KEY)) { 126600142756SJeff Mahoney ret = add_inline_refs(fs_info, path, bytenr, 126700142756SJeff Mahoney &info_level, &preftrees, 12683ec4d323SEdmund Nadolski &total_refs, sc); 12698da6d581SJan Schmidt if (ret) 12708da6d581SJan Schmidt goto out; 1271e0c476b1SJeff Mahoney ret = add_keyed_refs(fs_info, path, bytenr, info_level, 12723ec4d323SEdmund Nadolski &preftrees, sc); 12738da6d581SJan Schmidt if (ret) 12748da6d581SJan Schmidt goto out; 12758da6d581SJan Schmidt } 12768da6d581SJan Schmidt } 127786d5f994SEdmund Nadolski 12788da6d581SJan Schmidt btrfs_release_path(path); 12798da6d581SJan Schmidt 128038e3eebfSJosef Bacik ret = add_missing_keys(fs_info, &preftrees, path->skip_locking == 0); 1281d5c88b73SJan Schmidt if (ret) 1282d5c88b73SJan Schmidt goto out; 1283d5c88b73SJan Schmidt 1284ecf160b4SLiu Bo WARN_ON(!RB_EMPTY_ROOT(&preftrees.indirect_missing_keys.root.rb_root)); 12858da6d581SJan Schmidt 128686d5f994SEdmund Nadolski ret = resolve_indirect_refs(fs_info, path, time_seq, &preftrees, 1287c995ab3cSZygo Blaxell extent_item_pos, total_refs, sc, ignore_offset); 12888da6d581SJan Schmidt if (ret) 12898da6d581SJan Schmidt goto out; 12908da6d581SJan Schmidt 1291ecf160b4SLiu Bo WARN_ON(!RB_EMPTY_ROOT(&preftrees.indirect.root.rb_root)); 12928da6d581SJan Schmidt 129386d5f994SEdmund Nadolski /* 129486d5f994SEdmund Nadolski * This walks the tree of merged and resolved refs. Tree blocks are 129586d5f994SEdmund Nadolski * read in as needed. Unique entries are added to the ulist, and 129686d5f994SEdmund Nadolski * the list of found roots is updated. 129786d5f994SEdmund Nadolski * 129886d5f994SEdmund Nadolski * We release the entire tree in one go before returning. 129986d5f994SEdmund Nadolski */ 1300ecf160b4SLiu Bo node = rb_first_cached(&preftrees.direct.root); 130186d5f994SEdmund Nadolski while (node) { 130286d5f994SEdmund Nadolski ref = rb_entry(node, struct prelim_ref, rbnode); 130386d5f994SEdmund Nadolski node = rb_next(&ref->rbnode); 1304c8195a7bSZygo Blaxell /* 1305c8195a7bSZygo Blaxell * ref->count < 0 can happen here if there are delayed 1306c8195a7bSZygo Blaxell * refs with a node->action of BTRFS_DROP_DELAYED_REF. 1307c8195a7bSZygo Blaxell * prelim_ref_insert() relies on this when merging 1308c8195a7bSZygo Blaxell * identical refs to keep the overall count correct. 1309c8195a7bSZygo Blaxell * prelim_ref_insert() will merge only those refs 1310c8195a7bSZygo Blaxell * which compare identically. Any refs having 1311c8195a7bSZygo Blaxell * e.g. different offsets would not be merged, 1312c8195a7bSZygo Blaxell * and would retain their original ref->count < 0. 1313c8195a7bSZygo Blaxell */ 131498cfee21SWang Shilong if (roots && ref->count && ref->root_id && ref->parent == 0) { 13153ec4d323SEdmund Nadolski if (sc && sc->root_objectid && 13163ec4d323SEdmund Nadolski ref->root_id != sc->root_objectid) { 1317dc046b10SJosef Bacik ret = BACKREF_FOUND_SHARED; 1318dc046b10SJosef Bacik goto out; 1319dc046b10SJosef Bacik } 1320dc046b10SJosef Bacik 13218da6d581SJan Schmidt /* no parent == root of tree */ 13228da6d581SJan Schmidt ret = ulist_add(roots, ref->root_id, 0, GFP_NOFS); 1323f1723939SWang Shilong if (ret < 0) 1324f1723939SWang Shilong goto out; 13258da6d581SJan Schmidt } 13268da6d581SJan Schmidt if (ref->count && ref->parent) { 13278a56457fSJosef Bacik if (extent_item_pos && !ref->inode_list && 13288a56457fSJosef Bacik ref->level == 0) { 1329976b1908SJan Schmidt struct extent_buffer *eb; 1330707e8a07SDavid Sterba 1331581c1760SQu Wenruo eb = read_tree_block(fs_info, ref->parent, 0, 1332581c1760SQu Wenruo ref->level, NULL); 133364c043deSLiu Bo if (IS_ERR(eb)) { 133464c043deSLiu Bo ret = PTR_ERR(eb); 133564c043deSLiu Bo goto out; 133664c043deSLiu Bo } else if (!extent_buffer_uptodate(eb)) { 1337416bc658SJosef Bacik free_extent_buffer(eb); 1338c16c2e2eSWang Shilong ret = -EIO; 1339c16c2e2eSWang Shilong goto out; 1340416bc658SJosef Bacik } 134138e3eebfSJosef Bacik 134238e3eebfSJosef Bacik if (!path->skip_locking) { 13436f7ff6d7SFilipe Manana btrfs_tree_read_lock(eb); 1344300aa896SDavid Sterba btrfs_set_lock_blocking_read(eb); 134538e3eebfSJosef Bacik } 1346976b1908SJan Schmidt ret = find_extent_in_eb(eb, bytenr, 1347c995ab3cSZygo Blaxell *extent_item_pos, &eie, ignore_offset); 134838e3eebfSJosef Bacik if (!path->skip_locking) 13496f7ff6d7SFilipe Manana btrfs_tree_read_unlock_blocking(eb); 1350976b1908SJan Schmidt free_extent_buffer(eb); 1351f5929cd8SFilipe David Borba Manana if (ret < 0) 1352f5929cd8SFilipe David Borba Manana goto out; 1353f5929cd8SFilipe David Borba Manana ref->inode_list = eie; 1354976b1908SJan Schmidt } 13554eb1f66dSTakashi Iwai ret = ulist_add_merge_ptr(refs, ref->parent, 13564eb1f66dSTakashi Iwai ref->inode_list, 13574eb1f66dSTakashi Iwai (void **)&eie, GFP_NOFS); 1358f1723939SWang Shilong if (ret < 0) 1359f1723939SWang Shilong goto out; 13603301958bSJan Schmidt if (!ret && extent_item_pos) { 13613301958bSJan Schmidt /* 13623301958bSJan Schmidt * we've recorded that parent, so we must extend 13633301958bSJan Schmidt * its inode list here 13643301958bSJan Schmidt */ 13653301958bSJan Schmidt BUG_ON(!eie); 13663301958bSJan Schmidt while (eie->next) 13673301958bSJan Schmidt eie = eie->next; 13683301958bSJan Schmidt eie->next = ref->inode_list; 13693301958bSJan Schmidt } 1370f05c4746SWang Shilong eie = NULL; 13718da6d581SJan Schmidt } 13729dd14fd6SEdmund Nadolski cond_resched(); 13738da6d581SJan Schmidt } 13748da6d581SJan Schmidt 13758da6d581SJan Schmidt out: 13768da6d581SJan Schmidt btrfs_free_path(path); 137786d5f994SEdmund Nadolski 137886d5f994SEdmund Nadolski prelim_release(&preftrees.direct); 137986d5f994SEdmund Nadolski prelim_release(&preftrees.indirect); 138086d5f994SEdmund Nadolski prelim_release(&preftrees.indirect_missing_keys); 138186d5f994SEdmund Nadolski 1382f05c4746SWang Shilong if (ret < 0) 1383f05c4746SWang Shilong free_inode_elem_list(eie); 13848da6d581SJan Schmidt return ret; 13858da6d581SJan Schmidt } 13868da6d581SJan Schmidt 1387976b1908SJan Schmidt static void free_leaf_list(struct ulist *blocks) 1388976b1908SJan Schmidt { 1389976b1908SJan Schmidt struct ulist_node *node = NULL; 1390976b1908SJan Schmidt struct extent_inode_elem *eie; 1391976b1908SJan Schmidt struct ulist_iterator uiter; 1392976b1908SJan Schmidt 1393976b1908SJan Schmidt ULIST_ITER_INIT(&uiter); 1394976b1908SJan Schmidt while ((node = ulist_next(blocks, &uiter))) { 1395976b1908SJan Schmidt if (!node->aux) 1396976b1908SJan Schmidt continue; 13974dae077aSJeff Mahoney eie = unode_aux_to_inode_list(node); 1398f05c4746SWang Shilong free_inode_elem_list(eie); 1399976b1908SJan Schmidt node->aux = 0; 1400976b1908SJan Schmidt } 1401976b1908SJan Schmidt 1402976b1908SJan Schmidt ulist_free(blocks); 1403976b1908SJan Schmidt } 1404976b1908SJan Schmidt 14058da6d581SJan Schmidt /* 14068da6d581SJan Schmidt * Finds all leafs with a reference to the specified combination of bytenr and 14078da6d581SJan Schmidt * offset. key_list_head will point to a list of corresponding keys (caller must 14088da6d581SJan Schmidt * free each list element). The leafs will be stored in the leafs ulist, which 14098da6d581SJan Schmidt * must be freed with ulist_free. 14108da6d581SJan Schmidt * 14118da6d581SJan Schmidt * returns 0 on success, <0 on error 14128da6d581SJan Schmidt */ 14138da6d581SJan Schmidt static int btrfs_find_all_leafs(struct btrfs_trans_handle *trans, 14148da6d581SJan Schmidt struct btrfs_fs_info *fs_info, u64 bytenr, 1415097b8a7cSJan Schmidt u64 time_seq, struct ulist **leafs, 1416c995ab3cSZygo Blaxell const u64 *extent_item_pos, bool ignore_offset) 14178da6d581SJan Schmidt { 14188da6d581SJan Schmidt int ret; 14198da6d581SJan Schmidt 14208da6d581SJan Schmidt *leafs = ulist_alloc(GFP_NOFS); 142198cfee21SWang Shilong if (!*leafs) 14228da6d581SJan Schmidt return -ENOMEM; 14238da6d581SJan Schmidt 1424afce772eSLu Fengqi ret = find_parent_nodes(trans, fs_info, bytenr, time_seq, 1425c995ab3cSZygo Blaxell *leafs, NULL, extent_item_pos, NULL, ignore_offset); 14268da6d581SJan Schmidt if (ret < 0 && ret != -ENOENT) { 1427976b1908SJan Schmidt free_leaf_list(*leafs); 14288da6d581SJan Schmidt return ret; 14298da6d581SJan Schmidt } 14308da6d581SJan Schmidt 14318da6d581SJan Schmidt return 0; 14328da6d581SJan Schmidt } 14338da6d581SJan Schmidt 14348da6d581SJan Schmidt /* 14358da6d581SJan Schmidt * walk all backrefs for a given extent to find all roots that reference this 14368da6d581SJan Schmidt * extent. Walking a backref means finding all extents that reference this 14378da6d581SJan Schmidt * extent and in turn walk the backrefs of those, too. Naturally this is a 14388da6d581SJan Schmidt * recursive process, but here it is implemented in an iterative fashion: We 14398da6d581SJan Schmidt * find all referencing extents for the extent in question and put them on a 14408da6d581SJan Schmidt * list. In turn, we find all referencing extents for those, further appending 14418da6d581SJan Schmidt * to the list. The way we iterate the list allows adding more elements after 14428da6d581SJan Schmidt * the current while iterating. The process stops when we reach the end of the 14438da6d581SJan Schmidt * list. Found roots are added to the roots list. 14448da6d581SJan Schmidt * 14458da6d581SJan Schmidt * returns 0 on success, < 0 on error. 14468da6d581SJan Schmidt */ 1447e0c476b1SJeff Mahoney static int btrfs_find_all_roots_safe(struct btrfs_trans_handle *trans, 14488da6d581SJan Schmidt struct btrfs_fs_info *fs_info, u64 bytenr, 1449c995ab3cSZygo Blaxell u64 time_seq, struct ulist **roots, 1450c995ab3cSZygo Blaxell bool ignore_offset) 14518da6d581SJan Schmidt { 14528da6d581SJan Schmidt struct ulist *tmp; 14538da6d581SJan Schmidt struct ulist_node *node = NULL; 1454cd1b413cSJan Schmidt struct ulist_iterator uiter; 14558da6d581SJan Schmidt int ret; 14568da6d581SJan Schmidt 14578da6d581SJan Schmidt tmp = ulist_alloc(GFP_NOFS); 14588da6d581SJan Schmidt if (!tmp) 14598da6d581SJan Schmidt return -ENOMEM; 14608da6d581SJan Schmidt *roots = ulist_alloc(GFP_NOFS); 14618da6d581SJan Schmidt if (!*roots) { 14628da6d581SJan Schmidt ulist_free(tmp); 14638da6d581SJan Schmidt return -ENOMEM; 14648da6d581SJan Schmidt } 14658da6d581SJan Schmidt 1466cd1b413cSJan Schmidt ULIST_ITER_INIT(&uiter); 14678da6d581SJan Schmidt while (1) { 1468afce772eSLu Fengqi ret = find_parent_nodes(trans, fs_info, bytenr, time_seq, 1469c995ab3cSZygo Blaxell tmp, *roots, NULL, NULL, ignore_offset); 14708da6d581SJan Schmidt if (ret < 0 && ret != -ENOENT) { 14718da6d581SJan Schmidt ulist_free(tmp); 14728da6d581SJan Schmidt ulist_free(*roots); 14738da6d581SJan Schmidt return ret; 14748da6d581SJan Schmidt } 1475cd1b413cSJan Schmidt node = ulist_next(tmp, &uiter); 14768da6d581SJan Schmidt if (!node) 14778da6d581SJan Schmidt break; 14788da6d581SJan Schmidt bytenr = node->val; 1479bca1a290SWang Shilong cond_resched(); 14808da6d581SJan Schmidt } 14818da6d581SJan Schmidt 14828da6d581SJan Schmidt ulist_free(tmp); 14838da6d581SJan Schmidt return 0; 14848da6d581SJan Schmidt } 14858da6d581SJan Schmidt 14869e351cc8SJosef Bacik int btrfs_find_all_roots(struct btrfs_trans_handle *trans, 14879e351cc8SJosef Bacik struct btrfs_fs_info *fs_info, u64 bytenr, 1488c995ab3cSZygo Blaxell u64 time_seq, struct ulist **roots, 1489c995ab3cSZygo Blaxell bool ignore_offset) 14909e351cc8SJosef Bacik { 14919e351cc8SJosef Bacik int ret; 14929e351cc8SJosef Bacik 14939e351cc8SJosef Bacik if (!trans) 14949e351cc8SJosef Bacik down_read(&fs_info->commit_root_sem); 1495e0c476b1SJeff Mahoney ret = btrfs_find_all_roots_safe(trans, fs_info, bytenr, 1496c995ab3cSZygo Blaxell time_seq, roots, ignore_offset); 14979e351cc8SJosef Bacik if (!trans) 14989e351cc8SJosef Bacik up_read(&fs_info->commit_root_sem); 14999e351cc8SJosef Bacik return ret; 15009e351cc8SJosef Bacik } 15019e351cc8SJosef Bacik 15022c2ed5aaSMark Fasheh /** 15032c2ed5aaSMark Fasheh * btrfs_check_shared - tell us whether an extent is shared 15042c2ed5aaSMark Fasheh * 15052c2ed5aaSMark Fasheh * btrfs_check_shared uses the backref walking code but will short 15062c2ed5aaSMark Fasheh * circuit as soon as it finds a root or inode that doesn't match the 15072c2ed5aaSMark Fasheh * one passed in. This provides a significant performance benefit for 15082c2ed5aaSMark Fasheh * callers (such as fiemap) which want to know whether the extent is 15092c2ed5aaSMark Fasheh * shared but do not need a ref count. 15102c2ed5aaSMark Fasheh * 151103628cdbSFilipe Manana * This attempts to attach to the running transaction in order to account for 151203628cdbSFilipe Manana * delayed refs, but continues on even when no running transaction exists. 1513bb739cf0SEdmund Nadolski * 15142c2ed5aaSMark Fasheh * Return: 0 if extent is not shared, 1 if it is shared, < 0 on error. 15152c2ed5aaSMark Fasheh */ 15165911c8feSDavid Sterba int btrfs_check_shared(struct btrfs_root *root, u64 inum, u64 bytenr, 15175911c8feSDavid Sterba struct ulist *roots, struct ulist *tmp) 1518dc046b10SJosef Bacik { 1519bb739cf0SEdmund Nadolski struct btrfs_fs_info *fs_info = root->fs_info; 1520bb739cf0SEdmund Nadolski struct btrfs_trans_handle *trans; 1521dc046b10SJosef Bacik struct ulist_iterator uiter; 1522dc046b10SJosef Bacik struct ulist_node *node; 15233284da7bSDavid Sterba struct seq_list elem = SEQ_LIST_INIT(elem); 1524dc046b10SJosef Bacik int ret = 0; 15253ec4d323SEdmund Nadolski struct share_check shared = { 15264fd786e6SMisono Tomohiro .root_objectid = root->root_key.objectid, 15273ec4d323SEdmund Nadolski .inum = inum, 15283ec4d323SEdmund Nadolski .share_count = 0, 15293ec4d323SEdmund Nadolski }; 1530dc046b10SJosef Bacik 15315911c8feSDavid Sterba ulist_init(roots); 15325911c8feSDavid Sterba ulist_init(tmp); 1533dc046b10SJosef Bacik 1534a6d155d2SFilipe Manana trans = btrfs_join_transaction_nostart(root); 1535bb739cf0SEdmund Nadolski if (IS_ERR(trans)) { 153603628cdbSFilipe Manana if (PTR_ERR(trans) != -ENOENT && PTR_ERR(trans) != -EROFS) { 153703628cdbSFilipe Manana ret = PTR_ERR(trans); 153803628cdbSFilipe Manana goto out; 153903628cdbSFilipe Manana } 1540bb739cf0SEdmund Nadolski trans = NULL; 1541dc046b10SJosef Bacik down_read(&fs_info->commit_root_sem); 1542bb739cf0SEdmund Nadolski } else { 1543bb739cf0SEdmund Nadolski btrfs_get_tree_mod_seq(fs_info, &elem); 1544bb739cf0SEdmund Nadolski } 1545bb739cf0SEdmund Nadolski 1546dc046b10SJosef Bacik ULIST_ITER_INIT(&uiter); 1547dc046b10SJosef Bacik while (1) { 1548dc046b10SJosef Bacik ret = find_parent_nodes(trans, fs_info, bytenr, elem.seq, tmp, 1549c995ab3cSZygo Blaxell roots, NULL, &shared, false); 1550dc046b10SJosef Bacik if (ret == BACKREF_FOUND_SHARED) { 15512c2ed5aaSMark Fasheh /* this is the only condition under which we return 1 */ 1552dc046b10SJosef Bacik ret = 1; 1553dc046b10SJosef Bacik break; 1554dc046b10SJosef Bacik } 1555dc046b10SJosef Bacik if (ret < 0 && ret != -ENOENT) 1556dc046b10SJosef Bacik break; 15572c2ed5aaSMark Fasheh ret = 0; 1558dc046b10SJosef Bacik node = ulist_next(tmp, &uiter); 1559dc046b10SJosef Bacik if (!node) 1560dc046b10SJosef Bacik break; 1561dc046b10SJosef Bacik bytenr = node->val; 156218bf591bSEdmund Nadolski shared.share_count = 0; 1563dc046b10SJosef Bacik cond_resched(); 1564dc046b10SJosef Bacik } 1565bb739cf0SEdmund Nadolski 1566bb739cf0SEdmund Nadolski if (trans) { 1567dc046b10SJosef Bacik btrfs_put_tree_mod_seq(fs_info, &elem); 1568bb739cf0SEdmund Nadolski btrfs_end_transaction(trans); 1569bb739cf0SEdmund Nadolski } else { 1570dc046b10SJosef Bacik up_read(&fs_info->commit_root_sem); 1571bb739cf0SEdmund Nadolski } 157203628cdbSFilipe Manana out: 15735911c8feSDavid Sterba ulist_release(roots); 15745911c8feSDavid Sterba ulist_release(tmp); 1575dc046b10SJosef Bacik return ret; 1576dc046b10SJosef Bacik } 1577dc046b10SJosef Bacik 1578f186373fSMark Fasheh int btrfs_find_one_extref(struct btrfs_root *root, u64 inode_objectid, 1579f186373fSMark Fasheh u64 start_off, struct btrfs_path *path, 1580f186373fSMark Fasheh struct btrfs_inode_extref **ret_extref, 1581f186373fSMark Fasheh u64 *found_off) 1582f186373fSMark Fasheh { 1583f186373fSMark Fasheh int ret, slot; 1584f186373fSMark Fasheh struct btrfs_key key; 1585f186373fSMark Fasheh struct btrfs_key found_key; 1586f186373fSMark Fasheh struct btrfs_inode_extref *extref; 158773980becSJeff Mahoney const struct extent_buffer *leaf; 1588f186373fSMark Fasheh unsigned long ptr; 1589f186373fSMark Fasheh 1590f186373fSMark Fasheh key.objectid = inode_objectid; 1591962a298fSDavid Sterba key.type = BTRFS_INODE_EXTREF_KEY; 1592f186373fSMark Fasheh key.offset = start_off; 1593f186373fSMark Fasheh 1594f186373fSMark Fasheh ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); 1595f186373fSMark Fasheh if (ret < 0) 1596f186373fSMark Fasheh return ret; 1597f186373fSMark Fasheh 1598f186373fSMark Fasheh while (1) { 1599f186373fSMark Fasheh leaf = path->nodes[0]; 1600f186373fSMark Fasheh slot = path->slots[0]; 1601f186373fSMark Fasheh if (slot >= btrfs_header_nritems(leaf)) { 1602f186373fSMark Fasheh /* 1603f186373fSMark Fasheh * If the item at offset is not found, 1604f186373fSMark Fasheh * btrfs_search_slot will point us to the slot 1605f186373fSMark Fasheh * where it should be inserted. In our case 1606f186373fSMark Fasheh * that will be the slot directly before the 1607f186373fSMark Fasheh * next INODE_REF_KEY_V2 item. In the case 1608f186373fSMark Fasheh * that we're pointing to the last slot in a 1609f186373fSMark Fasheh * leaf, we must move one leaf over. 1610f186373fSMark Fasheh */ 1611f186373fSMark Fasheh ret = btrfs_next_leaf(root, path); 1612f186373fSMark Fasheh if (ret) { 1613f186373fSMark Fasheh if (ret >= 1) 1614f186373fSMark Fasheh ret = -ENOENT; 1615f186373fSMark Fasheh break; 1616f186373fSMark Fasheh } 1617f186373fSMark Fasheh continue; 1618f186373fSMark Fasheh } 1619f186373fSMark Fasheh 1620f186373fSMark Fasheh btrfs_item_key_to_cpu(leaf, &found_key, slot); 1621f186373fSMark Fasheh 1622f186373fSMark Fasheh /* 1623f186373fSMark Fasheh * Check that we're still looking at an extended ref key for 1624f186373fSMark Fasheh * this particular objectid. If we have different 1625f186373fSMark Fasheh * objectid or type then there are no more to be found 1626f186373fSMark Fasheh * in the tree and we can exit. 1627f186373fSMark Fasheh */ 1628f186373fSMark Fasheh ret = -ENOENT; 1629f186373fSMark Fasheh if (found_key.objectid != inode_objectid) 1630f186373fSMark Fasheh break; 1631962a298fSDavid Sterba if (found_key.type != BTRFS_INODE_EXTREF_KEY) 1632f186373fSMark Fasheh break; 1633f186373fSMark Fasheh 1634f186373fSMark Fasheh ret = 0; 1635f186373fSMark Fasheh ptr = btrfs_item_ptr_offset(leaf, path->slots[0]); 1636f186373fSMark Fasheh extref = (struct btrfs_inode_extref *)ptr; 1637f186373fSMark Fasheh *ret_extref = extref; 1638f186373fSMark Fasheh if (found_off) 1639f186373fSMark Fasheh *found_off = found_key.offset; 1640f186373fSMark Fasheh break; 1641f186373fSMark Fasheh } 1642f186373fSMark Fasheh 1643f186373fSMark Fasheh return ret; 1644f186373fSMark Fasheh } 1645f186373fSMark Fasheh 164648a3b636SEric Sandeen /* 164748a3b636SEric Sandeen * this iterates to turn a name (from iref/extref) into a full filesystem path. 164848a3b636SEric Sandeen * Elements of the path are separated by '/' and the path is guaranteed to be 164948a3b636SEric Sandeen * 0-terminated. the path is only given within the current file system. 165048a3b636SEric Sandeen * Therefore, it never starts with a '/'. the caller is responsible to provide 165148a3b636SEric Sandeen * "size" bytes in "dest". the dest buffer will be filled backwards. finally, 165248a3b636SEric Sandeen * the start point of the resulting string is returned. this pointer is within 165348a3b636SEric Sandeen * dest, normally. 165448a3b636SEric Sandeen * in case the path buffer would overflow, the pointer is decremented further 165548a3b636SEric Sandeen * as if output was written to the buffer, though no more output is actually 165648a3b636SEric Sandeen * generated. that way, the caller can determine how much space would be 165748a3b636SEric Sandeen * required for the path to fit into the buffer. in that case, the returned 165848a3b636SEric Sandeen * value will be smaller than dest. callers must check this! 165948a3b636SEric Sandeen */ 166096b5bd77SJan Schmidt char *btrfs_ref_to_path(struct btrfs_root *fs_root, struct btrfs_path *path, 1661d24bec3aSMark Fasheh u32 name_len, unsigned long name_off, 1662a542ad1bSJan Schmidt struct extent_buffer *eb_in, u64 parent, 1663a542ad1bSJan Schmidt char *dest, u32 size) 1664a542ad1bSJan Schmidt { 1665a542ad1bSJan Schmidt int slot; 1666a542ad1bSJan Schmidt u64 next_inum; 1667a542ad1bSJan Schmidt int ret; 1668661bec6bSGabriel de Perthuis s64 bytes_left = ((s64)size) - 1; 1669a542ad1bSJan Schmidt struct extent_buffer *eb = eb_in; 1670a542ad1bSJan Schmidt struct btrfs_key found_key; 1671b916a59aSJan Schmidt int leave_spinning = path->leave_spinning; 1672d24bec3aSMark Fasheh struct btrfs_inode_ref *iref; 1673a542ad1bSJan Schmidt 1674a542ad1bSJan Schmidt if (bytes_left >= 0) 1675a542ad1bSJan Schmidt dest[bytes_left] = '\0'; 1676a542ad1bSJan Schmidt 1677b916a59aSJan Schmidt path->leave_spinning = 1; 1678a542ad1bSJan Schmidt while (1) { 1679d24bec3aSMark Fasheh bytes_left -= name_len; 1680a542ad1bSJan Schmidt if (bytes_left >= 0) 1681a542ad1bSJan Schmidt read_extent_buffer(eb, dest + bytes_left, 1682d24bec3aSMark Fasheh name_off, name_len); 1683b916a59aSJan Schmidt if (eb != eb_in) { 16840c0fe3b0SFilipe Manana if (!path->skip_locking) 1685b916a59aSJan Schmidt btrfs_tree_read_unlock_blocking(eb); 1686a542ad1bSJan Schmidt free_extent_buffer(eb); 1687b916a59aSJan Schmidt } 1688c234a24dSDavid Sterba ret = btrfs_find_item(fs_root, path, parent, 0, 1689c234a24dSDavid Sterba BTRFS_INODE_REF_KEY, &found_key); 16908f24b496SJan Schmidt if (ret > 0) 16918f24b496SJan Schmidt ret = -ENOENT; 1692a542ad1bSJan Schmidt if (ret) 1693a542ad1bSJan Schmidt break; 1694d24bec3aSMark Fasheh 1695a542ad1bSJan Schmidt next_inum = found_key.offset; 1696a542ad1bSJan Schmidt 1697a542ad1bSJan Schmidt /* regular exit ahead */ 1698a542ad1bSJan Schmidt if (parent == next_inum) 1699a542ad1bSJan Schmidt break; 1700a542ad1bSJan Schmidt 1701a542ad1bSJan Schmidt slot = path->slots[0]; 1702a542ad1bSJan Schmidt eb = path->nodes[0]; 1703a542ad1bSJan Schmidt /* make sure we can use eb after releasing the path */ 1704b916a59aSJan Schmidt if (eb != eb_in) { 17050c0fe3b0SFilipe Manana if (!path->skip_locking) 1706300aa896SDavid Sterba btrfs_set_lock_blocking_read(eb); 17070c0fe3b0SFilipe Manana path->nodes[0] = NULL; 17080c0fe3b0SFilipe Manana path->locks[0] = 0; 1709b916a59aSJan Schmidt } 1710a542ad1bSJan Schmidt btrfs_release_path(path); 1711a542ad1bSJan Schmidt iref = btrfs_item_ptr(eb, slot, struct btrfs_inode_ref); 1712d24bec3aSMark Fasheh 1713d24bec3aSMark Fasheh name_len = btrfs_inode_ref_name_len(eb, iref); 1714d24bec3aSMark Fasheh name_off = (unsigned long)(iref + 1); 1715d24bec3aSMark Fasheh 1716a542ad1bSJan Schmidt parent = next_inum; 1717a542ad1bSJan Schmidt --bytes_left; 1718a542ad1bSJan Schmidt if (bytes_left >= 0) 1719a542ad1bSJan Schmidt dest[bytes_left] = '/'; 1720a542ad1bSJan Schmidt } 1721a542ad1bSJan Schmidt 1722a542ad1bSJan Schmidt btrfs_release_path(path); 1723b916a59aSJan Schmidt path->leave_spinning = leave_spinning; 1724a542ad1bSJan Schmidt 1725a542ad1bSJan Schmidt if (ret) 1726a542ad1bSJan Schmidt return ERR_PTR(ret); 1727a542ad1bSJan Schmidt 1728a542ad1bSJan Schmidt return dest + bytes_left; 1729a542ad1bSJan Schmidt } 1730a542ad1bSJan Schmidt 1731a542ad1bSJan Schmidt /* 1732a542ad1bSJan Schmidt * this makes the path point to (logical EXTENT_ITEM *) 1733a542ad1bSJan Schmidt * returns BTRFS_EXTENT_FLAG_DATA for data, BTRFS_EXTENT_FLAG_TREE_BLOCK for 1734a542ad1bSJan Schmidt * tree blocks and <0 on error. 1735a542ad1bSJan Schmidt */ 1736a542ad1bSJan Schmidt int extent_from_logical(struct btrfs_fs_info *fs_info, u64 logical, 173769917e43SLiu Bo struct btrfs_path *path, struct btrfs_key *found_key, 173869917e43SLiu Bo u64 *flags_ret) 1739a542ad1bSJan Schmidt { 1740a542ad1bSJan Schmidt int ret; 1741a542ad1bSJan Schmidt u64 flags; 1742261c84b6SJosef Bacik u64 size = 0; 1743a542ad1bSJan Schmidt u32 item_size; 174473980becSJeff Mahoney const struct extent_buffer *eb; 1745a542ad1bSJan Schmidt struct btrfs_extent_item *ei; 1746a542ad1bSJan Schmidt struct btrfs_key key; 1747a542ad1bSJan Schmidt 1748261c84b6SJosef Bacik if (btrfs_fs_incompat(fs_info, SKINNY_METADATA)) 1749261c84b6SJosef Bacik key.type = BTRFS_METADATA_ITEM_KEY; 1750261c84b6SJosef Bacik else 1751a542ad1bSJan Schmidt key.type = BTRFS_EXTENT_ITEM_KEY; 1752a542ad1bSJan Schmidt key.objectid = logical; 1753a542ad1bSJan Schmidt key.offset = (u64)-1; 1754a542ad1bSJan Schmidt 1755a542ad1bSJan Schmidt ret = btrfs_search_slot(NULL, fs_info->extent_root, &key, path, 0, 0); 1756a542ad1bSJan Schmidt if (ret < 0) 1757a542ad1bSJan Schmidt return ret; 1758a542ad1bSJan Schmidt 1759850a8cdfSWang Shilong ret = btrfs_previous_extent_item(fs_info->extent_root, path, 0); 1760850a8cdfSWang Shilong if (ret) { 1761850a8cdfSWang Shilong if (ret > 0) 1762580f0a67SJosef Bacik ret = -ENOENT; 1763580f0a67SJosef Bacik return ret; 1764580f0a67SJosef Bacik } 1765850a8cdfSWang Shilong btrfs_item_key_to_cpu(path->nodes[0], found_key, path->slots[0]); 1766261c84b6SJosef Bacik if (found_key->type == BTRFS_METADATA_ITEM_KEY) 1767da17066cSJeff Mahoney size = fs_info->nodesize; 1768261c84b6SJosef Bacik else if (found_key->type == BTRFS_EXTENT_ITEM_KEY) 1769261c84b6SJosef Bacik size = found_key->offset; 1770261c84b6SJosef Bacik 1771580f0a67SJosef Bacik if (found_key->objectid > logical || 1772261c84b6SJosef Bacik found_key->objectid + size <= logical) { 1773ab8d0fc4SJeff Mahoney btrfs_debug(fs_info, 1774ab8d0fc4SJeff Mahoney "logical %llu is not within any extent", logical); 1775a542ad1bSJan Schmidt return -ENOENT; 17764692cf58SJan Schmidt } 1777a542ad1bSJan Schmidt 1778a542ad1bSJan Schmidt eb = path->nodes[0]; 1779a542ad1bSJan Schmidt item_size = btrfs_item_size_nr(eb, path->slots[0]); 1780a542ad1bSJan Schmidt BUG_ON(item_size < sizeof(*ei)); 1781a542ad1bSJan Schmidt 1782a542ad1bSJan Schmidt ei = btrfs_item_ptr(eb, path->slots[0], struct btrfs_extent_item); 1783a542ad1bSJan Schmidt flags = btrfs_extent_flags(eb, ei); 1784a542ad1bSJan Schmidt 1785ab8d0fc4SJeff Mahoney btrfs_debug(fs_info, 1786ab8d0fc4SJeff Mahoney "logical %llu is at position %llu within the extent (%llu EXTENT_ITEM %llu) flags %#llx size %u", 1787c1c9ff7cSGeert Uytterhoeven logical, logical - found_key->objectid, found_key->objectid, 1788c1c9ff7cSGeert Uytterhoeven found_key->offset, flags, item_size); 178969917e43SLiu Bo 179069917e43SLiu Bo WARN_ON(!flags_ret); 179169917e43SLiu Bo if (flags_ret) { 1792a542ad1bSJan Schmidt if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) 179369917e43SLiu Bo *flags_ret = BTRFS_EXTENT_FLAG_TREE_BLOCK; 179469917e43SLiu Bo else if (flags & BTRFS_EXTENT_FLAG_DATA) 179569917e43SLiu Bo *flags_ret = BTRFS_EXTENT_FLAG_DATA; 179669917e43SLiu Bo else 1797290342f6SArnd Bergmann BUG(); 179869917e43SLiu Bo return 0; 179969917e43SLiu Bo } 1800a542ad1bSJan Schmidt 1801a542ad1bSJan Schmidt return -EIO; 1802a542ad1bSJan Schmidt } 1803a542ad1bSJan Schmidt 1804a542ad1bSJan Schmidt /* 1805a542ad1bSJan Schmidt * helper function to iterate extent inline refs. ptr must point to a 0 value 1806a542ad1bSJan Schmidt * for the first call and may be modified. it is used to track state. 1807a542ad1bSJan Schmidt * if more refs exist, 0 is returned and the next call to 1808e0c476b1SJeff Mahoney * get_extent_inline_ref must pass the modified ptr parameter to get the 1809a542ad1bSJan Schmidt * next ref. after the last ref was processed, 1 is returned. 1810a542ad1bSJan Schmidt * returns <0 on error 1811a542ad1bSJan Schmidt */ 1812e0c476b1SJeff Mahoney static int get_extent_inline_ref(unsigned long *ptr, 181373980becSJeff Mahoney const struct extent_buffer *eb, 181473980becSJeff Mahoney const struct btrfs_key *key, 181573980becSJeff Mahoney const struct btrfs_extent_item *ei, 181673980becSJeff Mahoney u32 item_size, 1817a542ad1bSJan Schmidt struct btrfs_extent_inline_ref **out_eiref, 1818a542ad1bSJan Schmidt int *out_type) 1819a542ad1bSJan Schmidt { 1820a542ad1bSJan Schmidt unsigned long end; 1821a542ad1bSJan Schmidt u64 flags; 1822a542ad1bSJan Schmidt struct btrfs_tree_block_info *info; 1823a542ad1bSJan Schmidt 1824a542ad1bSJan Schmidt if (!*ptr) { 1825a542ad1bSJan Schmidt /* first call */ 1826a542ad1bSJan Schmidt flags = btrfs_extent_flags(eb, ei); 1827a542ad1bSJan Schmidt if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) { 18286eda71d0SLiu Bo if (key->type == BTRFS_METADATA_ITEM_KEY) { 18296eda71d0SLiu Bo /* a skinny metadata extent */ 18306eda71d0SLiu Bo *out_eiref = 18316eda71d0SLiu Bo (struct btrfs_extent_inline_ref *)(ei + 1); 18326eda71d0SLiu Bo } else { 18336eda71d0SLiu Bo WARN_ON(key->type != BTRFS_EXTENT_ITEM_KEY); 1834a542ad1bSJan Schmidt info = (struct btrfs_tree_block_info *)(ei + 1); 1835a542ad1bSJan Schmidt *out_eiref = 1836a542ad1bSJan Schmidt (struct btrfs_extent_inline_ref *)(info + 1); 18376eda71d0SLiu Bo } 1838a542ad1bSJan Schmidt } else { 1839a542ad1bSJan Schmidt *out_eiref = (struct btrfs_extent_inline_ref *)(ei + 1); 1840a542ad1bSJan Schmidt } 1841a542ad1bSJan Schmidt *ptr = (unsigned long)*out_eiref; 1842cd857dd6SLiu Bo if ((unsigned long)(*ptr) >= (unsigned long)ei + item_size) 1843a542ad1bSJan Schmidt return -ENOENT; 1844a542ad1bSJan Schmidt } 1845a542ad1bSJan Schmidt 1846a542ad1bSJan Schmidt end = (unsigned long)ei + item_size; 18476eda71d0SLiu Bo *out_eiref = (struct btrfs_extent_inline_ref *)(*ptr); 18483de28d57SLiu Bo *out_type = btrfs_get_extent_inline_ref_type(eb, *out_eiref, 18493de28d57SLiu Bo BTRFS_REF_TYPE_ANY); 18503de28d57SLiu Bo if (*out_type == BTRFS_REF_TYPE_INVALID) 1851af431dcbSSu Yue return -EUCLEAN; 1852a542ad1bSJan Schmidt 1853a542ad1bSJan Schmidt *ptr += btrfs_extent_inline_ref_size(*out_type); 1854a542ad1bSJan Schmidt WARN_ON(*ptr > end); 1855a542ad1bSJan Schmidt if (*ptr == end) 1856a542ad1bSJan Schmidt return 1; /* last */ 1857a542ad1bSJan Schmidt 1858a542ad1bSJan Schmidt return 0; 1859a542ad1bSJan Schmidt } 1860a542ad1bSJan Schmidt 1861a542ad1bSJan Schmidt /* 1862a542ad1bSJan Schmidt * reads the tree block backref for an extent. tree level and root are returned 1863a542ad1bSJan Schmidt * through out_level and out_root. ptr must point to a 0 value for the first 1864e0c476b1SJeff Mahoney * call and may be modified (see get_extent_inline_ref comment). 1865a542ad1bSJan Schmidt * returns 0 if data was provided, 1 if there was no more data to provide or 1866a542ad1bSJan Schmidt * <0 on error. 1867a542ad1bSJan Schmidt */ 1868a542ad1bSJan Schmidt int tree_backref_for_extent(unsigned long *ptr, struct extent_buffer *eb, 18696eda71d0SLiu Bo struct btrfs_key *key, struct btrfs_extent_item *ei, 18706eda71d0SLiu Bo u32 item_size, u64 *out_root, u8 *out_level) 1871a542ad1bSJan Schmidt { 1872a542ad1bSJan Schmidt int ret; 1873a542ad1bSJan Schmidt int type; 1874a542ad1bSJan Schmidt struct btrfs_extent_inline_ref *eiref; 1875a542ad1bSJan Schmidt 1876a542ad1bSJan Schmidt if (*ptr == (unsigned long)-1) 1877a542ad1bSJan Schmidt return 1; 1878a542ad1bSJan Schmidt 1879a542ad1bSJan Schmidt while (1) { 1880e0c476b1SJeff Mahoney ret = get_extent_inline_ref(ptr, eb, key, ei, item_size, 1881a542ad1bSJan Schmidt &eiref, &type); 1882a542ad1bSJan Schmidt if (ret < 0) 1883a542ad1bSJan Schmidt return ret; 1884a542ad1bSJan Schmidt 1885a542ad1bSJan Schmidt if (type == BTRFS_TREE_BLOCK_REF_KEY || 1886a542ad1bSJan Schmidt type == BTRFS_SHARED_BLOCK_REF_KEY) 1887a542ad1bSJan Schmidt break; 1888a542ad1bSJan Schmidt 1889a542ad1bSJan Schmidt if (ret == 1) 1890a542ad1bSJan Schmidt return 1; 1891a542ad1bSJan Schmidt } 1892a542ad1bSJan Schmidt 1893a542ad1bSJan Schmidt /* we can treat both ref types equally here */ 1894a542ad1bSJan Schmidt *out_root = btrfs_extent_inline_ref_offset(eb, eiref); 1895a1317f45SFilipe Manana 1896a1317f45SFilipe Manana if (key->type == BTRFS_EXTENT_ITEM_KEY) { 1897a1317f45SFilipe Manana struct btrfs_tree_block_info *info; 1898a1317f45SFilipe Manana 1899a1317f45SFilipe Manana info = (struct btrfs_tree_block_info *)(ei + 1); 1900a542ad1bSJan Schmidt *out_level = btrfs_tree_block_level(eb, info); 1901a1317f45SFilipe Manana } else { 1902a1317f45SFilipe Manana ASSERT(key->type == BTRFS_METADATA_ITEM_KEY); 1903a1317f45SFilipe Manana *out_level = (u8)key->offset; 1904a1317f45SFilipe Manana } 1905a542ad1bSJan Schmidt 1906a542ad1bSJan Schmidt if (ret == 1) 1907a542ad1bSJan Schmidt *ptr = (unsigned long)-1; 1908a542ad1bSJan Schmidt 1909a542ad1bSJan Schmidt return 0; 1910a542ad1bSJan Schmidt } 1911a542ad1bSJan Schmidt 1912ab8d0fc4SJeff Mahoney static int iterate_leaf_refs(struct btrfs_fs_info *fs_info, 1913ab8d0fc4SJeff Mahoney struct extent_inode_elem *inode_list, 1914976b1908SJan Schmidt u64 root, u64 extent_item_objectid, 19154692cf58SJan Schmidt iterate_extent_inodes_t *iterate, void *ctx) 1916a542ad1bSJan Schmidt { 1917976b1908SJan Schmidt struct extent_inode_elem *eie; 19184692cf58SJan Schmidt int ret = 0; 1919a542ad1bSJan Schmidt 1920976b1908SJan Schmidt for (eie = inode_list; eie; eie = eie->next) { 1921ab8d0fc4SJeff Mahoney btrfs_debug(fs_info, 1922ab8d0fc4SJeff Mahoney "ref for %llu resolved, key (%llu EXTEND_DATA %llu), root %llu", 1923ab8d0fc4SJeff Mahoney extent_item_objectid, eie->inum, 1924ab8d0fc4SJeff Mahoney eie->offset, root); 1925976b1908SJan Schmidt ret = iterate(eie->inum, eie->offset, root, ctx); 19264692cf58SJan Schmidt if (ret) { 1927ab8d0fc4SJeff Mahoney btrfs_debug(fs_info, 1928ab8d0fc4SJeff Mahoney "stopping iteration for %llu due to ret=%d", 1929976b1908SJan Schmidt extent_item_objectid, ret); 1930a542ad1bSJan Schmidt break; 1931a542ad1bSJan Schmidt } 1932a542ad1bSJan Schmidt } 1933a542ad1bSJan Schmidt 1934a542ad1bSJan Schmidt return ret; 1935a542ad1bSJan Schmidt } 1936a542ad1bSJan Schmidt 1937a542ad1bSJan Schmidt /* 1938a542ad1bSJan Schmidt * calls iterate() for every inode that references the extent identified by 19394692cf58SJan Schmidt * the given parameters. 1940a542ad1bSJan Schmidt * when the iterator function returns a non-zero value, iteration stops. 1941a542ad1bSJan Schmidt */ 1942a542ad1bSJan Schmidt int iterate_extent_inodes(struct btrfs_fs_info *fs_info, 19434692cf58SJan Schmidt u64 extent_item_objectid, u64 extent_item_pos, 19447a3ae2f8SJan Schmidt int search_commit_root, 1945c995ab3cSZygo Blaxell iterate_extent_inodes_t *iterate, void *ctx, 1946c995ab3cSZygo Blaxell bool ignore_offset) 1947a542ad1bSJan Schmidt { 1948a542ad1bSJan Schmidt int ret; 1949da61d31aSJosef Bacik struct btrfs_trans_handle *trans = NULL; 19507a3ae2f8SJan Schmidt struct ulist *refs = NULL; 19517a3ae2f8SJan Schmidt struct ulist *roots = NULL; 19524692cf58SJan Schmidt struct ulist_node *ref_node = NULL; 19534692cf58SJan Schmidt struct ulist_node *root_node = NULL; 19543284da7bSDavid Sterba struct seq_list tree_mod_seq_elem = SEQ_LIST_INIT(tree_mod_seq_elem); 1955cd1b413cSJan Schmidt struct ulist_iterator ref_uiter; 1956cd1b413cSJan Schmidt struct ulist_iterator root_uiter; 1957a542ad1bSJan Schmidt 1958ab8d0fc4SJeff Mahoney btrfs_debug(fs_info, "resolving all inodes for extent %llu", 19594692cf58SJan Schmidt extent_item_objectid); 19604692cf58SJan Schmidt 1961da61d31aSJosef Bacik if (!search_commit_root) { 1962bfc61c36SFilipe Manana trans = btrfs_attach_transaction(fs_info->extent_root); 1963bfc61c36SFilipe Manana if (IS_ERR(trans)) { 1964bfc61c36SFilipe Manana if (PTR_ERR(trans) != -ENOENT && 1965bfc61c36SFilipe Manana PTR_ERR(trans) != -EROFS) 19667a3ae2f8SJan Schmidt return PTR_ERR(trans); 1967bfc61c36SFilipe Manana trans = NULL; 19687a3ae2f8SJan Schmidt } 1969bfc61c36SFilipe Manana } 1970bfc61c36SFilipe Manana 1971bfc61c36SFilipe Manana if (trans) 1972bfc61c36SFilipe Manana btrfs_get_tree_mod_seq(fs_info, &tree_mod_seq_elem); 1973bfc61c36SFilipe Manana else 1974bfc61c36SFilipe Manana down_read(&fs_info->commit_root_sem); 19754692cf58SJan Schmidt 19764692cf58SJan Schmidt ret = btrfs_find_all_leafs(trans, fs_info, extent_item_objectid, 1977097b8a7cSJan Schmidt tree_mod_seq_elem.seq, &refs, 1978c995ab3cSZygo Blaxell &extent_item_pos, ignore_offset); 19794692cf58SJan Schmidt if (ret) 19804692cf58SJan Schmidt goto out; 19814692cf58SJan Schmidt 1982cd1b413cSJan Schmidt ULIST_ITER_INIT(&ref_uiter); 1983cd1b413cSJan Schmidt while (!ret && (ref_node = ulist_next(refs, &ref_uiter))) { 1984e0c476b1SJeff Mahoney ret = btrfs_find_all_roots_safe(trans, fs_info, ref_node->val, 1985c995ab3cSZygo Blaxell tree_mod_seq_elem.seq, &roots, 1986c995ab3cSZygo Blaxell ignore_offset); 19874692cf58SJan Schmidt if (ret) 1988a542ad1bSJan Schmidt break; 1989cd1b413cSJan Schmidt ULIST_ITER_INIT(&root_uiter); 1990cd1b413cSJan Schmidt while (!ret && (root_node = ulist_next(roots, &root_uiter))) { 1991ab8d0fc4SJeff Mahoney btrfs_debug(fs_info, 1992ab8d0fc4SJeff Mahoney "root %llu references leaf %llu, data list %#llx", 1993ab8d0fc4SJeff Mahoney root_node->val, ref_node->val, 1994c1c9ff7cSGeert Uytterhoeven ref_node->aux); 1995ab8d0fc4SJeff Mahoney ret = iterate_leaf_refs(fs_info, 1996ab8d0fc4SJeff Mahoney (struct extent_inode_elem *) 1997995e01b7SJan Schmidt (uintptr_t)ref_node->aux, 1998995e01b7SJan Schmidt root_node->val, 1999995e01b7SJan Schmidt extent_item_objectid, 2000a542ad1bSJan Schmidt iterate, ctx); 20014692cf58SJan Schmidt } 2002976b1908SJan Schmidt ulist_free(roots); 2003a542ad1bSJan Schmidt } 2004a542ad1bSJan Schmidt 2005976b1908SJan Schmidt free_leaf_list(refs); 20064692cf58SJan Schmidt out: 2007bfc61c36SFilipe Manana if (trans) { 20088445f61cSJan Schmidt btrfs_put_tree_mod_seq(fs_info, &tree_mod_seq_elem); 20093a45bb20SJeff Mahoney btrfs_end_transaction(trans); 20109e351cc8SJosef Bacik } else { 20119e351cc8SJosef Bacik up_read(&fs_info->commit_root_sem); 20127a3ae2f8SJan Schmidt } 20137a3ae2f8SJan Schmidt 2014a542ad1bSJan Schmidt return ret; 2015a542ad1bSJan Schmidt } 2016a542ad1bSJan Schmidt 2017a542ad1bSJan Schmidt int iterate_inodes_from_logical(u64 logical, struct btrfs_fs_info *fs_info, 2018a542ad1bSJan Schmidt struct btrfs_path *path, 2019c995ab3cSZygo Blaxell iterate_extent_inodes_t *iterate, void *ctx, 2020c995ab3cSZygo Blaxell bool ignore_offset) 2021a542ad1bSJan Schmidt { 2022a542ad1bSJan Schmidt int ret; 20234692cf58SJan Schmidt u64 extent_item_pos; 202469917e43SLiu Bo u64 flags = 0; 2025a542ad1bSJan Schmidt struct btrfs_key found_key; 20267a3ae2f8SJan Schmidt int search_commit_root = path->search_commit_root; 2027a542ad1bSJan Schmidt 202869917e43SLiu Bo ret = extent_from_logical(fs_info, logical, path, &found_key, &flags); 20294692cf58SJan Schmidt btrfs_release_path(path); 2030a542ad1bSJan Schmidt if (ret < 0) 2031a542ad1bSJan Schmidt return ret; 203269917e43SLiu Bo if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) 20333627bf45SStefan Behrens return -EINVAL; 2034a542ad1bSJan Schmidt 20354692cf58SJan Schmidt extent_item_pos = logical - found_key.objectid; 20367a3ae2f8SJan Schmidt ret = iterate_extent_inodes(fs_info, found_key.objectid, 20377a3ae2f8SJan Schmidt extent_item_pos, search_commit_root, 2038c995ab3cSZygo Blaxell iterate, ctx, ignore_offset); 2039a542ad1bSJan Schmidt 2040a542ad1bSJan Schmidt return ret; 2041a542ad1bSJan Schmidt } 2042a542ad1bSJan Schmidt 2043d24bec3aSMark Fasheh typedef int (iterate_irefs_t)(u64 parent, u32 name_len, unsigned long name_off, 2044d24bec3aSMark Fasheh struct extent_buffer *eb, void *ctx); 2045d24bec3aSMark Fasheh 2046d24bec3aSMark Fasheh static int iterate_inode_refs(u64 inum, struct btrfs_root *fs_root, 2047a542ad1bSJan Schmidt struct btrfs_path *path, 2048a542ad1bSJan Schmidt iterate_irefs_t *iterate, void *ctx) 2049a542ad1bSJan Schmidt { 2050aefc1eb1SJan Schmidt int ret = 0; 2051a542ad1bSJan Schmidt int slot; 2052a542ad1bSJan Schmidt u32 cur; 2053a542ad1bSJan Schmidt u32 len; 2054a542ad1bSJan Schmidt u32 name_len; 2055a542ad1bSJan Schmidt u64 parent = 0; 2056a542ad1bSJan Schmidt int found = 0; 2057a542ad1bSJan Schmidt struct extent_buffer *eb; 2058a542ad1bSJan Schmidt struct btrfs_item *item; 2059a542ad1bSJan Schmidt struct btrfs_inode_ref *iref; 2060a542ad1bSJan Schmidt struct btrfs_key found_key; 2061a542ad1bSJan Schmidt 2062aefc1eb1SJan Schmidt while (!ret) { 2063c234a24dSDavid Sterba ret = btrfs_find_item(fs_root, path, inum, 2064c234a24dSDavid Sterba parent ? parent + 1 : 0, BTRFS_INODE_REF_KEY, 2065a542ad1bSJan Schmidt &found_key); 2066c234a24dSDavid Sterba 2067a542ad1bSJan Schmidt if (ret < 0) 2068a542ad1bSJan Schmidt break; 2069a542ad1bSJan Schmidt if (ret) { 2070a542ad1bSJan Schmidt ret = found ? 0 : -ENOENT; 2071a542ad1bSJan Schmidt break; 2072a542ad1bSJan Schmidt } 2073a542ad1bSJan Schmidt ++found; 2074a542ad1bSJan Schmidt 2075a542ad1bSJan Schmidt parent = found_key.offset; 2076a542ad1bSJan Schmidt slot = path->slots[0]; 20773fe81ce2SFilipe David Borba Manana eb = btrfs_clone_extent_buffer(path->nodes[0]); 20783fe81ce2SFilipe David Borba Manana if (!eb) { 20793fe81ce2SFilipe David Borba Manana ret = -ENOMEM; 20803fe81ce2SFilipe David Borba Manana break; 20813fe81ce2SFilipe David Borba Manana } 2082a542ad1bSJan Schmidt btrfs_release_path(path); 2083a542ad1bSJan Schmidt 2084dd3cc16bSRoss Kirk item = btrfs_item_nr(slot); 2085a542ad1bSJan Schmidt iref = btrfs_item_ptr(eb, slot, struct btrfs_inode_ref); 2086a542ad1bSJan Schmidt 2087a542ad1bSJan Schmidt for (cur = 0; cur < btrfs_item_size(eb, item); cur += len) { 2088a542ad1bSJan Schmidt name_len = btrfs_inode_ref_name_len(eb, iref); 2089a542ad1bSJan Schmidt /* path must be released before calling iterate()! */ 2090ab8d0fc4SJeff Mahoney btrfs_debug(fs_root->fs_info, 2091ab8d0fc4SJeff Mahoney "following ref at offset %u for inode %llu in tree %llu", 20924fd786e6SMisono Tomohiro cur, found_key.objectid, 20934fd786e6SMisono Tomohiro fs_root->root_key.objectid); 2094d24bec3aSMark Fasheh ret = iterate(parent, name_len, 2095d24bec3aSMark Fasheh (unsigned long)(iref + 1), eb, ctx); 2096aefc1eb1SJan Schmidt if (ret) 2097a542ad1bSJan Schmidt break; 2098a542ad1bSJan Schmidt len = sizeof(*iref) + name_len; 2099a542ad1bSJan Schmidt iref = (struct btrfs_inode_ref *)((char *)iref + len); 2100a542ad1bSJan Schmidt } 2101a542ad1bSJan Schmidt free_extent_buffer(eb); 2102a542ad1bSJan Schmidt } 2103a542ad1bSJan Schmidt 2104a542ad1bSJan Schmidt btrfs_release_path(path); 2105a542ad1bSJan Schmidt 2106a542ad1bSJan Schmidt return ret; 2107a542ad1bSJan Schmidt } 2108a542ad1bSJan Schmidt 2109d24bec3aSMark Fasheh static int iterate_inode_extrefs(u64 inum, struct btrfs_root *fs_root, 2110d24bec3aSMark Fasheh struct btrfs_path *path, 2111d24bec3aSMark Fasheh iterate_irefs_t *iterate, void *ctx) 2112d24bec3aSMark Fasheh { 2113d24bec3aSMark Fasheh int ret; 2114d24bec3aSMark Fasheh int slot; 2115d24bec3aSMark Fasheh u64 offset = 0; 2116d24bec3aSMark Fasheh u64 parent; 2117d24bec3aSMark Fasheh int found = 0; 2118d24bec3aSMark Fasheh struct extent_buffer *eb; 2119d24bec3aSMark Fasheh struct btrfs_inode_extref *extref; 2120d24bec3aSMark Fasheh u32 item_size; 2121d24bec3aSMark Fasheh u32 cur_offset; 2122d24bec3aSMark Fasheh unsigned long ptr; 2123d24bec3aSMark Fasheh 2124d24bec3aSMark Fasheh while (1) { 2125d24bec3aSMark Fasheh ret = btrfs_find_one_extref(fs_root, inum, offset, path, &extref, 2126d24bec3aSMark Fasheh &offset); 2127d24bec3aSMark Fasheh if (ret < 0) 2128d24bec3aSMark Fasheh break; 2129d24bec3aSMark Fasheh if (ret) { 2130d24bec3aSMark Fasheh ret = found ? 0 : -ENOENT; 2131d24bec3aSMark Fasheh break; 2132d24bec3aSMark Fasheh } 2133d24bec3aSMark Fasheh ++found; 2134d24bec3aSMark Fasheh 2135d24bec3aSMark Fasheh slot = path->slots[0]; 21363fe81ce2SFilipe David Borba Manana eb = btrfs_clone_extent_buffer(path->nodes[0]); 21373fe81ce2SFilipe David Borba Manana if (!eb) { 21383fe81ce2SFilipe David Borba Manana ret = -ENOMEM; 21393fe81ce2SFilipe David Borba Manana break; 21403fe81ce2SFilipe David Borba Manana } 2141d24bec3aSMark Fasheh btrfs_release_path(path); 2142d24bec3aSMark Fasheh 21432849a854SChris Mason item_size = btrfs_item_size_nr(eb, slot); 21442849a854SChris Mason ptr = btrfs_item_ptr_offset(eb, slot); 2145d24bec3aSMark Fasheh cur_offset = 0; 2146d24bec3aSMark Fasheh 2147d24bec3aSMark Fasheh while (cur_offset < item_size) { 2148d24bec3aSMark Fasheh u32 name_len; 2149d24bec3aSMark Fasheh 2150d24bec3aSMark Fasheh extref = (struct btrfs_inode_extref *)(ptr + cur_offset); 2151d24bec3aSMark Fasheh parent = btrfs_inode_extref_parent(eb, extref); 2152d24bec3aSMark Fasheh name_len = btrfs_inode_extref_name_len(eb, extref); 2153d24bec3aSMark Fasheh ret = iterate(parent, name_len, 2154d24bec3aSMark Fasheh (unsigned long)&extref->name, eb, ctx); 2155d24bec3aSMark Fasheh if (ret) 2156d24bec3aSMark Fasheh break; 2157d24bec3aSMark Fasheh 21582849a854SChris Mason cur_offset += btrfs_inode_extref_name_len(eb, extref); 2159d24bec3aSMark Fasheh cur_offset += sizeof(*extref); 2160d24bec3aSMark Fasheh } 2161d24bec3aSMark Fasheh free_extent_buffer(eb); 2162d24bec3aSMark Fasheh 2163d24bec3aSMark Fasheh offset++; 2164d24bec3aSMark Fasheh } 2165d24bec3aSMark Fasheh 2166d24bec3aSMark Fasheh btrfs_release_path(path); 2167d24bec3aSMark Fasheh 2168d24bec3aSMark Fasheh return ret; 2169d24bec3aSMark Fasheh } 2170d24bec3aSMark Fasheh 2171d24bec3aSMark Fasheh static int iterate_irefs(u64 inum, struct btrfs_root *fs_root, 2172d24bec3aSMark Fasheh struct btrfs_path *path, iterate_irefs_t *iterate, 2173d24bec3aSMark Fasheh void *ctx) 2174d24bec3aSMark Fasheh { 2175d24bec3aSMark Fasheh int ret; 2176d24bec3aSMark Fasheh int found_refs = 0; 2177d24bec3aSMark Fasheh 2178d24bec3aSMark Fasheh ret = iterate_inode_refs(inum, fs_root, path, iterate, ctx); 2179d24bec3aSMark Fasheh if (!ret) 2180d24bec3aSMark Fasheh ++found_refs; 2181d24bec3aSMark Fasheh else if (ret != -ENOENT) 2182d24bec3aSMark Fasheh return ret; 2183d24bec3aSMark Fasheh 2184d24bec3aSMark Fasheh ret = iterate_inode_extrefs(inum, fs_root, path, iterate, ctx); 2185d24bec3aSMark Fasheh if (ret == -ENOENT && found_refs) 2186d24bec3aSMark Fasheh return 0; 2187d24bec3aSMark Fasheh 2188d24bec3aSMark Fasheh return ret; 2189d24bec3aSMark Fasheh } 2190d24bec3aSMark Fasheh 2191a542ad1bSJan Schmidt /* 2192a542ad1bSJan Schmidt * returns 0 if the path could be dumped (probably truncated) 2193a542ad1bSJan Schmidt * returns <0 in case of an error 2194a542ad1bSJan Schmidt */ 2195d24bec3aSMark Fasheh static int inode_to_path(u64 inum, u32 name_len, unsigned long name_off, 2196a542ad1bSJan Schmidt struct extent_buffer *eb, void *ctx) 2197a542ad1bSJan Schmidt { 2198a542ad1bSJan Schmidt struct inode_fs_paths *ipath = ctx; 2199a542ad1bSJan Schmidt char *fspath; 2200a542ad1bSJan Schmidt char *fspath_min; 2201a542ad1bSJan Schmidt int i = ipath->fspath->elem_cnt; 2202a542ad1bSJan Schmidt const int s_ptr = sizeof(char *); 2203a542ad1bSJan Schmidt u32 bytes_left; 2204a542ad1bSJan Schmidt 2205a542ad1bSJan Schmidt bytes_left = ipath->fspath->bytes_left > s_ptr ? 2206a542ad1bSJan Schmidt ipath->fspath->bytes_left - s_ptr : 0; 2207a542ad1bSJan Schmidt 2208740c3d22SChris Mason fspath_min = (char *)ipath->fspath->val + (i + 1) * s_ptr; 220996b5bd77SJan Schmidt fspath = btrfs_ref_to_path(ipath->fs_root, ipath->btrfs_path, name_len, 221096b5bd77SJan Schmidt name_off, eb, inum, fspath_min, bytes_left); 2211a542ad1bSJan Schmidt if (IS_ERR(fspath)) 2212a542ad1bSJan Schmidt return PTR_ERR(fspath); 2213a542ad1bSJan Schmidt 2214a542ad1bSJan Schmidt if (fspath > fspath_min) { 2215745c4d8eSJeff Mahoney ipath->fspath->val[i] = (u64)(unsigned long)fspath; 2216a542ad1bSJan Schmidt ++ipath->fspath->elem_cnt; 2217a542ad1bSJan Schmidt ipath->fspath->bytes_left = fspath - fspath_min; 2218a542ad1bSJan Schmidt } else { 2219a542ad1bSJan Schmidt ++ipath->fspath->elem_missed; 2220a542ad1bSJan Schmidt ipath->fspath->bytes_missing += fspath_min - fspath; 2221a542ad1bSJan Schmidt ipath->fspath->bytes_left = 0; 2222a542ad1bSJan Schmidt } 2223a542ad1bSJan Schmidt 2224a542ad1bSJan Schmidt return 0; 2225a542ad1bSJan Schmidt } 2226a542ad1bSJan Schmidt 2227a542ad1bSJan Schmidt /* 2228a542ad1bSJan Schmidt * this dumps all file system paths to the inode into the ipath struct, provided 2229a542ad1bSJan Schmidt * is has been created large enough. each path is zero-terminated and accessed 2230740c3d22SChris Mason * from ipath->fspath->val[i]. 2231a542ad1bSJan Schmidt * when it returns, there are ipath->fspath->elem_cnt number of paths available 2232740c3d22SChris Mason * in ipath->fspath->val[]. when the allocated space wasn't sufficient, the 223301327610SNicholas D Steeves * number of missed paths is recorded in ipath->fspath->elem_missed, otherwise, 2234a542ad1bSJan Schmidt * it's zero. ipath->fspath->bytes_missing holds the number of bytes that would 2235a542ad1bSJan Schmidt * have been needed to return all paths. 2236a542ad1bSJan Schmidt */ 2237a542ad1bSJan Schmidt int paths_from_inode(u64 inum, struct inode_fs_paths *ipath) 2238a542ad1bSJan Schmidt { 2239a542ad1bSJan Schmidt return iterate_irefs(inum, ipath->fs_root, ipath->btrfs_path, 2240a542ad1bSJan Schmidt inode_to_path, ipath); 2241a542ad1bSJan Schmidt } 2242a542ad1bSJan Schmidt 2243a542ad1bSJan Schmidt struct btrfs_data_container *init_data_container(u32 total_bytes) 2244a542ad1bSJan Schmidt { 2245a542ad1bSJan Schmidt struct btrfs_data_container *data; 2246a542ad1bSJan Schmidt size_t alloc_bytes; 2247a542ad1bSJan Schmidt 2248a542ad1bSJan Schmidt alloc_bytes = max_t(size_t, total_bytes, sizeof(*data)); 2249f54de068SDavid Sterba data = kvmalloc(alloc_bytes, GFP_KERNEL); 2250a542ad1bSJan Schmidt if (!data) 2251a542ad1bSJan Schmidt return ERR_PTR(-ENOMEM); 2252a542ad1bSJan Schmidt 2253a542ad1bSJan Schmidt if (total_bytes >= sizeof(*data)) { 2254a542ad1bSJan Schmidt data->bytes_left = total_bytes - sizeof(*data); 2255a542ad1bSJan Schmidt data->bytes_missing = 0; 2256a542ad1bSJan Schmidt } else { 2257a542ad1bSJan Schmidt data->bytes_missing = sizeof(*data) - total_bytes; 2258a542ad1bSJan Schmidt data->bytes_left = 0; 2259a542ad1bSJan Schmidt } 2260a542ad1bSJan Schmidt 2261a542ad1bSJan Schmidt data->elem_cnt = 0; 2262a542ad1bSJan Schmidt data->elem_missed = 0; 2263a542ad1bSJan Schmidt 2264a542ad1bSJan Schmidt return data; 2265a542ad1bSJan Schmidt } 2266a542ad1bSJan Schmidt 2267a542ad1bSJan Schmidt /* 2268a542ad1bSJan Schmidt * allocates space to return multiple file system paths for an inode. 2269a542ad1bSJan Schmidt * total_bytes to allocate are passed, note that space usable for actual path 2270a542ad1bSJan Schmidt * information will be total_bytes - sizeof(struct inode_fs_paths). 2271a542ad1bSJan Schmidt * the returned pointer must be freed with free_ipath() in the end. 2272a542ad1bSJan Schmidt */ 2273a542ad1bSJan Schmidt struct inode_fs_paths *init_ipath(s32 total_bytes, struct btrfs_root *fs_root, 2274a542ad1bSJan Schmidt struct btrfs_path *path) 2275a542ad1bSJan Schmidt { 2276a542ad1bSJan Schmidt struct inode_fs_paths *ifp; 2277a542ad1bSJan Schmidt struct btrfs_data_container *fspath; 2278a542ad1bSJan Schmidt 2279a542ad1bSJan Schmidt fspath = init_data_container(total_bytes); 2280a542ad1bSJan Schmidt if (IS_ERR(fspath)) 2281afc6961fSMisono Tomohiro return ERR_CAST(fspath); 2282a542ad1bSJan Schmidt 2283f54de068SDavid Sterba ifp = kmalloc(sizeof(*ifp), GFP_KERNEL); 2284a542ad1bSJan Schmidt if (!ifp) { 2285f54de068SDavid Sterba kvfree(fspath); 2286a542ad1bSJan Schmidt return ERR_PTR(-ENOMEM); 2287a542ad1bSJan Schmidt } 2288a542ad1bSJan Schmidt 2289a542ad1bSJan Schmidt ifp->btrfs_path = path; 2290a542ad1bSJan Schmidt ifp->fspath = fspath; 2291a542ad1bSJan Schmidt ifp->fs_root = fs_root; 2292a542ad1bSJan Schmidt 2293a542ad1bSJan Schmidt return ifp; 2294a542ad1bSJan Schmidt } 2295a542ad1bSJan Schmidt 2296a542ad1bSJan Schmidt void free_ipath(struct inode_fs_paths *ipath) 2297a542ad1bSJan Schmidt { 22984735fb28SJesper Juhl if (!ipath) 22994735fb28SJesper Juhl return; 2300f54de068SDavid Sterba kvfree(ipath->fspath); 2301a542ad1bSJan Schmidt kfree(ipath); 2302a542ad1bSJan Schmidt } 2303