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