]>
Commit | Line | Data |
---|---|---|
c1d7c514 | 1 | // SPDX-License-Identifier: GPL-2.0 |
5d4f98a2 YZ |
2 | /* |
3 | * Copyright (C) 2009 Oracle. All rights reserved. | |
5d4f98a2 YZ |
4 | */ |
5 | ||
6 | #include <linux/sched.h> | |
7 | #include <linux/pagemap.h> | |
8 | #include <linux/writeback.h> | |
9 | #include <linux/blkdev.h> | |
10 | #include <linux/rbtree.h> | |
5a0e3ad6 | 11 | #include <linux/slab.h> |
726a3421 | 12 | #include <linux/error-injection.h> |
5d4f98a2 YZ |
13 | #include "ctree.h" |
14 | #include "disk-io.h" | |
15 | #include "transaction.h" | |
16 | #include "volumes.h" | |
17 | #include "locking.h" | |
18 | #include "btrfs_inode.h" | |
19 | #include "async-thread.h" | |
0af3d00b | 20 | #include "free-space-cache.h" |
581bb050 | 21 | #include "inode-map.h" |
62b99540 | 22 | #include "qgroup.h" |
cdccee99 | 23 | #include "print-tree.h" |
86736342 | 24 | #include "delalloc-space.h" |
aac0023c | 25 | #include "block-group.h" |
19b546d7 | 26 | #include "backref.h" |
5d4f98a2 | 27 | |
0c891389 QW |
28 | /* |
29 | * Relocation overview | |
30 | * | |
31 | * [What does relocation do] | |
32 | * | |
33 | * The objective of relocation is to relocate all extents of the target block | |
34 | * group to other block groups. | |
35 | * This is utilized by resize (shrink only), profile converting, compacting | |
36 | * space, or balance routine to spread chunks over devices. | |
37 | * | |
38 | * Before | After | |
39 | * ------------------------------------------------------------------ | |
40 | * BG A: 10 data extents | BG A: deleted | |
41 | * BG B: 2 data extents | BG B: 10 data extents (2 old + 8 relocated) | |
42 | * BG C: 1 extents | BG C: 3 data extents (1 old + 2 relocated) | |
43 | * | |
44 | * [How does relocation work] | |
45 | * | |
46 | * 1. Mark the target block group read-only | |
47 | * New extents won't be allocated from the target block group. | |
48 | * | |
49 | * 2.1 Record each extent in the target block group | |
50 | * To build a proper map of extents to be relocated. | |
51 | * | |
52 | * 2.2 Build data reloc tree and reloc trees | |
53 | * Data reloc tree will contain an inode, recording all newly relocated | |
54 | * data extents. | |
55 | * There will be only one data reloc tree for one data block group. | |
56 | * | |
57 | * Reloc tree will be a special snapshot of its source tree, containing | |
58 | * relocated tree blocks. | |
59 | * Each tree referring to a tree block in target block group will get its | |
60 | * reloc tree built. | |
61 | * | |
62 | * 2.3 Swap source tree with its corresponding reloc tree | |
63 | * Each involved tree only refers to new extents after swap. | |
64 | * | |
65 | * 3. Cleanup reloc trees and data reloc tree. | |
66 | * As old extents in the target block group are still referenced by reloc | |
67 | * trees, we need to clean them up before really freeing the target block | |
68 | * group. | |
69 | * | |
70 | * The main complexity is in steps 2.2 and 2.3. | |
71 | * | |
72 | * The entry point of relocation is relocate_block_group() function. | |
73 | */ | |
74 | ||
5d4f98a2 YZ |
75 | /* |
76 | * backref_node, mapping_node and tree_block start with this | |
77 | */ | |
78 | struct tree_entry { | |
79 | struct rb_node rb_node; | |
80 | u64 bytenr; | |
81 | }; | |
82 | ||
83 | /* | |
84 | * present a tree block in the backref cache | |
85 | */ | |
86 | struct backref_node { | |
87 | struct rb_node rb_node; | |
88 | u64 bytenr; | |
3fd0a558 YZ |
89 | |
90 | u64 new_bytenr; | |
91 | /* objectid of tree block owner, can be not uptodate */ | |
5d4f98a2 | 92 | u64 owner; |
3fd0a558 YZ |
93 | /* link to pending, changed or detached list */ |
94 | struct list_head list; | |
5d4f98a2 YZ |
95 | /* list of upper level blocks reference this block */ |
96 | struct list_head upper; | |
97 | /* list of child blocks in the cache */ | |
98 | struct list_head lower; | |
99 | /* NULL if this node is not tree root */ | |
100 | struct btrfs_root *root; | |
101 | /* extent buffer got by COW the block */ | |
102 | struct extent_buffer *eb; | |
103 | /* level of tree block */ | |
104 | unsigned int level:8; | |
3fd0a558 YZ |
105 | /* is the block in non-reference counted tree */ |
106 | unsigned int cowonly:1; | |
107 | /* 1 if no child node in the cache */ | |
5d4f98a2 YZ |
108 | unsigned int lowest:1; |
109 | /* is the extent buffer locked */ | |
110 | unsigned int locked:1; | |
111 | /* has the block been processed */ | |
112 | unsigned int processed:1; | |
113 | /* have backrefs of this block been checked */ | |
114 | unsigned int checked:1; | |
3fd0a558 YZ |
115 | /* |
116 | * 1 if corresponding block has been cowed but some upper | |
117 | * level block pointers may not point to the new location | |
118 | */ | |
119 | unsigned int pending:1; | |
120 | /* | |
121 | * 1 if the backref node isn't connected to any other | |
122 | * backref node. | |
123 | */ | |
124 | unsigned int detached:1; | |
5d4f98a2 YZ |
125 | }; |
126 | ||
127 | /* | |
128 | * present a block pointer in the backref cache | |
129 | */ | |
130 | struct backref_edge { | |
131 | struct list_head list[2]; | |
132 | struct backref_node *node[2]; | |
5d4f98a2 YZ |
133 | }; |
134 | ||
135 | #define LOWER 0 | |
136 | #define UPPER 1 | |
0647bf56 | 137 | #define RELOCATION_RESERVED_NODES 256 |
5d4f98a2 YZ |
138 | |
139 | struct backref_cache { | |
140 | /* red black tree of all backref nodes in the cache */ | |
141 | struct rb_root rb_root; | |
3fd0a558 YZ |
142 | /* for passing backref nodes to btrfs_reloc_cow_block */ |
143 | struct backref_node *path[BTRFS_MAX_LEVEL]; | |
144 | /* | |
145 | * list of blocks that have been cowed but some block | |
146 | * pointers in upper level blocks may not reflect the | |
147 | * new location | |
148 | */ | |
5d4f98a2 | 149 | struct list_head pending[BTRFS_MAX_LEVEL]; |
3fd0a558 YZ |
150 | /* list of backref nodes with no child node */ |
151 | struct list_head leaves; | |
152 | /* list of blocks that have been cowed in current transaction */ | |
153 | struct list_head changed; | |
154 | /* list of detached backref node. */ | |
155 | struct list_head detached; | |
156 | ||
157 | u64 last_trans; | |
158 | ||
159 | int nr_nodes; | |
160 | int nr_edges; | |
5d4f98a2 YZ |
161 | }; |
162 | ||
163 | /* | |
164 | * map address of tree root to tree | |
165 | */ | |
166 | struct mapping_node { | |
167 | struct rb_node rb_node; | |
168 | u64 bytenr; | |
169 | void *data; | |
170 | }; | |
171 | ||
172 | struct mapping_tree { | |
173 | struct rb_root rb_root; | |
174 | spinlock_t lock; | |
175 | }; | |
176 | ||
177 | /* | |
178 | * present a tree block to process | |
179 | */ | |
180 | struct tree_block { | |
181 | struct rb_node rb_node; | |
182 | u64 bytenr; | |
183 | struct btrfs_key key; | |
184 | unsigned int level:8; | |
185 | unsigned int key_ready:1; | |
186 | }; | |
187 | ||
0257bb82 YZ |
188 | #define MAX_EXTENTS 128 |
189 | ||
190 | struct file_extent_cluster { | |
191 | u64 start; | |
192 | u64 end; | |
193 | u64 boundary[MAX_EXTENTS]; | |
194 | unsigned int nr; | |
195 | }; | |
196 | ||
5d4f98a2 YZ |
197 | struct reloc_control { |
198 | /* block group to relocate */ | |
32da5386 | 199 | struct btrfs_block_group *block_group; |
5d4f98a2 YZ |
200 | /* extent tree */ |
201 | struct btrfs_root *extent_root; | |
202 | /* inode for moving data */ | |
203 | struct inode *data_inode; | |
3fd0a558 YZ |
204 | |
205 | struct btrfs_block_rsv *block_rsv; | |
206 | ||
207 | struct backref_cache backref_cache; | |
208 | ||
209 | struct file_extent_cluster cluster; | |
5d4f98a2 YZ |
210 | /* tree blocks have been processed */ |
211 | struct extent_io_tree processed_blocks; | |
212 | /* map start of tree root to corresponding reloc tree */ | |
213 | struct mapping_tree reloc_root_tree; | |
214 | /* list of reloc trees */ | |
215 | struct list_head reloc_roots; | |
d2311e69 QW |
216 | /* list of subvolume trees that get relocated */ |
217 | struct list_head dirty_subvol_roots; | |
3fd0a558 YZ |
218 | /* size of metadata reservation for merging reloc trees */ |
219 | u64 merging_rsv_size; | |
220 | /* size of relocated tree nodes */ | |
221 | u64 nodes_relocated; | |
0647bf56 WS |
222 | /* reserved size for block group relocation*/ |
223 | u64 reserved_bytes; | |
3fd0a558 | 224 | |
5d4f98a2 YZ |
225 | u64 search_start; |
226 | u64 extents_found; | |
3fd0a558 | 227 | |
3fd0a558 YZ |
228 | unsigned int stage:8; |
229 | unsigned int create_reloc_tree:1; | |
230 | unsigned int merge_reloc_tree:1; | |
5d4f98a2 | 231 | unsigned int found_file_extent:1; |
5d4f98a2 YZ |
232 | }; |
233 | ||
234 | /* stages of data relocation */ | |
235 | #define MOVE_DATA_EXTENTS 0 | |
236 | #define UPDATE_DATA_PTRS 1 | |
237 | ||
3fd0a558 YZ |
238 | static void remove_backref_node(struct backref_cache *cache, |
239 | struct backref_node *node); | |
240 | static void __mark_block_processed(struct reloc_control *rc, | |
241 | struct backref_node *node); | |
5d4f98a2 YZ |
242 | |
243 | static void mapping_tree_init(struct mapping_tree *tree) | |
244 | { | |
6bef4d31 | 245 | tree->rb_root = RB_ROOT; |
5d4f98a2 YZ |
246 | spin_lock_init(&tree->lock); |
247 | } | |
248 | ||
249 | static void backref_cache_init(struct backref_cache *cache) | |
250 | { | |
251 | int i; | |
6bef4d31 | 252 | cache->rb_root = RB_ROOT; |
5d4f98a2 YZ |
253 | for (i = 0; i < BTRFS_MAX_LEVEL; i++) |
254 | INIT_LIST_HEAD(&cache->pending[i]); | |
3fd0a558 YZ |
255 | INIT_LIST_HEAD(&cache->changed); |
256 | INIT_LIST_HEAD(&cache->detached); | |
257 | INIT_LIST_HEAD(&cache->leaves); | |
258 | } | |
259 | ||
260 | static void backref_cache_cleanup(struct backref_cache *cache) | |
261 | { | |
262 | struct backref_node *node; | |
263 | int i; | |
264 | ||
265 | while (!list_empty(&cache->detached)) { | |
266 | node = list_entry(cache->detached.next, | |
267 | struct backref_node, list); | |
268 | remove_backref_node(cache, node); | |
269 | } | |
270 | ||
271 | while (!list_empty(&cache->leaves)) { | |
272 | node = list_entry(cache->leaves.next, | |
273 | struct backref_node, lower); | |
274 | remove_backref_node(cache, node); | |
275 | } | |
276 | ||
277 | cache->last_trans = 0; | |
278 | ||
279 | for (i = 0; i < BTRFS_MAX_LEVEL; i++) | |
f4907095 LB |
280 | ASSERT(list_empty(&cache->pending[i])); |
281 | ASSERT(list_empty(&cache->changed)); | |
282 | ASSERT(list_empty(&cache->detached)); | |
283 | ASSERT(RB_EMPTY_ROOT(&cache->rb_root)); | |
284 | ASSERT(!cache->nr_nodes); | |
285 | ASSERT(!cache->nr_edges); | |
3fd0a558 YZ |
286 | } |
287 | ||
288 | static struct backref_node *alloc_backref_node(struct backref_cache *cache) | |
289 | { | |
290 | struct backref_node *node; | |
291 | ||
292 | node = kzalloc(sizeof(*node), GFP_NOFS); | |
293 | if (node) { | |
294 | INIT_LIST_HEAD(&node->list); | |
295 | INIT_LIST_HEAD(&node->upper); | |
296 | INIT_LIST_HEAD(&node->lower); | |
297 | RB_CLEAR_NODE(&node->rb_node); | |
298 | cache->nr_nodes++; | |
299 | } | |
300 | return node; | |
301 | } | |
302 | ||
303 | static void free_backref_node(struct backref_cache *cache, | |
304 | struct backref_node *node) | |
305 | { | |
306 | if (node) { | |
307 | cache->nr_nodes--; | |
00246528 | 308 | btrfs_put_root(node->root); |
3fd0a558 YZ |
309 | kfree(node); |
310 | } | |
311 | } | |
312 | ||
313 | static struct backref_edge *alloc_backref_edge(struct backref_cache *cache) | |
314 | { | |
315 | struct backref_edge *edge; | |
316 | ||
317 | edge = kzalloc(sizeof(*edge), GFP_NOFS); | |
318 | if (edge) | |
319 | cache->nr_edges++; | |
320 | return edge; | |
5d4f98a2 YZ |
321 | } |
322 | ||
3fd0a558 YZ |
323 | static void free_backref_edge(struct backref_cache *cache, |
324 | struct backref_edge *edge) | |
5d4f98a2 | 325 | { |
3fd0a558 YZ |
326 | if (edge) { |
327 | cache->nr_edges--; | |
328 | kfree(edge); | |
329 | } | |
5d4f98a2 YZ |
330 | } |
331 | ||
332 | static struct rb_node *tree_insert(struct rb_root *root, u64 bytenr, | |
333 | struct rb_node *node) | |
334 | { | |
335 | struct rb_node **p = &root->rb_node; | |
336 | struct rb_node *parent = NULL; | |
337 | struct tree_entry *entry; | |
338 | ||
339 | while (*p) { | |
340 | parent = *p; | |
341 | entry = rb_entry(parent, struct tree_entry, rb_node); | |
342 | ||
343 | if (bytenr < entry->bytenr) | |
344 | p = &(*p)->rb_left; | |
345 | else if (bytenr > entry->bytenr) | |
346 | p = &(*p)->rb_right; | |
347 | else | |
348 | return parent; | |
349 | } | |
350 | ||
351 | rb_link_node(node, parent, p); | |
352 | rb_insert_color(node, root); | |
353 | return NULL; | |
354 | } | |
355 | ||
356 | static struct rb_node *tree_search(struct rb_root *root, u64 bytenr) | |
357 | { | |
358 | struct rb_node *n = root->rb_node; | |
359 | struct tree_entry *entry; | |
360 | ||
361 | while (n) { | |
362 | entry = rb_entry(n, struct tree_entry, rb_node); | |
363 | ||
364 | if (bytenr < entry->bytenr) | |
365 | n = n->rb_left; | |
366 | else if (bytenr > entry->bytenr) | |
367 | n = n->rb_right; | |
368 | else | |
369 | return n; | |
370 | } | |
371 | return NULL; | |
372 | } | |
373 | ||
48a3b636 | 374 | static void backref_tree_panic(struct rb_node *rb_node, int errno, u64 bytenr) |
43c04fb1 JM |
375 | { |
376 | ||
377 | struct btrfs_fs_info *fs_info = NULL; | |
378 | struct backref_node *bnode = rb_entry(rb_node, struct backref_node, | |
379 | rb_node); | |
380 | if (bnode->root) | |
381 | fs_info = bnode->root->fs_info; | |
5d163e0e JM |
382 | btrfs_panic(fs_info, errno, |
383 | "Inconsistency in backref cache found at offset %llu", | |
384 | bytenr); | |
43c04fb1 JM |
385 | } |
386 | ||
5d4f98a2 YZ |
387 | /* |
388 | * walk up backref nodes until reach node presents tree root | |
389 | */ | |
390 | static struct backref_node *walk_up_backref(struct backref_node *node, | |
391 | struct backref_edge *edges[], | |
392 | int *index) | |
393 | { | |
394 | struct backref_edge *edge; | |
395 | int idx = *index; | |
396 | ||
397 | while (!list_empty(&node->upper)) { | |
398 | edge = list_entry(node->upper.next, | |
399 | struct backref_edge, list[LOWER]); | |
400 | edges[idx++] = edge; | |
401 | node = edge->node[UPPER]; | |
402 | } | |
3fd0a558 | 403 | BUG_ON(node->detached); |
5d4f98a2 YZ |
404 | *index = idx; |
405 | return node; | |
406 | } | |
407 | ||
408 | /* | |
409 | * walk down backref nodes to find start of next reference path | |
410 | */ | |
411 | static struct backref_node *walk_down_backref(struct backref_edge *edges[], | |
412 | int *index) | |
413 | { | |
414 | struct backref_edge *edge; | |
415 | struct backref_node *lower; | |
416 | int idx = *index; | |
417 | ||
418 | while (idx > 0) { | |
419 | edge = edges[idx - 1]; | |
420 | lower = edge->node[LOWER]; | |
421 | if (list_is_last(&edge->list[LOWER], &lower->upper)) { | |
422 | idx--; | |
423 | continue; | |
424 | } | |
425 | edge = list_entry(edge->list[LOWER].next, | |
426 | struct backref_edge, list[LOWER]); | |
427 | edges[idx - 1] = edge; | |
428 | *index = idx; | |
429 | return edge->node[UPPER]; | |
430 | } | |
431 | *index = 0; | |
432 | return NULL; | |
433 | } | |
434 | ||
3fd0a558 YZ |
435 | static void unlock_node_buffer(struct backref_node *node) |
436 | { | |
437 | if (node->locked) { | |
438 | btrfs_tree_unlock(node->eb); | |
439 | node->locked = 0; | |
440 | } | |
441 | } | |
442 | ||
5d4f98a2 YZ |
443 | static void drop_node_buffer(struct backref_node *node) |
444 | { | |
445 | if (node->eb) { | |
3fd0a558 | 446 | unlock_node_buffer(node); |
5d4f98a2 YZ |
447 | free_extent_buffer(node->eb); |
448 | node->eb = NULL; | |
449 | } | |
450 | } | |
451 | ||
452 | static void drop_backref_node(struct backref_cache *tree, | |
453 | struct backref_node *node) | |
454 | { | |
5d4f98a2 YZ |
455 | BUG_ON(!list_empty(&node->upper)); |
456 | ||
457 | drop_node_buffer(node); | |
3fd0a558 | 458 | list_del(&node->list); |
5d4f98a2 | 459 | list_del(&node->lower); |
3fd0a558 YZ |
460 | if (!RB_EMPTY_NODE(&node->rb_node)) |
461 | rb_erase(&node->rb_node, &tree->rb_root); | |
462 | free_backref_node(tree, node); | |
5d4f98a2 YZ |
463 | } |
464 | ||
465 | /* | |
466 | * remove a backref node from the backref cache | |
467 | */ | |
468 | static void remove_backref_node(struct backref_cache *cache, | |
469 | struct backref_node *node) | |
470 | { | |
471 | struct backref_node *upper; | |
472 | struct backref_edge *edge; | |
473 | ||
474 | if (!node) | |
475 | return; | |
476 | ||
3fd0a558 | 477 | BUG_ON(!node->lowest && !node->detached); |
5d4f98a2 YZ |
478 | while (!list_empty(&node->upper)) { |
479 | edge = list_entry(node->upper.next, struct backref_edge, | |
480 | list[LOWER]); | |
481 | upper = edge->node[UPPER]; | |
482 | list_del(&edge->list[LOWER]); | |
483 | list_del(&edge->list[UPPER]); | |
3fd0a558 YZ |
484 | free_backref_edge(cache, edge); |
485 | ||
486 | if (RB_EMPTY_NODE(&upper->rb_node)) { | |
487 | BUG_ON(!list_empty(&node->upper)); | |
488 | drop_backref_node(cache, node); | |
489 | node = upper; | |
490 | node->lowest = 1; | |
491 | continue; | |
492 | } | |
5d4f98a2 | 493 | /* |
3fd0a558 | 494 | * add the node to leaf node list if no other |
5d4f98a2 YZ |
495 | * child block cached. |
496 | */ | |
497 | if (list_empty(&upper->lower)) { | |
3fd0a558 | 498 | list_add_tail(&upper->lower, &cache->leaves); |
5d4f98a2 YZ |
499 | upper->lowest = 1; |
500 | } | |
501 | } | |
3fd0a558 | 502 | |
5d4f98a2 YZ |
503 | drop_backref_node(cache, node); |
504 | } | |
505 | ||
3fd0a558 YZ |
506 | static void update_backref_node(struct backref_cache *cache, |
507 | struct backref_node *node, u64 bytenr) | |
508 | { | |
509 | struct rb_node *rb_node; | |
510 | rb_erase(&node->rb_node, &cache->rb_root); | |
511 | node->bytenr = bytenr; | |
512 | rb_node = tree_insert(&cache->rb_root, node->bytenr, &node->rb_node); | |
43c04fb1 JM |
513 | if (rb_node) |
514 | backref_tree_panic(rb_node, -EEXIST, bytenr); | |
3fd0a558 YZ |
515 | } |
516 | ||
517 | /* | |
518 | * update backref cache after a transaction commit | |
519 | */ | |
520 | static int update_backref_cache(struct btrfs_trans_handle *trans, | |
521 | struct backref_cache *cache) | |
522 | { | |
523 | struct backref_node *node; | |
524 | int level = 0; | |
525 | ||
526 | if (cache->last_trans == 0) { | |
527 | cache->last_trans = trans->transid; | |
528 | return 0; | |
529 | } | |
530 | ||
531 | if (cache->last_trans == trans->transid) | |
532 | return 0; | |
533 | ||
534 | /* | |
535 | * detached nodes are used to avoid unnecessary backref | |
536 | * lookup. transaction commit changes the extent tree. | |
537 | * so the detached nodes are no longer useful. | |
538 | */ | |
539 | while (!list_empty(&cache->detached)) { | |
540 | node = list_entry(cache->detached.next, | |
541 | struct backref_node, list); | |
542 | remove_backref_node(cache, node); | |
543 | } | |
544 | ||
545 | while (!list_empty(&cache->changed)) { | |
546 | node = list_entry(cache->changed.next, | |
547 | struct backref_node, list); | |
548 | list_del_init(&node->list); | |
549 | BUG_ON(node->pending); | |
550 | update_backref_node(cache, node, node->new_bytenr); | |
551 | } | |
552 | ||
553 | /* | |
554 | * some nodes can be left in the pending list if there were | |
555 | * errors during processing the pending nodes. | |
556 | */ | |
557 | for (level = 0; level < BTRFS_MAX_LEVEL; level++) { | |
558 | list_for_each_entry(node, &cache->pending[level], list) { | |
559 | BUG_ON(!node->pending); | |
560 | if (node->bytenr == node->new_bytenr) | |
561 | continue; | |
562 | update_backref_node(cache, node, node->new_bytenr); | |
563 | } | |
564 | } | |
565 | ||
566 | cache->last_trans = 0; | |
567 | return 1; | |
568 | } | |
569 | ||
6282675e QW |
570 | static bool reloc_root_is_dead(struct btrfs_root *root) |
571 | { | |
572 | /* | |
573 | * Pair with set_bit/clear_bit in clean_dirty_subvols and | |
574 | * btrfs_update_reloc_root. We need to see the updated bit before | |
575 | * trying to access reloc_root | |
576 | */ | |
577 | smp_rmb(); | |
578 | if (test_bit(BTRFS_ROOT_DEAD_RELOC_TREE, &root->state)) | |
579 | return true; | |
580 | return false; | |
581 | } | |
582 | ||
583 | /* | |
584 | * Check if this subvolume tree has valid reloc tree. | |
585 | * | |
586 | * Reloc tree after swap is considered dead, thus not considered as valid. | |
587 | * This is enough for most callers, as they don't distinguish dead reloc root | |
588 | * from no reloc root. But should_ignore_root() below is a special case. | |
589 | */ | |
590 | static bool have_reloc_root(struct btrfs_root *root) | |
591 | { | |
592 | if (reloc_root_is_dead(root)) | |
593 | return false; | |
594 | if (!root->reloc_root) | |
595 | return false; | |
596 | return true; | |
597 | } | |
f2a97a9d | 598 | |
3fd0a558 YZ |
599 | static int should_ignore_root(struct btrfs_root *root) |
600 | { | |
601 | struct btrfs_root *reloc_root; | |
602 | ||
27cdeb70 | 603 | if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state)) |
3fd0a558 YZ |
604 | return 0; |
605 | ||
6282675e QW |
606 | /* This root has been merged with its reloc tree, we can ignore it */ |
607 | if (reloc_root_is_dead(root)) | |
608 | return 1; | |
609 | ||
3fd0a558 YZ |
610 | reloc_root = root->reloc_root; |
611 | if (!reloc_root) | |
612 | return 0; | |
613 | ||
4d4225fc JB |
614 | if (btrfs_header_generation(reloc_root->commit_root) == |
615 | root->fs_info->running_transaction->transid) | |
3fd0a558 YZ |
616 | return 0; |
617 | /* | |
618 | * if there is reloc tree and it was created in previous | |
619 | * transaction backref lookup can find the reloc tree, | |
620 | * so backref node for the fs tree root is useless for | |
621 | * relocation. | |
622 | */ | |
623 | return 1; | |
624 | } | |
5d4f98a2 YZ |
625 | /* |
626 | * find reloc tree by address of tree root | |
627 | */ | |
628 | static struct btrfs_root *find_reloc_root(struct reloc_control *rc, | |
629 | u64 bytenr) | |
630 | { | |
631 | struct rb_node *rb_node; | |
632 | struct mapping_node *node; | |
633 | struct btrfs_root *root = NULL; | |
634 | ||
635 | spin_lock(&rc->reloc_root_tree.lock); | |
636 | rb_node = tree_search(&rc->reloc_root_tree.rb_root, bytenr); | |
637 | if (rb_node) { | |
638 | node = rb_entry(rb_node, struct mapping_node, rb_node); | |
639 | root = (struct btrfs_root *)node->data; | |
640 | } | |
641 | spin_unlock(&rc->reloc_root_tree.lock); | |
00246528 | 642 | return btrfs_grab_root(root); |
5d4f98a2 YZ |
643 | } |
644 | ||
5d4f98a2 YZ |
645 | static struct btrfs_root *read_fs_root(struct btrfs_fs_info *fs_info, |
646 | u64 root_objectid) | |
647 | { | |
648 | struct btrfs_key key; | |
649 | ||
650 | key.objectid = root_objectid; | |
651 | key.type = BTRFS_ROOT_ITEM_KEY; | |
e1922118 | 652 | key.offset = (u64)-1; |
5d4f98a2 | 653 | |
bc44d7c4 | 654 | return btrfs_get_fs_root(fs_info, &key, false); |
5d4f98a2 YZ |
655 | } |
656 | ||
5d4f98a2 YZ |
657 | static noinline_for_stack |
658 | int find_inline_backref(struct extent_buffer *leaf, int slot, | |
659 | unsigned long *ptr, unsigned long *end) | |
660 | { | |
3173a18f | 661 | struct btrfs_key key; |
5d4f98a2 YZ |
662 | struct btrfs_extent_item *ei; |
663 | struct btrfs_tree_block_info *bi; | |
664 | u32 item_size; | |
665 | ||
3173a18f JB |
666 | btrfs_item_key_to_cpu(leaf, &key, slot); |
667 | ||
5d4f98a2 | 668 | item_size = btrfs_item_size_nr(leaf, slot); |
ba3c2b19 NB |
669 | if (item_size < sizeof(*ei)) { |
670 | btrfs_print_v0_err(leaf->fs_info); | |
671 | btrfs_handle_fs_error(leaf->fs_info, -EINVAL, NULL); | |
672 | return 1; | |
673 | } | |
5d4f98a2 YZ |
674 | ei = btrfs_item_ptr(leaf, slot, struct btrfs_extent_item); |
675 | WARN_ON(!(btrfs_extent_flags(leaf, ei) & | |
676 | BTRFS_EXTENT_FLAG_TREE_BLOCK)); | |
677 | ||
3173a18f JB |
678 | if (key.type == BTRFS_EXTENT_ITEM_KEY && |
679 | item_size <= sizeof(*ei) + sizeof(*bi)) { | |
5d4f98a2 YZ |
680 | WARN_ON(item_size < sizeof(*ei) + sizeof(*bi)); |
681 | return 1; | |
682 | } | |
d062d13c JB |
683 | if (key.type == BTRFS_METADATA_ITEM_KEY && |
684 | item_size <= sizeof(*ei)) { | |
685 | WARN_ON(item_size < sizeof(*ei)); | |
686 | return 1; | |
687 | } | |
5d4f98a2 | 688 | |
3173a18f JB |
689 | if (key.type == BTRFS_EXTENT_ITEM_KEY) { |
690 | bi = (struct btrfs_tree_block_info *)(ei + 1); | |
691 | *ptr = (unsigned long)(bi + 1); | |
692 | } else { | |
693 | *ptr = (unsigned long)(ei + 1); | |
694 | } | |
5d4f98a2 YZ |
695 | *end = (unsigned long)ei + item_size; |
696 | return 0; | |
697 | } | |
698 | ||
699 | /* | |
700 | * build backref tree for a given tree block. root of the backref tree | |
701 | * corresponds the tree block, leaves of the backref tree correspond | |
702 | * roots of b-trees that reference the tree block. | |
703 | * | |
704 | * the basic idea of this function is check backrefs of a given block | |
01327610 NS |
705 | * to find upper level blocks that reference the block, and then check |
706 | * backrefs of these upper level blocks recursively. the recursion stop | |
5d4f98a2 YZ |
707 | * when tree root is reached or backrefs for the block is cached. |
708 | * | |
709 | * NOTE: if we find backrefs for a block are cached, we know backrefs | |
710 | * for all upper level blocks that directly/indirectly reference the | |
711 | * block are also cached. | |
712 | */ | |
3fd0a558 YZ |
713 | static noinline_for_stack |
714 | struct backref_node *build_backref_tree(struct reloc_control *rc, | |
715 | struct btrfs_key *node_key, | |
716 | int level, u64 bytenr) | |
5d4f98a2 | 717 | { |
3fd0a558 | 718 | struct backref_cache *cache = &rc->backref_cache; |
fa6ac715 QW |
719 | struct btrfs_path *path1; /* For searching extent root */ |
720 | struct btrfs_path *path2; /* For searching parent of TREE_BLOCK_REF */ | |
5d4f98a2 YZ |
721 | struct extent_buffer *eb; |
722 | struct btrfs_root *root; | |
723 | struct backref_node *cur; | |
724 | struct backref_node *upper; | |
725 | struct backref_node *lower; | |
726 | struct backref_node *node = NULL; | |
727 | struct backref_node *exist = NULL; | |
728 | struct backref_edge *edge; | |
729 | struct rb_node *rb_node; | |
730 | struct btrfs_key key; | |
731 | unsigned long end; | |
732 | unsigned long ptr; | |
fa6ac715 | 733 | LIST_HEAD(list); /* Pending edge list, upper node needs to be checked */ |
3fd0a558 YZ |
734 | LIST_HEAD(useless); |
735 | int cowonly; | |
5d4f98a2 YZ |
736 | int ret; |
737 | int err = 0; | |
b6c60c80 | 738 | bool need_check = true; |
5d4f98a2 YZ |
739 | |
740 | path1 = btrfs_alloc_path(); | |
741 | path2 = btrfs_alloc_path(); | |
742 | if (!path1 || !path2) { | |
743 | err = -ENOMEM; | |
744 | goto out; | |
745 | } | |
746 | ||
3fd0a558 | 747 | node = alloc_backref_node(cache); |
5d4f98a2 YZ |
748 | if (!node) { |
749 | err = -ENOMEM; | |
750 | goto out; | |
751 | } | |
752 | ||
5d4f98a2 | 753 | node->bytenr = bytenr; |
5d4f98a2 YZ |
754 | node->level = level; |
755 | node->lowest = 1; | |
756 | cur = node; | |
757 | again: | |
758 | end = 0; | |
759 | ptr = 0; | |
760 | key.objectid = cur->bytenr; | |
3173a18f | 761 | key.type = BTRFS_METADATA_ITEM_KEY; |
5d4f98a2 YZ |
762 | key.offset = (u64)-1; |
763 | ||
764 | path1->search_commit_root = 1; | |
765 | path1->skip_locking = 1; | |
766 | ret = btrfs_search_slot(NULL, rc->extent_root, &key, path1, | |
767 | 0, 0); | |
768 | if (ret < 0) { | |
769 | err = ret; | |
770 | goto out; | |
771 | } | |
75bfb9af JB |
772 | ASSERT(ret); |
773 | ASSERT(path1->slots[0]); | |
5d4f98a2 YZ |
774 | |
775 | path1->slots[0]--; | |
776 | ||
777 | WARN_ON(cur->checked); | |
778 | if (!list_empty(&cur->upper)) { | |
779 | /* | |
70f23fd6 | 780 | * the backref was added previously when processing |
5d4f98a2 YZ |
781 | * backref of type BTRFS_TREE_BLOCK_REF_KEY |
782 | */ | |
75bfb9af | 783 | ASSERT(list_is_singular(&cur->upper)); |
5d4f98a2 YZ |
784 | edge = list_entry(cur->upper.next, struct backref_edge, |
785 | list[LOWER]); | |
75bfb9af | 786 | ASSERT(list_empty(&edge->list[UPPER])); |
5d4f98a2 YZ |
787 | exist = edge->node[UPPER]; |
788 | /* | |
789 | * add the upper level block to pending list if we need | |
790 | * check its backrefs | |
791 | */ | |
792 | if (!exist->checked) | |
793 | list_add_tail(&edge->list[UPPER], &list); | |
794 | } else { | |
795 | exist = NULL; | |
796 | } | |
797 | ||
798 | while (1) { | |
799 | cond_resched(); | |
800 | eb = path1->nodes[0]; | |
801 | ||
802 | if (ptr >= end) { | |
803 | if (path1->slots[0] >= btrfs_header_nritems(eb)) { | |
804 | ret = btrfs_next_leaf(rc->extent_root, path1); | |
805 | if (ret < 0) { | |
806 | err = ret; | |
807 | goto out; | |
808 | } | |
809 | if (ret > 0) | |
810 | break; | |
811 | eb = path1->nodes[0]; | |
812 | } | |
813 | ||
814 | btrfs_item_key_to_cpu(eb, &key, path1->slots[0]); | |
815 | if (key.objectid != cur->bytenr) { | |
816 | WARN_ON(exist); | |
817 | break; | |
818 | } | |
819 | ||
3173a18f JB |
820 | if (key.type == BTRFS_EXTENT_ITEM_KEY || |
821 | key.type == BTRFS_METADATA_ITEM_KEY) { | |
5d4f98a2 YZ |
822 | ret = find_inline_backref(eb, path1->slots[0], |
823 | &ptr, &end); | |
824 | if (ret) | |
825 | goto next; | |
826 | } | |
827 | } | |
828 | ||
829 | if (ptr < end) { | |
830 | /* update key for inline back ref */ | |
831 | struct btrfs_extent_inline_ref *iref; | |
3de28d57 | 832 | int type; |
5d4f98a2 | 833 | iref = (struct btrfs_extent_inline_ref *)ptr; |
3de28d57 LB |
834 | type = btrfs_get_extent_inline_ref_type(eb, iref, |
835 | BTRFS_REF_TYPE_BLOCK); | |
836 | if (type == BTRFS_REF_TYPE_INVALID) { | |
af431dcb | 837 | err = -EUCLEAN; |
3de28d57 LB |
838 | goto out; |
839 | } | |
840 | key.type = type; | |
5d4f98a2 | 841 | key.offset = btrfs_extent_inline_ref_offset(eb, iref); |
3de28d57 | 842 | |
5d4f98a2 YZ |
843 | WARN_ON(key.type != BTRFS_TREE_BLOCK_REF_KEY && |
844 | key.type != BTRFS_SHARED_BLOCK_REF_KEY); | |
845 | } | |
846 | ||
fa6ac715 QW |
847 | /* |
848 | * Parent node found and matches current inline ref, no need to | |
849 | * rebuild this node for this inline ref. | |
850 | */ | |
5d4f98a2 YZ |
851 | if (exist && |
852 | ((key.type == BTRFS_TREE_BLOCK_REF_KEY && | |
853 | exist->owner == key.offset) || | |
854 | (key.type == BTRFS_SHARED_BLOCK_REF_KEY && | |
855 | exist->bytenr == key.offset))) { | |
856 | exist = NULL; | |
857 | goto next; | |
858 | } | |
859 | ||
fa6ac715 | 860 | /* SHARED_BLOCK_REF means key.offset is the parent bytenr */ |
5d4f98a2 | 861 | if (key.type == BTRFS_SHARED_BLOCK_REF_KEY) { |
5d4f98a2 YZ |
862 | if (key.objectid == key.offset) { |
863 | /* | |
fa6ac715 QW |
864 | * Only root blocks of reloc trees use backref |
865 | * pointing to itself. | |
5d4f98a2 YZ |
866 | */ |
867 | root = find_reloc_root(rc, cur->bytenr); | |
75bfb9af | 868 | ASSERT(root); |
5d4f98a2 YZ |
869 | cur->root = root; |
870 | break; | |
871 | } | |
872 | ||
3fd0a558 | 873 | edge = alloc_backref_edge(cache); |
5d4f98a2 YZ |
874 | if (!edge) { |
875 | err = -ENOMEM; | |
876 | goto out; | |
877 | } | |
878 | rb_node = tree_search(&cache->rb_root, key.offset); | |
879 | if (!rb_node) { | |
3fd0a558 | 880 | upper = alloc_backref_node(cache); |
5d4f98a2 | 881 | if (!upper) { |
3fd0a558 | 882 | free_backref_edge(cache, edge); |
5d4f98a2 YZ |
883 | err = -ENOMEM; |
884 | goto out; | |
885 | } | |
5d4f98a2 | 886 | upper->bytenr = key.offset; |
5d4f98a2 YZ |
887 | upper->level = cur->level + 1; |
888 | /* | |
889 | * backrefs for the upper level block isn't | |
890 | * cached, add the block to pending list | |
891 | */ | |
892 | list_add_tail(&edge->list[UPPER], &list); | |
893 | } else { | |
894 | upper = rb_entry(rb_node, struct backref_node, | |
895 | rb_node); | |
75bfb9af | 896 | ASSERT(upper->checked); |
5d4f98a2 YZ |
897 | INIT_LIST_HEAD(&edge->list[UPPER]); |
898 | } | |
3fd0a558 | 899 | list_add_tail(&edge->list[LOWER], &cur->upper); |
5d4f98a2 | 900 | edge->node[LOWER] = cur; |
3fd0a558 | 901 | edge->node[UPPER] = upper; |
5d4f98a2 YZ |
902 | |
903 | goto next; | |
6d8ff4e4 | 904 | } else if (unlikely(key.type == BTRFS_EXTENT_REF_V0_KEY)) { |
ba3c2b19 NB |
905 | err = -EINVAL; |
906 | btrfs_print_v0_err(rc->extent_root->fs_info); | |
907 | btrfs_handle_fs_error(rc->extent_root->fs_info, err, | |
908 | NULL); | |
909 | goto out; | |
5d4f98a2 YZ |
910 | } else if (key.type != BTRFS_TREE_BLOCK_REF_KEY) { |
911 | goto next; | |
912 | } | |
913 | ||
fa6ac715 QW |
914 | /* |
915 | * key.type == BTRFS_TREE_BLOCK_REF_KEY, inline ref offset | |
916 | * means the root objectid. We need to search the tree to get | |
917 | * its parent bytenr. | |
918 | */ | |
5d4f98a2 YZ |
919 | root = read_fs_root(rc->extent_root->fs_info, key.offset); |
920 | if (IS_ERR(root)) { | |
921 | err = PTR_ERR(root); | |
922 | goto out; | |
923 | } | |
924 | ||
27cdeb70 | 925 | if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state)) |
3fd0a558 YZ |
926 | cur->cowonly = 1; |
927 | ||
5d4f98a2 YZ |
928 | if (btrfs_root_level(&root->root_item) == cur->level) { |
929 | /* tree root */ | |
75bfb9af | 930 | ASSERT(btrfs_root_bytenr(&root->root_item) == |
5d4f98a2 | 931 | cur->bytenr); |
0b530bc5 | 932 | if (should_ignore_root(root)) { |
00246528 | 933 | btrfs_put_root(root); |
3fd0a558 | 934 | list_add(&cur->list, &useless); |
0b530bc5 | 935 | } else { |
3fd0a558 | 936 | cur->root = root; |
0b530bc5 | 937 | } |
5d4f98a2 YZ |
938 | break; |
939 | } | |
940 | ||
941 | level = cur->level + 1; | |
942 | ||
fa6ac715 | 943 | /* Search the tree to find parent blocks referring the block. */ |
5d4f98a2 YZ |
944 | path2->search_commit_root = 1; |
945 | path2->skip_locking = 1; | |
946 | path2->lowest_level = level; | |
947 | ret = btrfs_search_slot(NULL, root, node_key, path2, 0, 0); | |
948 | path2->lowest_level = 0; | |
949 | if (ret < 0) { | |
00246528 | 950 | btrfs_put_root(root); |
5d4f98a2 YZ |
951 | err = ret; |
952 | goto out; | |
953 | } | |
33c66f43 YZ |
954 | if (ret > 0 && path2->slots[level] > 0) |
955 | path2->slots[level]--; | |
5d4f98a2 YZ |
956 | |
957 | eb = path2->nodes[level]; | |
3561b9db LB |
958 | if (btrfs_node_blockptr(eb, path2->slots[level]) != |
959 | cur->bytenr) { | |
960 | btrfs_err(root->fs_info, | |
961 | "couldn't find block (%llu) (level %d) in tree (%llu) with key (%llu %u %llu)", | |
4fd786e6 MT |
962 | cur->bytenr, level - 1, |
963 | root->root_key.objectid, | |
3561b9db LB |
964 | node_key->objectid, node_key->type, |
965 | node_key->offset); | |
00246528 | 966 | btrfs_put_root(root); |
3561b9db LB |
967 | err = -ENOENT; |
968 | goto out; | |
969 | } | |
5d4f98a2 | 970 | lower = cur; |
b6c60c80 | 971 | need_check = true; |
fa6ac715 QW |
972 | |
973 | /* Add all nodes and edges in the path */ | |
5d4f98a2 YZ |
974 | for (; level < BTRFS_MAX_LEVEL; level++) { |
975 | if (!path2->nodes[level]) { | |
75bfb9af | 976 | ASSERT(btrfs_root_bytenr(&root->root_item) == |
5d4f98a2 | 977 | lower->bytenr); |
0b530bc5 | 978 | if (should_ignore_root(root)) { |
00246528 | 979 | btrfs_put_root(root); |
3fd0a558 | 980 | list_add(&lower->list, &useless); |
0b530bc5 | 981 | } else { |
3fd0a558 | 982 | lower->root = root; |
0b530bc5 | 983 | } |
5d4f98a2 YZ |
984 | break; |
985 | } | |
986 | ||
3fd0a558 | 987 | edge = alloc_backref_edge(cache); |
5d4f98a2 | 988 | if (!edge) { |
00246528 | 989 | btrfs_put_root(root); |
5d4f98a2 YZ |
990 | err = -ENOMEM; |
991 | goto out; | |
992 | } | |
993 | ||
994 | eb = path2->nodes[level]; | |
995 | rb_node = tree_search(&cache->rb_root, eb->start); | |
996 | if (!rb_node) { | |
3fd0a558 | 997 | upper = alloc_backref_node(cache); |
5d4f98a2 | 998 | if (!upper) { |
00246528 | 999 | btrfs_put_root(root); |
3fd0a558 | 1000 | free_backref_edge(cache, edge); |
5d4f98a2 YZ |
1001 | err = -ENOMEM; |
1002 | goto out; | |
1003 | } | |
5d4f98a2 YZ |
1004 | upper->bytenr = eb->start; |
1005 | upper->owner = btrfs_header_owner(eb); | |
1006 | upper->level = lower->level + 1; | |
27cdeb70 MX |
1007 | if (!test_bit(BTRFS_ROOT_REF_COWS, |
1008 | &root->state)) | |
3fd0a558 | 1009 | upper->cowonly = 1; |
5d4f98a2 YZ |
1010 | |
1011 | /* | |
1012 | * if we know the block isn't shared | |
1013 | * we can void checking its backrefs. | |
1014 | */ | |
1015 | if (btrfs_block_can_be_shared(root, eb)) | |
1016 | upper->checked = 0; | |
1017 | else | |
1018 | upper->checked = 1; | |
1019 | ||
1020 | /* | |
1021 | * add the block to pending list if we | |
b6c60c80 JB |
1022 | * need check its backrefs, we only do this once |
1023 | * while walking up a tree as we will catch | |
1024 | * anything else later on. | |
5d4f98a2 | 1025 | */ |
b6c60c80 JB |
1026 | if (!upper->checked && need_check) { |
1027 | need_check = false; | |
5d4f98a2 YZ |
1028 | list_add_tail(&edge->list[UPPER], |
1029 | &list); | |
bbe90514 JB |
1030 | } else { |
1031 | if (upper->checked) | |
1032 | need_check = true; | |
5d4f98a2 | 1033 | INIT_LIST_HEAD(&edge->list[UPPER]); |
bbe90514 | 1034 | } |
5d4f98a2 YZ |
1035 | } else { |
1036 | upper = rb_entry(rb_node, struct backref_node, | |
1037 | rb_node); | |
75bfb9af | 1038 | ASSERT(upper->checked); |
5d4f98a2 | 1039 | INIT_LIST_HEAD(&edge->list[UPPER]); |
3fd0a558 YZ |
1040 | if (!upper->owner) |
1041 | upper->owner = btrfs_header_owner(eb); | |
5d4f98a2 YZ |
1042 | } |
1043 | list_add_tail(&edge->list[LOWER], &lower->upper); | |
5d4f98a2 | 1044 | edge->node[LOWER] = lower; |
3fd0a558 | 1045 | edge->node[UPPER] = upper; |
5d4f98a2 | 1046 | |
0b530bc5 | 1047 | if (rb_node) { |
00246528 | 1048 | btrfs_put_root(root); |
5d4f98a2 | 1049 | break; |
0b530bc5 | 1050 | } |
5d4f98a2 YZ |
1051 | lower = upper; |
1052 | upper = NULL; | |
1053 | } | |
b3b4aa74 | 1054 | btrfs_release_path(path2); |
5d4f98a2 YZ |
1055 | next: |
1056 | if (ptr < end) { | |
1057 | ptr += btrfs_extent_inline_ref_size(key.type); | |
1058 | if (ptr >= end) { | |
1059 | WARN_ON(ptr > end); | |
1060 | ptr = 0; | |
1061 | end = 0; | |
1062 | } | |
1063 | } | |
1064 | if (ptr >= end) | |
1065 | path1->slots[0]++; | |
1066 | } | |
b3b4aa74 | 1067 | btrfs_release_path(path1); |
5d4f98a2 YZ |
1068 | |
1069 | cur->checked = 1; | |
1070 | WARN_ON(exist); | |
1071 | ||
1072 | /* the pending list isn't empty, take the first block to process */ | |
1073 | if (!list_empty(&list)) { | |
1074 | edge = list_entry(list.next, struct backref_edge, list[UPPER]); | |
1075 | list_del_init(&edge->list[UPPER]); | |
1076 | cur = edge->node[UPPER]; | |
1077 | goto again; | |
1078 | } | |
1079 | ||
1080 | /* | |
1081 | * everything goes well, connect backref nodes and insert backref nodes | |
1082 | * into the cache. | |
1083 | */ | |
75bfb9af | 1084 | ASSERT(node->checked); |
3fd0a558 YZ |
1085 | cowonly = node->cowonly; |
1086 | if (!cowonly) { | |
1087 | rb_node = tree_insert(&cache->rb_root, node->bytenr, | |
1088 | &node->rb_node); | |
43c04fb1 JM |
1089 | if (rb_node) |
1090 | backref_tree_panic(rb_node, -EEXIST, node->bytenr); | |
3fd0a558 YZ |
1091 | list_add_tail(&node->lower, &cache->leaves); |
1092 | } | |
5d4f98a2 YZ |
1093 | |
1094 | list_for_each_entry(edge, &node->upper, list[LOWER]) | |
1095 | list_add_tail(&edge->list[UPPER], &list); | |
1096 | ||
1097 | while (!list_empty(&list)) { | |
1098 | edge = list_entry(list.next, struct backref_edge, list[UPPER]); | |
1099 | list_del_init(&edge->list[UPPER]); | |
1100 | upper = edge->node[UPPER]; | |
3fd0a558 YZ |
1101 | if (upper->detached) { |
1102 | list_del(&edge->list[LOWER]); | |
1103 | lower = edge->node[LOWER]; | |
1104 | free_backref_edge(cache, edge); | |
1105 | if (list_empty(&lower->upper)) | |
1106 | list_add(&lower->list, &useless); | |
1107 | continue; | |
1108 | } | |
5d4f98a2 YZ |
1109 | |
1110 | if (!RB_EMPTY_NODE(&upper->rb_node)) { | |
1111 | if (upper->lowest) { | |
1112 | list_del_init(&upper->lower); | |
1113 | upper->lowest = 0; | |
1114 | } | |
1115 | ||
1116 | list_add_tail(&edge->list[UPPER], &upper->lower); | |
1117 | continue; | |
1118 | } | |
1119 | ||
75bfb9af JB |
1120 | if (!upper->checked) { |
1121 | /* | |
1122 | * Still want to blow up for developers since this is a | |
1123 | * logic bug. | |
1124 | */ | |
1125 | ASSERT(0); | |
1126 | err = -EINVAL; | |
1127 | goto out; | |
1128 | } | |
1129 | if (cowonly != upper->cowonly) { | |
1130 | ASSERT(0); | |
1131 | err = -EINVAL; | |
1132 | goto out; | |
1133 | } | |
1134 | ||
3fd0a558 YZ |
1135 | if (!cowonly) { |
1136 | rb_node = tree_insert(&cache->rb_root, upper->bytenr, | |
1137 | &upper->rb_node); | |
43c04fb1 JM |
1138 | if (rb_node) |
1139 | backref_tree_panic(rb_node, -EEXIST, | |
1140 | upper->bytenr); | |
3fd0a558 | 1141 | } |
5d4f98a2 YZ |
1142 | |
1143 | list_add_tail(&edge->list[UPPER], &upper->lower); | |
1144 | ||
1145 | list_for_each_entry(edge, &upper->upper, list[LOWER]) | |
1146 | list_add_tail(&edge->list[UPPER], &list); | |
1147 | } | |
3fd0a558 YZ |
1148 | /* |
1149 | * process useless backref nodes. backref nodes for tree leaves | |
1150 | * are deleted from the cache. backref nodes for upper level | |
1151 | * tree blocks are left in the cache to avoid unnecessary backref | |
1152 | * lookup. | |
1153 | */ | |
1154 | while (!list_empty(&useless)) { | |
1155 | upper = list_entry(useless.next, struct backref_node, list); | |
1156 | list_del_init(&upper->list); | |
75bfb9af | 1157 | ASSERT(list_empty(&upper->upper)); |
3fd0a558 YZ |
1158 | if (upper == node) |
1159 | node = NULL; | |
1160 | if (upper->lowest) { | |
1161 | list_del_init(&upper->lower); | |
1162 | upper->lowest = 0; | |
1163 | } | |
1164 | while (!list_empty(&upper->lower)) { | |
1165 | edge = list_entry(upper->lower.next, | |
1166 | struct backref_edge, list[UPPER]); | |
1167 | list_del(&edge->list[UPPER]); | |
1168 | list_del(&edge->list[LOWER]); | |
1169 | lower = edge->node[LOWER]; | |
1170 | free_backref_edge(cache, edge); | |
1171 | ||
1172 | if (list_empty(&lower->upper)) | |
1173 | list_add(&lower->list, &useless); | |
1174 | } | |
1175 | __mark_block_processed(rc, upper); | |
1176 | if (upper->level > 0) { | |
1177 | list_add(&upper->list, &cache->detached); | |
1178 | upper->detached = 1; | |
1179 | } else { | |
1180 | rb_erase(&upper->rb_node, &cache->rb_root); | |
1181 | free_backref_node(cache, upper); | |
1182 | } | |
1183 | } | |
5d4f98a2 YZ |
1184 | out: |
1185 | btrfs_free_path(path1); | |
1186 | btrfs_free_path(path2); | |
1187 | if (err) { | |
3fd0a558 YZ |
1188 | while (!list_empty(&useless)) { |
1189 | lower = list_entry(useless.next, | |
75bfb9af JB |
1190 | struct backref_node, list); |
1191 | list_del_init(&lower->list); | |
3fd0a558 | 1192 | } |
75bfb9af JB |
1193 | while (!list_empty(&list)) { |
1194 | edge = list_first_entry(&list, struct backref_edge, | |
1195 | list[UPPER]); | |
1196 | list_del(&edge->list[UPPER]); | |
3fd0a558 | 1197 | list_del(&edge->list[LOWER]); |
75bfb9af | 1198 | lower = edge->node[LOWER]; |
5d4f98a2 | 1199 | upper = edge->node[UPPER]; |
3fd0a558 | 1200 | free_backref_edge(cache, edge); |
75bfb9af JB |
1201 | |
1202 | /* | |
1203 | * Lower is no longer linked to any upper backref nodes | |
1204 | * and isn't in the cache, we can free it ourselves. | |
1205 | */ | |
1206 | if (list_empty(&lower->upper) && | |
1207 | RB_EMPTY_NODE(&lower->rb_node)) | |
1208 | list_add(&lower->list, &useless); | |
1209 | ||
1210 | if (!RB_EMPTY_NODE(&upper->rb_node)) | |
1211 | continue; | |
1212 | ||
01327610 | 1213 | /* Add this guy's upper edges to the list to process */ |
75bfb9af JB |
1214 | list_for_each_entry(edge, &upper->upper, list[LOWER]) |
1215 | list_add_tail(&edge->list[UPPER], &list); | |
1216 | if (list_empty(&upper->upper)) | |
1217 | list_add(&upper->list, &useless); | |
1218 | } | |
1219 | ||
1220 | while (!list_empty(&useless)) { | |
1221 | lower = list_entry(useless.next, | |
1222 | struct backref_node, list); | |
1223 | list_del_init(&lower->list); | |
0fd8c3da LB |
1224 | if (lower == node) |
1225 | node = NULL; | |
75bfb9af | 1226 | free_backref_node(cache, lower); |
5d4f98a2 | 1227 | } |
0fd8c3da | 1228 | |
8e19c973 | 1229 | remove_backref_node(cache, node); |
5d4f98a2 YZ |
1230 | return ERR_PTR(err); |
1231 | } | |
75bfb9af | 1232 | ASSERT(!node || !node->detached); |
5d4f98a2 YZ |
1233 | return node; |
1234 | } | |
1235 | ||
3fd0a558 YZ |
1236 | /* |
1237 | * helper to add backref node for the newly created snapshot. | |
1238 | * the backref node is created by cloning backref node that | |
1239 | * corresponds to root of source tree | |
1240 | */ | |
1241 | static int clone_backref_node(struct btrfs_trans_handle *trans, | |
1242 | struct reloc_control *rc, | |
1243 | struct btrfs_root *src, | |
1244 | struct btrfs_root *dest) | |
1245 | { | |
1246 | struct btrfs_root *reloc_root = src->reloc_root; | |
1247 | struct backref_cache *cache = &rc->backref_cache; | |
1248 | struct backref_node *node = NULL; | |
1249 | struct backref_node *new_node; | |
1250 | struct backref_edge *edge; | |
1251 | struct backref_edge *new_edge; | |
1252 | struct rb_node *rb_node; | |
1253 | ||
1254 | if (cache->last_trans > 0) | |
1255 | update_backref_cache(trans, cache); | |
1256 | ||
1257 | rb_node = tree_search(&cache->rb_root, src->commit_root->start); | |
1258 | if (rb_node) { | |
1259 | node = rb_entry(rb_node, struct backref_node, rb_node); | |
1260 | if (node->detached) | |
1261 | node = NULL; | |
1262 | else | |
1263 | BUG_ON(node->new_bytenr != reloc_root->node->start); | |
1264 | } | |
1265 | ||
1266 | if (!node) { | |
1267 | rb_node = tree_search(&cache->rb_root, | |
1268 | reloc_root->commit_root->start); | |
1269 | if (rb_node) { | |
1270 | node = rb_entry(rb_node, struct backref_node, | |
1271 | rb_node); | |
1272 | BUG_ON(node->detached); | |
1273 | } | |
1274 | } | |
1275 | ||
1276 | if (!node) | |
1277 | return 0; | |
1278 | ||
1279 | new_node = alloc_backref_node(cache); | |
1280 | if (!new_node) | |
1281 | return -ENOMEM; | |
1282 | ||
1283 | new_node->bytenr = dest->node->start; | |
1284 | new_node->level = node->level; | |
1285 | new_node->lowest = node->lowest; | |
6848ad64 | 1286 | new_node->checked = 1; |
00246528 | 1287 | new_node->root = btrfs_grab_root(dest); |
0b530bc5 | 1288 | ASSERT(new_node->root); |
3fd0a558 YZ |
1289 | |
1290 | if (!node->lowest) { | |
1291 | list_for_each_entry(edge, &node->lower, list[UPPER]) { | |
1292 | new_edge = alloc_backref_edge(cache); | |
1293 | if (!new_edge) | |
1294 | goto fail; | |
1295 | ||
1296 | new_edge->node[UPPER] = new_node; | |
1297 | new_edge->node[LOWER] = edge->node[LOWER]; | |
1298 | list_add_tail(&new_edge->list[UPPER], | |
1299 | &new_node->lower); | |
1300 | } | |
76b9e23d MX |
1301 | } else { |
1302 | list_add_tail(&new_node->lower, &cache->leaves); | |
3fd0a558 YZ |
1303 | } |
1304 | ||
1305 | rb_node = tree_insert(&cache->rb_root, new_node->bytenr, | |
1306 | &new_node->rb_node); | |
43c04fb1 JM |
1307 | if (rb_node) |
1308 | backref_tree_panic(rb_node, -EEXIST, new_node->bytenr); | |
3fd0a558 YZ |
1309 | |
1310 | if (!new_node->lowest) { | |
1311 | list_for_each_entry(new_edge, &new_node->lower, list[UPPER]) { | |
1312 | list_add_tail(&new_edge->list[LOWER], | |
1313 | &new_edge->node[LOWER]->upper); | |
1314 | } | |
1315 | } | |
1316 | return 0; | |
1317 | fail: | |
1318 | while (!list_empty(&new_node->lower)) { | |
1319 | new_edge = list_entry(new_node->lower.next, | |
1320 | struct backref_edge, list[UPPER]); | |
1321 | list_del(&new_edge->list[UPPER]); | |
1322 | free_backref_edge(cache, new_edge); | |
1323 | } | |
1324 | free_backref_node(cache, new_node); | |
1325 | return -ENOMEM; | |
1326 | } | |
1327 | ||
5d4f98a2 YZ |
1328 | /* |
1329 | * helper to add 'address of tree root -> reloc tree' mapping | |
1330 | */ | |
ffd7b339 | 1331 | static int __must_check __add_reloc_root(struct btrfs_root *root) |
5d4f98a2 | 1332 | { |
0b246afa | 1333 | struct btrfs_fs_info *fs_info = root->fs_info; |
5d4f98a2 YZ |
1334 | struct rb_node *rb_node; |
1335 | struct mapping_node *node; | |
0b246afa | 1336 | struct reloc_control *rc = fs_info->reloc_ctl; |
5d4f98a2 YZ |
1337 | |
1338 | node = kmalloc(sizeof(*node), GFP_NOFS); | |
ffd7b339 JM |
1339 | if (!node) |
1340 | return -ENOMEM; | |
5d4f98a2 | 1341 | |
ea287ab1 | 1342 | node->bytenr = root->commit_root->start; |
5d4f98a2 YZ |
1343 | node->data = root; |
1344 | ||
1345 | spin_lock(&rc->reloc_root_tree.lock); | |
1346 | rb_node = tree_insert(&rc->reloc_root_tree.rb_root, | |
1347 | node->bytenr, &node->rb_node); | |
1348 | spin_unlock(&rc->reloc_root_tree.lock); | |
ffd7b339 | 1349 | if (rb_node) { |
0b246afa | 1350 | btrfs_panic(fs_info, -EEXIST, |
5d163e0e JM |
1351 | "Duplicate root found for start=%llu while inserting into relocation tree", |
1352 | node->bytenr); | |
ffd7b339 | 1353 | } |
5d4f98a2 YZ |
1354 | |
1355 | list_add_tail(&root->root_list, &rc->reloc_roots); | |
1356 | return 0; | |
1357 | } | |
1358 | ||
1359 | /* | |
c974c464 | 1360 | * helper to delete the 'address of tree root -> reloc tree' |
5d4f98a2 YZ |
1361 | * mapping |
1362 | */ | |
c974c464 | 1363 | static void __del_reloc_root(struct btrfs_root *root) |
5d4f98a2 | 1364 | { |
0b246afa | 1365 | struct btrfs_fs_info *fs_info = root->fs_info; |
5d4f98a2 YZ |
1366 | struct rb_node *rb_node; |
1367 | struct mapping_node *node = NULL; | |
0b246afa | 1368 | struct reloc_control *rc = fs_info->reloc_ctl; |
f44deb74 | 1369 | bool put_ref = false; |
5d4f98a2 | 1370 | |
65c6e82b | 1371 | if (rc && root->node) { |
389305b2 QW |
1372 | spin_lock(&rc->reloc_root_tree.lock); |
1373 | rb_node = tree_search(&rc->reloc_root_tree.rb_root, | |
ea287ab1 | 1374 | root->commit_root->start); |
389305b2 QW |
1375 | if (rb_node) { |
1376 | node = rb_entry(rb_node, struct mapping_node, rb_node); | |
1377 | rb_erase(&node->rb_node, &rc->reloc_root_tree.rb_root); | |
ea287ab1 | 1378 | RB_CLEAR_NODE(&node->rb_node); |
389305b2 QW |
1379 | } |
1380 | spin_unlock(&rc->reloc_root_tree.lock); | |
1381 | if (!node) | |
1382 | return; | |
1383 | BUG_ON((struct btrfs_root *)node->data != root); | |
5d4f98a2 | 1384 | } |
5d4f98a2 | 1385 | |
f44deb74 JB |
1386 | /* |
1387 | * We only put the reloc root here if it's on the list. There's a lot | |
1388 | * of places where the pattern is to splice the rc->reloc_roots, process | |
1389 | * the reloc roots, and then add the reloc root back onto | |
1390 | * rc->reloc_roots. If we call __del_reloc_root while it's off of the | |
1391 | * list we don't want the reference being dropped, because the guy | |
1392 | * messing with the list is in charge of the reference. | |
1393 | */ | |
0b246afa | 1394 | spin_lock(&fs_info->trans_lock); |
f44deb74 JB |
1395 | if (!list_empty(&root->root_list)) { |
1396 | put_ref = true; | |
1397 | list_del_init(&root->root_list); | |
1398 | } | |
0b246afa | 1399 | spin_unlock(&fs_info->trans_lock); |
f44deb74 JB |
1400 | if (put_ref) |
1401 | btrfs_put_root(root); | |
c974c464 WS |
1402 | kfree(node); |
1403 | } | |
1404 | ||
1405 | /* | |
1406 | * helper to update the 'address of tree root -> reloc tree' | |
1407 | * mapping | |
1408 | */ | |
ea287ab1 | 1409 | static int __update_reloc_root(struct btrfs_root *root) |
c974c464 | 1410 | { |
0b246afa | 1411 | struct btrfs_fs_info *fs_info = root->fs_info; |
c974c464 WS |
1412 | struct rb_node *rb_node; |
1413 | struct mapping_node *node = NULL; | |
0b246afa | 1414 | struct reloc_control *rc = fs_info->reloc_ctl; |
c974c464 WS |
1415 | |
1416 | spin_lock(&rc->reloc_root_tree.lock); | |
1417 | rb_node = tree_search(&rc->reloc_root_tree.rb_root, | |
ea287ab1 | 1418 | root->commit_root->start); |
c974c464 WS |
1419 | if (rb_node) { |
1420 | node = rb_entry(rb_node, struct mapping_node, rb_node); | |
1421 | rb_erase(&node->rb_node, &rc->reloc_root_tree.rb_root); | |
5d4f98a2 | 1422 | } |
c974c464 WS |
1423 | spin_unlock(&rc->reloc_root_tree.lock); |
1424 | ||
1425 | if (!node) | |
1426 | return 0; | |
1427 | BUG_ON((struct btrfs_root *)node->data != root); | |
1428 | ||
1429 | spin_lock(&rc->reloc_root_tree.lock); | |
ea287ab1 | 1430 | node->bytenr = root->node->start; |
c974c464 WS |
1431 | rb_node = tree_insert(&rc->reloc_root_tree.rb_root, |
1432 | node->bytenr, &node->rb_node); | |
1433 | spin_unlock(&rc->reloc_root_tree.lock); | |
1434 | if (rb_node) | |
1435 | backref_tree_panic(rb_node, -EEXIST, node->bytenr); | |
5d4f98a2 YZ |
1436 | return 0; |
1437 | } | |
1438 | ||
3fd0a558 YZ |
1439 | static struct btrfs_root *create_reloc_root(struct btrfs_trans_handle *trans, |
1440 | struct btrfs_root *root, u64 objectid) | |
5d4f98a2 | 1441 | { |
0b246afa | 1442 | struct btrfs_fs_info *fs_info = root->fs_info; |
5d4f98a2 YZ |
1443 | struct btrfs_root *reloc_root; |
1444 | struct extent_buffer *eb; | |
1445 | struct btrfs_root_item *root_item; | |
1446 | struct btrfs_key root_key; | |
1447 | int ret; | |
1448 | ||
5d4f98a2 YZ |
1449 | root_item = kmalloc(sizeof(*root_item), GFP_NOFS); |
1450 | BUG_ON(!root_item); | |
1451 | ||
1452 | root_key.objectid = BTRFS_TREE_RELOC_OBJECTID; | |
1453 | root_key.type = BTRFS_ROOT_ITEM_KEY; | |
3fd0a558 | 1454 | root_key.offset = objectid; |
5d4f98a2 | 1455 | |
3fd0a558 | 1456 | if (root->root_key.objectid == objectid) { |
054570a1 FM |
1457 | u64 commit_root_gen; |
1458 | ||
3fd0a558 YZ |
1459 | /* called by btrfs_init_reloc_root */ |
1460 | ret = btrfs_copy_root(trans, root, root->commit_root, &eb, | |
1461 | BTRFS_TREE_RELOC_OBJECTID); | |
1462 | BUG_ON(ret); | |
054570a1 FM |
1463 | /* |
1464 | * Set the last_snapshot field to the generation of the commit | |
1465 | * root - like this ctree.c:btrfs_block_can_be_shared() behaves | |
1466 | * correctly (returns true) when the relocation root is created | |
1467 | * either inside the critical section of a transaction commit | |
1468 | * (through transaction.c:qgroup_account_snapshot()) and when | |
1469 | * it's created before the transaction commit is started. | |
1470 | */ | |
1471 | commit_root_gen = btrfs_header_generation(root->commit_root); | |
1472 | btrfs_set_root_last_snapshot(&root->root_item, commit_root_gen); | |
3fd0a558 YZ |
1473 | } else { |
1474 | /* | |
1475 | * called by btrfs_reloc_post_snapshot_hook. | |
1476 | * the source tree is a reloc tree, all tree blocks | |
1477 | * modified after it was created have RELOC flag | |
1478 | * set in their headers. so it's OK to not update | |
1479 | * the 'last_snapshot'. | |
1480 | */ | |
1481 | ret = btrfs_copy_root(trans, root, root->node, &eb, | |
1482 | BTRFS_TREE_RELOC_OBJECTID); | |
1483 | BUG_ON(ret); | |
1484 | } | |
5d4f98a2 | 1485 | |
5d4f98a2 | 1486 | memcpy(root_item, &root->root_item, sizeof(*root_item)); |
5d4f98a2 YZ |
1487 | btrfs_set_root_bytenr(root_item, eb->start); |
1488 | btrfs_set_root_level(root_item, btrfs_header_level(eb)); | |
1489 | btrfs_set_root_generation(root_item, trans->transid); | |
3fd0a558 YZ |
1490 | |
1491 | if (root->root_key.objectid == objectid) { | |
1492 | btrfs_set_root_refs(root_item, 0); | |
1493 | memset(&root_item->drop_progress, 0, | |
1494 | sizeof(struct btrfs_disk_key)); | |
1495 | root_item->drop_level = 0; | |
1496 | } | |
5d4f98a2 YZ |
1497 | |
1498 | btrfs_tree_unlock(eb); | |
1499 | free_extent_buffer(eb); | |
1500 | ||
0b246afa | 1501 | ret = btrfs_insert_root(trans, fs_info->tree_root, |
5d4f98a2 YZ |
1502 | &root_key, root_item); |
1503 | BUG_ON(ret); | |
1504 | kfree(root_item); | |
1505 | ||
3dbf1738 | 1506 | reloc_root = btrfs_read_tree_root(fs_info->tree_root, &root_key); |
5d4f98a2 | 1507 | BUG_ON(IS_ERR(reloc_root)); |
3dbf1738 | 1508 | set_bit(BTRFS_ROOT_REF_COWS, &reloc_root->state); |
5d4f98a2 | 1509 | reloc_root->last_trans = trans->transid; |
3fd0a558 YZ |
1510 | return reloc_root; |
1511 | } | |
1512 | ||
1513 | /* | |
1514 | * create reloc tree for a given fs tree. reloc tree is just a | |
1515 | * snapshot of the fs tree with special root objectid. | |
f44deb74 JB |
1516 | * |
1517 | * The reloc_root comes out of here with two references, one for | |
1518 | * root->reloc_root, and another for being on the rc->reloc_roots list. | |
3fd0a558 YZ |
1519 | */ |
1520 | int btrfs_init_reloc_root(struct btrfs_trans_handle *trans, | |
1521 | struct btrfs_root *root) | |
1522 | { | |
0b246afa | 1523 | struct btrfs_fs_info *fs_info = root->fs_info; |
3fd0a558 | 1524 | struct btrfs_root *reloc_root; |
0b246afa | 1525 | struct reloc_control *rc = fs_info->reloc_ctl; |
20dd2cbf | 1526 | struct btrfs_block_rsv *rsv; |
3fd0a558 | 1527 | int clear_rsv = 0; |
ffd7b339 | 1528 | int ret; |
3fd0a558 | 1529 | |
aec7db3b | 1530 | if (!rc) |
2abc726a JB |
1531 | return 0; |
1532 | ||
1fac4a54 QW |
1533 | /* |
1534 | * The subvolume has reloc tree but the swap is finished, no need to | |
1535 | * create/update the dead reloc tree | |
1536 | */ | |
6282675e | 1537 | if (reloc_root_is_dead(root)) |
1fac4a54 QW |
1538 | return 0; |
1539 | ||
aec7db3b JB |
1540 | /* |
1541 | * This is subtle but important. We do not do | |
1542 | * record_root_in_transaction for reloc roots, instead we record their | |
1543 | * corresponding fs root, and then here we update the last trans for the | |
1544 | * reloc root. This means that we have to do this for the entire life | |
1545 | * of the reloc root, regardless of which stage of the relocation we are | |
1546 | * in. | |
1547 | */ | |
3fd0a558 YZ |
1548 | if (root->reloc_root) { |
1549 | reloc_root = root->reloc_root; | |
1550 | reloc_root->last_trans = trans->transid; | |
1551 | return 0; | |
1552 | } | |
1553 | ||
aec7db3b JB |
1554 | /* |
1555 | * We are merging reloc roots, we do not need new reloc trees. Also | |
1556 | * reloc trees never need their own reloc tree. | |
1557 | */ | |
1558 | if (!rc->create_reloc_tree || | |
1559 | root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) | |
1560 | return 0; | |
1561 | ||
20dd2cbf MX |
1562 | if (!trans->reloc_reserved) { |
1563 | rsv = trans->block_rsv; | |
3fd0a558 YZ |
1564 | trans->block_rsv = rc->block_rsv; |
1565 | clear_rsv = 1; | |
1566 | } | |
1567 | reloc_root = create_reloc_root(trans, root, root->root_key.objectid); | |
1568 | if (clear_rsv) | |
20dd2cbf | 1569 | trans->block_rsv = rsv; |
5d4f98a2 | 1570 | |
ffd7b339 JM |
1571 | ret = __add_reloc_root(reloc_root); |
1572 | BUG_ON(ret < 0); | |
f44deb74 | 1573 | root->reloc_root = btrfs_grab_root(reloc_root); |
5d4f98a2 YZ |
1574 | return 0; |
1575 | } | |
1576 | ||
1577 | /* | |
1578 | * update root item of reloc tree | |
1579 | */ | |
1580 | int btrfs_update_reloc_root(struct btrfs_trans_handle *trans, | |
1581 | struct btrfs_root *root) | |
1582 | { | |
0b246afa | 1583 | struct btrfs_fs_info *fs_info = root->fs_info; |
5d4f98a2 YZ |
1584 | struct btrfs_root *reloc_root; |
1585 | struct btrfs_root_item *root_item; | |
5d4f98a2 YZ |
1586 | int ret; |
1587 | ||
6282675e | 1588 | if (!have_reloc_root(root)) |
7585717f | 1589 | goto out; |
5d4f98a2 YZ |
1590 | |
1591 | reloc_root = root->reloc_root; | |
1592 | root_item = &reloc_root->root_item; | |
1593 | ||
f44deb74 JB |
1594 | /* |
1595 | * We are probably ok here, but __del_reloc_root() will drop its ref of | |
1596 | * the root. We have the ref for root->reloc_root, but just in case | |
1597 | * hold it while we update the reloc root. | |
1598 | */ | |
1599 | btrfs_grab_root(reloc_root); | |
1600 | ||
d2311e69 | 1601 | /* root->reloc_root will stay until current relocation finished */ |
0b246afa | 1602 | if (fs_info->reloc_ctl->merge_reloc_tree && |
3fd0a558 | 1603 | btrfs_root_refs(root_item) == 0) { |
d2311e69 | 1604 | set_bit(BTRFS_ROOT_DEAD_RELOC_TREE, &root->state); |
6282675e QW |
1605 | /* |
1606 | * Mark the tree as dead before we change reloc_root so | |
1607 | * have_reloc_root will not touch it from now on. | |
1608 | */ | |
1609 | smp_wmb(); | |
c974c464 | 1610 | __del_reloc_root(reloc_root); |
5d4f98a2 YZ |
1611 | } |
1612 | ||
5d4f98a2 | 1613 | if (reloc_root->commit_root != reloc_root->node) { |
ea287ab1 | 1614 | __update_reloc_root(reloc_root); |
5d4f98a2 YZ |
1615 | btrfs_set_root_node(root_item, reloc_root->node); |
1616 | free_extent_buffer(reloc_root->commit_root); | |
1617 | reloc_root->commit_root = btrfs_root_node(reloc_root); | |
1618 | } | |
1619 | ||
0b246afa | 1620 | ret = btrfs_update_root(trans, fs_info->tree_root, |
5d4f98a2 YZ |
1621 | &reloc_root->root_key, root_item); |
1622 | BUG_ON(ret); | |
f44deb74 | 1623 | btrfs_put_root(reloc_root); |
7585717f | 1624 | out: |
5d4f98a2 YZ |
1625 | return 0; |
1626 | } | |
1627 | ||
1628 | /* | |
1629 | * helper to find first cached inode with inode number >= objectid | |
1630 | * in a subvolume | |
1631 | */ | |
1632 | static struct inode *find_next_inode(struct btrfs_root *root, u64 objectid) | |
1633 | { | |
1634 | struct rb_node *node; | |
1635 | struct rb_node *prev; | |
1636 | struct btrfs_inode *entry; | |
1637 | struct inode *inode; | |
1638 | ||
1639 | spin_lock(&root->inode_lock); | |
1640 | again: | |
1641 | node = root->inode_tree.rb_node; | |
1642 | prev = NULL; | |
1643 | while (node) { | |
1644 | prev = node; | |
1645 | entry = rb_entry(node, struct btrfs_inode, rb_node); | |
1646 | ||
4a0cc7ca | 1647 | if (objectid < btrfs_ino(entry)) |
5d4f98a2 | 1648 | node = node->rb_left; |
4a0cc7ca | 1649 | else if (objectid > btrfs_ino(entry)) |
5d4f98a2 YZ |
1650 | node = node->rb_right; |
1651 | else | |
1652 | break; | |
1653 | } | |
1654 | if (!node) { | |
1655 | while (prev) { | |
1656 | entry = rb_entry(prev, struct btrfs_inode, rb_node); | |
4a0cc7ca | 1657 | if (objectid <= btrfs_ino(entry)) { |
5d4f98a2 YZ |
1658 | node = prev; |
1659 | break; | |
1660 | } | |
1661 | prev = rb_next(prev); | |
1662 | } | |
1663 | } | |
1664 | while (node) { | |
1665 | entry = rb_entry(node, struct btrfs_inode, rb_node); | |
1666 | inode = igrab(&entry->vfs_inode); | |
1667 | if (inode) { | |
1668 | spin_unlock(&root->inode_lock); | |
1669 | return inode; | |
1670 | } | |
1671 | ||
4a0cc7ca | 1672 | objectid = btrfs_ino(entry) + 1; |
5d4f98a2 YZ |
1673 | if (cond_resched_lock(&root->inode_lock)) |
1674 | goto again; | |
1675 | ||
1676 | node = rb_next(node); | |
1677 | } | |
1678 | spin_unlock(&root->inode_lock); | |
1679 | return NULL; | |
1680 | } | |
1681 | ||
32da5386 | 1682 | static int in_block_group(u64 bytenr, struct btrfs_block_group *block_group) |
5d4f98a2 | 1683 | { |
b3470b5d DS |
1684 | if (bytenr >= block_group->start && |
1685 | bytenr < block_group->start + block_group->length) | |
5d4f98a2 YZ |
1686 | return 1; |
1687 | return 0; | |
1688 | } | |
1689 | ||
1690 | /* | |
1691 | * get new location of data | |
1692 | */ | |
1693 | static int get_new_location(struct inode *reloc_inode, u64 *new_bytenr, | |
1694 | u64 bytenr, u64 num_bytes) | |
1695 | { | |
1696 | struct btrfs_root *root = BTRFS_I(reloc_inode)->root; | |
1697 | struct btrfs_path *path; | |
1698 | struct btrfs_file_extent_item *fi; | |
1699 | struct extent_buffer *leaf; | |
1700 | int ret; | |
1701 | ||
1702 | path = btrfs_alloc_path(); | |
1703 | if (!path) | |
1704 | return -ENOMEM; | |
1705 | ||
1706 | bytenr -= BTRFS_I(reloc_inode)->index_cnt; | |
f85b7379 DS |
1707 | ret = btrfs_lookup_file_extent(NULL, root, path, |
1708 | btrfs_ino(BTRFS_I(reloc_inode)), bytenr, 0); | |
5d4f98a2 YZ |
1709 | if (ret < 0) |
1710 | goto out; | |
1711 | if (ret > 0) { | |
1712 | ret = -ENOENT; | |
1713 | goto out; | |
1714 | } | |
1715 | ||
1716 | leaf = path->nodes[0]; | |
1717 | fi = btrfs_item_ptr(leaf, path->slots[0], | |
1718 | struct btrfs_file_extent_item); | |
1719 | ||
1720 | BUG_ON(btrfs_file_extent_offset(leaf, fi) || | |
1721 | btrfs_file_extent_compression(leaf, fi) || | |
1722 | btrfs_file_extent_encryption(leaf, fi) || | |
1723 | btrfs_file_extent_other_encoding(leaf, fi)); | |
1724 | ||
1725 | if (num_bytes != btrfs_file_extent_disk_num_bytes(leaf, fi)) { | |
83d4cfd4 | 1726 | ret = -EINVAL; |
5d4f98a2 YZ |
1727 | goto out; |
1728 | } | |
1729 | ||
3fd0a558 | 1730 | *new_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
5d4f98a2 YZ |
1731 | ret = 0; |
1732 | out: | |
1733 | btrfs_free_path(path); | |
1734 | return ret; | |
1735 | } | |
1736 | ||
1737 | /* | |
1738 | * update file extent items in the tree leaf to point to | |
1739 | * the new locations. | |
1740 | */ | |
3fd0a558 YZ |
1741 | static noinline_for_stack |
1742 | int replace_file_extents(struct btrfs_trans_handle *trans, | |
1743 | struct reloc_control *rc, | |
1744 | struct btrfs_root *root, | |
1745 | struct extent_buffer *leaf) | |
5d4f98a2 | 1746 | { |
0b246afa | 1747 | struct btrfs_fs_info *fs_info = root->fs_info; |
5d4f98a2 YZ |
1748 | struct btrfs_key key; |
1749 | struct btrfs_file_extent_item *fi; | |
1750 | struct inode *inode = NULL; | |
5d4f98a2 YZ |
1751 | u64 parent; |
1752 | u64 bytenr; | |
3fd0a558 | 1753 | u64 new_bytenr = 0; |
5d4f98a2 YZ |
1754 | u64 num_bytes; |
1755 | u64 end; | |
1756 | u32 nritems; | |
1757 | u32 i; | |
83d4cfd4 | 1758 | int ret = 0; |
5d4f98a2 YZ |
1759 | int first = 1; |
1760 | int dirty = 0; | |
1761 | ||
1762 | if (rc->stage != UPDATE_DATA_PTRS) | |
1763 | return 0; | |
1764 | ||
1765 | /* reloc trees always use full backref */ | |
1766 | if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) | |
1767 | parent = leaf->start; | |
1768 | else | |
1769 | parent = 0; | |
1770 | ||
1771 | nritems = btrfs_header_nritems(leaf); | |
1772 | for (i = 0; i < nritems; i++) { | |
82fa113f QW |
1773 | struct btrfs_ref ref = { 0 }; |
1774 | ||
5d4f98a2 YZ |
1775 | cond_resched(); |
1776 | btrfs_item_key_to_cpu(leaf, &key, i); | |
1777 | if (key.type != BTRFS_EXTENT_DATA_KEY) | |
1778 | continue; | |
1779 | fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item); | |
1780 | if (btrfs_file_extent_type(leaf, fi) == | |
1781 | BTRFS_FILE_EXTENT_INLINE) | |
1782 | continue; | |
1783 | bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); | |
1784 | num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi); | |
1785 | if (bytenr == 0) | |
1786 | continue; | |
1787 | if (!in_block_group(bytenr, rc->block_group)) | |
1788 | continue; | |
1789 | ||
1790 | /* | |
1791 | * if we are modifying block in fs tree, wait for readpage | |
1792 | * to complete and drop the extent cache | |
1793 | */ | |
1794 | if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) { | |
5d4f98a2 YZ |
1795 | if (first) { |
1796 | inode = find_next_inode(root, key.objectid); | |
5d4f98a2 | 1797 | first = 0; |
4a0cc7ca | 1798 | } else if (inode && btrfs_ino(BTRFS_I(inode)) < key.objectid) { |
3fd0a558 | 1799 | btrfs_add_delayed_iput(inode); |
5d4f98a2 | 1800 | inode = find_next_inode(root, key.objectid); |
5d4f98a2 | 1801 | } |
4a0cc7ca | 1802 | if (inode && btrfs_ino(BTRFS_I(inode)) == key.objectid) { |
5d4f98a2 YZ |
1803 | end = key.offset + |
1804 | btrfs_file_extent_num_bytes(leaf, fi); | |
1805 | WARN_ON(!IS_ALIGNED(key.offset, | |
0b246afa JM |
1806 | fs_info->sectorsize)); |
1807 | WARN_ON(!IS_ALIGNED(end, fs_info->sectorsize)); | |
5d4f98a2 YZ |
1808 | end--; |
1809 | ret = try_lock_extent(&BTRFS_I(inode)->io_tree, | |
d0082371 | 1810 | key.offset, end); |
5d4f98a2 YZ |
1811 | if (!ret) |
1812 | continue; | |
1813 | ||
dcdbc059 NB |
1814 | btrfs_drop_extent_cache(BTRFS_I(inode), |
1815 | key.offset, end, 1); | |
5d4f98a2 | 1816 | unlock_extent(&BTRFS_I(inode)->io_tree, |
d0082371 | 1817 | key.offset, end); |
5d4f98a2 YZ |
1818 | } |
1819 | } | |
1820 | ||
1821 | ret = get_new_location(rc->data_inode, &new_bytenr, | |
1822 | bytenr, num_bytes); | |
83d4cfd4 JB |
1823 | if (ret) { |
1824 | /* | |
1825 | * Don't have to abort since we've not changed anything | |
1826 | * in the file extent yet. | |
1827 | */ | |
1828 | break; | |
3fd0a558 | 1829 | } |
5d4f98a2 YZ |
1830 | |
1831 | btrfs_set_file_extent_disk_bytenr(leaf, fi, new_bytenr); | |
1832 | dirty = 1; | |
1833 | ||
1834 | key.offset -= btrfs_file_extent_offset(leaf, fi); | |
82fa113f QW |
1835 | btrfs_init_generic_ref(&ref, BTRFS_ADD_DELAYED_REF, new_bytenr, |
1836 | num_bytes, parent); | |
1837 | ref.real_root = root->root_key.objectid; | |
1838 | btrfs_init_data_ref(&ref, btrfs_header_owner(leaf), | |
1839 | key.objectid, key.offset); | |
1840 | ret = btrfs_inc_extent_ref(trans, &ref); | |
83d4cfd4 | 1841 | if (ret) { |
66642832 | 1842 | btrfs_abort_transaction(trans, ret); |
83d4cfd4 JB |
1843 | break; |
1844 | } | |
5d4f98a2 | 1845 | |
ffd4bb2a QW |
1846 | btrfs_init_generic_ref(&ref, BTRFS_DROP_DELAYED_REF, bytenr, |
1847 | num_bytes, parent); | |
1848 | ref.real_root = root->root_key.objectid; | |
1849 | btrfs_init_data_ref(&ref, btrfs_header_owner(leaf), | |
1850 | key.objectid, key.offset); | |
1851 | ret = btrfs_free_extent(trans, &ref); | |
83d4cfd4 | 1852 | if (ret) { |
66642832 | 1853 | btrfs_abort_transaction(trans, ret); |
83d4cfd4 JB |
1854 | break; |
1855 | } | |
5d4f98a2 YZ |
1856 | } |
1857 | if (dirty) | |
1858 | btrfs_mark_buffer_dirty(leaf); | |
3fd0a558 YZ |
1859 | if (inode) |
1860 | btrfs_add_delayed_iput(inode); | |
83d4cfd4 | 1861 | return ret; |
5d4f98a2 YZ |
1862 | } |
1863 | ||
1864 | static noinline_for_stack | |
1865 | int memcmp_node_keys(struct extent_buffer *eb, int slot, | |
1866 | struct btrfs_path *path, int level) | |
1867 | { | |
1868 | struct btrfs_disk_key key1; | |
1869 | struct btrfs_disk_key key2; | |
1870 | btrfs_node_key(eb, &key1, slot); | |
1871 | btrfs_node_key(path->nodes[level], &key2, path->slots[level]); | |
1872 | return memcmp(&key1, &key2, sizeof(key1)); | |
1873 | } | |
1874 | ||
1875 | /* | |
1876 | * try to replace tree blocks in fs tree with the new blocks | |
1877 | * in reloc tree. tree blocks haven't been modified since the | |
1878 | * reloc tree was create can be replaced. | |
1879 | * | |
1880 | * if a block was replaced, level of the block + 1 is returned. | |
1881 | * if no block got replaced, 0 is returned. if there are other | |
1882 | * errors, a negative error number is returned. | |
1883 | */ | |
3fd0a558 | 1884 | static noinline_for_stack |
3d0174f7 | 1885 | int replace_path(struct btrfs_trans_handle *trans, struct reloc_control *rc, |
3fd0a558 YZ |
1886 | struct btrfs_root *dest, struct btrfs_root *src, |
1887 | struct btrfs_path *path, struct btrfs_key *next_key, | |
1888 | int lowest_level, int max_level) | |
5d4f98a2 | 1889 | { |
0b246afa | 1890 | struct btrfs_fs_info *fs_info = dest->fs_info; |
5d4f98a2 YZ |
1891 | struct extent_buffer *eb; |
1892 | struct extent_buffer *parent; | |
82fa113f | 1893 | struct btrfs_ref ref = { 0 }; |
5d4f98a2 YZ |
1894 | struct btrfs_key key; |
1895 | u64 old_bytenr; | |
1896 | u64 new_bytenr; | |
1897 | u64 old_ptr_gen; | |
1898 | u64 new_ptr_gen; | |
1899 | u64 last_snapshot; | |
1900 | u32 blocksize; | |
3fd0a558 | 1901 | int cow = 0; |
5d4f98a2 YZ |
1902 | int level; |
1903 | int ret; | |
1904 | int slot; | |
1905 | ||
1906 | BUG_ON(src->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID); | |
1907 | BUG_ON(dest->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID); | |
5d4f98a2 YZ |
1908 | |
1909 | last_snapshot = btrfs_root_last_snapshot(&src->root_item); | |
3fd0a558 | 1910 | again: |
5d4f98a2 YZ |
1911 | slot = path->slots[lowest_level]; |
1912 | btrfs_node_key_to_cpu(path->nodes[lowest_level], &key, slot); | |
1913 | ||
1914 | eb = btrfs_lock_root_node(dest); | |
8bead258 | 1915 | btrfs_set_lock_blocking_write(eb); |
5d4f98a2 YZ |
1916 | level = btrfs_header_level(eb); |
1917 | ||
1918 | if (level < lowest_level) { | |
1919 | btrfs_tree_unlock(eb); | |
1920 | free_extent_buffer(eb); | |
1921 | return 0; | |
1922 | } | |
1923 | ||
3fd0a558 YZ |
1924 | if (cow) { |
1925 | ret = btrfs_cow_block(trans, dest, eb, NULL, 0, &eb); | |
1926 | BUG_ON(ret); | |
1927 | } | |
8bead258 | 1928 | btrfs_set_lock_blocking_write(eb); |
5d4f98a2 YZ |
1929 | |
1930 | if (next_key) { | |
1931 | next_key->objectid = (u64)-1; | |
1932 | next_key->type = (u8)-1; | |
1933 | next_key->offset = (u64)-1; | |
1934 | } | |
1935 | ||
1936 | parent = eb; | |
1937 | while (1) { | |
581c1760 QW |
1938 | struct btrfs_key first_key; |
1939 | ||
5d4f98a2 YZ |
1940 | level = btrfs_header_level(parent); |
1941 | BUG_ON(level < lowest_level); | |
1942 | ||
1943 | ret = btrfs_bin_search(parent, &key, level, &slot); | |
cbca7d59 FM |
1944 | if (ret < 0) |
1945 | break; | |
5d4f98a2 YZ |
1946 | if (ret && slot > 0) |
1947 | slot--; | |
1948 | ||
1949 | if (next_key && slot + 1 < btrfs_header_nritems(parent)) | |
1950 | btrfs_node_key_to_cpu(parent, next_key, slot + 1); | |
1951 | ||
1952 | old_bytenr = btrfs_node_blockptr(parent, slot); | |
0b246afa | 1953 | blocksize = fs_info->nodesize; |
5d4f98a2 | 1954 | old_ptr_gen = btrfs_node_ptr_generation(parent, slot); |
17515f1b | 1955 | btrfs_node_key_to_cpu(parent, &first_key, slot); |
5d4f98a2 YZ |
1956 | |
1957 | if (level <= max_level) { | |
1958 | eb = path->nodes[level]; | |
1959 | new_bytenr = btrfs_node_blockptr(eb, | |
1960 | path->slots[level]); | |
1961 | new_ptr_gen = btrfs_node_ptr_generation(eb, | |
1962 | path->slots[level]); | |
1963 | } else { | |
1964 | new_bytenr = 0; | |
1965 | new_ptr_gen = 0; | |
1966 | } | |
1967 | ||
fae7f21c | 1968 | if (WARN_ON(new_bytenr > 0 && new_bytenr == old_bytenr)) { |
5d4f98a2 YZ |
1969 | ret = level; |
1970 | break; | |
1971 | } | |
1972 | ||
1973 | if (new_bytenr == 0 || old_ptr_gen > last_snapshot || | |
1974 | memcmp_node_keys(parent, slot, path, level)) { | |
3fd0a558 | 1975 | if (level <= lowest_level) { |
5d4f98a2 YZ |
1976 | ret = 0; |
1977 | break; | |
1978 | } | |
1979 | ||
581c1760 QW |
1980 | eb = read_tree_block(fs_info, old_bytenr, old_ptr_gen, |
1981 | level - 1, &first_key); | |
64c043de LB |
1982 | if (IS_ERR(eb)) { |
1983 | ret = PTR_ERR(eb); | |
264813ac | 1984 | break; |
64c043de LB |
1985 | } else if (!extent_buffer_uptodate(eb)) { |
1986 | ret = -EIO; | |
416bc658 | 1987 | free_extent_buffer(eb); |
379cde74 | 1988 | break; |
416bc658 | 1989 | } |
5d4f98a2 | 1990 | btrfs_tree_lock(eb); |
3fd0a558 YZ |
1991 | if (cow) { |
1992 | ret = btrfs_cow_block(trans, dest, eb, parent, | |
1993 | slot, &eb); | |
1994 | BUG_ON(ret); | |
5d4f98a2 | 1995 | } |
8bead258 | 1996 | btrfs_set_lock_blocking_write(eb); |
5d4f98a2 YZ |
1997 | |
1998 | btrfs_tree_unlock(parent); | |
1999 | free_extent_buffer(parent); | |
2000 | ||
2001 | parent = eb; | |
2002 | continue; | |
2003 | } | |
2004 | ||
3fd0a558 YZ |
2005 | if (!cow) { |
2006 | btrfs_tree_unlock(parent); | |
2007 | free_extent_buffer(parent); | |
2008 | cow = 1; | |
2009 | goto again; | |
2010 | } | |
2011 | ||
5d4f98a2 YZ |
2012 | btrfs_node_key_to_cpu(path->nodes[level], &key, |
2013 | path->slots[level]); | |
b3b4aa74 | 2014 | btrfs_release_path(path); |
5d4f98a2 YZ |
2015 | |
2016 | path->lowest_level = level; | |
2017 | ret = btrfs_search_slot(trans, src, &key, path, 0, 1); | |
2018 | path->lowest_level = 0; | |
2019 | BUG_ON(ret); | |
2020 | ||
824d8dff QW |
2021 | /* |
2022 | * Info qgroup to trace both subtrees. | |
2023 | * | |
2024 | * We must trace both trees. | |
2025 | * 1) Tree reloc subtree | |
2026 | * If not traced, we will leak data numbers | |
2027 | * 2) Fs subtree | |
2028 | * If not traced, we will double count old data | |
f616f5cd QW |
2029 | * |
2030 | * We don't scan the subtree right now, but only record | |
2031 | * the swapped tree blocks. | |
2032 | * The real subtree rescan is delayed until we have new | |
2033 | * CoW on the subtree root node before transaction commit. | |
824d8dff | 2034 | */ |
370a11b8 QW |
2035 | ret = btrfs_qgroup_add_swapped_blocks(trans, dest, |
2036 | rc->block_group, parent, slot, | |
2037 | path->nodes[level], path->slots[level], | |
2038 | last_snapshot); | |
2039 | if (ret < 0) | |
2040 | break; | |
5d4f98a2 YZ |
2041 | /* |
2042 | * swap blocks in fs tree and reloc tree. | |
2043 | */ | |
2044 | btrfs_set_node_blockptr(parent, slot, new_bytenr); | |
2045 | btrfs_set_node_ptr_generation(parent, slot, new_ptr_gen); | |
2046 | btrfs_mark_buffer_dirty(parent); | |
2047 | ||
2048 | btrfs_set_node_blockptr(path->nodes[level], | |
2049 | path->slots[level], old_bytenr); | |
2050 | btrfs_set_node_ptr_generation(path->nodes[level], | |
2051 | path->slots[level], old_ptr_gen); | |
2052 | btrfs_mark_buffer_dirty(path->nodes[level]); | |
2053 | ||
82fa113f QW |
2054 | btrfs_init_generic_ref(&ref, BTRFS_ADD_DELAYED_REF, old_bytenr, |
2055 | blocksize, path->nodes[level]->start); | |
2056 | ref.skip_qgroup = true; | |
2057 | btrfs_init_tree_ref(&ref, level - 1, src->root_key.objectid); | |
2058 | ret = btrfs_inc_extent_ref(trans, &ref); | |
5d4f98a2 | 2059 | BUG_ON(ret); |
82fa113f QW |
2060 | btrfs_init_generic_ref(&ref, BTRFS_ADD_DELAYED_REF, new_bytenr, |
2061 | blocksize, 0); | |
2062 | ref.skip_qgroup = true; | |
2063 | btrfs_init_tree_ref(&ref, level - 1, dest->root_key.objectid); | |
2064 | ret = btrfs_inc_extent_ref(trans, &ref); | |
5d4f98a2 YZ |
2065 | BUG_ON(ret); |
2066 | ||
ffd4bb2a QW |
2067 | btrfs_init_generic_ref(&ref, BTRFS_DROP_DELAYED_REF, new_bytenr, |
2068 | blocksize, path->nodes[level]->start); | |
2069 | btrfs_init_tree_ref(&ref, level - 1, src->root_key.objectid); | |
2070 | ref.skip_qgroup = true; | |
2071 | ret = btrfs_free_extent(trans, &ref); | |
5d4f98a2 YZ |
2072 | BUG_ON(ret); |
2073 | ||
ffd4bb2a QW |
2074 | btrfs_init_generic_ref(&ref, BTRFS_DROP_DELAYED_REF, old_bytenr, |
2075 | blocksize, 0); | |
2076 | btrfs_init_tree_ref(&ref, level - 1, dest->root_key.objectid); | |
2077 | ref.skip_qgroup = true; | |
2078 | ret = btrfs_free_extent(trans, &ref); | |
5d4f98a2 YZ |
2079 | BUG_ON(ret); |
2080 | ||
2081 | btrfs_unlock_up_safe(path, 0); | |
2082 | ||
2083 | ret = level; | |
2084 | break; | |
2085 | } | |
2086 | btrfs_tree_unlock(parent); | |
2087 | free_extent_buffer(parent); | |
2088 | return ret; | |
2089 | } | |
2090 | ||
2091 | /* | |
2092 | * helper to find next relocated block in reloc tree | |
2093 | */ | |
2094 | static noinline_for_stack | |
2095 | int walk_up_reloc_tree(struct btrfs_root *root, struct btrfs_path *path, | |
2096 | int *level) | |
2097 | { | |
2098 | struct extent_buffer *eb; | |
2099 | int i; | |
2100 | u64 last_snapshot; | |
2101 | u32 nritems; | |
2102 | ||
2103 | last_snapshot = btrfs_root_last_snapshot(&root->root_item); | |
2104 | ||
2105 | for (i = 0; i < *level; i++) { | |
2106 | free_extent_buffer(path->nodes[i]); | |
2107 | path->nodes[i] = NULL; | |
2108 | } | |
2109 | ||
2110 | for (i = *level; i < BTRFS_MAX_LEVEL && path->nodes[i]; i++) { | |
2111 | eb = path->nodes[i]; | |
2112 | nritems = btrfs_header_nritems(eb); | |
2113 | while (path->slots[i] + 1 < nritems) { | |
2114 | path->slots[i]++; | |
2115 | if (btrfs_node_ptr_generation(eb, path->slots[i]) <= | |
2116 | last_snapshot) | |
2117 | continue; | |
2118 | ||
2119 | *level = i; | |
2120 | return 0; | |
2121 | } | |
2122 | free_extent_buffer(path->nodes[i]); | |
2123 | path->nodes[i] = NULL; | |
2124 | } | |
2125 | return 1; | |
2126 | } | |
2127 | ||
2128 | /* | |
2129 | * walk down reloc tree to find relocated block of lowest level | |
2130 | */ | |
2131 | static noinline_for_stack | |
2132 | int walk_down_reloc_tree(struct btrfs_root *root, struct btrfs_path *path, | |
2133 | int *level) | |
2134 | { | |
2ff7e61e | 2135 | struct btrfs_fs_info *fs_info = root->fs_info; |
5d4f98a2 YZ |
2136 | struct extent_buffer *eb = NULL; |
2137 | int i; | |
2138 | u64 bytenr; | |
2139 | u64 ptr_gen = 0; | |
2140 | u64 last_snapshot; | |
5d4f98a2 YZ |
2141 | u32 nritems; |
2142 | ||
2143 | last_snapshot = btrfs_root_last_snapshot(&root->root_item); | |
2144 | ||
2145 | for (i = *level; i > 0; i--) { | |
581c1760 QW |
2146 | struct btrfs_key first_key; |
2147 | ||
5d4f98a2 YZ |
2148 | eb = path->nodes[i]; |
2149 | nritems = btrfs_header_nritems(eb); | |
2150 | while (path->slots[i] < nritems) { | |
2151 | ptr_gen = btrfs_node_ptr_generation(eb, path->slots[i]); | |
2152 | if (ptr_gen > last_snapshot) | |
2153 | break; | |
2154 | path->slots[i]++; | |
2155 | } | |
2156 | if (path->slots[i] >= nritems) { | |
2157 | if (i == *level) | |
2158 | break; | |
2159 | *level = i + 1; | |
2160 | return 0; | |
2161 | } | |
2162 | if (i == 1) { | |
2163 | *level = i; | |
2164 | return 0; | |
2165 | } | |
2166 | ||
2167 | bytenr = btrfs_node_blockptr(eb, path->slots[i]); | |
581c1760 QW |
2168 | btrfs_node_key_to_cpu(eb, &first_key, path->slots[i]); |
2169 | eb = read_tree_block(fs_info, bytenr, ptr_gen, i - 1, | |
2170 | &first_key); | |
64c043de LB |
2171 | if (IS_ERR(eb)) { |
2172 | return PTR_ERR(eb); | |
2173 | } else if (!extent_buffer_uptodate(eb)) { | |
416bc658 JB |
2174 | free_extent_buffer(eb); |
2175 | return -EIO; | |
2176 | } | |
5d4f98a2 YZ |
2177 | BUG_ON(btrfs_header_level(eb) != i - 1); |
2178 | path->nodes[i - 1] = eb; | |
2179 | path->slots[i - 1] = 0; | |
2180 | } | |
2181 | return 1; | |
2182 | } | |
2183 | ||
2184 | /* | |
2185 | * invalidate extent cache for file extents whose key in range of | |
2186 | * [min_key, max_key) | |
2187 | */ | |
2188 | static int invalidate_extent_cache(struct btrfs_root *root, | |
2189 | struct btrfs_key *min_key, | |
2190 | struct btrfs_key *max_key) | |
2191 | { | |
0b246afa | 2192 | struct btrfs_fs_info *fs_info = root->fs_info; |
5d4f98a2 YZ |
2193 | struct inode *inode = NULL; |
2194 | u64 objectid; | |
2195 | u64 start, end; | |
33345d01 | 2196 | u64 ino; |
5d4f98a2 YZ |
2197 | |
2198 | objectid = min_key->objectid; | |
2199 | while (1) { | |
2200 | cond_resched(); | |
2201 | iput(inode); | |
2202 | ||
2203 | if (objectid > max_key->objectid) | |
2204 | break; | |
2205 | ||
2206 | inode = find_next_inode(root, objectid); | |
2207 | if (!inode) | |
2208 | break; | |
4a0cc7ca | 2209 | ino = btrfs_ino(BTRFS_I(inode)); |
5d4f98a2 | 2210 | |
33345d01 | 2211 | if (ino > max_key->objectid) { |
5d4f98a2 YZ |
2212 | iput(inode); |
2213 | break; | |
2214 | } | |
2215 | ||
33345d01 | 2216 | objectid = ino + 1; |
5d4f98a2 YZ |
2217 | if (!S_ISREG(inode->i_mode)) |
2218 | continue; | |
2219 | ||
33345d01 | 2220 | if (unlikely(min_key->objectid == ino)) { |
5d4f98a2 YZ |
2221 | if (min_key->type > BTRFS_EXTENT_DATA_KEY) |
2222 | continue; | |
2223 | if (min_key->type < BTRFS_EXTENT_DATA_KEY) | |
2224 | start = 0; | |
2225 | else { | |
2226 | start = min_key->offset; | |
0b246afa | 2227 | WARN_ON(!IS_ALIGNED(start, fs_info->sectorsize)); |
5d4f98a2 YZ |
2228 | } |
2229 | } else { | |
2230 | start = 0; | |
2231 | } | |
2232 | ||
33345d01 | 2233 | if (unlikely(max_key->objectid == ino)) { |
5d4f98a2 YZ |
2234 | if (max_key->type < BTRFS_EXTENT_DATA_KEY) |
2235 | continue; | |
2236 | if (max_key->type > BTRFS_EXTENT_DATA_KEY) { | |
2237 | end = (u64)-1; | |
2238 | } else { | |
2239 | if (max_key->offset == 0) | |
2240 | continue; | |
2241 | end = max_key->offset; | |
0b246afa | 2242 | WARN_ON(!IS_ALIGNED(end, fs_info->sectorsize)); |
5d4f98a2 YZ |
2243 | end--; |
2244 | } | |
2245 | } else { | |
2246 | end = (u64)-1; | |
2247 | } | |
2248 | ||
2249 | /* the lock_extent waits for readpage to complete */ | |
d0082371 | 2250 | lock_extent(&BTRFS_I(inode)->io_tree, start, end); |
dcdbc059 | 2251 | btrfs_drop_extent_cache(BTRFS_I(inode), start, end, 1); |
d0082371 | 2252 | unlock_extent(&BTRFS_I(inode)->io_tree, start, end); |
5d4f98a2 YZ |
2253 | } |
2254 | return 0; | |
2255 | } | |
2256 | ||
2257 | static int find_next_key(struct btrfs_path *path, int level, | |
2258 | struct btrfs_key *key) | |
2259 | ||
2260 | { | |
2261 | while (level < BTRFS_MAX_LEVEL) { | |
2262 | if (!path->nodes[level]) | |
2263 | break; | |
2264 | if (path->slots[level] + 1 < | |
2265 | btrfs_header_nritems(path->nodes[level])) { | |
2266 | btrfs_node_key_to_cpu(path->nodes[level], key, | |
2267 | path->slots[level] + 1); | |
2268 | return 0; | |
2269 | } | |
2270 | level++; | |
2271 | } | |
2272 | return 1; | |
2273 | } | |
2274 | ||
d2311e69 QW |
2275 | /* |
2276 | * Insert current subvolume into reloc_control::dirty_subvol_roots | |
2277 | */ | |
2278 | static void insert_dirty_subvol(struct btrfs_trans_handle *trans, | |
2279 | struct reloc_control *rc, | |
2280 | struct btrfs_root *root) | |
2281 | { | |
2282 | struct btrfs_root *reloc_root = root->reloc_root; | |
2283 | struct btrfs_root_item *reloc_root_item; | |
2284 | ||
2285 | /* @root must be a subvolume tree root with a valid reloc tree */ | |
2286 | ASSERT(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID); | |
2287 | ASSERT(reloc_root); | |
2288 | ||
2289 | reloc_root_item = &reloc_root->root_item; | |
2290 | memset(&reloc_root_item->drop_progress, 0, | |
2291 | sizeof(reloc_root_item->drop_progress)); | |
2292 | reloc_root_item->drop_level = 0; | |
2293 | btrfs_set_root_refs(reloc_root_item, 0); | |
2294 | btrfs_update_reloc_root(trans, root); | |
2295 | ||
2296 | if (list_empty(&root->reloc_dirty_list)) { | |
00246528 | 2297 | btrfs_grab_root(root); |
d2311e69 QW |
2298 | list_add_tail(&root->reloc_dirty_list, &rc->dirty_subvol_roots); |
2299 | } | |
2300 | } | |
2301 | ||
2302 | static int clean_dirty_subvols(struct reloc_control *rc) | |
2303 | { | |
2304 | struct btrfs_root *root; | |
2305 | struct btrfs_root *next; | |
2306 | int ret = 0; | |
30d40577 | 2307 | int ret2; |
d2311e69 QW |
2308 | |
2309 | list_for_each_entry_safe(root, next, &rc->dirty_subvol_roots, | |
2310 | reloc_dirty_list) { | |
30d40577 QW |
2311 | if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) { |
2312 | /* Merged subvolume, cleanup its reloc root */ | |
2313 | struct btrfs_root *reloc_root = root->reloc_root; | |
d2311e69 | 2314 | |
30d40577 QW |
2315 | list_del_init(&root->reloc_dirty_list); |
2316 | root->reloc_root = NULL; | |
6282675e QW |
2317 | /* |
2318 | * Need barrier to ensure clear_bit() only happens after | |
2319 | * root->reloc_root = NULL. Pairs with have_reloc_root. | |
2320 | */ | |
2321 | smp_wmb(); | |
1fac4a54 | 2322 | clear_bit(BTRFS_ROOT_DEAD_RELOC_TREE, &root->state); |
f28de8d8 | 2323 | if (reloc_root) { |
f44deb74 JB |
2324 | /* |
2325 | * btrfs_drop_snapshot drops our ref we hold for | |
2326 | * ->reloc_root. If it fails however we must | |
2327 | * drop the ref ourselves. | |
2328 | */ | |
f28de8d8 | 2329 | ret2 = btrfs_drop_snapshot(reloc_root, 0, 1); |
f44deb74 JB |
2330 | if (ret2 < 0) { |
2331 | btrfs_put_root(reloc_root); | |
2332 | if (!ret) | |
2333 | ret = ret2; | |
2334 | } | |
f28de8d8 | 2335 | } |
00246528 | 2336 | btrfs_put_root(root); |
30d40577 QW |
2337 | } else { |
2338 | /* Orphan reloc tree, just clean it up */ | |
0078a9f9 | 2339 | ret2 = btrfs_drop_snapshot(root, 0, 1); |
f44deb74 JB |
2340 | if (ret2 < 0) { |
2341 | btrfs_put_root(root); | |
2342 | if (!ret) | |
2343 | ret = ret2; | |
2344 | } | |
d2311e69 | 2345 | } |
d2311e69 QW |
2346 | } |
2347 | return ret; | |
2348 | } | |
2349 | ||
5d4f98a2 YZ |
2350 | /* |
2351 | * merge the relocated tree blocks in reloc tree with corresponding | |
2352 | * fs tree. | |
2353 | */ | |
2354 | static noinline_for_stack int merge_reloc_root(struct reloc_control *rc, | |
2355 | struct btrfs_root *root) | |
2356 | { | |
0b246afa | 2357 | struct btrfs_fs_info *fs_info = rc->extent_root->fs_info; |
5d4f98a2 YZ |
2358 | struct btrfs_key key; |
2359 | struct btrfs_key next_key; | |
9e6a0c52 | 2360 | struct btrfs_trans_handle *trans = NULL; |
5d4f98a2 YZ |
2361 | struct btrfs_root *reloc_root; |
2362 | struct btrfs_root_item *root_item; | |
2363 | struct btrfs_path *path; | |
3fd0a558 | 2364 | struct extent_buffer *leaf; |
5d4f98a2 YZ |
2365 | int level; |
2366 | int max_level; | |
2367 | int replaced = 0; | |
2368 | int ret; | |
2369 | int err = 0; | |
3fd0a558 | 2370 | u32 min_reserved; |
5d4f98a2 YZ |
2371 | |
2372 | path = btrfs_alloc_path(); | |
2373 | if (!path) | |
2374 | return -ENOMEM; | |
e4058b54 | 2375 | path->reada = READA_FORWARD; |
5d4f98a2 YZ |
2376 | |
2377 | reloc_root = root->reloc_root; | |
2378 | root_item = &reloc_root->root_item; | |
2379 | ||
2380 | if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) { | |
2381 | level = btrfs_root_level(root_item); | |
67439dad | 2382 | atomic_inc(&reloc_root->node->refs); |
5d4f98a2 YZ |
2383 | path->nodes[level] = reloc_root->node; |
2384 | path->slots[level] = 0; | |
2385 | } else { | |
2386 | btrfs_disk_key_to_cpu(&key, &root_item->drop_progress); | |
2387 | ||
2388 | level = root_item->drop_level; | |
2389 | BUG_ON(level == 0); | |
2390 | path->lowest_level = level; | |
2391 | ret = btrfs_search_slot(NULL, reloc_root, &key, path, 0, 0); | |
33c66f43 | 2392 | path->lowest_level = 0; |
5d4f98a2 YZ |
2393 | if (ret < 0) { |
2394 | btrfs_free_path(path); | |
2395 | return ret; | |
2396 | } | |
2397 | ||
2398 | btrfs_node_key_to_cpu(path->nodes[level], &next_key, | |
2399 | path->slots[level]); | |
2400 | WARN_ON(memcmp(&key, &next_key, sizeof(key))); | |
2401 | ||
2402 | btrfs_unlock_up_safe(path, 0); | |
2403 | } | |
2404 | ||
0b246afa | 2405 | min_reserved = fs_info->nodesize * (BTRFS_MAX_LEVEL - 1) * 2; |
3fd0a558 | 2406 | memset(&next_key, 0, sizeof(next_key)); |
5d4f98a2 | 2407 | |
3fd0a558 | 2408 | while (1) { |
08e007d2 MX |
2409 | ret = btrfs_block_rsv_refill(root, rc->block_rsv, min_reserved, |
2410 | BTRFS_RESERVE_FLUSH_ALL); | |
3fd0a558 | 2411 | if (ret) { |
9e6a0c52 JB |
2412 | err = ret; |
2413 | goto out; | |
5d4f98a2 | 2414 | } |
9e6a0c52 JB |
2415 | trans = btrfs_start_transaction(root, 0); |
2416 | if (IS_ERR(trans)) { | |
2417 | err = PTR_ERR(trans); | |
2418 | trans = NULL; | |
2419 | goto out; | |
2420 | } | |
2abc726a JB |
2421 | |
2422 | /* | |
2423 | * At this point we no longer have a reloc_control, so we can't | |
2424 | * depend on btrfs_init_reloc_root to update our last_trans. | |
2425 | * | |
2426 | * But that's ok, we started the trans handle on our | |
2427 | * corresponding fs_root, which means it's been added to the | |
2428 | * dirty list. At commit time we'll still call | |
2429 | * btrfs_update_reloc_root() and update our root item | |
2430 | * appropriately. | |
2431 | */ | |
2432 | reloc_root->last_trans = trans->transid; | |
9e6a0c52 | 2433 | trans->block_rsv = rc->block_rsv; |
5d4f98a2 | 2434 | |
5d4f98a2 | 2435 | replaced = 0; |
5d4f98a2 YZ |
2436 | max_level = level; |
2437 | ||
2438 | ret = walk_down_reloc_tree(reloc_root, path, &level); | |
2439 | if (ret < 0) { | |
2440 | err = ret; | |
2441 | goto out; | |
2442 | } | |
2443 | if (ret > 0) | |
2444 | break; | |
2445 | ||
2446 | if (!find_next_key(path, level, &key) && | |
2447 | btrfs_comp_cpu_keys(&next_key, &key) >= 0) { | |
2448 | ret = 0; | |
5d4f98a2 | 2449 | } else { |
3d0174f7 | 2450 | ret = replace_path(trans, rc, root, reloc_root, path, |
3fd0a558 | 2451 | &next_key, level, max_level); |
5d4f98a2 YZ |
2452 | } |
2453 | if (ret < 0) { | |
2454 | err = ret; | |
2455 | goto out; | |
2456 | } | |
2457 | ||
2458 | if (ret > 0) { | |
2459 | level = ret; | |
2460 | btrfs_node_key_to_cpu(path->nodes[level], &key, | |
2461 | path->slots[level]); | |
2462 | replaced = 1; | |
5d4f98a2 YZ |
2463 | } |
2464 | ||
2465 | ret = walk_up_reloc_tree(reloc_root, path, &level); | |
2466 | if (ret > 0) | |
2467 | break; | |
2468 | ||
2469 | BUG_ON(level == 0); | |
2470 | /* | |
2471 | * save the merging progress in the drop_progress. | |
2472 | * this is OK since root refs == 1 in this case. | |
2473 | */ | |
2474 | btrfs_node_key(path->nodes[level], &root_item->drop_progress, | |
2475 | path->slots[level]); | |
2476 | root_item->drop_level = level; | |
2477 | ||
3a45bb20 | 2478 | btrfs_end_transaction_throttle(trans); |
9e6a0c52 | 2479 | trans = NULL; |
5d4f98a2 | 2480 | |
2ff7e61e | 2481 | btrfs_btree_balance_dirty(fs_info); |
5d4f98a2 YZ |
2482 | |
2483 | if (replaced && rc->stage == UPDATE_DATA_PTRS) | |
2484 | invalidate_extent_cache(root, &key, &next_key); | |
2485 | } | |
2486 | ||
2487 | /* | |
2488 | * handle the case only one block in the fs tree need to be | |
2489 | * relocated and the block is tree root. | |
2490 | */ | |
2491 | leaf = btrfs_lock_root_node(root); | |
2492 | ret = btrfs_cow_block(trans, root, leaf, NULL, 0, &leaf); | |
2493 | btrfs_tree_unlock(leaf); | |
2494 | free_extent_buffer(leaf); | |
2495 | if (ret < 0) | |
2496 | err = ret; | |
2497 | out: | |
2498 | btrfs_free_path(path); | |
2499 | ||
d2311e69 QW |
2500 | if (err == 0) |
2501 | insert_dirty_subvol(trans, rc, root); | |
5d4f98a2 | 2502 | |
9e6a0c52 | 2503 | if (trans) |
3a45bb20 | 2504 | btrfs_end_transaction_throttle(trans); |
5d4f98a2 | 2505 | |
2ff7e61e | 2506 | btrfs_btree_balance_dirty(fs_info); |
5d4f98a2 | 2507 | |
5d4f98a2 YZ |
2508 | if (replaced && rc->stage == UPDATE_DATA_PTRS) |
2509 | invalidate_extent_cache(root, &key, &next_key); | |
2510 | ||
2511 | return err; | |
2512 | } | |
2513 | ||
3fd0a558 YZ |
2514 | static noinline_for_stack |
2515 | int prepare_to_merge(struct reloc_control *rc, int err) | |
5d4f98a2 | 2516 | { |
3fd0a558 | 2517 | struct btrfs_root *root = rc->extent_root; |
0b246afa | 2518 | struct btrfs_fs_info *fs_info = root->fs_info; |
5d4f98a2 | 2519 | struct btrfs_root *reloc_root; |
3fd0a558 YZ |
2520 | struct btrfs_trans_handle *trans; |
2521 | LIST_HEAD(reloc_roots); | |
2522 | u64 num_bytes = 0; | |
2523 | int ret; | |
3fd0a558 | 2524 | |
0b246afa JM |
2525 | mutex_lock(&fs_info->reloc_mutex); |
2526 | rc->merging_rsv_size += fs_info->nodesize * (BTRFS_MAX_LEVEL - 1) * 2; | |
3fd0a558 | 2527 | rc->merging_rsv_size += rc->nodes_relocated * 2; |
0b246afa | 2528 | mutex_unlock(&fs_info->reloc_mutex); |
7585717f | 2529 | |
3fd0a558 YZ |
2530 | again: |
2531 | if (!err) { | |
2532 | num_bytes = rc->merging_rsv_size; | |
08e007d2 MX |
2533 | ret = btrfs_block_rsv_add(root, rc->block_rsv, num_bytes, |
2534 | BTRFS_RESERVE_FLUSH_ALL); | |
3fd0a558 YZ |
2535 | if (ret) |
2536 | err = ret; | |
2537 | } | |
2538 | ||
7a7eaa40 | 2539 | trans = btrfs_join_transaction(rc->extent_root); |
3612b495 TI |
2540 | if (IS_ERR(trans)) { |
2541 | if (!err) | |
2ff7e61e | 2542 | btrfs_block_rsv_release(fs_info, rc->block_rsv, |
63f018be | 2543 | num_bytes, NULL); |
3612b495 TI |
2544 | return PTR_ERR(trans); |
2545 | } | |
3fd0a558 YZ |
2546 | |
2547 | if (!err) { | |
2548 | if (num_bytes != rc->merging_rsv_size) { | |
3a45bb20 | 2549 | btrfs_end_transaction(trans); |
2ff7e61e | 2550 | btrfs_block_rsv_release(fs_info, rc->block_rsv, |
63f018be | 2551 | num_bytes, NULL); |
3fd0a558 YZ |
2552 | goto again; |
2553 | } | |
2554 | } | |
5d4f98a2 | 2555 | |
3fd0a558 YZ |
2556 | rc->merge_reloc_tree = 1; |
2557 | ||
2558 | while (!list_empty(&rc->reloc_roots)) { | |
2559 | reloc_root = list_entry(rc->reloc_roots.next, | |
2560 | struct btrfs_root, root_list); | |
2561 | list_del_init(&reloc_root->root_list); | |
5d4f98a2 | 2562 | |
0b246afa | 2563 | root = read_fs_root(fs_info, reloc_root->root_key.offset); |
5d4f98a2 YZ |
2564 | BUG_ON(IS_ERR(root)); |
2565 | BUG_ON(root->reloc_root != reloc_root); | |
2566 | ||
3fd0a558 YZ |
2567 | /* |
2568 | * set reference count to 1, so btrfs_recover_relocation | |
2569 | * knows it should resumes merging | |
2570 | */ | |
2571 | if (!err) | |
2572 | btrfs_set_root_refs(&reloc_root->root_item, 1); | |
5d4f98a2 | 2573 | btrfs_update_reloc_root(trans, root); |
5d4f98a2 | 2574 | |
3fd0a558 | 2575 | list_add(&reloc_root->root_list, &reloc_roots); |
00246528 | 2576 | btrfs_put_root(root); |
3fd0a558 | 2577 | } |
5d4f98a2 | 2578 | |
3fd0a558 | 2579 | list_splice(&reloc_roots, &rc->reloc_roots); |
5d4f98a2 | 2580 | |
3fd0a558 | 2581 | if (!err) |
3a45bb20 | 2582 | btrfs_commit_transaction(trans); |
3fd0a558 | 2583 | else |
3a45bb20 | 2584 | btrfs_end_transaction(trans); |
3fd0a558 | 2585 | return err; |
5d4f98a2 YZ |
2586 | } |
2587 | ||
aca1bba6 LB |
2588 | static noinline_for_stack |
2589 | void free_reloc_roots(struct list_head *list) | |
2590 | { | |
2591 | struct btrfs_root *reloc_root; | |
2592 | ||
2593 | while (!list_empty(list)) { | |
2594 | reloc_root = list_entry(list->next, struct btrfs_root, | |
2595 | root_list); | |
bb166d72 | 2596 | __del_reloc_root(reloc_root); |
aca1bba6 LB |
2597 | } |
2598 | } | |
2599 | ||
3fd0a558 | 2600 | static noinline_for_stack |
94404e82 | 2601 | void merge_reloc_roots(struct reloc_control *rc) |
5d4f98a2 | 2602 | { |
0b246afa | 2603 | struct btrfs_fs_info *fs_info = rc->extent_root->fs_info; |
5d4f98a2 | 2604 | struct btrfs_root *root; |
3fd0a558 YZ |
2605 | struct btrfs_root *reloc_root; |
2606 | LIST_HEAD(reloc_roots); | |
2607 | int found = 0; | |
aca1bba6 | 2608 | int ret = 0; |
3fd0a558 YZ |
2609 | again: |
2610 | root = rc->extent_root; | |
7585717f CM |
2611 | |
2612 | /* | |
2613 | * this serializes us with btrfs_record_root_in_transaction, | |
2614 | * we have to make sure nobody is in the middle of | |
2615 | * adding their roots to the list while we are | |
2616 | * doing this splice | |
2617 | */ | |
0b246afa | 2618 | mutex_lock(&fs_info->reloc_mutex); |
3fd0a558 | 2619 | list_splice_init(&rc->reloc_roots, &reloc_roots); |
0b246afa | 2620 | mutex_unlock(&fs_info->reloc_mutex); |
5d4f98a2 | 2621 | |
3fd0a558 YZ |
2622 | while (!list_empty(&reloc_roots)) { |
2623 | found = 1; | |
2624 | reloc_root = list_entry(reloc_roots.next, | |
2625 | struct btrfs_root, root_list); | |
5d4f98a2 | 2626 | |
3fd0a558 | 2627 | if (btrfs_root_refs(&reloc_root->root_item) > 0) { |
0b246afa | 2628 | root = read_fs_root(fs_info, |
3fd0a558 YZ |
2629 | reloc_root->root_key.offset); |
2630 | BUG_ON(IS_ERR(root)); | |
2631 | BUG_ON(root->reloc_root != reloc_root); | |
5d4f98a2 | 2632 | |
3fd0a558 | 2633 | ret = merge_reloc_root(rc, root); |
00246528 | 2634 | btrfs_put_root(root); |
b37b39cd | 2635 | if (ret) { |
25e293c2 WS |
2636 | if (list_empty(&reloc_root->root_list)) |
2637 | list_add_tail(&reloc_root->root_list, | |
2638 | &reloc_roots); | |
aca1bba6 | 2639 | goto out; |
b37b39cd | 2640 | } |
3fd0a558 YZ |
2641 | } else { |
2642 | list_del_init(&reloc_root->root_list); | |
30d40577 QW |
2643 | /* Don't forget to queue this reloc root for cleanup */ |
2644 | list_add_tail(&reloc_root->reloc_dirty_list, | |
2645 | &rc->dirty_subvol_roots); | |
3fd0a558 | 2646 | } |
5d4f98a2 YZ |
2647 | } |
2648 | ||
3fd0a558 YZ |
2649 | if (found) { |
2650 | found = 0; | |
2651 | goto again; | |
2652 | } | |
aca1bba6 LB |
2653 | out: |
2654 | if (ret) { | |
0b246afa | 2655 | btrfs_handle_fs_error(fs_info, ret, NULL); |
aca1bba6 LB |
2656 | if (!list_empty(&reloc_roots)) |
2657 | free_reloc_roots(&reloc_roots); | |
467bb1d2 WS |
2658 | |
2659 | /* new reloc root may be added */ | |
0b246afa | 2660 | mutex_lock(&fs_info->reloc_mutex); |
467bb1d2 | 2661 | list_splice_init(&rc->reloc_roots, &reloc_roots); |
0b246afa | 2662 | mutex_unlock(&fs_info->reloc_mutex); |
467bb1d2 WS |
2663 | if (!list_empty(&reloc_roots)) |
2664 | free_reloc_roots(&reloc_roots); | |
aca1bba6 LB |
2665 | } |
2666 | ||
7b7b7431 JB |
2667 | /* |
2668 | * We used to have | |
2669 | * | |
2670 | * BUG_ON(!RB_EMPTY_ROOT(&rc->reloc_root_tree.rb_root)); | |
2671 | * | |
2672 | * here, but it's wrong. If we fail to start the transaction in | |
2673 | * prepare_to_merge() we will have only 0 ref reloc roots, none of which | |
2674 | * have actually been removed from the reloc_root_tree rb tree. This is | |
2675 | * fine because we're bailing here, and we hold a reference on the root | |
2676 | * for the list that holds it, so these roots will be cleaned up when we | |
2677 | * do the reloc_dirty_list afterwards. Meanwhile the root->reloc_root | |
2678 | * will be cleaned up on unmount. | |
2679 | * | |
2680 | * The remaining nodes will be cleaned up by free_reloc_control. | |
2681 | */ | |
5d4f98a2 YZ |
2682 | } |
2683 | ||
2684 | static void free_block_list(struct rb_root *blocks) | |
2685 | { | |
2686 | struct tree_block *block; | |
2687 | struct rb_node *rb_node; | |
2688 | while ((rb_node = rb_first(blocks))) { | |
2689 | block = rb_entry(rb_node, struct tree_block, rb_node); | |
2690 | rb_erase(rb_node, blocks); | |
2691 | kfree(block); | |
2692 | } | |
2693 | } | |
2694 | ||
2695 | static int record_reloc_root_in_trans(struct btrfs_trans_handle *trans, | |
2696 | struct btrfs_root *reloc_root) | |
2697 | { | |
0b246afa | 2698 | struct btrfs_fs_info *fs_info = reloc_root->fs_info; |
5d4f98a2 | 2699 | struct btrfs_root *root; |
442b1ac5 | 2700 | int ret; |
5d4f98a2 YZ |
2701 | |
2702 | if (reloc_root->last_trans == trans->transid) | |
2703 | return 0; | |
2704 | ||
0b246afa | 2705 | root = read_fs_root(fs_info, reloc_root->root_key.offset); |
5d4f98a2 YZ |
2706 | BUG_ON(IS_ERR(root)); |
2707 | BUG_ON(root->reloc_root != reloc_root); | |
442b1ac5 | 2708 | ret = btrfs_record_root_in_trans(trans, root); |
00246528 | 2709 | btrfs_put_root(root); |
5d4f98a2 | 2710 | |
442b1ac5 | 2711 | return ret; |
5d4f98a2 YZ |
2712 | } |
2713 | ||
3fd0a558 YZ |
2714 | static noinline_for_stack |
2715 | struct btrfs_root *select_reloc_root(struct btrfs_trans_handle *trans, | |
2716 | struct reloc_control *rc, | |
2717 | struct backref_node *node, | |
dc4103f9 | 2718 | struct backref_edge *edges[]) |
5d4f98a2 YZ |
2719 | { |
2720 | struct backref_node *next; | |
2721 | struct btrfs_root *root; | |
3fd0a558 YZ |
2722 | int index = 0; |
2723 | ||
5d4f98a2 YZ |
2724 | next = node; |
2725 | while (1) { | |
2726 | cond_resched(); | |
2727 | next = walk_up_backref(next, edges, &index); | |
2728 | root = next->root; | |
3fd0a558 | 2729 | BUG_ON(!root); |
27cdeb70 | 2730 | BUG_ON(!test_bit(BTRFS_ROOT_REF_COWS, &root->state)); |
5d4f98a2 YZ |
2731 | |
2732 | if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) { | |
2733 | record_reloc_root_in_trans(trans, root); | |
2734 | break; | |
2735 | } | |
2736 | ||
3fd0a558 YZ |
2737 | btrfs_record_root_in_trans(trans, root); |
2738 | root = root->reloc_root; | |
2739 | ||
2740 | if (next->new_bytenr != root->node->start) { | |
2741 | BUG_ON(next->new_bytenr); | |
2742 | BUG_ON(!list_empty(&next->list)); | |
2743 | next->new_bytenr = root->node->start; | |
00246528 JB |
2744 | btrfs_put_root(next->root); |
2745 | next->root = btrfs_grab_root(root); | |
0b530bc5 | 2746 | ASSERT(next->root); |
3fd0a558 YZ |
2747 | list_add_tail(&next->list, |
2748 | &rc->backref_cache.changed); | |
2749 | __mark_block_processed(rc, next); | |
5d4f98a2 YZ |
2750 | break; |
2751 | } | |
2752 | ||
3fd0a558 | 2753 | WARN_ON(1); |
5d4f98a2 YZ |
2754 | root = NULL; |
2755 | next = walk_down_backref(edges, &index); | |
2756 | if (!next || next->level <= node->level) | |
2757 | break; | |
2758 | } | |
3fd0a558 YZ |
2759 | if (!root) |
2760 | return NULL; | |
5d4f98a2 | 2761 | |
3fd0a558 YZ |
2762 | next = node; |
2763 | /* setup backref node path for btrfs_reloc_cow_block */ | |
2764 | while (1) { | |
2765 | rc->backref_cache.path[next->level] = next; | |
2766 | if (--index < 0) | |
2767 | break; | |
2768 | next = edges[index]->node[UPPER]; | |
5d4f98a2 | 2769 | } |
5d4f98a2 YZ |
2770 | return root; |
2771 | } | |
2772 | ||
3fd0a558 YZ |
2773 | /* |
2774 | * select a tree root for relocation. return NULL if the block | |
2775 | * is reference counted. we should use do_relocation() in this | |
2776 | * case. return a tree root pointer if the block isn't reference | |
2777 | * counted. return -ENOENT if the block is root of reloc tree. | |
2778 | */ | |
5d4f98a2 | 2779 | static noinline_for_stack |
147d256e | 2780 | struct btrfs_root *select_one_root(struct backref_node *node) |
5d4f98a2 | 2781 | { |
3fd0a558 YZ |
2782 | struct backref_node *next; |
2783 | struct btrfs_root *root; | |
2784 | struct btrfs_root *fs_root = NULL; | |
5d4f98a2 | 2785 | struct backref_edge *edges[BTRFS_MAX_LEVEL - 1]; |
3fd0a558 YZ |
2786 | int index = 0; |
2787 | ||
2788 | next = node; | |
2789 | while (1) { | |
2790 | cond_resched(); | |
2791 | next = walk_up_backref(next, edges, &index); | |
2792 | root = next->root; | |
2793 | BUG_ON(!root); | |
2794 | ||
25985edc | 2795 | /* no other choice for non-references counted tree */ |
27cdeb70 | 2796 | if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state)) |
3fd0a558 YZ |
2797 | return root; |
2798 | ||
2799 | if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) | |
2800 | fs_root = root; | |
2801 | ||
2802 | if (next != node) | |
2803 | return NULL; | |
2804 | ||
2805 | next = walk_down_backref(edges, &index); | |
2806 | if (!next || next->level <= node->level) | |
2807 | break; | |
2808 | } | |
2809 | ||
2810 | if (!fs_root) | |
2811 | return ERR_PTR(-ENOENT); | |
2812 | return fs_root; | |
5d4f98a2 YZ |
2813 | } |
2814 | ||
2815 | static noinline_for_stack | |
3fd0a558 YZ |
2816 | u64 calcu_metadata_size(struct reloc_control *rc, |
2817 | struct backref_node *node, int reserve) | |
5d4f98a2 | 2818 | { |
0b246afa | 2819 | struct btrfs_fs_info *fs_info = rc->extent_root->fs_info; |
3fd0a558 YZ |
2820 | struct backref_node *next = node; |
2821 | struct backref_edge *edge; | |
2822 | struct backref_edge *edges[BTRFS_MAX_LEVEL - 1]; | |
2823 | u64 num_bytes = 0; | |
2824 | int index = 0; | |
2825 | ||
2826 | BUG_ON(reserve && node->processed); | |
2827 | ||
2828 | while (next) { | |
2829 | cond_resched(); | |
2830 | while (1) { | |
2831 | if (next->processed && (reserve || next != node)) | |
2832 | break; | |
2833 | ||
0b246afa | 2834 | num_bytes += fs_info->nodesize; |
3fd0a558 YZ |
2835 | |
2836 | if (list_empty(&next->upper)) | |
2837 | break; | |
2838 | ||
2839 | edge = list_entry(next->upper.next, | |
2840 | struct backref_edge, list[LOWER]); | |
2841 | edges[index++] = edge; | |
2842 | next = edge->node[UPPER]; | |
2843 | } | |
2844 | next = walk_down_backref(edges, &index); | |
2845 | } | |
2846 | return num_bytes; | |
5d4f98a2 YZ |
2847 | } |
2848 | ||
3fd0a558 YZ |
2849 | static int reserve_metadata_space(struct btrfs_trans_handle *trans, |
2850 | struct reloc_control *rc, | |
2851 | struct backref_node *node) | |
5d4f98a2 | 2852 | { |
3fd0a558 | 2853 | struct btrfs_root *root = rc->extent_root; |
da17066c | 2854 | struct btrfs_fs_info *fs_info = root->fs_info; |
3fd0a558 YZ |
2855 | u64 num_bytes; |
2856 | int ret; | |
0647bf56 | 2857 | u64 tmp; |
3fd0a558 YZ |
2858 | |
2859 | num_bytes = calcu_metadata_size(rc, node, 1) * 2; | |
5d4f98a2 | 2860 | |
3fd0a558 | 2861 | trans->block_rsv = rc->block_rsv; |
0647bf56 | 2862 | rc->reserved_bytes += num_bytes; |
8ca17f0f JB |
2863 | |
2864 | /* | |
2865 | * We are under a transaction here so we can only do limited flushing. | |
2866 | * If we get an enospc just kick back -EAGAIN so we know to drop the | |
2867 | * transaction and try to refill when we can flush all the things. | |
2868 | */ | |
0647bf56 | 2869 | ret = btrfs_block_rsv_refill(root, rc->block_rsv, num_bytes, |
8ca17f0f | 2870 | BTRFS_RESERVE_FLUSH_LIMIT); |
3fd0a558 | 2871 | if (ret) { |
da17066c | 2872 | tmp = fs_info->nodesize * RELOCATION_RESERVED_NODES; |
8ca17f0f JB |
2873 | while (tmp <= rc->reserved_bytes) |
2874 | tmp <<= 1; | |
2875 | /* | |
2876 | * only one thread can access block_rsv at this point, | |
2877 | * so we don't need hold lock to protect block_rsv. | |
2878 | * we expand more reservation size here to allow enough | |
52042d8e | 2879 | * space for relocation and we will return earlier in |
8ca17f0f JB |
2880 | * enospc case. |
2881 | */ | |
da17066c JM |
2882 | rc->block_rsv->size = tmp + fs_info->nodesize * |
2883 | RELOCATION_RESERVED_NODES; | |
8ca17f0f | 2884 | return -EAGAIN; |
5d4f98a2 | 2885 | } |
3fd0a558 | 2886 | |
3fd0a558 YZ |
2887 | return 0; |
2888 | } | |
2889 | ||
5d4f98a2 YZ |
2890 | /* |
2891 | * relocate a block tree, and then update pointers in upper level | |
2892 | * blocks that reference the block to point to the new location. | |
2893 | * | |
2894 | * if called by link_to_upper, the block has already been relocated. | |
2895 | * in that case this function just updates pointers. | |
2896 | */ | |
2897 | static int do_relocation(struct btrfs_trans_handle *trans, | |
3fd0a558 | 2898 | struct reloc_control *rc, |
5d4f98a2 YZ |
2899 | struct backref_node *node, |
2900 | struct btrfs_key *key, | |
2901 | struct btrfs_path *path, int lowest) | |
2902 | { | |
2ff7e61e | 2903 | struct btrfs_fs_info *fs_info = rc->extent_root->fs_info; |
5d4f98a2 YZ |
2904 | struct backref_node *upper; |
2905 | struct backref_edge *edge; | |
2906 | struct backref_edge *edges[BTRFS_MAX_LEVEL - 1]; | |
2907 | struct btrfs_root *root; | |
2908 | struct extent_buffer *eb; | |
2909 | u32 blocksize; | |
2910 | u64 bytenr; | |
2911 | u64 generation; | |
5d4f98a2 YZ |
2912 | int slot; |
2913 | int ret; | |
2914 | int err = 0; | |
2915 | ||
2916 | BUG_ON(lowest && node->eb); | |
2917 | ||
2918 | path->lowest_level = node->level + 1; | |
3fd0a558 | 2919 | rc->backref_cache.path[node->level] = node; |
5d4f98a2 | 2920 | list_for_each_entry(edge, &node->upper, list[LOWER]) { |
581c1760 | 2921 | struct btrfs_key first_key; |
82fa113f | 2922 | struct btrfs_ref ref = { 0 }; |
581c1760 | 2923 | |
5d4f98a2 | 2924 | cond_resched(); |
5d4f98a2 YZ |
2925 | |
2926 | upper = edge->node[UPPER]; | |
dc4103f9 | 2927 | root = select_reloc_root(trans, rc, upper, edges); |
3fd0a558 YZ |
2928 | BUG_ON(!root); |
2929 | ||
2930 | if (upper->eb && !upper->locked) { | |
2931 | if (!lowest) { | |
2932 | ret = btrfs_bin_search(upper->eb, key, | |
2933 | upper->level, &slot); | |
cbca7d59 FM |
2934 | if (ret < 0) { |
2935 | err = ret; | |
2936 | goto next; | |
2937 | } | |
3fd0a558 YZ |
2938 | BUG_ON(ret); |
2939 | bytenr = btrfs_node_blockptr(upper->eb, slot); | |
2940 | if (node->eb->start == bytenr) | |
2941 | goto next; | |
2942 | } | |
5d4f98a2 | 2943 | drop_node_buffer(upper); |
3fd0a558 | 2944 | } |
5d4f98a2 YZ |
2945 | |
2946 | if (!upper->eb) { | |
2947 | ret = btrfs_search_slot(trans, root, key, path, 0, 1); | |
3561b9db LB |
2948 | if (ret) { |
2949 | if (ret < 0) | |
2950 | err = ret; | |
2951 | else | |
2952 | err = -ENOENT; | |
2953 | ||
2954 | btrfs_release_path(path); | |
5d4f98a2 YZ |
2955 | break; |
2956 | } | |
5d4f98a2 | 2957 | |
3fd0a558 YZ |
2958 | if (!upper->eb) { |
2959 | upper->eb = path->nodes[upper->level]; | |
2960 | path->nodes[upper->level] = NULL; | |
2961 | } else { | |
2962 | BUG_ON(upper->eb != path->nodes[upper->level]); | |
2963 | } | |
5d4f98a2 | 2964 | |
3fd0a558 YZ |
2965 | upper->locked = 1; |
2966 | path->locks[upper->level] = 0; | |
5d4f98a2 | 2967 | |
3fd0a558 | 2968 | slot = path->slots[upper->level]; |
b3b4aa74 | 2969 | btrfs_release_path(path); |
5d4f98a2 YZ |
2970 | } else { |
2971 | ret = btrfs_bin_search(upper->eb, key, upper->level, | |
2972 | &slot); | |
cbca7d59 FM |
2973 | if (ret < 0) { |
2974 | err = ret; | |
2975 | goto next; | |
2976 | } | |
5d4f98a2 YZ |
2977 | BUG_ON(ret); |
2978 | } | |
2979 | ||
2980 | bytenr = btrfs_node_blockptr(upper->eb, slot); | |
3fd0a558 | 2981 | if (lowest) { |
4547f4d8 LB |
2982 | if (bytenr != node->bytenr) { |
2983 | btrfs_err(root->fs_info, | |
2984 | "lowest leaf/node mismatch: bytenr %llu node->bytenr %llu slot %d upper %llu", | |
2985 | bytenr, node->bytenr, slot, | |
2986 | upper->eb->start); | |
2987 | err = -EIO; | |
2988 | goto next; | |
2989 | } | |
5d4f98a2 | 2990 | } else { |
3fd0a558 YZ |
2991 | if (node->eb->start == bytenr) |
2992 | goto next; | |
5d4f98a2 YZ |
2993 | } |
2994 | ||
da17066c | 2995 | blocksize = root->fs_info->nodesize; |
5d4f98a2 | 2996 | generation = btrfs_node_ptr_generation(upper->eb, slot); |
581c1760 QW |
2997 | btrfs_node_key_to_cpu(upper->eb, &first_key, slot); |
2998 | eb = read_tree_block(fs_info, bytenr, generation, | |
2999 | upper->level - 1, &first_key); | |
64c043de LB |
3000 | if (IS_ERR(eb)) { |
3001 | err = PTR_ERR(eb); | |
3002 | goto next; | |
3003 | } else if (!extent_buffer_uptodate(eb)) { | |
416bc658 | 3004 | free_extent_buffer(eb); |
97d9a8a4 TI |
3005 | err = -EIO; |
3006 | goto next; | |
3007 | } | |
5d4f98a2 | 3008 | btrfs_tree_lock(eb); |
8bead258 | 3009 | btrfs_set_lock_blocking_write(eb); |
5d4f98a2 YZ |
3010 | |
3011 | if (!node->eb) { | |
3012 | ret = btrfs_cow_block(trans, root, eb, upper->eb, | |
3013 | slot, &eb); | |
3fd0a558 YZ |
3014 | btrfs_tree_unlock(eb); |
3015 | free_extent_buffer(eb); | |
5d4f98a2 YZ |
3016 | if (ret < 0) { |
3017 | err = ret; | |
3fd0a558 | 3018 | goto next; |
5d4f98a2 | 3019 | } |
3fd0a558 | 3020 | BUG_ON(node->eb != eb); |
5d4f98a2 YZ |
3021 | } else { |
3022 | btrfs_set_node_blockptr(upper->eb, slot, | |
3023 | node->eb->start); | |
3024 | btrfs_set_node_ptr_generation(upper->eb, slot, | |
3025 | trans->transid); | |
3026 | btrfs_mark_buffer_dirty(upper->eb); | |
3027 | ||
82fa113f QW |
3028 | btrfs_init_generic_ref(&ref, BTRFS_ADD_DELAYED_REF, |
3029 | node->eb->start, blocksize, | |
3030 | upper->eb->start); | |
3031 | ref.real_root = root->root_key.objectid; | |
3032 | btrfs_init_tree_ref(&ref, node->level, | |
3033 | btrfs_header_owner(upper->eb)); | |
3034 | ret = btrfs_inc_extent_ref(trans, &ref); | |
5d4f98a2 YZ |
3035 | BUG_ON(ret); |
3036 | ||
3037 | ret = btrfs_drop_subtree(trans, root, eb, upper->eb); | |
3038 | BUG_ON(ret); | |
5d4f98a2 | 3039 | } |
3fd0a558 YZ |
3040 | next: |
3041 | if (!upper->pending) | |
3042 | drop_node_buffer(upper); | |
3043 | else | |
3044 | unlock_node_buffer(upper); | |
3045 | if (err) | |
3046 | break; | |
5d4f98a2 | 3047 | } |
3fd0a558 YZ |
3048 | |
3049 | if (!err && node->pending) { | |
3050 | drop_node_buffer(node); | |
3051 | list_move_tail(&node->list, &rc->backref_cache.changed); | |
3052 | node->pending = 0; | |
3053 | } | |
3054 | ||
5d4f98a2 | 3055 | path->lowest_level = 0; |
3fd0a558 | 3056 | BUG_ON(err == -ENOSPC); |
5d4f98a2 YZ |
3057 | return err; |
3058 | } | |
3059 | ||
3060 | static int link_to_upper(struct btrfs_trans_handle *trans, | |
3fd0a558 | 3061 | struct reloc_control *rc, |
5d4f98a2 YZ |
3062 | struct backref_node *node, |
3063 | struct btrfs_path *path) | |
3064 | { | |
3065 | struct btrfs_key key; | |
5d4f98a2 YZ |
3066 | |
3067 | btrfs_node_key_to_cpu(node->eb, &key, 0); | |
3fd0a558 | 3068 | return do_relocation(trans, rc, node, &key, path, 0); |
5d4f98a2 YZ |
3069 | } |
3070 | ||
3071 | static int finish_pending_nodes(struct btrfs_trans_handle *trans, | |
3fd0a558 YZ |
3072 | struct reloc_control *rc, |
3073 | struct btrfs_path *path, int err) | |
5d4f98a2 | 3074 | { |
3fd0a558 YZ |
3075 | LIST_HEAD(list); |
3076 | struct backref_cache *cache = &rc->backref_cache; | |
5d4f98a2 YZ |
3077 | struct backref_node *node; |
3078 | int level; | |
3079 | int ret; | |
5d4f98a2 YZ |
3080 | |
3081 | for (level = 0; level < BTRFS_MAX_LEVEL; level++) { | |
3082 | while (!list_empty(&cache->pending[level])) { | |
3083 | node = list_entry(cache->pending[level].next, | |
3fd0a558 YZ |
3084 | struct backref_node, list); |
3085 | list_move_tail(&node->list, &list); | |
3086 | BUG_ON(!node->pending); | |
5d4f98a2 | 3087 | |
3fd0a558 YZ |
3088 | if (!err) { |
3089 | ret = link_to_upper(trans, rc, node, path); | |
3090 | if (ret < 0) | |
3091 | err = ret; | |
3092 | } | |
5d4f98a2 | 3093 | } |
3fd0a558 | 3094 | list_splice_init(&list, &cache->pending[level]); |
5d4f98a2 | 3095 | } |
5d4f98a2 YZ |
3096 | return err; |
3097 | } | |
3098 | ||
3099 | static void mark_block_processed(struct reloc_control *rc, | |
3fd0a558 YZ |
3100 | u64 bytenr, u32 blocksize) |
3101 | { | |
3102 | set_extent_bits(&rc->processed_blocks, bytenr, bytenr + blocksize - 1, | |
ceeb0ae7 | 3103 | EXTENT_DIRTY); |
3fd0a558 YZ |
3104 | } |
3105 | ||
3106 | static void __mark_block_processed(struct reloc_control *rc, | |
3107 | struct backref_node *node) | |
5d4f98a2 YZ |
3108 | { |
3109 | u32 blocksize; | |
3110 | if (node->level == 0 || | |
3111 | in_block_group(node->bytenr, rc->block_group)) { | |
da17066c | 3112 | blocksize = rc->extent_root->fs_info->nodesize; |
3fd0a558 | 3113 | mark_block_processed(rc, node->bytenr, blocksize); |
5d4f98a2 YZ |
3114 | } |
3115 | node->processed = 1; | |
3116 | } | |
3117 | ||
3118 | /* | |
3119 | * mark a block and all blocks directly/indirectly reference the block | |
3120 | * as processed. | |
3121 | */ | |
3122 | static void update_processed_blocks(struct reloc_control *rc, | |
3123 | struct backref_node *node) | |
3124 | { | |
3125 | struct backref_node *next = node; | |
3126 | struct backref_edge *edge; | |
3127 | struct backref_edge *edges[BTRFS_MAX_LEVEL - 1]; | |
3128 | int index = 0; | |
3129 | ||
3130 | while (next) { | |
3131 | cond_resched(); | |
3132 | while (1) { | |
3133 | if (next->processed) | |
3134 | break; | |
3135 | ||
3fd0a558 | 3136 | __mark_block_processed(rc, next); |
5d4f98a2 YZ |
3137 | |
3138 | if (list_empty(&next->upper)) | |
3139 | break; | |
3140 | ||
3141 | edge = list_entry(next->upper.next, | |
3142 | struct backref_edge, list[LOWER]); | |
3143 | edges[index++] = edge; | |
3144 | next = edge->node[UPPER]; | |
3145 | } | |
3146 | next = walk_down_backref(edges, &index); | |
3147 | } | |
3148 | } | |
3149 | ||
7476dfda | 3150 | static int tree_block_processed(u64 bytenr, struct reloc_control *rc) |
3fd0a558 | 3151 | { |
da17066c | 3152 | u32 blocksize = rc->extent_root->fs_info->nodesize; |
7476dfda | 3153 | |
3fd0a558 YZ |
3154 | if (test_range_bit(&rc->processed_blocks, bytenr, |
3155 | bytenr + blocksize - 1, EXTENT_DIRTY, 1, NULL)) | |
3156 | return 1; | |
3157 | return 0; | |
5d4f98a2 YZ |
3158 | } |
3159 | ||
2ff7e61e | 3160 | static int get_tree_block_key(struct btrfs_fs_info *fs_info, |
5d4f98a2 YZ |
3161 | struct tree_block *block) |
3162 | { | |
3163 | struct extent_buffer *eb; | |
3164 | ||
581c1760 QW |
3165 | eb = read_tree_block(fs_info, block->bytenr, block->key.offset, |
3166 | block->level, NULL); | |
64c043de LB |
3167 | if (IS_ERR(eb)) { |
3168 | return PTR_ERR(eb); | |
3169 | } else if (!extent_buffer_uptodate(eb)) { | |
416bc658 JB |
3170 | free_extent_buffer(eb); |
3171 | return -EIO; | |
3172 | } | |
5d4f98a2 YZ |
3173 | if (block->level == 0) |
3174 | btrfs_item_key_to_cpu(eb, &block->key, 0); | |
3175 | else | |
3176 | btrfs_node_key_to_cpu(eb, &block->key, 0); | |
3177 | free_extent_buffer(eb); | |
3178 | block->key_ready = 1; | |
3179 | return 0; | |
3180 | } | |
3181 | ||
5d4f98a2 YZ |
3182 | /* |
3183 | * helper function to relocate a tree block | |
3184 | */ | |
3185 | static int relocate_tree_block(struct btrfs_trans_handle *trans, | |
3186 | struct reloc_control *rc, | |
3187 | struct backref_node *node, | |
3188 | struct btrfs_key *key, | |
3189 | struct btrfs_path *path) | |
3190 | { | |
3191 | struct btrfs_root *root; | |
3fd0a558 YZ |
3192 | int ret = 0; |
3193 | ||
3194 | if (!node) | |
3195 | return 0; | |
5d4f98a2 | 3196 | |
5f6b2e5c JB |
3197 | /* |
3198 | * If we fail here we want to drop our backref_node because we are going | |
3199 | * to start over and regenerate the tree for it. | |
3200 | */ | |
3201 | ret = reserve_metadata_space(trans, rc, node); | |
3202 | if (ret) | |
3203 | goto out; | |
3204 | ||
3fd0a558 | 3205 | BUG_ON(node->processed); |
147d256e | 3206 | root = select_one_root(node); |
3fd0a558 | 3207 | if (root == ERR_PTR(-ENOENT)) { |
5d4f98a2 | 3208 | update_processed_blocks(rc, node); |
3fd0a558 | 3209 | goto out; |
5d4f98a2 YZ |
3210 | } |
3211 | ||
3fd0a558 | 3212 | if (root) { |
27cdeb70 | 3213 | if (test_bit(BTRFS_ROOT_REF_COWS, &root->state)) { |
3fd0a558 YZ |
3214 | BUG_ON(node->new_bytenr); |
3215 | BUG_ON(!list_empty(&node->list)); | |
3216 | btrfs_record_root_in_trans(trans, root); | |
3217 | root = root->reloc_root; | |
3218 | node->new_bytenr = root->node->start; | |
00246528 JB |
3219 | btrfs_put_root(node->root); |
3220 | node->root = btrfs_grab_root(root); | |
0b530bc5 | 3221 | ASSERT(node->root); |
3fd0a558 YZ |
3222 | list_add_tail(&node->list, &rc->backref_cache.changed); |
3223 | } else { | |
3224 | path->lowest_level = node->level; | |
3225 | ret = btrfs_search_slot(trans, root, key, path, 0, 1); | |
b3b4aa74 | 3226 | btrfs_release_path(path); |
3fd0a558 YZ |
3227 | if (ret > 0) |
3228 | ret = 0; | |
3229 | } | |
3230 | if (!ret) | |
3231 | update_processed_blocks(rc, node); | |
3232 | } else { | |
3233 | ret = do_relocation(trans, rc, node, key, path, 1); | |
3234 | } | |
5d4f98a2 | 3235 | out: |
0647bf56 | 3236 | if (ret || node->level == 0 || node->cowonly) |
3fd0a558 | 3237 | remove_backref_node(&rc->backref_cache, node); |
5d4f98a2 YZ |
3238 | return ret; |
3239 | } | |
3240 | ||
3241 | /* | |
3242 | * relocate a list of blocks | |
3243 | */ | |
3244 | static noinline_for_stack | |
3245 | int relocate_tree_blocks(struct btrfs_trans_handle *trans, | |
3246 | struct reloc_control *rc, struct rb_root *blocks) | |
3247 | { | |
2ff7e61e | 3248 | struct btrfs_fs_info *fs_info = rc->extent_root->fs_info; |
5d4f98a2 YZ |
3249 | struct backref_node *node; |
3250 | struct btrfs_path *path; | |
3251 | struct tree_block *block; | |
98ff7b94 | 3252 | struct tree_block *next; |
5d4f98a2 YZ |
3253 | int ret; |
3254 | int err = 0; | |
3255 | ||
3256 | path = btrfs_alloc_path(); | |
e1a12670 LB |
3257 | if (!path) { |
3258 | err = -ENOMEM; | |
34c2b290 | 3259 | goto out_free_blocks; |
e1a12670 | 3260 | } |
5d4f98a2 | 3261 | |
98ff7b94 QW |
3262 | /* Kick in readahead for tree blocks with missing keys */ |
3263 | rbtree_postorder_for_each_entry_safe(block, next, blocks, rb_node) { | |
5d4f98a2 | 3264 | if (!block->key_ready) |
2ff7e61e | 3265 | readahead_tree_block(fs_info, block->bytenr); |
5d4f98a2 YZ |
3266 | } |
3267 | ||
98ff7b94 QW |
3268 | /* Get first keys */ |
3269 | rbtree_postorder_for_each_entry_safe(block, next, blocks, rb_node) { | |
34c2b290 | 3270 | if (!block->key_ready) { |
2ff7e61e | 3271 | err = get_tree_block_key(fs_info, block); |
34c2b290 DS |
3272 | if (err) |
3273 | goto out_free_path; | |
3274 | } | |
5d4f98a2 YZ |
3275 | } |
3276 | ||
98ff7b94 QW |
3277 | /* Do tree relocation */ |
3278 | rbtree_postorder_for_each_entry_safe(block, next, blocks, rb_node) { | |
3fd0a558 | 3279 | node = build_backref_tree(rc, &block->key, |
5d4f98a2 YZ |
3280 | block->level, block->bytenr); |
3281 | if (IS_ERR(node)) { | |
3282 | err = PTR_ERR(node); | |
3283 | goto out; | |
3284 | } | |
3285 | ||
3286 | ret = relocate_tree_block(trans, rc, node, &block->key, | |
3287 | path); | |
3288 | if (ret < 0) { | |
50dbbb71 JB |
3289 | err = ret; |
3290 | break; | |
5d4f98a2 | 3291 | } |
5d4f98a2 | 3292 | } |
5d4f98a2 | 3293 | out: |
3fd0a558 | 3294 | err = finish_pending_nodes(trans, rc, path, err); |
5d4f98a2 | 3295 | |
34c2b290 | 3296 | out_free_path: |
5d4f98a2 | 3297 | btrfs_free_path(path); |
34c2b290 | 3298 | out_free_blocks: |
e1a12670 | 3299 | free_block_list(blocks); |
5d4f98a2 YZ |
3300 | return err; |
3301 | } | |
3302 | ||
efa56464 YZ |
3303 | static noinline_for_stack |
3304 | int prealloc_file_extent_cluster(struct inode *inode, | |
3305 | struct file_extent_cluster *cluster) | |
3306 | { | |
3307 | u64 alloc_hint = 0; | |
3308 | u64 start; | |
3309 | u64 end; | |
3310 | u64 offset = BTRFS_I(inode)->index_cnt; | |
3311 | u64 num_bytes; | |
3312 | int nr = 0; | |
3313 | int ret = 0; | |
dcb40c19 WX |
3314 | u64 prealloc_start = cluster->start - offset; |
3315 | u64 prealloc_end = cluster->end - offset; | |
18513091 | 3316 | u64 cur_offset; |
364ecf36 | 3317 | struct extent_changeset *data_reserved = NULL; |
efa56464 YZ |
3318 | |
3319 | BUG_ON(cluster->start != cluster->boundary[0]); | |
5955102c | 3320 | inode_lock(inode); |
efa56464 | 3321 | |
364ecf36 | 3322 | ret = btrfs_check_data_free_space(inode, &data_reserved, prealloc_start, |
dcb40c19 | 3323 | prealloc_end + 1 - prealloc_start); |
efa56464 YZ |
3324 | if (ret) |
3325 | goto out; | |
3326 | ||
18513091 | 3327 | cur_offset = prealloc_start; |
efa56464 YZ |
3328 | while (nr < cluster->nr) { |
3329 | start = cluster->boundary[nr] - offset; | |
3330 | if (nr + 1 < cluster->nr) | |
3331 | end = cluster->boundary[nr + 1] - 1 - offset; | |
3332 | else | |
3333 | end = cluster->end - offset; | |
3334 | ||
d0082371 | 3335 | lock_extent(&BTRFS_I(inode)->io_tree, start, end); |
efa56464 | 3336 | num_bytes = end + 1 - start; |
18513091 | 3337 | if (cur_offset < start) |
bc42bda2 QW |
3338 | btrfs_free_reserved_data_space(inode, data_reserved, |
3339 | cur_offset, start - cur_offset); | |
efa56464 YZ |
3340 | ret = btrfs_prealloc_file_range(inode, 0, start, |
3341 | num_bytes, num_bytes, | |
3342 | end + 1, &alloc_hint); | |
18513091 | 3343 | cur_offset = end + 1; |
d0082371 | 3344 | unlock_extent(&BTRFS_I(inode)->io_tree, start, end); |
efa56464 YZ |
3345 | if (ret) |
3346 | break; | |
3347 | nr++; | |
3348 | } | |
18513091 | 3349 | if (cur_offset < prealloc_end) |
bc42bda2 QW |
3350 | btrfs_free_reserved_data_space(inode, data_reserved, |
3351 | cur_offset, prealloc_end + 1 - cur_offset); | |
efa56464 | 3352 | out: |
5955102c | 3353 | inode_unlock(inode); |
364ecf36 | 3354 | extent_changeset_free(data_reserved); |
efa56464 YZ |
3355 | return ret; |
3356 | } | |
3357 | ||
5d4f98a2 | 3358 | static noinline_for_stack |
0257bb82 YZ |
3359 | int setup_extent_mapping(struct inode *inode, u64 start, u64 end, |
3360 | u64 block_start) | |
3361 | { | |
0257bb82 YZ |
3362 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
3363 | struct extent_map *em; | |
3364 | int ret = 0; | |
3365 | ||
172ddd60 | 3366 | em = alloc_extent_map(); |
0257bb82 YZ |
3367 | if (!em) |
3368 | return -ENOMEM; | |
3369 | ||
3370 | em->start = start; | |
3371 | em->len = end + 1 - start; | |
3372 | em->block_len = em->len; | |
3373 | em->block_start = block_start; | |
0257bb82 YZ |
3374 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
3375 | ||
d0082371 | 3376 | lock_extent(&BTRFS_I(inode)->io_tree, start, end); |
0257bb82 YZ |
3377 | while (1) { |
3378 | write_lock(&em_tree->lock); | |
09a2a8f9 | 3379 | ret = add_extent_mapping(em_tree, em, 0); |
0257bb82 YZ |
3380 | write_unlock(&em_tree->lock); |
3381 | if (ret != -EEXIST) { | |
3382 | free_extent_map(em); | |
3383 | break; | |
3384 | } | |
dcdbc059 | 3385 | btrfs_drop_extent_cache(BTRFS_I(inode), start, end, 0); |
0257bb82 | 3386 | } |
d0082371 | 3387 | unlock_extent(&BTRFS_I(inode)->io_tree, start, end); |
0257bb82 YZ |
3388 | return ret; |
3389 | } | |
3390 | ||
726a3421 QW |
3391 | /* |
3392 | * Allow error injection to test balance cancellation | |
3393 | */ | |
3394 | int btrfs_should_cancel_balance(struct btrfs_fs_info *fs_info) | |
3395 | { | |
3396 | return atomic_read(&fs_info->balance_cancel_req); | |
3397 | } | |
3398 | ALLOW_ERROR_INJECTION(btrfs_should_cancel_balance, TRUE); | |
3399 | ||
0257bb82 YZ |
3400 | static int relocate_file_extent_cluster(struct inode *inode, |
3401 | struct file_extent_cluster *cluster) | |
5d4f98a2 | 3402 | { |
2ff7e61e | 3403 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
5d4f98a2 YZ |
3404 | u64 page_start; |
3405 | u64 page_end; | |
0257bb82 YZ |
3406 | u64 offset = BTRFS_I(inode)->index_cnt; |
3407 | unsigned long index; | |
5d4f98a2 | 3408 | unsigned long last_index; |
5d4f98a2 YZ |
3409 | struct page *page; |
3410 | struct file_ra_state *ra; | |
3b16a4e3 | 3411 | gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping); |
0257bb82 | 3412 | int nr = 0; |
5d4f98a2 YZ |
3413 | int ret = 0; |
3414 | ||
0257bb82 YZ |
3415 | if (!cluster->nr) |
3416 | return 0; | |
3417 | ||
5d4f98a2 YZ |
3418 | ra = kzalloc(sizeof(*ra), GFP_NOFS); |
3419 | if (!ra) | |
3420 | return -ENOMEM; | |
3421 | ||
efa56464 YZ |
3422 | ret = prealloc_file_extent_cluster(inode, cluster); |
3423 | if (ret) | |
3424 | goto out; | |
0257bb82 | 3425 | |
efa56464 | 3426 | file_ra_state_init(ra, inode->i_mapping); |
5d4f98a2 | 3427 | |
0257bb82 YZ |
3428 | ret = setup_extent_mapping(inode, cluster->start - offset, |
3429 | cluster->end - offset, cluster->start); | |
5d4f98a2 | 3430 | if (ret) |
efa56464 | 3431 | goto out; |
5d4f98a2 | 3432 | |
09cbfeaf KS |
3433 | index = (cluster->start - offset) >> PAGE_SHIFT; |
3434 | last_index = (cluster->end - offset) >> PAGE_SHIFT; | |
0257bb82 | 3435 | while (index <= last_index) { |
9f3db423 NB |
3436 | ret = btrfs_delalloc_reserve_metadata(BTRFS_I(inode), |
3437 | PAGE_SIZE); | |
efa56464 YZ |
3438 | if (ret) |
3439 | goto out; | |
3440 | ||
0257bb82 | 3441 | page = find_lock_page(inode->i_mapping, index); |
5d4f98a2 | 3442 | if (!page) { |
0257bb82 YZ |
3443 | page_cache_sync_readahead(inode->i_mapping, |
3444 | ra, NULL, index, | |
3445 | last_index + 1 - index); | |
a94733d0 | 3446 | page = find_or_create_page(inode->i_mapping, index, |
3b16a4e3 | 3447 | mask); |
0257bb82 | 3448 | if (!page) { |
691fa059 | 3449 | btrfs_delalloc_release_metadata(BTRFS_I(inode), |
43b18595 | 3450 | PAGE_SIZE, true); |
44db1216 | 3451 | btrfs_delalloc_release_extents(BTRFS_I(inode), |
8702ba93 | 3452 | PAGE_SIZE); |
0257bb82 | 3453 | ret = -ENOMEM; |
efa56464 | 3454 | goto out; |
0257bb82 | 3455 | } |
5d4f98a2 | 3456 | } |
0257bb82 YZ |
3457 | |
3458 | if (PageReadahead(page)) { | |
3459 | page_cache_async_readahead(inode->i_mapping, | |
3460 | ra, NULL, page, index, | |
3461 | last_index + 1 - index); | |
3462 | } | |
3463 | ||
5d4f98a2 YZ |
3464 | if (!PageUptodate(page)) { |
3465 | btrfs_readpage(NULL, page); | |
3466 | lock_page(page); | |
3467 | if (!PageUptodate(page)) { | |
3468 | unlock_page(page); | |
09cbfeaf | 3469 | put_page(page); |
691fa059 | 3470 | btrfs_delalloc_release_metadata(BTRFS_I(inode), |
43b18595 | 3471 | PAGE_SIZE, true); |
8b62f87b | 3472 | btrfs_delalloc_release_extents(BTRFS_I(inode), |
8702ba93 | 3473 | PAGE_SIZE); |
5d4f98a2 | 3474 | ret = -EIO; |
efa56464 | 3475 | goto out; |
5d4f98a2 YZ |
3476 | } |
3477 | } | |
5d4f98a2 | 3478 | |
4eee4fa4 | 3479 | page_start = page_offset(page); |
09cbfeaf | 3480 | page_end = page_start + PAGE_SIZE - 1; |
0257bb82 | 3481 | |
d0082371 | 3482 | lock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end); |
0257bb82 | 3483 | |
5d4f98a2 YZ |
3484 | set_page_extent_mapped(page); |
3485 | ||
0257bb82 YZ |
3486 | if (nr < cluster->nr && |
3487 | page_start + offset == cluster->boundary[nr]) { | |
3488 | set_extent_bits(&BTRFS_I(inode)->io_tree, | |
3489 | page_start, page_end, | |
ceeb0ae7 | 3490 | EXTENT_BOUNDARY); |
0257bb82 YZ |
3491 | nr++; |
3492 | } | |
5d4f98a2 | 3493 | |
765f3ceb | 3494 | ret = btrfs_set_extent_delalloc(inode, page_start, page_end, 0, |
330a5827 | 3495 | NULL); |
765f3ceb NB |
3496 | if (ret) { |
3497 | unlock_page(page); | |
3498 | put_page(page); | |
3499 | btrfs_delalloc_release_metadata(BTRFS_I(inode), | |
43b18595 | 3500 | PAGE_SIZE, true); |
765f3ceb | 3501 | btrfs_delalloc_release_extents(BTRFS_I(inode), |
8702ba93 | 3502 | PAGE_SIZE); |
765f3ceb NB |
3503 | |
3504 | clear_extent_bits(&BTRFS_I(inode)->io_tree, | |
3505 | page_start, page_end, | |
3506 | EXTENT_LOCKED | EXTENT_BOUNDARY); | |
3507 | goto out; | |
3508 | ||
3509 | } | |
5d4f98a2 | 3510 | set_page_dirty(page); |
5d4f98a2 | 3511 | |
0257bb82 | 3512 | unlock_extent(&BTRFS_I(inode)->io_tree, |
d0082371 | 3513 | page_start, page_end); |
5d4f98a2 | 3514 | unlock_page(page); |
09cbfeaf | 3515 | put_page(page); |
0257bb82 YZ |
3516 | |
3517 | index++; | |
8702ba93 | 3518 | btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE); |
efa56464 | 3519 | balance_dirty_pages_ratelimited(inode->i_mapping); |
2ff7e61e | 3520 | btrfs_throttle(fs_info); |
7f913c7c QW |
3521 | if (btrfs_should_cancel_balance(fs_info)) { |
3522 | ret = -ECANCELED; | |
3523 | goto out; | |
3524 | } | |
5d4f98a2 | 3525 | } |
0257bb82 | 3526 | WARN_ON(nr != cluster->nr); |
efa56464 | 3527 | out: |
5d4f98a2 | 3528 | kfree(ra); |
5d4f98a2 YZ |
3529 | return ret; |
3530 | } | |
3531 | ||
3532 | static noinline_for_stack | |
0257bb82 YZ |
3533 | int relocate_data_extent(struct inode *inode, struct btrfs_key *extent_key, |
3534 | struct file_extent_cluster *cluster) | |
5d4f98a2 | 3535 | { |
0257bb82 | 3536 | int ret; |
5d4f98a2 | 3537 | |
0257bb82 YZ |
3538 | if (cluster->nr > 0 && extent_key->objectid != cluster->end + 1) { |
3539 | ret = relocate_file_extent_cluster(inode, cluster); | |
3540 | if (ret) | |
3541 | return ret; | |
3542 | cluster->nr = 0; | |
5d4f98a2 | 3543 | } |
5d4f98a2 | 3544 | |
0257bb82 YZ |
3545 | if (!cluster->nr) |
3546 | cluster->start = extent_key->objectid; | |
3547 | else | |
3548 | BUG_ON(cluster->nr >= MAX_EXTENTS); | |
3549 | cluster->end = extent_key->objectid + extent_key->offset - 1; | |
3550 | cluster->boundary[cluster->nr] = extent_key->objectid; | |
3551 | cluster->nr++; | |
3552 | ||
3553 | if (cluster->nr >= MAX_EXTENTS) { | |
3554 | ret = relocate_file_extent_cluster(inode, cluster); | |
3555 | if (ret) | |
3556 | return ret; | |
3557 | cluster->nr = 0; | |
3558 | } | |
3559 | return 0; | |
5d4f98a2 YZ |
3560 | } |
3561 | ||
5d4f98a2 YZ |
3562 | /* |
3563 | * helper to add a tree block to the list. | |
3564 | * the major work is getting the generation and level of the block | |
3565 | */ | |
3566 | static int add_tree_block(struct reloc_control *rc, | |
3567 | struct btrfs_key *extent_key, | |
3568 | struct btrfs_path *path, | |
3569 | struct rb_root *blocks) | |
3570 | { | |
3571 | struct extent_buffer *eb; | |
3572 | struct btrfs_extent_item *ei; | |
3573 | struct btrfs_tree_block_info *bi; | |
3574 | struct tree_block *block; | |
3575 | struct rb_node *rb_node; | |
3576 | u32 item_size; | |
3577 | int level = -1; | |
7fdf4b60 | 3578 | u64 generation; |
5d4f98a2 YZ |
3579 | |
3580 | eb = path->nodes[0]; | |
3581 | item_size = btrfs_item_size_nr(eb, path->slots[0]); | |
3582 | ||
3173a18f JB |
3583 | if (extent_key->type == BTRFS_METADATA_ITEM_KEY || |
3584 | item_size >= sizeof(*ei) + sizeof(*bi)) { | |
5d4f98a2 YZ |
3585 | ei = btrfs_item_ptr(eb, path->slots[0], |
3586 | struct btrfs_extent_item); | |
3173a18f JB |
3587 | if (extent_key->type == BTRFS_EXTENT_ITEM_KEY) { |
3588 | bi = (struct btrfs_tree_block_info *)(ei + 1); | |
3589 | level = btrfs_tree_block_level(eb, bi); | |
3590 | } else { | |
3591 | level = (int)extent_key->offset; | |
3592 | } | |
5d4f98a2 | 3593 | generation = btrfs_extent_generation(eb, ei); |
6d8ff4e4 | 3594 | } else if (unlikely(item_size == sizeof(struct btrfs_extent_item_v0))) { |
ba3c2b19 NB |
3595 | btrfs_print_v0_err(eb->fs_info); |
3596 | btrfs_handle_fs_error(eb->fs_info, -EINVAL, NULL); | |
3597 | return -EINVAL; | |
5d4f98a2 | 3598 | } else { |
5d4f98a2 | 3599 | BUG(); |
5d4f98a2 YZ |
3600 | } |
3601 | ||
b3b4aa74 | 3602 | btrfs_release_path(path); |
5d4f98a2 YZ |
3603 | |
3604 | BUG_ON(level == -1); | |
3605 | ||
3606 | block = kmalloc(sizeof(*block), GFP_NOFS); | |
3607 | if (!block) | |
3608 | return -ENOMEM; | |
3609 | ||
3610 | block->bytenr = extent_key->objectid; | |
da17066c | 3611 | block->key.objectid = rc->extent_root->fs_info->nodesize; |
5d4f98a2 YZ |
3612 | block->key.offset = generation; |
3613 | block->level = level; | |
3614 | block->key_ready = 0; | |
3615 | ||
3616 | rb_node = tree_insert(blocks, block->bytenr, &block->rb_node); | |
43c04fb1 JM |
3617 | if (rb_node) |
3618 | backref_tree_panic(rb_node, -EEXIST, block->bytenr); | |
5d4f98a2 YZ |
3619 | |
3620 | return 0; | |
3621 | } | |
3622 | ||
3623 | /* | |
3624 | * helper to add tree blocks for backref of type BTRFS_SHARED_DATA_REF_KEY | |
3625 | */ | |
3626 | static int __add_tree_block(struct reloc_control *rc, | |
3627 | u64 bytenr, u32 blocksize, | |
3628 | struct rb_root *blocks) | |
3629 | { | |
0b246afa | 3630 | struct btrfs_fs_info *fs_info = rc->extent_root->fs_info; |
5d4f98a2 YZ |
3631 | struct btrfs_path *path; |
3632 | struct btrfs_key key; | |
3633 | int ret; | |
0b246afa | 3634 | bool skinny = btrfs_fs_incompat(fs_info, SKINNY_METADATA); |
5d4f98a2 | 3635 | |
7476dfda | 3636 | if (tree_block_processed(bytenr, rc)) |
5d4f98a2 YZ |
3637 | return 0; |
3638 | ||
3639 | if (tree_search(blocks, bytenr)) | |
3640 | return 0; | |
3641 | ||
3642 | path = btrfs_alloc_path(); | |
3643 | if (!path) | |
3644 | return -ENOMEM; | |
aee68ee5 | 3645 | again: |
5d4f98a2 | 3646 | key.objectid = bytenr; |
aee68ee5 JB |
3647 | if (skinny) { |
3648 | key.type = BTRFS_METADATA_ITEM_KEY; | |
3649 | key.offset = (u64)-1; | |
3650 | } else { | |
3651 | key.type = BTRFS_EXTENT_ITEM_KEY; | |
3652 | key.offset = blocksize; | |
3653 | } | |
5d4f98a2 YZ |
3654 | |
3655 | path->search_commit_root = 1; | |
3656 | path->skip_locking = 1; | |
3657 | ret = btrfs_search_slot(NULL, rc->extent_root, &key, path, 0, 0); | |
3658 | if (ret < 0) | |
3659 | goto out; | |
5d4f98a2 | 3660 | |
aee68ee5 JB |
3661 | if (ret > 0 && skinny) { |
3662 | if (path->slots[0]) { | |
3663 | path->slots[0]--; | |
3664 | btrfs_item_key_to_cpu(path->nodes[0], &key, | |
3665 | path->slots[0]); | |
3666 | if (key.objectid == bytenr && | |
3667 | (key.type == BTRFS_METADATA_ITEM_KEY || | |
3668 | (key.type == BTRFS_EXTENT_ITEM_KEY && | |
3669 | key.offset == blocksize))) | |
3670 | ret = 0; | |
3671 | } | |
3672 | ||
3673 | if (ret) { | |
3674 | skinny = false; | |
3675 | btrfs_release_path(path); | |
3676 | goto again; | |
3677 | } | |
3173a18f | 3678 | } |
cdccee99 LB |
3679 | if (ret) { |
3680 | ASSERT(ret == 1); | |
3681 | btrfs_print_leaf(path->nodes[0]); | |
3682 | btrfs_err(fs_info, | |
3683 | "tree block extent item (%llu) is not found in extent tree", | |
3684 | bytenr); | |
3685 | WARN_ON(1); | |
3686 | ret = -EINVAL; | |
3687 | goto out; | |
3688 | } | |
3173a18f | 3689 | |
5d4f98a2 YZ |
3690 | ret = add_tree_block(rc, &key, path, blocks); |
3691 | out: | |
3692 | btrfs_free_path(path); | |
3693 | return ret; | |
3694 | } | |
3695 | ||
0af3d00b | 3696 | static int delete_block_group_cache(struct btrfs_fs_info *fs_info, |
32da5386 | 3697 | struct btrfs_block_group *block_group, |
1bbc621e CM |
3698 | struct inode *inode, |
3699 | u64 ino) | |
0af3d00b JB |
3700 | { |
3701 | struct btrfs_key key; | |
0af3d00b JB |
3702 | struct btrfs_root *root = fs_info->tree_root; |
3703 | struct btrfs_trans_handle *trans; | |
0af3d00b JB |
3704 | int ret = 0; |
3705 | ||
3706 | if (inode) | |
3707 | goto truncate; | |
3708 | ||
3709 | key.objectid = ino; | |
3710 | key.type = BTRFS_INODE_ITEM_KEY; | |
3711 | key.offset = 0; | |
3712 | ||
4c66e0d4 | 3713 | inode = btrfs_iget(fs_info->sb, &key, root); |
2e19f1f9 | 3714 | if (IS_ERR(inode)) |
0af3d00b | 3715 | return -ENOENT; |
0af3d00b JB |
3716 | |
3717 | truncate: | |
2ff7e61e | 3718 | ret = btrfs_check_trunc_cache_free_space(fs_info, |
7b61cd92 MX |
3719 | &fs_info->global_block_rsv); |
3720 | if (ret) | |
3721 | goto out; | |
3722 | ||
7a7eaa40 | 3723 | trans = btrfs_join_transaction(root); |
0af3d00b | 3724 | if (IS_ERR(trans)) { |
3612b495 | 3725 | ret = PTR_ERR(trans); |
0af3d00b JB |
3726 | goto out; |
3727 | } | |
3728 | ||
77ab86bf | 3729 | ret = btrfs_truncate_free_space_cache(trans, block_group, inode); |
0af3d00b | 3730 | |
3a45bb20 | 3731 | btrfs_end_transaction(trans); |
2ff7e61e | 3732 | btrfs_btree_balance_dirty(fs_info); |
0af3d00b JB |
3733 | out: |
3734 | iput(inode); | |
3735 | return ret; | |
3736 | } | |
3737 | ||
5d4f98a2 | 3738 | /* |
19b546d7 QW |
3739 | * Locate the free space cache EXTENT_DATA in root tree leaf and delete the |
3740 | * cache inode, to avoid free space cache data extent blocking data relocation. | |
5d4f98a2 | 3741 | */ |
19b546d7 QW |
3742 | static int delete_v1_space_cache(struct extent_buffer *leaf, |
3743 | struct btrfs_block_group *block_group, | |
3744 | u64 data_bytenr) | |
5d4f98a2 | 3745 | { |
19b546d7 QW |
3746 | u64 space_cache_ino; |
3747 | struct btrfs_file_extent_item *ei; | |
5d4f98a2 | 3748 | struct btrfs_key key; |
19b546d7 QW |
3749 | bool found = false; |
3750 | int i; | |
5d4f98a2 YZ |
3751 | int ret; |
3752 | ||
19b546d7 QW |
3753 | if (btrfs_header_owner(leaf) != BTRFS_ROOT_TREE_OBJECTID) |
3754 | return 0; | |
5d4f98a2 | 3755 | |
19b546d7 QW |
3756 | for (i = 0; i < btrfs_header_nritems(leaf); i++) { |
3757 | btrfs_item_key_to_cpu(leaf, &key, i); | |
3758 | if (key.type != BTRFS_EXTENT_DATA_KEY) | |
3759 | continue; | |
3760 | ei = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item); | |
3761 | if (btrfs_file_extent_type(leaf, ei) == BTRFS_FILE_EXTENT_REG && | |
3762 | btrfs_file_extent_disk_bytenr(leaf, ei) == data_bytenr) { | |
3763 | found = true; | |
3764 | space_cache_ino = key.objectid; | |
5d4f98a2 | 3765 | break; |
5d4f98a2 | 3766 | } |
5d4f98a2 | 3767 | } |
19b546d7 QW |
3768 | if (!found) |
3769 | return -ENOENT; | |
3770 | ret = delete_block_group_cache(leaf->fs_info, block_group, NULL, | |
3771 | space_cache_ino); | |
3772 | return ret; | |
5d4f98a2 YZ |
3773 | } |
3774 | ||
3775 | /* | |
2c016dc2 | 3776 | * helper to find all tree blocks that reference a given data extent |
5d4f98a2 YZ |
3777 | */ |
3778 | static noinline_for_stack | |
3779 | int add_data_references(struct reloc_control *rc, | |
3780 | struct btrfs_key *extent_key, | |
3781 | struct btrfs_path *path, | |
3782 | struct rb_root *blocks) | |
3783 | { | |
19b546d7 QW |
3784 | struct btrfs_fs_info *fs_info = rc->extent_root->fs_info; |
3785 | struct ulist *leaves = NULL; | |
3786 | struct ulist_iterator leaf_uiter; | |
3787 | struct ulist_node *ref_node = NULL; | |
3788 | const u32 blocksize = fs_info->nodesize; | |
647f63bd | 3789 | int ret = 0; |
5d4f98a2 | 3790 | |
19b546d7 QW |
3791 | btrfs_release_path(path); |
3792 | ret = btrfs_find_all_leafs(NULL, fs_info, extent_key->objectid, | |
3793 | 0, &leaves, NULL, true); | |
3794 | if (ret < 0) | |
3795 | return ret; | |
5d4f98a2 | 3796 | |
19b546d7 QW |
3797 | ULIST_ITER_INIT(&leaf_uiter); |
3798 | while ((ref_node = ulist_next(leaves, &leaf_uiter))) { | |
3799 | struct extent_buffer *eb; | |
5d4f98a2 | 3800 | |
19b546d7 QW |
3801 | eb = read_tree_block(fs_info, ref_node->val, 0, 0, NULL); |
3802 | if (IS_ERR(eb)) { | |
3803 | ret = PTR_ERR(eb); | |
5d4f98a2 | 3804 | break; |
5d4f98a2 | 3805 | } |
19b546d7 QW |
3806 | ret = delete_v1_space_cache(eb, rc->block_group, |
3807 | extent_key->objectid); | |
3808 | free_extent_buffer(eb); | |
3809 | if (ret < 0) | |
3810 | break; | |
3811 | ret = __add_tree_block(rc, ref_node->val, blocksize, blocks); | |
3812 | if (ret < 0) | |
5d4f98a2 | 3813 | break; |
5d4f98a2 | 3814 | } |
19b546d7 | 3815 | if (ret < 0) |
5d4f98a2 | 3816 | free_block_list(blocks); |
19b546d7 QW |
3817 | ulist_free(leaves); |
3818 | return ret; | |
5d4f98a2 YZ |
3819 | } |
3820 | ||
3821 | /* | |
2c016dc2 | 3822 | * helper to find next unprocessed extent |
5d4f98a2 YZ |
3823 | */ |
3824 | static noinline_for_stack | |
147d256e | 3825 | int find_next_extent(struct reloc_control *rc, struct btrfs_path *path, |
3fd0a558 | 3826 | struct btrfs_key *extent_key) |
5d4f98a2 | 3827 | { |
0b246afa | 3828 | struct btrfs_fs_info *fs_info = rc->extent_root->fs_info; |
5d4f98a2 YZ |
3829 | struct btrfs_key key; |
3830 | struct extent_buffer *leaf; | |
3831 | u64 start, end, last; | |
3832 | int ret; | |
3833 | ||
b3470b5d | 3834 | last = rc->block_group->start + rc->block_group->length; |
5d4f98a2 YZ |
3835 | while (1) { |
3836 | cond_resched(); | |
3837 | if (rc->search_start >= last) { | |
3838 | ret = 1; | |
3839 | break; | |
3840 | } | |
3841 | ||
3842 | key.objectid = rc->search_start; | |
3843 | key.type = BTRFS_EXTENT_ITEM_KEY; | |
3844 | key.offset = 0; | |
3845 | ||
3846 | path->search_commit_root = 1; | |
3847 | path->skip_locking = 1; | |
3848 | ret = btrfs_search_slot(NULL, rc->extent_root, &key, path, | |
3849 | 0, 0); | |
3850 | if (ret < 0) | |
3851 | break; | |
3852 | next: | |
3853 | leaf = path->nodes[0]; | |
3854 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { | |
3855 | ret = btrfs_next_leaf(rc->extent_root, path); | |
3856 | if (ret != 0) | |
3857 | break; | |
3858 | leaf = path->nodes[0]; | |
3859 | } | |
3860 | ||
3861 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); | |
3862 | if (key.objectid >= last) { | |
3863 | ret = 1; | |
3864 | break; | |
3865 | } | |
3866 | ||
3173a18f JB |
3867 | if (key.type != BTRFS_EXTENT_ITEM_KEY && |
3868 | key.type != BTRFS_METADATA_ITEM_KEY) { | |
3869 | path->slots[0]++; | |
3870 | goto next; | |
3871 | } | |
3872 | ||
3873 | if (key.type == BTRFS_EXTENT_ITEM_KEY && | |
5d4f98a2 YZ |
3874 | key.objectid + key.offset <= rc->search_start) { |
3875 | path->slots[0]++; | |
3876 | goto next; | |
3877 | } | |
3878 | ||
3173a18f | 3879 | if (key.type == BTRFS_METADATA_ITEM_KEY && |
0b246afa | 3880 | key.objectid + fs_info->nodesize <= |
3173a18f JB |
3881 | rc->search_start) { |
3882 | path->slots[0]++; | |
3883 | goto next; | |
3884 | } | |
3885 | ||
5d4f98a2 YZ |
3886 | ret = find_first_extent_bit(&rc->processed_blocks, |
3887 | key.objectid, &start, &end, | |
e6138876 | 3888 | EXTENT_DIRTY, NULL); |
5d4f98a2 YZ |
3889 | |
3890 | if (ret == 0 && start <= key.objectid) { | |
b3b4aa74 | 3891 | btrfs_release_path(path); |
5d4f98a2 YZ |
3892 | rc->search_start = end + 1; |
3893 | } else { | |
3173a18f JB |
3894 | if (key.type == BTRFS_EXTENT_ITEM_KEY) |
3895 | rc->search_start = key.objectid + key.offset; | |
3896 | else | |
3897 | rc->search_start = key.objectid + | |
0b246afa | 3898 | fs_info->nodesize; |
3fd0a558 | 3899 | memcpy(extent_key, &key, sizeof(key)); |
5d4f98a2 YZ |
3900 | return 0; |
3901 | } | |
3902 | } | |
b3b4aa74 | 3903 | btrfs_release_path(path); |
5d4f98a2 YZ |
3904 | return ret; |
3905 | } | |
3906 | ||
3907 | static void set_reloc_control(struct reloc_control *rc) | |
3908 | { | |
3909 | struct btrfs_fs_info *fs_info = rc->extent_root->fs_info; | |
7585717f CM |
3910 | |
3911 | mutex_lock(&fs_info->reloc_mutex); | |
5d4f98a2 | 3912 | fs_info->reloc_ctl = rc; |
7585717f | 3913 | mutex_unlock(&fs_info->reloc_mutex); |
5d4f98a2 YZ |
3914 | } |
3915 | ||
3916 | static void unset_reloc_control(struct reloc_control *rc) | |
3917 | { | |
3918 | struct btrfs_fs_info *fs_info = rc->extent_root->fs_info; | |
7585717f CM |
3919 | |
3920 | mutex_lock(&fs_info->reloc_mutex); | |
5d4f98a2 | 3921 | fs_info->reloc_ctl = NULL; |
7585717f | 3922 | mutex_unlock(&fs_info->reloc_mutex); |
5d4f98a2 YZ |
3923 | } |
3924 | ||
3925 | static int check_extent_flags(u64 flags) | |
3926 | { | |
3927 | if ((flags & BTRFS_EXTENT_FLAG_DATA) && | |
3928 | (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)) | |
3929 | return 1; | |
3930 | if (!(flags & BTRFS_EXTENT_FLAG_DATA) && | |
3931 | !(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)) | |
3932 | return 1; | |
3933 | if ((flags & BTRFS_EXTENT_FLAG_DATA) && | |
3934 | (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) | |
3935 | return 1; | |
3936 | return 0; | |
3937 | } | |
3938 | ||
3fd0a558 YZ |
3939 | static noinline_for_stack |
3940 | int prepare_to_relocate(struct reloc_control *rc) | |
3941 | { | |
3942 | struct btrfs_trans_handle *trans; | |
ac2fabac | 3943 | int ret; |
3fd0a558 | 3944 | |
2ff7e61e | 3945 | rc->block_rsv = btrfs_alloc_block_rsv(rc->extent_root->fs_info, |
66d8f3dd | 3946 | BTRFS_BLOCK_RSV_TEMP); |
3fd0a558 YZ |
3947 | if (!rc->block_rsv) |
3948 | return -ENOMEM; | |
3949 | ||
3fd0a558 | 3950 | memset(&rc->cluster, 0, sizeof(rc->cluster)); |
b3470b5d | 3951 | rc->search_start = rc->block_group->start; |
3fd0a558 YZ |
3952 | rc->extents_found = 0; |
3953 | rc->nodes_relocated = 0; | |
3954 | rc->merging_rsv_size = 0; | |
0647bf56 | 3955 | rc->reserved_bytes = 0; |
da17066c | 3956 | rc->block_rsv->size = rc->extent_root->fs_info->nodesize * |
0647bf56 | 3957 | RELOCATION_RESERVED_NODES; |
ac2fabac JB |
3958 | ret = btrfs_block_rsv_refill(rc->extent_root, |
3959 | rc->block_rsv, rc->block_rsv->size, | |
3960 | BTRFS_RESERVE_FLUSH_ALL); | |
3961 | if (ret) | |
3962 | return ret; | |
3fd0a558 YZ |
3963 | |
3964 | rc->create_reloc_tree = 1; | |
3965 | set_reloc_control(rc); | |
3966 | ||
7a7eaa40 | 3967 | trans = btrfs_join_transaction(rc->extent_root); |
28818947 LB |
3968 | if (IS_ERR(trans)) { |
3969 | unset_reloc_control(rc); | |
3970 | /* | |
3971 | * extent tree is not a ref_cow tree and has no reloc_root to | |
3972 | * cleanup. And callers are responsible to free the above | |
3973 | * block rsv. | |
3974 | */ | |
3975 | return PTR_ERR(trans); | |
3976 | } | |
3a45bb20 | 3977 | btrfs_commit_transaction(trans); |
3fd0a558 YZ |
3978 | return 0; |
3979 | } | |
76dda93c | 3980 | |
5d4f98a2 YZ |
3981 | static noinline_for_stack int relocate_block_group(struct reloc_control *rc) |
3982 | { | |
2ff7e61e | 3983 | struct btrfs_fs_info *fs_info = rc->extent_root->fs_info; |
5d4f98a2 YZ |
3984 | struct rb_root blocks = RB_ROOT; |
3985 | struct btrfs_key key; | |
3986 | struct btrfs_trans_handle *trans = NULL; | |
3987 | struct btrfs_path *path; | |
3988 | struct btrfs_extent_item *ei; | |
5d4f98a2 YZ |
3989 | u64 flags; |
3990 | u32 item_size; | |
3991 | int ret; | |
3992 | int err = 0; | |
c87f08ca | 3993 | int progress = 0; |
5d4f98a2 YZ |
3994 | |
3995 | path = btrfs_alloc_path(); | |
3fd0a558 | 3996 | if (!path) |
5d4f98a2 | 3997 | return -ENOMEM; |
e4058b54 | 3998 | path->reada = READA_FORWARD; |
5d4f98a2 | 3999 | |
3fd0a558 YZ |
4000 | ret = prepare_to_relocate(rc); |
4001 | if (ret) { | |
4002 | err = ret; | |
4003 | goto out_free; | |
4004 | } | |
5d4f98a2 YZ |
4005 | |
4006 | while (1) { | |
0647bf56 WS |
4007 | rc->reserved_bytes = 0; |
4008 | ret = btrfs_block_rsv_refill(rc->extent_root, | |
4009 | rc->block_rsv, rc->block_rsv->size, | |
4010 | BTRFS_RESERVE_FLUSH_ALL); | |
4011 | if (ret) { | |
4012 | err = ret; | |
4013 | break; | |
4014 | } | |
c87f08ca | 4015 | progress++; |
a22285a6 | 4016 | trans = btrfs_start_transaction(rc->extent_root, 0); |
0f788c58 LB |
4017 | if (IS_ERR(trans)) { |
4018 | err = PTR_ERR(trans); | |
4019 | trans = NULL; | |
4020 | break; | |
4021 | } | |
c87f08ca | 4022 | restart: |
3fd0a558 | 4023 | if (update_backref_cache(trans, &rc->backref_cache)) { |
3a45bb20 | 4024 | btrfs_end_transaction(trans); |
42a657f5 | 4025 | trans = NULL; |
3fd0a558 YZ |
4026 | continue; |
4027 | } | |
4028 | ||
147d256e | 4029 | ret = find_next_extent(rc, path, &key); |
5d4f98a2 YZ |
4030 | if (ret < 0) |
4031 | err = ret; | |
4032 | if (ret != 0) | |
4033 | break; | |
4034 | ||
4035 | rc->extents_found++; | |
4036 | ||
4037 | ei = btrfs_item_ptr(path->nodes[0], path->slots[0], | |
4038 | struct btrfs_extent_item); | |
3fd0a558 | 4039 | item_size = btrfs_item_size_nr(path->nodes[0], path->slots[0]); |
5d4f98a2 YZ |
4040 | if (item_size >= sizeof(*ei)) { |
4041 | flags = btrfs_extent_flags(path->nodes[0], ei); | |
4042 | ret = check_extent_flags(flags); | |
4043 | BUG_ON(ret); | |
6d8ff4e4 | 4044 | } else if (unlikely(item_size == sizeof(struct btrfs_extent_item_v0))) { |
ba3c2b19 NB |
4045 | err = -EINVAL; |
4046 | btrfs_print_v0_err(trans->fs_info); | |
4047 | btrfs_abort_transaction(trans, err); | |
4048 | break; | |
5d4f98a2 | 4049 | } else { |
5d4f98a2 | 4050 | BUG(); |
5d4f98a2 YZ |
4051 | } |
4052 | ||
4053 | if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) { | |
4054 | ret = add_tree_block(rc, &key, path, &blocks); | |
4055 | } else if (rc->stage == UPDATE_DATA_PTRS && | |
3fd0a558 | 4056 | (flags & BTRFS_EXTENT_FLAG_DATA)) { |
5d4f98a2 YZ |
4057 | ret = add_data_references(rc, &key, path, &blocks); |
4058 | } else { | |
b3b4aa74 | 4059 | btrfs_release_path(path); |
5d4f98a2 YZ |
4060 | ret = 0; |
4061 | } | |
4062 | if (ret < 0) { | |
3fd0a558 | 4063 | err = ret; |
5d4f98a2 YZ |
4064 | break; |
4065 | } | |
4066 | ||
4067 | if (!RB_EMPTY_ROOT(&blocks)) { | |
4068 | ret = relocate_tree_blocks(trans, rc, &blocks); | |
4069 | if (ret < 0) { | |
3fd0a558 YZ |
4070 | if (ret != -EAGAIN) { |
4071 | err = ret; | |
4072 | break; | |
4073 | } | |
4074 | rc->extents_found--; | |
4075 | rc->search_start = key.objectid; | |
4076 | } | |
4077 | } | |
4078 | ||
3a45bb20 | 4079 | btrfs_end_transaction_throttle(trans); |
2ff7e61e | 4080 | btrfs_btree_balance_dirty(fs_info); |
5d4f98a2 | 4081 | trans = NULL; |
5d4f98a2 YZ |
4082 | |
4083 | if (rc->stage == MOVE_DATA_EXTENTS && | |
4084 | (flags & BTRFS_EXTENT_FLAG_DATA)) { | |
4085 | rc->found_file_extent = 1; | |
0257bb82 | 4086 | ret = relocate_data_extent(rc->data_inode, |
3fd0a558 | 4087 | &key, &rc->cluster); |
5d4f98a2 YZ |
4088 | if (ret < 0) { |
4089 | err = ret; | |
4090 | break; | |
4091 | } | |
4092 | } | |
f31ea088 QW |
4093 | if (btrfs_should_cancel_balance(fs_info)) { |
4094 | err = -ECANCELED; | |
4095 | break; | |
4096 | } | |
5d4f98a2 | 4097 | } |
c87f08ca | 4098 | if (trans && progress && err == -ENOSPC) { |
43a7e99d | 4099 | ret = btrfs_force_chunk_alloc(trans, rc->block_group->flags); |
9689457b | 4100 | if (ret == 1) { |
c87f08ca CM |
4101 | err = 0; |
4102 | progress = 0; | |
4103 | goto restart; | |
4104 | } | |
4105 | } | |
3fd0a558 | 4106 | |
b3b4aa74 | 4107 | btrfs_release_path(path); |
91166212 | 4108 | clear_extent_bits(&rc->processed_blocks, 0, (u64)-1, EXTENT_DIRTY); |
5d4f98a2 YZ |
4109 | |
4110 | if (trans) { | |
3a45bb20 | 4111 | btrfs_end_transaction_throttle(trans); |
2ff7e61e | 4112 | btrfs_btree_balance_dirty(fs_info); |
5d4f98a2 YZ |
4113 | } |
4114 | ||
0257bb82 | 4115 | if (!err) { |
3fd0a558 YZ |
4116 | ret = relocate_file_extent_cluster(rc->data_inode, |
4117 | &rc->cluster); | |
0257bb82 YZ |
4118 | if (ret < 0) |
4119 | err = ret; | |
4120 | } | |
4121 | ||
3fd0a558 YZ |
4122 | rc->create_reloc_tree = 0; |
4123 | set_reloc_control(rc); | |
0257bb82 | 4124 | |
3fd0a558 | 4125 | backref_cache_cleanup(&rc->backref_cache); |
63f018be | 4126 | btrfs_block_rsv_release(fs_info, rc->block_rsv, (u64)-1, NULL); |
5d4f98a2 | 4127 | |
7f913c7c QW |
4128 | /* |
4129 | * Even in the case when the relocation is cancelled, we should all go | |
4130 | * through prepare_to_merge() and merge_reloc_roots(). | |
4131 | * | |
4132 | * For error (including cancelled balance), prepare_to_merge() will | |
4133 | * mark all reloc trees orphan, then queue them for cleanup in | |
4134 | * merge_reloc_roots() | |
4135 | */ | |
3fd0a558 | 4136 | err = prepare_to_merge(rc, err); |
5d4f98a2 YZ |
4137 | |
4138 | merge_reloc_roots(rc); | |
4139 | ||
3fd0a558 | 4140 | rc->merge_reloc_tree = 0; |
5d4f98a2 | 4141 | unset_reloc_control(rc); |
63f018be | 4142 | btrfs_block_rsv_release(fs_info, rc->block_rsv, (u64)-1, NULL); |
5d4f98a2 YZ |
4143 | |
4144 | /* get rid of pinned extents */ | |
7a7eaa40 | 4145 | trans = btrfs_join_transaction(rc->extent_root); |
62b99540 | 4146 | if (IS_ERR(trans)) { |
3612b495 | 4147 | err = PTR_ERR(trans); |
62b99540 QW |
4148 | goto out_free; |
4149 | } | |
3a45bb20 | 4150 | btrfs_commit_transaction(trans); |
6217b0fa | 4151 | out_free: |
d2311e69 QW |
4152 | ret = clean_dirty_subvols(rc); |
4153 | if (ret < 0 && !err) | |
4154 | err = ret; | |
2ff7e61e | 4155 | btrfs_free_block_rsv(fs_info, rc->block_rsv); |
3fd0a558 | 4156 | btrfs_free_path(path); |
5d4f98a2 YZ |
4157 | return err; |
4158 | } | |
4159 | ||
4160 | static int __insert_orphan_inode(struct btrfs_trans_handle *trans, | |
0257bb82 | 4161 | struct btrfs_root *root, u64 objectid) |
5d4f98a2 YZ |
4162 | { |
4163 | struct btrfs_path *path; | |
4164 | struct btrfs_inode_item *item; | |
4165 | struct extent_buffer *leaf; | |
4166 | int ret; | |
4167 | ||
4168 | path = btrfs_alloc_path(); | |
4169 | if (!path) | |
4170 | return -ENOMEM; | |
4171 | ||
4172 | ret = btrfs_insert_empty_inode(trans, root, path, objectid); | |
4173 | if (ret) | |
4174 | goto out; | |
4175 | ||
4176 | leaf = path->nodes[0]; | |
4177 | item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_inode_item); | |
b159fa28 | 4178 | memzero_extent_buffer(leaf, (unsigned long)item, sizeof(*item)); |
5d4f98a2 | 4179 | btrfs_set_inode_generation(leaf, item, 1); |
0257bb82 | 4180 | btrfs_set_inode_size(leaf, item, 0); |
5d4f98a2 | 4181 | btrfs_set_inode_mode(leaf, item, S_IFREG | 0600); |
3fd0a558 YZ |
4182 | btrfs_set_inode_flags(leaf, item, BTRFS_INODE_NOCOMPRESS | |
4183 | BTRFS_INODE_PREALLOC); | |
5d4f98a2 | 4184 | btrfs_mark_buffer_dirty(leaf); |
5d4f98a2 YZ |
4185 | out: |
4186 | btrfs_free_path(path); | |
4187 | return ret; | |
4188 | } | |
4189 | ||
4190 | /* | |
4191 | * helper to create inode for data relocation. | |
4192 | * the inode is in data relocation tree and its link count is 0 | |
4193 | */ | |
3fd0a558 YZ |
4194 | static noinline_for_stack |
4195 | struct inode *create_reloc_inode(struct btrfs_fs_info *fs_info, | |
32da5386 | 4196 | struct btrfs_block_group *group) |
5d4f98a2 YZ |
4197 | { |
4198 | struct inode *inode = NULL; | |
4199 | struct btrfs_trans_handle *trans; | |
4200 | struct btrfs_root *root; | |
4201 | struct btrfs_key key; | |
4624900d | 4202 | u64 objectid; |
5d4f98a2 YZ |
4203 | int err = 0; |
4204 | ||
4205 | root = read_fs_root(fs_info, BTRFS_DATA_RELOC_TREE_OBJECTID); | |
4206 | if (IS_ERR(root)) | |
4207 | return ERR_CAST(root); | |
4208 | ||
a22285a6 | 4209 | trans = btrfs_start_transaction(root, 6); |
76deacf0 | 4210 | if (IS_ERR(trans)) { |
00246528 | 4211 | btrfs_put_root(root); |
3fd0a558 | 4212 | return ERR_CAST(trans); |
76deacf0 | 4213 | } |
5d4f98a2 | 4214 | |
581bb050 | 4215 | err = btrfs_find_free_objectid(root, &objectid); |
5d4f98a2 YZ |
4216 | if (err) |
4217 | goto out; | |
4218 | ||
0257bb82 | 4219 | err = __insert_orphan_inode(trans, root, objectid); |
5d4f98a2 YZ |
4220 | BUG_ON(err); |
4221 | ||
4222 | key.objectid = objectid; | |
4223 | key.type = BTRFS_INODE_ITEM_KEY; | |
4224 | key.offset = 0; | |
4c66e0d4 | 4225 | inode = btrfs_iget(fs_info->sb, &key, root); |
2e19f1f9 | 4226 | BUG_ON(IS_ERR(inode)); |
b3470b5d | 4227 | BTRFS_I(inode)->index_cnt = group->start; |
5d4f98a2 | 4228 | |
73f2e545 | 4229 | err = btrfs_orphan_add(trans, BTRFS_I(inode)); |
5d4f98a2 | 4230 | out: |
00246528 | 4231 | btrfs_put_root(root); |
3a45bb20 | 4232 | btrfs_end_transaction(trans); |
2ff7e61e | 4233 | btrfs_btree_balance_dirty(fs_info); |
5d4f98a2 YZ |
4234 | if (err) { |
4235 | if (inode) | |
4236 | iput(inode); | |
4237 | inode = ERR_PTR(err); | |
4238 | } | |
4239 | return inode; | |
4240 | } | |
4241 | ||
c258d6e3 | 4242 | static struct reloc_control *alloc_reloc_control(struct btrfs_fs_info *fs_info) |
3fd0a558 YZ |
4243 | { |
4244 | struct reloc_control *rc; | |
4245 | ||
4246 | rc = kzalloc(sizeof(*rc), GFP_NOFS); | |
4247 | if (!rc) | |
4248 | return NULL; | |
4249 | ||
4250 | INIT_LIST_HEAD(&rc->reloc_roots); | |
d2311e69 | 4251 | INIT_LIST_HEAD(&rc->dirty_subvol_roots); |
3fd0a558 YZ |
4252 | backref_cache_init(&rc->backref_cache); |
4253 | mapping_tree_init(&rc->reloc_root_tree); | |
43eb5f29 QW |
4254 | extent_io_tree_init(fs_info, &rc->processed_blocks, |
4255 | IO_TREE_RELOC_BLOCKS, NULL); | |
3fd0a558 YZ |
4256 | return rc; |
4257 | } | |
4258 | ||
1a0afa0e JB |
4259 | static void free_reloc_control(struct reloc_control *rc) |
4260 | { | |
4261 | struct mapping_node *node, *tmp; | |
4262 | ||
4263 | free_reloc_roots(&rc->reloc_roots); | |
4264 | rbtree_postorder_for_each_entry_safe(node, tmp, | |
4265 | &rc->reloc_root_tree.rb_root, rb_node) | |
4266 | kfree(node); | |
4267 | ||
4268 | kfree(rc); | |
4269 | } | |
4270 | ||
ebce0e01 AB |
4271 | /* |
4272 | * Print the block group being relocated | |
4273 | */ | |
4274 | static void describe_relocation(struct btrfs_fs_info *fs_info, | |
32da5386 | 4275 | struct btrfs_block_group *block_group) |
ebce0e01 | 4276 | { |
f89e09cf | 4277 | char buf[128] = {'\0'}; |
ebce0e01 | 4278 | |
f89e09cf | 4279 | btrfs_describe_block_groups(block_group->flags, buf, sizeof(buf)); |
ebce0e01 AB |
4280 | |
4281 | btrfs_info(fs_info, | |
4282 | "relocating block group %llu flags %s", | |
b3470b5d | 4283 | block_group->start, buf); |
ebce0e01 AB |
4284 | } |
4285 | ||
430640e3 QW |
4286 | static const char *stage_to_string(int stage) |
4287 | { | |
4288 | if (stage == MOVE_DATA_EXTENTS) | |
4289 | return "move data extents"; | |
4290 | if (stage == UPDATE_DATA_PTRS) | |
4291 | return "update data pointers"; | |
4292 | return "unknown"; | |
4293 | } | |
4294 | ||
5d4f98a2 YZ |
4295 | /* |
4296 | * function to relocate all extents in a block group. | |
4297 | */ | |
6bccf3ab | 4298 | int btrfs_relocate_block_group(struct btrfs_fs_info *fs_info, u64 group_start) |
5d4f98a2 | 4299 | { |
32da5386 | 4300 | struct btrfs_block_group *bg; |
6bccf3ab | 4301 | struct btrfs_root *extent_root = fs_info->extent_root; |
5d4f98a2 | 4302 | struct reloc_control *rc; |
0af3d00b JB |
4303 | struct inode *inode; |
4304 | struct btrfs_path *path; | |
5d4f98a2 | 4305 | int ret; |
f0486c68 | 4306 | int rw = 0; |
5d4f98a2 YZ |
4307 | int err = 0; |
4308 | ||
eede2bf3 OS |
4309 | bg = btrfs_lookup_block_group(fs_info, group_start); |
4310 | if (!bg) | |
4311 | return -ENOENT; | |
4312 | ||
4313 | if (btrfs_pinned_by_swapfile(fs_info, bg)) { | |
4314 | btrfs_put_block_group(bg); | |
4315 | return -ETXTBSY; | |
4316 | } | |
4317 | ||
c258d6e3 | 4318 | rc = alloc_reloc_control(fs_info); |
eede2bf3 OS |
4319 | if (!rc) { |
4320 | btrfs_put_block_group(bg); | |
5d4f98a2 | 4321 | return -ENOMEM; |
eede2bf3 | 4322 | } |
5d4f98a2 | 4323 | |
f0486c68 | 4324 | rc->extent_root = extent_root; |
eede2bf3 | 4325 | rc->block_group = bg; |
5d4f98a2 | 4326 | |
b12de528 | 4327 | ret = btrfs_inc_block_group_ro(rc->block_group, true); |
868f401a Z |
4328 | if (ret) { |
4329 | err = ret; | |
4330 | goto out; | |
f0486c68 | 4331 | } |
868f401a | 4332 | rw = 1; |
f0486c68 | 4333 | |
0af3d00b JB |
4334 | path = btrfs_alloc_path(); |
4335 | if (!path) { | |
4336 | err = -ENOMEM; | |
4337 | goto out; | |
4338 | } | |
4339 | ||
7949f339 | 4340 | inode = lookup_free_space_inode(rc->block_group, path); |
0af3d00b JB |
4341 | btrfs_free_path(path); |
4342 | ||
4343 | if (!IS_ERR(inode)) | |
1bbc621e | 4344 | ret = delete_block_group_cache(fs_info, rc->block_group, inode, 0); |
0af3d00b JB |
4345 | else |
4346 | ret = PTR_ERR(inode); | |
4347 | ||
4348 | if (ret && ret != -ENOENT) { | |
4349 | err = ret; | |
4350 | goto out; | |
4351 | } | |
4352 | ||
5d4f98a2 YZ |
4353 | rc->data_inode = create_reloc_inode(fs_info, rc->block_group); |
4354 | if (IS_ERR(rc->data_inode)) { | |
4355 | err = PTR_ERR(rc->data_inode); | |
4356 | rc->data_inode = NULL; | |
4357 | goto out; | |
4358 | } | |
4359 | ||
0b246afa | 4360 | describe_relocation(fs_info, rc->block_group); |
5d4f98a2 | 4361 | |
9cfa3e34 | 4362 | btrfs_wait_block_group_reservations(rc->block_group); |
f78c436c | 4363 | btrfs_wait_nocow_writers(rc->block_group); |
6374e57a | 4364 | btrfs_wait_ordered_roots(fs_info, U64_MAX, |
b3470b5d DS |
4365 | rc->block_group->start, |
4366 | rc->block_group->length); | |
5d4f98a2 YZ |
4367 | |
4368 | while (1) { | |
430640e3 QW |
4369 | int finishes_stage; |
4370 | ||
76dda93c | 4371 | mutex_lock(&fs_info->cleaner_mutex); |
5d4f98a2 | 4372 | ret = relocate_block_group(rc); |
76dda93c | 4373 | mutex_unlock(&fs_info->cleaner_mutex); |
ff612ba7 | 4374 | if (ret < 0) |
5d4f98a2 | 4375 | err = ret; |
5d4f98a2 | 4376 | |
430640e3 | 4377 | finishes_stage = rc->stage; |
ff612ba7 JB |
4378 | /* |
4379 | * We may have gotten ENOSPC after we already dirtied some | |
4380 | * extents. If writeout happens while we're relocating a | |
4381 | * different block group we could end up hitting the | |
4382 | * BUG_ON(rc->stage == UPDATE_DATA_PTRS) in | |
4383 | * btrfs_reloc_cow_block. Make sure we write everything out | |
4384 | * properly so we don't trip over this problem, and then break | |
4385 | * out of the loop if we hit an error. | |
4386 | */ | |
5d4f98a2 | 4387 | if (rc->stage == MOVE_DATA_EXTENTS && rc->found_file_extent) { |
0ef8b726 JB |
4388 | ret = btrfs_wait_ordered_range(rc->data_inode, 0, |
4389 | (u64)-1); | |
ff612ba7 | 4390 | if (ret) |
0ef8b726 | 4391 | err = ret; |
5d4f98a2 YZ |
4392 | invalidate_mapping_pages(rc->data_inode->i_mapping, |
4393 | 0, -1); | |
4394 | rc->stage = UPDATE_DATA_PTRS; | |
5d4f98a2 | 4395 | } |
ff612ba7 JB |
4396 | |
4397 | if (err < 0) | |
4398 | goto out; | |
4399 | ||
4400 | if (rc->extents_found == 0) | |
4401 | break; | |
4402 | ||
430640e3 QW |
4403 | btrfs_info(fs_info, "found %llu extents, stage: %s", |
4404 | rc->extents_found, stage_to_string(finishes_stage)); | |
5d4f98a2 YZ |
4405 | } |
4406 | ||
5d4f98a2 YZ |
4407 | WARN_ON(rc->block_group->pinned > 0); |
4408 | WARN_ON(rc->block_group->reserved > 0); | |
bf38be65 | 4409 | WARN_ON(rc->block_group->used > 0); |
5d4f98a2 | 4410 | out: |
f0486c68 | 4411 | if (err && rw) |
2ff7e61e | 4412 | btrfs_dec_block_group_ro(rc->block_group); |
5d4f98a2 | 4413 | iput(rc->data_inode); |
5d4f98a2 | 4414 | btrfs_put_block_group(rc->block_group); |
1a0afa0e | 4415 | free_reloc_control(rc); |
5d4f98a2 YZ |
4416 | return err; |
4417 | } | |
4418 | ||
76dda93c YZ |
4419 | static noinline_for_stack int mark_garbage_root(struct btrfs_root *root) |
4420 | { | |
0b246afa | 4421 | struct btrfs_fs_info *fs_info = root->fs_info; |
76dda93c | 4422 | struct btrfs_trans_handle *trans; |
79787eaa | 4423 | int ret, err; |
76dda93c | 4424 | |
0b246afa | 4425 | trans = btrfs_start_transaction(fs_info->tree_root, 0); |
79787eaa JM |
4426 | if (IS_ERR(trans)) |
4427 | return PTR_ERR(trans); | |
76dda93c YZ |
4428 | |
4429 | memset(&root->root_item.drop_progress, 0, | |
4430 | sizeof(root->root_item.drop_progress)); | |
4431 | root->root_item.drop_level = 0; | |
4432 | btrfs_set_root_refs(&root->root_item, 0); | |
0b246afa | 4433 | ret = btrfs_update_root(trans, fs_info->tree_root, |
76dda93c | 4434 | &root->root_key, &root->root_item); |
76dda93c | 4435 | |
3a45bb20 | 4436 | err = btrfs_end_transaction(trans); |
79787eaa JM |
4437 | if (err) |
4438 | return err; | |
4439 | return ret; | |
76dda93c YZ |
4440 | } |
4441 | ||
5d4f98a2 YZ |
4442 | /* |
4443 | * recover relocation interrupted by system crash. | |
4444 | * | |
4445 | * this function resumes merging reloc trees with corresponding fs trees. | |
4446 | * this is important for keeping the sharing of tree blocks | |
4447 | */ | |
4448 | int btrfs_recover_relocation(struct btrfs_root *root) | |
4449 | { | |
0b246afa | 4450 | struct btrfs_fs_info *fs_info = root->fs_info; |
5d4f98a2 YZ |
4451 | LIST_HEAD(reloc_roots); |
4452 | struct btrfs_key key; | |
4453 | struct btrfs_root *fs_root; | |
4454 | struct btrfs_root *reloc_root; | |
4455 | struct btrfs_path *path; | |
4456 | struct extent_buffer *leaf; | |
4457 | struct reloc_control *rc = NULL; | |
4458 | struct btrfs_trans_handle *trans; | |
4459 | int ret; | |
4460 | int err = 0; | |
4461 | ||
4462 | path = btrfs_alloc_path(); | |
4463 | if (!path) | |
4464 | return -ENOMEM; | |
e4058b54 | 4465 | path->reada = READA_BACK; |
5d4f98a2 YZ |
4466 | |
4467 | key.objectid = BTRFS_TREE_RELOC_OBJECTID; | |
4468 | key.type = BTRFS_ROOT_ITEM_KEY; | |
4469 | key.offset = (u64)-1; | |
4470 | ||
4471 | while (1) { | |
0b246afa | 4472 | ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, |
5d4f98a2 YZ |
4473 | path, 0, 0); |
4474 | if (ret < 0) { | |
4475 | err = ret; | |
4476 | goto out; | |
4477 | } | |
4478 | if (ret > 0) { | |
4479 | if (path->slots[0] == 0) | |
4480 | break; | |
4481 | path->slots[0]--; | |
4482 | } | |
4483 | leaf = path->nodes[0]; | |
4484 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); | |
b3b4aa74 | 4485 | btrfs_release_path(path); |
5d4f98a2 YZ |
4486 | |
4487 | if (key.objectid != BTRFS_TREE_RELOC_OBJECTID || | |
4488 | key.type != BTRFS_ROOT_ITEM_KEY) | |
4489 | break; | |
4490 | ||
3dbf1738 | 4491 | reloc_root = btrfs_read_tree_root(root, &key); |
5d4f98a2 YZ |
4492 | if (IS_ERR(reloc_root)) { |
4493 | err = PTR_ERR(reloc_root); | |
4494 | goto out; | |
4495 | } | |
4496 | ||
3dbf1738 | 4497 | set_bit(BTRFS_ROOT_REF_COWS, &reloc_root->state); |
5d4f98a2 YZ |
4498 | list_add(&reloc_root->root_list, &reloc_roots); |
4499 | ||
4500 | if (btrfs_root_refs(&reloc_root->root_item) > 0) { | |
0b246afa | 4501 | fs_root = read_fs_root(fs_info, |
5d4f98a2 YZ |
4502 | reloc_root->root_key.offset); |
4503 | if (IS_ERR(fs_root)) { | |
76dda93c YZ |
4504 | ret = PTR_ERR(fs_root); |
4505 | if (ret != -ENOENT) { | |
4506 | err = ret; | |
4507 | goto out; | |
4508 | } | |
79787eaa JM |
4509 | ret = mark_garbage_root(reloc_root); |
4510 | if (ret < 0) { | |
4511 | err = ret; | |
4512 | goto out; | |
4513 | } | |
932fd26d | 4514 | } else { |
00246528 | 4515 | btrfs_put_root(fs_root); |
5d4f98a2 YZ |
4516 | } |
4517 | } | |
4518 | ||
4519 | if (key.offset == 0) | |
4520 | break; | |
4521 | ||
4522 | key.offset--; | |
4523 | } | |
b3b4aa74 | 4524 | btrfs_release_path(path); |
5d4f98a2 YZ |
4525 | |
4526 | if (list_empty(&reloc_roots)) | |
4527 | goto out; | |
4528 | ||
c258d6e3 | 4529 | rc = alloc_reloc_control(fs_info); |
5d4f98a2 YZ |
4530 | if (!rc) { |
4531 | err = -ENOMEM; | |
4532 | goto out; | |
4533 | } | |
4534 | ||
0b246afa | 4535 | rc->extent_root = fs_info->extent_root; |
5d4f98a2 YZ |
4536 | |
4537 | set_reloc_control(rc); | |
4538 | ||
7a7eaa40 | 4539 | trans = btrfs_join_transaction(rc->extent_root); |
3612b495 | 4540 | if (IS_ERR(trans)) { |
3612b495 | 4541 | err = PTR_ERR(trans); |
fb2d83ee | 4542 | goto out_unset; |
3612b495 | 4543 | } |
3fd0a558 YZ |
4544 | |
4545 | rc->merge_reloc_tree = 1; | |
4546 | ||
5d4f98a2 YZ |
4547 | while (!list_empty(&reloc_roots)) { |
4548 | reloc_root = list_entry(reloc_roots.next, | |
4549 | struct btrfs_root, root_list); | |
4550 | list_del(&reloc_root->root_list); | |
4551 | ||
4552 | if (btrfs_root_refs(&reloc_root->root_item) == 0) { | |
4553 | list_add_tail(&reloc_root->root_list, | |
4554 | &rc->reloc_roots); | |
4555 | continue; | |
4556 | } | |
4557 | ||
0b246afa | 4558 | fs_root = read_fs_root(fs_info, reloc_root->root_key.offset); |
79787eaa JM |
4559 | if (IS_ERR(fs_root)) { |
4560 | err = PTR_ERR(fs_root); | |
ca1aa281 | 4561 | list_add_tail(&reloc_root->root_list, &reloc_roots); |
1402d17d | 4562 | btrfs_end_transaction(trans); |
fb2d83ee | 4563 | goto out_unset; |
79787eaa | 4564 | } |
5d4f98a2 | 4565 | |
ffd7b339 | 4566 | err = __add_reloc_root(reloc_root); |
79787eaa | 4567 | BUG_ON(err < 0); /* -ENOMEM or logic error */ |
f44deb74 | 4568 | fs_root->reloc_root = btrfs_grab_root(reloc_root); |
00246528 | 4569 | btrfs_put_root(fs_root); |
5d4f98a2 YZ |
4570 | } |
4571 | ||
3a45bb20 | 4572 | err = btrfs_commit_transaction(trans); |
79787eaa | 4573 | if (err) |
fb2d83ee | 4574 | goto out_unset; |
5d4f98a2 YZ |
4575 | |
4576 | merge_reloc_roots(rc); | |
4577 | ||
4578 | unset_reloc_control(rc); | |
4579 | ||
7a7eaa40 | 4580 | trans = btrfs_join_transaction(rc->extent_root); |
62b99540 | 4581 | if (IS_ERR(trans)) { |
3612b495 | 4582 | err = PTR_ERR(trans); |
6217b0fa | 4583 | goto out_clean; |
62b99540 | 4584 | } |
3a45bb20 | 4585 | err = btrfs_commit_transaction(trans); |
6217b0fa | 4586 | out_clean: |
d2311e69 QW |
4587 | ret = clean_dirty_subvols(rc); |
4588 | if (ret < 0 && !err) | |
4589 | err = ret; | |
fb2d83ee JB |
4590 | out_unset: |
4591 | unset_reloc_control(rc); | |
1a0afa0e | 4592 | free_reloc_control(rc); |
3612b495 | 4593 | out: |
aca1bba6 LB |
4594 | if (!list_empty(&reloc_roots)) |
4595 | free_reloc_roots(&reloc_roots); | |
4596 | ||
5d4f98a2 YZ |
4597 | btrfs_free_path(path); |
4598 | ||
4599 | if (err == 0) { | |
4600 | /* cleanup orphan inode in data relocation tree */ | |
0b246afa | 4601 | fs_root = read_fs_root(fs_info, BTRFS_DATA_RELOC_TREE_OBJECTID); |
932fd26d | 4602 | if (IS_ERR(fs_root)) { |
5d4f98a2 | 4603 | err = PTR_ERR(fs_root); |
932fd26d | 4604 | } else { |
9f583209 | 4605 | err = btrfs_orphan_cleanup(fs_root); |
00246528 | 4606 | btrfs_put_root(fs_root); |
932fd26d | 4607 | } |
5d4f98a2 YZ |
4608 | } |
4609 | return err; | |
4610 | } | |
4611 | ||
4612 | /* | |
4613 | * helper to add ordered checksum for data relocation. | |
4614 | * | |
4615 | * cloning checksum properly handles the nodatasum extents. | |
4616 | * it also saves CPU time to re-calculate the checksum. | |
4617 | */ | |
4618 | int btrfs_reloc_clone_csums(struct inode *inode, u64 file_pos, u64 len) | |
4619 | { | |
0b246afa | 4620 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
5d4f98a2 | 4621 | struct btrfs_ordered_sum *sums; |
5d4f98a2 | 4622 | struct btrfs_ordered_extent *ordered; |
5d4f98a2 YZ |
4623 | int ret; |
4624 | u64 disk_bytenr; | |
4577b014 | 4625 | u64 new_bytenr; |
5d4f98a2 YZ |
4626 | LIST_HEAD(list); |
4627 | ||
4628 | ordered = btrfs_lookup_ordered_extent(inode, file_pos); | |
bffe633e | 4629 | BUG_ON(ordered->file_offset != file_pos || ordered->num_bytes != len); |
5d4f98a2 YZ |
4630 | |
4631 | disk_bytenr = file_pos + BTRFS_I(inode)->index_cnt; | |
0b246afa | 4632 | ret = btrfs_lookup_csums_range(fs_info->csum_root, disk_bytenr, |
a2de733c | 4633 | disk_bytenr + len - 1, &list, 0); |
79787eaa JM |
4634 | if (ret) |
4635 | goto out; | |
5d4f98a2 YZ |
4636 | |
4637 | while (!list_empty(&list)) { | |
4638 | sums = list_entry(list.next, struct btrfs_ordered_sum, list); | |
4639 | list_del_init(&sums->list); | |
4640 | ||
4577b014 JB |
4641 | /* |
4642 | * We need to offset the new_bytenr based on where the csum is. | |
4643 | * We need to do this because we will read in entire prealloc | |
4644 | * extents but we may have written to say the middle of the | |
4645 | * prealloc extent, so we need to make sure the csum goes with | |
4646 | * the right disk offset. | |
4647 | * | |
4648 | * We can do this because the data reloc inode refers strictly | |
4649 | * to the on disk bytes, so we don't have to worry about | |
4650 | * disk_len vs real len like with real inodes since it's all | |
4651 | * disk length. | |
4652 | */ | |
bffe633e | 4653 | new_bytenr = ordered->disk_bytenr + sums->bytenr - disk_bytenr; |
4577b014 | 4654 | sums->bytenr = new_bytenr; |
5d4f98a2 | 4655 | |
f9756261 | 4656 | btrfs_add_ordered_sum(ordered, sums); |
5d4f98a2 | 4657 | } |
79787eaa | 4658 | out: |
5d4f98a2 | 4659 | btrfs_put_ordered_extent(ordered); |
411fc6bc | 4660 | return ret; |
5d4f98a2 | 4661 | } |
3fd0a558 | 4662 | |
83d4cfd4 JB |
4663 | int btrfs_reloc_cow_block(struct btrfs_trans_handle *trans, |
4664 | struct btrfs_root *root, struct extent_buffer *buf, | |
4665 | struct extent_buffer *cow) | |
3fd0a558 | 4666 | { |
0b246afa | 4667 | struct btrfs_fs_info *fs_info = root->fs_info; |
3fd0a558 YZ |
4668 | struct reloc_control *rc; |
4669 | struct backref_node *node; | |
4670 | int first_cow = 0; | |
4671 | int level; | |
83d4cfd4 | 4672 | int ret = 0; |
3fd0a558 | 4673 | |
0b246afa | 4674 | rc = fs_info->reloc_ctl; |
3fd0a558 | 4675 | if (!rc) |
83d4cfd4 | 4676 | return 0; |
3fd0a558 YZ |
4677 | |
4678 | BUG_ON(rc->stage == UPDATE_DATA_PTRS && | |
4679 | root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID); | |
4680 | ||
4681 | level = btrfs_header_level(buf); | |
4682 | if (btrfs_header_generation(buf) <= | |
4683 | btrfs_root_last_snapshot(&root->root_item)) | |
4684 | first_cow = 1; | |
4685 | ||
4686 | if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID && | |
4687 | rc->create_reloc_tree) { | |
4688 | WARN_ON(!first_cow && level == 0); | |
4689 | ||
4690 | node = rc->backref_cache.path[level]; | |
4691 | BUG_ON(node->bytenr != buf->start && | |
4692 | node->new_bytenr != buf->start); | |
4693 | ||
4694 | drop_node_buffer(node); | |
67439dad | 4695 | atomic_inc(&cow->refs); |
3fd0a558 YZ |
4696 | node->eb = cow; |
4697 | node->new_bytenr = cow->start; | |
4698 | ||
4699 | if (!node->pending) { | |
4700 | list_move_tail(&node->list, | |
4701 | &rc->backref_cache.pending[level]); | |
4702 | node->pending = 1; | |
4703 | } | |
4704 | ||
4705 | if (first_cow) | |
4706 | __mark_block_processed(rc, node); | |
4707 | ||
4708 | if (first_cow && level > 0) | |
4709 | rc->nodes_relocated += buf->len; | |
4710 | } | |
4711 | ||
83d4cfd4 | 4712 | if (level == 0 && first_cow && rc->stage == UPDATE_DATA_PTRS) |
3fd0a558 | 4713 | ret = replace_file_extents(trans, rc, root, cow); |
83d4cfd4 | 4714 | return ret; |
3fd0a558 YZ |
4715 | } |
4716 | ||
4717 | /* | |
4718 | * called before creating snapshot. it calculates metadata reservation | |
01327610 | 4719 | * required for relocating tree blocks in the snapshot |
3fd0a558 | 4720 | */ |
147d256e | 4721 | void btrfs_reloc_pre_snapshot(struct btrfs_pending_snapshot *pending, |
3fd0a558 YZ |
4722 | u64 *bytes_to_reserve) |
4723 | { | |
10995c04 QW |
4724 | struct btrfs_root *root = pending->root; |
4725 | struct reloc_control *rc = root->fs_info->reloc_ctl; | |
3fd0a558 | 4726 | |
6282675e | 4727 | if (!rc || !have_reloc_root(root)) |
3fd0a558 YZ |
4728 | return; |
4729 | ||
3fd0a558 YZ |
4730 | if (!rc->merge_reloc_tree) |
4731 | return; | |
4732 | ||
4733 | root = root->reloc_root; | |
4734 | BUG_ON(btrfs_root_refs(&root->root_item) == 0); | |
4735 | /* | |
4736 | * relocation is in the stage of merging trees. the space | |
4737 | * used by merging a reloc tree is twice the size of | |
4738 | * relocated tree nodes in the worst case. half for cowing | |
4739 | * the reloc tree, half for cowing the fs tree. the space | |
4740 | * used by cowing the reloc tree will be freed after the | |
4741 | * tree is dropped. if we create snapshot, cowing the fs | |
4742 | * tree may use more space than it frees. so we need | |
4743 | * reserve extra space. | |
4744 | */ | |
4745 | *bytes_to_reserve += rc->nodes_relocated; | |
4746 | } | |
4747 | ||
4748 | /* | |
4749 | * called after snapshot is created. migrate block reservation | |
4750 | * and create reloc root for the newly created snapshot | |
f44deb74 JB |
4751 | * |
4752 | * This is similar to btrfs_init_reloc_root(), we come out of here with two | |
4753 | * references held on the reloc_root, one for root->reloc_root and one for | |
4754 | * rc->reloc_roots. | |
3fd0a558 | 4755 | */ |
49b25e05 | 4756 | int btrfs_reloc_post_snapshot(struct btrfs_trans_handle *trans, |
3fd0a558 YZ |
4757 | struct btrfs_pending_snapshot *pending) |
4758 | { | |
4759 | struct btrfs_root *root = pending->root; | |
4760 | struct btrfs_root *reloc_root; | |
4761 | struct btrfs_root *new_root; | |
10995c04 | 4762 | struct reloc_control *rc = root->fs_info->reloc_ctl; |
3fd0a558 YZ |
4763 | int ret; |
4764 | ||
6282675e | 4765 | if (!rc || !have_reloc_root(root)) |
49b25e05 | 4766 | return 0; |
3fd0a558 YZ |
4767 | |
4768 | rc = root->fs_info->reloc_ctl; | |
4769 | rc->merging_rsv_size += rc->nodes_relocated; | |
4770 | ||
4771 | if (rc->merge_reloc_tree) { | |
4772 | ret = btrfs_block_rsv_migrate(&pending->block_rsv, | |
4773 | rc->block_rsv, | |
3a584174 | 4774 | rc->nodes_relocated, true); |
49b25e05 JM |
4775 | if (ret) |
4776 | return ret; | |
3fd0a558 YZ |
4777 | } |
4778 | ||
4779 | new_root = pending->snap; | |
4780 | reloc_root = create_reloc_root(trans, root->reloc_root, | |
4781 | new_root->root_key.objectid); | |
49b25e05 JM |
4782 | if (IS_ERR(reloc_root)) |
4783 | return PTR_ERR(reloc_root); | |
3fd0a558 | 4784 | |
ffd7b339 JM |
4785 | ret = __add_reloc_root(reloc_root); |
4786 | BUG_ON(ret < 0); | |
f44deb74 | 4787 | new_root->reloc_root = btrfs_grab_root(reloc_root); |
3fd0a558 | 4788 | |
49b25e05 | 4789 | if (rc->create_reloc_tree) |
3fd0a558 | 4790 | ret = clone_backref_node(trans, rc, root, reloc_root); |
49b25e05 | 4791 | return ret; |
3fd0a558 | 4792 | } |