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" 18c7f13d42SJosef Bacik #include "fs.h" 1907e81dc9SJosef Bacik #include "accessors.h" 20a0231804SJosef Bacik #include "extent-tree.h" 2167707479SJosef Bacik #include "relocation.h" 22a542ad1bSJan Schmidt 23877c1476SFilipe Manana /* Just arbitrary numbers so we can be sure one of these happened. */ 24dc046b10SJosef Bacik #define BACKREF_FOUND_SHARED 6 25877c1476SFilipe Manana #define BACKREF_FOUND_NOT_SHARED 7 26dc046b10SJosef Bacik 27976b1908SJan Schmidt struct extent_inode_elem { 28976b1908SJan Schmidt u64 inum; 29976b1908SJan Schmidt u64 offset; 30*c7499a64SFilipe Manana u64 num_bytes; 31976b1908SJan Schmidt struct extent_inode_elem *next; 32976b1908SJan Schmidt }; 33976b1908SJan Schmidt 3473980becSJeff Mahoney static int check_extent_in_eb(const struct btrfs_key *key, 3573980becSJeff Mahoney const struct extent_buffer *eb, 3673980becSJeff Mahoney const struct btrfs_file_extent_item *fi, 37976b1908SJan Schmidt u64 extent_item_pos, 38c995ab3cSZygo Blaxell struct extent_inode_elem **eie, 39c995ab3cSZygo Blaxell bool ignore_offset) 40976b1908SJan Schmidt { 41*c7499a64SFilipe Manana const u64 data_len = btrfs_file_extent_num_bytes(eb, fi); 428ca15e05SJosef Bacik u64 offset = 0; 438ca15e05SJosef Bacik struct extent_inode_elem *e; 448ca15e05SJosef Bacik 45c995ab3cSZygo Blaxell if (!ignore_offset && 46c995ab3cSZygo Blaxell !btrfs_file_extent_compression(eb, fi) && 478ca15e05SJosef Bacik !btrfs_file_extent_encryption(eb, fi) && 488ca15e05SJosef Bacik !btrfs_file_extent_other_encoding(eb, fi)) { 49976b1908SJan Schmidt u64 data_offset; 50976b1908SJan Schmidt 51976b1908SJan Schmidt data_offset = btrfs_file_extent_offset(eb, fi); 52976b1908SJan Schmidt 53976b1908SJan Schmidt if (extent_item_pos < data_offset || 54976b1908SJan Schmidt extent_item_pos >= data_offset + data_len) 55976b1908SJan Schmidt return 1; 568ca15e05SJosef Bacik offset = extent_item_pos - data_offset; 578ca15e05SJosef Bacik } 58976b1908SJan Schmidt 59976b1908SJan Schmidt e = kmalloc(sizeof(*e), GFP_NOFS); 60976b1908SJan Schmidt if (!e) 61976b1908SJan Schmidt return -ENOMEM; 62976b1908SJan Schmidt 63976b1908SJan Schmidt e->next = *eie; 64976b1908SJan Schmidt e->inum = key->objectid; 658ca15e05SJosef Bacik e->offset = key->offset + offset; 66*c7499a64SFilipe Manana e->num_bytes = data_len; 67976b1908SJan Schmidt *eie = e; 68976b1908SJan Schmidt 69976b1908SJan Schmidt return 0; 70976b1908SJan Schmidt } 71976b1908SJan Schmidt 72f05c4746SWang Shilong static void free_inode_elem_list(struct extent_inode_elem *eie) 73f05c4746SWang Shilong { 74f05c4746SWang Shilong struct extent_inode_elem *eie_next; 75f05c4746SWang Shilong 76f05c4746SWang Shilong for (; eie; eie = eie_next) { 77f05c4746SWang Shilong eie_next = eie->next; 78f05c4746SWang Shilong kfree(eie); 79f05c4746SWang Shilong } 80f05c4746SWang Shilong } 81f05c4746SWang Shilong 8273980becSJeff Mahoney static int find_extent_in_eb(const struct extent_buffer *eb, 8373980becSJeff Mahoney u64 wanted_disk_byte, u64 extent_item_pos, 84c995ab3cSZygo Blaxell struct extent_inode_elem **eie, 85c995ab3cSZygo Blaxell bool ignore_offset) 86976b1908SJan Schmidt { 87976b1908SJan Schmidt u64 disk_byte; 88976b1908SJan Schmidt struct btrfs_key key; 89976b1908SJan Schmidt struct btrfs_file_extent_item *fi; 90976b1908SJan Schmidt int slot; 91976b1908SJan Schmidt int nritems; 92976b1908SJan Schmidt int extent_type; 93976b1908SJan Schmidt int ret; 94976b1908SJan Schmidt 95976b1908SJan Schmidt /* 96976b1908SJan Schmidt * from the shared data ref, we only have the leaf but we need 97976b1908SJan Schmidt * the key. thus, we must look into all items and see that we 98976b1908SJan Schmidt * find one (some) with a reference to our extent item. 99976b1908SJan Schmidt */ 100976b1908SJan Schmidt nritems = btrfs_header_nritems(eb); 101976b1908SJan Schmidt for (slot = 0; slot < nritems; ++slot) { 102976b1908SJan Schmidt btrfs_item_key_to_cpu(eb, &key, slot); 103976b1908SJan Schmidt if (key.type != BTRFS_EXTENT_DATA_KEY) 104976b1908SJan Schmidt continue; 105976b1908SJan Schmidt fi = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item); 106976b1908SJan Schmidt extent_type = btrfs_file_extent_type(eb, fi); 107976b1908SJan Schmidt if (extent_type == BTRFS_FILE_EXTENT_INLINE) 108976b1908SJan Schmidt continue; 109976b1908SJan Schmidt /* don't skip BTRFS_FILE_EXTENT_PREALLOC, we can handle that */ 110976b1908SJan Schmidt disk_byte = btrfs_file_extent_disk_bytenr(eb, fi); 111976b1908SJan Schmidt if (disk_byte != wanted_disk_byte) 112976b1908SJan Schmidt continue; 113976b1908SJan Schmidt 114c995ab3cSZygo Blaxell ret = check_extent_in_eb(&key, eb, fi, extent_item_pos, eie, ignore_offset); 115976b1908SJan Schmidt if (ret < 0) 116976b1908SJan Schmidt return ret; 117976b1908SJan Schmidt } 118976b1908SJan Schmidt 119976b1908SJan Schmidt return 0; 120976b1908SJan Schmidt } 121976b1908SJan Schmidt 12286d5f994SEdmund Nadolski struct preftree { 123ecf160b4SLiu Bo struct rb_root_cached root; 1246c336b21SJeff Mahoney unsigned int count; 12586d5f994SEdmund Nadolski }; 12686d5f994SEdmund Nadolski 127ecf160b4SLiu Bo #define PREFTREE_INIT { .root = RB_ROOT_CACHED, .count = 0 } 12886d5f994SEdmund Nadolski 12986d5f994SEdmund Nadolski struct preftrees { 13086d5f994SEdmund Nadolski struct preftree direct; /* BTRFS_SHARED_[DATA|BLOCK]_REF_KEY */ 13186d5f994SEdmund Nadolski struct preftree indirect; /* BTRFS_[TREE_BLOCK|EXTENT_DATA]_REF_KEY */ 13286d5f994SEdmund Nadolski struct preftree indirect_missing_keys; 13386d5f994SEdmund Nadolski }; 13486d5f994SEdmund Nadolski 1353ec4d323SEdmund Nadolski /* 1363ec4d323SEdmund Nadolski * Checks for a shared extent during backref search. 1373ec4d323SEdmund Nadolski * 1383ec4d323SEdmund Nadolski * The share_count tracks prelim_refs (direct and indirect) having a 1393ec4d323SEdmund Nadolski * ref->count >0: 1403ec4d323SEdmund Nadolski * - incremented when a ref->count transitions to >0 1413ec4d323SEdmund Nadolski * - decremented when a ref->count transitions to <1 1423ec4d323SEdmund Nadolski */ 1433ec4d323SEdmund Nadolski struct share_check { 144877c1476SFilipe Manana struct btrfs_backref_share_check_ctx *ctx; 145877c1476SFilipe Manana struct btrfs_root *root; 1463ec4d323SEdmund Nadolski u64 inum; 14773e339e6SFilipe Manana u64 data_bytenr; 1486976201fSFilipe Manana u64 data_extent_gen; 14973e339e6SFilipe Manana /* 15073e339e6SFilipe Manana * Counts number of inodes that refer to an extent (different inodes in 15173e339e6SFilipe Manana * the same root or different roots) that we could find. The sharedness 15273e339e6SFilipe Manana * check typically stops once this counter gets greater than 1, so it 15373e339e6SFilipe Manana * may not reflect the total number of inodes. 15473e339e6SFilipe Manana */ 1553ec4d323SEdmund Nadolski int share_count; 15673e339e6SFilipe Manana /* 15773e339e6SFilipe Manana * The number of times we found our inode refers to the data extent we 15873e339e6SFilipe Manana * are determining the sharedness. In other words, how many file extent 15973e339e6SFilipe Manana * items we could find for our inode that point to our target data 16073e339e6SFilipe Manana * extent. The value we get here after finishing the extent sharedness 16173e339e6SFilipe Manana * check may be smaller than reality, but if it ends up being greater 16273e339e6SFilipe Manana * than 1, then we know for sure the inode has multiple file extent 16373e339e6SFilipe Manana * items that point to our inode, and we can safely assume it's useful 16473e339e6SFilipe Manana * to cache the sharedness check result. 16573e339e6SFilipe Manana */ 16673e339e6SFilipe Manana int self_ref_count; 1674fc7b572SFilipe Manana bool have_delayed_delete_refs; 1683ec4d323SEdmund Nadolski }; 1693ec4d323SEdmund Nadolski 1703ec4d323SEdmund Nadolski static inline int extent_is_shared(struct share_check *sc) 1713ec4d323SEdmund Nadolski { 1723ec4d323SEdmund Nadolski return (sc && sc->share_count > 1) ? BACKREF_FOUND_SHARED : 0; 1733ec4d323SEdmund Nadolski } 1743ec4d323SEdmund Nadolski 175b9e9a6cbSWang Shilong static struct kmem_cache *btrfs_prelim_ref_cache; 176b9e9a6cbSWang Shilong 177b9e9a6cbSWang Shilong int __init btrfs_prelim_ref_init(void) 178b9e9a6cbSWang Shilong { 179b9e9a6cbSWang Shilong btrfs_prelim_ref_cache = kmem_cache_create("btrfs_prelim_ref", 180e0c476b1SJeff Mahoney sizeof(struct prelim_ref), 181b9e9a6cbSWang Shilong 0, 182fba4b697SNikolay Borisov SLAB_MEM_SPREAD, 183b9e9a6cbSWang Shilong NULL); 184b9e9a6cbSWang Shilong if (!btrfs_prelim_ref_cache) 185b9e9a6cbSWang Shilong return -ENOMEM; 186b9e9a6cbSWang Shilong return 0; 187b9e9a6cbSWang Shilong } 188b9e9a6cbSWang Shilong 189e67c718bSDavid Sterba void __cold btrfs_prelim_ref_exit(void) 190b9e9a6cbSWang Shilong { 191b9e9a6cbSWang Shilong kmem_cache_destroy(btrfs_prelim_ref_cache); 192b9e9a6cbSWang Shilong } 193b9e9a6cbSWang Shilong 19486d5f994SEdmund Nadolski static void free_pref(struct prelim_ref *ref) 19586d5f994SEdmund Nadolski { 19686d5f994SEdmund Nadolski kmem_cache_free(btrfs_prelim_ref_cache, ref); 19786d5f994SEdmund Nadolski } 19886d5f994SEdmund Nadolski 19986d5f994SEdmund Nadolski /* 20086d5f994SEdmund Nadolski * Return 0 when both refs are for the same block (and can be merged). 20186d5f994SEdmund Nadolski * A -1 return indicates ref1 is a 'lower' block than ref2, while 1 20286d5f994SEdmund Nadolski * indicates a 'higher' block. 20386d5f994SEdmund Nadolski */ 20486d5f994SEdmund Nadolski static int prelim_ref_compare(struct prelim_ref *ref1, 20586d5f994SEdmund Nadolski struct prelim_ref *ref2) 20686d5f994SEdmund Nadolski { 20786d5f994SEdmund Nadolski if (ref1->level < ref2->level) 20886d5f994SEdmund Nadolski return -1; 20986d5f994SEdmund Nadolski if (ref1->level > ref2->level) 21086d5f994SEdmund Nadolski return 1; 21186d5f994SEdmund Nadolski if (ref1->root_id < ref2->root_id) 21286d5f994SEdmund Nadolski return -1; 21386d5f994SEdmund Nadolski if (ref1->root_id > ref2->root_id) 21486d5f994SEdmund Nadolski return 1; 21586d5f994SEdmund Nadolski if (ref1->key_for_search.type < ref2->key_for_search.type) 21686d5f994SEdmund Nadolski return -1; 21786d5f994SEdmund Nadolski if (ref1->key_for_search.type > ref2->key_for_search.type) 21886d5f994SEdmund Nadolski return 1; 21986d5f994SEdmund Nadolski if (ref1->key_for_search.objectid < ref2->key_for_search.objectid) 22086d5f994SEdmund Nadolski return -1; 22186d5f994SEdmund Nadolski if (ref1->key_for_search.objectid > ref2->key_for_search.objectid) 22286d5f994SEdmund Nadolski return 1; 22386d5f994SEdmund Nadolski if (ref1->key_for_search.offset < ref2->key_for_search.offset) 22486d5f994SEdmund Nadolski return -1; 22586d5f994SEdmund Nadolski if (ref1->key_for_search.offset > ref2->key_for_search.offset) 22686d5f994SEdmund Nadolski return 1; 22786d5f994SEdmund Nadolski if (ref1->parent < ref2->parent) 22886d5f994SEdmund Nadolski return -1; 22986d5f994SEdmund Nadolski if (ref1->parent > ref2->parent) 23086d5f994SEdmund Nadolski return 1; 23186d5f994SEdmund Nadolski 23286d5f994SEdmund Nadolski return 0; 23386d5f994SEdmund Nadolski } 23486d5f994SEdmund Nadolski 235ccc8dc75SColin Ian King static void update_share_count(struct share_check *sc, int oldcount, 23673e339e6SFilipe Manana int newcount, struct prelim_ref *newref) 2373ec4d323SEdmund Nadolski { 2383ec4d323SEdmund Nadolski if ((!sc) || (oldcount == 0 && newcount < 1)) 2393ec4d323SEdmund Nadolski return; 2403ec4d323SEdmund Nadolski 2413ec4d323SEdmund Nadolski if (oldcount > 0 && newcount < 1) 2423ec4d323SEdmund Nadolski sc->share_count--; 2433ec4d323SEdmund Nadolski else if (oldcount < 1 && newcount > 0) 2443ec4d323SEdmund Nadolski sc->share_count++; 24573e339e6SFilipe Manana 246877c1476SFilipe Manana if (newref->root_id == sc->root->root_key.objectid && 24773e339e6SFilipe Manana newref->wanted_disk_byte == sc->data_bytenr && 24873e339e6SFilipe Manana newref->key_for_search.objectid == sc->inum) 24973e339e6SFilipe Manana sc->self_ref_count += newref->count; 2503ec4d323SEdmund Nadolski } 2513ec4d323SEdmund Nadolski 25286d5f994SEdmund Nadolski /* 25386d5f994SEdmund Nadolski * Add @newref to the @root rbtree, merging identical refs. 25486d5f994SEdmund Nadolski * 2553ec4d323SEdmund Nadolski * Callers should assume that newref has been freed after calling. 25686d5f994SEdmund Nadolski */ 25700142756SJeff Mahoney static void prelim_ref_insert(const struct btrfs_fs_info *fs_info, 25800142756SJeff Mahoney struct preftree *preftree, 2593ec4d323SEdmund Nadolski struct prelim_ref *newref, 2603ec4d323SEdmund Nadolski struct share_check *sc) 26186d5f994SEdmund Nadolski { 262ecf160b4SLiu Bo struct rb_root_cached *root; 26386d5f994SEdmund Nadolski struct rb_node **p; 26486d5f994SEdmund Nadolski struct rb_node *parent = NULL; 26586d5f994SEdmund Nadolski struct prelim_ref *ref; 26686d5f994SEdmund Nadolski int result; 267ecf160b4SLiu Bo bool leftmost = true; 26886d5f994SEdmund Nadolski 26986d5f994SEdmund Nadolski root = &preftree->root; 270ecf160b4SLiu Bo p = &root->rb_root.rb_node; 27186d5f994SEdmund Nadolski 27286d5f994SEdmund Nadolski while (*p) { 27386d5f994SEdmund Nadolski parent = *p; 27486d5f994SEdmund Nadolski ref = rb_entry(parent, struct prelim_ref, rbnode); 27586d5f994SEdmund Nadolski result = prelim_ref_compare(ref, newref); 27686d5f994SEdmund Nadolski if (result < 0) { 27786d5f994SEdmund Nadolski p = &(*p)->rb_left; 27886d5f994SEdmund Nadolski } else if (result > 0) { 27986d5f994SEdmund Nadolski p = &(*p)->rb_right; 280ecf160b4SLiu Bo leftmost = false; 28186d5f994SEdmund Nadolski } else { 28286d5f994SEdmund Nadolski /* Identical refs, merge them and free @newref */ 28386d5f994SEdmund Nadolski struct extent_inode_elem *eie = ref->inode_list; 28486d5f994SEdmund Nadolski 28586d5f994SEdmund Nadolski while (eie && eie->next) 28686d5f994SEdmund Nadolski eie = eie->next; 28786d5f994SEdmund Nadolski 28886d5f994SEdmund Nadolski if (!eie) 28986d5f994SEdmund Nadolski ref->inode_list = newref->inode_list; 29086d5f994SEdmund Nadolski else 29186d5f994SEdmund Nadolski eie->next = newref->inode_list; 29200142756SJeff Mahoney trace_btrfs_prelim_ref_merge(fs_info, ref, newref, 29300142756SJeff Mahoney preftree->count); 2943ec4d323SEdmund Nadolski /* 2953ec4d323SEdmund Nadolski * A delayed ref can have newref->count < 0. 2963ec4d323SEdmund Nadolski * The ref->count is updated to follow any 2973ec4d323SEdmund Nadolski * BTRFS_[ADD|DROP]_DELAYED_REF actions. 2983ec4d323SEdmund Nadolski */ 2993ec4d323SEdmund Nadolski update_share_count(sc, ref->count, 30073e339e6SFilipe Manana ref->count + newref->count, newref); 30186d5f994SEdmund Nadolski ref->count += newref->count; 30286d5f994SEdmund Nadolski free_pref(newref); 30386d5f994SEdmund Nadolski return; 30486d5f994SEdmund Nadolski } 30586d5f994SEdmund Nadolski } 30686d5f994SEdmund Nadolski 30773e339e6SFilipe Manana update_share_count(sc, 0, newref->count, newref); 3086c336b21SJeff Mahoney preftree->count++; 30900142756SJeff Mahoney trace_btrfs_prelim_ref_insert(fs_info, newref, NULL, preftree->count); 31086d5f994SEdmund Nadolski rb_link_node(&newref->rbnode, parent, p); 311ecf160b4SLiu Bo rb_insert_color_cached(&newref->rbnode, root, leftmost); 31286d5f994SEdmund Nadolski } 31386d5f994SEdmund Nadolski 31486d5f994SEdmund Nadolski /* 31586d5f994SEdmund Nadolski * Release the entire tree. We don't care about internal consistency so 31686d5f994SEdmund Nadolski * just free everything and then reset the tree root. 31786d5f994SEdmund Nadolski */ 31886d5f994SEdmund Nadolski static void prelim_release(struct preftree *preftree) 31986d5f994SEdmund Nadolski { 32086d5f994SEdmund Nadolski struct prelim_ref *ref, *next_ref; 32186d5f994SEdmund Nadolski 322ecf160b4SLiu Bo rbtree_postorder_for_each_entry_safe(ref, next_ref, 32392876eecSFilipe Manana &preftree->root.rb_root, rbnode) { 32492876eecSFilipe Manana free_inode_elem_list(ref->inode_list); 32586d5f994SEdmund Nadolski free_pref(ref); 32692876eecSFilipe Manana } 32786d5f994SEdmund Nadolski 328ecf160b4SLiu Bo preftree->root = RB_ROOT_CACHED; 3296c336b21SJeff Mahoney preftree->count = 0; 33086d5f994SEdmund Nadolski } 33186d5f994SEdmund Nadolski 332d5c88b73SJan Schmidt /* 333d5c88b73SJan Schmidt * the rules for all callers of this function are: 334d5c88b73SJan Schmidt * - obtaining the parent is the goal 335d5c88b73SJan Schmidt * - if you add a key, you must know that it is a correct key 336d5c88b73SJan Schmidt * - if you cannot add the parent or a correct key, then we will look into the 337d5c88b73SJan Schmidt * block later to set a correct key 338d5c88b73SJan Schmidt * 339d5c88b73SJan Schmidt * delayed refs 340d5c88b73SJan Schmidt * ============ 341d5c88b73SJan Schmidt * backref type | shared | indirect | shared | indirect 342d5c88b73SJan Schmidt * information | tree | tree | data | data 343d5c88b73SJan Schmidt * --------------------+--------+----------+--------+---------- 344d5c88b73SJan Schmidt * parent logical | y | - | - | - 345d5c88b73SJan Schmidt * key to resolve | - | y | y | y 346d5c88b73SJan Schmidt * tree block logical | - | - | - | - 347d5c88b73SJan Schmidt * root for resolving | y | y | y | y 348d5c88b73SJan Schmidt * 349d5c88b73SJan Schmidt * - column 1: we've the parent -> done 350d5c88b73SJan Schmidt * - column 2, 3, 4: we use the key to find the parent 351d5c88b73SJan Schmidt * 352d5c88b73SJan Schmidt * on disk refs (inline or keyed) 353d5c88b73SJan Schmidt * ============================== 354d5c88b73SJan Schmidt * backref type | shared | indirect | shared | indirect 355d5c88b73SJan Schmidt * information | tree | tree | data | data 356d5c88b73SJan Schmidt * --------------------+--------+----------+--------+---------- 357d5c88b73SJan Schmidt * parent logical | y | - | y | - 358d5c88b73SJan Schmidt * key to resolve | - | - | - | y 359d5c88b73SJan Schmidt * tree block logical | y | y | y | y 360d5c88b73SJan Schmidt * root for resolving | - | y | y | y 361d5c88b73SJan Schmidt * 362d5c88b73SJan Schmidt * - column 1, 3: we've the parent -> done 363d5c88b73SJan Schmidt * - column 2: we take the first key from the block to find the parent 364e0c476b1SJeff Mahoney * (see add_missing_keys) 365d5c88b73SJan Schmidt * - column 4: we use the key to find the parent 366d5c88b73SJan Schmidt * 367d5c88b73SJan Schmidt * additional information that's available but not required to find the parent 368d5c88b73SJan Schmidt * block might help in merging entries to gain some speed. 369d5c88b73SJan Schmidt */ 37000142756SJeff Mahoney static int add_prelim_ref(const struct btrfs_fs_info *fs_info, 37100142756SJeff Mahoney struct preftree *preftree, u64 root_id, 372e0c476b1SJeff Mahoney const struct btrfs_key *key, int level, u64 parent, 3733ec4d323SEdmund Nadolski u64 wanted_disk_byte, int count, 3743ec4d323SEdmund Nadolski struct share_check *sc, gfp_t gfp_mask) 3758da6d581SJan Schmidt { 376e0c476b1SJeff Mahoney struct prelim_ref *ref; 3778da6d581SJan Schmidt 37848ec4736SLiu Bo if (root_id == BTRFS_DATA_RELOC_TREE_OBJECTID) 37948ec4736SLiu Bo return 0; 38048ec4736SLiu Bo 381b9e9a6cbSWang Shilong ref = kmem_cache_alloc(btrfs_prelim_ref_cache, gfp_mask); 3828da6d581SJan Schmidt if (!ref) 3838da6d581SJan Schmidt return -ENOMEM; 3848da6d581SJan Schmidt 3858da6d581SJan Schmidt ref->root_id = root_id; 3867ac8b88eSethanwu if (key) 387d5c88b73SJan Schmidt ref->key_for_search = *key; 3887ac8b88eSethanwu else 389d5c88b73SJan Schmidt memset(&ref->key_for_search, 0, sizeof(ref->key_for_search)); 3908da6d581SJan Schmidt 3913301958bSJan Schmidt ref->inode_list = NULL; 3928da6d581SJan Schmidt ref->level = level; 3938da6d581SJan Schmidt ref->count = count; 3948da6d581SJan Schmidt ref->parent = parent; 3958da6d581SJan Schmidt ref->wanted_disk_byte = wanted_disk_byte; 3963ec4d323SEdmund Nadolski prelim_ref_insert(fs_info, preftree, ref, sc); 3973ec4d323SEdmund Nadolski return extent_is_shared(sc); 3988da6d581SJan Schmidt } 3998da6d581SJan Schmidt 40086d5f994SEdmund Nadolski /* direct refs use root == 0, key == NULL */ 40100142756SJeff Mahoney static int add_direct_ref(const struct btrfs_fs_info *fs_info, 40200142756SJeff Mahoney struct preftrees *preftrees, int level, u64 parent, 4033ec4d323SEdmund Nadolski u64 wanted_disk_byte, int count, 4043ec4d323SEdmund Nadolski struct share_check *sc, gfp_t gfp_mask) 40586d5f994SEdmund Nadolski { 40600142756SJeff Mahoney return add_prelim_ref(fs_info, &preftrees->direct, 0, NULL, level, 4073ec4d323SEdmund Nadolski parent, wanted_disk_byte, count, sc, gfp_mask); 40886d5f994SEdmund Nadolski } 40986d5f994SEdmund Nadolski 41086d5f994SEdmund Nadolski /* indirect refs use parent == 0 */ 41100142756SJeff Mahoney static int add_indirect_ref(const struct btrfs_fs_info *fs_info, 41200142756SJeff Mahoney struct preftrees *preftrees, u64 root_id, 41386d5f994SEdmund Nadolski const struct btrfs_key *key, int level, 4143ec4d323SEdmund Nadolski u64 wanted_disk_byte, int count, 4153ec4d323SEdmund Nadolski struct share_check *sc, gfp_t gfp_mask) 41686d5f994SEdmund Nadolski { 41786d5f994SEdmund Nadolski struct preftree *tree = &preftrees->indirect; 41886d5f994SEdmund Nadolski 41986d5f994SEdmund Nadolski if (!key) 42086d5f994SEdmund Nadolski tree = &preftrees->indirect_missing_keys; 42100142756SJeff Mahoney return add_prelim_ref(fs_info, tree, root_id, key, level, 0, 4223ec4d323SEdmund Nadolski wanted_disk_byte, count, sc, gfp_mask); 42386d5f994SEdmund Nadolski } 42486d5f994SEdmund Nadolski 425ed58f2e6Sethanwu static int is_shared_data_backref(struct preftrees *preftrees, u64 bytenr) 426ed58f2e6Sethanwu { 427ed58f2e6Sethanwu struct rb_node **p = &preftrees->direct.root.rb_root.rb_node; 428ed58f2e6Sethanwu struct rb_node *parent = NULL; 429ed58f2e6Sethanwu struct prelim_ref *ref = NULL; 4309c6c723fSArnd Bergmann struct prelim_ref target = {}; 431ed58f2e6Sethanwu int result; 432ed58f2e6Sethanwu 433ed58f2e6Sethanwu target.parent = bytenr; 434ed58f2e6Sethanwu 435ed58f2e6Sethanwu while (*p) { 436ed58f2e6Sethanwu parent = *p; 437ed58f2e6Sethanwu ref = rb_entry(parent, struct prelim_ref, rbnode); 438ed58f2e6Sethanwu result = prelim_ref_compare(ref, &target); 439ed58f2e6Sethanwu 440ed58f2e6Sethanwu if (result < 0) 441ed58f2e6Sethanwu p = &(*p)->rb_left; 442ed58f2e6Sethanwu else if (result > 0) 443ed58f2e6Sethanwu p = &(*p)->rb_right; 444ed58f2e6Sethanwu else 445ed58f2e6Sethanwu return 1; 446ed58f2e6Sethanwu } 447ed58f2e6Sethanwu return 0; 448ed58f2e6Sethanwu } 449ed58f2e6Sethanwu 4508da6d581SJan Schmidt static int add_all_parents(struct btrfs_root *root, struct btrfs_path *path, 451ed58f2e6Sethanwu struct ulist *parents, 452ed58f2e6Sethanwu struct preftrees *preftrees, struct prelim_ref *ref, 45344853868SJosef Bacik int level, u64 time_seq, const u64 *extent_item_pos, 454b25b0b87Sethanwu bool ignore_offset) 4558da6d581SJan Schmidt { 45669bca40dSAlexander Block int ret = 0; 45769bca40dSAlexander Block int slot; 45869bca40dSAlexander Block struct extent_buffer *eb; 45969bca40dSAlexander Block struct btrfs_key key; 4607ef81ac8SJosef Bacik struct btrfs_key *key_for_search = &ref->key_for_search; 4618da6d581SJan Schmidt struct btrfs_file_extent_item *fi; 462ed8c4913SJosef Bacik struct extent_inode_elem *eie = NULL, *old = NULL; 4638da6d581SJan Schmidt u64 disk_byte; 4647ef81ac8SJosef Bacik u64 wanted_disk_byte = ref->wanted_disk_byte; 4657ef81ac8SJosef Bacik u64 count = 0; 4667ac8b88eSethanwu u64 data_offset; 4678da6d581SJan Schmidt 46869bca40dSAlexander Block if (level != 0) { 46969bca40dSAlexander Block eb = path->nodes[level]; 47069bca40dSAlexander Block ret = ulist_add(parents, eb->start, 0, GFP_NOFS); 4713301958bSJan Schmidt if (ret < 0) 4723301958bSJan Schmidt return ret; 4738da6d581SJan Schmidt return 0; 47469bca40dSAlexander Block } 4758da6d581SJan Schmidt 4768da6d581SJan Schmidt /* 477ed58f2e6Sethanwu * 1. We normally enter this function with the path already pointing to 47869bca40dSAlexander Block * the first item to check. But sometimes, we may enter it with 479ed58f2e6Sethanwu * slot == nritems. 480ed58f2e6Sethanwu * 2. We are searching for normal backref but bytenr of this leaf 481ed58f2e6Sethanwu * matches shared data backref 482cfc0eed0Sethanwu * 3. The leaf owner is not equal to the root we are searching 483cfc0eed0Sethanwu * 484ed58f2e6Sethanwu * For these cases, go to the next leaf before we continue. 4858da6d581SJan Schmidt */ 486ed58f2e6Sethanwu eb = path->nodes[0]; 487ed58f2e6Sethanwu if (path->slots[0] >= btrfs_header_nritems(eb) || 488cfc0eed0Sethanwu is_shared_data_backref(preftrees, eb->start) || 489cfc0eed0Sethanwu ref->root_id != btrfs_header_owner(eb)) { 490f3a84ccdSFilipe Manana if (time_seq == BTRFS_SEQ_LAST) 49121633fc6SQu Wenruo ret = btrfs_next_leaf(root, path); 49221633fc6SQu Wenruo else 4933d7806ecSJan Schmidt ret = btrfs_next_old_leaf(root, path, time_seq); 49421633fc6SQu Wenruo } 4958da6d581SJan Schmidt 496b25b0b87Sethanwu while (!ret && count < ref->count) { 4978da6d581SJan Schmidt eb = path->nodes[0]; 49869bca40dSAlexander Block slot = path->slots[0]; 49969bca40dSAlexander Block 50069bca40dSAlexander Block btrfs_item_key_to_cpu(eb, &key, slot); 50169bca40dSAlexander Block 50269bca40dSAlexander Block if (key.objectid != key_for_search->objectid || 50369bca40dSAlexander Block key.type != BTRFS_EXTENT_DATA_KEY) 50469bca40dSAlexander Block break; 50569bca40dSAlexander Block 506ed58f2e6Sethanwu /* 507ed58f2e6Sethanwu * We are searching for normal backref but bytenr of this leaf 508cfc0eed0Sethanwu * matches shared data backref, OR 509cfc0eed0Sethanwu * the leaf owner is not equal to the root we are searching for 510ed58f2e6Sethanwu */ 511cfc0eed0Sethanwu if (slot == 0 && 512cfc0eed0Sethanwu (is_shared_data_backref(preftrees, eb->start) || 513cfc0eed0Sethanwu ref->root_id != btrfs_header_owner(eb))) { 514f3a84ccdSFilipe Manana if (time_seq == BTRFS_SEQ_LAST) 515ed58f2e6Sethanwu ret = btrfs_next_leaf(root, path); 516ed58f2e6Sethanwu else 517ed58f2e6Sethanwu ret = btrfs_next_old_leaf(root, path, time_seq); 518ed58f2e6Sethanwu continue; 519ed58f2e6Sethanwu } 52069bca40dSAlexander Block fi = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item); 5218da6d581SJan Schmidt disk_byte = btrfs_file_extent_disk_bytenr(eb, fi); 5227ac8b88eSethanwu data_offset = btrfs_file_extent_offset(eb, fi); 52369bca40dSAlexander Block 52469bca40dSAlexander Block if (disk_byte == wanted_disk_byte) { 52569bca40dSAlexander Block eie = NULL; 526ed8c4913SJosef Bacik old = NULL; 5277ac8b88eSethanwu if (ref->key_for_search.offset == key.offset - data_offset) 5287ef81ac8SJosef Bacik count++; 5297ac8b88eSethanwu else 5307ac8b88eSethanwu goto next; 53169bca40dSAlexander Block if (extent_item_pos) { 53269bca40dSAlexander Block ret = check_extent_in_eb(&key, eb, fi, 53369bca40dSAlexander Block *extent_item_pos, 534c995ab3cSZygo Blaxell &eie, ignore_offset); 53569bca40dSAlexander Block if (ret < 0) 53669bca40dSAlexander Block break; 5378da6d581SJan Schmidt } 538ed8c4913SJosef Bacik if (ret > 0) 539ed8c4913SJosef Bacik goto next; 5404eb1f66dSTakashi Iwai ret = ulist_add_merge_ptr(parents, eb->start, 5414eb1f66dSTakashi Iwai eie, (void **)&old, GFP_NOFS); 54269bca40dSAlexander Block if (ret < 0) 54369bca40dSAlexander Block break; 544ed8c4913SJosef Bacik if (!ret && extent_item_pos) { 545ed8c4913SJosef Bacik while (old->next) 546ed8c4913SJosef Bacik old = old->next; 547ed8c4913SJosef Bacik old->next = eie; 54869bca40dSAlexander Block } 549f05c4746SWang Shilong eie = NULL; 55069bca40dSAlexander Block } 551ed8c4913SJosef Bacik next: 552f3a84ccdSFilipe Manana if (time_seq == BTRFS_SEQ_LAST) 55321633fc6SQu Wenruo ret = btrfs_next_item(root, path); 55421633fc6SQu Wenruo else 55569bca40dSAlexander Block ret = btrfs_next_old_item(root, path, time_seq); 5568da6d581SJan Schmidt } 5578da6d581SJan Schmidt 55869bca40dSAlexander Block if (ret > 0) 55969bca40dSAlexander Block ret = 0; 560f05c4746SWang Shilong else if (ret < 0) 561f05c4746SWang Shilong free_inode_elem_list(eie); 56269bca40dSAlexander Block return ret; 5638da6d581SJan Schmidt } 5648da6d581SJan Schmidt 5658da6d581SJan Schmidt /* 5668da6d581SJan Schmidt * resolve an indirect backref in the form (root_id, key, level) 5678da6d581SJan Schmidt * to a logical address 5688da6d581SJan Schmidt */ 569e0c476b1SJeff Mahoney static int resolve_indirect_ref(struct btrfs_fs_info *fs_info, 570da61d31aSJosef Bacik struct btrfs_path *path, u64 time_seq, 571ed58f2e6Sethanwu struct preftrees *preftrees, 572e0c476b1SJeff Mahoney struct prelim_ref *ref, struct ulist *parents, 573b25b0b87Sethanwu const u64 *extent_item_pos, bool ignore_offset) 5748da6d581SJan Schmidt { 5758da6d581SJan Schmidt struct btrfs_root *root; 5768da6d581SJan Schmidt struct extent_buffer *eb; 5778da6d581SJan Schmidt int ret = 0; 5788da6d581SJan Schmidt int root_level; 5798da6d581SJan Schmidt int level = ref->level; 5807ac8b88eSethanwu struct btrfs_key search_key = ref->key_for_search; 5818da6d581SJan Schmidt 58249d11beaSJosef Bacik /* 58349d11beaSJosef Bacik * If we're search_commit_root we could possibly be holding locks on 58449d11beaSJosef Bacik * other tree nodes. This happens when qgroups does backref walks when 58549d11beaSJosef Bacik * adding new delayed refs. To deal with this we need to look in cache 58649d11beaSJosef Bacik * for the root, and if we don't find it then we need to search the 58749d11beaSJosef Bacik * tree_root's commit root, thus the btrfs_get_fs_root_commit_root usage 58849d11beaSJosef Bacik * here. 58949d11beaSJosef Bacik */ 59049d11beaSJosef Bacik if (path->search_commit_root) 59149d11beaSJosef Bacik root = btrfs_get_fs_root_commit_root(fs_info, path, ref->root_id); 59249d11beaSJosef Bacik else 59356e9357aSDavid Sterba root = btrfs_get_fs_root(fs_info, ref->root_id, false); 5948da6d581SJan Schmidt if (IS_ERR(root)) { 5958da6d581SJan Schmidt ret = PTR_ERR(root); 5969326f76fSJosef Bacik goto out_free; 5979326f76fSJosef Bacik } 5989326f76fSJosef Bacik 59939dba873SJosef Bacik if (!path->search_commit_root && 60039dba873SJosef Bacik test_bit(BTRFS_ROOT_DELETING, &root->state)) { 60139dba873SJosef Bacik ret = -ENOENT; 60239dba873SJosef Bacik goto out; 60339dba873SJosef Bacik } 60439dba873SJosef Bacik 605f5ee5c9aSJeff Mahoney if (btrfs_is_testing(fs_info)) { 606d9ee522bSJosef Bacik ret = -ENOENT; 607d9ee522bSJosef Bacik goto out; 608d9ee522bSJosef Bacik } 609d9ee522bSJosef Bacik 6109e351cc8SJosef Bacik if (path->search_commit_root) 6119e351cc8SJosef Bacik root_level = btrfs_header_level(root->commit_root); 612f3a84ccdSFilipe Manana else if (time_seq == BTRFS_SEQ_LAST) 61321633fc6SQu Wenruo root_level = btrfs_header_level(root->node); 6149e351cc8SJosef Bacik else 6155b6602e7SJan Schmidt root_level = btrfs_old_root_level(root, time_seq); 6168da6d581SJan Schmidt 617c75e8394SJosef Bacik if (root_level + 1 == level) 6188da6d581SJan Schmidt goto out; 6198da6d581SJan Schmidt 6207ac8b88eSethanwu /* 6217ac8b88eSethanwu * We can often find data backrefs with an offset that is too large 6227ac8b88eSethanwu * (>= LLONG_MAX, maximum allowed file offset) due to underflows when 6237ac8b88eSethanwu * subtracting a file's offset with the data offset of its 6247ac8b88eSethanwu * corresponding extent data item. This can happen for example in the 6257ac8b88eSethanwu * clone ioctl. 6267ac8b88eSethanwu * 6277ac8b88eSethanwu * So if we detect such case we set the search key's offset to zero to 6287ac8b88eSethanwu * make sure we will find the matching file extent item at 6297ac8b88eSethanwu * add_all_parents(), otherwise we will miss it because the offset 6307ac8b88eSethanwu * taken form the backref is much larger then the offset of the file 6317ac8b88eSethanwu * extent item. This can make us scan a very large number of file 6327ac8b88eSethanwu * extent items, but at least it will not make us miss any. 6337ac8b88eSethanwu * 6347ac8b88eSethanwu * This is an ugly workaround for a behaviour that should have never 6357ac8b88eSethanwu * existed, but it does and a fix for the clone ioctl would touch a lot 6367ac8b88eSethanwu * of places, cause backwards incompatibility and would not fix the 6377ac8b88eSethanwu * problem for extents cloned with older kernels. 6387ac8b88eSethanwu */ 6397ac8b88eSethanwu if (search_key.type == BTRFS_EXTENT_DATA_KEY && 6407ac8b88eSethanwu search_key.offset >= LLONG_MAX) 6417ac8b88eSethanwu search_key.offset = 0; 6428da6d581SJan Schmidt path->lowest_level = level; 643f3a84ccdSFilipe Manana if (time_seq == BTRFS_SEQ_LAST) 6447ac8b88eSethanwu ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0); 64521633fc6SQu Wenruo else 6467ac8b88eSethanwu ret = btrfs_search_old_slot(root, &search_key, path, time_seq); 647538f72cdSWang Shilong 648ab8d0fc4SJeff Mahoney btrfs_debug(fs_info, 649ab8d0fc4SJeff Mahoney "search slot in root %llu (level %d, ref count %d) returned %d for key (%llu %u %llu)", 650c1c9ff7cSGeert Uytterhoeven ref->root_id, level, ref->count, ret, 651c1c9ff7cSGeert Uytterhoeven ref->key_for_search.objectid, ref->key_for_search.type, 652c1c9ff7cSGeert Uytterhoeven ref->key_for_search.offset); 6538da6d581SJan Schmidt if (ret < 0) 6548da6d581SJan Schmidt goto out; 6558da6d581SJan Schmidt 6568da6d581SJan Schmidt eb = path->nodes[level]; 6579345457fSJan Schmidt while (!eb) { 658fae7f21cSDulshani Gunawardhana if (WARN_ON(!level)) { 6598da6d581SJan Schmidt ret = 1; 6608da6d581SJan Schmidt goto out; 6618da6d581SJan Schmidt } 6629345457fSJan Schmidt level--; 6639345457fSJan Schmidt eb = path->nodes[level]; 6649345457fSJan Schmidt } 6658da6d581SJan Schmidt 666ed58f2e6Sethanwu ret = add_all_parents(root, path, parents, preftrees, ref, level, 667b25b0b87Sethanwu time_seq, extent_item_pos, ignore_offset); 6688da6d581SJan Schmidt out: 66900246528SJosef Bacik btrfs_put_root(root); 6709326f76fSJosef Bacik out_free: 671da61d31aSJosef Bacik path->lowest_level = 0; 672da61d31aSJosef Bacik btrfs_release_path(path); 6738da6d581SJan Schmidt return ret; 6748da6d581SJan Schmidt } 6758da6d581SJan Schmidt 6764dae077aSJeff Mahoney static struct extent_inode_elem * 6774dae077aSJeff Mahoney unode_aux_to_inode_list(struct ulist_node *node) 6784dae077aSJeff Mahoney { 6794dae077aSJeff Mahoney if (!node) 6804dae077aSJeff Mahoney return NULL; 6814dae077aSJeff Mahoney return (struct extent_inode_elem *)(uintptr_t)node->aux; 6824dae077aSJeff Mahoney } 6834dae077aSJeff Mahoney 6845614dc3aSFilipe Manana static void free_leaf_list(struct ulist *ulist) 6855614dc3aSFilipe Manana { 6865614dc3aSFilipe Manana struct ulist_node *node; 6875614dc3aSFilipe Manana struct ulist_iterator uiter; 6885614dc3aSFilipe Manana 6895614dc3aSFilipe Manana ULIST_ITER_INIT(&uiter); 6905614dc3aSFilipe Manana while ((node = ulist_next(ulist, &uiter))) 6915614dc3aSFilipe Manana free_inode_elem_list(unode_aux_to_inode_list(node)); 6925614dc3aSFilipe Manana 6935614dc3aSFilipe Manana ulist_free(ulist); 6945614dc3aSFilipe Manana } 6955614dc3aSFilipe Manana 6968da6d581SJan Schmidt /* 69752042d8eSAndrea Gelmini * We maintain three separate rbtrees: one for direct refs, one for 69886d5f994SEdmund Nadolski * indirect refs which have a key, and one for indirect refs which do not 69986d5f994SEdmund Nadolski * have a key. Each tree does merge on insertion. 70086d5f994SEdmund Nadolski * 70186d5f994SEdmund Nadolski * Once all of the references are located, we iterate over the tree of 70286d5f994SEdmund Nadolski * indirect refs with missing keys. An appropriate key is located and 70386d5f994SEdmund Nadolski * the ref is moved onto the tree for indirect refs. After all missing 70486d5f994SEdmund Nadolski * keys are thus located, we iterate over the indirect ref tree, resolve 70586d5f994SEdmund Nadolski * each reference, and then insert the resolved reference onto the 70686d5f994SEdmund Nadolski * direct tree (merging there too). 70786d5f994SEdmund Nadolski * 70886d5f994SEdmund Nadolski * New backrefs (i.e., for parent nodes) are added to the appropriate 70986d5f994SEdmund Nadolski * rbtree as they are encountered. The new backrefs are subsequently 71086d5f994SEdmund Nadolski * resolved as above. 7118da6d581SJan Schmidt */ 712e0c476b1SJeff Mahoney static int resolve_indirect_refs(struct btrfs_fs_info *fs_info, 713da61d31aSJosef Bacik struct btrfs_path *path, u64 time_seq, 71486d5f994SEdmund Nadolski struct preftrees *preftrees, 715b25b0b87Sethanwu const u64 *extent_item_pos, 716c995ab3cSZygo Blaxell struct share_check *sc, bool ignore_offset) 7178da6d581SJan Schmidt { 7188da6d581SJan Schmidt int err; 7198da6d581SJan Schmidt int ret = 0; 7208da6d581SJan Schmidt struct ulist *parents; 7218da6d581SJan Schmidt struct ulist_node *node; 722cd1b413cSJan Schmidt struct ulist_iterator uiter; 72386d5f994SEdmund Nadolski struct rb_node *rnode; 7248da6d581SJan Schmidt 7258da6d581SJan Schmidt parents = ulist_alloc(GFP_NOFS); 7268da6d581SJan Schmidt if (!parents) 7278da6d581SJan Schmidt return -ENOMEM; 7288da6d581SJan Schmidt 7298da6d581SJan Schmidt /* 73086d5f994SEdmund Nadolski * We could trade memory usage for performance here by iterating 73186d5f994SEdmund Nadolski * the tree, allocating new refs for each insertion, and then 73286d5f994SEdmund Nadolski * freeing the entire indirect tree when we're done. In some test 73386d5f994SEdmund Nadolski * cases, the tree can grow quite large (~200k objects). 7348da6d581SJan Schmidt */ 735ecf160b4SLiu Bo while ((rnode = rb_first_cached(&preftrees->indirect.root))) { 73686d5f994SEdmund Nadolski struct prelim_ref *ref; 73786d5f994SEdmund Nadolski 73886d5f994SEdmund Nadolski ref = rb_entry(rnode, struct prelim_ref, rbnode); 73986d5f994SEdmund Nadolski if (WARN(ref->parent, 74086d5f994SEdmund Nadolski "BUG: direct ref found in indirect tree")) { 74186d5f994SEdmund Nadolski ret = -EINVAL; 74286d5f994SEdmund Nadolski goto out; 74386d5f994SEdmund Nadolski } 74486d5f994SEdmund Nadolski 745ecf160b4SLiu Bo rb_erase_cached(&ref->rbnode, &preftrees->indirect.root); 7466c336b21SJeff Mahoney preftrees->indirect.count--; 74786d5f994SEdmund Nadolski 74886d5f994SEdmund Nadolski if (ref->count == 0) { 74986d5f994SEdmund Nadolski free_pref(ref); 7508da6d581SJan Schmidt continue; 75186d5f994SEdmund Nadolski } 75286d5f994SEdmund Nadolski 753877c1476SFilipe Manana if (sc && ref->root_id != sc->root->root_key.objectid) { 75486d5f994SEdmund Nadolski free_pref(ref); 755dc046b10SJosef Bacik ret = BACKREF_FOUND_SHARED; 756dc046b10SJosef Bacik goto out; 757dc046b10SJosef Bacik } 758ed58f2e6Sethanwu err = resolve_indirect_ref(fs_info, path, time_seq, preftrees, 759ed58f2e6Sethanwu ref, parents, extent_item_pos, 760b25b0b87Sethanwu ignore_offset); 76195def2edSWang Shilong /* 76295def2edSWang Shilong * we can only tolerate ENOENT,otherwise,we should catch error 76395def2edSWang Shilong * and return directly. 76495def2edSWang Shilong */ 76595def2edSWang Shilong if (err == -ENOENT) { 7663ec4d323SEdmund Nadolski prelim_ref_insert(fs_info, &preftrees->direct, ref, 7673ec4d323SEdmund Nadolski NULL); 7688da6d581SJan Schmidt continue; 76995def2edSWang Shilong } else if (err) { 77086d5f994SEdmund Nadolski free_pref(ref); 77195def2edSWang Shilong ret = err; 77295def2edSWang Shilong goto out; 77395def2edSWang Shilong } 7748da6d581SJan Schmidt 7758da6d581SJan Schmidt /* we put the first parent into the ref at hand */ 776cd1b413cSJan Schmidt ULIST_ITER_INIT(&uiter); 777cd1b413cSJan Schmidt node = ulist_next(parents, &uiter); 7788da6d581SJan Schmidt ref->parent = node ? node->val : 0; 7794dae077aSJeff Mahoney ref->inode_list = unode_aux_to_inode_list(node); 7808da6d581SJan Schmidt 78186d5f994SEdmund Nadolski /* Add a prelim_ref(s) for any other parent(s). */ 782cd1b413cSJan Schmidt while ((node = ulist_next(parents, &uiter))) { 78386d5f994SEdmund Nadolski struct prelim_ref *new_ref; 78486d5f994SEdmund Nadolski 785b9e9a6cbSWang Shilong new_ref = kmem_cache_alloc(btrfs_prelim_ref_cache, 786b9e9a6cbSWang Shilong GFP_NOFS); 7878da6d581SJan Schmidt if (!new_ref) { 78886d5f994SEdmund Nadolski free_pref(ref); 7898da6d581SJan Schmidt ret = -ENOMEM; 790e36902d4SWang Shilong goto out; 7918da6d581SJan Schmidt } 7928da6d581SJan Schmidt memcpy(new_ref, ref, sizeof(*ref)); 7938da6d581SJan Schmidt new_ref->parent = node->val; 7944dae077aSJeff Mahoney new_ref->inode_list = unode_aux_to_inode_list(node); 7953ec4d323SEdmund Nadolski prelim_ref_insert(fs_info, &preftrees->direct, 7963ec4d323SEdmund Nadolski new_ref, NULL); 7978da6d581SJan Schmidt } 79886d5f994SEdmund Nadolski 7993ec4d323SEdmund Nadolski /* 80052042d8eSAndrea Gelmini * Now it's a direct ref, put it in the direct tree. We must 8013ec4d323SEdmund Nadolski * do this last because the ref could be merged/freed here. 8023ec4d323SEdmund Nadolski */ 8033ec4d323SEdmund Nadolski prelim_ref_insert(fs_info, &preftrees->direct, ref, NULL); 80486d5f994SEdmund Nadolski 8058da6d581SJan Schmidt ulist_reinit(parents); 8069dd14fd6SEdmund Nadolski cond_resched(); 8078da6d581SJan Schmidt } 808e36902d4SWang Shilong out: 8095614dc3aSFilipe Manana /* 8105614dc3aSFilipe Manana * We may have inode lists attached to refs in the parents ulist, so we 8115614dc3aSFilipe Manana * must free them before freeing the ulist and its refs. 8125614dc3aSFilipe Manana */ 8135614dc3aSFilipe Manana free_leaf_list(parents); 8148da6d581SJan Schmidt return ret; 8158da6d581SJan Schmidt } 8168da6d581SJan Schmidt 817d5c88b73SJan Schmidt /* 818d5c88b73SJan Schmidt * read tree blocks and add keys where required. 819d5c88b73SJan Schmidt */ 820e0c476b1SJeff Mahoney static int add_missing_keys(struct btrfs_fs_info *fs_info, 82138e3eebfSJosef Bacik struct preftrees *preftrees, bool lock) 822d5c88b73SJan Schmidt { 823e0c476b1SJeff Mahoney struct prelim_ref *ref; 824d5c88b73SJan Schmidt struct extent_buffer *eb; 82586d5f994SEdmund Nadolski struct preftree *tree = &preftrees->indirect_missing_keys; 82686d5f994SEdmund Nadolski struct rb_node *node; 827d5c88b73SJan Schmidt 828ecf160b4SLiu Bo while ((node = rb_first_cached(&tree->root))) { 82986d5f994SEdmund Nadolski ref = rb_entry(node, struct prelim_ref, rbnode); 830ecf160b4SLiu Bo rb_erase_cached(node, &tree->root); 83186d5f994SEdmund Nadolski 83286d5f994SEdmund Nadolski BUG_ON(ref->parent); /* should not be a direct ref */ 83386d5f994SEdmund Nadolski BUG_ON(ref->key_for_search.type); 834d5c88b73SJan Schmidt BUG_ON(!ref->wanted_disk_byte); 83586d5f994SEdmund Nadolski 8361b7ec85eSJosef Bacik eb = read_tree_block(fs_info, ref->wanted_disk_byte, 8371b7ec85eSJosef Bacik ref->root_id, 0, ref->level - 1, NULL); 83864c043deSLiu Bo if (IS_ERR(eb)) { 83986d5f994SEdmund Nadolski free_pref(ref); 84064c043deSLiu Bo return PTR_ERR(eb); 8414eb150d6SQu Wenruo } 8424eb150d6SQu Wenruo if (!extent_buffer_uptodate(eb)) { 84386d5f994SEdmund Nadolski free_pref(ref); 844416bc658SJosef Bacik free_extent_buffer(eb); 845416bc658SJosef Bacik return -EIO; 846416bc658SJosef Bacik } 8474eb150d6SQu Wenruo 84838e3eebfSJosef Bacik if (lock) 849d5c88b73SJan Schmidt btrfs_tree_read_lock(eb); 850d5c88b73SJan Schmidt if (btrfs_header_level(eb) == 0) 851d5c88b73SJan Schmidt btrfs_item_key_to_cpu(eb, &ref->key_for_search, 0); 852d5c88b73SJan Schmidt else 853d5c88b73SJan Schmidt btrfs_node_key_to_cpu(eb, &ref->key_for_search, 0); 85438e3eebfSJosef Bacik if (lock) 855d5c88b73SJan Schmidt btrfs_tree_read_unlock(eb); 856d5c88b73SJan Schmidt free_extent_buffer(eb); 8573ec4d323SEdmund Nadolski prelim_ref_insert(fs_info, &preftrees->indirect, ref, NULL); 8589dd14fd6SEdmund Nadolski cond_resched(); 859d5c88b73SJan Schmidt } 860d5c88b73SJan Schmidt return 0; 861d5c88b73SJan Schmidt } 862d5c88b73SJan Schmidt 8638da6d581SJan Schmidt /* 8648da6d581SJan Schmidt * add all currently queued delayed refs from this head whose seq nr is 8658da6d581SJan Schmidt * smaller or equal that seq to the list 8668da6d581SJan Schmidt */ 86700142756SJeff Mahoney static int add_delayed_refs(const struct btrfs_fs_info *fs_info, 86800142756SJeff Mahoney struct btrfs_delayed_ref_head *head, u64 seq, 869b25b0b87Sethanwu struct preftrees *preftrees, struct share_check *sc) 8708da6d581SJan Schmidt { 871c6fc2454SQu Wenruo struct btrfs_delayed_ref_node *node; 872d5c88b73SJan Schmidt struct btrfs_key key; 8730e0adbcfSJosef Bacik struct rb_node *n; 87401747e92SEdmund Nadolski int count; 875b1375d64SJan Schmidt int ret = 0; 8768da6d581SJan Schmidt 877d7df2c79SJosef Bacik spin_lock(&head->lock); 878e3d03965SLiu Bo for (n = rb_first_cached(&head->ref_tree); n; n = rb_next(n)) { 8790e0adbcfSJosef Bacik node = rb_entry(n, struct btrfs_delayed_ref_node, 8800e0adbcfSJosef Bacik ref_node); 8818da6d581SJan Schmidt if (node->seq > seq) 8828da6d581SJan Schmidt continue; 8838da6d581SJan Schmidt 8848da6d581SJan Schmidt switch (node->action) { 8858da6d581SJan Schmidt case BTRFS_ADD_DELAYED_EXTENT: 8868da6d581SJan Schmidt case BTRFS_UPDATE_DELAYED_HEAD: 8878da6d581SJan Schmidt WARN_ON(1); 8888da6d581SJan Schmidt continue; 8898da6d581SJan Schmidt case BTRFS_ADD_DELAYED_REF: 89001747e92SEdmund Nadolski count = node->ref_mod; 8918da6d581SJan Schmidt break; 8928da6d581SJan Schmidt case BTRFS_DROP_DELAYED_REF: 89301747e92SEdmund Nadolski count = node->ref_mod * -1; 8948da6d581SJan Schmidt break; 8958da6d581SJan Schmidt default: 896290342f6SArnd Bergmann BUG(); 8978da6d581SJan Schmidt } 8988da6d581SJan Schmidt switch (node->type) { 8998da6d581SJan Schmidt case BTRFS_TREE_BLOCK_REF_KEY: { 90086d5f994SEdmund Nadolski /* NORMAL INDIRECT METADATA backref */ 9018da6d581SJan Schmidt struct btrfs_delayed_tree_ref *ref; 902943553efSFilipe Manana struct btrfs_key *key_ptr = NULL; 903943553efSFilipe Manana 904943553efSFilipe Manana if (head->extent_op && head->extent_op->update_key) { 905943553efSFilipe Manana btrfs_disk_key_to_cpu(&key, &head->extent_op->key); 906943553efSFilipe Manana key_ptr = &key; 907943553efSFilipe Manana } 9088da6d581SJan Schmidt 9098da6d581SJan Schmidt ref = btrfs_delayed_node_to_tree_ref(node); 91000142756SJeff Mahoney ret = add_indirect_ref(fs_info, preftrees, ref->root, 911943553efSFilipe Manana key_ptr, ref->level + 1, 91201747e92SEdmund Nadolski node->bytenr, count, sc, 91301747e92SEdmund Nadolski GFP_ATOMIC); 9148da6d581SJan Schmidt break; 9158da6d581SJan Schmidt } 9168da6d581SJan Schmidt case BTRFS_SHARED_BLOCK_REF_KEY: { 91786d5f994SEdmund Nadolski /* SHARED DIRECT METADATA backref */ 9188da6d581SJan Schmidt struct btrfs_delayed_tree_ref *ref; 9198da6d581SJan Schmidt 9208da6d581SJan Schmidt ref = btrfs_delayed_node_to_tree_ref(node); 92186d5f994SEdmund Nadolski 92201747e92SEdmund Nadolski ret = add_direct_ref(fs_info, preftrees, ref->level + 1, 92301747e92SEdmund Nadolski ref->parent, node->bytenr, count, 9243ec4d323SEdmund Nadolski sc, GFP_ATOMIC); 9258da6d581SJan Schmidt break; 9268da6d581SJan Schmidt } 9278da6d581SJan Schmidt case BTRFS_EXTENT_DATA_REF_KEY: { 92886d5f994SEdmund Nadolski /* NORMAL INDIRECT DATA backref */ 9298da6d581SJan Schmidt struct btrfs_delayed_data_ref *ref; 9308da6d581SJan Schmidt ref = btrfs_delayed_node_to_data_ref(node); 9318da6d581SJan Schmidt 9328da6d581SJan Schmidt key.objectid = ref->objectid; 9338da6d581SJan Schmidt key.type = BTRFS_EXTENT_DATA_KEY; 9348da6d581SJan Schmidt key.offset = ref->offset; 935dc046b10SJosef Bacik 936dc046b10SJosef Bacik /* 9374fc7b572SFilipe Manana * If we have a share check context and a reference for 9384fc7b572SFilipe Manana * another inode, we can't exit immediately. This is 9394fc7b572SFilipe Manana * because even if this is a BTRFS_ADD_DELAYED_REF 9404fc7b572SFilipe Manana * reference we may find next a BTRFS_DROP_DELAYED_REF 9414fc7b572SFilipe Manana * which cancels out this ADD reference. 9424fc7b572SFilipe Manana * 9434fc7b572SFilipe Manana * If this is a DROP reference and there was no previous 9444fc7b572SFilipe Manana * ADD reference, then we need to signal that when we 9454fc7b572SFilipe Manana * process references from the extent tree (through 9464fc7b572SFilipe Manana * add_inline_refs() and add_keyed_refs()), we should 9474fc7b572SFilipe Manana * not exit early if we find a reference for another 9484fc7b572SFilipe Manana * inode, because one of the delayed DROP references 9494fc7b572SFilipe Manana * may cancel that reference in the extent tree. 950dc046b10SJosef Bacik */ 9514fc7b572SFilipe Manana if (sc && count < 0) 9524fc7b572SFilipe Manana sc->have_delayed_delete_refs = true; 953dc046b10SJosef Bacik 95400142756SJeff Mahoney ret = add_indirect_ref(fs_info, preftrees, ref->root, 95501747e92SEdmund Nadolski &key, 0, node->bytenr, count, sc, 95601747e92SEdmund Nadolski GFP_ATOMIC); 9578da6d581SJan Schmidt break; 9588da6d581SJan Schmidt } 9598da6d581SJan Schmidt case BTRFS_SHARED_DATA_REF_KEY: { 96086d5f994SEdmund Nadolski /* SHARED DIRECT FULL backref */ 9618da6d581SJan Schmidt struct btrfs_delayed_data_ref *ref; 9628da6d581SJan Schmidt 9638da6d581SJan Schmidt ref = btrfs_delayed_node_to_data_ref(node); 96486d5f994SEdmund Nadolski 96501747e92SEdmund Nadolski ret = add_direct_ref(fs_info, preftrees, 0, ref->parent, 96601747e92SEdmund Nadolski node->bytenr, count, sc, 96701747e92SEdmund Nadolski GFP_ATOMIC); 9688da6d581SJan Schmidt break; 9698da6d581SJan Schmidt } 9708da6d581SJan Schmidt default: 9718da6d581SJan Schmidt WARN_ON(1); 9728da6d581SJan Schmidt } 9733ec4d323SEdmund Nadolski /* 9743ec4d323SEdmund Nadolski * We must ignore BACKREF_FOUND_SHARED until all delayed 9753ec4d323SEdmund Nadolski * refs have been checked. 9763ec4d323SEdmund Nadolski */ 9773ec4d323SEdmund Nadolski if (ret && (ret != BACKREF_FOUND_SHARED)) 978d7df2c79SJosef Bacik break; 9798da6d581SJan Schmidt } 9803ec4d323SEdmund Nadolski if (!ret) 9813ec4d323SEdmund Nadolski ret = extent_is_shared(sc); 9824fc7b572SFilipe Manana 983d7df2c79SJosef Bacik spin_unlock(&head->lock); 984d7df2c79SJosef Bacik return ret; 9858da6d581SJan Schmidt } 9868da6d581SJan Schmidt 9878da6d581SJan Schmidt /* 9888da6d581SJan Schmidt * add all inline backrefs for bytenr to the list 9893ec4d323SEdmund Nadolski * 9903ec4d323SEdmund Nadolski * Returns 0 on success, <0 on error, or BACKREF_FOUND_SHARED. 9918da6d581SJan Schmidt */ 99200142756SJeff Mahoney static int add_inline_refs(const struct btrfs_fs_info *fs_info, 99300142756SJeff Mahoney struct btrfs_path *path, u64 bytenr, 99486d5f994SEdmund Nadolski int *info_level, struct preftrees *preftrees, 995b25b0b87Sethanwu struct share_check *sc) 9968da6d581SJan Schmidt { 997b1375d64SJan Schmidt int ret = 0; 9988da6d581SJan Schmidt int slot; 9998da6d581SJan Schmidt struct extent_buffer *leaf; 10008da6d581SJan Schmidt struct btrfs_key key; 1001261c84b6SJosef Bacik struct btrfs_key found_key; 10028da6d581SJan Schmidt unsigned long ptr; 10038da6d581SJan Schmidt unsigned long end; 10048da6d581SJan Schmidt struct btrfs_extent_item *ei; 10058da6d581SJan Schmidt u64 flags; 10068da6d581SJan Schmidt u64 item_size; 10078da6d581SJan Schmidt 10088da6d581SJan Schmidt /* 10098da6d581SJan Schmidt * enumerate all inline refs 10108da6d581SJan Schmidt */ 10118da6d581SJan Schmidt leaf = path->nodes[0]; 1012dadcaf78SJan Schmidt slot = path->slots[0]; 10138da6d581SJan Schmidt 10143212fa14SJosef Bacik item_size = btrfs_item_size(leaf, slot); 10158da6d581SJan Schmidt BUG_ON(item_size < sizeof(*ei)); 10168da6d581SJan Schmidt 10178da6d581SJan Schmidt ei = btrfs_item_ptr(leaf, slot, struct btrfs_extent_item); 10188da6d581SJan Schmidt flags = btrfs_extent_flags(leaf, ei); 1019261c84b6SJosef Bacik btrfs_item_key_to_cpu(leaf, &found_key, slot); 10208da6d581SJan Schmidt 10218da6d581SJan Schmidt ptr = (unsigned long)(ei + 1); 10228da6d581SJan Schmidt end = (unsigned long)ei + item_size; 10238da6d581SJan Schmidt 1024261c84b6SJosef Bacik if (found_key.type == BTRFS_EXTENT_ITEM_KEY && 1025261c84b6SJosef Bacik flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) { 10268da6d581SJan Schmidt struct btrfs_tree_block_info *info; 10278da6d581SJan Schmidt 10288da6d581SJan Schmidt info = (struct btrfs_tree_block_info *)ptr; 10298da6d581SJan Schmidt *info_level = btrfs_tree_block_level(leaf, info); 10308da6d581SJan Schmidt ptr += sizeof(struct btrfs_tree_block_info); 10318da6d581SJan Schmidt BUG_ON(ptr > end); 1032261c84b6SJosef Bacik } else if (found_key.type == BTRFS_METADATA_ITEM_KEY) { 1033261c84b6SJosef Bacik *info_level = found_key.offset; 10348da6d581SJan Schmidt } else { 10358da6d581SJan Schmidt BUG_ON(!(flags & BTRFS_EXTENT_FLAG_DATA)); 10368da6d581SJan Schmidt } 10378da6d581SJan Schmidt 10388da6d581SJan Schmidt while (ptr < end) { 10398da6d581SJan Schmidt struct btrfs_extent_inline_ref *iref; 10408da6d581SJan Schmidt u64 offset; 10418da6d581SJan Schmidt int type; 10428da6d581SJan Schmidt 10438da6d581SJan Schmidt iref = (struct btrfs_extent_inline_ref *)ptr; 10443de28d57SLiu Bo type = btrfs_get_extent_inline_ref_type(leaf, iref, 10453de28d57SLiu Bo BTRFS_REF_TYPE_ANY); 10463de28d57SLiu Bo if (type == BTRFS_REF_TYPE_INVALID) 1047af431dcbSSu Yue return -EUCLEAN; 10483de28d57SLiu Bo 10498da6d581SJan Schmidt offset = btrfs_extent_inline_ref_offset(leaf, iref); 10508da6d581SJan Schmidt 10518da6d581SJan Schmidt switch (type) { 10528da6d581SJan Schmidt case BTRFS_SHARED_BLOCK_REF_KEY: 105300142756SJeff Mahoney ret = add_direct_ref(fs_info, preftrees, 105400142756SJeff Mahoney *info_level + 1, offset, 10553ec4d323SEdmund Nadolski bytenr, 1, NULL, GFP_NOFS); 10568da6d581SJan Schmidt break; 10578da6d581SJan Schmidt case BTRFS_SHARED_DATA_REF_KEY: { 10588da6d581SJan Schmidt struct btrfs_shared_data_ref *sdref; 10598da6d581SJan Schmidt int count; 10608da6d581SJan Schmidt 10618da6d581SJan Schmidt sdref = (struct btrfs_shared_data_ref *)(iref + 1); 10628da6d581SJan Schmidt count = btrfs_shared_data_ref_count(leaf, sdref); 106386d5f994SEdmund Nadolski 106400142756SJeff Mahoney ret = add_direct_ref(fs_info, preftrees, 0, offset, 10653ec4d323SEdmund Nadolski bytenr, count, sc, GFP_NOFS); 10668da6d581SJan Schmidt break; 10678da6d581SJan Schmidt } 10688da6d581SJan Schmidt case BTRFS_TREE_BLOCK_REF_KEY: 106900142756SJeff Mahoney ret = add_indirect_ref(fs_info, preftrees, offset, 107000142756SJeff Mahoney NULL, *info_level + 1, 10713ec4d323SEdmund Nadolski bytenr, 1, NULL, GFP_NOFS); 10728da6d581SJan Schmidt break; 10738da6d581SJan Schmidt case BTRFS_EXTENT_DATA_REF_KEY: { 10748da6d581SJan Schmidt struct btrfs_extent_data_ref *dref; 10758da6d581SJan Schmidt int count; 10768da6d581SJan Schmidt u64 root; 10778da6d581SJan Schmidt 10788da6d581SJan Schmidt dref = (struct btrfs_extent_data_ref *)(&iref->offset); 10798da6d581SJan Schmidt count = btrfs_extent_data_ref_count(leaf, dref); 10808da6d581SJan Schmidt key.objectid = btrfs_extent_data_ref_objectid(leaf, 10818da6d581SJan Schmidt dref); 10828da6d581SJan Schmidt key.type = BTRFS_EXTENT_DATA_KEY; 10838da6d581SJan Schmidt key.offset = btrfs_extent_data_ref_offset(leaf, dref); 1084dc046b10SJosef Bacik 1085a0a5472aSFilipe Manana if (sc && key.objectid != sc->inum && 10864fc7b572SFilipe Manana !sc->have_delayed_delete_refs) { 1087dc046b10SJosef Bacik ret = BACKREF_FOUND_SHARED; 1088dc046b10SJosef Bacik break; 1089dc046b10SJosef Bacik } 1090dc046b10SJosef Bacik 10918da6d581SJan Schmidt root = btrfs_extent_data_ref_root(leaf, dref); 109286d5f994SEdmund Nadolski 109300142756SJeff Mahoney ret = add_indirect_ref(fs_info, preftrees, root, 109400142756SJeff Mahoney &key, 0, bytenr, count, 10953ec4d323SEdmund Nadolski sc, GFP_NOFS); 10964fc7b572SFilipe Manana 10978da6d581SJan Schmidt break; 10988da6d581SJan Schmidt } 10998da6d581SJan Schmidt default: 11008da6d581SJan Schmidt WARN_ON(1); 11018da6d581SJan Schmidt } 11021149ab6bSWang Shilong if (ret) 11031149ab6bSWang Shilong return ret; 11048da6d581SJan Schmidt ptr += btrfs_extent_inline_ref_size(type); 11058da6d581SJan Schmidt } 11068da6d581SJan Schmidt 11078da6d581SJan Schmidt return 0; 11088da6d581SJan Schmidt } 11098da6d581SJan Schmidt 11108da6d581SJan Schmidt /* 11118da6d581SJan Schmidt * add all non-inline backrefs for bytenr to the list 11123ec4d323SEdmund Nadolski * 11133ec4d323SEdmund Nadolski * Returns 0 on success, <0 on error, or BACKREF_FOUND_SHARED. 11148da6d581SJan Schmidt */ 111598cc4222SJosef Bacik static int add_keyed_refs(struct btrfs_root *extent_root, 11168da6d581SJan Schmidt struct btrfs_path *path, u64 bytenr, 111786d5f994SEdmund Nadolski int info_level, struct preftrees *preftrees, 11183ec4d323SEdmund Nadolski struct share_check *sc) 11198da6d581SJan Schmidt { 112098cc4222SJosef Bacik struct btrfs_fs_info *fs_info = extent_root->fs_info; 11218da6d581SJan Schmidt int ret; 11228da6d581SJan Schmidt int slot; 11238da6d581SJan Schmidt struct extent_buffer *leaf; 11248da6d581SJan Schmidt struct btrfs_key key; 11258da6d581SJan Schmidt 11268da6d581SJan Schmidt while (1) { 11278da6d581SJan Schmidt ret = btrfs_next_item(extent_root, path); 11288da6d581SJan Schmidt if (ret < 0) 11298da6d581SJan Schmidt break; 11308da6d581SJan Schmidt if (ret) { 11318da6d581SJan Schmidt ret = 0; 11328da6d581SJan Schmidt break; 11338da6d581SJan Schmidt } 11348da6d581SJan Schmidt 11358da6d581SJan Schmidt slot = path->slots[0]; 11368da6d581SJan Schmidt leaf = path->nodes[0]; 11378da6d581SJan Schmidt btrfs_item_key_to_cpu(leaf, &key, slot); 11388da6d581SJan Schmidt 11398da6d581SJan Schmidt if (key.objectid != bytenr) 11408da6d581SJan Schmidt break; 11418da6d581SJan Schmidt if (key.type < BTRFS_TREE_BLOCK_REF_KEY) 11428da6d581SJan Schmidt continue; 11438da6d581SJan Schmidt if (key.type > BTRFS_SHARED_DATA_REF_KEY) 11448da6d581SJan Schmidt break; 11458da6d581SJan Schmidt 11468da6d581SJan Schmidt switch (key.type) { 11478da6d581SJan Schmidt case BTRFS_SHARED_BLOCK_REF_KEY: 114886d5f994SEdmund Nadolski /* SHARED DIRECT METADATA backref */ 114900142756SJeff Mahoney ret = add_direct_ref(fs_info, preftrees, 115000142756SJeff Mahoney info_level + 1, key.offset, 11513ec4d323SEdmund Nadolski bytenr, 1, NULL, GFP_NOFS); 11528da6d581SJan Schmidt break; 11538da6d581SJan Schmidt case BTRFS_SHARED_DATA_REF_KEY: { 115486d5f994SEdmund Nadolski /* SHARED DIRECT FULL backref */ 11558da6d581SJan Schmidt struct btrfs_shared_data_ref *sdref; 11568da6d581SJan Schmidt int count; 11578da6d581SJan Schmidt 11588da6d581SJan Schmidt sdref = btrfs_item_ptr(leaf, slot, 11598da6d581SJan Schmidt struct btrfs_shared_data_ref); 11608da6d581SJan Schmidt count = btrfs_shared_data_ref_count(leaf, sdref); 116100142756SJeff Mahoney ret = add_direct_ref(fs_info, preftrees, 0, 116200142756SJeff Mahoney key.offset, bytenr, count, 11633ec4d323SEdmund Nadolski sc, GFP_NOFS); 11648da6d581SJan Schmidt break; 11658da6d581SJan Schmidt } 11668da6d581SJan Schmidt case BTRFS_TREE_BLOCK_REF_KEY: 116786d5f994SEdmund Nadolski /* NORMAL INDIRECT METADATA backref */ 116800142756SJeff Mahoney ret = add_indirect_ref(fs_info, preftrees, key.offset, 116900142756SJeff Mahoney NULL, info_level + 1, bytenr, 11703ec4d323SEdmund Nadolski 1, NULL, GFP_NOFS); 11718da6d581SJan Schmidt break; 11728da6d581SJan Schmidt case BTRFS_EXTENT_DATA_REF_KEY: { 117386d5f994SEdmund Nadolski /* NORMAL INDIRECT DATA backref */ 11748da6d581SJan Schmidt struct btrfs_extent_data_ref *dref; 11758da6d581SJan Schmidt int count; 11768da6d581SJan Schmidt u64 root; 11778da6d581SJan Schmidt 11788da6d581SJan Schmidt dref = btrfs_item_ptr(leaf, slot, 11798da6d581SJan Schmidt struct btrfs_extent_data_ref); 11808da6d581SJan Schmidt count = btrfs_extent_data_ref_count(leaf, dref); 11818da6d581SJan Schmidt key.objectid = btrfs_extent_data_ref_objectid(leaf, 11828da6d581SJan Schmidt dref); 11838da6d581SJan Schmidt key.type = BTRFS_EXTENT_DATA_KEY; 11848da6d581SJan Schmidt key.offset = btrfs_extent_data_ref_offset(leaf, dref); 1185dc046b10SJosef Bacik 1186a0a5472aSFilipe Manana if (sc && key.objectid != sc->inum && 11874fc7b572SFilipe Manana !sc->have_delayed_delete_refs) { 1188dc046b10SJosef Bacik ret = BACKREF_FOUND_SHARED; 1189dc046b10SJosef Bacik break; 1190dc046b10SJosef Bacik } 1191dc046b10SJosef Bacik 11928da6d581SJan Schmidt root = btrfs_extent_data_ref_root(leaf, dref); 119300142756SJeff Mahoney ret = add_indirect_ref(fs_info, preftrees, root, 119400142756SJeff Mahoney &key, 0, bytenr, count, 11953ec4d323SEdmund Nadolski sc, GFP_NOFS); 11968da6d581SJan Schmidt break; 11978da6d581SJan Schmidt } 11988da6d581SJan Schmidt default: 11998da6d581SJan Schmidt WARN_ON(1); 12008da6d581SJan Schmidt } 12011149ab6bSWang Shilong if (ret) 12021149ab6bSWang Shilong return ret; 12031149ab6bSWang Shilong 12048da6d581SJan Schmidt } 12058da6d581SJan Schmidt 12068da6d581SJan Schmidt return ret; 12078da6d581SJan Schmidt } 12088da6d581SJan Schmidt 12098da6d581SJan Schmidt /* 1210583f4ac5SFilipe Manana * The caller has joined a transaction or is holding a read lock on the 1211583f4ac5SFilipe Manana * fs_info->commit_root_sem semaphore, so no need to worry about the root's last 1212583f4ac5SFilipe Manana * snapshot field changing while updating or checking the cache. 1213583f4ac5SFilipe Manana */ 1214583f4ac5SFilipe Manana static bool lookup_backref_shared_cache(struct btrfs_backref_share_check_ctx *ctx, 1215583f4ac5SFilipe Manana struct btrfs_root *root, 1216583f4ac5SFilipe Manana u64 bytenr, int level, bool *is_shared) 1217583f4ac5SFilipe Manana { 1218583f4ac5SFilipe Manana struct btrfs_backref_shared_cache_entry *entry; 1219583f4ac5SFilipe Manana 1220583f4ac5SFilipe Manana if (!ctx->use_path_cache) 1221583f4ac5SFilipe Manana return false; 1222583f4ac5SFilipe Manana 1223583f4ac5SFilipe Manana if (WARN_ON_ONCE(level >= BTRFS_MAX_LEVEL)) 1224583f4ac5SFilipe Manana return false; 1225583f4ac5SFilipe Manana 1226583f4ac5SFilipe Manana /* 1227583f4ac5SFilipe Manana * Level -1 is used for the data extent, which is not reliable to cache 1228583f4ac5SFilipe Manana * because its reference count can increase or decrease without us 1229583f4ac5SFilipe Manana * realizing. We cache results only for extent buffers that lead from 1230583f4ac5SFilipe Manana * the root node down to the leaf with the file extent item. 1231583f4ac5SFilipe Manana */ 1232583f4ac5SFilipe Manana ASSERT(level >= 0); 1233583f4ac5SFilipe Manana 1234583f4ac5SFilipe Manana entry = &ctx->path_cache_entries[level]; 1235583f4ac5SFilipe Manana 1236583f4ac5SFilipe Manana /* Unused cache entry or being used for some other extent buffer. */ 1237583f4ac5SFilipe Manana if (entry->bytenr != bytenr) 1238583f4ac5SFilipe Manana return false; 1239583f4ac5SFilipe Manana 1240583f4ac5SFilipe Manana /* 1241583f4ac5SFilipe Manana * We cached a false result, but the last snapshot generation of the 1242583f4ac5SFilipe Manana * root changed, so we now have a snapshot. Don't trust the result. 1243583f4ac5SFilipe Manana */ 1244583f4ac5SFilipe Manana if (!entry->is_shared && 1245583f4ac5SFilipe Manana entry->gen != btrfs_root_last_snapshot(&root->root_item)) 1246583f4ac5SFilipe Manana return false; 1247583f4ac5SFilipe Manana 1248583f4ac5SFilipe Manana /* 1249583f4ac5SFilipe Manana * If we cached a true result and the last generation used for dropping 1250583f4ac5SFilipe Manana * a root changed, we can not trust the result, because the dropped root 1251583f4ac5SFilipe Manana * could be a snapshot sharing this extent buffer. 1252583f4ac5SFilipe Manana */ 1253583f4ac5SFilipe Manana if (entry->is_shared && 1254583f4ac5SFilipe Manana entry->gen != btrfs_get_last_root_drop_gen(root->fs_info)) 1255583f4ac5SFilipe Manana return false; 1256583f4ac5SFilipe Manana 1257583f4ac5SFilipe Manana *is_shared = entry->is_shared; 1258583f4ac5SFilipe Manana /* 1259583f4ac5SFilipe Manana * If the node at this level is shared, than all nodes below are also 1260583f4ac5SFilipe Manana * shared. Currently some of the nodes below may be marked as not shared 1261583f4ac5SFilipe Manana * because we have just switched from one leaf to another, and switched 1262583f4ac5SFilipe Manana * also other nodes above the leaf and below the current level, so mark 1263583f4ac5SFilipe Manana * them as shared. 1264583f4ac5SFilipe Manana */ 1265583f4ac5SFilipe Manana if (*is_shared) { 1266583f4ac5SFilipe Manana for (int i = 0; i < level; i++) { 1267583f4ac5SFilipe Manana ctx->path_cache_entries[i].is_shared = true; 1268583f4ac5SFilipe Manana ctx->path_cache_entries[i].gen = entry->gen; 1269583f4ac5SFilipe Manana } 1270583f4ac5SFilipe Manana } 1271583f4ac5SFilipe Manana 1272583f4ac5SFilipe Manana return true; 1273583f4ac5SFilipe Manana } 1274583f4ac5SFilipe Manana 1275583f4ac5SFilipe Manana /* 1276583f4ac5SFilipe Manana * The caller has joined a transaction or is holding a read lock on the 1277583f4ac5SFilipe Manana * fs_info->commit_root_sem semaphore, so no need to worry about the root's last 1278583f4ac5SFilipe Manana * snapshot field changing while updating or checking the cache. 1279583f4ac5SFilipe Manana */ 1280583f4ac5SFilipe Manana static void store_backref_shared_cache(struct btrfs_backref_share_check_ctx *ctx, 1281583f4ac5SFilipe Manana struct btrfs_root *root, 1282583f4ac5SFilipe Manana u64 bytenr, int level, bool is_shared) 1283583f4ac5SFilipe Manana { 1284583f4ac5SFilipe Manana struct btrfs_backref_shared_cache_entry *entry; 1285583f4ac5SFilipe Manana u64 gen; 1286583f4ac5SFilipe Manana 1287583f4ac5SFilipe Manana if (!ctx->use_path_cache) 1288583f4ac5SFilipe Manana return; 1289583f4ac5SFilipe Manana 1290583f4ac5SFilipe Manana if (WARN_ON_ONCE(level >= BTRFS_MAX_LEVEL)) 1291583f4ac5SFilipe Manana return; 1292583f4ac5SFilipe Manana 1293583f4ac5SFilipe Manana /* 1294583f4ac5SFilipe Manana * Level -1 is used for the data extent, which is not reliable to cache 1295583f4ac5SFilipe Manana * because its reference count can increase or decrease without us 1296583f4ac5SFilipe Manana * realizing. We cache results only for extent buffers that lead from 1297583f4ac5SFilipe Manana * the root node down to the leaf with the file extent item. 1298583f4ac5SFilipe Manana */ 1299583f4ac5SFilipe Manana ASSERT(level >= 0); 1300583f4ac5SFilipe Manana 1301583f4ac5SFilipe Manana if (is_shared) 1302583f4ac5SFilipe Manana gen = btrfs_get_last_root_drop_gen(root->fs_info); 1303583f4ac5SFilipe Manana else 1304583f4ac5SFilipe Manana gen = btrfs_root_last_snapshot(&root->root_item); 1305583f4ac5SFilipe Manana 1306583f4ac5SFilipe Manana entry = &ctx->path_cache_entries[level]; 1307583f4ac5SFilipe Manana entry->bytenr = bytenr; 1308583f4ac5SFilipe Manana entry->is_shared = is_shared; 1309583f4ac5SFilipe Manana entry->gen = gen; 1310583f4ac5SFilipe Manana 1311583f4ac5SFilipe Manana /* 1312583f4ac5SFilipe Manana * If we found an extent buffer is shared, set the cache result for all 1313583f4ac5SFilipe Manana * extent buffers below it to true. As nodes in the path are COWed, 1314583f4ac5SFilipe Manana * their sharedness is moved to their children, and if a leaf is COWed, 1315583f4ac5SFilipe Manana * then the sharedness of a data extent becomes direct, the refcount of 1316583f4ac5SFilipe Manana * data extent is increased in the extent item at the extent tree. 1317583f4ac5SFilipe Manana */ 1318583f4ac5SFilipe Manana if (is_shared) { 1319583f4ac5SFilipe Manana for (int i = 0; i < level; i++) { 1320583f4ac5SFilipe Manana entry = &ctx->path_cache_entries[i]; 1321583f4ac5SFilipe Manana entry->is_shared = is_shared; 1322583f4ac5SFilipe Manana entry->gen = gen; 1323583f4ac5SFilipe Manana } 1324583f4ac5SFilipe Manana } 1325583f4ac5SFilipe Manana } 1326583f4ac5SFilipe Manana 1327583f4ac5SFilipe Manana /* 13288da6d581SJan Schmidt * this adds all existing backrefs (inline backrefs, backrefs and delayed 13298da6d581SJan Schmidt * refs) for the given bytenr to the refs list, merges duplicates and resolves 13308da6d581SJan Schmidt * indirect refs to their parent bytenr. 13318da6d581SJan Schmidt * When roots are found, they're added to the roots list 13328da6d581SJan Schmidt * 1333f3a84ccdSFilipe Manana * If time_seq is set to BTRFS_SEQ_LAST, it will not search delayed_refs, and 1334f3a84ccdSFilipe Manana * behave much like trans == NULL case, the difference only lies in it will not 133521633fc6SQu Wenruo * commit root. 133621633fc6SQu Wenruo * The special case is for qgroup to search roots in commit_transaction(). 133721633fc6SQu Wenruo * 13383ec4d323SEdmund Nadolski * @sc - if !NULL, then immediately return BACKREF_FOUND_SHARED when a 13393ec4d323SEdmund Nadolski * shared extent is detected. 13403ec4d323SEdmund Nadolski * 13413ec4d323SEdmund Nadolski * Otherwise this returns 0 for success and <0 for an error. 13423ec4d323SEdmund Nadolski * 1343c995ab3cSZygo Blaxell * If ignore_offset is set to false, only extent refs whose offsets match 1344c995ab3cSZygo Blaxell * extent_item_pos are returned. If true, every extent ref is returned 1345c995ab3cSZygo Blaxell * and extent_item_pos is ignored. 1346c995ab3cSZygo Blaxell * 13478da6d581SJan Schmidt * FIXME some caching might speed things up 13488da6d581SJan Schmidt */ 13498da6d581SJan Schmidt static int find_parent_nodes(struct btrfs_trans_handle *trans, 13508da6d581SJan Schmidt struct btrfs_fs_info *fs_info, u64 bytenr, 1351097b8a7cSJan Schmidt u64 time_seq, struct ulist *refs, 1352dc046b10SJosef Bacik struct ulist *roots, const u64 *extent_item_pos, 1353c995ab3cSZygo Blaxell struct share_check *sc, bool ignore_offset) 13548da6d581SJan Schmidt { 135529cbcf40SJosef Bacik struct btrfs_root *root = btrfs_extent_root(fs_info, bytenr); 13568da6d581SJan Schmidt struct btrfs_key key; 13578da6d581SJan Schmidt struct btrfs_path *path; 13588da6d581SJan Schmidt struct btrfs_delayed_ref_root *delayed_refs = NULL; 1359d3b01064SLi Zefan struct btrfs_delayed_ref_head *head; 13608da6d581SJan Schmidt int info_level = 0; 13618da6d581SJan Schmidt int ret; 1362e0c476b1SJeff Mahoney struct prelim_ref *ref; 136386d5f994SEdmund Nadolski struct rb_node *node; 1364f05c4746SWang Shilong struct extent_inode_elem *eie = NULL; 136586d5f994SEdmund Nadolski struct preftrees preftrees = { 136686d5f994SEdmund Nadolski .direct = PREFTREE_INIT, 136786d5f994SEdmund Nadolski .indirect = PREFTREE_INIT, 136886d5f994SEdmund Nadolski .indirect_missing_keys = PREFTREE_INIT 136986d5f994SEdmund Nadolski }; 13708da6d581SJan Schmidt 137156f5c199SFilipe Manana /* Roots ulist is not needed when using a sharedness check context. */ 137256f5c199SFilipe Manana if (sc) 137356f5c199SFilipe Manana ASSERT(roots == NULL); 137456f5c199SFilipe Manana 13758da6d581SJan Schmidt key.objectid = bytenr; 13768da6d581SJan Schmidt key.offset = (u64)-1; 1377261c84b6SJosef Bacik if (btrfs_fs_incompat(fs_info, SKINNY_METADATA)) 1378261c84b6SJosef Bacik key.type = BTRFS_METADATA_ITEM_KEY; 1379261c84b6SJosef Bacik else 1380261c84b6SJosef Bacik key.type = BTRFS_EXTENT_ITEM_KEY; 13818da6d581SJan Schmidt 13828da6d581SJan Schmidt path = btrfs_alloc_path(); 13838da6d581SJan Schmidt if (!path) 13848da6d581SJan Schmidt return -ENOMEM; 1385e84752d4SWang Shilong if (!trans) { 1386da61d31aSJosef Bacik path->search_commit_root = 1; 1387e84752d4SWang Shilong path->skip_locking = 1; 1388e84752d4SWang Shilong } 13898da6d581SJan Schmidt 1390f3a84ccdSFilipe Manana if (time_seq == BTRFS_SEQ_LAST) 139121633fc6SQu Wenruo path->skip_locking = 1; 139221633fc6SQu Wenruo 13938da6d581SJan Schmidt again: 1394d3b01064SLi Zefan head = NULL; 1395d3b01064SLi Zefan 139698cc4222SJosef Bacik ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); 13978da6d581SJan Schmidt if (ret < 0) 13988da6d581SJan Schmidt goto out; 1399fcba0120SJosef Bacik if (ret == 0) { 1400fcba0120SJosef Bacik /* This shouldn't happen, indicates a bug or fs corruption. */ 1401fcba0120SJosef Bacik ASSERT(ret != 0); 1402fcba0120SJosef Bacik ret = -EUCLEAN; 1403fcba0120SJosef Bacik goto out; 1404fcba0120SJosef Bacik } 14058da6d581SJan Schmidt 140621633fc6SQu Wenruo if (trans && likely(trans->type != __TRANS_DUMMY) && 1407f3a84ccdSFilipe Manana time_seq != BTRFS_SEQ_LAST) { 14088da6d581SJan Schmidt /* 14099665ebd5SJosef Bacik * We have a specific time_seq we care about and trans which 14109665ebd5SJosef Bacik * means we have the path lock, we need to grab the ref head and 14119665ebd5SJosef Bacik * lock it so we have a consistent view of the refs at the given 14129665ebd5SJosef Bacik * time. 14138da6d581SJan Schmidt */ 14148da6d581SJan Schmidt delayed_refs = &trans->transaction->delayed_refs; 14158da6d581SJan Schmidt spin_lock(&delayed_refs->lock); 1416f72ad18eSLiu Bo head = btrfs_find_delayed_ref_head(delayed_refs, bytenr); 14178da6d581SJan Schmidt if (head) { 14188da6d581SJan Schmidt if (!mutex_trylock(&head->mutex)) { 1419d278850eSJosef Bacik refcount_inc(&head->refs); 14208da6d581SJan Schmidt spin_unlock(&delayed_refs->lock); 14218da6d581SJan Schmidt 14228da6d581SJan Schmidt btrfs_release_path(path); 14238da6d581SJan Schmidt 14248da6d581SJan Schmidt /* 14258da6d581SJan Schmidt * Mutex was contended, block until it's 14268da6d581SJan Schmidt * released and try again 14278da6d581SJan Schmidt */ 14288da6d581SJan Schmidt mutex_lock(&head->mutex); 14298da6d581SJan Schmidt mutex_unlock(&head->mutex); 1430d278850eSJosef Bacik btrfs_put_delayed_ref_head(head); 14318da6d581SJan Schmidt goto again; 14328da6d581SJan Schmidt } 1433d7df2c79SJosef Bacik spin_unlock(&delayed_refs->lock); 143400142756SJeff Mahoney ret = add_delayed_refs(fs_info, head, time_seq, 1435b25b0b87Sethanwu &preftrees, sc); 1436155725c9SJan Schmidt mutex_unlock(&head->mutex); 1437d7df2c79SJosef Bacik if (ret) 14388da6d581SJan Schmidt goto out; 1439d7df2c79SJosef Bacik } else { 14408da6d581SJan Schmidt spin_unlock(&delayed_refs->lock); 14417a3ae2f8SJan Schmidt } 1442d7df2c79SJosef Bacik } 14438da6d581SJan Schmidt 14448da6d581SJan Schmidt if (path->slots[0]) { 14458da6d581SJan Schmidt struct extent_buffer *leaf; 14468da6d581SJan Schmidt int slot; 14478da6d581SJan Schmidt 1448dadcaf78SJan Schmidt path->slots[0]--; 14498da6d581SJan Schmidt leaf = path->nodes[0]; 1450dadcaf78SJan Schmidt slot = path->slots[0]; 14518da6d581SJan Schmidt btrfs_item_key_to_cpu(leaf, &key, slot); 14528da6d581SJan Schmidt if (key.objectid == bytenr && 1453261c84b6SJosef Bacik (key.type == BTRFS_EXTENT_ITEM_KEY || 1454261c84b6SJosef Bacik key.type == BTRFS_METADATA_ITEM_KEY)) { 145500142756SJeff Mahoney ret = add_inline_refs(fs_info, path, bytenr, 1456b25b0b87Sethanwu &info_level, &preftrees, sc); 14578da6d581SJan Schmidt if (ret) 14588da6d581SJan Schmidt goto out; 145998cc4222SJosef Bacik ret = add_keyed_refs(root, path, bytenr, info_level, 14603ec4d323SEdmund Nadolski &preftrees, sc); 14618da6d581SJan Schmidt if (ret) 14628da6d581SJan Schmidt goto out; 14638da6d581SJan Schmidt } 14648da6d581SJan Schmidt } 146586d5f994SEdmund Nadolski 146656f5c199SFilipe Manana /* 146756f5c199SFilipe Manana * If we have a share context and we reached here, it means the extent 146856f5c199SFilipe Manana * is not directly shared (no multiple reference items for it), 146956f5c199SFilipe Manana * otherwise we would have exited earlier with a return value of 147056f5c199SFilipe Manana * BACKREF_FOUND_SHARED after processing delayed references or while 147156f5c199SFilipe Manana * processing inline or keyed references from the extent tree. 147256f5c199SFilipe Manana * The extent may however be indirectly shared through shared subtrees 147356f5c199SFilipe Manana * as a result from creating snapshots, so we determine below what is 147456f5c199SFilipe Manana * its parent node, in case we are dealing with a metadata extent, or 147556f5c199SFilipe Manana * what's the leaf (or leaves), from a fs tree, that has a file extent 147656f5c199SFilipe Manana * item pointing to it in case we are dealing with a data extent. 147756f5c199SFilipe Manana */ 147856f5c199SFilipe Manana ASSERT(extent_is_shared(sc) == 0); 147956f5c199SFilipe Manana 1480877c1476SFilipe Manana /* 1481877c1476SFilipe Manana * If we are here for a data extent and we have a share_check structure 1482877c1476SFilipe Manana * it means the data extent is not directly shared (does not have 1483877c1476SFilipe Manana * multiple reference items), so we have to check if a path in the fs 1484877c1476SFilipe Manana * tree (going from the root node down to the leaf that has the file 1485877c1476SFilipe Manana * extent item pointing to the data extent) is shared, that is, if any 1486877c1476SFilipe Manana * of the extent buffers in the path is referenced by other trees. 1487877c1476SFilipe Manana */ 1488877c1476SFilipe Manana if (sc && bytenr == sc->data_bytenr) { 1489877c1476SFilipe Manana /* 14906976201fSFilipe Manana * If our data extent is from a generation more recent than the 14916976201fSFilipe Manana * last generation used to snapshot the root, then we know that 14926976201fSFilipe Manana * it can not be shared through subtrees, so we can skip 14936976201fSFilipe Manana * resolving indirect references, there's no point in 14946976201fSFilipe Manana * determining the extent buffers for the path from the fs tree 14956976201fSFilipe Manana * root node down to the leaf that has the file extent item that 14966976201fSFilipe Manana * points to the data extent. 14976976201fSFilipe Manana */ 14986976201fSFilipe Manana if (sc->data_extent_gen > 14996976201fSFilipe Manana btrfs_root_last_snapshot(&sc->root->root_item)) { 15006976201fSFilipe Manana ret = BACKREF_FOUND_NOT_SHARED; 15016976201fSFilipe Manana goto out; 15026976201fSFilipe Manana } 15036976201fSFilipe Manana 15046976201fSFilipe Manana /* 1505877c1476SFilipe Manana * If we are only determining if a data extent is shared or not 1506877c1476SFilipe Manana * and the corresponding file extent item is located in the same 1507877c1476SFilipe Manana * leaf as the previous file extent item, we can skip resolving 1508877c1476SFilipe Manana * indirect references for a data extent, since the fs tree path 1509877c1476SFilipe Manana * is the same (same leaf, so same path). We skip as long as the 1510877c1476SFilipe Manana * cached result for the leaf is valid and only if there's only 1511877c1476SFilipe Manana * one file extent item pointing to the data extent, because in 1512877c1476SFilipe Manana * the case of multiple file extent items, they may be located 1513877c1476SFilipe Manana * in different leaves and therefore we have multiple paths. 1514877c1476SFilipe Manana */ 1515877c1476SFilipe Manana if (sc->ctx->curr_leaf_bytenr == sc->ctx->prev_leaf_bytenr && 1516877c1476SFilipe Manana sc->self_ref_count == 1) { 1517877c1476SFilipe Manana bool cached; 1518877c1476SFilipe Manana bool is_shared; 1519877c1476SFilipe Manana 1520877c1476SFilipe Manana cached = lookup_backref_shared_cache(sc->ctx, sc->root, 1521877c1476SFilipe Manana sc->ctx->curr_leaf_bytenr, 1522877c1476SFilipe Manana 0, &is_shared); 1523877c1476SFilipe Manana if (cached) { 1524877c1476SFilipe Manana if (is_shared) 1525877c1476SFilipe Manana ret = BACKREF_FOUND_SHARED; 1526877c1476SFilipe Manana else 1527877c1476SFilipe Manana ret = BACKREF_FOUND_NOT_SHARED; 1528877c1476SFilipe Manana goto out; 1529877c1476SFilipe Manana } 1530877c1476SFilipe Manana } 1531877c1476SFilipe Manana } 1532877c1476SFilipe Manana 15338da6d581SJan Schmidt btrfs_release_path(path); 15348da6d581SJan Schmidt 153538e3eebfSJosef Bacik ret = add_missing_keys(fs_info, &preftrees, path->skip_locking == 0); 1536d5c88b73SJan Schmidt if (ret) 1537d5c88b73SJan Schmidt goto out; 1538d5c88b73SJan Schmidt 1539ecf160b4SLiu Bo WARN_ON(!RB_EMPTY_ROOT(&preftrees.indirect_missing_keys.root.rb_root)); 15408da6d581SJan Schmidt 154186d5f994SEdmund Nadolski ret = resolve_indirect_refs(fs_info, path, time_seq, &preftrees, 1542b25b0b87Sethanwu extent_item_pos, sc, ignore_offset); 15438da6d581SJan Schmidt if (ret) 15448da6d581SJan Schmidt goto out; 15458da6d581SJan Schmidt 1546ecf160b4SLiu Bo WARN_ON(!RB_EMPTY_ROOT(&preftrees.indirect.root.rb_root)); 15478da6d581SJan Schmidt 154886d5f994SEdmund Nadolski /* 154986d5f994SEdmund Nadolski * This walks the tree of merged and resolved refs. Tree blocks are 155086d5f994SEdmund Nadolski * read in as needed. Unique entries are added to the ulist, and 155186d5f994SEdmund Nadolski * the list of found roots is updated. 155286d5f994SEdmund Nadolski * 155386d5f994SEdmund Nadolski * We release the entire tree in one go before returning. 155486d5f994SEdmund Nadolski */ 1555ecf160b4SLiu Bo node = rb_first_cached(&preftrees.direct.root); 155686d5f994SEdmund Nadolski while (node) { 155786d5f994SEdmund Nadolski ref = rb_entry(node, struct prelim_ref, rbnode); 155886d5f994SEdmund Nadolski node = rb_next(&ref->rbnode); 1559c8195a7bSZygo Blaxell /* 1560c8195a7bSZygo Blaxell * ref->count < 0 can happen here if there are delayed 1561c8195a7bSZygo Blaxell * refs with a node->action of BTRFS_DROP_DELAYED_REF. 1562c8195a7bSZygo Blaxell * prelim_ref_insert() relies on this when merging 1563c8195a7bSZygo Blaxell * identical refs to keep the overall count correct. 1564c8195a7bSZygo Blaxell * prelim_ref_insert() will merge only those refs 1565c8195a7bSZygo Blaxell * which compare identically. Any refs having 1566c8195a7bSZygo Blaxell * e.g. different offsets would not be merged, 1567c8195a7bSZygo Blaxell * and would retain their original ref->count < 0. 1568c8195a7bSZygo Blaxell */ 156998cfee21SWang Shilong if (roots && ref->count && ref->root_id && ref->parent == 0) { 15708da6d581SJan Schmidt /* no parent == root of tree */ 15718da6d581SJan Schmidt ret = ulist_add(roots, ref->root_id, 0, GFP_NOFS); 1572f1723939SWang Shilong if (ret < 0) 1573f1723939SWang Shilong goto out; 15748da6d581SJan Schmidt } 15758da6d581SJan Schmidt if (ref->count && ref->parent) { 15768a56457fSJosef Bacik if (extent_item_pos && !ref->inode_list && 15778a56457fSJosef Bacik ref->level == 0) { 1578976b1908SJan Schmidt struct extent_buffer *eb; 1579707e8a07SDavid Sterba 1580581c1760SQu Wenruo eb = read_tree_block(fs_info, ref->parent, 0, 15811b7ec85eSJosef Bacik 0, ref->level, NULL); 158264c043deSLiu Bo if (IS_ERR(eb)) { 158364c043deSLiu Bo ret = PTR_ERR(eb); 158464c043deSLiu Bo goto out; 15854eb150d6SQu Wenruo } 15864eb150d6SQu Wenruo if (!extent_buffer_uptodate(eb)) { 1587416bc658SJosef Bacik free_extent_buffer(eb); 1588c16c2e2eSWang Shilong ret = -EIO; 1589c16c2e2eSWang Shilong goto out; 1590416bc658SJosef Bacik } 159138e3eebfSJosef Bacik 1592ac5887c8SJosef Bacik if (!path->skip_locking) 15936f7ff6d7SFilipe Manana btrfs_tree_read_lock(eb); 1594976b1908SJan Schmidt ret = find_extent_in_eb(eb, bytenr, 1595c995ab3cSZygo Blaxell *extent_item_pos, &eie, ignore_offset); 159638e3eebfSJosef Bacik if (!path->skip_locking) 1597ac5887c8SJosef Bacik btrfs_tree_read_unlock(eb); 1598976b1908SJan Schmidt free_extent_buffer(eb); 1599f5929cd8SFilipe David Borba Manana if (ret < 0) 1600f5929cd8SFilipe David Borba Manana goto out; 1601f5929cd8SFilipe David Borba Manana ref->inode_list = eie; 160292876eecSFilipe Manana /* 160392876eecSFilipe Manana * We transferred the list ownership to the ref, 160492876eecSFilipe Manana * so set to NULL to avoid a double free in case 160592876eecSFilipe Manana * an error happens after this. 160692876eecSFilipe Manana */ 160792876eecSFilipe Manana eie = NULL; 1608976b1908SJan Schmidt } 16094eb1f66dSTakashi Iwai ret = ulist_add_merge_ptr(refs, ref->parent, 16104eb1f66dSTakashi Iwai ref->inode_list, 16114eb1f66dSTakashi Iwai (void **)&eie, GFP_NOFS); 1612f1723939SWang Shilong if (ret < 0) 1613f1723939SWang Shilong goto out; 16143301958bSJan Schmidt if (!ret && extent_item_pos) { 16153301958bSJan Schmidt /* 16169f05c09dSJosef Bacik * We've recorded that parent, so we must extend 16179f05c09dSJosef Bacik * its inode list here. 16189f05c09dSJosef Bacik * 16199f05c09dSJosef Bacik * However if there was corruption we may not 16209f05c09dSJosef Bacik * have found an eie, return an error in this 16219f05c09dSJosef Bacik * case. 16223301958bSJan Schmidt */ 16239f05c09dSJosef Bacik ASSERT(eie); 16249f05c09dSJosef Bacik if (!eie) { 16259f05c09dSJosef Bacik ret = -EUCLEAN; 16269f05c09dSJosef Bacik goto out; 16279f05c09dSJosef Bacik } 16283301958bSJan Schmidt while (eie->next) 16293301958bSJan Schmidt eie = eie->next; 16303301958bSJan Schmidt eie->next = ref->inode_list; 16313301958bSJan Schmidt } 1632f05c4746SWang Shilong eie = NULL; 163392876eecSFilipe Manana /* 163492876eecSFilipe Manana * We have transferred the inode list ownership from 163592876eecSFilipe Manana * this ref to the ref we added to the 'refs' ulist. 163692876eecSFilipe Manana * So set this ref's inode list to NULL to avoid 163792876eecSFilipe Manana * use-after-free when our caller uses it or double 163892876eecSFilipe Manana * frees in case an error happens before we return. 163992876eecSFilipe Manana */ 164092876eecSFilipe Manana ref->inode_list = NULL; 16418da6d581SJan Schmidt } 16429dd14fd6SEdmund Nadolski cond_resched(); 16438da6d581SJan Schmidt } 16448da6d581SJan Schmidt 16458da6d581SJan Schmidt out: 16468da6d581SJan Schmidt btrfs_free_path(path); 164786d5f994SEdmund Nadolski 164886d5f994SEdmund Nadolski prelim_release(&preftrees.direct); 164986d5f994SEdmund Nadolski prelim_release(&preftrees.indirect); 165086d5f994SEdmund Nadolski prelim_release(&preftrees.indirect_missing_keys); 165186d5f994SEdmund Nadolski 1652f05c4746SWang Shilong if (ret < 0) 1653f05c4746SWang Shilong free_inode_elem_list(eie); 16548da6d581SJan Schmidt return ret; 16558da6d581SJan Schmidt } 16568da6d581SJan Schmidt 16578da6d581SJan Schmidt /* 16588da6d581SJan Schmidt * Finds all leafs with a reference to the specified combination of bytenr and 16598da6d581SJan Schmidt * offset. key_list_head will point to a list of corresponding keys (caller must 16608da6d581SJan Schmidt * free each list element). The leafs will be stored in the leafs ulist, which 16618da6d581SJan Schmidt * must be freed with ulist_free. 16628da6d581SJan Schmidt * 16638da6d581SJan Schmidt * returns 0 on success, <0 on error 16648da6d581SJan Schmidt */ 166519b546d7SQu Wenruo int btrfs_find_all_leafs(struct btrfs_trans_handle *trans, 16668da6d581SJan Schmidt struct btrfs_fs_info *fs_info, u64 bytenr, 1667097b8a7cSJan Schmidt u64 time_seq, struct ulist **leafs, 1668c995ab3cSZygo Blaxell const u64 *extent_item_pos, bool ignore_offset) 16698da6d581SJan Schmidt { 16708da6d581SJan Schmidt int ret; 16718da6d581SJan Schmidt 16728da6d581SJan Schmidt *leafs = ulist_alloc(GFP_NOFS); 167398cfee21SWang Shilong if (!*leafs) 16748da6d581SJan Schmidt return -ENOMEM; 16758da6d581SJan Schmidt 1676afce772eSLu Fengqi ret = find_parent_nodes(trans, fs_info, bytenr, time_seq, 1677c995ab3cSZygo Blaxell *leafs, NULL, extent_item_pos, NULL, ignore_offset); 16788da6d581SJan Schmidt if (ret < 0 && ret != -ENOENT) { 1679976b1908SJan Schmidt free_leaf_list(*leafs); 16808da6d581SJan Schmidt return ret; 16818da6d581SJan Schmidt } 16828da6d581SJan Schmidt 16838da6d581SJan Schmidt return 0; 16848da6d581SJan Schmidt } 16858da6d581SJan Schmidt 16868da6d581SJan Schmidt /* 16878da6d581SJan Schmidt * walk all backrefs for a given extent to find all roots that reference this 16888da6d581SJan Schmidt * extent. Walking a backref means finding all extents that reference this 16898da6d581SJan Schmidt * extent and in turn walk the backrefs of those, too. Naturally this is a 16908da6d581SJan Schmidt * recursive process, but here it is implemented in an iterative fashion: We 16918da6d581SJan Schmidt * find all referencing extents for the extent in question and put them on a 16928da6d581SJan Schmidt * list. In turn, we find all referencing extents for those, further appending 16938da6d581SJan Schmidt * to the list. The way we iterate the list allows adding more elements after 16948da6d581SJan Schmidt * the current while iterating. The process stops when we reach the end of the 16958da6d581SJan Schmidt * list. Found roots are added to the roots list. 16968da6d581SJan Schmidt * 16978da6d581SJan Schmidt * returns 0 on success, < 0 on error. 16988da6d581SJan Schmidt */ 1699e0c476b1SJeff Mahoney static int btrfs_find_all_roots_safe(struct btrfs_trans_handle *trans, 17008da6d581SJan Schmidt struct btrfs_fs_info *fs_info, u64 bytenr, 1701c995ab3cSZygo Blaxell u64 time_seq, struct ulist **roots, 1702c995ab3cSZygo Blaxell bool ignore_offset) 17038da6d581SJan Schmidt { 17048da6d581SJan Schmidt struct ulist *tmp; 17058da6d581SJan Schmidt struct ulist_node *node = NULL; 1706cd1b413cSJan Schmidt struct ulist_iterator uiter; 17078da6d581SJan Schmidt int ret; 17088da6d581SJan Schmidt 17098da6d581SJan Schmidt tmp = ulist_alloc(GFP_NOFS); 17108da6d581SJan Schmidt if (!tmp) 17118da6d581SJan Schmidt return -ENOMEM; 17128da6d581SJan Schmidt *roots = ulist_alloc(GFP_NOFS); 17138da6d581SJan Schmidt if (!*roots) { 17148da6d581SJan Schmidt ulist_free(tmp); 17158da6d581SJan Schmidt return -ENOMEM; 17168da6d581SJan Schmidt } 17178da6d581SJan Schmidt 1718cd1b413cSJan Schmidt ULIST_ITER_INIT(&uiter); 17198da6d581SJan Schmidt while (1) { 1720afce772eSLu Fengqi ret = find_parent_nodes(trans, fs_info, bytenr, time_seq, 1721c995ab3cSZygo Blaxell tmp, *roots, NULL, NULL, ignore_offset); 17228da6d581SJan Schmidt if (ret < 0 && ret != -ENOENT) { 17238da6d581SJan Schmidt ulist_free(tmp); 17248da6d581SJan Schmidt ulist_free(*roots); 1725580c079bSFilipe Manana *roots = NULL; 17268da6d581SJan Schmidt return ret; 17278da6d581SJan Schmidt } 1728cd1b413cSJan Schmidt node = ulist_next(tmp, &uiter); 17298da6d581SJan Schmidt if (!node) 17308da6d581SJan Schmidt break; 17318da6d581SJan Schmidt bytenr = node->val; 1732bca1a290SWang Shilong cond_resched(); 17338da6d581SJan Schmidt } 17348da6d581SJan Schmidt 17358da6d581SJan Schmidt ulist_free(tmp); 17368da6d581SJan Schmidt return 0; 17378da6d581SJan Schmidt } 17388da6d581SJan Schmidt 17399e351cc8SJosef Bacik int btrfs_find_all_roots(struct btrfs_trans_handle *trans, 17409e351cc8SJosef Bacik struct btrfs_fs_info *fs_info, u64 bytenr, 1741c995ab3cSZygo Blaxell u64 time_seq, struct ulist **roots, 1742c7bcbb21SFilipe Manana bool skip_commit_root_sem) 17439e351cc8SJosef Bacik { 17449e351cc8SJosef Bacik int ret; 17459e351cc8SJosef Bacik 17468949b9a1SFilipe Manana if (!trans && !skip_commit_root_sem) 17479e351cc8SJosef Bacik down_read(&fs_info->commit_root_sem); 1748e0c476b1SJeff Mahoney ret = btrfs_find_all_roots_safe(trans, fs_info, bytenr, 1749c7bcbb21SFilipe Manana time_seq, roots, false); 17508949b9a1SFilipe Manana if (!trans && !skip_commit_root_sem) 17519e351cc8SJosef Bacik up_read(&fs_info->commit_root_sem); 17529e351cc8SJosef Bacik return ret; 17539e351cc8SJosef Bacik } 17549e351cc8SJosef Bacik 175584a7949dSFilipe Manana struct btrfs_backref_share_check_ctx *btrfs_alloc_backref_share_check_ctx(void) 175684a7949dSFilipe Manana { 175784a7949dSFilipe Manana struct btrfs_backref_share_check_ctx *ctx; 175884a7949dSFilipe Manana 175984a7949dSFilipe Manana ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); 176084a7949dSFilipe Manana if (!ctx) 176184a7949dSFilipe Manana return NULL; 176284a7949dSFilipe Manana 176384a7949dSFilipe Manana ulist_init(&ctx->refs); 176484a7949dSFilipe Manana 176584a7949dSFilipe Manana return ctx; 176684a7949dSFilipe Manana } 176784a7949dSFilipe Manana 176884a7949dSFilipe Manana void btrfs_free_backref_share_ctx(struct btrfs_backref_share_check_ctx *ctx) 176984a7949dSFilipe Manana { 177084a7949dSFilipe Manana if (!ctx) 177184a7949dSFilipe Manana return; 177284a7949dSFilipe Manana 177384a7949dSFilipe Manana ulist_release(&ctx->refs); 177484a7949dSFilipe Manana kfree(ctx); 177584a7949dSFilipe Manana } 177684a7949dSFilipe Manana 177712a824dcSFilipe Manana /* 17788eedaddaSFilipe Manana * Check if a data extent is shared or not. 17796e353e3bSNikolay Borisov * 1780ceb707daSFilipe Manana * @inode: The inode whose extent we are checking. 1781b8f164e3SFilipe Manana * @bytenr: Logical bytenr of the extent we are checking. 1782b8f164e3SFilipe Manana * @extent_gen: Generation of the extent (file extent item) or 0 if it is 1783b8f164e3SFilipe Manana * not known. 178461dbb952SFilipe Manana * @ctx: A backref sharedness check context. 17852c2ed5aaSMark Fasheh * 17868eedaddaSFilipe Manana * btrfs_is_data_extent_shared uses the backref walking code but will short 17872c2ed5aaSMark Fasheh * circuit as soon as it finds a root or inode that doesn't match the 17882c2ed5aaSMark Fasheh * one passed in. This provides a significant performance benefit for 17892c2ed5aaSMark Fasheh * callers (such as fiemap) which want to know whether the extent is 17902c2ed5aaSMark Fasheh * shared but do not need a ref count. 17912c2ed5aaSMark Fasheh * 179203628cdbSFilipe Manana * This attempts to attach to the running transaction in order to account for 179303628cdbSFilipe Manana * delayed refs, but continues on even when no running transaction exists. 1794bb739cf0SEdmund Nadolski * 17952c2ed5aaSMark Fasheh * Return: 0 if extent is not shared, 1 if it is shared, < 0 on error. 17962c2ed5aaSMark Fasheh */ 1797ceb707daSFilipe Manana int btrfs_is_data_extent_shared(struct btrfs_inode *inode, u64 bytenr, 1798b8f164e3SFilipe Manana u64 extent_gen, 179961dbb952SFilipe Manana struct btrfs_backref_share_check_ctx *ctx) 1800dc046b10SJosef Bacik { 1801ceb707daSFilipe Manana struct btrfs_root *root = inode->root; 1802bb739cf0SEdmund Nadolski struct btrfs_fs_info *fs_info = root->fs_info; 1803bb739cf0SEdmund Nadolski struct btrfs_trans_handle *trans; 1804dc046b10SJosef Bacik struct ulist_iterator uiter; 1805dc046b10SJosef Bacik struct ulist_node *node; 1806f3a84ccdSFilipe Manana struct btrfs_seq_list elem = BTRFS_SEQ_LIST_INIT(elem); 1807dc046b10SJosef Bacik int ret = 0; 18083ec4d323SEdmund Nadolski struct share_check shared = { 1809877c1476SFilipe Manana .ctx = ctx, 1810877c1476SFilipe Manana .root = root, 1811ceb707daSFilipe Manana .inum = btrfs_ino(inode), 181273e339e6SFilipe Manana .data_bytenr = bytenr, 18136976201fSFilipe Manana .data_extent_gen = extent_gen, 18143ec4d323SEdmund Nadolski .share_count = 0, 181573e339e6SFilipe Manana .self_ref_count = 0, 18164fc7b572SFilipe Manana .have_delayed_delete_refs = false, 18173ec4d323SEdmund Nadolski }; 181812a824dcSFilipe Manana int level; 1819dc046b10SJosef Bacik 182073e339e6SFilipe Manana for (int i = 0; i < BTRFS_BACKREF_CTX_PREV_EXTENTS_SIZE; i++) { 182173e339e6SFilipe Manana if (ctx->prev_extents_cache[i].bytenr == bytenr) 182273e339e6SFilipe Manana return ctx->prev_extents_cache[i].is_shared; 182373e339e6SFilipe Manana } 182473e339e6SFilipe Manana 182584a7949dSFilipe Manana ulist_init(&ctx->refs); 1826dc046b10SJosef Bacik 1827a6d155d2SFilipe Manana trans = btrfs_join_transaction_nostart(root); 1828bb739cf0SEdmund Nadolski if (IS_ERR(trans)) { 182903628cdbSFilipe Manana if (PTR_ERR(trans) != -ENOENT && PTR_ERR(trans) != -EROFS) { 183003628cdbSFilipe Manana ret = PTR_ERR(trans); 183103628cdbSFilipe Manana goto out; 183203628cdbSFilipe Manana } 1833bb739cf0SEdmund Nadolski trans = NULL; 1834dc046b10SJosef Bacik down_read(&fs_info->commit_root_sem); 1835bb739cf0SEdmund Nadolski } else { 1836bb739cf0SEdmund Nadolski btrfs_get_tree_mod_seq(fs_info, &elem); 1837bb739cf0SEdmund Nadolski } 1838bb739cf0SEdmund Nadolski 183912a824dcSFilipe Manana /* -1 means we are in the bytenr of the data extent. */ 184012a824dcSFilipe Manana level = -1; 1841dc046b10SJosef Bacik ULIST_ITER_INIT(&uiter); 184261dbb952SFilipe Manana ctx->use_path_cache = true; 1843dc046b10SJosef Bacik while (1) { 184412a824dcSFilipe Manana bool is_shared; 184512a824dcSFilipe Manana bool cached; 184612a824dcSFilipe Manana 184784a7949dSFilipe Manana ret = find_parent_nodes(trans, fs_info, bytenr, elem.seq, &ctx->refs, 1848b6296858SFilipe Manana NULL, NULL, &shared, false); 1849877c1476SFilipe Manana if (ret == BACKREF_FOUND_SHARED || 1850877c1476SFilipe Manana ret == BACKREF_FOUND_NOT_SHARED) { 1851877c1476SFilipe Manana /* If shared must return 1, otherwise return 0. */ 1852877c1476SFilipe Manana ret = (ret == BACKREF_FOUND_SHARED) ? 1 : 0; 185312a824dcSFilipe Manana if (level >= 0) 185461dbb952SFilipe Manana store_backref_shared_cache(ctx, root, bytenr, 1855877c1476SFilipe Manana level, ret == 1); 1856dc046b10SJosef Bacik break; 1857dc046b10SJosef Bacik } 1858dc046b10SJosef Bacik if (ret < 0 && ret != -ENOENT) 1859dc046b10SJosef Bacik break; 18602c2ed5aaSMark Fasheh ret = 0; 1861b8f164e3SFilipe Manana 186263c84b46SFilipe Manana /* 186363c84b46SFilipe Manana * If our data extent was not directly shared (without multiple 186463c84b46SFilipe Manana * reference items), than it might have a single reference item 186563c84b46SFilipe Manana * with a count > 1 for the same offset, which means there are 2 186663c84b46SFilipe Manana * (or more) file extent items that point to the data extent - 186763c84b46SFilipe Manana * this happens when a file extent item needs to be split and 186863c84b46SFilipe Manana * then one item gets moved to another leaf due to a b+tree leaf 186963c84b46SFilipe Manana * split when inserting some item. In this case the file extent 187063c84b46SFilipe Manana * items may be located in different leaves and therefore some 187163c84b46SFilipe Manana * of the leaves may be referenced through shared subtrees while 187263c84b46SFilipe Manana * others are not. Since our extent buffer cache only works for 187363c84b46SFilipe Manana * a single path (by far the most common case and simpler to 187463c84b46SFilipe Manana * deal with), we can not use it if we have multiple leaves 187563c84b46SFilipe Manana * (which implies multiple paths). 187663c84b46SFilipe Manana */ 187784a7949dSFilipe Manana if (level == -1 && ctx->refs.nnodes > 1) 187861dbb952SFilipe Manana ctx->use_path_cache = false; 187963c84b46SFilipe Manana 188012a824dcSFilipe Manana if (level >= 0) 188161dbb952SFilipe Manana store_backref_shared_cache(ctx, root, bytenr, 188212a824dcSFilipe Manana level, false); 188384a7949dSFilipe Manana node = ulist_next(&ctx->refs, &uiter); 1884dc046b10SJosef Bacik if (!node) 1885dc046b10SJosef Bacik break; 1886dc046b10SJosef Bacik bytenr = node->val; 188712a824dcSFilipe Manana level++; 188861dbb952SFilipe Manana cached = lookup_backref_shared_cache(ctx, root, bytenr, level, 188912a824dcSFilipe Manana &is_shared); 189012a824dcSFilipe Manana if (cached) { 189112a824dcSFilipe Manana ret = (is_shared ? 1 : 0); 189212a824dcSFilipe Manana break; 189312a824dcSFilipe Manana } 189418bf591bSEdmund Nadolski shared.share_count = 0; 18954fc7b572SFilipe Manana shared.have_delayed_delete_refs = false; 1896dc046b10SJosef Bacik cond_resched(); 1897dc046b10SJosef Bacik } 1898bb739cf0SEdmund Nadolski 189973e339e6SFilipe Manana /* 190073e339e6SFilipe Manana * Cache the sharedness result for the data extent if we know our inode 190173e339e6SFilipe Manana * has more than 1 file extent item that refers to the data extent. 190273e339e6SFilipe Manana */ 190373e339e6SFilipe Manana if (ret >= 0 && shared.self_ref_count > 1) { 190473e339e6SFilipe Manana int slot = ctx->prev_extents_cache_slot; 190573e339e6SFilipe Manana 190673e339e6SFilipe Manana ctx->prev_extents_cache[slot].bytenr = shared.data_bytenr; 190773e339e6SFilipe Manana ctx->prev_extents_cache[slot].is_shared = (ret == 1); 190873e339e6SFilipe Manana 190973e339e6SFilipe Manana slot = (slot + 1) % BTRFS_BACKREF_CTX_PREV_EXTENTS_SIZE; 191073e339e6SFilipe Manana ctx->prev_extents_cache_slot = slot; 191173e339e6SFilipe Manana } 191273e339e6SFilipe Manana 1913bb739cf0SEdmund Nadolski if (trans) { 1914dc046b10SJosef Bacik btrfs_put_tree_mod_seq(fs_info, &elem); 1915bb739cf0SEdmund Nadolski btrfs_end_transaction(trans); 1916bb739cf0SEdmund Nadolski } else { 1917dc046b10SJosef Bacik up_read(&fs_info->commit_root_sem); 1918bb739cf0SEdmund Nadolski } 191903628cdbSFilipe Manana out: 192084a7949dSFilipe Manana ulist_release(&ctx->refs); 1921877c1476SFilipe Manana ctx->prev_leaf_bytenr = ctx->curr_leaf_bytenr; 1922877c1476SFilipe Manana 1923dc046b10SJosef Bacik return ret; 1924dc046b10SJosef Bacik } 1925dc046b10SJosef Bacik 1926f186373fSMark Fasheh int btrfs_find_one_extref(struct btrfs_root *root, u64 inode_objectid, 1927f186373fSMark Fasheh u64 start_off, struct btrfs_path *path, 1928f186373fSMark Fasheh struct btrfs_inode_extref **ret_extref, 1929f186373fSMark Fasheh u64 *found_off) 1930f186373fSMark Fasheh { 1931f186373fSMark Fasheh int ret, slot; 1932f186373fSMark Fasheh struct btrfs_key key; 1933f186373fSMark Fasheh struct btrfs_key found_key; 1934f186373fSMark Fasheh struct btrfs_inode_extref *extref; 193573980becSJeff Mahoney const struct extent_buffer *leaf; 1936f186373fSMark Fasheh unsigned long ptr; 1937f186373fSMark Fasheh 1938f186373fSMark Fasheh key.objectid = inode_objectid; 1939962a298fSDavid Sterba key.type = BTRFS_INODE_EXTREF_KEY; 1940f186373fSMark Fasheh key.offset = start_off; 1941f186373fSMark Fasheh 1942f186373fSMark Fasheh ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); 1943f186373fSMark Fasheh if (ret < 0) 1944f186373fSMark Fasheh return ret; 1945f186373fSMark Fasheh 1946f186373fSMark Fasheh while (1) { 1947f186373fSMark Fasheh leaf = path->nodes[0]; 1948f186373fSMark Fasheh slot = path->slots[0]; 1949f186373fSMark Fasheh if (slot >= btrfs_header_nritems(leaf)) { 1950f186373fSMark Fasheh /* 1951f186373fSMark Fasheh * If the item at offset is not found, 1952f186373fSMark Fasheh * btrfs_search_slot will point us to the slot 1953f186373fSMark Fasheh * where it should be inserted. In our case 1954f186373fSMark Fasheh * that will be the slot directly before the 1955f186373fSMark Fasheh * next INODE_REF_KEY_V2 item. In the case 1956f186373fSMark Fasheh * that we're pointing to the last slot in a 1957f186373fSMark Fasheh * leaf, we must move one leaf over. 1958f186373fSMark Fasheh */ 1959f186373fSMark Fasheh ret = btrfs_next_leaf(root, path); 1960f186373fSMark Fasheh if (ret) { 1961f186373fSMark Fasheh if (ret >= 1) 1962f186373fSMark Fasheh ret = -ENOENT; 1963f186373fSMark Fasheh break; 1964f186373fSMark Fasheh } 1965f186373fSMark Fasheh continue; 1966f186373fSMark Fasheh } 1967f186373fSMark Fasheh 1968f186373fSMark Fasheh btrfs_item_key_to_cpu(leaf, &found_key, slot); 1969f186373fSMark Fasheh 1970f186373fSMark Fasheh /* 1971f186373fSMark Fasheh * Check that we're still looking at an extended ref key for 1972f186373fSMark Fasheh * this particular objectid. If we have different 1973f186373fSMark Fasheh * objectid or type then there are no more to be found 1974f186373fSMark Fasheh * in the tree and we can exit. 1975f186373fSMark Fasheh */ 1976f186373fSMark Fasheh ret = -ENOENT; 1977f186373fSMark Fasheh if (found_key.objectid != inode_objectid) 1978f186373fSMark Fasheh break; 1979962a298fSDavid Sterba if (found_key.type != BTRFS_INODE_EXTREF_KEY) 1980f186373fSMark Fasheh break; 1981f186373fSMark Fasheh 1982f186373fSMark Fasheh ret = 0; 1983f186373fSMark Fasheh ptr = btrfs_item_ptr_offset(leaf, path->slots[0]); 1984f186373fSMark Fasheh extref = (struct btrfs_inode_extref *)ptr; 1985f186373fSMark Fasheh *ret_extref = extref; 1986f186373fSMark Fasheh if (found_off) 1987f186373fSMark Fasheh *found_off = found_key.offset; 1988f186373fSMark Fasheh break; 1989f186373fSMark Fasheh } 1990f186373fSMark Fasheh 1991f186373fSMark Fasheh return ret; 1992f186373fSMark Fasheh } 1993f186373fSMark Fasheh 199448a3b636SEric Sandeen /* 199548a3b636SEric Sandeen * this iterates to turn a name (from iref/extref) into a full filesystem path. 199648a3b636SEric Sandeen * Elements of the path are separated by '/' and the path is guaranteed to be 199748a3b636SEric Sandeen * 0-terminated. the path is only given within the current file system. 199848a3b636SEric Sandeen * Therefore, it never starts with a '/'. the caller is responsible to provide 199948a3b636SEric Sandeen * "size" bytes in "dest". the dest buffer will be filled backwards. finally, 200048a3b636SEric Sandeen * the start point of the resulting string is returned. this pointer is within 200148a3b636SEric Sandeen * dest, normally. 200248a3b636SEric Sandeen * in case the path buffer would overflow, the pointer is decremented further 200348a3b636SEric Sandeen * as if output was written to the buffer, though no more output is actually 200448a3b636SEric Sandeen * generated. that way, the caller can determine how much space would be 200548a3b636SEric Sandeen * required for the path to fit into the buffer. in that case, the returned 200648a3b636SEric Sandeen * value will be smaller than dest. callers must check this! 200748a3b636SEric Sandeen */ 200896b5bd77SJan Schmidt char *btrfs_ref_to_path(struct btrfs_root *fs_root, struct btrfs_path *path, 2009d24bec3aSMark Fasheh u32 name_len, unsigned long name_off, 2010a542ad1bSJan Schmidt struct extent_buffer *eb_in, u64 parent, 2011a542ad1bSJan Schmidt char *dest, u32 size) 2012a542ad1bSJan Schmidt { 2013a542ad1bSJan Schmidt int slot; 2014a542ad1bSJan Schmidt u64 next_inum; 2015a542ad1bSJan Schmidt int ret; 2016661bec6bSGabriel de Perthuis s64 bytes_left = ((s64)size) - 1; 2017a542ad1bSJan Schmidt struct extent_buffer *eb = eb_in; 2018a542ad1bSJan Schmidt struct btrfs_key found_key; 2019d24bec3aSMark Fasheh struct btrfs_inode_ref *iref; 2020a542ad1bSJan Schmidt 2021a542ad1bSJan Schmidt if (bytes_left >= 0) 2022a542ad1bSJan Schmidt dest[bytes_left] = '\0'; 2023a542ad1bSJan Schmidt 2024a542ad1bSJan Schmidt while (1) { 2025d24bec3aSMark Fasheh bytes_left -= name_len; 2026a542ad1bSJan Schmidt if (bytes_left >= 0) 2027a542ad1bSJan Schmidt read_extent_buffer(eb, dest + bytes_left, 2028d24bec3aSMark Fasheh name_off, name_len); 2029b916a59aSJan Schmidt if (eb != eb_in) { 20300c0fe3b0SFilipe Manana if (!path->skip_locking) 2031ac5887c8SJosef Bacik btrfs_tree_read_unlock(eb); 2032a542ad1bSJan Schmidt free_extent_buffer(eb); 2033b916a59aSJan Schmidt } 2034c234a24dSDavid Sterba ret = btrfs_find_item(fs_root, path, parent, 0, 2035c234a24dSDavid Sterba BTRFS_INODE_REF_KEY, &found_key); 20368f24b496SJan Schmidt if (ret > 0) 20378f24b496SJan Schmidt ret = -ENOENT; 2038a542ad1bSJan Schmidt if (ret) 2039a542ad1bSJan Schmidt break; 2040d24bec3aSMark Fasheh 2041a542ad1bSJan Schmidt next_inum = found_key.offset; 2042a542ad1bSJan Schmidt 2043a542ad1bSJan Schmidt /* regular exit ahead */ 2044a542ad1bSJan Schmidt if (parent == next_inum) 2045a542ad1bSJan Schmidt break; 2046a542ad1bSJan Schmidt 2047a542ad1bSJan Schmidt slot = path->slots[0]; 2048a542ad1bSJan Schmidt eb = path->nodes[0]; 2049a542ad1bSJan Schmidt /* make sure we can use eb after releasing the path */ 2050b916a59aSJan Schmidt if (eb != eb_in) { 20510c0fe3b0SFilipe Manana path->nodes[0] = NULL; 20520c0fe3b0SFilipe Manana path->locks[0] = 0; 2053b916a59aSJan Schmidt } 2054a542ad1bSJan Schmidt btrfs_release_path(path); 2055a542ad1bSJan Schmidt iref = btrfs_item_ptr(eb, slot, struct btrfs_inode_ref); 2056d24bec3aSMark Fasheh 2057d24bec3aSMark Fasheh name_len = btrfs_inode_ref_name_len(eb, iref); 2058d24bec3aSMark Fasheh name_off = (unsigned long)(iref + 1); 2059d24bec3aSMark Fasheh 2060a542ad1bSJan Schmidt parent = next_inum; 2061a542ad1bSJan Schmidt --bytes_left; 2062a542ad1bSJan Schmidt if (bytes_left >= 0) 2063a542ad1bSJan Schmidt dest[bytes_left] = '/'; 2064a542ad1bSJan Schmidt } 2065a542ad1bSJan Schmidt 2066a542ad1bSJan Schmidt btrfs_release_path(path); 2067a542ad1bSJan Schmidt 2068a542ad1bSJan Schmidt if (ret) 2069a542ad1bSJan Schmidt return ERR_PTR(ret); 2070a542ad1bSJan Schmidt 2071a542ad1bSJan Schmidt return dest + bytes_left; 2072a542ad1bSJan Schmidt } 2073a542ad1bSJan Schmidt 2074a542ad1bSJan Schmidt /* 2075a542ad1bSJan Schmidt * this makes the path point to (logical EXTENT_ITEM *) 2076a542ad1bSJan Schmidt * returns BTRFS_EXTENT_FLAG_DATA for data, BTRFS_EXTENT_FLAG_TREE_BLOCK for 2077a542ad1bSJan Schmidt * tree blocks and <0 on error. 2078a542ad1bSJan Schmidt */ 2079a542ad1bSJan Schmidt int extent_from_logical(struct btrfs_fs_info *fs_info, u64 logical, 208069917e43SLiu Bo struct btrfs_path *path, struct btrfs_key *found_key, 208169917e43SLiu Bo u64 *flags_ret) 2082a542ad1bSJan Schmidt { 208329cbcf40SJosef Bacik struct btrfs_root *extent_root = btrfs_extent_root(fs_info, logical); 2084a542ad1bSJan Schmidt int ret; 2085a542ad1bSJan Schmidt u64 flags; 2086261c84b6SJosef Bacik u64 size = 0; 2087a542ad1bSJan Schmidt u32 item_size; 208873980becSJeff Mahoney const struct extent_buffer *eb; 2089a542ad1bSJan Schmidt struct btrfs_extent_item *ei; 2090a542ad1bSJan Schmidt struct btrfs_key key; 2091a542ad1bSJan Schmidt 2092261c84b6SJosef Bacik if (btrfs_fs_incompat(fs_info, SKINNY_METADATA)) 2093261c84b6SJosef Bacik key.type = BTRFS_METADATA_ITEM_KEY; 2094261c84b6SJosef Bacik else 2095a542ad1bSJan Schmidt key.type = BTRFS_EXTENT_ITEM_KEY; 2096a542ad1bSJan Schmidt key.objectid = logical; 2097a542ad1bSJan Schmidt key.offset = (u64)-1; 2098a542ad1bSJan Schmidt 209929cbcf40SJosef Bacik ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0); 2100a542ad1bSJan Schmidt if (ret < 0) 2101a542ad1bSJan Schmidt return ret; 2102a542ad1bSJan Schmidt 210329cbcf40SJosef Bacik ret = btrfs_previous_extent_item(extent_root, path, 0); 2104850a8cdfSWang Shilong if (ret) { 2105850a8cdfSWang Shilong if (ret > 0) 2106580f0a67SJosef Bacik ret = -ENOENT; 2107580f0a67SJosef Bacik return ret; 2108580f0a67SJosef Bacik } 2109850a8cdfSWang Shilong btrfs_item_key_to_cpu(path->nodes[0], found_key, path->slots[0]); 2110261c84b6SJosef Bacik if (found_key->type == BTRFS_METADATA_ITEM_KEY) 2111da17066cSJeff Mahoney size = fs_info->nodesize; 2112261c84b6SJosef Bacik else if (found_key->type == BTRFS_EXTENT_ITEM_KEY) 2113261c84b6SJosef Bacik size = found_key->offset; 2114261c84b6SJosef Bacik 2115580f0a67SJosef Bacik if (found_key->objectid > logical || 2116261c84b6SJosef Bacik found_key->objectid + size <= logical) { 2117ab8d0fc4SJeff Mahoney btrfs_debug(fs_info, 2118ab8d0fc4SJeff Mahoney "logical %llu is not within any extent", logical); 2119a542ad1bSJan Schmidt return -ENOENT; 21204692cf58SJan Schmidt } 2121a542ad1bSJan Schmidt 2122a542ad1bSJan Schmidt eb = path->nodes[0]; 21233212fa14SJosef Bacik item_size = btrfs_item_size(eb, path->slots[0]); 2124a542ad1bSJan Schmidt BUG_ON(item_size < sizeof(*ei)); 2125a542ad1bSJan Schmidt 2126a542ad1bSJan Schmidt ei = btrfs_item_ptr(eb, path->slots[0], struct btrfs_extent_item); 2127a542ad1bSJan Schmidt flags = btrfs_extent_flags(eb, ei); 2128a542ad1bSJan Schmidt 2129ab8d0fc4SJeff Mahoney btrfs_debug(fs_info, 2130ab8d0fc4SJeff Mahoney "logical %llu is at position %llu within the extent (%llu EXTENT_ITEM %llu) flags %#llx size %u", 2131c1c9ff7cSGeert Uytterhoeven logical, logical - found_key->objectid, found_key->objectid, 2132c1c9ff7cSGeert Uytterhoeven found_key->offset, flags, item_size); 213369917e43SLiu Bo 213469917e43SLiu Bo WARN_ON(!flags_ret); 213569917e43SLiu Bo if (flags_ret) { 2136a542ad1bSJan Schmidt if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) 213769917e43SLiu Bo *flags_ret = BTRFS_EXTENT_FLAG_TREE_BLOCK; 213869917e43SLiu Bo else if (flags & BTRFS_EXTENT_FLAG_DATA) 213969917e43SLiu Bo *flags_ret = BTRFS_EXTENT_FLAG_DATA; 214069917e43SLiu Bo else 2141290342f6SArnd Bergmann BUG(); 214269917e43SLiu Bo return 0; 214369917e43SLiu Bo } 2144a542ad1bSJan Schmidt 2145a542ad1bSJan Schmidt return -EIO; 2146a542ad1bSJan Schmidt } 2147a542ad1bSJan Schmidt 2148a542ad1bSJan Schmidt /* 2149a542ad1bSJan Schmidt * helper function to iterate extent inline refs. ptr must point to a 0 value 2150a542ad1bSJan Schmidt * for the first call and may be modified. it is used to track state. 2151a542ad1bSJan Schmidt * if more refs exist, 0 is returned and the next call to 2152e0c476b1SJeff Mahoney * get_extent_inline_ref must pass the modified ptr parameter to get the 2153a542ad1bSJan Schmidt * next ref. after the last ref was processed, 1 is returned. 2154a542ad1bSJan Schmidt * returns <0 on error 2155a542ad1bSJan Schmidt */ 2156e0c476b1SJeff Mahoney static int get_extent_inline_ref(unsigned long *ptr, 215773980becSJeff Mahoney const struct extent_buffer *eb, 215873980becSJeff Mahoney const struct btrfs_key *key, 215973980becSJeff Mahoney const struct btrfs_extent_item *ei, 216073980becSJeff Mahoney u32 item_size, 2161a542ad1bSJan Schmidt struct btrfs_extent_inline_ref **out_eiref, 2162a542ad1bSJan Schmidt int *out_type) 2163a542ad1bSJan Schmidt { 2164a542ad1bSJan Schmidt unsigned long end; 2165a542ad1bSJan Schmidt u64 flags; 2166a542ad1bSJan Schmidt struct btrfs_tree_block_info *info; 2167a542ad1bSJan Schmidt 2168a542ad1bSJan Schmidt if (!*ptr) { 2169a542ad1bSJan Schmidt /* first call */ 2170a542ad1bSJan Schmidt flags = btrfs_extent_flags(eb, ei); 2171a542ad1bSJan Schmidt if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) { 21726eda71d0SLiu Bo if (key->type == BTRFS_METADATA_ITEM_KEY) { 21736eda71d0SLiu Bo /* a skinny metadata extent */ 21746eda71d0SLiu Bo *out_eiref = 21756eda71d0SLiu Bo (struct btrfs_extent_inline_ref *)(ei + 1); 21766eda71d0SLiu Bo } else { 21776eda71d0SLiu Bo WARN_ON(key->type != BTRFS_EXTENT_ITEM_KEY); 2178a542ad1bSJan Schmidt info = (struct btrfs_tree_block_info *)(ei + 1); 2179a542ad1bSJan Schmidt *out_eiref = 2180a542ad1bSJan Schmidt (struct btrfs_extent_inline_ref *)(info + 1); 21816eda71d0SLiu Bo } 2182a542ad1bSJan Schmidt } else { 2183a542ad1bSJan Schmidt *out_eiref = (struct btrfs_extent_inline_ref *)(ei + 1); 2184a542ad1bSJan Schmidt } 2185a542ad1bSJan Schmidt *ptr = (unsigned long)*out_eiref; 2186cd857dd6SLiu Bo if ((unsigned long)(*ptr) >= (unsigned long)ei + item_size) 2187a542ad1bSJan Schmidt return -ENOENT; 2188a542ad1bSJan Schmidt } 2189a542ad1bSJan Schmidt 2190a542ad1bSJan Schmidt end = (unsigned long)ei + item_size; 21916eda71d0SLiu Bo *out_eiref = (struct btrfs_extent_inline_ref *)(*ptr); 21923de28d57SLiu Bo *out_type = btrfs_get_extent_inline_ref_type(eb, *out_eiref, 21933de28d57SLiu Bo BTRFS_REF_TYPE_ANY); 21943de28d57SLiu Bo if (*out_type == BTRFS_REF_TYPE_INVALID) 2195af431dcbSSu Yue return -EUCLEAN; 2196a542ad1bSJan Schmidt 2197a542ad1bSJan Schmidt *ptr += btrfs_extent_inline_ref_size(*out_type); 2198a542ad1bSJan Schmidt WARN_ON(*ptr > end); 2199a542ad1bSJan Schmidt if (*ptr == end) 2200a542ad1bSJan Schmidt return 1; /* last */ 2201a542ad1bSJan Schmidt 2202a542ad1bSJan Schmidt return 0; 2203a542ad1bSJan Schmidt } 2204a542ad1bSJan Schmidt 2205a542ad1bSJan Schmidt /* 2206a542ad1bSJan Schmidt * reads the tree block backref for an extent. tree level and root are returned 2207a542ad1bSJan Schmidt * through out_level and out_root. ptr must point to a 0 value for the first 2208e0c476b1SJeff Mahoney * call and may be modified (see get_extent_inline_ref comment). 2209a542ad1bSJan Schmidt * returns 0 if data was provided, 1 if there was no more data to provide or 2210a542ad1bSJan Schmidt * <0 on error. 2211a542ad1bSJan Schmidt */ 2212a542ad1bSJan Schmidt int tree_backref_for_extent(unsigned long *ptr, struct extent_buffer *eb, 22136eda71d0SLiu Bo struct btrfs_key *key, struct btrfs_extent_item *ei, 22146eda71d0SLiu Bo u32 item_size, u64 *out_root, u8 *out_level) 2215a542ad1bSJan Schmidt { 2216a542ad1bSJan Schmidt int ret; 2217a542ad1bSJan Schmidt int type; 2218a542ad1bSJan Schmidt struct btrfs_extent_inline_ref *eiref; 2219a542ad1bSJan Schmidt 2220a542ad1bSJan Schmidt if (*ptr == (unsigned long)-1) 2221a542ad1bSJan Schmidt return 1; 2222a542ad1bSJan Schmidt 2223a542ad1bSJan Schmidt while (1) { 2224e0c476b1SJeff Mahoney ret = get_extent_inline_ref(ptr, eb, key, ei, item_size, 2225a542ad1bSJan Schmidt &eiref, &type); 2226a542ad1bSJan Schmidt if (ret < 0) 2227a542ad1bSJan Schmidt return ret; 2228a542ad1bSJan Schmidt 2229a542ad1bSJan Schmidt if (type == BTRFS_TREE_BLOCK_REF_KEY || 2230a542ad1bSJan Schmidt type == BTRFS_SHARED_BLOCK_REF_KEY) 2231a542ad1bSJan Schmidt break; 2232a542ad1bSJan Schmidt 2233a542ad1bSJan Schmidt if (ret == 1) 2234a542ad1bSJan Schmidt return 1; 2235a542ad1bSJan Schmidt } 2236a542ad1bSJan Schmidt 2237a542ad1bSJan Schmidt /* we can treat both ref types equally here */ 2238a542ad1bSJan Schmidt *out_root = btrfs_extent_inline_ref_offset(eb, eiref); 2239a1317f45SFilipe Manana 2240a1317f45SFilipe Manana if (key->type == BTRFS_EXTENT_ITEM_KEY) { 2241a1317f45SFilipe Manana struct btrfs_tree_block_info *info; 2242a1317f45SFilipe Manana 2243a1317f45SFilipe Manana info = (struct btrfs_tree_block_info *)(ei + 1); 2244a542ad1bSJan Schmidt *out_level = btrfs_tree_block_level(eb, info); 2245a1317f45SFilipe Manana } else { 2246a1317f45SFilipe Manana ASSERT(key->type == BTRFS_METADATA_ITEM_KEY); 2247a1317f45SFilipe Manana *out_level = (u8)key->offset; 2248a1317f45SFilipe Manana } 2249a542ad1bSJan Schmidt 2250a542ad1bSJan Schmidt if (ret == 1) 2251a542ad1bSJan Schmidt *ptr = (unsigned long)-1; 2252a542ad1bSJan Schmidt 2253a542ad1bSJan Schmidt return 0; 2254a542ad1bSJan Schmidt } 2255a542ad1bSJan Schmidt 2256ab8d0fc4SJeff Mahoney static int iterate_leaf_refs(struct btrfs_fs_info *fs_info, 2257ab8d0fc4SJeff Mahoney struct extent_inode_elem *inode_list, 2258976b1908SJan Schmidt u64 root, u64 extent_item_objectid, 22594692cf58SJan Schmidt iterate_extent_inodes_t *iterate, void *ctx) 2260a542ad1bSJan Schmidt { 2261976b1908SJan Schmidt struct extent_inode_elem *eie; 22624692cf58SJan Schmidt int ret = 0; 2263a542ad1bSJan Schmidt 2264976b1908SJan Schmidt for (eie = inode_list; eie; eie = eie->next) { 2265ab8d0fc4SJeff Mahoney btrfs_debug(fs_info, 2266ab8d0fc4SJeff Mahoney "ref for %llu resolved, key (%llu EXTEND_DATA %llu), root %llu", 2267ab8d0fc4SJeff Mahoney extent_item_objectid, eie->inum, 2268ab8d0fc4SJeff Mahoney eie->offset, root); 2269*c7499a64SFilipe Manana ret = iterate(eie->inum, eie->offset, eie->num_bytes, root, ctx); 22704692cf58SJan Schmidt if (ret) { 2271ab8d0fc4SJeff Mahoney btrfs_debug(fs_info, 2272ab8d0fc4SJeff Mahoney "stopping iteration for %llu due to ret=%d", 2273976b1908SJan Schmidt extent_item_objectid, ret); 2274a542ad1bSJan Schmidt break; 2275a542ad1bSJan Schmidt } 2276a542ad1bSJan Schmidt } 2277a542ad1bSJan Schmidt 2278a542ad1bSJan Schmidt return ret; 2279a542ad1bSJan Schmidt } 2280a542ad1bSJan Schmidt 2281a542ad1bSJan Schmidt /* 2282a542ad1bSJan Schmidt * calls iterate() for every inode that references the extent identified by 22834692cf58SJan Schmidt * the given parameters. 2284a542ad1bSJan Schmidt * when the iterator function returns a non-zero value, iteration stops. 2285a542ad1bSJan Schmidt */ 2286a542ad1bSJan Schmidt int iterate_extent_inodes(struct btrfs_fs_info *fs_info, 22874692cf58SJan Schmidt u64 extent_item_objectid, u64 extent_item_pos, 22887a3ae2f8SJan Schmidt int search_commit_root, 2289c995ab3cSZygo Blaxell iterate_extent_inodes_t *iterate, void *ctx, 2290c995ab3cSZygo Blaxell bool ignore_offset) 2291a542ad1bSJan Schmidt { 2292a542ad1bSJan Schmidt int ret; 2293da61d31aSJosef Bacik struct btrfs_trans_handle *trans = NULL; 22947a3ae2f8SJan Schmidt struct ulist *refs = NULL; 22957a3ae2f8SJan Schmidt struct ulist *roots = NULL; 22964692cf58SJan Schmidt struct ulist_node *ref_node = NULL; 22974692cf58SJan Schmidt struct ulist_node *root_node = NULL; 2298f3a84ccdSFilipe Manana struct btrfs_seq_list seq_elem = BTRFS_SEQ_LIST_INIT(seq_elem); 2299cd1b413cSJan Schmidt struct ulist_iterator ref_uiter; 2300cd1b413cSJan Schmidt struct ulist_iterator root_uiter; 2301a542ad1bSJan Schmidt 2302ab8d0fc4SJeff Mahoney btrfs_debug(fs_info, "resolving all inodes for extent %llu", 23034692cf58SJan Schmidt extent_item_objectid); 23044692cf58SJan Schmidt 2305da61d31aSJosef Bacik if (!search_commit_root) { 230630a9da5dSJosef Bacik trans = btrfs_attach_transaction(fs_info->tree_root); 2307bfc61c36SFilipe Manana if (IS_ERR(trans)) { 2308bfc61c36SFilipe Manana if (PTR_ERR(trans) != -ENOENT && 2309bfc61c36SFilipe Manana PTR_ERR(trans) != -EROFS) 23107a3ae2f8SJan Schmidt return PTR_ERR(trans); 2311bfc61c36SFilipe Manana trans = NULL; 23127a3ae2f8SJan Schmidt } 2313bfc61c36SFilipe Manana } 2314bfc61c36SFilipe Manana 2315bfc61c36SFilipe Manana if (trans) 2316f3a84ccdSFilipe Manana btrfs_get_tree_mod_seq(fs_info, &seq_elem); 2317bfc61c36SFilipe Manana else 2318bfc61c36SFilipe Manana down_read(&fs_info->commit_root_sem); 23194692cf58SJan Schmidt 23204692cf58SJan Schmidt ret = btrfs_find_all_leafs(trans, fs_info, extent_item_objectid, 2321f3a84ccdSFilipe Manana seq_elem.seq, &refs, 2322c995ab3cSZygo Blaxell &extent_item_pos, ignore_offset); 23234692cf58SJan Schmidt if (ret) 23244692cf58SJan Schmidt goto out; 23254692cf58SJan Schmidt 2326cd1b413cSJan Schmidt ULIST_ITER_INIT(&ref_uiter); 2327cd1b413cSJan Schmidt while (!ret && (ref_node = ulist_next(refs, &ref_uiter))) { 2328e0c476b1SJeff Mahoney ret = btrfs_find_all_roots_safe(trans, fs_info, ref_node->val, 2329f3a84ccdSFilipe Manana seq_elem.seq, &roots, 2330c995ab3cSZygo Blaxell ignore_offset); 23314692cf58SJan Schmidt if (ret) 2332a542ad1bSJan Schmidt break; 2333cd1b413cSJan Schmidt ULIST_ITER_INIT(&root_uiter); 2334cd1b413cSJan Schmidt while (!ret && (root_node = ulist_next(roots, &root_uiter))) { 2335ab8d0fc4SJeff Mahoney btrfs_debug(fs_info, 2336ab8d0fc4SJeff Mahoney "root %llu references leaf %llu, data list %#llx", 2337ab8d0fc4SJeff Mahoney root_node->val, ref_node->val, 2338c1c9ff7cSGeert Uytterhoeven ref_node->aux); 2339ab8d0fc4SJeff Mahoney ret = iterate_leaf_refs(fs_info, 2340ab8d0fc4SJeff Mahoney (struct extent_inode_elem *) 2341995e01b7SJan Schmidt (uintptr_t)ref_node->aux, 2342995e01b7SJan Schmidt root_node->val, 2343995e01b7SJan Schmidt extent_item_objectid, 2344a542ad1bSJan Schmidt iterate, ctx); 23454692cf58SJan Schmidt } 2346976b1908SJan Schmidt ulist_free(roots); 2347a542ad1bSJan Schmidt } 2348a542ad1bSJan Schmidt 2349976b1908SJan Schmidt free_leaf_list(refs); 23504692cf58SJan Schmidt out: 2351bfc61c36SFilipe Manana if (trans) { 2352f3a84ccdSFilipe Manana btrfs_put_tree_mod_seq(fs_info, &seq_elem); 23533a45bb20SJeff Mahoney btrfs_end_transaction(trans); 23549e351cc8SJosef Bacik } else { 23559e351cc8SJosef Bacik up_read(&fs_info->commit_root_sem); 23567a3ae2f8SJan Schmidt } 23577a3ae2f8SJan Schmidt 2358a542ad1bSJan Schmidt return ret; 2359a542ad1bSJan Schmidt } 2360a542ad1bSJan Schmidt 2361*c7499a64SFilipe Manana static int build_ino_list(u64 inum, u64 offset, u64 num_bytes, u64 root, void *ctx) 2362e3059ec0SDavid Sterba { 2363e3059ec0SDavid Sterba struct btrfs_data_container *inodes = ctx; 2364e3059ec0SDavid Sterba const size_t c = 3 * sizeof(u64); 2365e3059ec0SDavid Sterba 2366e3059ec0SDavid Sterba if (inodes->bytes_left >= c) { 2367e3059ec0SDavid Sterba inodes->bytes_left -= c; 2368e3059ec0SDavid Sterba inodes->val[inodes->elem_cnt] = inum; 2369e3059ec0SDavid Sterba inodes->val[inodes->elem_cnt + 1] = offset; 2370e3059ec0SDavid Sterba inodes->val[inodes->elem_cnt + 2] = root; 2371e3059ec0SDavid Sterba inodes->elem_cnt += 3; 2372e3059ec0SDavid Sterba } else { 2373e3059ec0SDavid Sterba inodes->bytes_missing += c - inodes->bytes_left; 2374e3059ec0SDavid Sterba inodes->bytes_left = 0; 2375e3059ec0SDavid Sterba inodes->elem_missed += 3; 2376e3059ec0SDavid Sterba } 2377e3059ec0SDavid Sterba 2378e3059ec0SDavid Sterba return 0; 2379e3059ec0SDavid Sterba } 2380e3059ec0SDavid Sterba 2381a542ad1bSJan Schmidt int iterate_inodes_from_logical(u64 logical, struct btrfs_fs_info *fs_info, 2382a542ad1bSJan Schmidt struct btrfs_path *path, 2383e3059ec0SDavid Sterba void *ctx, bool ignore_offset) 2384a542ad1bSJan Schmidt { 2385a542ad1bSJan Schmidt int ret; 23864692cf58SJan Schmidt u64 extent_item_pos; 238769917e43SLiu Bo u64 flags = 0; 2388a542ad1bSJan Schmidt struct btrfs_key found_key; 23897a3ae2f8SJan Schmidt int search_commit_root = path->search_commit_root; 2390a542ad1bSJan Schmidt 239169917e43SLiu Bo ret = extent_from_logical(fs_info, logical, path, &found_key, &flags); 23924692cf58SJan Schmidt btrfs_release_path(path); 2393a542ad1bSJan Schmidt if (ret < 0) 2394a542ad1bSJan Schmidt return ret; 239569917e43SLiu Bo if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) 23963627bf45SStefan Behrens return -EINVAL; 2397a542ad1bSJan Schmidt 23984692cf58SJan Schmidt extent_item_pos = logical - found_key.objectid; 23997a3ae2f8SJan Schmidt ret = iterate_extent_inodes(fs_info, found_key.objectid, 24007a3ae2f8SJan Schmidt extent_item_pos, search_commit_root, 2401e3059ec0SDavid Sterba build_ino_list, ctx, ignore_offset); 2402a542ad1bSJan Schmidt 2403a542ad1bSJan Schmidt return ret; 2404a542ad1bSJan Schmidt } 2405a542ad1bSJan Schmidt 2406ad6240f6SDavid Sterba static int inode_to_path(u64 inum, u32 name_len, unsigned long name_off, 2407875d1daaSDavid Sterba struct extent_buffer *eb, struct inode_fs_paths *ipath); 2408d24bec3aSMark Fasheh 2409875d1daaSDavid Sterba static int iterate_inode_refs(u64 inum, struct inode_fs_paths *ipath) 2410a542ad1bSJan Schmidt { 2411aefc1eb1SJan Schmidt int ret = 0; 2412a542ad1bSJan Schmidt int slot; 2413a542ad1bSJan Schmidt u32 cur; 2414a542ad1bSJan Schmidt u32 len; 2415a542ad1bSJan Schmidt u32 name_len; 2416a542ad1bSJan Schmidt u64 parent = 0; 2417a542ad1bSJan Schmidt int found = 0; 2418875d1daaSDavid Sterba struct btrfs_root *fs_root = ipath->fs_root; 2419875d1daaSDavid Sterba struct btrfs_path *path = ipath->btrfs_path; 2420a542ad1bSJan Schmidt struct extent_buffer *eb; 2421a542ad1bSJan Schmidt struct btrfs_inode_ref *iref; 2422a542ad1bSJan Schmidt struct btrfs_key found_key; 2423a542ad1bSJan Schmidt 2424aefc1eb1SJan Schmidt while (!ret) { 2425c234a24dSDavid Sterba ret = btrfs_find_item(fs_root, path, inum, 2426c234a24dSDavid Sterba parent ? parent + 1 : 0, BTRFS_INODE_REF_KEY, 2427a542ad1bSJan Schmidt &found_key); 2428c234a24dSDavid Sterba 2429a542ad1bSJan Schmidt if (ret < 0) 2430a542ad1bSJan Schmidt break; 2431a542ad1bSJan Schmidt if (ret) { 2432a542ad1bSJan Schmidt ret = found ? 0 : -ENOENT; 2433a542ad1bSJan Schmidt break; 2434a542ad1bSJan Schmidt } 2435a542ad1bSJan Schmidt ++found; 2436a542ad1bSJan Schmidt 2437a542ad1bSJan Schmidt parent = found_key.offset; 2438a542ad1bSJan Schmidt slot = path->slots[0]; 24393fe81ce2SFilipe David Borba Manana eb = btrfs_clone_extent_buffer(path->nodes[0]); 24403fe81ce2SFilipe David Borba Manana if (!eb) { 24413fe81ce2SFilipe David Borba Manana ret = -ENOMEM; 24423fe81ce2SFilipe David Borba Manana break; 24433fe81ce2SFilipe David Borba Manana } 2444a542ad1bSJan Schmidt btrfs_release_path(path); 2445a542ad1bSJan Schmidt 2446a542ad1bSJan Schmidt iref = btrfs_item_ptr(eb, slot, struct btrfs_inode_ref); 2447a542ad1bSJan Schmidt 24483212fa14SJosef Bacik for (cur = 0; cur < btrfs_item_size(eb, slot); cur += len) { 2449a542ad1bSJan Schmidt name_len = btrfs_inode_ref_name_len(eb, iref); 2450a542ad1bSJan Schmidt /* path must be released before calling iterate()! */ 2451ab8d0fc4SJeff Mahoney btrfs_debug(fs_root->fs_info, 2452ab8d0fc4SJeff Mahoney "following ref at offset %u for inode %llu in tree %llu", 24534fd786e6SMisono Tomohiro cur, found_key.objectid, 24544fd786e6SMisono Tomohiro fs_root->root_key.objectid); 2455ad6240f6SDavid Sterba ret = inode_to_path(parent, name_len, 2456875d1daaSDavid Sterba (unsigned long)(iref + 1), eb, ipath); 2457aefc1eb1SJan Schmidt if (ret) 2458a542ad1bSJan Schmidt break; 2459a542ad1bSJan Schmidt len = sizeof(*iref) + name_len; 2460a542ad1bSJan Schmidt iref = (struct btrfs_inode_ref *)((char *)iref + len); 2461a542ad1bSJan Schmidt } 2462a542ad1bSJan Schmidt free_extent_buffer(eb); 2463a542ad1bSJan Schmidt } 2464a542ad1bSJan Schmidt 2465a542ad1bSJan Schmidt btrfs_release_path(path); 2466a542ad1bSJan Schmidt 2467a542ad1bSJan Schmidt return ret; 2468a542ad1bSJan Schmidt } 2469a542ad1bSJan Schmidt 2470875d1daaSDavid Sterba static int iterate_inode_extrefs(u64 inum, struct inode_fs_paths *ipath) 2471d24bec3aSMark Fasheh { 2472d24bec3aSMark Fasheh int ret; 2473d24bec3aSMark Fasheh int slot; 2474d24bec3aSMark Fasheh u64 offset = 0; 2475d24bec3aSMark Fasheh u64 parent; 2476d24bec3aSMark Fasheh int found = 0; 2477875d1daaSDavid Sterba struct btrfs_root *fs_root = ipath->fs_root; 2478875d1daaSDavid Sterba struct btrfs_path *path = ipath->btrfs_path; 2479d24bec3aSMark Fasheh struct extent_buffer *eb; 2480d24bec3aSMark Fasheh struct btrfs_inode_extref *extref; 2481d24bec3aSMark Fasheh u32 item_size; 2482d24bec3aSMark Fasheh u32 cur_offset; 2483d24bec3aSMark Fasheh unsigned long ptr; 2484d24bec3aSMark Fasheh 2485d24bec3aSMark Fasheh while (1) { 2486d24bec3aSMark Fasheh ret = btrfs_find_one_extref(fs_root, inum, offset, path, &extref, 2487d24bec3aSMark Fasheh &offset); 2488d24bec3aSMark Fasheh if (ret < 0) 2489d24bec3aSMark Fasheh break; 2490d24bec3aSMark Fasheh if (ret) { 2491d24bec3aSMark Fasheh ret = found ? 0 : -ENOENT; 2492d24bec3aSMark Fasheh break; 2493d24bec3aSMark Fasheh } 2494d24bec3aSMark Fasheh ++found; 2495d24bec3aSMark Fasheh 2496d24bec3aSMark Fasheh slot = path->slots[0]; 24973fe81ce2SFilipe David Borba Manana eb = btrfs_clone_extent_buffer(path->nodes[0]); 24983fe81ce2SFilipe David Borba Manana if (!eb) { 24993fe81ce2SFilipe David Borba Manana ret = -ENOMEM; 25003fe81ce2SFilipe David Borba Manana break; 25013fe81ce2SFilipe David Borba Manana } 2502d24bec3aSMark Fasheh btrfs_release_path(path); 2503d24bec3aSMark Fasheh 25043212fa14SJosef Bacik item_size = btrfs_item_size(eb, slot); 25052849a854SChris Mason ptr = btrfs_item_ptr_offset(eb, slot); 2506d24bec3aSMark Fasheh cur_offset = 0; 2507d24bec3aSMark Fasheh 2508d24bec3aSMark Fasheh while (cur_offset < item_size) { 2509d24bec3aSMark Fasheh u32 name_len; 2510d24bec3aSMark Fasheh 2511d24bec3aSMark Fasheh extref = (struct btrfs_inode_extref *)(ptr + cur_offset); 2512d24bec3aSMark Fasheh parent = btrfs_inode_extref_parent(eb, extref); 2513d24bec3aSMark Fasheh name_len = btrfs_inode_extref_name_len(eb, extref); 2514ad6240f6SDavid Sterba ret = inode_to_path(parent, name_len, 2515875d1daaSDavid Sterba (unsigned long)&extref->name, eb, ipath); 2516d24bec3aSMark Fasheh if (ret) 2517d24bec3aSMark Fasheh break; 2518d24bec3aSMark Fasheh 25192849a854SChris Mason cur_offset += btrfs_inode_extref_name_len(eb, extref); 2520d24bec3aSMark Fasheh cur_offset += sizeof(*extref); 2521d24bec3aSMark Fasheh } 2522d24bec3aSMark Fasheh free_extent_buffer(eb); 2523d24bec3aSMark Fasheh 2524d24bec3aSMark Fasheh offset++; 2525d24bec3aSMark Fasheh } 2526d24bec3aSMark Fasheh 2527d24bec3aSMark Fasheh btrfs_release_path(path); 2528d24bec3aSMark Fasheh 2529d24bec3aSMark Fasheh return ret; 2530d24bec3aSMark Fasheh } 2531d24bec3aSMark Fasheh 2532a542ad1bSJan Schmidt /* 2533a542ad1bSJan Schmidt * returns 0 if the path could be dumped (probably truncated) 2534a542ad1bSJan Schmidt * returns <0 in case of an error 2535a542ad1bSJan Schmidt */ 2536d24bec3aSMark Fasheh static int inode_to_path(u64 inum, u32 name_len, unsigned long name_off, 2537875d1daaSDavid Sterba struct extent_buffer *eb, struct inode_fs_paths *ipath) 2538a542ad1bSJan Schmidt { 2539a542ad1bSJan Schmidt char *fspath; 2540a542ad1bSJan Schmidt char *fspath_min; 2541a542ad1bSJan Schmidt int i = ipath->fspath->elem_cnt; 2542a542ad1bSJan Schmidt const int s_ptr = sizeof(char *); 2543a542ad1bSJan Schmidt u32 bytes_left; 2544a542ad1bSJan Schmidt 2545a542ad1bSJan Schmidt bytes_left = ipath->fspath->bytes_left > s_ptr ? 2546a542ad1bSJan Schmidt ipath->fspath->bytes_left - s_ptr : 0; 2547a542ad1bSJan Schmidt 2548740c3d22SChris Mason fspath_min = (char *)ipath->fspath->val + (i + 1) * s_ptr; 254996b5bd77SJan Schmidt fspath = btrfs_ref_to_path(ipath->fs_root, ipath->btrfs_path, name_len, 255096b5bd77SJan Schmidt name_off, eb, inum, fspath_min, bytes_left); 2551a542ad1bSJan Schmidt if (IS_ERR(fspath)) 2552a542ad1bSJan Schmidt return PTR_ERR(fspath); 2553a542ad1bSJan Schmidt 2554a542ad1bSJan Schmidt if (fspath > fspath_min) { 2555745c4d8eSJeff Mahoney ipath->fspath->val[i] = (u64)(unsigned long)fspath; 2556a542ad1bSJan Schmidt ++ipath->fspath->elem_cnt; 2557a542ad1bSJan Schmidt ipath->fspath->bytes_left = fspath - fspath_min; 2558a542ad1bSJan Schmidt } else { 2559a542ad1bSJan Schmidt ++ipath->fspath->elem_missed; 2560a542ad1bSJan Schmidt ipath->fspath->bytes_missing += fspath_min - fspath; 2561a542ad1bSJan Schmidt ipath->fspath->bytes_left = 0; 2562a542ad1bSJan Schmidt } 2563a542ad1bSJan Schmidt 2564a542ad1bSJan Schmidt return 0; 2565a542ad1bSJan Schmidt } 2566a542ad1bSJan Schmidt 2567a542ad1bSJan Schmidt /* 2568a542ad1bSJan Schmidt * this dumps all file system paths to the inode into the ipath struct, provided 2569a542ad1bSJan Schmidt * is has been created large enough. each path is zero-terminated and accessed 2570740c3d22SChris Mason * from ipath->fspath->val[i]. 2571a542ad1bSJan Schmidt * when it returns, there are ipath->fspath->elem_cnt number of paths available 2572740c3d22SChris Mason * in ipath->fspath->val[]. when the allocated space wasn't sufficient, the 257301327610SNicholas D Steeves * number of missed paths is recorded in ipath->fspath->elem_missed, otherwise, 2574a542ad1bSJan Schmidt * it's zero. ipath->fspath->bytes_missing holds the number of bytes that would 2575a542ad1bSJan Schmidt * have been needed to return all paths. 2576a542ad1bSJan Schmidt */ 2577a542ad1bSJan Schmidt int paths_from_inode(u64 inum, struct inode_fs_paths *ipath) 2578a542ad1bSJan Schmidt { 2579ad6240f6SDavid Sterba int ret; 2580ad6240f6SDavid Sterba int found_refs = 0; 2581ad6240f6SDavid Sterba 2582875d1daaSDavid Sterba ret = iterate_inode_refs(inum, ipath); 2583ad6240f6SDavid Sterba if (!ret) 2584ad6240f6SDavid Sterba ++found_refs; 2585ad6240f6SDavid Sterba else if (ret != -ENOENT) 2586ad6240f6SDavid Sterba return ret; 2587ad6240f6SDavid Sterba 2588875d1daaSDavid Sterba ret = iterate_inode_extrefs(inum, ipath); 2589ad6240f6SDavid Sterba if (ret == -ENOENT && found_refs) 2590ad6240f6SDavid Sterba return 0; 2591ad6240f6SDavid Sterba 2592ad6240f6SDavid Sterba return ret; 2593a542ad1bSJan Schmidt } 2594a542ad1bSJan Schmidt 2595a542ad1bSJan Schmidt struct btrfs_data_container *init_data_container(u32 total_bytes) 2596a542ad1bSJan Schmidt { 2597a542ad1bSJan Schmidt struct btrfs_data_container *data; 2598a542ad1bSJan Schmidt size_t alloc_bytes; 2599a542ad1bSJan Schmidt 2600a542ad1bSJan Schmidt alloc_bytes = max_t(size_t, total_bytes, sizeof(*data)); 2601f54de068SDavid Sterba data = kvmalloc(alloc_bytes, GFP_KERNEL); 2602a542ad1bSJan Schmidt if (!data) 2603a542ad1bSJan Schmidt return ERR_PTR(-ENOMEM); 2604a542ad1bSJan Schmidt 2605a542ad1bSJan Schmidt if (total_bytes >= sizeof(*data)) { 2606a542ad1bSJan Schmidt data->bytes_left = total_bytes - sizeof(*data); 2607a542ad1bSJan Schmidt data->bytes_missing = 0; 2608a542ad1bSJan Schmidt } else { 2609a542ad1bSJan Schmidt data->bytes_missing = sizeof(*data) - total_bytes; 2610a542ad1bSJan Schmidt data->bytes_left = 0; 2611a542ad1bSJan Schmidt } 2612a542ad1bSJan Schmidt 2613a542ad1bSJan Schmidt data->elem_cnt = 0; 2614a542ad1bSJan Schmidt data->elem_missed = 0; 2615a542ad1bSJan Schmidt 2616a542ad1bSJan Schmidt return data; 2617a542ad1bSJan Schmidt } 2618a542ad1bSJan Schmidt 2619a542ad1bSJan Schmidt /* 2620a542ad1bSJan Schmidt * allocates space to return multiple file system paths for an inode. 2621a542ad1bSJan Schmidt * total_bytes to allocate are passed, note that space usable for actual path 2622a542ad1bSJan Schmidt * information will be total_bytes - sizeof(struct inode_fs_paths). 2623a542ad1bSJan Schmidt * the returned pointer must be freed with free_ipath() in the end. 2624a542ad1bSJan Schmidt */ 2625a542ad1bSJan Schmidt struct inode_fs_paths *init_ipath(s32 total_bytes, struct btrfs_root *fs_root, 2626a542ad1bSJan Schmidt struct btrfs_path *path) 2627a542ad1bSJan Schmidt { 2628a542ad1bSJan Schmidt struct inode_fs_paths *ifp; 2629a542ad1bSJan Schmidt struct btrfs_data_container *fspath; 2630a542ad1bSJan Schmidt 2631a542ad1bSJan Schmidt fspath = init_data_container(total_bytes); 2632a542ad1bSJan Schmidt if (IS_ERR(fspath)) 2633afc6961fSMisono Tomohiro return ERR_CAST(fspath); 2634a542ad1bSJan Schmidt 2635f54de068SDavid Sterba ifp = kmalloc(sizeof(*ifp), GFP_KERNEL); 2636a542ad1bSJan Schmidt if (!ifp) { 2637f54de068SDavid Sterba kvfree(fspath); 2638a542ad1bSJan Schmidt return ERR_PTR(-ENOMEM); 2639a542ad1bSJan Schmidt } 2640a542ad1bSJan Schmidt 2641a542ad1bSJan Schmidt ifp->btrfs_path = path; 2642a542ad1bSJan Schmidt ifp->fspath = fspath; 2643a542ad1bSJan Schmidt ifp->fs_root = fs_root; 2644a542ad1bSJan Schmidt 2645a542ad1bSJan Schmidt return ifp; 2646a542ad1bSJan Schmidt } 2647a542ad1bSJan Schmidt 2648a542ad1bSJan Schmidt void free_ipath(struct inode_fs_paths *ipath) 2649a542ad1bSJan Schmidt { 26504735fb28SJesper Juhl if (!ipath) 26514735fb28SJesper Juhl return; 2652f54de068SDavid Sterba kvfree(ipath->fspath); 2653a542ad1bSJan Schmidt kfree(ipath); 2654a542ad1bSJan Schmidt } 2655a37f232bSQu Wenruo 2656d68194b2SDavid Sterba struct btrfs_backref_iter *btrfs_backref_iter_alloc(struct btrfs_fs_info *fs_info) 2657a37f232bSQu Wenruo { 2658a37f232bSQu Wenruo struct btrfs_backref_iter *ret; 2659a37f232bSQu Wenruo 2660d68194b2SDavid Sterba ret = kzalloc(sizeof(*ret), GFP_NOFS); 2661a37f232bSQu Wenruo if (!ret) 2662a37f232bSQu Wenruo return NULL; 2663a37f232bSQu Wenruo 2664a37f232bSQu Wenruo ret->path = btrfs_alloc_path(); 2665c15c2ec0SBoleyn Su if (!ret->path) { 2666a37f232bSQu Wenruo kfree(ret); 2667a37f232bSQu Wenruo return NULL; 2668a37f232bSQu Wenruo } 2669a37f232bSQu Wenruo 2670a37f232bSQu Wenruo /* Current backref iterator only supports iteration in commit root */ 2671a37f232bSQu Wenruo ret->path->search_commit_root = 1; 2672a37f232bSQu Wenruo ret->path->skip_locking = 1; 2673a37f232bSQu Wenruo ret->fs_info = fs_info; 2674a37f232bSQu Wenruo 2675a37f232bSQu Wenruo return ret; 2676a37f232bSQu Wenruo } 2677a37f232bSQu Wenruo 2678a37f232bSQu Wenruo int btrfs_backref_iter_start(struct btrfs_backref_iter *iter, u64 bytenr) 2679a37f232bSQu Wenruo { 2680a37f232bSQu Wenruo struct btrfs_fs_info *fs_info = iter->fs_info; 268129cbcf40SJosef Bacik struct btrfs_root *extent_root = btrfs_extent_root(fs_info, bytenr); 2682a37f232bSQu Wenruo struct btrfs_path *path = iter->path; 2683a37f232bSQu Wenruo struct btrfs_extent_item *ei; 2684a37f232bSQu Wenruo struct btrfs_key key; 2685a37f232bSQu Wenruo int ret; 2686a37f232bSQu Wenruo 2687a37f232bSQu Wenruo key.objectid = bytenr; 2688a37f232bSQu Wenruo key.type = BTRFS_METADATA_ITEM_KEY; 2689a37f232bSQu Wenruo key.offset = (u64)-1; 2690a37f232bSQu Wenruo iter->bytenr = bytenr; 2691a37f232bSQu Wenruo 269229cbcf40SJosef Bacik ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0); 2693a37f232bSQu Wenruo if (ret < 0) 2694a37f232bSQu Wenruo return ret; 2695a37f232bSQu Wenruo if (ret == 0) { 2696a37f232bSQu Wenruo ret = -EUCLEAN; 2697a37f232bSQu Wenruo goto release; 2698a37f232bSQu Wenruo } 2699a37f232bSQu Wenruo if (path->slots[0] == 0) { 2700a37f232bSQu Wenruo WARN_ON(IS_ENABLED(CONFIG_BTRFS_DEBUG)); 2701a37f232bSQu Wenruo ret = -EUCLEAN; 2702a37f232bSQu Wenruo goto release; 2703a37f232bSQu Wenruo } 2704a37f232bSQu Wenruo path->slots[0]--; 2705a37f232bSQu Wenruo 2706a37f232bSQu Wenruo btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); 2707a37f232bSQu Wenruo if ((key.type != BTRFS_EXTENT_ITEM_KEY && 2708a37f232bSQu Wenruo key.type != BTRFS_METADATA_ITEM_KEY) || key.objectid != bytenr) { 2709a37f232bSQu Wenruo ret = -ENOENT; 2710a37f232bSQu Wenruo goto release; 2711a37f232bSQu Wenruo } 2712a37f232bSQu Wenruo memcpy(&iter->cur_key, &key, sizeof(key)); 2713a37f232bSQu Wenruo iter->item_ptr = (u32)btrfs_item_ptr_offset(path->nodes[0], 2714a37f232bSQu Wenruo path->slots[0]); 2715a37f232bSQu Wenruo iter->end_ptr = (u32)(iter->item_ptr + 27163212fa14SJosef Bacik btrfs_item_size(path->nodes[0], path->slots[0])); 2717a37f232bSQu Wenruo ei = btrfs_item_ptr(path->nodes[0], path->slots[0], 2718a37f232bSQu Wenruo struct btrfs_extent_item); 2719a37f232bSQu Wenruo 2720a37f232bSQu Wenruo /* 2721a37f232bSQu Wenruo * Only support iteration on tree backref yet. 2722a37f232bSQu Wenruo * 2723a37f232bSQu Wenruo * This is an extra precaution for non skinny-metadata, where 2724a37f232bSQu Wenruo * EXTENT_ITEM is also used for tree blocks, that we can only use 2725a37f232bSQu Wenruo * extent flags to determine if it's a tree block. 2726a37f232bSQu Wenruo */ 2727a37f232bSQu Wenruo if (btrfs_extent_flags(path->nodes[0], ei) & BTRFS_EXTENT_FLAG_DATA) { 2728a37f232bSQu Wenruo ret = -ENOTSUPP; 2729a37f232bSQu Wenruo goto release; 2730a37f232bSQu Wenruo } 2731a37f232bSQu Wenruo iter->cur_ptr = (u32)(iter->item_ptr + sizeof(*ei)); 2732a37f232bSQu Wenruo 2733a37f232bSQu Wenruo /* If there is no inline backref, go search for keyed backref */ 2734a37f232bSQu Wenruo if (iter->cur_ptr >= iter->end_ptr) { 273529cbcf40SJosef Bacik ret = btrfs_next_item(extent_root, path); 2736a37f232bSQu Wenruo 2737a37f232bSQu Wenruo /* No inline nor keyed ref */ 2738a37f232bSQu Wenruo if (ret > 0) { 2739a37f232bSQu Wenruo ret = -ENOENT; 2740a37f232bSQu Wenruo goto release; 2741a37f232bSQu Wenruo } 2742a37f232bSQu Wenruo if (ret < 0) 2743a37f232bSQu Wenruo goto release; 2744a37f232bSQu Wenruo 2745a37f232bSQu Wenruo btrfs_item_key_to_cpu(path->nodes[0], &iter->cur_key, 2746a37f232bSQu Wenruo path->slots[0]); 2747a37f232bSQu Wenruo if (iter->cur_key.objectid != bytenr || 2748a37f232bSQu Wenruo (iter->cur_key.type != BTRFS_SHARED_BLOCK_REF_KEY && 2749a37f232bSQu Wenruo iter->cur_key.type != BTRFS_TREE_BLOCK_REF_KEY)) { 2750a37f232bSQu Wenruo ret = -ENOENT; 2751a37f232bSQu Wenruo goto release; 2752a37f232bSQu Wenruo } 2753a37f232bSQu Wenruo iter->cur_ptr = (u32)btrfs_item_ptr_offset(path->nodes[0], 2754a37f232bSQu Wenruo path->slots[0]); 2755a37f232bSQu Wenruo iter->item_ptr = iter->cur_ptr; 27563212fa14SJosef Bacik iter->end_ptr = (u32)(iter->item_ptr + btrfs_item_size( 2757a37f232bSQu Wenruo path->nodes[0], path->slots[0])); 2758a37f232bSQu Wenruo } 2759a37f232bSQu Wenruo 2760a37f232bSQu Wenruo return 0; 2761a37f232bSQu Wenruo release: 2762a37f232bSQu Wenruo btrfs_backref_iter_release(iter); 2763a37f232bSQu Wenruo return ret; 2764a37f232bSQu Wenruo } 2765c39c2ddcSQu Wenruo 2766c39c2ddcSQu Wenruo /* 2767c39c2ddcSQu Wenruo * Go to the next backref item of current bytenr, can be either inlined or 2768c39c2ddcSQu Wenruo * keyed. 2769c39c2ddcSQu Wenruo * 2770c39c2ddcSQu Wenruo * Caller needs to check whether it's inline ref or not by iter->cur_key. 2771c39c2ddcSQu Wenruo * 2772c39c2ddcSQu Wenruo * Return 0 if we get next backref without problem. 2773c39c2ddcSQu Wenruo * Return >0 if there is no extra backref for this bytenr. 2774c39c2ddcSQu Wenruo * Return <0 if there is something wrong happened. 2775c39c2ddcSQu Wenruo */ 2776c39c2ddcSQu Wenruo int btrfs_backref_iter_next(struct btrfs_backref_iter *iter) 2777c39c2ddcSQu Wenruo { 2778c39c2ddcSQu Wenruo struct extent_buffer *eb = btrfs_backref_get_eb(iter); 277929cbcf40SJosef Bacik struct btrfs_root *extent_root; 2780c39c2ddcSQu Wenruo struct btrfs_path *path = iter->path; 2781c39c2ddcSQu Wenruo struct btrfs_extent_inline_ref *iref; 2782c39c2ddcSQu Wenruo int ret; 2783c39c2ddcSQu Wenruo u32 size; 2784c39c2ddcSQu Wenruo 2785c39c2ddcSQu Wenruo if (btrfs_backref_iter_is_inline_ref(iter)) { 2786c39c2ddcSQu Wenruo /* We're still inside the inline refs */ 2787c39c2ddcSQu Wenruo ASSERT(iter->cur_ptr < iter->end_ptr); 2788c39c2ddcSQu Wenruo 2789c39c2ddcSQu Wenruo if (btrfs_backref_has_tree_block_info(iter)) { 2790c39c2ddcSQu Wenruo /* First tree block info */ 2791c39c2ddcSQu Wenruo size = sizeof(struct btrfs_tree_block_info); 2792c39c2ddcSQu Wenruo } else { 2793c39c2ddcSQu Wenruo /* Use inline ref type to determine the size */ 2794c39c2ddcSQu Wenruo int type; 2795c39c2ddcSQu Wenruo 2796c39c2ddcSQu Wenruo iref = (struct btrfs_extent_inline_ref *) 2797c39c2ddcSQu Wenruo ((unsigned long)iter->cur_ptr); 2798c39c2ddcSQu Wenruo type = btrfs_extent_inline_ref_type(eb, iref); 2799c39c2ddcSQu Wenruo 2800c39c2ddcSQu Wenruo size = btrfs_extent_inline_ref_size(type); 2801c39c2ddcSQu Wenruo } 2802c39c2ddcSQu Wenruo iter->cur_ptr += size; 2803c39c2ddcSQu Wenruo if (iter->cur_ptr < iter->end_ptr) 2804c39c2ddcSQu Wenruo return 0; 2805c39c2ddcSQu Wenruo 2806c39c2ddcSQu Wenruo /* All inline items iterated, fall through */ 2807c39c2ddcSQu Wenruo } 2808c39c2ddcSQu Wenruo 2809c39c2ddcSQu Wenruo /* We're at keyed items, there is no inline item, go to the next one */ 281029cbcf40SJosef Bacik extent_root = btrfs_extent_root(iter->fs_info, iter->bytenr); 281129cbcf40SJosef Bacik ret = btrfs_next_item(extent_root, iter->path); 2812c39c2ddcSQu Wenruo if (ret) 2813c39c2ddcSQu Wenruo return ret; 2814c39c2ddcSQu Wenruo 2815c39c2ddcSQu Wenruo btrfs_item_key_to_cpu(path->nodes[0], &iter->cur_key, path->slots[0]); 2816c39c2ddcSQu Wenruo if (iter->cur_key.objectid != iter->bytenr || 2817c39c2ddcSQu Wenruo (iter->cur_key.type != BTRFS_TREE_BLOCK_REF_KEY && 2818c39c2ddcSQu Wenruo iter->cur_key.type != BTRFS_SHARED_BLOCK_REF_KEY)) 2819c39c2ddcSQu Wenruo return 1; 2820c39c2ddcSQu Wenruo iter->item_ptr = (u32)btrfs_item_ptr_offset(path->nodes[0], 2821c39c2ddcSQu Wenruo path->slots[0]); 2822c39c2ddcSQu Wenruo iter->cur_ptr = iter->item_ptr; 28233212fa14SJosef Bacik iter->end_ptr = iter->item_ptr + (u32)btrfs_item_size(path->nodes[0], 2824c39c2ddcSQu Wenruo path->slots[0]); 2825c39c2ddcSQu Wenruo return 0; 2826c39c2ddcSQu Wenruo } 2827584fb121SQu Wenruo 2828584fb121SQu Wenruo void btrfs_backref_init_cache(struct btrfs_fs_info *fs_info, 2829584fb121SQu Wenruo struct btrfs_backref_cache *cache, int is_reloc) 2830584fb121SQu Wenruo { 2831584fb121SQu Wenruo int i; 2832584fb121SQu Wenruo 2833584fb121SQu Wenruo cache->rb_root = RB_ROOT; 2834584fb121SQu Wenruo for (i = 0; i < BTRFS_MAX_LEVEL; i++) 2835584fb121SQu Wenruo INIT_LIST_HEAD(&cache->pending[i]); 2836584fb121SQu Wenruo INIT_LIST_HEAD(&cache->changed); 2837584fb121SQu Wenruo INIT_LIST_HEAD(&cache->detached); 2838584fb121SQu Wenruo INIT_LIST_HEAD(&cache->leaves); 2839584fb121SQu Wenruo INIT_LIST_HEAD(&cache->pending_edge); 2840584fb121SQu Wenruo INIT_LIST_HEAD(&cache->useless_node); 2841584fb121SQu Wenruo cache->fs_info = fs_info; 2842584fb121SQu Wenruo cache->is_reloc = is_reloc; 2843584fb121SQu Wenruo } 2844b1818dabSQu Wenruo 2845b1818dabSQu Wenruo struct btrfs_backref_node *btrfs_backref_alloc_node( 2846b1818dabSQu Wenruo struct btrfs_backref_cache *cache, u64 bytenr, int level) 2847b1818dabSQu Wenruo { 2848b1818dabSQu Wenruo struct btrfs_backref_node *node; 2849b1818dabSQu Wenruo 2850b1818dabSQu Wenruo ASSERT(level >= 0 && level < BTRFS_MAX_LEVEL); 2851b1818dabSQu Wenruo node = kzalloc(sizeof(*node), GFP_NOFS); 2852b1818dabSQu Wenruo if (!node) 2853b1818dabSQu Wenruo return node; 2854b1818dabSQu Wenruo 2855b1818dabSQu Wenruo INIT_LIST_HEAD(&node->list); 2856b1818dabSQu Wenruo INIT_LIST_HEAD(&node->upper); 2857b1818dabSQu Wenruo INIT_LIST_HEAD(&node->lower); 2858b1818dabSQu Wenruo RB_CLEAR_NODE(&node->rb_node); 2859b1818dabSQu Wenruo cache->nr_nodes++; 2860b1818dabSQu Wenruo node->level = level; 2861b1818dabSQu Wenruo node->bytenr = bytenr; 2862b1818dabSQu Wenruo 2863b1818dabSQu Wenruo return node; 2864b1818dabSQu Wenruo } 286547254d07SQu Wenruo 286647254d07SQu Wenruo struct btrfs_backref_edge *btrfs_backref_alloc_edge( 286747254d07SQu Wenruo struct btrfs_backref_cache *cache) 286847254d07SQu Wenruo { 286947254d07SQu Wenruo struct btrfs_backref_edge *edge; 287047254d07SQu Wenruo 287147254d07SQu Wenruo edge = kzalloc(sizeof(*edge), GFP_NOFS); 287247254d07SQu Wenruo if (edge) 287347254d07SQu Wenruo cache->nr_edges++; 287447254d07SQu Wenruo return edge; 287547254d07SQu Wenruo } 2876023acb07SQu Wenruo 2877023acb07SQu Wenruo /* 2878023acb07SQu Wenruo * Drop the backref node from cache, also cleaning up all its 2879023acb07SQu Wenruo * upper edges and any uncached nodes in the path. 2880023acb07SQu Wenruo * 2881023acb07SQu Wenruo * This cleanup happens bottom up, thus the node should either 2882023acb07SQu Wenruo * be the lowest node in the cache or a detached node. 2883023acb07SQu Wenruo */ 2884023acb07SQu Wenruo void btrfs_backref_cleanup_node(struct btrfs_backref_cache *cache, 2885023acb07SQu Wenruo struct btrfs_backref_node *node) 2886023acb07SQu Wenruo { 2887023acb07SQu Wenruo struct btrfs_backref_node *upper; 2888023acb07SQu Wenruo struct btrfs_backref_edge *edge; 2889023acb07SQu Wenruo 2890023acb07SQu Wenruo if (!node) 2891023acb07SQu Wenruo return; 2892023acb07SQu Wenruo 2893023acb07SQu Wenruo BUG_ON(!node->lowest && !node->detached); 2894023acb07SQu Wenruo while (!list_empty(&node->upper)) { 2895023acb07SQu Wenruo edge = list_entry(node->upper.next, struct btrfs_backref_edge, 2896023acb07SQu Wenruo list[LOWER]); 2897023acb07SQu Wenruo upper = edge->node[UPPER]; 2898023acb07SQu Wenruo list_del(&edge->list[LOWER]); 2899023acb07SQu Wenruo list_del(&edge->list[UPPER]); 2900023acb07SQu Wenruo btrfs_backref_free_edge(cache, edge); 2901023acb07SQu Wenruo 2902023acb07SQu Wenruo /* 2903023acb07SQu Wenruo * Add the node to leaf node list if no other child block 2904023acb07SQu Wenruo * cached. 2905023acb07SQu Wenruo */ 2906023acb07SQu Wenruo if (list_empty(&upper->lower)) { 2907023acb07SQu Wenruo list_add_tail(&upper->lower, &cache->leaves); 2908023acb07SQu Wenruo upper->lowest = 1; 2909023acb07SQu Wenruo } 2910023acb07SQu Wenruo } 2911023acb07SQu Wenruo 2912023acb07SQu Wenruo btrfs_backref_drop_node(cache, node); 2913023acb07SQu Wenruo } 291413fe1bdbSQu Wenruo 291513fe1bdbSQu Wenruo /* 291613fe1bdbSQu Wenruo * Release all nodes/edges from current cache 291713fe1bdbSQu Wenruo */ 291813fe1bdbSQu Wenruo void btrfs_backref_release_cache(struct btrfs_backref_cache *cache) 291913fe1bdbSQu Wenruo { 292013fe1bdbSQu Wenruo struct btrfs_backref_node *node; 292113fe1bdbSQu Wenruo int i; 292213fe1bdbSQu Wenruo 292313fe1bdbSQu Wenruo while (!list_empty(&cache->detached)) { 292413fe1bdbSQu Wenruo node = list_entry(cache->detached.next, 292513fe1bdbSQu Wenruo struct btrfs_backref_node, list); 292613fe1bdbSQu Wenruo btrfs_backref_cleanup_node(cache, node); 292713fe1bdbSQu Wenruo } 292813fe1bdbSQu Wenruo 292913fe1bdbSQu Wenruo while (!list_empty(&cache->leaves)) { 293013fe1bdbSQu Wenruo node = list_entry(cache->leaves.next, 293113fe1bdbSQu Wenruo struct btrfs_backref_node, lower); 293213fe1bdbSQu Wenruo btrfs_backref_cleanup_node(cache, node); 293313fe1bdbSQu Wenruo } 293413fe1bdbSQu Wenruo 293513fe1bdbSQu Wenruo cache->last_trans = 0; 293613fe1bdbSQu Wenruo 293713fe1bdbSQu Wenruo for (i = 0; i < BTRFS_MAX_LEVEL; i++) 293813fe1bdbSQu Wenruo ASSERT(list_empty(&cache->pending[i])); 293913fe1bdbSQu Wenruo ASSERT(list_empty(&cache->pending_edge)); 294013fe1bdbSQu Wenruo ASSERT(list_empty(&cache->useless_node)); 294113fe1bdbSQu Wenruo ASSERT(list_empty(&cache->changed)); 294213fe1bdbSQu Wenruo ASSERT(list_empty(&cache->detached)); 294313fe1bdbSQu Wenruo ASSERT(RB_EMPTY_ROOT(&cache->rb_root)); 294413fe1bdbSQu Wenruo ASSERT(!cache->nr_nodes); 294513fe1bdbSQu Wenruo ASSERT(!cache->nr_edges); 294613fe1bdbSQu Wenruo } 29471b60d2ecSQu Wenruo 29481b60d2ecSQu Wenruo /* 29491b60d2ecSQu Wenruo * Handle direct tree backref 29501b60d2ecSQu Wenruo * 29511b60d2ecSQu Wenruo * Direct tree backref means, the backref item shows its parent bytenr 29521b60d2ecSQu Wenruo * directly. This is for SHARED_BLOCK_REF backref (keyed or inlined). 29531b60d2ecSQu Wenruo * 29541b60d2ecSQu Wenruo * @ref_key: The converted backref key. 29551b60d2ecSQu Wenruo * For keyed backref, it's the item key. 29561b60d2ecSQu Wenruo * For inlined backref, objectid is the bytenr, 29571b60d2ecSQu Wenruo * type is btrfs_inline_ref_type, offset is 29581b60d2ecSQu Wenruo * btrfs_inline_ref_offset. 29591b60d2ecSQu Wenruo */ 29601b60d2ecSQu Wenruo static int handle_direct_tree_backref(struct btrfs_backref_cache *cache, 29611b60d2ecSQu Wenruo struct btrfs_key *ref_key, 29621b60d2ecSQu Wenruo struct btrfs_backref_node *cur) 29631b60d2ecSQu Wenruo { 29641b60d2ecSQu Wenruo struct btrfs_backref_edge *edge; 29651b60d2ecSQu Wenruo struct btrfs_backref_node *upper; 29661b60d2ecSQu Wenruo struct rb_node *rb_node; 29671b60d2ecSQu Wenruo 29681b60d2ecSQu Wenruo ASSERT(ref_key->type == BTRFS_SHARED_BLOCK_REF_KEY); 29691b60d2ecSQu Wenruo 29701b60d2ecSQu Wenruo /* Only reloc root uses backref pointing to itself */ 29711b60d2ecSQu Wenruo if (ref_key->objectid == ref_key->offset) { 29721b60d2ecSQu Wenruo struct btrfs_root *root; 29731b60d2ecSQu Wenruo 29741b60d2ecSQu Wenruo cur->is_reloc_root = 1; 29751b60d2ecSQu Wenruo /* Only reloc backref cache cares about a specific root */ 29761b60d2ecSQu Wenruo if (cache->is_reloc) { 29771b60d2ecSQu Wenruo root = find_reloc_root(cache->fs_info, cur->bytenr); 2978f78743fbSJosef Bacik if (!root) 29791b60d2ecSQu Wenruo return -ENOENT; 29801b60d2ecSQu Wenruo cur->root = root; 29811b60d2ecSQu Wenruo } else { 29821b60d2ecSQu Wenruo /* 29831b60d2ecSQu Wenruo * For generic purpose backref cache, reloc root node 29841b60d2ecSQu Wenruo * is useless. 29851b60d2ecSQu Wenruo */ 29861b60d2ecSQu Wenruo list_add(&cur->list, &cache->useless_node); 29871b60d2ecSQu Wenruo } 29881b60d2ecSQu Wenruo return 0; 29891b60d2ecSQu Wenruo } 29901b60d2ecSQu Wenruo 29911b60d2ecSQu Wenruo edge = btrfs_backref_alloc_edge(cache); 29921b60d2ecSQu Wenruo if (!edge) 29931b60d2ecSQu Wenruo return -ENOMEM; 29941b60d2ecSQu Wenruo 29951b60d2ecSQu Wenruo rb_node = rb_simple_search(&cache->rb_root, ref_key->offset); 29961b60d2ecSQu Wenruo if (!rb_node) { 29971b60d2ecSQu Wenruo /* Parent node not yet cached */ 29981b60d2ecSQu Wenruo upper = btrfs_backref_alloc_node(cache, ref_key->offset, 29991b60d2ecSQu Wenruo cur->level + 1); 30001b60d2ecSQu Wenruo if (!upper) { 30011b60d2ecSQu Wenruo btrfs_backref_free_edge(cache, edge); 30021b60d2ecSQu Wenruo return -ENOMEM; 30031b60d2ecSQu Wenruo } 30041b60d2ecSQu Wenruo 30051b60d2ecSQu Wenruo /* 30061b60d2ecSQu Wenruo * Backrefs for the upper level block isn't cached, add the 30071b60d2ecSQu Wenruo * block to pending list 30081b60d2ecSQu Wenruo */ 30091b60d2ecSQu Wenruo list_add_tail(&edge->list[UPPER], &cache->pending_edge); 30101b60d2ecSQu Wenruo } else { 30111b60d2ecSQu Wenruo /* Parent node already cached */ 30121b60d2ecSQu Wenruo upper = rb_entry(rb_node, struct btrfs_backref_node, rb_node); 30131b60d2ecSQu Wenruo ASSERT(upper->checked); 30141b60d2ecSQu Wenruo INIT_LIST_HEAD(&edge->list[UPPER]); 30151b60d2ecSQu Wenruo } 30161b60d2ecSQu Wenruo btrfs_backref_link_edge(edge, cur, upper, LINK_LOWER); 30171b60d2ecSQu Wenruo return 0; 30181b60d2ecSQu Wenruo } 30191b60d2ecSQu Wenruo 30201b60d2ecSQu Wenruo /* 30211b60d2ecSQu Wenruo * Handle indirect tree backref 30221b60d2ecSQu Wenruo * 30231b60d2ecSQu Wenruo * Indirect tree backref means, we only know which tree the node belongs to. 30241b60d2ecSQu Wenruo * We still need to do a tree search to find out the parents. This is for 30251b60d2ecSQu Wenruo * TREE_BLOCK_REF backref (keyed or inlined). 30261b60d2ecSQu Wenruo * 30271b60d2ecSQu Wenruo * @ref_key: The same as @ref_key in handle_direct_tree_backref() 30281b60d2ecSQu Wenruo * @tree_key: The first key of this tree block. 30291b60d2ecSQu Wenruo * @path: A clean (released) path, to avoid allocating path every time 30301b60d2ecSQu Wenruo * the function get called. 30311b60d2ecSQu Wenruo */ 30321b60d2ecSQu Wenruo static int handle_indirect_tree_backref(struct btrfs_backref_cache *cache, 30331b60d2ecSQu Wenruo struct btrfs_path *path, 30341b60d2ecSQu Wenruo struct btrfs_key *ref_key, 30351b60d2ecSQu Wenruo struct btrfs_key *tree_key, 30361b60d2ecSQu Wenruo struct btrfs_backref_node *cur) 30371b60d2ecSQu Wenruo { 30381b60d2ecSQu Wenruo struct btrfs_fs_info *fs_info = cache->fs_info; 30391b60d2ecSQu Wenruo struct btrfs_backref_node *upper; 30401b60d2ecSQu Wenruo struct btrfs_backref_node *lower; 30411b60d2ecSQu Wenruo struct btrfs_backref_edge *edge; 30421b60d2ecSQu Wenruo struct extent_buffer *eb; 30431b60d2ecSQu Wenruo struct btrfs_root *root; 30441b60d2ecSQu Wenruo struct rb_node *rb_node; 30451b60d2ecSQu Wenruo int level; 30461b60d2ecSQu Wenruo bool need_check = true; 30471b60d2ecSQu Wenruo int ret; 30481b60d2ecSQu Wenruo 304956e9357aSDavid Sterba root = btrfs_get_fs_root(fs_info, ref_key->offset, false); 30501b60d2ecSQu Wenruo if (IS_ERR(root)) 30511b60d2ecSQu Wenruo return PTR_ERR(root); 305292a7cc42SQu Wenruo if (!test_bit(BTRFS_ROOT_SHAREABLE, &root->state)) 30531b60d2ecSQu Wenruo cur->cowonly = 1; 30541b60d2ecSQu Wenruo 30551b60d2ecSQu Wenruo if (btrfs_root_level(&root->root_item) == cur->level) { 30561b60d2ecSQu Wenruo /* Tree root */ 30571b60d2ecSQu Wenruo ASSERT(btrfs_root_bytenr(&root->root_item) == cur->bytenr); 3058876de781SQu Wenruo /* 3059876de781SQu Wenruo * For reloc backref cache, we may ignore reloc root. But for 3060876de781SQu Wenruo * general purpose backref cache, we can't rely on 3061876de781SQu Wenruo * btrfs_should_ignore_reloc_root() as it may conflict with 3062876de781SQu Wenruo * current running relocation and lead to missing root. 3063876de781SQu Wenruo * 3064876de781SQu Wenruo * For general purpose backref cache, reloc root detection is 3065876de781SQu Wenruo * completely relying on direct backref (key->offset is parent 3066876de781SQu Wenruo * bytenr), thus only do such check for reloc cache. 3067876de781SQu Wenruo */ 3068876de781SQu Wenruo if (btrfs_should_ignore_reloc_root(root) && cache->is_reloc) { 30691b60d2ecSQu Wenruo btrfs_put_root(root); 30701b60d2ecSQu Wenruo list_add(&cur->list, &cache->useless_node); 30711b60d2ecSQu Wenruo } else { 30721b60d2ecSQu Wenruo cur->root = root; 30731b60d2ecSQu Wenruo } 30741b60d2ecSQu Wenruo return 0; 30751b60d2ecSQu Wenruo } 30761b60d2ecSQu Wenruo 30771b60d2ecSQu Wenruo level = cur->level + 1; 30781b60d2ecSQu Wenruo 30791b60d2ecSQu Wenruo /* Search the tree to find parent blocks referring to the block */ 30801b60d2ecSQu Wenruo path->search_commit_root = 1; 30811b60d2ecSQu Wenruo path->skip_locking = 1; 30821b60d2ecSQu Wenruo path->lowest_level = level; 30831b60d2ecSQu Wenruo ret = btrfs_search_slot(NULL, root, tree_key, path, 0, 0); 30841b60d2ecSQu Wenruo path->lowest_level = 0; 30851b60d2ecSQu Wenruo if (ret < 0) { 30861b60d2ecSQu Wenruo btrfs_put_root(root); 30871b60d2ecSQu Wenruo return ret; 30881b60d2ecSQu Wenruo } 30891b60d2ecSQu Wenruo if (ret > 0 && path->slots[level] > 0) 30901b60d2ecSQu Wenruo path->slots[level]--; 30911b60d2ecSQu Wenruo 30921b60d2ecSQu Wenruo eb = path->nodes[level]; 30931b60d2ecSQu Wenruo if (btrfs_node_blockptr(eb, path->slots[level]) != cur->bytenr) { 30941b60d2ecSQu Wenruo btrfs_err(fs_info, 30951b60d2ecSQu Wenruo "couldn't find block (%llu) (level %d) in tree (%llu) with key (%llu %u %llu)", 30961b60d2ecSQu Wenruo cur->bytenr, level - 1, root->root_key.objectid, 30971b60d2ecSQu Wenruo tree_key->objectid, tree_key->type, tree_key->offset); 30981b60d2ecSQu Wenruo btrfs_put_root(root); 30991b60d2ecSQu Wenruo ret = -ENOENT; 31001b60d2ecSQu Wenruo goto out; 31011b60d2ecSQu Wenruo } 31021b60d2ecSQu Wenruo lower = cur; 31031b60d2ecSQu Wenruo 31041b60d2ecSQu Wenruo /* Add all nodes and edges in the path */ 31051b60d2ecSQu Wenruo for (; level < BTRFS_MAX_LEVEL; level++) { 31061b60d2ecSQu Wenruo if (!path->nodes[level]) { 31071b60d2ecSQu Wenruo ASSERT(btrfs_root_bytenr(&root->root_item) == 31081b60d2ecSQu Wenruo lower->bytenr); 3109876de781SQu Wenruo /* Same as previous should_ignore_reloc_root() call */ 3110876de781SQu Wenruo if (btrfs_should_ignore_reloc_root(root) && 3111876de781SQu Wenruo cache->is_reloc) { 31121b60d2ecSQu Wenruo btrfs_put_root(root); 31131b60d2ecSQu Wenruo list_add(&lower->list, &cache->useless_node); 31141b60d2ecSQu Wenruo } else { 31151b60d2ecSQu Wenruo lower->root = root; 31161b60d2ecSQu Wenruo } 31171b60d2ecSQu Wenruo break; 31181b60d2ecSQu Wenruo } 31191b60d2ecSQu Wenruo 31201b60d2ecSQu Wenruo edge = btrfs_backref_alloc_edge(cache); 31211b60d2ecSQu Wenruo if (!edge) { 31221b60d2ecSQu Wenruo btrfs_put_root(root); 31231b60d2ecSQu Wenruo ret = -ENOMEM; 31241b60d2ecSQu Wenruo goto out; 31251b60d2ecSQu Wenruo } 31261b60d2ecSQu Wenruo 31271b60d2ecSQu Wenruo eb = path->nodes[level]; 31281b60d2ecSQu Wenruo rb_node = rb_simple_search(&cache->rb_root, eb->start); 31291b60d2ecSQu Wenruo if (!rb_node) { 31301b60d2ecSQu Wenruo upper = btrfs_backref_alloc_node(cache, eb->start, 31311b60d2ecSQu Wenruo lower->level + 1); 31321b60d2ecSQu Wenruo if (!upper) { 31331b60d2ecSQu Wenruo btrfs_put_root(root); 31341b60d2ecSQu Wenruo btrfs_backref_free_edge(cache, edge); 31351b60d2ecSQu Wenruo ret = -ENOMEM; 31361b60d2ecSQu Wenruo goto out; 31371b60d2ecSQu Wenruo } 31381b60d2ecSQu Wenruo upper->owner = btrfs_header_owner(eb); 313992a7cc42SQu Wenruo if (!test_bit(BTRFS_ROOT_SHAREABLE, &root->state)) 31401b60d2ecSQu Wenruo upper->cowonly = 1; 31411b60d2ecSQu Wenruo 31421b60d2ecSQu Wenruo /* 31431b60d2ecSQu Wenruo * If we know the block isn't shared we can avoid 31441b60d2ecSQu Wenruo * checking its backrefs. 31451b60d2ecSQu Wenruo */ 31461b60d2ecSQu Wenruo if (btrfs_block_can_be_shared(root, eb)) 31471b60d2ecSQu Wenruo upper->checked = 0; 31481b60d2ecSQu Wenruo else 31491b60d2ecSQu Wenruo upper->checked = 1; 31501b60d2ecSQu Wenruo 31511b60d2ecSQu Wenruo /* 31521b60d2ecSQu Wenruo * Add the block to pending list if we need to check its 31531b60d2ecSQu Wenruo * backrefs, we only do this once while walking up a 31541b60d2ecSQu Wenruo * tree as we will catch anything else later on. 31551b60d2ecSQu Wenruo */ 31561b60d2ecSQu Wenruo if (!upper->checked && need_check) { 31571b60d2ecSQu Wenruo need_check = false; 31581b60d2ecSQu Wenruo list_add_tail(&edge->list[UPPER], 31591b60d2ecSQu Wenruo &cache->pending_edge); 31601b60d2ecSQu Wenruo } else { 31611b60d2ecSQu Wenruo if (upper->checked) 31621b60d2ecSQu Wenruo need_check = true; 31631b60d2ecSQu Wenruo INIT_LIST_HEAD(&edge->list[UPPER]); 31641b60d2ecSQu Wenruo } 31651b60d2ecSQu Wenruo } else { 31661b60d2ecSQu Wenruo upper = rb_entry(rb_node, struct btrfs_backref_node, 31671b60d2ecSQu Wenruo rb_node); 31681b60d2ecSQu Wenruo ASSERT(upper->checked); 31691b60d2ecSQu Wenruo INIT_LIST_HEAD(&edge->list[UPPER]); 31701b60d2ecSQu Wenruo if (!upper->owner) 31711b60d2ecSQu Wenruo upper->owner = btrfs_header_owner(eb); 31721b60d2ecSQu Wenruo } 31731b60d2ecSQu Wenruo btrfs_backref_link_edge(edge, lower, upper, LINK_LOWER); 31741b60d2ecSQu Wenruo 31751b60d2ecSQu Wenruo if (rb_node) { 31761b60d2ecSQu Wenruo btrfs_put_root(root); 31771b60d2ecSQu Wenruo break; 31781b60d2ecSQu Wenruo } 31791b60d2ecSQu Wenruo lower = upper; 31801b60d2ecSQu Wenruo upper = NULL; 31811b60d2ecSQu Wenruo } 31821b60d2ecSQu Wenruo out: 31831b60d2ecSQu Wenruo btrfs_release_path(path); 31841b60d2ecSQu Wenruo return ret; 31851b60d2ecSQu Wenruo } 31861b60d2ecSQu Wenruo 31871b60d2ecSQu Wenruo /* 31881b60d2ecSQu Wenruo * Add backref node @cur into @cache. 31891b60d2ecSQu Wenruo * 31901b60d2ecSQu Wenruo * NOTE: Even if the function returned 0, @cur is not yet cached as its upper 31911b60d2ecSQu Wenruo * links aren't yet bi-directional. Needs to finish such links. 3192fc997ed0SQu Wenruo * Use btrfs_backref_finish_upper_links() to finish such linkage. 31931b60d2ecSQu Wenruo * 31941b60d2ecSQu Wenruo * @path: Released path for indirect tree backref lookup 31951b60d2ecSQu Wenruo * @iter: Released backref iter for extent tree search 31961b60d2ecSQu Wenruo * @node_key: The first key of the tree block 31971b60d2ecSQu Wenruo */ 31981b60d2ecSQu Wenruo int btrfs_backref_add_tree_node(struct btrfs_backref_cache *cache, 31991b60d2ecSQu Wenruo struct btrfs_path *path, 32001b60d2ecSQu Wenruo struct btrfs_backref_iter *iter, 32011b60d2ecSQu Wenruo struct btrfs_key *node_key, 32021b60d2ecSQu Wenruo struct btrfs_backref_node *cur) 32031b60d2ecSQu Wenruo { 32041b60d2ecSQu Wenruo struct btrfs_fs_info *fs_info = cache->fs_info; 32051b60d2ecSQu Wenruo struct btrfs_backref_edge *edge; 32061b60d2ecSQu Wenruo struct btrfs_backref_node *exist; 32071b60d2ecSQu Wenruo int ret; 32081b60d2ecSQu Wenruo 32091b60d2ecSQu Wenruo ret = btrfs_backref_iter_start(iter, cur->bytenr); 32101b60d2ecSQu Wenruo if (ret < 0) 32111b60d2ecSQu Wenruo return ret; 32121b60d2ecSQu Wenruo /* 32131b60d2ecSQu Wenruo * We skip the first btrfs_tree_block_info, as we don't use the key 32141b60d2ecSQu Wenruo * stored in it, but fetch it from the tree block 32151b60d2ecSQu Wenruo */ 32161b60d2ecSQu Wenruo if (btrfs_backref_has_tree_block_info(iter)) { 32171b60d2ecSQu Wenruo ret = btrfs_backref_iter_next(iter); 32181b60d2ecSQu Wenruo if (ret < 0) 32191b60d2ecSQu Wenruo goto out; 32201b60d2ecSQu Wenruo /* No extra backref? This means the tree block is corrupted */ 32211b60d2ecSQu Wenruo if (ret > 0) { 32221b60d2ecSQu Wenruo ret = -EUCLEAN; 32231b60d2ecSQu Wenruo goto out; 32241b60d2ecSQu Wenruo } 32251b60d2ecSQu Wenruo } 32261b60d2ecSQu Wenruo WARN_ON(cur->checked); 32271b60d2ecSQu Wenruo if (!list_empty(&cur->upper)) { 32281b60d2ecSQu Wenruo /* 32291b60d2ecSQu Wenruo * The backref was added previously when processing backref of 32301b60d2ecSQu Wenruo * type BTRFS_TREE_BLOCK_REF_KEY 32311b60d2ecSQu Wenruo */ 32321b60d2ecSQu Wenruo ASSERT(list_is_singular(&cur->upper)); 32331b60d2ecSQu Wenruo edge = list_entry(cur->upper.next, struct btrfs_backref_edge, 32341b60d2ecSQu Wenruo list[LOWER]); 32351b60d2ecSQu Wenruo ASSERT(list_empty(&edge->list[UPPER])); 32361b60d2ecSQu Wenruo exist = edge->node[UPPER]; 32371b60d2ecSQu Wenruo /* 32381b60d2ecSQu Wenruo * Add the upper level block to pending list if we need check 32391b60d2ecSQu Wenruo * its backrefs 32401b60d2ecSQu Wenruo */ 32411b60d2ecSQu Wenruo if (!exist->checked) 32421b60d2ecSQu Wenruo list_add_tail(&edge->list[UPPER], &cache->pending_edge); 32431b60d2ecSQu Wenruo } else { 32441b60d2ecSQu Wenruo exist = NULL; 32451b60d2ecSQu Wenruo } 32461b60d2ecSQu Wenruo 32471b60d2ecSQu Wenruo for (; ret == 0; ret = btrfs_backref_iter_next(iter)) { 32481b60d2ecSQu Wenruo struct extent_buffer *eb; 32491b60d2ecSQu Wenruo struct btrfs_key key; 32501b60d2ecSQu Wenruo int type; 32511b60d2ecSQu Wenruo 32521b60d2ecSQu Wenruo cond_resched(); 32531b60d2ecSQu Wenruo eb = btrfs_backref_get_eb(iter); 32541b60d2ecSQu Wenruo 32551b60d2ecSQu Wenruo key.objectid = iter->bytenr; 32561b60d2ecSQu Wenruo if (btrfs_backref_iter_is_inline_ref(iter)) { 32571b60d2ecSQu Wenruo struct btrfs_extent_inline_ref *iref; 32581b60d2ecSQu Wenruo 32591b60d2ecSQu Wenruo /* Update key for inline backref */ 32601b60d2ecSQu Wenruo iref = (struct btrfs_extent_inline_ref *) 32611b60d2ecSQu Wenruo ((unsigned long)iter->cur_ptr); 32621b60d2ecSQu Wenruo type = btrfs_get_extent_inline_ref_type(eb, iref, 32631b60d2ecSQu Wenruo BTRFS_REF_TYPE_BLOCK); 32641b60d2ecSQu Wenruo if (type == BTRFS_REF_TYPE_INVALID) { 32651b60d2ecSQu Wenruo ret = -EUCLEAN; 32661b60d2ecSQu Wenruo goto out; 32671b60d2ecSQu Wenruo } 32681b60d2ecSQu Wenruo key.type = type; 32691b60d2ecSQu Wenruo key.offset = btrfs_extent_inline_ref_offset(eb, iref); 32701b60d2ecSQu Wenruo } else { 32711b60d2ecSQu Wenruo key.type = iter->cur_key.type; 32721b60d2ecSQu Wenruo key.offset = iter->cur_key.offset; 32731b60d2ecSQu Wenruo } 32741b60d2ecSQu Wenruo 32751b60d2ecSQu Wenruo /* 32761b60d2ecSQu Wenruo * Parent node found and matches current inline ref, no need to 32771b60d2ecSQu Wenruo * rebuild this node for this inline ref 32781b60d2ecSQu Wenruo */ 32791b60d2ecSQu Wenruo if (exist && 32801b60d2ecSQu Wenruo ((key.type == BTRFS_TREE_BLOCK_REF_KEY && 32811b60d2ecSQu Wenruo exist->owner == key.offset) || 32821b60d2ecSQu Wenruo (key.type == BTRFS_SHARED_BLOCK_REF_KEY && 32831b60d2ecSQu Wenruo exist->bytenr == key.offset))) { 32841b60d2ecSQu Wenruo exist = NULL; 32851b60d2ecSQu Wenruo continue; 32861b60d2ecSQu Wenruo } 32871b60d2ecSQu Wenruo 32881b60d2ecSQu Wenruo /* SHARED_BLOCK_REF means key.offset is the parent bytenr */ 32891b60d2ecSQu Wenruo if (key.type == BTRFS_SHARED_BLOCK_REF_KEY) { 32901b60d2ecSQu Wenruo ret = handle_direct_tree_backref(cache, &key, cur); 32911b60d2ecSQu Wenruo if (ret < 0) 32921b60d2ecSQu Wenruo goto out; 32931b60d2ecSQu Wenruo continue; 32941b60d2ecSQu Wenruo } else if (unlikely(key.type == BTRFS_EXTENT_REF_V0_KEY)) { 32951b60d2ecSQu Wenruo ret = -EINVAL; 32961b60d2ecSQu Wenruo btrfs_print_v0_err(fs_info); 32971b60d2ecSQu Wenruo btrfs_handle_fs_error(fs_info, ret, NULL); 32981b60d2ecSQu Wenruo goto out; 32991b60d2ecSQu Wenruo } else if (key.type != BTRFS_TREE_BLOCK_REF_KEY) { 33001b60d2ecSQu Wenruo continue; 33011b60d2ecSQu Wenruo } 33021b60d2ecSQu Wenruo 33031b60d2ecSQu Wenruo /* 33041b60d2ecSQu Wenruo * key.type == BTRFS_TREE_BLOCK_REF_KEY, inline ref offset 33051b60d2ecSQu Wenruo * means the root objectid. We need to search the tree to get 33061b60d2ecSQu Wenruo * its parent bytenr. 33071b60d2ecSQu Wenruo */ 33081b60d2ecSQu Wenruo ret = handle_indirect_tree_backref(cache, path, &key, node_key, 33091b60d2ecSQu Wenruo cur); 33101b60d2ecSQu Wenruo if (ret < 0) 33111b60d2ecSQu Wenruo goto out; 33121b60d2ecSQu Wenruo } 33131b60d2ecSQu Wenruo ret = 0; 33141b60d2ecSQu Wenruo cur->checked = 1; 33151b60d2ecSQu Wenruo WARN_ON(exist); 33161b60d2ecSQu Wenruo out: 33171b60d2ecSQu Wenruo btrfs_backref_iter_release(iter); 33181b60d2ecSQu Wenruo return ret; 33191b60d2ecSQu Wenruo } 3320fc997ed0SQu Wenruo 3321fc997ed0SQu Wenruo /* 3322fc997ed0SQu Wenruo * Finish the upwards linkage created by btrfs_backref_add_tree_node() 3323fc997ed0SQu Wenruo */ 3324fc997ed0SQu Wenruo int btrfs_backref_finish_upper_links(struct btrfs_backref_cache *cache, 3325fc997ed0SQu Wenruo struct btrfs_backref_node *start) 3326fc997ed0SQu Wenruo { 3327fc997ed0SQu Wenruo struct list_head *useless_node = &cache->useless_node; 3328fc997ed0SQu Wenruo struct btrfs_backref_edge *edge; 3329fc997ed0SQu Wenruo struct rb_node *rb_node; 3330fc997ed0SQu Wenruo LIST_HEAD(pending_edge); 3331fc997ed0SQu Wenruo 3332fc997ed0SQu Wenruo ASSERT(start->checked); 3333fc997ed0SQu Wenruo 3334fc997ed0SQu Wenruo /* Insert this node to cache if it's not COW-only */ 3335fc997ed0SQu Wenruo if (!start->cowonly) { 3336fc997ed0SQu Wenruo rb_node = rb_simple_insert(&cache->rb_root, start->bytenr, 3337fc997ed0SQu Wenruo &start->rb_node); 3338fc997ed0SQu Wenruo if (rb_node) 3339fc997ed0SQu Wenruo btrfs_backref_panic(cache->fs_info, start->bytenr, 3340fc997ed0SQu Wenruo -EEXIST); 3341fc997ed0SQu Wenruo list_add_tail(&start->lower, &cache->leaves); 3342fc997ed0SQu Wenruo } 3343fc997ed0SQu Wenruo 3344fc997ed0SQu Wenruo /* 3345fc997ed0SQu Wenruo * Use breadth first search to iterate all related edges. 3346fc997ed0SQu Wenruo * 3347fc997ed0SQu Wenruo * The starting points are all the edges of this node 3348fc997ed0SQu Wenruo */ 3349fc997ed0SQu Wenruo list_for_each_entry(edge, &start->upper, list[LOWER]) 3350fc997ed0SQu Wenruo list_add_tail(&edge->list[UPPER], &pending_edge); 3351fc997ed0SQu Wenruo 3352fc997ed0SQu Wenruo while (!list_empty(&pending_edge)) { 3353fc997ed0SQu Wenruo struct btrfs_backref_node *upper; 3354fc997ed0SQu Wenruo struct btrfs_backref_node *lower; 3355fc997ed0SQu Wenruo 3356fc997ed0SQu Wenruo edge = list_first_entry(&pending_edge, 3357fc997ed0SQu Wenruo struct btrfs_backref_edge, list[UPPER]); 3358fc997ed0SQu Wenruo list_del_init(&edge->list[UPPER]); 3359fc997ed0SQu Wenruo upper = edge->node[UPPER]; 3360fc997ed0SQu Wenruo lower = edge->node[LOWER]; 3361fc997ed0SQu Wenruo 3362fc997ed0SQu Wenruo /* Parent is detached, no need to keep any edges */ 3363fc997ed0SQu Wenruo if (upper->detached) { 3364fc997ed0SQu Wenruo list_del(&edge->list[LOWER]); 3365fc997ed0SQu Wenruo btrfs_backref_free_edge(cache, edge); 3366fc997ed0SQu Wenruo 3367fc997ed0SQu Wenruo /* Lower node is orphan, queue for cleanup */ 3368fc997ed0SQu Wenruo if (list_empty(&lower->upper)) 3369fc997ed0SQu Wenruo list_add(&lower->list, useless_node); 3370fc997ed0SQu Wenruo continue; 3371fc997ed0SQu Wenruo } 3372fc997ed0SQu Wenruo 3373fc997ed0SQu Wenruo /* 3374fc997ed0SQu Wenruo * All new nodes added in current build_backref_tree() haven't 3375fc997ed0SQu Wenruo * been linked to the cache rb tree. 3376fc997ed0SQu Wenruo * So if we have upper->rb_node populated, this means a cache 3377fc997ed0SQu Wenruo * hit. We only need to link the edge, as @upper and all its 3378fc997ed0SQu Wenruo * parents have already been linked. 3379fc997ed0SQu Wenruo */ 3380fc997ed0SQu Wenruo if (!RB_EMPTY_NODE(&upper->rb_node)) { 3381fc997ed0SQu Wenruo if (upper->lowest) { 3382fc997ed0SQu Wenruo list_del_init(&upper->lower); 3383fc997ed0SQu Wenruo upper->lowest = 0; 3384fc997ed0SQu Wenruo } 3385fc997ed0SQu Wenruo 3386fc997ed0SQu Wenruo list_add_tail(&edge->list[UPPER], &upper->lower); 3387fc997ed0SQu Wenruo continue; 3388fc997ed0SQu Wenruo } 3389fc997ed0SQu Wenruo 3390fc997ed0SQu Wenruo /* Sanity check, we shouldn't have any unchecked nodes */ 3391fc997ed0SQu Wenruo if (!upper->checked) { 3392fc997ed0SQu Wenruo ASSERT(0); 3393fc997ed0SQu Wenruo return -EUCLEAN; 3394fc997ed0SQu Wenruo } 3395fc997ed0SQu Wenruo 3396fc997ed0SQu Wenruo /* Sanity check, COW-only node has non-COW-only parent */ 3397fc997ed0SQu Wenruo if (start->cowonly != upper->cowonly) { 3398fc997ed0SQu Wenruo ASSERT(0); 3399fc997ed0SQu Wenruo return -EUCLEAN; 3400fc997ed0SQu Wenruo } 3401fc997ed0SQu Wenruo 3402fc997ed0SQu Wenruo /* Only cache non-COW-only (subvolume trees) tree blocks */ 3403fc997ed0SQu Wenruo if (!upper->cowonly) { 3404fc997ed0SQu Wenruo rb_node = rb_simple_insert(&cache->rb_root, upper->bytenr, 3405fc997ed0SQu Wenruo &upper->rb_node); 3406fc997ed0SQu Wenruo if (rb_node) { 3407fc997ed0SQu Wenruo btrfs_backref_panic(cache->fs_info, 3408fc997ed0SQu Wenruo upper->bytenr, -EEXIST); 3409fc997ed0SQu Wenruo return -EUCLEAN; 3410fc997ed0SQu Wenruo } 3411fc997ed0SQu Wenruo } 3412fc997ed0SQu Wenruo 3413fc997ed0SQu Wenruo list_add_tail(&edge->list[UPPER], &upper->lower); 3414fc997ed0SQu Wenruo 3415fc997ed0SQu Wenruo /* 3416fc997ed0SQu Wenruo * Also queue all the parent edges of this uncached node 3417fc997ed0SQu Wenruo * to finish the upper linkage 3418fc997ed0SQu Wenruo */ 3419fc997ed0SQu Wenruo list_for_each_entry(edge, &upper->upper, list[LOWER]) 3420fc997ed0SQu Wenruo list_add_tail(&edge->list[UPPER], &pending_edge); 3421fc997ed0SQu Wenruo } 3422fc997ed0SQu Wenruo return 0; 3423fc997ed0SQu Wenruo } 34241b23ea18SQu Wenruo 34251b23ea18SQu Wenruo void btrfs_backref_error_cleanup(struct btrfs_backref_cache *cache, 34261b23ea18SQu Wenruo struct btrfs_backref_node *node) 34271b23ea18SQu Wenruo { 34281b23ea18SQu Wenruo struct btrfs_backref_node *lower; 34291b23ea18SQu Wenruo struct btrfs_backref_node *upper; 34301b23ea18SQu Wenruo struct btrfs_backref_edge *edge; 34311b23ea18SQu Wenruo 34321b23ea18SQu Wenruo while (!list_empty(&cache->useless_node)) { 34331b23ea18SQu Wenruo lower = list_first_entry(&cache->useless_node, 34341b23ea18SQu Wenruo struct btrfs_backref_node, list); 34351b23ea18SQu Wenruo list_del_init(&lower->list); 34361b23ea18SQu Wenruo } 34371b23ea18SQu Wenruo while (!list_empty(&cache->pending_edge)) { 34381b23ea18SQu Wenruo edge = list_first_entry(&cache->pending_edge, 34391b23ea18SQu Wenruo struct btrfs_backref_edge, list[UPPER]); 34401b23ea18SQu Wenruo list_del(&edge->list[UPPER]); 34411b23ea18SQu Wenruo list_del(&edge->list[LOWER]); 34421b23ea18SQu Wenruo lower = edge->node[LOWER]; 34431b23ea18SQu Wenruo upper = edge->node[UPPER]; 34441b23ea18SQu Wenruo btrfs_backref_free_edge(cache, edge); 34451b23ea18SQu Wenruo 34461b23ea18SQu Wenruo /* 34471b23ea18SQu Wenruo * Lower is no longer linked to any upper backref nodes and 34481b23ea18SQu Wenruo * isn't in the cache, we can free it ourselves. 34491b23ea18SQu Wenruo */ 34501b23ea18SQu Wenruo if (list_empty(&lower->upper) && 34511b23ea18SQu Wenruo RB_EMPTY_NODE(&lower->rb_node)) 34521b23ea18SQu Wenruo list_add(&lower->list, &cache->useless_node); 34531b23ea18SQu Wenruo 34541b23ea18SQu Wenruo if (!RB_EMPTY_NODE(&upper->rb_node)) 34551b23ea18SQu Wenruo continue; 34561b23ea18SQu Wenruo 34571b23ea18SQu Wenruo /* Add this guy's upper edges to the list to process */ 34581b23ea18SQu Wenruo list_for_each_entry(edge, &upper->upper, list[LOWER]) 34591b23ea18SQu Wenruo list_add_tail(&edge->list[UPPER], 34601b23ea18SQu Wenruo &cache->pending_edge); 34611b23ea18SQu Wenruo if (list_empty(&upper->upper)) 34621b23ea18SQu Wenruo list_add(&upper->list, &cache->useless_node); 34631b23ea18SQu Wenruo } 34641b23ea18SQu Wenruo 34651b23ea18SQu Wenruo while (!list_empty(&cache->useless_node)) { 34661b23ea18SQu Wenruo lower = list_first_entry(&cache->useless_node, 34671b23ea18SQu Wenruo struct btrfs_backref_node, list); 34681b23ea18SQu Wenruo list_del_init(&lower->list); 34691b23ea18SQu Wenruo if (lower == node) 34701b23ea18SQu Wenruo node = NULL; 347149ecc679SJosef Bacik btrfs_backref_drop_node(cache, lower); 34721b23ea18SQu Wenruo } 34731b23ea18SQu Wenruo 34741b23ea18SQu Wenruo btrfs_backref_cleanup_node(cache, node); 34751b23ea18SQu Wenruo ASSERT(list_empty(&cache->useless_node) && 34761b23ea18SQu Wenruo list_empty(&cache->pending_edge)); 34771b23ea18SQu Wenruo } 3478