blob: 27480ef9813cd4af9a3713b1ed6fcd64157147bd [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 Bacik75bfb9a2014-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 Bacik75bfb9a2014-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 Bacik75bfb9a2014-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 Bacik75bfb9a2014-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 Bacik75bfb9a2014-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 Bacik75bfb9a2014-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 Bacik75bfb9a2014-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];
926 WARN_ON(btrfs_node_blockptr(eb, path2->slots[level]) !=
927 cur->bytenr);
928
929 lower = cur;
Josef Bacikb6c60c82013-07-30 16:30:30 -0400930 need_check = true;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400931 for (; level < BTRFS_MAX_LEVEL; level++) {
932 if (!path2->nodes[level]) {
Josef Bacik75bfb9a2014-09-19 10:40:00 -0400933 ASSERT(btrfs_root_bytenr(&root->root_item) ==
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400934 lower->bytenr);
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400935 if (should_ignore_root(root))
936 list_add(&lower->list, &useless);
937 else
938 lower->root = root;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400939 break;
940 }
941
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400942 edge = alloc_backref_edge(cache);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400943 if (!edge) {
944 err = -ENOMEM;
945 goto out;
946 }
947
948 eb = path2->nodes[level];
949 rb_node = tree_search(&cache->rb_root, eb->start);
950 if (!rb_node) {
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400951 upper = alloc_backref_node(cache);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400952 if (!upper) {
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400953 free_backref_edge(cache, edge);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400954 err = -ENOMEM;
955 goto out;
956 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400957 upper->bytenr = eb->start;
958 upper->owner = btrfs_header_owner(eb);
959 upper->level = lower->level + 1;
Miao Xie27cdeb72014-04-02 19:51:05 +0800960 if (!test_bit(BTRFS_ROOT_REF_COWS,
961 &root->state))
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400962 upper->cowonly = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400963
964 /*
965 * if we know the block isn't shared
966 * we can void checking its backrefs.
967 */
968 if (btrfs_block_can_be_shared(root, eb))
969 upper->checked = 0;
970 else
971 upper->checked = 1;
972
973 /*
974 * add the block to pending list if we
Josef Bacikb6c60c82013-07-30 16:30:30 -0400975 * need check its backrefs, we only do this once
976 * while walking up a tree as we will catch
977 * anything else later on.
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400978 */
Josef Bacikb6c60c82013-07-30 16:30:30 -0400979 if (!upper->checked && need_check) {
980 need_check = false;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400981 list_add_tail(&edge->list[UPPER],
982 &list);
Josef Bacikbbe90512014-09-19 15:43:34 -0400983 } else {
984 if (upper->checked)
985 need_check = true;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400986 INIT_LIST_HEAD(&edge->list[UPPER]);
Josef Bacikbbe90512014-09-19 15:43:34 -0400987 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400988 } else {
989 upper = rb_entry(rb_node, struct backref_node,
990 rb_node);
Josef Bacik75bfb9a2014-09-19 10:40:00 -0400991 ASSERT(upper->checked);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400992 INIT_LIST_HEAD(&edge->list[UPPER]);
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400993 if (!upper->owner)
994 upper->owner = btrfs_header_owner(eb);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400995 }
996 list_add_tail(&edge->list[LOWER], &lower->upper);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400997 edge->node[LOWER] = lower;
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400998 edge->node[UPPER] = upper;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400999
1000 if (rb_node)
1001 break;
1002 lower = upper;
1003 upper = NULL;
1004 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001005 btrfs_release_path(path2);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001006next:
1007 if (ptr < end) {
1008 ptr += btrfs_extent_inline_ref_size(key.type);
1009 if (ptr >= end) {
1010 WARN_ON(ptr > end);
1011 ptr = 0;
1012 end = 0;
1013 }
1014 }
1015 if (ptr >= end)
1016 path1->slots[0]++;
1017 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001018 btrfs_release_path(path1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001019
1020 cur->checked = 1;
1021 WARN_ON(exist);
1022
1023 /* the pending list isn't empty, take the first block to process */
1024 if (!list_empty(&list)) {
1025 edge = list_entry(list.next, struct backref_edge, list[UPPER]);
1026 list_del_init(&edge->list[UPPER]);
1027 cur = edge->node[UPPER];
1028 goto again;
1029 }
1030
1031 /*
1032 * everything goes well, connect backref nodes and insert backref nodes
1033 * into the cache.
1034 */
Josef Bacik75bfb9a2014-09-19 10:40:00 -04001035 ASSERT(node->checked);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001036 cowonly = node->cowonly;
1037 if (!cowonly) {
1038 rb_node = tree_insert(&cache->rb_root, node->bytenr,
1039 &node->rb_node);
Jeff Mahoney43c04fb2011-10-03 23:22:33 -04001040 if (rb_node)
1041 backref_tree_panic(rb_node, -EEXIST, node->bytenr);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001042 list_add_tail(&node->lower, &cache->leaves);
1043 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001044
1045 list_for_each_entry(edge, &node->upper, list[LOWER])
1046 list_add_tail(&edge->list[UPPER], &list);
1047
1048 while (!list_empty(&list)) {
1049 edge = list_entry(list.next, struct backref_edge, list[UPPER]);
1050 list_del_init(&edge->list[UPPER]);
1051 upper = edge->node[UPPER];
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001052 if (upper->detached) {
1053 list_del(&edge->list[LOWER]);
1054 lower = edge->node[LOWER];
1055 free_backref_edge(cache, edge);
1056 if (list_empty(&lower->upper))
1057 list_add(&lower->list, &useless);
1058 continue;
1059 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001060
1061 if (!RB_EMPTY_NODE(&upper->rb_node)) {
1062 if (upper->lowest) {
1063 list_del_init(&upper->lower);
1064 upper->lowest = 0;
1065 }
1066
1067 list_add_tail(&edge->list[UPPER], &upper->lower);
1068 continue;
1069 }
1070
Josef Bacik75bfb9a2014-09-19 10:40:00 -04001071 if (!upper->checked) {
1072 /*
1073 * Still want to blow up for developers since this is a
1074 * logic bug.
1075 */
1076 ASSERT(0);
1077 err = -EINVAL;
1078 goto out;
1079 }
1080 if (cowonly != upper->cowonly) {
1081 ASSERT(0);
1082 err = -EINVAL;
1083 goto out;
1084 }
1085
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001086 if (!cowonly) {
1087 rb_node = tree_insert(&cache->rb_root, upper->bytenr,
1088 &upper->rb_node);
Jeff Mahoney43c04fb2011-10-03 23:22:33 -04001089 if (rb_node)
1090 backref_tree_panic(rb_node, -EEXIST,
1091 upper->bytenr);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001092 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001093
1094 list_add_tail(&edge->list[UPPER], &upper->lower);
1095
1096 list_for_each_entry(edge, &upper->upper, list[LOWER])
1097 list_add_tail(&edge->list[UPPER], &list);
1098 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001099 /*
1100 * process useless backref nodes. backref nodes for tree leaves
1101 * are deleted from the cache. backref nodes for upper level
1102 * tree blocks are left in the cache to avoid unnecessary backref
1103 * lookup.
1104 */
1105 while (!list_empty(&useless)) {
1106 upper = list_entry(useless.next, struct backref_node, list);
1107 list_del_init(&upper->list);
Josef Bacik75bfb9a2014-09-19 10:40:00 -04001108 ASSERT(list_empty(&upper->upper));
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001109 if (upper == node)
1110 node = NULL;
1111 if (upper->lowest) {
1112 list_del_init(&upper->lower);
1113 upper->lowest = 0;
1114 }
1115 while (!list_empty(&upper->lower)) {
1116 edge = list_entry(upper->lower.next,
1117 struct backref_edge, list[UPPER]);
1118 list_del(&edge->list[UPPER]);
1119 list_del(&edge->list[LOWER]);
1120 lower = edge->node[LOWER];
1121 free_backref_edge(cache, edge);
1122
1123 if (list_empty(&lower->upper))
1124 list_add(&lower->list, &useless);
1125 }
1126 __mark_block_processed(rc, upper);
1127 if (upper->level > 0) {
1128 list_add(&upper->list, &cache->detached);
1129 upper->detached = 1;
1130 } else {
1131 rb_erase(&upper->rb_node, &cache->rb_root);
1132 free_backref_node(cache, upper);
1133 }
1134 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001135out:
1136 btrfs_free_path(path1);
1137 btrfs_free_path(path2);
1138 if (err) {
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001139 while (!list_empty(&useless)) {
1140 lower = list_entry(useless.next,
Josef Bacik75bfb9a2014-09-19 10:40:00 -04001141 struct backref_node, list);
1142 list_del_init(&lower->list);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001143 }
Josef Bacik75bfb9a2014-09-19 10:40:00 -04001144 while (!list_empty(&list)) {
1145 edge = list_first_entry(&list, struct backref_edge,
1146 list[UPPER]);
1147 list_del(&edge->list[UPPER]);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001148 list_del(&edge->list[LOWER]);
Josef Bacik75bfb9a2014-09-19 10:40:00 -04001149 lower = edge->node[LOWER];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001150 upper = edge->node[UPPER];
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001151 free_backref_edge(cache, edge);
Josef Bacik75bfb9a2014-09-19 10:40:00 -04001152
1153 /*
1154 * Lower is no longer linked to any upper backref nodes
1155 * and isn't in the cache, we can free it ourselves.
1156 */
1157 if (list_empty(&lower->upper) &&
1158 RB_EMPTY_NODE(&lower->rb_node))
1159 list_add(&lower->list, &useless);
1160
1161 if (!RB_EMPTY_NODE(&upper->rb_node))
1162 continue;
1163
Nicholas D Steeves01327612016-05-19 21:18:45 -04001164 /* Add this guy's upper edges to the list to process */
Josef Bacik75bfb9a2014-09-19 10:40:00 -04001165 list_for_each_entry(edge, &upper->upper, list[LOWER])
1166 list_add_tail(&edge->list[UPPER], &list);
1167 if (list_empty(&upper->upper))
1168 list_add(&upper->list, &useless);
1169 }
1170
1171 while (!list_empty(&useless)) {
1172 lower = list_entry(useless.next,
1173 struct backref_node, list);
1174 list_del_init(&lower->list);
Liu Bo0fd8c3d2016-07-12 10:29:37 -07001175 if (lower == node)
1176 node = NULL;
Josef Bacik75bfb9a2014-09-19 10:40:00 -04001177 free_backref_node(cache, lower);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001178 }
Liu Bo0fd8c3d2016-07-12 10:29:37 -07001179
1180 free_backref_node(cache, node);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001181 return ERR_PTR(err);
1182 }
Josef Bacik75bfb9a2014-09-19 10:40:00 -04001183 ASSERT(!node || !node->detached);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001184 return node;
1185}
1186
1187/*
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001188 * helper to add backref node for the newly created snapshot.
1189 * the backref node is created by cloning backref node that
1190 * corresponds to root of source tree
1191 */
1192static int clone_backref_node(struct btrfs_trans_handle *trans,
1193 struct reloc_control *rc,
1194 struct btrfs_root *src,
1195 struct btrfs_root *dest)
1196{
1197 struct btrfs_root *reloc_root = src->reloc_root;
1198 struct backref_cache *cache = &rc->backref_cache;
1199 struct backref_node *node = NULL;
1200 struct backref_node *new_node;
1201 struct backref_edge *edge;
1202 struct backref_edge *new_edge;
1203 struct rb_node *rb_node;
1204
1205 if (cache->last_trans > 0)
1206 update_backref_cache(trans, cache);
1207
1208 rb_node = tree_search(&cache->rb_root, src->commit_root->start);
1209 if (rb_node) {
1210 node = rb_entry(rb_node, struct backref_node, rb_node);
1211 if (node->detached)
1212 node = NULL;
1213 else
1214 BUG_ON(node->new_bytenr != reloc_root->node->start);
1215 }
1216
1217 if (!node) {
1218 rb_node = tree_search(&cache->rb_root,
1219 reloc_root->commit_root->start);
1220 if (rb_node) {
1221 node = rb_entry(rb_node, struct backref_node,
1222 rb_node);
1223 BUG_ON(node->detached);
1224 }
1225 }
1226
1227 if (!node)
1228 return 0;
1229
1230 new_node = alloc_backref_node(cache);
1231 if (!new_node)
1232 return -ENOMEM;
1233
1234 new_node->bytenr = dest->node->start;
1235 new_node->level = node->level;
1236 new_node->lowest = node->lowest;
Yan, Zheng6848ad62011-02-14 16:00:03 -05001237 new_node->checked = 1;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001238 new_node->root = dest;
1239
1240 if (!node->lowest) {
1241 list_for_each_entry(edge, &node->lower, list[UPPER]) {
1242 new_edge = alloc_backref_edge(cache);
1243 if (!new_edge)
1244 goto fail;
1245
1246 new_edge->node[UPPER] = new_node;
1247 new_edge->node[LOWER] = edge->node[LOWER];
1248 list_add_tail(&new_edge->list[UPPER],
1249 &new_node->lower);
1250 }
Miao Xie76b9e232011-11-10 20:45:05 -05001251 } else {
1252 list_add_tail(&new_node->lower, &cache->leaves);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001253 }
1254
1255 rb_node = tree_insert(&cache->rb_root, new_node->bytenr,
1256 &new_node->rb_node);
Jeff Mahoney43c04fb2011-10-03 23:22:33 -04001257 if (rb_node)
1258 backref_tree_panic(rb_node, -EEXIST, new_node->bytenr);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001259
1260 if (!new_node->lowest) {
1261 list_for_each_entry(new_edge, &new_node->lower, list[UPPER]) {
1262 list_add_tail(&new_edge->list[LOWER],
1263 &new_edge->node[LOWER]->upper);
1264 }
1265 }
1266 return 0;
1267fail:
1268 while (!list_empty(&new_node->lower)) {
1269 new_edge = list_entry(new_node->lower.next,
1270 struct backref_edge, list[UPPER]);
1271 list_del(&new_edge->list[UPPER]);
1272 free_backref_edge(cache, new_edge);
1273 }
1274 free_backref_node(cache, new_node);
1275 return -ENOMEM;
1276}
1277
1278/*
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001279 * helper to add 'address of tree root -> reloc tree' mapping
1280 */
Jeff Mahoneyffd7b332011-10-03 23:23:15 -04001281static int __must_check __add_reloc_root(struct btrfs_root *root)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001282{
1283 struct rb_node *rb_node;
1284 struct mapping_node *node;
1285 struct reloc_control *rc = root->fs_info->reloc_ctl;
1286
1287 node = kmalloc(sizeof(*node), GFP_NOFS);
Jeff Mahoneyffd7b332011-10-03 23:23:15 -04001288 if (!node)
1289 return -ENOMEM;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001290
1291 node->bytenr = root->node->start;
1292 node->data = root;
1293
1294 spin_lock(&rc->reloc_root_tree.lock);
1295 rb_node = tree_insert(&rc->reloc_root_tree.rb_root,
1296 node->bytenr, &node->rb_node);
1297 spin_unlock(&rc->reloc_root_tree.lock);
Jeff Mahoneyffd7b332011-10-03 23:23:15 -04001298 if (rb_node) {
Jeff Mahoneyffd7b332011-10-03 23:23:15 -04001299 btrfs_panic(root->fs_info, -EEXIST, "Duplicate root found "
1300 "for start=%llu while inserting into relocation "
David Sterba351fd352014-05-15 16:48:20 +02001301 "tree", node->bytenr);
Dan Carpenter23291a02012-06-25 05:15:23 -06001302 kfree(node);
1303 return -EEXIST;
Jeff Mahoneyffd7b332011-10-03 23:23:15 -04001304 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001305
1306 list_add_tail(&root->root_list, &rc->reloc_roots);
1307 return 0;
1308}
1309
1310/*
Wang Shilongc974c462013-12-11 19:29:51 +08001311 * helper to delete the 'address of tree root -> reloc tree'
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001312 * mapping
1313 */
Wang Shilongc974c462013-12-11 19:29:51 +08001314static void __del_reloc_root(struct btrfs_root *root)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001315{
1316 struct rb_node *rb_node;
1317 struct mapping_node *node = NULL;
1318 struct reloc_control *rc = root->fs_info->reloc_ctl;
1319
1320 spin_lock(&rc->reloc_root_tree.lock);
1321 rb_node = tree_search(&rc->reloc_root_tree.rb_root,
Wang Shilongc974c462013-12-11 19:29:51 +08001322 root->node->start);
1323 if (rb_node) {
1324 node = rb_entry(rb_node, struct mapping_node, rb_node);
1325 rb_erase(&node->rb_node, &rc->reloc_root_tree.rb_root);
1326 }
1327 spin_unlock(&rc->reloc_root_tree.lock);
1328
1329 if (!node)
1330 return;
1331 BUG_ON((struct btrfs_root *)node->data != root);
1332
1333 spin_lock(&root->fs_info->trans_lock);
1334 list_del_init(&root->root_list);
1335 spin_unlock(&root->fs_info->trans_lock);
1336 kfree(node);
1337}
1338
1339/*
1340 * helper to update the 'address of tree root -> reloc tree'
1341 * mapping
1342 */
1343static int __update_reloc_root(struct btrfs_root *root, u64 new_bytenr)
1344{
1345 struct rb_node *rb_node;
1346 struct mapping_node *node = NULL;
1347 struct reloc_control *rc = root->fs_info->reloc_ctl;
1348
1349 spin_lock(&rc->reloc_root_tree.lock);
1350 rb_node = tree_search(&rc->reloc_root_tree.rb_root,
1351 root->node->start);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001352 if (rb_node) {
1353 node = rb_entry(rb_node, struct mapping_node, rb_node);
1354 rb_erase(&node->rb_node, &rc->reloc_root_tree.rb_root);
1355 }
1356 spin_unlock(&rc->reloc_root_tree.lock);
1357
Liu Bo8f71f3e2013-03-04 16:25:36 +00001358 if (!node)
1359 return 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001360 BUG_ON((struct btrfs_root *)node->data != root);
1361
Wang Shilongc974c462013-12-11 19:29:51 +08001362 spin_lock(&rc->reloc_root_tree.lock);
1363 node->bytenr = new_bytenr;
1364 rb_node = tree_insert(&rc->reloc_root_tree.rb_root,
1365 node->bytenr, &node->rb_node);
1366 spin_unlock(&rc->reloc_root_tree.lock);
1367 if (rb_node)
1368 backref_tree_panic(rb_node, -EEXIST, node->bytenr);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001369 return 0;
1370}
1371
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001372static struct btrfs_root *create_reloc_root(struct btrfs_trans_handle *trans,
1373 struct btrfs_root *root, u64 objectid)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001374{
1375 struct btrfs_root *reloc_root;
1376 struct extent_buffer *eb;
1377 struct btrfs_root_item *root_item;
1378 struct btrfs_key root_key;
Miao Xie5bc72472013-06-06 03:28:03 +00001379 u64 last_snap = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001380 int ret;
1381
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001382 root_item = kmalloc(sizeof(*root_item), GFP_NOFS);
1383 BUG_ON(!root_item);
1384
1385 root_key.objectid = BTRFS_TREE_RELOC_OBJECTID;
1386 root_key.type = BTRFS_ROOT_ITEM_KEY;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001387 root_key.offset = objectid;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001388
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001389 if (root->root_key.objectid == objectid) {
1390 /* called by btrfs_init_reloc_root */
1391 ret = btrfs_copy_root(trans, root, root->commit_root, &eb,
1392 BTRFS_TREE_RELOC_OBJECTID);
1393 BUG_ON(ret);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001394
Miao Xie5bc72472013-06-06 03:28:03 +00001395 last_snap = btrfs_root_last_snapshot(&root->root_item);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001396 btrfs_set_root_last_snapshot(&root->root_item,
1397 trans->transid - 1);
1398 } else {
1399 /*
1400 * called by btrfs_reloc_post_snapshot_hook.
1401 * the source tree is a reloc tree, all tree blocks
1402 * modified after it was created have RELOC flag
1403 * set in their headers. so it's OK to not update
1404 * the 'last_snapshot'.
1405 */
1406 ret = btrfs_copy_root(trans, root, root->node, &eb,
1407 BTRFS_TREE_RELOC_OBJECTID);
1408 BUG_ON(ret);
1409 }
1410
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001411 memcpy(root_item, &root->root_item, sizeof(*root_item));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001412 btrfs_set_root_bytenr(root_item, eb->start);
1413 btrfs_set_root_level(root_item, btrfs_header_level(eb));
1414 btrfs_set_root_generation(root_item, trans->transid);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001415
1416 if (root->root_key.objectid == objectid) {
1417 btrfs_set_root_refs(root_item, 0);
1418 memset(&root_item->drop_progress, 0,
1419 sizeof(struct btrfs_disk_key));
1420 root_item->drop_level = 0;
Miao Xie5bc72472013-06-06 03:28:03 +00001421 /*
1422 * abuse rtransid, it is safe because it is impossible to
1423 * receive data into a relocation tree.
1424 */
1425 btrfs_set_root_rtransid(root_item, last_snap);
1426 btrfs_set_root_otransid(root_item, trans->transid);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001427 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001428
1429 btrfs_tree_unlock(eb);
1430 free_extent_buffer(eb);
1431
1432 ret = btrfs_insert_root(trans, root->fs_info->tree_root,
1433 &root_key, root_item);
1434 BUG_ON(ret);
1435 kfree(root_item);
1436
Miao Xiecb517ea2013-05-15 07:48:19 +00001437 reloc_root = btrfs_read_fs_root(root->fs_info->tree_root, &root_key);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001438 BUG_ON(IS_ERR(reloc_root));
1439 reloc_root->last_trans = trans->transid;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001440 return reloc_root;
1441}
1442
1443/*
1444 * create reloc tree for a given fs tree. reloc tree is just a
1445 * snapshot of the fs tree with special root objectid.
1446 */
1447int btrfs_init_reloc_root(struct btrfs_trans_handle *trans,
1448 struct btrfs_root *root)
1449{
1450 struct btrfs_root *reloc_root;
1451 struct reloc_control *rc = root->fs_info->reloc_ctl;
Miao Xie20dd2cb2013-09-25 21:47:45 +08001452 struct btrfs_block_rsv *rsv;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001453 int clear_rsv = 0;
Jeff Mahoneyffd7b332011-10-03 23:23:15 -04001454 int ret;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001455
1456 if (root->reloc_root) {
1457 reloc_root = root->reloc_root;
1458 reloc_root->last_trans = trans->transid;
1459 return 0;
1460 }
1461
1462 if (!rc || !rc->create_reloc_tree ||
1463 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
1464 return 0;
1465
Miao Xie20dd2cb2013-09-25 21:47:45 +08001466 if (!trans->reloc_reserved) {
1467 rsv = trans->block_rsv;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001468 trans->block_rsv = rc->block_rsv;
1469 clear_rsv = 1;
1470 }
1471 reloc_root = create_reloc_root(trans, root, root->root_key.objectid);
1472 if (clear_rsv)
Miao Xie20dd2cb2013-09-25 21:47:45 +08001473 trans->block_rsv = rsv;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001474
Jeff Mahoneyffd7b332011-10-03 23:23:15 -04001475 ret = __add_reloc_root(reloc_root);
1476 BUG_ON(ret < 0);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001477 root->reloc_root = reloc_root;
1478 return 0;
1479}
1480
1481/*
1482 * update root item of reloc tree
1483 */
1484int btrfs_update_reloc_root(struct btrfs_trans_handle *trans,
1485 struct btrfs_root *root)
1486{
1487 struct btrfs_root *reloc_root;
1488 struct btrfs_root_item *root_item;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001489 int ret;
1490
1491 if (!root->reloc_root)
Chris Mason75857172011-06-13 20:00:16 -04001492 goto out;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001493
1494 reloc_root = root->reloc_root;
1495 root_item = &reloc_root->root_item;
1496
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001497 if (root->fs_info->reloc_ctl->merge_reloc_tree &&
1498 btrfs_root_refs(root_item) == 0) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001499 root->reloc_root = NULL;
Wang Shilongc974c462013-12-11 19:29:51 +08001500 __del_reloc_root(reloc_root);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001501 }
1502
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001503 if (reloc_root->commit_root != reloc_root->node) {
1504 btrfs_set_root_node(root_item, reloc_root->node);
1505 free_extent_buffer(reloc_root->commit_root);
1506 reloc_root->commit_root = btrfs_root_node(reloc_root);
1507 }
1508
1509 ret = btrfs_update_root(trans, root->fs_info->tree_root,
1510 &reloc_root->root_key, root_item);
1511 BUG_ON(ret);
Chris Mason75857172011-06-13 20:00:16 -04001512
1513out:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001514 return 0;
1515}
1516
1517/*
1518 * helper to find first cached inode with inode number >= objectid
1519 * in a subvolume
1520 */
1521static struct inode *find_next_inode(struct btrfs_root *root, u64 objectid)
1522{
1523 struct rb_node *node;
1524 struct rb_node *prev;
1525 struct btrfs_inode *entry;
1526 struct inode *inode;
1527
1528 spin_lock(&root->inode_lock);
1529again:
1530 node = root->inode_tree.rb_node;
1531 prev = NULL;
1532 while (node) {
1533 prev = node;
1534 entry = rb_entry(node, struct btrfs_inode, rb_node);
1535
Li Zefan33345d012011-04-20 10:31:50 +08001536 if (objectid < btrfs_ino(&entry->vfs_inode))
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001537 node = node->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08001538 else if (objectid > btrfs_ino(&entry->vfs_inode))
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001539 node = node->rb_right;
1540 else
1541 break;
1542 }
1543 if (!node) {
1544 while (prev) {
1545 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08001546 if (objectid <= btrfs_ino(&entry->vfs_inode)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001547 node = prev;
1548 break;
1549 }
1550 prev = rb_next(prev);
1551 }
1552 }
1553 while (node) {
1554 entry = rb_entry(node, struct btrfs_inode, rb_node);
1555 inode = igrab(&entry->vfs_inode);
1556 if (inode) {
1557 spin_unlock(&root->inode_lock);
1558 return inode;
1559 }
1560
Li Zefan33345d012011-04-20 10:31:50 +08001561 objectid = btrfs_ino(&entry->vfs_inode) + 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001562 if (cond_resched_lock(&root->inode_lock))
1563 goto again;
1564
1565 node = rb_next(node);
1566 }
1567 spin_unlock(&root->inode_lock);
1568 return NULL;
1569}
1570
1571static int in_block_group(u64 bytenr,
1572 struct btrfs_block_group_cache *block_group)
1573{
1574 if (bytenr >= block_group->key.objectid &&
1575 bytenr < block_group->key.objectid + block_group->key.offset)
1576 return 1;
1577 return 0;
1578}
1579
1580/*
1581 * get new location of data
1582 */
1583static int get_new_location(struct inode *reloc_inode, u64 *new_bytenr,
1584 u64 bytenr, u64 num_bytes)
1585{
1586 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
1587 struct btrfs_path *path;
1588 struct btrfs_file_extent_item *fi;
1589 struct extent_buffer *leaf;
1590 int ret;
1591
1592 path = btrfs_alloc_path();
1593 if (!path)
1594 return -ENOMEM;
1595
1596 bytenr -= BTRFS_I(reloc_inode)->index_cnt;
Li Zefan33345d012011-04-20 10:31:50 +08001597 ret = btrfs_lookup_file_extent(NULL, root, path, btrfs_ino(reloc_inode),
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001598 bytenr, 0);
1599 if (ret < 0)
1600 goto out;
1601 if (ret > 0) {
1602 ret = -ENOENT;
1603 goto out;
1604 }
1605
1606 leaf = path->nodes[0];
1607 fi = btrfs_item_ptr(leaf, path->slots[0],
1608 struct btrfs_file_extent_item);
1609
1610 BUG_ON(btrfs_file_extent_offset(leaf, fi) ||
1611 btrfs_file_extent_compression(leaf, fi) ||
1612 btrfs_file_extent_encryption(leaf, fi) ||
1613 btrfs_file_extent_other_encoding(leaf, fi));
1614
1615 if (num_bytes != btrfs_file_extent_disk_num_bytes(leaf, fi)) {
Josef Bacik83d4cfd2013-08-30 15:09:51 -04001616 ret = -EINVAL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001617 goto out;
1618 }
1619
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001620 *new_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001621 ret = 0;
1622out:
1623 btrfs_free_path(path);
1624 return ret;
1625}
1626
1627/*
1628 * update file extent items in the tree leaf to point to
1629 * the new locations.
1630 */
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001631static noinline_for_stack
1632int replace_file_extents(struct btrfs_trans_handle *trans,
1633 struct reloc_control *rc,
1634 struct btrfs_root *root,
1635 struct extent_buffer *leaf)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001636{
1637 struct btrfs_key key;
1638 struct btrfs_file_extent_item *fi;
1639 struct inode *inode = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001640 u64 parent;
1641 u64 bytenr;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001642 u64 new_bytenr = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001643 u64 num_bytes;
1644 u64 end;
1645 u32 nritems;
1646 u32 i;
Josef Bacik83d4cfd2013-08-30 15:09:51 -04001647 int ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001648 int first = 1;
1649 int dirty = 0;
1650
1651 if (rc->stage != UPDATE_DATA_PTRS)
1652 return 0;
1653
1654 /* reloc trees always use full backref */
1655 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
1656 parent = leaf->start;
1657 else
1658 parent = 0;
1659
1660 nritems = btrfs_header_nritems(leaf);
1661 for (i = 0; i < nritems; i++) {
1662 cond_resched();
1663 btrfs_item_key_to_cpu(leaf, &key, i);
1664 if (key.type != BTRFS_EXTENT_DATA_KEY)
1665 continue;
1666 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
1667 if (btrfs_file_extent_type(leaf, fi) ==
1668 BTRFS_FILE_EXTENT_INLINE)
1669 continue;
1670 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
1671 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
1672 if (bytenr == 0)
1673 continue;
1674 if (!in_block_group(bytenr, rc->block_group))
1675 continue;
1676
1677 /*
1678 * if we are modifying block in fs tree, wait for readpage
1679 * to complete and drop the extent cache
1680 */
1681 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001682 if (first) {
1683 inode = find_next_inode(root, key.objectid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001684 first = 0;
Li Zefan33345d012011-04-20 10:31:50 +08001685 } else if (inode && btrfs_ino(inode) < key.objectid) {
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001686 btrfs_add_delayed_iput(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001687 inode = find_next_inode(root, key.objectid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001688 }
Li Zefan33345d012011-04-20 10:31:50 +08001689 if (inode && btrfs_ino(inode) == key.objectid) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001690 end = key.offset +
1691 btrfs_file_extent_num_bytes(leaf, fi);
1692 WARN_ON(!IS_ALIGNED(key.offset,
1693 root->sectorsize));
1694 WARN_ON(!IS_ALIGNED(end, root->sectorsize));
1695 end--;
1696 ret = try_lock_extent(&BTRFS_I(inode)->io_tree,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001697 key.offset, end);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001698 if (!ret)
1699 continue;
1700
1701 btrfs_drop_extent_cache(inode, key.offset, end,
1702 1);
1703 unlock_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 }
1706 }
1707
1708 ret = get_new_location(rc->data_inode, &new_bytenr,
1709 bytenr, num_bytes);
Josef Bacik83d4cfd2013-08-30 15:09:51 -04001710 if (ret) {
1711 /*
1712 * Don't have to abort since we've not changed anything
1713 * in the file extent yet.
1714 */
1715 break;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001716 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001717
1718 btrfs_set_file_extent_disk_bytenr(leaf, fi, new_bytenr);
1719 dirty = 1;
1720
1721 key.offset -= btrfs_file_extent_offset(leaf, fi);
1722 ret = btrfs_inc_extent_ref(trans, root, new_bytenr,
1723 num_bytes, parent,
1724 btrfs_header_owner(leaf),
Filipe Mananab06c4bf2015-10-23 07:52:54 +01001725 key.objectid, key.offset);
Josef Bacik83d4cfd2013-08-30 15:09:51 -04001726 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04001727 btrfs_abort_transaction(trans, ret);
Josef Bacik83d4cfd2013-08-30 15:09:51 -04001728 break;
1729 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001730
1731 ret = btrfs_free_extent(trans, root, bytenr, num_bytes,
1732 parent, btrfs_header_owner(leaf),
Filipe Mananab06c4bf2015-10-23 07:52:54 +01001733 key.objectid, key.offset);
Josef Bacik83d4cfd2013-08-30 15:09:51 -04001734 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04001735 btrfs_abort_transaction(trans, ret);
Josef Bacik83d4cfd2013-08-30 15:09:51 -04001736 break;
1737 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001738 }
1739 if (dirty)
1740 btrfs_mark_buffer_dirty(leaf);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001741 if (inode)
1742 btrfs_add_delayed_iput(inode);
Josef Bacik83d4cfd2013-08-30 15:09:51 -04001743 return ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001744}
1745
1746static noinline_for_stack
1747int memcmp_node_keys(struct extent_buffer *eb, int slot,
1748 struct btrfs_path *path, int level)
1749{
1750 struct btrfs_disk_key key1;
1751 struct btrfs_disk_key key2;
1752 btrfs_node_key(eb, &key1, slot);
1753 btrfs_node_key(path->nodes[level], &key2, path->slots[level]);
1754 return memcmp(&key1, &key2, sizeof(key1));
1755}
1756
1757/*
1758 * try to replace tree blocks in fs tree with the new blocks
1759 * in reloc tree. tree blocks haven't been modified since the
1760 * reloc tree was create can be replaced.
1761 *
1762 * if a block was replaced, level of the block + 1 is returned.
1763 * if no block got replaced, 0 is returned. if there are other
1764 * errors, a negative error number is returned.
1765 */
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001766static noinline_for_stack
1767int replace_path(struct btrfs_trans_handle *trans,
1768 struct btrfs_root *dest, struct btrfs_root *src,
1769 struct btrfs_path *path, struct btrfs_key *next_key,
1770 int lowest_level, int max_level)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001771{
1772 struct extent_buffer *eb;
1773 struct extent_buffer *parent;
1774 struct btrfs_key key;
1775 u64 old_bytenr;
1776 u64 new_bytenr;
1777 u64 old_ptr_gen;
1778 u64 new_ptr_gen;
1779 u64 last_snapshot;
1780 u32 blocksize;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001781 int cow = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001782 int level;
1783 int ret;
1784 int slot;
1785
1786 BUG_ON(src->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
1787 BUG_ON(dest->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001788
1789 last_snapshot = btrfs_root_last_snapshot(&src->root_item);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001790again:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001791 slot = path->slots[lowest_level];
1792 btrfs_node_key_to_cpu(path->nodes[lowest_level], &key, slot);
1793
1794 eb = btrfs_lock_root_node(dest);
1795 btrfs_set_lock_blocking(eb);
1796 level = btrfs_header_level(eb);
1797
1798 if (level < lowest_level) {
1799 btrfs_tree_unlock(eb);
1800 free_extent_buffer(eb);
1801 return 0;
1802 }
1803
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001804 if (cow) {
1805 ret = btrfs_cow_block(trans, dest, eb, NULL, 0, &eb);
1806 BUG_ON(ret);
1807 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001808 btrfs_set_lock_blocking(eb);
1809
1810 if (next_key) {
1811 next_key->objectid = (u64)-1;
1812 next_key->type = (u8)-1;
1813 next_key->offset = (u64)-1;
1814 }
1815
1816 parent = eb;
1817 while (1) {
1818 level = btrfs_header_level(parent);
1819 BUG_ON(level < lowest_level);
1820
1821 ret = btrfs_bin_search(parent, &key, level, &slot);
1822 if (ret && slot > 0)
1823 slot--;
1824
1825 if (next_key && slot + 1 < btrfs_header_nritems(parent))
1826 btrfs_node_key_to_cpu(parent, next_key, slot + 1);
1827
1828 old_bytenr = btrfs_node_blockptr(parent, slot);
David Sterba707e8a02014-06-04 19:22:26 +02001829 blocksize = dest->nodesize;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001830 old_ptr_gen = btrfs_node_ptr_generation(parent, slot);
1831
1832 if (level <= max_level) {
1833 eb = path->nodes[level];
1834 new_bytenr = btrfs_node_blockptr(eb,
1835 path->slots[level]);
1836 new_ptr_gen = btrfs_node_ptr_generation(eb,
1837 path->slots[level]);
1838 } else {
1839 new_bytenr = 0;
1840 new_ptr_gen = 0;
1841 }
1842
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05301843 if (WARN_ON(new_bytenr > 0 && new_bytenr == old_bytenr)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001844 ret = level;
1845 break;
1846 }
1847
1848 if (new_bytenr == 0 || old_ptr_gen > last_snapshot ||
1849 memcmp_node_keys(parent, slot, path, level)) {
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001850 if (level <= lowest_level) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001851 ret = 0;
1852 break;
1853 }
1854
David Sterbace86cd52014-06-15 01:07:32 +02001855 eb = read_tree_block(dest, old_bytenr, old_ptr_gen);
Liu Bo64c043d2015-05-25 17:30:15 +08001856 if (IS_ERR(eb)) {
1857 ret = PTR_ERR(eb);
Liu Bo264813a2016-03-21 14:59:53 -07001858 break;
Liu Bo64c043d2015-05-25 17:30:15 +08001859 } else if (!extent_buffer_uptodate(eb)) {
1860 ret = -EIO;
Josef Bacik416bc652013-04-23 14:17:42 -04001861 free_extent_buffer(eb);
Stefan Behrens379cde72013-05-08 08:56:09 +00001862 break;
Josef Bacik416bc652013-04-23 14:17:42 -04001863 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001864 btrfs_tree_lock(eb);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001865 if (cow) {
1866 ret = btrfs_cow_block(trans, dest, eb, parent,
1867 slot, &eb);
1868 BUG_ON(ret);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001869 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001870 btrfs_set_lock_blocking(eb);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001871
1872 btrfs_tree_unlock(parent);
1873 free_extent_buffer(parent);
1874
1875 parent = eb;
1876 continue;
1877 }
1878
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001879 if (!cow) {
1880 btrfs_tree_unlock(parent);
1881 free_extent_buffer(parent);
1882 cow = 1;
1883 goto again;
1884 }
1885
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001886 btrfs_node_key_to_cpu(path->nodes[level], &key,
1887 path->slots[level]);
David Sterbab3b4aa72011-04-21 01:20:15 +02001888 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001889
1890 path->lowest_level = level;
1891 ret = btrfs_search_slot(trans, src, &key, path, 0, 1);
1892 path->lowest_level = 0;
1893 BUG_ON(ret);
1894
1895 /*
1896 * swap blocks in fs tree and reloc tree.
1897 */
1898 btrfs_set_node_blockptr(parent, slot, new_bytenr);
1899 btrfs_set_node_ptr_generation(parent, slot, new_ptr_gen);
1900 btrfs_mark_buffer_dirty(parent);
1901
1902 btrfs_set_node_blockptr(path->nodes[level],
1903 path->slots[level], old_bytenr);
1904 btrfs_set_node_ptr_generation(path->nodes[level],
1905 path->slots[level], old_ptr_gen);
1906 btrfs_mark_buffer_dirty(path->nodes[level]);
1907
1908 ret = btrfs_inc_extent_ref(trans, src, old_bytenr, blocksize,
1909 path->nodes[level]->start,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01001910 src->root_key.objectid, level - 1, 0);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001911 BUG_ON(ret);
1912 ret = btrfs_inc_extent_ref(trans, dest, new_bytenr, blocksize,
1913 0, dest->root_key.objectid, level - 1,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01001914 0);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001915 BUG_ON(ret);
1916
1917 ret = btrfs_free_extent(trans, src, new_bytenr, blocksize,
1918 path->nodes[level]->start,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01001919 src->root_key.objectid, level - 1, 0);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001920 BUG_ON(ret);
1921
1922 ret = btrfs_free_extent(trans, dest, old_bytenr, blocksize,
1923 0, dest->root_key.objectid, level - 1,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01001924 0);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001925 BUG_ON(ret);
1926
1927 btrfs_unlock_up_safe(path, 0);
1928
1929 ret = level;
1930 break;
1931 }
1932 btrfs_tree_unlock(parent);
1933 free_extent_buffer(parent);
1934 return ret;
1935}
1936
1937/*
1938 * helper to find next relocated block in reloc tree
1939 */
1940static noinline_for_stack
1941int walk_up_reloc_tree(struct btrfs_root *root, struct btrfs_path *path,
1942 int *level)
1943{
1944 struct extent_buffer *eb;
1945 int i;
1946 u64 last_snapshot;
1947 u32 nritems;
1948
1949 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
1950
1951 for (i = 0; i < *level; i++) {
1952 free_extent_buffer(path->nodes[i]);
1953 path->nodes[i] = NULL;
1954 }
1955
1956 for (i = *level; i < BTRFS_MAX_LEVEL && path->nodes[i]; i++) {
1957 eb = path->nodes[i];
1958 nritems = btrfs_header_nritems(eb);
1959 while (path->slots[i] + 1 < nritems) {
1960 path->slots[i]++;
1961 if (btrfs_node_ptr_generation(eb, path->slots[i]) <=
1962 last_snapshot)
1963 continue;
1964
1965 *level = i;
1966 return 0;
1967 }
1968 free_extent_buffer(path->nodes[i]);
1969 path->nodes[i] = NULL;
1970 }
1971 return 1;
1972}
1973
1974/*
1975 * walk down reloc tree to find relocated block of lowest level
1976 */
1977static noinline_for_stack
1978int walk_down_reloc_tree(struct btrfs_root *root, struct btrfs_path *path,
1979 int *level)
1980{
1981 struct extent_buffer *eb = NULL;
1982 int i;
1983 u64 bytenr;
1984 u64 ptr_gen = 0;
1985 u64 last_snapshot;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001986 u32 nritems;
1987
1988 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
1989
1990 for (i = *level; i > 0; i--) {
1991 eb = path->nodes[i];
1992 nritems = btrfs_header_nritems(eb);
1993 while (path->slots[i] < nritems) {
1994 ptr_gen = btrfs_node_ptr_generation(eb, path->slots[i]);
1995 if (ptr_gen > last_snapshot)
1996 break;
1997 path->slots[i]++;
1998 }
1999 if (path->slots[i] >= nritems) {
2000 if (i == *level)
2001 break;
2002 *level = i + 1;
2003 return 0;
2004 }
2005 if (i == 1) {
2006 *level = i;
2007 return 0;
2008 }
2009
2010 bytenr = btrfs_node_blockptr(eb, path->slots[i]);
David Sterbace86cd52014-06-15 01:07:32 +02002011 eb = read_tree_block(root, bytenr, ptr_gen);
Liu Bo64c043d2015-05-25 17:30:15 +08002012 if (IS_ERR(eb)) {
2013 return PTR_ERR(eb);
2014 } else if (!extent_buffer_uptodate(eb)) {
Josef Bacik416bc652013-04-23 14:17:42 -04002015 free_extent_buffer(eb);
2016 return -EIO;
2017 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002018 BUG_ON(btrfs_header_level(eb) != i - 1);
2019 path->nodes[i - 1] = eb;
2020 path->slots[i - 1] = 0;
2021 }
2022 return 1;
2023}
2024
2025/*
2026 * invalidate extent cache for file extents whose key in range of
2027 * [min_key, max_key)
2028 */
2029static int invalidate_extent_cache(struct btrfs_root *root,
2030 struct btrfs_key *min_key,
2031 struct btrfs_key *max_key)
2032{
2033 struct inode *inode = NULL;
2034 u64 objectid;
2035 u64 start, end;
Li Zefan33345d012011-04-20 10:31:50 +08002036 u64 ino;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002037
2038 objectid = min_key->objectid;
2039 while (1) {
2040 cond_resched();
2041 iput(inode);
2042
2043 if (objectid > max_key->objectid)
2044 break;
2045
2046 inode = find_next_inode(root, objectid);
2047 if (!inode)
2048 break;
Li Zefan33345d012011-04-20 10:31:50 +08002049 ino = btrfs_ino(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002050
Li Zefan33345d012011-04-20 10:31:50 +08002051 if (ino > max_key->objectid) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002052 iput(inode);
2053 break;
2054 }
2055
Li Zefan33345d012011-04-20 10:31:50 +08002056 objectid = ino + 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002057 if (!S_ISREG(inode->i_mode))
2058 continue;
2059
Li Zefan33345d012011-04-20 10:31:50 +08002060 if (unlikely(min_key->objectid == ino)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002061 if (min_key->type > BTRFS_EXTENT_DATA_KEY)
2062 continue;
2063 if (min_key->type < BTRFS_EXTENT_DATA_KEY)
2064 start = 0;
2065 else {
2066 start = min_key->offset;
2067 WARN_ON(!IS_ALIGNED(start, root->sectorsize));
2068 }
2069 } else {
2070 start = 0;
2071 }
2072
Li Zefan33345d012011-04-20 10:31:50 +08002073 if (unlikely(max_key->objectid == ino)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002074 if (max_key->type < BTRFS_EXTENT_DATA_KEY)
2075 continue;
2076 if (max_key->type > BTRFS_EXTENT_DATA_KEY) {
2077 end = (u64)-1;
2078 } else {
2079 if (max_key->offset == 0)
2080 continue;
2081 end = max_key->offset;
2082 WARN_ON(!IS_ALIGNED(end, root->sectorsize));
2083 end--;
2084 }
2085 } else {
2086 end = (u64)-1;
2087 }
2088
2089 /* the lock_extent waits for readpage to complete */
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002090 lock_extent(&BTRFS_I(inode)->io_tree, start, end);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002091 btrfs_drop_extent_cache(inode, start, end, 1);
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002092 unlock_extent(&BTRFS_I(inode)->io_tree, start, end);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002093 }
2094 return 0;
2095}
2096
2097static int find_next_key(struct btrfs_path *path, int level,
2098 struct btrfs_key *key)
2099
2100{
2101 while (level < BTRFS_MAX_LEVEL) {
2102 if (!path->nodes[level])
2103 break;
2104 if (path->slots[level] + 1 <
2105 btrfs_header_nritems(path->nodes[level])) {
2106 btrfs_node_key_to_cpu(path->nodes[level], key,
2107 path->slots[level] + 1);
2108 return 0;
2109 }
2110 level++;
2111 }
2112 return 1;
2113}
2114
2115/*
2116 * merge the relocated tree blocks in reloc tree with corresponding
2117 * fs tree.
2118 */
2119static noinline_for_stack int merge_reloc_root(struct reloc_control *rc,
2120 struct btrfs_root *root)
2121{
2122 LIST_HEAD(inode_list);
2123 struct btrfs_key key;
2124 struct btrfs_key next_key;
Josef Bacik9e6a0c52013-10-31 10:07:19 -04002125 struct btrfs_trans_handle *trans = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002126 struct btrfs_root *reloc_root;
2127 struct btrfs_root_item *root_item;
2128 struct btrfs_path *path;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002129 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002130 int level;
2131 int max_level;
2132 int replaced = 0;
2133 int ret;
2134 int err = 0;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002135 u32 min_reserved;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002136
2137 path = btrfs_alloc_path();
2138 if (!path)
2139 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +01002140 path->reada = READA_FORWARD;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002141
2142 reloc_root = root->reloc_root;
2143 root_item = &reloc_root->root_item;
2144
2145 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
2146 level = btrfs_root_level(root_item);
2147 extent_buffer_get(reloc_root->node);
2148 path->nodes[level] = reloc_root->node;
2149 path->slots[level] = 0;
2150 } else {
2151 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
2152
2153 level = root_item->drop_level;
2154 BUG_ON(level == 0);
2155 path->lowest_level = level;
2156 ret = btrfs_search_slot(NULL, reloc_root, &key, path, 0, 0);
Yan Zheng33c66f42009-07-22 09:59:00 -04002157 path->lowest_level = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002158 if (ret < 0) {
2159 btrfs_free_path(path);
2160 return ret;
2161 }
2162
2163 btrfs_node_key_to_cpu(path->nodes[level], &next_key,
2164 path->slots[level]);
2165 WARN_ON(memcmp(&key, &next_key, sizeof(key)));
2166
2167 btrfs_unlock_up_safe(path, 0);
2168 }
2169
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002170 min_reserved = root->nodesize * (BTRFS_MAX_LEVEL - 1) * 2;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002171 memset(&next_key, 0, sizeof(next_key));
2172
2173 while (1) {
Miao Xie08e007d2012-10-16 11:33:38 +00002174 ret = btrfs_block_rsv_refill(root, rc->block_rsv, min_reserved,
2175 BTRFS_RESERVE_FLUSH_ALL);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002176 if (ret) {
Josef Bacik9e6a0c52013-10-31 10:07:19 -04002177 err = ret;
2178 goto out;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002179 }
Josef Bacik9e6a0c52013-10-31 10:07:19 -04002180 trans = btrfs_start_transaction(root, 0);
2181 if (IS_ERR(trans)) {
2182 err = PTR_ERR(trans);
2183 trans = NULL;
2184 goto out;
2185 }
2186 trans->block_rsv = rc->block_rsv;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002187
2188 replaced = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002189 max_level = level;
2190
2191 ret = walk_down_reloc_tree(reloc_root, path, &level);
2192 if (ret < 0) {
2193 err = ret;
2194 goto out;
2195 }
2196 if (ret > 0)
2197 break;
2198
2199 if (!find_next_key(path, level, &key) &&
2200 btrfs_comp_cpu_keys(&next_key, &key) >= 0) {
2201 ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002202 } else {
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002203 ret = replace_path(trans, root, reloc_root, path,
2204 &next_key, level, max_level);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002205 }
2206 if (ret < 0) {
2207 err = ret;
2208 goto out;
2209 }
2210
2211 if (ret > 0) {
2212 level = ret;
2213 btrfs_node_key_to_cpu(path->nodes[level], &key,
2214 path->slots[level]);
2215 replaced = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002216 }
2217
2218 ret = walk_up_reloc_tree(reloc_root, path, &level);
2219 if (ret > 0)
2220 break;
2221
2222 BUG_ON(level == 0);
2223 /*
2224 * save the merging progress in the drop_progress.
2225 * this is OK since root refs == 1 in this case.
2226 */
2227 btrfs_node_key(path->nodes[level], &root_item->drop_progress,
2228 path->slots[level]);
2229 root_item->drop_level = level;
2230
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002231 btrfs_end_transaction_throttle(trans, root);
Josef Bacik9e6a0c52013-10-31 10:07:19 -04002232 trans = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002233
Liu Bob53d3f52012-11-14 14:34:34 +00002234 btrfs_btree_balance_dirty(root);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002235
2236 if (replaced && rc->stage == UPDATE_DATA_PTRS)
2237 invalidate_extent_cache(root, &key, &next_key);
2238 }
2239
2240 /*
2241 * handle the case only one block in the fs tree need to be
2242 * relocated and the block is tree root.
2243 */
2244 leaf = btrfs_lock_root_node(root);
2245 ret = btrfs_cow_block(trans, root, leaf, NULL, 0, &leaf);
2246 btrfs_tree_unlock(leaf);
2247 free_extent_buffer(leaf);
2248 if (ret < 0)
2249 err = ret;
2250out:
2251 btrfs_free_path(path);
2252
2253 if (err == 0) {
2254 memset(&root_item->drop_progress, 0,
2255 sizeof(root_item->drop_progress));
2256 root_item->drop_level = 0;
2257 btrfs_set_root_refs(root_item, 0);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002258 btrfs_update_reloc_root(trans, root);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002259 }
2260
Josef Bacik9e6a0c52013-10-31 10:07:19 -04002261 if (trans)
2262 btrfs_end_transaction_throttle(trans, root);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002263
Liu Bob53d3f52012-11-14 14:34:34 +00002264 btrfs_btree_balance_dirty(root);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002265
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002266 if (replaced && rc->stage == UPDATE_DATA_PTRS)
2267 invalidate_extent_cache(root, &key, &next_key);
2268
2269 return err;
2270}
2271
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002272static noinline_for_stack
2273int prepare_to_merge(struct reloc_control *rc, int err)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002274{
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002275 struct btrfs_root *root = rc->extent_root;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002276 struct btrfs_root *reloc_root;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002277 struct btrfs_trans_handle *trans;
2278 LIST_HEAD(reloc_roots);
2279 u64 num_bytes = 0;
2280 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002281
Chris Mason75857172011-06-13 20:00:16 -04002282 mutex_lock(&root->fs_info->reloc_mutex);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002283 rc->merging_rsv_size += root->nodesize * (BTRFS_MAX_LEVEL - 1) * 2;
2284 rc->merging_rsv_size += rc->nodes_relocated * 2;
Chris Mason75857172011-06-13 20:00:16 -04002285 mutex_unlock(&root->fs_info->reloc_mutex);
2286
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002287again:
2288 if (!err) {
2289 num_bytes = rc->merging_rsv_size;
Miao Xie08e007d2012-10-16 11:33:38 +00002290 ret = btrfs_block_rsv_add(root, rc->block_rsv, num_bytes,
2291 BTRFS_RESERVE_FLUSH_ALL);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002292 if (ret)
2293 err = ret;
2294 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002295
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002296 trans = btrfs_join_transaction(rc->extent_root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00002297 if (IS_ERR(trans)) {
2298 if (!err)
2299 btrfs_block_rsv_release(rc->extent_root,
2300 rc->block_rsv, num_bytes);
2301 return PTR_ERR(trans);
2302 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002303
2304 if (!err) {
2305 if (num_bytes != rc->merging_rsv_size) {
2306 btrfs_end_transaction(trans, rc->extent_root);
2307 btrfs_block_rsv_release(rc->extent_root,
2308 rc->block_rsv, num_bytes);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002309 goto again;
2310 }
2311 }
2312
2313 rc->merge_reloc_tree = 1;
2314
2315 while (!list_empty(&rc->reloc_roots)) {
2316 reloc_root = list_entry(rc->reloc_roots.next,
2317 struct btrfs_root, root_list);
2318 list_del_init(&reloc_root->root_list);
2319
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002320 root = read_fs_root(reloc_root->fs_info,
2321 reloc_root->root_key.offset);
2322 BUG_ON(IS_ERR(root));
2323 BUG_ON(root->reloc_root != reloc_root);
2324
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002325 /*
2326 * set reference count to 1, so btrfs_recover_relocation
2327 * knows it should resumes merging
2328 */
2329 if (!err)
2330 btrfs_set_root_refs(&reloc_root->root_item, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002331 btrfs_update_reloc_root(trans, root);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002332
2333 list_add(&reloc_root->root_list, &reloc_roots);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002334 }
2335
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002336 list_splice(&reloc_roots, &rc->reloc_roots);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002337
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002338 if (!err)
2339 btrfs_commit_transaction(trans, rc->extent_root);
2340 else
2341 btrfs_end_transaction(trans, rc->extent_root);
2342 return err;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002343}
2344
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002345static noinline_for_stack
Liu Boaca1bba2013-03-04 16:25:37 +00002346void free_reloc_roots(struct list_head *list)
2347{
2348 struct btrfs_root *reloc_root;
2349
2350 while (!list_empty(list)) {
2351 reloc_root = list_entry(list->next, struct btrfs_root,
2352 root_list);
Wang Shilongc974c462013-12-11 19:29:51 +08002353 __del_reloc_root(reloc_root);
Liu Boaca1bba2013-03-04 16:25:37 +00002354 }
2355}
2356
2357static noinline_for_stack
David Sterba94404e82014-07-30 01:53:30 +02002358void merge_reloc_roots(struct reloc_control *rc)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002359{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002360 struct btrfs_root *root;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002361 struct btrfs_root *reloc_root;
Miao Xie5bc72472013-06-06 03:28:03 +00002362 u64 last_snap;
2363 u64 otransid;
2364 u64 objectid;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002365 LIST_HEAD(reloc_roots);
2366 int found = 0;
Liu Boaca1bba2013-03-04 16:25:37 +00002367 int ret = 0;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002368again:
2369 root = rc->extent_root;
Chris Mason75857172011-06-13 20:00:16 -04002370
2371 /*
2372 * this serializes us with btrfs_record_root_in_transaction,
2373 * we have to make sure nobody is in the middle of
2374 * adding their roots to the list while we are
2375 * doing this splice
2376 */
2377 mutex_lock(&root->fs_info->reloc_mutex);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002378 list_splice_init(&rc->reloc_roots, &reloc_roots);
Chris Mason75857172011-06-13 20:00:16 -04002379 mutex_unlock(&root->fs_info->reloc_mutex);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002380
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002381 while (!list_empty(&reloc_roots)) {
2382 found = 1;
2383 reloc_root = list_entry(reloc_roots.next,
2384 struct btrfs_root, root_list);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002385
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002386 if (btrfs_root_refs(&reloc_root->root_item) > 0) {
2387 root = read_fs_root(reloc_root->fs_info,
2388 reloc_root->root_key.offset);
2389 BUG_ON(IS_ERR(root));
2390 BUG_ON(root->reloc_root != reloc_root);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002391
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002392 ret = merge_reloc_root(rc, root);
Josef Bacikb37b39c2013-07-23 16:57:15 -04002393 if (ret) {
Wang Shilong25e293c2013-12-26 13:10:50 +08002394 if (list_empty(&reloc_root->root_list))
2395 list_add_tail(&reloc_root->root_list,
2396 &reloc_roots);
Liu Boaca1bba2013-03-04 16:25:37 +00002397 goto out;
Josef Bacikb37b39c2013-07-23 16:57:15 -04002398 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002399 } else {
2400 list_del_init(&reloc_root->root_list);
2401 }
Miao Xie5bc72472013-06-06 03:28:03 +00002402
2403 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04002404 * we keep the old last snapshot transid in rtranid when we
Miao Xie5bc72472013-06-06 03:28:03 +00002405 * created the relocation tree.
2406 */
2407 last_snap = btrfs_root_rtransid(&reloc_root->root_item);
2408 otransid = btrfs_root_otransid(&reloc_root->root_item);
2409 objectid = reloc_root->root_key.offset;
2410
Jeff Mahoney2c536792011-10-03 23:22:41 -04002411 ret = btrfs_drop_snapshot(reloc_root, rc->block_rsv, 0, 1);
Liu Boaca1bba2013-03-04 16:25:37 +00002412 if (ret < 0) {
2413 if (list_empty(&reloc_root->root_list))
2414 list_add_tail(&reloc_root->root_list,
2415 &reloc_roots);
2416 goto out;
2417 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002418 }
2419
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002420 if (found) {
2421 found = 0;
2422 goto again;
2423 }
Liu Boaca1bba2013-03-04 16:25:37 +00002424out:
2425 if (ret) {
Anand Jain34d97002016-03-16 16:43:06 +08002426 btrfs_handle_fs_error(root->fs_info, ret, NULL);
Liu Boaca1bba2013-03-04 16:25:37 +00002427 if (!list_empty(&reloc_roots))
2428 free_reloc_roots(&reloc_roots);
Wang Shilong467bb1d2013-12-11 19:29:52 +08002429
2430 /* new reloc root may be added */
2431 mutex_lock(&root->fs_info->reloc_mutex);
2432 list_splice_init(&rc->reloc_roots, &reloc_roots);
2433 mutex_unlock(&root->fs_info->reloc_mutex);
2434 if (!list_empty(&reloc_roots))
2435 free_reloc_roots(&reloc_roots);
Liu Boaca1bba2013-03-04 16:25:37 +00002436 }
2437
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002438 BUG_ON(!RB_EMPTY_ROOT(&rc->reloc_root_tree.rb_root));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002439}
2440
2441static void free_block_list(struct rb_root *blocks)
2442{
2443 struct tree_block *block;
2444 struct rb_node *rb_node;
2445 while ((rb_node = rb_first(blocks))) {
2446 block = rb_entry(rb_node, struct tree_block, rb_node);
2447 rb_erase(rb_node, blocks);
2448 kfree(block);
2449 }
2450}
2451
2452static int record_reloc_root_in_trans(struct btrfs_trans_handle *trans,
2453 struct btrfs_root *reloc_root)
2454{
2455 struct btrfs_root *root;
2456
2457 if (reloc_root->last_trans == trans->transid)
2458 return 0;
2459
2460 root = read_fs_root(reloc_root->fs_info, reloc_root->root_key.offset);
2461 BUG_ON(IS_ERR(root));
2462 BUG_ON(root->reloc_root != reloc_root);
2463
2464 return btrfs_record_root_in_trans(trans, root);
2465}
2466
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002467static noinline_for_stack
2468struct btrfs_root *select_reloc_root(struct btrfs_trans_handle *trans,
2469 struct reloc_control *rc,
2470 struct backref_node *node,
Wang Shilongdc4103f2013-12-26 13:10:49 +08002471 struct backref_edge *edges[])
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002472{
2473 struct backref_node *next;
2474 struct btrfs_root *root;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002475 int index = 0;
2476
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002477 next = node;
2478 while (1) {
2479 cond_resched();
2480 next = walk_up_backref(next, edges, &index);
2481 root = next->root;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002482 BUG_ON(!root);
Miao Xie27cdeb72014-04-02 19:51:05 +08002483 BUG_ON(!test_bit(BTRFS_ROOT_REF_COWS, &root->state));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002484
2485 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
2486 record_reloc_root_in_trans(trans, root);
2487 break;
2488 }
2489
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002490 btrfs_record_root_in_trans(trans, root);
2491 root = root->reloc_root;
2492
2493 if (next->new_bytenr != root->node->start) {
2494 BUG_ON(next->new_bytenr);
2495 BUG_ON(!list_empty(&next->list));
2496 next->new_bytenr = root->node->start;
2497 next->root = root;
2498 list_add_tail(&next->list,
2499 &rc->backref_cache.changed);
2500 __mark_block_processed(rc, next);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002501 break;
2502 }
2503
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002504 WARN_ON(1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002505 root = NULL;
2506 next = walk_down_backref(edges, &index);
2507 if (!next || next->level <= node->level)
2508 break;
2509 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002510 if (!root)
2511 return NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002512
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002513 next = node;
2514 /* setup backref node path for btrfs_reloc_cow_block */
2515 while (1) {
2516 rc->backref_cache.path[next->level] = next;
2517 if (--index < 0)
2518 break;
2519 next = edges[index]->node[UPPER];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002520 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002521 return root;
2522}
2523
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002524/*
2525 * select a tree root for relocation. return NULL if the block
2526 * is reference counted. we should use do_relocation() in this
2527 * case. return a tree root pointer if the block isn't reference
2528 * counted. return -ENOENT if the block is root of reloc tree.
2529 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002530static noinline_for_stack
Zhaolei147d2562015-08-06 20:58:11 +08002531struct btrfs_root *select_one_root(struct backref_node *node)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002532{
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002533 struct backref_node *next;
2534 struct btrfs_root *root;
2535 struct btrfs_root *fs_root = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002536 struct backref_edge *edges[BTRFS_MAX_LEVEL - 1];
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002537 int index = 0;
2538
2539 next = node;
2540 while (1) {
2541 cond_resched();
2542 next = walk_up_backref(next, edges, &index);
2543 root = next->root;
2544 BUG_ON(!root);
2545
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002546 /* no other choice for non-references counted tree */
Miao Xie27cdeb72014-04-02 19:51:05 +08002547 if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state))
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002548 return root;
2549
2550 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID)
2551 fs_root = root;
2552
2553 if (next != node)
2554 return NULL;
2555
2556 next = walk_down_backref(edges, &index);
2557 if (!next || next->level <= node->level)
2558 break;
2559 }
2560
2561 if (!fs_root)
2562 return ERR_PTR(-ENOENT);
2563 return fs_root;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002564}
2565
2566static noinline_for_stack
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002567u64 calcu_metadata_size(struct reloc_control *rc,
2568 struct backref_node *node, int reserve)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002569{
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002570 struct backref_node *next = node;
2571 struct backref_edge *edge;
2572 struct backref_edge *edges[BTRFS_MAX_LEVEL - 1];
2573 u64 num_bytes = 0;
2574 int index = 0;
2575
2576 BUG_ON(reserve && node->processed);
2577
2578 while (next) {
2579 cond_resched();
2580 while (1) {
2581 if (next->processed && (reserve || next != node))
2582 break;
2583
David Sterba707e8a02014-06-04 19:22:26 +02002584 num_bytes += rc->extent_root->nodesize;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002585
2586 if (list_empty(&next->upper))
2587 break;
2588
2589 edge = list_entry(next->upper.next,
2590 struct backref_edge, list[LOWER]);
2591 edges[index++] = edge;
2592 next = edge->node[UPPER];
2593 }
2594 next = walk_down_backref(edges, &index);
2595 }
2596 return num_bytes;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002597}
2598
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002599static int reserve_metadata_space(struct btrfs_trans_handle *trans,
2600 struct reloc_control *rc,
2601 struct backref_node *node)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002602{
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002603 struct btrfs_root *root = rc->extent_root;
2604 u64 num_bytes;
2605 int ret;
Wang Shilong0647bf52013-11-20 09:01:52 +08002606 u64 tmp;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002607
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002608 num_bytes = calcu_metadata_size(rc, node, 1) * 2;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002609
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002610 trans->block_rsv = rc->block_rsv;
Wang Shilong0647bf52013-11-20 09:01:52 +08002611 rc->reserved_bytes += num_bytes;
Josef Bacik8ca17f02016-05-27 13:24:13 -04002612
2613 /*
2614 * We are under a transaction here so we can only do limited flushing.
2615 * If we get an enospc just kick back -EAGAIN so we know to drop the
2616 * transaction and try to refill when we can flush all the things.
2617 */
Wang Shilong0647bf52013-11-20 09:01:52 +08002618 ret = btrfs_block_rsv_refill(root, rc->block_rsv, num_bytes,
Josef Bacik8ca17f02016-05-27 13:24:13 -04002619 BTRFS_RESERVE_FLUSH_LIMIT);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002620 if (ret) {
Josef Bacik8ca17f02016-05-27 13:24:13 -04002621 tmp = rc->extent_root->nodesize * RELOCATION_RESERVED_NODES;
2622 while (tmp <= rc->reserved_bytes)
2623 tmp <<= 1;
2624 /*
2625 * only one thread can access block_rsv at this point,
2626 * so we don't need hold lock to protect block_rsv.
2627 * we expand more reservation size here to allow enough
2628 * space for relocation and we will return eailer in
2629 * enospc case.
2630 */
2631 rc->block_rsv->size = tmp + rc->extent_root->nodesize *
2632 RELOCATION_RESERVED_NODES;
2633 return -EAGAIN;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002634 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002635
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002636 return 0;
2637}
2638
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002639/*
2640 * relocate a block tree, and then update pointers in upper level
2641 * blocks that reference the block to point to the new location.
2642 *
2643 * if called by link_to_upper, the block has already been relocated.
2644 * in that case this function just updates pointers.
2645 */
2646static int do_relocation(struct btrfs_trans_handle *trans,
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002647 struct reloc_control *rc,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002648 struct backref_node *node,
2649 struct btrfs_key *key,
2650 struct btrfs_path *path, int lowest)
2651{
2652 struct backref_node *upper;
2653 struct backref_edge *edge;
2654 struct backref_edge *edges[BTRFS_MAX_LEVEL - 1];
2655 struct btrfs_root *root;
2656 struct extent_buffer *eb;
2657 u32 blocksize;
2658 u64 bytenr;
2659 u64 generation;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002660 int slot;
2661 int ret;
2662 int err = 0;
2663
2664 BUG_ON(lowest && node->eb);
2665
2666 path->lowest_level = node->level + 1;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002667 rc->backref_cache.path[node->level] = node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002668 list_for_each_entry(edge, &node->upper, list[LOWER]) {
2669 cond_resched();
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002670
2671 upper = edge->node[UPPER];
Wang Shilongdc4103f2013-12-26 13:10:49 +08002672 root = select_reloc_root(trans, rc, upper, edges);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002673 BUG_ON(!root);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002674
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002675 if (upper->eb && !upper->locked) {
2676 if (!lowest) {
2677 ret = btrfs_bin_search(upper->eb, key,
2678 upper->level, &slot);
2679 BUG_ON(ret);
2680 bytenr = btrfs_node_blockptr(upper->eb, slot);
2681 if (node->eb->start == bytenr)
2682 goto next;
2683 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002684 drop_node_buffer(upper);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002685 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002686
2687 if (!upper->eb) {
2688 ret = btrfs_search_slot(trans, root, key, path, 0, 1);
2689 if (ret < 0) {
2690 err = ret;
2691 break;
2692 }
2693 BUG_ON(ret > 0);
2694
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002695 if (!upper->eb) {
2696 upper->eb = path->nodes[upper->level];
2697 path->nodes[upper->level] = NULL;
2698 } else {
2699 BUG_ON(upper->eb != path->nodes[upper->level]);
2700 }
2701
2702 upper->locked = 1;
2703 path->locks[upper->level] = 0;
2704
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002705 slot = path->slots[upper->level];
David Sterbab3b4aa72011-04-21 01:20:15 +02002706 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002707 } else {
2708 ret = btrfs_bin_search(upper->eb, key, upper->level,
2709 &slot);
2710 BUG_ON(ret);
2711 }
2712
2713 bytenr = btrfs_node_blockptr(upper->eb, slot);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002714 if (lowest) {
2715 BUG_ON(bytenr != node->bytenr);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002716 } else {
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002717 if (node->eb->start == bytenr)
2718 goto next;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002719 }
2720
David Sterba707e8a02014-06-04 19:22:26 +02002721 blocksize = root->nodesize;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002722 generation = btrfs_node_ptr_generation(upper->eb, slot);
David Sterbace86cd52014-06-15 01:07:32 +02002723 eb = read_tree_block(root, bytenr, generation);
Liu Bo64c043d2015-05-25 17:30:15 +08002724 if (IS_ERR(eb)) {
2725 err = PTR_ERR(eb);
2726 goto next;
2727 } else if (!extent_buffer_uptodate(eb)) {
Josef Bacik416bc652013-04-23 14:17:42 -04002728 free_extent_buffer(eb);
Tsutomu Itoh97d9a8a2011-03-24 06:33:21 +00002729 err = -EIO;
2730 goto next;
2731 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002732 btrfs_tree_lock(eb);
2733 btrfs_set_lock_blocking(eb);
2734
2735 if (!node->eb) {
2736 ret = btrfs_cow_block(trans, root, eb, upper->eb,
2737 slot, &eb);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002738 btrfs_tree_unlock(eb);
2739 free_extent_buffer(eb);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002740 if (ret < 0) {
2741 err = ret;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002742 goto next;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002743 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002744 BUG_ON(node->eb != eb);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002745 } else {
2746 btrfs_set_node_blockptr(upper->eb, slot,
2747 node->eb->start);
2748 btrfs_set_node_ptr_generation(upper->eb, slot,
2749 trans->transid);
2750 btrfs_mark_buffer_dirty(upper->eb);
2751
2752 ret = btrfs_inc_extent_ref(trans, root,
2753 node->eb->start, blocksize,
2754 upper->eb->start,
2755 btrfs_header_owner(upper->eb),
Filipe Mananab06c4bf2015-10-23 07:52:54 +01002756 node->level, 0);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002757 BUG_ON(ret);
2758
2759 ret = btrfs_drop_subtree(trans, root, eb, upper->eb);
2760 BUG_ON(ret);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002761 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002762next:
2763 if (!upper->pending)
2764 drop_node_buffer(upper);
2765 else
2766 unlock_node_buffer(upper);
2767 if (err)
2768 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002769 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002770
2771 if (!err && node->pending) {
2772 drop_node_buffer(node);
2773 list_move_tail(&node->list, &rc->backref_cache.changed);
2774 node->pending = 0;
2775 }
2776
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002777 path->lowest_level = 0;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002778 BUG_ON(err == -ENOSPC);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002779 return err;
2780}
2781
2782static int link_to_upper(struct btrfs_trans_handle *trans,
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002783 struct reloc_control *rc,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002784 struct backref_node *node,
2785 struct btrfs_path *path)
2786{
2787 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002788
2789 btrfs_node_key_to_cpu(node->eb, &key, 0);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002790 return do_relocation(trans, rc, node, &key, path, 0);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002791}
2792
2793static int finish_pending_nodes(struct btrfs_trans_handle *trans,
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002794 struct reloc_control *rc,
2795 struct btrfs_path *path, int err)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002796{
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002797 LIST_HEAD(list);
2798 struct backref_cache *cache = &rc->backref_cache;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002799 struct backref_node *node;
2800 int level;
2801 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002802
2803 for (level = 0; level < BTRFS_MAX_LEVEL; level++) {
2804 while (!list_empty(&cache->pending[level])) {
2805 node = list_entry(cache->pending[level].next,
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002806 struct backref_node, list);
2807 list_move_tail(&node->list, &list);
2808 BUG_ON(!node->pending);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002809
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002810 if (!err) {
2811 ret = link_to_upper(trans, rc, node, path);
2812 if (ret < 0)
2813 err = ret;
2814 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002815 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002816 list_splice_init(&list, &cache->pending[level]);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002817 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002818 return err;
2819}
2820
2821static void mark_block_processed(struct reloc_control *rc,
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002822 u64 bytenr, u32 blocksize)
2823{
2824 set_extent_bits(&rc->processed_blocks, bytenr, bytenr + blocksize - 1,
David Sterbaceeb0ae2016-04-26 23:54:39 +02002825 EXTENT_DIRTY);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002826}
2827
2828static void __mark_block_processed(struct reloc_control *rc,
2829 struct backref_node *node)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002830{
2831 u32 blocksize;
2832 if (node->level == 0 ||
2833 in_block_group(node->bytenr, rc->block_group)) {
David Sterba707e8a02014-06-04 19:22:26 +02002834 blocksize = rc->extent_root->nodesize;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002835 mark_block_processed(rc, node->bytenr, blocksize);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002836 }
2837 node->processed = 1;
2838}
2839
2840/*
2841 * mark a block and all blocks directly/indirectly reference the block
2842 * as processed.
2843 */
2844static void update_processed_blocks(struct reloc_control *rc,
2845 struct backref_node *node)
2846{
2847 struct backref_node *next = node;
2848 struct backref_edge *edge;
2849 struct backref_edge *edges[BTRFS_MAX_LEVEL - 1];
2850 int index = 0;
2851
2852 while (next) {
2853 cond_resched();
2854 while (1) {
2855 if (next->processed)
2856 break;
2857
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002858 __mark_block_processed(rc, next);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002859
2860 if (list_empty(&next->upper))
2861 break;
2862
2863 edge = list_entry(next->upper.next,
2864 struct backref_edge, list[LOWER]);
2865 edges[index++] = edge;
2866 next = edge->node[UPPER];
2867 }
2868 next = walk_down_backref(edges, &index);
2869 }
2870}
2871
David Sterba7476dfd2014-06-15 03:34:59 +02002872static int tree_block_processed(u64 bytenr, struct reloc_control *rc)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002873{
David Sterba7476dfd2014-06-15 03:34:59 +02002874 u32 blocksize = rc->extent_root->nodesize;
2875
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002876 if (test_range_bit(&rc->processed_blocks, bytenr,
Chris Mason9655d292009-09-02 15:22:30 -04002877 bytenr + blocksize - 1, EXTENT_DIRTY, 1, NULL))
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002878 return 1;
2879 return 0;
2880}
2881
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002882static int get_tree_block_key(struct reloc_control *rc,
2883 struct tree_block *block)
2884{
2885 struct extent_buffer *eb;
2886
2887 BUG_ON(block->key_ready);
2888 eb = read_tree_block(rc->extent_root, block->bytenr,
David Sterbace86cd52014-06-15 01:07:32 +02002889 block->key.offset);
Liu Bo64c043d2015-05-25 17:30:15 +08002890 if (IS_ERR(eb)) {
2891 return PTR_ERR(eb);
2892 } else if (!extent_buffer_uptodate(eb)) {
Josef Bacik416bc652013-04-23 14:17:42 -04002893 free_extent_buffer(eb);
2894 return -EIO;
2895 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002896 WARN_ON(btrfs_header_level(eb) != block->level);
2897 if (block->level == 0)
2898 btrfs_item_key_to_cpu(eb, &block->key, 0);
2899 else
2900 btrfs_node_key_to_cpu(eb, &block->key, 0);
2901 free_extent_buffer(eb);
2902 block->key_ready = 1;
2903 return 0;
2904}
2905
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002906/*
2907 * helper function to relocate a tree block
2908 */
2909static int relocate_tree_block(struct btrfs_trans_handle *trans,
2910 struct reloc_control *rc,
2911 struct backref_node *node,
2912 struct btrfs_key *key,
2913 struct btrfs_path *path)
2914{
2915 struct btrfs_root *root;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002916 int ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002917
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002918 if (!node)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002919 return 0;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002920
2921 BUG_ON(node->processed);
Zhaolei147d2562015-08-06 20:58:11 +08002922 root = select_one_root(node);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002923 if (root == ERR_PTR(-ENOENT)) {
2924 update_processed_blocks(rc, node);
2925 goto out;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002926 }
2927
Miao Xie27cdeb72014-04-02 19:51:05 +08002928 if (!root || test_bit(BTRFS_ROOT_REF_COWS, &root->state)) {
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002929 ret = reserve_metadata_space(trans, rc, node);
2930 if (ret)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002931 goto out;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002932 }
2933
2934 if (root) {
Miao Xie27cdeb72014-04-02 19:51:05 +08002935 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state)) {
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002936 BUG_ON(node->new_bytenr);
2937 BUG_ON(!list_empty(&node->list));
2938 btrfs_record_root_in_trans(trans, root);
2939 root = root->reloc_root;
2940 node->new_bytenr = root->node->start;
2941 node->root = root;
2942 list_add_tail(&node->list, &rc->backref_cache.changed);
2943 } else {
2944 path->lowest_level = node->level;
2945 ret = btrfs_search_slot(trans, root, key, path, 0, 1);
David Sterbab3b4aa72011-04-21 01:20:15 +02002946 btrfs_release_path(path);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002947 if (ret > 0)
2948 ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002949 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002950 if (!ret)
2951 update_processed_blocks(rc, node);
2952 } else {
2953 ret = do_relocation(trans, rc, node, key, path, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002954 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002955out:
Wang Shilong0647bf52013-11-20 09:01:52 +08002956 if (ret || node->level == 0 || node->cowonly)
Yan, Zheng3fd0a552010-05-16 10:49:59 -04002957 remove_backref_node(&rc->backref_cache, node);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002958 return ret;
2959}
2960
2961/*
2962 * relocate a list of blocks
2963 */
2964static noinline_for_stack
2965int relocate_tree_blocks(struct btrfs_trans_handle *trans,
2966 struct reloc_control *rc, struct rb_root *blocks)
2967{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002968 struct backref_node *node;
2969 struct btrfs_path *path;
2970 struct tree_block *block;
2971 struct rb_node *rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002972 int ret;
2973 int err = 0;
2974
2975 path = btrfs_alloc_path();
Liu Boe1a12672013-03-04 16:25:38 +00002976 if (!path) {
2977 err = -ENOMEM;
David Sterba34c2b292013-04-26 12:56:04 +00002978 goto out_free_blocks;
Liu Boe1a12672013-03-04 16:25:38 +00002979 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002980
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002981 rb_node = rb_first(blocks);
2982 while (rb_node) {
2983 block = rb_entry(rb_node, struct tree_block, rb_node);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002984 if (!block->key_ready)
David Sterbad3e46fe2014-06-15 02:04:19 +02002985 readahead_tree_block(rc->extent_root, block->bytenr);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002986 rb_node = rb_next(rb_node);
2987 }
2988
2989 rb_node = rb_first(blocks);
2990 while (rb_node) {
2991 block = rb_entry(rb_node, struct tree_block, rb_node);
David Sterba34c2b292013-04-26 12:56:04 +00002992 if (!block->key_ready) {
2993 err = get_tree_block_key(rc, block);
2994 if (err)
2995 goto out_free_path;
2996 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002997 rb_node = rb_next(rb_node);
2998 }
2999
3000 rb_node = rb_first(blocks);
3001 while (rb_node) {
3002 block = rb_entry(rb_node, struct tree_block, rb_node);
3003
Yan, Zheng3fd0a552010-05-16 10:49:59 -04003004 node = build_backref_tree(rc, &block->key,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003005 block->level, block->bytenr);
3006 if (IS_ERR(node)) {
3007 err = PTR_ERR(node);
3008 goto out;
3009 }
3010
3011 ret = relocate_tree_block(trans, rc, node, &block->key,
3012 path);
3013 if (ret < 0) {
Yan, Zheng3fd0a552010-05-16 10:49:59 -04003014 if (ret != -EAGAIN || rb_node == rb_first(blocks))
3015 err = ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003016 goto out;
3017 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003018 rb_node = rb_next(rb_node);
3019 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003020out:
Yan, Zheng3fd0a552010-05-16 10:49:59 -04003021 err = finish_pending_nodes(trans, rc, path, err);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003022
David Sterba34c2b292013-04-26 12:56:04 +00003023out_free_path:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003024 btrfs_free_path(path);
David Sterba34c2b292013-04-26 12:56:04 +00003025out_free_blocks:
Liu Boe1a12672013-03-04 16:25:38 +00003026 free_block_list(blocks);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003027 return err;
3028}
3029
3030static noinline_for_stack
Yan, Zhengefa56462010-05-16 10:49:59 -04003031int prealloc_file_extent_cluster(struct inode *inode,
3032 struct file_extent_cluster *cluster)
3033{
3034 u64 alloc_hint = 0;
3035 u64 start;
3036 u64 end;
3037 u64 offset = BTRFS_I(inode)->index_cnt;
3038 u64 num_bytes;
3039 int nr = 0;
3040 int ret = 0;
3041
3042 BUG_ON(cluster->start != cluster->boundary[0]);
Al Viro59551022016-01-22 15:40:57 -05003043 inode_lock(inode);
Yan, Zhengefa56462010-05-16 10:49:59 -04003044
Qu Wenruo7cf5b972015-09-08 17:25:55 +08003045 ret = btrfs_check_data_free_space(inode, cluster->start,
3046 cluster->end + 1 - cluster->start);
Yan, Zhengefa56462010-05-16 10:49:59 -04003047 if (ret)
3048 goto out;
3049
3050 while (nr < cluster->nr) {
3051 start = cluster->boundary[nr] - offset;
3052 if (nr + 1 < cluster->nr)
3053 end = cluster->boundary[nr + 1] - 1 - offset;
3054 else
3055 end = cluster->end - offset;
3056
Jeff Mahoneyd0082372012-03-01 14:57:19 +01003057 lock_extent(&BTRFS_I(inode)->io_tree, start, end);
Yan, Zhengefa56462010-05-16 10:49:59 -04003058 num_bytes = end + 1 - start;
3059 ret = btrfs_prealloc_file_range(inode, 0, start,
3060 num_bytes, num_bytes,
3061 end + 1, &alloc_hint);
Jeff Mahoneyd0082372012-03-01 14:57:19 +01003062 unlock_extent(&BTRFS_I(inode)->io_tree, start, end);
Yan, Zhengefa56462010-05-16 10:49:59 -04003063 if (ret)
3064 break;
3065 nr++;
3066 }
Qu Wenruo7cf5b972015-09-08 17:25:55 +08003067 btrfs_free_reserved_data_space(inode, cluster->start,
3068 cluster->end + 1 - cluster->start);
Yan, Zhengefa56462010-05-16 10:49:59 -04003069out:
Al Viro59551022016-01-22 15:40:57 -05003070 inode_unlock(inode);
Yan, Zhengefa56462010-05-16 10:49:59 -04003071 return ret;
3072}
3073
3074static noinline_for_stack
Yan, Zheng0257bb82009-09-24 09:17:31 -04003075int setup_extent_mapping(struct inode *inode, u64 start, u64 end,
3076 u64 block_start)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003077{
3078 struct btrfs_root *root = BTRFS_I(inode)->root;
3079 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
3080 struct extent_map *em;
Yan, Zheng0257bb82009-09-24 09:17:31 -04003081 int ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003082
David Sterba172ddd62011-04-21 00:48:27 +02003083 em = alloc_extent_map();
Yan, Zheng0257bb82009-09-24 09:17:31 -04003084 if (!em)
3085 return -ENOMEM;
3086
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003087 em->start = start;
Yan, Zheng0257bb82009-09-24 09:17:31 -04003088 em->len = end + 1 - start;
3089 em->block_len = em->len;
3090 em->block_start = block_start;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003091 em->bdev = root->fs_info->fs_devices->latest_bdev;
3092 set_bit(EXTENT_FLAG_PINNED, &em->flags);
3093
Jeff Mahoneyd0082372012-03-01 14:57:19 +01003094 lock_extent(&BTRFS_I(inode)->io_tree, start, end);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003095 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04003096 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04003097 ret = add_extent_mapping(em_tree, em, 0);
Chris Mason890871b2009-09-02 16:24:52 -04003098 write_unlock(&em_tree->lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003099 if (ret != -EEXIST) {
3100 free_extent_map(em);
3101 break;
3102 }
3103 btrfs_drop_extent_cache(inode, start, end, 0);
3104 }
Jeff Mahoneyd0082372012-03-01 14:57:19 +01003105 unlock_extent(&BTRFS_I(inode)->io_tree, start, end);
Yan, Zheng0257bb82009-09-24 09:17:31 -04003106 return ret;
3107}
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003108
Yan, Zheng0257bb82009-09-24 09:17:31 -04003109static int relocate_file_extent_cluster(struct inode *inode,
3110 struct file_extent_cluster *cluster)
3111{
3112 u64 page_start;
3113 u64 page_end;
3114 u64 offset = BTRFS_I(inode)->index_cnt;
3115 unsigned long index;
3116 unsigned long last_index;
Yan, Zheng0257bb82009-09-24 09:17:31 -04003117 struct page *page;
3118 struct file_ra_state *ra;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04003119 gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
Yan, Zheng0257bb82009-09-24 09:17:31 -04003120 int nr = 0;
3121 int ret = 0;
3122
3123 if (!cluster->nr)
3124 return 0;
3125
3126 ra = kzalloc(sizeof(*ra), GFP_NOFS);
3127 if (!ra)
3128 return -ENOMEM;
3129
Yan, Zhengefa56462010-05-16 10:49:59 -04003130 ret = prealloc_file_extent_cluster(inode, cluster);
Yan, Zheng0257bb82009-09-24 09:17:31 -04003131 if (ret)
Yan, Zhengefa56462010-05-16 10:49:59 -04003132 goto out;
Yan, Zheng0257bb82009-09-24 09:17:31 -04003133
3134 file_ra_state_init(ra, inode->i_mapping);
3135
Yan, Zhengefa56462010-05-16 10:49:59 -04003136 ret = setup_extent_mapping(inode, cluster->start - offset,
3137 cluster->end - offset, cluster->start);
3138 if (ret)
3139 goto out;
3140
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003141 index = (cluster->start - offset) >> PAGE_SHIFT;
3142 last_index = (cluster->end - offset) >> PAGE_SHIFT;
Yan, Zheng0257bb82009-09-24 09:17:31 -04003143 while (index <= last_index) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003144 ret = btrfs_delalloc_reserve_metadata(inode, PAGE_SIZE);
Yan, Zhengefa56462010-05-16 10:49:59 -04003145 if (ret)
3146 goto out;
3147
Yan, Zheng0257bb82009-09-24 09:17:31 -04003148 page = find_lock_page(inode->i_mapping, index);
3149 if (!page) {
3150 page_cache_sync_readahead(inode->i_mapping,
3151 ra, NULL, index,
3152 last_index + 1 - index);
Josef Bacika94733d2011-07-11 10:47:06 -04003153 page = find_or_create_page(inode->i_mapping, index,
Josef Bacik3b16a4e2011-09-21 15:05:58 -04003154 mask);
Yan, Zheng0257bb82009-09-24 09:17:31 -04003155 if (!page) {
Yan, Zhengefa56462010-05-16 10:49:59 -04003156 btrfs_delalloc_release_metadata(inode,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003157 PAGE_SIZE);
Yan, Zheng0257bb82009-09-24 09:17:31 -04003158 ret = -ENOMEM;
Yan, Zhengefa56462010-05-16 10:49:59 -04003159 goto out;
Yan, Zheng0257bb82009-09-24 09:17:31 -04003160 }
3161 }
3162
3163 if (PageReadahead(page)) {
3164 page_cache_async_readahead(inode->i_mapping,
3165 ra, NULL, page, index,
3166 last_index + 1 - index);
3167 }
3168
3169 if (!PageUptodate(page)) {
3170 btrfs_readpage(NULL, page);
3171 lock_page(page);
3172 if (!PageUptodate(page)) {
3173 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003174 put_page(page);
Yan, Zhengefa56462010-05-16 10:49:59 -04003175 btrfs_delalloc_release_metadata(inode,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003176 PAGE_SIZE);
Yan, Zheng0257bb82009-09-24 09:17:31 -04003177 ret = -EIO;
Yan, Zhengefa56462010-05-16 10:49:59 -04003178 goto out;
Yan, Zheng0257bb82009-09-24 09:17:31 -04003179 }
3180 }
3181
Miao Xie4eee4fa2012-12-21 09:17:45 +00003182 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003183 page_end = page_start + PAGE_SIZE - 1;
Yan, Zheng0257bb82009-09-24 09:17:31 -04003184
Jeff Mahoneyd0082372012-03-01 14:57:19 +01003185 lock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end);
Yan, Zheng0257bb82009-09-24 09:17:31 -04003186
3187 set_page_extent_mapped(page);
3188
3189 if (nr < cluster->nr &&
3190 page_start + offset == cluster->boundary[nr]) {
3191 set_extent_bits(&BTRFS_I(inode)->io_tree,
3192 page_start, page_end,
David Sterbaceeb0ae2016-04-26 23:54:39 +02003193 EXTENT_BOUNDARY);
Yan, Zheng0257bb82009-09-24 09:17:31 -04003194 nr++;
3195 }
Yan, Zheng0257bb82009-09-24 09:17:31 -04003196
Yan, Zhengefa56462010-05-16 10:49:59 -04003197 btrfs_set_extent_delalloc(inode, page_start, page_end, NULL);
Yan, Zheng0257bb82009-09-24 09:17:31 -04003198 set_page_dirty(page);
Yan, Zheng0257bb82009-09-24 09:17:31 -04003199
3200 unlock_extent(&BTRFS_I(inode)->io_tree,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01003201 page_start, page_end);
Yan, Zheng0257bb82009-09-24 09:17:31 -04003202 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003203 put_page(page);
Yan, Zheng0257bb82009-09-24 09:17:31 -04003204
3205 index++;
Yan, Zhengefa56462010-05-16 10:49:59 -04003206 balance_dirty_pages_ratelimited(inode->i_mapping);
3207 btrfs_throttle(BTRFS_I(inode)->root);
Yan, Zheng0257bb82009-09-24 09:17:31 -04003208 }
3209 WARN_ON(nr != cluster->nr);
Yan, Zhengefa56462010-05-16 10:49:59 -04003210out:
Yan, Zheng0257bb82009-09-24 09:17:31 -04003211 kfree(ra);
3212 return ret;
3213}
3214
3215static noinline_for_stack
3216int relocate_data_extent(struct inode *inode, struct btrfs_key *extent_key,
3217 struct file_extent_cluster *cluster)
3218{
3219 int ret;
3220
3221 if (cluster->nr > 0 && extent_key->objectid != cluster->end + 1) {
3222 ret = relocate_file_extent_cluster(inode, cluster);
3223 if (ret)
3224 return ret;
3225 cluster->nr = 0;
3226 }
3227
3228 if (!cluster->nr)
3229 cluster->start = extent_key->objectid;
3230 else
3231 BUG_ON(cluster->nr >= MAX_EXTENTS);
3232 cluster->end = extent_key->objectid + extent_key->offset - 1;
3233 cluster->boundary[cluster->nr] = extent_key->objectid;
3234 cluster->nr++;
3235
3236 if (cluster->nr >= MAX_EXTENTS) {
3237 ret = relocate_file_extent_cluster(inode, cluster);
3238 if (ret)
3239 return ret;
3240 cluster->nr = 0;
3241 }
3242 return 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003243}
3244
3245#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3246static int get_ref_objectid_v0(struct reloc_control *rc,
3247 struct btrfs_path *path,
3248 struct btrfs_key *extent_key,
3249 u64 *ref_objectid, int *path_change)
3250{
3251 struct btrfs_key key;
3252 struct extent_buffer *leaf;
3253 struct btrfs_extent_ref_v0 *ref0;
3254 int ret;
3255 int slot;
3256
3257 leaf = path->nodes[0];
3258 slot = path->slots[0];
3259 while (1) {
3260 if (slot >= btrfs_header_nritems(leaf)) {
3261 ret = btrfs_next_leaf(rc->extent_root, path);
3262 if (ret < 0)
3263 return ret;
3264 BUG_ON(ret > 0);
3265 leaf = path->nodes[0];
3266 slot = path->slots[0];
3267 if (path_change)
3268 *path_change = 1;
3269 }
3270 btrfs_item_key_to_cpu(leaf, &key, slot);
3271 if (key.objectid != extent_key->objectid)
3272 return -ENOENT;
3273
3274 if (key.type != BTRFS_EXTENT_REF_V0_KEY) {
3275 slot++;
3276 continue;
3277 }
3278 ref0 = btrfs_item_ptr(leaf, slot,
3279 struct btrfs_extent_ref_v0);
3280 *ref_objectid = btrfs_ref_objectid_v0(leaf, ref0);
3281 break;
3282 }
3283 return 0;
3284}
3285#endif
3286
3287/*
3288 * helper to add a tree block to the list.
3289 * the major work is getting the generation and level of the block
3290 */
3291static int add_tree_block(struct reloc_control *rc,
3292 struct btrfs_key *extent_key,
3293 struct btrfs_path *path,
3294 struct rb_root *blocks)
3295{
3296 struct extent_buffer *eb;
3297 struct btrfs_extent_item *ei;
3298 struct btrfs_tree_block_info *bi;
3299 struct tree_block *block;
3300 struct rb_node *rb_node;
3301 u32 item_size;
3302 int level = -1;
Wang Shilong7fdf4b62013-10-25 18:52:08 +08003303 u64 generation;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003304
3305 eb = path->nodes[0];
3306 item_size = btrfs_item_size_nr(eb, path->slots[0]);
3307
Josef Bacik3173a182013-03-07 14:22:04 -05003308 if (extent_key->type == BTRFS_METADATA_ITEM_KEY ||
3309 item_size >= sizeof(*ei) + sizeof(*bi)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003310 ei = btrfs_item_ptr(eb, path->slots[0],
3311 struct btrfs_extent_item);
Josef Bacik3173a182013-03-07 14:22:04 -05003312 if (extent_key->type == BTRFS_EXTENT_ITEM_KEY) {
3313 bi = (struct btrfs_tree_block_info *)(ei + 1);
3314 level = btrfs_tree_block_level(eb, bi);
3315 } else {
3316 level = (int)extent_key->offset;
3317 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003318 generation = btrfs_extent_generation(eb, ei);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003319 } else {
3320#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3321 u64 ref_owner;
3322 int ret;
3323
3324 BUG_ON(item_size != sizeof(struct btrfs_extent_item_v0));
3325 ret = get_ref_objectid_v0(rc, path, extent_key,
3326 &ref_owner, NULL);
Andi Kleen411fc6b2010-10-29 15:14:31 -04003327 if (ret < 0)
3328 return ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003329 BUG_ON(ref_owner >= BTRFS_MAX_LEVEL);
3330 level = (int)ref_owner;
3331 /* FIXME: get real generation */
3332 generation = 0;
3333#else
3334 BUG();
3335#endif
3336 }
3337
David Sterbab3b4aa72011-04-21 01:20:15 +02003338 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003339
3340 BUG_ON(level == -1);
3341
3342 block = kmalloc(sizeof(*block), GFP_NOFS);
3343 if (!block)
3344 return -ENOMEM;
3345
3346 block->bytenr = extent_key->objectid;
David Sterba707e8a02014-06-04 19:22:26 +02003347 block->key.objectid = rc->extent_root->nodesize;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003348 block->key.offset = generation;
3349 block->level = level;
3350 block->key_ready = 0;
3351
3352 rb_node = tree_insert(blocks, block->bytenr, &block->rb_node);
Jeff Mahoney43c04fb2011-10-03 23:22:33 -04003353 if (rb_node)
3354 backref_tree_panic(rb_node, -EEXIST, block->bytenr);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003355
3356 return 0;
3357}
3358
3359/*
3360 * helper to add tree blocks for backref of type BTRFS_SHARED_DATA_REF_KEY
3361 */
3362static int __add_tree_block(struct reloc_control *rc,
3363 u64 bytenr, u32 blocksize,
3364 struct rb_root *blocks)
3365{
3366 struct btrfs_path *path;
3367 struct btrfs_key key;
3368 int ret;
Josef Bacikaee68ee2013-06-13 13:50:23 -04003369 bool skinny = btrfs_fs_incompat(rc->extent_root->fs_info,
3370 SKINNY_METADATA);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003371
David Sterba7476dfd2014-06-15 03:34:59 +02003372 if (tree_block_processed(bytenr, rc))
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003373 return 0;
3374
3375 if (tree_search(blocks, bytenr))
3376 return 0;
3377
3378 path = btrfs_alloc_path();
3379 if (!path)
3380 return -ENOMEM;
Josef Bacikaee68ee2013-06-13 13:50:23 -04003381again:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003382 key.objectid = bytenr;
Josef Bacikaee68ee2013-06-13 13:50:23 -04003383 if (skinny) {
3384 key.type = BTRFS_METADATA_ITEM_KEY;
3385 key.offset = (u64)-1;
3386 } else {
3387 key.type = BTRFS_EXTENT_ITEM_KEY;
3388 key.offset = blocksize;
3389 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003390
3391 path->search_commit_root = 1;
3392 path->skip_locking = 1;
3393 ret = btrfs_search_slot(NULL, rc->extent_root, &key, path, 0, 0);
3394 if (ret < 0)
3395 goto out;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003396
Josef Bacikaee68ee2013-06-13 13:50:23 -04003397 if (ret > 0 && skinny) {
3398 if (path->slots[0]) {
3399 path->slots[0]--;
3400 btrfs_item_key_to_cpu(path->nodes[0], &key,
3401 path->slots[0]);
3402 if (key.objectid == bytenr &&
3403 (key.type == BTRFS_METADATA_ITEM_KEY ||
3404 (key.type == BTRFS_EXTENT_ITEM_KEY &&
3405 key.offset == blocksize)))
3406 ret = 0;
3407 }
3408
3409 if (ret) {
3410 skinny = false;
3411 btrfs_release_path(path);
3412 goto again;
3413 }
Josef Bacik3173a182013-03-07 14:22:04 -05003414 }
3415 BUG_ON(ret);
3416
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003417 ret = add_tree_block(rc, &key, path, blocks);
3418out:
3419 btrfs_free_path(path);
3420 return ret;
3421}
3422
3423/*
3424 * helper to check if the block use full backrefs for pointers in it
3425 */
3426static int block_use_full_backref(struct reloc_control *rc,
3427 struct extent_buffer *eb)
3428{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003429 u64 flags;
3430 int ret;
3431
3432 if (btrfs_header_flag(eb, BTRFS_HEADER_FLAG_RELOC) ||
3433 btrfs_header_backref_rev(eb) < BTRFS_MIXED_BACKREF_REV)
3434 return 1;
3435
Yan, Zheng3fd0a552010-05-16 10:49:59 -04003436 ret = btrfs_lookup_extent_info(NULL, rc->extent_root,
Josef Bacik3173a182013-03-07 14:22:04 -05003437 eb->start, btrfs_header_level(eb), 1,
3438 NULL, &flags);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003439 BUG_ON(ret);
3440
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003441 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)
3442 ret = 1;
3443 else
3444 ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003445 return ret;
3446}
3447
Josef Bacik0af3d002010-06-21 14:48:16 -04003448static int delete_block_group_cache(struct btrfs_fs_info *fs_info,
Chris Mason1bbc6212015-04-06 12:46:08 -07003449 struct btrfs_block_group_cache *block_group,
3450 struct inode *inode,
3451 u64 ino)
Josef Bacik0af3d002010-06-21 14:48:16 -04003452{
3453 struct btrfs_key key;
Josef Bacik0af3d002010-06-21 14:48:16 -04003454 struct btrfs_root *root = fs_info->tree_root;
3455 struct btrfs_trans_handle *trans;
Josef Bacik0af3d002010-06-21 14:48:16 -04003456 int ret = 0;
3457
3458 if (inode)
3459 goto truncate;
3460
3461 key.objectid = ino;
3462 key.type = BTRFS_INODE_ITEM_KEY;
3463 key.offset = 0;
3464
3465 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
Tsutomu Itohf54fb852012-09-06 03:08:59 -06003466 if (IS_ERR(inode) || is_bad_inode(inode)) {
3467 if (!IS_ERR(inode))
Josef Bacik0af3d002010-06-21 14:48:16 -04003468 iput(inode);
3469 return -ENOENT;
3470 }
3471
3472truncate:
Miao Xie7b61cd92013-05-13 13:55:09 +00003473 ret = btrfs_check_trunc_cache_free_space(root,
3474 &fs_info->global_block_rsv);
3475 if (ret)
3476 goto out;
3477
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003478 trans = btrfs_join_transaction(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04003479 if (IS_ERR(trans)) {
Tsutomu Itoh3612b492011-01-25 02:51:38 +00003480 ret = PTR_ERR(trans);
Josef Bacik0af3d002010-06-21 14:48:16 -04003481 goto out;
3482 }
3483
Chris Mason1bbc6212015-04-06 12:46:08 -07003484 ret = btrfs_truncate_free_space_cache(root, trans, block_group, inode);
Josef Bacik0af3d002010-06-21 14:48:16 -04003485
Josef Bacik0af3d002010-06-21 14:48:16 -04003486 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00003487 btrfs_btree_balance_dirty(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04003488out:
3489 iput(inode);
3490 return ret;
3491}
3492
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003493/*
3494 * helper to add tree blocks for backref of type BTRFS_EXTENT_DATA_REF_KEY
3495 * this function scans fs tree to find blocks reference the data extent
3496 */
3497static int find_data_references(struct reloc_control *rc,
3498 struct btrfs_key *extent_key,
3499 struct extent_buffer *leaf,
3500 struct btrfs_extent_data_ref *ref,
3501 struct rb_root *blocks)
3502{
3503 struct btrfs_path *path;
3504 struct tree_block *block;
3505 struct btrfs_root *root;
3506 struct btrfs_file_extent_item *fi;
3507 struct rb_node *rb_node;
3508 struct btrfs_key key;
3509 u64 ref_root;
3510 u64 ref_objectid;
3511 u64 ref_offset;
3512 u32 ref_count;
3513 u32 nritems;
3514 int err = 0;
3515 int added = 0;
3516 int counted;
3517 int ret;
3518
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003519 ref_root = btrfs_extent_data_ref_root(leaf, ref);
3520 ref_objectid = btrfs_extent_data_ref_objectid(leaf, ref);
3521 ref_offset = btrfs_extent_data_ref_offset(leaf, ref);
3522 ref_count = btrfs_extent_data_ref_count(leaf, ref);
3523
Josef Bacik0af3d002010-06-21 14:48:16 -04003524 /*
3525 * This is an extent belonging to the free space cache, lets just delete
3526 * it and redo the search.
3527 */
3528 if (ref_root == BTRFS_ROOT_TREE_OBJECTID) {
3529 ret = delete_block_group_cache(rc->extent_root->fs_info,
Chris Mason1bbc6212015-04-06 12:46:08 -07003530 rc->block_group,
Josef Bacik0af3d002010-06-21 14:48:16 -04003531 NULL, ref_objectid);
3532 if (ret != -ENOENT)
3533 return ret;
3534 ret = 0;
3535 }
3536
3537 path = btrfs_alloc_path();
3538 if (!path)
3539 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +01003540 path->reada = READA_FORWARD;
Josef Bacik0af3d002010-06-21 14:48:16 -04003541
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003542 root = read_fs_root(rc->extent_root->fs_info, ref_root);
3543 if (IS_ERR(root)) {
3544 err = PTR_ERR(root);
3545 goto out;
3546 }
3547
3548 key.objectid = ref_objectid;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003549 key.type = BTRFS_EXTENT_DATA_KEY;
Yan, Zheng84850e82011-08-29 09:25:53 +08003550 if (ref_offset > ((u64)-1 << 32))
3551 key.offset = 0;
3552 else
3553 key.offset = ref_offset;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003554
3555 path->search_commit_root = 1;
3556 path->skip_locking = 1;
3557 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3558 if (ret < 0) {
3559 err = ret;
3560 goto out;
3561 }
3562
3563 leaf = path->nodes[0];
3564 nritems = btrfs_header_nritems(leaf);
3565 /*
3566 * the references in tree blocks that use full backrefs
3567 * are not counted in
3568 */
3569 if (block_use_full_backref(rc, leaf))
3570 counted = 0;
3571 else
3572 counted = 1;
3573 rb_node = tree_search(blocks, leaf->start);
3574 if (rb_node) {
3575 if (counted)
3576 added = 1;
3577 else
3578 path->slots[0] = nritems;
3579 }
3580
3581 while (ref_count > 0) {
3582 while (path->slots[0] >= nritems) {
3583 ret = btrfs_next_leaf(root, path);
3584 if (ret < 0) {
3585 err = ret;
3586 goto out;
3587 }
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303588 if (WARN_ON(ret > 0))
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003589 goto out;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003590
3591 leaf = path->nodes[0];
3592 nritems = btrfs_header_nritems(leaf);
3593 added = 0;
3594
3595 if (block_use_full_backref(rc, leaf))
3596 counted = 0;
3597 else
3598 counted = 1;
3599 rb_node = tree_search(blocks, leaf->start);
3600 if (rb_node) {
3601 if (counted)
3602 added = 1;
3603 else
3604 path->slots[0] = nritems;
3605 }
3606 }
3607
3608 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303609 if (WARN_ON(key.objectid != ref_objectid ||
3610 key.type != BTRFS_EXTENT_DATA_KEY))
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003611 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003612
3613 fi = btrfs_item_ptr(leaf, path->slots[0],
3614 struct btrfs_file_extent_item);
3615
3616 if (btrfs_file_extent_type(leaf, fi) ==
3617 BTRFS_FILE_EXTENT_INLINE)
3618 goto next;
3619
3620 if (btrfs_file_extent_disk_bytenr(leaf, fi) !=
3621 extent_key->objectid)
3622 goto next;
3623
3624 key.offset -= btrfs_file_extent_offset(leaf, fi);
3625 if (key.offset != ref_offset)
3626 goto next;
3627
3628 if (counted)
3629 ref_count--;
3630 if (added)
3631 goto next;
3632
David Sterba7476dfd2014-06-15 03:34:59 +02003633 if (!tree_block_processed(leaf->start, rc)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003634 block = kmalloc(sizeof(*block), GFP_NOFS);
3635 if (!block) {
3636 err = -ENOMEM;
3637 break;
3638 }
3639 block->bytenr = leaf->start;
3640 btrfs_item_key_to_cpu(leaf, &block->key, 0);
3641 block->level = 0;
3642 block->key_ready = 1;
3643 rb_node = tree_insert(blocks, block->bytenr,
3644 &block->rb_node);
Jeff Mahoney43c04fb2011-10-03 23:22:33 -04003645 if (rb_node)
3646 backref_tree_panic(rb_node, -EEXIST,
3647 block->bytenr);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003648 }
3649 if (counted)
3650 added = 1;
3651 else
3652 path->slots[0] = nritems;
3653next:
3654 path->slots[0]++;
3655
3656 }
3657out:
3658 btrfs_free_path(path);
3659 return err;
3660}
3661
3662/*
Liu Bo2c016dc2012-12-26 15:32:17 +08003663 * helper to find all tree blocks that reference a given data extent
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003664 */
3665static noinline_for_stack
3666int add_data_references(struct reloc_control *rc,
3667 struct btrfs_key *extent_key,
3668 struct btrfs_path *path,
3669 struct rb_root *blocks)
3670{
3671 struct btrfs_key key;
3672 struct extent_buffer *eb;
3673 struct btrfs_extent_data_ref *dref;
3674 struct btrfs_extent_inline_ref *iref;
3675 unsigned long ptr;
3676 unsigned long end;
David Sterba707e8a02014-06-04 19:22:26 +02003677 u32 blocksize = rc->extent_root->nodesize;
Filipe David Borba Manana647f63b2013-07-13 12:25:15 +01003678 int ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003679 int err = 0;
3680
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003681 eb = path->nodes[0];
3682 ptr = btrfs_item_ptr_offset(eb, path->slots[0]);
3683 end = ptr + btrfs_item_size_nr(eb, path->slots[0]);
3684#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3685 if (ptr + sizeof(struct btrfs_extent_item_v0) == end)
3686 ptr = end;
3687 else
3688#endif
3689 ptr += sizeof(struct btrfs_extent_item);
3690
3691 while (ptr < end) {
3692 iref = (struct btrfs_extent_inline_ref *)ptr;
3693 key.type = btrfs_extent_inline_ref_type(eb, iref);
3694 if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
3695 key.offset = btrfs_extent_inline_ref_offset(eb, iref);
3696 ret = __add_tree_block(rc, key.offset, blocksize,
3697 blocks);
3698 } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
3699 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
3700 ret = find_data_references(rc, extent_key,
3701 eb, dref, blocks);
3702 } else {
3703 BUG();
3704 }
Filipe David Borba Manana647f63b2013-07-13 12:25:15 +01003705 if (ret) {
3706 err = ret;
3707 goto out;
3708 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003709 ptr += btrfs_extent_inline_ref_size(key.type);
3710 }
3711 WARN_ON(ptr > end);
3712
3713 while (1) {
3714 cond_resched();
3715 eb = path->nodes[0];
3716 if (path->slots[0] >= btrfs_header_nritems(eb)) {
3717 ret = btrfs_next_leaf(rc->extent_root, path);
3718 if (ret < 0) {
3719 err = ret;
3720 break;
3721 }
3722 if (ret > 0)
3723 break;
3724 eb = path->nodes[0];
3725 }
3726
3727 btrfs_item_key_to_cpu(eb, &key, path->slots[0]);
3728 if (key.objectid != extent_key->objectid)
3729 break;
3730
3731#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3732 if (key.type == BTRFS_SHARED_DATA_REF_KEY ||
3733 key.type == BTRFS_EXTENT_REF_V0_KEY) {
3734#else
3735 BUG_ON(key.type == BTRFS_EXTENT_REF_V0_KEY);
3736 if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
3737#endif
3738 ret = __add_tree_block(rc, key.offset, blocksize,
3739 blocks);
3740 } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
3741 dref = btrfs_item_ptr(eb, path->slots[0],
3742 struct btrfs_extent_data_ref);
3743 ret = find_data_references(rc, extent_key,
3744 eb, dref, blocks);
3745 } else {
3746 ret = 0;
3747 }
3748 if (ret) {
3749 err = ret;
3750 break;
3751 }
3752 path->slots[0]++;
3753 }
Filipe David Borba Manana647f63b2013-07-13 12:25:15 +01003754out:
David Sterbab3b4aa72011-04-21 01:20:15 +02003755 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003756 if (err)
3757 free_block_list(blocks);
3758 return err;
3759}
3760
3761/*
Liu Bo2c016dc2012-12-26 15:32:17 +08003762 * helper to find next unprocessed extent
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003763 */
3764static noinline_for_stack
Zhaolei147d2562015-08-06 20:58:11 +08003765int find_next_extent(struct reloc_control *rc, struct btrfs_path *path,
Yan, Zheng3fd0a552010-05-16 10:49:59 -04003766 struct btrfs_key *extent_key)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003767{
3768 struct btrfs_key key;
3769 struct extent_buffer *leaf;
3770 u64 start, end, last;
3771 int ret;
3772
3773 last = rc->block_group->key.objectid + rc->block_group->key.offset;
3774 while (1) {
3775 cond_resched();
3776 if (rc->search_start >= last) {
3777 ret = 1;
3778 break;
3779 }
3780
3781 key.objectid = rc->search_start;
3782 key.type = BTRFS_EXTENT_ITEM_KEY;
3783 key.offset = 0;
3784
3785 path->search_commit_root = 1;
3786 path->skip_locking = 1;
3787 ret = btrfs_search_slot(NULL, rc->extent_root, &key, path,
3788 0, 0);
3789 if (ret < 0)
3790 break;
3791next:
3792 leaf = path->nodes[0];
3793 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
3794 ret = btrfs_next_leaf(rc->extent_root, path);
3795 if (ret != 0)
3796 break;
3797 leaf = path->nodes[0];
3798 }
3799
3800 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
3801 if (key.objectid >= last) {
3802 ret = 1;
3803 break;
3804 }
3805
Josef Bacik3173a182013-03-07 14:22:04 -05003806 if (key.type != BTRFS_EXTENT_ITEM_KEY &&
3807 key.type != BTRFS_METADATA_ITEM_KEY) {
3808 path->slots[0]++;
3809 goto next;
3810 }
3811
3812 if (key.type == BTRFS_EXTENT_ITEM_KEY &&
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003813 key.objectid + key.offset <= rc->search_start) {
3814 path->slots[0]++;
3815 goto next;
3816 }
3817
Josef Bacik3173a182013-03-07 14:22:04 -05003818 if (key.type == BTRFS_METADATA_ITEM_KEY &&
David Sterba707e8a02014-06-04 19:22:26 +02003819 key.objectid + rc->extent_root->nodesize <=
Josef Bacik3173a182013-03-07 14:22:04 -05003820 rc->search_start) {
3821 path->slots[0]++;
3822 goto next;
3823 }
3824
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003825 ret = find_first_extent_bit(&rc->processed_blocks,
3826 key.objectid, &start, &end,
Josef Bacike6138872012-09-27 17:07:30 -04003827 EXTENT_DIRTY, NULL);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003828
3829 if (ret == 0 && start <= key.objectid) {
David Sterbab3b4aa72011-04-21 01:20:15 +02003830 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003831 rc->search_start = end + 1;
3832 } else {
Josef Bacik3173a182013-03-07 14:22:04 -05003833 if (key.type == BTRFS_EXTENT_ITEM_KEY)
3834 rc->search_start = key.objectid + key.offset;
3835 else
3836 rc->search_start = key.objectid +
David Sterba707e8a02014-06-04 19:22:26 +02003837 rc->extent_root->nodesize;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04003838 memcpy(extent_key, &key, sizeof(key));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003839 return 0;
3840 }
3841 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003842 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003843 return ret;
3844}
3845
3846static void set_reloc_control(struct reloc_control *rc)
3847{
3848 struct btrfs_fs_info *fs_info = rc->extent_root->fs_info;
Chris Mason75857172011-06-13 20:00:16 -04003849
3850 mutex_lock(&fs_info->reloc_mutex);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003851 fs_info->reloc_ctl = rc;
Chris Mason75857172011-06-13 20:00:16 -04003852 mutex_unlock(&fs_info->reloc_mutex);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003853}
3854
3855static void unset_reloc_control(struct reloc_control *rc)
3856{
3857 struct btrfs_fs_info *fs_info = rc->extent_root->fs_info;
Chris Mason75857172011-06-13 20:00:16 -04003858
3859 mutex_lock(&fs_info->reloc_mutex);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003860 fs_info->reloc_ctl = NULL;
Chris Mason75857172011-06-13 20:00:16 -04003861 mutex_unlock(&fs_info->reloc_mutex);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003862}
3863
3864static int check_extent_flags(u64 flags)
3865{
3866 if ((flags & BTRFS_EXTENT_FLAG_DATA) &&
3867 (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK))
3868 return 1;
3869 if (!(flags & BTRFS_EXTENT_FLAG_DATA) &&
3870 !(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK))
3871 return 1;
3872 if ((flags & BTRFS_EXTENT_FLAG_DATA) &&
3873 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
3874 return 1;
3875 return 0;
3876}
3877
Yan, Zheng3fd0a552010-05-16 10:49:59 -04003878static noinline_for_stack
3879int prepare_to_relocate(struct reloc_control *rc)
3880{
3881 struct btrfs_trans_handle *trans;
Josef Bacikac2faba2016-05-27 13:08:26 -04003882 int ret;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04003883
Miao Xie66d8f3d2012-09-06 04:02:28 -06003884 rc->block_rsv = btrfs_alloc_block_rsv(rc->extent_root,
3885 BTRFS_BLOCK_RSV_TEMP);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04003886 if (!rc->block_rsv)
3887 return -ENOMEM;
3888
Yan, Zheng3fd0a552010-05-16 10:49:59 -04003889 memset(&rc->cluster, 0, sizeof(rc->cluster));
3890 rc->search_start = rc->block_group->key.objectid;
3891 rc->extents_found = 0;
3892 rc->nodes_relocated = 0;
3893 rc->merging_rsv_size = 0;
Wang Shilong0647bf52013-11-20 09:01:52 +08003894 rc->reserved_bytes = 0;
3895 rc->block_rsv->size = rc->extent_root->nodesize *
3896 RELOCATION_RESERVED_NODES;
Josef Bacikac2faba2016-05-27 13:08:26 -04003897 ret = btrfs_block_rsv_refill(rc->extent_root,
3898 rc->block_rsv, rc->block_rsv->size,
3899 BTRFS_RESERVE_FLUSH_ALL);
3900 if (ret)
3901 return ret;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04003902
3903 rc->create_reloc_tree = 1;
3904 set_reloc_control(rc);
3905
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003906 trans = btrfs_join_transaction(rc->extent_root);
Liu Bo28818942013-03-04 16:25:39 +00003907 if (IS_ERR(trans)) {
3908 unset_reloc_control(rc);
3909 /*
3910 * extent tree is not a ref_cow tree and has no reloc_root to
3911 * cleanup. And callers are responsible to free the above
3912 * block rsv.
3913 */
3914 return PTR_ERR(trans);
3915 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -04003916 btrfs_commit_transaction(trans, rc->extent_root);
3917 return 0;
3918}
Yan, Zheng76dda932009-09-21 16:00:26 -04003919
Qu Wenruo62b99542016-08-15 10:36:51 +08003920/*
3921 * Qgroup fixer for data chunk relocation.
3922 * The data relocation is done in the following steps
3923 * 1) Copy data extents into data reloc tree
3924 * 2) Create tree reloc tree(special snapshot) for related subvolumes
3925 * 3) Modify file extents in tree reloc tree
3926 * 4) Merge tree reloc tree with original fs tree, by swapping tree blocks
3927 *
3928 * The problem is, data and tree reloc tree are not accounted to qgroup,
3929 * and 4) will only info qgroup to track tree blocks change, not file extents
3930 * in the tree blocks.
3931 *
3932 * The good news is, related data extents are all in data reloc tree, so we
3933 * only need to info qgroup to track all file extents in data reloc tree
3934 * before commit trans.
3935 */
3936static int qgroup_fix_relocated_data_extents(struct btrfs_trans_handle *trans,
3937 struct reloc_control *rc)
3938{
3939 struct btrfs_fs_info *fs_info = rc->extent_root->fs_info;
3940 struct inode *inode = rc->data_inode;
3941 struct btrfs_root *data_reloc_root = BTRFS_I(inode)->root;
3942 struct btrfs_path *path;
3943 struct btrfs_key key;
3944 int ret = 0;
3945
3946 if (!fs_info->quota_enabled)
3947 return 0;
3948
3949 /*
3950 * Only for stage where we update data pointers the qgroup fix is
3951 * valid.
3952 * For MOVING_DATA stage, we will miss the timing of swapping tree
3953 * blocks, and won't fix it.
3954 */
3955 if (!(rc->stage == UPDATE_DATA_PTRS && rc->extents_found))
3956 return 0;
3957
3958 path = btrfs_alloc_path();
3959 if (!path)
3960 return -ENOMEM;
3961 key.objectid = btrfs_ino(inode);
3962 key.type = BTRFS_EXTENT_DATA_KEY;
3963 key.offset = 0;
3964
3965 ret = btrfs_search_slot(NULL, data_reloc_root, &key, path, 0, 0);
3966 if (ret < 0)
3967 goto out;
3968
3969 lock_extent(&BTRFS_I(inode)->io_tree, 0, (u64)-1);
3970 while (1) {
3971 struct btrfs_file_extent_item *fi;
3972
3973 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
3974 if (key.objectid > btrfs_ino(inode))
3975 break;
3976 if (key.type != BTRFS_EXTENT_DATA_KEY)
3977 goto next;
3978 fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
3979 struct btrfs_file_extent_item);
3980 if (btrfs_file_extent_type(path->nodes[0], fi) !=
3981 BTRFS_FILE_EXTENT_REG)
3982 goto next;
3983 ret = btrfs_qgroup_insert_dirty_extent(trans, fs_info,
3984 btrfs_file_extent_disk_bytenr(path->nodes[0], fi),
3985 btrfs_file_extent_disk_num_bytes(path->nodes[0], fi),
3986 GFP_NOFS);
3987 if (ret < 0)
3988 break;
3989next:
3990 ret = btrfs_next_item(data_reloc_root, path);
3991 if (ret < 0)
3992 break;
3993 if (ret > 0) {
3994 ret = 0;
3995 break;
3996 }
3997 }
3998 unlock_extent(&BTRFS_I(inode)->io_tree, 0 , (u64)-1);
3999out:
4000 btrfs_free_path(path);
4001 return ret;
4002}
4003
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004004static noinline_for_stack int relocate_block_group(struct reloc_control *rc)
4005{
4006 struct rb_root blocks = RB_ROOT;
4007 struct btrfs_key key;
4008 struct btrfs_trans_handle *trans = NULL;
4009 struct btrfs_path *path;
4010 struct btrfs_extent_item *ei;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004011 u64 flags;
4012 u32 item_size;
4013 int ret;
4014 int err = 0;
Chris Masonc87f08c2011-02-16 13:57:04 -05004015 int progress = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004016
4017 path = btrfs_alloc_path();
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004018 if (!path)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004019 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +01004020 path->reada = READA_FORWARD;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004021
4022 ret = prepare_to_relocate(rc);
4023 if (ret) {
4024 err = ret;
4025 goto out_free;
Jiri Slaby2423fdf2010-01-06 16:57:22 +00004026 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004027
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004028 while (1) {
Wang Shilong0647bf52013-11-20 09:01:52 +08004029 rc->reserved_bytes = 0;
4030 ret = btrfs_block_rsv_refill(rc->extent_root,
4031 rc->block_rsv, rc->block_rsv->size,
4032 BTRFS_RESERVE_FLUSH_ALL);
4033 if (ret) {
4034 err = ret;
4035 break;
4036 }
Chris Masonc87f08c2011-02-16 13:57:04 -05004037 progress++;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004038 trans = btrfs_start_transaction(rc->extent_root, 0);
Liu Bo0f788c52013-03-04 16:25:40 +00004039 if (IS_ERR(trans)) {
4040 err = PTR_ERR(trans);
4041 trans = NULL;
4042 break;
4043 }
Chris Masonc87f08c2011-02-16 13:57:04 -05004044restart:
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004045 if (update_backref_cache(trans, &rc->backref_cache)) {
4046 btrfs_end_transaction(trans, rc->extent_root);
4047 continue;
4048 }
4049
Zhaolei147d2562015-08-06 20:58:11 +08004050 ret = find_next_extent(rc, path, &key);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004051 if (ret < 0)
4052 err = ret;
4053 if (ret != 0)
4054 break;
4055
4056 rc->extents_found++;
4057
4058 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
4059 struct btrfs_extent_item);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004060 item_size = btrfs_item_size_nr(path->nodes[0], path->slots[0]);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004061 if (item_size >= sizeof(*ei)) {
4062 flags = btrfs_extent_flags(path->nodes[0], ei);
4063 ret = check_extent_flags(flags);
4064 BUG_ON(ret);
4065
4066 } else {
4067#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
4068 u64 ref_owner;
4069 int path_change = 0;
4070
4071 BUG_ON(item_size !=
4072 sizeof(struct btrfs_extent_item_v0));
4073 ret = get_ref_objectid_v0(rc, path, &key, &ref_owner,
4074 &path_change);
Zhaolei4b3576e2015-08-05 18:00:02 +08004075 if (ret < 0) {
4076 err = ret;
4077 break;
4078 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004079 if (ref_owner < BTRFS_FIRST_FREE_OBJECTID)
4080 flags = BTRFS_EXTENT_FLAG_TREE_BLOCK;
4081 else
4082 flags = BTRFS_EXTENT_FLAG_DATA;
4083
4084 if (path_change) {
David Sterbab3b4aa72011-04-21 01:20:15 +02004085 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004086
4087 path->search_commit_root = 1;
4088 path->skip_locking = 1;
4089 ret = btrfs_search_slot(NULL, rc->extent_root,
4090 &key, path, 0, 0);
4091 if (ret < 0) {
4092 err = ret;
4093 break;
4094 }
4095 BUG_ON(ret > 0);
4096 }
4097#else
4098 BUG();
4099#endif
4100 }
4101
4102 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
4103 ret = add_tree_block(rc, &key, path, &blocks);
4104 } else if (rc->stage == UPDATE_DATA_PTRS &&
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004105 (flags & BTRFS_EXTENT_FLAG_DATA)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004106 ret = add_data_references(rc, &key, path, &blocks);
4107 } else {
David Sterbab3b4aa72011-04-21 01:20:15 +02004108 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004109 ret = 0;
4110 }
4111 if (ret < 0) {
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004112 err = ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004113 break;
4114 }
4115
4116 if (!RB_EMPTY_ROOT(&blocks)) {
4117 ret = relocate_tree_blocks(trans, rc, &blocks);
4118 if (ret < 0) {
Wang Shilong1708cc52013-12-28 19:52:39 +08004119 /*
4120 * if we fail to relocate tree blocks, force to update
4121 * backref cache when committing transaction.
4122 */
4123 rc->backref_cache.last_trans = trans->transid - 1;
4124
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004125 if (ret != -EAGAIN) {
4126 err = ret;
4127 break;
4128 }
4129 rc->extents_found--;
4130 rc->search_start = key.objectid;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004131 }
4132 }
4133
Wang Shilong0647bf52013-11-20 09:01:52 +08004134 btrfs_end_transaction_throttle(trans, rc->extent_root);
4135 btrfs_btree_balance_dirty(rc->extent_root);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004136 trans = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004137
4138 if (rc->stage == MOVE_DATA_EXTENTS &&
4139 (flags & BTRFS_EXTENT_FLAG_DATA)) {
4140 rc->found_file_extent = 1;
Yan, Zheng0257bb82009-09-24 09:17:31 -04004141 ret = relocate_data_extent(rc->data_inode,
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004142 &key, &rc->cluster);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004143 if (ret < 0) {
4144 err = ret;
4145 break;
4146 }
4147 }
4148 }
Chris Masonc87f08c2011-02-16 13:57:04 -05004149 if (trans && progress && err == -ENOSPC) {
4150 ret = btrfs_force_chunk_alloc(trans, rc->extent_root,
4151 rc->block_group->flags);
Shilong Wang96894572015-04-12 14:35:20 +08004152 if (ret == 1) {
Chris Masonc87f08c2011-02-16 13:57:04 -05004153 err = 0;
4154 progress = 0;
4155 goto restart;
4156 }
4157 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004158
David Sterbab3b4aa72011-04-21 01:20:15 +02004159 btrfs_release_path(path);
David Sterba91166212016-04-26 23:54:39 +02004160 clear_extent_bits(&rc->processed_blocks, 0, (u64)-1, EXTENT_DIRTY);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004161
4162 if (trans) {
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004163 btrfs_end_transaction_throttle(trans, rc->extent_root);
Liu Bob53d3f52012-11-14 14:34:34 +00004164 btrfs_btree_balance_dirty(rc->extent_root);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004165 }
4166
Yan, Zheng0257bb82009-09-24 09:17:31 -04004167 if (!err) {
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004168 ret = relocate_file_extent_cluster(rc->data_inode,
4169 &rc->cluster);
Yan, Zheng0257bb82009-09-24 09:17:31 -04004170 if (ret < 0)
4171 err = ret;
4172 }
4173
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004174 rc->create_reloc_tree = 0;
4175 set_reloc_control(rc);
Yan, Zheng0257bb82009-09-24 09:17:31 -04004176
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004177 backref_cache_cleanup(&rc->backref_cache);
4178 btrfs_block_rsv_release(rc->extent_root, rc->block_rsv, (u64)-1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004179
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004180 err = prepare_to_merge(rc, err);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004181
4182 merge_reloc_roots(rc);
4183
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004184 rc->merge_reloc_tree = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004185 unset_reloc_control(rc);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004186 btrfs_block_rsv_release(rc->extent_root, rc->block_rsv, (u64)-1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004187
4188 /* get rid of pinned extents */
Josef Bacik7a7eaa42011-04-13 12:54:33 -04004189 trans = btrfs_join_transaction(rc->extent_root);
Qu Wenruo62b99542016-08-15 10:36:51 +08004190 if (IS_ERR(trans)) {
Tsutomu Itoh3612b492011-01-25 02:51:38 +00004191 err = PTR_ERR(trans);
Qu Wenruo62b99542016-08-15 10:36:51 +08004192 goto out_free;
4193 }
4194 err = qgroup_fix_relocated_data_extents(trans, rc);
4195 if (err < 0) {
4196 btrfs_abort_transaction(trans, err);
4197 goto out_free;
4198 }
4199 btrfs_commit_transaction(trans, rc->extent_root);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004200out_free:
4201 btrfs_free_block_rsv(rc->extent_root, rc->block_rsv);
4202 btrfs_free_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004203 return err;
4204}
4205
4206static int __insert_orphan_inode(struct btrfs_trans_handle *trans,
Yan, Zheng0257bb82009-09-24 09:17:31 -04004207 struct btrfs_root *root, u64 objectid)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004208{
4209 struct btrfs_path *path;
4210 struct btrfs_inode_item *item;
4211 struct extent_buffer *leaf;
4212 int ret;
4213
4214 path = btrfs_alloc_path();
4215 if (!path)
4216 return -ENOMEM;
4217
4218 ret = btrfs_insert_empty_inode(trans, root, path, objectid);
4219 if (ret)
4220 goto out;
4221
4222 leaf = path->nodes[0];
4223 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_inode_item);
4224 memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item));
4225 btrfs_set_inode_generation(leaf, item, 1);
Yan, Zheng0257bb82009-09-24 09:17:31 -04004226 btrfs_set_inode_size(leaf, item, 0);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004227 btrfs_set_inode_mode(leaf, item, S_IFREG | 0600);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004228 btrfs_set_inode_flags(leaf, item, BTRFS_INODE_NOCOMPRESS |
4229 BTRFS_INODE_PREALLOC);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004230 btrfs_mark_buffer_dirty(leaf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004231out:
4232 btrfs_free_path(path);
4233 return ret;
4234}
4235
4236/*
4237 * helper to create inode for data relocation.
4238 * the inode is in data relocation tree and its link count is 0
4239 */
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004240static noinline_for_stack
4241struct inode *create_reloc_inode(struct btrfs_fs_info *fs_info,
4242 struct btrfs_block_group_cache *group)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004243{
4244 struct inode *inode = NULL;
4245 struct btrfs_trans_handle *trans;
4246 struct btrfs_root *root;
4247 struct btrfs_key key;
Zhaolei46249002015-08-05 18:00:03 +08004248 u64 objectid;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004249 int err = 0;
4250
4251 root = read_fs_root(fs_info, BTRFS_DATA_RELOC_TREE_OBJECTID);
4252 if (IS_ERR(root))
4253 return ERR_CAST(root);
4254
Yan, Zhenga22285a2010-05-16 10:48:46 -04004255 trans = btrfs_start_transaction(root, 6);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004256 if (IS_ERR(trans))
4257 return ERR_CAST(trans);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004258
Li Zefan581bb052011-04-20 10:06:11 +08004259 err = btrfs_find_free_objectid(root, &objectid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004260 if (err)
4261 goto out;
4262
Yan, Zheng0257bb82009-09-24 09:17:31 -04004263 err = __insert_orphan_inode(trans, root, objectid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004264 BUG_ON(err);
4265
4266 key.objectid = objectid;
4267 key.type = BTRFS_INODE_ITEM_KEY;
4268 key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +00004269 inode = btrfs_iget(root->fs_info->sb, &key, root, NULL);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004270 BUG_ON(IS_ERR(inode) || is_bad_inode(inode));
4271 BTRFS_I(inode)->index_cnt = group->key.objectid;
4272
4273 err = btrfs_orphan_add(trans, inode);
4274out:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004275 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00004276 btrfs_btree_balance_dirty(root);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004277 if (err) {
4278 if (inode)
4279 iput(inode);
4280 inode = ERR_PTR(err);
4281 }
4282 return inode;
4283}
4284
Josef Bacika9995ee2013-05-31 13:04:36 -04004285static struct reloc_control *alloc_reloc_control(struct btrfs_fs_info *fs_info)
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004286{
4287 struct reloc_control *rc;
4288
4289 rc = kzalloc(sizeof(*rc), GFP_NOFS);
4290 if (!rc)
4291 return NULL;
4292
4293 INIT_LIST_HEAD(&rc->reloc_roots);
4294 backref_cache_init(&rc->backref_cache);
4295 mapping_tree_init(&rc->reloc_root_tree);
Josef Bacika9995ee2013-05-31 13:04:36 -04004296 extent_io_tree_init(&rc->processed_blocks,
4297 fs_info->btree_inode->i_mapping);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004298 return rc;
4299}
4300
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004301/*
4302 * function to relocate all extents in a block group.
4303 */
4304int btrfs_relocate_block_group(struct btrfs_root *extent_root, u64 group_start)
4305{
4306 struct btrfs_fs_info *fs_info = extent_root->fs_info;
4307 struct reloc_control *rc;
Josef Bacik0af3d002010-06-21 14:48:16 -04004308 struct inode *inode;
4309 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004310 int ret;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004311 int rw = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004312 int err = 0;
4313
Josef Bacika9995ee2013-05-31 13:04:36 -04004314 rc = alloc_reloc_control(fs_info);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004315 if (!rc)
4316 return -ENOMEM;
4317
Yan, Zhengf0486c62010-05-16 10:46:25 -04004318 rc->extent_root = extent_root;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004319
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004320 rc->block_group = btrfs_lookup_block_group(fs_info, group_start);
4321 BUG_ON(!rc->block_group);
4322
Zhaolei868f4012015-08-05 16:43:27 +08004323 ret = btrfs_inc_block_group_ro(extent_root, rc->block_group);
4324 if (ret) {
4325 err = ret;
4326 goto out;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004327 }
Zhaolei868f4012015-08-05 16:43:27 +08004328 rw = 1;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004329
Josef Bacik0af3d002010-06-21 14:48:16 -04004330 path = btrfs_alloc_path();
4331 if (!path) {
4332 err = -ENOMEM;
4333 goto out;
4334 }
4335
4336 inode = lookup_free_space_inode(fs_info->tree_root, rc->block_group,
4337 path);
4338 btrfs_free_path(path);
4339
4340 if (!IS_ERR(inode))
Chris Mason1bbc6212015-04-06 12:46:08 -07004341 ret = delete_block_group_cache(fs_info, rc->block_group, inode, 0);
Josef Bacik0af3d002010-06-21 14:48:16 -04004342 else
4343 ret = PTR_ERR(inode);
4344
4345 if (ret && ret != -ENOENT) {
4346 err = ret;
4347 goto out;
4348 }
4349
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004350 rc->data_inode = create_reloc_inode(fs_info, rc->block_group);
4351 if (IS_ERR(rc->data_inode)) {
4352 err = PTR_ERR(rc->data_inode);
4353 rc->data_inode = NULL;
4354 goto out;
4355 }
4356
Frank Holtonefe120a2013-12-20 11:37:06 -05004357 btrfs_info(extent_root->fs_info, "relocating block group %llu flags %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02004358 rc->block_group->key.objectid, rc->block_group->flags);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004359
Filipe Manana9cfa3e32016-04-26 15:39:32 +01004360 btrfs_wait_block_group_reservations(rc->block_group);
Filipe Mananaf78c4362016-05-09 13:15:41 +01004361 btrfs_wait_nocow_writers(rc->block_group);
Filipe Manana578def72016-04-26 15:36:38 +01004362 btrfs_wait_ordered_roots(fs_info, -1,
4363 rc->block_group->key.objectid,
4364 rc->block_group->key.offset);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004365
4366 while (1) {
Yan, Zheng76dda932009-09-21 16:00:26 -04004367 mutex_lock(&fs_info->cleaner_mutex);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004368 ret = relocate_block_group(rc);
Yan, Zheng76dda932009-09-21 16:00:26 -04004369 mutex_unlock(&fs_info->cleaner_mutex);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004370 if (ret < 0) {
4371 err = ret;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004372 goto out;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004373 }
4374
4375 if (rc->extents_found == 0)
4376 break;
4377
Frank Holtonefe120a2013-12-20 11:37:06 -05004378 btrfs_info(extent_root->fs_info, "found %llu extents",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02004379 rc->extents_found);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004380
4381 if (rc->stage == MOVE_DATA_EXTENTS && rc->found_file_extent) {
Josef Bacik0ef8b722013-10-25 16:13:35 -04004382 ret = btrfs_wait_ordered_range(rc->data_inode, 0,
4383 (u64)-1);
4384 if (ret) {
4385 err = ret;
4386 goto out;
4387 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004388 invalidate_mapping_pages(rc->data_inode->i_mapping,
4389 0, -1);
4390 rc->stage = UPDATE_DATA_PTRS;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004391 }
4392 }
4393
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004394 WARN_ON(rc->block_group->pinned > 0);
4395 WARN_ON(rc->block_group->reserved > 0);
4396 WARN_ON(btrfs_block_group_used(&rc->block_group->item) > 0);
4397out:
Yan, Zhengf0486c62010-05-16 10:46:25 -04004398 if (err && rw)
Zhaolei868f4012015-08-05 16:43:27 +08004399 btrfs_dec_block_group_ro(extent_root, rc->block_group);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004400 iput(rc->data_inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004401 btrfs_put_block_group(rc->block_group);
4402 kfree(rc);
4403 return err;
4404}
4405
Yan, Zheng76dda932009-09-21 16:00:26 -04004406static noinline_for_stack int mark_garbage_root(struct btrfs_root *root)
4407{
4408 struct btrfs_trans_handle *trans;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004409 int ret, err;
Yan, Zheng76dda932009-09-21 16:00:26 -04004410
Yan, Zhenga22285a2010-05-16 10:48:46 -04004411 trans = btrfs_start_transaction(root->fs_info->tree_root, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004412 if (IS_ERR(trans))
4413 return PTR_ERR(trans);
Yan, Zheng76dda932009-09-21 16:00:26 -04004414
4415 memset(&root->root_item.drop_progress, 0,
4416 sizeof(root->root_item.drop_progress));
4417 root->root_item.drop_level = 0;
4418 btrfs_set_root_refs(&root->root_item, 0);
4419 ret = btrfs_update_root(trans, root->fs_info->tree_root,
4420 &root->root_key, &root->root_item);
Yan, Zheng76dda932009-09-21 16:00:26 -04004421
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004422 err = btrfs_end_transaction(trans, root->fs_info->tree_root);
4423 if (err)
4424 return err;
4425 return ret;
Yan, Zheng76dda932009-09-21 16:00:26 -04004426}
4427
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004428/*
4429 * recover relocation interrupted by system crash.
4430 *
4431 * this function resumes merging reloc trees with corresponding fs trees.
4432 * this is important for keeping the sharing of tree blocks
4433 */
4434int btrfs_recover_relocation(struct btrfs_root *root)
4435{
4436 LIST_HEAD(reloc_roots);
4437 struct btrfs_key key;
4438 struct btrfs_root *fs_root;
4439 struct btrfs_root *reloc_root;
4440 struct btrfs_path *path;
4441 struct extent_buffer *leaf;
4442 struct reloc_control *rc = NULL;
4443 struct btrfs_trans_handle *trans;
4444 int ret;
4445 int err = 0;
4446
4447 path = btrfs_alloc_path();
4448 if (!path)
4449 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +01004450 path->reada = READA_BACK;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004451
4452 key.objectid = BTRFS_TREE_RELOC_OBJECTID;
4453 key.type = BTRFS_ROOT_ITEM_KEY;
4454 key.offset = (u64)-1;
4455
4456 while (1) {
4457 ret = btrfs_search_slot(NULL, root->fs_info->tree_root, &key,
4458 path, 0, 0);
4459 if (ret < 0) {
4460 err = ret;
4461 goto out;
4462 }
4463 if (ret > 0) {
4464 if (path->slots[0] == 0)
4465 break;
4466 path->slots[0]--;
4467 }
4468 leaf = path->nodes[0];
4469 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02004470 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004471
4472 if (key.objectid != BTRFS_TREE_RELOC_OBJECTID ||
4473 key.type != BTRFS_ROOT_ITEM_KEY)
4474 break;
4475
Miao Xiecb517ea2013-05-15 07:48:19 +00004476 reloc_root = btrfs_read_fs_root(root, &key);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004477 if (IS_ERR(reloc_root)) {
4478 err = PTR_ERR(reloc_root);
4479 goto out;
4480 }
4481
4482 list_add(&reloc_root->root_list, &reloc_roots);
4483
4484 if (btrfs_root_refs(&reloc_root->root_item) > 0) {
4485 fs_root = read_fs_root(root->fs_info,
4486 reloc_root->root_key.offset);
4487 if (IS_ERR(fs_root)) {
Yan, Zheng76dda932009-09-21 16:00:26 -04004488 ret = PTR_ERR(fs_root);
4489 if (ret != -ENOENT) {
4490 err = ret;
4491 goto out;
4492 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004493 ret = mark_garbage_root(reloc_root);
4494 if (ret < 0) {
4495 err = ret;
4496 goto out;
4497 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004498 }
4499 }
4500
4501 if (key.offset == 0)
4502 break;
4503
4504 key.offset--;
4505 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004506 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004507
4508 if (list_empty(&reloc_roots))
4509 goto out;
4510
Josef Bacika9995ee2013-05-31 13:04:36 -04004511 rc = alloc_reloc_control(root->fs_info);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004512 if (!rc) {
4513 err = -ENOMEM;
4514 goto out;
4515 }
4516
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004517 rc->extent_root = root->fs_info->extent_root;
4518
4519 set_reloc_control(rc);
4520
Josef Bacik7a7eaa42011-04-13 12:54:33 -04004521 trans = btrfs_join_transaction(rc->extent_root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00004522 if (IS_ERR(trans)) {
4523 unset_reloc_control(rc);
4524 err = PTR_ERR(trans);
4525 goto out_free;
4526 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004527
4528 rc->merge_reloc_tree = 1;
4529
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004530 while (!list_empty(&reloc_roots)) {
4531 reloc_root = list_entry(reloc_roots.next,
4532 struct btrfs_root, root_list);
4533 list_del(&reloc_root->root_list);
4534
4535 if (btrfs_root_refs(&reloc_root->root_item) == 0) {
4536 list_add_tail(&reloc_root->root_list,
4537 &rc->reloc_roots);
4538 continue;
4539 }
4540
4541 fs_root = read_fs_root(root->fs_info,
4542 reloc_root->root_key.offset);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004543 if (IS_ERR(fs_root)) {
4544 err = PTR_ERR(fs_root);
4545 goto out_free;
4546 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004547
Jeff Mahoneyffd7b332011-10-03 23:23:15 -04004548 err = __add_reloc_root(reloc_root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004549 BUG_ON(err < 0); /* -ENOMEM or logic error */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004550 fs_root->reloc_root = reloc_root;
4551 }
4552
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004553 err = btrfs_commit_transaction(trans, rc->extent_root);
4554 if (err)
4555 goto out_free;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004556
4557 merge_reloc_roots(rc);
4558
4559 unset_reloc_control(rc);
4560
Josef Bacik7a7eaa42011-04-13 12:54:33 -04004561 trans = btrfs_join_transaction(rc->extent_root);
Qu Wenruo62b99542016-08-15 10:36:51 +08004562 if (IS_ERR(trans)) {
Tsutomu Itoh3612b492011-01-25 02:51:38 +00004563 err = PTR_ERR(trans);
Qu Wenruo62b99542016-08-15 10:36:51 +08004564 goto out_free;
4565 }
4566 err = qgroup_fix_relocated_data_extents(trans, rc);
4567 if (err < 0) {
4568 btrfs_abort_transaction(trans, err);
4569 goto out_free;
4570 }
4571 err = btrfs_commit_transaction(trans, rc->extent_root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00004572out_free:
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004573 kfree(rc);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00004574out:
Liu Boaca1bba2013-03-04 16:25:37 +00004575 if (!list_empty(&reloc_roots))
4576 free_reloc_roots(&reloc_roots);
4577
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004578 btrfs_free_path(path);
4579
4580 if (err == 0) {
4581 /* cleanup orphan inode in data relocation tree */
4582 fs_root = read_fs_root(root->fs_info,
4583 BTRFS_DATA_RELOC_TREE_OBJECTID);
4584 if (IS_ERR(fs_root))
4585 err = PTR_ERR(fs_root);
Miao Xied7ce5842010-02-02 08:46:44 +00004586 else
Josef Bacik66b4ffd2011-01-31 16:22:42 -05004587 err = btrfs_orphan_cleanup(fs_root);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004588 }
4589 return err;
4590}
4591
4592/*
4593 * helper to add ordered checksum for data relocation.
4594 *
4595 * cloning checksum properly handles the nodatasum extents.
4596 * it also saves CPU time to re-calculate the checksum.
4597 */
4598int btrfs_reloc_clone_csums(struct inode *inode, u64 file_pos, u64 len)
4599{
4600 struct btrfs_ordered_sum *sums;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004601 struct btrfs_ordered_extent *ordered;
4602 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004603 int ret;
4604 u64 disk_bytenr;
Josef Bacik4577b012013-09-27 09:33:09 -04004605 u64 new_bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004606 LIST_HEAD(list);
4607
4608 ordered = btrfs_lookup_ordered_extent(inode, file_pos);
4609 BUG_ON(ordered->file_offset != file_pos || ordered->len != len);
4610
4611 disk_bytenr = file_pos + BTRFS_I(inode)->index_cnt;
4612 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, disk_bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01004613 disk_bytenr + len - 1, &list, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004614 if (ret)
4615 goto out;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004616
4617 while (!list_empty(&list)) {
4618 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
4619 list_del_init(&sums->list);
4620
Josef Bacik4577b012013-09-27 09:33:09 -04004621 /*
4622 * We need to offset the new_bytenr based on where the csum is.
4623 * We need to do this because we will read in entire prealloc
4624 * extents but we may have written to say the middle of the
4625 * prealloc extent, so we need to make sure the csum goes with
4626 * the right disk offset.
4627 *
4628 * We can do this because the data reloc inode refers strictly
4629 * to the on disk bytes, so we don't have to worry about
4630 * disk_len vs real len like with real inodes since it's all
4631 * disk length.
4632 */
4633 new_bytenr = ordered->start + (sums->bytenr - disk_bytenr);
4634 sums->bytenr = new_bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004635
4636 btrfs_add_ordered_sum(inode, ordered, sums);
4637 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004638out:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004639 btrfs_put_ordered_extent(ordered);
Andi Kleen411fc6b2010-10-29 15:14:31 -04004640 return ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004641}
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004642
Josef Bacik83d4cfd2013-08-30 15:09:51 -04004643int btrfs_reloc_cow_block(struct btrfs_trans_handle *trans,
4644 struct btrfs_root *root, struct extent_buffer *buf,
4645 struct extent_buffer *cow)
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004646{
4647 struct reloc_control *rc;
4648 struct backref_node *node;
4649 int first_cow = 0;
4650 int level;
Josef Bacik83d4cfd2013-08-30 15:09:51 -04004651 int ret = 0;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004652
4653 rc = root->fs_info->reloc_ctl;
4654 if (!rc)
Josef Bacik83d4cfd2013-08-30 15:09:51 -04004655 return 0;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004656
4657 BUG_ON(rc->stage == UPDATE_DATA_PTRS &&
4658 root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID);
4659
Wang Shilongc974c462013-12-11 19:29:51 +08004660 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
4661 if (buf == root->node)
4662 __update_reloc_root(root, cow->start);
4663 }
4664
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004665 level = btrfs_header_level(buf);
4666 if (btrfs_header_generation(buf) <=
4667 btrfs_root_last_snapshot(&root->root_item))
4668 first_cow = 1;
4669
4670 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID &&
4671 rc->create_reloc_tree) {
4672 WARN_ON(!first_cow && level == 0);
4673
4674 node = rc->backref_cache.path[level];
4675 BUG_ON(node->bytenr != buf->start &&
4676 node->new_bytenr != buf->start);
4677
4678 drop_node_buffer(node);
4679 extent_buffer_get(cow);
4680 node->eb = cow;
4681 node->new_bytenr = cow->start;
4682
4683 if (!node->pending) {
4684 list_move_tail(&node->list,
4685 &rc->backref_cache.pending[level]);
4686 node->pending = 1;
4687 }
4688
4689 if (first_cow)
4690 __mark_block_processed(rc, node);
4691
4692 if (first_cow && level > 0)
4693 rc->nodes_relocated += buf->len;
4694 }
4695
Josef Bacik83d4cfd2013-08-30 15:09:51 -04004696 if (level == 0 && first_cow && rc->stage == UPDATE_DATA_PTRS)
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004697 ret = replace_file_extents(trans, rc, root, cow);
Josef Bacik83d4cfd2013-08-30 15:09:51 -04004698 return ret;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004699}
4700
4701/*
4702 * called before creating snapshot. it calculates metadata reservation
Nicholas D Steeves01327612016-05-19 21:18:45 -04004703 * required for relocating tree blocks in the snapshot
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004704 */
Zhaolei147d2562015-08-06 20:58:11 +08004705void btrfs_reloc_pre_snapshot(struct btrfs_pending_snapshot *pending,
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004706 u64 *bytes_to_reserve)
4707{
4708 struct btrfs_root *root;
4709 struct reloc_control *rc;
4710
4711 root = pending->root;
4712 if (!root->reloc_root)
4713 return;
4714
4715 rc = root->fs_info->reloc_ctl;
4716 if (!rc->merge_reloc_tree)
4717 return;
4718
4719 root = root->reloc_root;
4720 BUG_ON(btrfs_root_refs(&root->root_item) == 0);
4721 /*
4722 * relocation is in the stage of merging trees. the space
4723 * used by merging a reloc tree is twice the size of
4724 * relocated tree nodes in the worst case. half for cowing
4725 * the reloc tree, half for cowing the fs tree. the space
4726 * used by cowing the reloc tree will be freed after the
4727 * tree is dropped. if we create snapshot, cowing the fs
4728 * tree may use more space than it frees. so we need
4729 * reserve extra space.
4730 */
4731 *bytes_to_reserve += rc->nodes_relocated;
4732}
4733
4734/*
4735 * called after snapshot is created. migrate block reservation
4736 * and create reloc root for the newly created snapshot
4737 */
Jeff Mahoney49b25e02012-03-01 17:24:58 +01004738int btrfs_reloc_post_snapshot(struct btrfs_trans_handle *trans,
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004739 struct btrfs_pending_snapshot *pending)
4740{
4741 struct btrfs_root *root = pending->root;
4742 struct btrfs_root *reloc_root;
4743 struct btrfs_root *new_root;
4744 struct reloc_control *rc;
4745 int ret;
4746
4747 if (!root->reloc_root)
Jeff Mahoney49b25e02012-03-01 17:24:58 +01004748 return 0;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004749
4750 rc = root->fs_info->reloc_ctl;
4751 rc->merging_rsv_size += rc->nodes_relocated;
4752
4753 if (rc->merge_reloc_tree) {
4754 ret = btrfs_block_rsv_migrate(&pending->block_rsv,
4755 rc->block_rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04004756 rc->nodes_relocated, 1);
Jeff Mahoney49b25e02012-03-01 17:24:58 +01004757 if (ret)
4758 return ret;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004759 }
4760
4761 new_root = pending->snap;
4762 reloc_root = create_reloc_root(trans, root->reloc_root,
4763 new_root->root_key.objectid);
Jeff Mahoney49b25e02012-03-01 17:24:58 +01004764 if (IS_ERR(reloc_root))
4765 return PTR_ERR(reloc_root);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004766
Jeff Mahoneyffd7b332011-10-03 23:23:15 -04004767 ret = __add_reloc_root(reloc_root);
4768 BUG_ON(ret < 0);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004769 new_root->reloc_root = reloc_root;
4770
Jeff Mahoney49b25e02012-03-01 17:24:58 +01004771 if (rc->create_reloc_tree)
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004772 ret = clone_backref_node(trans, rc, root, reloc_root);
Jeff Mahoney49b25e02012-03-01 17:24:58 +01004773 return ret;
Yan, Zheng3fd0a552010-05-16 10:49:59 -04004774}