blob: 3c0973a6857257814cbfb8597d291d41d7936bba [file] [log] [blame]
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001/*
2 * Copyright (C) 2009 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
19#include <linux/sched.h>
20#include <linux/pagemap.h>
21#include <linux/writeback.h>
22#include <linux/blkdev.h>
23#include <linux/rbtree.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090024#include <linux/slab.h>
Yan Zheng5d4f98a2009-06-10 10:45:14 -040025#include "ctree.h"
26#include "disk-io.h"
27#include "transaction.h"
28#include "volumes.h"
29#include "locking.h"
30#include "btrfs_inode.h"
31#include "async-thread.h"
Josef Bacik0af3d002010-06-21 14:48:16 -040032#include "free-space-cache.h"
Li Zefan581bb052011-04-20 10:06:11 +080033#include "inode-map.h"
Qu Wenruo62b99542016-08-15 10:36:51 +080034#include "qgroup.h"
Yan Zheng5d4f98a2009-06-10 10:45:14 -040035
36/*
37 * backref_node, mapping_node and tree_block start with this
38 */
39struct tree_entry {
40 struct rb_node rb_node;
41 u64 bytenr;
42};
43
44/*
45 * present a tree block in the backref cache
46 */
47struct backref_node {
48 struct rb_node rb_node;
49 u64 bytenr;
Yan, Zheng3fd0a552010-05-16 10:49:59 -040050
51 u64 new_bytenr;
52 /* objectid of tree block owner, can be not uptodate */
Yan Zheng5d4f98a2009-06-10 10:45:14 -040053 u64 owner;
Yan, Zheng3fd0a552010-05-16 10:49:59 -040054 /* link to pending, changed or detached list */
55 struct list_head list;
Yan Zheng5d4f98a2009-06-10 10:45:14 -040056 /* list of upper level blocks reference this block */
57 struct list_head upper;
58 /* list of child blocks in the cache */
59 struct list_head lower;
60 /* NULL if this node is not tree root */
61 struct btrfs_root *root;
62 /* extent buffer got by COW the block */
63 struct extent_buffer *eb;
64 /* level of tree block */
65 unsigned int level:8;
Yan, Zheng3fd0a552010-05-16 10:49:59 -040066 /* is the block in non-reference counted tree */
67 unsigned int cowonly:1;
68 /* 1 if no child node in the cache */
Yan Zheng5d4f98a2009-06-10 10:45:14 -040069 unsigned int lowest:1;
70 /* is the extent buffer locked */
71 unsigned int locked:1;
72 /* has the block been processed */
73 unsigned int processed:1;
74 /* have backrefs of this block been checked */
75 unsigned int checked:1;
Yan, Zheng3fd0a552010-05-16 10:49:59 -040076 /*
77 * 1 if corresponding block has been cowed but some upper
78 * level block pointers may not point to the new location
79 */
80 unsigned int pending:1;
81 /*
82 * 1 if the backref node isn't connected to any other
83 * backref node.
84 */
85 unsigned int detached:1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -040086};
87
88/*
89 * present a block pointer in the backref cache
90 */
91struct backref_edge {
92 struct list_head list[2];
93 struct backref_node *node[2];
Yan Zheng5d4f98a2009-06-10 10:45:14 -040094};
95
96#define LOWER 0
97#define UPPER 1
Wang Shilong0647bf52013-11-20 09:01:52 +080098#define RELOCATION_RESERVED_NODES 256
Yan Zheng5d4f98a2009-06-10 10:45:14 -040099
100struct backref_cache {
101 /* red black tree of all backref nodes in the cache */
102 struct rb_root rb_root;
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400103 /* for passing backref nodes to btrfs_reloc_cow_block */
104 struct backref_node *path[BTRFS_MAX_LEVEL];
105 /*
106 * list of blocks that have been cowed but some block
107 * pointers in upper level blocks may not reflect the
108 * new location
109 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400110 struct list_head pending[BTRFS_MAX_LEVEL];
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400111 /* list of backref nodes with no child node */
112 struct list_head leaves;
113 /* list of blocks that have been cowed in current transaction */
114 struct list_head changed;
115 /* list of detached backref node. */
116 struct list_head detached;
117
118 u64 last_trans;
119
120 int nr_nodes;
121 int nr_edges;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400122};
123
124/*
125 * map address of tree root to tree
126 */
127struct mapping_node {
128 struct rb_node rb_node;
129 u64 bytenr;
130 void *data;
131};
132
133struct mapping_tree {
134 struct rb_root rb_root;
135 spinlock_t lock;
136};
137
138/*
139 * present a tree block to process
140 */
141struct tree_block {
142 struct rb_node rb_node;
143 u64 bytenr;
144 struct btrfs_key key;
145 unsigned int level:8;
146 unsigned int key_ready:1;
147};
148
Yan, Zheng0257bb82009-09-24 09:17:31 -0400149#define MAX_EXTENTS 128
150
151struct file_extent_cluster {
152 u64 start;
153 u64 end;
154 u64 boundary[MAX_EXTENTS];
155 unsigned int nr;
156};
157
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400158struct reloc_control {
159 /* block group to relocate */
160 struct btrfs_block_group_cache *block_group;
161 /* extent tree */
162 struct btrfs_root *extent_root;
163 /* inode for moving data */
164 struct inode *data_inode;
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400165
166 struct btrfs_block_rsv *block_rsv;
167
168 struct backref_cache backref_cache;
169
170 struct file_extent_cluster cluster;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400171 /* tree blocks have been processed */
172 struct extent_io_tree processed_blocks;
173 /* map start of tree root to corresponding reloc tree */
174 struct mapping_tree reloc_root_tree;
175 /* list of reloc trees */
176 struct list_head reloc_roots;
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400177 /* size of metadata reservation for merging reloc trees */
178 u64 merging_rsv_size;
179 /* size of relocated tree nodes */
180 u64 nodes_relocated;
Wang Shilong0647bf52013-11-20 09:01:52 +0800181 /* reserved size for block group relocation*/
182 u64 reserved_bytes;
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400183
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400184 u64 search_start;
185 u64 extents_found;
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400186
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400187 unsigned int stage:8;
188 unsigned int create_reloc_tree:1;
189 unsigned int merge_reloc_tree:1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400190 unsigned int found_file_extent:1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400191};
192
193/* stages of data relocation */
194#define MOVE_DATA_EXTENTS 0
195#define UPDATE_DATA_PTRS 1
196
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400197static void remove_backref_node(struct backref_cache *cache,
198 struct backref_node *node);
199static void __mark_block_processed(struct reloc_control *rc,
200 struct backref_node *node);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400201
202static void mapping_tree_init(struct mapping_tree *tree)
203{
Eric Paris6bef4d32010-02-23 19:43:04 +0000204 tree->rb_root = RB_ROOT;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400205 spin_lock_init(&tree->lock);
206}
207
208static void backref_cache_init(struct backref_cache *cache)
209{
210 int i;
Eric Paris6bef4d32010-02-23 19:43:04 +0000211 cache->rb_root = RB_ROOT;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400212 for (i = 0; i < BTRFS_MAX_LEVEL; i++)
213 INIT_LIST_HEAD(&cache->pending[i]);
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400214 INIT_LIST_HEAD(&cache->changed);
215 INIT_LIST_HEAD(&cache->detached);
216 INIT_LIST_HEAD(&cache->leaves);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400217}
218
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400219static void backref_cache_cleanup(struct backref_cache *cache)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400220{
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400221 struct backref_node *node;
222 int i;
223
224 while (!list_empty(&cache->detached)) {
225 node = list_entry(cache->detached.next,
226 struct backref_node, list);
227 remove_backref_node(cache, node);
228 }
229
230 while (!list_empty(&cache->leaves)) {
231 node = list_entry(cache->leaves.next,
232 struct backref_node, lower);
233 remove_backref_node(cache, node);
234 }
235
236 cache->last_trans = 0;
237
238 for (i = 0; i < BTRFS_MAX_LEVEL; i++)
Liu Bof4907092016-07-11 18:52:57 -0700239 ASSERT(list_empty(&cache->pending[i]));
240 ASSERT(list_empty(&cache->changed));
241 ASSERT(list_empty(&cache->detached));
242 ASSERT(RB_EMPTY_ROOT(&cache->rb_root));
243 ASSERT(!cache->nr_nodes);
244 ASSERT(!cache->nr_edges);
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400245}
246
247static struct backref_node *alloc_backref_node(struct backref_cache *cache)
248{
249 struct backref_node *node;
250
251 node = kzalloc(sizeof(*node), GFP_NOFS);
252 if (node) {
253 INIT_LIST_HEAD(&node->list);
254 INIT_LIST_HEAD(&node->upper);
255 INIT_LIST_HEAD(&node->lower);
256 RB_CLEAR_NODE(&node->rb_node);
257 cache->nr_nodes++;
258 }
259 return node;
260}
261
262static void free_backref_node(struct backref_cache *cache,
263 struct backref_node *node)
264{
265 if (node) {
266 cache->nr_nodes--;
267 kfree(node);
268 }
269}
270
271static struct backref_edge *alloc_backref_edge(struct backref_cache *cache)
272{
273 struct backref_edge *edge;
274
275 edge = kzalloc(sizeof(*edge), GFP_NOFS);
276 if (edge)
277 cache->nr_edges++;
278 return edge;
279}
280
281static void free_backref_edge(struct backref_cache *cache,
282 struct backref_edge *edge)
283{
284 if (edge) {
285 cache->nr_edges--;
286 kfree(edge);
287 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400288}
289
290static struct rb_node *tree_insert(struct rb_root *root, u64 bytenr,
291 struct rb_node *node)
292{
293 struct rb_node **p = &root->rb_node;
294 struct rb_node *parent = NULL;
295 struct tree_entry *entry;
296
297 while (*p) {
298 parent = *p;
299 entry = rb_entry(parent, struct tree_entry, rb_node);
300
301 if (bytenr < entry->bytenr)
302 p = &(*p)->rb_left;
303 else if (bytenr > entry->bytenr)
304 p = &(*p)->rb_right;
305 else
306 return parent;
307 }
308
309 rb_link_node(node, parent, p);
310 rb_insert_color(node, root);
311 return NULL;
312}
313
314static struct rb_node *tree_search(struct rb_root *root, u64 bytenr)
315{
316 struct rb_node *n = root->rb_node;
317 struct tree_entry *entry;
318
319 while (n) {
320 entry = rb_entry(n, struct tree_entry, rb_node);
321
322 if (bytenr < entry->bytenr)
323 n = n->rb_left;
324 else if (bytenr > entry->bytenr)
325 n = n->rb_right;
326 else
327 return n;
328 }
329 return NULL;
330}
331
Eric Sandeen48a3b632013-04-25 20:41:01 +0000332static void backref_tree_panic(struct rb_node *rb_node, int errno, u64 bytenr)
Jeff Mahoney43c04fb2011-10-03 23:22:33 -0400333{
334
335 struct btrfs_fs_info *fs_info = NULL;
336 struct backref_node *bnode = rb_entry(rb_node, struct backref_node,
337 rb_node);
338 if (bnode->root)
339 fs_info = bnode->root->fs_info;
340 btrfs_panic(fs_info, errno, "Inconsistency in backref cache "
David Sterba351fd352014-05-15 16:48:20 +0200341 "found at offset %llu", bytenr);
Jeff Mahoney43c04fb2011-10-03 23:22:33 -0400342}
343
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400344/*
345 * walk up backref nodes until reach node presents tree root
346 */
347static struct backref_node *walk_up_backref(struct backref_node *node,
348 struct backref_edge *edges[],
349 int *index)
350{
351 struct backref_edge *edge;
352 int idx = *index;
353
354 while (!list_empty(&node->upper)) {
355 edge = list_entry(node->upper.next,
356 struct backref_edge, list[LOWER]);
357 edges[idx++] = edge;
358 node = edge->node[UPPER];
359 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400360 BUG_ON(node->detached);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400361 *index = idx;
362 return node;
363}
364
365/*
366 * walk down backref nodes to find start of next reference path
367 */
368static struct backref_node *walk_down_backref(struct backref_edge *edges[],
369 int *index)
370{
371 struct backref_edge *edge;
372 struct backref_node *lower;
373 int idx = *index;
374
375 while (idx > 0) {
376 edge = edges[idx - 1];
377 lower = edge->node[LOWER];
378 if (list_is_last(&edge->list[LOWER], &lower->upper)) {
379 idx--;
380 continue;
381 }
382 edge = list_entry(edge->list[LOWER].next,
383 struct backref_edge, list[LOWER]);
384 edges[idx - 1] = edge;
385 *index = idx;
386 return edge->node[UPPER];
387 }
388 *index = 0;
389 return NULL;
390}
391
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400392static void unlock_node_buffer(struct backref_node *node)
393{
394 if (node->locked) {
395 btrfs_tree_unlock(node->eb);
396 node->locked = 0;
397 }
398}
399
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400400static void drop_node_buffer(struct backref_node *node)
401{
402 if (node->eb) {
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400403 unlock_node_buffer(node);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400404 free_extent_buffer(node->eb);
405 node->eb = NULL;
406 }
407}
408
409static void drop_backref_node(struct backref_cache *tree,
410 struct backref_node *node)
411{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400412 BUG_ON(!list_empty(&node->upper));
413
414 drop_node_buffer(node);
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400415 list_del(&node->list);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400416 list_del(&node->lower);
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400417 if (!RB_EMPTY_NODE(&node->rb_node))
418 rb_erase(&node->rb_node, &tree->rb_root);
419 free_backref_node(tree, node);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400420}
421
422/*
423 * remove a backref node from the backref cache
424 */
425static void remove_backref_node(struct backref_cache *cache,
426 struct backref_node *node)
427{
428 struct backref_node *upper;
429 struct backref_edge *edge;
430
431 if (!node)
432 return;
433
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400434 BUG_ON(!node->lowest && !node->detached);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400435 while (!list_empty(&node->upper)) {
436 edge = list_entry(node->upper.next, struct backref_edge,
437 list[LOWER]);
438 upper = edge->node[UPPER];
439 list_del(&edge->list[LOWER]);
440 list_del(&edge->list[UPPER]);
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400441 free_backref_edge(cache, edge);
442
443 if (RB_EMPTY_NODE(&upper->rb_node)) {
444 BUG_ON(!list_empty(&node->upper));
445 drop_backref_node(cache, node);
446 node = upper;
447 node->lowest = 1;
448 continue;
449 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400450 /*
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400451 * add the node to leaf node list if no other
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400452 * child block cached.
453 */
454 if (list_empty(&upper->lower)) {
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400455 list_add_tail(&upper->lower, &cache->leaves);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400456 upper->lowest = 1;
457 }
458 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400459
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400460 drop_backref_node(cache, node);
461}
462
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400463static void update_backref_node(struct backref_cache *cache,
464 struct backref_node *node, u64 bytenr)
465{
466 struct rb_node *rb_node;
467 rb_erase(&node->rb_node, &cache->rb_root);
468 node->bytenr = bytenr;
469 rb_node = tree_insert(&cache->rb_root, node->bytenr, &node->rb_node);
Jeff Mahoney43c04fb2011-10-03 23:22:33 -0400470 if (rb_node)
471 backref_tree_panic(rb_node, -EEXIST, bytenr);
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400472}
473
474/*
475 * update backref cache after a transaction commit
476 */
477static int update_backref_cache(struct btrfs_trans_handle *trans,
478 struct backref_cache *cache)
479{
480 struct backref_node *node;
481 int level = 0;
482
483 if (cache->last_trans == 0) {
484 cache->last_trans = trans->transid;
485 return 0;
486 }
487
488 if (cache->last_trans == trans->transid)
489 return 0;
490
491 /*
492 * detached nodes are used to avoid unnecessary backref
493 * lookup. transaction commit changes the extent tree.
494 * so the detached nodes are no longer useful.
495 */
496 while (!list_empty(&cache->detached)) {
497 node = list_entry(cache->detached.next,
498 struct backref_node, list);
499 remove_backref_node(cache, node);
500 }
501
502 while (!list_empty(&cache->changed)) {
503 node = list_entry(cache->changed.next,
504 struct backref_node, list);
505 list_del_init(&node->list);
506 BUG_ON(node->pending);
507 update_backref_node(cache, node, node->new_bytenr);
508 }
509
510 /*
511 * some nodes can be left in the pending list if there were
512 * errors during processing the pending nodes.
513 */
514 for (level = 0; level < BTRFS_MAX_LEVEL; level++) {
515 list_for_each_entry(node, &cache->pending[level], list) {
516 BUG_ON(!node->pending);
517 if (node->bytenr == node->new_bytenr)
518 continue;
519 update_backref_node(cache, node, node->new_bytenr);
520 }
521 }
522
523 cache->last_trans = 0;
524 return 1;
525}
526
David Sterbaf2a97a92011-05-05 12:44:41 +0200527
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400528static int should_ignore_root(struct btrfs_root *root)
529{
530 struct btrfs_root *reloc_root;
531
Miao Xie27cdeb72014-04-02 19:51:05 +0800532 if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state))
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400533 return 0;
534
535 reloc_root = root->reloc_root;
536 if (!reloc_root)
537 return 0;
538
539 if (btrfs_root_last_snapshot(&reloc_root->root_item) ==
540 root->fs_info->running_transaction->transid - 1)
541 return 0;
542 /*
543 * if there is reloc tree and it was created in previous
544 * transaction backref lookup can find the reloc tree,
545 * so backref node for the fs tree root is useless for
546 * relocation.
547 */
548 return 1;
549}
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400550/*
551 * find reloc tree by address of tree root
552 */
553static struct btrfs_root *find_reloc_root(struct reloc_control *rc,
554 u64 bytenr)
555{
556 struct rb_node *rb_node;
557 struct mapping_node *node;
558 struct btrfs_root *root = NULL;
559
560 spin_lock(&rc->reloc_root_tree.lock);
561 rb_node = tree_search(&rc->reloc_root_tree.rb_root, bytenr);
562 if (rb_node) {
563 node = rb_entry(rb_node, struct mapping_node, rb_node);
564 root = (struct btrfs_root *)node->data;
565 }
566 spin_unlock(&rc->reloc_root_tree.lock);
567 return root;
568}
569
570static int is_cowonly_root(u64 root_objectid)
571{
572 if (root_objectid == BTRFS_ROOT_TREE_OBJECTID ||
573 root_objectid == BTRFS_EXTENT_TREE_OBJECTID ||
574 root_objectid == BTRFS_CHUNK_TREE_OBJECTID ||
575 root_objectid == BTRFS_DEV_TREE_OBJECTID ||
576 root_objectid == BTRFS_TREE_LOG_OBJECTID ||
Wang Shilong66463742013-12-10 00:14:34 +0800577 root_objectid == BTRFS_CSUM_TREE_OBJECTID ||
578 root_objectid == BTRFS_UUID_TREE_OBJECTID ||
David Sterba3e4c5ef2016-01-25 16:47:10 +0100579 root_objectid == BTRFS_QUOTA_TREE_OBJECTID ||
580 root_objectid == BTRFS_FREE_SPACE_TREE_OBJECTID)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400581 return 1;
582 return 0;
583}
584
585static struct btrfs_root *read_fs_root(struct btrfs_fs_info *fs_info,
586 u64 root_objectid)
587{
588 struct btrfs_key key;
589
590 key.objectid = root_objectid;
591 key.type = BTRFS_ROOT_ITEM_KEY;
592 if (is_cowonly_root(root_objectid))
593 key.offset = 0;
594 else
595 key.offset = (u64)-1;
596
Miao Xiec00869f2013-09-25 21:47:44 +0800597 return btrfs_get_fs_root(fs_info, &key, false);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400598}
599
600#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
601static noinline_for_stack
602struct btrfs_root *find_tree_root(struct reloc_control *rc,
603 struct extent_buffer *leaf,
604 struct btrfs_extent_ref_v0 *ref0)
605{
606 struct btrfs_root *root;
607 u64 root_objectid = btrfs_ref_root_v0(leaf, ref0);
608 u64 generation = btrfs_ref_generation_v0(leaf, ref0);
609
610 BUG_ON(root_objectid == BTRFS_TREE_RELOC_OBJECTID);
611
612 root = read_fs_root(rc->extent_root->fs_info, root_objectid);
613 BUG_ON(IS_ERR(root));
614
Miao Xie27cdeb72014-04-02 19:51:05 +0800615 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400616 generation != btrfs_root_generation(&root->root_item))
617 return NULL;
618
619 return root;
620}
621#endif
622
623static noinline_for_stack
624int find_inline_backref(struct extent_buffer *leaf, int slot,
625 unsigned long *ptr, unsigned long *end)
626{
Josef Bacik3173a182013-03-07 14:22:04 -0500627 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400628 struct btrfs_extent_item *ei;
629 struct btrfs_tree_block_info *bi;
630 u32 item_size;
631
Josef Bacik3173a182013-03-07 14:22:04 -0500632 btrfs_item_key_to_cpu(leaf, &key, slot);
633
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400634 item_size = btrfs_item_size_nr(leaf, slot);
635#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
636 if (item_size < sizeof(*ei)) {
637 WARN_ON(item_size != sizeof(struct btrfs_extent_item_v0));
638 return 1;
639 }
640#endif
641 ei = btrfs_item_ptr(leaf, slot, struct btrfs_extent_item);
642 WARN_ON(!(btrfs_extent_flags(leaf, ei) &
643 BTRFS_EXTENT_FLAG_TREE_BLOCK));
644
Josef Bacik3173a182013-03-07 14:22:04 -0500645 if (key.type == BTRFS_EXTENT_ITEM_KEY &&
646 item_size <= sizeof(*ei) + sizeof(*bi)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400647 WARN_ON(item_size < sizeof(*ei) + sizeof(*bi));
648 return 1;
649 }
Josef Bacikd062d132013-07-30 15:44:09 -0400650 if (key.type == BTRFS_METADATA_ITEM_KEY &&
651 item_size <= sizeof(*ei)) {
652 WARN_ON(item_size < sizeof(*ei));
653 return 1;
654 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400655
Josef Bacik3173a182013-03-07 14:22:04 -0500656 if (key.type == BTRFS_EXTENT_ITEM_KEY) {
657 bi = (struct btrfs_tree_block_info *)(ei + 1);
658 *ptr = (unsigned long)(bi + 1);
659 } else {
660 *ptr = (unsigned long)(ei + 1);
661 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400662 *end = (unsigned long)ei + item_size;
663 return 0;
664}
665
666/*
667 * build backref tree for a given tree block. root of the backref tree
668 * corresponds the tree block, leaves of the backref tree correspond
669 * roots of b-trees that reference the tree block.
670 *
671 * the basic idea of this function is check backrefs of a given block
Nicholas D Steeves01327612016-05-19 21:18:45 -0400672 * to find upper level blocks that reference the block, and then check
673 * backrefs of these upper level blocks recursively. the recursion stop
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400674 * when tree root is reached or backrefs for the block is cached.
675 *
676 * NOTE: if we find backrefs for a block are cached, we know backrefs
677 * for all upper level blocks that directly/indirectly reference the
678 * block are also cached.
679 */
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400680static noinline_for_stack
681struct backref_node *build_backref_tree(struct reloc_control *rc,
682 struct btrfs_key *node_key,
683 int level, u64 bytenr)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400684{
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400685 struct backref_cache *cache = &rc->backref_cache;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400686 struct btrfs_path *path1;
687 struct btrfs_path *path2;
688 struct extent_buffer *eb;
689 struct btrfs_root *root;
690 struct backref_node *cur;
691 struct backref_node *upper;
692 struct backref_node *lower;
693 struct backref_node *node = NULL;
694 struct backref_node *exist = NULL;
695 struct backref_edge *edge;
696 struct rb_node *rb_node;
697 struct btrfs_key key;
698 unsigned long end;
699 unsigned long ptr;
700 LIST_HEAD(list);
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400701 LIST_HEAD(useless);
702 int cowonly;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400703 int ret;
704 int err = 0;
Josef Bacikb6c60c82013-07-30 16:30:30 -0400705 bool need_check = true;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400706
707 path1 = btrfs_alloc_path();
708 path2 = btrfs_alloc_path();
709 if (!path1 || !path2) {
710 err = -ENOMEM;
711 goto out;
712 }
David Sterbae4058b52015-11-27 16:31:35 +0100713 path1->reada = READA_FORWARD;
714 path2->reada = READA_FORWARD;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400715
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400716 node = alloc_backref_node(cache);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400717 if (!node) {
718 err = -ENOMEM;
719 goto out;
720 }
721
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400722 node->bytenr = bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400723 node->level = level;
724 node->lowest = 1;
725 cur = node;
726again:
727 end = 0;
728 ptr = 0;
729 key.objectid = cur->bytenr;
Josef Bacik3173a182013-03-07 14:22:04 -0500730 key.type = BTRFS_METADATA_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400731 key.offset = (u64)-1;
732
733 path1->search_commit_root = 1;
734 path1->skip_locking = 1;
735 ret = btrfs_search_slot(NULL, rc->extent_root, &key, path1,
736 0, 0);
737 if (ret < 0) {
738 err = ret;
739 goto out;
740 }
Josef Bacik75bfb9af2014-09-19 10:40:00 -0400741 ASSERT(ret);
742 ASSERT(path1->slots[0]);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400743
744 path1->slots[0]--;
745
746 WARN_ON(cur->checked);
747 if (!list_empty(&cur->upper)) {
748 /*
Justin P. Mattock70f23fd2011-05-10 10:16:21 +0200749 * the backref was added previously when processing
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400750 * backref of type BTRFS_TREE_BLOCK_REF_KEY
751 */
Josef Bacik75bfb9af2014-09-19 10:40:00 -0400752 ASSERT(list_is_singular(&cur->upper));
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400753 edge = list_entry(cur->upper.next, struct backref_edge,
754 list[LOWER]);
Josef Bacik75bfb9af2014-09-19 10:40:00 -0400755 ASSERT(list_empty(&edge->list[UPPER]));
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400756 exist = edge->node[UPPER];
757 /*
758 * add the upper level block to pending list if we need
759 * check its backrefs
760 */
761 if (!exist->checked)
762 list_add_tail(&edge->list[UPPER], &list);
763 } else {
764 exist = NULL;
765 }
766
767 while (1) {
768 cond_resched();
769 eb = path1->nodes[0];
770
771 if (ptr >= end) {
772 if (path1->slots[0] >= btrfs_header_nritems(eb)) {
773 ret = btrfs_next_leaf(rc->extent_root, path1);
774 if (ret < 0) {
775 err = ret;
776 goto out;
777 }
778 if (ret > 0)
779 break;
780 eb = path1->nodes[0];
781 }
782
783 btrfs_item_key_to_cpu(eb, &key, path1->slots[0]);
784 if (key.objectid != cur->bytenr) {
785 WARN_ON(exist);
786 break;
787 }
788
Josef Bacik3173a182013-03-07 14:22:04 -0500789 if (key.type == BTRFS_EXTENT_ITEM_KEY ||
790 key.type == BTRFS_METADATA_ITEM_KEY) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400791 ret = find_inline_backref(eb, path1->slots[0],
792 &ptr, &end);
793 if (ret)
794 goto next;
795 }
796 }
797
798 if (ptr < end) {
799 /* update key for inline back ref */
800 struct btrfs_extent_inline_ref *iref;
801 iref = (struct btrfs_extent_inline_ref *)ptr;
802 key.type = btrfs_extent_inline_ref_type(eb, iref);
803 key.offset = btrfs_extent_inline_ref_offset(eb, iref);
804 WARN_ON(key.type != BTRFS_TREE_BLOCK_REF_KEY &&
805 key.type != BTRFS_SHARED_BLOCK_REF_KEY);
806 }
807
808 if (exist &&
809 ((key.type == BTRFS_TREE_BLOCK_REF_KEY &&
810 exist->owner == key.offset) ||
811 (key.type == BTRFS_SHARED_BLOCK_REF_KEY &&
812 exist->bytenr == key.offset))) {
813 exist = NULL;
814 goto next;
815 }
816
817#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
818 if (key.type == BTRFS_SHARED_BLOCK_REF_KEY ||
819 key.type == BTRFS_EXTENT_REF_V0_KEY) {
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400820 if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400821 struct btrfs_extent_ref_v0 *ref0;
822 ref0 = btrfs_item_ptr(eb, path1->slots[0],
823 struct btrfs_extent_ref_v0);
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400824 if (key.objectid == key.offset) {
Yan, Zheng046f2642010-05-31 08:58:47 +0000825 root = find_tree_root(rc, eb, ref0);
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400826 if (root && !should_ignore_root(root))
827 cur->root = root;
828 else
829 list_add(&cur->list, &useless);
830 break;
831 }
Yan, Zheng046f2642010-05-31 08:58:47 +0000832 if (is_cowonly_root(btrfs_ref_root_v0(eb,
833 ref0)))
834 cur->cowonly = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400835 }
836#else
Josef Bacik75bfb9af2014-09-19 10:40:00 -0400837 ASSERT(key.type != BTRFS_EXTENT_REF_V0_KEY);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400838 if (key.type == BTRFS_SHARED_BLOCK_REF_KEY) {
839#endif
840 if (key.objectid == key.offset) {
841 /*
842 * only root blocks of reloc trees use
843 * backref of this type.
844 */
845 root = find_reloc_root(rc, cur->bytenr);
Josef Bacik75bfb9af2014-09-19 10:40:00 -0400846 ASSERT(root);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400847 cur->root = root;
848 break;
849 }
850
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400851 edge = alloc_backref_edge(cache);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400852 if (!edge) {
853 err = -ENOMEM;
854 goto out;
855 }
856 rb_node = tree_search(&cache->rb_root, key.offset);
857 if (!rb_node) {
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400858 upper = alloc_backref_node(cache);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400859 if (!upper) {
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400860 free_backref_edge(cache, edge);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400861 err = -ENOMEM;
862 goto out;
863 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400864 upper->bytenr = key.offset;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400865 upper->level = cur->level + 1;
866 /*
867 * backrefs for the upper level block isn't
868 * cached, add the block to pending list
869 */
870 list_add_tail(&edge->list[UPPER], &list);
871 } else {
872 upper = rb_entry(rb_node, struct backref_node,
873 rb_node);
Josef Bacik75bfb9af2014-09-19 10:40:00 -0400874 ASSERT(upper->checked);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400875 INIT_LIST_HEAD(&edge->list[UPPER]);
876 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400877 list_add_tail(&edge->list[LOWER], &cur->upper);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400878 edge->node[LOWER] = cur;
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400879 edge->node[UPPER] = upper;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400880
881 goto next;
882 } else if (key.type != BTRFS_TREE_BLOCK_REF_KEY) {
883 goto next;
884 }
885
886 /* key.type == BTRFS_TREE_BLOCK_REF_KEY */
887 root = read_fs_root(rc->extent_root->fs_info, key.offset);
888 if (IS_ERR(root)) {
889 err = PTR_ERR(root);
890 goto out;
891 }
892
Miao Xie27cdeb72014-04-02 19:51:05 +0800893 if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state))
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400894 cur->cowonly = 1;
895
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400896 if (btrfs_root_level(&root->root_item) == cur->level) {
897 /* tree root */
Josef Bacik75bfb9af2014-09-19 10:40:00 -0400898 ASSERT(btrfs_root_bytenr(&root->root_item) ==
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400899 cur->bytenr);
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400900 if (should_ignore_root(root))
901 list_add(&cur->list, &useless);
902 else
903 cur->root = root;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400904 break;
905 }
906
907 level = cur->level + 1;
908
909 /*
910 * searching the tree to find upper level blocks
911 * reference the block.
912 */
913 path2->search_commit_root = 1;
914 path2->skip_locking = 1;
915 path2->lowest_level = level;
916 ret = btrfs_search_slot(NULL, root, node_key, path2, 0, 0);
917 path2->lowest_level = 0;
918 if (ret < 0) {
919 err = ret;
920 goto out;
921 }
Yan Zheng33c66f42009-07-22 09:59:00 -0400922 if (ret > 0 && path2->slots[level] > 0)
923 path2->slots[level]--;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400924
925 eb = path2->nodes[level];
Liu Bo3561b9d2016-09-14 08:51:46 -0700926 if (btrfs_node_blockptr(eb, path2->slots[level]) !=
927 cur->bytenr) {
928 btrfs_err(root->fs_info,
929 "couldn't find block (%llu) (level %d) in tree (%llu) with key (%llu %u %llu)",
930 cur->bytenr, level - 1, root->objectid,
931 node_key->objectid, node_key->type,
932 node_key->offset);
933 err = -ENOENT;
934 goto out;
935 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400936 lower = cur;
Josef Bacikb6c60c82013-07-30 16:30:30 -0400937 need_check = true;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400938 for (; level < BTRFS_MAX_LEVEL; level++) {
939 if (!path2->nodes[level]) {
Josef Bacik75bfb9af2014-09-19 10:40:00 -0400940 ASSERT(btrfs_root_bytenr(&root->root_item) ==
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400941 lower->bytenr);
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400942 if (should_ignore_root(root))
943 list_add(&lower->list, &useless);
944 else
945 lower->root = root;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400946 break;
947 }
948
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400949 edge = alloc_backref_edge(cache);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400950 if (!edge) {
951 err = -ENOMEM;
952 goto out;
953 }
954
955 eb = path2->nodes[level];
956 rb_node = tree_search(&cache->rb_root, eb->start);
957 if (!rb_node) {
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400958 upper = alloc_backref_node(cache);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400959 if (!upper) {
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400960 free_backref_edge(cache, edge);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400961 err = -ENOMEM;
962 goto out;
963 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400964 upper->bytenr = eb->start;
965 upper->owner = btrfs_header_owner(eb);
966 upper->level = lower->level + 1;
Miao Xie27cdeb72014-04-02 19:51:05 +0800967 if (!test_bit(BTRFS_ROOT_REF_COWS,
968 &root->state))
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400969 upper->cowonly = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400970
971 /*
972 * if we know the block isn't shared
973 * we can void checking its backrefs.
974 */
975 if (btrfs_block_can_be_shared(root, eb))
976 upper->checked = 0;
977 else
978 upper->checked = 1;
979
980 /*
981 * add the block to pending list if we
Josef Bacikb6c60c82013-07-30 16:30:30 -0400982 * need check its backrefs, we only do this once
983 * while walking up a tree as we will catch
984 * anything else later on.
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400985 */
Josef Bacikb6c60c82013-07-30 16:30:30 -0400986 if (!upper->checked && need_check) {
987 need_check = false;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400988 list_add_tail(&edge->list[UPPER],
989 &list);
Josef Bacikbbe90512014-09-19 15:43:34 -0400990 } else {
991 if (upper->checked)
992 need_check = true;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400993 INIT_LIST_HEAD(&edge->list[UPPER]);
Josef Bacikbbe90512014-09-19 15:43:34 -0400994 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400995 } else {
996 upper = rb_entry(rb_node, struct backref_node,
997 rb_node);
Josef Bacik75bfb9af2014-09-19 10:40:00 -0400998 ASSERT(upper->checked);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400999 INIT_LIST_HEAD(&edge->list[UPPER]);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001000 if (!upper->owner)
1001 upper->owner = btrfs_header_owner(eb);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001002 }
1003 list_add_tail(&edge->list[LOWER], &lower->upper);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001004 edge->node[LOWER] = lower;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001005 edge->node[UPPER] = upper;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001006
1007 if (rb_node)
1008 break;
1009 lower = upper;
1010 upper = NULL;
1011 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001012 btrfs_release_path(path2);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001013next:
1014 if (ptr < end) {
1015 ptr += btrfs_extent_inline_ref_size(key.type);
1016 if (ptr >= end) {
1017 WARN_ON(ptr > end);
1018 ptr = 0;
1019 end = 0;
1020 }
1021 }
1022 if (ptr >= end)
1023 path1->slots[0]++;
1024 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001025 btrfs_release_path(path1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001026
1027 cur->checked = 1;
1028 WARN_ON(exist);
1029
1030 /* the pending list isn't empty, take the first block to process */
1031 if (!list_empty(&list)) {
1032 edge = list_entry(list.next, struct backref_edge, list[UPPER]);
1033 list_del_init(&edge->list[UPPER]);
1034 cur = edge->node[UPPER];
1035 goto again;
1036 }
1037
1038 /*
1039 * everything goes well, connect backref nodes and insert backref nodes
1040 * into the cache.
1041 */
Josef Bacik75bfb9af2014-09-19 10:40:00 -04001042 ASSERT(node->checked);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001043 cowonly = node->cowonly;
1044 if (!cowonly) {
1045 rb_node = tree_insert(&cache->rb_root, node->bytenr,
1046 &node->rb_node);
Jeff Mahoney43c04fb2011-10-03 23:22:33 -04001047 if (rb_node)
1048 backref_tree_panic(rb_node, -EEXIST, node->bytenr);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001049 list_add_tail(&node->lower, &cache->leaves);
1050 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001051
1052 list_for_each_entry(edge, &node->upper, list[LOWER])
1053 list_add_tail(&edge->list[UPPER], &list);
1054
1055 while (!list_empty(&list)) {
1056 edge = list_entry(list.next, struct backref_edge, list[UPPER]);
1057 list_del_init(&edge->list[UPPER]);
1058 upper = edge->node[UPPER];
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001059 if (upper->detached) {
1060 list_del(&edge->list[LOWER]);
1061 lower = edge->node[LOWER];
1062 free_backref_edge(cache, edge);
1063 if (list_empty(&lower->upper))
1064 list_add(&lower->list, &useless);
1065 continue;
1066 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001067
1068 if (!RB_EMPTY_NODE(&upper->rb_node)) {
1069 if (upper->lowest) {
1070 list_del_init(&upper->lower);
1071 upper->lowest = 0;
1072 }
1073
1074 list_add_tail(&edge->list[UPPER], &upper->lower);
1075 continue;
1076 }
1077
Josef Bacik75bfb9af2014-09-19 10:40:00 -04001078 if (!upper->checked) {
1079 /*
1080 * Still want to blow up for developers since this is a
1081 * logic bug.
1082 */
1083 ASSERT(0);
1084 err = -EINVAL;
1085 goto out;
1086 }
1087 if (cowonly != upper->cowonly) {
1088 ASSERT(0);
1089 err = -EINVAL;
1090 goto out;
1091 }
1092
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001093 if (!cowonly) {
1094 rb_node = tree_insert(&cache->rb_root, upper->bytenr,
1095 &upper->rb_node);
Jeff Mahoney43c04fb2011-10-03 23:22:33 -04001096 if (rb_node)
1097 backref_tree_panic(rb_node, -EEXIST,
1098 upper->bytenr);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001099 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001100
1101 list_add_tail(&edge->list[UPPER], &upper->lower);
1102
1103 list_for_each_entry(edge, &upper->upper, list[LOWER])
1104 list_add_tail(&edge->list[UPPER], &list);
1105 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001106 /*
1107 * process useless backref nodes. backref nodes for tree leaves
1108 * are deleted from the cache. backref nodes for upper level
1109 * tree blocks are left in the cache to avoid unnecessary backref
1110 * lookup.
1111 */
1112 while (!list_empty(&useless)) {
1113 upper = list_entry(useless.next, struct backref_node, list);
1114 list_del_init(&upper->list);
Josef Bacik75bfb9af2014-09-19 10:40:00 -04001115 ASSERT(list_empty(&upper->upper));
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001116 if (upper == node)
1117 node = NULL;
1118 if (upper->lowest) {
1119 list_del_init(&upper->lower);
1120 upper->lowest = 0;
1121 }
1122 while (!list_empty(&upper->lower)) {
1123 edge = list_entry(upper->lower.next,
1124 struct backref_edge, list[UPPER]);
1125 list_del(&edge->list[UPPER]);
1126 list_del(&edge->list[LOWER]);
1127 lower = edge->node[LOWER];
1128 free_backref_edge(cache, edge);
1129
1130 if (list_empty(&lower->upper))
1131 list_add(&lower->list, &useless);
1132 }
1133 __mark_block_processed(rc, upper);
1134 if (upper->level > 0) {
1135 list_add(&upper->list, &cache->detached);
1136 upper->detached = 1;
1137 } else {
1138 rb_erase(&upper->rb_node, &cache->rb_root);
1139 free_backref_node(cache, upper);
1140 }
1141 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001142out:
1143 btrfs_free_path(path1);
1144 btrfs_free_path(path2);
1145 if (err) {
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001146 while (!list_empty(&useless)) {
1147 lower = list_entry(useless.next,
Josef Bacik75bfb9af2014-09-19 10:40:00 -04001148 struct backref_node, list);
1149 list_del_init(&lower->list);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001150 }
Josef Bacik75bfb9af2014-09-19 10:40:00 -04001151 while (!list_empty(&list)) {
1152 edge = list_first_entry(&list, struct backref_edge,
1153 list[UPPER]);
1154 list_del(&edge->list[UPPER]);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001155 list_del(&edge->list[LOWER]);
Josef Bacik75bfb9af2014-09-19 10:40:00 -04001156 lower = edge->node[LOWER];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001157 upper = edge->node[UPPER];
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001158 free_backref_edge(cache, edge);
Josef Bacik75bfb9af2014-09-19 10:40:00 -04001159
1160 /*
1161 * Lower is no longer linked to any upper backref nodes
1162 * and isn't in the cache, we can free it ourselves.
1163 */
1164 if (list_empty(&lower->upper) &&
1165 RB_EMPTY_NODE(&lower->rb_node))
1166 list_add(&lower->list, &useless);
1167
1168 if (!RB_EMPTY_NODE(&upper->rb_node))
1169 continue;
1170
Nicholas D Steeves01327612016-05-19 21:18:45 -04001171 /* Add this guy's upper edges to the list to process */
Josef Bacik75bfb9af2014-09-19 10:40:00 -04001172 list_for_each_entry(edge, &upper->upper, list[LOWER])
1173 list_add_tail(&edge->list[UPPER], &list);
1174 if (list_empty(&upper->upper))
1175 list_add(&upper->list, &useless);
1176 }
1177
1178 while (!list_empty(&useless)) {
1179 lower = list_entry(useless.next,
1180 struct backref_node, list);
1181 list_del_init(&lower->list);
Liu Bo0fd8c3d2016-07-12 10:29:37 -07001182 if (lower == node)
1183 node = NULL;
Josef Bacik75bfb9af2014-09-19 10:40:00 -04001184 free_backref_node(cache, lower);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001185 }
Liu Bo0fd8c3d2016-07-12 10:29:37 -07001186
1187 free_backref_node(cache, node);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001188 return ERR_PTR(err);
1189 }
Josef Bacik75bfb9af2014-09-19 10:40:00 -04001190 ASSERT(!node || !node->detached);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001191 return node;
1192}
1193
1194/*
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001195 * helper to add backref node for the newly created snapshot.
1196 * the backref node is created by cloning backref node that
1197 * corresponds to root of source tree
1198 */
1199static int clone_backref_node(struct btrfs_trans_handle *trans,
1200 struct reloc_control *rc,
1201 struct btrfs_root *src,
1202 struct btrfs_root *dest)
1203{
1204 struct btrfs_root *reloc_root = src->reloc_root;
1205 struct backref_cache *cache = &rc->backref_cache;
1206 struct backref_node *node = NULL;
1207 struct backref_node *new_node;
1208 struct backref_edge *edge;
1209 struct backref_edge *new_edge;
1210 struct rb_node *rb_node;
1211
1212 if (cache->last_trans > 0)
1213 update_backref_cache(trans, cache);
1214
1215 rb_node = tree_search(&cache->rb_root, src->commit_root->start);
1216 if (rb_node) {
1217 node = rb_entry(rb_node, struct backref_node, rb_node);
1218 if (node->detached)
1219 node = NULL;
1220 else
1221 BUG_ON(node->new_bytenr != reloc_root->node->start);
1222 }
1223
1224 if (!node) {
1225 rb_node = tree_search(&cache->rb_root,
1226 reloc_root->commit_root->start);
1227 if (rb_node) {
1228 node = rb_entry(rb_node, struct backref_node,
1229 rb_node);
1230 BUG_ON(node->detached);
1231 }
1232 }
1233
1234 if (!node)
1235 return 0;
1236
1237 new_node = alloc_backref_node(cache);
1238 if (!new_node)
1239 return -ENOMEM;
1240
1241 new_node->bytenr = dest->node->start;
1242 new_node->level = node->level;
1243 new_node->lowest = node->lowest;
Yan, Zheng6848ad62011-02-14 16:00:03 -05001244 new_node->checked = 1;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001245 new_node->root = dest;
1246
1247 if (!node->lowest) {
1248 list_for_each_entry(edge, &node->lower, list[UPPER]) {
1249 new_edge = alloc_backref_edge(cache);
1250 if (!new_edge)
1251 goto fail;
1252
1253 new_edge->node[UPPER] = new_node;
1254 new_edge->node[LOWER] = edge->node[LOWER];
1255 list_add_tail(&new_edge->list[UPPER],
1256 &new_node->lower);
1257 }
Miao Xie76b9e232011-11-10 20:45:05 -05001258 } else {
1259 list_add_tail(&new_node->lower, &cache->leaves);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001260 }
1261
1262 rb_node = tree_insert(&cache->rb_root, new_node->bytenr,
1263 &new_node->rb_node);
Jeff Mahoney43c04fb2011-10-03 23:22:33 -04001264 if (rb_node)
1265 backref_tree_panic(rb_node, -EEXIST, new_node->bytenr);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001266
1267 if (!new_node->lowest) {
1268 list_for_each_entry(new_edge, &new_node->lower, list[UPPER]) {
1269 list_add_tail(&new_edge->list[LOWER],
1270 &new_edge->node[LOWER]->upper);
1271 }
1272 }
1273 return 0;
1274fail:
1275 while (!list_empty(&new_node->lower)) {
1276 new_edge = list_entry(new_node->lower.next,
1277 struct backref_edge, list[UPPER]);
1278 list_del(&new_edge->list[UPPER]);
1279 free_backref_edge(cache, new_edge);
1280 }
1281 free_backref_node(cache, new_node);
1282 return -ENOMEM;
1283}
1284
1285/*
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001286 * helper to add 'address of tree root -> reloc tree' mapping
1287 */
Jeff Mahoneyffd7b332011-10-03 23:23:15 -04001288static int __must_check __add_reloc_root(struct btrfs_root *root)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001289{
1290 struct rb_node *rb_node;
1291 struct mapping_node *node;
1292 struct reloc_control *rc = root->fs_info->reloc_ctl;
1293
1294 node = kmalloc(sizeof(*node), GFP_NOFS);
Jeff Mahoneyffd7b332011-10-03 23:23:15 -04001295 if (!node)
1296 return -ENOMEM;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001297
1298 node->bytenr = root->node->start;
1299 node->data = root;
1300
1301 spin_lock(&rc->reloc_root_tree.lock);
1302 rb_node = tree_insert(&rc->reloc_root_tree.rb_root,
1303 node->bytenr, &node->rb_node);
1304 spin_unlock(&rc->reloc_root_tree.lock);
Jeff Mahoneyffd7b332011-10-03 23:23:15 -04001305 if (rb_node) {
Jeff Mahoneyffd7b332011-10-03 23:23:15 -04001306 btrfs_panic(root->fs_info, -EEXIST, "Duplicate root found "
1307 "for start=%llu while inserting into relocation "
David Sterba351fd352014-05-15 16:48:20 +02001308 "tree", node->bytenr);
Dan Carpenter23291a02012-06-25 05:15:23 -06001309 kfree(node);
1310 return -EEXIST;
Jeff Mahoneyffd7b332011-10-03 23:23:15 -04001311 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001312
1313 list_add_tail(&root->root_list, &rc->reloc_roots);
1314 return 0;
1315}
1316
1317/*
Wang Shilongc974c462013-12-11 19:29:51 +08001318 * helper to delete the 'address of tree root -> reloc tree'
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001319 * mapping
1320 */
Wang Shilongc974c462013-12-11 19:29:51 +08001321static void __del_reloc_root(struct btrfs_root *root)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001322{
1323 struct rb_node *rb_node;
1324 struct mapping_node *node = NULL;
1325 struct reloc_control *rc = root->fs_info->reloc_ctl;
1326
1327 spin_lock(&rc->reloc_root_tree.lock);
1328 rb_node = tree_search(&rc->reloc_root_tree.rb_root,
Wang Shilongc974c462013-12-11 19:29:51 +08001329 root->node->start);
1330 if (rb_node) {
1331 node = rb_entry(rb_node, struct mapping_node, rb_node);
1332 rb_erase(&node->rb_node, &rc->reloc_root_tree.rb_root);
1333 }
1334 spin_unlock(&rc->reloc_root_tree.lock);
1335
1336 if (!node)
1337 return;
1338 BUG_ON((struct btrfs_root *)node->data != root);
1339
1340 spin_lock(&root->fs_info->trans_lock);
1341 list_del_init(&root->root_list);
1342 spin_unlock(&root->fs_info->trans_lock);
1343 kfree(node);
1344}
1345
1346/*
1347 * helper to update the 'address of tree root -> reloc tree'
1348 * mapping
1349 */
1350static int __update_reloc_root(struct btrfs_root *root, u64 new_bytenr)
1351{
1352 struct rb_node *rb_node;
1353 struct mapping_node *node = NULL;
1354 struct reloc_control *rc = root->fs_info->reloc_ctl;
1355
1356 spin_lock(&rc->reloc_root_tree.lock);
1357 rb_node = tree_search(&rc->reloc_root_tree.rb_root,
1358 root->node->start);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001359 if (rb_node) {
1360 node = rb_entry(rb_node, struct mapping_node, rb_node);
1361 rb_erase(&node->rb_node, &rc->reloc_root_tree.rb_root);
1362 }
1363 spin_unlock(&rc->reloc_root_tree.lock);
1364
Liu Bo8f71f3e2013-03-04 16:25:36 +00001365 if (!node)
1366 return 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001367 BUG_ON((struct btrfs_root *)node->data != root);
1368
Wang Shilongc974c462013-12-11 19:29:51 +08001369 spin_lock(&rc->reloc_root_tree.lock);
1370 node->bytenr = new_bytenr;
1371 rb_node = tree_insert(&rc->reloc_root_tree.rb_root,
1372 node->bytenr, &node->rb_node);
1373 spin_unlock(&rc->reloc_root_tree.lock);
1374 if (rb_node)
1375 backref_tree_panic(rb_node, -EEXIST, node->bytenr);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001376 return 0;
1377}
1378
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001379static struct btrfs_root *create_reloc_root(struct btrfs_trans_handle *trans,
1380 struct btrfs_root *root, u64 objectid)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001381{
1382 struct btrfs_root *reloc_root;
1383 struct extent_buffer *eb;
1384 struct btrfs_root_item *root_item;
1385 struct btrfs_key root_key;
Miao Xie5bc72472013-06-06 03:28:03 +00001386 u64 last_snap = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001387 int ret;
1388
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001389 root_item = kmalloc(sizeof(*root_item), GFP_NOFS);
1390 BUG_ON(!root_item);
1391
1392 root_key.objectid = BTRFS_TREE_RELOC_OBJECTID;
1393 root_key.type = BTRFS_ROOT_ITEM_KEY;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001394 root_key.offset = objectid;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001395
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001396 if (root->root_key.objectid == objectid) {
1397 /* called by btrfs_init_reloc_root */
1398 ret = btrfs_copy_root(trans, root, root->commit_root, &eb,
1399 BTRFS_TREE_RELOC_OBJECTID);
1400 BUG_ON(ret);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001401
Miao Xie5bc72472013-06-06 03:28:03 +00001402 last_snap = btrfs_root_last_snapshot(&root->root_item);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001403 btrfs_set_root_last_snapshot(&root->root_item,
1404 trans->transid - 1);
1405 } else {
1406 /*
1407 * called by btrfs_reloc_post_snapshot_hook.
1408 * the source tree is a reloc tree, all tree blocks
1409 * modified after it was created have RELOC flag
1410 * set in their headers. so it's OK to not update
1411 * the 'last_snapshot'.
1412 */
1413 ret = btrfs_copy_root(trans, root, root->node, &eb,
1414 BTRFS_TREE_RELOC_OBJECTID);
1415 BUG_ON(ret);
1416 }
1417
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001418 memcpy(root_item, &root->root_item, sizeof(*root_item));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001419 btrfs_set_root_bytenr(root_item, eb->start);
1420 btrfs_set_root_level(root_item, btrfs_header_level(eb));
1421 btrfs_set_root_generation(root_item, trans->transid);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001422
1423 if (root->root_key.objectid == objectid) {
1424 btrfs_set_root_refs(root_item, 0);
1425 memset(&root_item->drop_progress, 0,
1426 sizeof(struct btrfs_disk_key));
1427 root_item->drop_level = 0;
Miao Xie5bc72472013-06-06 03:28:03 +00001428 /*
1429 * abuse rtransid, it is safe because it is impossible to
1430 * receive data into a relocation tree.
1431 */
1432 btrfs_set_root_rtransid(root_item, last_snap);
1433 btrfs_set_root_otransid(root_item, trans->transid);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001434 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001435
1436 btrfs_tree_unlock(eb);
1437 free_extent_buffer(eb);
1438
1439 ret = btrfs_insert_root(trans, root->fs_info->tree_root,
1440 &root_key, root_item);
1441 BUG_ON(ret);
1442 kfree(root_item);
1443
Miao Xiecb517ea2013-05-15 07:48:19 +00001444 reloc_root = btrfs_read_fs_root(root->fs_info->tree_root, &root_key);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001445 BUG_ON(IS_ERR(reloc_root));
1446 reloc_root->last_trans = trans->transid;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001447 return reloc_root;
1448}
1449
1450/*
1451 * create reloc tree for a given fs tree. reloc tree is just a
1452 * snapshot of the fs tree with special root objectid.
1453 */
1454int btrfs_init_reloc_root(struct btrfs_trans_handle *trans,
1455 struct btrfs_root *root)
1456{
1457 struct btrfs_root *reloc_root;
1458 struct reloc_control *rc = root->fs_info->reloc_ctl;
Miao Xie20dd2cb2013-09-25 21:47:45 +08001459 struct btrfs_block_rsv *rsv;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001460 int clear_rsv = 0;
Jeff Mahoneyffd7b332011-10-03 23:23:15 -04001461 int ret;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001462
1463 if (root->reloc_root) {
1464 reloc_root = root->reloc_root;
1465 reloc_root->last_trans = trans->transid;
1466 return 0;
1467 }
1468
1469 if (!rc || !rc->create_reloc_tree ||
1470 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
1471 return 0;
1472
Miao Xie20dd2cb2013-09-25 21:47:45 +08001473 if (!trans->reloc_reserved) {
1474 rsv = trans->block_rsv;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001475 trans->block_rsv = rc->block_rsv;
1476 clear_rsv = 1;
1477 }
1478 reloc_root = create_reloc_root(trans, root, root->root_key.objectid);
1479 if (clear_rsv)
Miao Xie20dd2cb2013-09-25 21:47:45 +08001480 trans->block_rsv = rsv;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001481
Jeff Mahoneyffd7b332011-10-03 23:23:15 -04001482 ret = __add_reloc_root(reloc_root);
1483 BUG_ON(ret < 0);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001484 root->reloc_root = reloc_root;
1485 return 0;
1486}
1487
1488/*
1489 * update root item of reloc tree
1490 */
1491int btrfs_update_reloc_root(struct btrfs_trans_handle *trans,
1492 struct btrfs_root *root)
1493{
1494 struct btrfs_root *reloc_root;
1495 struct btrfs_root_item *root_item;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001496 int ret;
1497
1498 if (!root->reloc_root)
Chris Mason75857172011-06-13 20:00:16 -04001499 goto out;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001500
1501 reloc_root = root->reloc_root;
1502 root_item = &reloc_root->root_item;
1503
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001504 if (root->fs_info->reloc_ctl->merge_reloc_tree &&
1505 btrfs_root_refs(root_item) == 0) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001506 root->reloc_root = NULL;
Wang Shilongc974c462013-12-11 19:29:51 +08001507 __del_reloc_root(reloc_root);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001508 }
1509
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001510 if (reloc_root->commit_root != reloc_root->node) {
1511 btrfs_set_root_node(root_item, reloc_root->node);
1512 free_extent_buffer(reloc_root->commit_root);
1513 reloc_root->commit_root = btrfs_root_node(reloc_root);
1514 }
1515
1516 ret = btrfs_update_root(trans, root->fs_info->tree_root,
1517 &reloc_root->root_key, root_item);
1518 BUG_ON(ret);
Chris Mason75857172011-06-13 20:00:16 -04001519
1520out:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001521 return 0;
1522}
1523
1524/*
1525 * helper to find first cached inode with inode number >= objectid
1526 * in a subvolume
1527 */
1528static struct inode *find_next_inode(struct btrfs_root *root, u64 objectid)
1529{
1530 struct rb_node *node;
1531 struct rb_node *prev;
1532 struct btrfs_inode *entry;
1533 struct inode *inode;
1534
1535 spin_lock(&root->inode_lock);
1536again:
1537 node = root->inode_tree.rb_node;
1538 prev = NULL;
1539 while (node) {
1540 prev = node;
1541 entry = rb_entry(node, struct btrfs_inode, rb_node);
1542
Li Zefan33345d012011-04-20 10:31:50 +08001543 if (objectid < btrfs_ino(&entry->vfs_inode))
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001544 node = node->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08001545 else if (objectid > btrfs_ino(&entry->vfs_inode))
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001546 node = node->rb_right;
1547 else
1548 break;
1549 }
1550 if (!node) {
1551 while (prev) {
1552 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08001553 if (objectid <= btrfs_ino(&entry->vfs_inode)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001554 node = prev;
1555 break;
1556 }
1557 prev = rb_next(prev);
1558 }
1559 }
1560 while (node) {
1561 entry = rb_entry(node, struct btrfs_inode, rb_node);
1562 inode = igrab(&entry->vfs_inode);
1563 if (inode) {
1564 spin_unlock(&root->inode_lock);
1565 return inode;
1566 }
1567
Li Zefan33345d012011-04-20 10:31:50 +08001568 objectid = btrfs_ino(&entry->vfs_inode) + 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001569 if (cond_resched_lock(&root->inode_lock))
1570 goto again;
1571
1572 node = rb_next(node);
1573 }
1574 spin_unlock(&root->inode_lock);
1575 return NULL;
1576}
1577
1578static int in_block_group(u64 bytenr,
1579 struct btrfs_block_group_cache *block_group)
1580{
1581 if (bytenr >= block_group->key.objectid &&
1582 bytenr < block_group->key.objectid + block_group->key.offset)
1583 return 1;
1584 return 0;
1585}
1586
1587/*
1588 * get new location of data
1589 */
1590static int get_new_location(struct inode *reloc_inode, u64 *new_bytenr,
1591 u64 bytenr, u64 num_bytes)
1592{
1593 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
1594 struct btrfs_path *path;
1595 struct btrfs_file_extent_item *fi;
1596 struct extent_buffer *leaf;
1597 int ret;
1598
1599 path = btrfs_alloc_path();
1600 if (!path)
1601 return -ENOMEM;
1602
1603 bytenr -= BTRFS_I(reloc_inode)->index_cnt;
Li Zefan33345d012011-04-20 10:31:50 +08001604 ret = btrfs_lookup_file_extent(NULL, root, path, btrfs_ino(reloc_inode),
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001605 bytenr, 0);
1606 if (ret < 0)
1607 goto out;
1608 if (ret > 0) {
1609 ret = -ENOENT;
1610 goto out;
1611 }
1612
1613 leaf = path->nodes[0];
1614 fi = btrfs_item_ptr(leaf, path->slots[0],
1615 struct btrfs_file_extent_item);
1616
1617 BUG_ON(btrfs_file_extent_offset(leaf, fi) ||
1618 btrfs_file_extent_compression(leaf, fi) ||
1619 btrfs_file_extent_encryption(leaf, fi) ||
1620 btrfs_file_extent_other_encoding(leaf, fi));
1621
1622 if (num_bytes != btrfs_file_extent_disk_num_bytes(leaf, fi)) {
Josef Bacik83d4cfd2013-08-30 15:09:51 -04001623 ret = -EINVAL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001624 goto out;
1625 }
1626
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001627 *new_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001628 ret = 0;
1629out:
1630 btrfs_free_path(path);
1631 return ret;
1632}
1633
1634/*
1635 * update file extent items in the tree leaf to point to
1636 * the new locations.
1637 */
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001638static noinline_for_stack
1639int replace_file_extents(struct btrfs_trans_handle *trans,
1640 struct reloc_control *rc,
1641 struct btrfs_root *root,
1642 struct extent_buffer *leaf)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001643{
1644 struct btrfs_key key;
1645 struct btrfs_file_extent_item *fi;
1646 struct inode *inode = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001647 u64 parent;
1648 u64 bytenr;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001649 u64 new_bytenr = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001650 u64 num_bytes;
1651 u64 end;
1652 u32 nritems;
1653 u32 i;
Josef Bacik83d4cfd2013-08-30 15:09:51 -04001654 int ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001655 int first = 1;
1656 int dirty = 0;
1657
1658 if (rc->stage != UPDATE_DATA_PTRS)
1659 return 0;
1660
1661 /* reloc trees always use full backref */
1662 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
1663 parent = leaf->start;
1664 else
1665 parent = 0;
1666
1667 nritems = btrfs_header_nritems(leaf);
1668 for (i = 0; i < nritems; i++) {
1669 cond_resched();
1670 btrfs_item_key_to_cpu(leaf, &key, i);
1671 if (key.type != BTRFS_EXTENT_DATA_KEY)
1672 continue;
1673 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
1674 if (btrfs_file_extent_type(leaf, fi) ==
1675 BTRFS_FILE_EXTENT_INLINE)
1676 continue;
1677 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
1678 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
1679 if (bytenr == 0)
1680 continue;
1681 if (!in_block_group(bytenr, rc->block_group))
1682 continue;
1683
1684 /*
1685 * if we are modifying block in fs tree, wait for readpage
1686 * to complete and drop the extent cache
1687 */
1688 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001689 if (first) {
1690 inode = find_next_inode(root, key.objectid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001691 first = 0;
Li Zefan33345d012011-04-20 10:31:50 +08001692 } else if (inode && btrfs_ino(inode) < key.objectid) {
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001693 btrfs_add_delayed_iput(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001694 inode = find_next_inode(root, key.objectid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001695 }
Li Zefan33345d012011-04-20 10:31:50 +08001696 if (inode && btrfs_ino(inode) == key.objectid) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001697 end = key.offset +
1698 btrfs_file_extent_num_bytes(leaf, fi);
1699 WARN_ON(!IS_ALIGNED(key.offset,
1700 root->sectorsize));
1701 WARN_ON(!IS_ALIGNED(end, root->sectorsize));
1702 end--;
1703 ret = try_lock_extent(&BTRFS_I(inode)->io_tree,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001704 key.offset, end);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001705 if (!ret)
1706 continue;
1707
1708 btrfs_drop_extent_cache(inode, key.offset, end,
1709 1);
1710 unlock_extent(&BTRFS_I(inode)->io_tree,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001711 key.offset, end);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001712 }
1713 }
1714
1715 ret = get_new_location(rc->data_inode, &new_bytenr,
1716 bytenr, num_bytes);
Josef Bacik83d4cfd2013-08-30 15:09:51 -04001717 if (ret) {
1718 /*
1719 * Don't have to abort since we've not changed anything
1720 * in the file extent yet.
1721 */
1722 break;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001723 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001724
1725 btrfs_set_file_extent_disk_bytenr(leaf, fi, new_bytenr);
1726 dirty = 1;
1727
1728 key.offset -= btrfs_file_extent_offset(leaf, fi);
1729 ret = btrfs_inc_extent_ref(trans, root, new_bytenr,
1730 num_bytes, parent,
1731 btrfs_header_owner(leaf),
Filipe Mananab06c4bf2015-10-23 07:52:54 +01001732 key.objectid, key.offset);
Josef Bacik83d4cfd2013-08-30 15:09:51 -04001733 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04001734 btrfs_abort_transaction(trans, ret);
Josef Bacik83d4cfd2013-08-30 15:09:51 -04001735 break;
1736 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001737
1738 ret = btrfs_free_extent(trans, root, bytenr, num_bytes,
1739 parent, btrfs_header_owner(leaf),
Filipe Mananab06c4bf2015-10-23 07:52:54 +01001740 key.objectid, key.offset);
Josef Bacik83d4cfd2013-08-30 15:09:51 -04001741 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04001742 btrfs_abort_transaction(trans, ret);
Josef Bacik83d4cfd2013-08-30 15:09:51 -04001743 break;
1744 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001745 }
1746 if (dirty)
1747 btrfs_mark_buffer_dirty(leaf);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001748 if (inode)
1749 btrfs_add_delayed_iput(inode);
Josef Bacik83d4cfd2013-08-30 15:09:51 -04001750 return ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001751}
1752
1753static noinline_for_stack
1754int memcmp_node_keys(struct extent_buffer *eb, int slot,
1755 struct btrfs_path *path, int level)
1756{
1757 struct btrfs_disk_key key1;
1758 struct btrfs_disk_key key2;
1759 btrfs_node_key(eb, &key1, slot);
1760 btrfs_node_key(path->nodes[level], &key2, path->slots[level]);
1761 return memcmp(&key1, &key2, sizeof(key1));
1762}
1763
1764/*
1765 * try to replace tree blocks in fs tree with the new blocks
1766 * in reloc tree. tree blocks haven't been modified since the
1767 * reloc tree was create can be replaced.
1768 *
1769 * if a block was replaced, level of the block + 1 is returned.
1770 * if no block got replaced, 0 is returned. if there are other
1771 * errors, a negative error number is returned.
1772 */
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001773static noinline_for_stack
1774int replace_path(struct btrfs_trans_handle *trans,
1775 struct btrfs_root *dest, struct btrfs_root *src,
1776 struct btrfs_path *path, struct btrfs_key *next_key,
1777 int lowest_level, int max_level)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001778{
1779 struct extent_buffer *eb;
1780 struct extent_buffer *parent;
1781 struct btrfs_key key;
1782 u64 old_bytenr;
1783 u64 new_bytenr;
1784 u64 old_ptr_gen;
1785 u64 new_ptr_gen;
1786 u64 last_snapshot;
1787 u32 blocksize;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001788 int cow = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001789 int level;
1790 int ret;
1791 int slot;
1792
1793 BUG_ON(src->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
1794 BUG_ON(dest->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001795
1796 last_snapshot = btrfs_root_last_snapshot(&src->root_item);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001797again:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001798 slot = path->slots[lowest_level];
1799 btrfs_node_key_to_cpu(path->nodes[lowest_level], &key, slot);
1800
1801 eb = btrfs_lock_root_node(dest);
1802 btrfs_set_lock_blocking(eb);
1803 level = btrfs_header_level(eb);
1804
1805 if (level < lowest_level) {
1806 btrfs_tree_unlock(eb);
1807 free_extent_buffer(eb);
1808 return 0;
1809 }
1810
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001811 if (cow) {
1812 ret = btrfs_cow_block(trans, dest, eb, NULL, 0, &eb);
1813 BUG_ON(ret);
1814 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001815 btrfs_set_lock_blocking(eb);
1816
1817 if (next_key) {
1818 next_key->objectid = (u64)-1;
1819 next_key->type = (u8)-1;
1820 next_key->offset = (u64)-1;
1821 }
1822
1823 parent = eb;
1824 while (1) {
1825 level = btrfs_header_level(parent);
1826 BUG_ON(level < lowest_level);
1827
1828 ret = btrfs_bin_search(parent, &key, level, &slot);
1829 if (ret && slot > 0)
1830 slot--;
1831
1832 if (next_key && slot + 1 < btrfs_header_nritems(parent))
1833 btrfs_node_key_to_cpu(parent, next_key, slot + 1);
1834
1835 old_bytenr = btrfs_node_blockptr(parent, slot);
David Sterba707e8a02014-06-04 19:22:26 +02001836 blocksize = dest->nodesize;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001837 old_ptr_gen = btrfs_node_ptr_generation(parent, slot);
1838
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
David Sterbace86cd52014-06-15 01:07:32 +02001862 eb = read_tree_block(dest, old_bytenr, old_ptr_gen);
Liu Bo64c043d2015-05-25 17:30:15 +08001863 if (IS_ERR(eb)) {
1864 ret = PTR_ERR(eb);
Liu Bo264813a2016-03-21 14:59:53 -07001865 break;
Liu Bo64c043d2015-05-25 17:30:15 +08001866 } else if (!extent_buffer_uptodate(eb)) {
1867 ret = -EIO;
Josef Bacik416bc652013-04-23 14:17:42 -04001868 free_extent_buffer(eb);
Stefan Behrens379cde72013-05-08 08:56:09 +00001869 break;
Josef Bacik416bc652013-04-23 14:17:42 -04001870 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001871 btrfs_tree_lock(eb);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001872 if (cow) {
1873 ret = btrfs_cow_block(trans, dest, eb, parent,
1874 slot, &eb);
1875 BUG_ON(ret);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001876 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001877 btrfs_set_lock_blocking(eb);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001878
1879 btrfs_tree_unlock(parent);
1880 free_extent_buffer(parent);
1881
1882 parent = eb;
1883 continue;
1884 }
1885
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001886 if (!cow) {
1887 btrfs_tree_unlock(parent);
1888 free_extent_buffer(parent);
1889 cow = 1;
1890 goto again;
1891 }
1892
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001893 btrfs_node_key_to_cpu(path->nodes[level], &key,
1894 path->slots[level]);
David Sterbab3b4aa72011-04-21 01:20:15 +02001895 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001896
1897 path->lowest_level = level;
1898 ret = btrfs_search_slot(trans, src, &key, path, 0, 1);
1899 path->lowest_level = 0;
1900 BUG_ON(ret);
1901
1902 /*
1903 * swap blocks in fs tree and reloc tree.
1904 */
1905 btrfs_set_node_blockptr(parent, slot, new_bytenr);
1906 btrfs_set_node_ptr_generation(parent, slot, new_ptr_gen);
1907 btrfs_mark_buffer_dirty(parent);
1908
1909 btrfs_set_node_blockptr(path->nodes[level],
1910 path->slots[level], old_bytenr);
1911 btrfs_set_node_ptr_generation(path->nodes[level],
1912 path->slots[level], old_ptr_gen);
1913 btrfs_mark_buffer_dirty(path->nodes[level]);
1914
1915 ret = btrfs_inc_extent_ref(trans, src, old_bytenr, blocksize,
1916 path->nodes[level]->start,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01001917 src->root_key.objectid, level - 1, 0);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001918 BUG_ON(ret);
1919 ret = btrfs_inc_extent_ref(trans, dest, new_bytenr, blocksize,
1920 0, dest->root_key.objectid, level - 1,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01001921 0);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001922 BUG_ON(ret);
1923
1924 ret = btrfs_free_extent(trans, src, new_bytenr, blocksize,
1925 path->nodes[level]->start,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01001926 src->root_key.objectid, level - 1, 0);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001927 BUG_ON(ret);
1928
1929 ret = btrfs_free_extent(trans, dest, old_bytenr, blocksize,
1930 0, dest->root_key.objectid, level - 1,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01001931 0);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001932 BUG_ON(ret);
1933
1934 btrfs_unlock_up_safe(path, 0);
1935
1936 ret = level;
1937 break;
1938 }
1939 btrfs_tree_unlock(parent);
1940 free_extent_buffer(parent);
1941 return ret;
1942}
1943
1944/*
1945 * helper to find next relocated block in reloc tree
1946 */
1947static noinline_for_stack
1948int walk_up_reloc_tree(struct btrfs_root *root, struct btrfs_path *path,
1949 int *level)
1950{
1951 struct extent_buffer *eb;
1952 int i;
1953 u64 last_snapshot;
1954 u32 nritems;
1955
1956 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
1957
1958 for (i = 0; i < *level; i++) {
1959 free_extent_buffer(path->nodes[i]);
1960 path->nodes[i] = NULL;
1961 }
1962
1963 for (i = *level; i < BTRFS_MAX_LEVEL && path->nodes[i]; i++) {
1964 eb = path->nodes[i];
1965 nritems = btrfs_header_nritems(eb);
1966 while (path->slots[i] + 1 < nritems) {
1967 path->slots[i]++;
1968 if (btrfs_node_ptr_generation(eb, path->slots[i]) <=
1969 last_snapshot)
1970 continue;
1971
1972 *level = i;
1973 return 0;
1974 }
1975 free_extent_buffer(path->nodes[i]);
1976 path->nodes[i] = NULL;
1977 }
1978 return 1;
1979}
1980
1981/*
1982 * walk down reloc tree to find relocated block of lowest level
1983 */
1984static noinline_for_stack
1985int walk_down_reloc_tree(struct btrfs_root *root, struct btrfs_path *path,
1986 int *level)
1987{
1988 struct extent_buffer *eb = NULL;
1989 int i;
1990 u64 bytenr;
1991 u64 ptr_gen = 0;
1992 u64 last_snapshot;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001993 u32 nritems;
1994
1995 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
1996
1997 for (i = *level; i > 0; i--) {
1998 eb = path->nodes[i];
1999 nritems = btrfs_header_nritems(eb);
2000 while (path->slots[i] < nritems) {
2001 ptr_gen = btrfs_node_ptr_generation(eb, path->slots[i]);
2002 if (ptr_gen > last_snapshot)
2003 break;
2004 path->slots[i]++;
2005 }
2006 if (path->slots[i] >= nritems) {
2007 if (i == *level)
2008 break;
2009 *level = i + 1;
2010 return 0;
2011 }
2012 if (i == 1) {
2013 *level = i;
2014 return 0;
2015 }
2016
2017 bytenr = btrfs_node_blockptr(eb, path->slots[i]);
David Sterbace86cd52014-06-15 01:07:32 +02002018 eb = read_tree_block(root, bytenr, ptr_gen);
Liu Bo64c043d2015-05-25 17:30:15 +08002019 if (IS_ERR(eb)) {
2020 return PTR_ERR(eb);
2021 } else if (!extent_buffer_uptodate(eb)) {
Josef Bacik416bc652013-04-23 14:17:42 -04002022 free_extent_buffer(eb);
2023 return -EIO;
2024 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002025 BUG_ON(btrfs_header_level(eb) != i - 1);
2026 path->nodes[i - 1] = eb;
2027 path->slots[i - 1] = 0;
2028 }
2029 return 1;
2030}
2031
2032/*
2033 * invalidate extent cache for file extents whose key in range of
2034 * [min_key, max_key)
2035 */
2036static int invalidate_extent_cache(struct btrfs_root *root,
2037 struct btrfs_key *min_key,
2038 struct btrfs_key *max_key)
2039{
2040 struct inode *inode = NULL;
2041 u64 objectid;
2042 u64 start, end;
Li Zefan33345d012011-04-20 10:31:50 +08002043 u64 ino;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002044
2045 objectid = min_key->objectid;
2046 while (1) {
2047 cond_resched();
2048 iput(inode);
2049
2050 if (objectid > max_key->objectid)
2051 break;
2052
2053 inode = find_next_inode(root, objectid);
2054 if (!inode)
2055 break;
Li Zefan33345d012011-04-20 10:31:50 +08002056 ino = btrfs_ino(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002057
Li Zefan33345d012011-04-20 10:31:50 +08002058 if (ino > max_key->objectid) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002059 iput(inode);
2060 break;
2061 }
2062
Li Zefan33345d012011-04-20 10:31:50 +08002063 objectid = ino + 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002064 if (!S_ISREG(inode->i_mode))
2065 continue;
2066
Li Zefan33345d012011-04-20 10:31:50 +08002067 if (unlikely(min_key->objectid == ino)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002068 if (min_key->type > BTRFS_EXTENT_DATA_KEY)
2069 continue;
2070 if (min_key->type < BTRFS_EXTENT_DATA_KEY)
2071 start = 0;
2072 else {
2073 start = min_key->offset;
2074 WARN_ON(!IS_ALIGNED(start, root->sectorsize));
2075 }
2076 } else {
2077 start = 0;
2078 }
2079
Li Zefan33345d012011-04-20 10:31:50 +08002080 if (unlikely(max_key->objectid == ino)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002081 if (max_key->type < BTRFS_EXTENT_DATA_KEY)
2082 continue;
2083 if (max_key->type > BTRFS_EXTENT_DATA_KEY) {
2084 end = (u64)-1;
2085 } else {
2086 if (max_key->offset == 0)
2087 continue;
2088 end = max_key->offset;
2089 WARN_ON(!IS_ALIGNED(end, root->sectorsize));
2090 end--;
2091 }
2092 } else {
2093 end = (u64)-1;
2094 }
2095
2096 /* the lock_extent waits for readpage to complete */
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002097 lock_extent(&BTRFS_I(inode)->io_tree, start, end);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002098 btrfs_drop_extent_cache(inode, start, end, 1);
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002099 unlock_extent(&BTRFS_I(inode)->io_tree, start, end);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002100 }
2101 return 0;
2102}
2103
2104static int find_next_key(struct btrfs_path *path, int level,
2105 struct btrfs_key *key)
2106
2107{
2108 while (level < BTRFS_MAX_LEVEL) {
2109 if (!path->nodes[level])
2110 break;
2111 if (path->slots[level] + 1 <
2112 btrfs_header_nritems(path->nodes[level])) {
2113 btrfs_node_key_to_cpu(path->nodes[level], key,
2114 path->slots[level] + 1);
2115 return 0;
2116 }
2117 level++;
2118 }
2119 return 1;
2120}
2121
2122/*
2123 * merge the relocated tree blocks in reloc tree with corresponding
2124 * fs tree.
2125 */
2126static noinline_for_stack int merge_reloc_root(struct reloc_control *rc,
2127 struct btrfs_root *root)
2128{
2129 LIST_HEAD(inode_list);
2130 struct btrfs_key key;
2131 struct btrfs_key next_key;
Josef Bacik9e6a0c522013-10-31 10:07:19 -04002132 struct btrfs_trans_handle *trans = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002133 struct btrfs_root *reloc_root;
2134 struct btrfs_root_item *root_item;
2135 struct btrfs_path *path;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002136 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002137 int level;
2138 int max_level;
2139 int replaced = 0;
2140 int ret;
2141 int err = 0;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002142 u32 min_reserved;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002143
2144 path = btrfs_alloc_path();
2145 if (!path)
2146 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +01002147 path->reada = READA_FORWARD;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002148
2149 reloc_root = root->reloc_root;
2150 root_item = &reloc_root->root_item;
2151
2152 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
2153 level = btrfs_root_level(root_item);
2154 extent_buffer_get(reloc_root->node);
2155 path->nodes[level] = reloc_root->node;
2156 path->slots[level] = 0;
2157 } else {
2158 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
2159
2160 level = root_item->drop_level;
2161 BUG_ON(level == 0);
2162 path->lowest_level = level;
2163 ret = btrfs_search_slot(NULL, reloc_root, &key, path, 0, 0);
Yan Zheng33c66f42009-07-22 09:59:00 -04002164 path->lowest_level = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002165 if (ret < 0) {
2166 btrfs_free_path(path);
2167 return ret;
2168 }
2169
2170 btrfs_node_key_to_cpu(path->nodes[level], &next_key,
2171 path->slots[level]);
2172 WARN_ON(memcmp(&key, &next_key, sizeof(key)));
2173
2174 btrfs_unlock_up_safe(path, 0);
2175 }
2176
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002177 min_reserved = root->nodesize * (BTRFS_MAX_LEVEL - 1) * 2;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002178 memset(&next_key, 0, sizeof(next_key));
2179
2180 while (1) {
Miao Xie08e007d2012-10-16 11:33:38 +00002181 ret = btrfs_block_rsv_refill(root, rc->block_rsv, min_reserved,
2182 BTRFS_RESERVE_FLUSH_ALL);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002183 if (ret) {
Josef Bacik9e6a0c522013-10-31 10:07:19 -04002184 err = ret;
2185 goto out;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002186 }
Josef Bacik9e6a0c522013-10-31 10:07:19 -04002187 trans = btrfs_start_transaction(root, 0);
2188 if (IS_ERR(trans)) {
2189 err = PTR_ERR(trans);
2190 trans = NULL;
2191 goto out;
2192 }
2193 trans->block_rsv = rc->block_rsv;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002194
2195 replaced = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002196 max_level = level;
2197
2198 ret = walk_down_reloc_tree(reloc_root, path, &level);
2199 if (ret < 0) {
2200 err = ret;
2201 goto out;
2202 }
2203 if (ret > 0)
2204 break;
2205
2206 if (!find_next_key(path, level, &key) &&
2207 btrfs_comp_cpu_keys(&next_key, &key) >= 0) {
2208 ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002209 } else {
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002210 ret = replace_path(trans, root, reloc_root, path,
2211 &next_key, level, max_level);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002212 }
2213 if (ret < 0) {
2214 err = ret;
2215 goto out;
2216 }
2217
2218 if (ret > 0) {
2219 level = ret;
2220 btrfs_node_key_to_cpu(path->nodes[level], &key,
2221 path->slots[level]);
2222 replaced = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002223 }
2224
2225 ret = walk_up_reloc_tree(reloc_root, path, &level);
2226 if (ret > 0)
2227 break;
2228
2229 BUG_ON(level == 0);
2230 /*
2231 * save the merging progress in the drop_progress.
2232 * this is OK since root refs == 1 in this case.
2233 */
2234 btrfs_node_key(path->nodes[level], &root_item->drop_progress,
2235 path->slots[level]);
2236 root_item->drop_level = level;
2237
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002238 btrfs_end_transaction_throttle(trans, root);
Josef Bacik9e6a0c522013-10-31 10:07:19 -04002239 trans = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002240
Liu Bob53d3f52012-11-14 14:34:34 +00002241 btrfs_btree_balance_dirty(root);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002242
2243 if (replaced && rc->stage == UPDATE_DATA_PTRS)
2244 invalidate_extent_cache(root, &key, &next_key);
2245 }
2246
2247 /*
2248 * handle the case only one block in the fs tree need to be
2249 * relocated and the block is tree root.
2250 */
2251 leaf = btrfs_lock_root_node(root);
2252 ret = btrfs_cow_block(trans, root, leaf, NULL, 0, &leaf);
2253 btrfs_tree_unlock(leaf);
2254 free_extent_buffer(leaf);
2255 if (ret < 0)
2256 err = ret;
2257out:
2258 btrfs_free_path(path);
2259
2260 if (err == 0) {
2261 memset(&root_item->drop_progress, 0,
2262 sizeof(root_item->drop_progress));
2263 root_item->drop_level = 0;
2264 btrfs_set_root_refs(root_item, 0);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002265 btrfs_update_reloc_root(trans, root);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002266 }
2267
Josef Bacik9e6a0c522013-10-31 10:07:19 -04002268 if (trans)
2269 btrfs_end_transaction_throttle(trans, root);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002270
Liu Bob53d3f52012-11-14 14:34:34 +00002271 btrfs_btree_balance_dirty(root);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002272
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002273 if (replaced && rc->stage == UPDATE_DATA_PTRS)
2274 invalidate_extent_cache(root, &key, &next_key);
2275
2276 return err;
2277}
2278
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002279static noinline_for_stack
2280int prepare_to_merge(struct reloc_control *rc, int err)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002281{
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002282 struct btrfs_root *root = rc->extent_root;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002283 struct btrfs_root *reloc_root;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002284 struct btrfs_trans_handle *trans;
2285 LIST_HEAD(reloc_roots);
2286 u64 num_bytes = 0;
2287 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002288
Chris Mason75857172011-06-13 20:00:16 -04002289 mutex_lock(&root->fs_info->reloc_mutex);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002290 rc->merging_rsv_size += root->nodesize * (BTRFS_MAX_LEVEL - 1) * 2;
2291 rc->merging_rsv_size += rc->nodes_relocated * 2;
Chris Mason75857172011-06-13 20:00:16 -04002292 mutex_unlock(&root->fs_info->reloc_mutex);
2293
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002294again:
2295 if (!err) {
2296 num_bytes = rc->merging_rsv_size;
Miao Xie08e007d2012-10-16 11:33:38 +00002297 ret = btrfs_block_rsv_add(root, rc->block_rsv, num_bytes,
2298 BTRFS_RESERVE_FLUSH_ALL);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002299 if (ret)
2300 err = ret;
2301 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002302
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002303 trans = btrfs_join_transaction(rc->extent_root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00002304 if (IS_ERR(trans)) {
2305 if (!err)
2306 btrfs_block_rsv_release(rc->extent_root,
2307 rc->block_rsv, num_bytes);
2308 return PTR_ERR(trans);
2309 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002310
2311 if (!err) {
2312 if (num_bytes != rc->merging_rsv_size) {
2313 btrfs_end_transaction(trans, rc->extent_root);
2314 btrfs_block_rsv_release(rc->extent_root,
2315 rc->block_rsv, num_bytes);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002316 goto again;
2317 }
2318 }
2319
2320 rc->merge_reloc_tree = 1;
2321
2322 while (!list_empty(&rc->reloc_roots)) {
2323 reloc_root = list_entry(rc->reloc_roots.next,
2324 struct btrfs_root, root_list);
2325 list_del_init(&reloc_root->root_list);
2326
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002327 root = read_fs_root(reloc_root->fs_info,
2328 reloc_root->root_key.offset);
2329 BUG_ON(IS_ERR(root));
2330 BUG_ON(root->reloc_root != reloc_root);
2331
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002332 /*
2333 * set reference count to 1, so btrfs_recover_relocation
2334 * knows it should resumes merging
2335 */
2336 if (!err)
2337 btrfs_set_root_refs(&reloc_root->root_item, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002338 btrfs_update_reloc_root(trans, root);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002339
2340 list_add(&reloc_root->root_list, &reloc_roots);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002341 }
2342
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002343 list_splice(&reloc_roots, &rc->reloc_roots);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002344
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002345 if (!err)
2346 btrfs_commit_transaction(trans, rc->extent_root);
2347 else
2348 btrfs_end_transaction(trans, rc->extent_root);
2349 return err;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002350}
2351
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002352static noinline_for_stack
Liu Boaca1bba2013-03-04 16:25:37 +00002353void free_reloc_roots(struct list_head *list)
2354{
2355 struct btrfs_root *reloc_root;
2356
2357 while (!list_empty(list)) {
2358 reloc_root = list_entry(list->next, struct btrfs_root,
2359 root_list);
Josef Bacik6bdf1312016-09-02 15:25:43 -04002360 free_extent_buffer(reloc_root->node);
2361 free_extent_buffer(reloc_root->commit_root);
2362 reloc_root->node = NULL;
2363 reloc_root->commit_root = NULL;
Wang Shilongc974c462013-12-11 19:29:51 +08002364 __del_reloc_root(reloc_root);
Liu Boaca1bba2013-03-04 16:25:37 +00002365 }
2366}
2367
2368static noinline_for_stack
David Sterba94404e82014-07-30 01:53:30 +02002369void merge_reloc_roots(struct reloc_control *rc)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002370{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002371 struct btrfs_root *root;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002372 struct btrfs_root *reloc_root;
Miao Xie5bc72472013-06-06 03:28:03 +00002373 u64 last_snap;
2374 u64 otransid;
2375 u64 objectid;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002376 LIST_HEAD(reloc_roots);
2377 int found = 0;
Liu Boaca1bba2013-03-04 16:25:37 +00002378 int ret = 0;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002379again:
2380 root = rc->extent_root;
Chris Mason75857172011-06-13 20:00:16 -04002381
2382 /*
2383 * this serializes us with btrfs_record_root_in_transaction,
2384 * we have to make sure nobody is in the middle of
2385 * adding their roots to the list while we are
2386 * doing this splice
2387 */
2388 mutex_lock(&root->fs_info->reloc_mutex);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002389 list_splice_init(&rc->reloc_roots, &reloc_roots);
Chris Mason75857172011-06-13 20:00:16 -04002390 mutex_unlock(&root->fs_info->reloc_mutex);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002391
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002392 while (!list_empty(&reloc_roots)) {
2393 found = 1;
2394 reloc_root = list_entry(reloc_roots.next,
2395 struct btrfs_root, root_list);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002396
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002397 if (btrfs_root_refs(&reloc_root->root_item) > 0) {
2398 root = read_fs_root(reloc_root->fs_info,
2399 reloc_root->root_key.offset);
2400 BUG_ON(IS_ERR(root));
2401 BUG_ON(root->reloc_root != reloc_root);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002402
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002403 ret = merge_reloc_root(rc, root);
Josef Bacikb37b39c2013-07-23 16:57:15 -04002404 if (ret) {
Wang Shilong25e293c2013-12-26 13:10:50 +08002405 if (list_empty(&reloc_root->root_list))
2406 list_add_tail(&reloc_root->root_list,
2407 &reloc_roots);
Liu Boaca1bba2013-03-04 16:25:37 +00002408 goto out;
Josef Bacikb37b39c2013-07-23 16:57:15 -04002409 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002410 } else {
2411 list_del_init(&reloc_root->root_list);
2412 }
Miao Xie5bc72472013-06-06 03:28:03 +00002413
2414 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04002415 * we keep the old last snapshot transid in rtranid when we
Miao Xie5bc72472013-06-06 03:28:03 +00002416 * created the relocation tree.
2417 */
2418 last_snap = btrfs_root_rtransid(&reloc_root->root_item);
2419 otransid = btrfs_root_otransid(&reloc_root->root_item);
2420 objectid = reloc_root->root_key.offset;
2421
Jeff Mahoney2c536792011-10-03 23:22:41 -04002422 ret = btrfs_drop_snapshot(reloc_root, rc->block_rsv, 0, 1);
Liu Boaca1bba2013-03-04 16:25:37 +00002423 if (ret < 0) {
2424 if (list_empty(&reloc_root->root_list))
2425 list_add_tail(&reloc_root->root_list,
2426 &reloc_roots);
2427 goto out;
2428 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002429 }
2430
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002431 if (found) {
2432 found = 0;
2433 goto again;
2434 }
Liu Boaca1bba2013-03-04 16:25:37 +00002435out:
2436 if (ret) {
Anand Jain34d97002016-03-16 16:43:06 +08002437 btrfs_handle_fs_error(root->fs_info, ret, NULL);
Liu Boaca1bba2013-03-04 16:25:37 +00002438 if (!list_empty(&reloc_roots))
2439 free_reloc_roots(&reloc_roots);
Wang Shilong467bb1d2013-12-11 19:29:52 +08002440
2441 /* new reloc root may be added */
2442 mutex_lock(&root->fs_info->reloc_mutex);
2443 list_splice_init(&rc->reloc_roots, &reloc_roots);
2444 mutex_unlock(&root->fs_info->reloc_mutex);
2445 if (!list_empty(&reloc_roots))
2446 free_reloc_roots(&reloc_roots);
Liu Boaca1bba2013-03-04 16:25:37 +00002447 }
2448
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002449 BUG_ON(!RB_EMPTY_ROOT(&rc->reloc_root_tree.rb_root));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002450}
2451
2452static void free_block_list(struct rb_root *blocks)
2453{
2454 struct tree_block *block;
2455 struct rb_node *rb_node;
2456 while ((rb_node = rb_first(blocks))) {
2457 block = rb_entry(rb_node, struct tree_block, rb_node);
2458 rb_erase(rb_node, blocks);
2459 kfree(block);
2460 }
2461}
2462
2463static int record_reloc_root_in_trans(struct btrfs_trans_handle *trans,
2464 struct btrfs_root *reloc_root)
2465{
2466 struct btrfs_root *root;
2467
2468 if (reloc_root->last_trans == trans->transid)
2469 return 0;
2470
2471 root = read_fs_root(reloc_root->fs_info, reloc_root->root_key.offset);
2472 BUG_ON(IS_ERR(root));
2473 BUG_ON(root->reloc_root != reloc_root);
2474
2475 return btrfs_record_root_in_trans(trans, root);
2476}
2477
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002478static noinline_for_stack
2479struct btrfs_root *select_reloc_root(struct btrfs_trans_handle *trans,
2480 struct reloc_control *rc,
2481 struct backref_node *node,
Wang Shilongdc4103f2013-12-26 13:10:49 +08002482 struct backref_edge *edges[])
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002483{
2484 struct backref_node *next;
2485 struct btrfs_root *root;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002486 int index = 0;
2487
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002488 next = node;
2489 while (1) {
2490 cond_resched();
2491 next = walk_up_backref(next, edges, &index);
2492 root = next->root;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002493 BUG_ON(!root);
Miao Xie27cdeb72014-04-02 19:51:05 +08002494 BUG_ON(!test_bit(BTRFS_ROOT_REF_COWS, &root->state));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002495
2496 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
2497 record_reloc_root_in_trans(trans, root);
2498 break;
2499 }
2500
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002501 btrfs_record_root_in_trans(trans, root);
2502 root = root->reloc_root;
2503
2504 if (next->new_bytenr != root->node->start) {
2505 BUG_ON(next->new_bytenr);
2506 BUG_ON(!list_empty(&next->list));
2507 next->new_bytenr = root->node->start;
2508 next->root = root;
2509 list_add_tail(&next->list,
2510 &rc->backref_cache.changed);
2511 __mark_block_processed(rc, next);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002512 break;
2513 }
2514
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002515 WARN_ON(1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002516 root = NULL;
2517 next = walk_down_backref(edges, &index);
2518 if (!next || next->level <= node->level)
2519 break;
2520 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002521 if (!root)
2522 return NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002523
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002524 next = node;
2525 /* setup backref node path for btrfs_reloc_cow_block */
2526 while (1) {
2527 rc->backref_cache.path[next->level] = next;
2528 if (--index < 0)
2529 break;
2530 next = edges[index]->node[UPPER];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002531 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002532 return root;
2533}
2534
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002535/*
2536 * select a tree root for relocation. return NULL if the block
2537 * is reference counted. we should use do_relocation() in this
2538 * case. return a tree root pointer if the block isn't reference
2539 * counted. return -ENOENT if the block is root of reloc tree.
2540 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002541static noinline_for_stack
Zhaolei147d2562015-08-06 20:58:11 +08002542struct btrfs_root *select_one_root(struct backref_node *node)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002543{
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002544 struct backref_node *next;
2545 struct btrfs_root *root;
2546 struct btrfs_root *fs_root = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002547 struct backref_edge *edges[BTRFS_MAX_LEVEL - 1];
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002548 int index = 0;
2549
2550 next = node;
2551 while (1) {
2552 cond_resched();
2553 next = walk_up_backref(next, edges, &index);
2554 root = next->root;
2555 BUG_ON(!root);
2556
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002557 /* no other choice for non-references counted tree */
Miao Xie27cdeb72014-04-02 19:51:05 +08002558 if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state))
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002559 return root;
2560
2561 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID)
2562 fs_root = root;
2563
2564 if (next != node)
2565 return NULL;
2566
2567 next = walk_down_backref(edges, &index);
2568 if (!next || next->level <= node->level)
2569 break;
2570 }
2571
2572 if (!fs_root)
2573 return ERR_PTR(-ENOENT);
2574 return fs_root;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002575}
2576
2577static noinline_for_stack
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002578u64 calcu_metadata_size(struct reloc_control *rc,
2579 struct backref_node *node, int reserve)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002580{
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002581 struct backref_node *next = node;
2582 struct backref_edge *edge;
2583 struct backref_edge *edges[BTRFS_MAX_LEVEL - 1];
2584 u64 num_bytes = 0;
2585 int index = 0;
2586
2587 BUG_ON(reserve && node->processed);
2588
2589 while (next) {
2590 cond_resched();
2591 while (1) {
2592 if (next->processed && (reserve || next != node))
2593 break;
2594
David Sterba707e8a02014-06-04 19:22:26 +02002595 num_bytes += rc->extent_root->nodesize;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002596
2597 if (list_empty(&next->upper))
2598 break;
2599
2600 edge = list_entry(next->upper.next,
2601 struct backref_edge, list[LOWER]);
2602 edges[index++] = edge;
2603 next = edge->node[UPPER];
2604 }
2605 next = walk_down_backref(edges, &index);
2606 }
2607 return num_bytes;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002608}
2609
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002610static int reserve_metadata_space(struct btrfs_trans_handle *trans,
2611 struct reloc_control *rc,
2612 struct backref_node *node)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002613{
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002614 struct btrfs_root *root = rc->extent_root;
2615 u64 num_bytes;
2616 int ret;
Wang Shilong0647bf52013-11-20 09:01:52 +08002617 u64 tmp;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002618
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002619 num_bytes = calcu_metadata_size(rc, node, 1) * 2;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002620
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002621 trans->block_rsv = rc->block_rsv;
Wang Shilong0647bf52013-11-20 09:01:52 +08002622 rc->reserved_bytes += num_bytes;
Josef Bacik8ca17f02016-05-27 13:24:13 -04002623
2624 /*
2625 * We are under a transaction here so we can only do limited flushing.
2626 * If we get an enospc just kick back -EAGAIN so we know to drop the
2627 * transaction and try to refill when we can flush all the things.
2628 */
Wang Shilong0647bf52013-11-20 09:01:52 +08002629 ret = btrfs_block_rsv_refill(root, rc->block_rsv, num_bytes,
Josef Bacik8ca17f02016-05-27 13:24:13 -04002630 BTRFS_RESERVE_FLUSH_LIMIT);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002631 if (ret) {
Josef Bacik8ca17f02016-05-27 13:24:13 -04002632 tmp = rc->extent_root->nodesize * RELOCATION_RESERVED_NODES;
2633 while (tmp <= rc->reserved_bytes)
2634 tmp <<= 1;
2635 /*
2636 * only one thread can access block_rsv at this point,
2637 * so we don't need hold lock to protect block_rsv.
2638 * we expand more reservation size here to allow enough
2639 * space for relocation and we will return eailer in
2640 * enospc case.
2641 */
2642 rc->block_rsv->size = tmp + rc->extent_root->nodesize *
2643 RELOCATION_RESERVED_NODES;
2644 return -EAGAIN;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002645 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002646
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002647 return 0;
2648}
2649
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002650/*
2651 * relocate a block tree, and then update pointers in upper level
2652 * blocks that reference the block to point to the new location.
2653 *
2654 * if called by link_to_upper, the block has already been relocated.
2655 * in that case this function just updates pointers.
2656 */
2657static int do_relocation(struct btrfs_trans_handle *trans,
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002658 struct reloc_control *rc,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002659 struct backref_node *node,
2660 struct btrfs_key *key,
2661 struct btrfs_path *path, int lowest)
2662{
2663 struct backref_node *upper;
2664 struct backref_edge *edge;
2665 struct backref_edge *edges[BTRFS_MAX_LEVEL - 1];
2666 struct btrfs_root *root;
2667 struct extent_buffer *eb;
2668 u32 blocksize;
2669 u64 bytenr;
2670 u64 generation;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002671 int slot;
2672 int ret;
2673 int err = 0;
2674
2675 BUG_ON(lowest && node->eb);
2676
2677 path->lowest_level = node->level + 1;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002678 rc->backref_cache.path[node->level] = node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002679 list_for_each_entry(edge, &node->upper, list[LOWER]) {
2680 cond_resched();
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002681
2682 upper = edge->node[UPPER];
Wang Shilongdc4103f2013-12-26 13:10:49 +08002683 root = select_reloc_root(trans, rc, upper, edges);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002684 BUG_ON(!root);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002685
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002686 if (upper->eb && !upper->locked) {
2687 if (!lowest) {
2688 ret = btrfs_bin_search(upper->eb, key,
2689 upper->level, &slot);
2690 BUG_ON(ret);
2691 bytenr = btrfs_node_blockptr(upper->eb, slot);
2692 if (node->eb->start == bytenr)
2693 goto next;
2694 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002695 drop_node_buffer(upper);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002696 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002697
2698 if (!upper->eb) {
2699 ret = btrfs_search_slot(trans, root, key, path, 0, 1);
Liu Bo3561b9d2016-09-14 08:51:46 -07002700 if (ret) {
2701 if (ret < 0)
2702 err = ret;
2703 else
2704 err = -ENOENT;
2705
2706 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002707 break;
2708 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002709
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002710 if (!upper->eb) {
2711 upper->eb = path->nodes[upper->level];
2712 path->nodes[upper->level] = NULL;
2713 } else {
2714 BUG_ON(upper->eb != path->nodes[upper->level]);
2715 }
2716
2717 upper->locked = 1;
2718 path->locks[upper->level] = 0;
2719
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002720 slot = path->slots[upper->level];
David Sterbab3b4aa72011-04-21 01:20:15 +02002721 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002722 } else {
2723 ret = btrfs_bin_search(upper->eb, key, upper->level,
2724 &slot);
2725 BUG_ON(ret);
2726 }
2727
2728 bytenr = btrfs_node_blockptr(upper->eb, slot);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002729 if (lowest) {
2730 BUG_ON(bytenr != node->bytenr);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002731 } else {
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002732 if (node->eb->start == bytenr)
2733 goto next;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002734 }
2735
David Sterba707e8a02014-06-04 19:22:26 +02002736 blocksize = root->nodesize;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002737 generation = btrfs_node_ptr_generation(upper->eb, slot);
David Sterbace86cd52014-06-15 01:07:32 +02002738 eb = read_tree_block(root, bytenr, generation);
Liu Bo64c043d2015-05-25 17:30:15 +08002739 if (IS_ERR(eb)) {
2740 err = PTR_ERR(eb);
2741 goto next;
2742 } else if (!extent_buffer_uptodate(eb)) {
Josef Bacik416bc652013-04-23 14:17:42 -04002743 free_extent_buffer(eb);
Tsutomu Itoh97d9a8a2011-03-24 06:33:21 +00002744 err = -EIO;
2745 goto next;
2746 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002747 btrfs_tree_lock(eb);
2748 btrfs_set_lock_blocking(eb);
2749
2750 if (!node->eb) {
2751 ret = btrfs_cow_block(trans, root, eb, upper->eb,
2752 slot, &eb);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002753 btrfs_tree_unlock(eb);
2754 free_extent_buffer(eb);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002755 if (ret < 0) {
2756 err = ret;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002757 goto next;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002758 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002759 BUG_ON(node->eb != eb);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002760 } else {
2761 btrfs_set_node_blockptr(upper->eb, slot,
2762 node->eb->start);
2763 btrfs_set_node_ptr_generation(upper->eb, slot,
2764 trans->transid);
2765 btrfs_mark_buffer_dirty(upper->eb);
2766
2767 ret = btrfs_inc_extent_ref(trans, root,
2768 node->eb->start, blocksize,
2769 upper->eb->start,
2770 btrfs_header_owner(upper->eb),
Filipe Mananab06c4bf2015-10-23 07:52:54 +01002771 node->level, 0);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002772 BUG_ON(ret);
2773
2774 ret = btrfs_drop_subtree(trans, root, eb, upper->eb);
2775 BUG_ON(ret);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002776 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002777next:
2778 if (!upper->pending)
2779 drop_node_buffer(upper);
2780 else
2781 unlock_node_buffer(upper);
2782 if (err)
2783 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002784 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002785
2786 if (!err && node->pending) {
2787 drop_node_buffer(node);
2788 list_move_tail(&node->list, &rc->backref_cache.changed);
2789 node->pending = 0;
2790 }
2791
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002792 path->lowest_level = 0;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002793 BUG_ON(err == -ENOSPC);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002794 return err;
2795}
2796
2797static int link_to_upper(struct btrfs_trans_handle *trans,
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002798 struct reloc_control *rc,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002799 struct backref_node *node,
2800 struct btrfs_path *path)
2801{
2802 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002803
2804 btrfs_node_key_to_cpu(node->eb, &key, 0);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002805 return do_relocation(trans, rc, node, &key, path, 0);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002806}
2807
2808static int finish_pending_nodes(struct btrfs_trans_handle *trans,
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002809 struct reloc_control *rc,
2810 struct btrfs_path *path, int err)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002811{
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002812 LIST_HEAD(list);
2813 struct backref_cache *cache = &rc->backref_cache;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002814 struct backref_node *node;
2815 int level;
2816 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002817
2818 for (level = 0; level < BTRFS_MAX_LEVEL; level++) {
2819 while (!list_empty(&cache->pending[level])) {
2820 node = list_entry(cache->pending[level].next,
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002821 struct backref_node, list);
2822 list_move_tail(&node->list, &list);
2823 BUG_ON(!node->pending);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002824
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002825 if (!err) {
2826 ret = link_to_upper(trans, rc, node, path);
2827 if (ret < 0)
2828 err = ret;
2829 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002830 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002831 list_splice_init(&list, &cache->pending[level]);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002832 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002833 return err;
2834}
2835
2836static void mark_block_processed(struct reloc_control *rc,
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002837 u64 bytenr, u32 blocksize)
2838{
2839 set_extent_bits(&rc->processed_blocks, bytenr, bytenr + blocksize - 1,
David Sterbaceeb0ae2016-04-26 23:54:39 +02002840 EXTENT_DIRTY);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002841}
2842
2843static void __mark_block_processed(struct reloc_control *rc,
2844 struct backref_node *node)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002845{
2846 u32 blocksize;
2847 if (node->level == 0 ||
2848 in_block_group(node->bytenr, rc->block_group)) {
David Sterba707e8a02014-06-04 19:22:26 +02002849 blocksize = rc->extent_root->nodesize;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002850 mark_block_processed(rc, node->bytenr, blocksize);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002851 }
2852 node->processed = 1;
2853}
2854
2855/*
2856 * mark a block and all blocks directly/indirectly reference the block
2857 * as processed.
2858 */
2859static void update_processed_blocks(struct reloc_control *rc,
2860 struct backref_node *node)
2861{
2862 struct backref_node *next = node;
2863 struct backref_edge *edge;
2864 struct backref_edge *edges[BTRFS_MAX_LEVEL - 1];
2865 int index = 0;
2866
2867 while (next) {
2868 cond_resched();
2869 while (1) {
2870 if (next->processed)
2871 break;
2872
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002873 __mark_block_processed(rc, next);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002874
2875 if (list_empty(&next->upper))
2876 break;
2877
2878 edge = list_entry(next->upper.next,
2879 struct backref_edge, list[LOWER]);
2880 edges[index++] = edge;
2881 next = edge->node[UPPER];
2882 }
2883 next = walk_down_backref(edges, &index);
2884 }
2885}
2886
David Sterba7476dfd2014-06-15 03:34:59 +02002887static int tree_block_processed(u64 bytenr, struct reloc_control *rc)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002888{
David Sterba7476dfd2014-06-15 03:34:59 +02002889 u32 blocksize = rc->extent_root->nodesize;
2890
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002891 if (test_range_bit(&rc->processed_blocks, bytenr,
Chris Mason9655d292009-09-02 15:22:30 -04002892 bytenr + blocksize - 1, EXTENT_DIRTY, 1, NULL))
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002893 return 1;
2894 return 0;
2895}
2896
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002897static int get_tree_block_key(struct reloc_control *rc,
2898 struct tree_block *block)
2899{
2900 struct extent_buffer *eb;
2901
2902 BUG_ON(block->key_ready);
2903 eb = read_tree_block(rc->extent_root, block->bytenr,
David Sterbace86cd52014-06-15 01:07:32 +02002904 block->key.offset);
Liu Bo64c043d2015-05-25 17:30:15 +08002905 if (IS_ERR(eb)) {
2906 return PTR_ERR(eb);
2907 } else if (!extent_buffer_uptodate(eb)) {
Josef Bacik416bc652013-04-23 14:17:42 -04002908 free_extent_buffer(eb);
2909 return -EIO;
2910 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002911 WARN_ON(btrfs_header_level(eb) != block->level);
2912 if (block->level == 0)
2913 btrfs_item_key_to_cpu(eb, &block->key, 0);
2914 else
2915 btrfs_node_key_to_cpu(eb, &block->key, 0);
2916 free_extent_buffer(eb);
2917 block->key_ready = 1;
2918 return 0;
2919}
2920
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002921/*
2922 * helper function to relocate a tree block
2923 */
2924static int relocate_tree_block(struct btrfs_trans_handle *trans,
2925 struct reloc_control *rc,
2926 struct backref_node *node,
2927 struct btrfs_key *key,
2928 struct btrfs_path *path)
2929{
2930 struct btrfs_root *root;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002931 int ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002932
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002933 if (!node)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002934 return 0;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002935
2936 BUG_ON(node->processed);
Zhaolei147d2562015-08-06 20:58:11 +08002937 root = select_one_root(node);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002938 if (root == ERR_PTR(-ENOENT)) {
2939 update_processed_blocks(rc, node);
2940 goto out;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002941 }
2942
Miao Xie27cdeb72014-04-02 19:51:05 +08002943 if (!root || test_bit(BTRFS_ROOT_REF_COWS, &root->state)) {
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002944 ret = reserve_metadata_space(trans, rc, node);
2945 if (ret)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002946 goto out;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002947 }
2948
2949 if (root) {
Miao Xie27cdeb72014-04-02 19:51:05 +08002950 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state)) {
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002951 BUG_ON(node->new_bytenr);
2952 BUG_ON(!list_empty(&node->list));
2953 btrfs_record_root_in_trans(trans, root);
2954 root = root->reloc_root;
2955 node->new_bytenr = root->node->start;
2956 node->root = root;
2957 list_add_tail(&node->list, &rc->backref_cache.changed);
2958 } else {
2959 path->lowest_level = node->level;
2960 ret = btrfs_search_slot(trans, root, key, path, 0, 1);
David Sterbab3b4aa72011-04-21 01:20:15 +02002961 btrfs_release_path(path);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002962 if (ret > 0)
2963 ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002964 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002965 if (!ret)
2966 update_processed_blocks(rc, node);
2967 } else {
2968 ret = do_relocation(trans, rc, node, key, path, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002969 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002970out:
Wang Shilong0647bf52013-11-20 09:01:52 +08002971 if (ret || node->level == 0 || node->cowonly)
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002972 remove_backref_node(&rc->backref_cache, node);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002973 return ret;
2974}
2975
2976/*
2977 * relocate a list of blocks
2978 */
2979static noinline_for_stack
2980int relocate_tree_blocks(struct btrfs_trans_handle *trans,
2981 struct reloc_control *rc, struct rb_root *blocks)
2982{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002983 struct backref_node *node;
2984 struct btrfs_path *path;
2985 struct tree_block *block;
2986 struct rb_node *rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002987 int ret;
2988 int err = 0;
2989
2990 path = btrfs_alloc_path();
Liu Boe1a12672013-03-04 16:25:38 +00002991 if (!path) {
2992 err = -ENOMEM;
David Sterba34c2b292013-04-26 12:56:04 +00002993 goto out_free_blocks;
Liu Boe1a12672013-03-04 16:25:38 +00002994 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002995
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002996 rb_node = rb_first(blocks);
2997 while (rb_node) {
2998 block = rb_entry(rb_node, struct tree_block, rb_node);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002999 if (!block->key_ready)
David Sterbad3e46fe2014-06-15 02:04:19 +02003000 readahead_tree_block(rc->extent_root, block->bytenr);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003001 rb_node = rb_next(rb_node);
3002 }
3003
3004 rb_node = rb_first(blocks);
3005 while (rb_node) {
3006 block = rb_entry(rb_node, struct tree_block, rb_node);
David Sterba34c2b292013-04-26 12:56:04 +00003007 if (!block->key_ready) {
3008 err = get_tree_block_key(rc, block);
3009 if (err)
3010 goto out_free_path;
3011 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003012 rb_node = rb_next(rb_node);
3013 }
3014
3015 rb_node = rb_first(blocks);
3016 while (rb_node) {
3017 block = rb_entry(rb_node, struct tree_block, rb_node);
3018
Yan, Zheng3fd0a552010-05-16 10:49:59 -04003019 node = build_backref_tree(rc, &block->key,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003020 block->level, block->bytenr);
3021 if (IS_ERR(node)) {
3022 err = PTR_ERR(node);
3023 goto out;
3024 }
3025
3026 ret = relocate_tree_block(trans, rc, node, &block->key,
3027 path);
3028 if (ret < 0) {
Yan, Zheng3fd0a552010-05-16 10:49:59 -04003029 if (ret != -EAGAIN || rb_node == rb_first(blocks))
3030 err = ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003031 goto out;
3032 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003033 rb_node = rb_next(rb_node);
3034 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003035out:
Yan, Zheng3fd0a552010-05-16 10:49:59 -04003036 err = finish_pending_nodes(trans, rc, path, err);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003037
David Sterba34c2b292013-04-26 12:56:04 +00003038out_free_path:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003039 btrfs_free_path(path);
David Sterba34c2b292013-04-26 12:56:04 +00003040out_free_blocks:
Liu Boe1a12672013-03-04 16:25:38 +00003041 free_block_list(blocks);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003042 return err;
3043}
3044
3045static noinline_for_stack
Yan, Zhengefa56462010-05-16 10:49:59 -04003046int prealloc_file_extent_cluster(struct inode *inode,
3047 struct file_extent_cluster *cluster)
3048{
3049 u64 alloc_hint = 0;
3050 u64 start;
3051 u64 end;
3052 u64 offset = BTRFS_I(inode)->index_cnt;
3053 u64 num_bytes;
3054 int nr = 0;
3055 int ret = 0;
Wang Xiaoguangdcb40c12016-07-25 15:51:38 +08003056 u64 prealloc_start = cluster->start - offset;
3057 u64 prealloc_end = cluster->end - offset;
Wang Xiaoguang18513092016-07-25 15:51:40 +08003058 u64 cur_offset;
Yan, Zhengefa56462010-05-16 10:49:59 -04003059
3060 BUG_ON(cluster->start != cluster->boundary[0]);
Al Viro59551022016-01-22 15:40:57 -05003061 inode_lock(inode);
Yan, Zhengefa56462010-05-16 10:49:59 -04003062
Wang Xiaoguangdcb40c12016-07-25 15:51:38 +08003063 ret = btrfs_check_data_free_space(inode, prealloc_start,
3064 prealloc_end + 1 - prealloc_start);
Yan, Zhengefa56462010-05-16 10:49:59 -04003065 if (ret)
3066 goto out;
3067
Wang Xiaoguang18513092016-07-25 15:51:40 +08003068 cur_offset = prealloc_start;
Yan, Zhengefa56462010-05-16 10:49:59 -04003069 while (nr < cluster->nr) {
3070 start = cluster->boundary[nr] - offset;
3071 if (nr + 1 < cluster->nr)
3072 end = cluster->boundary[nr + 1] - 1 - offset;
3073 else
3074 end = cluster->end - offset;
3075
Jeff Mahoneyd0082372012-03-01 14:57:19 +01003076 lock_extent(&BTRFS_I(inode)->io_tree, start, end);
Yan, Zhengefa56462010-05-16 10:49:59 -04003077 num_bytes = end + 1 - start;
Wang Xiaoguang18513092016-07-25 15:51:40 +08003078 if (cur_offset < start)
3079 btrfs_free_reserved_data_space(inode, cur_offset,
3080 start - cur_offset);
Yan, Zhengefa56462010-05-16 10:49:59 -04003081 ret = btrfs_prealloc_file_range(inode, 0, start,
3082 num_bytes, num_bytes,
3083 end + 1, &alloc_hint);
Wang Xiaoguang18513092016-07-25 15:51:40 +08003084 cur_offset = end + 1;
Jeff Mahoneyd0082372012-03-01 14:57:19 +01003085 unlock_extent(&BTRFS_I(inode)->io_tree, start, end);
Yan, Zhengefa56462010-05-16 10:49:59 -04003086 if (ret)
3087 break;
3088 nr++;
3089 }
Wang Xiaoguang18513092016-07-25 15:51:40 +08003090 if (cur_offset < prealloc_end)
3091 btrfs_free_reserved_data_space(inode, cur_offset,
3092 prealloc_end + 1 - cur_offset);
Yan, Zhengefa56462010-05-16 10:49:59 -04003093out:
Al Viro59551022016-01-22 15:40:57 -05003094 inode_unlock(inode);
Yan, Zhengefa56462010-05-16 10:49:59 -04003095 return ret;
3096}
3097
3098static noinline_for_stack
Yan, Zheng0257bb82009-09-24 09:17:31 -04003099int setup_extent_mapping(struct inode *inode, u64 start, u64 end,
3100 u64 block_start)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003101{
3102 struct btrfs_root *root = BTRFS_I(inode)->root;
3103 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
3104 struct extent_map *em;
Yan, Zheng0257bb82009-09-24 09:17:31 -04003105 int ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003106
David Sterba172ddd62011-04-21 00:48:27 +02003107 em = alloc_extent_map();
Yan, Zheng0257bb82009-09-24 09:17:31 -04003108 if (!em)
3109 return -ENOMEM;
3110
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003111 em->start = start;
Yan, Zheng0257bb82009-09-24 09:17:31 -04003112 em->len = end + 1 - start;
3113 em->block_len = em->len;
3114 em->block_start = block_start;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003115 em->bdev = root->fs_info->fs_devices->latest_bdev;
3116 set_bit(EXTENT_FLAG_PINNED, &em->flags);
3117
Jeff Mahoneyd0082372012-03-01 14:57:19 +01003118 lock_extent(&BTRFS_I(inode)->io_tree, start, end);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003119 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04003120 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04003121 ret = add_extent_mapping(em_tree, em, 0);
Chris Mason890871b2009-09-02 16:24:52 -04003122 write_unlock(&em_tree->lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003123 if (ret != -EEXIST) {
3124 free_extent_map(em);
3125 break;
3126 }
3127 btrfs_drop_extent_cache(inode, start, end, 0);
3128 }
Jeff Mahoneyd0082372012-03-01 14:57:19 +01003129 unlock_extent(&BTRFS_I(inode)->io_tree, start, end);
Yan, Zheng0257bb82009-09-24 09:17:31 -04003130 return ret;
3131}
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003132
Yan, Zheng0257bb82009-09-24 09:17:31 -04003133static int relocate_file_extent_cluster(struct inode *inode,
3134 struct file_extent_cluster *cluster)
3135{
3136 u64 page_start;
3137 u64 page_end;
3138 u64 offset = BTRFS_I(inode)->index_cnt;
3139 unsigned long index;
3140 unsigned long last_index;
Yan, Zheng0257bb82009-09-24 09:17:31 -04003141 struct page *page;
3142 struct file_ra_state *ra;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04003143 gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
Yan, Zheng0257bb82009-09-24 09:17:31 -04003144 int nr = 0;
3145 int ret = 0;
3146
3147 if (!cluster->nr)
3148 return 0;
3149
3150 ra = kzalloc(sizeof(*ra), GFP_NOFS);
3151 if (!ra)
3152 return -ENOMEM;
3153
Yan, Zhengefa56462010-05-16 10:49:59 -04003154 ret = prealloc_file_extent_cluster(inode, cluster);
Yan, Zheng0257bb82009-09-24 09:17:31 -04003155 if (ret)
Yan, Zhengefa56462010-05-16 10:49:59 -04003156 goto out;
Yan, Zheng0257bb82009-09-24 09:17:31 -04003157
3158 file_ra_state_init(ra, inode->i_mapping);
3159
Yan, Zhengefa56462010-05-16 10:49:59 -04003160 ret = setup_extent_mapping(inode, cluster->start - offset,
3161 cluster->end - offset, cluster->start);
3162 if (ret)
3163 goto out;
3164
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003165 index = (cluster->start - offset) >> PAGE_SHIFT;
3166 last_index = (cluster->end - offset) >> PAGE_SHIFT;
Yan, Zheng0257bb82009-09-24 09:17:31 -04003167 while (index <= last_index) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003168 ret = btrfs_delalloc_reserve_metadata(inode, PAGE_SIZE);
Yan, Zhengefa56462010-05-16 10:49:59 -04003169 if (ret)
3170 goto out;
3171
Yan, Zheng0257bb82009-09-24 09:17:31 -04003172 page = find_lock_page(inode->i_mapping, index);
3173 if (!page) {
3174 page_cache_sync_readahead(inode->i_mapping,
3175 ra, NULL, index,
3176 last_index + 1 - index);
Josef Bacika94733d2011-07-11 10:47:06 -04003177 page = find_or_create_page(inode->i_mapping, index,
Josef Bacik3b16a4e2011-09-21 15:05:58 -04003178 mask);
Yan, Zheng0257bb82009-09-24 09:17:31 -04003179 if (!page) {
Yan, Zhengefa56462010-05-16 10:49:59 -04003180 btrfs_delalloc_release_metadata(inode,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003181 PAGE_SIZE);
Yan, Zheng0257bb82009-09-24 09:17:31 -04003182 ret = -ENOMEM;
Yan, Zhengefa56462010-05-16 10:49:59 -04003183 goto out;
Yan, Zheng0257bb82009-09-24 09:17:31 -04003184 }
3185 }
3186
3187 if (PageReadahead(page)) {
3188 page_cache_async_readahead(inode->i_mapping,
3189 ra, NULL, page, index,
3190 last_index + 1 - index);
3191 }
3192
3193 if (!PageUptodate(page)) {
3194 btrfs_readpage(NULL, page);
3195 lock_page(page);
3196 if (!PageUptodate(page)) {
3197 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003198 put_page(page);
Yan, Zhengefa56462010-05-16 10:49:59 -04003199 btrfs_delalloc_release_metadata(inode,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003200 PAGE_SIZE);
Yan, Zheng0257bb82009-09-24 09:17:31 -04003201 ret = -EIO;
Yan, Zhengefa56462010-05-16 10:49:59 -04003202 goto out;
Yan, Zheng0257bb82009-09-24 09:17:31 -04003203 }
3204 }
3205
Miao Xie4eee4fa2012-12-21 09:17:45 +00003206 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003207 page_end = page_start + PAGE_SIZE - 1;
Yan, Zheng0257bb82009-09-24 09:17:31 -04003208
Jeff Mahoneyd0082372012-03-01 14:57:19 +01003209 lock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end);
Yan, Zheng0257bb82009-09-24 09:17:31 -04003210
3211 set_page_extent_mapped(page);
3212
3213 if (nr < cluster->nr &&
3214 page_start + offset == cluster->boundary[nr]) {
3215 set_extent_bits(&BTRFS_I(inode)->io_tree,
3216 page_start, page_end,
David Sterbaceeb0ae2016-04-26 23:54:39 +02003217 EXTENT_BOUNDARY);
Yan, Zheng0257bb82009-09-24 09:17:31 -04003218 nr++;
3219 }
Yan, Zheng0257bb82009-09-24 09:17:31 -04003220
Qu Wenruoba8b04c2016-07-19 16:50:36 +08003221 btrfs_set_extent_delalloc(inode, page_start, page_end, NULL, 0);
Yan, Zheng0257bb82009-09-24 09:17:31 -04003222 set_page_dirty(page);
Yan, Zheng0257bb82009-09-24 09:17:31 -04003223
3224 unlock_extent(&BTRFS_I(inode)->io_tree,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01003225 page_start, page_end);
Yan, Zheng0257bb82009-09-24 09:17:31 -04003226 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003227 put_page(page);
Yan, Zheng0257bb82009-09-24 09:17:31 -04003228
3229 index++;
Yan, Zhengefa56462010-05-16 10:49:59 -04003230 balance_dirty_pages_ratelimited(inode->i_mapping);
3231 btrfs_throttle(BTRFS_I(inode)->root);
Yan, Zheng0257bb82009-09-24 09:17:31 -04003232 }
3233 WARN_ON(nr != cluster->nr);
Yan, Zhengefa56462010-05-16 10:49:59 -04003234out:
Yan, Zheng0257bb82009-09-24 09:17:31 -04003235 kfree(ra);
3236 return ret;
3237}
3238
3239static noinline_for_stack
3240int relocate_data_extent(struct inode *inode, struct btrfs_key *extent_key,
3241 struct file_extent_cluster *cluster)
3242{
3243 int ret;
3244
3245 if (cluster->nr > 0 && extent_key->objectid != cluster->end + 1) {
3246 ret = relocate_file_extent_cluster(inode, cluster);
3247 if (ret)
3248 return ret;
3249 cluster->nr = 0;
3250 }
3251
3252 if (!cluster->nr)
3253 cluster->start = extent_key->objectid;
3254 else
3255 BUG_ON(cluster->nr >= MAX_EXTENTS);
3256 cluster->end = extent_key->objectid + extent_key->offset - 1;
3257 cluster->boundary[cluster->nr] = extent_key->objectid;
3258 cluster->nr++;
3259
3260 if (cluster->nr >= MAX_EXTENTS) {
3261 ret = relocate_file_extent_cluster(inode, cluster);
3262 if (ret)
3263 return ret;
3264 cluster->nr = 0;
3265 }
3266 return 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003267}
3268
3269#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3270static int get_ref_objectid_v0(struct reloc_control *rc,
3271 struct btrfs_path *path,
3272 struct btrfs_key *extent_key,
3273 u64 *ref_objectid, int *path_change)
3274{
3275 struct btrfs_key key;
3276 struct extent_buffer *leaf;
3277 struct btrfs_extent_ref_v0 *ref0;
3278 int ret;
3279 int slot;
3280
3281 leaf = path->nodes[0];
3282 slot = path->slots[0];
3283 while (1) {
3284 if (slot >= btrfs_header_nritems(leaf)) {
3285 ret = btrfs_next_leaf(rc->extent_root, path);
3286 if (ret < 0)
3287 return ret;
3288 BUG_ON(ret > 0);
3289 leaf = path->nodes[0];
3290 slot = path->slots[0];
3291 if (path_change)
3292 *path_change = 1;
3293 }
3294 btrfs_item_key_to_cpu(leaf, &key, slot);
3295 if (key.objectid != extent_key->objectid)
3296 return -ENOENT;
3297
3298 if (key.type != BTRFS_EXTENT_REF_V0_KEY) {
3299 slot++;
3300 continue;
3301 }
3302 ref0 = btrfs_item_ptr(leaf, slot,
3303 struct btrfs_extent_ref_v0);
3304 *ref_objectid = btrfs_ref_objectid_v0(leaf, ref0);
3305 break;
3306 }
3307 return 0;
3308}
3309#endif
3310
3311/*
3312 * helper to add a tree block to the list.
3313 * the major work is getting the generation and level of the block
3314 */
3315static int add_tree_block(struct reloc_control *rc,
3316 struct btrfs_key *extent_key,
3317 struct btrfs_path *path,
3318 struct rb_root *blocks)
3319{
3320 struct extent_buffer *eb;
3321 struct btrfs_extent_item *ei;
3322 struct btrfs_tree_block_info *bi;
3323 struct tree_block *block;
3324 struct rb_node *rb_node;
3325 u32 item_size;
3326 int level = -1;
Wang Shilong7fdf4b62013-10-25 18:52:08 +08003327 u64 generation;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003328
3329 eb = path->nodes[0];
3330 item_size = btrfs_item_size_nr(eb, path->slots[0]);
3331
Josef Bacik3173a182013-03-07 14:22:04 -05003332 if (extent_key->type == BTRFS_METADATA_ITEM_KEY ||
3333 item_size >= sizeof(*ei) + sizeof(*bi)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003334 ei = btrfs_item_ptr(eb, path->slots[0],
3335 struct btrfs_extent_item);
Josef Bacik3173a182013-03-07 14:22:04 -05003336 if (extent_key->type == BTRFS_EXTENT_ITEM_KEY) {
3337 bi = (struct btrfs_tree_block_info *)(ei + 1);
3338 level = btrfs_tree_block_level(eb, bi);
3339 } else {
3340 level = (int)extent_key->offset;
3341 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003342 generation = btrfs_extent_generation(eb, ei);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003343 } else {
3344#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3345 u64 ref_owner;
3346 int ret;
3347
3348 BUG_ON(item_size != sizeof(struct btrfs_extent_item_v0));
3349 ret = get_ref_objectid_v0(rc, path, extent_key,
3350 &ref_owner, NULL);
Andi Kleen411fc6b2010-10-29 15:14:31 -04003351 if (ret < 0)
3352 return ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003353 BUG_ON(ref_owner >= BTRFS_MAX_LEVEL);
3354 level = (int)ref_owner;
3355 /* FIXME: get real generation */
3356 generation = 0;
3357#else
3358 BUG();
3359#endif
3360 }
3361
David Sterbab3b4aa72011-04-21 01:20:15 +02003362 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003363
3364 BUG_ON(level == -1);
3365
3366 block = kmalloc(sizeof(*block), GFP_NOFS);
3367 if (!block)
3368 return -ENOMEM;
3369
3370 block->bytenr = extent_key->objectid;
David Sterba707e8a02014-06-04 19:22:26 +02003371 block->key.objectid = rc->extent_root->nodesize;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003372 block->key.offset = generation;
3373 block->level = level;
3374 block->key_ready = 0;
3375
3376 rb_node = tree_insert(blocks, block->bytenr, &block->rb_node);
Jeff Mahoney43c04fb2011-10-03 23:22:33 -04003377 if (rb_node)
3378 backref_tree_panic(rb_node, -EEXIST, block->bytenr);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003379
3380 return 0;
3381}
3382
3383/*
3384 * helper to add tree blocks for backref of type BTRFS_SHARED_DATA_REF_KEY
3385 */
3386static int __add_tree_block(struct reloc_control *rc,
3387 u64 bytenr, u32 blocksize,
3388 struct rb_root *blocks)
3389{
3390 struct btrfs_path *path;
3391 struct btrfs_key key;
3392 int ret;
Josef Bacikaee68ee2013-06-13 13:50:23 -04003393 bool skinny = btrfs_fs_incompat(rc->extent_root->fs_info,
3394 SKINNY_METADATA);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003395
David Sterba7476dfd2014-06-15 03:34:59 +02003396 if (tree_block_processed(bytenr, rc))
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003397 return 0;
3398
3399 if (tree_search(blocks, bytenr))
3400 return 0;
3401
3402 path = btrfs_alloc_path();
3403 if (!path)
3404 return -ENOMEM;
Josef Bacikaee68ee2013-06-13 13:50:23 -04003405again:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003406 key.objectid = bytenr;
Josef Bacikaee68ee2013-06-13 13:50:23 -04003407 if (skinny) {
3408 key.type = BTRFS_METADATA_ITEM_KEY;
3409 key.offset = (u64)-1;
3410 } else {
3411 key.type = BTRFS_EXTENT_ITEM_KEY;
3412 key.offset = blocksize;
3413 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003414
3415 path->search_commit_root = 1;
3416 path->skip_locking = 1;
3417 ret = btrfs_search_slot(NULL, rc->extent_root, &key, path, 0, 0);
3418 if (ret < 0)
3419 goto out;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003420
Josef Bacikaee68ee2013-06-13 13:50:23 -04003421 if (ret > 0 && skinny) {
3422 if (path->slots[0]) {
3423 path->slots[0]--;
3424 btrfs_item_key_to_cpu(path->nodes[0], &key,
3425 path->slots[0]);
3426 if (key.objectid == bytenr &&
3427 (key.type == BTRFS_METADATA_ITEM_KEY ||
3428 (key.type == BTRFS_EXTENT_ITEM_KEY &&
3429 key.offset == blocksize)))
3430 ret = 0;
3431 }
3432
3433 if (ret) {
3434 skinny = false;
3435 btrfs_release_path(path);
3436 goto again;
3437 }
Josef Bacik3173a182013-03-07 14:22:04 -05003438 }
3439 BUG_ON(ret);
3440
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003441 ret = add_tree_block(rc, &key, path, blocks);
3442out:
3443 btrfs_free_path(path);
3444 return ret;
3445}
3446
3447/*
3448 * helper to check if the block use full backrefs for pointers in it
3449 */
3450static int block_use_full_backref(struct reloc_control *rc,
3451 struct extent_buffer *eb)
3452{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003453 u64 flags;
3454 int ret;
3455
3456 if (btrfs_header_flag(eb, BTRFS_HEADER_FLAG_RELOC) ||
3457 btrfs_header_backref_rev(eb) < BTRFS_MIXED_BACKREF_REV)
3458 return 1;
3459
Yan, Zheng3fd0a552010-05-16 10:49:59 -04003460 ret = btrfs_lookup_extent_info(NULL, rc->extent_root,
Josef Bacik3173a182013-03-07 14:22:04 -05003461 eb->start, btrfs_header_level(eb), 1,
3462 NULL, &flags);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003463 BUG_ON(ret);
3464
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003465 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)
3466 ret = 1;
3467 else
3468 ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003469 return ret;
3470}
3471
Josef Bacik0af3d002010-06-21 14:48:16 -04003472static int delete_block_group_cache(struct btrfs_fs_info *fs_info,
Chris Mason1bbc6212015-04-06 12:46:08 -07003473 struct btrfs_block_group_cache *block_group,
3474 struct inode *inode,
3475 u64 ino)
Josef Bacik0af3d002010-06-21 14:48:16 -04003476{
3477 struct btrfs_key key;
Josef Bacik0af3d002010-06-21 14:48:16 -04003478 struct btrfs_root *root = fs_info->tree_root;
3479 struct btrfs_trans_handle *trans;
Josef Bacik0af3d002010-06-21 14:48:16 -04003480 int ret = 0;
3481
3482 if (inode)
3483 goto truncate;
3484
3485 key.objectid = ino;
3486 key.type = BTRFS_INODE_ITEM_KEY;
3487 key.offset = 0;
3488
3489 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
Tsutomu Itohf54fb852012-09-06 03:08:59 -06003490 if (IS_ERR(inode) || is_bad_inode(inode)) {
3491 if (!IS_ERR(inode))
Josef Bacik0af3d002010-06-21 14:48:16 -04003492 iput(inode);
3493 return -ENOENT;
3494 }
3495
3496truncate:
Miao Xie7b61cd92013-05-13 13:55:09 +00003497 ret = btrfs_check_trunc_cache_free_space(root,
3498 &fs_info->global_block_rsv);
3499 if (ret)
3500 goto out;
3501
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003502 trans = btrfs_join_transaction(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04003503 if (IS_ERR(trans)) {
Tsutomu Itoh3612b492011-01-25 02:51:38 +00003504 ret = PTR_ERR(trans);
Josef Bacik0af3d002010-06-21 14:48:16 -04003505 goto out;
3506 }
3507
Chris Mason1bbc6212015-04-06 12:46:08 -07003508 ret = btrfs_truncate_free_space_cache(root, trans, block_group, inode);
Josef Bacik0af3d002010-06-21 14:48:16 -04003509
Josef Bacik0af3d002010-06-21 14:48:16 -04003510 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00003511 btrfs_btree_balance_dirty(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04003512out:
3513 iput(inode);
3514 return ret;
3515}
3516
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003517/*
3518 * helper to add tree blocks for backref of type BTRFS_EXTENT_DATA_REF_KEY
3519 * this function scans fs tree to find blocks reference the data extent
3520 */
3521static int find_data_references(struct reloc_control *rc,
3522 struct btrfs_key *extent_key,
3523 struct extent_buffer *leaf,
3524 struct btrfs_extent_data_ref *ref,
3525 struct rb_root *blocks)
3526{
3527 struct btrfs_path *path;
3528 struct tree_block *block;
3529 struct btrfs_root *root;
3530 struct btrfs_file_extent_item *fi;
3531 struct rb_node *rb_node;
3532 struct btrfs_key key;
3533 u64 ref_root;
3534 u64 ref_objectid;
3535 u64 ref_offset;
3536 u32 ref_count;
3537 u32 nritems;
3538 int err = 0;
3539 int added = 0;
3540 int counted;
3541 int ret;
3542
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003543 ref_root = btrfs_extent_data_ref_root(leaf, ref);
3544 ref_objectid = btrfs_extent_data_ref_objectid(leaf, ref);
3545 ref_offset = btrfs_extent_data_ref_offset(leaf, ref);
3546 ref_count = btrfs_extent_data_ref_count(leaf, ref);
3547
Josef Bacik0af3d002010-06-21 14:48:16 -04003548 /*
3549 * This is an extent belonging to the free space cache, lets just delete
3550 * it and redo the search.
3551 */
3552 if (ref_root == BTRFS_ROOT_TREE_OBJECTID) {
3553 ret = delete_block_group_cache(rc->extent_root->fs_info,
Chris Mason1bbc6212015-04-06 12:46:08 -07003554 rc->block_group,
Josef Bacik0af3d002010-06-21 14:48:16 -04003555 NULL, ref_objectid);
3556 if (ret != -ENOENT)
3557 return ret;
3558 ret = 0;
3559 }
3560
3561 path = btrfs_alloc_path();
3562 if (!path)
3563 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +01003564 path->reada = READA_FORWARD;
Josef Bacik0af3d002010-06-21 14:48:16 -04003565
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003566 root = read_fs_root(rc->extent_root->fs_info, ref_root);
3567 if (IS_ERR(root)) {
3568 err = PTR_ERR(root);
3569 goto out;
3570 }
3571
3572 key.objectid = ref_objectid;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003573 key.type = BTRFS_EXTENT_DATA_KEY;
Yan, Zheng84850e82011-08-29 09:25:53 +08003574 if (ref_offset > ((u64)-1 << 32))
3575 key.offset = 0;
3576 else
3577 key.offset = ref_offset;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003578
3579 path->search_commit_root = 1;
3580 path->skip_locking = 1;
3581 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3582 if (ret < 0) {
3583 err = ret;
3584 goto out;
3585 }
3586
3587 leaf = path->nodes[0];
3588 nritems = btrfs_header_nritems(leaf);
3589 /*
3590 * the references in tree blocks that use full backrefs
3591 * are not counted in
3592 */
3593 if (block_use_full_backref(rc, leaf))
3594 counted = 0;
3595 else
3596 counted = 1;
3597 rb_node = tree_search(blocks, leaf->start);
3598 if (rb_node) {
3599 if (counted)
3600 added = 1;
3601 else
3602 path->slots[0] = nritems;
3603 }
3604
3605 while (ref_count > 0) {
3606 while (path->slots[0] >= nritems) {
3607 ret = btrfs_next_leaf(root, path);
3608 if (ret < 0) {
3609 err = ret;
3610 goto out;
3611 }
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303612 if (WARN_ON(ret > 0))
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003613 goto out;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003614
3615 leaf = path->nodes[0];
3616 nritems = btrfs_header_nritems(leaf);
3617 added = 0;
3618
3619 if (block_use_full_backref(rc, leaf))
3620 counted = 0;
3621 else
3622 counted = 1;
3623 rb_node = tree_search(blocks, leaf->start);
3624 if (rb_node) {
3625 if (counted)
3626 added = 1;
3627 else
3628 path->slots[0] = nritems;
3629 }
3630 }
3631
3632 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303633 if (WARN_ON(key.objectid != ref_objectid ||
3634 key.type != BTRFS_EXTENT_DATA_KEY))
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003635 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003636
3637 fi = btrfs_item_ptr(leaf, path->slots[0],
3638 struct btrfs_file_extent_item);
3639
3640 if (btrfs_file_extent_type(leaf, fi) ==
3641 BTRFS_FILE_EXTENT_INLINE)
3642 goto next;
3643
3644 if (btrfs_file_extent_disk_bytenr(leaf, fi) !=
3645 extent_key->objectid)
3646 goto next;
3647
3648 key.offset -= btrfs_file_extent_offset(leaf, fi);
3649 if (key.offset != ref_offset)
3650 goto next;
3651
3652 if (counted)
3653 ref_count--;
3654 if (added)
3655 goto next;
3656
David Sterba7476dfd2014-06-15 03:34:59 +02003657 if (!tree_block_processed(leaf->start, rc)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003658 block = kmalloc(sizeof(*block), GFP_NOFS);
3659 if (!block) {
3660 err = -ENOMEM;
3661 break;
3662 }
3663 block->bytenr = leaf->start;
3664 btrfs_item_key_to_cpu(leaf, &block->key, 0);
3665 block->level = 0;
3666 block->key_ready = 1;
3667 rb_node = tree_insert(blocks, block->bytenr,
3668 &block->rb_node);
Jeff Mahoney43c04fb2011-10-03 23:22:33 -04003669 if (rb_node)
3670 backref_tree_panic(rb_node, -EEXIST,
3671 block->bytenr);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003672 }
3673 if (counted)
3674 added = 1;
3675 else
3676 path->slots[0] = nritems;
3677next:
3678 path->slots[0]++;
3679
3680 }
3681out:
3682 btrfs_free_path(path);
3683 return err;
3684}
3685
3686/*
Liu Bo2c016dc2012-12-26 15:32:17 +08003687 * helper to find all tree blocks that reference a given data extent
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003688 */
3689static noinline_for_stack
3690int add_data_references(struct reloc_control *rc,
3691 struct btrfs_key *extent_key,
3692 struct btrfs_path *path,
3693 struct rb_root *blocks)
3694{
3695 struct btrfs_key key;
3696 struct extent_buffer *eb;
3697 struct btrfs_extent_data_ref *dref;
3698 struct btrfs_extent_inline_ref *iref;
3699 unsigned long ptr;
3700 unsigned long end;
David Sterba707e8a02014-06-04 19:22:26 +02003701 u32 blocksize = rc->extent_root->nodesize;
Filipe David Borba Manana647f63b2013-07-13 12:25:15 +01003702 int ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003703 int err = 0;
3704
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003705 eb = path->nodes[0];
3706 ptr = btrfs_item_ptr_offset(eb, path->slots[0]);
3707 end = ptr + btrfs_item_size_nr(eb, path->slots[0]);
3708#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3709 if (ptr + sizeof(struct btrfs_extent_item_v0) == end)
3710 ptr = end;
3711 else
3712#endif
3713 ptr += sizeof(struct btrfs_extent_item);
3714
3715 while (ptr < end) {
3716 iref = (struct btrfs_extent_inline_ref *)ptr;
3717 key.type = btrfs_extent_inline_ref_type(eb, iref);
3718 if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
3719 key.offset = btrfs_extent_inline_ref_offset(eb, iref);
3720 ret = __add_tree_block(rc, key.offset, blocksize,
3721 blocks);
3722 } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
3723 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
3724 ret = find_data_references(rc, extent_key,
3725 eb, dref, blocks);
3726 } else {
3727 BUG();
3728 }
Filipe David Borba Manana647f63b2013-07-13 12:25:15 +01003729 if (ret) {
3730 err = ret;
3731 goto out;
3732 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003733 ptr += btrfs_extent_inline_ref_size(key.type);
3734 }
3735 WARN_ON(ptr > end);
3736
3737 while (1) {
3738 cond_resched();
3739 eb = path->nodes[0];
3740 if (path->slots[0] >= btrfs_header_nritems(eb)) {
3741 ret = btrfs_next_leaf(rc->extent_root, path);
3742 if (ret < 0) {
3743 err = ret;
3744 break;
3745 }
3746 if (ret > 0)
3747 break;
3748 eb = path->nodes[0];
3749 }
3750
3751 btrfs_item_key_to_cpu(eb, &key, path->slots[0]);
3752 if (key.objectid != extent_key->objectid)
3753 break;
3754
3755#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3756 if (key.type == BTRFS_SHARED_DATA_REF_KEY ||
3757 key.type == BTRFS_EXTENT_REF_V0_KEY) {
3758#else
3759 BUG_ON(key.type == BTRFS_EXTENT_REF_V0_KEY);
3760 if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
3761#endif
3762 ret = __add_tree_block(rc, key.offset, blocksize,
3763 blocks);
3764 } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
3765 dref = btrfs_item_ptr(eb, path->slots[0],
3766 struct btrfs_extent_data_ref);
3767 ret = find_data_references(rc, extent_key,
3768 eb, dref, blocks);
3769 } else {
3770 ret = 0;
3771 }
3772 if (ret) {
3773 err = ret;
3774 break;
3775 }
3776 path->slots[0]++;
3777 }
Filipe David Borba Manana647f63b2013-07-13 12:25:15 +01003778out:
David Sterbab3b4aa72011-04-21 01:20:15 +02003779 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003780 if (err)
3781 free_block_list(blocks);
3782 return err;
3783}
3784
3785/*
Liu Bo2c016dc2012-12-26 15:32:17 +08003786 * helper to find next unprocessed extent
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003787 */
3788static noinline_for_stack
Zhaolei147d2562015-08-06 20:58:11 +08003789int find_next_extent(struct reloc_control *rc, struct btrfs_path *path,
Yan, Zheng3fd0a552010-05-16 10:49:59 -04003790 struct btrfs_key *extent_key)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003791{
3792 struct btrfs_key key;
3793 struct extent_buffer *leaf;
3794 u64 start, end, last;
3795 int ret;
3796
3797 last = rc->block_group->key.objectid + rc->block_group->key.offset;
3798 while (1) {
3799 cond_resched();
3800 if (rc->search_start >= last) {
3801 ret = 1;
3802 break;
3803 }
3804
3805 key.objectid = rc->search_start;
3806 key.type = BTRFS_EXTENT_ITEM_KEY;
3807 key.offset = 0;
3808
3809 path->search_commit_root = 1;
3810 path->skip_locking = 1;
3811 ret = btrfs_search_slot(NULL, rc->extent_root, &key, path,
3812 0, 0);
3813 if (ret < 0)
3814 break;
3815next:
3816 leaf = path->nodes[0];
3817 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
3818 ret = btrfs_next_leaf(rc->extent_root, path);
3819 if (ret != 0)
3820 break;
3821 leaf = path->nodes[0];
3822 }
3823
3824 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
3825 if (key.objectid >= last) {
3826 ret = 1;
3827 break;
3828 }
3829
Josef Bacik3173a182013-03-07 14:22:04 -05003830 if (key.type != BTRFS_EXTENT_ITEM_KEY &&
3831 key.type != BTRFS_METADATA_ITEM_KEY) {
3832 path->slots[0]++;
3833 goto next;
3834 }
3835
3836 if (key.type == BTRFS_EXTENT_ITEM_KEY &&
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003837 key.objectid + key.offset <= rc->search_start) {
3838 path->slots[0]++;
3839 goto next;
3840 }
3841
Josef Bacik3173a182013-03-07 14:22:04 -05003842 if (key.type == BTRFS_METADATA_ITEM_KEY &&
David Sterba707e8a02014-06-04 19:22:26 +02003843 key.objectid + rc->extent_root->nodesize <=
Josef Bacik3173a182013-03-07 14:22:04 -05003844 rc->search_start) {
3845 path->slots[0]++;
3846 goto next;
3847 }
3848
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003849 ret = find_first_extent_bit(&rc->processed_blocks,
3850 key.objectid, &start, &end,
Josef Bacike6138872012-09-27 17:07:30 -04003851 EXTENT_DIRTY, NULL);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003852
3853 if (ret == 0 && start <= key.objectid) {
David Sterbab3b4aa72011-04-21 01:20:15 +02003854 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003855 rc->search_start = end + 1;
3856 } else {
Josef Bacik3173a182013-03-07 14:22:04 -05003857 if (key.type == BTRFS_EXTENT_ITEM_KEY)
3858 rc->search_start = key.objectid + key.offset;
3859 else
3860 rc->search_start = key.objectid +
David Sterba707e8a02014-06-04 19:22:26 +02003861 rc->extent_root->nodesize;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04003862 memcpy(extent_key, &key, sizeof(key));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003863 return 0;
3864 }
3865 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003866 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003867 return ret;
3868}
3869
3870static void set_reloc_control(struct reloc_control *rc)
3871{
3872 struct btrfs_fs_info *fs_info = rc->extent_root->fs_info;
Chris Mason75857172011-06-13 20:00:16 -04003873
3874 mutex_lock(&fs_info->reloc_mutex);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003875 fs_info->reloc_ctl = rc;
Chris Mason75857172011-06-13 20:00:16 -04003876 mutex_unlock(&fs_info->reloc_mutex);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003877}
3878
3879static void unset_reloc_control(struct reloc_control *rc)
3880{
3881 struct btrfs_fs_info *fs_info = rc->extent_root->fs_info;
Chris Mason75857172011-06-13 20:00:16 -04003882
3883 mutex_lock(&fs_info->reloc_mutex);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003884 fs_info->reloc_ctl = NULL;
Chris Mason75857172011-06-13 20:00:16 -04003885 mutex_unlock(&fs_info->reloc_mutex);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003886}
3887
3888static int check_extent_flags(u64 flags)
3889{
3890 if ((flags & BTRFS_EXTENT_FLAG_DATA) &&
3891 (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK))
3892 return 1;
3893 if (!(flags & BTRFS_EXTENT_FLAG_DATA) &&
3894 !(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK))
3895 return 1;
3896 if ((flags & BTRFS_EXTENT_FLAG_DATA) &&
3897 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
3898 return 1;
3899 return 0;
3900}
3901
Yan, Zheng3fd0a552010-05-16 10:49:59 -04003902static noinline_for_stack
3903int prepare_to_relocate(struct reloc_control *rc)
3904{
3905 struct btrfs_trans_handle *trans;
Josef Bacikac2faba2016-05-27 13:08:26 -04003906 int ret;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04003907
Miao Xie66d8f3d2012-09-06 04:02:28 -06003908 rc->block_rsv = btrfs_alloc_block_rsv(rc->extent_root,
3909 BTRFS_BLOCK_RSV_TEMP);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04003910 if (!rc->block_rsv)
3911 return -ENOMEM;
3912
Yan, Zheng3fd0a552010-05-16 10:49:59 -04003913 memset(&rc->cluster, 0, sizeof(rc->cluster));
3914 rc->search_start = rc->block_group->key.objectid;
3915 rc->extents_found = 0;
3916 rc->nodes_relocated = 0;
3917 rc->merging_rsv_size = 0;
Wang Shilong0647bf52013-11-20 09:01:52 +08003918 rc->reserved_bytes = 0;
3919 rc->block_rsv->size = rc->extent_root->nodesize *
3920 RELOCATION_RESERVED_NODES;
Josef Bacikac2faba2016-05-27 13:08:26 -04003921 ret = btrfs_block_rsv_refill(rc->extent_root,
3922 rc->block_rsv, rc->block_rsv->size,
3923 BTRFS_RESERVE_FLUSH_ALL);
3924 if (ret)
3925 return ret;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04003926
3927 rc->create_reloc_tree = 1;
3928 set_reloc_control(rc);
3929
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003930 trans = btrfs_join_transaction(rc->extent_root);
Liu Bo28818942013-03-04 16:25:39 +00003931 if (IS_ERR(trans)) {
3932 unset_reloc_control(rc);
3933 /*
3934 * extent tree is not a ref_cow tree and has no reloc_root to
3935 * cleanup. And callers are responsible to free the above
3936 * block rsv.
3937 */
3938 return PTR_ERR(trans);
3939 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -04003940 btrfs_commit_transaction(trans, rc->extent_root);
3941 return 0;
3942}
Yan, Zheng76dda932009-09-21 16:00:26 -04003943
Qu Wenruo62b99542016-08-15 10:36:51 +08003944/*
3945 * Qgroup fixer for data chunk relocation.
3946 * The data relocation is done in the following steps
3947 * 1) Copy data extents into data reloc tree
3948 * 2) Create tree reloc tree(special snapshot) for related subvolumes
3949 * 3) Modify file extents in tree reloc tree
3950 * 4) Merge tree reloc tree with original fs tree, by swapping tree blocks
3951 *
3952 * The problem is, data and tree reloc tree are not accounted to qgroup,
3953 * and 4) will only info qgroup to track tree blocks change, not file extents
3954 * in the tree blocks.
3955 *
3956 * The good news is, related data extents are all in data reloc tree, so we
3957 * only need to info qgroup to track all file extents in data reloc tree
3958 * before commit trans.
3959 */
3960static int qgroup_fix_relocated_data_extents(struct btrfs_trans_handle *trans,
3961 struct reloc_control *rc)
3962{
3963 struct btrfs_fs_info *fs_info = rc->extent_root->fs_info;
3964 struct inode *inode = rc->data_inode;
3965 struct btrfs_root *data_reloc_root = BTRFS_I(inode)->root;
3966 struct btrfs_path *path;
3967 struct btrfs_key key;
3968 int ret = 0;
3969
Josef Bacikafcdd122016-09-02 15:40:02 -04003970 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
Qu Wenruo62b99542016-08-15 10:36:51 +08003971 return 0;
3972
3973 /*
3974 * Only for stage where we update data pointers the qgroup fix is
3975 * valid.
3976 * For MOVING_DATA stage, we will miss the timing of swapping tree
3977 * blocks, and won't fix it.
3978 */
3979 if (!(rc->stage == UPDATE_DATA_PTRS && rc->extents_found))
3980 return 0;
3981
3982 path = btrfs_alloc_path();
3983 if (!path)
3984 return -ENOMEM;
3985 key.objectid = btrfs_ino(inode);
3986 key.type = BTRFS_EXTENT_DATA_KEY;
3987 key.offset = 0;
3988
3989 ret = btrfs_search_slot(NULL, data_reloc_root, &key, path, 0, 0);
3990 if (ret < 0)
3991 goto out;
3992
3993 lock_extent(&BTRFS_I(inode)->io_tree, 0, (u64)-1);
3994 while (1) {
3995 struct btrfs_file_extent_item *fi;
3996
3997 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
3998 if (key.objectid > btrfs_ino(inode))
3999 break;
4000 if (key.type != BTRFS_EXTENT_DATA_KEY)
4001 goto next;
4002 fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
4003 struct btrfs_file_extent_item);
4004 if (btrfs_file_extent_type(path->nodes[0], fi) !=
4005 BTRFS_FILE_EXTENT_REG)
4006 goto next;
4007 ret = btrfs_qgroup_insert_dirty_extent(trans, fs_info,
4008 btrfs_file_extent_disk_bytenr(path->nodes[0], fi),
4009 btrfs_file_extent_disk_num_bytes(path->nodes[0], fi),
4010 GFP_NOFS);
4011 if (ret < 0)
4012 break;
4013next:
4014 ret = btrfs_next_item(data_reloc_root, path);
4015 if (ret < 0)
4016 break;
4017 if (ret > 0) {
4018 ret = 0;
4019 break;
4020 }
4021 }
4022 unlock_extent(&BTRFS_I(inode)->io_tree, 0 , (u64)-1);
4023out:
4024 btrfs_free_path(path);
4025 return ret;
4026}
4027
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004028static noinline_for_stack int relocate_block_group(struct reloc_control *rc)
4029{
4030 struct rb_root blocks = RB_ROOT;
4031 struct btrfs_key key;
4032 struct btrfs_trans_handle *trans = NULL;
4033 struct btrfs_path *path;
4034 struct btrfs_extent_item *ei;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004035 u64 flags;
4036 u32 item_size;
4037 int ret;
4038 int err = 0;
Chris Masonc87f08c2011-02-16 13:57:04 -05004039 int progress = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004040
4041 path = btrfs_alloc_path();
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004042 if (!path)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004043 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +01004044 path->reada = READA_FORWARD;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004045
4046 ret = prepare_to_relocate(rc);
4047 if (ret) {
4048 err = ret;
4049 goto out_free;
Jiri Slaby2423fdf2010-01-06 16:57:22 +00004050 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004051
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004052 while (1) {
Wang Shilong0647bf52013-11-20 09:01:52 +08004053 rc->reserved_bytes = 0;
4054 ret = btrfs_block_rsv_refill(rc->extent_root,
4055 rc->block_rsv, rc->block_rsv->size,
4056 BTRFS_RESERVE_FLUSH_ALL);
4057 if (ret) {
4058 err = ret;
4059 break;
4060 }
Chris Masonc87f08c2011-02-16 13:57:04 -05004061 progress++;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004062 trans = btrfs_start_transaction(rc->extent_root, 0);
Liu Bo0f788c52013-03-04 16:25:40 +00004063 if (IS_ERR(trans)) {
4064 err = PTR_ERR(trans);
4065 trans = NULL;
4066 break;
4067 }
Chris Masonc87f08c2011-02-16 13:57:04 -05004068restart:
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004069 if (update_backref_cache(trans, &rc->backref_cache)) {
4070 btrfs_end_transaction(trans, rc->extent_root);
4071 continue;
4072 }
4073
Zhaolei147d2562015-08-06 20:58:11 +08004074 ret = find_next_extent(rc, path, &key);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004075 if (ret < 0)
4076 err = ret;
4077 if (ret != 0)
4078 break;
4079
4080 rc->extents_found++;
4081
4082 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
4083 struct btrfs_extent_item);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004084 item_size = btrfs_item_size_nr(path->nodes[0], path->slots[0]);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004085 if (item_size >= sizeof(*ei)) {
4086 flags = btrfs_extent_flags(path->nodes[0], ei);
4087 ret = check_extent_flags(flags);
4088 BUG_ON(ret);
4089
4090 } else {
4091#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
4092 u64 ref_owner;
4093 int path_change = 0;
4094
4095 BUG_ON(item_size !=
4096 sizeof(struct btrfs_extent_item_v0));
4097 ret = get_ref_objectid_v0(rc, path, &key, &ref_owner,
4098 &path_change);
Zhaolei4b3576e2015-08-05 18:00:02 +08004099 if (ret < 0) {
4100 err = ret;
4101 break;
4102 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004103 if (ref_owner < BTRFS_FIRST_FREE_OBJECTID)
4104 flags = BTRFS_EXTENT_FLAG_TREE_BLOCK;
4105 else
4106 flags = BTRFS_EXTENT_FLAG_DATA;
4107
4108 if (path_change) {
David Sterbab3b4aa72011-04-21 01:20:15 +02004109 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004110
4111 path->search_commit_root = 1;
4112 path->skip_locking = 1;
4113 ret = btrfs_search_slot(NULL, rc->extent_root,
4114 &key, path, 0, 0);
4115 if (ret < 0) {
4116 err = ret;
4117 break;
4118 }
4119 BUG_ON(ret > 0);
4120 }
4121#else
4122 BUG();
4123#endif
4124 }
4125
4126 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
4127 ret = add_tree_block(rc, &key, path, &blocks);
4128 } else if (rc->stage == UPDATE_DATA_PTRS &&
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004129 (flags & BTRFS_EXTENT_FLAG_DATA)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004130 ret = add_data_references(rc, &key, path, &blocks);
4131 } else {
David Sterbab3b4aa72011-04-21 01:20:15 +02004132 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004133 ret = 0;
4134 }
4135 if (ret < 0) {
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004136 err = ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004137 break;
4138 }
4139
4140 if (!RB_EMPTY_ROOT(&blocks)) {
4141 ret = relocate_tree_blocks(trans, rc, &blocks);
4142 if (ret < 0) {
Wang Shilong1708cc52013-12-28 19:52:39 +08004143 /*
4144 * if we fail to relocate tree blocks, force to update
4145 * backref cache when committing transaction.
4146 */
4147 rc->backref_cache.last_trans = trans->transid - 1;
4148
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004149 if (ret != -EAGAIN) {
4150 err = ret;
4151 break;
4152 }
4153 rc->extents_found--;
4154 rc->search_start = key.objectid;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004155 }
4156 }
4157
Wang Shilong0647bf52013-11-20 09:01:52 +08004158 btrfs_end_transaction_throttle(trans, rc->extent_root);
4159 btrfs_btree_balance_dirty(rc->extent_root);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004160 trans = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004161
4162 if (rc->stage == MOVE_DATA_EXTENTS &&
4163 (flags & BTRFS_EXTENT_FLAG_DATA)) {
4164 rc->found_file_extent = 1;
Yan, Zheng0257bb82009-09-24 09:17:31 -04004165 ret = relocate_data_extent(rc->data_inode,
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004166 &key, &rc->cluster);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004167 if (ret < 0) {
4168 err = ret;
4169 break;
4170 }
4171 }
4172 }
Chris Masonc87f08c2011-02-16 13:57:04 -05004173 if (trans && progress && err == -ENOSPC) {
4174 ret = btrfs_force_chunk_alloc(trans, rc->extent_root,
4175 rc->block_group->flags);
Shilong Wang96894572015-04-12 14:35:20 +08004176 if (ret == 1) {
Chris Masonc87f08c2011-02-16 13:57:04 -05004177 err = 0;
4178 progress = 0;
4179 goto restart;
4180 }
4181 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004182
David Sterbab3b4aa72011-04-21 01:20:15 +02004183 btrfs_release_path(path);
David Sterba91166212016-04-26 23:54:39 +02004184 clear_extent_bits(&rc->processed_blocks, 0, (u64)-1, EXTENT_DIRTY);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004185
4186 if (trans) {
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004187 btrfs_end_transaction_throttle(trans, rc->extent_root);
Liu Bob53d3f52012-11-14 14:34:34 +00004188 btrfs_btree_balance_dirty(rc->extent_root);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004189 }
4190
Yan, Zheng0257bb82009-09-24 09:17:31 -04004191 if (!err) {
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004192 ret = relocate_file_extent_cluster(rc->data_inode,
4193 &rc->cluster);
Yan, Zheng0257bb82009-09-24 09:17:31 -04004194 if (ret < 0)
4195 err = ret;
4196 }
4197
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004198 rc->create_reloc_tree = 0;
4199 set_reloc_control(rc);
Yan, Zheng0257bb82009-09-24 09:17:31 -04004200
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004201 backref_cache_cleanup(&rc->backref_cache);
4202 btrfs_block_rsv_release(rc->extent_root, rc->block_rsv, (u64)-1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004203
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004204 err = prepare_to_merge(rc, err);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004205
4206 merge_reloc_roots(rc);
4207
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004208 rc->merge_reloc_tree = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004209 unset_reloc_control(rc);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004210 btrfs_block_rsv_release(rc->extent_root, rc->block_rsv, (u64)-1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004211
4212 /* get rid of pinned extents */
Josef Bacik7a7eaa42011-04-13 12:54:33 -04004213 trans = btrfs_join_transaction(rc->extent_root);
Qu Wenruo62b99542016-08-15 10:36:51 +08004214 if (IS_ERR(trans)) {
Tsutomu Itoh3612b492011-01-25 02:51:38 +00004215 err = PTR_ERR(trans);
Qu Wenruo62b99542016-08-15 10:36:51 +08004216 goto out_free;
4217 }
Liu Boa9b1fc82016-08-31 16:43:33 -07004218 ret = qgroup_fix_relocated_data_extents(trans, rc);
4219 if (ret < 0) {
4220 btrfs_abort_transaction(trans, ret);
4221 if (!err)
4222 err = ret;
Qu Wenruo62b99542016-08-15 10:36:51 +08004223 goto out_free;
4224 }
4225 btrfs_commit_transaction(trans, rc->extent_root);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004226out_free:
4227 btrfs_free_block_rsv(rc->extent_root, rc->block_rsv);
4228 btrfs_free_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004229 return err;
4230}
4231
4232static int __insert_orphan_inode(struct btrfs_trans_handle *trans,
Yan, Zheng0257bb82009-09-24 09:17:31 -04004233 struct btrfs_root *root, u64 objectid)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004234{
4235 struct btrfs_path *path;
4236 struct btrfs_inode_item *item;
4237 struct extent_buffer *leaf;
4238 int ret;
4239
4240 path = btrfs_alloc_path();
4241 if (!path)
4242 return -ENOMEM;
4243
4244 ret = btrfs_insert_empty_inode(trans, root, path, objectid);
4245 if (ret)
4246 goto out;
4247
4248 leaf = path->nodes[0];
4249 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_inode_item);
4250 memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item));
4251 btrfs_set_inode_generation(leaf, item, 1);
Yan, Zheng0257bb82009-09-24 09:17:31 -04004252 btrfs_set_inode_size(leaf, item, 0);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004253 btrfs_set_inode_mode(leaf, item, S_IFREG | 0600);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004254 btrfs_set_inode_flags(leaf, item, BTRFS_INODE_NOCOMPRESS |
4255 BTRFS_INODE_PREALLOC);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004256 btrfs_mark_buffer_dirty(leaf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004257out:
4258 btrfs_free_path(path);
4259 return ret;
4260}
4261
4262/*
4263 * helper to create inode for data relocation.
4264 * the inode is in data relocation tree and its link count is 0
4265 */
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004266static noinline_for_stack
4267struct inode *create_reloc_inode(struct btrfs_fs_info *fs_info,
4268 struct btrfs_block_group_cache *group)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004269{
4270 struct inode *inode = NULL;
4271 struct btrfs_trans_handle *trans;
4272 struct btrfs_root *root;
4273 struct btrfs_key key;
Zhaolei46249002015-08-05 18:00:03 +08004274 u64 objectid;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004275 int err = 0;
4276
4277 root = read_fs_root(fs_info, BTRFS_DATA_RELOC_TREE_OBJECTID);
4278 if (IS_ERR(root))
4279 return ERR_CAST(root);
4280
Yan, Zhenga22285a2010-05-16 10:48:46 -04004281 trans = btrfs_start_transaction(root, 6);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004282 if (IS_ERR(trans))
4283 return ERR_CAST(trans);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004284
Li Zefan581bb052011-04-20 10:06:11 +08004285 err = btrfs_find_free_objectid(root, &objectid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004286 if (err)
4287 goto out;
4288
Yan, Zheng0257bb82009-09-24 09:17:31 -04004289 err = __insert_orphan_inode(trans, root, objectid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004290 BUG_ON(err);
4291
4292 key.objectid = objectid;
4293 key.type = BTRFS_INODE_ITEM_KEY;
4294 key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +00004295 inode = btrfs_iget(root->fs_info->sb, &key, root, NULL);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004296 BUG_ON(IS_ERR(inode) || is_bad_inode(inode));
4297 BTRFS_I(inode)->index_cnt = group->key.objectid;
4298
4299 err = btrfs_orphan_add(trans, inode);
4300out:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004301 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00004302 btrfs_btree_balance_dirty(root);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004303 if (err) {
4304 if (inode)
4305 iput(inode);
4306 inode = ERR_PTR(err);
4307 }
4308 return inode;
4309}
4310
Josef Bacika9995ee2013-05-31 13:04:36 -04004311static struct reloc_control *alloc_reloc_control(struct btrfs_fs_info *fs_info)
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004312{
4313 struct reloc_control *rc;
4314
4315 rc = kzalloc(sizeof(*rc), GFP_NOFS);
4316 if (!rc)
4317 return NULL;
4318
4319 INIT_LIST_HEAD(&rc->reloc_roots);
4320 backref_cache_init(&rc->backref_cache);
4321 mapping_tree_init(&rc->reloc_root_tree);
Josef Bacika9995ee2013-05-31 13:04:36 -04004322 extent_io_tree_init(&rc->processed_blocks,
4323 fs_info->btree_inode->i_mapping);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004324 return rc;
4325}
4326
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004327/*
4328 * function to relocate all extents in a block group.
4329 */
4330int btrfs_relocate_block_group(struct btrfs_root *extent_root, u64 group_start)
4331{
4332 struct btrfs_fs_info *fs_info = extent_root->fs_info;
4333 struct reloc_control *rc;
Josef Bacik0af3d002010-06-21 14:48:16 -04004334 struct inode *inode;
4335 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004336 int ret;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004337 int rw = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004338 int err = 0;
4339
Josef Bacika9995ee2013-05-31 13:04:36 -04004340 rc = alloc_reloc_control(fs_info);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004341 if (!rc)
4342 return -ENOMEM;
4343
Yan, Zhengf0486c62010-05-16 10:46:25 -04004344 rc->extent_root = extent_root;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004345
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004346 rc->block_group = btrfs_lookup_block_group(fs_info, group_start);
4347 BUG_ON(!rc->block_group);
4348
Zhaolei868f4012015-08-05 16:43:27 +08004349 ret = btrfs_inc_block_group_ro(extent_root, rc->block_group);
4350 if (ret) {
4351 err = ret;
4352 goto out;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004353 }
Zhaolei868f4012015-08-05 16:43:27 +08004354 rw = 1;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004355
Josef Bacik0af3d002010-06-21 14:48:16 -04004356 path = btrfs_alloc_path();
4357 if (!path) {
4358 err = -ENOMEM;
4359 goto out;
4360 }
4361
4362 inode = lookup_free_space_inode(fs_info->tree_root, rc->block_group,
4363 path);
4364 btrfs_free_path(path);
4365
4366 if (!IS_ERR(inode))
Chris Mason1bbc6212015-04-06 12:46:08 -07004367 ret = delete_block_group_cache(fs_info, rc->block_group, inode, 0);
Josef Bacik0af3d002010-06-21 14:48:16 -04004368 else
4369 ret = PTR_ERR(inode);
4370
4371 if (ret && ret != -ENOENT) {
4372 err = ret;
4373 goto out;
4374 }
4375
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004376 rc->data_inode = create_reloc_inode(fs_info, rc->block_group);
4377 if (IS_ERR(rc->data_inode)) {
4378 err = PTR_ERR(rc->data_inode);
4379 rc->data_inode = NULL;
4380 goto out;
4381 }
4382
Frank Holtonefe120a2013-12-20 11:37:06 -05004383 btrfs_info(extent_root->fs_info, "relocating block group %llu flags %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02004384 rc->block_group->key.objectid, rc->block_group->flags);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004385
Filipe Manana9cfa3e32016-04-26 15:39:32 +01004386 btrfs_wait_block_group_reservations(rc->block_group);
Filipe Mananaf78c4362016-05-09 13:15:41 +01004387 btrfs_wait_nocow_writers(rc->block_group);
Filipe Manana578def72016-04-26 15:36:38 +01004388 btrfs_wait_ordered_roots(fs_info, -1,
4389 rc->block_group->key.objectid,
4390 rc->block_group->key.offset);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004391
4392 while (1) {
Yan, Zheng76dda932009-09-21 16:00:26 -04004393 mutex_lock(&fs_info->cleaner_mutex);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004394 ret = relocate_block_group(rc);
Yan, Zheng76dda932009-09-21 16:00:26 -04004395 mutex_unlock(&fs_info->cleaner_mutex);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004396 if (ret < 0) {
4397 err = ret;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004398 goto out;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004399 }
4400
4401 if (rc->extents_found == 0)
4402 break;
4403
Frank Holtonefe120a2013-12-20 11:37:06 -05004404 btrfs_info(extent_root->fs_info, "found %llu extents",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02004405 rc->extents_found);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004406
4407 if (rc->stage == MOVE_DATA_EXTENTS && rc->found_file_extent) {
Josef Bacik0ef8b722013-10-25 16:13:35 -04004408 ret = btrfs_wait_ordered_range(rc->data_inode, 0,
4409 (u64)-1);
4410 if (ret) {
4411 err = ret;
4412 goto out;
4413 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004414 invalidate_mapping_pages(rc->data_inode->i_mapping,
4415 0, -1);
4416 rc->stage = UPDATE_DATA_PTRS;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004417 }
4418 }
4419
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004420 WARN_ON(rc->block_group->pinned > 0);
4421 WARN_ON(rc->block_group->reserved > 0);
4422 WARN_ON(btrfs_block_group_used(&rc->block_group->item) > 0);
4423out:
Yan, Zhengf0486c62010-05-16 10:46:25 -04004424 if (err && rw)
Zhaolei868f4012015-08-05 16:43:27 +08004425 btrfs_dec_block_group_ro(extent_root, rc->block_group);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004426 iput(rc->data_inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004427 btrfs_put_block_group(rc->block_group);
4428 kfree(rc);
4429 return err;
4430}
4431
Yan, Zheng76dda932009-09-21 16:00:26 -04004432static noinline_for_stack int mark_garbage_root(struct btrfs_root *root)
4433{
4434 struct btrfs_trans_handle *trans;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004435 int ret, err;
Yan, Zheng76dda932009-09-21 16:00:26 -04004436
Yan, Zhenga22285a2010-05-16 10:48:46 -04004437 trans = btrfs_start_transaction(root->fs_info->tree_root, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004438 if (IS_ERR(trans))
4439 return PTR_ERR(trans);
Yan, Zheng76dda932009-09-21 16:00:26 -04004440
4441 memset(&root->root_item.drop_progress, 0,
4442 sizeof(root->root_item.drop_progress));
4443 root->root_item.drop_level = 0;
4444 btrfs_set_root_refs(&root->root_item, 0);
4445 ret = btrfs_update_root(trans, root->fs_info->tree_root,
4446 &root->root_key, &root->root_item);
Yan, Zheng76dda932009-09-21 16:00:26 -04004447
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004448 err = btrfs_end_transaction(trans, root->fs_info->tree_root);
4449 if (err)
4450 return err;
4451 return ret;
Yan, Zheng76dda932009-09-21 16:00:26 -04004452}
4453
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004454/*
4455 * recover relocation interrupted by system crash.
4456 *
4457 * this function resumes merging reloc trees with corresponding fs trees.
4458 * this is important for keeping the sharing of tree blocks
4459 */
4460int btrfs_recover_relocation(struct btrfs_root *root)
4461{
4462 LIST_HEAD(reloc_roots);
4463 struct btrfs_key key;
4464 struct btrfs_root *fs_root;
4465 struct btrfs_root *reloc_root;
4466 struct btrfs_path *path;
4467 struct extent_buffer *leaf;
4468 struct reloc_control *rc = NULL;
4469 struct btrfs_trans_handle *trans;
4470 int ret;
4471 int err = 0;
4472
4473 path = btrfs_alloc_path();
4474 if (!path)
4475 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +01004476 path->reada = READA_BACK;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004477
4478 key.objectid = BTRFS_TREE_RELOC_OBJECTID;
4479 key.type = BTRFS_ROOT_ITEM_KEY;
4480 key.offset = (u64)-1;
4481
4482 while (1) {
4483 ret = btrfs_search_slot(NULL, root->fs_info->tree_root, &key,
4484 path, 0, 0);
4485 if (ret < 0) {
4486 err = ret;
4487 goto out;
4488 }
4489 if (ret > 0) {
4490 if (path->slots[0] == 0)
4491 break;
4492 path->slots[0]--;
4493 }
4494 leaf = path->nodes[0];
4495 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02004496 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004497
4498 if (key.objectid != BTRFS_TREE_RELOC_OBJECTID ||
4499 key.type != BTRFS_ROOT_ITEM_KEY)
4500 break;
4501
Miao Xiecb517ea2013-05-15 07:48:19 +00004502 reloc_root = btrfs_read_fs_root(root, &key);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004503 if (IS_ERR(reloc_root)) {
4504 err = PTR_ERR(reloc_root);
4505 goto out;
4506 }
4507
4508 list_add(&reloc_root->root_list, &reloc_roots);
4509
4510 if (btrfs_root_refs(&reloc_root->root_item) > 0) {
4511 fs_root = read_fs_root(root->fs_info,
4512 reloc_root->root_key.offset);
4513 if (IS_ERR(fs_root)) {
Yan, Zheng76dda932009-09-21 16:00:26 -04004514 ret = PTR_ERR(fs_root);
4515 if (ret != -ENOENT) {
4516 err = ret;
4517 goto out;
4518 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004519 ret = mark_garbage_root(reloc_root);
4520 if (ret < 0) {
4521 err = ret;
4522 goto out;
4523 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004524 }
4525 }
4526
4527 if (key.offset == 0)
4528 break;
4529
4530 key.offset--;
4531 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004532 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004533
4534 if (list_empty(&reloc_roots))
4535 goto out;
4536
Josef Bacika9995ee2013-05-31 13:04:36 -04004537 rc = alloc_reloc_control(root->fs_info);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004538 if (!rc) {
4539 err = -ENOMEM;
4540 goto out;
4541 }
4542
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004543 rc->extent_root = root->fs_info->extent_root;
4544
4545 set_reloc_control(rc);
4546
Josef Bacik7a7eaa42011-04-13 12:54:33 -04004547 trans = btrfs_join_transaction(rc->extent_root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00004548 if (IS_ERR(trans)) {
4549 unset_reloc_control(rc);
4550 err = PTR_ERR(trans);
4551 goto out_free;
4552 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004553
4554 rc->merge_reloc_tree = 1;
4555
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004556 while (!list_empty(&reloc_roots)) {
4557 reloc_root = list_entry(reloc_roots.next,
4558 struct btrfs_root, root_list);
4559 list_del(&reloc_root->root_list);
4560
4561 if (btrfs_root_refs(&reloc_root->root_item) == 0) {
4562 list_add_tail(&reloc_root->root_list,
4563 &rc->reloc_roots);
4564 continue;
4565 }
4566
4567 fs_root = read_fs_root(root->fs_info,
4568 reloc_root->root_key.offset);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004569 if (IS_ERR(fs_root)) {
4570 err = PTR_ERR(fs_root);
4571 goto out_free;
4572 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004573
Jeff Mahoneyffd7b332011-10-03 23:23:15 -04004574 err = __add_reloc_root(reloc_root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004575 BUG_ON(err < 0); /* -ENOMEM or logic error */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004576 fs_root->reloc_root = reloc_root;
4577 }
4578
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004579 err = btrfs_commit_transaction(trans, rc->extent_root);
4580 if (err)
4581 goto out_free;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004582
4583 merge_reloc_roots(rc);
4584
4585 unset_reloc_control(rc);
4586
Josef Bacik7a7eaa42011-04-13 12:54:33 -04004587 trans = btrfs_join_transaction(rc->extent_root);
Qu Wenruo62b99542016-08-15 10:36:51 +08004588 if (IS_ERR(trans)) {
Tsutomu Itoh3612b492011-01-25 02:51:38 +00004589 err = PTR_ERR(trans);
Qu Wenruo62b99542016-08-15 10:36:51 +08004590 goto out_free;
4591 }
4592 err = qgroup_fix_relocated_data_extents(trans, rc);
4593 if (err < 0) {
4594 btrfs_abort_transaction(trans, err);
4595 goto out_free;
4596 }
4597 err = btrfs_commit_transaction(trans, rc->extent_root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00004598out_free:
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004599 kfree(rc);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00004600out:
Liu Boaca1bba2013-03-04 16:25:37 +00004601 if (!list_empty(&reloc_roots))
4602 free_reloc_roots(&reloc_roots);
4603
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004604 btrfs_free_path(path);
4605
4606 if (err == 0) {
4607 /* cleanup orphan inode in data relocation tree */
4608 fs_root = read_fs_root(root->fs_info,
4609 BTRFS_DATA_RELOC_TREE_OBJECTID);
4610 if (IS_ERR(fs_root))
4611 err = PTR_ERR(fs_root);
Miao Xied7ce5842010-02-02 08:46:44 +00004612 else
Josef Bacik66b4ffd2011-01-31 16:22:42 -05004613 err = btrfs_orphan_cleanup(fs_root);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004614 }
4615 return err;
4616}
4617
4618/*
4619 * helper to add ordered checksum for data relocation.
4620 *
4621 * cloning checksum properly handles the nodatasum extents.
4622 * it also saves CPU time to re-calculate the checksum.
4623 */
4624int btrfs_reloc_clone_csums(struct inode *inode, u64 file_pos, u64 len)
4625{
4626 struct btrfs_ordered_sum *sums;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004627 struct btrfs_ordered_extent *ordered;
4628 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004629 int ret;
4630 u64 disk_bytenr;
Josef Bacik4577b012013-09-27 09:33:09 -04004631 u64 new_bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004632 LIST_HEAD(list);
4633
4634 ordered = btrfs_lookup_ordered_extent(inode, file_pos);
4635 BUG_ON(ordered->file_offset != file_pos || ordered->len != len);
4636
4637 disk_bytenr = file_pos + BTRFS_I(inode)->index_cnt;
4638 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, disk_bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01004639 disk_bytenr + len - 1, &list, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004640 if (ret)
4641 goto out;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004642
4643 while (!list_empty(&list)) {
4644 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
4645 list_del_init(&sums->list);
4646
Josef Bacik4577b012013-09-27 09:33:09 -04004647 /*
4648 * We need to offset the new_bytenr based on where the csum is.
4649 * We need to do this because we will read in entire prealloc
4650 * extents but we may have written to say the middle of the
4651 * prealloc extent, so we need to make sure the csum goes with
4652 * the right disk offset.
4653 *
4654 * We can do this because the data reloc inode refers strictly
4655 * to the on disk bytes, so we don't have to worry about
4656 * disk_len vs real len like with real inodes since it's all
4657 * disk length.
4658 */
4659 new_bytenr = ordered->start + (sums->bytenr - disk_bytenr);
4660 sums->bytenr = new_bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004661
4662 btrfs_add_ordered_sum(inode, ordered, sums);
4663 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004664out:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004665 btrfs_put_ordered_extent(ordered);
Andi Kleen411fc6b2010-10-29 15:14:31 -04004666 return ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004667}
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004668
Josef Bacik83d4cfd2013-08-30 15:09:51 -04004669int btrfs_reloc_cow_block(struct btrfs_trans_handle *trans,
4670 struct btrfs_root *root, struct extent_buffer *buf,
4671 struct extent_buffer *cow)
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004672{
4673 struct reloc_control *rc;
4674 struct backref_node *node;
4675 int first_cow = 0;
4676 int level;
Josef Bacik83d4cfd2013-08-30 15:09:51 -04004677 int ret = 0;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004678
4679 rc = root->fs_info->reloc_ctl;
4680 if (!rc)
Josef Bacik83d4cfd2013-08-30 15:09:51 -04004681 return 0;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004682
4683 BUG_ON(rc->stage == UPDATE_DATA_PTRS &&
4684 root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID);
4685
Wang Shilongc974c462013-12-11 19:29:51 +08004686 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
4687 if (buf == root->node)
4688 __update_reloc_root(root, cow->start);
4689 }
4690
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004691 level = btrfs_header_level(buf);
4692 if (btrfs_header_generation(buf) <=
4693 btrfs_root_last_snapshot(&root->root_item))
4694 first_cow = 1;
4695
4696 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID &&
4697 rc->create_reloc_tree) {
4698 WARN_ON(!first_cow && level == 0);
4699
4700 node = rc->backref_cache.path[level];
4701 BUG_ON(node->bytenr != buf->start &&
4702 node->new_bytenr != buf->start);
4703
4704 drop_node_buffer(node);
4705 extent_buffer_get(cow);
4706 node->eb = cow;
4707 node->new_bytenr = cow->start;
4708
4709 if (!node->pending) {
4710 list_move_tail(&node->list,
4711 &rc->backref_cache.pending[level]);
4712 node->pending = 1;
4713 }
4714
4715 if (first_cow)
4716 __mark_block_processed(rc, node);
4717
4718 if (first_cow && level > 0)
4719 rc->nodes_relocated += buf->len;
4720 }
4721
Josef Bacik83d4cfd2013-08-30 15:09:51 -04004722 if (level == 0 && first_cow && rc->stage == UPDATE_DATA_PTRS)
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004723 ret = replace_file_extents(trans, rc, root, cow);
Josef Bacik83d4cfd2013-08-30 15:09:51 -04004724 return ret;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004725}
4726
4727/*
4728 * called before creating snapshot. it calculates metadata reservation
Nicholas D Steeves01327612016-05-19 21:18:45 -04004729 * required for relocating tree blocks in the snapshot
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004730 */
Zhaolei147d2562015-08-06 20:58:11 +08004731void btrfs_reloc_pre_snapshot(struct btrfs_pending_snapshot *pending,
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004732 u64 *bytes_to_reserve)
4733{
4734 struct btrfs_root *root;
4735 struct reloc_control *rc;
4736
4737 root = pending->root;
4738 if (!root->reloc_root)
4739 return;
4740
4741 rc = root->fs_info->reloc_ctl;
4742 if (!rc->merge_reloc_tree)
4743 return;
4744
4745 root = root->reloc_root;
4746 BUG_ON(btrfs_root_refs(&root->root_item) == 0);
4747 /*
4748 * relocation is in the stage of merging trees. the space
4749 * used by merging a reloc tree is twice the size of
4750 * relocated tree nodes in the worst case. half for cowing
4751 * the reloc tree, half for cowing the fs tree. the space
4752 * used by cowing the reloc tree will be freed after the
4753 * tree is dropped. if we create snapshot, cowing the fs
4754 * tree may use more space than it frees. so we need
4755 * reserve extra space.
4756 */
4757 *bytes_to_reserve += rc->nodes_relocated;
4758}
4759
4760/*
4761 * called after snapshot is created. migrate block reservation
4762 * and create reloc root for the newly created snapshot
4763 */
Jeff Mahoney49b25e02012-03-01 17:24:58 +01004764int btrfs_reloc_post_snapshot(struct btrfs_trans_handle *trans,
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004765 struct btrfs_pending_snapshot *pending)
4766{
4767 struct btrfs_root *root = pending->root;
4768 struct btrfs_root *reloc_root;
4769 struct btrfs_root *new_root;
4770 struct reloc_control *rc;
4771 int ret;
4772
4773 if (!root->reloc_root)
Jeff Mahoney49b25e02012-03-01 17:24:58 +01004774 return 0;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004775
4776 rc = root->fs_info->reloc_ctl;
4777 rc->merging_rsv_size += rc->nodes_relocated;
4778
4779 if (rc->merge_reloc_tree) {
4780 ret = btrfs_block_rsv_migrate(&pending->block_rsv,
4781 rc->block_rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04004782 rc->nodes_relocated, 1);
Jeff Mahoney49b25e02012-03-01 17:24:58 +01004783 if (ret)
4784 return ret;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004785 }
4786
4787 new_root = pending->snap;
4788 reloc_root = create_reloc_root(trans, root->reloc_root,
4789 new_root->root_key.objectid);
Jeff Mahoney49b25e02012-03-01 17:24:58 +01004790 if (IS_ERR(reloc_root))
4791 return PTR_ERR(reloc_root);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004792
Jeff Mahoneyffd7b332011-10-03 23:23:15 -04004793 ret = __add_reloc_root(reloc_root);
4794 BUG_ON(ret < 0);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004795 new_root->reloc_root = reloc_root;
4796
Jeff Mahoney49b25e02012-03-01 17:24:58 +01004797 if (rc->create_reloc_tree)
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004798 ret = clone_backref_node(trans, rc, root, reloc_root);
Jeff Mahoney49b25e02012-03-01 17:24:58 +01004799 return ret;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004800}