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" 161b60d2ecSQu Wenruo #include "misc.h" 17a542ad1bSJan Schmidt 18dc046b10SJosef Bacik /* Just an arbitrary number so we can be sure this happened */ 19dc046b10SJosef Bacik #define BACKREF_FOUND_SHARED 6 20dc046b10SJosef Bacik 21976b1908SJan Schmidt struct extent_inode_elem { 22976b1908SJan Schmidt u64 inum; 23976b1908SJan Schmidt u64 offset; 24976b1908SJan Schmidt struct extent_inode_elem *next; 25976b1908SJan Schmidt }; 26976b1908SJan Schmidt 2773980becSJeff Mahoney static int check_extent_in_eb(const struct btrfs_key *key, 2873980becSJeff Mahoney const struct extent_buffer *eb, 2973980becSJeff Mahoney const struct btrfs_file_extent_item *fi, 30976b1908SJan Schmidt u64 extent_item_pos, 31c995ab3cSZygo Blaxell struct extent_inode_elem **eie, 32c995ab3cSZygo Blaxell bool ignore_offset) 33976b1908SJan Schmidt { 348ca15e05SJosef Bacik u64 offset = 0; 358ca15e05SJosef Bacik struct extent_inode_elem *e; 368ca15e05SJosef Bacik 37c995ab3cSZygo Blaxell if (!ignore_offset && 38c995ab3cSZygo Blaxell !btrfs_file_extent_compression(eb, fi) && 398ca15e05SJosef Bacik !btrfs_file_extent_encryption(eb, fi) && 408ca15e05SJosef Bacik !btrfs_file_extent_other_encoding(eb, fi)) { 41976b1908SJan Schmidt u64 data_offset; 42976b1908SJan Schmidt u64 data_len; 43976b1908SJan Schmidt 44976b1908SJan Schmidt data_offset = btrfs_file_extent_offset(eb, fi); 45976b1908SJan Schmidt data_len = btrfs_file_extent_num_bytes(eb, fi); 46976b1908SJan Schmidt 47976b1908SJan Schmidt if (extent_item_pos < data_offset || 48976b1908SJan Schmidt extent_item_pos >= data_offset + data_len) 49976b1908SJan Schmidt return 1; 508ca15e05SJosef Bacik offset = extent_item_pos - data_offset; 518ca15e05SJosef Bacik } 52976b1908SJan Schmidt 53976b1908SJan Schmidt e = kmalloc(sizeof(*e), GFP_NOFS); 54976b1908SJan Schmidt if (!e) 55976b1908SJan Schmidt return -ENOMEM; 56976b1908SJan Schmidt 57976b1908SJan Schmidt e->next = *eie; 58976b1908SJan Schmidt e->inum = key->objectid; 598ca15e05SJosef Bacik e->offset = key->offset + offset; 60976b1908SJan Schmidt *eie = e; 61976b1908SJan Schmidt 62976b1908SJan Schmidt return 0; 63976b1908SJan Schmidt } 64976b1908SJan Schmidt 65f05c4746SWang Shilong static void free_inode_elem_list(struct extent_inode_elem *eie) 66f05c4746SWang Shilong { 67f05c4746SWang Shilong struct extent_inode_elem *eie_next; 68f05c4746SWang Shilong 69f05c4746SWang Shilong for (; eie; eie = eie_next) { 70f05c4746SWang Shilong eie_next = eie->next; 71f05c4746SWang Shilong kfree(eie); 72f05c4746SWang Shilong } 73f05c4746SWang Shilong } 74f05c4746SWang Shilong 7573980becSJeff Mahoney static int find_extent_in_eb(const struct extent_buffer *eb, 7673980becSJeff Mahoney u64 wanted_disk_byte, u64 extent_item_pos, 77c995ab3cSZygo Blaxell struct extent_inode_elem **eie, 78c995ab3cSZygo Blaxell bool ignore_offset) 79976b1908SJan Schmidt { 80976b1908SJan Schmidt u64 disk_byte; 81976b1908SJan Schmidt struct btrfs_key key; 82976b1908SJan Schmidt struct btrfs_file_extent_item *fi; 83976b1908SJan Schmidt int slot; 84976b1908SJan Schmidt int nritems; 85976b1908SJan Schmidt int extent_type; 86976b1908SJan Schmidt int ret; 87976b1908SJan Schmidt 88976b1908SJan Schmidt /* 89976b1908SJan Schmidt * from the shared data ref, we only have the leaf but we need 90976b1908SJan Schmidt * the key. thus, we must look into all items and see that we 91976b1908SJan Schmidt * find one (some) with a reference to our extent item. 92976b1908SJan Schmidt */ 93976b1908SJan Schmidt nritems = btrfs_header_nritems(eb); 94976b1908SJan Schmidt for (slot = 0; slot < nritems; ++slot) { 95976b1908SJan Schmidt btrfs_item_key_to_cpu(eb, &key, slot); 96976b1908SJan Schmidt if (key.type != BTRFS_EXTENT_DATA_KEY) 97976b1908SJan Schmidt continue; 98976b1908SJan Schmidt fi = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item); 99976b1908SJan Schmidt extent_type = btrfs_file_extent_type(eb, fi); 100976b1908SJan Schmidt if (extent_type == BTRFS_FILE_EXTENT_INLINE) 101976b1908SJan Schmidt continue; 102976b1908SJan Schmidt /* don't skip BTRFS_FILE_EXTENT_PREALLOC, we can handle that */ 103976b1908SJan Schmidt disk_byte = btrfs_file_extent_disk_bytenr(eb, fi); 104976b1908SJan Schmidt if (disk_byte != wanted_disk_byte) 105976b1908SJan Schmidt continue; 106976b1908SJan Schmidt 107c995ab3cSZygo Blaxell ret = check_extent_in_eb(&key, eb, fi, extent_item_pos, eie, ignore_offset); 108976b1908SJan Schmidt if (ret < 0) 109976b1908SJan Schmidt return ret; 110976b1908SJan Schmidt } 111976b1908SJan Schmidt 112976b1908SJan Schmidt return 0; 113976b1908SJan Schmidt } 114976b1908SJan Schmidt 11586d5f994SEdmund Nadolski struct preftree { 116ecf160b4SLiu Bo struct rb_root_cached root; 1176c336b21SJeff Mahoney unsigned int count; 11886d5f994SEdmund Nadolski }; 11986d5f994SEdmund Nadolski 120ecf160b4SLiu Bo #define PREFTREE_INIT { .root = RB_ROOT_CACHED, .count = 0 } 12186d5f994SEdmund Nadolski 12286d5f994SEdmund Nadolski struct preftrees { 12386d5f994SEdmund Nadolski struct preftree direct; /* BTRFS_SHARED_[DATA|BLOCK]_REF_KEY */ 12486d5f994SEdmund Nadolski struct preftree indirect; /* BTRFS_[TREE_BLOCK|EXTENT_DATA]_REF_KEY */ 12586d5f994SEdmund Nadolski struct preftree indirect_missing_keys; 12686d5f994SEdmund Nadolski }; 12786d5f994SEdmund Nadolski 1283ec4d323SEdmund Nadolski /* 1293ec4d323SEdmund Nadolski * Checks for a shared extent during backref search. 1303ec4d323SEdmund Nadolski * 1313ec4d323SEdmund Nadolski * The share_count tracks prelim_refs (direct and indirect) having a 1323ec4d323SEdmund Nadolski * ref->count >0: 1333ec4d323SEdmund Nadolski * - incremented when a ref->count transitions to >0 1343ec4d323SEdmund Nadolski * - decremented when a ref->count transitions to <1 1353ec4d323SEdmund Nadolski */ 1363ec4d323SEdmund Nadolski struct share_check { 1373ec4d323SEdmund Nadolski u64 root_objectid; 1383ec4d323SEdmund Nadolski u64 inum; 1393ec4d323SEdmund Nadolski int share_count; 1403ec4d323SEdmund Nadolski }; 1413ec4d323SEdmund Nadolski 1423ec4d323SEdmund Nadolski static inline int extent_is_shared(struct share_check *sc) 1433ec4d323SEdmund Nadolski { 1443ec4d323SEdmund Nadolski return (sc && sc->share_count > 1) ? BACKREF_FOUND_SHARED : 0; 1453ec4d323SEdmund Nadolski } 1463ec4d323SEdmund Nadolski 147b9e9a6cbSWang Shilong static struct kmem_cache *btrfs_prelim_ref_cache; 148b9e9a6cbSWang Shilong 149b9e9a6cbSWang Shilong int __init btrfs_prelim_ref_init(void) 150b9e9a6cbSWang Shilong { 151b9e9a6cbSWang Shilong btrfs_prelim_ref_cache = kmem_cache_create("btrfs_prelim_ref", 152e0c476b1SJeff Mahoney sizeof(struct prelim_ref), 153b9e9a6cbSWang Shilong 0, 154fba4b697SNikolay Borisov SLAB_MEM_SPREAD, 155b9e9a6cbSWang Shilong NULL); 156b9e9a6cbSWang Shilong if (!btrfs_prelim_ref_cache) 157b9e9a6cbSWang Shilong return -ENOMEM; 158b9e9a6cbSWang Shilong return 0; 159b9e9a6cbSWang Shilong } 160b9e9a6cbSWang Shilong 161e67c718bSDavid Sterba void __cold btrfs_prelim_ref_exit(void) 162b9e9a6cbSWang Shilong { 163b9e9a6cbSWang Shilong kmem_cache_destroy(btrfs_prelim_ref_cache); 164b9e9a6cbSWang Shilong } 165b9e9a6cbSWang Shilong 16686d5f994SEdmund Nadolski static void free_pref(struct prelim_ref *ref) 16786d5f994SEdmund Nadolski { 16886d5f994SEdmund Nadolski kmem_cache_free(btrfs_prelim_ref_cache, ref); 16986d5f994SEdmund Nadolski } 17086d5f994SEdmund Nadolski 17186d5f994SEdmund Nadolski /* 17286d5f994SEdmund Nadolski * Return 0 when both refs are for the same block (and can be merged). 17386d5f994SEdmund Nadolski * A -1 return indicates ref1 is a 'lower' block than ref2, while 1 17486d5f994SEdmund Nadolski * indicates a 'higher' block. 17586d5f994SEdmund Nadolski */ 17686d5f994SEdmund Nadolski static int prelim_ref_compare(struct prelim_ref *ref1, 17786d5f994SEdmund Nadolski struct prelim_ref *ref2) 17886d5f994SEdmund Nadolski { 17986d5f994SEdmund Nadolski if (ref1->level < ref2->level) 18086d5f994SEdmund Nadolski return -1; 18186d5f994SEdmund Nadolski if (ref1->level > ref2->level) 18286d5f994SEdmund Nadolski return 1; 18386d5f994SEdmund Nadolski if (ref1->root_id < ref2->root_id) 18486d5f994SEdmund Nadolski return -1; 18586d5f994SEdmund Nadolski if (ref1->root_id > ref2->root_id) 18686d5f994SEdmund Nadolski return 1; 18786d5f994SEdmund Nadolski if (ref1->key_for_search.type < ref2->key_for_search.type) 18886d5f994SEdmund Nadolski return -1; 18986d5f994SEdmund Nadolski if (ref1->key_for_search.type > ref2->key_for_search.type) 19086d5f994SEdmund Nadolski return 1; 19186d5f994SEdmund Nadolski if (ref1->key_for_search.objectid < ref2->key_for_search.objectid) 19286d5f994SEdmund Nadolski return -1; 19386d5f994SEdmund Nadolski if (ref1->key_for_search.objectid > ref2->key_for_search.objectid) 19486d5f994SEdmund Nadolski return 1; 19586d5f994SEdmund Nadolski if (ref1->key_for_search.offset < ref2->key_for_search.offset) 19686d5f994SEdmund Nadolski return -1; 19786d5f994SEdmund Nadolski if (ref1->key_for_search.offset > ref2->key_for_search.offset) 19886d5f994SEdmund Nadolski return 1; 19986d5f994SEdmund Nadolski if (ref1->parent < ref2->parent) 20086d5f994SEdmund Nadolski return -1; 20186d5f994SEdmund Nadolski if (ref1->parent > ref2->parent) 20286d5f994SEdmund Nadolski return 1; 20386d5f994SEdmund Nadolski 20486d5f994SEdmund Nadolski return 0; 20586d5f994SEdmund Nadolski } 20686d5f994SEdmund Nadolski 207ccc8dc75SColin Ian King static void update_share_count(struct share_check *sc, int oldcount, 208ccc8dc75SColin Ian King int newcount) 2093ec4d323SEdmund Nadolski { 2103ec4d323SEdmund Nadolski if ((!sc) || (oldcount == 0 && newcount < 1)) 2113ec4d323SEdmund Nadolski return; 2123ec4d323SEdmund Nadolski 2133ec4d323SEdmund Nadolski if (oldcount > 0 && newcount < 1) 2143ec4d323SEdmund Nadolski sc->share_count--; 2153ec4d323SEdmund Nadolski else if (oldcount < 1 && newcount > 0) 2163ec4d323SEdmund Nadolski sc->share_count++; 2173ec4d323SEdmund Nadolski } 2183ec4d323SEdmund Nadolski 21986d5f994SEdmund Nadolski /* 22086d5f994SEdmund Nadolski * Add @newref to the @root rbtree, merging identical refs. 22186d5f994SEdmund Nadolski * 2223ec4d323SEdmund Nadolski * Callers should assume that newref has been freed after calling. 22386d5f994SEdmund Nadolski */ 22400142756SJeff Mahoney static void prelim_ref_insert(const struct btrfs_fs_info *fs_info, 22500142756SJeff Mahoney struct preftree *preftree, 2263ec4d323SEdmund Nadolski struct prelim_ref *newref, 2273ec4d323SEdmund Nadolski struct share_check *sc) 22886d5f994SEdmund Nadolski { 229ecf160b4SLiu Bo struct rb_root_cached *root; 23086d5f994SEdmund Nadolski struct rb_node **p; 23186d5f994SEdmund Nadolski struct rb_node *parent = NULL; 23286d5f994SEdmund Nadolski struct prelim_ref *ref; 23386d5f994SEdmund Nadolski int result; 234ecf160b4SLiu Bo bool leftmost = true; 23586d5f994SEdmund Nadolski 23686d5f994SEdmund Nadolski root = &preftree->root; 237ecf160b4SLiu Bo p = &root->rb_root.rb_node; 23886d5f994SEdmund Nadolski 23986d5f994SEdmund Nadolski while (*p) { 24086d5f994SEdmund Nadolski parent = *p; 24186d5f994SEdmund Nadolski ref = rb_entry(parent, struct prelim_ref, rbnode); 24286d5f994SEdmund Nadolski result = prelim_ref_compare(ref, newref); 24386d5f994SEdmund Nadolski if (result < 0) { 24486d5f994SEdmund Nadolski p = &(*p)->rb_left; 24586d5f994SEdmund Nadolski } else if (result > 0) { 24686d5f994SEdmund Nadolski p = &(*p)->rb_right; 247ecf160b4SLiu Bo leftmost = false; 24886d5f994SEdmund Nadolski } else { 24986d5f994SEdmund Nadolski /* Identical refs, merge them and free @newref */ 25086d5f994SEdmund Nadolski struct extent_inode_elem *eie = ref->inode_list; 25186d5f994SEdmund Nadolski 25286d5f994SEdmund Nadolski while (eie && eie->next) 25386d5f994SEdmund Nadolski eie = eie->next; 25486d5f994SEdmund Nadolski 25586d5f994SEdmund Nadolski if (!eie) 25686d5f994SEdmund Nadolski ref->inode_list = newref->inode_list; 25786d5f994SEdmund Nadolski else 25886d5f994SEdmund Nadolski eie->next = newref->inode_list; 25900142756SJeff Mahoney trace_btrfs_prelim_ref_merge(fs_info, ref, newref, 26000142756SJeff Mahoney preftree->count); 2613ec4d323SEdmund Nadolski /* 2623ec4d323SEdmund Nadolski * A delayed ref can have newref->count < 0. 2633ec4d323SEdmund Nadolski * The ref->count is updated to follow any 2643ec4d323SEdmund Nadolski * BTRFS_[ADD|DROP]_DELAYED_REF actions. 2653ec4d323SEdmund Nadolski */ 2663ec4d323SEdmund Nadolski update_share_count(sc, ref->count, 2673ec4d323SEdmund Nadolski ref->count + newref->count); 26886d5f994SEdmund Nadolski ref->count += newref->count; 26986d5f994SEdmund Nadolski free_pref(newref); 27086d5f994SEdmund Nadolski return; 27186d5f994SEdmund Nadolski } 27286d5f994SEdmund Nadolski } 27386d5f994SEdmund Nadolski 2743ec4d323SEdmund Nadolski update_share_count(sc, 0, newref->count); 2756c336b21SJeff Mahoney preftree->count++; 27600142756SJeff Mahoney trace_btrfs_prelim_ref_insert(fs_info, newref, NULL, preftree->count); 27786d5f994SEdmund Nadolski rb_link_node(&newref->rbnode, parent, p); 278ecf160b4SLiu Bo rb_insert_color_cached(&newref->rbnode, root, leftmost); 27986d5f994SEdmund Nadolski } 28086d5f994SEdmund Nadolski 28186d5f994SEdmund Nadolski /* 28286d5f994SEdmund Nadolski * Release the entire tree. We don't care about internal consistency so 28386d5f994SEdmund Nadolski * just free everything and then reset the tree root. 28486d5f994SEdmund Nadolski */ 28586d5f994SEdmund Nadolski static void prelim_release(struct preftree *preftree) 28686d5f994SEdmund Nadolski { 28786d5f994SEdmund Nadolski struct prelim_ref *ref, *next_ref; 28886d5f994SEdmund Nadolski 289ecf160b4SLiu Bo rbtree_postorder_for_each_entry_safe(ref, next_ref, 290ecf160b4SLiu Bo &preftree->root.rb_root, rbnode) 29186d5f994SEdmund Nadolski free_pref(ref); 29286d5f994SEdmund Nadolski 293ecf160b4SLiu Bo preftree->root = RB_ROOT_CACHED; 2946c336b21SJeff Mahoney preftree->count = 0; 29586d5f994SEdmund Nadolski } 29686d5f994SEdmund Nadolski 297d5c88b73SJan Schmidt /* 298d5c88b73SJan Schmidt * the rules for all callers of this function are: 299d5c88b73SJan Schmidt * - obtaining the parent is the goal 300d5c88b73SJan Schmidt * - if you add a key, you must know that it is a correct key 301d5c88b73SJan Schmidt * - if you cannot add the parent or a correct key, then we will look into the 302d5c88b73SJan Schmidt * block later to set a correct key 303d5c88b73SJan Schmidt * 304d5c88b73SJan Schmidt * delayed refs 305d5c88b73SJan Schmidt * ============ 306d5c88b73SJan Schmidt * backref type | shared | indirect | shared | indirect 307d5c88b73SJan Schmidt * information | tree | tree | data | data 308d5c88b73SJan Schmidt * --------------------+--------+----------+--------+---------- 309d5c88b73SJan Schmidt * parent logical | y | - | - | - 310d5c88b73SJan Schmidt * key to resolve | - | y | y | y 311d5c88b73SJan Schmidt * tree block logical | - | - | - | - 312d5c88b73SJan Schmidt * root for resolving | y | y | y | y 313d5c88b73SJan Schmidt * 314d5c88b73SJan Schmidt * - column 1: we've the parent -> done 315d5c88b73SJan Schmidt * - column 2, 3, 4: we use the key to find the parent 316d5c88b73SJan Schmidt * 317d5c88b73SJan Schmidt * on disk refs (inline or keyed) 318d5c88b73SJan Schmidt * ============================== 319d5c88b73SJan Schmidt * backref type | shared | indirect | shared | indirect 320d5c88b73SJan Schmidt * information | tree | tree | data | data 321d5c88b73SJan Schmidt * --------------------+--------+----------+--------+---------- 322d5c88b73SJan Schmidt * parent logical | y | - | y | - 323d5c88b73SJan Schmidt * key to resolve | - | - | - | y 324d5c88b73SJan Schmidt * tree block logical | y | y | y | y 325d5c88b73SJan Schmidt * root for resolving | - | y | y | y 326d5c88b73SJan Schmidt * 327d5c88b73SJan Schmidt * - column 1, 3: we've the parent -> done 328d5c88b73SJan Schmidt * - column 2: we take the first key from the block to find the parent 329e0c476b1SJeff Mahoney * (see add_missing_keys) 330d5c88b73SJan Schmidt * - column 4: we use the key to find the parent 331d5c88b73SJan Schmidt * 332d5c88b73SJan Schmidt * additional information that's available but not required to find the parent 333d5c88b73SJan Schmidt * block might help in merging entries to gain some speed. 334d5c88b73SJan Schmidt */ 33500142756SJeff Mahoney static int add_prelim_ref(const struct btrfs_fs_info *fs_info, 33600142756SJeff Mahoney struct preftree *preftree, u64 root_id, 337e0c476b1SJeff Mahoney const struct btrfs_key *key, int level, u64 parent, 3383ec4d323SEdmund Nadolski u64 wanted_disk_byte, int count, 3393ec4d323SEdmund Nadolski struct share_check *sc, gfp_t gfp_mask) 3408da6d581SJan Schmidt { 341e0c476b1SJeff Mahoney struct prelim_ref *ref; 3428da6d581SJan Schmidt 34348ec4736SLiu Bo if (root_id == BTRFS_DATA_RELOC_TREE_OBJECTID) 34448ec4736SLiu Bo return 0; 34548ec4736SLiu Bo 346b9e9a6cbSWang Shilong ref = kmem_cache_alloc(btrfs_prelim_ref_cache, gfp_mask); 3478da6d581SJan Schmidt if (!ref) 3488da6d581SJan Schmidt return -ENOMEM; 3498da6d581SJan Schmidt 3508da6d581SJan Schmidt ref->root_id = root_id; 3517ac8b88eSethanwu if (key) 352d5c88b73SJan Schmidt ref->key_for_search = *key; 3537ac8b88eSethanwu else 354d5c88b73SJan Schmidt memset(&ref->key_for_search, 0, sizeof(ref->key_for_search)); 3558da6d581SJan Schmidt 3563301958bSJan Schmidt ref->inode_list = NULL; 3578da6d581SJan Schmidt ref->level = level; 3588da6d581SJan Schmidt ref->count = count; 3598da6d581SJan Schmidt ref->parent = parent; 3608da6d581SJan Schmidt ref->wanted_disk_byte = wanted_disk_byte; 3613ec4d323SEdmund Nadolski prelim_ref_insert(fs_info, preftree, ref, sc); 3623ec4d323SEdmund Nadolski return extent_is_shared(sc); 3638da6d581SJan Schmidt } 3648da6d581SJan Schmidt 36586d5f994SEdmund Nadolski /* direct refs use root == 0, key == NULL */ 36600142756SJeff Mahoney static int add_direct_ref(const struct btrfs_fs_info *fs_info, 36700142756SJeff Mahoney struct preftrees *preftrees, int level, u64 parent, 3683ec4d323SEdmund Nadolski u64 wanted_disk_byte, int count, 3693ec4d323SEdmund Nadolski struct share_check *sc, gfp_t gfp_mask) 37086d5f994SEdmund Nadolski { 37100142756SJeff Mahoney return add_prelim_ref(fs_info, &preftrees->direct, 0, NULL, level, 3723ec4d323SEdmund Nadolski parent, wanted_disk_byte, count, sc, gfp_mask); 37386d5f994SEdmund Nadolski } 37486d5f994SEdmund Nadolski 37586d5f994SEdmund Nadolski /* indirect refs use parent == 0 */ 37600142756SJeff Mahoney static int add_indirect_ref(const struct btrfs_fs_info *fs_info, 37700142756SJeff Mahoney struct preftrees *preftrees, u64 root_id, 37886d5f994SEdmund Nadolski const struct btrfs_key *key, int level, 3793ec4d323SEdmund Nadolski u64 wanted_disk_byte, int count, 3803ec4d323SEdmund Nadolski struct share_check *sc, gfp_t gfp_mask) 38186d5f994SEdmund Nadolski { 38286d5f994SEdmund Nadolski struct preftree *tree = &preftrees->indirect; 38386d5f994SEdmund Nadolski 38486d5f994SEdmund Nadolski if (!key) 38586d5f994SEdmund Nadolski tree = &preftrees->indirect_missing_keys; 38600142756SJeff Mahoney return add_prelim_ref(fs_info, tree, root_id, key, level, 0, 3873ec4d323SEdmund Nadolski wanted_disk_byte, count, sc, gfp_mask); 38886d5f994SEdmund Nadolski } 38986d5f994SEdmund Nadolski 390ed58f2e6Sethanwu static int is_shared_data_backref(struct preftrees *preftrees, u64 bytenr) 391ed58f2e6Sethanwu { 392ed58f2e6Sethanwu struct rb_node **p = &preftrees->direct.root.rb_root.rb_node; 393ed58f2e6Sethanwu struct rb_node *parent = NULL; 394ed58f2e6Sethanwu struct prelim_ref *ref = NULL; 3959c6c723fSArnd Bergmann struct prelim_ref target = {}; 396ed58f2e6Sethanwu int result; 397ed58f2e6Sethanwu 398ed58f2e6Sethanwu target.parent = bytenr; 399ed58f2e6Sethanwu 400ed58f2e6Sethanwu while (*p) { 401ed58f2e6Sethanwu parent = *p; 402ed58f2e6Sethanwu ref = rb_entry(parent, struct prelim_ref, rbnode); 403ed58f2e6Sethanwu result = prelim_ref_compare(ref, &target); 404ed58f2e6Sethanwu 405ed58f2e6Sethanwu if (result < 0) 406ed58f2e6Sethanwu p = &(*p)->rb_left; 407ed58f2e6Sethanwu else if (result > 0) 408ed58f2e6Sethanwu p = &(*p)->rb_right; 409ed58f2e6Sethanwu else 410ed58f2e6Sethanwu return 1; 411ed58f2e6Sethanwu } 412ed58f2e6Sethanwu return 0; 413ed58f2e6Sethanwu } 414ed58f2e6Sethanwu 4158da6d581SJan Schmidt static int add_all_parents(struct btrfs_root *root, struct btrfs_path *path, 416ed58f2e6Sethanwu struct ulist *parents, 417ed58f2e6Sethanwu struct preftrees *preftrees, struct prelim_ref *ref, 41844853868SJosef Bacik int level, u64 time_seq, const u64 *extent_item_pos, 419b25b0b87Sethanwu bool ignore_offset) 4208da6d581SJan Schmidt { 42169bca40dSAlexander Block int ret = 0; 42269bca40dSAlexander Block int slot; 42369bca40dSAlexander Block struct extent_buffer *eb; 42469bca40dSAlexander Block struct btrfs_key key; 4257ef81ac8SJosef Bacik struct btrfs_key *key_for_search = &ref->key_for_search; 4268da6d581SJan Schmidt struct btrfs_file_extent_item *fi; 427ed8c4913SJosef Bacik struct extent_inode_elem *eie = NULL, *old = NULL; 4288da6d581SJan Schmidt u64 disk_byte; 4297ef81ac8SJosef Bacik u64 wanted_disk_byte = ref->wanted_disk_byte; 4307ef81ac8SJosef Bacik u64 count = 0; 4317ac8b88eSethanwu u64 data_offset; 4328da6d581SJan Schmidt 43369bca40dSAlexander Block if (level != 0) { 43469bca40dSAlexander Block eb = path->nodes[level]; 43569bca40dSAlexander Block ret = ulist_add(parents, eb->start, 0, GFP_NOFS); 4363301958bSJan Schmidt if (ret < 0) 4373301958bSJan Schmidt return ret; 4388da6d581SJan Schmidt return 0; 43969bca40dSAlexander Block } 4408da6d581SJan Schmidt 4418da6d581SJan Schmidt /* 442ed58f2e6Sethanwu * 1. We normally enter this function with the path already pointing to 44369bca40dSAlexander Block * the first item to check. But sometimes, we may enter it with 444ed58f2e6Sethanwu * slot == nritems. 445ed58f2e6Sethanwu * 2. We are searching for normal backref but bytenr of this leaf 446ed58f2e6Sethanwu * matches shared data backref 447cfc0eed0Sethanwu * 3. The leaf owner is not equal to the root we are searching 448cfc0eed0Sethanwu * 449ed58f2e6Sethanwu * For these cases, go to the next leaf before we continue. 4508da6d581SJan Schmidt */ 451ed58f2e6Sethanwu eb = path->nodes[0]; 452ed58f2e6Sethanwu if (path->slots[0] >= btrfs_header_nritems(eb) || 453cfc0eed0Sethanwu is_shared_data_backref(preftrees, eb->start) || 454cfc0eed0Sethanwu ref->root_id != btrfs_header_owner(eb)) { 455de47c9d3SEdmund Nadolski if (time_seq == SEQ_LAST) 45621633fc6SQu Wenruo ret = btrfs_next_leaf(root, path); 45721633fc6SQu Wenruo else 4583d7806ecSJan Schmidt ret = btrfs_next_old_leaf(root, path, time_seq); 45921633fc6SQu Wenruo } 4608da6d581SJan Schmidt 461b25b0b87Sethanwu while (!ret && count < ref->count) { 4628da6d581SJan Schmidt eb = path->nodes[0]; 46369bca40dSAlexander Block slot = path->slots[0]; 46469bca40dSAlexander Block 46569bca40dSAlexander Block btrfs_item_key_to_cpu(eb, &key, slot); 46669bca40dSAlexander Block 46769bca40dSAlexander Block if (key.objectid != key_for_search->objectid || 46869bca40dSAlexander Block key.type != BTRFS_EXTENT_DATA_KEY) 46969bca40dSAlexander Block break; 47069bca40dSAlexander Block 471ed58f2e6Sethanwu /* 472ed58f2e6Sethanwu * We are searching for normal backref but bytenr of this leaf 473cfc0eed0Sethanwu * matches shared data backref, OR 474cfc0eed0Sethanwu * the leaf owner is not equal to the root we are searching for 475ed58f2e6Sethanwu */ 476cfc0eed0Sethanwu if (slot == 0 && 477cfc0eed0Sethanwu (is_shared_data_backref(preftrees, eb->start) || 478cfc0eed0Sethanwu ref->root_id != btrfs_header_owner(eb))) { 479ed58f2e6Sethanwu if (time_seq == SEQ_LAST) 480ed58f2e6Sethanwu ret = btrfs_next_leaf(root, path); 481ed58f2e6Sethanwu else 482ed58f2e6Sethanwu ret = btrfs_next_old_leaf(root, path, time_seq); 483ed58f2e6Sethanwu continue; 484ed58f2e6Sethanwu } 48569bca40dSAlexander Block fi = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item); 4868da6d581SJan Schmidt disk_byte = btrfs_file_extent_disk_bytenr(eb, fi); 4877ac8b88eSethanwu data_offset = btrfs_file_extent_offset(eb, fi); 48869bca40dSAlexander Block 48969bca40dSAlexander Block if (disk_byte == wanted_disk_byte) { 49069bca40dSAlexander Block eie = NULL; 491ed8c4913SJosef Bacik old = NULL; 4927ac8b88eSethanwu if (ref->key_for_search.offset == key.offset - data_offset) 4937ef81ac8SJosef Bacik count++; 4947ac8b88eSethanwu else 4957ac8b88eSethanwu goto next; 49669bca40dSAlexander Block if (extent_item_pos) { 49769bca40dSAlexander Block ret = check_extent_in_eb(&key, eb, fi, 49869bca40dSAlexander Block *extent_item_pos, 499c995ab3cSZygo Blaxell &eie, ignore_offset); 50069bca40dSAlexander Block if (ret < 0) 50169bca40dSAlexander Block break; 5028da6d581SJan Schmidt } 503ed8c4913SJosef Bacik if (ret > 0) 504ed8c4913SJosef Bacik goto next; 5054eb1f66dSTakashi Iwai ret = ulist_add_merge_ptr(parents, eb->start, 5064eb1f66dSTakashi Iwai eie, (void **)&old, GFP_NOFS); 50769bca40dSAlexander Block if (ret < 0) 50869bca40dSAlexander Block break; 509ed8c4913SJosef Bacik if (!ret && extent_item_pos) { 510ed8c4913SJosef Bacik while (old->next) 511ed8c4913SJosef Bacik old = old->next; 512ed8c4913SJosef Bacik old->next = eie; 51369bca40dSAlexander Block } 514f05c4746SWang Shilong eie = NULL; 51569bca40dSAlexander Block } 516ed8c4913SJosef Bacik next: 517de47c9d3SEdmund Nadolski if (time_seq == SEQ_LAST) 51821633fc6SQu Wenruo ret = btrfs_next_item(root, path); 51921633fc6SQu Wenruo else 52069bca40dSAlexander Block ret = btrfs_next_old_item(root, path, time_seq); 5218da6d581SJan Schmidt } 5228da6d581SJan Schmidt 52369bca40dSAlexander Block if (ret > 0) 52469bca40dSAlexander Block ret = 0; 525f05c4746SWang Shilong else if (ret < 0) 526f05c4746SWang Shilong free_inode_elem_list(eie); 52769bca40dSAlexander Block return ret; 5288da6d581SJan Schmidt } 5298da6d581SJan Schmidt 5308da6d581SJan Schmidt /* 5318da6d581SJan Schmidt * resolve an indirect backref in the form (root_id, key, level) 5328da6d581SJan Schmidt * to a logical address 5338da6d581SJan Schmidt */ 534e0c476b1SJeff Mahoney static int resolve_indirect_ref(struct btrfs_fs_info *fs_info, 535da61d31aSJosef Bacik struct btrfs_path *path, u64 time_seq, 536ed58f2e6Sethanwu struct preftrees *preftrees, 537e0c476b1SJeff Mahoney struct prelim_ref *ref, struct ulist *parents, 538b25b0b87Sethanwu const u64 *extent_item_pos, bool ignore_offset) 5398da6d581SJan Schmidt { 5408da6d581SJan Schmidt struct btrfs_root *root; 5418da6d581SJan Schmidt struct extent_buffer *eb; 5428da6d581SJan Schmidt int ret = 0; 5438da6d581SJan Schmidt int root_level; 5448da6d581SJan Schmidt int level = ref->level; 5457ac8b88eSethanwu struct btrfs_key search_key = ref->key_for_search; 5468da6d581SJan Schmidt 54756e9357aSDavid Sterba root = btrfs_get_fs_root(fs_info, ref->root_id, false); 5488da6d581SJan Schmidt if (IS_ERR(root)) { 5498da6d581SJan Schmidt ret = PTR_ERR(root); 5509326f76fSJosef Bacik goto out_free; 5519326f76fSJosef Bacik } 5529326f76fSJosef Bacik 55339dba873SJosef Bacik if (!path->search_commit_root && 55439dba873SJosef Bacik test_bit(BTRFS_ROOT_DELETING, &root->state)) { 55539dba873SJosef Bacik ret = -ENOENT; 55639dba873SJosef Bacik goto out; 55739dba873SJosef Bacik } 55839dba873SJosef Bacik 559f5ee5c9aSJeff Mahoney if (btrfs_is_testing(fs_info)) { 560d9ee522bSJosef Bacik ret = -ENOENT; 561d9ee522bSJosef Bacik goto out; 562d9ee522bSJosef Bacik } 563d9ee522bSJosef Bacik 5649e351cc8SJosef Bacik if (path->search_commit_root) 5659e351cc8SJosef Bacik root_level = btrfs_header_level(root->commit_root); 566de47c9d3SEdmund Nadolski else if (time_seq == SEQ_LAST) 56721633fc6SQu Wenruo root_level = btrfs_header_level(root->node); 5689e351cc8SJosef Bacik else 5695b6602e7SJan Schmidt root_level = btrfs_old_root_level(root, time_seq); 5708da6d581SJan Schmidt 571c75e8394SJosef Bacik if (root_level + 1 == level) 5728da6d581SJan Schmidt goto out; 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 602ab8d0fc4SJeff Mahoney btrfs_debug(fs_info, 603ab8d0fc4SJeff Mahoney "search slot in root %llu (level %d, ref count %d) returned %d for key (%llu %u %llu)", 604c1c9ff7cSGeert Uytterhoeven ref->root_id, level, ref->count, ret, 605c1c9ff7cSGeert Uytterhoeven ref->key_for_search.objectid, ref->key_for_search.type, 606c1c9ff7cSGeert Uytterhoeven ref->key_for_search.offset); 6078da6d581SJan Schmidt if (ret < 0) 6088da6d581SJan Schmidt goto out; 6098da6d581SJan Schmidt 6108da6d581SJan Schmidt eb = path->nodes[level]; 6119345457fSJan Schmidt while (!eb) { 612fae7f21cSDulshani Gunawardhana if (WARN_ON(!level)) { 6138da6d581SJan Schmidt ret = 1; 6148da6d581SJan Schmidt goto out; 6158da6d581SJan Schmidt } 6169345457fSJan Schmidt level--; 6179345457fSJan Schmidt eb = path->nodes[level]; 6189345457fSJan Schmidt } 6198da6d581SJan Schmidt 620ed58f2e6Sethanwu ret = add_all_parents(root, path, parents, preftrees, ref, level, 621b25b0b87Sethanwu time_seq, extent_item_pos, ignore_offset); 6228da6d581SJan Schmidt out: 62300246528SJosef Bacik btrfs_put_root(root); 6249326f76fSJosef Bacik out_free: 625da61d31aSJosef Bacik path->lowest_level = 0; 626da61d31aSJosef Bacik btrfs_release_path(path); 6278da6d581SJan Schmidt return ret; 6288da6d581SJan Schmidt } 6298da6d581SJan Schmidt 6304dae077aSJeff Mahoney static struct extent_inode_elem * 6314dae077aSJeff Mahoney unode_aux_to_inode_list(struct ulist_node *node) 6324dae077aSJeff Mahoney { 6334dae077aSJeff Mahoney if (!node) 6344dae077aSJeff Mahoney return NULL; 6354dae077aSJeff Mahoney return (struct extent_inode_elem *)(uintptr_t)node->aux; 6364dae077aSJeff Mahoney } 6374dae077aSJeff Mahoney 6388da6d581SJan Schmidt /* 63952042d8eSAndrea Gelmini * We maintain three separate rbtrees: one for direct refs, one for 64086d5f994SEdmund Nadolski * indirect refs which have a key, and one for indirect refs which do not 64186d5f994SEdmund Nadolski * have a key. Each tree does merge on insertion. 64286d5f994SEdmund Nadolski * 64386d5f994SEdmund Nadolski * Once all of the references are located, we iterate over the tree of 64486d5f994SEdmund Nadolski * indirect refs with missing keys. An appropriate key is located and 64586d5f994SEdmund Nadolski * the ref is moved onto the tree for indirect refs. After all missing 64686d5f994SEdmund Nadolski * keys are thus located, we iterate over the indirect ref tree, resolve 64786d5f994SEdmund Nadolski * each reference, and then insert the resolved reference onto the 64886d5f994SEdmund Nadolski * direct tree (merging there too). 64986d5f994SEdmund Nadolski * 65086d5f994SEdmund Nadolski * New backrefs (i.e., for parent nodes) are added to the appropriate 65186d5f994SEdmund Nadolski * rbtree as they are encountered. The new backrefs are subsequently 65286d5f994SEdmund Nadolski * resolved as above. 6538da6d581SJan Schmidt */ 654e0c476b1SJeff Mahoney static int resolve_indirect_refs(struct btrfs_fs_info *fs_info, 655da61d31aSJosef Bacik struct btrfs_path *path, u64 time_seq, 65686d5f994SEdmund Nadolski struct preftrees *preftrees, 657b25b0b87Sethanwu const u64 *extent_item_pos, 658c995ab3cSZygo Blaxell struct share_check *sc, bool ignore_offset) 6598da6d581SJan Schmidt { 6608da6d581SJan Schmidt int err; 6618da6d581SJan Schmidt int ret = 0; 6628da6d581SJan Schmidt struct ulist *parents; 6638da6d581SJan Schmidt struct ulist_node *node; 664cd1b413cSJan Schmidt struct ulist_iterator uiter; 66586d5f994SEdmund Nadolski struct rb_node *rnode; 6668da6d581SJan Schmidt 6678da6d581SJan Schmidt parents = ulist_alloc(GFP_NOFS); 6688da6d581SJan Schmidt if (!parents) 6698da6d581SJan Schmidt return -ENOMEM; 6708da6d581SJan Schmidt 6718da6d581SJan Schmidt /* 67286d5f994SEdmund Nadolski * We could trade memory usage for performance here by iterating 67386d5f994SEdmund Nadolski * the tree, allocating new refs for each insertion, and then 67486d5f994SEdmund Nadolski * freeing the entire indirect tree when we're done. In some test 67586d5f994SEdmund Nadolski * cases, the tree can grow quite large (~200k objects). 6768da6d581SJan Schmidt */ 677ecf160b4SLiu Bo while ((rnode = rb_first_cached(&preftrees->indirect.root))) { 67886d5f994SEdmund Nadolski struct prelim_ref *ref; 67986d5f994SEdmund Nadolski 68086d5f994SEdmund Nadolski ref = rb_entry(rnode, struct prelim_ref, rbnode); 68186d5f994SEdmund Nadolski if (WARN(ref->parent, 68286d5f994SEdmund Nadolski "BUG: direct ref found in indirect tree")) { 68386d5f994SEdmund Nadolski ret = -EINVAL; 68486d5f994SEdmund Nadolski goto out; 68586d5f994SEdmund Nadolski } 68686d5f994SEdmund Nadolski 687ecf160b4SLiu Bo rb_erase_cached(&ref->rbnode, &preftrees->indirect.root); 6886c336b21SJeff Mahoney preftrees->indirect.count--; 68986d5f994SEdmund Nadolski 69086d5f994SEdmund Nadolski if (ref->count == 0) { 69186d5f994SEdmund Nadolski free_pref(ref); 6928da6d581SJan Schmidt continue; 69386d5f994SEdmund Nadolski } 69486d5f994SEdmund Nadolski 6953ec4d323SEdmund Nadolski if (sc && sc->root_objectid && 6963ec4d323SEdmund Nadolski ref->root_id != sc->root_objectid) { 69786d5f994SEdmund Nadolski free_pref(ref); 698dc046b10SJosef Bacik ret = BACKREF_FOUND_SHARED; 699dc046b10SJosef Bacik goto out; 700dc046b10SJosef Bacik } 701ed58f2e6Sethanwu err = resolve_indirect_ref(fs_info, path, time_seq, preftrees, 702ed58f2e6Sethanwu ref, parents, extent_item_pos, 703b25b0b87Sethanwu ignore_offset); 70495def2edSWang Shilong /* 70595def2edSWang Shilong * we can only tolerate ENOENT,otherwise,we should catch error 70695def2edSWang Shilong * and return directly. 70795def2edSWang Shilong */ 70895def2edSWang Shilong if (err == -ENOENT) { 7093ec4d323SEdmund Nadolski prelim_ref_insert(fs_info, &preftrees->direct, ref, 7103ec4d323SEdmund Nadolski NULL); 7118da6d581SJan Schmidt continue; 71295def2edSWang Shilong } else if (err) { 71386d5f994SEdmund Nadolski free_pref(ref); 71495def2edSWang Shilong ret = err; 71595def2edSWang Shilong goto out; 71695def2edSWang Shilong } 7178da6d581SJan Schmidt 7188da6d581SJan Schmidt /* we put the first parent into the ref at hand */ 719cd1b413cSJan Schmidt ULIST_ITER_INIT(&uiter); 720cd1b413cSJan Schmidt node = ulist_next(parents, &uiter); 7218da6d581SJan Schmidt ref->parent = node ? node->val : 0; 7224dae077aSJeff Mahoney ref->inode_list = unode_aux_to_inode_list(node); 7238da6d581SJan Schmidt 72486d5f994SEdmund Nadolski /* Add a prelim_ref(s) for any other parent(s). */ 725cd1b413cSJan Schmidt while ((node = ulist_next(parents, &uiter))) { 72686d5f994SEdmund Nadolski struct prelim_ref *new_ref; 72786d5f994SEdmund Nadolski 728b9e9a6cbSWang Shilong new_ref = kmem_cache_alloc(btrfs_prelim_ref_cache, 729b9e9a6cbSWang Shilong GFP_NOFS); 7308da6d581SJan Schmidt if (!new_ref) { 73186d5f994SEdmund Nadolski free_pref(ref); 7328da6d581SJan Schmidt ret = -ENOMEM; 733e36902d4SWang Shilong goto out; 7348da6d581SJan Schmidt } 7358da6d581SJan Schmidt memcpy(new_ref, ref, sizeof(*ref)); 7368da6d581SJan Schmidt new_ref->parent = node->val; 7374dae077aSJeff Mahoney new_ref->inode_list = unode_aux_to_inode_list(node); 7383ec4d323SEdmund Nadolski prelim_ref_insert(fs_info, &preftrees->direct, 7393ec4d323SEdmund Nadolski new_ref, NULL); 7408da6d581SJan Schmidt } 74186d5f994SEdmund Nadolski 7423ec4d323SEdmund Nadolski /* 74352042d8eSAndrea Gelmini * Now it's a direct ref, put it in the direct tree. We must 7443ec4d323SEdmund Nadolski * do this last because the ref could be merged/freed here. 7453ec4d323SEdmund Nadolski */ 7463ec4d323SEdmund Nadolski prelim_ref_insert(fs_info, &preftrees->direct, ref, NULL); 74786d5f994SEdmund Nadolski 7488da6d581SJan Schmidt ulist_reinit(parents); 7499dd14fd6SEdmund Nadolski cond_resched(); 7508da6d581SJan Schmidt } 751e36902d4SWang Shilong out: 7528da6d581SJan Schmidt ulist_free(parents); 7538da6d581SJan Schmidt return ret; 7548da6d581SJan Schmidt } 7558da6d581SJan Schmidt 756d5c88b73SJan Schmidt /* 757d5c88b73SJan Schmidt * read tree blocks and add keys where required. 758d5c88b73SJan Schmidt */ 759e0c476b1SJeff Mahoney static int add_missing_keys(struct btrfs_fs_info *fs_info, 76038e3eebfSJosef Bacik struct preftrees *preftrees, bool lock) 761d5c88b73SJan Schmidt { 762e0c476b1SJeff Mahoney struct prelim_ref *ref; 763d5c88b73SJan Schmidt struct extent_buffer *eb; 76486d5f994SEdmund Nadolski struct preftree *tree = &preftrees->indirect_missing_keys; 76586d5f994SEdmund Nadolski struct rb_node *node; 766d5c88b73SJan Schmidt 767ecf160b4SLiu Bo while ((node = rb_first_cached(&tree->root))) { 76886d5f994SEdmund Nadolski ref = rb_entry(node, struct prelim_ref, rbnode); 769ecf160b4SLiu Bo rb_erase_cached(node, &tree->root); 77086d5f994SEdmund Nadolski 77186d5f994SEdmund Nadolski BUG_ON(ref->parent); /* should not be a direct ref */ 77286d5f994SEdmund Nadolski BUG_ON(ref->key_for_search.type); 773d5c88b73SJan Schmidt BUG_ON(!ref->wanted_disk_byte); 77486d5f994SEdmund Nadolski 775581c1760SQu Wenruo eb = read_tree_block(fs_info, ref->wanted_disk_byte, 0, 776581c1760SQu Wenruo ref->level - 1, NULL); 77764c043deSLiu Bo if (IS_ERR(eb)) { 77886d5f994SEdmund Nadolski free_pref(ref); 77964c043deSLiu Bo return PTR_ERR(eb); 78064c043deSLiu Bo } else if (!extent_buffer_uptodate(eb)) { 78186d5f994SEdmund Nadolski free_pref(ref); 782416bc658SJosef Bacik free_extent_buffer(eb); 783416bc658SJosef Bacik return -EIO; 784416bc658SJosef Bacik } 78538e3eebfSJosef Bacik if (lock) 786d5c88b73SJan Schmidt btrfs_tree_read_lock(eb); 787d5c88b73SJan Schmidt if (btrfs_header_level(eb) == 0) 788d5c88b73SJan Schmidt btrfs_item_key_to_cpu(eb, &ref->key_for_search, 0); 789d5c88b73SJan Schmidt else 790d5c88b73SJan Schmidt btrfs_node_key_to_cpu(eb, &ref->key_for_search, 0); 79138e3eebfSJosef Bacik if (lock) 792d5c88b73SJan Schmidt btrfs_tree_read_unlock(eb); 793d5c88b73SJan Schmidt free_extent_buffer(eb); 7943ec4d323SEdmund Nadolski prelim_ref_insert(fs_info, &preftrees->indirect, ref, NULL); 7959dd14fd6SEdmund Nadolski cond_resched(); 796d5c88b73SJan Schmidt } 797d5c88b73SJan Schmidt return 0; 798d5c88b73SJan Schmidt } 799d5c88b73SJan Schmidt 8008da6d581SJan Schmidt /* 8018da6d581SJan Schmidt * add all currently queued delayed refs from this head whose seq nr is 8028da6d581SJan Schmidt * smaller or equal that seq to the list 8038da6d581SJan Schmidt */ 80400142756SJeff Mahoney static int add_delayed_refs(const struct btrfs_fs_info *fs_info, 80500142756SJeff Mahoney struct btrfs_delayed_ref_head *head, u64 seq, 806b25b0b87Sethanwu struct preftrees *preftrees, struct share_check *sc) 8078da6d581SJan Schmidt { 808c6fc2454SQu Wenruo struct btrfs_delayed_ref_node *node; 8098da6d581SJan Schmidt struct btrfs_delayed_extent_op *extent_op = head->extent_op; 810d5c88b73SJan Schmidt struct btrfs_key key; 81186d5f994SEdmund Nadolski struct btrfs_key tmp_op_key; 8120e0adbcfSJosef Bacik struct rb_node *n; 81301747e92SEdmund Nadolski int count; 814b1375d64SJan Schmidt int ret = 0; 8158da6d581SJan Schmidt 816a6dbceafSNikolay Borisov if (extent_op && extent_op->update_key) 81786d5f994SEdmund Nadolski btrfs_disk_key_to_cpu(&tmp_op_key, &extent_op->key); 8188da6d581SJan Schmidt 819d7df2c79SJosef Bacik spin_lock(&head->lock); 820e3d03965SLiu Bo for (n = rb_first_cached(&head->ref_tree); n; n = rb_next(n)) { 8210e0adbcfSJosef Bacik node = rb_entry(n, struct btrfs_delayed_ref_node, 8220e0adbcfSJosef Bacik ref_node); 8238da6d581SJan Schmidt if (node->seq > seq) 8248da6d581SJan Schmidt continue; 8258da6d581SJan Schmidt 8268da6d581SJan Schmidt switch (node->action) { 8278da6d581SJan Schmidt case BTRFS_ADD_DELAYED_EXTENT: 8288da6d581SJan Schmidt case BTRFS_UPDATE_DELAYED_HEAD: 8298da6d581SJan Schmidt WARN_ON(1); 8308da6d581SJan Schmidt continue; 8318da6d581SJan Schmidt case BTRFS_ADD_DELAYED_REF: 83201747e92SEdmund Nadolski count = node->ref_mod; 8338da6d581SJan Schmidt break; 8348da6d581SJan Schmidt case BTRFS_DROP_DELAYED_REF: 83501747e92SEdmund Nadolski count = node->ref_mod * -1; 8368da6d581SJan Schmidt break; 8378da6d581SJan Schmidt default: 838290342f6SArnd Bergmann BUG(); 8398da6d581SJan Schmidt } 8408da6d581SJan Schmidt switch (node->type) { 8418da6d581SJan Schmidt case BTRFS_TREE_BLOCK_REF_KEY: { 84286d5f994SEdmund Nadolski /* NORMAL INDIRECT METADATA backref */ 8438da6d581SJan Schmidt struct btrfs_delayed_tree_ref *ref; 8448da6d581SJan Schmidt 8458da6d581SJan Schmidt ref = btrfs_delayed_node_to_tree_ref(node); 84600142756SJeff Mahoney ret = add_indirect_ref(fs_info, preftrees, ref->root, 84700142756SJeff Mahoney &tmp_op_key, ref->level + 1, 84801747e92SEdmund Nadolski node->bytenr, count, sc, 84901747e92SEdmund Nadolski GFP_ATOMIC); 8508da6d581SJan Schmidt break; 8518da6d581SJan Schmidt } 8528da6d581SJan Schmidt case BTRFS_SHARED_BLOCK_REF_KEY: { 85386d5f994SEdmund Nadolski /* SHARED DIRECT METADATA backref */ 8548da6d581SJan Schmidt struct btrfs_delayed_tree_ref *ref; 8558da6d581SJan Schmidt 8568da6d581SJan Schmidt ref = btrfs_delayed_node_to_tree_ref(node); 85786d5f994SEdmund Nadolski 85801747e92SEdmund Nadolski ret = add_direct_ref(fs_info, preftrees, ref->level + 1, 85901747e92SEdmund Nadolski ref->parent, node->bytenr, count, 8603ec4d323SEdmund Nadolski sc, GFP_ATOMIC); 8618da6d581SJan Schmidt break; 8628da6d581SJan Schmidt } 8638da6d581SJan Schmidt case BTRFS_EXTENT_DATA_REF_KEY: { 86486d5f994SEdmund Nadolski /* NORMAL INDIRECT DATA backref */ 8658da6d581SJan Schmidt struct btrfs_delayed_data_ref *ref; 8668da6d581SJan Schmidt ref = btrfs_delayed_node_to_data_ref(node); 8678da6d581SJan Schmidt 8688da6d581SJan Schmidt key.objectid = ref->objectid; 8698da6d581SJan Schmidt key.type = BTRFS_EXTENT_DATA_KEY; 8708da6d581SJan Schmidt key.offset = ref->offset; 871dc046b10SJosef Bacik 872dc046b10SJosef Bacik /* 873dc046b10SJosef Bacik * Found a inum that doesn't match our known inum, we 874dc046b10SJosef Bacik * know it's shared. 875dc046b10SJosef Bacik */ 8763ec4d323SEdmund Nadolski if (sc && sc->inum && ref->objectid != sc->inum) { 877dc046b10SJosef Bacik ret = BACKREF_FOUND_SHARED; 8783ec4d323SEdmund Nadolski goto out; 879dc046b10SJosef Bacik } 880dc046b10SJosef Bacik 88100142756SJeff Mahoney ret = add_indirect_ref(fs_info, preftrees, ref->root, 88201747e92SEdmund Nadolski &key, 0, node->bytenr, count, sc, 88301747e92SEdmund Nadolski GFP_ATOMIC); 8848da6d581SJan Schmidt break; 8858da6d581SJan Schmidt } 8868da6d581SJan Schmidt case BTRFS_SHARED_DATA_REF_KEY: { 88786d5f994SEdmund Nadolski /* SHARED DIRECT FULL backref */ 8888da6d581SJan Schmidt struct btrfs_delayed_data_ref *ref; 8898da6d581SJan Schmidt 8908da6d581SJan Schmidt ref = btrfs_delayed_node_to_data_ref(node); 89186d5f994SEdmund Nadolski 89201747e92SEdmund Nadolski ret = add_direct_ref(fs_info, preftrees, 0, ref->parent, 89301747e92SEdmund Nadolski node->bytenr, count, sc, 89401747e92SEdmund Nadolski GFP_ATOMIC); 8958da6d581SJan Schmidt break; 8968da6d581SJan Schmidt } 8978da6d581SJan Schmidt default: 8988da6d581SJan Schmidt WARN_ON(1); 8998da6d581SJan Schmidt } 9003ec4d323SEdmund Nadolski /* 9013ec4d323SEdmund Nadolski * We must ignore BACKREF_FOUND_SHARED until all delayed 9023ec4d323SEdmund Nadolski * refs have been checked. 9033ec4d323SEdmund Nadolski */ 9043ec4d323SEdmund Nadolski if (ret && (ret != BACKREF_FOUND_SHARED)) 905d7df2c79SJosef Bacik break; 9068da6d581SJan Schmidt } 9073ec4d323SEdmund Nadolski if (!ret) 9083ec4d323SEdmund Nadolski ret = extent_is_shared(sc); 9093ec4d323SEdmund Nadolski out: 910d7df2c79SJosef Bacik spin_unlock(&head->lock); 911d7df2c79SJosef Bacik return ret; 9128da6d581SJan Schmidt } 9138da6d581SJan Schmidt 9148da6d581SJan Schmidt /* 9158da6d581SJan Schmidt * add all inline backrefs for bytenr to the list 9163ec4d323SEdmund Nadolski * 9173ec4d323SEdmund Nadolski * Returns 0 on success, <0 on error, or BACKREF_FOUND_SHARED. 9188da6d581SJan Schmidt */ 91900142756SJeff Mahoney static int add_inline_refs(const struct btrfs_fs_info *fs_info, 92000142756SJeff Mahoney struct btrfs_path *path, u64 bytenr, 92186d5f994SEdmund Nadolski int *info_level, struct preftrees *preftrees, 922b25b0b87Sethanwu struct share_check *sc) 9238da6d581SJan Schmidt { 924b1375d64SJan Schmidt int ret = 0; 9258da6d581SJan Schmidt int slot; 9268da6d581SJan Schmidt struct extent_buffer *leaf; 9278da6d581SJan Schmidt struct btrfs_key key; 928261c84b6SJosef Bacik struct btrfs_key found_key; 9298da6d581SJan Schmidt unsigned long ptr; 9308da6d581SJan Schmidt unsigned long end; 9318da6d581SJan Schmidt struct btrfs_extent_item *ei; 9328da6d581SJan Schmidt u64 flags; 9338da6d581SJan Schmidt u64 item_size; 9348da6d581SJan Schmidt 9358da6d581SJan Schmidt /* 9368da6d581SJan Schmidt * enumerate all inline refs 9378da6d581SJan Schmidt */ 9388da6d581SJan Schmidt leaf = path->nodes[0]; 939dadcaf78SJan Schmidt slot = path->slots[0]; 9408da6d581SJan Schmidt 9418da6d581SJan Schmidt item_size = btrfs_item_size_nr(leaf, slot); 9428da6d581SJan Schmidt BUG_ON(item_size < sizeof(*ei)); 9438da6d581SJan Schmidt 9448da6d581SJan Schmidt ei = btrfs_item_ptr(leaf, slot, struct btrfs_extent_item); 9458da6d581SJan Schmidt flags = btrfs_extent_flags(leaf, ei); 946261c84b6SJosef Bacik btrfs_item_key_to_cpu(leaf, &found_key, slot); 9478da6d581SJan Schmidt 9488da6d581SJan Schmidt ptr = (unsigned long)(ei + 1); 9498da6d581SJan Schmidt end = (unsigned long)ei + item_size; 9508da6d581SJan Schmidt 951261c84b6SJosef Bacik if (found_key.type == BTRFS_EXTENT_ITEM_KEY && 952261c84b6SJosef Bacik flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) { 9538da6d581SJan Schmidt struct btrfs_tree_block_info *info; 9548da6d581SJan Schmidt 9558da6d581SJan Schmidt info = (struct btrfs_tree_block_info *)ptr; 9568da6d581SJan Schmidt *info_level = btrfs_tree_block_level(leaf, info); 9578da6d581SJan Schmidt ptr += sizeof(struct btrfs_tree_block_info); 9588da6d581SJan Schmidt BUG_ON(ptr > end); 959261c84b6SJosef Bacik } else if (found_key.type == BTRFS_METADATA_ITEM_KEY) { 960261c84b6SJosef Bacik *info_level = found_key.offset; 9618da6d581SJan Schmidt } else { 9628da6d581SJan Schmidt BUG_ON(!(flags & BTRFS_EXTENT_FLAG_DATA)); 9638da6d581SJan Schmidt } 9648da6d581SJan Schmidt 9658da6d581SJan Schmidt while (ptr < end) { 9668da6d581SJan Schmidt struct btrfs_extent_inline_ref *iref; 9678da6d581SJan Schmidt u64 offset; 9688da6d581SJan Schmidt int type; 9698da6d581SJan Schmidt 9708da6d581SJan Schmidt iref = (struct btrfs_extent_inline_ref *)ptr; 9713de28d57SLiu Bo type = btrfs_get_extent_inline_ref_type(leaf, iref, 9723de28d57SLiu Bo BTRFS_REF_TYPE_ANY); 9733de28d57SLiu Bo if (type == BTRFS_REF_TYPE_INVALID) 974af431dcbSSu Yue return -EUCLEAN; 9753de28d57SLiu Bo 9768da6d581SJan Schmidt offset = btrfs_extent_inline_ref_offset(leaf, iref); 9778da6d581SJan Schmidt 9788da6d581SJan Schmidt switch (type) { 9798da6d581SJan Schmidt case BTRFS_SHARED_BLOCK_REF_KEY: 98000142756SJeff Mahoney ret = add_direct_ref(fs_info, preftrees, 98100142756SJeff Mahoney *info_level + 1, offset, 9823ec4d323SEdmund Nadolski bytenr, 1, NULL, GFP_NOFS); 9838da6d581SJan Schmidt break; 9848da6d581SJan Schmidt case BTRFS_SHARED_DATA_REF_KEY: { 9858da6d581SJan Schmidt struct btrfs_shared_data_ref *sdref; 9868da6d581SJan Schmidt int count; 9878da6d581SJan Schmidt 9888da6d581SJan Schmidt sdref = (struct btrfs_shared_data_ref *)(iref + 1); 9898da6d581SJan Schmidt count = btrfs_shared_data_ref_count(leaf, sdref); 99086d5f994SEdmund Nadolski 99100142756SJeff Mahoney ret = add_direct_ref(fs_info, preftrees, 0, offset, 9923ec4d323SEdmund Nadolski bytenr, count, sc, GFP_NOFS); 9938da6d581SJan Schmidt break; 9948da6d581SJan Schmidt } 9958da6d581SJan Schmidt case BTRFS_TREE_BLOCK_REF_KEY: 99600142756SJeff Mahoney ret = add_indirect_ref(fs_info, preftrees, offset, 99700142756SJeff Mahoney NULL, *info_level + 1, 9983ec4d323SEdmund Nadolski bytenr, 1, NULL, GFP_NOFS); 9998da6d581SJan Schmidt break; 10008da6d581SJan Schmidt case BTRFS_EXTENT_DATA_REF_KEY: { 10018da6d581SJan Schmidt struct btrfs_extent_data_ref *dref; 10028da6d581SJan Schmidt int count; 10038da6d581SJan Schmidt u64 root; 10048da6d581SJan Schmidt 10058da6d581SJan Schmidt dref = (struct btrfs_extent_data_ref *)(&iref->offset); 10068da6d581SJan Schmidt count = btrfs_extent_data_ref_count(leaf, dref); 10078da6d581SJan Schmidt key.objectid = btrfs_extent_data_ref_objectid(leaf, 10088da6d581SJan Schmidt dref); 10098da6d581SJan Schmidt key.type = BTRFS_EXTENT_DATA_KEY; 10108da6d581SJan Schmidt key.offset = btrfs_extent_data_ref_offset(leaf, dref); 1011dc046b10SJosef Bacik 10123ec4d323SEdmund Nadolski if (sc && sc->inum && key.objectid != sc->inum) { 1013dc046b10SJosef Bacik ret = BACKREF_FOUND_SHARED; 1014dc046b10SJosef Bacik break; 1015dc046b10SJosef Bacik } 1016dc046b10SJosef Bacik 10178da6d581SJan Schmidt root = btrfs_extent_data_ref_root(leaf, dref); 101886d5f994SEdmund Nadolski 101900142756SJeff Mahoney ret = add_indirect_ref(fs_info, preftrees, root, 102000142756SJeff Mahoney &key, 0, bytenr, count, 10213ec4d323SEdmund Nadolski sc, GFP_NOFS); 10228da6d581SJan Schmidt break; 10238da6d581SJan Schmidt } 10248da6d581SJan Schmidt default: 10258da6d581SJan Schmidt WARN_ON(1); 10268da6d581SJan Schmidt } 10271149ab6bSWang Shilong if (ret) 10281149ab6bSWang Shilong return ret; 10298da6d581SJan Schmidt ptr += btrfs_extent_inline_ref_size(type); 10308da6d581SJan Schmidt } 10318da6d581SJan Schmidt 10328da6d581SJan Schmidt return 0; 10338da6d581SJan Schmidt } 10348da6d581SJan Schmidt 10358da6d581SJan Schmidt /* 10368da6d581SJan Schmidt * add all non-inline backrefs for bytenr to the list 10373ec4d323SEdmund Nadolski * 10383ec4d323SEdmund Nadolski * Returns 0 on success, <0 on error, or BACKREF_FOUND_SHARED. 10398da6d581SJan Schmidt */ 1040e0c476b1SJeff Mahoney static int add_keyed_refs(struct btrfs_fs_info *fs_info, 10418da6d581SJan Schmidt struct btrfs_path *path, u64 bytenr, 104286d5f994SEdmund Nadolski int info_level, struct preftrees *preftrees, 10433ec4d323SEdmund Nadolski struct share_check *sc) 10448da6d581SJan Schmidt { 10458da6d581SJan Schmidt struct btrfs_root *extent_root = fs_info->extent_root; 10468da6d581SJan Schmidt int ret; 10478da6d581SJan Schmidt int slot; 10488da6d581SJan Schmidt struct extent_buffer *leaf; 10498da6d581SJan Schmidt struct btrfs_key key; 10508da6d581SJan Schmidt 10518da6d581SJan Schmidt while (1) { 10528da6d581SJan Schmidt ret = btrfs_next_item(extent_root, path); 10538da6d581SJan Schmidt if (ret < 0) 10548da6d581SJan Schmidt break; 10558da6d581SJan Schmidt if (ret) { 10568da6d581SJan Schmidt ret = 0; 10578da6d581SJan Schmidt break; 10588da6d581SJan Schmidt } 10598da6d581SJan Schmidt 10608da6d581SJan Schmidt slot = path->slots[0]; 10618da6d581SJan Schmidt leaf = path->nodes[0]; 10628da6d581SJan Schmidt btrfs_item_key_to_cpu(leaf, &key, slot); 10638da6d581SJan Schmidt 10648da6d581SJan Schmidt if (key.objectid != bytenr) 10658da6d581SJan Schmidt break; 10668da6d581SJan Schmidt if (key.type < BTRFS_TREE_BLOCK_REF_KEY) 10678da6d581SJan Schmidt continue; 10688da6d581SJan Schmidt if (key.type > BTRFS_SHARED_DATA_REF_KEY) 10698da6d581SJan Schmidt break; 10708da6d581SJan Schmidt 10718da6d581SJan Schmidt switch (key.type) { 10728da6d581SJan Schmidt case BTRFS_SHARED_BLOCK_REF_KEY: 107386d5f994SEdmund Nadolski /* SHARED DIRECT METADATA backref */ 107400142756SJeff Mahoney ret = add_direct_ref(fs_info, preftrees, 107500142756SJeff Mahoney info_level + 1, key.offset, 10763ec4d323SEdmund Nadolski bytenr, 1, NULL, GFP_NOFS); 10778da6d581SJan Schmidt break; 10788da6d581SJan Schmidt case BTRFS_SHARED_DATA_REF_KEY: { 107986d5f994SEdmund Nadolski /* SHARED DIRECT FULL backref */ 10808da6d581SJan Schmidt struct btrfs_shared_data_ref *sdref; 10818da6d581SJan Schmidt int count; 10828da6d581SJan Schmidt 10838da6d581SJan Schmidt sdref = btrfs_item_ptr(leaf, slot, 10848da6d581SJan Schmidt struct btrfs_shared_data_ref); 10858da6d581SJan Schmidt count = btrfs_shared_data_ref_count(leaf, sdref); 108600142756SJeff Mahoney ret = add_direct_ref(fs_info, preftrees, 0, 108700142756SJeff Mahoney key.offset, bytenr, count, 10883ec4d323SEdmund Nadolski sc, GFP_NOFS); 10898da6d581SJan Schmidt break; 10908da6d581SJan Schmidt } 10918da6d581SJan Schmidt case BTRFS_TREE_BLOCK_REF_KEY: 109286d5f994SEdmund Nadolski /* NORMAL INDIRECT METADATA backref */ 109300142756SJeff Mahoney ret = add_indirect_ref(fs_info, preftrees, key.offset, 109400142756SJeff Mahoney NULL, info_level + 1, bytenr, 10953ec4d323SEdmund Nadolski 1, NULL, GFP_NOFS); 10968da6d581SJan Schmidt break; 10978da6d581SJan Schmidt case BTRFS_EXTENT_DATA_REF_KEY: { 109886d5f994SEdmund Nadolski /* NORMAL INDIRECT DATA backref */ 10998da6d581SJan Schmidt struct btrfs_extent_data_ref *dref; 11008da6d581SJan Schmidt int count; 11018da6d581SJan Schmidt u64 root; 11028da6d581SJan Schmidt 11038da6d581SJan Schmidt dref = btrfs_item_ptr(leaf, slot, 11048da6d581SJan Schmidt struct btrfs_extent_data_ref); 11058da6d581SJan Schmidt count = btrfs_extent_data_ref_count(leaf, dref); 11068da6d581SJan Schmidt key.objectid = btrfs_extent_data_ref_objectid(leaf, 11078da6d581SJan Schmidt dref); 11088da6d581SJan Schmidt key.type = BTRFS_EXTENT_DATA_KEY; 11098da6d581SJan Schmidt key.offset = btrfs_extent_data_ref_offset(leaf, dref); 1110dc046b10SJosef Bacik 11113ec4d323SEdmund Nadolski if (sc && sc->inum && key.objectid != sc->inum) { 1112dc046b10SJosef Bacik ret = BACKREF_FOUND_SHARED; 1113dc046b10SJosef Bacik break; 1114dc046b10SJosef Bacik } 1115dc046b10SJosef Bacik 11168da6d581SJan Schmidt root = btrfs_extent_data_ref_root(leaf, dref); 111700142756SJeff Mahoney ret = add_indirect_ref(fs_info, preftrees, root, 111800142756SJeff Mahoney &key, 0, bytenr, count, 11193ec4d323SEdmund Nadolski sc, GFP_NOFS); 11208da6d581SJan Schmidt break; 11218da6d581SJan Schmidt } 11228da6d581SJan Schmidt default: 11238da6d581SJan Schmidt WARN_ON(1); 11248da6d581SJan Schmidt } 11251149ab6bSWang Shilong if (ret) 11261149ab6bSWang Shilong return ret; 11271149ab6bSWang Shilong 11288da6d581SJan Schmidt } 11298da6d581SJan Schmidt 11308da6d581SJan Schmidt return ret; 11318da6d581SJan Schmidt } 11328da6d581SJan Schmidt 11338da6d581SJan Schmidt /* 11348da6d581SJan Schmidt * this adds all existing backrefs (inline backrefs, backrefs and delayed 11358da6d581SJan Schmidt * refs) for the given bytenr to the refs list, merges duplicates and resolves 11368da6d581SJan Schmidt * indirect refs to their parent bytenr. 11378da6d581SJan Schmidt * When roots are found, they're added to the roots list 11388da6d581SJan Schmidt * 1139de47c9d3SEdmund Nadolski * If time_seq is set to SEQ_LAST, it will not search delayed_refs, and behave 114021633fc6SQu Wenruo * much like trans == NULL case, the difference only lies in it will not 114121633fc6SQu Wenruo * commit root. 114221633fc6SQu Wenruo * The special case is for qgroup to search roots in commit_transaction(). 114321633fc6SQu Wenruo * 11443ec4d323SEdmund Nadolski * @sc - if !NULL, then immediately return BACKREF_FOUND_SHARED when a 11453ec4d323SEdmund Nadolski * shared extent is detected. 11463ec4d323SEdmund Nadolski * 11473ec4d323SEdmund Nadolski * Otherwise this returns 0 for success and <0 for an error. 11483ec4d323SEdmund Nadolski * 1149c995ab3cSZygo Blaxell * If ignore_offset is set to false, only extent refs whose offsets match 1150c995ab3cSZygo Blaxell * extent_item_pos are returned. If true, every extent ref is returned 1151c995ab3cSZygo Blaxell * and extent_item_pos is ignored. 1152c995ab3cSZygo Blaxell * 11538da6d581SJan Schmidt * FIXME some caching might speed things up 11548da6d581SJan Schmidt */ 11558da6d581SJan Schmidt static int find_parent_nodes(struct btrfs_trans_handle *trans, 11568da6d581SJan Schmidt struct btrfs_fs_info *fs_info, u64 bytenr, 1157097b8a7cSJan Schmidt u64 time_seq, struct ulist *refs, 1158dc046b10SJosef Bacik struct ulist *roots, const u64 *extent_item_pos, 1159c995ab3cSZygo Blaxell struct share_check *sc, bool ignore_offset) 11608da6d581SJan Schmidt { 11618da6d581SJan Schmidt struct btrfs_key key; 11628da6d581SJan Schmidt struct btrfs_path *path; 11638da6d581SJan Schmidt struct btrfs_delayed_ref_root *delayed_refs = NULL; 1164d3b01064SLi Zefan struct btrfs_delayed_ref_head *head; 11658da6d581SJan Schmidt int info_level = 0; 11668da6d581SJan Schmidt int ret; 1167e0c476b1SJeff Mahoney struct prelim_ref *ref; 116886d5f994SEdmund Nadolski struct rb_node *node; 1169f05c4746SWang Shilong struct extent_inode_elem *eie = NULL; 117086d5f994SEdmund Nadolski struct preftrees preftrees = { 117186d5f994SEdmund Nadolski .direct = PREFTREE_INIT, 117286d5f994SEdmund Nadolski .indirect = PREFTREE_INIT, 117386d5f994SEdmund Nadolski .indirect_missing_keys = PREFTREE_INIT 117486d5f994SEdmund Nadolski }; 11758da6d581SJan Schmidt 11768da6d581SJan Schmidt key.objectid = bytenr; 11778da6d581SJan Schmidt key.offset = (u64)-1; 1178261c84b6SJosef Bacik if (btrfs_fs_incompat(fs_info, SKINNY_METADATA)) 1179261c84b6SJosef Bacik key.type = BTRFS_METADATA_ITEM_KEY; 1180261c84b6SJosef Bacik else 1181261c84b6SJosef Bacik key.type = BTRFS_EXTENT_ITEM_KEY; 11828da6d581SJan Schmidt 11838da6d581SJan Schmidt path = btrfs_alloc_path(); 11848da6d581SJan Schmidt if (!path) 11858da6d581SJan Schmidt return -ENOMEM; 1186e84752d4SWang Shilong if (!trans) { 1187da61d31aSJosef Bacik path->search_commit_root = 1; 1188e84752d4SWang Shilong path->skip_locking = 1; 1189e84752d4SWang Shilong } 11908da6d581SJan Schmidt 1191de47c9d3SEdmund Nadolski if (time_seq == SEQ_LAST) 119221633fc6SQu Wenruo path->skip_locking = 1; 119321633fc6SQu Wenruo 11948da6d581SJan Schmidt /* 11958da6d581SJan Schmidt * grab both a lock on the path and a lock on the delayed ref head. 11968da6d581SJan Schmidt * We need both to get a consistent picture of how the refs look 11978da6d581SJan Schmidt * at a specified point in time 11988da6d581SJan Schmidt */ 11998da6d581SJan Schmidt again: 1200d3b01064SLi Zefan head = NULL; 1201d3b01064SLi Zefan 12028da6d581SJan Schmidt ret = btrfs_search_slot(trans, fs_info->extent_root, &key, path, 0, 0); 12038da6d581SJan Schmidt if (ret < 0) 12048da6d581SJan Schmidt goto out; 12058da6d581SJan Schmidt BUG_ON(ret == 0); 12068da6d581SJan Schmidt 1207faa2dbf0SJosef Bacik #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS 120821633fc6SQu Wenruo if (trans && likely(trans->type != __TRANS_DUMMY) && 1209de47c9d3SEdmund Nadolski time_seq != SEQ_LAST) { 1210faa2dbf0SJosef Bacik #else 1211de47c9d3SEdmund Nadolski if (trans && time_seq != SEQ_LAST) { 1212faa2dbf0SJosef Bacik #endif 12138da6d581SJan Schmidt /* 12147a3ae2f8SJan Schmidt * look if there are updates for this ref queued and lock the 12157a3ae2f8SJan Schmidt * head 12168da6d581SJan Schmidt */ 12178da6d581SJan Schmidt delayed_refs = &trans->transaction->delayed_refs; 12188da6d581SJan Schmidt spin_lock(&delayed_refs->lock); 1219f72ad18eSLiu Bo head = btrfs_find_delayed_ref_head(delayed_refs, bytenr); 12208da6d581SJan Schmidt if (head) { 12218da6d581SJan Schmidt if (!mutex_trylock(&head->mutex)) { 1222d278850eSJosef Bacik refcount_inc(&head->refs); 12238da6d581SJan Schmidt spin_unlock(&delayed_refs->lock); 12248da6d581SJan Schmidt 12258da6d581SJan Schmidt btrfs_release_path(path); 12268da6d581SJan Schmidt 12278da6d581SJan Schmidt /* 12288da6d581SJan Schmidt * Mutex was contended, block until it's 12298da6d581SJan Schmidt * released and try again 12308da6d581SJan Schmidt */ 12318da6d581SJan Schmidt mutex_lock(&head->mutex); 12328da6d581SJan Schmidt mutex_unlock(&head->mutex); 1233d278850eSJosef Bacik btrfs_put_delayed_ref_head(head); 12348da6d581SJan Schmidt goto again; 12358da6d581SJan Schmidt } 1236d7df2c79SJosef Bacik spin_unlock(&delayed_refs->lock); 123700142756SJeff Mahoney ret = add_delayed_refs(fs_info, head, time_seq, 1238b25b0b87Sethanwu &preftrees, sc); 1239155725c9SJan Schmidt mutex_unlock(&head->mutex); 1240d7df2c79SJosef Bacik if (ret) 12418da6d581SJan Schmidt goto out; 1242d7df2c79SJosef Bacik } else { 12438da6d581SJan Schmidt spin_unlock(&delayed_refs->lock); 12447a3ae2f8SJan Schmidt } 1245d7df2c79SJosef Bacik } 12468da6d581SJan Schmidt 12478da6d581SJan Schmidt if (path->slots[0]) { 12488da6d581SJan Schmidt struct extent_buffer *leaf; 12498da6d581SJan Schmidt int slot; 12508da6d581SJan Schmidt 1251dadcaf78SJan Schmidt path->slots[0]--; 12528da6d581SJan Schmidt leaf = path->nodes[0]; 1253dadcaf78SJan Schmidt slot = path->slots[0]; 12548da6d581SJan Schmidt btrfs_item_key_to_cpu(leaf, &key, slot); 12558da6d581SJan Schmidt if (key.objectid == bytenr && 1256261c84b6SJosef Bacik (key.type == BTRFS_EXTENT_ITEM_KEY || 1257261c84b6SJosef Bacik key.type == BTRFS_METADATA_ITEM_KEY)) { 125800142756SJeff Mahoney ret = add_inline_refs(fs_info, path, bytenr, 1259b25b0b87Sethanwu &info_level, &preftrees, sc); 12608da6d581SJan Schmidt if (ret) 12618da6d581SJan Schmidt goto out; 1262e0c476b1SJeff Mahoney ret = add_keyed_refs(fs_info, path, bytenr, info_level, 12633ec4d323SEdmund Nadolski &preftrees, sc); 12648da6d581SJan Schmidt if (ret) 12658da6d581SJan Schmidt goto out; 12668da6d581SJan Schmidt } 12678da6d581SJan Schmidt } 126886d5f994SEdmund Nadolski 12698da6d581SJan Schmidt btrfs_release_path(path); 12708da6d581SJan Schmidt 127138e3eebfSJosef Bacik ret = add_missing_keys(fs_info, &preftrees, path->skip_locking == 0); 1272d5c88b73SJan Schmidt if (ret) 1273d5c88b73SJan Schmidt goto out; 1274d5c88b73SJan Schmidt 1275ecf160b4SLiu Bo WARN_ON(!RB_EMPTY_ROOT(&preftrees.indirect_missing_keys.root.rb_root)); 12768da6d581SJan Schmidt 127786d5f994SEdmund Nadolski ret = resolve_indirect_refs(fs_info, path, time_seq, &preftrees, 1278b25b0b87Sethanwu extent_item_pos, sc, ignore_offset); 12798da6d581SJan Schmidt if (ret) 12808da6d581SJan Schmidt goto out; 12818da6d581SJan Schmidt 1282ecf160b4SLiu Bo WARN_ON(!RB_EMPTY_ROOT(&preftrees.indirect.root.rb_root)); 12838da6d581SJan Schmidt 128486d5f994SEdmund Nadolski /* 128586d5f994SEdmund Nadolski * This walks the tree of merged and resolved refs. Tree blocks are 128686d5f994SEdmund Nadolski * read in as needed. Unique entries are added to the ulist, and 128786d5f994SEdmund Nadolski * the list of found roots is updated. 128886d5f994SEdmund Nadolski * 128986d5f994SEdmund Nadolski * We release the entire tree in one go before returning. 129086d5f994SEdmund Nadolski */ 1291ecf160b4SLiu Bo node = rb_first_cached(&preftrees.direct.root); 129286d5f994SEdmund Nadolski while (node) { 129386d5f994SEdmund Nadolski ref = rb_entry(node, struct prelim_ref, rbnode); 129486d5f994SEdmund Nadolski node = rb_next(&ref->rbnode); 1295c8195a7bSZygo Blaxell /* 1296c8195a7bSZygo Blaxell * ref->count < 0 can happen here if there are delayed 1297c8195a7bSZygo Blaxell * refs with a node->action of BTRFS_DROP_DELAYED_REF. 1298c8195a7bSZygo Blaxell * prelim_ref_insert() relies on this when merging 1299c8195a7bSZygo Blaxell * identical refs to keep the overall count correct. 1300c8195a7bSZygo Blaxell * prelim_ref_insert() will merge only those refs 1301c8195a7bSZygo Blaxell * which compare identically. Any refs having 1302c8195a7bSZygo Blaxell * e.g. different offsets would not be merged, 1303c8195a7bSZygo Blaxell * and would retain their original ref->count < 0. 1304c8195a7bSZygo Blaxell */ 130598cfee21SWang Shilong if (roots && ref->count && ref->root_id && ref->parent == 0) { 13063ec4d323SEdmund Nadolski if (sc && sc->root_objectid && 13073ec4d323SEdmund Nadolski ref->root_id != sc->root_objectid) { 1308dc046b10SJosef Bacik ret = BACKREF_FOUND_SHARED; 1309dc046b10SJosef Bacik goto out; 1310dc046b10SJosef Bacik } 1311dc046b10SJosef Bacik 13128da6d581SJan Schmidt /* no parent == root of tree */ 13138da6d581SJan Schmidt ret = ulist_add(roots, ref->root_id, 0, GFP_NOFS); 1314f1723939SWang Shilong if (ret < 0) 1315f1723939SWang Shilong goto out; 13168da6d581SJan Schmidt } 13178da6d581SJan Schmidt if (ref->count && ref->parent) { 13188a56457fSJosef Bacik if (extent_item_pos && !ref->inode_list && 13198a56457fSJosef Bacik ref->level == 0) { 1320976b1908SJan Schmidt struct extent_buffer *eb; 1321707e8a07SDavid Sterba 1322581c1760SQu Wenruo eb = read_tree_block(fs_info, ref->parent, 0, 1323581c1760SQu Wenruo ref->level, NULL); 132464c043deSLiu Bo if (IS_ERR(eb)) { 132564c043deSLiu Bo ret = PTR_ERR(eb); 132664c043deSLiu Bo goto out; 132764c043deSLiu Bo } else if (!extent_buffer_uptodate(eb)) { 1328416bc658SJosef Bacik free_extent_buffer(eb); 1329c16c2e2eSWang Shilong ret = -EIO; 1330c16c2e2eSWang Shilong goto out; 1331416bc658SJosef Bacik } 133238e3eebfSJosef Bacik 133338e3eebfSJosef Bacik if (!path->skip_locking) { 13346f7ff6d7SFilipe Manana btrfs_tree_read_lock(eb); 1335300aa896SDavid Sterba btrfs_set_lock_blocking_read(eb); 133638e3eebfSJosef Bacik } 1337976b1908SJan Schmidt ret = find_extent_in_eb(eb, bytenr, 1338c995ab3cSZygo Blaxell *extent_item_pos, &eie, ignore_offset); 133938e3eebfSJosef Bacik if (!path->skip_locking) 13406f7ff6d7SFilipe Manana btrfs_tree_read_unlock_blocking(eb); 1341976b1908SJan Schmidt free_extent_buffer(eb); 1342f5929cd8SFilipe David Borba Manana if (ret < 0) 1343f5929cd8SFilipe David Borba Manana goto out; 1344f5929cd8SFilipe David Borba Manana ref->inode_list = eie; 1345976b1908SJan Schmidt } 13464eb1f66dSTakashi Iwai ret = ulist_add_merge_ptr(refs, ref->parent, 13474eb1f66dSTakashi Iwai ref->inode_list, 13484eb1f66dSTakashi Iwai (void **)&eie, GFP_NOFS); 1349f1723939SWang Shilong if (ret < 0) 1350f1723939SWang Shilong goto out; 13513301958bSJan Schmidt if (!ret && extent_item_pos) { 13523301958bSJan Schmidt /* 13533301958bSJan Schmidt * we've recorded that parent, so we must extend 13543301958bSJan Schmidt * its inode list here 13553301958bSJan Schmidt */ 13563301958bSJan Schmidt BUG_ON(!eie); 13573301958bSJan Schmidt while (eie->next) 13583301958bSJan Schmidt eie = eie->next; 13593301958bSJan Schmidt eie->next = ref->inode_list; 13603301958bSJan Schmidt } 1361f05c4746SWang Shilong eie = NULL; 13628da6d581SJan Schmidt } 13639dd14fd6SEdmund Nadolski cond_resched(); 13648da6d581SJan Schmidt } 13658da6d581SJan Schmidt 13668da6d581SJan Schmidt out: 13678da6d581SJan Schmidt btrfs_free_path(path); 136886d5f994SEdmund Nadolski 136986d5f994SEdmund Nadolski prelim_release(&preftrees.direct); 137086d5f994SEdmund Nadolski prelim_release(&preftrees.indirect); 137186d5f994SEdmund Nadolski prelim_release(&preftrees.indirect_missing_keys); 137286d5f994SEdmund Nadolski 1373f05c4746SWang Shilong if (ret < 0) 1374f05c4746SWang Shilong free_inode_elem_list(eie); 13758da6d581SJan Schmidt return ret; 13768da6d581SJan Schmidt } 13778da6d581SJan Schmidt 1378976b1908SJan Schmidt static void free_leaf_list(struct ulist *blocks) 1379976b1908SJan Schmidt { 1380976b1908SJan Schmidt struct ulist_node *node = NULL; 1381976b1908SJan Schmidt struct extent_inode_elem *eie; 1382976b1908SJan Schmidt struct ulist_iterator uiter; 1383976b1908SJan Schmidt 1384976b1908SJan Schmidt ULIST_ITER_INIT(&uiter); 1385976b1908SJan Schmidt while ((node = ulist_next(blocks, &uiter))) { 1386976b1908SJan Schmidt if (!node->aux) 1387976b1908SJan Schmidt continue; 13884dae077aSJeff Mahoney eie = unode_aux_to_inode_list(node); 1389f05c4746SWang Shilong free_inode_elem_list(eie); 1390976b1908SJan Schmidt node->aux = 0; 1391976b1908SJan Schmidt } 1392976b1908SJan Schmidt 1393976b1908SJan Schmidt ulist_free(blocks); 1394976b1908SJan Schmidt } 1395976b1908SJan Schmidt 13968da6d581SJan Schmidt /* 13978da6d581SJan Schmidt * Finds all leafs with a reference to the specified combination of bytenr and 13988da6d581SJan Schmidt * offset. key_list_head will point to a list of corresponding keys (caller must 13998da6d581SJan Schmidt * free each list element). The leafs will be stored in the leafs ulist, which 14008da6d581SJan Schmidt * must be freed with ulist_free. 14018da6d581SJan Schmidt * 14028da6d581SJan Schmidt * returns 0 on success, <0 on error 14038da6d581SJan Schmidt */ 140419b546d7SQu Wenruo int btrfs_find_all_leafs(struct btrfs_trans_handle *trans, 14058da6d581SJan Schmidt struct btrfs_fs_info *fs_info, u64 bytenr, 1406097b8a7cSJan Schmidt u64 time_seq, struct ulist **leafs, 1407c995ab3cSZygo Blaxell const u64 *extent_item_pos, bool ignore_offset) 14088da6d581SJan Schmidt { 14098da6d581SJan Schmidt int ret; 14108da6d581SJan Schmidt 14118da6d581SJan Schmidt *leafs = ulist_alloc(GFP_NOFS); 141298cfee21SWang Shilong if (!*leafs) 14138da6d581SJan Schmidt return -ENOMEM; 14148da6d581SJan Schmidt 1415afce772eSLu Fengqi ret = find_parent_nodes(trans, fs_info, bytenr, time_seq, 1416c995ab3cSZygo Blaxell *leafs, NULL, extent_item_pos, NULL, ignore_offset); 14178da6d581SJan Schmidt if (ret < 0 && ret != -ENOENT) { 1418976b1908SJan Schmidt free_leaf_list(*leafs); 14198da6d581SJan Schmidt return ret; 14208da6d581SJan Schmidt } 14218da6d581SJan Schmidt 14228da6d581SJan Schmidt return 0; 14238da6d581SJan Schmidt } 14248da6d581SJan Schmidt 14258da6d581SJan Schmidt /* 14268da6d581SJan Schmidt * walk all backrefs for a given extent to find all roots that reference this 14278da6d581SJan Schmidt * extent. Walking a backref means finding all extents that reference this 14288da6d581SJan Schmidt * extent and in turn walk the backrefs of those, too. Naturally this is a 14298da6d581SJan Schmidt * recursive process, but here it is implemented in an iterative fashion: We 14308da6d581SJan Schmidt * find all referencing extents for the extent in question and put them on a 14318da6d581SJan Schmidt * list. In turn, we find all referencing extents for those, further appending 14328da6d581SJan Schmidt * to the list. The way we iterate the list allows adding more elements after 14338da6d581SJan Schmidt * the current while iterating. The process stops when we reach the end of the 14348da6d581SJan Schmidt * list. Found roots are added to the roots list. 14358da6d581SJan Schmidt * 14368da6d581SJan Schmidt * returns 0 on success, < 0 on error. 14378da6d581SJan Schmidt */ 1438e0c476b1SJeff Mahoney static int btrfs_find_all_roots_safe(struct btrfs_trans_handle *trans, 14398da6d581SJan Schmidt struct btrfs_fs_info *fs_info, u64 bytenr, 1440c995ab3cSZygo Blaxell u64 time_seq, struct ulist **roots, 1441c995ab3cSZygo Blaxell bool ignore_offset) 14428da6d581SJan Schmidt { 14438da6d581SJan Schmidt struct ulist *tmp; 14448da6d581SJan Schmidt struct ulist_node *node = NULL; 1445cd1b413cSJan Schmidt struct ulist_iterator uiter; 14468da6d581SJan Schmidt int ret; 14478da6d581SJan Schmidt 14488da6d581SJan Schmidt tmp = ulist_alloc(GFP_NOFS); 14498da6d581SJan Schmidt if (!tmp) 14508da6d581SJan Schmidt return -ENOMEM; 14518da6d581SJan Schmidt *roots = ulist_alloc(GFP_NOFS); 14528da6d581SJan Schmidt if (!*roots) { 14538da6d581SJan Schmidt ulist_free(tmp); 14548da6d581SJan Schmidt return -ENOMEM; 14558da6d581SJan Schmidt } 14568da6d581SJan Schmidt 1457cd1b413cSJan Schmidt ULIST_ITER_INIT(&uiter); 14588da6d581SJan Schmidt while (1) { 1459afce772eSLu Fengqi ret = find_parent_nodes(trans, fs_info, bytenr, time_seq, 1460c995ab3cSZygo Blaxell tmp, *roots, NULL, NULL, ignore_offset); 14618da6d581SJan Schmidt if (ret < 0 && ret != -ENOENT) { 14628da6d581SJan Schmidt ulist_free(tmp); 14638da6d581SJan Schmidt ulist_free(*roots); 1464*580c079bSFilipe Manana *roots = NULL; 14658da6d581SJan Schmidt return ret; 14668da6d581SJan Schmidt } 1467cd1b413cSJan Schmidt node = ulist_next(tmp, &uiter); 14688da6d581SJan Schmidt if (!node) 14698da6d581SJan Schmidt break; 14708da6d581SJan Schmidt bytenr = node->val; 1471bca1a290SWang Shilong cond_resched(); 14728da6d581SJan Schmidt } 14738da6d581SJan Schmidt 14748da6d581SJan Schmidt ulist_free(tmp); 14758da6d581SJan Schmidt return 0; 14768da6d581SJan Schmidt } 14778da6d581SJan Schmidt 14789e351cc8SJosef Bacik int btrfs_find_all_roots(struct btrfs_trans_handle *trans, 14799e351cc8SJosef Bacik struct btrfs_fs_info *fs_info, u64 bytenr, 1480c995ab3cSZygo Blaxell u64 time_seq, struct ulist **roots, 1481c995ab3cSZygo Blaxell bool ignore_offset) 14829e351cc8SJosef Bacik { 14839e351cc8SJosef Bacik int ret; 14849e351cc8SJosef Bacik 14859e351cc8SJosef Bacik if (!trans) 14869e351cc8SJosef Bacik down_read(&fs_info->commit_root_sem); 1487e0c476b1SJeff Mahoney ret = btrfs_find_all_roots_safe(trans, fs_info, bytenr, 1488c995ab3cSZygo Blaxell time_seq, roots, ignore_offset); 14899e351cc8SJosef Bacik if (!trans) 14909e351cc8SJosef Bacik up_read(&fs_info->commit_root_sem); 14919e351cc8SJosef Bacik return ret; 14929e351cc8SJosef Bacik } 14939e351cc8SJosef Bacik 14942c2ed5aaSMark Fasheh /** 14952c2ed5aaSMark Fasheh * btrfs_check_shared - tell us whether an extent is shared 14962c2ed5aaSMark Fasheh * 14972c2ed5aaSMark Fasheh * btrfs_check_shared uses the backref walking code but will short 14982c2ed5aaSMark Fasheh * circuit as soon as it finds a root or inode that doesn't match the 14992c2ed5aaSMark Fasheh * one passed in. This provides a significant performance benefit for 15002c2ed5aaSMark Fasheh * callers (such as fiemap) which want to know whether the extent is 15012c2ed5aaSMark Fasheh * shared but do not need a ref count. 15022c2ed5aaSMark Fasheh * 150303628cdbSFilipe Manana * This attempts to attach to the running transaction in order to account for 150403628cdbSFilipe Manana * delayed refs, but continues on even when no running transaction exists. 1505bb739cf0SEdmund Nadolski * 15062c2ed5aaSMark Fasheh * Return: 0 if extent is not shared, 1 if it is shared, < 0 on error. 15072c2ed5aaSMark Fasheh */ 15085911c8feSDavid Sterba int btrfs_check_shared(struct btrfs_root *root, u64 inum, u64 bytenr, 15095911c8feSDavid Sterba struct ulist *roots, struct ulist *tmp) 1510dc046b10SJosef Bacik { 1511bb739cf0SEdmund Nadolski struct btrfs_fs_info *fs_info = root->fs_info; 1512bb739cf0SEdmund Nadolski struct btrfs_trans_handle *trans; 1513dc046b10SJosef Bacik struct ulist_iterator uiter; 1514dc046b10SJosef Bacik struct ulist_node *node; 15153284da7bSDavid Sterba struct seq_list elem = SEQ_LIST_INIT(elem); 1516dc046b10SJosef Bacik int ret = 0; 15173ec4d323SEdmund Nadolski struct share_check shared = { 15184fd786e6SMisono Tomohiro .root_objectid = root->root_key.objectid, 15193ec4d323SEdmund Nadolski .inum = inum, 15203ec4d323SEdmund Nadolski .share_count = 0, 15213ec4d323SEdmund Nadolski }; 1522dc046b10SJosef Bacik 15235911c8feSDavid Sterba ulist_init(roots); 15245911c8feSDavid Sterba ulist_init(tmp); 1525dc046b10SJosef Bacik 1526a6d155d2SFilipe Manana trans = btrfs_join_transaction_nostart(root); 1527bb739cf0SEdmund Nadolski if (IS_ERR(trans)) { 152803628cdbSFilipe Manana if (PTR_ERR(trans) != -ENOENT && PTR_ERR(trans) != -EROFS) { 152903628cdbSFilipe Manana ret = PTR_ERR(trans); 153003628cdbSFilipe Manana goto out; 153103628cdbSFilipe Manana } 1532bb739cf0SEdmund Nadolski trans = NULL; 1533dc046b10SJosef Bacik down_read(&fs_info->commit_root_sem); 1534bb739cf0SEdmund Nadolski } else { 1535bb739cf0SEdmund Nadolski btrfs_get_tree_mod_seq(fs_info, &elem); 1536bb739cf0SEdmund Nadolski } 1537bb739cf0SEdmund Nadolski 1538dc046b10SJosef Bacik ULIST_ITER_INIT(&uiter); 1539dc046b10SJosef Bacik while (1) { 1540dc046b10SJosef Bacik ret = find_parent_nodes(trans, fs_info, bytenr, elem.seq, tmp, 1541c995ab3cSZygo Blaxell roots, NULL, &shared, false); 1542dc046b10SJosef Bacik if (ret == BACKREF_FOUND_SHARED) { 15432c2ed5aaSMark Fasheh /* this is the only condition under which we return 1 */ 1544dc046b10SJosef Bacik ret = 1; 1545dc046b10SJosef Bacik break; 1546dc046b10SJosef Bacik } 1547dc046b10SJosef Bacik if (ret < 0 && ret != -ENOENT) 1548dc046b10SJosef Bacik break; 15492c2ed5aaSMark Fasheh ret = 0; 1550dc046b10SJosef Bacik node = ulist_next(tmp, &uiter); 1551dc046b10SJosef Bacik if (!node) 1552dc046b10SJosef Bacik break; 1553dc046b10SJosef Bacik bytenr = node->val; 155418bf591bSEdmund Nadolski shared.share_count = 0; 1555dc046b10SJosef Bacik cond_resched(); 1556dc046b10SJosef Bacik } 1557bb739cf0SEdmund Nadolski 1558bb739cf0SEdmund Nadolski if (trans) { 1559dc046b10SJosef Bacik btrfs_put_tree_mod_seq(fs_info, &elem); 1560bb739cf0SEdmund Nadolski btrfs_end_transaction(trans); 1561bb739cf0SEdmund Nadolski } else { 1562dc046b10SJosef Bacik up_read(&fs_info->commit_root_sem); 1563bb739cf0SEdmund Nadolski } 156403628cdbSFilipe Manana out: 15655911c8feSDavid Sterba ulist_release(roots); 15665911c8feSDavid Sterba ulist_release(tmp); 1567dc046b10SJosef Bacik return ret; 1568dc046b10SJosef Bacik } 1569dc046b10SJosef Bacik 1570f186373fSMark Fasheh int btrfs_find_one_extref(struct btrfs_root *root, u64 inode_objectid, 1571f186373fSMark Fasheh u64 start_off, struct btrfs_path *path, 1572f186373fSMark Fasheh struct btrfs_inode_extref **ret_extref, 1573f186373fSMark Fasheh u64 *found_off) 1574f186373fSMark Fasheh { 1575f186373fSMark Fasheh int ret, slot; 1576f186373fSMark Fasheh struct btrfs_key key; 1577f186373fSMark Fasheh struct btrfs_key found_key; 1578f186373fSMark Fasheh struct btrfs_inode_extref *extref; 157973980becSJeff Mahoney const struct extent_buffer *leaf; 1580f186373fSMark Fasheh unsigned long ptr; 1581f186373fSMark Fasheh 1582f186373fSMark Fasheh key.objectid = inode_objectid; 1583962a298fSDavid Sterba key.type = BTRFS_INODE_EXTREF_KEY; 1584f186373fSMark Fasheh key.offset = start_off; 1585f186373fSMark Fasheh 1586f186373fSMark Fasheh ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); 1587f186373fSMark Fasheh if (ret < 0) 1588f186373fSMark Fasheh return ret; 1589f186373fSMark Fasheh 1590f186373fSMark Fasheh while (1) { 1591f186373fSMark Fasheh leaf = path->nodes[0]; 1592f186373fSMark Fasheh slot = path->slots[0]; 1593f186373fSMark Fasheh if (slot >= btrfs_header_nritems(leaf)) { 1594f186373fSMark Fasheh /* 1595f186373fSMark Fasheh * If the item at offset is not found, 1596f186373fSMark Fasheh * btrfs_search_slot will point us to the slot 1597f186373fSMark Fasheh * where it should be inserted. In our case 1598f186373fSMark Fasheh * that will be the slot directly before the 1599f186373fSMark Fasheh * next INODE_REF_KEY_V2 item. In the case 1600f186373fSMark Fasheh * that we're pointing to the last slot in a 1601f186373fSMark Fasheh * leaf, we must move one leaf over. 1602f186373fSMark Fasheh */ 1603f186373fSMark Fasheh ret = btrfs_next_leaf(root, path); 1604f186373fSMark Fasheh if (ret) { 1605f186373fSMark Fasheh if (ret >= 1) 1606f186373fSMark Fasheh ret = -ENOENT; 1607f186373fSMark Fasheh break; 1608f186373fSMark Fasheh } 1609f186373fSMark Fasheh continue; 1610f186373fSMark Fasheh } 1611f186373fSMark Fasheh 1612f186373fSMark Fasheh btrfs_item_key_to_cpu(leaf, &found_key, slot); 1613f186373fSMark Fasheh 1614f186373fSMark Fasheh /* 1615f186373fSMark Fasheh * Check that we're still looking at an extended ref key for 1616f186373fSMark Fasheh * this particular objectid. If we have different 1617f186373fSMark Fasheh * objectid or type then there are no more to be found 1618f186373fSMark Fasheh * in the tree and we can exit. 1619f186373fSMark Fasheh */ 1620f186373fSMark Fasheh ret = -ENOENT; 1621f186373fSMark Fasheh if (found_key.objectid != inode_objectid) 1622f186373fSMark Fasheh break; 1623962a298fSDavid Sterba if (found_key.type != BTRFS_INODE_EXTREF_KEY) 1624f186373fSMark Fasheh break; 1625f186373fSMark Fasheh 1626f186373fSMark Fasheh ret = 0; 1627f186373fSMark Fasheh ptr = btrfs_item_ptr_offset(leaf, path->slots[0]); 1628f186373fSMark Fasheh extref = (struct btrfs_inode_extref *)ptr; 1629f186373fSMark Fasheh *ret_extref = extref; 1630f186373fSMark Fasheh if (found_off) 1631f186373fSMark Fasheh *found_off = found_key.offset; 1632f186373fSMark Fasheh break; 1633f186373fSMark Fasheh } 1634f186373fSMark Fasheh 1635f186373fSMark Fasheh return ret; 1636f186373fSMark Fasheh } 1637f186373fSMark Fasheh 163848a3b636SEric Sandeen /* 163948a3b636SEric Sandeen * this iterates to turn a name (from iref/extref) into a full filesystem path. 164048a3b636SEric Sandeen * Elements of the path are separated by '/' and the path is guaranteed to be 164148a3b636SEric Sandeen * 0-terminated. the path is only given within the current file system. 164248a3b636SEric Sandeen * Therefore, it never starts with a '/'. the caller is responsible to provide 164348a3b636SEric Sandeen * "size" bytes in "dest". the dest buffer will be filled backwards. finally, 164448a3b636SEric Sandeen * the start point of the resulting string is returned. this pointer is within 164548a3b636SEric Sandeen * dest, normally. 164648a3b636SEric Sandeen * in case the path buffer would overflow, the pointer is decremented further 164748a3b636SEric Sandeen * as if output was written to the buffer, though no more output is actually 164848a3b636SEric Sandeen * generated. that way, the caller can determine how much space would be 164948a3b636SEric Sandeen * required for the path to fit into the buffer. in that case, the returned 165048a3b636SEric Sandeen * value will be smaller than dest. callers must check this! 165148a3b636SEric Sandeen */ 165296b5bd77SJan Schmidt char *btrfs_ref_to_path(struct btrfs_root *fs_root, struct btrfs_path *path, 1653d24bec3aSMark Fasheh u32 name_len, unsigned long name_off, 1654a542ad1bSJan Schmidt struct extent_buffer *eb_in, u64 parent, 1655a542ad1bSJan Schmidt char *dest, u32 size) 1656a542ad1bSJan Schmidt { 1657a542ad1bSJan Schmidt int slot; 1658a542ad1bSJan Schmidt u64 next_inum; 1659a542ad1bSJan Schmidt int ret; 1660661bec6bSGabriel de Perthuis s64 bytes_left = ((s64)size) - 1; 1661a542ad1bSJan Schmidt struct extent_buffer *eb = eb_in; 1662a542ad1bSJan Schmidt struct btrfs_key found_key; 1663b916a59aSJan Schmidt int leave_spinning = path->leave_spinning; 1664d24bec3aSMark Fasheh struct btrfs_inode_ref *iref; 1665a542ad1bSJan Schmidt 1666a542ad1bSJan Schmidt if (bytes_left >= 0) 1667a542ad1bSJan Schmidt dest[bytes_left] = '\0'; 1668a542ad1bSJan Schmidt 1669b916a59aSJan Schmidt path->leave_spinning = 1; 1670a542ad1bSJan Schmidt while (1) { 1671d24bec3aSMark Fasheh bytes_left -= name_len; 1672a542ad1bSJan Schmidt if (bytes_left >= 0) 1673a542ad1bSJan Schmidt read_extent_buffer(eb, dest + bytes_left, 1674d24bec3aSMark Fasheh name_off, name_len); 1675b916a59aSJan Schmidt if (eb != eb_in) { 16760c0fe3b0SFilipe Manana if (!path->skip_locking) 1677b916a59aSJan Schmidt btrfs_tree_read_unlock_blocking(eb); 1678a542ad1bSJan Schmidt free_extent_buffer(eb); 1679b916a59aSJan Schmidt } 1680c234a24dSDavid Sterba ret = btrfs_find_item(fs_root, path, parent, 0, 1681c234a24dSDavid Sterba BTRFS_INODE_REF_KEY, &found_key); 16828f24b496SJan Schmidt if (ret > 0) 16838f24b496SJan Schmidt ret = -ENOENT; 1684a542ad1bSJan Schmidt if (ret) 1685a542ad1bSJan Schmidt break; 1686d24bec3aSMark Fasheh 1687a542ad1bSJan Schmidt next_inum = found_key.offset; 1688a542ad1bSJan Schmidt 1689a542ad1bSJan Schmidt /* regular exit ahead */ 1690a542ad1bSJan Schmidt if (parent == next_inum) 1691a542ad1bSJan Schmidt break; 1692a542ad1bSJan Schmidt 1693a542ad1bSJan Schmidt slot = path->slots[0]; 1694a542ad1bSJan Schmidt eb = path->nodes[0]; 1695a542ad1bSJan Schmidt /* make sure we can use eb after releasing the path */ 1696b916a59aSJan Schmidt if (eb != eb_in) { 16970c0fe3b0SFilipe Manana if (!path->skip_locking) 1698300aa896SDavid Sterba btrfs_set_lock_blocking_read(eb); 16990c0fe3b0SFilipe Manana path->nodes[0] = NULL; 17000c0fe3b0SFilipe Manana path->locks[0] = 0; 1701b916a59aSJan Schmidt } 1702a542ad1bSJan Schmidt btrfs_release_path(path); 1703a542ad1bSJan Schmidt iref = btrfs_item_ptr(eb, slot, struct btrfs_inode_ref); 1704d24bec3aSMark Fasheh 1705d24bec3aSMark Fasheh name_len = btrfs_inode_ref_name_len(eb, iref); 1706d24bec3aSMark Fasheh name_off = (unsigned long)(iref + 1); 1707d24bec3aSMark Fasheh 1708a542ad1bSJan Schmidt parent = next_inum; 1709a542ad1bSJan Schmidt --bytes_left; 1710a542ad1bSJan Schmidt if (bytes_left >= 0) 1711a542ad1bSJan Schmidt dest[bytes_left] = '/'; 1712a542ad1bSJan Schmidt } 1713a542ad1bSJan Schmidt 1714a542ad1bSJan Schmidt btrfs_release_path(path); 1715b916a59aSJan Schmidt path->leave_spinning = leave_spinning; 1716a542ad1bSJan Schmidt 1717a542ad1bSJan Schmidt if (ret) 1718a542ad1bSJan Schmidt return ERR_PTR(ret); 1719a542ad1bSJan Schmidt 1720a542ad1bSJan Schmidt return dest + bytes_left; 1721a542ad1bSJan Schmidt } 1722a542ad1bSJan Schmidt 1723a542ad1bSJan Schmidt /* 1724a542ad1bSJan Schmidt * this makes the path point to (logical EXTENT_ITEM *) 1725a542ad1bSJan Schmidt * returns BTRFS_EXTENT_FLAG_DATA for data, BTRFS_EXTENT_FLAG_TREE_BLOCK for 1726a542ad1bSJan Schmidt * tree blocks and <0 on error. 1727a542ad1bSJan Schmidt */ 1728a542ad1bSJan Schmidt int extent_from_logical(struct btrfs_fs_info *fs_info, u64 logical, 172969917e43SLiu Bo struct btrfs_path *path, struct btrfs_key *found_key, 173069917e43SLiu Bo u64 *flags_ret) 1731a542ad1bSJan Schmidt { 1732a542ad1bSJan Schmidt int ret; 1733a542ad1bSJan Schmidt u64 flags; 1734261c84b6SJosef Bacik u64 size = 0; 1735a542ad1bSJan Schmidt u32 item_size; 173673980becSJeff Mahoney const struct extent_buffer *eb; 1737a542ad1bSJan Schmidt struct btrfs_extent_item *ei; 1738a542ad1bSJan Schmidt struct btrfs_key key; 1739a542ad1bSJan Schmidt 1740261c84b6SJosef Bacik if (btrfs_fs_incompat(fs_info, SKINNY_METADATA)) 1741261c84b6SJosef Bacik key.type = BTRFS_METADATA_ITEM_KEY; 1742261c84b6SJosef Bacik else 1743a542ad1bSJan Schmidt key.type = BTRFS_EXTENT_ITEM_KEY; 1744a542ad1bSJan Schmidt key.objectid = logical; 1745a542ad1bSJan Schmidt key.offset = (u64)-1; 1746a542ad1bSJan Schmidt 1747a542ad1bSJan Schmidt ret = btrfs_search_slot(NULL, fs_info->extent_root, &key, path, 0, 0); 1748a542ad1bSJan Schmidt if (ret < 0) 1749a542ad1bSJan Schmidt return ret; 1750a542ad1bSJan Schmidt 1751850a8cdfSWang Shilong ret = btrfs_previous_extent_item(fs_info->extent_root, path, 0); 1752850a8cdfSWang Shilong if (ret) { 1753850a8cdfSWang Shilong if (ret > 0) 1754580f0a67SJosef Bacik ret = -ENOENT; 1755580f0a67SJosef Bacik return ret; 1756580f0a67SJosef Bacik } 1757850a8cdfSWang Shilong btrfs_item_key_to_cpu(path->nodes[0], found_key, path->slots[0]); 1758261c84b6SJosef Bacik if (found_key->type == BTRFS_METADATA_ITEM_KEY) 1759da17066cSJeff Mahoney size = fs_info->nodesize; 1760261c84b6SJosef Bacik else if (found_key->type == BTRFS_EXTENT_ITEM_KEY) 1761261c84b6SJosef Bacik size = found_key->offset; 1762261c84b6SJosef Bacik 1763580f0a67SJosef Bacik if (found_key->objectid > logical || 1764261c84b6SJosef Bacik found_key->objectid + size <= logical) { 1765ab8d0fc4SJeff Mahoney btrfs_debug(fs_info, 1766ab8d0fc4SJeff Mahoney "logical %llu is not within any extent", logical); 1767a542ad1bSJan Schmidt return -ENOENT; 17684692cf58SJan Schmidt } 1769a542ad1bSJan Schmidt 1770a542ad1bSJan Schmidt eb = path->nodes[0]; 1771a542ad1bSJan Schmidt item_size = btrfs_item_size_nr(eb, path->slots[0]); 1772a542ad1bSJan Schmidt BUG_ON(item_size < sizeof(*ei)); 1773a542ad1bSJan Schmidt 1774a542ad1bSJan Schmidt ei = btrfs_item_ptr(eb, path->slots[0], struct btrfs_extent_item); 1775a542ad1bSJan Schmidt flags = btrfs_extent_flags(eb, ei); 1776a542ad1bSJan Schmidt 1777ab8d0fc4SJeff Mahoney btrfs_debug(fs_info, 1778ab8d0fc4SJeff Mahoney "logical %llu is at position %llu within the extent (%llu EXTENT_ITEM %llu) flags %#llx size %u", 1779c1c9ff7cSGeert Uytterhoeven logical, logical - found_key->objectid, found_key->objectid, 1780c1c9ff7cSGeert Uytterhoeven found_key->offset, flags, item_size); 178169917e43SLiu Bo 178269917e43SLiu Bo WARN_ON(!flags_ret); 178369917e43SLiu Bo if (flags_ret) { 1784a542ad1bSJan Schmidt if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) 178569917e43SLiu Bo *flags_ret = BTRFS_EXTENT_FLAG_TREE_BLOCK; 178669917e43SLiu Bo else if (flags & BTRFS_EXTENT_FLAG_DATA) 178769917e43SLiu Bo *flags_ret = BTRFS_EXTENT_FLAG_DATA; 178869917e43SLiu Bo else 1789290342f6SArnd Bergmann BUG(); 179069917e43SLiu Bo return 0; 179169917e43SLiu Bo } 1792a542ad1bSJan Schmidt 1793a542ad1bSJan Schmidt return -EIO; 1794a542ad1bSJan Schmidt } 1795a542ad1bSJan Schmidt 1796a542ad1bSJan Schmidt /* 1797a542ad1bSJan Schmidt * helper function to iterate extent inline refs. ptr must point to a 0 value 1798a542ad1bSJan Schmidt * for the first call and may be modified. it is used to track state. 1799a542ad1bSJan Schmidt * if more refs exist, 0 is returned and the next call to 1800e0c476b1SJeff Mahoney * get_extent_inline_ref must pass the modified ptr parameter to get the 1801a542ad1bSJan Schmidt * next ref. after the last ref was processed, 1 is returned. 1802a542ad1bSJan Schmidt * returns <0 on error 1803a542ad1bSJan Schmidt */ 1804e0c476b1SJeff Mahoney static int get_extent_inline_ref(unsigned long *ptr, 180573980becSJeff Mahoney const struct extent_buffer *eb, 180673980becSJeff Mahoney const struct btrfs_key *key, 180773980becSJeff Mahoney const struct btrfs_extent_item *ei, 180873980becSJeff Mahoney u32 item_size, 1809a542ad1bSJan Schmidt struct btrfs_extent_inline_ref **out_eiref, 1810a542ad1bSJan Schmidt int *out_type) 1811a542ad1bSJan Schmidt { 1812a542ad1bSJan Schmidt unsigned long end; 1813a542ad1bSJan Schmidt u64 flags; 1814a542ad1bSJan Schmidt struct btrfs_tree_block_info *info; 1815a542ad1bSJan Schmidt 1816a542ad1bSJan Schmidt if (!*ptr) { 1817a542ad1bSJan Schmidt /* first call */ 1818a542ad1bSJan Schmidt flags = btrfs_extent_flags(eb, ei); 1819a542ad1bSJan Schmidt if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) { 18206eda71d0SLiu Bo if (key->type == BTRFS_METADATA_ITEM_KEY) { 18216eda71d0SLiu Bo /* a skinny metadata extent */ 18226eda71d0SLiu Bo *out_eiref = 18236eda71d0SLiu Bo (struct btrfs_extent_inline_ref *)(ei + 1); 18246eda71d0SLiu Bo } else { 18256eda71d0SLiu Bo WARN_ON(key->type != BTRFS_EXTENT_ITEM_KEY); 1826a542ad1bSJan Schmidt info = (struct btrfs_tree_block_info *)(ei + 1); 1827a542ad1bSJan Schmidt *out_eiref = 1828a542ad1bSJan Schmidt (struct btrfs_extent_inline_ref *)(info + 1); 18296eda71d0SLiu Bo } 1830a542ad1bSJan Schmidt } else { 1831a542ad1bSJan Schmidt *out_eiref = (struct btrfs_extent_inline_ref *)(ei + 1); 1832a542ad1bSJan Schmidt } 1833a542ad1bSJan Schmidt *ptr = (unsigned long)*out_eiref; 1834cd857dd6SLiu Bo if ((unsigned long)(*ptr) >= (unsigned long)ei + item_size) 1835a542ad1bSJan Schmidt return -ENOENT; 1836a542ad1bSJan Schmidt } 1837a542ad1bSJan Schmidt 1838a542ad1bSJan Schmidt end = (unsigned long)ei + item_size; 18396eda71d0SLiu Bo *out_eiref = (struct btrfs_extent_inline_ref *)(*ptr); 18403de28d57SLiu Bo *out_type = btrfs_get_extent_inline_ref_type(eb, *out_eiref, 18413de28d57SLiu Bo BTRFS_REF_TYPE_ANY); 18423de28d57SLiu Bo if (*out_type == BTRFS_REF_TYPE_INVALID) 1843af431dcbSSu Yue return -EUCLEAN; 1844a542ad1bSJan Schmidt 1845a542ad1bSJan Schmidt *ptr += btrfs_extent_inline_ref_size(*out_type); 1846a542ad1bSJan Schmidt WARN_ON(*ptr > end); 1847a542ad1bSJan Schmidt if (*ptr == end) 1848a542ad1bSJan Schmidt return 1; /* last */ 1849a542ad1bSJan Schmidt 1850a542ad1bSJan Schmidt return 0; 1851a542ad1bSJan Schmidt } 1852a542ad1bSJan Schmidt 1853a542ad1bSJan Schmidt /* 1854a542ad1bSJan Schmidt * reads the tree block backref for an extent. tree level and root are returned 1855a542ad1bSJan Schmidt * through out_level and out_root. ptr must point to a 0 value for the first 1856e0c476b1SJeff Mahoney * call and may be modified (see get_extent_inline_ref comment). 1857a542ad1bSJan Schmidt * returns 0 if data was provided, 1 if there was no more data to provide or 1858a542ad1bSJan Schmidt * <0 on error. 1859a542ad1bSJan Schmidt */ 1860a542ad1bSJan Schmidt int tree_backref_for_extent(unsigned long *ptr, struct extent_buffer *eb, 18616eda71d0SLiu Bo struct btrfs_key *key, struct btrfs_extent_item *ei, 18626eda71d0SLiu Bo u32 item_size, u64 *out_root, u8 *out_level) 1863a542ad1bSJan Schmidt { 1864a542ad1bSJan Schmidt int ret; 1865a542ad1bSJan Schmidt int type; 1866a542ad1bSJan Schmidt struct btrfs_extent_inline_ref *eiref; 1867a542ad1bSJan Schmidt 1868a542ad1bSJan Schmidt if (*ptr == (unsigned long)-1) 1869a542ad1bSJan Schmidt return 1; 1870a542ad1bSJan Schmidt 1871a542ad1bSJan Schmidt while (1) { 1872e0c476b1SJeff Mahoney ret = get_extent_inline_ref(ptr, eb, key, ei, item_size, 1873a542ad1bSJan Schmidt &eiref, &type); 1874a542ad1bSJan Schmidt if (ret < 0) 1875a542ad1bSJan Schmidt return ret; 1876a542ad1bSJan Schmidt 1877a542ad1bSJan Schmidt if (type == BTRFS_TREE_BLOCK_REF_KEY || 1878a542ad1bSJan Schmidt type == BTRFS_SHARED_BLOCK_REF_KEY) 1879a542ad1bSJan Schmidt break; 1880a542ad1bSJan Schmidt 1881a542ad1bSJan Schmidt if (ret == 1) 1882a542ad1bSJan Schmidt return 1; 1883a542ad1bSJan Schmidt } 1884a542ad1bSJan Schmidt 1885a542ad1bSJan Schmidt /* we can treat both ref types equally here */ 1886a542ad1bSJan Schmidt *out_root = btrfs_extent_inline_ref_offset(eb, eiref); 1887a1317f45SFilipe Manana 1888a1317f45SFilipe Manana if (key->type == BTRFS_EXTENT_ITEM_KEY) { 1889a1317f45SFilipe Manana struct btrfs_tree_block_info *info; 1890a1317f45SFilipe Manana 1891a1317f45SFilipe Manana info = (struct btrfs_tree_block_info *)(ei + 1); 1892a542ad1bSJan Schmidt *out_level = btrfs_tree_block_level(eb, info); 1893a1317f45SFilipe Manana } else { 1894a1317f45SFilipe Manana ASSERT(key->type == BTRFS_METADATA_ITEM_KEY); 1895a1317f45SFilipe Manana *out_level = (u8)key->offset; 1896a1317f45SFilipe Manana } 1897a542ad1bSJan Schmidt 1898a542ad1bSJan Schmidt if (ret == 1) 1899a542ad1bSJan Schmidt *ptr = (unsigned long)-1; 1900a542ad1bSJan Schmidt 1901a542ad1bSJan Schmidt return 0; 1902a542ad1bSJan Schmidt } 1903a542ad1bSJan Schmidt 1904ab8d0fc4SJeff Mahoney static int iterate_leaf_refs(struct btrfs_fs_info *fs_info, 1905ab8d0fc4SJeff Mahoney struct extent_inode_elem *inode_list, 1906976b1908SJan Schmidt u64 root, u64 extent_item_objectid, 19074692cf58SJan Schmidt iterate_extent_inodes_t *iterate, void *ctx) 1908a542ad1bSJan Schmidt { 1909976b1908SJan Schmidt struct extent_inode_elem *eie; 19104692cf58SJan Schmidt int ret = 0; 1911a542ad1bSJan Schmidt 1912976b1908SJan Schmidt for (eie = inode_list; eie; eie = eie->next) { 1913ab8d0fc4SJeff Mahoney btrfs_debug(fs_info, 1914ab8d0fc4SJeff Mahoney "ref for %llu resolved, key (%llu EXTEND_DATA %llu), root %llu", 1915ab8d0fc4SJeff Mahoney extent_item_objectid, eie->inum, 1916ab8d0fc4SJeff Mahoney eie->offset, root); 1917976b1908SJan Schmidt ret = iterate(eie->inum, eie->offset, root, ctx); 19184692cf58SJan Schmidt if (ret) { 1919ab8d0fc4SJeff Mahoney btrfs_debug(fs_info, 1920ab8d0fc4SJeff Mahoney "stopping iteration for %llu due to ret=%d", 1921976b1908SJan Schmidt extent_item_objectid, ret); 1922a542ad1bSJan Schmidt break; 1923a542ad1bSJan Schmidt } 1924a542ad1bSJan Schmidt } 1925a542ad1bSJan Schmidt 1926a542ad1bSJan Schmidt return ret; 1927a542ad1bSJan Schmidt } 1928a542ad1bSJan Schmidt 1929a542ad1bSJan Schmidt /* 1930a542ad1bSJan Schmidt * calls iterate() for every inode that references the extent identified by 19314692cf58SJan Schmidt * the given parameters. 1932a542ad1bSJan Schmidt * when the iterator function returns a non-zero value, iteration stops. 1933a542ad1bSJan Schmidt */ 1934a542ad1bSJan Schmidt int iterate_extent_inodes(struct btrfs_fs_info *fs_info, 19354692cf58SJan Schmidt u64 extent_item_objectid, u64 extent_item_pos, 19367a3ae2f8SJan Schmidt int search_commit_root, 1937c995ab3cSZygo Blaxell iterate_extent_inodes_t *iterate, void *ctx, 1938c995ab3cSZygo Blaxell bool ignore_offset) 1939a542ad1bSJan Schmidt { 1940a542ad1bSJan Schmidt int ret; 1941da61d31aSJosef Bacik struct btrfs_trans_handle *trans = NULL; 19427a3ae2f8SJan Schmidt struct ulist *refs = NULL; 19437a3ae2f8SJan Schmidt struct ulist *roots = NULL; 19444692cf58SJan Schmidt struct ulist_node *ref_node = NULL; 19454692cf58SJan Schmidt struct ulist_node *root_node = NULL; 19463284da7bSDavid Sterba struct seq_list tree_mod_seq_elem = SEQ_LIST_INIT(tree_mod_seq_elem); 1947cd1b413cSJan Schmidt struct ulist_iterator ref_uiter; 1948cd1b413cSJan Schmidt struct ulist_iterator root_uiter; 1949a542ad1bSJan Schmidt 1950ab8d0fc4SJeff Mahoney btrfs_debug(fs_info, "resolving all inodes for extent %llu", 19514692cf58SJan Schmidt extent_item_objectid); 19524692cf58SJan Schmidt 1953da61d31aSJosef Bacik if (!search_commit_root) { 1954bfc61c36SFilipe Manana trans = btrfs_attach_transaction(fs_info->extent_root); 1955bfc61c36SFilipe Manana if (IS_ERR(trans)) { 1956bfc61c36SFilipe Manana if (PTR_ERR(trans) != -ENOENT && 1957bfc61c36SFilipe Manana PTR_ERR(trans) != -EROFS) 19587a3ae2f8SJan Schmidt return PTR_ERR(trans); 1959bfc61c36SFilipe Manana trans = NULL; 19607a3ae2f8SJan Schmidt } 1961bfc61c36SFilipe Manana } 1962bfc61c36SFilipe Manana 1963bfc61c36SFilipe Manana if (trans) 1964bfc61c36SFilipe Manana btrfs_get_tree_mod_seq(fs_info, &tree_mod_seq_elem); 1965bfc61c36SFilipe Manana else 1966bfc61c36SFilipe Manana down_read(&fs_info->commit_root_sem); 19674692cf58SJan Schmidt 19684692cf58SJan Schmidt ret = btrfs_find_all_leafs(trans, fs_info, extent_item_objectid, 1969097b8a7cSJan Schmidt tree_mod_seq_elem.seq, &refs, 1970c995ab3cSZygo Blaxell &extent_item_pos, ignore_offset); 19714692cf58SJan Schmidt if (ret) 19724692cf58SJan Schmidt goto out; 19734692cf58SJan Schmidt 1974cd1b413cSJan Schmidt ULIST_ITER_INIT(&ref_uiter); 1975cd1b413cSJan Schmidt while (!ret && (ref_node = ulist_next(refs, &ref_uiter))) { 1976e0c476b1SJeff Mahoney ret = btrfs_find_all_roots_safe(trans, fs_info, ref_node->val, 1977c995ab3cSZygo Blaxell tree_mod_seq_elem.seq, &roots, 1978c995ab3cSZygo Blaxell ignore_offset); 19794692cf58SJan Schmidt if (ret) 1980a542ad1bSJan Schmidt break; 1981cd1b413cSJan Schmidt ULIST_ITER_INIT(&root_uiter); 1982cd1b413cSJan Schmidt while (!ret && (root_node = ulist_next(roots, &root_uiter))) { 1983ab8d0fc4SJeff Mahoney btrfs_debug(fs_info, 1984ab8d0fc4SJeff Mahoney "root %llu references leaf %llu, data list %#llx", 1985ab8d0fc4SJeff Mahoney root_node->val, ref_node->val, 1986c1c9ff7cSGeert Uytterhoeven ref_node->aux); 1987ab8d0fc4SJeff Mahoney ret = iterate_leaf_refs(fs_info, 1988ab8d0fc4SJeff Mahoney (struct extent_inode_elem *) 1989995e01b7SJan Schmidt (uintptr_t)ref_node->aux, 1990995e01b7SJan Schmidt root_node->val, 1991995e01b7SJan Schmidt extent_item_objectid, 1992a542ad1bSJan Schmidt iterate, ctx); 19934692cf58SJan Schmidt } 1994976b1908SJan Schmidt ulist_free(roots); 1995a542ad1bSJan Schmidt } 1996a542ad1bSJan Schmidt 1997976b1908SJan Schmidt free_leaf_list(refs); 19984692cf58SJan Schmidt out: 1999bfc61c36SFilipe Manana if (trans) { 20008445f61cSJan Schmidt btrfs_put_tree_mod_seq(fs_info, &tree_mod_seq_elem); 20013a45bb20SJeff Mahoney btrfs_end_transaction(trans); 20029e351cc8SJosef Bacik } else { 20039e351cc8SJosef Bacik up_read(&fs_info->commit_root_sem); 20047a3ae2f8SJan Schmidt } 20057a3ae2f8SJan Schmidt 2006a542ad1bSJan Schmidt return ret; 2007a542ad1bSJan Schmidt } 2008a542ad1bSJan Schmidt 2009a542ad1bSJan Schmidt int iterate_inodes_from_logical(u64 logical, struct btrfs_fs_info *fs_info, 2010a542ad1bSJan Schmidt struct btrfs_path *path, 2011c995ab3cSZygo Blaxell iterate_extent_inodes_t *iterate, void *ctx, 2012c995ab3cSZygo Blaxell bool ignore_offset) 2013a542ad1bSJan Schmidt { 2014a542ad1bSJan Schmidt int ret; 20154692cf58SJan Schmidt u64 extent_item_pos; 201669917e43SLiu Bo u64 flags = 0; 2017a542ad1bSJan Schmidt struct btrfs_key found_key; 20187a3ae2f8SJan Schmidt int search_commit_root = path->search_commit_root; 2019a542ad1bSJan Schmidt 202069917e43SLiu Bo ret = extent_from_logical(fs_info, logical, path, &found_key, &flags); 20214692cf58SJan Schmidt btrfs_release_path(path); 2022a542ad1bSJan Schmidt if (ret < 0) 2023a542ad1bSJan Schmidt return ret; 202469917e43SLiu Bo if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) 20253627bf45SStefan Behrens return -EINVAL; 2026a542ad1bSJan Schmidt 20274692cf58SJan Schmidt extent_item_pos = logical - found_key.objectid; 20287a3ae2f8SJan Schmidt ret = iterate_extent_inodes(fs_info, found_key.objectid, 20297a3ae2f8SJan Schmidt extent_item_pos, search_commit_root, 2030c995ab3cSZygo Blaxell iterate, ctx, ignore_offset); 2031a542ad1bSJan Schmidt 2032a542ad1bSJan Schmidt return ret; 2033a542ad1bSJan Schmidt } 2034a542ad1bSJan Schmidt 2035d24bec3aSMark Fasheh typedef int (iterate_irefs_t)(u64 parent, u32 name_len, unsigned long name_off, 2036d24bec3aSMark Fasheh struct extent_buffer *eb, void *ctx); 2037d24bec3aSMark Fasheh 2038d24bec3aSMark Fasheh static int iterate_inode_refs(u64 inum, struct btrfs_root *fs_root, 2039a542ad1bSJan Schmidt struct btrfs_path *path, 2040a542ad1bSJan Schmidt iterate_irefs_t *iterate, void *ctx) 2041a542ad1bSJan Schmidt { 2042aefc1eb1SJan Schmidt int ret = 0; 2043a542ad1bSJan Schmidt int slot; 2044a542ad1bSJan Schmidt u32 cur; 2045a542ad1bSJan Schmidt u32 len; 2046a542ad1bSJan Schmidt u32 name_len; 2047a542ad1bSJan Schmidt u64 parent = 0; 2048a542ad1bSJan Schmidt int found = 0; 2049a542ad1bSJan Schmidt struct extent_buffer *eb; 2050a542ad1bSJan Schmidt struct btrfs_item *item; 2051a542ad1bSJan Schmidt struct btrfs_inode_ref *iref; 2052a542ad1bSJan Schmidt struct btrfs_key found_key; 2053a542ad1bSJan Schmidt 2054aefc1eb1SJan Schmidt while (!ret) { 2055c234a24dSDavid Sterba ret = btrfs_find_item(fs_root, path, inum, 2056c234a24dSDavid Sterba parent ? parent + 1 : 0, BTRFS_INODE_REF_KEY, 2057a542ad1bSJan Schmidt &found_key); 2058c234a24dSDavid Sterba 2059a542ad1bSJan Schmidt if (ret < 0) 2060a542ad1bSJan Schmidt break; 2061a542ad1bSJan Schmidt if (ret) { 2062a542ad1bSJan Schmidt ret = found ? 0 : -ENOENT; 2063a542ad1bSJan Schmidt break; 2064a542ad1bSJan Schmidt } 2065a542ad1bSJan Schmidt ++found; 2066a542ad1bSJan Schmidt 2067a542ad1bSJan Schmidt parent = found_key.offset; 2068a542ad1bSJan Schmidt slot = path->slots[0]; 20693fe81ce2SFilipe David Borba Manana eb = btrfs_clone_extent_buffer(path->nodes[0]); 20703fe81ce2SFilipe David Borba Manana if (!eb) { 20713fe81ce2SFilipe David Borba Manana ret = -ENOMEM; 20723fe81ce2SFilipe David Borba Manana break; 20733fe81ce2SFilipe David Borba Manana } 2074a542ad1bSJan Schmidt btrfs_release_path(path); 2075a542ad1bSJan Schmidt 2076dd3cc16bSRoss Kirk item = btrfs_item_nr(slot); 2077a542ad1bSJan Schmidt iref = btrfs_item_ptr(eb, slot, struct btrfs_inode_ref); 2078a542ad1bSJan Schmidt 2079a542ad1bSJan Schmidt for (cur = 0; cur < btrfs_item_size(eb, item); cur += len) { 2080a542ad1bSJan Schmidt name_len = btrfs_inode_ref_name_len(eb, iref); 2081a542ad1bSJan Schmidt /* path must be released before calling iterate()! */ 2082ab8d0fc4SJeff Mahoney btrfs_debug(fs_root->fs_info, 2083ab8d0fc4SJeff Mahoney "following ref at offset %u for inode %llu in tree %llu", 20844fd786e6SMisono Tomohiro cur, found_key.objectid, 20854fd786e6SMisono Tomohiro fs_root->root_key.objectid); 2086d24bec3aSMark Fasheh ret = iterate(parent, name_len, 2087d24bec3aSMark Fasheh (unsigned long)(iref + 1), eb, ctx); 2088aefc1eb1SJan Schmidt if (ret) 2089a542ad1bSJan Schmidt break; 2090a542ad1bSJan Schmidt len = sizeof(*iref) + name_len; 2091a542ad1bSJan Schmidt iref = (struct btrfs_inode_ref *)((char *)iref + len); 2092a542ad1bSJan Schmidt } 2093a542ad1bSJan Schmidt free_extent_buffer(eb); 2094a542ad1bSJan Schmidt } 2095a542ad1bSJan Schmidt 2096a542ad1bSJan Schmidt btrfs_release_path(path); 2097a542ad1bSJan Schmidt 2098a542ad1bSJan Schmidt return ret; 2099a542ad1bSJan Schmidt } 2100a542ad1bSJan Schmidt 2101d24bec3aSMark Fasheh static int iterate_inode_extrefs(u64 inum, struct btrfs_root *fs_root, 2102d24bec3aSMark Fasheh struct btrfs_path *path, 2103d24bec3aSMark Fasheh iterate_irefs_t *iterate, void *ctx) 2104d24bec3aSMark Fasheh { 2105d24bec3aSMark Fasheh int ret; 2106d24bec3aSMark Fasheh int slot; 2107d24bec3aSMark Fasheh u64 offset = 0; 2108d24bec3aSMark Fasheh u64 parent; 2109d24bec3aSMark Fasheh int found = 0; 2110d24bec3aSMark Fasheh struct extent_buffer *eb; 2111d24bec3aSMark Fasheh struct btrfs_inode_extref *extref; 2112d24bec3aSMark Fasheh u32 item_size; 2113d24bec3aSMark Fasheh u32 cur_offset; 2114d24bec3aSMark Fasheh unsigned long ptr; 2115d24bec3aSMark Fasheh 2116d24bec3aSMark Fasheh while (1) { 2117d24bec3aSMark Fasheh ret = btrfs_find_one_extref(fs_root, inum, offset, path, &extref, 2118d24bec3aSMark Fasheh &offset); 2119d24bec3aSMark Fasheh if (ret < 0) 2120d24bec3aSMark Fasheh break; 2121d24bec3aSMark Fasheh if (ret) { 2122d24bec3aSMark Fasheh ret = found ? 0 : -ENOENT; 2123d24bec3aSMark Fasheh break; 2124d24bec3aSMark Fasheh } 2125d24bec3aSMark Fasheh ++found; 2126d24bec3aSMark Fasheh 2127d24bec3aSMark Fasheh slot = path->slots[0]; 21283fe81ce2SFilipe David Borba Manana eb = btrfs_clone_extent_buffer(path->nodes[0]); 21293fe81ce2SFilipe David Borba Manana if (!eb) { 21303fe81ce2SFilipe David Borba Manana ret = -ENOMEM; 21313fe81ce2SFilipe David Borba Manana break; 21323fe81ce2SFilipe David Borba Manana } 2133d24bec3aSMark Fasheh btrfs_release_path(path); 2134d24bec3aSMark Fasheh 21352849a854SChris Mason item_size = btrfs_item_size_nr(eb, slot); 21362849a854SChris Mason ptr = btrfs_item_ptr_offset(eb, slot); 2137d24bec3aSMark Fasheh cur_offset = 0; 2138d24bec3aSMark Fasheh 2139d24bec3aSMark Fasheh while (cur_offset < item_size) { 2140d24bec3aSMark Fasheh u32 name_len; 2141d24bec3aSMark Fasheh 2142d24bec3aSMark Fasheh extref = (struct btrfs_inode_extref *)(ptr + cur_offset); 2143d24bec3aSMark Fasheh parent = btrfs_inode_extref_parent(eb, extref); 2144d24bec3aSMark Fasheh name_len = btrfs_inode_extref_name_len(eb, extref); 2145d24bec3aSMark Fasheh ret = iterate(parent, name_len, 2146d24bec3aSMark Fasheh (unsigned long)&extref->name, eb, ctx); 2147d24bec3aSMark Fasheh if (ret) 2148d24bec3aSMark Fasheh break; 2149d24bec3aSMark Fasheh 21502849a854SChris Mason cur_offset += btrfs_inode_extref_name_len(eb, extref); 2151d24bec3aSMark Fasheh cur_offset += sizeof(*extref); 2152d24bec3aSMark Fasheh } 2153d24bec3aSMark Fasheh free_extent_buffer(eb); 2154d24bec3aSMark Fasheh 2155d24bec3aSMark Fasheh offset++; 2156d24bec3aSMark Fasheh } 2157d24bec3aSMark Fasheh 2158d24bec3aSMark Fasheh btrfs_release_path(path); 2159d24bec3aSMark Fasheh 2160d24bec3aSMark Fasheh return ret; 2161d24bec3aSMark Fasheh } 2162d24bec3aSMark Fasheh 2163d24bec3aSMark Fasheh static int iterate_irefs(u64 inum, struct btrfs_root *fs_root, 2164d24bec3aSMark Fasheh struct btrfs_path *path, iterate_irefs_t *iterate, 2165d24bec3aSMark Fasheh void *ctx) 2166d24bec3aSMark Fasheh { 2167d24bec3aSMark Fasheh int ret; 2168d24bec3aSMark Fasheh int found_refs = 0; 2169d24bec3aSMark Fasheh 2170d24bec3aSMark Fasheh ret = iterate_inode_refs(inum, fs_root, path, iterate, ctx); 2171d24bec3aSMark Fasheh if (!ret) 2172d24bec3aSMark Fasheh ++found_refs; 2173d24bec3aSMark Fasheh else if (ret != -ENOENT) 2174d24bec3aSMark Fasheh return ret; 2175d24bec3aSMark Fasheh 2176d24bec3aSMark Fasheh ret = iterate_inode_extrefs(inum, fs_root, path, iterate, ctx); 2177d24bec3aSMark Fasheh if (ret == -ENOENT && found_refs) 2178d24bec3aSMark Fasheh return 0; 2179d24bec3aSMark Fasheh 2180d24bec3aSMark Fasheh return ret; 2181d24bec3aSMark Fasheh } 2182d24bec3aSMark Fasheh 2183a542ad1bSJan Schmidt /* 2184a542ad1bSJan Schmidt * returns 0 if the path could be dumped (probably truncated) 2185a542ad1bSJan Schmidt * returns <0 in case of an error 2186a542ad1bSJan Schmidt */ 2187d24bec3aSMark Fasheh static int inode_to_path(u64 inum, u32 name_len, unsigned long name_off, 2188a542ad1bSJan Schmidt struct extent_buffer *eb, void *ctx) 2189a542ad1bSJan Schmidt { 2190a542ad1bSJan Schmidt struct inode_fs_paths *ipath = ctx; 2191a542ad1bSJan Schmidt char *fspath; 2192a542ad1bSJan Schmidt char *fspath_min; 2193a542ad1bSJan Schmidt int i = ipath->fspath->elem_cnt; 2194a542ad1bSJan Schmidt const int s_ptr = sizeof(char *); 2195a542ad1bSJan Schmidt u32 bytes_left; 2196a542ad1bSJan Schmidt 2197a542ad1bSJan Schmidt bytes_left = ipath->fspath->bytes_left > s_ptr ? 2198a542ad1bSJan Schmidt ipath->fspath->bytes_left - s_ptr : 0; 2199a542ad1bSJan Schmidt 2200740c3d22SChris Mason fspath_min = (char *)ipath->fspath->val + (i + 1) * s_ptr; 220196b5bd77SJan Schmidt fspath = btrfs_ref_to_path(ipath->fs_root, ipath->btrfs_path, name_len, 220296b5bd77SJan Schmidt name_off, eb, inum, fspath_min, bytes_left); 2203a542ad1bSJan Schmidt if (IS_ERR(fspath)) 2204a542ad1bSJan Schmidt return PTR_ERR(fspath); 2205a542ad1bSJan Schmidt 2206a542ad1bSJan Schmidt if (fspath > fspath_min) { 2207745c4d8eSJeff Mahoney ipath->fspath->val[i] = (u64)(unsigned long)fspath; 2208a542ad1bSJan Schmidt ++ipath->fspath->elem_cnt; 2209a542ad1bSJan Schmidt ipath->fspath->bytes_left = fspath - fspath_min; 2210a542ad1bSJan Schmidt } else { 2211a542ad1bSJan Schmidt ++ipath->fspath->elem_missed; 2212a542ad1bSJan Schmidt ipath->fspath->bytes_missing += fspath_min - fspath; 2213a542ad1bSJan Schmidt ipath->fspath->bytes_left = 0; 2214a542ad1bSJan Schmidt } 2215a542ad1bSJan Schmidt 2216a542ad1bSJan Schmidt return 0; 2217a542ad1bSJan Schmidt } 2218a542ad1bSJan Schmidt 2219a542ad1bSJan Schmidt /* 2220a542ad1bSJan Schmidt * this dumps all file system paths to the inode into the ipath struct, provided 2221a542ad1bSJan Schmidt * is has been created large enough. each path is zero-terminated and accessed 2222740c3d22SChris Mason * from ipath->fspath->val[i]. 2223a542ad1bSJan Schmidt * when it returns, there are ipath->fspath->elem_cnt number of paths available 2224740c3d22SChris Mason * in ipath->fspath->val[]. when the allocated space wasn't sufficient, the 222501327610SNicholas D Steeves * number of missed paths is recorded in ipath->fspath->elem_missed, otherwise, 2226a542ad1bSJan Schmidt * it's zero. ipath->fspath->bytes_missing holds the number of bytes that would 2227a542ad1bSJan Schmidt * have been needed to return all paths. 2228a542ad1bSJan Schmidt */ 2229a542ad1bSJan Schmidt int paths_from_inode(u64 inum, struct inode_fs_paths *ipath) 2230a542ad1bSJan Schmidt { 2231a542ad1bSJan Schmidt return iterate_irefs(inum, ipath->fs_root, ipath->btrfs_path, 2232a542ad1bSJan Schmidt inode_to_path, ipath); 2233a542ad1bSJan Schmidt } 2234a542ad1bSJan Schmidt 2235a542ad1bSJan Schmidt struct btrfs_data_container *init_data_container(u32 total_bytes) 2236a542ad1bSJan Schmidt { 2237a542ad1bSJan Schmidt struct btrfs_data_container *data; 2238a542ad1bSJan Schmidt size_t alloc_bytes; 2239a542ad1bSJan Schmidt 2240a542ad1bSJan Schmidt alloc_bytes = max_t(size_t, total_bytes, sizeof(*data)); 2241f54de068SDavid Sterba data = kvmalloc(alloc_bytes, GFP_KERNEL); 2242a542ad1bSJan Schmidt if (!data) 2243a542ad1bSJan Schmidt return ERR_PTR(-ENOMEM); 2244a542ad1bSJan Schmidt 2245a542ad1bSJan Schmidt if (total_bytes >= sizeof(*data)) { 2246a542ad1bSJan Schmidt data->bytes_left = total_bytes - sizeof(*data); 2247a542ad1bSJan Schmidt data->bytes_missing = 0; 2248a542ad1bSJan Schmidt } else { 2249a542ad1bSJan Schmidt data->bytes_missing = sizeof(*data) - total_bytes; 2250a542ad1bSJan Schmidt data->bytes_left = 0; 2251a542ad1bSJan Schmidt } 2252a542ad1bSJan Schmidt 2253a542ad1bSJan Schmidt data->elem_cnt = 0; 2254a542ad1bSJan Schmidt data->elem_missed = 0; 2255a542ad1bSJan Schmidt 2256a542ad1bSJan Schmidt return data; 2257a542ad1bSJan Schmidt } 2258a542ad1bSJan Schmidt 2259a542ad1bSJan Schmidt /* 2260a542ad1bSJan Schmidt * allocates space to return multiple file system paths for an inode. 2261a542ad1bSJan Schmidt * total_bytes to allocate are passed, note that space usable for actual path 2262a542ad1bSJan Schmidt * information will be total_bytes - sizeof(struct inode_fs_paths). 2263a542ad1bSJan Schmidt * the returned pointer must be freed with free_ipath() in the end. 2264a542ad1bSJan Schmidt */ 2265a542ad1bSJan Schmidt struct inode_fs_paths *init_ipath(s32 total_bytes, struct btrfs_root *fs_root, 2266a542ad1bSJan Schmidt struct btrfs_path *path) 2267a542ad1bSJan Schmidt { 2268a542ad1bSJan Schmidt struct inode_fs_paths *ifp; 2269a542ad1bSJan Schmidt struct btrfs_data_container *fspath; 2270a542ad1bSJan Schmidt 2271a542ad1bSJan Schmidt fspath = init_data_container(total_bytes); 2272a542ad1bSJan Schmidt if (IS_ERR(fspath)) 2273afc6961fSMisono Tomohiro return ERR_CAST(fspath); 2274a542ad1bSJan Schmidt 2275f54de068SDavid Sterba ifp = kmalloc(sizeof(*ifp), GFP_KERNEL); 2276a542ad1bSJan Schmidt if (!ifp) { 2277f54de068SDavid Sterba kvfree(fspath); 2278a542ad1bSJan Schmidt return ERR_PTR(-ENOMEM); 2279a542ad1bSJan Schmidt } 2280a542ad1bSJan Schmidt 2281a542ad1bSJan Schmidt ifp->btrfs_path = path; 2282a542ad1bSJan Schmidt ifp->fspath = fspath; 2283a542ad1bSJan Schmidt ifp->fs_root = fs_root; 2284a542ad1bSJan Schmidt 2285a542ad1bSJan Schmidt return ifp; 2286a542ad1bSJan Schmidt } 2287a542ad1bSJan Schmidt 2288a542ad1bSJan Schmidt void free_ipath(struct inode_fs_paths *ipath) 2289a542ad1bSJan Schmidt { 22904735fb28SJesper Juhl if (!ipath) 22914735fb28SJesper Juhl return; 2292f54de068SDavid Sterba kvfree(ipath->fspath); 2293a542ad1bSJan Schmidt kfree(ipath); 2294a542ad1bSJan Schmidt } 2295a37f232bSQu Wenruo 2296a37f232bSQu Wenruo struct btrfs_backref_iter *btrfs_backref_iter_alloc( 2297a37f232bSQu Wenruo struct btrfs_fs_info *fs_info, gfp_t gfp_flag) 2298a37f232bSQu Wenruo { 2299a37f232bSQu Wenruo struct btrfs_backref_iter *ret; 2300a37f232bSQu Wenruo 2301a37f232bSQu Wenruo ret = kzalloc(sizeof(*ret), gfp_flag); 2302a37f232bSQu Wenruo if (!ret) 2303a37f232bSQu Wenruo return NULL; 2304a37f232bSQu Wenruo 2305a37f232bSQu Wenruo ret->path = btrfs_alloc_path(); 2306a37f232bSQu Wenruo if (!ret) { 2307a37f232bSQu Wenruo kfree(ret); 2308a37f232bSQu Wenruo return NULL; 2309a37f232bSQu Wenruo } 2310a37f232bSQu Wenruo 2311a37f232bSQu Wenruo /* Current backref iterator only supports iteration in commit root */ 2312a37f232bSQu Wenruo ret->path->search_commit_root = 1; 2313a37f232bSQu Wenruo ret->path->skip_locking = 1; 2314a37f232bSQu Wenruo ret->fs_info = fs_info; 2315a37f232bSQu Wenruo 2316a37f232bSQu Wenruo return ret; 2317a37f232bSQu Wenruo } 2318a37f232bSQu Wenruo 2319a37f232bSQu Wenruo int btrfs_backref_iter_start(struct btrfs_backref_iter *iter, u64 bytenr) 2320a37f232bSQu Wenruo { 2321a37f232bSQu Wenruo struct btrfs_fs_info *fs_info = iter->fs_info; 2322a37f232bSQu Wenruo struct btrfs_path *path = iter->path; 2323a37f232bSQu Wenruo struct btrfs_extent_item *ei; 2324a37f232bSQu Wenruo struct btrfs_key key; 2325a37f232bSQu Wenruo int ret; 2326a37f232bSQu Wenruo 2327a37f232bSQu Wenruo key.objectid = bytenr; 2328a37f232bSQu Wenruo key.type = BTRFS_METADATA_ITEM_KEY; 2329a37f232bSQu Wenruo key.offset = (u64)-1; 2330a37f232bSQu Wenruo iter->bytenr = bytenr; 2331a37f232bSQu Wenruo 2332a37f232bSQu Wenruo ret = btrfs_search_slot(NULL, fs_info->extent_root, &key, path, 0, 0); 2333a37f232bSQu Wenruo if (ret < 0) 2334a37f232bSQu Wenruo return ret; 2335a37f232bSQu Wenruo if (ret == 0) { 2336a37f232bSQu Wenruo ret = -EUCLEAN; 2337a37f232bSQu Wenruo goto release; 2338a37f232bSQu Wenruo } 2339a37f232bSQu Wenruo if (path->slots[0] == 0) { 2340a37f232bSQu Wenruo WARN_ON(IS_ENABLED(CONFIG_BTRFS_DEBUG)); 2341a37f232bSQu Wenruo ret = -EUCLEAN; 2342a37f232bSQu Wenruo goto release; 2343a37f232bSQu Wenruo } 2344a37f232bSQu Wenruo path->slots[0]--; 2345a37f232bSQu Wenruo 2346a37f232bSQu Wenruo btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); 2347a37f232bSQu Wenruo if ((key.type != BTRFS_EXTENT_ITEM_KEY && 2348a37f232bSQu Wenruo key.type != BTRFS_METADATA_ITEM_KEY) || key.objectid != bytenr) { 2349a37f232bSQu Wenruo ret = -ENOENT; 2350a37f232bSQu Wenruo goto release; 2351a37f232bSQu Wenruo } 2352a37f232bSQu Wenruo memcpy(&iter->cur_key, &key, sizeof(key)); 2353a37f232bSQu Wenruo iter->item_ptr = (u32)btrfs_item_ptr_offset(path->nodes[0], 2354a37f232bSQu Wenruo path->slots[0]); 2355a37f232bSQu Wenruo iter->end_ptr = (u32)(iter->item_ptr + 2356a37f232bSQu Wenruo btrfs_item_size_nr(path->nodes[0], path->slots[0])); 2357a37f232bSQu Wenruo ei = btrfs_item_ptr(path->nodes[0], path->slots[0], 2358a37f232bSQu Wenruo struct btrfs_extent_item); 2359a37f232bSQu Wenruo 2360a37f232bSQu Wenruo /* 2361a37f232bSQu Wenruo * Only support iteration on tree backref yet. 2362a37f232bSQu Wenruo * 2363a37f232bSQu Wenruo * This is an extra precaution for non skinny-metadata, where 2364a37f232bSQu Wenruo * EXTENT_ITEM is also used for tree blocks, that we can only use 2365a37f232bSQu Wenruo * extent flags to determine if it's a tree block. 2366a37f232bSQu Wenruo */ 2367a37f232bSQu Wenruo if (btrfs_extent_flags(path->nodes[0], ei) & BTRFS_EXTENT_FLAG_DATA) { 2368a37f232bSQu Wenruo ret = -ENOTSUPP; 2369a37f232bSQu Wenruo goto release; 2370a37f232bSQu Wenruo } 2371a37f232bSQu Wenruo iter->cur_ptr = (u32)(iter->item_ptr + sizeof(*ei)); 2372a37f232bSQu Wenruo 2373a37f232bSQu Wenruo /* If there is no inline backref, go search for keyed backref */ 2374a37f232bSQu Wenruo if (iter->cur_ptr >= iter->end_ptr) { 2375a37f232bSQu Wenruo ret = btrfs_next_item(fs_info->extent_root, path); 2376a37f232bSQu Wenruo 2377a37f232bSQu Wenruo /* No inline nor keyed ref */ 2378a37f232bSQu Wenruo if (ret > 0) { 2379a37f232bSQu Wenruo ret = -ENOENT; 2380a37f232bSQu Wenruo goto release; 2381a37f232bSQu Wenruo } 2382a37f232bSQu Wenruo if (ret < 0) 2383a37f232bSQu Wenruo goto release; 2384a37f232bSQu Wenruo 2385a37f232bSQu Wenruo btrfs_item_key_to_cpu(path->nodes[0], &iter->cur_key, 2386a37f232bSQu Wenruo path->slots[0]); 2387a37f232bSQu Wenruo if (iter->cur_key.objectid != bytenr || 2388a37f232bSQu Wenruo (iter->cur_key.type != BTRFS_SHARED_BLOCK_REF_KEY && 2389a37f232bSQu Wenruo iter->cur_key.type != BTRFS_TREE_BLOCK_REF_KEY)) { 2390a37f232bSQu Wenruo ret = -ENOENT; 2391a37f232bSQu Wenruo goto release; 2392a37f232bSQu Wenruo } 2393a37f232bSQu Wenruo iter->cur_ptr = (u32)btrfs_item_ptr_offset(path->nodes[0], 2394a37f232bSQu Wenruo path->slots[0]); 2395a37f232bSQu Wenruo iter->item_ptr = iter->cur_ptr; 2396a37f232bSQu Wenruo iter->end_ptr = (u32)(iter->item_ptr + btrfs_item_size_nr( 2397a37f232bSQu Wenruo path->nodes[0], path->slots[0])); 2398a37f232bSQu Wenruo } 2399a37f232bSQu Wenruo 2400a37f232bSQu Wenruo return 0; 2401a37f232bSQu Wenruo release: 2402a37f232bSQu Wenruo btrfs_backref_iter_release(iter); 2403a37f232bSQu Wenruo return ret; 2404a37f232bSQu Wenruo } 2405c39c2ddcSQu Wenruo 2406c39c2ddcSQu Wenruo /* 2407c39c2ddcSQu Wenruo * Go to the next backref item of current bytenr, can be either inlined or 2408c39c2ddcSQu Wenruo * keyed. 2409c39c2ddcSQu Wenruo * 2410c39c2ddcSQu Wenruo * Caller needs to check whether it's inline ref or not by iter->cur_key. 2411c39c2ddcSQu Wenruo * 2412c39c2ddcSQu Wenruo * Return 0 if we get next backref without problem. 2413c39c2ddcSQu Wenruo * Return >0 if there is no extra backref for this bytenr. 2414c39c2ddcSQu Wenruo * Return <0 if there is something wrong happened. 2415c39c2ddcSQu Wenruo */ 2416c39c2ddcSQu Wenruo int btrfs_backref_iter_next(struct btrfs_backref_iter *iter) 2417c39c2ddcSQu Wenruo { 2418c39c2ddcSQu Wenruo struct extent_buffer *eb = btrfs_backref_get_eb(iter); 2419c39c2ddcSQu Wenruo struct btrfs_path *path = iter->path; 2420c39c2ddcSQu Wenruo struct btrfs_extent_inline_ref *iref; 2421c39c2ddcSQu Wenruo int ret; 2422c39c2ddcSQu Wenruo u32 size; 2423c39c2ddcSQu Wenruo 2424c39c2ddcSQu Wenruo if (btrfs_backref_iter_is_inline_ref(iter)) { 2425c39c2ddcSQu Wenruo /* We're still inside the inline refs */ 2426c39c2ddcSQu Wenruo ASSERT(iter->cur_ptr < iter->end_ptr); 2427c39c2ddcSQu Wenruo 2428c39c2ddcSQu Wenruo if (btrfs_backref_has_tree_block_info(iter)) { 2429c39c2ddcSQu Wenruo /* First tree block info */ 2430c39c2ddcSQu Wenruo size = sizeof(struct btrfs_tree_block_info); 2431c39c2ddcSQu Wenruo } else { 2432c39c2ddcSQu Wenruo /* Use inline ref type to determine the size */ 2433c39c2ddcSQu Wenruo int type; 2434c39c2ddcSQu Wenruo 2435c39c2ddcSQu Wenruo iref = (struct btrfs_extent_inline_ref *) 2436c39c2ddcSQu Wenruo ((unsigned long)iter->cur_ptr); 2437c39c2ddcSQu Wenruo type = btrfs_extent_inline_ref_type(eb, iref); 2438c39c2ddcSQu Wenruo 2439c39c2ddcSQu Wenruo size = btrfs_extent_inline_ref_size(type); 2440c39c2ddcSQu Wenruo } 2441c39c2ddcSQu Wenruo iter->cur_ptr += size; 2442c39c2ddcSQu Wenruo if (iter->cur_ptr < iter->end_ptr) 2443c39c2ddcSQu Wenruo return 0; 2444c39c2ddcSQu Wenruo 2445c39c2ddcSQu Wenruo /* All inline items iterated, fall through */ 2446c39c2ddcSQu Wenruo } 2447c39c2ddcSQu Wenruo 2448c39c2ddcSQu Wenruo /* We're at keyed items, there is no inline item, go to the next one */ 2449c39c2ddcSQu Wenruo ret = btrfs_next_item(iter->fs_info->extent_root, iter->path); 2450c39c2ddcSQu Wenruo if (ret) 2451c39c2ddcSQu Wenruo return ret; 2452c39c2ddcSQu Wenruo 2453c39c2ddcSQu Wenruo btrfs_item_key_to_cpu(path->nodes[0], &iter->cur_key, path->slots[0]); 2454c39c2ddcSQu Wenruo if (iter->cur_key.objectid != iter->bytenr || 2455c39c2ddcSQu Wenruo (iter->cur_key.type != BTRFS_TREE_BLOCK_REF_KEY && 2456c39c2ddcSQu Wenruo iter->cur_key.type != BTRFS_SHARED_BLOCK_REF_KEY)) 2457c39c2ddcSQu Wenruo return 1; 2458c39c2ddcSQu Wenruo iter->item_ptr = (u32)btrfs_item_ptr_offset(path->nodes[0], 2459c39c2ddcSQu Wenruo path->slots[0]); 2460c39c2ddcSQu Wenruo iter->cur_ptr = iter->item_ptr; 2461c39c2ddcSQu Wenruo iter->end_ptr = iter->item_ptr + (u32)btrfs_item_size_nr(path->nodes[0], 2462c39c2ddcSQu Wenruo path->slots[0]); 2463c39c2ddcSQu Wenruo return 0; 2464c39c2ddcSQu Wenruo } 2465584fb121SQu Wenruo 2466584fb121SQu Wenruo void btrfs_backref_init_cache(struct btrfs_fs_info *fs_info, 2467584fb121SQu Wenruo struct btrfs_backref_cache *cache, int is_reloc) 2468584fb121SQu Wenruo { 2469584fb121SQu Wenruo int i; 2470584fb121SQu Wenruo 2471584fb121SQu Wenruo cache->rb_root = RB_ROOT; 2472584fb121SQu Wenruo for (i = 0; i < BTRFS_MAX_LEVEL; i++) 2473584fb121SQu Wenruo INIT_LIST_HEAD(&cache->pending[i]); 2474584fb121SQu Wenruo INIT_LIST_HEAD(&cache->changed); 2475584fb121SQu Wenruo INIT_LIST_HEAD(&cache->detached); 2476584fb121SQu Wenruo INIT_LIST_HEAD(&cache->leaves); 2477584fb121SQu Wenruo INIT_LIST_HEAD(&cache->pending_edge); 2478584fb121SQu Wenruo INIT_LIST_HEAD(&cache->useless_node); 2479584fb121SQu Wenruo cache->fs_info = fs_info; 2480584fb121SQu Wenruo cache->is_reloc = is_reloc; 2481584fb121SQu Wenruo } 2482b1818dabSQu Wenruo 2483b1818dabSQu Wenruo struct btrfs_backref_node *btrfs_backref_alloc_node( 2484b1818dabSQu Wenruo struct btrfs_backref_cache *cache, u64 bytenr, int level) 2485b1818dabSQu Wenruo { 2486b1818dabSQu Wenruo struct btrfs_backref_node *node; 2487b1818dabSQu Wenruo 2488b1818dabSQu Wenruo ASSERT(level >= 0 && level < BTRFS_MAX_LEVEL); 2489b1818dabSQu Wenruo node = kzalloc(sizeof(*node), GFP_NOFS); 2490b1818dabSQu Wenruo if (!node) 2491b1818dabSQu Wenruo return node; 2492b1818dabSQu Wenruo 2493b1818dabSQu Wenruo INIT_LIST_HEAD(&node->list); 2494b1818dabSQu Wenruo INIT_LIST_HEAD(&node->upper); 2495b1818dabSQu Wenruo INIT_LIST_HEAD(&node->lower); 2496b1818dabSQu Wenruo RB_CLEAR_NODE(&node->rb_node); 2497b1818dabSQu Wenruo cache->nr_nodes++; 2498b1818dabSQu Wenruo node->level = level; 2499b1818dabSQu Wenruo node->bytenr = bytenr; 2500b1818dabSQu Wenruo 2501b1818dabSQu Wenruo return node; 2502b1818dabSQu Wenruo } 250347254d07SQu Wenruo 250447254d07SQu Wenruo struct btrfs_backref_edge *btrfs_backref_alloc_edge( 250547254d07SQu Wenruo struct btrfs_backref_cache *cache) 250647254d07SQu Wenruo { 250747254d07SQu Wenruo struct btrfs_backref_edge *edge; 250847254d07SQu Wenruo 250947254d07SQu Wenruo edge = kzalloc(sizeof(*edge), GFP_NOFS); 251047254d07SQu Wenruo if (edge) 251147254d07SQu Wenruo cache->nr_edges++; 251247254d07SQu Wenruo return edge; 251347254d07SQu Wenruo } 2514023acb07SQu Wenruo 2515023acb07SQu Wenruo /* 2516023acb07SQu Wenruo * Drop the backref node from cache, also cleaning up all its 2517023acb07SQu Wenruo * upper edges and any uncached nodes in the path. 2518023acb07SQu Wenruo * 2519023acb07SQu Wenruo * This cleanup happens bottom up, thus the node should either 2520023acb07SQu Wenruo * be the lowest node in the cache or a detached node. 2521023acb07SQu Wenruo */ 2522023acb07SQu Wenruo void btrfs_backref_cleanup_node(struct btrfs_backref_cache *cache, 2523023acb07SQu Wenruo struct btrfs_backref_node *node) 2524023acb07SQu Wenruo { 2525023acb07SQu Wenruo struct btrfs_backref_node *upper; 2526023acb07SQu Wenruo struct btrfs_backref_edge *edge; 2527023acb07SQu Wenruo 2528023acb07SQu Wenruo if (!node) 2529023acb07SQu Wenruo return; 2530023acb07SQu Wenruo 2531023acb07SQu Wenruo BUG_ON(!node->lowest && !node->detached); 2532023acb07SQu Wenruo while (!list_empty(&node->upper)) { 2533023acb07SQu Wenruo edge = list_entry(node->upper.next, struct btrfs_backref_edge, 2534023acb07SQu Wenruo list[LOWER]); 2535023acb07SQu Wenruo upper = edge->node[UPPER]; 2536023acb07SQu Wenruo list_del(&edge->list[LOWER]); 2537023acb07SQu Wenruo list_del(&edge->list[UPPER]); 2538023acb07SQu Wenruo btrfs_backref_free_edge(cache, edge); 2539023acb07SQu Wenruo 2540023acb07SQu Wenruo if (RB_EMPTY_NODE(&upper->rb_node)) { 2541023acb07SQu Wenruo BUG_ON(!list_empty(&node->upper)); 2542023acb07SQu Wenruo btrfs_backref_drop_node(cache, node); 2543023acb07SQu Wenruo node = upper; 2544023acb07SQu Wenruo node->lowest = 1; 2545023acb07SQu Wenruo continue; 2546023acb07SQu Wenruo } 2547023acb07SQu Wenruo /* 2548023acb07SQu Wenruo * Add the node to leaf node list if no other child block 2549023acb07SQu Wenruo * cached. 2550023acb07SQu Wenruo */ 2551023acb07SQu Wenruo if (list_empty(&upper->lower)) { 2552023acb07SQu Wenruo list_add_tail(&upper->lower, &cache->leaves); 2553023acb07SQu Wenruo upper->lowest = 1; 2554023acb07SQu Wenruo } 2555023acb07SQu Wenruo } 2556023acb07SQu Wenruo 2557023acb07SQu Wenruo btrfs_backref_drop_node(cache, node); 2558023acb07SQu Wenruo } 255913fe1bdbSQu Wenruo 256013fe1bdbSQu Wenruo /* 256113fe1bdbSQu Wenruo * Release all nodes/edges from current cache 256213fe1bdbSQu Wenruo */ 256313fe1bdbSQu Wenruo void btrfs_backref_release_cache(struct btrfs_backref_cache *cache) 256413fe1bdbSQu Wenruo { 256513fe1bdbSQu Wenruo struct btrfs_backref_node *node; 256613fe1bdbSQu Wenruo int i; 256713fe1bdbSQu Wenruo 256813fe1bdbSQu Wenruo while (!list_empty(&cache->detached)) { 256913fe1bdbSQu Wenruo node = list_entry(cache->detached.next, 257013fe1bdbSQu Wenruo struct btrfs_backref_node, list); 257113fe1bdbSQu Wenruo btrfs_backref_cleanup_node(cache, node); 257213fe1bdbSQu Wenruo } 257313fe1bdbSQu Wenruo 257413fe1bdbSQu Wenruo while (!list_empty(&cache->leaves)) { 257513fe1bdbSQu Wenruo node = list_entry(cache->leaves.next, 257613fe1bdbSQu Wenruo struct btrfs_backref_node, lower); 257713fe1bdbSQu Wenruo btrfs_backref_cleanup_node(cache, node); 257813fe1bdbSQu Wenruo } 257913fe1bdbSQu Wenruo 258013fe1bdbSQu Wenruo cache->last_trans = 0; 258113fe1bdbSQu Wenruo 258213fe1bdbSQu Wenruo for (i = 0; i < BTRFS_MAX_LEVEL; i++) 258313fe1bdbSQu Wenruo ASSERT(list_empty(&cache->pending[i])); 258413fe1bdbSQu Wenruo ASSERT(list_empty(&cache->pending_edge)); 258513fe1bdbSQu Wenruo ASSERT(list_empty(&cache->useless_node)); 258613fe1bdbSQu Wenruo ASSERT(list_empty(&cache->changed)); 258713fe1bdbSQu Wenruo ASSERT(list_empty(&cache->detached)); 258813fe1bdbSQu Wenruo ASSERT(RB_EMPTY_ROOT(&cache->rb_root)); 258913fe1bdbSQu Wenruo ASSERT(!cache->nr_nodes); 259013fe1bdbSQu Wenruo ASSERT(!cache->nr_edges); 259113fe1bdbSQu Wenruo } 25921b60d2ecSQu Wenruo 25931b60d2ecSQu Wenruo /* 25941b60d2ecSQu Wenruo * Handle direct tree backref 25951b60d2ecSQu Wenruo * 25961b60d2ecSQu Wenruo * Direct tree backref means, the backref item shows its parent bytenr 25971b60d2ecSQu Wenruo * directly. This is for SHARED_BLOCK_REF backref (keyed or inlined). 25981b60d2ecSQu Wenruo * 25991b60d2ecSQu Wenruo * @ref_key: The converted backref key. 26001b60d2ecSQu Wenruo * For keyed backref, it's the item key. 26011b60d2ecSQu Wenruo * For inlined backref, objectid is the bytenr, 26021b60d2ecSQu Wenruo * type is btrfs_inline_ref_type, offset is 26031b60d2ecSQu Wenruo * btrfs_inline_ref_offset. 26041b60d2ecSQu Wenruo */ 26051b60d2ecSQu Wenruo static int handle_direct_tree_backref(struct btrfs_backref_cache *cache, 26061b60d2ecSQu Wenruo struct btrfs_key *ref_key, 26071b60d2ecSQu Wenruo struct btrfs_backref_node *cur) 26081b60d2ecSQu Wenruo { 26091b60d2ecSQu Wenruo struct btrfs_backref_edge *edge; 26101b60d2ecSQu Wenruo struct btrfs_backref_node *upper; 26111b60d2ecSQu Wenruo struct rb_node *rb_node; 26121b60d2ecSQu Wenruo 26131b60d2ecSQu Wenruo ASSERT(ref_key->type == BTRFS_SHARED_BLOCK_REF_KEY); 26141b60d2ecSQu Wenruo 26151b60d2ecSQu Wenruo /* Only reloc root uses backref pointing to itself */ 26161b60d2ecSQu Wenruo if (ref_key->objectid == ref_key->offset) { 26171b60d2ecSQu Wenruo struct btrfs_root *root; 26181b60d2ecSQu Wenruo 26191b60d2ecSQu Wenruo cur->is_reloc_root = 1; 26201b60d2ecSQu Wenruo /* Only reloc backref cache cares about a specific root */ 26211b60d2ecSQu Wenruo if (cache->is_reloc) { 26221b60d2ecSQu Wenruo root = find_reloc_root(cache->fs_info, cur->bytenr); 26231b60d2ecSQu Wenruo if (WARN_ON(!root)) 26241b60d2ecSQu Wenruo return -ENOENT; 26251b60d2ecSQu Wenruo cur->root = root; 26261b60d2ecSQu Wenruo } else { 26271b60d2ecSQu Wenruo /* 26281b60d2ecSQu Wenruo * For generic purpose backref cache, reloc root node 26291b60d2ecSQu Wenruo * is useless. 26301b60d2ecSQu Wenruo */ 26311b60d2ecSQu Wenruo list_add(&cur->list, &cache->useless_node); 26321b60d2ecSQu Wenruo } 26331b60d2ecSQu Wenruo return 0; 26341b60d2ecSQu Wenruo } 26351b60d2ecSQu Wenruo 26361b60d2ecSQu Wenruo edge = btrfs_backref_alloc_edge(cache); 26371b60d2ecSQu Wenruo if (!edge) 26381b60d2ecSQu Wenruo return -ENOMEM; 26391b60d2ecSQu Wenruo 26401b60d2ecSQu Wenruo rb_node = rb_simple_search(&cache->rb_root, ref_key->offset); 26411b60d2ecSQu Wenruo if (!rb_node) { 26421b60d2ecSQu Wenruo /* Parent node not yet cached */ 26431b60d2ecSQu Wenruo upper = btrfs_backref_alloc_node(cache, ref_key->offset, 26441b60d2ecSQu Wenruo cur->level + 1); 26451b60d2ecSQu Wenruo if (!upper) { 26461b60d2ecSQu Wenruo btrfs_backref_free_edge(cache, edge); 26471b60d2ecSQu Wenruo return -ENOMEM; 26481b60d2ecSQu Wenruo } 26491b60d2ecSQu Wenruo 26501b60d2ecSQu Wenruo /* 26511b60d2ecSQu Wenruo * Backrefs for the upper level block isn't cached, add the 26521b60d2ecSQu Wenruo * block to pending list 26531b60d2ecSQu Wenruo */ 26541b60d2ecSQu Wenruo list_add_tail(&edge->list[UPPER], &cache->pending_edge); 26551b60d2ecSQu Wenruo } else { 26561b60d2ecSQu Wenruo /* Parent node already cached */ 26571b60d2ecSQu Wenruo upper = rb_entry(rb_node, struct btrfs_backref_node, rb_node); 26581b60d2ecSQu Wenruo ASSERT(upper->checked); 26591b60d2ecSQu Wenruo INIT_LIST_HEAD(&edge->list[UPPER]); 26601b60d2ecSQu Wenruo } 26611b60d2ecSQu Wenruo btrfs_backref_link_edge(edge, cur, upper, LINK_LOWER); 26621b60d2ecSQu Wenruo return 0; 26631b60d2ecSQu Wenruo } 26641b60d2ecSQu Wenruo 26651b60d2ecSQu Wenruo /* 26661b60d2ecSQu Wenruo * Handle indirect tree backref 26671b60d2ecSQu Wenruo * 26681b60d2ecSQu Wenruo * Indirect tree backref means, we only know which tree the node belongs to. 26691b60d2ecSQu Wenruo * We still need to do a tree search to find out the parents. This is for 26701b60d2ecSQu Wenruo * TREE_BLOCK_REF backref (keyed or inlined). 26711b60d2ecSQu Wenruo * 26721b60d2ecSQu Wenruo * @ref_key: The same as @ref_key in handle_direct_tree_backref() 26731b60d2ecSQu Wenruo * @tree_key: The first key of this tree block. 26741b60d2ecSQu Wenruo * @path: A clean (released) path, to avoid allocating path everytime 26751b60d2ecSQu Wenruo * the function get called. 26761b60d2ecSQu Wenruo */ 26771b60d2ecSQu Wenruo static int handle_indirect_tree_backref(struct btrfs_backref_cache *cache, 26781b60d2ecSQu Wenruo struct btrfs_path *path, 26791b60d2ecSQu Wenruo struct btrfs_key *ref_key, 26801b60d2ecSQu Wenruo struct btrfs_key *tree_key, 26811b60d2ecSQu Wenruo struct btrfs_backref_node *cur) 26821b60d2ecSQu Wenruo { 26831b60d2ecSQu Wenruo struct btrfs_fs_info *fs_info = cache->fs_info; 26841b60d2ecSQu Wenruo struct btrfs_backref_node *upper; 26851b60d2ecSQu Wenruo struct btrfs_backref_node *lower; 26861b60d2ecSQu Wenruo struct btrfs_backref_edge *edge; 26871b60d2ecSQu Wenruo struct extent_buffer *eb; 26881b60d2ecSQu Wenruo struct btrfs_root *root; 26891b60d2ecSQu Wenruo struct rb_node *rb_node; 26901b60d2ecSQu Wenruo int level; 26911b60d2ecSQu Wenruo bool need_check = true; 26921b60d2ecSQu Wenruo int ret; 26931b60d2ecSQu Wenruo 269456e9357aSDavid Sterba root = btrfs_get_fs_root(fs_info, ref_key->offset, false); 26951b60d2ecSQu Wenruo if (IS_ERR(root)) 26961b60d2ecSQu Wenruo return PTR_ERR(root); 269792a7cc42SQu Wenruo if (!test_bit(BTRFS_ROOT_SHAREABLE, &root->state)) 26981b60d2ecSQu Wenruo cur->cowonly = 1; 26991b60d2ecSQu Wenruo 27001b60d2ecSQu Wenruo if (btrfs_root_level(&root->root_item) == cur->level) { 27011b60d2ecSQu Wenruo /* Tree root */ 27021b60d2ecSQu Wenruo ASSERT(btrfs_root_bytenr(&root->root_item) == cur->bytenr); 2703876de781SQu Wenruo /* 2704876de781SQu Wenruo * For reloc backref cache, we may ignore reloc root. But for 2705876de781SQu Wenruo * general purpose backref cache, we can't rely on 2706876de781SQu Wenruo * btrfs_should_ignore_reloc_root() as it may conflict with 2707876de781SQu Wenruo * current running relocation and lead to missing root. 2708876de781SQu Wenruo * 2709876de781SQu Wenruo * For general purpose backref cache, reloc root detection is 2710876de781SQu Wenruo * completely relying on direct backref (key->offset is parent 2711876de781SQu Wenruo * bytenr), thus only do such check for reloc cache. 2712876de781SQu Wenruo */ 2713876de781SQu Wenruo if (btrfs_should_ignore_reloc_root(root) && cache->is_reloc) { 27141b60d2ecSQu Wenruo btrfs_put_root(root); 27151b60d2ecSQu Wenruo list_add(&cur->list, &cache->useless_node); 27161b60d2ecSQu Wenruo } else { 27171b60d2ecSQu Wenruo cur->root = root; 27181b60d2ecSQu Wenruo } 27191b60d2ecSQu Wenruo return 0; 27201b60d2ecSQu Wenruo } 27211b60d2ecSQu Wenruo 27221b60d2ecSQu Wenruo level = cur->level + 1; 27231b60d2ecSQu Wenruo 27241b60d2ecSQu Wenruo /* Search the tree to find parent blocks referring to the block */ 27251b60d2ecSQu Wenruo path->search_commit_root = 1; 27261b60d2ecSQu Wenruo path->skip_locking = 1; 27271b60d2ecSQu Wenruo path->lowest_level = level; 27281b60d2ecSQu Wenruo ret = btrfs_search_slot(NULL, root, tree_key, path, 0, 0); 27291b60d2ecSQu Wenruo path->lowest_level = 0; 27301b60d2ecSQu Wenruo if (ret < 0) { 27311b60d2ecSQu Wenruo btrfs_put_root(root); 27321b60d2ecSQu Wenruo return ret; 27331b60d2ecSQu Wenruo } 27341b60d2ecSQu Wenruo if (ret > 0 && path->slots[level] > 0) 27351b60d2ecSQu Wenruo path->slots[level]--; 27361b60d2ecSQu Wenruo 27371b60d2ecSQu Wenruo eb = path->nodes[level]; 27381b60d2ecSQu Wenruo if (btrfs_node_blockptr(eb, path->slots[level]) != cur->bytenr) { 27391b60d2ecSQu Wenruo btrfs_err(fs_info, 27401b60d2ecSQu Wenruo "couldn't find block (%llu) (level %d) in tree (%llu) with key (%llu %u %llu)", 27411b60d2ecSQu Wenruo cur->bytenr, level - 1, root->root_key.objectid, 27421b60d2ecSQu Wenruo tree_key->objectid, tree_key->type, tree_key->offset); 27431b60d2ecSQu Wenruo btrfs_put_root(root); 27441b60d2ecSQu Wenruo ret = -ENOENT; 27451b60d2ecSQu Wenruo goto out; 27461b60d2ecSQu Wenruo } 27471b60d2ecSQu Wenruo lower = cur; 27481b60d2ecSQu Wenruo 27491b60d2ecSQu Wenruo /* Add all nodes and edges in the path */ 27501b60d2ecSQu Wenruo for (; level < BTRFS_MAX_LEVEL; level++) { 27511b60d2ecSQu Wenruo if (!path->nodes[level]) { 27521b60d2ecSQu Wenruo ASSERT(btrfs_root_bytenr(&root->root_item) == 27531b60d2ecSQu Wenruo lower->bytenr); 2754876de781SQu Wenruo /* Same as previous should_ignore_reloc_root() call */ 2755876de781SQu Wenruo if (btrfs_should_ignore_reloc_root(root) && 2756876de781SQu Wenruo cache->is_reloc) { 27571b60d2ecSQu Wenruo btrfs_put_root(root); 27581b60d2ecSQu Wenruo list_add(&lower->list, &cache->useless_node); 27591b60d2ecSQu Wenruo } else { 27601b60d2ecSQu Wenruo lower->root = root; 27611b60d2ecSQu Wenruo } 27621b60d2ecSQu Wenruo break; 27631b60d2ecSQu Wenruo } 27641b60d2ecSQu Wenruo 27651b60d2ecSQu Wenruo edge = btrfs_backref_alloc_edge(cache); 27661b60d2ecSQu Wenruo if (!edge) { 27671b60d2ecSQu Wenruo btrfs_put_root(root); 27681b60d2ecSQu Wenruo ret = -ENOMEM; 27691b60d2ecSQu Wenruo goto out; 27701b60d2ecSQu Wenruo } 27711b60d2ecSQu Wenruo 27721b60d2ecSQu Wenruo eb = path->nodes[level]; 27731b60d2ecSQu Wenruo rb_node = rb_simple_search(&cache->rb_root, eb->start); 27741b60d2ecSQu Wenruo if (!rb_node) { 27751b60d2ecSQu Wenruo upper = btrfs_backref_alloc_node(cache, eb->start, 27761b60d2ecSQu Wenruo lower->level + 1); 27771b60d2ecSQu Wenruo if (!upper) { 27781b60d2ecSQu Wenruo btrfs_put_root(root); 27791b60d2ecSQu Wenruo btrfs_backref_free_edge(cache, edge); 27801b60d2ecSQu Wenruo ret = -ENOMEM; 27811b60d2ecSQu Wenruo goto out; 27821b60d2ecSQu Wenruo } 27831b60d2ecSQu Wenruo upper->owner = btrfs_header_owner(eb); 278492a7cc42SQu Wenruo if (!test_bit(BTRFS_ROOT_SHAREABLE, &root->state)) 27851b60d2ecSQu Wenruo upper->cowonly = 1; 27861b60d2ecSQu Wenruo 27871b60d2ecSQu Wenruo /* 27881b60d2ecSQu Wenruo * If we know the block isn't shared we can avoid 27891b60d2ecSQu Wenruo * checking its backrefs. 27901b60d2ecSQu Wenruo */ 27911b60d2ecSQu Wenruo if (btrfs_block_can_be_shared(root, eb)) 27921b60d2ecSQu Wenruo upper->checked = 0; 27931b60d2ecSQu Wenruo else 27941b60d2ecSQu Wenruo upper->checked = 1; 27951b60d2ecSQu Wenruo 27961b60d2ecSQu Wenruo /* 27971b60d2ecSQu Wenruo * Add the block to pending list if we need to check its 27981b60d2ecSQu Wenruo * backrefs, we only do this once while walking up a 27991b60d2ecSQu Wenruo * tree as we will catch anything else later on. 28001b60d2ecSQu Wenruo */ 28011b60d2ecSQu Wenruo if (!upper->checked && need_check) { 28021b60d2ecSQu Wenruo need_check = false; 28031b60d2ecSQu Wenruo list_add_tail(&edge->list[UPPER], 28041b60d2ecSQu Wenruo &cache->pending_edge); 28051b60d2ecSQu Wenruo } else { 28061b60d2ecSQu Wenruo if (upper->checked) 28071b60d2ecSQu Wenruo need_check = true; 28081b60d2ecSQu Wenruo INIT_LIST_HEAD(&edge->list[UPPER]); 28091b60d2ecSQu Wenruo } 28101b60d2ecSQu Wenruo } else { 28111b60d2ecSQu Wenruo upper = rb_entry(rb_node, struct btrfs_backref_node, 28121b60d2ecSQu Wenruo rb_node); 28131b60d2ecSQu Wenruo ASSERT(upper->checked); 28141b60d2ecSQu Wenruo INIT_LIST_HEAD(&edge->list[UPPER]); 28151b60d2ecSQu Wenruo if (!upper->owner) 28161b60d2ecSQu Wenruo upper->owner = btrfs_header_owner(eb); 28171b60d2ecSQu Wenruo } 28181b60d2ecSQu Wenruo btrfs_backref_link_edge(edge, lower, upper, LINK_LOWER); 28191b60d2ecSQu Wenruo 28201b60d2ecSQu Wenruo if (rb_node) { 28211b60d2ecSQu Wenruo btrfs_put_root(root); 28221b60d2ecSQu Wenruo break; 28231b60d2ecSQu Wenruo } 28241b60d2ecSQu Wenruo lower = upper; 28251b60d2ecSQu Wenruo upper = NULL; 28261b60d2ecSQu Wenruo } 28271b60d2ecSQu Wenruo out: 28281b60d2ecSQu Wenruo btrfs_release_path(path); 28291b60d2ecSQu Wenruo return ret; 28301b60d2ecSQu Wenruo } 28311b60d2ecSQu Wenruo 28321b60d2ecSQu Wenruo /* 28331b60d2ecSQu Wenruo * Add backref node @cur into @cache. 28341b60d2ecSQu Wenruo * 28351b60d2ecSQu Wenruo * NOTE: Even if the function returned 0, @cur is not yet cached as its upper 28361b60d2ecSQu Wenruo * links aren't yet bi-directional. Needs to finish such links. 2837fc997ed0SQu Wenruo * Use btrfs_backref_finish_upper_links() to finish such linkage. 28381b60d2ecSQu Wenruo * 28391b60d2ecSQu Wenruo * @path: Released path for indirect tree backref lookup 28401b60d2ecSQu Wenruo * @iter: Released backref iter for extent tree search 28411b60d2ecSQu Wenruo * @node_key: The first key of the tree block 28421b60d2ecSQu Wenruo */ 28431b60d2ecSQu Wenruo int btrfs_backref_add_tree_node(struct btrfs_backref_cache *cache, 28441b60d2ecSQu Wenruo struct btrfs_path *path, 28451b60d2ecSQu Wenruo struct btrfs_backref_iter *iter, 28461b60d2ecSQu Wenruo struct btrfs_key *node_key, 28471b60d2ecSQu Wenruo struct btrfs_backref_node *cur) 28481b60d2ecSQu Wenruo { 28491b60d2ecSQu Wenruo struct btrfs_fs_info *fs_info = cache->fs_info; 28501b60d2ecSQu Wenruo struct btrfs_backref_edge *edge; 28511b60d2ecSQu Wenruo struct btrfs_backref_node *exist; 28521b60d2ecSQu Wenruo int ret; 28531b60d2ecSQu Wenruo 28541b60d2ecSQu Wenruo ret = btrfs_backref_iter_start(iter, cur->bytenr); 28551b60d2ecSQu Wenruo if (ret < 0) 28561b60d2ecSQu Wenruo return ret; 28571b60d2ecSQu Wenruo /* 28581b60d2ecSQu Wenruo * We skip the first btrfs_tree_block_info, as we don't use the key 28591b60d2ecSQu Wenruo * stored in it, but fetch it from the tree block 28601b60d2ecSQu Wenruo */ 28611b60d2ecSQu Wenruo if (btrfs_backref_has_tree_block_info(iter)) { 28621b60d2ecSQu Wenruo ret = btrfs_backref_iter_next(iter); 28631b60d2ecSQu Wenruo if (ret < 0) 28641b60d2ecSQu Wenruo goto out; 28651b60d2ecSQu Wenruo /* No extra backref? This means the tree block is corrupted */ 28661b60d2ecSQu Wenruo if (ret > 0) { 28671b60d2ecSQu Wenruo ret = -EUCLEAN; 28681b60d2ecSQu Wenruo goto out; 28691b60d2ecSQu Wenruo } 28701b60d2ecSQu Wenruo } 28711b60d2ecSQu Wenruo WARN_ON(cur->checked); 28721b60d2ecSQu Wenruo if (!list_empty(&cur->upper)) { 28731b60d2ecSQu Wenruo /* 28741b60d2ecSQu Wenruo * The backref was added previously when processing backref of 28751b60d2ecSQu Wenruo * type BTRFS_TREE_BLOCK_REF_KEY 28761b60d2ecSQu Wenruo */ 28771b60d2ecSQu Wenruo ASSERT(list_is_singular(&cur->upper)); 28781b60d2ecSQu Wenruo edge = list_entry(cur->upper.next, struct btrfs_backref_edge, 28791b60d2ecSQu Wenruo list[LOWER]); 28801b60d2ecSQu Wenruo ASSERT(list_empty(&edge->list[UPPER])); 28811b60d2ecSQu Wenruo exist = edge->node[UPPER]; 28821b60d2ecSQu Wenruo /* 28831b60d2ecSQu Wenruo * Add the upper level block to pending list if we need check 28841b60d2ecSQu Wenruo * its backrefs 28851b60d2ecSQu Wenruo */ 28861b60d2ecSQu Wenruo if (!exist->checked) 28871b60d2ecSQu Wenruo list_add_tail(&edge->list[UPPER], &cache->pending_edge); 28881b60d2ecSQu Wenruo } else { 28891b60d2ecSQu Wenruo exist = NULL; 28901b60d2ecSQu Wenruo } 28911b60d2ecSQu Wenruo 28921b60d2ecSQu Wenruo for (; ret == 0; ret = btrfs_backref_iter_next(iter)) { 28931b60d2ecSQu Wenruo struct extent_buffer *eb; 28941b60d2ecSQu Wenruo struct btrfs_key key; 28951b60d2ecSQu Wenruo int type; 28961b60d2ecSQu Wenruo 28971b60d2ecSQu Wenruo cond_resched(); 28981b60d2ecSQu Wenruo eb = btrfs_backref_get_eb(iter); 28991b60d2ecSQu Wenruo 29001b60d2ecSQu Wenruo key.objectid = iter->bytenr; 29011b60d2ecSQu Wenruo if (btrfs_backref_iter_is_inline_ref(iter)) { 29021b60d2ecSQu Wenruo struct btrfs_extent_inline_ref *iref; 29031b60d2ecSQu Wenruo 29041b60d2ecSQu Wenruo /* Update key for inline backref */ 29051b60d2ecSQu Wenruo iref = (struct btrfs_extent_inline_ref *) 29061b60d2ecSQu Wenruo ((unsigned long)iter->cur_ptr); 29071b60d2ecSQu Wenruo type = btrfs_get_extent_inline_ref_type(eb, iref, 29081b60d2ecSQu Wenruo BTRFS_REF_TYPE_BLOCK); 29091b60d2ecSQu Wenruo if (type == BTRFS_REF_TYPE_INVALID) { 29101b60d2ecSQu Wenruo ret = -EUCLEAN; 29111b60d2ecSQu Wenruo goto out; 29121b60d2ecSQu Wenruo } 29131b60d2ecSQu Wenruo key.type = type; 29141b60d2ecSQu Wenruo key.offset = btrfs_extent_inline_ref_offset(eb, iref); 29151b60d2ecSQu Wenruo } else { 29161b60d2ecSQu Wenruo key.type = iter->cur_key.type; 29171b60d2ecSQu Wenruo key.offset = iter->cur_key.offset; 29181b60d2ecSQu Wenruo } 29191b60d2ecSQu Wenruo 29201b60d2ecSQu Wenruo /* 29211b60d2ecSQu Wenruo * Parent node found and matches current inline ref, no need to 29221b60d2ecSQu Wenruo * rebuild this node for this inline ref 29231b60d2ecSQu Wenruo */ 29241b60d2ecSQu Wenruo if (exist && 29251b60d2ecSQu Wenruo ((key.type == BTRFS_TREE_BLOCK_REF_KEY && 29261b60d2ecSQu Wenruo exist->owner == key.offset) || 29271b60d2ecSQu Wenruo (key.type == BTRFS_SHARED_BLOCK_REF_KEY && 29281b60d2ecSQu Wenruo exist->bytenr == key.offset))) { 29291b60d2ecSQu Wenruo exist = NULL; 29301b60d2ecSQu Wenruo continue; 29311b60d2ecSQu Wenruo } 29321b60d2ecSQu Wenruo 29331b60d2ecSQu Wenruo /* SHARED_BLOCK_REF means key.offset is the parent bytenr */ 29341b60d2ecSQu Wenruo if (key.type == BTRFS_SHARED_BLOCK_REF_KEY) { 29351b60d2ecSQu Wenruo ret = handle_direct_tree_backref(cache, &key, cur); 29361b60d2ecSQu Wenruo if (ret < 0) 29371b60d2ecSQu Wenruo goto out; 29381b60d2ecSQu Wenruo continue; 29391b60d2ecSQu Wenruo } else if (unlikely(key.type == BTRFS_EXTENT_REF_V0_KEY)) { 29401b60d2ecSQu Wenruo ret = -EINVAL; 29411b60d2ecSQu Wenruo btrfs_print_v0_err(fs_info); 29421b60d2ecSQu Wenruo btrfs_handle_fs_error(fs_info, ret, NULL); 29431b60d2ecSQu Wenruo goto out; 29441b60d2ecSQu Wenruo } else if (key.type != BTRFS_TREE_BLOCK_REF_KEY) { 29451b60d2ecSQu Wenruo continue; 29461b60d2ecSQu Wenruo } 29471b60d2ecSQu Wenruo 29481b60d2ecSQu Wenruo /* 29491b60d2ecSQu Wenruo * key.type == BTRFS_TREE_BLOCK_REF_KEY, inline ref offset 29501b60d2ecSQu Wenruo * means the root objectid. We need to search the tree to get 29511b60d2ecSQu Wenruo * its parent bytenr. 29521b60d2ecSQu Wenruo */ 29531b60d2ecSQu Wenruo ret = handle_indirect_tree_backref(cache, path, &key, node_key, 29541b60d2ecSQu Wenruo cur); 29551b60d2ecSQu Wenruo if (ret < 0) 29561b60d2ecSQu Wenruo goto out; 29571b60d2ecSQu Wenruo } 29581b60d2ecSQu Wenruo ret = 0; 29591b60d2ecSQu Wenruo cur->checked = 1; 29601b60d2ecSQu Wenruo WARN_ON(exist); 29611b60d2ecSQu Wenruo out: 29621b60d2ecSQu Wenruo btrfs_backref_iter_release(iter); 29631b60d2ecSQu Wenruo return ret; 29641b60d2ecSQu Wenruo } 2965fc997ed0SQu Wenruo 2966fc997ed0SQu Wenruo /* 2967fc997ed0SQu Wenruo * Finish the upwards linkage created by btrfs_backref_add_tree_node() 2968fc997ed0SQu Wenruo */ 2969fc997ed0SQu Wenruo int btrfs_backref_finish_upper_links(struct btrfs_backref_cache *cache, 2970fc997ed0SQu Wenruo struct btrfs_backref_node *start) 2971fc997ed0SQu Wenruo { 2972fc997ed0SQu Wenruo struct list_head *useless_node = &cache->useless_node; 2973fc997ed0SQu Wenruo struct btrfs_backref_edge *edge; 2974fc997ed0SQu Wenruo struct rb_node *rb_node; 2975fc997ed0SQu Wenruo LIST_HEAD(pending_edge); 2976fc997ed0SQu Wenruo 2977fc997ed0SQu Wenruo ASSERT(start->checked); 2978fc997ed0SQu Wenruo 2979fc997ed0SQu Wenruo /* Insert this node to cache if it's not COW-only */ 2980fc997ed0SQu Wenruo if (!start->cowonly) { 2981fc997ed0SQu Wenruo rb_node = rb_simple_insert(&cache->rb_root, start->bytenr, 2982fc997ed0SQu Wenruo &start->rb_node); 2983fc997ed0SQu Wenruo if (rb_node) 2984fc997ed0SQu Wenruo btrfs_backref_panic(cache->fs_info, start->bytenr, 2985fc997ed0SQu Wenruo -EEXIST); 2986fc997ed0SQu Wenruo list_add_tail(&start->lower, &cache->leaves); 2987fc997ed0SQu Wenruo } 2988fc997ed0SQu Wenruo 2989fc997ed0SQu Wenruo /* 2990fc997ed0SQu Wenruo * Use breadth first search to iterate all related edges. 2991fc997ed0SQu Wenruo * 2992fc997ed0SQu Wenruo * The starting points are all the edges of this node 2993fc997ed0SQu Wenruo */ 2994fc997ed0SQu Wenruo list_for_each_entry(edge, &start->upper, list[LOWER]) 2995fc997ed0SQu Wenruo list_add_tail(&edge->list[UPPER], &pending_edge); 2996fc997ed0SQu Wenruo 2997fc997ed0SQu Wenruo while (!list_empty(&pending_edge)) { 2998fc997ed0SQu Wenruo struct btrfs_backref_node *upper; 2999fc997ed0SQu Wenruo struct btrfs_backref_node *lower; 3000fc997ed0SQu Wenruo struct rb_node *rb_node; 3001fc997ed0SQu Wenruo 3002fc997ed0SQu Wenruo edge = list_first_entry(&pending_edge, 3003fc997ed0SQu Wenruo struct btrfs_backref_edge, list[UPPER]); 3004fc997ed0SQu Wenruo list_del_init(&edge->list[UPPER]); 3005fc997ed0SQu Wenruo upper = edge->node[UPPER]; 3006fc997ed0SQu Wenruo lower = edge->node[LOWER]; 3007fc997ed0SQu Wenruo 3008fc997ed0SQu Wenruo /* Parent is detached, no need to keep any edges */ 3009fc997ed0SQu Wenruo if (upper->detached) { 3010fc997ed0SQu Wenruo list_del(&edge->list[LOWER]); 3011fc997ed0SQu Wenruo btrfs_backref_free_edge(cache, edge); 3012fc997ed0SQu Wenruo 3013fc997ed0SQu Wenruo /* Lower node is orphan, queue for cleanup */ 3014fc997ed0SQu Wenruo if (list_empty(&lower->upper)) 3015fc997ed0SQu Wenruo list_add(&lower->list, useless_node); 3016fc997ed0SQu Wenruo continue; 3017fc997ed0SQu Wenruo } 3018fc997ed0SQu Wenruo 3019fc997ed0SQu Wenruo /* 3020fc997ed0SQu Wenruo * All new nodes added in current build_backref_tree() haven't 3021fc997ed0SQu Wenruo * been linked to the cache rb tree. 3022fc997ed0SQu Wenruo * So if we have upper->rb_node populated, this means a cache 3023fc997ed0SQu Wenruo * hit. We only need to link the edge, as @upper and all its 3024fc997ed0SQu Wenruo * parents have already been linked. 3025fc997ed0SQu Wenruo */ 3026fc997ed0SQu Wenruo if (!RB_EMPTY_NODE(&upper->rb_node)) { 3027fc997ed0SQu Wenruo if (upper->lowest) { 3028fc997ed0SQu Wenruo list_del_init(&upper->lower); 3029fc997ed0SQu Wenruo upper->lowest = 0; 3030fc997ed0SQu Wenruo } 3031fc997ed0SQu Wenruo 3032fc997ed0SQu Wenruo list_add_tail(&edge->list[UPPER], &upper->lower); 3033fc997ed0SQu Wenruo continue; 3034fc997ed0SQu Wenruo } 3035fc997ed0SQu Wenruo 3036fc997ed0SQu Wenruo /* Sanity check, we shouldn't have any unchecked nodes */ 3037fc997ed0SQu Wenruo if (!upper->checked) { 3038fc997ed0SQu Wenruo ASSERT(0); 3039fc997ed0SQu Wenruo return -EUCLEAN; 3040fc997ed0SQu Wenruo } 3041fc997ed0SQu Wenruo 3042fc997ed0SQu Wenruo /* Sanity check, COW-only node has non-COW-only parent */ 3043fc997ed0SQu Wenruo if (start->cowonly != upper->cowonly) { 3044fc997ed0SQu Wenruo ASSERT(0); 3045fc997ed0SQu Wenruo return -EUCLEAN; 3046fc997ed0SQu Wenruo } 3047fc997ed0SQu Wenruo 3048fc997ed0SQu Wenruo /* Only cache non-COW-only (subvolume trees) tree blocks */ 3049fc997ed0SQu Wenruo if (!upper->cowonly) { 3050fc997ed0SQu Wenruo rb_node = rb_simple_insert(&cache->rb_root, upper->bytenr, 3051fc997ed0SQu Wenruo &upper->rb_node); 3052fc997ed0SQu Wenruo if (rb_node) { 3053fc997ed0SQu Wenruo btrfs_backref_panic(cache->fs_info, 3054fc997ed0SQu Wenruo upper->bytenr, -EEXIST); 3055fc997ed0SQu Wenruo return -EUCLEAN; 3056fc997ed0SQu Wenruo } 3057fc997ed0SQu Wenruo } 3058fc997ed0SQu Wenruo 3059fc997ed0SQu Wenruo list_add_tail(&edge->list[UPPER], &upper->lower); 3060fc997ed0SQu Wenruo 3061fc997ed0SQu Wenruo /* 3062fc997ed0SQu Wenruo * Also queue all the parent edges of this uncached node 3063fc997ed0SQu Wenruo * to finish the upper linkage 3064fc997ed0SQu Wenruo */ 3065fc997ed0SQu Wenruo list_for_each_entry(edge, &upper->upper, list[LOWER]) 3066fc997ed0SQu Wenruo list_add_tail(&edge->list[UPPER], &pending_edge); 3067fc997ed0SQu Wenruo } 3068fc997ed0SQu Wenruo return 0; 3069fc997ed0SQu Wenruo } 30701b23ea18SQu Wenruo 30711b23ea18SQu Wenruo void btrfs_backref_error_cleanup(struct btrfs_backref_cache *cache, 30721b23ea18SQu Wenruo struct btrfs_backref_node *node) 30731b23ea18SQu Wenruo { 30741b23ea18SQu Wenruo struct btrfs_backref_node *lower; 30751b23ea18SQu Wenruo struct btrfs_backref_node *upper; 30761b23ea18SQu Wenruo struct btrfs_backref_edge *edge; 30771b23ea18SQu Wenruo 30781b23ea18SQu Wenruo while (!list_empty(&cache->useless_node)) { 30791b23ea18SQu Wenruo lower = list_first_entry(&cache->useless_node, 30801b23ea18SQu Wenruo struct btrfs_backref_node, list); 30811b23ea18SQu Wenruo list_del_init(&lower->list); 30821b23ea18SQu Wenruo } 30831b23ea18SQu Wenruo while (!list_empty(&cache->pending_edge)) { 30841b23ea18SQu Wenruo edge = list_first_entry(&cache->pending_edge, 30851b23ea18SQu Wenruo struct btrfs_backref_edge, list[UPPER]); 30861b23ea18SQu Wenruo list_del(&edge->list[UPPER]); 30871b23ea18SQu Wenruo list_del(&edge->list[LOWER]); 30881b23ea18SQu Wenruo lower = edge->node[LOWER]; 30891b23ea18SQu Wenruo upper = edge->node[UPPER]; 30901b23ea18SQu Wenruo btrfs_backref_free_edge(cache, edge); 30911b23ea18SQu Wenruo 30921b23ea18SQu Wenruo /* 30931b23ea18SQu Wenruo * Lower is no longer linked to any upper backref nodes and 30941b23ea18SQu Wenruo * isn't in the cache, we can free it ourselves. 30951b23ea18SQu Wenruo */ 30961b23ea18SQu Wenruo if (list_empty(&lower->upper) && 30971b23ea18SQu Wenruo RB_EMPTY_NODE(&lower->rb_node)) 30981b23ea18SQu Wenruo list_add(&lower->list, &cache->useless_node); 30991b23ea18SQu Wenruo 31001b23ea18SQu Wenruo if (!RB_EMPTY_NODE(&upper->rb_node)) 31011b23ea18SQu Wenruo continue; 31021b23ea18SQu Wenruo 31031b23ea18SQu Wenruo /* Add this guy's upper edges to the list to process */ 31041b23ea18SQu Wenruo list_for_each_entry(edge, &upper->upper, list[LOWER]) 31051b23ea18SQu Wenruo list_add_tail(&edge->list[UPPER], 31061b23ea18SQu Wenruo &cache->pending_edge); 31071b23ea18SQu Wenruo if (list_empty(&upper->upper)) 31081b23ea18SQu Wenruo list_add(&upper->list, &cache->useless_node); 31091b23ea18SQu Wenruo } 31101b23ea18SQu Wenruo 31111b23ea18SQu Wenruo while (!list_empty(&cache->useless_node)) { 31121b23ea18SQu Wenruo lower = list_first_entry(&cache->useless_node, 31131b23ea18SQu Wenruo struct btrfs_backref_node, list); 31141b23ea18SQu Wenruo list_del_init(&lower->list); 31151b23ea18SQu Wenruo if (lower == node) 31161b23ea18SQu Wenruo node = NULL; 31171b23ea18SQu Wenruo btrfs_backref_free_node(cache, lower); 31181b23ea18SQu Wenruo } 31191b23ea18SQu Wenruo 31201b23ea18SQu Wenruo btrfs_backref_cleanup_node(cache, node); 31211b23ea18SQu Wenruo ASSERT(list_empty(&cache->useless_node) && 31221b23ea18SQu Wenruo list_empty(&cache->pending_edge)); 31231b23ea18SQu Wenruo } 3124