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