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" 17f3a84ccdSFilipe Manana #include "tree-mod-log.h" 18a542ad1bSJan Schmidt 19dc046b10SJosef Bacik /* Just an arbitrary number so we can be sure this happened */ 20dc046b10SJosef Bacik #define BACKREF_FOUND_SHARED 6 21dc046b10SJosef Bacik 22976b1908SJan Schmidt struct extent_inode_elem { 23976b1908SJan Schmidt u64 inum; 24976b1908SJan Schmidt u64 offset; 25976b1908SJan Schmidt struct extent_inode_elem *next; 26976b1908SJan Schmidt }; 27976b1908SJan Schmidt 2873980becSJeff Mahoney static int check_extent_in_eb(const struct btrfs_key *key, 2973980becSJeff Mahoney const struct extent_buffer *eb, 3073980becSJeff Mahoney const struct btrfs_file_extent_item *fi, 31976b1908SJan Schmidt u64 extent_item_pos, 32c995ab3cSZygo Blaxell struct extent_inode_elem **eie, 33c995ab3cSZygo Blaxell bool ignore_offset) 34976b1908SJan Schmidt { 358ca15e05SJosef Bacik u64 offset = 0; 368ca15e05SJosef Bacik struct extent_inode_elem *e; 378ca15e05SJosef Bacik 38c995ab3cSZygo Blaxell if (!ignore_offset && 39c995ab3cSZygo Blaxell !btrfs_file_extent_compression(eb, fi) && 408ca15e05SJosef Bacik !btrfs_file_extent_encryption(eb, fi) && 418ca15e05SJosef Bacik !btrfs_file_extent_other_encoding(eb, fi)) { 42976b1908SJan Schmidt u64 data_offset; 43976b1908SJan Schmidt u64 data_len; 44976b1908SJan Schmidt 45976b1908SJan Schmidt data_offset = btrfs_file_extent_offset(eb, fi); 46976b1908SJan Schmidt data_len = btrfs_file_extent_num_bytes(eb, fi); 47976b1908SJan Schmidt 48976b1908SJan Schmidt if (extent_item_pos < data_offset || 49976b1908SJan Schmidt extent_item_pos >= data_offset + data_len) 50976b1908SJan Schmidt return 1; 518ca15e05SJosef Bacik offset = extent_item_pos - data_offset; 528ca15e05SJosef Bacik } 53976b1908SJan Schmidt 54976b1908SJan Schmidt e = kmalloc(sizeof(*e), GFP_NOFS); 55976b1908SJan Schmidt if (!e) 56976b1908SJan Schmidt return -ENOMEM; 57976b1908SJan Schmidt 58976b1908SJan Schmidt e->next = *eie; 59976b1908SJan Schmidt e->inum = key->objectid; 608ca15e05SJosef Bacik e->offset = key->offset + offset; 61976b1908SJan Schmidt *eie = e; 62976b1908SJan Schmidt 63976b1908SJan Schmidt return 0; 64976b1908SJan Schmidt } 65976b1908SJan Schmidt 66f05c4746SWang Shilong static void free_inode_elem_list(struct extent_inode_elem *eie) 67f05c4746SWang Shilong { 68f05c4746SWang Shilong struct extent_inode_elem *eie_next; 69f05c4746SWang Shilong 70f05c4746SWang Shilong for (; eie; eie = eie_next) { 71f05c4746SWang Shilong eie_next = eie->next; 72f05c4746SWang Shilong kfree(eie); 73f05c4746SWang Shilong } 74f05c4746SWang Shilong } 75f05c4746SWang Shilong 7673980becSJeff Mahoney static int find_extent_in_eb(const struct extent_buffer *eb, 7773980becSJeff Mahoney u64 wanted_disk_byte, u64 extent_item_pos, 78c995ab3cSZygo Blaxell struct extent_inode_elem **eie, 79c995ab3cSZygo Blaxell bool ignore_offset) 80976b1908SJan Schmidt { 81976b1908SJan Schmidt u64 disk_byte; 82976b1908SJan Schmidt struct btrfs_key key; 83976b1908SJan Schmidt struct btrfs_file_extent_item *fi; 84976b1908SJan Schmidt int slot; 85976b1908SJan Schmidt int nritems; 86976b1908SJan Schmidt int extent_type; 87976b1908SJan Schmidt int ret; 88976b1908SJan Schmidt 89976b1908SJan Schmidt /* 90976b1908SJan Schmidt * from the shared data ref, we only have the leaf but we need 91976b1908SJan Schmidt * the key. thus, we must look into all items and see that we 92976b1908SJan Schmidt * find one (some) with a reference to our extent item. 93976b1908SJan Schmidt */ 94976b1908SJan Schmidt nritems = btrfs_header_nritems(eb); 95976b1908SJan Schmidt for (slot = 0; slot < nritems; ++slot) { 96976b1908SJan Schmidt btrfs_item_key_to_cpu(eb, &key, slot); 97976b1908SJan Schmidt if (key.type != BTRFS_EXTENT_DATA_KEY) 98976b1908SJan Schmidt continue; 99976b1908SJan Schmidt fi = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item); 100976b1908SJan Schmidt extent_type = btrfs_file_extent_type(eb, fi); 101976b1908SJan Schmidt if (extent_type == BTRFS_FILE_EXTENT_INLINE) 102976b1908SJan Schmidt continue; 103976b1908SJan Schmidt /* don't skip BTRFS_FILE_EXTENT_PREALLOC, we can handle that */ 104976b1908SJan Schmidt disk_byte = btrfs_file_extent_disk_bytenr(eb, fi); 105976b1908SJan Schmidt if (disk_byte != wanted_disk_byte) 106976b1908SJan Schmidt continue; 107976b1908SJan Schmidt 108c995ab3cSZygo Blaxell ret = check_extent_in_eb(&key, eb, fi, extent_item_pos, eie, ignore_offset); 109976b1908SJan Schmidt if (ret < 0) 110976b1908SJan Schmidt return ret; 111976b1908SJan Schmidt } 112976b1908SJan Schmidt 113976b1908SJan Schmidt return 0; 114976b1908SJan Schmidt } 115976b1908SJan Schmidt 11686d5f994SEdmund Nadolski struct preftree { 117ecf160b4SLiu Bo struct rb_root_cached root; 1186c336b21SJeff Mahoney unsigned int count; 11986d5f994SEdmund Nadolski }; 12086d5f994SEdmund Nadolski 121ecf160b4SLiu Bo #define PREFTREE_INIT { .root = RB_ROOT_CACHED, .count = 0 } 12286d5f994SEdmund Nadolski 12386d5f994SEdmund Nadolski struct preftrees { 12486d5f994SEdmund Nadolski struct preftree direct; /* BTRFS_SHARED_[DATA|BLOCK]_REF_KEY */ 12586d5f994SEdmund Nadolski struct preftree indirect; /* BTRFS_[TREE_BLOCK|EXTENT_DATA]_REF_KEY */ 12686d5f994SEdmund Nadolski struct preftree indirect_missing_keys; 12786d5f994SEdmund Nadolski }; 12886d5f994SEdmund Nadolski 1293ec4d323SEdmund Nadolski /* 1303ec4d323SEdmund Nadolski * Checks for a shared extent during backref search. 1313ec4d323SEdmund Nadolski * 1323ec4d323SEdmund Nadolski * The share_count tracks prelim_refs (direct and indirect) having a 1333ec4d323SEdmund Nadolski * ref->count >0: 1343ec4d323SEdmund Nadolski * - incremented when a ref->count transitions to >0 1353ec4d323SEdmund Nadolski * - decremented when a ref->count transitions to <1 1363ec4d323SEdmund Nadolski */ 1373ec4d323SEdmund Nadolski struct share_check { 1383ec4d323SEdmund Nadolski u64 root_objectid; 1393ec4d323SEdmund Nadolski u64 inum; 1403ec4d323SEdmund Nadolski int share_count; 1413ec4d323SEdmund Nadolski }; 1423ec4d323SEdmund Nadolski 1433ec4d323SEdmund Nadolski static inline int extent_is_shared(struct share_check *sc) 1443ec4d323SEdmund Nadolski { 1453ec4d323SEdmund Nadolski return (sc && sc->share_count > 1) ? BACKREF_FOUND_SHARED : 0; 1463ec4d323SEdmund Nadolski } 1473ec4d323SEdmund Nadolski 148b9e9a6cbSWang Shilong static struct kmem_cache *btrfs_prelim_ref_cache; 149b9e9a6cbSWang Shilong 150b9e9a6cbSWang Shilong int __init btrfs_prelim_ref_init(void) 151b9e9a6cbSWang Shilong { 152b9e9a6cbSWang Shilong btrfs_prelim_ref_cache = kmem_cache_create("btrfs_prelim_ref", 153e0c476b1SJeff Mahoney sizeof(struct prelim_ref), 154b9e9a6cbSWang Shilong 0, 155fba4b697SNikolay Borisov SLAB_MEM_SPREAD, 156b9e9a6cbSWang Shilong NULL); 157b9e9a6cbSWang Shilong if (!btrfs_prelim_ref_cache) 158b9e9a6cbSWang Shilong return -ENOMEM; 159b9e9a6cbSWang Shilong return 0; 160b9e9a6cbSWang Shilong } 161b9e9a6cbSWang Shilong 162e67c718bSDavid Sterba void __cold btrfs_prelim_ref_exit(void) 163b9e9a6cbSWang Shilong { 164b9e9a6cbSWang Shilong kmem_cache_destroy(btrfs_prelim_ref_cache); 165b9e9a6cbSWang Shilong } 166b9e9a6cbSWang Shilong 16786d5f994SEdmund Nadolski static void free_pref(struct prelim_ref *ref) 16886d5f994SEdmund Nadolski { 16986d5f994SEdmund Nadolski kmem_cache_free(btrfs_prelim_ref_cache, ref); 17086d5f994SEdmund Nadolski } 17186d5f994SEdmund Nadolski 17286d5f994SEdmund Nadolski /* 17386d5f994SEdmund Nadolski * Return 0 when both refs are for the same block (and can be merged). 17486d5f994SEdmund Nadolski * A -1 return indicates ref1 is a 'lower' block than ref2, while 1 17586d5f994SEdmund Nadolski * indicates a 'higher' block. 17686d5f994SEdmund Nadolski */ 17786d5f994SEdmund Nadolski static int prelim_ref_compare(struct prelim_ref *ref1, 17886d5f994SEdmund Nadolski struct prelim_ref *ref2) 17986d5f994SEdmund Nadolski { 18086d5f994SEdmund Nadolski if (ref1->level < ref2->level) 18186d5f994SEdmund Nadolski return -1; 18286d5f994SEdmund Nadolski if (ref1->level > ref2->level) 18386d5f994SEdmund Nadolski return 1; 18486d5f994SEdmund Nadolski if (ref1->root_id < ref2->root_id) 18586d5f994SEdmund Nadolski return -1; 18686d5f994SEdmund Nadolski if (ref1->root_id > ref2->root_id) 18786d5f994SEdmund Nadolski return 1; 18886d5f994SEdmund Nadolski if (ref1->key_for_search.type < ref2->key_for_search.type) 18986d5f994SEdmund Nadolski return -1; 19086d5f994SEdmund Nadolski if (ref1->key_for_search.type > ref2->key_for_search.type) 19186d5f994SEdmund Nadolski return 1; 19286d5f994SEdmund Nadolski if (ref1->key_for_search.objectid < ref2->key_for_search.objectid) 19386d5f994SEdmund Nadolski return -1; 19486d5f994SEdmund Nadolski if (ref1->key_for_search.objectid > ref2->key_for_search.objectid) 19586d5f994SEdmund Nadolski return 1; 19686d5f994SEdmund Nadolski if (ref1->key_for_search.offset < ref2->key_for_search.offset) 19786d5f994SEdmund Nadolski return -1; 19886d5f994SEdmund Nadolski if (ref1->key_for_search.offset > ref2->key_for_search.offset) 19986d5f994SEdmund Nadolski return 1; 20086d5f994SEdmund Nadolski if (ref1->parent < ref2->parent) 20186d5f994SEdmund Nadolski return -1; 20286d5f994SEdmund Nadolski if (ref1->parent > ref2->parent) 20386d5f994SEdmund Nadolski return 1; 20486d5f994SEdmund Nadolski 20586d5f994SEdmund Nadolski return 0; 20686d5f994SEdmund Nadolski } 20786d5f994SEdmund Nadolski 208ccc8dc75SColin Ian King static void update_share_count(struct share_check *sc, int oldcount, 209ccc8dc75SColin Ian King int newcount) 2103ec4d323SEdmund Nadolski { 2113ec4d323SEdmund Nadolski if ((!sc) || (oldcount == 0 && newcount < 1)) 2123ec4d323SEdmund Nadolski return; 2133ec4d323SEdmund Nadolski 2143ec4d323SEdmund Nadolski if (oldcount > 0 && newcount < 1) 2153ec4d323SEdmund Nadolski sc->share_count--; 2163ec4d323SEdmund Nadolski else if (oldcount < 1 && newcount > 0) 2173ec4d323SEdmund Nadolski sc->share_count++; 2183ec4d323SEdmund Nadolski } 2193ec4d323SEdmund Nadolski 22086d5f994SEdmund Nadolski /* 22186d5f994SEdmund Nadolski * Add @newref to the @root rbtree, merging identical refs. 22286d5f994SEdmund Nadolski * 2233ec4d323SEdmund Nadolski * Callers should assume that newref has been freed after calling. 22486d5f994SEdmund Nadolski */ 22500142756SJeff Mahoney static void prelim_ref_insert(const struct btrfs_fs_info *fs_info, 22600142756SJeff Mahoney struct preftree *preftree, 2273ec4d323SEdmund Nadolski struct prelim_ref *newref, 2283ec4d323SEdmund Nadolski struct share_check *sc) 22986d5f994SEdmund Nadolski { 230ecf160b4SLiu Bo struct rb_root_cached *root; 23186d5f994SEdmund Nadolski struct rb_node **p; 23286d5f994SEdmund Nadolski struct rb_node *parent = NULL; 23386d5f994SEdmund Nadolski struct prelim_ref *ref; 23486d5f994SEdmund Nadolski int result; 235ecf160b4SLiu Bo bool leftmost = true; 23686d5f994SEdmund Nadolski 23786d5f994SEdmund Nadolski root = &preftree->root; 238ecf160b4SLiu Bo p = &root->rb_root.rb_node; 23986d5f994SEdmund Nadolski 24086d5f994SEdmund Nadolski while (*p) { 24186d5f994SEdmund Nadolski parent = *p; 24286d5f994SEdmund Nadolski ref = rb_entry(parent, struct prelim_ref, rbnode); 24386d5f994SEdmund Nadolski result = prelim_ref_compare(ref, newref); 24486d5f994SEdmund Nadolski if (result < 0) { 24586d5f994SEdmund Nadolski p = &(*p)->rb_left; 24686d5f994SEdmund Nadolski } else if (result > 0) { 24786d5f994SEdmund Nadolski p = &(*p)->rb_right; 248ecf160b4SLiu Bo leftmost = false; 24986d5f994SEdmund Nadolski } else { 25086d5f994SEdmund Nadolski /* Identical refs, merge them and free @newref */ 25186d5f994SEdmund Nadolski struct extent_inode_elem *eie = ref->inode_list; 25286d5f994SEdmund Nadolski 25386d5f994SEdmund Nadolski while (eie && eie->next) 25486d5f994SEdmund Nadolski eie = eie->next; 25586d5f994SEdmund Nadolski 25686d5f994SEdmund Nadolski if (!eie) 25786d5f994SEdmund Nadolski ref->inode_list = newref->inode_list; 25886d5f994SEdmund Nadolski else 25986d5f994SEdmund Nadolski eie->next = newref->inode_list; 26000142756SJeff Mahoney trace_btrfs_prelim_ref_merge(fs_info, ref, newref, 26100142756SJeff Mahoney preftree->count); 2623ec4d323SEdmund Nadolski /* 2633ec4d323SEdmund Nadolski * A delayed ref can have newref->count < 0. 2643ec4d323SEdmund Nadolski * The ref->count is updated to follow any 2653ec4d323SEdmund Nadolski * BTRFS_[ADD|DROP]_DELAYED_REF actions. 2663ec4d323SEdmund Nadolski */ 2673ec4d323SEdmund Nadolski update_share_count(sc, ref->count, 2683ec4d323SEdmund Nadolski ref->count + newref->count); 26986d5f994SEdmund Nadolski ref->count += newref->count; 27086d5f994SEdmund Nadolski free_pref(newref); 27186d5f994SEdmund Nadolski return; 27286d5f994SEdmund Nadolski } 27386d5f994SEdmund Nadolski } 27486d5f994SEdmund Nadolski 2753ec4d323SEdmund Nadolski update_share_count(sc, 0, newref->count); 2766c336b21SJeff Mahoney preftree->count++; 27700142756SJeff Mahoney trace_btrfs_prelim_ref_insert(fs_info, newref, NULL, preftree->count); 27886d5f994SEdmund Nadolski rb_link_node(&newref->rbnode, parent, p); 279ecf160b4SLiu Bo rb_insert_color_cached(&newref->rbnode, root, leftmost); 28086d5f994SEdmund Nadolski } 28186d5f994SEdmund Nadolski 28286d5f994SEdmund Nadolski /* 28386d5f994SEdmund Nadolski * Release the entire tree. We don't care about internal consistency so 28486d5f994SEdmund Nadolski * just free everything and then reset the tree root. 28586d5f994SEdmund Nadolski */ 28686d5f994SEdmund Nadolski static void prelim_release(struct preftree *preftree) 28786d5f994SEdmund Nadolski { 28886d5f994SEdmund Nadolski struct prelim_ref *ref, *next_ref; 28986d5f994SEdmund Nadolski 290ecf160b4SLiu Bo rbtree_postorder_for_each_entry_safe(ref, next_ref, 291ecf160b4SLiu Bo &preftree->root.rb_root, rbnode) 29286d5f994SEdmund Nadolski free_pref(ref); 29386d5f994SEdmund Nadolski 294ecf160b4SLiu Bo preftree->root = RB_ROOT_CACHED; 2956c336b21SJeff Mahoney preftree->count = 0; 29686d5f994SEdmund Nadolski } 29786d5f994SEdmund Nadolski 298d5c88b73SJan Schmidt /* 299d5c88b73SJan Schmidt * the rules for all callers of this function are: 300d5c88b73SJan Schmidt * - obtaining the parent is the goal 301d5c88b73SJan Schmidt * - if you add a key, you must know that it is a correct key 302d5c88b73SJan Schmidt * - if you cannot add the parent or a correct key, then we will look into the 303d5c88b73SJan Schmidt * block later to set a correct key 304d5c88b73SJan Schmidt * 305d5c88b73SJan Schmidt * delayed refs 306d5c88b73SJan Schmidt * ============ 307d5c88b73SJan Schmidt * backref type | shared | indirect | shared | indirect 308d5c88b73SJan Schmidt * information | tree | tree | data | data 309d5c88b73SJan Schmidt * --------------------+--------+----------+--------+---------- 310d5c88b73SJan Schmidt * parent logical | y | - | - | - 311d5c88b73SJan Schmidt * key to resolve | - | y | y | y 312d5c88b73SJan Schmidt * tree block logical | - | - | - | - 313d5c88b73SJan Schmidt * root for resolving | y | y | y | y 314d5c88b73SJan Schmidt * 315d5c88b73SJan Schmidt * - column 1: we've the parent -> done 316d5c88b73SJan Schmidt * - column 2, 3, 4: we use the key to find the parent 317d5c88b73SJan Schmidt * 318d5c88b73SJan Schmidt * on disk refs (inline or keyed) 319d5c88b73SJan Schmidt * ============================== 320d5c88b73SJan Schmidt * backref type | shared | indirect | shared | indirect 321d5c88b73SJan Schmidt * information | tree | tree | data | data 322d5c88b73SJan Schmidt * --------------------+--------+----------+--------+---------- 323d5c88b73SJan Schmidt * parent logical | y | - | y | - 324d5c88b73SJan Schmidt * key to resolve | - | - | - | y 325d5c88b73SJan Schmidt * tree block logical | y | y | y | y 326d5c88b73SJan Schmidt * root for resolving | - | y | y | y 327d5c88b73SJan Schmidt * 328d5c88b73SJan Schmidt * - column 1, 3: we've the parent -> done 329d5c88b73SJan Schmidt * - column 2: we take the first key from the block to find the parent 330e0c476b1SJeff Mahoney * (see add_missing_keys) 331d5c88b73SJan Schmidt * - column 4: we use the key to find the parent 332d5c88b73SJan Schmidt * 333d5c88b73SJan Schmidt * additional information that's available but not required to find the parent 334d5c88b73SJan Schmidt * block might help in merging entries to gain some speed. 335d5c88b73SJan Schmidt */ 33600142756SJeff Mahoney static int add_prelim_ref(const struct btrfs_fs_info *fs_info, 33700142756SJeff Mahoney struct preftree *preftree, u64 root_id, 338e0c476b1SJeff Mahoney const struct btrfs_key *key, int level, u64 parent, 3393ec4d323SEdmund Nadolski u64 wanted_disk_byte, int count, 3403ec4d323SEdmund Nadolski struct share_check *sc, gfp_t gfp_mask) 3418da6d581SJan Schmidt { 342e0c476b1SJeff Mahoney struct prelim_ref *ref; 3438da6d581SJan Schmidt 34448ec4736SLiu Bo if (root_id == BTRFS_DATA_RELOC_TREE_OBJECTID) 34548ec4736SLiu Bo return 0; 34648ec4736SLiu Bo 347b9e9a6cbSWang Shilong ref = kmem_cache_alloc(btrfs_prelim_ref_cache, gfp_mask); 3488da6d581SJan Schmidt if (!ref) 3498da6d581SJan Schmidt return -ENOMEM; 3508da6d581SJan Schmidt 3518da6d581SJan Schmidt ref->root_id = root_id; 3527ac8b88eSethanwu if (key) 353d5c88b73SJan Schmidt ref->key_for_search = *key; 3547ac8b88eSethanwu else 355d5c88b73SJan Schmidt memset(&ref->key_for_search, 0, sizeof(ref->key_for_search)); 3568da6d581SJan Schmidt 3573301958bSJan Schmidt ref->inode_list = NULL; 3588da6d581SJan Schmidt ref->level = level; 3598da6d581SJan Schmidt ref->count = count; 3608da6d581SJan Schmidt ref->parent = parent; 3618da6d581SJan Schmidt ref->wanted_disk_byte = wanted_disk_byte; 3623ec4d323SEdmund Nadolski prelim_ref_insert(fs_info, preftree, ref, sc); 3633ec4d323SEdmund Nadolski return extent_is_shared(sc); 3648da6d581SJan Schmidt } 3658da6d581SJan Schmidt 36686d5f994SEdmund Nadolski /* direct refs use root == 0, key == NULL */ 36700142756SJeff Mahoney static int add_direct_ref(const struct btrfs_fs_info *fs_info, 36800142756SJeff Mahoney struct preftrees *preftrees, int level, u64 parent, 3693ec4d323SEdmund Nadolski u64 wanted_disk_byte, int count, 3703ec4d323SEdmund Nadolski struct share_check *sc, gfp_t gfp_mask) 37186d5f994SEdmund Nadolski { 37200142756SJeff Mahoney return add_prelim_ref(fs_info, &preftrees->direct, 0, NULL, level, 3733ec4d323SEdmund Nadolski parent, wanted_disk_byte, count, sc, gfp_mask); 37486d5f994SEdmund Nadolski } 37586d5f994SEdmund Nadolski 37686d5f994SEdmund Nadolski /* indirect refs use parent == 0 */ 37700142756SJeff Mahoney static int add_indirect_ref(const struct btrfs_fs_info *fs_info, 37800142756SJeff Mahoney struct preftrees *preftrees, u64 root_id, 37986d5f994SEdmund Nadolski const struct btrfs_key *key, int level, 3803ec4d323SEdmund Nadolski u64 wanted_disk_byte, int count, 3813ec4d323SEdmund Nadolski struct share_check *sc, gfp_t gfp_mask) 38286d5f994SEdmund Nadolski { 38386d5f994SEdmund Nadolski struct preftree *tree = &preftrees->indirect; 38486d5f994SEdmund Nadolski 38586d5f994SEdmund Nadolski if (!key) 38686d5f994SEdmund Nadolski tree = &preftrees->indirect_missing_keys; 38700142756SJeff Mahoney return add_prelim_ref(fs_info, tree, root_id, key, level, 0, 3883ec4d323SEdmund Nadolski wanted_disk_byte, count, sc, gfp_mask); 38986d5f994SEdmund Nadolski } 39086d5f994SEdmund Nadolski 391ed58f2e6Sethanwu static int is_shared_data_backref(struct preftrees *preftrees, u64 bytenr) 392ed58f2e6Sethanwu { 393ed58f2e6Sethanwu struct rb_node **p = &preftrees->direct.root.rb_root.rb_node; 394ed58f2e6Sethanwu struct rb_node *parent = NULL; 395ed58f2e6Sethanwu struct prelim_ref *ref = NULL; 3969c6c723fSArnd Bergmann struct prelim_ref target = {}; 397ed58f2e6Sethanwu int result; 398ed58f2e6Sethanwu 399ed58f2e6Sethanwu target.parent = bytenr; 400ed58f2e6Sethanwu 401ed58f2e6Sethanwu while (*p) { 402ed58f2e6Sethanwu parent = *p; 403ed58f2e6Sethanwu ref = rb_entry(parent, struct prelim_ref, rbnode); 404ed58f2e6Sethanwu result = prelim_ref_compare(ref, &target); 405ed58f2e6Sethanwu 406ed58f2e6Sethanwu if (result < 0) 407ed58f2e6Sethanwu p = &(*p)->rb_left; 408ed58f2e6Sethanwu else if (result > 0) 409ed58f2e6Sethanwu p = &(*p)->rb_right; 410ed58f2e6Sethanwu else 411ed58f2e6Sethanwu return 1; 412ed58f2e6Sethanwu } 413ed58f2e6Sethanwu return 0; 414ed58f2e6Sethanwu } 415ed58f2e6Sethanwu 4168da6d581SJan Schmidt static int add_all_parents(struct btrfs_root *root, struct btrfs_path *path, 417ed58f2e6Sethanwu struct ulist *parents, 418ed58f2e6Sethanwu struct preftrees *preftrees, struct prelim_ref *ref, 41944853868SJosef Bacik int level, u64 time_seq, const u64 *extent_item_pos, 420b25b0b87Sethanwu bool ignore_offset) 4218da6d581SJan Schmidt { 42269bca40dSAlexander Block int ret = 0; 42369bca40dSAlexander Block int slot; 42469bca40dSAlexander Block struct extent_buffer *eb; 42569bca40dSAlexander Block struct btrfs_key key; 4267ef81ac8SJosef Bacik struct btrfs_key *key_for_search = &ref->key_for_search; 4278da6d581SJan Schmidt struct btrfs_file_extent_item *fi; 428ed8c4913SJosef Bacik struct extent_inode_elem *eie = NULL, *old = NULL; 4298da6d581SJan Schmidt u64 disk_byte; 4307ef81ac8SJosef Bacik u64 wanted_disk_byte = ref->wanted_disk_byte; 4317ef81ac8SJosef Bacik u64 count = 0; 4327ac8b88eSethanwu u64 data_offset; 4338da6d581SJan Schmidt 43469bca40dSAlexander Block if (level != 0) { 43569bca40dSAlexander Block eb = path->nodes[level]; 43669bca40dSAlexander Block ret = ulist_add(parents, eb->start, 0, GFP_NOFS); 4373301958bSJan Schmidt if (ret < 0) 4383301958bSJan Schmidt return ret; 4398da6d581SJan Schmidt return 0; 44069bca40dSAlexander Block } 4418da6d581SJan Schmidt 4428da6d581SJan Schmidt /* 443ed58f2e6Sethanwu * 1. We normally enter this function with the path already pointing to 44469bca40dSAlexander Block * the first item to check. But sometimes, we may enter it with 445ed58f2e6Sethanwu * slot == nritems. 446ed58f2e6Sethanwu * 2. We are searching for normal backref but bytenr of this leaf 447ed58f2e6Sethanwu * matches shared data backref 448cfc0eed0Sethanwu * 3. The leaf owner is not equal to the root we are searching 449cfc0eed0Sethanwu * 450ed58f2e6Sethanwu * For these cases, go to the next leaf before we continue. 4518da6d581SJan Schmidt */ 452ed58f2e6Sethanwu eb = path->nodes[0]; 453ed58f2e6Sethanwu if (path->slots[0] >= btrfs_header_nritems(eb) || 454cfc0eed0Sethanwu is_shared_data_backref(preftrees, eb->start) || 455cfc0eed0Sethanwu ref->root_id != btrfs_header_owner(eb)) { 456f3a84ccdSFilipe Manana if (time_seq == BTRFS_SEQ_LAST) 45721633fc6SQu Wenruo ret = btrfs_next_leaf(root, path); 45821633fc6SQu Wenruo else 4593d7806ecSJan Schmidt ret = btrfs_next_old_leaf(root, path, time_seq); 46021633fc6SQu Wenruo } 4618da6d581SJan Schmidt 462b25b0b87Sethanwu while (!ret && count < ref->count) { 4638da6d581SJan Schmidt eb = path->nodes[0]; 46469bca40dSAlexander Block slot = path->slots[0]; 46569bca40dSAlexander Block 46669bca40dSAlexander Block btrfs_item_key_to_cpu(eb, &key, slot); 46769bca40dSAlexander Block 46869bca40dSAlexander Block if (key.objectid != key_for_search->objectid || 46969bca40dSAlexander Block key.type != BTRFS_EXTENT_DATA_KEY) 47069bca40dSAlexander Block break; 47169bca40dSAlexander Block 472ed58f2e6Sethanwu /* 473ed58f2e6Sethanwu * We are searching for normal backref but bytenr of this leaf 474cfc0eed0Sethanwu * matches shared data backref, OR 475cfc0eed0Sethanwu * the leaf owner is not equal to the root we are searching for 476ed58f2e6Sethanwu */ 477cfc0eed0Sethanwu if (slot == 0 && 478cfc0eed0Sethanwu (is_shared_data_backref(preftrees, eb->start) || 479cfc0eed0Sethanwu ref->root_id != btrfs_header_owner(eb))) { 480f3a84ccdSFilipe Manana if (time_seq == BTRFS_SEQ_LAST) 481ed58f2e6Sethanwu ret = btrfs_next_leaf(root, path); 482ed58f2e6Sethanwu else 483ed58f2e6Sethanwu ret = btrfs_next_old_leaf(root, path, time_seq); 484ed58f2e6Sethanwu continue; 485ed58f2e6Sethanwu } 48669bca40dSAlexander Block fi = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item); 4878da6d581SJan Schmidt disk_byte = btrfs_file_extent_disk_bytenr(eb, fi); 4887ac8b88eSethanwu data_offset = btrfs_file_extent_offset(eb, fi); 48969bca40dSAlexander Block 49069bca40dSAlexander Block if (disk_byte == wanted_disk_byte) { 49169bca40dSAlexander Block eie = NULL; 492ed8c4913SJosef Bacik old = NULL; 4937ac8b88eSethanwu if (ref->key_for_search.offset == key.offset - data_offset) 4947ef81ac8SJosef Bacik count++; 4957ac8b88eSethanwu else 4967ac8b88eSethanwu goto next; 49769bca40dSAlexander Block if (extent_item_pos) { 49869bca40dSAlexander Block ret = check_extent_in_eb(&key, eb, fi, 49969bca40dSAlexander Block *extent_item_pos, 500c995ab3cSZygo Blaxell &eie, ignore_offset); 50169bca40dSAlexander Block if (ret < 0) 50269bca40dSAlexander Block break; 5038da6d581SJan Schmidt } 504ed8c4913SJosef Bacik if (ret > 0) 505ed8c4913SJosef Bacik goto next; 5064eb1f66dSTakashi Iwai ret = ulist_add_merge_ptr(parents, eb->start, 5074eb1f66dSTakashi Iwai eie, (void **)&old, GFP_NOFS); 50869bca40dSAlexander Block if (ret < 0) 50969bca40dSAlexander Block break; 510ed8c4913SJosef Bacik if (!ret && extent_item_pos) { 511ed8c4913SJosef Bacik while (old->next) 512ed8c4913SJosef Bacik old = old->next; 513ed8c4913SJosef Bacik old->next = eie; 51469bca40dSAlexander Block } 515f05c4746SWang Shilong eie = NULL; 51669bca40dSAlexander Block } 517ed8c4913SJosef Bacik next: 518f3a84ccdSFilipe Manana if (time_seq == BTRFS_SEQ_LAST) 51921633fc6SQu Wenruo ret = btrfs_next_item(root, path); 52021633fc6SQu Wenruo else 52169bca40dSAlexander Block ret = btrfs_next_old_item(root, path, time_seq); 5228da6d581SJan Schmidt } 5238da6d581SJan Schmidt 52469bca40dSAlexander Block if (ret > 0) 52569bca40dSAlexander Block ret = 0; 526f05c4746SWang Shilong else if (ret < 0) 527f05c4746SWang Shilong free_inode_elem_list(eie); 52869bca40dSAlexander Block return ret; 5298da6d581SJan Schmidt } 5308da6d581SJan Schmidt 5318da6d581SJan Schmidt /* 5328da6d581SJan Schmidt * resolve an indirect backref in the form (root_id, key, level) 5338da6d581SJan Schmidt * to a logical address 5348da6d581SJan Schmidt */ 535e0c476b1SJeff Mahoney static int resolve_indirect_ref(struct btrfs_fs_info *fs_info, 536da61d31aSJosef Bacik struct btrfs_path *path, u64 time_seq, 537ed58f2e6Sethanwu struct preftrees *preftrees, 538e0c476b1SJeff Mahoney struct prelim_ref *ref, struct ulist *parents, 539b25b0b87Sethanwu const u64 *extent_item_pos, bool ignore_offset) 5408da6d581SJan Schmidt { 5418da6d581SJan Schmidt struct btrfs_root *root; 5428da6d581SJan Schmidt struct extent_buffer *eb; 5438da6d581SJan Schmidt int ret = 0; 5448da6d581SJan Schmidt int root_level; 5458da6d581SJan Schmidt int level = ref->level; 5467ac8b88eSethanwu struct btrfs_key search_key = ref->key_for_search; 5478da6d581SJan Schmidt 54849d11beaSJosef Bacik /* 54949d11beaSJosef Bacik * If we're search_commit_root we could possibly be holding locks on 55049d11beaSJosef Bacik * other tree nodes. This happens when qgroups does backref walks when 55149d11beaSJosef Bacik * adding new delayed refs. To deal with this we need to look in cache 55249d11beaSJosef Bacik * for the root, and if we don't find it then we need to search the 55349d11beaSJosef Bacik * tree_root's commit root, thus the btrfs_get_fs_root_commit_root usage 55449d11beaSJosef Bacik * here. 55549d11beaSJosef Bacik */ 55649d11beaSJosef Bacik if (path->search_commit_root) 55749d11beaSJosef Bacik root = btrfs_get_fs_root_commit_root(fs_info, path, ref->root_id); 55849d11beaSJosef Bacik else 55956e9357aSDavid Sterba root = btrfs_get_fs_root(fs_info, ref->root_id, false); 5608da6d581SJan Schmidt if (IS_ERR(root)) { 5618da6d581SJan Schmidt ret = PTR_ERR(root); 5629326f76fSJosef Bacik goto out_free; 5639326f76fSJosef Bacik } 5649326f76fSJosef Bacik 56539dba873SJosef Bacik if (!path->search_commit_root && 56639dba873SJosef Bacik test_bit(BTRFS_ROOT_DELETING, &root->state)) { 56739dba873SJosef Bacik ret = -ENOENT; 56839dba873SJosef Bacik goto out; 56939dba873SJosef Bacik } 57039dba873SJosef Bacik 571f5ee5c9aSJeff Mahoney if (btrfs_is_testing(fs_info)) { 572d9ee522bSJosef Bacik ret = -ENOENT; 573d9ee522bSJosef Bacik goto out; 574d9ee522bSJosef Bacik } 575d9ee522bSJosef Bacik 5769e351cc8SJosef Bacik if (path->search_commit_root) 5779e351cc8SJosef Bacik root_level = btrfs_header_level(root->commit_root); 578f3a84ccdSFilipe Manana else if (time_seq == BTRFS_SEQ_LAST) 57921633fc6SQu Wenruo root_level = btrfs_header_level(root->node); 5809e351cc8SJosef Bacik else 5815b6602e7SJan Schmidt root_level = btrfs_old_root_level(root, time_seq); 5828da6d581SJan Schmidt 583c75e8394SJosef Bacik if (root_level + 1 == level) 5848da6d581SJan Schmidt goto out; 5858da6d581SJan Schmidt 5867ac8b88eSethanwu /* 5877ac8b88eSethanwu * We can often find data backrefs with an offset that is too large 5887ac8b88eSethanwu * (>= LLONG_MAX, maximum allowed file offset) due to underflows when 5897ac8b88eSethanwu * subtracting a file's offset with the data offset of its 5907ac8b88eSethanwu * corresponding extent data item. This can happen for example in the 5917ac8b88eSethanwu * clone ioctl. 5927ac8b88eSethanwu * 5937ac8b88eSethanwu * So if we detect such case we set the search key's offset to zero to 5947ac8b88eSethanwu * make sure we will find the matching file extent item at 5957ac8b88eSethanwu * add_all_parents(), otherwise we will miss it because the offset 5967ac8b88eSethanwu * taken form the backref is much larger then the offset of the file 5977ac8b88eSethanwu * extent item. This can make us scan a very large number of file 5987ac8b88eSethanwu * extent items, but at least it will not make us miss any. 5997ac8b88eSethanwu * 6007ac8b88eSethanwu * This is an ugly workaround for a behaviour that should have never 6017ac8b88eSethanwu * existed, but it does and a fix for the clone ioctl would touch a lot 6027ac8b88eSethanwu * of places, cause backwards incompatibility and would not fix the 6037ac8b88eSethanwu * problem for extents cloned with older kernels. 6047ac8b88eSethanwu */ 6057ac8b88eSethanwu if (search_key.type == BTRFS_EXTENT_DATA_KEY && 6067ac8b88eSethanwu search_key.offset >= LLONG_MAX) 6077ac8b88eSethanwu search_key.offset = 0; 6088da6d581SJan Schmidt path->lowest_level = level; 609f3a84ccdSFilipe Manana if (time_seq == BTRFS_SEQ_LAST) 6107ac8b88eSethanwu ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0); 61121633fc6SQu Wenruo else 6127ac8b88eSethanwu ret = btrfs_search_old_slot(root, &search_key, path, time_seq); 613538f72cdSWang Shilong 614ab8d0fc4SJeff Mahoney btrfs_debug(fs_info, 615ab8d0fc4SJeff Mahoney "search slot in root %llu (level %d, ref count %d) returned %d for key (%llu %u %llu)", 616c1c9ff7cSGeert Uytterhoeven ref->root_id, level, ref->count, ret, 617c1c9ff7cSGeert Uytterhoeven ref->key_for_search.objectid, ref->key_for_search.type, 618c1c9ff7cSGeert Uytterhoeven ref->key_for_search.offset); 6198da6d581SJan Schmidt if (ret < 0) 6208da6d581SJan Schmidt goto out; 6218da6d581SJan Schmidt 6228da6d581SJan Schmidt eb = path->nodes[level]; 6239345457fSJan Schmidt while (!eb) { 624fae7f21cSDulshani Gunawardhana if (WARN_ON(!level)) { 6258da6d581SJan Schmidt ret = 1; 6268da6d581SJan Schmidt goto out; 6278da6d581SJan Schmidt } 6289345457fSJan Schmidt level--; 6299345457fSJan Schmidt eb = path->nodes[level]; 6309345457fSJan Schmidt } 6318da6d581SJan Schmidt 632ed58f2e6Sethanwu ret = add_all_parents(root, path, parents, preftrees, ref, level, 633b25b0b87Sethanwu time_seq, extent_item_pos, ignore_offset); 6348da6d581SJan Schmidt out: 63500246528SJosef Bacik btrfs_put_root(root); 6369326f76fSJosef Bacik out_free: 637da61d31aSJosef Bacik path->lowest_level = 0; 638da61d31aSJosef Bacik btrfs_release_path(path); 6398da6d581SJan Schmidt return ret; 6408da6d581SJan Schmidt } 6418da6d581SJan Schmidt 6424dae077aSJeff Mahoney static struct extent_inode_elem * 6434dae077aSJeff Mahoney unode_aux_to_inode_list(struct ulist_node *node) 6444dae077aSJeff Mahoney { 6454dae077aSJeff Mahoney if (!node) 6464dae077aSJeff Mahoney return NULL; 6474dae077aSJeff Mahoney return (struct extent_inode_elem *)(uintptr_t)node->aux; 6484dae077aSJeff Mahoney } 6494dae077aSJeff Mahoney 6508da6d581SJan Schmidt /* 65152042d8eSAndrea Gelmini * We maintain three separate rbtrees: one for direct refs, one for 65286d5f994SEdmund Nadolski * indirect refs which have a key, and one for indirect refs which do not 65386d5f994SEdmund Nadolski * have a key. Each tree does merge on insertion. 65486d5f994SEdmund Nadolski * 65586d5f994SEdmund Nadolski * Once all of the references are located, we iterate over the tree of 65686d5f994SEdmund Nadolski * indirect refs with missing keys. An appropriate key is located and 65786d5f994SEdmund Nadolski * the ref is moved onto the tree for indirect refs. After all missing 65886d5f994SEdmund Nadolski * keys are thus located, we iterate over the indirect ref tree, resolve 65986d5f994SEdmund Nadolski * each reference, and then insert the resolved reference onto the 66086d5f994SEdmund Nadolski * direct tree (merging there too). 66186d5f994SEdmund Nadolski * 66286d5f994SEdmund Nadolski * New backrefs (i.e., for parent nodes) are added to the appropriate 66386d5f994SEdmund Nadolski * rbtree as they are encountered. The new backrefs are subsequently 66486d5f994SEdmund Nadolski * resolved as above. 6658da6d581SJan Schmidt */ 666e0c476b1SJeff Mahoney static int resolve_indirect_refs(struct btrfs_fs_info *fs_info, 667da61d31aSJosef Bacik struct btrfs_path *path, u64 time_seq, 66886d5f994SEdmund Nadolski struct preftrees *preftrees, 669b25b0b87Sethanwu const u64 *extent_item_pos, 670c995ab3cSZygo Blaxell struct share_check *sc, bool ignore_offset) 6718da6d581SJan Schmidt { 6728da6d581SJan Schmidt int err; 6738da6d581SJan Schmidt int ret = 0; 6748da6d581SJan Schmidt struct ulist *parents; 6758da6d581SJan Schmidt struct ulist_node *node; 676cd1b413cSJan Schmidt struct ulist_iterator uiter; 67786d5f994SEdmund Nadolski struct rb_node *rnode; 6788da6d581SJan Schmidt 6798da6d581SJan Schmidt parents = ulist_alloc(GFP_NOFS); 6808da6d581SJan Schmidt if (!parents) 6818da6d581SJan Schmidt return -ENOMEM; 6828da6d581SJan Schmidt 6838da6d581SJan Schmidt /* 68486d5f994SEdmund Nadolski * We could trade memory usage for performance here by iterating 68586d5f994SEdmund Nadolski * the tree, allocating new refs for each insertion, and then 68686d5f994SEdmund Nadolski * freeing the entire indirect tree when we're done. In some test 68786d5f994SEdmund Nadolski * cases, the tree can grow quite large (~200k objects). 6888da6d581SJan Schmidt */ 689ecf160b4SLiu Bo while ((rnode = rb_first_cached(&preftrees->indirect.root))) { 69086d5f994SEdmund Nadolski struct prelim_ref *ref; 69186d5f994SEdmund Nadolski 69286d5f994SEdmund Nadolski ref = rb_entry(rnode, struct prelim_ref, rbnode); 69386d5f994SEdmund Nadolski if (WARN(ref->parent, 69486d5f994SEdmund Nadolski "BUG: direct ref found in indirect tree")) { 69586d5f994SEdmund Nadolski ret = -EINVAL; 69686d5f994SEdmund Nadolski goto out; 69786d5f994SEdmund Nadolski } 69886d5f994SEdmund Nadolski 699ecf160b4SLiu Bo rb_erase_cached(&ref->rbnode, &preftrees->indirect.root); 7006c336b21SJeff Mahoney preftrees->indirect.count--; 70186d5f994SEdmund Nadolski 70286d5f994SEdmund Nadolski if (ref->count == 0) { 70386d5f994SEdmund Nadolski free_pref(ref); 7048da6d581SJan Schmidt continue; 70586d5f994SEdmund Nadolski } 70686d5f994SEdmund Nadolski 7073ec4d323SEdmund Nadolski if (sc && sc->root_objectid && 7083ec4d323SEdmund Nadolski ref->root_id != sc->root_objectid) { 70986d5f994SEdmund Nadolski free_pref(ref); 710dc046b10SJosef Bacik ret = BACKREF_FOUND_SHARED; 711dc046b10SJosef Bacik goto out; 712dc046b10SJosef Bacik } 713ed58f2e6Sethanwu err = resolve_indirect_ref(fs_info, path, time_seq, preftrees, 714ed58f2e6Sethanwu ref, parents, extent_item_pos, 715b25b0b87Sethanwu ignore_offset); 71695def2edSWang Shilong /* 71795def2edSWang Shilong * we can only tolerate ENOENT,otherwise,we should catch error 71895def2edSWang Shilong * and return directly. 71995def2edSWang Shilong */ 72095def2edSWang Shilong if (err == -ENOENT) { 7213ec4d323SEdmund Nadolski prelim_ref_insert(fs_info, &preftrees->direct, ref, 7223ec4d323SEdmund Nadolski NULL); 7238da6d581SJan Schmidt continue; 72495def2edSWang Shilong } else if (err) { 72586d5f994SEdmund Nadolski free_pref(ref); 72695def2edSWang Shilong ret = err; 72795def2edSWang Shilong goto out; 72895def2edSWang Shilong } 7298da6d581SJan Schmidt 7308da6d581SJan Schmidt /* we put the first parent into the ref at hand */ 731cd1b413cSJan Schmidt ULIST_ITER_INIT(&uiter); 732cd1b413cSJan Schmidt node = ulist_next(parents, &uiter); 7338da6d581SJan Schmidt ref->parent = node ? node->val : 0; 7344dae077aSJeff Mahoney ref->inode_list = unode_aux_to_inode_list(node); 7358da6d581SJan Schmidt 73686d5f994SEdmund Nadolski /* Add a prelim_ref(s) for any other parent(s). */ 737cd1b413cSJan Schmidt while ((node = ulist_next(parents, &uiter))) { 73886d5f994SEdmund Nadolski struct prelim_ref *new_ref; 73986d5f994SEdmund Nadolski 740b9e9a6cbSWang Shilong new_ref = kmem_cache_alloc(btrfs_prelim_ref_cache, 741b9e9a6cbSWang Shilong GFP_NOFS); 7428da6d581SJan Schmidt if (!new_ref) { 74386d5f994SEdmund Nadolski free_pref(ref); 7448da6d581SJan Schmidt ret = -ENOMEM; 745e36902d4SWang Shilong goto out; 7468da6d581SJan Schmidt } 7478da6d581SJan Schmidt memcpy(new_ref, ref, sizeof(*ref)); 7488da6d581SJan Schmidt new_ref->parent = node->val; 7494dae077aSJeff Mahoney new_ref->inode_list = unode_aux_to_inode_list(node); 7503ec4d323SEdmund Nadolski prelim_ref_insert(fs_info, &preftrees->direct, 7513ec4d323SEdmund Nadolski new_ref, NULL); 7528da6d581SJan Schmidt } 75386d5f994SEdmund Nadolski 7543ec4d323SEdmund Nadolski /* 75552042d8eSAndrea Gelmini * Now it's a direct ref, put it in the direct tree. We must 7563ec4d323SEdmund Nadolski * do this last because the ref could be merged/freed here. 7573ec4d323SEdmund Nadolski */ 7583ec4d323SEdmund Nadolski prelim_ref_insert(fs_info, &preftrees->direct, ref, NULL); 75986d5f994SEdmund Nadolski 7608da6d581SJan Schmidt ulist_reinit(parents); 7619dd14fd6SEdmund Nadolski cond_resched(); 7628da6d581SJan Schmidt } 763e36902d4SWang Shilong out: 7648da6d581SJan Schmidt ulist_free(parents); 7658da6d581SJan Schmidt return ret; 7668da6d581SJan Schmidt } 7678da6d581SJan Schmidt 768d5c88b73SJan Schmidt /* 769d5c88b73SJan Schmidt * read tree blocks and add keys where required. 770d5c88b73SJan Schmidt */ 771e0c476b1SJeff Mahoney static int add_missing_keys(struct btrfs_fs_info *fs_info, 77238e3eebfSJosef Bacik struct preftrees *preftrees, bool lock) 773d5c88b73SJan Schmidt { 774e0c476b1SJeff Mahoney struct prelim_ref *ref; 775d5c88b73SJan Schmidt struct extent_buffer *eb; 77686d5f994SEdmund Nadolski struct preftree *tree = &preftrees->indirect_missing_keys; 77786d5f994SEdmund Nadolski struct rb_node *node; 778d5c88b73SJan Schmidt 779ecf160b4SLiu Bo while ((node = rb_first_cached(&tree->root))) { 78086d5f994SEdmund Nadolski ref = rb_entry(node, struct prelim_ref, rbnode); 781ecf160b4SLiu Bo rb_erase_cached(node, &tree->root); 78286d5f994SEdmund Nadolski 78386d5f994SEdmund Nadolski BUG_ON(ref->parent); /* should not be a direct ref */ 78486d5f994SEdmund Nadolski BUG_ON(ref->key_for_search.type); 785d5c88b73SJan Schmidt BUG_ON(!ref->wanted_disk_byte); 78686d5f994SEdmund Nadolski 7871b7ec85eSJosef Bacik eb = read_tree_block(fs_info, ref->wanted_disk_byte, 7881b7ec85eSJosef Bacik ref->root_id, 0, ref->level - 1, NULL); 78964c043deSLiu Bo if (IS_ERR(eb)) { 79086d5f994SEdmund Nadolski free_pref(ref); 79164c043deSLiu Bo return PTR_ERR(eb); 7924eb150d6SQu Wenruo } 7934eb150d6SQu Wenruo if (!extent_buffer_uptodate(eb)) { 79486d5f994SEdmund Nadolski free_pref(ref); 795416bc658SJosef Bacik free_extent_buffer(eb); 796416bc658SJosef Bacik return -EIO; 797416bc658SJosef Bacik } 7984eb150d6SQu Wenruo 79938e3eebfSJosef Bacik if (lock) 800d5c88b73SJan Schmidt btrfs_tree_read_lock(eb); 801d5c88b73SJan Schmidt if (btrfs_header_level(eb) == 0) 802d5c88b73SJan Schmidt btrfs_item_key_to_cpu(eb, &ref->key_for_search, 0); 803d5c88b73SJan Schmidt else 804d5c88b73SJan Schmidt btrfs_node_key_to_cpu(eb, &ref->key_for_search, 0); 80538e3eebfSJosef Bacik if (lock) 806d5c88b73SJan Schmidt btrfs_tree_read_unlock(eb); 807d5c88b73SJan Schmidt free_extent_buffer(eb); 8083ec4d323SEdmund Nadolski prelim_ref_insert(fs_info, &preftrees->indirect, ref, NULL); 8099dd14fd6SEdmund Nadolski cond_resched(); 810d5c88b73SJan Schmidt } 811d5c88b73SJan Schmidt return 0; 812d5c88b73SJan Schmidt } 813d5c88b73SJan Schmidt 8148da6d581SJan Schmidt /* 8158da6d581SJan Schmidt * add all currently queued delayed refs from this head whose seq nr is 8168da6d581SJan Schmidt * smaller or equal that seq to the list 8178da6d581SJan Schmidt */ 81800142756SJeff Mahoney static int add_delayed_refs(const struct btrfs_fs_info *fs_info, 81900142756SJeff Mahoney struct btrfs_delayed_ref_head *head, u64 seq, 820b25b0b87Sethanwu struct preftrees *preftrees, struct share_check *sc) 8218da6d581SJan Schmidt { 822c6fc2454SQu Wenruo struct btrfs_delayed_ref_node *node; 8238da6d581SJan Schmidt struct btrfs_delayed_extent_op *extent_op = head->extent_op; 824d5c88b73SJan Schmidt struct btrfs_key key; 82586d5f994SEdmund Nadolski struct btrfs_key tmp_op_key; 8260e0adbcfSJosef Bacik struct rb_node *n; 82701747e92SEdmund Nadolski int count; 828b1375d64SJan Schmidt int ret = 0; 8298da6d581SJan Schmidt 830a6dbceafSNikolay Borisov if (extent_op && extent_op->update_key) 83186d5f994SEdmund Nadolski btrfs_disk_key_to_cpu(&tmp_op_key, &extent_op->key); 8328da6d581SJan Schmidt 833d7df2c79SJosef Bacik spin_lock(&head->lock); 834e3d03965SLiu Bo for (n = rb_first_cached(&head->ref_tree); n; n = rb_next(n)) { 8350e0adbcfSJosef Bacik node = rb_entry(n, struct btrfs_delayed_ref_node, 8360e0adbcfSJosef Bacik ref_node); 8378da6d581SJan Schmidt if (node->seq > seq) 8388da6d581SJan Schmidt continue; 8398da6d581SJan Schmidt 8408da6d581SJan Schmidt switch (node->action) { 8418da6d581SJan Schmidt case BTRFS_ADD_DELAYED_EXTENT: 8428da6d581SJan Schmidt case BTRFS_UPDATE_DELAYED_HEAD: 8438da6d581SJan Schmidt WARN_ON(1); 8448da6d581SJan Schmidt continue; 8458da6d581SJan Schmidt case BTRFS_ADD_DELAYED_REF: 84601747e92SEdmund Nadolski count = node->ref_mod; 8478da6d581SJan Schmidt break; 8488da6d581SJan Schmidt case BTRFS_DROP_DELAYED_REF: 84901747e92SEdmund Nadolski count = node->ref_mod * -1; 8508da6d581SJan Schmidt break; 8518da6d581SJan Schmidt default: 852290342f6SArnd Bergmann BUG(); 8538da6d581SJan Schmidt } 8548da6d581SJan Schmidt switch (node->type) { 8558da6d581SJan Schmidt case BTRFS_TREE_BLOCK_REF_KEY: { 85686d5f994SEdmund Nadolski /* NORMAL INDIRECT METADATA backref */ 8578da6d581SJan Schmidt struct btrfs_delayed_tree_ref *ref; 8588da6d581SJan Schmidt 8598da6d581SJan Schmidt ref = btrfs_delayed_node_to_tree_ref(node); 86000142756SJeff Mahoney ret = add_indirect_ref(fs_info, preftrees, ref->root, 86100142756SJeff Mahoney &tmp_op_key, ref->level + 1, 86201747e92SEdmund Nadolski node->bytenr, count, sc, 86301747e92SEdmund Nadolski GFP_ATOMIC); 8648da6d581SJan Schmidt break; 8658da6d581SJan Schmidt } 8668da6d581SJan Schmidt case BTRFS_SHARED_BLOCK_REF_KEY: { 86786d5f994SEdmund Nadolski /* SHARED DIRECT METADATA backref */ 8688da6d581SJan Schmidt struct btrfs_delayed_tree_ref *ref; 8698da6d581SJan Schmidt 8708da6d581SJan Schmidt ref = btrfs_delayed_node_to_tree_ref(node); 87186d5f994SEdmund Nadolski 87201747e92SEdmund Nadolski ret = add_direct_ref(fs_info, preftrees, ref->level + 1, 87301747e92SEdmund Nadolski ref->parent, node->bytenr, count, 8743ec4d323SEdmund Nadolski sc, GFP_ATOMIC); 8758da6d581SJan Schmidt break; 8768da6d581SJan Schmidt } 8778da6d581SJan Schmidt case BTRFS_EXTENT_DATA_REF_KEY: { 87886d5f994SEdmund Nadolski /* NORMAL INDIRECT DATA backref */ 8798da6d581SJan Schmidt struct btrfs_delayed_data_ref *ref; 8808da6d581SJan Schmidt ref = btrfs_delayed_node_to_data_ref(node); 8818da6d581SJan Schmidt 8828da6d581SJan Schmidt key.objectid = ref->objectid; 8838da6d581SJan Schmidt key.type = BTRFS_EXTENT_DATA_KEY; 8848da6d581SJan Schmidt key.offset = ref->offset; 885dc046b10SJosef Bacik 886dc046b10SJosef Bacik /* 887dc046b10SJosef Bacik * Found a inum that doesn't match our known inum, we 888dc046b10SJosef Bacik * know it's shared. 889dc046b10SJosef Bacik */ 8903ec4d323SEdmund Nadolski if (sc && sc->inum && ref->objectid != sc->inum) { 891dc046b10SJosef Bacik ret = BACKREF_FOUND_SHARED; 8923ec4d323SEdmund Nadolski goto out; 893dc046b10SJosef Bacik } 894dc046b10SJosef Bacik 89500142756SJeff Mahoney ret = add_indirect_ref(fs_info, preftrees, ref->root, 89601747e92SEdmund Nadolski &key, 0, node->bytenr, count, sc, 89701747e92SEdmund Nadolski GFP_ATOMIC); 8988da6d581SJan Schmidt break; 8998da6d581SJan Schmidt } 9008da6d581SJan Schmidt case BTRFS_SHARED_DATA_REF_KEY: { 90186d5f994SEdmund Nadolski /* SHARED DIRECT FULL backref */ 9028da6d581SJan Schmidt struct btrfs_delayed_data_ref *ref; 9038da6d581SJan Schmidt 9048da6d581SJan Schmidt ref = btrfs_delayed_node_to_data_ref(node); 90586d5f994SEdmund Nadolski 90601747e92SEdmund Nadolski ret = add_direct_ref(fs_info, preftrees, 0, ref->parent, 90701747e92SEdmund Nadolski node->bytenr, count, sc, 90801747e92SEdmund Nadolski GFP_ATOMIC); 9098da6d581SJan Schmidt break; 9108da6d581SJan Schmidt } 9118da6d581SJan Schmidt default: 9128da6d581SJan Schmidt WARN_ON(1); 9138da6d581SJan Schmidt } 9143ec4d323SEdmund Nadolski /* 9153ec4d323SEdmund Nadolski * We must ignore BACKREF_FOUND_SHARED until all delayed 9163ec4d323SEdmund Nadolski * refs have been checked. 9173ec4d323SEdmund Nadolski */ 9183ec4d323SEdmund Nadolski if (ret && (ret != BACKREF_FOUND_SHARED)) 919d7df2c79SJosef Bacik break; 9208da6d581SJan Schmidt } 9213ec4d323SEdmund Nadolski if (!ret) 9223ec4d323SEdmund Nadolski ret = extent_is_shared(sc); 9233ec4d323SEdmund Nadolski out: 924d7df2c79SJosef Bacik spin_unlock(&head->lock); 925d7df2c79SJosef Bacik return ret; 9268da6d581SJan Schmidt } 9278da6d581SJan Schmidt 9288da6d581SJan Schmidt /* 9298da6d581SJan Schmidt * add all inline backrefs for bytenr to the list 9303ec4d323SEdmund Nadolski * 9313ec4d323SEdmund Nadolski * Returns 0 on success, <0 on error, or BACKREF_FOUND_SHARED. 9328da6d581SJan Schmidt */ 93300142756SJeff Mahoney static int add_inline_refs(const struct btrfs_fs_info *fs_info, 93400142756SJeff Mahoney struct btrfs_path *path, u64 bytenr, 93586d5f994SEdmund Nadolski int *info_level, struct preftrees *preftrees, 936b25b0b87Sethanwu struct share_check *sc) 9378da6d581SJan Schmidt { 938b1375d64SJan Schmidt int ret = 0; 9398da6d581SJan Schmidt int slot; 9408da6d581SJan Schmidt struct extent_buffer *leaf; 9418da6d581SJan Schmidt struct btrfs_key key; 942261c84b6SJosef Bacik struct btrfs_key found_key; 9438da6d581SJan Schmidt unsigned long ptr; 9448da6d581SJan Schmidt unsigned long end; 9458da6d581SJan Schmidt struct btrfs_extent_item *ei; 9468da6d581SJan Schmidt u64 flags; 9478da6d581SJan Schmidt u64 item_size; 9488da6d581SJan Schmidt 9498da6d581SJan Schmidt /* 9508da6d581SJan Schmidt * enumerate all inline refs 9518da6d581SJan Schmidt */ 9528da6d581SJan Schmidt leaf = path->nodes[0]; 953dadcaf78SJan Schmidt slot = path->slots[0]; 9548da6d581SJan Schmidt 9553212fa14SJosef Bacik item_size = btrfs_item_size(leaf, slot); 9568da6d581SJan Schmidt BUG_ON(item_size < sizeof(*ei)); 9578da6d581SJan Schmidt 9588da6d581SJan Schmidt ei = btrfs_item_ptr(leaf, slot, struct btrfs_extent_item); 9598da6d581SJan Schmidt flags = btrfs_extent_flags(leaf, ei); 960261c84b6SJosef Bacik btrfs_item_key_to_cpu(leaf, &found_key, slot); 9618da6d581SJan Schmidt 9628da6d581SJan Schmidt ptr = (unsigned long)(ei + 1); 9638da6d581SJan Schmidt end = (unsigned long)ei + item_size; 9648da6d581SJan Schmidt 965261c84b6SJosef Bacik if (found_key.type == BTRFS_EXTENT_ITEM_KEY && 966261c84b6SJosef Bacik flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) { 9678da6d581SJan Schmidt struct btrfs_tree_block_info *info; 9688da6d581SJan Schmidt 9698da6d581SJan Schmidt info = (struct btrfs_tree_block_info *)ptr; 9708da6d581SJan Schmidt *info_level = btrfs_tree_block_level(leaf, info); 9718da6d581SJan Schmidt ptr += sizeof(struct btrfs_tree_block_info); 9728da6d581SJan Schmidt BUG_ON(ptr > end); 973261c84b6SJosef Bacik } else if (found_key.type == BTRFS_METADATA_ITEM_KEY) { 974261c84b6SJosef Bacik *info_level = found_key.offset; 9758da6d581SJan Schmidt } else { 9768da6d581SJan Schmidt BUG_ON(!(flags & BTRFS_EXTENT_FLAG_DATA)); 9778da6d581SJan Schmidt } 9788da6d581SJan Schmidt 9798da6d581SJan Schmidt while (ptr < end) { 9808da6d581SJan Schmidt struct btrfs_extent_inline_ref *iref; 9818da6d581SJan Schmidt u64 offset; 9828da6d581SJan Schmidt int type; 9838da6d581SJan Schmidt 9848da6d581SJan Schmidt iref = (struct btrfs_extent_inline_ref *)ptr; 9853de28d57SLiu Bo type = btrfs_get_extent_inline_ref_type(leaf, iref, 9863de28d57SLiu Bo BTRFS_REF_TYPE_ANY); 9873de28d57SLiu Bo if (type == BTRFS_REF_TYPE_INVALID) 988af431dcbSSu Yue return -EUCLEAN; 9893de28d57SLiu Bo 9908da6d581SJan Schmidt offset = btrfs_extent_inline_ref_offset(leaf, iref); 9918da6d581SJan Schmidt 9928da6d581SJan Schmidt switch (type) { 9938da6d581SJan Schmidt case BTRFS_SHARED_BLOCK_REF_KEY: 99400142756SJeff Mahoney ret = add_direct_ref(fs_info, preftrees, 99500142756SJeff Mahoney *info_level + 1, offset, 9963ec4d323SEdmund Nadolski bytenr, 1, NULL, GFP_NOFS); 9978da6d581SJan Schmidt break; 9988da6d581SJan Schmidt case BTRFS_SHARED_DATA_REF_KEY: { 9998da6d581SJan Schmidt struct btrfs_shared_data_ref *sdref; 10008da6d581SJan Schmidt int count; 10018da6d581SJan Schmidt 10028da6d581SJan Schmidt sdref = (struct btrfs_shared_data_ref *)(iref + 1); 10038da6d581SJan Schmidt count = btrfs_shared_data_ref_count(leaf, sdref); 100486d5f994SEdmund Nadolski 100500142756SJeff Mahoney ret = add_direct_ref(fs_info, preftrees, 0, offset, 10063ec4d323SEdmund Nadolski bytenr, count, sc, GFP_NOFS); 10078da6d581SJan Schmidt break; 10088da6d581SJan Schmidt } 10098da6d581SJan Schmidt case BTRFS_TREE_BLOCK_REF_KEY: 101000142756SJeff Mahoney ret = add_indirect_ref(fs_info, preftrees, offset, 101100142756SJeff Mahoney NULL, *info_level + 1, 10123ec4d323SEdmund Nadolski bytenr, 1, NULL, GFP_NOFS); 10138da6d581SJan Schmidt break; 10148da6d581SJan Schmidt case BTRFS_EXTENT_DATA_REF_KEY: { 10158da6d581SJan Schmidt struct btrfs_extent_data_ref *dref; 10168da6d581SJan Schmidt int count; 10178da6d581SJan Schmidt u64 root; 10188da6d581SJan Schmidt 10198da6d581SJan Schmidt dref = (struct btrfs_extent_data_ref *)(&iref->offset); 10208da6d581SJan Schmidt count = btrfs_extent_data_ref_count(leaf, dref); 10218da6d581SJan Schmidt key.objectid = btrfs_extent_data_ref_objectid(leaf, 10228da6d581SJan Schmidt dref); 10238da6d581SJan Schmidt key.type = BTRFS_EXTENT_DATA_KEY; 10248da6d581SJan Schmidt key.offset = btrfs_extent_data_ref_offset(leaf, dref); 1025dc046b10SJosef Bacik 10263ec4d323SEdmund Nadolski if (sc && sc->inum && key.objectid != sc->inum) { 1027dc046b10SJosef Bacik ret = BACKREF_FOUND_SHARED; 1028dc046b10SJosef Bacik break; 1029dc046b10SJosef Bacik } 1030dc046b10SJosef Bacik 10318da6d581SJan Schmidt root = btrfs_extent_data_ref_root(leaf, dref); 103286d5f994SEdmund Nadolski 103300142756SJeff Mahoney ret = add_indirect_ref(fs_info, preftrees, root, 103400142756SJeff Mahoney &key, 0, bytenr, count, 10353ec4d323SEdmund Nadolski sc, GFP_NOFS); 10368da6d581SJan Schmidt break; 10378da6d581SJan Schmidt } 10388da6d581SJan Schmidt default: 10398da6d581SJan Schmidt WARN_ON(1); 10408da6d581SJan Schmidt } 10411149ab6bSWang Shilong if (ret) 10421149ab6bSWang Shilong return ret; 10438da6d581SJan Schmidt ptr += btrfs_extent_inline_ref_size(type); 10448da6d581SJan Schmidt } 10458da6d581SJan Schmidt 10468da6d581SJan Schmidt return 0; 10478da6d581SJan Schmidt } 10488da6d581SJan Schmidt 10498da6d581SJan Schmidt /* 10508da6d581SJan Schmidt * add all non-inline backrefs for bytenr to the list 10513ec4d323SEdmund Nadolski * 10523ec4d323SEdmund Nadolski * Returns 0 on success, <0 on error, or BACKREF_FOUND_SHARED. 10538da6d581SJan Schmidt */ 105498cc4222SJosef Bacik static int add_keyed_refs(struct btrfs_root *extent_root, 10558da6d581SJan Schmidt struct btrfs_path *path, u64 bytenr, 105686d5f994SEdmund Nadolski int info_level, struct preftrees *preftrees, 10573ec4d323SEdmund Nadolski struct share_check *sc) 10588da6d581SJan Schmidt { 105998cc4222SJosef Bacik struct btrfs_fs_info *fs_info = extent_root->fs_info; 10608da6d581SJan Schmidt int ret; 10618da6d581SJan Schmidt int slot; 10628da6d581SJan Schmidt struct extent_buffer *leaf; 10638da6d581SJan Schmidt struct btrfs_key key; 10648da6d581SJan Schmidt 10658da6d581SJan Schmidt while (1) { 10668da6d581SJan Schmidt ret = btrfs_next_item(extent_root, path); 10678da6d581SJan Schmidt if (ret < 0) 10688da6d581SJan Schmidt break; 10698da6d581SJan Schmidt if (ret) { 10708da6d581SJan Schmidt ret = 0; 10718da6d581SJan Schmidt break; 10728da6d581SJan Schmidt } 10738da6d581SJan Schmidt 10748da6d581SJan Schmidt slot = path->slots[0]; 10758da6d581SJan Schmidt leaf = path->nodes[0]; 10768da6d581SJan Schmidt btrfs_item_key_to_cpu(leaf, &key, slot); 10778da6d581SJan Schmidt 10788da6d581SJan Schmidt if (key.objectid != bytenr) 10798da6d581SJan Schmidt break; 10808da6d581SJan Schmidt if (key.type < BTRFS_TREE_BLOCK_REF_KEY) 10818da6d581SJan Schmidt continue; 10828da6d581SJan Schmidt if (key.type > BTRFS_SHARED_DATA_REF_KEY) 10838da6d581SJan Schmidt break; 10848da6d581SJan Schmidt 10858da6d581SJan Schmidt switch (key.type) { 10868da6d581SJan Schmidt case BTRFS_SHARED_BLOCK_REF_KEY: 108786d5f994SEdmund Nadolski /* SHARED DIRECT METADATA backref */ 108800142756SJeff Mahoney ret = add_direct_ref(fs_info, preftrees, 108900142756SJeff Mahoney info_level + 1, key.offset, 10903ec4d323SEdmund Nadolski bytenr, 1, NULL, GFP_NOFS); 10918da6d581SJan Schmidt break; 10928da6d581SJan Schmidt case BTRFS_SHARED_DATA_REF_KEY: { 109386d5f994SEdmund Nadolski /* SHARED DIRECT FULL backref */ 10948da6d581SJan Schmidt struct btrfs_shared_data_ref *sdref; 10958da6d581SJan Schmidt int count; 10968da6d581SJan Schmidt 10978da6d581SJan Schmidt sdref = btrfs_item_ptr(leaf, slot, 10988da6d581SJan Schmidt struct btrfs_shared_data_ref); 10998da6d581SJan Schmidt count = btrfs_shared_data_ref_count(leaf, sdref); 110000142756SJeff Mahoney ret = add_direct_ref(fs_info, preftrees, 0, 110100142756SJeff Mahoney key.offset, bytenr, count, 11023ec4d323SEdmund Nadolski sc, GFP_NOFS); 11038da6d581SJan Schmidt break; 11048da6d581SJan Schmidt } 11058da6d581SJan Schmidt case BTRFS_TREE_BLOCK_REF_KEY: 110686d5f994SEdmund Nadolski /* NORMAL INDIRECT METADATA backref */ 110700142756SJeff Mahoney ret = add_indirect_ref(fs_info, preftrees, key.offset, 110800142756SJeff Mahoney NULL, info_level + 1, bytenr, 11093ec4d323SEdmund Nadolski 1, NULL, GFP_NOFS); 11108da6d581SJan Schmidt break; 11118da6d581SJan Schmidt case BTRFS_EXTENT_DATA_REF_KEY: { 111286d5f994SEdmund Nadolski /* NORMAL INDIRECT DATA backref */ 11138da6d581SJan Schmidt struct btrfs_extent_data_ref *dref; 11148da6d581SJan Schmidt int count; 11158da6d581SJan Schmidt u64 root; 11168da6d581SJan Schmidt 11178da6d581SJan Schmidt dref = btrfs_item_ptr(leaf, slot, 11188da6d581SJan Schmidt struct btrfs_extent_data_ref); 11198da6d581SJan Schmidt count = btrfs_extent_data_ref_count(leaf, dref); 11208da6d581SJan Schmidt key.objectid = btrfs_extent_data_ref_objectid(leaf, 11218da6d581SJan Schmidt dref); 11228da6d581SJan Schmidt key.type = BTRFS_EXTENT_DATA_KEY; 11238da6d581SJan Schmidt key.offset = btrfs_extent_data_ref_offset(leaf, dref); 1124dc046b10SJosef Bacik 11253ec4d323SEdmund Nadolski if (sc && sc->inum && key.objectid != sc->inum) { 1126dc046b10SJosef Bacik ret = BACKREF_FOUND_SHARED; 1127dc046b10SJosef Bacik break; 1128dc046b10SJosef Bacik } 1129dc046b10SJosef Bacik 11308da6d581SJan Schmidt root = btrfs_extent_data_ref_root(leaf, dref); 113100142756SJeff Mahoney ret = add_indirect_ref(fs_info, preftrees, root, 113200142756SJeff Mahoney &key, 0, bytenr, count, 11333ec4d323SEdmund Nadolski sc, GFP_NOFS); 11348da6d581SJan Schmidt break; 11358da6d581SJan Schmidt } 11368da6d581SJan Schmidt default: 11378da6d581SJan Schmidt WARN_ON(1); 11388da6d581SJan Schmidt } 11391149ab6bSWang Shilong if (ret) 11401149ab6bSWang Shilong return ret; 11411149ab6bSWang Shilong 11428da6d581SJan Schmidt } 11438da6d581SJan Schmidt 11448da6d581SJan Schmidt return ret; 11458da6d581SJan Schmidt } 11468da6d581SJan Schmidt 11478da6d581SJan Schmidt /* 11488da6d581SJan Schmidt * this adds all existing backrefs (inline backrefs, backrefs and delayed 11498da6d581SJan Schmidt * refs) for the given bytenr to the refs list, merges duplicates and resolves 11508da6d581SJan Schmidt * indirect refs to their parent bytenr. 11518da6d581SJan Schmidt * When roots are found, they're added to the roots list 11528da6d581SJan Schmidt * 1153f3a84ccdSFilipe Manana * If time_seq is set to BTRFS_SEQ_LAST, it will not search delayed_refs, and 1154f3a84ccdSFilipe Manana * behave much like trans == NULL case, the difference only lies in it will not 115521633fc6SQu Wenruo * commit root. 115621633fc6SQu Wenruo * The special case is for qgroup to search roots in commit_transaction(). 115721633fc6SQu Wenruo * 11583ec4d323SEdmund Nadolski * @sc - if !NULL, then immediately return BACKREF_FOUND_SHARED when a 11593ec4d323SEdmund Nadolski * shared extent is detected. 11603ec4d323SEdmund Nadolski * 11613ec4d323SEdmund Nadolski * Otherwise this returns 0 for success and <0 for an error. 11623ec4d323SEdmund Nadolski * 1163c995ab3cSZygo Blaxell * If ignore_offset is set to false, only extent refs whose offsets match 1164c995ab3cSZygo Blaxell * extent_item_pos are returned. If true, every extent ref is returned 1165c995ab3cSZygo Blaxell * and extent_item_pos is ignored. 1166c995ab3cSZygo Blaxell * 11678da6d581SJan Schmidt * FIXME some caching might speed things up 11688da6d581SJan Schmidt */ 11698da6d581SJan Schmidt static int find_parent_nodes(struct btrfs_trans_handle *trans, 11708da6d581SJan Schmidt struct btrfs_fs_info *fs_info, u64 bytenr, 1171097b8a7cSJan Schmidt u64 time_seq, struct ulist *refs, 1172dc046b10SJosef Bacik struct ulist *roots, const u64 *extent_item_pos, 1173c995ab3cSZygo Blaxell struct share_check *sc, bool ignore_offset) 11748da6d581SJan Schmidt { 117529cbcf40SJosef Bacik struct btrfs_root *root = btrfs_extent_root(fs_info, bytenr); 11768da6d581SJan Schmidt struct btrfs_key key; 11778da6d581SJan Schmidt struct btrfs_path *path; 11788da6d581SJan Schmidt struct btrfs_delayed_ref_root *delayed_refs = NULL; 1179d3b01064SLi Zefan struct btrfs_delayed_ref_head *head; 11808da6d581SJan Schmidt int info_level = 0; 11818da6d581SJan Schmidt int ret; 1182e0c476b1SJeff Mahoney struct prelim_ref *ref; 118386d5f994SEdmund Nadolski struct rb_node *node; 1184f05c4746SWang Shilong struct extent_inode_elem *eie = NULL; 118586d5f994SEdmund Nadolski struct preftrees preftrees = { 118686d5f994SEdmund Nadolski .direct = PREFTREE_INIT, 118786d5f994SEdmund Nadolski .indirect = PREFTREE_INIT, 118886d5f994SEdmund Nadolski .indirect_missing_keys = PREFTREE_INIT 118986d5f994SEdmund Nadolski }; 11908da6d581SJan Schmidt 11918da6d581SJan Schmidt key.objectid = bytenr; 11928da6d581SJan Schmidt key.offset = (u64)-1; 1193261c84b6SJosef Bacik if (btrfs_fs_incompat(fs_info, SKINNY_METADATA)) 1194261c84b6SJosef Bacik key.type = BTRFS_METADATA_ITEM_KEY; 1195261c84b6SJosef Bacik else 1196261c84b6SJosef Bacik key.type = BTRFS_EXTENT_ITEM_KEY; 11978da6d581SJan Schmidt 11988da6d581SJan Schmidt path = btrfs_alloc_path(); 11998da6d581SJan Schmidt if (!path) 12008da6d581SJan Schmidt return -ENOMEM; 1201e84752d4SWang Shilong if (!trans) { 1202da61d31aSJosef Bacik path->search_commit_root = 1; 1203e84752d4SWang Shilong path->skip_locking = 1; 1204e84752d4SWang Shilong } 12058da6d581SJan Schmidt 1206f3a84ccdSFilipe Manana if (time_seq == BTRFS_SEQ_LAST) 120721633fc6SQu Wenruo path->skip_locking = 1; 120821633fc6SQu Wenruo 12098da6d581SJan Schmidt again: 1210d3b01064SLi Zefan head = NULL; 1211d3b01064SLi Zefan 121298cc4222SJosef Bacik ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); 12138da6d581SJan Schmidt if (ret < 0) 12148da6d581SJan Schmidt goto out; 1215fcba0120SJosef Bacik if (ret == 0) { 1216fcba0120SJosef Bacik /* This shouldn't happen, indicates a bug or fs corruption. */ 1217fcba0120SJosef Bacik ASSERT(ret != 0); 1218fcba0120SJosef Bacik ret = -EUCLEAN; 1219fcba0120SJosef Bacik goto out; 1220fcba0120SJosef Bacik } 12218da6d581SJan Schmidt 122221633fc6SQu Wenruo if (trans && likely(trans->type != __TRANS_DUMMY) && 1223f3a84ccdSFilipe Manana time_seq != BTRFS_SEQ_LAST) { 12248da6d581SJan Schmidt /* 12259665ebd5SJosef Bacik * We have a specific time_seq we care about and trans which 12269665ebd5SJosef Bacik * means we have the path lock, we need to grab the ref head and 12279665ebd5SJosef Bacik * lock it so we have a consistent view of the refs at the given 12289665ebd5SJosef Bacik * time. 12298da6d581SJan Schmidt */ 12308da6d581SJan Schmidt delayed_refs = &trans->transaction->delayed_refs; 12318da6d581SJan Schmidt spin_lock(&delayed_refs->lock); 1232f72ad18eSLiu Bo head = btrfs_find_delayed_ref_head(delayed_refs, bytenr); 12338da6d581SJan Schmidt if (head) { 12348da6d581SJan Schmidt if (!mutex_trylock(&head->mutex)) { 1235d278850eSJosef Bacik refcount_inc(&head->refs); 12368da6d581SJan Schmidt spin_unlock(&delayed_refs->lock); 12378da6d581SJan Schmidt 12388da6d581SJan Schmidt btrfs_release_path(path); 12398da6d581SJan Schmidt 12408da6d581SJan Schmidt /* 12418da6d581SJan Schmidt * Mutex was contended, block until it's 12428da6d581SJan Schmidt * released and try again 12438da6d581SJan Schmidt */ 12448da6d581SJan Schmidt mutex_lock(&head->mutex); 12458da6d581SJan Schmidt mutex_unlock(&head->mutex); 1246d278850eSJosef Bacik btrfs_put_delayed_ref_head(head); 12478da6d581SJan Schmidt goto again; 12488da6d581SJan Schmidt } 1249d7df2c79SJosef Bacik spin_unlock(&delayed_refs->lock); 125000142756SJeff Mahoney ret = add_delayed_refs(fs_info, head, time_seq, 1251b25b0b87Sethanwu &preftrees, sc); 1252155725c9SJan Schmidt mutex_unlock(&head->mutex); 1253d7df2c79SJosef Bacik if (ret) 12548da6d581SJan Schmidt goto out; 1255d7df2c79SJosef Bacik } else { 12568da6d581SJan Schmidt spin_unlock(&delayed_refs->lock); 12577a3ae2f8SJan Schmidt } 1258d7df2c79SJosef Bacik } 12598da6d581SJan Schmidt 12608da6d581SJan Schmidt if (path->slots[0]) { 12618da6d581SJan Schmidt struct extent_buffer *leaf; 12628da6d581SJan Schmidt int slot; 12638da6d581SJan Schmidt 1264dadcaf78SJan Schmidt path->slots[0]--; 12658da6d581SJan Schmidt leaf = path->nodes[0]; 1266dadcaf78SJan Schmidt slot = path->slots[0]; 12678da6d581SJan Schmidt btrfs_item_key_to_cpu(leaf, &key, slot); 12688da6d581SJan Schmidt if (key.objectid == bytenr && 1269261c84b6SJosef Bacik (key.type == BTRFS_EXTENT_ITEM_KEY || 1270261c84b6SJosef Bacik key.type == BTRFS_METADATA_ITEM_KEY)) { 127100142756SJeff Mahoney ret = add_inline_refs(fs_info, path, bytenr, 1272b25b0b87Sethanwu &info_level, &preftrees, sc); 12738da6d581SJan Schmidt if (ret) 12748da6d581SJan Schmidt goto out; 127598cc4222SJosef Bacik ret = add_keyed_refs(root, path, bytenr, info_level, 12763ec4d323SEdmund Nadolski &preftrees, sc); 12778da6d581SJan Schmidt if (ret) 12788da6d581SJan Schmidt goto out; 12798da6d581SJan Schmidt } 12808da6d581SJan Schmidt } 128186d5f994SEdmund Nadolski 12828da6d581SJan Schmidt btrfs_release_path(path); 12838da6d581SJan Schmidt 128438e3eebfSJosef Bacik ret = add_missing_keys(fs_info, &preftrees, path->skip_locking == 0); 1285d5c88b73SJan Schmidt if (ret) 1286d5c88b73SJan Schmidt goto out; 1287d5c88b73SJan Schmidt 1288ecf160b4SLiu Bo WARN_ON(!RB_EMPTY_ROOT(&preftrees.indirect_missing_keys.root.rb_root)); 12898da6d581SJan Schmidt 129086d5f994SEdmund Nadolski ret = resolve_indirect_refs(fs_info, path, time_seq, &preftrees, 1291b25b0b87Sethanwu extent_item_pos, sc, ignore_offset); 12928da6d581SJan Schmidt if (ret) 12938da6d581SJan Schmidt goto out; 12948da6d581SJan Schmidt 1295ecf160b4SLiu Bo WARN_ON(!RB_EMPTY_ROOT(&preftrees.indirect.root.rb_root)); 12968da6d581SJan Schmidt 129786d5f994SEdmund Nadolski /* 129886d5f994SEdmund Nadolski * This walks the tree of merged and resolved refs. Tree blocks are 129986d5f994SEdmund Nadolski * read in as needed. Unique entries are added to the ulist, and 130086d5f994SEdmund Nadolski * the list of found roots is updated. 130186d5f994SEdmund Nadolski * 130286d5f994SEdmund Nadolski * We release the entire tree in one go before returning. 130386d5f994SEdmund Nadolski */ 1304ecf160b4SLiu Bo node = rb_first_cached(&preftrees.direct.root); 130586d5f994SEdmund Nadolski while (node) { 130686d5f994SEdmund Nadolski ref = rb_entry(node, struct prelim_ref, rbnode); 130786d5f994SEdmund Nadolski node = rb_next(&ref->rbnode); 1308c8195a7bSZygo Blaxell /* 1309c8195a7bSZygo Blaxell * ref->count < 0 can happen here if there are delayed 1310c8195a7bSZygo Blaxell * refs with a node->action of BTRFS_DROP_DELAYED_REF. 1311c8195a7bSZygo Blaxell * prelim_ref_insert() relies on this when merging 1312c8195a7bSZygo Blaxell * identical refs to keep the overall count correct. 1313c8195a7bSZygo Blaxell * prelim_ref_insert() will merge only those refs 1314c8195a7bSZygo Blaxell * which compare identically. Any refs having 1315c8195a7bSZygo Blaxell * e.g. different offsets would not be merged, 1316c8195a7bSZygo Blaxell * and would retain their original ref->count < 0. 1317c8195a7bSZygo Blaxell */ 131898cfee21SWang Shilong if (roots && ref->count && ref->root_id && ref->parent == 0) { 13193ec4d323SEdmund Nadolski if (sc && sc->root_objectid && 13203ec4d323SEdmund Nadolski ref->root_id != sc->root_objectid) { 1321dc046b10SJosef Bacik ret = BACKREF_FOUND_SHARED; 1322dc046b10SJosef Bacik goto out; 1323dc046b10SJosef Bacik } 1324dc046b10SJosef Bacik 13258da6d581SJan Schmidt /* no parent == root of tree */ 13268da6d581SJan Schmidt ret = ulist_add(roots, ref->root_id, 0, GFP_NOFS); 1327f1723939SWang Shilong if (ret < 0) 1328f1723939SWang Shilong goto out; 13298da6d581SJan Schmidt } 13308da6d581SJan Schmidt if (ref->count && ref->parent) { 13318a56457fSJosef Bacik if (extent_item_pos && !ref->inode_list && 13328a56457fSJosef Bacik ref->level == 0) { 1333976b1908SJan Schmidt struct extent_buffer *eb; 1334707e8a07SDavid Sterba 1335581c1760SQu Wenruo eb = read_tree_block(fs_info, ref->parent, 0, 13361b7ec85eSJosef Bacik 0, ref->level, NULL); 133764c043deSLiu Bo if (IS_ERR(eb)) { 133864c043deSLiu Bo ret = PTR_ERR(eb); 133964c043deSLiu Bo goto out; 13404eb150d6SQu Wenruo } 13414eb150d6SQu Wenruo if (!extent_buffer_uptodate(eb)) { 1342416bc658SJosef Bacik free_extent_buffer(eb); 1343c16c2e2eSWang Shilong ret = -EIO; 1344c16c2e2eSWang Shilong goto out; 1345416bc658SJosef Bacik } 134638e3eebfSJosef Bacik 1347ac5887c8SJosef Bacik if (!path->skip_locking) 13486f7ff6d7SFilipe Manana btrfs_tree_read_lock(eb); 1349976b1908SJan Schmidt ret = find_extent_in_eb(eb, bytenr, 1350c995ab3cSZygo Blaxell *extent_item_pos, &eie, ignore_offset); 135138e3eebfSJosef Bacik if (!path->skip_locking) 1352ac5887c8SJosef Bacik btrfs_tree_read_unlock(eb); 1353976b1908SJan Schmidt free_extent_buffer(eb); 1354f5929cd8SFilipe David Borba Manana if (ret < 0) 1355f5929cd8SFilipe David Borba Manana goto out; 1356f5929cd8SFilipe David Borba Manana ref->inode_list = eie; 1357976b1908SJan Schmidt } 13584eb1f66dSTakashi Iwai ret = ulist_add_merge_ptr(refs, ref->parent, 13594eb1f66dSTakashi Iwai ref->inode_list, 13604eb1f66dSTakashi Iwai (void **)&eie, GFP_NOFS); 1361f1723939SWang Shilong if (ret < 0) 1362f1723939SWang Shilong goto out; 13633301958bSJan Schmidt if (!ret && extent_item_pos) { 13643301958bSJan Schmidt /* 13659f05c09dSJosef Bacik * We've recorded that parent, so we must extend 13669f05c09dSJosef Bacik * its inode list here. 13679f05c09dSJosef Bacik * 13689f05c09dSJosef Bacik * However if there was corruption we may not 13699f05c09dSJosef Bacik * have found an eie, return an error in this 13709f05c09dSJosef Bacik * case. 13713301958bSJan Schmidt */ 13729f05c09dSJosef Bacik ASSERT(eie); 13739f05c09dSJosef Bacik if (!eie) { 13749f05c09dSJosef Bacik ret = -EUCLEAN; 13759f05c09dSJosef Bacik goto out; 13769f05c09dSJosef Bacik } 13773301958bSJan Schmidt while (eie->next) 13783301958bSJan Schmidt eie = eie->next; 13793301958bSJan Schmidt eie->next = ref->inode_list; 13803301958bSJan Schmidt } 1381f05c4746SWang Shilong eie = NULL; 13828da6d581SJan Schmidt } 13839dd14fd6SEdmund Nadolski cond_resched(); 13848da6d581SJan Schmidt } 13858da6d581SJan Schmidt 13868da6d581SJan Schmidt out: 13878da6d581SJan Schmidt btrfs_free_path(path); 138886d5f994SEdmund Nadolski 138986d5f994SEdmund Nadolski prelim_release(&preftrees.direct); 139086d5f994SEdmund Nadolski prelim_release(&preftrees.indirect); 139186d5f994SEdmund Nadolski prelim_release(&preftrees.indirect_missing_keys); 139286d5f994SEdmund Nadolski 1393f05c4746SWang Shilong if (ret < 0) 1394f05c4746SWang Shilong free_inode_elem_list(eie); 13958da6d581SJan Schmidt return ret; 13968da6d581SJan Schmidt } 13978da6d581SJan Schmidt 1398976b1908SJan Schmidt static void free_leaf_list(struct ulist *blocks) 1399976b1908SJan Schmidt { 1400976b1908SJan Schmidt struct ulist_node *node = NULL; 1401976b1908SJan Schmidt struct extent_inode_elem *eie; 1402976b1908SJan Schmidt struct ulist_iterator uiter; 1403976b1908SJan Schmidt 1404976b1908SJan Schmidt ULIST_ITER_INIT(&uiter); 1405976b1908SJan Schmidt while ((node = ulist_next(blocks, &uiter))) { 1406976b1908SJan Schmidt if (!node->aux) 1407976b1908SJan Schmidt continue; 14084dae077aSJeff Mahoney eie = unode_aux_to_inode_list(node); 1409f05c4746SWang Shilong free_inode_elem_list(eie); 1410976b1908SJan Schmidt node->aux = 0; 1411976b1908SJan Schmidt } 1412976b1908SJan Schmidt 1413976b1908SJan Schmidt ulist_free(blocks); 1414976b1908SJan Schmidt } 1415976b1908SJan Schmidt 14168da6d581SJan Schmidt /* 14178da6d581SJan Schmidt * Finds all leafs with a reference to the specified combination of bytenr and 14188da6d581SJan Schmidt * offset. key_list_head will point to a list of corresponding keys (caller must 14198da6d581SJan Schmidt * free each list element). The leafs will be stored in the leafs ulist, which 14208da6d581SJan Schmidt * must be freed with ulist_free. 14218da6d581SJan Schmidt * 14228da6d581SJan Schmidt * returns 0 on success, <0 on error 14238da6d581SJan Schmidt */ 142419b546d7SQu Wenruo int btrfs_find_all_leafs(struct btrfs_trans_handle *trans, 14258da6d581SJan Schmidt struct btrfs_fs_info *fs_info, u64 bytenr, 1426097b8a7cSJan Schmidt u64 time_seq, struct ulist **leafs, 1427c995ab3cSZygo Blaxell const u64 *extent_item_pos, bool ignore_offset) 14288da6d581SJan Schmidt { 14298da6d581SJan Schmidt int ret; 14308da6d581SJan Schmidt 14318da6d581SJan Schmidt *leafs = ulist_alloc(GFP_NOFS); 143298cfee21SWang Shilong if (!*leafs) 14338da6d581SJan Schmidt return -ENOMEM; 14348da6d581SJan Schmidt 1435afce772eSLu Fengqi ret = find_parent_nodes(trans, fs_info, bytenr, time_seq, 1436c995ab3cSZygo Blaxell *leafs, NULL, extent_item_pos, NULL, ignore_offset); 14378da6d581SJan Schmidt if (ret < 0 && ret != -ENOENT) { 1438976b1908SJan Schmidt free_leaf_list(*leafs); 14398da6d581SJan Schmidt return ret; 14408da6d581SJan Schmidt } 14418da6d581SJan Schmidt 14428da6d581SJan Schmidt return 0; 14438da6d581SJan Schmidt } 14448da6d581SJan Schmidt 14458da6d581SJan Schmidt /* 14468da6d581SJan Schmidt * walk all backrefs for a given extent to find all roots that reference this 14478da6d581SJan Schmidt * extent. Walking a backref means finding all extents that reference this 14488da6d581SJan Schmidt * extent and in turn walk the backrefs of those, too. Naturally this is a 14498da6d581SJan Schmidt * recursive process, but here it is implemented in an iterative fashion: We 14508da6d581SJan Schmidt * find all referencing extents for the extent in question and put them on a 14518da6d581SJan Schmidt * list. In turn, we find all referencing extents for those, further appending 14528da6d581SJan Schmidt * to the list. The way we iterate the list allows adding more elements after 14538da6d581SJan Schmidt * the current while iterating. The process stops when we reach the end of the 14548da6d581SJan Schmidt * list. Found roots are added to the roots list. 14558da6d581SJan Schmidt * 14568da6d581SJan Schmidt * returns 0 on success, < 0 on error. 14578da6d581SJan Schmidt */ 1458e0c476b1SJeff Mahoney static int btrfs_find_all_roots_safe(struct btrfs_trans_handle *trans, 14598da6d581SJan Schmidt struct btrfs_fs_info *fs_info, u64 bytenr, 1460c995ab3cSZygo Blaxell u64 time_seq, struct ulist **roots, 1461c995ab3cSZygo Blaxell bool ignore_offset) 14628da6d581SJan Schmidt { 14638da6d581SJan Schmidt struct ulist *tmp; 14648da6d581SJan Schmidt struct ulist_node *node = NULL; 1465cd1b413cSJan Schmidt struct ulist_iterator uiter; 14668da6d581SJan Schmidt int ret; 14678da6d581SJan Schmidt 14688da6d581SJan Schmidt tmp = ulist_alloc(GFP_NOFS); 14698da6d581SJan Schmidt if (!tmp) 14708da6d581SJan Schmidt return -ENOMEM; 14718da6d581SJan Schmidt *roots = ulist_alloc(GFP_NOFS); 14728da6d581SJan Schmidt if (!*roots) { 14738da6d581SJan Schmidt ulist_free(tmp); 14748da6d581SJan Schmidt return -ENOMEM; 14758da6d581SJan Schmidt } 14768da6d581SJan Schmidt 1477cd1b413cSJan Schmidt ULIST_ITER_INIT(&uiter); 14788da6d581SJan Schmidt while (1) { 1479afce772eSLu Fengqi ret = find_parent_nodes(trans, fs_info, bytenr, time_seq, 1480c995ab3cSZygo Blaxell tmp, *roots, NULL, NULL, ignore_offset); 14818da6d581SJan Schmidt if (ret < 0 && ret != -ENOENT) { 14828da6d581SJan Schmidt ulist_free(tmp); 14838da6d581SJan Schmidt ulist_free(*roots); 1484580c079bSFilipe Manana *roots = NULL; 14858da6d581SJan Schmidt return ret; 14868da6d581SJan Schmidt } 1487cd1b413cSJan Schmidt node = ulist_next(tmp, &uiter); 14888da6d581SJan Schmidt if (!node) 14898da6d581SJan Schmidt break; 14908da6d581SJan Schmidt bytenr = node->val; 1491bca1a290SWang Shilong cond_resched(); 14928da6d581SJan Schmidt } 14938da6d581SJan Schmidt 14948da6d581SJan Schmidt ulist_free(tmp); 14958da6d581SJan Schmidt return 0; 14968da6d581SJan Schmidt } 14978da6d581SJan Schmidt 14989e351cc8SJosef Bacik int btrfs_find_all_roots(struct btrfs_trans_handle *trans, 14999e351cc8SJosef Bacik struct btrfs_fs_info *fs_info, u64 bytenr, 1500c995ab3cSZygo Blaxell u64 time_seq, struct ulist **roots, 1501c7bcbb21SFilipe Manana bool skip_commit_root_sem) 15029e351cc8SJosef Bacik { 15039e351cc8SJosef Bacik int ret; 15049e351cc8SJosef Bacik 15058949b9a1SFilipe Manana if (!trans && !skip_commit_root_sem) 15069e351cc8SJosef Bacik down_read(&fs_info->commit_root_sem); 1507e0c476b1SJeff Mahoney ret = btrfs_find_all_roots_safe(trans, fs_info, bytenr, 1508c7bcbb21SFilipe Manana time_seq, roots, false); 15098949b9a1SFilipe Manana if (!trans && !skip_commit_root_sem) 15109e351cc8SJosef Bacik up_read(&fs_info->commit_root_sem); 15119e351cc8SJosef Bacik return ret; 15129e351cc8SJosef Bacik } 15139e351cc8SJosef Bacik 15148eedaddaSFilipe Manana /* 1515*12a824dcSFilipe Manana * The caller has joined a transaction or is holding a read lock on the 1516*12a824dcSFilipe Manana * fs_info->commit_root_sem semaphore, so no need to worry about the root's last 1517*12a824dcSFilipe Manana * snapshot field changing while updating or checking the cache. 1518*12a824dcSFilipe Manana */ 1519*12a824dcSFilipe Manana static bool lookup_backref_shared_cache(struct btrfs_backref_shared_cache *cache, 1520*12a824dcSFilipe Manana struct btrfs_root *root, 1521*12a824dcSFilipe Manana u64 bytenr, int level, bool *is_shared) 1522*12a824dcSFilipe Manana { 1523*12a824dcSFilipe Manana struct btrfs_backref_shared_cache_entry *entry; 1524*12a824dcSFilipe Manana 1525*12a824dcSFilipe Manana if (WARN_ON_ONCE(level >= BTRFS_MAX_LEVEL)) 1526*12a824dcSFilipe Manana return false; 1527*12a824dcSFilipe Manana 1528*12a824dcSFilipe Manana /* 1529*12a824dcSFilipe Manana * Level -1 is used for the data extent, which is not reliable to cache 1530*12a824dcSFilipe Manana * because its reference count can increase or decrease without us 1531*12a824dcSFilipe Manana * realizing. We cache results only for extent buffers that lead from 1532*12a824dcSFilipe Manana * the root node down to the leaf with the file extent item. 1533*12a824dcSFilipe Manana */ 1534*12a824dcSFilipe Manana ASSERT(level >= 0); 1535*12a824dcSFilipe Manana 1536*12a824dcSFilipe Manana entry = &cache->entries[level]; 1537*12a824dcSFilipe Manana 1538*12a824dcSFilipe Manana /* Unused cache entry or being used for some other extent buffer. */ 1539*12a824dcSFilipe Manana if (entry->bytenr != bytenr) 1540*12a824dcSFilipe Manana return false; 1541*12a824dcSFilipe Manana 1542*12a824dcSFilipe Manana /* 1543*12a824dcSFilipe Manana * We cached a false result, but the last snapshot generation of the 1544*12a824dcSFilipe Manana * root changed, so we now have a snapshot. Don't trust the result. 1545*12a824dcSFilipe Manana */ 1546*12a824dcSFilipe Manana if (!entry->is_shared && 1547*12a824dcSFilipe Manana entry->gen != btrfs_root_last_snapshot(&root->root_item)) 1548*12a824dcSFilipe Manana return false; 1549*12a824dcSFilipe Manana 1550*12a824dcSFilipe Manana /* 1551*12a824dcSFilipe Manana * If we cached a true result and the last generation used for dropping 1552*12a824dcSFilipe Manana * a root changed, we can not trust the result, because the dropped root 1553*12a824dcSFilipe Manana * could be a snapshot sharing this extent buffer. 1554*12a824dcSFilipe Manana */ 1555*12a824dcSFilipe Manana if (entry->is_shared && 1556*12a824dcSFilipe Manana entry->gen != btrfs_get_last_root_drop_gen(root->fs_info)) 1557*12a824dcSFilipe Manana return false; 1558*12a824dcSFilipe Manana 1559*12a824dcSFilipe Manana *is_shared = entry->is_shared; 1560*12a824dcSFilipe Manana 1561*12a824dcSFilipe Manana return true; 1562*12a824dcSFilipe Manana } 1563*12a824dcSFilipe Manana 1564*12a824dcSFilipe Manana /* 1565*12a824dcSFilipe Manana * The caller has joined a transaction or is holding a read lock on the 1566*12a824dcSFilipe Manana * fs_info->commit_root_sem semaphore, so no need to worry about the root's last 1567*12a824dcSFilipe Manana * snapshot field changing while updating or checking the cache. 1568*12a824dcSFilipe Manana */ 1569*12a824dcSFilipe Manana static void store_backref_shared_cache(struct btrfs_backref_shared_cache *cache, 1570*12a824dcSFilipe Manana struct btrfs_root *root, 1571*12a824dcSFilipe Manana u64 bytenr, int level, bool is_shared) 1572*12a824dcSFilipe Manana { 1573*12a824dcSFilipe Manana struct btrfs_backref_shared_cache_entry *entry; 1574*12a824dcSFilipe Manana u64 gen; 1575*12a824dcSFilipe Manana 1576*12a824dcSFilipe Manana if (WARN_ON_ONCE(level >= BTRFS_MAX_LEVEL)) 1577*12a824dcSFilipe Manana return; 1578*12a824dcSFilipe Manana 1579*12a824dcSFilipe Manana /* 1580*12a824dcSFilipe Manana * Level -1 is used for the data extent, which is not reliable to cache 1581*12a824dcSFilipe Manana * because its reference count can increase or decrease without us 1582*12a824dcSFilipe Manana * realizing. We cache results only for extent buffers that lead from 1583*12a824dcSFilipe Manana * the root node down to the leaf with the file extent item. 1584*12a824dcSFilipe Manana */ 1585*12a824dcSFilipe Manana ASSERT(level >= 0); 1586*12a824dcSFilipe Manana 1587*12a824dcSFilipe Manana if (is_shared) 1588*12a824dcSFilipe Manana gen = btrfs_get_last_root_drop_gen(root->fs_info); 1589*12a824dcSFilipe Manana else 1590*12a824dcSFilipe Manana gen = btrfs_root_last_snapshot(&root->root_item); 1591*12a824dcSFilipe Manana 1592*12a824dcSFilipe Manana entry = &cache->entries[level]; 1593*12a824dcSFilipe Manana entry->bytenr = bytenr; 1594*12a824dcSFilipe Manana entry->is_shared = is_shared; 1595*12a824dcSFilipe Manana entry->gen = gen; 1596*12a824dcSFilipe Manana 1597*12a824dcSFilipe Manana /* 1598*12a824dcSFilipe Manana * If we found an extent buffer is shared, set the cache result for all 1599*12a824dcSFilipe Manana * extent buffers below it to true. As nodes in the path are COWed, 1600*12a824dcSFilipe Manana * their sharedness is moved to their children, and if a leaf is COWed, 1601*12a824dcSFilipe Manana * then the sharedness of a data extent becomes direct, the refcount of 1602*12a824dcSFilipe Manana * data extent is increased in the extent item at the extent tree. 1603*12a824dcSFilipe Manana */ 1604*12a824dcSFilipe Manana if (is_shared) { 1605*12a824dcSFilipe Manana for (int i = 0; i < level; i++) { 1606*12a824dcSFilipe Manana entry = &cache->entries[i]; 1607*12a824dcSFilipe Manana entry->is_shared = is_shared; 1608*12a824dcSFilipe Manana entry->gen = gen; 1609*12a824dcSFilipe Manana } 1610*12a824dcSFilipe Manana } 1611*12a824dcSFilipe Manana } 1612*12a824dcSFilipe Manana 1613*12a824dcSFilipe Manana /* 16148eedaddaSFilipe Manana * Check if a data extent is shared or not. 16156e353e3bSNikolay Borisov * 16166e353e3bSNikolay Borisov * @root: root inode belongs to 16176e353e3bSNikolay Borisov * @inum: inode number of the inode whose extent we are checking 16186e353e3bSNikolay Borisov * @bytenr: logical bytenr of the extent we are checking 16196e353e3bSNikolay Borisov * @roots: list of roots this extent is shared among 16206e353e3bSNikolay Borisov * @tmp: temporary list used for iteration 1621*12a824dcSFilipe Manana * @cache: a backref lookup result cache 16222c2ed5aaSMark Fasheh * 16238eedaddaSFilipe Manana * btrfs_is_data_extent_shared uses the backref walking code but will short 16242c2ed5aaSMark Fasheh * circuit as soon as it finds a root or inode that doesn't match the 16252c2ed5aaSMark Fasheh * one passed in. This provides a significant performance benefit for 16262c2ed5aaSMark Fasheh * callers (such as fiemap) which want to know whether the extent is 16272c2ed5aaSMark Fasheh * shared but do not need a ref count. 16282c2ed5aaSMark Fasheh * 162903628cdbSFilipe Manana * This attempts to attach to the running transaction in order to account for 163003628cdbSFilipe Manana * delayed refs, but continues on even when no running transaction exists. 1631bb739cf0SEdmund Nadolski * 16322c2ed5aaSMark Fasheh * Return: 0 if extent is not shared, 1 if it is shared, < 0 on error. 16332c2ed5aaSMark Fasheh */ 16348eedaddaSFilipe Manana int btrfs_is_data_extent_shared(struct btrfs_root *root, u64 inum, u64 bytenr, 1635*12a824dcSFilipe Manana struct ulist *roots, struct ulist *tmp, 1636*12a824dcSFilipe Manana struct btrfs_backref_shared_cache *cache) 1637dc046b10SJosef Bacik { 1638bb739cf0SEdmund Nadolski struct btrfs_fs_info *fs_info = root->fs_info; 1639bb739cf0SEdmund Nadolski struct btrfs_trans_handle *trans; 1640dc046b10SJosef Bacik struct ulist_iterator uiter; 1641dc046b10SJosef Bacik struct ulist_node *node; 1642f3a84ccdSFilipe Manana struct btrfs_seq_list elem = BTRFS_SEQ_LIST_INIT(elem); 1643dc046b10SJosef Bacik int ret = 0; 16443ec4d323SEdmund Nadolski struct share_check shared = { 16454fd786e6SMisono Tomohiro .root_objectid = root->root_key.objectid, 16463ec4d323SEdmund Nadolski .inum = inum, 16473ec4d323SEdmund Nadolski .share_count = 0, 16483ec4d323SEdmund Nadolski }; 1649*12a824dcSFilipe Manana int level; 1650dc046b10SJosef Bacik 16515911c8feSDavid Sterba ulist_init(roots); 16525911c8feSDavid Sterba ulist_init(tmp); 1653dc046b10SJosef Bacik 1654a6d155d2SFilipe Manana trans = btrfs_join_transaction_nostart(root); 1655bb739cf0SEdmund Nadolski if (IS_ERR(trans)) { 165603628cdbSFilipe Manana if (PTR_ERR(trans) != -ENOENT && PTR_ERR(trans) != -EROFS) { 165703628cdbSFilipe Manana ret = PTR_ERR(trans); 165803628cdbSFilipe Manana goto out; 165903628cdbSFilipe Manana } 1660bb739cf0SEdmund Nadolski trans = NULL; 1661dc046b10SJosef Bacik down_read(&fs_info->commit_root_sem); 1662bb739cf0SEdmund Nadolski } else { 1663bb739cf0SEdmund Nadolski btrfs_get_tree_mod_seq(fs_info, &elem); 1664bb739cf0SEdmund Nadolski } 1665bb739cf0SEdmund Nadolski 1666*12a824dcSFilipe Manana /* -1 means we are in the bytenr of the data extent. */ 1667*12a824dcSFilipe Manana level = -1; 1668dc046b10SJosef Bacik ULIST_ITER_INIT(&uiter); 1669dc046b10SJosef Bacik while (1) { 1670*12a824dcSFilipe Manana bool is_shared; 1671*12a824dcSFilipe Manana bool cached; 1672*12a824dcSFilipe Manana 1673dc046b10SJosef Bacik ret = find_parent_nodes(trans, fs_info, bytenr, elem.seq, tmp, 1674c995ab3cSZygo Blaxell roots, NULL, &shared, false); 1675dc046b10SJosef Bacik if (ret == BACKREF_FOUND_SHARED) { 16762c2ed5aaSMark Fasheh /* this is the only condition under which we return 1 */ 1677dc046b10SJosef Bacik ret = 1; 1678*12a824dcSFilipe Manana if (level >= 0) 1679*12a824dcSFilipe Manana store_backref_shared_cache(cache, root, bytenr, 1680*12a824dcSFilipe Manana level, true); 1681dc046b10SJosef Bacik break; 1682dc046b10SJosef Bacik } 1683dc046b10SJosef Bacik if (ret < 0 && ret != -ENOENT) 1684dc046b10SJosef Bacik break; 16852c2ed5aaSMark Fasheh ret = 0; 1686*12a824dcSFilipe Manana if (level >= 0) 1687*12a824dcSFilipe Manana store_backref_shared_cache(cache, root, bytenr, 1688*12a824dcSFilipe Manana level, false); 1689dc046b10SJosef Bacik node = ulist_next(tmp, &uiter); 1690dc046b10SJosef Bacik if (!node) 1691dc046b10SJosef Bacik break; 1692dc046b10SJosef Bacik bytenr = node->val; 1693*12a824dcSFilipe Manana level++; 1694*12a824dcSFilipe Manana cached = lookup_backref_shared_cache(cache, root, bytenr, level, 1695*12a824dcSFilipe Manana &is_shared); 1696*12a824dcSFilipe Manana if (cached) { 1697*12a824dcSFilipe Manana ret = (is_shared ? 1 : 0); 1698*12a824dcSFilipe Manana break; 1699*12a824dcSFilipe Manana } 170018bf591bSEdmund Nadolski shared.share_count = 0; 1701dc046b10SJosef Bacik cond_resched(); 1702dc046b10SJosef Bacik } 1703bb739cf0SEdmund Nadolski 1704bb739cf0SEdmund Nadolski if (trans) { 1705dc046b10SJosef Bacik btrfs_put_tree_mod_seq(fs_info, &elem); 1706bb739cf0SEdmund Nadolski btrfs_end_transaction(trans); 1707bb739cf0SEdmund Nadolski } else { 1708dc046b10SJosef Bacik up_read(&fs_info->commit_root_sem); 1709bb739cf0SEdmund Nadolski } 171003628cdbSFilipe Manana out: 17115911c8feSDavid Sterba ulist_release(roots); 17125911c8feSDavid Sterba ulist_release(tmp); 1713dc046b10SJosef Bacik return ret; 1714dc046b10SJosef Bacik } 1715dc046b10SJosef Bacik 1716f186373fSMark Fasheh int btrfs_find_one_extref(struct btrfs_root *root, u64 inode_objectid, 1717f186373fSMark Fasheh u64 start_off, struct btrfs_path *path, 1718f186373fSMark Fasheh struct btrfs_inode_extref **ret_extref, 1719f186373fSMark Fasheh u64 *found_off) 1720f186373fSMark Fasheh { 1721f186373fSMark Fasheh int ret, slot; 1722f186373fSMark Fasheh struct btrfs_key key; 1723f186373fSMark Fasheh struct btrfs_key found_key; 1724f186373fSMark Fasheh struct btrfs_inode_extref *extref; 172573980becSJeff Mahoney const struct extent_buffer *leaf; 1726f186373fSMark Fasheh unsigned long ptr; 1727f186373fSMark Fasheh 1728f186373fSMark Fasheh key.objectid = inode_objectid; 1729962a298fSDavid Sterba key.type = BTRFS_INODE_EXTREF_KEY; 1730f186373fSMark Fasheh key.offset = start_off; 1731f186373fSMark Fasheh 1732f186373fSMark Fasheh ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); 1733f186373fSMark Fasheh if (ret < 0) 1734f186373fSMark Fasheh return ret; 1735f186373fSMark Fasheh 1736f186373fSMark Fasheh while (1) { 1737f186373fSMark Fasheh leaf = path->nodes[0]; 1738f186373fSMark Fasheh slot = path->slots[0]; 1739f186373fSMark Fasheh if (slot >= btrfs_header_nritems(leaf)) { 1740f186373fSMark Fasheh /* 1741f186373fSMark Fasheh * If the item at offset is not found, 1742f186373fSMark Fasheh * btrfs_search_slot will point us to the slot 1743f186373fSMark Fasheh * where it should be inserted. In our case 1744f186373fSMark Fasheh * that will be the slot directly before the 1745f186373fSMark Fasheh * next INODE_REF_KEY_V2 item. In the case 1746f186373fSMark Fasheh * that we're pointing to the last slot in a 1747f186373fSMark Fasheh * leaf, we must move one leaf over. 1748f186373fSMark Fasheh */ 1749f186373fSMark Fasheh ret = btrfs_next_leaf(root, path); 1750f186373fSMark Fasheh if (ret) { 1751f186373fSMark Fasheh if (ret >= 1) 1752f186373fSMark Fasheh ret = -ENOENT; 1753f186373fSMark Fasheh break; 1754f186373fSMark Fasheh } 1755f186373fSMark Fasheh continue; 1756f186373fSMark Fasheh } 1757f186373fSMark Fasheh 1758f186373fSMark Fasheh btrfs_item_key_to_cpu(leaf, &found_key, slot); 1759f186373fSMark Fasheh 1760f186373fSMark Fasheh /* 1761f186373fSMark Fasheh * Check that we're still looking at an extended ref key for 1762f186373fSMark Fasheh * this particular objectid. If we have different 1763f186373fSMark Fasheh * objectid or type then there are no more to be found 1764f186373fSMark Fasheh * in the tree and we can exit. 1765f186373fSMark Fasheh */ 1766f186373fSMark Fasheh ret = -ENOENT; 1767f186373fSMark Fasheh if (found_key.objectid != inode_objectid) 1768f186373fSMark Fasheh break; 1769962a298fSDavid Sterba if (found_key.type != BTRFS_INODE_EXTREF_KEY) 1770f186373fSMark Fasheh break; 1771f186373fSMark Fasheh 1772f186373fSMark Fasheh ret = 0; 1773f186373fSMark Fasheh ptr = btrfs_item_ptr_offset(leaf, path->slots[0]); 1774f186373fSMark Fasheh extref = (struct btrfs_inode_extref *)ptr; 1775f186373fSMark Fasheh *ret_extref = extref; 1776f186373fSMark Fasheh if (found_off) 1777f186373fSMark Fasheh *found_off = found_key.offset; 1778f186373fSMark Fasheh break; 1779f186373fSMark Fasheh } 1780f186373fSMark Fasheh 1781f186373fSMark Fasheh return ret; 1782f186373fSMark Fasheh } 1783f186373fSMark Fasheh 178448a3b636SEric Sandeen /* 178548a3b636SEric Sandeen * this iterates to turn a name (from iref/extref) into a full filesystem path. 178648a3b636SEric Sandeen * Elements of the path are separated by '/' and the path is guaranteed to be 178748a3b636SEric Sandeen * 0-terminated. the path is only given within the current file system. 178848a3b636SEric Sandeen * Therefore, it never starts with a '/'. the caller is responsible to provide 178948a3b636SEric Sandeen * "size" bytes in "dest". the dest buffer will be filled backwards. finally, 179048a3b636SEric Sandeen * the start point of the resulting string is returned. this pointer is within 179148a3b636SEric Sandeen * dest, normally. 179248a3b636SEric Sandeen * in case the path buffer would overflow, the pointer is decremented further 179348a3b636SEric Sandeen * as if output was written to the buffer, though no more output is actually 179448a3b636SEric Sandeen * generated. that way, the caller can determine how much space would be 179548a3b636SEric Sandeen * required for the path to fit into the buffer. in that case, the returned 179648a3b636SEric Sandeen * value will be smaller than dest. callers must check this! 179748a3b636SEric Sandeen */ 179896b5bd77SJan Schmidt char *btrfs_ref_to_path(struct btrfs_root *fs_root, struct btrfs_path *path, 1799d24bec3aSMark Fasheh u32 name_len, unsigned long name_off, 1800a542ad1bSJan Schmidt struct extent_buffer *eb_in, u64 parent, 1801a542ad1bSJan Schmidt char *dest, u32 size) 1802a542ad1bSJan Schmidt { 1803a542ad1bSJan Schmidt int slot; 1804a542ad1bSJan Schmidt u64 next_inum; 1805a542ad1bSJan Schmidt int ret; 1806661bec6bSGabriel de Perthuis s64 bytes_left = ((s64)size) - 1; 1807a542ad1bSJan Schmidt struct extent_buffer *eb = eb_in; 1808a542ad1bSJan Schmidt struct btrfs_key found_key; 1809d24bec3aSMark Fasheh struct btrfs_inode_ref *iref; 1810a542ad1bSJan Schmidt 1811a542ad1bSJan Schmidt if (bytes_left >= 0) 1812a542ad1bSJan Schmidt dest[bytes_left] = '\0'; 1813a542ad1bSJan Schmidt 1814a542ad1bSJan Schmidt while (1) { 1815d24bec3aSMark Fasheh bytes_left -= name_len; 1816a542ad1bSJan Schmidt if (bytes_left >= 0) 1817a542ad1bSJan Schmidt read_extent_buffer(eb, dest + bytes_left, 1818d24bec3aSMark Fasheh name_off, name_len); 1819b916a59aSJan Schmidt if (eb != eb_in) { 18200c0fe3b0SFilipe Manana if (!path->skip_locking) 1821ac5887c8SJosef Bacik btrfs_tree_read_unlock(eb); 1822a542ad1bSJan Schmidt free_extent_buffer(eb); 1823b916a59aSJan Schmidt } 1824c234a24dSDavid Sterba ret = btrfs_find_item(fs_root, path, parent, 0, 1825c234a24dSDavid Sterba BTRFS_INODE_REF_KEY, &found_key); 18268f24b496SJan Schmidt if (ret > 0) 18278f24b496SJan Schmidt ret = -ENOENT; 1828a542ad1bSJan Schmidt if (ret) 1829a542ad1bSJan Schmidt break; 1830d24bec3aSMark Fasheh 1831a542ad1bSJan Schmidt next_inum = found_key.offset; 1832a542ad1bSJan Schmidt 1833a542ad1bSJan Schmidt /* regular exit ahead */ 1834a542ad1bSJan Schmidt if (parent == next_inum) 1835a542ad1bSJan Schmidt break; 1836a542ad1bSJan Schmidt 1837a542ad1bSJan Schmidt slot = path->slots[0]; 1838a542ad1bSJan Schmidt eb = path->nodes[0]; 1839a542ad1bSJan Schmidt /* make sure we can use eb after releasing the path */ 1840b916a59aSJan Schmidt if (eb != eb_in) { 18410c0fe3b0SFilipe Manana path->nodes[0] = NULL; 18420c0fe3b0SFilipe Manana path->locks[0] = 0; 1843b916a59aSJan Schmidt } 1844a542ad1bSJan Schmidt btrfs_release_path(path); 1845a542ad1bSJan Schmidt iref = btrfs_item_ptr(eb, slot, struct btrfs_inode_ref); 1846d24bec3aSMark Fasheh 1847d24bec3aSMark Fasheh name_len = btrfs_inode_ref_name_len(eb, iref); 1848d24bec3aSMark Fasheh name_off = (unsigned long)(iref + 1); 1849d24bec3aSMark Fasheh 1850a542ad1bSJan Schmidt parent = next_inum; 1851a542ad1bSJan Schmidt --bytes_left; 1852a542ad1bSJan Schmidt if (bytes_left >= 0) 1853a542ad1bSJan Schmidt dest[bytes_left] = '/'; 1854a542ad1bSJan Schmidt } 1855a542ad1bSJan Schmidt 1856a542ad1bSJan Schmidt btrfs_release_path(path); 1857a542ad1bSJan Schmidt 1858a542ad1bSJan Schmidt if (ret) 1859a542ad1bSJan Schmidt return ERR_PTR(ret); 1860a542ad1bSJan Schmidt 1861a542ad1bSJan Schmidt return dest + bytes_left; 1862a542ad1bSJan Schmidt } 1863a542ad1bSJan Schmidt 1864a542ad1bSJan Schmidt /* 1865a542ad1bSJan Schmidt * this makes the path point to (logical EXTENT_ITEM *) 1866a542ad1bSJan Schmidt * returns BTRFS_EXTENT_FLAG_DATA for data, BTRFS_EXTENT_FLAG_TREE_BLOCK for 1867a542ad1bSJan Schmidt * tree blocks and <0 on error. 1868a542ad1bSJan Schmidt */ 1869a542ad1bSJan Schmidt int extent_from_logical(struct btrfs_fs_info *fs_info, u64 logical, 187069917e43SLiu Bo struct btrfs_path *path, struct btrfs_key *found_key, 187169917e43SLiu Bo u64 *flags_ret) 1872a542ad1bSJan Schmidt { 187329cbcf40SJosef Bacik struct btrfs_root *extent_root = btrfs_extent_root(fs_info, logical); 1874a542ad1bSJan Schmidt int ret; 1875a542ad1bSJan Schmidt u64 flags; 1876261c84b6SJosef Bacik u64 size = 0; 1877a542ad1bSJan Schmidt u32 item_size; 187873980becSJeff Mahoney const struct extent_buffer *eb; 1879a542ad1bSJan Schmidt struct btrfs_extent_item *ei; 1880a542ad1bSJan Schmidt struct btrfs_key key; 1881a542ad1bSJan Schmidt 1882261c84b6SJosef Bacik if (btrfs_fs_incompat(fs_info, SKINNY_METADATA)) 1883261c84b6SJosef Bacik key.type = BTRFS_METADATA_ITEM_KEY; 1884261c84b6SJosef Bacik else 1885a542ad1bSJan Schmidt key.type = BTRFS_EXTENT_ITEM_KEY; 1886a542ad1bSJan Schmidt key.objectid = logical; 1887a542ad1bSJan Schmidt key.offset = (u64)-1; 1888a542ad1bSJan Schmidt 188929cbcf40SJosef Bacik ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0); 1890a542ad1bSJan Schmidt if (ret < 0) 1891a542ad1bSJan Schmidt return ret; 1892a542ad1bSJan Schmidt 189329cbcf40SJosef Bacik ret = btrfs_previous_extent_item(extent_root, path, 0); 1894850a8cdfSWang Shilong if (ret) { 1895850a8cdfSWang Shilong if (ret > 0) 1896580f0a67SJosef Bacik ret = -ENOENT; 1897580f0a67SJosef Bacik return ret; 1898580f0a67SJosef Bacik } 1899850a8cdfSWang Shilong btrfs_item_key_to_cpu(path->nodes[0], found_key, path->slots[0]); 1900261c84b6SJosef Bacik if (found_key->type == BTRFS_METADATA_ITEM_KEY) 1901da17066cSJeff Mahoney size = fs_info->nodesize; 1902261c84b6SJosef Bacik else if (found_key->type == BTRFS_EXTENT_ITEM_KEY) 1903261c84b6SJosef Bacik size = found_key->offset; 1904261c84b6SJosef Bacik 1905580f0a67SJosef Bacik if (found_key->objectid > logical || 1906261c84b6SJosef Bacik found_key->objectid + size <= logical) { 1907ab8d0fc4SJeff Mahoney btrfs_debug(fs_info, 1908ab8d0fc4SJeff Mahoney "logical %llu is not within any extent", logical); 1909a542ad1bSJan Schmidt return -ENOENT; 19104692cf58SJan Schmidt } 1911a542ad1bSJan Schmidt 1912a542ad1bSJan Schmidt eb = path->nodes[0]; 19133212fa14SJosef Bacik item_size = btrfs_item_size(eb, path->slots[0]); 1914a542ad1bSJan Schmidt BUG_ON(item_size < sizeof(*ei)); 1915a542ad1bSJan Schmidt 1916a542ad1bSJan Schmidt ei = btrfs_item_ptr(eb, path->slots[0], struct btrfs_extent_item); 1917a542ad1bSJan Schmidt flags = btrfs_extent_flags(eb, ei); 1918a542ad1bSJan Schmidt 1919ab8d0fc4SJeff Mahoney btrfs_debug(fs_info, 1920ab8d0fc4SJeff Mahoney "logical %llu is at position %llu within the extent (%llu EXTENT_ITEM %llu) flags %#llx size %u", 1921c1c9ff7cSGeert Uytterhoeven logical, logical - found_key->objectid, found_key->objectid, 1922c1c9ff7cSGeert Uytterhoeven found_key->offset, flags, item_size); 192369917e43SLiu Bo 192469917e43SLiu Bo WARN_ON(!flags_ret); 192569917e43SLiu Bo if (flags_ret) { 1926a542ad1bSJan Schmidt if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) 192769917e43SLiu Bo *flags_ret = BTRFS_EXTENT_FLAG_TREE_BLOCK; 192869917e43SLiu Bo else if (flags & BTRFS_EXTENT_FLAG_DATA) 192969917e43SLiu Bo *flags_ret = BTRFS_EXTENT_FLAG_DATA; 193069917e43SLiu Bo else 1931290342f6SArnd Bergmann BUG(); 193269917e43SLiu Bo return 0; 193369917e43SLiu Bo } 1934a542ad1bSJan Schmidt 1935a542ad1bSJan Schmidt return -EIO; 1936a542ad1bSJan Schmidt } 1937a542ad1bSJan Schmidt 1938a542ad1bSJan Schmidt /* 1939a542ad1bSJan Schmidt * helper function to iterate extent inline refs. ptr must point to a 0 value 1940a542ad1bSJan Schmidt * for the first call and may be modified. it is used to track state. 1941a542ad1bSJan Schmidt * if more refs exist, 0 is returned and the next call to 1942e0c476b1SJeff Mahoney * get_extent_inline_ref must pass the modified ptr parameter to get the 1943a542ad1bSJan Schmidt * next ref. after the last ref was processed, 1 is returned. 1944a542ad1bSJan Schmidt * returns <0 on error 1945a542ad1bSJan Schmidt */ 1946e0c476b1SJeff Mahoney static int get_extent_inline_ref(unsigned long *ptr, 194773980becSJeff Mahoney const struct extent_buffer *eb, 194873980becSJeff Mahoney const struct btrfs_key *key, 194973980becSJeff Mahoney const struct btrfs_extent_item *ei, 195073980becSJeff Mahoney u32 item_size, 1951a542ad1bSJan Schmidt struct btrfs_extent_inline_ref **out_eiref, 1952a542ad1bSJan Schmidt int *out_type) 1953a542ad1bSJan Schmidt { 1954a542ad1bSJan Schmidt unsigned long end; 1955a542ad1bSJan Schmidt u64 flags; 1956a542ad1bSJan Schmidt struct btrfs_tree_block_info *info; 1957a542ad1bSJan Schmidt 1958a542ad1bSJan Schmidt if (!*ptr) { 1959a542ad1bSJan Schmidt /* first call */ 1960a542ad1bSJan Schmidt flags = btrfs_extent_flags(eb, ei); 1961a542ad1bSJan Schmidt if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) { 19626eda71d0SLiu Bo if (key->type == BTRFS_METADATA_ITEM_KEY) { 19636eda71d0SLiu Bo /* a skinny metadata extent */ 19646eda71d0SLiu Bo *out_eiref = 19656eda71d0SLiu Bo (struct btrfs_extent_inline_ref *)(ei + 1); 19666eda71d0SLiu Bo } else { 19676eda71d0SLiu Bo WARN_ON(key->type != BTRFS_EXTENT_ITEM_KEY); 1968a542ad1bSJan Schmidt info = (struct btrfs_tree_block_info *)(ei + 1); 1969a542ad1bSJan Schmidt *out_eiref = 1970a542ad1bSJan Schmidt (struct btrfs_extent_inline_ref *)(info + 1); 19716eda71d0SLiu Bo } 1972a542ad1bSJan Schmidt } else { 1973a542ad1bSJan Schmidt *out_eiref = (struct btrfs_extent_inline_ref *)(ei + 1); 1974a542ad1bSJan Schmidt } 1975a542ad1bSJan Schmidt *ptr = (unsigned long)*out_eiref; 1976cd857dd6SLiu Bo if ((unsigned long)(*ptr) >= (unsigned long)ei + item_size) 1977a542ad1bSJan Schmidt return -ENOENT; 1978a542ad1bSJan Schmidt } 1979a542ad1bSJan Schmidt 1980a542ad1bSJan Schmidt end = (unsigned long)ei + item_size; 19816eda71d0SLiu Bo *out_eiref = (struct btrfs_extent_inline_ref *)(*ptr); 19823de28d57SLiu Bo *out_type = btrfs_get_extent_inline_ref_type(eb, *out_eiref, 19833de28d57SLiu Bo BTRFS_REF_TYPE_ANY); 19843de28d57SLiu Bo if (*out_type == BTRFS_REF_TYPE_INVALID) 1985af431dcbSSu Yue return -EUCLEAN; 1986a542ad1bSJan Schmidt 1987a542ad1bSJan Schmidt *ptr += btrfs_extent_inline_ref_size(*out_type); 1988a542ad1bSJan Schmidt WARN_ON(*ptr > end); 1989a542ad1bSJan Schmidt if (*ptr == end) 1990a542ad1bSJan Schmidt return 1; /* last */ 1991a542ad1bSJan Schmidt 1992a542ad1bSJan Schmidt return 0; 1993a542ad1bSJan Schmidt } 1994a542ad1bSJan Schmidt 1995a542ad1bSJan Schmidt /* 1996a542ad1bSJan Schmidt * reads the tree block backref for an extent. tree level and root are returned 1997a542ad1bSJan Schmidt * through out_level and out_root. ptr must point to a 0 value for the first 1998e0c476b1SJeff Mahoney * call and may be modified (see get_extent_inline_ref comment). 1999a542ad1bSJan Schmidt * returns 0 if data was provided, 1 if there was no more data to provide or 2000a542ad1bSJan Schmidt * <0 on error. 2001a542ad1bSJan Schmidt */ 2002a542ad1bSJan Schmidt int tree_backref_for_extent(unsigned long *ptr, struct extent_buffer *eb, 20036eda71d0SLiu Bo struct btrfs_key *key, struct btrfs_extent_item *ei, 20046eda71d0SLiu Bo u32 item_size, u64 *out_root, u8 *out_level) 2005a542ad1bSJan Schmidt { 2006a542ad1bSJan Schmidt int ret; 2007a542ad1bSJan Schmidt int type; 2008a542ad1bSJan Schmidt struct btrfs_extent_inline_ref *eiref; 2009a542ad1bSJan Schmidt 2010a542ad1bSJan Schmidt if (*ptr == (unsigned long)-1) 2011a542ad1bSJan Schmidt return 1; 2012a542ad1bSJan Schmidt 2013a542ad1bSJan Schmidt while (1) { 2014e0c476b1SJeff Mahoney ret = get_extent_inline_ref(ptr, eb, key, ei, item_size, 2015a542ad1bSJan Schmidt &eiref, &type); 2016a542ad1bSJan Schmidt if (ret < 0) 2017a542ad1bSJan Schmidt return ret; 2018a542ad1bSJan Schmidt 2019a542ad1bSJan Schmidt if (type == BTRFS_TREE_BLOCK_REF_KEY || 2020a542ad1bSJan Schmidt type == BTRFS_SHARED_BLOCK_REF_KEY) 2021a542ad1bSJan Schmidt break; 2022a542ad1bSJan Schmidt 2023a542ad1bSJan Schmidt if (ret == 1) 2024a542ad1bSJan Schmidt return 1; 2025a542ad1bSJan Schmidt } 2026a542ad1bSJan Schmidt 2027a542ad1bSJan Schmidt /* we can treat both ref types equally here */ 2028a542ad1bSJan Schmidt *out_root = btrfs_extent_inline_ref_offset(eb, eiref); 2029a1317f45SFilipe Manana 2030a1317f45SFilipe Manana if (key->type == BTRFS_EXTENT_ITEM_KEY) { 2031a1317f45SFilipe Manana struct btrfs_tree_block_info *info; 2032a1317f45SFilipe Manana 2033a1317f45SFilipe Manana info = (struct btrfs_tree_block_info *)(ei + 1); 2034a542ad1bSJan Schmidt *out_level = btrfs_tree_block_level(eb, info); 2035a1317f45SFilipe Manana } else { 2036a1317f45SFilipe Manana ASSERT(key->type == BTRFS_METADATA_ITEM_KEY); 2037a1317f45SFilipe Manana *out_level = (u8)key->offset; 2038a1317f45SFilipe Manana } 2039a542ad1bSJan Schmidt 2040a542ad1bSJan Schmidt if (ret == 1) 2041a542ad1bSJan Schmidt *ptr = (unsigned long)-1; 2042a542ad1bSJan Schmidt 2043a542ad1bSJan Schmidt return 0; 2044a542ad1bSJan Schmidt } 2045a542ad1bSJan Schmidt 2046ab8d0fc4SJeff Mahoney static int iterate_leaf_refs(struct btrfs_fs_info *fs_info, 2047ab8d0fc4SJeff Mahoney struct extent_inode_elem *inode_list, 2048976b1908SJan Schmidt u64 root, u64 extent_item_objectid, 20494692cf58SJan Schmidt iterate_extent_inodes_t *iterate, void *ctx) 2050a542ad1bSJan Schmidt { 2051976b1908SJan Schmidt struct extent_inode_elem *eie; 20524692cf58SJan Schmidt int ret = 0; 2053a542ad1bSJan Schmidt 2054976b1908SJan Schmidt for (eie = inode_list; eie; eie = eie->next) { 2055ab8d0fc4SJeff Mahoney btrfs_debug(fs_info, 2056ab8d0fc4SJeff Mahoney "ref for %llu resolved, key (%llu EXTEND_DATA %llu), root %llu", 2057ab8d0fc4SJeff Mahoney extent_item_objectid, eie->inum, 2058ab8d0fc4SJeff Mahoney eie->offset, root); 2059976b1908SJan Schmidt ret = iterate(eie->inum, eie->offset, root, ctx); 20604692cf58SJan Schmidt if (ret) { 2061ab8d0fc4SJeff Mahoney btrfs_debug(fs_info, 2062ab8d0fc4SJeff Mahoney "stopping iteration for %llu due to ret=%d", 2063976b1908SJan Schmidt extent_item_objectid, ret); 2064a542ad1bSJan Schmidt break; 2065a542ad1bSJan Schmidt } 2066a542ad1bSJan Schmidt } 2067a542ad1bSJan Schmidt 2068a542ad1bSJan Schmidt return ret; 2069a542ad1bSJan Schmidt } 2070a542ad1bSJan Schmidt 2071a542ad1bSJan Schmidt /* 2072a542ad1bSJan Schmidt * calls iterate() for every inode that references the extent identified by 20734692cf58SJan Schmidt * the given parameters. 2074a542ad1bSJan Schmidt * when the iterator function returns a non-zero value, iteration stops. 2075a542ad1bSJan Schmidt */ 2076a542ad1bSJan Schmidt int iterate_extent_inodes(struct btrfs_fs_info *fs_info, 20774692cf58SJan Schmidt u64 extent_item_objectid, u64 extent_item_pos, 20787a3ae2f8SJan Schmidt int search_commit_root, 2079c995ab3cSZygo Blaxell iterate_extent_inodes_t *iterate, void *ctx, 2080c995ab3cSZygo Blaxell bool ignore_offset) 2081a542ad1bSJan Schmidt { 2082a542ad1bSJan Schmidt int ret; 2083da61d31aSJosef Bacik struct btrfs_trans_handle *trans = NULL; 20847a3ae2f8SJan Schmidt struct ulist *refs = NULL; 20857a3ae2f8SJan Schmidt struct ulist *roots = NULL; 20864692cf58SJan Schmidt struct ulist_node *ref_node = NULL; 20874692cf58SJan Schmidt struct ulist_node *root_node = NULL; 2088f3a84ccdSFilipe Manana struct btrfs_seq_list seq_elem = BTRFS_SEQ_LIST_INIT(seq_elem); 2089cd1b413cSJan Schmidt struct ulist_iterator ref_uiter; 2090cd1b413cSJan Schmidt struct ulist_iterator root_uiter; 2091a542ad1bSJan Schmidt 2092ab8d0fc4SJeff Mahoney btrfs_debug(fs_info, "resolving all inodes for extent %llu", 20934692cf58SJan Schmidt extent_item_objectid); 20944692cf58SJan Schmidt 2095da61d31aSJosef Bacik if (!search_commit_root) { 209630a9da5dSJosef Bacik trans = btrfs_attach_transaction(fs_info->tree_root); 2097bfc61c36SFilipe Manana if (IS_ERR(trans)) { 2098bfc61c36SFilipe Manana if (PTR_ERR(trans) != -ENOENT && 2099bfc61c36SFilipe Manana PTR_ERR(trans) != -EROFS) 21007a3ae2f8SJan Schmidt return PTR_ERR(trans); 2101bfc61c36SFilipe Manana trans = NULL; 21027a3ae2f8SJan Schmidt } 2103bfc61c36SFilipe Manana } 2104bfc61c36SFilipe Manana 2105bfc61c36SFilipe Manana if (trans) 2106f3a84ccdSFilipe Manana btrfs_get_tree_mod_seq(fs_info, &seq_elem); 2107bfc61c36SFilipe Manana else 2108bfc61c36SFilipe Manana down_read(&fs_info->commit_root_sem); 21094692cf58SJan Schmidt 21104692cf58SJan Schmidt ret = btrfs_find_all_leafs(trans, fs_info, extent_item_objectid, 2111f3a84ccdSFilipe Manana seq_elem.seq, &refs, 2112c995ab3cSZygo Blaxell &extent_item_pos, ignore_offset); 21134692cf58SJan Schmidt if (ret) 21144692cf58SJan Schmidt goto out; 21154692cf58SJan Schmidt 2116cd1b413cSJan Schmidt ULIST_ITER_INIT(&ref_uiter); 2117cd1b413cSJan Schmidt while (!ret && (ref_node = ulist_next(refs, &ref_uiter))) { 2118e0c476b1SJeff Mahoney ret = btrfs_find_all_roots_safe(trans, fs_info, ref_node->val, 2119f3a84ccdSFilipe Manana seq_elem.seq, &roots, 2120c995ab3cSZygo Blaxell ignore_offset); 21214692cf58SJan Schmidt if (ret) 2122a542ad1bSJan Schmidt break; 2123cd1b413cSJan Schmidt ULIST_ITER_INIT(&root_uiter); 2124cd1b413cSJan Schmidt while (!ret && (root_node = ulist_next(roots, &root_uiter))) { 2125ab8d0fc4SJeff Mahoney btrfs_debug(fs_info, 2126ab8d0fc4SJeff Mahoney "root %llu references leaf %llu, data list %#llx", 2127ab8d0fc4SJeff Mahoney root_node->val, ref_node->val, 2128c1c9ff7cSGeert Uytterhoeven ref_node->aux); 2129ab8d0fc4SJeff Mahoney ret = iterate_leaf_refs(fs_info, 2130ab8d0fc4SJeff Mahoney (struct extent_inode_elem *) 2131995e01b7SJan Schmidt (uintptr_t)ref_node->aux, 2132995e01b7SJan Schmidt root_node->val, 2133995e01b7SJan Schmidt extent_item_objectid, 2134a542ad1bSJan Schmidt iterate, ctx); 21354692cf58SJan Schmidt } 2136976b1908SJan Schmidt ulist_free(roots); 2137a542ad1bSJan Schmidt } 2138a542ad1bSJan Schmidt 2139976b1908SJan Schmidt free_leaf_list(refs); 21404692cf58SJan Schmidt out: 2141bfc61c36SFilipe Manana if (trans) { 2142f3a84ccdSFilipe Manana btrfs_put_tree_mod_seq(fs_info, &seq_elem); 21433a45bb20SJeff Mahoney btrfs_end_transaction(trans); 21449e351cc8SJosef Bacik } else { 21459e351cc8SJosef Bacik up_read(&fs_info->commit_root_sem); 21467a3ae2f8SJan Schmidt } 21477a3ae2f8SJan Schmidt 2148a542ad1bSJan Schmidt return ret; 2149a542ad1bSJan Schmidt } 2150a542ad1bSJan Schmidt 2151e3059ec0SDavid Sterba static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx) 2152e3059ec0SDavid Sterba { 2153e3059ec0SDavid Sterba struct btrfs_data_container *inodes = ctx; 2154e3059ec0SDavid Sterba const size_t c = 3 * sizeof(u64); 2155e3059ec0SDavid Sterba 2156e3059ec0SDavid Sterba if (inodes->bytes_left >= c) { 2157e3059ec0SDavid Sterba inodes->bytes_left -= c; 2158e3059ec0SDavid Sterba inodes->val[inodes->elem_cnt] = inum; 2159e3059ec0SDavid Sterba inodes->val[inodes->elem_cnt + 1] = offset; 2160e3059ec0SDavid Sterba inodes->val[inodes->elem_cnt + 2] = root; 2161e3059ec0SDavid Sterba inodes->elem_cnt += 3; 2162e3059ec0SDavid Sterba } else { 2163e3059ec0SDavid Sterba inodes->bytes_missing += c - inodes->bytes_left; 2164e3059ec0SDavid Sterba inodes->bytes_left = 0; 2165e3059ec0SDavid Sterba inodes->elem_missed += 3; 2166e3059ec0SDavid Sterba } 2167e3059ec0SDavid Sterba 2168e3059ec0SDavid Sterba return 0; 2169e3059ec0SDavid Sterba } 2170e3059ec0SDavid Sterba 2171a542ad1bSJan Schmidt int iterate_inodes_from_logical(u64 logical, struct btrfs_fs_info *fs_info, 2172a542ad1bSJan Schmidt struct btrfs_path *path, 2173e3059ec0SDavid Sterba void *ctx, bool ignore_offset) 2174a542ad1bSJan Schmidt { 2175a542ad1bSJan Schmidt int ret; 21764692cf58SJan Schmidt u64 extent_item_pos; 217769917e43SLiu Bo u64 flags = 0; 2178a542ad1bSJan Schmidt struct btrfs_key found_key; 21797a3ae2f8SJan Schmidt int search_commit_root = path->search_commit_root; 2180a542ad1bSJan Schmidt 218169917e43SLiu Bo ret = extent_from_logical(fs_info, logical, path, &found_key, &flags); 21824692cf58SJan Schmidt btrfs_release_path(path); 2183a542ad1bSJan Schmidt if (ret < 0) 2184a542ad1bSJan Schmidt return ret; 218569917e43SLiu Bo if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) 21863627bf45SStefan Behrens return -EINVAL; 2187a542ad1bSJan Schmidt 21884692cf58SJan Schmidt extent_item_pos = logical - found_key.objectid; 21897a3ae2f8SJan Schmidt ret = iterate_extent_inodes(fs_info, found_key.objectid, 21907a3ae2f8SJan Schmidt extent_item_pos, search_commit_root, 2191e3059ec0SDavid Sterba build_ino_list, ctx, ignore_offset); 2192a542ad1bSJan Schmidt 2193a542ad1bSJan Schmidt return ret; 2194a542ad1bSJan Schmidt } 2195a542ad1bSJan Schmidt 2196ad6240f6SDavid Sterba static int inode_to_path(u64 inum, u32 name_len, unsigned long name_off, 2197875d1daaSDavid Sterba struct extent_buffer *eb, struct inode_fs_paths *ipath); 2198d24bec3aSMark Fasheh 2199875d1daaSDavid Sterba static int iterate_inode_refs(u64 inum, struct inode_fs_paths *ipath) 2200a542ad1bSJan Schmidt { 2201aefc1eb1SJan Schmidt int ret = 0; 2202a542ad1bSJan Schmidt int slot; 2203a542ad1bSJan Schmidt u32 cur; 2204a542ad1bSJan Schmidt u32 len; 2205a542ad1bSJan Schmidt u32 name_len; 2206a542ad1bSJan Schmidt u64 parent = 0; 2207a542ad1bSJan Schmidt int found = 0; 2208875d1daaSDavid Sterba struct btrfs_root *fs_root = ipath->fs_root; 2209875d1daaSDavid Sterba struct btrfs_path *path = ipath->btrfs_path; 2210a542ad1bSJan Schmidt struct extent_buffer *eb; 2211a542ad1bSJan Schmidt struct btrfs_inode_ref *iref; 2212a542ad1bSJan Schmidt struct btrfs_key found_key; 2213a542ad1bSJan Schmidt 2214aefc1eb1SJan Schmidt while (!ret) { 2215c234a24dSDavid Sterba ret = btrfs_find_item(fs_root, path, inum, 2216c234a24dSDavid Sterba parent ? parent + 1 : 0, BTRFS_INODE_REF_KEY, 2217a542ad1bSJan Schmidt &found_key); 2218c234a24dSDavid Sterba 2219a542ad1bSJan Schmidt if (ret < 0) 2220a542ad1bSJan Schmidt break; 2221a542ad1bSJan Schmidt if (ret) { 2222a542ad1bSJan Schmidt ret = found ? 0 : -ENOENT; 2223a542ad1bSJan Schmidt break; 2224a542ad1bSJan Schmidt } 2225a542ad1bSJan Schmidt ++found; 2226a542ad1bSJan Schmidt 2227a542ad1bSJan Schmidt parent = found_key.offset; 2228a542ad1bSJan Schmidt slot = path->slots[0]; 22293fe81ce2SFilipe David Borba Manana eb = btrfs_clone_extent_buffer(path->nodes[0]); 22303fe81ce2SFilipe David Borba Manana if (!eb) { 22313fe81ce2SFilipe David Borba Manana ret = -ENOMEM; 22323fe81ce2SFilipe David Borba Manana break; 22333fe81ce2SFilipe David Borba Manana } 2234a542ad1bSJan Schmidt btrfs_release_path(path); 2235a542ad1bSJan Schmidt 2236a542ad1bSJan Schmidt iref = btrfs_item_ptr(eb, slot, struct btrfs_inode_ref); 2237a542ad1bSJan Schmidt 22383212fa14SJosef Bacik for (cur = 0; cur < btrfs_item_size(eb, slot); cur += len) { 2239a542ad1bSJan Schmidt name_len = btrfs_inode_ref_name_len(eb, iref); 2240a542ad1bSJan Schmidt /* path must be released before calling iterate()! */ 2241ab8d0fc4SJeff Mahoney btrfs_debug(fs_root->fs_info, 2242ab8d0fc4SJeff Mahoney "following ref at offset %u for inode %llu in tree %llu", 22434fd786e6SMisono Tomohiro cur, found_key.objectid, 22444fd786e6SMisono Tomohiro fs_root->root_key.objectid); 2245ad6240f6SDavid Sterba ret = inode_to_path(parent, name_len, 2246875d1daaSDavid Sterba (unsigned long)(iref + 1), eb, ipath); 2247aefc1eb1SJan Schmidt if (ret) 2248a542ad1bSJan Schmidt break; 2249a542ad1bSJan Schmidt len = sizeof(*iref) + name_len; 2250a542ad1bSJan Schmidt iref = (struct btrfs_inode_ref *)((char *)iref + len); 2251a542ad1bSJan Schmidt } 2252a542ad1bSJan Schmidt free_extent_buffer(eb); 2253a542ad1bSJan Schmidt } 2254a542ad1bSJan Schmidt 2255a542ad1bSJan Schmidt btrfs_release_path(path); 2256a542ad1bSJan Schmidt 2257a542ad1bSJan Schmidt return ret; 2258a542ad1bSJan Schmidt } 2259a542ad1bSJan Schmidt 2260875d1daaSDavid Sterba static int iterate_inode_extrefs(u64 inum, struct inode_fs_paths *ipath) 2261d24bec3aSMark Fasheh { 2262d24bec3aSMark Fasheh int ret; 2263d24bec3aSMark Fasheh int slot; 2264d24bec3aSMark Fasheh u64 offset = 0; 2265d24bec3aSMark Fasheh u64 parent; 2266d24bec3aSMark Fasheh int found = 0; 2267875d1daaSDavid Sterba struct btrfs_root *fs_root = ipath->fs_root; 2268875d1daaSDavid Sterba struct btrfs_path *path = ipath->btrfs_path; 2269d24bec3aSMark Fasheh struct extent_buffer *eb; 2270d24bec3aSMark Fasheh struct btrfs_inode_extref *extref; 2271d24bec3aSMark Fasheh u32 item_size; 2272d24bec3aSMark Fasheh u32 cur_offset; 2273d24bec3aSMark Fasheh unsigned long ptr; 2274d24bec3aSMark Fasheh 2275d24bec3aSMark Fasheh while (1) { 2276d24bec3aSMark Fasheh ret = btrfs_find_one_extref(fs_root, inum, offset, path, &extref, 2277d24bec3aSMark Fasheh &offset); 2278d24bec3aSMark Fasheh if (ret < 0) 2279d24bec3aSMark Fasheh break; 2280d24bec3aSMark Fasheh if (ret) { 2281d24bec3aSMark Fasheh ret = found ? 0 : -ENOENT; 2282d24bec3aSMark Fasheh break; 2283d24bec3aSMark Fasheh } 2284d24bec3aSMark Fasheh ++found; 2285d24bec3aSMark Fasheh 2286d24bec3aSMark Fasheh slot = path->slots[0]; 22873fe81ce2SFilipe David Borba Manana eb = btrfs_clone_extent_buffer(path->nodes[0]); 22883fe81ce2SFilipe David Borba Manana if (!eb) { 22893fe81ce2SFilipe David Borba Manana ret = -ENOMEM; 22903fe81ce2SFilipe David Borba Manana break; 22913fe81ce2SFilipe David Borba Manana } 2292d24bec3aSMark Fasheh btrfs_release_path(path); 2293d24bec3aSMark Fasheh 22943212fa14SJosef Bacik item_size = btrfs_item_size(eb, slot); 22952849a854SChris Mason ptr = btrfs_item_ptr_offset(eb, slot); 2296d24bec3aSMark Fasheh cur_offset = 0; 2297d24bec3aSMark Fasheh 2298d24bec3aSMark Fasheh while (cur_offset < item_size) { 2299d24bec3aSMark Fasheh u32 name_len; 2300d24bec3aSMark Fasheh 2301d24bec3aSMark Fasheh extref = (struct btrfs_inode_extref *)(ptr + cur_offset); 2302d24bec3aSMark Fasheh parent = btrfs_inode_extref_parent(eb, extref); 2303d24bec3aSMark Fasheh name_len = btrfs_inode_extref_name_len(eb, extref); 2304ad6240f6SDavid Sterba ret = inode_to_path(parent, name_len, 2305875d1daaSDavid Sterba (unsigned long)&extref->name, eb, ipath); 2306d24bec3aSMark Fasheh if (ret) 2307d24bec3aSMark Fasheh break; 2308d24bec3aSMark Fasheh 23092849a854SChris Mason cur_offset += btrfs_inode_extref_name_len(eb, extref); 2310d24bec3aSMark Fasheh cur_offset += sizeof(*extref); 2311d24bec3aSMark Fasheh } 2312d24bec3aSMark Fasheh free_extent_buffer(eb); 2313d24bec3aSMark Fasheh 2314d24bec3aSMark Fasheh offset++; 2315d24bec3aSMark Fasheh } 2316d24bec3aSMark Fasheh 2317d24bec3aSMark Fasheh btrfs_release_path(path); 2318d24bec3aSMark Fasheh 2319d24bec3aSMark Fasheh return ret; 2320d24bec3aSMark Fasheh } 2321d24bec3aSMark Fasheh 2322a542ad1bSJan Schmidt /* 2323a542ad1bSJan Schmidt * returns 0 if the path could be dumped (probably truncated) 2324a542ad1bSJan Schmidt * returns <0 in case of an error 2325a542ad1bSJan Schmidt */ 2326d24bec3aSMark Fasheh static int inode_to_path(u64 inum, u32 name_len, unsigned long name_off, 2327875d1daaSDavid Sterba struct extent_buffer *eb, struct inode_fs_paths *ipath) 2328a542ad1bSJan Schmidt { 2329a542ad1bSJan Schmidt char *fspath; 2330a542ad1bSJan Schmidt char *fspath_min; 2331a542ad1bSJan Schmidt int i = ipath->fspath->elem_cnt; 2332a542ad1bSJan Schmidt const int s_ptr = sizeof(char *); 2333a542ad1bSJan Schmidt u32 bytes_left; 2334a542ad1bSJan Schmidt 2335a542ad1bSJan Schmidt bytes_left = ipath->fspath->bytes_left > s_ptr ? 2336a542ad1bSJan Schmidt ipath->fspath->bytes_left - s_ptr : 0; 2337a542ad1bSJan Schmidt 2338740c3d22SChris Mason fspath_min = (char *)ipath->fspath->val + (i + 1) * s_ptr; 233996b5bd77SJan Schmidt fspath = btrfs_ref_to_path(ipath->fs_root, ipath->btrfs_path, name_len, 234096b5bd77SJan Schmidt name_off, eb, inum, fspath_min, bytes_left); 2341a542ad1bSJan Schmidt if (IS_ERR(fspath)) 2342a542ad1bSJan Schmidt return PTR_ERR(fspath); 2343a542ad1bSJan Schmidt 2344a542ad1bSJan Schmidt if (fspath > fspath_min) { 2345745c4d8eSJeff Mahoney ipath->fspath->val[i] = (u64)(unsigned long)fspath; 2346a542ad1bSJan Schmidt ++ipath->fspath->elem_cnt; 2347a542ad1bSJan Schmidt ipath->fspath->bytes_left = fspath - fspath_min; 2348a542ad1bSJan Schmidt } else { 2349a542ad1bSJan Schmidt ++ipath->fspath->elem_missed; 2350a542ad1bSJan Schmidt ipath->fspath->bytes_missing += fspath_min - fspath; 2351a542ad1bSJan Schmidt ipath->fspath->bytes_left = 0; 2352a542ad1bSJan Schmidt } 2353a542ad1bSJan Schmidt 2354a542ad1bSJan Schmidt return 0; 2355a542ad1bSJan Schmidt } 2356a542ad1bSJan Schmidt 2357a542ad1bSJan Schmidt /* 2358a542ad1bSJan Schmidt * this dumps all file system paths to the inode into the ipath struct, provided 2359a542ad1bSJan Schmidt * is has been created large enough. each path is zero-terminated and accessed 2360740c3d22SChris Mason * from ipath->fspath->val[i]. 2361a542ad1bSJan Schmidt * when it returns, there are ipath->fspath->elem_cnt number of paths available 2362740c3d22SChris Mason * in ipath->fspath->val[]. when the allocated space wasn't sufficient, the 236301327610SNicholas D Steeves * number of missed paths is recorded in ipath->fspath->elem_missed, otherwise, 2364a542ad1bSJan Schmidt * it's zero. ipath->fspath->bytes_missing holds the number of bytes that would 2365a542ad1bSJan Schmidt * have been needed to return all paths. 2366a542ad1bSJan Schmidt */ 2367a542ad1bSJan Schmidt int paths_from_inode(u64 inum, struct inode_fs_paths *ipath) 2368a542ad1bSJan Schmidt { 2369ad6240f6SDavid Sterba int ret; 2370ad6240f6SDavid Sterba int found_refs = 0; 2371ad6240f6SDavid Sterba 2372875d1daaSDavid Sterba ret = iterate_inode_refs(inum, ipath); 2373ad6240f6SDavid Sterba if (!ret) 2374ad6240f6SDavid Sterba ++found_refs; 2375ad6240f6SDavid Sterba else if (ret != -ENOENT) 2376ad6240f6SDavid Sterba return ret; 2377ad6240f6SDavid Sterba 2378875d1daaSDavid Sterba ret = iterate_inode_extrefs(inum, ipath); 2379ad6240f6SDavid Sterba if (ret == -ENOENT && found_refs) 2380ad6240f6SDavid Sterba return 0; 2381ad6240f6SDavid Sterba 2382ad6240f6SDavid Sterba return ret; 2383a542ad1bSJan Schmidt } 2384a542ad1bSJan Schmidt 2385a542ad1bSJan Schmidt struct btrfs_data_container *init_data_container(u32 total_bytes) 2386a542ad1bSJan Schmidt { 2387a542ad1bSJan Schmidt struct btrfs_data_container *data; 2388a542ad1bSJan Schmidt size_t alloc_bytes; 2389a542ad1bSJan Schmidt 2390a542ad1bSJan Schmidt alloc_bytes = max_t(size_t, total_bytes, sizeof(*data)); 2391f54de068SDavid Sterba data = kvmalloc(alloc_bytes, GFP_KERNEL); 2392a542ad1bSJan Schmidt if (!data) 2393a542ad1bSJan Schmidt return ERR_PTR(-ENOMEM); 2394a542ad1bSJan Schmidt 2395a542ad1bSJan Schmidt if (total_bytes >= sizeof(*data)) { 2396a542ad1bSJan Schmidt data->bytes_left = total_bytes - sizeof(*data); 2397a542ad1bSJan Schmidt data->bytes_missing = 0; 2398a542ad1bSJan Schmidt } else { 2399a542ad1bSJan Schmidt data->bytes_missing = sizeof(*data) - total_bytes; 2400a542ad1bSJan Schmidt data->bytes_left = 0; 2401a542ad1bSJan Schmidt } 2402a542ad1bSJan Schmidt 2403a542ad1bSJan Schmidt data->elem_cnt = 0; 2404a542ad1bSJan Schmidt data->elem_missed = 0; 2405a542ad1bSJan Schmidt 2406a542ad1bSJan Schmidt return data; 2407a542ad1bSJan Schmidt } 2408a542ad1bSJan Schmidt 2409a542ad1bSJan Schmidt /* 2410a542ad1bSJan Schmidt * allocates space to return multiple file system paths for an inode. 2411a542ad1bSJan Schmidt * total_bytes to allocate are passed, note that space usable for actual path 2412a542ad1bSJan Schmidt * information will be total_bytes - sizeof(struct inode_fs_paths). 2413a542ad1bSJan Schmidt * the returned pointer must be freed with free_ipath() in the end. 2414a542ad1bSJan Schmidt */ 2415a542ad1bSJan Schmidt struct inode_fs_paths *init_ipath(s32 total_bytes, struct btrfs_root *fs_root, 2416a542ad1bSJan Schmidt struct btrfs_path *path) 2417a542ad1bSJan Schmidt { 2418a542ad1bSJan Schmidt struct inode_fs_paths *ifp; 2419a542ad1bSJan Schmidt struct btrfs_data_container *fspath; 2420a542ad1bSJan Schmidt 2421a542ad1bSJan Schmidt fspath = init_data_container(total_bytes); 2422a542ad1bSJan Schmidt if (IS_ERR(fspath)) 2423afc6961fSMisono Tomohiro return ERR_CAST(fspath); 2424a542ad1bSJan Schmidt 2425f54de068SDavid Sterba ifp = kmalloc(sizeof(*ifp), GFP_KERNEL); 2426a542ad1bSJan Schmidt if (!ifp) { 2427f54de068SDavid Sterba kvfree(fspath); 2428a542ad1bSJan Schmidt return ERR_PTR(-ENOMEM); 2429a542ad1bSJan Schmidt } 2430a542ad1bSJan Schmidt 2431a542ad1bSJan Schmidt ifp->btrfs_path = path; 2432a542ad1bSJan Schmidt ifp->fspath = fspath; 2433a542ad1bSJan Schmidt ifp->fs_root = fs_root; 2434a542ad1bSJan Schmidt 2435a542ad1bSJan Schmidt return ifp; 2436a542ad1bSJan Schmidt } 2437a542ad1bSJan Schmidt 2438a542ad1bSJan Schmidt void free_ipath(struct inode_fs_paths *ipath) 2439a542ad1bSJan Schmidt { 24404735fb28SJesper Juhl if (!ipath) 24414735fb28SJesper Juhl return; 2442f54de068SDavid Sterba kvfree(ipath->fspath); 2443a542ad1bSJan Schmidt kfree(ipath); 2444a542ad1bSJan Schmidt } 2445a37f232bSQu Wenruo 2446a37f232bSQu Wenruo struct btrfs_backref_iter *btrfs_backref_iter_alloc( 2447a37f232bSQu Wenruo struct btrfs_fs_info *fs_info, gfp_t gfp_flag) 2448a37f232bSQu Wenruo { 2449a37f232bSQu Wenruo struct btrfs_backref_iter *ret; 2450a37f232bSQu Wenruo 2451a37f232bSQu Wenruo ret = kzalloc(sizeof(*ret), gfp_flag); 2452a37f232bSQu Wenruo if (!ret) 2453a37f232bSQu Wenruo return NULL; 2454a37f232bSQu Wenruo 2455a37f232bSQu Wenruo ret->path = btrfs_alloc_path(); 2456c15c2ec0SBoleyn Su if (!ret->path) { 2457a37f232bSQu Wenruo kfree(ret); 2458a37f232bSQu Wenruo return NULL; 2459a37f232bSQu Wenruo } 2460a37f232bSQu Wenruo 2461a37f232bSQu Wenruo /* Current backref iterator only supports iteration in commit root */ 2462a37f232bSQu Wenruo ret->path->search_commit_root = 1; 2463a37f232bSQu Wenruo ret->path->skip_locking = 1; 2464a37f232bSQu Wenruo ret->fs_info = fs_info; 2465a37f232bSQu Wenruo 2466a37f232bSQu Wenruo return ret; 2467a37f232bSQu Wenruo } 2468a37f232bSQu Wenruo 2469a37f232bSQu Wenruo int btrfs_backref_iter_start(struct btrfs_backref_iter *iter, u64 bytenr) 2470a37f232bSQu Wenruo { 2471a37f232bSQu Wenruo struct btrfs_fs_info *fs_info = iter->fs_info; 247229cbcf40SJosef Bacik struct btrfs_root *extent_root = btrfs_extent_root(fs_info, bytenr); 2473a37f232bSQu Wenruo struct btrfs_path *path = iter->path; 2474a37f232bSQu Wenruo struct btrfs_extent_item *ei; 2475a37f232bSQu Wenruo struct btrfs_key key; 2476a37f232bSQu Wenruo int ret; 2477a37f232bSQu Wenruo 2478a37f232bSQu Wenruo key.objectid = bytenr; 2479a37f232bSQu Wenruo key.type = BTRFS_METADATA_ITEM_KEY; 2480a37f232bSQu Wenruo key.offset = (u64)-1; 2481a37f232bSQu Wenruo iter->bytenr = bytenr; 2482a37f232bSQu Wenruo 248329cbcf40SJosef Bacik ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0); 2484a37f232bSQu Wenruo if (ret < 0) 2485a37f232bSQu Wenruo return ret; 2486a37f232bSQu Wenruo if (ret == 0) { 2487a37f232bSQu Wenruo ret = -EUCLEAN; 2488a37f232bSQu Wenruo goto release; 2489a37f232bSQu Wenruo } 2490a37f232bSQu Wenruo if (path->slots[0] == 0) { 2491a37f232bSQu Wenruo WARN_ON(IS_ENABLED(CONFIG_BTRFS_DEBUG)); 2492a37f232bSQu Wenruo ret = -EUCLEAN; 2493a37f232bSQu Wenruo goto release; 2494a37f232bSQu Wenruo } 2495a37f232bSQu Wenruo path->slots[0]--; 2496a37f232bSQu Wenruo 2497a37f232bSQu Wenruo btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); 2498a37f232bSQu Wenruo if ((key.type != BTRFS_EXTENT_ITEM_KEY && 2499a37f232bSQu Wenruo key.type != BTRFS_METADATA_ITEM_KEY) || key.objectid != bytenr) { 2500a37f232bSQu Wenruo ret = -ENOENT; 2501a37f232bSQu Wenruo goto release; 2502a37f232bSQu Wenruo } 2503a37f232bSQu Wenruo memcpy(&iter->cur_key, &key, sizeof(key)); 2504a37f232bSQu Wenruo iter->item_ptr = (u32)btrfs_item_ptr_offset(path->nodes[0], 2505a37f232bSQu Wenruo path->slots[0]); 2506a37f232bSQu Wenruo iter->end_ptr = (u32)(iter->item_ptr + 25073212fa14SJosef Bacik btrfs_item_size(path->nodes[0], path->slots[0])); 2508a37f232bSQu Wenruo ei = btrfs_item_ptr(path->nodes[0], path->slots[0], 2509a37f232bSQu Wenruo struct btrfs_extent_item); 2510a37f232bSQu Wenruo 2511a37f232bSQu Wenruo /* 2512a37f232bSQu Wenruo * Only support iteration on tree backref yet. 2513a37f232bSQu Wenruo * 2514a37f232bSQu Wenruo * This is an extra precaution for non skinny-metadata, where 2515a37f232bSQu Wenruo * EXTENT_ITEM is also used for tree blocks, that we can only use 2516a37f232bSQu Wenruo * extent flags to determine if it's a tree block. 2517a37f232bSQu Wenruo */ 2518a37f232bSQu Wenruo if (btrfs_extent_flags(path->nodes[0], ei) & BTRFS_EXTENT_FLAG_DATA) { 2519a37f232bSQu Wenruo ret = -ENOTSUPP; 2520a37f232bSQu Wenruo goto release; 2521a37f232bSQu Wenruo } 2522a37f232bSQu Wenruo iter->cur_ptr = (u32)(iter->item_ptr + sizeof(*ei)); 2523a37f232bSQu Wenruo 2524a37f232bSQu Wenruo /* If there is no inline backref, go search for keyed backref */ 2525a37f232bSQu Wenruo if (iter->cur_ptr >= iter->end_ptr) { 252629cbcf40SJosef Bacik ret = btrfs_next_item(extent_root, path); 2527a37f232bSQu Wenruo 2528a37f232bSQu Wenruo /* No inline nor keyed ref */ 2529a37f232bSQu Wenruo if (ret > 0) { 2530a37f232bSQu Wenruo ret = -ENOENT; 2531a37f232bSQu Wenruo goto release; 2532a37f232bSQu Wenruo } 2533a37f232bSQu Wenruo if (ret < 0) 2534a37f232bSQu Wenruo goto release; 2535a37f232bSQu Wenruo 2536a37f232bSQu Wenruo btrfs_item_key_to_cpu(path->nodes[0], &iter->cur_key, 2537a37f232bSQu Wenruo path->slots[0]); 2538a37f232bSQu Wenruo if (iter->cur_key.objectid != bytenr || 2539a37f232bSQu Wenruo (iter->cur_key.type != BTRFS_SHARED_BLOCK_REF_KEY && 2540a37f232bSQu Wenruo iter->cur_key.type != BTRFS_TREE_BLOCK_REF_KEY)) { 2541a37f232bSQu Wenruo ret = -ENOENT; 2542a37f232bSQu Wenruo goto release; 2543a37f232bSQu Wenruo } 2544a37f232bSQu Wenruo iter->cur_ptr = (u32)btrfs_item_ptr_offset(path->nodes[0], 2545a37f232bSQu Wenruo path->slots[0]); 2546a37f232bSQu Wenruo iter->item_ptr = iter->cur_ptr; 25473212fa14SJosef Bacik iter->end_ptr = (u32)(iter->item_ptr + btrfs_item_size( 2548a37f232bSQu Wenruo path->nodes[0], path->slots[0])); 2549a37f232bSQu Wenruo } 2550a37f232bSQu Wenruo 2551a37f232bSQu Wenruo return 0; 2552a37f232bSQu Wenruo release: 2553a37f232bSQu Wenruo btrfs_backref_iter_release(iter); 2554a37f232bSQu Wenruo return ret; 2555a37f232bSQu Wenruo } 2556c39c2ddcSQu Wenruo 2557c39c2ddcSQu Wenruo /* 2558c39c2ddcSQu Wenruo * Go to the next backref item of current bytenr, can be either inlined or 2559c39c2ddcSQu Wenruo * keyed. 2560c39c2ddcSQu Wenruo * 2561c39c2ddcSQu Wenruo * Caller needs to check whether it's inline ref or not by iter->cur_key. 2562c39c2ddcSQu Wenruo * 2563c39c2ddcSQu Wenruo * Return 0 if we get next backref without problem. 2564c39c2ddcSQu Wenruo * Return >0 if there is no extra backref for this bytenr. 2565c39c2ddcSQu Wenruo * Return <0 if there is something wrong happened. 2566c39c2ddcSQu Wenruo */ 2567c39c2ddcSQu Wenruo int btrfs_backref_iter_next(struct btrfs_backref_iter *iter) 2568c39c2ddcSQu Wenruo { 2569c39c2ddcSQu Wenruo struct extent_buffer *eb = btrfs_backref_get_eb(iter); 257029cbcf40SJosef Bacik struct btrfs_root *extent_root; 2571c39c2ddcSQu Wenruo struct btrfs_path *path = iter->path; 2572c39c2ddcSQu Wenruo struct btrfs_extent_inline_ref *iref; 2573c39c2ddcSQu Wenruo int ret; 2574c39c2ddcSQu Wenruo u32 size; 2575c39c2ddcSQu Wenruo 2576c39c2ddcSQu Wenruo if (btrfs_backref_iter_is_inline_ref(iter)) { 2577c39c2ddcSQu Wenruo /* We're still inside the inline refs */ 2578c39c2ddcSQu Wenruo ASSERT(iter->cur_ptr < iter->end_ptr); 2579c39c2ddcSQu Wenruo 2580c39c2ddcSQu Wenruo if (btrfs_backref_has_tree_block_info(iter)) { 2581c39c2ddcSQu Wenruo /* First tree block info */ 2582c39c2ddcSQu Wenruo size = sizeof(struct btrfs_tree_block_info); 2583c39c2ddcSQu Wenruo } else { 2584c39c2ddcSQu Wenruo /* Use inline ref type to determine the size */ 2585c39c2ddcSQu Wenruo int type; 2586c39c2ddcSQu Wenruo 2587c39c2ddcSQu Wenruo iref = (struct btrfs_extent_inline_ref *) 2588c39c2ddcSQu Wenruo ((unsigned long)iter->cur_ptr); 2589c39c2ddcSQu Wenruo type = btrfs_extent_inline_ref_type(eb, iref); 2590c39c2ddcSQu Wenruo 2591c39c2ddcSQu Wenruo size = btrfs_extent_inline_ref_size(type); 2592c39c2ddcSQu Wenruo } 2593c39c2ddcSQu Wenruo iter->cur_ptr += size; 2594c39c2ddcSQu Wenruo if (iter->cur_ptr < iter->end_ptr) 2595c39c2ddcSQu Wenruo return 0; 2596c39c2ddcSQu Wenruo 2597c39c2ddcSQu Wenruo /* All inline items iterated, fall through */ 2598c39c2ddcSQu Wenruo } 2599c39c2ddcSQu Wenruo 2600c39c2ddcSQu Wenruo /* We're at keyed items, there is no inline item, go to the next one */ 260129cbcf40SJosef Bacik extent_root = btrfs_extent_root(iter->fs_info, iter->bytenr); 260229cbcf40SJosef Bacik ret = btrfs_next_item(extent_root, iter->path); 2603c39c2ddcSQu Wenruo if (ret) 2604c39c2ddcSQu Wenruo return ret; 2605c39c2ddcSQu Wenruo 2606c39c2ddcSQu Wenruo btrfs_item_key_to_cpu(path->nodes[0], &iter->cur_key, path->slots[0]); 2607c39c2ddcSQu Wenruo if (iter->cur_key.objectid != iter->bytenr || 2608c39c2ddcSQu Wenruo (iter->cur_key.type != BTRFS_TREE_BLOCK_REF_KEY && 2609c39c2ddcSQu Wenruo iter->cur_key.type != BTRFS_SHARED_BLOCK_REF_KEY)) 2610c39c2ddcSQu Wenruo return 1; 2611c39c2ddcSQu Wenruo iter->item_ptr = (u32)btrfs_item_ptr_offset(path->nodes[0], 2612c39c2ddcSQu Wenruo path->slots[0]); 2613c39c2ddcSQu Wenruo iter->cur_ptr = iter->item_ptr; 26143212fa14SJosef Bacik iter->end_ptr = iter->item_ptr + (u32)btrfs_item_size(path->nodes[0], 2615c39c2ddcSQu Wenruo path->slots[0]); 2616c39c2ddcSQu Wenruo return 0; 2617c39c2ddcSQu Wenruo } 2618584fb121SQu Wenruo 2619584fb121SQu Wenruo void btrfs_backref_init_cache(struct btrfs_fs_info *fs_info, 2620584fb121SQu Wenruo struct btrfs_backref_cache *cache, int is_reloc) 2621584fb121SQu Wenruo { 2622584fb121SQu Wenruo int i; 2623584fb121SQu Wenruo 2624584fb121SQu Wenruo cache->rb_root = RB_ROOT; 2625584fb121SQu Wenruo for (i = 0; i < BTRFS_MAX_LEVEL; i++) 2626584fb121SQu Wenruo INIT_LIST_HEAD(&cache->pending[i]); 2627584fb121SQu Wenruo INIT_LIST_HEAD(&cache->changed); 2628584fb121SQu Wenruo INIT_LIST_HEAD(&cache->detached); 2629584fb121SQu Wenruo INIT_LIST_HEAD(&cache->leaves); 2630584fb121SQu Wenruo INIT_LIST_HEAD(&cache->pending_edge); 2631584fb121SQu Wenruo INIT_LIST_HEAD(&cache->useless_node); 2632584fb121SQu Wenruo cache->fs_info = fs_info; 2633584fb121SQu Wenruo cache->is_reloc = is_reloc; 2634584fb121SQu Wenruo } 2635b1818dabSQu Wenruo 2636b1818dabSQu Wenruo struct btrfs_backref_node *btrfs_backref_alloc_node( 2637b1818dabSQu Wenruo struct btrfs_backref_cache *cache, u64 bytenr, int level) 2638b1818dabSQu Wenruo { 2639b1818dabSQu Wenruo struct btrfs_backref_node *node; 2640b1818dabSQu Wenruo 2641b1818dabSQu Wenruo ASSERT(level >= 0 && level < BTRFS_MAX_LEVEL); 2642b1818dabSQu Wenruo node = kzalloc(sizeof(*node), GFP_NOFS); 2643b1818dabSQu Wenruo if (!node) 2644b1818dabSQu Wenruo return node; 2645b1818dabSQu Wenruo 2646b1818dabSQu Wenruo INIT_LIST_HEAD(&node->list); 2647b1818dabSQu Wenruo INIT_LIST_HEAD(&node->upper); 2648b1818dabSQu Wenruo INIT_LIST_HEAD(&node->lower); 2649b1818dabSQu Wenruo RB_CLEAR_NODE(&node->rb_node); 2650b1818dabSQu Wenruo cache->nr_nodes++; 2651b1818dabSQu Wenruo node->level = level; 2652b1818dabSQu Wenruo node->bytenr = bytenr; 2653b1818dabSQu Wenruo 2654b1818dabSQu Wenruo return node; 2655b1818dabSQu Wenruo } 265647254d07SQu Wenruo 265747254d07SQu Wenruo struct btrfs_backref_edge *btrfs_backref_alloc_edge( 265847254d07SQu Wenruo struct btrfs_backref_cache *cache) 265947254d07SQu Wenruo { 266047254d07SQu Wenruo struct btrfs_backref_edge *edge; 266147254d07SQu Wenruo 266247254d07SQu Wenruo edge = kzalloc(sizeof(*edge), GFP_NOFS); 266347254d07SQu Wenruo if (edge) 266447254d07SQu Wenruo cache->nr_edges++; 266547254d07SQu Wenruo return edge; 266647254d07SQu Wenruo } 2667023acb07SQu Wenruo 2668023acb07SQu Wenruo /* 2669023acb07SQu Wenruo * Drop the backref node from cache, also cleaning up all its 2670023acb07SQu Wenruo * upper edges and any uncached nodes in the path. 2671023acb07SQu Wenruo * 2672023acb07SQu Wenruo * This cleanup happens bottom up, thus the node should either 2673023acb07SQu Wenruo * be the lowest node in the cache or a detached node. 2674023acb07SQu Wenruo */ 2675023acb07SQu Wenruo void btrfs_backref_cleanup_node(struct btrfs_backref_cache *cache, 2676023acb07SQu Wenruo struct btrfs_backref_node *node) 2677023acb07SQu Wenruo { 2678023acb07SQu Wenruo struct btrfs_backref_node *upper; 2679023acb07SQu Wenruo struct btrfs_backref_edge *edge; 2680023acb07SQu Wenruo 2681023acb07SQu Wenruo if (!node) 2682023acb07SQu Wenruo return; 2683023acb07SQu Wenruo 2684023acb07SQu Wenruo BUG_ON(!node->lowest && !node->detached); 2685023acb07SQu Wenruo while (!list_empty(&node->upper)) { 2686023acb07SQu Wenruo edge = list_entry(node->upper.next, struct btrfs_backref_edge, 2687023acb07SQu Wenruo list[LOWER]); 2688023acb07SQu Wenruo upper = edge->node[UPPER]; 2689023acb07SQu Wenruo list_del(&edge->list[LOWER]); 2690023acb07SQu Wenruo list_del(&edge->list[UPPER]); 2691023acb07SQu Wenruo btrfs_backref_free_edge(cache, edge); 2692023acb07SQu Wenruo 2693023acb07SQu Wenruo /* 2694023acb07SQu Wenruo * Add the node to leaf node list if no other child block 2695023acb07SQu Wenruo * cached. 2696023acb07SQu Wenruo */ 2697023acb07SQu Wenruo if (list_empty(&upper->lower)) { 2698023acb07SQu Wenruo list_add_tail(&upper->lower, &cache->leaves); 2699023acb07SQu Wenruo upper->lowest = 1; 2700023acb07SQu Wenruo } 2701023acb07SQu Wenruo } 2702023acb07SQu Wenruo 2703023acb07SQu Wenruo btrfs_backref_drop_node(cache, node); 2704023acb07SQu Wenruo } 270513fe1bdbSQu Wenruo 270613fe1bdbSQu Wenruo /* 270713fe1bdbSQu Wenruo * Release all nodes/edges from current cache 270813fe1bdbSQu Wenruo */ 270913fe1bdbSQu Wenruo void btrfs_backref_release_cache(struct btrfs_backref_cache *cache) 271013fe1bdbSQu Wenruo { 271113fe1bdbSQu Wenruo struct btrfs_backref_node *node; 271213fe1bdbSQu Wenruo int i; 271313fe1bdbSQu Wenruo 271413fe1bdbSQu Wenruo while (!list_empty(&cache->detached)) { 271513fe1bdbSQu Wenruo node = list_entry(cache->detached.next, 271613fe1bdbSQu Wenruo struct btrfs_backref_node, list); 271713fe1bdbSQu Wenruo btrfs_backref_cleanup_node(cache, node); 271813fe1bdbSQu Wenruo } 271913fe1bdbSQu Wenruo 272013fe1bdbSQu Wenruo while (!list_empty(&cache->leaves)) { 272113fe1bdbSQu Wenruo node = list_entry(cache->leaves.next, 272213fe1bdbSQu Wenruo struct btrfs_backref_node, lower); 272313fe1bdbSQu Wenruo btrfs_backref_cleanup_node(cache, node); 272413fe1bdbSQu Wenruo } 272513fe1bdbSQu Wenruo 272613fe1bdbSQu Wenruo cache->last_trans = 0; 272713fe1bdbSQu Wenruo 272813fe1bdbSQu Wenruo for (i = 0; i < BTRFS_MAX_LEVEL; i++) 272913fe1bdbSQu Wenruo ASSERT(list_empty(&cache->pending[i])); 273013fe1bdbSQu Wenruo ASSERT(list_empty(&cache->pending_edge)); 273113fe1bdbSQu Wenruo ASSERT(list_empty(&cache->useless_node)); 273213fe1bdbSQu Wenruo ASSERT(list_empty(&cache->changed)); 273313fe1bdbSQu Wenruo ASSERT(list_empty(&cache->detached)); 273413fe1bdbSQu Wenruo ASSERT(RB_EMPTY_ROOT(&cache->rb_root)); 273513fe1bdbSQu Wenruo ASSERT(!cache->nr_nodes); 273613fe1bdbSQu Wenruo ASSERT(!cache->nr_edges); 273713fe1bdbSQu Wenruo } 27381b60d2ecSQu Wenruo 27391b60d2ecSQu Wenruo /* 27401b60d2ecSQu Wenruo * Handle direct tree backref 27411b60d2ecSQu Wenruo * 27421b60d2ecSQu Wenruo * Direct tree backref means, the backref item shows its parent bytenr 27431b60d2ecSQu Wenruo * directly. This is for SHARED_BLOCK_REF backref (keyed or inlined). 27441b60d2ecSQu Wenruo * 27451b60d2ecSQu Wenruo * @ref_key: The converted backref key. 27461b60d2ecSQu Wenruo * For keyed backref, it's the item key. 27471b60d2ecSQu Wenruo * For inlined backref, objectid is the bytenr, 27481b60d2ecSQu Wenruo * type is btrfs_inline_ref_type, offset is 27491b60d2ecSQu Wenruo * btrfs_inline_ref_offset. 27501b60d2ecSQu Wenruo */ 27511b60d2ecSQu Wenruo static int handle_direct_tree_backref(struct btrfs_backref_cache *cache, 27521b60d2ecSQu Wenruo struct btrfs_key *ref_key, 27531b60d2ecSQu Wenruo struct btrfs_backref_node *cur) 27541b60d2ecSQu Wenruo { 27551b60d2ecSQu Wenruo struct btrfs_backref_edge *edge; 27561b60d2ecSQu Wenruo struct btrfs_backref_node *upper; 27571b60d2ecSQu Wenruo struct rb_node *rb_node; 27581b60d2ecSQu Wenruo 27591b60d2ecSQu Wenruo ASSERT(ref_key->type == BTRFS_SHARED_BLOCK_REF_KEY); 27601b60d2ecSQu Wenruo 27611b60d2ecSQu Wenruo /* Only reloc root uses backref pointing to itself */ 27621b60d2ecSQu Wenruo if (ref_key->objectid == ref_key->offset) { 27631b60d2ecSQu Wenruo struct btrfs_root *root; 27641b60d2ecSQu Wenruo 27651b60d2ecSQu Wenruo cur->is_reloc_root = 1; 27661b60d2ecSQu Wenruo /* Only reloc backref cache cares about a specific root */ 27671b60d2ecSQu Wenruo if (cache->is_reloc) { 27681b60d2ecSQu Wenruo root = find_reloc_root(cache->fs_info, cur->bytenr); 2769f78743fbSJosef Bacik if (!root) 27701b60d2ecSQu Wenruo return -ENOENT; 27711b60d2ecSQu Wenruo cur->root = root; 27721b60d2ecSQu Wenruo } else { 27731b60d2ecSQu Wenruo /* 27741b60d2ecSQu Wenruo * For generic purpose backref cache, reloc root node 27751b60d2ecSQu Wenruo * is useless. 27761b60d2ecSQu Wenruo */ 27771b60d2ecSQu Wenruo list_add(&cur->list, &cache->useless_node); 27781b60d2ecSQu Wenruo } 27791b60d2ecSQu Wenruo return 0; 27801b60d2ecSQu Wenruo } 27811b60d2ecSQu Wenruo 27821b60d2ecSQu Wenruo edge = btrfs_backref_alloc_edge(cache); 27831b60d2ecSQu Wenruo if (!edge) 27841b60d2ecSQu Wenruo return -ENOMEM; 27851b60d2ecSQu Wenruo 27861b60d2ecSQu Wenruo rb_node = rb_simple_search(&cache->rb_root, ref_key->offset); 27871b60d2ecSQu Wenruo if (!rb_node) { 27881b60d2ecSQu Wenruo /* Parent node not yet cached */ 27891b60d2ecSQu Wenruo upper = btrfs_backref_alloc_node(cache, ref_key->offset, 27901b60d2ecSQu Wenruo cur->level + 1); 27911b60d2ecSQu Wenruo if (!upper) { 27921b60d2ecSQu Wenruo btrfs_backref_free_edge(cache, edge); 27931b60d2ecSQu Wenruo return -ENOMEM; 27941b60d2ecSQu Wenruo } 27951b60d2ecSQu Wenruo 27961b60d2ecSQu Wenruo /* 27971b60d2ecSQu Wenruo * Backrefs for the upper level block isn't cached, add the 27981b60d2ecSQu Wenruo * block to pending list 27991b60d2ecSQu Wenruo */ 28001b60d2ecSQu Wenruo list_add_tail(&edge->list[UPPER], &cache->pending_edge); 28011b60d2ecSQu Wenruo } else { 28021b60d2ecSQu Wenruo /* Parent node already cached */ 28031b60d2ecSQu Wenruo upper = rb_entry(rb_node, struct btrfs_backref_node, rb_node); 28041b60d2ecSQu Wenruo ASSERT(upper->checked); 28051b60d2ecSQu Wenruo INIT_LIST_HEAD(&edge->list[UPPER]); 28061b60d2ecSQu Wenruo } 28071b60d2ecSQu Wenruo btrfs_backref_link_edge(edge, cur, upper, LINK_LOWER); 28081b60d2ecSQu Wenruo return 0; 28091b60d2ecSQu Wenruo } 28101b60d2ecSQu Wenruo 28111b60d2ecSQu Wenruo /* 28121b60d2ecSQu Wenruo * Handle indirect tree backref 28131b60d2ecSQu Wenruo * 28141b60d2ecSQu Wenruo * Indirect tree backref means, we only know which tree the node belongs to. 28151b60d2ecSQu Wenruo * We still need to do a tree search to find out the parents. This is for 28161b60d2ecSQu Wenruo * TREE_BLOCK_REF backref (keyed or inlined). 28171b60d2ecSQu Wenruo * 28181b60d2ecSQu Wenruo * @ref_key: The same as @ref_key in handle_direct_tree_backref() 28191b60d2ecSQu Wenruo * @tree_key: The first key of this tree block. 28201b60d2ecSQu Wenruo * @path: A clean (released) path, to avoid allocating path every time 28211b60d2ecSQu Wenruo * the function get called. 28221b60d2ecSQu Wenruo */ 28231b60d2ecSQu Wenruo static int handle_indirect_tree_backref(struct btrfs_backref_cache *cache, 28241b60d2ecSQu Wenruo struct btrfs_path *path, 28251b60d2ecSQu Wenruo struct btrfs_key *ref_key, 28261b60d2ecSQu Wenruo struct btrfs_key *tree_key, 28271b60d2ecSQu Wenruo struct btrfs_backref_node *cur) 28281b60d2ecSQu Wenruo { 28291b60d2ecSQu Wenruo struct btrfs_fs_info *fs_info = cache->fs_info; 28301b60d2ecSQu Wenruo struct btrfs_backref_node *upper; 28311b60d2ecSQu Wenruo struct btrfs_backref_node *lower; 28321b60d2ecSQu Wenruo struct btrfs_backref_edge *edge; 28331b60d2ecSQu Wenruo struct extent_buffer *eb; 28341b60d2ecSQu Wenruo struct btrfs_root *root; 28351b60d2ecSQu Wenruo struct rb_node *rb_node; 28361b60d2ecSQu Wenruo int level; 28371b60d2ecSQu Wenruo bool need_check = true; 28381b60d2ecSQu Wenruo int ret; 28391b60d2ecSQu Wenruo 284056e9357aSDavid Sterba root = btrfs_get_fs_root(fs_info, ref_key->offset, false); 28411b60d2ecSQu Wenruo if (IS_ERR(root)) 28421b60d2ecSQu Wenruo return PTR_ERR(root); 284392a7cc42SQu Wenruo if (!test_bit(BTRFS_ROOT_SHAREABLE, &root->state)) 28441b60d2ecSQu Wenruo cur->cowonly = 1; 28451b60d2ecSQu Wenruo 28461b60d2ecSQu Wenruo if (btrfs_root_level(&root->root_item) == cur->level) { 28471b60d2ecSQu Wenruo /* Tree root */ 28481b60d2ecSQu Wenruo ASSERT(btrfs_root_bytenr(&root->root_item) == cur->bytenr); 2849876de781SQu Wenruo /* 2850876de781SQu Wenruo * For reloc backref cache, we may ignore reloc root. But for 2851876de781SQu Wenruo * general purpose backref cache, we can't rely on 2852876de781SQu Wenruo * btrfs_should_ignore_reloc_root() as it may conflict with 2853876de781SQu Wenruo * current running relocation and lead to missing root. 2854876de781SQu Wenruo * 2855876de781SQu Wenruo * For general purpose backref cache, reloc root detection is 2856876de781SQu Wenruo * completely relying on direct backref (key->offset is parent 2857876de781SQu Wenruo * bytenr), thus only do such check for reloc cache. 2858876de781SQu Wenruo */ 2859876de781SQu Wenruo if (btrfs_should_ignore_reloc_root(root) && cache->is_reloc) { 28601b60d2ecSQu Wenruo btrfs_put_root(root); 28611b60d2ecSQu Wenruo list_add(&cur->list, &cache->useless_node); 28621b60d2ecSQu Wenruo } else { 28631b60d2ecSQu Wenruo cur->root = root; 28641b60d2ecSQu Wenruo } 28651b60d2ecSQu Wenruo return 0; 28661b60d2ecSQu Wenruo } 28671b60d2ecSQu Wenruo 28681b60d2ecSQu Wenruo level = cur->level + 1; 28691b60d2ecSQu Wenruo 28701b60d2ecSQu Wenruo /* Search the tree to find parent blocks referring to the block */ 28711b60d2ecSQu Wenruo path->search_commit_root = 1; 28721b60d2ecSQu Wenruo path->skip_locking = 1; 28731b60d2ecSQu Wenruo path->lowest_level = level; 28741b60d2ecSQu Wenruo ret = btrfs_search_slot(NULL, root, tree_key, path, 0, 0); 28751b60d2ecSQu Wenruo path->lowest_level = 0; 28761b60d2ecSQu Wenruo if (ret < 0) { 28771b60d2ecSQu Wenruo btrfs_put_root(root); 28781b60d2ecSQu Wenruo return ret; 28791b60d2ecSQu Wenruo } 28801b60d2ecSQu Wenruo if (ret > 0 && path->slots[level] > 0) 28811b60d2ecSQu Wenruo path->slots[level]--; 28821b60d2ecSQu Wenruo 28831b60d2ecSQu Wenruo eb = path->nodes[level]; 28841b60d2ecSQu Wenruo if (btrfs_node_blockptr(eb, path->slots[level]) != cur->bytenr) { 28851b60d2ecSQu Wenruo btrfs_err(fs_info, 28861b60d2ecSQu Wenruo "couldn't find block (%llu) (level %d) in tree (%llu) with key (%llu %u %llu)", 28871b60d2ecSQu Wenruo cur->bytenr, level - 1, root->root_key.objectid, 28881b60d2ecSQu Wenruo tree_key->objectid, tree_key->type, tree_key->offset); 28891b60d2ecSQu Wenruo btrfs_put_root(root); 28901b60d2ecSQu Wenruo ret = -ENOENT; 28911b60d2ecSQu Wenruo goto out; 28921b60d2ecSQu Wenruo } 28931b60d2ecSQu Wenruo lower = cur; 28941b60d2ecSQu Wenruo 28951b60d2ecSQu Wenruo /* Add all nodes and edges in the path */ 28961b60d2ecSQu Wenruo for (; level < BTRFS_MAX_LEVEL; level++) { 28971b60d2ecSQu Wenruo if (!path->nodes[level]) { 28981b60d2ecSQu Wenruo ASSERT(btrfs_root_bytenr(&root->root_item) == 28991b60d2ecSQu Wenruo lower->bytenr); 2900876de781SQu Wenruo /* Same as previous should_ignore_reloc_root() call */ 2901876de781SQu Wenruo if (btrfs_should_ignore_reloc_root(root) && 2902876de781SQu Wenruo cache->is_reloc) { 29031b60d2ecSQu Wenruo btrfs_put_root(root); 29041b60d2ecSQu Wenruo list_add(&lower->list, &cache->useless_node); 29051b60d2ecSQu Wenruo } else { 29061b60d2ecSQu Wenruo lower->root = root; 29071b60d2ecSQu Wenruo } 29081b60d2ecSQu Wenruo break; 29091b60d2ecSQu Wenruo } 29101b60d2ecSQu Wenruo 29111b60d2ecSQu Wenruo edge = btrfs_backref_alloc_edge(cache); 29121b60d2ecSQu Wenruo if (!edge) { 29131b60d2ecSQu Wenruo btrfs_put_root(root); 29141b60d2ecSQu Wenruo ret = -ENOMEM; 29151b60d2ecSQu Wenruo goto out; 29161b60d2ecSQu Wenruo } 29171b60d2ecSQu Wenruo 29181b60d2ecSQu Wenruo eb = path->nodes[level]; 29191b60d2ecSQu Wenruo rb_node = rb_simple_search(&cache->rb_root, eb->start); 29201b60d2ecSQu Wenruo if (!rb_node) { 29211b60d2ecSQu Wenruo upper = btrfs_backref_alloc_node(cache, eb->start, 29221b60d2ecSQu Wenruo lower->level + 1); 29231b60d2ecSQu Wenruo if (!upper) { 29241b60d2ecSQu Wenruo btrfs_put_root(root); 29251b60d2ecSQu Wenruo btrfs_backref_free_edge(cache, edge); 29261b60d2ecSQu Wenruo ret = -ENOMEM; 29271b60d2ecSQu Wenruo goto out; 29281b60d2ecSQu Wenruo } 29291b60d2ecSQu Wenruo upper->owner = btrfs_header_owner(eb); 293092a7cc42SQu Wenruo if (!test_bit(BTRFS_ROOT_SHAREABLE, &root->state)) 29311b60d2ecSQu Wenruo upper->cowonly = 1; 29321b60d2ecSQu Wenruo 29331b60d2ecSQu Wenruo /* 29341b60d2ecSQu Wenruo * If we know the block isn't shared we can avoid 29351b60d2ecSQu Wenruo * checking its backrefs. 29361b60d2ecSQu Wenruo */ 29371b60d2ecSQu Wenruo if (btrfs_block_can_be_shared(root, eb)) 29381b60d2ecSQu Wenruo upper->checked = 0; 29391b60d2ecSQu Wenruo else 29401b60d2ecSQu Wenruo upper->checked = 1; 29411b60d2ecSQu Wenruo 29421b60d2ecSQu Wenruo /* 29431b60d2ecSQu Wenruo * Add the block to pending list if we need to check its 29441b60d2ecSQu Wenruo * backrefs, we only do this once while walking up a 29451b60d2ecSQu Wenruo * tree as we will catch anything else later on. 29461b60d2ecSQu Wenruo */ 29471b60d2ecSQu Wenruo if (!upper->checked && need_check) { 29481b60d2ecSQu Wenruo need_check = false; 29491b60d2ecSQu Wenruo list_add_tail(&edge->list[UPPER], 29501b60d2ecSQu Wenruo &cache->pending_edge); 29511b60d2ecSQu Wenruo } else { 29521b60d2ecSQu Wenruo if (upper->checked) 29531b60d2ecSQu Wenruo need_check = true; 29541b60d2ecSQu Wenruo INIT_LIST_HEAD(&edge->list[UPPER]); 29551b60d2ecSQu Wenruo } 29561b60d2ecSQu Wenruo } else { 29571b60d2ecSQu Wenruo upper = rb_entry(rb_node, struct btrfs_backref_node, 29581b60d2ecSQu Wenruo rb_node); 29591b60d2ecSQu Wenruo ASSERT(upper->checked); 29601b60d2ecSQu Wenruo INIT_LIST_HEAD(&edge->list[UPPER]); 29611b60d2ecSQu Wenruo if (!upper->owner) 29621b60d2ecSQu Wenruo upper->owner = btrfs_header_owner(eb); 29631b60d2ecSQu Wenruo } 29641b60d2ecSQu Wenruo btrfs_backref_link_edge(edge, lower, upper, LINK_LOWER); 29651b60d2ecSQu Wenruo 29661b60d2ecSQu Wenruo if (rb_node) { 29671b60d2ecSQu Wenruo btrfs_put_root(root); 29681b60d2ecSQu Wenruo break; 29691b60d2ecSQu Wenruo } 29701b60d2ecSQu Wenruo lower = upper; 29711b60d2ecSQu Wenruo upper = NULL; 29721b60d2ecSQu Wenruo } 29731b60d2ecSQu Wenruo out: 29741b60d2ecSQu Wenruo btrfs_release_path(path); 29751b60d2ecSQu Wenruo return ret; 29761b60d2ecSQu Wenruo } 29771b60d2ecSQu Wenruo 29781b60d2ecSQu Wenruo /* 29791b60d2ecSQu Wenruo * Add backref node @cur into @cache. 29801b60d2ecSQu Wenruo * 29811b60d2ecSQu Wenruo * NOTE: Even if the function returned 0, @cur is not yet cached as its upper 29821b60d2ecSQu Wenruo * links aren't yet bi-directional. Needs to finish such links. 2983fc997ed0SQu Wenruo * Use btrfs_backref_finish_upper_links() to finish such linkage. 29841b60d2ecSQu Wenruo * 29851b60d2ecSQu Wenruo * @path: Released path for indirect tree backref lookup 29861b60d2ecSQu Wenruo * @iter: Released backref iter for extent tree search 29871b60d2ecSQu Wenruo * @node_key: The first key of the tree block 29881b60d2ecSQu Wenruo */ 29891b60d2ecSQu Wenruo int btrfs_backref_add_tree_node(struct btrfs_backref_cache *cache, 29901b60d2ecSQu Wenruo struct btrfs_path *path, 29911b60d2ecSQu Wenruo struct btrfs_backref_iter *iter, 29921b60d2ecSQu Wenruo struct btrfs_key *node_key, 29931b60d2ecSQu Wenruo struct btrfs_backref_node *cur) 29941b60d2ecSQu Wenruo { 29951b60d2ecSQu Wenruo struct btrfs_fs_info *fs_info = cache->fs_info; 29961b60d2ecSQu Wenruo struct btrfs_backref_edge *edge; 29971b60d2ecSQu Wenruo struct btrfs_backref_node *exist; 29981b60d2ecSQu Wenruo int ret; 29991b60d2ecSQu Wenruo 30001b60d2ecSQu Wenruo ret = btrfs_backref_iter_start(iter, cur->bytenr); 30011b60d2ecSQu Wenruo if (ret < 0) 30021b60d2ecSQu Wenruo return ret; 30031b60d2ecSQu Wenruo /* 30041b60d2ecSQu Wenruo * We skip the first btrfs_tree_block_info, as we don't use the key 30051b60d2ecSQu Wenruo * stored in it, but fetch it from the tree block 30061b60d2ecSQu Wenruo */ 30071b60d2ecSQu Wenruo if (btrfs_backref_has_tree_block_info(iter)) { 30081b60d2ecSQu Wenruo ret = btrfs_backref_iter_next(iter); 30091b60d2ecSQu Wenruo if (ret < 0) 30101b60d2ecSQu Wenruo goto out; 30111b60d2ecSQu Wenruo /* No extra backref? This means the tree block is corrupted */ 30121b60d2ecSQu Wenruo if (ret > 0) { 30131b60d2ecSQu Wenruo ret = -EUCLEAN; 30141b60d2ecSQu Wenruo goto out; 30151b60d2ecSQu Wenruo } 30161b60d2ecSQu Wenruo } 30171b60d2ecSQu Wenruo WARN_ON(cur->checked); 30181b60d2ecSQu Wenruo if (!list_empty(&cur->upper)) { 30191b60d2ecSQu Wenruo /* 30201b60d2ecSQu Wenruo * The backref was added previously when processing backref of 30211b60d2ecSQu Wenruo * type BTRFS_TREE_BLOCK_REF_KEY 30221b60d2ecSQu Wenruo */ 30231b60d2ecSQu Wenruo ASSERT(list_is_singular(&cur->upper)); 30241b60d2ecSQu Wenruo edge = list_entry(cur->upper.next, struct btrfs_backref_edge, 30251b60d2ecSQu Wenruo list[LOWER]); 30261b60d2ecSQu Wenruo ASSERT(list_empty(&edge->list[UPPER])); 30271b60d2ecSQu Wenruo exist = edge->node[UPPER]; 30281b60d2ecSQu Wenruo /* 30291b60d2ecSQu Wenruo * Add the upper level block to pending list if we need check 30301b60d2ecSQu Wenruo * its backrefs 30311b60d2ecSQu Wenruo */ 30321b60d2ecSQu Wenruo if (!exist->checked) 30331b60d2ecSQu Wenruo list_add_tail(&edge->list[UPPER], &cache->pending_edge); 30341b60d2ecSQu Wenruo } else { 30351b60d2ecSQu Wenruo exist = NULL; 30361b60d2ecSQu Wenruo } 30371b60d2ecSQu Wenruo 30381b60d2ecSQu Wenruo for (; ret == 0; ret = btrfs_backref_iter_next(iter)) { 30391b60d2ecSQu Wenruo struct extent_buffer *eb; 30401b60d2ecSQu Wenruo struct btrfs_key key; 30411b60d2ecSQu Wenruo int type; 30421b60d2ecSQu Wenruo 30431b60d2ecSQu Wenruo cond_resched(); 30441b60d2ecSQu Wenruo eb = btrfs_backref_get_eb(iter); 30451b60d2ecSQu Wenruo 30461b60d2ecSQu Wenruo key.objectid = iter->bytenr; 30471b60d2ecSQu Wenruo if (btrfs_backref_iter_is_inline_ref(iter)) { 30481b60d2ecSQu Wenruo struct btrfs_extent_inline_ref *iref; 30491b60d2ecSQu Wenruo 30501b60d2ecSQu Wenruo /* Update key for inline backref */ 30511b60d2ecSQu Wenruo iref = (struct btrfs_extent_inline_ref *) 30521b60d2ecSQu Wenruo ((unsigned long)iter->cur_ptr); 30531b60d2ecSQu Wenruo type = btrfs_get_extent_inline_ref_type(eb, iref, 30541b60d2ecSQu Wenruo BTRFS_REF_TYPE_BLOCK); 30551b60d2ecSQu Wenruo if (type == BTRFS_REF_TYPE_INVALID) { 30561b60d2ecSQu Wenruo ret = -EUCLEAN; 30571b60d2ecSQu Wenruo goto out; 30581b60d2ecSQu Wenruo } 30591b60d2ecSQu Wenruo key.type = type; 30601b60d2ecSQu Wenruo key.offset = btrfs_extent_inline_ref_offset(eb, iref); 30611b60d2ecSQu Wenruo } else { 30621b60d2ecSQu Wenruo key.type = iter->cur_key.type; 30631b60d2ecSQu Wenruo key.offset = iter->cur_key.offset; 30641b60d2ecSQu Wenruo } 30651b60d2ecSQu Wenruo 30661b60d2ecSQu Wenruo /* 30671b60d2ecSQu Wenruo * Parent node found and matches current inline ref, no need to 30681b60d2ecSQu Wenruo * rebuild this node for this inline ref 30691b60d2ecSQu Wenruo */ 30701b60d2ecSQu Wenruo if (exist && 30711b60d2ecSQu Wenruo ((key.type == BTRFS_TREE_BLOCK_REF_KEY && 30721b60d2ecSQu Wenruo exist->owner == key.offset) || 30731b60d2ecSQu Wenruo (key.type == BTRFS_SHARED_BLOCK_REF_KEY && 30741b60d2ecSQu Wenruo exist->bytenr == key.offset))) { 30751b60d2ecSQu Wenruo exist = NULL; 30761b60d2ecSQu Wenruo continue; 30771b60d2ecSQu Wenruo } 30781b60d2ecSQu Wenruo 30791b60d2ecSQu Wenruo /* SHARED_BLOCK_REF means key.offset is the parent bytenr */ 30801b60d2ecSQu Wenruo if (key.type == BTRFS_SHARED_BLOCK_REF_KEY) { 30811b60d2ecSQu Wenruo ret = handle_direct_tree_backref(cache, &key, cur); 30821b60d2ecSQu Wenruo if (ret < 0) 30831b60d2ecSQu Wenruo goto out; 30841b60d2ecSQu Wenruo continue; 30851b60d2ecSQu Wenruo } else if (unlikely(key.type == BTRFS_EXTENT_REF_V0_KEY)) { 30861b60d2ecSQu Wenruo ret = -EINVAL; 30871b60d2ecSQu Wenruo btrfs_print_v0_err(fs_info); 30881b60d2ecSQu Wenruo btrfs_handle_fs_error(fs_info, ret, NULL); 30891b60d2ecSQu Wenruo goto out; 30901b60d2ecSQu Wenruo } else if (key.type != BTRFS_TREE_BLOCK_REF_KEY) { 30911b60d2ecSQu Wenruo continue; 30921b60d2ecSQu Wenruo } 30931b60d2ecSQu Wenruo 30941b60d2ecSQu Wenruo /* 30951b60d2ecSQu Wenruo * key.type == BTRFS_TREE_BLOCK_REF_KEY, inline ref offset 30961b60d2ecSQu Wenruo * means the root objectid. We need to search the tree to get 30971b60d2ecSQu Wenruo * its parent bytenr. 30981b60d2ecSQu Wenruo */ 30991b60d2ecSQu Wenruo ret = handle_indirect_tree_backref(cache, path, &key, node_key, 31001b60d2ecSQu Wenruo cur); 31011b60d2ecSQu Wenruo if (ret < 0) 31021b60d2ecSQu Wenruo goto out; 31031b60d2ecSQu Wenruo } 31041b60d2ecSQu Wenruo ret = 0; 31051b60d2ecSQu Wenruo cur->checked = 1; 31061b60d2ecSQu Wenruo WARN_ON(exist); 31071b60d2ecSQu Wenruo out: 31081b60d2ecSQu Wenruo btrfs_backref_iter_release(iter); 31091b60d2ecSQu Wenruo return ret; 31101b60d2ecSQu Wenruo } 3111fc997ed0SQu Wenruo 3112fc997ed0SQu Wenruo /* 3113fc997ed0SQu Wenruo * Finish the upwards linkage created by btrfs_backref_add_tree_node() 3114fc997ed0SQu Wenruo */ 3115fc997ed0SQu Wenruo int btrfs_backref_finish_upper_links(struct btrfs_backref_cache *cache, 3116fc997ed0SQu Wenruo struct btrfs_backref_node *start) 3117fc997ed0SQu Wenruo { 3118fc997ed0SQu Wenruo struct list_head *useless_node = &cache->useless_node; 3119fc997ed0SQu Wenruo struct btrfs_backref_edge *edge; 3120fc997ed0SQu Wenruo struct rb_node *rb_node; 3121fc997ed0SQu Wenruo LIST_HEAD(pending_edge); 3122fc997ed0SQu Wenruo 3123fc997ed0SQu Wenruo ASSERT(start->checked); 3124fc997ed0SQu Wenruo 3125fc997ed0SQu Wenruo /* Insert this node to cache if it's not COW-only */ 3126fc997ed0SQu Wenruo if (!start->cowonly) { 3127fc997ed0SQu Wenruo rb_node = rb_simple_insert(&cache->rb_root, start->bytenr, 3128fc997ed0SQu Wenruo &start->rb_node); 3129fc997ed0SQu Wenruo if (rb_node) 3130fc997ed0SQu Wenruo btrfs_backref_panic(cache->fs_info, start->bytenr, 3131fc997ed0SQu Wenruo -EEXIST); 3132fc997ed0SQu Wenruo list_add_tail(&start->lower, &cache->leaves); 3133fc997ed0SQu Wenruo } 3134fc997ed0SQu Wenruo 3135fc997ed0SQu Wenruo /* 3136fc997ed0SQu Wenruo * Use breadth first search to iterate all related edges. 3137fc997ed0SQu Wenruo * 3138fc997ed0SQu Wenruo * The starting points are all the edges of this node 3139fc997ed0SQu Wenruo */ 3140fc997ed0SQu Wenruo list_for_each_entry(edge, &start->upper, list[LOWER]) 3141fc997ed0SQu Wenruo list_add_tail(&edge->list[UPPER], &pending_edge); 3142fc997ed0SQu Wenruo 3143fc997ed0SQu Wenruo while (!list_empty(&pending_edge)) { 3144fc997ed0SQu Wenruo struct btrfs_backref_node *upper; 3145fc997ed0SQu Wenruo struct btrfs_backref_node *lower; 3146fc997ed0SQu Wenruo 3147fc997ed0SQu Wenruo edge = list_first_entry(&pending_edge, 3148fc997ed0SQu Wenruo struct btrfs_backref_edge, list[UPPER]); 3149fc997ed0SQu Wenruo list_del_init(&edge->list[UPPER]); 3150fc997ed0SQu Wenruo upper = edge->node[UPPER]; 3151fc997ed0SQu Wenruo lower = edge->node[LOWER]; 3152fc997ed0SQu Wenruo 3153fc997ed0SQu Wenruo /* Parent is detached, no need to keep any edges */ 3154fc997ed0SQu Wenruo if (upper->detached) { 3155fc997ed0SQu Wenruo list_del(&edge->list[LOWER]); 3156fc997ed0SQu Wenruo btrfs_backref_free_edge(cache, edge); 3157fc997ed0SQu Wenruo 3158fc997ed0SQu Wenruo /* Lower node is orphan, queue for cleanup */ 3159fc997ed0SQu Wenruo if (list_empty(&lower->upper)) 3160fc997ed0SQu Wenruo list_add(&lower->list, useless_node); 3161fc997ed0SQu Wenruo continue; 3162fc997ed0SQu Wenruo } 3163fc997ed0SQu Wenruo 3164fc997ed0SQu Wenruo /* 3165fc997ed0SQu Wenruo * All new nodes added in current build_backref_tree() haven't 3166fc997ed0SQu Wenruo * been linked to the cache rb tree. 3167fc997ed0SQu Wenruo * So if we have upper->rb_node populated, this means a cache 3168fc997ed0SQu Wenruo * hit. We only need to link the edge, as @upper and all its 3169fc997ed0SQu Wenruo * parents have already been linked. 3170fc997ed0SQu Wenruo */ 3171fc997ed0SQu Wenruo if (!RB_EMPTY_NODE(&upper->rb_node)) { 3172fc997ed0SQu Wenruo if (upper->lowest) { 3173fc997ed0SQu Wenruo list_del_init(&upper->lower); 3174fc997ed0SQu Wenruo upper->lowest = 0; 3175fc997ed0SQu Wenruo } 3176fc997ed0SQu Wenruo 3177fc997ed0SQu Wenruo list_add_tail(&edge->list[UPPER], &upper->lower); 3178fc997ed0SQu Wenruo continue; 3179fc997ed0SQu Wenruo } 3180fc997ed0SQu Wenruo 3181fc997ed0SQu Wenruo /* Sanity check, we shouldn't have any unchecked nodes */ 3182fc997ed0SQu Wenruo if (!upper->checked) { 3183fc997ed0SQu Wenruo ASSERT(0); 3184fc997ed0SQu Wenruo return -EUCLEAN; 3185fc997ed0SQu Wenruo } 3186fc997ed0SQu Wenruo 3187fc997ed0SQu Wenruo /* Sanity check, COW-only node has non-COW-only parent */ 3188fc997ed0SQu Wenruo if (start->cowonly != upper->cowonly) { 3189fc997ed0SQu Wenruo ASSERT(0); 3190fc997ed0SQu Wenruo return -EUCLEAN; 3191fc997ed0SQu Wenruo } 3192fc997ed0SQu Wenruo 3193fc997ed0SQu Wenruo /* Only cache non-COW-only (subvolume trees) tree blocks */ 3194fc997ed0SQu Wenruo if (!upper->cowonly) { 3195fc997ed0SQu Wenruo rb_node = rb_simple_insert(&cache->rb_root, upper->bytenr, 3196fc997ed0SQu Wenruo &upper->rb_node); 3197fc997ed0SQu Wenruo if (rb_node) { 3198fc997ed0SQu Wenruo btrfs_backref_panic(cache->fs_info, 3199fc997ed0SQu Wenruo upper->bytenr, -EEXIST); 3200fc997ed0SQu Wenruo return -EUCLEAN; 3201fc997ed0SQu Wenruo } 3202fc997ed0SQu Wenruo } 3203fc997ed0SQu Wenruo 3204fc997ed0SQu Wenruo list_add_tail(&edge->list[UPPER], &upper->lower); 3205fc997ed0SQu Wenruo 3206fc997ed0SQu Wenruo /* 3207fc997ed0SQu Wenruo * Also queue all the parent edges of this uncached node 3208fc997ed0SQu Wenruo * to finish the upper linkage 3209fc997ed0SQu Wenruo */ 3210fc997ed0SQu Wenruo list_for_each_entry(edge, &upper->upper, list[LOWER]) 3211fc997ed0SQu Wenruo list_add_tail(&edge->list[UPPER], &pending_edge); 3212fc997ed0SQu Wenruo } 3213fc997ed0SQu Wenruo return 0; 3214fc997ed0SQu Wenruo } 32151b23ea18SQu Wenruo 32161b23ea18SQu Wenruo void btrfs_backref_error_cleanup(struct btrfs_backref_cache *cache, 32171b23ea18SQu Wenruo struct btrfs_backref_node *node) 32181b23ea18SQu Wenruo { 32191b23ea18SQu Wenruo struct btrfs_backref_node *lower; 32201b23ea18SQu Wenruo struct btrfs_backref_node *upper; 32211b23ea18SQu Wenruo struct btrfs_backref_edge *edge; 32221b23ea18SQu Wenruo 32231b23ea18SQu Wenruo while (!list_empty(&cache->useless_node)) { 32241b23ea18SQu Wenruo lower = list_first_entry(&cache->useless_node, 32251b23ea18SQu Wenruo struct btrfs_backref_node, list); 32261b23ea18SQu Wenruo list_del_init(&lower->list); 32271b23ea18SQu Wenruo } 32281b23ea18SQu Wenruo while (!list_empty(&cache->pending_edge)) { 32291b23ea18SQu Wenruo edge = list_first_entry(&cache->pending_edge, 32301b23ea18SQu Wenruo struct btrfs_backref_edge, list[UPPER]); 32311b23ea18SQu Wenruo list_del(&edge->list[UPPER]); 32321b23ea18SQu Wenruo list_del(&edge->list[LOWER]); 32331b23ea18SQu Wenruo lower = edge->node[LOWER]; 32341b23ea18SQu Wenruo upper = edge->node[UPPER]; 32351b23ea18SQu Wenruo btrfs_backref_free_edge(cache, edge); 32361b23ea18SQu Wenruo 32371b23ea18SQu Wenruo /* 32381b23ea18SQu Wenruo * Lower is no longer linked to any upper backref nodes and 32391b23ea18SQu Wenruo * isn't in the cache, we can free it ourselves. 32401b23ea18SQu Wenruo */ 32411b23ea18SQu Wenruo if (list_empty(&lower->upper) && 32421b23ea18SQu Wenruo RB_EMPTY_NODE(&lower->rb_node)) 32431b23ea18SQu Wenruo list_add(&lower->list, &cache->useless_node); 32441b23ea18SQu Wenruo 32451b23ea18SQu Wenruo if (!RB_EMPTY_NODE(&upper->rb_node)) 32461b23ea18SQu Wenruo continue; 32471b23ea18SQu Wenruo 32481b23ea18SQu Wenruo /* Add this guy's upper edges to the list to process */ 32491b23ea18SQu Wenruo list_for_each_entry(edge, &upper->upper, list[LOWER]) 32501b23ea18SQu Wenruo list_add_tail(&edge->list[UPPER], 32511b23ea18SQu Wenruo &cache->pending_edge); 32521b23ea18SQu Wenruo if (list_empty(&upper->upper)) 32531b23ea18SQu Wenruo list_add(&upper->list, &cache->useless_node); 32541b23ea18SQu Wenruo } 32551b23ea18SQu Wenruo 32561b23ea18SQu Wenruo while (!list_empty(&cache->useless_node)) { 32571b23ea18SQu Wenruo lower = list_first_entry(&cache->useless_node, 32581b23ea18SQu Wenruo struct btrfs_backref_node, list); 32591b23ea18SQu Wenruo list_del_init(&lower->list); 32601b23ea18SQu Wenruo if (lower == node) 32611b23ea18SQu Wenruo node = NULL; 326249ecc679SJosef Bacik btrfs_backref_drop_node(cache, lower); 32631b23ea18SQu Wenruo } 32641b23ea18SQu Wenruo 32651b23ea18SQu Wenruo btrfs_backref_cleanup_node(cache, node); 32661b23ea18SQu Wenruo ASSERT(list_empty(&cache->useless_node) && 32671b23ea18SQu Wenruo list_empty(&cache->pending_edge)); 32681b23ea18SQu Wenruo } 3269