1a542ad1bSJan Schmidt /* 2a542ad1bSJan Schmidt * Copyright (C) 2011 STRATO. All rights reserved. 3a542ad1bSJan Schmidt * 4a542ad1bSJan Schmidt * This program is free software; you can redistribute it and/or 5a542ad1bSJan Schmidt * modify it under the terms of the GNU General Public 6a542ad1bSJan Schmidt * License v2 as published by the Free Software Foundation. 7a542ad1bSJan Schmidt * 8a542ad1bSJan Schmidt * This program is distributed in the hope that it will be useful, 9a542ad1bSJan Schmidt * but WITHOUT ANY WARRANTY; without even the implied warranty of 10a542ad1bSJan Schmidt * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 11a542ad1bSJan Schmidt * General Public License for more details. 12a542ad1bSJan Schmidt * 13a542ad1bSJan Schmidt * You should have received a copy of the GNU General Public 14a542ad1bSJan Schmidt * License along with this program; if not, write to the 15a542ad1bSJan Schmidt * Free Software Foundation, Inc., 59 Temple Place - Suite 330, 16a542ad1bSJan Schmidt * Boston, MA 021110-1307, USA. 17a542ad1bSJan Schmidt */ 18a542ad1bSJan Schmidt 19f54de068SDavid Sterba #include <linux/mm.h> 20afce772eSLu Fengqi #include <linux/rbtree.h> 21a542ad1bSJan Schmidt #include "ctree.h" 22a542ad1bSJan Schmidt #include "disk-io.h" 23a542ad1bSJan Schmidt #include "backref.h" 248da6d581SJan Schmidt #include "ulist.h" 258da6d581SJan Schmidt #include "transaction.h" 268da6d581SJan Schmidt #include "delayed-ref.h" 27b916a59aSJan Schmidt #include "locking.h" 28a542ad1bSJan Schmidt 29f58d88b3SEdmund Nadolski enum merge_mode { 30f58d88b3SEdmund Nadolski MERGE_IDENTICAL_KEYS = 1, 31f58d88b3SEdmund Nadolski MERGE_IDENTICAL_PARENTS, 32f58d88b3SEdmund Nadolski }; 33f58d88b3SEdmund Nadolski 34dc046b10SJosef Bacik /* Just an arbitrary number so we can be sure this happened */ 35dc046b10SJosef Bacik #define BACKREF_FOUND_SHARED 6 36dc046b10SJosef Bacik 37976b1908SJan Schmidt struct extent_inode_elem { 38976b1908SJan Schmidt u64 inum; 39976b1908SJan Schmidt u64 offset; 40976b1908SJan Schmidt struct extent_inode_elem *next; 41976b1908SJan Schmidt }; 42976b1908SJan Schmidt 43afce772eSLu Fengqi /* 44afce772eSLu Fengqi * ref_root is used as the root of the ref tree that hold a collection 45afce772eSLu Fengqi * of unique references. 46afce772eSLu Fengqi */ 47afce772eSLu Fengqi struct ref_root { 48afce772eSLu Fengqi struct rb_root rb_root; 49afce772eSLu Fengqi 50afce772eSLu Fengqi /* 51afce772eSLu Fengqi * The unique_refs represents the number of ref_nodes with a positive 52afce772eSLu Fengqi * count stored in the tree. Even if a ref_node (the count is greater 53afce772eSLu Fengqi * than one) is added, the unique_refs will only increase by one. 54afce772eSLu Fengqi */ 55afce772eSLu Fengqi unsigned int unique_refs; 56afce772eSLu Fengqi }; 57afce772eSLu Fengqi 58afce772eSLu Fengqi /* ref_node is used to store a unique reference to the ref tree. */ 59afce772eSLu Fengqi struct ref_node { 60afce772eSLu Fengqi struct rb_node rb_node; 61afce772eSLu Fengqi 62afce772eSLu Fengqi /* For NORMAL_REF, otherwise all these fields should be set to 0 */ 63afce772eSLu Fengqi u64 root_id; 64afce772eSLu Fengqi u64 object_id; 65afce772eSLu Fengqi u64 offset; 66afce772eSLu Fengqi 67afce772eSLu Fengqi /* For SHARED_REF, otherwise parent field should be set to 0 */ 68afce772eSLu Fengqi u64 parent; 69afce772eSLu Fengqi 70afce772eSLu Fengqi /* Ref to the ref_mod of btrfs_delayed_ref_node */ 71afce772eSLu Fengqi int ref_mod; 72afce772eSLu Fengqi }; 73afce772eSLu Fengqi 74afce772eSLu Fengqi /* Dynamically allocate and initialize a ref_root */ 75afce772eSLu Fengqi static struct ref_root *ref_root_alloc(void) 76afce772eSLu Fengqi { 77afce772eSLu Fengqi struct ref_root *ref_tree; 78afce772eSLu Fengqi 79afce772eSLu Fengqi ref_tree = kmalloc(sizeof(*ref_tree), GFP_NOFS); 80afce772eSLu Fengqi if (!ref_tree) 81afce772eSLu Fengqi return NULL; 82afce772eSLu Fengqi 83afce772eSLu Fengqi ref_tree->rb_root = RB_ROOT; 84afce772eSLu Fengqi ref_tree->unique_refs = 0; 85afce772eSLu Fengqi 86afce772eSLu Fengqi return ref_tree; 87afce772eSLu Fengqi } 88afce772eSLu Fengqi 89afce772eSLu Fengqi /* Free all nodes in the ref tree, and reinit ref_root */ 90afce772eSLu Fengqi static void ref_root_fini(struct ref_root *ref_tree) 91afce772eSLu Fengqi { 92afce772eSLu Fengqi struct ref_node *node; 93afce772eSLu Fengqi struct rb_node *next; 94afce772eSLu Fengqi 95afce772eSLu Fengqi while ((next = rb_first(&ref_tree->rb_root)) != NULL) { 96afce772eSLu Fengqi node = rb_entry(next, struct ref_node, rb_node); 97afce772eSLu Fengqi rb_erase(next, &ref_tree->rb_root); 98afce772eSLu Fengqi kfree(node); 99afce772eSLu Fengqi } 100afce772eSLu Fengqi 101afce772eSLu Fengqi ref_tree->rb_root = RB_ROOT; 102afce772eSLu Fengqi ref_tree->unique_refs = 0; 103afce772eSLu Fengqi } 104afce772eSLu Fengqi 105afce772eSLu Fengqi static void ref_root_free(struct ref_root *ref_tree) 106afce772eSLu Fengqi { 107afce772eSLu Fengqi if (!ref_tree) 108afce772eSLu Fengqi return; 109afce772eSLu Fengqi 110afce772eSLu Fengqi ref_root_fini(ref_tree); 111afce772eSLu Fengqi kfree(ref_tree); 112afce772eSLu Fengqi } 113afce772eSLu Fengqi 114afce772eSLu Fengqi /* 115afce772eSLu Fengqi * Compare ref_node with (root_id, object_id, offset, parent) 116afce772eSLu Fengqi * 117afce772eSLu Fengqi * The function compares two ref_node a and b. It returns an integer less 118afce772eSLu Fengqi * than, equal to, or greater than zero , respectively, to be less than, to 119afce772eSLu Fengqi * equal, or be greater than b. 120afce772eSLu Fengqi */ 121afce772eSLu Fengqi static int ref_node_cmp(struct ref_node *a, struct ref_node *b) 122afce772eSLu Fengqi { 123afce772eSLu Fengqi if (a->root_id < b->root_id) 124afce772eSLu Fengqi return -1; 125afce772eSLu Fengqi else if (a->root_id > b->root_id) 126afce772eSLu Fengqi return 1; 127afce772eSLu Fengqi 128afce772eSLu Fengqi if (a->object_id < b->object_id) 129afce772eSLu Fengqi return -1; 130afce772eSLu Fengqi else if (a->object_id > b->object_id) 131afce772eSLu Fengqi return 1; 132afce772eSLu Fengqi 133afce772eSLu Fengqi if (a->offset < b->offset) 134afce772eSLu Fengqi return -1; 135afce772eSLu Fengqi else if (a->offset > b->offset) 136afce772eSLu Fengqi return 1; 137afce772eSLu Fengqi 138afce772eSLu Fengqi if (a->parent < b->parent) 139afce772eSLu Fengqi return -1; 140afce772eSLu Fengqi else if (a->parent > b->parent) 141afce772eSLu Fengqi return 1; 142afce772eSLu Fengqi 143afce772eSLu Fengqi return 0; 144afce772eSLu Fengqi } 145afce772eSLu Fengqi 146afce772eSLu Fengqi /* 147afce772eSLu Fengqi * Search ref_node with (root_id, object_id, offset, parent) in the tree 148afce772eSLu Fengqi * 149afce772eSLu Fengqi * if found, the pointer of the ref_node will be returned; 150afce772eSLu Fengqi * if not found, NULL will be returned and pos will point to the rb_node for 151afce772eSLu Fengqi * insert, pos_parent will point to pos'parent for insert; 152afce772eSLu Fengqi */ 153afce772eSLu Fengqi static struct ref_node *__ref_tree_search(struct ref_root *ref_tree, 154afce772eSLu Fengqi struct rb_node ***pos, 155afce772eSLu Fengqi struct rb_node **pos_parent, 156afce772eSLu Fengqi u64 root_id, u64 object_id, 157afce772eSLu Fengqi u64 offset, u64 parent) 158afce772eSLu Fengqi { 159afce772eSLu Fengqi struct ref_node *cur = NULL; 160afce772eSLu Fengqi struct ref_node entry; 161afce772eSLu Fengqi int ret; 162afce772eSLu Fengqi 163afce772eSLu Fengqi entry.root_id = root_id; 164afce772eSLu Fengqi entry.object_id = object_id; 165afce772eSLu Fengqi entry.offset = offset; 166afce772eSLu Fengqi entry.parent = parent; 167afce772eSLu Fengqi 168afce772eSLu Fengqi *pos = &ref_tree->rb_root.rb_node; 169afce772eSLu Fengqi 170afce772eSLu Fengqi while (**pos) { 171afce772eSLu Fengqi *pos_parent = **pos; 172afce772eSLu Fengqi cur = rb_entry(*pos_parent, struct ref_node, rb_node); 173afce772eSLu Fengqi 174afce772eSLu Fengqi ret = ref_node_cmp(cur, &entry); 175afce772eSLu Fengqi if (ret > 0) 176afce772eSLu Fengqi *pos = &(**pos)->rb_left; 177afce772eSLu Fengqi else if (ret < 0) 178afce772eSLu Fengqi *pos = &(**pos)->rb_right; 179afce772eSLu Fengqi else 180afce772eSLu Fengqi return cur; 181afce772eSLu Fengqi } 182afce772eSLu Fengqi 183afce772eSLu Fengqi return NULL; 184afce772eSLu Fengqi } 185afce772eSLu Fengqi 186afce772eSLu Fengqi /* 187afce772eSLu Fengqi * Insert a ref_node to the ref tree 188afce772eSLu Fengqi * @pos used for specifiy the position to insert 189afce772eSLu Fengqi * @pos_parent for specifiy pos's parent 190afce772eSLu Fengqi * 191afce772eSLu Fengqi * success, return 0; 192afce772eSLu Fengqi * ref_node already exists, return -EEXIST; 193afce772eSLu Fengqi */ 194afce772eSLu Fengqi static int ref_tree_insert(struct ref_root *ref_tree, struct rb_node **pos, 195afce772eSLu Fengqi struct rb_node *pos_parent, struct ref_node *ins) 196afce772eSLu Fengqi { 197afce772eSLu Fengqi struct rb_node **p = NULL; 198afce772eSLu Fengqi struct rb_node *parent = NULL; 199afce772eSLu Fengqi struct ref_node *cur = NULL; 200afce772eSLu Fengqi 201afce772eSLu Fengqi if (!pos) { 202afce772eSLu Fengqi cur = __ref_tree_search(ref_tree, &p, &parent, ins->root_id, 203afce772eSLu Fengqi ins->object_id, ins->offset, 204afce772eSLu Fengqi ins->parent); 205afce772eSLu Fengqi if (cur) 206afce772eSLu Fengqi return -EEXIST; 207afce772eSLu Fengqi } else { 208afce772eSLu Fengqi p = pos; 209afce772eSLu Fengqi parent = pos_parent; 210afce772eSLu Fengqi } 211afce772eSLu Fengqi 212afce772eSLu Fengqi rb_link_node(&ins->rb_node, parent, p); 213afce772eSLu Fengqi rb_insert_color(&ins->rb_node, &ref_tree->rb_root); 214afce772eSLu Fengqi 215afce772eSLu Fengqi return 0; 216afce772eSLu Fengqi } 217afce772eSLu Fengqi 218afce772eSLu Fengqi /* Erase and free ref_node, caller should update ref_root->unique_refs */ 219afce772eSLu Fengqi static void ref_tree_remove(struct ref_root *ref_tree, struct ref_node *node) 220afce772eSLu Fengqi { 221afce772eSLu Fengqi rb_erase(&node->rb_node, &ref_tree->rb_root); 222afce772eSLu Fengqi kfree(node); 223afce772eSLu Fengqi } 224afce772eSLu Fengqi 225afce772eSLu Fengqi /* 226afce772eSLu Fengqi * Update ref_root->unique_refs 227afce772eSLu Fengqi * 228afce772eSLu Fengqi * Call __ref_tree_search 229afce772eSLu Fengqi * 1. if ref_node doesn't exist, ref_tree_insert this node, and update 230afce772eSLu Fengqi * ref_root->unique_refs: 231afce772eSLu Fengqi * if ref_node->ref_mod > 0, ref_root->unique_refs++; 232afce772eSLu Fengqi * if ref_node->ref_mod < 0, do noting; 233afce772eSLu Fengqi * 234afce772eSLu Fengqi * 2. if ref_node is found, then get origin ref_node->ref_mod, and update 235afce772eSLu Fengqi * ref_node->ref_mod. 236afce772eSLu Fengqi * if ref_node->ref_mod is equal to 0,then call ref_tree_remove 237afce772eSLu Fengqi * 238afce772eSLu Fengqi * according to origin_mod and new_mod, update ref_root->items 239afce772eSLu Fengqi * +----------------+--------------+-------------+ 240afce772eSLu Fengqi * | |new_count <= 0|new_count > 0| 241afce772eSLu Fengqi * +----------------+--------------+-------------+ 242afce772eSLu Fengqi * |origin_count < 0| 0 | 1 | 243afce772eSLu Fengqi * +----------------+--------------+-------------+ 244afce772eSLu Fengqi * |origin_count > 0| -1 | 0 | 245afce772eSLu Fengqi * +----------------+--------------+-------------+ 246afce772eSLu Fengqi * 247afce772eSLu Fengqi * In case of allocation failure, -ENOMEM is returned and the ref_tree stays 248afce772eSLu Fengqi * unaltered. 249afce772eSLu Fengqi * Success, return 0 250afce772eSLu Fengqi */ 251afce772eSLu Fengqi static int ref_tree_add(struct ref_root *ref_tree, u64 root_id, u64 object_id, 252afce772eSLu Fengqi u64 offset, u64 parent, int count) 253afce772eSLu Fengqi { 254afce772eSLu Fengqi struct ref_node *node = NULL; 255afce772eSLu Fengqi struct rb_node **pos = NULL; 256afce772eSLu Fengqi struct rb_node *pos_parent = NULL; 257afce772eSLu Fengqi int origin_count; 258afce772eSLu Fengqi int ret; 259afce772eSLu Fengqi 260afce772eSLu Fengqi if (!count) 261afce772eSLu Fengqi return 0; 262afce772eSLu Fengqi 263afce772eSLu Fengqi node = __ref_tree_search(ref_tree, &pos, &pos_parent, root_id, 264afce772eSLu Fengqi object_id, offset, parent); 265afce772eSLu Fengqi if (node == NULL) { 266afce772eSLu Fengqi node = kmalloc(sizeof(*node), GFP_NOFS); 267afce772eSLu Fengqi if (!node) 268afce772eSLu Fengqi return -ENOMEM; 269afce772eSLu Fengqi 270afce772eSLu Fengqi node->root_id = root_id; 271afce772eSLu Fengqi node->object_id = object_id; 272afce772eSLu Fengqi node->offset = offset; 273afce772eSLu Fengqi node->parent = parent; 274afce772eSLu Fengqi node->ref_mod = count; 275afce772eSLu Fengqi 276afce772eSLu Fengqi ret = ref_tree_insert(ref_tree, pos, pos_parent, node); 277afce772eSLu Fengqi ASSERT(!ret); 278afce772eSLu Fengqi if (ret) { 279afce772eSLu Fengqi kfree(node); 280afce772eSLu Fengqi return ret; 281afce772eSLu Fengqi } 282afce772eSLu Fengqi 283afce772eSLu Fengqi ref_tree->unique_refs += node->ref_mod > 0 ? 1 : 0; 284afce772eSLu Fengqi 285afce772eSLu Fengqi return 0; 286afce772eSLu Fengqi } 287afce772eSLu Fengqi 288afce772eSLu Fengqi origin_count = node->ref_mod; 289afce772eSLu Fengqi node->ref_mod += count; 290afce772eSLu Fengqi 291afce772eSLu Fengqi if (node->ref_mod > 0) 292afce772eSLu Fengqi ref_tree->unique_refs += origin_count > 0 ? 0 : 1; 293afce772eSLu Fengqi else if (node->ref_mod <= 0) 294afce772eSLu Fengqi ref_tree->unique_refs += origin_count > 0 ? -1 : 0; 295afce772eSLu Fengqi 296afce772eSLu Fengqi if (!node->ref_mod) 297afce772eSLu Fengqi ref_tree_remove(ref_tree, node); 298afce772eSLu Fengqi 299afce772eSLu Fengqi return 0; 300afce772eSLu Fengqi } 301afce772eSLu Fengqi 30273980becSJeff Mahoney static int check_extent_in_eb(const struct btrfs_key *key, 30373980becSJeff Mahoney const struct extent_buffer *eb, 30473980becSJeff Mahoney const struct btrfs_file_extent_item *fi, 305976b1908SJan Schmidt u64 extent_item_pos, 306976b1908SJan Schmidt struct extent_inode_elem **eie) 307976b1908SJan Schmidt { 3088ca15e05SJosef Bacik u64 offset = 0; 3098ca15e05SJosef Bacik struct extent_inode_elem *e; 3108ca15e05SJosef Bacik 3118ca15e05SJosef Bacik if (!btrfs_file_extent_compression(eb, fi) && 3128ca15e05SJosef Bacik !btrfs_file_extent_encryption(eb, fi) && 3138ca15e05SJosef Bacik !btrfs_file_extent_other_encoding(eb, fi)) { 314976b1908SJan Schmidt u64 data_offset; 315976b1908SJan Schmidt u64 data_len; 316976b1908SJan Schmidt 317976b1908SJan Schmidt data_offset = btrfs_file_extent_offset(eb, fi); 318976b1908SJan Schmidt data_len = btrfs_file_extent_num_bytes(eb, fi); 319976b1908SJan Schmidt 320976b1908SJan Schmidt if (extent_item_pos < data_offset || 321976b1908SJan Schmidt extent_item_pos >= data_offset + data_len) 322976b1908SJan Schmidt return 1; 3238ca15e05SJosef Bacik offset = extent_item_pos - data_offset; 3248ca15e05SJosef Bacik } 325976b1908SJan Schmidt 326976b1908SJan Schmidt e = kmalloc(sizeof(*e), GFP_NOFS); 327976b1908SJan Schmidt if (!e) 328976b1908SJan Schmidt return -ENOMEM; 329976b1908SJan Schmidt 330976b1908SJan Schmidt e->next = *eie; 331976b1908SJan Schmidt e->inum = key->objectid; 3328ca15e05SJosef Bacik e->offset = key->offset + offset; 333976b1908SJan Schmidt *eie = e; 334976b1908SJan Schmidt 335976b1908SJan Schmidt return 0; 336976b1908SJan Schmidt } 337976b1908SJan Schmidt 338f05c4746SWang Shilong static void free_inode_elem_list(struct extent_inode_elem *eie) 339f05c4746SWang Shilong { 340f05c4746SWang Shilong struct extent_inode_elem *eie_next; 341f05c4746SWang Shilong 342f05c4746SWang Shilong for (; eie; eie = eie_next) { 343f05c4746SWang Shilong eie_next = eie->next; 344f05c4746SWang Shilong kfree(eie); 345f05c4746SWang Shilong } 346f05c4746SWang Shilong } 347f05c4746SWang Shilong 34873980becSJeff Mahoney static int find_extent_in_eb(const struct extent_buffer *eb, 34973980becSJeff Mahoney u64 wanted_disk_byte, u64 extent_item_pos, 350976b1908SJan Schmidt struct extent_inode_elem **eie) 351976b1908SJan Schmidt { 352976b1908SJan Schmidt u64 disk_byte; 353976b1908SJan Schmidt struct btrfs_key key; 354976b1908SJan Schmidt struct btrfs_file_extent_item *fi; 355976b1908SJan Schmidt int slot; 356976b1908SJan Schmidt int nritems; 357976b1908SJan Schmidt int extent_type; 358976b1908SJan Schmidt int ret; 359976b1908SJan Schmidt 360976b1908SJan Schmidt /* 361976b1908SJan Schmidt * from the shared data ref, we only have the leaf but we need 362976b1908SJan Schmidt * the key. thus, we must look into all items and see that we 363976b1908SJan Schmidt * find one (some) with a reference to our extent item. 364976b1908SJan Schmidt */ 365976b1908SJan Schmidt nritems = btrfs_header_nritems(eb); 366976b1908SJan Schmidt for (slot = 0; slot < nritems; ++slot) { 367976b1908SJan Schmidt btrfs_item_key_to_cpu(eb, &key, slot); 368976b1908SJan Schmidt if (key.type != BTRFS_EXTENT_DATA_KEY) 369976b1908SJan Schmidt continue; 370976b1908SJan Schmidt fi = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item); 371976b1908SJan Schmidt extent_type = btrfs_file_extent_type(eb, fi); 372976b1908SJan Schmidt if (extent_type == BTRFS_FILE_EXTENT_INLINE) 373976b1908SJan Schmidt continue; 374976b1908SJan Schmidt /* don't skip BTRFS_FILE_EXTENT_PREALLOC, we can handle that */ 375976b1908SJan Schmidt disk_byte = btrfs_file_extent_disk_bytenr(eb, fi); 376976b1908SJan Schmidt if (disk_byte != wanted_disk_byte) 377976b1908SJan Schmidt continue; 378976b1908SJan Schmidt 379976b1908SJan Schmidt ret = check_extent_in_eb(&key, eb, fi, extent_item_pos, eie); 380976b1908SJan Schmidt if (ret < 0) 381976b1908SJan Schmidt return ret; 382976b1908SJan Schmidt } 383976b1908SJan Schmidt 384976b1908SJan Schmidt return 0; 385976b1908SJan Schmidt } 386976b1908SJan Schmidt 3878da6d581SJan Schmidt /* 3888da6d581SJan Schmidt * this structure records all encountered refs on the way up to the root 3898da6d581SJan Schmidt */ 3908da6d581SJan Schmidt struct __prelim_ref { 3918da6d581SJan Schmidt struct list_head list; 3928da6d581SJan Schmidt u64 root_id; 393d5c88b73SJan Schmidt struct btrfs_key key_for_search; 3948da6d581SJan Schmidt int level; 3958da6d581SJan Schmidt int count; 3963301958bSJan Schmidt struct extent_inode_elem *inode_list; 3978da6d581SJan Schmidt u64 parent; 3988da6d581SJan Schmidt u64 wanted_disk_byte; 3998da6d581SJan Schmidt }; 4008da6d581SJan Schmidt 401b9e9a6cbSWang Shilong static struct kmem_cache *btrfs_prelim_ref_cache; 402b9e9a6cbSWang Shilong 403b9e9a6cbSWang Shilong int __init btrfs_prelim_ref_init(void) 404b9e9a6cbSWang Shilong { 405b9e9a6cbSWang Shilong btrfs_prelim_ref_cache = kmem_cache_create("btrfs_prelim_ref", 406b9e9a6cbSWang Shilong sizeof(struct __prelim_ref), 407b9e9a6cbSWang Shilong 0, 408fba4b697SNikolay Borisov SLAB_MEM_SPREAD, 409b9e9a6cbSWang Shilong NULL); 410b9e9a6cbSWang Shilong if (!btrfs_prelim_ref_cache) 411b9e9a6cbSWang Shilong return -ENOMEM; 412b9e9a6cbSWang Shilong return 0; 413b9e9a6cbSWang Shilong } 414b9e9a6cbSWang Shilong 415b9e9a6cbSWang Shilong void btrfs_prelim_ref_exit(void) 416b9e9a6cbSWang Shilong { 417b9e9a6cbSWang Shilong kmem_cache_destroy(btrfs_prelim_ref_cache); 418b9e9a6cbSWang Shilong } 419b9e9a6cbSWang Shilong 420d5c88b73SJan Schmidt /* 421d5c88b73SJan Schmidt * the rules for all callers of this function are: 422d5c88b73SJan Schmidt * - obtaining the parent is the goal 423d5c88b73SJan Schmidt * - if you add a key, you must know that it is a correct key 424d5c88b73SJan Schmidt * - if you cannot add the parent or a correct key, then we will look into the 425d5c88b73SJan Schmidt * block later to set a correct key 426d5c88b73SJan Schmidt * 427d5c88b73SJan Schmidt * delayed refs 428d5c88b73SJan Schmidt * ============ 429d5c88b73SJan Schmidt * backref type | shared | indirect | shared | indirect 430d5c88b73SJan Schmidt * information | tree | tree | data | data 431d5c88b73SJan Schmidt * --------------------+--------+----------+--------+---------- 432d5c88b73SJan Schmidt * parent logical | y | - | - | - 433d5c88b73SJan Schmidt * key to resolve | - | y | y | y 434d5c88b73SJan Schmidt * tree block logical | - | - | - | - 435d5c88b73SJan Schmidt * root for resolving | y | y | y | y 436d5c88b73SJan Schmidt * 437d5c88b73SJan Schmidt * - column 1: we've the parent -> done 438d5c88b73SJan Schmidt * - column 2, 3, 4: we use the key to find the parent 439d5c88b73SJan Schmidt * 440d5c88b73SJan Schmidt * on disk refs (inline or keyed) 441d5c88b73SJan Schmidt * ============================== 442d5c88b73SJan Schmidt * backref type | shared | indirect | shared | indirect 443d5c88b73SJan Schmidt * information | tree | tree | data | data 444d5c88b73SJan Schmidt * --------------------+--------+----------+--------+---------- 445d5c88b73SJan Schmidt * parent logical | y | - | y | - 446d5c88b73SJan Schmidt * key to resolve | - | - | - | y 447d5c88b73SJan Schmidt * tree block logical | y | y | y | y 448d5c88b73SJan Schmidt * root for resolving | - | y | y | y 449d5c88b73SJan Schmidt * 450d5c88b73SJan Schmidt * - column 1, 3: we've the parent -> done 451d5c88b73SJan Schmidt * - column 2: we take the first key from the block to find the parent 452d5c88b73SJan Schmidt * (see __add_missing_keys) 453d5c88b73SJan Schmidt * - column 4: we use the key to find the parent 454d5c88b73SJan Schmidt * 455d5c88b73SJan Schmidt * additional information that's available but not required to find the parent 456d5c88b73SJan Schmidt * block might help in merging entries to gain some speed. 457d5c88b73SJan Schmidt */ 458d5c88b73SJan Schmidt 4598da6d581SJan Schmidt static int __add_prelim_ref(struct list_head *head, u64 root_id, 46073980becSJeff Mahoney const struct btrfs_key *key, int level, 461742916b8SWang Shilong u64 parent, u64 wanted_disk_byte, int count, 462742916b8SWang Shilong gfp_t gfp_mask) 4638da6d581SJan Schmidt { 4648da6d581SJan Schmidt struct __prelim_ref *ref; 4658da6d581SJan Schmidt 46648ec4736SLiu Bo if (root_id == BTRFS_DATA_RELOC_TREE_OBJECTID) 46748ec4736SLiu Bo return 0; 46848ec4736SLiu Bo 469b9e9a6cbSWang Shilong ref = kmem_cache_alloc(btrfs_prelim_ref_cache, gfp_mask); 4708da6d581SJan Schmidt if (!ref) 4718da6d581SJan Schmidt return -ENOMEM; 4728da6d581SJan Schmidt 4738da6d581SJan Schmidt ref->root_id = root_id; 474d6589101SFilipe Manana if (key) { 475d5c88b73SJan Schmidt ref->key_for_search = *key; 476d6589101SFilipe Manana /* 477d6589101SFilipe Manana * We can often find data backrefs with an offset that is too 478d6589101SFilipe Manana * large (>= LLONG_MAX, maximum allowed file offset) due to 479d6589101SFilipe Manana * underflows when subtracting a file's offset with the data 480d6589101SFilipe Manana * offset of its corresponding extent data item. This can 481d6589101SFilipe Manana * happen for example in the clone ioctl. 482d6589101SFilipe Manana * So if we detect such case we set the search key's offset to 483d6589101SFilipe Manana * zero to make sure we will find the matching file extent item 484d6589101SFilipe Manana * at add_all_parents(), otherwise we will miss it because the 485d6589101SFilipe Manana * offset taken form the backref is much larger then the offset 486d6589101SFilipe Manana * of the file extent item. This can make us scan a very large 487d6589101SFilipe Manana * number of file extent items, but at least it will not make 488d6589101SFilipe Manana * us miss any. 489d6589101SFilipe Manana * This is an ugly workaround for a behaviour that should have 490d6589101SFilipe Manana * never existed, but it does and a fix for the clone ioctl 491d6589101SFilipe Manana * would touch a lot of places, cause backwards incompatibility 492d6589101SFilipe Manana * and would not fix the problem for extents cloned with older 493d6589101SFilipe Manana * kernels. 494d6589101SFilipe Manana */ 495d6589101SFilipe Manana if (ref->key_for_search.type == BTRFS_EXTENT_DATA_KEY && 496d6589101SFilipe Manana ref->key_for_search.offset >= LLONG_MAX) 497d6589101SFilipe Manana ref->key_for_search.offset = 0; 498d6589101SFilipe Manana } else { 499d5c88b73SJan Schmidt memset(&ref->key_for_search, 0, sizeof(ref->key_for_search)); 500d6589101SFilipe Manana } 5018da6d581SJan Schmidt 5023301958bSJan Schmidt ref->inode_list = NULL; 5038da6d581SJan Schmidt ref->level = level; 5048da6d581SJan Schmidt ref->count = count; 5058da6d581SJan Schmidt ref->parent = parent; 5068da6d581SJan Schmidt ref->wanted_disk_byte = wanted_disk_byte; 5078da6d581SJan Schmidt list_add_tail(&ref->list, head); 5088da6d581SJan Schmidt 5098da6d581SJan Schmidt return 0; 5108da6d581SJan Schmidt } 5118da6d581SJan Schmidt 5128da6d581SJan Schmidt static int add_all_parents(struct btrfs_root *root, struct btrfs_path *path, 5137ef81ac8SJosef Bacik struct ulist *parents, struct __prelim_ref *ref, 51444853868SJosef Bacik int level, u64 time_seq, const u64 *extent_item_pos, 51544853868SJosef Bacik u64 total_refs) 5168da6d581SJan Schmidt { 51769bca40dSAlexander Block int ret = 0; 51869bca40dSAlexander Block int slot; 51969bca40dSAlexander Block struct extent_buffer *eb; 52069bca40dSAlexander Block struct btrfs_key key; 5217ef81ac8SJosef Bacik struct btrfs_key *key_for_search = &ref->key_for_search; 5228da6d581SJan Schmidt struct btrfs_file_extent_item *fi; 523ed8c4913SJosef Bacik struct extent_inode_elem *eie = NULL, *old = NULL; 5248da6d581SJan Schmidt u64 disk_byte; 5257ef81ac8SJosef Bacik u64 wanted_disk_byte = ref->wanted_disk_byte; 5267ef81ac8SJosef Bacik u64 count = 0; 5278da6d581SJan Schmidt 52869bca40dSAlexander Block if (level != 0) { 52969bca40dSAlexander Block eb = path->nodes[level]; 53069bca40dSAlexander Block ret = ulist_add(parents, eb->start, 0, GFP_NOFS); 5313301958bSJan Schmidt if (ret < 0) 5323301958bSJan Schmidt return ret; 5338da6d581SJan Schmidt return 0; 53469bca40dSAlexander Block } 5358da6d581SJan Schmidt 5368da6d581SJan Schmidt /* 53769bca40dSAlexander Block * We normally enter this function with the path already pointing to 53869bca40dSAlexander Block * the first item to check. But sometimes, we may enter it with 53969bca40dSAlexander Block * slot==nritems. In that case, go to the next leaf before we continue. 5408da6d581SJan Schmidt */ 54121633fc6SQu Wenruo if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) { 542de47c9d3SEdmund Nadolski if (time_seq == SEQ_LAST) 54321633fc6SQu Wenruo ret = btrfs_next_leaf(root, path); 54421633fc6SQu Wenruo else 5453d7806ecSJan Schmidt ret = btrfs_next_old_leaf(root, path, time_seq); 54621633fc6SQu Wenruo } 5478da6d581SJan Schmidt 54844853868SJosef Bacik while (!ret && count < total_refs) { 5498da6d581SJan Schmidt eb = path->nodes[0]; 55069bca40dSAlexander Block slot = path->slots[0]; 55169bca40dSAlexander Block 55269bca40dSAlexander Block btrfs_item_key_to_cpu(eb, &key, slot); 55369bca40dSAlexander Block 55469bca40dSAlexander Block if (key.objectid != key_for_search->objectid || 55569bca40dSAlexander Block key.type != BTRFS_EXTENT_DATA_KEY) 55669bca40dSAlexander Block break; 55769bca40dSAlexander Block 55869bca40dSAlexander Block fi = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item); 5598da6d581SJan Schmidt disk_byte = btrfs_file_extent_disk_bytenr(eb, fi); 56069bca40dSAlexander Block 56169bca40dSAlexander Block if (disk_byte == wanted_disk_byte) { 56269bca40dSAlexander Block eie = NULL; 563ed8c4913SJosef Bacik old = NULL; 5647ef81ac8SJosef Bacik count++; 56569bca40dSAlexander Block if (extent_item_pos) { 56669bca40dSAlexander Block ret = check_extent_in_eb(&key, eb, fi, 56769bca40dSAlexander Block *extent_item_pos, 56869bca40dSAlexander Block &eie); 56969bca40dSAlexander Block if (ret < 0) 57069bca40dSAlexander Block break; 5718da6d581SJan Schmidt } 572ed8c4913SJosef Bacik if (ret > 0) 573ed8c4913SJosef Bacik goto next; 5744eb1f66dSTakashi Iwai ret = ulist_add_merge_ptr(parents, eb->start, 5754eb1f66dSTakashi Iwai eie, (void **)&old, GFP_NOFS); 57669bca40dSAlexander Block if (ret < 0) 57769bca40dSAlexander Block break; 578ed8c4913SJosef Bacik if (!ret && extent_item_pos) { 579ed8c4913SJosef Bacik while (old->next) 580ed8c4913SJosef Bacik old = old->next; 581ed8c4913SJosef Bacik old->next = eie; 58269bca40dSAlexander Block } 583f05c4746SWang Shilong eie = NULL; 58469bca40dSAlexander Block } 585ed8c4913SJosef Bacik next: 586de47c9d3SEdmund Nadolski if (time_seq == SEQ_LAST) 58721633fc6SQu Wenruo ret = btrfs_next_item(root, path); 58821633fc6SQu Wenruo else 58969bca40dSAlexander Block ret = btrfs_next_old_item(root, path, time_seq); 5908da6d581SJan Schmidt } 5918da6d581SJan Schmidt 59269bca40dSAlexander Block if (ret > 0) 59369bca40dSAlexander Block ret = 0; 594f05c4746SWang Shilong else if (ret < 0) 595f05c4746SWang Shilong free_inode_elem_list(eie); 59669bca40dSAlexander Block return ret; 5978da6d581SJan Schmidt } 5988da6d581SJan Schmidt 5998da6d581SJan Schmidt /* 6008da6d581SJan Schmidt * resolve an indirect backref in the form (root_id, key, level) 6018da6d581SJan Schmidt * to a logical address 6028da6d581SJan Schmidt */ 6038da6d581SJan Schmidt static int __resolve_indirect_ref(struct btrfs_fs_info *fs_info, 604da61d31aSJosef Bacik struct btrfs_path *path, u64 time_seq, 6058da6d581SJan Schmidt struct __prelim_ref *ref, 606976b1908SJan Schmidt struct ulist *parents, 60744853868SJosef Bacik const u64 *extent_item_pos, u64 total_refs) 6088da6d581SJan Schmidt { 6098da6d581SJan Schmidt struct btrfs_root *root; 6108da6d581SJan Schmidt struct btrfs_key root_key; 6118da6d581SJan Schmidt struct extent_buffer *eb; 6128da6d581SJan Schmidt int ret = 0; 6138da6d581SJan Schmidt int root_level; 6148da6d581SJan Schmidt int level = ref->level; 615538f72cdSWang Shilong int index; 6168da6d581SJan Schmidt 6178da6d581SJan Schmidt root_key.objectid = ref->root_id; 6188da6d581SJan Schmidt root_key.type = BTRFS_ROOT_ITEM_KEY; 6198da6d581SJan Schmidt root_key.offset = (u64)-1; 620538f72cdSWang Shilong 621538f72cdSWang Shilong index = srcu_read_lock(&fs_info->subvol_srcu); 622538f72cdSWang Shilong 6232d9e9776SJosef Bacik root = btrfs_get_fs_root(fs_info, &root_key, false); 6248da6d581SJan Schmidt if (IS_ERR(root)) { 625538f72cdSWang Shilong srcu_read_unlock(&fs_info->subvol_srcu, index); 6268da6d581SJan Schmidt ret = PTR_ERR(root); 6278da6d581SJan Schmidt goto out; 6288da6d581SJan Schmidt } 6298da6d581SJan Schmidt 630f5ee5c9aSJeff Mahoney if (btrfs_is_testing(fs_info)) { 631d9ee522bSJosef Bacik srcu_read_unlock(&fs_info->subvol_srcu, index); 632d9ee522bSJosef Bacik ret = -ENOENT; 633d9ee522bSJosef Bacik goto out; 634d9ee522bSJosef Bacik } 635d9ee522bSJosef Bacik 6369e351cc8SJosef Bacik if (path->search_commit_root) 6379e351cc8SJosef Bacik root_level = btrfs_header_level(root->commit_root); 638de47c9d3SEdmund Nadolski else if (time_seq == SEQ_LAST) 63921633fc6SQu Wenruo root_level = btrfs_header_level(root->node); 6409e351cc8SJosef Bacik else 6415b6602e7SJan Schmidt root_level = btrfs_old_root_level(root, time_seq); 6428da6d581SJan Schmidt 643538f72cdSWang Shilong if (root_level + 1 == level) { 644538f72cdSWang Shilong srcu_read_unlock(&fs_info->subvol_srcu, index); 6458da6d581SJan Schmidt goto out; 646538f72cdSWang Shilong } 6478da6d581SJan Schmidt 6488da6d581SJan Schmidt path->lowest_level = level; 649de47c9d3SEdmund Nadolski if (time_seq == SEQ_LAST) 65021633fc6SQu Wenruo ret = btrfs_search_slot(NULL, root, &ref->key_for_search, path, 65121633fc6SQu Wenruo 0, 0); 65221633fc6SQu Wenruo else 65321633fc6SQu Wenruo ret = btrfs_search_old_slot(root, &ref->key_for_search, path, 65421633fc6SQu Wenruo time_seq); 655538f72cdSWang Shilong 656538f72cdSWang Shilong /* root node has been locked, we can release @subvol_srcu safely here */ 657538f72cdSWang Shilong srcu_read_unlock(&fs_info->subvol_srcu, index); 658538f72cdSWang Shilong 659ab8d0fc4SJeff Mahoney btrfs_debug(fs_info, 660ab8d0fc4SJeff Mahoney "search slot in root %llu (level %d, ref count %d) returned %d for key (%llu %u %llu)", 661c1c9ff7cSGeert Uytterhoeven ref->root_id, level, ref->count, ret, 662c1c9ff7cSGeert Uytterhoeven ref->key_for_search.objectid, ref->key_for_search.type, 663c1c9ff7cSGeert Uytterhoeven ref->key_for_search.offset); 6648da6d581SJan Schmidt if (ret < 0) 6658da6d581SJan Schmidt goto out; 6668da6d581SJan Schmidt 6678da6d581SJan Schmidt eb = path->nodes[level]; 6689345457fSJan Schmidt while (!eb) { 669fae7f21cSDulshani Gunawardhana if (WARN_ON(!level)) { 6708da6d581SJan Schmidt ret = 1; 6718da6d581SJan Schmidt goto out; 6728da6d581SJan Schmidt } 6739345457fSJan Schmidt level--; 6749345457fSJan Schmidt eb = path->nodes[level]; 6759345457fSJan Schmidt } 6768da6d581SJan Schmidt 6777ef81ac8SJosef Bacik ret = add_all_parents(root, path, parents, ref, level, time_seq, 67844853868SJosef Bacik extent_item_pos, total_refs); 6798da6d581SJan Schmidt out: 680da61d31aSJosef Bacik path->lowest_level = 0; 681da61d31aSJosef Bacik btrfs_release_path(path); 6828da6d581SJan Schmidt return ret; 6838da6d581SJan Schmidt } 6848da6d581SJan Schmidt 685*4dae077aSJeff Mahoney static struct extent_inode_elem * 686*4dae077aSJeff Mahoney unode_aux_to_inode_list(struct ulist_node *node) 687*4dae077aSJeff Mahoney { 688*4dae077aSJeff Mahoney if (!node) 689*4dae077aSJeff Mahoney return NULL; 690*4dae077aSJeff Mahoney return (struct extent_inode_elem *)(uintptr_t)node->aux; 691*4dae077aSJeff Mahoney } 692*4dae077aSJeff Mahoney 6938da6d581SJan Schmidt /* 6948da6d581SJan Schmidt * resolve all indirect backrefs from the list 6958da6d581SJan Schmidt */ 6968da6d581SJan Schmidt static int __resolve_indirect_refs(struct btrfs_fs_info *fs_info, 697da61d31aSJosef Bacik struct btrfs_path *path, u64 time_seq, 698976b1908SJan Schmidt struct list_head *head, 699dc046b10SJosef Bacik const u64 *extent_item_pos, u64 total_refs, 700dc046b10SJosef Bacik u64 root_objectid) 7018da6d581SJan Schmidt { 7028da6d581SJan Schmidt int err; 7038da6d581SJan Schmidt int ret = 0; 7048da6d581SJan Schmidt struct __prelim_ref *ref; 7058da6d581SJan Schmidt struct __prelim_ref *ref_safe; 7068da6d581SJan Schmidt struct __prelim_ref *new_ref; 7078da6d581SJan Schmidt struct ulist *parents; 7088da6d581SJan Schmidt struct ulist_node *node; 709cd1b413cSJan Schmidt struct ulist_iterator uiter; 7108da6d581SJan Schmidt 7118da6d581SJan Schmidt parents = ulist_alloc(GFP_NOFS); 7128da6d581SJan Schmidt if (!parents) 7138da6d581SJan Schmidt return -ENOMEM; 7148da6d581SJan Schmidt 7158da6d581SJan Schmidt /* 7168da6d581SJan Schmidt * _safe allows us to insert directly after the current item without 7178da6d581SJan Schmidt * iterating over the newly inserted items. 7188da6d581SJan Schmidt * we're also allowed to re-assign ref during iteration. 7198da6d581SJan Schmidt */ 7208da6d581SJan Schmidt list_for_each_entry_safe(ref, ref_safe, head, list) { 7218da6d581SJan Schmidt if (ref->parent) /* already direct */ 7228da6d581SJan Schmidt continue; 7238da6d581SJan Schmidt if (ref->count == 0) 7248da6d581SJan Schmidt continue; 725dc046b10SJosef Bacik if (root_objectid && ref->root_id != root_objectid) { 726dc046b10SJosef Bacik ret = BACKREF_FOUND_SHARED; 727dc046b10SJosef Bacik goto out; 728dc046b10SJosef Bacik } 729da61d31aSJosef Bacik err = __resolve_indirect_ref(fs_info, path, time_seq, ref, 73044853868SJosef Bacik parents, extent_item_pos, 73144853868SJosef Bacik total_refs); 73295def2edSWang Shilong /* 73395def2edSWang Shilong * we can only tolerate ENOENT,otherwise,we should catch error 73495def2edSWang Shilong * and return directly. 73595def2edSWang Shilong */ 73695def2edSWang Shilong if (err == -ENOENT) { 7378da6d581SJan Schmidt continue; 73895def2edSWang Shilong } else if (err) { 73995def2edSWang Shilong ret = err; 74095def2edSWang Shilong goto out; 74195def2edSWang Shilong } 7428da6d581SJan Schmidt 7438da6d581SJan Schmidt /* we put the first parent into the ref at hand */ 744cd1b413cSJan Schmidt ULIST_ITER_INIT(&uiter); 745cd1b413cSJan Schmidt node = ulist_next(parents, &uiter); 7468da6d581SJan Schmidt ref->parent = node ? node->val : 0; 747*4dae077aSJeff Mahoney ref->inode_list = unode_aux_to_inode_list(node); 7488da6d581SJan Schmidt 7498da6d581SJan Schmidt /* additional parents require new refs being added here */ 750cd1b413cSJan Schmidt while ((node = ulist_next(parents, &uiter))) { 751b9e9a6cbSWang Shilong new_ref = kmem_cache_alloc(btrfs_prelim_ref_cache, 752b9e9a6cbSWang Shilong GFP_NOFS); 7538da6d581SJan Schmidt if (!new_ref) { 7548da6d581SJan Schmidt ret = -ENOMEM; 755e36902d4SWang Shilong goto out; 7568da6d581SJan Schmidt } 7578da6d581SJan Schmidt memcpy(new_ref, ref, sizeof(*ref)); 7588da6d581SJan Schmidt new_ref->parent = node->val; 759*4dae077aSJeff Mahoney new_ref->inode_list = unode_aux_to_inode_list(node); 7608da6d581SJan Schmidt list_add(&new_ref->list, &ref->list); 7618da6d581SJan Schmidt } 7628da6d581SJan Schmidt ulist_reinit(parents); 7638da6d581SJan Schmidt } 764e36902d4SWang Shilong out: 7658da6d581SJan Schmidt ulist_free(parents); 7668da6d581SJan Schmidt return ret; 7678da6d581SJan Schmidt } 7688da6d581SJan Schmidt 769d5c88b73SJan Schmidt static inline int ref_for_same_block(struct __prelim_ref *ref1, 770d5c88b73SJan Schmidt struct __prelim_ref *ref2) 771d5c88b73SJan Schmidt { 772d5c88b73SJan Schmidt if (ref1->level != ref2->level) 773d5c88b73SJan Schmidt return 0; 774d5c88b73SJan Schmidt if (ref1->root_id != ref2->root_id) 775d5c88b73SJan Schmidt return 0; 776d5c88b73SJan Schmidt if (ref1->key_for_search.type != ref2->key_for_search.type) 777d5c88b73SJan Schmidt return 0; 778d5c88b73SJan Schmidt if (ref1->key_for_search.objectid != ref2->key_for_search.objectid) 779d5c88b73SJan Schmidt return 0; 780d5c88b73SJan Schmidt if (ref1->key_for_search.offset != ref2->key_for_search.offset) 781d5c88b73SJan Schmidt return 0; 782d5c88b73SJan Schmidt if (ref1->parent != ref2->parent) 783d5c88b73SJan Schmidt return 0; 784d5c88b73SJan Schmidt 785d5c88b73SJan Schmidt return 1; 786d5c88b73SJan Schmidt } 787d5c88b73SJan Schmidt 788d5c88b73SJan Schmidt /* 789d5c88b73SJan Schmidt * read tree blocks and add keys where required. 790d5c88b73SJan Schmidt */ 791d5c88b73SJan Schmidt static int __add_missing_keys(struct btrfs_fs_info *fs_info, 792d5c88b73SJan Schmidt struct list_head *head) 793d5c88b73SJan Schmidt { 794a7ca4225SGeliang Tang struct __prelim_ref *ref; 795d5c88b73SJan Schmidt struct extent_buffer *eb; 796d5c88b73SJan Schmidt 797a7ca4225SGeliang Tang list_for_each_entry(ref, head, list) { 798d5c88b73SJan Schmidt if (ref->parent) 799d5c88b73SJan Schmidt continue; 800d5c88b73SJan Schmidt if (ref->key_for_search.type) 801d5c88b73SJan Schmidt continue; 802d5c88b73SJan Schmidt BUG_ON(!ref->wanted_disk_byte); 8032ff7e61eSJeff Mahoney eb = read_tree_block(fs_info, ref->wanted_disk_byte, 0); 80464c043deSLiu Bo if (IS_ERR(eb)) { 80564c043deSLiu Bo return PTR_ERR(eb); 80664c043deSLiu Bo } else if (!extent_buffer_uptodate(eb)) { 807416bc658SJosef Bacik free_extent_buffer(eb); 808416bc658SJosef Bacik return -EIO; 809416bc658SJosef Bacik } 810d5c88b73SJan Schmidt btrfs_tree_read_lock(eb); 811d5c88b73SJan Schmidt if (btrfs_header_level(eb) == 0) 812d5c88b73SJan Schmidt btrfs_item_key_to_cpu(eb, &ref->key_for_search, 0); 813d5c88b73SJan Schmidt else 814d5c88b73SJan Schmidt btrfs_node_key_to_cpu(eb, &ref->key_for_search, 0); 815d5c88b73SJan Schmidt btrfs_tree_read_unlock(eb); 816d5c88b73SJan Schmidt free_extent_buffer(eb); 817d5c88b73SJan Schmidt } 818d5c88b73SJan Schmidt return 0; 819d5c88b73SJan Schmidt } 820d5c88b73SJan Schmidt 8218da6d581SJan Schmidt /* 82200db646dSQu Wenruo * merge backrefs and adjust counts accordingly 8238da6d581SJan Schmidt * 824f58d88b3SEdmund Nadolski * FIXME: For MERGE_IDENTICAL_KEYS, if we add more keys in __add_prelim_ref 825f58d88b3SEdmund Nadolski * then we can merge more here. Additionally, we could even add a key 826f58d88b3SEdmund Nadolski * range for the blocks we looked into to merge even more (-> replace 827f58d88b3SEdmund Nadolski * unresolved refs by those having a parent). 8288da6d581SJan Schmidt */ 829f58d88b3SEdmund Nadolski static void __merge_refs(struct list_head *head, enum merge_mode mode) 8308da6d581SJan Schmidt { 8318e217858SGeliang Tang struct __prelim_ref *pos1; 8328da6d581SJan Schmidt 8338e217858SGeliang Tang list_for_each_entry(pos1, head, list) { 8348e217858SGeliang Tang struct __prelim_ref *pos2 = pos1, *tmp; 8358da6d581SJan Schmidt 8368e217858SGeliang Tang list_for_each_entry_safe_continue(pos2, tmp, head, list) { 8378f682f69SDave Jones struct __prelim_ref *ref1 = pos1, *ref2 = pos2; 8383ef5969cSAlexander Block struct extent_inode_elem *eie; 8398da6d581SJan Schmidt 840d5c88b73SJan Schmidt if (!ref_for_same_block(ref1, ref2)) 8418da6d581SJan Schmidt continue; 842f58d88b3SEdmund Nadolski if (mode == MERGE_IDENTICAL_KEYS) { 8438f682f69SDave Jones if (!ref1->parent && ref2->parent) 8448f682f69SDave Jones swap(ref1, ref2); 8458da6d581SJan Schmidt } else { 8468da6d581SJan Schmidt if (ref1->parent != ref2->parent) 8478da6d581SJan Schmidt continue; 8488da6d581SJan Schmidt } 8493ef5969cSAlexander Block 8503ef5969cSAlexander Block eie = ref1->inode_list; 8513ef5969cSAlexander Block while (eie && eie->next) 8523ef5969cSAlexander Block eie = eie->next; 8533ef5969cSAlexander Block if (eie) 8543ef5969cSAlexander Block eie->next = ref2->inode_list; 8553ef5969cSAlexander Block else 8563ef5969cSAlexander Block ref1->inode_list = ref2->inode_list; 8573ef5969cSAlexander Block ref1->count += ref2->count; 8583ef5969cSAlexander Block 8598da6d581SJan Schmidt list_del(&ref2->list); 860b9e9a6cbSWang Shilong kmem_cache_free(btrfs_prelim_ref_cache, ref2); 861d8422ba3SQu Wenruo cond_resched(); 8628da6d581SJan Schmidt } 8638da6d581SJan Schmidt 8648da6d581SJan Schmidt } 8658da6d581SJan Schmidt } 8668da6d581SJan Schmidt 8678da6d581SJan Schmidt /* 8688da6d581SJan Schmidt * add all currently queued delayed refs from this head whose seq nr is 8698da6d581SJan Schmidt * smaller or equal that seq to the list 8708da6d581SJan Schmidt */ 8718da6d581SJan Schmidt static int __add_delayed_refs(struct btrfs_delayed_ref_head *head, u64 seq, 872dc046b10SJosef Bacik struct list_head *prefs, u64 *total_refs, 873dc046b10SJosef Bacik u64 inum) 8748da6d581SJan Schmidt { 875c6fc2454SQu Wenruo struct btrfs_delayed_ref_node *node; 8768da6d581SJan Schmidt struct btrfs_delayed_extent_op *extent_op = head->extent_op; 877d5c88b73SJan Schmidt struct btrfs_key key; 878d5c88b73SJan Schmidt struct btrfs_key op_key = {0}; 8798da6d581SJan Schmidt int sgn; 880b1375d64SJan Schmidt int ret = 0; 8818da6d581SJan Schmidt 8828da6d581SJan Schmidt if (extent_op && extent_op->update_key) 883d5c88b73SJan Schmidt btrfs_disk_key_to_cpu(&op_key, &extent_op->key); 8848da6d581SJan Schmidt 885d7df2c79SJosef Bacik spin_lock(&head->lock); 886c6fc2454SQu Wenruo list_for_each_entry(node, &head->ref_list, list) { 8878da6d581SJan Schmidt if (node->seq > seq) 8888da6d581SJan Schmidt continue; 8898da6d581SJan Schmidt 8908da6d581SJan Schmidt switch (node->action) { 8918da6d581SJan Schmidt case BTRFS_ADD_DELAYED_EXTENT: 8928da6d581SJan Schmidt case BTRFS_UPDATE_DELAYED_HEAD: 8938da6d581SJan Schmidt WARN_ON(1); 8948da6d581SJan Schmidt continue; 8958da6d581SJan Schmidt case BTRFS_ADD_DELAYED_REF: 8968da6d581SJan Schmidt sgn = 1; 8978da6d581SJan Schmidt break; 8988da6d581SJan Schmidt case BTRFS_DROP_DELAYED_REF: 8998da6d581SJan Schmidt sgn = -1; 9008da6d581SJan Schmidt break; 9018da6d581SJan Schmidt default: 9028da6d581SJan Schmidt BUG_ON(1); 9038da6d581SJan Schmidt } 90444853868SJosef Bacik *total_refs += (node->ref_mod * sgn); 9058da6d581SJan Schmidt switch (node->type) { 9068da6d581SJan Schmidt case BTRFS_TREE_BLOCK_REF_KEY: { 9078da6d581SJan Schmidt struct btrfs_delayed_tree_ref *ref; 9088da6d581SJan Schmidt 9098da6d581SJan Schmidt ref = btrfs_delayed_node_to_tree_ref(node); 910d5c88b73SJan Schmidt ret = __add_prelim_ref(prefs, ref->root, &op_key, 9118da6d581SJan Schmidt ref->level + 1, 0, node->bytenr, 912742916b8SWang Shilong node->ref_mod * sgn, GFP_ATOMIC); 9138da6d581SJan Schmidt break; 9148da6d581SJan Schmidt } 9158da6d581SJan Schmidt case BTRFS_SHARED_BLOCK_REF_KEY: { 9168da6d581SJan Schmidt struct btrfs_delayed_tree_ref *ref; 9178da6d581SJan Schmidt 9188da6d581SJan Schmidt ref = btrfs_delayed_node_to_tree_ref(node); 919acdf898dSLiu Bo ret = __add_prelim_ref(prefs, 0, NULL, 9208da6d581SJan Schmidt ref->level + 1, ref->parent, 9218da6d581SJan Schmidt node->bytenr, 922742916b8SWang Shilong node->ref_mod * sgn, GFP_ATOMIC); 9238da6d581SJan Schmidt break; 9248da6d581SJan Schmidt } 9258da6d581SJan Schmidt case BTRFS_EXTENT_DATA_REF_KEY: { 9268da6d581SJan Schmidt struct btrfs_delayed_data_ref *ref; 9278da6d581SJan Schmidt ref = btrfs_delayed_node_to_data_ref(node); 9288da6d581SJan Schmidt 9298da6d581SJan Schmidt key.objectid = ref->objectid; 9308da6d581SJan Schmidt key.type = BTRFS_EXTENT_DATA_KEY; 9318da6d581SJan Schmidt key.offset = ref->offset; 932dc046b10SJosef Bacik 933dc046b10SJosef Bacik /* 934dc046b10SJosef Bacik * Found a inum that doesn't match our known inum, we 935dc046b10SJosef Bacik * know it's shared. 936dc046b10SJosef Bacik */ 937dc046b10SJosef Bacik if (inum && ref->objectid != inum) { 938dc046b10SJosef Bacik ret = BACKREF_FOUND_SHARED; 939dc046b10SJosef Bacik break; 940dc046b10SJosef Bacik } 941dc046b10SJosef Bacik 9428da6d581SJan Schmidt ret = __add_prelim_ref(prefs, ref->root, &key, 0, 0, 9438da6d581SJan Schmidt node->bytenr, 944742916b8SWang Shilong node->ref_mod * sgn, GFP_ATOMIC); 9458da6d581SJan Schmidt break; 9468da6d581SJan Schmidt } 9478da6d581SJan Schmidt case BTRFS_SHARED_DATA_REF_KEY: { 9488da6d581SJan Schmidt struct btrfs_delayed_data_ref *ref; 9498da6d581SJan Schmidt 9508da6d581SJan Schmidt ref = btrfs_delayed_node_to_data_ref(node); 951acdf898dSLiu Bo ret = __add_prelim_ref(prefs, 0, NULL, 0, 9528da6d581SJan Schmidt ref->parent, node->bytenr, 953742916b8SWang Shilong node->ref_mod * sgn, GFP_ATOMIC); 9548da6d581SJan Schmidt break; 9558da6d581SJan Schmidt } 9568da6d581SJan Schmidt default: 9578da6d581SJan Schmidt WARN_ON(1); 9588da6d581SJan Schmidt } 9591149ab6bSWang Shilong if (ret) 960d7df2c79SJosef Bacik break; 9618da6d581SJan Schmidt } 962d7df2c79SJosef Bacik spin_unlock(&head->lock); 963d7df2c79SJosef Bacik return ret; 9648da6d581SJan Schmidt } 9658da6d581SJan Schmidt 9668da6d581SJan Schmidt /* 9678da6d581SJan Schmidt * add all inline backrefs for bytenr to the list 9688da6d581SJan Schmidt */ 969eeac44cbSDavid Sterba static int __add_inline_refs(struct btrfs_path *path, u64 bytenr, 97044853868SJosef Bacik int *info_level, struct list_head *prefs, 971afce772eSLu Fengqi struct ref_root *ref_tree, 972dc046b10SJosef Bacik u64 *total_refs, u64 inum) 9738da6d581SJan Schmidt { 974b1375d64SJan Schmidt int ret = 0; 9758da6d581SJan Schmidt int slot; 9768da6d581SJan Schmidt struct extent_buffer *leaf; 9778da6d581SJan Schmidt struct btrfs_key key; 978261c84b6SJosef Bacik struct btrfs_key found_key; 9798da6d581SJan Schmidt unsigned long ptr; 9808da6d581SJan Schmidt unsigned long end; 9818da6d581SJan Schmidt struct btrfs_extent_item *ei; 9828da6d581SJan Schmidt u64 flags; 9838da6d581SJan Schmidt u64 item_size; 9848da6d581SJan Schmidt 9858da6d581SJan Schmidt /* 9868da6d581SJan Schmidt * enumerate all inline refs 9878da6d581SJan Schmidt */ 9888da6d581SJan Schmidt leaf = path->nodes[0]; 989dadcaf78SJan Schmidt slot = path->slots[0]; 9908da6d581SJan Schmidt 9918da6d581SJan Schmidt item_size = btrfs_item_size_nr(leaf, slot); 9928da6d581SJan Schmidt BUG_ON(item_size < sizeof(*ei)); 9938da6d581SJan Schmidt 9948da6d581SJan Schmidt ei = btrfs_item_ptr(leaf, slot, struct btrfs_extent_item); 9958da6d581SJan Schmidt flags = btrfs_extent_flags(leaf, ei); 99644853868SJosef Bacik *total_refs += btrfs_extent_refs(leaf, ei); 997261c84b6SJosef Bacik btrfs_item_key_to_cpu(leaf, &found_key, slot); 9988da6d581SJan Schmidt 9998da6d581SJan Schmidt ptr = (unsigned long)(ei + 1); 10008da6d581SJan Schmidt end = (unsigned long)ei + item_size; 10018da6d581SJan Schmidt 1002261c84b6SJosef Bacik if (found_key.type == BTRFS_EXTENT_ITEM_KEY && 1003261c84b6SJosef Bacik flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) { 10048da6d581SJan Schmidt struct btrfs_tree_block_info *info; 10058da6d581SJan Schmidt 10068da6d581SJan Schmidt info = (struct btrfs_tree_block_info *)ptr; 10078da6d581SJan Schmidt *info_level = btrfs_tree_block_level(leaf, info); 10088da6d581SJan Schmidt ptr += sizeof(struct btrfs_tree_block_info); 10098da6d581SJan Schmidt BUG_ON(ptr > end); 1010261c84b6SJosef Bacik } else if (found_key.type == BTRFS_METADATA_ITEM_KEY) { 1011261c84b6SJosef Bacik *info_level = found_key.offset; 10128da6d581SJan Schmidt } else { 10138da6d581SJan Schmidt BUG_ON(!(flags & BTRFS_EXTENT_FLAG_DATA)); 10148da6d581SJan Schmidt } 10158da6d581SJan Schmidt 10168da6d581SJan Schmidt while (ptr < end) { 10178da6d581SJan Schmidt struct btrfs_extent_inline_ref *iref; 10188da6d581SJan Schmidt u64 offset; 10198da6d581SJan Schmidt int type; 10208da6d581SJan Schmidt 10218da6d581SJan Schmidt iref = (struct btrfs_extent_inline_ref *)ptr; 10228da6d581SJan Schmidt type = btrfs_extent_inline_ref_type(leaf, iref); 10238da6d581SJan Schmidt offset = btrfs_extent_inline_ref_offset(leaf, iref); 10248da6d581SJan Schmidt 10258da6d581SJan Schmidt switch (type) { 10268da6d581SJan Schmidt case BTRFS_SHARED_BLOCK_REF_KEY: 1027d5c88b73SJan Schmidt ret = __add_prelim_ref(prefs, 0, NULL, 10288da6d581SJan Schmidt *info_level + 1, offset, 1029742916b8SWang Shilong bytenr, 1, GFP_NOFS); 10308da6d581SJan Schmidt break; 10318da6d581SJan Schmidt case BTRFS_SHARED_DATA_REF_KEY: { 10328da6d581SJan Schmidt struct btrfs_shared_data_ref *sdref; 10338da6d581SJan Schmidt int count; 10348da6d581SJan Schmidt 10358da6d581SJan Schmidt sdref = (struct btrfs_shared_data_ref *)(iref + 1); 10368da6d581SJan Schmidt count = btrfs_shared_data_ref_count(leaf, sdref); 10378da6d581SJan Schmidt ret = __add_prelim_ref(prefs, 0, NULL, 0, offset, 1038742916b8SWang Shilong bytenr, count, GFP_NOFS); 1039afce772eSLu Fengqi if (ref_tree) { 1040afce772eSLu Fengqi if (!ret) 1041afce772eSLu Fengqi ret = ref_tree_add(ref_tree, 0, 0, 0, 1042afce772eSLu Fengqi bytenr, count); 1043afce772eSLu Fengqi if (!ret && ref_tree->unique_refs > 1) 1044afce772eSLu Fengqi ret = BACKREF_FOUND_SHARED; 1045afce772eSLu Fengqi } 10468da6d581SJan Schmidt break; 10478da6d581SJan Schmidt } 10488da6d581SJan Schmidt case BTRFS_TREE_BLOCK_REF_KEY: 1049d5c88b73SJan Schmidt ret = __add_prelim_ref(prefs, offset, NULL, 1050d5c88b73SJan Schmidt *info_level + 1, 0, 1051742916b8SWang Shilong bytenr, 1, GFP_NOFS); 10528da6d581SJan Schmidt break; 10538da6d581SJan Schmidt case BTRFS_EXTENT_DATA_REF_KEY: { 10548da6d581SJan Schmidt struct btrfs_extent_data_ref *dref; 10558da6d581SJan Schmidt int count; 10568da6d581SJan Schmidt u64 root; 10578da6d581SJan Schmidt 10588da6d581SJan Schmidt dref = (struct btrfs_extent_data_ref *)(&iref->offset); 10598da6d581SJan Schmidt count = btrfs_extent_data_ref_count(leaf, dref); 10608da6d581SJan Schmidt key.objectid = btrfs_extent_data_ref_objectid(leaf, 10618da6d581SJan Schmidt dref); 10628da6d581SJan Schmidt key.type = BTRFS_EXTENT_DATA_KEY; 10638da6d581SJan Schmidt key.offset = btrfs_extent_data_ref_offset(leaf, dref); 1064dc046b10SJosef Bacik 1065dc046b10SJosef Bacik if (inum && key.objectid != inum) { 1066dc046b10SJosef Bacik ret = BACKREF_FOUND_SHARED; 1067dc046b10SJosef Bacik break; 1068dc046b10SJosef Bacik } 1069dc046b10SJosef Bacik 10708da6d581SJan Schmidt root = btrfs_extent_data_ref_root(leaf, dref); 1071d5c88b73SJan Schmidt ret = __add_prelim_ref(prefs, root, &key, 0, 0, 1072742916b8SWang Shilong bytenr, count, GFP_NOFS); 1073afce772eSLu Fengqi if (ref_tree) { 1074afce772eSLu Fengqi if (!ret) 1075afce772eSLu Fengqi ret = ref_tree_add(ref_tree, root, 1076afce772eSLu Fengqi key.objectid, 1077afce772eSLu Fengqi key.offset, 0, 1078afce772eSLu Fengqi count); 1079afce772eSLu Fengqi if (!ret && ref_tree->unique_refs > 1) 1080afce772eSLu Fengqi ret = BACKREF_FOUND_SHARED; 1081afce772eSLu Fengqi } 10828da6d581SJan Schmidt break; 10838da6d581SJan Schmidt } 10848da6d581SJan Schmidt default: 10858da6d581SJan Schmidt WARN_ON(1); 10868da6d581SJan Schmidt } 10871149ab6bSWang Shilong if (ret) 10881149ab6bSWang Shilong return ret; 10898da6d581SJan Schmidt ptr += btrfs_extent_inline_ref_size(type); 10908da6d581SJan Schmidt } 10918da6d581SJan Schmidt 10928da6d581SJan Schmidt return 0; 10938da6d581SJan Schmidt } 10948da6d581SJan Schmidt 10958da6d581SJan Schmidt /* 10968da6d581SJan Schmidt * add all non-inline backrefs for bytenr to the list 10978da6d581SJan Schmidt */ 10988da6d581SJan Schmidt static int __add_keyed_refs(struct btrfs_fs_info *fs_info, 10998da6d581SJan Schmidt struct btrfs_path *path, u64 bytenr, 1100afce772eSLu Fengqi int info_level, struct list_head *prefs, 1101afce772eSLu Fengqi struct ref_root *ref_tree, u64 inum) 11028da6d581SJan Schmidt { 11038da6d581SJan Schmidt struct btrfs_root *extent_root = fs_info->extent_root; 11048da6d581SJan Schmidt int ret; 11058da6d581SJan Schmidt int slot; 11068da6d581SJan Schmidt struct extent_buffer *leaf; 11078da6d581SJan Schmidt struct btrfs_key key; 11088da6d581SJan Schmidt 11098da6d581SJan Schmidt while (1) { 11108da6d581SJan Schmidt ret = btrfs_next_item(extent_root, path); 11118da6d581SJan Schmidt if (ret < 0) 11128da6d581SJan Schmidt break; 11138da6d581SJan Schmidt if (ret) { 11148da6d581SJan Schmidt ret = 0; 11158da6d581SJan Schmidt break; 11168da6d581SJan Schmidt } 11178da6d581SJan Schmidt 11188da6d581SJan Schmidt slot = path->slots[0]; 11198da6d581SJan Schmidt leaf = path->nodes[0]; 11208da6d581SJan Schmidt btrfs_item_key_to_cpu(leaf, &key, slot); 11218da6d581SJan Schmidt 11228da6d581SJan Schmidt if (key.objectid != bytenr) 11238da6d581SJan Schmidt break; 11248da6d581SJan Schmidt if (key.type < BTRFS_TREE_BLOCK_REF_KEY) 11258da6d581SJan Schmidt continue; 11268da6d581SJan Schmidt if (key.type > BTRFS_SHARED_DATA_REF_KEY) 11278da6d581SJan Schmidt break; 11288da6d581SJan Schmidt 11298da6d581SJan Schmidt switch (key.type) { 11308da6d581SJan Schmidt case BTRFS_SHARED_BLOCK_REF_KEY: 1131d5c88b73SJan Schmidt ret = __add_prelim_ref(prefs, 0, NULL, 11328da6d581SJan Schmidt info_level + 1, key.offset, 1133742916b8SWang Shilong bytenr, 1, GFP_NOFS); 11348da6d581SJan Schmidt break; 11358da6d581SJan Schmidt case BTRFS_SHARED_DATA_REF_KEY: { 11368da6d581SJan Schmidt struct btrfs_shared_data_ref *sdref; 11378da6d581SJan Schmidt int count; 11388da6d581SJan Schmidt 11398da6d581SJan Schmidt sdref = btrfs_item_ptr(leaf, slot, 11408da6d581SJan Schmidt struct btrfs_shared_data_ref); 11418da6d581SJan Schmidt count = btrfs_shared_data_ref_count(leaf, sdref); 11428da6d581SJan Schmidt ret = __add_prelim_ref(prefs, 0, NULL, 0, key.offset, 1143742916b8SWang Shilong bytenr, count, GFP_NOFS); 1144afce772eSLu Fengqi if (ref_tree) { 1145afce772eSLu Fengqi if (!ret) 1146afce772eSLu Fengqi ret = ref_tree_add(ref_tree, 0, 0, 0, 1147afce772eSLu Fengqi bytenr, count); 1148afce772eSLu Fengqi if (!ret && ref_tree->unique_refs > 1) 1149afce772eSLu Fengqi ret = BACKREF_FOUND_SHARED; 1150afce772eSLu Fengqi } 11518da6d581SJan Schmidt break; 11528da6d581SJan Schmidt } 11538da6d581SJan Schmidt case BTRFS_TREE_BLOCK_REF_KEY: 1154d5c88b73SJan Schmidt ret = __add_prelim_ref(prefs, key.offset, NULL, 1155d5c88b73SJan Schmidt info_level + 1, 0, 1156742916b8SWang Shilong bytenr, 1, GFP_NOFS); 11578da6d581SJan Schmidt break; 11588da6d581SJan Schmidt case BTRFS_EXTENT_DATA_REF_KEY: { 11598da6d581SJan Schmidt struct btrfs_extent_data_ref *dref; 11608da6d581SJan Schmidt int count; 11618da6d581SJan Schmidt u64 root; 11628da6d581SJan Schmidt 11638da6d581SJan Schmidt dref = btrfs_item_ptr(leaf, slot, 11648da6d581SJan Schmidt struct btrfs_extent_data_ref); 11658da6d581SJan Schmidt count = btrfs_extent_data_ref_count(leaf, dref); 11668da6d581SJan Schmidt key.objectid = btrfs_extent_data_ref_objectid(leaf, 11678da6d581SJan Schmidt dref); 11688da6d581SJan Schmidt key.type = BTRFS_EXTENT_DATA_KEY; 11698da6d581SJan Schmidt key.offset = btrfs_extent_data_ref_offset(leaf, dref); 1170dc046b10SJosef Bacik 1171dc046b10SJosef Bacik if (inum && key.objectid != inum) { 1172dc046b10SJosef Bacik ret = BACKREF_FOUND_SHARED; 1173dc046b10SJosef Bacik break; 1174dc046b10SJosef Bacik } 1175dc046b10SJosef Bacik 11768da6d581SJan Schmidt root = btrfs_extent_data_ref_root(leaf, dref); 11778da6d581SJan Schmidt ret = __add_prelim_ref(prefs, root, &key, 0, 0, 1178742916b8SWang Shilong bytenr, count, GFP_NOFS); 1179afce772eSLu Fengqi if (ref_tree) { 1180afce772eSLu Fengqi if (!ret) 1181afce772eSLu Fengqi ret = ref_tree_add(ref_tree, root, 1182afce772eSLu Fengqi key.objectid, 1183afce772eSLu Fengqi key.offset, 0, 1184afce772eSLu Fengqi count); 1185afce772eSLu Fengqi if (!ret && ref_tree->unique_refs > 1) 1186afce772eSLu Fengqi ret = BACKREF_FOUND_SHARED; 1187afce772eSLu Fengqi } 11888da6d581SJan Schmidt break; 11898da6d581SJan Schmidt } 11908da6d581SJan Schmidt default: 11918da6d581SJan Schmidt WARN_ON(1); 11928da6d581SJan Schmidt } 11931149ab6bSWang Shilong if (ret) 11941149ab6bSWang Shilong return ret; 11951149ab6bSWang Shilong 11968da6d581SJan Schmidt } 11978da6d581SJan Schmidt 11988da6d581SJan Schmidt return ret; 11998da6d581SJan Schmidt } 12008da6d581SJan Schmidt 12018da6d581SJan Schmidt /* 12028da6d581SJan Schmidt * this adds all existing backrefs (inline backrefs, backrefs and delayed 12038da6d581SJan Schmidt * refs) for the given bytenr to the refs list, merges duplicates and resolves 12048da6d581SJan Schmidt * indirect refs to their parent bytenr. 12058da6d581SJan Schmidt * When roots are found, they're added to the roots list 12068da6d581SJan Schmidt * 12072c2ed5aaSMark Fasheh * NOTE: This can return values > 0 12082c2ed5aaSMark Fasheh * 1209de47c9d3SEdmund Nadolski * If time_seq is set to SEQ_LAST, it will not search delayed_refs, and behave 121021633fc6SQu Wenruo * much like trans == NULL case, the difference only lies in it will not 121121633fc6SQu Wenruo * commit root. 121221633fc6SQu Wenruo * The special case is for qgroup to search roots in commit_transaction(). 121321633fc6SQu Wenruo * 1214afce772eSLu Fengqi * If check_shared is set to 1, any extent has more than one ref item, will 1215afce772eSLu Fengqi * be returned BACKREF_FOUND_SHARED immediately. 1216afce772eSLu Fengqi * 12178da6d581SJan Schmidt * FIXME some caching might speed things up 12188da6d581SJan Schmidt */ 12198da6d581SJan Schmidt static int find_parent_nodes(struct btrfs_trans_handle *trans, 12208da6d581SJan Schmidt struct btrfs_fs_info *fs_info, u64 bytenr, 1221097b8a7cSJan Schmidt u64 time_seq, struct ulist *refs, 1222dc046b10SJosef Bacik struct ulist *roots, const u64 *extent_item_pos, 1223afce772eSLu Fengqi u64 root_objectid, u64 inum, int check_shared) 12248da6d581SJan Schmidt { 12258da6d581SJan Schmidt struct btrfs_key key; 12268da6d581SJan Schmidt struct btrfs_path *path; 12278da6d581SJan Schmidt struct btrfs_delayed_ref_root *delayed_refs = NULL; 1228d3b01064SLi Zefan struct btrfs_delayed_ref_head *head; 12298da6d581SJan Schmidt int info_level = 0; 12308da6d581SJan Schmidt int ret; 12318da6d581SJan Schmidt struct list_head prefs_delayed; 12328da6d581SJan Schmidt struct list_head prefs; 12338da6d581SJan Schmidt struct __prelim_ref *ref; 1234f05c4746SWang Shilong struct extent_inode_elem *eie = NULL; 1235afce772eSLu Fengqi struct ref_root *ref_tree = NULL; 123644853868SJosef Bacik u64 total_refs = 0; 12378da6d581SJan Schmidt 12388da6d581SJan Schmidt INIT_LIST_HEAD(&prefs); 12398da6d581SJan Schmidt INIT_LIST_HEAD(&prefs_delayed); 12408da6d581SJan Schmidt 12418da6d581SJan Schmidt key.objectid = bytenr; 12428da6d581SJan Schmidt key.offset = (u64)-1; 1243261c84b6SJosef Bacik if (btrfs_fs_incompat(fs_info, SKINNY_METADATA)) 1244261c84b6SJosef Bacik key.type = BTRFS_METADATA_ITEM_KEY; 1245261c84b6SJosef Bacik else 1246261c84b6SJosef Bacik key.type = BTRFS_EXTENT_ITEM_KEY; 12478da6d581SJan Schmidt 12488da6d581SJan Schmidt path = btrfs_alloc_path(); 12498da6d581SJan Schmidt if (!path) 12508da6d581SJan Schmidt return -ENOMEM; 1251e84752d4SWang Shilong if (!trans) { 1252da61d31aSJosef Bacik path->search_commit_root = 1; 1253e84752d4SWang Shilong path->skip_locking = 1; 1254e84752d4SWang Shilong } 12558da6d581SJan Schmidt 1256de47c9d3SEdmund Nadolski if (time_seq == SEQ_LAST) 125721633fc6SQu Wenruo path->skip_locking = 1; 125821633fc6SQu Wenruo 12598da6d581SJan Schmidt /* 12608da6d581SJan Schmidt * grab both a lock on the path and a lock on the delayed ref head. 12618da6d581SJan Schmidt * We need both to get a consistent picture of how the refs look 12628da6d581SJan Schmidt * at a specified point in time 12638da6d581SJan Schmidt */ 12648da6d581SJan Schmidt again: 1265d3b01064SLi Zefan head = NULL; 1266d3b01064SLi Zefan 1267afce772eSLu Fengqi if (check_shared) { 1268afce772eSLu Fengqi if (!ref_tree) { 1269afce772eSLu Fengqi ref_tree = ref_root_alloc(); 1270afce772eSLu Fengqi if (!ref_tree) { 1271afce772eSLu Fengqi ret = -ENOMEM; 1272afce772eSLu Fengqi goto out; 1273afce772eSLu Fengqi } 1274afce772eSLu Fengqi } else { 1275afce772eSLu Fengqi ref_root_fini(ref_tree); 1276afce772eSLu Fengqi } 1277afce772eSLu Fengqi } 1278afce772eSLu Fengqi 12798da6d581SJan Schmidt ret = btrfs_search_slot(trans, fs_info->extent_root, &key, path, 0, 0); 12808da6d581SJan Schmidt if (ret < 0) 12818da6d581SJan Schmidt goto out; 12828da6d581SJan Schmidt BUG_ON(ret == 0); 12838da6d581SJan Schmidt 1284faa2dbf0SJosef Bacik #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS 128521633fc6SQu Wenruo if (trans && likely(trans->type != __TRANS_DUMMY) && 1286de47c9d3SEdmund Nadolski time_seq != SEQ_LAST) { 1287faa2dbf0SJosef Bacik #else 1288de47c9d3SEdmund Nadolski if (trans && time_seq != SEQ_LAST) { 1289faa2dbf0SJosef Bacik #endif 12908da6d581SJan Schmidt /* 12917a3ae2f8SJan Schmidt * look if there are updates for this ref queued and lock the 12927a3ae2f8SJan Schmidt * head 12938da6d581SJan Schmidt */ 12948da6d581SJan Schmidt delayed_refs = &trans->transaction->delayed_refs; 12958da6d581SJan Schmidt spin_lock(&delayed_refs->lock); 1296f72ad18eSLiu Bo head = btrfs_find_delayed_ref_head(delayed_refs, bytenr); 12978da6d581SJan Schmidt if (head) { 12988da6d581SJan Schmidt if (!mutex_trylock(&head->mutex)) { 12996df8cdf5SElena Reshetova refcount_inc(&head->node.refs); 13008da6d581SJan Schmidt spin_unlock(&delayed_refs->lock); 13018da6d581SJan Schmidt 13028da6d581SJan Schmidt btrfs_release_path(path); 13038da6d581SJan Schmidt 13048da6d581SJan Schmidt /* 13058da6d581SJan Schmidt * Mutex was contended, block until it's 13068da6d581SJan Schmidt * released and try again 13078da6d581SJan Schmidt */ 13088da6d581SJan Schmidt mutex_lock(&head->mutex); 13098da6d581SJan Schmidt mutex_unlock(&head->mutex); 13108da6d581SJan Schmidt btrfs_put_delayed_ref(&head->node); 13118da6d581SJan Schmidt goto again; 13128da6d581SJan Schmidt } 1313d7df2c79SJosef Bacik spin_unlock(&delayed_refs->lock); 1314097b8a7cSJan Schmidt ret = __add_delayed_refs(head, time_seq, 1315dc046b10SJosef Bacik &prefs_delayed, &total_refs, 1316dc046b10SJosef Bacik inum); 1317155725c9SJan Schmidt mutex_unlock(&head->mutex); 1318d7df2c79SJosef Bacik if (ret) 13198da6d581SJan Schmidt goto out; 1320d7df2c79SJosef Bacik } else { 13218da6d581SJan Schmidt spin_unlock(&delayed_refs->lock); 13227a3ae2f8SJan Schmidt } 1323afce772eSLu Fengqi 1324afce772eSLu Fengqi if (check_shared && !list_empty(&prefs_delayed)) { 1325afce772eSLu Fengqi /* 1326afce772eSLu Fengqi * Add all delay_ref to the ref_tree and check if there 1327afce772eSLu Fengqi * are multiple ref items added. 1328afce772eSLu Fengqi */ 1329afce772eSLu Fengqi list_for_each_entry(ref, &prefs_delayed, list) { 1330afce772eSLu Fengqi if (ref->key_for_search.type) { 1331afce772eSLu Fengqi ret = ref_tree_add(ref_tree, 1332afce772eSLu Fengqi ref->root_id, 1333afce772eSLu Fengqi ref->key_for_search.objectid, 1334afce772eSLu Fengqi ref->key_for_search.offset, 1335afce772eSLu Fengqi 0, ref->count); 1336afce772eSLu Fengqi if (ret) 1337afce772eSLu Fengqi goto out; 1338afce772eSLu Fengqi } else { 1339afce772eSLu Fengqi ret = ref_tree_add(ref_tree, 0, 0, 0, 1340afce772eSLu Fengqi ref->parent, ref->count); 1341afce772eSLu Fengqi if (ret) 1342afce772eSLu Fengqi goto out; 1343afce772eSLu Fengqi } 1344afce772eSLu Fengqi 1345afce772eSLu Fengqi } 1346afce772eSLu Fengqi 1347afce772eSLu Fengqi if (ref_tree->unique_refs > 1) { 1348afce772eSLu Fengqi ret = BACKREF_FOUND_SHARED; 1349afce772eSLu Fengqi goto out; 1350afce772eSLu Fengqi } 1351afce772eSLu Fengqi 1352afce772eSLu Fengqi } 1353d7df2c79SJosef Bacik } 13548da6d581SJan Schmidt 13558da6d581SJan Schmidt if (path->slots[0]) { 13568da6d581SJan Schmidt struct extent_buffer *leaf; 13578da6d581SJan Schmidt int slot; 13588da6d581SJan Schmidt 1359dadcaf78SJan Schmidt path->slots[0]--; 13608da6d581SJan Schmidt leaf = path->nodes[0]; 1361dadcaf78SJan Schmidt slot = path->slots[0]; 13628da6d581SJan Schmidt btrfs_item_key_to_cpu(leaf, &key, slot); 13638da6d581SJan Schmidt if (key.objectid == bytenr && 1364261c84b6SJosef Bacik (key.type == BTRFS_EXTENT_ITEM_KEY || 1365261c84b6SJosef Bacik key.type == BTRFS_METADATA_ITEM_KEY)) { 1366eeac44cbSDavid Sterba ret = __add_inline_refs(path, bytenr, 136744853868SJosef Bacik &info_level, &prefs, 1368afce772eSLu Fengqi ref_tree, &total_refs, 1369afce772eSLu Fengqi inum); 13708da6d581SJan Schmidt if (ret) 13718da6d581SJan Schmidt goto out; 1372d5c88b73SJan Schmidt ret = __add_keyed_refs(fs_info, path, bytenr, 1373afce772eSLu Fengqi info_level, &prefs, 1374afce772eSLu Fengqi ref_tree, inum); 13758da6d581SJan Schmidt if (ret) 13768da6d581SJan Schmidt goto out; 13778da6d581SJan Schmidt } 13788da6d581SJan Schmidt } 13798da6d581SJan Schmidt btrfs_release_path(path); 13808da6d581SJan Schmidt 13818da6d581SJan Schmidt list_splice_init(&prefs_delayed, &prefs); 13828da6d581SJan Schmidt 1383d5c88b73SJan Schmidt ret = __add_missing_keys(fs_info, &prefs); 1384d5c88b73SJan Schmidt if (ret) 1385d5c88b73SJan Schmidt goto out; 1386d5c88b73SJan Schmidt 1387f58d88b3SEdmund Nadolski __merge_refs(&prefs, MERGE_IDENTICAL_KEYS); 13888da6d581SJan Schmidt 1389da61d31aSJosef Bacik ret = __resolve_indirect_refs(fs_info, path, time_seq, &prefs, 1390dc046b10SJosef Bacik extent_item_pos, total_refs, 1391dc046b10SJosef Bacik root_objectid); 13928da6d581SJan Schmidt if (ret) 13938da6d581SJan Schmidt goto out; 13948da6d581SJan Schmidt 1395f58d88b3SEdmund Nadolski __merge_refs(&prefs, MERGE_IDENTICAL_PARENTS); 13968da6d581SJan Schmidt 13978da6d581SJan Schmidt while (!list_empty(&prefs)) { 13988da6d581SJan Schmidt ref = list_first_entry(&prefs, struct __prelim_ref, list); 13996c1500f2SJulia Lawall WARN_ON(ref->count < 0); 140098cfee21SWang Shilong if (roots && ref->count && ref->root_id && ref->parent == 0) { 1401dc046b10SJosef Bacik if (root_objectid && ref->root_id != root_objectid) { 1402dc046b10SJosef Bacik ret = BACKREF_FOUND_SHARED; 1403dc046b10SJosef Bacik goto out; 1404dc046b10SJosef Bacik } 1405dc046b10SJosef Bacik 14068da6d581SJan Schmidt /* no parent == root of tree */ 14078da6d581SJan Schmidt ret = ulist_add(roots, ref->root_id, 0, GFP_NOFS); 1408f1723939SWang Shilong if (ret < 0) 1409f1723939SWang Shilong goto out; 14108da6d581SJan Schmidt } 14118da6d581SJan Schmidt if (ref->count && ref->parent) { 14128a56457fSJosef Bacik if (extent_item_pos && !ref->inode_list && 14138a56457fSJosef Bacik ref->level == 0) { 1414976b1908SJan Schmidt struct extent_buffer *eb; 1415707e8a07SDavid Sterba 14162ff7e61eSJeff Mahoney eb = read_tree_block(fs_info, ref->parent, 0); 141764c043deSLiu Bo if (IS_ERR(eb)) { 141864c043deSLiu Bo ret = PTR_ERR(eb); 141964c043deSLiu Bo goto out; 142064c043deSLiu Bo } else if (!extent_buffer_uptodate(eb)) { 1421416bc658SJosef Bacik free_extent_buffer(eb); 1422c16c2e2eSWang Shilong ret = -EIO; 1423c16c2e2eSWang Shilong goto out; 1424416bc658SJosef Bacik } 14256f7ff6d7SFilipe Manana btrfs_tree_read_lock(eb); 14266f7ff6d7SFilipe Manana btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK); 1427976b1908SJan Schmidt ret = find_extent_in_eb(eb, bytenr, 1428976b1908SJan Schmidt *extent_item_pos, &eie); 14296f7ff6d7SFilipe Manana btrfs_tree_read_unlock_blocking(eb); 1430976b1908SJan Schmidt free_extent_buffer(eb); 1431f5929cd8SFilipe David Borba Manana if (ret < 0) 1432f5929cd8SFilipe David Borba Manana goto out; 1433f5929cd8SFilipe David Borba Manana ref->inode_list = eie; 1434976b1908SJan Schmidt } 14354eb1f66dSTakashi Iwai ret = ulist_add_merge_ptr(refs, ref->parent, 14364eb1f66dSTakashi Iwai ref->inode_list, 14374eb1f66dSTakashi Iwai (void **)&eie, GFP_NOFS); 1438f1723939SWang Shilong if (ret < 0) 1439f1723939SWang Shilong goto out; 14403301958bSJan Schmidt if (!ret && extent_item_pos) { 14413301958bSJan Schmidt /* 14423301958bSJan Schmidt * we've recorded that parent, so we must extend 14433301958bSJan Schmidt * its inode list here 14443301958bSJan Schmidt */ 14453301958bSJan Schmidt BUG_ON(!eie); 14463301958bSJan Schmidt while (eie->next) 14473301958bSJan Schmidt eie = eie->next; 14483301958bSJan Schmidt eie->next = ref->inode_list; 14493301958bSJan Schmidt } 1450f05c4746SWang Shilong eie = NULL; 14518da6d581SJan Schmidt } 1452a4fdb61eSWang Shilong list_del(&ref->list); 1453b9e9a6cbSWang Shilong kmem_cache_free(btrfs_prelim_ref_cache, ref); 14548da6d581SJan Schmidt } 14558da6d581SJan Schmidt 14568da6d581SJan Schmidt out: 14578da6d581SJan Schmidt btrfs_free_path(path); 1458afce772eSLu Fengqi ref_root_free(ref_tree); 14598da6d581SJan Schmidt while (!list_empty(&prefs)) { 14608da6d581SJan Schmidt ref = list_first_entry(&prefs, struct __prelim_ref, list); 14618da6d581SJan Schmidt list_del(&ref->list); 1462b9e9a6cbSWang Shilong kmem_cache_free(btrfs_prelim_ref_cache, ref); 14638da6d581SJan Schmidt } 14648da6d581SJan Schmidt while (!list_empty(&prefs_delayed)) { 14658da6d581SJan Schmidt ref = list_first_entry(&prefs_delayed, struct __prelim_ref, 14668da6d581SJan Schmidt list); 14678da6d581SJan Schmidt list_del(&ref->list); 1468b9e9a6cbSWang Shilong kmem_cache_free(btrfs_prelim_ref_cache, ref); 14698da6d581SJan Schmidt } 1470f05c4746SWang Shilong if (ret < 0) 1471f05c4746SWang Shilong free_inode_elem_list(eie); 14728da6d581SJan Schmidt return ret; 14738da6d581SJan Schmidt } 14748da6d581SJan Schmidt 1475976b1908SJan Schmidt static void free_leaf_list(struct ulist *blocks) 1476976b1908SJan Schmidt { 1477976b1908SJan Schmidt struct ulist_node *node = NULL; 1478976b1908SJan Schmidt struct extent_inode_elem *eie; 1479976b1908SJan Schmidt struct ulist_iterator uiter; 1480976b1908SJan Schmidt 1481976b1908SJan Schmidt ULIST_ITER_INIT(&uiter); 1482976b1908SJan Schmidt while ((node = ulist_next(blocks, &uiter))) { 1483976b1908SJan Schmidt if (!node->aux) 1484976b1908SJan Schmidt continue; 1485*4dae077aSJeff Mahoney eie = unode_aux_to_inode_list(node); 1486f05c4746SWang Shilong free_inode_elem_list(eie); 1487976b1908SJan Schmidt node->aux = 0; 1488976b1908SJan Schmidt } 1489976b1908SJan Schmidt 1490976b1908SJan Schmidt ulist_free(blocks); 1491976b1908SJan Schmidt } 1492976b1908SJan Schmidt 14938da6d581SJan Schmidt /* 14948da6d581SJan Schmidt * Finds all leafs with a reference to the specified combination of bytenr and 14958da6d581SJan Schmidt * offset. key_list_head will point to a list of corresponding keys (caller must 14968da6d581SJan Schmidt * free each list element). The leafs will be stored in the leafs ulist, which 14978da6d581SJan Schmidt * must be freed with ulist_free. 14988da6d581SJan Schmidt * 14998da6d581SJan Schmidt * returns 0 on success, <0 on error 15008da6d581SJan Schmidt */ 15018da6d581SJan Schmidt static int btrfs_find_all_leafs(struct btrfs_trans_handle *trans, 15028da6d581SJan Schmidt struct btrfs_fs_info *fs_info, u64 bytenr, 1503097b8a7cSJan Schmidt u64 time_seq, struct ulist **leafs, 1504976b1908SJan Schmidt const u64 *extent_item_pos) 15058da6d581SJan Schmidt { 15068da6d581SJan Schmidt int ret; 15078da6d581SJan Schmidt 15088da6d581SJan Schmidt *leafs = ulist_alloc(GFP_NOFS); 150998cfee21SWang Shilong if (!*leafs) 15108da6d581SJan Schmidt return -ENOMEM; 15118da6d581SJan Schmidt 1512afce772eSLu Fengqi ret = find_parent_nodes(trans, fs_info, bytenr, time_seq, 1513afce772eSLu Fengqi *leafs, NULL, extent_item_pos, 0, 0, 0); 15148da6d581SJan Schmidt if (ret < 0 && ret != -ENOENT) { 1515976b1908SJan Schmidt free_leaf_list(*leafs); 15168da6d581SJan Schmidt return ret; 15178da6d581SJan Schmidt } 15188da6d581SJan Schmidt 15198da6d581SJan Schmidt return 0; 15208da6d581SJan Schmidt } 15218da6d581SJan Schmidt 15228da6d581SJan Schmidt /* 15238da6d581SJan Schmidt * walk all backrefs for a given extent to find all roots that reference this 15248da6d581SJan Schmidt * extent. Walking a backref means finding all extents that reference this 15258da6d581SJan Schmidt * extent and in turn walk the backrefs of those, too. Naturally this is a 15268da6d581SJan Schmidt * recursive process, but here it is implemented in an iterative fashion: We 15278da6d581SJan Schmidt * find all referencing extents for the extent in question and put them on a 15288da6d581SJan Schmidt * list. In turn, we find all referencing extents for those, further appending 15298da6d581SJan Schmidt * to the list. The way we iterate the list allows adding more elements after 15308da6d581SJan Schmidt * the current while iterating. The process stops when we reach the end of the 15318da6d581SJan Schmidt * list. Found roots are added to the roots list. 15328da6d581SJan Schmidt * 15338da6d581SJan Schmidt * returns 0 on success, < 0 on error. 15348da6d581SJan Schmidt */ 15359e351cc8SJosef Bacik static int __btrfs_find_all_roots(struct btrfs_trans_handle *trans, 15368da6d581SJan Schmidt struct btrfs_fs_info *fs_info, u64 bytenr, 1537097b8a7cSJan Schmidt u64 time_seq, struct ulist **roots) 15388da6d581SJan Schmidt { 15398da6d581SJan Schmidt struct ulist *tmp; 15408da6d581SJan Schmidt struct ulist_node *node = NULL; 1541cd1b413cSJan Schmidt struct ulist_iterator uiter; 15428da6d581SJan Schmidt int ret; 15438da6d581SJan Schmidt 15448da6d581SJan Schmidt tmp = ulist_alloc(GFP_NOFS); 15458da6d581SJan Schmidt if (!tmp) 15468da6d581SJan Schmidt return -ENOMEM; 15478da6d581SJan Schmidt *roots = ulist_alloc(GFP_NOFS); 15488da6d581SJan Schmidt if (!*roots) { 15498da6d581SJan Schmidt ulist_free(tmp); 15508da6d581SJan Schmidt return -ENOMEM; 15518da6d581SJan Schmidt } 15528da6d581SJan Schmidt 1553cd1b413cSJan Schmidt ULIST_ITER_INIT(&uiter); 15548da6d581SJan Schmidt while (1) { 1555afce772eSLu Fengqi ret = find_parent_nodes(trans, fs_info, bytenr, time_seq, 1556afce772eSLu Fengqi tmp, *roots, NULL, 0, 0, 0); 15578da6d581SJan Schmidt if (ret < 0 && ret != -ENOENT) { 15588da6d581SJan Schmidt ulist_free(tmp); 15598da6d581SJan Schmidt ulist_free(*roots); 15608da6d581SJan Schmidt return ret; 15618da6d581SJan Schmidt } 1562cd1b413cSJan Schmidt node = ulist_next(tmp, &uiter); 15638da6d581SJan Schmidt if (!node) 15648da6d581SJan Schmidt break; 15658da6d581SJan Schmidt bytenr = node->val; 1566bca1a290SWang Shilong cond_resched(); 15678da6d581SJan Schmidt } 15688da6d581SJan Schmidt 15698da6d581SJan Schmidt ulist_free(tmp); 15708da6d581SJan Schmidt return 0; 15718da6d581SJan Schmidt } 15728da6d581SJan Schmidt 15739e351cc8SJosef Bacik int btrfs_find_all_roots(struct btrfs_trans_handle *trans, 15749e351cc8SJosef Bacik struct btrfs_fs_info *fs_info, u64 bytenr, 15759e351cc8SJosef Bacik u64 time_seq, struct ulist **roots) 15769e351cc8SJosef Bacik { 15779e351cc8SJosef Bacik int ret; 15789e351cc8SJosef Bacik 15799e351cc8SJosef Bacik if (!trans) 15809e351cc8SJosef Bacik down_read(&fs_info->commit_root_sem); 15819e351cc8SJosef Bacik ret = __btrfs_find_all_roots(trans, fs_info, bytenr, time_seq, roots); 15829e351cc8SJosef Bacik if (!trans) 15839e351cc8SJosef Bacik up_read(&fs_info->commit_root_sem); 15849e351cc8SJosef Bacik return ret; 15859e351cc8SJosef Bacik } 15869e351cc8SJosef Bacik 15872c2ed5aaSMark Fasheh /** 15882c2ed5aaSMark Fasheh * btrfs_check_shared - tell us whether an extent is shared 15892c2ed5aaSMark Fasheh * 15902c2ed5aaSMark Fasheh * @trans: optional trans handle 15912c2ed5aaSMark Fasheh * 15922c2ed5aaSMark Fasheh * btrfs_check_shared uses the backref walking code but will short 15932c2ed5aaSMark Fasheh * circuit as soon as it finds a root or inode that doesn't match the 15942c2ed5aaSMark Fasheh * one passed in. This provides a significant performance benefit for 15952c2ed5aaSMark Fasheh * callers (such as fiemap) which want to know whether the extent is 15962c2ed5aaSMark Fasheh * shared but do not need a ref count. 15972c2ed5aaSMark Fasheh * 15982c2ed5aaSMark Fasheh * Return: 0 if extent is not shared, 1 if it is shared, < 0 on error. 15992c2ed5aaSMark Fasheh */ 1600dc046b10SJosef Bacik int btrfs_check_shared(struct btrfs_trans_handle *trans, 1601dc046b10SJosef Bacik struct btrfs_fs_info *fs_info, u64 root_objectid, 1602dc046b10SJosef Bacik u64 inum, u64 bytenr) 1603dc046b10SJosef Bacik { 1604dc046b10SJosef Bacik struct ulist *tmp = NULL; 1605dc046b10SJosef Bacik struct ulist *roots = NULL; 1606dc046b10SJosef Bacik struct ulist_iterator uiter; 1607dc046b10SJosef Bacik struct ulist_node *node; 16083284da7bSDavid Sterba struct seq_list elem = SEQ_LIST_INIT(elem); 1609dc046b10SJosef Bacik int ret = 0; 1610dc046b10SJosef Bacik 1611dc046b10SJosef Bacik tmp = ulist_alloc(GFP_NOFS); 1612dc046b10SJosef Bacik roots = ulist_alloc(GFP_NOFS); 1613dc046b10SJosef Bacik if (!tmp || !roots) { 1614dc046b10SJosef Bacik ulist_free(tmp); 1615dc046b10SJosef Bacik ulist_free(roots); 1616dc046b10SJosef Bacik return -ENOMEM; 1617dc046b10SJosef Bacik } 1618dc046b10SJosef Bacik 1619dc046b10SJosef Bacik if (trans) 1620dc046b10SJosef Bacik btrfs_get_tree_mod_seq(fs_info, &elem); 1621dc046b10SJosef Bacik else 1622dc046b10SJosef Bacik down_read(&fs_info->commit_root_sem); 1623dc046b10SJosef Bacik ULIST_ITER_INIT(&uiter); 1624dc046b10SJosef Bacik while (1) { 1625dc046b10SJosef Bacik ret = find_parent_nodes(trans, fs_info, bytenr, elem.seq, tmp, 1626afce772eSLu Fengqi roots, NULL, root_objectid, inum, 1); 1627dc046b10SJosef Bacik if (ret == BACKREF_FOUND_SHARED) { 16282c2ed5aaSMark Fasheh /* this is the only condition under which we return 1 */ 1629dc046b10SJosef Bacik ret = 1; 1630dc046b10SJosef Bacik break; 1631dc046b10SJosef Bacik } 1632dc046b10SJosef Bacik if (ret < 0 && ret != -ENOENT) 1633dc046b10SJosef Bacik break; 16342c2ed5aaSMark Fasheh ret = 0; 1635dc046b10SJosef Bacik node = ulist_next(tmp, &uiter); 1636dc046b10SJosef Bacik if (!node) 1637dc046b10SJosef Bacik break; 1638dc046b10SJosef Bacik bytenr = node->val; 1639dc046b10SJosef Bacik cond_resched(); 1640dc046b10SJosef Bacik } 1641dc046b10SJosef Bacik if (trans) 1642dc046b10SJosef Bacik btrfs_put_tree_mod_seq(fs_info, &elem); 1643dc046b10SJosef Bacik else 1644dc046b10SJosef Bacik up_read(&fs_info->commit_root_sem); 1645dc046b10SJosef Bacik ulist_free(tmp); 1646dc046b10SJosef Bacik ulist_free(roots); 1647dc046b10SJosef Bacik return ret; 1648dc046b10SJosef Bacik } 1649dc046b10SJosef Bacik 1650f186373fSMark Fasheh int btrfs_find_one_extref(struct btrfs_root *root, u64 inode_objectid, 1651f186373fSMark Fasheh u64 start_off, struct btrfs_path *path, 1652f186373fSMark Fasheh struct btrfs_inode_extref **ret_extref, 1653f186373fSMark Fasheh u64 *found_off) 1654f186373fSMark Fasheh { 1655f186373fSMark Fasheh int ret, slot; 1656f186373fSMark Fasheh struct btrfs_key key; 1657f186373fSMark Fasheh struct btrfs_key found_key; 1658f186373fSMark Fasheh struct btrfs_inode_extref *extref; 165973980becSJeff Mahoney const struct extent_buffer *leaf; 1660f186373fSMark Fasheh unsigned long ptr; 1661f186373fSMark Fasheh 1662f186373fSMark Fasheh key.objectid = inode_objectid; 1663962a298fSDavid Sterba key.type = BTRFS_INODE_EXTREF_KEY; 1664f186373fSMark Fasheh key.offset = start_off; 1665f186373fSMark Fasheh 1666f186373fSMark Fasheh ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); 1667f186373fSMark Fasheh if (ret < 0) 1668f186373fSMark Fasheh return ret; 1669f186373fSMark Fasheh 1670f186373fSMark Fasheh while (1) { 1671f186373fSMark Fasheh leaf = path->nodes[0]; 1672f186373fSMark Fasheh slot = path->slots[0]; 1673f186373fSMark Fasheh if (slot >= btrfs_header_nritems(leaf)) { 1674f186373fSMark Fasheh /* 1675f186373fSMark Fasheh * If the item at offset is not found, 1676f186373fSMark Fasheh * btrfs_search_slot will point us to the slot 1677f186373fSMark Fasheh * where it should be inserted. In our case 1678f186373fSMark Fasheh * that will be the slot directly before the 1679f186373fSMark Fasheh * next INODE_REF_KEY_V2 item. In the case 1680f186373fSMark Fasheh * that we're pointing to the last slot in a 1681f186373fSMark Fasheh * leaf, we must move one leaf over. 1682f186373fSMark Fasheh */ 1683f186373fSMark Fasheh ret = btrfs_next_leaf(root, path); 1684f186373fSMark Fasheh if (ret) { 1685f186373fSMark Fasheh if (ret >= 1) 1686f186373fSMark Fasheh ret = -ENOENT; 1687f186373fSMark Fasheh break; 1688f186373fSMark Fasheh } 1689f186373fSMark Fasheh continue; 1690f186373fSMark Fasheh } 1691f186373fSMark Fasheh 1692f186373fSMark Fasheh btrfs_item_key_to_cpu(leaf, &found_key, slot); 1693f186373fSMark Fasheh 1694f186373fSMark Fasheh /* 1695f186373fSMark Fasheh * Check that we're still looking at an extended ref key for 1696f186373fSMark Fasheh * this particular objectid. If we have different 1697f186373fSMark Fasheh * objectid or type then there are no more to be found 1698f186373fSMark Fasheh * in the tree and we can exit. 1699f186373fSMark Fasheh */ 1700f186373fSMark Fasheh ret = -ENOENT; 1701f186373fSMark Fasheh if (found_key.objectid != inode_objectid) 1702f186373fSMark Fasheh break; 1703962a298fSDavid Sterba if (found_key.type != BTRFS_INODE_EXTREF_KEY) 1704f186373fSMark Fasheh break; 1705f186373fSMark Fasheh 1706f186373fSMark Fasheh ret = 0; 1707f186373fSMark Fasheh ptr = btrfs_item_ptr_offset(leaf, path->slots[0]); 1708f186373fSMark Fasheh extref = (struct btrfs_inode_extref *)ptr; 1709f186373fSMark Fasheh *ret_extref = extref; 1710f186373fSMark Fasheh if (found_off) 1711f186373fSMark Fasheh *found_off = found_key.offset; 1712f186373fSMark Fasheh break; 1713f186373fSMark Fasheh } 1714f186373fSMark Fasheh 1715f186373fSMark Fasheh return ret; 1716f186373fSMark Fasheh } 1717f186373fSMark Fasheh 171848a3b636SEric Sandeen /* 171948a3b636SEric Sandeen * this iterates to turn a name (from iref/extref) into a full filesystem path. 172048a3b636SEric Sandeen * Elements of the path are separated by '/' and the path is guaranteed to be 172148a3b636SEric Sandeen * 0-terminated. the path is only given within the current file system. 172248a3b636SEric Sandeen * Therefore, it never starts with a '/'. the caller is responsible to provide 172348a3b636SEric Sandeen * "size" bytes in "dest". the dest buffer will be filled backwards. finally, 172448a3b636SEric Sandeen * the start point of the resulting string is returned. this pointer is within 172548a3b636SEric Sandeen * dest, normally. 172648a3b636SEric Sandeen * in case the path buffer would overflow, the pointer is decremented further 172748a3b636SEric Sandeen * as if output was written to the buffer, though no more output is actually 172848a3b636SEric Sandeen * generated. that way, the caller can determine how much space would be 172948a3b636SEric Sandeen * required for the path to fit into the buffer. in that case, the returned 173048a3b636SEric Sandeen * value will be smaller than dest. callers must check this! 173148a3b636SEric Sandeen */ 173296b5bd77SJan Schmidt char *btrfs_ref_to_path(struct btrfs_root *fs_root, struct btrfs_path *path, 1733d24bec3aSMark Fasheh u32 name_len, unsigned long name_off, 1734a542ad1bSJan Schmidt struct extent_buffer *eb_in, u64 parent, 1735a542ad1bSJan Schmidt char *dest, u32 size) 1736a542ad1bSJan Schmidt { 1737a542ad1bSJan Schmidt int slot; 1738a542ad1bSJan Schmidt u64 next_inum; 1739a542ad1bSJan Schmidt int ret; 1740661bec6bSGabriel de Perthuis s64 bytes_left = ((s64)size) - 1; 1741a542ad1bSJan Schmidt struct extent_buffer *eb = eb_in; 1742a542ad1bSJan Schmidt struct btrfs_key found_key; 1743b916a59aSJan Schmidt int leave_spinning = path->leave_spinning; 1744d24bec3aSMark Fasheh struct btrfs_inode_ref *iref; 1745a542ad1bSJan Schmidt 1746a542ad1bSJan Schmidt if (bytes_left >= 0) 1747a542ad1bSJan Schmidt dest[bytes_left] = '\0'; 1748a542ad1bSJan Schmidt 1749b916a59aSJan Schmidt path->leave_spinning = 1; 1750a542ad1bSJan Schmidt while (1) { 1751d24bec3aSMark Fasheh bytes_left -= name_len; 1752a542ad1bSJan Schmidt if (bytes_left >= 0) 1753a542ad1bSJan Schmidt read_extent_buffer(eb, dest + bytes_left, 1754d24bec3aSMark Fasheh name_off, name_len); 1755b916a59aSJan Schmidt if (eb != eb_in) { 17560c0fe3b0SFilipe Manana if (!path->skip_locking) 1757b916a59aSJan Schmidt btrfs_tree_read_unlock_blocking(eb); 1758a542ad1bSJan Schmidt free_extent_buffer(eb); 1759b916a59aSJan Schmidt } 1760c234a24dSDavid Sterba ret = btrfs_find_item(fs_root, path, parent, 0, 1761c234a24dSDavid Sterba BTRFS_INODE_REF_KEY, &found_key); 17628f24b496SJan Schmidt if (ret > 0) 17638f24b496SJan Schmidt ret = -ENOENT; 1764a542ad1bSJan Schmidt if (ret) 1765a542ad1bSJan Schmidt break; 1766d24bec3aSMark Fasheh 1767a542ad1bSJan Schmidt next_inum = found_key.offset; 1768a542ad1bSJan Schmidt 1769a542ad1bSJan Schmidt /* regular exit ahead */ 1770a542ad1bSJan Schmidt if (parent == next_inum) 1771a542ad1bSJan Schmidt break; 1772a542ad1bSJan Schmidt 1773a542ad1bSJan Schmidt slot = path->slots[0]; 1774a542ad1bSJan Schmidt eb = path->nodes[0]; 1775a542ad1bSJan Schmidt /* make sure we can use eb after releasing the path */ 1776b916a59aSJan Schmidt if (eb != eb_in) { 17770c0fe3b0SFilipe Manana if (!path->skip_locking) 1778b916a59aSJan Schmidt btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK); 17790c0fe3b0SFilipe Manana path->nodes[0] = NULL; 17800c0fe3b0SFilipe Manana path->locks[0] = 0; 1781b916a59aSJan Schmidt } 1782a542ad1bSJan Schmidt btrfs_release_path(path); 1783a542ad1bSJan Schmidt iref = btrfs_item_ptr(eb, slot, struct btrfs_inode_ref); 1784d24bec3aSMark Fasheh 1785d24bec3aSMark Fasheh name_len = btrfs_inode_ref_name_len(eb, iref); 1786d24bec3aSMark Fasheh name_off = (unsigned long)(iref + 1); 1787d24bec3aSMark Fasheh 1788a542ad1bSJan Schmidt parent = next_inum; 1789a542ad1bSJan Schmidt --bytes_left; 1790a542ad1bSJan Schmidt if (bytes_left >= 0) 1791a542ad1bSJan Schmidt dest[bytes_left] = '/'; 1792a542ad1bSJan Schmidt } 1793a542ad1bSJan Schmidt 1794a542ad1bSJan Schmidt btrfs_release_path(path); 1795b916a59aSJan Schmidt path->leave_spinning = leave_spinning; 1796a542ad1bSJan Schmidt 1797a542ad1bSJan Schmidt if (ret) 1798a542ad1bSJan Schmidt return ERR_PTR(ret); 1799a542ad1bSJan Schmidt 1800a542ad1bSJan Schmidt return dest + bytes_left; 1801a542ad1bSJan Schmidt } 1802a542ad1bSJan Schmidt 1803a542ad1bSJan Schmidt /* 1804a542ad1bSJan Schmidt * this makes the path point to (logical EXTENT_ITEM *) 1805a542ad1bSJan Schmidt * returns BTRFS_EXTENT_FLAG_DATA for data, BTRFS_EXTENT_FLAG_TREE_BLOCK for 1806a542ad1bSJan Schmidt * tree blocks and <0 on error. 1807a542ad1bSJan Schmidt */ 1808a542ad1bSJan Schmidt int extent_from_logical(struct btrfs_fs_info *fs_info, u64 logical, 180969917e43SLiu Bo struct btrfs_path *path, struct btrfs_key *found_key, 181069917e43SLiu Bo u64 *flags_ret) 1811a542ad1bSJan Schmidt { 1812a542ad1bSJan Schmidt int ret; 1813a542ad1bSJan Schmidt u64 flags; 1814261c84b6SJosef Bacik u64 size = 0; 1815a542ad1bSJan Schmidt u32 item_size; 181673980becSJeff Mahoney const struct extent_buffer *eb; 1817a542ad1bSJan Schmidt struct btrfs_extent_item *ei; 1818a542ad1bSJan Schmidt struct btrfs_key key; 1819a542ad1bSJan Schmidt 1820261c84b6SJosef Bacik if (btrfs_fs_incompat(fs_info, SKINNY_METADATA)) 1821261c84b6SJosef Bacik key.type = BTRFS_METADATA_ITEM_KEY; 1822261c84b6SJosef Bacik else 1823a542ad1bSJan Schmidt key.type = BTRFS_EXTENT_ITEM_KEY; 1824a542ad1bSJan Schmidt key.objectid = logical; 1825a542ad1bSJan Schmidt key.offset = (u64)-1; 1826a542ad1bSJan Schmidt 1827a542ad1bSJan Schmidt ret = btrfs_search_slot(NULL, fs_info->extent_root, &key, path, 0, 0); 1828a542ad1bSJan Schmidt if (ret < 0) 1829a542ad1bSJan Schmidt return ret; 1830a542ad1bSJan Schmidt 1831850a8cdfSWang Shilong ret = btrfs_previous_extent_item(fs_info->extent_root, path, 0); 1832850a8cdfSWang Shilong if (ret) { 1833850a8cdfSWang Shilong if (ret > 0) 1834580f0a67SJosef Bacik ret = -ENOENT; 1835580f0a67SJosef Bacik return ret; 1836580f0a67SJosef Bacik } 1837850a8cdfSWang Shilong btrfs_item_key_to_cpu(path->nodes[0], found_key, path->slots[0]); 1838261c84b6SJosef Bacik if (found_key->type == BTRFS_METADATA_ITEM_KEY) 1839da17066cSJeff Mahoney size = fs_info->nodesize; 1840261c84b6SJosef Bacik else if (found_key->type == BTRFS_EXTENT_ITEM_KEY) 1841261c84b6SJosef Bacik size = found_key->offset; 1842261c84b6SJosef Bacik 1843580f0a67SJosef Bacik if (found_key->objectid > logical || 1844261c84b6SJosef Bacik found_key->objectid + size <= logical) { 1845ab8d0fc4SJeff Mahoney btrfs_debug(fs_info, 1846ab8d0fc4SJeff Mahoney "logical %llu is not within any extent", logical); 1847a542ad1bSJan Schmidt return -ENOENT; 18484692cf58SJan Schmidt } 1849a542ad1bSJan Schmidt 1850a542ad1bSJan Schmidt eb = path->nodes[0]; 1851a542ad1bSJan Schmidt item_size = btrfs_item_size_nr(eb, path->slots[0]); 1852a542ad1bSJan Schmidt BUG_ON(item_size < sizeof(*ei)); 1853a542ad1bSJan Schmidt 1854a542ad1bSJan Schmidt ei = btrfs_item_ptr(eb, path->slots[0], struct btrfs_extent_item); 1855a542ad1bSJan Schmidt flags = btrfs_extent_flags(eb, ei); 1856a542ad1bSJan Schmidt 1857ab8d0fc4SJeff Mahoney btrfs_debug(fs_info, 1858ab8d0fc4SJeff Mahoney "logical %llu is at position %llu within the extent (%llu EXTENT_ITEM %llu) flags %#llx size %u", 1859c1c9ff7cSGeert Uytterhoeven logical, logical - found_key->objectid, found_key->objectid, 1860c1c9ff7cSGeert Uytterhoeven found_key->offset, flags, item_size); 186169917e43SLiu Bo 186269917e43SLiu Bo WARN_ON(!flags_ret); 186369917e43SLiu Bo if (flags_ret) { 1864a542ad1bSJan Schmidt if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) 186569917e43SLiu Bo *flags_ret = BTRFS_EXTENT_FLAG_TREE_BLOCK; 186669917e43SLiu Bo else if (flags & BTRFS_EXTENT_FLAG_DATA) 186769917e43SLiu Bo *flags_ret = BTRFS_EXTENT_FLAG_DATA; 186869917e43SLiu Bo else 186969917e43SLiu Bo BUG_ON(1); 187069917e43SLiu Bo return 0; 187169917e43SLiu Bo } 1872a542ad1bSJan Schmidt 1873a542ad1bSJan Schmidt return -EIO; 1874a542ad1bSJan Schmidt } 1875a542ad1bSJan Schmidt 1876a542ad1bSJan Schmidt /* 1877a542ad1bSJan Schmidt * helper function to iterate extent inline refs. ptr must point to a 0 value 1878a542ad1bSJan Schmidt * for the first call and may be modified. it is used to track state. 1879a542ad1bSJan Schmidt * if more refs exist, 0 is returned and the next call to 1880a542ad1bSJan Schmidt * __get_extent_inline_ref must pass the modified ptr parameter to get the 1881a542ad1bSJan Schmidt * next ref. after the last ref was processed, 1 is returned. 1882a542ad1bSJan Schmidt * returns <0 on error 1883a542ad1bSJan Schmidt */ 188473980becSJeff Mahoney static int __get_extent_inline_ref(unsigned long *ptr, 188573980becSJeff Mahoney const struct extent_buffer *eb, 188673980becSJeff Mahoney const struct btrfs_key *key, 188773980becSJeff Mahoney const struct btrfs_extent_item *ei, 188873980becSJeff Mahoney u32 item_size, 1889a542ad1bSJan Schmidt struct btrfs_extent_inline_ref **out_eiref, 1890a542ad1bSJan Schmidt int *out_type) 1891a542ad1bSJan Schmidt { 1892a542ad1bSJan Schmidt unsigned long end; 1893a542ad1bSJan Schmidt u64 flags; 1894a542ad1bSJan Schmidt struct btrfs_tree_block_info *info; 1895a542ad1bSJan Schmidt 1896a542ad1bSJan Schmidt if (!*ptr) { 1897a542ad1bSJan Schmidt /* first call */ 1898a542ad1bSJan Schmidt flags = btrfs_extent_flags(eb, ei); 1899a542ad1bSJan Schmidt if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) { 19006eda71d0SLiu Bo if (key->type == BTRFS_METADATA_ITEM_KEY) { 19016eda71d0SLiu Bo /* a skinny metadata extent */ 19026eda71d0SLiu Bo *out_eiref = 19036eda71d0SLiu Bo (struct btrfs_extent_inline_ref *)(ei + 1); 19046eda71d0SLiu Bo } else { 19056eda71d0SLiu Bo WARN_ON(key->type != BTRFS_EXTENT_ITEM_KEY); 1906a542ad1bSJan Schmidt info = (struct btrfs_tree_block_info *)(ei + 1); 1907a542ad1bSJan Schmidt *out_eiref = 1908a542ad1bSJan Schmidt (struct btrfs_extent_inline_ref *)(info + 1); 19096eda71d0SLiu Bo } 1910a542ad1bSJan Schmidt } else { 1911a542ad1bSJan Schmidt *out_eiref = (struct btrfs_extent_inline_ref *)(ei + 1); 1912a542ad1bSJan Schmidt } 1913a542ad1bSJan Schmidt *ptr = (unsigned long)*out_eiref; 1914cd857dd6SLiu Bo if ((unsigned long)(*ptr) >= (unsigned long)ei + item_size) 1915a542ad1bSJan Schmidt return -ENOENT; 1916a542ad1bSJan Schmidt } 1917a542ad1bSJan Schmidt 1918a542ad1bSJan Schmidt end = (unsigned long)ei + item_size; 19196eda71d0SLiu Bo *out_eiref = (struct btrfs_extent_inline_ref *)(*ptr); 1920a542ad1bSJan Schmidt *out_type = btrfs_extent_inline_ref_type(eb, *out_eiref); 1921a542ad1bSJan Schmidt 1922a542ad1bSJan Schmidt *ptr += btrfs_extent_inline_ref_size(*out_type); 1923a542ad1bSJan Schmidt WARN_ON(*ptr > end); 1924a542ad1bSJan Schmidt if (*ptr == end) 1925a542ad1bSJan Schmidt return 1; /* last */ 1926a542ad1bSJan Schmidt 1927a542ad1bSJan Schmidt return 0; 1928a542ad1bSJan Schmidt } 1929a542ad1bSJan Schmidt 1930a542ad1bSJan Schmidt /* 1931a542ad1bSJan Schmidt * reads the tree block backref for an extent. tree level and root are returned 1932a542ad1bSJan Schmidt * through out_level and out_root. ptr must point to a 0 value for the first 1933a542ad1bSJan Schmidt * call and may be modified (see __get_extent_inline_ref comment). 1934a542ad1bSJan Schmidt * returns 0 if data was provided, 1 if there was no more data to provide or 1935a542ad1bSJan Schmidt * <0 on error. 1936a542ad1bSJan Schmidt */ 1937a542ad1bSJan Schmidt int tree_backref_for_extent(unsigned long *ptr, struct extent_buffer *eb, 19386eda71d0SLiu Bo struct btrfs_key *key, struct btrfs_extent_item *ei, 19396eda71d0SLiu Bo u32 item_size, u64 *out_root, u8 *out_level) 1940a542ad1bSJan Schmidt { 1941a542ad1bSJan Schmidt int ret; 1942a542ad1bSJan Schmidt int type; 1943a542ad1bSJan Schmidt struct btrfs_extent_inline_ref *eiref; 1944a542ad1bSJan Schmidt 1945a542ad1bSJan Schmidt if (*ptr == (unsigned long)-1) 1946a542ad1bSJan Schmidt return 1; 1947a542ad1bSJan Schmidt 1948a542ad1bSJan Schmidt while (1) { 19496eda71d0SLiu Bo ret = __get_extent_inline_ref(ptr, eb, key, ei, item_size, 1950a542ad1bSJan Schmidt &eiref, &type); 1951a542ad1bSJan Schmidt if (ret < 0) 1952a542ad1bSJan Schmidt return ret; 1953a542ad1bSJan Schmidt 1954a542ad1bSJan Schmidt if (type == BTRFS_TREE_BLOCK_REF_KEY || 1955a542ad1bSJan Schmidt type == BTRFS_SHARED_BLOCK_REF_KEY) 1956a542ad1bSJan Schmidt break; 1957a542ad1bSJan Schmidt 1958a542ad1bSJan Schmidt if (ret == 1) 1959a542ad1bSJan Schmidt return 1; 1960a542ad1bSJan Schmidt } 1961a542ad1bSJan Schmidt 1962a542ad1bSJan Schmidt /* we can treat both ref types equally here */ 1963a542ad1bSJan Schmidt *out_root = btrfs_extent_inline_ref_offset(eb, eiref); 1964a1317f45SFilipe Manana 1965a1317f45SFilipe Manana if (key->type == BTRFS_EXTENT_ITEM_KEY) { 1966a1317f45SFilipe Manana struct btrfs_tree_block_info *info; 1967a1317f45SFilipe Manana 1968a1317f45SFilipe Manana info = (struct btrfs_tree_block_info *)(ei + 1); 1969a542ad1bSJan Schmidt *out_level = btrfs_tree_block_level(eb, info); 1970a1317f45SFilipe Manana } else { 1971a1317f45SFilipe Manana ASSERT(key->type == BTRFS_METADATA_ITEM_KEY); 1972a1317f45SFilipe Manana *out_level = (u8)key->offset; 1973a1317f45SFilipe Manana } 1974a542ad1bSJan Schmidt 1975a542ad1bSJan Schmidt if (ret == 1) 1976a542ad1bSJan Schmidt *ptr = (unsigned long)-1; 1977a542ad1bSJan Schmidt 1978a542ad1bSJan Schmidt return 0; 1979a542ad1bSJan Schmidt } 1980a542ad1bSJan Schmidt 1981ab8d0fc4SJeff Mahoney static int iterate_leaf_refs(struct btrfs_fs_info *fs_info, 1982ab8d0fc4SJeff Mahoney struct extent_inode_elem *inode_list, 1983976b1908SJan Schmidt u64 root, u64 extent_item_objectid, 19844692cf58SJan Schmidt iterate_extent_inodes_t *iterate, void *ctx) 1985a542ad1bSJan Schmidt { 1986976b1908SJan Schmidt struct extent_inode_elem *eie; 19874692cf58SJan Schmidt int ret = 0; 1988a542ad1bSJan Schmidt 1989976b1908SJan Schmidt for (eie = inode_list; eie; eie = eie->next) { 1990ab8d0fc4SJeff Mahoney btrfs_debug(fs_info, 1991ab8d0fc4SJeff Mahoney "ref for %llu resolved, key (%llu EXTEND_DATA %llu), root %llu", 1992ab8d0fc4SJeff Mahoney extent_item_objectid, eie->inum, 1993ab8d0fc4SJeff Mahoney eie->offset, root); 1994976b1908SJan Schmidt ret = iterate(eie->inum, eie->offset, root, ctx); 19954692cf58SJan Schmidt if (ret) { 1996ab8d0fc4SJeff Mahoney btrfs_debug(fs_info, 1997ab8d0fc4SJeff Mahoney "stopping iteration for %llu due to ret=%d", 1998976b1908SJan Schmidt extent_item_objectid, ret); 1999a542ad1bSJan Schmidt break; 2000a542ad1bSJan Schmidt } 2001a542ad1bSJan Schmidt } 2002a542ad1bSJan Schmidt 2003a542ad1bSJan Schmidt return ret; 2004a542ad1bSJan Schmidt } 2005a542ad1bSJan Schmidt 2006a542ad1bSJan Schmidt /* 2007a542ad1bSJan Schmidt * calls iterate() for every inode that references the extent identified by 20084692cf58SJan Schmidt * the given parameters. 2009a542ad1bSJan Schmidt * when the iterator function returns a non-zero value, iteration stops. 2010a542ad1bSJan Schmidt */ 2011a542ad1bSJan Schmidt int iterate_extent_inodes(struct btrfs_fs_info *fs_info, 20124692cf58SJan Schmidt u64 extent_item_objectid, u64 extent_item_pos, 20137a3ae2f8SJan Schmidt int search_commit_root, 2014a542ad1bSJan Schmidt iterate_extent_inodes_t *iterate, void *ctx) 2015a542ad1bSJan Schmidt { 2016a542ad1bSJan Schmidt int ret; 2017da61d31aSJosef Bacik struct btrfs_trans_handle *trans = NULL; 20187a3ae2f8SJan Schmidt struct ulist *refs = NULL; 20197a3ae2f8SJan Schmidt struct ulist *roots = NULL; 20204692cf58SJan Schmidt struct ulist_node *ref_node = NULL; 20214692cf58SJan Schmidt struct ulist_node *root_node = NULL; 20223284da7bSDavid Sterba struct seq_list tree_mod_seq_elem = SEQ_LIST_INIT(tree_mod_seq_elem); 2023cd1b413cSJan Schmidt struct ulist_iterator ref_uiter; 2024cd1b413cSJan Schmidt struct ulist_iterator root_uiter; 2025a542ad1bSJan Schmidt 2026ab8d0fc4SJeff Mahoney btrfs_debug(fs_info, "resolving all inodes for extent %llu", 20274692cf58SJan Schmidt extent_item_objectid); 20284692cf58SJan Schmidt 2029da61d31aSJosef Bacik if (!search_commit_root) { 20307a3ae2f8SJan Schmidt trans = btrfs_join_transaction(fs_info->extent_root); 20317a3ae2f8SJan Schmidt if (IS_ERR(trans)) 20327a3ae2f8SJan Schmidt return PTR_ERR(trans); 20338445f61cSJan Schmidt btrfs_get_tree_mod_seq(fs_info, &tree_mod_seq_elem); 20349e351cc8SJosef Bacik } else { 20359e351cc8SJosef Bacik down_read(&fs_info->commit_root_sem); 20367a3ae2f8SJan Schmidt } 20374692cf58SJan Schmidt 20384692cf58SJan Schmidt ret = btrfs_find_all_leafs(trans, fs_info, extent_item_objectid, 2039097b8a7cSJan Schmidt tree_mod_seq_elem.seq, &refs, 20408445f61cSJan Schmidt &extent_item_pos); 20414692cf58SJan Schmidt if (ret) 20424692cf58SJan Schmidt goto out; 20434692cf58SJan Schmidt 2044cd1b413cSJan Schmidt ULIST_ITER_INIT(&ref_uiter); 2045cd1b413cSJan Schmidt while (!ret && (ref_node = ulist_next(refs, &ref_uiter))) { 20469e351cc8SJosef Bacik ret = __btrfs_find_all_roots(trans, fs_info, ref_node->val, 20478445f61cSJan Schmidt tree_mod_seq_elem.seq, &roots); 20484692cf58SJan Schmidt if (ret) 2049a542ad1bSJan Schmidt break; 2050cd1b413cSJan Schmidt ULIST_ITER_INIT(&root_uiter); 2051cd1b413cSJan Schmidt while (!ret && (root_node = ulist_next(roots, &root_uiter))) { 2052ab8d0fc4SJeff Mahoney btrfs_debug(fs_info, 2053ab8d0fc4SJeff Mahoney "root %llu references leaf %llu, data list %#llx", 2054ab8d0fc4SJeff Mahoney root_node->val, ref_node->val, 2055c1c9ff7cSGeert Uytterhoeven ref_node->aux); 2056ab8d0fc4SJeff Mahoney ret = iterate_leaf_refs(fs_info, 2057ab8d0fc4SJeff Mahoney (struct extent_inode_elem *) 2058995e01b7SJan Schmidt (uintptr_t)ref_node->aux, 2059995e01b7SJan Schmidt root_node->val, 2060995e01b7SJan Schmidt extent_item_objectid, 2061a542ad1bSJan Schmidt iterate, ctx); 20624692cf58SJan Schmidt } 2063976b1908SJan Schmidt ulist_free(roots); 2064a542ad1bSJan Schmidt } 2065a542ad1bSJan Schmidt 2066976b1908SJan Schmidt free_leaf_list(refs); 20674692cf58SJan Schmidt out: 20687a3ae2f8SJan Schmidt if (!search_commit_root) { 20698445f61cSJan Schmidt btrfs_put_tree_mod_seq(fs_info, &tree_mod_seq_elem); 20703a45bb20SJeff Mahoney btrfs_end_transaction(trans); 20719e351cc8SJosef Bacik } else { 20729e351cc8SJosef Bacik up_read(&fs_info->commit_root_sem); 20737a3ae2f8SJan Schmidt } 20747a3ae2f8SJan Schmidt 2075a542ad1bSJan Schmidt return ret; 2076a542ad1bSJan Schmidt } 2077a542ad1bSJan Schmidt 2078a542ad1bSJan Schmidt int iterate_inodes_from_logical(u64 logical, struct btrfs_fs_info *fs_info, 2079a542ad1bSJan Schmidt struct btrfs_path *path, 2080a542ad1bSJan Schmidt iterate_extent_inodes_t *iterate, void *ctx) 2081a542ad1bSJan Schmidt { 2082a542ad1bSJan Schmidt int ret; 20834692cf58SJan Schmidt u64 extent_item_pos; 208469917e43SLiu Bo u64 flags = 0; 2085a542ad1bSJan Schmidt struct btrfs_key found_key; 20867a3ae2f8SJan Schmidt int search_commit_root = path->search_commit_root; 2087a542ad1bSJan Schmidt 208869917e43SLiu Bo ret = extent_from_logical(fs_info, logical, path, &found_key, &flags); 20894692cf58SJan Schmidt btrfs_release_path(path); 2090a542ad1bSJan Schmidt if (ret < 0) 2091a542ad1bSJan Schmidt return ret; 209269917e43SLiu Bo if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) 20933627bf45SStefan Behrens return -EINVAL; 2094a542ad1bSJan Schmidt 20954692cf58SJan Schmidt extent_item_pos = logical - found_key.objectid; 20967a3ae2f8SJan Schmidt ret = iterate_extent_inodes(fs_info, found_key.objectid, 20977a3ae2f8SJan Schmidt extent_item_pos, search_commit_root, 20987a3ae2f8SJan Schmidt iterate, ctx); 2099a542ad1bSJan Schmidt 2100a542ad1bSJan Schmidt return ret; 2101a542ad1bSJan Schmidt } 2102a542ad1bSJan Schmidt 2103d24bec3aSMark Fasheh typedef int (iterate_irefs_t)(u64 parent, u32 name_len, unsigned long name_off, 2104d24bec3aSMark Fasheh struct extent_buffer *eb, void *ctx); 2105d24bec3aSMark Fasheh 2106d24bec3aSMark Fasheh static int iterate_inode_refs(u64 inum, struct btrfs_root *fs_root, 2107a542ad1bSJan Schmidt struct btrfs_path *path, 2108a542ad1bSJan Schmidt iterate_irefs_t *iterate, void *ctx) 2109a542ad1bSJan Schmidt { 2110aefc1eb1SJan Schmidt int ret = 0; 2111a542ad1bSJan Schmidt int slot; 2112a542ad1bSJan Schmidt u32 cur; 2113a542ad1bSJan Schmidt u32 len; 2114a542ad1bSJan Schmidt u32 name_len; 2115a542ad1bSJan Schmidt u64 parent = 0; 2116a542ad1bSJan Schmidt int found = 0; 2117a542ad1bSJan Schmidt struct extent_buffer *eb; 2118a542ad1bSJan Schmidt struct btrfs_item *item; 2119a542ad1bSJan Schmidt struct btrfs_inode_ref *iref; 2120a542ad1bSJan Schmidt struct btrfs_key found_key; 2121a542ad1bSJan Schmidt 2122aefc1eb1SJan Schmidt while (!ret) { 2123c234a24dSDavid Sterba ret = btrfs_find_item(fs_root, path, inum, 2124c234a24dSDavid Sterba parent ? parent + 1 : 0, BTRFS_INODE_REF_KEY, 2125a542ad1bSJan Schmidt &found_key); 2126c234a24dSDavid Sterba 2127a542ad1bSJan Schmidt if (ret < 0) 2128a542ad1bSJan Schmidt break; 2129a542ad1bSJan Schmidt if (ret) { 2130a542ad1bSJan Schmidt ret = found ? 0 : -ENOENT; 2131a542ad1bSJan Schmidt break; 2132a542ad1bSJan Schmidt } 2133a542ad1bSJan Schmidt ++found; 2134a542ad1bSJan Schmidt 2135a542ad1bSJan Schmidt parent = found_key.offset; 2136a542ad1bSJan Schmidt slot = path->slots[0]; 21373fe81ce2SFilipe David Borba Manana eb = btrfs_clone_extent_buffer(path->nodes[0]); 21383fe81ce2SFilipe David Borba Manana if (!eb) { 21393fe81ce2SFilipe David Borba Manana ret = -ENOMEM; 21403fe81ce2SFilipe David Borba Manana break; 21413fe81ce2SFilipe David Borba Manana } 21423fe81ce2SFilipe David Borba Manana extent_buffer_get(eb); 2143b916a59aSJan Schmidt btrfs_tree_read_lock(eb); 2144b916a59aSJan Schmidt btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK); 2145a542ad1bSJan Schmidt btrfs_release_path(path); 2146a542ad1bSJan Schmidt 2147dd3cc16bSRoss Kirk item = btrfs_item_nr(slot); 2148a542ad1bSJan Schmidt iref = btrfs_item_ptr(eb, slot, struct btrfs_inode_ref); 2149a542ad1bSJan Schmidt 2150a542ad1bSJan Schmidt for (cur = 0; cur < btrfs_item_size(eb, item); cur += len) { 2151a542ad1bSJan Schmidt name_len = btrfs_inode_ref_name_len(eb, iref); 2152a542ad1bSJan Schmidt /* path must be released before calling iterate()! */ 2153ab8d0fc4SJeff Mahoney btrfs_debug(fs_root->fs_info, 2154ab8d0fc4SJeff Mahoney "following ref at offset %u for inode %llu in tree %llu", 2155ab8d0fc4SJeff Mahoney cur, found_key.objectid, fs_root->objectid); 2156d24bec3aSMark Fasheh ret = iterate(parent, name_len, 2157d24bec3aSMark Fasheh (unsigned long)(iref + 1), eb, ctx); 2158aefc1eb1SJan Schmidt if (ret) 2159a542ad1bSJan Schmidt break; 2160a542ad1bSJan Schmidt len = sizeof(*iref) + name_len; 2161a542ad1bSJan Schmidt iref = (struct btrfs_inode_ref *)((char *)iref + len); 2162a542ad1bSJan Schmidt } 2163b916a59aSJan Schmidt btrfs_tree_read_unlock_blocking(eb); 2164a542ad1bSJan Schmidt free_extent_buffer(eb); 2165a542ad1bSJan Schmidt } 2166a542ad1bSJan Schmidt 2167a542ad1bSJan Schmidt btrfs_release_path(path); 2168a542ad1bSJan Schmidt 2169a542ad1bSJan Schmidt return ret; 2170a542ad1bSJan Schmidt } 2171a542ad1bSJan Schmidt 2172d24bec3aSMark Fasheh static int iterate_inode_extrefs(u64 inum, struct btrfs_root *fs_root, 2173d24bec3aSMark Fasheh struct btrfs_path *path, 2174d24bec3aSMark Fasheh iterate_irefs_t *iterate, void *ctx) 2175d24bec3aSMark Fasheh { 2176d24bec3aSMark Fasheh int ret; 2177d24bec3aSMark Fasheh int slot; 2178d24bec3aSMark Fasheh u64 offset = 0; 2179d24bec3aSMark Fasheh u64 parent; 2180d24bec3aSMark Fasheh int found = 0; 2181d24bec3aSMark Fasheh struct extent_buffer *eb; 2182d24bec3aSMark Fasheh struct btrfs_inode_extref *extref; 2183d24bec3aSMark Fasheh u32 item_size; 2184d24bec3aSMark Fasheh u32 cur_offset; 2185d24bec3aSMark Fasheh unsigned long ptr; 2186d24bec3aSMark Fasheh 2187d24bec3aSMark Fasheh while (1) { 2188d24bec3aSMark Fasheh ret = btrfs_find_one_extref(fs_root, inum, offset, path, &extref, 2189d24bec3aSMark Fasheh &offset); 2190d24bec3aSMark Fasheh if (ret < 0) 2191d24bec3aSMark Fasheh break; 2192d24bec3aSMark Fasheh if (ret) { 2193d24bec3aSMark Fasheh ret = found ? 0 : -ENOENT; 2194d24bec3aSMark Fasheh break; 2195d24bec3aSMark Fasheh } 2196d24bec3aSMark Fasheh ++found; 2197d24bec3aSMark Fasheh 2198d24bec3aSMark Fasheh slot = path->slots[0]; 21993fe81ce2SFilipe David Borba Manana eb = btrfs_clone_extent_buffer(path->nodes[0]); 22003fe81ce2SFilipe David Borba Manana if (!eb) { 22013fe81ce2SFilipe David Borba Manana ret = -ENOMEM; 22023fe81ce2SFilipe David Borba Manana break; 22033fe81ce2SFilipe David Borba Manana } 22043fe81ce2SFilipe David Borba Manana extent_buffer_get(eb); 2205d24bec3aSMark Fasheh 2206d24bec3aSMark Fasheh btrfs_tree_read_lock(eb); 2207d24bec3aSMark Fasheh btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK); 2208d24bec3aSMark Fasheh btrfs_release_path(path); 2209d24bec3aSMark Fasheh 22102849a854SChris Mason item_size = btrfs_item_size_nr(eb, slot); 22112849a854SChris Mason ptr = btrfs_item_ptr_offset(eb, slot); 2212d24bec3aSMark Fasheh cur_offset = 0; 2213d24bec3aSMark Fasheh 2214d24bec3aSMark Fasheh while (cur_offset < item_size) { 2215d24bec3aSMark Fasheh u32 name_len; 2216d24bec3aSMark Fasheh 2217d24bec3aSMark Fasheh extref = (struct btrfs_inode_extref *)(ptr + cur_offset); 2218d24bec3aSMark Fasheh parent = btrfs_inode_extref_parent(eb, extref); 2219d24bec3aSMark Fasheh name_len = btrfs_inode_extref_name_len(eb, extref); 2220d24bec3aSMark Fasheh ret = iterate(parent, name_len, 2221d24bec3aSMark Fasheh (unsigned long)&extref->name, eb, ctx); 2222d24bec3aSMark Fasheh if (ret) 2223d24bec3aSMark Fasheh break; 2224d24bec3aSMark Fasheh 22252849a854SChris Mason cur_offset += btrfs_inode_extref_name_len(eb, extref); 2226d24bec3aSMark Fasheh cur_offset += sizeof(*extref); 2227d24bec3aSMark Fasheh } 2228d24bec3aSMark Fasheh btrfs_tree_read_unlock_blocking(eb); 2229d24bec3aSMark Fasheh free_extent_buffer(eb); 2230d24bec3aSMark Fasheh 2231d24bec3aSMark Fasheh offset++; 2232d24bec3aSMark Fasheh } 2233d24bec3aSMark Fasheh 2234d24bec3aSMark Fasheh btrfs_release_path(path); 2235d24bec3aSMark Fasheh 2236d24bec3aSMark Fasheh return ret; 2237d24bec3aSMark Fasheh } 2238d24bec3aSMark Fasheh 2239d24bec3aSMark Fasheh static int iterate_irefs(u64 inum, struct btrfs_root *fs_root, 2240d24bec3aSMark Fasheh struct btrfs_path *path, iterate_irefs_t *iterate, 2241d24bec3aSMark Fasheh void *ctx) 2242d24bec3aSMark Fasheh { 2243d24bec3aSMark Fasheh int ret; 2244d24bec3aSMark Fasheh int found_refs = 0; 2245d24bec3aSMark Fasheh 2246d24bec3aSMark Fasheh ret = iterate_inode_refs(inum, fs_root, path, iterate, ctx); 2247d24bec3aSMark Fasheh if (!ret) 2248d24bec3aSMark Fasheh ++found_refs; 2249d24bec3aSMark Fasheh else if (ret != -ENOENT) 2250d24bec3aSMark Fasheh return ret; 2251d24bec3aSMark Fasheh 2252d24bec3aSMark Fasheh ret = iterate_inode_extrefs(inum, fs_root, path, iterate, ctx); 2253d24bec3aSMark Fasheh if (ret == -ENOENT && found_refs) 2254d24bec3aSMark Fasheh return 0; 2255d24bec3aSMark Fasheh 2256d24bec3aSMark Fasheh return ret; 2257d24bec3aSMark Fasheh } 2258d24bec3aSMark Fasheh 2259a542ad1bSJan Schmidt /* 2260a542ad1bSJan Schmidt * returns 0 if the path could be dumped (probably truncated) 2261a542ad1bSJan Schmidt * returns <0 in case of an error 2262a542ad1bSJan Schmidt */ 2263d24bec3aSMark Fasheh static int inode_to_path(u64 inum, u32 name_len, unsigned long name_off, 2264a542ad1bSJan Schmidt struct extent_buffer *eb, void *ctx) 2265a542ad1bSJan Schmidt { 2266a542ad1bSJan Schmidt struct inode_fs_paths *ipath = ctx; 2267a542ad1bSJan Schmidt char *fspath; 2268a542ad1bSJan Schmidt char *fspath_min; 2269a542ad1bSJan Schmidt int i = ipath->fspath->elem_cnt; 2270a542ad1bSJan Schmidt const int s_ptr = sizeof(char *); 2271a542ad1bSJan Schmidt u32 bytes_left; 2272a542ad1bSJan Schmidt 2273a542ad1bSJan Schmidt bytes_left = ipath->fspath->bytes_left > s_ptr ? 2274a542ad1bSJan Schmidt ipath->fspath->bytes_left - s_ptr : 0; 2275a542ad1bSJan Schmidt 2276740c3d22SChris Mason fspath_min = (char *)ipath->fspath->val + (i + 1) * s_ptr; 227796b5bd77SJan Schmidt fspath = btrfs_ref_to_path(ipath->fs_root, ipath->btrfs_path, name_len, 227896b5bd77SJan Schmidt name_off, eb, inum, fspath_min, bytes_left); 2279a542ad1bSJan Schmidt if (IS_ERR(fspath)) 2280a542ad1bSJan Schmidt return PTR_ERR(fspath); 2281a542ad1bSJan Schmidt 2282a542ad1bSJan Schmidt if (fspath > fspath_min) { 2283745c4d8eSJeff Mahoney ipath->fspath->val[i] = (u64)(unsigned long)fspath; 2284a542ad1bSJan Schmidt ++ipath->fspath->elem_cnt; 2285a542ad1bSJan Schmidt ipath->fspath->bytes_left = fspath - fspath_min; 2286a542ad1bSJan Schmidt } else { 2287a542ad1bSJan Schmidt ++ipath->fspath->elem_missed; 2288a542ad1bSJan Schmidt ipath->fspath->bytes_missing += fspath_min - fspath; 2289a542ad1bSJan Schmidt ipath->fspath->bytes_left = 0; 2290a542ad1bSJan Schmidt } 2291a542ad1bSJan Schmidt 2292a542ad1bSJan Schmidt return 0; 2293a542ad1bSJan Schmidt } 2294a542ad1bSJan Schmidt 2295a542ad1bSJan Schmidt /* 2296a542ad1bSJan Schmidt * this dumps all file system paths to the inode into the ipath struct, provided 2297a542ad1bSJan Schmidt * is has been created large enough. each path is zero-terminated and accessed 2298740c3d22SChris Mason * from ipath->fspath->val[i]. 2299a542ad1bSJan Schmidt * when it returns, there are ipath->fspath->elem_cnt number of paths available 2300740c3d22SChris Mason * in ipath->fspath->val[]. when the allocated space wasn't sufficient, the 230101327610SNicholas D Steeves * number of missed paths is recorded in ipath->fspath->elem_missed, otherwise, 2302a542ad1bSJan Schmidt * it's zero. ipath->fspath->bytes_missing holds the number of bytes that would 2303a542ad1bSJan Schmidt * have been needed to return all paths. 2304a542ad1bSJan Schmidt */ 2305a542ad1bSJan Schmidt int paths_from_inode(u64 inum, struct inode_fs_paths *ipath) 2306a542ad1bSJan Schmidt { 2307a542ad1bSJan Schmidt return iterate_irefs(inum, ipath->fs_root, ipath->btrfs_path, 2308a542ad1bSJan Schmidt inode_to_path, ipath); 2309a542ad1bSJan Schmidt } 2310a542ad1bSJan Schmidt 2311a542ad1bSJan Schmidt struct btrfs_data_container *init_data_container(u32 total_bytes) 2312a542ad1bSJan Schmidt { 2313a542ad1bSJan Schmidt struct btrfs_data_container *data; 2314a542ad1bSJan Schmidt size_t alloc_bytes; 2315a542ad1bSJan Schmidt 2316a542ad1bSJan Schmidt alloc_bytes = max_t(size_t, total_bytes, sizeof(*data)); 2317f54de068SDavid Sterba data = kvmalloc(alloc_bytes, GFP_KERNEL); 2318a542ad1bSJan Schmidt if (!data) 2319a542ad1bSJan Schmidt return ERR_PTR(-ENOMEM); 2320a542ad1bSJan Schmidt 2321a542ad1bSJan Schmidt if (total_bytes >= sizeof(*data)) { 2322a542ad1bSJan Schmidt data->bytes_left = total_bytes - sizeof(*data); 2323a542ad1bSJan Schmidt data->bytes_missing = 0; 2324a542ad1bSJan Schmidt } else { 2325a542ad1bSJan Schmidt data->bytes_missing = sizeof(*data) - total_bytes; 2326a542ad1bSJan Schmidt data->bytes_left = 0; 2327a542ad1bSJan Schmidt } 2328a542ad1bSJan Schmidt 2329a542ad1bSJan Schmidt data->elem_cnt = 0; 2330a542ad1bSJan Schmidt data->elem_missed = 0; 2331a542ad1bSJan Schmidt 2332a542ad1bSJan Schmidt return data; 2333a542ad1bSJan Schmidt } 2334a542ad1bSJan Schmidt 2335a542ad1bSJan Schmidt /* 2336a542ad1bSJan Schmidt * allocates space to return multiple file system paths for an inode. 2337a542ad1bSJan Schmidt * total_bytes to allocate are passed, note that space usable for actual path 2338a542ad1bSJan Schmidt * information will be total_bytes - sizeof(struct inode_fs_paths). 2339a542ad1bSJan Schmidt * the returned pointer must be freed with free_ipath() in the end. 2340a542ad1bSJan Schmidt */ 2341a542ad1bSJan Schmidt struct inode_fs_paths *init_ipath(s32 total_bytes, struct btrfs_root *fs_root, 2342a542ad1bSJan Schmidt struct btrfs_path *path) 2343a542ad1bSJan Schmidt { 2344a542ad1bSJan Schmidt struct inode_fs_paths *ifp; 2345a542ad1bSJan Schmidt struct btrfs_data_container *fspath; 2346a542ad1bSJan Schmidt 2347a542ad1bSJan Schmidt fspath = init_data_container(total_bytes); 2348a542ad1bSJan Schmidt if (IS_ERR(fspath)) 2349a542ad1bSJan Schmidt return (void *)fspath; 2350a542ad1bSJan Schmidt 2351f54de068SDavid Sterba ifp = kmalloc(sizeof(*ifp), GFP_KERNEL); 2352a542ad1bSJan Schmidt if (!ifp) { 2353f54de068SDavid Sterba kvfree(fspath); 2354a542ad1bSJan Schmidt return ERR_PTR(-ENOMEM); 2355a542ad1bSJan Schmidt } 2356a542ad1bSJan Schmidt 2357a542ad1bSJan Schmidt ifp->btrfs_path = path; 2358a542ad1bSJan Schmidt ifp->fspath = fspath; 2359a542ad1bSJan Schmidt ifp->fs_root = fs_root; 2360a542ad1bSJan Schmidt 2361a542ad1bSJan Schmidt return ifp; 2362a542ad1bSJan Schmidt } 2363a542ad1bSJan Schmidt 2364a542ad1bSJan Schmidt void free_ipath(struct inode_fs_paths *ipath) 2365a542ad1bSJan Schmidt { 23664735fb28SJesper Juhl if (!ipath) 23674735fb28SJesper Juhl return; 2368f54de068SDavid Sterba kvfree(ipath->fspath); 2369a542ad1bSJan Schmidt kfree(ipath); 2370a542ad1bSJan Schmidt } 2371