blob: b41d94a6471b5fc4c7ed7d26781a7fcc05f60d11 [file] [log] [blame]
Jan Schmidta542ad12011-06-13 19:52:59 +02001/*
2 * Copyright (C) 2011 STRATO. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
19#include "ctree.h"
20#include "disk-io.h"
21#include "backref.h"
Jan Schmidt8da6d582011-11-23 18:55:04 +010022#include "ulist.h"
23#include "transaction.h"
24#include "delayed-ref.h"
Jan Schmidtb916a592012-04-13 12:28:08 +020025#include "locking.h"
Jan Schmidta542ad12011-06-13 19:52:59 +020026
Jan Schmidt8da6d582011-11-23 18:55:04 +010027/*
28 * this structure records all encountered refs on the way up to the root
29 */
30struct __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
40static 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
66static 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
77add_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 */
119static int __resolve_indirect_ref(struct btrfs_fs_info *fs_info,
Jan Schmidt7a3ae2f2012-03-23 17:32:28 +0100120 int search_commit_root,
Jan Schmidt8da6d582011-11-23 18:55:04 +0100121 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;
Jan Schmidt7a3ae2f2012-03-23 17:32:28 +0100136 path->search_commit_root = !!search_commit_root;
Jan Schmidt8da6d582011-11-23 18:55:04 +0100137
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);
185out:
186 btrfs_free_path(path);
187 return ret;
188}
189
190/*
191 * resolve all indirect backrefs from the list
192 */
193static int __resolve_indirect_refs(struct btrfs_fs_info *fs_info,
Jan Schmidt7a3ae2f2012-03-23 17:32:28 +0100194 int search_commit_root,
Jan Schmidt8da6d582011-11-23 18:55:04 +0100195 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;
Jan Schmidtcd1b4132012-05-22 14:56:50 +0200204 struct ulist_iterator uiter;
Jan Schmidt8da6d582011-11-23 18:55:04 +0100205
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;
Jan Schmidt7a3ae2f2012-03-23 17:32:28 +0100220 err = __resolve_indirect_ref(fs_info, search_commit_root,
221 ref, parents);
Jan Schmidt8da6d582011-11-23 18:55:04 +0100222 if (err) {
223 if (ret == 0)
224 ret = err;
225 continue;
226 }
227
228 /* we put the first parent into the ref at hand */
Jan Schmidtcd1b4132012-05-22 14:56:50 +0200229 ULIST_ITER_INIT(&uiter);
230 node = ulist_next(parents, &uiter);
Jan Schmidt8da6d582011-11-23 18:55:04 +0100231 ref->parent = node ? node->val : 0;
232
233 /* additional parents require new refs being added here */
Jan Schmidtcd1b4132012-05-22 14:56:50 +0200234 while ((node = ulist_next(parents, &uiter))) {
Jan Schmidt8da6d582011-11-23 18:55:04 +0100235 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 */
257static 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 */
300static 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;
Jan Schmidtb1375d62012-01-26 15:01:11 -0500307 int ret = 0;
Jan Schmidt8da6d582011-11-23 18:55:04 +0100308
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 */
397static 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{
Jan Schmidtb1375d62012-01-26 15:01:11 -0500402 int ret = 0;
Jan Schmidt8da6d582011-11-23 18:55:04 +0100403 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];
416 slot = path->slots[0] - 1;
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 */
499static 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 */
585static 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;
Li Zefand3b01062012-03-03 07:41:15 -0500593 struct btrfs_delayed_ref_head *head;
Jan Schmidt8da6d582011-11-23 18:55:04 +0100594 int info_level = 0;
595 int ret;
Jan Schmidt7a3ae2f2012-03-23 17:32:28 +0100596 int search_commit_root = (trans == BTRFS_BACKREF_SEARCH_COMMIT_ROOT);
Jan Schmidt8da6d582011-11-23 18:55:04 +0100597 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;
Jan Schmidt7a3ae2f2012-03-23 17:32:28 +0100611 path->search_commit_root = !!search_commit_root;
Jan Schmidt8da6d582011-11-23 18:55:04 +0100612
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 */
618again:
Li Zefand3b01062012-03-03 07:41:15 -0500619 head = NULL;
620
Jan Schmidt8da6d582011-11-23 18:55:04 +0100621 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
Jan Schmidt7a3ae2f2012-03-23 17:32:28 +0100626 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);
Jan Schmidt8da6d582011-11-23 18:55:04 +0100638
Jan Schmidt7a3ae2f2012-03-23 17:32:28 +0100639 btrfs_release_path(path);
Jan Schmidt8da6d582011-11-23 18:55:04 +0100640
Jan Schmidt7a3ae2f2012-03-23 17:32:28 +0100641 /*
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 }
Jan Schmidt8da6d582011-11-23 18:55:04 +0100656 }
Jan Schmidt7a3ae2f2012-03-23 17:32:28 +0100657 spin_unlock(&delayed_refs->lock);
Jan Schmidt8da6d582011-11-23 18:55:04 +0100658 }
Jan Schmidt8da6d582011-11-23 18:55:04 +0100659
660 if (path->slots[0]) {
661 struct extent_buffer *leaf;
662 int slot;
663
664 leaf = path->nodes[0];
665 slot = path->slots[0] - 1;
666 btrfs_item_key_to_cpu(leaf, &key, slot);
667 if (key.objectid == bytenr &&
668 key.type == BTRFS_EXTENT_ITEM_KEY) {
669 ret = __add_inline_refs(fs_info, path, bytenr,
670 &info_key, &info_level, &prefs);
671 if (ret)
672 goto out;
673 ret = __add_keyed_refs(fs_info, path, bytenr, &info_key,
674 info_level, &prefs);
675 if (ret)
676 goto out;
677 }
678 }
679 btrfs_release_path(path);
680
681 /*
682 * when adding the delayed refs above, the info_key might not have
683 * been known yet. Go over the list and replace the missing keys
684 */
685 list_for_each_entry(ref, &prefs_delayed, list) {
686 if ((ref->key.offset | ref->key.type | ref->key.objectid) == 0)
687 memcpy(&ref->key, &info_key, sizeof(ref->key));
688 }
689 list_splice_init(&prefs_delayed, &prefs);
690
691 ret = __merge_refs(&prefs, 1);
692 if (ret)
693 goto out;
694
Jan Schmidt7a3ae2f2012-03-23 17:32:28 +0100695 ret = __resolve_indirect_refs(fs_info, search_commit_root, &prefs);
Jan Schmidt8da6d582011-11-23 18:55:04 +0100696 if (ret)
697 goto out;
698
699 ret = __merge_refs(&prefs, 2);
700 if (ret)
701 goto out;
702
703 while (!list_empty(&prefs)) {
704 ref = list_first_entry(&prefs, struct __prelim_ref, list);
705 list_del(&ref->list);
706 if (ref->count < 0)
707 WARN_ON(1);
708 if (ref->count && ref->root_id && ref->parent == 0) {
709 /* no parent == root of tree */
710 ret = ulist_add(roots, ref->root_id, 0, GFP_NOFS);
711 BUG_ON(ret < 0);
712 }
713 if (ref->count && ref->parent) {
714 ret = ulist_add(refs, ref->parent, 0, GFP_NOFS);
715 BUG_ON(ret < 0);
716 }
717 kfree(ref);
718 }
719
720out:
721 if (head)
722 mutex_unlock(&head->mutex);
723 btrfs_free_path(path);
724 while (!list_empty(&prefs)) {
725 ref = list_first_entry(&prefs, struct __prelim_ref, list);
726 list_del(&ref->list);
727 kfree(ref);
728 }
729 while (!list_empty(&prefs_delayed)) {
730 ref = list_first_entry(&prefs_delayed, struct __prelim_ref,
731 list);
732 list_del(&ref->list);
733 kfree(ref);
734 }
735
736 return ret;
737}
738
739/*
740 * Finds all leafs with a reference to the specified combination of bytenr and
741 * offset. key_list_head will point to a list of corresponding keys (caller must
742 * free each list element). The leafs will be stored in the leafs ulist, which
743 * must be freed with ulist_free.
744 *
745 * returns 0 on success, <0 on error
746 */
747static int btrfs_find_all_leafs(struct btrfs_trans_handle *trans,
748 struct btrfs_fs_info *fs_info, u64 bytenr,
749 u64 num_bytes, u64 seq, struct ulist **leafs)
750{
751 struct ulist *tmp;
752 int ret;
753
754 tmp = ulist_alloc(GFP_NOFS);
755 if (!tmp)
756 return -ENOMEM;
757 *leafs = ulist_alloc(GFP_NOFS);
758 if (!*leafs) {
759 ulist_free(tmp);
760 return -ENOMEM;
761 }
762
763 ret = find_parent_nodes(trans, fs_info, bytenr, seq, *leafs, tmp);
764 ulist_free(tmp);
765
766 if (ret < 0 && ret != -ENOENT) {
767 ulist_free(*leafs);
768 return ret;
769 }
770
771 return 0;
772}
773
774/*
775 * walk all backrefs for a given extent to find all roots that reference this
776 * extent. Walking a backref means finding all extents that reference this
777 * extent and in turn walk the backrefs of those, too. Naturally this is a
778 * recursive process, but here it is implemented in an iterative fashion: We
779 * find all referencing extents for the extent in question and put them on a
780 * list. In turn, we find all referencing extents for those, further appending
781 * to the list. The way we iterate the list allows adding more elements after
782 * the current while iterating. The process stops when we reach the end of the
783 * list. Found roots are added to the roots list.
784 *
785 * returns 0 on success, < 0 on error.
786 */
787int btrfs_find_all_roots(struct btrfs_trans_handle *trans,
788 struct btrfs_fs_info *fs_info, u64 bytenr,
789 u64 num_bytes, u64 seq, struct ulist **roots)
790{
791 struct ulist *tmp;
792 struct ulist_node *node = NULL;
Jan Schmidtcd1b4132012-05-22 14:56:50 +0200793 struct ulist_iterator uiter;
Jan Schmidt8da6d582011-11-23 18:55:04 +0100794 int ret;
795
796 tmp = ulist_alloc(GFP_NOFS);
797 if (!tmp)
798 return -ENOMEM;
799 *roots = ulist_alloc(GFP_NOFS);
800 if (!*roots) {
801 ulist_free(tmp);
802 return -ENOMEM;
803 }
804
Jan Schmidtcd1b4132012-05-22 14:56:50 +0200805 ULIST_ITER_INIT(&uiter);
Jan Schmidt8da6d582011-11-23 18:55:04 +0100806 while (1) {
807 ret = find_parent_nodes(trans, fs_info, bytenr, seq,
808 tmp, *roots);
809 if (ret < 0 && ret != -ENOENT) {
810 ulist_free(tmp);
811 ulist_free(*roots);
812 return ret;
813 }
Jan Schmidtcd1b4132012-05-22 14:56:50 +0200814 node = ulist_next(tmp, &uiter);
Jan Schmidt8da6d582011-11-23 18:55:04 +0100815 if (!node)
816 break;
817 bytenr = node->val;
818 }
819
820 ulist_free(tmp);
821 return 0;
822}
823
824
Jan Schmidta542ad12011-06-13 19:52:59 +0200825static int __inode_info(u64 inum, u64 ioff, u8 key_type,
826 struct btrfs_root *fs_root, struct btrfs_path *path,
827 struct btrfs_key *found_key)
828{
829 int ret;
830 struct btrfs_key key;
831 struct extent_buffer *eb;
832
833 key.type = key_type;
834 key.objectid = inum;
835 key.offset = ioff;
836
837 ret = btrfs_search_slot(NULL, fs_root, &key, path, 0, 0);
838 if (ret < 0)
839 return ret;
840
841 eb = path->nodes[0];
842 if (ret && path->slots[0] >= btrfs_header_nritems(eb)) {
843 ret = btrfs_next_leaf(fs_root, path);
844 if (ret)
845 return ret;
846 eb = path->nodes[0];
847 }
848
849 btrfs_item_key_to_cpu(eb, found_key, path->slots[0]);
850 if (found_key->type != key.type || found_key->objectid != key.objectid)
851 return 1;
852
853 return 0;
854}
855
856/*
857 * this makes the path point to (inum INODE_ITEM ioff)
858 */
859int inode_item_info(u64 inum, u64 ioff, struct btrfs_root *fs_root,
860 struct btrfs_path *path)
861{
862 struct btrfs_key key;
863 return __inode_info(inum, ioff, BTRFS_INODE_ITEM_KEY, fs_root, path,
864 &key);
865}
866
867static int inode_ref_info(u64 inum, u64 ioff, struct btrfs_root *fs_root,
868 struct btrfs_path *path,
869 struct btrfs_key *found_key)
870{
871 return __inode_info(inum, ioff, BTRFS_INODE_REF_KEY, fs_root, path,
872 found_key);
873}
874
875/*
876 * this iterates to turn a btrfs_inode_ref into a full filesystem path. elements
877 * of the path are separated by '/' and the path is guaranteed to be
878 * 0-terminated. the path is only given within the current file system.
879 * Therefore, it never starts with a '/'. the caller is responsible to provide
880 * "size" bytes in "dest". the dest buffer will be filled backwards. finally,
881 * the start point of the resulting string is returned. this pointer is within
882 * dest, normally.
883 * in case the path buffer would overflow, the pointer is decremented further
884 * as if output was written to the buffer, though no more output is actually
885 * generated. that way, the caller can determine how much space would be
886 * required for the path to fit into the buffer. in that case, the returned
887 * value will be smaller than dest. callers must check this!
888 */
889static char *iref_to_path(struct btrfs_root *fs_root, struct btrfs_path *path,
890 struct btrfs_inode_ref *iref,
891 struct extent_buffer *eb_in, u64 parent,
892 char *dest, u32 size)
893{
894 u32 len;
895 int slot;
896 u64 next_inum;
897 int ret;
898 s64 bytes_left = size - 1;
899 struct extent_buffer *eb = eb_in;
900 struct btrfs_key found_key;
Jan Schmidtb916a592012-04-13 12:28:08 +0200901 int leave_spinning = path->leave_spinning;
Jan Schmidta542ad12011-06-13 19:52:59 +0200902
903 if (bytes_left >= 0)
904 dest[bytes_left] = '\0';
905
Jan Schmidtb916a592012-04-13 12:28:08 +0200906 path->leave_spinning = 1;
Jan Schmidta542ad12011-06-13 19:52:59 +0200907 while (1) {
908 len = btrfs_inode_ref_name_len(eb, iref);
909 bytes_left -= len;
910 if (bytes_left >= 0)
911 read_extent_buffer(eb, dest + bytes_left,
912 (unsigned long)(iref + 1), len);
Jan Schmidtb916a592012-04-13 12:28:08 +0200913 if (eb != eb_in) {
914 btrfs_tree_read_unlock_blocking(eb);
Jan Schmidta542ad12011-06-13 19:52:59 +0200915 free_extent_buffer(eb);
Jan Schmidtb916a592012-04-13 12:28:08 +0200916 }
Jan Schmidta542ad12011-06-13 19:52:59 +0200917 ret = inode_ref_info(parent, 0, fs_root, path, &found_key);
Jan Schmidt8f24b492012-02-08 16:01:01 +0100918 if (ret > 0)
919 ret = -ENOENT;
Jan Schmidta542ad12011-06-13 19:52:59 +0200920 if (ret)
921 break;
922 next_inum = found_key.offset;
923
924 /* regular exit ahead */
925 if (parent == next_inum)
926 break;
927
928 slot = path->slots[0];
929 eb = path->nodes[0];
930 /* make sure we can use eb after releasing the path */
Jan Schmidtb916a592012-04-13 12:28:08 +0200931 if (eb != eb_in) {
Jan Schmidta542ad12011-06-13 19:52:59 +0200932 atomic_inc(&eb->refs);
Jan Schmidtb916a592012-04-13 12:28:08 +0200933 btrfs_tree_read_lock(eb);
934 btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK);
935 }
Jan Schmidta542ad12011-06-13 19:52:59 +0200936 btrfs_release_path(path);
937
938 iref = btrfs_item_ptr(eb, slot, struct btrfs_inode_ref);
939 parent = next_inum;
940 --bytes_left;
941 if (bytes_left >= 0)
942 dest[bytes_left] = '/';
943 }
944
945 btrfs_release_path(path);
Jan Schmidtb916a592012-04-13 12:28:08 +0200946 path->leave_spinning = leave_spinning;
Jan Schmidta542ad12011-06-13 19:52:59 +0200947
948 if (ret)
949 return ERR_PTR(ret);
950
951 return dest + bytes_left;
952}
953
954/*
955 * this makes the path point to (logical EXTENT_ITEM *)
956 * returns BTRFS_EXTENT_FLAG_DATA for data, BTRFS_EXTENT_FLAG_TREE_BLOCK for
957 * tree blocks and <0 on error.
958 */
959int extent_from_logical(struct btrfs_fs_info *fs_info, u64 logical,
960 struct btrfs_path *path, struct btrfs_key *found_key)
961{
962 int ret;
963 u64 flags;
964 u32 item_size;
965 struct extent_buffer *eb;
966 struct btrfs_extent_item *ei;
967 struct btrfs_key key;
968
969 key.type = BTRFS_EXTENT_ITEM_KEY;
970 key.objectid = logical;
971 key.offset = (u64)-1;
972
973 ret = btrfs_search_slot(NULL, fs_info->extent_root, &key, path, 0, 0);
974 if (ret < 0)
975 return ret;
976 ret = btrfs_previous_item(fs_info->extent_root, path,
977 0, BTRFS_EXTENT_ITEM_KEY);
978 if (ret < 0)
979 return ret;
980
981 btrfs_item_key_to_cpu(path->nodes[0], found_key, path->slots[0]);
982 if (found_key->type != BTRFS_EXTENT_ITEM_KEY ||
983 found_key->objectid > logical ||
Jan Schmidt4692cf52011-12-02 14:56:41 +0100984 found_key->objectid + found_key->offset <= logical) {
985 pr_debug("logical %llu is not within any extent\n",
986 (unsigned long long)logical);
Jan Schmidta542ad12011-06-13 19:52:59 +0200987 return -ENOENT;
Jan Schmidt4692cf52011-12-02 14:56:41 +0100988 }
Jan Schmidta542ad12011-06-13 19:52:59 +0200989
990 eb = path->nodes[0];
991 item_size = btrfs_item_size_nr(eb, path->slots[0]);
992 BUG_ON(item_size < sizeof(*ei));
993
994 ei = btrfs_item_ptr(eb, path->slots[0], struct btrfs_extent_item);
995 flags = btrfs_extent_flags(eb, ei);
996
Jan Schmidt4692cf52011-12-02 14:56:41 +0100997 pr_debug("logical %llu is at position %llu within the extent (%llu "
998 "EXTENT_ITEM %llu) flags %#llx size %u\n",
999 (unsigned long long)logical,
1000 (unsigned long long)(logical - found_key->objectid),
1001 (unsigned long long)found_key->objectid,
1002 (unsigned long long)found_key->offset,
1003 (unsigned long long)flags, item_size);
Jan Schmidta542ad12011-06-13 19:52:59 +02001004 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)
1005 return BTRFS_EXTENT_FLAG_TREE_BLOCK;
1006 if (flags & BTRFS_EXTENT_FLAG_DATA)
1007 return BTRFS_EXTENT_FLAG_DATA;
1008
1009 return -EIO;
1010}
1011
1012/*
1013 * helper function to iterate extent inline refs. ptr must point to a 0 value
1014 * for the first call and may be modified. it is used to track state.
1015 * if more refs exist, 0 is returned and the next call to
1016 * __get_extent_inline_ref must pass the modified ptr parameter to get the
1017 * next ref. after the last ref was processed, 1 is returned.
1018 * returns <0 on error
1019 */
1020static int __get_extent_inline_ref(unsigned long *ptr, struct extent_buffer *eb,
1021 struct btrfs_extent_item *ei, u32 item_size,
1022 struct btrfs_extent_inline_ref **out_eiref,
1023 int *out_type)
1024{
1025 unsigned long end;
1026 u64 flags;
1027 struct btrfs_tree_block_info *info;
1028
1029 if (!*ptr) {
1030 /* first call */
1031 flags = btrfs_extent_flags(eb, ei);
1032 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
1033 info = (struct btrfs_tree_block_info *)(ei + 1);
1034 *out_eiref =
1035 (struct btrfs_extent_inline_ref *)(info + 1);
1036 } else {
1037 *out_eiref = (struct btrfs_extent_inline_ref *)(ei + 1);
1038 }
1039 *ptr = (unsigned long)*out_eiref;
1040 if ((void *)*ptr >= (void *)ei + item_size)
1041 return -ENOENT;
1042 }
1043
1044 end = (unsigned long)ei + item_size;
1045 *out_eiref = (struct btrfs_extent_inline_ref *)*ptr;
1046 *out_type = btrfs_extent_inline_ref_type(eb, *out_eiref);
1047
1048 *ptr += btrfs_extent_inline_ref_size(*out_type);
1049 WARN_ON(*ptr > end);
1050 if (*ptr == end)
1051 return 1; /* last */
1052
1053 return 0;
1054}
1055
1056/*
1057 * reads the tree block backref for an extent. tree level and root are returned
1058 * through out_level and out_root. ptr must point to a 0 value for the first
1059 * call and may be modified (see __get_extent_inline_ref comment).
1060 * returns 0 if data was provided, 1 if there was no more data to provide or
1061 * <0 on error.
1062 */
1063int tree_backref_for_extent(unsigned long *ptr, struct extent_buffer *eb,
1064 struct btrfs_extent_item *ei, u32 item_size,
1065 u64 *out_root, u8 *out_level)
1066{
1067 int ret;
1068 int type;
1069 struct btrfs_tree_block_info *info;
1070 struct btrfs_extent_inline_ref *eiref;
1071
1072 if (*ptr == (unsigned long)-1)
1073 return 1;
1074
1075 while (1) {
1076 ret = __get_extent_inline_ref(ptr, eb, ei, item_size,
1077 &eiref, &type);
1078 if (ret < 0)
1079 return ret;
1080
1081 if (type == BTRFS_TREE_BLOCK_REF_KEY ||
1082 type == BTRFS_SHARED_BLOCK_REF_KEY)
1083 break;
1084
1085 if (ret == 1)
1086 return 1;
1087 }
1088
1089 /* we can treat both ref types equally here */
1090 info = (struct btrfs_tree_block_info *)(ei + 1);
1091 *out_root = btrfs_extent_inline_ref_offset(eb, eiref);
1092 *out_level = btrfs_tree_block_level(eb, info);
1093
1094 if (ret == 1)
1095 *ptr = (unsigned long)-1;
1096
1097 return 0;
1098}
1099
Jan Schmidt7a3ae2f2012-03-23 17:32:28 +01001100static int iterate_leaf_refs(struct btrfs_fs_info *fs_info, u64 logical,
Jan Schmidt4692cf52011-12-02 14:56:41 +01001101 u64 orig_extent_item_objectid,
1102 u64 extent_item_pos, u64 root,
1103 iterate_extent_inodes_t *iterate, void *ctx)
Jan Schmidta542ad12011-06-13 19:52:59 +02001104{
1105 u64 disk_byte;
1106 struct btrfs_key key;
1107 struct btrfs_file_extent_item *fi;
1108 struct extent_buffer *eb;
1109 int slot;
1110 int nritems;
Jan Schmidt4692cf52011-12-02 14:56:41 +01001111 int ret = 0;
1112 int extent_type;
1113 u64 data_offset;
1114 u64 data_len;
Jan Schmidta542ad12011-06-13 19:52:59 +02001115
1116 eb = read_tree_block(fs_info->tree_root, logical,
1117 fs_info->tree_root->leafsize, 0);
1118 if (!eb)
1119 return -EIO;
1120
1121 /*
1122 * from the shared data ref, we only have the leaf but we need
1123 * the key. thus, we must look into all items and see that we
1124 * find one (some) with a reference to our extent item.
1125 */
1126 nritems = btrfs_header_nritems(eb);
1127 for (slot = 0; slot < nritems; ++slot) {
1128 btrfs_item_key_to_cpu(eb, &key, slot);
1129 if (key.type != BTRFS_EXTENT_DATA_KEY)
1130 continue;
1131 fi = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
Jan Schmidt4692cf52011-12-02 14:56:41 +01001132 extent_type = btrfs_file_extent_type(eb, fi);
1133 if (extent_type == BTRFS_FILE_EXTENT_INLINE)
1134 continue;
1135 /* don't skip BTRFS_FILE_EXTENT_PREALLOC, we can handle that */
Jan Schmidta542ad12011-06-13 19:52:59 +02001136 disk_byte = btrfs_file_extent_disk_bytenr(eb, fi);
Jan Schmidt4692cf52011-12-02 14:56:41 +01001137 if (disk_byte != orig_extent_item_objectid)
1138 continue;
Jan Schmidta542ad12011-06-13 19:52:59 +02001139
Jan Schmidt4692cf52011-12-02 14:56:41 +01001140 data_offset = btrfs_file_extent_offset(eb, fi);
1141 data_len = btrfs_file_extent_num_bytes(eb, fi);
1142
1143 if (extent_item_pos < data_offset ||
1144 extent_item_pos >= data_offset + data_len)
1145 continue;
1146
1147 pr_debug("ref for %llu resolved, key (%llu EXTEND_DATA %llu), "
1148 "root %llu\n", orig_extent_item_objectid,
1149 key.objectid, key.offset, root);
1150 ret = iterate(key.objectid,
1151 key.offset + (extent_item_pos - data_offset),
1152 root, ctx);
1153 if (ret) {
1154 pr_debug("stopping iteration because ret=%d\n", ret);
1155 break;
1156 }
Jan Schmidta542ad12011-06-13 19:52:59 +02001157 }
1158
1159 free_extent_buffer(eb);
Jan Schmidt4692cf52011-12-02 14:56:41 +01001160
Jan Schmidta542ad12011-06-13 19:52:59 +02001161 return ret;
1162}
1163
1164/*
1165 * calls iterate() for every inode that references the extent identified by
Jan Schmidt4692cf52011-12-02 14:56:41 +01001166 * the given parameters.
Jan Schmidta542ad12011-06-13 19:52:59 +02001167 * when the iterator function returns a non-zero value, iteration stops.
1168 */
1169int iterate_extent_inodes(struct btrfs_fs_info *fs_info,
Jan Schmidt4692cf52011-12-02 14:56:41 +01001170 u64 extent_item_objectid, u64 extent_item_pos,
Jan Schmidt7a3ae2f2012-03-23 17:32:28 +01001171 int search_commit_root,
Jan Schmidta542ad12011-06-13 19:52:59 +02001172 iterate_extent_inodes_t *iterate, void *ctx)
1173{
Jan Schmidta542ad12011-06-13 19:52:59 +02001174 int ret;
Jan Schmidta542ad12011-06-13 19:52:59 +02001175 struct list_head data_refs = LIST_HEAD_INIT(data_refs);
1176 struct list_head shared_refs = LIST_HEAD_INIT(shared_refs);
Jan Schmidt4692cf52011-12-02 14:56:41 +01001177 struct btrfs_trans_handle *trans;
Jan Schmidt7a3ae2f2012-03-23 17:32:28 +01001178 struct ulist *refs = NULL;
1179 struct ulist *roots = NULL;
Jan Schmidt4692cf52011-12-02 14:56:41 +01001180 struct ulist_node *ref_node = NULL;
1181 struct ulist_node *root_node = NULL;
1182 struct seq_list seq_elem;
Jan Schmidtcd1b4132012-05-22 14:56:50 +02001183 struct ulist_iterator ref_uiter;
1184 struct ulist_iterator root_uiter;
Jan Schmidt7a3ae2f2012-03-23 17:32:28 +01001185 struct btrfs_delayed_ref_root *delayed_refs = NULL;
Jan Schmidta542ad12011-06-13 19:52:59 +02001186
Jan Schmidt4692cf52011-12-02 14:56:41 +01001187 pr_debug("resolving all inodes for extent %llu\n",
1188 extent_item_objectid);
1189
Jan Schmidt7a3ae2f2012-03-23 17:32:28 +01001190 if (search_commit_root) {
1191 trans = BTRFS_BACKREF_SEARCH_COMMIT_ROOT;
1192 } else {
1193 trans = btrfs_join_transaction(fs_info->extent_root);
1194 if (IS_ERR(trans))
1195 return PTR_ERR(trans);
1196
1197 delayed_refs = &trans->transaction->delayed_refs;
1198 spin_lock(&delayed_refs->lock);
1199 btrfs_get_delayed_seq(delayed_refs, &seq_elem);
1200 spin_unlock(&delayed_refs->lock);
1201 }
Jan Schmidt4692cf52011-12-02 14:56:41 +01001202
1203 ret = btrfs_find_all_leafs(trans, fs_info, extent_item_objectid,
1204 extent_item_pos, seq_elem.seq,
1205 &refs);
1206
1207 if (ret)
1208 goto out;
1209
Jan Schmidtcd1b4132012-05-22 14:56:50 +02001210 ULIST_ITER_INIT(&ref_uiter);
1211 while (!ret && (ref_node = ulist_next(refs, &ref_uiter))) {
Jan Schmidt4692cf52011-12-02 14:56:41 +01001212 ret = btrfs_find_all_roots(trans, fs_info, ref_node->val, -1,
1213 seq_elem.seq, &roots);
1214 if (ret)
Jan Schmidta542ad12011-06-13 19:52:59 +02001215 break;
Jan Schmidtcd1b4132012-05-22 14:56:50 +02001216 ULIST_ITER_INIT(&root_uiter);
1217 while (!ret && (root_node = ulist_next(roots, &root_uiter))) {
Jan Schmidt4692cf52011-12-02 14:56:41 +01001218 pr_debug("root %llu references leaf %llu\n",
1219 root_node->val, ref_node->val);
Jan Schmidt7a3ae2f2012-03-23 17:32:28 +01001220 ret = iterate_leaf_refs(fs_info, ref_node->val,
Jan Schmidt4692cf52011-12-02 14:56:41 +01001221 extent_item_objectid,
1222 extent_item_pos, root_node->val,
1223 iterate, ctx);
Jan Schmidta542ad12011-06-13 19:52:59 +02001224 }
1225 }
1226
Jan Schmidt4692cf52011-12-02 14:56:41 +01001227 ulist_free(refs);
1228 ulist_free(roots);
1229out:
Jan Schmidt7a3ae2f2012-03-23 17:32:28 +01001230 if (!search_commit_root) {
1231 btrfs_put_delayed_seq(delayed_refs, &seq_elem);
1232 btrfs_end_transaction(trans, fs_info->extent_root);
1233 }
1234
Jan Schmidta542ad12011-06-13 19:52:59 +02001235 return ret;
1236}
1237
1238int iterate_inodes_from_logical(u64 logical, struct btrfs_fs_info *fs_info,
1239 struct btrfs_path *path,
1240 iterate_extent_inodes_t *iterate, void *ctx)
1241{
1242 int ret;
Jan Schmidt4692cf52011-12-02 14:56:41 +01001243 u64 extent_item_pos;
Jan Schmidta542ad12011-06-13 19:52:59 +02001244 struct btrfs_key found_key;
Jan Schmidt7a3ae2f2012-03-23 17:32:28 +01001245 int search_commit_root = path->search_commit_root;
Jan Schmidta542ad12011-06-13 19:52:59 +02001246
1247 ret = extent_from_logical(fs_info, logical, path,
1248 &found_key);
Jan Schmidt4692cf52011-12-02 14:56:41 +01001249 btrfs_release_path(path);
Jan Schmidta542ad12011-06-13 19:52:59 +02001250 if (ret & BTRFS_EXTENT_FLAG_TREE_BLOCK)
1251 ret = -EINVAL;
1252 if (ret < 0)
1253 return ret;
1254
Jan Schmidt4692cf52011-12-02 14:56:41 +01001255 extent_item_pos = logical - found_key.objectid;
Jan Schmidt7a3ae2f2012-03-23 17:32:28 +01001256 ret = iterate_extent_inodes(fs_info, found_key.objectid,
1257 extent_item_pos, search_commit_root,
1258 iterate, ctx);
Jan Schmidta542ad12011-06-13 19:52:59 +02001259
1260 return ret;
1261}
1262
1263static int iterate_irefs(u64 inum, struct btrfs_root *fs_root,
1264 struct btrfs_path *path,
1265 iterate_irefs_t *iterate, void *ctx)
1266{
Jan Schmidtaefc1eb2012-04-13 12:28:00 +02001267 int ret = 0;
Jan Schmidta542ad12011-06-13 19:52:59 +02001268 int slot;
1269 u32 cur;
1270 u32 len;
1271 u32 name_len;
1272 u64 parent = 0;
1273 int found = 0;
1274 struct extent_buffer *eb;
1275 struct btrfs_item *item;
1276 struct btrfs_inode_ref *iref;
1277 struct btrfs_key found_key;
1278
Jan Schmidtaefc1eb2012-04-13 12:28:00 +02001279 while (!ret) {
Jan Schmidtb916a592012-04-13 12:28:08 +02001280 path->leave_spinning = 1;
Jan Schmidta542ad12011-06-13 19:52:59 +02001281 ret = inode_ref_info(inum, parent ? parent+1 : 0, fs_root, path,
1282 &found_key);
1283 if (ret < 0)
1284 break;
1285 if (ret) {
1286 ret = found ? 0 : -ENOENT;
1287 break;
1288 }
1289 ++found;
1290
1291 parent = found_key.offset;
1292 slot = path->slots[0];
1293 eb = path->nodes[0];
1294 /* make sure we can use eb after releasing the path */
1295 atomic_inc(&eb->refs);
Jan Schmidtb916a592012-04-13 12:28:08 +02001296 btrfs_tree_read_lock(eb);
1297 btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK);
Jan Schmidta542ad12011-06-13 19:52:59 +02001298 btrfs_release_path(path);
1299
1300 item = btrfs_item_nr(eb, slot);
1301 iref = btrfs_item_ptr(eb, slot, struct btrfs_inode_ref);
1302
1303 for (cur = 0; cur < btrfs_item_size(eb, item); cur += len) {
1304 name_len = btrfs_inode_ref_name_len(eb, iref);
1305 /* path must be released before calling iterate()! */
Jan Schmidt4692cf52011-12-02 14:56:41 +01001306 pr_debug("following ref at offset %u for inode %llu in "
1307 "tree %llu\n", cur,
1308 (unsigned long long)found_key.objectid,
1309 (unsigned long long)fs_root->objectid);
Jan Schmidta542ad12011-06-13 19:52:59 +02001310 ret = iterate(parent, iref, eb, ctx);
Jan Schmidtaefc1eb2012-04-13 12:28:00 +02001311 if (ret)
Jan Schmidta542ad12011-06-13 19:52:59 +02001312 break;
Jan Schmidta542ad12011-06-13 19:52:59 +02001313 len = sizeof(*iref) + name_len;
1314 iref = (struct btrfs_inode_ref *)((char *)iref + len);
1315 }
Jan Schmidtb916a592012-04-13 12:28:08 +02001316 btrfs_tree_read_unlock_blocking(eb);
Jan Schmidta542ad12011-06-13 19:52:59 +02001317 free_extent_buffer(eb);
1318 }
1319
1320 btrfs_release_path(path);
1321
1322 return ret;
1323}
1324
1325/*
1326 * returns 0 if the path could be dumped (probably truncated)
1327 * returns <0 in case of an error
1328 */
1329static int inode_to_path(u64 inum, struct btrfs_inode_ref *iref,
1330 struct extent_buffer *eb, void *ctx)
1331{
1332 struct inode_fs_paths *ipath = ctx;
1333 char *fspath;
1334 char *fspath_min;
1335 int i = ipath->fspath->elem_cnt;
1336 const int s_ptr = sizeof(char *);
1337 u32 bytes_left;
1338
1339 bytes_left = ipath->fspath->bytes_left > s_ptr ?
1340 ipath->fspath->bytes_left - s_ptr : 0;
1341
Chris Mason740c3d22011-11-02 15:48:34 -04001342 fspath_min = (char *)ipath->fspath->val + (i + 1) * s_ptr;
Jan Schmidta542ad12011-06-13 19:52:59 +02001343 fspath = iref_to_path(ipath->fs_root, ipath->btrfs_path, iref, eb,
1344 inum, fspath_min, bytes_left);
1345 if (IS_ERR(fspath))
1346 return PTR_ERR(fspath);
1347
1348 if (fspath > fspath_min) {
Jan Schmidt4692cf52011-12-02 14:56:41 +01001349 pr_debug("path resolved: %s\n", fspath);
Jeff Mahoney745c4d82011-11-20 07:31:57 -05001350 ipath->fspath->val[i] = (u64)(unsigned long)fspath;
Jan Schmidta542ad12011-06-13 19:52:59 +02001351 ++ipath->fspath->elem_cnt;
1352 ipath->fspath->bytes_left = fspath - fspath_min;
1353 } else {
Jan Schmidt4692cf52011-12-02 14:56:41 +01001354 pr_debug("missed path, not enough space. missing bytes: %lu, "
1355 "constructed so far: %s\n",
1356 (unsigned long)(fspath_min - fspath), fspath_min);
Jan Schmidta542ad12011-06-13 19:52:59 +02001357 ++ipath->fspath->elem_missed;
1358 ipath->fspath->bytes_missing += fspath_min - fspath;
1359 ipath->fspath->bytes_left = 0;
1360 }
1361
1362 return 0;
1363}
1364
1365/*
1366 * this dumps all file system paths to the inode into the ipath struct, provided
1367 * is has been created large enough. each path is zero-terminated and accessed
Chris Mason740c3d22011-11-02 15:48:34 -04001368 * from ipath->fspath->val[i].
Jan Schmidta542ad12011-06-13 19:52:59 +02001369 * when it returns, there are ipath->fspath->elem_cnt number of paths available
Chris Mason740c3d22011-11-02 15:48:34 -04001370 * in ipath->fspath->val[]. when the allocated space wasn't sufficient, the
Jan Schmidta542ad12011-06-13 19:52:59 +02001371 * number of missed paths in recored in ipath->fspath->elem_missed, otherwise,
1372 * it's zero. ipath->fspath->bytes_missing holds the number of bytes that would
1373 * have been needed to return all paths.
1374 */
1375int paths_from_inode(u64 inum, struct inode_fs_paths *ipath)
1376{
1377 return iterate_irefs(inum, ipath->fs_root, ipath->btrfs_path,
1378 inode_to_path, ipath);
1379}
1380
Jan Schmidta542ad12011-06-13 19:52:59 +02001381struct btrfs_data_container *init_data_container(u32 total_bytes)
1382{
1383 struct btrfs_data_container *data;
1384 size_t alloc_bytes;
1385
1386 alloc_bytes = max_t(size_t, total_bytes, sizeof(*data));
1387 data = kmalloc(alloc_bytes, GFP_NOFS);
1388 if (!data)
1389 return ERR_PTR(-ENOMEM);
1390
1391 if (total_bytes >= sizeof(*data)) {
1392 data->bytes_left = total_bytes - sizeof(*data);
1393 data->bytes_missing = 0;
1394 } else {
1395 data->bytes_missing = sizeof(*data) - total_bytes;
1396 data->bytes_left = 0;
1397 }
1398
1399 data->elem_cnt = 0;
1400 data->elem_missed = 0;
1401
1402 return data;
1403}
1404
1405/*
1406 * allocates space to return multiple file system paths for an inode.
1407 * total_bytes to allocate are passed, note that space usable for actual path
1408 * information will be total_bytes - sizeof(struct inode_fs_paths).
1409 * the returned pointer must be freed with free_ipath() in the end.
1410 */
1411struct inode_fs_paths *init_ipath(s32 total_bytes, struct btrfs_root *fs_root,
1412 struct btrfs_path *path)
1413{
1414 struct inode_fs_paths *ifp;
1415 struct btrfs_data_container *fspath;
1416
1417 fspath = init_data_container(total_bytes);
1418 if (IS_ERR(fspath))
1419 return (void *)fspath;
1420
1421 ifp = kmalloc(sizeof(*ifp), GFP_NOFS);
1422 if (!ifp) {
1423 kfree(fspath);
1424 return ERR_PTR(-ENOMEM);
1425 }
1426
1427 ifp->btrfs_path = path;
1428 ifp->fspath = fspath;
1429 ifp->fs_root = fs_root;
1430
1431 return ifp;
1432}
1433
1434void free_ipath(struct inode_fs_paths *ipath)
1435{
Jesper Juhl4735fb22012-04-12 22:47:52 +02001436 if (!ipath)
1437 return;
Ilya Dryomov5eb56d22012-03-27 17:09:18 +03001438 kfree(ipath->fspath);
Jan Schmidta542ad12011-06-13 19:52:59 +02001439 kfree(ipath);
1440}