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" |
1b60d2ec | 16 | #include "misc.h" |
f3a84ccd | 17 | #include "tree-mod-log.h" |
a542ad1b | 18 | |
dc046b10 JB |
19 | /* Just an arbitrary number so we can be sure this happened */ |
20 | #define BACKREF_FOUND_SHARED 6 | |
21 | ||
976b1908 JS |
22 | struct extent_inode_elem { |
23 | u64 inum; | |
24 | u64 offset; | |
25 | struct extent_inode_elem *next; | |
26 | }; | |
27 | ||
73980bec JM |
28 | static int check_extent_in_eb(const struct btrfs_key *key, |
29 | const struct extent_buffer *eb, | |
30 | const struct btrfs_file_extent_item *fi, | |
31 | u64 extent_item_pos, | |
c995ab3c ZB |
32 | struct extent_inode_elem **eie, |
33 | bool ignore_offset) | |
976b1908 | 34 | { |
8ca15e05 | 35 | u64 offset = 0; |
976b1908 JS |
36 | struct extent_inode_elem *e; |
37 | ||
c995ab3c ZB |
38 | if (!ignore_offset && |
39 | !btrfs_file_extent_compression(eb, fi) && | |
8ca15e05 JB |
40 | !btrfs_file_extent_encryption(eb, fi) && |
41 | !btrfs_file_extent_other_encoding(eb, fi)) { | |
42 | u64 data_offset; | |
43 | u64 data_len; | |
976b1908 | 44 | |
8ca15e05 JB |
45 | data_offset = btrfs_file_extent_offset(eb, fi); |
46 | data_len = btrfs_file_extent_num_bytes(eb, fi); | |
47 | ||
48 | if (extent_item_pos < data_offset || | |
49 | extent_item_pos >= data_offset + data_len) | |
50 | return 1; | |
51 | offset = extent_item_pos - data_offset; | |
52 | } | |
976b1908 JS |
53 | |
54 | e = kmalloc(sizeof(*e), GFP_NOFS); | |
55 | if (!e) | |
56 | return -ENOMEM; | |
57 | ||
58 | e->next = *eie; | |
59 | e->inum = key->objectid; | |
8ca15e05 | 60 | e->offset = key->offset + offset; |
976b1908 JS |
61 | *eie = e; |
62 | ||
63 | return 0; | |
64 | } | |
65 | ||
f05c4746 WS |
66 | static void free_inode_elem_list(struct extent_inode_elem *eie) |
67 | { | |
68 | struct extent_inode_elem *eie_next; | |
69 | ||
70 | for (; eie; eie = eie_next) { | |
71 | eie_next = eie->next; | |
72 | kfree(eie); | |
73 | } | |
74 | } | |
75 | ||
73980bec JM |
76 | static int find_extent_in_eb(const struct extent_buffer *eb, |
77 | u64 wanted_disk_byte, u64 extent_item_pos, | |
c995ab3c ZB |
78 | struct extent_inode_elem **eie, |
79 | bool ignore_offset) | |
976b1908 JS |
80 | { |
81 | u64 disk_byte; | |
82 | struct btrfs_key key; | |
83 | struct btrfs_file_extent_item *fi; | |
84 | int slot; | |
85 | int nritems; | |
86 | int extent_type; | |
87 | int ret; | |
88 | ||
89 | /* | |
90 | * from the shared data ref, we only have the leaf but we need | |
91 | * the key. thus, we must look into all items and see that we | |
92 | * find one (some) with a reference to our extent item. | |
93 | */ | |
94 | nritems = btrfs_header_nritems(eb); | |
95 | for (slot = 0; slot < nritems; ++slot) { | |
96 | btrfs_item_key_to_cpu(eb, &key, slot); | |
97 | if (key.type != BTRFS_EXTENT_DATA_KEY) | |
98 | continue; | |
99 | fi = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item); | |
100 | extent_type = btrfs_file_extent_type(eb, fi); | |
101 | if (extent_type == BTRFS_FILE_EXTENT_INLINE) | |
102 | continue; | |
103 | /* don't skip BTRFS_FILE_EXTENT_PREALLOC, we can handle that */ | |
104 | disk_byte = btrfs_file_extent_disk_bytenr(eb, fi); | |
105 | if (disk_byte != wanted_disk_byte) | |
106 | continue; | |
107 | ||
c995ab3c | 108 | ret = check_extent_in_eb(&key, eb, fi, extent_item_pos, eie, ignore_offset); |
976b1908 JS |
109 | if (ret < 0) |
110 | return ret; | |
111 | } | |
112 | ||
113 | return 0; | |
114 | } | |
115 | ||
86d5f994 | 116 | struct preftree { |
ecf160b4 | 117 | struct rb_root_cached root; |
6c336b21 | 118 | unsigned int count; |
86d5f994 EN |
119 | }; |
120 | ||
ecf160b4 | 121 | #define PREFTREE_INIT { .root = RB_ROOT_CACHED, .count = 0 } |
86d5f994 EN |
122 | |
123 | struct preftrees { | |
124 | struct preftree direct; /* BTRFS_SHARED_[DATA|BLOCK]_REF_KEY */ | |
125 | struct preftree indirect; /* BTRFS_[TREE_BLOCK|EXTENT_DATA]_REF_KEY */ | |
126 | struct preftree indirect_missing_keys; | |
127 | }; | |
128 | ||
3ec4d323 EN |
129 | /* |
130 | * Checks for a shared extent during backref search. | |
131 | * | |
132 | * The share_count tracks prelim_refs (direct and indirect) having a | |
133 | * ref->count >0: | |
134 | * - incremented when a ref->count transitions to >0 | |
135 | * - decremented when a ref->count transitions to <1 | |
136 | */ | |
137 | struct share_check { | |
138 | u64 root_objectid; | |
139 | u64 inum; | |
140 | int share_count; | |
4fc7b572 | 141 | bool have_delayed_delete_refs; |
3ec4d323 EN |
142 | }; |
143 | ||
144 | static inline int extent_is_shared(struct share_check *sc) | |
145 | { | |
146 | return (sc && sc->share_count > 1) ? BACKREF_FOUND_SHARED : 0; | |
147 | } | |
148 | ||
b9e9a6cb WS |
149 | static struct kmem_cache *btrfs_prelim_ref_cache; |
150 | ||
151 | int __init btrfs_prelim_ref_init(void) | |
152 | { | |
153 | btrfs_prelim_ref_cache = kmem_cache_create("btrfs_prelim_ref", | |
e0c476b1 | 154 | sizeof(struct prelim_ref), |
b9e9a6cb | 155 | 0, |
fba4b697 | 156 | SLAB_MEM_SPREAD, |
b9e9a6cb WS |
157 | NULL); |
158 | if (!btrfs_prelim_ref_cache) | |
159 | return -ENOMEM; | |
160 | return 0; | |
161 | } | |
162 | ||
e67c718b | 163 | void __cold btrfs_prelim_ref_exit(void) |
b9e9a6cb | 164 | { |
5598e900 | 165 | kmem_cache_destroy(btrfs_prelim_ref_cache); |
b9e9a6cb WS |
166 | } |
167 | ||
86d5f994 EN |
168 | static void free_pref(struct prelim_ref *ref) |
169 | { | |
170 | kmem_cache_free(btrfs_prelim_ref_cache, ref); | |
171 | } | |
172 | ||
173 | /* | |
174 | * Return 0 when both refs are for the same block (and can be merged). | |
175 | * A -1 return indicates ref1 is a 'lower' block than ref2, while 1 | |
176 | * indicates a 'higher' block. | |
177 | */ | |
178 | static int prelim_ref_compare(struct prelim_ref *ref1, | |
179 | struct prelim_ref *ref2) | |
180 | { | |
181 | if (ref1->level < ref2->level) | |
182 | return -1; | |
183 | if (ref1->level > ref2->level) | |
184 | return 1; | |
185 | if (ref1->root_id < ref2->root_id) | |
186 | return -1; | |
187 | if (ref1->root_id > ref2->root_id) | |
188 | return 1; | |
189 | if (ref1->key_for_search.type < ref2->key_for_search.type) | |
190 | return -1; | |
191 | if (ref1->key_for_search.type > ref2->key_for_search.type) | |
192 | return 1; | |
193 | if (ref1->key_for_search.objectid < ref2->key_for_search.objectid) | |
194 | return -1; | |
195 | if (ref1->key_for_search.objectid > ref2->key_for_search.objectid) | |
196 | return 1; | |
197 | if (ref1->key_for_search.offset < ref2->key_for_search.offset) | |
198 | return -1; | |
199 | if (ref1->key_for_search.offset > ref2->key_for_search.offset) | |
200 | return 1; | |
201 | if (ref1->parent < ref2->parent) | |
202 | return -1; | |
203 | if (ref1->parent > ref2->parent) | |
204 | return 1; | |
205 | ||
206 | return 0; | |
207 | } | |
208 | ||
ccc8dc75 CIK |
209 | static void update_share_count(struct share_check *sc, int oldcount, |
210 | int newcount) | |
3ec4d323 EN |
211 | { |
212 | if ((!sc) || (oldcount == 0 && newcount < 1)) | |
213 | return; | |
214 | ||
215 | if (oldcount > 0 && newcount < 1) | |
216 | sc->share_count--; | |
217 | else if (oldcount < 1 && newcount > 0) | |
218 | sc->share_count++; | |
219 | } | |
220 | ||
86d5f994 EN |
221 | /* |
222 | * Add @newref to the @root rbtree, merging identical refs. | |
223 | * | |
3ec4d323 | 224 | * Callers should assume that newref has been freed after calling. |
86d5f994 | 225 | */ |
00142756 JM |
226 | static void prelim_ref_insert(const struct btrfs_fs_info *fs_info, |
227 | struct preftree *preftree, | |
3ec4d323 EN |
228 | struct prelim_ref *newref, |
229 | struct share_check *sc) | |
86d5f994 | 230 | { |
ecf160b4 | 231 | struct rb_root_cached *root; |
86d5f994 EN |
232 | struct rb_node **p; |
233 | struct rb_node *parent = NULL; | |
234 | struct prelim_ref *ref; | |
235 | int result; | |
ecf160b4 | 236 | bool leftmost = true; |
86d5f994 EN |
237 | |
238 | root = &preftree->root; | |
ecf160b4 | 239 | p = &root->rb_root.rb_node; |
86d5f994 EN |
240 | |
241 | while (*p) { | |
242 | parent = *p; | |
243 | ref = rb_entry(parent, struct prelim_ref, rbnode); | |
244 | result = prelim_ref_compare(ref, newref); | |
245 | if (result < 0) { | |
246 | p = &(*p)->rb_left; | |
247 | } else if (result > 0) { | |
248 | p = &(*p)->rb_right; | |
ecf160b4 | 249 | leftmost = false; |
86d5f994 EN |
250 | } else { |
251 | /* Identical refs, merge them and free @newref */ | |
252 | struct extent_inode_elem *eie = ref->inode_list; | |
253 | ||
254 | while (eie && eie->next) | |
255 | eie = eie->next; | |
256 | ||
257 | if (!eie) | |
258 | ref->inode_list = newref->inode_list; | |
259 | else | |
260 | eie->next = newref->inode_list; | |
00142756 JM |
261 | trace_btrfs_prelim_ref_merge(fs_info, ref, newref, |
262 | preftree->count); | |
3ec4d323 EN |
263 | /* |
264 | * A delayed ref can have newref->count < 0. | |
265 | * The ref->count is updated to follow any | |
266 | * BTRFS_[ADD|DROP]_DELAYED_REF actions. | |
267 | */ | |
268 | update_share_count(sc, ref->count, | |
269 | ref->count + newref->count); | |
86d5f994 EN |
270 | ref->count += newref->count; |
271 | free_pref(newref); | |
272 | return; | |
273 | } | |
274 | } | |
275 | ||
3ec4d323 | 276 | update_share_count(sc, 0, newref->count); |
6c336b21 | 277 | preftree->count++; |
00142756 | 278 | trace_btrfs_prelim_ref_insert(fs_info, newref, NULL, preftree->count); |
86d5f994 | 279 | rb_link_node(&newref->rbnode, parent, p); |
ecf160b4 | 280 | rb_insert_color_cached(&newref->rbnode, root, leftmost); |
86d5f994 EN |
281 | } |
282 | ||
283 | /* | |
284 | * Release the entire tree. We don't care about internal consistency so | |
285 | * just free everything and then reset the tree root. | |
286 | */ | |
287 | static void prelim_release(struct preftree *preftree) | |
288 | { | |
289 | struct prelim_ref *ref, *next_ref; | |
290 | ||
ecf160b4 | 291 | rbtree_postorder_for_each_entry_safe(ref, next_ref, |
92876eec FM |
292 | &preftree->root.rb_root, rbnode) { |
293 | free_inode_elem_list(ref->inode_list); | |
86d5f994 | 294 | free_pref(ref); |
92876eec | 295 | } |
86d5f994 | 296 | |
ecf160b4 | 297 | preftree->root = RB_ROOT_CACHED; |
6c336b21 | 298 | preftree->count = 0; |
86d5f994 EN |
299 | } |
300 | ||
d5c88b73 JS |
301 | /* |
302 | * the rules for all callers of this function are: | |
303 | * - obtaining the parent is the goal | |
304 | * - if you add a key, you must know that it is a correct key | |
305 | * - if you cannot add the parent or a correct key, then we will look into the | |
306 | * block later to set a correct key | |
307 | * | |
308 | * delayed refs | |
309 | * ============ | |
310 | * backref type | shared | indirect | shared | indirect | |
311 | * information | tree | tree | data | data | |
312 | * --------------------+--------+----------+--------+---------- | |
313 | * parent logical | y | - | - | - | |
314 | * key to resolve | - | y | y | y | |
315 | * tree block logical | - | - | - | - | |
316 | * root for resolving | y | y | y | y | |
317 | * | |
318 | * - column 1: we've the parent -> done | |
319 | * - column 2, 3, 4: we use the key to find the parent | |
320 | * | |
321 | * on disk refs (inline or keyed) | |
322 | * ============================== | |
323 | * backref type | shared | indirect | shared | indirect | |
324 | * information | tree | tree | data | data | |
325 | * --------------------+--------+----------+--------+---------- | |
326 | * parent logical | y | - | y | - | |
327 | * key to resolve | - | - | - | y | |
328 | * tree block logical | y | y | y | y | |
329 | * root for resolving | - | y | y | y | |
330 | * | |
331 | * - column 1, 3: we've the parent -> done | |
332 | * - column 2: we take the first key from the block to find the parent | |
e0c476b1 | 333 | * (see add_missing_keys) |
d5c88b73 JS |
334 | * - column 4: we use the key to find the parent |
335 | * | |
336 | * additional information that's available but not required to find the parent | |
337 | * block might help in merging entries to gain some speed. | |
338 | */ | |
00142756 JM |
339 | static int add_prelim_ref(const struct btrfs_fs_info *fs_info, |
340 | struct preftree *preftree, u64 root_id, | |
e0c476b1 | 341 | const struct btrfs_key *key, int level, u64 parent, |
3ec4d323 EN |
342 | u64 wanted_disk_byte, int count, |
343 | struct share_check *sc, gfp_t gfp_mask) | |
8da6d581 | 344 | { |
e0c476b1 | 345 | struct prelim_ref *ref; |
8da6d581 | 346 | |
48ec4736 LB |
347 | if (root_id == BTRFS_DATA_RELOC_TREE_OBJECTID) |
348 | return 0; | |
349 | ||
b9e9a6cb | 350 | ref = kmem_cache_alloc(btrfs_prelim_ref_cache, gfp_mask); |
8da6d581 JS |
351 | if (!ref) |
352 | return -ENOMEM; | |
353 | ||
354 | ref->root_id = root_id; | |
7ac8b88e | 355 | if (key) |
d5c88b73 | 356 | ref->key_for_search = *key; |
7ac8b88e | 357 | else |
d5c88b73 | 358 | memset(&ref->key_for_search, 0, sizeof(ref->key_for_search)); |
8da6d581 | 359 | |
3301958b | 360 | ref->inode_list = NULL; |
8da6d581 JS |
361 | ref->level = level; |
362 | ref->count = count; | |
363 | ref->parent = parent; | |
364 | ref->wanted_disk_byte = wanted_disk_byte; | |
3ec4d323 EN |
365 | prelim_ref_insert(fs_info, preftree, ref, sc); |
366 | return extent_is_shared(sc); | |
8da6d581 JS |
367 | } |
368 | ||
86d5f994 | 369 | /* direct refs use root == 0, key == NULL */ |
00142756 JM |
370 | static int add_direct_ref(const struct btrfs_fs_info *fs_info, |
371 | struct preftrees *preftrees, int level, u64 parent, | |
3ec4d323 EN |
372 | u64 wanted_disk_byte, int count, |
373 | struct share_check *sc, gfp_t gfp_mask) | |
86d5f994 | 374 | { |
00142756 | 375 | return add_prelim_ref(fs_info, &preftrees->direct, 0, NULL, level, |
3ec4d323 | 376 | parent, wanted_disk_byte, count, sc, gfp_mask); |
86d5f994 EN |
377 | } |
378 | ||
379 | /* indirect refs use parent == 0 */ | |
00142756 JM |
380 | static int add_indirect_ref(const struct btrfs_fs_info *fs_info, |
381 | struct preftrees *preftrees, u64 root_id, | |
86d5f994 | 382 | const struct btrfs_key *key, int level, |
3ec4d323 EN |
383 | u64 wanted_disk_byte, int count, |
384 | struct share_check *sc, gfp_t gfp_mask) | |
86d5f994 EN |
385 | { |
386 | struct preftree *tree = &preftrees->indirect; | |
387 | ||
388 | if (!key) | |
389 | tree = &preftrees->indirect_missing_keys; | |
00142756 | 390 | return add_prelim_ref(fs_info, tree, root_id, key, level, 0, |
3ec4d323 | 391 | wanted_disk_byte, count, sc, gfp_mask); |
86d5f994 EN |
392 | } |
393 | ||
ed58f2e6 | 394 | static int is_shared_data_backref(struct preftrees *preftrees, u64 bytenr) |
395 | { | |
396 | struct rb_node **p = &preftrees->direct.root.rb_root.rb_node; | |
397 | struct rb_node *parent = NULL; | |
398 | struct prelim_ref *ref = NULL; | |
9c6c723f | 399 | struct prelim_ref target = {}; |
ed58f2e6 | 400 | int result; |
401 | ||
402 | target.parent = bytenr; | |
403 | ||
404 | while (*p) { | |
405 | parent = *p; | |
406 | ref = rb_entry(parent, struct prelim_ref, rbnode); | |
407 | result = prelim_ref_compare(ref, &target); | |
408 | ||
409 | if (result < 0) | |
410 | p = &(*p)->rb_left; | |
411 | else if (result > 0) | |
412 | p = &(*p)->rb_right; | |
413 | else | |
414 | return 1; | |
415 | } | |
416 | return 0; | |
417 | } | |
418 | ||
8da6d581 | 419 | static int add_all_parents(struct btrfs_root *root, struct btrfs_path *path, |
ed58f2e6 | 420 | struct ulist *parents, |
421 | struct preftrees *preftrees, struct prelim_ref *ref, | |
44853868 | 422 | int level, u64 time_seq, const u64 *extent_item_pos, |
b25b0b87 | 423 | bool ignore_offset) |
8da6d581 | 424 | { |
69bca40d AB |
425 | int ret = 0; |
426 | int slot; | |
427 | struct extent_buffer *eb; | |
428 | struct btrfs_key key; | |
7ef81ac8 | 429 | struct btrfs_key *key_for_search = &ref->key_for_search; |
8da6d581 | 430 | struct btrfs_file_extent_item *fi; |
ed8c4913 | 431 | struct extent_inode_elem *eie = NULL, *old = NULL; |
8da6d581 | 432 | u64 disk_byte; |
7ef81ac8 JB |
433 | u64 wanted_disk_byte = ref->wanted_disk_byte; |
434 | u64 count = 0; | |
7ac8b88e | 435 | u64 data_offset; |
8da6d581 | 436 | |
69bca40d AB |
437 | if (level != 0) { |
438 | eb = path->nodes[level]; | |
439 | ret = ulist_add(parents, eb->start, 0, GFP_NOFS); | |
3301958b JS |
440 | if (ret < 0) |
441 | return ret; | |
8da6d581 | 442 | return 0; |
69bca40d | 443 | } |
8da6d581 JS |
444 | |
445 | /* | |
ed58f2e6 | 446 | * 1. We normally enter this function with the path already pointing to |
447 | * the first item to check. But sometimes, we may enter it with | |
448 | * slot == nritems. | |
449 | * 2. We are searching for normal backref but bytenr of this leaf | |
450 | * matches shared data backref | |
cfc0eed0 | 451 | * 3. The leaf owner is not equal to the root we are searching |
452 | * | |
ed58f2e6 | 453 | * For these cases, go to the next leaf before we continue. |
8da6d581 | 454 | */ |
ed58f2e6 | 455 | eb = path->nodes[0]; |
456 | if (path->slots[0] >= btrfs_header_nritems(eb) || | |
cfc0eed0 | 457 | is_shared_data_backref(preftrees, eb->start) || |
458 | ref->root_id != btrfs_header_owner(eb)) { | |
f3a84ccd | 459 | if (time_seq == BTRFS_SEQ_LAST) |
21633fc6 QW |
460 | ret = btrfs_next_leaf(root, path); |
461 | else | |
462 | ret = btrfs_next_old_leaf(root, path, time_seq); | |
463 | } | |
8da6d581 | 464 | |
b25b0b87 | 465 | while (!ret && count < ref->count) { |
8da6d581 | 466 | eb = path->nodes[0]; |
69bca40d AB |
467 | slot = path->slots[0]; |
468 | ||
469 | btrfs_item_key_to_cpu(eb, &key, slot); | |
470 | ||
471 | if (key.objectid != key_for_search->objectid || | |
472 | key.type != BTRFS_EXTENT_DATA_KEY) | |
473 | break; | |
474 | ||
ed58f2e6 | 475 | /* |
476 | * We are searching for normal backref but bytenr of this leaf | |
cfc0eed0 | 477 | * matches shared data backref, OR |
478 | * the leaf owner is not equal to the root we are searching for | |
ed58f2e6 | 479 | */ |
cfc0eed0 | 480 | if (slot == 0 && |
481 | (is_shared_data_backref(preftrees, eb->start) || | |
482 | ref->root_id != btrfs_header_owner(eb))) { | |
f3a84ccd | 483 | if (time_seq == BTRFS_SEQ_LAST) |
ed58f2e6 | 484 | ret = btrfs_next_leaf(root, path); |
485 | else | |
486 | ret = btrfs_next_old_leaf(root, path, time_seq); | |
487 | continue; | |
488 | } | |
69bca40d AB |
489 | fi = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item); |
490 | disk_byte = btrfs_file_extent_disk_bytenr(eb, fi); | |
7ac8b88e | 491 | data_offset = btrfs_file_extent_offset(eb, fi); |
69bca40d AB |
492 | |
493 | if (disk_byte == wanted_disk_byte) { | |
494 | eie = NULL; | |
ed8c4913 | 495 | old = NULL; |
7ac8b88e | 496 | if (ref->key_for_search.offset == key.offset - data_offset) |
497 | count++; | |
498 | else | |
499 | goto next; | |
69bca40d AB |
500 | if (extent_item_pos) { |
501 | ret = check_extent_in_eb(&key, eb, fi, | |
502 | *extent_item_pos, | |
c995ab3c | 503 | &eie, ignore_offset); |
69bca40d AB |
504 | if (ret < 0) |
505 | break; | |
506 | } | |
ed8c4913 JB |
507 | if (ret > 0) |
508 | goto next; | |
4eb1f66d TI |
509 | ret = ulist_add_merge_ptr(parents, eb->start, |
510 | eie, (void **)&old, GFP_NOFS); | |
ed8c4913 JB |
511 | if (ret < 0) |
512 | break; | |
513 | if (!ret && extent_item_pos) { | |
514 | while (old->next) | |
515 | old = old->next; | |
516 | old->next = eie; | |
69bca40d | 517 | } |
f05c4746 | 518 | eie = NULL; |
8da6d581 | 519 | } |
ed8c4913 | 520 | next: |
f3a84ccd | 521 | if (time_seq == BTRFS_SEQ_LAST) |
21633fc6 QW |
522 | ret = btrfs_next_item(root, path); |
523 | else | |
524 | ret = btrfs_next_old_item(root, path, time_seq); | |
8da6d581 JS |
525 | } |
526 | ||
69bca40d AB |
527 | if (ret > 0) |
528 | ret = 0; | |
f05c4746 WS |
529 | else if (ret < 0) |
530 | free_inode_elem_list(eie); | |
69bca40d | 531 | return ret; |
8da6d581 JS |
532 | } |
533 | ||
534 | /* | |
535 | * resolve an indirect backref in the form (root_id, key, level) | |
536 | * to a logical address | |
537 | */ | |
e0c476b1 JM |
538 | static int resolve_indirect_ref(struct btrfs_fs_info *fs_info, |
539 | struct btrfs_path *path, u64 time_seq, | |
ed58f2e6 | 540 | struct preftrees *preftrees, |
e0c476b1 | 541 | struct prelim_ref *ref, struct ulist *parents, |
b25b0b87 | 542 | const u64 *extent_item_pos, bool ignore_offset) |
8da6d581 | 543 | { |
8da6d581 | 544 | struct btrfs_root *root; |
8da6d581 JS |
545 | struct extent_buffer *eb; |
546 | int ret = 0; | |
547 | int root_level; | |
548 | int level = ref->level; | |
7ac8b88e | 549 | struct btrfs_key search_key = ref->key_for_search; |
8da6d581 | 550 | |
49d11bea JB |
551 | /* |
552 | * If we're search_commit_root we could possibly be holding locks on | |
553 | * other tree nodes. This happens when qgroups does backref walks when | |
554 | * adding new delayed refs. To deal with this we need to look in cache | |
555 | * for the root, and if we don't find it then we need to search the | |
556 | * tree_root's commit root, thus the btrfs_get_fs_root_commit_root usage | |
557 | * here. | |
558 | */ | |
559 | if (path->search_commit_root) | |
560 | root = btrfs_get_fs_root_commit_root(fs_info, path, ref->root_id); | |
561 | else | |
562 | root = btrfs_get_fs_root(fs_info, ref->root_id, false); | |
8da6d581 JS |
563 | if (IS_ERR(root)) { |
564 | ret = PTR_ERR(root); | |
9326f76f JB |
565 | goto out_free; |
566 | } | |
567 | ||
39dba873 JB |
568 | if (!path->search_commit_root && |
569 | test_bit(BTRFS_ROOT_DELETING, &root->state)) { | |
570 | ret = -ENOENT; | |
571 | goto out; | |
572 | } | |
573 | ||
f5ee5c9a | 574 | if (btrfs_is_testing(fs_info)) { |
d9ee522b JB |
575 | ret = -ENOENT; |
576 | goto out; | |
577 | } | |
578 | ||
9e351cc8 JB |
579 | if (path->search_commit_root) |
580 | root_level = btrfs_header_level(root->commit_root); | |
f3a84ccd | 581 | else if (time_seq == BTRFS_SEQ_LAST) |
21633fc6 | 582 | root_level = btrfs_header_level(root->node); |
9e351cc8 JB |
583 | else |
584 | root_level = btrfs_old_root_level(root, time_seq); | |
8da6d581 | 585 | |
c75e8394 | 586 | if (root_level + 1 == level) |
8da6d581 JS |
587 | goto out; |
588 | ||
7ac8b88e | 589 | /* |
590 | * We can often find data backrefs with an offset that is too large | |
591 | * (>= LLONG_MAX, maximum allowed file offset) due to underflows when | |
592 | * subtracting a file's offset with the data offset of its | |
593 | * corresponding extent data item. This can happen for example in the | |
594 | * clone ioctl. | |
595 | * | |
596 | * So if we detect such case we set the search key's offset to zero to | |
597 | * make sure we will find the matching file extent item at | |
598 | * add_all_parents(), otherwise we will miss it because the offset | |
599 | * taken form the backref is much larger then the offset of the file | |
600 | * extent item. This can make us scan a very large number of file | |
601 | * extent items, but at least it will not make us miss any. | |
602 | * | |
603 | * This is an ugly workaround for a behaviour that should have never | |
604 | * existed, but it does and a fix for the clone ioctl would touch a lot | |
605 | * of places, cause backwards incompatibility and would not fix the | |
606 | * problem for extents cloned with older kernels. | |
607 | */ | |
608 | if (search_key.type == BTRFS_EXTENT_DATA_KEY && | |
609 | search_key.offset >= LLONG_MAX) | |
610 | search_key.offset = 0; | |
8da6d581 | 611 | path->lowest_level = level; |
f3a84ccd | 612 | if (time_seq == BTRFS_SEQ_LAST) |
7ac8b88e | 613 | ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0); |
21633fc6 | 614 | else |
7ac8b88e | 615 | ret = btrfs_search_old_slot(root, &search_key, path, time_seq); |
538f72cd | 616 | |
ab8d0fc4 JM |
617 | btrfs_debug(fs_info, |
618 | "search slot in root %llu (level %d, ref count %d) returned %d for key (%llu %u %llu)", | |
c1c9ff7c GU |
619 | ref->root_id, level, ref->count, ret, |
620 | ref->key_for_search.objectid, ref->key_for_search.type, | |
621 | ref->key_for_search.offset); | |
8da6d581 JS |
622 | if (ret < 0) |
623 | goto out; | |
624 | ||
625 | eb = path->nodes[level]; | |
9345457f | 626 | while (!eb) { |
fae7f21c | 627 | if (WARN_ON(!level)) { |
9345457f JS |
628 | ret = 1; |
629 | goto out; | |
630 | } | |
631 | level--; | |
632 | eb = path->nodes[level]; | |
8da6d581 JS |
633 | } |
634 | ||
ed58f2e6 | 635 | ret = add_all_parents(root, path, parents, preftrees, ref, level, |
b25b0b87 | 636 | time_seq, extent_item_pos, ignore_offset); |
8da6d581 | 637 | out: |
00246528 | 638 | btrfs_put_root(root); |
9326f76f | 639 | out_free: |
da61d31a JB |
640 | path->lowest_level = 0; |
641 | btrfs_release_path(path); | |
8da6d581 JS |
642 | return ret; |
643 | } | |
644 | ||
4dae077a JM |
645 | static struct extent_inode_elem * |
646 | unode_aux_to_inode_list(struct ulist_node *node) | |
647 | { | |
648 | if (!node) | |
649 | return NULL; | |
650 | return (struct extent_inode_elem *)(uintptr_t)node->aux; | |
651 | } | |
652 | ||
5614dc3a FM |
653 | static void free_leaf_list(struct ulist *ulist) |
654 | { | |
655 | struct ulist_node *node; | |
656 | struct ulist_iterator uiter; | |
657 | ||
658 | ULIST_ITER_INIT(&uiter); | |
659 | while ((node = ulist_next(ulist, &uiter))) | |
660 | free_inode_elem_list(unode_aux_to_inode_list(node)); | |
661 | ||
662 | ulist_free(ulist); | |
663 | } | |
664 | ||
8da6d581 | 665 | /* |
52042d8e | 666 | * We maintain three separate rbtrees: one for direct refs, one for |
86d5f994 EN |
667 | * indirect refs which have a key, and one for indirect refs which do not |
668 | * have a key. Each tree does merge on insertion. | |
669 | * | |
670 | * Once all of the references are located, we iterate over the tree of | |
671 | * indirect refs with missing keys. An appropriate key is located and | |
672 | * the ref is moved onto the tree for indirect refs. After all missing | |
673 | * keys are thus located, we iterate over the indirect ref tree, resolve | |
674 | * each reference, and then insert the resolved reference onto the | |
675 | * direct tree (merging there too). | |
676 | * | |
677 | * New backrefs (i.e., for parent nodes) are added to the appropriate | |
678 | * rbtree as they are encountered. The new backrefs are subsequently | |
679 | * resolved as above. | |
8da6d581 | 680 | */ |
e0c476b1 JM |
681 | static int resolve_indirect_refs(struct btrfs_fs_info *fs_info, |
682 | struct btrfs_path *path, u64 time_seq, | |
86d5f994 | 683 | struct preftrees *preftrees, |
b25b0b87 | 684 | const u64 *extent_item_pos, |
c995ab3c | 685 | struct share_check *sc, bool ignore_offset) |
8da6d581 JS |
686 | { |
687 | int err; | |
688 | int ret = 0; | |
8da6d581 JS |
689 | struct ulist *parents; |
690 | struct ulist_node *node; | |
cd1b413c | 691 | struct ulist_iterator uiter; |
86d5f994 | 692 | struct rb_node *rnode; |
8da6d581 JS |
693 | |
694 | parents = ulist_alloc(GFP_NOFS); | |
695 | if (!parents) | |
696 | return -ENOMEM; | |
697 | ||
698 | /* | |
86d5f994 EN |
699 | * We could trade memory usage for performance here by iterating |
700 | * the tree, allocating new refs for each insertion, and then | |
701 | * freeing the entire indirect tree when we're done. In some test | |
702 | * cases, the tree can grow quite large (~200k objects). | |
8da6d581 | 703 | */ |
ecf160b4 | 704 | while ((rnode = rb_first_cached(&preftrees->indirect.root))) { |
86d5f994 EN |
705 | struct prelim_ref *ref; |
706 | ||
707 | ref = rb_entry(rnode, struct prelim_ref, rbnode); | |
708 | if (WARN(ref->parent, | |
709 | "BUG: direct ref found in indirect tree")) { | |
710 | ret = -EINVAL; | |
711 | goto out; | |
712 | } | |
713 | ||
ecf160b4 | 714 | rb_erase_cached(&ref->rbnode, &preftrees->indirect.root); |
6c336b21 | 715 | preftrees->indirect.count--; |
86d5f994 EN |
716 | |
717 | if (ref->count == 0) { | |
718 | free_pref(ref); | |
8da6d581 | 719 | continue; |
86d5f994 EN |
720 | } |
721 | ||
c9024219 | 722 | if (sc && ref->root_id != sc->root_objectid) { |
86d5f994 | 723 | free_pref(ref); |
dc046b10 JB |
724 | ret = BACKREF_FOUND_SHARED; |
725 | goto out; | |
726 | } | |
ed58f2e6 | 727 | err = resolve_indirect_ref(fs_info, path, time_seq, preftrees, |
728 | ref, parents, extent_item_pos, | |
b25b0b87 | 729 | ignore_offset); |
95def2ed WS |
730 | /* |
731 | * we can only tolerate ENOENT,otherwise,we should catch error | |
732 | * and return directly. | |
733 | */ | |
734 | if (err == -ENOENT) { | |
3ec4d323 EN |
735 | prelim_ref_insert(fs_info, &preftrees->direct, ref, |
736 | NULL); | |
8da6d581 | 737 | continue; |
95def2ed | 738 | } else if (err) { |
86d5f994 | 739 | free_pref(ref); |
95def2ed WS |
740 | ret = err; |
741 | goto out; | |
742 | } | |
8da6d581 JS |
743 | |
744 | /* we put the first parent into the ref at hand */ | |
cd1b413c JS |
745 | ULIST_ITER_INIT(&uiter); |
746 | node = ulist_next(parents, &uiter); | |
8da6d581 | 747 | ref->parent = node ? node->val : 0; |
4dae077a | 748 | ref->inode_list = unode_aux_to_inode_list(node); |
8da6d581 | 749 | |
86d5f994 | 750 | /* Add a prelim_ref(s) for any other parent(s). */ |
cd1b413c | 751 | while ((node = ulist_next(parents, &uiter))) { |
86d5f994 EN |
752 | struct prelim_ref *new_ref; |
753 | ||
b9e9a6cb WS |
754 | new_ref = kmem_cache_alloc(btrfs_prelim_ref_cache, |
755 | GFP_NOFS); | |
8da6d581 | 756 | if (!new_ref) { |
86d5f994 | 757 | free_pref(ref); |
8da6d581 | 758 | ret = -ENOMEM; |
e36902d4 | 759 | goto out; |
8da6d581 JS |
760 | } |
761 | memcpy(new_ref, ref, sizeof(*ref)); | |
762 | new_ref->parent = node->val; | |
4dae077a | 763 | new_ref->inode_list = unode_aux_to_inode_list(node); |
3ec4d323 EN |
764 | prelim_ref_insert(fs_info, &preftrees->direct, |
765 | new_ref, NULL); | |
8da6d581 | 766 | } |
86d5f994 | 767 | |
3ec4d323 | 768 | /* |
52042d8e | 769 | * Now it's a direct ref, put it in the direct tree. We must |
3ec4d323 EN |
770 | * do this last because the ref could be merged/freed here. |
771 | */ | |
772 | prelim_ref_insert(fs_info, &preftrees->direct, ref, NULL); | |
86d5f994 | 773 | |
8da6d581 | 774 | ulist_reinit(parents); |
9dd14fd6 | 775 | cond_resched(); |
8da6d581 | 776 | } |
e36902d4 | 777 | out: |
5614dc3a FM |
778 | /* |
779 | * We may have inode lists attached to refs in the parents ulist, so we | |
780 | * must free them before freeing the ulist and its refs. | |
781 | */ | |
782 | free_leaf_list(parents); | |
8da6d581 JS |
783 | return ret; |
784 | } | |
785 | ||
d5c88b73 JS |
786 | /* |
787 | * read tree blocks and add keys where required. | |
788 | */ | |
e0c476b1 | 789 | static int add_missing_keys(struct btrfs_fs_info *fs_info, |
38e3eebf | 790 | struct preftrees *preftrees, bool lock) |
d5c88b73 | 791 | { |
e0c476b1 | 792 | struct prelim_ref *ref; |
d5c88b73 | 793 | struct extent_buffer *eb; |
86d5f994 EN |
794 | struct preftree *tree = &preftrees->indirect_missing_keys; |
795 | struct rb_node *node; | |
d5c88b73 | 796 | |
ecf160b4 | 797 | while ((node = rb_first_cached(&tree->root))) { |
86d5f994 | 798 | ref = rb_entry(node, struct prelim_ref, rbnode); |
ecf160b4 | 799 | rb_erase_cached(node, &tree->root); |
86d5f994 EN |
800 | |
801 | BUG_ON(ref->parent); /* should not be a direct ref */ | |
802 | BUG_ON(ref->key_for_search.type); | |
d5c88b73 | 803 | BUG_ON(!ref->wanted_disk_byte); |
86d5f994 | 804 | |
1b7ec85e JB |
805 | eb = read_tree_block(fs_info, ref->wanted_disk_byte, |
806 | ref->root_id, 0, ref->level - 1, NULL); | |
64c043de | 807 | if (IS_ERR(eb)) { |
86d5f994 | 808 | free_pref(ref); |
64c043de | 809 | return PTR_ERR(eb); |
4eb150d6 QW |
810 | } |
811 | if (!extent_buffer_uptodate(eb)) { | |
86d5f994 | 812 | free_pref(ref); |
416bc658 JB |
813 | free_extent_buffer(eb); |
814 | return -EIO; | |
815 | } | |
4eb150d6 | 816 | |
38e3eebf JB |
817 | if (lock) |
818 | btrfs_tree_read_lock(eb); | |
d5c88b73 JS |
819 | if (btrfs_header_level(eb) == 0) |
820 | btrfs_item_key_to_cpu(eb, &ref->key_for_search, 0); | |
821 | else | |
822 | btrfs_node_key_to_cpu(eb, &ref->key_for_search, 0); | |
38e3eebf JB |
823 | if (lock) |
824 | btrfs_tree_read_unlock(eb); | |
d5c88b73 | 825 | free_extent_buffer(eb); |
3ec4d323 | 826 | prelim_ref_insert(fs_info, &preftrees->indirect, ref, NULL); |
9dd14fd6 | 827 | cond_resched(); |
d5c88b73 JS |
828 | } |
829 | return 0; | |
830 | } | |
831 | ||
8da6d581 JS |
832 | /* |
833 | * add all currently queued delayed refs from this head whose seq nr is | |
834 | * smaller or equal that seq to the list | |
835 | */ | |
00142756 JM |
836 | static int add_delayed_refs(const struct btrfs_fs_info *fs_info, |
837 | struct btrfs_delayed_ref_head *head, u64 seq, | |
b25b0b87 | 838 | struct preftrees *preftrees, struct share_check *sc) |
8da6d581 | 839 | { |
c6fc2454 | 840 | struct btrfs_delayed_ref_node *node; |
d5c88b73 | 841 | struct btrfs_key key; |
0e0adbcf | 842 | struct rb_node *n; |
01747e92 | 843 | int count; |
b1375d64 | 844 | int ret = 0; |
8da6d581 | 845 | |
d7df2c79 | 846 | spin_lock(&head->lock); |
e3d03965 | 847 | for (n = rb_first_cached(&head->ref_tree); n; n = rb_next(n)) { |
0e0adbcf JB |
848 | node = rb_entry(n, struct btrfs_delayed_ref_node, |
849 | ref_node); | |
8da6d581 JS |
850 | if (node->seq > seq) |
851 | continue; | |
852 | ||
853 | switch (node->action) { | |
854 | case BTRFS_ADD_DELAYED_EXTENT: | |
855 | case BTRFS_UPDATE_DELAYED_HEAD: | |
856 | WARN_ON(1); | |
857 | continue; | |
858 | case BTRFS_ADD_DELAYED_REF: | |
01747e92 | 859 | count = node->ref_mod; |
8da6d581 JS |
860 | break; |
861 | case BTRFS_DROP_DELAYED_REF: | |
01747e92 | 862 | count = node->ref_mod * -1; |
8da6d581 JS |
863 | break; |
864 | default: | |
290342f6 | 865 | BUG(); |
8da6d581 JS |
866 | } |
867 | switch (node->type) { | |
868 | case BTRFS_TREE_BLOCK_REF_KEY: { | |
86d5f994 | 869 | /* NORMAL INDIRECT METADATA backref */ |
8da6d581 | 870 | struct btrfs_delayed_tree_ref *ref; |
943553ef FM |
871 | struct btrfs_key *key_ptr = NULL; |
872 | ||
873 | if (head->extent_op && head->extent_op->update_key) { | |
874 | btrfs_disk_key_to_cpu(&key, &head->extent_op->key); | |
875 | key_ptr = &key; | |
876 | } | |
8da6d581 JS |
877 | |
878 | ref = btrfs_delayed_node_to_tree_ref(node); | |
00142756 | 879 | ret = add_indirect_ref(fs_info, preftrees, ref->root, |
943553ef | 880 | key_ptr, ref->level + 1, |
01747e92 EN |
881 | node->bytenr, count, sc, |
882 | GFP_ATOMIC); | |
8da6d581 JS |
883 | break; |
884 | } | |
885 | case BTRFS_SHARED_BLOCK_REF_KEY: { | |
86d5f994 | 886 | /* SHARED DIRECT METADATA backref */ |
8da6d581 JS |
887 | struct btrfs_delayed_tree_ref *ref; |
888 | ||
889 | ref = btrfs_delayed_node_to_tree_ref(node); | |
86d5f994 | 890 | |
01747e92 EN |
891 | ret = add_direct_ref(fs_info, preftrees, ref->level + 1, |
892 | ref->parent, node->bytenr, count, | |
3ec4d323 | 893 | sc, GFP_ATOMIC); |
8da6d581 JS |
894 | break; |
895 | } | |
896 | case BTRFS_EXTENT_DATA_REF_KEY: { | |
86d5f994 | 897 | /* NORMAL INDIRECT DATA backref */ |
8da6d581 | 898 | struct btrfs_delayed_data_ref *ref; |
8da6d581 JS |
899 | ref = btrfs_delayed_node_to_data_ref(node); |
900 | ||
901 | key.objectid = ref->objectid; | |
902 | key.type = BTRFS_EXTENT_DATA_KEY; | |
903 | key.offset = ref->offset; | |
dc046b10 JB |
904 | |
905 | /* | |
4fc7b572 FM |
906 | * If we have a share check context and a reference for |
907 | * another inode, we can't exit immediately. This is | |
908 | * because even if this is a BTRFS_ADD_DELAYED_REF | |
909 | * reference we may find next a BTRFS_DROP_DELAYED_REF | |
910 | * which cancels out this ADD reference. | |
911 | * | |
912 | * If this is a DROP reference and there was no previous | |
913 | * ADD reference, then we need to signal that when we | |
914 | * process references from the extent tree (through | |
915 | * add_inline_refs() and add_keyed_refs()), we should | |
916 | * not exit early if we find a reference for another | |
917 | * inode, because one of the delayed DROP references | |
918 | * may cancel that reference in the extent tree. | |
dc046b10 | 919 | */ |
4fc7b572 FM |
920 | if (sc && count < 0) |
921 | sc->have_delayed_delete_refs = true; | |
dc046b10 | 922 | |
00142756 | 923 | ret = add_indirect_ref(fs_info, preftrees, ref->root, |
01747e92 EN |
924 | &key, 0, node->bytenr, count, sc, |
925 | GFP_ATOMIC); | |
8da6d581 JS |
926 | break; |
927 | } | |
928 | case BTRFS_SHARED_DATA_REF_KEY: { | |
86d5f994 | 929 | /* SHARED DIRECT FULL backref */ |
8da6d581 | 930 | struct btrfs_delayed_data_ref *ref; |
8da6d581 JS |
931 | |
932 | ref = btrfs_delayed_node_to_data_ref(node); | |
86d5f994 | 933 | |
01747e92 EN |
934 | ret = add_direct_ref(fs_info, preftrees, 0, ref->parent, |
935 | node->bytenr, count, sc, | |
936 | GFP_ATOMIC); | |
8da6d581 JS |
937 | break; |
938 | } | |
939 | default: | |
940 | WARN_ON(1); | |
941 | } | |
3ec4d323 EN |
942 | /* |
943 | * We must ignore BACKREF_FOUND_SHARED until all delayed | |
944 | * refs have been checked. | |
945 | */ | |
946 | if (ret && (ret != BACKREF_FOUND_SHARED)) | |
d7df2c79 | 947 | break; |
8da6d581 | 948 | } |
3ec4d323 EN |
949 | if (!ret) |
950 | ret = extent_is_shared(sc); | |
4fc7b572 | 951 | |
d7df2c79 JB |
952 | spin_unlock(&head->lock); |
953 | return ret; | |
8da6d581 JS |
954 | } |
955 | ||
956 | /* | |
957 | * add all inline backrefs for bytenr to the list | |
3ec4d323 EN |
958 | * |
959 | * Returns 0 on success, <0 on error, or BACKREF_FOUND_SHARED. | |
8da6d581 | 960 | */ |
00142756 JM |
961 | static int add_inline_refs(const struct btrfs_fs_info *fs_info, |
962 | struct btrfs_path *path, u64 bytenr, | |
86d5f994 | 963 | int *info_level, struct preftrees *preftrees, |
b25b0b87 | 964 | struct share_check *sc) |
8da6d581 | 965 | { |
b1375d64 | 966 | int ret = 0; |
8da6d581 JS |
967 | int slot; |
968 | struct extent_buffer *leaf; | |
969 | struct btrfs_key key; | |
261c84b6 | 970 | struct btrfs_key found_key; |
8da6d581 JS |
971 | unsigned long ptr; |
972 | unsigned long end; | |
973 | struct btrfs_extent_item *ei; | |
974 | u64 flags; | |
975 | u64 item_size; | |
976 | ||
977 | /* | |
978 | * enumerate all inline refs | |
979 | */ | |
980 | leaf = path->nodes[0]; | |
dadcaf78 | 981 | slot = path->slots[0]; |
8da6d581 | 982 | |
3212fa14 | 983 | item_size = btrfs_item_size(leaf, slot); |
8da6d581 JS |
984 | BUG_ON(item_size < sizeof(*ei)); |
985 | ||
986 | ei = btrfs_item_ptr(leaf, slot, struct btrfs_extent_item); | |
987 | flags = btrfs_extent_flags(leaf, ei); | |
261c84b6 | 988 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
8da6d581 JS |
989 | |
990 | ptr = (unsigned long)(ei + 1); | |
991 | end = (unsigned long)ei + item_size; | |
992 | ||
261c84b6 JB |
993 | if (found_key.type == BTRFS_EXTENT_ITEM_KEY && |
994 | flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) { | |
8da6d581 | 995 | struct btrfs_tree_block_info *info; |
8da6d581 JS |
996 | |
997 | info = (struct btrfs_tree_block_info *)ptr; | |
998 | *info_level = btrfs_tree_block_level(leaf, info); | |
8da6d581 JS |
999 | ptr += sizeof(struct btrfs_tree_block_info); |
1000 | BUG_ON(ptr > end); | |
261c84b6 JB |
1001 | } else if (found_key.type == BTRFS_METADATA_ITEM_KEY) { |
1002 | *info_level = found_key.offset; | |
8da6d581 JS |
1003 | } else { |
1004 | BUG_ON(!(flags & BTRFS_EXTENT_FLAG_DATA)); | |
1005 | } | |
1006 | ||
1007 | while (ptr < end) { | |
1008 | struct btrfs_extent_inline_ref *iref; | |
1009 | u64 offset; | |
1010 | int type; | |
1011 | ||
1012 | iref = (struct btrfs_extent_inline_ref *)ptr; | |
3de28d57 LB |
1013 | type = btrfs_get_extent_inline_ref_type(leaf, iref, |
1014 | BTRFS_REF_TYPE_ANY); | |
1015 | if (type == BTRFS_REF_TYPE_INVALID) | |
af431dcb | 1016 | return -EUCLEAN; |
3de28d57 | 1017 | |
8da6d581 JS |
1018 | offset = btrfs_extent_inline_ref_offset(leaf, iref); |
1019 | ||
1020 | switch (type) { | |
1021 | case BTRFS_SHARED_BLOCK_REF_KEY: | |
00142756 JM |
1022 | ret = add_direct_ref(fs_info, preftrees, |
1023 | *info_level + 1, offset, | |
3ec4d323 | 1024 | bytenr, 1, NULL, GFP_NOFS); |
8da6d581 JS |
1025 | break; |
1026 | case BTRFS_SHARED_DATA_REF_KEY: { | |
1027 | struct btrfs_shared_data_ref *sdref; | |
1028 | int count; | |
1029 | ||
1030 | sdref = (struct btrfs_shared_data_ref *)(iref + 1); | |
1031 | count = btrfs_shared_data_ref_count(leaf, sdref); | |
86d5f994 | 1032 | |
00142756 | 1033 | ret = add_direct_ref(fs_info, preftrees, 0, offset, |
3ec4d323 | 1034 | bytenr, count, sc, GFP_NOFS); |
8da6d581 JS |
1035 | break; |
1036 | } | |
1037 | case BTRFS_TREE_BLOCK_REF_KEY: | |
00142756 JM |
1038 | ret = add_indirect_ref(fs_info, preftrees, offset, |
1039 | NULL, *info_level + 1, | |
3ec4d323 | 1040 | bytenr, 1, NULL, GFP_NOFS); |
8da6d581 JS |
1041 | break; |
1042 | case BTRFS_EXTENT_DATA_REF_KEY: { | |
1043 | struct btrfs_extent_data_ref *dref; | |
1044 | int count; | |
1045 | u64 root; | |
1046 | ||
1047 | dref = (struct btrfs_extent_data_ref *)(&iref->offset); | |
1048 | count = btrfs_extent_data_ref_count(leaf, dref); | |
1049 | key.objectid = btrfs_extent_data_ref_objectid(leaf, | |
1050 | dref); | |
1051 | key.type = BTRFS_EXTENT_DATA_KEY; | |
1052 | key.offset = btrfs_extent_data_ref_offset(leaf, dref); | |
dc046b10 | 1053 | |
a0a5472a | 1054 | if (sc && key.objectid != sc->inum && |
4fc7b572 | 1055 | !sc->have_delayed_delete_refs) { |
dc046b10 JB |
1056 | ret = BACKREF_FOUND_SHARED; |
1057 | break; | |
1058 | } | |
1059 | ||
8da6d581 | 1060 | root = btrfs_extent_data_ref_root(leaf, dref); |
86d5f994 | 1061 | |
00142756 JM |
1062 | ret = add_indirect_ref(fs_info, preftrees, root, |
1063 | &key, 0, bytenr, count, | |
3ec4d323 | 1064 | sc, GFP_NOFS); |
4fc7b572 | 1065 | |
8da6d581 JS |
1066 | break; |
1067 | } | |
1068 | default: | |
1069 | WARN_ON(1); | |
1070 | } | |
1149ab6b WS |
1071 | if (ret) |
1072 | return ret; | |
8da6d581 JS |
1073 | ptr += btrfs_extent_inline_ref_size(type); |
1074 | } | |
1075 | ||
1076 | return 0; | |
1077 | } | |
1078 | ||
1079 | /* | |
1080 | * add all non-inline backrefs for bytenr to the list | |
3ec4d323 EN |
1081 | * |
1082 | * Returns 0 on success, <0 on error, or BACKREF_FOUND_SHARED. | |
8da6d581 | 1083 | */ |
98cc4222 | 1084 | static int add_keyed_refs(struct btrfs_root *extent_root, |
e0c476b1 | 1085 | struct btrfs_path *path, u64 bytenr, |
86d5f994 | 1086 | int info_level, struct preftrees *preftrees, |
3ec4d323 | 1087 | struct share_check *sc) |
8da6d581 | 1088 | { |
98cc4222 | 1089 | struct btrfs_fs_info *fs_info = extent_root->fs_info; |
8da6d581 JS |
1090 | int ret; |
1091 | int slot; | |
1092 | struct extent_buffer *leaf; | |
1093 | struct btrfs_key key; | |
1094 | ||
1095 | while (1) { | |
1096 | ret = btrfs_next_item(extent_root, path); | |
1097 | if (ret < 0) | |
1098 | break; | |
1099 | if (ret) { | |
1100 | ret = 0; | |
1101 | break; | |
1102 | } | |
1103 | ||
1104 | slot = path->slots[0]; | |
1105 | leaf = path->nodes[0]; | |
1106 | btrfs_item_key_to_cpu(leaf, &key, slot); | |
1107 | ||
1108 | if (key.objectid != bytenr) | |
1109 | break; | |
1110 | if (key.type < BTRFS_TREE_BLOCK_REF_KEY) | |
1111 | continue; | |
1112 | if (key.type > BTRFS_SHARED_DATA_REF_KEY) | |
1113 | break; | |
1114 | ||
1115 | switch (key.type) { | |
1116 | case BTRFS_SHARED_BLOCK_REF_KEY: | |
86d5f994 | 1117 | /* SHARED DIRECT METADATA backref */ |
00142756 JM |
1118 | ret = add_direct_ref(fs_info, preftrees, |
1119 | info_level + 1, key.offset, | |
3ec4d323 | 1120 | bytenr, 1, NULL, GFP_NOFS); |
8da6d581 JS |
1121 | break; |
1122 | case BTRFS_SHARED_DATA_REF_KEY: { | |
86d5f994 | 1123 | /* SHARED DIRECT FULL backref */ |
8da6d581 JS |
1124 | struct btrfs_shared_data_ref *sdref; |
1125 | int count; | |
1126 | ||
1127 | sdref = btrfs_item_ptr(leaf, slot, | |
1128 | struct btrfs_shared_data_ref); | |
1129 | count = btrfs_shared_data_ref_count(leaf, sdref); | |
00142756 JM |
1130 | ret = add_direct_ref(fs_info, preftrees, 0, |
1131 | key.offset, bytenr, count, | |
3ec4d323 | 1132 | sc, GFP_NOFS); |
8da6d581 JS |
1133 | break; |
1134 | } | |
1135 | case BTRFS_TREE_BLOCK_REF_KEY: | |
86d5f994 | 1136 | /* NORMAL INDIRECT METADATA backref */ |
00142756 JM |
1137 | ret = add_indirect_ref(fs_info, preftrees, key.offset, |
1138 | NULL, info_level + 1, bytenr, | |
3ec4d323 | 1139 | 1, NULL, GFP_NOFS); |
8da6d581 JS |
1140 | break; |
1141 | case BTRFS_EXTENT_DATA_REF_KEY: { | |
86d5f994 | 1142 | /* NORMAL INDIRECT DATA backref */ |
8da6d581 JS |
1143 | struct btrfs_extent_data_ref *dref; |
1144 | int count; | |
1145 | u64 root; | |
1146 | ||
1147 | dref = btrfs_item_ptr(leaf, slot, | |
1148 | struct btrfs_extent_data_ref); | |
1149 | count = btrfs_extent_data_ref_count(leaf, dref); | |
1150 | key.objectid = btrfs_extent_data_ref_objectid(leaf, | |
1151 | dref); | |
1152 | key.type = BTRFS_EXTENT_DATA_KEY; | |
1153 | key.offset = btrfs_extent_data_ref_offset(leaf, dref); | |
dc046b10 | 1154 | |
a0a5472a | 1155 | if (sc && key.objectid != sc->inum && |
4fc7b572 | 1156 | !sc->have_delayed_delete_refs) { |
dc046b10 JB |
1157 | ret = BACKREF_FOUND_SHARED; |
1158 | break; | |
1159 | } | |
1160 | ||
8da6d581 | 1161 | root = btrfs_extent_data_ref_root(leaf, dref); |
00142756 JM |
1162 | ret = add_indirect_ref(fs_info, preftrees, root, |
1163 | &key, 0, bytenr, count, | |
3ec4d323 | 1164 | sc, GFP_NOFS); |
8da6d581 JS |
1165 | break; |
1166 | } | |
1167 | default: | |
1168 | WARN_ON(1); | |
1169 | } | |
1149ab6b WS |
1170 | if (ret) |
1171 | return ret; | |
1172 | ||
8da6d581 JS |
1173 | } |
1174 | ||
1175 | return ret; | |
1176 | } | |
1177 | ||
1178 | /* | |
1179 | * this adds all existing backrefs (inline backrefs, backrefs and delayed | |
1180 | * refs) for the given bytenr to the refs list, merges duplicates and resolves | |
1181 | * indirect refs to their parent bytenr. | |
1182 | * When roots are found, they're added to the roots list | |
1183 | * | |
f3a84ccd FM |
1184 | * If time_seq is set to BTRFS_SEQ_LAST, it will not search delayed_refs, and |
1185 | * behave much like trans == NULL case, the difference only lies in it will not | |
21633fc6 QW |
1186 | * commit root. |
1187 | * The special case is for qgroup to search roots in commit_transaction(). | |
1188 | * | |
3ec4d323 EN |
1189 | * @sc - if !NULL, then immediately return BACKREF_FOUND_SHARED when a |
1190 | * shared extent is detected. | |
1191 | * | |
1192 | * Otherwise this returns 0 for success and <0 for an error. | |
1193 | * | |
c995ab3c ZB |
1194 | * If ignore_offset is set to false, only extent refs whose offsets match |
1195 | * extent_item_pos are returned. If true, every extent ref is returned | |
1196 | * and extent_item_pos is ignored. | |
1197 | * | |
8da6d581 JS |
1198 | * FIXME some caching might speed things up |
1199 | */ | |
1200 | static int find_parent_nodes(struct btrfs_trans_handle *trans, | |
1201 | struct btrfs_fs_info *fs_info, u64 bytenr, | |
097b8a7c | 1202 | u64 time_seq, struct ulist *refs, |
dc046b10 | 1203 | struct ulist *roots, const u64 *extent_item_pos, |
c995ab3c | 1204 | struct share_check *sc, bool ignore_offset) |
8da6d581 | 1205 | { |
29cbcf40 | 1206 | struct btrfs_root *root = btrfs_extent_root(fs_info, bytenr); |
8da6d581 JS |
1207 | struct btrfs_key key; |
1208 | struct btrfs_path *path; | |
8da6d581 | 1209 | struct btrfs_delayed_ref_root *delayed_refs = NULL; |
d3b01064 | 1210 | struct btrfs_delayed_ref_head *head; |
8da6d581 JS |
1211 | int info_level = 0; |
1212 | int ret; | |
e0c476b1 | 1213 | struct prelim_ref *ref; |
86d5f994 | 1214 | struct rb_node *node; |
f05c4746 | 1215 | struct extent_inode_elem *eie = NULL; |
86d5f994 EN |
1216 | struct preftrees preftrees = { |
1217 | .direct = PREFTREE_INIT, | |
1218 | .indirect = PREFTREE_INIT, | |
1219 | .indirect_missing_keys = PREFTREE_INIT | |
1220 | }; | |
8da6d581 JS |
1221 | |
1222 | key.objectid = bytenr; | |
8da6d581 | 1223 | key.offset = (u64)-1; |
261c84b6 JB |
1224 | if (btrfs_fs_incompat(fs_info, SKINNY_METADATA)) |
1225 | key.type = BTRFS_METADATA_ITEM_KEY; | |
1226 | else | |
1227 | key.type = BTRFS_EXTENT_ITEM_KEY; | |
8da6d581 JS |
1228 | |
1229 | path = btrfs_alloc_path(); | |
1230 | if (!path) | |
1231 | return -ENOMEM; | |
e84752d4 | 1232 | if (!trans) { |
da61d31a | 1233 | path->search_commit_root = 1; |
e84752d4 WS |
1234 | path->skip_locking = 1; |
1235 | } | |
8da6d581 | 1236 | |
f3a84ccd | 1237 | if (time_seq == BTRFS_SEQ_LAST) |
21633fc6 QW |
1238 | path->skip_locking = 1; |
1239 | ||
8da6d581 | 1240 | again: |
d3b01064 LZ |
1241 | head = NULL; |
1242 | ||
98cc4222 | 1243 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
8da6d581 JS |
1244 | if (ret < 0) |
1245 | goto out; | |
fcba0120 JB |
1246 | if (ret == 0) { |
1247 | /* This shouldn't happen, indicates a bug or fs corruption. */ | |
1248 | ASSERT(ret != 0); | |
1249 | ret = -EUCLEAN; | |
1250 | goto out; | |
1251 | } | |
8da6d581 | 1252 | |
21633fc6 | 1253 | if (trans && likely(trans->type != __TRANS_DUMMY) && |
f3a84ccd | 1254 | time_seq != BTRFS_SEQ_LAST) { |
7a3ae2f8 | 1255 | /* |
9665ebd5 JB |
1256 | * We have a specific time_seq we care about and trans which |
1257 | * means we have the path lock, we need to grab the ref head and | |
1258 | * lock it so we have a consistent view of the refs at the given | |
1259 | * time. | |
7a3ae2f8 JS |
1260 | */ |
1261 | delayed_refs = &trans->transaction->delayed_refs; | |
1262 | spin_lock(&delayed_refs->lock); | |
f72ad18e | 1263 | head = btrfs_find_delayed_ref_head(delayed_refs, bytenr); |
7a3ae2f8 JS |
1264 | if (head) { |
1265 | if (!mutex_trylock(&head->mutex)) { | |
d278850e | 1266 | refcount_inc(&head->refs); |
7a3ae2f8 JS |
1267 | spin_unlock(&delayed_refs->lock); |
1268 | ||
1269 | btrfs_release_path(path); | |
1270 | ||
1271 | /* | |
1272 | * Mutex was contended, block until it's | |
1273 | * released and try again | |
1274 | */ | |
1275 | mutex_lock(&head->mutex); | |
1276 | mutex_unlock(&head->mutex); | |
d278850e | 1277 | btrfs_put_delayed_ref_head(head); |
7a3ae2f8 JS |
1278 | goto again; |
1279 | } | |
d7df2c79 | 1280 | spin_unlock(&delayed_refs->lock); |
00142756 | 1281 | ret = add_delayed_refs(fs_info, head, time_seq, |
b25b0b87 | 1282 | &preftrees, sc); |
155725c9 | 1283 | mutex_unlock(&head->mutex); |
d7df2c79 | 1284 | if (ret) |
7a3ae2f8 | 1285 | goto out; |
d7df2c79 JB |
1286 | } else { |
1287 | spin_unlock(&delayed_refs->lock); | |
d3b01064 | 1288 | } |
8da6d581 | 1289 | } |
8da6d581 JS |
1290 | |
1291 | if (path->slots[0]) { | |
1292 | struct extent_buffer *leaf; | |
1293 | int slot; | |
1294 | ||
dadcaf78 | 1295 | path->slots[0]--; |
8da6d581 | 1296 | leaf = path->nodes[0]; |
dadcaf78 | 1297 | slot = path->slots[0]; |
8da6d581 JS |
1298 | btrfs_item_key_to_cpu(leaf, &key, slot); |
1299 | if (key.objectid == bytenr && | |
261c84b6 JB |
1300 | (key.type == BTRFS_EXTENT_ITEM_KEY || |
1301 | key.type == BTRFS_METADATA_ITEM_KEY)) { | |
00142756 | 1302 | ret = add_inline_refs(fs_info, path, bytenr, |
b25b0b87 | 1303 | &info_level, &preftrees, sc); |
8da6d581 JS |
1304 | if (ret) |
1305 | goto out; | |
98cc4222 | 1306 | ret = add_keyed_refs(root, path, bytenr, info_level, |
3ec4d323 | 1307 | &preftrees, sc); |
8da6d581 JS |
1308 | if (ret) |
1309 | goto out; | |
1310 | } | |
1311 | } | |
8da6d581 | 1312 | |
86d5f994 | 1313 | btrfs_release_path(path); |
8da6d581 | 1314 | |
38e3eebf | 1315 | ret = add_missing_keys(fs_info, &preftrees, path->skip_locking == 0); |
d5c88b73 JS |
1316 | if (ret) |
1317 | goto out; | |
1318 | ||
ecf160b4 | 1319 | WARN_ON(!RB_EMPTY_ROOT(&preftrees.indirect_missing_keys.root.rb_root)); |
8da6d581 | 1320 | |
86d5f994 | 1321 | ret = resolve_indirect_refs(fs_info, path, time_seq, &preftrees, |
b25b0b87 | 1322 | extent_item_pos, sc, ignore_offset); |
8da6d581 JS |
1323 | if (ret) |
1324 | goto out; | |
1325 | ||
ecf160b4 | 1326 | WARN_ON(!RB_EMPTY_ROOT(&preftrees.indirect.root.rb_root)); |
8da6d581 | 1327 | |
86d5f994 EN |
1328 | /* |
1329 | * This walks the tree of merged and resolved refs. Tree blocks are | |
1330 | * read in as needed. Unique entries are added to the ulist, and | |
1331 | * the list of found roots is updated. | |
1332 | * | |
1333 | * We release the entire tree in one go before returning. | |
1334 | */ | |
ecf160b4 | 1335 | node = rb_first_cached(&preftrees.direct.root); |
86d5f994 EN |
1336 | while (node) { |
1337 | ref = rb_entry(node, struct prelim_ref, rbnode); | |
1338 | node = rb_next(&ref->rbnode); | |
c8195a7b ZB |
1339 | /* |
1340 | * ref->count < 0 can happen here if there are delayed | |
1341 | * refs with a node->action of BTRFS_DROP_DELAYED_REF. | |
1342 | * prelim_ref_insert() relies on this when merging | |
1343 | * identical refs to keep the overall count correct. | |
1344 | * prelim_ref_insert() will merge only those refs | |
1345 | * which compare identically. Any refs having | |
1346 | * e.g. different offsets would not be merged, | |
1347 | * and would retain their original ref->count < 0. | |
1348 | */ | |
98cfee21 | 1349 | if (roots && ref->count && ref->root_id && ref->parent == 0) { |
c9024219 | 1350 | if (sc && ref->root_id != sc->root_objectid) { |
dc046b10 JB |
1351 | ret = BACKREF_FOUND_SHARED; |
1352 | goto out; | |
1353 | } | |
1354 | ||
8da6d581 JS |
1355 | /* no parent == root of tree */ |
1356 | ret = ulist_add(roots, ref->root_id, 0, GFP_NOFS); | |
f1723939 WS |
1357 | if (ret < 0) |
1358 | goto out; | |
8da6d581 JS |
1359 | } |
1360 | if (ref->count && ref->parent) { | |
8a56457f JB |
1361 | if (extent_item_pos && !ref->inode_list && |
1362 | ref->level == 0) { | |
976b1908 | 1363 | struct extent_buffer *eb; |
707e8a07 | 1364 | |
581c1760 | 1365 | eb = read_tree_block(fs_info, ref->parent, 0, |
1b7ec85e | 1366 | 0, ref->level, NULL); |
64c043de LB |
1367 | if (IS_ERR(eb)) { |
1368 | ret = PTR_ERR(eb); | |
1369 | goto out; | |
4eb150d6 QW |
1370 | } |
1371 | if (!extent_buffer_uptodate(eb)) { | |
416bc658 | 1372 | free_extent_buffer(eb); |
c16c2e2e WS |
1373 | ret = -EIO; |
1374 | goto out; | |
416bc658 | 1375 | } |
38e3eebf | 1376 | |
ac5887c8 | 1377 | if (!path->skip_locking) |
38e3eebf | 1378 | btrfs_tree_read_lock(eb); |
976b1908 | 1379 | ret = find_extent_in_eb(eb, bytenr, |
c995ab3c | 1380 | *extent_item_pos, &eie, ignore_offset); |
38e3eebf | 1381 | if (!path->skip_locking) |
ac5887c8 | 1382 | btrfs_tree_read_unlock(eb); |
976b1908 | 1383 | free_extent_buffer(eb); |
f5929cd8 FDBM |
1384 | if (ret < 0) |
1385 | goto out; | |
1386 | ref->inode_list = eie; | |
92876eec FM |
1387 | /* |
1388 | * We transferred the list ownership to the ref, | |
1389 | * so set to NULL to avoid a double free in case | |
1390 | * an error happens after this. | |
1391 | */ | |
1392 | eie = NULL; | |
976b1908 | 1393 | } |
4eb1f66d TI |
1394 | ret = ulist_add_merge_ptr(refs, ref->parent, |
1395 | ref->inode_list, | |
1396 | (void **)&eie, GFP_NOFS); | |
f1723939 WS |
1397 | if (ret < 0) |
1398 | goto out; | |
3301958b JS |
1399 | if (!ret && extent_item_pos) { |
1400 | /* | |
9f05c09d JB |
1401 | * We've recorded that parent, so we must extend |
1402 | * its inode list here. | |
1403 | * | |
1404 | * However if there was corruption we may not | |
1405 | * have found an eie, return an error in this | |
1406 | * case. | |
3301958b | 1407 | */ |
9f05c09d JB |
1408 | ASSERT(eie); |
1409 | if (!eie) { | |
1410 | ret = -EUCLEAN; | |
1411 | goto out; | |
1412 | } | |
3301958b JS |
1413 | while (eie->next) |
1414 | eie = eie->next; | |
1415 | eie->next = ref->inode_list; | |
1416 | } | |
f05c4746 | 1417 | eie = NULL; |
92876eec FM |
1418 | /* |
1419 | * We have transferred the inode list ownership from | |
1420 | * this ref to the ref we added to the 'refs' ulist. | |
1421 | * So set this ref's inode list to NULL to avoid | |
1422 | * use-after-free when our caller uses it or double | |
1423 | * frees in case an error happens before we return. | |
1424 | */ | |
1425 | ref->inode_list = NULL; | |
8da6d581 | 1426 | } |
9dd14fd6 | 1427 | cond_resched(); |
8da6d581 JS |
1428 | } |
1429 | ||
1430 | out: | |
8da6d581 | 1431 | btrfs_free_path(path); |
86d5f994 EN |
1432 | |
1433 | prelim_release(&preftrees.direct); | |
1434 | prelim_release(&preftrees.indirect); | |
1435 | prelim_release(&preftrees.indirect_missing_keys); | |
1436 | ||
f05c4746 WS |
1437 | if (ret < 0) |
1438 | free_inode_elem_list(eie); | |
8da6d581 JS |
1439 | return ret; |
1440 | } | |
1441 | ||
1442 | /* | |
1443 | * Finds all leafs with a reference to the specified combination of bytenr and | |
1444 | * offset. key_list_head will point to a list of corresponding keys (caller must | |
1445 | * free each list element). The leafs will be stored in the leafs ulist, which | |
1446 | * must be freed with ulist_free. | |
1447 | * | |
1448 | * returns 0 on success, <0 on error | |
1449 | */ | |
19b546d7 QW |
1450 | int btrfs_find_all_leafs(struct btrfs_trans_handle *trans, |
1451 | struct btrfs_fs_info *fs_info, u64 bytenr, | |
1452 | u64 time_seq, struct ulist **leafs, | |
1453 | const u64 *extent_item_pos, bool ignore_offset) | |
8da6d581 | 1454 | { |
8da6d581 JS |
1455 | int ret; |
1456 | ||
8da6d581 | 1457 | *leafs = ulist_alloc(GFP_NOFS); |
98cfee21 | 1458 | if (!*leafs) |
8da6d581 | 1459 | return -ENOMEM; |
8da6d581 | 1460 | |
afce772e | 1461 | ret = find_parent_nodes(trans, fs_info, bytenr, time_seq, |
c995ab3c | 1462 | *leafs, NULL, extent_item_pos, NULL, ignore_offset); |
8da6d581 | 1463 | if (ret < 0 && ret != -ENOENT) { |
976b1908 | 1464 | free_leaf_list(*leafs); |
8da6d581 JS |
1465 | return ret; |
1466 | } | |
1467 | ||
1468 | return 0; | |
1469 | } | |
1470 | ||
1471 | /* | |
1472 | * walk all backrefs for a given extent to find all roots that reference this | |
1473 | * extent. Walking a backref means finding all extents that reference this | |
1474 | * extent and in turn walk the backrefs of those, too. Naturally this is a | |
1475 | * recursive process, but here it is implemented in an iterative fashion: We | |
1476 | * find all referencing extents for the extent in question and put them on a | |
1477 | * list. In turn, we find all referencing extents for those, further appending | |
1478 | * to the list. The way we iterate the list allows adding more elements after | |
1479 | * the current while iterating. The process stops when we reach the end of the | |
1480 | * list. Found roots are added to the roots list. | |
1481 | * | |
1482 | * returns 0 on success, < 0 on error. | |
1483 | */ | |
e0c476b1 JM |
1484 | static int btrfs_find_all_roots_safe(struct btrfs_trans_handle *trans, |
1485 | struct btrfs_fs_info *fs_info, u64 bytenr, | |
c995ab3c ZB |
1486 | u64 time_seq, struct ulist **roots, |
1487 | bool ignore_offset) | |
8da6d581 JS |
1488 | { |
1489 | struct ulist *tmp; | |
1490 | struct ulist_node *node = NULL; | |
cd1b413c | 1491 | struct ulist_iterator uiter; |
8da6d581 JS |
1492 | int ret; |
1493 | ||
1494 | tmp = ulist_alloc(GFP_NOFS); | |
1495 | if (!tmp) | |
1496 | return -ENOMEM; | |
1497 | *roots = ulist_alloc(GFP_NOFS); | |
1498 | if (!*roots) { | |
1499 | ulist_free(tmp); | |
1500 | return -ENOMEM; | |
1501 | } | |
1502 | ||
cd1b413c | 1503 | ULIST_ITER_INIT(&uiter); |
8da6d581 | 1504 | while (1) { |
afce772e | 1505 | ret = find_parent_nodes(trans, fs_info, bytenr, time_seq, |
c995ab3c | 1506 | tmp, *roots, NULL, NULL, ignore_offset); |
8da6d581 JS |
1507 | if (ret < 0 && ret != -ENOENT) { |
1508 | ulist_free(tmp); | |
1509 | ulist_free(*roots); | |
580c079b | 1510 | *roots = NULL; |
8da6d581 JS |
1511 | return ret; |
1512 | } | |
cd1b413c | 1513 | node = ulist_next(tmp, &uiter); |
8da6d581 JS |
1514 | if (!node) |
1515 | break; | |
1516 | bytenr = node->val; | |
bca1a290 | 1517 | cond_resched(); |
8da6d581 JS |
1518 | } |
1519 | ||
1520 | ulist_free(tmp); | |
1521 | return 0; | |
1522 | } | |
1523 | ||
9e351cc8 JB |
1524 | int btrfs_find_all_roots(struct btrfs_trans_handle *trans, |
1525 | struct btrfs_fs_info *fs_info, u64 bytenr, | |
c995ab3c | 1526 | u64 time_seq, struct ulist **roots, |
c7bcbb21 | 1527 | bool skip_commit_root_sem) |
9e351cc8 JB |
1528 | { |
1529 | int ret; | |
1530 | ||
8949b9a1 | 1531 | if (!trans && !skip_commit_root_sem) |
9e351cc8 | 1532 | down_read(&fs_info->commit_root_sem); |
e0c476b1 | 1533 | ret = btrfs_find_all_roots_safe(trans, fs_info, bytenr, |
c7bcbb21 | 1534 | time_seq, roots, false); |
8949b9a1 | 1535 | if (!trans && !skip_commit_root_sem) |
9e351cc8 JB |
1536 | up_read(&fs_info->commit_root_sem); |
1537 | return ret; | |
1538 | } | |
1539 | ||
12a824dc FM |
1540 | /* |
1541 | * The caller has joined a transaction or is holding a read lock on the | |
1542 | * fs_info->commit_root_sem semaphore, so no need to worry about the root's last | |
1543 | * snapshot field changing while updating or checking the cache. | |
1544 | */ | |
61dbb952 | 1545 | static bool lookup_backref_shared_cache(struct btrfs_backref_share_check_ctx *ctx, |
12a824dc FM |
1546 | struct btrfs_root *root, |
1547 | u64 bytenr, int level, bool *is_shared) | |
1548 | { | |
1549 | struct btrfs_backref_shared_cache_entry *entry; | |
1550 | ||
61dbb952 | 1551 | if (!ctx->use_path_cache) |
63c84b46 FM |
1552 | return false; |
1553 | ||
12a824dc FM |
1554 | if (WARN_ON_ONCE(level >= BTRFS_MAX_LEVEL)) |
1555 | return false; | |
1556 | ||
1557 | /* | |
1558 | * Level -1 is used for the data extent, which is not reliable to cache | |
1559 | * because its reference count can increase or decrease without us | |
1560 | * realizing. We cache results only for extent buffers that lead from | |
1561 | * the root node down to the leaf with the file extent item. | |
1562 | */ | |
1563 | ASSERT(level >= 0); | |
1564 | ||
61dbb952 | 1565 | entry = &ctx->path_cache_entries[level]; |
12a824dc FM |
1566 | |
1567 | /* Unused cache entry or being used for some other extent buffer. */ | |
1568 | if (entry->bytenr != bytenr) | |
1569 | return false; | |
1570 | ||
1571 | /* | |
1572 | * We cached a false result, but the last snapshot generation of the | |
1573 | * root changed, so we now have a snapshot. Don't trust the result. | |
1574 | */ | |
1575 | if (!entry->is_shared && | |
1576 | entry->gen != btrfs_root_last_snapshot(&root->root_item)) | |
1577 | return false; | |
1578 | ||
1579 | /* | |
1580 | * If we cached a true result and the last generation used for dropping | |
1581 | * a root changed, we can not trust the result, because the dropped root | |
1582 | * could be a snapshot sharing this extent buffer. | |
1583 | */ | |
1584 | if (entry->is_shared && | |
1585 | entry->gen != btrfs_get_last_root_drop_gen(root->fs_info)) | |
1586 | return false; | |
1587 | ||
1588 | *is_shared = entry->is_shared; | |
96dbcc00 FM |
1589 | /* |
1590 | * If the node at this level is shared, than all nodes below are also | |
1591 | * shared. Currently some of the nodes below may be marked as not shared | |
1592 | * because we have just switched from one leaf to another, and switched | |
1593 | * also other nodes above the leaf and below the current level, so mark | |
1594 | * them as shared. | |
1595 | */ | |
1596 | if (*is_shared) { | |
1597 | for (int i = 0; i < level; i++) { | |
61dbb952 FM |
1598 | ctx->path_cache_entries[i].is_shared = true; |
1599 | ctx->path_cache_entries[i].gen = entry->gen; | |
96dbcc00 FM |
1600 | } |
1601 | } | |
12a824dc FM |
1602 | |
1603 | return true; | |
1604 | } | |
1605 | ||
1606 | /* | |
1607 | * The caller has joined a transaction or is holding a read lock on the | |
1608 | * fs_info->commit_root_sem semaphore, so no need to worry about the root's last | |
1609 | * snapshot field changing while updating or checking the cache. | |
1610 | */ | |
61dbb952 | 1611 | static void store_backref_shared_cache(struct btrfs_backref_share_check_ctx *ctx, |
12a824dc FM |
1612 | struct btrfs_root *root, |
1613 | u64 bytenr, int level, bool is_shared) | |
1614 | { | |
1615 | struct btrfs_backref_shared_cache_entry *entry; | |
1616 | u64 gen; | |
1617 | ||
61dbb952 | 1618 | if (!ctx->use_path_cache) |
63c84b46 FM |
1619 | return; |
1620 | ||
12a824dc FM |
1621 | if (WARN_ON_ONCE(level >= BTRFS_MAX_LEVEL)) |
1622 | return; | |
1623 | ||
1624 | /* | |
1625 | * Level -1 is used for the data extent, which is not reliable to cache | |
1626 | * because its reference count can increase or decrease without us | |
1627 | * realizing. We cache results only for extent buffers that lead from | |
1628 | * the root node down to the leaf with the file extent item. | |
1629 | */ | |
1630 | ASSERT(level >= 0); | |
1631 | ||
1632 | if (is_shared) | |
1633 | gen = btrfs_get_last_root_drop_gen(root->fs_info); | |
1634 | else | |
1635 | gen = btrfs_root_last_snapshot(&root->root_item); | |
1636 | ||
61dbb952 | 1637 | entry = &ctx->path_cache_entries[level]; |
12a824dc FM |
1638 | entry->bytenr = bytenr; |
1639 | entry->is_shared = is_shared; | |
1640 | entry->gen = gen; | |
1641 | ||
1642 | /* | |
1643 | * If we found an extent buffer is shared, set the cache result for all | |
1644 | * extent buffers below it to true. As nodes in the path are COWed, | |
1645 | * their sharedness is moved to their children, and if a leaf is COWed, | |
1646 | * then the sharedness of a data extent becomes direct, the refcount of | |
1647 | * data extent is increased in the extent item at the extent tree. | |
1648 | */ | |
1649 | if (is_shared) { | |
1650 | for (int i = 0; i < level; i++) { | |
61dbb952 | 1651 | entry = &ctx->path_cache_entries[i]; |
12a824dc FM |
1652 | entry->is_shared = is_shared; |
1653 | entry->gen = gen; | |
1654 | } | |
1655 | } | |
1656 | } | |
1657 | ||
84a7949d FM |
1658 | struct btrfs_backref_share_check_ctx *btrfs_alloc_backref_share_check_ctx(void) |
1659 | { | |
1660 | struct btrfs_backref_share_check_ctx *ctx; | |
1661 | ||
1662 | ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); | |
1663 | if (!ctx) | |
1664 | return NULL; | |
1665 | ||
1666 | ulist_init(&ctx->refs); | |
84a7949d FM |
1667 | |
1668 | return ctx; | |
1669 | } | |
1670 | ||
1671 | void btrfs_free_backref_share_ctx(struct btrfs_backref_share_check_ctx *ctx) | |
1672 | { | |
1673 | if (!ctx) | |
1674 | return; | |
1675 | ||
1676 | ulist_release(&ctx->refs); | |
84a7949d FM |
1677 | kfree(ctx); |
1678 | } | |
1679 | ||
8eedadda FM |
1680 | /* |
1681 | * Check if a data extent is shared or not. | |
6e353e3b | 1682 | * |
ceb707da | 1683 | * @inode: The inode whose extent we are checking. |
b8f164e3 FM |
1684 | * @bytenr: Logical bytenr of the extent we are checking. |
1685 | * @extent_gen: Generation of the extent (file extent item) or 0 if it is | |
1686 | * not known. | |
61dbb952 | 1687 | * @ctx: A backref sharedness check context. |
2c2ed5aa | 1688 | * |
8eedadda | 1689 | * btrfs_is_data_extent_shared uses the backref walking code but will short |
2c2ed5aa MF |
1690 | * circuit as soon as it finds a root or inode that doesn't match the |
1691 | * one passed in. This provides a significant performance benefit for | |
1692 | * callers (such as fiemap) which want to know whether the extent is | |
1693 | * shared but do not need a ref count. | |
1694 | * | |
03628cdb FM |
1695 | * This attempts to attach to the running transaction in order to account for |
1696 | * delayed refs, but continues on even when no running transaction exists. | |
bb739cf0 | 1697 | * |
2c2ed5aa MF |
1698 | * Return: 0 if extent is not shared, 1 if it is shared, < 0 on error. |
1699 | */ | |
ceb707da | 1700 | int btrfs_is_data_extent_shared(struct btrfs_inode *inode, u64 bytenr, |
b8f164e3 | 1701 | u64 extent_gen, |
61dbb952 | 1702 | struct btrfs_backref_share_check_ctx *ctx) |
dc046b10 | 1703 | { |
ceb707da | 1704 | struct btrfs_root *root = inode->root; |
bb739cf0 EN |
1705 | struct btrfs_fs_info *fs_info = root->fs_info; |
1706 | struct btrfs_trans_handle *trans; | |
dc046b10 JB |
1707 | struct ulist_iterator uiter; |
1708 | struct ulist_node *node; | |
f3a84ccd | 1709 | struct btrfs_seq_list elem = BTRFS_SEQ_LIST_INIT(elem); |
dc046b10 | 1710 | int ret = 0; |
3ec4d323 | 1711 | struct share_check shared = { |
4fd786e6 | 1712 | .root_objectid = root->root_key.objectid, |
ceb707da | 1713 | .inum = btrfs_ino(inode), |
3ec4d323 | 1714 | .share_count = 0, |
4fc7b572 | 1715 | .have_delayed_delete_refs = false, |
3ec4d323 | 1716 | }; |
12a824dc | 1717 | int level; |
dc046b10 | 1718 | |
84a7949d | 1719 | ulist_init(&ctx->refs); |
dc046b10 | 1720 | |
a6d155d2 | 1721 | trans = btrfs_join_transaction_nostart(root); |
bb739cf0 | 1722 | if (IS_ERR(trans)) { |
03628cdb FM |
1723 | if (PTR_ERR(trans) != -ENOENT && PTR_ERR(trans) != -EROFS) { |
1724 | ret = PTR_ERR(trans); | |
1725 | goto out; | |
1726 | } | |
bb739cf0 | 1727 | trans = NULL; |
dc046b10 | 1728 | down_read(&fs_info->commit_root_sem); |
bb739cf0 EN |
1729 | } else { |
1730 | btrfs_get_tree_mod_seq(fs_info, &elem); | |
1731 | } | |
1732 | ||
12a824dc FM |
1733 | /* -1 means we are in the bytenr of the data extent. */ |
1734 | level = -1; | |
dc046b10 | 1735 | ULIST_ITER_INIT(&uiter); |
61dbb952 | 1736 | ctx->use_path_cache = true; |
dc046b10 | 1737 | while (1) { |
12a824dc FM |
1738 | bool is_shared; |
1739 | bool cached; | |
1740 | ||
84a7949d | 1741 | ret = find_parent_nodes(trans, fs_info, bytenr, elem.seq, &ctx->refs, |
b6296858 | 1742 | NULL, NULL, &shared, false); |
dc046b10 | 1743 | if (ret == BACKREF_FOUND_SHARED) { |
2c2ed5aa | 1744 | /* this is the only condition under which we return 1 */ |
dc046b10 | 1745 | ret = 1; |
12a824dc | 1746 | if (level >= 0) |
61dbb952 | 1747 | store_backref_shared_cache(ctx, root, bytenr, |
12a824dc | 1748 | level, true); |
dc046b10 JB |
1749 | break; |
1750 | } | |
1751 | if (ret < 0 && ret != -ENOENT) | |
1752 | break; | |
2c2ed5aa | 1753 | ret = 0; |
b8f164e3 FM |
1754 | /* |
1755 | * If our data extent is not shared through reflinks and it was | |
1756 | * created in a generation after the last one used to create a | |
1757 | * snapshot of the inode's root, then it can not be shared | |
1758 | * indirectly through subtrees, as that can only happen with | |
1759 | * snapshots. In this case bail out, no need to check for the | |
1760 | * sharedness of extent buffers. | |
1761 | */ | |
1762 | if (level == -1 && | |
1763 | extent_gen > btrfs_root_last_snapshot(&root->root_item)) | |
1764 | break; | |
1765 | ||
63c84b46 FM |
1766 | /* |
1767 | * If our data extent was not directly shared (without multiple | |
1768 | * reference items), than it might have a single reference item | |
1769 | * with a count > 1 for the same offset, which means there are 2 | |
1770 | * (or more) file extent items that point to the data extent - | |
1771 | * this happens when a file extent item needs to be split and | |
1772 | * then one item gets moved to another leaf due to a b+tree leaf | |
1773 | * split when inserting some item. In this case the file extent | |
1774 | * items may be located in different leaves and therefore some | |
1775 | * of the leaves may be referenced through shared subtrees while | |
1776 | * others are not. Since our extent buffer cache only works for | |
1777 | * a single path (by far the most common case and simpler to | |
1778 | * deal with), we can not use it if we have multiple leaves | |
1779 | * (which implies multiple paths). | |
1780 | */ | |
84a7949d | 1781 | if (level == -1 && ctx->refs.nnodes > 1) |
61dbb952 | 1782 | ctx->use_path_cache = false; |
63c84b46 | 1783 | |
12a824dc | 1784 | if (level >= 0) |
61dbb952 | 1785 | store_backref_shared_cache(ctx, root, bytenr, |
12a824dc | 1786 | level, false); |
84a7949d | 1787 | node = ulist_next(&ctx->refs, &uiter); |
dc046b10 JB |
1788 | if (!node) |
1789 | break; | |
1790 | bytenr = node->val; | |
12a824dc | 1791 | level++; |
61dbb952 | 1792 | cached = lookup_backref_shared_cache(ctx, root, bytenr, level, |
12a824dc FM |
1793 | &is_shared); |
1794 | if (cached) { | |
1795 | ret = (is_shared ? 1 : 0); | |
1796 | break; | |
1797 | } | |
18bf591b | 1798 | shared.share_count = 0; |
4fc7b572 | 1799 | shared.have_delayed_delete_refs = false; |
dc046b10 JB |
1800 | cond_resched(); |
1801 | } | |
bb739cf0 EN |
1802 | |
1803 | if (trans) { | |
dc046b10 | 1804 | btrfs_put_tree_mod_seq(fs_info, &elem); |
bb739cf0 EN |
1805 | btrfs_end_transaction(trans); |
1806 | } else { | |
dc046b10 | 1807 | up_read(&fs_info->commit_root_sem); |
bb739cf0 | 1808 | } |
03628cdb | 1809 | out: |
84a7949d | 1810 | ulist_release(&ctx->refs); |
dc046b10 JB |
1811 | return ret; |
1812 | } | |
1813 | ||
f186373f MF |
1814 | int btrfs_find_one_extref(struct btrfs_root *root, u64 inode_objectid, |
1815 | u64 start_off, struct btrfs_path *path, | |
1816 | struct btrfs_inode_extref **ret_extref, | |
1817 | u64 *found_off) | |
1818 | { | |
1819 | int ret, slot; | |
1820 | struct btrfs_key key; | |
1821 | struct btrfs_key found_key; | |
1822 | struct btrfs_inode_extref *extref; | |
73980bec | 1823 | const struct extent_buffer *leaf; |
f186373f MF |
1824 | unsigned long ptr; |
1825 | ||
1826 | key.objectid = inode_objectid; | |
962a298f | 1827 | key.type = BTRFS_INODE_EXTREF_KEY; |
f186373f MF |
1828 | key.offset = start_off; |
1829 | ||
1830 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
1831 | if (ret < 0) | |
1832 | return ret; | |
1833 | ||
1834 | while (1) { | |
1835 | leaf = path->nodes[0]; | |
1836 | slot = path->slots[0]; | |
1837 | if (slot >= btrfs_header_nritems(leaf)) { | |
1838 | /* | |
1839 | * If the item at offset is not found, | |
1840 | * btrfs_search_slot will point us to the slot | |
1841 | * where it should be inserted. In our case | |
1842 | * that will be the slot directly before the | |
1843 | * next INODE_REF_KEY_V2 item. In the case | |
1844 | * that we're pointing to the last slot in a | |
1845 | * leaf, we must move one leaf over. | |
1846 | */ | |
1847 | ret = btrfs_next_leaf(root, path); | |
1848 | if (ret) { | |
1849 | if (ret >= 1) | |
1850 | ret = -ENOENT; | |
1851 | break; | |
1852 | } | |
1853 | continue; | |
1854 | } | |
1855 | ||
1856 | btrfs_item_key_to_cpu(leaf, &found_key, slot); | |
1857 | ||
1858 | /* | |
1859 | * Check that we're still looking at an extended ref key for | |
1860 | * this particular objectid. If we have different | |
1861 | * objectid or type then there are no more to be found | |
1862 | * in the tree and we can exit. | |
1863 | */ | |
1864 | ret = -ENOENT; | |
1865 | if (found_key.objectid != inode_objectid) | |
1866 | break; | |
962a298f | 1867 | if (found_key.type != BTRFS_INODE_EXTREF_KEY) |
f186373f MF |
1868 | break; |
1869 | ||
1870 | ret = 0; | |
1871 | ptr = btrfs_item_ptr_offset(leaf, path->slots[0]); | |
1872 | extref = (struct btrfs_inode_extref *)ptr; | |
1873 | *ret_extref = extref; | |
1874 | if (found_off) | |
1875 | *found_off = found_key.offset; | |
1876 | break; | |
1877 | } | |
1878 | ||
1879 | return ret; | |
1880 | } | |
1881 | ||
48a3b636 ES |
1882 | /* |
1883 | * this iterates to turn a name (from iref/extref) into a full filesystem path. | |
1884 | * Elements of the path are separated by '/' and the path is guaranteed to be | |
1885 | * 0-terminated. the path is only given within the current file system. | |
1886 | * Therefore, it never starts with a '/'. the caller is responsible to provide | |
1887 | * "size" bytes in "dest". the dest buffer will be filled backwards. finally, | |
1888 | * the start point of the resulting string is returned. this pointer is within | |
1889 | * dest, normally. | |
1890 | * in case the path buffer would overflow, the pointer is decremented further | |
1891 | * as if output was written to the buffer, though no more output is actually | |
1892 | * generated. that way, the caller can determine how much space would be | |
1893 | * required for the path to fit into the buffer. in that case, the returned | |
1894 | * value will be smaller than dest. callers must check this! | |
1895 | */ | |
96b5bd77 JS |
1896 | char *btrfs_ref_to_path(struct btrfs_root *fs_root, struct btrfs_path *path, |
1897 | u32 name_len, unsigned long name_off, | |
1898 | struct extent_buffer *eb_in, u64 parent, | |
1899 | char *dest, u32 size) | |
a542ad1b | 1900 | { |
a542ad1b JS |
1901 | int slot; |
1902 | u64 next_inum; | |
1903 | int ret; | |
661bec6b | 1904 | s64 bytes_left = ((s64)size) - 1; |
a542ad1b JS |
1905 | struct extent_buffer *eb = eb_in; |
1906 | struct btrfs_key found_key; | |
d24bec3a | 1907 | struct btrfs_inode_ref *iref; |
a542ad1b JS |
1908 | |
1909 | if (bytes_left >= 0) | |
1910 | dest[bytes_left] = '\0'; | |
1911 | ||
1912 | while (1) { | |
d24bec3a | 1913 | bytes_left -= name_len; |
a542ad1b JS |
1914 | if (bytes_left >= 0) |
1915 | read_extent_buffer(eb, dest + bytes_left, | |
d24bec3a | 1916 | name_off, name_len); |
b916a59a | 1917 | if (eb != eb_in) { |
0c0fe3b0 | 1918 | if (!path->skip_locking) |
ac5887c8 | 1919 | btrfs_tree_read_unlock(eb); |
a542ad1b | 1920 | free_extent_buffer(eb); |
b916a59a | 1921 | } |
c234a24d DS |
1922 | ret = btrfs_find_item(fs_root, path, parent, 0, |
1923 | BTRFS_INODE_REF_KEY, &found_key); | |
8f24b496 JS |
1924 | if (ret > 0) |
1925 | ret = -ENOENT; | |
a542ad1b JS |
1926 | if (ret) |
1927 | break; | |
d24bec3a | 1928 | |
a542ad1b JS |
1929 | next_inum = found_key.offset; |
1930 | ||
1931 | /* regular exit ahead */ | |
1932 | if (parent == next_inum) | |
1933 | break; | |
1934 | ||
1935 | slot = path->slots[0]; | |
1936 | eb = path->nodes[0]; | |
1937 | /* make sure we can use eb after releasing the path */ | |
b916a59a | 1938 | if (eb != eb_in) { |
0c0fe3b0 FM |
1939 | path->nodes[0] = NULL; |
1940 | path->locks[0] = 0; | |
b916a59a | 1941 | } |
a542ad1b | 1942 | btrfs_release_path(path); |
a542ad1b | 1943 | iref = btrfs_item_ptr(eb, slot, struct btrfs_inode_ref); |
d24bec3a MF |
1944 | |
1945 | name_len = btrfs_inode_ref_name_len(eb, iref); | |
1946 | name_off = (unsigned long)(iref + 1); | |
1947 | ||
a542ad1b JS |
1948 | parent = next_inum; |
1949 | --bytes_left; | |
1950 | if (bytes_left >= 0) | |
1951 | dest[bytes_left] = '/'; | |
1952 | } | |
1953 | ||
1954 | btrfs_release_path(path); | |
1955 | ||
1956 | if (ret) | |
1957 | return ERR_PTR(ret); | |
1958 | ||
1959 | return dest + bytes_left; | |
1960 | } | |
1961 | ||
1962 | /* | |
1963 | * this makes the path point to (logical EXTENT_ITEM *) | |
1964 | * returns BTRFS_EXTENT_FLAG_DATA for data, BTRFS_EXTENT_FLAG_TREE_BLOCK for | |
1965 | * tree blocks and <0 on error. | |
1966 | */ | |
1967 | int extent_from_logical(struct btrfs_fs_info *fs_info, u64 logical, | |
69917e43 LB |
1968 | struct btrfs_path *path, struct btrfs_key *found_key, |
1969 | u64 *flags_ret) | |
a542ad1b | 1970 | { |
29cbcf40 | 1971 | struct btrfs_root *extent_root = btrfs_extent_root(fs_info, logical); |
a542ad1b JS |
1972 | int ret; |
1973 | u64 flags; | |
261c84b6 | 1974 | u64 size = 0; |
a542ad1b | 1975 | u32 item_size; |
73980bec | 1976 | const struct extent_buffer *eb; |
a542ad1b JS |
1977 | struct btrfs_extent_item *ei; |
1978 | struct btrfs_key key; | |
1979 | ||
261c84b6 JB |
1980 | if (btrfs_fs_incompat(fs_info, SKINNY_METADATA)) |
1981 | key.type = BTRFS_METADATA_ITEM_KEY; | |
1982 | else | |
1983 | key.type = BTRFS_EXTENT_ITEM_KEY; | |
a542ad1b JS |
1984 | key.objectid = logical; |
1985 | key.offset = (u64)-1; | |
1986 | ||
29cbcf40 | 1987 | ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0); |
a542ad1b JS |
1988 | if (ret < 0) |
1989 | return ret; | |
a542ad1b | 1990 | |
29cbcf40 | 1991 | ret = btrfs_previous_extent_item(extent_root, path, 0); |
850a8cdf WS |
1992 | if (ret) { |
1993 | if (ret > 0) | |
1994 | ret = -ENOENT; | |
1995 | return ret; | |
580f0a67 | 1996 | } |
850a8cdf | 1997 | btrfs_item_key_to_cpu(path->nodes[0], found_key, path->slots[0]); |
261c84b6 | 1998 | if (found_key->type == BTRFS_METADATA_ITEM_KEY) |
da17066c | 1999 | size = fs_info->nodesize; |
261c84b6 JB |
2000 | else if (found_key->type == BTRFS_EXTENT_ITEM_KEY) |
2001 | size = found_key->offset; | |
2002 | ||
580f0a67 | 2003 | if (found_key->objectid > logical || |
261c84b6 | 2004 | found_key->objectid + size <= logical) { |
ab8d0fc4 JM |
2005 | btrfs_debug(fs_info, |
2006 | "logical %llu is not within any extent", logical); | |
a542ad1b | 2007 | return -ENOENT; |
4692cf58 | 2008 | } |
a542ad1b JS |
2009 | |
2010 | eb = path->nodes[0]; | |
3212fa14 | 2011 | item_size = btrfs_item_size(eb, path->slots[0]); |
a542ad1b JS |
2012 | BUG_ON(item_size < sizeof(*ei)); |
2013 | ||
2014 | ei = btrfs_item_ptr(eb, path->slots[0], struct btrfs_extent_item); | |
2015 | flags = btrfs_extent_flags(eb, ei); | |
2016 | ||
ab8d0fc4 JM |
2017 | btrfs_debug(fs_info, |
2018 | "logical %llu is at position %llu within the extent (%llu EXTENT_ITEM %llu) flags %#llx size %u", | |
c1c9ff7c GU |
2019 | logical, logical - found_key->objectid, found_key->objectid, |
2020 | found_key->offset, flags, item_size); | |
69917e43 LB |
2021 | |
2022 | WARN_ON(!flags_ret); | |
2023 | if (flags_ret) { | |
2024 | if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) | |
2025 | *flags_ret = BTRFS_EXTENT_FLAG_TREE_BLOCK; | |
2026 | else if (flags & BTRFS_EXTENT_FLAG_DATA) | |
2027 | *flags_ret = BTRFS_EXTENT_FLAG_DATA; | |
2028 | else | |
290342f6 | 2029 | BUG(); |
69917e43 LB |
2030 | return 0; |
2031 | } | |
a542ad1b JS |
2032 | |
2033 | return -EIO; | |
2034 | } | |
2035 | ||
2036 | /* | |
2037 | * helper function to iterate extent inline refs. ptr must point to a 0 value | |
2038 | * for the first call and may be modified. it is used to track state. | |
2039 | * if more refs exist, 0 is returned and the next call to | |
e0c476b1 | 2040 | * get_extent_inline_ref must pass the modified ptr parameter to get the |
a542ad1b JS |
2041 | * next ref. after the last ref was processed, 1 is returned. |
2042 | * returns <0 on error | |
2043 | */ | |
e0c476b1 JM |
2044 | static int get_extent_inline_ref(unsigned long *ptr, |
2045 | const struct extent_buffer *eb, | |
2046 | const struct btrfs_key *key, | |
2047 | const struct btrfs_extent_item *ei, | |
2048 | u32 item_size, | |
2049 | struct btrfs_extent_inline_ref **out_eiref, | |
2050 | int *out_type) | |
a542ad1b JS |
2051 | { |
2052 | unsigned long end; | |
2053 | u64 flags; | |
2054 | struct btrfs_tree_block_info *info; | |
2055 | ||
2056 | if (!*ptr) { | |
2057 | /* first call */ | |
2058 | flags = btrfs_extent_flags(eb, ei); | |
2059 | if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) { | |
6eda71d0 LB |
2060 | if (key->type == BTRFS_METADATA_ITEM_KEY) { |
2061 | /* a skinny metadata extent */ | |
2062 | *out_eiref = | |
2063 | (struct btrfs_extent_inline_ref *)(ei + 1); | |
2064 | } else { | |
2065 | WARN_ON(key->type != BTRFS_EXTENT_ITEM_KEY); | |
2066 | info = (struct btrfs_tree_block_info *)(ei + 1); | |
2067 | *out_eiref = | |
2068 | (struct btrfs_extent_inline_ref *)(info + 1); | |
2069 | } | |
a542ad1b JS |
2070 | } else { |
2071 | *out_eiref = (struct btrfs_extent_inline_ref *)(ei + 1); | |
2072 | } | |
2073 | *ptr = (unsigned long)*out_eiref; | |
cd857dd6 | 2074 | if ((unsigned long)(*ptr) >= (unsigned long)ei + item_size) |
a542ad1b JS |
2075 | return -ENOENT; |
2076 | } | |
2077 | ||
2078 | end = (unsigned long)ei + item_size; | |
6eda71d0 | 2079 | *out_eiref = (struct btrfs_extent_inline_ref *)(*ptr); |
3de28d57 LB |
2080 | *out_type = btrfs_get_extent_inline_ref_type(eb, *out_eiref, |
2081 | BTRFS_REF_TYPE_ANY); | |
2082 | if (*out_type == BTRFS_REF_TYPE_INVALID) | |
af431dcb | 2083 | return -EUCLEAN; |
a542ad1b JS |
2084 | |
2085 | *ptr += btrfs_extent_inline_ref_size(*out_type); | |
2086 | WARN_ON(*ptr > end); | |
2087 | if (*ptr == end) | |
2088 | return 1; /* last */ | |
2089 | ||
2090 | return 0; | |
2091 | } | |
2092 | ||
2093 | /* | |
2094 | * reads the tree block backref for an extent. tree level and root are returned | |
2095 | * through out_level and out_root. ptr must point to a 0 value for the first | |
e0c476b1 | 2096 | * call and may be modified (see get_extent_inline_ref comment). |
a542ad1b JS |
2097 | * returns 0 if data was provided, 1 if there was no more data to provide or |
2098 | * <0 on error. | |
2099 | */ | |
2100 | int tree_backref_for_extent(unsigned long *ptr, struct extent_buffer *eb, | |
6eda71d0 LB |
2101 | struct btrfs_key *key, struct btrfs_extent_item *ei, |
2102 | u32 item_size, u64 *out_root, u8 *out_level) | |
a542ad1b JS |
2103 | { |
2104 | int ret; | |
2105 | int type; | |
a542ad1b JS |
2106 | struct btrfs_extent_inline_ref *eiref; |
2107 | ||
2108 | if (*ptr == (unsigned long)-1) | |
2109 | return 1; | |
2110 | ||
2111 | while (1) { | |
e0c476b1 | 2112 | ret = get_extent_inline_ref(ptr, eb, key, ei, item_size, |
6eda71d0 | 2113 | &eiref, &type); |
a542ad1b JS |
2114 | if (ret < 0) |
2115 | return ret; | |
2116 | ||
2117 | if (type == BTRFS_TREE_BLOCK_REF_KEY || | |
2118 | type == BTRFS_SHARED_BLOCK_REF_KEY) | |
2119 | break; | |
2120 | ||
2121 | if (ret == 1) | |
2122 | return 1; | |
2123 | } | |
2124 | ||
2125 | /* we can treat both ref types equally here */ | |
a542ad1b | 2126 | *out_root = btrfs_extent_inline_ref_offset(eb, eiref); |
a1317f45 FM |
2127 | |
2128 | if (key->type == BTRFS_EXTENT_ITEM_KEY) { | |
2129 | struct btrfs_tree_block_info *info; | |
2130 | ||
2131 | info = (struct btrfs_tree_block_info *)(ei + 1); | |
2132 | *out_level = btrfs_tree_block_level(eb, info); | |
2133 | } else { | |
2134 | ASSERT(key->type == BTRFS_METADATA_ITEM_KEY); | |
2135 | *out_level = (u8)key->offset; | |
2136 | } | |
a542ad1b JS |
2137 | |
2138 | if (ret == 1) | |
2139 | *ptr = (unsigned long)-1; | |
2140 | ||
2141 | return 0; | |
2142 | } | |
2143 | ||
ab8d0fc4 JM |
2144 | static int iterate_leaf_refs(struct btrfs_fs_info *fs_info, |
2145 | struct extent_inode_elem *inode_list, | |
2146 | u64 root, u64 extent_item_objectid, | |
2147 | iterate_extent_inodes_t *iterate, void *ctx) | |
a542ad1b | 2148 | { |
976b1908 | 2149 | struct extent_inode_elem *eie; |
4692cf58 | 2150 | int ret = 0; |
4692cf58 | 2151 | |
976b1908 | 2152 | for (eie = inode_list; eie; eie = eie->next) { |
ab8d0fc4 JM |
2153 | btrfs_debug(fs_info, |
2154 | "ref for %llu resolved, key (%llu EXTEND_DATA %llu), root %llu", | |
2155 | extent_item_objectid, eie->inum, | |
2156 | eie->offset, root); | |
976b1908 | 2157 | ret = iterate(eie->inum, eie->offset, root, ctx); |
4692cf58 | 2158 | if (ret) { |
ab8d0fc4 JM |
2159 | btrfs_debug(fs_info, |
2160 | "stopping iteration for %llu due to ret=%d", | |
2161 | extent_item_objectid, ret); | |
4692cf58 JS |
2162 | break; |
2163 | } | |
a542ad1b JS |
2164 | } |
2165 | ||
a542ad1b JS |
2166 | return ret; |
2167 | } | |
2168 | ||
2169 | /* | |
2170 | * calls iterate() for every inode that references the extent identified by | |
4692cf58 | 2171 | * the given parameters. |
a542ad1b JS |
2172 | * when the iterator function returns a non-zero value, iteration stops. |
2173 | */ | |
2174 | int iterate_extent_inodes(struct btrfs_fs_info *fs_info, | |
4692cf58 | 2175 | u64 extent_item_objectid, u64 extent_item_pos, |
7a3ae2f8 | 2176 | int search_commit_root, |
c995ab3c ZB |
2177 | iterate_extent_inodes_t *iterate, void *ctx, |
2178 | bool ignore_offset) | |
a542ad1b | 2179 | { |
a542ad1b | 2180 | int ret; |
da61d31a | 2181 | struct btrfs_trans_handle *trans = NULL; |
7a3ae2f8 JS |
2182 | struct ulist *refs = NULL; |
2183 | struct ulist *roots = NULL; | |
4692cf58 JS |
2184 | struct ulist_node *ref_node = NULL; |
2185 | struct ulist_node *root_node = NULL; | |
f3a84ccd | 2186 | struct btrfs_seq_list seq_elem = BTRFS_SEQ_LIST_INIT(seq_elem); |
cd1b413c JS |
2187 | struct ulist_iterator ref_uiter; |
2188 | struct ulist_iterator root_uiter; | |
a542ad1b | 2189 | |
ab8d0fc4 | 2190 | btrfs_debug(fs_info, "resolving all inodes for extent %llu", |
4692cf58 | 2191 | extent_item_objectid); |
a542ad1b | 2192 | |
da61d31a | 2193 | if (!search_commit_root) { |
30a9da5d | 2194 | trans = btrfs_attach_transaction(fs_info->tree_root); |
bfc61c36 FM |
2195 | if (IS_ERR(trans)) { |
2196 | if (PTR_ERR(trans) != -ENOENT && | |
2197 | PTR_ERR(trans) != -EROFS) | |
2198 | return PTR_ERR(trans); | |
2199 | trans = NULL; | |
2200 | } | |
2201 | } | |
2202 | ||
2203 | if (trans) | |
f3a84ccd | 2204 | btrfs_get_tree_mod_seq(fs_info, &seq_elem); |
bfc61c36 | 2205 | else |
9e351cc8 | 2206 | down_read(&fs_info->commit_root_sem); |
a542ad1b | 2207 | |
4692cf58 | 2208 | ret = btrfs_find_all_leafs(trans, fs_info, extent_item_objectid, |
f3a84ccd | 2209 | seq_elem.seq, &refs, |
c995ab3c | 2210 | &extent_item_pos, ignore_offset); |
4692cf58 JS |
2211 | if (ret) |
2212 | goto out; | |
a542ad1b | 2213 | |
cd1b413c JS |
2214 | ULIST_ITER_INIT(&ref_uiter); |
2215 | while (!ret && (ref_node = ulist_next(refs, &ref_uiter))) { | |
e0c476b1 | 2216 | ret = btrfs_find_all_roots_safe(trans, fs_info, ref_node->val, |
f3a84ccd | 2217 | seq_elem.seq, &roots, |
c995ab3c | 2218 | ignore_offset); |
4692cf58 JS |
2219 | if (ret) |
2220 | break; | |
cd1b413c JS |
2221 | ULIST_ITER_INIT(&root_uiter); |
2222 | while (!ret && (root_node = ulist_next(roots, &root_uiter))) { | |
ab8d0fc4 JM |
2223 | btrfs_debug(fs_info, |
2224 | "root %llu references leaf %llu, data list %#llx", | |
2225 | root_node->val, ref_node->val, | |
2226 | ref_node->aux); | |
2227 | ret = iterate_leaf_refs(fs_info, | |
2228 | (struct extent_inode_elem *) | |
995e01b7 JS |
2229 | (uintptr_t)ref_node->aux, |
2230 | root_node->val, | |
2231 | extent_item_objectid, | |
2232 | iterate, ctx); | |
4692cf58 | 2233 | } |
976b1908 | 2234 | ulist_free(roots); |
a542ad1b JS |
2235 | } |
2236 | ||
976b1908 | 2237 | free_leaf_list(refs); |
4692cf58 | 2238 | out: |
bfc61c36 | 2239 | if (trans) { |
f3a84ccd | 2240 | btrfs_put_tree_mod_seq(fs_info, &seq_elem); |
3a45bb20 | 2241 | btrfs_end_transaction(trans); |
9e351cc8 JB |
2242 | } else { |
2243 | up_read(&fs_info->commit_root_sem); | |
7a3ae2f8 JS |
2244 | } |
2245 | ||
a542ad1b JS |
2246 | return ret; |
2247 | } | |
2248 | ||
e3059ec0 DS |
2249 | static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx) |
2250 | { | |
2251 | struct btrfs_data_container *inodes = ctx; | |
2252 | const size_t c = 3 * sizeof(u64); | |
2253 | ||
2254 | if (inodes->bytes_left >= c) { | |
2255 | inodes->bytes_left -= c; | |
2256 | inodes->val[inodes->elem_cnt] = inum; | |
2257 | inodes->val[inodes->elem_cnt + 1] = offset; | |
2258 | inodes->val[inodes->elem_cnt + 2] = root; | |
2259 | inodes->elem_cnt += 3; | |
2260 | } else { | |
2261 | inodes->bytes_missing += c - inodes->bytes_left; | |
2262 | inodes->bytes_left = 0; | |
2263 | inodes->elem_missed += 3; | |
2264 | } | |
2265 | ||
2266 | return 0; | |
2267 | } | |
2268 | ||
a542ad1b JS |
2269 | int iterate_inodes_from_logical(u64 logical, struct btrfs_fs_info *fs_info, |
2270 | struct btrfs_path *path, | |
e3059ec0 | 2271 | void *ctx, bool ignore_offset) |
a542ad1b JS |
2272 | { |
2273 | int ret; | |
4692cf58 | 2274 | u64 extent_item_pos; |
69917e43 | 2275 | u64 flags = 0; |
a542ad1b | 2276 | struct btrfs_key found_key; |
7a3ae2f8 | 2277 | int search_commit_root = path->search_commit_root; |
a542ad1b | 2278 | |
69917e43 | 2279 | ret = extent_from_logical(fs_info, logical, path, &found_key, &flags); |
4692cf58 | 2280 | btrfs_release_path(path); |
a542ad1b JS |
2281 | if (ret < 0) |
2282 | return ret; | |
69917e43 | 2283 | if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) |
3627bf45 | 2284 | return -EINVAL; |
a542ad1b | 2285 | |
4692cf58 | 2286 | extent_item_pos = logical - found_key.objectid; |
7a3ae2f8 JS |
2287 | ret = iterate_extent_inodes(fs_info, found_key.objectid, |
2288 | extent_item_pos, search_commit_root, | |
e3059ec0 | 2289 | build_ino_list, ctx, ignore_offset); |
a542ad1b JS |
2290 | |
2291 | return ret; | |
2292 | } | |
2293 | ||
ad6240f6 | 2294 | static int inode_to_path(u64 inum, u32 name_len, unsigned long name_off, |
875d1daa | 2295 | struct extent_buffer *eb, struct inode_fs_paths *ipath); |
d24bec3a | 2296 | |
875d1daa | 2297 | static int iterate_inode_refs(u64 inum, struct inode_fs_paths *ipath) |
a542ad1b | 2298 | { |
aefc1eb1 | 2299 | int ret = 0; |
a542ad1b JS |
2300 | int slot; |
2301 | u32 cur; | |
2302 | u32 len; | |
2303 | u32 name_len; | |
2304 | u64 parent = 0; | |
2305 | int found = 0; | |
875d1daa DS |
2306 | struct btrfs_root *fs_root = ipath->fs_root; |
2307 | struct btrfs_path *path = ipath->btrfs_path; | |
a542ad1b | 2308 | struct extent_buffer *eb; |
a542ad1b JS |
2309 | struct btrfs_inode_ref *iref; |
2310 | struct btrfs_key found_key; | |
2311 | ||
aefc1eb1 | 2312 | while (!ret) { |
c234a24d DS |
2313 | ret = btrfs_find_item(fs_root, path, inum, |
2314 | parent ? parent + 1 : 0, BTRFS_INODE_REF_KEY, | |
2315 | &found_key); | |
2316 | ||
a542ad1b JS |
2317 | if (ret < 0) |
2318 | break; | |
2319 | if (ret) { | |
2320 | ret = found ? 0 : -ENOENT; | |
2321 | break; | |
2322 | } | |
2323 | ++found; | |
2324 | ||
2325 | parent = found_key.offset; | |
2326 | slot = path->slots[0]; | |
3fe81ce2 FDBM |
2327 | eb = btrfs_clone_extent_buffer(path->nodes[0]); |
2328 | if (!eb) { | |
2329 | ret = -ENOMEM; | |
2330 | break; | |
2331 | } | |
a542ad1b JS |
2332 | btrfs_release_path(path); |
2333 | ||
a542ad1b JS |
2334 | iref = btrfs_item_ptr(eb, slot, struct btrfs_inode_ref); |
2335 | ||
3212fa14 | 2336 | for (cur = 0; cur < btrfs_item_size(eb, slot); cur += len) { |
a542ad1b JS |
2337 | name_len = btrfs_inode_ref_name_len(eb, iref); |
2338 | /* path must be released before calling iterate()! */ | |
ab8d0fc4 JM |
2339 | btrfs_debug(fs_root->fs_info, |
2340 | "following ref at offset %u for inode %llu in tree %llu", | |
4fd786e6 MT |
2341 | cur, found_key.objectid, |
2342 | fs_root->root_key.objectid); | |
ad6240f6 | 2343 | ret = inode_to_path(parent, name_len, |
875d1daa | 2344 | (unsigned long)(iref + 1), eb, ipath); |
aefc1eb1 | 2345 | if (ret) |
a542ad1b | 2346 | break; |
a542ad1b JS |
2347 | len = sizeof(*iref) + name_len; |
2348 | iref = (struct btrfs_inode_ref *)((char *)iref + len); | |
2349 | } | |
2350 | free_extent_buffer(eb); | |
2351 | } | |
2352 | ||
2353 | btrfs_release_path(path); | |
2354 | ||
2355 | return ret; | |
2356 | } | |
2357 | ||
875d1daa | 2358 | static int iterate_inode_extrefs(u64 inum, struct inode_fs_paths *ipath) |
d24bec3a MF |
2359 | { |
2360 | int ret; | |
2361 | int slot; | |
2362 | u64 offset = 0; | |
2363 | u64 parent; | |
2364 | int found = 0; | |
875d1daa DS |
2365 | struct btrfs_root *fs_root = ipath->fs_root; |
2366 | struct btrfs_path *path = ipath->btrfs_path; | |
d24bec3a MF |
2367 | struct extent_buffer *eb; |
2368 | struct btrfs_inode_extref *extref; | |
d24bec3a MF |
2369 | u32 item_size; |
2370 | u32 cur_offset; | |
2371 | unsigned long ptr; | |
2372 | ||
2373 | while (1) { | |
2374 | ret = btrfs_find_one_extref(fs_root, inum, offset, path, &extref, | |
2375 | &offset); | |
2376 | if (ret < 0) | |
2377 | break; | |
2378 | if (ret) { | |
2379 | ret = found ? 0 : -ENOENT; | |
2380 | break; | |
2381 | } | |
2382 | ++found; | |
2383 | ||
2384 | slot = path->slots[0]; | |
3fe81ce2 FDBM |
2385 | eb = btrfs_clone_extent_buffer(path->nodes[0]); |
2386 | if (!eb) { | |
2387 | ret = -ENOMEM; | |
2388 | break; | |
2389 | } | |
d24bec3a MF |
2390 | btrfs_release_path(path); |
2391 | ||
3212fa14 | 2392 | item_size = btrfs_item_size(eb, slot); |
2849a854 | 2393 | ptr = btrfs_item_ptr_offset(eb, slot); |
d24bec3a MF |
2394 | cur_offset = 0; |
2395 | ||
2396 | while (cur_offset < item_size) { | |
2397 | u32 name_len; | |
2398 | ||
2399 | extref = (struct btrfs_inode_extref *)(ptr + cur_offset); | |
2400 | parent = btrfs_inode_extref_parent(eb, extref); | |
2401 | name_len = btrfs_inode_extref_name_len(eb, extref); | |
ad6240f6 | 2402 | ret = inode_to_path(parent, name_len, |
875d1daa | 2403 | (unsigned long)&extref->name, eb, ipath); |
d24bec3a MF |
2404 | if (ret) |
2405 | break; | |
2406 | ||
2849a854 | 2407 | cur_offset += btrfs_inode_extref_name_len(eb, extref); |
d24bec3a MF |
2408 | cur_offset += sizeof(*extref); |
2409 | } | |
d24bec3a MF |
2410 | free_extent_buffer(eb); |
2411 | ||
2412 | offset++; | |
2413 | } | |
2414 | ||
2415 | btrfs_release_path(path); | |
2416 | ||
2417 | return ret; | |
2418 | } | |
2419 | ||
a542ad1b JS |
2420 | /* |
2421 | * returns 0 if the path could be dumped (probably truncated) | |
2422 | * returns <0 in case of an error | |
2423 | */ | |
d24bec3a | 2424 | static int inode_to_path(u64 inum, u32 name_len, unsigned long name_off, |
875d1daa | 2425 | struct extent_buffer *eb, struct inode_fs_paths *ipath) |
a542ad1b | 2426 | { |
a542ad1b JS |
2427 | char *fspath; |
2428 | char *fspath_min; | |
2429 | int i = ipath->fspath->elem_cnt; | |
2430 | const int s_ptr = sizeof(char *); | |
2431 | u32 bytes_left; | |
2432 | ||
2433 | bytes_left = ipath->fspath->bytes_left > s_ptr ? | |
2434 | ipath->fspath->bytes_left - s_ptr : 0; | |
2435 | ||
740c3d22 | 2436 | fspath_min = (char *)ipath->fspath->val + (i + 1) * s_ptr; |
96b5bd77 JS |
2437 | fspath = btrfs_ref_to_path(ipath->fs_root, ipath->btrfs_path, name_len, |
2438 | name_off, eb, inum, fspath_min, bytes_left); | |
a542ad1b JS |
2439 | if (IS_ERR(fspath)) |
2440 | return PTR_ERR(fspath); | |
2441 | ||
2442 | if (fspath > fspath_min) { | |
745c4d8e | 2443 | ipath->fspath->val[i] = (u64)(unsigned long)fspath; |
a542ad1b JS |
2444 | ++ipath->fspath->elem_cnt; |
2445 | ipath->fspath->bytes_left = fspath - fspath_min; | |
2446 | } else { | |
2447 | ++ipath->fspath->elem_missed; | |
2448 | ipath->fspath->bytes_missing += fspath_min - fspath; | |
2449 | ipath->fspath->bytes_left = 0; | |
2450 | } | |
2451 | ||
2452 | return 0; | |
2453 | } | |
2454 | ||
2455 | /* | |
2456 | * this dumps all file system paths to the inode into the ipath struct, provided | |
2457 | * is has been created large enough. each path is zero-terminated and accessed | |
740c3d22 | 2458 | * from ipath->fspath->val[i]. |
a542ad1b | 2459 | * when it returns, there are ipath->fspath->elem_cnt number of paths available |
740c3d22 | 2460 | * in ipath->fspath->val[]. when the allocated space wasn't sufficient, the |
01327610 | 2461 | * number of missed paths is recorded in ipath->fspath->elem_missed, otherwise, |
a542ad1b JS |
2462 | * it's zero. ipath->fspath->bytes_missing holds the number of bytes that would |
2463 | * have been needed to return all paths. | |
2464 | */ | |
2465 | int paths_from_inode(u64 inum, struct inode_fs_paths *ipath) | |
2466 | { | |
ad6240f6 DS |
2467 | int ret; |
2468 | int found_refs = 0; | |
2469 | ||
875d1daa | 2470 | ret = iterate_inode_refs(inum, ipath); |
ad6240f6 DS |
2471 | if (!ret) |
2472 | ++found_refs; | |
2473 | else if (ret != -ENOENT) | |
2474 | return ret; | |
2475 | ||
875d1daa | 2476 | ret = iterate_inode_extrefs(inum, ipath); |
ad6240f6 DS |
2477 | if (ret == -ENOENT && found_refs) |
2478 | return 0; | |
2479 | ||
2480 | return ret; | |
a542ad1b JS |
2481 | } |
2482 | ||
a542ad1b JS |
2483 | struct btrfs_data_container *init_data_container(u32 total_bytes) |
2484 | { | |
2485 | struct btrfs_data_container *data; | |
2486 | size_t alloc_bytes; | |
2487 | ||
2488 | alloc_bytes = max_t(size_t, total_bytes, sizeof(*data)); | |
f54de068 | 2489 | data = kvmalloc(alloc_bytes, GFP_KERNEL); |
a542ad1b JS |
2490 | if (!data) |
2491 | return ERR_PTR(-ENOMEM); | |
2492 | ||
2493 | if (total_bytes >= sizeof(*data)) { | |
2494 | data->bytes_left = total_bytes - sizeof(*data); | |
2495 | data->bytes_missing = 0; | |
2496 | } else { | |
2497 | data->bytes_missing = sizeof(*data) - total_bytes; | |
2498 | data->bytes_left = 0; | |
2499 | } | |
2500 | ||
2501 | data->elem_cnt = 0; | |
2502 | data->elem_missed = 0; | |
2503 | ||
2504 | return data; | |
2505 | } | |
2506 | ||
2507 | /* | |
2508 | * allocates space to return multiple file system paths for an inode. | |
2509 | * total_bytes to allocate are passed, note that space usable for actual path | |
2510 | * information will be total_bytes - sizeof(struct inode_fs_paths). | |
2511 | * the returned pointer must be freed with free_ipath() in the end. | |
2512 | */ | |
2513 | struct inode_fs_paths *init_ipath(s32 total_bytes, struct btrfs_root *fs_root, | |
2514 | struct btrfs_path *path) | |
2515 | { | |
2516 | struct inode_fs_paths *ifp; | |
2517 | struct btrfs_data_container *fspath; | |
2518 | ||
2519 | fspath = init_data_container(total_bytes); | |
2520 | if (IS_ERR(fspath)) | |
afc6961f | 2521 | return ERR_CAST(fspath); |
a542ad1b | 2522 | |
f54de068 | 2523 | ifp = kmalloc(sizeof(*ifp), GFP_KERNEL); |
a542ad1b | 2524 | if (!ifp) { |
f54de068 | 2525 | kvfree(fspath); |
a542ad1b JS |
2526 | return ERR_PTR(-ENOMEM); |
2527 | } | |
2528 | ||
2529 | ifp->btrfs_path = path; | |
2530 | ifp->fspath = fspath; | |
2531 | ifp->fs_root = fs_root; | |
2532 | ||
2533 | return ifp; | |
2534 | } | |
2535 | ||
2536 | void free_ipath(struct inode_fs_paths *ipath) | |
2537 | { | |
4735fb28 JJ |
2538 | if (!ipath) |
2539 | return; | |
f54de068 | 2540 | kvfree(ipath->fspath); |
a542ad1b JS |
2541 | kfree(ipath); |
2542 | } | |
a37f232b QW |
2543 | |
2544 | struct btrfs_backref_iter *btrfs_backref_iter_alloc( | |
2545 | struct btrfs_fs_info *fs_info, gfp_t gfp_flag) | |
2546 | { | |
2547 | struct btrfs_backref_iter *ret; | |
2548 | ||
2549 | ret = kzalloc(sizeof(*ret), gfp_flag); | |
2550 | if (!ret) | |
2551 | return NULL; | |
2552 | ||
2553 | ret->path = btrfs_alloc_path(); | |
c15c2ec0 | 2554 | if (!ret->path) { |
a37f232b QW |
2555 | kfree(ret); |
2556 | return NULL; | |
2557 | } | |
2558 | ||
2559 | /* Current backref iterator only supports iteration in commit root */ | |
2560 | ret->path->search_commit_root = 1; | |
2561 | ret->path->skip_locking = 1; | |
2562 | ret->fs_info = fs_info; | |
2563 | ||
2564 | return ret; | |
2565 | } | |
2566 | ||
2567 | int btrfs_backref_iter_start(struct btrfs_backref_iter *iter, u64 bytenr) | |
2568 | { | |
2569 | struct btrfs_fs_info *fs_info = iter->fs_info; | |
29cbcf40 | 2570 | struct btrfs_root *extent_root = btrfs_extent_root(fs_info, bytenr); |
a37f232b QW |
2571 | struct btrfs_path *path = iter->path; |
2572 | struct btrfs_extent_item *ei; | |
2573 | struct btrfs_key key; | |
2574 | int ret; | |
2575 | ||
2576 | key.objectid = bytenr; | |
2577 | key.type = BTRFS_METADATA_ITEM_KEY; | |
2578 | key.offset = (u64)-1; | |
2579 | iter->bytenr = bytenr; | |
2580 | ||
29cbcf40 | 2581 | ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0); |
a37f232b QW |
2582 | if (ret < 0) |
2583 | return ret; | |
2584 | if (ret == 0) { | |
2585 | ret = -EUCLEAN; | |
2586 | goto release; | |
2587 | } | |
2588 | if (path->slots[0] == 0) { | |
2589 | WARN_ON(IS_ENABLED(CONFIG_BTRFS_DEBUG)); | |
2590 | ret = -EUCLEAN; | |
2591 | goto release; | |
2592 | } | |
2593 | path->slots[0]--; | |
2594 | ||
2595 | btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); | |
2596 | if ((key.type != BTRFS_EXTENT_ITEM_KEY && | |
2597 | key.type != BTRFS_METADATA_ITEM_KEY) || key.objectid != bytenr) { | |
2598 | ret = -ENOENT; | |
2599 | goto release; | |
2600 | } | |
2601 | memcpy(&iter->cur_key, &key, sizeof(key)); | |
2602 | iter->item_ptr = (u32)btrfs_item_ptr_offset(path->nodes[0], | |
2603 | path->slots[0]); | |
2604 | iter->end_ptr = (u32)(iter->item_ptr + | |
3212fa14 | 2605 | btrfs_item_size(path->nodes[0], path->slots[0])); |
a37f232b QW |
2606 | ei = btrfs_item_ptr(path->nodes[0], path->slots[0], |
2607 | struct btrfs_extent_item); | |
2608 | ||
2609 | /* | |
2610 | * Only support iteration on tree backref yet. | |
2611 | * | |
2612 | * This is an extra precaution for non skinny-metadata, where | |
2613 | * EXTENT_ITEM is also used for tree blocks, that we can only use | |
2614 | * extent flags to determine if it's a tree block. | |
2615 | */ | |
2616 | if (btrfs_extent_flags(path->nodes[0], ei) & BTRFS_EXTENT_FLAG_DATA) { | |
2617 | ret = -ENOTSUPP; | |
2618 | goto release; | |
2619 | } | |
2620 | iter->cur_ptr = (u32)(iter->item_ptr + sizeof(*ei)); | |
2621 | ||
2622 | /* If there is no inline backref, go search for keyed backref */ | |
2623 | if (iter->cur_ptr >= iter->end_ptr) { | |
29cbcf40 | 2624 | ret = btrfs_next_item(extent_root, path); |
a37f232b QW |
2625 | |
2626 | /* No inline nor keyed ref */ | |
2627 | if (ret > 0) { | |
2628 | ret = -ENOENT; | |
2629 | goto release; | |
2630 | } | |
2631 | if (ret < 0) | |
2632 | goto release; | |
2633 | ||
2634 | btrfs_item_key_to_cpu(path->nodes[0], &iter->cur_key, | |
2635 | path->slots[0]); | |
2636 | if (iter->cur_key.objectid != bytenr || | |
2637 | (iter->cur_key.type != BTRFS_SHARED_BLOCK_REF_KEY && | |
2638 | iter->cur_key.type != BTRFS_TREE_BLOCK_REF_KEY)) { | |
2639 | ret = -ENOENT; | |
2640 | goto release; | |
2641 | } | |
2642 | iter->cur_ptr = (u32)btrfs_item_ptr_offset(path->nodes[0], | |
2643 | path->slots[0]); | |
2644 | iter->item_ptr = iter->cur_ptr; | |
3212fa14 | 2645 | iter->end_ptr = (u32)(iter->item_ptr + btrfs_item_size( |
a37f232b QW |
2646 | path->nodes[0], path->slots[0])); |
2647 | } | |
2648 | ||
2649 | return 0; | |
2650 | release: | |
2651 | btrfs_backref_iter_release(iter); | |
2652 | return ret; | |
2653 | } | |
c39c2ddc QW |
2654 | |
2655 | /* | |
2656 | * Go to the next backref item of current bytenr, can be either inlined or | |
2657 | * keyed. | |
2658 | * | |
2659 | * Caller needs to check whether it's inline ref or not by iter->cur_key. | |
2660 | * | |
2661 | * Return 0 if we get next backref without problem. | |
2662 | * Return >0 if there is no extra backref for this bytenr. | |
2663 | * Return <0 if there is something wrong happened. | |
2664 | */ | |
2665 | int btrfs_backref_iter_next(struct btrfs_backref_iter *iter) | |
2666 | { | |
2667 | struct extent_buffer *eb = btrfs_backref_get_eb(iter); | |
29cbcf40 | 2668 | struct btrfs_root *extent_root; |
c39c2ddc QW |
2669 | struct btrfs_path *path = iter->path; |
2670 | struct btrfs_extent_inline_ref *iref; | |
2671 | int ret; | |
2672 | u32 size; | |
2673 | ||
2674 | if (btrfs_backref_iter_is_inline_ref(iter)) { | |
2675 | /* We're still inside the inline refs */ | |
2676 | ASSERT(iter->cur_ptr < iter->end_ptr); | |
2677 | ||
2678 | if (btrfs_backref_has_tree_block_info(iter)) { | |
2679 | /* First tree block info */ | |
2680 | size = sizeof(struct btrfs_tree_block_info); | |
2681 | } else { | |
2682 | /* Use inline ref type to determine the size */ | |
2683 | int type; | |
2684 | ||
2685 | iref = (struct btrfs_extent_inline_ref *) | |
2686 | ((unsigned long)iter->cur_ptr); | |
2687 | type = btrfs_extent_inline_ref_type(eb, iref); | |
2688 | ||
2689 | size = btrfs_extent_inline_ref_size(type); | |
2690 | } | |
2691 | iter->cur_ptr += size; | |
2692 | if (iter->cur_ptr < iter->end_ptr) | |
2693 | return 0; | |
2694 | ||
2695 | /* All inline items iterated, fall through */ | |
2696 | } | |
2697 | ||
2698 | /* We're at keyed items, there is no inline item, go to the next one */ | |
29cbcf40 JB |
2699 | extent_root = btrfs_extent_root(iter->fs_info, iter->bytenr); |
2700 | ret = btrfs_next_item(extent_root, iter->path); | |
c39c2ddc QW |
2701 | if (ret) |
2702 | return ret; | |
2703 | ||
2704 | btrfs_item_key_to_cpu(path->nodes[0], &iter->cur_key, path->slots[0]); | |
2705 | if (iter->cur_key.objectid != iter->bytenr || | |
2706 | (iter->cur_key.type != BTRFS_TREE_BLOCK_REF_KEY && | |
2707 | iter->cur_key.type != BTRFS_SHARED_BLOCK_REF_KEY)) | |
2708 | return 1; | |
2709 | iter->item_ptr = (u32)btrfs_item_ptr_offset(path->nodes[0], | |
2710 | path->slots[0]); | |
2711 | iter->cur_ptr = iter->item_ptr; | |
3212fa14 | 2712 | iter->end_ptr = iter->item_ptr + (u32)btrfs_item_size(path->nodes[0], |
c39c2ddc QW |
2713 | path->slots[0]); |
2714 | return 0; | |
2715 | } | |
584fb121 QW |
2716 | |
2717 | void btrfs_backref_init_cache(struct btrfs_fs_info *fs_info, | |
2718 | struct btrfs_backref_cache *cache, int is_reloc) | |
2719 | { | |
2720 | int i; | |
2721 | ||
2722 | cache->rb_root = RB_ROOT; | |
2723 | for (i = 0; i < BTRFS_MAX_LEVEL; i++) | |
2724 | INIT_LIST_HEAD(&cache->pending[i]); | |
2725 | INIT_LIST_HEAD(&cache->changed); | |
2726 | INIT_LIST_HEAD(&cache->detached); | |
2727 | INIT_LIST_HEAD(&cache->leaves); | |
2728 | INIT_LIST_HEAD(&cache->pending_edge); | |
2729 | INIT_LIST_HEAD(&cache->useless_node); | |
2730 | cache->fs_info = fs_info; | |
2731 | cache->is_reloc = is_reloc; | |
2732 | } | |
b1818dab QW |
2733 | |
2734 | struct btrfs_backref_node *btrfs_backref_alloc_node( | |
2735 | struct btrfs_backref_cache *cache, u64 bytenr, int level) | |
2736 | { | |
2737 | struct btrfs_backref_node *node; | |
2738 | ||
2739 | ASSERT(level >= 0 && level < BTRFS_MAX_LEVEL); | |
2740 | node = kzalloc(sizeof(*node), GFP_NOFS); | |
2741 | if (!node) | |
2742 | return node; | |
2743 | ||
2744 | INIT_LIST_HEAD(&node->list); | |
2745 | INIT_LIST_HEAD(&node->upper); | |
2746 | INIT_LIST_HEAD(&node->lower); | |
2747 | RB_CLEAR_NODE(&node->rb_node); | |
2748 | cache->nr_nodes++; | |
2749 | node->level = level; | |
2750 | node->bytenr = bytenr; | |
2751 | ||
2752 | return node; | |
2753 | } | |
47254d07 QW |
2754 | |
2755 | struct btrfs_backref_edge *btrfs_backref_alloc_edge( | |
2756 | struct btrfs_backref_cache *cache) | |
2757 | { | |
2758 | struct btrfs_backref_edge *edge; | |
2759 | ||
2760 | edge = kzalloc(sizeof(*edge), GFP_NOFS); | |
2761 | if (edge) | |
2762 | cache->nr_edges++; | |
2763 | return edge; | |
2764 | } | |
023acb07 QW |
2765 | |
2766 | /* | |
2767 | * Drop the backref node from cache, also cleaning up all its | |
2768 | * upper edges and any uncached nodes in the path. | |
2769 | * | |
2770 | * This cleanup happens bottom up, thus the node should either | |
2771 | * be the lowest node in the cache or a detached node. | |
2772 | */ | |
2773 | void btrfs_backref_cleanup_node(struct btrfs_backref_cache *cache, | |
2774 | struct btrfs_backref_node *node) | |
2775 | { | |
2776 | struct btrfs_backref_node *upper; | |
2777 | struct btrfs_backref_edge *edge; | |
2778 | ||
2779 | if (!node) | |
2780 | return; | |
2781 | ||
2782 | BUG_ON(!node->lowest && !node->detached); | |
2783 | while (!list_empty(&node->upper)) { | |
2784 | edge = list_entry(node->upper.next, struct btrfs_backref_edge, | |
2785 | list[LOWER]); | |
2786 | upper = edge->node[UPPER]; | |
2787 | list_del(&edge->list[LOWER]); | |
2788 | list_del(&edge->list[UPPER]); | |
2789 | btrfs_backref_free_edge(cache, edge); | |
2790 | ||
023acb07 QW |
2791 | /* |
2792 | * Add the node to leaf node list if no other child block | |
2793 | * cached. | |
2794 | */ | |
2795 | if (list_empty(&upper->lower)) { | |
2796 | list_add_tail(&upper->lower, &cache->leaves); | |
2797 | upper->lowest = 1; | |
2798 | } | |
2799 | } | |
2800 | ||
2801 | btrfs_backref_drop_node(cache, node); | |
2802 | } | |
13fe1bdb QW |
2803 | |
2804 | /* | |
2805 | * Release all nodes/edges from current cache | |
2806 | */ | |
2807 | void btrfs_backref_release_cache(struct btrfs_backref_cache *cache) | |
2808 | { | |
2809 | struct btrfs_backref_node *node; | |
2810 | int i; | |
2811 | ||
2812 | while (!list_empty(&cache->detached)) { | |
2813 | node = list_entry(cache->detached.next, | |
2814 | struct btrfs_backref_node, list); | |
2815 | btrfs_backref_cleanup_node(cache, node); | |
2816 | } | |
2817 | ||
2818 | while (!list_empty(&cache->leaves)) { | |
2819 | node = list_entry(cache->leaves.next, | |
2820 | struct btrfs_backref_node, lower); | |
2821 | btrfs_backref_cleanup_node(cache, node); | |
2822 | } | |
2823 | ||
2824 | cache->last_trans = 0; | |
2825 | ||
2826 | for (i = 0; i < BTRFS_MAX_LEVEL; i++) | |
2827 | ASSERT(list_empty(&cache->pending[i])); | |
2828 | ASSERT(list_empty(&cache->pending_edge)); | |
2829 | ASSERT(list_empty(&cache->useless_node)); | |
2830 | ASSERT(list_empty(&cache->changed)); | |
2831 | ASSERT(list_empty(&cache->detached)); | |
2832 | ASSERT(RB_EMPTY_ROOT(&cache->rb_root)); | |
2833 | ASSERT(!cache->nr_nodes); | |
2834 | ASSERT(!cache->nr_edges); | |
2835 | } | |
1b60d2ec QW |
2836 | |
2837 | /* | |
2838 | * Handle direct tree backref | |
2839 | * | |
2840 | * Direct tree backref means, the backref item shows its parent bytenr | |
2841 | * directly. This is for SHARED_BLOCK_REF backref (keyed or inlined). | |
2842 | * | |
2843 | * @ref_key: The converted backref key. | |
2844 | * For keyed backref, it's the item key. | |
2845 | * For inlined backref, objectid is the bytenr, | |
2846 | * type is btrfs_inline_ref_type, offset is | |
2847 | * btrfs_inline_ref_offset. | |
2848 | */ | |
2849 | static int handle_direct_tree_backref(struct btrfs_backref_cache *cache, | |
2850 | struct btrfs_key *ref_key, | |
2851 | struct btrfs_backref_node *cur) | |
2852 | { | |
2853 | struct btrfs_backref_edge *edge; | |
2854 | struct btrfs_backref_node *upper; | |
2855 | struct rb_node *rb_node; | |
2856 | ||
2857 | ASSERT(ref_key->type == BTRFS_SHARED_BLOCK_REF_KEY); | |
2858 | ||
2859 | /* Only reloc root uses backref pointing to itself */ | |
2860 | if (ref_key->objectid == ref_key->offset) { | |
2861 | struct btrfs_root *root; | |
2862 | ||
2863 | cur->is_reloc_root = 1; | |
2864 | /* Only reloc backref cache cares about a specific root */ | |
2865 | if (cache->is_reloc) { | |
2866 | root = find_reloc_root(cache->fs_info, cur->bytenr); | |
f78743fb | 2867 | if (!root) |
1b60d2ec QW |
2868 | return -ENOENT; |
2869 | cur->root = root; | |
2870 | } else { | |
2871 | /* | |
2872 | * For generic purpose backref cache, reloc root node | |
2873 | * is useless. | |
2874 | */ | |
2875 | list_add(&cur->list, &cache->useless_node); | |
2876 | } | |
2877 | return 0; | |
2878 | } | |
2879 | ||
2880 | edge = btrfs_backref_alloc_edge(cache); | |
2881 | if (!edge) | |
2882 | return -ENOMEM; | |
2883 | ||
2884 | rb_node = rb_simple_search(&cache->rb_root, ref_key->offset); | |
2885 | if (!rb_node) { | |
2886 | /* Parent node not yet cached */ | |
2887 | upper = btrfs_backref_alloc_node(cache, ref_key->offset, | |
2888 | cur->level + 1); | |
2889 | if (!upper) { | |
2890 | btrfs_backref_free_edge(cache, edge); | |
2891 | return -ENOMEM; | |
2892 | } | |
2893 | ||
2894 | /* | |
2895 | * Backrefs for the upper level block isn't cached, add the | |
2896 | * block to pending list | |
2897 | */ | |
2898 | list_add_tail(&edge->list[UPPER], &cache->pending_edge); | |
2899 | } else { | |
2900 | /* Parent node already cached */ | |
2901 | upper = rb_entry(rb_node, struct btrfs_backref_node, rb_node); | |
2902 | ASSERT(upper->checked); | |
2903 | INIT_LIST_HEAD(&edge->list[UPPER]); | |
2904 | } | |
2905 | btrfs_backref_link_edge(edge, cur, upper, LINK_LOWER); | |
2906 | return 0; | |
2907 | } | |
2908 | ||
2909 | /* | |
2910 | * Handle indirect tree backref | |
2911 | * | |
2912 | * Indirect tree backref means, we only know which tree the node belongs to. | |
2913 | * We still need to do a tree search to find out the parents. This is for | |
2914 | * TREE_BLOCK_REF backref (keyed or inlined). | |
2915 | * | |
2916 | * @ref_key: The same as @ref_key in handle_direct_tree_backref() | |
2917 | * @tree_key: The first key of this tree block. | |
1a9fd417 | 2918 | * @path: A clean (released) path, to avoid allocating path every time |
1b60d2ec QW |
2919 | * the function get called. |
2920 | */ | |
2921 | static int handle_indirect_tree_backref(struct btrfs_backref_cache *cache, | |
2922 | struct btrfs_path *path, | |
2923 | struct btrfs_key *ref_key, | |
2924 | struct btrfs_key *tree_key, | |
2925 | struct btrfs_backref_node *cur) | |
2926 | { | |
2927 | struct btrfs_fs_info *fs_info = cache->fs_info; | |
2928 | struct btrfs_backref_node *upper; | |
2929 | struct btrfs_backref_node *lower; | |
2930 | struct btrfs_backref_edge *edge; | |
2931 | struct extent_buffer *eb; | |
2932 | struct btrfs_root *root; | |
1b60d2ec QW |
2933 | struct rb_node *rb_node; |
2934 | int level; | |
2935 | bool need_check = true; | |
2936 | int ret; | |
2937 | ||
56e9357a | 2938 | root = btrfs_get_fs_root(fs_info, ref_key->offset, false); |
1b60d2ec QW |
2939 | if (IS_ERR(root)) |
2940 | return PTR_ERR(root); | |
92a7cc42 | 2941 | if (!test_bit(BTRFS_ROOT_SHAREABLE, &root->state)) |
1b60d2ec QW |
2942 | cur->cowonly = 1; |
2943 | ||
2944 | if (btrfs_root_level(&root->root_item) == cur->level) { | |
2945 | /* Tree root */ | |
2946 | ASSERT(btrfs_root_bytenr(&root->root_item) == cur->bytenr); | |
876de781 QW |
2947 | /* |
2948 | * For reloc backref cache, we may ignore reloc root. But for | |
2949 | * general purpose backref cache, we can't rely on | |
2950 | * btrfs_should_ignore_reloc_root() as it may conflict with | |
2951 | * current running relocation and lead to missing root. | |
2952 | * | |
2953 | * For general purpose backref cache, reloc root detection is | |
2954 | * completely relying on direct backref (key->offset is parent | |
2955 | * bytenr), thus only do such check for reloc cache. | |
2956 | */ | |
2957 | if (btrfs_should_ignore_reloc_root(root) && cache->is_reloc) { | |
1b60d2ec QW |
2958 | btrfs_put_root(root); |
2959 | list_add(&cur->list, &cache->useless_node); | |
2960 | } else { | |
2961 | cur->root = root; | |
2962 | } | |
2963 | return 0; | |
2964 | } | |
2965 | ||
2966 | level = cur->level + 1; | |
2967 | ||
2968 | /* Search the tree to find parent blocks referring to the block */ | |
2969 | path->search_commit_root = 1; | |
2970 | path->skip_locking = 1; | |
2971 | path->lowest_level = level; | |
2972 | ret = btrfs_search_slot(NULL, root, tree_key, path, 0, 0); | |
2973 | path->lowest_level = 0; | |
2974 | if (ret < 0) { | |
2975 | btrfs_put_root(root); | |
2976 | return ret; | |
2977 | } | |
2978 | if (ret > 0 && path->slots[level] > 0) | |
2979 | path->slots[level]--; | |
2980 | ||
2981 | eb = path->nodes[level]; | |
2982 | if (btrfs_node_blockptr(eb, path->slots[level]) != cur->bytenr) { | |
2983 | btrfs_err(fs_info, | |
2984 | "couldn't find block (%llu) (level %d) in tree (%llu) with key (%llu %u %llu)", | |
2985 | cur->bytenr, level - 1, root->root_key.objectid, | |
2986 | tree_key->objectid, tree_key->type, tree_key->offset); | |
2987 | btrfs_put_root(root); | |
2988 | ret = -ENOENT; | |
2989 | goto out; | |
2990 | } | |
2991 | lower = cur; | |
2992 | ||
2993 | /* Add all nodes and edges in the path */ | |
2994 | for (; level < BTRFS_MAX_LEVEL; level++) { | |
2995 | if (!path->nodes[level]) { | |
2996 | ASSERT(btrfs_root_bytenr(&root->root_item) == | |
2997 | lower->bytenr); | |
876de781 QW |
2998 | /* Same as previous should_ignore_reloc_root() call */ |
2999 | if (btrfs_should_ignore_reloc_root(root) && | |
3000 | cache->is_reloc) { | |
1b60d2ec QW |
3001 | btrfs_put_root(root); |
3002 | list_add(&lower->list, &cache->useless_node); | |
3003 | } else { | |
3004 | lower->root = root; | |
3005 | } | |
3006 | break; | |
3007 | } | |
3008 | ||
3009 | edge = btrfs_backref_alloc_edge(cache); | |
3010 | if (!edge) { | |
3011 | btrfs_put_root(root); | |
3012 | ret = -ENOMEM; | |
3013 | goto out; | |
3014 | } | |
3015 | ||
3016 | eb = path->nodes[level]; | |
3017 | rb_node = rb_simple_search(&cache->rb_root, eb->start); | |
3018 | if (!rb_node) { | |
3019 | upper = btrfs_backref_alloc_node(cache, eb->start, | |
3020 | lower->level + 1); | |
3021 | if (!upper) { | |
3022 | btrfs_put_root(root); | |
3023 | btrfs_backref_free_edge(cache, edge); | |
3024 | ret = -ENOMEM; | |
3025 | goto out; | |
3026 | } | |
3027 | upper->owner = btrfs_header_owner(eb); | |
92a7cc42 | 3028 | if (!test_bit(BTRFS_ROOT_SHAREABLE, &root->state)) |
1b60d2ec QW |
3029 | upper->cowonly = 1; |
3030 | ||
3031 | /* | |
3032 | * If we know the block isn't shared we can avoid | |
3033 | * checking its backrefs. | |
3034 | */ | |
3035 | if (btrfs_block_can_be_shared(root, eb)) | |
3036 | upper->checked = 0; | |
3037 | else | |
3038 | upper->checked = 1; | |
3039 | ||
3040 | /* | |
3041 | * Add the block to pending list if we need to check its | |
3042 | * backrefs, we only do this once while walking up a | |
3043 | * tree as we will catch anything else later on. | |
3044 | */ | |
3045 | if (!upper->checked && need_check) { | |
3046 | need_check = false; | |
3047 | list_add_tail(&edge->list[UPPER], | |
3048 | &cache->pending_edge); | |
3049 | } else { | |
3050 | if (upper->checked) | |
3051 | need_check = true; | |
3052 | INIT_LIST_HEAD(&edge->list[UPPER]); | |
3053 | } | |
3054 | } else { | |
3055 | upper = rb_entry(rb_node, struct btrfs_backref_node, | |
3056 | rb_node); | |
3057 | ASSERT(upper->checked); | |
3058 | INIT_LIST_HEAD(&edge->list[UPPER]); | |
3059 | if (!upper->owner) | |
3060 | upper->owner = btrfs_header_owner(eb); | |
3061 | } | |
3062 | btrfs_backref_link_edge(edge, lower, upper, LINK_LOWER); | |
3063 | ||
3064 | if (rb_node) { | |
3065 | btrfs_put_root(root); | |
3066 | break; | |
3067 | } | |
3068 | lower = upper; | |
3069 | upper = NULL; | |
3070 | } | |
3071 | out: | |
3072 | btrfs_release_path(path); | |
3073 | return ret; | |
3074 | } | |
3075 | ||
3076 | /* | |
3077 | * Add backref node @cur into @cache. | |
3078 | * | |
3079 | * NOTE: Even if the function returned 0, @cur is not yet cached as its upper | |
3080 | * links aren't yet bi-directional. Needs to finish such links. | |
fc997ed0 | 3081 | * Use btrfs_backref_finish_upper_links() to finish such linkage. |
1b60d2ec QW |
3082 | * |
3083 | * @path: Released path for indirect tree backref lookup | |
3084 | * @iter: Released backref iter for extent tree search | |
3085 | * @node_key: The first key of the tree block | |
3086 | */ | |
3087 | int btrfs_backref_add_tree_node(struct btrfs_backref_cache *cache, | |
3088 | struct btrfs_path *path, | |
3089 | struct btrfs_backref_iter *iter, | |
3090 | struct btrfs_key *node_key, | |
3091 | struct btrfs_backref_node *cur) | |
3092 | { | |
3093 | struct btrfs_fs_info *fs_info = cache->fs_info; | |
3094 | struct btrfs_backref_edge *edge; | |
3095 | struct btrfs_backref_node *exist; | |
3096 | int ret; | |
3097 | ||
3098 | ret = btrfs_backref_iter_start(iter, cur->bytenr); | |
3099 | if (ret < 0) | |
3100 | return ret; | |
3101 | /* | |
3102 | * We skip the first btrfs_tree_block_info, as we don't use the key | |
3103 | * stored in it, but fetch it from the tree block | |
3104 | */ | |
3105 | if (btrfs_backref_has_tree_block_info(iter)) { | |
3106 | ret = btrfs_backref_iter_next(iter); | |
3107 | if (ret < 0) | |
3108 | goto out; | |
3109 | /* No extra backref? This means the tree block is corrupted */ | |
3110 | if (ret > 0) { | |
3111 | ret = -EUCLEAN; | |
3112 | goto out; | |
3113 | } | |
3114 | } | |
3115 | WARN_ON(cur->checked); | |
3116 | if (!list_empty(&cur->upper)) { | |
3117 | /* | |
3118 | * The backref was added previously when processing backref of | |
3119 | * type BTRFS_TREE_BLOCK_REF_KEY | |
3120 | */ | |
3121 | ASSERT(list_is_singular(&cur->upper)); | |
3122 | edge = list_entry(cur->upper.next, struct btrfs_backref_edge, | |
3123 | list[LOWER]); | |
3124 | ASSERT(list_empty(&edge->list[UPPER])); | |
3125 | exist = edge->node[UPPER]; | |
3126 | /* | |
3127 | * Add the upper level block to pending list if we need check | |
3128 | * its backrefs | |
3129 | */ | |
3130 | if (!exist->checked) | |
3131 | list_add_tail(&edge->list[UPPER], &cache->pending_edge); | |
3132 | } else { | |
3133 | exist = NULL; | |
3134 | } | |
3135 | ||
3136 | for (; ret == 0; ret = btrfs_backref_iter_next(iter)) { | |
3137 | struct extent_buffer *eb; | |
3138 | struct btrfs_key key; | |
3139 | int type; | |
3140 | ||
3141 | cond_resched(); | |
3142 | eb = btrfs_backref_get_eb(iter); | |
3143 | ||
3144 | key.objectid = iter->bytenr; | |
3145 | if (btrfs_backref_iter_is_inline_ref(iter)) { | |
3146 | struct btrfs_extent_inline_ref *iref; | |
3147 | ||
3148 | /* Update key for inline backref */ | |
3149 | iref = (struct btrfs_extent_inline_ref *) | |
3150 | ((unsigned long)iter->cur_ptr); | |
3151 | type = btrfs_get_extent_inline_ref_type(eb, iref, | |
3152 | BTRFS_REF_TYPE_BLOCK); | |
3153 | if (type == BTRFS_REF_TYPE_INVALID) { | |
3154 | ret = -EUCLEAN; | |
3155 | goto out; | |
3156 | } | |
3157 | key.type = type; | |
3158 | key.offset = btrfs_extent_inline_ref_offset(eb, iref); | |
3159 | } else { | |
3160 | key.type = iter->cur_key.type; | |
3161 | key.offset = iter->cur_key.offset; | |
3162 | } | |
3163 | ||
3164 | /* | |
3165 | * Parent node found and matches current inline ref, no need to | |
3166 | * rebuild this node for this inline ref | |
3167 | */ | |
3168 | if (exist && | |
3169 | ((key.type == BTRFS_TREE_BLOCK_REF_KEY && | |
3170 | exist->owner == key.offset) || | |
3171 | (key.type == BTRFS_SHARED_BLOCK_REF_KEY && | |
3172 | exist->bytenr == key.offset))) { | |
3173 | exist = NULL; | |
3174 | continue; | |
3175 | } | |
3176 | ||
3177 | /* SHARED_BLOCK_REF means key.offset is the parent bytenr */ | |
3178 | if (key.type == BTRFS_SHARED_BLOCK_REF_KEY) { | |
3179 | ret = handle_direct_tree_backref(cache, &key, cur); | |
3180 | if (ret < 0) | |
3181 | goto out; | |
3182 | continue; | |
3183 | } else if (unlikely(key.type == BTRFS_EXTENT_REF_V0_KEY)) { | |
3184 | ret = -EINVAL; | |
3185 | btrfs_print_v0_err(fs_info); | |
3186 | btrfs_handle_fs_error(fs_info, ret, NULL); | |
3187 | goto out; | |
3188 | } else if (key.type != BTRFS_TREE_BLOCK_REF_KEY) { | |
3189 | continue; | |
3190 | } | |
3191 | ||
3192 | /* | |
3193 | * key.type == BTRFS_TREE_BLOCK_REF_KEY, inline ref offset | |
3194 | * means the root objectid. We need to search the tree to get | |
3195 | * its parent bytenr. | |
3196 | */ | |
3197 | ret = handle_indirect_tree_backref(cache, path, &key, node_key, | |
3198 | cur); | |
3199 | if (ret < 0) | |
3200 | goto out; | |
3201 | } | |
3202 | ret = 0; | |
3203 | cur->checked = 1; | |
3204 | WARN_ON(exist); | |
3205 | out: | |
3206 | btrfs_backref_iter_release(iter); | |
3207 | return ret; | |
3208 | } | |
fc997ed0 QW |
3209 | |
3210 | /* | |
3211 | * Finish the upwards linkage created by btrfs_backref_add_tree_node() | |
3212 | */ | |
3213 | int btrfs_backref_finish_upper_links(struct btrfs_backref_cache *cache, | |
3214 | struct btrfs_backref_node *start) | |
3215 | { | |
3216 | struct list_head *useless_node = &cache->useless_node; | |
3217 | struct btrfs_backref_edge *edge; | |
3218 | struct rb_node *rb_node; | |
3219 | LIST_HEAD(pending_edge); | |
3220 | ||
3221 | ASSERT(start->checked); | |
3222 | ||
3223 | /* Insert this node to cache if it's not COW-only */ | |
3224 | if (!start->cowonly) { | |
3225 | rb_node = rb_simple_insert(&cache->rb_root, start->bytenr, | |
3226 | &start->rb_node); | |
3227 | if (rb_node) | |
3228 | btrfs_backref_panic(cache->fs_info, start->bytenr, | |
3229 | -EEXIST); | |
3230 | list_add_tail(&start->lower, &cache->leaves); | |
3231 | } | |
3232 | ||
3233 | /* | |
3234 | * Use breadth first search to iterate all related edges. | |
3235 | * | |
3236 | * The starting points are all the edges of this node | |
3237 | */ | |
3238 | list_for_each_entry(edge, &start->upper, list[LOWER]) | |
3239 | list_add_tail(&edge->list[UPPER], &pending_edge); | |
3240 | ||
3241 | while (!list_empty(&pending_edge)) { | |
3242 | struct btrfs_backref_node *upper; | |
3243 | struct btrfs_backref_node *lower; | |
fc997ed0 QW |
3244 | |
3245 | edge = list_first_entry(&pending_edge, | |
3246 | struct btrfs_backref_edge, list[UPPER]); | |
3247 | list_del_init(&edge->list[UPPER]); | |
3248 | upper = edge->node[UPPER]; | |
3249 | lower = edge->node[LOWER]; | |
3250 | ||
3251 | /* Parent is detached, no need to keep any edges */ | |
3252 | if (upper->detached) { | |
3253 | list_del(&edge->list[LOWER]); | |
3254 | btrfs_backref_free_edge(cache, edge); | |
3255 | ||
3256 | /* Lower node is orphan, queue for cleanup */ | |
3257 | if (list_empty(&lower->upper)) | |
3258 | list_add(&lower->list, useless_node); | |
3259 | continue; | |
3260 | } | |
3261 | ||
3262 | /* | |
3263 | * All new nodes added in current build_backref_tree() haven't | |
3264 | * been linked to the cache rb tree. | |
3265 | * So if we have upper->rb_node populated, this means a cache | |
3266 | * hit. We only need to link the edge, as @upper and all its | |
3267 | * parents have already been linked. | |
3268 | */ | |
3269 | if (!RB_EMPTY_NODE(&upper->rb_node)) { | |
3270 | if (upper->lowest) { | |
3271 | list_del_init(&upper->lower); | |
3272 | upper->lowest = 0; | |
3273 | } | |
3274 | ||
3275 | list_add_tail(&edge->list[UPPER], &upper->lower); | |
3276 | continue; | |
3277 | } | |
3278 | ||
3279 | /* Sanity check, we shouldn't have any unchecked nodes */ | |
3280 | if (!upper->checked) { | |
3281 | ASSERT(0); | |
3282 | return -EUCLEAN; | |
3283 | } | |
3284 | ||
3285 | /* Sanity check, COW-only node has non-COW-only parent */ | |
3286 | if (start->cowonly != upper->cowonly) { | |
3287 | ASSERT(0); | |
3288 | return -EUCLEAN; | |
3289 | } | |
3290 | ||
3291 | /* Only cache non-COW-only (subvolume trees) tree blocks */ | |
3292 | if (!upper->cowonly) { | |
3293 | rb_node = rb_simple_insert(&cache->rb_root, upper->bytenr, | |
3294 | &upper->rb_node); | |
3295 | if (rb_node) { | |
3296 | btrfs_backref_panic(cache->fs_info, | |
3297 | upper->bytenr, -EEXIST); | |
3298 | return -EUCLEAN; | |
3299 | } | |
3300 | } | |
3301 | ||
3302 | list_add_tail(&edge->list[UPPER], &upper->lower); | |
3303 | ||
3304 | /* | |
3305 | * Also queue all the parent edges of this uncached node | |
3306 | * to finish the upper linkage | |
3307 | */ | |
3308 | list_for_each_entry(edge, &upper->upper, list[LOWER]) | |
3309 | list_add_tail(&edge->list[UPPER], &pending_edge); | |
3310 | } | |
3311 | return 0; | |
3312 | } | |
1b23ea18 QW |
3313 | |
3314 | void btrfs_backref_error_cleanup(struct btrfs_backref_cache *cache, | |
3315 | struct btrfs_backref_node *node) | |
3316 | { | |
3317 | struct btrfs_backref_node *lower; | |
3318 | struct btrfs_backref_node *upper; | |
3319 | struct btrfs_backref_edge *edge; | |
3320 | ||
3321 | while (!list_empty(&cache->useless_node)) { | |
3322 | lower = list_first_entry(&cache->useless_node, | |
3323 | struct btrfs_backref_node, list); | |
3324 | list_del_init(&lower->list); | |
3325 | } | |
3326 | while (!list_empty(&cache->pending_edge)) { | |
3327 | edge = list_first_entry(&cache->pending_edge, | |
3328 | struct btrfs_backref_edge, list[UPPER]); | |
3329 | list_del(&edge->list[UPPER]); | |
3330 | list_del(&edge->list[LOWER]); | |
3331 | lower = edge->node[LOWER]; | |
3332 | upper = edge->node[UPPER]; | |
3333 | btrfs_backref_free_edge(cache, edge); | |
3334 | ||
3335 | /* | |
3336 | * Lower is no longer linked to any upper backref nodes and | |
3337 | * isn't in the cache, we can free it ourselves. | |
3338 | */ | |
3339 | if (list_empty(&lower->upper) && | |
3340 | RB_EMPTY_NODE(&lower->rb_node)) | |
3341 | list_add(&lower->list, &cache->useless_node); | |
3342 | ||
3343 | if (!RB_EMPTY_NODE(&upper->rb_node)) | |
3344 | continue; | |
3345 | ||
3346 | /* Add this guy's upper edges to the list to process */ | |
3347 | list_for_each_entry(edge, &upper->upper, list[LOWER]) | |
3348 | list_add_tail(&edge->list[UPPER], | |
3349 | &cache->pending_edge); | |
3350 | if (list_empty(&upper->upper)) | |
3351 | list_add(&upper->list, &cache->useless_node); | |
3352 | } | |
3353 | ||
3354 | while (!list_empty(&cache->useless_node)) { | |
3355 | lower = list_first_entry(&cache->useless_node, | |
3356 | struct btrfs_backref_node, list); | |
3357 | list_del_init(&lower->list); | |
3358 | if (lower == node) | |
3359 | node = NULL; | |
49ecc679 | 3360 | btrfs_backref_drop_node(cache, lower); |
1b23ea18 QW |
3361 | } |
3362 | ||
3363 | btrfs_backref_cleanup_node(cache, node); | |
3364 | ASSERT(list_empty(&cache->useless_node) && | |
3365 | list_empty(&cache->pending_edge)); | |
3366 | } |