blob: 00504657b60225f5c00346f16098609db679f90e [file] [log] [blame]
David Sterbac1d7c512018-04-03 19:23:33 +02001// SPDX-License-Identifier: GPL-2.0
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002/*
3 * Copyright (C) 2009 Oracle. All rights reserved.
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004 */
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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090011#include <linux/slab.h>
Yan Zheng5d4f98a2009-06-10 10:45:14 -040012#include "ctree.h"
13#include "disk-io.h"
14#include "transaction.h"
15#include "volumes.h"
16#include "locking.h"
17#include "btrfs_inode.h"
18#include "async-thread.h"
Josef Bacik0af3d002010-06-21 14:48:16 -040019#include "free-space-cache.h"
Li Zefan581bb052011-04-20 10:06:11 +080020#include "inode-map.h"
Qu Wenruo62b99542016-08-15 10:36:51 +080021#include "qgroup.h"
Liu Bocdccee92017-08-18 15:15:23 -060022#include "print-tree.h"
Josef Bacik86736342019-06-19 15:12:00 -040023#include "delalloc-space.h"
Josef Bacikaac00232019-06-20 15:37:44 -040024#include "block-group.h"
Yan Zheng5d4f98a2009-06-10 10:45:14 -040025
26/*
27 * backref_node, mapping_node and tree_block start with this
28 */
29struct tree_entry {
30 struct rb_node rb_node;
31 u64 bytenr;
32};
33
34/*
35 * present a tree block in the backref cache
36 */
37struct backref_node {
38 struct rb_node rb_node;
39 u64 bytenr;
Yan, Zheng3fd0a552010-05-16 10:49:59 -040040
41 u64 new_bytenr;
42 /* objectid of tree block owner, can be not uptodate */
Yan Zheng5d4f98a2009-06-10 10:45:14 -040043 u64 owner;
Yan, Zheng3fd0a552010-05-16 10:49:59 -040044 /* link to pending, changed or detached list */
45 struct list_head list;
Yan Zheng5d4f98a2009-06-10 10:45:14 -040046 /* list of upper level blocks reference this block */
47 struct list_head upper;
48 /* list of child blocks in the cache */
49 struct list_head lower;
50 /* NULL if this node is not tree root */
51 struct btrfs_root *root;
52 /* extent buffer got by COW the block */
53 struct extent_buffer *eb;
54 /* level of tree block */
55 unsigned int level:8;
Yan, Zheng3fd0a552010-05-16 10:49:59 -040056 /* is the block in non-reference counted tree */
57 unsigned int cowonly:1;
58 /* 1 if no child node in the cache */
Yan Zheng5d4f98a2009-06-10 10:45:14 -040059 unsigned int lowest:1;
60 /* is the extent buffer locked */
61 unsigned int locked:1;
62 /* has the block been processed */
63 unsigned int processed:1;
64 /* have backrefs of this block been checked */
65 unsigned int checked:1;
Yan, Zheng3fd0a552010-05-16 10:49:59 -040066 /*
67 * 1 if corresponding block has been cowed but some upper
68 * level block pointers may not point to the new location
69 */
70 unsigned int pending:1;
71 /*
72 * 1 if the backref node isn't connected to any other
73 * backref node.
74 */
75 unsigned int detached:1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -040076};
77
78/*
79 * present a block pointer in the backref cache
80 */
81struct backref_edge {
82 struct list_head list[2];
83 struct backref_node *node[2];
Yan Zheng5d4f98a2009-06-10 10:45:14 -040084};
85
86#define LOWER 0
87#define UPPER 1
Wang Shilong0647bf52013-11-20 09:01:52 +080088#define RELOCATION_RESERVED_NODES 256
Yan Zheng5d4f98a2009-06-10 10:45:14 -040089
90struct backref_cache {
91 /* red black tree of all backref nodes in the cache */
92 struct rb_root rb_root;
Yan, Zheng3fd0a552010-05-16 10:49:59 -040093 /* for passing backref nodes to btrfs_reloc_cow_block */
94 struct backref_node *path[BTRFS_MAX_LEVEL];
95 /*
96 * list of blocks that have been cowed but some block
97 * pointers in upper level blocks may not reflect the
98 * new location
99 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400100 struct list_head pending[BTRFS_MAX_LEVEL];
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400101 /* list of backref nodes with no child node */
102 struct list_head leaves;
103 /* list of blocks that have been cowed in current transaction */
104 struct list_head changed;
105 /* list of detached backref node. */
106 struct list_head detached;
107
108 u64 last_trans;
109
110 int nr_nodes;
111 int nr_edges;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400112};
113
114/*
115 * map address of tree root to tree
116 */
117struct mapping_node {
118 struct rb_node rb_node;
119 u64 bytenr;
120 void *data;
121};
122
123struct mapping_tree {
124 struct rb_root rb_root;
125 spinlock_t lock;
126};
127
128/*
129 * present a tree block to process
130 */
131struct tree_block {
132 struct rb_node rb_node;
133 u64 bytenr;
134 struct btrfs_key key;
135 unsigned int level:8;
136 unsigned int key_ready:1;
137};
138
Yan, Zheng0257bb82009-09-24 09:17:31 -0400139#define MAX_EXTENTS 128
140
141struct file_extent_cluster {
142 u64 start;
143 u64 end;
144 u64 boundary[MAX_EXTENTS];
145 unsigned int nr;
146};
147
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400148struct reloc_control {
149 /* block group to relocate */
150 struct btrfs_block_group_cache *block_group;
151 /* extent tree */
152 struct btrfs_root *extent_root;
153 /* inode for moving data */
154 struct inode *data_inode;
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400155
156 struct btrfs_block_rsv *block_rsv;
157
158 struct backref_cache backref_cache;
159
160 struct file_extent_cluster cluster;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400161 /* tree blocks have been processed */
162 struct extent_io_tree processed_blocks;
163 /* map start of tree root to corresponding reloc tree */
164 struct mapping_tree reloc_root_tree;
165 /* list of reloc trees */
166 struct list_head reloc_roots;
Qu Wenruod2311e62019-01-23 15:15:14 +0800167 /* list of subvolume trees that get relocated */
168 struct list_head dirty_subvol_roots;
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400169 /* size of metadata reservation for merging reloc trees */
170 u64 merging_rsv_size;
171 /* size of relocated tree nodes */
172 u64 nodes_relocated;
Wang Shilong0647bf52013-11-20 09:01:52 +0800173 /* reserved size for block group relocation*/
174 u64 reserved_bytes;
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400175
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400176 u64 search_start;
177 u64 extents_found;
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400178
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400179 unsigned int stage:8;
180 unsigned int create_reloc_tree:1;
181 unsigned int merge_reloc_tree:1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400182 unsigned int found_file_extent:1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400183};
184
185/* stages of data relocation */
186#define MOVE_DATA_EXTENTS 0
187#define UPDATE_DATA_PTRS 1
188
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400189static void remove_backref_node(struct backref_cache *cache,
190 struct backref_node *node);
191static void __mark_block_processed(struct reloc_control *rc,
192 struct backref_node *node);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400193
194static void mapping_tree_init(struct mapping_tree *tree)
195{
Eric Paris6bef4d32010-02-23 19:43:04 +0000196 tree->rb_root = RB_ROOT;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400197 spin_lock_init(&tree->lock);
198}
199
200static void backref_cache_init(struct backref_cache *cache)
201{
202 int i;
Eric Paris6bef4d32010-02-23 19:43:04 +0000203 cache->rb_root = RB_ROOT;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400204 for (i = 0; i < BTRFS_MAX_LEVEL; i++)
205 INIT_LIST_HEAD(&cache->pending[i]);
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400206 INIT_LIST_HEAD(&cache->changed);
207 INIT_LIST_HEAD(&cache->detached);
208 INIT_LIST_HEAD(&cache->leaves);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400209}
210
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400211static void backref_cache_cleanup(struct backref_cache *cache)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400212{
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400213 struct backref_node *node;
214 int i;
215
216 while (!list_empty(&cache->detached)) {
217 node = list_entry(cache->detached.next,
218 struct backref_node, list);
219 remove_backref_node(cache, node);
220 }
221
222 while (!list_empty(&cache->leaves)) {
223 node = list_entry(cache->leaves.next,
224 struct backref_node, lower);
225 remove_backref_node(cache, node);
226 }
227
228 cache->last_trans = 0;
229
230 for (i = 0; i < BTRFS_MAX_LEVEL; i++)
Liu Bof4907092016-07-11 18:52:57 -0700231 ASSERT(list_empty(&cache->pending[i]));
232 ASSERT(list_empty(&cache->changed));
233 ASSERT(list_empty(&cache->detached));
234 ASSERT(RB_EMPTY_ROOT(&cache->rb_root));
235 ASSERT(!cache->nr_nodes);
236 ASSERT(!cache->nr_edges);
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400237}
238
239static struct backref_node *alloc_backref_node(struct backref_cache *cache)
240{
241 struct backref_node *node;
242
243 node = kzalloc(sizeof(*node), GFP_NOFS);
244 if (node) {
245 INIT_LIST_HEAD(&node->list);
246 INIT_LIST_HEAD(&node->upper);
247 INIT_LIST_HEAD(&node->lower);
248 RB_CLEAR_NODE(&node->rb_node);
249 cache->nr_nodes++;
250 }
251 return node;
252}
253
254static void free_backref_node(struct backref_cache *cache,
255 struct backref_node *node)
256{
257 if (node) {
258 cache->nr_nodes--;
259 kfree(node);
260 }
261}
262
263static struct backref_edge *alloc_backref_edge(struct backref_cache *cache)
264{
265 struct backref_edge *edge;
266
267 edge = kzalloc(sizeof(*edge), GFP_NOFS);
268 if (edge)
269 cache->nr_edges++;
270 return edge;
271}
272
273static void free_backref_edge(struct backref_cache *cache,
274 struct backref_edge *edge)
275{
276 if (edge) {
277 cache->nr_edges--;
278 kfree(edge);
279 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400280}
281
282static struct rb_node *tree_insert(struct rb_root *root, u64 bytenr,
283 struct rb_node *node)
284{
285 struct rb_node **p = &root->rb_node;
286 struct rb_node *parent = NULL;
287 struct tree_entry *entry;
288
289 while (*p) {
290 parent = *p;
291 entry = rb_entry(parent, struct tree_entry, rb_node);
292
293 if (bytenr < entry->bytenr)
294 p = &(*p)->rb_left;
295 else if (bytenr > entry->bytenr)
296 p = &(*p)->rb_right;
297 else
298 return parent;
299 }
300
301 rb_link_node(node, parent, p);
302 rb_insert_color(node, root);
303 return NULL;
304}
305
306static struct rb_node *tree_search(struct rb_root *root, u64 bytenr)
307{
308 struct rb_node *n = root->rb_node;
309 struct tree_entry *entry;
310
311 while (n) {
312 entry = rb_entry(n, struct tree_entry, rb_node);
313
314 if (bytenr < entry->bytenr)
315 n = n->rb_left;
316 else if (bytenr > entry->bytenr)
317 n = n->rb_right;
318 else
319 return n;
320 }
321 return NULL;
322}
323
Eric Sandeen48a3b632013-04-25 20:41:01 +0000324static void backref_tree_panic(struct rb_node *rb_node, int errno, u64 bytenr)
Jeff Mahoney43c04fb2011-10-03 23:22:33 -0400325{
326
327 struct btrfs_fs_info *fs_info = NULL;
328 struct backref_node *bnode = rb_entry(rb_node, struct backref_node,
329 rb_node);
330 if (bnode->root)
331 fs_info = bnode->root->fs_info;
Jeff Mahoney5d163e02016-09-20 10:05:00 -0400332 btrfs_panic(fs_info, errno,
333 "Inconsistency in backref cache found at offset %llu",
334 bytenr);
Jeff Mahoney43c04fb2011-10-03 23:22:33 -0400335}
336
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400337/*
338 * walk up backref nodes until reach node presents tree root
339 */
340static struct backref_node *walk_up_backref(struct backref_node *node,
341 struct backref_edge *edges[],
342 int *index)
343{
344 struct backref_edge *edge;
345 int idx = *index;
346
347 while (!list_empty(&node->upper)) {
348 edge = list_entry(node->upper.next,
349 struct backref_edge, list[LOWER]);
350 edges[idx++] = edge;
351 node = edge->node[UPPER];
352 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400353 BUG_ON(node->detached);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400354 *index = idx;
355 return node;
356}
357
358/*
359 * walk down backref nodes to find start of next reference path
360 */
361static struct backref_node *walk_down_backref(struct backref_edge *edges[],
362 int *index)
363{
364 struct backref_edge *edge;
365 struct backref_node *lower;
366 int idx = *index;
367
368 while (idx > 0) {
369 edge = edges[idx - 1];
370 lower = edge->node[LOWER];
371 if (list_is_last(&edge->list[LOWER], &lower->upper)) {
372 idx--;
373 continue;
374 }
375 edge = list_entry(edge->list[LOWER].next,
376 struct backref_edge, list[LOWER]);
377 edges[idx - 1] = edge;
378 *index = idx;
379 return edge->node[UPPER];
380 }
381 *index = 0;
382 return NULL;
383}
384
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400385static void unlock_node_buffer(struct backref_node *node)
386{
387 if (node->locked) {
388 btrfs_tree_unlock(node->eb);
389 node->locked = 0;
390 }
391}
392
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400393static void drop_node_buffer(struct backref_node *node)
394{
395 if (node->eb) {
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400396 unlock_node_buffer(node);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400397 free_extent_buffer(node->eb);
398 node->eb = NULL;
399 }
400}
401
402static void drop_backref_node(struct backref_cache *tree,
403 struct backref_node *node)
404{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400405 BUG_ON(!list_empty(&node->upper));
406
407 drop_node_buffer(node);
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400408 list_del(&node->list);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400409 list_del(&node->lower);
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400410 if (!RB_EMPTY_NODE(&node->rb_node))
411 rb_erase(&node->rb_node, &tree->rb_root);
412 free_backref_node(tree, node);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400413}
414
415/*
416 * remove a backref node from the backref cache
417 */
418static void remove_backref_node(struct backref_cache *cache,
419 struct backref_node *node)
420{
421 struct backref_node *upper;
422 struct backref_edge *edge;
423
424 if (!node)
425 return;
426
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400427 BUG_ON(!node->lowest && !node->detached);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400428 while (!list_empty(&node->upper)) {
429 edge = list_entry(node->upper.next, struct backref_edge,
430 list[LOWER]);
431 upper = edge->node[UPPER];
432 list_del(&edge->list[LOWER]);
433 list_del(&edge->list[UPPER]);
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400434 free_backref_edge(cache, edge);
435
436 if (RB_EMPTY_NODE(&upper->rb_node)) {
437 BUG_ON(!list_empty(&node->upper));
438 drop_backref_node(cache, node);
439 node = upper;
440 node->lowest = 1;
441 continue;
442 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400443 /*
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400444 * add the node to leaf node list if no other
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400445 * child block cached.
446 */
447 if (list_empty(&upper->lower)) {
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400448 list_add_tail(&upper->lower, &cache->leaves);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400449 upper->lowest = 1;
450 }
451 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400452
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400453 drop_backref_node(cache, node);
454}
455
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400456static void update_backref_node(struct backref_cache *cache,
457 struct backref_node *node, u64 bytenr)
458{
459 struct rb_node *rb_node;
460 rb_erase(&node->rb_node, &cache->rb_root);
461 node->bytenr = bytenr;
462 rb_node = tree_insert(&cache->rb_root, node->bytenr, &node->rb_node);
Jeff Mahoney43c04fb2011-10-03 23:22:33 -0400463 if (rb_node)
464 backref_tree_panic(rb_node, -EEXIST, bytenr);
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400465}
466
467/*
468 * update backref cache after a transaction commit
469 */
470static int update_backref_cache(struct btrfs_trans_handle *trans,
471 struct backref_cache *cache)
472{
473 struct backref_node *node;
474 int level = 0;
475
476 if (cache->last_trans == 0) {
477 cache->last_trans = trans->transid;
478 return 0;
479 }
480
481 if (cache->last_trans == trans->transid)
482 return 0;
483
484 /*
485 * detached nodes are used to avoid unnecessary backref
486 * lookup. transaction commit changes the extent tree.
487 * so the detached nodes are no longer useful.
488 */
489 while (!list_empty(&cache->detached)) {
490 node = list_entry(cache->detached.next,
491 struct backref_node, list);
492 remove_backref_node(cache, node);
493 }
494
495 while (!list_empty(&cache->changed)) {
496 node = list_entry(cache->changed.next,
497 struct backref_node, list);
498 list_del_init(&node->list);
499 BUG_ON(node->pending);
500 update_backref_node(cache, node, node->new_bytenr);
501 }
502
503 /*
504 * some nodes can be left in the pending list if there were
505 * errors during processing the pending nodes.
506 */
507 for (level = 0; level < BTRFS_MAX_LEVEL; level++) {
508 list_for_each_entry(node, &cache->pending[level], list) {
509 BUG_ON(!node->pending);
510 if (node->bytenr == node->new_bytenr)
511 continue;
512 update_backref_node(cache, node, node->new_bytenr);
513 }
514 }
515
516 cache->last_trans = 0;
517 return 1;
518}
519
David Sterbaf2a97a92011-05-05 12:44:41 +0200520
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400521static int should_ignore_root(struct btrfs_root *root)
522{
523 struct btrfs_root *reloc_root;
524
Miao Xie27cdeb72014-04-02 19:51:05 +0800525 if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state))
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400526 return 0;
527
528 reloc_root = root->reloc_root;
529 if (!reloc_root)
530 return 0;
531
532 if (btrfs_root_last_snapshot(&reloc_root->root_item) ==
533 root->fs_info->running_transaction->transid - 1)
534 return 0;
535 /*
536 * if there is reloc tree and it was created in previous
537 * transaction backref lookup can find the reloc tree,
538 * so backref node for the fs tree root is useless for
539 * relocation.
540 */
541 return 1;
542}
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400543/*
544 * find reloc tree by address of tree root
545 */
546static struct btrfs_root *find_reloc_root(struct reloc_control *rc,
547 u64 bytenr)
548{
549 struct rb_node *rb_node;
550 struct mapping_node *node;
551 struct btrfs_root *root = NULL;
552
553 spin_lock(&rc->reloc_root_tree.lock);
554 rb_node = tree_search(&rc->reloc_root_tree.rb_root, bytenr);
555 if (rb_node) {
556 node = rb_entry(rb_node, struct mapping_node, rb_node);
557 root = (struct btrfs_root *)node->data;
558 }
559 spin_unlock(&rc->reloc_root_tree.lock);
560 return root;
561}
562
563static int is_cowonly_root(u64 root_objectid)
564{
565 if (root_objectid == BTRFS_ROOT_TREE_OBJECTID ||
566 root_objectid == BTRFS_EXTENT_TREE_OBJECTID ||
567 root_objectid == BTRFS_CHUNK_TREE_OBJECTID ||
568 root_objectid == BTRFS_DEV_TREE_OBJECTID ||
569 root_objectid == BTRFS_TREE_LOG_OBJECTID ||
Wang Shilong66463742013-12-10 00:14:34 +0800570 root_objectid == BTRFS_CSUM_TREE_OBJECTID ||
571 root_objectid == BTRFS_UUID_TREE_OBJECTID ||
David Sterba3e4c5ef2016-01-25 16:47:10 +0100572 root_objectid == BTRFS_QUOTA_TREE_OBJECTID ||
573 root_objectid == BTRFS_FREE_SPACE_TREE_OBJECTID)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400574 return 1;
575 return 0;
576}
577
578static struct btrfs_root *read_fs_root(struct btrfs_fs_info *fs_info,
579 u64 root_objectid)
580{
581 struct btrfs_key key;
582
583 key.objectid = root_objectid;
584 key.type = BTRFS_ROOT_ITEM_KEY;
585 if (is_cowonly_root(root_objectid))
586 key.offset = 0;
587 else
588 key.offset = (u64)-1;
589
Miao Xiec00869f2013-09-25 21:47:44 +0800590 return btrfs_get_fs_root(fs_info, &key, false);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400591}
592
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400593static noinline_for_stack
594int find_inline_backref(struct extent_buffer *leaf, int slot,
595 unsigned long *ptr, unsigned long *end)
596{
Josef Bacik3173a182013-03-07 14:22:04 -0500597 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400598 struct btrfs_extent_item *ei;
599 struct btrfs_tree_block_info *bi;
600 u32 item_size;
601
Josef Bacik3173a182013-03-07 14:22:04 -0500602 btrfs_item_key_to_cpu(leaf, &key, slot);
603
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400604 item_size = btrfs_item_size_nr(leaf, slot);
Nikolay Borisovba3c2b12018-06-26 16:57:36 +0300605 if (item_size < sizeof(*ei)) {
606 btrfs_print_v0_err(leaf->fs_info);
607 btrfs_handle_fs_error(leaf->fs_info, -EINVAL, NULL);
608 return 1;
609 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400610 ei = btrfs_item_ptr(leaf, slot, struct btrfs_extent_item);
611 WARN_ON(!(btrfs_extent_flags(leaf, ei) &
612 BTRFS_EXTENT_FLAG_TREE_BLOCK));
613
Josef Bacik3173a182013-03-07 14:22:04 -0500614 if (key.type == BTRFS_EXTENT_ITEM_KEY &&
615 item_size <= sizeof(*ei) + sizeof(*bi)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400616 WARN_ON(item_size < sizeof(*ei) + sizeof(*bi));
617 return 1;
618 }
Josef Bacikd062d132013-07-30 15:44:09 -0400619 if (key.type == BTRFS_METADATA_ITEM_KEY &&
620 item_size <= sizeof(*ei)) {
621 WARN_ON(item_size < sizeof(*ei));
622 return 1;
623 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400624
Josef Bacik3173a182013-03-07 14:22:04 -0500625 if (key.type == BTRFS_EXTENT_ITEM_KEY) {
626 bi = (struct btrfs_tree_block_info *)(ei + 1);
627 *ptr = (unsigned long)(bi + 1);
628 } else {
629 *ptr = (unsigned long)(ei + 1);
630 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400631 *end = (unsigned long)ei + item_size;
632 return 0;
633}
634
635/*
636 * build backref tree for a given tree block. root of the backref tree
637 * corresponds the tree block, leaves of the backref tree correspond
638 * roots of b-trees that reference the tree block.
639 *
640 * the basic idea of this function is check backrefs of a given block
Nicholas D Steeves01327612016-05-19 21:18:45 -0400641 * to find upper level blocks that reference the block, and then check
642 * backrefs of these upper level blocks recursively. the recursion stop
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400643 * when tree root is reached or backrefs for the block is cached.
644 *
645 * NOTE: if we find backrefs for a block are cached, we know backrefs
646 * for all upper level blocks that directly/indirectly reference the
647 * block are also cached.
648 */
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400649static noinline_for_stack
650struct backref_node *build_backref_tree(struct reloc_control *rc,
651 struct btrfs_key *node_key,
652 int level, u64 bytenr)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400653{
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400654 struct backref_cache *cache = &rc->backref_cache;
Qu Wenruofa6ac712018-09-25 14:37:46 +0800655 struct btrfs_path *path1; /* For searching extent root */
656 struct btrfs_path *path2; /* For searching parent of TREE_BLOCK_REF */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400657 struct extent_buffer *eb;
658 struct btrfs_root *root;
659 struct backref_node *cur;
660 struct backref_node *upper;
661 struct backref_node *lower;
662 struct backref_node *node = NULL;
663 struct backref_node *exist = NULL;
664 struct backref_edge *edge;
665 struct rb_node *rb_node;
666 struct btrfs_key key;
667 unsigned long end;
668 unsigned long ptr;
Qu Wenruofa6ac712018-09-25 14:37:46 +0800669 LIST_HEAD(list); /* Pending edge list, upper node needs to be checked */
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400670 LIST_HEAD(useless);
671 int cowonly;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400672 int ret;
673 int err = 0;
Josef Bacikb6c60c82013-07-30 16:30:30 -0400674 bool need_check = true;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400675
676 path1 = btrfs_alloc_path();
677 path2 = btrfs_alloc_path();
678 if (!path1 || !path2) {
679 err = -ENOMEM;
680 goto out;
681 }
David Sterbae4058b52015-11-27 16:31:35 +0100682 path1->reada = READA_FORWARD;
683 path2->reada = READA_FORWARD;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400684
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400685 node = alloc_backref_node(cache);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400686 if (!node) {
687 err = -ENOMEM;
688 goto out;
689 }
690
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400691 node->bytenr = bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400692 node->level = level;
693 node->lowest = 1;
694 cur = node;
695again:
696 end = 0;
697 ptr = 0;
698 key.objectid = cur->bytenr;
Josef Bacik3173a182013-03-07 14:22:04 -0500699 key.type = BTRFS_METADATA_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400700 key.offset = (u64)-1;
701
702 path1->search_commit_root = 1;
703 path1->skip_locking = 1;
704 ret = btrfs_search_slot(NULL, rc->extent_root, &key, path1,
705 0, 0);
706 if (ret < 0) {
707 err = ret;
708 goto out;
709 }
Josef Bacik75bfb9af2014-09-19 10:40:00 -0400710 ASSERT(ret);
711 ASSERT(path1->slots[0]);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400712
713 path1->slots[0]--;
714
715 WARN_ON(cur->checked);
716 if (!list_empty(&cur->upper)) {
717 /*
Justin P. Mattock70f23fd2011-05-10 10:16:21 +0200718 * the backref was added previously when processing
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400719 * backref of type BTRFS_TREE_BLOCK_REF_KEY
720 */
Josef Bacik75bfb9af2014-09-19 10:40:00 -0400721 ASSERT(list_is_singular(&cur->upper));
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400722 edge = list_entry(cur->upper.next, struct backref_edge,
723 list[LOWER]);
Josef Bacik75bfb9af2014-09-19 10:40:00 -0400724 ASSERT(list_empty(&edge->list[UPPER]));
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400725 exist = edge->node[UPPER];
726 /*
727 * add the upper level block to pending list if we need
728 * check its backrefs
729 */
730 if (!exist->checked)
731 list_add_tail(&edge->list[UPPER], &list);
732 } else {
733 exist = NULL;
734 }
735
736 while (1) {
737 cond_resched();
738 eb = path1->nodes[0];
739
740 if (ptr >= end) {
741 if (path1->slots[0] >= btrfs_header_nritems(eb)) {
742 ret = btrfs_next_leaf(rc->extent_root, path1);
743 if (ret < 0) {
744 err = ret;
745 goto out;
746 }
747 if (ret > 0)
748 break;
749 eb = path1->nodes[0];
750 }
751
752 btrfs_item_key_to_cpu(eb, &key, path1->slots[0]);
753 if (key.objectid != cur->bytenr) {
754 WARN_ON(exist);
755 break;
756 }
757
Josef Bacik3173a182013-03-07 14:22:04 -0500758 if (key.type == BTRFS_EXTENT_ITEM_KEY ||
759 key.type == BTRFS_METADATA_ITEM_KEY) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400760 ret = find_inline_backref(eb, path1->slots[0],
761 &ptr, &end);
762 if (ret)
763 goto next;
764 }
765 }
766
767 if (ptr < end) {
768 /* update key for inline back ref */
769 struct btrfs_extent_inline_ref *iref;
Liu Bo3de28d52017-08-18 15:15:19 -0600770 int type;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400771 iref = (struct btrfs_extent_inline_ref *)ptr;
Liu Bo3de28d52017-08-18 15:15:19 -0600772 type = btrfs_get_extent_inline_ref_type(eb, iref,
773 BTRFS_REF_TYPE_BLOCK);
774 if (type == BTRFS_REF_TYPE_INVALID) {
Su Yueaf431dc2018-06-22 16:18:01 +0800775 err = -EUCLEAN;
Liu Bo3de28d52017-08-18 15:15:19 -0600776 goto out;
777 }
778 key.type = type;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400779 key.offset = btrfs_extent_inline_ref_offset(eb, iref);
Liu Bo3de28d52017-08-18 15:15:19 -0600780
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400781 WARN_ON(key.type != BTRFS_TREE_BLOCK_REF_KEY &&
782 key.type != BTRFS_SHARED_BLOCK_REF_KEY);
783 }
784
Qu Wenruofa6ac712018-09-25 14:37:46 +0800785 /*
786 * Parent node found and matches current inline ref, no need to
787 * rebuild this node for this inline ref.
788 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400789 if (exist &&
790 ((key.type == BTRFS_TREE_BLOCK_REF_KEY &&
791 exist->owner == key.offset) ||
792 (key.type == BTRFS_SHARED_BLOCK_REF_KEY &&
793 exist->bytenr == key.offset))) {
794 exist = NULL;
795 goto next;
796 }
797
Qu Wenruofa6ac712018-09-25 14:37:46 +0800798 /* SHARED_BLOCK_REF means key.offset is the parent bytenr */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400799 if (key.type == BTRFS_SHARED_BLOCK_REF_KEY) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400800 if (key.objectid == key.offset) {
801 /*
Qu Wenruofa6ac712018-09-25 14:37:46 +0800802 * Only root blocks of reloc trees use backref
803 * pointing to itself.
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400804 */
805 root = find_reloc_root(rc, cur->bytenr);
Josef Bacik75bfb9af2014-09-19 10:40:00 -0400806 ASSERT(root);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400807 cur->root = root;
808 break;
809 }
810
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400811 edge = alloc_backref_edge(cache);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400812 if (!edge) {
813 err = -ENOMEM;
814 goto out;
815 }
816 rb_node = tree_search(&cache->rb_root, key.offset);
817 if (!rb_node) {
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400818 upper = alloc_backref_node(cache);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400819 if (!upper) {
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400820 free_backref_edge(cache, edge);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400821 err = -ENOMEM;
822 goto out;
823 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400824 upper->bytenr = key.offset;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400825 upper->level = cur->level + 1;
826 /*
827 * backrefs for the upper level block isn't
828 * cached, add the block to pending list
829 */
830 list_add_tail(&edge->list[UPPER], &list);
831 } else {
832 upper = rb_entry(rb_node, struct backref_node,
833 rb_node);
Josef Bacik75bfb9af2014-09-19 10:40:00 -0400834 ASSERT(upper->checked);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400835 INIT_LIST_HEAD(&edge->list[UPPER]);
836 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400837 list_add_tail(&edge->list[LOWER], &cur->upper);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400838 edge->node[LOWER] = cur;
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400839 edge->node[UPPER] = upper;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400840
841 goto next;
David Sterba6d8ff4e2018-06-26 16:20:59 +0200842 } else if (unlikely(key.type == BTRFS_EXTENT_REF_V0_KEY)) {
Nikolay Borisovba3c2b12018-06-26 16:57:36 +0300843 err = -EINVAL;
844 btrfs_print_v0_err(rc->extent_root->fs_info);
845 btrfs_handle_fs_error(rc->extent_root->fs_info, err,
846 NULL);
847 goto out;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400848 } else if (key.type != BTRFS_TREE_BLOCK_REF_KEY) {
849 goto next;
850 }
851
Qu Wenruofa6ac712018-09-25 14:37:46 +0800852 /*
853 * key.type == BTRFS_TREE_BLOCK_REF_KEY, inline ref offset
854 * means the root objectid. We need to search the tree to get
855 * its parent bytenr.
856 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400857 root = read_fs_root(rc->extent_root->fs_info, key.offset);
858 if (IS_ERR(root)) {
859 err = PTR_ERR(root);
860 goto out;
861 }
862
Miao Xie27cdeb72014-04-02 19:51:05 +0800863 if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state))
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400864 cur->cowonly = 1;
865
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400866 if (btrfs_root_level(&root->root_item) == cur->level) {
867 /* tree root */
Josef Bacik75bfb9af2014-09-19 10:40:00 -0400868 ASSERT(btrfs_root_bytenr(&root->root_item) ==
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400869 cur->bytenr);
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400870 if (should_ignore_root(root))
871 list_add(&cur->list, &useless);
872 else
873 cur->root = root;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400874 break;
875 }
876
877 level = cur->level + 1;
878
Qu Wenruofa6ac712018-09-25 14:37:46 +0800879 /* Search the tree to find parent blocks referring the block. */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400880 path2->search_commit_root = 1;
881 path2->skip_locking = 1;
882 path2->lowest_level = level;
883 ret = btrfs_search_slot(NULL, root, node_key, path2, 0, 0);
884 path2->lowest_level = 0;
885 if (ret < 0) {
886 err = ret;
887 goto out;
888 }
Yan Zheng33c66f42009-07-22 09:59:00 -0400889 if (ret > 0 && path2->slots[level] > 0)
890 path2->slots[level]--;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400891
892 eb = path2->nodes[level];
Liu Bo3561b9d2016-09-14 08:51:46 -0700893 if (btrfs_node_blockptr(eb, path2->slots[level]) !=
894 cur->bytenr) {
895 btrfs_err(root->fs_info,
896 "couldn't find block (%llu) (level %d) in tree (%llu) with key (%llu %u %llu)",
Misono Tomohiro4fd786e2018-08-06 14:25:24 +0900897 cur->bytenr, level - 1,
898 root->root_key.objectid,
Liu Bo3561b9d2016-09-14 08:51:46 -0700899 node_key->objectid, node_key->type,
900 node_key->offset);
901 err = -ENOENT;
902 goto out;
903 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400904 lower = cur;
Josef Bacikb6c60c82013-07-30 16:30:30 -0400905 need_check = true;
Qu Wenruofa6ac712018-09-25 14:37:46 +0800906
907 /* Add all nodes and edges in the path */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400908 for (; level < BTRFS_MAX_LEVEL; level++) {
909 if (!path2->nodes[level]) {
Josef Bacik75bfb9af2014-09-19 10:40:00 -0400910 ASSERT(btrfs_root_bytenr(&root->root_item) ==
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400911 lower->bytenr);
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400912 if (should_ignore_root(root))
913 list_add(&lower->list, &useless);
914 else
915 lower->root = root;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400916 break;
917 }
918
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400919 edge = alloc_backref_edge(cache);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400920 if (!edge) {
921 err = -ENOMEM;
922 goto out;
923 }
924
925 eb = path2->nodes[level];
926 rb_node = tree_search(&cache->rb_root, eb->start);
927 if (!rb_node) {
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400928 upper = alloc_backref_node(cache);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400929 if (!upper) {
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400930 free_backref_edge(cache, edge);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400931 err = -ENOMEM;
932 goto out;
933 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400934 upper->bytenr = eb->start;
935 upper->owner = btrfs_header_owner(eb);
936 upper->level = lower->level + 1;
Miao Xie27cdeb72014-04-02 19:51:05 +0800937 if (!test_bit(BTRFS_ROOT_REF_COWS,
938 &root->state))
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400939 upper->cowonly = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400940
941 /*
942 * if we know the block isn't shared
943 * we can void checking its backrefs.
944 */
945 if (btrfs_block_can_be_shared(root, eb))
946 upper->checked = 0;
947 else
948 upper->checked = 1;
949
950 /*
951 * add the block to pending list if we
Josef Bacikb6c60c82013-07-30 16:30:30 -0400952 * need check its backrefs, we only do this once
953 * while walking up a tree as we will catch
954 * anything else later on.
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400955 */
Josef Bacikb6c60c82013-07-30 16:30:30 -0400956 if (!upper->checked && need_check) {
957 need_check = false;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400958 list_add_tail(&edge->list[UPPER],
959 &list);
Josef Bacikbbe90512014-09-19 15:43:34 -0400960 } else {
961 if (upper->checked)
962 need_check = true;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400963 INIT_LIST_HEAD(&edge->list[UPPER]);
Josef Bacikbbe90512014-09-19 15:43:34 -0400964 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400965 } else {
966 upper = rb_entry(rb_node, struct backref_node,
967 rb_node);
Josef Bacik75bfb9af2014-09-19 10:40:00 -0400968 ASSERT(upper->checked);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400969 INIT_LIST_HEAD(&edge->list[UPPER]);
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400970 if (!upper->owner)
971 upper->owner = btrfs_header_owner(eb);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400972 }
973 list_add_tail(&edge->list[LOWER], &lower->upper);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400974 edge->node[LOWER] = lower;
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400975 edge->node[UPPER] = upper;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400976
977 if (rb_node)
978 break;
979 lower = upper;
980 upper = NULL;
981 }
David Sterbab3b4aa72011-04-21 01:20:15 +0200982 btrfs_release_path(path2);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400983next:
984 if (ptr < end) {
985 ptr += btrfs_extent_inline_ref_size(key.type);
986 if (ptr >= end) {
987 WARN_ON(ptr > end);
988 ptr = 0;
989 end = 0;
990 }
991 }
992 if (ptr >= end)
993 path1->slots[0]++;
994 }
David Sterbab3b4aa72011-04-21 01:20:15 +0200995 btrfs_release_path(path1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400996
997 cur->checked = 1;
998 WARN_ON(exist);
999
1000 /* the pending list isn't empty, take the first block to process */
1001 if (!list_empty(&list)) {
1002 edge = list_entry(list.next, struct backref_edge, list[UPPER]);
1003 list_del_init(&edge->list[UPPER]);
1004 cur = edge->node[UPPER];
1005 goto again;
1006 }
1007
1008 /*
1009 * everything goes well, connect backref nodes and insert backref nodes
1010 * into the cache.
1011 */
Josef Bacik75bfb9af2014-09-19 10:40:00 -04001012 ASSERT(node->checked);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001013 cowonly = node->cowonly;
1014 if (!cowonly) {
1015 rb_node = tree_insert(&cache->rb_root, node->bytenr,
1016 &node->rb_node);
Jeff Mahoney43c04fb2011-10-03 23:22:33 -04001017 if (rb_node)
1018 backref_tree_panic(rb_node, -EEXIST, node->bytenr);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001019 list_add_tail(&node->lower, &cache->leaves);
1020 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001021
1022 list_for_each_entry(edge, &node->upper, list[LOWER])
1023 list_add_tail(&edge->list[UPPER], &list);
1024
1025 while (!list_empty(&list)) {
1026 edge = list_entry(list.next, struct backref_edge, list[UPPER]);
1027 list_del_init(&edge->list[UPPER]);
1028 upper = edge->node[UPPER];
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001029 if (upper->detached) {
1030 list_del(&edge->list[LOWER]);
1031 lower = edge->node[LOWER];
1032 free_backref_edge(cache, edge);
1033 if (list_empty(&lower->upper))
1034 list_add(&lower->list, &useless);
1035 continue;
1036 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001037
1038 if (!RB_EMPTY_NODE(&upper->rb_node)) {
1039 if (upper->lowest) {
1040 list_del_init(&upper->lower);
1041 upper->lowest = 0;
1042 }
1043
1044 list_add_tail(&edge->list[UPPER], &upper->lower);
1045 continue;
1046 }
1047
Josef Bacik75bfb9af2014-09-19 10:40:00 -04001048 if (!upper->checked) {
1049 /*
1050 * Still want to blow up for developers since this is a
1051 * logic bug.
1052 */
1053 ASSERT(0);
1054 err = -EINVAL;
1055 goto out;
1056 }
1057 if (cowonly != upper->cowonly) {
1058 ASSERT(0);
1059 err = -EINVAL;
1060 goto out;
1061 }
1062
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001063 if (!cowonly) {
1064 rb_node = tree_insert(&cache->rb_root, upper->bytenr,
1065 &upper->rb_node);
Jeff Mahoney43c04fb2011-10-03 23:22:33 -04001066 if (rb_node)
1067 backref_tree_panic(rb_node, -EEXIST,
1068 upper->bytenr);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001069 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001070
1071 list_add_tail(&edge->list[UPPER], &upper->lower);
1072
1073 list_for_each_entry(edge, &upper->upper, list[LOWER])
1074 list_add_tail(&edge->list[UPPER], &list);
1075 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001076 /*
1077 * process useless backref nodes. backref nodes for tree leaves
1078 * are deleted from the cache. backref nodes for upper level
1079 * tree blocks are left in the cache to avoid unnecessary backref
1080 * lookup.
1081 */
1082 while (!list_empty(&useless)) {
1083 upper = list_entry(useless.next, struct backref_node, list);
1084 list_del_init(&upper->list);
Josef Bacik75bfb9af2014-09-19 10:40:00 -04001085 ASSERT(list_empty(&upper->upper));
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001086 if (upper == node)
1087 node = NULL;
1088 if (upper->lowest) {
1089 list_del_init(&upper->lower);
1090 upper->lowest = 0;
1091 }
1092 while (!list_empty(&upper->lower)) {
1093 edge = list_entry(upper->lower.next,
1094 struct backref_edge, list[UPPER]);
1095 list_del(&edge->list[UPPER]);
1096 list_del(&edge->list[LOWER]);
1097 lower = edge->node[LOWER];
1098 free_backref_edge(cache, edge);
1099
1100 if (list_empty(&lower->upper))
1101 list_add(&lower->list, &useless);
1102 }
1103 __mark_block_processed(rc, upper);
1104 if (upper->level > 0) {
1105 list_add(&upper->list, &cache->detached);
1106 upper->detached = 1;
1107 } else {
1108 rb_erase(&upper->rb_node, &cache->rb_root);
1109 free_backref_node(cache, upper);
1110 }
1111 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001112out:
1113 btrfs_free_path(path1);
1114 btrfs_free_path(path2);
1115 if (err) {
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001116 while (!list_empty(&useless)) {
1117 lower = list_entry(useless.next,
Josef Bacik75bfb9af2014-09-19 10:40:00 -04001118 struct backref_node, list);
1119 list_del_init(&lower->list);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001120 }
Josef Bacik75bfb9af2014-09-19 10:40:00 -04001121 while (!list_empty(&list)) {
1122 edge = list_first_entry(&list, struct backref_edge,
1123 list[UPPER]);
1124 list_del(&edge->list[UPPER]);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001125 list_del(&edge->list[LOWER]);
Josef Bacik75bfb9af2014-09-19 10:40:00 -04001126 lower = edge->node[LOWER];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001127 upper = edge->node[UPPER];
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001128 free_backref_edge(cache, edge);
Josef Bacik75bfb9af2014-09-19 10:40:00 -04001129
1130 /*
1131 * Lower is no longer linked to any upper backref nodes
1132 * and isn't in the cache, we can free it ourselves.
1133 */
1134 if (list_empty(&lower->upper) &&
1135 RB_EMPTY_NODE(&lower->rb_node))
1136 list_add(&lower->list, &useless);
1137
1138 if (!RB_EMPTY_NODE(&upper->rb_node))
1139 continue;
1140
Nicholas D Steeves01327612016-05-19 21:18:45 -04001141 /* Add this guy's upper edges to the list to process */
Josef Bacik75bfb9af2014-09-19 10:40:00 -04001142 list_for_each_entry(edge, &upper->upper, list[LOWER])
1143 list_add_tail(&edge->list[UPPER], &list);
1144 if (list_empty(&upper->upper))
1145 list_add(&upper->list, &useless);
1146 }
1147
1148 while (!list_empty(&useless)) {
1149 lower = list_entry(useless.next,
1150 struct backref_node, list);
1151 list_del_init(&lower->list);
Liu Bo0fd8c3d2016-07-12 10:29:37 -07001152 if (lower == node)
1153 node = NULL;
Josef Bacik75bfb9af2014-09-19 10:40:00 -04001154 free_backref_node(cache, lower);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001155 }
Liu Bo0fd8c3d2016-07-12 10:29:37 -07001156
1157 free_backref_node(cache, node);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001158 return ERR_PTR(err);
1159 }
Josef Bacik75bfb9af2014-09-19 10:40:00 -04001160 ASSERT(!node || !node->detached);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001161 return node;
1162}
1163
1164/*
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001165 * helper to add backref node for the newly created snapshot.
1166 * the backref node is created by cloning backref node that
1167 * corresponds to root of source tree
1168 */
1169static int clone_backref_node(struct btrfs_trans_handle *trans,
1170 struct reloc_control *rc,
1171 struct btrfs_root *src,
1172 struct btrfs_root *dest)
1173{
1174 struct btrfs_root *reloc_root = src->reloc_root;
1175 struct backref_cache *cache = &rc->backref_cache;
1176 struct backref_node *node = NULL;
1177 struct backref_node *new_node;
1178 struct backref_edge *edge;
1179 struct backref_edge *new_edge;
1180 struct rb_node *rb_node;
1181
1182 if (cache->last_trans > 0)
1183 update_backref_cache(trans, cache);
1184
1185 rb_node = tree_search(&cache->rb_root, src->commit_root->start);
1186 if (rb_node) {
1187 node = rb_entry(rb_node, struct backref_node, rb_node);
1188 if (node->detached)
1189 node = NULL;
1190 else
1191 BUG_ON(node->new_bytenr != reloc_root->node->start);
1192 }
1193
1194 if (!node) {
1195 rb_node = tree_search(&cache->rb_root,
1196 reloc_root->commit_root->start);
1197 if (rb_node) {
1198 node = rb_entry(rb_node, struct backref_node,
1199 rb_node);
1200 BUG_ON(node->detached);
1201 }
1202 }
1203
1204 if (!node)
1205 return 0;
1206
1207 new_node = alloc_backref_node(cache);
1208 if (!new_node)
1209 return -ENOMEM;
1210
1211 new_node->bytenr = dest->node->start;
1212 new_node->level = node->level;
1213 new_node->lowest = node->lowest;
Yan, Zheng6848ad62011-02-14 16:00:03 -05001214 new_node->checked = 1;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001215 new_node->root = dest;
1216
1217 if (!node->lowest) {
1218 list_for_each_entry(edge, &node->lower, list[UPPER]) {
1219 new_edge = alloc_backref_edge(cache);
1220 if (!new_edge)
1221 goto fail;
1222
1223 new_edge->node[UPPER] = new_node;
1224 new_edge->node[LOWER] = edge->node[LOWER];
1225 list_add_tail(&new_edge->list[UPPER],
1226 &new_node->lower);
1227 }
Miao Xie76b9e232011-11-10 20:45:05 -05001228 } else {
1229 list_add_tail(&new_node->lower, &cache->leaves);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001230 }
1231
1232 rb_node = tree_insert(&cache->rb_root, new_node->bytenr,
1233 &new_node->rb_node);
Jeff Mahoney43c04fb2011-10-03 23:22:33 -04001234 if (rb_node)
1235 backref_tree_panic(rb_node, -EEXIST, new_node->bytenr);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001236
1237 if (!new_node->lowest) {
1238 list_for_each_entry(new_edge, &new_node->lower, list[UPPER]) {
1239 list_add_tail(&new_edge->list[LOWER],
1240 &new_edge->node[LOWER]->upper);
1241 }
1242 }
1243 return 0;
1244fail:
1245 while (!list_empty(&new_node->lower)) {
1246 new_edge = list_entry(new_node->lower.next,
1247 struct backref_edge, list[UPPER]);
1248 list_del(&new_edge->list[UPPER]);
1249 free_backref_edge(cache, new_edge);
1250 }
1251 free_backref_node(cache, new_node);
1252 return -ENOMEM;
1253}
1254
1255/*
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001256 * helper to add 'address of tree root -> reloc tree' mapping
1257 */
Jeff Mahoneyffd7b332011-10-03 23:23:15 -04001258static int __must_check __add_reloc_root(struct btrfs_root *root)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001259{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001260 struct btrfs_fs_info *fs_info = root->fs_info;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001261 struct rb_node *rb_node;
1262 struct mapping_node *node;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001263 struct reloc_control *rc = fs_info->reloc_ctl;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001264
1265 node = kmalloc(sizeof(*node), GFP_NOFS);
Jeff Mahoneyffd7b332011-10-03 23:23:15 -04001266 if (!node)
1267 return -ENOMEM;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001268
1269 node->bytenr = root->node->start;
1270 node->data = root;
1271
1272 spin_lock(&rc->reloc_root_tree.lock);
1273 rb_node = tree_insert(&rc->reloc_root_tree.rb_root,
1274 node->bytenr, &node->rb_node);
1275 spin_unlock(&rc->reloc_root_tree.lock);
Jeff Mahoneyffd7b332011-10-03 23:23:15 -04001276 if (rb_node) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001277 btrfs_panic(fs_info, -EEXIST,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04001278 "Duplicate root found for start=%llu while inserting into relocation tree",
1279 node->bytenr);
Jeff Mahoneyffd7b332011-10-03 23:23:15 -04001280 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001281
1282 list_add_tail(&root->root_list, &rc->reloc_roots);
1283 return 0;
1284}
1285
1286/*
Wang Shilongc974c462013-12-11 19:29:51 +08001287 * helper to delete the 'address of tree root -> reloc tree'
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001288 * mapping
1289 */
Wang Shilongc974c462013-12-11 19:29:51 +08001290static void __del_reloc_root(struct btrfs_root *root)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001291{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001292 struct btrfs_fs_info *fs_info = root->fs_info;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001293 struct rb_node *rb_node;
1294 struct mapping_node *node = NULL;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001295 struct reloc_control *rc = fs_info->reloc_ctl;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001296
Qu Wenruo65c6e822018-08-21 09:42:03 +08001297 if (rc && root->node) {
Qu Wenruo389305b2018-07-03 17:10:07 +08001298 spin_lock(&rc->reloc_root_tree.lock);
1299 rb_node = tree_search(&rc->reloc_root_tree.rb_root,
1300 root->node->start);
1301 if (rb_node) {
1302 node = rb_entry(rb_node, struct mapping_node, rb_node);
1303 rb_erase(&node->rb_node, &rc->reloc_root_tree.rb_root);
1304 }
1305 spin_unlock(&rc->reloc_root_tree.lock);
1306 if (!node)
1307 return;
1308 BUG_ON((struct btrfs_root *)node->data != root);
Wang Shilongc974c462013-12-11 19:29:51 +08001309 }
Wang Shilongc974c462013-12-11 19:29:51 +08001310
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001311 spin_lock(&fs_info->trans_lock);
Wang Shilongc974c462013-12-11 19:29:51 +08001312 list_del_init(&root->root_list);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001313 spin_unlock(&fs_info->trans_lock);
Wang Shilongc974c462013-12-11 19:29:51 +08001314 kfree(node);
1315}
1316
1317/*
1318 * helper to update the 'address of tree root -> reloc tree'
1319 * mapping
1320 */
1321static int __update_reloc_root(struct btrfs_root *root, u64 new_bytenr)
1322{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001323 struct btrfs_fs_info *fs_info = root->fs_info;
Wang Shilongc974c462013-12-11 19:29:51 +08001324 struct rb_node *rb_node;
1325 struct mapping_node *node = NULL;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001326 struct reloc_control *rc = fs_info->reloc_ctl;
Wang Shilongc974c462013-12-11 19:29:51 +08001327
1328 spin_lock(&rc->reloc_root_tree.lock);
1329 rb_node = tree_search(&rc->reloc_root_tree.rb_root,
1330 root->node->start);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001331 if (rb_node) {
1332 node = rb_entry(rb_node, struct mapping_node, rb_node);
1333 rb_erase(&node->rb_node, &rc->reloc_root_tree.rb_root);
1334 }
1335 spin_unlock(&rc->reloc_root_tree.lock);
1336
Liu Bo8f71f3e2013-03-04 16:25:36 +00001337 if (!node)
1338 return 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001339 BUG_ON((struct btrfs_root *)node->data != root);
1340
Wang Shilongc974c462013-12-11 19:29:51 +08001341 spin_lock(&rc->reloc_root_tree.lock);
1342 node->bytenr = new_bytenr;
1343 rb_node = tree_insert(&rc->reloc_root_tree.rb_root,
1344 node->bytenr, &node->rb_node);
1345 spin_unlock(&rc->reloc_root_tree.lock);
1346 if (rb_node)
1347 backref_tree_panic(rb_node, -EEXIST, node->bytenr);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001348 return 0;
1349}
1350
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001351static struct btrfs_root *create_reloc_root(struct btrfs_trans_handle *trans,
1352 struct btrfs_root *root, u64 objectid)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001353{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001354 struct btrfs_fs_info *fs_info = root->fs_info;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001355 struct btrfs_root *reloc_root;
1356 struct extent_buffer *eb;
1357 struct btrfs_root_item *root_item;
1358 struct btrfs_key root_key;
1359 int ret;
1360
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001361 root_item = kmalloc(sizeof(*root_item), GFP_NOFS);
1362 BUG_ON(!root_item);
1363
1364 root_key.objectid = BTRFS_TREE_RELOC_OBJECTID;
1365 root_key.type = BTRFS_ROOT_ITEM_KEY;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001366 root_key.offset = objectid;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001367
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001368 if (root->root_key.objectid == objectid) {
Filipe Manana054570a2016-11-01 11:23:31 +00001369 u64 commit_root_gen;
1370
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001371 /* called by btrfs_init_reloc_root */
1372 ret = btrfs_copy_root(trans, root, root->commit_root, &eb,
1373 BTRFS_TREE_RELOC_OBJECTID);
1374 BUG_ON(ret);
Filipe Manana054570a2016-11-01 11:23:31 +00001375 /*
1376 * Set the last_snapshot field to the generation of the commit
1377 * root - like this ctree.c:btrfs_block_can_be_shared() behaves
1378 * correctly (returns true) when the relocation root is created
1379 * either inside the critical section of a transaction commit
1380 * (through transaction.c:qgroup_account_snapshot()) and when
1381 * it's created before the transaction commit is started.
1382 */
1383 commit_root_gen = btrfs_header_generation(root->commit_root);
1384 btrfs_set_root_last_snapshot(&root->root_item, commit_root_gen);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001385 } else {
1386 /*
1387 * called by btrfs_reloc_post_snapshot_hook.
1388 * the source tree is a reloc tree, all tree blocks
1389 * modified after it was created have RELOC flag
1390 * set in their headers. so it's OK to not update
1391 * the 'last_snapshot'.
1392 */
1393 ret = btrfs_copy_root(trans, root, root->node, &eb,
1394 BTRFS_TREE_RELOC_OBJECTID);
1395 BUG_ON(ret);
1396 }
1397
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001398 memcpy(root_item, &root->root_item, sizeof(*root_item));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001399 btrfs_set_root_bytenr(root_item, eb->start);
1400 btrfs_set_root_level(root_item, btrfs_header_level(eb));
1401 btrfs_set_root_generation(root_item, trans->transid);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001402
1403 if (root->root_key.objectid == objectid) {
1404 btrfs_set_root_refs(root_item, 0);
1405 memset(&root_item->drop_progress, 0,
1406 sizeof(struct btrfs_disk_key));
1407 root_item->drop_level = 0;
1408 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001409
1410 btrfs_tree_unlock(eb);
1411 free_extent_buffer(eb);
1412
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001413 ret = btrfs_insert_root(trans, fs_info->tree_root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001414 &root_key, root_item);
1415 BUG_ON(ret);
1416 kfree(root_item);
1417
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001418 reloc_root = btrfs_read_fs_root(fs_info->tree_root, &root_key);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001419 BUG_ON(IS_ERR(reloc_root));
1420 reloc_root->last_trans = trans->transid;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001421 return reloc_root;
1422}
1423
1424/*
1425 * create reloc tree for a given fs tree. reloc tree is just a
1426 * snapshot of the fs tree with special root objectid.
1427 */
1428int btrfs_init_reloc_root(struct btrfs_trans_handle *trans,
1429 struct btrfs_root *root)
1430{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001431 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001432 struct btrfs_root *reloc_root;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001433 struct reloc_control *rc = fs_info->reloc_ctl;
Miao Xie20dd2cb2013-09-25 21:47:45 +08001434 struct btrfs_block_rsv *rsv;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001435 int clear_rsv = 0;
Jeff Mahoneyffd7b332011-10-03 23:23:15 -04001436 int ret;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001437
Qu Wenruo1fac4a52019-09-23 14:56:14 +08001438 /*
1439 * The subvolume has reloc tree but the swap is finished, no need to
1440 * create/update the dead reloc tree
1441 */
1442 if (test_bit(BTRFS_ROOT_DEAD_RELOC_TREE, &root->state))
1443 return 0;
1444
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001445 if (root->reloc_root) {
1446 reloc_root = root->reloc_root;
1447 reloc_root->last_trans = trans->transid;
1448 return 0;
1449 }
1450
1451 if (!rc || !rc->create_reloc_tree ||
1452 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
1453 return 0;
1454
Miao Xie20dd2cb2013-09-25 21:47:45 +08001455 if (!trans->reloc_reserved) {
1456 rsv = trans->block_rsv;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001457 trans->block_rsv = rc->block_rsv;
1458 clear_rsv = 1;
1459 }
1460 reloc_root = create_reloc_root(trans, root, root->root_key.objectid);
1461 if (clear_rsv)
Miao Xie20dd2cb2013-09-25 21:47:45 +08001462 trans->block_rsv = rsv;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001463
Jeff Mahoneyffd7b332011-10-03 23:23:15 -04001464 ret = __add_reloc_root(reloc_root);
1465 BUG_ON(ret < 0);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001466 root->reloc_root = reloc_root;
1467 return 0;
1468}
1469
1470/*
1471 * update root item of reloc tree
1472 */
1473int btrfs_update_reloc_root(struct btrfs_trans_handle *trans,
1474 struct btrfs_root *root)
1475{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001476 struct btrfs_fs_info *fs_info = root->fs_info;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001477 struct btrfs_root *reloc_root;
1478 struct btrfs_root_item *root_item;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001479 int ret;
1480
Qu Wenruod2311e62019-01-23 15:15:14 +08001481 if (test_bit(BTRFS_ROOT_DEAD_RELOC_TREE, &root->state) ||
1482 !root->reloc_root)
Chris Mason75857172011-06-13 20:00:16 -04001483 goto out;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001484
1485 reloc_root = root->reloc_root;
1486 root_item = &reloc_root->root_item;
1487
Qu Wenruod2311e62019-01-23 15:15:14 +08001488 /* root->reloc_root will stay until current relocation finished */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001489 if (fs_info->reloc_ctl->merge_reloc_tree &&
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001490 btrfs_root_refs(root_item) == 0) {
Qu Wenruod2311e62019-01-23 15:15:14 +08001491 set_bit(BTRFS_ROOT_DEAD_RELOC_TREE, &root->state);
Wang Shilongc974c462013-12-11 19:29:51 +08001492 __del_reloc_root(reloc_root);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001493 }
1494
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001495 if (reloc_root->commit_root != reloc_root->node) {
1496 btrfs_set_root_node(root_item, reloc_root->node);
1497 free_extent_buffer(reloc_root->commit_root);
1498 reloc_root->commit_root = btrfs_root_node(reloc_root);
1499 }
1500
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001501 ret = btrfs_update_root(trans, fs_info->tree_root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001502 &reloc_root->root_key, root_item);
1503 BUG_ON(ret);
Chris Mason75857172011-06-13 20:00:16 -04001504
1505out:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001506 return 0;
1507}
1508
1509/*
1510 * helper to find first cached inode with inode number >= objectid
1511 * in a subvolume
1512 */
1513static struct inode *find_next_inode(struct btrfs_root *root, u64 objectid)
1514{
1515 struct rb_node *node;
1516 struct rb_node *prev;
1517 struct btrfs_inode *entry;
1518 struct inode *inode;
1519
1520 spin_lock(&root->inode_lock);
1521again:
1522 node = root->inode_tree.rb_node;
1523 prev = NULL;
1524 while (node) {
1525 prev = node;
1526 entry = rb_entry(node, struct btrfs_inode, rb_node);
1527
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02001528 if (objectid < btrfs_ino(entry))
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001529 node = node->rb_left;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02001530 else if (objectid > btrfs_ino(entry))
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001531 node = node->rb_right;
1532 else
1533 break;
1534 }
1535 if (!node) {
1536 while (prev) {
1537 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02001538 if (objectid <= btrfs_ino(entry)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001539 node = prev;
1540 break;
1541 }
1542 prev = rb_next(prev);
1543 }
1544 }
1545 while (node) {
1546 entry = rb_entry(node, struct btrfs_inode, rb_node);
1547 inode = igrab(&entry->vfs_inode);
1548 if (inode) {
1549 spin_unlock(&root->inode_lock);
1550 return inode;
1551 }
1552
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02001553 objectid = btrfs_ino(entry) + 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001554 if (cond_resched_lock(&root->inode_lock))
1555 goto again;
1556
1557 node = rb_next(node);
1558 }
1559 spin_unlock(&root->inode_lock);
1560 return NULL;
1561}
1562
1563static int in_block_group(u64 bytenr,
1564 struct btrfs_block_group_cache *block_group)
1565{
1566 if (bytenr >= block_group->key.objectid &&
1567 bytenr < block_group->key.objectid + block_group->key.offset)
1568 return 1;
1569 return 0;
1570}
1571
1572/*
1573 * get new location of data
1574 */
1575static int get_new_location(struct inode *reloc_inode, u64 *new_bytenr,
1576 u64 bytenr, u64 num_bytes)
1577{
1578 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
1579 struct btrfs_path *path;
1580 struct btrfs_file_extent_item *fi;
1581 struct extent_buffer *leaf;
1582 int ret;
1583
1584 path = btrfs_alloc_path();
1585 if (!path)
1586 return -ENOMEM;
1587
1588 bytenr -= BTRFS_I(reloc_inode)->index_cnt;
David Sterbaf85b7372017-01-20 14:54:07 +01001589 ret = btrfs_lookup_file_extent(NULL, root, path,
1590 btrfs_ino(BTRFS_I(reloc_inode)), bytenr, 0);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001591 if (ret < 0)
1592 goto out;
1593 if (ret > 0) {
1594 ret = -ENOENT;
1595 goto out;
1596 }
1597
1598 leaf = path->nodes[0];
1599 fi = btrfs_item_ptr(leaf, path->slots[0],
1600 struct btrfs_file_extent_item);
1601
1602 BUG_ON(btrfs_file_extent_offset(leaf, fi) ||
1603 btrfs_file_extent_compression(leaf, fi) ||
1604 btrfs_file_extent_encryption(leaf, fi) ||
1605 btrfs_file_extent_other_encoding(leaf, fi));
1606
1607 if (num_bytes != btrfs_file_extent_disk_num_bytes(leaf, fi)) {
Josef Bacik83d4cfd2013-08-30 15:09:51 -04001608 ret = -EINVAL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001609 goto out;
1610 }
1611
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001612 *new_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001613 ret = 0;
1614out:
1615 btrfs_free_path(path);
1616 return ret;
1617}
1618
1619/*
1620 * update file extent items in the tree leaf to point to
1621 * the new locations.
1622 */
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001623static noinline_for_stack
1624int replace_file_extents(struct btrfs_trans_handle *trans,
1625 struct reloc_control *rc,
1626 struct btrfs_root *root,
1627 struct extent_buffer *leaf)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001628{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001629 struct btrfs_fs_info *fs_info = root->fs_info;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001630 struct btrfs_key key;
1631 struct btrfs_file_extent_item *fi;
1632 struct inode *inode = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001633 u64 parent;
1634 u64 bytenr;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001635 u64 new_bytenr = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001636 u64 num_bytes;
1637 u64 end;
1638 u32 nritems;
1639 u32 i;
Josef Bacik83d4cfd2013-08-30 15:09:51 -04001640 int ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001641 int first = 1;
1642 int dirty = 0;
1643
1644 if (rc->stage != UPDATE_DATA_PTRS)
1645 return 0;
1646
1647 /* reloc trees always use full backref */
1648 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
1649 parent = leaf->start;
1650 else
1651 parent = 0;
1652
1653 nritems = btrfs_header_nritems(leaf);
1654 for (i = 0; i < nritems; i++) {
Qu Wenruo82fa1132019-04-04 14:45:35 +08001655 struct btrfs_ref ref = { 0 };
1656
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001657 cond_resched();
1658 btrfs_item_key_to_cpu(leaf, &key, i);
1659 if (key.type != BTRFS_EXTENT_DATA_KEY)
1660 continue;
1661 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
1662 if (btrfs_file_extent_type(leaf, fi) ==
1663 BTRFS_FILE_EXTENT_INLINE)
1664 continue;
1665 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
1666 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
1667 if (bytenr == 0)
1668 continue;
1669 if (!in_block_group(bytenr, rc->block_group))
1670 continue;
1671
1672 /*
1673 * if we are modifying block in fs tree, wait for readpage
1674 * to complete and drop the extent cache
1675 */
1676 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001677 if (first) {
1678 inode = find_next_inode(root, key.objectid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001679 first = 0;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02001680 } else if (inode && btrfs_ino(BTRFS_I(inode)) < key.objectid) {
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001681 btrfs_add_delayed_iput(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001682 inode = find_next_inode(root, key.objectid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001683 }
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02001684 if (inode && btrfs_ino(BTRFS_I(inode)) == key.objectid) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001685 end = key.offset +
1686 btrfs_file_extent_num_bytes(leaf, fi);
1687 WARN_ON(!IS_ALIGNED(key.offset,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001688 fs_info->sectorsize));
1689 WARN_ON(!IS_ALIGNED(end, fs_info->sectorsize));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001690 end--;
1691 ret = try_lock_extent(&BTRFS_I(inode)->io_tree,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001692 key.offset, end);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001693 if (!ret)
1694 continue;
1695
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02001696 btrfs_drop_extent_cache(BTRFS_I(inode),
1697 key.offset, end, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001698 unlock_extent(&BTRFS_I(inode)->io_tree,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001699 key.offset, end);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001700 }
1701 }
1702
1703 ret = get_new_location(rc->data_inode, &new_bytenr,
1704 bytenr, num_bytes);
Josef Bacik83d4cfd2013-08-30 15:09:51 -04001705 if (ret) {
1706 /*
1707 * Don't have to abort since we've not changed anything
1708 * in the file extent yet.
1709 */
1710 break;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001711 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001712
1713 btrfs_set_file_extent_disk_bytenr(leaf, fi, new_bytenr);
1714 dirty = 1;
1715
1716 key.offset -= btrfs_file_extent_offset(leaf, fi);
Qu Wenruo82fa1132019-04-04 14:45:35 +08001717 btrfs_init_generic_ref(&ref, BTRFS_ADD_DELAYED_REF, new_bytenr,
1718 num_bytes, parent);
1719 ref.real_root = root->root_key.objectid;
1720 btrfs_init_data_ref(&ref, btrfs_header_owner(leaf),
1721 key.objectid, key.offset);
1722 ret = btrfs_inc_extent_ref(trans, &ref);
Josef Bacik83d4cfd2013-08-30 15:09:51 -04001723 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04001724 btrfs_abort_transaction(trans, ret);
Josef Bacik83d4cfd2013-08-30 15:09:51 -04001725 break;
1726 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001727
Qu Wenruoffd4bb22019-04-04 14:45:36 +08001728 btrfs_init_generic_ref(&ref, BTRFS_DROP_DELAYED_REF, bytenr,
1729 num_bytes, parent);
1730 ref.real_root = root->root_key.objectid;
1731 btrfs_init_data_ref(&ref, btrfs_header_owner(leaf),
1732 key.objectid, key.offset);
1733 ret = btrfs_free_extent(trans, &ref);
Josef Bacik83d4cfd2013-08-30 15:09:51 -04001734 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04001735 btrfs_abort_transaction(trans, ret);
Josef Bacik83d4cfd2013-08-30 15:09:51 -04001736 break;
1737 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001738 }
1739 if (dirty)
1740 btrfs_mark_buffer_dirty(leaf);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001741 if (inode)
1742 btrfs_add_delayed_iput(inode);
Josef Bacik83d4cfd2013-08-30 15:09:51 -04001743 return ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001744}
1745
1746static noinline_for_stack
1747int memcmp_node_keys(struct extent_buffer *eb, int slot,
1748 struct btrfs_path *path, int level)
1749{
1750 struct btrfs_disk_key key1;
1751 struct btrfs_disk_key key2;
1752 btrfs_node_key(eb, &key1, slot);
1753 btrfs_node_key(path->nodes[level], &key2, path->slots[level]);
1754 return memcmp(&key1, &key2, sizeof(key1));
1755}
1756
1757/*
1758 * try to replace tree blocks in fs tree with the new blocks
1759 * in reloc tree. tree blocks haven't been modified since the
1760 * reloc tree was create can be replaced.
1761 *
1762 * if a block was replaced, level of the block + 1 is returned.
1763 * if no block got replaced, 0 is returned. if there are other
1764 * errors, a negative error number is returned.
1765 */
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001766static noinline_for_stack
Qu Wenruo3d0174f2018-09-27 14:42:35 +08001767int replace_path(struct btrfs_trans_handle *trans, struct reloc_control *rc,
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001768 struct btrfs_root *dest, struct btrfs_root *src,
1769 struct btrfs_path *path, struct btrfs_key *next_key,
1770 int lowest_level, int max_level)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001771{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001772 struct btrfs_fs_info *fs_info = dest->fs_info;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001773 struct extent_buffer *eb;
1774 struct extent_buffer *parent;
Qu Wenruo82fa1132019-04-04 14:45:35 +08001775 struct btrfs_ref ref = { 0 };
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001776 struct btrfs_key key;
1777 u64 old_bytenr;
1778 u64 new_bytenr;
1779 u64 old_ptr_gen;
1780 u64 new_ptr_gen;
1781 u64 last_snapshot;
1782 u32 blocksize;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001783 int cow = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001784 int level;
1785 int ret;
1786 int slot;
1787
1788 BUG_ON(src->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
1789 BUG_ON(dest->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001790
1791 last_snapshot = btrfs_root_last_snapshot(&src->root_item);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001792again:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001793 slot = path->slots[lowest_level];
1794 btrfs_node_key_to_cpu(path->nodes[lowest_level], &key, slot);
1795
1796 eb = btrfs_lock_root_node(dest);
David Sterba8bead252018-04-04 02:03:48 +02001797 btrfs_set_lock_blocking_write(eb);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001798 level = btrfs_header_level(eb);
1799
1800 if (level < lowest_level) {
1801 btrfs_tree_unlock(eb);
1802 free_extent_buffer(eb);
1803 return 0;
1804 }
1805
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001806 if (cow) {
1807 ret = btrfs_cow_block(trans, dest, eb, NULL, 0, &eb);
1808 BUG_ON(ret);
1809 }
David Sterba8bead252018-04-04 02:03:48 +02001810 btrfs_set_lock_blocking_write(eb);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001811
1812 if (next_key) {
1813 next_key->objectid = (u64)-1;
1814 next_key->type = (u8)-1;
1815 next_key->offset = (u64)-1;
1816 }
1817
1818 parent = eb;
1819 while (1) {
Qu Wenruo581c1762018-03-29 09:08:11 +08001820 struct btrfs_key first_key;
1821
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001822 level = btrfs_header_level(parent);
1823 BUG_ON(level < lowest_level);
1824
1825 ret = btrfs_bin_search(parent, &key, level, &slot);
Filipe Mananacbca7d52019-02-18 16:57:26 +00001826 if (ret < 0)
1827 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001828 if (ret && slot > 0)
1829 slot--;
1830
1831 if (next_key && slot + 1 < btrfs_header_nritems(parent))
1832 btrfs_node_key_to_cpu(parent, next_key, slot + 1);
1833
1834 old_bytenr = btrfs_node_blockptr(parent, slot);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001835 blocksize = fs_info->nodesize;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001836 old_ptr_gen = btrfs_node_ptr_generation(parent, slot);
Qu Wenruo17515f12018-04-23 17:32:04 +08001837 btrfs_node_key_to_cpu(parent, &first_key, slot);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001838
1839 if (level <= max_level) {
1840 eb = path->nodes[level];
1841 new_bytenr = btrfs_node_blockptr(eb,
1842 path->slots[level]);
1843 new_ptr_gen = btrfs_node_ptr_generation(eb,
1844 path->slots[level]);
1845 } else {
1846 new_bytenr = 0;
1847 new_ptr_gen = 0;
1848 }
1849
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05301850 if (WARN_ON(new_bytenr > 0 && new_bytenr == old_bytenr)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001851 ret = level;
1852 break;
1853 }
1854
1855 if (new_bytenr == 0 || old_ptr_gen > last_snapshot ||
1856 memcmp_node_keys(parent, slot, path, level)) {
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001857 if (level <= lowest_level) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001858 ret = 0;
1859 break;
1860 }
1861
Qu Wenruo581c1762018-03-29 09:08:11 +08001862 eb = read_tree_block(fs_info, old_bytenr, old_ptr_gen,
1863 level - 1, &first_key);
Liu Bo64c043d2015-05-25 17:30:15 +08001864 if (IS_ERR(eb)) {
1865 ret = PTR_ERR(eb);
Liu Bo264813a2016-03-21 14:59:53 -07001866 break;
Liu Bo64c043d2015-05-25 17:30:15 +08001867 } else if (!extent_buffer_uptodate(eb)) {
1868 ret = -EIO;
Josef Bacik416bc652013-04-23 14:17:42 -04001869 free_extent_buffer(eb);
Stefan Behrens379cde72013-05-08 08:56:09 +00001870 break;
Josef Bacik416bc652013-04-23 14:17:42 -04001871 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001872 btrfs_tree_lock(eb);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001873 if (cow) {
1874 ret = btrfs_cow_block(trans, dest, eb, parent,
1875 slot, &eb);
1876 BUG_ON(ret);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001877 }
David Sterba8bead252018-04-04 02:03:48 +02001878 btrfs_set_lock_blocking_write(eb);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001879
1880 btrfs_tree_unlock(parent);
1881 free_extent_buffer(parent);
1882
1883 parent = eb;
1884 continue;
1885 }
1886
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001887 if (!cow) {
1888 btrfs_tree_unlock(parent);
1889 free_extent_buffer(parent);
1890 cow = 1;
1891 goto again;
1892 }
1893
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001894 btrfs_node_key_to_cpu(path->nodes[level], &key,
1895 path->slots[level]);
David Sterbab3b4aa72011-04-21 01:20:15 +02001896 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001897
1898 path->lowest_level = level;
1899 ret = btrfs_search_slot(trans, src, &key, path, 0, 1);
1900 path->lowest_level = 0;
1901 BUG_ON(ret);
1902
1903 /*
Qu Wenruo824d8df2016-10-18 09:31:29 +08001904 * Info qgroup to trace both subtrees.
1905 *
1906 * We must trace both trees.
1907 * 1) Tree reloc subtree
1908 * If not traced, we will leak data numbers
1909 * 2) Fs subtree
1910 * If not traced, we will double count old data
Qu Wenruof616f5c2019-01-23 15:15:17 +08001911 *
1912 * We don't scan the subtree right now, but only record
1913 * the swapped tree blocks.
1914 * The real subtree rescan is delayed until we have new
1915 * CoW on the subtree root node before transaction commit.
Qu Wenruo824d8df2016-10-18 09:31:29 +08001916 */
Qu Wenruo370a11b2019-01-23 15:15:16 +08001917 ret = btrfs_qgroup_add_swapped_blocks(trans, dest,
1918 rc->block_group, parent, slot,
1919 path->nodes[level], path->slots[level],
1920 last_snapshot);
1921 if (ret < 0)
1922 break;
Qu Wenruo824d8df2016-10-18 09:31:29 +08001923 /*
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001924 * swap blocks in fs tree and reloc tree.
1925 */
1926 btrfs_set_node_blockptr(parent, slot, new_bytenr);
1927 btrfs_set_node_ptr_generation(parent, slot, new_ptr_gen);
1928 btrfs_mark_buffer_dirty(parent);
1929
1930 btrfs_set_node_blockptr(path->nodes[level],
1931 path->slots[level], old_bytenr);
1932 btrfs_set_node_ptr_generation(path->nodes[level],
1933 path->slots[level], old_ptr_gen);
1934 btrfs_mark_buffer_dirty(path->nodes[level]);
1935
Qu Wenruo82fa1132019-04-04 14:45:35 +08001936 btrfs_init_generic_ref(&ref, BTRFS_ADD_DELAYED_REF, old_bytenr,
1937 blocksize, path->nodes[level]->start);
1938 ref.skip_qgroup = true;
1939 btrfs_init_tree_ref(&ref, level - 1, src->root_key.objectid);
1940 ret = btrfs_inc_extent_ref(trans, &ref);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001941 BUG_ON(ret);
Qu Wenruo82fa1132019-04-04 14:45:35 +08001942 btrfs_init_generic_ref(&ref, BTRFS_ADD_DELAYED_REF, new_bytenr,
1943 blocksize, 0);
1944 ref.skip_qgroup = true;
1945 btrfs_init_tree_ref(&ref, level - 1, dest->root_key.objectid);
1946 ret = btrfs_inc_extent_ref(trans, &ref);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001947 BUG_ON(ret);
1948
Qu Wenruoffd4bb22019-04-04 14:45:36 +08001949 btrfs_init_generic_ref(&ref, BTRFS_DROP_DELAYED_REF, new_bytenr,
1950 blocksize, path->nodes[level]->start);
1951 btrfs_init_tree_ref(&ref, level - 1, src->root_key.objectid);
1952 ref.skip_qgroup = true;
1953 ret = btrfs_free_extent(trans, &ref);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001954 BUG_ON(ret);
1955
Qu Wenruoffd4bb22019-04-04 14:45:36 +08001956 btrfs_init_generic_ref(&ref, BTRFS_DROP_DELAYED_REF, old_bytenr,
1957 blocksize, 0);
1958 btrfs_init_tree_ref(&ref, level - 1, dest->root_key.objectid);
1959 ref.skip_qgroup = true;
1960 ret = btrfs_free_extent(trans, &ref);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001961 BUG_ON(ret);
1962
1963 btrfs_unlock_up_safe(path, 0);
1964
1965 ret = level;
1966 break;
1967 }
1968 btrfs_tree_unlock(parent);
1969 free_extent_buffer(parent);
1970 return ret;
1971}
1972
1973/*
1974 * helper to find next relocated block in reloc tree
1975 */
1976static noinline_for_stack
1977int walk_up_reloc_tree(struct btrfs_root *root, struct btrfs_path *path,
1978 int *level)
1979{
1980 struct extent_buffer *eb;
1981 int i;
1982 u64 last_snapshot;
1983 u32 nritems;
1984
1985 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
1986
1987 for (i = 0; i < *level; i++) {
1988 free_extent_buffer(path->nodes[i]);
1989 path->nodes[i] = NULL;
1990 }
1991
1992 for (i = *level; i < BTRFS_MAX_LEVEL && path->nodes[i]; i++) {
1993 eb = path->nodes[i];
1994 nritems = btrfs_header_nritems(eb);
1995 while (path->slots[i] + 1 < nritems) {
1996 path->slots[i]++;
1997 if (btrfs_node_ptr_generation(eb, path->slots[i]) <=
1998 last_snapshot)
1999 continue;
2000
2001 *level = i;
2002 return 0;
2003 }
2004 free_extent_buffer(path->nodes[i]);
2005 path->nodes[i] = NULL;
2006 }
2007 return 1;
2008}
2009
2010/*
2011 * walk down reloc tree to find relocated block of lowest level
2012 */
2013static noinline_for_stack
2014int walk_down_reloc_tree(struct btrfs_root *root, struct btrfs_path *path,
2015 int *level)
2016{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002017 struct btrfs_fs_info *fs_info = root->fs_info;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002018 struct extent_buffer *eb = NULL;
2019 int i;
2020 u64 bytenr;
2021 u64 ptr_gen = 0;
2022 u64 last_snapshot;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002023 u32 nritems;
2024
2025 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
2026
2027 for (i = *level; i > 0; i--) {
Qu Wenruo581c1762018-03-29 09:08:11 +08002028 struct btrfs_key first_key;
2029
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002030 eb = path->nodes[i];
2031 nritems = btrfs_header_nritems(eb);
2032 while (path->slots[i] < nritems) {
2033 ptr_gen = btrfs_node_ptr_generation(eb, path->slots[i]);
2034 if (ptr_gen > last_snapshot)
2035 break;
2036 path->slots[i]++;
2037 }
2038 if (path->slots[i] >= nritems) {
2039 if (i == *level)
2040 break;
2041 *level = i + 1;
2042 return 0;
2043 }
2044 if (i == 1) {
2045 *level = i;
2046 return 0;
2047 }
2048
2049 bytenr = btrfs_node_blockptr(eb, path->slots[i]);
Qu Wenruo581c1762018-03-29 09:08:11 +08002050 btrfs_node_key_to_cpu(eb, &first_key, path->slots[i]);
2051 eb = read_tree_block(fs_info, bytenr, ptr_gen, i - 1,
2052 &first_key);
Liu Bo64c043d2015-05-25 17:30:15 +08002053 if (IS_ERR(eb)) {
2054 return PTR_ERR(eb);
2055 } else if (!extent_buffer_uptodate(eb)) {
Josef Bacik416bc652013-04-23 14:17:42 -04002056 free_extent_buffer(eb);
2057 return -EIO;
2058 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002059 BUG_ON(btrfs_header_level(eb) != i - 1);
2060 path->nodes[i - 1] = eb;
2061 path->slots[i - 1] = 0;
2062 }
2063 return 1;
2064}
2065
2066/*
2067 * invalidate extent cache for file extents whose key in range of
2068 * [min_key, max_key)
2069 */
2070static int invalidate_extent_cache(struct btrfs_root *root,
2071 struct btrfs_key *min_key,
2072 struct btrfs_key *max_key)
2073{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002074 struct btrfs_fs_info *fs_info = root->fs_info;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002075 struct inode *inode = NULL;
2076 u64 objectid;
2077 u64 start, end;
Li Zefan33345d012011-04-20 10:31:50 +08002078 u64 ino;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002079
2080 objectid = min_key->objectid;
2081 while (1) {
2082 cond_resched();
2083 iput(inode);
2084
2085 if (objectid > max_key->objectid)
2086 break;
2087
2088 inode = find_next_inode(root, objectid);
2089 if (!inode)
2090 break;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002091 ino = btrfs_ino(BTRFS_I(inode));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002092
Li Zefan33345d012011-04-20 10:31:50 +08002093 if (ino > max_key->objectid) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002094 iput(inode);
2095 break;
2096 }
2097
Li Zefan33345d012011-04-20 10:31:50 +08002098 objectid = ino + 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002099 if (!S_ISREG(inode->i_mode))
2100 continue;
2101
Li Zefan33345d012011-04-20 10:31:50 +08002102 if (unlikely(min_key->objectid == ino)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002103 if (min_key->type > BTRFS_EXTENT_DATA_KEY)
2104 continue;
2105 if (min_key->type < BTRFS_EXTENT_DATA_KEY)
2106 start = 0;
2107 else {
2108 start = min_key->offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002109 WARN_ON(!IS_ALIGNED(start, fs_info->sectorsize));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002110 }
2111 } else {
2112 start = 0;
2113 }
2114
Li Zefan33345d012011-04-20 10:31:50 +08002115 if (unlikely(max_key->objectid == ino)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002116 if (max_key->type < BTRFS_EXTENT_DATA_KEY)
2117 continue;
2118 if (max_key->type > BTRFS_EXTENT_DATA_KEY) {
2119 end = (u64)-1;
2120 } else {
2121 if (max_key->offset == 0)
2122 continue;
2123 end = max_key->offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002124 WARN_ON(!IS_ALIGNED(end, fs_info->sectorsize));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002125 end--;
2126 }
2127 } else {
2128 end = (u64)-1;
2129 }
2130
2131 /* the lock_extent waits for readpage to complete */
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002132 lock_extent(&BTRFS_I(inode)->io_tree, start, end);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02002133 btrfs_drop_extent_cache(BTRFS_I(inode), start, end, 1);
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002134 unlock_extent(&BTRFS_I(inode)->io_tree, start, end);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002135 }
2136 return 0;
2137}
2138
2139static int find_next_key(struct btrfs_path *path, int level,
2140 struct btrfs_key *key)
2141
2142{
2143 while (level < BTRFS_MAX_LEVEL) {
2144 if (!path->nodes[level])
2145 break;
2146 if (path->slots[level] + 1 <
2147 btrfs_header_nritems(path->nodes[level])) {
2148 btrfs_node_key_to_cpu(path->nodes[level], key,
2149 path->slots[level] + 1);
2150 return 0;
2151 }
2152 level++;
2153 }
2154 return 1;
2155}
2156
2157/*
Qu Wenruod2311e62019-01-23 15:15:14 +08002158 * Insert current subvolume into reloc_control::dirty_subvol_roots
2159 */
2160static void insert_dirty_subvol(struct btrfs_trans_handle *trans,
2161 struct reloc_control *rc,
2162 struct btrfs_root *root)
2163{
2164 struct btrfs_root *reloc_root = root->reloc_root;
2165 struct btrfs_root_item *reloc_root_item;
2166
2167 /* @root must be a subvolume tree root with a valid reloc tree */
2168 ASSERT(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
2169 ASSERT(reloc_root);
2170
2171 reloc_root_item = &reloc_root->root_item;
2172 memset(&reloc_root_item->drop_progress, 0,
2173 sizeof(reloc_root_item->drop_progress));
2174 reloc_root_item->drop_level = 0;
2175 btrfs_set_root_refs(reloc_root_item, 0);
2176 btrfs_update_reloc_root(trans, root);
2177
2178 if (list_empty(&root->reloc_dirty_list)) {
2179 btrfs_grab_fs_root(root);
2180 list_add_tail(&root->reloc_dirty_list, &rc->dirty_subvol_roots);
2181 }
2182}
2183
2184static int clean_dirty_subvols(struct reloc_control *rc)
2185{
2186 struct btrfs_root *root;
2187 struct btrfs_root *next;
2188 int ret = 0;
Qu Wenruo30d40572019-05-22 16:33:11 +08002189 int ret2;
Qu Wenruod2311e62019-01-23 15:15:14 +08002190
2191 list_for_each_entry_safe(root, next, &rc->dirty_subvol_roots,
2192 reloc_dirty_list) {
Qu Wenruo30d40572019-05-22 16:33:11 +08002193 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
2194 /* Merged subvolume, cleanup its reloc root */
2195 struct btrfs_root *reloc_root = root->reloc_root;
Qu Wenruod2311e62019-01-23 15:15:14 +08002196
Qu Wenruo30d40572019-05-22 16:33:11 +08002197 list_del_init(&root->reloc_dirty_list);
2198 root->reloc_root = NULL;
2199 if (reloc_root) {
Qu Wenruod2311e62019-01-23 15:15:14 +08002200
Qu Wenruo30d40572019-05-22 16:33:11 +08002201 ret2 = btrfs_drop_snapshot(reloc_root, NULL, 0, 1);
2202 if (ret2 < 0 && !ret)
2203 ret = ret2;
2204 }
Qu Wenruo1fac4a52019-09-23 14:56:14 +08002205 clear_bit(BTRFS_ROOT_DEAD_RELOC_TREE, &root->state);
Qu Wenruo30d40572019-05-22 16:33:11 +08002206 btrfs_put_fs_root(root);
2207 } else {
2208 /* Orphan reloc tree, just clean it up */
2209 ret2 = btrfs_drop_snapshot(root, NULL, 0, 1);
Qu Wenruod2311e62019-01-23 15:15:14 +08002210 if (ret2 < 0 && !ret)
2211 ret = ret2;
2212 }
Qu Wenruod2311e62019-01-23 15:15:14 +08002213 }
2214 return ret;
2215}
2216
2217/*
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002218 * merge the relocated tree blocks in reloc tree with corresponding
2219 * fs tree.
2220 */
2221static noinline_for_stack int merge_reloc_root(struct reloc_control *rc,
2222 struct btrfs_root *root)
2223{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002224 struct btrfs_fs_info *fs_info = rc->extent_root->fs_info;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002225 struct btrfs_key key;
2226 struct btrfs_key next_key;
Josef Bacik9e6a0c522013-10-31 10:07:19 -04002227 struct btrfs_trans_handle *trans = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002228 struct btrfs_root *reloc_root;
2229 struct btrfs_root_item *root_item;
2230 struct btrfs_path *path;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002231 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002232 int level;
2233 int max_level;
2234 int replaced = 0;
2235 int ret;
2236 int err = 0;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002237 u32 min_reserved;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002238
2239 path = btrfs_alloc_path();
2240 if (!path)
2241 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +01002242 path->reada = READA_FORWARD;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002243
2244 reloc_root = root->reloc_root;
2245 root_item = &reloc_root->root_item;
2246
2247 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
2248 level = btrfs_root_level(root_item);
2249 extent_buffer_get(reloc_root->node);
2250 path->nodes[level] = reloc_root->node;
2251 path->slots[level] = 0;
2252 } else {
2253 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
2254
2255 level = root_item->drop_level;
2256 BUG_ON(level == 0);
2257 path->lowest_level = level;
2258 ret = btrfs_search_slot(NULL, reloc_root, &key, path, 0, 0);
Yan Zheng33c66f42009-07-22 09:59:00 -04002259 path->lowest_level = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002260 if (ret < 0) {
2261 btrfs_free_path(path);
2262 return ret;
2263 }
2264
2265 btrfs_node_key_to_cpu(path->nodes[level], &next_key,
2266 path->slots[level]);
2267 WARN_ON(memcmp(&key, &next_key, sizeof(key)));
2268
2269 btrfs_unlock_up_safe(path, 0);
2270 }
2271
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002272 min_reserved = fs_info->nodesize * (BTRFS_MAX_LEVEL - 1) * 2;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002273 memset(&next_key, 0, sizeof(next_key));
2274
2275 while (1) {
Miao Xie08e007d2012-10-16 11:33:38 +00002276 ret = btrfs_block_rsv_refill(root, rc->block_rsv, min_reserved,
2277 BTRFS_RESERVE_FLUSH_ALL);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002278 if (ret) {
Josef Bacik9e6a0c522013-10-31 10:07:19 -04002279 err = ret;
2280 goto out;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002281 }
Josef Bacik9e6a0c522013-10-31 10:07:19 -04002282 trans = btrfs_start_transaction(root, 0);
2283 if (IS_ERR(trans)) {
2284 err = PTR_ERR(trans);
2285 trans = NULL;
2286 goto out;
2287 }
2288 trans->block_rsv = rc->block_rsv;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002289
2290 replaced = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002291 max_level = level;
2292
2293 ret = walk_down_reloc_tree(reloc_root, path, &level);
2294 if (ret < 0) {
2295 err = ret;
2296 goto out;
2297 }
2298 if (ret > 0)
2299 break;
2300
2301 if (!find_next_key(path, level, &key) &&
2302 btrfs_comp_cpu_keys(&next_key, &key) >= 0) {
2303 ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002304 } else {
Qu Wenruo3d0174f2018-09-27 14:42:35 +08002305 ret = replace_path(trans, rc, root, reloc_root, path,
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002306 &next_key, level, max_level);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002307 }
2308 if (ret < 0) {
2309 err = ret;
2310 goto out;
2311 }
2312
2313 if (ret > 0) {
2314 level = ret;
2315 btrfs_node_key_to_cpu(path->nodes[level], &key,
2316 path->slots[level]);
2317 replaced = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002318 }
2319
2320 ret = walk_up_reloc_tree(reloc_root, path, &level);
2321 if (ret > 0)
2322 break;
2323
2324 BUG_ON(level == 0);
2325 /*
2326 * save the merging progress in the drop_progress.
2327 * this is OK since root refs == 1 in this case.
2328 */
2329 btrfs_node_key(path->nodes[level], &root_item->drop_progress,
2330 path->slots[level]);
2331 root_item->drop_level = level;
2332
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002333 btrfs_end_transaction_throttle(trans);
Josef Bacik9e6a0c522013-10-31 10:07:19 -04002334 trans = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002335
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002336 btrfs_btree_balance_dirty(fs_info);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002337
2338 if (replaced && rc->stage == UPDATE_DATA_PTRS)
2339 invalidate_extent_cache(root, &key, &next_key);
2340 }
2341
2342 /*
2343 * handle the case only one block in the fs tree need to be
2344 * relocated and the block is tree root.
2345 */
2346 leaf = btrfs_lock_root_node(root);
2347 ret = btrfs_cow_block(trans, root, leaf, NULL, 0, &leaf);
2348 btrfs_tree_unlock(leaf);
2349 free_extent_buffer(leaf);
2350 if (ret < 0)
2351 err = ret;
2352out:
2353 btrfs_free_path(path);
2354
Qu Wenruod2311e62019-01-23 15:15:14 +08002355 if (err == 0)
2356 insert_dirty_subvol(trans, rc, root);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002357
Josef Bacik9e6a0c522013-10-31 10:07:19 -04002358 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002359 btrfs_end_transaction_throttle(trans);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002360
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002361 btrfs_btree_balance_dirty(fs_info);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002362
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002363 if (replaced && rc->stage == UPDATE_DATA_PTRS)
2364 invalidate_extent_cache(root, &key, &next_key);
2365
2366 return err;
2367}
2368
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002369static noinline_for_stack
2370int prepare_to_merge(struct reloc_control *rc, int err)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002371{
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002372 struct btrfs_root *root = rc->extent_root;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002373 struct btrfs_fs_info *fs_info = root->fs_info;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002374 struct btrfs_root *reloc_root;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002375 struct btrfs_trans_handle *trans;
2376 LIST_HEAD(reloc_roots);
2377 u64 num_bytes = 0;
2378 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002379
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002380 mutex_lock(&fs_info->reloc_mutex);
2381 rc->merging_rsv_size += fs_info->nodesize * (BTRFS_MAX_LEVEL - 1) * 2;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002382 rc->merging_rsv_size += rc->nodes_relocated * 2;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002383 mutex_unlock(&fs_info->reloc_mutex);
Chris Mason75857172011-06-13 20:00:16 -04002384
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002385again:
2386 if (!err) {
2387 num_bytes = rc->merging_rsv_size;
Miao Xie08e007d2012-10-16 11:33:38 +00002388 ret = btrfs_block_rsv_add(root, rc->block_rsv, num_bytes,
2389 BTRFS_RESERVE_FLUSH_ALL);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002390 if (ret)
2391 err = ret;
2392 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002393
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002394 trans = btrfs_join_transaction(rc->extent_root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00002395 if (IS_ERR(trans)) {
2396 if (!err)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002397 btrfs_block_rsv_release(fs_info, rc->block_rsv,
2398 num_bytes);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00002399 return PTR_ERR(trans);
2400 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002401
2402 if (!err) {
2403 if (num_bytes != rc->merging_rsv_size) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002404 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002405 btrfs_block_rsv_release(fs_info, rc->block_rsv,
2406 num_bytes);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002407 goto again;
2408 }
2409 }
2410
2411 rc->merge_reloc_tree = 1;
2412
2413 while (!list_empty(&rc->reloc_roots)) {
2414 reloc_root = list_entry(rc->reloc_roots.next,
2415 struct btrfs_root, root_list);
2416 list_del_init(&reloc_root->root_list);
2417
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002418 root = read_fs_root(fs_info, reloc_root->root_key.offset);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002419 BUG_ON(IS_ERR(root));
2420 BUG_ON(root->reloc_root != reloc_root);
2421
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002422 /*
2423 * set reference count to 1, so btrfs_recover_relocation
2424 * knows it should resumes merging
2425 */
2426 if (!err)
2427 btrfs_set_root_refs(&reloc_root->root_item, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002428 btrfs_update_reloc_root(trans, root);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002429
2430 list_add(&reloc_root->root_list, &reloc_roots);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002431 }
2432
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002433 list_splice(&reloc_roots, &rc->reloc_roots);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002434
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002435 if (!err)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002436 btrfs_commit_transaction(trans);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002437 else
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002438 btrfs_end_transaction(trans);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002439 return err;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002440}
2441
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002442static noinline_for_stack
Liu Boaca1bba2013-03-04 16:25:37 +00002443void free_reloc_roots(struct list_head *list)
2444{
2445 struct btrfs_root *reloc_root;
2446
2447 while (!list_empty(list)) {
2448 reloc_root = list_entry(list->next, struct btrfs_root,
2449 root_list);
Naohiro Aotabb166d72017-08-25 14:15:14 +09002450 __del_reloc_root(reloc_root);
Josef Bacik6bdf1312016-09-02 15:25:43 -04002451 free_extent_buffer(reloc_root->node);
2452 free_extent_buffer(reloc_root->commit_root);
2453 reloc_root->node = NULL;
2454 reloc_root->commit_root = NULL;
Liu Boaca1bba2013-03-04 16:25:37 +00002455 }
2456}
2457
2458static noinline_for_stack
David Sterba94404e82014-07-30 01:53:30 +02002459void merge_reloc_roots(struct reloc_control *rc)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002460{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002461 struct btrfs_fs_info *fs_info = rc->extent_root->fs_info;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002462 struct btrfs_root *root;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002463 struct btrfs_root *reloc_root;
2464 LIST_HEAD(reloc_roots);
2465 int found = 0;
Liu Boaca1bba2013-03-04 16:25:37 +00002466 int ret = 0;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002467again:
2468 root = rc->extent_root;
Chris Mason75857172011-06-13 20:00:16 -04002469
2470 /*
2471 * this serializes us with btrfs_record_root_in_transaction,
2472 * we have to make sure nobody is in the middle of
2473 * adding their roots to the list while we are
2474 * doing this splice
2475 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002476 mutex_lock(&fs_info->reloc_mutex);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002477 list_splice_init(&rc->reloc_roots, &reloc_roots);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002478 mutex_unlock(&fs_info->reloc_mutex);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002479
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002480 while (!list_empty(&reloc_roots)) {
2481 found = 1;
2482 reloc_root = list_entry(reloc_roots.next,
2483 struct btrfs_root, root_list);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002484
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002485 if (btrfs_root_refs(&reloc_root->root_item) > 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002486 root = read_fs_root(fs_info,
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002487 reloc_root->root_key.offset);
2488 BUG_ON(IS_ERR(root));
2489 BUG_ON(root->reloc_root != reloc_root);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002490
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002491 ret = merge_reloc_root(rc, root);
Josef Bacikb37b39c2013-07-23 16:57:15 -04002492 if (ret) {
Wang Shilong25e293c2013-12-26 13:10:50 +08002493 if (list_empty(&reloc_root->root_list))
2494 list_add_tail(&reloc_root->root_list,
2495 &reloc_roots);
Liu Boaca1bba2013-03-04 16:25:37 +00002496 goto out;
Josef Bacikb37b39c2013-07-23 16:57:15 -04002497 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002498 } else {
2499 list_del_init(&reloc_root->root_list);
Qu Wenruo30d40572019-05-22 16:33:11 +08002500 /* Don't forget to queue this reloc root for cleanup */
2501 list_add_tail(&reloc_root->reloc_dirty_list,
2502 &rc->dirty_subvol_roots);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002503 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002504 }
2505
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002506 if (found) {
2507 found = 0;
2508 goto again;
2509 }
Liu Boaca1bba2013-03-04 16:25:37 +00002510out:
2511 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002512 btrfs_handle_fs_error(fs_info, ret, NULL);
Liu Boaca1bba2013-03-04 16:25:37 +00002513 if (!list_empty(&reloc_roots))
2514 free_reloc_roots(&reloc_roots);
Wang Shilong467bb1d2013-12-11 19:29:52 +08002515
2516 /* new reloc root may be added */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002517 mutex_lock(&fs_info->reloc_mutex);
Wang Shilong467bb1d2013-12-11 19:29:52 +08002518 list_splice_init(&rc->reloc_roots, &reloc_roots);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002519 mutex_unlock(&fs_info->reloc_mutex);
Wang Shilong467bb1d2013-12-11 19:29:52 +08002520 if (!list_empty(&reloc_roots))
2521 free_reloc_roots(&reloc_roots);
Liu Boaca1bba2013-03-04 16:25:37 +00002522 }
2523
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002524 BUG_ON(!RB_EMPTY_ROOT(&rc->reloc_root_tree.rb_root));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002525}
2526
2527static void free_block_list(struct rb_root *blocks)
2528{
2529 struct tree_block *block;
2530 struct rb_node *rb_node;
2531 while ((rb_node = rb_first(blocks))) {
2532 block = rb_entry(rb_node, struct tree_block, rb_node);
2533 rb_erase(rb_node, blocks);
2534 kfree(block);
2535 }
2536}
2537
2538static int record_reloc_root_in_trans(struct btrfs_trans_handle *trans,
2539 struct btrfs_root *reloc_root)
2540{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002541 struct btrfs_fs_info *fs_info = reloc_root->fs_info;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002542 struct btrfs_root *root;
2543
2544 if (reloc_root->last_trans == trans->transid)
2545 return 0;
2546
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002547 root = read_fs_root(fs_info, reloc_root->root_key.offset);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002548 BUG_ON(IS_ERR(root));
2549 BUG_ON(root->reloc_root != reloc_root);
2550
2551 return btrfs_record_root_in_trans(trans, root);
2552}
2553
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002554static noinline_for_stack
2555struct btrfs_root *select_reloc_root(struct btrfs_trans_handle *trans,
2556 struct reloc_control *rc,
2557 struct backref_node *node,
Wang Shilongdc4103f2013-12-26 13:10:49 +08002558 struct backref_edge *edges[])
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002559{
2560 struct backref_node *next;
2561 struct btrfs_root *root;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002562 int index = 0;
2563
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002564 next = node;
2565 while (1) {
2566 cond_resched();
2567 next = walk_up_backref(next, edges, &index);
2568 root = next->root;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002569 BUG_ON(!root);
Miao Xie27cdeb72014-04-02 19:51:05 +08002570 BUG_ON(!test_bit(BTRFS_ROOT_REF_COWS, &root->state));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002571
2572 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
2573 record_reloc_root_in_trans(trans, root);
2574 break;
2575 }
2576
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002577 btrfs_record_root_in_trans(trans, root);
2578 root = root->reloc_root;
2579
2580 if (next->new_bytenr != root->node->start) {
2581 BUG_ON(next->new_bytenr);
2582 BUG_ON(!list_empty(&next->list));
2583 next->new_bytenr = root->node->start;
2584 next->root = root;
2585 list_add_tail(&next->list,
2586 &rc->backref_cache.changed);
2587 __mark_block_processed(rc, next);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002588 break;
2589 }
2590
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002591 WARN_ON(1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002592 root = NULL;
2593 next = walk_down_backref(edges, &index);
2594 if (!next || next->level <= node->level)
2595 break;
2596 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002597 if (!root)
2598 return NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002599
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002600 next = node;
2601 /* setup backref node path for btrfs_reloc_cow_block */
2602 while (1) {
2603 rc->backref_cache.path[next->level] = next;
2604 if (--index < 0)
2605 break;
2606 next = edges[index]->node[UPPER];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002607 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002608 return root;
2609}
2610
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002611/*
2612 * select a tree root for relocation. return NULL if the block
2613 * is reference counted. we should use do_relocation() in this
2614 * case. return a tree root pointer if the block isn't reference
2615 * counted. return -ENOENT if the block is root of reloc tree.
2616 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002617static noinline_for_stack
Zhaolei147d2562015-08-06 20:58:11 +08002618struct btrfs_root *select_one_root(struct backref_node *node)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002619{
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002620 struct backref_node *next;
2621 struct btrfs_root *root;
2622 struct btrfs_root *fs_root = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002623 struct backref_edge *edges[BTRFS_MAX_LEVEL - 1];
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002624 int index = 0;
2625
2626 next = node;
2627 while (1) {
2628 cond_resched();
2629 next = walk_up_backref(next, edges, &index);
2630 root = next->root;
2631 BUG_ON(!root);
2632
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002633 /* no other choice for non-references counted tree */
Miao Xie27cdeb72014-04-02 19:51:05 +08002634 if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state))
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002635 return root;
2636
2637 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID)
2638 fs_root = root;
2639
2640 if (next != node)
2641 return NULL;
2642
2643 next = walk_down_backref(edges, &index);
2644 if (!next || next->level <= node->level)
2645 break;
2646 }
2647
2648 if (!fs_root)
2649 return ERR_PTR(-ENOENT);
2650 return fs_root;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002651}
2652
2653static noinline_for_stack
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002654u64 calcu_metadata_size(struct reloc_control *rc,
2655 struct backref_node *node, int reserve)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002656{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002657 struct btrfs_fs_info *fs_info = rc->extent_root->fs_info;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002658 struct backref_node *next = node;
2659 struct backref_edge *edge;
2660 struct backref_edge *edges[BTRFS_MAX_LEVEL - 1];
2661 u64 num_bytes = 0;
2662 int index = 0;
2663
2664 BUG_ON(reserve && node->processed);
2665
2666 while (next) {
2667 cond_resched();
2668 while (1) {
2669 if (next->processed && (reserve || next != node))
2670 break;
2671
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002672 num_bytes += fs_info->nodesize;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002673
2674 if (list_empty(&next->upper))
2675 break;
2676
2677 edge = list_entry(next->upper.next,
2678 struct backref_edge, list[LOWER]);
2679 edges[index++] = edge;
2680 next = edge->node[UPPER];
2681 }
2682 next = walk_down_backref(edges, &index);
2683 }
2684 return num_bytes;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002685}
2686
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002687static int reserve_metadata_space(struct btrfs_trans_handle *trans,
2688 struct reloc_control *rc,
2689 struct backref_node *node)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002690{
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002691 struct btrfs_root *root = rc->extent_root;
Jeff Mahoneyda170662016-06-15 09:22:56 -04002692 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002693 u64 num_bytes;
2694 int ret;
Wang Shilong0647bf52013-11-20 09:01:52 +08002695 u64 tmp;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002696
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002697 num_bytes = calcu_metadata_size(rc, node, 1) * 2;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002698
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002699 trans->block_rsv = rc->block_rsv;
Wang Shilong0647bf52013-11-20 09:01:52 +08002700 rc->reserved_bytes += num_bytes;
Josef Bacik8ca17f02016-05-27 13:24:13 -04002701
2702 /*
2703 * We are under a transaction here so we can only do limited flushing.
2704 * If we get an enospc just kick back -EAGAIN so we know to drop the
2705 * transaction and try to refill when we can flush all the things.
2706 */
Wang Shilong0647bf52013-11-20 09:01:52 +08002707 ret = btrfs_block_rsv_refill(root, rc->block_rsv, num_bytes,
Josef Bacik8ca17f02016-05-27 13:24:13 -04002708 BTRFS_RESERVE_FLUSH_LIMIT);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002709 if (ret) {
Jeff Mahoneyda170662016-06-15 09:22:56 -04002710 tmp = fs_info->nodesize * RELOCATION_RESERVED_NODES;
Josef Bacik8ca17f02016-05-27 13:24:13 -04002711 while (tmp <= rc->reserved_bytes)
2712 tmp <<= 1;
2713 /*
2714 * only one thread can access block_rsv at this point,
2715 * so we don't need hold lock to protect block_rsv.
2716 * we expand more reservation size here to allow enough
Andrea Gelmini52042d82018-11-28 12:05:13 +01002717 * space for relocation and we will return earlier in
Josef Bacik8ca17f02016-05-27 13:24:13 -04002718 * enospc case.
2719 */
Jeff Mahoneyda170662016-06-15 09:22:56 -04002720 rc->block_rsv->size = tmp + fs_info->nodesize *
2721 RELOCATION_RESERVED_NODES;
Josef Bacik8ca17f02016-05-27 13:24:13 -04002722 return -EAGAIN;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002723 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002724
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002725 return 0;
2726}
2727
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002728/*
2729 * relocate a block tree, and then update pointers in upper level
2730 * blocks that reference the block to point to the new location.
2731 *
2732 * if called by link_to_upper, the block has already been relocated.
2733 * in that case this function just updates pointers.
2734 */
2735static int do_relocation(struct btrfs_trans_handle *trans,
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002736 struct reloc_control *rc,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002737 struct backref_node *node,
2738 struct btrfs_key *key,
2739 struct btrfs_path *path, int lowest)
2740{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002741 struct btrfs_fs_info *fs_info = rc->extent_root->fs_info;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002742 struct backref_node *upper;
2743 struct backref_edge *edge;
2744 struct backref_edge *edges[BTRFS_MAX_LEVEL - 1];
2745 struct btrfs_root *root;
2746 struct extent_buffer *eb;
2747 u32 blocksize;
2748 u64 bytenr;
2749 u64 generation;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002750 int slot;
2751 int ret;
2752 int err = 0;
2753
2754 BUG_ON(lowest && node->eb);
2755
2756 path->lowest_level = node->level + 1;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002757 rc->backref_cache.path[node->level] = node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002758 list_for_each_entry(edge, &node->upper, list[LOWER]) {
Qu Wenruo581c1762018-03-29 09:08:11 +08002759 struct btrfs_key first_key;
Qu Wenruo82fa1132019-04-04 14:45:35 +08002760 struct btrfs_ref ref = { 0 };
Qu Wenruo581c1762018-03-29 09:08:11 +08002761
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002762 cond_resched();
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002763
2764 upper = edge->node[UPPER];
Wang Shilongdc4103f2013-12-26 13:10:49 +08002765 root = select_reloc_root(trans, rc, upper, edges);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002766 BUG_ON(!root);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002767
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002768 if (upper->eb && !upper->locked) {
2769 if (!lowest) {
2770 ret = btrfs_bin_search(upper->eb, key,
2771 upper->level, &slot);
Filipe Mananacbca7d52019-02-18 16:57:26 +00002772 if (ret < 0) {
2773 err = ret;
2774 goto next;
2775 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002776 BUG_ON(ret);
2777 bytenr = btrfs_node_blockptr(upper->eb, slot);
2778 if (node->eb->start == bytenr)
2779 goto next;
2780 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002781 drop_node_buffer(upper);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002782 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002783
2784 if (!upper->eb) {
2785 ret = btrfs_search_slot(trans, root, key, path, 0, 1);
Liu Bo3561b9d2016-09-14 08:51:46 -07002786 if (ret) {
2787 if (ret < 0)
2788 err = ret;
2789 else
2790 err = -ENOENT;
2791
2792 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002793 break;
2794 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002795
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002796 if (!upper->eb) {
2797 upper->eb = path->nodes[upper->level];
2798 path->nodes[upper->level] = NULL;
2799 } else {
2800 BUG_ON(upper->eb != path->nodes[upper->level]);
2801 }
2802
2803 upper->locked = 1;
2804 path->locks[upper->level] = 0;
2805
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002806 slot = path->slots[upper->level];
David Sterbab3b4aa72011-04-21 01:20:15 +02002807 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002808 } else {
2809 ret = btrfs_bin_search(upper->eb, key, upper->level,
2810 &slot);
Filipe Mananacbca7d52019-02-18 16:57:26 +00002811 if (ret < 0) {
2812 err = ret;
2813 goto next;
2814 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002815 BUG_ON(ret);
2816 }
2817
2818 bytenr = btrfs_node_blockptr(upper->eb, slot);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002819 if (lowest) {
Liu Bo4547f4d2016-09-23 14:05:04 -07002820 if (bytenr != node->bytenr) {
2821 btrfs_err(root->fs_info,
2822 "lowest leaf/node mismatch: bytenr %llu node->bytenr %llu slot %d upper %llu",
2823 bytenr, node->bytenr, slot,
2824 upper->eb->start);
2825 err = -EIO;
2826 goto next;
2827 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002828 } else {
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002829 if (node->eb->start == bytenr)
2830 goto next;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002831 }
2832
Jeff Mahoneyda170662016-06-15 09:22:56 -04002833 blocksize = root->fs_info->nodesize;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002834 generation = btrfs_node_ptr_generation(upper->eb, slot);
Qu Wenruo581c1762018-03-29 09:08:11 +08002835 btrfs_node_key_to_cpu(upper->eb, &first_key, slot);
2836 eb = read_tree_block(fs_info, bytenr, generation,
2837 upper->level - 1, &first_key);
Liu Bo64c043d2015-05-25 17:30:15 +08002838 if (IS_ERR(eb)) {
2839 err = PTR_ERR(eb);
2840 goto next;
2841 } else if (!extent_buffer_uptodate(eb)) {
Josef Bacik416bc652013-04-23 14:17:42 -04002842 free_extent_buffer(eb);
Tsutomu Itoh97d9a8a2011-03-24 06:33:21 +00002843 err = -EIO;
2844 goto next;
2845 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002846 btrfs_tree_lock(eb);
David Sterba8bead252018-04-04 02:03:48 +02002847 btrfs_set_lock_blocking_write(eb);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002848
2849 if (!node->eb) {
2850 ret = btrfs_cow_block(trans, root, eb, upper->eb,
2851 slot, &eb);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002852 btrfs_tree_unlock(eb);
2853 free_extent_buffer(eb);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002854 if (ret < 0) {
2855 err = ret;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002856 goto next;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002857 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002858 BUG_ON(node->eb != eb);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002859 } else {
2860 btrfs_set_node_blockptr(upper->eb, slot,
2861 node->eb->start);
2862 btrfs_set_node_ptr_generation(upper->eb, slot,
2863 trans->transid);
2864 btrfs_mark_buffer_dirty(upper->eb);
2865
Qu Wenruo82fa1132019-04-04 14:45:35 +08002866 btrfs_init_generic_ref(&ref, BTRFS_ADD_DELAYED_REF,
2867 node->eb->start, blocksize,
2868 upper->eb->start);
2869 ref.real_root = root->root_key.objectid;
2870 btrfs_init_tree_ref(&ref, node->level,
2871 btrfs_header_owner(upper->eb));
2872 ret = btrfs_inc_extent_ref(trans, &ref);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002873 BUG_ON(ret);
2874
2875 ret = btrfs_drop_subtree(trans, root, eb, upper->eb);
2876 BUG_ON(ret);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002877 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002878next:
2879 if (!upper->pending)
2880 drop_node_buffer(upper);
2881 else
2882 unlock_node_buffer(upper);
2883 if (err)
2884 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002885 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002886
2887 if (!err && node->pending) {
2888 drop_node_buffer(node);
2889 list_move_tail(&node->list, &rc->backref_cache.changed);
2890 node->pending = 0;
2891 }
2892
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002893 path->lowest_level = 0;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002894 BUG_ON(err == -ENOSPC);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002895 return err;
2896}
2897
2898static int link_to_upper(struct btrfs_trans_handle *trans,
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002899 struct reloc_control *rc,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002900 struct backref_node *node,
2901 struct btrfs_path *path)
2902{
2903 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002904
2905 btrfs_node_key_to_cpu(node->eb, &key, 0);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002906 return do_relocation(trans, rc, node, &key, path, 0);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002907}
2908
2909static int finish_pending_nodes(struct btrfs_trans_handle *trans,
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002910 struct reloc_control *rc,
2911 struct btrfs_path *path, int err)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002912{
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002913 LIST_HEAD(list);
2914 struct backref_cache *cache = &rc->backref_cache;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002915 struct backref_node *node;
2916 int level;
2917 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002918
2919 for (level = 0; level < BTRFS_MAX_LEVEL; level++) {
2920 while (!list_empty(&cache->pending[level])) {
2921 node = list_entry(cache->pending[level].next,
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002922 struct backref_node, list);
2923 list_move_tail(&node->list, &list);
2924 BUG_ON(!node->pending);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002925
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002926 if (!err) {
2927 ret = link_to_upper(trans, rc, node, path);
2928 if (ret < 0)
2929 err = ret;
2930 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002931 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002932 list_splice_init(&list, &cache->pending[level]);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002933 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002934 return err;
2935}
2936
2937static void mark_block_processed(struct reloc_control *rc,
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002938 u64 bytenr, u32 blocksize)
2939{
2940 set_extent_bits(&rc->processed_blocks, bytenr, bytenr + blocksize - 1,
David Sterbaceeb0ae2016-04-26 23:54:39 +02002941 EXTENT_DIRTY);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002942}
2943
2944static void __mark_block_processed(struct reloc_control *rc,
2945 struct backref_node *node)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002946{
2947 u32 blocksize;
2948 if (node->level == 0 ||
2949 in_block_group(node->bytenr, rc->block_group)) {
Jeff Mahoneyda170662016-06-15 09:22:56 -04002950 blocksize = rc->extent_root->fs_info->nodesize;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002951 mark_block_processed(rc, node->bytenr, blocksize);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002952 }
2953 node->processed = 1;
2954}
2955
2956/*
2957 * mark a block and all blocks directly/indirectly reference the block
2958 * as processed.
2959 */
2960static void update_processed_blocks(struct reloc_control *rc,
2961 struct backref_node *node)
2962{
2963 struct backref_node *next = node;
2964 struct backref_edge *edge;
2965 struct backref_edge *edges[BTRFS_MAX_LEVEL - 1];
2966 int index = 0;
2967
2968 while (next) {
2969 cond_resched();
2970 while (1) {
2971 if (next->processed)
2972 break;
2973
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002974 __mark_block_processed(rc, next);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002975
2976 if (list_empty(&next->upper))
2977 break;
2978
2979 edge = list_entry(next->upper.next,
2980 struct backref_edge, list[LOWER]);
2981 edges[index++] = edge;
2982 next = edge->node[UPPER];
2983 }
2984 next = walk_down_backref(edges, &index);
2985 }
2986}
2987
David Sterba7476dfd2014-06-15 03:34:59 +02002988static int tree_block_processed(u64 bytenr, struct reloc_control *rc)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002989{
Jeff Mahoneyda170662016-06-15 09:22:56 -04002990 u32 blocksize = rc->extent_root->fs_info->nodesize;
David Sterba7476dfd2014-06-15 03:34:59 +02002991
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002992 if (test_range_bit(&rc->processed_blocks, bytenr,
Chris Mason9655d292009-09-02 15:22:30 -04002993 bytenr + blocksize - 1, EXTENT_DIRTY, 1, NULL))
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002994 return 1;
2995 return 0;
2996}
2997
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002998static int get_tree_block_key(struct btrfs_fs_info *fs_info,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002999 struct tree_block *block)
3000{
3001 struct extent_buffer *eb;
3002
3003 BUG_ON(block->key_ready);
Qu Wenruo581c1762018-03-29 09:08:11 +08003004 eb = read_tree_block(fs_info, block->bytenr, block->key.offset,
3005 block->level, NULL);
Liu Bo64c043d2015-05-25 17:30:15 +08003006 if (IS_ERR(eb)) {
3007 return PTR_ERR(eb);
3008 } else if (!extent_buffer_uptodate(eb)) {
Josef Bacik416bc652013-04-23 14:17:42 -04003009 free_extent_buffer(eb);
3010 return -EIO;
3011 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003012 if (block->level == 0)
3013 btrfs_item_key_to_cpu(eb, &block->key, 0);
3014 else
3015 btrfs_node_key_to_cpu(eb, &block->key, 0);
3016 free_extent_buffer(eb);
3017 block->key_ready = 1;
3018 return 0;
3019}
3020
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003021/*
3022 * helper function to relocate a tree block
3023 */
3024static int relocate_tree_block(struct btrfs_trans_handle *trans,
3025 struct reloc_control *rc,
3026 struct backref_node *node,
3027 struct btrfs_key *key,
3028 struct btrfs_path *path)
3029{
3030 struct btrfs_root *root;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04003031 int ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003032
Yan, Zheng3fd0a552010-05-16 10:49:59 -04003033 if (!node)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003034 return 0;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04003035
3036 BUG_ON(node->processed);
Zhaolei147d2562015-08-06 20:58:11 +08003037 root = select_one_root(node);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04003038 if (root == ERR_PTR(-ENOENT)) {
3039 update_processed_blocks(rc, node);
3040 goto out;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003041 }
3042
Miao Xie27cdeb72014-04-02 19:51:05 +08003043 if (!root || test_bit(BTRFS_ROOT_REF_COWS, &root->state)) {
Yan, Zheng3fd0a552010-05-16 10:49:59 -04003044 ret = reserve_metadata_space(trans, rc, node);
3045 if (ret)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003046 goto out;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04003047 }
3048
3049 if (root) {
Miao Xie27cdeb72014-04-02 19:51:05 +08003050 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state)) {
Yan, Zheng3fd0a552010-05-16 10:49:59 -04003051 BUG_ON(node->new_bytenr);
3052 BUG_ON(!list_empty(&node->list));
3053 btrfs_record_root_in_trans(trans, root);
3054 root = root->reloc_root;
3055 node->new_bytenr = root->node->start;
3056 node->root = root;
3057 list_add_tail(&node->list, &rc->backref_cache.changed);
3058 } else {
3059 path->lowest_level = node->level;
3060 ret = btrfs_search_slot(trans, root, key, path, 0, 1);
David Sterbab3b4aa72011-04-21 01:20:15 +02003061 btrfs_release_path(path);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04003062 if (ret > 0)
3063 ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003064 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -04003065 if (!ret)
3066 update_processed_blocks(rc, node);
3067 } else {
3068 ret = do_relocation(trans, rc, node, key, path, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003069 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003070out:
Wang Shilong0647bf52013-11-20 09:01:52 +08003071 if (ret || node->level == 0 || node->cowonly)
Yan, Zheng3fd0a552010-05-16 10:49:59 -04003072 remove_backref_node(&rc->backref_cache, node);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003073 return ret;
3074}
3075
3076/*
3077 * relocate a list of blocks
3078 */
3079static noinline_for_stack
3080int relocate_tree_blocks(struct btrfs_trans_handle *trans,
3081 struct reloc_control *rc, struct rb_root *blocks)
3082{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003083 struct btrfs_fs_info *fs_info = rc->extent_root->fs_info;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003084 struct backref_node *node;
3085 struct btrfs_path *path;
3086 struct tree_block *block;
Qu Wenruo98ff7b942018-09-21 15:20:29 +08003087 struct tree_block *next;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003088 int ret;
3089 int err = 0;
3090
3091 path = btrfs_alloc_path();
Liu Boe1a12672013-03-04 16:25:38 +00003092 if (!path) {
3093 err = -ENOMEM;
David Sterba34c2b292013-04-26 12:56:04 +00003094 goto out_free_blocks;
Liu Boe1a12672013-03-04 16:25:38 +00003095 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003096
Qu Wenruo98ff7b942018-09-21 15:20:29 +08003097 /* Kick in readahead for tree blocks with missing keys */
3098 rbtree_postorder_for_each_entry_safe(block, next, blocks, rb_node) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003099 if (!block->key_ready)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003100 readahead_tree_block(fs_info, block->bytenr);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003101 }
3102
Qu Wenruo98ff7b942018-09-21 15:20:29 +08003103 /* Get first keys */
3104 rbtree_postorder_for_each_entry_safe(block, next, blocks, rb_node) {
David Sterba34c2b292013-04-26 12:56:04 +00003105 if (!block->key_ready) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003106 err = get_tree_block_key(fs_info, block);
David Sterba34c2b292013-04-26 12:56:04 +00003107 if (err)
3108 goto out_free_path;
3109 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003110 }
3111
Qu Wenruo98ff7b942018-09-21 15:20:29 +08003112 /* Do tree relocation */
3113 rbtree_postorder_for_each_entry_safe(block, next, blocks, rb_node) {
Yan, Zheng3fd0a552010-05-16 10:49:59 -04003114 node = build_backref_tree(rc, &block->key,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003115 block->level, block->bytenr);
3116 if (IS_ERR(node)) {
3117 err = PTR_ERR(node);
3118 goto out;
3119 }
3120
3121 ret = relocate_tree_block(trans, rc, node, &block->key,
3122 path);
3123 if (ret < 0) {
Qu Wenruo98ff7b942018-09-21 15:20:29 +08003124 if (ret != -EAGAIN || &block->rb_node == rb_first(blocks))
Yan, Zheng3fd0a552010-05-16 10:49:59 -04003125 err = ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003126 goto out;
3127 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003128 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003129out:
Yan, Zheng3fd0a552010-05-16 10:49:59 -04003130 err = finish_pending_nodes(trans, rc, path, err);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003131
David Sterba34c2b292013-04-26 12:56:04 +00003132out_free_path:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003133 btrfs_free_path(path);
David Sterba34c2b292013-04-26 12:56:04 +00003134out_free_blocks:
Liu Boe1a12672013-03-04 16:25:38 +00003135 free_block_list(blocks);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003136 return err;
3137}
3138
3139static noinline_for_stack
Yan, Zhengefa56462010-05-16 10:49:59 -04003140int prealloc_file_extent_cluster(struct inode *inode,
3141 struct file_extent_cluster *cluster)
3142{
3143 u64 alloc_hint = 0;
3144 u64 start;
3145 u64 end;
3146 u64 offset = BTRFS_I(inode)->index_cnt;
3147 u64 num_bytes;
3148 int nr = 0;
3149 int ret = 0;
Wang Xiaoguangdcb40c12016-07-25 15:51:38 +08003150 u64 prealloc_start = cluster->start - offset;
3151 u64 prealloc_end = cluster->end - offset;
Wang Xiaoguang18513092016-07-25 15:51:40 +08003152 u64 cur_offset;
Qu Wenruo364ecf32017-02-27 15:10:38 +08003153 struct extent_changeset *data_reserved = NULL;
Yan, Zhengefa56462010-05-16 10:49:59 -04003154
3155 BUG_ON(cluster->start != cluster->boundary[0]);
Al Viro59551022016-01-22 15:40:57 -05003156 inode_lock(inode);
Yan, Zhengefa56462010-05-16 10:49:59 -04003157
Qu Wenruo364ecf32017-02-27 15:10:38 +08003158 ret = btrfs_check_data_free_space(inode, &data_reserved, prealloc_start,
Wang Xiaoguangdcb40c12016-07-25 15:51:38 +08003159 prealloc_end + 1 - prealloc_start);
Yan, Zhengefa56462010-05-16 10:49:59 -04003160 if (ret)
3161 goto out;
3162
Wang Xiaoguang18513092016-07-25 15:51:40 +08003163 cur_offset = prealloc_start;
Yan, Zhengefa56462010-05-16 10:49:59 -04003164 while (nr < cluster->nr) {
3165 start = cluster->boundary[nr] - offset;
3166 if (nr + 1 < cluster->nr)
3167 end = cluster->boundary[nr + 1] - 1 - offset;
3168 else
3169 end = cluster->end - offset;
3170
Jeff Mahoneyd0082372012-03-01 14:57:19 +01003171 lock_extent(&BTRFS_I(inode)->io_tree, start, end);
Yan, Zhengefa56462010-05-16 10:49:59 -04003172 num_bytes = end + 1 - start;
Wang Xiaoguang18513092016-07-25 15:51:40 +08003173 if (cur_offset < start)
Qu Wenruobc42bda2017-02-27 15:10:39 +08003174 btrfs_free_reserved_data_space(inode, data_reserved,
3175 cur_offset, start - cur_offset);
Yan, Zhengefa56462010-05-16 10:49:59 -04003176 ret = btrfs_prealloc_file_range(inode, 0, start,
3177 num_bytes, num_bytes,
3178 end + 1, &alloc_hint);
Wang Xiaoguang18513092016-07-25 15:51:40 +08003179 cur_offset = end + 1;
Jeff Mahoneyd0082372012-03-01 14:57:19 +01003180 unlock_extent(&BTRFS_I(inode)->io_tree, start, end);
Yan, Zhengefa56462010-05-16 10:49:59 -04003181 if (ret)
3182 break;
3183 nr++;
3184 }
Wang Xiaoguang18513092016-07-25 15:51:40 +08003185 if (cur_offset < prealloc_end)
Qu Wenruobc42bda2017-02-27 15:10:39 +08003186 btrfs_free_reserved_data_space(inode, data_reserved,
3187 cur_offset, prealloc_end + 1 - cur_offset);
Yan, Zhengefa56462010-05-16 10:49:59 -04003188out:
Al Viro59551022016-01-22 15:40:57 -05003189 inode_unlock(inode);
Qu Wenruo364ecf32017-02-27 15:10:38 +08003190 extent_changeset_free(data_reserved);
Yan, Zhengefa56462010-05-16 10:49:59 -04003191 return ret;
3192}
3193
3194static noinline_for_stack
Yan, Zheng0257bb82009-09-24 09:17:31 -04003195int setup_extent_mapping(struct inode *inode, u64 start, u64 end,
3196 u64 block_start)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003197{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003198 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003199 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
3200 struct extent_map *em;
Yan, Zheng0257bb82009-09-24 09:17:31 -04003201 int ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003202
David Sterba172ddd62011-04-21 00:48:27 +02003203 em = alloc_extent_map();
Yan, Zheng0257bb82009-09-24 09:17:31 -04003204 if (!em)
3205 return -ENOMEM;
3206
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003207 em->start = start;
Yan, Zheng0257bb82009-09-24 09:17:31 -04003208 em->len = end + 1 - start;
3209 em->block_len = em->len;
3210 em->block_start = block_start;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003211 em->bdev = fs_info->fs_devices->latest_bdev;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003212 set_bit(EXTENT_FLAG_PINNED, &em->flags);
3213
Jeff Mahoneyd0082372012-03-01 14:57:19 +01003214 lock_extent(&BTRFS_I(inode)->io_tree, start, end);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003215 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04003216 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04003217 ret = add_extent_mapping(em_tree, em, 0);
Chris Mason890871b2009-09-02 16:24:52 -04003218 write_unlock(&em_tree->lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003219 if (ret != -EEXIST) {
3220 free_extent_map(em);
3221 break;
3222 }
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02003223 btrfs_drop_extent_cache(BTRFS_I(inode), start, end, 0);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003224 }
Jeff Mahoneyd0082372012-03-01 14:57:19 +01003225 unlock_extent(&BTRFS_I(inode)->io_tree, start, end);
Yan, Zheng0257bb82009-09-24 09:17:31 -04003226 return ret;
3227}
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003228
Yan, Zheng0257bb82009-09-24 09:17:31 -04003229static int relocate_file_extent_cluster(struct inode *inode,
3230 struct file_extent_cluster *cluster)
3231{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003232 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan, Zheng0257bb82009-09-24 09:17:31 -04003233 u64 page_start;
3234 u64 page_end;
3235 u64 offset = BTRFS_I(inode)->index_cnt;
3236 unsigned long index;
3237 unsigned long last_index;
Yan, Zheng0257bb82009-09-24 09:17:31 -04003238 struct page *page;
3239 struct file_ra_state *ra;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04003240 gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
Yan, Zheng0257bb82009-09-24 09:17:31 -04003241 int nr = 0;
3242 int ret = 0;
3243
3244 if (!cluster->nr)
3245 return 0;
3246
3247 ra = kzalloc(sizeof(*ra), GFP_NOFS);
3248 if (!ra)
3249 return -ENOMEM;
3250
Yan, Zhengefa56462010-05-16 10:49:59 -04003251 ret = prealloc_file_extent_cluster(inode, cluster);
Yan, Zheng0257bb82009-09-24 09:17:31 -04003252 if (ret)
Yan, Zhengefa56462010-05-16 10:49:59 -04003253 goto out;
Yan, Zheng0257bb82009-09-24 09:17:31 -04003254
3255 file_ra_state_init(ra, inode->i_mapping);
3256
Yan, Zhengefa56462010-05-16 10:49:59 -04003257 ret = setup_extent_mapping(inode, cluster->start - offset,
3258 cluster->end - offset, cluster->start);
3259 if (ret)
3260 goto out;
3261
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003262 index = (cluster->start - offset) >> PAGE_SHIFT;
3263 last_index = (cluster->end - offset) >> PAGE_SHIFT;
Yan, Zheng0257bb82009-09-24 09:17:31 -04003264 while (index <= last_index) {
Nikolay Borisov9f3db422017-02-20 13:50:41 +02003265 ret = btrfs_delalloc_reserve_metadata(BTRFS_I(inode),
3266 PAGE_SIZE);
Yan, Zhengefa56462010-05-16 10:49:59 -04003267 if (ret)
3268 goto out;
3269
Yan, Zheng0257bb82009-09-24 09:17:31 -04003270 page = find_lock_page(inode->i_mapping, index);
3271 if (!page) {
3272 page_cache_sync_readahead(inode->i_mapping,
3273 ra, NULL, index,
3274 last_index + 1 - index);
Josef Bacika94733d2011-07-11 10:47:06 -04003275 page = find_or_create_page(inode->i_mapping, index,
Josef Bacik3b16a4e2011-09-21 15:05:58 -04003276 mask);
Yan, Zheng0257bb82009-09-24 09:17:31 -04003277 if (!page) {
Nikolay Borisov691fa052017-02-20 13:50:42 +02003278 btrfs_delalloc_release_metadata(BTRFS_I(inode),
Qu Wenruo43b18592017-12-12 15:34:32 +08003279 PAGE_SIZE, true);
Yan, Zheng0257bb82009-09-24 09:17:31 -04003280 ret = -ENOMEM;
Yan, Zhengefa56462010-05-16 10:49:59 -04003281 goto out;
Yan, Zheng0257bb82009-09-24 09:17:31 -04003282 }
3283 }
3284
3285 if (PageReadahead(page)) {
3286 page_cache_async_readahead(inode->i_mapping,
3287 ra, NULL, page, index,
3288 last_index + 1 - index);
3289 }
3290
3291 if (!PageUptodate(page)) {
3292 btrfs_readpage(NULL, page);
3293 lock_page(page);
3294 if (!PageUptodate(page)) {
3295 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003296 put_page(page);
Nikolay Borisov691fa052017-02-20 13:50:42 +02003297 btrfs_delalloc_release_metadata(BTRFS_I(inode),
Qu Wenruo43b18592017-12-12 15:34:32 +08003298 PAGE_SIZE, true);
Josef Bacik8b62f872017-10-19 14:15:55 -04003299 btrfs_delalloc_release_extents(BTRFS_I(inode),
Qu Wenruo43b18592017-12-12 15:34:32 +08003300 PAGE_SIZE, true);
Yan, Zheng0257bb82009-09-24 09:17:31 -04003301 ret = -EIO;
Yan, Zhengefa56462010-05-16 10:49:59 -04003302 goto out;
Yan, Zheng0257bb82009-09-24 09:17:31 -04003303 }
3304 }
3305
Miao Xie4eee4fa2012-12-21 09:17:45 +00003306 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003307 page_end = page_start + PAGE_SIZE - 1;
Yan, Zheng0257bb82009-09-24 09:17:31 -04003308
Jeff Mahoneyd0082372012-03-01 14:57:19 +01003309 lock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end);
Yan, Zheng0257bb82009-09-24 09:17:31 -04003310
3311 set_page_extent_mapped(page);
3312
3313 if (nr < cluster->nr &&
3314 page_start + offset == cluster->boundary[nr]) {
3315 set_extent_bits(&BTRFS_I(inode)->io_tree,
3316 page_start, page_end,
David Sterbaceeb0ae2016-04-26 23:54:39 +02003317 EXTENT_BOUNDARY);
Yan, Zheng0257bb82009-09-24 09:17:31 -04003318 nr++;
3319 }
Yan, Zheng0257bb82009-09-24 09:17:31 -04003320
Nikolay Borisov765f3ce2018-01-31 17:14:02 +02003321 ret = btrfs_set_extent_delalloc(inode, page_start, page_end, 0,
Nikolay Borisov330a5822019-07-17 16:18:17 +03003322 NULL);
Nikolay Borisov765f3ce2018-01-31 17:14:02 +02003323 if (ret) {
3324 unlock_page(page);
3325 put_page(page);
3326 btrfs_delalloc_release_metadata(BTRFS_I(inode),
Qu Wenruo43b18592017-12-12 15:34:32 +08003327 PAGE_SIZE, true);
Nikolay Borisov765f3ce2018-01-31 17:14:02 +02003328 btrfs_delalloc_release_extents(BTRFS_I(inode),
Qu Wenruo43b18592017-12-12 15:34:32 +08003329 PAGE_SIZE, true);
Nikolay Borisov765f3ce2018-01-31 17:14:02 +02003330
3331 clear_extent_bits(&BTRFS_I(inode)->io_tree,
3332 page_start, page_end,
3333 EXTENT_LOCKED | EXTENT_BOUNDARY);
3334 goto out;
3335
3336 }
Yan, Zheng0257bb82009-09-24 09:17:31 -04003337 set_page_dirty(page);
Yan, Zheng0257bb82009-09-24 09:17:31 -04003338
3339 unlock_extent(&BTRFS_I(inode)->io_tree,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01003340 page_start, page_end);
Yan, Zheng0257bb82009-09-24 09:17:31 -04003341 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003342 put_page(page);
Yan, Zheng0257bb82009-09-24 09:17:31 -04003343
3344 index++;
Qu Wenruo43b18592017-12-12 15:34:32 +08003345 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE,
3346 false);
Yan, Zhengefa56462010-05-16 10:49:59 -04003347 balance_dirty_pages_ratelimited(inode->i_mapping);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003348 btrfs_throttle(fs_info);
Yan, Zheng0257bb82009-09-24 09:17:31 -04003349 }
3350 WARN_ON(nr != cluster->nr);
Yan, Zhengefa56462010-05-16 10:49:59 -04003351out:
Yan, Zheng0257bb82009-09-24 09:17:31 -04003352 kfree(ra);
3353 return ret;
3354}
3355
3356static noinline_for_stack
3357int relocate_data_extent(struct inode *inode, struct btrfs_key *extent_key,
3358 struct file_extent_cluster *cluster)
3359{
3360 int ret;
3361
3362 if (cluster->nr > 0 && extent_key->objectid != cluster->end + 1) {
3363 ret = relocate_file_extent_cluster(inode, cluster);
3364 if (ret)
3365 return ret;
3366 cluster->nr = 0;
3367 }
3368
3369 if (!cluster->nr)
3370 cluster->start = extent_key->objectid;
3371 else
3372 BUG_ON(cluster->nr >= MAX_EXTENTS);
3373 cluster->end = extent_key->objectid + extent_key->offset - 1;
3374 cluster->boundary[cluster->nr] = extent_key->objectid;
3375 cluster->nr++;
3376
3377 if (cluster->nr >= MAX_EXTENTS) {
3378 ret = relocate_file_extent_cluster(inode, cluster);
3379 if (ret)
3380 return ret;
3381 cluster->nr = 0;
3382 }
3383 return 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003384}
3385
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003386/*
3387 * helper to add a tree block to the list.
3388 * the major work is getting the generation and level of the block
3389 */
3390static int add_tree_block(struct reloc_control *rc,
3391 struct btrfs_key *extent_key,
3392 struct btrfs_path *path,
3393 struct rb_root *blocks)
3394{
3395 struct extent_buffer *eb;
3396 struct btrfs_extent_item *ei;
3397 struct btrfs_tree_block_info *bi;
3398 struct tree_block *block;
3399 struct rb_node *rb_node;
3400 u32 item_size;
3401 int level = -1;
Wang Shilong7fdf4b62013-10-25 18:52:08 +08003402 u64 generation;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003403
3404 eb = path->nodes[0];
3405 item_size = btrfs_item_size_nr(eb, path->slots[0]);
3406
Josef Bacik3173a182013-03-07 14:22:04 -05003407 if (extent_key->type == BTRFS_METADATA_ITEM_KEY ||
3408 item_size >= sizeof(*ei) + sizeof(*bi)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003409 ei = btrfs_item_ptr(eb, path->slots[0],
3410 struct btrfs_extent_item);
Josef Bacik3173a182013-03-07 14:22:04 -05003411 if (extent_key->type == BTRFS_EXTENT_ITEM_KEY) {
3412 bi = (struct btrfs_tree_block_info *)(ei + 1);
3413 level = btrfs_tree_block_level(eb, bi);
3414 } else {
3415 level = (int)extent_key->offset;
3416 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003417 generation = btrfs_extent_generation(eb, ei);
David Sterba6d8ff4e2018-06-26 16:20:59 +02003418 } else if (unlikely(item_size == sizeof(struct btrfs_extent_item_v0))) {
Nikolay Borisovba3c2b12018-06-26 16:57:36 +03003419 btrfs_print_v0_err(eb->fs_info);
3420 btrfs_handle_fs_error(eb->fs_info, -EINVAL, NULL);
3421 return -EINVAL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003422 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003423 BUG();
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003424 }
3425
David Sterbab3b4aa72011-04-21 01:20:15 +02003426 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003427
3428 BUG_ON(level == -1);
3429
3430 block = kmalloc(sizeof(*block), GFP_NOFS);
3431 if (!block)
3432 return -ENOMEM;
3433
3434 block->bytenr = extent_key->objectid;
Jeff Mahoneyda170662016-06-15 09:22:56 -04003435 block->key.objectid = rc->extent_root->fs_info->nodesize;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003436 block->key.offset = generation;
3437 block->level = level;
3438 block->key_ready = 0;
3439
3440 rb_node = tree_insert(blocks, block->bytenr, &block->rb_node);
Jeff Mahoney43c04fb2011-10-03 23:22:33 -04003441 if (rb_node)
3442 backref_tree_panic(rb_node, -EEXIST, block->bytenr);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003443
3444 return 0;
3445}
3446
3447/*
3448 * helper to add tree blocks for backref of type BTRFS_SHARED_DATA_REF_KEY
3449 */
3450static int __add_tree_block(struct reloc_control *rc,
3451 u64 bytenr, u32 blocksize,
3452 struct rb_root *blocks)
3453{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003454 struct btrfs_fs_info *fs_info = rc->extent_root->fs_info;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003455 struct btrfs_path *path;
3456 struct btrfs_key key;
3457 int ret;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003458 bool skinny = btrfs_fs_incompat(fs_info, SKINNY_METADATA);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003459
David Sterba7476dfd2014-06-15 03:34:59 +02003460 if (tree_block_processed(bytenr, rc))
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003461 return 0;
3462
3463 if (tree_search(blocks, bytenr))
3464 return 0;
3465
3466 path = btrfs_alloc_path();
3467 if (!path)
3468 return -ENOMEM;
Josef Bacikaee68ee2013-06-13 13:50:23 -04003469again:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003470 key.objectid = bytenr;
Josef Bacikaee68ee2013-06-13 13:50:23 -04003471 if (skinny) {
3472 key.type = BTRFS_METADATA_ITEM_KEY;
3473 key.offset = (u64)-1;
3474 } else {
3475 key.type = BTRFS_EXTENT_ITEM_KEY;
3476 key.offset = blocksize;
3477 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003478
3479 path->search_commit_root = 1;
3480 path->skip_locking = 1;
3481 ret = btrfs_search_slot(NULL, rc->extent_root, &key, path, 0, 0);
3482 if (ret < 0)
3483 goto out;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003484
Josef Bacikaee68ee2013-06-13 13:50:23 -04003485 if (ret > 0 && skinny) {
3486 if (path->slots[0]) {
3487 path->slots[0]--;
3488 btrfs_item_key_to_cpu(path->nodes[0], &key,
3489 path->slots[0]);
3490 if (key.objectid == bytenr &&
3491 (key.type == BTRFS_METADATA_ITEM_KEY ||
3492 (key.type == BTRFS_EXTENT_ITEM_KEY &&
3493 key.offset == blocksize)))
3494 ret = 0;
3495 }
3496
3497 if (ret) {
3498 skinny = false;
3499 btrfs_release_path(path);
3500 goto again;
3501 }
Josef Bacik3173a182013-03-07 14:22:04 -05003502 }
Liu Bocdccee92017-08-18 15:15:23 -06003503 if (ret) {
3504 ASSERT(ret == 1);
3505 btrfs_print_leaf(path->nodes[0]);
3506 btrfs_err(fs_info,
3507 "tree block extent item (%llu) is not found in extent tree",
3508 bytenr);
3509 WARN_ON(1);
3510 ret = -EINVAL;
3511 goto out;
3512 }
Josef Bacik3173a182013-03-07 14:22:04 -05003513
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003514 ret = add_tree_block(rc, &key, path, blocks);
3515out:
3516 btrfs_free_path(path);
3517 return ret;
3518}
3519
3520/*
3521 * helper to check if the block use full backrefs for pointers in it
3522 */
3523static int block_use_full_backref(struct reloc_control *rc,
3524 struct extent_buffer *eb)
3525{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003526 u64 flags;
3527 int ret;
3528
3529 if (btrfs_header_flag(eb, BTRFS_HEADER_FLAG_RELOC) ||
3530 btrfs_header_backref_rev(eb) < BTRFS_MIXED_BACKREF_REV)
3531 return 1;
3532
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003533 ret = btrfs_lookup_extent_info(NULL, rc->extent_root->fs_info,
Josef Bacik3173a182013-03-07 14:22:04 -05003534 eb->start, btrfs_header_level(eb), 1,
3535 NULL, &flags);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003536 BUG_ON(ret);
3537
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003538 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)
3539 ret = 1;
3540 else
3541 ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003542 return ret;
3543}
3544
Josef Bacik0af3d002010-06-21 14:48:16 -04003545static int delete_block_group_cache(struct btrfs_fs_info *fs_info,
Chris Mason1bbc6212015-04-06 12:46:08 -07003546 struct btrfs_block_group_cache *block_group,
3547 struct inode *inode,
3548 u64 ino)
Josef Bacik0af3d002010-06-21 14:48:16 -04003549{
3550 struct btrfs_key key;
Josef Bacik0af3d002010-06-21 14:48:16 -04003551 struct btrfs_root *root = fs_info->tree_root;
3552 struct btrfs_trans_handle *trans;
Josef Bacik0af3d002010-06-21 14:48:16 -04003553 int ret = 0;
3554
3555 if (inode)
3556 goto truncate;
3557
3558 key.objectid = ino;
3559 key.type = BTRFS_INODE_ITEM_KEY;
3560 key.offset = 0;
3561
3562 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
Al Viro2e19f1f2018-07-29 23:04:45 +01003563 if (IS_ERR(inode))
Josef Bacik0af3d002010-06-21 14:48:16 -04003564 return -ENOENT;
Josef Bacik0af3d002010-06-21 14:48:16 -04003565
3566truncate:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003567 ret = btrfs_check_trunc_cache_free_space(fs_info,
Miao Xie7b61cd92013-05-13 13:55:09 +00003568 &fs_info->global_block_rsv);
3569 if (ret)
3570 goto out;
3571
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003572 trans = btrfs_join_transaction(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04003573 if (IS_ERR(trans)) {
Tsutomu Itoh3612b492011-01-25 02:51:38 +00003574 ret = PTR_ERR(trans);
Josef Bacik0af3d002010-06-21 14:48:16 -04003575 goto out;
3576 }
3577
Jeff Mahoney77ab86b2017-02-15 16:28:30 -05003578 ret = btrfs_truncate_free_space_cache(trans, block_group, inode);
Josef Bacik0af3d002010-06-21 14:48:16 -04003579
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003580 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003581 btrfs_btree_balance_dirty(fs_info);
Josef Bacik0af3d002010-06-21 14:48:16 -04003582out:
3583 iput(inode);
3584 return ret;
3585}
3586
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003587/*
3588 * helper to add tree blocks for backref of type BTRFS_EXTENT_DATA_REF_KEY
3589 * this function scans fs tree to find blocks reference the data extent
3590 */
3591static int find_data_references(struct reloc_control *rc,
3592 struct btrfs_key *extent_key,
3593 struct extent_buffer *leaf,
3594 struct btrfs_extent_data_ref *ref,
3595 struct rb_root *blocks)
3596{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003597 struct btrfs_fs_info *fs_info = rc->extent_root->fs_info;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003598 struct btrfs_path *path;
3599 struct tree_block *block;
3600 struct btrfs_root *root;
3601 struct btrfs_file_extent_item *fi;
3602 struct rb_node *rb_node;
3603 struct btrfs_key key;
3604 u64 ref_root;
3605 u64 ref_objectid;
3606 u64 ref_offset;
3607 u32 ref_count;
3608 u32 nritems;
3609 int err = 0;
3610 int added = 0;
3611 int counted;
3612 int ret;
3613
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003614 ref_root = btrfs_extent_data_ref_root(leaf, ref);
3615 ref_objectid = btrfs_extent_data_ref_objectid(leaf, ref);
3616 ref_offset = btrfs_extent_data_ref_offset(leaf, ref);
3617 ref_count = btrfs_extent_data_ref_count(leaf, ref);
3618
Josef Bacik0af3d002010-06-21 14:48:16 -04003619 /*
3620 * This is an extent belonging to the free space cache, lets just delete
3621 * it and redo the search.
3622 */
3623 if (ref_root == BTRFS_ROOT_TREE_OBJECTID) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003624 ret = delete_block_group_cache(fs_info, rc->block_group,
Josef Bacik0af3d002010-06-21 14:48:16 -04003625 NULL, ref_objectid);
3626 if (ret != -ENOENT)
3627 return ret;
3628 ret = 0;
3629 }
3630
3631 path = btrfs_alloc_path();
3632 if (!path)
3633 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +01003634 path->reada = READA_FORWARD;
Josef Bacik0af3d002010-06-21 14:48:16 -04003635
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003636 root = read_fs_root(fs_info, ref_root);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003637 if (IS_ERR(root)) {
3638 err = PTR_ERR(root);
3639 goto out;
3640 }
3641
3642 key.objectid = ref_objectid;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003643 key.type = BTRFS_EXTENT_DATA_KEY;
Yan, Zheng84850e82011-08-29 09:25:53 +08003644 if (ref_offset > ((u64)-1 << 32))
3645 key.offset = 0;
3646 else
3647 key.offset = ref_offset;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003648
3649 path->search_commit_root = 1;
3650 path->skip_locking = 1;
3651 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3652 if (ret < 0) {
3653 err = ret;
3654 goto out;
3655 }
3656
3657 leaf = path->nodes[0];
3658 nritems = btrfs_header_nritems(leaf);
3659 /*
3660 * the references in tree blocks that use full backrefs
3661 * are not counted in
3662 */
3663 if (block_use_full_backref(rc, leaf))
3664 counted = 0;
3665 else
3666 counted = 1;
3667 rb_node = tree_search(blocks, leaf->start);
3668 if (rb_node) {
3669 if (counted)
3670 added = 1;
3671 else
3672 path->slots[0] = nritems;
3673 }
3674
3675 while (ref_count > 0) {
3676 while (path->slots[0] >= nritems) {
3677 ret = btrfs_next_leaf(root, path);
3678 if (ret < 0) {
3679 err = ret;
3680 goto out;
3681 }
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303682 if (WARN_ON(ret > 0))
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003683 goto out;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003684
3685 leaf = path->nodes[0];
3686 nritems = btrfs_header_nritems(leaf);
3687 added = 0;
3688
3689 if (block_use_full_backref(rc, leaf))
3690 counted = 0;
3691 else
3692 counted = 1;
3693 rb_node = tree_search(blocks, leaf->start);
3694 if (rb_node) {
3695 if (counted)
3696 added = 1;
3697 else
3698 path->slots[0] = nritems;
3699 }
3700 }
3701
3702 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303703 if (WARN_ON(key.objectid != ref_objectid ||
3704 key.type != BTRFS_EXTENT_DATA_KEY))
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003705 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003706
3707 fi = btrfs_item_ptr(leaf, path->slots[0],
3708 struct btrfs_file_extent_item);
3709
3710 if (btrfs_file_extent_type(leaf, fi) ==
3711 BTRFS_FILE_EXTENT_INLINE)
3712 goto next;
3713
3714 if (btrfs_file_extent_disk_bytenr(leaf, fi) !=
3715 extent_key->objectid)
3716 goto next;
3717
3718 key.offset -= btrfs_file_extent_offset(leaf, fi);
3719 if (key.offset != ref_offset)
3720 goto next;
3721
3722 if (counted)
3723 ref_count--;
3724 if (added)
3725 goto next;
3726
David Sterba7476dfd2014-06-15 03:34:59 +02003727 if (!tree_block_processed(leaf->start, rc)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003728 block = kmalloc(sizeof(*block), GFP_NOFS);
3729 if (!block) {
3730 err = -ENOMEM;
3731 break;
3732 }
3733 block->bytenr = leaf->start;
3734 btrfs_item_key_to_cpu(leaf, &block->key, 0);
3735 block->level = 0;
3736 block->key_ready = 1;
3737 rb_node = tree_insert(blocks, block->bytenr,
3738 &block->rb_node);
Jeff Mahoney43c04fb2011-10-03 23:22:33 -04003739 if (rb_node)
3740 backref_tree_panic(rb_node, -EEXIST,
3741 block->bytenr);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003742 }
3743 if (counted)
3744 added = 1;
3745 else
3746 path->slots[0] = nritems;
3747next:
3748 path->slots[0]++;
3749
3750 }
3751out:
3752 btrfs_free_path(path);
3753 return err;
3754}
3755
3756/*
Liu Bo2c016dc2012-12-26 15:32:17 +08003757 * helper to find all tree blocks that reference a given data extent
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003758 */
3759static noinline_for_stack
3760int add_data_references(struct reloc_control *rc,
3761 struct btrfs_key *extent_key,
3762 struct btrfs_path *path,
3763 struct rb_root *blocks)
3764{
3765 struct btrfs_key key;
3766 struct extent_buffer *eb;
3767 struct btrfs_extent_data_ref *dref;
3768 struct btrfs_extent_inline_ref *iref;
3769 unsigned long ptr;
3770 unsigned long end;
Jeff Mahoneyda170662016-06-15 09:22:56 -04003771 u32 blocksize = rc->extent_root->fs_info->nodesize;
Filipe David Borba Manana647f63b2013-07-13 12:25:15 +01003772 int ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003773 int err = 0;
3774
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003775 eb = path->nodes[0];
3776 ptr = btrfs_item_ptr_offset(eb, path->slots[0]);
3777 end = ptr + btrfs_item_size_nr(eb, path->slots[0]);
Nikolay Borisova79865c2018-06-21 09:45:00 +03003778 ptr += sizeof(struct btrfs_extent_item);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003779
3780 while (ptr < end) {
3781 iref = (struct btrfs_extent_inline_ref *)ptr;
Liu Bo3de28d52017-08-18 15:15:19 -06003782 key.type = btrfs_get_extent_inline_ref_type(eb, iref,
3783 BTRFS_REF_TYPE_DATA);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003784 if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
3785 key.offset = btrfs_extent_inline_ref_offset(eb, iref);
3786 ret = __add_tree_block(rc, key.offset, blocksize,
3787 blocks);
3788 } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
3789 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
3790 ret = find_data_references(rc, extent_key,
3791 eb, dref, blocks);
3792 } else {
Su Yueaf431dc2018-06-22 16:18:01 +08003793 ret = -EUCLEAN;
Liu Bob14c55a2017-08-18 15:15:22 -06003794 btrfs_err(rc->extent_root->fs_info,
3795 "extent %llu slot %d has an invalid inline ref type",
3796 eb->start, path->slots[0]);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003797 }
Filipe David Borba Manana647f63b2013-07-13 12:25:15 +01003798 if (ret) {
3799 err = ret;
3800 goto out;
3801 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003802 ptr += btrfs_extent_inline_ref_size(key.type);
3803 }
3804 WARN_ON(ptr > end);
3805
3806 while (1) {
3807 cond_resched();
3808 eb = path->nodes[0];
3809 if (path->slots[0] >= btrfs_header_nritems(eb)) {
3810 ret = btrfs_next_leaf(rc->extent_root, path);
3811 if (ret < 0) {
3812 err = ret;
3813 break;
3814 }
3815 if (ret > 0)
3816 break;
3817 eb = path->nodes[0];
3818 }
3819
3820 btrfs_item_key_to_cpu(eb, &key, path->slots[0]);
3821 if (key.objectid != extent_key->objectid)
3822 break;
3823
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003824 if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003825 ret = __add_tree_block(rc, key.offset, blocksize,
3826 blocks);
3827 } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
3828 dref = btrfs_item_ptr(eb, path->slots[0],
3829 struct btrfs_extent_data_ref);
3830 ret = find_data_references(rc, extent_key,
3831 eb, dref, blocks);
David Sterba6d8ff4e2018-06-26 16:20:59 +02003832 } else if (unlikely(key.type == BTRFS_EXTENT_REF_V0_KEY)) {
Nikolay Borisovba3c2b12018-06-26 16:57:36 +03003833 btrfs_print_v0_err(eb->fs_info);
3834 btrfs_handle_fs_error(eb->fs_info, -EINVAL, NULL);
3835 ret = -EINVAL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003836 } else {
3837 ret = 0;
3838 }
3839 if (ret) {
3840 err = ret;
3841 break;
3842 }
3843 path->slots[0]++;
3844 }
Filipe David Borba Manana647f63b2013-07-13 12:25:15 +01003845out:
David Sterbab3b4aa72011-04-21 01:20:15 +02003846 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003847 if (err)
3848 free_block_list(blocks);
3849 return err;
3850}
3851
3852/*
Liu Bo2c016dc2012-12-26 15:32:17 +08003853 * helper to find next unprocessed extent
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003854 */
3855static noinline_for_stack
Zhaolei147d2562015-08-06 20:58:11 +08003856int find_next_extent(struct reloc_control *rc, struct btrfs_path *path,
Yan, Zheng3fd0a552010-05-16 10:49:59 -04003857 struct btrfs_key *extent_key)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003858{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003859 struct btrfs_fs_info *fs_info = rc->extent_root->fs_info;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003860 struct btrfs_key key;
3861 struct extent_buffer *leaf;
3862 u64 start, end, last;
3863 int ret;
3864
3865 last = rc->block_group->key.objectid + rc->block_group->key.offset;
3866 while (1) {
3867 cond_resched();
3868 if (rc->search_start >= last) {
3869 ret = 1;
3870 break;
3871 }
3872
3873 key.objectid = rc->search_start;
3874 key.type = BTRFS_EXTENT_ITEM_KEY;
3875 key.offset = 0;
3876
3877 path->search_commit_root = 1;
3878 path->skip_locking = 1;
3879 ret = btrfs_search_slot(NULL, rc->extent_root, &key, path,
3880 0, 0);
3881 if (ret < 0)
3882 break;
3883next:
3884 leaf = path->nodes[0];
3885 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
3886 ret = btrfs_next_leaf(rc->extent_root, path);
3887 if (ret != 0)
3888 break;
3889 leaf = path->nodes[0];
3890 }
3891
3892 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
3893 if (key.objectid >= last) {
3894 ret = 1;
3895 break;
3896 }
3897
Josef Bacik3173a182013-03-07 14:22:04 -05003898 if (key.type != BTRFS_EXTENT_ITEM_KEY &&
3899 key.type != BTRFS_METADATA_ITEM_KEY) {
3900 path->slots[0]++;
3901 goto next;
3902 }
3903
3904 if (key.type == BTRFS_EXTENT_ITEM_KEY &&
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003905 key.objectid + key.offset <= rc->search_start) {
3906 path->slots[0]++;
3907 goto next;
3908 }
3909
Josef Bacik3173a182013-03-07 14:22:04 -05003910 if (key.type == BTRFS_METADATA_ITEM_KEY &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003911 key.objectid + fs_info->nodesize <=
Josef Bacik3173a182013-03-07 14:22:04 -05003912 rc->search_start) {
3913 path->slots[0]++;
3914 goto next;
3915 }
3916
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003917 ret = find_first_extent_bit(&rc->processed_blocks,
3918 key.objectid, &start, &end,
Josef Bacike6138872012-09-27 17:07:30 -04003919 EXTENT_DIRTY, NULL);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003920
3921 if (ret == 0 && start <= key.objectid) {
David Sterbab3b4aa72011-04-21 01:20:15 +02003922 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003923 rc->search_start = end + 1;
3924 } else {
Josef Bacik3173a182013-03-07 14:22:04 -05003925 if (key.type == BTRFS_EXTENT_ITEM_KEY)
3926 rc->search_start = key.objectid + key.offset;
3927 else
3928 rc->search_start = key.objectid +
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003929 fs_info->nodesize;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04003930 memcpy(extent_key, &key, sizeof(key));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003931 return 0;
3932 }
3933 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003934 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003935 return ret;
3936}
3937
3938static void set_reloc_control(struct reloc_control *rc)
3939{
3940 struct btrfs_fs_info *fs_info = rc->extent_root->fs_info;
Chris Mason75857172011-06-13 20:00:16 -04003941
3942 mutex_lock(&fs_info->reloc_mutex);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003943 fs_info->reloc_ctl = rc;
Chris Mason75857172011-06-13 20:00:16 -04003944 mutex_unlock(&fs_info->reloc_mutex);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003945}
3946
3947static void unset_reloc_control(struct reloc_control *rc)
3948{
3949 struct btrfs_fs_info *fs_info = rc->extent_root->fs_info;
Chris Mason75857172011-06-13 20:00:16 -04003950
3951 mutex_lock(&fs_info->reloc_mutex);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003952 fs_info->reloc_ctl = NULL;
Chris Mason75857172011-06-13 20:00:16 -04003953 mutex_unlock(&fs_info->reloc_mutex);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003954}
3955
3956static int check_extent_flags(u64 flags)
3957{
3958 if ((flags & BTRFS_EXTENT_FLAG_DATA) &&
3959 (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK))
3960 return 1;
3961 if (!(flags & BTRFS_EXTENT_FLAG_DATA) &&
3962 !(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK))
3963 return 1;
3964 if ((flags & BTRFS_EXTENT_FLAG_DATA) &&
3965 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
3966 return 1;
3967 return 0;
3968}
3969
Yan, Zheng3fd0a552010-05-16 10:49:59 -04003970static noinline_for_stack
3971int prepare_to_relocate(struct reloc_control *rc)
3972{
3973 struct btrfs_trans_handle *trans;
Josef Bacikac2faba2016-05-27 13:08:26 -04003974 int ret;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04003975
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003976 rc->block_rsv = btrfs_alloc_block_rsv(rc->extent_root->fs_info,
Miao Xie66d8f3d2012-09-06 04:02:28 -06003977 BTRFS_BLOCK_RSV_TEMP);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04003978 if (!rc->block_rsv)
3979 return -ENOMEM;
3980
Yan, Zheng3fd0a552010-05-16 10:49:59 -04003981 memset(&rc->cluster, 0, sizeof(rc->cluster));
3982 rc->search_start = rc->block_group->key.objectid;
3983 rc->extents_found = 0;
3984 rc->nodes_relocated = 0;
3985 rc->merging_rsv_size = 0;
Wang Shilong0647bf52013-11-20 09:01:52 +08003986 rc->reserved_bytes = 0;
Jeff Mahoneyda170662016-06-15 09:22:56 -04003987 rc->block_rsv->size = rc->extent_root->fs_info->nodesize *
Wang Shilong0647bf52013-11-20 09:01:52 +08003988 RELOCATION_RESERVED_NODES;
Josef Bacikac2faba2016-05-27 13:08:26 -04003989 ret = btrfs_block_rsv_refill(rc->extent_root,
3990 rc->block_rsv, rc->block_rsv->size,
3991 BTRFS_RESERVE_FLUSH_ALL);
3992 if (ret)
3993 return ret;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04003994
3995 rc->create_reloc_tree = 1;
3996 set_reloc_control(rc);
3997
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003998 trans = btrfs_join_transaction(rc->extent_root);
Liu Bo28818942013-03-04 16:25:39 +00003999 if (IS_ERR(trans)) {
4000 unset_reloc_control(rc);
4001 /*
4002 * extent tree is not a ref_cow tree and has no reloc_root to
4003 * cleanup. And callers are responsible to free the above
4004 * block rsv.
4005 */
4006 return PTR_ERR(trans);
4007 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004008 btrfs_commit_transaction(trans);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004009 return 0;
4010}
Yan, Zheng76dda932009-09-21 16:00:26 -04004011
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004012static noinline_for_stack int relocate_block_group(struct reloc_control *rc)
4013{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004014 struct btrfs_fs_info *fs_info = rc->extent_root->fs_info;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004015 struct rb_root blocks = RB_ROOT;
4016 struct btrfs_key key;
4017 struct btrfs_trans_handle *trans = NULL;
4018 struct btrfs_path *path;
4019 struct btrfs_extent_item *ei;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004020 u64 flags;
4021 u32 item_size;
4022 int ret;
4023 int err = 0;
Chris Masonc87f08c2011-02-16 13:57:04 -05004024 int progress = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004025
4026 path = btrfs_alloc_path();
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004027 if (!path)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004028 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +01004029 path->reada = READA_FORWARD;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004030
4031 ret = prepare_to_relocate(rc);
4032 if (ret) {
4033 err = ret;
4034 goto out_free;
Jiri Slaby2423fdfb2010-01-06 16:57:22 +00004035 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004036
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004037 while (1) {
Wang Shilong0647bf52013-11-20 09:01:52 +08004038 rc->reserved_bytes = 0;
4039 ret = btrfs_block_rsv_refill(rc->extent_root,
4040 rc->block_rsv, rc->block_rsv->size,
4041 BTRFS_RESERVE_FLUSH_ALL);
4042 if (ret) {
4043 err = ret;
4044 break;
4045 }
Chris Masonc87f08c2011-02-16 13:57:04 -05004046 progress++;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004047 trans = btrfs_start_transaction(rc->extent_root, 0);
Liu Bo0f788c52013-03-04 16:25:40 +00004048 if (IS_ERR(trans)) {
4049 err = PTR_ERR(trans);
4050 trans = NULL;
4051 break;
4052 }
Chris Masonc87f08c2011-02-16 13:57:04 -05004053restart:
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004054 if (update_backref_cache(trans, &rc->backref_cache)) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004055 btrfs_end_transaction(trans);
Pan Bian42a657f2018-11-23 18:10:15 +08004056 trans = NULL;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004057 continue;
4058 }
4059
Zhaolei147d2562015-08-06 20:58:11 +08004060 ret = find_next_extent(rc, path, &key);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004061 if (ret < 0)
4062 err = ret;
4063 if (ret != 0)
4064 break;
4065
4066 rc->extents_found++;
4067
4068 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
4069 struct btrfs_extent_item);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004070 item_size = btrfs_item_size_nr(path->nodes[0], path->slots[0]);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004071 if (item_size >= sizeof(*ei)) {
4072 flags = btrfs_extent_flags(path->nodes[0], ei);
4073 ret = check_extent_flags(flags);
4074 BUG_ON(ret);
David Sterba6d8ff4e2018-06-26 16:20:59 +02004075 } else if (unlikely(item_size == sizeof(struct btrfs_extent_item_v0))) {
Nikolay Borisovba3c2b12018-06-26 16:57:36 +03004076 err = -EINVAL;
4077 btrfs_print_v0_err(trans->fs_info);
4078 btrfs_abort_transaction(trans, err);
4079 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004080 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004081 BUG();
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004082 }
4083
4084 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
4085 ret = add_tree_block(rc, &key, path, &blocks);
4086 } else if (rc->stage == UPDATE_DATA_PTRS &&
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004087 (flags & BTRFS_EXTENT_FLAG_DATA)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004088 ret = add_data_references(rc, &key, path, &blocks);
4089 } else {
David Sterbab3b4aa72011-04-21 01:20:15 +02004090 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004091 ret = 0;
4092 }
4093 if (ret < 0) {
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004094 err = ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004095 break;
4096 }
4097
4098 if (!RB_EMPTY_ROOT(&blocks)) {
4099 ret = relocate_tree_blocks(trans, rc, &blocks);
4100 if (ret < 0) {
Wang Shilong1708cc52013-12-28 19:52:39 +08004101 /*
4102 * if we fail to relocate tree blocks, force to update
4103 * backref cache when committing transaction.
4104 */
4105 rc->backref_cache.last_trans = trans->transid - 1;
4106
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004107 if (ret != -EAGAIN) {
4108 err = ret;
4109 break;
4110 }
4111 rc->extents_found--;
4112 rc->search_start = key.objectid;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004113 }
4114 }
4115
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004116 btrfs_end_transaction_throttle(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004117 btrfs_btree_balance_dirty(fs_info);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004118 trans = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004119
4120 if (rc->stage == MOVE_DATA_EXTENTS &&
4121 (flags & BTRFS_EXTENT_FLAG_DATA)) {
4122 rc->found_file_extent = 1;
Yan, Zheng0257bb82009-09-24 09:17:31 -04004123 ret = relocate_data_extent(rc->data_inode,
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004124 &key, &rc->cluster);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004125 if (ret < 0) {
4126 err = ret;
4127 break;
4128 }
4129 }
4130 }
Chris Masonc87f08c2011-02-16 13:57:04 -05004131 if (trans && progress && err == -ENOSPC) {
Nikolay Borisov43a7e992018-06-20 15:49:15 +03004132 ret = btrfs_force_chunk_alloc(trans, rc->block_group->flags);
Shilong Wang96894572015-04-12 14:35:20 +08004133 if (ret == 1) {
Chris Masonc87f08c2011-02-16 13:57:04 -05004134 err = 0;
4135 progress = 0;
4136 goto restart;
4137 }
4138 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004139
David Sterbab3b4aa72011-04-21 01:20:15 +02004140 btrfs_release_path(path);
David Sterba91166212016-04-26 23:54:39 +02004141 clear_extent_bits(&rc->processed_blocks, 0, (u64)-1, EXTENT_DIRTY);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004142
4143 if (trans) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004144 btrfs_end_transaction_throttle(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004145 btrfs_btree_balance_dirty(fs_info);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004146 }
4147
Yan, Zheng0257bb82009-09-24 09:17:31 -04004148 if (!err) {
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004149 ret = relocate_file_extent_cluster(rc->data_inode,
4150 &rc->cluster);
Yan, Zheng0257bb82009-09-24 09:17:31 -04004151 if (ret < 0)
4152 err = ret;
4153 }
4154
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004155 rc->create_reloc_tree = 0;
4156 set_reloc_control(rc);
Yan, Zheng0257bb82009-09-24 09:17:31 -04004157
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004158 backref_cache_cleanup(&rc->backref_cache);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004159 btrfs_block_rsv_release(fs_info, rc->block_rsv, (u64)-1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004160
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004161 err = prepare_to_merge(rc, err);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004162
4163 merge_reloc_roots(rc);
4164
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004165 rc->merge_reloc_tree = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004166 unset_reloc_control(rc);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004167 btrfs_block_rsv_release(fs_info, rc->block_rsv, (u64)-1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004168
4169 /* get rid of pinned extents */
Josef Bacik7a7eaa42011-04-13 12:54:33 -04004170 trans = btrfs_join_transaction(rc->extent_root);
Qu Wenruo62b99542016-08-15 10:36:51 +08004171 if (IS_ERR(trans)) {
Tsutomu Itoh3612b492011-01-25 02:51:38 +00004172 err = PTR_ERR(trans);
Qu Wenruo62b99542016-08-15 10:36:51 +08004173 goto out_free;
4174 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004175 btrfs_commit_transaction(trans);
Qu Wenruod2311e62019-01-23 15:15:14 +08004176 ret = clean_dirty_subvols(rc);
4177 if (ret < 0 && !err)
4178 err = ret;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004179out_free:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004180 btrfs_free_block_rsv(fs_info, rc->block_rsv);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004181 btrfs_free_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004182 return err;
4183}
4184
4185static int __insert_orphan_inode(struct btrfs_trans_handle *trans,
Yan, Zheng0257bb82009-09-24 09:17:31 -04004186 struct btrfs_root *root, u64 objectid)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004187{
4188 struct btrfs_path *path;
4189 struct btrfs_inode_item *item;
4190 struct extent_buffer *leaf;
4191 int ret;
4192
4193 path = btrfs_alloc_path();
4194 if (!path)
4195 return -ENOMEM;
4196
4197 ret = btrfs_insert_empty_inode(trans, root, path, objectid);
4198 if (ret)
4199 goto out;
4200
4201 leaf = path->nodes[0];
4202 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_inode_item);
David Sterbab159fa22016-11-08 18:09:03 +01004203 memzero_extent_buffer(leaf, (unsigned long)item, sizeof(*item));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004204 btrfs_set_inode_generation(leaf, item, 1);
Yan, Zheng0257bb82009-09-24 09:17:31 -04004205 btrfs_set_inode_size(leaf, item, 0);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004206 btrfs_set_inode_mode(leaf, item, S_IFREG | 0600);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004207 btrfs_set_inode_flags(leaf, item, BTRFS_INODE_NOCOMPRESS |
4208 BTRFS_INODE_PREALLOC);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004209 btrfs_mark_buffer_dirty(leaf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004210out:
4211 btrfs_free_path(path);
4212 return ret;
4213}
4214
4215/*
4216 * helper to create inode for data relocation.
4217 * the inode is in data relocation tree and its link count is 0
4218 */
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004219static noinline_for_stack
4220struct inode *create_reloc_inode(struct btrfs_fs_info *fs_info,
4221 struct btrfs_block_group_cache *group)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004222{
4223 struct inode *inode = NULL;
4224 struct btrfs_trans_handle *trans;
4225 struct btrfs_root *root;
4226 struct btrfs_key key;
Zhaolei46249002015-08-05 18:00:03 +08004227 u64 objectid;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004228 int err = 0;
4229
4230 root = read_fs_root(fs_info, BTRFS_DATA_RELOC_TREE_OBJECTID);
4231 if (IS_ERR(root))
4232 return ERR_CAST(root);
4233
Yan, Zhenga22285a2010-05-16 10:48:46 -04004234 trans = btrfs_start_transaction(root, 6);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004235 if (IS_ERR(trans))
4236 return ERR_CAST(trans);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004237
Li Zefan581bb052011-04-20 10:06:11 +08004238 err = btrfs_find_free_objectid(root, &objectid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004239 if (err)
4240 goto out;
4241
Yan, Zheng0257bb82009-09-24 09:17:31 -04004242 err = __insert_orphan_inode(trans, root, objectid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004243 BUG_ON(err);
4244
4245 key.objectid = objectid;
4246 key.type = BTRFS_INODE_ITEM_KEY;
4247 key.offset = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004248 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
Al Viro2e19f1f2018-07-29 23:04:45 +01004249 BUG_ON(IS_ERR(inode));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004250 BTRFS_I(inode)->index_cnt = group->key.objectid;
4251
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004252 err = btrfs_orphan_add(trans, BTRFS_I(inode));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004253out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004254 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004255 btrfs_btree_balance_dirty(fs_info);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004256 if (err) {
4257 if (inode)
4258 iput(inode);
4259 inode = ERR_PTR(err);
4260 }
4261 return inode;
4262}
4263
Qu Wenruoc258d6e2019-03-01 10:47:58 +08004264static struct reloc_control *alloc_reloc_control(struct btrfs_fs_info *fs_info)
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004265{
4266 struct reloc_control *rc;
4267
4268 rc = kzalloc(sizeof(*rc), GFP_NOFS);
4269 if (!rc)
4270 return NULL;
4271
4272 INIT_LIST_HEAD(&rc->reloc_roots);
Qu Wenruod2311e62019-01-23 15:15:14 +08004273 INIT_LIST_HEAD(&rc->dirty_subvol_roots);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004274 backref_cache_init(&rc->backref_cache);
4275 mapping_tree_init(&rc->reloc_root_tree);
Qu Wenruo43eb5f22019-03-01 10:47:59 +08004276 extent_io_tree_init(fs_info, &rc->processed_blocks,
4277 IO_TREE_RELOC_BLOCKS, NULL);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004278 return rc;
4279}
4280
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004281/*
Adam Borowskiebce0e02016-11-14 18:44:34 +01004282 * Print the block group being relocated
4283 */
4284static void describe_relocation(struct btrfs_fs_info *fs_info,
4285 struct btrfs_block_group_cache *block_group)
4286{
Anand Jainf89e09c2018-11-20 16:12:55 +08004287 char buf[128] = {'\0'};
Adam Borowskiebce0e02016-11-14 18:44:34 +01004288
Anand Jainf89e09c2018-11-20 16:12:55 +08004289 btrfs_describe_block_groups(block_group->flags, buf, sizeof(buf));
Adam Borowskiebce0e02016-11-14 18:44:34 +01004290
4291 btrfs_info(fs_info,
4292 "relocating block group %llu flags %s",
Anand Jainf89e09c2018-11-20 16:12:55 +08004293 block_group->key.objectid, buf);
Adam Borowskiebce0e02016-11-14 18:44:34 +01004294}
4295
4296/*
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004297 * function to relocate all extents in a block group.
4298 */
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04004299int btrfs_relocate_block_group(struct btrfs_fs_info *fs_info, u64 group_start)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004300{
Omar Sandovaleede2bf2016-11-03 10:28:12 -07004301 struct btrfs_block_group_cache *bg;
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04004302 struct btrfs_root *extent_root = fs_info->extent_root;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004303 struct reloc_control *rc;
Josef Bacik0af3d002010-06-21 14:48:16 -04004304 struct inode *inode;
4305 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004306 int ret;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004307 int rw = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004308 int err = 0;
4309
Omar Sandovaleede2bf2016-11-03 10:28:12 -07004310 bg = btrfs_lookup_block_group(fs_info, group_start);
4311 if (!bg)
4312 return -ENOENT;
4313
4314 if (btrfs_pinned_by_swapfile(fs_info, bg)) {
4315 btrfs_put_block_group(bg);
4316 return -ETXTBSY;
4317 }
4318
Qu Wenruoc258d6e2019-03-01 10:47:58 +08004319 rc = alloc_reloc_control(fs_info);
Omar Sandovaleede2bf2016-11-03 10:28:12 -07004320 if (!rc) {
4321 btrfs_put_block_group(bg);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004322 return -ENOMEM;
Omar Sandovaleede2bf2016-11-03 10:28:12 -07004323 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004324
Yan, Zhengf0486c62010-05-16 10:46:25 -04004325 rc->extent_root = extent_root;
Omar Sandovaleede2bf2016-11-03 10:28:12 -07004326 rc->block_group = bg;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004327
Nikolay Borisovc83488a2018-06-20 15:49:14 +03004328 ret = btrfs_inc_block_group_ro(rc->block_group);
Zhaolei868f4012015-08-05 16:43:27 +08004329 if (ret) {
4330 err = ret;
4331 goto out;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004332 }
Zhaolei868f4012015-08-05 16:43:27 +08004333 rw = 1;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004334
Josef Bacik0af3d002010-06-21 14:48:16 -04004335 path = btrfs_alloc_path();
4336 if (!path) {
4337 err = -ENOMEM;
4338 goto out;
4339 }
4340
David Sterba7949f332019-03-20 13:40:19 +01004341 inode = lookup_free_space_inode(rc->block_group, path);
Josef Bacik0af3d002010-06-21 14:48:16 -04004342 btrfs_free_path(path);
4343
4344 if (!IS_ERR(inode))
Chris Mason1bbc6212015-04-06 12:46:08 -07004345 ret = delete_block_group_cache(fs_info, rc->block_group, inode, 0);
Josef Bacik0af3d002010-06-21 14:48:16 -04004346 else
4347 ret = PTR_ERR(inode);
4348
4349 if (ret && ret != -ENOENT) {
4350 err = ret;
4351 goto out;
4352 }
4353
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004354 rc->data_inode = create_reloc_inode(fs_info, rc->block_group);
4355 if (IS_ERR(rc->data_inode)) {
4356 err = PTR_ERR(rc->data_inode);
4357 rc->data_inode = NULL;
4358 goto out;
4359 }
4360
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004361 describe_relocation(fs_info, rc->block_group);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004362
Filipe Manana9cfa3e32016-04-26 15:39:32 +01004363 btrfs_wait_block_group_reservations(rc->block_group);
Filipe Mananaf78c4362016-05-09 13:15:41 +01004364 btrfs_wait_nocow_writers(rc->block_group);
Chris Mason6374e57a2017-06-23 09:48:21 -07004365 btrfs_wait_ordered_roots(fs_info, U64_MAX,
Filipe Manana578def72016-04-26 15:36:38 +01004366 rc->block_group->key.objectid,
4367 rc->block_group->key.offset);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004368
4369 while (1) {
Yan, Zheng76dda932009-09-21 16:00:26 -04004370 mutex_lock(&fs_info->cleaner_mutex);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004371 ret = relocate_block_group(rc);
Yan, Zheng76dda932009-09-21 16:00:26 -04004372 mutex_unlock(&fs_info->cleaner_mutex);
Josef Bacikff612ba2019-02-25 11:14:45 -05004373 if (ret < 0)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004374 err = ret;
Josef Bacikff612ba2019-02-25 11:14:45 -05004375
4376 /*
4377 * We may have gotten ENOSPC after we already dirtied some
4378 * extents. If writeout happens while we're relocating a
4379 * different block group we could end up hitting the
4380 * BUG_ON(rc->stage == UPDATE_DATA_PTRS) in
4381 * btrfs_reloc_cow_block. Make sure we write everything out
4382 * properly so we don't trip over this problem, and then break
4383 * out of the loop if we hit an error.
4384 */
4385 if (rc->stage == MOVE_DATA_EXTENTS && rc->found_file_extent) {
4386 ret = btrfs_wait_ordered_range(rc->data_inode, 0,
4387 (u64)-1);
4388 if (ret)
4389 err = ret;
4390 invalidate_mapping_pages(rc->data_inode->i_mapping,
4391 0, -1);
4392 rc->stage = UPDATE_DATA_PTRS;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004393 }
4394
Josef Bacikff612ba2019-02-25 11:14:45 -05004395 if (err < 0)
4396 goto out;
4397
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004398 if (rc->extents_found == 0)
4399 break;
4400
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004401 btrfs_info(fs_info, "found %llu extents", rc->extents_found);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004402
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004403 }
4404
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004405 WARN_ON(rc->block_group->pinned > 0);
4406 WARN_ON(rc->block_group->reserved > 0);
4407 WARN_ON(btrfs_block_group_used(&rc->block_group->item) > 0);
4408out:
Yan, Zhengf0486c62010-05-16 10:46:25 -04004409 if (err && rw)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004410 btrfs_dec_block_group_ro(rc->block_group);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004411 iput(rc->data_inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004412 btrfs_put_block_group(rc->block_group);
4413 kfree(rc);
4414 return err;
4415}
4416
Yan, Zheng76dda932009-09-21 16:00:26 -04004417static noinline_for_stack int mark_garbage_root(struct btrfs_root *root)
4418{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004419 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng76dda932009-09-21 16:00:26 -04004420 struct btrfs_trans_handle *trans;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004421 int ret, err;
Yan, Zheng76dda932009-09-21 16:00:26 -04004422
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004423 trans = btrfs_start_transaction(fs_info->tree_root, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004424 if (IS_ERR(trans))
4425 return PTR_ERR(trans);
Yan, Zheng76dda932009-09-21 16:00:26 -04004426
4427 memset(&root->root_item.drop_progress, 0,
4428 sizeof(root->root_item.drop_progress));
4429 root->root_item.drop_level = 0;
4430 btrfs_set_root_refs(&root->root_item, 0);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004431 ret = btrfs_update_root(trans, fs_info->tree_root,
Yan, Zheng76dda932009-09-21 16:00:26 -04004432 &root->root_key, &root->root_item);
Yan, Zheng76dda932009-09-21 16:00:26 -04004433
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004434 err = btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004435 if (err)
4436 return err;
4437 return ret;
Yan, Zheng76dda932009-09-21 16:00:26 -04004438}
4439
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004440/*
4441 * recover relocation interrupted by system crash.
4442 *
4443 * this function resumes merging reloc trees with corresponding fs trees.
4444 * this is important for keeping the sharing of tree blocks
4445 */
4446int btrfs_recover_relocation(struct btrfs_root *root)
4447{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004448 struct btrfs_fs_info *fs_info = root->fs_info;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004449 LIST_HEAD(reloc_roots);
4450 struct btrfs_key key;
4451 struct btrfs_root *fs_root;
4452 struct btrfs_root *reloc_root;
4453 struct btrfs_path *path;
4454 struct extent_buffer *leaf;
4455 struct reloc_control *rc = NULL;
4456 struct btrfs_trans_handle *trans;
4457 int ret;
4458 int err = 0;
4459
4460 path = btrfs_alloc_path();
4461 if (!path)
4462 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +01004463 path->reada = READA_BACK;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004464
4465 key.objectid = BTRFS_TREE_RELOC_OBJECTID;
4466 key.type = BTRFS_ROOT_ITEM_KEY;
4467 key.offset = (u64)-1;
4468
4469 while (1) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004470 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004471 path, 0, 0);
4472 if (ret < 0) {
4473 err = ret;
4474 goto out;
4475 }
4476 if (ret > 0) {
4477 if (path->slots[0] == 0)
4478 break;
4479 path->slots[0]--;
4480 }
4481 leaf = path->nodes[0];
4482 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02004483 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004484
4485 if (key.objectid != BTRFS_TREE_RELOC_OBJECTID ||
4486 key.type != BTRFS_ROOT_ITEM_KEY)
4487 break;
4488
Miao Xiecb517ea2013-05-15 07:48:19 +00004489 reloc_root = btrfs_read_fs_root(root, &key);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004490 if (IS_ERR(reloc_root)) {
4491 err = PTR_ERR(reloc_root);
4492 goto out;
4493 }
4494
4495 list_add(&reloc_root->root_list, &reloc_roots);
4496
4497 if (btrfs_root_refs(&reloc_root->root_item) > 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004498 fs_root = read_fs_root(fs_info,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004499 reloc_root->root_key.offset);
4500 if (IS_ERR(fs_root)) {
Yan, Zheng76dda932009-09-21 16:00:26 -04004501 ret = PTR_ERR(fs_root);
4502 if (ret != -ENOENT) {
4503 err = ret;
4504 goto out;
4505 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004506 ret = mark_garbage_root(reloc_root);
4507 if (ret < 0) {
4508 err = ret;
4509 goto out;
4510 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004511 }
4512 }
4513
4514 if (key.offset == 0)
4515 break;
4516
4517 key.offset--;
4518 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004519 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004520
4521 if (list_empty(&reloc_roots))
4522 goto out;
4523
Qu Wenruoc258d6e2019-03-01 10:47:58 +08004524 rc = alloc_reloc_control(fs_info);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004525 if (!rc) {
4526 err = -ENOMEM;
4527 goto out;
4528 }
4529
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004530 rc->extent_root = fs_info->extent_root;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004531
4532 set_reloc_control(rc);
4533
Josef Bacik7a7eaa42011-04-13 12:54:33 -04004534 trans = btrfs_join_transaction(rc->extent_root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00004535 if (IS_ERR(trans)) {
4536 unset_reloc_control(rc);
4537 err = PTR_ERR(trans);
4538 goto out_free;
4539 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004540
4541 rc->merge_reloc_tree = 1;
4542
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004543 while (!list_empty(&reloc_roots)) {
4544 reloc_root = list_entry(reloc_roots.next,
4545 struct btrfs_root, root_list);
4546 list_del(&reloc_root->root_list);
4547
4548 if (btrfs_root_refs(&reloc_root->root_item) == 0) {
4549 list_add_tail(&reloc_root->root_list,
4550 &rc->reloc_roots);
4551 continue;
4552 }
4553
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004554 fs_root = read_fs_root(fs_info, reloc_root->root_key.offset);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004555 if (IS_ERR(fs_root)) {
4556 err = PTR_ERR(fs_root);
4557 goto out_free;
4558 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004559
Jeff Mahoneyffd7b332011-10-03 23:23:15 -04004560 err = __add_reloc_root(reloc_root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004561 BUG_ON(err < 0); /* -ENOMEM or logic error */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004562 fs_root->reloc_root = reloc_root;
4563 }
4564
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004565 err = btrfs_commit_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004566 if (err)
4567 goto out_free;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004568
4569 merge_reloc_roots(rc);
4570
4571 unset_reloc_control(rc);
4572
Josef Bacik7a7eaa42011-04-13 12:54:33 -04004573 trans = btrfs_join_transaction(rc->extent_root);
Qu Wenruo62b99542016-08-15 10:36:51 +08004574 if (IS_ERR(trans)) {
Tsutomu Itoh3612b492011-01-25 02:51:38 +00004575 err = PTR_ERR(trans);
Qu Wenruo62b99542016-08-15 10:36:51 +08004576 goto out_free;
4577 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004578 err = btrfs_commit_transaction(trans);
Qu Wenruod2311e62019-01-23 15:15:14 +08004579
4580 ret = clean_dirty_subvols(rc);
4581 if (ret < 0 && !err)
4582 err = ret;
Tsutomu Itoh3612b492011-01-25 02:51:38 +00004583out_free:
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004584 kfree(rc);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00004585out:
Liu Boaca1bba2013-03-04 16:25:37 +00004586 if (!list_empty(&reloc_roots))
4587 free_reloc_roots(&reloc_roots);
4588
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004589 btrfs_free_path(path);
4590
4591 if (err == 0) {
4592 /* cleanup orphan inode in data relocation tree */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004593 fs_root = read_fs_root(fs_info, BTRFS_DATA_RELOC_TREE_OBJECTID);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004594 if (IS_ERR(fs_root))
4595 err = PTR_ERR(fs_root);
Miao Xied7ce5842010-02-02 08:46:44 +00004596 else
Josef Bacik66b4ffd2011-01-31 16:22:42 -05004597 err = btrfs_orphan_cleanup(fs_root);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004598 }
4599 return err;
4600}
4601
4602/*
4603 * helper to add ordered checksum for data relocation.
4604 *
4605 * cloning checksum properly handles the nodatasum extents.
4606 * it also saves CPU time to re-calculate the checksum.
4607 */
4608int btrfs_reloc_clone_csums(struct inode *inode, u64 file_pos, u64 len)
4609{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004610 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004611 struct btrfs_ordered_sum *sums;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004612 struct btrfs_ordered_extent *ordered;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004613 int ret;
4614 u64 disk_bytenr;
Josef Bacik4577b012013-09-27 09:33:09 -04004615 u64 new_bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004616 LIST_HEAD(list);
4617
4618 ordered = btrfs_lookup_ordered_extent(inode, file_pos);
4619 BUG_ON(ordered->file_offset != file_pos || ordered->len != len);
4620
4621 disk_bytenr = file_pos + BTRFS_I(inode)->index_cnt;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004622 ret = btrfs_lookup_csums_range(fs_info->csum_root, disk_bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01004623 disk_bytenr + len - 1, &list, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004624 if (ret)
4625 goto out;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004626
4627 while (!list_empty(&list)) {
4628 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
4629 list_del_init(&sums->list);
4630
Josef Bacik4577b012013-09-27 09:33:09 -04004631 /*
4632 * We need to offset the new_bytenr based on where the csum is.
4633 * We need to do this because we will read in entire prealloc
4634 * extents but we may have written to say the middle of the
4635 * prealloc extent, so we need to make sure the csum goes with
4636 * the right disk offset.
4637 *
4638 * We can do this because the data reloc inode refers strictly
4639 * to the on disk bytes, so we don't have to worry about
4640 * disk_len vs real len like with real inodes since it's all
4641 * disk length.
4642 */
4643 new_bytenr = ordered->start + (sums->bytenr - disk_bytenr);
4644 sums->bytenr = new_bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004645
Nikolay Borisovf9756262019-04-10 16:16:11 +03004646 btrfs_add_ordered_sum(ordered, sums);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004647 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004648out:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004649 btrfs_put_ordered_extent(ordered);
Andi Kleen411fc6b2010-10-29 15:14:31 -04004650 return ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004651}
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004652
Josef Bacik83d4cfd2013-08-30 15:09:51 -04004653int btrfs_reloc_cow_block(struct btrfs_trans_handle *trans,
4654 struct btrfs_root *root, struct extent_buffer *buf,
4655 struct extent_buffer *cow)
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004656{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004657 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004658 struct reloc_control *rc;
4659 struct backref_node *node;
4660 int first_cow = 0;
4661 int level;
Josef Bacik83d4cfd2013-08-30 15:09:51 -04004662 int ret = 0;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004663
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004664 rc = fs_info->reloc_ctl;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004665 if (!rc)
Josef Bacik83d4cfd2013-08-30 15:09:51 -04004666 return 0;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004667
4668 BUG_ON(rc->stage == UPDATE_DATA_PTRS &&
4669 root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID);
4670
Wang Shilongc974c462013-12-11 19:29:51 +08004671 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
4672 if (buf == root->node)
4673 __update_reloc_root(root, cow->start);
4674 }
4675
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004676 level = btrfs_header_level(buf);
4677 if (btrfs_header_generation(buf) <=
4678 btrfs_root_last_snapshot(&root->root_item))
4679 first_cow = 1;
4680
4681 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID &&
4682 rc->create_reloc_tree) {
4683 WARN_ON(!first_cow && level == 0);
4684
4685 node = rc->backref_cache.path[level];
4686 BUG_ON(node->bytenr != buf->start &&
4687 node->new_bytenr != buf->start);
4688
4689 drop_node_buffer(node);
4690 extent_buffer_get(cow);
4691 node->eb = cow;
4692 node->new_bytenr = cow->start;
4693
4694 if (!node->pending) {
4695 list_move_tail(&node->list,
4696 &rc->backref_cache.pending[level]);
4697 node->pending = 1;
4698 }
4699
4700 if (first_cow)
4701 __mark_block_processed(rc, node);
4702
4703 if (first_cow && level > 0)
4704 rc->nodes_relocated += buf->len;
4705 }
4706
Josef Bacik83d4cfd2013-08-30 15:09:51 -04004707 if (level == 0 && first_cow && rc->stage == UPDATE_DATA_PTRS)
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004708 ret = replace_file_extents(trans, rc, root, cow);
Josef Bacik83d4cfd2013-08-30 15:09:51 -04004709 return ret;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004710}
4711
4712/*
4713 * called before creating snapshot. it calculates metadata reservation
Nicholas D Steeves01327612016-05-19 21:18:45 -04004714 * required for relocating tree blocks in the snapshot
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004715 */
Zhaolei147d2562015-08-06 20:58:11 +08004716void btrfs_reloc_pre_snapshot(struct btrfs_pending_snapshot *pending,
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004717 u64 *bytes_to_reserve)
4718{
Qu Wenruo10995c02019-03-18 10:48:19 +08004719 struct btrfs_root *root = pending->root;
4720 struct reloc_control *rc = root->fs_info->reloc_ctl;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004721
Qu Wenruo10995c02019-03-18 10:48:19 +08004722 if (!root->reloc_root || !rc)
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004723 return;
4724
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004725 if (!rc->merge_reloc_tree)
4726 return;
4727
4728 root = root->reloc_root;
4729 BUG_ON(btrfs_root_refs(&root->root_item) == 0);
4730 /*
4731 * relocation is in the stage of merging trees. the space
4732 * used by merging a reloc tree is twice the size of
4733 * relocated tree nodes in the worst case. half for cowing
4734 * the reloc tree, half for cowing the fs tree. the space
4735 * used by cowing the reloc tree will be freed after the
4736 * tree is dropped. if we create snapshot, cowing the fs
4737 * tree may use more space than it frees. so we need
4738 * reserve extra space.
4739 */
4740 *bytes_to_reserve += rc->nodes_relocated;
4741}
4742
4743/*
4744 * called after snapshot is created. migrate block reservation
4745 * and create reloc root for the newly created snapshot
4746 */
Jeff Mahoney49b25e02012-03-01 17:24:58 +01004747int btrfs_reloc_post_snapshot(struct btrfs_trans_handle *trans,
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004748 struct btrfs_pending_snapshot *pending)
4749{
4750 struct btrfs_root *root = pending->root;
4751 struct btrfs_root *reloc_root;
4752 struct btrfs_root *new_root;
Qu Wenruo10995c02019-03-18 10:48:19 +08004753 struct reloc_control *rc = root->fs_info->reloc_ctl;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004754 int ret;
4755
Qu Wenruo10995c02019-03-18 10:48:19 +08004756 if (!root->reloc_root || !rc)
Jeff Mahoney49b25e02012-03-01 17:24:58 +01004757 return 0;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004758
4759 rc = root->fs_info->reloc_ctl;
4760 rc->merging_rsv_size += rc->nodes_relocated;
4761
4762 if (rc->merge_reloc_tree) {
4763 ret = btrfs_block_rsv_migrate(&pending->block_rsv,
4764 rc->block_rsv,
Lu Fengqi3a584172018-08-04 21:10:55 +08004765 rc->nodes_relocated, true);
Jeff Mahoney49b25e02012-03-01 17:24:58 +01004766 if (ret)
4767 return ret;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004768 }
4769
4770 new_root = pending->snap;
4771 reloc_root = create_reloc_root(trans, root->reloc_root,
4772 new_root->root_key.objectid);
Jeff Mahoney49b25e02012-03-01 17:24:58 +01004773 if (IS_ERR(reloc_root))
4774 return PTR_ERR(reloc_root);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004775
Jeff Mahoneyffd7b332011-10-03 23:23:15 -04004776 ret = __add_reloc_root(reloc_root);
4777 BUG_ON(ret < 0);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004778 new_root->reloc_root = reloc_root;
4779
Jeff Mahoney49b25e02012-03-01 17:24:58 +01004780 if (rc->create_reloc_tree)
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004781 ret = clone_backref_node(trans, rc, root, reloc_root);
Jeff Mahoney49b25e02012-03-01 17:24:58 +01004782 return ret;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004783}