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" 2227137facSChristoph Hellwig #include "tree-checker.h" 23a542ad1bSJan Schmidt 24877c1476SFilipe Manana /* Just arbitrary numbers so we can be sure one of these happened. */ 25dc046b10SJosef Bacik #define BACKREF_FOUND_SHARED 6 26877c1476SFilipe Manana #define BACKREF_FOUND_NOT_SHARED 7 27dc046b10SJosef Bacik 28976b1908SJan Schmidt struct extent_inode_elem { 29976b1908SJan Schmidt u64 inum; 30976b1908SJan Schmidt u64 offset; 31c7499a64SFilipe Manana u64 num_bytes; 32976b1908SJan Schmidt struct extent_inode_elem *next; 33976b1908SJan Schmidt }; 34976b1908SJan Schmidt 3588ffb665SFilipe Manana static int check_extent_in_eb(struct btrfs_backref_walk_ctx *ctx, 3688ffb665SFilipe Manana const struct btrfs_key *key, 3773980becSJeff Mahoney const struct extent_buffer *eb, 3873980becSJeff Mahoney const struct btrfs_file_extent_item *fi, 396ce6ba53SFilipe Manana struct extent_inode_elem **eie) 40976b1908SJan Schmidt { 41c7499a64SFilipe Manana const u64 data_len = btrfs_file_extent_num_bytes(eb, fi); 4288ffb665SFilipe Manana u64 offset = key->offset; 438ca15e05SJosef Bacik struct extent_inode_elem *e; 4488ffb665SFilipe Manana const u64 *root_ids; 4588ffb665SFilipe Manana int root_count; 4688ffb665SFilipe Manana bool cached; 478ca15e05SJosef Bacik 480cad8f14SFilipe Manana if (!ctx->ignore_extent_item_pos && 490cad8f14SFilipe Manana !btrfs_file_extent_compression(eb, fi) && 508ca15e05SJosef Bacik !btrfs_file_extent_encryption(eb, fi) && 518ca15e05SJosef Bacik !btrfs_file_extent_other_encoding(eb, fi)) { 52976b1908SJan Schmidt u64 data_offset; 53976b1908SJan Schmidt 54976b1908SJan Schmidt data_offset = btrfs_file_extent_offset(eb, fi); 55976b1908SJan Schmidt 5688ffb665SFilipe Manana if (ctx->extent_item_pos < data_offset || 5788ffb665SFilipe Manana ctx->extent_item_pos >= data_offset + data_len) 58976b1908SJan Schmidt return 1; 5988ffb665SFilipe Manana offset += ctx->extent_item_pos - data_offset; 608ca15e05SJosef Bacik } 61976b1908SJan Schmidt 6288ffb665SFilipe Manana if (!ctx->indirect_ref_iterator || !ctx->cache_lookup) 6388ffb665SFilipe Manana goto add_inode_elem; 6488ffb665SFilipe Manana 6588ffb665SFilipe Manana cached = ctx->cache_lookup(eb->start, ctx->user_ctx, &root_ids, 6688ffb665SFilipe Manana &root_count); 6788ffb665SFilipe Manana if (!cached) 6888ffb665SFilipe Manana goto add_inode_elem; 6988ffb665SFilipe Manana 7088ffb665SFilipe Manana for (int i = 0; i < root_count; i++) { 7188ffb665SFilipe Manana int ret; 7288ffb665SFilipe Manana 7388ffb665SFilipe Manana ret = ctx->indirect_ref_iterator(key->objectid, offset, 7488ffb665SFilipe Manana data_len, root_ids[i], 7588ffb665SFilipe Manana ctx->user_ctx); 7688ffb665SFilipe Manana if (ret) 7788ffb665SFilipe Manana return ret; 7888ffb665SFilipe Manana } 7988ffb665SFilipe Manana 8088ffb665SFilipe Manana add_inode_elem: 81976b1908SJan Schmidt e = kmalloc(sizeof(*e), GFP_NOFS); 82976b1908SJan Schmidt if (!e) 83976b1908SJan Schmidt return -ENOMEM; 84976b1908SJan Schmidt 85976b1908SJan Schmidt e->next = *eie; 86976b1908SJan Schmidt e->inum = key->objectid; 8788ffb665SFilipe Manana e->offset = offset; 88c7499a64SFilipe Manana e->num_bytes = data_len; 89976b1908SJan Schmidt *eie = e; 90976b1908SJan Schmidt 91976b1908SJan Schmidt return 0; 92976b1908SJan Schmidt } 93976b1908SJan Schmidt 94f05c4746SWang Shilong static void free_inode_elem_list(struct extent_inode_elem *eie) 95f05c4746SWang Shilong { 96f05c4746SWang Shilong struct extent_inode_elem *eie_next; 97f05c4746SWang Shilong 98f05c4746SWang Shilong for (; eie; eie = eie_next) { 99f05c4746SWang Shilong eie_next = eie->next; 100f05c4746SWang Shilong kfree(eie); 101f05c4746SWang Shilong } 102f05c4746SWang Shilong } 103f05c4746SWang Shilong 10488ffb665SFilipe Manana static int find_extent_in_eb(struct btrfs_backref_walk_ctx *ctx, 10588ffb665SFilipe Manana const struct extent_buffer *eb, 1066ce6ba53SFilipe Manana struct extent_inode_elem **eie) 107976b1908SJan Schmidt { 108976b1908SJan Schmidt u64 disk_byte; 109976b1908SJan Schmidt struct btrfs_key key; 110976b1908SJan Schmidt struct btrfs_file_extent_item *fi; 111976b1908SJan Schmidt int slot; 112976b1908SJan Schmidt int nritems; 113976b1908SJan Schmidt int extent_type; 114976b1908SJan Schmidt int ret; 115976b1908SJan Schmidt 116976b1908SJan Schmidt /* 117976b1908SJan Schmidt * from the shared data ref, we only have the leaf but we need 118976b1908SJan Schmidt * the key. thus, we must look into all items and see that we 119976b1908SJan Schmidt * find one (some) with a reference to our extent item. 120976b1908SJan Schmidt */ 121976b1908SJan Schmidt nritems = btrfs_header_nritems(eb); 122976b1908SJan Schmidt for (slot = 0; slot < nritems; ++slot) { 123976b1908SJan Schmidt btrfs_item_key_to_cpu(eb, &key, slot); 124976b1908SJan Schmidt if (key.type != BTRFS_EXTENT_DATA_KEY) 125976b1908SJan Schmidt continue; 126976b1908SJan Schmidt fi = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item); 127976b1908SJan Schmidt extent_type = btrfs_file_extent_type(eb, fi); 128976b1908SJan Schmidt if (extent_type == BTRFS_FILE_EXTENT_INLINE) 129976b1908SJan Schmidt continue; 130976b1908SJan Schmidt /* don't skip BTRFS_FILE_EXTENT_PREALLOC, we can handle that */ 131976b1908SJan Schmidt disk_byte = btrfs_file_extent_disk_bytenr(eb, fi); 13288ffb665SFilipe Manana if (disk_byte != ctx->bytenr) 133976b1908SJan Schmidt continue; 134976b1908SJan Schmidt 13588ffb665SFilipe Manana ret = check_extent_in_eb(ctx, &key, eb, fi, eie); 13688ffb665SFilipe Manana if (ret == BTRFS_ITERATE_EXTENT_INODES_STOP || ret < 0) 137976b1908SJan Schmidt return ret; 138976b1908SJan Schmidt } 139976b1908SJan Schmidt 140976b1908SJan Schmidt return 0; 141976b1908SJan Schmidt } 142976b1908SJan Schmidt 14386d5f994SEdmund Nadolski struct preftree { 144ecf160b4SLiu Bo struct rb_root_cached root; 1456c336b21SJeff Mahoney unsigned int count; 14686d5f994SEdmund Nadolski }; 14786d5f994SEdmund Nadolski 148ecf160b4SLiu Bo #define PREFTREE_INIT { .root = RB_ROOT_CACHED, .count = 0 } 14986d5f994SEdmund Nadolski 15086d5f994SEdmund Nadolski struct preftrees { 15186d5f994SEdmund Nadolski struct preftree direct; /* BTRFS_SHARED_[DATA|BLOCK]_REF_KEY */ 15286d5f994SEdmund Nadolski struct preftree indirect; /* BTRFS_[TREE_BLOCK|EXTENT_DATA]_REF_KEY */ 15386d5f994SEdmund Nadolski struct preftree indirect_missing_keys; 15486d5f994SEdmund Nadolski }; 15586d5f994SEdmund Nadolski 1563ec4d323SEdmund Nadolski /* 1573ec4d323SEdmund Nadolski * Checks for a shared extent during backref search. 1583ec4d323SEdmund Nadolski * 1593ec4d323SEdmund Nadolski * The share_count tracks prelim_refs (direct and indirect) having a 1603ec4d323SEdmund Nadolski * ref->count >0: 1613ec4d323SEdmund Nadolski * - incremented when a ref->count transitions to >0 1623ec4d323SEdmund Nadolski * - decremented when a ref->count transitions to <1 1633ec4d323SEdmund Nadolski */ 1643ec4d323SEdmund Nadolski struct share_check { 165877c1476SFilipe Manana struct btrfs_backref_share_check_ctx *ctx; 166877c1476SFilipe Manana struct btrfs_root *root; 1673ec4d323SEdmund Nadolski u64 inum; 16873e339e6SFilipe Manana u64 data_bytenr; 1696976201fSFilipe Manana u64 data_extent_gen; 17073e339e6SFilipe Manana /* 17173e339e6SFilipe Manana * Counts number of inodes that refer to an extent (different inodes in 17273e339e6SFilipe Manana * the same root or different roots) that we could find. The sharedness 17373e339e6SFilipe Manana * check typically stops once this counter gets greater than 1, so it 17473e339e6SFilipe Manana * may not reflect the total number of inodes. 17573e339e6SFilipe Manana */ 1763ec4d323SEdmund Nadolski int share_count; 17773e339e6SFilipe Manana /* 17873e339e6SFilipe Manana * The number of times we found our inode refers to the data extent we 17973e339e6SFilipe Manana * are determining the sharedness. In other words, how many file extent 18073e339e6SFilipe Manana * items we could find for our inode that point to our target data 18173e339e6SFilipe Manana * extent. The value we get here after finishing the extent sharedness 18273e339e6SFilipe Manana * check may be smaller than reality, but if it ends up being greater 18373e339e6SFilipe Manana * than 1, then we know for sure the inode has multiple file extent 18473e339e6SFilipe Manana * items that point to our inode, and we can safely assume it's useful 18573e339e6SFilipe Manana * to cache the sharedness check result. 18673e339e6SFilipe Manana */ 18773e339e6SFilipe Manana int self_ref_count; 1884fc7b572SFilipe Manana bool have_delayed_delete_refs; 1893ec4d323SEdmund Nadolski }; 1903ec4d323SEdmund Nadolski 1913ec4d323SEdmund Nadolski static inline int extent_is_shared(struct share_check *sc) 1923ec4d323SEdmund Nadolski { 1933ec4d323SEdmund Nadolski return (sc && sc->share_count > 1) ? BACKREF_FOUND_SHARED : 0; 1943ec4d323SEdmund Nadolski } 1953ec4d323SEdmund Nadolski 196b9e9a6cbSWang Shilong static struct kmem_cache *btrfs_prelim_ref_cache; 197b9e9a6cbSWang Shilong 198b9e9a6cbSWang Shilong int __init btrfs_prelim_ref_init(void) 199b9e9a6cbSWang Shilong { 200b9e9a6cbSWang Shilong btrfs_prelim_ref_cache = kmem_cache_create("btrfs_prelim_ref", 201e0c476b1SJeff Mahoney sizeof(struct prelim_ref), 202b9e9a6cbSWang Shilong 0, 203fba4b697SNikolay Borisov SLAB_MEM_SPREAD, 204b9e9a6cbSWang Shilong NULL); 205b9e9a6cbSWang Shilong if (!btrfs_prelim_ref_cache) 206b9e9a6cbSWang Shilong return -ENOMEM; 207b9e9a6cbSWang Shilong return 0; 208b9e9a6cbSWang Shilong } 209b9e9a6cbSWang Shilong 210e67c718bSDavid Sterba void __cold btrfs_prelim_ref_exit(void) 211b9e9a6cbSWang Shilong { 212b9e9a6cbSWang Shilong kmem_cache_destroy(btrfs_prelim_ref_cache); 213b9e9a6cbSWang Shilong } 214b9e9a6cbSWang Shilong 21586d5f994SEdmund Nadolski static void free_pref(struct prelim_ref *ref) 21686d5f994SEdmund Nadolski { 21786d5f994SEdmund Nadolski kmem_cache_free(btrfs_prelim_ref_cache, ref); 21886d5f994SEdmund Nadolski } 21986d5f994SEdmund Nadolski 22086d5f994SEdmund Nadolski /* 22186d5f994SEdmund Nadolski * Return 0 when both refs are for the same block (and can be merged). 22286d5f994SEdmund Nadolski * A -1 return indicates ref1 is a 'lower' block than ref2, while 1 22386d5f994SEdmund Nadolski * indicates a 'higher' block. 22486d5f994SEdmund Nadolski */ 22586d5f994SEdmund Nadolski static int prelim_ref_compare(struct prelim_ref *ref1, 22686d5f994SEdmund Nadolski struct prelim_ref *ref2) 22786d5f994SEdmund Nadolski { 22886d5f994SEdmund Nadolski if (ref1->level < ref2->level) 22986d5f994SEdmund Nadolski return -1; 23086d5f994SEdmund Nadolski if (ref1->level > ref2->level) 23186d5f994SEdmund Nadolski return 1; 23286d5f994SEdmund Nadolski if (ref1->root_id < ref2->root_id) 23386d5f994SEdmund Nadolski return -1; 23486d5f994SEdmund Nadolski if (ref1->root_id > ref2->root_id) 23586d5f994SEdmund Nadolski return 1; 23686d5f994SEdmund Nadolski if (ref1->key_for_search.type < ref2->key_for_search.type) 23786d5f994SEdmund Nadolski return -1; 23886d5f994SEdmund Nadolski if (ref1->key_for_search.type > ref2->key_for_search.type) 23986d5f994SEdmund Nadolski return 1; 24086d5f994SEdmund Nadolski if (ref1->key_for_search.objectid < ref2->key_for_search.objectid) 24186d5f994SEdmund Nadolski return -1; 24286d5f994SEdmund Nadolski if (ref1->key_for_search.objectid > ref2->key_for_search.objectid) 24386d5f994SEdmund Nadolski return 1; 24486d5f994SEdmund Nadolski if (ref1->key_for_search.offset < ref2->key_for_search.offset) 24586d5f994SEdmund Nadolski return -1; 24686d5f994SEdmund Nadolski if (ref1->key_for_search.offset > ref2->key_for_search.offset) 24786d5f994SEdmund Nadolski return 1; 24886d5f994SEdmund Nadolski if (ref1->parent < ref2->parent) 24986d5f994SEdmund Nadolski return -1; 25086d5f994SEdmund Nadolski if (ref1->parent > ref2->parent) 25186d5f994SEdmund Nadolski return 1; 25286d5f994SEdmund Nadolski 25386d5f994SEdmund Nadolski return 0; 25486d5f994SEdmund Nadolski } 25586d5f994SEdmund Nadolski 256ccc8dc75SColin Ian King static void update_share_count(struct share_check *sc, int oldcount, 25773e339e6SFilipe Manana int newcount, struct prelim_ref *newref) 2583ec4d323SEdmund Nadolski { 2593ec4d323SEdmund Nadolski if ((!sc) || (oldcount == 0 && newcount < 1)) 2603ec4d323SEdmund Nadolski return; 2613ec4d323SEdmund Nadolski 2623ec4d323SEdmund Nadolski if (oldcount > 0 && newcount < 1) 2633ec4d323SEdmund Nadolski sc->share_count--; 2643ec4d323SEdmund Nadolski else if (oldcount < 1 && newcount > 0) 2653ec4d323SEdmund Nadolski sc->share_count++; 26673e339e6SFilipe Manana 267877c1476SFilipe Manana if (newref->root_id == sc->root->root_key.objectid && 26873e339e6SFilipe Manana newref->wanted_disk_byte == sc->data_bytenr && 26973e339e6SFilipe Manana newref->key_for_search.objectid == sc->inum) 27073e339e6SFilipe Manana sc->self_ref_count += newref->count; 2713ec4d323SEdmund Nadolski } 2723ec4d323SEdmund Nadolski 27386d5f994SEdmund Nadolski /* 27486d5f994SEdmund Nadolski * Add @newref to the @root rbtree, merging identical refs. 27586d5f994SEdmund Nadolski * 2763ec4d323SEdmund Nadolski * Callers should assume that newref has been freed after calling. 27786d5f994SEdmund Nadolski */ 27800142756SJeff Mahoney static void prelim_ref_insert(const struct btrfs_fs_info *fs_info, 27900142756SJeff Mahoney struct preftree *preftree, 2803ec4d323SEdmund Nadolski struct prelim_ref *newref, 2813ec4d323SEdmund Nadolski struct share_check *sc) 28286d5f994SEdmund Nadolski { 283ecf160b4SLiu Bo struct rb_root_cached *root; 28486d5f994SEdmund Nadolski struct rb_node **p; 28586d5f994SEdmund Nadolski struct rb_node *parent = NULL; 28686d5f994SEdmund Nadolski struct prelim_ref *ref; 28786d5f994SEdmund Nadolski int result; 288ecf160b4SLiu Bo bool leftmost = true; 28986d5f994SEdmund Nadolski 29086d5f994SEdmund Nadolski root = &preftree->root; 291ecf160b4SLiu Bo p = &root->rb_root.rb_node; 29286d5f994SEdmund Nadolski 29386d5f994SEdmund Nadolski while (*p) { 29486d5f994SEdmund Nadolski parent = *p; 29586d5f994SEdmund Nadolski ref = rb_entry(parent, struct prelim_ref, rbnode); 29686d5f994SEdmund Nadolski result = prelim_ref_compare(ref, newref); 29786d5f994SEdmund Nadolski if (result < 0) { 29886d5f994SEdmund Nadolski p = &(*p)->rb_left; 29986d5f994SEdmund Nadolski } else if (result > 0) { 30086d5f994SEdmund Nadolski p = &(*p)->rb_right; 301ecf160b4SLiu Bo leftmost = false; 30286d5f994SEdmund Nadolski } else { 30386d5f994SEdmund Nadolski /* Identical refs, merge them and free @newref */ 30486d5f994SEdmund Nadolski struct extent_inode_elem *eie = ref->inode_list; 30586d5f994SEdmund Nadolski 30686d5f994SEdmund Nadolski while (eie && eie->next) 30786d5f994SEdmund Nadolski eie = eie->next; 30886d5f994SEdmund Nadolski 30986d5f994SEdmund Nadolski if (!eie) 31086d5f994SEdmund Nadolski ref->inode_list = newref->inode_list; 31186d5f994SEdmund Nadolski else 31286d5f994SEdmund Nadolski eie->next = newref->inode_list; 31300142756SJeff Mahoney trace_btrfs_prelim_ref_merge(fs_info, ref, newref, 31400142756SJeff Mahoney preftree->count); 3153ec4d323SEdmund Nadolski /* 3163ec4d323SEdmund Nadolski * A delayed ref can have newref->count < 0. 3173ec4d323SEdmund Nadolski * The ref->count is updated to follow any 3183ec4d323SEdmund Nadolski * BTRFS_[ADD|DROP]_DELAYED_REF actions. 3193ec4d323SEdmund Nadolski */ 3203ec4d323SEdmund Nadolski update_share_count(sc, ref->count, 32173e339e6SFilipe Manana ref->count + newref->count, newref); 32286d5f994SEdmund Nadolski ref->count += newref->count; 32386d5f994SEdmund Nadolski free_pref(newref); 32486d5f994SEdmund Nadolski return; 32586d5f994SEdmund Nadolski } 32686d5f994SEdmund Nadolski } 32786d5f994SEdmund Nadolski 32873e339e6SFilipe Manana update_share_count(sc, 0, newref->count, newref); 3296c336b21SJeff Mahoney preftree->count++; 33000142756SJeff Mahoney trace_btrfs_prelim_ref_insert(fs_info, newref, NULL, preftree->count); 33186d5f994SEdmund Nadolski rb_link_node(&newref->rbnode, parent, p); 332ecf160b4SLiu Bo rb_insert_color_cached(&newref->rbnode, root, leftmost); 33386d5f994SEdmund Nadolski } 33486d5f994SEdmund Nadolski 33586d5f994SEdmund Nadolski /* 33686d5f994SEdmund Nadolski * Release the entire tree. We don't care about internal consistency so 33786d5f994SEdmund Nadolski * just free everything and then reset the tree root. 33886d5f994SEdmund Nadolski */ 33986d5f994SEdmund Nadolski static void prelim_release(struct preftree *preftree) 34086d5f994SEdmund Nadolski { 34186d5f994SEdmund Nadolski struct prelim_ref *ref, *next_ref; 34286d5f994SEdmund Nadolski 343ecf160b4SLiu Bo rbtree_postorder_for_each_entry_safe(ref, next_ref, 34492876eecSFilipe Manana &preftree->root.rb_root, rbnode) { 34592876eecSFilipe Manana free_inode_elem_list(ref->inode_list); 34686d5f994SEdmund Nadolski free_pref(ref); 34792876eecSFilipe Manana } 34886d5f994SEdmund Nadolski 349ecf160b4SLiu Bo preftree->root = RB_ROOT_CACHED; 3506c336b21SJeff Mahoney preftree->count = 0; 35186d5f994SEdmund Nadolski } 35286d5f994SEdmund Nadolski 353d5c88b73SJan Schmidt /* 354d5c88b73SJan Schmidt * the rules for all callers of this function are: 355d5c88b73SJan Schmidt * - obtaining the parent is the goal 356d5c88b73SJan Schmidt * - if you add a key, you must know that it is a correct key 357d5c88b73SJan Schmidt * - if you cannot add the parent or a correct key, then we will look into the 358d5c88b73SJan Schmidt * block later to set a correct key 359d5c88b73SJan Schmidt * 360d5c88b73SJan Schmidt * delayed refs 361d5c88b73SJan Schmidt * ============ 362d5c88b73SJan Schmidt * backref type | shared | indirect | shared | indirect 363d5c88b73SJan Schmidt * information | tree | tree | data | data 364d5c88b73SJan Schmidt * --------------------+--------+----------+--------+---------- 365d5c88b73SJan Schmidt * parent logical | y | - | - | - 366d5c88b73SJan Schmidt * key to resolve | - | y | y | y 367d5c88b73SJan Schmidt * tree block logical | - | - | - | - 368d5c88b73SJan Schmidt * root for resolving | y | y | y | y 369d5c88b73SJan Schmidt * 370d5c88b73SJan Schmidt * - column 1: we've the parent -> done 371d5c88b73SJan Schmidt * - column 2, 3, 4: we use the key to find the parent 372d5c88b73SJan Schmidt * 373d5c88b73SJan Schmidt * on disk refs (inline or keyed) 374d5c88b73SJan Schmidt * ============================== 375d5c88b73SJan Schmidt * backref type | shared | indirect | shared | indirect 376d5c88b73SJan Schmidt * information | tree | tree | data | data 377d5c88b73SJan Schmidt * --------------------+--------+----------+--------+---------- 378d5c88b73SJan Schmidt * parent logical | y | - | y | - 379d5c88b73SJan Schmidt * key to resolve | - | - | - | y 380d5c88b73SJan Schmidt * tree block logical | y | y | y | y 381d5c88b73SJan Schmidt * root for resolving | - | y | y | y 382d5c88b73SJan Schmidt * 383d5c88b73SJan Schmidt * - column 1, 3: we've the parent -> done 384d5c88b73SJan Schmidt * - column 2: we take the first key from the block to find the parent 385e0c476b1SJeff Mahoney * (see add_missing_keys) 386d5c88b73SJan Schmidt * - column 4: we use the key to find the parent 387d5c88b73SJan Schmidt * 388d5c88b73SJan Schmidt * additional information that's available but not required to find the parent 389d5c88b73SJan Schmidt * block might help in merging entries to gain some speed. 390d5c88b73SJan Schmidt */ 39100142756SJeff Mahoney static int add_prelim_ref(const struct btrfs_fs_info *fs_info, 39200142756SJeff Mahoney struct preftree *preftree, u64 root_id, 393e0c476b1SJeff Mahoney const struct btrfs_key *key, int level, u64 parent, 3943ec4d323SEdmund Nadolski u64 wanted_disk_byte, int count, 3953ec4d323SEdmund Nadolski struct share_check *sc, gfp_t gfp_mask) 3968da6d581SJan Schmidt { 397e0c476b1SJeff Mahoney struct prelim_ref *ref; 3988da6d581SJan Schmidt 39948ec4736SLiu Bo if (root_id == BTRFS_DATA_RELOC_TREE_OBJECTID) 40048ec4736SLiu Bo return 0; 40148ec4736SLiu Bo 402b9e9a6cbSWang Shilong ref = kmem_cache_alloc(btrfs_prelim_ref_cache, gfp_mask); 4038da6d581SJan Schmidt if (!ref) 4048da6d581SJan Schmidt return -ENOMEM; 4058da6d581SJan Schmidt 4068da6d581SJan Schmidt ref->root_id = root_id; 4077ac8b88eSethanwu if (key) 408d5c88b73SJan Schmidt ref->key_for_search = *key; 4097ac8b88eSethanwu else 410d5c88b73SJan Schmidt memset(&ref->key_for_search, 0, sizeof(ref->key_for_search)); 4118da6d581SJan Schmidt 4123301958bSJan Schmidt ref->inode_list = NULL; 4138da6d581SJan Schmidt ref->level = level; 4148da6d581SJan Schmidt ref->count = count; 4158da6d581SJan Schmidt ref->parent = parent; 4168da6d581SJan Schmidt ref->wanted_disk_byte = wanted_disk_byte; 4173ec4d323SEdmund Nadolski prelim_ref_insert(fs_info, preftree, ref, sc); 4183ec4d323SEdmund Nadolski return extent_is_shared(sc); 4198da6d581SJan Schmidt } 4208da6d581SJan Schmidt 42186d5f994SEdmund Nadolski /* direct refs use root == 0, key == NULL */ 42200142756SJeff Mahoney static int add_direct_ref(const struct btrfs_fs_info *fs_info, 42300142756SJeff Mahoney struct preftrees *preftrees, int level, u64 parent, 4243ec4d323SEdmund Nadolski u64 wanted_disk_byte, int count, 4253ec4d323SEdmund Nadolski struct share_check *sc, gfp_t gfp_mask) 42686d5f994SEdmund Nadolski { 42700142756SJeff Mahoney return add_prelim_ref(fs_info, &preftrees->direct, 0, NULL, level, 4283ec4d323SEdmund Nadolski parent, wanted_disk_byte, count, sc, gfp_mask); 42986d5f994SEdmund Nadolski } 43086d5f994SEdmund Nadolski 43186d5f994SEdmund Nadolski /* indirect refs use parent == 0 */ 43200142756SJeff Mahoney static int add_indirect_ref(const struct btrfs_fs_info *fs_info, 43300142756SJeff Mahoney struct preftrees *preftrees, u64 root_id, 43486d5f994SEdmund Nadolski const struct btrfs_key *key, int level, 4353ec4d323SEdmund Nadolski u64 wanted_disk_byte, int count, 4363ec4d323SEdmund Nadolski struct share_check *sc, gfp_t gfp_mask) 43786d5f994SEdmund Nadolski { 43886d5f994SEdmund Nadolski struct preftree *tree = &preftrees->indirect; 43986d5f994SEdmund Nadolski 44086d5f994SEdmund Nadolski if (!key) 44186d5f994SEdmund Nadolski tree = &preftrees->indirect_missing_keys; 44200142756SJeff Mahoney return add_prelim_ref(fs_info, tree, root_id, key, level, 0, 4433ec4d323SEdmund Nadolski wanted_disk_byte, count, sc, gfp_mask); 44486d5f994SEdmund Nadolski } 44586d5f994SEdmund Nadolski 446ed58f2e6Sethanwu static int is_shared_data_backref(struct preftrees *preftrees, u64 bytenr) 447ed58f2e6Sethanwu { 448ed58f2e6Sethanwu struct rb_node **p = &preftrees->direct.root.rb_root.rb_node; 449ed58f2e6Sethanwu struct rb_node *parent = NULL; 450ed58f2e6Sethanwu struct prelim_ref *ref = NULL; 4519c6c723fSArnd Bergmann struct prelim_ref target = {}; 452ed58f2e6Sethanwu int result; 453ed58f2e6Sethanwu 454ed58f2e6Sethanwu target.parent = bytenr; 455ed58f2e6Sethanwu 456ed58f2e6Sethanwu while (*p) { 457ed58f2e6Sethanwu parent = *p; 458ed58f2e6Sethanwu ref = rb_entry(parent, struct prelim_ref, rbnode); 459ed58f2e6Sethanwu result = prelim_ref_compare(ref, &target); 460ed58f2e6Sethanwu 461ed58f2e6Sethanwu if (result < 0) 462ed58f2e6Sethanwu p = &(*p)->rb_left; 463ed58f2e6Sethanwu else if (result > 0) 464ed58f2e6Sethanwu p = &(*p)->rb_right; 465ed58f2e6Sethanwu else 466ed58f2e6Sethanwu return 1; 467ed58f2e6Sethanwu } 468ed58f2e6Sethanwu return 0; 469ed58f2e6Sethanwu } 470ed58f2e6Sethanwu 471a2c8d27eSFilipe Manana static int add_all_parents(struct btrfs_backref_walk_ctx *ctx, 472a2c8d27eSFilipe Manana struct btrfs_root *root, struct btrfs_path *path, 473ed58f2e6Sethanwu struct ulist *parents, 474ed58f2e6Sethanwu struct preftrees *preftrees, struct prelim_ref *ref, 475a2c8d27eSFilipe Manana int level) 4768da6d581SJan Schmidt { 47769bca40dSAlexander Block int ret = 0; 47869bca40dSAlexander Block int slot; 47969bca40dSAlexander Block struct extent_buffer *eb; 48069bca40dSAlexander Block struct btrfs_key key; 4817ef81ac8SJosef Bacik struct btrfs_key *key_for_search = &ref->key_for_search; 4828da6d581SJan Schmidt struct btrfs_file_extent_item *fi; 483ed8c4913SJosef Bacik struct extent_inode_elem *eie = NULL, *old = NULL; 4848da6d581SJan Schmidt u64 disk_byte; 4857ef81ac8SJosef Bacik u64 wanted_disk_byte = ref->wanted_disk_byte; 4867ef81ac8SJosef Bacik u64 count = 0; 4877ac8b88eSethanwu u64 data_offset; 488560840afSBoris Burkov u8 type; 4898da6d581SJan Schmidt 49069bca40dSAlexander Block if (level != 0) { 49169bca40dSAlexander Block eb = path->nodes[level]; 49269bca40dSAlexander Block ret = ulist_add(parents, eb->start, 0, GFP_NOFS); 4933301958bSJan Schmidt if (ret < 0) 4943301958bSJan Schmidt return ret; 4958da6d581SJan Schmidt return 0; 49669bca40dSAlexander Block } 4978da6d581SJan Schmidt 4988da6d581SJan Schmidt /* 499ed58f2e6Sethanwu * 1. We normally enter this function with the path already pointing to 50069bca40dSAlexander Block * the first item to check. But sometimes, we may enter it with 501ed58f2e6Sethanwu * slot == nritems. 502ed58f2e6Sethanwu * 2. We are searching for normal backref but bytenr of this leaf 503ed58f2e6Sethanwu * matches shared data backref 504cfc0eed0Sethanwu * 3. The leaf owner is not equal to the root we are searching 505cfc0eed0Sethanwu * 506ed58f2e6Sethanwu * For these cases, go to the next leaf before we continue. 5078da6d581SJan Schmidt */ 508ed58f2e6Sethanwu eb = path->nodes[0]; 509ed58f2e6Sethanwu if (path->slots[0] >= btrfs_header_nritems(eb) || 510cfc0eed0Sethanwu is_shared_data_backref(preftrees, eb->start) || 511cfc0eed0Sethanwu ref->root_id != btrfs_header_owner(eb)) { 512a2c8d27eSFilipe Manana if (ctx->time_seq == BTRFS_SEQ_LAST) 51321633fc6SQu Wenruo ret = btrfs_next_leaf(root, path); 51421633fc6SQu Wenruo else 515a2c8d27eSFilipe Manana ret = btrfs_next_old_leaf(root, path, ctx->time_seq); 51621633fc6SQu Wenruo } 5178da6d581SJan Schmidt 518b25b0b87Sethanwu while (!ret && count < ref->count) { 5198da6d581SJan Schmidt eb = path->nodes[0]; 52069bca40dSAlexander Block slot = path->slots[0]; 52169bca40dSAlexander Block 52269bca40dSAlexander Block btrfs_item_key_to_cpu(eb, &key, slot); 52369bca40dSAlexander Block 52469bca40dSAlexander Block if (key.objectid != key_for_search->objectid || 52569bca40dSAlexander Block key.type != BTRFS_EXTENT_DATA_KEY) 52669bca40dSAlexander Block break; 52769bca40dSAlexander Block 528ed58f2e6Sethanwu /* 529ed58f2e6Sethanwu * We are searching for normal backref but bytenr of this leaf 530cfc0eed0Sethanwu * matches shared data backref, OR 531cfc0eed0Sethanwu * the leaf owner is not equal to the root we are searching for 532ed58f2e6Sethanwu */ 533cfc0eed0Sethanwu if (slot == 0 && 534cfc0eed0Sethanwu (is_shared_data_backref(preftrees, eb->start) || 535cfc0eed0Sethanwu ref->root_id != btrfs_header_owner(eb))) { 536a2c8d27eSFilipe Manana if (ctx->time_seq == BTRFS_SEQ_LAST) 537ed58f2e6Sethanwu ret = btrfs_next_leaf(root, path); 538ed58f2e6Sethanwu else 539a2c8d27eSFilipe Manana ret = btrfs_next_old_leaf(root, path, ctx->time_seq); 540ed58f2e6Sethanwu continue; 541ed58f2e6Sethanwu } 54269bca40dSAlexander Block fi = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item); 543560840afSBoris Burkov type = btrfs_file_extent_type(eb, fi); 544560840afSBoris Burkov if (type == BTRFS_FILE_EXTENT_INLINE) 545560840afSBoris Burkov goto next; 5468da6d581SJan Schmidt disk_byte = btrfs_file_extent_disk_bytenr(eb, fi); 5477ac8b88eSethanwu data_offset = btrfs_file_extent_offset(eb, fi); 54869bca40dSAlexander Block 54969bca40dSAlexander Block if (disk_byte == wanted_disk_byte) { 55069bca40dSAlexander Block eie = NULL; 551ed8c4913SJosef Bacik old = NULL; 5527ac8b88eSethanwu if (ref->key_for_search.offset == key.offset - data_offset) 5537ef81ac8SJosef Bacik count++; 5547ac8b88eSethanwu else 5557ac8b88eSethanwu goto next; 5560cad8f14SFilipe Manana if (!ctx->skip_inode_ref_list) { 55788ffb665SFilipe Manana ret = check_extent_in_eb(ctx, &key, eb, fi, &eie); 55888ffb665SFilipe Manana if (ret == BTRFS_ITERATE_EXTENT_INODES_STOP || 55988ffb665SFilipe Manana ret < 0) 56069bca40dSAlexander Block break; 5618da6d581SJan Schmidt } 562ed8c4913SJosef Bacik if (ret > 0) 563ed8c4913SJosef Bacik goto next; 5644eb1f66dSTakashi Iwai ret = ulist_add_merge_ptr(parents, eb->start, 5654eb1f66dSTakashi Iwai eie, (void **)&old, GFP_NOFS); 56669bca40dSAlexander Block if (ret < 0) 56769bca40dSAlexander Block break; 5680cad8f14SFilipe Manana if (!ret && !ctx->skip_inode_ref_list) { 569ed8c4913SJosef Bacik while (old->next) 570ed8c4913SJosef Bacik old = old->next; 571ed8c4913SJosef Bacik old->next = eie; 57269bca40dSAlexander Block } 573f05c4746SWang Shilong eie = NULL; 57469bca40dSAlexander Block } 575ed8c4913SJosef Bacik next: 576a2c8d27eSFilipe Manana if (ctx->time_seq == BTRFS_SEQ_LAST) 57721633fc6SQu Wenruo ret = btrfs_next_item(root, path); 57821633fc6SQu Wenruo else 579a2c8d27eSFilipe Manana ret = btrfs_next_old_item(root, path, ctx->time_seq); 5808da6d581SJan Schmidt } 5818da6d581SJan Schmidt 58288ffb665SFilipe Manana if (ret == BTRFS_ITERATE_EXTENT_INODES_STOP || ret < 0) 583f05c4746SWang Shilong free_inode_elem_list(eie); 58488ffb665SFilipe Manana else if (ret > 0) 58588ffb665SFilipe Manana ret = 0; 58688ffb665SFilipe Manana 58769bca40dSAlexander Block return ret; 5888da6d581SJan Schmidt } 5898da6d581SJan Schmidt 5908da6d581SJan Schmidt /* 5918da6d581SJan Schmidt * resolve an indirect backref in the form (root_id, key, level) 5928da6d581SJan Schmidt * to a logical address 5938da6d581SJan Schmidt */ 594a2c8d27eSFilipe Manana static int resolve_indirect_ref(struct btrfs_backref_walk_ctx *ctx, 595a2c8d27eSFilipe Manana struct btrfs_path *path, 596ed58f2e6Sethanwu struct preftrees *preftrees, 597a2c8d27eSFilipe Manana struct prelim_ref *ref, struct ulist *parents) 5988da6d581SJan Schmidt { 5998da6d581SJan Schmidt struct btrfs_root *root; 6008da6d581SJan Schmidt struct extent_buffer *eb; 6018da6d581SJan Schmidt int ret = 0; 6028da6d581SJan Schmidt int root_level; 6038da6d581SJan Schmidt int level = ref->level; 6047ac8b88eSethanwu struct btrfs_key search_key = ref->key_for_search; 6058da6d581SJan Schmidt 60649d11beaSJosef Bacik /* 60749d11beaSJosef Bacik * If we're search_commit_root we could possibly be holding locks on 60849d11beaSJosef Bacik * other tree nodes. This happens when qgroups does backref walks when 60949d11beaSJosef Bacik * adding new delayed refs. To deal with this we need to look in cache 61049d11beaSJosef Bacik * for the root, and if we don't find it then we need to search the 61149d11beaSJosef Bacik * tree_root's commit root, thus the btrfs_get_fs_root_commit_root usage 61249d11beaSJosef Bacik * here. 61349d11beaSJosef Bacik */ 61449d11beaSJosef Bacik if (path->search_commit_root) 615a2c8d27eSFilipe Manana root = btrfs_get_fs_root_commit_root(ctx->fs_info, path, ref->root_id); 61649d11beaSJosef Bacik else 617a2c8d27eSFilipe Manana root = btrfs_get_fs_root(ctx->fs_info, ref->root_id, false); 6188da6d581SJan Schmidt if (IS_ERR(root)) { 6198da6d581SJan Schmidt ret = PTR_ERR(root); 6209326f76fSJosef Bacik goto out_free; 6219326f76fSJosef Bacik } 6229326f76fSJosef Bacik 62339dba873SJosef Bacik if (!path->search_commit_root && 62439dba873SJosef Bacik test_bit(BTRFS_ROOT_DELETING, &root->state)) { 62539dba873SJosef Bacik ret = -ENOENT; 62639dba873SJosef Bacik goto out; 62739dba873SJosef Bacik } 62839dba873SJosef Bacik 629a2c8d27eSFilipe Manana if (btrfs_is_testing(ctx->fs_info)) { 630d9ee522bSJosef Bacik ret = -ENOENT; 631d9ee522bSJosef Bacik goto out; 632d9ee522bSJosef Bacik } 633d9ee522bSJosef Bacik 6349e351cc8SJosef Bacik if (path->search_commit_root) 6359e351cc8SJosef Bacik root_level = btrfs_header_level(root->commit_root); 636a2c8d27eSFilipe Manana else if (ctx->time_seq == BTRFS_SEQ_LAST) 63721633fc6SQu Wenruo root_level = btrfs_header_level(root->node); 6389e351cc8SJosef Bacik else 639a2c8d27eSFilipe Manana root_level = btrfs_old_root_level(root, ctx->time_seq); 6408da6d581SJan Schmidt 641c75e8394SJosef Bacik if (root_level + 1 == level) 6428da6d581SJan Schmidt goto out; 6438da6d581SJan Schmidt 6447ac8b88eSethanwu /* 6457ac8b88eSethanwu * We can often find data backrefs with an offset that is too large 6467ac8b88eSethanwu * (>= LLONG_MAX, maximum allowed file offset) due to underflows when 6477ac8b88eSethanwu * subtracting a file's offset with the data offset of its 6487ac8b88eSethanwu * corresponding extent data item. This can happen for example in the 6497ac8b88eSethanwu * clone ioctl. 6507ac8b88eSethanwu * 6517ac8b88eSethanwu * So if we detect such case we set the search key's offset to zero to 6527ac8b88eSethanwu * make sure we will find the matching file extent item at 6537ac8b88eSethanwu * add_all_parents(), otherwise we will miss it because the offset 6547ac8b88eSethanwu * taken form the backref is much larger then the offset of the file 6557ac8b88eSethanwu * extent item. This can make us scan a very large number of file 6567ac8b88eSethanwu * extent items, but at least it will not make us miss any. 6577ac8b88eSethanwu * 6587ac8b88eSethanwu * This is an ugly workaround for a behaviour that should have never 6597ac8b88eSethanwu * existed, but it does and a fix for the clone ioctl would touch a lot 6607ac8b88eSethanwu * of places, cause backwards incompatibility and would not fix the 6617ac8b88eSethanwu * problem for extents cloned with older kernels. 6627ac8b88eSethanwu */ 6637ac8b88eSethanwu if (search_key.type == BTRFS_EXTENT_DATA_KEY && 6647ac8b88eSethanwu search_key.offset >= LLONG_MAX) 6657ac8b88eSethanwu search_key.offset = 0; 6668da6d581SJan Schmidt path->lowest_level = level; 667a2c8d27eSFilipe Manana if (ctx->time_seq == BTRFS_SEQ_LAST) 6687ac8b88eSethanwu ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0); 66921633fc6SQu Wenruo else 670a2c8d27eSFilipe Manana ret = btrfs_search_old_slot(root, &search_key, path, ctx->time_seq); 671538f72cdSWang Shilong 672a2c8d27eSFilipe Manana btrfs_debug(ctx->fs_info, 673ab8d0fc4SJeff Mahoney "search slot in root %llu (level %d, ref count %d) returned %d for key (%llu %u %llu)", 674c1c9ff7cSGeert Uytterhoeven ref->root_id, level, ref->count, ret, 675c1c9ff7cSGeert Uytterhoeven ref->key_for_search.objectid, ref->key_for_search.type, 676c1c9ff7cSGeert Uytterhoeven ref->key_for_search.offset); 6778da6d581SJan Schmidt if (ret < 0) 6788da6d581SJan Schmidt goto out; 6798da6d581SJan Schmidt 6808da6d581SJan Schmidt eb = path->nodes[level]; 6819345457fSJan Schmidt while (!eb) { 682fae7f21cSDulshani Gunawardhana if (WARN_ON(!level)) { 6838da6d581SJan Schmidt ret = 1; 6848da6d581SJan Schmidt goto out; 6858da6d581SJan Schmidt } 6869345457fSJan Schmidt level--; 6879345457fSJan Schmidt eb = path->nodes[level]; 6889345457fSJan Schmidt } 6898da6d581SJan Schmidt 690a2c8d27eSFilipe Manana ret = add_all_parents(ctx, root, path, parents, preftrees, ref, level); 6918da6d581SJan Schmidt out: 69200246528SJosef Bacik btrfs_put_root(root); 6939326f76fSJosef Bacik out_free: 694da61d31aSJosef Bacik path->lowest_level = 0; 695da61d31aSJosef Bacik btrfs_release_path(path); 6968da6d581SJan Schmidt return ret; 6978da6d581SJan Schmidt } 6988da6d581SJan Schmidt 6994dae077aSJeff Mahoney static struct extent_inode_elem * 7004dae077aSJeff Mahoney unode_aux_to_inode_list(struct ulist_node *node) 7014dae077aSJeff Mahoney { 7024dae077aSJeff Mahoney if (!node) 7034dae077aSJeff Mahoney return NULL; 7044dae077aSJeff Mahoney return (struct extent_inode_elem *)(uintptr_t)node->aux; 7054dae077aSJeff Mahoney } 7064dae077aSJeff Mahoney 7075614dc3aSFilipe Manana static void free_leaf_list(struct ulist *ulist) 7085614dc3aSFilipe Manana { 7095614dc3aSFilipe Manana struct ulist_node *node; 7105614dc3aSFilipe Manana struct ulist_iterator uiter; 7115614dc3aSFilipe Manana 7125614dc3aSFilipe Manana ULIST_ITER_INIT(&uiter); 7135614dc3aSFilipe Manana while ((node = ulist_next(ulist, &uiter))) 7145614dc3aSFilipe Manana free_inode_elem_list(unode_aux_to_inode_list(node)); 7155614dc3aSFilipe Manana 7165614dc3aSFilipe Manana ulist_free(ulist); 7175614dc3aSFilipe Manana } 7185614dc3aSFilipe Manana 7198da6d581SJan Schmidt /* 72052042d8eSAndrea Gelmini * We maintain three separate rbtrees: one for direct refs, one for 72186d5f994SEdmund Nadolski * indirect refs which have a key, and one for indirect refs which do not 72286d5f994SEdmund Nadolski * have a key. Each tree does merge on insertion. 72386d5f994SEdmund Nadolski * 72486d5f994SEdmund Nadolski * Once all of the references are located, we iterate over the tree of 72586d5f994SEdmund Nadolski * indirect refs with missing keys. An appropriate key is located and 72686d5f994SEdmund Nadolski * the ref is moved onto the tree for indirect refs. After all missing 72786d5f994SEdmund Nadolski * keys are thus located, we iterate over the indirect ref tree, resolve 72886d5f994SEdmund Nadolski * each reference, and then insert the resolved reference onto the 72986d5f994SEdmund Nadolski * direct tree (merging there too). 73086d5f994SEdmund Nadolski * 73186d5f994SEdmund Nadolski * New backrefs (i.e., for parent nodes) are added to the appropriate 73286d5f994SEdmund Nadolski * rbtree as they are encountered. The new backrefs are subsequently 73386d5f994SEdmund Nadolski * resolved as above. 7348da6d581SJan Schmidt */ 735a2c8d27eSFilipe Manana static int resolve_indirect_refs(struct btrfs_backref_walk_ctx *ctx, 736a2c8d27eSFilipe Manana struct btrfs_path *path, 73786d5f994SEdmund Nadolski struct preftrees *preftrees, 7386ce6ba53SFilipe Manana struct share_check *sc) 7398da6d581SJan Schmidt { 7408da6d581SJan Schmidt int err; 7418da6d581SJan Schmidt int ret = 0; 7428da6d581SJan Schmidt struct ulist *parents; 7438da6d581SJan Schmidt struct ulist_node *node; 744cd1b413cSJan Schmidt struct ulist_iterator uiter; 74586d5f994SEdmund Nadolski struct rb_node *rnode; 7468da6d581SJan Schmidt 7478da6d581SJan Schmidt parents = ulist_alloc(GFP_NOFS); 7488da6d581SJan Schmidt if (!parents) 7498da6d581SJan Schmidt return -ENOMEM; 7508da6d581SJan Schmidt 7518da6d581SJan Schmidt /* 75286d5f994SEdmund Nadolski * We could trade memory usage for performance here by iterating 75386d5f994SEdmund Nadolski * the tree, allocating new refs for each insertion, and then 75486d5f994SEdmund Nadolski * freeing the entire indirect tree when we're done. In some test 75586d5f994SEdmund Nadolski * cases, the tree can grow quite large (~200k objects). 7568da6d581SJan Schmidt */ 757ecf160b4SLiu Bo while ((rnode = rb_first_cached(&preftrees->indirect.root))) { 75886d5f994SEdmund Nadolski struct prelim_ref *ref; 75986d5f994SEdmund Nadolski 76086d5f994SEdmund Nadolski ref = rb_entry(rnode, struct prelim_ref, rbnode); 76186d5f994SEdmund Nadolski if (WARN(ref->parent, 76286d5f994SEdmund Nadolski "BUG: direct ref found in indirect tree")) { 76386d5f994SEdmund Nadolski ret = -EINVAL; 76486d5f994SEdmund Nadolski goto out; 76586d5f994SEdmund Nadolski } 76686d5f994SEdmund Nadolski 767ecf160b4SLiu Bo rb_erase_cached(&ref->rbnode, &preftrees->indirect.root); 7686c336b21SJeff Mahoney preftrees->indirect.count--; 76986d5f994SEdmund Nadolski 77086d5f994SEdmund Nadolski if (ref->count == 0) { 77186d5f994SEdmund Nadolski free_pref(ref); 7728da6d581SJan Schmidt continue; 77386d5f994SEdmund Nadolski } 77486d5f994SEdmund Nadolski 775877c1476SFilipe Manana if (sc && ref->root_id != sc->root->root_key.objectid) { 77686d5f994SEdmund Nadolski free_pref(ref); 777dc046b10SJosef Bacik ret = BACKREF_FOUND_SHARED; 778dc046b10SJosef Bacik goto out; 779dc046b10SJosef Bacik } 780a2c8d27eSFilipe Manana err = resolve_indirect_ref(ctx, path, preftrees, ref, parents); 78195def2edSWang Shilong /* 78295def2edSWang Shilong * we can only tolerate ENOENT,otherwise,we should catch error 78395def2edSWang Shilong * and return directly. 78495def2edSWang Shilong */ 78595def2edSWang Shilong if (err == -ENOENT) { 786a2c8d27eSFilipe Manana prelim_ref_insert(ctx->fs_info, &preftrees->direct, ref, 7873ec4d323SEdmund Nadolski NULL); 7888da6d581SJan Schmidt continue; 78995def2edSWang Shilong } else if (err) { 79086d5f994SEdmund Nadolski free_pref(ref); 79195def2edSWang Shilong ret = err; 79295def2edSWang Shilong goto out; 79395def2edSWang Shilong } 7948da6d581SJan Schmidt 7958da6d581SJan Schmidt /* we put the first parent into the ref at hand */ 796cd1b413cSJan Schmidt ULIST_ITER_INIT(&uiter); 797cd1b413cSJan Schmidt node = ulist_next(parents, &uiter); 7988da6d581SJan Schmidt ref->parent = node ? node->val : 0; 7994dae077aSJeff Mahoney ref->inode_list = unode_aux_to_inode_list(node); 8008da6d581SJan Schmidt 80186d5f994SEdmund Nadolski /* Add a prelim_ref(s) for any other parent(s). */ 802cd1b413cSJan Schmidt while ((node = ulist_next(parents, &uiter))) { 80386d5f994SEdmund Nadolski struct prelim_ref *new_ref; 80486d5f994SEdmund Nadolski 805b9e9a6cbSWang Shilong new_ref = kmem_cache_alloc(btrfs_prelim_ref_cache, 806b9e9a6cbSWang Shilong GFP_NOFS); 8078da6d581SJan Schmidt if (!new_ref) { 80886d5f994SEdmund Nadolski free_pref(ref); 8098da6d581SJan Schmidt ret = -ENOMEM; 810e36902d4SWang Shilong goto out; 8118da6d581SJan Schmidt } 8128da6d581SJan Schmidt memcpy(new_ref, ref, sizeof(*ref)); 8138da6d581SJan Schmidt new_ref->parent = node->val; 8144dae077aSJeff Mahoney new_ref->inode_list = unode_aux_to_inode_list(node); 815a2c8d27eSFilipe Manana prelim_ref_insert(ctx->fs_info, &preftrees->direct, 8163ec4d323SEdmund Nadolski new_ref, NULL); 8178da6d581SJan Schmidt } 81886d5f994SEdmund Nadolski 8193ec4d323SEdmund Nadolski /* 82052042d8eSAndrea Gelmini * Now it's a direct ref, put it in the direct tree. We must 8213ec4d323SEdmund Nadolski * do this last because the ref could be merged/freed here. 8223ec4d323SEdmund Nadolski */ 823a2c8d27eSFilipe Manana prelim_ref_insert(ctx->fs_info, &preftrees->direct, ref, NULL); 82486d5f994SEdmund Nadolski 8258da6d581SJan Schmidt ulist_reinit(parents); 8269dd14fd6SEdmund Nadolski cond_resched(); 8278da6d581SJan Schmidt } 828e36902d4SWang Shilong out: 8295614dc3aSFilipe Manana /* 8305614dc3aSFilipe Manana * We may have inode lists attached to refs in the parents ulist, so we 8315614dc3aSFilipe Manana * must free them before freeing the ulist and its refs. 8325614dc3aSFilipe Manana */ 8335614dc3aSFilipe Manana free_leaf_list(parents); 8348da6d581SJan Schmidt return ret; 8358da6d581SJan Schmidt } 8368da6d581SJan Schmidt 837d5c88b73SJan Schmidt /* 838d5c88b73SJan Schmidt * read tree blocks and add keys where required. 839d5c88b73SJan Schmidt */ 840e0c476b1SJeff Mahoney static int add_missing_keys(struct btrfs_fs_info *fs_info, 84138e3eebfSJosef Bacik struct preftrees *preftrees, bool lock) 842d5c88b73SJan Schmidt { 843e0c476b1SJeff Mahoney struct prelim_ref *ref; 844d5c88b73SJan Schmidt struct extent_buffer *eb; 84586d5f994SEdmund Nadolski struct preftree *tree = &preftrees->indirect_missing_keys; 84686d5f994SEdmund Nadolski struct rb_node *node; 847d5c88b73SJan Schmidt 848ecf160b4SLiu Bo while ((node = rb_first_cached(&tree->root))) { 849789d6a3aSQu Wenruo struct btrfs_tree_parent_check check = { 0 }; 850789d6a3aSQu Wenruo 85186d5f994SEdmund Nadolski ref = rb_entry(node, struct prelim_ref, rbnode); 852ecf160b4SLiu Bo rb_erase_cached(node, &tree->root); 85386d5f994SEdmund Nadolski 85486d5f994SEdmund Nadolski BUG_ON(ref->parent); /* should not be a direct ref */ 85586d5f994SEdmund Nadolski BUG_ON(ref->key_for_search.type); 856d5c88b73SJan Schmidt BUG_ON(!ref->wanted_disk_byte); 85786d5f994SEdmund Nadolski 858789d6a3aSQu Wenruo check.level = ref->level - 1; 859789d6a3aSQu Wenruo check.owner_root = ref->root_id; 860789d6a3aSQu Wenruo 861789d6a3aSQu Wenruo eb = read_tree_block(fs_info, ref->wanted_disk_byte, &check); 86264c043deSLiu Bo if (IS_ERR(eb)) { 86386d5f994SEdmund Nadolski free_pref(ref); 86464c043deSLiu Bo return PTR_ERR(eb); 8654eb150d6SQu Wenruo } 8664eb150d6SQu Wenruo if (!extent_buffer_uptodate(eb)) { 86786d5f994SEdmund Nadolski free_pref(ref); 868416bc658SJosef Bacik free_extent_buffer(eb); 869416bc658SJosef Bacik return -EIO; 870416bc658SJosef Bacik } 8714eb150d6SQu Wenruo 87238e3eebfSJosef Bacik if (lock) 873d5c88b73SJan Schmidt btrfs_tree_read_lock(eb); 874d5c88b73SJan Schmidt if (btrfs_header_level(eb) == 0) 875d5c88b73SJan Schmidt btrfs_item_key_to_cpu(eb, &ref->key_for_search, 0); 876d5c88b73SJan Schmidt else 877d5c88b73SJan Schmidt btrfs_node_key_to_cpu(eb, &ref->key_for_search, 0); 87838e3eebfSJosef Bacik if (lock) 879d5c88b73SJan Schmidt btrfs_tree_read_unlock(eb); 880d5c88b73SJan Schmidt free_extent_buffer(eb); 8813ec4d323SEdmund Nadolski prelim_ref_insert(fs_info, &preftrees->indirect, ref, NULL); 8829dd14fd6SEdmund Nadolski cond_resched(); 883d5c88b73SJan Schmidt } 884d5c88b73SJan Schmidt return 0; 885d5c88b73SJan Schmidt } 886d5c88b73SJan Schmidt 8878da6d581SJan Schmidt /* 8888da6d581SJan Schmidt * add all currently queued delayed refs from this head whose seq nr is 8898da6d581SJan Schmidt * smaller or equal that seq to the list 8908da6d581SJan Schmidt */ 89100142756SJeff Mahoney static int add_delayed_refs(const struct btrfs_fs_info *fs_info, 89200142756SJeff Mahoney struct btrfs_delayed_ref_head *head, u64 seq, 893b25b0b87Sethanwu struct preftrees *preftrees, struct share_check *sc) 8948da6d581SJan Schmidt { 895c6fc2454SQu Wenruo struct btrfs_delayed_ref_node *node; 896d5c88b73SJan Schmidt struct btrfs_key key; 8970e0adbcfSJosef Bacik struct rb_node *n; 89801747e92SEdmund Nadolski int count; 899b1375d64SJan Schmidt int ret = 0; 9008da6d581SJan Schmidt 901d7df2c79SJosef Bacik spin_lock(&head->lock); 902e3d03965SLiu Bo for (n = rb_first_cached(&head->ref_tree); n; n = rb_next(n)) { 9030e0adbcfSJosef Bacik node = rb_entry(n, struct btrfs_delayed_ref_node, 9040e0adbcfSJosef Bacik ref_node); 9058da6d581SJan Schmidt if (node->seq > seq) 9068da6d581SJan Schmidt continue; 9078da6d581SJan Schmidt 9088da6d581SJan Schmidt switch (node->action) { 9098da6d581SJan Schmidt case BTRFS_ADD_DELAYED_EXTENT: 9108da6d581SJan Schmidt case BTRFS_UPDATE_DELAYED_HEAD: 9118da6d581SJan Schmidt WARN_ON(1); 9128da6d581SJan Schmidt continue; 9138da6d581SJan Schmidt case BTRFS_ADD_DELAYED_REF: 91401747e92SEdmund Nadolski count = node->ref_mod; 9158da6d581SJan Schmidt break; 9168da6d581SJan Schmidt case BTRFS_DROP_DELAYED_REF: 91701747e92SEdmund Nadolski count = node->ref_mod * -1; 9188da6d581SJan Schmidt break; 9198da6d581SJan Schmidt default: 920290342f6SArnd Bergmann BUG(); 9218da6d581SJan Schmidt } 9228da6d581SJan Schmidt switch (node->type) { 9238da6d581SJan Schmidt case BTRFS_TREE_BLOCK_REF_KEY: { 92486d5f994SEdmund Nadolski /* NORMAL INDIRECT METADATA backref */ 9258da6d581SJan Schmidt struct btrfs_delayed_tree_ref *ref; 926943553efSFilipe Manana struct btrfs_key *key_ptr = NULL; 927943553efSFilipe Manana 928943553efSFilipe Manana if (head->extent_op && head->extent_op->update_key) { 929943553efSFilipe Manana btrfs_disk_key_to_cpu(&key, &head->extent_op->key); 930943553efSFilipe Manana key_ptr = &key; 931943553efSFilipe Manana } 9328da6d581SJan Schmidt 9338da6d581SJan Schmidt ref = btrfs_delayed_node_to_tree_ref(node); 93400142756SJeff Mahoney ret = add_indirect_ref(fs_info, preftrees, ref->root, 935943553efSFilipe Manana key_ptr, ref->level + 1, 93601747e92SEdmund Nadolski node->bytenr, count, sc, 93701747e92SEdmund Nadolski GFP_ATOMIC); 9388da6d581SJan Schmidt break; 9398da6d581SJan Schmidt } 9408da6d581SJan Schmidt case BTRFS_SHARED_BLOCK_REF_KEY: { 94186d5f994SEdmund Nadolski /* SHARED DIRECT METADATA backref */ 9428da6d581SJan Schmidt struct btrfs_delayed_tree_ref *ref; 9438da6d581SJan Schmidt 9448da6d581SJan Schmidt ref = btrfs_delayed_node_to_tree_ref(node); 94586d5f994SEdmund Nadolski 94601747e92SEdmund Nadolski ret = add_direct_ref(fs_info, preftrees, ref->level + 1, 94701747e92SEdmund Nadolski ref->parent, node->bytenr, count, 9483ec4d323SEdmund Nadolski sc, GFP_ATOMIC); 9498da6d581SJan Schmidt break; 9508da6d581SJan Schmidt } 9518da6d581SJan Schmidt case BTRFS_EXTENT_DATA_REF_KEY: { 95286d5f994SEdmund Nadolski /* NORMAL INDIRECT DATA backref */ 9538da6d581SJan Schmidt struct btrfs_delayed_data_ref *ref; 9548da6d581SJan Schmidt ref = btrfs_delayed_node_to_data_ref(node); 9558da6d581SJan Schmidt 9568da6d581SJan Schmidt key.objectid = ref->objectid; 9578da6d581SJan Schmidt key.type = BTRFS_EXTENT_DATA_KEY; 9588da6d581SJan Schmidt key.offset = ref->offset; 959dc046b10SJosef Bacik 960dc046b10SJosef Bacik /* 9614fc7b572SFilipe Manana * If we have a share check context and a reference for 9624fc7b572SFilipe Manana * another inode, we can't exit immediately. This is 9634fc7b572SFilipe Manana * because even if this is a BTRFS_ADD_DELAYED_REF 9644fc7b572SFilipe Manana * reference we may find next a BTRFS_DROP_DELAYED_REF 9654fc7b572SFilipe Manana * which cancels out this ADD reference. 9664fc7b572SFilipe Manana * 9674fc7b572SFilipe Manana * If this is a DROP reference and there was no previous 9684fc7b572SFilipe Manana * ADD reference, then we need to signal that when we 9694fc7b572SFilipe Manana * process references from the extent tree (through 9704fc7b572SFilipe Manana * add_inline_refs() and add_keyed_refs()), we should 9714fc7b572SFilipe Manana * not exit early if we find a reference for another 9724fc7b572SFilipe Manana * inode, because one of the delayed DROP references 9734fc7b572SFilipe Manana * may cancel that reference in the extent tree. 974dc046b10SJosef Bacik */ 9754fc7b572SFilipe Manana if (sc && count < 0) 9764fc7b572SFilipe Manana sc->have_delayed_delete_refs = true; 977dc046b10SJosef Bacik 97800142756SJeff Mahoney ret = add_indirect_ref(fs_info, preftrees, ref->root, 97901747e92SEdmund Nadolski &key, 0, node->bytenr, count, sc, 98001747e92SEdmund Nadolski GFP_ATOMIC); 9818da6d581SJan Schmidt break; 9828da6d581SJan Schmidt } 9838da6d581SJan Schmidt case BTRFS_SHARED_DATA_REF_KEY: { 98486d5f994SEdmund Nadolski /* SHARED DIRECT FULL backref */ 9858da6d581SJan Schmidt struct btrfs_delayed_data_ref *ref; 9868da6d581SJan Schmidt 9878da6d581SJan Schmidt ref = btrfs_delayed_node_to_data_ref(node); 98886d5f994SEdmund Nadolski 98901747e92SEdmund Nadolski ret = add_direct_ref(fs_info, preftrees, 0, ref->parent, 99001747e92SEdmund Nadolski node->bytenr, count, sc, 99101747e92SEdmund Nadolski GFP_ATOMIC); 9928da6d581SJan Schmidt break; 9938da6d581SJan Schmidt } 9948da6d581SJan Schmidt default: 9958da6d581SJan Schmidt WARN_ON(1); 9968da6d581SJan Schmidt } 9973ec4d323SEdmund Nadolski /* 9983ec4d323SEdmund Nadolski * We must ignore BACKREF_FOUND_SHARED until all delayed 9993ec4d323SEdmund Nadolski * refs have been checked. 10003ec4d323SEdmund Nadolski */ 10013ec4d323SEdmund Nadolski if (ret && (ret != BACKREF_FOUND_SHARED)) 1002d7df2c79SJosef Bacik break; 10038da6d581SJan Schmidt } 10043ec4d323SEdmund Nadolski if (!ret) 10053ec4d323SEdmund Nadolski ret = extent_is_shared(sc); 10064fc7b572SFilipe Manana 1007d7df2c79SJosef Bacik spin_unlock(&head->lock); 1008d7df2c79SJosef Bacik return ret; 10098da6d581SJan Schmidt } 10108da6d581SJan Schmidt 10118da6d581SJan Schmidt /* 10128da6d581SJan Schmidt * add all inline backrefs for bytenr to the list 10133ec4d323SEdmund Nadolski * 10143ec4d323SEdmund Nadolski * Returns 0 on success, <0 on error, or BACKREF_FOUND_SHARED. 10158da6d581SJan Schmidt */ 1016f73853c7SFilipe Manana static int add_inline_refs(struct btrfs_backref_walk_ctx *ctx, 1017f73853c7SFilipe Manana struct btrfs_path *path, 101886d5f994SEdmund Nadolski int *info_level, struct preftrees *preftrees, 1019b25b0b87Sethanwu struct share_check *sc) 10208da6d581SJan Schmidt { 1021b1375d64SJan Schmidt int ret = 0; 10228da6d581SJan Schmidt int slot; 10238da6d581SJan Schmidt struct extent_buffer *leaf; 10248da6d581SJan Schmidt struct btrfs_key key; 1025261c84b6SJosef Bacik struct btrfs_key found_key; 10268da6d581SJan Schmidt unsigned long ptr; 10278da6d581SJan Schmidt unsigned long end; 10288da6d581SJan Schmidt struct btrfs_extent_item *ei; 10298da6d581SJan Schmidt u64 flags; 10308da6d581SJan Schmidt u64 item_size; 10318da6d581SJan Schmidt 10328da6d581SJan Schmidt /* 10338da6d581SJan Schmidt * enumerate all inline refs 10348da6d581SJan Schmidt */ 10358da6d581SJan Schmidt leaf = path->nodes[0]; 1036dadcaf78SJan Schmidt slot = path->slots[0]; 10378da6d581SJan Schmidt 10383212fa14SJosef Bacik item_size = btrfs_item_size(leaf, slot); 10398da6d581SJan Schmidt BUG_ON(item_size < sizeof(*ei)); 10408da6d581SJan Schmidt 10418da6d581SJan Schmidt ei = btrfs_item_ptr(leaf, slot, struct btrfs_extent_item); 1042f73853c7SFilipe Manana 1043f73853c7SFilipe Manana if (ctx->check_extent_item) { 1044f73853c7SFilipe Manana ret = ctx->check_extent_item(ctx->bytenr, ei, leaf, ctx->user_ctx); 1045f73853c7SFilipe Manana if (ret) 1046f73853c7SFilipe Manana return ret; 1047f73853c7SFilipe Manana } 1048f73853c7SFilipe Manana 10498da6d581SJan Schmidt flags = btrfs_extent_flags(leaf, ei); 1050261c84b6SJosef Bacik btrfs_item_key_to_cpu(leaf, &found_key, slot); 10518da6d581SJan Schmidt 10528da6d581SJan Schmidt ptr = (unsigned long)(ei + 1); 10538da6d581SJan Schmidt end = (unsigned long)ei + item_size; 10548da6d581SJan Schmidt 1055261c84b6SJosef Bacik if (found_key.type == BTRFS_EXTENT_ITEM_KEY && 1056261c84b6SJosef Bacik flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) { 10578da6d581SJan Schmidt struct btrfs_tree_block_info *info; 10588da6d581SJan Schmidt 10598da6d581SJan Schmidt info = (struct btrfs_tree_block_info *)ptr; 10608da6d581SJan Schmidt *info_level = btrfs_tree_block_level(leaf, info); 10618da6d581SJan Schmidt ptr += sizeof(struct btrfs_tree_block_info); 10628da6d581SJan Schmidt BUG_ON(ptr > end); 1063261c84b6SJosef Bacik } else if (found_key.type == BTRFS_METADATA_ITEM_KEY) { 1064261c84b6SJosef Bacik *info_level = found_key.offset; 10658da6d581SJan Schmidt } else { 10668da6d581SJan Schmidt BUG_ON(!(flags & BTRFS_EXTENT_FLAG_DATA)); 10678da6d581SJan Schmidt } 10688da6d581SJan Schmidt 10698da6d581SJan Schmidt while (ptr < end) { 10708da6d581SJan Schmidt struct btrfs_extent_inline_ref *iref; 10718da6d581SJan Schmidt u64 offset; 10728da6d581SJan Schmidt int type; 10738da6d581SJan Schmidt 10748da6d581SJan Schmidt iref = (struct btrfs_extent_inline_ref *)ptr; 10753de28d57SLiu Bo type = btrfs_get_extent_inline_ref_type(leaf, iref, 10763de28d57SLiu Bo BTRFS_REF_TYPE_ANY); 10773de28d57SLiu Bo if (type == BTRFS_REF_TYPE_INVALID) 1078af431dcbSSu Yue return -EUCLEAN; 10793de28d57SLiu Bo 10808da6d581SJan Schmidt offset = btrfs_extent_inline_ref_offset(leaf, iref); 10818da6d581SJan Schmidt 10828da6d581SJan Schmidt switch (type) { 10838da6d581SJan Schmidt case BTRFS_SHARED_BLOCK_REF_KEY: 1084f73853c7SFilipe Manana ret = add_direct_ref(ctx->fs_info, preftrees, 108500142756SJeff Mahoney *info_level + 1, offset, 1086f73853c7SFilipe Manana ctx->bytenr, 1, NULL, GFP_NOFS); 10878da6d581SJan Schmidt break; 10888da6d581SJan Schmidt case BTRFS_SHARED_DATA_REF_KEY: { 10898da6d581SJan Schmidt struct btrfs_shared_data_ref *sdref; 10908da6d581SJan Schmidt int count; 10918da6d581SJan Schmidt 10928da6d581SJan Schmidt sdref = (struct btrfs_shared_data_ref *)(iref + 1); 10938da6d581SJan Schmidt count = btrfs_shared_data_ref_count(leaf, sdref); 109486d5f994SEdmund Nadolski 1095f73853c7SFilipe Manana ret = add_direct_ref(ctx->fs_info, preftrees, 0, offset, 1096f73853c7SFilipe Manana ctx->bytenr, count, sc, GFP_NOFS); 10978da6d581SJan Schmidt break; 10988da6d581SJan Schmidt } 10998da6d581SJan Schmidt case BTRFS_TREE_BLOCK_REF_KEY: 1100f73853c7SFilipe Manana ret = add_indirect_ref(ctx->fs_info, preftrees, offset, 110100142756SJeff Mahoney NULL, *info_level + 1, 1102f73853c7SFilipe Manana ctx->bytenr, 1, NULL, GFP_NOFS); 11038da6d581SJan Schmidt break; 11048da6d581SJan Schmidt case BTRFS_EXTENT_DATA_REF_KEY: { 11058da6d581SJan Schmidt struct btrfs_extent_data_ref *dref; 11068da6d581SJan Schmidt int count; 11078da6d581SJan Schmidt u64 root; 11088da6d581SJan Schmidt 11098da6d581SJan Schmidt dref = (struct btrfs_extent_data_ref *)(&iref->offset); 11108da6d581SJan Schmidt count = btrfs_extent_data_ref_count(leaf, dref); 11118da6d581SJan Schmidt key.objectid = btrfs_extent_data_ref_objectid(leaf, 11128da6d581SJan Schmidt dref); 11138da6d581SJan Schmidt key.type = BTRFS_EXTENT_DATA_KEY; 11148da6d581SJan Schmidt key.offset = btrfs_extent_data_ref_offset(leaf, dref); 1115dc046b10SJosef Bacik 1116a0a5472aSFilipe Manana if (sc && key.objectid != sc->inum && 11174fc7b572SFilipe Manana !sc->have_delayed_delete_refs) { 1118dc046b10SJosef Bacik ret = BACKREF_FOUND_SHARED; 1119dc046b10SJosef Bacik break; 1120dc046b10SJosef Bacik } 1121dc046b10SJosef Bacik 11228da6d581SJan Schmidt root = btrfs_extent_data_ref_root(leaf, dref); 112386d5f994SEdmund Nadolski 1124adf02418SFilipe Manana if (!ctx->skip_data_ref || 1125adf02418SFilipe Manana !ctx->skip_data_ref(root, key.objectid, key.offset, 1126adf02418SFilipe Manana ctx->user_ctx)) 1127adf02418SFilipe Manana ret = add_indirect_ref(ctx->fs_info, preftrees, 1128adf02418SFilipe Manana root, &key, 0, ctx->bytenr, 1129adf02418SFilipe Manana count, sc, GFP_NOFS); 11308da6d581SJan Schmidt break; 11318da6d581SJan Schmidt } 11328da6d581SJan Schmidt default: 11338da6d581SJan Schmidt WARN_ON(1); 11348da6d581SJan Schmidt } 11351149ab6bSWang Shilong if (ret) 11361149ab6bSWang Shilong return ret; 11378da6d581SJan Schmidt ptr += btrfs_extent_inline_ref_size(type); 11388da6d581SJan Schmidt } 11398da6d581SJan Schmidt 11408da6d581SJan Schmidt return 0; 11418da6d581SJan Schmidt } 11428da6d581SJan Schmidt 11438da6d581SJan Schmidt /* 11448da6d581SJan Schmidt * add all non-inline backrefs for bytenr to the list 11453ec4d323SEdmund Nadolski * 11463ec4d323SEdmund Nadolski * Returns 0 on success, <0 on error, or BACKREF_FOUND_SHARED. 11478da6d581SJan Schmidt */ 1148adf02418SFilipe Manana static int add_keyed_refs(struct btrfs_backref_walk_ctx *ctx, 1149adf02418SFilipe Manana struct btrfs_root *extent_root, 1150adf02418SFilipe Manana struct btrfs_path *path, 115186d5f994SEdmund Nadolski int info_level, struct preftrees *preftrees, 11523ec4d323SEdmund Nadolski struct share_check *sc) 11538da6d581SJan Schmidt { 115498cc4222SJosef Bacik struct btrfs_fs_info *fs_info = extent_root->fs_info; 11558da6d581SJan Schmidt int ret; 11568da6d581SJan Schmidt int slot; 11578da6d581SJan Schmidt struct extent_buffer *leaf; 11588da6d581SJan Schmidt struct btrfs_key key; 11598da6d581SJan Schmidt 11608da6d581SJan Schmidt while (1) { 11618da6d581SJan Schmidt ret = btrfs_next_item(extent_root, path); 11628da6d581SJan Schmidt if (ret < 0) 11638da6d581SJan Schmidt break; 11648da6d581SJan Schmidt if (ret) { 11658da6d581SJan Schmidt ret = 0; 11668da6d581SJan Schmidt break; 11678da6d581SJan Schmidt } 11688da6d581SJan Schmidt 11698da6d581SJan Schmidt slot = path->slots[0]; 11708da6d581SJan Schmidt leaf = path->nodes[0]; 11718da6d581SJan Schmidt btrfs_item_key_to_cpu(leaf, &key, slot); 11728da6d581SJan Schmidt 1173adf02418SFilipe Manana if (key.objectid != ctx->bytenr) 11748da6d581SJan Schmidt break; 11758da6d581SJan Schmidt if (key.type < BTRFS_TREE_BLOCK_REF_KEY) 11768da6d581SJan Schmidt continue; 11778da6d581SJan Schmidt if (key.type > BTRFS_SHARED_DATA_REF_KEY) 11788da6d581SJan Schmidt break; 11798da6d581SJan Schmidt 11808da6d581SJan Schmidt switch (key.type) { 11818da6d581SJan Schmidt case BTRFS_SHARED_BLOCK_REF_KEY: 118286d5f994SEdmund Nadolski /* SHARED DIRECT METADATA backref */ 118300142756SJeff Mahoney ret = add_direct_ref(fs_info, preftrees, 118400142756SJeff Mahoney info_level + 1, key.offset, 1185adf02418SFilipe Manana ctx->bytenr, 1, NULL, GFP_NOFS); 11868da6d581SJan Schmidt break; 11878da6d581SJan Schmidt case BTRFS_SHARED_DATA_REF_KEY: { 118886d5f994SEdmund Nadolski /* SHARED DIRECT FULL backref */ 11898da6d581SJan Schmidt struct btrfs_shared_data_ref *sdref; 11908da6d581SJan Schmidt int count; 11918da6d581SJan Schmidt 11928da6d581SJan Schmidt sdref = btrfs_item_ptr(leaf, slot, 11938da6d581SJan Schmidt struct btrfs_shared_data_ref); 11948da6d581SJan Schmidt count = btrfs_shared_data_ref_count(leaf, sdref); 119500142756SJeff Mahoney ret = add_direct_ref(fs_info, preftrees, 0, 1196adf02418SFilipe Manana key.offset, ctx->bytenr, count, 11973ec4d323SEdmund Nadolski sc, GFP_NOFS); 11988da6d581SJan Schmidt break; 11998da6d581SJan Schmidt } 12008da6d581SJan Schmidt case BTRFS_TREE_BLOCK_REF_KEY: 120186d5f994SEdmund Nadolski /* NORMAL INDIRECT METADATA backref */ 120200142756SJeff Mahoney ret = add_indirect_ref(fs_info, preftrees, key.offset, 1203adf02418SFilipe Manana NULL, info_level + 1, ctx->bytenr, 12043ec4d323SEdmund Nadolski 1, NULL, GFP_NOFS); 12058da6d581SJan Schmidt break; 12068da6d581SJan Schmidt case BTRFS_EXTENT_DATA_REF_KEY: { 120786d5f994SEdmund Nadolski /* NORMAL INDIRECT DATA backref */ 12088da6d581SJan Schmidt struct btrfs_extent_data_ref *dref; 12098da6d581SJan Schmidt int count; 12108da6d581SJan Schmidt u64 root; 12118da6d581SJan Schmidt 12128da6d581SJan Schmidt dref = btrfs_item_ptr(leaf, slot, 12138da6d581SJan Schmidt struct btrfs_extent_data_ref); 12148da6d581SJan Schmidt count = btrfs_extent_data_ref_count(leaf, dref); 12158da6d581SJan Schmidt key.objectid = btrfs_extent_data_ref_objectid(leaf, 12168da6d581SJan Schmidt dref); 12178da6d581SJan Schmidt key.type = BTRFS_EXTENT_DATA_KEY; 12188da6d581SJan Schmidt key.offset = btrfs_extent_data_ref_offset(leaf, dref); 1219dc046b10SJosef Bacik 1220a0a5472aSFilipe Manana if (sc && key.objectid != sc->inum && 12214fc7b572SFilipe Manana !sc->have_delayed_delete_refs) { 1222dc046b10SJosef Bacik ret = BACKREF_FOUND_SHARED; 1223dc046b10SJosef Bacik break; 1224dc046b10SJosef Bacik } 1225dc046b10SJosef Bacik 12268da6d581SJan Schmidt root = btrfs_extent_data_ref_root(leaf, dref); 1227adf02418SFilipe Manana 1228adf02418SFilipe Manana if (!ctx->skip_data_ref || 1229adf02418SFilipe Manana !ctx->skip_data_ref(root, key.objectid, key.offset, 1230adf02418SFilipe Manana ctx->user_ctx)) 123100142756SJeff Mahoney ret = add_indirect_ref(fs_info, preftrees, root, 1232adf02418SFilipe Manana &key, 0, ctx->bytenr, 1233adf02418SFilipe Manana count, sc, GFP_NOFS); 12348da6d581SJan Schmidt break; 12358da6d581SJan Schmidt } 12368da6d581SJan Schmidt default: 12378da6d581SJan Schmidt WARN_ON(1); 12388da6d581SJan Schmidt } 12391149ab6bSWang Shilong if (ret) 12401149ab6bSWang Shilong return ret; 12411149ab6bSWang Shilong 12428da6d581SJan Schmidt } 12438da6d581SJan Schmidt 12448da6d581SJan Schmidt return ret; 12458da6d581SJan Schmidt } 12468da6d581SJan Schmidt 12478da6d581SJan Schmidt /* 1248583f4ac5SFilipe Manana * The caller has joined a transaction or is holding a read lock on the 1249583f4ac5SFilipe Manana * fs_info->commit_root_sem semaphore, so no need to worry about the root's last 1250583f4ac5SFilipe Manana * snapshot field changing while updating or checking the cache. 1251583f4ac5SFilipe Manana */ 1252583f4ac5SFilipe Manana static bool lookup_backref_shared_cache(struct btrfs_backref_share_check_ctx *ctx, 1253583f4ac5SFilipe Manana struct btrfs_root *root, 1254583f4ac5SFilipe Manana u64 bytenr, int level, bool *is_shared) 1255583f4ac5SFilipe Manana { 12564e4488d4SFilipe Manana const struct btrfs_fs_info *fs_info = root->fs_info; 1257583f4ac5SFilipe Manana struct btrfs_backref_shared_cache_entry *entry; 1258583f4ac5SFilipe Manana 12594e4488d4SFilipe Manana if (!current->journal_info) 12604e4488d4SFilipe Manana lockdep_assert_held(&fs_info->commit_root_sem); 12614e4488d4SFilipe Manana 1262583f4ac5SFilipe Manana if (!ctx->use_path_cache) 1263583f4ac5SFilipe Manana return false; 1264583f4ac5SFilipe Manana 1265583f4ac5SFilipe Manana if (WARN_ON_ONCE(level >= BTRFS_MAX_LEVEL)) 1266583f4ac5SFilipe Manana return false; 1267583f4ac5SFilipe Manana 1268583f4ac5SFilipe Manana /* 1269583f4ac5SFilipe Manana * Level -1 is used for the data extent, which is not reliable to cache 1270583f4ac5SFilipe Manana * because its reference count can increase or decrease without us 1271583f4ac5SFilipe Manana * realizing. We cache results only for extent buffers that lead from 1272583f4ac5SFilipe Manana * the root node down to the leaf with the file extent item. 1273583f4ac5SFilipe Manana */ 1274583f4ac5SFilipe Manana ASSERT(level >= 0); 1275583f4ac5SFilipe Manana 1276583f4ac5SFilipe Manana entry = &ctx->path_cache_entries[level]; 1277583f4ac5SFilipe Manana 1278583f4ac5SFilipe Manana /* Unused cache entry or being used for some other extent buffer. */ 1279583f4ac5SFilipe Manana if (entry->bytenr != bytenr) 1280583f4ac5SFilipe Manana return false; 1281583f4ac5SFilipe Manana 1282583f4ac5SFilipe Manana /* 1283583f4ac5SFilipe Manana * We cached a false result, but the last snapshot generation of the 1284583f4ac5SFilipe Manana * root changed, so we now have a snapshot. Don't trust the result. 1285583f4ac5SFilipe Manana */ 1286583f4ac5SFilipe Manana if (!entry->is_shared && 1287583f4ac5SFilipe Manana entry->gen != btrfs_root_last_snapshot(&root->root_item)) 1288583f4ac5SFilipe Manana return false; 1289583f4ac5SFilipe Manana 1290583f4ac5SFilipe Manana /* 1291583f4ac5SFilipe Manana * If we cached a true result and the last generation used for dropping 1292583f4ac5SFilipe Manana * a root changed, we can not trust the result, because the dropped root 1293583f4ac5SFilipe Manana * could be a snapshot sharing this extent buffer. 1294583f4ac5SFilipe Manana */ 1295583f4ac5SFilipe Manana if (entry->is_shared && 12964e4488d4SFilipe Manana entry->gen != btrfs_get_last_root_drop_gen(fs_info)) 1297583f4ac5SFilipe Manana return false; 1298583f4ac5SFilipe Manana 1299583f4ac5SFilipe Manana *is_shared = entry->is_shared; 1300583f4ac5SFilipe Manana /* 1301583f4ac5SFilipe Manana * If the node at this level is shared, than all nodes below are also 1302583f4ac5SFilipe Manana * shared. Currently some of the nodes below may be marked as not shared 1303583f4ac5SFilipe Manana * because we have just switched from one leaf to another, and switched 1304583f4ac5SFilipe Manana * also other nodes above the leaf and below the current level, so mark 1305583f4ac5SFilipe Manana * them as shared. 1306583f4ac5SFilipe Manana */ 1307583f4ac5SFilipe Manana if (*is_shared) { 1308583f4ac5SFilipe Manana for (int i = 0; i < level; i++) { 1309583f4ac5SFilipe Manana ctx->path_cache_entries[i].is_shared = true; 1310583f4ac5SFilipe Manana ctx->path_cache_entries[i].gen = entry->gen; 1311583f4ac5SFilipe Manana } 1312583f4ac5SFilipe Manana } 1313583f4ac5SFilipe Manana 1314583f4ac5SFilipe Manana return true; 1315583f4ac5SFilipe Manana } 1316583f4ac5SFilipe Manana 1317583f4ac5SFilipe Manana /* 1318583f4ac5SFilipe Manana * The caller has joined a transaction or is holding a read lock on the 1319583f4ac5SFilipe Manana * fs_info->commit_root_sem semaphore, so no need to worry about the root's last 1320583f4ac5SFilipe Manana * snapshot field changing while updating or checking the cache. 1321583f4ac5SFilipe Manana */ 1322583f4ac5SFilipe Manana static void store_backref_shared_cache(struct btrfs_backref_share_check_ctx *ctx, 1323583f4ac5SFilipe Manana struct btrfs_root *root, 1324583f4ac5SFilipe Manana u64 bytenr, int level, bool is_shared) 1325583f4ac5SFilipe Manana { 13264e4488d4SFilipe Manana const struct btrfs_fs_info *fs_info = root->fs_info; 1327583f4ac5SFilipe Manana struct btrfs_backref_shared_cache_entry *entry; 1328583f4ac5SFilipe Manana u64 gen; 1329583f4ac5SFilipe Manana 13304e4488d4SFilipe Manana if (!current->journal_info) 13314e4488d4SFilipe Manana lockdep_assert_held(&fs_info->commit_root_sem); 13324e4488d4SFilipe Manana 1333583f4ac5SFilipe Manana if (!ctx->use_path_cache) 1334583f4ac5SFilipe Manana return; 1335583f4ac5SFilipe Manana 1336583f4ac5SFilipe Manana if (WARN_ON_ONCE(level >= BTRFS_MAX_LEVEL)) 1337583f4ac5SFilipe Manana return; 1338583f4ac5SFilipe Manana 1339583f4ac5SFilipe Manana /* 1340583f4ac5SFilipe Manana * Level -1 is used for the data extent, which is not reliable to cache 1341583f4ac5SFilipe Manana * because its reference count can increase or decrease without us 1342583f4ac5SFilipe Manana * realizing. We cache results only for extent buffers that lead from 1343583f4ac5SFilipe Manana * the root node down to the leaf with the file extent item. 1344583f4ac5SFilipe Manana */ 1345583f4ac5SFilipe Manana ASSERT(level >= 0); 1346583f4ac5SFilipe Manana 1347583f4ac5SFilipe Manana if (is_shared) 13484e4488d4SFilipe Manana gen = btrfs_get_last_root_drop_gen(fs_info); 1349583f4ac5SFilipe Manana else 1350583f4ac5SFilipe Manana gen = btrfs_root_last_snapshot(&root->root_item); 1351583f4ac5SFilipe Manana 1352583f4ac5SFilipe Manana entry = &ctx->path_cache_entries[level]; 1353583f4ac5SFilipe Manana entry->bytenr = bytenr; 1354583f4ac5SFilipe Manana entry->is_shared = is_shared; 1355583f4ac5SFilipe Manana entry->gen = gen; 1356583f4ac5SFilipe Manana 1357583f4ac5SFilipe Manana /* 1358583f4ac5SFilipe Manana * If we found an extent buffer is shared, set the cache result for all 1359583f4ac5SFilipe Manana * extent buffers below it to true. As nodes in the path are COWed, 1360583f4ac5SFilipe Manana * their sharedness is moved to their children, and if a leaf is COWed, 1361583f4ac5SFilipe Manana * then the sharedness of a data extent becomes direct, the refcount of 1362583f4ac5SFilipe Manana * data extent is increased in the extent item at the extent tree. 1363583f4ac5SFilipe Manana */ 1364583f4ac5SFilipe Manana if (is_shared) { 1365583f4ac5SFilipe Manana for (int i = 0; i < level; i++) { 1366583f4ac5SFilipe Manana entry = &ctx->path_cache_entries[i]; 1367583f4ac5SFilipe Manana entry->is_shared = is_shared; 1368583f4ac5SFilipe Manana entry->gen = gen; 1369583f4ac5SFilipe Manana } 1370583f4ac5SFilipe Manana } 1371583f4ac5SFilipe Manana } 1372583f4ac5SFilipe Manana 1373583f4ac5SFilipe Manana /* 13748da6d581SJan Schmidt * this adds all existing backrefs (inline backrefs, backrefs and delayed 13758da6d581SJan Schmidt * refs) for the given bytenr to the refs list, merges duplicates and resolves 13768da6d581SJan Schmidt * indirect refs to their parent bytenr. 13778da6d581SJan Schmidt * When roots are found, they're added to the roots list 13788da6d581SJan Schmidt * 1379a2c8d27eSFilipe Manana * @ctx: Backref walking context object, must be not NULL. 1380a2c8d27eSFilipe Manana * @sc: If !NULL, then immediately return BACKREF_FOUND_SHARED when a 13813ec4d323SEdmund Nadolski * shared extent is detected. 13823ec4d323SEdmund Nadolski * 13833ec4d323SEdmund Nadolski * Otherwise this returns 0 for success and <0 for an error. 13843ec4d323SEdmund Nadolski * 13858da6d581SJan Schmidt * FIXME some caching might speed things up 13868da6d581SJan Schmidt */ 1387a2c8d27eSFilipe Manana static int find_parent_nodes(struct btrfs_backref_walk_ctx *ctx, 13886ce6ba53SFilipe Manana struct share_check *sc) 13898da6d581SJan Schmidt { 1390a2c8d27eSFilipe Manana struct btrfs_root *root = btrfs_extent_root(ctx->fs_info, ctx->bytenr); 13918da6d581SJan Schmidt struct btrfs_key key; 13928da6d581SJan Schmidt struct btrfs_path *path; 13938da6d581SJan Schmidt struct btrfs_delayed_ref_root *delayed_refs = NULL; 1394d3b01064SLi Zefan struct btrfs_delayed_ref_head *head; 13958da6d581SJan Schmidt int info_level = 0; 13968da6d581SJan Schmidt int ret; 1397e0c476b1SJeff Mahoney struct prelim_ref *ref; 139886d5f994SEdmund Nadolski struct rb_node *node; 1399f05c4746SWang Shilong struct extent_inode_elem *eie = NULL; 140086d5f994SEdmund Nadolski struct preftrees preftrees = { 140186d5f994SEdmund Nadolski .direct = PREFTREE_INIT, 140286d5f994SEdmund Nadolski .indirect = PREFTREE_INIT, 140386d5f994SEdmund Nadolski .indirect_missing_keys = PREFTREE_INIT 140486d5f994SEdmund Nadolski }; 14058da6d581SJan Schmidt 140656f5c199SFilipe Manana /* Roots ulist is not needed when using a sharedness check context. */ 140756f5c199SFilipe Manana if (sc) 1408a2c8d27eSFilipe Manana ASSERT(ctx->roots == NULL); 140956f5c199SFilipe Manana 1410a2c8d27eSFilipe Manana key.objectid = ctx->bytenr; 14118da6d581SJan Schmidt key.offset = (u64)-1; 1412a2c8d27eSFilipe Manana if (btrfs_fs_incompat(ctx->fs_info, SKINNY_METADATA)) 1413261c84b6SJosef Bacik key.type = BTRFS_METADATA_ITEM_KEY; 1414261c84b6SJosef Bacik else 1415261c84b6SJosef Bacik key.type = BTRFS_EXTENT_ITEM_KEY; 14168da6d581SJan Schmidt 14178da6d581SJan Schmidt path = btrfs_alloc_path(); 14188da6d581SJan Schmidt if (!path) 14198da6d581SJan Schmidt return -ENOMEM; 1420a2c8d27eSFilipe Manana if (!ctx->trans) { 1421da61d31aSJosef Bacik path->search_commit_root = 1; 1422e84752d4SWang Shilong path->skip_locking = 1; 1423e84752d4SWang Shilong } 14248da6d581SJan Schmidt 1425a2c8d27eSFilipe Manana if (ctx->time_seq == BTRFS_SEQ_LAST) 142621633fc6SQu Wenruo path->skip_locking = 1; 142721633fc6SQu Wenruo 14288da6d581SJan Schmidt again: 1429d3b01064SLi Zefan head = NULL; 1430d3b01064SLi Zefan 143198cc4222SJosef Bacik ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); 14328da6d581SJan Schmidt if (ret < 0) 14338da6d581SJan Schmidt goto out; 1434fcba0120SJosef Bacik if (ret == 0) { 1435fcba0120SJosef Bacik /* This shouldn't happen, indicates a bug or fs corruption. */ 1436fcba0120SJosef Bacik ASSERT(ret != 0); 1437fcba0120SJosef Bacik ret = -EUCLEAN; 1438fcba0120SJosef Bacik goto out; 1439fcba0120SJosef Bacik } 14408da6d581SJan Schmidt 1441a2c8d27eSFilipe Manana if (ctx->trans && likely(ctx->trans->type != __TRANS_DUMMY) && 1442a2c8d27eSFilipe Manana ctx->time_seq != BTRFS_SEQ_LAST) { 14438da6d581SJan Schmidt /* 14449665ebd5SJosef Bacik * We have a specific time_seq we care about and trans which 14459665ebd5SJosef Bacik * means we have the path lock, we need to grab the ref head and 14469665ebd5SJosef Bacik * lock it so we have a consistent view of the refs at the given 14479665ebd5SJosef Bacik * time. 14488da6d581SJan Schmidt */ 1449a2c8d27eSFilipe Manana delayed_refs = &ctx->trans->transaction->delayed_refs; 14508da6d581SJan Schmidt spin_lock(&delayed_refs->lock); 1451a2c8d27eSFilipe Manana head = btrfs_find_delayed_ref_head(delayed_refs, ctx->bytenr); 14528da6d581SJan Schmidt if (head) { 14538da6d581SJan Schmidt if (!mutex_trylock(&head->mutex)) { 1454d278850eSJosef Bacik refcount_inc(&head->refs); 14558da6d581SJan Schmidt spin_unlock(&delayed_refs->lock); 14568da6d581SJan Schmidt 14578da6d581SJan Schmidt btrfs_release_path(path); 14588da6d581SJan Schmidt 14598da6d581SJan Schmidt /* 14608da6d581SJan Schmidt * Mutex was contended, block until it's 14618da6d581SJan Schmidt * released and try again 14628da6d581SJan Schmidt */ 14638da6d581SJan Schmidt mutex_lock(&head->mutex); 14648da6d581SJan Schmidt mutex_unlock(&head->mutex); 1465d278850eSJosef Bacik btrfs_put_delayed_ref_head(head); 14668da6d581SJan Schmidt goto again; 14678da6d581SJan Schmidt } 1468d7df2c79SJosef Bacik spin_unlock(&delayed_refs->lock); 1469a2c8d27eSFilipe Manana ret = add_delayed_refs(ctx->fs_info, head, ctx->time_seq, 1470b25b0b87Sethanwu &preftrees, sc); 1471155725c9SJan Schmidt mutex_unlock(&head->mutex); 1472d7df2c79SJosef Bacik if (ret) 14738da6d581SJan Schmidt goto out; 1474d7df2c79SJosef Bacik } else { 14758da6d581SJan Schmidt spin_unlock(&delayed_refs->lock); 14767a3ae2f8SJan Schmidt } 1477d7df2c79SJosef Bacik } 14788da6d581SJan Schmidt 14798da6d581SJan Schmidt if (path->slots[0]) { 14808da6d581SJan Schmidt struct extent_buffer *leaf; 14818da6d581SJan Schmidt int slot; 14828da6d581SJan Schmidt 1483dadcaf78SJan Schmidt path->slots[0]--; 14848da6d581SJan Schmidt leaf = path->nodes[0]; 1485dadcaf78SJan Schmidt slot = path->slots[0]; 14868da6d581SJan Schmidt btrfs_item_key_to_cpu(leaf, &key, slot); 1487a2c8d27eSFilipe Manana if (key.objectid == ctx->bytenr && 1488261c84b6SJosef Bacik (key.type == BTRFS_EXTENT_ITEM_KEY || 1489261c84b6SJosef Bacik key.type == BTRFS_METADATA_ITEM_KEY)) { 1490f73853c7SFilipe Manana ret = add_inline_refs(ctx, path, &info_level, 1491f73853c7SFilipe Manana &preftrees, sc); 14928da6d581SJan Schmidt if (ret) 14938da6d581SJan Schmidt goto out; 1494adf02418SFilipe Manana ret = add_keyed_refs(ctx, root, path, info_level, 14953ec4d323SEdmund Nadolski &preftrees, sc); 14968da6d581SJan Schmidt if (ret) 14978da6d581SJan Schmidt goto out; 14988da6d581SJan Schmidt } 14998da6d581SJan Schmidt } 150086d5f994SEdmund Nadolski 150156f5c199SFilipe Manana /* 150256f5c199SFilipe Manana * If we have a share context and we reached here, it means the extent 150356f5c199SFilipe Manana * is not directly shared (no multiple reference items for it), 150456f5c199SFilipe Manana * otherwise we would have exited earlier with a return value of 150556f5c199SFilipe Manana * BACKREF_FOUND_SHARED after processing delayed references or while 150656f5c199SFilipe Manana * processing inline or keyed references from the extent tree. 150756f5c199SFilipe Manana * The extent may however be indirectly shared through shared subtrees 150856f5c199SFilipe Manana * as a result from creating snapshots, so we determine below what is 150956f5c199SFilipe Manana * its parent node, in case we are dealing with a metadata extent, or 151056f5c199SFilipe Manana * what's the leaf (or leaves), from a fs tree, that has a file extent 151156f5c199SFilipe Manana * item pointing to it in case we are dealing with a data extent. 151256f5c199SFilipe Manana */ 151356f5c199SFilipe Manana ASSERT(extent_is_shared(sc) == 0); 151456f5c199SFilipe Manana 1515877c1476SFilipe Manana /* 1516877c1476SFilipe Manana * If we are here for a data extent and we have a share_check structure 1517877c1476SFilipe Manana * it means the data extent is not directly shared (does not have 1518877c1476SFilipe Manana * multiple reference items), so we have to check if a path in the fs 1519877c1476SFilipe Manana * tree (going from the root node down to the leaf that has the file 1520877c1476SFilipe Manana * extent item pointing to the data extent) is shared, that is, if any 1521877c1476SFilipe Manana * of the extent buffers in the path is referenced by other trees. 1522877c1476SFilipe Manana */ 1523a2c8d27eSFilipe Manana if (sc && ctx->bytenr == sc->data_bytenr) { 1524877c1476SFilipe Manana /* 15256976201fSFilipe Manana * If our data extent is from a generation more recent than the 15266976201fSFilipe Manana * last generation used to snapshot the root, then we know that 15276976201fSFilipe Manana * it can not be shared through subtrees, so we can skip 15286976201fSFilipe Manana * resolving indirect references, there's no point in 15296976201fSFilipe Manana * determining the extent buffers for the path from the fs tree 15306976201fSFilipe Manana * root node down to the leaf that has the file extent item that 15316976201fSFilipe Manana * points to the data extent. 15326976201fSFilipe Manana */ 15336976201fSFilipe Manana if (sc->data_extent_gen > 15346976201fSFilipe Manana btrfs_root_last_snapshot(&sc->root->root_item)) { 15356976201fSFilipe Manana ret = BACKREF_FOUND_NOT_SHARED; 15366976201fSFilipe Manana goto out; 15376976201fSFilipe Manana } 15386976201fSFilipe Manana 15396976201fSFilipe Manana /* 1540877c1476SFilipe Manana * If we are only determining if a data extent is shared or not 1541877c1476SFilipe Manana * and the corresponding file extent item is located in the same 1542877c1476SFilipe Manana * leaf as the previous file extent item, we can skip resolving 1543877c1476SFilipe Manana * indirect references for a data extent, since the fs tree path 1544877c1476SFilipe Manana * is the same (same leaf, so same path). We skip as long as the 1545877c1476SFilipe Manana * cached result for the leaf is valid and only if there's only 1546877c1476SFilipe Manana * one file extent item pointing to the data extent, because in 1547877c1476SFilipe Manana * the case of multiple file extent items, they may be located 1548877c1476SFilipe Manana * in different leaves and therefore we have multiple paths. 1549877c1476SFilipe Manana */ 1550877c1476SFilipe Manana if (sc->ctx->curr_leaf_bytenr == sc->ctx->prev_leaf_bytenr && 1551877c1476SFilipe Manana sc->self_ref_count == 1) { 1552877c1476SFilipe Manana bool cached; 1553877c1476SFilipe Manana bool is_shared; 1554877c1476SFilipe Manana 1555877c1476SFilipe Manana cached = lookup_backref_shared_cache(sc->ctx, sc->root, 1556877c1476SFilipe Manana sc->ctx->curr_leaf_bytenr, 1557877c1476SFilipe Manana 0, &is_shared); 1558877c1476SFilipe Manana if (cached) { 1559877c1476SFilipe Manana if (is_shared) 1560877c1476SFilipe Manana ret = BACKREF_FOUND_SHARED; 1561877c1476SFilipe Manana else 1562877c1476SFilipe Manana ret = BACKREF_FOUND_NOT_SHARED; 1563877c1476SFilipe Manana goto out; 1564877c1476SFilipe Manana } 1565877c1476SFilipe Manana } 1566877c1476SFilipe Manana } 1567877c1476SFilipe Manana 15688da6d581SJan Schmidt btrfs_release_path(path); 15698da6d581SJan Schmidt 1570a2c8d27eSFilipe Manana ret = add_missing_keys(ctx->fs_info, &preftrees, path->skip_locking == 0); 1571d5c88b73SJan Schmidt if (ret) 1572d5c88b73SJan Schmidt goto out; 1573d5c88b73SJan Schmidt 1574ecf160b4SLiu Bo WARN_ON(!RB_EMPTY_ROOT(&preftrees.indirect_missing_keys.root.rb_root)); 15758da6d581SJan Schmidt 1576a2c8d27eSFilipe Manana ret = resolve_indirect_refs(ctx, path, &preftrees, sc); 15778da6d581SJan Schmidt if (ret) 15788da6d581SJan Schmidt goto out; 15798da6d581SJan Schmidt 1580ecf160b4SLiu Bo WARN_ON(!RB_EMPTY_ROOT(&preftrees.indirect.root.rb_root)); 15818da6d581SJan Schmidt 158286d5f994SEdmund Nadolski /* 158386d5f994SEdmund Nadolski * This walks the tree of merged and resolved refs. Tree blocks are 158486d5f994SEdmund Nadolski * read in as needed. Unique entries are added to the ulist, and 158586d5f994SEdmund Nadolski * the list of found roots is updated. 158686d5f994SEdmund Nadolski * 158786d5f994SEdmund Nadolski * We release the entire tree in one go before returning. 158886d5f994SEdmund Nadolski */ 1589ecf160b4SLiu Bo node = rb_first_cached(&preftrees.direct.root); 159086d5f994SEdmund Nadolski while (node) { 159186d5f994SEdmund Nadolski ref = rb_entry(node, struct prelim_ref, rbnode); 159286d5f994SEdmund Nadolski node = rb_next(&ref->rbnode); 1593c8195a7bSZygo Blaxell /* 1594c8195a7bSZygo Blaxell * ref->count < 0 can happen here if there are delayed 1595c8195a7bSZygo Blaxell * refs with a node->action of BTRFS_DROP_DELAYED_REF. 1596c8195a7bSZygo Blaxell * prelim_ref_insert() relies on this when merging 1597c8195a7bSZygo Blaxell * identical refs to keep the overall count correct. 1598c8195a7bSZygo Blaxell * prelim_ref_insert() will merge only those refs 1599c8195a7bSZygo Blaxell * which compare identically. Any refs having 1600c8195a7bSZygo Blaxell * e.g. different offsets would not be merged, 1601c8195a7bSZygo Blaxell * and would retain their original ref->count < 0. 1602c8195a7bSZygo Blaxell */ 1603a2c8d27eSFilipe Manana if (ctx->roots && ref->count && ref->root_id && ref->parent == 0) { 16048da6d581SJan Schmidt /* no parent == root of tree */ 1605a2c8d27eSFilipe Manana ret = ulist_add(ctx->roots, ref->root_id, 0, GFP_NOFS); 1606f1723939SWang Shilong if (ret < 0) 1607f1723939SWang Shilong goto out; 16088da6d581SJan Schmidt } 16098da6d581SJan Schmidt if (ref->count && ref->parent) { 16100cad8f14SFilipe Manana if (!ctx->skip_inode_ref_list && !ref->inode_list && 1611a2c8d27eSFilipe Manana ref->level == 0) { 1612789d6a3aSQu Wenruo struct btrfs_tree_parent_check check = { 0 }; 1613976b1908SJan Schmidt struct extent_buffer *eb; 1614707e8a07SDavid Sterba 1615789d6a3aSQu Wenruo check.level = ref->level; 1616789d6a3aSQu Wenruo 1617789d6a3aSQu Wenruo eb = read_tree_block(ctx->fs_info, ref->parent, 1618789d6a3aSQu Wenruo &check); 161964c043deSLiu Bo if (IS_ERR(eb)) { 162064c043deSLiu Bo ret = PTR_ERR(eb); 162164c043deSLiu Bo goto out; 16224eb150d6SQu Wenruo } 16234eb150d6SQu Wenruo if (!extent_buffer_uptodate(eb)) { 1624416bc658SJosef Bacik free_extent_buffer(eb); 1625c16c2e2eSWang Shilong ret = -EIO; 1626c16c2e2eSWang Shilong goto out; 1627416bc658SJosef Bacik } 162838e3eebfSJosef Bacik 1629ac5887c8SJosef Bacik if (!path->skip_locking) 16306f7ff6d7SFilipe Manana btrfs_tree_read_lock(eb); 163188ffb665SFilipe Manana ret = find_extent_in_eb(ctx, eb, &eie); 163238e3eebfSJosef Bacik if (!path->skip_locking) 1633ac5887c8SJosef Bacik btrfs_tree_read_unlock(eb); 1634976b1908SJan Schmidt free_extent_buffer(eb); 163588ffb665SFilipe Manana if (ret == BTRFS_ITERATE_EXTENT_INODES_STOP || 163688ffb665SFilipe Manana ret < 0) 1637f5929cd8SFilipe David Borba Manana goto out; 1638f5929cd8SFilipe David Borba Manana ref->inode_list = eie; 163992876eecSFilipe Manana /* 164092876eecSFilipe Manana * We transferred the list ownership to the ref, 164192876eecSFilipe Manana * so set to NULL to avoid a double free in case 164292876eecSFilipe Manana * an error happens after this. 164392876eecSFilipe Manana */ 164492876eecSFilipe Manana eie = NULL; 1645976b1908SJan Schmidt } 1646a2c8d27eSFilipe Manana ret = ulist_add_merge_ptr(ctx->refs, ref->parent, 16474eb1f66dSTakashi Iwai ref->inode_list, 16484eb1f66dSTakashi Iwai (void **)&eie, GFP_NOFS); 1649f1723939SWang Shilong if (ret < 0) 1650f1723939SWang Shilong goto out; 16510cad8f14SFilipe Manana if (!ret && !ctx->skip_inode_ref_list) { 16523301958bSJan Schmidt /* 16539f05c09dSJosef Bacik * We've recorded that parent, so we must extend 16549f05c09dSJosef Bacik * its inode list here. 16559f05c09dSJosef Bacik * 16569f05c09dSJosef Bacik * However if there was corruption we may not 16579f05c09dSJosef Bacik * have found an eie, return an error in this 16589f05c09dSJosef Bacik * case. 16593301958bSJan Schmidt */ 16609f05c09dSJosef Bacik ASSERT(eie); 16619f05c09dSJosef Bacik if (!eie) { 16629f05c09dSJosef Bacik ret = -EUCLEAN; 16639f05c09dSJosef Bacik goto out; 16649f05c09dSJosef Bacik } 16653301958bSJan Schmidt while (eie->next) 16663301958bSJan Schmidt eie = eie->next; 16673301958bSJan Schmidt eie->next = ref->inode_list; 16683301958bSJan Schmidt } 1669f05c4746SWang Shilong eie = NULL; 167092876eecSFilipe Manana /* 167192876eecSFilipe Manana * We have transferred the inode list ownership from 167292876eecSFilipe Manana * this ref to the ref we added to the 'refs' ulist. 167392876eecSFilipe Manana * So set this ref's inode list to NULL to avoid 167492876eecSFilipe Manana * use-after-free when our caller uses it or double 167592876eecSFilipe Manana * frees in case an error happens before we return. 167692876eecSFilipe Manana */ 167792876eecSFilipe Manana ref->inode_list = NULL; 16788da6d581SJan Schmidt } 16799dd14fd6SEdmund Nadolski cond_resched(); 16808da6d581SJan Schmidt } 16818da6d581SJan Schmidt 16828da6d581SJan Schmidt out: 16838da6d581SJan Schmidt btrfs_free_path(path); 168486d5f994SEdmund Nadolski 168586d5f994SEdmund Nadolski prelim_release(&preftrees.direct); 168686d5f994SEdmund Nadolski prelim_release(&preftrees.indirect); 168786d5f994SEdmund Nadolski prelim_release(&preftrees.indirect_missing_keys); 168886d5f994SEdmund Nadolski 168988ffb665SFilipe Manana if (ret == BTRFS_ITERATE_EXTENT_INODES_STOP || ret < 0) 1690f05c4746SWang Shilong free_inode_elem_list(eie); 16918da6d581SJan Schmidt return ret; 16928da6d581SJan Schmidt } 16938da6d581SJan Schmidt 16948da6d581SJan Schmidt /* 1695a2c8d27eSFilipe Manana * Finds all leaves with a reference to the specified combination of 1696a2c8d27eSFilipe Manana * @ctx->bytenr and @ctx->extent_item_pos. The bytenr of the found leaves are 1697a2c8d27eSFilipe Manana * added to the ulist at @ctx->refs, and that ulist is allocated by this 1698a2c8d27eSFilipe Manana * function. The caller should free the ulist with free_leaf_list() if 1699a2c8d27eSFilipe Manana * @ctx->ignore_extent_item_pos is false, otherwise a fimple ulist_free() is 1700a2c8d27eSFilipe Manana * enough. 17018da6d581SJan Schmidt * 1702a2c8d27eSFilipe Manana * Returns 0 on success and < 0 on error. On error @ctx->refs is not allocated. 17038da6d581SJan Schmidt */ 1704a2c8d27eSFilipe Manana int btrfs_find_all_leafs(struct btrfs_backref_walk_ctx *ctx) 17058da6d581SJan Schmidt { 17068da6d581SJan Schmidt int ret; 17078da6d581SJan Schmidt 1708a2c8d27eSFilipe Manana ASSERT(ctx->refs == NULL); 1709a2c8d27eSFilipe Manana 1710a2c8d27eSFilipe Manana ctx->refs = ulist_alloc(GFP_NOFS); 1711a2c8d27eSFilipe Manana if (!ctx->refs) 17128da6d581SJan Schmidt return -ENOMEM; 17138da6d581SJan Schmidt 1714a2c8d27eSFilipe Manana ret = find_parent_nodes(ctx, NULL); 171588ffb665SFilipe Manana if (ret == BTRFS_ITERATE_EXTENT_INODES_STOP || 171688ffb665SFilipe Manana (ret < 0 && ret != -ENOENT)) { 1717a2c8d27eSFilipe Manana free_leaf_list(ctx->refs); 1718a2c8d27eSFilipe Manana ctx->refs = NULL; 17198da6d581SJan Schmidt return ret; 17208da6d581SJan Schmidt } 17218da6d581SJan Schmidt 17228da6d581SJan Schmidt return 0; 17238da6d581SJan Schmidt } 17248da6d581SJan Schmidt 17258da6d581SJan Schmidt /* 1726a2c8d27eSFilipe Manana * Walk all backrefs for a given extent to find all roots that reference this 17278da6d581SJan Schmidt * extent. Walking a backref means finding all extents that reference this 17288da6d581SJan Schmidt * extent and in turn walk the backrefs of those, too. Naturally this is a 17298da6d581SJan Schmidt * recursive process, but here it is implemented in an iterative fashion: We 17308da6d581SJan Schmidt * find all referencing extents for the extent in question and put them on a 17318da6d581SJan Schmidt * list. In turn, we find all referencing extents for those, further appending 17328da6d581SJan Schmidt * to the list. The way we iterate the list allows adding more elements after 17338da6d581SJan Schmidt * the current while iterating. The process stops when we reach the end of the 1734a2c8d27eSFilipe Manana * list. 17358da6d581SJan Schmidt * 17361baea6f1SFilipe Manana * Found roots are added to @ctx->roots, which is allocated by this function if 17371baea6f1SFilipe Manana * it points to NULL, in which case the caller is responsible for freeing it 17381baea6f1SFilipe Manana * after it's not needed anymore. 17391baea6f1SFilipe Manana * This function requires @ctx->refs to be NULL, as it uses it for allocating a 17401baea6f1SFilipe Manana * ulist to do temporary work, and frees it before returning. 1741a2c8d27eSFilipe Manana * 17421baea6f1SFilipe Manana * Returns 0 on success, < 0 on error. 17438da6d581SJan Schmidt */ 1744a2c8d27eSFilipe Manana static int btrfs_find_all_roots_safe(struct btrfs_backref_walk_ctx *ctx) 17458da6d581SJan Schmidt { 1746a2c8d27eSFilipe Manana const u64 orig_bytenr = ctx->bytenr; 17470cad8f14SFilipe Manana const bool orig_skip_inode_ref_list = ctx->skip_inode_ref_list; 17481baea6f1SFilipe Manana bool roots_ulist_allocated = false; 1749cd1b413cSJan Schmidt struct ulist_iterator uiter; 1750a2c8d27eSFilipe Manana int ret = 0; 17518da6d581SJan Schmidt 1752a2c8d27eSFilipe Manana ASSERT(ctx->refs == NULL); 1753a2c8d27eSFilipe Manana 1754a2c8d27eSFilipe Manana ctx->refs = ulist_alloc(GFP_NOFS); 1755a2c8d27eSFilipe Manana if (!ctx->refs) 17568da6d581SJan Schmidt return -ENOMEM; 1757a2c8d27eSFilipe Manana 17581baea6f1SFilipe Manana if (!ctx->roots) { 1759a2c8d27eSFilipe Manana ctx->roots = ulist_alloc(GFP_NOFS); 1760a2c8d27eSFilipe Manana if (!ctx->roots) { 1761a2c8d27eSFilipe Manana ulist_free(ctx->refs); 1762a2c8d27eSFilipe Manana ctx->refs = NULL; 17638da6d581SJan Schmidt return -ENOMEM; 17648da6d581SJan Schmidt } 17651baea6f1SFilipe Manana roots_ulist_allocated = true; 17661baea6f1SFilipe Manana } 17678da6d581SJan Schmidt 17680cad8f14SFilipe Manana ctx->skip_inode_ref_list = true; 1769a2c8d27eSFilipe Manana 1770cd1b413cSJan Schmidt ULIST_ITER_INIT(&uiter); 17718da6d581SJan Schmidt while (1) { 1772a2c8d27eSFilipe Manana struct ulist_node *node; 1773a2c8d27eSFilipe Manana 1774a2c8d27eSFilipe Manana ret = find_parent_nodes(ctx, NULL); 17758da6d581SJan Schmidt if (ret < 0 && ret != -ENOENT) { 17761baea6f1SFilipe Manana if (roots_ulist_allocated) { 1777a2c8d27eSFilipe Manana ulist_free(ctx->roots); 1778a2c8d27eSFilipe Manana ctx->roots = NULL; 17791baea6f1SFilipe Manana } 1780a2c8d27eSFilipe Manana break; 17818da6d581SJan Schmidt } 1782a2c8d27eSFilipe Manana ret = 0; 1783a2c8d27eSFilipe Manana node = ulist_next(ctx->refs, &uiter); 17848da6d581SJan Schmidt if (!node) 17858da6d581SJan Schmidt break; 1786a2c8d27eSFilipe Manana ctx->bytenr = node->val; 1787bca1a290SWang Shilong cond_resched(); 17888da6d581SJan Schmidt } 17898da6d581SJan Schmidt 1790a2c8d27eSFilipe Manana ulist_free(ctx->refs); 1791a2c8d27eSFilipe Manana ctx->refs = NULL; 1792a2c8d27eSFilipe Manana ctx->bytenr = orig_bytenr; 17930cad8f14SFilipe Manana ctx->skip_inode_ref_list = orig_skip_inode_ref_list; 1794a2c8d27eSFilipe Manana 1795a2c8d27eSFilipe Manana return ret; 17968da6d581SJan Schmidt } 17978da6d581SJan Schmidt 1798a2c8d27eSFilipe Manana int btrfs_find_all_roots(struct btrfs_backref_walk_ctx *ctx, 1799c7bcbb21SFilipe Manana bool skip_commit_root_sem) 18009e351cc8SJosef Bacik { 18019e351cc8SJosef Bacik int ret; 18029e351cc8SJosef Bacik 1803a2c8d27eSFilipe Manana if (!ctx->trans && !skip_commit_root_sem) 1804a2c8d27eSFilipe Manana down_read(&ctx->fs_info->commit_root_sem); 1805a2c8d27eSFilipe Manana ret = btrfs_find_all_roots_safe(ctx); 1806a2c8d27eSFilipe Manana if (!ctx->trans && !skip_commit_root_sem) 1807a2c8d27eSFilipe Manana up_read(&ctx->fs_info->commit_root_sem); 18089e351cc8SJosef Bacik return ret; 18099e351cc8SJosef Bacik } 18109e351cc8SJosef Bacik 181184a7949dSFilipe Manana struct btrfs_backref_share_check_ctx *btrfs_alloc_backref_share_check_ctx(void) 181284a7949dSFilipe Manana { 181384a7949dSFilipe Manana struct btrfs_backref_share_check_ctx *ctx; 181484a7949dSFilipe Manana 181584a7949dSFilipe Manana ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); 181684a7949dSFilipe Manana if (!ctx) 181784a7949dSFilipe Manana return NULL; 181884a7949dSFilipe Manana 181984a7949dSFilipe Manana ulist_init(&ctx->refs); 182084a7949dSFilipe Manana 182184a7949dSFilipe Manana return ctx; 182284a7949dSFilipe Manana } 182384a7949dSFilipe Manana 182484a7949dSFilipe Manana void btrfs_free_backref_share_ctx(struct btrfs_backref_share_check_ctx *ctx) 182584a7949dSFilipe Manana { 182684a7949dSFilipe Manana if (!ctx) 182784a7949dSFilipe Manana return; 182884a7949dSFilipe Manana 182984a7949dSFilipe Manana ulist_release(&ctx->refs); 183084a7949dSFilipe Manana kfree(ctx); 183184a7949dSFilipe Manana } 183284a7949dSFilipe Manana 183312a824dcSFilipe Manana /* 18348eedaddaSFilipe Manana * Check if a data extent is shared or not. 18356e353e3bSNikolay Borisov * 1836ceb707daSFilipe Manana * @inode: The inode whose extent we are checking. 1837b8f164e3SFilipe Manana * @bytenr: Logical bytenr of the extent we are checking. 1838b8f164e3SFilipe Manana * @extent_gen: Generation of the extent (file extent item) or 0 if it is 1839b8f164e3SFilipe Manana * not known. 184061dbb952SFilipe Manana * @ctx: A backref sharedness check context. 18412c2ed5aaSMark Fasheh * 18428eedaddaSFilipe Manana * btrfs_is_data_extent_shared uses the backref walking code but will short 18432c2ed5aaSMark Fasheh * circuit as soon as it finds a root or inode that doesn't match the 18442c2ed5aaSMark Fasheh * one passed in. This provides a significant performance benefit for 18452c2ed5aaSMark Fasheh * callers (such as fiemap) which want to know whether the extent is 18462c2ed5aaSMark Fasheh * shared but do not need a ref count. 18472c2ed5aaSMark Fasheh * 184803628cdbSFilipe Manana * This attempts to attach to the running transaction in order to account for 184903628cdbSFilipe Manana * delayed refs, but continues on even when no running transaction exists. 1850bb739cf0SEdmund Nadolski * 18512c2ed5aaSMark Fasheh * Return: 0 if extent is not shared, 1 if it is shared, < 0 on error. 18522c2ed5aaSMark Fasheh */ 1853ceb707daSFilipe Manana int btrfs_is_data_extent_shared(struct btrfs_inode *inode, u64 bytenr, 1854b8f164e3SFilipe Manana u64 extent_gen, 185561dbb952SFilipe Manana struct btrfs_backref_share_check_ctx *ctx) 1856dc046b10SJosef Bacik { 1857a2c8d27eSFilipe Manana struct btrfs_backref_walk_ctx walk_ctx = { 0 }; 1858ceb707daSFilipe Manana struct btrfs_root *root = inode->root; 1859bb739cf0SEdmund Nadolski struct btrfs_fs_info *fs_info = root->fs_info; 1860bb739cf0SEdmund Nadolski struct btrfs_trans_handle *trans; 1861dc046b10SJosef Bacik struct ulist_iterator uiter; 1862dc046b10SJosef Bacik struct ulist_node *node; 1863f3a84ccdSFilipe Manana struct btrfs_seq_list elem = BTRFS_SEQ_LIST_INIT(elem); 1864dc046b10SJosef Bacik int ret = 0; 18653ec4d323SEdmund Nadolski struct share_check shared = { 1866877c1476SFilipe Manana .ctx = ctx, 1867877c1476SFilipe Manana .root = root, 1868ceb707daSFilipe Manana .inum = btrfs_ino(inode), 186973e339e6SFilipe Manana .data_bytenr = bytenr, 18706976201fSFilipe Manana .data_extent_gen = extent_gen, 18713ec4d323SEdmund Nadolski .share_count = 0, 187273e339e6SFilipe Manana .self_ref_count = 0, 18734fc7b572SFilipe Manana .have_delayed_delete_refs = false, 18743ec4d323SEdmund Nadolski }; 187512a824dcSFilipe Manana int level; 1876e2fd8306SFilipe Manana bool leaf_cached; 1877e2fd8306SFilipe Manana bool leaf_is_shared; 1878dc046b10SJosef Bacik 187973e339e6SFilipe Manana for (int i = 0; i < BTRFS_BACKREF_CTX_PREV_EXTENTS_SIZE; i++) { 188073e339e6SFilipe Manana if (ctx->prev_extents_cache[i].bytenr == bytenr) 188173e339e6SFilipe Manana return ctx->prev_extents_cache[i].is_shared; 188273e339e6SFilipe Manana } 188373e339e6SFilipe Manana 188484a7949dSFilipe Manana ulist_init(&ctx->refs); 1885dc046b10SJosef Bacik 1886a6d155d2SFilipe Manana trans = btrfs_join_transaction_nostart(root); 1887bb739cf0SEdmund Nadolski if (IS_ERR(trans)) { 188803628cdbSFilipe Manana if (PTR_ERR(trans) != -ENOENT && PTR_ERR(trans) != -EROFS) { 188903628cdbSFilipe Manana ret = PTR_ERR(trans); 189003628cdbSFilipe Manana goto out; 189103628cdbSFilipe Manana } 1892bb739cf0SEdmund Nadolski trans = NULL; 1893dc046b10SJosef Bacik down_read(&fs_info->commit_root_sem); 1894bb739cf0SEdmund Nadolski } else { 1895bb739cf0SEdmund Nadolski btrfs_get_tree_mod_seq(fs_info, &elem); 1896a2c8d27eSFilipe Manana walk_ctx.time_seq = elem.seq; 1897bb739cf0SEdmund Nadolski } 1898bb739cf0SEdmund Nadolski 1899e2fd8306SFilipe Manana ctx->use_path_cache = true; 1900e2fd8306SFilipe Manana 1901e2fd8306SFilipe Manana /* 1902e2fd8306SFilipe Manana * We may have previously determined that the current leaf is shared. 1903e2fd8306SFilipe Manana * If it is, then we have a data extent that is shared due to a shared 1904e2fd8306SFilipe Manana * subtree (caused by snapshotting) and we don't need to check for data 1905e2fd8306SFilipe Manana * backrefs. If the leaf is not shared, then we must do backref walking 1906e2fd8306SFilipe Manana * to determine if the data extent is shared through reflinks. 1907e2fd8306SFilipe Manana */ 1908e2fd8306SFilipe Manana leaf_cached = lookup_backref_shared_cache(ctx, root, 1909e2fd8306SFilipe Manana ctx->curr_leaf_bytenr, 0, 1910e2fd8306SFilipe Manana &leaf_is_shared); 1911e2fd8306SFilipe Manana if (leaf_cached && leaf_is_shared) { 1912e2fd8306SFilipe Manana ret = 1; 1913e2fd8306SFilipe Manana goto out_trans; 1914e2fd8306SFilipe Manana } 1915e2fd8306SFilipe Manana 19160cad8f14SFilipe Manana walk_ctx.skip_inode_ref_list = true; 1917a2c8d27eSFilipe Manana walk_ctx.trans = trans; 1918a2c8d27eSFilipe Manana walk_ctx.fs_info = fs_info; 1919a2c8d27eSFilipe Manana walk_ctx.refs = &ctx->refs; 1920a2c8d27eSFilipe Manana 192112a824dcSFilipe Manana /* -1 means we are in the bytenr of the data extent. */ 192212a824dcSFilipe Manana level = -1; 1923dc046b10SJosef Bacik ULIST_ITER_INIT(&uiter); 1924dc046b10SJosef Bacik while (1) { 19252280d425SFilipe Manana const unsigned long prev_ref_count = ctx->refs.nnodes; 192612a824dcSFilipe Manana 1927a2c8d27eSFilipe Manana walk_ctx.bytenr = bytenr; 1928a2c8d27eSFilipe Manana ret = find_parent_nodes(&walk_ctx, &shared); 1929877c1476SFilipe Manana if (ret == BACKREF_FOUND_SHARED || 1930877c1476SFilipe Manana ret == BACKREF_FOUND_NOT_SHARED) { 1931877c1476SFilipe Manana /* If shared must return 1, otherwise return 0. */ 1932877c1476SFilipe Manana ret = (ret == BACKREF_FOUND_SHARED) ? 1 : 0; 193312a824dcSFilipe Manana if (level >= 0) 193461dbb952SFilipe Manana store_backref_shared_cache(ctx, root, bytenr, 1935877c1476SFilipe Manana level, ret == 1); 1936dc046b10SJosef Bacik break; 1937dc046b10SJosef Bacik } 1938dc046b10SJosef Bacik if (ret < 0 && ret != -ENOENT) 1939dc046b10SJosef Bacik break; 19402c2ed5aaSMark Fasheh ret = 0; 1941b8f164e3SFilipe Manana 194263c84b46SFilipe Manana /* 19432280d425SFilipe Manana * More than one extent buffer (bytenr) may have been added to 19442280d425SFilipe Manana * the ctx->refs ulist, in which case we have to check multiple 19452280d425SFilipe Manana * tree paths in case the first one is not shared, so we can not 19462280d425SFilipe Manana * use the path cache which is made for a single path. Multiple 19472280d425SFilipe Manana * extent buffers at the current level happen when: 19482280d425SFilipe Manana * 19492280d425SFilipe Manana * 1) level -1, the data extent: If our data extent was not 19502280d425SFilipe Manana * directly shared (without multiple reference items), then 19512280d425SFilipe Manana * it might have a single reference item with a count > 1 for 19522280d425SFilipe Manana * the same offset, which means there are 2 (or more) file 19532280d425SFilipe Manana * extent items that point to the data extent - this happens 19542280d425SFilipe Manana * when a file extent item needs to be split and then one 19552280d425SFilipe Manana * item gets moved to another leaf due to a b+tree leaf split 19562280d425SFilipe Manana * when inserting some item. In this case the file extent 19572280d425SFilipe Manana * items may be located in different leaves and therefore 19582280d425SFilipe Manana * some of the leaves may be referenced through shared 19592280d425SFilipe Manana * subtrees while others are not. Since our extent buffer 19602280d425SFilipe Manana * cache only works for a single path (by far the most common 19612280d425SFilipe Manana * case and simpler to deal with), we can not use it if we 19622280d425SFilipe Manana * have multiple leaves (which implies multiple paths). 19632280d425SFilipe Manana * 19642280d425SFilipe Manana * 2) level >= 0, a tree node/leaf: We can have a mix of direct 19652280d425SFilipe Manana * and indirect references on a b+tree node/leaf, so we have 19662280d425SFilipe Manana * to check multiple paths, and the extent buffer (the 19672280d425SFilipe Manana * current bytenr) may be shared or not. One example is 19682280d425SFilipe Manana * during relocation as we may get a shared tree block ref 19692280d425SFilipe Manana * (direct ref) and a non-shared tree block ref (indirect 19702280d425SFilipe Manana * ref) for the same node/leaf. 197163c84b46SFilipe Manana */ 19722280d425SFilipe Manana if ((ctx->refs.nnodes - prev_ref_count) > 1) 197361dbb952SFilipe Manana ctx->use_path_cache = false; 197463c84b46SFilipe Manana 197512a824dcSFilipe Manana if (level >= 0) 197661dbb952SFilipe Manana store_backref_shared_cache(ctx, root, bytenr, 197712a824dcSFilipe Manana level, false); 197884a7949dSFilipe Manana node = ulist_next(&ctx->refs, &uiter); 1979dc046b10SJosef Bacik if (!node) 1980dc046b10SJosef Bacik break; 1981dc046b10SJosef Bacik bytenr = node->val; 19822280d425SFilipe Manana if (ctx->use_path_cache) { 19832280d425SFilipe Manana bool is_shared; 19842280d425SFilipe Manana bool cached; 19852280d425SFilipe Manana 198612a824dcSFilipe Manana level++; 19872280d425SFilipe Manana cached = lookup_backref_shared_cache(ctx, root, bytenr, 19882280d425SFilipe Manana level, &is_shared); 198912a824dcSFilipe Manana if (cached) { 199012a824dcSFilipe Manana ret = (is_shared ? 1 : 0); 199112a824dcSFilipe Manana break; 199212a824dcSFilipe Manana } 19932280d425SFilipe Manana } 199418bf591bSEdmund Nadolski shared.share_count = 0; 19954fc7b572SFilipe Manana shared.have_delayed_delete_refs = false; 1996dc046b10SJosef Bacik cond_resched(); 1997dc046b10SJosef Bacik } 1998bb739cf0SEdmund Nadolski 199973e339e6SFilipe Manana /* 20002280d425SFilipe Manana * If the path cache is disabled, then it means at some tree level we 20012280d425SFilipe Manana * got multiple parents due to a mix of direct and indirect backrefs or 20022280d425SFilipe Manana * multiple leaves with file extent items pointing to the same data 20032280d425SFilipe Manana * extent. We have to invalidate the cache and cache only the sharedness 20042280d425SFilipe Manana * result for the levels where we got only one node/reference. 20052280d425SFilipe Manana */ 20062280d425SFilipe Manana if (!ctx->use_path_cache) { 20072280d425SFilipe Manana int i = 0; 20082280d425SFilipe Manana 20092280d425SFilipe Manana level--; 20102280d425SFilipe Manana if (ret >= 0 && level >= 0) { 20112280d425SFilipe Manana bytenr = ctx->path_cache_entries[level].bytenr; 20122280d425SFilipe Manana ctx->use_path_cache = true; 20132280d425SFilipe Manana store_backref_shared_cache(ctx, root, bytenr, level, ret); 20142280d425SFilipe Manana i = level + 1; 20152280d425SFilipe Manana } 20162280d425SFilipe Manana 20172280d425SFilipe Manana for ( ; i < BTRFS_MAX_LEVEL; i++) 20182280d425SFilipe Manana ctx->path_cache_entries[i].bytenr = 0; 20192280d425SFilipe Manana } 20202280d425SFilipe Manana 20212280d425SFilipe Manana /* 202273e339e6SFilipe Manana * Cache the sharedness result for the data extent if we know our inode 202373e339e6SFilipe Manana * has more than 1 file extent item that refers to the data extent. 202473e339e6SFilipe Manana */ 202573e339e6SFilipe Manana if (ret >= 0 && shared.self_ref_count > 1) { 202673e339e6SFilipe Manana int slot = ctx->prev_extents_cache_slot; 202773e339e6SFilipe Manana 202873e339e6SFilipe Manana ctx->prev_extents_cache[slot].bytenr = shared.data_bytenr; 202973e339e6SFilipe Manana ctx->prev_extents_cache[slot].is_shared = (ret == 1); 203073e339e6SFilipe Manana 203173e339e6SFilipe Manana slot = (slot + 1) % BTRFS_BACKREF_CTX_PREV_EXTENTS_SIZE; 203273e339e6SFilipe Manana ctx->prev_extents_cache_slot = slot; 203373e339e6SFilipe Manana } 203473e339e6SFilipe Manana 2035e2fd8306SFilipe Manana out_trans: 2036bb739cf0SEdmund Nadolski if (trans) { 2037dc046b10SJosef Bacik btrfs_put_tree_mod_seq(fs_info, &elem); 2038bb739cf0SEdmund Nadolski btrfs_end_transaction(trans); 2039bb739cf0SEdmund Nadolski } else { 2040dc046b10SJosef Bacik up_read(&fs_info->commit_root_sem); 2041bb739cf0SEdmund Nadolski } 204203628cdbSFilipe Manana out: 204384a7949dSFilipe Manana ulist_release(&ctx->refs); 2044877c1476SFilipe Manana ctx->prev_leaf_bytenr = ctx->curr_leaf_bytenr; 2045877c1476SFilipe Manana 2046dc046b10SJosef Bacik return ret; 2047dc046b10SJosef Bacik } 2048dc046b10SJosef Bacik 2049f186373fSMark Fasheh int btrfs_find_one_extref(struct btrfs_root *root, u64 inode_objectid, 2050f186373fSMark Fasheh u64 start_off, struct btrfs_path *path, 2051f186373fSMark Fasheh struct btrfs_inode_extref **ret_extref, 2052f186373fSMark Fasheh u64 *found_off) 2053f186373fSMark Fasheh { 2054f186373fSMark Fasheh int ret, slot; 2055f186373fSMark Fasheh struct btrfs_key key; 2056f186373fSMark Fasheh struct btrfs_key found_key; 2057f186373fSMark Fasheh struct btrfs_inode_extref *extref; 205873980becSJeff Mahoney const struct extent_buffer *leaf; 2059f186373fSMark Fasheh unsigned long ptr; 2060f186373fSMark Fasheh 2061f186373fSMark Fasheh key.objectid = inode_objectid; 2062962a298fSDavid Sterba key.type = BTRFS_INODE_EXTREF_KEY; 2063f186373fSMark Fasheh key.offset = start_off; 2064f186373fSMark Fasheh 2065f186373fSMark Fasheh ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); 2066f186373fSMark Fasheh if (ret < 0) 2067f186373fSMark Fasheh return ret; 2068f186373fSMark Fasheh 2069f186373fSMark Fasheh while (1) { 2070f186373fSMark Fasheh leaf = path->nodes[0]; 2071f186373fSMark Fasheh slot = path->slots[0]; 2072f186373fSMark Fasheh if (slot >= btrfs_header_nritems(leaf)) { 2073f186373fSMark Fasheh /* 2074f186373fSMark Fasheh * If the item at offset is not found, 2075f186373fSMark Fasheh * btrfs_search_slot will point us to the slot 2076f186373fSMark Fasheh * where it should be inserted. In our case 2077f186373fSMark Fasheh * that will be the slot directly before the 2078f186373fSMark Fasheh * next INODE_REF_KEY_V2 item. In the case 2079f186373fSMark Fasheh * that we're pointing to the last slot in a 2080f186373fSMark Fasheh * leaf, we must move one leaf over. 2081f186373fSMark Fasheh */ 2082f186373fSMark Fasheh ret = btrfs_next_leaf(root, path); 2083f186373fSMark Fasheh if (ret) { 2084f186373fSMark Fasheh if (ret >= 1) 2085f186373fSMark Fasheh ret = -ENOENT; 2086f186373fSMark Fasheh break; 2087f186373fSMark Fasheh } 2088f186373fSMark Fasheh continue; 2089f186373fSMark Fasheh } 2090f186373fSMark Fasheh 2091f186373fSMark Fasheh btrfs_item_key_to_cpu(leaf, &found_key, slot); 2092f186373fSMark Fasheh 2093f186373fSMark Fasheh /* 2094f186373fSMark Fasheh * Check that we're still looking at an extended ref key for 2095f186373fSMark Fasheh * this particular objectid. If we have different 2096f186373fSMark Fasheh * objectid or type then there are no more to be found 2097f186373fSMark Fasheh * in the tree and we can exit. 2098f186373fSMark Fasheh */ 2099f186373fSMark Fasheh ret = -ENOENT; 2100f186373fSMark Fasheh if (found_key.objectid != inode_objectid) 2101f186373fSMark Fasheh break; 2102962a298fSDavid Sterba if (found_key.type != BTRFS_INODE_EXTREF_KEY) 2103f186373fSMark Fasheh break; 2104f186373fSMark Fasheh 2105f186373fSMark Fasheh ret = 0; 2106f186373fSMark Fasheh ptr = btrfs_item_ptr_offset(leaf, path->slots[0]); 2107f186373fSMark Fasheh extref = (struct btrfs_inode_extref *)ptr; 2108f186373fSMark Fasheh *ret_extref = extref; 2109f186373fSMark Fasheh if (found_off) 2110f186373fSMark Fasheh *found_off = found_key.offset; 2111f186373fSMark Fasheh break; 2112f186373fSMark Fasheh } 2113f186373fSMark Fasheh 2114f186373fSMark Fasheh return ret; 2115f186373fSMark Fasheh } 2116f186373fSMark Fasheh 211748a3b636SEric Sandeen /* 211848a3b636SEric Sandeen * this iterates to turn a name (from iref/extref) into a full filesystem path. 211948a3b636SEric Sandeen * Elements of the path are separated by '/' and the path is guaranteed to be 212048a3b636SEric Sandeen * 0-terminated. the path is only given within the current file system. 212148a3b636SEric Sandeen * Therefore, it never starts with a '/'. the caller is responsible to provide 212248a3b636SEric Sandeen * "size" bytes in "dest". the dest buffer will be filled backwards. finally, 212348a3b636SEric Sandeen * the start point of the resulting string is returned. this pointer is within 212448a3b636SEric Sandeen * dest, normally. 212548a3b636SEric Sandeen * in case the path buffer would overflow, the pointer is decremented further 212648a3b636SEric Sandeen * as if output was written to the buffer, though no more output is actually 212748a3b636SEric Sandeen * generated. that way, the caller can determine how much space would be 212848a3b636SEric Sandeen * required for the path to fit into the buffer. in that case, the returned 212948a3b636SEric Sandeen * value will be smaller than dest. callers must check this! 213048a3b636SEric Sandeen */ 213196b5bd77SJan Schmidt char *btrfs_ref_to_path(struct btrfs_root *fs_root, struct btrfs_path *path, 2132d24bec3aSMark Fasheh u32 name_len, unsigned long name_off, 2133a542ad1bSJan Schmidt struct extent_buffer *eb_in, u64 parent, 2134a542ad1bSJan Schmidt char *dest, u32 size) 2135a542ad1bSJan Schmidt { 2136a542ad1bSJan Schmidt int slot; 2137a542ad1bSJan Schmidt u64 next_inum; 2138a542ad1bSJan Schmidt int ret; 2139661bec6bSGabriel de Perthuis s64 bytes_left = ((s64)size) - 1; 2140a542ad1bSJan Schmidt struct extent_buffer *eb = eb_in; 2141a542ad1bSJan Schmidt struct btrfs_key found_key; 2142d24bec3aSMark Fasheh struct btrfs_inode_ref *iref; 2143a542ad1bSJan Schmidt 2144a542ad1bSJan Schmidt if (bytes_left >= 0) 2145a542ad1bSJan Schmidt dest[bytes_left] = '\0'; 2146a542ad1bSJan Schmidt 2147a542ad1bSJan Schmidt while (1) { 2148d24bec3aSMark Fasheh bytes_left -= name_len; 2149a542ad1bSJan Schmidt if (bytes_left >= 0) 2150a542ad1bSJan Schmidt read_extent_buffer(eb, dest + bytes_left, 2151d24bec3aSMark Fasheh name_off, name_len); 2152b916a59aSJan Schmidt if (eb != eb_in) { 21530c0fe3b0SFilipe Manana if (!path->skip_locking) 2154ac5887c8SJosef Bacik btrfs_tree_read_unlock(eb); 2155a542ad1bSJan Schmidt free_extent_buffer(eb); 2156b916a59aSJan Schmidt } 2157c234a24dSDavid Sterba ret = btrfs_find_item(fs_root, path, parent, 0, 2158c234a24dSDavid Sterba BTRFS_INODE_REF_KEY, &found_key); 21598f24b496SJan Schmidt if (ret > 0) 21608f24b496SJan Schmidt ret = -ENOENT; 2161a542ad1bSJan Schmidt if (ret) 2162a542ad1bSJan Schmidt break; 2163d24bec3aSMark Fasheh 2164a542ad1bSJan Schmidt next_inum = found_key.offset; 2165a542ad1bSJan Schmidt 2166a542ad1bSJan Schmidt /* regular exit ahead */ 2167a542ad1bSJan Schmidt if (parent == next_inum) 2168a542ad1bSJan Schmidt break; 2169a542ad1bSJan Schmidt 2170a542ad1bSJan Schmidt slot = path->slots[0]; 2171a542ad1bSJan Schmidt eb = path->nodes[0]; 2172a542ad1bSJan Schmidt /* make sure we can use eb after releasing the path */ 2173b916a59aSJan Schmidt if (eb != eb_in) { 21740c0fe3b0SFilipe Manana path->nodes[0] = NULL; 21750c0fe3b0SFilipe Manana path->locks[0] = 0; 2176b916a59aSJan Schmidt } 2177a542ad1bSJan Schmidt btrfs_release_path(path); 2178a542ad1bSJan Schmidt iref = btrfs_item_ptr(eb, slot, struct btrfs_inode_ref); 2179d24bec3aSMark Fasheh 2180d24bec3aSMark Fasheh name_len = btrfs_inode_ref_name_len(eb, iref); 2181d24bec3aSMark Fasheh name_off = (unsigned long)(iref + 1); 2182d24bec3aSMark Fasheh 2183a542ad1bSJan Schmidt parent = next_inum; 2184a542ad1bSJan Schmidt --bytes_left; 2185a542ad1bSJan Schmidt if (bytes_left >= 0) 2186a542ad1bSJan Schmidt dest[bytes_left] = '/'; 2187a542ad1bSJan Schmidt } 2188a542ad1bSJan Schmidt 2189a542ad1bSJan Schmidt btrfs_release_path(path); 2190a542ad1bSJan Schmidt 2191a542ad1bSJan Schmidt if (ret) 2192a542ad1bSJan Schmidt return ERR_PTR(ret); 2193a542ad1bSJan Schmidt 2194a542ad1bSJan Schmidt return dest + bytes_left; 2195a542ad1bSJan Schmidt } 2196a542ad1bSJan Schmidt 2197a542ad1bSJan Schmidt /* 2198a542ad1bSJan Schmidt * this makes the path point to (logical EXTENT_ITEM *) 2199a542ad1bSJan Schmidt * returns BTRFS_EXTENT_FLAG_DATA for data, BTRFS_EXTENT_FLAG_TREE_BLOCK for 2200a542ad1bSJan Schmidt * tree blocks and <0 on error. 2201a542ad1bSJan Schmidt */ 2202a542ad1bSJan Schmidt int extent_from_logical(struct btrfs_fs_info *fs_info, u64 logical, 220369917e43SLiu Bo struct btrfs_path *path, struct btrfs_key *found_key, 220469917e43SLiu Bo u64 *flags_ret) 2205a542ad1bSJan Schmidt { 220629cbcf40SJosef Bacik struct btrfs_root *extent_root = btrfs_extent_root(fs_info, logical); 2207a542ad1bSJan Schmidt int ret; 2208a542ad1bSJan Schmidt u64 flags; 2209261c84b6SJosef Bacik u64 size = 0; 2210a542ad1bSJan Schmidt u32 item_size; 221173980becSJeff Mahoney const struct extent_buffer *eb; 2212a542ad1bSJan Schmidt struct btrfs_extent_item *ei; 2213a542ad1bSJan Schmidt struct btrfs_key key; 2214a542ad1bSJan Schmidt 2215261c84b6SJosef Bacik if (btrfs_fs_incompat(fs_info, SKINNY_METADATA)) 2216261c84b6SJosef Bacik key.type = BTRFS_METADATA_ITEM_KEY; 2217261c84b6SJosef Bacik else 2218a542ad1bSJan Schmidt key.type = BTRFS_EXTENT_ITEM_KEY; 2219a542ad1bSJan Schmidt key.objectid = logical; 2220a542ad1bSJan Schmidt key.offset = (u64)-1; 2221a542ad1bSJan Schmidt 222229cbcf40SJosef Bacik ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0); 2223a542ad1bSJan Schmidt if (ret < 0) 2224a542ad1bSJan Schmidt return ret; 2225a542ad1bSJan Schmidt 222629cbcf40SJosef Bacik ret = btrfs_previous_extent_item(extent_root, path, 0); 2227850a8cdfSWang Shilong if (ret) { 2228850a8cdfSWang Shilong if (ret > 0) 2229580f0a67SJosef Bacik ret = -ENOENT; 2230580f0a67SJosef Bacik return ret; 2231580f0a67SJosef Bacik } 2232850a8cdfSWang Shilong btrfs_item_key_to_cpu(path->nodes[0], found_key, path->slots[0]); 2233261c84b6SJosef Bacik if (found_key->type == BTRFS_METADATA_ITEM_KEY) 2234da17066cSJeff Mahoney size = fs_info->nodesize; 2235261c84b6SJosef Bacik else if (found_key->type == BTRFS_EXTENT_ITEM_KEY) 2236261c84b6SJosef Bacik size = found_key->offset; 2237261c84b6SJosef Bacik 2238580f0a67SJosef Bacik if (found_key->objectid > logical || 2239261c84b6SJosef Bacik found_key->objectid + size <= logical) { 2240ab8d0fc4SJeff Mahoney btrfs_debug(fs_info, 2241ab8d0fc4SJeff Mahoney "logical %llu is not within any extent", logical); 2242a542ad1bSJan Schmidt return -ENOENT; 22434692cf58SJan Schmidt } 2244a542ad1bSJan Schmidt 2245a542ad1bSJan Schmidt eb = path->nodes[0]; 22463212fa14SJosef Bacik item_size = btrfs_item_size(eb, path->slots[0]); 2247a542ad1bSJan Schmidt BUG_ON(item_size < sizeof(*ei)); 2248a542ad1bSJan Schmidt 2249a542ad1bSJan Schmidt ei = btrfs_item_ptr(eb, path->slots[0], struct btrfs_extent_item); 2250a542ad1bSJan Schmidt flags = btrfs_extent_flags(eb, ei); 2251a542ad1bSJan Schmidt 2252ab8d0fc4SJeff Mahoney btrfs_debug(fs_info, 2253ab8d0fc4SJeff Mahoney "logical %llu is at position %llu within the extent (%llu EXTENT_ITEM %llu) flags %#llx size %u", 2254c1c9ff7cSGeert Uytterhoeven logical, logical - found_key->objectid, found_key->objectid, 2255c1c9ff7cSGeert Uytterhoeven found_key->offset, flags, item_size); 225669917e43SLiu Bo 225769917e43SLiu Bo WARN_ON(!flags_ret); 225869917e43SLiu Bo if (flags_ret) { 2259a542ad1bSJan Schmidt if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) 226069917e43SLiu Bo *flags_ret = BTRFS_EXTENT_FLAG_TREE_BLOCK; 226169917e43SLiu Bo else if (flags & BTRFS_EXTENT_FLAG_DATA) 226269917e43SLiu Bo *flags_ret = BTRFS_EXTENT_FLAG_DATA; 226369917e43SLiu Bo else 2264290342f6SArnd Bergmann BUG(); 226569917e43SLiu Bo return 0; 226669917e43SLiu Bo } 2267a542ad1bSJan Schmidt 2268a542ad1bSJan Schmidt return -EIO; 2269a542ad1bSJan Schmidt } 2270a542ad1bSJan Schmidt 2271a542ad1bSJan Schmidt /* 2272a542ad1bSJan Schmidt * helper function to iterate extent inline refs. ptr must point to a 0 value 2273a542ad1bSJan Schmidt * for the first call and may be modified. it is used to track state. 2274a542ad1bSJan Schmidt * if more refs exist, 0 is returned and the next call to 2275e0c476b1SJeff Mahoney * get_extent_inline_ref must pass the modified ptr parameter to get the 2276a542ad1bSJan Schmidt * next ref. after the last ref was processed, 1 is returned. 2277a542ad1bSJan Schmidt * returns <0 on error 2278a542ad1bSJan Schmidt */ 2279e0c476b1SJeff Mahoney static int get_extent_inline_ref(unsigned long *ptr, 228073980becSJeff Mahoney const struct extent_buffer *eb, 228173980becSJeff Mahoney const struct btrfs_key *key, 228273980becSJeff Mahoney const struct btrfs_extent_item *ei, 228373980becSJeff Mahoney u32 item_size, 2284a542ad1bSJan Schmidt struct btrfs_extent_inline_ref **out_eiref, 2285a542ad1bSJan Schmidt int *out_type) 2286a542ad1bSJan Schmidt { 2287a542ad1bSJan Schmidt unsigned long end; 2288a542ad1bSJan Schmidt u64 flags; 2289a542ad1bSJan Schmidt struct btrfs_tree_block_info *info; 2290a542ad1bSJan Schmidt 2291a542ad1bSJan Schmidt if (!*ptr) { 2292a542ad1bSJan Schmidt /* first call */ 2293a542ad1bSJan Schmidt flags = btrfs_extent_flags(eb, ei); 2294a542ad1bSJan Schmidt if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) { 22956eda71d0SLiu Bo if (key->type == BTRFS_METADATA_ITEM_KEY) { 22966eda71d0SLiu Bo /* a skinny metadata extent */ 22976eda71d0SLiu Bo *out_eiref = 22986eda71d0SLiu Bo (struct btrfs_extent_inline_ref *)(ei + 1); 22996eda71d0SLiu Bo } else { 23006eda71d0SLiu Bo WARN_ON(key->type != BTRFS_EXTENT_ITEM_KEY); 2301a542ad1bSJan Schmidt info = (struct btrfs_tree_block_info *)(ei + 1); 2302a542ad1bSJan Schmidt *out_eiref = 2303a542ad1bSJan Schmidt (struct btrfs_extent_inline_ref *)(info + 1); 23046eda71d0SLiu Bo } 2305a542ad1bSJan Schmidt } else { 2306a542ad1bSJan Schmidt *out_eiref = (struct btrfs_extent_inline_ref *)(ei + 1); 2307a542ad1bSJan Schmidt } 2308a542ad1bSJan Schmidt *ptr = (unsigned long)*out_eiref; 2309cd857dd6SLiu Bo if ((unsigned long)(*ptr) >= (unsigned long)ei + item_size) 2310a542ad1bSJan Schmidt return -ENOENT; 2311a542ad1bSJan Schmidt } 2312a542ad1bSJan Schmidt 2313a542ad1bSJan Schmidt end = (unsigned long)ei + item_size; 23146eda71d0SLiu Bo *out_eiref = (struct btrfs_extent_inline_ref *)(*ptr); 23153de28d57SLiu Bo *out_type = btrfs_get_extent_inline_ref_type(eb, *out_eiref, 23163de28d57SLiu Bo BTRFS_REF_TYPE_ANY); 23173de28d57SLiu Bo if (*out_type == BTRFS_REF_TYPE_INVALID) 2318af431dcbSSu Yue return -EUCLEAN; 2319a542ad1bSJan Schmidt 2320a542ad1bSJan Schmidt *ptr += btrfs_extent_inline_ref_size(*out_type); 2321a542ad1bSJan Schmidt WARN_ON(*ptr > end); 2322a542ad1bSJan Schmidt if (*ptr == end) 2323a542ad1bSJan Schmidt return 1; /* last */ 2324a542ad1bSJan Schmidt 2325a542ad1bSJan Schmidt return 0; 2326a542ad1bSJan Schmidt } 2327a542ad1bSJan Schmidt 2328a542ad1bSJan Schmidt /* 2329a542ad1bSJan Schmidt * reads the tree block backref for an extent. tree level and root are returned 2330a542ad1bSJan Schmidt * through out_level and out_root. ptr must point to a 0 value for the first 2331e0c476b1SJeff Mahoney * call and may be modified (see get_extent_inline_ref comment). 2332a542ad1bSJan Schmidt * returns 0 if data was provided, 1 if there was no more data to provide or 2333a542ad1bSJan Schmidt * <0 on error. 2334a542ad1bSJan Schmidt */ 2335a542ad1bSJan Schmidt int tree_backref_for_extent(unsigned long *ptr, struct extent_buffer *eb, 23366eda71d0SLiu Bo struct btrfs_key *key, struct btrfs_extent_item *ei, 23376eda71d0SLiu Bo u32 item_size, u64 *out_root, u8 *out_level) 2338a542ad1bSJan Schmidt { 2339a542ad1bSJan Schmidt int ret; 2340a542ad1bSJan Schmidt int type; 2341a542ad1bSJan Schmidt struct btrfs_extent_inline_ref *eiref; 2342a542ad1bSJan Schmidt 2343a542ad1bSJan Schmidt if (*ptr == (unsigned long)-1) 2344a542ad1bSJan Schmidt return 1; 2345a542ad1bSJan Schmidt 2346a542ad1bSJan Schmidt while (1) { 2347e0c476b1SJeff Mahoney ret = get_extent_inline_ref(ptr, eb, key, ei, item_size, 2348a542ad1bSJan Schmidt &eiref, &type); 2349a542ad1bSJan Schmidt if (ret < 0) 2350a542ad1bSJan Schmidt return ret; 2351a542ad1bSJan Schmidt 2352a542ad1bSJan Schmidt if (type == BTRFS_TREE_BLOCK_REF_KEY || 2353a542ad1bSJan Schmidt type == BTRFS_SHARED_BLOCK_REF_KEY) 2354a542ad1bSJan Schmidt break; 2355a542ad1bSJan Schmidt 2356a542ad1bSJan Schmidt if (ret == 1) 2357a542ad1bSJan Schmidt return 1; 2358a542ad1bSJan Schmidt } 2359a542ad1bSJan Schmidt 2360a542ad1bSJan Schmidt /* we can treat both ref types equally here */ 2361a542ad1bSJan Schmidt *out_root = btrfs_extent_inline_ref_offset(eb, eiref); 2362a1317f45SFilipe Manana 2363a1317f45SFilipe Manana if (key->type == BTRFS_EXTENT_ITEM_KEY) { 2364a1317f45SFilipe Manana struct btrfs_tree_block_info *info; 2365a1317f45SFilipe Manana 2366a1317f45SFilipe Manana info = (struct btrfs_tree_block_info *)(ei + 1); 2367a542ad1bSJan Schmidt *out_level = btrfs_tree_block_level(eb, info); 2368a1317f45SFilipe Manana } else { 2369a1317f45SFilipe Manana ASSERT(key->type == BTRFS_METADATA_ITEM_KEY); 2370a1317f45SFilipe Manana *out_level = (u8)key->offset; 2371a1317f45SFilipe Manana } 2372a542ad1bSJan Schmidt 2373a542ad1bSJan Schmidt if (ret == 1) 2374a542ad1bSJan Schmidt *ptr = (unsigned long)-1; 2375a542ad1bSJan Schmidt 2376a542ad1bSJan Schmidt return 0; 2377a542ad1bSJan Schmidt } 2378a542ad1bSJan Schmidt 2379ab8d0fc4SJeff Mahoney static int iterate_leaf_refs(struct btrfs_fs_info *fs_info, 2380ab8d0fc4SJeff Mahoney struct extent_inode_elem *inode_list, 2381976b1908SJan Schmidt u64 root, u64 extent_item_objectid, 23824692cf58SJan Schmidt iterate_extent_inodes_t *iterate, void *ctx) 2383a542ad1bSJan Schmidt { 2384976b1908SJan Schmidt struct extent_inode_elem *eie; 23854692cf58SJan Schmidt int ret = 0; 2386a542ad1bSJan Schmidt 2387976b1908SJan Schmidt for (eie = inode_list; eie; eie = eie->next) { 2388ab8d0fc4SJeff Mahoney btrfs_debug(fs_info, 2389ab8d0fc4SJeff Mahoney "ref for %llu resolved, key (%llu EXTEND_DATA %llu), root %llu", 2390ab8d0fc4SJeff Mahoney extent_item_objectid, eie->inum, 2391ab8d0fc4SJeff Mahoney eie->offset, root); 2392c7499a64SFilipe Manana ret = iterate(eie->inum, eie->offset, eie->num_bytes, root, ctx); 23934692cf58SJan Schmidt if (ret) { 2394ab8d0fc4SJeff Mahoney btrfs_debug(fs_info, 2395ab8d0fc4SJeff Mahoney "stopping iteration for %llu due to ret=%d", 2396976b1908SJan Schmidt extent_item_objectid, ret); 2397a542ad1bSJan Schmidt break; 2398a542ad1bSJan Schmidt } 2399a542ad1bSJan Schmidt } 2400a542ad1bSJan Schmidt 2401a542ad1bSJan Schmidt return ret; 2402a542ad1bSJan Schmidt } 2403a542ad1bSJan Schmidt 2404a542ad1bSJan Schmidt /* 2405a542ad1bSJan Schmidt * calls iterate() for every inode that references the extent identified by 24064692cf58SJan Schmidt * the given parameters. 2407a542ad1bSJan Schmidt * when the iterator function returns a non-zero value, iteration stops. 2408a542ad1bSJan Schmidt */ 2409a2c8d27eSFilipe Manana int iterate_extent_inodes(struct btrfs_backref_walk_ctx *ctx, 2410a2c8d27eSFilipe Manana bool search_commit_root, 2411a2c8d27eSFilipe Manana iterate_extent_inodes_t *iterate, void *user_ctx) 2412a542ad1bSJan Schmidt { 2413a542ad1bSJan Schmidt int ret; 2414a2c8d27eSFilipe Manana struct ulist *refs; 2415a2c8d27eSFilipe Manana struct ulist_node *ref_node; 2416f3a84ccdSFilipe Manana struct btrfs_seq_list seq_elem = BTRFS_SEQ_LIST_INIT(seq_elem); 2417cd1b413cSJan Schmidt struct ulist_iterator ref_uiter; 2418a542ad1bSJan Schmidt 2419a2c8d27eSFilipe Manana btrfs_debug(ctx->fs_info, "resolving all inodes for extent %llu", 2420a2c8d27eSFilipe Manana ctx->bytenr); 2421a2c8d27eSFilipe Manana 2422a2c8d27eSFilipe Manana ASSERT(ctx->trans == NULL); 24231baea6f1SFilipe Manana ASSERT(ctx->roots == NULL); 24241baea6f1SFilipe Manana 2425da61d31aSJosef Bacik if (!search_commit_root) { 2426a2c8d27eSFilipe Manana struct btrfs_trans_handle *trans; 2427a2c8d27eSFilipe Manana 2428a2c8d27eSFilipe Manana trans = btrfs_attach_transaction(ctx->fs_info->tree_root); 2429bfc61c36SFilipe Manana if (IS_ERR(trans)) { 2430bfc61c36SFilipe Manana if (PTR_ERR(trans) != -ENOENT && 243166d04209SFilipe Manana PTR_ERR(trans) != -EROFS) 24327a3ae2f8SJan Schmidt return PTR_ERR(trans); 2433bfc61c36SFilipe Manana trans = NULL; 24347a3ae2f8SJan Schmidt } 2435a2c8d27eSFilipe Manana ctx->trans = trans; 2436bfc61c36SFilipe Manana } 2437bfc61c36SFilipe Manana 2438a2c8d27eSFilipe Manana if (ctx->trans) { 2439a2c8d27eSFilipe Manana btrfs_get_tree_mod_seq(ctx->fs_info, &seq_elem); 2440a2c8d27eSFilipe Manana ctx->time_seq = seq_elem.seq; 2441a2c8d27eSFilipe Manana } else { 2442a2c8d27eSFilipe Manana down_read(&ctx->fs_info->commit_root_sem); 2443a2c8d27eSFilipe Manana } 24444692cf58SJan Schmidt 2445a2c8d27eSFilipe Manana ret = btrfs_find_all_leafs(ctx); 24464692cf58SJan Schmidt if (ret) 24474692cf58SJan Schmidt goto out; 2448a2c8d27eSFilipe Manana refs = ctx->refs; 2449a2c8d27eSFilipe Manana ctx->refs = NULL; 24504692cf58SJan Schmidt 2451cd1b413cSJan Schmidt ULIST_ITER_INIT(&ref_uiter); 2452cd1b413cSJan Schmidt while (!ret && (ref_node = ulist_next(refs, &ref_uiter))) { 245366d04209SFilipe Manana const u64 leaf_bytenr = ref_node->val; 2454a2c8d27eSFilipe Manana struct ulist_node *root_node; 2455a2c8d27eSFilipe Manana struct ulist_iterator root_uiter; 245666d04209SFilipe Manana struct extent_inode_elem *inode_list; 2457a2c8d27eSFilipe Manana 245866d04209SFilipe Manana inode_list = (struct extent_inode_elem *)(uintptr_t)ref_node->aux; 245966d04209SFilipe Manana 246066d04209SFilipe Manana if (ctx->cache_lookup) { 246166d04209SFilipe Manana const u64 *root_ids; 246266d04209SFilipe Manana int root_count; 246366d04209SFilipe Manana bool cached; 246466d04209SFilipe Manana 246566d04209SFilipe Manana cached = ctx->cache_lookup(leaf_bytenr, ctx->user_ctx, 246666d04209SFilipe Manana &root_ids, &root_count); 246766d04209SFilipe Manana if (cached) { 246866d04209SFilipe Manana for (int i = 0; i < root_count; i++) { 246966d04209SFilipe Manana ret = iterate_leaf_refs(ctx->fs_info, 247066d04209SFilipe Manana inode_list, 247166d04209SFilipe Manana root_ids[i], 247266d04209SFilipe Manana leaf_bytenr, 247366d04209SFilipe Manana iterate, 247466d04209SFilipe Manana user_ctx); 247566d04209SFilipe Manana if (ret) 247666d04209SFilipe Manana break; 247766d04209SFilipe Manana } 247866d04209SFilipe Manana continue; 247966d04209SFilipe Manana } 248066d04209SFilipe Manana } 248166d04209SFilipe Manana 248266d04209SFilipe Manana if (!ctx->roots) { 248366d04209SFilipe Manana ctx->roots = ulist_alloc(GFP_NOFS); 248466d04209SFilipe Manana if (!ctx->roots) { 248566d04209SFilipe Manana ret = -ENOMEM; 248666d04209SFilipe Manana break; 248766d04209SFilipe Manana } 248866d04209SFilipe Manana } 248966d04209SFilipe Manana 249066d04209SFilipe Manana ctx->bytenr = leaf_bytenr; 2491a2c8d27eSFilipe Manana ret = btrfs_find_all_roots_safe(ctx); 24924692cf58SJan Schmidt if (ret) 2493a542ad1bSJan Schmidt break; 2494a2c8d27eSFilipe Manana 249566d04209SFilipe Manana if (ctx->cache_store) 249666d04209SFilipe Manana ctx->cache_store(leaf_bytenr, ctx->roots, ctx->user_ctx); 249766d04209SFilipe Manana 2498cd1b413cSJan Schmidt ULIST_ITER_INIT(&root_uiter); 2499a2c8d27eSFilipe Manana while (!ret && (root_node = ulist_next(ctx->roots, &root_uiter))) { 2500a2c8d27eSFilipe Manana btrfs_debug(ctx->fs_info, 2501ab8d0fc4SJeff Mahoney "root %llu references leaf %llu, data list %#llx", 2502ab8d0fc4SJeff Mahoney root_node->val, ref_node->val, 2503c1c9ff7cSGeert Uytterhoeven ref_node->aux); 250466d04209SFilipe Manana ret = iterate_leaf_refs(ctx->fs_info, inode_list, 2505a2c8d27eSFilipe Manana root_node->val, ctx->bytenr, 2506a2c8d27eSFilipe Manana iterate, user_ctx); 25074692cf58SJan Schmidt } 25081baea6f1SFilipe Manana ulist_reinit(ctx->roots); 2509a542ad1bSJan Schmidt } 2510a542ad1bSJan Schmidt 2511976b1908SJan Schmidt free_leaf_list(refs); 25124692cf58SJan Schmidt out: 2513a2c8d27eSFilipe Manana if (ctx->trans) { 2514a2c8d27eSFilipe Manana btrfs_put_tree_mod_seq(ctx->fs_info, &seq_elem); 2515a2c8d27eSFilipe Manana btrfs_end_transaction(ctx->trans); 2516a2c8d27eSFilipe Manana ctx->trans = NULL; 25179e351cc8SJosef Bacik } else { 2518a2c8d27eSFilipe Manana up_read(&ctx->fs_info->commit_root_sem); 25197a3ae2f8SJan Schmidt } 25207a3ae2f8SJan Schmidt 25211baea6f1SFilipe Manana ulist_free(ctx->roots); 25221baea6f1SFilipe Manana ctx->roots = NULL; 25231baea6f1SFilipe Manana 252488ffb665SFilipe Manana if (ret == BTRFS_ITERATE_EXTENT_INODES_STOP) 252588ffb665SFilipe Manana ret = 0; 252688ffb665SFilipe Manana 2527a542ad1bSJan Schmidt return ret; 2528a542ad1bSJan Schmidt } 2529a542ad1bSJan Schmidt 2530c7499a64SFilipe Manana static int build_ino_list(u64 inum, u64 offset, u64 num_bytes, u64 root, void *ctx) 2531e3059ec0SDavid Sterba { 2532e3059ec0SDavid Sterba struct btrfs_data_container *inodes = ctx; 2533e3059ec0SDavid Sterba const size_t c = 3 * sizeof(u64); 2534e3059ec0SDavid Sterba 2535e3059ec0SDavid Sterba if (inodes->bytes_left >= c) { 2536e3059ec0SDavid Sterba inodes->bytes_left -= c; 2537e3059ec0SDavid Sterba inodes->val[inodes->elem_cnt] = inum; 2538e3059ec0SDavid Sterba inodes->val[inodes->elem_cnt + 1] = offset; 2539e3059ec0SDavid Sterba inodes->val[inodes->elem_cnt + 2] = root; 2540e3059ec0SDavid Sterba inodes->elem_cnt += 3; 2541e3059ec0SDavid Sterba } else { 2542e3059ec0SDavid Sterba inodes->bytes_missing += c - inodes->bytes_left; 2543e3059ec0SDavid Sterba inodes->bytes_left = 0; 2544e3059ec0SDavid Sterba inodes->elem_missed += 3; 2545e3059ec0SDavid Sterba } 2546e3059ec0SDavid Sterba 2547e3059ec0SDavid Sterba return 0; 2548e3059ec0SDavid Sterba } 2549e3059ec0SDavid Sterba 2550a542ad1bSJan Schmidt int iterate_inodes_from_logical(u64 logical, struct btrfs_fs_info *fs_info, 2551a542ad1bSJan Schmidt struct btrfs_path *path, 2552e3059ec0SDavid Sterba void *ctx, bool ignore_offset) 2553a542ad1bSJan Schmidt { 2554a2c8d27eSFilipe Manana struct btrfs_backref_walk_ctx walk_ctx = { 0 }; 2555a542ad1bSJan Schmidt int ret; 255669917e43SLiu Bo u64 flags = 0; 2557a542ad1bSJan Schmidt struct btrfs_key found_key; 25587a3ae2f8SJan Schmidt int search_commit_root = path->search_commit_root; 2559a542ad1bSJan Schmidt 256069917e43SLiu Bo ret = extent_from_logical(fs_info, logical, path, &found_key, &flags); 25614692cf58SJan Schmidt btrfs_release_path(path); 2562a542ad1bSJan Schmidt if (ret < 0) 2563a542ad1bSJan Schmidt return ret; 256469917e43SLiu Bo if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) 25653627bf45SStefan Behrens return -EINVAL; 2566a542ad1bSJan Schmidt 2567a2c8d27eSFilipe Manana walk_ctx.bytenr = found_key.objectid; 25686ce6ba53SFilipe Manana if (ignore_offset) 2569a2c8d27eSFilipe Manana walk_ctx.ignore_extent_item_pos = true; 25706ce6ba53SFilipe Manana else 2571a2c8d27eSFilipe Manana walk_ctx.extent_item_pos = logical - found_key.objectid; 2572a2c8d27eSFilipe Manana walk_ctx.fs_info = fs_info; 25736ce6ba53SFilipe Manana 2574a2c8d27eSFilipe Manana return iterate_extent_inodes(&walk_ctx, search_commit_root, 25756ce6ba53SFilipe Manana build_ino_list, ctx); 2576a542ad1bSJan Schmidt } 2577a542ad1bSJan Schmidt 2578ad6240f6SDavid Sterba static int inode_to_path(u64 inum, u32 name_len, unsigned long name_off, 2579875d1daaSDavid Sterba struct extent_buffer *eb, struct inode_fs_paths *ipath); 2580d24bec3aSMark Fasheh 2581875d1daaSDavid Sterba static int iterate_inode_refs(u64 inum, struct inode_fs_paths *ipath) 2582a542ad1bSJan Schmidt { 2583aefc1eb1SJan Schmidt int ret = 0; 2584a542ad1bSJan Schmidt int slot; 2585a542ad1bSJan Schmidt u32 cur; 2586a542ad1bSJan Schmidt u32 len; 2587a542ad1bSJan Schmidt u32 name_len; 2588a542ad1bSJan Schmidt u64 parent = 0; 2589a542ad1bSJan Schmidt int found = 0; 2590875d1daaSDavid Sterba struct btrfs_root *fs_root = ipath->fs_root; 2591875d1daaSDavid Sterba struct btrfs_path *path = ipath->btrfs_path; 2592a542ad1bSJan Schmidt struct extent_buffer *eb; 2593a542ad1bSJan Schmidt struct btrfs_inode_ref *iref; 2594a542ad1bSJan Schmidt struct btrfs_key found_key; 2595a542ad1bSJan Schmidt 2596aefc1eb1SJan Schmidt while (!ret) { 2597c234a24dSDavid Sterba ret = btrfs_find_item(fs_root, path, inum, 2598c234a24dSDavid Sterba parent ? parent + 1 : 0, BTRFS_INODE_REF_KEY, 2599a542ad1bSJan Schmidt &found_key); 2600c234a24dSDavid Sterba 2601a542ad1bSJan Schmidt if (ret < 0) 2602a542ad1bSJan Schmidt break; 2603a542ad1bSJan Schmidt if (ret) { 2604a542ad1bSJan Schmidt ret = found ? 0 : -ENOENT; 2605a542ad1bSJan Schmidt break; 2606a542ad1bSJan Schmidt } 2607a542ad1bSJan Schmidt ++found; 2608a542ad1bSJan Schmidt 2609a542ad1bSJan Schmidt parent = found_key.offset; 2610a542ad1bSJan Schmidt slot = path->slots[0]; 26113fe81ce2SFilipe David Borba Manana eb = btrfs_clone_extent_buffer(path->nodes[0]); 26123fe81ce2SFilipe David Borba Manana if (!eb) { 26133fe81ce2SFilipe David Borba Manana ret = -ENOMEM; 26143fe81ce2SFilipe David Borba Manana break; 26153fe81ce2SFilipe David Borba Manana } 2616a542ad1bSJan Schmidt btrfs_release_path(path); 2617a542ad1bSJan Schmidt 2618a542ad1bSJan Schmidt iref = btrfs_item_ptr(eb, slot, struct btrfs_inode_ref); 2619a542ad1bSJan Schmidt 26203212fa14SJosef Bacik for (cur = 0; cur < btrfs_item_size(eb, slot); cur += len) { 2621a542ad1bSJan Schmidt name_len = btrfs_inode_ref_name_len(eb, iref); 2622a542ad1bSJan Schmidt /* path must be released before calling iterate()! */ 2623ab8d0fc4SJeff Mahoney btrfs_debug(fs_root->fs_info, 2624ab8d0fc4SJeff Mahoney "following ref at offset %u for inode %llu in tree %llu", 26254fd786e6SMisono Tomohiro cur, found_key.objectid, 26264fd786e6SMisono Tomohiro fs_root->root_key.objectid); 2627ad6240f6SDavid Sterba ret = inode_to_path(parent, name_len, 2628875d1daaSDavid Sterba (unsigned long)(iref + 1), eb, ipath); 2629aefc1eb1SJan Schmidt if (ret) 2630a542ad1bSJan Schmidt break; 2631a542ad1bSJan Schmidt len = sizeof(*iref) + name_len; 2632a542ad1bSJan Schmidt iref = (struct btrfs_inode_ref *)((char *)iref + len); 2633a542ad1bSJan Schmidt } 2634a542ad1bSJan Schmidt free_extent_buffer(eb); 2635a542ad1bSJan Schmidt } 2636a542ad1bSJan Schmidt 2637a542ad1bSJan Schmidt btrfs_release_path(path); 2638a542ad1bSJan Schmidt 2639a542ad1bSJan Schmidt return ret; 2640a542ad1bSJan Schmidt } 2641a542ad1bSJan Schmidt 2642875d1daaSDavid Sterba static int iterate_inode_extrefs(u64 inum, struct inode_fs_paths *ipath) 2643d24bec3aSMark Fasheh { 2644d24bec3aSMark Fasheh int ret; 2645d24bec3aSMark Fasheh int slot; 2646d24bec3aSMark Fasheh u64 offset = 0; 2647d24bec3aSMark Fasheh u64 parent; 2648d24bec3aSMark Fasheh int found = 0; 2649875d1daaSDavid Sterba struct btrfs_root *fs_root = ipath->fs_root; 2650875d1daaSDavid Sterba struct btrfs_path *path = ipath->btrfs_path; 2651d24bec3aSMark Fasheh struct extent_buffer *eb; 2652d24bec3aSMark Fasheh struct btrfs_inode_extref *extref; 2653d24bec3aSMark Fasheh u32 item_size; 2654d24bec3aSMark Fasheh u32 cur_offset; 2655d24bec3aSMark Fasheh unsigned long ptr; 2656d24bec3aSMark Fasheh 2657d24bec3aSMark Fasheh while (1) { 2658d24bec3aSMark Fasheh ret = btrfs_find_one_extref(fs_root, inum, offset, path, &extref, 2659d24bec3aSMark Fasheh &offset); 2660d24bec3aSMark Fasheh if (ret < 0) 2661d24bec3aSMark Fasheh break; 2662d24bec3aSMark Fasheh if (ret) { 2663d24bec3aSMark Fasheh ret = found ? 0 : -ENOENT; 2664d24bec3aSMark Fasheh break; 2665d24bec3aSMark Fasheh } 2666d24bec3aSMark Fasheh ++found; 2667d24bec3aSMark Fasheh 2668d24bec3aSMark Fasheh slot = path->slots[0]; 26693fe81ce2SFilipe David Borba Manana eb = btrfs_clone_extent_buffer(path->nodes[0]); 26703fe81ce2SFilipe David Borba Manana if (!eb) { 26713fe81ce2SFilipe David Borba Manana ret = -ENOMEM; 26723fe81ce2SFilipe David Borba Manana break; 26733fe81ce2SFilipe David Borba Manana } 2674d24bec3aSMark Fasheh btrfs_release_path(path); 2675d24bec3aSMark Fasheh 26763212fa14SJosef Bacik item_size = btrfs_item_size(eb, slot); 26772849a854SChris Mason ptr = btrfs_item_ptr_offset(eb, slot); 2678d24bec3aSMark Fasheh cur_offset = 0; 2679d24bec3aSMark Fasheh 2680d24bec3aSMark Fasheh while (cur_offset < item_size) { 2681d24bec3aSMark Fasheh u32 name_len; 2682d24bec3aSMark Fasheh 2683d24bec3aSMark Fasheh extref = (struct btrfs_inode_extref *)(ptr + cur_offset); 2684d24bec3aSMark Fasheh parent = btrfs_inode_extref_parent(eb, extref); 2685d24bec3aSMark Fasheh name_len = btrfs_inode_extref_name_len(eb, extref); 2686ad6240f6SDavid Sterba ret = inode_to_path(parent, name_len, 2687875d1daaSDavid Sterba (unsigned long)&extref->name, eb, ipath); 2688d24bec3aSMark Fasheh if (ret) 2689d24bec3aSMark Fasheh break; 2690d24bec3aSMark Fasheh 26912849a854SChris Mason cur_offset += btrfs_inode_extref_name_len(eb, extref); 2692d24bec3aSMark Fasheh cur_offset += sizeof(*extref); 2693d24bec3aSMark Fasheh } 2694d24bec3aSMark Fasheh free_extent_buffer(eb); 2695d24bec3aSMark Fasheh 2696d24bec3aSMark Fasheh offset++; 2697d24bec3aSMark Fasheh } 2698d24bec3aSMark Fasheh 2699d24bec3aSMark Fasheh btrfs_release_path(path); 2700d24bec3aSMark Fasheh 2701d24bec3aSMark Fasheh return ret; 2702d24bec3aSMark Fasheh } 2703d24bec3aSMark Fasheh 2704a542ad1bSJan Schmidt /* 2705a542ad1bSJan Schmidt * returns 0 if the path could be dumped (probably truncated) 2706a542ad1bSJan Schmidt * returns <0 in case of an error 2707a542ad1bSJan Schmidt */ 2708d24bec3aSMark Fasheh static int inode_to_path(u64 inum, u32 name_len, unsigned long name_off, 2709875d1daaSDavid Sterba struct extent_buffer *eb, struct inode_fs_paths *ipath) 2710a542ad1bSJan Schmidt { 2711a542ad1bSJan Schmidt char *fspath; 2712a542ad1bSJan Schmidt char *fspath_min; 2713a542ad1bSJan Schmidt int i = ipath->fspath->elem_cnt; 2714a542ad1bSJan Schmidt const int s_ptr = sizeof(char *); 2715a542ad1bSJan Schmidt u32 bytes_left; 2716a542ad1bSJan Schmidt 2717a542ad1bSJan Schmidt bytes_left = ipath->fspath->bytes_left > s_ptr ? 2718a542ad1bSJan Schmidt ipath->fspath->bytes_left - s_ptr : 0; 2719a542ad1bSJan Schmidt 2720740c3d22SChris Mason fspath_min = (char *)ipath->fspath->val + (i + 1) * s_ptr; 272196b5bd77SJan Schmidt fspath = btrfs_ref_to_path(ipath->fs_root, ipath->btrfs_path, name_len, 272296b5bd77SJan Schmidt name_off, eb, inum, fspath_min, bytes_left); 2723a542ad1bSJan Schmidt if (IS_ERR(fspath)) 2724a542ad1bSJan Schmidt return PTR_ERR(fspath); 2725a542ad1bSJan Schmidt 2726a542ad1bSJan Schmidt if (fspath > fspath_min) { 2727745c4d8eSJeff Mahoney ipath->fspath->val[i] = (u64)(unsigned long)fspath; 2728a542ad1bSJan Schmidt ++ipath->fspath->elem_cnt; 2729a542ad1bSJan Schmidt ipath->fspath->bytes_left = fspath - fspath_min; 2730a542ad1bSJan Schmidt } else { 2731a542ad1bSJan Schmidt ++ipath->fspath->elem_missed; 2732a542ad1bSJan Schmidt ipath->fspath->bytes_missing += fspath_min - fspath; 2733a542ad1bSJan Schmidt ipath->fspath->bytes_left = 0; 2734a542ad1bSJan Schmidt } 2735a542ad1bSJan Schmidt 2736a542ad1bSJan Schmidt return 0; 2737a542ad1bSJan Schmidt } 2738a542ad1bSJan Schmidt 2739a542ad1bSJan Schmidt /* 2740a542ad1bSJan Schmidt * this dumps all file system paths to the inode into the ipath struct, provided 2741a542ad1bSJan Schmidt * is has been created large enough. each path is zero-terminated and accessed 2742740c3d22SChris Mason * from ipath->fspath->val[i]. 2743a542ad1bSJan Schmidt * when it returns, there are ipath->fspath->elem_cnt number of paths available 2744740c3d22SChris Mason * in ipath->fspath->val[]. when the allocated space wasn't sufficient, the 274501327610SNicholas D Steeves * number of missed paths is recorded in ipath->fspath->elem_missed, otherwise, 2746a542ad1bSJan Schmidt * it's zero. ipath->fspath->bytes_missing holds the number of bytes that would 2747a542ad1bSJan Schmidt * have been needed to return all paths. 2748a542ad1bSJan Schmidt */ 2749a542ad1bSJan Schmidt int paths_from_inode(u64 inum, struct inode_fs_paths *ipath) 2750a542ad1bSJan Schmidt { 2751ad6240f6SDavid Sterba int ret; 2752ad6240f6SDavid Sterba int found_refs = 0; 2753ad6240f6SDavid Sterba 2754875d1daaSDavid Sterba ret = iterate_inode_refs(inum, ipath); 2755ad6240f6SDavid Sterba if (!ret) 2756ad6240f6SDavid Sterba ++found_refs; 2757ad6240f6SDavid Sterba else if (ret != -ENOENT) 2758ad6240f6SDavid Sterba return ret; 2759ad6240f6SDavid Sterba 2760875d1daaSDavid Sterba ret = iterate_inode_extrefs(inum, ipath); 2761ad6240f6SDavid Sterba if (ret == -ENOENT && found_refs) 2762ad6240f6SDavid Sterba return 0; 2763ad6240f6SDavid Sterba 2764ad6240f6SDavid Sterba return ret; 2765a542ad1bSJan Schmidt } 2766a542ad1bSJan Schmidt 2767a542ad1bSJan Schmidt struct btrfs_data_container *init_data_container(u32 total_bytes) 2768a542ad1bSJan Schmidt { 2769a542ad1bSJan Schmidt struct btrfs_data_container *data; 2770a542ad1bSJan Schmidt size_t alloc_bytes; 2771a542ad1bSJan Schmidt 2772a542ad1bSJan Schmidt alloc_bytes = max_t(size_t, total_bytes, sizeof(*data)); 2773*3a63cee1SJohannes Thumshirn data = kvzalloc(alloc_bytes, GFP_KERNEL); 2774a542ad1bSJan Schmidt if (!data) 2775a542ad1bSJan Schmidt return ERR_PTR(-ENOMEM); 2776a542ad1bSJan Schmidt 2777*3a63cee1SJohannes Thumshirn if (total_bytes >= sizeof(*data)) 2778a542ad1bSJan Schmidt data->bytes_left = total_bytes - sizeof(*data); 2779*3a63cee1SJohannes Thumshirn else 2780a542ad1bSJan Schmidt data->bytes_missing = sizeof(*data) - total_bytes; 2781a542ad1bSJan Schmidt 2782a542ad1bSJan Schmidt return data; 2783a542ad1bSJan Schmidt } 2784a542ad1bSJan Schmidt 2785a542ad1bSJan Schmidt /* 2786a542ad1bSJan Schmidt * allocates space to return multiple file system paths for an inode. 2787a542ad1bSJan Schmidt * total_bytes to allocate are passed, note that space usable for actual path 2788a542ad1bSJan Schmidt * information will be total_bytes - sizeof(struct inode_fs_paths). 2789a542ad1bSJan Schmidt * the returned pointer must be freed with free_ipath() in the end. 2790a542ad1bSJan Schmidt */ 2791a542ad1bSJan Schmidt struct inode_fs_paths *init_ipath(s32 total_bytes, struct btrfs_root *fs_root, 2792a542ad1bSJan Schmidt struct btrfs_path *path) 2793a542ad1bSJan Schmidt { 2794a542ad1bSJan Schmidt struct inode_fs_paths *ifp; 2795a542ad1bSJan Schmidt struct btrfs_data_container *fspath; 2796a542ad1bSJan Schmidt 2797a542ad1bSJan Schmidt fspath = init_data_container(total_bytes); 2798a542ad1bSJan Schmidt if (IS_ERR(fspath)) 2799afc6961fSMisono Tomohiro return ERR_CAST(fspath); 2800a542ad1bSJan Schmidt 2801f54de068SDavid Sterba ifp = kmalloc(sizeof(*ifp), GFP_KERNEL); 2802a542ad1bSJan Schmidt if (!ifp) { 2803f54de068SDavid Sterba kvfree(fspath); 2804a542ad1bSJan Schmidt return ERR_PTR(-ENOMEM); 2805a542ad1bSJan Schmidt } 2806a542ad1bSJan Schmidt 2807a542ad1bSJan Schmidt ifp->btrfs_path = path; 2808a542ad1bSJan Schmidt ifp->fspath = fspath; 2809a542ad1bSJan Schmidt ifp->fs_root = fs_root; 2810a542ad1bSJan Schmidt 2811a542ad1bSJan Schmidt return ifp; 2812a542ad1bSJan Schmidt } 2813a542ad1bSJan Schmidt 2814a542ad1bSJan Schmidt void free_ipath(struct inode_fs_paths *ipath) 2815a542ad1bSJan Schmidt { 28164735fb28SJesper Juhl if (!ipath) 28174735fb28SJesper Juhl return; 2818f54de068SDavid Sterba kvfree(ipath->fspath); 2819a542ad1bSJan Schmidt kfree(ipath); 2820a542ad1bSJan Schmidt } 2821a37f232bSQu Wenruo 2822d68194b2SDavid Sterba struct btrfs_backref_iter *btrfs_backref_iter_alloc(struct btrfs_fs_info *fs_info) 2823a37f232bSQu Wenruo { 2824a37f232bSQu Wenruo struct btrfs_backref_iter *ret; 2825a37f232bSQu Wenruo 2826d68194b2SDavid Sterba ret = kzalloc(sizeof(*ret), GFP_NOFS); 2827a37f232bSQu Wenruo if (!ret) 2828a37f232bSQu Wenruo return NULL; 2829a37f232bSQu Wenruo 2830a37f232bSQu Wenruo ret->path = btrfs_alloc_path(); 2831c15c2ec0SBoleyn Su if (!ret->path) { 2832a37f232bSQu Wenruo kfree(ret); 2833a37f232bSQu Wenruo return NULL; 2834a37f232bSQu Wenruo } 2835a37f232bSQu Wenruo 2836a37f232bSQu Wenruo /* Current backref iterator only supports iteration in commit root */ 2837a37f232bSQu Wenruo ret->path->search_commit_root = 1; 2838a37f232bSQu Wenruo ret->path->skip_locking = 1; 2839a37f232bSQu Wenruo ret->fs_info = fs_info; 2840a37f232bSQu Wenruo 2841a37f232bSQu Wenruo return ret; 2842a37f232bSQu Wenruo } 2843a37f232bSQu Wenruo 2844a37f232bSQu Wenruo int btrfs_backref_iter_start(struct btrfs_backref_iter *iter, u64 bytenr) 2845a37f232bSQu Wenruo { 2846a37f232bSQu Wenruo struct btrfs_fs_info *fs_info = iter->fs_info; 284729cbcf40SJosef Bacik struct btrfs_root *extent_root = btrfs_extent_root(fs_info, bytenr); 2848a37f232bSQu Wenruo struct btrfs_path *path = iter->path; 2849a37f232bSQu Wenruo struct btrfs_extent_item *ei; 2850a37f232bSQu Wenruo struct btrfs_key key; 2851a37f232bSQu Wenruo int ret; 2852a37f232bSQu Wenruo 2853a37f232bSQu Wenruo key.objectid = bytenr; 2854a37f232bSQu Wenruo key.type = BTRFS_METADATA_ITEM_KEY; 2855a37f232bSQu Wenruo key.offset = (u64)-1; 2856a37f232bSQu Wenruo iter->bytenr = bytenr; 2857a37f232bSQu Wenruo 285829cbcf40SJosef Bacik ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0); 2859a37f232bSQu Wenruo if (ret < 0) 2860a37f232bSQu Wenruo return ret; 2861a37f232bSQu Wenruo if (ret == 0) { 2862a37f232bSQu Wenruo ret = -EUCLEAN; 2863a37f232bSQu Wenruo goto release; 2864a37f232bSQu Wenruo } 2865a37f232bSQu Wenruo if (path->slots[0] == 0) { 2866a37f232bSQu Wenruo WARN_ON(IS_ENABLED(CONFIG_BTRFS_DEBUG)); 2867a37f232bSQu Wenruo ret = -EUCLEAN; 2868a37f232bSQu Wenruo goto release; 2869a37f232bSQu Wenruo } 2870a37f232bSQu Wenruo path->slots[0]--; 2871a37f232bSQu Wenruo 2872a37f232bSQu Wenruo btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); 2873a37f232bSQu Wenruo if ((key.type != BTRFS_EXTENT_ITEM_KEY && 2874a37f232bSQu Wenruo key.type != BTRFS_METADATA_ITEM_KEY) || key.objectid != bytenr) { 2875a37f232bSQu Wenruo ret = -ENOENT; 2876a37f232bSQu Wenruo goto release; 2877a37f232bSQu Wenruo } 2878a37f232bSQu Wenruo memcpy(&iter->cur_key, &key, sizeof(key)); 2879a37f232bSQu Wenruo iter->item_ptr = (u32)btrfs_item_ptr_offset(path->nodes[0], 2880a37f232bSQu Wenruo path->slots[0]); 2881a37f232bSQu Wenruo iter->end_ptr = (u32)(iter->item_ptr + 28823212fa14SJosef Bacik btrfs_item_size(path->nodes[0], path->slots[0])); 2883a37f232bSQu Wenruo ei = btrfs_item_ptr(path->nodes[0], path->slots[0], 2884a37f232bSQu Wenruo struct btrfs_extent_item); 2885a37f232bSQu Wenruo 2886a37f232bSQu Wenruo /* 2887a37f232bSQu Wenruo * Only support iteration on tree backref yet. 2888a37f232bSQu Wenruo * 2889a37f232bSQu Wenruo * This is an extra precaution for non skinny-metadata, where 2890a37f232bSQu Wenruo * EXTENT_ITEM is also used for tree blocks, that we can only use 2891a37f232bSQu Wenruo * extent flags to determine if it's a tree block. 2892a37f232bSQu Wenruo */ 2893a37f232bSQu Wenruo if (btrfs_extent_flags(path->nodes[0], ei) & BTRFS_EXTENT_FLAG_DATA) { 2894a37f232bSQu Wenruo ret = -ENOTSUPP; 2895a37f232bSQu Wenruo goto release; 2896a37f232bSQu Wenruo } 2897a37f232bSQu Wenruo iter->cur_ptr = (u32)(iter->item_ptr + sizeof(*ei)); 2898a37f232bSQu Wenruo 2899a37f232bSQu Wenruo /* If there is no inline backref, go search for keyed backref */ 2900a37f232bSQu Wenruo if (iter->cur_ptr >= iter->end_ptr) { 290129cbcf40SJosef Bacik ret = btrfs_next_item(extent_root, path); 2902a37f232bSQu Wenruo 2903a37f232bSQu Wenruo /* No inline nor keyed ref */ 2904a37f232bSQu Wenruo if (ret > 0) { 2905a37f232bSQu Wenruo ret = -ENOENT; 2906a37f232bSQu Wenruo goto release; 2907a37f232bSQu Wenruo } 2908a37f232bSQu Wenruo if (ret < 0) 2909a37f232bSQu Wenruo goto release; 2910a37f232bSQu Wenruo 2911a37f232bSQu Wenruo btrfs_item_key_to_cpu(path->nodes[0], &iter->cur_key, 2912a37f232bSQu Wenruo path->slots[0]); 2913a37f232bSQu Wenruo if (iter->cur_key.objectid != bytenr || 2914a37f232bSQu Wenruo (iter->cur_key.type != BTRFS_SHARED_BLOCK_REF_KEY && 2915a37f232bSQu Wenruo iter->cur_key.type != BTRFS_TREE_BLOCK_REF_KEY)) { 2916a37f232bSQu Wenruo ret = -ENOENT; 2917a37f232bSQu Wenruo goto release; 2918a37f232bSQu Wenruo } 2919a37f232bSQu Wenruo iter->cur_ptr = (u32)btrfs_item_ptr_offset(path->nodes[0], 2920a37f232bSQu Wenruo path->slots[0]); 2921a37f232bSQu Wenruo iter->item_ptr = iter->cur_ptr; 29223212fa14SJosef Bacik iter->end_ptr = (u32)(iter->item_ptr + btrfs_item_size( 2923a37f232bSQu Wenruo path->nodes[0], path->slots[0])); 2924a37f232bSQu Wenruo } 2925a37f232bSQu Wenruo 2926a37f232bSQu Wenruo return 0; 2927a37f232bSQu Wenruo release: 2928a37f232bSQu Wenruo btrfs_backref_iter_release(iter); 2929a37f232bSQu Wenruo return ret; 2930a37f232bSQu Wenruo } 2931c39c2ddcSQu Wenruo 2932c39c2ddcSQu Wenruo /* 2933c39c2ddcSQu Wenruo * Go to the next backref item of current bytenr, can be either inlined or 2934c39c2ddcSQu Wenruo * keyed. 2935c39c2ddcSQu Wenruo * 2936c39c2ddcSQu Wenruo * Caller needs to check whether it's inline ref or not by iter->cur_key. 2937c39c2ddcSQu Wenruo * 2938c39c2ddcSQu Wenruo * Return 0 if we get next backref without problem. 2939c39c2ddcSQu Wenruo * Return >0 if there is no extra backref for this bytenr. 2940c39c2ddcSQu Wenruo * Return <0 if there is something wrong happened. 2941c39c2ddcSQu Wenruo */ 2942c39c2ddcSQu Wenruo int btrfs_backref_iter_next(struct btrfs_backref_iter *iter) 2943c39c2ddcSQu Wenruo { 2944c39c2ddcSQu Wenruo struct extent_buffer *eb = btrfs_backref_get_eb(iter); 294529cbcf40SJosef Bacik struct btrfs_root *extent_root; 2946c39c2ddcSQu Wenruo struct btrfs_path *path = iter->path; 2947c39c2ddcSQu Wenruo struct btrfs_extent_inline_ref *iref; 2948c39c2ddcSQu Wenruo int ret; 2949c39c2ddcSQu Wenruo u32 size; 2950c39c2ddcSQu Wenruo 2951c39c2ddcSQu Wenruo if (btrfs_backref_iter_is_inline_ref(iter)) { 2952c39c2ddcSQu Wenruo /* We're still inside the inline refs */ 2953c39c2ddcSQu Wenruo ASSERT(iter->cur_ptr < iter->end_ptr); 2954c39c2ddcSQu Wenruo 2955c39c2ddcSQu Wenruo if (btrfs_backref_has_tree_block_info(iter)) { 2956c39c2ddcSQu Wenruo /* First tree block info */ 2957c39c2ddcSQu Wenruo size = sizeof(struct btrfs_tree_block_info); 2958c39c2ddcSQu Wenruo } else { 2959c39c2ddcSQu Wenruo /* Use inline ref type to determine the size */ 2960c39c2ddcSQu Wenruo int type; 2961c39c2ddcSQu Wenruo 2962c39c2ddcSQu Wenruo iref = (struct btrfs_extent_inline_ref *) 2963c39c2ddcSQu Wenruo ((unsigned long)iter->cur_ptr); 2964c39c2ddcSQu Wenruo type = btrfs_extent_inline_ref_type(eb, iref); 2965c39c2ddcSQu Wenruo 2966c39c2ddcSQu Wenruo size = btrfs_extent_inline_ref_size(type); 2967c39c2ddcSQu Wenruo } 2968c39c2ddcSQu Wenruo iter->cur_ptr += size; 2969c39c2ddcSQu Wenruo if (iter->cur_ptr < iter->end_ptr) 2970c39c2ddcSQu Wenruo return 0; 2971c39c2ddcSQu Wenruo 2972c39c2ddcSQu Wenruo /* All inline items iterated, fall through */ 2973c39c2ddcSQu Wenruo } 2974c39c2ddcSQu Wenruo 2975c39c2ddcSQu Wenruo /* We're at keyed items, there is no inline item, go to the next one */ 297629cbcf40SJosef Bacik extent_root = btrfs_extent_root(iter->fs_info, iter->bytenr); 297729cbcf40SJosef Bacik ret = btrfs_next_item(extent_root, iter->path); 2978c39c2ddcSQu Wenruo if (ret) 2979c39c2ddcSQu Wenruo return ret; 2980c39c2ddcSQu Wenruo 2981c39c2ddcSQu Wenruo btrfs_item_key_to_cpu(path->nodes[0], &iter->cur_key, path->slots[0]); 2982c39c2ddcSQu Wenruo if (iter->cur_key.objectid != iter->bytenr || 2983c39c2ddcSQu Wenruo (iter->cur_key.type != BTRFS_TREE_BLOCK_REF_KEY && 2984c39c2ddcSQu Wenruo iter->cur_key.type != BTRFS_SHARED_BLOCK_REF_KEY)) 2985c39c2ddcSQu Wenruo return 1; 2986c39c2ddcSQu Wenruo iter->item_ptr = (u32)btrfs_item_ptr_offset(path->nodes[0], 2987c39c2ddcSQu Wenruo path->slots[0]); 2988c39c2ddcSQu Wenruo iter->cur_ptr = iter->item_ptr; 29893212fa14SJosef Bacik iter->end_ptr = iter->item_ptr + (u32)btrfs_item_size(path->nodes[0], 2990c39c2ddcSQu Wenruo path->slots[0]); 2991c39c2ddcSQu Wenruo return 0; 2992c39c2ddcSQu Wenruo } 2993584fb121SQu Wenruo 2994584fb121SQu Wenruo void btrfs_backref_init_cache(struct btrfs_fs_info *fs_info, 2995584fb121SQu Wenruo struct btrfs_backref_cache *cache, int is_reloc) 2996584fb121SQu Wenruo { 2997584fb121SQu Wenruo int i; 2998584fb121SQu Wenruo 2999584fb121SQu Wenruo cache->rb_root = RB_ROOT; 3000584fb121SQu Wenruo for (i = 0; i < BTRFS_MAX_LEVEL; i++) 3001584fb121SQu Wenruo INIT_LIST_HEAD(&cache->pending[i]); 3002584fb121SQu Wenruo INIT_LIST_HEAD(&cache->changed); 3003584fb121SQu Wenruo INIT_LIST_HEAD(&cache->detached); 3004584fb121SQu Wenruo INIT_LIST_HEAD(&cache->leaves); 3005584fb121SQu Wenruo INIT_LIST_HEAD(&cache->pending_edge); 3006584fb121SQu Wenruo INIT_LIST_HEAD(&cache->useless_node); 3007584fb121SQu Wenruo cache->fs_info = fs_info; 3008584fb121SQu Wenruo cache->is_reloc = is_reloc; 3009584fb121SQu Wenruo } 3010b1818dabSQu Wenruo 3011b1818dabSQu Wenruo struct btrfs_backref_node *btrfs_backref_alloc_node( 3012b1818dabSQu Wenruo struct btrfs_backref_cache *cache, u64 bytenr, int level) 3013b1818dabSQu Wenruo { 3014b1818dabSQu Wenruo struct btrfs_backref_node *node; 3015b1818dabSQu Wenruo 3016b1818dabSQu Wenruo ASSERT(level >= 0 && level < BTRFS_MAX_LEVEL); 3017b1818dabSQu Wenruo node = kzalloc(sizeof(*node), GFP_NOFS); 3018b1818dabSQu Wenruo if (!node) 3019b1818dabSQu Wenruo return node; 3020b1818dabSQu Wenruo 3021b1818dabSQu Wenruo INIT_LIST_HEAD(&node->list); 3022b1818dabSQu Wenruo INIT_LIST_HEAD(&node->upper); 3023b1818dabSQu Wenruo INIT_LIST_HEAD(&node->lower); 3024b1818dabSQu Wenruo RB_CLEAR_NODE(&node->rb_node); 3025b1818dabSQu Wenruo cache->nr_nodes++; 3026b1818dabSQu Wenruo node->level = level; 3027b1818dabSQu Wenruo node->bytenr = bytenr; 3028b1818dabSQu Wenruo 3029b1818dabSQu Wenruo return node; 3030b1818dabSQu Wenruo } 303147254d07SQu Wenruo 303247254d07SQu Wenruo struct btrfs_backref_edge *btrfs_backref_alloc_edge( 303347254d07SQu Wenruo struct btrfs_backref_cache *cache) 303447254d07SQu Wenruo { 303547254d07SQu Wenruo struct btrfs_backref_edge *edge; 303647254d07SQu Wenruo 303747254d07SQu Wenruo edge = kzalloc(sizeof(*edge), GFP_NOFS); 303847254d07SQu Wenruo if (edge) 303947254d07SQu Wenruo cache->nr_edges++; 304047254d07SQu Wenruo return edge; 304147254d07SQu Wenruo } 3042023acb07SQu Wenruo 3043023acb07SQu Wenruo /* 3044023acb07SQu Wenruo * Drop the backref node from cache, also cleaning up all its 3045023acb07SQu Wenruo * upper edges and any uncached nodes in the path. 3046023acb07SQu Wenruo * 3047023acb07SQu Wenruo * This cleanup happens bottom up, thus the node should either 3048023acb07SQu Wenruo * be the lowest node in the cache or a detached node. 3049023acb07SQu Wenruo */ 3050023acb07SQu Wenruo void btrfs_backref_cleanup_node(struct btrfs_backref_cache *cache, 3051023acb07SQu Wenruo struct btrfs_backref_node *node) 3052023acb07SQu Wenruo { 3053023acb07SQu Wenruo struct btrfs_backref_node *upper; 3054023acb07SQu Wenruo struct btrfs_backref_edge *edge; 3055023acb07SQu Wenruo 3056023acb07SQu Wenruo if (!node) 3057023acb07SQu Wenruo return; 3058023acb07SQu Wenruo 3059023acb07SQu Wenruo BUG_ON(!node->lowest && !node->detached); 3060023acb07SQu Wenruo while (!list_empty(&node->upper)) { 3061023acb07SQu Wenruo edge = list_entry(node->upper.next, struct btrfs_backref_edge, 3062023acb07SQu Wenruo list[LOWER]); 3063023acb07SQu Wenruo upper = edge->node[UPPER]; 3064023acb07SQu Wenruo list_del(&edge->list[LOWER]); 3065023acb07SQu Wenruo list_del(&edge->list[UPPER]); 3066023acb07SQu Wenruo btrfs_backref_free_edge(cache, edge); 3067023acb07SQu Wenruo 3068023acb07SQu Wenruo /* 3069023acb07SQu Wenruo * Add the node to leaf node list if no other child block 3070023acb07SQu Wenruo * cached. 3071023acb07SQu Wenruo */ 3072023acb07SQu Wenruo if (list_empty(&upper->lower)) { 3073023acb07SQu Wenruo list_add_tail(&upper->lower, &cache->leaves); 3074023acb07SQu Wenruo upper->lowest = 1; 3075023acb07SQu Wenruo } 3076023acb07SQu Wenruo } 3077023acb07SQu Wenruo 3078023acb07SQu Wenruo btrfs_backref_drop_node(cache, node); 3079023acb07SQu Wenruo } 308013fe1bdbSQu Wenruo 308113fe1bdbSQu Wenruo /* 308213fe1bdbSQu Wenruo * Release all nodes/edges from current cache 308313fe1bdbSQu Wenruo */ 308413fe1bdbSQu Wenruo void btrfs_backref_release_cache(struct btrfs_backref_cache *cache) 308513fe1bdbSQu Wenruo { 308613fe1bdbSQu Wenruo struct btrfs_backref_node *node; 308713fe1bdbSQu Wenruo int i; 308813fe1bdbSQu Wenruo 308913fe1bdbSQu Wenruo while (!list_empty(&cache->detached)) { 309013fe1bdbSQu Wenruo node = list_entry(cache->detached.next, 309113fe1bdbSQu Wenruo struct btrfs_backref_node, list); 309213fe1bdbSQu Wenruo btrfs_backref_cleanup_node(cache, node); 309313fe1bdbSQu Wenruo } 309413fe1bdbSQu Wenruo 309513fe1bdbSQu Wenruo while (!list_empty(&cache->leaves)) { 309613fe1bdbSQu Wenruo node = list_entry(cache->leaves.next, 309713fe1bdbSQu Wenruo struct btrfs_backref_node, lower); 309813fe1bdbSQu Wenruo btrfs_backref_cleanup_node(cache, node); 309913fe1bdbSQu Wenruo } 310013fe1bdbSQu Wenruo 310113fe1bdbSQu Wenruo cache->last_trans = 0; 310213fe1bdbSQu Wenruo 310313fe1bdbSQu Wenruo for (i = 0; i < BTRFS_MAX_LEVEL; i++) 310413fe1bdbSQu Wenruo ASSERT(list_empty(&cache->pending[i])); 310513fe1bdbSQu Wenruo ASSERT(list_empty(&cache->pending_edge)); 310613fe1bdbSQu Wenruo ASSERT(list_empty(&cache->useless_node)); 310713fe1bdbSQu Wenruo ASSERT(list_empty(&cache->changed)); 310813fe1bdbSQu Wenruo ASSERT(list_empty(&cache->detached)); 310913fe1bdbSQu Wenruo ASSERT(RB_EMPTY_ROOT(&cache->rb_root)); 311013fe1bdbSQu Wenruo ASSERT(!cache->nr_nodes); 311113fe1bdbSQu Wenruo ASSERT(!cache->nr_edges); 311213fe1bdbSQu Wenruo } 31131b60d2ecSQu Wenruo 31141b60d2ecSQu Wenruo /* 31151b60d2ecSQu Wenruo * Handle direct tree backref 31161b60d2ecSQu Wenruo * 31171b60d2ecSQu Wenruo * Direct tree backref means, the backref item shows its parent bytenr 31181b60d2ecSQu Wenruo * directly. This is for SHARED_BLOCK_REF backref (keyed or inlined). 31191b60d2ecSQu Wenruo * 31201b60d2ecSQu Wenruo * @ref_key: The converted backref key. 31211b60d2ecSQu Wenruo * For keyed backref, it's the item key. 31221b60d2ecSQu Wenruo * For inlined backref, objectid is the bytenr, 31231b60d2ecSQu Wenruo * type is btrfs_inline_ref_type, offset is 31241b60d2ecSQu Wenruo * btrfs_inline_ref_offset. 31251b60d2ecSQu Wenruo */ 31261b60d2ecSQu Wenruo static int handle_direct_tree_backref(struct btrfs_backref_cache *cache, 31271b60d2ecSQu Wenruo struct btrfs_key *ref_key, 31281b60d2ecSQu Wenruo struct btrfs_backref_node *cur) 31291b60d2ecSQu Wenruo { 31301b60d2ecSQu Wenruo struct btrfs_backref_edge *edge; 31311b60d2ecSQu Wenruo struct btrfs_backref_node *upper; 31321b60d2ecSQu Wenruo struct rb_node *rb_node; 31331b60d2ecSQu Wenruo 31341b60d2ecSQu Wenruo ASSERT(ref_key->type == BTRFS_SHARED_BLOCK_REF_KEY); 31351b60d2ecSQu Wenruo 31361b60d2ecSQu Wenruo /* Only reloc root uses backref pointing to itself */ 31371b60d2ecSQu Wenruo if (ref_key->objectid == ref_key->offset) { 31381b60d2ecSQu Wenruo struct btrfs_root *root; 31391b60d2ecSQu Wenruo 31401b60d2ecSQu Wenruo cur->is_reloc_root = 1; 31411b60d2ecSQu Wenruo /* Only reloc backref cache cares about a specific root */ 31421b60d2ecSQu Wenruo if (cache->is_reloc) { 31431b60d2ecSQu Wenruo root = find_reloc_root(cache->fs_info, cur->bytenr); 3144f78743fbSJosef Bacik if (!root) 31451b60d2ecSQu Wenruo return -ENOENT; 31461b60d2ecSQu Wenruo cur->root = root; 31471b60d2ecSQu Wenruo } else { 31481b60d2ecSQu Wenruo /* 31491b60d2ecSQu Wenruo * For generic purpose backref cache, reloc root node 31501b60d2ecSQu Wenruo * is useless. 31511b60d2ecSQu Wenruo */ 31521b60d2ecSQu Wenruo list_add(&cur->list, &cache->useless_node); 31531b60d2ecSQu Wenruo } 31541b60d2ecSQu Wenruo return 0; 31551b60d2ecSQu Wenruo } 31561b60d2ecSQu Wenruo 31571b60d2ecSQu Wenruo edge = btrfs_backref_alloc_edge(cache); 31581b60d2ecSQu Wenruo if (!edge) 31591b60d2ecSQu Wenruo return -ENOMEM; 31601b60d2ecSQu Wenruo 31611b60d2ecSQu Wenruo rb_node = rb_simple_search(&cache->rb_root, ref_key->offset); 31621b60d2ecSQu Wenruo if (!rb_node) { 31631b60d2ecSQu Wenruo /* Parent node not yet cached */ 31641b60d2ecSQu Wenruo upper = btrfs_backref_alloc_node(cache, ref_key->offset, 31651b60d2ecSQu Wenruo cur->level + 1); 31661b60d2ecSQu Wenruo if (!upper) { 31671b60d2ecSQu Wenruo btrfs_backref_free_edge(cache, edge); 31681b60d2ecSQu Wenruo return -ENOMEM; 31691b60d2ecSQu Wenruo } 31701b60d2ecSQu Wenruo 31711b60d2ecSQu Wenruo /* 31721b60d2ecSQu Wenruo * Backrefs for the upper level block isn't cached, add the 31731b60d2ecSQu Wenruo * block to pending list 31741b60d2ecSQu Wenruo */ 31751b60d2ecSQu Wenruo list_add_tail(&edge->list[UPPER], &cache->pending_edge); 31761b60d2ecSQu Wenruo } else { 31771b60d2ecSQu Wenruo /* Parent node already cached */ 31781b60d2ecSQu Wenruo upper = rb_entry(rb_node, struct btrfs_backref_node, rb_node); 31791b60d2ecSQu Wenruo ASSERT(upper->checked); 31801b60d2ecSQu Wenruo INIT_LIST_HEAD(&edge->list[UPPER]); 31811b60d2ecSQu Wenruo } 31821b60d2ecSQu Wenruo btrfs_backref_link_edge(edge, cur, upper, LINK_LOWER); 31831b60d2ecSQu Wenruo return 0; 31841b60d2ecSQu Wenruo } 31851b60d2ecSQu Wenruo 31861b60d2ecSQu Wenruo /* 31871b60d2ecSQu Wenruo * Handle indirect tree backref 31881b60d2ecSQu Wenruo * 31891b60d2ecSQu Wenruo * Indirect tree backref means, we only know which tree the node belongs to. 31901b60d2ecSQu Wenruo * We still need to do a tree search to find out the parents. This is for 31911b60d2ecSQu Wenruo * TREE_BLOCK_REF backref (keyed or inlined). 31921b60d2ecSQu Wenruo * 3193eb96e221SFilipe Manana * @trans: Transaction handle. 31941b60d2ecSQu Wenruo * @ref_key: The same as @ref_key in handle_direct_tree_backref() 31951b60d2ecSQu Wenruo * @tree_key: The first key of this tree block. 31961b60d2ecSQu Wenruo * @path: A clean (released) path, to avoid allocating path every time 31971b60d2ecSQu Wenruo * the function get called. 31981b60d2ecSQu Wenruo */ 3199eb96e221SFilipe Manana static int handle_indirect_tree_backref(struct btrfs_trans_handle *trans, 3200eb96e221SFilipe Manana struct btrfs_backref_cache *cache, 32011b60d2ecSQu Wenruo struct btrfs_path *path, 32021b60d2ecSQu Wenruo struct btrfs_key *ref_key, 32031b60d2ecSQu Wenruo struct btrfs_key *tree_key, 32041b60d2ecSQu Wenruo struct btrfs_backref_node *cur) 32051b60d2ecSQu Wenruo { 32061b60d2ecSQu Wenruo struct btrfs_fs_info *fs_info = cache->fs_info; 32071b60d2ecSQu Wenruo struct btrfs_backref_node *upper; 32081b60d2ecSQu Wenruo struct btrfs_backref_node *lower; 32091b60d2ecSQu Wenruo struct btrfs_backref_edge *edge; 32101b60d2ecSQu Wenruo struct extent_buffer *eb; 32111b60d2ecSQu Wenruo struct btrfs_root *root; 32121b60d2ecSQu Wenruo struct rb_node *rb_node; 32131b60d2ecSQu Wenruo int level; 32141b60d2ecSQu Wenruo bool need_check = true; 32151b60d2ecSQu Wenruo int ret; 32161b60d2ecSQu Wenruo 321756e9357aSDavid Sterba root = btrfs_get_fs_root(fs_info, ref_key->offset, false); 32181b60d2ecSQu Wenruo if (IS_ERR(root)) 32191b60d2ecSQu Wenruo return PTR_ERR(root); 322092a7cc42SQu Wenruo if (!test_bit(BTRFS_ROOT_SHAREABLE, &root->state)) 32211b60d2ecSQu Wenruo cur->cowonly = 1; 32221b60d2ecSQu Wenruo 32231b60d2ecSQu Wenruo if (btrfs_root_level(&root->root_item) == cur->level) { 32241b60d2ecSQu Wenruo /* Tree root */ 32251b60d2ecSQu Wenruo ASSERT(btrfs_root_bytenr(&root->root_item) == cur->bytenr); 3226876de781SQu Wenruo /* 3227876de781SQu Wenruo * For reloc backref cache, we may ignore reloc root. But for 3228876de781SQu Wenruo * general purpose backref cache, we can't rely on 3229876de781SQu Wenruo * btrfs_should_ignore_reloc_root() as it may conflict with 3230876de781SQu Wenruo * current running relocation and lead to missing root. 3231876de781SQu Wenruo * 3232876de781SQu Wenruo * For general purpose backref cache, reloc root detection is 3233876de781SQu Wenruo * completely relying on direct backref (key->offset is parent 3234876de781SQu Wenruo * bytenr), thus only do such check for reloc cache. 3235876de781SQu Wenruo */ 3236876de781SQu Wenruo if (btrfs_should_ignore_reloc_root(root) && cache->is_reloc) { 32371b60d2ecSQu Wenruo btrfs_put_root(root); 32381b60d2ecSQu Wenruo list_add(&cur->list, &cache->useless_node); 32391b60d2ecSQu Wenruo } else { 32401b60d2ecSQu Wenruo cur->root = root; 32411b60d2ecSQu Wenruo } 32421b60d2ecSQu Wenruo return 0; 32431b60d2ecSQu Wenruo } 32441b60d2ecSQu Wenruo 32451b60d2ecSQu Wenruo level = cur->level + 1; 32461b60d2ecSQu Wenruo 32471b60d2ecSQu Wenruo /* Search the tree to find parent blocks referring to the block */ 32481b60d2ecSQu Wenruo path->search_commit_root = 1; 32491b60d2ecSQu Wenruo path->skip_locking = 1; 32501b60d2ecSQu Wenruo path->lowest_level = level; 32511b60d2ecSQu Wenruo ret = btrfs_search_slot(NULL, root, tree_key, path, 0, 0); 32521b60d2ecSQu Wenruo path->lowest_level = 0; 32531b60d2ecSQu Wenruo if (ret < 0) { 32541b60d2ecSQu Wenruo btrfs_put_root(root); 32551b60d2ecSQu Wenruo return ret; 32561b60d2ecSQu Wenruo } 32571b60d2ecSQu Wenruo if (ret > 0 && path->slots[level] > 0) 32581b60d2ecSQu Wenruo path->slots[level]--; 32591b60d2ecSQu Wenruo 32601b60d2ecSQu Wenruo eb = path->nodes[level]; 32611b60d2ecSQu Wenruo if (btrfs_node_blockptr(eb, path->slots[level]) != cur->bytenr) { 32621b60d2ecSQu Wenruo btrfs_err(fs_info, 32631b60d2ecSQu Wenruo "couldn't find block (%llu) (level %d) in tree (%llu) with key (%llu %u %llu)", 32641b60d2ecSQu Wenruo cur->bytenr, level - 1, root->root_key.objectid, 32651b60d2ecSQu Wenruo tree_key->objectid, tree_key->type, tree_key->offset); 32661b60d2ecSQu Wenruo btrfs_put_root(root); 32671b60d2ecSQu Wenruo ret = -ENOENT; 32681b60d2ecSQu Wenruo goto out; 32691b60d2ecSQu Wenruo } 32701b60d2ecSQu Wenruo lower = cur; 32711b60d2ecSQu Wenruo 32721b60d2ecSQu Wenruo /* Add all nodes and edges in the path */ 32731b60d2ecSQu Wenruo for (; level < BTRFS_MAX_LEVEL; level++) { 32741b60d2ecSQu Wenruo if (!path->nodes[level]) { 32751b60d2ecSQu Wenruo ASSERT(btrfs_root_bytenr(&root->root_item) == 32761b60d2ecSQu Wenruo lower->bytenr); 3277876de781SQu Wenruo /* Same as previous should_ignore_reloc_root() call */ 3278876de781SQu Wenruo if (btrfs_should_ignore_reloc_root(root) && 3279876de781SQu Wenruo cache->is_reloc) { 32801b60d2ecSQu Wenruo btrfs_put_root(root); 32811b60d2ecSQu Wenruo list_add(&lower->list, &cache->useless_node); 32821b60d2ecSQu Wenruo } else { 32831b60d2ecSQu Wenruo lower->root = root; 32841b60d2ecSQu Wenruo } 32851b60d2ecSQu Wenruo break; 32861b60d2ecSQu Wenruo } 32871b60d2ecSQu Wenruo 32881b60d2ecSQu Wenruo edge = btrfs_backref_alloc_edge(cache); 32891b60d2ecSQu Wenruo if (!edge) { 32901b60d2ecSQu Wenruo btrfs_put_root(root); 32911b60d2ecSQu Wenruo ret = -ENOMEM; 32921b60d2ecSQu Wenruo goto out; 32931b60d2ecSQu Wenruo } 32941b60d2ecSQu Wenruo 32951b60d2ecSQu Wenruo eb = path->nodes[level]; 32961b60d2ecSQu Wenruo rb_node = rb_simple_search(&cache->rb_root, eb->start); 32971b60d2ecSQu Wenruo if (!rb_node) { 32981b60d2ecSQu Wenruo upper = btrfs_backref_alloc_node(cache, eb->start, 32991b60d2ecSQu Wenruo lower->level + 1); 33001b60d2ecSQu Wenruo if (!upper) { 33011b60d2ecSQu Wenruo btrfs_put_root(root); 33021b60d2ecSQu Wenruo btrfs_backref_free_edge(cache, edge); 33031b60d2ecSQu Wenruo ret = -ENOMEM; 33041b60d2ecSQu Wenruo goto out; 33051b60d2ecSQu Wenruo } 33061b60d2ecSQu Wenruo upper->owner = btrfs_header_owner(eb); 330792a7cc42SQu Wenruo if (!test_bit(BTRFS_ROOT_SHAREABLE, &root->state)) 33081b60d2ecSQu Wenruo upper->cowonly = 1; 33091b60d2ecSQu Wenruo 33101b60d2ecSQu Wenruo /* 33111b60d2ecSQu Wenruo * If we know the block isn't shared we can avoid 33121b60d2ecSQu Wenruo * checking its backrefs. 33131b60d2ecSQu Wenruo */ 3314eb96e221SFilipe Manana if (btrfs_block_can_be_shared(trans, root, eb)) 33151b60d2ecSQu Wenruo upper->checked = 0; 33161b60d2ecSQu Wenruo else 33171b60d2ecSQu Wenruo upper->checked = 1; 33181b60d2ecSQu Wenruo 33191b60d2ecSQu Wenruo /* 33201b60d2ecSQu Wenruo * Add the block to pending list if we need to check its 33211b60d2ecSQu Wenruo * backrefs, we only do this once while walking up a 33221b60d2ecSQu Wenruo * tree as we will catch anything else later on. 33231b60d2ecSQu Wenruo */ 33241b60d2ecSQu Wenruo if (!upper->checked && need_check) { 33251b60d2ecSQu Wenruo need_check = false; 33261b60d2ecSQu Wenruo list_add_tail(&edge->list[UPPER], 33271b60d2ecSQu Wenruo &cache->pending_edge); 33281b60d2ecSQu Wenruo } else { 33291b60d2ecSQu Wenruo if (upper->checked) 33301b60d2ecSQu Wenruo need_check = true; 33311b60d2ecSQu Wenruo INIT_LIST_HEAD(&edge->list[UPPER]); 33321b60d2ecSQu Wenruo } 33331b60d2ecSQu Wenruo } else { 33341b60d2ecSQu Wenruo upper = rb_entry(rb_node, struct btrfs_backref_node, 33351b60d2ecSQu Wenruo rb_node); 33361b60d2ecSQu Wenruo ASSERT(upper->checked); 33371b60d2ecSQu Wenruo INIT_LIST_HEAD(&edge->list[UPPER]); 33381b60d2ecSQu Wenruo if (!upper->owner) 33391b60d2ecSQu Wenruo upper->owner = btrfs_header_owner(eb); 33401b60d2ecSQu Wenruo } 33411b60d2ecSQu Wenruo btrfs_backref_link_edge(edge, lower, upper, LINK_LOWER); 33421b60d2ecSQu Wenruo 33431b60d2ecSQu Wenruo if (rb_node) { 33441b60d2ecSQu Wenruo btrfs_put_root(root); 33451b60d2ecSQu Wenruo break; 33461b60d2ecSQu Wenruo } 33471b60d2ecSQu Wenruo lower = upper; 33481b60d2ecSQu Wenruo upper = NULL; 33491b60d2ecSQu Wenruo } 33501b60d2ecSQu Wenruo out: 33511b60d2ecSQu Wenruo btrfs_release_path(path); 33521b60d2ecSQu Wenruo return ret; 33531b60d2ecSQu Wenruo } 33541b60d2ecSQu Wenruo 33551b60d2ecSQu Wenruo /* 33561b60d2ecSQu Wenruo * Add backref node @cur into @cache. 33571b60d2ecSQu Wenruo * 33581b60d2ecSQu Wenruo * NOTE: Even if the function returned 0, @cur is not yet cached as its upper 33591b60d2ecSQu Wenruo * links aren't yet bi-directional. Needs to finish such links. 3360fc997ed0SQu Wenruo * Use btrfs_backref_finish_upper_links() to finish such linkage. 33611b60d2ecSQu Wenruo * 3362eb96e221SFilipe Manana * @trans: Transaction handle. 33631b60d2ecSQu Wenruo * @path: Released path for indirect tree backref lookup 33641b60d2ecSQu Wenruo * @iter: Released backref iter for extent tree search 33651b60d2ecSQu Wenruo * @node_key: The first key of the tree block 33661b60d2ecSQu Wenruo */ 3367eb96e221SFilipe Manana int btrfs_backref_add_tree_node(struct btrfs_trans_handle *trans, 3368eb96e221SFilipe Manana struct btrfs_backref_cache *cache, 33691b60d2ecSQu Wenruo struct btrfs_path *path, 33701b60d2ecSQu Wenruo struct btrfs_backref_iter *iter, 33711b60d2ecSQu Wenruo struct btrfs_key *node_key, 33721b60d2ecSQu Wenruo struct btrfs_backref_node *cur) 33731b60d2ecSQu Wenruo { 33741b60d2ecSQu Wenruo struct btrfs_backref_edge *edge; 33751b60d2ecSQu Wenruo struct btrfs_backref_node *exist; 33761b60d2ecSQu Wenruo int ret; 33771b60d2ecSQu Wenruo 33781b60d2ecSQu Wenruo ret = btrfs_backref_iter_start(iter, cur->bytenr); 33791b60d2ecSQu Wenruo if (ret < 0) 33801b60d2ecSQu Wenruo return ret; 33811b60d2ecSQu Wenruo /* 33821b60d2ecSQu Wenruo * We skip the first btrfs_tree_block_info, as we don't use the key 33831b60d2ecSQu Wenruo * stored in it, but fetch it from the tree block 33841b60d2ecSQu Wenruo */ 33851b60d2ecSQu Wenruo if (btrfs_backref_has_tree_block_info(iter)) { 33861b60d2ecSQu Wenruo ret = btrfs_backref_iter_next(iter); 33871b60d2ecSQu Wenruo if (ret < 0) 33881b60d2ecSQu Wenruo goto out; 33891b60d2ecSQu Wenruo /* No extra backref? This means the tree block is corrupted */ 33901b60d2ecSQu Wenruo if (ret > 0) { 33911b60d2ecSQu Wenruo ret = -EUCLEAN; 33921b60d2ecSQu Wenruo goto out; 33931b60d2ecSQu Wenruo } 33941b60d2ecSQu Wenruo } 33951b60d2ecSQu Wenruo WARN_ON(cur->checked); 33961b60d2ecSQu Wenruo if (!list_empty(&cur->upper)) { 33971b60d2ecSQu Wenruo /* 33981b60d2ecSQu Wenruo * The backref was added previously when processing backref of 33991b60d2ecSQu Wenruo * type BTRFS_TREE_BLOCK_REF_KEY 34001b60d2ecSQu Wenruo */ 34011b60d2ecSQu Wenruo ASSERT(list_is_singular(&cur->upper)); 34021b60d2ecSQu Wenruo edge = list_entry(cur->upper.next, struct btrfs_backref_edge, 34031b60d2ecSQu Wenruo list[LOWER]); 34041b60d2ecSQu Wenruo ASSERT(list_empty(&edge->list[UPPER])); 34051b60d2ecSQu Wenruo exist = edge->node[UPPER]; 34061b60d2ecSQu Wenruo /* 34071b60d2ecSQu Wenruo * Add the upper level block to pending list if we need check 34081b60d2ecSQu Wenruo * its backrefs 34091b60d2ecSQu Wenruo */ 34101b60d2ecSQu Wenruo if (!exist->checked) 34111b60d2ecSQu Wenruo list_add_tail(&edge->list[UPPER], &cache->pending_edge); 34121b60d2ecSQu Wenruo } else { 34131b60d2ecSQu Wenruo exist = NULL; 34141b60d2ecSQu Wenruo } 34151b60d2ecSQu Wenruo 34161b60d2ecSQu Wenruo for (; ret == 0; ret = btrfs_backref_iter_next(iter)) { 34171b60d2ecSQu Wenruo struct extent_buffer *eb; 34181b60d2ecSQu Wenruo struct btrfs_key key; 34191b60d2ecSQu Wenruo int type; 34201b60d2ecSQu Wenruo 34211b60d2ecSQu Wenruo cond_resched(); 34221b60d2ecSQu Wenruo eb = btrfs_backref_get_eb(iter); 34231b60d2ecSQu Wenruo 34241b60d2ecSQu Wenruo key.objectid = iter->bytenr; 34251b60d2ecSQu Wenruo if (btrfs_backref_iter_is_inline_ref(iter)) { 34261b60d2ecSQu Wenruo struct btrfs_extent_inline_ref *iref; 34271b60d2ecSQu Wenruo 34281b60d2ecSQu Wenruo /* Update key for inline backref */ 34291b60d2ecSQu Wenruo iref = (struct btrfs_extent_inline_ref *) 34301b60d2ecSQu Wenruo ((unsigned long)iter->cur_ptr); 34311b60d2ecSQu Wenruo type = btrfs_get_extent_inline_ref_type(eb, iref, 34321b60d2ecSQu Wenruo BTRFS_REF_TYPE_BLOCK); 34331b60d2ecSQu Wenruo if (type == BTRFS_REF_TYPE_INVALID) { 34341b60d2ecSQu Wenruo ret = -EUCLEAN; 34351b60d2ecSQu Wenruo goto out; 34361b60d2ecSQu Wenruo } 34371b60d2ecSQu Wenruo key.type = type; 34381b60d2ecSQu Wenruo key.offset = btrfs_extent_inline_ref_offset(eb, iref); 34391b60d2ecSQu Wenruo } else { 34401b60d2ecSQu Wenruo key.type = iter->cur_key.type; 34411b60d2ecSQu Wenruo key.offset = iter->cur_key.offset; 34421b60d2ecSQu Wenruo } 34431b60d2ecSQu Wenruo 34441b60d2ecSQu Wenruo /* 34451b60d2ecSQu Wenruo * Parent node found and matches current inline ref, no need to 34461b60d2ecSQu Wenruo * rebuild this node for this inline ref 34471b60d2ecSQu Wenruo */ 34481b60d2ecSQu Wenruo if (exist && 34491b60d2ecSQu Wenruo ((key.type == BTRFS_TREE_BLOCK_REF_KEY && 34501b60d2ecSQu Wenruo exist->owner == key.offset) || 34511b60d2ecSQu Wenruo (key.type == BTRFS_SHARED_BLOCK_REF_KEY && 34521b60d2ecSQu Wenruo exist->bytenr == key.offset))) { 34531b60d2ecSQu Wenruo exist = NULL; 34541b60d2ecSQu Wenruo continue; 34551b60d2ecSQu Wenruo } 34561b60d2ecSQu Wenruo 34571b60d2ecSQu Wenruo /* SHARED_BLOCK_REF means key.offset is the parent bytenr */ 34581b60d2ecSQu Wenruo if (key.type == BTRFS_SHARED_BLOCK_REF_KEY) { 34591b60d2ecSQu Wenruo ret = handle_direct_tree_backref(cache, &key, cur); 34601b60d2ecSQu Wenruo if (ret < 0) 34611b60d2ecSQu Wenruo goto out; 3462182741d2SQu Wenruo } else if (key.type == BTRFS_TREE_BLOCK_REF_KEY) { 34631b60d2ecSQu Wenruo /* 3464182741d2SQu Wenruo * key.type == BTRFS_TREE_BLOCK_REF_KEY, inline ref 3465182741d2SQu Wenruo * offset means the root objectid. We need to search 3466182741d2SQu Wenruo * the tree to get its parent bytenr. 34671b60d2ecSQu Wenruo */ 3468eb96e221SFilipe Manana ret = handle_indirect_tree_backref(trans, cache, path, 3469eb96e221SFilipe Manana &key, node_key, cur); 34701b60d2ecSQu Wenruo if (ret < 0) 34711b60d2ecSQu Wenruo goto out; 34721b60d2ecSQu Wenruo } 3473182741d2SQu Wenruo /* 3474182741d2SQu Wenruo * Unrecognized tree backref items (if it can pass tree-checker) 3475182741d2SQu Wenruo * would be ignored. 3476182741d2SQu Wenruo */ 3477182741d2SQu Wenruo } 34781b60d2ecSQu Wenruo ret = 0; 34791b60d2ecSQu Wenruo cur->checked = 1; 34801b60d2ecSQu Wenruo WARN_ON(exist); 34811b60d2ecSQu Wenruo out: 34821b60d2ecSQu Wenruo btrfs_backref_iter_release(iter); 34831b60d2ecSQu Wenruo return ret; 34841b60d2ecSQu Wenruo } 3485fc997ed0SQu Wenruo 3486fc997ed0SQu Wenruo /* 3487fc997ed0SQu Wenruo * Finish the upwards linkage created by btrfs_backref_add_tree_node() 3488fc997ed0SQu Wenruo */ 3489fc997ed0SQu Wenruo int btrfs_backref_finish_upper_links(struct btrfs_backref_cache *cache, 3490fc997ed0SQu Wenruo struct btrfs_backref_node *start) 3491fc997ed0SQu Wenruo { 3492fc997ed0SQu Wenruo struct list_head *useless_node = &cache->useless_node; 3493fc997ed0SQu Wenruo struct btrfs_backref_edge *edge; 3494fc997ed0SQu Wenruo struct rb_node *rb_node; 3495fc997ed0SQu Wenruo LIST_HEAD(pending_edge); 3496fc997ed0SQu Wenruo 3497fc997ed0SQu Wenruo ASSERT(start->checked); 3498fc997ed0SQu Wenruo 3499fc997ed0SQu Wenruo /* Insert this node to cache if it's not COW-only */ 3500fc997ed0SQu Wenruo if (!start->cowonly) { 3501fc997ed0SQu Wenruo rb_node = rb_simple_insert(&cache->rb_root, start->bytenr, 3502fc997ed0SQu Wenruo &start->rb_node); 3503fc997ed0SQu Wenruo if (rb_node) 3504fc997ed0SQu Wenruo btrfs_backref_panic(cache->fs_info, start->bytenr, 3505fc997ed0SQu Wenruo -EEXIST); 3506fc997ed0SQu Wenruo list_add_tail(&start->lower, &cache->leaves); 3507fc997ed0SQu Wenruo } 3508fc997ed0SQu Wenruo 3509fc997ed0SQu Wenruo /* 3510fc997ed0SQu Wenruo * Use breadth first search to iterate all related edges. 3511fc997ed0SQu Wenruo * 3512fc997ed0SQu Wenruo * The starting points are all the edges of this node 3513fc997ed0SQu Wenruo */ 3514fc997ed0SQu Wenruo list_for_each_entry(edge, &start->upper, list[LOWER]) 3515fc997ed0SQu Wenruo list_add_tail(&edge->list[UPPER], &pending_edge); 3516fc997ed0SQu Wenruo 3517fc997ed0SQu Wenruo while (!list_empty(&pending_edge)) { 3518fc997ed0SQu Wenruo struct btrfs_backref_node *upper; 3519fc997ed0SQu Wenruo struct btrfs_backref_node *lower; 3520fc997ed0SQu Wenruo 3521fc997ed0SQu Wenruo edge = list_first_entry(&pending_edge, 3522fc997ed0SQu Wenruo struct btrfs_backref_edge, list[UPPER]); 3523fc997ed0SQu Wenruo list_del_init(&edge->list[UPPER]); 3524fc997ed0SQu Wenruo upper = edge->node[UPPER]; 3525fc997ed0SQu Wenruo lower = edge->node[LOWER]; 3526fc997ed0SQu Wenruo 3527fc997ed0SQu Wenruo /* Parent is detached, no need to keep any edges */ 3528fc997ed0SQu Wenruo if (upper->detached) { 3529fc997ed0SQu Wenruo list_del(&edge->list[LOWER]); 3530fc997ed0SQu Wenruo btrfs_backref_free_edge(cache, edge); 3531fc997ed0SQu Wenruo 3532fc997ed0SQu Wenruo /* Lower node is orphan, queue for cleanup */ 3533fc997ed0SQu Wenruo if (list_empty(&lower->upper)) 3534fc997ed0SQu Wenruo list_add(&lower->list, useless_node); 3535fc997ed0SQu Wenruo continue; 3536fc997ed0SQu Wenruo } 3537fc997ed0SQu Wenruo 3538fc997ed0SQu Wenruo /* 3539fc997ed0SQu Wenruo * All new nodes added in current build_backref_tree() haven't 3540fc997ed0SQu Wenruo * been linked to the cache rb tree. 3541fc997ed0SQu Wenruo * So if we have upper->rb_node populated, this means a cache 3542fc997ed0SQu Wenruo * hit. We only need to link the edge, as @upper and all its 3543fc997ed0SQu Wenruo * parents have already been linked. 3544fc997ed0SQu Wenruo */ 3545fc997ed0SQu Wenruo if (!RB_EMPTY_NODE(&upper->rb_node)) { 3546fc997ed0SQu Wenruo if (upper->lowest) { 3547fc997ed0SQu Wenruo list_del_init(&upper->lower); 3548fc997ed0SQu Wenruo upper->lowest = 0; 3549fc997ed0SQu Wenruo } 3550fc997ed0SQu Wenruo 3551fc997ed0SQu Wenruo list_add_tail(&edge->list[UPPER], &upper->lower); 3552fc997ed0SQu Wenruo continue; 3553fc997ed0SQu Wenruo } 3554fc997ed0SQu Wenruo 3555fc997ed0SQu Wenruo /* Sanity check, we shouldn't have any unchecked nodes */ 3556fc997ed0SQu Wenruo if (!upper->checked) { 3557fc997ed0SQu Wenruo ASSERT(0); 3558fc997ed0SQu Wenruo return -EUCLEAN; 3559fc997ed0SQu Wenruo } 3560fc997ed0SQu Wenruo 3561fc997ed0SQu Wenruo /* Sanity check, COW-only node has non-COW-only parent */ 3562fc997ed0SQu Wenruo if (start->cowonly != upper->cowonly) { 3563fc997ed0SQu Wenruo ASSERT(0); 3564fc997ed0SQu Wenruo return -EUCLEAN; 3565fc997ed0SQu Wenruo } 3566fc997ed0SQu Wenruo 3567fc997ed0SQu Wenruo /* Only cache non-COW-only (subvolume trees) tree blocks */ 3568fc997ed0SQu Wenruo if (!upper->cowonly) { 3569fc997ed0SQu Wenruo rb_node = rb_simple_insert(&cache->rb_root, upper->bytenr, 3570fc997ed0SQu Wenruo &upper->rb_node); 3571fc997ed0SQu Wenruo if (rb_node) { 3572fc997ed0SQu Wenruo btrfs_backref_panic(cache->fs_info, 3573fc997ed0SQu Wenruo upper->bytenr, -EEXIST); 3574fc997ed0SQu Wenruo return -EUCLEAN; 3575fc997ed0SQu Wenruo } 3576fc997ed0SQu Wenruo } 3577fc997ed0SQu Wenruo 3578fc997ed0SQu Wenruo list_add_tail(&edge->list[UPPER], &upper->lower); 3579fc997ed0SQu Wenruo 3580fc997ed0SQu Wenruo /* 3581fc997ed0SQu Wenruo * Also queue all the parent edges of this uncached node 3582fc997ed0SQu Wenruo * to finish the upper linkage 3583fc997ed0SQu Wenruo */ 3584fc997ed0SQu Wenruo list_for_each_entry(edge, &upper->upper, list[LOWER]) 3585fc997ed0SQu Wenruo list_add_tail(&edge->list[UPPER], &pending_edge); 3586fc997ed0SQu Wenruo } 3587fc997ed0SQu Wenruo return 0; 3588fc997ed0SQu Wenruo } 35891b23ea18SQu Wenruo 35901b23ea18SQu Wenruo void btrfs_backref_error_cleanup(struct btrfs_backref_cache *cache, 35911b23ea18SQu Wenruo struct btrfs_backref_node *node) 35921b23ea18SQu Wenruo { 35931b23ea18SQu Wenruo struct btrfs_backref_node *lower; 35941b23ea18SQu Wenruo struct btrfs_backref_node *upper; 35951b23ea18SQu Wenruo struct btrfs_backref_edge *edge; 35961b23ea18SQu Wenruo 35971b23ea18SQu Wenruo while (!list_empty(&cache->useless_node)) { 35981b23ea18SQu Wenruo lower = list_first_entry(&cache->useless_node, 35991b23ea18SQu Wenruo struct btrfs_backref_node, list); 36001b23ea18SQu Wenruo list_del_init(&lower->list); 36011b23ea18SQu Wenruo } 36021b23ea18SQu Wenruo while (!list_empty(&cache->pending_edge)) { 36031b23ea18SQu Wenruo edge = list_first_entry(&cache->pending_edge, 36041b23ea18SQu Wenruo struct btrfs_backref_edge, list[UPPER]); 36051b23ea18SQu Wenruo list_del(&edge->list[UPPER]); 36061b23ea18SQu Wenruo list_del(&edge->list[LOWER]); 36071b23ea18SQu Wenruo lower = edge->node[LOWER]; 36081b23ea18SQu Wenruo upper = edge->node[UPPER]; 36091b23ea18SQu Wenruo btrfs_backref_free_edge(cache, edge); 36101b23ea18SQu Wenruo 36111b23ea18SQu Wenruo /* 36121b23ea18SQu Wenruo * Lower is no longer linked to any upper backref nodes and 36131b23ea18SQu Wenruo * isn't in the cache, we can free it ourselves. 36141b23ea18SQu Wenruo */ 36151b23ea18SQu Wenruo if (list_empty(&lower->upper) && 36161b23ea18SQu Wenruo RB_EMPTY_NODE(&lower->rb_node)) 36171b23ea18SQu Wenruo list_add(&lower->list, &cache->useless_node); 36181b23ea18SQu Wenruo 36191b23ea18SQu Wenruo if (!RB_EMPTY_NODE(&upper->rb_node)) 36201b23ea18SQu Wenruo continue; 36211b23ea18SQu Wenruo 36221b23ea18SQu Wenruo /* Add this guy's upper edges to the list to process */ 36231b23ea18SQu Wenruo list_for_each_entry(edge, &upper->upper, list[LOWER]) 36241b23ea18SQu Wenruo list_add_tail(&edge->list[UPPER], 36251b23ea18SQu Wenruo &cache->pending_edge); 36261b23ea18SQu Wenruo if (list_empty(&upper->upper)) 36271b23ea18SQu Wenruo list_add(&upper->list, &cache->useless_node); 36281b23ea18SQu Wenruo } 36291b23ea18SQu Wenruo 36301b23ea18SQu Wenruo while (!list_empty(&cache->useless_node)) { 36311b23ea18SQu Wenruo lower = list_first_entry(&cache->useless_node, 36321b23ea18SQu Wenruo struct btrfs_backref_node, list); 36331b23ea18SQu Wenruo list_del_init(&lower->list); 36341b23ea18SQu Wenruo if (lower == node) 36351b23ea18SQu Wenruo node = NULL; 363649ecc679SJosef Bacik btrfs_backref_drop_node(cache, lower); 36371b23ea18SQu Wenruo } 36381b23ea18SQu Wenruo 36391b23ea18SQu Wenruo btrfs_backref_cleanup_node(cache, node); 36401b23ea18SQu Wenruo ASSERT(list_empty(&cache->useless_node) && 36411b23ea18SQu Wenruo list_empty(&cache->pending_edge)); 36421b23ea18SQu Wenruo } 3643