blob: fd13101aafa3f0bdce823e894eb1dcf1248d5eff [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 Schmidt976b1902012-05-17 16:43:03 +020027struct extent_inode_elem {
28 u64 inum;
29 u64 offset;
30 struct extent_inode_elem *next;
31};
32
33static int check_extent_in_eb(struct btrfs_key *key, struct extent_buffer *eb,
34 struct btrfs_file_extent_item *fi,
35 u64 extent_item_pos,
36 struct extent_inode_elem **eie)
37{
38 u64 data_offset;
39 u64 data_len;
40 struct extent_inode_elem *e;
41
42 data_offset = btrfs_file_extent_offset(eb, fi);
43 data_len = btrfs_file_extent_num_bytes(eb, fi);
44
45 if (extent_item_pos < data_offset ||
46 extent_item_pos >= data_offset + data_len)
47 return 1;
48
49 e = kmalloc(sizeof(*e), GFP_NOFS);
50 if (!e)
51 return -ENOMEM;
52
53 e->next = *eie;
54 e->inum = key->objectid;
55 e->offset = key->offset + (extent_item_pos - data_offset);
56 *eie = e;
57
58 return 0;
59}
60
61static int find_extent_in_eb(struct extent_buffer *eb, u64 wanted_disk_byte,
62 u64 extent_item_pos,
63 struct extent_inode_elem **eie)
64{
65 u64 disk_byte;
66 struct btrfs_key key;
67 struct btrfs_file_extent_item *fi;
68 int slot;
69 int nritems;
70 int extent_type;
71 int ret;
72
73 /*
74 * from the shared data ref, we only have the leaf but we need
75 * the key. thus, we must look into all items and see that we
76 * find one (some) with a reference to our extent item.
77 */
78 nritems = btrfs_header_nritems(eb);
79 for (slot = 0; slot < nritems; ++slot) {
80 btrfs_item_key_to_cpu(eb, &key, slot);
81 if (key.type != BTRFS_EXTENT_DATA_KEY)
82 continue;
83 fi = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
84 extent_type = btrfs_file_extent_type(eb, fi);
85 if (extent_type == BTRFS_FILE_EXTENT_INLINE)
86 continue;
87 /* don't skip BTRFS_FILE_EXTENT_PREALLOC, we can handle that */
88 disk_byte = btrfs_file_extent_disk_bytenr(eb, fi);
89 if (disk_byte != wanted_disk_byte)
90 continue;
91
92 ret = check_extent_in_eb(&key, eb, fi, extent_item_pos, eie);
93 if (ret < 0)
94 return ret;
95 }
96
97 return 0;
98}
99
Jan Schmidt8da6d582011-11-23 18:55:04 +0100100/*
101 * this structure records all encountered refs on the way up to the root
102 */
103struct __prelim_ref {
104 struct list_head list;
105 u64 root_id;
Jan Schmidtd5c88b72012-05-15 17:55:51 +0200106 struct btrfs_key key_for_search;
Jan Schmidt8da6d582011-11-23 18:55:04 +0100107 int level;
108 int count;
109 u64 parent;
110 u64 wanted_disk_byte;
111};
112
Jan Schmidtd5c88b72012-05-15 17:55:51 +0200113/*
114 * the rules for all callers of this function are:
115 * - obtaining the parent is the goal
116 * - if you add a key, you must know that it is a correct key
117 * - if you cannot add the parent or a correct key, then we will look into the
118 * block later to set a correct key
119 *
120 * delayed refs
121 * ============
122 * backref type | shared | indirect | shared | indirect
123 * information | tree | tree | data | data
124 * --------------------+--------+----------+--------+----------
125 * parent logical | y | - | - | -
126 * key to resolve | - | y | y | y
127 * tree block logical | - | - | - | -
128 * root for resolving | y | y | y | y
129 *
130 * - column 1: we've the parent -> done
131 * - column 2, 3, 4: we use the key to find the parent
132 *
133 * on disk refs (inline or keyed)
134 * ==============================
135 * backref type | shared | indirect | shared | indirect
136 * information | tree | tree | data | data
137 * --------------------+--------+----------+--------+----------
138 * parent logical | y | - | y | -
139 * key to resolve | - | - | - | y
140 * tree block logical | y | y | y | y
141 * root for resolving | - | y | y | y
142 *
143 * - column 1, 3: we've the parent -> done
144 * - column 2: we take the first key from the block to find the parent
145 * (see __add_missing_keys)
146 * - column 4: we use the key to find the parent
147 *
148 * additional information that's available but not required to find the parent
149 * block might help in merging entries to gain some speed.
150 */
151
Jan Schmidt8da6d582011-11-23 18:55:04 +0100152static int __add_prelim_ref(struct list_head *head, u64 root_id,
Jan Schmidtd5c88b72012-05-15 17:55:51 +0200153 struct btrfs_key *key, int level,
154 u64 parent, u64 wanted_disk_byte, int count)
Jan Schmidt8da6d582011-11-23 18:55:04 +0100155{
156 struct __prelim_ref *ref;
157
158 /* in case we're adding delayed refs, we're holding the refs spinlock */
159 ref = kmalloc(sizeof(*ref), GFP_ATOMIC);
160 if (!ref)
161 return -ENOMEM;
162
163 ref->root_id = root_id;
164 if (key)
Jan Schmidtd5c88b72012-05-15 17:55:51 +0200165 ref->key_for_search = *key;
Jan Schmidt8da6d582011-11-23 18:55:04 +0100166 else
Jan Schmidtd5c88b72012-05-15 17:55:51 +0200167 memset(&ref->key_for_search, 0, sizeof(ref->key_for_search));
Jan Schmidt8da6d582011-11-23 18:55:04 +0100168
169 ref->level = level;
170 ref->count = count;
171 ref->parent = parent;
172 ref->wanted_disk_byte = wanted_disk_byte;
173 list_add_tail(&ref->list, head);
174
175 return 0;
176}
177
178static int add_all_parents(struct btrfs_root *root, struct btrfs_path *path,
Jan Schmidt976b1902012-05-17 16:43:03 +0200179 struct ulist *parents, int level,
180 struct btrfs_key *key, u64 wanted_disk_byte,
181 const u64 *extent_item_pos)
Jan Schmidt8da6d582011-11-23 18:55:04 +0100182{
183 int ret;
184 int slot;
Jan Schmidt976b1902012-05-17 16:43:03 +0200185 struct extent_buffer *eb = path->nodes[level];
Jan Schmidt8da6d582011-11-23 18:55:04 +0100186 struct btrfs_file_extent_item *fi;
Jan Schmidt8da6d582011-11-23 18:55:04 +0100187 u64 disk_byte;
Jan Schmidt976b1902012-05-17 16:43:03 +0200188 u64 wanted_objectid = key->objectid;
Jan Schmidt8da6d582011-11-23 18:55:04 +0100189
190add_parent:
191 ret = ulist_add(parents, eb->start, 0, GFP_NOFS);
192 if (ret < 0)
193 return ret;
194
195 if (level != 0)
196 return 0;
197
198 /*
199 * if the current leaf is full with EXTENT_DATA items, we must
200 * check the next one if that holds a reference as well.
201 * ref->count cannot be used to skip this check.
202 * repeat this until we don't find any additional EXTENT_DATA items.
203 */
204 while (1) {
205 ret = btrfs_next_leaf(root, path);
206 if (ret < 0)
207 return ret;
208 if (ret)
209 return 0;
210
211 eb = path->nodes[0];
212 for (slot = 0; slot < btrfs_header_nritems(eb); ++slot) {
Jan Schmidt976b1902012-05-17 16:43:03 +0200213 btrfs_item_key_to_cpu(eb, key, slot);
214 if (key->objectid != wanted_objectid ||
215 key->type != BTRFS_EXTENT_DATA_KEY)
Jan Schmidt8da6d582011-11-23 18:55:04 +0100216 return 0;
217 fi = btrfs_item_ptr(eb, slot,
218 struct btrfs_file_extent_item);
219 disk_byte = btrfs_file_extent_disk_bytenr(eb, fi);
220 if (disk_byte == wanted_disk_byte)
221 goto add_parent;
222 }
223 }
224
225 return 0;
226}
227
228/*
229 * resolve an indirect backref in the form (root_id, key, level)
230 * to a logical address
231 */
232static int __resolve_indirect_ref(struct btrfs_fs_info *fs_info,
Jan Schmidt7a3ae2f2012-03-23 17:32:28 +0100233 int search_commit_root,
Jan Schmidt8da6d582011-11-23 18:55:04 +0100234 struct __prelim_ref *ref,
Jan Schmidt976b1902012-05-17 16:43:03 +0200235 struct ulist *parents,
236 const u64 *extent_item_pos)
Jan Schmidt8da6d582011-11-23 18:55:04 +0100237{
238 struct btrfs_path *path;
239 struct btrfs_root *root;
240 struct btrfs_key root_key;
241 struct btrfs_key key = {0};
242 struct extent_buffer *eb;
243 int ret = 0;
244 int root_level;
245 int level = ref->level;
246
247 path = btrfs_alloc_path();
248 if (!path)
249 return -ENOMEM;
Jan Schmidt7a3ae2f2012-03-23 17:32:28 +0100250 path->search_commit_root = !!search_commit_root;
Jan Schmidt8da6d582011-11-23 18:55:04 +0100251
252 root_key.objectid = ref->root_id;
253 root_key.type = BTRFS_ROOT_ITEM_KEY;
254 root_key.offset = (u64)-1;
255 root = btrfs_read_fs_root_no_name(fs_info, &root_key);
256 if (IS_ERR(root)) {
257 ret = PTR_ERR(root);
258 goto out;
259 }
260
261 rcu_read_lock();
262 root_level = btrfs_header_level(root->node);
263 rcu_read_unlock();
264
265 if (root_level + 1 == level)
266 goto out;
267
268 path->lowest_level = level;
Jan Schmidtd5c88b72012-05-15 17:55:51 +0200269 ret = btrfs_search_slot(NULL, root, &ref->key_for_search, path, 0, 0);
Jan Schmidt8da6d582011-11-23 18:55:04 +0100270 pr_debug("search slot in root %llu (level %d, ref count %d) returned "
271 "%d for key (%llu %u %llu)\n",
272 (unsigned long long)ref->root_id, level, ref->count, ret,
Jan Schmidtd5c88b72012-05-15 17:55:51 +0200273 (unsigned long long)ref->key_for_search.objectid,
274 ref->key_for_search.type,
275 (unsigned long long)ref->key_for_search.offset);
Jan Schmidt8da6d582011-11-23 18:55:04 +0100276 if (ret < 0)
277 goto out;
278
279 eb = path->nodes[level];
280 if (!eb) {
281 WARN_ON(1);
282 ret = 1;
283 goto out;
284 }
285
286 if (level == 0) {
287 if (ret == 1 && path->slots[0] >= btrfs_header_nritems(eb)) {
288 ret = btrfs_next_leaf(root, path);
289 if (ret)
290 goto out;
291 eb = path->nodes[0];
292 }
293
294 btrfs_item_key_to_cpu(eb, &key, path->slots[0]);
295 }
296
Jan Schmidt976b1902012-05-17 16:43:03 +0200297 ret = add_all_parents(root, path, parents, level, &key,
298 ref->wanted_disk_byte, extent_item_pos);
Jan Schmidt8da6d582011-11-23 18:55:04 +0100299out:
300 btrfs_free_path(path);
301 return ret;
302}
303
304/*
305 * resolve all indirect backrefs from the list
306 */
307static int __resolve_indirect_refs(struct btrfs_fs_info *fs_info,
Jan Schmidt7a3ae2f2012-03-23 17:32:28 +0100308 int search_commit_root,
Jan Schmidt976b1902012-05-17 16:43:03 +0200309 struct list_head *head,
310 const u64 *extent_item_pos)
Jan Schmidt8da6d582011-11-23 18:55:04 +0100311{
312 int err;
313 int ret = 0;
314 struct __prelim_ref *ref;
315 struct __prelim_ref *ref_safe;
316 struct __prelim_ref *new_ref;
317 struct ulist *parents;
318 struct ulist_node *node;
Jan Schmidtcd1b4132012-05-22 14:56:50 +0200319 struct ulist_iterator uiter;
Jan Schmidt8da6d582011-11-23 18:55:04 +0100320
321 parents = ulist_alloc(GFP_NOFS);
322 if (!parents)
323 return -ENOMEM;
324
325 /*
326 * _safe allows us to insert directly after the current item without
327 * iterating over the newly inserted items.
328 * we're also allowed to re-assign ref during iteration.
329 */
330 list_for_each_entry_safe(ref, ref_safe, head, list) {
331 if (ref->parent) /* already direct */
332 continue;
333 if (ref->count == 0)
334 continue;
Jan Schmidt7a3ae2f2012-03-23 17:32:28 +0100335 err = __resolve_indirect_ref(fs_info, search_commit_root,
Jan Schmidt976b1902012-05-17 16:43:03 +0200336 ref, parents, extent_item_pos);
Jan Schmidt8da6d582011-11-23 18:55:04 +0100337 if (err) {
338 if (ret == 0)
339 ret = err;
340 continue;
341 }
342
343 /* we put the first parent into the ref at hand */
Jan Schmidtcd1b4132012-05-22 14:56:50 +0200344 ULIST_ITER_INIT(&uiter);
345 node = ulist_next(parents, &uiter);
Jan Schmidt8da6d582011-11-23 18:55:04 +0100346 ref->parent = node ? node->val : 0;
347
348 /* additional parents require new refs being added here */
Jan Schmidtcd1b4132012-05-22 14:56:50 +0200349 while ((node = ulist_next(parents, &uiter))) {
Jan Schmidt8da6d582011-11-23 18:55:04 +0100350 new_ref = kmalloc(sizeof(*new_ref), GFP_NOFS);
351 if (!new_ref) {
352 ret = -ENOMEM;
353 break;
354 }
355 memcpy(new_ref, ref, sizeof(*ref));
356 new_ref->parent = node->val;
357 list_add(&new_ref->list, &ref->list);
358 }
359 ulist_reinit(parents);
360 }
361
362 ulist_free(parents);
363 return ret;
364}
365
Jan Schmidtd5c88b72012-05-15 17:55:51 +0200366static inline int ref_for_same_block(struct __prelim_ref *ref1,
367 struct __prelim_ref *ref2)
368{
369 if (ref1->level != ref2->level)
370 return 0;
371 if (ref1->root_id != ref2->root_id)
372 return 0;
373 if (ref1->key_for_search.type != ref2->key_for_search.type)
374 return 0;
375 if (ref1->key_for_search.objectid != ref2->key_for_search.objectid)
376 return 0;
377 if (ref1->key_for_search.offset != ref2->key_for_search.offset)
378 return 0;
379 if (ref1->parent != ref2->parent)
380 return 0;
381
382 return 1;
383}
384
385/*
386 * read tree blocks and add keys where required.
387 */
388static int __add_missing_keys(struct btrfs_fs_info *fs_info,
389 struct list_head *head)
390{
391 struct list_head *pos;
392 struct extent_buffer *eb;
393
394 list_for_each(pos, head) {
395 struct __prelim_ref *ref;
396 ref = list_entry(pos, struct __prelim_ref, list);
397
398 if (ref->parent)
399 continue;
400 if (ref->key_for_search.type)
401 continue;
402 BUG_ON(!ref->wanted_disk_byte);
403 eb = read_tree_block(fs_info->tree_root, ref->wanted_disk_byte,
404 fs_info->tree_root->leafsize, 0);
405 BUG_ON(!eb);
406 btrfs_tree_read_lock(eb);
407 if (btrfs_header_level(eb) == 0)
408 btrfs_item_key_to_cpu(eb, &ref->key_for_search, 0);
409 else
410 btrfs_node_key_to_cpu(eb, &ref->key_for_search, 0);
411 btrfs_tree_read_unlock(eb);
412 free_extent_buffer(eb);
413 }
414 return 0;
415}
416
Jan Schmidt8da6d582011-11-23 18:55:04 +0100417/*
418 * merge two lists of backrefs and adjust counts accordingly
419 *
420 * mode = 1: merge identical keys, if key is set
Jan Schmidtd5c88b72012-05-15 17:55:51 +0200421 * FIXME: if we add more keys in __add_prelim_ref, we can merge more here.
422 * additionally, we could even add a key range for the blocks we
423 * looked into to merge even more (-> replace unresolved refs by those
424 * having a parent).
Jan Schmidt8da6d582011-11-23 18:55:04 +0100425 * mode = 2: merge identical parents
426 */
427static int __merge_refs(struct list_head *head, int mode)
428{
429 struct list_head *pos1;
430
431 list_for_each(pos1, head) {
432 struct list_head *n2;
433 struct list_head *pos2;
434 struct __prelim_ref *ref1;
435
436 ref1 = list_entry(pos1, struct __prelim_ref, list);
437
Jan Schmidt8da6d582011-11-23 18:55:04 +0100438 for (pos2 = pos1->next, n2 = pos2->next; pos2 != head;
439 pos2 = n2, n2 = pos2->next) {
440 struct __prelim_ref *ref2;
Jan Schmidtd5c88b72012-05-15 17:55:51 +0200441 struct __prelim_ref *xchg;
Jan Schmidt8da6d582011-11-23 18:55:04 +0100442
443 ref2 = list_entry(pos2, struct __prelim_ref, list);
444
445 if (mode == 1) {
Jan Schmidtd5c88b72012-05-15 17:55:51 +0200446 if (!ref_for_same_block(ref1, ref2))
Jan Schmidt8da6d582011-11-23 18:55:04 +0100447 continue;
Jan Schmidtd5c88b72012-05-15 17:55:51 +0200448 if (!ref1->parent && ref2->parent) {
449 xchg = ref1;
450 ref1 = ref2;
451 ref2 = xchg;
452 }
Jan Schmidt8da6d582011-11-23 18:55:04 +0100453 ref1->count += ref2->count;
454 } else {
455 if (ref1->parent != ref2->parent)
456 continue;
457 ref1->count += ref2->count;
458 }
459 list_del(&ref2->list);
460 kfree(ref2);
461 }
462
463 }
464 return 0;
465}
466
467/*
468 * add all currently queued delayed refs from this head whose seq nr is
469 * smaller or equal that seq to the list
470 */
471static int __add_delayed_refs(struct btrfs_delayed_ref_head *head, u64 seq,
Jan Schmidt8da6d582011-11-23 18:55:04 +0100472 struct list_head *prefs)
473{
474 struct btrfs_delayed_extent_op *extent_op = head->extent_op;
475 struct rb_node *n = &head->node.rb_node;
Jan Schmidtd5c88b72012-05-15 17:55:51 +0200476 struct btrfs_key key;
477 struct btrfs_key op_key = {0};
Jan Schmidt8da6d582011-11-23 18:55:04 +0100478 int sgn;
Jan Schmidtb1375d62012-01-26 15:01:11 -0500479 int ret = 0;
Jan Schmidt8da6d582011-11-23 18:55:04 +0100480
481 if (extent_op && extent_op->update_key)
Jan Schmidtd5c88b72012-05-15 17:55:51 +0200482 btrfs_disk_key_to_cpu(&op_key, &extent_op->key);
Jan Schmidt8da6d582011-11-23 18:55:04 +0100483
484 while ((n = rb_prev(n))) {
485 struct btrfs_delayed_ref_node *node;
486 node = rb_entry(n, struct btrfs_delayed_ref_node,
487 rb_node);
488 if (node->bytenr != head->node.bytenr)
489 break;
490 WARN_ON(node->is_head);
491
492 if (node->seq > seq)
493 continue;
494
495 switch (node->action) {
496 case BTRFS_ADD_DELAYED_EXTENT:
497 case BTRFS_UPDATE_DELAYED_HEAD:
498 WARN_ON(1);
499 continue;
500 case BTRFS_ADD_DELAYED_REF:
501 sgn = 1;
502 break;
503 case BTRFS_DROP_DELAYED_REF:
504 sgn = -1;
505 break;
506 default:
507 BUG_ON(1);
508 }
509 switch (node->type) {
510 case BTRFS_TREE_BLOCK_REF_KEY: {
511 struct btrfs_delayed_tree_ref *ref;
512
513 ref = btrfs_delayed_node_to_tree_ref(node);
Jan Schmidtd5c88b72012-05-15 17:55:51 +0200514 ret = __add_prelim_ref(prefs, ref->root, &op_key,
Jan Schmidt8da6d582011-11-23 18:55:04 +0100515 ref->level + 1, 0, node->bytenr,
516 node->ref_mod * sgn);
517 break;
518 }
519 case BTRFS_SHARED_BLOCK_REF_KEY: {
520 struct btrfs_delayed_tree_ref *ref;
521
522 ref = btrfs_delayed_node_to_tree_ref(node);
Jan Schmidtd5c88b72012-05-15 17:55:51 +0200523 ret = __add_prelim_ref(prefs, ref->root, NULL,
Jan Schmidt8da6d582011-11-23 18:55:04 +0100524 ref->level + 1, ref->parent,
525 node->bytenr,
526 node->ref_mod * sgn);
527 break;
528 }
529 case BTRFS_EXTENT_DATA_REF_KEY: {
530 struct btrfs_delayed_data_ref *ref;
Jan Schmidt8da6d582011-11-23 18:55:04 +0100531 ref = btrfs_delayed_node_to_data_ref(node);
532
533 key.objectid = ref->objectid;
534 key.type = BTRFS_EXTENT_DATA_KEY;
535 key.offset = ref->offset;
536 ret = __add_prelim_ref(prefs, ref->root, &key, 0, 0,
537 node->bytenr,
538 node->ref_mod * sgn);
539 break;
540 }
541 case BTRFS_SHARED_DATA_REF_KEY: {
542 struct btrfs_delayed_data_ref *ref;
Jan Schmidt8da6d582011-11-23 18:55:04 +0100543
544 ref = btrfs_delayed_node_to_data_ref(node);
545
546 key.objectid = ref->objectid;
547 key.type = BTRFS_EXTENT_DATA_KEY;
548 key.offset = ref->offset;
549 ret = __add_prelim_ref(prefs, ref->root, &key, 0,
550 ref->parent, node->bytenr,
551 node->ref_mod * sgn);
552 break;
553 }
554 default:
555 WARN_ON(1);
556 }
557 BUG_ON(ret);
558 }
559
560 return 0;
561}
562
563/*
564 * add all inline backrefs for bytenr to the list
565 */
566static int __add_inline_refs(struct btrfs_fs_info *fs_info,
567 struct btrfs_path *path, u64 bytenr,
Jan Schmidtd5c88b72012-05-15 17:55:51 +0200568 int *info_level, struct list_head *prefs)
Jan Schmidt8da6d582011-11-23 18:55:04 +0100569{
Jan Schmidtb1375d62012-01-26 15:01:11 -0500570 int ret = 0;
Jan Schmidt8da6d582011-11-23 18:55:04 +0100571 int slot;
572 struct extent_buffer *leaf;
573 struct btrfs_key key;
574 unsigned long ptr;
575 unsigned long end;
576 struct btrfs_extent_item *ei;
577 u64 flags;
578 u64 item_size;
579
580 /*
581 * enumerate all inline refs
582 */
583 leaf = path->nodes[0];
Jan Schmidtdadcaf72012-05-22 13:43:25 +0200584 slot = path->slots[0];
Jan Schmidt8da6d582011-11-23 18:55:04 +0100585
586 item_size = btrfs_item_size_nr(leaf, slot);
587 BUG_ON(item_size < sizeof(*ei));
588
589 ei = btrfs_item_ptr(leaf, slot, struct btrfs_extent_item);
590 flags = btrfs_extent_flags(leaf, ei);
591
592 ptr = (unsigned long)(ei + 1);
593 end = (unsigned long)ei + item_size;
594
595 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
596 struct btrfs_tree_block_info *info;
Jan Schmidt8da6d582011-11-23 18:55:04 +0100597
598 info = (struct btrfs_tree_block_info *)ptr;
599 *info_level = btrfs_tree_block_level(leaf, info);
Jan Schmidt8da6d582011-11-23 18:55:04 +0100600 ptr += sizeof(struct btrfs_tree_block_info);
601 BUG_ON(ptr > end);
602 } else {
603 BUG_ON(!(flags & BTRFS_EXTENT_FLAG_DATA));
604 }
605
606 while (ptr < end) {
607 struct btrfs_extent_inline_ref *iref;
608 u64 offset;
609 int type;
610
611 iref = (struct btrfs_extent_inline_ref *)ptr;
612 type = btrfs_extent_inline_ref_type(leaf, iref);
613 offset = btrfs_extent_inline_ref_offset(leaf, iref);
614
615 switch (type) {
616 case BTRFS_SHARED_BLOCK_REF_KEY:
Jan Schmidtd5c88b72012-05-15 17:55:51 +0200617 ret = __add_prelim_ref(prefs, 0, NULL,
Jan Schmidt8da6d582011-11-23 18:55:04 +0100618 *info_level + 1, offset,
619 bytenr, 1);
620 break;
621 case BTRFS_SHARED_DATA_REF_KEY: {
622 struct btrfs_shared_data_ref *sdref;
623 int count;
624
625 sdref = (struct btrfs_shared_data_ref *)(iref + 1);
626 count = btrfs_shared_data_ref_count(leaf, sdref);
627 ret = __add_prelim_ref(prefs, 0, NULL, 0, offset,
628 bytenr, count);
629 break;
630 }
631 case BTRFS_TREE_BLOCK_REF_KEY:
Jan Schmidtd5c88b72012-05-15 17:55:51 +0200632 ret = __add_prelim_ref(prefs, offset, NULL,
633 *info_level + 1, 0,
634 bytenr, 1);
Jan Schmidt8da6d582011-11-23 18:55:04 +0100635 break;
636 case BTRFS_EXTENT_DATA_REF_KEY: {
637 struct btrfs_extent_data_ref *dref;
638 int count;
639 u64 root;
640
641 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
642 count = btrfs_extent_data_ref_count(leaf, dref);
643 key.objectid = btrfs_extent_data_ref_objectid(leaf,
644 dref);
645 key.type = BTRFS_EXTENT_DATA_KEY;
646 key.offset = btrfs_extent_data_ref_offset(leaf, dref);
647 root = btrfs_extent_data_ref_root(leaf, dref);
Jan Schmidtd5c88b72012-05-15 17:55:51 +0200648 ret = __add_prelim_ref(prefs, root, &key, 0, 0,
649 bytenr, count);
Jan Schmidt8da6d582011-11-23 18:55:04 +0100650 break;
651 }
652 default:
653 WARN_ON(1);
654 }
655 BUG_ON(ret);
656 ptr += btrfs_extent_inline_ref_size(type);
657 }
658
659 return 0;
660}
661
662/*
663 * add all non-inline backrefs for bytenr to the list
664 */
665static int __add_keyed_refs(struct btrfs_fs_info *fs_info,
666 struct btrfs_path *path, u64 bytenr,
Jan Schmidtd5c88b72012-05-15 17:55:51 +0200667 int info_level, struct list_head *prefs)
Jan Schmidt8da6d582011-11-23 18:55:04 +0100668{
669 struct btrfs_root *extent_root = fs_info->extent_root;
670 int ret;
671 int slot;
672 struct extent_buffer *leaf;
673 struct btrfs_key key;
674
675 while (1) {
676 ret = btrfs_next_item(extent_root, path);
677 if (ret < 0)
678 break;
679 if (ret) {
680 ret = 0;
681 break;
682 }
683
684 slot = path->slots[0];
685 leaf = path->nodes[0];
686 btrfs_item_key_to_cpu(leaf, &key, slot);
687
688 if (key.objectid != bytenr)
689 break;
690 if (key.type < BTRFS_TREE_BLOCK_REF_KEY)
691 continue;
692 if (key.type > BTRFS_SHARED_DATA_REF_KEY)
693 break;
694
695 switch (key.type) {
696 case BTRFS_SHARED_BLOCK_REF_KEY:
Jan Schmidtd5c88b72012-05-15 17:55:51 +0200697 ret = __add_prelim_ref(prefs, 0, NULL,
Jan Schmidt8da6d582011-11-23 18:55:04 +0100698 info_level + 1, key.offset,
699 bytenr, 1);
700 break;
701 case BTRFS_SHARED_DATA_REF_KEY: {
702 struct btrfs_shared_data_ref *sdref;
703 int count;
704
705 sdref = btrfs_item_ptr(leaf, slot,
706 struct btrfs_shared_data_ref);
707 count = btrfs_shared_data_ref_count(leaf, sdref);
708 ret = __add_prelim_ref(prefs, 0, NULL, 0, key.offset,
709 bytenr, count);
710 break;
711 }
712 case BTRFS_TREE_BLOCK_REF_KEY:
Jan Schmidtd5c88b72012-05-15 17:55:51 +0200713 ret = __add_prelim_ref(prefs, key.offset, NULL,
714 info_level + 1, 0,
715 bytenr, 1);
Jan Schmidt8da6d582011-11-23 18:55:04 +0100716 break;
717 case BTRFS_EXTENT_DATA_REF_KEY: {
718 struct btrfs_extent_data_ref *dref;
719 int count;
720 u64 root;
721
722 dref = btrfs_item_ptr(leaf, slot,
723 struct btrfs_extent_data_ref);
724 count = btrfs_extent_data_ref_count(leaf, dref);
725 key.objectid = btrfs_extent_data_ref_objectid(leaf,
726 dref);
727 key.type = BTRFS_EXTENT_DATA_KEY;
728 key.offset = btrfs_extent_data_ref_offset(leaf, dref);
729 root = btrfs_extent_data_ref_root(leaf, dref);
730 ret = __add_prelim_ref(prefs, root, &key, 0, 0,
Jan Schmidtd5c88b72012-05-15 17:55:51 +0200731 bytenr, count);
Jan Schmidt8da6d582011-11-23 18:55:04 +0100732 break;
733 }
734 default:
735 WARN_ON(1);
736 }
737 BUG_ON(ret);
738 }
739
740 return ret;
741}
742
743/*
744 * this adds all existing backrefs (inline backrefs, backrefs and delayed
745 * refs) for the given bytenr to the refs list, merges duplicates and resolves
746 * indirect refs to their parent bytenr.
747 * When roots are found, they're added to the roots list
748 *
749 * FIXME some caching might speed things up
750 */
751static int find_parent_nodes(struct btrfs_trans_handle *trans,
752 struct btrfs_fs_info *fs_info, u64 bytenr,
Jan Schmidt976b1902012-05-17 16:43:03 +0200753 u64 seq, struct ulist *refs, struct ulist *roots,
754 const u64 *extent_item_pos)
Jan Schmidt8da6d582011-11-23 18:55:04 +0100755{
756 struct btrfs_key key;
757 struct btrfs_path *path;
Jan Schmidt8da6d582011-11-23 18:55:04 +0100758 struct btrfs_delayed_ref_root *delayed_refs = NULL;
Li Zefand3b01062012-03-03 07:41:15 -0500759 struct btrfs_delayed_ref_head *head;
Jan Schmidt8da6d582011-11-23 18:55:04 +0100760 int info_level = 0;
761 int ret;
Jan Schmidt7a3ae2f2012-03-23 17:32:28 +0100762 int search_commit_root = (trans == BTRFS_BACKREF_SEARCH_COMMIT_ROOT);
Jan Schmidt8da6d582011-11-23 18:55:04 +0100763 struct list_head prefs_delayed;
764 struct list_head prefs;
765 struct __prelim_ref *ref;
766
767 INIT_LIST_HEAD(&prefs);
768 INIT_LIST_HEAD(&prefs_delayed);
769
770 key.objectid = bytenr;
771 key.type = BTRFS_EXTENT_ITEM_KEY;
772 key.offset = (u64)-1;
773
774 path = btrfs_alloc_path();
775 if (!path)
776 return -ENOMEM;
Jan Schmidt7a3ae2f2012-03-23 17:32:28 +0100777 path->search_commit_root = !!search_commit_root;
Jan Schmidt8da6d582011-11-23 18:55:04 +0100778
779 /*
780 * grab both a lock on the path and a lock on the delayed ref head.
781 * We need both to get a consistent picture of how the refs look
782 * at a specified point in time
783 */
784again:
Li Zefand3b01062012-03-03 07:41:15 -0500785 head = NULL;
786
Jan Schmidt8da6d582011-11-23 18:55:04 +0100787 ret = btrfs_search_slot(trans, fs_info->extent_root, &key, path, 0, 0);
788 if (ret < 0)
789 goto out;
790 BUG_ON(ret == 0);
791
Jan Schmidt7a3ae2f2012-03-23 17:32:28 +0100792 if (trans != BTRFS_BACKREF_SEARCH_COMMIT_ROOT) {
793 /*
794 * look if there are updates for this ref queued and lock the
795 * head
796 */
797 delayed_refs = &trans->transaction->delayed_refs;
798 spin_lock(&delayed_refs->lock);
799 head = btrfs_find_delayed_ref_head(trans, bytenr);
800 if (head) {
801 if (!mutex_trylock(&head->mutex)) {
802 atomic_inc(&head->node.refs);
803 spin_unlock(&delayed_refs->lock);
Jan Schmidt8da6d582011-11-23 18:55:04 +0100804
Jan Schmidt7a3ae2f2012-03-23 17:32:28 +0100805 btrfs_release_path(path);
Jan Schmidt8da6d582011-11-23 18:55:04 +0100806
Jan Schmidt7a3ae2f2012-03-23 17:32:28 +0100807 /*
808 * Mutex was contended, block until it's
809 * released and try again
810 */
811 mutex_lock(&head->mutex);
812 mutex_unlock(&head->mutex);
813 btrfs_put_delayed_ref(&head->node);
814 goto again;
815 }
Jan Schmidtd5c88b72012-05-15 17:55:51 +0200816 ret = __add_delayed_refs(head, seq, &prefs_delayed);
Jan Schmidt7a3ae2f2012-03-23 17:32:28 +0100817 if (ret) {
818 spin_unlock(&delayed_refs->lock);
819 goto out;
820 }
Jan Schmidt8da6d582011-11-23 18:55:04 +0100821 }
Jan Schmidt7a3ae2f2012-03-23 17:32:28 +0100822 spin_unlock(&delayed_refs->lock);
Jan Schmidt8da6d582011-11-23 18:55:04 +0100823 }
Jan Schmidt8da6d582011-11-23 18:55:04 +0100824
825 if (path->slots[0]) {
826 struct extent_buffer *leaf;
827 int slot;
828
Jan Schmidtdadcaf72012-05-22 13:43:25 +0200829 path->slots[0]--;
Jan Schmidt8da6d582011-11-23 18:55:04 +0100830 leaf = path->nodes[0];
Jan Schmidtdadcaf72012-05-22 13:43:25 +0200831 slot = path->slots[0];
Jan Schmidt8da6d582011-11-23 18:55:04 +0100832 btrfs_item_key_to_cpu(leaf, &key, slot);
833 if (key.objectid == bytenr &&
834 key.type == BTRFS_EXTENT_ITEM_KEY) {
835 ret = __add_inline_refs(fs_info, path, bytenr,
Jan Schmidtd5c88b72012-05-15 17:55:51 +0200836 &info_level, &prefs);
Jan Schmidt8da6d582011-11-23 18:55:04 +0100837 if (ret)
838 goto out;
Jan Schmidtd5c88b72012-05-15 17:55:51 +0200839 ret = __add_keyed_refs(fs_info, path, bytenr,
Jan Schmidt8da6d582011-11-23 18:55:04 +0100840 info_level, &prefs);
841 if (ret)
842 goto out;
843 }
844 }
845 btrfs_release_path(path);
846
Jan Schmidt8da6d582011-11-23 18:55:04 +0100847 list_splice_init(&prefs_delayed, &prefs);
848
Jan Schmidtd5c88b72012-05-15 17:55:51 +0200849 ret = __add_missing_keys(fs_info, &prefs);
850 if (ret)
851 goto out;
852
Jan Schmidt8da6d582011-11-23 18:55:04 +0100853 ret = __merge_refs(&prefs, 1);
854 if (ret)
855 goto out;
856
Jan Schmidt976b1902012-05-17 16:43:03 +0200857 ret = __resolve_indirect_refs(fs_info, search_commit_root, &prefs,
858 extent_item_pos);
Jan Schmidt8da6d582011-11-23 18:55:04 +0100859 if (ret)
860 goto out;
861
862 ret = __merge_refs(&prefs, 2);
863 if (ret)
864 goto out;
865
866 while (!list_empty(&prefs)) {
867 ref = list_first_entry(&prefs, struct __prelim_ref, list);
868 list_del(&ref->list);
869 if (ref->count < 0)
870 WARN_ON(1);
871 if (ref->count && ref->root_id && ref->parent == 0) {
872 /* no parent == root of tree */
873 ret = ulist_add(roots, ref->root_id, 0, GFP_NOFS);
874 BUG_ON(ret < 0);
875 }
876 if (ref->count && ref->parent) {
Jan Schmidt976b1902012-05-17 16:43:03 +0200877 struct extent_inode_elem *eie = NULL;
878 if (extent_item_pos) {
879 u32 bsz;
880 struct extent_buffer *eb;
881 bsz = btrfs_level_size(fs_info->extent_root,
882 info_level);
883 eb = read_tree_block(fs_info->extent_root,
884 ref->parent, bsz, 0);
885 BUG_ON(!eb);
886 ret = find_extent_in_eb(eb, bytenr,
887 *extent_item_pos, &eie);
888 free_extent_buffer(eb);
889 }
890 ret = ulist_add(refs, ref->parent,
891 (unsigned long)eie, GFP_NOFS);
Jan Schmidt8da6d582011-11-23 18:55:04 +0100892 BUG_ON(ret < 0);
893 }
894 kfree(ref);
895 }
896
897out:
898 if (head)
899 mutex_unlock(&head->mutex);
900 btrfs_free_path(path);
901 while (!list_empty(&prefs)) {
902 ref = list_first_entry(&prefs, struct __prelim_ref, list);
903 list_del(&ref->list);
904 kfree(ref);
905 }
906 while (!list_empty(&prefs_delayed)) {
907 ref = list_first_entry(&prefs_delayed, struct __prelim_ref,
908 list);
909 list_del(&ref->list);
910 kfree(ref);
911 }
912
913 return ret;
914}
915
Jan Schmidt976b1902012-05-17 16:43:03 +0200916static void free_leaf_list(struct ulist *blocks)
917{
918 struct ulist_node *node = NULL;
919 struct extent_inode_elem *eie;
920 struct extent_inode_elem *eie_next;
921 struct ulist_iterator uiter;
922
923 ULIST_ITER_INIT(&uiter);
924 while ((node = ulist_next(blocks, &uiter))) {
925 if (!node->aux)
926 continue;
927 eie = (struct extent_inode_elem *)node->aux;
928 for (; eie; eie = eie_next) {
929 eie_next = eie->next;
930 kfree(eie);
931 }
932 node->aux = 0;
933 }
934
935 ulist_free(blocks);
936}
937
Jan Schmidt8da6d582011-11-23 18:55:04 +0100938/*
939 * Finds all leafs with a reference to the specified combination of bytenr and
940 * offset. key_list_head will point to a list of corresponding keys (caller must
941 * free each list element). The leafs will be stored in the leafs ulist, which
942 * must be freed with ulist_free.
943 *
944 * returns 0 on success, <0 on error
945 */
946static int btrfs_find_all_leafs(struct btrfs_trans_handle *trans,
947 struct btrfs_fs_info *fs_info, u64 bytenr,
Jan Schmidt976b1902012-05-17 16:43:03 +0200948 u64 seq, struct ulist **leafs,
949 const u64 *extent_item_pos)
Jan Schmidt8da6d582011-11-23 18:55:04 +0100950{
951 struct ulist *tmp;
952 int ret;
953
954 tmp = ulist_alloc(GFP_NOFS);
955 if (!tmp)
956 return -ENOMEM;
957 *leafs = ulist_alloc(GFP_NOFS);
958 if (!*leafs) {
959 ulist_free(tmp);
960 return -ENOMEM;
961 }
962
Jan Schmidt976b1902012-05-17 16:43:03 +0200963 ret = find_parent_nodes(trans, fs_info, bytenr, seq, *leafs, tmp,
964 extent_item_pos);
Jan Schmidt8da6d582011-11-23 18:55:04 +0100965 ulist_free(tmp);
966
967 if (ret < 0 && ret != -ENOENT) {
Jan Schmidt976b1902012-05-17 16:43:03 +0200968 free_leaf_list(*leafs);
Jan Schmidt8da6d582011-11-23 18:55:04 +0100969 return ret;
970 }
971
972 return 0;
973}
974
975/*
976 * walk all backrefs for a given extent to find all roots that reference this
977 * extent. Walking a backref means finding all extents that reference this
978 * extent and in turn walk the backrefs of those, too. Naturally this is a
979 * recursive process, but here it is implemented in an iterative fashion: We
980 * find all referencing extents for the extent in question and put them on a
981 * list. In turn, we find all referencing extents for those, further appending
982 * to the list. The way we iterate the list allows adding more elements after
983 * the current while iterating. The process stops when we reach the end of the
984 * list. Found roots are added to the roots list.
985 *
986 * returns 0 on success, < 0 on error.
987 */
988int btrfs_find_all_roots(struct btrfs_trans_handle *trans,
989 struct btrfs_fs_info *fs_info, u64 bytenr,
Jan Schmidt976b1902012-05-17 16:43:03 +0200990 u64 seq, struct ulist **roots)
Jan Schmidt8da6d582011-11-23 18:55:04 +0100991{
992 struct ulist *tmp;
993 struct ulist_node *node = NULL;
Jan Schmidtcd1b4132012-05-22 14:56:50 +0200994 struct ulist_iterator uiter;
Jan Schmidt8da6d582011-11-23 18:55:04 +0100995 int ret;
996
997 tmp = ulist_alloc(GFP_NOFS);
998 if (!tmp)
999 return -ENOMEM;
1000 *roots = ulist_alloc(GFP_NOFS);
1001 if (!*roots) {
1002 ulist_free(tmp);
1003 return -ENOMEM;
1004 }
1005
Jan Schmidtcd1b4132012-05-22 14:56:50 +02001006 ULIST_ITER_INIT(&uiter);
Jan Schmidt8da6d582011-11-23 18:55:04 +01001007 while (1) {
1008 ret = find_parent_nodes(trans, fs_info, bytenr, seq,
Jan Schmidt976b1902012-05-17 16:43:03 +02001009 tmp, *roots, NULL);
Jan Schmidt8da6d582011-11-23 18:55:04 +01001010 if (ret < 0 && ret != -ENOENT) {
1011 ulist_free(tmp);
1012 ulist_free(*roots);
1013 return ret;
1014 }
Jan Schmidtcd1b4132012-05-22 14:56:50 +02001015 node = ulist_next(tmp, &uiter);
Jan Schmidt8da6d582011-11-23 18:55:04 +01001016 if (!node)
1017 break;
1018 bytenr = node->val;
1019 }
1020
1021 ulist_free(tmp);
1022 return 0;
1023}
1024
1025
Jan Schmidta542ad12011-06-13 19:52:59 +02001026static int __inode_info(u64 inum, u64 ioff, u8 key_type,
1027 struct btrfs_root *fs_root, struct btrfs_path *path,
1028 struct btrfs_key *found_key)
1029{
1030 int ret;
1031 struct btrfs_key key;
1032 struct extent_buffer *eb;
1033
1034 key.type = key_type;
1035 key.objectid = inum;
1036 key.offset = ioff;
1037
1038 ret = btrfs_search_slot(NULL, fs_root, &key, path, 0, 0);
1039 if (ret < 0)
1040 return ret;
1041
1042 eb = path->nodes[0];
1043 if (ret && path->slots[0] >= btrfs_header_nritems(eb)) {
1044 ret = btrfs_next_leaf(fs_root, path);
1045 if (ret)
1046 return ret;
1047 eb = path->nodes[0];
1048 }
1049
1050 btrfs_item_key_to_cpu(eb, found_key, path->slots[0]);
1051 if (found_key->type != key.type || found_key->objectid != key.objectid)
1052 return 1;
1053
1054 return 0;
1055}
1056
1057/*
1058 * this makes the path point to (inum INODE_ITEM ioff)
1059 */
1060int inode_item_info(u64 inum, u64 ioff, struct btrfs_root *fs_root,
1061 struct btrfs_path *path)
1062{
1063 struct btrfs_key key;
1064 return __inode_info(inum, ioff, BTRFS_INODE_ITEM_KEY, fs_root, path,
1065 &key);
1066}
1067
1068static int inode_ref_info(u64 inum, u64 ioff, struct btrfs_root *fs_root,
1069 struct btrfs_path *path,
1070 struct btrfs_key *found_key)
1071{
1072 return __inode_info(inum, ioff, BTRFS_INODE_REF_KEY, fs_root, path,
1073 found_key);
1074}
1075
1076/*
1077 * this iterates to turn a btrfs_inode_ref into a full filesystem path. elements
1078 * of the path are separated by '/' and the path is guaranteed to be
1079 * 0-terminated. the path is only given within the current file system.
1080 * Therefore, it never starts with a '/'. the caller is responsible to provide
1081 * "size" bytes in "dest". the dest buffer will be filled backwards. finally,
1082 * the start point of the resulting string is returned. this pointer is within
1083 * dest, normally.
1084 * in case the path buffer would overflow, the pointer is decremented further
1085 * as if output was written to the buffer, though no more output is actually
1086 * generated. that way, the caller can determine how much space would be
1087 * required for the path to fit into the buffer. in that case, the returned
1088 * value will be smaller than dest. callers must check this!
1089 */
1090static char *iref_to_path(struct btrfs_root *fs_root, struct btrfs_path *path,
1091 struct btrfs_inode_ref *iref,
1092 struct extent_buffer *eb_in, u64 parent,
1093 char *dest, u32 size)
1094{
1095 u32 len;
1096 int slot;
1097 u64 next_inum;
1098 int ret;
1099 s64 bytes_left = size - 1;
1100 struct extent_buffer *eb = eb_in;
1101 struct btrfs_key found_key;
Jan Schmidtb916a592012-04-13 12:28:08 +02001102 int leave_spinning = path->leave_spinning;
Jan Schmidta542ad12011-06-13 19:52:59 +02001103
1104 if (bytes_left >= 0)
1105 dest[bytes_left] = '\0';
1106
Jan Schmidtb916a592012-04-13 12:28:08 +02001107 path->leave_spinning = 1;
Jan Schmidta542ad12011-06-13 19:52:59 +02001108 while (1) {
1109 len = btrfs_inode_ref_name_len(eb, iref);
1110 bytes_left -= len;
1111 if (bytes_left >= 0)
1112 read_extent_buffer(eb, dest + bytes_left,
1113 (unsigned long)(iref + 1), len);
Jan Schmidtb916a592012-04-13 12:28:08 +02001114 if (eb != eb_in) {
1115 btrfs_tree_read_unlock_blocking(eb);
Jan Schmidta542ad12011-06-13 19:52:59 +02001116 free_extent_buffer(eb);
Jan Schmidtb916a592012-04-13 12:28:08 +02001117 }
Jan Schmidta542ad12011-06-13 19:52:59 +02001118 ret = inode_ref_info(parent, 0, fs_root, path, &found_key);
Jan Schmidt8f24b492012-02-08 16:01:01 +01001119 if (ret > 0)
1120 ret = -ENOENT;
Jan Schmidta542ad12011-06-13 19:52:59 +02001121 if (ret)
1122 break;
1123 next_inum = found_key.offset;
1124
1125 /* regular exit ahead */
1126 if (parent == next_inum)
1127 break;
1128
1129 slot = path->slots[0];
1130 eb = path->nodes[0];
1131 /* make sure we can use eb after releasing the path */
Jan Schmidtb916a592012-04-13 12:28:08 +02001132 if (eb != eb_in) {
Jan Schmidta542ad12011-06-13 19:52:59 +02001133 atomic_inc(&eb->refs);
Jan Schmidtb916a592012-04-13 12:28:08 +02001134 btrfs_tree_read_lock(eb);
1135 btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK);
1136 }
Jan Schmidta542ad12011-06-13 19:52:59 +02001137 btrfs_release_path(path);
1138
1139 iref = btrfs_item_ptr(eb, slot, struct btrfs_inode_ref);
1140 parent = next_inum;
1141 --bytes_left;
1142 if (bytes_left >= 0)
1143 dest[bytes_left] = '/';
1144 }
1145
1146 btrfs_release_path(path);
Jan Schmidtb916a592012-04-13 12:28:08 +02001147 path->leave_spinning = leave_spinning;
Jan Schmidta542ad12011-06-13 19:52:59 +02001148
1149 if (ret)
1150 return ERR_PTR(ret);
1151
1152 return dest + bytes_left;
1153}
1154
1155/*
1156 * this makes the path point to (logical EXTENT_ITEM *)
1157 * returns BTRFS_EXTENT_FLAG_DATA for data, BTRFS_EXTENT_FLAG_TREE_BLOCK for
1158 * tree blocks and <0 on error.
1159 */
1160int extent_from_logical(struct btrfs_fs_info *fs_info, u64 logical,
1161 struct btrfs_path *path, struct btrfs_key *found_key)
1162{
1163 int ret;
1164 u64 flags;
1165 u32 item_size;
1166 struct extent_buffer *eb;
1167 struct btrfs_extent_item *ei;
1168 struct btrfs_key key;
1169
1170 key.type = BTRFS_EXTENT_ITEM_KEY;
1171 key.objectid = logical;
1172 key.offset = (u64)-1;
1173
1174 ret = btrfs_search_slot(NULL, fs_info->extent_root, &key, path, 0, 0);
1175 if (ret < 0)
1176 return ret;
1177 ret = btrfs_previous_item(fs_info->extent_root, path,
1178 0, BTRFS_EXTENT_ITEM_KEY);
1179 if (ret < 0)
1180 return ret;
1181
1182 btrfs_item_key_to_cpu(path->nodes[0], found_key, path->slots[0]);
1183 if (found_key->type != BTRFS_EXTENT_ITEM_KEY ||
1184 found_key->objectid > logical ||
Jan Schmidt4692cf52011-12-02 14:56:41 +01001185 found_key->objectid + found_key->offset <= logical) {
1186 pr_debug("logical %llu is not within any extent\n",
1187 (unsigned long long)logical);
Jan Schmidta542ad12011-06-13 19:52:59 +02001188 return -ENOENT;
Jan Schmidt4692cf52011-12-02 14:56:41 +01001189 }
Jan Schmidta542ad12011-06-13 19:52:59 +02001190
1191 eb = path->nodes[0];
1192 item_size = btrfs_item_size_nr(eb, path->slots[0]);
1193 BUG_ON(item_size < sizeof(*ei));
1194
1195 ei = btrfs_item_ptr(eb, path->slots[0], struct btrfs_extent_item);
1196 flags = btrfs_extent_flags(eb, ei);
1197
Jan Schmidt4692cf52011-12-02 14:56:41 +01001198 pr_debug("logical %llu is at position %llu within the extent (%llu "
1199 "EXTENT_ITEM %llu) flags %#llx size %u\n",
1200 (unsigned long long)logical,
1201 (unsigned long long)(logical - found_key->objectid),
1202 (unsigned long long)found_key->objectid,
1203 (unsigned long long)found_key->offset,
1204 (unsigned long long)flags, item_size);
Jan Schmidta542ad12011-06-13 19:52:59 +02001205 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)
1206 return BTRFS_EXTENT_FLAG_TREE_BLOCK;
1207 if (flags & BTRFS_EXTENT_FLAG_DATA)
1208 return BTRFS_EXTENT_FLAG_DATA;
1209
1210 return -EIO;
1211}
1212
1213/*
1214 * helper function to iterate extent inline refs. ptr must point to a 0 value
1215 * for the first call and may be modified. it is used to track state.
1216 * if more refs exist, 0 is returned and the next call to
1217 * __get_extent_inline_ref must pass the modified ptr parameter to get the
1218 * next ref. after the last ref was processed, 1 is returned.
1219 * returns <0 on error
1220 */
1221static int __get_extent_inline_ref(unsigned long *ptr, struct extent_buffer *eb,
1222 struct btrfs_extent_item *ei, u32 item_size,
1223 struct btrfs_extent_inline_ref **out_eiref,
1224 int *out_type)
1225{
1226 unsigned long end;
1227 u64 flags;
1228 struct btrfs_tree_block_info *info;
1229
1230 if (!*ptr) {
1231 /* first call */
1232 flags = btrfs_extent_flags(eb, ei);
1233 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
1234 info = (struct btrfs_tree_block_info *)(ei + 1);
1235 *out_eiref =
1236 (struct btrfs_extent_inline_ref *)(info + 1);
1237 } else {
1238 *out_eiref = (struct btrfs_extent_inline_ref *)(ei + 1);
1239 }
1240 *ptr = (unsigned long)*out_eiref;
1241 if ((void *)*ptr >= (void *)ei + item_size)
1242 return -ENOENT;
1243 }
1244
1245 end = (unsigned long)ei + item_size;
1246 *out_eiref = (struct btrfs_extent_inline_ref *)*ptr;
1247 *out_type = btrfs_extent_inline_ref_type(eb, *out_eiref);
1248
1249 *ptr += btrfs_extent_inline_ref_size(*out_type);
1250 WARN_ON(*ptr > end);
1251 if (*ptr == end)
1252 return 1; /* last */
1253
1254 return 0;
1255}
1256
1257/*
1258 * reads the tree block backref for an extent. tree level and root are returned
1259 * through out_level and out_root. ptr must point to a 0 value for the first
1260 * call and may be modified (see __get_extent_inline_ref comment).
1261 * returns 0 if data was provided, 1 if there was no more data to provide or
1262 * <0 on error.
1263 */
1264int tree_backref_for_extent(unsigned long *ptr, struct extent_buffer *eb,
1265 struct btrfs_extent_item *ei, u32 item_size,
1266 u64 *out_root, u8 *out_level)
1267{
1268 int ret;
1269 int type;
1270 struct btrfs_tree_block_info *info;
1271 struct btrfs_extent_inline_ref *eiref;
1272
1273 if (*ptr == (unsigned long)-1)
1274 return 1;
1275
1276 while (1) {
1277 ret = __get_extent_inline_ref(ptr, eb, ei, item_size,
1278 &eiref, &type);
1279 if (ret < 0)
1280 return ret;
1281
1282 if (type == BTRFS_TREE_BLOCK_REF_KEY ||
1283 type == BTRFS_SHARED_BLOCK_REF_KEY)
1284 break;
1285
1286 if (ret == 1)
1287 return 1;
1288 }
1289
1290 /* we can treat both ref types equally here */
1291 info = (struct btrfs_tree_block_info *)(ei + 1);
1292 *out_root = btrfs_extent_inline_ref_offset(eb, eiref);
1293 *out_level = btrfs_tree_block_level(eb, info);
1294
1295 if (ret == 1)
1296 *ptr = (unsigned long)-1;
1297
1298 return 0;
1299}
1300
Jan Schmidt976b1902012-05-17 16:43:03 +02001301static int iterate_leaf_refs(struct extent_inode_elem *inode_list,
1302 u64 root, u64 extent_item_objectid,
Jan Schmidt4692cf52011-12-02 14:56:41 +01001303 iterate_extent_inodes_t *iterate, void *ctx)
Jan Schmidta542ad12011-06-13 19:52:59 +02001304{
Jan Schmidt976b1902012-05-17 16:43:03 +02001305 struct extent_inode_elem *eie;
Jan Schmidt4692cf52011-12-02 14:56:41 +01001306 int ret = 0;
Jan Schmidta542ad12011-06-13 19:52:59 +02001307
Jan Schmidt976b1902012-05-17 16:43:03 +02001308 for (eie = inode_list; eie; eie = eie->next) {
Jan Schmidt4692cf52011-12-02 14:56:41 +01001309 pr_debug("ref for %llu resolved, key (%llu EXTEND_DATA %llu), "
Jan Schmidt976b1902012-05-17 16:43:03 +02001310 "root %llu\n", extent_item_objectid,
1311 eie->inum, eie->offset, root);
1312 ret = iterate(eie->inum, eie->offset, root, ctx);
Jan Schmidt4692cf52011-12-02 14:56:41 +01001313 if (ret) {
Jan Schmidt976b1902012-05-17 16:43:03 +02001314 pr_debug("stopping iteration for %llu due to ret=%d\n",
1315 extent_item_objectid, ret);
Jan Schmidt4692cf52011-12-02 14:56:41 +01001316 break;
1317 }
Jan Schmidta542ad12011-06-13 19:52:59 +02001318 }
1319
Jan Schmidta542ad12011-06-13 19:52:59 +02001320 return ret;
1321}
1322
1323/*
1324 * calls iterate() for every inode that references the extent identified by
Jan Schmidt4692cf52011-12-02 14:56:41 +01001325 * the given parameters.
Jan Schmidta542ad12011-06-13 19:52:59 +02001326 * when the iterator function returns a non-zero value, iteration stops.
1327 */
1328int iterate_extent_inodes(struct btrfs_fs_info *fs_info,
Jan Schmidt4692cf52011-12-02 14:56:41 +01001329 u64 extent_item_objectid, u64 extent_item_pos,
Jan Schmidt7a3ae2f2012-03-23 17:32:28 +01001330 int search_commit_root,
Jan Schmidta542ad12011-06-13 19:52:59 +02001331 iterate_extent_inodes_t *iterate, void *ctx)
1332{
Jan Schmidta542ad12011-06-13 19:52:59 +02001333 int ret;
Jan Schmidta542ad12011-06-13 19:52:59 +02001334 struct list_head data_refs = LIST_HEAD_INIT(data_refs);
1335 struct list_head shared_refs = LIST_HEAD_INIT(shared_refs);
Jan Schmidt4692cf52011-12-02 14:56:41 +01001336 struct btrfs_trans_handle *trans;
Jan Schmidt7a3ae2f2012-03-23 17:32:28 +01001337 struct ulist *refs = NULL;
1338 struct ulist *roots = NULL;
Jan Schmidt4692cf52011-12-02 14:56:41 +01001339 struct ulist_node *ref_node = NULL;
1340 struct ulist_node *root_node = NULL;
1341 struct seq_list seq_elem;
Jan Schmidtcd1b4132012-05-22 14:56:50 +02001342 struct ulist_iterator ref_uiter;
1343 struct ulist_iterator root_uiter;
Jan Schmidt7a3ae2f2012-03-23 17:32:28 +01001344 struct btrfs_delayed_ref_root *delayed_refs = NULL;
Jan Schmidta542ad12011-06-13 19:52:59 +02001345
Jan Schmidt4692cf52011-12-02 14:56:41 +01001346 pr_debug("resolving all inodes for extent %llu\n",
1347 extent_item_objectid);
1348
Jan Schmidt7a3ae2f2012-03-23 17:32:28 +01001349 if (search_commit_root) {
1350 trans = BTRFS_BACKREF_SEARCH_COMMIT_ROOT;
1351 } else {
1352 trans = btrfs_join_transaction(fs_info->extent_root);
1353 if (IS_ERR(trans))
1354 return PTR_ERR(trans);
1355
1356 delayed_refs = &trans->transaction->delayed_refs;
1357 spin_lock(&delayed_refs->lock);
1358 btrfs_get_delayed_seq(delayed_refs, &seq_elem);
1359 spin_unlock(&delayed_refs->lock);
1360 }
Jan Schmidt4692cf52011-12-02 14:56:41 +01001361
1362 ret = btrfs_find_all_leafs(trans, fs_info, extent_item_objectid,
Jan Schmidt976b1902012-05-17 16:43:03 +02001363 seq_elem.seq, &refs, &extent_item_pos);
Jan Schmidt4692cf52011-12-02 14:56:41 +01001364 if (ret)
1365 goto out;
1366
Jan Schmidtcd1b4132012-05-22 14:56:50 +02001367 ULIST_ITER_INIT(&ref_uiter);
1368 while (!ret && (ref_node = ulist_next(refs, &ref_uiter))) {
Jan Schmidt976b1902012-05-17 16:43:03 +02001369 ret = btrfs_find_all_roots(trans, fs_info, ref_node->val,
Jan Schmidt4692cf52011-12-02 14:56:41 +01001370 seq_elem.seq, &roots);
1371 if (ret)
Jan Schmidta542ad12011-06-13 19:52:59 +02001372 break;
Jan Schmidtcd1b4132012-05-22 14:56:50 +02001373 ULIST_ITER_INIT(&root_uiter);
1374 while (!ret && (root_node = ulist_next(roots, &root_uiter))) {
Jan Schmidt976b1902012-05-17 16:43:03 +02001375 pr_debug("root %llu references leaf %llu, data list "
1376 "%#lx\n", root_node->val, ref_node->val,
1377 ref_node->aux);
1378 ret = iterate_leaf_refs(
1379 (struct extent_inode_elem *)ref_node->aux,
1380 root_node->val, extent_item_objectid,
1381 iterate, ctx);
Jan Schmidta542ad12011-06-13 19:52:59 +02001382 }
Jan Schmidt976b1902012-05-17 16:43:03 +02001383 ulist_free(roots);
1384 roots = NULL;
Jan Schmidta542ad12011-06-13 19:52:59 +02001385 }
1386
Jan Schmidt976b1902012-05-17 16:43:03 +02001387 free_leaf_list(refs);
Jan Schmidt4692cf52011-12-02 14:56:41 +01001388 ulist_free(roots);
1389out:
Jan Schmidt7a3ae2f2012-03-23 17:32:28 +01001390 if (!search_commit_root) {
1391 btrfs_put_delayed_seq(delayed_refs, &seq_elem);
1392 btrfs_end_transaction(trans, fs_info->extent_root);
1393 }
1394
Jan Schmidta542ad12011-06-13 19:52:59 +02001395 return ret;
1396}
1397
1398int iterate_inodes_from_logical(u64 logical, struct btrfs_fs_info *fs_info,
1399 struct btrfs_path *path,
1400 iterate_extent_inodes_t *iterate, void *ctx)
1401{
1402 int ret;
Jan Schmidt4692cf52011-12-02 14:56:41 +01001403 u64 extent_item_pos;
Jan Schmidta542ad12011-06-13 19:52:59 +02001404 struct btrfs_key found_key;
Jan Schmidt7a3ae2f2012-03-23 17:32:28 +01001405 int search_commit_root = path->search_commit_root;
Jan Schmidta542ad12011-06-13 19:52:59 +02001406
1407 ret = extent_from_logical(fs_info, logical, path,
1408 &found_key);
Jan Schmidt4692cf52011-12-02 14:56:41 +01001409 btrfs_release_path(path);
Jan Schmidta542ad12011-06-13 19:52:59 +02001410 if (ret & BTRFS_EXTENT_FLAG_TREE_BLOCK)
1411 ret = -EINVAL;
1412 if (ret < 0)
1413 return ret;
1414
Jan Schmidt4692cf52011-12-02 14:56:41 +01001415 extent_item_pos = logical - found_key.objectid;
Jan Schmidt7a3ae2f2012-03-23 17:32:28 +01001416 ret = iterate_extent_inodes(fs_info, found_key.objectid,
1417 extent_item_pos, search_commit_root,
1418 iterate, ctx);
Jan Schmidta542ad12011-06-13 19:52:59 +02001419
1420 return ret;
1421}
1422
1423static int iterate_irefs(u64 inum, struct btrfs_root *fs_root,
1424 struct btrfs_path *path,
1425 iterate_irefs_t *iterate, void *ctx)
1426{
Jan Schmidtaefc1eb2012-04-13 12:28:00 +02001427 int ret = 0;
Jan Schmidta542ad12011-06-13 19:52:59 +02001428 int slot;
1429 u32 cur;
1430 u32 len;
1431 u32 name_len;
1432 u64 parent = 0;
1433 int found = 0;
1434 struct extent_buffer *eb;
1435 struct btrfs_item *item;
1436 struct btrfs_inode_ref *iref;
1437 struct btrfs_key found_key;
1438
Jan Schmidtaefc1eb2012-04-13 12:28:00 +02001439 while (!ret) {
Jan Schmidtb916a592012-04-13 12:28:08 +02001440 path->leave_spinning = 1;
Jan Schmidta542ad12011-06-13 19:52:59 +02001441 ret = inode_ref_info(inum, parent ? parent+1 : 0, fs_root, path,
1442 &found_key);
1443 if (ret < 0)
1444 break;
1445 if (ret) {
1446 ret = found ? 0 : -ENOENT;
1447 break;
1448 }
1449 ++found;
1450
1451 parent = found_key.offset;
1452 slot = path->slots[0];
1453 eb = path->nodes[0];
1454 /* make sure we can use eb after releasing the path */
1455 atomic_inc(&eb->refs);
Jan Schmidtb916a592012-04-13 12:28:08 +02001456 btrfs_tree_read_lock(eb);
1457 btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK);
Jan Schmidta542ad12011-06-13 19:52:59 +02001458 btrfs_release_path(path);
1459
1460 item = btrfs_item_nr(eb, slot);
1461 iref = btrfs_item_ptr(eb, slot, struct btrfs_inode_ref);
1462
1463 for (cur = 0; cur < btrfs_item_size(eb, item); cur += len) {
1464 name_len = btrfs_inode_ref_name_len(eb, iref);
1465 /* path must be released before calling iterate()! */
Jan Schmidt4692cf52011-12-02 14:56:41 +01001466 pr_debug("following ref at offset %u for inode %llu in "
1467 "tree %llu\n", cur,
1468 (unsigned long long)found_key.objectid,
1469 (unsigned long long)fs_root->objectid);
Jan Schmidta542ad12011-06-13 19:52:59 +02001470 ret = iterate(parent, iref, eb, ctx);
Jan Schmidtaefc1eb2012-04-13 12:28:00 +02001471 if (ret)
Jan Schmidta542ad12011-06-13 19:52:59 +02001472 break;
Jan Schmidta542ad12011-06-13 19:52:59 +02001473 len = sizeof(*iref) + name_len;
1474 iref = (struct btrfs_inode_ref *)((char *)iref + len);
1475 }
Jan Schmidtb916a592012-04-13 12:28:08 +02001476 btrfs_tree_read_unlock_blocking(eb);
Jan Schmidta542ad12011-06-13 19:52:59 +02001477 free_extent_buffer(eb);
1478 }
1479
1480 btrfs_release_path(path);
1481
1482 return ret;
1483}
1484
1485/*
1486 * returns 0 if the path could be dumped (probably truncated)
1487 * returns <0 in case of an error
1488 */
1489static int inode_to_path(u64 inum, struct btrfs_inode_ref *iref,
1490 struct extent_buffer *eb, void *ctx)
1491{
1492 struct inode_fs_paths *ipath = ctx;
1493 char *fspath;
1494 char *fspath_min;
1495 int i = ipath->fspath->elem_cnt;
1496 const int s_ptr = sizeof(char *);
1497 u32 bytes_left;
1498
1499 bytes_left = ipath->fspath->bytes_left > s_ptr ?
1500 ipath->fspath->bytes_left - s_ptr : 0;
1501
Chris Mason740c3d22011-11-02 15:48:34 -04001502 fspath_min = (char *)ipath->fspath->val + (i + 1) * s_ptr;
Jan Schmidta542ad12011-06-13 19:52:59 +02001503 fspath = iref_to_path(ipath->fs_root, ipath->btrfs_path, iref, eb,
1504 inum, fspath_min, bytes_left);
1505 if (IS_ERR(fspath))
1506 return PTR_ERR(fspath);
1507
1508 if (fspath > fspath_min) {
Jan Schmidt4692cf52011-12-02 14:56:41 +01001509 pr_debug("path resolved: %s\n", fspath);
Jeff Mahoney745c4d82011-11-20 07:31:57 -05001510 ipath->fspath->val[i] = (u64)(unsigned long)fspath;
Jan Schmidta542ad12011-06-13 19:52:59 +02001511 ++ipath->fspath->elem_cnt;
1512 ipath->fspath->bytes_left = fspath - fspath_min;
1513 } else {
Jan Schmidt4692cf52011-12-02 14:56:41 +01001514 pr_debug("missed path, not enough space. missing bytes: %lu, "
1515 "constructed so far: %s\n",
1516 (unsigned long)(fspath_min - fspath), fspath_min);
Jan Schmidta542ad12011-06-13 19:52:59 +02001517 ++ipath->fspath->elem_missed;
1518 ipath->fspath->bytes_missing += fspath_min - fspath;
1519 ipath->fspath->bytes_left = 0;
1520 }
1521
1522 return 0;
1523}
1524
1525/*
1526 * this dumps all file system paths to the inode into the ipath struct, provided
1527 * is has been created large enough. each path is zero-terminated and accessed
Chris Mason740c3d22011-11-02 15:48:34 -04001528 * from ipath->fspath->val[i].
Jan Schmidta542ad12011-06-13 19:52:59 +02001529 * when it returns, there are ipath->fspath->elem_cnt number of paths available
Chris Mason740c3d22011-11-02 15:48:34 -04001530 * in ipath->fspath->val[]. when the allocated space wasn't sufficient, the
Jan Schmidta542ad12011-06-13 19:52:59 +02001531 * number of missed paths in recored in ipath->fspath->elem_missed, otherwise,
1532 * it's zero. ipath->fspath->bytes_missing holds the number of bytes that would
1533 * have been needed to return all paths.
1534 */
1535int paths_from_inode(u64 inum, struct inode_fs_paths *ipath)
1536{
1537 return iterate_irefs(inum, ipath->fs_root, ipath->btrfs_path,
1538 inode_to_path, ipath);
1539}
1540
Jan Schmidta542ad12011-06-13 19:52:59 +02001541struct btrfs_data_container *init_data_container(u32 total_bytes)
1542{
1543 struct btrfs_data_container *data;
1544 size_t alloc_bytes;
1545
1546 alloc_bytes = max_t(size_t, total_bytes, sizeof(*data));
1547 data = kmalloc(alloc_bytes, GFP_NOFS);
1548 if (!data)
1549 return ERR_PTR(-ENOMEM);
1550
1551 if (total_bytes >= sizeof(*data)) {
1552 data->bytes_left = total_bytes - sizeof(*data);
1553 data->bytes_missing = 0;
1554 } else {
1555 data->bytes_missing = sizeof(*data) - total_bytes;
1556 data->bytes_left = 0;
1557 }
1558
1559 data->elem_cnt = 0;
1560 data->elem_missed = 0;
1561
1562 return data;
1563}
1564
1565/*
1566 * allocates space to return multiple file system paths for an inode.
1567 * total_bytes to allocate are passed, note that space usable for actual path
1568 * information will be total_bytes - sizeof(struct inode_fs_paths).
1569 * the returned pointer must be freed with free_ipath() in the end.
1570 */
1571struct inode_fs_paths *init_ipath(s32 total_bytes, struct btrfs_root *fs_root,
1572 struct btrfs_path *path)
1573{
1574 struct inode_fs_paths *ifp;
1575 struct btrfs_data_container *fspath;
1576
1577 fspath = init_data_container(total_bytes);
1578 if (IS_ERR(fspath))
1579 return (void *)fspath;
1580
1581 ifp = kmalloc(sizeof(*ifp), GFP_NOFS);
1582 if (!ifp) {
1583 kfree(fspath);
1584 return ERR_PTR(-ENOMEM);
1585 }
1586
1587 ifp->btrfs_path = path;
1588 ifp->fspath = fspath;
1589 ifp->fs_root = fs_root;
1590
1591 return ifp;
1592}
1593
1594void free_ipath(struct inode_fs_paths *ipath)
1595{
Jesper Juhl4735fb22012-04-12 22:47:52 +02001596 if (!ipath)
1597 return;
Ilya Dryomov5eb56d22012-03-27 17:09:18 +03001598 kfree(ipath->fspath);
Jan Schmidta542ad12011-06-13 19:52:59 +02001599 kfree(ipath);
1600}