]>
Commit | Line | Data |
---|---|---|
a542ad1b JS |
1 | /* |
2 | * Copyright (C) 2011 STRATO. All rights reserved. | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or | |
5 | * modify it under the terms of the GNU General Public | |
6 | * License v2 as published by the Free Software Foundation. | |
7 | * | |
8 | * This program is distributed in the hope that it will be useful, | |
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
11 | * General Public License for more details. | |
12 | * | |
13 | * You should have received a copy of the GNU General Public | |
14 | * License along with this program; if not, write to the | |
15 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
16 | * Boston, MA 021110-1307, USA. | |
17 | */ | |
18 | ||
19 | #include "ctree.h" | |
20 | #include "disk-io.h" | |
21 | #include "backref.h" | |
8da6d581 JS |
22 | #include "ulist.h" |
23 | #include "transaction.h" | |
24 | #include "delayed-ref.h" | |
b916a59a | 25 | #include "locking.h" |
a542ad1b | 26 | |
8da6d581 JS |
27 | /* |
28 | * this structure records all encountered refs on the way up to the root | |
29 | */ | |
30 | struct __prelim_ref { | |
31 | struct list_head list; | |
32 | u64 root_id; | |
33 | struct btrfs_key key; | |
34 | int level; | |
35 | int count; | |
36 | u64 parent; | |
37 | u64 wanted_disk_byte; | |
38 | }; | |
39 | ||
40 | static int __add_prelim_ref(struct list_head *head, u64 root_id, | |
41 | struct btrfs_key *key, int level, u64 parent, | |
42 | u64 wanted_disk_byte, int count) | |
43 | { | |
44 | struct __prelim_ref *ref; | |
45 | ||
46 | /* in case we're adding delayed refs, we're holding the refs spinlock */ | |
47 | ref = kmalloc(sizeof(*ref), GFP_ATOMIC); | |
48 | if (!ref) | |
49 | return -ENOMEM; | |
50 | ||
51 | ref->root_id = root_id; | |
52 | if (key) | |
53 | ref->key = *key; | |
54 | else | |
55 | memset(&ref->key, 0, sizeof(ref->key)); | |
56 | ||
57 | ref->level = level; | |
58 | ref->count = count; | |
59 | ref->parent = parent; | |
60 | ref->wanted_disk_byte = wanted_disk_byte; | |
61 | list_add_tail(&ref->list, head); | |
62 | ||
63 | return 0; | |
64 | } | |
65 | ||
66 | static int add_all_parents(struct btrfs_root *root, struct btrfs_path *path, | |
67 | struct ulist *parents, | |
68 | struct extent_buffer *eb, int level, | |
69 | u64 wanted_objectid, u64 wanted_disk_byte) | |
70 | { | |
71 | int ret; | |
72 | int slot; | |
73 | struct btrfs_file_extent_item *fi; | |
74 | struct btrfs_key key; | |
75 | u64 disk_byte; | |
76 | ||
77 | add_parent: | |
78 | ret = ulist_add(parents, eb->start, 0, GFP_NOFS); | |
79 | if (ret < 0) | |
80 | return ret; | |
81 | ||
82 | if (level != 0) | |
83 | return 0; | |
84 | ||
85 | /* | |
86 | * if the current leaf is full with EXTENT_DATA items, we must | |
87 | * check the next one if that holds a reference as well. | |
88 | * ref->count cannot be used to skip this check. | |
89 | * repeat this until we don't find any additional EXTENT_DATA items. | |
90 | */ | |
91 | while (1) { | |
92 | ret = btrfs_next_leaf(root, path); | |
93 | if (ret < 0) | |
94 | return ret; | |
95 | if (ret) | |
96 | return 0; | |
97 | ||
98 | eb = path->nodes[0]; | |
99 | for (slot = 0; slot < btrfs_header_nritems(eb); ++slot) { | |
100 | btrfs_item_key_to_cpu(eb, &key, slot); | |
101 | if (key.objectid != wanted_objectid || | |
102 | key.type != BTRFS_EXTENT_DATA_KEY) | |
103 | return 0; | |
104 | fi = btrfs_item_ptr(eb, slot, | |
105 | struct btrfs_file_extent_item); | |
106 | disk_byte = btrfs_file_extent_disk_bytenr(eb, fi); | |
107 | if (disk_byte == wanted_disk_byte) | |
108 | goto add_parent; | |
109 | } | |
110 | } | |
111 | ||
112 | return 0; | |
113 | } | |
114 | ||
115 | /* | |
116 | * resolve an indirect backref in the form (root_id, key, level) | |
117 | * to a logical address | |
118 | */ | |
119 | static int __resolve_indirect_ref(struct btrfs_fs_info *fs_info, | |
7a3ae2f8 | 120 | int search_commit_root, |
8da6d581 JS |
121 | struct __prelim_ref *ref, |
122 | struct ulist *parents) | |
123 | { | |
124 | struct btrfs_path *path; | |
125 | struct btrfs_root *root; | |
126 | struct btrfs_key root_key; | |
127 | struct btrfs_key key = {0}; | |
128 | struct extent_buffer *eb; | |
129 | int ret = 0; | |
130 | int root_level; | |
131 | int level = ref->level; | |
132 | ||
133 | path = btrfs_alloc_path(); | |
134 | if (!path) | |
135 | return -ENOMEM; | |
7a3ae2f8 | 136 | path->search_commit_root = !!search_commit_root; |
8da6d581 JS |
137 | |
138 | root_key.objectid = ref->root_id; | |
139 | root_key.type = BTRFS_ROOT_ITEM_KEY; | |
140 | root_key.offset = (u64)-1; | |
141 | root = btrfs_read_fs_root_no_name(fs_info, &root_key); | |
142 | if (IS_ERR(root)) { | |
143 | ret = PTR_ERR(root); | |
144 | goto out; | |
145 | } | |
146 | ||
147 | rcu_read_lock(); | |
148 | root_level = btrfs_header_level(root->node); | |
149 | rcu_read_unlock(); | |
150 | ||
151 | if (root_level + 1 == level) | |
152 | goto out; | |
153 | ||
154 | path->lowest_level = level; | |
155 | ret = btrfs_search_slot(NULL, root, &ref->key, path, 0, 0); | |
156 | pr_debug("search slot in root %llu (level %d, ref count %d) returned " | |
157 | "%d for key (%llu %u %llu)\n", | |
158 | (unsigned long long)ref->root_id, level, ref->count, ret, | |
159 | (unsigned long long)ref->key.objectid, ref->key.type, | |
160 | (unsigned long long)ref->key.offset); | |
161 | if (ret < 0) | |
162 | goto out; | |
163 | ||
164 | eb = path->nodes[level]; | |
165 | if (!eb) { | |
166 | WARN_ON(1); | |
167 | ret = 1; | |
168 | goto out; | |
169 | } | |
170 | ||
171 | if (level == 0) { | |
172 | if (ret == 1 && path->slots[0] >= btrfs_header_nritems(eb)) { | |
173 | ret = btrfs_next_leaf(root, path); | |
174 | if (ret) | |
175 | goto out; | |
176 | eb = path->nodes[0]; | |
177 | } | |
178 | ||
179 | btrfs_item_key_to_cpu(eb, &key, path->slots[0]); | |
180 | } | |
181 | ||
182 | /* the last two parameters will only be used for level == 0 */ | |
183 | ret = add_all_parents(root, path, parents, eb, level, key.objectid, | |
184 | ref->wanted_disk_byte); | |
185 | out: | |
186 | btrfs_free_path(path); | |
187 | return ret; | |
188 | } | |
189 | ||
190 | /* | |
191 | * resolve all indirect backrefs from the list | |
192 | */ | |
193 | static int __resolve_indirect_refs(struct btrfs_fs_info *fs_info, | |
7a3ae2f8 | 194 | int search_commit_root, |
8da6d581 JS |
195 | struct list_head *head) |
196 | { | |
197 | int err; | |
198 | int ret = 0; | |
199 | struct __prelim_ref *ref; | |
200 | struct __prelim_ref *ref_safe; | |
201 | struct __prelim_ref *new_ref; | |
202 | struct ulist *parents; | |
203 | struct ulist_node *node; | |
cd1b413c | 204 | struct ulist_iterator uiter; |
8da6d581 JS |
205 | |
206 | parents = ulist_alloc(GFP_NOFS); | |
207 | if (!parents) | |
208 | return -ENOMEM; | |
209 | ||
210 | /* | |
211 | * _safe allows us to insert directly after the current item without | |
212 | * iterating over the newly inserted items. | |
213 | * we're also allowed to re-assign ref during iteration. | |
214 | */ | |
215 | list_for_each_entry_safe(ref, ref_safe, head, list) { | |
216 | if (ref->parent) /* already direct */ | |
217 | continue; | |
218 | if (ref->count == 0) | |
219 | continue; | |
7a3ae2f8 JS |
220 | err = __resolve_indirect_ref(fs_info, search_commit_root, |
221 | ref, parents); | |
8da6d581 JS |
222 | if (err) { |
223 | if (ret == 0) | |
224 | ret = err; | |
225 | continue; | |
226 | } | |
227 | ||
228 | /* we put the first parent into the ref at hand */ | |
cd1b413c JS |
229 | ULIST_ITER_INIT(&uiter); |
230 | node = ulist_next(parents, &uiter); | |
8da6d581 JS |
231 | ref->parent = node ? node->val : 0; |
232 | ||
233 | /* additional parents require new refs being added here */ | |
cd1b413c | 234 | while ((node = ulist_next(parents, &uiter))) { |
8da6d581 JS |
235 | new_ref = kmalloc(sizeof(*new_ref), GFP_NOFS); |
236 | if (!new_ref) { | |
237 | ret = -ENOMEM; | |
238 | break; | |
239 | } | |
240 | memcpy(new_ref, ref, sizeof(*ref)); | |
241 | new_ref->parent = node->val; | |
242 | list_add(&new_ref->list, &ref->list); | |
243 | } | |
244 | ulist_reinit(parents); | |
245 | } | |
246 | ||
247 | ulist_free(parents); | |
248 | return ret; | |
249 | } | |
250 | ||
251 | /* | |
252 | * merge two lists of backrefs and adjust counts accordingly | |
253 | * | |
254 | * mode = 1: merge identical keys, if key is set | |
255 | * mode = 2: merge identical parents | |
256 | */ | |
257 | static int __merge_refs(struct list_head *head, int mode) | |
258 | { | |
259 | struct list_head *pos1; | |
260 | ||
261 | list_for_each(pos1, head) { | |
262 | struct list_head *n2; | |
263 | struct list_head *pos2; | |
264 | struct __prelim_ref *ref1; | |
265 | ||
266 | ref1 = list_entry(pos1, struct __prelim_ref, list); | |
267 | ||
268 | if (mode == 1 && ref1->key.type == 0) | |
269 | continue; | |
270 | for (pos2 = pos1->next, n2 = pos2->next; pos2 != head; | |
271 | pos2 = n2, n2 = pos2->next) { | |
272 | struct __prelim_ref *ref2; | |
273 | ||
274 | ref2 = list_entry(pos2, struct __prelim_ref, list); | |
275 | ||
276 | if (mode == 1) { | |
277 | if (memcmp(&ref1->key, &ref2->key, | |
278 | sizeof(ref1->key)) || | |
279 | ref1->level != ref2->level || | |
280 | ref1->root_id != ref2->root_id) | |
281 | continue; | |
282 | ref1->count += ref2->count; | |
283 | } else { | |
284 | if (ref1->parent != ref2->parent) | |
285 | continue; | |
286 | ref1->count += ref2->count; | |
287 | } | |
288 | list_del(&ref2->list); | |
289 | kfree(ref2); | |
290 | } | |
291 | ||
292 | } | |
293 | return 0; | |
294 | } | |
295 | ||
296 | /* | |
297 | * add all currently queued delayed refs from this head whose seq nr is | |
298 | * smaller or equal that seq to the list | |
299 | */ | |
300 | static int __add_delayed_refs(struct btrfs_delayed_ref_head *head, u64 seq, | |
301 | struct btrfs_key *info_key, | |
302 | struct list_head *prefs) | |
303 | { | |
304 | struct btrfs_delayed_extent_op *extent_op = head->extent_op; | |
305 | struct rb_node *n = &head->node.rb_node; | |
306 | int sgn; | |
b1375d64 | 307 | int ret = 0; |
8da6d581 JS |
308 | |
309 | if (extent_op && extent_op->update_key) | |
310 | btrfs_disk_key_to_cpu(info_key, &extent_op->key); | |
311 | ||
312 | while ((n = rb_prev(n))) { | |
313 | struct btrfs_delayed_ref_node *node; | |
314 | node = rb_entry(n, struct btrfs_delayed_ref_node, | |
315 | rb_node); | |
316 | if (node->bytenr != head->node.bytenr) | |
317 | break; | |
318 | WARN_ON(node->is_head); | |
319 | ||
320 | if (node->seq > seq) | |
321 | continue; | |
322 | ||
323 | switch (node->action) { | |
324 | case BTRFS_ADD_DELAYED_EXTENT: | |
325 | case BTRFS_UPDATE_DELAYED_HEAD: | |
326 | WARN_ON(1); | |
327 | continue; | |
328 | case BTRFS_ADD_DELAYED_REF: | |
329 | sgn = 1; | |
330 | break; | |
331 | case BTRFS_DROP_DELAYED_REF: | |
332 | sgn = -1; | |
333 | break; | |
334 | default: | |
335 | BUG_ON(1); | |
336 | } | |
337 | switch (node->type) { | |
338 | case BTRFS_TREE_BLOCK_REF_KEY: { | |
339 | struct btrfs_delayed_tree_ref *ref; | |
340 | ||
341 | ref = btrfs_delayed_node_to_tree_ref(node); | |
342 | ret = __add_prelim_ref(prefs, ref->root, info_key, | |
343 | ref->level + 1, 0, node->bytenr, | |
344 | node->ref_mod * sgn); | |
345 | break; | |
346 | } | |
347 | case BTRFS_SHARED_BLOCK_REF_KEY: { | |
348 | struct btrfs_delayed_tree_ref *ref; | |
349 | ||
350 | ref = btrfs_delayed_node_to_tree_ref(node); | |
351 | ret = __add_prelim_ref(prefs, ref->root, info_key, | |
352 | ref->level + 1, ref->parent, | |
353 | node->bytenr, | |
354 | node->ref_mod * sgn); | |
355 | break; | |
356 | } | |
357 | case BTRFS_EXTENT_DATA_REF_KEY: { | |
358 | struct btrfs_delayed_data_ref *ref; | |
359 | struct btrfs_key key; | |
360 | ||
361 | ref = btrfs_delayed_node_to_data_ref(node); | |
362 | ||
363 | key.objectid = ref->objectid; | |
364 | key.type = BTRFS_EXTENT_DATA_KEY; | |
365 | key.offset = ref->offset; | |
366 | ret = __add_prelim_ref(prefs, ref->root, &key, 0, 0, | |
367 | node->bytenr, | |
368 | node->ref_mod * sgn); | |
369 | break; | |
370 | } | |
371 | case BTRFS_SHARED_DATA_REF_KEY: { | |
372 | struct btrfs_delayed_data_ref *ref; | |
373 | struct btrfs_key key; | |
374 | ||
375 | ref = btrfs_delayed_node_to_data_ref(node); | |
376 | ||
377 | key.objectid = ref->objectid; | |
378 | key.type = BTRFS_EXTENT_DATA_KEY; | |
379 | key.offset = ref->offset; | |
380 | ret = __add_prelim_ref(prefs, ref->root, &key, 0, | |
381 | ref->parent, node->bytenr, | |
382 | node->ref_mod * sgn); | |
383 | break; | |
384 | } | |
385 | default: | |
386 | WARN_ON(1); | |
387 | } | |
388 | BUG_ON(ret); | |
389 | } | |
390 | ||
391 | return 0; | |
392 | } | |
393 | ||
394 | /* | |
395 | * add all inline backrefs for bytenr to the list | |
396 | */ | |
397 | static int __add_inline_refs(struct btrfs_fs_info *fs_info, | |
398 | struct btrfs_path *path, u64 bytenr, | |
399 | struct btrfs_key *info_key, int *info_level, | |
400 | struct list_head *prefs) | |
401 | { | |
b1375d64 | 402 | int ret = 0; |
8da6d581 JS |
403 | int slot; |
404 | struct extent_buffer *leaf; | |
405 | struct btrfs_key key; | |
406 | unsigned long ptr; | |
407 | unsigned long end; | |
408 | struct btrfs_extent_item *ei; | |
409 | u64 flags; | |
410 | u64 item_size; | |
411 | ||
412 | /* | |
413 | * enumerate all inline refs | |
414 | */ | |
415 | leaf = path->nodes[0]; | |
dadcaf78 | 416 | slot = path->slots[0]; |
8da6d581 JS |
417 | |
418 | item_size = btrfs_item_size_nr(leaf, slot); | |
419 | BUG_ON(item_size < sizeof(*ei)); | |
420 | ||
421 | ei = btrfs_item_ptr(leaf, slot, struct btrfs_extent_item); | |
422 | flags = btrfs_extent_flags(leaf, ei); | |
423 | ||
424 | ptr = (unsigned long)(ei + 1); | |
425 | end = (unsigned long)ei + item_size; | |
426 | ||
427 | if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) { | |
428 | struct btrfs_tree_block_info *info; | |
429 | struct btrfs_disk_key disk_key; | |
430 | ||
431 | info = (struct btrfs_tree_block_info *)ptr; | |
432 | *info_level = btrfs_tree_block_level(leaf, info); | |
433 | btrfs_tree_block_key(leaf, info, &disk_key); | |
434 | btrfs_disk_key_to_cpu(info_key, &disk_key); | |
435 | ptr += sizeof(struct btrfs_tree_block_info); | |
436 | BUG_ON(ptr > end); | |
437 | } else { | |
438 | BUG_ON(!(flags & BTRFS_EXTENT_FLAG_DATA)); | |
439 | } | |
440 | ||
441 | while (ptr < end) { | |
442 | struct btrfs_extent_inline_ref *iref; | |
443 | u64 offset; | |
444 | int type; | |
445 | ||
446 | iref = (struct btrfs_extent_inline_ref *)ptr; | |
447 | type = btrfs_extent_inline_ref_type(leaf, iref); | |
448 | offset = btrfs_extent_inline_ref_offset(leaf, iref); | |
449 | ||
450 | switch (type) { | |
451 | case BTRFS_SHARED_BLOCK_REF_KEY: | |
452 | ret = __add_prelim_ref(prefs, 0, info_key, | |
453 | *info_level + 1, offset, | |
454 | bytenr, 1); | |
455 | break; | |
456 | case BTRFS_SHARED_DATA_REF_KEY: { | |
457 | struct btrfs_shared_data_ref *sdref; | |
458 | int count; | |
459 | ||
460 | sdref = (struct btrfs_shared_data_ref *)(iref + 1); | |
461 | count = btrfs_shared_data_ref_count(leaf, sdref); | |
462 | ret = __add_prelim_ref(prefs, 0, NULL, 0, offset, | |
463 | bytenr, count); | |
464 | break; | |
465 | } | |
466 | case BTRFS_TREE_BLOCK_REF_KEY: | |
467 | ret = __add_prelim_ref(prefs, offset, info_key, | |
468 | *info_level + 1, 0, bytenr, 1); | |
469 | break; | |
470 | case BTRFS_EXTENT_DATA_REF_KEY: { | |
471 | struct btrfs_extent_data_ref *dref; | |
472 | int count; | |
473 | u64 root; | |
474 | ||
475 | dref = (struct btrfs_extent_data_ref *)(&iref->offset); | |
476 | count = btrfs_extent_data_ref_count(leaf, dref); | |
477 | key.objectid = btrfs_extent_data_ref_objectid(leaf, | |
478 | dref); | |
479 | key.type = BTRFS_EXTENT_DATA_KEY; | |
480 | key.offset = btrfs_extent_data_ref_offset(leaf, dref); | |
481 | root = btrfs_extent_data_ref_root(leaf, dref); | |
482 | ret = __add_prelim_ref(prefs, root, &key, 0, 0, bytenr, | |
483 | count); | |
484 | break; | |
485 | } | |
486 | default: | |
487 | WARN_ON(1); | |
488 | } | |
489 | BUG_ON(ret); | |
490 | ptr += btrfs_extent_inline_ref_size(type); | |
491 | } | |
492 | ||
493 | return 0; | |
494 | } | |
495 | ||
496 | /* | |
497 | * add all non-inline backrefs for bytenr to the list | |
498 | */ | |
499 | static int __add_keyed_refs(struct btrfs_fs_info *fs_info, | |
500 | struct btrfs_path *path, u64 bytenr, | |
501 | struct btrfs_key *info_key, int info_level, | |
502 | struct list_head *prefs) | |
503 | { | |
504 | struct btrfs_root *extent_root = fs_info->extent_root; | |
505 | int ret; | |
506 | int slot; | |
507 | struct extent_buffer *leaf; | |
508 | struct btrfs_key key; | |
509 | ||
510 | while (1) { | |
511 | ret = btrfs_next_item(extent_root, path); | |
512 | if (ret < 0) | |
513 | break; | |
514 | if (ret) { | |
515 | ret = 0; | |
516 | break; | |
517 | } | |
518 | ||
519 | slot = path->slots[0]; | |
520 | leaf = path->nodes[0]; | |
521 | btrfs_item_key_to_cpu(leaf, &key, slot); | |
522 | ||
523 | if (key.objectid != bytenr) | |
524 | break; | |
525 | if (key.type < BTRFS_TREE_BLOCK_REF_KEY) | |
526 | continue; | |
527 | if (key.type > BTRFS_SHARED_DATA_REF_KEY) | |
528 | break; | |
529 | ||
530 | switch (key.type) { | |
531 | case BTRFS_SHARED_BLOCK_REF_KEY: | |
532 | ret = __add_prelim_ref(prefs, 0, info_key, | |
533 | info_level + 1, key.offset, | |
534 | bytenr, 1); | |
535 | break; | |
536 | case BTRFS_SHARED_DATA_REF_KEY: { | |
537 | struct btrfs_shared_data_ref *sdref; | |
538 | int count; | |
539 | ||
540 | sdref = btrfs_item_ptr(leaf, slot, | |
541 | struct btrfs_shared_data_ref); | |
542 | count = btrfs_shared_data_ref_count(leaf, sdref); | |
543 | ret = __add_prelim_ref(prefs, 0, NULL, 0, key.offset, | |
544 | bytenr, count); | |
545 | break; | |
546 | } | |
547 | case BTRFS_TREE_BLOCK_REF_KEY: | |
548 | ret = __add_prelim_ref(prefs, key.offset, info_key, | |
549 | info_level + 1, 0, bytenr, 1); | |
550 | break; | |
551 | case BTRFS_EXTENT_DATA_REF_KEY: { | |
552 | struct btrfs_extent_data_ref *dref; | |
553 | int count; | |
554 | u64 root; | |
555 | ||
556 | dref = btrfs_item_ptr(leaf, slot, | |
557 | struct btrfs_extent_data_ref); | |
558 | count = btrfs_extent_data_ref_count(leaf, dref); | |
559 | key.objectid = btrfs_extent_data_ref_objectid(leaf, | |
560 | dref); | |
561 | key.type = BTRFS_EXTENT_DATA_KEY; | |
562 | key.offset = btrfs_extent_data_ref_offset(leaf, dref); | |
563 | root = btrfs_extent_data_ref_root(leaf, dref); | |
564 | ret = __add_prelim_ref(prefs, root, &key, 0, 0, | |
565 | bytenr, count); | |
566 | break; | |
567 | } | |
568 | default: | |
569 | WARN_ON(1); | |
570 | } | |
571 | BUG_ON(ret); | |
572 | } | |
573 | ||
574 | return ret; | |
575 | } | |
576 | ||
577 | /* | |
578 | * this adds all existing backrefs (inline backrefs, backrefs and delayed | |
579 | * refs) for the given bytenr to the refs list, merges duplicates and resolves | |
580 | * indirect refs to their parent bytenr. | |
581 | * When roots are found, they're added to the roots list | |
582 | * | |
583 | * FIXME some caching might speed things up | |
584 | */ | |
585 | static int find_parent_nodes(struct btrfs_trans_handle *trans, | |
586 | struct btrfs_fs_info *fs_info, u64 bytenr, | |
587 | u64 seq, struct ulist *refs, struct ulist *roots) | |
588 | { | |
589 | struct btrfs_key key; | |
590 | struct btrfs_path *path; | |
591 | struct btrfs_key info_key = { 0 }; | |
592 | struct btrfs_delayed_ref_root *delayed_refs = NULL; | |
d3b01064 | 593 | struct btrfs_delayed_ref_head *head; |
8da6d581 JS |
594 | int info_level = 0; |
595 | int ret; | |
7a3ae2f8 | 596 | int search_commit_root = (trans == BTRFS_BACKREF_SEARCH_COMMIT_ROOT); |
8da6d581 JS |
597 | struct list_head prefs_delayed; |
598 | struct list_head prefs; | |
599 | struct __prelim_ref *ref; | |
600 | ||
601 | INIT_LIST_HEAD(&prefs); | |
602 | INIT_LIST_HEAD(&prefs_delayed); | |
603 | ||
604 | key.objectid = bytenr; | |
605 | key.type = BTRFS_EXTENT_ITEM_KEY; | |
606 | key.offset = (u64)-1; | |
607 | ||
608 | path = btrfs_alloc_path(); | |
609 | if (!path) | |
610 | return -ENOMEM; | |
7a3ae2f8 | 611 | path->search_commit_root = !!search_commit_root; |
8da6d581 JS |
612 | |
613 | /* | |
614 | * grab both a lock on the path and a lock on the delayed ref head. | |
615 | * We need both to get a consistent picture of how the refs look | |
616 | * at a specified point in time | |
617 | */ | |
618 | again: | |
d3b01064 LZ |
619 | head = NULL; |
620 | ||
8da6d581 JS |
621 | ret = btrfs_search_slot(trans, fs_info->extent_root, &key, path, 0, 0); |
622 | if (ret < 0) | |
623 | goto out; | |
624 | BUG_ON(ret == 0); | |
625 | ||
7a3ae2f8 JS |
626 | if (trans != BTRFS_BACKREF_SEARCH_COMMIT_ROOT) { |
627 | /* | |
628 | * look if there are updates for this ref queued and lock the | |
629 | * head | |
630 | */ | |
631 | delayed_refs = &trans->transaction->delayed_refs; | |
632 | spin_lock(&delayed_refs->lock); | |
633 | head = btrfs_find_delayed_ref_head(trans, bytenr); | |
634 | if (head) { | |
635 | if (!mutex_trylock(&head->mutex)) { | |
636 | atomic_inc(&head->node.refs); | |
637 | spin_unlock(&delayed_refs->lock); | |
638 | ||
639 | btrfs_release_path(path); | |
640 | ||
641 | /* | |
642 | * Mutex was contended, block until it's | |
643 | * released and try again | |
644 | */ | |
645 | mutex_lock(&head->mutex); | |
646 | mutex_unlock(&head->mutex); | |
647 | btrfs_put_delayed_ref(&head->node); | |
648 | goto again; | |
649 | } | |
650 | ret = __add_delayed_refs(head, seq, &info_key, | |
651 | &prefs_delayed); | |
652 | if (ret) { | |
653 | spin_unlock(&delayed_refs->lock); | |
654 | goto out; | |
655 | } | |
d3b01064 | 656 | } |
7a3ae2f8 | 657 | spin_unlock(&delayed_refs->lock); |
8da6d581 | 658 | } |
8da6d581 JS |
659 | |
660 | if (path->slots[0]) { | |
661 | struct extent_buffer *leaf; | |
662 | int slot; | |
663 | ||
dadcaf78 | 664 | path->slots[0]--; |
8da6d581 | 665 | leaf = path->nodes[0]; |
dadcaf78 | 666 | slot = path->slots[0]; |
8da6d581 JS |
667 | btrfs_item_key_to_cpu(leaf, &key, slot); |
668 | if (key.objectid == bytenr && | |
669 | key.type == BTRFS_EXTENT_ITEM_KEY) { | |
670 | ret = __add_inline_refs(fs_info, path, bytenr, | |
671 | &info_key, &info_level, &prefs); | |
672 | if (ret) | |
673 | goto out; | |
674 | ret = __add_keyed_refs(fs_info, path, bytenr, &info_key, | |
675 | info_level, &prefs); | |
676 | if (ret) | |
677 | goto out; | |
678 | } | |
679 | } | |
680 | btrfs_release_path(path); | |
681 | ||
682 | /* | |
683 | * when adding the delayed refs above, the info_key might not have | |
684 | * been known yet. Go over the list and replace the missing keys | |
685 | */ | |
686 | list_for_each_entry(ref, &prefs_delayed, list) { | |
687 | if ((ref->key.offset | ref->key.type | ref->key.objectid) == 0) | |
688 | memcpy(&ref->key, &info_key, sizeof(ref->key)); | |
689 | } | |
690 | list_splice_init(&prefs_delayed, &prefs); | |
691 | ||
692 | ret = __merge_refs(&prefs, 1); | |
693 | if (ret) | |
694 | goto out; | |
695 | ||
7a3ae2f8 | 696 | ret = __resolve_indirect_refs(fs_info, search_commit_root, &prefs); |
8da6d581 JS |
697 | if (ret) |
698 | goto out; | |
699 | ||
700 | ret = __merge_refs(&prefs, 2); | |
701 | if (ret) | |
702 | goto out; | |
703 | ||
704 | while (!list_empty(&prefs)) { | |
705 | ref = list_first_entry(&prefs, struct __prelim_ref, list); | |
706 | list_del(&ref->list); | |
707 | if (ref->count < 0) | |
708 | WARN_ON(1); | |
709 | if (ref->count && ref->root_id && ref->parent == 0) { | |
710 | /* no parent == root of tree */ | |
711 | ret = ulist_add(roots, ref->root_id, 0, GFP_NOFS); | |
712 | BUG_ON(ret < 0); | |
713 | } | |
714 | if (ref->count && ref->parent) { | |
715 | ret = ulist_add(refs, ref->parent, 0, GFP_NOFS); | |
716 | BUG_ON(ret < 0); | |
717 | } | |
718 | kfree(ref); | |
719 | } | |
720 | ||
721 | out: | |
722 | if (head) | |
723 | mutex_unlock(&head->mutex); | |
724 | btrfs_free_path(path); | |
725 | while (!list_empty(&prefs)) { | |
726 | ref = list_first_entry(&prefs, struct __prelim_ref, list); | |
727 | list_del(&ref->list); | |
728 | kfree(ref); | |
729 | } | |
730 | while (!list_empty(&prefs_delayed)) { | |
731 | ref = list_first_entry(&prefs_delayed, struct __prelim_ref, | |
732 | list); | |
733 | list_del(&ref->list); | |
734 | kfree(ref); | |
735 | } | |
736 | ||
737 | return ret; | |
738 | } | |
739 | ||
740 | /* | |
741 | * Finds all leafs with a reference to the specified combination of bytenr and | |
742 | * offset. key_list_head will point to a list of corresponding keys (caller must | |
743 | * free each list element). The leafs will be stored in the leafs ulist, which | |
744 | * must be freed with ulist_free. | |
745 | * | |
746 | * returns 0 on success, <0 on error | |
747 | */ | |
748 | static int btrfs_find_all_leafs(struct btrfs_trans_handle *trans, | |
749 | struct btrfs_fs_info *fs_info, u64 bytenr, | |
750 | u64 num_bytes, u64 seq, struct ulist **leafs) | |
751 | { | |
752 | struct ulist *tmp; | |
753 | int ret; | |
754 | ||
755 | tmp = ulist_alloc(GFP_NOFS); | |
756 | if (!tmp) | |
757 | return -ENOMEM; | |
758 | *leafs = ulist_alloc(GFP_NOFS); | |
759 | if (!*leafs) { | |
760 | ulist_free(tmp); | |
761 | return -ENOMEM; | |
762 | } | |
763 | ||
764 | ret = find_parent_nodes(trans, fs_info, bytenr, seq, *leafs, tmp); | |
765 | ulist_free(tmp); | |
766 | ||
767 | if (ret < 0 && ret != -ENOENT) { | |
768 | ulist_free(*leafs); | |
769 | return ret; | |
770 | } | |
771 | ||
772 | return 0; | |
773 | } | |
774 | ||
775 | /* | |
776 | * walk all backrefs for a given extent to find all roots that reference this | |
777 | * extent. Walking a backref means finding all extents that reference this | |
778 | * extent and in turn walk the backrefs of those, too. Naturally this is a | |
779 | * recursive process, but here it is implemented in an iterative fashion: We | |
780 | * find all referencing extents for the extent in question and put them on a | |
781 | * list. In turn, we find all referencing extents for those, further appending | |
782 | * to the list. The way we iterate the list allows adding more elements after | |
783 | * the current while iterating. The process stops when we reach the end of the | |
784 | * list. Found roots are added to the roots list. | |
785 | * | |
786 | * returns 0 on success, < 0 on error. | |
787 | */ | |
788 | int btrfs_find_all_roots(struct btrfs_trans_handle *trans, | |
789 | struct btrfs_fs_info *fs_info, u64 bytenr, | |
790 | u64 num_bytes, u64 seq, struct ulist **roots) | |
791 | { | |
792 | struct ulist *tmp; | |
793 | struct ulist_node *node = NULL; | |
cd1b413c | 794 | struct ulist_iterator uiter; |
8da6d581 JS |
795 | int ret; |
796 | ||
797 | tmp = ulist_alloc(GFP_NOFS); | |
798 | if (!tmp) | |
799 | return -ENOMEM; | |
800 | *roots = ulist_alloc(GFP_NOFS); | |
801 | if (!*roots) { | |
802 | ulist_free(tmp); | |
803 | return -ENOMEM; | |
804 | } | |
805 | ||
cd1b413c | 806 | ULIST_ITER_INIT(&uiter); |
8da6d581 JS |
807 | while (1) { |
808 | ret = find_parent_nodes(trans, fs_info, bytenr, seq, | |
809 | tmp, *roots); | |
810 | if (ret < 0 && ret != -ENOENT) { | |
811 | ulist_free(tmp); | |
812 | ulist_free(*roots); | |
813 | return ret; | |
814 | } | |
cd1b413c | 815 | node = ulist_next(tmp, &uiter); |
8da6d581 JS |
816 | if (!node) |
817 | break; | |
818 | bytenr = node->val; | |
819 | } | |
820 | ||
821 | ulist_free(tmp); | |
822 | return 0; | |
823 | } | |
824 | ||
825 | ||
a542ad1b JS |
826 | static int __inode_info(u64 inum, u64 ioff, u8 key_type, |
827 | struct btrfs_root *fs_root, struct btrfs_path *path, | |
828 | struct btrfs_key *found_key) | |
829 | { | |
830 | int ret; | |
831 | struct btrfs_key key; | |
832 | struct extent_buffer *eb; | |
833 | ||
834 | key.type = key_type; | |
835 | key.objectid = inum; | |
836 | key.offset = ioff; | |
837 | ||
838 | ret = btrfs_search_slot(NULL, fs_root, &key, path, 0, 0); | |
839 | if (ret < 0) | |
840 | return ret; | |
841 | ||
842 | eb = path->nodes[0]; | |
843 | if (ret && path->slots[0] >= btrfs_header_nritems(eb)) { | |
844 | ret = btrfs_next_leaf(fs_root, path); | |
845 | if (ret) | |
846 | return ret; | |
847 | eb = path->nodes[0]; | |
848 | } | |
849 | ||
850 | btrfs_item_key_to_cpu(eb, found_key, path->slots[0]); | |
851 | if (found_key->type != key.type || found_key->objectid != key.objectid) | |
852 | return 1; | |
853 | ||
854 | return 0; | |
855 | } | |
856 | ||
857 | /* | |
858 | * this makes the path point to (inum INODE_ITEM ioff) | |
859 | */ | |
860 | int inode_item_info(u64 inum, u64 ioff, struct btrfs_root *fs_root, | |
861 | struct btrfs_path *path) | |
862 | { | |
863 | struct btrfs_key key; | |
864 | return __inode_info(inum, ioff, BTRFS_INODE_ITEM_KEY, fs_root, path, | |
865 | &key); | |
866 | } | |
867 | ||
868 | static int inode_ref_info(u64 inum, u64 ioff, struct btrfs_root *fs_root, | |
869 | struct btrfs_path *path, | |
870 | struct btrfs_key *found_key) | |
871 | { | |
872 | return __inode_info(inum, ioff, BTRFS_INODE_REF_KEY, fs_root, path, | |
873 | found_key); | |
874 | } | |
875 | ||
876 | /* | |
877 | * this iterates to turn a btrfs_inode_ref into a full filesystem path. elements | |
878 | * of the path are separated by '/' and the path is guaranteed to be | |
879 | * 0-terminated. the path is only given within the current file system. | |
880 | * Therefore, it never starts with a '/'. the caller is responsible to provide | |
881 | * "size" bytes in "dest". the dest buffer will be filled backwards. finally, | |
882 | * the start point of the resulting string is returned. this pointer is within | |
883 | * dest, normally. | |
884 | * in case the path buffer would overflow, the pointer is decremented further | |
885 | * as if output was written to the buffer, though no more output is actually | |
886 | * generated. that way, the caller can determine how much space would be | |
887 | * required for the path to fit into the buffer. in that case, the returned | |
888 | * value will be smaller than dest. callers must check this! | |
889 | */ | |
890 | static char *iref_to_path(struct btrfs_root *fs_root, struct btrfs_path *path, | |
891 | struct btrfs_inode_ref *iref, | |
892 | struct extent_buffer *eb_in, u64 parent, | |
893 | char *dest, u32 size) | |
894 | { | |
895 | u32 len; | |
896 | int slot; | |
897 | u64 next_inum; | |
898 | int ret; | |
899 | s64 bytes_left = size - 1; | |
900 | struct extent_buffer *eb = eb_in; | |
901 | struct btrfs_key found_key; | |
b916a59a | 902 | int leave_spinning = path->leave_spinning; |
a542ad1b JS |
903 | |
904 | if (bytes_left >= 0) | |
905 | dest[bytes_left] = '\0'; | |
906 | ||
b916a59a | 907 | path->leave_spinning = 1; |
a542ad1b JS |
908 | while (1) { |
909 | len = btrfs_inode_ref_name_len(eb, iref); | |
910 | bytes_left -= len; | |
911 | if (bytes_left >= 0) | |
912 | read_extent_buffer(eb, dest + bytes_left, | |
913 | (unsigned long)(iref + 1), len); | |
b916a59a JS |
914 | if (eb != eb_in) { |
915 | btrfs_tree_read_unlock_blocking(eb); | |
a542ad1b | 916 | free_extent_buffer(eb); |
b916a59a | 917 | } |
a542ad1b | 918 | ret = inode_ref_info(parent, 0, fs_root, path, &found_key); |
8f24b496 JS |
919 | if (ret > 0) |
920 | ret = -ENOENT; | |
a542ad1b JS |
921 | if (ret) |
922 | break; | |
923 | next_inum = found_key.offset; | |
924 | ||
925 | /* regular exit ahead */ | |
926 | if (parent == next_inum) | |
927 | break; | |
928 | ||
929 | slot = path->slots[0]; | |
930 | eb = path->nodes[0]; | |
931 | /* make sure we can use eb after releasing the path */ | |
b916a59a | 932 | if (eb != eb_in) { |
a542ad1b | 933 | atomic_inc(&eb->refs); |
b916a59a JS |
934 | btrfs_tree_read_lock(eb); |
935 | btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK); | |
936 | } | |
a542ad1b JS |
937 | btrfs_release_path(path); |
938 | ||
939 | iref = btrfs_item_ptr(eb, slot, struct btrfs_inode_ref); | |
940 | parent = next_inum; | |
941 | --bytes_left; | |
942 | if (bytes_left >= 0) | |
943 | dest[bytes_left] = '/'; | |
944 | } | |
945 | ||
946 | btrfs_release_path(path); | |
b916a59a | 947 | path->leave_spinning = leave_spinning; |
a542ad1b JS |
948 | |
949 | if (ret) | |
950 | return ERR_PTR(ret); | |
951 | ||
952 | return dest + bytes_left; | |
953 | } | |
954 | ||
955 | /* | |
956 | * this makes the path point to (logical EXTENT_ITEM *) | |
957 | * returns BTRFS_EXTENT_FLAG_DATA for data, BTRFS_EXTENT_FLAG_TREE_BLOCK for | |
958 | * tree blocks and <0 on error. | |
959 | */ | |
960 | int extent_from_logical(struct btrfs_fs_info *fs_info, u64 logical, | |
961 | struct btrfs_path *path, struct btrfs_key *found_key) | |
962 | { | |
963 | int ret; | |
964 | u64 flags; | |
965 | u32 item_size; | |
966 | struct extent_buffer *eb; | |
967 | struct btrfs_extent_item *ei; | |
968 | struct btrfs_key key; | |
969 | ||
970 | key.type = BTRFS_EXTENT_ITEM_KEY; | |
971 | key.objectid = logical; | |
972 | key.offset = (u64)-1; | |
973 | ||
974 | ret = btrfs_search_slot(NULL, fs_info->extent_root, &key, path, 0, 0); | |
975 | if (ret < 0) | |
976 | return ret; | |
977 | ret = btrfs_previous_item(fs_info->extent_root, path, | |
978 | 0, BTRFS_EXTENT_ITEM_KEY); | |
979 | if (ret < 0) | |
980 | return ret; | |
981 | ||
982 | btrfs_item_key_to_cpu(path->nodes[0], found_key, path->slots[0]); | |
983 | if (found_key->type != BTRFS_EXTENT_ITEM_KEY || | |
984 | found_key->objectid > logical || | |
4692cf58 JS |
985 | found_key->objectid + found_key->offset <= logical) { |
986 | pr_debug("logical %llu is not within any extent\n", | |
987 | (unsigned long long)logical); | |
a542ad1b | 988 | return -ENOENT; |
4692cf58 | 989 | } |
a542ad1b JS |
990 | |
991 | eb = path->nodes[0]; | |
992 | item_size = btrfs_item_size_nr(eb, path->slots[0]); | |
993 | BUG_ON(item_size < sizeof(*ei)); | |
994 | ||
995 | ei = btrfs_item_ptr(eb, path->slots[0], struct btrfs_extent_item); | |
996 | flags = btrfs_extent_flags(eb, ei); | |
997 | ||
4692cf58 JS |
998 | pr_debug("logical %llu is at position %llu within the extent (%llu " |
999 | "EXTENT_ITEM %llu) flags %#llx size %u\n", | |
1000 | (unsigned long long)logical, | |
1001 | (unsigned long long)(logical - found_key->objectid), | |
1002 | (unsigned long long)found_key->objectid, | |
1003 | (unsigned long long)found_key->offset, | |
1004 | (unsigned long long)flags, item_size); | |
a542ad1b JS |
1005 | if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) |
1006 | return BTRFS_EXTENT_FLAG_TREE_BLOCK; | |
1007 | if (flags & BTRFS_EXTENT_FLAG_DATA) | |
1008 | return BTRFS_EXTENT_FLAG_DATA; | |
1009 | ||
1010 | return -EIO; | |
1011 | } | |
1012 | ||
1013 | /* | |
1014 | * helper function to iterate extent inline refs. ptr must point to a 0 value | |
1015 | * for the first call and may be modified. it is used to track state. | |
1016 | * if more refs exist, 0 is returned and the next call to | |
1017 | * __get_extent_inline_ref must pass the modified ptr parameter to get the | |
1018 | * next ref. after the last ref was processed, 1 is returned. | |
1019 | * returns <0 on error | |
1020 | */ | |
1021 | static int __get_extent_inline_ref(unsigned long *ptr, struct extent_buffer *eb, | |
1022 | struct btrfs_extent_item *ei, u32 item_size, | |
1023 | struct btrfs_extent_inline_ref **out_eiref, | |
1024 | int *out_type) | |
1025 | { | |
1026 | unsigned long end; | |
1027 | u64 flags; | |
1028 | struct btrfs_tree_block_info *info; | |
1029 | ||
1030 | if (!*ptr) { | |
1031 | /* first call */ | |
1032 | flags = btrfs_extent_flags(eb, ei); | |
1033 | if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) { | |
1034 | info = (struct btrfs_tree_block_info *)(ei + 1); | |
1035 | *out_eiref = | |
1036 | (struct btrfs_extent_inline_ref *)(info + 1); | |
1037 | } else { | |
1038 | *out_eiref = (struct btrfs_extent_inline_ref *)(ei + 1); | |
1039 | } | |
1040 | *ptr = (unsigned long)*out_eiref; | |
1041 | if ((void *)*ptr >= (void *)ei + item_size) | |
1042 | return -ENOENT; | |
1043 | } | |
1044 | ||
1045 | end = (unsigned long)ei + item_size; | |
1046 | *out_eiref = (struct btrfs_extent_inline_ref *)*ptr; | |
1047 | *out_type = btrfs_extent_inline_ref_type(eb, *out_eiref); | |
1048 | ||
1049 | *ptr += btrfs_extent_inline_ref_size(*out_type); | |
1050 | WARN_ON(*ptr > end); | |
1051 | if (*ptr == end) | |
1052 | return 1; /* last */ | |
1053 | ||
1054 | return 0; | |
1055 | } | |
1056 | ||
1057 | /* | |
1058 | * reads the tree block backref for an extent. tree level and root are returned | |
1059 | * through out_level and out_root. ptr must point to a 0 value for the first | |
1060 | * call and may be modified (see __get_extent_inline_ref comment). | |
1061 | * returns 0 if data was provided, 1 if there was no more data to provide or | |
1062 | * <0 on error. | |
1063 | */ | |
1064 | int tree_backref_for_extent(unsigned long *ptr, struct extent_buffer *eb, | |
1065 | struct btrfs_extent_item *ei, u32 item_size, | |
1066 | u64 *out_root, u8 *out_level) | |
1067 | { | |
1068 | int ret; | |
1069 | int type; | |
1070 | struct btrfs_tree_block_info *info; | |
1071 | struct btrfs_extent_inline_ref *eiref; | |
1072 | ||
1073 | if (*ptr == (unsigned long)-1) | |
1074 | return 1; | |
1075 | ||
1076 | while (1) { | |
1077 | ret = __get_extent_inline_ref(ptr, eb, ei, item_size, | |
1078 | &eiref, &type); | |
1079 | if (ret < 0) | |
1080 | return ret; | |
1081 | ||
1082 | if (type == BTRFS_TREE_BLOCK_REF_KEY || | |
1083 | type == BTRFS_SHARED_BLOCK_REF_KEY) | |
1084 | break; | |
1085 | ||
1086 | if (ret == 1) | |
1087 | return 1; | |
1088 | } | |
1089 | ||
1090 | /* we can treat both ref types equally here */ | |
1091 | info = (struct btrfs_tree_block_info *)(ei + 1); | |
1092 | *out_root = btrfs_extent_inline_ref_offset(eb, eiref); | |
1093 | *out_level = btrfs_tree_block_level(eb, info); | |
1094 | ||
1095 | if (ret == 1) | |
1096 | *ptr = (unsigned long)-1; | |
1097 | ||
1098 | return 0; | |
1099 | } | |
1100 | ||
7a3ae2f8 | 1101 | static int iterate_leaf_refs(struct btrfs_fs_info *fs_info, u64 logical, |
4692cf58 JS |
1102 | u64 orig_extent_item_objectid, |
1103 | u64 extent_item_pos, u64 root, | |
1104 | iterate_extent_inodes_t *iterate, void *ctx) | |
a542ad1b JS |
1105 | { |
1106 | u64 disk_byte; | |
1107 | struct btrfs_key key; | |
1108 | struct btrfs_file_extent_item *fi; | |
1109 | struct extent_buffer *eb; | |
1110 | int slot; | |
1111 | int nritems; | |
4692cf58 JS |
1112 | int ret = 0; |
1113 | int extent_type; | |
1114 | u64 data_offset; | |
1115 | u64 data_len; | |
a542ad1b JS |
1116 | |
1117 | eb = read_tree_block(fs_info->tree_root, logical, | |
1118 | fs_info->tree_root->leafsize, 0); | |
1119 | if (!eb) | |
1120 | return -EIO; | |
1121 | ||
1122 | /* | |
1123 | * from the shared data ref, we only have the leaf but we need | |
1124 | * the key. thus, we must look into all items and see that we | |
1125 | * find one (some) with a reference to our extent item. | |
1126 | */ | |
1127 | nritems = btrfs_header_nritems(eb); | |
1128 | for (slot = 0; slot < nritems; ++slot) { | |
1129 | btrfs_item_key_to_cpu(eb, &key, slot); | |
1130 | if (key.type != BTRFS_EXTENT_DATA_KEY) | |
1131 | continue; | |
1132 | fi = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item); | |
4692cf58 JS |
1133 | extent_type = btrfs_file_extent_type(eb, fi); |
1134 | if (extent_type == BTRFS_FILE_EXTENT_INLINE) | |
1135 | continue; | |
1136 | /* don't skip BTRFS_FILE_EXTENT_PREALLOC, we can handle that */ | |
a542ad1b | 1137 | disk_byte = btrfs_file_extent_disk_bytenr(eb, fi); |
4692cf58 JS |
1138 | if (disk_byte != orig_extent_item_objectid) |
1139 | continue; | |
a542ad1b | 1140 | |
4692cf58 JS |
1141 | data_offset = btrfs_file_extent_offset(eb, fi); |
1142 | data_len = btrfs_file_extent_num_bytes(eb, fi); | |
1143 | ||
1144 | if (extent_item_pos < data_offset || | |
1145 | extent_item_pos >= data_offset + data_len) | |
1146 | continue; | |
1147 | ||
1148 | pr_debug("ref for %llu resolved, key (%llu EXTEND_DATA %llu), " | |
1149 | "root %llu\n", orig_extent_item_objectid, | |
1150 | key.objectid, key.offset, root); | |
1151 | ret = iterate(key.objectid, | |
1152 | key.offset + (extent_item_pos - data_offset), | |
1153 | root, ctx); | |
1154 | if (ret) { | |
1155 | pr_debug("stopping iteration because ret=%d\n", ret); | |
1156 | break; | |
1157 | } | |
a542ad1b JS |
1158 | } |
1159 | ||
1160 | free_extent_buffer(eb); | |
4692cf58 | 1161 | |
a542ad1b JS |
1162 | return ret; |
1163 | } | |
1164 | ||
1165 | /* | |
1166 | * calls iterate() for every inode that references the extent identified by | |
4692cf58 | 1167 | * the given parameters. |
a542ad1b JS |
1168 | * when the iterator function returns a non-zero value, iteration stops. |
1169 | */ | |
1170 | int iterate_extent_inodes(struct btrfs_fs_info *fs_info, | |
4692cf58 | 1171 | u64 extent_item_objectid, u64 extent_item_pos, |
7a3ae2f8 | 1172 | int search_commit_root, |
a542ad1b JS |
1173 | iterate_extent_inodes_t *iterate, void *ctx) |
1174 | { | |
a542ad1b | 1175 | int ret; |
a542ad1b JS |
1176 | struct list_head data_refs = LIST_HEAD_INIT(data_refs); |
1177 | struct list_head shared_refs = LIST_HEAD_INIT(shared_refs); | |
4692cf58 | 1178 | struct btrfs_trans_handle *trans; |
7a3ae2f8 JS |
1179 | struct ulist *refs = NULL; |
1180 | struct ulist *roots = NULL; | |
4692cf58 JS |
1181 | struct ulist_node *ref_node = NULL; |
1182 | struct ulist_node *root_node = NULL; | |
1183 | struct seq_list seq_elem; | |
cd1b413c JS |
1184 | struct ulist_iterator ref_uiter; |
1185 | struct ulist_iterator root_uiter; | |
7a3ae2f8 | 1186 | struct btrfs_delayed_ref_root *delayed_refs = NULL; |
a542ad1b | 1187 | |
4692cf58 JS |
1188 | pr_debug("resolving all inodes for extent %llu\n", |
1189 | extent_item_objectid); | |
a542ad1b | 1190 | |
7a3ae2f8 JS |
1191 | if (search_commit_root) { |
1192 | trans = BTRFS_BACKREF_SEARCH_COMMIT_ROOT; | |
1193 | } else { | |
1194 | trans = btrfs_join_transaction(fs_info->extent_root); | |
1195 | if (IS_ERR(trans)) | |
1196 | return PTR_ERR(trans); | |
1197 | ||
1198 | delayed_refs = &trans->transaction->delayed_refs; | |
1199 | spin_lock(&delayed_refs->lock); | |
1200 | btrfs_get_delayed_seq(delayed_refs, &seq_elem); | |
1201 | spin_unlock(&delayed_refs->lock); | |
1202 | } | |
a542ad1b | 1203 | |
4692cf58 JS |
1204 | ret = btrfs_find_all_leafs(trans, fs_info, extent_item_objectid, |
1205 | extent_item_pos, seq_elem.seq, | |
1206 | &refs); | |
a542ad1b | 1207 | |
4692cf58 JS |
1208 | if (ret) |
1209 | goto out; | |
a542ad1b | 1210 | |
cd1b413c JS |
1211 | ULIST_ITER_INIT(&ref_uiter); |
1212 | while (!ret && (ref_node = ulist_next(refs, &ref_uiter))) { | |
4692cf58 JS |
1213 | ret = btrfs_find_all_roots(trans, fs_info, ref_node->val, -1, |
1214 | seq_elem.seq, &roots); | |
1215 | if (ret) | |
1216 | break; | |
cd1b413c JS |
1217 | ULIST_ITER_INIT(&root_uiter); |
1218 | while (!ret && (root_node = ulist_next(roots, &root_uiter))) { | |
4692cf58 JS |
1219 | pr_debug("root %llu references leaf %llu\n", |
1220 | root_node->val, ref_node->val); | |
7a3ae2f8 | 1221 | ret = iterate_leaf_refs(fs_info, ref_node->val, |
4692cf58 JS |
1222 | extent_item_objectid, |
1223 | extent_item_pos, root_node->val, | |
1224 | iterate, ctx); | |
1225 | } | |
a542ad1b JS |
1226 | } |
1227 | ||
4692cf58 JS |
1228 | ulist_free(refs); |
1229 | ulist_free(roots); | |
1230 | out: | |
7a3ae2f8 JS |
1231 | if (!search_commit_root) { |
1232 | btrfs_put_delayed_seq(delayed_refs, &seq_elem); | |
1233 | btrfs_end_transaction(trans, fs_info->extent_root); | |
1234 | } | |
1235 | ||
a542ad1b JS |
1236 | return ret; |
1237 | } | |
1238 | ||
1239 | int iterate_inodes_from_logical(u64 logical, struct btrfs_fs_info *fs_info, | |
1240 | struct btrfs_path *path, | |
1241 | iterate_extent_inodes_t *iterate, void *ctx) | |
1242 | { | |
1243 | int ret; | |
4692cf58 | 1244 | u64 extent_item_pos; |
a542ad1b | 1245 | struct btrfs_key found_key; |
7a3ae2f8 | 1246 | int search_commit_root = path->search_commit_root; |
a542ad1b JS |
1247 | |
1248 | ret = extent_from_logical(fs_info, logical, path, | |
1249 | &found_key); | |
4692cf58 | 1250 | btrfs_release_path(path); |
a542ad1b JS |
1251 | if (ret & BTRFS_EXTENT_FLAG_TREE_BLOCK) |
1252 | ret = -EINVAL; | |
1253 | if (ret < 0) | |
1254 | return ret; | |
1255 | ||
4692cf58 | 1256 | extent_item_pos = logical - found_key.objectid; |
7a3ae2f8 JS |
1257 | ret = iterate_extent_inodes(fs_info, found_key.objectid, |
1258 | extent_item_pos, search_commit_root, | |
1259 | iterate, ctx); | |
a542ad1b JS |
1260 | |
1261 | return ret; | |
1262 | } | |
1263 | ||
1264 | static int iterate_irefs(u64 inum, struct btrfs_root *fs_root, | |
1265 | struct btrfs_path *path, | |
1266 | iterate_irefs_t *iterate, void *ctx) | |
1267 | { | |
aefc1eb1 | 1268 | int ret = 0; |
a542ad1b JS |
1269 | int slot; |
1270 | u32 cur; | |
1271 | u32 len; | |
1272 | u32 name_len; | |
1273 | u64 parent = 0; | |
1274 | int found = 0; | |
1275 | struct extent_buffer *eb; | |
1276 | struct btrfs_item *item; | |
1277 | struct btrfs_inode_ref *iref; | |
1278 | struct btrfs_key found_key; | |
1279 | ||
aefc1eb1 | 1280 | while (!ret) { |
b916a59a | 1281 | path->leave_spinning = 1; |
a542ad1b JS |
1282 | ret = inode_ref_info(inum, parent ? parent+1 : 0, fs_root, path, |
1283 | &found_key); | |
1284 | if (ret < 0) | |
1285 | break; | |
1286 | if (ret) { | |
1287 | ret = found ? 0 : -ENOENT; | |
1288 | break; | |
1289 | } | |
1290 | ++found; | |
1291 | ||
1292 | parent = found_key.offset; | |
1293 | slot = path->slots[0]; | |
1294 | eb = path->nodes[0]; | |
1295 | /* make sure we can use eb after releasing the path */ | |
1296 | atomic_inc(&eb->refs); | |
b916a59a JS |
1297 | btrfs_tree_read_lock(eb); |
1298 | btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK); | |
a542ad1b JS |
1299 | btrfs_release_path(path); |
1300 | ||
1301 | item = btrfs_item_nr(eb, slot); | |
1302 | iref = btrfs_item_ptr(eb, slot, struct btrfs_inode_ref); | |
1303 | ||
1304 | for (cur = 0; cur < btrfs_item_size(eb, item); cur += len) { | |
1305 | name_len = btrfs_inode_ref_name_len(eb, iref); | |
1306 | /* path must be released before calling iterate()! */ | |
4692cf58 JS |
1307 | pr_debug("following ref at offset %u for inode %llu in " |
1308 | "tree %llu\n", cur, | |
1309 | (unsigned long long)found_key.objectid, | |
1310 | (unsigned long long)fs_root->objectid); | |
a542ad1b | 1311 | ret = iterate(parent, iref, eb, ctx); |
aefc1eb1 | 1312 | if (ret) |
a542ad1b | 1313 | break; |
a542ad1b JS |
1314 | len = sizeof(*iref) + name_len; |
1315 | iref = (struct btrfs_inode_ref *)((char *)iref + len); | |
1316 | } | |
b916a59a | 1317 | btrfs_tree_read_unlock_blocking(eb); |
a542ad1b JS |
1318 | free_extent_buffer(eb); |
1319 | } | |
1320 | ||
1321 | btrfs_release_path(path); | |
1322 | ||
1323 | return ret; | |
1324 | } | |
1325 | ||
1326 | /* | |
1327 | * returns 0 if the path could be dumped (probably truncated) | |
1328 | * returns <0 in case of an error | |
1329 | */ | |
1330 | static int inode_to_path(u64 inum, struct btrfs_inode_ref *iref, | |
1331 | struct extent_buffer *eb, void *ctx) | |
1332 | { | |
1333 | struct inode_fs_paths *ipath = ctx; | |
1334 | char *fspath; | |
1335 | char *fspath_min; | |
1336 | int i = ipath->fspath->elem_cnt; | |
1337 | const int s_ptr = sizeof(char *); | |
1338 | u32 bytes_left; | |
1339 | ||
1340 | bytes_left = ipath->fspath->bytes_left > s_ptr ? | |
1341 | ipath->fspath->bytes_left - s_ptr : 0; | |
1342 | ||
740c3d22 | 1343 | fspath_min = (char *)ipath->fspath->val + (i + 1) * s_ptr; |
a542ad1b JS |
1344 | fspath = iref_to_path(ipath->fs_root, ipath->btrfs_path, iref, eb, |
1345 | inum, fspath_min, bytes_left); | |
1346 | if (IS_ERR(fspath)) | |
1347 | return PTR_ERR(fspath); | |
1348 | ||
1349 | if (fspath > fspath_min) { | |
4692cf58 | 1350 | pr_debug("path resolved: %s\n", fspath); |
745c4d8e | 1351 | ipath->fspath->val[i] = (u64)(unsigned long)fspath; |
a542ad1b JS |
1352 | ++ipath->fspath->elem_cnt; |
1353 | ipath->fspath->bytes_left = fspath - fspath_min; | |
1354 | } else { | |
4692cf58 JS |
1355 | pr_debug("missed path, not enough space. missing bytes: %lu, " |
1356 | "constructed so far: %s\n", | |
1357 | (unsigned long)(fspath_min - fspath), fspath_min); | |
a542ad1b JS |
1358 | ++ipath->fspath->elem_missed; |
1359 | ipath->fspath->bytes_missing += fspath_min - fspath; | |
1360 | ipath->fspath->bytes_left = 0; | |
1361 | } | |
1362 | ||
1363 | return 0; | |
1364 | } | |
1365 | ||
1366 | /* | |
1367 | * this dumps all file system paths to the inode into the ipath struct, provided | |
1368 | * is has been created large enough. each path is zero-terminated and accessed | |
740c3d22 | 1369 | * from ipath->fspath->val[i]. |
a542ad1b | 1370 | * when it returns, there are ipath->fspath->elem_cnt number of paths available |
740c3d22 | 1371 | * in ipath->fspath->val[]. when the allocated space wasn't sufficient, the |
a542ad1b JS |
1372 | * number of missed paths in recored in ipath->fspath->elem_missed, otherwise, |
1373 | * it's zero. ipath->fspath->bytes_missing holds the number of bytes that would | |
1374 | * have been needed to return all paths. | |
1375 | */ | |
1376 | int paths_from_inode(u64 inum, struct inode_fs_paths *ipath) | |
1377 | { | |
1378 | return iterate_irefs(inum, ipath->fs_root, ipath->btrfs_path, | |
1379 | inode_to_path, ipath); | |
1380 | } | |
1381 | ||
a542ad1b JS |
1382 | struct btrfs_data_container *init_data_container(u32 total_bytes) |
1383 | { | |
1384 | struct btrfs_data_container *data; | |
1385 | size_t alloc_bytes; | |
1386 | ||
1387 | alloc_bytes = max_t(size_t, total_bytes, sizeof(*data)); | |
1388 | data = kmalloc(alloc_bytes, GFP_NOFS); | |
1389 | if (!data) | |
1390 | return ERR_PTR(-ENOMEM); | |
1391 | ||
1392 | if (total_bytes >= sizeof(*data)) { | |
1393 | data->bytes_left = total_bytes - sizeof(*data); | |
1394 | data->bytes_missing = 0; | |
1395 | } else { | |
1396 | data->bytes_missing = sizeof(*data) - total_bytes; | |
1397 | data->bytes_left = 0; | |
1398 | } | |
1399 | ||
1400 | data->elem_cnt = 0; | |
1401 | data->elem_missed = 0; | |
1402 | ||
1403 | return data; | |
1404 | } | |
1405 | ||
1406 | /* | |
1407 | * allocates space to return multiple file system paths for an inode. | |
1408 | * total_bytes to allocate are passed, note that space usable for actual path | |
1409 | * information will be total_bytes - sizeof(struct inode_fs_paths). | |
1410 | * the returned pointer must be freed with free_ipath() in the end. | |
1411 | */ | |
1412 | struct inode_fs_paths *init_ipath(s32 total_bytes, struct btrfs_root *fs_root, | |
1413 | struct btrfs_path *path) | |
1414 | { | |
1415 | struct inode_fs_paths *ifp; | |
1416 | struct btrfs_data_container *fspath; | |
1417 | ||
1418 | fspath = init_data_container(total_bytes); | |
1419 | if (IS_ERR(fspath)) | |
1420 | return (void *)fspath; | |
1421 | ||
1422 | ifp = kmalloc(sizeof(*ifp), GFP_NOFS); | |
1423 | if (!ifp) { | |
1424 | kfree(fspath); | |
1425 | return ERR_PTR(-ENOMEM); | |
1426 | } | |
1427 | ||
1428 | ifp->btrfs_path = path; | |
1429 | ifp->fspath = fspath; | |
1430 | ifp->fs_root = fs_root; | |
1431 | ||
1432 | return ifp; | |
1433 | } | |
1434 | ||
1435 | void free_ipath(struct inode_fs_paths *ipath) | |
1436 | { | |
4735fb28 JJ |
1437 | if (!ipath) |
1438 | return; | |
5eb56d25 | 1439 | kfree(ipath->fspath); |
a542ad1b JS |
1440 | kfree(ipath); |
1441 | } |