blob: 7d0dc100a09a816f3c1aec7089ebc7de98339114 [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,
Zygo Blaxellc995ab32017-09-22 13:58:45 -040043 struct extent_inode_elem **eie,
44 bool ignore_offset)
Jan Schmidt976b1902012-05-17 16:43:03 +020045{
Josef Bacik8ca15e02013-07-05 13:58:19 -040046 u64 offset = 0;
Jan Schmidt976b1902012-05-17 16:43:03 +020047 struct extent_inode_elem *e;
48
Zygo Blaxellc995ab32017-09-22 13:58:45 -040049 if (!ignore_offset &&
50 !btrfs_file_extent_compression(eb, fi) &&
Josef Bacik8ca15e02013-07-05 13:58:19 -040051 !btrfs_file_extent_encryption(eb, fi) &&
52 !btrfs_file_extent_other_encoding(eb, fi)) {
53 u64 data_offset;
54 u64 data_len;
Jan Schmidt976b1902012-05-17 16:43:03 +020055
Josef Bacik8ca15e02013-07-05 13:58:19 -040056 data_offset = btrfs_file_extent_offset(eb, fi);
57 data_len = btrfs_file_extent_num_bytes(eb, fi);
58
59 if (extent_item_pos < data_offset ||
60 extent_item_pos >= data_offset + data_len)
61 return 1;
62 offset = extent_item_pos - data_offset;
63 }
Jan Schmidt976b1902012-05-17 16:43:03 +020064
65 e = kmalloc(sizeof(*e), GFP_NOFS);
66 if (!e)
67 return -ENOMEM;
68
69 e->next = *eie;
70 e->inum = key->objectid;
Josef Bacik8ca15e02013-07-05 13:58:19 -040071 e->offset = key->offset + offset;
Jan Schmidt976b1902012-05-17 16:43:03 +020072 *eie = e;
73
74 return 0;
75}
76
Wang Shilongf05c4742014-01-28 19:13:38 +080077static void free_inode_elem_list(struct extent_inode_elem *eie)
78{
79 struct extent_inode_elem *eie_next;
80
81 for (; eie; eie = eie_next) {
82 eie_next = eie->next;
83 kfree(eie);
84 }
85}
86
Jeff Mahoney73980be2017-06-28 21:56:55 -060087static int find_extent_in_eb(const struct extent_buffer *eb,
88 u64 wanted_disk_byte, u64 extent_item_pos,
Zygo Blaxellc995ab32017-09-22 13:58:45 -040089 struct extent_inode_elem **eie,
90 bool ignore_offset)
Jan Schmidt976b1902012-05-17 16:43:03 +020091{
92 u64 disk_byte;
93 struct btrfs_key key;
94 struct btrfs_file_extent_item *fi;
95 int slot;
96 int nritems;
97 int extent_type;
98 int ret;
99
100 /*
101 * from the shared data ref, we only have the leaf but we need
102 * the key. thus, we must look into all items and see that we
103 * find one (some) with a reference to our extent item.
104 */
105 nritems = btrfs_header_nritems(eb);
106 for (slot = 0; slot < nritems; ++slot) {
107 btrfs_item_key_to_cpu(eb, &key, slot);
108 if (key.type != BTRFS_EXTENT_DATA_KEY)
109 continue;
110 fi = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
111 extent_type = btrfs_file_extent_type(eb, fi);
112 if (extent_type == BTRFS_FILE_EXTENT_INLINE)
113 continue;
114 /* don't skip BTRFS_FILE_EXTENT_PREALLOC, we can handle that */
115 disk_byte = btrfs_file_extent_disk_bytenr(eb, fi);
116 if (disk_byte != wanted_disk_byte)
117 continue;
118
Zygo Blaxellc995ab32017-09-22 13:58:45 -0400119 ret = check_extent_in_eb(&key, eb, fi, extent_item_pos, eie, ignore_offset);
Jan Schmidt976b1902012-05-17 16:43:03 +0200120 if (ret < 0)
121 return ret;
122 }
123
124 return 0;
125}
126
Edmund Nadolski86d5f992017-07-12 16:20:06 -0600127struct preftree {
128 struct rb_root root;
Jeff Mahoney6c336b22017-07-12 16:20:07 -0600129 unsigned int count;
Edmund Nadolski86d5f992017-07-12 16:20:06 -0600130};
131
Jeff Mahoney6c336b22017-07-12 16:20:07 -0600132#define PREFTREE_INIT { .root = RB_ROOT, .count = 0 }
Edmund Nadolski86d5f992017-07-12 16:20:06 -0600133
134struct preftrees {
135 struct preftree direct; /* BTRFS_SHARED_[DATA|BLOCK]_REF_KEY */
136 struct preftree indirect; /* BTRFS_[TREE_BLOCK|EXTENT_DATA]_REF_KEY */
137 struct preftree indirect_missing_keys;
138};
139
Edmund Nadolski3ec4d322017-07-12 16:20:10 -0600140/*
141 * Checks for a shared extent during backref search.
142 *
143 * The share_count tracks prelim_refs (direct and indirect) having a
144 * ref->count >0:
145 * - incremented when a ref->count transitions to >0
146 * - decremented when a ref->count transitions to <1
147 */
148struct share_check {
149 u64 root_objectid;
150 u64 inum;
151 int share_count;
152};
153
154static inline int extent_is_shared(struct share_check *sc)
155{
156 return (sc && sc->share_count > 1) ? BACKREF_FOUND_SHARED : 0;
157}
158
Wang Shilongb9e9a6c2013-08-09 13:25:36 +0800159static struct kmem_cache *btrfs_prelim_ref_cache;
160
161int __init btrfs_prelim_ref_init(void)
162{
163 btrfs_prelim_ref_cache = kmem_cache_create("btrfs_prelim_ref",
Jeff Mahoneye0c476b2017-06-28 21:56:57 -0600164 sizeof(struct prelim_ref),
Wang Shilongb9e9a6c2013-08-09 13:25:36 +0800165 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +0300166 SLAB_MEM_SPREAD,
Wang Shilongb9e9a6c2013-08-09 13:25:36 +0800167 NULL);
168 if (!btrfs_prelim_ref_cache)
169 return -ENOMEM;
170 return 0;
171}
172
173void btrfs_prelim_ref_exit(void)
174{
Kinglong Mee5598e902016-01-29 21:36:35 +0800175 kmem_cache_destroy(btrfs_prelim_ref_cache);
Wang Shilongb9e9a6c2013-08-09 13:25:36 +0800176}
177
Edmund Nadolski86d5f992017-07-12 16:20:06 -0600178static void free_pref(struct prelim_ref *ref)
179{
180 kmem_cache_free(btrfs_prelim_ref_cache, ref);
181}
182
183/*
184 * Return 0 when both refs are for the same block (and can be merged).
185 * A -1 return indicates ref1 is a 'lower' block than ref2, while 1
186 * indicates a 'higher' block.
187 */
188static int prelim_ref_compare(struct prelim_ref *ref1,
189 struct prelim_ref *ref2)
190{
191 if (ref1->level < ref2->level)
192 return -1;
193 if (ref1->level > ref2->level)
194 return 1;
195 if (ref1->root_id < ref2->root_id)
196 return -1;
197 if (ref1->root_id > ref2->root_id)
198 return 1;
199 if (ref1->key_for_search.type < ref2->key_for_search.type)
200 return -1;
201 if (ref1->key_for_search.type > ref2->key_for_search.type)
202 return 1;
203 if (ref1->key_for_search.objectid < ref2->key_for_search.objectid)
204 return -1;
205 if (ref1->key_for_search.objectid > ref2->key_for_search.objectid)
206 return 1;
207 if (ref1->key_for_search.offset < ref2->key_for_search.offset)
208 return -1;
209 if (ref1->key_for_search.offset > ref2->key_for_search.offset)
210 return 1;
211 if (ref1->parent < ref2->parent)
212 return -1;
213 if (ref1->parent > ref2->parent)
214 return 1;
215
216 return 0;
217}
218
Edmund Nadolski3ec4d322017-07-12 16:20:10 -0600219void update_share_count(struct share_check *sc, int oldcount, int newcount)
220{
221 if ((!sc) || (oldcount == 0 && newcount < 1))
222 return;
223
224 if (oldcount > 0 && newcount < 1)
225 sc->share_count--;
226 else if (oldcount < 1 && newcount > 0)
227 sc->share_count++;
228}
229
Edmund Nadolski86d5f992017-07-12 16:20:06 -0600230/*
231 * Add @newref to the @root rbtree, merging identical refs.
232 *
Edmund Nadolski3ec4d322017-07-12 16:20:10 -0600233 * Callers should assume that newref has been freed after calling.
Edmund Nadolski86d5f992017-07-12 16:20:06 -0600234 */
Jeff Mahoney00142752017-07-12 16:20:08 -0600235static void prelim_ref_insert(const struct btrfs_fs_info *fs_info,
236 struct preftree *preftree,
Edmund Nadolski3ec4d322017-07-12 16:20:10 -0600237 struct prelim_ref *newref,
238 struct share_check *sc)
Edmund Nadolski86d5f992017-07-12 16:20:06 -0600239{
240 struct rb_root *root;
241 struct rb_node **p;
242 struct rb_node *parent = NULL;
243 struct prelim_ref *ref;
244 int result;
245
246 root = &preftree->root;
247 p = &root->rb_node;
248
249 while (*p) {
250 parent = *p;
251 ref = rb_entry(parent, struct prelim_ref, rbnode);
252 result = prelim_ref_compare(ref, newref);
253 if (result < 0) {
254 p = &(*p)->rb_left;
255 } else if (result > 0) {
256 p = &(*p)->rb_right;
257 } else {
258 /* Identical refs, merge them and free @newref */
259 struct extent_inode_elem *eie = ref->inode_list;
260
261 while (eie && eie->next)
262 eie = eie->next;
263
264 if (!eie)
265 ref->inode_list = newref->inode_list;
266 else
267 eie->next = newref->inode_list;
Jeff Mahoney00142752017-07-12 16:20:08 -0600268 trace_btrfs_prelim_ref_merge(fs_info, ref, newref,
269 preftree->count);
Edmund Nadolski3ec4d322017-07-12 16:20:10 -0600270 /*
271 * A delayed ref can have newref->count < 0.
272 * The ref->count is updated to follow any
273 * BTRFS_[ADD|DROP]_DELAYED_REF actions.
274 */
275 update_share_count(sc, ref->count,
276 ref->count + newref->count);
Edmund Nadolski86d5f992017-07-12 16:20:06 -0600277 ref->count += newref->count;
278 free_pref(newref);
279 return;
280 }
281 }
282
Edmund Nadolski3ec4d322017-07-12 16:20:10 -0600283 update_share_count(sc, 0, newref->count);
Jeff Mahoney6c336b22017-07-12 16:20:07 -0600284 preftree->count++;
Jeff Mahoney00142752017-07-12 16:20:08 -0600285 trace_btrfs_prelim_ref_insert(fs_info, newref, NULL, preftree->count);
Edmund Nadolski86d5f992017-07-12 16:20:06 -0600286 rb_link_node(&newref->rbnode, parent, p);
287 rb_insert_color(&newref->rbnode, root);
288}
289
290/*
291 * Release the entire tree. We don't care about internal consistency so
292 * just free everything and then reset the tree root.
293 */
294static void prelim_release(struct preftree *preftree)
295{
296 struct prelim_ref *ref, *next_ref;
297
298 rbtree_postorder_for_each_entry_safe(ref, next_ref, &preftree->root,
299 rbnode)
300 free_pref(ref);
301
302 preftree->root = RB_ROOT;
Jeff Mahoney6c336b22017-07-12 16:20:07 -0600303 preftree->count = 0;
Edmund Nadolski86d5f992017-07-12 16:20:06 -0600304}
305
Jan Schmidtd5c88b72012-05-15 17:55:51 +0200306/*
307 * the rules for all callers of this function are:
308 * - obtaining the parent is the goal
309 * - if you add a key, you must know that it is a correct key
310 * - if you cannot add the parent or a correct key, then we will look into the
311 * block later to set a correct key
312 *
313 * delayed refs
314 * ============
315 * backref type | shared | indirect | shared | indirect
316 * information | tree | tree | data | data
317 * --------------------+--------+----------+--------+----------
318 * parent logical | y | - | - | -
319 * key to resolve | - | y | y | y
320 * tree block logical | - | - | - | -
321 * root for resolving | y | y | y | y
322 *
323 * - column 1: we've the parent -> done
324 * - column 2, 3, 4: we use the key to find the parent
325 *
326 * on disk refs (inline or keyed)
327 * ==============================
328 * backref type | shared | indirect | shared | indirect
329 * information | tree | tree | data | data
330 * --------------------+--------+----------+--------+----------
331 * parent logical | y | - | y | -
332 * key to resolve | - | - | - | y
333 * tree block logical | y | y | y | y
334 * root for resolving | - | y | y | y
335 *
336 * - column 1, 3: we've the parent -> done
337 * - column 2: we take the first key from the block to find the parent
Jeff Mahoneye0c476b2017-06-28 21:56:57 -0600338 * (see add_missing_keys)
Jan Schmidtd5c88b72012-05-15 17:55:51 +0200339 * - column 4: we use the key to find the parent
340 *
341 * additional information that's available but not required to find the parent
342 * block might help in merging entries to gain some speed.
343 */
Jeff Mahoney00142752017-07-12 16:20:08 -0600344static int add_prelim_ref(const struct btrfs_fs_info *fs_info,
345 struct preftree *preftree, u64 root_id,
Jeff Mahoneye0c476b2017-06-28 21:56:57 -0600346 const struct btrfs_key *key, int level, u64 parent,
Edmund Nadolski3ec4d322017-07-12 16:20:10 -0600347 u64 wanted_disk_byte, int count,
348 struct share_check *sc, gfp_t gfp_mask)
Jan Schmidt8da6d582011-11-23 18:55:04 +0100349{
Jeff Mahoneye0c476b2017-06-28 21:56:57 -0600350 struct prelim_ref *ref;
Jan Schmidt8da6d582011-11-23 18:55:04 +0100351
Liu Bo48ec4732013-10-30 13:25:24 +0800352 if (root_id == BTRFS_DATA_RELOC_TREE_OBJECTID)
353 return 0;
354
Wang Shilongb9e9a6c2013-08-09 13:25:36 +0800355 ref = kmem_cache_alloc(btrfs_prelim_ref_cache, gfp_mask);
Jan Schmidt8da6d582011-11-23 18:55:04 +0100356 if (!ref)
357 return -ENOMEM;
358
359 ref->root_id = root_id;
Filipe Mananad6589102015-07-29 17:21:17 +0100360 if (key) {
Jan Schmidtd5c88b72012-05-15 17:55:51 +0200361 ref->key_for_search = *key;
Filipe Mananad6589102015-07-29 17:21:17 +0100362 /*
363 * We can often find data backrefs with an offset that is too
364 * large (>= LLONG_MAX, maximum allowed file offset) due to
365 * underflows when subtracting a file's offset with the data
366 * offset of its corresponding extent data item. This can
367 * happen for example in the clone ioctl.
368 * So if we detect such case we set the search key's offset to
369 * zero to make sure we will find the matching file extent item
370 * at add_all_parents(), otherwise we will miss it because the
371 * offset taken form the backref is much larger then the offset
372 * of the file extent item. This can make us scan a very large
373 * number of file extent items, but at least it will not make
374 * us miss any.
375 * This is an ugly workaround for a behaviour that should have
376 * never existed, but it does and a fix for the clone ioctl
377 * would touch a lot of places, cause backwards incompatibility
378 * and would not fix the problem for extents cloned with older
379 * kernels.
380 */
381 if (ref->key_for_search.type == BTRFS_EXTENT_DATA_KEY &&
382 ref->key_for_search.offset >= LLONG_MAX)
383 ref->key_for_search.offset = 0;
384 } else {
Jan Schmidtd5c88b72012-05-15 17:55:51 +0200385 memset(&ref->key_for_search, 0, sizeof(ref->key_for_search));
Filipe Mananad6589102015-07-29 17:21:17 +0100386 }
Jan Schmidt8da6d582011-11-23 18:55:04 +0100387
Jan Schmidt33019582012-05-30 18:05:21 +0200388 ref->inode_list = NULL;
Jan Schmidt8da6d582011-11-23 18:55:04 +0100389 ref->level = level;
390 ref->count = count;
391 ref->parent = parent;
392 ref->wanted_disk_byte = wanted_disk_byte;
Edmund Nadolski3ec4d322017-07-12 16:20:10 -0600393 prelim_ref_insert(fs_info, preftree, ref, sc);
394 return extent_is_shared(sc);
Jan Schmidt8da6d582011-11-23 18:55:04 +0100395}
396
Edmund Nadolski86d5f992017-07-12 16:20:06 -0600397/* direct refs use root == 0, key == NULL */
Jeff Mahoney00142752017-07-12 16:20:08 -0600398static int add_direct_ref(const struct btrfs_fs_info *fs_info,
399 struct preftrees *preftrees, int level, u64 parent,
Edmund Nadolski3ec4d322017-07-12 16:20:10 -0600400 u64 wanted_disk_byte, int count,
401 struct share_check *sc, gfp_t gfp_mask)
Edmund Nadolski86d5f992017-07-12 16:20:06 -0600402{
Jeff Mahoney00142752017-07-12 16:20:08 -0600403 return add_prelim_ref(fs_info, &preftrees->direct, 0, NULL, level,
Edmund Nadolski3ec4d322017-07-12 16:20:10 -0600404 parent, wanted_disk_byte, count, sc, gfp_mask);
Edmund Nadolski86d5f992017-07-12 16:20:06 -0600405}
406
407/* indirect refs use parent == 0 */
Jeff Mahoney00142752017-07-12 16:20:08 -0600408static int add_indirect_ref(const struct btrfs_fs_info *fs_info,
409 struct preftrees *preftrees, u64 root_id,
Edmund Nadolski86d5f992017-07-12 16:20:06 -0600410 const struct btrfs_key *key, int level,
Edmund Nadolski3ec4d322017-07-12 16:20:10 -0600411 u64 wanted_disk_byte, int count,
412 struct share_check *sc, gfp_t gfp_mask)
Edmund Nadolski86d5f992017-07-12 16:20:06 -0600413{
414 struct preftree *tree = &preftrees->indirect;
415
416 if (!key)
417 tree = &preftrees->indirect_missing_keys;
Jeff Mahoney00142752017-07-12 16:20:08 -0600418 return add_prelim_ref(fs_info, tree, root_id, key, level, 0,
Edmund Nadolski3ec4d322017-07-12 16:20:10 -0600419 wanted_disk_byte, count, sc, gfp_mask);
Edmund Nadolski86d5f992017-07-12 16:20:06 -0600420}
421
Jan Schmidt8da6d582011-11-23 18:55:04 +0100422static int add_all_parents(struct btrfs_root *root, struct btrfs_path *path,
Jeff Mahoneye0c476b2017-06-28 21:56:57 -0600423 struct ulist *parents, struct prelim_ref *ref,
Josef Bacik44853862014-03-19 13:35:14 -0400424 int level, u64 time_seq, const u64 *extent_item_pos,
Zygo Blaxellc995ab32017-09-22 13:58:45 -0400425 u64 total_refs, bool ignore_offset)
Jan Schmidt8da6d582011-11-23 18:55:04 +0100426{
Alexander Block69bca402012-06-19 07:42:26 -0600427 int ret = 0;
428 int slot;
429 struct extent_buffer *eb;
430 struct btrfs_key key;
Josef Bacik7ef81ac2014-01-24 14:05:42 -0500431 struct btrfs_key *key_for_search = &ref->key_for_search;
Jan Schmidt8da6d582011-11-23 18:55:04 +0100432 struct btrfs_file_extent_item *fi;
Josef Baciked8c4912013-07-05 14:03:47 -0400433 struct extent_inode_elem *eie = NULL, *old = NULL;
Jan Schmidt8da6d582011-11-23 18:55:04 +0100434 u64 disk_byte;
Josef Bacik7ef81ac2014-01-24 14:05:42 -0500435 u64 wanted_disk_byte = ref->wanted_disk_byte;
436 u64 count = 0;
Jan Schmidt8da6d582011-11-23 18:55:04 +0100437
Alexander Block69bca402012-06-19 07:42:26 -0600438 if (level != 0) {
439 eb = path->nodes[level];
440 ret = ulist_add(parents, eb->start, 0, GFP_NOFS);
Jan Schmidt33019582012-05-30 18:05:21 +0200441 if (ret < 0)
442 return ret;
Jan Schmidt8da6d582011-11-23 18:55:04 +0100443 return 0;
Alexander Block69bca402012-06-19 07:42:26 -0600444 }
Jan Schmidt8da6d582011-11-23 18:55:04 +0100445
446 /*
Alexander Block69bca402012-06-19 07:42:26 -0600447 * We normally enter this function with the path already pointing to
448 * the first item to check. But sometimes, we may enter it with
449 * slot==nritems. In that case, go to the next leaf before we continue.
Jan Schmidt8da6d582011-11-23 18:55:04 +0100450 */
Qu Wenruo21633fc2015-04-16 14:54:50 +0800451 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
Edmund Nadolskide47c9d2017-03-16 10:04:34 -0600452 if (time_seq == SEQ_LAST)
Qu Wenruo21633fc2015-04-16 14:54:50 +0800453 ret = btrfs_next_leaf(root, path);
454 else
455 ret = btrfs_next_old_leaf(root, path, time_seq);
456 }
Jan Schmidt8da6d582011-11-23 18:55:04 +0100457
Josef Bacik44853862014-03-19 13:35:14 -0400458 while (!ret && count < total_refs) {
Jan Schmidt8da6d582011-11-23 18:55:04 +0100459 eb = path->nodes[0];
Alexander Block69bca402012-06-19 07:42:26 -0600460 slot = path->slots[0];
461
462 btrfs_item_key_to_cpu(eb, &key, slot);
463
464 if (key.objectid != key_for_search->objectid ||
465 key.type != BTRFS_EXTENT_DATA_KEY)
466 break;
467
468 fi = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
469 disk_byte = btrfs_file_extent_disk_bytenr(eb, fi);
470
471 if (disk_byte == wanted_disk_byte) {
472 eie = NULL;
Josef Baciked8c4912013-07-05 14:03:47 -0400473 old = NULL;
Josef Bacik7ef81ac2014-01-24 14:05:42 -0500474 count++;
Alexander Block69bca402012-06-19 07:42:26 -0600475 if (extent_item_pos) {
476 ret = check_extent_in_eb(&key, eb, fi,
477 *extent_item_pos,
Zygo Blaxellc995ab32017-09-22 13:58:45 -0400478 &eie, ignore_offset);
Alexander Block69bca402012-06-19 07:42:26 -0600479 if (ret < 0)
480 break;
481 }
Josef Baciked8c4912013-07-05 14:03:47 -0400482 if (ret > 0)
483 goto next;
Takashi Iwai4eb1f662014-07-28 10:57:04 +0200484 ret = ulist_add_merge_ptr(parents, eb->start,
485 eie, (void **)&old, GFP_NOFS);
Josef Baciked8c4912013-07-05 14:03:47 -0400486 if (ret < 0)
487 break;
488 if (!ret && extent_item_pos) {
489 while (old->next)
490 old = old->next;
491 old->next = eie;
Alexander Block69bca402012-06-19 07:42:26 -0600492 }
Wang Shilongf05c4742014-01-28 19:13:38 +0800493 eie = NULL;
Jan Schmidt8da6d582011-11-23 18:55:04 +0100494 }
Josef Baciked8c4912013-07-05 14:03:47 -0400495next:
Edmund Nadolskide47c9d2017-03-16 10:04:34 -0600496 if (time_seq == SEQ_LAST)
Qu Wenruo21633fc2015-04-16 14:54:50 +0800497 ret = btrfs_next_item(root, path);
498 else
499 ret = btrfs_next_old_item(root, path, time_seq);
Jan Schmidt8da6d582011-11-23 18:55:04 +0100500 }
501
Alexander Block69bca402012-06-19 07:42:26 -0600502 if (ret > 0)
503 ret = 0;
Wang Shilongf05c4742014-01-28 19:13:38 +0800504 else if (ret < 0)
505 free_inode_elem_list(eie);
Alexander Block69bca402012-06-19 07:42:26 -0600506 return ret;
Jan Schmidt8da6d582011-11-23 18:55:04 +0100507}
508
509/*
510 * resolve an indirect backref in the form (root_id, key, level)
511 * to a logical address
512 */
Jeff Mahoneye0c476b2017-06-28 21:56:57 -0600513static int resolve_indirect_ref(struct btrfs_fs_info *fs_info,
514 struct btrfs_path *path, u64 time_seq,
515 struct prelim_ref *ref, struct ulist *parents,
Zygo Blaxellc995ab32017-09-22 13:58:45 -0400516 const u64 *extent_item_pos, u64 total_refs,
517 bool ignore_offset)
Jan Schmidt8da6d582011-11-23 18:55:04 +0100518{
Jan Schmidt8da6d582011-11-23 18:55:04 +0100519 struct btrfs_root *root;
520 struct btrfs_key root_key;
Jan Schmidt8da6d582011-11-23 18:55:04 +0100521 struct extent_buffer *eb;
522 int ret = 0;
523 int root_level;
524 int level = ref->level;
Wang Shilong538f72cd2014-01-23 13:47:48 +0800525 int index;
Jan Schmidt8da6d582011-11-23 18:55:04 +0100526
Jan Schmidt8da6d582011-11-23 18:55:04 +0100527 root_key.objectid = ref->root_id;
528 root_key.type = BTRFS_ROOT_ITEM_KEY;
529 root_key.offset = (u64)-1;
Wang Shilong538f72cd2014-01-23 13:47:48 +0800530
531 index = srcu_read_lock(&fs_info->subvol_srcu);
532
Josef Bacik2d9e9772015-11-05 14:37:58 -0800533 root = btrfs_get_fs_root(fs_info, &root_key, false);
Jan Schmidt8da6d582011-11-23 18:55:04 +0100534 if (IS_ERR(root)) {
Wang Shilong538f72cd2014-01-23 13:47:48 +0800535 srcu_read_unlock(&fs_info->subvol_srcu, index);
Jan Schmidt8da6d582011-11-23 18:55:04 +0100536 ret = PTR_ERR(root);
537 goto out;
538 }
539
Jeff Mahoneyf5ee5c92016-06-21 09:52:41 -0400540 if (btrfs_is_testing(fs_info)) {
Josef Bacikd9ee5222015-10-05 10:35:29 -0400541 srcu_read_unlock(&fs_info->subvol_srcu, index);
542 ret = -ENOENT;
543 goto out;
544 }
545
Josef Bacik9e351cc2014-03-13 15:42:13 -0400546 if (path->search_commit_root)
547 root_level = btrfs_header_level(root->commit_root);
Edmund Nadolskide47c9d2017-03-16 10:04:34 -0600548 else if (time_seq == SEQ_LAST)
Qu Wenruo21633fc2015-04-16 14:54:50 +0800549 root_level = btrfs_header_level(root->node);
Josef Bacik9e351cc2014-03-13 15:42:13 -0400550 else
551 root_level = btrfs_old_root_level(root, time_seq);
Jan Schmidt8da6d582011-11-23 18:55:04 +0100552
Wang Shilong538f72cd2014-01-23 13:47:48 +0800553 if (root_level + 1 == level) {
554 srcu_read_unlock(&fs_info->subvol_srcu, index);
Jan Schmidt8da6d582011-11-23 18:55:04 +0100555 goto out;
Wang Shilong538f72cd2014-01-23 13:47:48 +0800556 }
Jan Schmidt8da6d582011-11-23 18:55:04 +0100557
558 path->lowest_level = level;
Edmund Nadolskide47c9d2017-03-16 10:04:34 -0600559 if (time_seq == SEQ_LAST)
Qu Wenruo21633fc2015-04-16 14:54:50 +0800560 ret = btrfs_search_slot(NULL, root, &ref->key_for_search, path,
561 0, 0);
562 else
563 ret = btrfs_search_old_slot(root, &ref->key_for_search, path,
564 time_seq);
Wang Shilong538f72cd2014-01-23 13:47:48 +0800565
566 /* root node has been locked, we can release @subvol_srcu safely here */
567 srcu_read_unlock(&fs_info->subvol_srcu, index);
568
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -0400569 btrfs_debug(fs_info,
570 "search slot in root %llu (level %d, ref count %d) returned %d for key (%llu %u %llu)",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +0200571 ref->root_id, level, ref->count, ret,
572 ref->key_for_search.objectid, ref->key_for_search.type,
573 ref->key_for_search.offset);
Jan Schmidt8da6d582011-11-23 18:55:04 +0100574 if (ret < 0)
575 goto out;
576
577 eb = path->nodes[level];
Jan Schmidt93454572012-06-27 15:23:09 +0200578 while (!eb) {
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +0530579 if (WARN_ON(!level)) {
Jan Schmidt93454572012-06-27 15:23:09 +0200580 ret = 1;
581 goto out;
582 }
583 level--;
584 eb = path->nodes[level];
Jan Schmidt8da6d582011-11-23 18:55:04 +0100585 }
586
Josef Bacik7ef81ac2014-01-24 14:05:42 -0500587 ret = add_all_parents(root, path, parents, ref, level, time_seq,
Zygo Blaxellc995ab32017-09-22 13:58:45 -0400588 extent_item_pos, total_refs, ignore_offset);
Jan Schmidt8da6d582011-11-23 18:55:04 +0100589out:
Josef Bacikda61d312013-06-12 16:20:08 -0400590 path->lowest_level = 0;
591 btrfs_release_path(path);
Jan Schmidt8da6d582011-11-23 18:55:04 +0100592 return ret;
593}
594
Jeff Mahoney4dae0772017-06-28 21:56:56 -0600595static struct extent_inode_elem *
596unode_aux_to_inode_list(struct ulist_node *node)
597{
598 if (!node)
599 return NULL;
600 return (struct extent_inode_elem *)(uintptr_t)node->aux;
601}
602
Jan Schmidt8da6d582011-11-23 18:55:04 +0100603/*
Edmund Nadolski86d5f992017-07-12 16:20:06 -0600604 * We maintain three seperate rbtrees: one for direct refs, one for
605 * indirect refs which have a key, and one for indirect refs which do not
606 * have a key. Each tree does merge on insertion.
607 *
608 * Once all of the references are located, we iterate over the tree of
609 * indirect refs with missing keys. An appropriate key is located and
610 * the ref is moved onto the tree for indirect refs. After all missing
611 * keys are thus located, we iterate over the indirect ref tree, resolve
612 * each reference, and then insert the resolved reference onto the
613 * direct tree (merging there too).
614 *
615 * New backrefs (i.e., for parent nodes) are added to the appropriate
616 * rbtree as they are encountered. The new backrefs are subsequently
617 * resolved as above.
Jan Schmidt8da6d582011-11-23 18:55:04 +0100618 */
Jeff Mahoneye0c476b2017-06-28 21:56:57 -0600619static int resolve_indirect_refs(struct btrfs_fs_info *fs_info,
620 struct btrfs_path *path, u64 time_seq,
Edmund Nadolski86d5f992017-07-12 16:20:06 -0600621 struct preftrees *preftrees,
Jeff Mahoneye0c476b2017-06-28 21:56:57 -0600622 const u64 *extent_item_pos, u64 total_refs,
Zygo Blaxellc995ab32017-09-22 13:58:45 -0400623 struct share_check *sc, bool ignore_offset)
Jan Schmidt8da6d582011-11-23 18:55:04 +0100624{
625 int err;
626 int ret = 0;
Jan Schmidt8da6d582011-11-23 18:55:04 +0100627 struct ulist *parents;
628 struct ulist_node *node;
Jan Schmidtcd1b4132012-05-22 14:56:50 +0200629 struct ulist_iterator uiter;
Edmund Nadolski86d5f992017-07-12 16:20:06 -0600630 struct rb_node *rnode;
Jan Schmidt8da6d582011-11-23 18:55:04 +0100631
632 parents = ulist_alloc(GFP_NOFS);
633 if (!parents)
634 return -ENOMEM;
635
636 /*
Edmund Nadolski86d5f992017-07-12 16:20:06 -0600637 * We could trade memory usage for performance here by iterating
638 * the tree, allocating new refs for each insertion, and then
639 * freeing the entire indirect tree when we're done. In some test
640 * cases, the tree can grow quite large (~200k objects).
Jan Schmidt8da6d582011-11-23 18:55:04 +0100641 */
Edmund Nadolski86d5f992017-07-12 16:20:06 -0600642 while ((rnode = rb_first(&preftrees->indirect.root))) {
643 struct prelim_ref *ref;
644
645 ref = rb_entry(rnode, struct prelim_ref, rbnode);
646 if (WARN(ref->parent,
647 "BUG: direct ref found in indirect tree")) {
648 ret = -EINVAL;
649 goto out;
650 }
651
652 rb_erase(&ref->rbnode, &preftrees->indirect.root);
Jeff Mahoney6c336b22017-07-12 16:20:07 -0600653 preftrees->indirect.count--;
Edmund Nadolski86d5f992017-07-12 16:20:06 -0600654
655 if (ref->count == 0) {
656 free_pref(ref);
Jan Schmidt8da6d582011-11-23 18:55:04 +0100657 continue;
Edmund Nadolski86d5f992017-07-12 16:20:06 -0600658 }
659
Edmund Nadolski3ec4d322017-07-12 16:20:10 -0600660 if (sc && sc->root_objectid &&
661 ref->root_id != sc->root_objectid) {
Edmund Nadolski86d5f992017-07-12 16:20:06 -0600662 free_pref(ref);
Josef Bacikdc046b12014-09-10 16:20:45 -0400663 ret = BACKREF_FOUND_SHARED;
664 goto out;
665 }
Jeff Mahoneye0c476b2017-06-28 21:56:57 -0600666 err = resolve_indirect_ref(fs_info, path, time_seq, ref,
667 parents, extent_item_pos,
Zygo Blaxellc995ab32017-09-22 13:58:45 -0400668 total_refs, ignore_offset);
Wang Shilong95def2ed2014-01-23 13:47:49 +0800669 /*
670 * we can only tolerate ENOENT,otherwise,we should catch error
671 * and return directly.
672 */
673 if (err == -ENOENT) {
Edmund Nadolski3ec4d322017-07-12 16:20:10 -0600674 prelim_ref_insert(fs_info, &preftrees->direct, ref,
675 NULL);
Jan Schmidt8da6d582011-11-23 18:55:04 +0100676 continue;
Wang Shilong95def2ed2014-01-23 13:47:49 +0800677 } else if (err) {
Edmund Nadolski86d5f992017-07-12 16:20:06 -0600678 free_pref(ref);
Wang Shilong95def2ed2014-01-23 13:47:49 +0800679 ret = err;
680 goto out;
681 }
Jan Schmidt8da6d582011-11-23 18:55:04 +0100682
683 /* we put the first parent into the ref at hand */
Jan Schmidtcd1b4132012-05-22 14:56:50 +0200684 ULIST_ITER_INIT(&uiter);
685 node = ulist_next(parents, &uiter);
Jan Schmidt8da6d582011-11-23 18:55:04 +0100686 ref->parent = node ? node->val : 0;
Jeff Mahoney4dae0772017-06-28 21:56:56 -0600687 ref->inode_list = unode_aux_to_inode_list(node);
Jan Schmidt8da6d582011-11-23 18:55:04 +0100688
Edmund Nadolski86d5f992017-07-12 16:20:06 -0600689 /* Add a prelim_ref(s) for any other parent(s). */
Jan Schmidtcd1b4132012-05-22 14:56:50 +0200690 while ((node = ulist_next(parents, &uiter))) {
Edmund Nadolski86d5f992017-07-12 16:20:06 -0600691 struct prelim_ref *new_ref;
692
Wang Shilongb9e9a6c2013-08-09 13:25:36 +0800693 new_ref = kmem_cache_alloc(btrfs_prelim_ref_cache,
694 GFP_NOFS);
Jan Schmidt8da6d582011-11-23 18:55:04 +0100695 if (!new_ref) {
Edmund Nadolski86d5f992017-07-12 16:20:06 -0600696 free_pref(ref);
Jan Schmidt8da6d582011-11-23 18:55:04 +0100697 ret = -ENOMEM;
Wang Shilonge36902d2013-04-15 10:26:38 +0000698 goto out;
Jan Schmidt8da6d582011-11-23 18:55:04 +0100699 }
700 memcpy(new_ref, ref, sizeof(*ref));
701 new_ref->parent = node->val;
Jeff Mahoney4dae0772017-06-28 21:56:56 -0600702 new_ref->inode_list = unode_aux_to_inode_list(node);
Edmund Nadolski3ec4d322017-07-12 16:20:10 -0600703 prelim_ref_insert(fs_info, &preftrees->direct,
704 new_ref, NULL);
Jan Schmidt8da6d582011-11-23 18:55:04 +0100705 }
Edmund Nadolski86d5f992017-07-12 16:20:06 -0600706
Edmund Nadolski3ec4d322017-07-12 16:20:10 -0600707 /*
708 * Now it's a direct ref, put it in the the direct tree. We must
709 * do this last because the ref could be merged/freed here.
710 */
711 prelim_ref_insert(fs_info, &preftrees->direct, ref, NULL);
Edmund Nadolski86d5f992017-07-12 16:20:06 -0600712
Jan Schmidt8da6d582011-11-23 18:55:04 +0100713 ulist_reinit(parents);
Edmund Nadolski9dd14fd2017-07-12 16:20:09 -0600714 cond_resched();
Jan Schmidt8da6d582011-11-23 18:55:04 +0100715 }
Wang Shilonge36902d2013-04-15 10:26:38 +0000716out:
Jan Schmidt8da6d582011-11-23 18:55:04 +0100717 ulist_free(parents);
718 return ret;
719}
720
Jan Schmidtd5c88b72012-05-15 17:55:51 +0200721/*
722 * read tree blocks and add keys where required.
723 */
Jeff Mahoneye0c476b2017-06-28 21:56:57 -0600724static int add_missing_keys(struct btrfs_fs_info *fs_info,
Edmund Nadolski86d5f992017-07-12 16:20:06 -0600725 struct preftrees *preftrees)
Jan Schmidtd5c88b72012-05-15 17:55:51 +0200726{
Jeff Mahoneye0c476b2017-06-28 21:56:57 -0600727 struct prelim_ref *ref;
Jan Schmidtd5c88b72012-05-15 17:55:51 +0200728 struct extent_buffer *eb;
Edmund Nadolski86d5f992017-07-12 16:20:06 -0600729 struct preftree *tree = &preftrees->indirect_missing_keys;
730 struct rb_node *node;
Jan Schmidtd5c88b72012-05-15 17:55:51 +0200731
Edmund Nadolski86d5f992017-07-12 16:20:06 -0600732 while ((node = rb_first(&tree->root))) {
733 ref = rb_entry(node, struct prelim_ref, rbnode);
734 rb_erase(node, &tree->root);
735
736 BUG_ON(ref->parent); /* should not be a direct ref */
737 BUG_ON(ref->key_for_search.type);
Jan Schmidtd5c88b72012-05-15 17:55:51 +0200738 BUG_ON(!ref->wanted_disk_byte);
Edmund Nadolski86d5f992017-07-12 16:20:06 -0600739
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400740 eb = read_tree_block(fs_info, ref->wanted_disk_byte, 0);
Liu Bo64c043d2015-05-25 17:30:15 +0800741 if (IS_ERR(eb)) {
Edmund Nadolski86d5f992017-07-12 16:20:06 -0600742 free_pref(ref);
Liu Bo64c043d2015-05-25 17:30:15 +0800743 return PTR_ERR(eb);
744 } else if (!extent_buffer_uptodate(eb)) {
Edmund Nadolski86d5f992017-07-12 16:20:06 -0600745 free_pref(ref);
Josef Bacik416bc652013-04-23 14:17:42 -0400746 free_extent_buffer(eb);
747 return -EIO;
748 }
Jan Schmidtd5c88b72012-05-15 17:55:51 +0200749 btrfs_tree_read_lock(eb);
750 if (btrfs_header_level(eb) == 0)
751 btrfs_item_key_to_cpu(eb, &ref->key_for_search, 0);
752 else
753 btrfs_node_key_to_cpu(eb, &ref->key_for_search, 0);
754 btrfs_tree_read_unlock(eb);
755 free_extent_buffer(eb);
Edmund Nadolski3ec4d322017-07-12 16:20:10 -0600756 prelim_ref_insert(fs_info, &preftrees->indirect, ref, NULL);
Edmund Nadolski9dd14fd2017-07-12 16:20:09 -0600757 cond_resched();
Jan Schmidtd5c88b72012-05-15 17:55:51 +0200758 }
759 return 0;
760}
761
Jan Schmidt8da6d582011-11-23 18:55:04 +0100762/*
Jan Schmidt8da6d582011-11-23 18:55:04 +0100763 * add all currently queued delayed refs from this head whose seq nr is
764 * smaller or equal that seq to the list
765 */
Jeff Mahoney00142752017-07-12 16:20:08 -0600766static int add_delayed_refs(const struct btrfs_fs_info *fs_info,
767 struct btrfs_delayed_ref_head *head, u64 seq,
Edmund Nadolski86d5f992017-07-12 16:20:06 -0600768 struct preftrees *preftrees, u64 *total_refs,
Edmund Nadolski3ec4d322017-07-12 16:20:10 -0600769 struct share_check *sc)
Jan Schmidt8da6d582011-11-23 18:55:04 +0100770{
Qu Wenruoc6fc2452015-03-30 17:03:00 +0800771 struct btrfs_delayed_ref_node *node;
Jan Schmidt8da6d582011-11-23 18:55:04 +0100772 struct btrfs_delayed_extent_op *extent_op = head->extent_op;
Jan Schmidtd5c88b72012-05-15 17:55:51 +0200773 struct btrfs_key key;
Edmund Nadolski86d5f992017-07-12 16:20:06 -0600774 struct btrfs_key tmp_op_key;
775 struct btrfs_key *op_key = NULL;
Josef Bacik0e0adbc2017-10-19 14:16:00 -0400776 struct rb_node *n;
Edmund Nadolski01747e92017-07-12 16:20:11 -0600777 int count;
Jan Schmidtb1375d62012-01-26 15:01:11 -0500778 int ret = 0;
Jan Schmidt8da6d582011-11-23 18:55:04 +0100779
Edmund Nadolski86d5f992017-07-12 16:20:06 -0600780 if (extent_op && extent_op->update_key) {
781 btrfs_disk_key_to_cpu(&tmp_op_key, &extent_op->key);
782 op_key = &tmp_op_key;
783 }
Jan Schmidt8da6d582011-11-23 18:55:04 +0100784
Josef Bacikd7df2c72014-01-23 09:21:38 -0500785 spin_lock(&head->lock);
Josef Bacik0e0adbc2017-10-19 14:16:00 -0400786 for (n = rb_first(&head->ref_tree); n; n = rb_next(n)) {
787 node = rb_entry(n, struct btrfs_delayed_ref_node,
788 ref_node);
Jan Schmidt8da6d582011-11-23 18:55:04 +0100789 if (node->seq > seq)
790 continue;
791
792 switch (node->action) {
793 case BTRFS_ADD_DELAYED_EXTENT:
794 case BTRFS_UPDATE_DELAYED_HEAD:
795 WARN_ON(1);
796 continue;
797 case BTRFS_ADD_DELAYED_REF:
Edmund Nadolski01747e92017-07-12 16:20:11 -0600798 count = node->ref_mod;
Jan Schmidt8da6d582011-11-23 18:55:04 +0100799 break;
800 case BTRFS_DROP_DELAYED_REF:
Edmund Nadolski01747e92017-07-12 16:20:11 -0600801 count = node->ref_mod * -1;
Jan Schmidt8da6d582011-11-23 18:55:04 +0100802 break;
803 default:
804 BUG_ON(1);
805 }
Edmund Nadolski01747e92017-07-12 16:20:11 -0600806 *total_refs += count;
Jan Schmidt8da6d582011-11-23 18:55:04 +0100807 switch (node->type) {
808 case BTRFS_TREE_BLOCK_REF_KEY: {
Edmund Nadolski86d5f992017-07-12 16:20:06 -0600809 /* NORMAL INDIRECT METADATA backref */
Jan Schmidt8da6d582011-11-23 18:55:04 +0100810 struct btrfs_delayed_tree_ref *ref;
811
812 ref = btrfs_delayed_node_to_tree_ref(node);
Jeff Mahoney00142752017-07-12 16:20:08 -0600813 ret = add_indirect_ref(fs_info, preftrees, ref->root,
814 &tmp_op_key, ref->level + 1,
Edmund Nadolski01747e92017-07-12 16:20:11 -0600815 node->bytenr, count, sc,
816 GFP_ATOMIC);
Jan Schmidt8da6d582011-11-23 18:55:04 +0100817 break;
818 }
819 case BTRFS_SHARED_BLOCK_REF_KEY: {
Edmund Nadolski86d5f992017-07-12 16:20:06 -0600820 /* SHARED DIRECT METADATA backref */
Jan Schmidt8da6d582011-11-23 18:55:04 +0100821 struct btrfs_delayed_tree_ref *ref;
822
823 ref = btrfs_delayed_node_to_tree_ref(node);
Edmund Nadolski86d5f992017-07-12 16:20:06 -0600824
Edmund Nadolski01747e92017-07-12 16:20:11 -0600825 ret = add_direct_ref(fs_info, preftrees, ref->level + 1,
826 ref->parent, node->bytenr, count,
Edmund Nadolski3ec4d322017-07-12 16:20:10 -0600827 sc, GFP_ATOMIC);
Jan Schmidt8da6d582011-11-23 18:55:04 +0100828 break;
829 }
830 case BTRFS_EXTENT_DATA_REF_KEY: {
Edmund Nadolski86d5f992017-07-12 16:20:06 -0600831 /* NORMAL INDIRECT DATA backref */
Jan Schmidt8da6d582011-11-23 18:55:04 +0100832 struct btrfs_delayed_data_ref *ref;
Jan Schmidt8da6d582011-11-23 18:55:04 +0100833 ref = btrfs_delayed_node_to_data_ref(node);
834
835 key.objectid = ref->objectid;
836 key.type = BTRFS_EXTENT_DATA_KEY;
837 key.offset = ref->offset;
Josef Bacikdc046b12014-09-10 16:20:45 -0400838
839 /*
840 * Found a inum that doesn't match our known inum, we
841 * know it's shared.
842 */
Edmund Nadolski3ec4d322017-07-12 16:20:10 -0600843 if (sc && sc->inum && ref->objectid != sc->inum) {
Josef Bacikdc046b12014-09-10 16:20:45 -0400844 ret = BACKREF_FOUND_SHARED;
Edmund Nadolski3ec4d322017-07-12 16:20:10 -0600845 goto out;
Josef Bacikdc046b12014-09-10 16:20:45 -0400846 }
847
Jeff Mahoney00142752017-07-12 16:20:08 -0600848 ret = add_indirect_ref(fs_info, preftrees, ref->root,
Edmund Nadolski01747e92017-07-12 16:20:11 -0600849 &key, 0, node->bytenr, count, sc,
850 GFP_ATOMIC);
Jan Schmidt8da6d582011-11-23 18:55:04 +0100851 break;
852 }
853 case BTRFS_SHARED_DATA_REF_KEY: {
Edmund Nadolski86d5f992017-07-12 16:20:06 -0600854 /* SHARED DIRECT FULL backref */
Jan Schmidt8da6d582011-11-23 18:55:04 +0100855 struct btrfs_delayed_data_ref *ref;
Jan Schmidt8da6d582011-11-23 18:55:04 +0100856
857 ref = btrfs_delayed_node_to_data_ref(node);
Edmund Nadolski86d5f992017-07-12 16:20:06 -0600858
Edmund Nadolski01747e92017-07-12 16:20:11 -0600859 ret = add_direct_ref(fs_info, preftrees, 0, ref->parent,
860 node->bytenr, count, sc,
861 GFP_ATOMIC);
Jan Schmidt8da6d582011-11-23 18:55:04 +0100862 break;
863 }
864 default:
865 WARN_ON(1);
866 }
Edmund Nadolski3ec4d322017-07-12 16:20:10 -0600867 /*
868 * We must ignore BACKREF_FOUND_SHARED until all delayed
869 * refs have been checked.
870 */
871 if (ret && (ret != BACKREF_FOUND_SHARED))
Josef Bacikd7df2c72014-01-23 09:21:38 -0500872 break;
Jan Schmidt8da6d582011-11-23 18:55:04 +0100873 }
Edmund Nadolski3ec4d322017-07-12 16:20:10 -0600874 if (!ret)
875 ret = extent_is_shared(sc);
876out:
Josef Bacikd7df2c72014-01-23 09:21:38 -0500877 spin_unlock(&head->lock);
878 return ret;
Jan Schmidt8da6d582011-11-23 18:55:04 +0100879}
880
881/*
882 * add all inline backrefs for bytenr to the list
Edmund Nadolski3ec4d322017-07-12 16:20:10 -0600883 *
884 * Returns 0 on success, <0 on error, or BACKREF_FOUND_SHARED.
Jan Schmidt8da6d582011-11-23 18:55:04 +0100885 */
Jeff Mahoney00142752017-07-12 16:20:08 -0600886static int add_inline_refs(const struct btrfs_fs_info *fs_info,
887 struct btrfs_path *path, u64 bytenr,
Edmund Nadolski86d5f992017-07-12 16:20:06 -0600888 int *info_level, struct preftrees *preftrees,
Edmund Nadolski3ec4d322017-07-12 16:20:10 -0600889 u64 *total_refs, struct share_check *sc)
Jan Schmidt8da6d582011-11-23 18:55:04 +0100890{
Jan Schmidtb1375d62012-01-26 15:01:11 -0500891 int ret = 0;
Jan Schmidt8da6d582011-11-23 18:55:04 +0100892 int slot;
893 struct extent_buffer *leaf;
894 struct btrfs_key key;
Josef Bacik261c84b2013-06-28 13:11:22 -0400895 struct btrfs_key found_key;
Jan Schmidt8da6d582011-11-23 18:55:04 +0100896 unsigned long ptr;
897 unsigned long end;
898 struct btrfs_extent_item *ei;
899 u64 flags;
900 u64 item_size;
901
902 /*
903 * enumerate all inline refs
904 */
905 leaf = path->nodes[0];
Jan Schmidtdadcaf72012-05-22 13:43:25 +0200906 slot = path->slots[0];
Jan Schmidt8da6d582011-11-23 18:55:04 +0100907
908 item_size = btrfs_item_size_nr(leaf, slot);
909 BUG_ON(item_size < sizeof(*ei));
910
911 ei = btrfs_item_ptr(leaf, slot, struct btrfs_extent_item);
912 flags = btrfs_extent_flags(leaf, ei);
Josef Bacik44853862014-03-19 13:35:14 -0400913 *total_refs += btrfs_extent_refs(leaf, ei);
Josef Bacik261c84b2013-06-28 13:11:22 -0400914 btrfs_item_key_to_cpu(leaf, &found_key, slot);
Jan Schmidt8da6d582011-11-23 18:55:04 +0100915
916 ptr = (unsigned long)(ei + 1);
917 end = (unsigned long)ei + item_size;
918
Josef Bacik261c84b2013-06-28 13:11:22 -0400919 if (found_key.type == BTRFS_EXTENT_ITEM_KEY &&
920 flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
Jan Schmidt8da6d582011-11-23 18:55:04 +0100921 struct btrfs_tree_block_info *info;
Jan Schmidt8da6d582011-11-23 18:55:04 +0100922
923 info = (struct btrfs_tree_block_info *)ptr;
924 *info_level = btrfs_tree_block_level(leaf, info);
Jan Schmidt8da6d582011-11-23 18:55:04 +0100925 ptr += sizeof(struct btrfs_tree_block_info);
926 BUG_ON(ptr > end);
Josef Bacik261c84b2013-06-28 13:11:22 -0400927 } else if (found_key.type == BTRFS_METADATA_ITEM_KEY) {
928 *info_level = found_key.offset;
Jan Schmidt8da6d582011-11-23 18:55:04 +0100929 } else {
930 BUG_ON(!(flags & BTRFS_EXTENT_FLAG_DATA));
931 }
932
933 while (ptr < end) {
934 struct btrfs_extent_inline_ref *iref;
935 u64 offset;
936 int type;
937
938 iref = (struct btrfs_extent_inline_ref *)ptr;
Liu Bo3de28d52017-08-18 15:15:19 -0600939 type = btrfs_get_extent_inline_ref_type(leaf, iref,
940 BTRFS_REF_TYPE_ANY);
941 if (type == BTRFS_REF_TYPE_INVALID)
942 return -EINVAL;
943
Jan Schmidt8da6d582011-11-23 18:55:04 +0100944 offset = btrfs_extent_inline_ref_offset(leaf, iref);
945
946 switch (type) {
947 case BTRFS_SHARED_BLOCK_REF_KEY:
Jeff Mahoney00142752017-07-12 16:20:08 -0600948 ret = add_direct_ref(fs_info, preftrees,
949 *info_level + 1, offset,
Edmund Nadolski3ec4d322017-07-12 16:20:10 -0600950 bytenr, 1, NULL, GFP_NOFS);
Jan Schmidt8da6d582011-11-23 18:55:04 +0100951 break;
952 case BTRFS_SHARED_DATA_REF_KEY: {
953 struct btrfs_shared_data_ref *sdref;
954 int count;
955
956 sdref = (struct btrfs_shared_data_ref *)(iref + 1);
957 count = btrfs_shared_data_ref_count(leaf, sdref);
Edmund Nadolski86d5f992017-07-12 16:20:06 -0600958
Jeff Mahoney00142752017-07-12 16:20:08 -0600959 ret = add_direct_ref(fs_info, preftrees, 0, offset,
Edmund Nadolski3ec4d322017-07-12 16:20:10 -0600960 bytenr, count, sc, GFP_NOFS);
Jan Schmidt8da6d582011-11-23 18:55:04 +0100961 break;
962 }
963 case BTRFS_TREE_BLOCK_REF_KEY:
Jeff Mahoney00142752017-07-12 16:20:08 -0600964 ret = add_indirect_ref(fs_info, preftrees, offset,
965 NULL, *info_level + 1,
Edmund Nadolski3ec4d322017-07-12 16:20:10 -0600966 bytenr, 1, NULL, GFP_NOFS);
Jan Schmidt8da6d582011-11-23 18:55:04 +0100967 break;
968 case BTRFS_EXTENT_DATA_REF_KEY: {
969 struct btrfs_extent_data_ref *dref;
970 int count;
971 u64 root;
972
973 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
974 count = btrfs_extent_data_ref_count(leaf, dref);
975 key.objectid = btrfs_extent_data_ref_objectid(leaf,
976 dref);
977 key.type = BTRFS_EXTENT_DATA_KEY;
978 key.offset = btrfs_extent_data_ref_offset(leaf, dref);
Josef Bacikdc046b12014-09-10 16:20:45 -0400979
Edmund Nadolski3ec4d322017-07-12 16:20:10 -0600980 if (sc && sc->inum && key.objectid != sc->inum) {
Josef Bacikdc046b12014-09-10 16:20:45 -0400981 ret = BACKREF_FOUND_SHARED;
982 break;
983 }
984
Jan Schmidt8da6d582011-11-23 18:55:04 +0100985 root = btrfs_extent_data_ref_root(leaf, dref);
Edmund Nadolski86d5f992017-07-12 16:20:06 -0600986
Jeff Mahoney00142752017-07-12 16:20:08 -0600987 ret = add_indirect_ref(fs_info, preftrees, root,
988 &key, 0, bytenr, count,
Edmund Nadolski3ec4d322017-07-12 16:20:10 -0600989 sc, GFP_NOFS);
Jan Schmidt8da6d582011-11-23 18:55:04 +0100990 break;
991 }
992 default:
993 WARN_ON(1);
994 }
Wang Shilong1149ab62013-04-10 11:22:50 +0000995 if (ret)
996 return ret;
Jan Schmidt8da6d582011-11-23 18:55:04 +0100997 ptr += btrfs_extent_inline_ref_size(type);
998 }
999
1000 return 0;
1001}
1002
1003/*
1004 * add all non-inline backrefs for bytenr to the list
Edmund Nadolski3ec4d322017-07-12 16:20:10 -06001005 *
1006 * Returns 0 on success, <0 on error, or BACKREF_FOUND_SHARED.
Jan Schmidt8da6d582011-11-23 18:55:04 +01001007 */
Jeff Mahoneye0c476b2017-06-28 21:56:57 -06001008static int add_keyed_refs(struct btrfs_fs_info *fs_info,
1009 struct btrfs_path *path, u64 bytenr,
Edmund Nadolski86d5f992017-07-12 16:20:06 -06001010 int info_level, struct preftrees *preftrees,
Edmund Nadolski3ec4d322017-07-12 16:20:10 -06001011 struct share_check *sc)
Jan Schmidt8da6d582011-11-23 18:55:04 +01001012{
1013 struct btrfs_root *extent_root = fs_info->extent_root;
1014 int ret;
1015 int slot;
1016 struct extent_buffer *leaf;
1017 struct btrfs_key key;
1018
1019 while (1) {
1020 ret = btrfs_next_item(extent_root, path);
1021 if (ret < 0)
1022 break;
1023 if (ret) {
1024 ret = 0;
1025 break;
1026 }
1027
1028 slot = path->slots[0];
1029 leaf = path->nodes[0];
1030 btrfs_item_key_to_cpu(leaf, &key, slot);
1031
1032 if (key.objectid != bytenr)
1033 break;
1034 if (key.type < BTRFS_TREE_BLOCK_REF_KEY)
1035 continue;
1036 if (key.type > BTRFS_SHARED_DATA_REF_KEY)
1037 break;
1038
1039 switch (key.type) {
1040 case BTRFS_SHARED_BLOCK_REF_KEY:
Edmund Nadolski86d5f992017-07-12 16:20:06 -06001041 /* SHARED DIRECT METADATA backref */
Jeff Mahoney00142752017-07-12 16:20:08 -06001042 ret = add_direct_ref(fs_info, preftrees,
1043 info_level + 1, key.offset,
Edmund Nadolski3ec4d322017-07-12 16:20:10 -06001044 bytenr, 1, NULL, GFP_NOFS);
Jan Schmidt8da6d582011-11-23 18:55:04 +01001045 break;
1046 case BTRFS_SHARED_DATA_REF_KEY: {
Edmund Nadolski86d5f992017-07-12 16:20:06 -06001047 /* SHARED DIRECT FULL backref */
Jan Schmidt8da6d582011-11-23 18:55:04 +01001048 struct btrfs_shared_data_ref *sdref;
1049 int count;
1050
1051 sdref = btrfs_item_ptr(leaf, slot,
1052 struct btrfs_shared_data_ref);
1053 count = btrfs_shared_data_ref_count(leaf, sdref);
Jeff Mahoney00142752017-07-12 16:20:08 -06001054 ret = add_direct_ref(fs_info, preftrees, 0,
1055 key.offset, bytenr, count,
Edmund Nadolski3ec4d322017-07-12 16:20:10 -06001056 sc, GFP_NOFS);
Jan Schmidt8da6d582011-11-23 18:55:04 +01001057 break;
1058 }
1059 case BTRFS_TREE_BLOCK_REF_KEY:
Edmund Nadolski86d5f992017-07-12 16:20:06 -06001060 /* NORMAL INDIRECT METADATA backref */
Jeff Mahoney00142752017-07-12 16:20:08 -06001061 ret = add_indirect_ref(fs_info, preftrees, key.offset,
1062 NULL, info_level + 1, bytenr,
Edmund Nadolski3ec4d322017-07-12 16:20:10 -06001063 1, NULL, GFP_NOFS);
Jan Schmidt8da6d582011-11-23 18:55:04 +01001064 break;
1065 case BTRFS_EXTENT_DATA_REF_KEY: {
Edmund Nadolski86d5f992017-07-12 16:20:06 -06001066 /* NORMAL INDIRECT DATA backref */
Jan Schmidt8da6d582011-11-23 18:55:04 +01001067 struct btrfs_extent_data_ref *dref;
1068 int count;
1069 u64 root;
1070
1071 dref = btrfs_item_ptr(leaf, slot,
1072 struct btrfs_extent_data_ref);
1073 count = btrfs_extent_data_ref_count(leaf, dref);
1074 key.objectid = btrfs_extent_data_ref_objectid(leaf,
1075 dref);
1076 key.type = BTRFS_EXTENT_DATA_KEY;
1077 key.offset = btrfs_extent_data_ref_offset(leaf, dref);
Josef Bacikdc046b12014-09-10 16:20:45 -04001078
Edmund Nadolski3ec4d322017-07-12 16:20:10 -06001079 if (sc && sc->inum && key.objectid != sc->inum) {
Josef Bacikdc046b12014-09-10 16:20:45 -04001080 ret = BACKREF_FOUND_SHARED;
1081 break;
1082 }
1083
Jan Schmidt8da6d582011-11-23 18:55:04 +01001084 root = btrfs_extent_data_ref_root(leaf, dref);
Jeff Mahoney00142752017-07-12 16:20:08 -06001085 ret = add_indirect_ref(fs_info, preftrees, root,
1086 &key, 0, bytenr, count,
Edmund Nadolski3ec4d322017-07-12 16:20:10 -06001087 sc, GFP_NOFS);
Jan Schmidt8da6d582011-11-23 18:55:04 +01001088 break;
1089 }
1090 default:
1091 WARN_ON(1);
1092 }
Wang Shilong1149ab62013-04-10 11:22:50 +00001093 if (ret)
1094 return ret;
1095
Jan Schmidt8da6d582011-11-23 18:55:04 +01001096 }
1097
1098 return ret;
1099}
1100
1101/*
1102 * this adds all existing backrefs (inline backrefs, backrefs and delayed
1103 * refs) for the given bytenr to the refs list, merges duplicates and resolves
1104 * indirect refs to their parent bytenr.
1105 * When roots are found, they're added to the roots list
1106 *
Edmund Nadolskide47c9d2017-03-16 10:04:34 -06001107 * If time_seq is set to SEQ_LAST, it will not search delayed_refs, and behave
Qu Wenruo21633fc2015-04-16 14:54:50 +08001108 * much like trans == NULL case, the difference only lies in it will not
1109 * commit root.
1110 * The special case is for qgroup to search roots in commit_transaction().
1111 *
Edmund Nadolski3ec4d322017-07-12 16:20:10 -06001112 * @sc - if !NULL, then immediately return BACKREF_FOUND_SHARED when a
1113 * shared extent is detected.
1114 *
1115 * Otherwise this returns 0 for success and <0 for an error.
1116 *
Zygo Blaxellc995ab32017-09-22 13:58:45 -04001117 * If ignore_offset is set to false, only extent refs whose offsets match
1118 * extent_item_pos are returned. If true, every extent ref is returned
1119 * and extent_item_pos is ignored.
1120 *
Jan Schmidt8da6d582011-11-23 18:55:04 +01001121 * FIXME some caching might speed things up
1122 */
1123static int find_parent_nodes(struct btrfs_trans_handle *trans,
1124 struct btrfs_fs_info *fs_info, u64 bytenr,
Jan Schmidt097b8a72012-06-21 11:08:04 +02001125 u64 time_seq, struct ulist *refs,
Josef Bacikdc046b12014-09-10 16:20:45 -04001126 struct ulist *roots, const u64 *extent_item_pos,
Zygo Blaxellc995ab32017-09-22 13:58:45 -04001127 struct share_check *sc, bool ignore_offset)
Jan Schmidt8da6d582011-11-23 18:55:04 +01001128{
1129 struct btrfs_key key;
1130 struct btrfs_path *path;
Jan Schmidt8da6d582011-11-23 18:55:04 +01001131 struct btrfs_delayed_ref_root *delayed_refs = NULL;
Li Zefand3b01062012-03-03 07:41:15 -05001132 struct btrfs_delayed_ref_head *head;
Jan Schmidt8da6d582011-11-23 18:55:04 +01001133 int info_level = 0;
1134 int ret;
Jeff Mahoneye0c476b2017-06-28 21:56:57 -06001135 struct prelim_ref *ref;
Edmund Nadolski86d5f992017-07-12 16:20:06 -06001136 struct rb_node *node;
Wang Shilongf05c4742014-01-28 19:13:38 +08001137 struct extent_inode_elem *eie = NULL;
Edmund Nadolski86d5f992017-07-12 16:20:06 -06001138 /* total of both direct AND indirect refs! */
Josef Bacik44853862014-03-19 13:35:14 -04001139 u64 total_refs = 0;
Edmund Nadolski86d5f992017-07-12 16:20:06 -06001140 struct preftrees preftrees = {
1141 .direct = PREFTREE_INIT,
1142 .indirect = PREFTREE_INIT,
1143 .indirect_missing_keys = PREFTREE_INIT
1144 };
Jan Schmidt8da6d582011-11-23 18:55:04 +01001145
1146 key.objectid = bytenr;
Jan Schmidt8da6d582011-11-23 18:55:04 +01001147 key.offset = (u64)-1;
Josef Bacik261c84b2013-06-28 13:11:22 -04001148 if (btrfs_fs_incompat(fs_info, SKINNY_METADATA))
1149 key.type = BTRFS_METADATA_ITEM_KEY;
1150 else
1151 key.type = BTRFS_EXTENT_ITEM_KEY;
Jan Schmidt8da6d582011-11-23 18:55:04 +01001152
1153 path = btrfs_alloc_path();
1154 if (!path)
1155 return -ENOMEM;
Wang Shilonge84752d2014-02-13 11:19:47 +08001156 if (!trans) {
Josef Bacikda61d312013-06-12 16:20:08 -04001157 path->search_commit_root = 1;
Wang Shilonge84752d2014-02-13 11:19:47 +08001158 path->skip_locking = 1;
1159 }
Jan Schmidt8da6d582011-11-23 18:55:04 +01001160
Edmund Nadolskide47c9d2017-03-16 10:04:34 -06001161 if (time_seq == SEQ_LAST)
Qu Wenruo21633fc2015-04-16 14:54:50 +08001162 path->skip_locking = 1;
1163
Jan Schmidt8da6d582011-11-23 18:55:04 +01001164 /*
1165 * grab both a lock on the path and a lock on the delayed ref head.
1166 * We need both to get a consistent picture of how the refs look
1167 * at a specified point in time
1168 */
1169again:
Li Zefand3b01062012-03-03 07:41:15 -05001170 head = NULL;
1171
Jan Schmidt8da6d582011-11-23 18:55:04 +01001172 ret = btrfs_search_slot(trans, fs_info->extent_root, &key, path, 0, 0);
1173 if (ret < 0)
1174 goto out;
1175 BUG_ON(ret == 0);
1176
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04001177#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
Qu Wenruo21633fc2015-04-16 14:54:50 +08001178 if (trans && likely(trans->type != __TRANS_DUMMY) &&
Edmund Nadolskide47c9d2017-03-16 10:04:34 -06001179 time_seq != SEQ_LAST) {
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04001180#else
Edmund Nadolskide47c9d2017-03-16 10:04:34 -06001181 if (trans && time_seq != SEQ_LAST) {
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04001182#endif
Jan Schmidt7a3ae2f2012-03-23 17:32:28 +01001183 /*
1184 * look if there are updates for this ref queued and lock the
1185 * head
1186 */
1187 delayed_refs = &trans->transaction->delayed_refs;
1188 spin_lock(&delayed_refs->lock);
Liu Bof72ad18e2017-01-30 12:24:37 -08001189 head = btrfs_find_delayed_ref_head(delayed_refs, bytenr);
Jan Schmidt7a3ae2f2012-03-23 17:32:28 +01001190 if (head) {
1191 if (!mutex_trylock(&head->mutex)) {
Josef Bacikd2788502017-09-29 15:43:57 -04001192 refcount_inc(&head->refs);
Jan Schmidt7a3ae2f2012-03-23 17:32:28 +01001193 spin_unlock(&delayed_refs->lock);
Jan Schmidt8da6d582011-11-23 18:55:04 +01001194
Jan Schmidt7a3ae2f2012-03-23 17:32:28 +01001195 btrfs_release_path(path);
Jan Schmidt8da6d582011-11-23 18:55:04 +01001196
Jan Schmidt7a3ae2f2012-03-23 17:32:28 +01001197 /*
1198 * Mutex was contended, block until it's
1199 * released and try again
1200 */
1201 mutex_lock(&head->mutex);
1202 mutex_unlock(&head->mutex);
Josef Bacikd2788502017-09-29 15:43:57 -04001203 btrfs_put_delayed_ref_head(head);
Jan Schmidt7a3ae2f2012-03-23 17:32:28 +01001204 goto again;
1205 }
Josef Bacikd7df2c72014-01-23 09:21:38 -05001206 spin_unlock(&delayed_refs->lock);
Jeff Mahoney00142752017-07-12 16:20:08 -06001207 ret = add_delayed_refs(fs_info, head, time_seq,
Edmund Nadolski3ec4d322017-07-12 16:20:10 -06001208 &preftrees, &total_refs, sc);
Jan Schmidt155725c2012-06-22 14:01:00 +02001209 mutex_unlock(&head->mutex);
Josef Bacikd7df2c72014-01-23 09:21:38 -05001210 if (ret)
Jan Schmidt7a3ae2f2012-03-23 17:32:28 +01001211 goto out;
Josef Bacikd7df2c72014-01-23 09:21:38 -05001212 } else {
1213 spin_unlock(&delayed_refs->lock);
Jan Schmidt8da6d582011-11-23 18:55:04 +01001214 }
Jan Schmidt8da6d582011-11-23 18:55:04 +01001215 }
Jan Schmidt8da6d582011-11-23 18:55:04 +01001216
1217 if (path->slots[0]) {
1218 struct extent_buffer *leaf;
1219 int slot;
1220
Jan Schmidtdadcaf72012-05-22 13:43:25 +02001221 path->slots[0]--;
Jan Schmidt8da6d582011-11-23 18:55:04 +01001222 leaf = path->nodes[0];
Jan Schmidtdadcaf72012-05-22 13:43:25 +02001223 slot = path->slots[0];
Jan Schmidt8da6d582011-11-23 18:55:04 +01001224 btrfs_item_key_to_cpu(leaf, &key, slot);
1225 if (key.objectid == bytenr &&
Josef Bacik261c84b2013-06-28 13:11:22 -04001226 (key.type == BTRFS_EXTENT_ITEM_KEY ||
1227 key.type == BTRFS_METADATA_ITEM_KEY)) {
Jeff Mahoney00142752017-07-12 16:20:08 -06001228 ret = add_inline_refs(fs_info, path, bytenr,
1229 &info_level, &preftrees,
Edmund Nadolski3ec4d322017-07-12 16:20:10 -06001230 &total_refs, sc);
Jan Schmidt8da6d582011-11-23 18:55:04 +01001231 if (ret)
1232 goto out;
Jeff Mahoneye0c476b2017-06-28 21:56:57 -06001233 ret = add_keyed_refs(fs_info, path, bytenr, info_level,
Edmund Nadolski3ec4d322017-07-12 16:20:10 -06001234 &preftrees, sc);
Jan Schmidt8da6d582011-11-23 18:55:04 +01001235 if (ret)
1236 goto out;
1237 }
1238 }
Edmund Nadolski86d5f992017-07-12 16:20:06 -06001239
Jan Schmidt8da6d582011-11-23 18:55:04 +01001240 btrfs_release_path(path);
1241
Edmund Nadolski86d5f992017-07-12 16:20:06 -06001242 ret = add_missing_keys(fs_info, &preftrees);
Jan Schmidtd5c88b72012-05-15 17:55:51 +02001243 if (ret)
1244 goto out;
1245
Edmund Nadolski86d5f992017-07-12 16:20:06 -06001246 WARN_ON(!RB_EMPTY_ROOT(&preftrees.indirect_missing_keys.root));
Jan Schmidt8da6d582011-11-23 18:55:04 +01001247
Edmund Nadolski86d5f992017-07-12 16:20:06 -06001248 ret = resolve_indirect_refs(fs_info, path, time_seq, &preftrees,
Zygo Blaxellc995ab32017-09-22 13:58:45 -04001249 extent_item_pos, total_refs, sc, ignore_offset);
Jan Schmidt8da6d582011-11-23 18:55:04 +01001250 if (ret)
1251 goto out;
1252
Edmund Nadolski86d5f992017-07-12 16:20:06 -06001253 WARN_ON(!RB_EMPTY_ROOT(&preftrees.indirect.root));
Jan Schmidt8da6d582011-11-23 18:55:04 +01001254
Edmund Nadolski86d5f992017-07-12 16:20:06 -06001255 /*
1256 * This walks the tree of merged and resolved refs. Tree blocks are
1257 * read in as needed. Unique entries are added to the ulist, and
1258 * the list of found roots is updated.
1259 *
1260 * We release the entire tree in one go before returning.
1261 */
1262 node = rb_first(&preftrees.direct.root);
1263 while (node) {
1264 ref = rb_entry(node, struct prelim_ref, rbnode);
1265 node = rb_next(&ref->rbnode);
Julia Lawall6c1500f2012-11-03 20:30:18 +00001266 WARN_ON(ref->count < 0);
Wang Shilong98cfee212014-02-01 00:42:05 +08001267 if (roots && ref->count && ref->root_id && ref->parent == 0) {
Edmund Nadolski3ec4d322017-07-12 16:20:10 -06001268 if (sc && sc->root_objectid &&
1269 ref->root_id != sc->root_objectid) {
Josef Bacikdc046b12014-09-10 16:20:45 -04001270 ret = BACKREF_FOUND_SHARED;
1271 goto out;
1272 }
1273
Jan Schmidt8da6d582011-11-23 18:55:04 +01001274 /* no parent == root of tree */
1275 ret = ulist_add(roots, ref->root_id, 0, GFP_NOFS);
Wang Shilongf1723932013-03-29 23:03:21 +00001276 if (ret < 0)
1277 goto out;
Jan Schmidt8da6d582011-11-23 18:55:04 +01001278 }
1279 if (ref->count && ref->parent) {
Josef Bacik8a564572014-06-05 16:08:45 -04001280 if (extent_item_pos && !ref->inode_list &&
1281 ref->level == 0) {
Jan Schmidt976b1902012-05-17 16:43:03 +02001282 struct extent_buffer *eb;
David Sterba707e8a02014-06-04 19:22:26 +02001283
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001284 eb = read_tree_block(fs_info, ref->parent, 0);
Liu Bo64c043d2015-05-25 17:30:15 +08001285 if (IS_ERR(eb)) {
1286 ret = PTR_ERR(eb);
1287 goto out;
1288 } else if (!extent_buffer_uptodate(eb)) {
Josef Bacik416bc652013-04-23 14:17:42 -04001289 free_extent_buffer(eb);
Wang Shilongc16c2e22013-05-08 08:10:25 +00001290 ret = -EIO;
1291 goto out;
Josef Bacik416bc652013-04-23 14:17:42 -04001292 }
Filipe Manana6f7ff6d2014-07-02 20:07:54 +01001293 btrfs_tree_read_lock(eb);
1294 btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK);
Jan Schmidt976b1902012-05-17 16:43:03 +02001295 ret = find_extent_in_eb(eb, bytenr,
Zygo Blaxellc995ab32017-09-22 13:58:45 -04001296 *extent_item_pos, &eie, ignore_offset);
Filipe Manana6f7ff6d2014-07-02 20:07:54 +01001297 btrfs_tree_read_unlock_blocking(eb);
Jan Schmidt976b1902012-05-17 16:43:03 +02001298 free_extent_buffer(eb);
Filipe David Borba Mananaf5929cd2013-07-31 00:26:35 +01001299 if (ret < 0)
1300 goto out;
1301 ref->inode_list = eie;
Jan Schmidt976b1902012-05-17 16:43:03 +02001302 }
Takashi Iwai4eb1f662014-07-28 10:57:04 +02001303 ret = ulist_add_merge_ptr(refs, ref->parent,
1304 ref->inode_list,
1305 (void **)&eie, GFP_NOFS);
Wang Shilongf1723932013-03-29 23:03:21 +00001306 if (ret < 0)
1307 goto out;
Jan Schmidt33019582012-05-30 18:05:21 +02001308 if (!ret && extent_item_pos) {
1309 /*
1310 * we've recorded that parent, so we must extend
1311 * its inode list here
1312 */
1313 BUG_ON(!eie);
1314 while (eie->next)
1315 eie = eie->next;
1316 eie->next = ref->inode_list;
1317 }
Wang Shilongf05c4742014-01-28 19:13:38 +08001318 eie = NULL;
Jan Schmidt8da6d582011-11-23 18:55:04 +01001319 }
Edmund Nadolski9dd14fd2017-07-12 16:20:09 -06001320 cond_resched();
Jan Schmidt8da6d582011-11-23 18:55:04 +01001321 }
1322
1323out:
Jan Schmidt8da6d582011-11-23 18:55:04 +01001324 btrfs_free_path(path);
Edmund Nadolski86d5f992017-07-12 16:20:06 -06001325
1326 prelim_release(&preftrees.direct);
1327 prelim_release(&preftrees.indirect);
1328 prelim_release(&preftrees.indirect_missing_keys);
1329
Wang Shilongf05c4742014-01-28 19:13:38 +08001330 if (ret < 0)
1331 free_inode_elem_list(eie);
Jan Schmidt8da6d582011-11-23 18:55:04 +01001332 return ret;
1333}
1334
Jan Schmidt976b1902012-05-17 16:43:03 +02001335static void free_leaf_list(struct ulist *blocks)
1336{
1337 struct ulist_node *node = NULL;
1338 struct extent_inode_elem *eie;
Jan Schmidt976b1902012-05-17 16:43:03 +02001339 struct ulist_iterator uiter;
1340
1341 ULIST_ITER_INIT(&uiter);
1342 while ((node = ulist_next(blocks, &uiter))) {
1343 if (!node->aux)
1344 continue;
Jeff Mahoney4dae0772017-06-28 21:56:56 -06001345 eie = unode_aux_to_inode_list(node);
Wang Shilongf05c4742014-01-28 19:13:38 +08001346 free_inode_elem_list(eie);
Jan Schmidt976b1902012-05-17 16:43:03 +02001347 node->aux = 0;
1348 }
1349
1350 ulist_free(blocks);
1351}
1352
Jan Schmidt8da6d582011-11-23 18:55:04 +01001353/*
1354 * Finds all leafs with a reference to the specified combination of bytenr and
1355 * offset. key_list_head will point to a list of corresponding keys (caller must
1356 * free each list element). The leafs will be stored in the leafs ulist, which
1357 * must be freed with ulist_free.
1358 *
1359 * returns 0 on success, <0 on error
1360 */
1361static int btrfs_find_all_leafs(struct btrfs_trans_handle *trans,
1362 struct btrfs_fs_info *fs_info, u64 bytenr,
Jan Schmidt097b8a72012-06-21 11:08:04 +02001363 u64 time_seq, struct ulist **leafs,
Zygo Blaxellc995ab32017-09-22 13:58:45 -04001364 const u64 *extent_item_pos, bool ignore_offset)
Jan Schmidt8da6d582011-11-23 18:55:04 +01001365{
Jan Schmidt8da6d582011-11-23 18:55:04 +01001366 int ret;
1367
Jan Schmidt8da6d582011-11-23 18:55:04 +01001368 *leafs = ulist_alloc(GFP_NOFS);
Wang Shilong98cfee212014-02-01 00:42:05 +08001369 if (!*leafs)
Jan Schmidt8da6d582011-11-23 18:55:04 +01001370 return -ENOMEM;
Jan Schmidt8da6d582011-11-23 18:55:04 +01001371
Lu Fengqiafce7722016-06-13 09:36:46 +08001372 ret = find_parent_nodes(trans, fs_info, bytenr, time_seq,
Zygo Blaxellc995ab32017-09-22 13:58:45 -04001373 *leafs, NULL, extent_item_pos, NULL, ignore_offset);
Jan Schmidt8da6d582011-11-23 18:55:04 +01001374 if (ret < 0 && ret != -ENOENT) {
Jan Schmidt976b1902012-05-17 16:43:03 +02001375 free_leaf_list(*leafs);
Jan Schmidt8da6d582011-11-23 18:55:04 +01001376 return ret;
1377 }
1378
1379 return 0;
1380}
1381
1382/*
1383 * walk all backrefs for a given extent to find all roots that reference this
1384 * extent. Walking a backref means finding all extents that reference this
1385 * extent and in turn walk the backrefs of those, too. Naturally this is a
1386 * recursive process, but here it is implemented in an iterative fashion: We
1387 * find all referencing extents for the extent in question and put them on a
1388 * list. In turn, we find all referencing extents for those, further appending
1389 * to the list. The way we iterate the list allows adding more elements after
1390 * the current while iterating. The process stops when we reach the end of the
1391 * list. Found roots are added to the roots list.
1392 *
1393 * returns 0 on success, < 0 on error.
1394 */
Jeff Mahoneye0c476b2017-06-28 21:56:57 -06001395static int btrfs_find_all_roots_safe(struct btrfs_trans_handle *trans,
1396 struct btrfs_fs_info *fs_info, u64 bytenr,
Zygo Blaxellc995ab32017-09-22 13:58:45 -04001397 u64 time_seq, struct ulist **roots,
1398 bool ignore_offset)
Jan Schmidt8da6d582011-11-23 18:55:04 +01001399{
1400 struct ulist *tmp;
1401 struct ulist_node *node = NULL;
Jan Schmidtcd1b4132012-05-22 14:56:50 +02001402 struct ulist_iterator uiter;
Jan Schmidt8da6d582011-11-23 18:55:04 +01001403 int ret;
1404
1405 tmp = ulist_alloc(GFP_NOFS);
1406 if (!tmp)
1407 return -ENOMEM;
1408 *roots = ulist_alloc(GFP_NOFS);
1409 if (!*roots) {
1410 ulist_free(tmp);
1411 return -ENOMEM;
1412 }
1413
Jan Schmidtcd1b4132012-05-22 14:56:50 +02001414 ULIST_ITER_INIT(&uiter);
Jan Schmidt8da6d582011-11-23 18:55:04 +01001415 while (1) {
Lu Fengqiafce7722016-06-13 09:36:46 +08001416 ret = find_parent_nodes(trans, fs_info, bytenr, time_seq,
Zygo Blaxellc995ab32017-09-22 13:58:45 -04001417 tmp, *roots, NULL, NULL, ignore_offset);
Jan Schmidt8da6d582011-11-23 18:55:04 +01001418 if (ret < 0 && ret != -ENOENT) {
1419 ulist_free(tmp);
1420 ulist_free(*roots);
1421 return ret;
1422 }
Jan Schmidtcd1b4132012-05-22 14:56:50 +02001423 node = ulist_next(tmp, &uiter);
Jan Schmidt8da6d582011-11-23 18:55:04 +01001424 if (!node)
1425 break;
1426 bytenr = node->val;
Wang Shilongbca1a292014-01-26 22:32:18 +08001427 cond_resched();
Jan Schmidt8da6d582011-11-23 18:55:04 +01001428 }
1429
1430 ulist_free(tmp);
1431 return 0;
1432}
1433
Josef Bacik9e351cc2014-03-13 15:42:13 -04001434int btrfs_find_all_roots(struct btrfs_trans_handle *trans,
1435 struct btrfs_fs_info *fs_info, u64 bytenr,
Zygo Blaxellc995ab32017-09-22 13:58:45 -04001436 u64 time_seq, struct ulist **roots,
1437 bool ignore_offset)
Josef Bacik9e351cc2014-03-13 15:42:13 -04001438{
1439 int ret;
1440
1441 if (!trans)
1442 down_read(&fs_info->commit_root_sem);
Jeff Mahoneye0c476b2017-06-28 21:56:57 -06001443 ret = btrfs_find_all_roots_safe(trans, fs_info, bytenr,
Zygo Blaxellc995ab32017-09-22 13:58:45 -04001444 time_seq, roots, ignore_offset);
Josef Bacik9e351cc2014-03-13 15:42:13 -04001445 if (!trans)
1446 up_read(&fs_info->commit_root_sem);
1447 return ret;
1448}
1449
Mark Fasheh2c2ed5a2015-05-19 12:49:50 -07001450/**
1451 * btrfs_check_shared - tell us whether an extent is shared
1452 *
Mark Fasheh2c2ed5a2015-05-19 12:49:50 -07001453 * btrfs_check_shared uses the backref walking code but will short
1454 * circuit as soon as it finds a root or inode that doesn't match the
1455 * one passed in. This provides a significant performance benefit for
1456 * callers (such as fiemap) which want to know whether the extent is
1457 * shared but do not need a ref count.
1458 *
Edmund Nadolskibb739cf2017-06-28 21:56:58 -06001459 * This attempts to allocate a transaction in order to account for
1460 * delayed refs, but continues on even when the alloc fails.
1461 *
Mark Fasheh2c2ed5a2015-05-19 12:49:50 -07001462 * Return: 0 if extent is not shared, 1 if it is shared, < 0 on error.
1463 */
Edmund Nadolskibb739cf2017-06-28 21:56:58 -06001464int btrfs_check_shared(struct btrfs_root *root, u64 inum, u64 bytenr)
Josef Bacikdc046b12014-09-10 16:20:45 -04001465{
Edmund Nadolskibb739cf2017-06-28 21:56:58 -06001466 struct btrfs_fs_info *fs_info = root->fs_info;
1467 struct btrfs_trans_handle *trans;
Josef Bacikdc046b12014-09-10 16:20:45 -04001468 struct ulist *tmp = NULL;
1469 struct ulist *roots = NULL;
1470 struct ulist_iterator uiter;
1471 struct ulist_node *node;
David Sterba3284da72015-02-25 15:47:32 +01001472 struct seq_list elem = SEQ_LIST_INIT(elem);
Josef Bacikdc046b12014-09-10 16:20:45 -04001473 int ret = 0;
Edmund Nadolski3ec4d322017-07-12 16:20:10 -06001474 struct share_check shared = {
1475 .root_objectid = root->objectid,
1476 .inum = inum,
1477 .share_count = 0,
1478 };
Josef Bacikdc046b12014-09-10 16:20:45 -04001479
1480 tmp = ulist_alloc(GFP_NOFS);
1481 roots = ulist_alloc(GFP_NOFS);
1482 if (!tmp || !roots) {
1483 ulist_free(tmp);
1484 ulist_free(roots);
1485 return -ENOMEM;
1486 }
1487
Edmund Nadolskibb739cf2017-06-28 21:56:58 -06001488 trans = btrfs_join_transaction(root);
1489 if (IS_ERR(trans)) {
1490 trans = NULL;
Josef Bacikdc046b12014-09-10 16:20:45 -04001491 down_read(&fs_info->commit_root_sem);
Edmund Nadolskibb739cf2017-06-28 21:56:58 -06001492 } else {
1493 btrfs_get_tree_mod_seq(fs_info, &elem);
1494 }
1495
Josef Bacikdc046b12014-09-10 16:20:45 -04001496 ULIST_ITER_INIT(&uiter);
1497 while (1) {
1498 ret = find_parent_nodes(trans, fs_info, bytenr, elem.seq, tmp,
Zygo Blaxellc995ab32017-09-22 13:58:45 -04001499 roots, NULL, &shared, false);
Josef Bacikdc046b12014-09-10 16:20:45 -04001500 if (ret == BACKREF_FOUND_SHARED) {
Mark Fasheh2c2ed5a2015-05-19 12:49:50 -07001501 /* this is the only condition under which we return 1 */
Josef Bacikdc046b12014-09-10 16:20:45 -04001502 ret = 1;
1503 break;
1504 }
1505 if (ret < 0 && ret != -ENOENT)
1506 break;
Mark Fasheh2c2ed5a2015-05-19 12:49:50 -07001507 ret = 0;
Josef Bacikdc046b12014-09-10 16:20:45 -04001508 node = ulist_next(tmp, &uiter);
1509 if (!node)
1510 break;
1511 bytenr = node->val;
1512 cond_resched();
1513 }
Edmund Nadolskibb739cf2017-06-28 21:56:58 -06001514
1515 if (trans) {
Josef Bacikdc046b12014-09-10 16:20:45 -04001516 btrfs_put_tree_mod_seq(fs_info, &elem);
Edmund Nadolskibb739cf2017-06-28 21:56:58 -06001517 btrfs_end_transaction(trans);
1518 } else {
Josef Bacikdc046b12014-09-10 16:20:45 -04001519 up_read(&fs_info->commit_root_sem);
Edmund Nadolskibb739cf2017-06-28 21:56:58 -06001520 }
Josef Bacikdc046b12014-09-10 16:20:45 -04001521 ulist_free(tmp);
1522 ulist_free(roots);
1523 return ret;
1524}
1525
Mark Fashehf1863732012-08-08 11:32:27 -07001526int btrfs_find_one_extref(struct btrfs_root *root, u64 inode_objectid,
1527 u64 start_off, struct btrfs_path *path,
1528 struct btrfs_inode_extref **ret_extref,
1529 u64 *found_off)
1530{
1531 int ret, slot;
1532 struct btrfs_key key;
1533 struct btrfs_key found_key;
1534 struct btrfs_inode_extref *extref;
Jeff Mahoney73980be2017-06-28 21:56:55 -06001535 const struct extent_buffer *leaf;
Mark Fashehf1863732012-08-08 11:32:27 -07001536 unsigned long ptr;
1537
1538 key.objectid = inode_objectid;
David Sterba962a2982014-06-04 18:41:45 +02001539 key.type = BTRFS_INODE_EXTREF_KEY;
Mark Fashehf1863732012-08-08 11:32:27 -07001540 key.offset = start_off;
1541
1542 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1543 if (ret < 0)
1544 return ret;
1545
1546 while (1) {
1547 leaf = path->nodes[0];
1548 slot = path->slots[0];
1549 if (slot >= btrfs_header_nritems(leaf)) {
1550 /*
1551 * If the item at offset is not found,
1552 * btrfs_search_slot will point us to the slot
1553 * where it should be inserted. In our case
1554 * that will be the slot directly before the
1555 * next INODE_REF_KEY_V2 item. In the case
1556 * that we're pointing to the last slot in a
1557 * leaf, we must move one leaf over.
1558 */
1559 ret = btrfs_next_leaf(root, path);
1560 if (ret) {
1561 if (ret >= 1)
1562 ret = -ENOENT;
1563 break;
1564 }
1565 continue;
1566 }
1567
1568 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1569
1570 /*
1571 * Check that we're still looking at an extended ref key for
1572 * this particular objectid. If we have different
1573 * objectid or type then there are no more to be found
1574 * in the tree and we can exit.
1575 */
1576 ret = -ENOENT;
1577 if (found_key.objectid != inode_objectid)
1578 break;
David Sterba962a2982014-06-04 18:41:45 +02001579 if (found_key.type != BTRFS_INODE_EXTREF_KEY)
Mark Fashehf1863732012-08-08 11:32:27 -07001580 break;
1581
1582 ret = 0;
1583 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
1584 extref = (struct btrfs_inode_extref *)ptr;
1585 *ret_extref = extref;
1586 if (found_off)
1587 *found_off = found_key.offset;
1588 break;
1589 }
1590
1591 return ret;
1592}
1593
Eric Sandeen48a3b632013-04-25 20:41:01 +00001594/*
1595 * this iterates to turn a name (from iref/extref) into a full filesystem path.
1596 * Elements of the path are separated by '/' and the path is guaranteed to be
1597 * 0-terminated. the path is only given within the current file system.
1598 * Therefore, it never starts with a '/'. the caller is responsible to provide
1599 * "size" bytes in "dest". the dest buffer will be filled backwards. finally,
1600 * the start point of the resulting string is returned. this pointer is within
1601 * dest, normally.
1602 * in case the path buffer would overflow, the pointer is decremented further
1603 * as if output was written to the buffer, though no more output is actually
1604 * generated. that way, the caller can determine how much space would be
1605 * required for the path to fit into the buffer. in that case, the returned
1606 * value will be smaller than dest. callers must check this!
1607 */
Jan Schmidt96b5bd72012-10-15 08:30:45 +00001608char *btrfs_ref_to_path(struct btrfs_root *fs_root, struct btrfs_path *path,
1609 u32 name_len, unsigned long name_off,
1610 struct extent_buffer *eb_in, u64 parent,
1611 char *dest, u32 size)
Jan Schmidta542ad12011-06-13 19:52:59 +02001612{
Jan Schmidta542ad12011-06-13 19:52:59 +02001613 int slot;
1614 u64 next_inum;
1615 int ret;
Gabriel de Perthuis661bec62012-10-10 08:50:47 -06001616 s64 bytes_left = ((s64)size) - 1;
Jan Schmidta542ad12011-06-13 19:52:59 +02001617 struct extent_buffer *eb = eb_in;
1618 struct btrfs_key found_key;
Jan Schmidtb916a592012-04-13 12:28:08 +02001619 int leave_spinning = path->leave_spinning;
Mark Fashehd24bec32012-08-08 11:33:54 -07001620 struct btrfs_inode_ref *iref;
Jan Schmidta542ad12011-06-13 19:52:59 +02001621
1622 if (bytes_left >= 0)
1623 dest[bytes_left] = '\0';
1624
Jan Schmidtb916a592012-04-13 12:28:08 +02001625 path->leave_spinning = 1;
Jan Schmidta542ad12011-06-13 19:52:59 +02001626 while (1) {
Mark Fashehd24bec32012-08-08 11:33:54 -07001627 bytes_left -= name_len;
Jan Schmidta542ad12011-06-13 19:52:59 +02001628 if (bytes_left >= 0)
1629 read_extent_buffer(eb, dest + bytes_left,
Mark Fashehd24bec32012-08-08 11:33:54 -07001630 name_off, name_len);
Jan Schmidtb916a592012-04-13 12:28:08 +02001631 if (eb != eb_in) {
Filipe Manana0c0fe3b2016-02-03 19:17:27 +00001632 if (!path->skip_locking)
1633 btrfs_tree_read_unlock_blocking(eb);
Jan Schmidta542ad12011-06-13 19:52:59 +02001634 free_extent_buffer(eb);
Jan Schmidtb916a592012-04-13 12:28:08 +02001635 }
David Sterbac234a242015-01-02 19:03:17 +01001636 ret = btrfs_find_item(fs_root, path, parent, 0,
1637 BTRFS_INODE_REF_KEY, &found_key);
Jan Schmidt8f24b492012-02-08 16:01:01 +01001638 if (ret > 0)
1639 ret = -ENOENT;
Jan Schmidta542ad12011-06-13 19:52:59 +02001640 if (ret)
1641 break;
Mark Fashehd24bec32012-08-08 11:33:54 -07001642
Jan Schmidta542ad12011-06-13 19:52:59 +02001643 next_inum = found_key.offset;
1644
1645 /* regular exit ahead */
1646 if (parent == next_inum)
1647 break;
1648
1649 slot = path->slots[0];
1650 eb = path->nodes[0];
1651 /* make sure we can use eb after releasing the path */
Jan Schmidtb916a592012-04-13 12:28:08 +02001652 if (eb != eb_in) {
Filipe Manana0c0fe3b2016-02-03 19:17:27 +00001653 if (!path->skip_locking)
1654 btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK);
1655 path->nodes[0] = NULL;
1656 path->locks[0] = 0;
Jan Schmidtb916a592012-04-13 12:28:08 +02001657 }
Jan Schmidta542ad12011-06-13 19:52:59 +02001658 btrfs_release_path(path);
Jan Schmidta542ad12011-06-13 19:52:59 +02001659 iref = btrfs_item_ptr(eb, slot, struct btrfs_inode_ref);
Mark Fashehd24bec32012-08-08 11:33:54 -07001660
1661 name_len = btrfs_inode_ref_name_len(eb, iref);
1662 name_off = (unsigned long)(iref + 1);
1663
Jan Schmidta542ad12011-06-13 19:52:59 +02001664 parent = next_inum;
1665 --bytes_left;
1666 if (bytes_left >= 0)
1667 dest[bytes_left] = '/';
1668 }
1669
1670 btrfs_release_path(path);
Jan Schmidtb916a592012-04-13 12:28:08 +02001671 path->leave_spinning = leave_spinning;
Jan Schmidta542ad12011-06-13 19:52:59 +02001672
1673 if (ret)
1674 return ERR_PTR(ret);
1675
1676 return dest + bytes_left;
1677}
1678
1679/*
1680 * this makes the path point to (logical EXTENT_ITEM *)
1681 * returns BTRFS_EXTENT_FLAG_DATA for data, BTRFS_EXTENT_FLAG_TREE_BLOCK for
1682 * tree blocks and <0 on error.
1683 */
1684int extent_from_logical(struct btrfs_fs_info *fs_info, u64 logical,
Liu Bo69917e42012-09-07 20:01:28 -06001685 struct btrfs_path *path, struct btrfs_key *found_key,
1686 u64 *flags_ret)
Jan Schmidta542ad12011-06-13 19:52:59 +02001687{
1688 int ret;
1689 u64 flags;
Josef Bacik261c84b2013-06-28 13:11:22 -04001690 u64 size = 0;
Jan Schmidta542ad12011-06-13 19:52:59 +02001691 u32 item_size;
Jeff Mahoney73980be2017-06-28 21:56:55 -06001692 const struct extent_buffer *eb;
Jan Schmidta542ad12011-06-13 19:52:59 +02001693 struct btrfs_extent_item *ei;
1694 struct btrfs_key key;
1695
Josef Bacik261c84b2013-06-28 13:11:22 -04001696 if (btrfs_fs_incompat(fs_info, SKINNY_METADATA))
1697 key.type = BTRFS_METADATA_ITEM_KEY;
1698 else
1699 key.type = BTRFS_EXTENT_ITEM_KEY;
Jan Schmidta542ad12011-06-13 19:52:59 +02001700 key.objectid = logical;
1701 key.offset = (u64)-1;
1702
1703 ret = btrfs_search_slot(NULL, fs_info->extent_root, &key, path, 0, 0);
1704 if (ret < 0)
1705 return ret;
Jan Schmidta542ad12011-06-13 19:52:59 +02001706
Wang Shilong850a8cd2014-02-06 20:02:29 +08001707 ret = btrfs_previous_extent_item(fs_info->extent_root, path, 0);
1708 if (ret) {
1709 if (ret > 0)
1710 ret = -ENOENT;
1711 return ret;
Josef Bacik580f0a62014-01-23 16:03:45 -05001712 }
Wang Shilong850a8cd2014-02-06 20:02:29 +08001713 btrfs_item_key_to_cpu(path->nodes[0], found_key, path->slots[0]);
Josef Bacik261c84b2013-06-28 13:11:22 -04001714 if (found_key->type == BTRFS_METADATA_ITEM_KEY)
Jeff Mahoneyda170662016-06-15 09:22:56 -04001715 size = fs_info->nodesize;
Josef Bacik261c84b2013-06-28 13:11:22 -04001716 else if (found_key->type == BTRFS_EXTENT_ITEM_KEY)
1717 size = found_key->offset;
1718
Josef Bacik580f0a62014-01-23 16:03:45 -05001719 if (found_key->objectid > logical ||
Josef Bacik261c84b2013-06-28 13:11:22 -04001720 found_key->objectid + size <= logical) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04001721 btrfs_debug(fs_info,
1722 "logical %llu is not within any extent", logical);
Jan Schmidta542ad12011-06-13 19:52:59 +02001723 return -ENOENT;
Jan Schmidt4692cf52011-12-02 14:56:41 +01001724 }
Jan Schmidta542ad12011-06-13 19:52:59 +02001725
1726 eb = path->nodes[0];
1727 item_size = btrfs_item_size_nr(eb, path->slots[0]);
1728 BUG_ON(item_size < sizeof(*ei));
1729
1730 ei = btrfs_item_ptr(eb, path->slots[0], struct btrfs_extent_item);
1731 flags = btrfs_extent_flags(eb, ei);
1732
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04001733 btrfs_debug(fs_info,
1734 "logical %llu is at position %llu within the extent (%llu EXTENT_ITEM %llu) flags %#llx size %u",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001735 logical, logical - found_key->objectid, found_key->objectid,
1736 found_key->offset, flags, item_size);
Liu Bo69917e42012-09-07 20:01:28 -06001737
1738 WARN_ON(!flags_ret);
1739 if (flags_ret) {
1740 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)
1741 *flags_ret = BTRFS_EXTENT_FLAG_TREE_BLOCK;
1742 else if (flags & BTRFS_EXTENT_FLAG_DATA)
1743 *flags_ret = BTRFS_EXTENT_FLAG_DATA;
1744 else
1745 BUG_ON(1);
1746 return 0;
1747 }
Jan Schmidta542ad12011-06-13 19:52:59 +02001748
1749 return -EIO;
1750}
1751
1752/*
1753 * helper function to iterate extent inline refs. ptr must point to a 0 value
1754 * for the first call and may be modified. it is used to track state.
1755 * if more refs exist, 0 is returned and the next call to
Jeff Mahoneye0c476b2017-06-28 21:56:57 -06001756 * get_extent_inline_ref must pass the modified ptr parameter to get the
Jan Schmidta542ad12011-06-13 19:52:59 +02001757 * next ref. after the last ref was processed, 1 is returned.
1758 * returns <0 on error
1759 */
Jeff Mahoneye0c476b2017-06-28 21:56:57 -06001760static int get_extent_inline_ref(unsigned long *ptr,
1761 const struct extent_buffer *eb,
1762 const struct btrfs_key *key,
1763 const struct btrfs_extent_item *ei,
1764 u32 item_size,
1765 struct btrfs_extent_inline_ref **out_eiref,
1766 int *out_type)
Jan Schmidta542ad12011-06-13 19:52:59 +02001767{
1768 unsigned long end;
1769 u64 flags;
1770 struct btrfs_tree_block_info *info;
1771
1772 if (!*ptr) {
1773 /* first call */
1774 flags = btrfs_extent_flags(eb, ei);
1775 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
Liu Bo6eda71d2014-06-09 10:54:07 +08001776 if (key->type == BTRFS_METADATA_ITEM_KEY) {
1777 /* a skinny metadata extent */
1778 *out_eiref =
1779 (struct btrfs_extent_inline_ref *)(ei + 1);
1780 } else {
1781 WARN_ON(key->type != BTRFS_EXTENT_ITEM_KEY);
1782 info = (struct btrfs_tree_block_info *)(ei + 1);
1783 *out_eiref =
1784 (struct btrfs_extent_inline_ref *)(info + 1);
1785 }
Jan Schmidta542ad12011-06-13 19:52:59 +02001786 } else {
1787 *out_eiref = (struct btrfs_extent_inline_ref *)(ei + 1);
1788 }
1789 *ptr = (unsigned long)*out_eiref;
Liu Bocd857dd2014-06-08 19:04:13 +08001790 if ((unsigned long)(*ptr) >= (unsigned long)ei + item_size)
Jan Schmidta542ad12011-06-13 19:52:59 +02001791 return -ENOENT;
1792 }
1793
1794 end = (unsigned long)ei + item_size;
Liu Bo6eda71d2014-06-09 10:54:07 +08001795 *out_eiref = (struct btrfs_extent_inline_ref *)(*ptr);
Liu Bo3de28d52017-08-18 15:15:19 -06001796 *out_type = btrfs_get_extent_inline_ref_type(eb, *out_eiref,
1797 BTRFS_REF_TYPE_ANY);
1798 if (*out_type == BTRFS_REF_TYPE_INVALID)
1799 return -EINVAL;
Jan Schmidta542ad12011-06-13 19:52:59 +02001800
1801 *ptr += btrfs_extent_inline_ref_size(*out_type);
1802 WARN_ON(*ptr > end);
1803 if (*ptr == end)
1804 return 1; /* last */
1805
1806 return 0;
1807}
1808
1809/*
1810 * reads the tree block backref for an extent. tree level and root are returned
1811 * through out_level and out_root. ptr must point to a 0 value for the first
Jeff Mahoneye0c476b2017-06-28 21:56:57 -06001812 * call and may be modified (see get_extent_inline_ref comment).
Jan Schmidta542ad12011-06-13 19:52:59 +02001813 * returns 0 if data was provided, 1 if there was no more data to provide or
1814 * <0 on error.
1815 */
1816int tree_backref_for_extent(unsigned long *ptr, struct extent_buffer *eb,
Liu Bo6eda71d2014-06-09 10:54:07 +08001817 struct btrfs_key *key, struct btrfs_extent_item *ei,
1818 u32 item_size, u64 *out_root, u8 *out_level)
Jan Schmidta542ad12011-06-13 19:52:59 +02001819{
1820 int ret;
1821 int type;
Jan Schmidta542ad12011-06-13 19:52:59 +02001822 struct btrfs_extent_inline_ref *eiref;
1823
1824 if (*ptr == (unsigned long)-1)
1825 return 1;
1826
1827 while (1) {
Jeff Mahoneye0c476b2017-06-28 21:56:57 -06001828 ret = get_extent_inline_ref(ptr, eb, key, ei, item_size,
Liu Bo6eda71d2014-06-09 10:54:07 +08001829 &eiref, &type);
Jan Schmidta542ad12011-06-13 19:52:59 +02001830 if (ret < 0)
1831 return ret;
1832
1833 if (type == BTRFS_TREE_BLOCK_REF_KEY ||
1834 type == BTRFS_SHARED_BLOCK_REF_KEY)
1835 break;
1836
1837 if (ret == 1)
1838 return 1;
1839 }
1840
1841 /* we can treat both ref types equally here */
Jan Schmidta542ad12011-06-13 19:52:59 +02001842 *out_root = btrfs_extent_inline_ref_offset(eb, eiref);
Filipe Mananaa1317f42014-12-15 16:04:42 +00001843
1844 if (key->type == BTRFS_EXTENT_ITEM_KEY) {
1845 struct btrfs_tree_block_info *info;
1846
1847 info = (struct btrfs_tree_block_info *)(ei + 1);
1848 *out_level = btrfs_tree_block_level(eb, info);
1849 } else {
1850 ASSERT(key->type == BTRFS_METADATA_ITEM_KEY);
1851 *out_level = (u8)key->offset;
1852 }
Jan Schmidta542ad12011-06-13 19:52:59 +02001853
1854 if (ret == 1)
1855 *ptr = (unsigned long)-1;
1856
1857 return 0;
1858}
1859
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04001860static int iterate_leaf_refs(struct btrfs_fs_info *fs_info,
1861 struct extent_inode_elem *inode_list,
1862 u64 root, u64 extent_item_objectid,
1863 iterate_extent_inodes_t *iterate, void *ctx)
Jan Schmidta542ad12011-06-13 19:52:59 +02001864{
Jan Schmidt976b1902012-05-17 16:43:03 +02001865 struct extent_inode_elem *eie;
Jan Schmidt4692cf52011-12-02 14:56:41 +01001866 int ret = 0;
Jan Schmidta542ad12011-06-13 19:52:59 +02001867
Jan Schmidt976b1902012-05-17 16:43:03 +02001868 for (eie = inode_list; eie; eie = eie->next) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04001869 btrfs_debug(fs_info,
1870 "ref for %llu resolved, key (%llu EXTEND_DATA %llu), root %llu",
1871 extent_item_objectid, eie->inum,
1872 eie->offset, root);
Jan Schmidt976b1902012-05-17 16:43:03 +02001873 ret = iterate(eie->inum, eie->offset, root, ctx);
Jan Schmidt4692cf52011-12-02 14:56:41 +01001874 if (ret) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04001875 btrfs_debug(fs_info,
1876 "stopping iteration for %llu due to ret=%d",
1877 extent_item_objectid, ret);
Jan Schmidt4692cf52011-12-02 14:56:41 +01001878 break;
1879 }
Jan Schmidta542ad12011-06-13 19:52:59 +02001880 }
1881
Jan Schmidta542ad12011-06-13 19:52:59 +02001882 return ret;
1883}
1884
1885/*
1886 * calls iterate() for every inode that references the extent identified by
Jan Schmidt4692cf52011-12-02 14:56:41 +01001887 * the given parameters.
Jan Schmidta542ad12011-06-13 19:52:59 +02001888 * when the iterator function returns a non-zero value, iteration stops.
1889 */
1890int iterate_extent_inodes(struct btrfs_fs_info *fs_info,
Jan Schmidt4692cf52011-12-02 14:56:41 +01001891 u64 extent_item_objectid, u64 extent_item_pos,
Jan Schmidt7a3ae2f2012-03-23 17:32:28 +01001892 int search_commit_root,
Zygo Blaxellc995ab32017-09-22 13:58:45 -04001893 iterate_extent_inodes_t *iterate, void *ctx,
1894 bool ignore_offset)
Jan Schmidta542ad12011-06-13 19:52:59 +02001895{
Jan Schmidta542ad12011-06-13 19:52:59 +02001896 int ret;
Josef Bacikda61d312013-06-12 16:20:08 -04001897 struct btrfs_trans_handle *trans = NULL;
Jan Schmidt7a3ae2f2012-03-23 17:32:28 +01001898 struct ulist *refs = NULL;
1899 struct ulist *roots = NULL;
Jan Schmidt4692cf52011-12-02 14:56:41 +01001900 struct ulist_node *ref_node = NULL;
1901 struct ulist_node *root_node = NULL;
David Sterba3284da72015-02-25 15:47:32 +01001902 struct seq_list tree_mod_seq_elem = SEQ_LIST_INIT(tree_mod_seq_elem);
Jan Schmidtcd1b4132012-05-22 14:56:50 +02001903 struct ulist_iterator ref_uiter;
1904 struct ulist_iterator root_uiter;
Jan Schmidta542ad12011-06-13 19:52:59 +02001905
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04001906 btrfs_debug(fs_info, "resolving all inodes for extent %llu",
Jan Schmidt4692cf52011-12-02 14:56:41 +01001907 extent_item_objectid);
1908
Josef Bacikda61d312013-06-12 16:20:08 -04001909 if (!search_commit_root) {
Jan Schmidt7a3ae2f2012-03-23 17:32:28 +01001910 trans = btrfs_join_transaction(fs_info->extent_root);
1911 if (IS_ERR(trans))
1912 return PTR_ERR(trans);
Jan Schmidt8445f612012-05-16 18:36:03 +02001913 btrfs_get_tree_mod_seq(fs_info, &tree_mod_seq_elem);
Josef Bacik9e351cc2014-03-13 15:42:13 -04001914 } else {
1915 down_read(&fs_info->commit_root_sem);
Jan Schmidt7a3ae2f2012-03-23 17:32:28 +01001916 }
Jan Schmidt4692cf52011-12-02 14:56:41 +01001917
1918 ret = btrfs_find_all_leafs(trans, fs_info, extent_item_objectid,
Jan Schmidt097b8a72012-06-21 11:08:04 +02001919 tree_mod_seq_elem.seq, &refs,
Zygo Blaxellc995ab32017-09-22 13:58:45 -04001920 &extent_item_pos, ignore_offset);
Jan Schmidt4692cf52011-12-02 14:56:41 +01001921 if (ret)
1922 goto out;
1923
Jan Schmidtcd1b4132012-05-22 14:56:50 +02001924 ULIST_ITER_INIT(&ref_uiter);
1925 while (!ret && (ref_node = ulist_next(refs, &ref_uiter))) {
Jeff Mahoneye0c476b2017-06-28 21:56:57 -06001926 ret = btrfs_find_all_roots_safe(trans, fs_info, ref_node->val,
Zygo Blaxellc995ab32017-09-22 13:58:45 -04001927 tree_mod_seq_elem.seq, &roots,
1928 ignore_offset);
Jan Schmidt4692cf52011-12-02 14:56:41 +01001929 if (ret)
Jan Schmidta542ad12011-06-13 19:52:59 +02001930 break;
Jan Schmidtcd1b4132012-05-22 14:56:50 +02001931 ULIST_ITER_INIT(&root_uiter);
1932 while (!ret && (root_node = ulist_next(roots, &root_uiter))) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04001933 btrfs_debug(fs_info,
1934 "root %llu references leaf %llu, data list %#llx",
1935 root_node->val, ref_node->val,
1936 ref_node->aux);
1937 ret = iterate_leaf_refs(fs_info,
1938 (struct extent_inode_elem *)
Jan Schmidt995e01b2012-08-13 02:52:38 -06001939 (uintptr_t)ref_node->aux,
1940 root_node->val,
1941 extent_item_objectid,
1942 iterate, ctx);
Jan Schmidta542ad12011-06-13 19:52:59 +02001943 }
Jan Schmidt976b1902012-05-17 16:43:03 +02001944 ulist_free(roots);
Jan Schmidta542ad12011-06-13 19:52:59 +02001945 }
1946
Jan Schmidt976b1902012-05-17 16:43:03 +02001947 free_leaf_list(refs);
Jan Schmidt4692cf52011-12-02 14:56:41 +01001948out:
Jan Schmidt7a3ae2f2012-03-23 17:32:28 +01001949 if (!search_commit_root) {
Jan Schmidt8445f612012-05-16 18:36:03 +02001950 btrfs_put_tree_mod_seq(fs_info, &tree_mod_seq_elem);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04001951 btrfs_end_transaction(trans);
Josef Bacik9e351cc2014-03-13 15:42:13 -04001952 } else {
1953 up_read(&fs_info->commit_root_sem);
Jan Schmidt7a3ae2f2012-03-23 17:32:28 +01001954 }
1955
Jan Schmidta542ad12011-06-13 19:52:59 +02001956 return ret;
1957}
1958
1959int iterate_inodes_from_logical(u64 logical, struct btrfs_fs_info *fs_info,
1960 struct btrfs_path *path,
Zygo Blaxellc995ab32017-09-22 13:58:45 -04001961 iterate_extent_inodes_t *iterate, void *ctx,
1962 bool ignore_offset)
Jan Schmidta542ad12011-06-13 19:52:59 +02001963{
1964 int ret;
Jan Schmidt4692cf52011-12-02 14:56:41 +01001965 u64 extent_item_pos;
Liu Bo69917e42012-09-07 20:01:28 -06001966 u64 flags = 0;
Jan Schmidta542ad12011-06-13 19:52:59 +02001967 struct btrfs_key found_key;
Jan Schmidt7a3ae2f2012-03-23 17:32:28 +01001968 int search_commit_root = path->search_commit_root;
Jan Schmidta542ad12011-06-13 19:52:59 +02001969
Liu Bo69917e42012-09-07 20:01:28 -06001970 ret = extent_from_logical(fs_info, logical, path, &found_key, &flags);
Jan Schmidt4692cf52011-12-02 14:56:41 +01001971 btrfs_release_path(path);
Jan Schmidta542ad12011-06-13 19:52:59 +02001972 if (ret < 0)
1973 return ret;
Liu Bo69917e42012-09-07 20:01:28 -06001974 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)
Stefan Behrens3627bf42012-08-01 04:28:01 -06001975 return -EINVAL;
Jan Schmidta542ad12011-06-13 19:52:59 +02001976
Jan Schmidt4692cf52011-12-02 14:56:41 +01001977 extent_item_pos = logical - found_key.objectid;
Jan Schmidt7a3ae2f2012-03-23 17:32:28 +01001978 ret = iterate_extent_inodes(fs_info, found_key.objectid,
1979 extent_item_pos, search_commit_root,
Zygo Blaxellc995ab32017-09-22 13:58:45 -04001980 iterate, ctx, ignore_offset);
Jan Schmidta542ad12011-06-13 19:52:59 +02001981
1982 return ret;
1983}
1984
Mark Fashehd24bec32012-08-08 11:33:54 -07001985typedef int (iterate_irefs_t)(u64 parent, u32 name_len, unsigned long name_off,
1986 struct extent_buffer *eb, void *ctx);
1987
1988static int iterate_inode_refs(u64 inum, struct btrfs_root *fs_root,
1989 struct btrfs_path *path,
1990 iterate_irefs_t *iterate, void *ctx)
Jan Schmidta542ad12011-06-13 19:52:59 +02001991{
Jan Schmidtaefc1eb2012-04-13 12:28:00 +02001992 int ret = 0;
Jan Schmidta542ad12011-06-13 19:52:59 +02001993 int slot;
1994 u32 cur;
1995 u32 len;
1996 u32 name_len;
1997 u64 parent = 0;
1998 int found = 0;
1999 struct extent_buffer *eb;
2000 struct btrfs_item *item;
2001 struct btrfs_inode_ref *iref;
2002 struct btrfs_key found_key;
2003
Jan Schmidtaefc1eb2012-04-13 12:28:00 +02002004 while (!ret) {
David Sterbac234a242015-01-02 19:03:17 +01002005 ret = btrfs_find_item(fs_root, path, inum,
2006 parent ? parent + 1 : 0, BTRFS_INODE_REF_KEY,
2007 &found_key);
2008
Jan Schmidta542ad12011-06-13 19:52:59 +02002009 if (ret < 0)
2010 break;
2011 if (ret) {
2012 ret = found ? 0 : -ENOENT;
2013 break;
2014 }
2015 ++found;
2016
2017 parent = found_key.offset;
2018 slot = path->slots[0];
Filipe David Borba Manana3fe81ce2013-12-15 12:43:58 +00002019 eb = btrfs_clone_extent_buffer(path->nodes[0]);
2020 if (!eb) {
2021 ret = -ENOMEM;
2022 break;
2023 }
2024 extent_buffer_get(eb);
Jan Schmidtb916a592012-04-13 12:28:08 +02002025 btrfs_tree_read_lock(eb);
2026 btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK);
Jan Schmidta542ad12011-06-13 19:52:59 +02002027 btrfs_release_path(path);
2028
Ross Kirkdd3cc162013-09-16 15:58:09 +01002029 item = btrfs_item_nr(slot);
Jan Schmidta542ad12011-06-13 19:52:59 +02002030 iref = btrfs_item_ptr(eb, slot, struct btrfs_inode_ref);
2031
2032 for (cur = 0; cur < btrfs_item_size(eb, item); cur += len) {
2033 name_len = btrfs_inode_ref_name_len(eb, iref);
2034 /* path must be released before calling iterate()! */
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002035 btrfs_debug(fs_root->fs_info,
2036 "following ref at offset %u for inode %llu in tree %llu",
2037 cur, found_key.objectid, fs_root->objectid);
Mark Fashehd24bec32012-08-08 11:33:54 -07002038 ret = iterate(parent, name_len,
2039 (unsigned long)(iref + 1), eb, ctx);
Jan Schmidtaefc1eb2012-04-13 12:28:00 +02002040 if (ret)
Jan Schmidta542ad12011-06-13 19:52:59 +02002041 break;
Jan Schmidta542ad12011-06-13 19:52:59 +02002042 len = sizeof(*iref) + name_len;
2043 iref = (struct btrfs_inode_ref *)((char *)iref + len);
2044 }
Jan Schmidtb916a592012-04-13 12:28:08 +02002045 btrfs_tree_read_unlock_blocking(eb);
Jan Schmidta542ad12011-06-13 19:52:59 +02002046 free_extent_buffer(eb);
2047 }
2048
2049 btrfs_release_path(path);
2050
2051 return ret;
2052}
2053
Mark Fashehd24bec32012-08-08 11:33:54 -07002054static int iterate_inode_extrefs(u64 inum, struct btrfs_root *fs_root,
2055 struct btrfs_path *path,
2056 iterate_irefs_t *iterate, void *ctx)
2057{
2058 int ret;
2059 int slot;
2060 u64 offset = 0;
2061 u64 parent;
2062 int found = 0;
2063 struct extent_buffer *eb;
2064 struct btrfs_inode_extref *extref;
Mark Fashehd24bec32012-08-08 11:33:54 -07002065 u32 item_size;
2066 u32 cur_offset;
2067 unsigned long ptr;
2068
2069 while (1) {
2070 ret = btrfs_find_one_extref(fs_root, inum, offset, path, &extref,
2071 &offset);
2072 if (ret < 0)
2073 break;
2074 if (ret) {
2075 ret = found ? 0 : -ENOENT;
2076 break;
2077 }
2078 ++found;
2079
2080 slot = path->slots[0];
Filipe David Borba Manana3fe81ce2013-12-15 12:43:58 +00002081 eb = btrfs_clone_extent_buffer(path->nodes[0]);
2082 if (!eb) {
2083 ret = -ENOMEM;
2084 break;
2085 }
2086 extent_buffer_get(eb);
Mark Fashehd24bec32012-08-08 11:33:54 -07002087
2088 btrfs_tree_read_lock(eb);
2089 btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK);
2090 btrfs_release_path(path);
2091
Chris Mason2849a852015-10-13 14:06:48 -04002092 item_size = btrfs_item_size_nr(eb, slot);
2093 ptr = btrfs_item_ptr_offset(eb, slot);
Mark Fashehd24bec32012-08-08 11:33:54 -07002094 cur_offset = 0;
2095
2096 while (cur_offset < item_size) {
2097 u32 name_len;
2098
2099 extref = (struct btrfs_inode_extref *)(ptr + cur_offset);
2100 parent = btrfs_inode_extref_parent(eb, extref);
2101 name_len = btrfs_inode_extref_name_len(eb, extref);
2102 ret = iterate(parent, name_len,
2103 (unsigned long)&extref->name, eb, ctx);
2104 if (ret)
2105 break;
2106
Chris Mason2849a852015-10-13 14:06:48 -04002107 cur_offset += btrfs_inode_extref_name_len(eb, extref);
Mark Fashehd24bec32012-08-08 11:33:54 -07002108 cur_offset += sizeof(*extref);
2109 }
2110 btrfs_tree_read_unlock_blocking(eb);
2111 free_extent_buffer(eb);
2112
2113 offset++;
2114 }
2115
2116 btrfs_release_path(path);
2117
2118 return ret;
2119}
2120
2121static int iterate_irefs(u64 inum, struct btrfs_root *fs_root,
2122 struct btrfs_path *path, iterate_irefs_t *iterate,
2123 void *ctx)
2124{
2125 int ret;
2126 int found_refs = 0;
2127
2128 ret = iterate_inode_refs(inum, fs_root, path, iterate, ctx);
2129 if (!ret)
2130 ++found_refs;
2131 else if (ret != -ENOENT)
2132 return ret;
2133
2134 ret = iterate_inode_extrefs(inum, fs_root, path, iterate, ctx);
2135 if (ret == -ENOENT && found_refs)
2136 return 0;
2137
2138 return ret;
2139}
2140
Jan Schmidta542ad12011-06-13 19:52:59 +02002141/*
2142 * returns 0 if the path could be dumped (probably truncated)
2143 * returns <0 in case of an error
2144 */
Mark Fashehd24bec32012-08-08 11:33:54 -07002145static int inode_to_path(u64 inum, u32 name_len, unsigned long name_off,
2146 struct extent_buffer *eb, void *ctx)
Jan Schmidta542ad12011-06-13 19:52:59 +02002147{
2148 struct inode_fs_paths *ipath = ctx;
2149 char *fspath;
2150 char *fspath_min;
2151 int i = ipath->fspath->elem_cnt;
2152 const int s_ptr = sizeof(char *);
2153 u32 bytes_left;
2154
2155 bytes_left = ipath->fspath->bytes_left > s_ptr ?
2156 ipath->fspath->bytes_left - s_ptr : 0;
2157
Chris Mason740c3d22011-11-02 15:48:34 -04002158 fspath_min = (char *)ipath->fspath->val + (i + 1) * s_ptr;
Jan Schmidt96b5bd72012-10-15 08:30:45 +00002159 fspath = btrfs_ref_to_path(ipath->fs_root, ipath->btrfs_path, name_len,
2160 name_off, eb, inum, fspath_min, bytes_left);
Jan Schmidta542ad12011-06-13 19:52:59 +02002161 if (IS_ERR(fspath))
2162 return PTR_ERR(fspath);
2163
2164 if (fspath > fspath_min) {
Jeff Mahoney745c4d82011-11-20 07:31:57 -05002165 ipath->fspath->val[i] = (u64)(unsigned long)fspath;
Jan Schmidta542ad12011-06-13 19:52:59 +02002166 ++ipath->fspath->elem_cnt;
2167 ipath->fspath->bytes_left = fspath - fspath_min;
2168 } else {
2169 ++ipath->fspath->elem_missed;
2170 ipath->fspath->bytes_missing += fspath_min - fspath;
2171 ipath->fspath->bytes_left = 0;
2172 }
2173
2174 return 0;
2175}
2176
2177/*
2178 * this dumps all file system paths to the inode into the ipath struct, provided
2179 * is has been created large enough. each path is zero-terminated and accessed
Chris Mason740c3d22011-11-02 15:48:34 -04002180 * from ipath->fspath->val[i].
Jan Schmidta542ad12011-06-13 19:52:59 +02002181 * when it returns, there are ipath->fspath->elem_cnt number of paths available
Chris Mason740c3d22011-11-02 15:48:34 -04002182 * in ipath->fspath->val[]. when the allocated space wasn't sufficient, the
Nicholas D Steeves01327612016-05-19 21:18:45 -04002183 * number of missed paths is recorded in ipath->fspath->elem_missed, otherwise,
Jan Schmidta542ad12011-06-13 19:52:59 +02002184 * it's zero. ipath->fspath->bytes_missing holds the number of bytes that would
2185 * have been needed to return all paths.
2186 */
2187int paths_from_inode(u64 inum, struct inode_fs_paths *ipath)
2188{
2189 return iterate_irefs(inum, ipath->fs_root, ipath->btrfs_path,
Mark Fashehd24bec32012-08-08 11:33:54 -07002190 inode_to_path, ipath);
Jan Schmidta542ad12011-06-13 19:52:59 +02002191}
2192
Jan Schmidta542ad12011-06-13 19:52:59 +02002193struct btrfs_data_container *init_data_container(u32 total_bytes)
2194{
2195 struct btrfs_data_container *data;
2196 size_t alloc_bytes;
2197
2198 alloc_bytes = max_t(size_t, total_bytes, sizeof(*data));
David Sterbaf54de062017-05-31 19:32:09 +02002199 data = kvmalloc(alloc_bytes, GFP_KERNEL);
Jan Schmidta542ad12011-06-13 19:52:59 +02002200 if (!data)
2201 return ERR_PTR(-ENOMEM);
2202
2203 if (total_bytes >= sizeof(*data)) {
2204 data->bytes_left = total_bytes - sizeof(*data);
2205 data->bytes_missing = 0;
2206 } else {
2207 data->bytes_missing = sizeof(*data) - total_bytes;
2208 data->bytes_left = 0;
2209 }
2210
2211 data->elem_cnt = 0;
2212 data->elem_missed = 0;
2213
2214 return data;
2215}
2216
2217/*
2218 * allocates space to return multiple file system paths for an inode.
2219 * total_bytes to allocate are passed, note that space usable for actual path
2220 * information will be total_bytes - sizeof(struct inode_fs_paths).
2221 * the returned pointer must be freed with free_ipath() in the end.
2222 */
2223struct inode_fs_paths *init_ipath(s32 total_bytes, struct btrfs_root *fs_root,
2224 struct btrfs_path *path)
2225{
2226 struct inode_fs_paths *ifp;
2227 struct btrfs_data_container *fspath;
2228
2229 fspath = init_data_container(total_bytes);
2230 if (IS_ERR(fspath))
2231 return (void *)fspath;
2232
David Sterbaf54de062017-05-31 19:32:09 +02002233 ifp = kmalloc(sizeof(*ifp), GFP_KERNEL);
Jan Schmidta542ad12011-06-13 19:52:59 +02002234 if (!ifp) {
David Sterbaf54de062017-05-31 19:32:09 +02002235 kvfree(fspath);
Jan Schmidta542ad12011-06-13 19:52:59 +02002236 return ERR_PTR(-ENOMEM);
2237 }
2238
2239 ifp->btrfs_path = path;
2240 ifp->fspath = fspath;
2241 ifp->fs_root = fs_root;
2242
2243 return ifp;
2244}
2245
2246void free_ipath(struct inode_fs_paths *ipath)
2247{
Jesper Juhl4735fb22012-04-12 22:47:52 +02002248 if (!ipath)
2249 return;
David Sterbaf54de062017-05-31 19:32:09 +02002250 kvfree(ipath->fspath);
Jan Schmidta542ad12011-06-13 19:52:59 +02002251 kfree(ipath);
2252}