Jan Schmidt | a542ad1 | 2011-06-13 19:52:59 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 STRATO. All rights reserved. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public |
| 6 | * License v2 as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 11 | * General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public |
| 14 | * License along with this program; if not, write to the |
| 15 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 16 | * Boston, MA 021110-1307, USA. |
| 17 | */ |
| 18 | |
David Sterba | f54de06 | 2017-05-31 19:32:09 +0200 | [diff] [blame] | 19 | #include <linux/mm.h> |
Lu Fengqi | afce772 | 2016-06-13 09:36:46 +0800 | [diff] [blame] | 20 | #include <linux/rbtree.h> |
Jeff Mahoney | 0014275 | 2017-07-12 16:20:08 -0600 | [diff] [blame] | 21 | #include <trace/events/btrfs.h> |
Jan Schmidt | a542ad1 | 2011-06-13 19:52:59 +0200 | [diff] [blame] | 22 | #include "ctree.h" |
| 23 | #include "disk-io.h" |
| 24 | #include "backref.h" |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 25 | #include "ulist.h" |
| 26 | #include "transaction.h" |
| 27 | #include "delayed-ref.h" |
Jan Schmidt | b916a59 | 2012-04-13 12:28:08 +0200 | [diff] [blame] | 28 | #include "locking.h" |
Jan Schmidt | a542ad1 | 2011-06-13 19:52:59 +0200 | [diff] [blame] | 29 | |
Josef Bacik | dc046b1 | 2014-09-10 16:20:45 -0400 | [diff] [blame] | 30 | /* Just an arbitrary number so we can be sure this happened */ |
| 31 | #define BACKREF_FOUND_SHARED 6 |
| 32 | |
Jan Schmidt | 976b190 | 2012-05-17 16:43:03 +0200 | [diff] [blame] | 33 | struct extent_inode_elem { |
| 34 | u64 inum; |
| 35 | u64 offset; |
| 36 | struct extent_inode_elem *next; |
| 37 | }; |
| 38 | |
Jeff Mahoney | 73980be | 2017-06-28 21:56:55 -0600 | [diff] [blame] | 39 | static int check_extent_in_eb(const struct btrfs_key *key, |
| 40 | const struct extent_buffer *eb, |
| 41 | const struct btrfs_file_extent_item *fi, |
| 42 | u64 extent_item_pos, |
| 43 | struct extent_inode_elem **eie) |
Jan Schmidt | 976b190 | 2012-05-17 16:43:03 +0200 | [diff] [blame] | 44 | { |
Josef Bacik | 8ca15e0 | 2013-07-05 13:58:19 -0400 | [diff] [blame] | 45 | u64 offset = 0; |
Jan Schmidt | 976b190 | 2012-05-17 16:43:03 +0200 | [diff] [blame] | 46 | struct extent_inode_elem *e; |
| 47 | |
Josef Bacik | 8ca15e0 | 2013-07-05 13:58:19 -0400 | [diff] [blame] | 48 | if (!btrfs_file_extent_compression(eb, fi) && |
| 49 | !btrfs_file_extent_encryption(eb, fi) && |
| 50 | !btrfs_file_extent_other_encoding(eb, fi)) { |
| 51 | u64 data_offset; |
| 52 | u64 data_len; |
Jan Schmidt | 976b190 | 2012-05-17 16:43:03 +0200 | [diff] [blame] | 53 | |
Josef Bacik | 8ca15e0 | 2013-07-05 13:58:19 -0400 | [diff] [blame] | 54 | data_offset = btrfs_file_extent_offset(eb, fi); |
| 55 | data_len = btrfs_file_extent_num_bytes(eb, fi); |
| 56 | |
| 57 | if (extent_item_pos < data_offset || |
| 58 | extent_item_pos >= data_offset + data_len) |
| 59 | return 1; |
| 60 | offset = extent_item_pos - data_offset; |
| 61 | } |
Jan Schmidt | 976b190 | 2012-05-17 16:43:03 +0200 | [diff] [blame] | 62 | |
| 63 | e = kmalloc(sizeof(*e), GFP_NOFS); |
| 64 | if (!e) |
| 65 | return -ENOMEM; |
| 66 | |
| 67 | e->next = *eie; |
| 68 | e->inum = key->objectid; |
Josef Bacik | 8ca15e0 | 2013-07-05 13:58:19 -0400 | [diff] [blame] | 69 | e->offset = key->offset + offset; |
Jan Schmidt | 976b190 | 2012-05-17 16:43:03 +0200 | [diff] [blame] | 70 | *eie = e; |
| 71 | |
| 72 | return 0; |
| 73 | } |
| 74 | |
Wang Shilong | f05c474 | 2014-01-28 19:13:38 +0800 | [diff] [blame] | 75 | static void free_inode_elem_list(struct extent_inode_elem *eie) |
| 76 | { |
| 77 | struct extent_inode_elem *eie_next; |
| 78 | |
| 79 | for (; eie; eie = eie_next) { |
| 80 | eie_next = eie->next; |
| 81 | kfree(eie); |
| 82 | } |
| 83 | } |
| 84 | |
Jeff Mahoney | 73980be | 2017-06-28 21:56:55 -0600 | [diff] [blame] | 85 | static int find_extent_in_eb(const struct extent_buffer *eb, |
| 86 | u64 wanted_disk_byte, u64 extent_item_pos, |
| 87 | struct extent_inode_elem **eie) |
Jan Schmidt | 976b190 | 2012-05-17 16:43:03 +0200 | [diff] [blame] | 88 | { |
| 89 | u64 disk_byte; |
| 90 | struct btrfs_key key; |
| 91 | struct btrfs_file_extent_item *fi; |
| 92 | int slot; |
| 93 | int nritems; |
| 94 | int extent_type; |
| 95 | int ret; |
| 96 | |
| 97 | /* |
| 98 | * from the shared data ref, we only have the leaf but we need |
| 99 | * the key. thus, we must look into all items and see that we |
| 100 | * find one (some) with a reference to our extent item. |
| 101 | */ |
| 102 | nritems = btrfs_header_nritems(eb); |
| 103 | for (slot = 0; slot < nritems; ++slot) { |
| 104 | btrfs_item_key_to_cpu(eb, &key, slot); |
| 105 | if (key.type != BTRFS_EXTENT_DATA_KEY) |
| 106 | continue; |
| 107 | fi = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item); |
| 108 | extent_type = btrfs_file_extent_type(eb, fi); |
| 109 | if (extent_type == BTRFS_FILE_EXTENT_INLINE) |
| 110 | continue; |
| 111 | /* don't skip BTRFS_FILE_EXTENT_PREALLOC, we can handle that */ |
| 112 | disk_byte = btrfs_file_extent_disk_bytenr(eb, fi); |
| 113 | if (disk_byte != wanted_disk_byte) |
| 114 | continue; |
| 115 | |
| 116 | ret = check_extent_in_eb(&key, eb, fi, extent_item_pos, eie); |
| 117 | if (ret < 0) |
| 118 | return ret; |
| 119 | } |
| 120 | |
| 121 | return 0; |
| 122 | } |
| 123 | |
Edmund Nadolski | 86d5f99 | 2017-07-12 16:20:06 -0600 | [diff] [blame] | 124 | struct preftree { |
| 125 | struct rb_root root; |
Jeff Mahoney | 6c336b2 | 2017-07-12 16:20:07 -0600 | [diff] [blame] | 126 | unsigned int count; |
Edmund Nadolski | 86d5f99 | 2017-07-12 16:20:06 -0600 | [diff] [blame] | 127 | }; |
| 128 | |
Jeff Mahoney | 6c336b2 | 2017-07-12 16:20:07 -0600 | [diff] [blame] | 129 | #define PREFTREE_INIT { .root = RB_ROOT, .count = 0 } |
Edmund Nadolski | 86d5f99 | 2017-07-12 16:20:06 -0600 | [diff] [blame] | 130 | |
| 131 | struct preftrees { |
| 132 | struct preftree direct; /* BTRFS_SHARED_[DATA|BLOCK]_REF_KEY */ |
| 133 | struct preftree indirect; /* BTRFS_[TREE_BLOCK|EXTENT_DATA]_REF_KEY */ |
| 134 | struct preftree indirect_missing_keys; |
| 135 | }; |
| 136 | |
Wang Shilong | b9e9a6c | 2013-08-09 13:25:36 +0800 | [diff] [blame] | 137 | static struct kmem_cache *btrfs_prelim_ref_cache; |
| 138 | |
| 139 | int __init btrfs_prelim_ref_init(void) |
| 140 | { |
| 141 | btrfs_prelim_ref_cache = kmem_cache_create("btrfs_prelim_ref", |
Jeff Mahoney | e0c476b | 2017-06-28 21:56:57 -0600 | [diff] [blame] | 142 | sizeof(struct prelim_ref), |
Wang Shilong | b9e9a6c | 2013-08-09 13:25:36 +0800 | [diff] [blame] | 143 | 0, |
Nikolay Borisov | fba4b69 | 2016-06-23 21:17:08 +0300 | [diff] [blame] | 144 | SLAB_MEM_SPREAD, |
Wang Shilong | b9e9a6c | 2013-08-09 13:25:36 +0800 | [diff] [blame] | 145 | NULL); |
| 146 | if (!btrfs_prelim_ref_cache) |
| 147 | return -ENOMEM; |
| 148 | return 0; |
| 149 | } |
| 150 | |
| 151 | void btrfs_prelim_ref_exit(void) |
| 152 | { |
Kinglong Mee | 5598e90 | 2016-01-29 21:36:35 +0800 | [diff] [blame] | 153 | kmem_cache_destroy(btrfs_prelim_ref_cache); |
Wang Shilong | b9e9a6c | 2013-08-09 13:25:36 +0800 | [diff] [blame] | 154 | } |
| 155 | |
Edmund Nadolski | 86d5f99 | 2017-07-12 16:20:06 -0600 | [diff] [blame] | 156 | static void free_pref(struct prelim_ref *ref) |
| 157 | { |
| 158 | kmem_cache_free(btrfs_prelim_ref_cache, ref); |
| 159 | } |
| 160 | |
| 161 | /* |
| 162 | * Return 0 when both refs are for the same block (and can be merged). |
| 163 | * A -1 return indicates ref1 is a 'lower' block than ref2, while 1 |
| 164 | * indicates a 'higher' block. |
| 165 | */ |
| 166 | static int prelim_ref_compare(struct prelim_ref *ref1, |
| 167 | struct prelim_ref *ref2) |
| 168 | { |
| 169 | if (ref1->level < ref2->level) |
| 170 | return -1; |
| 171 | if (ref1->level > ref2->level) |
| 172 | return 1; |
| 173 | if (ref1->root_id < ref2->root_id) |
| 174 | return -1; |
| 175 | if (ref1->root_id > ref2->root_id) |
| 176 | return 1; |
| 177 | if (ref1->key_for_search.type < ref2->key_for_search.type) |
| 178 | return -1; |
| 179 | if (ref1->key_for_search.type > ref2->key_for_search.type) |
| 180 | return 1; |
| 181 | if (ref1->key_for_search.objectid < ref2->key_for_search.objectid) |
| 182 | return -1; |
| 183 | if (ref1->key_for_search.objectid > ref2->key_for_search.objectid) |
| 184 | return 1; |
| 185 | if (ref1->key_for_search.offset < ref2->key_for_search.offset) |
| 186 | return -1; |
| 187 | if (ref1->key_for_search.offset > ref2->key_for_search.offset) |
| 188 | return 1; |
| 189 | if (ref1->parent < ref2->parent) |
| 190 | return -1; |
| 191 | if (ref1->parent > ref2->parent) |
| 192 | return 1; |
| 193 | |
| 194 | return 0; |
| 195 | } |
| 196 | |
| 197 | /* |
| 198 | * Add @newref to the @root rbtree, merging identical refs. |
| 199 | * |
| 200 | * Callers should assumed that newref has been freed after calling. |
| 201 | */ |
Jeff Mahoney | 0014275 | 2017-07-12 16:20:08 -0600 | [diff] [blame] | 202 | static void prelim_ref_insert(const struct btrfs_fs_info *fs_info, |
| 203 | struct preftree *preftree, |
Edmund Nadolski | 86d5f99 | 2017-07-12 16:20:06 -0600 | [diff] [blame] | 204 | struct prelim_ref *newref) |
| 205 | { |
| 206 | struct rb_root *root; |
| 207 | struct rb_node **p; |
| 208 | struct rb_node *parent = NULL; |
| 209 | struct prelim_ref *ref; |
| 210 | int result; |
| 211 | |
| 212 | root = &preftree->root; |
| 213 | p = &root->rb_node; |
| 214 | |
| 215 | while (*p) { |
| 216 | parent = *p; |
| 217 | ref = rb_entry(parent, struct prelim_ref, rbnode); |
| 218 | result = prelim_ref_compare(ref, newref); |
| 219 | if (result < 0) { |
| 220 | p = &(*p)->rb_left; |
| 221 | } else if (result > 0) { |
| 222 | p = &(*p)->rb_right; |
| 223 | } else { |
| 224 | /* Identical refs, merge them and free @newref */ |
| 225 | struct extent_inode_elem *eie = ref->inode_list; |
| 226 | |
| 227 | while (eie && eie->next) |
| 228 | eie = eie->next; |
| 229 | |
| 230 | if (!eie) |
| 231 | ref->inode_list = newref->inode_list; |
| 232 | else |
| 233 | eie->next = newref->inode_list; |
Jeff Mahoney | 0014275 | 2017-07-12 16:20:08 -0600 | [diff] [blame] | 234 | trace_btrfs_prelim_ref_merge(fs_info, ref, newref, |
| 235 | preftree->count); |
Edmund Nadolski | 86d5f99 | 2017-07-12 16:20:06 -0600 | [diff] [blame] | 236 | ref->count += newref->count; |
| 237 | free_pref(newref); |
| 238 | return; |
| 239 | } |
| 240 | } |
| 241 | |
Jeff Mahoney | 6c336b2 | 2017-07-12 16:20:07 -0600 | [diff] [blame] | 242 | preftree->count++; |
Jeff Mahoney | 0014275 | 2017-07-12 16:20:08 -0600 | [diff] [blame] | 243 | trace_btrfs_prelim_ref_insert(fs_info, newref, NULL, preftree->count); |
Edmund Nadolski | 86d5f99 | 2017-07-12 16:20:06 -0600 | [diff] [blame] | 244 | rb_link_node(&newref->rbnode, parent, p); |
| 245 | rb_insert_color(&newref->rbnode, root); |
| 246 | } |
| 247 | |
| 248 | /* |
| 249 | * Release the entire tree. We don't care about internal consistency so |
| 250 | * just free everything and then reset the tree root. |
| 251 | */ |
| 252 | static void prelim_release(struct preftree *preftree) |
| 253 | { |
| 254 | struct prelim_ref *ref, *next_ref; |
| 255 | |
| 256 | rbtree_postorder_for_each_entry_safe(ref, next_ref, &preftree->root, |
| 257 | rbnode) |
| 258 | free_pref(ref); |
| 259 | |
| 260 | preftree->root = RB_ROOT; |
Jeff Mahoney | 6c336b2 | 2017-07-12 16:20:07 -0600 | [diff] [blame] | 261 | preftree->count = 0; |
Edmund Nadolski | 86d5f99 | 2017-07-12 16:20:06 -0600 | [diff] [blame] | 262 | } |
| 263 | |
Jan Schmidt | d5c88b7 | 2012-05-15 17:55:51 +0200 | [diff] [blame] | 264 | /* |
| 265 | * the rules for all callers of this function are: |
| 266 | * - obtaining the parent is the goal |
| 267 | * - if you add a key, you must know that it is a correct key |
| 268 | * - if you cannot add the parent or a correct key, then we will look into the |
| 269 | * block later to set a correct key |
| 270 | * |
| 271 | * delayed refs |
| 272 | * ============ |
| 273 | * backref type | shared | indirect | shared | indirect |
| 274 | * information | tree | tree | data | data |
| 275 | * --------------------+--------+----------+--------+---------- |
| 276 | * parent logical | y | - | - | - |
| 277 | * key to resolve | - | y | y | y |
| 278 | * tree block logical | - | - | - | - |
| 279 | * root for resolving | y | y | y | y |
| 280 | * |
| 281 | * - column 1: we've the parent -> done |
| 282 | * - column 2, 3, 4: we use the key to find the parent |
| 283 | * |
| 284 | * on disk refs (inline or keyed) |
| 285 | * ============================== |
| 286 | * backref type | shared | indirect | shared | indirect |
| 287 | * information | tree | tree | data | data |
| 288 | * --------------------+--------+----------+--------+---------- |
| 289 | * parent logical | y | - | y | - |
| 290 | * key to resolve | - | - | - | y |
| 291 | * tree block logical | y | y | y | y |
| 292 | * root for resolving | - | y | y | y |
| 293 | * |
| 294 | * - column 1, 3: we've the parent -> done |
| 295 | * - column 2: we take the first key from the block to find the parent |
Jeff Mahoney | e0c476b | 2017-06-28 21:56:57 -0600 | [diff] [blame] | 296 | * (see add_missing_keys) |
Jan Schmidt | d5c88b7 | 2012-05-15 17:55:51 +0200 | [diff] [blame] | 297 | * - column 4: we use the key to find the parent |
| 298 | * |
| 299 | * additional information that's available but not required to find the parent |
| 300 | * block might help in merging entries to gain some speed. |
| 301 | */ |
Jeff Mahoney | 0014275 | 2017-07-12 16:20:08 -0600 | [diff] [blame] | 302 | static int add_prelim_ref(const struct btrfs_fs_info *fs_info, |
| 303 | struct preftree *preftree, u64 root_id, |
Jeff Mahoney | e0c476b | 2017-06-28 21:56:57 -0600 | [diff] [blame] | 304 | const struct btrfs_key *key, int level, u64 parent, |
| 305 | u64 wanted_disk_byte, int count, gfp_t gfp_mask) |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 306 | { |
Jeff Mahoney | e0c476b | 2017-06-28 21:56:57 -0600 | [diff] [blame] | 307 | struct prelim_ref *ref; |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 308 | |
Liu Bo | 48ec473 | 2013-10-30 13:25:24 +0800 | [diff] [blame] | 309 | if (root_id == BTRFS_DATA_RELOC_TREE_OBJECTID) |
| 310 | return 0; |
| 311 | |
Wang Shilong | b9e9a6c | 2013-08-09 13:25:36 +0800 | [diff] [blame] | 312 | ref = kmem_cache_alloc(btrfs_prelim_ref_cache, gfp_mask); |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 313 | if (!ref) |
| 314 | return -ENOMEM; |
| 315 | |
| 316 | ref->root_id = root_id; |
Filipe Manana | d658910 | 2015-07-29 17:21:17 +0100 | [diff] [blame] | 317 | if (key) { |
Jan Schmidt | d5c88b7 | 2012-05-15 17:55:51 +0200 | [diff] [blame] | 318 | ref->key_for_search = *key; |
Filipe Manana | d658910 | 2015-07-29 17:21:17 +0100 | [diff] [blame] | 319 | /* |
| 320 | * We can often find data backrefs with an offset that is too |
| 321 | * large (>= LLONG_MAX, maximum allowed file offset) due to |
| 322 | * underflows when subtracting a file's offset with the data |
| 323 | * offset of its corresponding extent data item. This can |
| 324 | * happen for example in the clone ioctl. |
| 325 | * So if we detect such case we set the search key's offset to |
| 326 | * zero to make sure we will find the matching file extent item |
| 327 | * at add_all_parents(), otherwise we will miss it because the |
| 328 | * offset taken form the backref is much larger then the offset |
| 329 | * of the file extent item. This can make us scan a very large |
| 330 | * number of file extent items, but at least it will not make |
| 331 | * us miss any. |
| 332 | * This is an ugly workaround for a behaviour that should have |
| 333 | * never existed, but it does and a fix for the clone ioctl |
| 334 | * would touch a lot of places, cause backwards incompatibility |
| 335 | * and would not fix the problem for extents cloned with older |
| 336 | * kernels. |
| 337 | */ |
| 338 | if (ref->key_for_search.type == BTRFS_EXTENT_DATA_KEY && |
| 339 | ref->key_for_search.offset >= LLONG_MAX) |
| 340 | ref->key_for_search.offset = 0; |
| 341 | } else { |
Jan Schmidt | d5c88b7 | 2012-05-15 17:55:51 +0200 | [diff] [blame] | 342 | memset(&ref->key_for_search, 0, sizeof(ref->key_for_search)); |
Filipe Manana | d658910 | 2015-07-29 17:21:17 +0100 | [diff] [blame] | 343 | } |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 344 | |
Jan Schmidt | 3301958 | 2012-05-30 18:05:21 +0200 | [diff] [blame] | 345 | ref->inode_list = NULL; |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 346 | ref->level = level; |
| 347 | ref->count = count; |
| 348 | ref->parent = parent; |
| 349 | ref->wanted_disk_byte = wanted_disk_byte; |
Jeff Mahoney | 0014275 | 2017-07-12 16:20:08 -0600 | [diff] [blame] | 350 | prelim_ref_insert(fs_info, preftree, ref); |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 351 | |
| 352 | return 0; |
| 353 | } |
| 354 | |
Edmund Nadolski | 86d5f99 | 2017-07-12 16:20:06 -0600 | [diff] [blame] | 355 | /* direct refs use root == 0, key == NULL */ |
Jeff Mahoney | 0014275 | 2017-07-12 16:20:08 -0600 | [diff] [blame] | 356 | static int add_direct_ref(const struct btrfs_fs_info *fs_info, |
| 357 | struct preftrees *preftrees, int level, u64 parent, |
Edmund Nadolski | 86d5f99 | 2017-07-12 16:20:06 -0600 | [diff] [blame] | 358 | u64 wanted_disk_byte, int count, gfp_t gfp_mask) |
| 359 | { |
Jeff Mahoney | 0014275 | 2017-07-12 16:20:08 -0600 | [diff] [blame] | 360 | return add_prelim_ref(fs_info, &preftrees->direct, 0, NULL, level, |
| 361 | parent, wanted_disk_byte, count, gfp_mask); |
Edmund Nadolski | 86d5f99 | 2017-07-12 16:20:06 -0600 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | /* indirect refs use parent == 0 */ |
Jeff Mahoney | 0014275 | 2017-07-12 16:20:08 -0600 | [diff] [blame] | 365 | static int add_indirect_ref(const struct btrfs_fs_info *fs_info, |
| 366 | struct preftrees *preftrees, u64 root_id, |
Edmund Nadolski | 86d5f99 | 2017-07-12 16:20:06 -0600 | [diff] [blame] | 367 | const struct btrfs_key *key, int level, |
| 368 | u64 wanted_disk_byte, int count, gfp_t gfp_mask) |
| 369 | { |
| 370 | struct preftree *tree = &preftrees->indirect; |
| 371 | |
| 372 | if (!key) |
| 373 | tree = &preftrees->indirect_missing_keys; |
Jeff Mahoney | 0014275 | 2017-07-12 16:20:08 -0600 | [diff] [blame] | 374 | return add_prelim_ref(fs_info, tree, root_id, key, level, 0, |
Edmund Nadolski | 86d5f99 | 2017-07-12 16:20:06 -0600 | [diff] [blame] | 375 | wanted_disk_byte, count, gfp_mask); |
| 376 | } |
| 377 | |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 378 | static int add_all_parents(struct btrfs_root *root, struct btrfs_path *path, |
Jeff Mahoney | e0c476b | 2017-06-28 21:56:57 -0600 | [diff] [blame] | 379 | struct ulist *parents, struct prelim_ref *ref, |
Josef Bacik | 4485386 | 2014-03-19 13:35:14 -0400 | [diff] [blame] | 380 | int level, u64 time_seq, const u64 *extent_item_pos, |
| 381 | u64 total_refs) |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 382 | { |
Alexander Block | 69bca40 | 2012-06-19 07:42:26 -0600 | [diff] [blame] | 383 | int ret = 0; |
| 384 | int slot; |
| 385 | struct extent_buffer *eb; |
| 386 | struct btrfs_key key; |
Josef Bacik | 7ef81ac | 2014-01-24 14:05:42 -0500 | [diff] [blame] | 387 | struct btrfs_key *key_for_search = &ref->key_for_search; |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 388 | struct btrfs_file_extent_item *fi; |
Josef Bacik | ed8c491 | 2013-07-05 14:03:47 -0400 | [diff] [blame] | 389 | struct extent_inode_elem *eie = NULL, *old = NULL; |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 390 | u64 disk_byte; |
Josef Bacik | 7ef81ac | 2014-01-24 14:05:42 -0500 | [diff] [blame] | 391 | u64 wanted_disk_byte = ref->wanted_disk_byte; |
| 392 | u64 count = 0; |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 393 | |
Alexander Block | 69bca40 | 2012-06-19 07:42:26 -0600 | [diff] [blame] | 394 | if (level != 0) { |
| 395 | eb = path->nodes[level]; |
| 396 | ret = ulist_add(parents, eb->start, 0, GFP_NOFS); |
Jan Schmidt | 3301958 | 2012-05-30 18:05:21 +0200 | [diff] [blame] | 397 | if (ret < 0) |
| 398 | return ret; |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 399 | return 0; |
Alexander Block | 69bca40 | 2012-06-19 07:42:26 -0600 | [diff] [blame] | 400 | } |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 401 | |
| 402 | /* |
Alexander Block | 69bca40 | 2012-06-19 07:42:26 -0600 | [diff] [blame] | 403 | * We normally enter this function with the path already pointing to |
| 404 | * the first item to check. But sometimes, we may enter it with |
| 405 | * slot==nritems. In that case, go to the next leaf before we continue. |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 406 | */ |
Qu Wenruo | 21633fc | 2015-04-16 14:54:50 +0800 | [diff] [blame] | 407 | if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) { |
Edmund Nadolski | de47c9d | 2017-03-16 10:04:34 -0600 | [diff] [blame] | 408 | if (time_seq == SEQ_LAST) |
Qu Wenruo | 21633fc | 2015-04-16 14:54:50 +0800 | [diff] [blame] | 409 | ret = btrfs_next_leaf(root, path); |
| 410 | else |
| 411 | ret = btrfs_next_old_leaf(root, path, time_seq); |
| 412 | } |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 413 | |
Josef Bacik | 4485386 | 2014-03-19 13:35:14 -0400 | [diff] [blame] | 414 | while (!ret && count < total_refs) { |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 415 | eb = path->nodes[0]; |
Alexander Block | 69bca40 | 2012-06-19 07:42:26 -0600 | [diff] [blame] | 416 | slot = path->slots[0]; |
| 417 | |
| 418 | btrfs_item_key_to_cpu(eb, &key, slot); |
| 419 | |
| 420 | if (key.objectid != key_for_search->objectid || |
| 421 | key.type != BTRFS_EXTENT_DATA_KEY) |
| 422 | break; |
| 423 | |
| 424 | fi = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item); |
| 425 | disk_byte = btrfs_file_extent_disk_bytenr(eb, fi); |
| 426 | |
| 427 | if (disk_byte == wanted_disk_byte) { |
| 428 | eie = NULL; |
Josef Bacik | ed8c491 | 2013-07-05 14:03:47 -0400 | [diff] [blame] | 429 | old = NULL; |
Josef Bacik | 7ef81ac | 2014-01-24 14:05:42 -0500 | [diff] [blame] | 430 | count++; |
Alexander Block | 69bca40 | 2012-06-19 07:42:26 -0600 | [diff] [blame] | 431 | if (extent_item_pos) { |
| 432 | ret = check_extent_in_eb(&key, eb, fi, |
| 433 | *extent_item_pos, |
| 434 | &eie); |
| 435 | if (ret < 0) |
| 436 | break; |
| 437 | } |
Josef Bacik | ed8c491 | 2013-07-05 14:03:47 -0400 | [diff] [blame] | 438 | if (ret > 0) |
| 439 | goto next; |
Takashi Iwai | 4eb1f66 | 2014-07-28 10:57:04 +0200 | [diff] [blame] | 440 | ret = ulist_add_merge_ptr(parents, eb->start, |
| 441 | eie, (void **)&old, GFP_NOFS); |
Josef Bacik | ed8c491 | 2013-07-05 14:03:47 -0400 | [diff] [blame] | 442 | if (ret < 0) |
| 443 | break; |
| 444 | if (!ret && extent_item_pos) { |
| 445 | while (old->next) |
| 446 | old = old->next; |
| 447 | old->next = eie; |
Alexander Block | 69bca40 | 2012-06-19 07:42:26 -0600 | [diff] [blame] | 448 | } |
Wang Shilong | f05c474 | 2014-01-28 19:13:38 +0800 | [diff] [blame] | 449 | eie = NULL; |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 450 | } |
Josef Bacik | ed8c491 | 2013-07-05 14:03:47 -0400 | [diff] [blame] | 451 | next: |
Edmund Nadolski | de47c9d | 2017-03-16 10:04:34 -0600 | [diff] [blame] | 452 | if (time_seq == SEQ_LAST) |
Qu Wenruo | 21633fc | 2015-04-16 14:54:50 +0800 | [diff] [blame] | 453 | ret = btrfs_next_item(root, path); |
| 454 | else |
| 455 | ret = btrfs_next_old_item(root, path, time_seq); |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 456 | } |
| 457 | |
Alexander Block | 69bca40 | 2012-06-19 07:42:26 -0600 | [diff] [blame] | 458 | if (ret > 0) |
| 459 | ret = 0; |
Wang Shilong | f05c474 | 2014-01-28 19:13:38 +0800 | [diff] [blame] | 460 | else if (ret < 0) |
| 461 | free_inode_elem_list(eie); |
Alexander Block | 69bca40 | 2012-06-19 07:42:26 -0600 | [diff] [blame] | 462 | return ret; |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 463 | } |
| 464 | |
| 465 | /* |
| 466 | * resolve an indirect backref in the form (root_id, key, level) |
| 467 | * to a logical address |
| 468 | */ |
Jeff Mahoney | e0c476b | 2017-06-28 21:56:57 -0600 | [diff] [blame] | 469 | static int resolve_indirect_ref(struct btrfs_fs_info *fs_info, |
| 470 | struct btrfs_path *path, u64 time_seq, |
| 471 | struct prelim_ref *ref, struct ulist *parents, |
| 472 | const u64 *extent_item_pos, u64 total_refs) |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 473 | { |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 474 | struct btrfs_root *root; |
| 475 | struct btrfs_key root_key; |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 476 | struct extent_buffer *eb; |
| 477 | int ret = 0; |
| 478 | int root_level; |
| 479 | int level = ref->level; |
Wang Shilong | 538f72cd | 2014-01-23 13:47:48 +0800 | [diff] [blame] | 480 | int index; |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 481 | |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 482 | root_key.objectid = ref->root_id; |
| 483 | root_key.type = BTRFS_ROOT_ITEM_KEY; |
| 484 | root_key.offset = (u64)-1; |
Wang Shilong | 538f72cd | 2014-01-23 13:47:48 +0800 | [diff] [blame] | 485 | |
| 486 | index = srcu_read_lock(&fs_info->subvol_srcu); |
| 487 | |
Josef Bacik | 2d9e977 | 2015-11-05 14:37:58 -0800 | [diff] [blame] | 488 | root = btrfs_get_fs_root(fs_info, &root_key, false); |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 489 | if (IS_ERR(root)) { |
Wang Shilong | 538f72cd | 2014-01-23 13:47:48 +0800 | [diff] [blame] | 490 | srcu_read_unlock(&fs_info->subvol_srcu, index); |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 491 | ret = PTR_ERR(root); |
| 492 | goto out; |
| 493 | } |
| 494 | |
Jeff Mahoney | f5ee5c9 | 2016-06-21 09:52:41 -0400 | [diff] [blame] | 495 | if (btrfs_is_testing(fs_info)) { |
Josef Bacik | d9ee522 | 2015-10-05 10:35:29 -0400 | [diff] [blame] | 496 | srcu_read_unlock(&fs_info->subvol_srcu, index); |
| 497 | ret = -ENOENT; |
| 498 | goto out; |
| 499 | } |
| 500 | |
Josef Bacik | 9e351cc | 2014-03-13 15:42:13 -0400 | [diff] [blame] | 501 | if (path->search_commit_root) |
| 502 | root_level = btrfs_header_level(root->commit_root); |
Edmund Nadolski | de47c9d | 2017-03-16 10:04:34 -0600 | [diff] [blame] | 503 | else if (time_seq == SEQ_LAST) |
Qu Wenruo | 21633fc | 2015-04-16 14:54:50 +0800 | [diff] [blame] | 504 | root_level = btrfs_header_level(root->node); |
Josef Bacik | 9e351cc | 2014-03-13 15:42:13 -0400 | [diff] [blame] | 505 | else |
| 506 | root_level = btrfs_old_root_level(root, time_seq); |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 507 | |
Wang Shilong | 538f72cd | 2014-01-23 13:47:48 +0800 | [diff] [blame] | 508 | if (root_level + 1 == level) { |
| 509 | srcu_read_unlock(&fs_info->subvol_srcu, index); |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 510 | goto out; |
Wang Shilong | 538f72cd | 2014-01-23 13:47:48 +0800 | [diff] [blame] | 511 | } |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 512 | |
| 513 | path->lowest_level = level; |
Edmund Nadolski | de47c9d | 2017-03-16 10:04:34 -0600 | [diff] [blame] | 514 | if (time_seq == SEQ_LAST) |
Qu Wenruo | 21633fc | 2015-04-16 14:54:50 +0800 | [diff] [blame] | 515 | ret = btrfs_search_slot(NULL, root, &ref->key_for_search, path, |
| 516 | 0, 0); |
| 517 | else |
| 518 | ret = btrfs_search_old_slot(root, &ref->key_for_search, path, |
| 519 | time_seq); |
Wang Shilong | 538f72cd | 2014-01-23 13:47:48 +0800 | [diff] [blame] | 520 | |
| 521 | /* root node has been locked, we can release @subvol_srcu safely here */ |
| 522 | srcu_read_unlock(&fs_info->subvol_srcu, index); |
| 523 | |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 524 | btrfs_debug(fs_info, |
| 525 | "search slot in root %llu (level %d, ref count %d) returned %d for key (%llu %u %llu)", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 526 | ref->root_id, level, ref->count, ret, |
| 527 | ref->key_for_search.objectid, ref->key_for_search.type, |
| 528 | ref->key_for_search.offset); |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 529 | if (ret < 0) |
| 530 | goto out; |
| 531 | |
| 532 | eb = path->nodes[level]; |
Jan Schmidt | 9345457 | 2012-06-27 15:23:09 +0200 | [diff] [blame] | 533 | while (!eb) { |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 534 | if (WARN_ON(!level)) { |
Jan Schmidt | 9345457 | 2012-06-27 15:23:09 +0200 | [diff] [blame] | 535 | ret = 1; |
| 536 | goto out; |
| 537 | } |
| 538 | level--; |
| 539 | eb = path->nodes[level]; |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 540 | } |
| 541 | |
Josef Bacik | 7ef81ac | 2014-01-24 14:05:42 -0500 | [diff] [blame] | 542 | ret = add_all_parents(root, path, parents, ref, level, time_seq, |
Josef Bacik | 4485386 | 2014-03-19 13:35:14 -0400 | [diff] [blame] | 543 | extent_item_pos, total_refs); |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 544 | out: |
Josef Bacik | da61d31 | 2013-06-12 16:20:08 -0400 | [diff] [blame] | 545 | path->lowest_level = 0; |
| 546 | btrfs_release_path(path); |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 547 | return ret; |
| 548 | } |
| 549 | |
Jeff Mahoney | 4dae077 | 2017-06-28 21:56:56 -0600 | [diff] [blame] | 550 | static struct extent_inode_elem * |
| 551 | unode_aux_to_inode_list(struct ulist_node *node) |
| 552 | { |
| 553 | if (!node) |
| 554 | return NULL; |
| 555 | return (struct extent_inode_elem *)(uintptr_t)node->aux; |
| 556 | } |
| 557 | |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 558 | /* |
Edmund Nadolski | 86d5f99 | 2017-07-12 16:20:06 -0600 | [diff] [blame] | 559 | * We maintain three seperate rbtrees: one for direct refs, one for |
| 560 | * indirect refs which have a key, and one for indirect refs which do not |
| 561 | * have a key. Each tree does merge on insertion. |
| 562 | * |
| 563 | * Once all of the references are located, we iterate over the tree of |
| 564 | * indirect refs with missing keys. An appropriate key is located and |
| 565 | * the ref is moved onto the tree for indirect refs. After all missing |
| 566 | * keys are thus located, we iterate over the indirect ref tree, resolve |
| 567 | * each reference, and then insert the resolved reference onto the |
| 568 | * direct tree (merging there too). |
| 569 | * |
| 570 | * New backrefs (i.e., for parent nodes) are added to the appropriate |
| 571 | * rbtree as they are encountered. The new backrefs are subsequently |
| 572 | * resolved as above. |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 573 | */ |
Jeff Mahoney | e0c476b | 2017-06-28 21:56:57 -0600 | [diff] [blame] | 574 | static int resolve_indirect_refs(struct btrfs_fs_info *fs_info, |
| 575 | struct btrfs_path *path, u64 time_seq, |
Edmund Nadolski | 86d5f99 | 2017-07-12 16:20:06 -0600 | [diff] [blame] | 576 | struct preftrees *preftrees, |
Jeff Mahoney | e0c476b | 2017-06-28 21:56:57 -0600 | [diff] [blame] | 577 | const u64 *extent_item_pos, u64 total_refs, |
| 578 | u64 root_objectid) |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 579 | { |
| 580 | int err; |
| 581 | int ret = 0; |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 582 | struct ulist *parents; |
| 583 | struct ulist_node *node; |
Jan Schmidt | cd1b413 | 2012-05-22 14:56:50 +0200 | [diff] [blame] | 584 | struct ulist_iterator uiter; |
Edmund Nadolski | 86d5f99 | 2017-07-12 16:20:06 -0600 | [diff] [blame] | 585 | struct rb_node *rnode; |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 586 | |
| 587 | parents = ulist_alloc(GFP_NOFS); |
| 588 | if (!parents) |
| 589 | return -ENOMEM; |
| 590 | |
| 591 | /* |
Edmund Nadolski | 86d5f99 | 2017-07-12 16:20:06 -0600 | [diff] [blame] | 592 | * We could trade memory usage for performance here by iterating |
| 593 | * the tree, allocating new refs for each insertion, and then |
| 594 | * freeing the entire indirect tree when we're done. In some test |
| 595 | * cases, the tree can grow quite large (~200k objects). |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 596 | */ |
Edmund Nadolski | 86d5f99 | 2017-07-12 16:20:06 -0600 | [diff] [blame] | 597 | while ((rnode = rb_first(&preftrees->indirect.root))) { |
| 598 | struct prelim_ref *ref; |
| 599 | |
| 600 | ref = rb_entry(rnode, struct prelim_ref, rbnode); |
| 601 | if (WARN(ref->parent, |
| 602 | "BUG: direct ref found in indirect tree")) { |
| 603 | ret = -EINVAL; |
| 604 | goto out; |
| 605 | } |
| 606 | |
| 607 | rb_erase(&ref->rbnode, &preftrees->indirect.root); |
Jeff Mahoney | 6c336b2 | 2017-07-12 16:20:07 -0600 | [diff] [blame] | 608 | preftrees->indirect.count--; |
Edmund Nadolski | 86d5f99 | 2017-07-12 16:20:06 -0600 | [diff] [blame] | 609 | |
| 610 | if (ref->count == 0) { |
| 611 | free_pref(ref); |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 612 | continue; |
Edmund Nadolski | 86d5f99 | 2017-07-12 16:20:06 -0600 | [diff] [blame] | 613 | } |
| 614 | |
Josef Bacik | dc046b1 | 2014-09-10 16:20:45 -0400 | [diff] [blame] | 615 | if (root_objectid && ref->root_id != root_objectid) { |
Edmund Nadolski | 86d5f99 | 2017-07-12 16:20:06 -0600 | [diff] [blame] | 616 | free_pref(ref); |
Josef Bacik | dc046b1 | 2014-09-10 16:20:45 -0400 | [diff] [blame] | 617 | ret = BACKREF_FOUND_SHARED; |
| 618 | goto out; |
| 619 | } |
Jeff Mahoney | e0c476b | 2017-06-28 21:56:57 -0600 | [diff] [blame] | 620 | err = resolve_indirect_ref(fs_info, path, time_seq, ref, |
| 621 | parents, extent_item_pos, |
| 622 | total_refs); |
Wang Shilong | 95def2ed | 2014-01-23 13:47:49 +0800 | [diff] [blame] | 623 | /* |
| 624 | * we can only tolerate ENOENT,otherwise,we should catch error |
| 625 | * and return directly. |
| 626 | */ |
| 627 | if (err == -ENOENT) { |
Jeff Mahoney | 0014275 | 2017-07-12 16:20:08 -0600 | [diff] [blame] | 628 | prelim_ref_insert(fs_info, &preftrees->direct, ref); |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 629 | continue; |
Wang Shilong | 95def2ed | 2014-01-23 13:47:49 +0800 | [diff] [blame] | 630 | } else if (err) { |
Edmund Nadolski | 86d5f99 | 2017-07-12 16:20:06 -0600 | [diff] [blame] | 631 | free_pref(ref); |
Wang Shilong | 95def2ed | 2014-01-23 13:47:49 +0800 | [diff] [blame] | 632 | ret = err; |
| 633 | goto out; |
| 634 | } |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 635 | |
| 636 | /* we put the first parent into the ref at hand */ |
Jan Schmidt | cd1b413 | 2012-05-22 14:56:50 +0200 | [diff] [blame] | 637 | ULIST_ITER_INIT(&uiter); |
| 638 | node = ulist_next(parents, &uiter); |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 639 | ref->parent = node ? node->val : 0; |
Jeff Mahoney | 4dae077 | 2017-06-28 21:56:56 -0600 | [diff] [blame] | 640 | ref->inode_list = unode_aux_to_inode_list(node); |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 641 | |
Edmund Nadolski | 86d5f99 | 2017-07-12 16:20:06 -0600 | [diff] [blame] | 642 | /* Add a prelim_ref(s) for any other parent(s). */ |
Jan Schmidt | cd1b413 | 2012-05-22 14:56:50 +0200 | [diff] [blame] | 643 | while ((node = ulist_next(parents, &uiter))) { |
Edmund Nadolski | 86d5f99 | 2017-07-12 16:20:06 -0600 | [diff] [blame] | 644 | struct prelim_ref *new_ref; |
| 645 | |
Wang Shilong | b9e9a6c | 2013-08-09 13:25:36 +0800 | [diff] [blame] | 646 | new_ref = kmem_cache_alloc(btrfs_prelim_ref_cache, |
| 647 | GFP_NOFS); |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 648 | if (!new_ref) { |
Edmund Nadolski | 86d5f99 | 2017-07-12 16:20:06 -0600 | [diff] [blame] | 649 | free_pref(ref); |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 650 | ret = -ENOMEM; |
Wang Shilong | e36902d | 2013-04-15 10:26:38 +0000 | [diff] [blame] | 651 | goto out; |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 652 | } |
| 653 | memcpy(new_ref, ref, sizeof(*ref)); |
| 654 | new_ref->parent = node->val; |
Jeff Mahoney | 4dae077 | 2017-06-28 21:56:56 -0600 | [diff] [blame] | 655 | new_ref->inode_list = unode_aux_to_inode_list(node); |
Jeff Mahoney | 0014275 | 2017-07-12 16:20:08 -0600 | [diff] [blame] | 656 | prelim_ref_insert(fs_info, &preftrees->direct, new_ref); |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 657 | } |
Edmund Nadolski | 86d5f99 | 2017-07-12 16:20:06 -0600 | [diff] [blame] | 658 | |
| 659 | /* Now it's a direct ref, put it in the the direct tree */ |
Jeff Mahoney | 0014275 | 2017-07-12 16:20:08 -0600 | [diff] [blame] | 660 | prelim_ref_insert(fs_info, &preftrees->direct, ref); |
Edmund Nadolski | 86d5f99 | 2017-07-12 16:20:06 -0600 | [diff] [blame] | 661 | |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 662 | ulist_reinit(parents); |
Edmund Nadolski | 9dd14fd | 2017-07-12 16:20:09 -0600 | [diff] [blame^] | 663 | cond_resched(); |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 664 | } |
Wang Shilong | e36902d | 2013-04-15 10:26:38 +0000 | [diff] [blame] | 665 | out: |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 666 | ulist_free(parents); |
| 667 | return ret; |
| 668 | } |
| 669 | |
Jan Schmidt | d5c88b7 | 2012-05-15 17:55:51 +0200 | [diff] [blame] | 670 | /* |
| 671 | * read tree blocks and add keys where required. |
| 672 | */ |
Jeff Mahoney | e0c476b | 2017-06-28 21:56:57 -0600 | [diff] [blame] | 673 | static int add_missing_keys(struct btrfs_fs_info *fs_info, |
Edmund Nadolski | 86d5f99 | 2017-07-12 16:20:06 -0600 | [diff] [blame] | 674 | struct preftrees *preftrees) |
Jan Schmidt | d5c88b7 | 2012-05-15 17:55:51 +0200 | [diff] [blame] | 675 | { |
Jeff Mahoney | e0c476b | 2017-06-28 21:56:57 -0600 | [diff] [blame] | 676 | struct prelim_ref *ref; |
Jan Schmidt | d5c88b7 | 2012-05-15 17:55:51 +0200 | [diff] [blame] | 677 | struct extent_buffer *eb; |
Edmund Nadolski | 86d5f99 | 2017-07-12 16:20:06 -0600 | [diff] [blame] | 678 | struct preftree *tree = &preftrees->indirect_missing_keys; |
| 679 | struct rb_node *node; |
Jan Schmidt | d5c88b7 | 2012-05-15 17:55:51 +0200 | [diff] [blame] | 680 | |
Edmund Nadolski | 86d5f99 | 2017-07-12 16:20:06 -0600 | [diff] [blame] | 681 | while ((node = rb_first(&tree->root))) { |
| 682 | ref = rb_entry(node, struct prelim_ref, rbnode); |
| 683 | rb_erase(node, &tree->root); |
| 684 | |
| 685 | BUG_ON(ref->parent); /* should not be a direct ref */ |
| 686 | BUG_ON(ref->key_for_search.type); |
Jan Schmidt | d5c88b7 | 2012-05-15 17:55:51 +0200 | [diff] [blame] | 687 | BUG_ON(!ref->wanted_disk_byte); |
Edmund Nadolski | 86d5f99 | 2017-07-12 16:20:06 -0600 | [diff] [blame] | 688 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 689 | eb = read_tree_block(fs_info, ref->wanted_disk_byte, 0); |
Liu Bo | 64c043d | 2015-05-25 17:30:15 +0800 | [diff] [blame] | 690 | if (IS_ERR(eb)) { |
Edmund Nadolski | 86d5f99 | 2017-07-12 16:20:06 -0600 | [diff] [blame] | 691 | free_pref(ref); |
Liu Bo | 64c043d | 2015-05-25 17:30:15 +0800 | [diff] [blame] | 692 | return PTR_ERR(eb); |
| 693 | } else if (!extent_buffer_uptodate(eb)) { |
Edmund Nadolski | 86d5f99 | 2017-07-12 16:20:06 -0600 | [diff] [blame] | 694 | free_pref(ref); |
Josef Bacik | 416bc65 | 2013-04-23 14:17:42 -0400 | [diff] [blame] | 695 | free_extent_buffer(eb); |
| 696 | return -EIO; |
| 697 | } |
Jan Schmidt | d5c88b7 | 2012-05-15 17:55:51 +0200 | [diff] [blame] | 698 | btrfs_tree_read_lock(eb); |
| 699 | if (btrfs_header_level(eb) == 0) |
| 700 | btrfs_item_key_to_cpu(eb, &ref->key_for_search, 0); |
| 701 | else |
| 702 | btrfs_node_key_to_cpu(eb, &ref->key_for_search, 0); |
| 703 | btrfs_tree_read_unlock(eb); |
| 704 | free_extent_buffer(eb); |
Jeff Mahoney | 0014275 | 2017-07-12 16:20:08 -0600 | [diff] [blame] | 705 | prelim_ref_insert(fs_info, &preftrees->indirect, ref); |
Edmund Nadolski | 9dd14fd | 2017-07-12 16:20:09 -0600 | [diff] [blame^] | 706 | cond_resched(); |
Jan Schmidt | d5c88b7 | 2012-05-15 17:55:51 +0200 | [diff] [blame] | 707 | } |
| 708 | return 0; |
| 709 | } |
| 710 | |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 711 | /* |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 712 | * add all currently queued delayed refs from this head whose seq nr is |
| 713 | * smaller or equal that seq to the list |
| 714 | */ |
Jeff Mahoney | 0014275 | 2017-07-12 16:20:08 -0600 | [diff] [blame] | 715 | static int add_delayed_refs(const struct btrfs_fs_info *fs_info, |
| 716 | struct btrfs_delayed_ref_head *head, u64 seq, |
Edmund Nadolski | 86d5f99 | 2017-07-12 16:20:06 -0600 | [diff] [blame] | 717 | struct preftrees *preftrees, u64 *total_refs, |
Jeff Mahoney | e0c476b | 2017-06-28 21:56:57 -0600 | [diff] [blame] | 718 | u64 inum) |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 719 | { |
Qu Wenruo | c6fc245 | 2015-03-30 17:03:00 +0800 | [diff] [blame] | 720 | struct btrfs_delayed_ref_node *node; |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 721 | struct btrfs_delayed_extent_op *extent_op = head->extent_op; |
Jan Schmidt | d5c88b7 | 2012-05-15 17:55:51 +0200 | [diff] [blame] | 722 | struct btrfs_key key; |
Edmund Nadolski | 86d5f99 | 2017-07-12 16:20:06 -0600 | [diff] [blame] | 723 | struct btrfs_key tmp_op_key; |
| 724 | struct btrfs_key *op_key = NULL; |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 725 | int sgn; |
Jan Schmidt | b1375d6 | 2012-01-26 15:01:11 -0500 | [diff] [blame] | 726 | int ret = 0; |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 727 | |
Edmund Nadolski | 86d5f99 | 2017-07-12 16:20:06 -0600 | [diff] [blame] | 728 | if (extent_op && extent_op->update_key) { |
| 729 | btrfs_disk_key_to_cpu(&tmp_op_key, &extent_op->key); |
| 730 | op_key = &tmp_op_key; |
| 731 | } |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 732 | |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 733 | spin_lock(&head->lock); |
Qu Wenruo | c6fc245 | 2015-03-30 17:03:00 +0800 | [diff] [blame] | 734 | list_for_each_entry(node, &head->ref_list, list) { |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 735 | if (node->seq > seq) |
| 736 | continue; |
| 737 | |
| 738 | switch (node->action) { |
| 739 | case BTRFS_ADD_DELAYED_EXTENT: |
| 740 | case BTRFS_UPDATE_DELAYED_HEAD: |
| 741 | WARN_ON(1); |
| 742 | continue; |
| 743 | case BTRFS_ADD_DELAYED_REF: |
| 744 | sgn = 1; |
| 745 | break; |
| 746 | case BTRFS_DROP_DELAYED_REF: |
| 747 | sgn = -1; |
| 748 | break; |
| 749 | default: |
| 750 | BUG_ON(1); |
| 751 | } |
Josef Bacik | 4485386 | 2014-03-19 13:35:14 -0400 | [diff] [blame] | 752 | *total_refs += (node->ref_mod * sgn); |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 753 | switch (node->type) { |
| 754 | case BTRFS_TREE_BLOCK_REF_KEY: { |
Edmund Nadolski | 86d5f99 | 2017-07-12 16:20:06 -0600 | [diff] [blame] | 755 | /* NORMAL INDIRECT METADATA backref */ |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 756 | struct btrfs_delayed_tree_ref *ref; |
| 757 | |
| 758 | ref = btrfs_delayed_node_to_tree_ref(node); |
Jeff Mahoney | 0014275 | 2017-07-12 16:20:08 -0600 | [diff] [blame] | 759 | ret = add_indirect_ref(fs_info, preftrees, ref->root, |
| 760 | &tmp_op_key, ref->level + 1, |
| 761 | node->bytenr, |
Edmund Nadolski | 86d5f99 | 2017-07-12 16:20:06 -0600 | [diff] [blame] | 762 | node->ref_mod * sgn, |
| 763 | GFP_ATOMIC); |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 764 | break; |
| 765 | } |
| 766 | case BTRFS_SHARED_BLOCK_REF_KEY: { |
Edmund Nadolski | 86d5f99 | 2017-07-12 16:20:06 -0600 | [diff] [blame] | 767 | /* SHARED DIRECT METADATA backref */ |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 768 | struct btrfs_delayed_tree_ref *ref; |
| 769 | |
| 770 | ref = btrfs_delayed_node_to_tree_ref(node); |
Edmund Nadolski | 86d5f99 | 2017-07-12 16:20:06 -0600 | [diff] [blame] | 771 | |
Jeff Mahoney | 0014275 | 2017-07-12 16:20:08 -0600 | [diff] [blame] | 772 | ret = add_direct_ref(fs_info, preftrees, |
| 773 | ref->level + 1, ref->parent, |
| 774 | node->bytenr, node->ref_mod * sgn, |
Edmund Nadolski | 86d5f99 | 2017-07-12 16:20:06 -0600 | [diff] [blame] | 775 | GFP_ATOMIC); |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 776 | break; |
| 777 | } |
| 778 | case BTRFS_EXTENT_DATA_REF_KEY: { |
Edmund Nadolski | 86d5f99 | 2017-07-12 16:20:06 -0600 | [diff] [blame] | 779 | /* NORMAL INDIRECT DATA backref */ |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 780 | struct btrfs_delayed_data_ref *ref; |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 781 | ref = btrfs_delayed_node_to_data_ref(node); |
| 782 | |
| 783 | key.objectid = ref->objectid; |
| 784 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 785 | key.offset = ref->offset; |
Josef Bacik | dc046b1 | 2014-09-10 16:20:45 -0400 | [diff] [blame] | 786 | |
| 787 | /* |
| 788 | * Found a inum that doesn't match our known inum, we |
| 789 | * know it's shared. |
| 790 | */ |
| 791 | if (inum && ref->objectid != inum) { |
| 792 | ret = BACKREF_FOUND_SHARED; |
| 793 | break; |
| 794 | } |
| 795 | |
Jeff Mahoney | 0014275 | 2017-07-12 16:20:08 -0600 | [diff] [blame] | 796 | ret = add_indirect_ref(fs_info, preftrees, ref->root, |
| 797 | &key, 0, node->bytenr, |
Edmund Nadolski | 86d5f99 | 2017-07-12 16:20:06 -0600 | [diff] [blame] | 798 | node->ref_mod * sgn, |
| 799 | GFP_ATOMIC); |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 800 | break; |
| 801 | } |
| 802 | case BTRFS_SHARED_DATA_REF_KEY: { |
Edmund Nadolski | 86d5f99 | 2017-07-12 16:20:06 -0600 | [diff] [blame] | 803 | /* SHARED DIRECT FULL backref */ |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 804 | struct btrfs_delayed_data_ref *ref; |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 805 | |
| 806 | ref = btrfs_delayed_node_to_data_ref(node); |
Edmund Nadolski | 86d5f99 | 2017-07-12 16:20:06 -0600 | [diff] [blame] | 807 | |
Jeff Mahoney | 0014275 | 2017-07-12 16:20:08 -0600 | [diff] [blame] | 808 | ret = add_direct_ref(fs_info, preftrees, 0, |
| 809 | ref->parent, node->bytenr, |
Edmund Nadolski | 86d5f99 | 2017-07-12 16:20:06 -0600 | [diff] [blame] | 810 | node->ref_mod * sgn, |
Jeff Mahoney | e0c476b | 2017-06-28 21:56:57 -0600 | [diff] [blame] | 811 | GFP_ATOMIC); |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 812 | break; |
| 813 | } |
| 814 | default: |
| 815 | WARN_ON(1); |
| 816 | } |
Wang Shilong | 1149ab6 | 2013-04-10 11:22:50 +0000 | [diff] [blame] | 817 | if (ret) |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 818 | break; |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 819 | } |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 820 | spin_unlock(&head->lock); |
| 821 | return ret; |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 822 | } |
| 823 | |
| 824 | /* |
| 825 | * add all inline backrefs for bytenr to the list |
| 826 | */ |
Jeff Mahoney | 0014275 | 2017-07-12 16:20:08 -0600 | [diff] [blame] | 827 | static int add_inline_refs(const struct btrfs_fs_info *fs_info, |
| 828 | struct btrfs_path *path, u64 bytenr, |
Edmund Nadolski | 86d5f99 | 2017-07-12 16:20:06 -0600 | [diff] [blame] | 829 | int *info_level, struct preftrees *preftrees, |
Jeff Mahoney | e0c476b | 2017-06-28 21:56:57 -0600 | [diff] [blame] | 830 | u64 *total_refs, u64 inum) |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 831 | { |
Jan Schmidt | b1375d6 | 2012-01-26 15:01:11 -0500 | [diff] [blame] | 832 | int ret = 0; |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 833 | int slot; |
| 834 | struct extent_buffer *leaf; |
| 835 | struct btrfs_key key; |
Josef Bacik | 261c84b | 2013-06-28 13:11:22 -0400 | [diff] [blame] | 836 | struct btrfs_key found_key; |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 837 | unsigned long ptr; |
| 838 | unsigned long end; |
| 839 | struct btrfs_extent_item *ei; |
| 840 | u64 flags; |
| 841 | u64 item_size; |
| 842 | |
| 843 | /* |
| 844 | * enumerate all inline refs |
| 845 | */ |
| 846 | leaf = path->nodes[0]; |
Jan Schmidt | dadcaf7 | 2012-05-22 13:43:25 +0200 | [diff] [blame] | 847 | slot = path->slots[0]; |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 848 | |
| 849 | item_size = btrfs_item_size_nr(leaf, slot); |
| 850 | BUG_ON(item_size < sizeof(*ei)); |
| 851 | |
| 852 | ei = btrfs_item_ptr(leaf, slot, struct btrfs_extent_item); |
| 853 | flags = btrfs_extent_flags(leaf, ei); |
Josef Bacik | 4485386 | 2014-03-19 13:35:14 -0400 | [diff] [blame] | 854 | *total_refs += btrfs_extent_refs(leaf, ei); |
Josef Bacik | 261c84b | 2013-06-28 13:11:22 -0400 | [diff] [blame] | 855 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 856 | |
| 857 | ptr = (unsigned long)(ei + 1); |
| 858 | end = (unsigned long)ei + item_size; |
| 859 | |
Josef Bacik | 261c84b | 2013-06-28 13:11:22 -0400 | [diff] [blame] | 860 | if (found_key.type == BTRFS_EXTENT_ITEM_KEY && |
| 861 | flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) { |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 862 | struct btrfs_tree_block_info *info; |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 863 | |
| 864 | info = (struct btrfs_tree_block_info *)ptr; |
| 865 | *info_level = btrfs_tree_block_level(leaf, info); |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 866 | ptr += sizeof(struct btrfs_tree_block_info); |
| 867 | BUG_ON(ptr > end); |
Josef Bacik | 261c84b | 2013-06-28 13:11:22 -0400 | [diff] [blame] | 868 | } else if (found_key.type == BTRFS_METADATA_ITEM_KEY) { |
| 869 | *info_level = found_key.offset; |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 870 | } else { |
| 871 | BUG_ON(!(flags & BTRFS_EXTENT_FLAG_DATA)); |
| 872 | } |
| 873 | |
| 874 | while (ptr < end) { |
| 875 | struct btrfs_extent_inline_ref *iref; |
| 876 | u64 offset; |
| 877 | int type; |
| 878 | |
| 879 | iref = (struct btrfs_extent_inline_ref *)ptr; |
| 880 | type = btrfs_extent_inline_ref_type(leaf, iref); |
| 881 | offset = btrfs_extent_inline_ref_offset(leaf, iref); |
| 882 | |
| 883 | switch (type) { |
| 884 | case BTRFS_SHARED_BLOCK_REF_KEY: |
Jeff Mahoney | 0014275 | 2017-07-12 16:20:08 -0600 | [diff] [blame] | 885 | ret = add_direct_ref(fs_info, preftrees, |
| 886 | *info_level + 1, offset, |
Edmund Nadolski | 86d5f99 | 2017-07-12 16:20:06 -0600 | [diff] [blame] | 887 | bytenr, 1, GFP_NOFS); |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 888 | break; |
| 889 | case BTRFS_SHARED_DATA_REF_KEY: { |
| 890 | struct btrfs_shared_data_ref *sdref; |
| 891 | int count; |
| 892 | |
| 893 | sdref = (struct btrfs_shared_data_ref *)(iref + 1); |
| 894 | count = btrfs_shared_data_ref_count(leaf, sdref); |
Edmund Nadolski | 86d5f99 | 2017-07-12 16:20:06 -0600 | [diff] [blame] | 895 | |
Jeff Mahoney | 0014275 | 2017-07-12 16:20:08 -0600 | [diff] [blame] | 896 | ret = add_direct_ref(fs_info, preftrees, 0, offset, |
Jeff Mahoney | e0c476b | 2017-06-28 21:56:57 -0600 | [diff] [blame] | 897 | bytenr, count, GFP_NOFS); |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 898 | break; |
| 899 | } |
| 900 | case BTRFS_TREE_BLOCK_REF_KEY: |
Jeff Mahoney | 0014275 | 2017-07-12 16:20:08 -0600 | [diff] [blame] | 901 | ret = add_indirect_ref(fs_info, preftrees, offset, |
| 902 | NULL, *info_level + 1, |
| 903 | bytenr, 1, GFP_NOFS); |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 904 | break; |
| 905 | case BTRFS_EXTENT_DATA_REF_KEY: { |
| 906 | struct btrfs_extent_data_ref *dref; |
| 907 | int count; |
| 908 | u64 root; |
| 909 | |
| 910 | dref = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 911 | count = btrfs_extent_data_ref_count(leaf, dref); |
| 912 | key.objectid = btrfs_extent_data_ref_objectid(leaf, |
| 913 | dref); |
| 914 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 915 | key.offset = btrfs_extent_data_ref_offset(leaf, dref); |
Josef Bacik | dc046b1 | 2014-09-10 16:20:45 -0400 | [diff] [blame] | 916 | |
| 917 | if (inum && key.objectid != inum) { |
| 918 | ret = BACKREF_FOUND_SHARED; |
| 919 | break; |
| 920 | } |
| 921 | |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 922 | root = btrfs_extent_data_ref_root(leaf, dref); |
Edmund Nadolski | 86d5f99 | 2017-07-12 16:20:06 -0600 | [diff] [blame] | 923 | |
Jeff Mahoney | 0014275 | 2017-07-12 16:20:08 -0600 | [diff] [blame] | 924 | ret = add_indirect_ref(fs_info, preftrees, root, |
| 925 | &key, 0, bytenr, count, |
| 926 | GFP_NOFS); |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 927 | break; |
| 928 | } |
| 929 | default: |
| 930 | WARN_ON(1); |
| 931 | } |
Wang Shilong | 1149ab6 | 2013-04-10 11:22:50 +0000 | [diff] [blame] | 932 | if (ret) |
| 933 | return ret; |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 934 | ptr += btrfs_extent_inline_ref_size(type); |
| 935 | } |
| 936 | |
| 937 | return 0; |
| 938 | } |
| 939 | |
| 940 | /* |
| 941 | * add all non-inline backrefs for bytenr to the list |
| 942 | */ |
Jeff Mahoney | e0c476b | 2017-06-28 21:56:57 -0600 | [diff] [blame] | 943 | static int add_keyed_refs(struct btrfs_fs_info *fs_info, |
| 944 | struct btrfs_path *path, u64 bytenr, |
Edmund Nadolski | 86d5f99 | 2017-07-12 16:20:06 -0600 | [diff] [blame] | 945 | int info_level, struct preftrees *preftrees, |
| 946 | u64 inum) |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 947 | { |
| 948 | struct btrfs_root *extent_root = fs_info->extent_root; |
| 949 | int ret; |
| 950 | int slot; |
| 951 | struct extent_buffer *leaf; |
| 952 | struct btrfs_key key; |
| 953 | |
| 954 | while (1) { |
| 955 | ret = btrfs_next_item(extent_root, path); |
| 956 | if (ret < 0) |
| 957 | break; |
| 958 | if (ret) { |
| 959 | ret = 0; |
| 960 | break; |
| 961 | } |
| 962 | |
| 963 | slot = path->slots[0]; |
| 964 | leaf = path->nodes[0]; |
| 965 | btrfs_item_key_to_cpu(leaf, &key, slot); |
| 966 | |
| 967 | if (key.objectid != bytenr) |
| 968 | break; |
| 969 | if (key.type < BTRFS_TREE_BLOCK_REF_KEY) |
| 970 | continue; |
| 971 | if (key.type > BTRFS_SHARED_DATA_REF_KEY) |
| 972 | break; |
| 973 | |
| 974 | switch (key.type) { |
| 975 | case BTRFS_SHARED_BLOCK_REF_KEY: |
Edmund Nadolski | 86d5f99 | 2017-07-12 16:20:06 -0600 | [diff] [blame] | 976 | /* SHARED DIRECT METADATA backref */ |
Jeff Mahoney | 0014275 | 2017-07-12 16:20:08 -0600 | [diff] [blame] | 977 | ret = add_direct_ref(fs_info, preftrees, |
| 978 | info_level + 1, key.offset, |
| 979 | bytenr, 1, GFP_NOFS); |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 980 | break; |
| 981 | case BTRFS_SHARED_DATA_REF_KEY: { |
Edmund Nadolski | 86d5f99 | 2017-07-12 16:20:06 -0600 | [diff] [blame] | 982 | /* SHARED DIRECT FULL backref */ |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 983 | struct btrfs_shared_data_ref *sdref; |
| 984 | int count; |
| 985 | |
| 986 | sdref = btrfs_item_ptr(leaf, slot, |
| 987 | struct btrfs_shared_data_ref); |
| 988 | count = btrfs_shared_data_ref_count(leaf, sdref); |
Jeff Mahoney | 0014275 | 2017-07-12 16:20:08 -0600 | [diff] [blame] | 989 | ret = add_direct_ref(fs_info, preftrees, 0, |
| 990 | key.offset, bytenr, count, |
| 991 | GFP_NOFS); |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 992 | break; |
| 993 | } |
| 994 | case BTRFS_TREE_BLOCK_REF_KEY: |
Edmund Nadolski | 86d5f99 | 2017-07-12 16:20:06 -0600 | [diff] [blame] | 995 | /* NORMAL INDIRECT METADATA backref */ |
Jeff Mahoney | 0014275 | 2017-07-12 16:20:08 -0600 | [diff] [blame] | 996 | ret = add_indirect_ref(fs_info, preftrees, key.offset, |
| 997 | NULL, info_level + 1, bytenr, |
| 998 | 1, GFP_NOFS); |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 999 | break; |
| 1000 | case BTRFS_EXTENT_DATA_REF_KEY: { |
Edmund Nadolski | 86d5f99 | 2017-07-12 16:20:06 -0600 | [diff] [blame] | 1001 | /* NORMAL INDIRECT DATA backref */ |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 1002 | struct btrfs_extent_data_ref *dref; |
| 1003 | int count; |
| 1004 | u64 root; |
| 1005 | |
| 1006 | dref = btrfs_item_ptr(leaf, slot, |
| 1007 | struct btrfs_extent_data_ref); |
| 1008 | count = btrfs_extent_data_ref_count(leaf, dref); |
| 1009 | key.objectid = btrfs_extent_data_ref_objectid(leaf, |
| 1010 | dref); |
| 1011 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 1012 | key.offset = btrfs_extent_data_ref_offset(leaf, dref); |
Josef Bacik | dc046b1 | 2014-09-10 16:20:45 -0400 | [diff] [blame] | 1013 | |
| 1014 | if (inum && key.objectid != inum) { |
| 1015 | ret = BACKREF_FOUND_SHARED; |
| 1016 | break; |
| 1017 | } |
| 1018 | |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 1019 | root = btrfs_extent_data_ref_root(leaf, dref); |
Jeff Mahoney | 0014275 | 2017-07-12 16:20:08 -0600 | [diff] [blame] | 1020 | ret = add_indirect_ref(fs_info, preftrees, root, |
| 1021 | &key, 0, bytenr, count, |
| 1022 | GFP_NOFS); |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 1023 | break; |
| 1024 | } |
| 1025 | default: |
| 1026 | WARN_ON(1); |
| 1027 | } |
Wang Shilong | 1149ab6 | 2013-04-10 11:22:50 +0000 | [diff] [blame] | 1028 | if (ret) |
| 1029 | return ret; |
| 1030 | |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 1031 | } |
| 1032 | |
| 1033 | return ret; |
| 1034 | } |
| 1035 | |
| 1036 | /* |
| 1037 | * this adds all existing backrefs (inline backrefs, backrefs and delayed |
| 1038 | * refs) for the given bytenr to the refs list, merges duplicates and resolves |
| 1039 | * indirect refs to their parent bytenr. |
| 1040 | * When roots are found, they're added to the roots list |
| 1041 | * |
Mark Fasheh | 2c2ed5a | 2015-05-19 12:49:50 -0700 | [diff] [blame] | 1042 | * NOTE: This can return values > 0 |
| 1043 | * |
Edmund Nadolski | de47c9d | 2017-03-16 10:04:34 -0600 | [diff] [blame] | 1044 | * If time_seq is set to SEQ_LAST, it will not search delayed_refs, and behave |
Qu Wenruo | 21633fc | 2015-04-16 14:54:50 +0800 | [diff] [blame] | 1045 | * much like trans == NULL case, the difference only lies in it will not |
| 1046 | * commit root. |
| 1047 | * The special case is for qgroup to search roots in commit_transaction(). |
| 1048 | * |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 1049 | * FIXME some caching might speed things up |
| 1050 | */ |
| 1051 | static int find_parent_nodes(struct btrfs_trans_handle *trans, |
| 1052 | struct btrfs_fs_info *fs_info, u64 bytenr, |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 1053 | u64 time_seq, struct ulist *refs, |
Josef Bacik | dc046b1 | 2014-09-10 16:20:45 -0400 | [diff] [blame] | 1054 | struct ulist *roots, const u64 *extent_item_pos, |
Edmund Nadolski | f695424 | 2017-06-28 21:56:59 -0600 | [diff] [blame] | 1055 | u64 root_objectid, u64 inum) |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 1056 | { |
| 1057 | struct btrfs_key key; |
| 1058 | struct btrfs_path *path; |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 1059 | struct btrfs_delayed_ref_root *delayed_refs = NULL; |
Li Zefan | d3b0106 | 2012-03-03 07:41:15 -0500 | [diff] [blame] | 1060 | struct btrfs_delayed_ref_head *head; |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 1061 | int info_level = 0; |
| 1062 | int ret; |
Jeff Mahoney | e0c476b | 2017-06-28 21:56:57 -0600 | [diff] [blame] | 1063 | struct prelim_ref *ref; |
Edmund Nadolski | 86d5f99 | 2017-07-12 16:20:06 -0600 | [diff] [blame] | 1064 | struct rb_node *node; |
Wang Shilong | f05c474 | 2014-01-28 19:13:38 +0800 | [diff] [blame] | 1065 | struct extent_inode_elem *eie = NULL; |
Edmund Nadolski | 86d5f99 | 2017-07-12 16:20:06 -0600 | [diff] [blame] | 1066 | /* total of both direct AND indirect refs! */ |
Josef Bacik | 4485386 | 2014-03-19 13:35:14 -0400 | [diff] [blame] | 1067 | u64 total_refs = 0; |
Edmund Nadolski | 86d5f99 | 2017-07-12 16:20:06 -0600 | [diff] [blame] | 1068 | struct preftrees preftrees = { |
| 1069 | .direct = PREFTREE_INIT, |
| 1070 | .indirect = PREFTREE_INIT, |
| 1071 | .indirect_missing_keys = PREFTREE_INIT |
| 1072 | }; |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 1073 | |
| 1074 | key.objectid = bytenr; |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 1075 | key.offset = (u64)-1; |
Josef Bacik | 261c84b | 2013-06-28 13:11:22 -0400 | [diff] [blame] | 1076 | if (btrfs_fs_incompat(fs_info, SKINNY_METADATA)) |
| 1077 | key.type = BTRFS_METADATA_ITEM_KEY; |
| 1078 | else |
| 1079 | key.type = BTRFS_EXTENT_ITEM_KEY; |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 1080 | |
| 1081 | path = btrfs_alloc_path(); |
| 1082 | if (!path) |
| 1083 | return -ENOMEM; |
Wang Shilong | e84752d | 2014-02-13 11:19:47 +0800 | [diff] [blame] | 1084 | if (!trans) { |
Josef Bacik | da61d31 | 2013-06-12 16:20:08 -0400 | [diff] [blame] | 1085 | path->search_commit_root = 1; |
Wang Shilong | e84752d | 2014-02-13 11:19:47 +0800 | [diff] [blame] | 1086 | path->skip_locking = 1; |
| 1087 | } |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 1088 | |
Edmund Nadolski | de47c9d | 2017-03-16 10:04:34 -0600 | [diff] [blame] | 1089 | if (time_seq == SEQ_LAST) |
Qu Wenruo | 21633fc | 2015-04-16 14:54:50 +0800 | [diff] [blame] | 1090 | path->skip_locking = 1; |
| 1091 | |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 1092 | /* |
| 1093 | * grab both a lock on the path and a lock on the delayed ref head. |
| 1094 | * We need both to get a consistent picture of how the refs look |
| 1095 | * at a specified point in time |
| 1096 | */ |
| 1097 | again: |
Li Zefan | d3b0106 | 2012-03-03 07:41:15 -0500 | [diff] [blame] | 1098 | head = NULL; |
| 1099 | |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 1100 | ret = btrfs_search_slot(trans, fs_info->extent_root, &key, path, 0, 0); |
| 1101 | if (ret < 0) |
| 1102 | goto out; |
| 1103 | BUG_ON(ret == 0); |
| 1104 | |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 1105 | #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS |
Qu Wenruo | 21633fc | 2015-04-16 14:54:50 +0800 | [diff] [blame] | 1106 | if (trans && likely(trans->type != __TRANS_DUMMY) && |
Edmund Nadolski | de47c9d | 2017-03-16 10:04:34 -0600 | [diff] [blame] | 1107 | time_seq != SEQ_LAST) { |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 1108 | #else |
Edmund Nadolski | de47c9d | 2017-03-16 10:04:34 -0600 | [diff] [blame] | 1109 | if (trans && time_seq != SEQ_LAST) { |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 1110 | #endif |
Jan Schmidt | 7a3ae2f | 2012-03-23 17:32:28 +0100 | [diff] [blame] | 1111 | /* |
| 1112 | * look if there are updates for this ref queued and lock the |
| 1113 | * head |
| 1114 | */ |
| 1115 | delayed_refs = &trans->transaction->delayed_refs; |
| 1116 | spin_lock(&delayed_refs->lock); |
Liu Bo | f72ad18e | 2017-01-30 12:24:37 -0800 | [diff] [blame] | 1117 | head = btrfs_find_delayed_ref_head(delayed_refs, bytenr); |
Jan Schmidt | 7a3ae2f | 2012-03-23 17:32:28 +0100 | [diff] [blame] | 1118 | if (head) { |
| 1119 | if (!mutex_trylock(&head->mutex)) { |
Elena Reshetova | 6df8cdf | 2017-03-03 10:55:15 +0200 | [diff] [blame] | 1120 | refcount_inc(&head->node.refs); |
Jan Schmidt | 7a3ae2f | 2012-03-23 17:32:28 +0100 | [diff] [blame] | 1121 | spin_unlock(&delayed_refs->lock); |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 1122 | |
Jan Schmidt | 7a3ae2f | 2012-03-23 17:32:28 +0100 | [diff] [blame] | 1123 | btrfs_release_path(path); |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 1124 | |
Jan Schmidt | 7a3ae2f | 2012-03-23 17:32:28 +0100 | [diff] [blame] | 1125 | /* |
| 1126 | * Mutex was contended, block until it's |
| 1127 | * released and try again |
| 1128 | */ |
| 1129 | mutex_lock(&head->mutex); |
| 1130 | mutex_unlock(&head->mutex); |
| 1131 | btrfs_put_delayed_ref(&head->node); |
| 1132 | goto again; |
| 1133 | } |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 1134 | spin_unlock(&delayed_refs->lock); |
Jeff Mahoney | 0014275 | 2017-07-12 16:20:08 -0600 | [diff] [blame] | 1135 | ret = add_delayed_refs(fs_info, head, time_seq, |
| 1136 | &preftrees, &total_refs, inum); |
Jan Schmidt | 155725c | 2012-06-22 14:01:00 +0200 | [diff] [blame] | 1137 | mutex_unlock(&head->mutex); |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 1138 | if (ret) |
Jan Schmidt | 7a3ae2f | 2012-03-23 17:32:28 +0100 | [diff] [blame] | 1139 | goto out; |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 1140 | } else { |
| 1141 | spin_unlock(&delayed_refs->lock); |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 1142 | } |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 1143 | } |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 1144 | |
| 1145 | if (path->slots[0]) { |
| 1146 | struct extent_buffer *leaf; |
| 1147 | int slot; |
| 1148 | |
Jan Schmidt | dadcaf7 | 2012-05-22 13:43:25 +0200 | [diff] [blame] | 1149 | path->slots[0]--; |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 1150 | leaf = path->nodes[0]; |
Jan Schmidt | dadcaf7 | 2012-05-22 13:43:25 +0200 | [diff] [blame] | 1151 | slot = path->slots[0]; |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 1152 | btrfs_item_key_to_cpu(leaf, &key, slot); |
| 1153 | if (key.objectid == bytenr && |
Josef Bacik | 261c84b | 2013-06-28 13:11:22 -0400 | [diff] [blame] | 1154 | (key.type == BTRFS_EXTENT_ITEM_KEY || |
| 1155 | key.type == BTRFS_METADATA_ITEM_KEY)) { |
Jeff Mahoney | 0014275 | 2017-07-12 16:20:08 -0600 | [diff] [blame] | 1156 | ret = add_inline_refs(fs_info, path, bytenr, |
| 1157 | &info_level, &preftrees, |
| 1158 | &total_refs, inum); |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 1159 | if (ret) |
| 1160 | goto out; |
Jeff Mahoney | e0c476b | 2017-06-28 21:56:57 -0600 | [diff] [blame] | 1161 | ret = add_keyed_refs(fs_info, path, bytenr, info_level, |
Edmund Nadolski | 86d5f99 | 2017-07-12 16:20:06 -0600 | [diff] [blame] | 1162 | &preftrees, inum); |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 1163 | if (ret) |
| 1164 | goto out; |
| 1165 | } |
| 1166 | } |
Edmund Nadolski | 86d5f99 | 2017-07-12 16:20:06 -0600 | [diff] [blame] | 1167 | |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 1168 | btrfs_release_path(path); |
| 1169 | |
Edmund Nadolski | 86d5f99 | 2017-07-12 16:20:06 -0600 | [diff] [blame] | 1170 | ret = add_missing_keys(fs_info, &preftrees); |
Jan Schmidt | d5c88b7 | 2012-05-15 17:55:51 +0200 | [diff] [blame] | 1171 | if (ret) |
| 1172 | goto out; |
| 1173 | |
Edmund Nadolski | 86d5f99 | 2017-07-12 16:20:06 -0600 | [diff] [blame] | 1174 | WARN_ON(!RB_EMPTY_ROOT(&preftrees.indirect_missing_keys.root)); |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 1175 | |
Edmund Nadolski | 86d5f99 | 2017-07-12 16:20:06 -0600 | [diff] [blame] | 1176 | ret = resolve_indirect_refs(fs_info, path, time_seq, &preftrees, |
Jeff Mahoney | e0c476b | 2017-06-28 21:56:57 -0600 | [diff] [blame] | 1177 | extent_item_pos, total_refs, |
| 1178 | root_objectid); |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 1179 | if (ret) |
| 1180 | goto out; |
| 1181 | |
Edmund Nadolski | 86d5f99 | 2017-07-12 16:20:06 -0600 | [diff] [blame] | 1182 | WARN_ON(!RB_EMPTY_ROOT(&preftrees.indirect.root)); |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 1183 | |
Edmund Nadolski | 86d5f99 | 2017-07-12 16:20:06 -0600 | [diff] [blame] | 1184 | /* |
| 1185 | * This walks the tree of merged and resolved refs. Tree blocks are |
| 1186 | * read in as needed. Unique entries are added to the ulist, and |
| 1187 | * the list of found roots is updated. |
| 1188 | * |
| 1189 | * We release the entire tree in one go before returning. |
| 1190 | */ |
| 1191 | node = rb_first(&preftrees.direct.root); |
| 1192 | while (node) { |
| 1193 | ref = rb_entry(node, struct prelim_ref, rbnode); |
| 1194 | node = rb_next(&ref->rbnode); |
Julia Lawall | 6c1500f | 2012-11-03 20:30:18 +0000 | [diff] [blame] | 1195 | WARN_ON(ref->count < 0); |
Wang Shilong | 98cfee21 | 2014-02-01 00:42:05 +0800 | [diff] [blame] | 1196 | if (roots && ref->count && ref->root_id && ref->parent == 0) { |
Josef Bacik | dc046b1 | 2014-09-10 16:20:45 -0400 | [diff] [blame] | 1197 | if (root_objectid && ref->root_id != root_objectid) { |
| 1198 | ret = BACKREF_FOUND_SHARED; |
| 1199 | goto out; |
| 1200 | } |
| 1201 | |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 1202 | /* no parent == root of tree */ |
| 1203 | ret = ulist_add(roots, ref->root_id, 0, GFP_NOFS); |
Wang Shilong | f172393 | 2013-03-29 23:03:21 +0000 | [diff] [blame] | 1204 | if (ret < 0) |
| 1205 | goto out; |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 1206 | } |
| 1207 | if (ref->count && ref->parent) { |
Josef Bacik | 8a56457 | 2014-06-05 16:08:45 -0400 | [diff] [blame] | 1208 | if (extent_item_pos && !ref->inode_list && |
| 1209 | ref->level == 0) { |
Jan Schmidt | 976b190 | 2012-05-17 16:43:03 +0200 | [diff] [blame] | 1210 | struct extent_buffer *eb; |
David Sterba | 707e8a0 | 2014-06-04 19:22:26 +0200 | [diff] [blame] | 1211 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 1212 | eb = read_tree_block(fs_info, ref->parent, 0); |
Liu Bo | 64c043d | 2015-05-25 17:30:15 +0800 | [diff] [blame] | 1213 | if (IS_ERR(eb)) { |
| 1214 | ret = PTR_ERR(eb); |
| 1215 | goto out; |
| 1216 | } else if (!extent_buffer_uptodate(eb)) { |
Josef Bacik | 416bc65 | 2013-04-23 14:17:42 -0400 | [diff] [blame] | 1217 | free_extent_buffer(eb); |
Wang Shilong | c16c2e2 | 2013-05-08 08:10:25 +0000 | [diff] [blame] | 1218 | ret = -EIO; |
| 1219 | goto out; |
Josef Bacik | 416bc65 | 2013-04-23 14:17:42 -0400 | [diff] [blame] | 1220 | } |
Filipe Manana | 6f7ff6d | 2014-07-02 20:07:54 +0100 | [diff] [blame] | 1221 | btrfs_tree_read_lock(eb); |
| 1222 | btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK); |
Jan Schmidt | 976b190 | 2012-05-17 16:43:03 +0200 | [diff] [blame] | 1223 | ret = find_extent_in_eb(eb, bytenr, |
| 1224 | *extent_item_pos, &eie); |
Filipe Manana | 6f7ff6d | 2014-07-02 20:07:54 +0100 | [diff] [blame] | 1225 | btrfs_tree_read_unlock_blocking(eb); |
Jan Schmidt | 976b190 | 2012-05-17 16:43:03 +0200 | [diff] [blame] | 1226 | free_extent_buffer(eb); |
Filipe David Borba Manana | f5929cd | 2013-07-31 00:26:35 +0100 | [diff] [blame] | 1227 | if (ret < 0) |
| 1228 | goto out; |
| 1229 | ref->inode_list = eie; |
Jan Schmidt | 976b190 | 2012-05-17 16:43:03 +0200 | [diff] [blame] | 1230 | } |
Takashi Iwai | 4eb1f66 | 2014-07-28 10:57:04 +0200 | [diff] [blame] | 1231 | ret = ulist_add_merge_ptr(refs, ref->parent, |
| 1232 | ref->inode_list, |
| 1233 | (void **)&eie, GFP_NOFS); |
Wang Shilong | f172393 | 2013-03-29 23:03:21 +0000 | [diff] [blame] | 1234 | if (ret < 0) |
| 1235 | goto out; |
Jan Schmidt | 3301958 | 2012-05-30 18:05:21 +0200 | [diff] [blame] | 1236 | if (!ret && extent_item_pos) { |
| 1237 | /* |
| 1238 | * we've recorded that parent, so we must extend |
| 1239 | * its inode list here |
| 1240 | */ |
| 1241 | BUG_ON(!eie); |
| 1242 | while (eie->next) |
| 1243 | eie = eie->next; |
| 1244 | eie->next = ref->inode_list; |
| 1245 | } |
Wang Shilong | f05c474 | 2014-01-28 19:13:38 +0800 | [diff] [blame] | 1246 | eie = NULL; |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 1247 | } |
Edmund Nadolski | 9dd14fd | 2017-07-12 16:20:09 -0600 | [diff] [blame^] | 1248 | cond_resched(); |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 1249 | } |
| 1250 | |
| 1251 | out: |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 1252 | btrfs_free_path(path); |
Edmund Nadolski | 86d5f99 | 2017-07-12 16:20:06 -0600 | [diff] [blame] | 1253 | |
| 1254 | prelim_release(&preftrees.direct); |
| 1255 | prelim_release(&preftrees.indirect); |
| 1256 | prelim_release(&preftrees.indirect_missing_keys); |
| 1257 | |
Wang Shilong | f05c474 | 2014-01-28 19:13:38 +0800 | [diff] [blame] | 1258 | if (ret < 0) |
| 1259 | free_inode_elem_list(eie); |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 1260 | return ret; |
| 1261 | } |
| 1262 | |
Jan Schmidt | 976b190 | 2012-05-17 16:43:03 +0200 | [diff] [blame] | 1263 | static void free_leaf_list(struct ulist *blocks) |
| 1264 | { |
| 1265 | struct ulist_node *node = NULL; |
| 1266 | struct extent_inode_elem *eie; |
Jan Schmidt | 976b190 | 2012-05-17 16:43:03 +0200 | [diff] [blame] | 1267 | struct ulist_iterator uiter; |
| 1268 | |
| 1269 | ULIST_ITER_INIT(&uiter); |
| 1270 | while ((node = ulist_next(blocks, &uiter))) { |
| 1271 | if (!node->aux) |
| 1272 | continue; |
Jeff Mahoney | 4dae077 | 2017-06-28 21:56:56 -0600 | [diff] [blame] | 1273 | eie = unode_aux_to_inode_list(node); |
Wang Shilong | f05c474 | 2014-01-28 19:13:38 +0800 | [diff] [blame] | 1274 | free_inode_elem_list(eie); |
Jan Schmidt | 976b190 | 2012-05-17 16:43:03 +0200 | [diff] [blame] | 1275 | node->aux = 0; |
| 1276 | } |
| 1277 | |
| 1278 | ulist_free(blocks); |
| 1279 | } |
| 1280 | |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 1281 | /* |
| 1282 | * Finds all leafs with a reference to the specified combination of bytenr and |
| 1283 | * offset. key_list_head will point to a list of corresponding keys (caller must |
| 1284 | * free each list element). The leafs will be stored in the leafs ulist, which |
| 1285 | * must be freed with ulist_free. |
| 1286 | * |
| 1287 | * returns 0 on success, <0 on error |
| 1288 | */ |
| 1289 | static int btrfs_find_all_leafs(struct btrfs_trans_handle *trans, |
| 1290 | struct btrfs_fs_info *fs_info, u64 bytenr, |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 1291 | u64 time_seq, struct ulist **leafs, |
Jan Schmidt | 976b190 | 2012-05-17 16:43:03 +0200 | [diff] [blame] | 1292 | const u64 *extent_item_pos) |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 1293 | { |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 1294 | int ret; |
| 1295 | |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 1296 | *leafs = ulist_alloc(GFP_NOFS); |
Wang Shilong | 98cfee21 | 2014-02-01 00:42:05 +0800 | [diff] [blame] | 1297 | if (!*leafs) |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 1298 | return -ENOMEM; |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 1299 | |
Lu Fengqi | afce772 | 2016-06-13 09:36:46 +0800 | [diff] [blame] | 1300 | ret = find_parent_nodes(trans, fs_info, bytenr, time_seq, |
Edmund Nadolski | f695424 | 2017-06-28 21:56:59 -0600 | [diff] [blame] | 1301 | *leafs, NULL, extent_item_pos, 0, 0); |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 1302 | if (ret < 0 && ret != -ENOENT) { |
Jan Schmidt | 976b190 | 2012-05-17 16:43:03 +0200 | [diff] [blame] | 1303 | free_leaf_list(*leafs); |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 1304 | return ret; |
| 1305 | } |
| 1306 | |
| 1307 | return 0; |
| 1308 | } |
| 1309 | |
| 1310 | /* |
| 1311 | * walk all backrefs for a given extent to find all roots that reference this |
| 1312 | * extent. Walking a backref means finding all extents that reference this |
| 1313 | * extent and in turn walk the backrefs of those, too. Naturally this is a |
| 1314 | * recursive process, but here it is implemented in an iterative fashion: We |
| 1315 | * find all referencing extents for the extent in question and put them on a |
| 1316 | * list. In turn, we find all referencing extents for those, further appending |
| 1317 | * to the list. The way we iterate the list allows adding more elements after |
| 1318 | * the current while iterating. The process stops when we reach the end of the |
| 1319 | * list. Found roots are added to the roots list. |
| 1320 | * |
| 1321 | * returns 0 on success, < 0 on error. |
| 1322 | */ |
Jeff Mahoney | e0c476b | 2017-06-28 21:56:57 -0600 | [diff] [blame] | 1323 | static int btrfs_find_all_roots_safe(struct btrfs_trans_handle *trans, |
| 1324 | struct btrfs_fs_info *fs_info, u64 bytenr, |
| 1325 | u64 time_seq, struct ulist **roots) |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 1326 | { |
| 1327 | struct ulist *tmp; |
| 1328 | struct ulist_node *node = NULL; |
Jan Schmidt | cd1b413 | 2012-05-22 14:56:50 +0200 | [diff] [blame] | 1329 | struct ulist_iterator uiter; |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 1330 | int ret; |
| 1331 | |
| 1332 | tmp = ulist_alloc(GFP_NOFS); |
| 1333 | if (!tmp) |
| 1334 | return -ENOMEM; |
| 1335 | *roots = ulist_alloc(GFP_NOFS); |
| 1336 | if (!*roots) { |
| 1337 | ulist_free(tmp); |
| 1338 | return -ENOMEM; |
| 1339 | } |
| 1340 | |
Jan Schmidt | cd1b413 | 2012-05-22 14:56:50 +0200 | [diff] [blame] | 1341 | ULIST_ITER_INIT(&uiter); |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 1342 | while (1) { |
Lu Fengqi | afce772 | 2016-06-13 09:36:46 +0800 | [diff] [blame] | 1343 | ret = find_parent_nodes(trans, fs_info, bytenr, time_seq, |
Edmund Nadolski | f695424 | 2017-06-28 21:56:59 -0600 | [diff] [blame] | 1344 | tmp, *roots, NULL, 0, 0); |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 1345 | if (ret < 0 && ret != -ENOENT) { |
| 1346 | ulist_free(tmp); |
| 1347 | ulist_free(*roots); |
| 1348 | return ret; |
| 1349 | } |
Jan Schmidt | cd1b413 | 2012-05-22 14:56:50 +0200 | [diff] [blame] | 1350 | node = ulist_next(tmp, &uiter); |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 1351 | if (!node) |
| 1352 | break; |
| 1353 | bytenr = node->val; |
Wang Shilong | bca1a29 | 2014-01-26 22:32:18 +0800 | [diff] [blame] | 1354 | cond_resched(); |
Jan Schmidt | 8da6d58 | 2011-11-23 18:55:04 +0100 | [diff] [blame] | 1355 | } |
| 1356 | |
| 1357 | ulist_free(tmp); |
| 1358 | return 0; |
| 1359 | } |
| 1360 | |
Josef Bacik | 9e351cc | 2014-03-13 15:42:13 -0400 | [diff] [blame] | 1361 | int btrfs_find_all_roots(struct btrfs_trans_handle *trans, |
| 1362 | struct btrfs_fs_info *fs_info, u64 bytenr, |
| 1363 | u64 time_seq, struct ulist **roots) |
| 1364 | { |
| 1365 | int ret; |
| 1366 | |
| 1367 | if (!trans) |
| 1368 | down_read(&fs_info->commit_root_sem); |
Jeff Mahoney | e0c476b | 2017-06-28 21:56:57 -0600 | [diff] [blame] | 1369 | ret = btrfs_find_all_roots_safe(trans, fs_info, bytenr, |
| 1370 | time_seq, roots); |
Josef Bacik | 9e351cc | 2014-03-13 15:42:13 -0400 | [diff] [blame] | 1371 | if (!trans) |
| 1372 | up_read(&fs_info->commit_root_sem); |
| 1373 | return ret; |
| 1374 | } |
| 1375 | |
Mark Fasheh | 2c2ed5a | 2015-05-19 12:49:50 -0700 | [diff] [blame] | 1376 | /** |
| 1377 | * btrfs_check_shared - tell us whether an extent is shared |
| 1378 | * |
Mark Fasheh | 2c2ed5a | 2015-05-19 12:49:50 -0700 | [diff] [blame] | 1379 | * btrfs_check_shared uses the backref walking code but will short |
| 1380 | * circuit as soon as it finds a root or inode that doesn't match the |
| 1381 | * one passed in. This provides a significant performance benefit for |
| 1382 | * callers (such as fiemap) which want to know whether the extent is |
| 1383 | * shared but do not need a ref count. |
| 1384 | * |
Edmund Nadolski | bb739cf | 2017-06-28 21:56:58 -0600 | [diff] [blame] | 1385 | * This attempts to allocate a transaction in order to account for |
| 1386 | * delayed refs, but continues on even when the alloc fails. |
| 1387 | * |
Mark Fasheh | 2c2ed5a | 2015-05-19 12:49:50 -0700 | [diff] [blame] | 1388 | * Return: 0 if extent is not shared, 1 if it is shared, < 0 on error. |
| 1389 | */ |
Edmund Nadolski | bb739cf | 2017-06-28 21:56:58 -0600 | [diff] [blame] | 1390 | int btrfs_check_shared(struct btrfs_root *root, u64 inum, u64 bytenr) |
Josef Bacik | dc046b1 | 2014-09-10 16:20:45 -0400 | [diff] [blame] | 1391 | { |
Edmund Nadolski | bb739cf | 2017-06-28 21:56:58 -0600 | [diff] [blame] | 1392 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 1393 | struct btrfs_trans_handle *trans; |
Josef Bacik | dc046b1 | 2014-09-10 16:20:45 -0400 | [diff] [blame] | 1394 | struct ulist *tmp = NULL; |
| 1395 | struct ulist *roots = NULL; |
| 1396 | struct ulist_iterator uiter; |
| 1397 | struct ulist_node *node; |
David Sterba | 3284da7 | 2015-02-25 15:47:32 +0100 | [diff] [blame] | 1398 | struct seq_list elem = SEQ_LIST_INIT(elem); |
Josef Bacik | dc046b1 | 2014-09-10 16:20:45 -0400 | [diff] [blame] | 1399 | int ret = 0; |
| 1400 | |
| 1401 | tmp = ulist_alloc(GFP_NOFS); |
| 1402 | roots = ulist_alloc(GFP_NOFS); |
| 1403 | if (!tmp || !roots) { |
| 1404 | ulist_free(tmp); |
| 1405 | ulist_free(roots); |
| 1406 | return -ENOMEM; |
| 1407 | } |
| 1408 | |
Edmund Nadolski | bb739cf | 2017-06-28 21:56:58 -0600 | [diff] [blame] | 1409 | trans = btrfs_join_transaction(root); |
| 1410 | if (IS_ERR(trans)) { |
| 1411 | trans = NULL; |
Josef Bacik | dc046b1 | 2014-09-10 16:20:45 -0400 | [diff] [blame] | 1412 | down_read(&fs_info->commit_root_sem); |
Edmund Nadolski | bb739cf | 2017-06-28 21:56:58 -0600 | [diff] [blame] | 1413 | } else { |
| 1414 | btrfs_get_tree_mod_seq(fs_info, &elem); |
| 1415 | } |
| 1416 | |
Josef Bacik | dc046b1 | 2014-09-10 16:20:45 -0400 | [diff] [blame] | 1417 | ULIST_ITER_INIT(&uiter); |
| 1418 | while (1) { |
| 1419 | ret = find_parent_nodes(trans, fs_info, bytenr, elem.seq, tmp, |
Edmund Nadolski | f695424 | 2017-06-28 21:56:59 -0600 | [diff] [blame] | 1420 | roots, NULL, root->objectid, inum); |
Josef Bacik | dc046b1 | 2014-09-10 16:20:45 -0400 | [diff] [blame] | 1421 | if (ret == BACKREF_FOUND_SHARED) { |
Mark Fasheh | 2c2ed5a | 2015-05-19 12:49:50 -0700 | [diff] [blame] | 1422 | /* this is the only condition under which we return 1 */ |
Josef Bacik | dc046b1 | 2014-09-10 16:20:45 -0400 | [diff] [blame] | 1423 | ret = 1; |
| 1424 | break; |
| 1425 | } |
| 1426 | if (ret < 0 && ret != -ENOENT) |
| 1427 | break; |
Mark Fasheh | 2c2ed5a | 2015-05-19 12:49:50 -0700 | [diff] [blame] | 1428 | ret = 0; |
Josef Bacik | dc046b1 | 2014-09-10 16:20:45 -0400 | [diff] [blame] | 1429 | node = ulist_next(tmp, &uiter); |
| 1430 | if (!node) |
| 1431 | break; |
| 1432 | bytenr = node->val; |
| 1433 | cond_resched(); |
| 1434 | } |
Edmund Nadolski | bb739cf | 2017-06-28 21:56:58 -0600 | [diff] [blame] | 1435 | |
| 1436 | if (trans) { |
Josef Bacik | dc046b1 | 2014-09-10 16:20:45 -0400 | [diff] [blame] | 1437 | btrfs_put_tree_mod_seq(fs_info, &elem); |
Edmund Nadolski | bb739cf | 2017-06-28 21:56:58 -0600 | [diff] [blame] | 1438 | btrfs_end_transaction(trans); |
| 1439 | } else { |
Josef Bacik | dc046b1 | 2014-09-10 16:20:45 -0400 | [diff] [blame] | 1440 | up_read(&fs_info->commit_root_sem); |
Edmund Nadolski | bb739cf | 2017-06-28 21:56:58 -0600 | [diff] [blame] | 1441 | } |
Josef Bacik | dc046b1 | 2014-09-10 16:20:45 -0400 | [diff] [blame] | 1442 | ulist_free(tmp); |
| 1443 | ulist_free(roots); |
| 1444 | return ret; |
| 1445 | } |
| 1446 | |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1447 | int btrfs_find_one_extref(struct btrfs_root *root, u64 inode_objectid, |
| 1448 | u64 start_off, struct btrfs_path *path, |
| 1449 | struct btrfs_inode_extref **ret_extref, |
| 1450 | u64 *found_off) |
| 1451 | { |
| 1452 | int ret, slot; |
| 1453 | struct btrfs_key key; |
| 1454 | struct btrfs_key found_key; |
| 1455 | struct btrfs_inode_extref *extref; |
Jeff Mahoney | 73980be | 2017-06-28 21:56:55 -0600 | [diff] [blame] | 1456 | const struct extent_buffer *leaf; |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1457 | unsigned long ptr; |
| 1458 | |
| 1459 | key.objectid = inode_objectid; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 1460 | key.type = BTRFS_INODE_EXTREF_KEY; |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1461 | key.offset = start_off; |
| 1462 | |
| 1463 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 1464 | if (ret < 0) |
| 1465 | return ret; |
| 1466 | |
| 1467 | while (1) { |
| 1468 | leaf = path->nodes[0]; |
| 1469 | slot = path->slots[0]; |
| 1470 | if (slot >= btrfs_header_nritems(leaf)) { |
| 1471 | /* |
| 1472 | * If the item at offset is not found, |
| 1473 | * btrfs_search_slot will point us to the slot |
| 1474 | * where it should be inserted. In our case |
| 1475 | * that will be the slot directly before the |
| 1476 | * next INODE_REF_KEY_V2 item. In the case |
| 1477 | * that we're pointing to the last slot in a |
| 1478 | * leaf, we must move one leaf over. |
| 1479 | */ |
| 1480 | ret = btrfs_next_leaf(root, path); |
| 1481 | if (ret) { |
| 1482 | if (ret >= 1) |
| 1483 | ret = -ENOENT; |
| 1484 | break; |
| 1485 | } |
| 1486 | continue; |
| 1487 | } |
| 1488 | |
| 1489 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 1490 | |
| 1491 | /* |
| 1492 | * Check that we're still looking at an extended ref key for |
| 1493 | * this particular objectid. If we have different |
| 1494 | * objectid or type then there are no more to be found |
| 1495 | * in the tree and we can exit. |
| 1496 | */ |
| 1497 | ret = -ENOENT; |
| 1498 | if (found_key.objectid != inode_objectid) |
| 1499 | break; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 1500 | if (found_key.type != BTRFS_INODE_EXTREF_KEY) |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 1501 | break; |
| 1502 | |
| 1503 | ret = 0; |
| 1504 | ptr = btrfs_item_ptr_offset(leaf, path->slots[0]); |
| 1505 | extref = (struct btrfs_inode_extref *)ptr; |
| 1506 | *ret_extref = extref; |
| 1507 | if (found_off) |
| 1508 | *found_off = found_key.offset; |
| 1509 | break; |
| 1510 | } |
| 1511 | |
| 1512 | return ret; |
| 1513 | } |
| 1514 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 1515 | /* |
| 1516 | * this iterates to turn a name (from iref/extref) into a full filesystem path. |
| 1517 | * Elements of the path are separated by '/' and the path is guaranteed to be |
| 1518 | * 0-terminated. the path is only given within the current file system. |
| 1519 | * Therefore, it never starts with a '/'. the caller is responsible to provide |
| 1520 | * "size" bytes in "dest". the dest buffer will be filled backwards. finally, |
| 1521 | * the start point of the resulting string is returned. this pointer is within |
| 1522 | * dest, normally. |
| 1523 | * in case the path buffer would overflow, the pointer is decremented further |
| 1524 | * as if output was written to the buffer, though no more output is actually |
| 1525 | * generated. that way, the caller can determine how much space would be |
| 1526 | * required for the path to fit into the buffer. in that case, the returned |
| 1527 | * value will be smaller than dest. callers must check this! |
| 1528 | */ |
Jan Schmidt | 96b5bd7 | 2012-10-15 08:30:45 +0000 | [diff] [blame] | 1529 | char *btrfs_ref_to_path(struct btrfs_root *fs_root, struct btrfs_path *path, |
| 1530 | u32 name_len, unsigned long name_off, |
| 1531 | struct extent_buffer *eb_in, u64 parent, |
| 1532 | char *dest, u32 size) |
Jan Schmidt | a542ad1 | 2011-06-13 19:52:59 +0200 | [diff] [blame] | 1533 | { |
Jan Schmidt | a542ad1 | 2011-06-13 19:52:59 +0200 | [diff] [blame] | 1534 | int slot; |
| 1535 | u64 next_inum; |
| 1536 | int ret; |
Gabriel de Perthuis | 661bec6 | 2012-10-10 08:50:47 -0600 | [diff] [blame] | 1537 | s64 bytes_left = ((s64)size) - 1; |
Jan Schmidt | a542ad1 | 2011-06-13 19:52:59 +0200 | [diff] [blame] | 1538 | struct extent_buffer *eb = eb_in; |
| 1539 | struct btrfs_key found_key; |
Jan Schmidt | b916a59 | 2012-04-13 12:28:08 +0200 | [diff] [blame] | 1540 | int leave_spinning = path->leave_spinning; |
Mark Fasheh | d24bec3 | 2012-08-08 11:33:54 -0700 | [diff] [blame] | 1541 | struct btrfs_inode_ref *iref; |
Jan Schmidt | a542ad1 | 2011-06-13 19:52:59 +0200 | [diff] [blame] | 1542 | |
| 1543 | if (bytes_left >= 0) |
| 1544 | dest[bytes_left] = '\0'; |
| 1545 | |
Jan Schmidt | b916a59 | 2012-04-13 12:28:08 +0200 | [diff] [blame] | 1546 | path->leave_spinning = 1; |
Jan Schmidt | a542ad1 | 2011-06-13 19:52:59 +0200 | [diff] [blame] | 1547 | while (1) { |
Mark Fasheh | d24bec3 | 2012-08-08 11:33:54 -0700 | [diff] [blame] | 1548 | bytes_left -= name_len; |
Jan Schmidt | a542ad1 | 2011-06-13 19:52:59 +0200 | [diff] [blame] | 1549 | if (bytes_left >= 0) |
| 1550 | read_extent_buffer(eb, dest + bytes_left, |
Mark Fasheh | d24bec3 | 2012-08-08 11:33:54 -0700 | [diff] [blame] | 1551 | name_off, name_len); |
Jan Schmidt | b916a59 | 2012-04-13 12:28:08 +0200 | [diff] [blame] | 1552 | if (eb != eb_in) { |
Filipe Manana | 0c0fe3b | 2016-02-03 19:17:27 +0000 | [diff] [blame] | 1553 | if (!path->skip_locking) |
| 1554 | btrfs_tree_read_unlock_blocking(eb); |
Jan Schmidt | a542ad1 | 2011-06-13 19:52:59 +0200 | [diff] [blame] | 1555 | free_extent_buffer(eb); |
Jan Schmidt | b916a59 | 2012-04-13 12:28:08 +0200 | [diff] [blame] | 1556 | } |
David Sterba | c234a24 | 2015-01-02 19:03:17 +0100 | [diff] [blame] | 1557 | ret = btrfs_find_item(fs_root, path, parent, 0, |
| 1558 | BTRFS_INODE_REF_KEY, &found_key); |
Jan Schmidt | 8f24b49 | 2012-02-08 16:01:01 +0100 | [diff] [blame] | 1559 | if (ret > 0) |
| 1560 | ret = -ENOENT; |
Jan Schmidt | a542ad1 | 2011-06-13 19:52:59 +0200 | [diff] [blame] | 1561 | if (ret) |
| 1562 | break; |
Mark Fasheh | d24bec3 | 2012-08-08 11:33:54 -0700 | [diff] [blame] | 1563 | |
Jan Schmidt | a542ad1 | 2011-06-13 19:52:59 +0200 | [diff] [blame] | 1564 | next_inum = found_key.offset; |
| 1565 | |
| 1566 | /* regular exit ahead */ |
| 1567 | if (parent == next_inum) |
| 1568 | break; |
| 1569 | |
| 1570 | slot = path->slots[0]; |
| 1571 | eb = path->nodes[0]; |
| 1572 | /* make sure we can use eb after releasing the path */ |
Jan Schmidt | b916a59 | 2012-04-13 12:28:08 +0200 | [diff] [blame] | 1573 | if (eb != eb_in) { |
Filipe Manana | 0c0fe3b | 2016-02-03 19:17:27 +0000 | [diff] [blame] | 1574 | if (!path->skip_locking) |
| 1575 | btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK); |
| 1576 | path->nodes[0] = NULL; |
| 1577 | path->locks[0] = 0; |
Jan Schmidt | b916a59 | 2012-04-13 12:28:08 +0200 | [diff] [blame] | 1578 | } |
Jan Schmidt | a542ad1 | 2011-06-13 19:52:59 +0200 | [diff] [blame] | 1579 | btrfs_release_path(path); |
Jan Schmidt | a542ad1 | 2011-06-13 19:52:59 +0200 | [diff] [blame] | 1580 | iref = btrfs_item_ptr(eb, slot, struct btrfs_inode_ref); |
Mark Fasheh | d24bec3 | 2012-08-08 11:33:54 -0700 | [diff] [blame] | 1581 | |
| 1582 | name_len = btrfs_inode_ref_name_len(eb, iref); |
| 1583 | name_off = (unsigned long)(iref + 1); |
| 1584 | |
Jan Schmidt | a542ad1 | 2011-06-13 19:52:59 +0200 | [diff] [blame] | 1585 | parent = next_inum; |
| 1586 | --bytes_left; |
| 1587 | if (bytes_left >= 0) |
| 1588 | dest[bytes_left] = '/'; |
| 1589 | } |
| 1590 | |
| 1591 | btrfs_release_path(path); |
Jan Schmidt | b916a59 | 2012-04-13 12:28:08 +0200 | [diff] [blame] | 1592 | path->leave_spinning = leave_spinning; |
Jan Schmidt | a542ad1 | 2011-06-13 19:52:59 +0200 | [diff] [blame] | 1593 | |
| 1594 | if (ret) |
| 1595 | return ERR_PTR(ret); |
| 1596 | |
| 1597 | return dest + bytes_left; |
| 1598 | } |
| 1599 | |
| 1600 | /* |
| 1601 | * this makes the path point to (logical EXTENT_ITEM *) |
| 1602 | * returns BTRFS_EXTENT_FLAG_DATA for data, BTRFS_EXTENT_FLAG_TREE_BLOCK for |
| 1603 | * tree blocks and <0 on error. |
| 1604 | */ |
| 1605 | int extent_from_logical(struct btrfs_fs_info *fs_info, u64 logical, |
Liu Bo | 69917e4 | 2012-09-07 20:01:28 -0600 | [diff] [blame] | 1606 | struct btrfs_path *path, struct btrfs_key *found_key, |
| 1607 | u64 *flags_ret) |
Jan Schmidt | a542ad1 | 2011-06-13 19:52:59 +0200 | [diff] [blame] | 1608 | { |
| 1609 | int ret; |
| 1610 | u64 flags; |
Josef Bacik | 261c84b | 2013-06-28 13:11:22 -0400 | [diff] [blame] | 1611 | u64 size = 0; |
Jan Schmidt | a542ad1 | 2011-06-13 19:52:59 +0200 | [diff] [blame] | 1612 | u32 item_size; |
Jeff Mahoney | 73980be | 2017-06-28 21:56:55 -0600 | [diff] [blame] | 1613 | const struct extent_buffer *eb; |
Jan Schmidt | a542ad1 | 2011-06-13 19:52:59 +0200 | [diff] [blame] | 1614 | struct btrfs_extent_item *ei; |
| 1615 | struct btrfs_key key; |
| 1616 | |
Josef Bacik | 261c84b | 2013-06-28 13:11:22 -0400 | [diff] [blame] | 1617 | if (btrfs_fs_incompat(fs_info, SKINNY_METADATA)) |
| 1618 | key.type = BTRFS_METADATA_ITEM_KEY; |
| 1619 | else |
| 1620 | key.type = BTRFS_EXTENT_ITEM_KEY; |
Jan Schmidt | a542ad1 | 2011-06-13 19:52:59 +0200 | [diff] [blame] | 1621 | key.objectid = logical; |
| 1622 | key.offset = (u64)-1; |
| 1623 | |
| 1624 | ret = btrfs_search_slot(NULL, fs_info->extent_root, &key, path, 0, 0); |
| 1625 | if (ret < 0) |
| 1626 | return ret; |
Jan Schmidt | a542ad1 | 2011-06-13 19:52:59 +0200 | [diff] [blame] | 1627 | |
Wang Shilong | 850a8cd | 2014-02-06 20:02:29 +0800 | [diff] [blame] | 1628 | ret = btrfs_previous_extent_item(fs_info->extent_root, path, 0); |
| 1629 | if (ret) { |
| 1630 | if (ret > 0) |
| 1631 | ret = -ENOENT; |
| 1632 | return ret; |
Josef Bacik | 580f0a6 | 2014-01-23 16:03:45 -0500 | [diff] [blame] | 1633 | } |
Wang Shilong | 850a8cd | 2014-02-06 20:02:29 +0800 | [diff] [blame] | 1634 | btrfs_item_key_to_cpu(path->nodes[0], found_key, path->slots[0]); |
Josef Bacik | 261c84b | 2013-06-28 13:11:22 -0400 | [diff] [blame] | 1635 | if (found_key->type == BTRFS_METADATA_ITEM_KEY) |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 1636 | size = fs_info->nodesize; |
Josef Bacik | 261c84b | 2013-06-28 13:11:22 -0400 | [diff] [blame] | 1637 | else if (found_key->type == BTRFS_EXTENT_ITEM_KEY) |
| 1638 | size = found_key->offset; |
| 1639 | |
Josef Bacik | 580f0a6 | 2014-01-23 16:03:45 -0500 | [diff] [blame] | 1640 | if (found_key->objectid > logical || |
Josef Bacik | 261c84b | 2013-06-28 13:11:22 -0400 | [diff] [blame] | 1641 | found_key->objectid + size <= logical) { |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 1642 | btrfs_debug(fs_info, |
| 1643 | "logical %llu is not within any extent", logical); |
Jan Schmidt | a542ad1 | 2011-06-13 19:52:59 +0200 | [diff] [blame] | 1644 | return -ENOENT; |
Jan Schmidt | 4692cf5 | 2011-12-02 14:56:41 +0100 | [diff] [blame] | 1645 | } |
Jan Schmidt | a542ad1 | 2011-06-13 19:52:59 +0200 | [diff] [blame] | 1646 | |
| 1647 | eb = path->nodes[0]; |
| 1648 | item_size = btrfs_item_size_nr(eb, path->slots[0]); |
| 1649 | BUG_ON(item_size < sizeof(*ei)); |
| 1650 | |
| 1651 | ei = btrfs_item_ptr(eb, path->slots[0], struct btrfs_extent_item); |
| 1652 | flags = btrfs_extent_flags(eb, ei); |
| 1653 | |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 1654 | btrfs_debug(fs_info, |
| 1655 | "logical %llu is at position %llu within the extent (%llu EXTENT_ITEM %llu) flags %#llx size %u", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 1656 | logical, logical - found_key->objectid, found_key->objectid, |
| 1657 | found_key->offset, flags, item_size); |
Liu Bo | 69917e4 | 2012-09-07 20:01:28 -0600 | [diff] [blame] | 1658 | |
| 1659 | WARN_ON(!flags_ret); |
| 1660 | if (flags_ret) { |
| 1661 | if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) |
| 1662 | *flags_ret = BTRFS_EXTENT_FLAG_TREE_BLOCK; |
| 1663 | else if (flags & BTRFS_EXTENT_FLAG_DATA) |
| 1664 | *flags_ret = BTRFS_EXTENT_FLAG_DATA; |
| 1665 | else |
| 1666 | BUG_ON(1); |
| 1667 | return 0; |
| 1668 | } |
Jan Schmidt | a542ad1 | 2011-06-13 19:52:59 +0200 | [diff] [blame] | 1669 | |
| 1670 | return -EIO; |
| 1671 | } |
| 1672 | |
| 1673 | /* |
| 1674 | * helper function to iterate extent inline refs. ptr must point to a 0 value |
| 1675 | * for the first call and may be modified. it is used to track state. |
| 1676 | * if more refs exist, 0 is returned and the next call to |
Jeff Mahoney | e0c476b | 2017-06-28 21:56:57 -0600 | [diff] [blame] | 1677 | * get_extent_inline_ref must pass the modified ptr parameter to get the |
Jan Schmidt | a542ad1 | 2011-06-13 19:52:59 +0200 | [diff] [blame] | 1678 | * next ref. after the last ref was processed, 1 is returned. |
| 1679 | * returns <0 on error |
| 1680 | */ |
Jeff Mahoney | e0c476b | 2017-06-28 21:56:57 -0600 | [diff] [blame] | 1681 | static int get_extent_inline_ref(unsigned long *ptr, |
| 1682 | const struct extent_buffer *eb, |
| 1683 | const struct btrfs_key *key, |
| 1684 | const struct btrfs_extent_item *ei, |
| 1685 | u32 item_size, |
| 1686 | struct btrfs_extent_inline_ref **out_eiref, |
| 1687 | int *out_type) |
Jan Schmidt | a542ad1 | 2011-06-13 19:52:59 +0200 | [diff] [blame] | 1688 | { |
| 1689 | unsigned long end; |
| 1690 | u64 flags; |
| 1691 | struct btrfs_tree_block_info *info; |
| 1692 | |
| 1693 | if (!*ptr) { |
| 1694 | /* first call */ |
| 1695 | flags = btrfs_extent_flags(eb, ei); |
| 1696 | if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) { |
Liu Bo | 6eda71d | 2014-06-09 10:54:07 +0800 | [diff] [blame] | 1697 | if (key->type == BTRFS_METADATA_ITEM_KEY) { |
| 1698 | /* a skinny metadata extent */ |
| 1699 | *out_eiref = |
| 1700 | (struct btrfs_extent_inline_ref *)(ei + 1); |
| 1701 | } else { |
| 1702 | WARN_ON(key->type != BTRFS_EXTENT_ITEM_KEY); |
| 1703 | info = (struct btrfs_tree_block_info *)(ei + 1); |
| 1704 | *out_eiref = |
| 1705 | (struct btrfs_extent_inline_ref *)(info + 1); |
| 1706 | } |
Jan Schmidt | a542ad1 | 2011-06-13 19:52:59 +0200 | [diff] [blame] | 1707 | } else { |
| 1708 | *out_eiref = (struct btrfs_extent_inline_ref *)(ei + 1); |
| 1709 | } |
| 1710 | *ptr = (unsigned long)*out_eiref; |
Liu Bo | cd857dd | 2014-06-08 19:04:13 +0800 | [diff] [blame] | 1711 | if ((unsigned long)(*ptr) >= (unsigned long)ei + item_size) |
Jan Schmidt | a542ad1 | 2011-06-13 19:52:59 +0200 | [diff] [blame] | 1712 | return -ENOENT; |
| 1713 | } |
| 1714 | |
| 1715 | end = (unsigned long)ei + item_size; |
Liu Bo | 6eda71d | 2014-06-09 10:54:07 +0800 | [diff] [blame] | 1716 | *out_eiref = (struct btrfs_extent_inline_ref *)(*ptr); |
Jan Schmidt | a542ad1 | 2011-06-13 19:52:59 +0200 | [diff] [blame] | 1717 | *out_type = btrfs_extent_inline_ref_type(eb, *out_eiref); |
| 1718 | |
| 1719 | *ptr += btrfs_extent_inline_ref_size(*out_type); |
| 1720 | WARN_ON(*ptr > end); |
| 1721 | if (*ptr == end) |
| 1722 | return 1; /* last */ |
| 1723 | |
| 1724 | return 0; |
| 1725 | } |
| 1726 | |
| 1727 | /* |
| 1728 | * reads the tree block backref for an extent. tree level and root are returned |
| 1729 | * through out_level and out_root. ptr must point to a 0 value for the first |
Jeff Mahoney | e0c476b | 2017-06-28 21:56:57 -0600 | [diff] [blame] | 1730 | * call and may be modified (see get_extent_inline_ref comment). |
Jan Schmidt | a542ad1 | 2011-06-13 19:52:59 +0200 | [diff] [blame] | 1731 | * returns 0 if data was provided, 1 if there was no more data to provide or |
| 1732 | * <0 on error. |
| 1733 | */ |
| 1734 | int tree_backref_for_extent(unsigned long *ptr, struct extent_buffer *eb, |
Liu Bo | 6eda71d | 2014-06-09 10:54:07 +0800 | [diff] [blame] | 1735 | struct btrfs_key *key, struct btrfs_extent_item *ei, |
| 1736 | u32 item_size, u64 *out_root, u8 *out_level) |
Jan Schmidt | a542ad1 | 2011-06-13 19:52:59 +0200 | [diff] [blame] | 1737 | { |
| 1738 | int ret; |
| 1739 | int type; |
Jan Schmidt | a542ad1 | 2011-06-13 19:52:59 +0200 | [diff] [blame] | 1740 | struct btrfs_extent_inline_ref *eiref; |
| 1741 | |
| 1742 | if (*ptr == (unsigned long)-1) |
| 1743 | return 1; |
| 1744 | |
| 1745 | while (1) { |
Jeff Mahoney | e0c476b | 2017-06-28 21:56:57 -0600 | [diff] [blame] | 1746 | ret = get_extent_inline_ref(ptr, eb, key, ei, item_size, |
Liu Bo | 6eda71d | 2014-06-09 10:54:07 +0800 | [diff] [blame] | 1747 | &eiref, &type); |
Jan Schmidt | a542ad1 | 2011-06-13 19:52:59 +0200 | [diff] [blame] | 1748 | if (ret < 0) |
| 1749 | return ret; |
| 1750 | |
| 1751 | if (type == BTRFS_TREE_BLOCK_REF_KEY || |
| 1752 | type == BTRFS_SHARED_BLOCK_REF_KEY) |
| 1753 | break; |
| 1754 | |
| 1755 | if (ret == 1) |
| 1756 | return 1; |
| 1757 | } |
| 1758 | |
| 1759 | /* we can treat both ref types equally here */ |
Jan Schmidt | a542ad1 | 2011-06-13 19:52:59 +0200 | [diff] [blame] | 1760 | *out_root = btrfs_extent_inline_ref_offset(eb, eiref); |
Filipe Manana | a1317f4 | 2014-12-15 16:04:42 +0000 | [diff] [blame] | 1761 | |
| 1762 | if (key->type == BTRFS_EXTENT_ITEM_KEY) { |
| 1763 | struct btrfs_tree_block_info *info; |
| 1764 | |
| 1765 | info = (struct btrfs_tree_block_info *)(ei + 1); |
| 1766 | *out_level = btrfs_tree_block_level(eb, info); |
| 1767 | } else { |
| 1768 | ASSERT(key->type == BTRFS_METADATA_ITEM_KEY); |
| 1769 | *out_level = (u8)key->offset; |
| 1770 | } |
Jan Schmidt | a542ad1 | 2011-06-13 19:52:59 +0200 | [diff] [blame] | 1771 | |
| 1772 | if (ret == 1) |
| 1773 | *ptr = (unsigned long)-1; |
| 1774 | |
| 1775 | return 0; |
| 1776 | } |
| 1777 | |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 1778 | static int iterate_leaf_refs(struct btrfs_fs_info *fs_info, |
| 1779 | struct extent_inode_elem *inode_list, |
| 1780 | u64 root, u64 extent_item_objectid, |
| 1781 | iterate_extent_inodes_t *iterate, void *ctx) |
Jan Schmidt | a542ad1 | 2011-06-13 19:52:59 +0200 | [diff] [blame] | 1782 | { |
Jan Schmidt | 976b190 | 2012-05-17 16:43:03 +0200 | [diff] [blame] | 1783 | struct extent_inode_elem *eie; |
Jan Schmidt | 4692cf5 | 2011-12-02 14:56:41 +0100 | [diff] [blame] | 1784 | int ret = 0; |
Jan Schmidt | a542ad1 | 2011-06-13 19:52:59 +0200 | [diff] [blame] | 1785 | |
Jan Schmidt | 976b190 | 2012-05-17 16:43:03 +0200 | [diff] [blame] | 1786 | for (eie = inode_list; eie; eie = eie->next) { |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 1787 | btrfs_debug(fs_info, |
| 1788 | "ref for %llu resolved, key (%llu EXTEND_DATA %llu), root %llu", |
| 1789 | extent_item_objectid, eie->inum, |
| 1790 | eie->offset, root); |
Jan Schmidt | 976b190 | 2012-05-17 16:43:03 +0200 | [diff] [blame] | 1791 | ret = iterate(eie->inum, eie->offset, root, ctx); |
Jan Schmidt | 4692cf5 | 2011-12-02 14:56:41 +0100 | [diff] [blame] | 1792 | if (ret) { |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 1793 | btrfs_debug(fs_info, |
| 1794 | "stopping iteration for %llu due to ret=%d", |
| 1795 | extent_item_objectid, ret); |
Jan Schmidt | 4692cf5 | 2011-12-02 14:56:41 +0100 | [diff] [blame] | 1796 | break; |
| 1797 | } |
Jan Schmidt | a542ad1 | 2011-06-13 19:52:59 +0200 | [diff] [blame] | 1798 | } |
| 1799 | |
Jan Schmidt | a542ad1 | 2011-06-13 19:52:59 +0200 | [diff] [blame] | 1800 | return ret; |
| 1801 | } |
| 1802 | |
| 1803 | /* |
| 1804 | * calls iterate() for every inode that references the extent identified by |
Jan Schmidt | 4692cf5 | 2011-12-02 14:56:41 +0100 | [diff] [blame] | 1805 | * the given parameters. |
Jan Schmidt | a542ad1 | 2011-06-13 19:52:59 +0200 | [diff] [blame] | 1806 | * when the iterator function returns a non-zero value, iteration stops. |
| 1807 | */ |
| 1808 | int iterate_extent_inodes(struct btrfs_fs_info *fs_info, |
Jan Schmidt | 4692cf5 | 2011-12-02 14:56:41 +0100 | [diff] [blame] | 1809 | u64 extent_item_objectid, u64 extent_item_pos, |
Jan Schmidt | 7a3ae2f | 2012-03-23 17:32:28 +0100 | [diff] [blame] | 1810 | int search_commit_root, |
Jan Schmidt | a542ad1 | 2011-06-13 19:52:59 +0200 | [diff] [blame] | 1811 | iterate_extent_inodes_t *iterate, void *ctx) |
| 1812 | { |
Jan Schmidt | a542ad1 | 2011-06-13 19:52:59 +0200 | [diff] [blame] | 1813 | int ret; |
Josef Bacik | da61d31 | 2013-06-12 16:20:08 -0400 | [diff] [blame] | 1814 | struct btrfs_trans_handle *trans = NULL; |
Jan Schmidt | 7a3ae2f | 2012-03-23 17:32:28 +0100 | [diff] [blame] | 1815 | struct ulist *refs = NULL; |
| 1816 | struct ulist *roots = NULL; |
Jan Schmidt | 4692cf5 | 2011-12-02 14:56:41 +0100 | [diff] [blame] | 1817 | struct ulist_node *ref_node = NULL; |
| 1818 | struct ulist_node *root_node = NULL; |
David Sterba | 3284da7 | 2015-02-25 15:47:32 +0100 | [diff] [blame] | 1819 | struct seq_list tree_mod_seq_elem = SEQ_LIST_INIT(tree_mod_seq_elem); |
Jan Schmidt | cd1b413 | 2012-05-22 14:56:50 +0200 | [diff] [blame] | 1820 | struct ulist_iterator ref_uiter; |
| 1821 | struct ulist_iterator root_uiter; |
Jan Schmidt | a542ad1 | 2011-06-13 19:52:59 +0200 | [diff] [blame] | 1822 | |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 1823 | btrfs_debug(fs_info, "resolving all inodes for extent %llu", |
Jan Schmidt | 4692cf5 | 2011-12-02 14:56:41 +0100 | [diff] [blame] | 1824 | extent_item_objectid); |
| 1825 | |
Josef Bacik | da61d31 | 2013-06-12 16:20:08 -0400 | [diff] [blame] | 1826 | if (!search_commit_root) { |
Jan Schmidt | 7a3ae2f | 2012-03-23 17:32:28 +0100 | [diff] [blame] | 1827 | trans = btrfs_join_transaction(fs_info->extent_root); |
| 1828 | if (IS_ERR(trans)) |
| 1829 | return PTR_ERR(trans); |
Jan Schmidt | 8445f61 | 2012-05-16 18:36:03 +0200 | [diff] [blame] | 1830 | btrfs_get_tree_mod_seq(fs_info, &tree_mod_seq_elem); |
Josef Bacik | 9e351cc | 2014-03-13 15:42:13 -0400 | [diff] [blame] | 1831 | } else { |
| 1832 | down_read(&fs_info->commit_root_sem); |
Jan Schmidt | 7a3ae2f | 2012-03-23 17:32:28 +0100 | [diff] [blame] | 1833 | } |
Jan Schmidt | 4692cf5 | 2011-12-02 14:56:41 +0100 | [diff] [blame] | 1834 | |
| 1835 | ret = btrfs_find_all_leafs(trans, fs_info, extent_item_objectid, |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 1836 | tree_mod_seq_elem.seq, &refs, |
Jan Schmidt | 8445f61 | 2012-05-16 18:36:03 +0200 | [diff] [blame] | 1837 | &extent_item_pos); |
Jan Schmidt | 4692cf5 | 2011-12-02 14:56:41 +0100 | [diff] [blame] | 1838 | if (ret) |
| 1839 | goto out; |
| 1840 | |
Jan Schmidt | cd1b413 | 2012-05-22 14:56:50 +0200 | [diff] [blame] | 1841 | ULIST_ITER_INIT(&ref_uiter); |
| 1842 | while (!ret && (ref_node = ulist_next(refs, &ref_uiter))) { |
Jeff Mahoney | e0c476b | 2017-06-28 21:56:57 -0600 | [diff] [blame] | 1843 | ret = btrfs_find_all_roots_safe(trans, fs_info, ref_node->val, |
| 1844 | tree_mod_seq_elem.seq, &roots); |
Jan Schmidt | 4692cf5 | 2011-12-02 14:56:41 +0100 | [diff] [blame] | 1845 | if (ret) |
Jan Schmidt | a542ad1 | 2011-06-13 19:52:59 +0200 | [diff] [blame] | 1846 | break; |
Jan Schmidt | cd1b413 | 2012-05-22 14:56:50 +0200 | [diff] [blame] | 1847 | ULIST_ITER_INIT(&root_uiter); |
| 1848 | while (!ret && (root_node = ulist_next(roots, &root_uiter))) { |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 1849 | btrfs_debug(fs_info, |
| 1850 | "root %llu references leaf %llu, data list %#llx", |
| 1851 | root_node->val, ref_node->val, |
| 1852 | ref_node->aux); |
| 1853 | ret = iterate_leaf_refs(fs_info, |
| 1854 | (struct extent_inode_elem *) |
Jan Schmidt | 995e01b | 2012-08-13 02:52:38 -0600 | [diff] [blame] | 1855 | (uintptr_t)ref_node->aux, |
| 1856 | root_node->val, |
| 1857 | extent_item_objectid, |
| 1858 | iterate, ctx); |
Jan Schmidt | a542ad1 | 2011-06-13 19:52:59 +0200 | [diff] [blame] | 1859 | } |
Jan Schmidt | 976b190 | 2012-05-17 16:43:03 +0200 | [diff] [blame] | 1860 | ulist_free(roots); |
Jan Schmidt | a542ad1 | 2011-06-13 19:52:59 +0200 | [diff] [blame] | 1861 | } |
| 1862 | |
Jan Schmidt | 976b190 | 2012-05-17 16:43:03 +0200 | [diff] [blame] | 1863 | free_leaf_list(refs); |
Jan Schmidt | 4692cf5 | 2011-12-02 14:56:41 +0100 | [diff] [blame] | 1864 | out: |
Jan Schmidt | 7a3ae2f | 2012-03-23 17:32:28 +0100 | [diff] [blame] | 1865 | if (!search_commit_root) { |
Jan Schmidt | 8445f61 | 2012-05-16 18:36:03 +0200 | [diff] [blame] | 1866 | btrfs_put_tree_mod_seq(fs_info, &tree_mod_seq_elem); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 1867 | btrfs_end_transaction(trans); |
Josef Bacik | 9e351cc | 2014-03-13 15:42:13 -0400 | [diff] [blame] | 1868 | } else { |
| 1869 | up_read(&fs_info->commit_root_sem); |
Jan Schmidt | 7a3ae2f | 2012-03-23 17:32:28 +0100 | [diff] [blame] | 1870 | } |
| 1871 | |
Jan Schmidt | a542ad1 | 2011-06-13 19:52:59 +0200 | [diff] [blame] | 1872 | return ret; |
| 1873 | } |
| 1874 | |
| 1875 | int iterate_inodes_from_logical(u64 logical, struct btrfs_fs_info *fs_info, |
| 1876 | struct btrfs_path *path, |
| 1877 | iterate_extent_inodes_t *iterate, void *ctx) |
| 1878 | { |
| 1879 | int ret; |
Jan Schmidt | 4692cf5 | 2011-12-02 14:56:41 +0100 | [diff] [blame] | 1880 | u64 extent_item_pos; |
Liu Bo | 69917e4 | 2012-09-07 20:01:28 -0600 | [diff] [blame] | 1881 | u64 flags = 0; |
Jan Schmidt | a542ad1 | 2011-06-13 19:52:59 +0200 | [diff] [blame] | 1882 | struct btrfs_key found_key; |
Jan Schmidt | 7a3ae2f | 2012-03-23 17:32:28 +0100 | [diff] [blame] | 1883 | int search_commit_root = path->search_commit_root; |
Jan Schmidt | a542ad1 | 2011-06-13 19:52:59 +0200 | [diff] [blame] | 1884 | |
Liu Bo | 69917e4 | 2012-09-07 20:01:28 -0600 | [diff] [blame] | 1885 | ret = extent_from_logical(fs_info, logical, path, &found_key, &flags); |
Jan Schmidt | 4692cf5 | 2011-12-02 14:56:41 +0100 | [diff] [blame] | 1886 | btrfs_release_path(path); |
Jan Schmidt | a542ad1 | 2011-06-13 19:52:59 +0200 | [diff] [blame] | 1887 | if (ret < 0) |
| 1888 | return ret; |
Liu Bo | 69917e4 | 2012-09-07 20:01:28 -0600 | [diff] [blame] | 1889 | if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) |
Stefan Behrens | 3627bf4 | 2012-08-01 04:28:01 -0600 | [diff] [blame] | 1890 | return -EINVAL; |
Jan Schmidt | a542ad1 | 2011-06-13 19:52:59 +0200 | [diff] [blame] | 1891 | |
Jan Schmidt | 4692cf5 | 2011-12-02 14:56:41 +0100 | [diff] [blame] | 1892 | extent_item_pos = logical - found_key.objectid; |
Jan Schmidt | 7a3ae2f | 2012-03-23 17:32:28 +0100 | [diff] [blame] | 1893 | ret = iterate_extent_inodes(fs_info, found_key.objectid, |
| 1894 | extent_item_pos, search_commit_root, |
| 1895 | iterate, ctx); |
Jan Schmidt | a542ad1 | 2011-06-13 19:52:59 +0200 | [diff] [blame] | 1896 | |
| 1897 | return ret; |
| 1898 | } |
| 1899 | |
Mark Fasheh | d24bec3 | 2012-08-08 11:33:54 -0700 | [diff] [blame] | 1900 | typedef int (iterate_irefs_t)(u64 parent, u32 name_len, unsigned long name_off, |
| 1901 | struct extent_buffer *eb, void *ctx); |
| 1902 | |
| 1903 | static int iterate_inode_refs(u64 inum, struct btrfs_root *fs_root, |
| 1904 | struct btrfs_path *path, |
| 1905 | iterate_irefs_t *iterate, void *ctx) |
Jan Schmidt | a542ad1 | 2011-06-13 19:52:59 +0200 | [diff] [blame] | 1906 | { |
Jan Schmidt | aefc1eb | 2012-04-13 12:28:00 +0200 | [diff] [blame] | 1907 | int ret = 0; |
Jan Schmidt | a542ad1 | 2011-06-13 19:52:59 +0200 | [diff] [blame] | 1908 | int slot; |
| 1909 | u32 cur; |
| 1910 | u32 len; |
| 1911 | u32 name_len; |
| 1912 | u64 parent = 0; |
| 1913 | int found = 0; |
| 1914 | struct extent_buffer *eb; |
| 1915 | struct btrfs_item *item; |
| 1916 | struct btrfs_inode_ref *iref; |
| 1917 | struct btrfs_key found_key; |
| 1918 | |
Jan Schmidt | aefc1eb | 2012-04-13 12:28:00 +0200 | [diff] [blame] | 1919 | while (!ret) { |
David Sterba | c234a24 | 2015-01-02 19:03:17 +0100 | [diff] [blame] | 1920 | ret = btrfs_find_item(fs_root, path, inum, |
| 1921 | parent ? parent + 1 : 0, BTRFS_INODE_REF_KEY, |
| 1922 | &found_key); |
| 1923 | |
Jan Schmidt | a542ad1 | 2011-06-13 19:52:59 +0200 | [diff] [blame] | 1924 | if (ret < 0) |
| 1925 | break; |
| 1926 | if (ret) { |
| 1927 | ret = found ? 0 : -ENOENT; |
| 1928 | break; |
| 1929 | } |
| 1930 | ++found; |
| 1931 | |
| 1932 | parent = found_key.offset; |
| 1933 | slot = path->slots[0]; |
Filipe David Borba Manana | 3fe81ce | 2013-12-15 12:43:58 +0000 | [diff] [blame] | 1934 | eb = btrfs_clone_extent_buffer(path->nodes[0]); |
| 1935 | if (!eb) { |
| 1936 | ret = -ENOMEM; |
| 1937 | break; |
| 1938 | } |
| 1939 | extent_buffer_get(eb); |
Jan Schmidt | b916a59 | 2012-04-13 12:28:08 +0200 | [diff] [blame] | 1940 | btrfs_tree_read_lock(eb); |
| 1941 | btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK); |
Jan Schmidt | a542ad1 | 2011-06-13 19:52:59 +0200 | [diff] [blame] | 1942 | btrfs_release_path(path); |
| 1943 | |
Ross Kirk | dd3cc16 | 2013-09-16 15:58:09 +0100 | [diff] [blame] | 1944 | item = btrfs_item_nr(slot); |
Jan Schmidt | a542ad1 | 2011-06-13 19:52:59 +0200 | [diff] [blame] | 1945 | iref = btrfs_item_ptr(eb, slot, struct btrfs_inode_ref); |
| 1946 | |
| 1947 | for (cur = 0; cur < btrfs_item_size(eb, item); cur += len) { |
| 1948 | name_len = btrfs_inode_ref_name_len(eb, iref); |
| 1949 | /* path must be released before calling iterate()! */ |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 1950 | btrfs_debug(fs_root->fs_info, |
| 1951 | "following ref at offset %u for inode %llu in tree %llu", |
| 1952 | cur, found_key.objectid, fs_root->objectid); |
Mark Fasheh | d24bec3 | 2012-08-08 11:33:54 -0700 | [diff] [blame] | 1953 | ret = iterate(parent, name_len, |
| 1954 | (unsigned long)(iref + 1), eb, ctx); |
Jan Schmidt | aefc1eb | 2012-04-13 12:28:00 +0200 | [diff] [blame] | 1955 | if (ret) |
Jan Schmidt | a542ad1 | 2011-06-13 19:52:59 +0200 | [diff] [blame] | 1956 | break; |
Jan Schmidt | a542ad1 | 2011-06-13 19:52:59 +0200 | [diff] [blame] | 1957 | len = sizeof(*iref) + name_len; |
| 1958 | iref = (struct btrfs_inode_ref *)((char *)iref + len); |
| 1959 | } |
Jan Schmidt | b916a59 | 2012-04-13 12:28:08 +0200 | [diff] [blame] | 1960 | btrfs_tree_read_unlock_blocking(eb); |
Jan Schmidt | a542ad1 | 2011-06-13 19:52:59 +0200 | [diff] [blame] | 1961 | free_extent_buffer(eb); |
| 1962 | } |
| 1963 | |
| 1964 | btrfs_release_path(path); |
| 1965 | |
| 1966 | return ret; |
| 1967 | } |
| 1968 | |
Mark Fasheh | d24bec3 | 2012-08-08 11:33:54 -0700 | [diff] [blame] | 1969 | static int iterate_inode_extrefs(u64 inum, struct btrfs_root *fs_root, |
| 1970 | struct btrfs_path *path, |
| 1971 | iterate_irefs_t *iterate, void *ctx) |
| 1972 | { |
| 1973 | int ret; |
| 1974 | int slot; |
| 1975 | u64 offset = 0; |
| 1976 | u64 parent; |
| 1977 | int found = 0; |
| 1978 | struct extent_buffer *eb; |
| 1979 | struct btrfs_inode_extref *extref; |
Mark Fasheh | d24bec3 | 2012-08-08 11:33:54 -0700 | [diff] [blame] | 1980 | u32 item_size; |
| 1981 | u32 cur_offset; |
| 1982 | unsigned long ptr; |
| 1983 | |
| 1984 | while (1) { |
| 1985 | ret = btrfs_find_one_extref(fs_root, inum, offset, path, &extref, |
| 1986 | &offset); |
| 1987 | if (ret < 0) |
| 1988 | break; |
| 1989 | if (ret) { |
| 1990 | ret = found ? 0 : -ENOENT; |
| 1991 | break; |
| 1992 | } |
| 1993 | ++found; |
| 1994 | |
| 1995 | slot = path->slots[0]; |
Filipe David Borba Manana | 3fe81ce | 2013-12-15 12:43:58 +0000 | [diff] [blame] | 1996 | eb = btrfs_clone_extent_buffer(path->nodes[0]); |
| 1997 | if (!eb) { |
| 1998 | ret = -ENOMEM; |
| 1999 | break; |
| 2000 | } |
| 2001 | extent_buffer_get(eb); |
Mark Fasheh | d24bec3 | 2012-08-08 11:33:54 -0700 | [diff] [blame] | 2002 | |
| 2003 | btrfs_tree_read_lock(eb); |
| 2004 | btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK); |
| 2005 | btrfs_release_path(path); |
| 2006 | |
Chris Mason | 2849a85 | 2015-10-13 14:06:48 -0400 | [diff] [blame] | 2007 | item_size = btrfs_item_size_nr(eb, slot); |
| 2008 | ptr = btrfs_item_ptr_offset(eb, slot); |
Mark Fasheh | d24bec3 | 2012-08-08 11:33:54 -0700 | [diff] [blame] | 2009 | cur_offset = 0; |
| 2010 | |
| 2011 | while (cur_offset < item_size) { |
| 2012 | u32 name_len; |
| 2013 | |
| 2014 | extref = (struct btrfs_inode_extref *)(ptr + cur_offset); |
| 2015 | parent = btrfs_inode_extref_parent(eb, extref); |
| 2016 | name_len = btrfs_inode_extref_name_len(eb, extref); |
| 2017 | ret = iterate(parent, name_len, |
| 2018 | (unsigned long)&extref->name, eb, ctx); |
| 2019 | if (ret) |
| 2020 | break; |
| 2021 | |
Chris Mason | 2849a85 | 2015-10-13 14:06:48 -0400 | [diff] [blame] | 2022 | cur_offset += btrfs_inode_extref_name_len(eb, extref); |
Mark Fasheh | d24bec3 | 2012-08-08 11:33:54 -0700 | [diff] [blame] | 2023 | cur_offset += sizeof(*extref); |
| 2024 | } |
| 2025 | btrfs_tree_read_unlock_blocking(eb); |
| 2026 | free_extent_buffer(eb); |
| 2027 | |
| 2028 | offset++; |
| 2029 | } |
| 2030 | |
| 2031 | btrfs_release_path(path); |
| 2032 | |
| 2033 | return ret; |
| 2034 | } |
| 2035 | |
| 2036 | static int iterate_irefs(u64 inum, struct btrfs_root *fs_root, |
| 2037 | struct btrfs_path *path, iterate_irefs_t *iterate, |
| 2038 | void *ctx) |
| 2039 | { |
| 2040 | int ret; |
| 2041 | int found_refs = 0; |
| 2042 | |
| 2043 | ret = iterate_inode_refs(inum, fs_root, path, iterate, ctx); |
| 2044 | if (!ret) |
| 2045 | ++found_refs; |
| 2046 | else if (ret != -ENOENT) |
| 2047 | return ret; |
| 2048 | |
| 2049 | ret = iterate_inode_extrefs(inum, fs_root, path, iterate, ctx); |
| 2050 | if (ret == -ENOENT && found_refs) |
| 2051 | return 0; |
| 2052 | |
| 2053 | return ret; |
| 2054 | } |
| 2055 | |
Jan Schmidt | a542ad1 | 2011-06-13 19:52:59 +0200 | [diff] [blame] | 2056 | /* |
| 2057 | * returns 0 if the path could be dumped (probably truncated) |
| 2058 | * returns <0 in case of an error |
| 2059 | */ |
Mark Fasheh | d24bec3 | 2012-08-08 11:33:54 -0700 | [diff] [blame] | 2060 | static int inode_to_path(u64 inum, u32 name_len, unsigned long name_off, |
| 2061 | struct extent_buffer *eb, void *ctx) |
Jan Schmidt | a542ad1 | 2011-06-13 19:52:59 +0200 | [diff] [blame] | 2062 | { |
| 2063 | struct inode_fs_paths *ipath = ctx; |
| 2064 | char *fspath; |
| 2065 | char *fspath_min; |
| 2066 | int i = ipath->fspath->elem_cnt; |
| 2067 | const int s_ptr = sizeof(char *); |
| 2068 | u32 bytes_left; |
| 2069 | |
| 2070 | bytes_left = ipath->fspath->bytes_left > s_ptr ? |
| 2071 | ipath->fspath->bytes_left - s_ptr : 0; |
| 2072 | |
Chris Mason | 740c3d2 | 2011-11-02 15:48:34 -0400 | [diff] [blame] | 2073 | fspath_min = (char *)ipath->fspath->val + (i + 1) * s_ptr; |
Jan Schmidt | 96b5bd7 | 2012-10-15 08:30:45 +0000 | [diff] [blame] | 2074 | fspath = btrfs_ref_to_path(ipath->fs_root, ipath->btrfs_path, name_len, |
| 2075 | name_off, eb, inum, fspath_min, bytes_left); |
Jan Schmidt | a542ad1 | 2011-06-13 19:52:59 +0200 | [diff] [blame] | 2076 | if (IS_ERR(fspath)) |
| 2077 | return PTR_ERR(fspath); |
| 2078 | |
| 2079 | if (fspath > fspath_min) { |
Jeff Mahoney | 745c4d8 | 2011-11-20 07:31:57 -0500 | [diff] [blame] | 2080 | ipath->fspath->val[i] = (u64)(unsigned long)fspath; |
Jan Schmidt | a542ad1 | 2011-06-13 19:52:59 +0200 | [diff] [blame] | 2081 | ++ipath->fspath->elem_cnt; |
| 2082 | ipath->fspath->bytes_left = fspath - fspath_min; |
| 2083 | } else { |
| 2084 | ++ipath->fspath->elem_missed; |
| 2085 | ipath->fspath->bytes_missing += fspath_min - fspath; |
| 2086 | ipath->fspath->bytes_left = 0; |
| 2087 | } |
| 2088 | |
| 2089 | return 0; |
| 2090 | } |
| 2091 | |
| 2092 | /* |
| 2093 | * this dumps all file system paths to the inode into the ipath struct, provided |
| 2094 | * is has been created large enough. each path is zero-terminated and accessed |
Chris Mason | 740c3d2 | 2011-11-02 15:48:34 -0400 | [diff] [blame] | 2095 | * from ipath->fspath->val[i]. |
Jan Schmidt | a542ad1 | 2011-06-13 19:52:59 +0200 | [diff] [blame] | 2096 | * when it returns, there are ipath->fspath->elem_cnt number of paths available |
Chris Mason | 740c3d2 | 2011-11-02 15:48:34 -0400 | [diff] [blame] | 2097 | * in ipath->fspath->val[]. when the allocated space wasn't sufficient, the |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 2098 | * number of missed paths is recorded in ipath->fspath->elem_missed, otherwise, |
Jan Schmidt | a542ad1 | 2011-06-13 19:52:59 +0200 | [diff] [blame] | 2099 | * it's zero. ipath->fspath->bytes_missing holds the number of bytes that would |
| 2100 | * have been needed to return all paths. |
| 2101 | */ |
| 2102 | int paths_from_inode(u64 inum, struct inode_fs_paths *ipath) |
| 2103 | { |
| 2104 | return iterate_irefs(inum, ipath->fs_root, ipath->btrfs_path, |
Mark Fasheh | d24bec3 | 2012-08-08 11:33:54 -0700 | [diff] [blame] | 2105 | inode_to_path, ipath); |
Jan Schmidt | a542ad1 | 2011-06-13 19:52:59 +0200 | [diff] [blame] | 2106 | } |
| 2107 | |
Jan Schmidt | a542ad1 | 2011-06-13 19:52:59 +0200 | [diff] [blame] | 2108 | struct btrfs_data_container *init_data_container(u32 total_bytes) |
| 2109 | { |
| 2110 | struct btrfs_data_container *data; |
| 2111 | size_t alloc_bytes; |
| 2112 | |
| 2113 | alloc_bytes = max_t(size_t, total_bytes, sizeof(*data)); |
David Sterba | f54de06 | 2017-05-31 19:32:09 +0200 | [diff] [blame] | 2114 | data = kvmalloc(alloc_bytes, GFP_KERNEL); |
Jan Schmidt | a542ad1 | 2011-06-13 19:52:59 +0200 | [diff] [blame] | 2115 | if (!data) |
| 2116 | return ERR_PTR(-ENOMEM); |
| 2117 | |
| 2118 | if (total_bytes >= sizeof(*data)) { |
| 2119 | data->bytes_left = total_bytes - sizeof(*data); |
| 2120 | data->bytes_missing = 0; |
| 2121 | } else { |
| 2122 | data->bytes_missing = sizeof(*data) - total_bytes; |
| 2123 | data->bytes_left = 0; |
| 2124 | } |
| 2125 | |
| 2126 | data->elem_cnt = 0; |
| 2127 | data->elem_missed = 0; |
| 2128 | |
| 2129 | return data; |
| 2130 | } |
| 2131 | |
| 2132 | /* |
| 2133 | * allocates space to return multiple file system paths for an inode. |
| 2134 | * total_bytes to allocate are passed, note that space usable for actual path |
| 2135 | * information will be total_bytes - sizeof(struct inode_fs_paths). |
| 2136 | * the returned pointer must be freed with free_ipath() in the end. |
| 2137 | */ |
| 2138 | struct inode_fs_paths *init_ipath(s32 total_bytes, struct btrfs_root *fs_root, |
| 2139 | struct btrfs_path *path) |
| 2140 | { |
| 2141 | struct inode_fs_paths *ifp; |
| 2142 | struct btrfs_data_container *fspath; |
| 2143 | |
| 2144 | fspath = init_data_container(total_bytes); |
| 2145 | if (IS_ERR(fspath)) |
| 2146 | return (void *)fspath; |
| 2147 | |
David Sterba | f54de06 | 2017-05-31 19:32:09 +0200 | [diff] [blame] | 2148 | ifp = kmalloc(sizeof(*ifp), GFP_KERNEL); |
Jan Schmidt | a542ad1 | 2011-06-13 19:52:59 +0200 | [diff] [blame] | 2149 | if (!ifp) { |
David Sterba | f54de06 | 2017-05-31 19:32:09 +0200 | [diff] [blame] | 2150 | kvfree(fspath); |
Jan Schmidt | a542ad1 | 2011-06-13 19:52:59 +0200 | [diff] [blame] | 2151 | return ERR_PTR(-ENOMEM); |
| 2152 | } |
| 2153 | |
| 2154 | ifp->btrfs_path = path; |
| 2155 | ifp->fspath = fspath; |
| 2156 | ifp->fs_root = fs_root; |
| 2157 | |
| 2158 | return ifp; |
| 2159 | } |
| 2160 | |
| 2161 | void free_ipath(struct inode_fs_paths *ipath) |
| 2162 | { |
Jesper Juhl | 4735fb2 | 2012-04-12 22:47:52 +0200 | [diff] [blame] | 2163 | if (!ipath) |
| 2164 | return; |
David Sterba | f54de06 | 2017-05-31 19:32:09 +0200 | [diff] [blame] | 2165 | kvfree(ipath->fspath); |
Jan Schmidt | a542ad1 | 2011-06-13 19:52:59 +0200 | [diff] [blame] | 2166 | kfree(ipath); |
| 2167 | } |