blob: 9593102bdc2cf344e606332ed18486c3187a4ba1 [file] [log] [blame]
Jan Schmidta542ad12011-06-13 19:52:59 +02001/*
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 Sterbaf54de062017-05-31 19:32:09 +020019#include <linux/mm.h>
Lu Fengqiafce7722016-06-13 09:36:46 +080020#include <linux/rbtree.h>
Jeff Mahoney00142752017-07-12 16:20:08 -060021#include <trace/events/btrfs.h>
Jan Schmidta542ad12011-06-13 19:52:59 +020022#include "ctree.h"
23#include "disk-io.h"
24#include "backref.h"
Jan Schmidt8da6d582011-11-23 18:55:04 +010025#include "ulist.h"
26#include "transaction.h"
27#include "delayed-ref.h"
Jan Schmidtb916a592012-04-13 12:28:08 +020028#include "locking.h"
Jan Schmidta542ad12011-06-13 19:52:59 +020029
Josef Bacikdc046b12014-09-10 16:20:45 -040030/* Just an arbitrary number so we can be sure this happened */
31#define BACKREF_FOUND_SHARED 6
32
Jan Schmidt976b1902012-05-17 16:43:03 +020033struct extent_inode_elem {
34 u64 inum;
35 u64 offset;
36 struct extent_inode_elem *next;
37};
38
Jeff Mahoney73980be2017-06-28 21:56:55 -060039static 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 Schmidt976b1902012-05-17 16:43:03 +020044{
Josef Bacik8ca15e02013-07-05 13:58:19 -040045 u64 offset = 0;
Jan Schmidt976b1902012-05-17 16:43:03 +020046 struct extent_inode_elem *e;
47
Josef Bacik8ca15e02013-07-05 13:58:19 -040048 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 Schmidt976b1902012-05-17 16:43:03 +020053
Josef Bacik8ca15e02013-07-05 13:58:19 -040054 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 Schmidt976b1902012-05-17 16:43:03 +020062
63 e = kmalloc(sizeof(*e), GFP_NOFS);
64 if (!e)
65 return -ENOMEM;
66
67 e->next = *eie;
68 e->inum = key->objectid;
Josef Bacik8ca15e02013-07-05 13:58:19 -040069 e->offset = key->offset + offset;
Jan Schmidt976b1902012-05-17 16:43:03 +020070 *eie = e;
71
72 return 0;
73}
74
Wang Shilongf05c4742014-01-28 19:13:38 +080075static 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 Mahoney73980be2017-06-28 21:56:55 -060085static 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 Schmidt976b1902012-05-17 16:43:03 +020088{
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 Nadolski86d5f992017-07-12 16:20:06 -0600124struct preftree {
125 struct rb_root root;
Jeff Mahoney6c336b22017-07-12 16:20:07 -0600126 unsigned int count;
Edmund Nadolski86d5f992017-07-12 16:20:06 -0600127};
128
Jeff Mahoney6c336b22017-07-12 16:20:07 -0600129#define PREFTREE_INIT { .root = RB_ROOT, .count = 0 }
Edmund Nadolski86d5f992017-07-12 16:20:06 -0600130
131struct 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 Shilongb9e9a6c2013-08-09 13:25:36 +0800137static struct kmem_cache *btrfs_prelim_ref_cache;
138
139int __init btrfs_prelim_ref_init(void)
140{
141 btrfs_prelim_ref_cache = kmem_cache_create("btrfs_prelim_ref",
Jeff Mahoneye0c476b2017-06-28 21:56:57 -0600142 sizeof(struct prelim_ref),
Wang Shilongb9e9a6c2013-08-09 13:25:36 +0800143 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +0300144 SLAB_MEM_SPREAD,
Wang Shilongb9e9a6c2013-08-09 13:25:36 +0800145 NULL);
146 if (!btrfs_prelim_ref_cache)
147 return -ENOMEM;
148 return 0;
149}
150
151void btrfs_prelim_ref_exit(void)
152{
Kinglong Mee5598e902016-01-29 21:36:35 +0800153 kmem_cache_destroy(btrfs_prelim_ref_cache);
Wang Shilongb9e9a6c2013-08-09 13:25:36 +0800154}
155
Edmund Nadolski86d5f992017-07-12 16:20:06 -0600156static 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 */
166static 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 Mahoney00142752017-07-12 16:20:08 -0600202static void prelim_ref_insert(const struct btrfs_fs_info *fs_info,
203 struct preftree *preftree,
Edmund Nadolski86d5f992017-07-12 16:20:06 -0600204 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 Mahoney00142752017-07-12 16:20:08 -0600234 trace_btrfs_prelim_ref_merge(fs_info, ref, newref,
235 preftree->count);
Edmund Nadolski86d5f992017-07-12 16:20:06 -0600236 ref->count += newref->count;
237 free_pref(newref);
238 return;
239 }
240 }
241
Jeff Mahoney6c336b22017-07-12 16:20:07 -0600242 preftree->count++;
Jeff Mahoney00142752017-07-12 16:20:08 -0600243 trace_btrfs_prelim_ref_insert(fs_info, newref, NULL, preftree->count);
Edmund Nadolski86d5f992017-07-12 16:20:06 -0600244 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 */
252static 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 Mahoney6c336b22017-07-12 16:20:07 -0600261 preftree->count = 0;
Edmund Nadolski86d5f992017-07-12 16:20:06 -0600262}
263
Jan Schmidtd5c88b72012-05-15 17:55:51 +0200264/*
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 Mahoneye0c476b2017-06-28 21:56:57 -0600296 * (see add_missing_keys)
Jan Schmidtd5c88b72012-05-15 17:55:51 +0200297 * - 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 Mahoney00142752017-07-12 16:20:08 -0600302static int add_prelim_ref(const struct btrfs_fs_info *fs_info,
303 struct preftree *preftree, u64 root_id,
Jeff Mahoneye0c476b2017-06-28 21:56:57 -0600304 const struct btrfs_key *key, int level, u64 parent,
305 u64 wanted_disk_byte, int count, gfp_t gfp_mask)
Jan Schmidt8da6d582011-11-23 18:55:04 +0100306{
Jeff Mahoneye0c476b2017-06-28 21:56:57 -0600307 struct prelim_ref *ref;
Jan Schmidt8da6d582011-11-23 18:55:04 +0100308
Liu Bo48ec4732013-10-30 13:25:24 +0800309 if (root_id == BTRFS_DATA_RELOC_TREE_OBJECTID)
310 return 0;
311
Wang Shilongb9e9a6c2013-08-09 13:25:36 +0800312 ref = kmem_cache_alloc(btrfs_prelim_ref_cache, gfp_mask);
Jan Schmidt8da6d582011-11-23 18:55:04 +0100313 if (!ref)
314 return -ENOMEM;
315
316 ref->root_id = root_id;
Filipe Mananad6589102015-07-29 17:21:17 +0100317 if (key) {
Jan Schmidtd5c88b72012-05-15 17:55:51 +0200318 ref->key_for_search = *key;
Filipe Mananad6589102015-07-29 17:21:17 +0100319 /*
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 Schmidtd5c88b72012-05-15 17:55:51 +0200342 memset(&ref->key_for_search, 0, sizeof(ref->key_for_search));
Filipe Mananad6589102015-07-29 17:21:17 +0100343 }
Jan Schmidt8da6d582011-11-23 18:55:04 +0100344
Jan Schmidt33019582012-05-30 18:05:21 +0200345 ref->inode_list = NULL;
Jan Schmidt8da6d582011-11-23 18:55:04 +0100346 ref->level = level;
347 ref->count = count;
348 ref->parent = parent;
349 ref->wanted_disk_byte = wanted_disk_byte;
Jeff Mahoney00142752017-07-12 16:20:08 -0600350 prelim_ref_insert(fs_info, preftree, ref);
Jan Schmidt8da6d582011-11-23 18:55:04 +0100351
352 return 0;
353}
354
Edmund Nadolski86d5f992017-07-12 16:20:06 -0600355/* direct refs use root == 0, key == NULL */
Jeff Mahoney00142752017-07-12 16:20:08 -0600356static int add_direct_ref(const struct btrfs_fs_info *fs_info,
357 struct preftrees *preftrees, int level, u64 parent,
Edmund Nadolski86d5f992017-07-12 16:20:06 -0600358 u64 wanted_disk_byte, int count, gfp_t gfp_mask)
359{
Jeff Mahoney00142752017-07-12 16:20:08 -0600360 return add_prelim_ref(fs_info, &preftrees->direct, 0, NULL, level,
361 parent, wanted_disk_byte, count, gfp_mask);
Edmund Nadolski86d5f992017-07-12 16:20:06 -0600362}
363
364/* indirect refs use parent == 0 */
Jeff Mahoney00142752017-07-12 16:20:08 -0600365static int add_indirect_ref(const struct btrfs_fs_info *fs_info,
366 struct preftrees *preftrees, u64 root_id,
Edmund Nadolski86d5f992017-07-12 16:20:06 -0600367 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 Mahoney00142752017-07-12 16:20:08 -0600374 return add_prelim_ref(fs_info, tree, root_id, key, level, 0,
Edmund Nadolski86d5f992017-07-12 16:20:06 -0600375 wanted_disk_byte, count, gfp_mask);
376}
377
Jan Schmidt8da6d582011-11-23 18:55:04 +0100378static int add_all_parents(struct btrfs_root *root, struct btrfs_path *path,
Jeff Mahoneye0c476b2017-06-28 21:56:57 -0600379 struct ulist *parents, struct prelim_ref *ref,
Josef Bacik44853862014-03-19 13:35:14 -0400380 int level, u64 time_seq, const u64 *extent_item_pos,
381 u64 total_refs)
Jan Schmidt8da6d582011-11-23 18:55:04 +0100382{
Alexander Block69bca402012-06-19 07:42:26 -0600383 int ret = 0;
384 int slot;
385 struct extent_buffer *eb;
386 struct btrfs_key key;
Josef Bacik7ef81ac2014-01-24 14:05:42 -0500387 struct btrfs_key *key_for_search = &ref->key_for_search;
Jan Schmidt8da6d582011-11-23 18:55:04 +0100388 struct btrfs_file_extent_item *fi;
Josef Baciked8c4912013-07-05 14:03:47 -0400389 struct extent_inode_elem *eie = NULL, *old = NULL;
Jan Schmidt8da6d582011-11-23 18:55:04 +0100390 u64 disk_byte;
Josef Bacik7ef81ac2014-01-24 14:05:42 -0500391 u64 wanted_disk_byte = ref->wanted_disk_byte;
392 u64 count = 0;
Jan Schmidt8da6d582011-11-23 18:55:04 +0100393
Alexander Block69bca402012-06-19 07:42:26 -0600394 if (level != 0) {
395 eb = path->nodes[level];
396 ret = ulist_add(parents, eb->start, 0, GFP_NOFS);
Jan Schmidt33019582012-05-30 18:05:21 +0200397 if (ret < 0)
398 return ret;
Jan Schmidt8da6d582011-11-23 18:55:04 +0100399 return 0;
Alexander Block69bca402012-06-19 07:42:26 -0600400 }
Jan Schmidt8da6d582011-11-23 18:55:04 +0100401
402 /*
Alexander Block69bca402012-06-19 07:42:26 -0600403 * 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 Schmidt8da6d582011-11-23 18:55:04 +0100406 */
Qu Wenruo21633fc2015-04-16 14:54:50 +0800407 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
Edmund Nadolskide47c9d2017-03-16 10:04:34 -0600408 if (time_seq == SEQ_LAST)
Qu Wenruo21633fc2015-04-16 14:54:50 +0800409 ret = btrfs_next_leaf(root, path);
410 else
411 ret = btrfs_next_old_leaf(root, path, time_seq);
412 }
Jan Schmidt8da6d582011-11-23 18:55:04 +0100413
Josef Bacik44853862014-03-19 13:35:14 -0400414 while (!ret && count < total_refs) {
Jan Schmidt8da6d582011-11-23 18:55:04 +0100415 eb = path->nodes[0];
Alexander Block69bca402012-06-19 07:42:26 -0600416 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 Baciked8c4912013-07-05 14:03:47 -0400429 old = NULL;
Josef Bacik7ef81ac2014-01-24 14:05:42 -0500430 count++;
Alexander Block69bca402012-06-19 07:42:26 -0600431 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 Baciked8c4912013-07-05 14:03:47 -0400438 if (ret > 0)
439 goto next;
Takashi Iwai4eb1f662014-07-28 10:57:04 +0200440 ret = ulist_add_merge_ptr(parents, eb->start,
441 eie, (void **)&old, GFP_NOFS);
Josef Baciked8c4912013-07-05 14:03:47 -0400442 if (ret < 0)
443 break;
444 if (!ret && extent_item_pos) {
445 while (old->next)
446 old = old->next;
447 old->next = eie;
Alexander Block69bca402012-06-19 07:42:26 -0600448 }
Wang Shilongf05c4742014-01-28 19:13:38 +0800449 eie = NULL;
Jan Schmidt8da6d582011-11-23 18:55:04 +0100450 }
Josef Baciked8c4912013-07-05 14:03:47 -0400451next:
Edmund Nadolskide47c9d2017-03-16 10:04:34 -0600452 if (time_seq == SEQ_LAST)
Qu Wenruo21633fc2015-04-16 14:54:50 +0800453 ret = btrfs_next_item(root, path);
454 else
455 ret = btrfs_next_old_item(root, path, time_seq);
Jan Schmidt8da6d582011-11-23 18:55:04 +0100456 }
457
Alexander Block69bca402012-06-19 07:42:26 -0600458 if (ret > 0)
459 ret = 0;
Wang Shilongf05c4742014-01-28 19:13:38 +0800460 else if (ret < 0)
461 free_inode_elem_list(eie);
Alexander Block69bca402012-06-19 07:42:26 -0600462 return ret;
Jan Schmidt8da6d582011-11-23 18:55:04 +0100463}
464
465/*
466 * resolve an indirect backref in the form (root_id, key, level)
467 * to a logical address
468 */
Jeff Mahoneye0c476b2017-06-28 21:56:57 -0600469static 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 Schmidt8da6d582011-11-23 18:55:04 +0100473{
Jan Schmidt8da6d582011-11-23 18:55:04 +0100474 struct btrfs_root *root;
475 struct btrfs_key root_key;
Jan Schmidt8da6d582011-11-23 18:55:04 +0100476 struct extent_buffer *eb;
477 int ret = 0;
478 int root_level;
479 int level = ref->level;
Wang Shilong538f72cd2014-01-23 13:47:48 +0800480 int index;
Jan Schmidt8da6d582011-11-23 18:55:04 +0100481
Jan Schmidt8da6d582011-11-23 18:55:04 +0100482 root_key.objectid = ref->root_id;
483 root_key.type = BTRFS_ROOT_ITEM_KEY;
484 root_key.offset = (u64)-1;
Wang Shilong538f72cd2014-01-23 13:47:48 +0800485
486 index = srcu_read_lock(&fs_info->subvol_srcu);
487
Josef Bacik2d9e9772015-11-05 14:37:58 -0800488 root = btrfs_get_fs_root(fs_info, &root_key, false);
Jan Schmidt8da6d582011-11-23 18:55:04 +0100489 if (IS_ERR(root)) {
Wang Shilong538f72cd2014-01-23 13:47:48 +0800490 srcu_read_unlock(&fs_info->subvol_srcu, index);
Jan Schmidt8da6d582011-11-23 18:55:04 +0100491 ret = PTR_ERR(root);
492 goto out;
493 }
494
Jeff Mahoneyf5ee5c92016-06-21 09:52:41 -0400495 if (btrfs_is_testing(fs_info)) {
Josef Bacikd9ee5222015-10-05 10:35:29 -0400496 srcu_read_unlock(&fs_info->subvol_srcu, index);
497 ret = -ENOENT;
498 goto out;
499 }
500
Josef Bacik9e351cc2014-03-13 15:42:13 -0400501 if (path->search_commit_root)
502 root_level = btrfs_header_level(root->commit_root);
Edmund Nadolskide47c9d2017-03-16 10:04:34 -0600503 else if (time_seq == SEQ_LAST)
Qu Wenruo21633fc2015-04-16 14:54:50 +0800504 root_level = btrfs_header_level(root->node);
Josef Bacik9e351cc2014-03-13 15:42:13 -0400505 else
506 root_level = btrfs_old_root_level(root, time_seq);
Jan Schmidt8da6d582011-11-23 18:55:04 +0100507
Wang Shilong538f72cd2014-01-23 13:47:48 +0800508 if (root_level + 1 == level) {
509 srcu_read_unlock(&fs_info->subvol_srcu, index);
Jan Schmidt8da6d582011-11-23 18:55:04 +0100510 goto out;
Wang Shilong538f72cd2014-01-23 13:47:48 +0800511 }
Jan Schmidt8da6d582011-11-23 18:55:04 +0100512
513 path->lowest_level = level;
Edmund Nadolskide47c9d2017-03-16 10:04:34 -0600514 if (time_seq == SEQ_LAST)
Qu Wenruo21633fc2015-04-16 14:54:50 +0800515 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 Shilong538f72cd2014-01-23 13:47:48 +0800520
521 /* root node has been locked, we can release @subvol_srcu safely here */
522 srcu_read_unlock(&fs_info->subvol_srcu, index);
523
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -0400524 btrfs_debug(fs_info,
525 "search slot in root %llu (level %d, ref count %d) returned %d for key (%llu %u %llu)",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +0200526 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 Schmidt8da6d582011-11-23 18:55:04 +0100529 if (ret < 0)
530 goto out;
531
532 eb = path->nodes[level];
Jan Schmidt93454572012-06-27 15:23:09 +0200533 while (!eb) {
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +0530534 if (WARN_ON(!level)) {
Jan Schmidt93454572012-06-27 15:23:09 +0200535 ret = 1;
536 goto out;
537 }
538 level--;
539 eb = path->nodes[level];
Jan Schmidt8da6d582011-11-23 18:55:04 +0100540 }
541
Josef Bacik7ef81ac2014-01-24 14:05:42 -0500542 ret = add_all_parents(root, path, parents, ref, level, time_seq,
Josef Bacik44853862014-03-19 13:35:14 -0400543 extent_item_pos, total_refs);
Jan Schmidt8da6d582011-11-23 18:55:04 +0100544out:
Josef Bacikda61d312013-06-12 16:20:08 -0400545 path->lowest_level = 0;
546 btrfs_release_path(path);
Jan Schmidt8da6d582011-11-23 18:55:04 +0100547 return ret;
548}
549
Jeff Mahoney4dae0772017-06-28 21:56:56 -0600550static struct extent_inode_elem *
551unode_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 Schmidt8da6d582011-11-23 18:55:04 +0100558/*
Edmund Nadolski86d5f992017-07-12 16:20:06 -0600559 * 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 Schmidt8da6d582011-11-23 18:55:04 +0100573 */
Jeff Mahoneye0c476b2017-06-28 21:56:57 -0600574static int resolve_indirect_refs(struct btrfs_fs_info *fs_info,
575 struct btrfs_path *path, u64 time_seq,
Edmund Nadolski86d5f992017-07-12 16:20:06 -0600576 struct preftrees *preftrees,
Jeff Mahoneye0c476b2017-06-28 21:56:57 -0600577 const u64 *extent_item_pos, u64 total_refs,
578 u64 root_objectid)
Jan Schmidt8da6d582011-11-23 18:55:04 +0100579{
580 int err;
581 int ret = 0;
Jan Schmidt8da6d582011-11-23 18:55:04 +0100582 struct ulist *parents;
583 struct ulist_node *node;
Jan Schmidtcd1b4132012-05-22 14:56:50 +0200584 struct ulist_iterator uiter;
Edmund Nadolski86d5f992017-07-12 16:20:06 -0600585 struct rb_node *rnode;
Jan Schmidt8da6d582011-11-23 18:55:04 +0100586
587 parents = ulist_alloc(GFP_NOFS);
588 if (!parents)
589 return -ENOMEM;
590
591 /*
Edmund Nadolski86d5f992017-07-12 16:20:06 -0600592 * 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 Schmidt8da6d582011-11-23 18:55:04 +0100596 */
Edmund Nadolski86d5f992017-07-12 16:20:06 -0600597 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 Mahoney6c336b22017-07-12 16:20:07 -0600608 preftrees->indirect.count--;
Edmund Nadolski86d5f992017-07-12 16:20:06 -0600609
610 if (ref->count == 0) {
611 free_pref(ref);
Jan Schmidt8da6d582011-11-23 18:55:04 +0100612 continue;
Edmund Nadolski86d5f992017-07-12 16:20:06 -0600613 }
614
Josef Bacikdc046b12014-09-10 16:20:45 -0400615 if (root_objectid && ref->root_id != root_objectid) {
Edmund Nadolski86d5f992017-07-12 16:20:06 -0600616 free_pref(ref);
Josef Bacikdc046b12014-09-10 16:20:45 -0400617 ret = BACKREF_FOUND_SHARED;
618 goto out;
619 }
Jeff Mahoneye0c476b2017-06-28 21:56:57 -0600620 err = resolve_indirect_ref(fs_info, path, time_seq, ref,
621 parents, extent_item_pos,
622 total_refs);
Wang Shilong95def2ed2014-01-23 13:47:49 +0800623 /*
624 * we can only tolerate ENOENT,otherwise,we should catch error
625 * and return directly.
626 */
627 if (err == -ENOENT) {
Jeff Mahoney00142752017-07-12 16:20:08 -0600628 prelim_ref_insert(fs_info, &preftrees->direct, ref);
Jan Schmidt8da6d582011-11-23 18:55:04 +0100629 continue;
Wang Shilong95def2ed2014-01-23 13:47:49 +0800630 } else if (err) {
Edmund Nadolski86d5f992017-07-12 16:20:06 -0600631 free_pref(ref);
Wang Shilong95def2ed2014-01-23 13:47:49 +0800632 ret = err;
633 goto out;
634 }
Jan Schmidt8da6d582011-11-23 18:55:04 +0100635
636 /* we put the first parent into the ref at hand */
Jan Schmidtcd1b4132012-05-22 14:56:50 +0200637 ULIST_ITER_INIT(&uiter);
638 node = ulist_next(parents, &uiter);
Jan Schmidt8da6d582011-11-23 18:55:04 +0100639 ref->parent = node ? node->val : 0;
Jeff Mahoney4dae0772017-06-28 21:56:56 -0600640 ref->inode_list = unode_aux_to_inode_list(node);
Jan Schmidt8da6d582011-11-23 18:55:04 +0100641
Edmund Nadolski86d5f992017-07-12 16:20:06 -0600642 /* Add a prelim_ref(s) for any other parent(s). */
Jan Schmidtcd1b4132012-05-22 14:56:50 +0200643 while ((node = ulist_next(parents, &uiter))) {
Edmund Nadolski86d5f992017-07-12 16:20:06 -0600644 struct prelim_ref *new_ref;
645
Wang Shilongb9e9a6c2013-08-09 13:25:36 +0800646 new_ref = kmem_cache_alloc(btrfs_prelim_ref_cache,
647 GFP_NOFS);
Jan Schmidt8da6d582011-11-23 18:55:04 +0100648 if (!new_ref) {
Edmund Nadolski86d5f992017-07-12 16:20:06 -0600649 free_pref(ref);
Jan Schmidt8da6d582011-11-23 18:55:04 +0100650 ret = -ENOMEM;
Wang Shilonge36902d2013-04-15 10:26:38 +0000651 goto out;
Jan Schmidt8da6d582011-11-23 18:55:04 +0100652 }
653 memcpy(new_ref, ref, sizeof(*ref));
654 new_ref->parent = node->val;
Jeff Mahoney4dae0772017-06-28 21:56:56 -0600655 new_ref->inode_list = unode_aux_to_inode_list(node);
Jeff Mahoney00142752017-07-12 16:20:08 -0600656 prelim_ref_insert(fs_info, &preftrees->direct, new_ref);
Jan Schmidt8da6d582011-11-23 18:55:04 +0100657 }
Edmund Nadolski86d5f992017-07-12 16:20:06 -0600658
659 /* Now it's a direct ref, put it in the the direct tree */
Jeff Mahoney00142752017-07-12 16:20:08 -0600660 prelim_ref_insert(fs_info, &preftrees->direct, ref);
Edmund Nadolski86d5f992017-07-12 16:20:06 -0600661
Jan Schmidt8da6d582011-11-23 18:55:04 +0100662 ulist_reinit(parents);
Edmund Nadolski9dd14fd2017-07-12 16:20:09 -0600663 cond_resched();
Jan Schmidt8da6d582011-11-23 18:55:04 +0100664 }
Wang Shilonge36902d2013-04-15 10:26:38 +0000665out:
Jan Schmidt8da6d582011-11-23 18:55:04 +0100666 ulist_free(parents);
667 return ret;
668}
669
Jan Schmidtd5c88b72012-05-15 17:55:51 +0200670/*
671 * read tree blocks and add keys where required.
672 */
Jeff Mahoneye0c476b2017-06-28 21:56:57 -0600673static int add_missing_keys(struct btrfs_fs_info *fs_info,
Edmund Nadolski86d5f992017-07-12 16:20:06 -0600674 struct preftrees *preftrees)
Jan Schmidtd5c88b72012-05-15 17:55:51 +0200675{
Jeff Mahoneye0c476b2017-06-28 21:56:57 -0600676 struct prelim_ref *ref;
Jan Schmidtd5c88b72012-05-15 17:55:51 +0200677 struct extent_buffer *eb;
Edmund Nadolski86d5f992017-07-12 16:20:06 -0600678 struct preftree *tree = &preftrees->indirect_missing_keys;
679 struct rb_node *node;
Jan Schmidtd5c88b72012-05-15 17:55:51 +0200680
Edmund Nadolski86d5f992017-07-12 16:20:06 -0600681 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 Schmidtd5c88b72012-05-15 17:55:51 +0200687 BUG_ON(!ref->wanted_disk_byte);
Edmund Nadolski86d5f992017-07-12 16:20:06 -0600688
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400689 eb = read_tree_block(fs_info, ref->wanted_disk_byte, 0);
Liu Bo64c043d2015-05-25 17:30:15 +0800690 if (IS_ERR(eb)) {
Edmund Nadolski86d5f992017-07-12 16:20:06 -0600691 free_pref(ref);
Liu Bo64c043d2015-05-25 17:30:15 +0800692 return PTR_ERR(eb);
693 } else if (!extent_buffer_uptodate(eb)) {
Edmund Nadolski86d5f992017-07-12 16:20:06 -0600694 free_pref(ref);
Josef Bacik416bc652013-04-23 14:17:42 -0400695 free_extent_buffer(eb);
696 return -EIO;
697 }
Jan Schmidtd5c88b72012-05-15 17:55:51 +0200698 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 Mahoney00142752017-07-12 16:20:08 -0600705 prelim_ref_insert(fs_info, &preftrees->indirect, ref);
Edmund Nadolski9dd14fd2017-07-12 16:20:09 -0600706 cond_resched();
Jan Schmidtd5c88b72012-05-15 17:55:51 +0200707 }
708 return 0;
709}
710
Jan Schmidt8da6d582011-11-23 18:55:04 +0100711/*
Jan Schmidt8da6d582011-11-23 18:55:04 +0100712 * add all currently queued delayed refs from this head whose seq nr is
713 * smaller or equal that seq to the list
714 */
Jeff Mahoney00142752017-07-12 16:20:08 -0600715static int add_delayed_refs(const struct btrfs_fs_info *fs_info,
716 struct btrfs_delayed_ref_head *head, u64 seq,
Edmund Nadolski86d5f992017-07-12 16:20:06 -0600717 struct preftrees *preftrees, u64 *total_refs,
Jeff Mahoneye0c476b2017-06-28 21:56:57 -0600718 u64 inum)
Jan Schmidt8da6d582011-11-23 18:55:04 +0100719{
Qu Wenruoc6fc2452015-03-30 17:03:00 +0800720 struct btrfs_delayed_ref_node *node;
Jan Schmidt8da6d582011-11-23 18:55:04 +0100721 struct btrfs_delayed_extent_op *extent_op = head->extent_op;
Jan Schmidtd5c88b72012-05-15 17:55:51 +0200722 struct btrfs_key key;
Edmund Nadolski86d5f992017-07-12 16:20:06 -0600723 struct btrfs_key tmp_op_key;
724 struct btrfs_key *op_key = NULL;
Jan Schmidt8da6d582011-11-23 18:55:04 +0100725 int sgn;
Jan Schmidtb1375d62012-01-26 15:01:11 -0500726 int ret = 0;
Jan Schmidt8da6d582011-11-23 18:55:04 +0100727
Edmund Nadolski86d5f992017-07-12 16:20:06 -0600728 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 Schmidt8da6d582011-11-23 18:55:04 +0100732
Josef Bacikd7df2c72014-01-23 09:21:38 -0500733 spin_lock(&head->lock);
Qu Wenruoc6fc2452015-03-30 17:03:00 +0800734 list_for_each_entry(node, &head->ref_list, list) {
Jan Schmidt8da6d582011-11-23 18:55:04 +0100735 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 Bacik44853862014-03-19 13:35:14 -0400752 *total_refs += (node->ref_mod * sgn);
Jan Schmidt8da6d582011-11-23 18:55:04 +0100753 switch (node->type) {
754 case BTRFS_TREE_BLOCK_REF_KEY: {
Edmund Nadolski86d5f992017-07-12 16:20:06 -0600755 /* NORMAL INDIRECT METADATA backref */
Jan Schmidt8da6d582011-11-23 18:55:04 +0100756 struct btrfs_delayed_tree_ref *ref;
757
758 ref = btrfs_delayed_node_to_tree_ref(node);
Jeff Mahoney00142752017-07-12 16:20:08 -0600759 ret = add_indirect_ref(fs_info, preftrees, ref->root,
760 &tmp_op_key, ref->level + 1,
761 node->bytenr,
Edmund Nadolski86d5f992017-07-12 16:20:06 -0600762 node->ref_mod * sgn,
763 GFP_ATOMIC);
Jan Schmidt8da6d582011-11-23 18:55:04 +0100764 break;
765 }
766 case BTRFS_SHARED_BLOCK_REF_KEY: {
Edmund Nadolski86d5f992017-07-12 16:20:06 -0600767 /* SHARED DIRECT METADATA backref */
Jan Schmidt8da6d582011-11-23 18:55:04 +0100768 struct btrfs_delayed_tree_ref *ref;
769
770 ref = btrfs_delayed_node_to_tree_ref(node);
Edmund Nadolski86d5f992017-07-12 16:20:06 -0600771
Jeff Mahoney00142752017-07-12 16:20:08 -0600772 ret = add_direct_ref(fs_info, preftrees,
773 ref->level + 1, ref->parent,
774 node->bytenr, node->ref_mod * sgn,
Edmund Nadolski86d5f992017-07-12 16:20:06 -0600775 GFP_ATOMIC);
Jan Schmidt8da6d582011-11-23 18:55:04 +0100776 break;
777 }
778 case BTRFS_EXTENT_DATA_REF_KEY: {
Edmund Nadolski86d5f992017-07-12 16:20:06 -0600779 /* NORMAL INDIRECT DATA backref */
Jan Schmidt8da6d582011-11-23 18:55:04 +0100780 struct btrfs_delayed_data_ref *ref;
Jan Schmidt8da6d582011-11-23 18:55:04 +0100781 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 Bacikdc046b12014-09-10 16:20:45 -0400786
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 Mahoney00142752017-07-12 16:20:08 -0600796 ret = add_indirect_ref(fs_info, preftrees, ref->root,
797 &key, 0, node->bytenr,
Edmund Nadolski86d5f992017-07-12 16:20:06 -0600798 node->ref_mod * sgn,
799 GFP_ATOMIC);
Jan Schmidt8da6d582011-11-23 18:55:04 +0100800 break;
801 }
802 case BTRFS_SHARED_DATA_REF_KEY: {
Edmund Nadolski86d5f992017-07-12 16:20:06 -0600803 /* SHARED DIRECT FULL backref */
Jan Schmidt8da6d582011-11-23 18:55:04 +0100804 struct btrfs_delayed_data_ref *ref;
Jan Schmidt8da6d582011-11-23 18:55:04 +0100805
806 ref = btrfs_delayed_node_to_data_ref(node);
Edmund Nadolski86d5f992017-07-12 16:20:06 -0600807
Jeff Mahoney00142752017-07-12 16:20:08 -0600808 ret = add_direct_ref(fs_info, preftrees, 0,
809 ref->parent, node->bytenr,
Edmund Nadolski86d5f992017-07-12 16:20:06 -0600810 node->ref_mod * sgn,
Jeff Mahoneye0c476b2017-06-28 21:56:57 -0600811 GFP_ATOMIC);
Jan Schmidt8da6d582011-11-23 18:55:04 +0100812 break;
813 }
814 default:
815 WARN_ON(1);
816 }
Wang Shilong1149ab62013-04-10 11:22:50 +0000817 if (ret)
Josef Bacikd7df2c72014-01-23 09:21:38 -0500818 break;
Jan Schmidt8da6d582011-11-23 18:55:04 +0100819 }
Josef Bacikd7df2c72014-01-23 09:21:38 -0500820 spin_unlock(&head->lock);
821 return ret;
Jan Schmidt8da6d582011-11-23 18:55:04 +0100822}
823
824/*
825 * add all inline backrefs for bytenr to the list
826 */
Jeff Mahoney00142752017-07-12 16:20:08 -0600827static int add_inline_refs(const struct btrfs_fs_info *fs_info,
828 struct btrfs_path *path, u64 bytenr,
Edmund Nadolski86d5f992017-07-12 16:20:06 -0600829 int *info_level, struct preftrees *preftrees,
Jeff Mahoneye0c476b2017-06-28 21:56:57 -0600830 u64 *total_refs, u64 inum)
Jan Schmidt8da6d582011-11-23 18:55:04 +0100831{
Jan Schmidtb1375d62012-01-26 15:01:11 -0500832 int ret = 0;
Jan Schmidt8da6d582011-11-23 18:55:04 +0100833 int slot;
834 struct extent_buffer *leaf;
835 struct btrfs_key key;
Josef Bacik261c84b2013-06-28 13:11:22 -0400836 struct btrfs_key found_key;
Jan Schmidt8da6d582011-11-23 18:55:04 +0100837 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 Schmidtdadcaf72012-05-22 13:43:25 +0200847 slot = path->slots[0];
Jan Schmidt8da6d582011-11-23 18:55:04 +0100848
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 Bacik44853862014-03-19 13:35:14 -0400854 *total_refs += btrfs_extent_refs(leaf, ei);
Josef Bacik261c84b2013-06-28 13:11:22 -0400855 btrfs_item_key_to_cpu(leaf, &found_key, slot);
Jan Schmidt8da6d582011-11-23 18:55:04 +0100856
857 ptr = (unsigned long)(ei + 1);
858 end = (unsigned long)ei + item_size;
859
Josef Bacik261c84b2013-06-28 13:11:22 -0400860 if (found_key.type == BTRFS_EXTENT_ITEM_KEY &&
861 flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
Jan Schmidt8da6d582011-11-23 18:55:04 +0100862 struct btrfs_tree_block_info *info;
Jan Schmidt8da6d582011-11-23 18:55:04 +0100863
864 info = (struct btrfs_tree_block_info *)ptr;
865 *info_level = btrfs_tree_block_level(leaf, info);
Jan Schmidt8da6d582011-11-23 18:55:04 +0100866 ptr += sizeof(struct btrfs_tree_block_info);
867 BUG_ON(ptr > end);
Josef Bacik261c84b2013-06-28 13:11:22 -0400868 } else if (found_key.type == BTRFS_METADATA_ITEM_KEY) {
869 *info_level = found_key.offset;
Jan Schmidt8da6d582011-11-23 18:55:04 +0100870 } 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 Mahoney00142752017-07-12 16:20:08 -0600885 ret = add_direct_ref(fs_info, preftrees,
886 *info_level + 1, offset,
Edmund Nadolski86d5f992017-07-12 16:20:06 -0600887 bytenr, 1, GFP_NOFS);
Jan Schmidt8da6d582011-11-23 18:55:04 +0100888 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 Nadolski86d5f992017-07-12 16:20:06 -0600895
Jeff Mahoney00142752017-07-12 16:20:08 -0600896 ret = add_direct_ref(fs_info, preftrees, 0, offset,
Jeff Mahoneye0c476b2017-06-28 21:56:57 -0600897 bytenr, count, GFP_NOFS);
Jan Schmidt8da6d582011-11-23 18:55:04 +0100898 break;
899 }
900 case BTRFS_TREE_BLOCK_REF_KEY:
Jeff Mahoney00142752017-07-12 16:20:08 -0600901 ret = add_indirect_ref(fs_info, preftrees, offset,
902 NULL, *info_level + 1,
903 bytenr, 1, GFP_NOFS);
Jan Schmidt8da6d582011-11-23 18:55:04 +0100904 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 Bacikdc046b12014-09-10 16:20:45 -0400916
917 if (inum && key.objectid != inum) {
918 ret = BACKREF_FOUND_SHARED;
919 break;
920 }
921
Jan Schmidt8da6d582011-11-23 18:55:04 +0100922 root = btrfs_extent_data_ref_root(leaf, dref);
Edmund Nadolski86d5f992017-07-12 16:20:06 -0600923
Jeff Mahoney00142752017-07-12 16:20:08 -0600924 ret = add_indirect_ref(fs_info, preftrees, root,
925 &key, 0, bytenr, count,
926 GFP_NOFS);
Jan Schmidt8da6d582011-11-23 18:55:04 +0100927 break;
928 }
929 default:
930 WARN_ON(1);
931 }
Wang Shilong1149ab62013-04-10 11:22:50 +0000932 if (ret)
933 return ret;
Jan Schmidt8da6d582011-11-23 18:55:04 +0100934 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 Mahoneye0c476b2017-06-28 21:56:57 -0600943static int add_keyed_refs(struct btrfs_fs_info *fs_info,
944 struct btrfs_path *path, u64 bytenr,
Edmund Nadolski86d5f992017-07-12 16:20:06 -0600945 int info_level, struct preftrees *preftrees,
946 u64 inum)
Jan Schmidt8da6d582011-11-23 18:55:04 +0100947{
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 Nadolski86d5f992017-07-12 16:20:06 -0600976 /* SHARED DIRECT METADATA backref */
Jeff Mahoney00142752017-07-12 16:20:08 -0600977 ret = add_direct_ref(fs_info, preftrees,
978 info_level + 1, key.offset,
979 bytenr, 1, GFP_NOFS);
Jan Schmidt8da6d582011-11-23 18:55:04 +0100980 break;
981 case BTRFS_SHARED_DATA_REF_KEY: {
Edmund Nadolski86d5f992017-07-12 16:20:06 -0600982 /* SHARED DIRECT FULL backref */
Jan Schmidt8da6d582011-11-23 18:55:04 +0100983 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 Mahoney00142752017-07-12 16:20:08 -0600989 ret = add_direct_ref(fs_info, preftrees, 0,
990 key.offset, bytenr, count,
991 GFP_NOFS);
Jan Schmidt8da6d582011-11-23 18:55:04 +0100992 break;
993 }
994 case BTRFS_TREE_BLOCK_REF_KEY:
Edmund Nadolski86d5f992017-07-12 16:20:06 -0600995 /* NORMAL INDIRECT METADATA backref */
Jeff Mahoney00142752017-07-12 16:20:08 -0600996 ret = add_indirect_ref(fs_info, preftrees, key.offset,
997 NULL, info_level + 1, bytenr,
998 1, GFP_NOFS);
Jan Schmidt8da6d582011-11-23 18:55:04 +0100999 break;
1000 case BTRFS_EXTENT_DATA_REF_KEY: {
Edmund Nadolski86d5f992017-07-12 16:20:06 -06001001 /* NORMAL INDIRECT DATA backref */
Jan Schmidt8da6d582011-11-23 18:55:04 +01001002 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 Bacikdc046b12014-09-10 16:20:45 -04001013
1014 if (inum && key.objectid != inum) {
1015 ret = BACKREF_FOUND_SHARED;
1016 break;
1017 }
1018
Jan Schmidt8da6d582011-11-23 18:55:04 +01001019 root = btrfs_extent_data_ref_root(leaf, dref);
Jeff Mahoney00142752017-07-12 16:20:08 -06001020 ret = add_indirect_ref(fs_info, preftrees, root,
1021 &key, 0, bytenr, count,
1022 GFP_NOFS);
Jan Schmidt8da6d582011-11-23 18:55:04 +01001023 break;
1024 }
1025 default:
1026 WARN_ON(1);
1027 }
Wang Shilong1149ab62013-04-10 11:22:50 +00001028 if (ret)
1029 return ret;
1030
Jan Schmidt8da6d582011-11-23 18:55:04 +01001031 }
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 Fasheh2c2ed5a2015-05-19 12:49:50 -07001042 * NOTE: This can return values > 0
1043 *
Edmund Nadolskide47c9d2017-03-16 10:04:34 -06001044 * If time_seq is set to SEQ_LAST, it will not search delayed_refs, and behave
Qu Wenruo21633fc2015-04-16 14:54:50 +08001045 * 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 Schmidt8da6d582011-11-23 18:55:04 +01001049 * FIXME some caching might speed things up
1050 */
1051static int find_parent_nodes(struct btrfs_trans_handle *trans,
1052 struct btrfs_fs_info *fs_info, u64 bytenr,
Jan Schmidt097b8a72012-06-21 11:08:04 +02001053 u64 time_seq, struct ulist *refs,
Josef Bacikdc046b12014-09-10 16:20:45 -04001054 struct ulist *roots, const u64 *extent_item_pos,
Edmund Nadolskif6954242017-06-28 21:56:59 -06001055 u64 root_objectid, u64 inum)
Jan Schmidt8da6d582011-11-23 18:55:04 +01001056{
1057 struct btrfs_key key;
1058 struct btrfs_path *path;
Jan Schmidt8da6d582011-11-23 18:55:04 +01001059 struct btrfs_delayed_ref_root *delayed_refs = NULL;
Li Zefand3b01062012-03-03 07:41:15 -05001060 struct btrfs_delayed_ref_head *head;
Jan Schmidt8da6d582011-11-23 18:55:04 +01001061 int info_level = 0;
1062 int ret;
Jeff Mahoneye0c476b2017-06-28 21:56:57 -06001063 struct prelim_ref *ref;
Edmund Nadolski86d5f992017-07-12 16:20:06 -06001064 struct rb_node *node;
Wang Shilongf05c4742014-01-28 19:13:38 +08001065 struct extent_inode_elem *eie = NULL;
Edmund Nadolski86d5f992017-07-12 16:20:06 -06001066 /* total of both direct AND indirect refs! */
Josef Bacik44853862014-03-19 13:35:14 -04001067 u64 total_refs = 0;
Edmund Nadolski86d5f992017-07-12 16:20:06 -06001068 struct preftrees preftrees = {
1069 .direct = PREFTREE_INIT,
1070 .indirect = PREFTREE_INIT,
1071 .indirect_missing_keys = PREFTREE_INIT
1072 };
Jan Schmidt8da6d582011-11-23 18:55:04 +01001073
1074 key.objectid = bytenr;
Jan Schmidt8da6d582011-11-23 18:55:04 +01001075 key.offset = (u64)-1;
Josef Bacik261c84b2013-06-28 13:11:22 -04001076 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 Schmidt8da6d582011-11-23 18:55:04 +01001080
1081 path = btrfs_alloc_path();
1082 if (!path)
1083 return -ENOMEM;
Wang Shilonge84752d2014-02-13 11:19:47 +08001084 if (!trans) {
Josef Bacikda61d312013-06-12 16:20:08 -04001085 path->search_commit_root = 1;
Wang Shilonge84752d2014-02-13 11:19:47 +08001086 path->skip_locking = 1;
1087 }
Jan Schmidt8da6d582011-11-23 18:55:04 +01001088
Edmund Nadolskide47c9d2017-03-16 10:04:34 -06001089 if (time_seq == SEQ_LAST)
Qu Wenruo21633fc2015-04-16 14:54:50 +08001090 path->skip_locking = 1;
1091
Jan Schmidt8da6d582011-11-23 18:55:04 +01001092 /*
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 */
1097again:
Li Zefand3b01062012-03-03 07:41:15 -05001098 head = NULL;
1099
Jan Schmidt8da6d582011-11-23 18:55:04 +01001100 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 Bacikfaa2dbf2014-05-07 17:06:09 -04001105#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
Qu Wenruo21633fc2015-04-16 14:54:50 +08001106 if (trans && likely(trans->type != __TRANS_DUMMY) &&
Edmund Nadolskide47c9d2017-03-16 10:04:34 -06001107 time_seq != SEQ_LAST) {
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04001108#else
Edmund Nadolskide47c9d2017-03-16 10:04:34 -06001109 if (trans && time_seq != SEQ_LAST) {
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04001110#endif
Jan Schmidt7a3ae2f2012-03-23 17:32:28 +01001111 /*
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 Bof72ad18e2017-01-30 12:24:37 -08001117 head = btrfs_find_delayed_ref_head(delayed_refs, bytenr);
Jan Schmidt7a3ae2f2012-03-23 17:32:28 +01001118 if (head) {
1119 if (!mutex_trylock(&head->mutex)) {
Elena Reshetova6df8cdf2017-03-03 10:55:15 +02001120 refcount_inc(&head->node.refs);
Jan Schmidt7a3ae2f2012-03-23 17:32:28 +01001121 spin_unlock(&delayed_refs->lock);
Jan Schmidt8da6d582011-11-23 18:55:04 +01001122
Jan Schmidt7a3ae2f2012-03-23 17:32:28 +01001123 btrfs_release_path(path);
Jan Schmidt8da6d582011-11-23 18:55:04 +01001124
Jan Schmidt7a3ae2f2012-03-23 17:32:28 +01001125 /*
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 Bacikd7df2c72014-01-23 09:21:38 -05001134 spin_unlock(&delayed_refs->lock);
Jeff Mahoney00142752017-07-12 16:20:08 -06001135 ret = add_delayed_refs(fs_info, head, time_seq,
1136 &preftrees, &total_refs, inum);
Jan Schmidt155725c2012-06-22 14:01:00 +02001137 mutex_unlock(&head->mutex);
Josef Bacikd7df2c72014-01-23 09:21:38 -05001138 if (ret)
Jan Schmidt7a3ae2f2012-03-23 17:32:28 +01001139 goto out;
Josef Bacikd7df2c72014-01-23 09:21:38 -05001140 } else {
1141 spin_unlock(&delayed_refs->lock);
Jan Schmidt8da6d582011-11-23 18:55:04 +01001142 }
Jan Schmidt8da6d582011-11-23 18:55:04 +01001143 }
Jan Schmidt8da6d582011-11-23 18:55:04 +01001144
1145 if (path->slots[0]) {
1146 struct extent_buffer *leaf;
1147 int slot;
1148
Jan Schmidtdadcaf72012-05-22 13:43:25 +02001149 path->slots[0]--;
Jan Schmidt8da6d582011-11-23 18:55:04 +01001150 leaf = path->nodes[0];
Jan Schmidtdadcaf72012-05-22 13:43:25 +02001151 slot = path->slots[0];
Jan Schmidt8da6d582011-11-23 18:55:04 +01001152 btrfs_item_key_to_cpu(leaf, &key, slot);
1153 if (key.objectid == bytenr &&
Josef Bacik261c84b2013-06-28 13:11:22 -04001154 (key.type == BTRFS_EXTENT_ITEM_KEY ||
1155 key.type == BTRFS_METADATA_ITEM_KEY)) {
Jeff Mahoney00142752017-07-12 16:20:08 -06001156 ret = add_inline_refs(fs_info, path, bytenr,
1157 &info_level, &preftrees,
1158 &total_refs, inum);
Jan Schmidt8da6d582011-11-23 18:55:04 +01001159 if (ret)
1160 goto out;
Jeff Mahoneye0c476b2017-06-28 21:56:57 -06001161 ret = add_keyed_refs(fs_info, path, bytenr, info_level,
Edmund Nadolski86d5f992017-07-12 16:20:06 -06001162 &preftrees, inum);
Jan Schmidt8da6d582011-11-23 18:55:04 +01001163 if (ret)
1164 goto out;
1165 }
1166 }
Edmund Nadolski86d5f992017-07-12 16:20:06 -06001167
Jan Schmidt8da6d582011-11-23 18:55:04 +01001168 btrfs_release_path(path);
1169
Edmund Nadolski86d5f992017-07-12 16:20:06 -06001170 ret = add_missing_keys(fs_info, &preftrees);
Jan Schmidtd5c88b72012-05-15 17:55:51 +02001171 if (ret)
1172 goto out;
1173
Edmund Nadolski86d5f992017-07-12 16:20:06 -06001174 WARN_ON(!RB_EMPTY_ROOT(&preftrees.indirect_missing_keys.root));
Jan Schmidt8da6d582011-11-23 18:55:04 +01001175
Edmund Nadolski86d5f992017-07-12 16:20:06 -06001176 ret = resolve_indirect_refs(fs_info, path, time_seq, &preftrees,
Jeff Mahoneye0c476b2017-06-28 21:56:57 -06001177 extent_item_pos, total_refs,
1178 root_objectid);
Jan Schmidt8da6d582011-11-23 18:55:04 +01001179 if (ret)
1180 goto out;
1181
Edmund Nadolski86d5f992017-07-12 16:20:06 -06001182 WARN_ON(!RB_EMPTY_ROOT(&preftrees.indirect.root));
Jan Schmidt8da6d582011-11-23 18:55:04 +01001183
Edmund Nadolski86d5f992017-07-12 16:20:06 -06001184 /*
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 Lawall6c1500f2012-11-03 20:30:18 +00001195 WARN_ON(ref->count < 0);
Wang Shilong98cfee212014-02-01 00:42:05 +08001196 if (roots && ref->count && ref->root_id && ref->parent == 0) {
Josef Bacikdc046b12014-09-10 16:20:45 -04001197 if (root_objectid && ref->root_id != root_objectid) {
1198 ret = BACKREF_FOUND_SHARED;
1199 goto out;
1200 }
1201
Jan Schmidt8da6d582011-11-23 18:55:04 +01001202 /* no parent == root of tree */
1203 ret = ulist_add(roots, ref->root_id, 0, GFP_NOFS);
Wang Shilongf1723932013-03-29 23:03:21 +00001204 if (ret < 0)
1205 goto out;
Jan Schmidt8da6d582011-11-23 18:55:04 +01001206 }
1207 if (ref->count && ref->parent) {
Josef Bacik8a564572014-06-05 16:08:45 -04001208 if (extent_item_pos && !ref->inode_list &&
1209 ref->level == 0) {
Jan Schmidt976b1902012-05-17 16:43:03 +02001210 struct extent_buffer *eb;
David Sterba707e8a02014-06-04 19:22:26 +02001211
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001212 eb = read_tree_block(fs_info, ref->parent, 0);
Liu Bo64c043d2015-05-25 17:30:15 +08001213 if (IS_ERR(eb)) {
1214 ret = PTR_ERR(eb);
1215 goto out;
1216 } else if (!extent_buffer_uptodate(eb)) {
Josef Bacik416bc652013-04-23 14:17:42 -04001217 free_extent_buffer(eb);
Wang Shilongc16c2e22013-05-08 08:10:25 +00001218 ret = -EIO;
1219 goto out;
Josef Bacik416bc652013-04-23 14:17:42 -04001220 }
Filipe Manana6f7ff6d2014-07-02 20:07:54 +01001221 btrfs_tree_read_lock(eb);
1222 btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK);
Jan Schmidt976b1902012-05-17 16:43:03 +02001223 ret = find_extent_in_eb(eb, bytenr,
1224 *extent_item_pos, &eie);
Filipe Manana6f7ff6d2014-07-02 20:07:54 +01001225 btrfs_tree_read_unlock_blocking(eb);
Jan Schmidt976b1902012-05-17 16:43:03 +02001226 free_extent_buffer(eb);
Filipe David Borba Mananaf5929cd2013-07-31 00:26:35 +01001227 if (ret < 0)
1228 goto out;
1229 ref->inode_list = eie;
Jan Schmidt976b1902012-05-17 16:43:03 +02001230 }
Takashi Iwai4eb1f662014-07-28 10:57:04 +02001231 ret = ulist_add_merge_ptr(refs, ref->parent,
1232 ref->inode_list,
1233 (void **)&eie, GFP_NOFS);
Wang Shilongf1723932013-03-29 23:03:21 +00001234 if (ret < 0)
1235 goto out;
Jan Schmidt33019582012-05-30 18:05:21 +02001236 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 Shilongf05c4742014-01-28 19:13:38 +08001246 eie = NULL;
Jan Schmidt8da6d582011-11-23 18:55:04 +01001247 }
Edmund Nadolski9dd14fd2017-07-12 16:20:09 -06001248 cond_resched();
Jan Schmidt8da6d582011-11-23 18:55:04 +01001249 }
1250
1251out:
Jan Schmidt8da6d582011-11-23 18:55:04 +01001252 btrfs_free_path(path);
Edmund Nadolski86d5f992017-07-12 16:20:06 -06001253
1254 prelim_release(&preftrees.direct);
1255 prelim_release(&preftrees.indirect);
1256 prelim_release(&preftrees.indirect_missing_keys);
1257
Wang Shilongf05c4742014-01-28 19:13:38 +08001258 if (ret < 0)
1259 free_inode_elem_list(eie);
Jan Schmidt8da6d582011-11-23 18:55:04 +01001260 return ret;
1261}
1262
Jan Schmidt976b1902012-05-17 16:43:03 +02001263static void free_leaf_list(struct ulist *blocks)
1264{
1265 struct ulist_node *node = NULL;
1266 struct extent_inode_elem *eie;
Jan Schmidt976b1902012-05-17 16:43:03 +02001267 struct ulist_iterator uiter;
1268
1269 ULIST_ITER_INIT(&uiter);
1270 while ((node = ulist_next(blocks, &uiter))) {
1271 if (!node->aux)
1272 continue;
Jeff Mahoney4dae0772017-06-28 21:56:56 -06001273 eie = unode_aux_to_inode_list(node);
Wang Shilongf05c4742014-01-28 19:13:38 +08001274 free_inode_elem_list(eie);
Jan Schmidt976b1902012-05-17 16:43:03 +02001275 node->aux = 0;
1276 }
1277
1278 ulist_free(blocks);
1279}
1280
Jan Schmidt8da6d582011-11-23 18:55:04 +01001281/*
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 */
1289static int btrfs_find_all_leafs(struct btrfs_trans_handle *trans,
1290 struct btrfs_fs_info *fs_info, u64 bytenr,
Jan Schmidt097b8a72012-06-21 11:08:04 +02001291 u64 time_seq, struct ulist **leafs,
Jan Schmidt976b1902012-05-17 16:43:03 +02001292 const u64 *extent_item_pos)
Jan Schmidt8da6d582011-11-23 18:55:04 +01001293{
Jan Schmidt8da6d582011-11-23 18:55:04 +01001294 int ret;
1295
Jan Schmidt8da6d582011-11-23 18:55:04 +01001296 *leafs = ulist_alloc(GFP_NOFS);
Wang Shilong98cfee212014-02-01 00:42:05 +08001297 if (!*leafs)
Jan Schmidt8da6d582011-11-23 18:55:04 +01001298 return -ENOMEM;
Jan Schmidt8da6d582011-11-23 18:55:04 +01001299
Lu Fengqiafce7722016-06-13 09:36:46 +08001300 ret = find_parent_nodes(trans, fs_info, bytenr, time_seq,
Edmund Nadolskif6954242017-06-28 21:56:59 -06001301 *leafs, NULL, extent_item_pos, 0, 0);
Jan Schmidt8da6d582011-11-23 18:55:04 +01001302 if (ret < 0 && ret != -ENOENT) {
Jan Schmidt976b1902012-05-17 16:43:03 +02001303 free_leaf_list(*leafs);
Jan Schmidt8da6d582011-11-23 18:55:04 +01001304 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 Mahoneye0c476b2017-06-28 21:56:57 -06001323static 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 Schmidt8da6d582011-11-23 18:55:04 +01001326{
1327 struct ulist *tmp;
1328 struct ulist_node *node = NULL;
Jan Schmidtcd1b4132012-05-22 14:56:50 +02001329 struct ulist_iterator uiter;
Jan Schmidt8da6d582011-11-23 18:55:04 +01001330 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 Schmidtcd1b4132012-05-22 14:56:50 +02001341 ULIST_ITER_INIT(&uiter);
Jan Schmidt8da6d582011-11-23 18:55:04 +01001342 while (1) {
Lu Fengqiafce7722016-06-13 09:36:46 +08001343 ret = find_parent_nodes(trans, fs_info, bytenr, time_seq,
Edmund Nadolskif6954242017-06-28 21:56:59 -06001344 tmp, *roots, NULL, 0, 0);
Jan Schmidt8da6d582011-11-23 18:55:04 +01001345 if (ret < 0 && ret != -ENOENT) {
1346 ulist_free(tmp);
1347 ulist_free(*roots);
1348 return ret;
1349 }
Jan Schmidtcd1b4132012-05-22 14:56:50 +02001350 node = ulist_next(tmp, &uiter);
Jan Schmidt8da6d582011-11-23 18:55:04 +01001351 if (!node)
1352 break;
1353 bytenr = node->val;
Wang Shilongbca1a292014-01-26 22:32:18 +08001354 cond_resched();
Jan Schmidt8da6d582011-11-23 18:55:04 +01001355 }
1356
1357 ulist_free(tmp);
1358 return 0;
1359}
1360
Josef Bacik9e351cc2014-03-13 15:42:13 -04001361int 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 Mahoneye0c476b2017-06-28 21:56:57 -06001369 ret = btrfs_find_all_roots_safe(trans, fs_info, bytenr,
1370 time_seq, roots);
Josef Bacik9e351cc2014-03-13 15:42:13 -04001371 if (!trans)
1372 up_read(&fs_info->commit_root_sem);
1373 return ret;
1374}
1375
Mark Fasheh2c2ed5a2015-05-19 12:49:50 -07001376/**
1377 * btrfs_check_shared - tell us whether an extent is shared
1378 *
Mark Fasheh2c2ed5a2015-05-19 12:49:50 -07001379 * 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 Nadolskibb739cf2017-06-28 21:56:58 -06001385 * This attempts to allocate a transaction in order to account for
1386 * delayed refs, but continues on even when the alloc fails.
1387 *
Mark Fasheh2c2ed5a2015-05-19 12:49:50 -07001388 * Return: 0 if extent is not shared, 1 if it is shared, < 0 on error.
1389 */
Edmund Nadolskibb739cf2017-06-28 21:56:58 -06001390int btrfs_check_shared(struct btrfs_root *root, u64 inum, u64 bytenr)
Josef Bacikdc046b12014-09-10 16:20:45 -04001391{
Edmund Nadolskibb739cf2017-06-28 21:56:58 -06001392 struct btrfs_fs_info *fs_info = root->fs_info;
1393 struct btrfs_trans_handle *trans;
Josef Bacikdc046b12014-09-10 16:20:45 -04001394 struct ulist *tmp = NULL;
1395 struct ulist *roots = NULL;
1396 struct ulist_iterator uiter;
1397 struct ulist_node *node;
David Sterba3284da72015-02-25 15:47:32 +01001398 struct seq_list elem = SEQ_LIST_INIT(elem);
Josef Bacikdc046b12014-09-10 16:20:45 -04001399 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 Nadolskibb739cf2017-06-28 21:56:58 -06001409 trans = btrfs_join_transaction(root);
1410 if (IS_ERR(trans)) {
1411 trans = NULL;
Josef Bacikdc046b12014-09-10 16:20:45 -04001412 down_read(&fs_info->commit_root_sem);
Edmund Nadolskibb739cf2017-06-28 21:56:58 -06001413 } else {
1414 btrfs_get_tree_mod_seq(fs_info, &elem);
1415 }
1416
Josef Bacikdc046b12014-09-10 16:20:45 -04001417 ULIST_ITER_INIT(&uiter);
1418 while (1) {
1419 ret = find_parent_nodes(trans, fs_info, bytenr, elem.seq, tmp,
Edmund Nadolskif6954242017-06-28 21:56:59 -06001420 roots, NULL, root->objectid, inum);
Josef Bacikdc046b12014-09-10 16:20:45 -04001421 if (ret == BACKREF_FOUND_SHARED) {
Mark Fasheh2c2ed5a2015-05-19 12:49:50 -07001422 /* this is the only condition under which we return 1 */
Josef Bacikdc046b12014-09-10 16:20:45 -04001423 ret = 1;
1424 break;
1425 }
1426 if (ret < 0 && ret != -ENOENT)
1427 break;
Mark Fasheh2c2ed5a2015-05-19 12:49:50 -07001428 ret = 0;
Josef Bacikdc046b12014-09-10 16:20:45 -04001429 node = ulist_next(tmp, &uiter);
1430 if (!node)
1431 break;
1432 bytenr = node->val;
1433 cond_resched();
1434 }
Edmund Nadolskibb739cf2017-06-28 21:56:58 -06001435
1436 if (trans) {
Josef Bacikdc046b12014-09-10 16:20:45 -04001437 btrfs_put_tree_mod_seq(fs_info, &elem);
Edmund Nadolskibb739cf2017-06-28 21:56:58 -06001438 btrfs_end_transaction(trans);
1439 } else {
Josef Bacikdc046b12014-09-10 16:20:45 -04001440 up_read(&fs_info->commit_root_sem);
Edmund Nadolskibb739cf2017-06-28 21:56:58 -06001441 }
Josef Bacikdc046b12014-09-10 16:20:45 -04001442 ulist_free(tmp);
1443 ulist_free(roots);
1444 return ret;
1445}
1446
Mark Fashehf1863732012-08-08 11:32:27 -07001447int 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 Mahoney73980be2017-06-28 21:56:55 -06001456 const struct extent_buffer *leaf;
Mark Fashehf1863732012-08-08 11:32:27 -07001457 unsigned long ptr;
1458
1459 key.objectid = inode_objectid;
David Sterba962a2982014-06-04 18:41:45 +02001460 key.type = BTRFS_INODE_EXTREF_KEY;
Mark Fashehf1863732012-08-08 11:32:27 -07001461 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 Sterba962a2982014-06-04 18:41:45 +02001500 if (found_key.type != BTRFS_INODE_EXTREF_KEY)
Mark Fashehf1863732012-08-08 11:32:27 -07001501 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 Sandeen48a3b632013-04-25 20:41:01 +00001515/*
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 Schmidt96b5bd72012-10-15 08:30:45 +00001529char *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 Schmidta542ad12011-06-13 19:52:59 +02001533{
Jan Schmidta542ad12011-06-13 19:52:59 +02001534 int slot;
1535 u64 next_inum;
1536 int ret;
Gabriel de Perthuis661bec62012-10-10 08:50:47 -06001537 s64 bytes_left = ((s64)size) - 1;
Jan Schmidta542ad12011-06-13 19:52:59 +02001538 struct extent_buffer *eb = eb_in;
1539 struct btrfs_key found_key;
Jan Schmidtb916a592012-04-13 12:28:08 +02001540 int leave_spinning = path->leave_spinning;
Mark Fashehd24bec32012-08-08 11:33:54 -07001541 struct btrfs_inode_ref *iref;
Jan Schmidta542ad12011-06-13 19:52:59 +02001542
1543 if (bytes_left >= 0)
1544 dest[bytes_left] = '\0';
1545
Jan Schmidtb916a592012-04-13 12:28:08 +02001546 path->leave_spinning = 1;
Jan Schmidta542ad12011-06-13 19:52:59 +02001547 while (1) {
Mark Fashehd24bec32012-08-08 11:33:54 -07001548 bytes_left -= name_len;
Jan Schmidta542ad12011-06-13 19:52:59 +02001549 if (bytes_left >= 0)
1550 read_extent_buffer(eb, dest + bytes_left,
Mark Fashehd24bec32012-08-08 11:33:54 -07001551 name_off, name_len);
Jan Schmidtb916a592012-04-13 12:28:08 +02001552 if (eb != eb_in) {
Filipe Manana0c0fe3b2016-02-03 19:17:27 +00001553 if (!path->skip_locking)
1554 btrfs_tree_read_unlock_blocking(eb);
Jan Schmidta542ad12011-06-13 19:52:59 +02001555 free_extent_buffer(eb);
Jan Schmidtb916a592012-04-13 12:28:08 +02001556 }
David Sterbac234a242015-01-02 19:03:17 +01001557 ret = btrfs_find_item(fs_root, path, parent, 0,
1558 BTRFS_INODE_REF_KEY, &found_key);
Jan Schmidt8f24b492012-02-08 16:01:01 +01001559 if (ret > 0)
1560 ret = -ENOENT;
Jan Schmidta542ad12011-06-13 19:52:59 +02001561 if (ret)
1562 break;
Mark Fashehd24bec32012-08-08 11:33:54 -07001563
Jan Schmidta542ad12011-06-13 19:52:59 +02001564 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 Schmidtb916a592012-04-13 12:28:08 +02001573 if (eb != eb_in) {
Filipe Manana0c0fe3b2016-02-03 19:17:27 +00001574 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 Schmidtb916a592012-04-13 12:28:08 +02001578 }
Jan Schmidta542ad12011-06-13 19:52:59 +02001579 btrfs_release_path(path);
Jan Schmidta542ad12011-06-13 19:52:59 +02001580 iref = btrfs_item_ptr(eb, slot, struct btrfs_inode_ref);
Mark Fashehd24bec32012-08-08 11:33:54 -07001581
1582 name_len = btrfs_inode_ref_name_len(eb, iref);
1583 name_off = (unsigned long)(iref + 1);
1584
Jan Schmidta542ad12011-06-13 19:52:59 +02001585 parent = next_inum;
1586 --bytes_left;
1587 if (bytes_left >= 0)
1588 dest[bytes_left] = '/';
1589 }
1590
1591 btrfs_release_path(path);
Jan Schmidtb916a592012-04-13 12:28:08 +02001592 path->leave_spinning = leave_spinning;
Jan Schmidta542ad12011-06-13 19:52:59 +02001593
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 */
1605int extent_from_logical(struct btrfs_fs_info *fs_info, u64 logical,
Liu Bo69917e42012-09-07 20:01:28 -06001606 struct btrfs_path *path, struct btrfs_key *found_key,
1607 u64 *flags_ret)
Jan Schmidta542ad12011-06-13 19:52:59 +02001608{
1609 int ret;
1610 u64 flags;
Josef Bacik261c84b2013-06-28 13:11:22 -04001611 u64 size = 0;
Jan Schmidta542ad12011-06-13 19:52:59 +02001612 u32 item_size;
Jeff Mahoney73980be2017-06-28 21:56:55 -06001613 const struct extent_buffer *eb;
Jan Schmidta542ad12011-06-13 19:52:59 +02001614 struct btrfs_extent_item *ei;
1615 struct btrfs_key key;
1616
Josef Bacik261c84b2013-06-28 13:11:22 -04001617 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 Schmidta542ad12011-06-13 19:52:59 +02001621 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 Schmidta542ad12011-06-13 19:52:59 +02001627
Wang Shilong850a8cd2014-02-06 20:02:29 +08001628 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 Bacik580f0a62014-01-23 16:03:45 -05001633 }
Wang Shilong850a8cd2014-02-06 20:02:29 +08001634 btrfs_item_key_to_cpu(path->nodes[0], found_key, path->slots[0]);
Josef Bacik261c84b2013-06-28 13:11:22 -04001635 if (found_key->type == BTRFS_METADATA_ITEM_KEY)
Jeff Mahoneyda170662016-06-15 09:22:56 -04001636 size = fs_info->nodesize;
Josef Bacik261c84b2013-06-28 13:11:22 -04001637 else if (found_key->type == BTRFS_EXTENT_ITEM_KEY)
1638 size = found_key->offset;
1639
Josef Bacik580f0a62014-01-23 16:03:45 -05001640 if (found_key->objectid > logical ||
Josef Bacik261c84b2013-06-28 13:11:22 -04001641 found_key->objectid + size <= logical) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04001642 btrfs_debug(fs_info,
1643 "logical %llu is not within any extent", logical);
Jan Schmidta542ad12011-06-13 19:52:59 +02001644 return -ENOENT;
Jan Schmidt4692cf52011-12-02 14:56:41 +01001645 }
Jan Schmidta542ad12011-06-13 19:52:59 +02001646
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 Mahoneyab8d0fc2016-09-20 10:05:02 -04001654 btrfs_debug(fs_info,
1655 "logical %llu is at position %llu within the extent (%llu EXTENT_ITEM %llu) flags %#llx size %u",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001656 logical, logical - found_key->objectid, found_key->objectid,
1657 found_key->offset, flags, item_size);
Liu Bo69917e42012-09-07 20:01:28 -06001658
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 Schmidta542ad12011-06-13 19:52:59 +02001669
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 Mahoneye0c476b2017-06-28 21:56:57 -06001677 * get_extent_inline_ref must pass the modified ptr parameter to get the
Jan Schmidta542ad12011-06-13 19:52:59 +02001678 * next ref. after the last ref was processed, 1 is returned.
1679 * returns <0 on error
1680 */
Jeff Mahoneye0c476b2017-06-28 21:56:57 -06001681static 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 Schmidta542ad12011-06-13 19:52:59 +02001688{
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 Bo6eda71d2014-06-09 10:54:07 +08001697 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 Schmidta542ad12011-06-13 19:52:59 +02001707 } else {
1708 *out_eiref = (struct btrfs_extent_inline_ref *)(ei + 1);
1709 }
1710 *ptr = (unsigned long)*out_eiref;
Liu Bocd857dd2014-06-08 19:04:13 +08001711 if ((unsigned long)(*ptr) >= (unsigned long)ei + item_size)
Jan Schmidta542ad12011-06-13 19:52:59 +02001712 return -ENOENT;
1713 }
1714
1715 end = (unsigned long)ei + item_size;
Liu Bo6eda71d2014-06-09 10:54:07 +08001716 *out_eiref = (struct btrfs_extent_inline_ref *)(*ptr);
Jan Schmidta542ad12011-06-13 19:52:59 +02001717 *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 Mahoneye0c476b2017-06-28 21:56:57 -06001730 * call and may be modified (see get_extent_inline_ref comment).
Jan Schmidta542ad12011-06-13 19:52:59 +02001731 * returns 0 if data was provided, 1 if there was no more data to provide or
1732 * <0 on error.
1733 */
1734int tree_backref_for_extent(unsigned long *ptr, struct extent_buffer *eb,
Liu Bo6eda71d2014-06-09 10:54:07 +08001735 struct btrfs_key *key, struct btrfs_extent_item *ei,
1736 u32 item_size, u64 *out_root, u8 *out_level)
Jan Schmidta542ad12011-06-13 19:52:59 +02001737{
1738 int ret;
1739 int type;
Jan Schmidta542ad12011-06-13 19:52:59 +02001740 struct btrfs_extent_inline_ref *eiref;
1741
1742 if (*ptr == (unsigned long)-1)
1743 return 1;
1744
1745 while (1) {
Jeff Mahoneye0c476b2017-06-28 21:56:57 -06001746 ret = get_extent_inline_ref(ptr, eb, key, ei, item_size,
Liu Bo6eda71d2014-06-09 10:54:07 +08001747 &eiref, &type);
Jan Schmidta542ad12011-06-13 19:52:59 +02001748 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 Schmidta542ad12011-06-13 19:52:59 +02001760 *out_root = btrfs_extent_inline_ref_offset(eb, eiref);
Filipe Mananaa1317f42014-12-15 16:04:42 +00001761
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 Schmidta542ad12011-06-13 19:52:59 +02001771
1772 if (ret == 1)
1773 *ptr = (unsigned long)-1;
1774
1775 return 0;
1776}
1777
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04001778static 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 Schmidta542ad12011-06-13 19:52:59 +02001782{
Jan Schmidt976b1902012-05-17 16:43:03 +02001783 struct extent_inode_elem *eie;
Jan Schmidt4692cf52011-12-02 14:56:41 +01001784 int ret = 0;
Jan Schmidta542ad12011-06-13 19:52:59 +02001785
Jan Schmidt976b1902012-05-17 16:43:03 +02001786 for (eie = inode_list; eie; eie = eie->next) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04001787 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 Schmidt976b1902012-05-17 16:43:03 +02001791 ret = iterate(eie->inum, eie->offset, root, ctx);
Jan Schmidt4692cf52011-12-02 14:56:41 +01001792 if (ret) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04001793 btrfs_debug(fs_info,
1794 "stopping iteration for %llu due to ret=%d",
1795 extent_item_objectid, ret);
Jan Schmidt4692cf52011-12-02 14:56:41 +01001796 break;
1797 }
Jan Schmidta542ad12011-06-13 19:52:59 +02001798 }
1799
Jan Schmidta542ad12011-06-13 19:52:59 +02001800 return ret;
1801}
1802
1803/*
1804 * calls iterate() for every inode that references the extent identified by
Jan Schmidt4692cf52011-12-02 14:56:41 +01001805 * the given parameters.
Jan Schmidta542ad12011-06-13 19:52:59 +02001806 * when the iterator function returns a non-zero value, iteration stops.
1807 */
1808int iterate_extent_inodes(struct btrfs_fs_info *fs_info,
Jan Schmidt4692cf52011-12-02 14:56:41 +01001809 u64 extent_item_objectid, u64 extent_item_pos,
Jan Schmidt7a3ae2f2012-03-23 17:32:28 +01001810 int search_commit_root,
Jan Schmidta542ad12011-06-13 19:52:59 +02001811 iterate_extent_inodes_t *iterate, void *ctx)
1812{
Jan Schmidta542ad12011-06-13 19:52:59 +02001813 int ret;
Josef Bacikda61d312013-06-12 16:20:08 -04001814 struct btrfs_trans_handle *trans = NULL;
Jan Schmidt7a3ae2f2012-03-23 17:32:28 +01001815 struct ulist *refs = NULL;
1816 struct ulist *roots = NULL;
Jan Schmidt4692cf52011-12-02 14:56:41 +01001817 struct ulist_node *ref_node = NULL;
1818 struct ulist_node *root_node = NULL;
David Sterba3284da72015-02-25 15:47:32 +01001819 struct seq_list tree_mod_seq_elem = SEQ_LIST_INIT(tree_mod_seq_elem);
Jan Schmidtcd1b4132012-05-22 14:56:50 +02001820 struct ulist_iterator ref_uiter;
1821 struct ulist_iterator root_uiter;
Jan Schmidta542ad12011-06-13 19:52:59 +02001822
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04001823 btrfs_debug(fs_info, "resolving all inodes for extent %llu",
Jan Schmidt4692cf52011-12-02 14:56:41 +01001824 extent_item_objectid);
1825
Josef Bacikda61d312013-06-12 16:20:08 -04001826 if (!search_commit_root) {
Jan Schmidt7a3ae2f2012-03-23 17:32:28 +01001827 trans = btrfs_join_transaction(fs_info->extent_root);
1828 if (IS_ERR(trans))
1829 return PTR_ERR(trans);
Jan Schmidt8445f612012-05-16 18:36:03 +02001830 btrfs_get_tree_mod_seq(fs_info, &tree_mod_seq_elem);
Josef Bacik9e351cc2014-03-13 15:42:13 -04001831 } else {
1832 down_read(&fs_info->commit_root_sem);
Jan Schmidt7a3ae2f2012-03-23 17:32:28 +01001833 }
Jan Schmidt4692cf52011-12-02 14:56:41 +01001834
1835 ret = btrfs_find_all_leafs(trans, fs_info, extent_item_objectid,
Jan Schmidt097b8a72012-06-21 11:08:04 +02001836 tree_mod_seq_elem.seq, &refs,
Jan Schmidt8445f612012-05-16 18:36:03 +02001837 &extent_item_pos);
Jan Schmidt4692cf52011-12-02 14:56:41 +01001838 if (ret)
1839 goto out;
1840
Jan Schmidtcd1b4132012-05-22 14:56:50 +02001841 ULIST_ITER_INIT(&ref_uiter);
1842 while (!ret && (ref_node = ulist_next(refs, &ref_uiter))) {
Jeff Mahoneye0c476b2017-06-28 21:56:57 -06001843 ret = btrfs_find_all_roots_safe(trans, fs_info, ref_node->val,
1844 tree_mod_seq_elem.seq, &roots);
Jan Schmidt4692cf52011-12-02 14:56:41 +01001845 if (ret)
Jan Schmidta542ad12011-06-13 19:52:59 +02001846 break;
Jan Schmidtcd1b4132012-05-22 14:56:50 +02001847 ULIST_ITER_INIT(&root_uiter);
1848 while (!ret && (root_node = ulist_next(roots, &root_uiter))) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04001849 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 Schmidt995e01b2012-08-13 02:52:38 -06001855 (uintptr_t)ref_node->aux,
1856 root_node->val,
1857 extent_item_objectid,
1858 iterate, ctx);
Jan Schmidta542ad12011-06-13 19:52:59 +02001859 }
Jan Schmidt976b1902012-05-17 16:43:03 +02001860 ulist_free(roots);
Jan Schmidta542ad12011-06-13 19:52:59 +02001861 }
1862
Jan Schmidt976b1902012-05-17 16:43:03 +02001863 free_leaf_list(refs);
Jan Schmidt4692cf52011-12-02 14:56:41 +01001864out:
Jan Schmidt7a3ae2f2012-03-23 17:32:28 +01001865 if (!search_commit_root) {
Jan Schmidt8445f612012-05-16 18:36:03 +02001866 btrfs_put_tree_mod_seq(fs_info, &tree_mod_seq_elem);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04001867 btrfs_end_transaction(trans);
Josef Bacik9e351cc2014-03-13 15:42:13 -04001868 } else {
1869 up_read(&fs_info->commit_root_sem);
Jan Schmidt7a3ae2f2012-03-23 17:32:28 +01001870 }
1871
Jan Schmidta542ad12011-06-13 19:52:59 +02001872 return ret;
1873}
1874
1875int 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 Schmidt4692cf52011-12-02 14:56:41 +01001880 u64 extent_item_pos;
Liu Bo69917e42012-09-07 20:01:28 -06001881 u64 flags = 0;
Jan Schmidta542ad12011-06-13 19:52:59 +02001882 struct btrfs_key found_key;
Jan Schmidt7a3ae2f2012-03-23 17:32:28 +01001883 int search_commit_root = path->search_commit_root;
Jan Schmidta542ad12011-06-13 19:52:59 +02001884
Liu Bo69917e42012-09-07 20:01:28 -06001885 ret = extent_from_logical(fs_info, logical, path, &found_key, &flags);
Jan Schmidt4692cf52011-12-02 14:56:41 +01001886 btrfs_release_path(path);
Jan Schmidta542ad12011-06-13 19:52:59 +02001887 if (ret < 0)
1888 return ret;
Liu Bo69917e42012-09-07 20:01:28 -06001889 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)
Stefan Behrens3627bf42012-08-01 04:28:01 -06001890 return -EINVAL;
Jan Schmidta542ad12011-06-13 19:52:59 +02001891
Jan Schmidt4692cf52011-12-02 14:56:41 +01001892 extent_item_pos = logical - found_key.objectid;
Jan Schmidt7a3ae2f2012-03-23 17:32:28 +01001893 ret = iterate_extent_inodes(fs_info, found_key.objectid,
1894 extent_item_pos, search_commit_root,
1895 iterate, ctx);
Jan Schmidta542ad12011-06-13 19:52:59 +02001896
1897 return ret;
1898}
1899
Mark Fashehd24bec32012-08-08 11:33:54 -07001900typedef int (iterate_irefs_t)(u64 parent, u32 name_len, unsigned long name_off,
1901 struct extent_buffer *eb, void *ctx);
1902
1903static int iterate_inode_refs(u64 inum, struct btrfs_root *fs_root,
1904 struct btrfs_path *path,
1905 iterate_irefs_t *iterate, void *ctx)
Jan Schmidta542ad12011-06-13 19:52:59 +02001906{
Jan Schmidtaefc1eb2012-04-13 12:28:00 +02001907 int ret = 0;
Jan Schmidta542ad12011-06-13 19:52:59 +02001908 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 Schmidtaefc1eb2012-04-13 12:28:00 +02001919 while (!ret) {
David Sterbac234a242015-01-02 19:03:17 +01001920 ret = btrfs_find_item(fs_root, path, inum,
1921 parent ? parent + 1 : 0, BTRFS_INODE_REF_KEY,
1922 &found_key);
1923
Jan Schmidta542ad12011-06-13 19:52:59 +02001924 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 Manana3fe81ce2013-12-15 12:43:58 +00001934 eb = btrfs_clone_extent_buffer(path->nodes[0]);
1935 if (!eb) {
1936 ret = -ENOMEM;
1937 break;
1938 }
1939 extent_buffer_get(eb);
Jan Schmidtb916a592012-04-13 12:28:08 +02001940 btrfs_tree_read_lock(eb);
1941 btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK);
Jan Schmidta542ad12011-06-13 19:52:59 +02001942 btrfs_release_path(path);
1943
Ross Kirkdd3cc162013-09-16 15:58:09 +01001944 item = btrfs_item_nr(slot);
Jan Schmidta542ad12011-06-13 19:52:59 +02001945 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 Mahoneyab8d0fc2016-09-20 10:05:02 -04001950 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 Fashehd24bec32012-08-08 11:33:54 -07001953 ret = iterate(parent, name_len,
1954 (unsigned long)(iref + 1), eb, ctx);
Jan Schmidtaefc1eb2012-04-13 12:28:00 +02001955 if (ret)
Jan Schmidta542ad12011-06-13 19:52:59 +02001956 break;
Jan Schmidta542ad12011-06-13 19:52:59 +02001957 len = sizeof(*iref) + name_len;
1958 iref = (struct btrfs_inode_ref *)((char *)iref + len);
1959 }
Jan Schmidtb916a592012-04-13 12:28:08 +02001960 btrfs_tree_read_unlock_blocking(eb);
Jan Schmidta542ad12011-06-13 19:52:59 +02001961 free_extent_buffer(eb);
1962 }
1963
1964 btrfs_release_path(path);
1965
1966 return ret;
1967}
1968
Mark Fashehd24bec32012-08-08 11:33:54 -07001969static 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 Fashehd24bec32012-08-08 11:33:54 -07001980 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 Manana3fe81ce2013-12-15 12:43:58 +00001996 eb = btrfs_clone_extent_buffer(path->nodes[0]);
1997 if (!eb) {
1998 ret = -ENOMEM;
1999 break;
2000 }
2001 extent_buffer_get(eb);
Mark Fashehd24bec32012-08-08 11:33:54 -07002002
2003 btrfs_tree_read_lock(eb);
2004 btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK);
2005 btrfs_release_path(path);
2006
Chris Mason2849a852015-10-13 14:06:48 -04002007 item_size = btrfs_item_size_nr(eb, slot);
2008 ptr = btrfs_item_ptr_offset(eb, slot);
Mark Fashehd24bec32012-08-08 11:33:54 -07002009 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 Mason2849a852015-10-13 14:06:48 -04002022 cur_offset += btrfs_inode_extref_name_len(eb, extref);
Mark Fashehd24bec32012-08-08 11:33:54 -07002023 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
2036static 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 Schmidta542ad12011-06-13 19:52:59 +02002056/*
2057 * returns 0 if the path could be dumped (probably truncated)
2058 * returns <0 in case of an error
2059 */
Mark Fashehd24bec32012-08-08 11:33:54 -07002060static int inode_to_path(u64 inum, u32 name_len, unsigned long name_off,
2061 struct extent_buffer *eb, void *ctx)
Jan Schmidta542ad12011-06-13 19:52:59 +02002062{
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 Mason740c3d22011-11-02 15:48:34 -04002073 fspath_min = (char *)ipath->fspath->val + (i + 1) * s_ptr;
Jan Schmidt96b5bd72012-10-15 08:30:45 +00002074 fspath = btrfs_ref_to_path(ipath->fs_root, ipath->btrfs_path, name_len,
2075 name_off, eb, inum, fspath_min, bytes_left);
Jan Schmidta542ad12011-06-13 19:52:59 +02002076 if (IS_ERR(fspath))
2077 return PTR_ERR(fspath);
2078
2079 if (fspath > fspath_min) {
Jeff Mahoney745c4d82011-11-20 07:31:57 -05002080 ipath->fspath->val[i] = (u64)(unsigned long)fspath;
Jan Schmidta542ad12011-06-13 19:52:59 +02002081 ++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 Mason740c3d22011-11-02 15:48:34 -04002095 * from ipath->fspath->val[i].
Jan Schmidta542ad12011-06-13 19:52:59 +02002096 * when it returns, there are ipath->fspath->elem_cnt number of paths available
Chris Mason740c3d22011-11-02 15:48:34 -04002097 * in ipath->fspath->val[]. when the allocated space wasn't sufficient, the
Nicholas D Steeves01327612016-05-19 21:18:45 -04002098 * number of missed paths is recorded in ipath->fspath->elem_missed, otherwise,
Jan Schmidta542ad12011-06-13 19:52:59 +02002099 * it's zero. ipath->fspath->bytes_missing holds the number of bytes that would
2100 * have been needed to return all paths.
2101 */
2102int paths_from_inode(u64 inum, struct inode_fs_paths *ipath)
2103{
2104 return iterate_irefs(inum, ipath->fs_root, ipath->btrfs_path,
Mark Fashehd24bec32012-08-08 11:33:54 -07002105 inode_to_path, ipath);
Jan Schmidta542ad12011-06-13 19:52:59 +02002106}
2107
Jan Schmidta542ad12011-06-13 19:52:59 +02002108struct 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 Sterbaf54de062017-05-31 19:32:09 +02002114 data = kvmalloc(alloc_bytes, GFP_KERNEL);
Jan Schmidta542ad12011-06-13 19:52:59 +02002115 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 */
2138struct 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 Sterbaf54de062017-05-31 19:32:09 +02002148 ifp = kmalloc(sizeof(*ifp), GFP_KERNEL);
Jan Schmidta542ad12011-06-13 19:52:59 +02002149 if (!ifp) {
David Sterbaf54de062017-05-31 19:32:09 +02002150 kvfree(fspath);
Jan Schmidta542ad12011-06-13 19:52:59 +02002151 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
2161void free_ipath(struct inode_fs_paths *ipath)
2162{
Jesper Juhl4735fb22012-04-12 22:47:52 +02002163 if (!ipath)
2164 return;
David Sterbaf54de062017-05-31 19:32:09 +02002165 kvfree(ipath->fspath);
Jan Schmidta542ad12011-06-13 19:52:59 +02002166 kfree(ipath);
2167}