162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 262306a36Sopenharmony_ci/* 362306a36Sopenharmony_ci * Copyright (C) 2011 STRATO. All rights reserved. 462306a36Sopenharmony_ci */ 562306a36Sopenharmony_ci 662306a36Sopenharmony_ci#include <linux/mm.h> 762306a36Sopenharmony_ci#include <linux/rbtree.h> 862306a36Sopenharmony_ci#include <trace/events/btrfs.h> 962306a36Sopenharmony_ci#include "ctree.h" 1062306a36Sopenharmony_ci#include "disk-io.h" 1162306a36Sopenharmony_ci#include "backref.h" 1262306a36Sopenharmony_ci#include "ulist.h" 1362306a36Sopenharmony_ci#include "transaction.h" 1462306a36Sopenharmony_ci#include "delayed-ref.h" 1562306a36Sopenharmony_ci#include "locking.h" 1662306a36Sopenharmony_ci#include "misc.h" 1762306a36Sopenharmony_ci#include "tree-mod-log.h" 1862306a36Sopenharmony_ci#include "fs.h" 1962306a36Sopenharmony_ci#include "accessors.h" 2062306a36Sopenharmony_ci#include "extent-tree.h" 2162306a36Sopenharmony_ci#include "relocation.h" 2262306a36Sopenharmony_ci#include "tree-checker.h" 2362306a36Sopenharmony_ci 2462306a36Sopenharmony_ci/* Just arbitrary numbers so we can be sure one of these happened. */ 2562306a36Sopenharmony_ci#define BACKREF_FOUND_SHARED 6 2662306a36Sopenharmony_ci#define BACKREF_FOUND_NOT_SHARED 7 2762306a36Sopenharmony_ci 2862306a36Sopenharmony_cistruct extent_inode_elem { 2962306a36Sopenharmony_ci u64 inum; 3062306a36Sopenharmony_ci u64 offset; 3162306a36Sopenharmony_ci u64 num_bytes; 3262306a36Sopenharmony_ci struct extent_inode_elem *next; 3362306a36Sopenharmony_ci}; 3462306a36Sopenharmony_ci 3562306a36Sopenharmony_cistatic int check_extent_in_eb(struct btrfs_backref_walk_ctx *ctx, 3662306a36Sopenharmony_ci const struct btrfs_key *key, 3762306a36Sopenharmony_ci const struct extent_buffer *eb, 3862306a36Sopenharmony_ci const struct btrfs_file_extent_item *fi, 3962306a36Sopenharmony_ci struct extent_inode_elem **eie) 4062306a36Sopenharmony_ci{ 4162306a36Sopenharmony_ci const u64 data_len = btrfs_file_extent_num_bytes(eb, fi); 4262306a36Sopenharmony_ci u64 offset = key->offset; 4362306a36Sopenharmony_ci struct extent_inode_elem *e; 4462306a36Sopenharmony_ci const u64 *root_ids; 4562306a36Sopenharmony_ci int root_count; 4662306a36Sopenharmony_ci bool cached; 4762306a36Sopenharmony_ci 4862306a36Sopenharmony_ci if (!ctx->ignore_extent_item_pos && 4962306a36Sopenharmony_ci !btrfs_file_extent_compression(eb, fi) && 5062306a36Sopenharmony_ci !btrfs_file_extent_encryption(eb, fi) && 5162306a36Sopenharmony_ci !btrfs_file_extent_other_encoding(eb, fi)) { 5262306a36Sopenharmony_ci u64 data_offset; 5362306a36Sopenharmony_ci 5462306a36Sopenharmony_ci data_offset = btrfs_file_extent_offset(eb, fi); 5562306a36Sopenharmony_ci 5662306a36Sopenharmony_ci if (ctx->extent_item_pos < data_offset || 5762306a36Sopenharmony_ci ctx->extent_item_pos >= data_offset + data_len) 5862306a36Sopenharmony_ci return 1; 5962306a36Sopenharmony_ci offset += ctx->extent_item_pos - data_offset; 6062306a36Sopenharmony_ci } 6162306a36Sopenharmony_ci 6262306a36Sopenharmony_ci if (!ctx->indirect_ref_iterator || !ctx->cache_lookup) 6362306a36Sopenharmony_ci goto add_inode_elem; 6462306a36Sopenharmony_ci 6562306a36Sopenharmony_ci cached = ctx->cache_lookup(eb->start, ctx->user_ctx, &root_ids, 6662306a36Sopenharmony_ci &root_count); 6762306a36Sopenharmony_ci if (!cached) 6862306a36Sopenharmony_ci goto add_inode_elem; 6962306a36Sopenharmony_ci 7062306a36Sopenharmony_ci for (int i = 0; i < root_count; i++) { 7162306a36Sopenharmony_ci int ret; 7262306a36Sopenharmony_ci 7362306a36Sopenharmony_ci ret = ctx->indirect_ref_iterator(key->objectid, offset, 7462306a36Sopenharmony_ci data_len, root_ids[i], 7562306a36Sopenharmony_ci ctx->user_ctx); 7662306a36Sopenharmony_ci if (ret) 7762306a36Sopenharmony_ci return ret; 7862306a36Sopenharmony_ci } 7962306a36Sopenharmony_ci 8062306a36Sopenharmony_ciadd_inode_elem: 8162306a36Sopenharmony_ci e = kmalloc(sizeof(*e), GFP_NOFS); 8262306a36Sopenharmony_ci if (!e) 8362306a36Sopenharmony_ci return -ENOMEM; 8462306a36Sopenharmony_ci 8562306a36Sopenharmony_ci e->next = *eie; 8662306a36Sopenharmony_ci e->inum = key->objectid; 8762306a36Sopenharmony_ci e->offset = offset; 8862306a36Sopenharmony_ci e->num_bytes = data_len; 8962306a36Sopenharmony_ci *eie = e; 9062306a36Sopenharmony_ci 9162306a36Sopenharmony_ci return 0; 9262306a36Sopenharmony_ci} 9362306a36Sopenharmony_ci 9462306a36Sopenharmony_cistatic void free_inode_elem_list(struct extent_inode_elem *eie) 9562306a36Sopenharmony_ci{ 9662306a36Sopenharmony_ci struct extent_inode_elem *eie_next; 9762306a36Sopenharmony_ci 9862306a36Sopenharmony_ci for (; eie; eie = eie_next) { 9962306a36Sopenharmony_ci eie_next = eie->next; 10062306a36Sopenharmony_ci kfree(eie); 10162306a36Sopenharmony_ci } 10262306a36Sopenharmony_ci} 10362306a36Sopenharmony_ci 10462306a36Sopenharmony_cistatic int find_extent_in_eb(struct btrfs_backref_walk_ctx *ctx, 10562306a36Sopenharmony_ci const struct extent_buffer *eb, 10662306a36Sopenharmony_ci struct extent_inode_elem **eie) 10762306a36Sopenharmony_ci{ 10862306a36Sopenharmony_ci u64 disk_byte; 10962306a36Sopenharmony_ci struct btrfs_key key; 11062306a36Sopenharmony_ci struct btrfs_file_extent_item *fi; 11162306a36Sopenharmony_ci int slot; 11262306a36Sopenharmony_ci int nritems; 11362306a36Sopenharmony_ci int extent_type; 11462306a36Sopenharmony_ci int ret; 11562306a36Sopenharmony_ci 11662306a36Sopenharmony_ci /* 11762306a36Sopenharmony_ci * from the shared data ref, we only have the leaf but we need 11862306a36Sopenharmony_ci * the key. thus, we must look into all items and see that we 11962306a36Sopenharmony_ci * find one (some) with a reference to our extent item. 12062306a36Sopenharmony_ci */ 12162306a36Sopenharmony_ci nritems = btrfs_header_nritems(eb); 12262306a36Sopenharmony_ci for (slot = 0; slot < nritems; ++slot) { 12362306a36Sopenharmony_ci btrfs_item_key_to_cpu(eb, &key, slot); 12462306a36Sopenharmony_ci if (key.type != BTRFS_EXTENT_DATA_KEY) 12562306a36Sopenharmony_ci continue; 12662306a36Sopenharmony_ci fi = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item); 12762306a36Sopenharmony_ci extent_type = btrfs_file_extent_type(eb, fi); 12862306a36Sopenharmony_ci if (extent_type == BTRFS_FILE_EXTENT_INLINE) 12962306a36Sopenharmony_ci continue; 13062306a36Sopenharmony_ci /* don't skip BTRFS_FILE_EXTENT_PREALLOC, we can handle that */ 13162306a36Sopenharmony_ci disk_byte = btrfs_file_extent_disk_bytenr(eb, fi); 13262306a36Sopenharmony_ci if (disk_byte != ctx->bytenr) 13362306a36Sopenharmony_ci continue; 13462306a36Sopenharmony_ci 13562306a36Sopenharmony_ci ret = check_extent_in_eb(ctx, &key, eb, fi, eie); 13662306a36Sopenharmony_ci if (ret == BTRFS_ITERATE_EXTENT_INODES_STOP || ret < 0) 13762306a36Sopenharmony_ci return ret; 13862306a36Sopenharmony_ci } 13962306a36Sopenharmony_ci 14062306a36Sopenharmony_ci return 0; 14162306a36Sopenharmony_ci} 14262306a36Sopenharmony_ci 14362306a36Sopenharmony_cistruct preftree { 14462306a36Sopenharmony_ci struct rb_root_cached root; 14562306a36Sopenharmony_ci unsigned int count; 14662306a36Sopenharmony_ci}; 14762306a36Sopenharmony_ci 14862306a36Sopenharmony_ci#define PREFTREE_INIT { .root = RB_ROOT_CACHED, .count = 0 } 14962306a36Sopenharmony_ci 15062306a36Sopenharmony_cistruct preftrees { 15162306a36Sopenharmony_ci struct preftree direct; /* BTRFS_SHARED_[DATA|BLOCK]_REF_KEY */ 15262306a36Sopenharmony_ci struct preftree indirect; /* BTRFS_[TREE_BLOCK|EXTENT_DATA]_REF_KEY */ 15362306a36Sopenharmony_ci struct preftree indirect_missing_keys; 15462306a36Sopenharmony_ci}; 15562306a36Sopenharmony_ci 15662306a36Sopenharmony_ci/* 15762306a36Sopenharmony_ci * Checks for a shared extent during backref search. 15862306a36Sopenharmony_ci * 15962306a36Sopenharmony_ci * The share_count tracks prelim_refs (direct and indirect) having a 16062306a36Sopenharmony_ci * ref->count >0: 16162306a36Sopenharmony_ci * - incremented when a ref->count transitions to >0 16262306a36Sopenharmony_ci * - decremented when a ref->count transitions to <1 16362306a36Sopenharmony_ci */ 16462306a36Sopenharmony_cistruct share_check { 16562306a36Sopenharmony_ci struct btrfs_backref_share_check_ctx *ctx; 16662306a36Sopenharmony_ci struct btrfs_root *root; 16762306a36Sopenharmony_ci u64 inum; 16862306a36Sopenharmony_ci u64 data_bytenr; 16962306a36Sopenharmony_ci u64 data_extent_gen; 17062306a36Sopenharmony_ci /* 17162306a36Sopenharmony_ci * Counts number of inodes that refer to an extent (different inodes in 17262306a36Sopenharmony_ci * the same root or different roots) that we could find. The sharedness 17362306a36Sopenharmony_ci * check typically stops once this counter gets greater than 1, so it 17462306a36Sopenharmony_ci * may not reflect the total number of inodes. 17562306a36Sopenharmony_ci */ 17662306a36Sopenharmony_ci int share_count; 17762306a36Sopenharmony_ci /* 17862306a36Sopenharmony_ci * The number of times we found our inode refers to the data extent we 17962306a36Sopenharmony_ci * are determining the sharedness. In other words, how many file extent 18062306a36Sopenharmony_ci * items we could find for our inode that point to our target data 18162306a36Sopenharmony_ci * extent. The value we get here after finishing the extent sharedness 18262306a36Sopenharmony_ci * check may be smaller than reality, but if it ends up being greater 18362306a36Sopenharmony_ci * than 1, then we know for sure the inode has multiple file extent 18462306a36Sopenharmony_ci * items that point to our inode, and we can safely assume it's useful 18562306a36Sopenharmony_ci * to cache the sharedness check result. 18662306a36Sopenharmony_ci */ 18762306a36Sopenharmony_ci int self_ref_count; 18862306a36Sopenharmony_ci bool have_delayed_delete_refs; 18962306a36Sopenharmony_ci}; 19062306a36Sopenharmony_ci 19162306a36Sopenharmony_cistatic inline int extent_is_shared(struct share_check *sc) 19262306a36Sopenharmony_ci{ 19362306a36Sopenharmony_ci return (sc && sc->share_count > 1) ? BACKREF_FOUND_SHARED : 0; 19462306a36Sopenharmony_ci} 19562306a36Sopenharmony_ci 19662306a36Sopenharmony_cistatic struct kmem_cache *btrfs_prelim_ref_cache; 19762306a36Sopenharmony_ci 19862306a36Sopenharmony_ciint __init btrfs_prelim_ref_init(void) 19962306a36Sopenharmony_ci{ 20062306a36Sopenharmony_ci btrfs_prelim_ref_cache = kmem_cache_create("btrfs_prelim_ref", 20162306a36Sopenharmony_ci sizeof(struct prelim_ref), 20262306a36Sopenharmony_ci 0, 20362306a36Sopenharmony_ci SLAB_MEM_SPREAD, 20462306a36Sopenharmony_ci NULL); 20562306a36Sopenharmony_ci if (!btrfs_prelim_ref_cache) 20662306a36Sopenharmony_ci return -ENOMEM; 20762306a36Sopenharmony_ci return 0; 20862306a36Sopenharmony_ci} 20962306a36Sopenharmony_ci 21062306a36Sopenharmony_civoid __cold btrfs_prelim_ref_exit(void) 21162306a36Sopenharmony_ci{ 21262306a36Sopenharmony_ci kmem_cache_destroy(btrfs_prelim_ref_cache); 21362306a36Sopenharmony_ci} 21462306a36Sopenharmony_ci 21562306a36Sopenharmony_cistatic void free_pref(struct prelim_ref *ref) 21662306a36Sopenharmony_ci{ 21762306a36Sopenharmony_ci kmem_cache_free(btrfs_prelim_ref_cache, ref); 21862306a36Sopenharmony_ci} 21962306a36Sopenharmony_ci 22062306a36Sopenharmony_ci/* 22162306a36Sopenharmony_ci * Return 0 when both refs are for the same block (and can be merged). 22262306a36Sopenharmony_ci * A -1 return indicates ref1 is a 'lower' block than ref2, while 1 22362306a36Sopenharmony_ci * indicates a 'higher' block. 22462306a36Sopenharmony_ci */ 22562306a36Sopenharmony_cistatic int prelim_ref_compare(struct prelim_ref *ref1, 22662306a36Sopenharmony_ci struct prelim_ref *ref2) 22762306a36Sopenharmony_ci{ 22862306a36Sopenharmony_ci if (ref1->level < ref2->level) 22962306a36Sopenharmony_ci return -1; 23062306a36Sopenharmony_ci if (ref1->level > ref2->level) 23162306a36Sopenharmony_ci return 1; 23262306a36Sopenharmony_ci if (ref1->root_id < ref2->root_id) 23362306a36Sopenharmony_ci return -1; 23462306a36Sopenharmony_ci if (ref1->root_id > ref2->root_id) 23562306a36Sopenharmony_ci return 1; 23662306a36Sopenharmony_ci if (ref1->key_for_search.type < ref2->key_for_search.type) 23762306a36Sopenharmony_ci return -1; 23862306a36Sopenharmony_ci if (ref1->key_for_search.type > ref2->key_for_search.type) 23962306a36Sopenharmony_ci return 1; 24062306a36Sopenharmony_ci if (ref1->key_for_search.objectid < ref2->key_for_search.objectid) 24162306a36Sopenharmony_ci return -1; 24262306a36Sopenharmony_ci if (ref1->key_for_search.objectid > ref2->key_for_search.objectid) 24362306a36Sopenharmony_ci return 1; 24462306a36Sopenharmony_ci if (ref1->key_for_search.offset < ref2->key_for_search.offset) 24562306a36Sopenharmony_ci return -1; 24662306a36Sopenharmony_ci if (ref1->key_for_search.offset > ref2->key_for_search.offset) 24762306a36Sopenharmony_ci return 1; 24862306a36Sopenharmony_ci if (ref1->parent < ref2->parent) 24962306a36Sopenharmony_ci return -1; 25062306a36Sopenharmony_ci if (ref1->parent > ref2->parent) 25162306a36Sopenharmony_ci return 1; 25262306a36Sopenharmony_ci 25362306a36Sopenharmony_ci return 0; 25462306a36Sopenharmony_ci} 25562306a36Sopenharmony_ci 25662306a36Sopenharmony_cistatic void update_share_count(struct share_check *sc, int oldcount, 25762306a36Sopenharmony_ci int newcount, struct prelim_ref *newref) 25862306a36Sopenharmony_ci{ 25962306a36Sopenharmony_ci if ((!sc) || (oldcount == 0 && newcount < 1)) 26062306a36Sopenharmony_ci return; 26162306a36Sopenharmony_ci 26262306a36Sopenharmony_ci if (oldcount > 0 && newcount < 1) 26362306a36Sopenharmony_ci sc->share_count--; 26462306a36Sopenharmony_ci else if (oldcount < 1 && newcount > 0) 26562306a36Sopenharmony_ci sc->share_count++; 26662306a36Sopenharmony_ci 26762306a36Sopenharmony_ci if (newref->root_id == sc->root->root_key.objectid && 26862306a36Sopenharmony_ci newref->wanted_disk_byte == sc->data_bytenr && 26962306a36Sopenharmony_ci newref->key_for_search.objectid == sc->inum) 27062306a36Sopenharmony_ci sc->self_ref_count += newref->count; 27162306a36Sopenharmony_ci} 27262306a36Sopenharmony_ci 27362306a36Sopenharmony_ci/* 27462306a36Sopenharmony_ci * Add @newref to the @root rbtree, merging identical refs. 27562306a36Sopenharmony_ci * 27662306a36Sopenharmony_ci * Callers should assume that newref has been freed after calling. 27762306a36Sopenharmony_ci */ 27862306a36Sopenharmony_cistatic void prelim_ref_insert(const struct btrfs_fs_info *fs_info, 27962306a36Sopenharmony_ci struct preftree *preftree, 28062306a36Sopenharmony_ci struct prelim_ref *newref, 28162306a36Sopenharmony_ci struct share_check *sc) 28262306a36Sopenharmony_ci{ 28362306a36Sopenharmony_ci struct rb_root_cached *root; 28462306a36Sopenharmony_ci struct rb_node **p; 28562306a36Sopenharmony_ci struct rb_node *parent = NULL; 28662306a36Sopenharmony_ci struct prelim_ref *ref; 28762306a36Sopenharmony_ci int result; 28862306a36Sopenharmony_ci bool leftmost = true; 28962306a36Sopenharmony_ci 29062306a36Sopenharmony_ci root = &preftree->root; 29162306a36Sopenharmony_ci p = &root->rb_root.rb_node; 29262306a36Sopenharmony_ci 29362306a36Sopenharmony_ci while (*p) { 29462306a36Sopenharmony_ci parent = *p; 29562306a36Sopenharmony_ci ref = rb_entry(parent, struct prelim_ref, rbnode); 29662306a36Sopenharmony_ci result = prelim_ref_compare(ref, newref); 29762306a36Sopenharmony_ci if (result < 0) { 29862306a36Sopenharmony_ci p = &(*p)->rb_left; 29962306a36Sopenharmony_ci } else if (result > 0) { 30062306a36Sopenharmony_ci p = &(*p)->rb_right; 30162306a36Sopenharmony_ci leftmost = false; 30262306a36Sopenharmony_ci } else { 30362306a36Sopenharmony_ci /* Identical refs, merge them and free @newref */ 30462306a36Sopenharmony_ci struct extent_inode_elem *eie = ref->inode_list; 30562306a36Sopenharmony_ci 30662306a36Sopenharmony_ci while (eie && eie->next) 30762306a36Sopenharmony_ci eie = eie->next; 30862306a36Sopenharmony_ci 30962306a36Sopenharmony_ci if (!eie) 31062306a36Sopenharmony_ci ref->inode_list = newref->inode_list; 31162306a36Sopenharmony_ci else 31262306a36Sopenharmony_ci eie->next = newref->inode_list; 31362306a36Sopenharmony_ci trace_btrfs_prelim_ref_merge(fs_info, ref, newref, 31462306a36Sopenharmony_ci preftree->count); 31562306a36Sopenharmony_ci /* 31662306a36Sopenharmony_ci * A delayed ref can have newref->count < 0. 31762306a36Sopenharmony_ci * The ref->count is updated to follow any 31862306a36Sopenharmony_ci * BTRFS_[ADD|DROP]_DELAYED_REF actions. 31962306a36Sopenharmony_ci */ 32062306a36Sopenharmony_ci update_share_count(sc, ref->count, 32162306a36Sopenharmony_ci ref->count + newref->count, newref); 32262306a36Sopenharmony_ci ref->count += newref->count; 32362306a36Sopenharmony_ci free_pref(newref); 32462306a36Sopenharmony_ci return; 32562306a36Sopenharmony_ci } 32662306a36Sopenharmony_ci } 32762306a36Sopenharmony_ci 32862306a36Sopenharmony_ci update_share_count(sc, 0, newref->count, newref); 32962306a36Sopenharmony_ci preftree->count++; 33062306a36Sopenharmony_ci trace_btrfs_prelim_ref_insert(fs_info, newref, NULL, preftree->count); 33162306a36Sopenharmony_ci rb_link_node(&newref->rbnode, parent, p); 33262306a36Sopenharmony_ci rb_insert_color_cached(&newref->rbnode, root, leftmost); 33362306a36Sopenharmony_ci} 33462306a36Sopenharmony_ci 33562306a36Sopenharmony_ci/* 33662306a36Sopenharmony_ci * Release the entire tree. We don't care about internal consistency so 33762306a36Sopenharmony_ci * just free everything and then reset the tree root. 33862306a36Sopenharmony_ci */ 33962306a36Sopenharmony_cistatic void prelim_release(struct preftree *preftree) 34062306a36Sopenharmony_ci{ 34162306a36Sopenharmony_ci struct prelim_ref *ref, *next_ref; 34262306a36Sopenharmony_ci 34362306a36Sopenharmony_ci rbtree_postorder_for_each_entry_safe(ref, next_ref, 34462306a36Sopenharmony_ci &preftree->root.rb_root, rbnode) { 34562306a36Sopenharmony_ci free_inode_elem_list(ref->inode_list); 34662306a36Sopenharmony_ci free_pref(ref); 34762306a36Sopenharmony_ci } 34862306a36Sopenharmony_ci 34962306a36Sopenharmony_ci preftree->root = RB_ROOT_CACHED; 35062306a36Sopenharmony_ci preftree->count = 0; 35162306a36Sopenharmony_ci} 35262306a36Sopenharmony_ci 35362306a36Sopenharmony_ci/* 35462306a36Sopenharmony_ci * the rules for all callers of this function are: 35562306a36Sopenharmony_ci * - obtaining the parent is the goal 35662306a36Sopenharmony_ci * - if you add a key, you must know that it is a correct key 35762306a36Sopenharmony_ci * - if you cannot add the parent or a correct key, then we will look into the 35862306a36Sopenharmony_ci * block later to set a correct key 35962306a36Sopenharmony_ci * 36062306a36Sopenharmony_ci * delayed refs 36162306a36Sopenharmony_ci * ============ 36262306a36Sopenharmony_ci * backref type | shared | indirect | shared | indirect 36362306a36Sopenharmony_ci * information | tree | tree | data | data 36462306a36Sopenharmony_ci * --------------------+--------+----------+--------+---------- 36562306a36Sopenharmony_ci * parent logical | y | - | - | - 36662306a36Sopenharmony_ci * key to resolve | - | y | y | y 36762306a36Sopenharmony_ci * tree block logical | - | - | - | - 36862306a36Sopenharmony_ci * root for resolving | y | y | y | y 36962306a36Sopenharmony_ci * 37062306a36Sopenharmony_ci * - column 1: we've the parent -> done 37162306a36Sopenharmony_ci * - column 2, 3, 4: we use the key to find the parent 37262306a36Sopenharmony_ci * 37362306a36Sopenharmony_ci * on disk refs (inline or keyed) 37462306a36Sopenharmony_ci * ============================== 37562306a36Sopenharmony_ci * backref type | shared | indirect | shared | indirect 37662306a36Sopenharmony_ci * information | tree | tree | data | data 37762306a36Sopenharmony_ci * --------------------+--------+----------+--------+---------- 37862306a36Sopenharmony_ci * parent logical | y | - | y | - 37962306a36Sopenharmony_ci * key to resolve | - | - | - | y 38062306a36Sopenharmony_ci * tree block logical | y | y | y | y 38162306a36Sopenharmony_ci * root for resolving | - | y | y | y 38262306a36Sopenharmony_ci * 38362306a36Sopenharmony_ci * - column 1, 3: we've the parent -> done 38462306a36Sopenharmony_ci * - column 2: we take the first key from the block to find the parent 38562306a36Sopenharmony_ci * (see add_missing_keys) 38662306a36Sopenharmony_ci * - column 4: we use the key to find the parent 38762306a36Sopenharmony_ci * 38862306a36Sopenharmony_ci * additional information that's available but not required to find the parent 38962306a36Sopenharmony_ci * block might help in merging entries to gain some speed. 39062306a36Sopenharmony_ci */ 39162306a36Sopenharmony_cistatic int add_prelim_ref(const struct btrfs_fs_info *fs_info, 39262306a36Sopenharmony_ci struct preftree *preftree, u64 root_id, 39362306a36Sopenharmony_ci const struct btrfs_key *key, int level, u64 parent, 39462306a36Sopenharmony_ci u64 wanted_disk_byte, int count, 39562306a36Sopenharmony_ci struct share_check *sc, gfp_t gfp_mask) 39662306a36Sopenharmony_ci{ 39762306a36Sopenharmony_ci struct prelim_ref *ref; 39862306a36Sopenharmony_ci 39962306a36Sopenharmony_ci if (root_id == BTRFS_DATA_RELOC_TREE_OBJECTID) 40062306a36Sopenharmony_ci return 0; 40162306a36Sopenharmony_ci 40262306a36Sopenharmony_ci ref = kmem_cache_alloc(btrfs_prelim_ref_cache, gfp_mask); 40362306a36Sopenharmony_ci if (!ref) 40462306a36Sopenharmony_ci return -ENOMEM; 40562306a36Sopenharmony_ci 40662306a36Sopenharmony_ci ref->root_id = root_id; 40762306a36Sopenharmony_ci if (key) 40862306a36Sopenharmony_ci ref->key_for_search = *key; 40962306a36Sopenharmony_ci else 41062306a36Sopenharmony_ci memset(&ref->key_for_search, 0, sizeof(ref->key_for_search)); 41162306a36Sopenharmony_ci 41262306a36Sopenharmony_ci ref->inode_list = NULL; 41362306a36Sopenharmony_ci ref->level = level; 41462306a36Sopenharmony_ci ref->count = count; 41562306a36Sopenharmony_ci ref->parent = parent; 41662306a36Sopenharmony_ci ref->wanted_disk_byte = wanted_disk_byte; 41762306a36Sopenharmony_ci prelim_ref_insert(fs_info, preftree, ref, sc); 41862306a36Sopenharmony_ci return extent_is_shared(sc); 41962306a36Sopenharmony_ci} 42062306a36Sopenharmony_ci 42162306a36Sopenharmony_ci/* direct refs use root == 0, key == NULL */ 42262306a36Sopenharmony_cistatic int add_direct_ref(const struct btrfs_fs_info *fs_info, 42362306a36Sopenharmony_ci struct preftrees *preftrees, int level, u64 parent, 42462306a36Sopenharmony_ci u64 wanted_disk_byte, int count, 42562306a36Sopenharmony_ci struct share_check *sc, gfp_t gfp_mask) 42662306a36Sopenharmony_ci{ 42762306a36Sopenharmony_ci return add_prelim_ref(fs_info, &preftrees->direct, 0, NULL, level, 42862306a36Sopenharmony_ci parent, wanted_disk_byte, count, sc, gfp_mask); 42962306a36Sopenharmony_ci} 43062306a36Sopenharmony_ci 43162306a36Sopenharmony_ci/* indirect refs use parent == 0 */ 43262306a36Sopenharmony_cistatic int add_indirect_ref(const struct btrfs_fs_info *fs_info, 43362306a36Sopenharmony_ci struct preftrees *preftrees, u64 root_id, 43462306a36Sopenharmony_ci const struct btrfs_key *key, int level, 43562306a36Sopenharmony_ci u64 wanted_disk_byte, int count, 43662306a36Sopenharmony_ci struct share_check *sc, gfp_t gfp_mask) 43762306a36Sopenharmony_ci{ 43862306a36Sopenharmony_ci struct preftree *tree = &preftrees->indirect; 43962306a36Sopenharmony_ci 44062306a36Sopenharmony_ci if (!key) 44162306a36Sopenharmony_ci tree = &preftrees->indirect_missing_keys; 44262306a36Sopenharmony_ci return add_prelim_ref(fs_info, tree, root_id, key, level, 0, 44362306a36Sopenharmony_ci wanted_disk_byte, count, sc, gfp_mask); 44462306a36Sopenharmony_ci} 44562306a36Sopenharmony_ci 44662306a36Sopenharmony_cistatic int is_shared_data_backref(struct preftrees *preftrees, u64 bytenr) 44762306a36Sopenharmony_ci{ 44862306a36Sopenharmony_ci struct rb_node **p = &preftrees->direct.root.rb_root.rb_node; 44962306a36Sopenharmony_ci struct rb_node *parent = NULL; 45062306a36Sopenharmony_ci struct prelim_ref *ref = NULL; 45162306a36Sopenharmony_ci struct prelim_ref target = {}; 45262306a36Sopenharmony_ci int result; 45362306a36Sopenharmony_ci 45462306a36Sopenharmony_ci target.parent = bytenr; 45562306a36Sopenharmony_ci 45662306a36Sopenharmony_ci while (*p) { 45762306a36Sopenharmony_ci parent = *p; 45862306a36Sopenharmony_ci ref = rb_entry(parent, struct prelim_ref, rbnode); 45962306a36Sopenharmony_ci result = prelim_ref_compare(ref, &target); 46062306a36Sopenharmony_ci 46162306a36Sopenharmony_ci if (result < 0) 46262306a36Sopenharmony_ci p = &(*p)->rb_left; 46362306a36Sopenharmony_ci else if (result > 0) 46462306a36Sopenharmony_ci p = &(*p)->rb_right; 46562306a36Sopenharmony_ci else 46662306a36Sopenharmony_ci return 1; 46762306a36Sopenharmony_ci } 46862306a36Sopenharmony_ci return 0; 46962306a36Sopenharmony_ci} 47062306a36Sopenharmony_ci 47162306a36Sopenharmony_cistatic int add_all_parents(struct btrfs_backref_walk_ctx *ctx, 47262306a36Sopenharmony_ci struct btrfs_root *root, struct btrfs_path *path, 47362306a36Sopenharmony_ci struct ulist *parents, 47462306a36Sopenharmony_ci struct preftrees *preftrees, struct prelim_ref *ref, 47562306a36Sopenharmony_ci int level) 47662306a36Sopenharmony_ci{ 47762306a36Sopenharmony_ci int ret = 0; 47862306a36Sopenharmony_ci int slot; 47962306a36Sopenharmony_ci struct extent_buffer *eb; 48062306a36Sopenharmony_ci struct btrfs_key key; 48162306a36Sopenharmony_ci struct btrfs_key *key_for_search = &ref->key_for_search; 48262306a36Sopenharmony_ci struct btrfs_file_extent_item *fi; 48362306a36Sopenharmony_ci struct extent_inode_elem *eie = NULL, *old = NULL; 48462306a36Sopenharmony_ci u64 disk_byte; 48562306a36Sopenharmony_ci u64 wanted_disk_byte = ref->wanted_disk_byte; 48662306a36Sopenharmony_ci u64 count = 0; 48762306a36Sopenharmony_ci u64 data_offset; 48862306a36Sopenharmony_ci u8 type; 48962306a36Sopenharmony_ci 49062306a36Sopenharmony_ci if (level != 0) { 49162306a36Sopenharmony_ci eb = path->nodes[level]; 49262306a36Sopenharmony_ci ret = ulist_add(parents, eb->start, 0, GFP_NOFS); 49362306a36Sopenharmony_ci if (ret < 0) 49462306a36Sopenharmony_ci return ret; 49562306a36Sopenharmony_ci return 0; 49662306a36Sopenharmony_ci } 49762306a36Sopenharmony_ci 49862306a36Sopenharmony_ci /* 49962306a36Sopenharmony_ci * 1. We normally enter this function with the path already pointing to 50062306a36Sopenharmony_ci * the first item to check. But sometimes, we may enter it with 50162306a36Sopenharmony_ci * slot == nritems. 50262306a36Sopenharmony_ci * 2. We are searching for normal backref but bytenr of this leaf 50362306a36Sopenharmony_ci * matches shared data backref 50462306a36Sopenharmony_ci * 3. The leaf owner is not equal to the root we are searching 50562306a36Sopenharmony_ci * 50662306a36Sopenharmony_ci * For these cases, go to the next leaf before we continue. 50762306a36Sopenharmony_ci */ 50862306a36Sopenharmony_ci eb = path->nodes[0]; 50962306a36Sopenharmony_ci if (path->slots[0] >= btrfs_header_nritems(eb) || 51062306a36Sopenharmony_ci is_shared_data_backref(preftrees, eb->start) || 51162306a36Sopenharmony_ci ref->root_id != btrfs_header_owner(eb)) { 51262306a36Sopenharmony_ci if (ctx->time_seq == BTRFS_SEQ_LAST) 51362306a36Sopenharmony_ci ret = btrfs_next_leaf(root, path); 51462306a36Sopenharmony_ci else 51562306a36Sopenharmony_ci ret = btrfs_next_old_leaf(root, path, ctx->time_seq); 51662306a36Sopenharmony_ci } 51762306a36Sopenharmony_ci 51862306a36Sopenharmony_ci while (!ret && count < ref->count) { 51962306a36Sopenharmony_ci eb = path->nodes[0]; 52062306a36Sopenharmony_ci slot = path->slots[0]; 52162306a36Sopenharmony_ci 52262306a36Sopenharmony_ci btrfs_item_key_to_cpu(eb, &key, slot); 52362306a36Sopenharmony_ci 52462306a36Sopenharmony_ci if (key.objectid != key_for_search->objectid || 52562306a36Sopenharmony_ci key.type != BTRFS_EXTENT_DATA_KEY) 52662306a36Sopenharmony_ci break; 52762306a36Sopenharmony_ci 52862306a36Sopenharmony_ci /* 52962306a36Sopenharmony_ci * We are searching for normal backref but bytenr of this leaf 53062306a36Sopenharmony_ci * matches shared data backref, OR 53162306a36Sopenharmony_ci * the leaf owner is not equal to the root we are searching for 53262306a36Sopenharmony_ci */ 53362306a36Sopenharmony_ci if (slot == 0 && 53462306a36Sopenharmony_ci (is_shared_data_backref(preftrees, eb->start) || 53562306a36Sopenharmony_ci ref->root_id != btrfs_header_owner(eb))) { 53662306a36Sopenharmony_ci if (ctx->time_seq == BTRFS_SEQ_LAST) 53762306a36Sopenharmony_ci ret = btrfs_next_leaf(root, path); 53862306a36Sopenharmony_ci else 53962306a36Sopenharmony_ci ret = btrfs_next_old_leaf(root, path, ctx->time_seq); 54062306a36Sopenharmony_ci continue; 54162306a36Sopenharmony_ci } 54262306a36Sopenharmony_ci fi = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item); 54362306a36Sopenharmony_ci type = btrfs_file_extent_type(eb, fi); 54462306a36Sopenharmony_ci if (type == BTRFS_FILE_EXTENT_INLINE) 54562306a36Sopenharmony_ci goto next; 54662306a36Sopenharmony_ci disk_byte = btrfs_file_extent_disk_bytenr(eb, fi); 54762306a36Sopenharmony_ci data_offset = btrfs_file_extent_offset(eb, fi); 54862306a36Sopenharmony_ci 54962306a36Sopenharmony_ci if (disk_byte == wanted_disk_byte) { 55062306a36Sopenharmony_ci eie = NULL; 55162306a36Sopenharmony_ci old = NULL; 55262306a36Sopenharmony_ci if (ref->key_for_search.offset == key.offset - data_offset) 55362306a36Sopenharmony_ci count++; 55462306a36Sopenharmony_ci else 55562306a36Sopenharmony_ci goto next; 55662306a36Sopenharmony_ci if (!ctx->skip_inode_ref_list) { 55762306a36Sopenharmony_ci ret = check_extent_in_eb(ctx, &key, eb, fi, &eie); 55862306a36Sopenharmony_ci if (ret == BTRFS_ITERATE_EXTENT_INODES_STOP || 55962306a36Sopenharmony_ci ret < 0) 56062306a36Sopenharmony_ci break; 56162306a36Sopenharmony_ci } 56262306a36Sopenharmony_ci if (ret > 0) 56362306a36Sopenharmony_ci goto next; 56462306a36Sopenharmony_ci ret = ulist_add_merge_ptr(parents, eb->start, 56562306a36Sopenharmony_ci eie, (void **)&old, GFP_NOFS); 56662306a36Sopenharmony_ci if (ret < 0) 56762306a36Sopenharmony_ci break; 56862306a36Sopenharmony_ci if (!ret && !ctx->skip_inode_ref_list) { 56962306a36Sopenharmony_ci while (old->next) 57062306a36Sopenharmony_ci old = old->next; 57162306a36Sopenharmony_ci old->next = eie; 57262306a36Sopenharmony_ci } 57362306a36Sopenharmony_ci eie = NULL; 57462306a36Sopenharmony_ci } 57562306a36Sopenharmony_cinext: 57662306a36Sopenharmony_ci if (ctx->time_seq == BTRFS_SEQ_LAST) 57762306a36Sopenharmony_ci ret = btrfs_next_item(root, path); 57862306a36Sopenharmony_ci else 57962306a36Sopenharmony_ci ret = btrfs_next_old_item(root, path, ctx->time_seq); 58062306a36Sopenharmony_ci } 58162306a36Sopenharmony_ci 58262306a36Sopenharmony_ci if (ret == BTRFS_ITERATE_EXTENT_INODES_STOP || ret < 0) 58362306a36Sopenharmony_ci free_inode_elem_list(eie); 58462306a36Sopenharmony_ci else if (ret > 0) 58562306a36Sopenharmony_ci ret = 0; 58662306a36Sopenharmony_ci 58762306a36Sopenharmony_ci return ret; 58862306a36Sopenharmony_ci} 58962306a36Sopenharmony_ci 59062306a36Sopenharmony_ci/* 59162306a36Sopenharmony_ci * resolve an indirect backref in the form (root_id, key, level) 59262306a36Sopenharmony_ci * to a logical address 59362306a36Sopenharmony_ci */ 59462306a36Sopenharmony_cistatic int resolve_indirect_ref(struct btrfs_backref_walk_ctx *ctx, 59562306a36Sopenharmony_ci struct btrfs_path *path, 59662306a36Sopenharmony_ci struct preftrees *preftrees, 59762306a36Sopenharmony_ci struct prelim_ref *ref, struct ulist *parents) 59862306a36Sopenharmony_ci{ 59962306a36Sopenharmony_ci struct btrfs_root *root; 60062306a36Sopenharmony_ci struct extent_buffer *eb; 60162306a36Sopenharmony_ci int ret = 0; 60262306a36Sopenharmony_ci int root_level; 60362306a36Sopenharmony_ci int level = ref->level; 60462306a36Sopenharmony_ci struct btrfs_key search_key = ref->key_for_search; 60562306a36Sopenharmony_ci 60662306a36Sopenharmony_ci /* 60762306a36Sopenharmony_ci * If we're search_commit_root we could possibly be holding locks on 60862306a36Sopenharmony_ci * other tree nodes. This happens when qgroups does backref walks when 60962306a36Sopenharmony_ci * adding new delayed refs. To deal with this we need to look in cache 61062306a36Sopenharmony_ci * for the root, and if we don't find it then we need to search the 61162306a36Sopenharmony_ci * tree_root's commit root, thus the btrfs_get_fs_root_commit_root usage 61262306a36Sopenharmony_ci * here. 61362306a36Sopenharmony_ci */ 61462306a36Sopenharmony_ci if (path->search_commit_root) 61562306a36Sopenharmony_ci root = btrfs_get_fs_root_commit_root(ctx->fs_info, path, ref->root_id); 61662306a36Sopenharmony_ci else 61762306a36Sopenharmony_ci root = btrfs_get_fs_root(ctx->fs_info, ref->root_id, false); 61862306a36Sopenharmony_ci if (IS_ERR(root)) { 61962306a36Sopenharmony_ci ret = PTR_ERR(root); 62062306a36Sopenharmony_ci goto out_free; 62162306a36Sopenharmony_ci } 62262306a36Sopenharmony_ci 62362306a36Sopenharmony_ci if (!path->search_commit_root && 62462306a36Sopenharmony_ci test_bit(BTRFS_ROOT_DELETING, &root->state)) { 62562306a36Sopenharmony_ci ret = -ENOENT; 62662306a36Sopenharmony_ci goto out; 62762306a36Sopenharmony_ci } 62862306a36Sopenharmony_ci 62962306a36Sopenharmony_ci if (btrfs_is_testing(ctx->fs_info)) { 63062306a36Sopenharmony_ci ret = -ENOENT; 63162306a36Sopenharmony_ci goto out; 63262306a36Sopenharmony_ci } 63362306a36Sopenharmony_ci 63462306a36Sopenharmony_ci if (path->search_commit_root) 63562306a36Sopenharmony_ci root_level = btrfs_header_level(root->commit_root); 63662306a36Sopenharmony_ci else if (ctx->time_seq == BTRFS_SEQ_LAST) 63762306a36Sopenharmony_ci root_level = btrfs_header_level(root->node); 63862306a36Sopenharmony_ci else 63962306a36Sopenharmony_ci root_level = btrfs_old_root_level(root, ctx->time_seq); 64062306a36Sopenharmony_ci 64162306a36Sopenharmony_ci if (root_level + 1 == level) 64262306a36Sopenharmony_ci goto out; 64362306a36Sopenharmony_ci 64462306a36Sopenharmony_ci /* 64562306a36Sopenharmony_ci * We can often find data backrefs with an offset that is too large 64662306a36Sopenharmony_ci * (>= LLONG_MAX, maximum allowed file offset) due to underflows when 64762306a36Sopenharmony_ci * subtracting a file's offset with the data offset of its 64862306a36Sopenharmony_ci * corresponding extent data item. This can happen for example in the 64962306a36Sopenharmony_ci * clone ioctl. 65062306a36Sopenharmony_ci * 65162306a36Sopenharmony_ci * So if we detect such case we set the search key's offset to zero to 65262306a36Sopenharmony_ci * make sure we will find the matching file extent item at 65362306a36Sopenharmony_ci * add_all_parents(), otherwise we will miss it because the offset 65462306a36Sopenharmony_ci * taken form the backref is much larger then the offset of the file 65562306a36Sopenharmony_ci * extent item. This can make us scan a very large number of file 65662306a36Sopenharmony_ci * extent items, but at least it will not make us miss any. 65762306a36Sopenharmony_ci * 65862306a36Sopenharmony_ci * This is an ugly workaround for a behaviour that should have never 65962306a36Sopenharmony_ci * existed, but it does and a fix for the clone ioctl would touch a lot 66062306a36Sopenharmony_ci * of places, cause backwards incompatibility and would not fix the 66162306a36Sopenharmony_ci * problem for extents cloned with older kernels. 66262306a36Sopenharmony_ci */ 66362306a36Sopenharmony_ci if (search_key.type == BTRFS_EXTENT_DATA_KEY && 66462306a36Sopenharmony_ci search_key.offset >= LLONG_MAX) 66562306a36Sopenharmony_ci search_key.offset = 0; 66662306a36Sopenharmony_ci path->lowest_level = level; 66762306a36Sopenharmony_ci if (ctx->time_seq == BTRFS_SEQ_LAST) 66862306a36Sopenharmony_ci ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0); 66962306a36Sopenharmony_ci else 67062306a36Sopenharmony_ci ret = btrfs_search_old_slot(root, &search_key, path, ctx->time_seq); 67162306a36Sopenharmony_ci 67262306a36Sopenharmony_ci btrfs_debug(ctx->fs_info, 67362306a36Sopenharmony_ci "search slot in root %llu (level %d, ref count %d) returned %d for key (%llu %u %llu)", 67462306a36Sopenharmony_ci ref->root_id, level, ref->count, ret, 67562306a36Sopenharmony_ci ref->key_for_search.objectid, ref->key_for_search.type, 67662306a36Sopenharmony_ci ref->key_for_search.offset); 67762306a36Sopenharmony_ci if (ret < 0) 67862306a36Sopenharmony_ci goto out; 67962306a36Sopenharmony_ci 68062306a36Sopenharmony_ci eb = path->nodes[level]; 68162306a36Sopenharmony_ci while (!eb) { 68262306a36Sopenharmony_ci if (WARN_ON(!level)) { 68362306a36Sopenharmony_ci ret = 1; 68462306a36Sopenharmony_ci goto out; 68562306a36Sopenharmony_ci } 68662306a36Sopenharmony_ci level--; 68762306a36Sopenharmony_ci eb = path->nodes[level]; 68862306a36Sopenharmony_ci } 68962306a36Sopenharmony_ci 69062306a36Sopenharmony_ci ret = add_all_parents(ctx, root, path, parents, preftrees, ref, level); 69162306a36Sopenharmony_ciout: 69262306a36Sopenharmony_ci btrfs_put_root(root); 69362306a36Sopenharmony_ciout_free: 69462306a36Sopenharmony_ci path->lowest_level = 0; 69562306a36Sopenharmony_ci btrfs_release_path(path); 69662306a36Sopenharmony_ci return ret; 69762306a36Sopenharmony_ci} 69862306a36Sopenharmony_ci 69962306a36Sopenharmony_cistatic struct extent_inode_elem * 70062306a36Sopenharmony_ciunode_aux_to_inode_list(struct ulist_node *node) 70162306a36Sopenharmony_ci{ 70262306a36Sopenharmony_ci if (!node) 70362306a36Sopenharmony_ci return NULL; 70462306a36Sopenharmony_ci return (struct extent_inode_elem *)(uintptr_t)node->aux; 70562306a36Sopenharmony_ci} 70662306a36Sopenharmony_ci 70762306a36Sopenharmony_cistatic void free_leaf_list(struct ulist *ulist) 70862306a36Sopenharmony_ci{ 70962306a36Sopenharmony_ci struct ulist_node *node; 71062306a36Sopenharmony_ci struct ulist_iterator uiter; 71162306a36Sopenharmony_ci 71262306a36Sopenharmony_ci ULIST_ITER_INIT(&uiter); 71362306a36Sopenharmony_ci while ((node = ulist_next(ulist, &uiter))) 71462306a36Sopenharmony_ci free_inode_elem_list(unode_aux_to_inode_list(node)); 71562306a36Sopenharmony_ci 71662306a36Sopenharmony_ci ulist_free(ulist); 71762306a36Sopenharmony_ci} 71862306a36Sopenharmony_ci 71962306a36Sopenharmony_ci/* 72062306a36Sopenharmony_ci * We maintain three separate rbtrees: one for direct refs, one for 72162306a36Sopenharmony_ci * indirect refs which have a key, and one for indirect refs which do not 72262306a36Sopenharmony_ci * have a key. Each tree does merge on insertion. 72362306a36Sopenharmony_ci * 72462306a36Sopenharmony_ci * Once all of the references are located, we iterate over the tree of 72562306a36Sopenharmony_ci * indirect refs with missing keys. An appropriate key is located and 72662306a36Sopenharmony_ci * the ref is moved onto the tree for indirect refs. After all missing 72762306a36Sopenharmony_ci * keys are thus located, we iterate over the indirect ref tree, resolve 72862306a36Sopenharmony_ci * each reference, and then insert the resolved reference onto the 72962306a36Sopenharmony_ci * direct tree (merging there too). 73062306a36Sopenharmony_ci * 73162306a36Sopenharmony_ci * New backrefs (i.e., for parent nodes) are added to the appropriate 73262306a36Sopenharmony_ci * rbtree as they are encountered. The new backrefs are subsequently 73362306a36Sopenharmony_ci * resolved as above. 73462306a36Sopenharmony_ci */ 73562306a36Sopenharmony_cistatic int resolve_indirect_refs(struct btrfs_backref_walk_ctx *ctx, 73662306a36Sopenharmony_ci struct btrfs_path *path, 73762306a36Sopenharmony_ci struct preftrees *preftrees, 73862306a36Sopenharmony_ci struct share_check *sc) 73962306a36Sopenharmony_ci{ 74062306a36Sopenharmony_ci int err; 74162306a36Sopenharmony_ci int ret = 0; 74262306a36Sopenharmony_ci struct ulist *parents; 74362306a36Sopenharmony_ci struct ulist_node *node; 74462306a36Sopenharmony_ci struct ulist_iterator uiter; 74562306a36Sopenharmony_ci struct rb_node *rnode; 74662306a36Sopenharmony_ci 74762306a36Sopenharmony_ci parents = ulist_alloc(GFP_NOFS); 74862306a36Sopenharmony_ci if (!parents) 74962306a36Sopenharmony_ci return -ENOMEM; 75062306a36Sopenharmony_ci 75162306a36Sopenharmony_ci /* 75262306a36Sopenharmony_ci * We could trade memory usage for performance here by iterating 75362306a36Sopenharmony_ci * the tree, allocating new refs for each insertion, and then 75462306a36Sopenharmony_ci * freeing the entire indirect tree when we're done. In some test 75562306a36Sopenharmony_ci * cases, the tree can grow quite large (~200k objects). 75662306a36Sopenharmony_ci */ 75762306a36Sopenharmony_ci while ((rnode = rb_first_cached(&preftrees->indirect.root))) { 75862306a36Sopenharmony_ci struct prelim_ref *ref; 75962306a36Sopenharmony_ci 76062306a36Sopenharmony_ci ref = rb_entry(rnode, struct prelim_ref, rbnode); 76162306a36Sopenharmony_ci if (WARN(ref->parent, 76262306a36Sopenharmony_ci "BUG: direct ref found in indirect tree")) { 76362306a36Sopenharmony_ci ret = -EINVAL; 76462306a36Sopenharmony_ci goto out; 76562306a36Sopenharmony_ci } 76662306a36Sopenharmony_ci 76762306a36Sopenharmony_ci rb_erase_cached(&ref->rbnode, &preftrees->indirect.root); 76862306a36Sopenharmony_ci preftrees->indirect.count--; 76962306a36Sopenharmony_ci 77062306a36Sopenharmony_ci if (ref->count == 0) { 77162306a36Sopenharmony_ci free_pref(ref); 77262306a36Sopenharmony_ci continue; 77362306a36Sopenharmony_ci } 77462306a36Sopenharmony_ci 77562306a36Sopenharmony_ci if (sc && ref->root_id != sc->root->root_key.objectid) { 77662306a36Sopenharmony_ci free_pref(ref); 77762306a36Sopenharmony_ci ret = BACKREF_FOUND_SHARED; 77862306a36Sopenharmony_ci goto out; 77962306a36Sopenharmony_ci } 78062306a36Sopenharmony_ci err = resolve_indirect_ref(ctx, path, preftrees, ref, parents); 78162306a36Sopenharmony_ci /* 78262306a36Sopenharmony_ci * we can only tolerate ENOENT,otherwise,we should catch error 78362306a36Sopenharmony_ci * and return directly. 78462306a36Sopenharmony_ci */ 78562306a36Sopenharmony_ci if (err == -ENOENT) { 78662306a36Sopenharmony_ci prelim_ref_insert(ctx->fs_info, &preftrees->direct, ref, 78762306a36Sopenharmony_ci NULL); 78862306a36Sopenharmony_ci continue; 78962306a36Sopenharmony_ci } else if (err) { 79062306a36Sopenharmony_ci free_pref(ref); 79162306a36Sopenharmony_ci ret = err; 79262306a36Sopenharmony_ci goto out; 79362306a36Sopenharmony_ci } 79462306a36Sopenharmony_ci 79562306a36Sopenharmony_ci /* we put the first parent into the ref at hand */ 79662306a36Sopenharmony_ci ULIST_ITER_INIT(&uiter); 79762306a36Sopenharmony_ci node = ulist_next(parents, &uiter); 79862306a36Sopenharmony_ci ref->parent = node ? node->val : 0; 79962306a36Sopenharmony_ci ref->inode_list = unode_aux_to_inode_list(node); 80062306a36Sopenharmony_ci 80162306a36Sopenharmony_ci /* Add a prelim_ref(s) for any other parent(s). */ 80262306a36Sopenharmony_ci while ((node = ulist_next(parents, &uiter))) { 80362306a36Sopenharmony_ci struct prelim_ref *new_ref; 80462306a36Sopenharmony_ci 80562306a36Sopenharmony_ci new_ref = kmem_cache_alloc(btrfs_prelim_ref_cache, 80662306a36Sopenharmony_ci GFP_NOFS); 80762306a36Sopenharmony_ci if (!new_ref) { 80862306a36Sopenharmony_ci free_pref(ref); 80962306a36Sopenharmony_ci ret = -ENOMEM; 81062306a36Sopenharmony_ci goto out; 81162306a36Sopenharmony_ci } 81262306a36Sopenharmony_ci memcpy(new_ref, ref, sizeof(*ref)); 81362306a36Sopenharmony_ci new_ref->parent = node->val; 81462306a36Sopenharmony_ci new_ref->inode_list = unode_aux_to_inode_list(node); 81562306a36Sopenharmony_ci prelim_ref_insert(ctx->fs_info, &preftrees->direct, 81662306a36Sopenharmony_ci new_ref, NULL); 81762306a36Sopenharmony_ci } 81862306a36Sopenharmony_ci 81962306a36Sopenharmony_ci /* 82062306a36Sopenharmony_ci * Now it's a direct ref, put it in the direct tree. We must 82162306a36Sopenharmony_ci * do this last because the ref could be merged/freed here. 82262306a36Sopenharmony_ci */ 82362306a36Sopenharmony_ci prelim_ref_insert(ctx->fs_info, &preftrees->direct, ref, NULL); 82462306a36Sopenharmony_ci 82562306a36Sopenharmony_ci ulist_reinit(parents); 82662306a36Sopenharmony_ci cond_resched(); 82762306a36Sopenharmony_ci } 82862306a36Sopenharmony_ciout: 82962306a36Sopenharmony_ci /* 83062306a36Sopenharmony_ci * We may have inode lists attached to refs in the parents ulist, so we 83162306a36Sopenharmony_ci * must free them before freeing the ulist and its refs. 83262306a36Sopenharmony_ci */ 83362306a36Sopenharmony_ci free_leaf_list(parents); 83462306a36Sopenharmony_ci return ret; 83562306a36Sopenharmony_ci} 83662306a36Sopenharmony_ci 83762306a36Sopenharmony_ci/* 83862306a36Sopenharmony_ci * read tree blocks and add keys where required. 83962306a36Sopenharmony_ci */ 84062306a36Sopenharmony_cistatic int add_missing_keys(struct btrfs_fs_info *fs_info, 84162306a36Sopenharmony_ci struct preftrees *preftrees, bool lock) 84262306a36Sopenharmony_ci{ 84362306a36Sopenharmony_ci struct prelim_ref *ref; 84462306a36Sopenharmony_ci struct extent_buffer *eb; 84562306a36Sopenharmony_ci struct preftree *tree = &preftrees->indirect_missing_keys; 84662306a36Sopenharmony_ci struct rb_node *node; 84762306a36Sopenharmony_ci 84862306a36Sopenharmony_ci while ((node = rb_first_cached(&tree->root))) { 84962306a36Sopenharmony_ci struct btrfs_tree_parent_check check = { 0 }; 85062306a36Sopenharmony_ci 85162306a36Sopenharmony_ci ref = rb_entry(node, struct prelim_ref, rbnode); 85262306a36Sopenharmony_ci rb_erase_cached(node, &tree->root); 85362306a36Sopenharmony_ci 85462306a36Sopenharmony_ci BUG_ON(ref->parent); /* should not be a direct ref */ 85562306a36Sopenharmony_ci BUG_ON(ref->key_for_search.type); 85662306a36Sopenharmony_ci BUG_ON(!ref->wanted_disk_byte); 85762306a36Sopenharmony_ci 85862306a36Sopenharmony_ci check.level = ref->level - 1; 85962306a36Sopenharmony_ci check.owner_root = ref->root_id; 86062306a36Sopenharmony_ci 86162306a36Sopenharmony_ci eb = read_tree_block(fs_info, ref->wanted_disk_byte, &check); 86262306a36Sopenharmony_ci if (IS_ERR(eb)) { 86362306a36Sopenharmony_ci free_pref(ref); 86462306a36Sopenharmony_ci return PTR_ERR(eb); 86562306a36Sopenharmony_ci } 86662306a36Sopenharmony_ci if (!extent_buffer_uptodate(eb)) { 86762306a36Sopenharmony_ci free_pref(ref); 86862306a36Sopenharmony_ci free_extent_buffer(eb); 86962306a36Sopenharmony_ci return -EIO; 87062306a36Sopenharmony_ci } 87162306a36Sopenharmony_ci 87262306a36Sopenharmony_ci if (lock) 87362306a36Sopenharmony_ci btrfs_tree_read_lock(eb); 87462306a36Sopenharmony_ci if (btrfs_header_level(eb) == 0) 87562306a36Sopenharmony_ci btrfs_item_key_to_cpu(eb, &ref->key_for_search, 0); 87662306a36Sopenharmony_ci else 87762306a36Sopenharmony_ci btrfs_node_key_to_cpu(eb, &ref->key_for_search, 0); 87862306a36Sopenharmony_ci if (lock) 87962306a36Sopenharmony_ci btrfs_tree_read_unlock(eb); 88062306a36Sopenharmony_ci free_extent_buffer(eb); 88162306a36Sopenharmony_ci prelim_ref_insert(fs_info, &preftrees->indirect, ref, NULL); 88262306a36Sopenharmony_ci cond_resched(); 88362306a36Sopenharmony_ci } 88462306a36Sopenharmony_ci return 0; 88562306a36Sopenharmony_ci} 88662306a36Sopenharmony_ci 88762306a36Sopenharmony_ci/* 88862306a36Sopenharmony_ci * add all currently queued delayed refs from this head whose seq nr is 88962306a36Sopenharmony_ci * smaller or equal that seq to the list 89062306a36Sopenharmony_ci */ 89162306a36Sopenharmony_cistatic int add_delayed_refs(const struct btrfs_fs_info *fs_info, 89262306a36Sopenharmony_ci struct btrfs_delayed_ref_head *head, u64 seq, 89362306a36Sopenharmony_ci struct preftrees *preftrees, struct share_check *sc) 89462306a36Sopenharmony_ci{ 89562306a36Sopenharmony_ci struct btrfs_delayed_ref_node *node; 89662306a36Sopenharmony_ci struct btrfs_key key; 89762306a36Sopenharmony_ci struct rb_node *n; 89862306a36Sopenharmony_ci int count; 89962306a36Sopenharmony_ci int ret = 0; 90062306a36Sopenharmony_ci 90162306a36Sopenharmony_ci spin_lock(&head->lock); 90262306a36Sopenharmony_ci for (n = rb_first_cached(&head->ref_tree); n; n = rb_next(n)) { 90362306a36Sopenharmony_ci node = rb_entry(n, struct btrfs_delayed_ref_node, 90462306a36Sopenharmony_ci ref_node); 90562306a36Sopenharmony_ci if (node->seq > seq) 90662306a36Sopenharmony_ci continue; 90762306a36Sopenharmony_ci 90862306a36Sopenharmony_ci switch (node->action) { 90962306a36Sopenharmony_ci case BTRFS_ADD_DELAYED_EXTENT: 91062306a36Sopenharmony_ci case BTRFS_UPDATE_DELAYED_HEAD: 91162306a36Sopenharmony_ci WARN_ON(1); 91262306a36Sopenharmony_ci continue; 91362306a36Sopenharmony_ci case BTRFS_ADD_DELAYED_REF: 91462306a36Sopenharmony_ci count = node->ref_mod; 91562306a36Sopenharmony_ci break; 91662306a36Sopenharmony_ci case BTRFS_DROP_DELAYED_REF: 91762306a36Sopenharmony_ci count = node->ref_mod * -1; 91862306a36Sopenharmony_ci break; 91962306a36Sopenharmony_ci default: 92062306a36Sopenharmony_ci BUG(); 92162306a36Sopenharmony_ci } 92262306a36Sopenharmony_ci switch (node->type) { 92362306a36Sopenharmony_ci case BTRFS_TREE_BLOCK_REF_KEY: { 92462306a36Sopenharmony_ci /* NORMAL INDIRECT METADATA backref */ 92562306a36Sopenharmony_ci struct btrfs_delayed_tree_ref *ref; 92662306a36Sopenharmony_ci struct btrfs_key *key_ptr = NULL; 92762306a36Sopenharmony_ci 92862306a36Sopenharmony_ci if (head->extent_op && head->extent_op->update_key) { 92962306a36Sopenharmony_ci btrfs_disk_key_to_cpu(&key, &head->extent_op->key); 93062306a36Sopenharmony_ci key_ptr = &key; 93162306a36Sopenharmony_ci } 93262306a36Sopenharmony_ci 93362306a36Sopenharmony_ci ref = btrfs_delayed_node_to_tree_ref(node); 93462306a36Sopenharmony_ci ret = add_indirect_ref(fs_info, preftrees, ref->root, 93562306a36Sopenharmony_ci key_ptr, ref->level + 1, 93662306a36Sopenharmony_ci node->bytenr, count, sc, 93762306a36Sopenharmony_ci GFP_ATOMIC); 93862306a36Sopenharmony_ci break; 93962306a36Sopenharmony_ci } 94062306a36Sopenharmony_ci case BTRFS_SHARED_BLOCK_REF_KEY: { 94162306a36Sopenharmony_ci /* SHARED DIRECT METADATA backref */ 94262306a36Sopenharmony_ci struct btrfs_delayed_tree_ref *ref; 94362306a36Sopenharmony_ci 94462306a36Sopenharmony_ci ref = btrfs_delayed_node_to_tree_ref(node); 94562306a36Sopenharmony_ci 94662306a36Sopenharmony_ci ret = add_direct_ref(fs_info, preftrees, ref->level + 1, 94762306a36Sopenharmony_ci ref->parent, node->bytenr, count, 94862306a36Sopenharmony_ci sc, GFP_ATOMIC); 94962306a36Sopenharmony_ci break; 95062306a36Sopenharmony_ci } 95162306a36Sopenharmony_ci case BTRFS_EXTENT_DATA_REF_KEY: { 95262306a36Sopenharmony_ci /* NORMAL INDIRECT DATA backref */ 95362306a36Sopenharmony_ci struct btrfs_delayed_data_ref *ref; 95462306a36Sopenharmony_ci ref = btrfs_delayed_node_to_data_ref(node); 95562306a36Sopenharmony_ci 95662306a36Sopenharmony_ci key.objectid = ref->objectid; 95762306a36Sopenharmony_ci key.type = BTRFS_EXTENT_DATA_KEY; 95862306a36Sopenharmony_ci key.offset = ref->offset; 95962306a36Sopenharmony_ci 96062306a36Sopenharmony_ci /* 96162306a36Sopenharmony_ci * If we have a share check context and a reference for 96262306a36Sopenharmony_ci * another inode, we can't exit immediately. This is 96362306a36Sopenharmony_ci * because even if this is a BTRFS_ADD_DELAYED_REF 96462306a36Sopenharmony_ci * reference we may find next a BTRFS_DROP_DELAYED_REF 96562306a36Sopenharmony_ci * which cancels out this ADD reference. 96662306a36Sopenharmony_ci * 96762306a36Sopenharmony_ci * If this is a DROP reference and there was no previous 96862306a36Sopenharmony_ci * ADD reference, then we need to signal that when we 96962306a36Sopenharmony_ci * process references from the extent tree (through 97062306a36Sopenharmony_ci * add_inline_refs() and add_keyed_refs()), we should 97162306a36Sopenharmony_ci * not exit early if we find a reference for another 97262306a36Sopenharmony_ci * inode, because one of the delayed DROP references 97362306a36Sopenharmony_ci * may cancel that reference in the extent tree. 97462306a36Sopenharmony_ci */ 97562306a36Sopenharmony_ci if (sc && count < 0) 97662306a36Sopenharmony_ci sc->have_delayed_delete_refs = true; 97762306a36Sopenharmony_ci 97862306a36Sopenharmony_ci ret = add_indirect_ref(fs_info, preftrees, ref->root, 97962306a36Sopenharmony_ci &key, 0, node->bytenr, count, sc, 98062306a36Sopenharmony_ci GFP_ATOMIC); 98162306a36Sopenharmony_ci break; 98262306a36Sopenharmony_ci } 98362306a36Sopenharmony_ci case BTRFS_SHARED_DATA_REF_KEY: { 98462306a36Sopenharmony_ci /* SHARED DIRECT FULL backref */ 98562306a36Sopenharmony_ci struct btrfs_delayed_data_ref *ref; 98662306a36Sopenharmony_ci 98762306a36Sopenharmony_ci ref = btrfs_delayed_node_to_data_ref(node); 98862306a36Sopenharmony_ci 98962306a36Sopenharmony_ci ret = add_direct_ref(fs_info, preftrees, 0, ref->parent, 99062306a36Sopenharmony_ci node->bytenr, count, sc, 99162306a36Sopenharmony_ci GFP_ATOMIC); 99262306a36Sopenharmony_ci break; 99362306a36Sopenharmony_ci } 99462306a36Sopenharmony_ci default: 99562306a36Sopenharmony_ci WARN_ON(1); 99662306a36Sopenharmony_ci } 99762306a36Sopenharmony_ci /* 99862306a36Sopenharmony_ci * We must ignore BACKREF_FOUND_SHARED until all delayed 99962306a36Sopenharmony_ci * refs have been checked. 100062306a36Sopenharmony_ci */ 100162306a36Sopenharmony_ci if (ret && (ret != BACKREF_FOUND_SHARED)) 100262306a36Sopenharmony_ci break; 100362306a36Sopenharmony_ci } 100462306a36Sopenharmony_ci if (!ret) 100562306a36Sopenharmony_ci ret = extent_is_shared(sc); 100662306a36Sopenharmony_ci 100762306a36Sopenharmony_ci spin_unlock(&head->lock); 100862306a36Sopenharmony_ci return ret; 100962306a36Sopenharmony_ci} 101062306a36Sopenharmony_ci 101162306a36Sopenharmony_ci/* 101262306a36Sopenharmony_ci * add all inline backrefs for bytenr to the list 101362306a36Sopenharmony_ci * 101462306a36Sopenharmony_ci * Returns 0 on success, <0 on error, or BACKREF_FOUND_SHARED. 101562306a36Sopenharmony_ci */ 101662306a36Sopenharmony_cistatic int add_inline_refs(struct btrfs_backref_walk_ctx *ctx, 101762306a36Sopenharmony_ci struct btrfs_path *path, 101862306a36Sopenharmony_ci int *info_level, struct preftrees *preftrees, 101962306a36Sopenharmony_ci struct share_check *sc) 102062306a36Sopenharmony_ci{ 102162306a36Sopenharmony_ci int ret = 0; 102262306a36Sopenharmony_ci int slot; 102362306a36Sopenharmony_ci struct extent_buffer *leaf; 102462306a36Sopenharmony_ci struct btrfs_key key; 102562306a36Sopenharmony_ci struct btrfs_key found_key; 102662306a36Sopenharmony_ci unsigned long ptr; 102762306a36Sopenharmony_ci unsigned long end; 102862306a36Sopenharmony_ci struct btrfs_extent_item *ei; 102962306a36Sopenharmony_ci u64 flags; 103062306a36Sopenharmony_ci u64 item_size; 103162306a36Sopenharmony_ci 103262306a36Sopenharmony_ci /* 103362306a36Sopenharmony_ci * enumerate all inline refs 103462306a36Sopenharmony_ci */ 103562306a36Sopenharmony_ci leaf = path->nodes[0]; 103662306a36Sopenharmony_ci slot = path->slots[0]; 103762306a36Sopenharmony_ci 103862306a36Sopenharmony_ci item_size = btrfs_item_size(leaf, slot); 103962306a36Sopenharmony_ci BUG_ON(item_size < sizeof(*ei)); 104062306a36Sopenharmony_ci 104162306a36Sopenharmony_ci ei = btrfs_item_ptr(leaf, slot, struct btrfs_extent_item); 104262306a36Sopenharmony_ci 104362306a36Sopenharmony_ci if (ctx->check_extent_item) { 104462306a36Sopenharmony_ci ret = ctx->check_extent_item(ctx->bytenr, ei, leaf, ctx->user_ctx); 104562306a36Sopenharmony_ci if (ret) 104662306a36Sopenharmony_ci return ret; 104762306a36Sopenharmony_ci } 104862306a36Sopenharmony_ci 104962306a36Sopenharmony_ci flags = btrfs_extent_flags(leaf, ei); 105062306a36Sopenharmony_ci btrfs_item_key_to_cpu(leaf, &found_key, slot); 105162306a36Sopenharmony_ci 105262306a36Sopenharmony_ci ptr = (unsigned long)(ei + 1); 105362306a36Sopenharmony_ci end = (unsigned long)ei + item_size; 105462306a36Sopenharmony_ci 105562306a36Sopenharmony_ci if (found_key.type == BTRFS_EXTENT_ITEM_KEY && 105662306a36Sopenharmony_ci flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) { 105762306a36Sopenharmony_ci struct btrfs_tree_block_info *info; 105862306a36Sopenharmony_ci 105962306a36Sopenharmony_ci info = (struct btrfs_tree_block_info *)ptr; 106062306a36Sopenharmony_ci *info_level = btrfs_tree_block_level(leaf, info); 106162306a36Sopenharmony_ci ptr += sizeof(struct btrfs_tree_block_info); 106262306a36Sopenharmony_ci BUG_ON(ptr > end); 106362306a36Sopenharmony_ci } else if (found_key.type == BTRFS_METADATA_ITEM_KEY) { 106462306a36Sopenharmony_ci *info_level = found_key.offset; 106562306a36Sopenharmony_ci } else { 106662306a36Sopenharmony_ci BUG_ON(!(flags & BTRFS_EXTENT_FLAG_DATA)); 106762306a36Sopenharmony_ci } 106862306a36Sopenharmony_ci 106962306a36Sopenharmony_ci while (ptr < end) { 107062306a36Sopenharmony_ci struct btrfs_extent_inline_ref *iref; 107162306a36Sopenharmony_ci u64 offset; 107262306a36Sopenharmony_ci int type; 107362306a36Sopenharmony_ci 107462306a36Sopenharmony_ci iref = (struct btrfs_extent_inline_ref *)ptr; 107562306a36Sopenharmony_ci type = btrfs_get_extent_inline_ref_type(leaf, iref, 107662306a36Sopenharmony_ci BTRFS_REF_TYPE_ANY); 107762306a36Sopenharmony_ci if (type == BTRFS_REF_TYPE_INVALID) 107862306a36Sopenharmony_ci return -EUCLEAN; 107962306a36Sopenharmony_ci 108062306a36Sopenharmony_ci offset = btrfs_extent_inline_ref_offset(leaf, iref); 108162306a36Sopenharmony_ci 108262306a36Sopenharmony_ci switch (type) { 108362306a36Sopenharmony_ci case BTRFS_SHARED_BLOCK_REF_KEY: 108462306a36Sopenharmony_ci ret = add_direct_ref(ctx->fs_info, preftrees, 108562306a36Sopenharmony_ci *info_level + 1, offset, 108662306a36Sopenharmony_ci ctx->bytenr, 1, NULL, GFP_NOFS); 108762306a36Sopenharmony_ci break; 108862306a36Sopenharmony_ci case BTRFS_SHARED_DATA_REF_KEY: { 108962306a36Sopenharmony_ci struct btrfs_shared_data_ref *sdref; 109062306a36Sopenharmony_ci int count; 109162306a36Sopenharmony_ci 109262306a36Sopenharmony_ci sdref = (struct btrfs_shared_data_ref *)(iref + 1); 109362306a36Sopenharmony_ci count = btrfs_shared_data_ref_count(leaf, sdref); 109462306a36Sopenharmony_ci 109562306a36Sopenharmony_ci ret = add_direct_ref(ctx->fs_info, preftrees, 0, offset, 109662306a36Sopenharmony_ci ctx->bytenr, count, sc, GFP_NOFS); 109762306a36Sopenharmony_ci break; 109862306a36Sopenharmony_ci } 109962306a36Sopenharmony_ci case BTRFS_TREE_BLOCK_REF_KEY: 110062306a36Sopenharmony_ci ret = add_indirect_ref(ctx->fs_info, preftrees, offset, 110162306a36Sopenharmony_ci NULL, *info_level + 1, 110262306a36Sopenharmony_ci ctx->bytenr, 1, NULL, GFP_NOFS); 110362306a36Sopenharmony_ci break; 110462306a36Sopenharmony_ci case BTRFS_EXTENT_DATA_REF_KEY: { 110562306a36Sopenharmony_ci struct btrfs_extent_data_ref *dref; 110662306a36Sopenharmony_ci int count; 110762306a36Sopenharmony_ci u64 root; 110862306a36Sopenharmony_ci 110962306a36Sopenharmony_ci dref = (struct btrfs_extent_data_ref *)(&iref->offset); 111062306a36Sopenharmony_ci count = btrfs_extent_data_ref_count(leaf, dref); 111162306a36Sopenharmony_ci key.objectid = btrfs_extent_data_ref_objectid(leaf, 111262306a36Sopenharmony_ci dref); 111362306a36Sopenharmony_ci key.type = BTRFS_EXTENT_DATA_KEY; 111462306a36Sopenharmony_ci key.offset = btrfs_extent_data_ref_offset(leaf, dref); 111562306a36Sopenharmony_ci 111662306a36Sopenharmony_ci if (sc && key.objectid != sc->inum && 111762306a36Sopenharmony_ci !sc->have_delayed_delete_refs) { 111862306a36Sopenharmony_ci ret = BACKREF_FOUND_SHARED; 111962306a36Sopenharmony_ci break; 112062306a36Sopenharmony_ci } 112162306a36Sopenharmony_ci 112262306a36Sopenharmony_ci root = btrfs_extent_data_ref_root(leaf, dref); 112362306a36Sopenharmony_ci 112462306a36Sopenharmony_ci if (!ctx->skip_data_ref || 112562306a36Sopenharmony_ci !ctx->skip_data_ref(root, key.objectid, key.offset, 112662306a36Sopenharmony_ci ctx->user_ctx)) 112762306a36Sopenharmony_ci ret = add_indirect_ref(ctx->fs_info, preftrees, 112862306a36Sopenharmony_ci root, &key, 0, ctx->bytenr, 112962306a36Sopenharmony_ci count, sc, GFP_NOFS); 113062306a36Sopenharmony_ci break; 113162306a36Sopenharmony_ci } 113262306a36Sopenharmony_ci default: 113362306a36Sopenharmony_ci WARN_ON(1); 113462306a36Sopenharmony_ci } 113562306a36Sopenharmony_ci if (ret) 113662306a36Sopenharmony_ci return ret; 113762306a36Sopenharmony_ci ptr += btrfs_extent_inline_ref_size(type); 113862306a36Sopenharmony_ci } 113962306a36Sopenharmony_ci 114062306a36Sopenharmony_ci return 0; 114162306a36Sopenharmony_ci} 114262306a36Sopenharmony_ci 114362306a36Sopenharmony_ci/* 114462306a36Sopenharmony_ci * add all non-inline backrefs for bytenr to the list 114562306a36Sopenharmony_ci * 114662306a36Sopenharmony_ci * Returns 0 on success, <0 on error, or BACKREF_FOUND_SHARED. 114762306a36Sopenharmony_ci */ 114862306a36Sopenharmony_cistatic int add_keyed_refs(struct btrfs_backref_walk_ctx *ctx, 114962306a36Sopenharmony_ci struct btrfs_root *extent_root, 115062306a36Sopenharmony_ci struct btrfs_path *path, 115162306a36Sopenharmony_ci int info_level, struct preftrees *preftrees, 115262306a36Sopenharmony_ci struct share_check *sc) 115362306a36Sopenharmony_ci{ 115462306a36Sopenharmony_ci struct btrfs_fs_info *fs_info = extent_root->fs_info; 115562306a36Sopenharmony_ci int ret; 115662306a36Sopenharmony_ci int slot; 115762306a36Sopenharmony_ci struct extent_buffer *leaf; 115862306a36Sopenharmony_ci struct btrfs_key key; 115962306a36Sopenharmony_ci 116062306a36Sopenharmony_ci while (1) { 116162306a36Sopenharmony_ci ret = btrfs_next_item(extent_root, path); 116262306a36Sopenharmony_ci if (ret < 0) 116362306a36Sopenharmony_ci break; 116462306a36Sopenharmony_ci if (ret) { 116562306a36Sopenharmony_ci ret = 0; 116662306a36Sopenharmony_ci break; 116762306a36Sopenharmony_ci } 116862306a36Sopenharmony_ci 116962306a36Sopenharmony_ci slot = path->slots[0]; 117062306a36Sopenharmony_ci leaf = path->nodes[0]; 117162306a36Sopenharmony_ci btrfs_item_key_to_cpu(leaf, &key, slot); 117262306a36Sopenharmony_ci 117362306a36Sopenharmony_ci if (key.objectid != ctx->bytenr) 117462306a36Sopenharmony_ci break; 117562306a36Sopenharmony_ci if (key.type < BTRFS_TREE_BLOCK_REF_KEY) 117662306a36Sopenharmony_ci continue; 117762306a36Sopenharmony_ci if (key.type > BTRFS_SHARED_DATA_REF_KEY) 117862306a36Sopenharmony_ci break; 117962306a36Sopenharmony_ci 118062306a36Sopenharmony_ci switch (key.type) { 118162306a36Sopenharmony_ci case BTRFS_SHARED_BLOCK_REF_KEY: 118262306a36Sopenharmony_ci /* SHARED DIRECT METADATA backref */ 118362306a36Sopenharmony_ci ret = add_direct_ref(fs_info, preftrees, 118462306a36Sopenharmony_ci info_level + 1, key.offset, 118562306a36Sopenharmony_ci ctx->bytenr, 1, NULL, GFP_NOFS); 118662306a36Sopenharmony_ci break; 118762306a36Sopenharmony_ci case BTRFS_SHARED_DATA_REF_KEY: { 118862306a36Sopenharmony_ci /* SHARED DIRECT FULL backref */ 118962306a36Sopenharmony_ci struct btrfs_shared_data_ref *sdref; 119062306a36Sopenharmony_ci int count; 119162306a36Sopenharmony_ci 119262306a36Sopenharmony_ci sdref = btrfs_item_ptr(leaf, slot, 119362306a36Sopenharmony_ci struct btrfs_shared_data_ref); 119462306a36Sopenharmony_ci count = btrfs_shared_data_ref_count(leaf, sdref); 119562306a36Sopenharmony_ci ret = add_direct_ref(fs_info, preftrees, 0, 119662306a36Sopenharmony_ci key.offset, ctx->bytenr, count, 119762306a36Sopenharmony_ci sc, GFP_NOFS); 119862306a36Sopenharmony_ci break; 119962306a36Sopenharmony_ci } 120062306a36Sopenharmony_ci case BTRFS_TREE_BLOCK_REF_KEY: 120162306a36Sopenharmony_ci /* NORMAL INDIRECT METADATA backref */ 120262306a36Sopenharmony_ci ret = add_indirect_ref(fs_info, preftrees, key.offset, 120362306a36Sopenharmony_ci NULL, info_level + 1, ctx->bytenr, 120462306a36Sopenharmony_ci 1, NULL, GFP_NOFS); 120562306a36Sopenharmony_ci break; 120662306a36Sopenharmony_ci case BTRFS_EXTENT_DATA_REF_KEY: { 120762306a36Sopenharmony_ci /* NORMAL INDIRECT DATA backref */ 120862306a36Sopenharmony_ci struct btrfs_extent_data_ref *dref; 120962306a36Sopenharmony_ci int count; 121062306a36Sopenharmony_ci u64 root; 121162306a36Sopenharmony_ci 121262306a36Sopenharmony_ci dref = btrfs_item_ptr(leaf, slot, 121362306a36Sopenharmony_ci struct btrfs_extent_data_ref); 121462306a36Sopenharmony_ci count = btrfs_extent_data_ref_count(leaf, dref); 121562306a36Sopenharmony_ci key.objectid = btrfs_extent_data_ref_objectid(leaf, 121662306a36Sopenharmony_ci dref); 121762306a36Sopenharmony_ci key.type = BTRFS_EXTENT_DATA_KEY; 121862306a36Sopenharmony_ci key.offset = btrfs_extent_data_ref_offset(leaf, dref); 121962306a36Sopenharmony_ci 122062306a36Sopenharmony_ci if (sc && key.objectid != sc->inum && 122162306a36Sopenharmony_ci !sc->have_delayed_delete_refs) { 122262306a36Sopenharmony_ci ret = BACKREF_FOUND_SHARED; 122362306a36Sopenharmony_ci break; 122462306a36Sopenharmony_ci } 122562306a36Sopenharmony_ci 122662306a36Sopenharmony_ci root = btrfs_extent_data_ref_root(leaf, dref); 122762306a36Sopenharmony_ci 122862306a36Sopenharmony_ci if (!ctx->skip_data_ref || 122962306a36Sopenharmony_ci !ctx->skip_data_ref(root, key.objectid, key.offset, 123062306a36Sopenharmony_ci ctx->user_ctx)) 123162306a36Sopenharmony_ci ret = add_indirect_ref(fs_info, preftrees, root, 123262306a36Sopenharmony_ci &key, 0, ctx->bytenr, 123362306a36Sopenharmony_ci count, sc, GFP_NOFS); 123462306a36Sopenharmony_ci break; 123562306a36Sopenharmony_ci } 123662306a36Sopenharmony_ci default: 123762306a36Sopenharmony_ci WARN_ON(1); 123862306a36Sopenharmony_ci } 123962306a36Sopenharmony_ci if (ret) 124062306a36Sopenharmony_ci return ret; 124162306a36Sopenharmony_ci 124262306a36Sopenharmony_ci } 124362306a36Sopenharmony_ci 124462306a36Sopenharmony_ci return ret; 124562306a36Sopenharmony_ci} 124662306a36Sopenharmony_ci 124762306a36Sopenharmony_ci/* 124862306a36Sopenharmony_ci * The caller has joined a transaction or is holding a read lock on the 124962306a36Sopenharmony_ci * fs_info->commit_root_sem semaphore, so no need to worry about the root's last 125062306a36Sopenharmony_ci * snapshot field changing while updating or checking the cache. 125162306a36Sopenharmony_ci */ 125262306a36Sopenharmony_cistatic bool lookup_backref_shared_cache(struct btrfs_backref_share_check_ctx *ctx, 125362306a36Sopenharmony_ci struct btrfs_root *root, 125462306a36Sopenharmony_ci u64 bytenr, int level, bool *is_shared) 125562306a36Sopenharmony_ci{ 125662306a36Sopenharmony_ci const struct btrfs_fs_info *fs_info = root->fs_info; 125762306a36Sopenharmony_ci struct btrfs_backref_shared_cache_entry *entry; 125862306a36Sopenharmony_ci 125962306a36Sopenharmony_ci if (!current->journal_info) 126062306a36Sopenharmony_ci lockdep_assert_held(&fs_info->commit_root_sem); 126162306a36Sopenharmony_ci 126262306a36Sopenharmony_ci if (!ctx->use_path_cache) 126362306a36Sopenharmony_ci return false; 126462306a36Sopenharmony_ci 126562306a36Sopenharmony_ci if (WARN_ON_ONCE(level >= BTRFS_MAX_LEVEL)) 126662306a36Sopenharmony_ci return false; 126762306a36Sopenharmony_ci 126862306a36Sopenharmony_ci /* 126962306a36Sopenharmony_ci * Level -1 is used for the data extent, which is not reliable to cache 127062306a36Sopenharmony_ci * because its reference count can increase or decrease without us 127162306a36Sopenharmony_ci * realizing. We cache results only for extent buffers that lead from 127262306a36Sopenharmony_ci * the root node down to the leaf with the file extent item. 127362306a36Sopenharmony_ci */ 127462306a36Sopenharmony_ci ASSERT(level >= 0); 127562306a36Sopenharmony_ci 127662306a36Sopenharmony_ci entry = &ctx->path_cache_entries[level]; 127762306a36Sopenharmony_ci 127862306a36Sopenharmony_ci /* Unused cache entry or being used for some other extent buffer. */ 127962306a36Sopenharmony_ci if (entry->bytenr != bytenr) 128062306a36Sopenharmony_ci return false; 128162306a36Sopenharmony_ci 128262306a36Sopenharmony_ci /* 128362306a36Sopenharmony_ci * We cached a false result, but the last snapshot generation of the 128462306a36Sopenharmony_ci * root changed, so we now have a snapshot. Don't trust the result. 128562306a36Sopenharmony_ci */ 128662306a36Sopenharmony_ci if (!entry->is_shared && 128762306a36Sopenharmony_ci entry->gen != btrfs_root_last_snapshot(&root->root_item)) 128862306a36Sopenharmony_ci return false; 128962306a36Sopenharmony_ci 129062306a36Sopenharmony_ci /* 129162306a36Sopenharmony_ci * If we cached a true result and the last generation used for dropping 129262306a36Sopenharmony_ci * a root changed, we can not trust the result, because the dropped root 129362306a36Sopenharmony_ci * could be a snapshot sharing this extent buffer. 129462306a36Sopenharmony_ci */ 129562306a36Sopenharmony_ci if (entry->is_shared && 129662306a36Sopenharmony_ci entry->gen != btrfs_get_last_root_drop_gen(fs_info)) 129762306a36Sopenharmony_ci return false; 129862306a36Sopenharmony_ci 129962306a36Sopenharmony_ci *is_shared = entry->is_shared; 130062306a36Sopenharmony_ci /* 130162306a36Sopenharmony_ci * If the node at this level is shared, than all nodes below are also 130262306a36Sopenharmony_ci * shared. Currently some of the nodes below may be marked as not shared 130362306a36Sopenharmony_ci * because we have just switched from one leaf to another, and switched 130462306a36Sopenharmony_ci * also other nodes above the leaf and below the current level, so mark 130562306a36Sopenharmony_ci * them as shared. 130662306a36Sopenharmony_ci */ 130762306a36Sopenharmony_ci if (*is_shared) { 130862306a36Sopenharmony_ci for (int i = 0; i < level; i++) { 130962306a36Sopenharmony_ci ctx->path_cache_entries[i].is_shared = true; 131062306a36Sopenharmony_ci ctx->path_cache_entries[i].gen = entry->gen; 131162306a36Sopenharmony_ci } 131262306a36Sopenharmony_ci } 131362306a36Sopenharmony_ci 131462306a36Sopenharmony_ci return true; 131562306a36Sopenharmony_ci} 131662306a36Sopenharmony_ci 131762306a36Sopenharmony_ci/* 131862306a36Sopenharmony_ci * The caller has joined a transaction or is holding a read lock on the 131962306a36Sopenharmony_ci * fs_info->commit_root_sem semaphore, so no need to worry about the root's last 132062306a36Sopenharmony_ci * snapshot field changing while updating or checking the cache. 132162306a36Sopenharmony_ci */ 132262306a36Sopenharmony_cistatic void store_backref_shared_cache(struct btrfs_backref_share_check_ctx *ctx, 132362306a36Sopenharmony_ci struct btrfs_root *root, 132462306a36Sopenharmony_ci u64 bytenr, int level, bool is_shared) 132562306a36Sopenharmony_ci{ 132662306a36Sopenharmony_ci const struct btrfs_fs_info *fs_info = root->fs_info; 132762306a36Sopenharmony_ci struct btrfs_backref_shared_cache_entry *entry; 132862306a36Sopenharmony_ci u64 gen; 132962306a36Sopenharmony_ci 133062306a36Sopenharmony_ci if (!current->journal_info) 133162306a36Sopenharmony_ci lockdep_assert_held(&fs_info->commit_root_sem); 133262306a36Sopenharmony_ci 133362306a36Sopenharmony_ci if (!ctx->use_path_cache) 133462306a36Sopenharmony_ci return; 133562306a36Sopenharmony_ci 133662306a36Sopenharmony_ci if (WARN_ON_ONCE(level >= BTRFS_MAX_LEVEL)) 133762306a36Sopenharmony_ci return; 133862306a36Sopenharmony_ci 133962306a36Sopenharmony_ci /* 134062306a36Sopenharmony_ci * Level -1 is used for the data extent, which is not reliable to cache 134162306a36Sopenharmony_ci * because its reference count can increase or decrease without us 134262306a36Sopenharmony_ci * realizing. We cache results only for extent buffers that lead from 134362306a36Sopenharmony_ci * the root node down to the leaf with the file extent item. 134462306a36Sopenharmony_ci */ 134562306a36Sopenharmony_ci ASSERT(level >= 0); 134662306a36Sopenharmony_ci 134762306a36Sopenharmony_ci if (is_shared) 134862306a36Sopenharmony_ci gen = btrfs_get_last_root_drop_gen(fs_info); 134962306a36Sopenharmony_ci else 135062306a36Sopenharmony_ci gen = btrfs_root_last_snapshot(&root->root_item); 135162306a36Sopenharmony_ci 135262306a36Sopenharmony_ci entry = &ctx->path_cache_entries[level]; 135362306a36Sopenharmony_ci entry->bytenr = bytenr; 135462306a36Sopenharmony_ci entry->is_shared = is_shared; 135562306a36Sopenharmony_ci entry->gen = gen; 135662306a36Sopenharmony_ci 135762306a36Sopenharmony_ci /* 135862306a36Sopenharmony_ci * If we found an extent buffer is shared, set the cache result for all 135962306a36Sopenharmony_ci * extent buffers below it to true. As nodes in the path are COWed, 136062306a36Sopenharmony_ci * their sharedness is moved to their children, and if a leaf is COWed, 136162306a36Sopenharmony_ci * then the sharedness of a data extent becomes direct, the refcount of 136262306a36Sopenharmony_ci * data extent is increased in the extent item at the extent tree. 136362306a36Sopenharmony_ci */ 136462306a36Sopenharmony_ci if (is_shared) { 136562306a36Sopenharmony_ci for (int i = 0; i < level; i++) { 136662306a36Sopenharmony_ci entry = &ctx->path_cache_entries[i]; 136762306a36Sopenharmony_ci entry->is_shared = is_shared; 136862306a36Sopenharmony_ci entry->gen = gen; 136962306a36Sopenharmony_ci } 137062306a36Sopenharmony_ci } 137162306a36Sopenharmony_ci} 137262306a36Sopenharmony_ci 137362306a36Sopenharmony_ci/* 137462306a36Sopenharmony_ci * this adds all existing backrefs (inline backrefs, backrefs and delayed 137562306a36Sopenharmony_ci * refs) for the given bytenr to the refs list, merges duplicates and resolves 137662306a36Sopenharmony_ci * indirect refs to their parent bytenr. 137762306a36Sopenharmony_ci * When roots are found, they're added to the roots list 137862306a36Sopenharmony_ci * 137962306a36Sopenharmony_ci * @ctx: Backref walking context object, must be not NULL. 138062306a36Sopenharmony_ci * @sc: If !NULL, then immediately return BACKREF_FOUND_SHARED when a 138162306a36Sopenharmony_ci * shared extent is detected. 138262306a36Sopenharmony_ci * 138362306a36Sopenharmony_ci * Otherwise this returns 0 for success and <0 for an error. 138462306a36Sopenharmony_ci * 138562306a36Sopenharmony_ci * FIXME some caching might speed things up 138662306a36Sopenharmony_ci */ 138762306a36Sopenharmony_cistatic int find_parent_nodes(struct btrfs_backref_walk_ctx *ctx, 138862306a36Sopenharmony_ci struct share_check *sc) 138962306a36Sopenharmony_ci{ 139062306a36Sopenharmony_ci struct btrfs_root *root = btrfs_extent_root(ctx->fs_info, ctx->bytenr); 139162306a36Sopenharmony_ci struct btrfs_key key; 139262306a36Sopenharmony_ci struct btrfs_path *path; 139362306a36Sopenharmony_ci struct btrfs_delayed_ref_root *delayed_refs = NULL; 139462306a36Sopenharmony_ci struct btrfs_delayed_ref_head *head; 139562306a36Sopenharmony_ci int info_level = 0; 139662306a36Sopenharmony_ci int ret; 139762306a36Sopenharmony_ci struct prelim_ref *ref; 139862306a36Sopenharmony_ci struct rb_node *node; 139962306a36Sopenharmony_ci struct extent_inode_elem *eie = NULL; 140062306a36Sopenharmony_ci struct preftrees preftrees = { 140162306a36Sopenharmony_ci .direct = PREFTREE_INIT, 140262306a36Sopenharmony_ci .indirect = PREFTREE_INIT, 140362306a36Sopenharmony_ci .indirect_missing_keys = PREFTREE_INIT 140462306a36Sopenharmony_ci }; 140562306a36Sopenharmony_ci 140662306a36Sopenharmony_ci /* Roots ulist is not needed when using a sharedness check context. */ 140762306a36Sopenharmony_ci if (sc) 140862306a36Sopenharmony_ci ASSERT(ctx->roots == NULL); 140962306a36Sopenharmony_ci 141062306a36Sopenharmony_ci key.objectid = ctx->bytenr; 141162306a36Sopenharmony_ci key.offset = (u64)-1; 141262306a36Sopenharmony_ci if (btrfs_fs_incompat(ctx->fs_info, SKINNY_METADATA)) 141362306a36Sopenharmony_ci key.type = BTRFS_METADATA_ITEM_KEY; 141462306a36Sopenharmony_ci else 141562306a36Sopenharmony_ci key.type = BTRFS_EXTENT_ITEM_KEY; 141662306a36Sopenharmony_ci 141762306a36Sopenharmony_ci path = btrfs_alloc_path(); 141862306a36Sopenharmony_ci if (!path) 141962306a36Sopenharmony_ci return -ENOMEM; 142062306a36Sopenharmony_ci if (!ctx->trans) { 142162306a36Sopenharmony_ci path->search_commit_root = 1; 142262306a36Sopenharmony_ci path->skip_locking = 1; 142362306a36Sopenharmony_ci } 142462306a36Sopenharmony_ci 142562306a36Sopenharmony_ci if (ctx->time_seq == BTRFS_SEQ_LAST) 142662306a36Sopenharmony_ci path->skip_locking = 1; 142762306a36Sopenharmony_ci 142862306a36Sopenharmony_ciagain: 142962306a36Sopenharmony_ci head = NULL; 143062306a36Sopenharmony_ci 143162306a36Sopenharmony_ci ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); 143262306a36Sopenharmony_ci if (ret < 0) 143362306a36Sopenharmony_ci goto out; 143462306a36Sopenharmony_ci if (ret == 0) { 143562306a36Sopenharmony_ci /* This shouldn't happen, indicates a bug or fs corruption. */ 143662306a36Sopenharmony_ci ASSERT(ret != 0); 143762306a36Sopenharmony_ci ret = -EUCLEAN; 143862306a36Sopenharmony_ci goto out; 143962306a36Sopenharmony_ci } 144062306a36Sopenharmony_ci 144162306a36Sopenharmony_ci if (ctx->trans && likely(ctx->trans->type != __TRANS_DUMMY) && 144262306a36Sopenharmony_ci ctx->time_seq != BTRFS_SEQ_LAST) { 144362306a36Sopenharmony_ci /* 144462306a36Sopenharmony_ci * We have a specific time_seq we care about and trans which 144562306a36Sopenharmony_ci * means we have the path lock, we need to grab the ref head and 144662306a36Sopenharmony_ci * lock it so we have a consistent view of the refs at the given 144762306a36Sopenharmony_ci * time. 144862306a36Sopenharmony_ci */ 144962306a36Sopenharmony_ci delayed_refs = &ctx->trans->transaction->delayed_refs; 145062306a36Sopenharmony_ci spin_lock(&delayed_refs->lock); 145162306a36Sopenharmony_ci head = btrfs_find_delayed_ref_head(delayed_refs, ctx->bytenr); 145262306a36Sopenharmony_ci if (head) { 145362306a36Sopenharmony_ci if (!mutex_trylock(&head->mutex)) { 145462306a36Sopenharmony_ci refcount_inc(&head->refs); 145562306a36Sopenharmony_ci spin_unlock(&delayed_refs->lock); 145662306a36Sopenharmony_ci 145762306a36Sopenharmony_ci btrfs_release_path(path); 145862306a36Sopenharmony_ci 145962306a36Sopenharmony_ci /* 146062306a36Sopenharmony_ci * Mutex was contended, block until it's 146162306a36Sopenharmony_ci * released and try again 146262306a36Sopenharmony_ci */ 146362306a36Sopenharmony_ci mutex_lock(&head->mutex); 146462306a36Sopenharmony_ci mutex_unlock(&head->mutex); 146562306a36Sopenharmony_ci btrfs_put_delayed_ref_head(head); 146662306a36Sopenharmony_ci goto again; 146762306a36Sopenharmony_ci } 146862306a36Sopenharmony_ci spin_unlock(&delayed_refs->lock); 146962306a36Sopenharmony_ci ret = add_delayed_refs(ctx->fs_info, head, ctx->time_seq, 147062306a36Sopenharmony_ci &preftrees, sc); 147162306a36Sopenharmony_ci mutex_unlock(&head->mutex); 147262306a36Sopenharmony_ci if (ret) 147362306a36Sopenharmony_ci goto out; 147462306a36Sopenharmony_ci } else { 147562306a36Sopenharmony_ci spin_unlock(&delayed_refs->lock); 147662306a36Sopenharmony_ci } 147762306a36Sopenharmony_ci } 147862306a36Sopenharmony_ci 147962306a36Sopenharmony_ci if (path->slots[0]) { 148062306a36Sopenharmony_ci struct extent_buffer *leaf; 148162306a36Sopenharmony_ci int slot; 148262306a36Sopenharmony_ci 148362306a36Sopenharmony_ci path->slots[0]--; 148462306a36Sopenharmony_ci leaf = path->nodes[0]; 148562306a36Sopenharmony_ci slot = path->slots[0]; 148662306a36Sopenharmony_ci btrfs_item_key_to_cpu(leaf, &key, slot); 148762306a36Sopenharmony_ci if (key.objectid == ctx->bytenr && 148862306a36Sopenharmony_ci (key.type == BTRFS_EXTENT_ITEM_KEY || 148962306a36Sopenharmony_ci key.type == BTRFS_METADATA_ITEM_KEY)) { 149062306a36Sopenharmony_ci ret = add_inline_refs(ctx, path, &info_level, 149162306a36Sopenharmony_ci &preftrees, sc); 149262306a36Sopenharmony_ci if (ret) 149362306a36Sopenharmony_ci goto out; 149462306a36Sopenharmony_ci ret = add_keyed_refs(ctx, root, path, info_level, 149562306a36Sopenharmony_ci &preftrees, sc); 149662306a36Sopenharmony_ci if (ret) 149762306a36Sopenharmony_ci goto out; 149862306a36Sopenharmony_ci } 149962306a36Sopenharmony_ci } 150062306a36Sopenharmony_ci 150162306a36Sopenharmony_ci /* 150262306a36Sopenharmony_ci * If we have a share context and we reached here, it means the extent 150362306a36Sopenharmony_ci * is not directly shared (no multiple reference items for it), 150462306a36Sopenharmony_ci * otherwise we would have exited earlier with a return value of 150562306a36Sopenharmony_ci * BACKREF_FOUND_SHARED after processing delayed references or while 150662306a36Sopenharmony_ci * processing inline or keyed references from the extent tree. 150762306a36Sopenharmony_ci * The extent may however be indirectly shared through shared subtrees 150862306a36Sopenharmony_ci * as a result from creating snapshots, so we determine below what is 150962306a36Sopenharmony_ci * its parent node, in case we are dealing with a metadata extent, or 151062306a36Sopenharmony_ci * what's the leaf (or leaves), from a fs tree, that has a file extent 151162306a36Sopenharmony_ci * item pointing to it in case we are dealing with a data extent. 151262306a36Sopenharmony_ci */ 151362306a36Sopenharmony_ci ASSERT(extent_is_shared(sc) == 0); 151462306a36Sopenharmony_ci 151562306a36Sopenharmony_ci /* 151662306a36Sopenharmony_ci * If we are here for a data extent and we have a share_check structure 151762306a36Sopenharmony_ci * it means the data extent is not directly shared (does not have 151862306a36Sopenharmony_ci * multiple reference items), so we have to check if a path in the fs 151962306a36Sopenharmony_ci * tree (going from the root node down to the leaf that has the file 152062306a36Sopenharmony_ci * extent item pointing to the data extent) is shared, that is, if any 152162306a36Sopenharmony_ci * of the extent buffers in the path is referenced by other trees. 152262306a36Sopenharmony_ci */ 152362306a36Sopenharmony_ci if (sc && ctx->bytenr == sc->data_bytenr) { 152462306a36Sopenharmony_ci /* 152562306a36Sopenharmony_ci * If our data extent is from a generation more recent than the 152662306a36Sopenharmony_ci * last generation used to snapshot the root, then we know that 152762306a36Sopenharmony_ci * it can not be shared through subtrees, so we can skip 152862306a36Sopenharmony_ci * resolving indirect references, there's no point in 152962306a36Sopenharmony_ci * determining the extent buffers for the path from the fs tree 153062306a36Sopenharmony_ci * root node down to the leaf that has the file extent item that 153162306a36Sopenharmony_ci * points to the data extent. 153262306a36Sopenharmony_ci */ 153362306a36Sopenharmony_ci if (sc->data_extent_gen > 153462306a36Sopenharmony_ci btrfs_root_last_snapshot(&sc->root->root_item)) { 153562306a36Sopenharmony_ci ret = BACKREF_FOUND_NOT_SHARED; 153662306a36Sopenharmony_ci goto out; 153762306a36Sopenharmony_ci } 153862306a36Sopenharmony_ci 153962306a36Sopenharmony_ci /* 154062306a36Sopenharmony_ci * If we are only determining if a data extent is shared or not 154162306a36Sopenharmony_ci * and the corresponding file extent item is located in the same 154262306a36Sopenharmony_ci * leaf as the previous file extent item, we can skip resolving 154362306a36Sopenharmony_ci * indirect references for a data extent, since the fs tree path 154462306a36Sopenharmony_ci * is the same (same leaf, so same path). We skip as long as the 154562306a36Sopenharmony_ci * cached result for the leaf is valid and only if there's only 154662306a36Sopenharmony_ci * one file extent item pointing to the data extent, because in 154762306a36Sopenharmony_ci * the case of multiple file extent items, they may be located 154862306a36Sopenharmony_ci * in different leaves and therefore we have multiple paths. 154962306a36Sopenharmony_ci */ 155062306a36Sopenharmony_ci if (sc->ctx->curr_leaf_bytenr == sc->ctx->prev_leaf_bytenr && 155162306a36Sopenharmony_ci sc->self_ref_count == 1) { 155262306a36Sopenharmony_ci bool cached; 155362306a36Sopenharmony_ci bool is_shared; 155462306a36Sopenharmony_ci 155562306a36Sopenharmony_ci cached = lookup_backref_shared_cache(sc->ctx, sc->root, 155662306a36Sopenharmony_ci sc->ctx->curr_leaf_bytenr, 155762306a36Sopenharmony_ci 0, &is_shared); 155862306a36Sopenharmony_ci if (cached) { 155962306a36Sopenharmony_ci if (is_shared) 156062306a36Sopenharmony_ci ret = BACKREF_FOUND_SHARED; 156162306a36Sopenharmony_ci else 156262306a36Sopenharmony_ci ret = BACKREF_FOUND_NOT_SHARED; 156362306a36Sopenharmony_ci goto out; 156462306a36Sopenharmony_ci } 156562306a36Sopenharmony_ci } 156662306a36Sopenharmony_ci } 156762306a36Sopenharmony_ci 156862306a36Sopenharmony_ci btrfs_release_path(path); 156962306a36Sopenharmony_ci 157062306a36Sopenharmony_ci ret = add_missing_keys(ctx->fs_info, &preftrees, path->skip_locking == 0); 157162306a36Sopenharmony_ci if (ret) 157262306a36Sopenharmony_ci goto out; 157362306a36Sopenharmony_ci 157462306a36Sopenharmony_ci WARN_ON(!RB_EMPTY_ROOT(&preftrees.indirect_missing_keys.root.rb_root)); 157562306a36Sopenharmony_ci 157662306a36Sopenharmony_ci ret = resolve_indirect_refs(ctx, path, &preftrees, sc); 157762306a36Sopenharmony_ci if (ret) 157862306a36Sopenharmony_ci goto out; 157962306a36Sopenharmony_ci 158062306a36Sopenharmony_ci WARN_ON(!RB_EMPTY_ROOT(&preftrees.indirect.root.rb_root)); 158162306a36Sopenharmony_ci 158262306a36Sopenharmony_ci /* 158362306a36Sopenharmony_ci * This walks the tree of merged and resolved refs. Tree blocks are 158462306a36Sopenharmony_ci * read in as needed. Unique entries are added to the ulist, and 158562306a36Sopenharmony_ci * the list of found roots is updated. 158662306a36Sopenharmony_ci * 158762306a36Sopenharmony_ci * We release the entire tree in one go before returning. 158862306a36Sopenharmony_ci */ 158962306a36Sopenharmony_ci node = rb_first_cached(&preftrees.direct.root); 159062306a36Sopenharmony_ci while (node) { 159162306a36Sopenharmony_ci ref = rb_entry(node, struct prelim_ref, rbnode); 159262306a36Sopenharmony_ci node = rb_next(&ref->rbnode); 159362306a36Sopenharmony_ci /* 159462306a36Sopenharmony_ci * ref->count < 0 can happen here if there are delayed 159562306a36Sopenharmony_ci * refs with a node->action of BTRFS_DROP_DELAYED_REF. 159662306a36Sopenharmony_ci * prelim_ref_insert() relies on this when merging 159762306a36Sopenharmony_ci * identical refs to keep the overall count correct. 159862306a36Sopenharmony_ci * prelim_ref_insert() will merge only those refs 159962306a36Sopenharmony_ci * which compare identically. Any refs having 160062306a36Sopenharmony_ci * e.g. different offsets would not be merged, 160162306a36Sopenharmony_ci * and would retain their original ref->count < 0. 160262306a36Sopenharmony_ci */ 160362306a36Sopenharmony_ci if (ctx->roots && ref->count && ref->root_id && ref->parent == 0) { 160462306a36Sopenharmony_ci /* no parent == root of tree */ 160562306a36Sopenharmony_ci ret = ulist_add(ctx->roots, ref->root_id, 0, GFP_NOFS); 160662306a36Sopenharmony_ci if (ret < 0) 160762306a36Sopenharmony_ci goto out; 160862306a36Sopenharmony_ci } 160962306a36Sopenharmony_ci if (ref->count && ref->parent) { 161062306a36Sopenharmony_ci if (!ctx->skip_inode_ref_list && !ref->inode_list && 161162306a36Sopenharmony_ci ref->level == 0) { 161262306a36Sopenharmony_ci struct btrfs_tree_parent_check check = { 0 }; 161362306a36Sopenharmony_ci struct extent_buffer *eb; 161462306a36Sopenharmony_ci 161562306a36Sopenharmony_ci check.level = ref->level; 161662306a36Sopenharmony_ci 161762306a36Sopenharmony_ci eb = read_tree_block(ctx->fs_info, ref->parent, 161862306a36Sopenharmony_ci &check); 161962306a36Sopenharmony_ci if (IS_ERR(eb)) { 162062306a36Sopenharmony_ci ret = PTR_ERR(eb); 162162306a36Sopenharmony_ci goto out; 162262306a36Sopenharmony_ci } 162362306a36Sopenharmony_ci if (!extent_buffer_uptodate(eb)) { 162462306a36Sopenharmony_ci free_extent_buffer(eb); 162562306a36Sopenharmony_ci ret = -EIO; 162662306a36Sopenharmony_ci goto out; 162762306a36Sopenharmony_ci } 162862306a36Sopenharmony_ci 162962306a36Sopenharmony_ci if (!path->skip_locking) 163062306a36Sopenharmony_ci btrfs_tree_read_lock(eb); 163162306a36Sopenharmony_ci ret = find_extent_in_eb(ctx, eb, &eie); 163262306a36Sopenharmony_ci if (!path->skip_locking) 163362306a36Sopenharmony_ci btrfs_tree_read_unlock(eb); 163462306a36Sopenharmony_ci free_extent_buffer(eb); 163562306a36Sopenharmony_ci if (ret == BTRFS_ITERATE_EXTENT_INODES_STOP || 163662306a36Sopenharmony_ci ret < 0) 163762306a36Sopenharmony_ci goto out; 163862306a36Sopenharmony_ci ref->inode_list = eie; 163962306a36Sopenharmony_ci /* 164062306a36Sopenharmony_ci * We transferred the list ownership to the ref, 164162306a36Sopenharmony_ci * so set to NULL to avoid a double free in case 164262306a36Sopenharmony_ci * an error happens after this. 164362306a36Sopenharmony_ci */ 164462306a36Sopenharmony_ci eie = NULL; 164562306a36Sopenharmony_ci } 164662306a36Sopenharmony_ci ret = ulist_add_merge_ptr(ctx->refs, ref->parent, 164762306a36Sopenharmony_ci ref->inode_list, 164862306a36Sopenharmony_ci (void **)&eie, GFP_NOFS); 164962306a36Sopenharmony_ci if (ret < 0) 165062306a36Sopenharmony_ci goto out; 165162306a36Sopenharmony_ci if (!ret && !ctx->skip_inode_ref_list) { 165262306a36Sopenharmony_ci /* 165362306a36Sopenharmony_ci * We've recorded that parent, so we must extend 165462306a36Sopenharmony_ci * its inode list here. 165562306a36Sopenharmony_ci * 165662306a36Sopenharmony_ci * However if there was corruption we may not 165762306a36Sopenharmony_ci * have found an eie, return an error in this 165862306a36Sopenharmony_ci * case. 165962306a36Sopenharmony_ci */ 166062306a36Sopenharmony_ci ASSERT(eie); 166162306a36Sopenharmony_ci if (!eie) { 166262306a36Sopenharmony_ci ret = -EUCLEAN; 166362306a36Sopenharmony_ci goto out; 166462306a36Sopenharmony_ci } 166562306a36Sopenharmony_ci while (eie->next) 166662306a36Sopenharmony_ci eie = eie->next; 166762306a36Sopenharmony_ci eie->next = ref->inode_list; 166862306a36Sopenharmony_ci } 166962306a36Sopenharmony_ci eie = NULL; 167062306a36Sopenharmony_ci /* 167162306a36Sopenharmony_ci * We have transferred the inode list ownership from 167262306a36Sopenharmony_ci * this ref to the ref we added to the 'refs' ulist. 167362306a36Sopenharmony_ci * So set this ref's inode list to NULL to avoid 167462306a36Sopenharmony_ci * use-after-free when our caller uses it or double 167562306a36Sopenharmony_ci * frees in case an error happens before we return. 167662306a36Sopenharmony_ci */ 167762306a36Sopenharmony_ci ref->inode_list = NULL; 167862306a36Sopenharmony_ci } 167962306a36Sopenharmony_ci cond_resched(); 168062306a36Sopenharmony_ci } 168162306a36Sopenharmony_ci 168262306a36Sopenharmony_ciout: 168362306a36Sopenharmony_ci btrfs_free_path(path); 168462306a36Sopenharmony_ci 168562306a36Sopenharmony_ci prelim_release(&preftrees.direct); 168662306a36Sopenharmony_ci prelim_release(&preftrees.indirect); 168762306a36Sopenharmony_ci prelim_release(&preftrees.indirect_missing_keys); 168862306a36Sopenharmony_ci 168962306a36Sopenharmony_ci if (ret == BTRFS_ITERATE_EXTENT_INODES_STOP || ret < 0) 169062306a36Sopenharmony_ci free_inode_elem_list(eie); 169162306a36Sopenharmony_ci return ret; 169262306a36Sopenharmony_ci} 169362306a36Sopenharmony_ci 169462306a36Sopenharmony_ci/* 169562306a36Sopenharmony_ci * Finds all leaves with a reference to the specified combination of 169662306a36Sopenharmony_ci * @ctx->bytenr and @ctx->extent_item_pos. The bytenr of the found leaves are 169762306a36Sopenharmony_ci * added to the ulist at @ctx->refs, and that ulist is allocated by this 169862306a36Sopenharmony_ci * function. The caller should free the ulist with free_leaf_list() if 169962306a36Sopenharmony_ci * @ctx->ignore_extent_item_pos is false, otherwise a fimple ulist_free() is 170062306a36Sopenharmony_ci * enough. 170162306a36Sopenharmony_ci * 170262306a36Sopenharmony_ci * Returns 0 on success and < 0 on error. On error @ctx->refs is not allocated. 170362306a36Sopenharmony_ci */ 170462306a36Sopenharmony_ciint btrfs_find_all_leafs(struct btrfs_backref_walk_ctx *ctx) 170562306a36Sopenharmony_ci{ 170662306a36Sopenharmony_ci int ret; 170762306a36Sopenharmony_ci 170862306a36Sopenharmony_ci ASSERT(ctx->refs == NULL); 170962306a36Sopenharmony_ci 171062306a36Sopenharmony_ci ctx->refs = ulist_alloc(GFP_NOFS); 171162306a36Sopenharmony_ci if (!ctx->refs) 171262306a36Sopenharmony_ci return -ENOMEM; 171362306a36Sopenharmony_ci 171462306a36Sopenharmony_ci ret = find_parent_nodes(ctx, NULL); 171562306a36Sopenharmony_ci if (ret == BTRFS_ITERATE_EXTENT_INODES_STOP || 171662306a36Sopenharmony_ci (ret < 0 && ret != -ENOENT)) { 171762306a36Sopenharmony_ci free_leaf_list(ctx->refs); 171862306a36Sopenharmony_ci ctx->refs = NULL; 171962306a36Sopenharmony_ci return ret; 172062306a36Sopenharmony_ci } 172162306a36Sopenharmony_ci 172262306a36Sopenharmony_ci return 0; 172362306a36Sopenharmony_ci} 172462306a36Sopenharmony_ci 172562306a36Sopenharmony_ci/* 172662306a36Sopenharmony_ci * Walk all backrefs for a given extent to find all roots that reference this 172762306a36Sopenharmony_ci * extent. Walking a backref means finding all extents that reference this 172862306a36Sopenharmony_ci * extent and in turn walk the backrefs of those, too. Naturally this is a 172962306a36Sopenharmony_ci * recursive process, but here it is implemented in an iterative fashion: We 173062306a36Sopenharmony_ci * find all referencing extents for the extent in question and put them on a 173162306a36Sopenharmony_ci * list. In turn, we find all referencing extents for those, further appending 173262306a36Sopenharmony_ci * to the list. The way we iterate the list allows adding more elements after 173362306a36Sopenharmony_ci * the current while iterating. The process stops when we reach the end of the 173462306a36Sopenharmony_ci * list. 173562306a36Sopenharmony_ci * 173662306a36Sopenharmony_ci * Found roots are added to @ctx->roots, which is allocated by this function if 173762306a36Sopenharmony_ci * it points to NULL, in which case the caller is responsible for freeing it 173862306a36Sopenharmony_ci * after it's not needed anymore. 173962306a36Sopenharmony_ci * This function requires @ctx->refs to be NULL, as it uses it for allocating a 174062306a36Sopenharmony_ci * ulist to do temporary work, and frees it before returning. 174162306a36Sopenharmony_ci * 174262306a36Sopenharmony_ci * Returns 0 on success, < 0 on error. 174362306a36Sopenharmony_ci */ 174462306a36Sopenharmony_cistatic int btrfs_find_all_roots_safe(struct btrfs_backref_walk_ctx *ctx) 174562306a36Sopenharmony_ci{ 174662306a36Sopenharmony_ci const u64 orig_bytenr = ctx->bytenr; 174762306a36Sopenharmony_ci const bool orig_skip_inode_ref_list = ctx->skip_inode_ref_list; 174862306a36Sopenharmony_ci bool roots_ulist_allocated = false; 174962306a36Sopenharmony_ci struct ulist_iterator uiter; 175062306a36Sopenharmony_ci int ret = 0; 175162306a36Sopenharmony_ci 175262306a36Sopenharmony_ci ASSERT(ctx->refs == NULL); 175362306a36Sopenharmony_ci 175462306a36Sopenharmony_ci ctx->refs = ulist_alloc(GFP_NOFS); 175562306a36Sopenharmony_ci if (!ctx->refs) 175662306a36Sopenharmony_ci return -ENOMEM; 175762306a36Sopenharmony_ci 175862306a36Sopenharmony_ci if (!ctx->roots) { 175962306a36Sopenharmony_ci ctx->roots = ulist_alloc(GFP_NOFS); 176062306a36Sopenharmony_ci if (!ctx->roots) { 176162306a36Sopenharmony_ci ulist_free(ctx->refs); 176262306a36Sopenharmony_ci ctx->refs = NULL; 176362306a36Sopenharmony_ci return -ENOMEM; 176462306a36Sopenharmony_ci } 176562306a36Sopenharmony_ci roots_ulist_allocated = true; 176662306a36Sopenharmony_ci } 176762306a36Sopenharmony_ci 176862306a36Sopenharmony_ci ctx->skip_inode_ref_list = true; 176962306a36Sopenharmony_ci 177062306a36Sopenharmony_ci ULIST_ITER_INIT(&uiter); 177162306a36Sopenharmony_ci while (1) { 177262306a36Sopenharmony_ci struct ulist_node *node; 177362306a36Sopenharmony_ci 177462306a36Sopenharmony_ci ret = find_parent_nodes(ctx, NULL); 177562306a36Sopenharmony_ci if (ret < 0 && ret != -ENOENT) { 177662306a36Sopenharmony_ci if (roots_ulist_allocated) { 177762306a36Sopenharmony_ci ulist_free(ctx->roots); 177862306a36Sopenharmony_ci ctx->roots = NULL; 177962306a36Sopenharmony_ci } 178062306a36Sopenharmony_ci break; 178162306a36Sopenharmony_ci } 178262306a36Sopenharmony_ci ret = 0; 178362306a36Sopenharmony_ci node = ulist_next(ctx->refs, &uiter); 178462306a36Sopenharmony_ci if (!node) 178562306a36Sopenharmony_ci break; 178662306a36Sopenharmony_ci ctx->bytenr = node->val; 178762306a36Sopenharmony_ci cond_resched(); 178862306a36Sopenharmony_ci } 178962306a36Sopenharmony_ci 179062306a36Sopenharmony_ci ulist_free(ctx->refs); 179162306a36Sopenharmony_ci ctx->refs = NULL; 179262306a36Sopenharmony_ci ctx->bytenr = orig_bytenr; 179362306a36Sopenharmony_ci ctx->skip_inode_ref_list = orig_skip_inode_ref_list; 179462306a36Sopenharmony_ci 179562306a36Sopenharmony_ci return ret; 179662306a36Sopenharmony_ci} 179762306a36Sopenharmony_ci 179862306a36Sopenharmony_ciint btrfs_find_all_roots(struct btrfs_backref_walk_ctx *ctx, 179962306a36Sopenharmony_ci bool skip_commit_root_sem) 180062306a36Sopenharmony_ci{ 180162306a36Sopenharmony_ci int ret; 180262306a36Sopenharmony_ci 180362306a36Sopenharmony_ci if (!ctx->trans && !skip_commit_root_sem) 180462306a36Sopenharmony_ci down_read(&ctx->fs_info->commit_root_sem); 180562306a36Sopenharmony_ci ret = btrfs_find_all_roots_safe(ctx); 180662306a36Sopenharmony_ci if (!ctx->trans && !skip_commit_root_sem) 180762306a36Sopenharmony_ci up_read(&ctx->fs_info->commit_root_sem); 180862306a36Sopenharmony_ci return ret; 180962306a36Sopenharmony_ci} 181062306a36Sopenharmony_ci 181162306a36Sopenharmony_cistruct btrfs_backref_share_check_ctx *btrfs_alloc_backref_share_check_ctx(void) 181262306a36Sopenharmony_ci{ 181362306a36Sopenharmony_ci struct btrfs_backref_share_check_ctx *ctx; 181462306a36Sopenharmony_ci 181562306a36Sopenharmony_ci ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); 181662306a36Sopenharmony_ci if (!ctx) 181762306a36Sopenharmony_ci return NULL; 181862306a36Sopenharmony_ci 181962306a36Sopenharmony_ci ulist_init(&ctx->refs); 182062306a36Sopenharmony_ci 182162306a36Sopenharmony_ci return ctx; 182262306a36Sopenharmony_ci} 182362306a36Sopenharmony_ci 182462306a36Sopenharmony_civoid btrfs_free_backref_share_ctx(struct btrfs_backref_share_check_ctx *ctx) 182562306a36Sopenharmony_ci{ 182662306a36Sopenharmony_ci if (!ctx) 182762306a36Sopenharmony_ci return; 182862306a36Sopenharmony_ci 182962306a36Sopenharmony_ci ulist_release(&ctx->refs); 183062306a36Sopenharmony_ci kfree(ctx); 183162306a36Sopenharmony_ci} 183262306a36Sopenharmony_ci 183362306a36Sopenharmony_ci/* 183462306a36Sopenharmony_ci * Check if a data extent is shared or not. 183562306a36Sopenharmony_ci * 183662306a36Sopenharmony_ci * @inode: The inode whose extent we are checking. 183762306a36Sopenharmony_ci * @bytenr: Logical bytenr of the extent we are checking. 183862306a36Sopenharmony_ci * @extent_gen: Generation of the extent (file extent item) or 0 if it is 183962306a36Sopenharmony_ci * not known. 184062306a36Sopenharmony_ci * @ctx: A backref sharedness check context. 184162306a36Sopenharmony_ci * 184262306a36Sopenharmony_ci * btrfs_is_data_extent_shared uses the backref walking code but will short 184362306a36Sopenharmony_ci * circuit as soon as it finds a root or inode that doesn't match the 184462306a36Sopenharmony_ci * one passed in. This provides a significant performance benefit for 184562306a36Sopenharmony_ci * callers (such as fiemap) which want to know whether the extent is 184662306a36Sopenharmony_ci * shared but do not need a ref count. 184762306a36Sopenharmony_ci * 184862306a36Sopenharmony_ci * This attempts to attach to the running transaction in order to account for 184962306a36Sopenharmony_ci * delayed refs, but continues on even when no running transaction exists. 185062306a36Sopenharmony_ci * 185162306a36Sopenharmony_ci * Return: 0 if extent is not shared, 1 if it is shared, < 0 on error. 185262306a36Sopenharmony_ci */ 185362306a36Sopenharmony_ciint btrfs_is_data_extent_shared(struct btrfs_inode *inode, u64 bytenr, 185462306a36Sopenharmony_ci u64 extent_gen, 185562306a36Sopenharmony_ci struct btrfs_backref_share_check_ctx *ctx) 185662306a36Sopenharmony_ci{ 185762306a36Sopenharmony_ci struct btrfs_backref_walk_ctx walk_ctx = { 0 }; 185862306a36Sopenharmony_ci struct btrfs_root *root = inode->root; 185962306a36Sopenharmony_ci struct btrfs_fs_info *fs_info = root->fs_info; 186062306a36Sopenharmony_ci struct btrfs_trans_handle *trans; 186162306a36Sopenharmony_ci struct ulist_iterator uiter; 186262306a36Sopenharmony_ci struct ulist_node *node; 186362306a36Sopenharmony_ci struct btrfs_seq_list elem = BTRFS_SEQ_LIST_INIT(elem); 186462306a36Sopenharmony_ci int ret = 0; 186562306a36Sopenharmony_ci struct share_check shared = { 186662306a36Sopenharmony_ci .ctx = ctx, 186762306a36Sopenharmony_ci .root = root, 186862306a36Sopenharmony_ci .inum = btrfs_ino(inode), 186962306a36Sopenharmony_ci .data_bytenr = bytenr, 187062306a36Sopenharmony_ci .data_extent_gen = extent_gen, 187162306a36Sopenharmony_ci .share_count = 0, 187262306a36Sopenharmony_ci .self_ref_count = 0, 187362306a36Sopenharmony_ci .have_delayed_delete_refs = false, 187462306a36Sopenharmony_ci }; 187562306a36Sopenharmony_ci int level; 187662306a36Sopenharmony_ci bool leaf_cached; 187762306a36Sopenharmony_ci bool leaf_is_shared; 187862306a36Sopenharmony_ci 187962306a36Sopenharmony_ci for (int i = 0; i < BTRFS_BACKREF_CTX_PREV_EXTENTS_SIZE; i++) { 188062306a36Sopenharmony_ci if (ctx->prev_extents_cache[i].bytenr == bytenr) 188162306a36Sopenharmony_ci return ctx->prev_extents_cache[i].is_shared; 188262306a36Sopenharmony_ci } 188362306a36Sopenharmony_ci 188462306a36Sopenharmony_ci ulist_init(&ctx->refs); 188562306a36Sopenharmony_ci 188662306a36Sopenharmony_ci trans = btrfs_join_transaction_nostart(root); 188762306a36Sopenharmony_ci if (IS_ERR(trans)) { 188862306a36Sopenharmony_ci if (PTR_ERR(trans) != -ENOENT && PTR_ERR(trans) != -EROFS) { 188962306a36Sopenharmony_ci ret = PTR_ERR(trans); 189062306a36Sopenharmony_ci goto out; 189162306a36Sopenharmony_ci } 189262306a36Sopenharmony_ci trans = NULL; 189362306a36Sopenharmony_ci down_read(&fs_info->commit_root_sem); 189462306a36Sopenharmony_ci } else { 189562306a36Sopenharmony_ci btrfs_get_tree_mod_seq(fs_info, &elem); 189662306a36Sopenharmony_ci walk_ctx.time_seq = elem.seq; 189762306a36Sopenharmony_ci } 189862306a36Sopenharmony_ci 189962306a36Sopenharmony_ci ctx->use_path_cache = true; 190062306a36Sopenharmony_ci 190162306a36Sopenharmony_ci /* 190262306a36Sopenharmony_ci * We may have previously determined that the current leaf is shared. 190362306a36Sopenharmony_ci * If it is, then we have a data extent that is shared due to a shared 190462306a36Sopenharmony_ci * subtree (caused by snapshotting) and we don't need to check for data 190562306a36Sopenharmony_ci * backrefs. If the leaf is not shared, then we must do backref walking 190662306a36Sopenharmony_ci * to determine if the data extent is shared through reflinks. 190762306a36Sopenharmony_ci */ 190862306a36Sopenharmony_ci leaf_cached = lookup_backref_shared_cache(ctx, root, 190962306a36Sopenharmony_ci ctx->curr_leaf_bytenr, 0, 191062306a36Sopenharmony_ci &leaf_is_shared); 191162306a36Sopenharmony_ci if (leaf_cached && leaf_is_shared) { 191262306a36Sopenharmony_ci ret = 1; 191362306a36Sopenharmony_ci goto out_trans; 191462306a36Sopenharmony_ci } 191562306a36Sopenharmony_ci 191662306a36Sopenharmony_ci walk_ctx.skip_inode_ref_list = true; 191762306a36Sopenharmony_ci walk_ctx.trans = trans; 191862306a36Sopenharmony_ci walk_ctx.fs_info = fs_info; 191962306a36Sopenharmony_ci walk_ctx.refs = &ctx->refs; 192062306a36Sopenharmony_ci 192162306a36Sopenharmony_ci /* -1 means we are in the bytenr of the data extent. */ 192262306a36Sopenharmony_ci level = -1; 192362306a36Sopenharmony_ci ULIST_ITER_INIT(&uiter); 192462306a36Sopenharmony_ci while (1) { 192562306a36Sopenharmony_ci const unsigned long prev_ref_count = ctx->refs.nnodes; 192662306a36Sopenharmony_ci 192762306a36Sopenharmony_ci walk_ctx.bytenr = bytenr; 192862306a36Sopenharmony_ci ret = find_parent_nodes(&walk_ctx, &shared); 192962306a36Sopenharmony_ci if (ret == BACKREF_FOUND_SHARED || 193062306a36Sopenharmony_ci ret == BACKREF_FOUND_NOT_SHARED) { 193162306a36Sopenharmony_ci /* If shared must return 1, otherwise return 0. */ 193262306a36Sopenharmony_ci ret = (ret == BACKREF_FOUND_SHARED) ? 1 : 0; 193362306a36Sopenharmony_ci if (level >= 0) 193462306a36Sopenharmony_ci store_backref_shared_cache(ctx, root, bytenr, 193562306a36Sopenharmony_ci level, ret == 1); 193662306a36Sopenharmony_ci break; 193762306a36Sopenharmony_ci } 193862306a36Sopenharmony_ci if (ret < 0 && ret != -ENOENT) 193962306a36Sopenharmony_ci break; 194062306a36Sopenharmony_ci ret = 0; 194162306a36Sopenharmony_ci 194262306a36Sopenharmony_ci /* 194362306a36Sopenharmony_ci * More than one extent buffer (bytenr) may have been added to 194462306a36Sopenharmony_ci * the ctx->refs ulist, in which case we have to check multiple 194562306a36Sopenharmony_ci * tree paths in case the first one is not shared, so we can not 194662306a36Sopenharmony_ci * use the path cache which is made for a single path. Multiple 194762306a36Sopenharmony_ci * extent buffers at the current level happen when: 194862306a36Sopenharmony_ci * 194962306a36Sopenharmony_ci * 1) level -1, the data extent: If our data extent was not 195062306a36Sopenharmony_ci * directly shared (without multiple reference items), then 195162306a36Sopenharmony_ci * it might have a single reference item with a count > 1 for 195262306a36Sopenharmony_ci * the same offset, which means there are 2 (or more) file 195362306a36Sopenharmony_ci * extent items that point to the data extent - this happens 195462306a36Sopenharmony_ci * when a file extent item needs to be split and then one 195562306a36Sopenharmony_ci * item gets moved to another leaf due to a b+tree leaf split 195662306a36Sopenharmony_ci * when inserting some item. In this case the file extent 195762306a36Sopenharmony_ci * items may be located in different leaves and therefore 195862306a36Sopenharmony_ci * some of the leaves may be referenced through shared 195962306a36Sopenharmony_ci * subtrees while others are not. Since our extent buffer 196062306a36Sopenharmony_ci * cache only works for a single path (by far the most common 196162306a36Sopenharmony_ci * case and simpler to deal with), we can not use it if we 196262306a36Sopenharmony_ci * have multiple leaves (which implies multiple paths). 196362306a36Sopenharmony_ci * 196462306a36Sopenharmony_ci * 2) level >= 0, a tree node/leaf: We can have a mix of direct 196562306a36Sopenharmony_ci * and indirect references on a b+tree node/leaf, so we have 196662306a36Sopenharmony_ci * to check multiple paths, and the extent buffer (the 196762306a36Sopenharmony_ci * current bytenr) may be shared or not. One example is 196862306a36Sopenharmony_ci * during relocation as we may get a shared tree block ref 196962306a36Sopenharmony_ci * (direct ref) and a non-shared tree block ref (indirect 197062306a36Sopenharmony_ci * ref) for the same node/leaf. 197162306a36Sopenharmony_ci */ 197262306a36Sopenharmony_ci if ((ctx->refs.nnodes - prev_ref_count) > 1) 197362306a36Sopenharmony_ci ctx->use_path_cache = false; 197462306a36Sopenharmony_ci 197562306a36Sopenharmony_ci if (level >= 0) 197662306a36Sopenharmony_ci store_backref_shared_cache(ctx, root, bytenr, 197762306a36Sopenharmony_ci level, false); 197862306a36Sopenharmony_ci node = ulist_next(&ctx->refs, &uiter); 197962306a36Sopenharmony_ci if (!node) 198062306a36Sopenharmony_ci break; 198162306a36Sopenharmony_ci bytenr = node->val; 198262306a36Sopenharmony_ci if (ctx->use_path_cache) { 198362306a36Sopenharmony_ci bool is_shared; 198462306a36Sopenharmony_ci bool cached; 198562306a36Sopenharmony_ci 198662306a36Sopenharmony_ci level++; 198762306a36Sopenharmony_ci cached = lookup_backref_shared_cache(ctx, root, bytenr, 198862306a36Sopenharmony_ci level, &is_shared); 198962306a36Sopenharmony_ci if (cached) { 199062306a36Sopenharmony_ci ret = (is_shared ? 1 : 0); 199162306a36Sopenharmony_ci break; 199262306a36Sopenharmony_ci } 199362306a36Sopenharmony_ci } 199462306a36Sopenharmony_ci shared.share_count = 0; 199562306a36Sopenharmony_ci shared.have_delayed_delete_refs = false; 199662306a36Sopenharmony_ci cond_resched(); 199762306a36Sopenharmony_ci } 199862306a36Sopenharmony_ci 199962306a36Sopenharmony_ci /* 200062306a36Sopenharmony_ci * If the path cache is disabled, then it means at some tree level we 200162306a36Sopenharmony_ci * got multiple parents due to a mix of direct and indirect backrefs or 200262306a36Sopenharmony_ci * multiple leaves with file extent items pointing to the same data 200362306a36Sopenharmony_ci * extent. We have to invalidate the cache and cache only the sharedness 200462306a36Sopenharmony_ci * result for the levels where we got only one node/reference. 200562306a36Sopenharmony_ci */ 200662306a36Sopenharmony_ci if (!ctx->use_path_cache) { 200762306a36Sopenharmony_ci int i = 0; 200862306a36Sopenharmony_ci 200962306a36Sopenharmony_ci level--; 201062306a36Sopenharmony_ci if (ret >= 0 && level >= 0) { 201162306a36Sopenharmony_ci bytenr = ctx->path_cache_entries[level].bytenr; 201262306a36Sopenharmony_ci ctx->use_path_cache = true; 201362306a36Sopenharmony_ci store_backref_shared_cache(ctx, root, bytenr, level, ret); 201462306a36Sopenharmony_ci i = level + 1; 201562306a36Sopenharmony_ci } 201662306a36Sopenharmony_ci 201762306a36Sopenharmony_ci for ( ; i < BTRFS_MAX_LEVEL; i++) 201862306a36Sopenharmony_ci ctx->path_cache_entries[i].bytenr = 0; 201962306a36Sopenharmony_ci } 202062306a36Sopenharmony_ci 202162306a36Sopenharmony_ci /* 202262306a36Sopenharmony_ci * Cache the sharedness result for the data extent if we know our inode 202362306a36Sopenharmony_ci * has more than 1 file extent item that refers to the data extent. 202462306a36Sopenharmony_ci */ 202562306a36Sopenharmony_ci if (ret >= 0 && shared.self_ref_count > 1) { 202662306a36Sopenharmony_ci int slot = ctx->prev_extents_cache_slot; 202762306a36Sopenharmony_ci 202862306a36Sopenharmony_ci ctx->prev_extents_cache[slot].bytenr = shared.data_bytenr; 202962306a36Sopenharmony_ci ctx->prev_extents_cache[slot].is_shared = (ret == 1); 203062306a36Sopenharmony_ci 203162306a36Sopenharmony_ci slot = (slot + 1) % BTRFS_BACKREF_CTX_PREV_EXTENTS_SIZE; 203262306a36Sopenharmony_ci ctx->prev_extents_cache_slot = slot; 203362306a36Sopenharmony_ci } 203462306a36Sopenharmony_ci 203562306a36Sopenharmony_ciout_trans: 203662306a36Sopenharmony_ci if (trans) { 203762306a36Sopenharmony_ci btrfs_put_tree_mod_seq(fs_info, &elem); 203862306a36Sopenharmony_ci btrfs_end_transaction(trans); 203962306a36Sopenharmony_ci } else { 204062306a36Sopenharmony_ci up_read(&fs_info->commit_root_sem); 204162306a36Sopenharmony_ci } 204262306a36Sopenharmony_ciout: 204362306a36Sopenharmony_ci ulist_release(&ctx->refs); 204462306a36Sopenharmony_ci ctx->prev_leaf_bytenr = ctx->curr_leaf_bytenr; 204562306a36Sopenharmony_ci 204662306a36Sopenharmony_ci return ret; 204762306a36Sopenharmony_ci} 204862306a36Sopenharmony_ci 204962306a36Sopenharmony_ciint btrfs_find_one_extref(struct btrfs_root *root, u64 inode_objectid, 205062306a36Sopenharmony_ci u64 start_off, struct btrfs_path *path, 205162306a36Sopenharmony_ci struct btrfs_inode_extref **ret_extref, 205262306a36Sopenharmony_ci u64 *found_off) 205362306a36Sopenharmony_ci{ 205462306a36Sopenharmony_ci int ret, slot; 205562306a36Sopenharmony_ci struct btrfs_key key; 205662306a36Sopenharmony_ci struct btrfs_key found_key; 205762306a36Sopenharmony_ci struct btrfs_inode_extref *extref; 205862306a36Sopenharmony_ci const struct extent_buffer *leaf; 205962306a36Sopenharmony_ci unsigned long ptr; 206062306a36Sopenharmony_ci 206162306a36Sopenharmony_ci key.objectid = inode_objectid; 206262306a36Sopenharmony_ci key.type = BTRFS_INODE_EXTREF_KEY; 206362306a36Sopenharmony_ci key.offset = start_off; 206462306a36Sopenharmony_ci 206562306a36Sopenharmony_ci ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); 206662306a36Sopenharmony_ci if (ret < 0) 206762306a36Sopenharmony_ci return ret; 206862306a36Sopenharmony_ci 206962306a36Sopenharmony_ci while (1) { 207062306a36Sopenharmony_ci leaf = path->nodes[0]; 207162306a36Sopenharmony_ci slot = path->slots[0]; 207262306a36Sopenharmony_ci if (slot >= btrfs_header_nritems(leaf)) { 207362306a36Sopenharmony_ci /* 207462306a36Sopenharmony_ci * If the item at offset is not found, 207562306a36Sopenharmony_ci * btrfs_search_slot will point us to the slot 207662306a36Sopenharmony_ci * where it should be inserted. In our case 207762306a36Sopenharmony_ci * that will be the slot directly before the 207862306a36Sopenharmony_ci * next INODE_REF_KEY_V2 item. In the case 207962306a36Sopenharmony_ci * that we're pointing to the last slot in a 208062306a36Sopenharmony_ci * leaf, we must move one leaf over. 208162306a36Sopenharmony_ci */ 208262306a36Sopenharmony_ci ret = btrfs_next_leaf(root, path); 208362306a36Sopenharmony_ci if (ret) { 208462306a36Sopenharmony_ci if (ret >= 1) 208562306a36Sopenharmony_ci ret = -ENOENT; 208662306a36Sopenharmony_ci break; 208762306a36Sopenharmony_ci } 208862306a36Sopenharmony_ci continue; 208962306a36Sopenharmony_ci } 209062306a36Sopenharmony_ci 209162306a36Sopenharmony_ci btrfs_item_key_to_cpu(leaf, &found_key, slot); 209262306a36Sopenharmony_ci 209362306a36Sopenharmony_ci /* 209462306a36Sopenharmony_ci * Check that we're still looking at an extended ref key for 209562306a36Sopenharmony_ci * this particular objectid. If we have different 209662306a36Sopenharmony_ci * objectid or type then there are no more to be found 209762306a36Sopenharmony_ci * in the tree and we can exit. 209862306a36Sopenharmony_ci */ 209962306a36Sopenharmony_ci ret = -ENOENT; 210062306a36Sopenharmony_ci if (found_key.objectid != inode_objectid) 210162306a36Sopenharmony_ci break; 210262306a36Sopenharmony_ci if (found_key.type != BTRFS_INODE_EXTREF_KEY) 210362306a36Sopenharmony_ci break; 210462306a36Sopenharmony_ci 210562306a36Sopenharmony_ci ret = 0; 210662306a36Sopenharmony_ci ptr = btrfs_item_ptr_offset(leaf, path->slots[0]); 210762306a36Sopenharmony_ci extref = (struct btrfs_inode_extref *)ptr; 210862306a36Sopenharmony_ci *ret_extref = extref; 210962306a36Sopenharmony_ci if (found_off) 211062306a36Sopenharmony_ci *found_off = found_key.offset; 211162306a36Sopenharmony_ci break; 211262306a36Sopenharmony_ci } 211362306a36Sopenharmony_ci 211462306a36Sopenharmony_ci return ret; 211562306a36Sopenharmony_ci} 211662306a36Sopenharmony_ci 211762306a36Sopenharmony_ci/* 211862306a36Sopenharmony_ci * this iterates to turn a name (from iref/extref) into a full filesystem path. 211962306a36Sopenharmony_ci * Elements of the path are separated by '/' and the path is guaranteed to be 212062306a36Sopenharmony_ci * 0-terminated. the path is only given within the current file system. 212162306a36Sopenharmony_ci * Therefore, it never starts with a '/'. the caller is responsible to provide 212262306a36Sopenharmony_ci * "size" bytes in "dest". the dest buffer will be filled backwards. finally, 212362306a36Sopenharmony_ci * the start point of the resulting string is returned. this pointer is within 212462306a36Sopenharmony_ci * dest, normally. 212562306a36Sopenharmony_ci * in case the path buffer would overflow, the pointer is decremented further 212662306a36Sopenharmony_ci * as if output was written to the buffer, though no more output is actually 212762306a36Sopenharmony_ci * generated. that way, the caller can determine how much space would be 212862306a36Sopenharmony_ci * required for the path to fit into the buffer. in that case, the returned 212962306a36Sopenharmony_ci * value will be smaller than dest. callers must check this! 213062306a36Sopenharmony_ci */ 213162306a36Sopenharmony_cichar *btrfs_ref_to_path(struct btrfs_root *fs_root, struct btrfs_path *path, 213262306a36Sopenharmony_ci u32 name_len, unsigned long name_off, 213362306a36Sopenharmony_ci struct extent_buffer *eb_in, u64 parent, 213462306a36Sopenharmony_ci char *dest, u32 size) 213562306a36Sopenharmony_ci{ 213662306a36Sopenharmony_ci int slot; 213762306a36Sopenharmony_ci u64 next_inum; 213862306a36Sopenharmony_ci int ret; 213962306a36Sopenharmony_ci s64 bytes_left = ((s64)size) - 1; 214062306a36Sopenharmony_ci struct extent_buffer *eb = eb_in; 214162306a36Sopenharmony_ci struct btrfs_key found_key; 214262306a36Sopenharmony_ci struct btrfs_inode_ref *iref; 214362306a36Sopenharmony_ci 214462306a36Sopenharmony_ci if (bytes_left >= 0) 214562306a36Sopenharmony_ci dest[bytes_left] = '\0'; 214662306a36Sopenharmony_ci 214762306a36Sopenharmony_ci while (1) { 214862306a36Sopenharmony_ci bytes_left -= name_len; 214962306a36Sopenharmony_ci if (bytes_left >= 0) 215062306a36Sopenharmony_ci read_extent_buffer(eb, dest + bytes_left, 215162306a36Sopenharmony_ci name_off, name_len); 215262306a36Sopenharmony_ci if (eb != eb_in) { 215362306a36Sopenharmony_ci if (!path->skip_locking) 215462306a36Sopenharmony_ci btrfs_tree_read_unlock(eb); 215562306a36Sopenharmony_ci free_extent_buffer(eb); 215662306a36Sopenharmony_ci } 215762306a36Sopenharmony_ci ret = btrfs_find_item(fs_root, path, parent, 0, 215862306a36Sopenharmony_ci BTRFS_INODE_REF_KEY, &found_key); 215962306a36Sopenharmony_ci if (ret > 0) 216062306a36Sopenharmony_ci ret = -ENOENT; 216162306a36Sopenharmony_ci if (ret) 216262306a36Sopenharmony_ci break; 216362306a36Sopenharmony_ci 216462306a36Sopenharmony_ci next_inum = found_key.offset; 216562306a36Sopenharmony_ci 216662306a36Sopenharmony_ci /* regular exit ahead */ 216762306a36Sopenharmony_ci if (parent == next_inum) 216862306a36Sopenharmony_ci break; 216962306a36Sopenharmony_ci 217062306a36Sopenharmony_ci slot = path->slots[0]; 217162306a36Sopenharmony_ci eb = path->nodes[0]; 217262306a36Sopenharmony_ci /* make sure we can use eb after releasing the path */ 217362306a36Sopenharmony_ci if (eb != eb_in) { 217462306a36Sopenharmony_ci path->nodes[0] = NULL; 217562306a36Sopenharmony_ci path->locks[0] = 0; 217662306a36Sopenharmony_ci } 217762306a36Sopenharmony_ci btrfs_release_path(path); 217862306a36Sopenharmony_ci iref = btrfs_item_ptr(eb, slot, struct btrfs_inode_ref); 217962306a36Sopenharmony_ci 218062306a36Sopenharmony_ci name_len = btrfs_inode_ref_name_len(eb, iref); 218162306a36Sopenharmony_ci name_off = (unsigned long)(iref + 1); 218262306a36Sopenharmony_ci 218362306a36Sopenharmony_ci parent = next_inum; 218462306a36Sopenharmony_ci --bytes_left; 218562306a36Sopenharmony_ci if (bytes_left >= 0) 218662306a36Sopenharmony_ci dest[bytes_left] = '/'; 218762306a36Sopenharmony_ci } 218862306a36Sopenharmony_ci 218962306a36Sopenharmony_ci btrfs_release_path(path); 219062306a36Sopenharmony_ci 219162306a36Sopenharmony_ci if (ret) 219262306a36Sopenharmony_ci return ERR_PTR(ret); 219362306a36Sopenharmony_ci 219462306a36Sopenharmony_ci return dest + bytes_left; 219562306a36Sopenharmony_ci} 219662306a36Sopenharmony_ci 219762306a36Sopenharmony_ci/* 219862306a36Sopenharmony_ci * this makes the path point to (logical EXTENT_ITEM *) 219962306a36Sopenharmony_ci * returns BTRFS_EXTENT_FLAG_DATA for data, BTRFS_EXTENT_FLAG_TREE_BLOCK for 220062306a36Sopenharmony_ci * tree blocks and <0 on error. 220162306a36Sopenharmony_ci */ 220262306a36Sopenharmony_ciint extent_from_logical(struct btrfs_fs_info *fs_info, u64 logical, 220362306a36Sopenharmony_ci struct btrfs_path *path, struct btrfs_key *found_key, 220462306a36Sopenharmony_ci u64 *flags_ret) 220562306a36Sopenharmony_ci{ 220662306a36Sopenharmony_ci struct btrfs_root *extent_root = btrfs_extent_root(fs_info, logical); 220762306a36Sopenharmony_ci int ret; 220862306a36Sopenharmony_ci u64 flags; 220962306a36Sopenharmony_ci u64 size = 0; 221062306a36Sopenharmony_ci u32 item_size; 221162306a36Sopenharmony_ci const struct extent_buffer *eb; 221262306a36Sopenharmony_ci struct btrfs_extent_item *ei; 221362306a36Sopenharmony_ci struct btrfs_key key; 221462306a36Sopenharmony_ci 221562306a36Sopenharmony_ci if (btrfs_fs_incompat(fs_info, SKINNY_METADATA)) 221662306a36Sopenharmony_ci key.type = BTRFS_METADATA_ITEM_KEY; 221762306a36Sopenharmony_ci else 221862306a36Sopenharmony_ci key.type = BTRFS_EXTENT_ITEM_KEY; 221962306a36Sopenharmony_ci key.objectid = logical; 222062306a36Sopenharmony_ci key.offset = (u64)-1; 222162306a36Sopenharmony_ci 222262306a36Sopenharmony_ci ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0); 222362306a36Sopenharmony_ci if (ret < 0) 222462306a36Sopenharmony_ci return ret; 222562306a36Sopenharmony_ci 222662306a36Sopenharmony_ci ret = btrfs_previous_extent_item(extent_root, path, 0); 222762306a36Sopenharmony_ci if (ret) { 222862306a36Sopenharmony_ci if (ret > 0) 222962306a36Sopenharmony_ci ret = -ENOENT; 223062306a36Sopenharmony_ci return ret; 223162306a36Sopenharmony_ci } 223262306a36Sopenharmony_ci btrfs_item_key_to_cpu(path->nodes[0], found_key, path->slots[0]); 223362306a36Sopenharmony_ci if (found_key->type == BTRFS_METADATA_ITEM_KEY) 223462306a36Sopenharmony_ci size = fs_info->nodesize; 223562306a36Sopenharmony_ci else if (found_key->type == BTRFS_EXTENT_ITEM_KEY) 223662306a36Sopenharmony_ci size = found_key->offset; 223762306a36Sopenharmony_ci 223862306a36Sopenharmony_ci if (found_key->objectid > logical || 223962306a36Sopenharmony_ci found_key->objectid + size <= logical) { 224062306a36Sopenharmony_ci btrfs_debug(fs_info, 224162306a36Sopenharmony_ci "logical %llu is not within any extent", logical); 224262306a36Sopenharmony_ci return -ENOENT; 224362306a36Sopenharmony_ci } 224462306a36Sopenharmony_ci 224562306a36Sopenharmony_ci eb = path->nodes[0]; 224662306a36Sopenharmony_ci item_size = btrfs_item_size(eb, path->slots[0]); 224762306a36Sopenharmony_ci BUG_ON(item_size < sizeof(*ei)); 224862306a36Sopenharmony_ci 224962306a36Sopenharmony_ci ei = btrfs_item_ptr(eb, path->slots[0], struct btrfs_extent_item); 225062306a36Sopenharmony_ci flags = btrfs_extent_flags(eb, ei); 225162306a36Sopenharmony_ci 225262306a36Sopenharmony_ci btrfs_debug(fs_info, 225362306a36Sopenharmony_ci "logical %llu is at position %llu within the extent (%llu EXTENT_ITEM %llu) flags %#llx size %u", 225462306a36Sopenharmony_ci logical, logical - found_key->objectid, found_key->objectid, 225562306a36Sopenharmony_ci found_key->offset, flags, item_size); 225662306a36Sopenharmony_ci 225762306a36Sopenharmony_ci WARN_ON(!flags_ret); 225862306a36Sopenharmony_ci if (flags_ret) { 225962306a36Sopenharmony_ci if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) 226062306a36Sopenharmony_ci *flags_ret = BTRFS_EXTENT_FLAG_TREE_BLOCK; 226162306a36Sopenharmony_ci else if (flags & BTRFS_EXTENT_FLAG_DATA) 226262306a36Sopenharmony_ci *flags_ret = BTRFS_EXTENT_FLAG_DATA; 226362306a36Sopenharmony_ci else 226462306a36Sopenharmony_ci BUG(); 226562306a36Sopenharmony_ci return 0; 226662306a36Sopenharmony_ci } 226762306a36Sopenharmony_ci 226862306a36Sopenharmony_ci return -EIO; 226962306a36Sopenharmony_ci} 227062306a36Sopenharmony_ci 227162306a36Sopenharmony_ci/* 227262306a36Sopenharmony_ci * helper function to iterate extent inline refs. ptr must point to a 0 value 227362306a36Sopenharmony_ci * for the first call and may be modified. it is used to track state. 227462306a36Sopenharmony_ci * if more refs exist, 0 is returned and the next call to 227562306a36Sopenharmony_ci * get_extent_inline_ref must pass the modified ptr parameter to get the 227662306a36Sopenharmony_ci * next ref. after the last ref was processed, 1 is returned. 227762306a36Sopenharmony_ci * returns <0 on error 227862306a36Sopenharmony_ci */ 227962306a36Sopenharmony_cistatic int get_extent_inline_ref(unsigned long *ptr, 228062306a36Sopenharmony_ci const struct extent_buffer *eb, 228162306a36Sopenharmony_ci const struct btrfs_key *key, 228262306a36Sopenharmony_ci const struct btrfs_extent_item *ei, 228362306a36Sopenharmony_ci u32 item_size, 228462306a36Sopenharmony_ci struct btrfs_extent_inline_ref **out_eiref, 228562306a36Sopenharmony_ci int *out_type) 228662306a36Sopenharmony_ci{ 228762306a36Sopenharmony_ci unsigned long end; 228862306a36Sopenharmony_ci u64 flags; 228962306a36Sopenharmony_ci struct btrfs_tree_block_info *info; 229062306a36Sopenharmony_ci 229162306a36Sopenharmony_ci if (!*ptr) { 229262306a36Sopenharmony_ci /* first call */ 229362306a36Sopenharmony_ci flags = btrfs_extent_flags(eb, ei); 229462306a36Sopenharmony_ci if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) { 229562306a36Sopenharmony_ci if (key->type == BTRFS_METADATA_ITEM_KEY) { 229662306a36Sopenharmony_ci /* a skinny metadata extent */ 229762306a36Sopenharmony_ci *out_eiref = 229862306a36Sopenharmony_ci (struct btrfs_extent_inline_ref *)(ei + 1); 229962306a36Sopenharmony_ci } else { 230062306a36Sopenharmony_ci WARN_ON(key->type != BTRFS_EXTENT_ITEM_KEY); 230162306a36Sopenharmony_ci info = (struct btrfs_tree_block_info *)(ei + 1); 230262306a36Sopenharmony_ci *out_eiref = 230362306a36Sopenharmony_ci (struct btrfs_extent_inline_ref *)(info + 1); 230462306a36Sopenharmony_ci } 230562306a36Sopenharmony_ci } else { 230662306a36Sopenharmony_ci *out_eiref = (struct btrfs_extent_inline_ref *)(ei + 1); 230762306a36Sopenharmony_ci } 230862306a36Sopenharmony_ci *ptr = (unsigned long)*out_eiref; 230962306a36Sopenharmony_ci if ((unsigned long)(*ptr) >= (unsigned long)ei + item_size) 231062306a36Sopenharmony_ci return -ENOENT; 231162306a36Sopenharmony_ci } 231262306a36Sopenharmony_ci 231362306a36Sopenharmony_ci end = (unsigned long)ei + item_size; 231462306a36Sopenharmony_ci *out_eiref = (struct btrfs_extent_inline_ref *)(*ptr); 231562306a36Sopenharmony_ci *out_type = btrfs_get_extent_inline_ref_type(eb, *out_eiref, 231662306a36Sopenharmony_ci BTRFS_REF_TYPE_ANY); 231762306a36Sopenharmony_ci if (*out_type == BTRFS_REF_TYPE_INVALID) 231862306a36Sopenharmony_ci return -EUCLEAN; 231962306a36Sopenharmony_ci 232062306a36Sopenharmony_ci *ptr += btrfs_extent_inline_ref_size(*out_type); 232162306a36Sopenharmony_ci WARN_ON(*ptr > end); 232262306a36Sopenharmony_ci if (*ptr == end) 232362306a36Sopenharmony_ci return 1; /* last */ 232462306a36Sopenharmony_ci 232562306a36Sopenharmony_ci return 0; 232662306a36Sopenharmony_ci} 232762306a36Sopenharmony_ci 232862306a36Sopenharmony_ci/* 232962306a36Sopenharmony_ci * reads the tree block backref for an extent. tree level and root are returned 233062306a36Sopenharmony_ci * through out_level and out_root. ptr must point to a 0 value for the first 233162306a36Sopenharmony_ci * call and may be modified (see get_extent_inline_ref comment). 233262306a36Sopenharmony_ci * returns 0 if data was provided, 1 if there was no more data to provide or 233362306a36Sopenharmony_ci * <0 on error. 233462306a36Sopenharmony_ci */ 233562306a36Sopenharmony_ciint tree_backref_for_extent(unsigned long *ptr, struct extent_buffer *eb, 233662306a36Sopenharmony_ci struct btrfs_key *key, struct btrfs_extent_item *ei, 233762306a36Sopenharmony_ci u32 item_size, u64 *out_root, u8 *out_level) 233862306a36Sopenharmony_ci{ 233962306a36Sopenharmony_ci int ret; 234062306a36Sopenharmony_ci int type; 234162306a36Sopenharmony_ci struct btrfs_extent_inline_ref *eiref; 234262306a36Sopenharmony_ci 234362306a36Sopenharmony_ci if (*ptr == (unsigned long)-1) 234462306a36Sopenharmony_ci return 1; 234562306a36Sopenharmony_ci 234662306a36Sopenharmony_ci while (1) { 234762306a36Sopenharmony_ci ret = get_extent_inline_ref(ptr, eb, key, ei, item_size, 234862306a36Sopenharmony_ci &eiref, &type); 234962306a36Sopenharmony_ci if (ret < 0) 235062306a36Sopenharmony_ci return ret; 235162306a36Sopenharmony_ci 235262306a36Sopenharmony_ci if (type == BTRFS_TREE_BLOCK_REF_KEY || 235362306a36Sopenharmony_ci type == BTRFS_SHARED_BLOCK_REF_KEY) 235462306a36Sopenharmony_ci break; 235562306a36Sopenharmony_ci 235662306a36Sopenharmony_ci if (ret == 1) 235762306a36Sopenharmony_ci return 1; 235862306a36Sopenharmony_ci } 235962306a36Sopenharmony_ci 236062306a36Sopenharmony_ci /* we can treat both ref types equally here */ 236162306a36Sopenharmony_ci *out_root = btrfs_extent_inline_ref_offset(eb, eiref); 236262306a36Sopenharmony_ci 236362306a36Sopenharmony_ci if (key->type == BTRFS_EXTENT_ITEM_KEY) { 236462306a36Sopenharmony_ci struct btrfs_tree_block_info *info; 236562306a36Sopenharmony_ci 236662306a36Sopenharmony_ci info = (struct btrfs_tree_block_info *)(ei + 1); 236762306a36Sopenharmony_ci *out_level = btrfs_tree_block_level(eb, info); 236862306a36Sopenharmony_ci } else { 236962306a36Sopenharmony_ci ASSERT(key->type == BTRFS_METADATA_ITEM_KEY); 237062306a36Sopenharmony_ci *out_level = (u8)key->offset; 237162306a36Sopenharmony_ci } 237262306a36Sopenharmony_ci 237362306a36Sopenharmony_ci if (ret == 1) 237462306a36Sopenharmony_ci *ptr = (unsigned long)-1; 237562306a36Sopenharmony_ci 237662306a36Sopenharmony_ci return 0; 237762306a36Sopenharmony_ci} 237862306a36Sopenharmony_ci 237962306a36Sopenharmony_cistatic int iterate_leaf_refs(struct btrfs_fs_info *fs_info, 238062306a36Sopenharmony_ci struct extent_inode_elem *inode_list, 238162306a36Sopenharmony_ci u64 root, u64 extent_item_objectid, 238262306a36Sopenharmony_ci iterate_extent_inodes_t *iterate, void *ctx) 238362306a36Sopenharmony_ci{ 238462306a36Sopenharmony_ci struct extent_inode_elem *eie; 238562306a36Sopenharmony_ci int ret = 0; 238662306a36Sopenharmony_ci 238762306a36Sopenharmony_ci for (eie = inode_list; eie; eie = eie->next) { 238862306a36Sopenharmony_ci btrfs_debug(fs_info, 238962306a36Sopenharmony_ci "ref for %llu resolved, key (%llu EXTEND_DATA %llu), root %llu", 239062306a36Sopenharmony_ci extent_item_objectid, eie->inum, 239162306a36Sopenharmony_ci eie->offset, root); 239262306a36Sopenharmony_ci ret = iterate(eie->inum, eie->offset, eie->num_bytes, root, ctx); 239362306a36Sopenharmony_ci if (ret) { 239462306a36Sopenharmony_ci btrfs_debug(fs_info, 239562306a36Sopenharmony_ci "stopping iteration for %llu due to ret=%d", 239662306a36Sopenharmony_ci extent_item_objectid, ret); 239762306a36Sopenharmony_ci break; 239862306a36Sopenharmony_ci } 239962306a36Sopenharmony_ci } 240062306a36Sopenharmony_ci 240162306a36Sopenharmony_ci return ret; 240262306a36Sopenharmony_ci} 240362306a36Sopenharmony_ci 240462306a36Sopenharmony_ci/* 240562306a36Sopenharmony_ci * calls iterate() for every inode that references the extent identified by 240662306a36Sopenharmony_ci * the given parameters. 240762306a36Sopenharmony_ci * when the iterator function returns a non-zero value, iteration stops. 240862306a36Sopenharmony_ci */ 240962306a36Sopenharmony_ciint iterate_extent_inodes(struct btrfs_backref_walk_ctx *ctx, 241062306a36Sopenharmony_ci bool search_commit_root, 241162306a36Sopenharmony_ci iterate_extent_inodes_t *iterate, void *user_ctx) 241262306a36Sopenharmony_ci{ 241362306a36Sopenharmony_ci int ret; 241462306a36Sopenharmony_ci struct ulist *refs; 241562306a36Sopenharmony_ci struct ulist_node *ref_node; 241662306a36Sopenharmony_ci struct btrfs_seq_list seq_elem = BTRFS_SEQ_LIST_INIT(seq_elem); 241762306a36Sopenharmony_ci struct ulist_iterator ref_uiter; 241862306a36Sopenharmony_ci 241962306a36Sopenharmony_ci btrfs_debug(ctx->fs_info, "resolving all inodes for extent %llu", 242062306a36Sopenharmony_ci ctx->bytenr); 242162306a36Sopenharmony_ci 242262306a36Sopenharmony_ci ASSERT(ctx->trans == NULL); 242362306a36Sopenharmony_ci ASSERT(ctx->roots == NULL); 242462306a36Sopenharmony_ci 242562306a36Sopenharmony_ci if (!search_commit_root) { 242662306a36Sopenharmony_ci struct btrfs_trans_handle *trans; 242762306a36Sopenharmony_ci 242862306a36Sopenharmony_ci trans = btrfs_attach_transaction(ctx->fs_info->tree_root); 242962306a36Sopenharmony_ci if (IS_ERR(trans)) { 243062306a36Sopenharmony_ci if (PTR_ERR(trans) != -ENOENT && 243162306a36Sopenharmony_ci PTR_ERR(trans) != -EROFS) 243262306a36Sopenharmony_ci return PTR_ERR(trans); 243362306a36Sopenharmony_ci trans = NULL; 243462306a36Sopenharmony_ci } 243562306a36Sopenharmony_ci ctx->trans = trans; 243662306a36Sopenharmony_ci } 243762306a36Sopenharmony_ci 243862306a36Sopenharmony_ci if (ctx->trans) { 243962306a36Sopenharmony_ci btrfs_get_tree_mod_seq(ctx->fs_info, &seq_elem); 244062306a36Sopenharmony_ci ctx->time_seq = seq_elem.seq; 244162306a36Sopenharmony_ci } else { 244262306a36Sopenharmony_ci down_read(&ctx->fs_info->commit_root_sem); 244362306a36Sopenharmony_ci } 244462306a36Sopenharmony_ci 244562306a36Sopenharmony_ci ret = btrfs_find_all_leafs(ctx); 244662306a36Sopenharmony_ci if (ret) 244762306a36Sopenharmony_ci goto out; 244862306a36Sopenharmony_ci refs = ctx->refs; 244962306a36Sopenharmony_ci ctx->refs = NULL; 245062306a36Sopenharmony_ci 245162306a36Sopenharmony_ci ULIST_ITER_INIT(&ref_uiter); 245262306a36Sopenharmony_ci while (!ret && (ref_node = ulist_next(refs, &ref_uiter))) { 245362306a36Sopenharmony_ci const u64 leaf_bytenr = ref_node->val; 245462306a36Sopenharmony_ci struct ulist_node *root_node; 245562306a36Sopenharmony_ci struct ulist_iterator root_uiter; 245662306a36Sopenharmony_ci struct extent_inode_elem *inode_list; 245762306a36Sopenharmony_ci 245862306a36Sopenharmony_ci inode_list = (struct extent_inode_elem *)(uintptr_t)ref_node->aux; 245962306a36Sopenharmony_ci 246062306a36Sopenharmony_ci if (ctx->cache_lookup) { 246162306a36Sopenharmony_ci const u64 *root_ids; 246262306a36Sopenharmony_ci int root_count; 246362306a36Sopenharmony_ci bool cached; 246462306a36Sopenharmony_ci 246562306a36Sopenharmony_ci cached = ctx->cache_lookup(leaf_bytenr, ctx->user_ctx, 246662306a36Sopenharmony_ci &root_ids, &root_count); 246762306a36Sopenharmony_ci if (cached) { 246862306a36Sopenharmony_ci for (int i = 0; i < root_count; i++) { 246962306a36Sopenharmony_ci ret = iterate_leaf_refs(ctx->fs_info, 247062306a36Sopenharmony_ci inode_list, 247162306a36Sopenharmony_ci root_ids[i], 247262306a36Sopenharmony_ci leaf_bytenr, 247362306a36Sopenharmony_ci iterate, 247462306a36Sopenharmony_ci user_ctx); 247562306a36Sopenharmony_ci if (ret) 247662306a36Sopenharmony_ci break; 247762306a36Sopenharmony_ci } 247862306a36Sopenharmony_ci continue; 247962306a36Sopenharmony_ci } 248062306a36Sopenharmony_ci } 248162306a36Sopenharmony_ci 248262306a36Sopenharmony_ci if (!ctx->roots) { 248362306a36Sopenharmony_ci ctx->roots = ulist_alloc(GFP_NOFS); 248462306a36Sopenharmony_ci if (!ctx->roots) { 248562306a36Sopenharmony_ci ret = -ENOMEM; 248662306a36Sopenharmony_ci break; 248762306a36Sopenharmony_ci } 248862306a36Sopenharmony_ci } 248962306a36Sopenharmony_ci 249062306a36Sopenharmony_ci ctx->bytenr = leaf_bytenr; 249162306a36Sopenharmony_ci ret = btrfs_find_all_roots_safe(ctx); 249262306a36Sopenharmony_ci if (ret) 249362306a36Sopenharmony_ci break; 249462306a36Sopenharmony_ci 249562306a36Sopenharmony_ci if (ctx->cache_store) 249662306a36Sopenharmony_ci ctx->cache_store(leaf_bytenr, ctx->roots, ctx->user_ctx); 249762306a36Sopenharmony_ci 249862306a36Sopenharmony_ci ULIST_ITER_INIT(&root_uiter); 249962306a36Sopenharmony_ci while (!ret && (root_node = ulist_next(ctx->roots, &root_uiter))) { 250062306a36Sopenharmony_ci btrfs_debug(ctx->fs_info, 250162306a36Sopenharmony_ci "root %llu references leaf %llu, data list %#llx", 250262306a36Sopenharmony_ci root_node->val, ref_node->val, 250362306a36Sopenharmony_ci ref_node->aux); 250462306a36Sopenharmony_ci ret = iterate_leaf_refs(ctx->fs_info, inode_list, 250562306a36Sopenharmony_ci root_node->val, ctx->bytenr, 250662306a36Sopenharmony_ci iterate, user_ctx); 250762306a36Sopenharmony_ci } 250862306a36Sopenharmony_ci ulist_reinit(ctx->roots); 250962306a36Sopenharmony_ci } 251062306a36Sopenharmony_ci 251162306a36Sopenharmony_ci free_leaf_list(refs); 251262306a36Sopenharmony_ciout: 251362306a36Sopenharmony_ci if (ctx->trans) { 251462306a36Sopenharmony_ci btrfs_put_tree_mod_seq(ctx->fs_info, &seq_elem); 251562306a36Sopenharmony_ci btrfs_end_transaction(ctx->trans); 251662306a36Sopenharmony_ci ctx->trans = NULL; 251762306a36Sopenharmony_ci } else { 251862306a36Sopenharmony_ci up_read(&ctx->fs_info->commit_root_sem); 251962306a36Sopenharmony_ci } 252062306a36Sopenharmony_ci 252162306a36Sopenharmony_ci ulist_free(ctx->roots); 252262306a36Sopenharmony_ci ctx->roots = NULL; 252362306a36Sopenharmony_ci 252462306a36Sopenharmony_ci if (ret == BTRFS_ITERATE_EXTENT_INODES_STOP) 252562306a36Sopenharmony_ci ret = 0; 252662306a36Sopenharmony_ci 252762306a36Sopenharmony_ci return ret; 252862306a36Sopenharmony_ci} 252962306a36Sopenharmony_ci 253062306a36Sopenharmony_cistatic int build_ino_list(u64 inum, u64 offset, u64 num_bytes, u64 root, void *ctx) 253162306a36Sopenharmony_ci{ 253262306a36Sopenharmony_ci struct btrfs_data_container *inodes = ctx; 253362306a36Sopenharmony_ci const size_t c = 3 * sizeof(u64); 253462306a36Sopenharmony_ci 253562306a36Sopenharmony_ci if (inodes->bytes_left >= c) { 253662306a36Sopenharmony_ci inodes->bytes_left -= c; 253762306a36Sopenharmony_ci inodes->val[inodes->elem_cnt] = inum; 253862306a36Sopenharmony_ci inodes->val[inodes->elem_cnt + 1] = offset; 253962306a36Sopenharmony_ci inodes->val[inodes->elem_cnt + 2] = root; 254062306a36Sopenharmony_ci inodes->elem_cnt += 3; 254162306a36Sopenharmony_ci } else { 254262306a36Sopenharmony_ci inodes->bytes_missing += c - inodes->bytes_left; 254362306a36Sopenharmony_ci inodes->bytes_left = 0; 254462306a36Sopenharmony_ci inodes->elem_missed += 3; 254562306a36Sopenharmony_ci } 254662306a36Sopenharmony_ci 254762306a36Sopenharmony_ci return 0; 254862306a36Sopenharmony_ci} 254962306a36Sopenharmony_ci 255062306a36Sopenharmony_ciint iterate_inodes_from_logical(u64 logical, struct btrfs_fs_info *fs_info, 255162306a36Sopenharmony_ci struct btrfs_path *path, 255262306a36Sopenharmony_ci void *ctx, bool ignore_offset) 255362306a36Sopenharmony_ci{ 255462306a36Sopenharmony_ci struct btrfs_backref_walk_ctx walk_ctx = { 0 }; 255562306a36Sopenharmony_ci int ret; 255662306a36Sopenharmony_ci u64 flags = 0; 255762306a36Sopenharmony_ci struct btrfs_key found_key; 255862306a36Sopenharmony_ci int search_commit_root = path->search_commit_root; 255962306a36Sopenharmony_ci 256062306a36Sopenharmony_ci ret = extent_from_logical(fs_info, logical, path, &found_key, &flags); 256162306a36Sopenharmony_ci btrfs_release_path(path); 256262306a36Sopenharmony_ci if (ret < 0) 256362306a36Sopenharmony_ci return ret; 256462306a36Sopenharmony_ci if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) 256562306a36Sopenharmony_ci return -EINVAL; 256662306a36Sopenharmony_ci 256762306a36Sopenharmony_ci walk_ctx.bytenr = found_key.objectid; 256862306a36Sopenharmony_ci if (ignore_offset) 256962306a36Sopenharmony_ci walk_ctx.ignore_extent_item_pos = true; 257062306a36Sopenharmony_ci else 257162306a36Sopenharmony_ci walk_ctx.extent_item_pos = logical - found_key.objectid; 257262306a36Sopenharmony_ci walk_ctx.fs_info = fs_info; 257362306a36Sopenharmony_ci 257462306a36Sopenharmony_ci return iterate_extent_inodes(&walk_ctx, search_commit_root, 257562306a36Sopenharmony_ci build_ino_list, ctx); 257662306a36Sopenharmony_ci} 257762306a36Sopenharmony_ci 257862306a36Sopenharmony_cistatic int inode_to_path(u64 inum, u32 name_len, unsigned long name_off, 257962306a36Sopenharmony_ci struct extent_buffer *eb, struct inode_fs_paths *ipath); 258062306a36Sopenharmony_ci 258162306a36Sopenharmony_cistatic int iterate_inode_refs(u64 inum, struct inode_fs_paths *ipath) 258262306a36Sopenharmony_ci{ 258362306a36Sopenharmony_ci int ret = 0; 258462306a36Sopenharmony_ci int slot; 258562306a36Sopenharmony_ci u32 cur; 258662306a36Sopenharmony_ci u32 len; 258762306a36Sopenharmony_ci u32 name_len; 258862306a36Sopenharmony_ci u64 parent = 0; 258962306a36Sopenharmony_ci int found = 0; 259062306a36Sopenharmony_ci struct btrfs_root *fs_root = ipath->fs_root; 259162306a36Sopenharmony_ci struct btrfs_path *path = ipath->btrfs_path; 259262306a36Sopenharmony_ci struct extent_buffer *eb; 259362306a36Sopenharmony_ci struct btrfs_inode_ref *iref; 259462306a36Sopenharmony_ci struct btrfs_key found_key; 259562306a36Sopenharmony_ci 259662306a36Sopenharmony_ci while (!ret) { 259762306a36Sopenharmony_ci ret = btrfs_find_item(fs_root, path, inum, 259862306a36Sopenharmony_ci parent ? parent + 1 : 0, BTRFS_INODE_REF_KEY, 259962306a36Sopenharmony_ci &found_key); 260062306a36Sopenharmony_ci 260162306a36Sopenharmony_ci if (ret < 0) 260262306a36Sopenharmony_ci break; 260362306a36Sopenharmony_ci if (ret) { 260462306a36Sopenharmony_ci ret = found ? 0 : -ENOENT; 260562306a36Sopenharmony_ci break; 260662306a36Sopenharmony_ci } 260762306a36Sopenharmony_ci ++found; 260862306a36Sopenharmony_ci 260962306a36Sopenharmony_ci parent = found_key.offset; 261062306a36Sopenharmony_ci slot = path->slots[0]; 261162306a36Sopenharmony_ci eb = btrfs_clone_extent_buffer(path->nodes[0]); 261262306a36Sopenharmony_ci if (!eb) { 261362306a36Sopenharmony_ci ret = -ENOMEM; 261462306a36Sopenharmony_ci break; 261562306a36Sopenharmony_ci } 261662306a36Sopenharmony_ci btrfs_release_path(path); 261762306a36Sopenharmony_ci 261862306a36Sopenharmony_ci iref = btrfs_item_ptr(eb, slot, struct btrfs_inode_ref); 261962306a36Sopenharmony_ci 262062306a36Sopenharmony_ci for (cur = 0; cur < btrfs_item_size(eb, slot); cur += len) { 262162306a36Sopenharmony_ci name_len = btrfs_inode_ref_name_len(eb, iref); 262262306a36Sopenharmony_ci /* path must be released before calling iterate()! */ 262362306a36Sopenharmony_ci btrfs_debug(fs_root->fs_info, 262462306a36Sopenharmony_ci "following ref at offset %u for inode %llu in tree %llu", 262562306a36Sopenharmony_ci cur, found_key.objectid, 262662306a36Sopenharmony_ci fs_root->root_key.objectid); 262762306a36Sopenharmony_ci ret = inode_to_path(parent, name_len, 262862306a36Sopenharmony_ci (unsigned long)(iref + 1), eb, ipath); 262962306a36Sopenharmony_ci if (ret) 263062306a36Sopenharmony_ci break; 263162306a36Sopenharmony_ci len = sizeof(*iref) + name_len; 263262306a36Sopenharmony_ci iref = (struct btrfs_inode_ref *)((char *)iref + len); 263362306a36Sopenharmony_ci } 263462306a36Sopenharmony_ci free_extent_buffer(eb); 263562306a36Sopenharmony_ci } 263662306a36Sopenharmony_ci 263762306a36Sopenharmony_ci btrfs_release_path(path); 263862306a36Sopenharmony_ci 263962306a36Sopenharmony_ci return ret; 264062306a36Sopenharmony_ci} 264162306a36Sopenharmony_ci 264262306a36Sopenharmony_cistatic int iterate_inode_extrefs(u64 inum, struct inode_fs_paths *ipath) 264362306a36Sopenharmony_ci{ 264462306a36Sopenharmony_ci int ret; 264562306a36Sopenharmony_ci int slot; 264662306a36Sopenharmony_ci u64 offset = 0; 264762306a36Sopenharmony_ci u64 parent; 264862306a36Sopenharmony_ci int found = 0; 264962306a36Sopenharmony_ci struct btrfs_root *fs_root = ipath->fs_root; 265062306a36Sopenharmony_ci struct btrfs_path *path = ipath->btrfs_path; 265162306a36Sopenharmony_ci struct extent_buffer *eb; 265262306a36Sopenharmony_ci struct btrfs_inode_extref *extref; 265362306a36Sopenharmony_ci u32 item_size; 265462306a36Sopenharmony_ci u32 cur_offset; 265562306a36Sopenharmony_ci unsigned long ptr; 265662306a36Sopenharmony_ci 265762306a36Sopenharmony_ci while (1) { 265862306a36Sopenharmony_ci ret = btrfs_find_one_extref(fs_root, inum, offset, path, &extref, 265962306a36Sopenharmony_ci &offset); 266062306a36Sopenharmony_ci if (ret < 0) 266162306a36Sopenharmony_ci break; 266262306a36Sopenharmony_ci if (ret) { 266362306a36Sopenharmony_ci ret = found ? 0 : -ENOENT; 266462306a36Sopenharmony_ci break; 266562306a36Sopenharmony_ci } 266662306a36Sopenharmony_ci ++found; 266762306a36Sopenharmony_ci 266862306a36Sopenharmony_ci slot = path->slots[0]; 266962306a36Sopenharmony_ci eb = btrfs_clone_extent_buffer(path->nodes[0]); 267062306a36Sopenharmony_ci if (!eb) { 267162306a36Sopenharmony_ci ret = -ENOMEM; 267262306a36Sopenharmony_ci break; 267362306a36Sopenharmony_ci } 267462306a36Sopenharmony_ci btrfs_release_path(path); 267562306a36Sopenharmony_ci 267662306a36Sopenharmony_ci item_size = btrfs_item_size(eb, slot); 267762306a36Sopenharmony_ci ptr = btrfs_item_ptr_offset(eb, slot); 267862306a36Sopenharmony_ci cur_offset = 0; 267962306a36Sopenharmony_ci 268062306a36Sopenharmony_ci while (cur_offset < item_size) { 268162306a36Sopenharmony_ci u32 name_len; 268262306a36Sopenharmony_ci 268362306a36Sopenharmony_ci extref = (struct btrfs_inode_extref *)(ptr + cur_offset); 268462306a36Sopenharmony_ci parent = btrfs_inode_extref_parent(eb, extref); 268562306a36Sopenharmony_ci name_len = btrfs_inode_extref_name_len(eb, extref); 268662306a36Sopenharmony_ci ret = inode_to_path(parent, name_len, 268762306a36Sopenharmony_ci (unsigned long)&extref->name, eb, ipath); 268862306a36Sopenharmony_ci if (ret) 268962306a36Sopenharmony_ci break; 269062306a36Sopenharmony_ci 269162306a36Sopenharmony_ci cur_offset += btrfs_inode_extref_name_len(eb, extref); 269262306a36Sopenharmony_ci cur_offset += sizeof(*extref); 269362306a36Sopenharmony_ci } 269462306a36Sopenharmony_ci free_extent_buffer(eb); 269562306a36Sopenharmony_ci 269662306a36Sopenharmony_ci offset++; 269762306a36Sopenharmony_ci } 269862306a36Sopenharmony_ci 269962306a36Sopenharmony_ci btrfs_release_path(path); 270062306a36Sopenharmony_ci 270162306a36Sopenharmony_ci return ret; 270262306a36Sopenharmony_ci} 270362306a36Sopenharmony_ci 270462306a36Sopenharmony_ci/* 270562306a36Sopenharmony_ci * returns 0 if the path could be dumped (probably truncated) 270662306a36Sopenharmony_ci * returns <0 in case of an error 270762306a36Sopenharmony_ci */ 270862306a36Sopenharmony_cistatic int inode_to_path(u64 inum, u32 name_len, unsigned long name_off, 270962306a36Sopenharmony_ci struct extent_buffer *eb, struct inode_fs_paths *ipath) 271062306a36Sopenharmony_ci{ 271162306a36Sopenharmony_ci char *fspath; 271262306a36Sopenharmony_ci char *fspath_min; 271362306a36Sopenharmony_ci int i = ipath->fspath->elem_cnt; 271462306a36Sopenharmony_ci const int s_ptr = sizeof(char *); 271562306a36Sopenharmony_ci u32 bytes_left; 271662306a36Sopenharmony_ci 271762306a36Sopenharmony_ci bytes_left = ipath->fspath->bytes_left > s_ptr ? 271862306a36Sopenharmony_ci ipath->fspath->bytes_left - s_ptr : 0; 271962306a36Sopenharmony_ci 272062306a36Sopenharmony_ci fspath_min = (char *)ipath->fspath->val + (i + 1) * s_ptr; 272162306a36Sopenharmony_ci fspath = btrfs_ref_to_path(ipath->fs_root, ipath->btrfs_path, name_len, 272262306a36Sopenharmony_ci name_off, eb, inum, fspath_min, bytes_left); 272362306a36Sopenharmony_ci if (IS_ERR(fspath)) 272462306a36Sopenharmony_ci return PTR_ERR(fspath); 272562306a36Sopenharmony_ci 272662306a36Sopenharmony_ci if (fspath > fspath_min) { 272762306a36Sopenharmony_ci ipath->fspath->val[i] = (u64)(unsigned long)fspath; 272862306a36Sopenharmony_ci ++ipath->fspath->elem_cnt; 272962306a36Sopenharmony_ci ipath->fspath->bytes_left = fspath - fspath_min; 273062306a36Sopenharmony_ci } else { 273162306a36Sopenharmony_ci ++ipath->fspath->elem_missed; 273262306a36Sopenharmony_ci ipath->fspath->bytes_missing += fspath_min - fspath; 273362306a36Sopenharmony_ci ipath->fspath->bytes_left = 0; 273462306a36Sopenharmony_ci } 273562306a36Sopenharmony_ci 273662306a36Sopenharmony_ci return 0; 273762306a36Sopenharmony_ci} 273862306a36Sopenharmony_ci 273962306a36Sopenharmony_ci/* 274062306a36Sopenharmony_ci * this dumps all file system paths to the inode into the ipath struct, provided 274162306a36Sopenharmony_ci * is has been created large enough. each path is zero-terminated and accessed 274262306a36Sopenharmony_ci * from ipath->fspath->val[i]. 274362306a36Sopenharmony_ci * when it returns, there are ipath->fspath->elem_cnt number of paths available 274462306a36Sopenharmony_ci * in ipath->fspath->val[]. when the allocated space wasn't sufficient, the 274562306a36Sopenharmony_ci * number of missed paths is recorded in ipath->fspath->elem_missed, otherwise, 274662306a36Sopenharmony_ci * it's zero. ipath->fspath->bytes_missing holds the number of bytes that would 274762306a36Sopenharmony_ci * have been needed to return all paths. 274862306a36Sopenharmony_ci */ 274962306a36Sopenharmony_ciint paths_from_inode(u64 inum, struct inode_fs_paths *ipath) 275062306a36Sopenharmony_ci{ 275162306a36Sopenharmony_ci int ret; 275262306a36Sopenharmony_ci int found_refs = 0; 275362306a36Sopenharmony_ci 275462306a36Sopenharmony_ci ret = iterate_inode_refs(inum, ipath); 275562306a36Sopenharmony_ci if (!ret) 275662306a36Sopenharmony_ci ++found_refs; 275762306a36Sopenharmony_ci else if (ret != -ENOENT) 275862306a36Sopenharmony_ci return ret; 275962306a36Sopenharmony_ci 276062306a36Sopenharmony_ci ret = iterate_inode_extrefs(inum, ipath); 276162306a36Sopenharmony_ci if (ret == -ENOENT && found_refs) 276262306a36Sopenharmony_ci return 0; 276362306a36Sopenharmony_ci 276462306a36Sopenharmony_ci return ret; 276562306a36Sopenharmony_ci} 276662306a36Sopenharmony_ci 276762306a36Sopenharmony_cistruct btrfs_data_container *init_data_container(u32 total_bytes) 276862306a36Sopenharmony_ci{ 276962306a36Sopenharmony_ci struct btrfs_data_container *data; 277062306a36Sopenharmony_ci size_t alloc_bytes; 277162306a36Sopenharmony_ci 277262306a36Sopenharmony_ci alloc_bytes = max_t(size_t, total_bytes, sizeof(*data)); 277362306a36Sopenharmony_ci data = kvmalloc(alloc_bytes, GFP_KERNEL); 277462306a36Sopenharmony_ci if (!data) 277562306a36Sopenharmony_ci return ERR_PTR(-ENOMEM); 277662306a36Sopenharmony_ci 277762306a36Sopenharmony_ci if (total_bytes >= sizeof(*data)) { 277862306a36Sopenharmony_ci data->bytes_left = total_bytes - sizeof(*data); 277962306a36Sopenharmony_ci data->bytes_missing = 0; 278062306a36Sopenharmony_ci } else { 278162306a36Sopenharmony_ci data->bytes_missing = sizeof(*data) - total_bytes; 278262306a36Sopenharmony_ci data->bytes_left = 0; 278362306a36Sopenharmony_ci } 278462306a36Sopenharmony_ci 278562306a36Sopenharmony_ci data->elem_cnt = 0; 278662306a36Sopenharmony_ci data->elem_missed = 0; 278762306a36Sopenharmony_ci 278862306a36Sopenharmony_ci return data; 278962306a36Sopenharmony_ci} 279062306a36Sopenharmony_ci 279162306a36Sopenharmony_ci/* 279262306a36Sopenharmony_ci * allocates space to return multiple file system paths for an inode. 279362306a36Sopenharmony_ci * total_bytes to allocate are passed, note that space usable for actual path 279462306a36Sopenharmony_ci * information will be total_bytes - sizeof(struct inode_fs_paths). 279562306a36Sopenharmony_ci * the returned pointer must be freed with free_ipath() in the end. 279662306a36Sopenharmony_ci */ 279762306a36Sopenharmony_cistruct inode_fs_paths *init_ipath(s32 total_bytes, struct btrfs_root *fs_root, 279862306a36Sopenharmony_ci struct btrfs_path *path) 279962306a36Sopenharmony_ci{ 280062306a36Sopenharmony_ci struct inode_fs_paths *ifp; 280162306a36Sopenharmony_ci struct btrfs_data_container *fspath; 280262306a36Sopenharmony_ci 280362306a36Sopenharmony_ci fspath = init_data_container(total_bytes); 280462306a36Sopenharmony_ci if (IS_ERR(fspath)) 280562306a36Sopenharmony_ci return ERR_CAST(fspath); 280662306a36Sopenharmony_ci 280762306a36Sopenharmony_ci ifp = kmalloc(sizeof(*ifp), GFP_KERNEL); 280862306a36Sopenharmony_ci if (!ifp) { 280962306a36Sopenharmony_ci kvfree(fspath); 281062306a36Sopenharmony_ci return ERR_PTR(-ENOMEM); 281162306a36Sopenharmony_ci } 281262306a36Sopenharmony_ci 281362306a36Sopenharmony_ci ifp->btrfs_path = path; 281462306a36Sopenharmony_ci ifp->fspath = fspath; 281562306a36Sopenharmony_ci ifp->fs_root = fs_root; 281662306a36Sopenharmony_ci 281762306a36Sopenharmony_ci return ifp; 281862306a36Sopenharmony_ci} 281962306a36Sopenharmony_ci 282062306a36Sopenharmony_civoid free_ipath(struct inode_fs_paths *ipath) 282162306a36Sopenharmony_ci{ 282262306a36Sopenharmony_ci if (!ipath) 282362306a36Sopenharmony_ci return; 282462306a36Sopenharmony_ci kvfree(ipath->fspath); 282562306a36Sopenharmony_ci kfree(ipath); 282662306a36Sopenharmony_ci} 282762306a36Sopenharmony_ci 282862306a36Sopenharmony_cistruct btrfs_backref_iter *btrfs_backref_iter_alloc(struct btrfs_fs_info *fs_info) 282962306a36Sopenharmony_ci{ 283062306a36Sopenharmony_ci struct btrfs_backref_iter *ret; 283162306a36Sopenharmony_ci 283262306a36Sopenharmony_ci ret = kzalloc(sizeof(*ret), GFP_NOFS); 283362306a36Sopenharmony_ci if (!ret) 283462306a36Sopenharmony_ci return NULL; 283562306a36Sopenharmony_ci 283662306a36Sopenharmony_ci ret->path = btrfs_alloc_path(); 283762306a36Sopenharmony_ci if (!ret->path) { 283862306a36Sopenharmony_ci kfree(ret); 283962306a36Sopenharmony_ci return NULL; 284062306a36Sopenharmony_ci } 284162306a36Sopenharmony_ci 284262306a36Sopenharmony_ci /* Current backref iterator only supports iteration in commit root */ 284362306a36Sopenharmony_ci ret->path->search_commit_root = 1; 284462306a36Sopenharmony_ci ret->path->skip_locking = 1; 284562306a36Sopenharmony_ci ret->fs_info = fs_info; 284662306a36Sopenharmony_ci 284762306a36Sopenharmony_ci return ret; 284862306a36Sopenharmony_ci} 284962306a36Sopenharmony_ci 285062306a36Sopenharmony_ciint btrfs_backref_iter_start(struct btrfs_backref_iter *iter, u64 bytenr) 285162306a36Sopenharmony_ci{ 285262306a36Sopenharmony_ci struct btrfs_fs_info *fs_info = iter->fs_info; 285362306a36Sopenharmony_ci struct btrfs_root *extent_root = btrfs_extent_root(fs_info, bytenr); 285462306a36Sopenharmony_ci struct btrfs_path *path = iter->path; 285562306a36Sopenharmony_ci struct btrfs_extent_item *ei; 285662306a36Sopenharmony_ci struct btrfs_key key; 285762306a36Sopenharmony_ci int ret; 285862306a36Sopenharmony_ci 285962306a36Sopenharmony_ci key.objectid = bytenr; 286062306a36Sopenharmony_ci key.type = BTRFS_METADATA_ITEM_KEY; 286162306a36Sopenharmony_ci key.offset = (u64)-1; 286262306a36Sopenharmony_ci iter->bytenr = bytenr; 286362306a36Sopenharmony_ci 286462306a36Sopenharmony_ci ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0); 286562306a36Sopenharmony_ci if (ret < 0) 286662306a36Sopenharmony_ci return ret; 286762306a36Sopenharmony_ci if (ret == 0) { 286862306a36Sopenharmony_ci ret = -EUCLEAN; 286962306a36Sopenharmony_ci goto release; 287062306a36Sopenharmony_ci } 287162306a36Sopenharmony_ci if (path->slots[0] == 0) { 287262306a36Sopenharmony_ci WARN_ON(IS_ENABLED(CONFIG_BTRFS_DEBUG)); 287362306a36Sopenharmony_ci ret = -EUCLEAN; 287462306a36Sopenharmony_ci goto release; 287562306a36Sopenharmony_ci } 287662306a36Sopenharmony_ci path->slots[0]--; 287762306a36Sopenharmony_ci 287862306a36Sopenharmony_ci btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); 287962306a36Sopenharmony_ci if ((key.type != BTRFS_EXTENT_ITEM_KEY && 288062306a36Sopenharmony_ci key.type != BTRFS_METADATA_ITEM_KEY) || key.objectid != bytenr) { 288162306a36Sopenharmony_ci ret = -ENOENT; 288262306a36Sopenharmony_ci goto release; 288362306a36Sopenharmony_ci } 288462306a36Sopenharmony_ci memcpy(&iter->cur_key, &key, sizeof(key)); 288562306a36Sopenharmony_ci iter->item_ptr = (u32)btrfs_item_ptr_offset(path->nodes[0], 288662306a36Sopenharmony_ci path->slots[0]); 288762306a36Sopenharmony_ci iter->end_ptr = (u32)(iter->item_ptr + 288862306a36Sopenharmony_ci btrfs_item_size(path->nodes[0], path->slots[0])); 288962306a36Sopenharmony_ci ei = btrfs_item_ptr(path->nodes[0], path->slots[0], 289062306a36Sopenharmony_ci struct btrfs_extent_item); 289162306a36Sopenharmony_ci 289262306a36Sopenharmony_ci /* 289362306a36Sopenharmony_ci * Only support iteration on tree backref yet. 289462306a36Sopenharmony_ci * 289562306a36Sopenharmony_ci * This is an extra precaution for non skinny-metadata, where 289662306a36Sopenharmony_ci * EXTENT_ITEM is also used for tree blocks, that we can only use 289762306a36Sopenharmony_ci * extent flags to determine if it's a tree block. 289862306a36Sopenharmony_ci */ 289962306a36Sopenharmony_ci if (btrfs_extent_flags(path->nodes[0], ei) & BTRFS_EXTENT_FLAG_DATA) { 290062306a36Sopenharmony_ci ret = -ENOTSUPP; 290162306a36Sopenharmony_ci goto release; 290262306a36Sopenharmony_ci } 290362306a36Sopenharmony_ci iter->cur_ptr = (u32)(iter->item_ptr + sizeof(*ei)); 290462306a36Sopenharmony_ci 290562306a36Sopenharmony_ci /* If there is no inline backref, go search for keyed backref */ 290662306a36Sopenharmony_ci if (iter->cur_ptr >= iter->end_ptr) { 290762306a36Sopenharmony_ci ret = btrfs_next_item(extent_root, path); 290862306a36Sopenharmony_ci 290962306a36Sopenharmony_ci /* No inline nor keyed ref */ 291062306a36Sopenharmony_ci if (ret > 0) { 291162306a36Sopenharmony_ci ret = -ENOENT; 291262306a36Sopenharmony_ci goto release; 291362306a36Sopenharmony_ci } 291462306a36Sopenharmony_ci if (ret < 0) 291562306a36Sopenharmony_ci goto release; 291662306a36Sopenharmony_ci 291762306a36Sopenharmony_ci btrfs_item_key_to_cpu(path->nodes[0], &iter->cur_key, 291862306a36Sopenharmony_ci path->slots[0]); 291962306a36Sopenharmony_ci if (iter->cur_key.objectid != bytenr || 292062306a36Sopenharmony_ci (iter->cur_key.type != BTRFS_SHARED_BLOCK_REF_KEY && 292162306a36Sopenharmony_ci iter->cur_key.type != BTRFS_TREE_BLOCK_REF_KEY)) { 292262306a36Sopenharmony_ci ret = -ENOENT; 292362306a36Sopenharmony_ci goto release; 292462306a36Sopenharmony_ci } 292562306a36Sopenharmony_ci iter->cur_ptr = (u32)btrfs_item_ptr_offset(path->nodes[0], 292662306a36Sopenharmony_ci path->slots[0]); 292762306a36Sopenharmony_ci iter->item_ptr = iter->cur_ptr; 292862306a36Sopenharmony_ci iter->end_ptr = (u32)(iter->item_ptr + btrfs_item_size( 292962306a36Sopenharmony_ci path->nodes[0], path->slots[0])); 293062306a36Sopenharmony_ci } 293162306a36Sopenharmony_ci 293262306a36Sopenharmony_ci return 0; 293362306a36Sopenharmony_cirelease: 293462306a36Sopenharmony_ci btrfs_backref_iter_release(iter); 293562306a36Sopenharmony_ci return ret; 293662306a36Sopenharmony_ci} 293762306a36Sopenharmony_ci 293862306a36Sopenharmony_ci/* 293962306a36Sopenharmony_ci * Go to the next backref item of current bytenr, can be either inlined or 294062306a36Sopenharmony_ci * keyed. 294162306a36Sopenharmony_ci * 294262306a36Sopenharmony_ci * Caller needs to check whether it's inline ref or not by iter->cur_key. 294362306a36Sopenharmony_ci * 294462306a36Sopenharmony_ci * Return 0 if we get next backref without problem. 294562306a36Sopenharmony_ci * Return >0 if there is no extra backref for this bytenr. 294662306a36Sopenharmony_ci * Return <0 if there is something wrong happened. 294762306a36Sopenharmony_ci */ 294862306a36Sopenharmony_ciint btrfs_backref_iter_next(struct btrfs_backref_iter *iter) 294962306a36Sopenharmony_ci{ 295062306a36Sopenharmony_ci struct extent_buffer *eb = btrfs_backref_get_eb(iter); 295162306a36Sopenharmony_ci struct btrfs_root *extent_root; 295262306a36Sopenharmony_ci struct btrfs_path *path = iter->path; 295362306a36Sopenharmony_ci struct btrfs_extent_inline_ref *iref; 295462306a36Sopenharmony_ci int ret; 295562306a36Sopenharmony_ci u32 size; 295662306a36Sopenharmony_ci 295762306a36Sopenharmony_ci if (btrfs_backref_iter_is_inline_ref(iter)) { 295862306a36Sopenharmony_ci /* We're still inside the inline refs */ 295962306a36Sopenharmony_ci ASSERT(iter->cur_ptr < iter->end_ptr); 296062306a36Sopenharmony_ci 296162306a36Sopenharmony_ci if (btrfs_backref_has_tree_block_info(iter)) { 296262306a36Sopenharmony_ci /* First tree block info */ 296362306a36Sopenharmony_ci size = sizeof(struct btrfs_tree_block_info); 296462306a36Sopenharmony_ci } else { 296562306a36Sopenharmony_ci /* Use inline ref type to determine the size */ 296662306a36Sopenharmony_ci int type; 296762306a36Sopenharmony_ci 296862306a36Sopenharmony_ci iref = (struct btrfs_extent_inline_ref *) 296962306a36Sopenharmony_ci ((unsigned long)iter->cur_ptr); 297062306a36Sopenharmony_ci type = btrfs_extent_inline_ref_type(eb, iref); 297162306a36Sopenharmony_ci 297262306a36Sopenharmony_ci size = btrfs_extent_inline_ref_size(type); 297362306a36Sopenharmony_ci } 297462306a36Sopenharmony_ci iter->cur_ptr += size; 297562306a36Sopenharmony_ci if (iter->cur_ptr < iter->end_ptr) 297662306a36Sopenharmony_ci return 0; 297762306a36Sopenharmony_ci 297862306a36Sopenharmony_ci /* All inline items iterated, fall through */ 297962306a36Sopenharmony_ci } 298062306a36Sopenharmony_ci 298162306a36Sopenharmony_ci /* We're at keyed items, there is no inline item, go to the next one */ 298262306a36Sopenharmony_ci extent_root = btrfs_extent_root(iter->fs_info, iter->bytenr); 298362306a36Sopenharmony_ci ret = btrfs_next_item(extent_root, iter->path); 298462306a36Sopenharmony_ci if (ret) 298562306a36Sopenharmony_ci return ret; 298662306a36Sopenharmony_ci 298762306a36Sopenharmony_ci btrfs_item_key_to_cpu(path->nodes[0], &iter->cur_key, path->slots[0]); 298862306a36Sopenharmony_ci if (iter->cur_key.objectid != iter->bytenr || 298962306a36Sopenharmony_ci (iter->cur_key.type != BTRFS_TREE_BLOCK_REF_KEY && 299062306a36Sopenharmony_ci iter->cur_key.type != BTRFS_SHARED_BLOCK_REF_KEY)) 299162306a36Sopenharmony_ci return 1; 299262306a36Sopenharmony_ci iter->item_ptr = (u32)btrfs_item_ptr_offset(path->nodes[0], 299362306a36Sopenharmony_ci path->slots[0]); 299462306a36Sopenharmony_ci iter->cur_ptr = iter->item_ptr; 299562306a36Sopenharmony_ci iter->end_ptr = iter->item_ptr + (u32)btrfs_item_size(path->nodes[0], 299662306a36Sopenharmony_ci path->slots[0]); 299762306a36Sopenharmony_ci return 0; 299862306a36Sopenharmony_ci} 299962306a36Sopenharmony_ci 300062306a36Sopenharmony_civoid btrfs_backref_init_cache(struct btrfs_fs_info *fs_info, 300162306a36Sopenharmony_ci struct btrfs_backref_cache *cache, int is_reloc) 300262306a36Sopenharmony_ci{ 300362306a36Sopenharmony_ci int i; 300462306a36Sopenharmony_ci 300562306a36Sopenharmony_ci cache->rb_root = RB_ROOT; 300662306a36Sopenharmony_ci for (i = 0; i < BTRFS_MAX_LEVEL; i++) 300762306a36Sopenharmony_ci INIT_LIST_HEAD(&cache->pending[i]); 300862306a36Sopenharmony_ci INIT_LIST_HEAD(&cache->changed); 300962306a36Sopenharmony_ci INIT_LIST_HEAD(&cache->detached); 301062306a36Sopenharmony_ci INIT_LIST_HEAD(&cache->leaves); 301162306a36Sopenharmony_ci INIT_LIST_HEAD(&cache->pending_edge); 301262306a36Sopenharmony_ci INIT_LIST_HEAD(&cache->useless_node); 301362306a36Sopenharmony_ci cache->fs_info = fs_info; 301462306a36Sopenharmony_ci cache->is_reloc = is_reloc; 301562306a36Sopenharmony_ci} 301662306a36Sopenharmony_ci 301762306a36Sopenharmony_cistruct btrfs_backref_node *btrfs_backref_alloc_node( 301862306a36Sopenharmony_ci struct btrfs_backref_cache *cache, u64 bytenr, int level) 301962306a36Sopenharmony_ci{ 302062306a36Sopenharmony_ci struct btrfs_backref_node *node; 302162306a36Sopenharmony_ci 302262306a36Sopenharmony_ci ASSERT(level >= 0 && level < BTRFS_MAX_LEVEL); 302362306a36Sopenharmony_ci node = kzalloc(sizeof(*node), GFP_NOFS); 302462306a36Sopenharmony_ci if (!node) 302562306a36Sopenharmony_ci return node; 302662306a36Sopenharmony_ci 302762306a36Sopenharmony_ci INIT_LIST_HEAD(&node->list); 302862306a36Sopenharmony_ci INIT_LIST_HEAD(&node->upper); 302962306a36Sopenharmony_ci INIT_LIST_HEAD(&node->lower); 303062306a36Sopenharmony_ci RB_CLEAR_NODE(&node->rb_node); 303162306a36Sopenharmony_ci cache->nr_nodes++; 303262306a36Sopenharmony_ci node->level = level; 303362306a36Sopenharmony_ci node->bytenr = bytenr; 303462306a36Sopenharmony_ci 303562306a36Sopenharmony_ci return node; 303662306a36Sopenharmony_ci} 303762306a36Sopenharmony_ci 303862306a36Sopenharmony_cistruct btrfs_backref_edge *btrfs_backref_alloc_edge( 303962306a36Sopenharmony_ci struct btrfs_backref_cache *cache) 304062306a36Sopenharmony_ci{ 304162306a36Sopenharmony_ci struct btrfs_backref_edge *edge; 304262306a36Sopenharmony_ci 304362306a36Sopenharmony_ci edge = kzalloc(sizeof(*edge), GFP_NOFS); 304462306a36Sopenharmony_ci if (edge) 304562306a36Sopenharmony_ci cache->nr_edges++; 304662306a36Sopenharmony_ci return edge; 304762306a36Sopenharmony_ci} 304862306a36Sopenharmony_ci 304962306a36Sopenharmony_ci/* 305062306a36Sopenharmony_ci * Drop the backref node from cache, also cleaning up all its 305162306a36Sopenharmony_ci * upper edges and any uncached nodes in the path. 305262306a36Sopenharmony_ci * 305362306a36Sopenharmony_ci * This cleanup happens bottom up, thus the node should either 305462306a36Sopenharmony_ci * be the lowest node in the cache or a detached node. 305562306a36Sopenharmony_ci */ 305662306a36Sopenharmony_civoid btrfs_backref_cleanup_node(struct btrfs_backref_cache *cache, 305762306a36Sopenharmony_ci struct btrfs_backref_node *node) 305862306a36Sopenharmony_ci{ 305962306a36Sopenharmony_ci struct btrfs_backref_node *upper; 306062306a36Sopenharmony_ci struct btrfs_backref_edge *edge; 306162306a36Sopenharmony_ci 306262306a36Sopenharmony_ci if (!node) 306362306a36Sopenharmony_ci return; 306462306a36Sopenharmony_ci 306562306a36Sopenharmony_ci BUG_ON(!node->lowest && !node->detached); 306662306a36Sopenharmony_ci while (!list_empty(&node->upper)) { 306762306a36Sopenharmony_ci edge = list_entry(node->upper.next, struct btrfs_backref_edge, 306862306a36Sopenharmony_ci list[LOWER]); 306962306a36Sopenharmony_ci upper = edge->node[UPPER]; 307062306a36Sopenharmony_ci list_del(&edge->list[LOWER]); 307162306a36Sopenharmony_ci list_del(&edge->list[UPPER]); 307262306a36Sopenharmony_ci btrfs_backref_free_edge(cache, edge); 307362306a36Sopenharmony_ci 307462306a36Sopenharmony_ci /* 307562306a36Sopenharmony_ci * Add the node to leaf node list if no other child block 307662306a36Sopenharmony_ci * cached. 307762306a36Sopenharmony_ci */ 307862306a36Sopenharmony_ci if (list_empty(&upper->lower)) { 307962306a36Sopenharmony_ci list_add_tail(&upper->lower, &cache->leaves); 308062306a36Sopenharmony_ci upper->lowest = 1; 308162306a36Sopenharmony_ci } 308262306a36Sopenharmony_ci } 308362306a36Sopenharmony_ci 308462306a36Sopenharmony_ci btrfs_backref_drop_node(cache, node); 308562306a36Sopenharmony_ci} 308662306a36Sopenharmony_ci 308762306a36Sopenharmony_ci/* 308862306a36Sopenharmony_ci * Release all nodes/edges from current cache 308962306a36Sopenharmony_ci */ 309062306a36Sopenharmony_civoid btrfs_backref_release_cache(struct btrfs_backref_cache *cache) 309162306a36Sopenharmony_ci{ 309262306a36Sopenharmony_ci struct btrfs_backref_node *node; 309362306a36Sopenharmony_ci int i; 309462306a36Sopenharmony_ci 309562306a36Sopenharmony_ci while (!list_empty(&cache->detached)) { 309662306a36Sopenharmony_ci node = list_entry(cache->detached.next, 309762306a36Sopenharmony_ci struct btrfs_backref_node, list); 309862306a36Sopenharmony_ci btrfs_backref_cleanup_node(cache, node); 309962306a36Sopenharmony_ci } 310062306a36Sopenharmony_ci 310162306a36Sopenharmony_ci while (!list_empty(&cache->leaves)) { 310262306a36Sopenharmony_ci node = list_entry(cache->leaves.next, 310362306a36Sopenharmony_ci struct btrfs_backref_node, lower); 310462306a36Sopenharmony_ci btrfs_backref_cleanup_node(cache, node); 310562306a36Sopenharmony_ci } 310662306a36Sopenharmony_ci 310762306a36Sopenharmony_ci cache->last_trans = 0; 310862306a36Sopenharmony_ci 310962306a36Sopenharmony_ci for (i = 0; i < BTRFS_MAX_LEVEL; i++) 311062306a36Sopenharmony_ci ASSERT(list_empty(&cache->pending[i])); 311162306a36Sopenharmony_ci ASSERT(list_empty(&cache->pending_edge)); 311262306a36Sopenharmony_ci ASSERT(list_empty(&cache->useless_node)); 311362306a36Sopenharmony_ci ASSERT(list_empty(&cache->changed)); 311462306a36Sopenharmony_ci ASSERT(list_empty(&cache->detached)); 311562306a36Sopenharmony_ci ASSERT(RB_EMPTY_ROOT(&cache->rb_root)); 311662306a36Sopenharmony_ci ASSERT(!cache->nr_nodes); 311762306a36Sopenharmony_ci ASSERT(!cache->nr_edges); 311862306a36Sopenharmony_ci} 311962306a36Sopenharmony_ci 312062306a36Sopenharmony_ci/* 312162306a36Sopenharmony_ci * Handle direct tree backref 312262306a36Sopenharmony_ci * 312362306a36Sopenharmony_ci * Direct tree backref means, the backref item shows its parent bytenr 312462306a36Sopenharmony_ci * directly. This is for SHARED_BLOCK_REF backref (keyed or inlined). 312562306a36Sopenharmony_ci * 312662306a36Sopenharmony_ci * @ref_key: The converted backref key. 312762306a36Sopenharmony_ci * For keyed backref, it's the item key. 312862306a36Sopenharmony_ci * For inlined backref, objectid is the bytenr, 312962306a36Sopenharmony_ci * type is btrfs_inline_ref_type, offset is 313062306a36Sopenharmony_ci * btrfs_inline_ref_offset. 313162306a36Sopenharmony_ci */ 313262306a36Sopenharmony_cistatic int handle_direct_tree_backref(struct btrfs_backref_cache *cache, 313362306a36Sopenharmony_ci struct btrfs_key *ref_key, 313462306a36Sopenharmony_ci struct btrfs_backref_node *cur) 313562306a36Sopenharmony_ci{ 313662306a36Sopenharmony_ci struct btrfs_backref_edge *edge; 313762306a36Sopenharmony_ci struct btrfs_backref_node *upper; 313862306a36Sopenharmony_ci struct rb_node *rb_node; 313962306a36Sopenharmony_ci 314062306a36Sopenharmony_ci ASSERT(ref_key->type == BTRFS_SHARED_BLOCK_REF_KEY); 314162306a36Sopenharmony_ci 314262306a36Sopenharmony_ci /* Only reloc root uses backref pointing to itself */ 314362306a36Sopenharmony_ci if (ref_key->objectid == ref_key->offset) { 314462306a36Sopenharmony_ci struct btrfs_root *root; 314562306a36Sopenharmony_ci 314662306a36Sopenharmony_ci cur->is_reloc_root = 1; 314762306a36Sopenharmony_ci /* Only reloc backref cache cares about a specific root */ 314862306a36Sopenharmony_ci if (cache->is_reloc) { 314962306a36Sopenharmony_ci root = find_reloc_root(cache->fs_info, cur->bytenr); 315062306a36Sopenharmony_ci if (!root) 315162306a36Sopenharmony_ci return -ENOENT; 315262306a36Sopenharmony_ci cur->root = root; 315362306a36Sopenharmony_ci } else { 315462306a36Sopenharmony_ci /* 315562306a36Sopenharmony_ci * For generic purpose backref cache, reloc root node 315662306a36Sopenharmony_ci * is useless. 315762306a36Sopenharmony_ci */ 315862306a36Sopenharmony_ci list_add(&cur->list, &cache->useless_node); 315962306a36Sopenharmony_ci } 316062306a36Sopenharmony_ci return 0; 316162306a36Sopenharmony_ci } 316262306a36Sopenharmony_ci 316362306a36Sopenharmony_ci edge = btrfs_backref_alloc_edge(cache); 316462306a36Sopenharmony_ci if (!edge) 316562306a36Sopenharmony_ci return -ENOMEM; 316662306a36Sopenharmony_ci 316762306a36Sopenharmony_ci rb_node = rb_simple_search(&cache->rb_root, ref_key->offset); 316862306a36Sopenharmony_ci if (!rb_node) { 316962306a36Sopenharmony_ci /* Parent node not yet cached */ 317062306a36Sopenharmony_ci upper = btrfs_backref_alloc_node(cache, ref_key->offset, 317162306a36Sopenharmony_ci cur->level + 1); 317262306a36Sopenharmony_ci if (!upper) { 317362306a36Sopenharmony_ci btrfs_backref_free_edge(cache, edge); 317462306a36Sopenharmony_ci return -ENOMEM; 317562306a36Sopenharmony_ci } 317662306a36Sopenharmony_ci 317762306a36Sopenharmony_ci /* 317862306a36Sopenharmony_ci * Backrefs for the upper level block isn't cached, add the 317962306a36Sopenharmony_ci * block to pending list 318062306a36Sopenharmony_ci */ 318162306a36Sopenharmony_ci list_add_tail(&edge->list[UPPER], &cache->pending_edge); 318262306a36Sopenharmony_ci } else { 318362306a36Sopenharmony_ci /* Parent node already cached */ 318462306a36Sopenharmony_ci upper = rb_entry(rb_node, struct btrfs_backref_node, rb_node); 318562306a36Sopenharmony_ci ASSERT(upper->checked); 318662306a36Sopenharmony_ci INIT_LIST_HEAD(&edge->list[UPPER]); 318762306a36Sopenharmony_ci } 318862306a36Sopenharmony_ci btrfs_backref_link_edge(edge, cur, upper, LINK_LOWER); 318962306a36Sopenharmony_ci return 0; 319062306a36Sopenharmony_ci} 319162306a36Sopenharmony_ci 319262306a36Sopenharmony_ci/* 319362306a36Sopenharmony_ci * Handle indirect tree backref 319462306a36Sopenharmony_ci * 319562306a36Sopenharmony_ci * Indirect tree backref means, we only know which tree the node belongs to. 319662306a36Sopenharmony_ci * We still need to do a tree search to find out the parents. This is for 319762306a36Sopenharmony_ci * TREE_BLOCK_REF backref (keyed or inlined). 319862306a36Sopenharmony_ci * 319962306a36Sopenharmony_ci * @trans: Transaction handle. 320062306a36Sopenharmony_ci * @ref_key: The same as @ref_key in handle_direct_tree_backref() 320162306a36Sopenharmony_ci * @tree_key: The first key of this tree block. 320262306a36Sopenharmony_ci * @path: A clean (released) path, to avoid allocating path every time 320362306a36Sopenharmony_ci * the function get called. 320462306a36Sopenharmony_ci */ 320562306a36Sopenharmony_cistatic int handle_indirect_tree_backref(struct btrfs_trans_handle *trans, 320662306a36Sopenharmony_ci struct btrfs_backref_cache *cache, 320762306a36Sopenharmony_ci struct btrfs_path *path, 320862306a36Sopenharmony_ci struct btrfs_key *ref_key, 320962306a36Sopenharmony_ci struct btrfs_key *tree_key, 321062306a36Sopenharmony_ci struct btrfs_backref_node *cur) 321162306a36Sopenharmony_ci{ 321262306a36Sopenharmony_ci struct btrfs_fs_info *fs_info = cache->fs_info; 321362306a36Sopenharmony_ci struct btrfs_backref_node *upper; 321462306a36Sopenharmony_ci struct btrfs_backref_node *lower; 321562306a36Sopenharmony_ci struct btrfs_backref_edge *edge; 321662306a36Sopenharmony_ci struct extent_buffer *eb; 321762306a36Sopenharmony_ci struct btrfs_root *root; 321862306a36Sopenharmony_ci struct rb_node *rb_node; 321962306a36Sopenharmony_ci int level; 322062306a36Sopenharmony_ci bool need_check = true; 322162306a36Sopenharmony_ci int ret; 322262306a36Sopenharmony_ci 322362306a36Sopenharmony_ci root = btrfs_get_fs_root(fs_info, ref_key->offset, false); 322462306a36Sopenharmony_ci if (IS_ERR(root)) 322562306a36Sopenharmony_ci return PTR_ERR(root); 322662306a36Sopenharmony_ci if (!test_bit(BTRFS_ROOT_SHAREABLE, &root->state)) 322762306a36Sopenharmony_ci cur->cowonly = 1; 322862306a36Sopenharmony_ci 322962306a36Sopenharmony_ci if (btrfs_root_level(&root->root_item) == cur->level) { 323062306a36Sopenharmony_ci /* Tree root */ 323162306a36Sopenharmony_ci ASSERT(btrfs_root_bytenr(&root->root_item) == cur->bytenr); 323262306a36Sopenharmony_ci /* 323362306a36Sopenharmony_ci * For reloc backref cache, we may ignore reloc root. But for 323462306a36Sopenharmony_ci * general purpose backref cache, we can't rely on 323562306a36Sopenharmony_ci * btrfs_should_ignore_reloc_root() as it may conflict with 323662306a36Sopenharmony_ci * current running relocation and lead to missing root. 323762306a36Sopenharmony_ci * 323862306a36Sopenharmony_ci * For general purpose backref cache, reloc root detection is 323962306a36Sopenharmony_ci * completely relying on direct backref (key->offset is parent 324062306a36Sopenharmony_ci * bytenr), thus only do such check for reloc cache. 324162306a36Sopenharmony_ci */ 324262306a36Sopenharmony_ci if (btrfs_should_ignore_reloc_root(root) && cache->is_reloc) { 324362306a36Sopenharmony_ci btrfs_put_root(root); 324462306a36Sopenharmony_ci list_add(&cur->list, &cache->useless_node); 324562306a36Sopenharmony_ci } else { 324662306a36Sopenharmony_ci cur->root = root; 324762306a36Sopenharmony_ci } 324862306a36Sopenharmony_ci return 0; 324962306a36Sopenharmony_ci } 325062306a36Sopenharmony_ci 325162306a36Sopenharmony_ci level = cur->level + 1; 325262306a36Sopenharmony_ci 325362306a36Sopenharmony_ci /* Search the tree to find parent blocks referring to the block */ 325462306a36Sopenharmony_ci path->search_commit_root = 1; 325562306a36Sopenharmony_ci path->skip_locking = 1; 325662306a36Sopenharmony_ci path->lowest_level = level; 325762306a36Sopenharmony_ci ret = btrfs_search_slot(NULL, root, tree_key, path, 0, 0); 325862306a36Sopenharmony_ci path->lowest_level = 0; 325962306a36Sopenharmony_ci if (ret < 0) { 326062306a36Sopenharmony_ci btrfs_put_root(root); 326162306a36Sopenharmony_ci return ret; 326262306a36Sopenharmony_ci } 326362306a36Sopenharmony_ci if (ret > 0 && path->slots[level] > 0) 326462306a36Sopenharmony_ci path->slots[level]--; 326562306a36Sopenharmony_ci 326662306a36Sopenharmony_ci eb = path->nodes[level]; 326762306a36Sopenharmony_ci if (btrfs_node_blockptr(eb, path->slots[level]) != cur->bytenr) { 326862306a36Sopenharmony_ci btrfs_err(fs_info, 326962306a36Sopenharmony_ci"couldn't find block (%llu) (level %d) in tree (%llu) with key (%llu %u %llu)", 327062306a36Sopenharmony_ci cur->bytenr, level - 1, root->root_key.objectid, 327162306a36Sopenharmony_ci tree_key->objectid, tree_key->type, tree_key->offset); 327262306a36Sopenharmony_ci btrfs_put_root(root); 327362306a36Sopenharmony_ci ret = -ENOENT; 327462306a36Sopenharmony_ci goto out; 327562306a36Sopenharmony_ci } 327662306a36Sopenharmony_ci lower = cur; 327762306a36Sopenharmony_ci 327862306a36Sopenharmony_ci /* Add all nodes and edges in the path */ 327962306a36Sopenharmony_ci for (; level < BTRFS_MAX_LEVEL; level++) { 328062306a36Sopenharmony_ci if (!path->nodes[level]) { 328162306a36Sopenharmony_ci ASSERT(btrfs_root_bytenr(&root->root_item) == 328262306a36Sopenharmony_ci lower->bytenr); 328362306a36Sopenharmony_ci /* Same as previous should_ignore_reloc_root() call */ 328462306a36Sopenharmony_ci if (btrfs_should_ignore_reloc_root(root) && 328562306a36Sopenharmony_ci cache->is_reloc) { 328662306a36Sopenharmony_ci btrfs_put_root(root); 328762306a36Sopenharmony_ci list_add(&lower->list, &cache->useless_node); 328862306a36Sopenharmony_ci } else { 328962306a36Sopenharmony_ci lower->root = root; 329062306a36Sopenharmony_ci } 329162306a36Sopenharmony_ci break; 329262306a36Sopenharmony_ci } 329362306a36Sopenharmony_ci 329462306a36Sopenharmony_ci edge = btrfs_backref_alloc_edge(cache); 329562306a36Sopenharmony_ci if (!edge) { 329662306a36Sopenharmony_ci btrfs_put_root(root); 329762306a36Sopenharmony_ci ret = -ENOMEM; 329862306a36Sopenharmony_ci goto out; 329962306a36Sopenharmony_ci } 330062306a36Sopenharmony_ci 330162306a36Sopenharmony_ci eb = path->nodes[level]; 330262306a36Sopenharmony_ci rb_node = rb_simple_search(&cache->rb_root, eb->start); 330362306a36Sopenharmony_ci if (!rb_node) { 330462306a36Sopenharmony_ci upper = btrfs_backref_alloc_node(cache, eb->start, 330562306a36Sopenharmony_ci lower->level + 1); 330662306a36Sopenharmony_ci if (!upper) { 330762306a36Sopenharmony_ci btrfs_put_root(root); 330862306a36Sopenharmony_ci btrfs_backref_free_edge(cache, edge); 330962306a36Sopenharmony_ci ret = -ENOMEM; 331062306a36Sopenharmony_ci goto out; 331162306a36Sopenharmony_ci } 331262306a36Sopenharmony_ci upper->owner = btrfs_header_owner(eb); 331362306a36Sopenharmony_ci if (!test_bit(BTRFS_ROOT_SHAREABLE, &root->state)) 331462306a36Sopenharmony_ci upper->cowonly = 1; 331562306a36Sopenharmony_ci 331662306a36Sopenharmony_ci /* 331762306a36Sopenharmony_ci * If we know the block isn't shared we can avoid 331862306a36Sopenharmony_ci * checking its backrefs. 331962306a36Sopenharmony_ci */ 332062306a36Sopenharmony_ci if (btrfs_block_can_be_shared(trans, root, eb)) 332162306a36Sopenharmony_ci upper->checked = 0; 332262306a36Sopenharmony_ci else 332362306a36Sopenharmony_ci upper->checked = 1; 332462306a36Sopenharmony_ci 332562306a36Sopenharmony_ci /* 332662306a36Sopenharmony_ci * Add the block to pending list if we need to check its 332762306a36Sopenharmony_ci * backrefs, we only do this once while walking up a 332862306a36Sopenharmony_ci * tree as we will catch anything else later on. 332962306a36Sopenharmony_ci */ 333062306a36Sopenharmony_ci if (!upper->checked && need_check) { 333162306a36Sopenharmony_ci need_check = false; 333262306a36Sopenharmony_ci list_add_tail(&edge->list[UPPER], 333362306a36Sopenharmony_ci &cache->pending_edge); 333462306a36Sopenharmony_ci } else { 333562306a36Sopenharmony_ci if (upper->checked) 333662306a36Sopenharmony_ci need_check = true; 333762306a36Sopenharmony_ci INIT_LIST_HEAD(&edge->list[UPPER]); 333862306a36Sopenharmony_ci } 333962306a36Sopenharmony_ci } else { 334062306a36Sopenharmony_ci upper = rb_entry(rb_node, struct btrfs_backref_node, 334162306a36Sopenharmony_ci rb_node); 334262306a36Sopenharmony_ci ASSERT(upper->checked); 334362306a36Sopenharmony_ci INIT_LIST_HEAD(&edge->list[UPPER]); 334462306a36Sopenharmony_ci if (!upper->owner) 334562306a36Sopenharmony_ci upper->owner = btrfs_header_owner(eb); 334662306a36Sopenharmony_ci } 334762306a36Sopenharmony_ci btrfs_backref_link_edge(edge, lower, upper, LINK_LOWER); 334862306a36Sopenharmony_ci 334962306a36Sopenharmony_ci if (rb_node) { 335062306a36Sopenharmony_ci btrfs_put_root(root); 335162306a36Sopenharmony_ci break; 335262306a36Sopenharmony_ci } 335362306a36Sopenharmony_ci lower = upper; 335462306a36Sopenharmony_ci upper = NULL; 335562306a36Sopenharmony_ci } 335662306a36Sopenharmony_ciout: 335762306a36Sopenharmony_ci btrfs_release_path(path); 335862306a36Sopenharmony_ci return ret; 335962306a36Sopenharmony_ci} 336062306a36Sopenharmony_ci 336162306a36Sopenharmony_ci/* 336262306a36Sopenharmony_ci * Add backref node @cur into @cache. 336362306a36Sopenharmony_ci * 336462306a36Sopenharmony_ci * NOTE: Even if the function returned 0, @cur is not yet cached as its upper 336562306a36Sopenharmony_ci * links aren't yet bi-directional. Needs to finish such links. 336662306a36Sopenharmony_ci * Use btrfs_backref_finish_upper_links() to finish such linkage. 336762306a36Sopenharmony_ci * 336862306a36Sopenharmony_ci * @trans: Transaction handle. 336962306a36Sopenharmony_ci * @path: Released path for indirect tree backref lookup 337062306a36Sopenharmony_ci * @iter: Released backref iter for extent tree search 337162306a36Sopenharmony_ci * @node_key: The first key of the tree block 337262306a36Sopenharmony_ci */ 337362306a36Sopenharmony_ciint btrfs_backref_add_tree_node(struct btrfs_trans_handle *trans, 337462306a36Sopenharmony_ci struct btrfs_backref_cache *cache, 337562306a36Sopenharmony_ci struct btrfs_path *path, 337662306a36Sopenharmony_ci struct btrfs_backref_iter *iter, 337762306a36Sopenharmony_ci struct btrfs_key *node_key, 337862306a36Sopenharmony_ci struct btrfs_backref_node *cur) 337962306a36Sopenharmony_ci{ 338062306a36Sopenharmony_ci struct btrfs_backref_edge *edge; 338162306a36Sopenharmony_ci struct btrfs_backref_node *exist; 338262306a36Sopenharmony_ci int ret; 338362306a36Sopenharmony_ci 338462306a36Sopenharmony_ci ret = btrfs_backref_iter_start(iter, cur->bytenr); 338562306a36Sopenharmony_ci if (ret < 0) 338662306a36Sopenharmony_ci return ret; 338762306a36Sopenharmony_ci /* 338862306a36Sopenharmony_ci * We skip the first btrfs_tree_block_info, as we don't use the key 338962306a36Sopenharmony_ci * stored in it, but fetch it from the tree block 339062306a36Sopenharmony_ci */ 339162306a36Sopenharmony_ci if (btrfs_backref_has_tree_block_info(iter)) { 339262306a36Sopenharmony_ci ret = btrfs_backref_iter_next(iter); 339362306a36Sopenharmony_ci if (ret < 0) 339462306a36Sopenharmony_ci goto out; 339562306a36Sopenharmony_ci /* No extra backref? This means the tree block is corrupted */ 339662306a36Sopenharmony_ci if (ret > 0) { 339762306a36Sopenharmony_ci ret = -EUCLEAN; 339862306a36Sopenharmony_ci goto out; 339962306a36Sopenharmony_ci } 340062306a36Sopenharmony_ci } 340162306a36Sopenharmony_ci WARN_ON(cur->checked); 340262306a36Sopenharmony_ci if (!list_empty(&cur->upper)) { 340362306a36Sopenharmony_ci /* 340462306a36Sopenharmony_ci * The backref was added previously when processing backref of 340562306a36Sopenharmony_ci * type BTRFS_TREE_BLOCK_REF_KEY 340662306a36Sopenharmony_ci */ 340762306a36Sopenharmony_ci ASSERT(list_is_singular(&cur->upper)); 340862306a36Sopenharmony_ci edge = list_entry(cur->upper.next, struct btrfs_backref_edge, 340962306a36Sopenharmony_ci list[LOWER]); 341062306a36Sopenharmony_ci ASSERT(list_empty(&edge->list[UPPER])); 341162306a36Sopenharmony_ci exist = edge->node[UPPER]; 341262306a36Sopenharmony_ci /* 341362306a36Sopenharmony_ci * Add the upper level block to pending list if we need check 341462306a36Sopenharmony_ci * its backrefs 341562306a36Sopenharmony_ci */ 341662306a36Sopenharmony_ci if (!exist->checked) 341762306a36Sopenharmony_ci list_add_tail(&edge->list[UPPER], &cache->pending_edge); 341862306a36Sopenharmony_ci } else { 341962306a36Sopenharmony_ci exist = NULL; 342062306a36Sopenharmony_ci } 342162306a36Sopenharmony_ci 342262306a36Sopenharmony_ci for (; ret == 0; ret = btrfs_backref_iter_next(iter)) { 342362306a36Sopenharmony_ci struct extent_buffer *eb; 342462306a36Sopenharmony_ci struct btrfs_key key; 342562306a36Sopenharmony_ci int type; 342662306a36Sopenharmony_ci 342762306a36Sopenharmony_ci cond_resched(); 342862306a36Sopenharmony_ci eb = btrfs_backref_get_eb(iter); 342962306a36Sopenharmony_ci 343062306a36Sopenharmony_ci key.objectid = iter->bytenr; 343162306a36Sopenharmony_ci if (btrfs_backref_iter_is_inline_ref(iter)) { 343262306a36Sopenharmony_ci struct btrfs_extent_inline_ref *iref; 343362306a36Sopenharmony_ci 343462306a36Sopenharmony_ci /* Update key for inline backref */ 343562306a36Sopenharmony_ci iref = (struct btrfs_extent_inline_ref *) 343662306a36Sopenharmony_ci ((unsigned long)iter->cur_ptr); 343762306a36Sopenharmony_ci type = btrfs_get_extent_inline_ref_type(eb, iref, 343862306a36Sopenharmony_ci BTRFS_REF_TYPE_BLOCK); 343962306a36Sopenharmony_ci if (type == BTRFS_REF_TYPE_INVALID) { 344062306a36Sopenharmony_ci ret = -EUCLEAN; 344162306a36Sopenharmony_ci goto out; 344262306a36Sopenharmony_ci } 344362306a36Sopenharmony_ci key.type = type; 344462306a36Sopenharmony_ci key.offset = btrfs_extent_inline_ref_offset(eb, iref); 344562306a36Sopenharmony_ci } else { 344662306a36Sopenharmony_ci key.type = iter->cur_key.type; 344762306a36Sopenharmony_ci key.offset = iter->cur_key.offset; 344862306a36Sopenharmony_ci } 344962306a36Sopenharmony_ci 345062306a36Sopenharmony_ci /* 345162306a36Sopenharmony_ci * Parent node found and matches current inline ref, no need to 345262306a36Sopenharmony_ci * rebuild this node for this inline ref 345362306a36Sopenharmony_ci */ 345462306a36Sopenharmony_ci if (exist && 345562306a36Sopenharmony_ci ((key.type == BTRFS_TREE_BLOCK_REF_KEY && 345662306a36Sopenharmony_ci exist->owner == key.offset) || 345762306a36Sopenharmony_ci (key.type == BTRFS_SHARED_BLOCK_REF_KEY && 345862306a36Sopenharmony_ci exist->bytenr == key.offset))) { 345962306a36Sopenharmony_ci exist = NULL; 346062306a36Sopenharmony_ci continue; 346162306a36Sopenharmony_ci } 346262306a36Sopenharmony_ci 346362306a36Sopenharmony_ci /* SHARED_BLOCK_REF means key.offset is the parent bytenr */ 346462306a36Sopenharmony_ci if (key.type == BTRFS_SHARED_BLOCK_REF_KEY) { 346562306a36Sopenharmony_ci ret = handle_direct_tree_backref(cache, &key, cur); 346662306a36Sopenharmony_ci if (ret < 0) 346762306a36Sopenharmony_ci goto out; 346862306a36Sopenharmony_ci } else if (key.type == BTRFS_TREE_BLOCK_REF_KEY) { 346962306a36Sopenharmony_ci /* 347062306a36Sopenharmony_ci * key.type == BTRFS_TREE_BLOCK_REF_KEY, inline ref 347162306a36Sopenharmony_ci * offset means the root objectid. We need to search 347262306a36Sopenharmony_ci * the tree to get its parent bytenr. 347362306a36Sopenharmony_ci */ 347462306a36Sopenharmony_ci ret = handle_indirect_tree_backref(trans, cache, path, 347562306a36Sopenharmony_ci &key, node_key, cur); 347662306a36Sopenharmony_ci if (ret < 0) 347762306a36Sopenharmony_ci goto out; 347862306a36Sopenharmony_ci } 347962306a36Sopenharmony_ci /* 348062306a36Sopenharmony_ci * Unrecognized tree backref items (if it can pass tree-checker) 348162306a36Sopenharmony_ci * would be ignored. 348262306a36Sopenharmony_ci */ 348362306a36Sopenharmony_ci } 348462306a36Sopenharmony_ci ret = 0; 348562306a36Sopenharmony_ci cur->checked = 1; 348662306a36Sopenharmony_ci WARN_ON(exist); 348762306a36Sopenharmony_ciout: 348862306a36Sopenharmony_ci btrfs_backref_iter_release(iter); 348962306a36Sopenharmony_ci return ret; 349062306a36Sopenharmony_ci} 349162306a36Sopenharmony_ci 349262306a36Sopenharmony_ci/* 349362306a36Sopenharmony_ci * Finish the upwards linkage created by btrfs_backref_add_tree_node() 349462306a36Sopenharmony_ci */ 349562306a36Sopenharmony_ciint btrfs_backref_finish_upper_links(struct btrfs_backref_cache *cache, 349662306a36Sopenharmony_ci struct btrfs_backref_node *start) 349762306a36Sopenharmony_ci{ 349862306a36Sopenharmony_ci struct list_head *useless_node = &cache->useless_node; 349962306a36Sopenharmony_ci struct btrfs_backref_edge *edge; 350062306a36Sopenharmony_ci struct rb_node *rb_node; 350162306a36Sopenharmony_ci LIST_HEAD(pending_edge); 350262306a36Sopenharmony_ci 350362306a36Sopenharmony_ci ASSERT(start->checked); 350462306a36Sopenharmony_ci 350562306a36Sopenharmony_ci /* Insert this node to cache if it's not COW-only */ 350662306a36Sopenharmony_ci if (!start->cowonly) { 350762306a36Sopenharmony_ci rb_node = rb_simple_insert(&cache->rb_root, start->bytenr, 350862306a36Sopenharmony_ci &start->rb_node); 350962306a36Sopenharmony_ci if (rb_node) 351062306a36Sopenharmony_ci btrfs_backref_panic(cache->fs_info, start->bytenr, 351162306a36Sopenharmony_ci -EEXIST); 351262306a36Sopenharmony_ci list_add_tail(&start->lower, &cache->leaves); 351362306a36Sopenharmony_ci } 351462306a36Sopenharmony_ci 351562306a36Sopenharmony_ci /* 351662306a36Sopenharmony_ci * Use breadth first search to iterate all related edges. 351762306a36Sopenharmony_ci * 351862306a36Sopenharmony_ci * The starting points are all the edges of this node 351962306a36Sopenharmony_ci */ 352062306a36Sopenharmony_ci list_for_each_entry(edge, &start->upper, list[LOWER]) 352162306a36Sopenharmony_ci list_add_tail(&edge->list[UPPER], &pending_edge); 352262306a36Sopenharmony_ci 352362306a36Sopenharmony_ci while (!list_empty(&pending_edge)) { 352462306a36Sopenharmony_ci struct btrfs_backref_node *upper; 352562306a36Sopenharmony_ci struct btrfs_backref_node *lower; 352662306a36Sopenharmony_ci 352762306a36Sopenharmony_ci edge = list_first_entry(&pending_edge, 352862306a36Sopenharmony_ci struct btrfs_backref_edge, list[UPPER]); 352962306a36Sopenharmony_ci list_del_init(&edge->list[UPPER]); 353062306a36Sopenharmony_ci upper = edge->node[UPPER]; 353162306a36Sopenharmony_ci lower = edge->node[LOWER]; 353262306a36Sopenharmony_ci 353362306a36Sopenharmony_ci /* Parent is detached, no need to keep any edges */ 353462306a36Sopenharmony_ci if (upper->detached) { 353562306a36Sopenharmony_ci list_del(&edge->list[LOWER]); 353662306a36Sopenharmony_ci btrfs_backref_free_edge(cache, edge); 353762306a36Sopenharmony_ci 353862306a36Sopenharmony_ci /* Lower node is orphan, queue for cleanup */ 353962306a36Sopenharmony_ci if (list_empty(&lower->upper)) 354062306a36Sopenharmony_ci list_add(&lower->list, useless_node); 354162306a36Sopenharmony_ci continue; 354262306a36Sopenharmony_ci } 354362306a36Sopenharmony_ci 354462306a36Sopenharmony_ci /* 354562306a36Sopenharmony_ci * All new nodes added in current build_backref_tree() haven't 354662306a36Sopenharmony_ci * been linked to the cache rb tree. 354762306a36Sopenharmony_ci * So if we have upper->rb_node populated, this means a cache 354862306a36Sopenharmony_ci * hit. We only need to link the edge, as @upper and all its 354962306a36Sopenharmony_ci * parents have already been linked. 355062306a36Sopenharmony_ci */ 355162306a36Sopenharmony_ci if (!RB_EMPTY_NODE(&upper->rb_node)) { 355262306a36Sopenharmony_ci if (upper->lowest) { 355362306a36Sopenharmony_ci list_del_init(&upper->lower); 355462306a36Sopenharmony_ci upper->lowest = 0; 355562306a36Sopenharmony_ci } 355662306a36Sopenharmony_ci 355762306a36Sopenharmony_ci list_add_tail(&edge->list[UPPER], &upper->lower); 355862306a36Sopenharmony_ci continue; 355962306a36Sopenharmony_ci } 356062306a36Sopenharmony_ci 356162306a36Sopenharmony_ci /* Sanity check, we shouldn't have any unchecked nodes */ 356262306a36Sopenharmony_ci if (!upper->checked) { 356362306a36Sopenharmony_ci ASSERT(0); 356462306a36Sopenharmony_ci return -EUCLEAN; 356562306a36Sopenharmony_ci } 356662306a36Sopenharmony_ci 356762306a36Sopenharmony_ci /* Sanity check, COW-only node has non-COW-only parent */ 356862306a36Sopenharmony_ci if (start->cowonly != upper->cowonly) { 356962306a36Sopenharmony_ci ASSERT(0); 357062306a36Sopenharmony_ci return -EUCLEAN; 357162306a36Sopenharmony_ci } 357262306a36Sopenharmony_ci 357362306a36Sopenharmony_ci /* Only cache non-COW-only (subvolume trees) tree blocks */ 357462306a36Sopenharmony_ci if (!upper->cowonly) { 357562306a36Sopenharmony_ci rb_node = rb_simple_insert(&cache->rb_root, upper->bytenr, 357662306a36Sopenharmony_ci &upper->rb_node); 357762306a36Sopenharmony_ci if (rb_node) { 357862306a36Sopenharmony_ci btrfs_backref_panic(cache->fs_info, 357962306a36Sopenharmony_ci upper->bytenr, -EEXIST); 358062306a36Sopenharmony_ci return -EUCLEAN; 358162306a36Sopenharmony_ci } 358262306a36Sopenharmony_ci } 358362306a36Sopenharmony_ci 358462306a36Sopenharmony_ci list_add_tail(&edge->list[UPPER], &upper->lower); 358562306a36Sopenharmony_ci 358662306a36Sopenharmony_ci /* 358762306a36Sopenharmony_ci * Also queue all the parent edges of this uncached node 358862306a36Sopenharmony_ci * to finish the upper linkage 358962306a36Sopenharmony_ci */ 359062306a36Sopenharmony_ci list_for_each_entry(edge, &upper->upper, list[LOWER]) 359162306a36Sopenharmony_ci list_add_tail(&edge->list[UPPER], &pending_edge); 359262306a36Sopenharmony_ci } 359362306a36Sopenharmony_ci return 0; 359462306a36Sopenharmony_ci} 359562306a36Sopenharmony_ci 359662306a36Sopenharmony_civoid btrfs_backref_error_cleanup(struct btrfs_backref_cache *cache, 359762306a36Sopenharmony_ci struct btrfs_backref_node *node) 359862306a36Sopenharmony_ci{ 359962306a36Sopenharmony_ci struct btrfs_backref_node *lower; 360062306a36Sopenharmony_ci struct btrfs_backref_node *upper; 360162306a36Sopenharmony_ci struct btrfs_backref_edge *edge; 360262306a36Sopenharmony_ci 360362306a36Sopenharmony_ci while (!list_empty(&cache->useless_node)) { 360462306a36Sopenharmony_ci lower = list_first_entry(&cache->useless_node, 360562306a36Sopenharmony_ci struct btrfs_backref_node, list); 360662306a36Sopenharmony_ci list_del_init(&lower->list); 360762306a36Sopenharmony_ci } 360862306a36Sopenharmony_ci while (!list_empty(&cache->pending_edge)) { 360962306a36Sopenharmony_ci edge = list_first_entry(&cache->pending_edge, 361062306a36Sopenharmony_ci struct btrfs_backref_edge, list[UPPER]); 361162306a36Sopenharmony_ci list_del(&edge->list[UPPER]); 361262306a36Sopenharmony_ci list_del(&edge->list[LOWER]); 361362306a36Sopenharmony_ci lower = edge->node[LOWER]; 361462306a36Sopenharmony_ci upper = edge->node[UPPER]; 361562306a36Sopenharmony_ci btrfs_backref_free_edge(cache, edge); 361662306a36Sopenharmony_ci 361762306a36Sopenharmony_ci /* 361862306a36Sopenharmony_ci * Lower is no longer linked to any upper backref nodes and 361962306a36Sopenharmony_ci * isn't in the cache, we can free it ourselves. 362062306a36Sopenharmony_ci */ 362162306a36Sopenharmony_ci if (list_empty(&lower->upper) && 362262306a36Sopenharmony_ci RB_EMPTY_NODE(&lower->rb_node)) 362362306a36Sopenharmony_ci list_add(&lower->list, &cache->useless_node); 362462306a36Sopenharmony_ci 362562306a36Sopenharmony_ci if (!RB_EMPTY_NODE(&upper->rb_node)) 362662306a36Sopenharmony_ci continue; 362762306a36Sopenharmony_ci 362862306a36Sopenharmony_ci /* Add this guy's upper edges to the list to process */ 362962306a36Sopenharmony_ci list_for_each_entry(edge, &upper->upper, list[LOWER]) 363062306a36Sopenharmony_ci list_add_tail(&edge->list[UPPER], 363162306a36Sopenharmony_ci &cache->pending_edge); 363262306a36Sopenharmony_ci if (list_empty(&upper->upper)) 363362306a36Sopenharmony_ci list_add(&upper->list, &cache->useless_node); 363462306a36Sopenharmony_ci } 363562306a36Sopenharmony_ci 363662306a36Sopenharmony_ci while (!list_empty(&cache->useless_node)) { 363762306a36Sopenharmony_ci lower = list_first_entry(&cache->useless_node, 363862306a36Sopenharmony_ci struct btrfs_backref_node, list); 363962306a36Sopenharmony_ci list_del_init(&lower->list); 364062306a36Sopenharmony_ci if (lower == node) 364162306a36Sopenharmony_ci node = NULL; 364262306a36Sopenharmony_ci btrfs_backref_drop_node(cache, lower); 364362306a36Sopenharmony_ci } 364462306a36Sopenharmony_ci 364562306a36Sopenharmony_ci btrfs_backref_cleanup_node(cache, node); 364662306a36Sopenharmony_ci ASSERT(list_empty(&cache->useless_node) && 364762306a36Sopenharmony_ci list_empty(&cache->pending_edge)); 364862306a36Sopenharmony_ci} 3649